Kea 3.0.0
d2_process.cc
Go to the documentation of this file.
1// Copyright (C) 2013-2025 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
59D2Process::D2Process(const char* name, const asiolink::IOServicePtr& io_service)
60 : DProcessBase(name, io_service, DCfgMgrBasePtr(new D2CfgMgr())),
61 reconf_queue_flag_(false), shutdown_type_(SD_NORMAL) {
62
63 // Instantiate queue manager. Note that queue manager does not start
64 // listening at this point. That can only occur after configuration has
65 // been received. This means that until we receive the configuration,
66 // D2 will neither receive nor process NameChangeRequests.
67 // Pass in IOService for NCR IO event processing.
68 queue_mgr_.reset(new D2QueueMgr(getIOService()));
69
70 // Instantiate update manager.
71 // Pass in both queue manager and configuration manager.
72 // Pass in IOService for DNS update transaction IO event processing.
74 update_mgr_.reset(new D2UpdateMgr(queue_mgr_, tmp, getIOService()));
75
76 // Initialize stats manager.
78};
79
80void
82 using namespace isc::config;
83 // Command managers use IO service to run asynchronous socket operations.
86
87 // Set the HTTP authentication default realm.
89
90 // D2 server does not use the interface manager.
93};
94
95void
98
101 }
102
103 D2ControllerPtr controller =
104 boost::dynamic_pointer_cast<D2Controller>(D2Controller::instance());
105 try {
106 // Now logging was initialized so commands can be registered.
107 controller->registerCommands();
108
109 // Loop forever until we are allowed to shutdown.
110 while (!canShutdown()) {
111 // Check on the state of the request queue. Take any
112 // actions necessary regarding it.
114
115 // Give update manager a time slice to queue new jobs and
116 // process finished ones.
117 update_mgr_->sweep();
118
119 // Wait on IO event(s) - block until one or more of the following
120 // has occurred:
121 // a. NCR message has been received
122 // b. Transaction IO has completed
123 // c. Interval timer expired
124 // d. Control channel event
125 // e. Something stopped IO service (runIO returns 0)
126 if (runIO() == 0) {
127 // Pretty sure this amounts to an unexpected stop and we
128 // should bail out now. Normal shutdowns do not utilize
129 // stopping the IOService.
131 "Primary IO service stopped unexpectedly");
132 }
133 }
134 } catch (const std::exception& ex) {
135 LOG_FATAL(d2_logger, DHCP_DDNS_FAILED).arg(ex.what());
136 controller->deregisterCommands();
138 "Process run method failed: " << ex.what());
139 }
140
144
145 controller->deregisterCommands();
146
148
149};
150
151size_t
153 // Handle events registered by hooks using external IOService objects.
155 // We want to block until at least one handler is called.
156 // Poll runs all that are ready. If none are ready it returns immediately
157 // with a count of zero.
158 size_t cnt = getIOService()->poll();
159 if (!cnt) {
160 // Poll ran no handlers either none are ready or the service has been
161 // stopped. Either way, call runOne to wait for a IO event. If the
162 // service is stopped it will return immediately with a cnt of zero.
163 cnt = getIOService()->runOne();
164 }
165 return (cnt);
166}
167
168bool
170 bool all_clear = false;
171
172 // If we have been told to shutdown, find out if we are ready to do so.
173 if (shouldShutdown()) {
174 switch (shutdown_type_) {
175 case SD_NORMAL:
176 // For a normal shutdown we need to stop the queue manager but
177 // wait until we have finished all the transactions in progress.
178 all_clear = (((queue_mgr_->getMgrState() != D2QueueMgr::RUNNING) &&
179 (queue_mgr_->getMgrState() != D2QueueMgr::STOPPING))
180 && (update_mgr_->getTransactionCount() == 0));
181 break;
182
183 case SD_DRAIN_FIRST:
184 // For a drain first shutdown we need to stop the queue manager but
185 // process all of the requests in the receive queue first.
186 all_clear = (((queue_mgr_->getMgrState() != D2QueueMgr::RUNNING) &&
187 (queue_mgr_->getMgrState() != D2QueueMgr::STOPPING))
188 && (queue_mgr_->getQueueSize() == 0)
189 && (update_mgr_->getTransactionCount() == 0));
190 break;
191
192 case SD_NOW:
193 // Get out right now, no niceties.
194 all_clear = true;
195 break;
196
197 default:
198 // shutdown_type_ is an enum and should only be one of the above.
199 // if its getting through to this, something is whacked.
200 break;
201 }
202
203 if (all_clear) {
206 .arg(getShutdownTypeStr(shutdown_type_));
207 }
208 }
209
210 return (all_clear);
211}
212
217 .arg(args ? args->str() : "(no arguments)");
218
219 // Default shutdown type is normal.
220 std::string type_str(getShutdownTypeStr(SD_NORMAL));
221 shutdown_type_ = SD_NORMAL;
222
223 if (args) {
224 if ((args->getType() == isc::data::Element::map) &&
225 args->contains("type")) {
226 type_str = args->get("type")->stringValue();
227
228 if (type_str == getShutdownTypeStr(SD_NORMAL)) {
229 shutdown_type_ = SD_NORMAL;
230 } else if (type_str == getShutdownTypeStr(SD_DRAIN_FIRST)) {
231 shutdown_type_ = SD_DRAIN_FIRST;
232 } else if (type_str == getShutdownTypeStr(SD_NOW)) {
233 shutdown_type_ = SD_NOW;
234 } else {
235 setShutdownFlag(false);
237 "Invalid Shutdown type: " +
238 type_str));
239 }
240 }
241 }
242
243 // Set the base class's shutdown flag.
244 setShutdownFlag(true);
246 "Shutdown initiated, type is: " +
247 type_str));
248}
249
253 .arg(check_only ? "check" : "update")
254 .arg(getD2CfgMgr()->redactConfig(config_set)->str());
255
257 answer = getCfgMgr()->simpleParseConfig(config_set, check_only,
258 std::bind(&D2Process::reconfigureCommandChannel, this));
259 if (check_only) {
260 return (answer);
261 }
262
263 int rcode = 0;
265 comment = isc::config::parseAnswer(rcode, answer);
266
267 if (rcode) {
268 // Non-zero means we got an invalid configuration, take no further
269 // action. In integrated mode, this will send a failed response back
270 // to the configuration backend.
271 reconf_queue_flag_ = false;
272 return (answer);
273 }
274
275 // Set the reconf_queue_flag to indicate that we need to reconfigure
276 // the queue manager. Reconfiguring the queue manager may be asynchronous
277 // and require one or more events to occur, therefore we set a flag
278 // indicating it needs to be done but we cannot do it here. It must
279 // be done over time, while events are being processed. Remember that
280 // the method we are in now is invoked as part of the configuration event
281 // callback. This means you can't wait for events here, you are already
282 // in one.
286 reconf_queue_flag_ = true;
287
288 // This hook point notifies hooks libraries that the configuration of the
289 // D2 server has completed. It provides the hook library with the pointer
290 // to the common IO service object, new server configuration in the JSON
291 // format and with the pointer to the configuration storage where the
292 // parsed configuration is stored.
293 std::string error("");
294 if (HooksManager::calloutsPresent(Hooks.hooks_index_d2_srv_configured_)) {
296
297 callout_handle->setArgument("io_context", getIOService());
298 callout_handle->setArgument("json_config", config_set);
299 callout_handle->setArgument("server_config",
300 getD2CfgMgr()->getD2CfgContext());
301 callout_handle->setArgument("error", error);
302
303 HooksManager::callCallouts(Hooks.hooks_index_d2_srv_configured_,
304 *callout_handle);
305
306 // The config can be rejected by a hook.
307 if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_DROP) {
308 callout_handle->getArgument("error", error);
310 .arg(error);
311 reconf_queue_flag_ = false;
313 return (answer);
314 }
315 }
316
318 try {
319 // Handle events registered by hooks using external IOService objects.
321 } catch (const std::exception& ex) {
322 std::ostringstream err;
323 err << "Error initializing hooks: "
324 << ex.what();
326 }
327
328 // If we are here, configuration was valid, at least it parsed correctly
329 // and therefore contained no invalid values.
330 // Return the success answer from above.
331 return (answer);
332}
333
334void
336 switch (queue_mgr_->getMgrState()){
338 if (reconf_queue_flag_ || shouldShutdown()) {
343 try {
346 .arg(reconf_queue_flag_ ? "reconfiguration" : "shutdown");
347 queue_mgr_->stopListening();
348 } catch (const isc::Exception& ex) {
349 // It is very unlikely that we would experience an error
350 // here, but theoretically possible.
352 .arg(ex.what());
353 }
354 }
355 break;
356
361 size_t threshold = (((queue_mgr_->getMaxQueueSize()
362 * QUEUE_RESTART_PERCENT)) / 100);
363 if (queue_mgr_->getQueueSize() <= threshold) {
365 .arg(threshold).arg(queue_mgr_->getMaxQueueSize());
366 try {
367 queue_mgr_->startListening();
368 } catch (const isc::Exception& ex) {
370 .arg(ex.what());
371 }
372 }
373
374 break;
375 }
376
385 if (!shouldShutdown()) {
388 }
389 break;
390
396 break;
397
398 default:
399 // If the reconfigure flag is set, then we are in a state now where
400 // we can do the reconfigure. In other words, we aren't RUNNING or
401 // STOPPING.
402 if (reconf_queue_flag_) {
406 }
407 break;
408 }
409}
410
411void
413 // Set reconfigure flag to false. We are only here because we have
414 // a valid configuration to work with so if we fail below, it will be
415 // an operational issue, such as a busy IP address. That will leave
416 // queue manager in INITTED state, which is fine.
417 // What we don't want is to continually attempt to reconfigure so set
418 // the flag false now.
422 reconf_queue_flag_ = false;
423 try {
424 // Wipe out the current listener.
425 queue_mgr_->removeListener();
426
427 // Get the configuration parameters that affect Queue Manager.
428 const D2ParamsPtr& d2_params = getD2CfgMgr()->getD2Params();
429
432 std::string ip_address = d2_params->getIpAddress().toText();
433 if (ip_address == "0.0.0.0" || ip_address == "::") {
435 } else if (ip_address != "127.0.0.1" && ip_address != "::1") {
437 }
438
439 // Instantiate the listener.
440 if (d2_params->getNcrProtocol() == dhcp_ddns::NCR_UDP) {
441 queue_mgr_->initUDPListener(d2_params->getIpAddress(),
442 d2_params->getPort(),
443 d2_params->getNcrFormat(), true);
444 } else {
446 // We should never get this far but if we do deal with it.
447 isc_throw(DProcessBaseError, "Unsupported NCR listener protocol:"
448 << dhcp_ddns::ncrProtocolToString(d2_params->
449 getNcrProtocol()));
450 }
451
452 // Now start it. This assumes that starting is a synchronous,
453 // blocking call that executes quickly.
456 queue_mgr_->startListening();
457 } catch (const isc::Exception& ex) {
458 // Queue manager failed to initialize and therefore not listening.
459 // This is most likely due to an unavailable IP address or port,
460 // which is a configuration issue.
462 }
463}
464
466 queue_mgr_->stopListening();
467 getIOService()->stopAndPoll();
468 queue_mgr_->removeListener();
469}
470
473 // The base class gives a base class pointer to our configuration manager.
474 // Since we are D2, and we need D2 specific extensions, we need a pointer
475 // to D2CfgMgr for some things.
476 return (boost::dynamic_pointer_cast<D2CfgMgr>(getCfgMgr()));
477}
478
480 const char* str;
481 switch (type) {
482 case SD_NORMAL:
483 str = "normal";
484 break;
485 case SD_DRAIN_FIRST:
486 str = "drain_first";
487 break;
488 case SD_NOW:
489 str = "now";
490 break;
491 default:
492 str = "invalid";
493 break;
494 }
495
496 return (str);
497}
498
499void
501 // Get new Unix socket configuration.
502 ConstElementPtr unix_config =
503 getD2CfgMgr()->getUnixControlSocketInfo();
504
505 // Determine if the socket configuration has changed. It has if
506 // both old and new configuration is specified but respective
507 // data elements aren't equal.
508 bool sock_changed = (unix_config && current_unix_control_socket_ &&
509 !unix_config->equals(*current_unix_control_socket_));
510
511 // If the previous or new socket configuration doesn't exist or
512 // the new configuration differs from the old configuration we
513 // close the existing socket and open a new socket as appropriate.
514 // Note that closing an existing socket means the client will not
515 // receive the configuration result.
516 if (!unix_config || !current_unix_control_socket_ || sock_changed) {
517 // Open the new sockets and close old ones, keeping reused.
518 if (unix_config) {
520 } else if (current_unix_control_socket_) {
522 }
523 }
524
525 // Commit the new socket configuration.
526 current_unix_control_socket_ = unix_config;
527
528 // Get new HTTP/HTTPS socket configuration.
529 ConstElementPtr http_config =
530 getD2CfgMgr()->getHttpControlSocketInfo();
531
532 // Open the new sockets and close old ones, keeping reused.
533 if (http_config) {
535 } else if (current_http_control_socket_) {
537 }
538
539 // Commit the new socket configuration.
540 current_http_control_socket_ = http_config;
541}
542
543} // namespace isc::d2
544} // namespace isc
CtrlAgentHooks Hooks
@ 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.
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:183
static process::DControllerBasePtr & instance()
Static singleton instance method.
D2Process(const char *name, const asiolink::IOServicePtr &io_service)
Constructor.
Definition d2_process.cc:59
static const unsigned int QUEUE_RESTART_PERCENT
Defines the point at which to resume receiving requests.
Definition d2_process.h:48
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.
Definition d2_process.cc:96
virtual void init()
Called after instantiation to perform initialization unique to D2.
Definition d2_process.cc:81
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:36
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:47
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 ...
#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_SUCCESS
Status code indicating a successful operation.
boost::shared_ptr< D2CfgMgr > D2CfgMgrPtr
Defines a shared pointer to D2CfgMgr.
Definition d2_cfg_mgr.h:367
const isc::log::MessageID DHCP_DDNS_SECURITY_CHECKS_DISABLED
Definition d2_messages.h:84
const isc::log::MessageID DHCP_DDNS_QUEUE_MGR_RECOVERING
Definition d2_messages.h:56
const isc::log::MessageID DHCP_DDNS_QUEUE_MGR_STOP_ERROR
Definition d2_messages.h:64
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:47
const isc::log::MessageID DHCP_DDNS_QUEUE_MGR_START_ERROR
Definition d2_messages.h:61
const isc::log::MessageID DHCP_DDNS_SHUTDOWN_COMMAND
Definition d2_messages.h:85
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:55
const isc::log::MessageID DHCP_DDNS_RUN_EXIT
Definition d2_messages.h:83
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:58
const isc::log::MessageID DHCP_DDNS_STARTED
Definition d2_messages.h:86
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:59
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:63
const isc::log::MessageID DHCP_DDNS_NOT_ON_LOOPBACK
Definition d2_messages.h:48
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
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...
boost::shared_ptr< DCfgMgrBase > DCfgMgrBasePtr
Defines a shared pointer to DCfgMgrBase.
Definition d_cfg_mgr.h:247
ConstElementPtr redactConfig(ConstElementPtr const &element, list< string > const &json_path, string obscure)
Redact a configuration.
Defines the logger used by the top-level component of kea-lfc.