Kea 3.3.4
d2_process.cc
Go to the documentation of this file.
1// Copyright (C) 2013-2026 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
11#include <config/command_mgr.h>
14#include <d2/d2_controller.h>
15#include <d2/d2_process.h>
16#include <d2srv/d2_cfg_mgr.h>
17#include <d2srv/d2_log.h>
18#include <d2srv/d2_stats.h>
19#include <d2srv/d2_tsig_key.h>
20#include <hooks/hooks.h>
21#include <hooks/hooks_manager.h>
22#include <util/filesystem.h>
23
24using namespace isc::asiolink;
25using namespace isc::config;
26using namespace isc::data;
27using namespace isc::hooks;
28using namespace isc::process;
29using namespace isc::util::file;
30
31namespace {
32
34struct D2ProcessHooks {
35 int hooks_index_d2_srv_configured_;
36
38 D2ProcessHooks() {
39 hooks_index_d2_srv_configured_ = HooksManager::registerHook("d2_srv_configured");
40 }
41
42};
43
44// Declare a Hooks object. As this is outside any function or method, it
45// will be instantiated (and the constructor run) when the module is loaded.
46// As a result, the hook indexes will be defined before any method in this
47// module is called.
48D2ProcessHooks Hooks;
49
50}
51
52namespace isc {
53namespace d2 {
54
55// Setting to 80% for now. This is an arbitrary choice and should probably
56// be configurable.
57const unsigned int D2Process::QUEUE_RESTART_PERCENT = 80;
58
59// IOService run time duration is 100 ms.
60const unsigned int D2Process::IO_SERVICE_RUN_TIME_USECS = (100 * 1000);
61
62D2Process::D2Process(const char* name, const asiolink::IOServicePtr& io_service)
63 : DProcessBase(name, io_service, DCfgMgrBasePtr(new D2CfgMgr())),
64 reconf_queue_flag_(false), shutdown_type_(SD_NORMAL) {
66 // Instantiate queue manager. Note that queue manager does not start
67 // listening at this point. That can only occur after configuration has
68 // been received. This means that until we receive the configuration,
69 // D2 will neither receive nor process NameChangeRequests.
70 // Pass in IOService for NCR IO event processing.
71 queue_mgr_.reset(new D2QueueMgr(getIOService(), tmp));
72
73 // Instantiate update manager.
74 // Pass in both queue manager and configuration manager.
75 // Pass in IOService for DNS update transaction IO event processing.
76 update_mgr_.reset(new D2UpdateMgr(queue_mgr_, tmp, getIOService()));
77
78 // Initialize stats manager.
80};
81
82void
84 using namespace isc::config;
85 // Command managers use IO service to run asynchronous socket operations.
88
89 // Set the HTTP authentication default realm.
91
92 // Set the HTTP supported service.
94
95 // D2 server does not use the interface manager.
98};
99
100void
102 LOG_INFO(d2_logger, DHCP_DDNS_STARTED).arg(VERSION);
103
106 }
107
108 D2ControllerPtr controller =
109 boost::dynamic_pointer_cast<D2Controller>(D2Controller::instance());
110 try {
111 // Now logging was initialized so commands can be registered.
112 controller->registerCommands();
113
114 // Loop forever until we are allowed to shutdown.
115 while (!canShutdown()) {
116 // Check on the state of the request queue. Take any
117 // actions necessary regarding it.
119
120 // Give update manager a time slice to queue new jobs and
121 // process finished ones.
122 update_mgr_->sweep();
123
124 // Wait on IO event(s) - block until one or more of the following
125 // has occurred:
126 // a. NCR message has been received
127 // b. Transaction IO has completed
128 // c. Interval timer expired
129 // d. Control channel event
130 // e. Something stopped IO service (runIO returns 0)
131 if (runIO() == 0) {
132 // Pretty sure this amounts to an unexpected stop and we
133 // should bail out now. Normal shutdowns do not utilize
134 // stopping the IOService.
136 "Primary IO service stopped unexpectedly");
137 }
138 }
139 } catch (const std::exception& ex) {
140 LOG_FATAL(d2_logger, DHCP_DDNS_FAILED).arg(ex.what());
141 controller->deregisterCommands();
143 "Process run method failed: " << ex.what());
144 }
145
149
150 controller->deregisterCommands();
151
153
154};
155
156size_t
158 // We want to process all ready handlers on both the managed IOServices
159 // and the main IOservice. If no handlers were executed on any of the
160 // IOServices will wait on the main IOService until at least one handler
161 // executes or we time out.
162 size_t cnt = IOServiceMgr::instance().pollIOServices();
163 cnt += getIOService()->poll();
164 if (!cnt) {
165 // Polling ran no handlers so either none are ready or the service has been
166 // stopped. Either way, call runOneFor() to wait for a IO event on the
167 // main service. If the service is stopped it will return immediately
168 // with a cnt of zero and timed_out set to false.
169 bool timed_out;
170 cnt = getIOService()->runOneFor(IO_SERVICE_RUN_TIME_USECS, timed_out);
171 if (timed_out) {
172 // Return 1 so caller knows the service has not stopped.
173 return (1);
174 }
175 }
176
177 return (cnt);
178}
179
180bool
182 bool all_clear = false;
183
184 // If we have been told to shutdown, find out if we are ready to do so.
185 if (shouldShutdown()) {
186 switch (shutdown_type_) {
187 case SD_NORMAL:
188 // For a normal shutdown we need to stop the queue manager but
189 // wait until we have finished all the transactions in progress.
190 all_clear = (((queue_mgr_->getMgrState() != D2QueueMgr::RUNNING) &&
191 (queue_mgr_->getMgrState() != D2QueueMgr::STOPPING))
192 && (update_mgr_->getTransactionCount() == 0));
193 break;
194
195 case SD_DRAIN_FIRST:
196 // For a drain first shutdown we need to stop the queue manager but
197 // process all of the requests in the receive queue first.
198 all_clear = (((queue_mgr_->getMgrState() != D2QueueMgr::RUNNING) &&
199 (queue_mgr_->getMgrState() != D2QueueMgr::STOPPING))
200 && (queue_mgr_->getQueueSize() == 0)
201 && (update_mgr_->getTransactionCount() == 0));
202 break;
203
204 case SD_NOW:
205 // Get out right now, no niceties.
206 all_clear = true;
207 break;
208
209 default:
210 // shutdown_type_ is an enum and should only be one of the above.
211 // if its getting through to this, something is whacked.
212 break;
213 }
214
215 if (all_clear) {
218 .arg(getShutdownTypeStr(shutdown_type_));
219 }
220 }
221
222 return (all_clear);
223}
224
229 .arg(args ? args->str() : "(no arguments)");
230
231 // Default shutdown type is normal.
232 std::string type_str(getShutdownTypeStr(SD_NORMAL));
233 shutdown_type_ = SD_NORMAL;
234
235 if (args) {
236 if ((args->getType() == isc::data::Element::map) &&
237 args->contains("type")) {
238 type_str = args->get("type")->stringValue();
239
240 if (type_str == getShutdownTypeStr(SD_NORMAL)) {
241 shutdown_type_ = SD_NORMAL;
242 } else if (type_str == getShutdownTypeStr(SD_DRAIN_FIRST)) {
243 shutdown_type_ = SD_DRAIN_FIRST;
244 } else if (type_str == getShutdownTypeStr(SD_NOW)) {
245 shutdown_type_ = SD_NOW;
246 } else {
247 setShutdownFlag(false);
249 "Invalid Shutdown type: " +
250 type_str));
251 }
252 }
253 }
254
255 // Set the base class's shutdown flag.
256 setShutdownFlag(true);
258 "Shutdown initiated, type is: " +
259 type_str));
260}
261
265 .arg(check_only ? "check" : "update")
266 .arg(getD2CfgMgr()->redactConfig(config_set)->str());
267
269 answer = getCfgMgr()->simpleParseConfig(config_set, check_only,
270 std::bind(&D2Process::reconfigureCommandChannel, this));
271 if (check_only) {
272 return (answer);
273 }
274
275 int rcode = 0;
277 comment = isc::config::parseAnswer(rcode, answer);
278
279 if (rcode) {
280 // Non-zero means we got an invalid configuration, take no further
281 // action. In integrated mode, this will send a failed response back
282 // to the configuration backend.
283 reconf_queue_flag_ = false;
284 return (answer);
285 }
286
287 // Set the reconf_queue_flag to indicate that we need to reconfigure
288 // the queue manager. Reconfiguring the queue manager may be asynchronous
289 // and require one or more events to occur, therefore we set a flag
290 // indicating it needs to be done but we cannot do it here. It must
291 // be done over time, while events are being processed. Remember that
292 // the method we are in now is invoked as part of the configuration event
293 // callback. This means you can't wait for events here, you are already
294 // in one.
298 reconf_queue_flag_ = true;
299
300 // This hook point notifies hooks libraries that the configuration of the
301 // D2 server has completed. It provides the hook library with the pointer
302 // to the common IO service object, new server configuration in the JSON
303 // format and with the pointer to the configuration storage where the
304 // parsed configuration is stored.
305 std::string error("");
306 if (HooksManager::calloutsPresent(Hooks.hooks_index_d2_srv_configured_)) {
308
309 callout_handle->setArgument("io_context", getIOService());
310 callout_handle->setArgument("json_config", config_set);
311 callout_handle->setArgument("server_config",
312 getD2CfgMgr()->getD2CfgContext());
313 callout_handle->setArgument("error", error);
314
315 HooksManager::callCallouts(Hooks.hooks_index_d2_srv_configured_,
316 *callout_handle);
317
318 // The config can be rejected by a hook.
319 if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_DROP) {
320 callout_handle->getArgument("error", error);
322 .arg(error);
323 reconf_queue_flag_ = false;
325 return (answer);
326 }
327 }
328
330 try {
331 // Handle events registered by hooks using external IOService objects.
333 } catch (const std::exception& ex) {
334 if (rcode == CONTROL_RESULT_FATAL_ERROR) {
335 return (answer);
336 }
337 std::ostringstream err;
338 err << "Error initializing hooks: "
339 << ex.what();
341 }
342
343 // If we are here, configuration was valid, at least it parsed correctly
344 // and therefore contained no invalid values.
345 // Return the success answer from above.
346 return (answer);
347}
348
349void
351 switch (queue_mgr_->getMgrState()){
353 if (reconf_queue_flag_ || shouldShutdown()) {
358 try {
361 .arg(reconf_queue_flag_ ? "reconfiguration" : "shutdown");
362 queue_mgr_->stopListening();
363 } catch (const isc::Exception& ex) {
364 // It is very unlikely that we would experience an error
365 // here, but theoretically possible.
367 .arg(ex.what());
368 }
369 }
370 break;
371
376 size_t threshold = (((queue_mgr_->getMaxQueueSize()
377 * QUEUE_RESTART_PERCENT)) / 100);
378 if (queue_mgr_->getQueueSize() <= threshold) {
380 .arg(threshold).arg(queue_mgr_->getMaxQueueSize());
381 try {
382 queue_mgr_->startListening();
383 } catch (const isc::Exception& ex) {
385 .arg(ex.what());
386 }
387 }
388
389 break;
390 }
391
400 if (!shouldShutdown()) {
403 }
404 break;
405
411 break;
412
413 default:
414 // If the reconfigure flag is set, then we are in a state now where
415 // we can do the reconfigure. In other words, we aren't RUNNING or
416 // STOPPING.
417 if (reconf_queue_flag_) {
421 }
422 break;
423 }
424}
425
426void
428 // Set reconfigure flag to false. We are only here because we have
429 // a valid configuration to work with so if we fail below, it will be
430 // an operational issue, such as a busy IP address. That will leave
431 // queue manager in INITTED state, which is fine.
432 // What we don't want is to continually attempt to reconfigure so set
433 // the flag false now.
437 reconf_queue_flag_ = false;
438 try {
439 // Wipe out the current listener.
440 queue_mgr_->removeListener();
441
442 // Get the configuration parameters that affect Queue Manager.
443 const D2ParamsPtr& d2_params = getD2CfgMgr()->getD2Params();
444
447 std::string ip_address = d2_params->getIpAddress().toText();
448 if (ip_address == "0.0.0.0" || ip_address == "::") {
450 } else if (ip_address != "127.0.0.1" && ip_address != "::1") {
452 }
453
454 // Instantiate the listener.
455 if (d2_params->getNcrProtocol() == dhcp_ddns::NCR_UDP) {
456 queue_mgr_->initUDPListener(d2_params->getIpAddress(),
457 d2_params->getPort(),
458 d2_params->getNcrFormat(), true);
459 } else {
461 // We should never get this far but if we do deal with it.
462 isc_throw(DProcessBaseError, "Unsupported NCR listener protocol:"
463 << dhcp_ddns::ncrProtocolToString(d2_params->
464 getNcrProtocol()));
465 }
466
467 // Iterate over queue entries and redo domain matching.
468 queue_mgr_->refreshQueuedMatching();
469
470 // Now start it. This assumes that starting is a synchronous,
471 // blocking call that executes quickly.
474 queue_mgr_->startListening();
475 } catch (const isc::Exception& ex) {
476 // Queue manager failed to initialize and therefore not listening.
477 // This is most likely due to an unavailable IP address or port,
478 // which is a configuration issue.
480 }
481}
482
484 queue_mgr_->stopListening();
485 getIOService()->stopAndPoll();
486 queue_mgr_->removeListener();
487}
488
491 // The base class gives a base class pointer to our configuration manager.
492 // Since we are D2, and we need D2 specific extensions, we need a pointer
493 // to D2CfgMgr for some things.
494 return (boost::dynamic_pointer_cast<D2CfgMgr>(getCfgMgr()));
495}
496
498 const char* str;
499 switch (type) {
500 case SD_NORMAL:
501 str = "normal";
502 break;
503 case SD_DRAIN_FIRST:
504 str = "drain_first";
505 break;
506 case SD_NOW:
507 str = "now";
508 break;
509 default:
510 str = "invalid";
511 break;
512 }
513
514 return (str);
515}
516
517void
519 // Get new Unix socket configuration.
520 ConstElementPtr unix_config =
521 getD2CfgMgr()->getUnixControlSocketInfo();
522
523 // Determine if the socket configuration has changed. It has if
524 // both old and new configuration is specified but respective
525 // data elements aren't equal.
526 bool sock_changed = (unix_config && current_unix_control_socket_ &&
527 !unix_config->equals(*current_unix_control_socket_));
528
529 // If the previous or new socket configuration doesn't exist or
530 // the new configuration differs from the old configuration we
531 // close the existing socket and open a new socket as appropriate.
532 // Note that closing an existing socket means the client will not
533 // receive the configuration result.
534 if (!unix_config || !current_unix_control_socket_ || sock_changed) {
535 // Open the new sockets and close old ones, keeping reused.
536 if (unix_config) {
538 } else if (current_unix_control_socket_) {
540 }
541 }
542
543 // Commit the new socket configuration.
544 current_unix_control_socket_ = unix_config;
545
546 // Get new HTTP/HTTPS socket configuration.
547 ConstElementPtr http_config =
548 getD2CfgMgr()->getHttpControlSocketInfo();
549
550 // Open the new sockets and close old ones, keeping reused.
551 if (http_config) {
553 } else if (current_http_control_socket_) {
555 }
556
557 // Commit the new socket configuration.
558 current_http_control_socket_ = http_config;
559}
560
561} // namespace isc::d2
562} // namespace isc
@ NEXT_STEP_DROP
drop the packet
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
static std::string DEFAULT_AUTHENTICATION_REALM
Default HTTP authentication realm.
static std::string SUPPORTED_SERVICE
Supported service.
void closeCommandSockets()
Close http control sockets.
void addExternalSockets(bool use_external=true)
Use external sockets flag.
static HttpCommandMgr & instance()
HttpCommandMgr is a singleton class.
void setIOService(const asiolink::IOServicePtr &io_service)
Sets IO service to be used by the http command manager.
void openCommandSockets(const isc::data::ConstElementPtr config)
Open http control sockets using configuration.
static UnixCommandMgr & instance()
UnixCommandMgr is a singleton class.
void setIOService(const asiolink::IOServicePtr &io_service)
Sets IO service to be used by the unix command manager.
void openCommandSockets(const isc::data::ConstElementPtr config)
Opens unix control socket with parameters specified in socket_info (required parameters: socket-type:...
void closeCommandSockets()
Shuts down any open unix control sockets.
void addExternalSockets(bool use_external=true)
Use external sockets flag.
DHCP-DDNS Configuration Manager.
Definition d2_cfg_mgr.h:189
static process::DControllerBasePtr & instance()
Static singleton instance method.
D2Process(const char *name, const asiolink::IOServicePtr &io_service)
Constructor.
Definition d2_process.cc:62
static const unsigned int IO_SERVICE_RUN_TIME_USECS
Amount of time to allow the main IOSerivce to wait for ready handlers before timimg out in microsecon...
Definition d2_process.h:54
static const unsigned int QUEUE_RESTART_PERCENT
Defines the point at which to resume receiving requests.
Definition d2_process.h:50
virtual bool canShutdown() const
Indicates whether or not the process can perform a shutdown.
virtual void checkQueueStatus()
Monitors current queue manager state, takes action accordingly.
virtual ~D2Process()
Destructor.
virtual void run()
Implements the process's event loop.
virtual void init()
Called after instantiation to perform initialization unique to D2.
Definition d2_process.cc:83
D2CfgMgrPtr getD2CfgMgr()
Returns a pointer to the configuration manager.
virtual isc::data::ConstElementPtr configure(isc::data::ConstElementPtr config_set, bool check_only=false)
Processes the given configuration.
void reconfigureCommandChannel()
(Re-)Configure the command channel.
virtual void reconfigureQueueMgr()
Initializes then starts the queue manager.
ShutdownType
Defines the shutdown types supported by D2Process.
Definition d2_process.h:38
virtual isc::data::ConstElementPtr shutdown(isc::data::ConstElementPtr args)
Initiates the D2Process shutdown process.
static const char * getShutdownTypeStr(const ShutdownType &type)
Returns a text label for the given shutdown type.
virtual size_t runIO()
Allows IO processing to run until at least callback is invoked.
D2QueueMgr creates and manages a queue of DNS update requests.
static void init()
Initialize D2 statistics.
Definition d2_stats.cc:48
D2UpdateMgr creates and manages update transactions.
static int registerHook(const std::string &name)
Register Hook.
static bool calloutsPresent(int index)
Are callouts present?
static boost::shared_ptr< CalloutHandle > createCalloutHandle()
Return callout handle.
static void callCallouts(int index, CalloutHandle &handle)
Calls the callouts for a given hook.
Exception thrown if the process encountered an operational error.
Definition d_process.h:24
void setShutdownFlag(bool value)
Sets the process shut down flag to the given value.
Definition d_process.h:162
DProcessBase(const char *app_name, asiolink::IOServicePtr io_service, DCfgMgrBasePtr cfg_mgr)
Constructor.
Definition d_process.h:87
bool shouldShutdown() const
Checks if the process has been instructed to shut down.
Definition d_process.h:155
asiolink::IOServicePtr & getIOService()
Fetches the controller's IOService.
Definition d_process.h:176
DCfgMgrBasePtr & getCfgMgr()
Fetches the process's configuration manager.
Definition d_process.h:191
static bool shouldEnforceSecurity()
Indicates security checks should be enforced.
This file contains several functions and constants that are used for handling commands and responses ...
Dhcp4Hooks Hooks
Definition dhcp4_srv.cc:213
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition macros.h:32
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition macros.h:20
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
Definition macros.h:26
#define LOG_FATAL(LOGGER, MESSAGE)
Macro to conveniently test fatal output and log it.
Definition macros.h:38
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
Definition macros.h:14
ConstElementPtr parseAnswer(int &rcode, const ConstElementPtr &msg)
Parses a standard config/command level answer and returns arguments or text status code.
const int CONTROL_RESULT_ERROR
Status code indicating a general failure.
ConstElementPtr createAnswer(const int status_code, const std::string &text, const ConstElementPtr &arg)
Creates a standard config/command level answer message.
ConstElementPtr createAnswer()
Creates a standard config/command level success answer message (i.e.
const int CONTROL_RESULT_FATAL_ERROR
Status code indicating that the command was unsuccessful and the configuration could not be reverted ...
const int CONTROL_RESULT_SUCCESS
Status code indicating a successful operation.
boost::shared_ptr< D2CfgMgr > D2CfgMgrPtr
Defines a shared pointer to D2CfgMgr.
Definition d2_cfg_mgr.h:389
const isc::log::MessageID DHCP_DDNS_SECURITY_CHECKS_DISABLED
Definition d2_messages.h:86
const isc::log::MessageID DHCP_DDNS_QUEUE_MGR_RECOVERING
Definition d2_messages.h:58
const isc::log::MessageID DHCP_DDNS_QUEUE_MGR_STOP_ERROR
Definition d2_messages.h:66
const isc::log::MessageID DHCP_DDNS_FAILED
Definition d2_messages.h:20
const isc::log::MessageID DHCP_DDNS_LISTENING_ON_ALL_INTERFACES
Definition d2_messages.h:48
const isc::log::MessageID DHCP_DDNS_QUEUE_MGR_START_ERROR
Definition d2_messages.h:63
const isc::log::MessageID DHCP_DDNS_SHUTDOWN_COMMAND
Definition d2_messages.h:87
const isc::log::MessageID DHCP_DDNS_CONFIGURE
Definition d2_messages.h:15
const isc::log::MessageID DHCP_DDNS_CLEARED_FOR_SHUTDOWN
Definition d2_messages.h:14
const isc::log::MessageID DHCP_DDNS_QUEUE_MGR_RECONFIGURING
Definition d2_messages.h:57
const isc::log::MessageID DHCP_DDNS_RUN_EXIT
Definition d2_messages.h:85
const isc::log::MessageID DHCP_DDNS_CONFIGURED_CALLOUT_DROP
Definition d2_messages.h:16
const isc::log::MessageID DHCP_DDNS_QUEUE_MGR_RESUME_ERROR
Definition d2_messages.h:60
const isc::log::MessageID DHCP_DDNS_STARTED
Definition d2_messages.h:88
boost::shared_ptr< D2Controller > D2ControllerPtr
Pointer to a process controller.
isc::log::Logger d2_logger("dhcpddns")
Defines the logger used within D2.
Definition d2_log.h:18
const isc::log::MessageID DHCP_DDNS_QUEUE_MGR_RESUMING
Definition d2_messages.h:61
boost::shared_ptr< D2Params > D2ParamsPtr
Defines a pointer for D2Params instances.
Definition d2_config.h:257
const isc::log::MessageID DHCP_DDNS_QUEUE_MGR_STOPPING
Definition d2_messages.h:65
const isc::log::MessageID DHCP_DDNS_NOT_ON_LOOPBACK
Definition d2_messages.h:49
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
std::string ncrProtocolToString(NameChangeProtocol protocol)
Function which converts NameChangeProtocol enums to text labels.
Definition ncr_io.cc:36
boost::shared_ptr< CalloutHandle > CalloutHandlePtr
A shared pointer to a CalloutHandle object.
const int DBGLVL_TRACE_BASIC
Trace basic operations.
const int DBGLVL_START_SHUT
This is given a value of 0 as that is the level selected if debugging is enabled without giving a lev...
ConstElementPtr redactConfig(ConstElementPtr const &element, list< string > const &json_path, string obscure, unsigned max_nesting_depth)
Redact a configuration.
boost::shared_ptr< DCfgMgrBase > DCfgMgrBasePtr
Defines a shared pointer to DCfgMgrBase.
Definition d_cfg_mgr.h:244
Defines the logger used by the top-level component of kea-lfc.