Kea 2.5.9
d2_process.cc
Go to the documentation of this file.
1// Copyright (C) 2013-2024 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>
12#include <d2/d2_controller.h>
13#include <d2/d2_process.h>
14#include <d2srv/d2_cfg_mgr.h>
15#include <d2srv/d2_log.h>
16#include <d2srv/d2_stats.h>
17#include <d2srv/d2_tsig_key.h>
18#include <hooks/hooks.h>
19#include <hooks/hooks_manager.h>
20
21using namespace isc::asiolink;
22using namespace isc::config;
23using namespace isc::hooks;
24using namespace isc::process;
25
26namespace {
27
29struct D2ProcessHooks {
30 int hooks_index_d2_srv_configured_;
31
33 D2ProcessHooks() {
34 hooks_index_d2_srv_configured_ = HooksManager::registerHook("d2_srv_configured");
35 }
36
37};
38
39// Declare a Hooks object. As this is outside any function or method, it
40// will be instantiated (and the constructor run) when the module is loaded.
41// As a result, the hook indexes will be defined before any method in this
42// module is called.
43D2ProcessHooks Hooks;
44
45}
46
47namespace isc {
48namespace d2 {
49
50// Setting to 80% for now. This is an arbitrary choice and should probably
51// be configurable.
52const unsigned int D2Process::QUEUE_RESTART_PERCENT = 80;
53
54D2Process::D2Process(const char* name, const asiolink::IOServicePtr& io_service)
55 : DProcessBase(name, io_service, DCfgMgrBasePtr(new D2CfgMgr())),
56 reconf_queue_flag_(false), shutdown_type_(SD_NORMAL) {
57
58 // Instantiate queue manager. Note that queue manager does not start
59 // listening at this point. That can only occur after configuration has
60 // been received. This means that until we receive the configuration,
61 // D2 will neither receive nor process NameChangeRequests.
62 // Pass in IOService for NCR IO event processing.
63 queue_mgr_.reset(new D2QueueMgr(getIOService()));
64
65 // Instantiate update manager.
66 // Pass in both queue manager and configuration manager.
67 // Pass in IOService for DNS update transaction IO event processing.
69 update_mgr_.reset(new D2UpdateMgr(queue_mgr_, tmp, getIOService()));
70
71 // Initialize stats manager.
73};
74
75void
77 // CommandMgr uses IO service to run asynchronous socket operations.
79};
80
81void
84 D2ControllerPtr controller =
85 boost::dynamic_pointer_cast<D2Controller>(D2Controller::instance());
86 try {
87 // Now logging was initialized so commands can be registered.
88 controller->registerCommands();
89
90 // Loop forever until we are allowed to shutdown.
91 while (!canShutdown()) {
92 // Check on the state of the request queue. Take any
93 // actions necessary regarding it.
95
96 // Give update manager a time slice to queue new jobs and
97 // process finished ones.
98 update_mgr_->sweep();
99
100 // Wait on IO event(s) - block until one or more of the following
101 // has occurred:
102 // a. NCR message has been received
103 // b. Transaction IO has completed
104 // c. Interval timer expired
105 // d. Control channel event
106 // e. Something stopped IO service (runIO returns 0)
107 if (runIO() == 0) {
108 // Pretty sure this amounts to an unexpected stop and we
109 // should bail out now. Normal shutdowns do not utilize
110 // stopping the IOService.
112 "Primary IO service stopped unexpectedly");
113 }
114 }
115 } catch (const std::exception& ex) {
116 LOG_FATAL(d2_logger, DHCP_DDNS_FAILED).arg(ex.what());
117 controller->deregisterCommands();
119 "Process run method failed: " << ex.what());
120 }
121
125
126 controller->deregisterCommands();
127
129
130};
131
132size_t
134 // Handle events registered by hooks using external IOService objects.
136 // We want to block until at least one handler is called. We'll use
137 // boost::asio::io_service directly for two reasons. First off
138 // asiolink::IOService::runOne is a void and boost::asio::io_service::stopped
139 // is not present in older versions of boost. We need to know if any
140 // handlers ran or if the io_service was stopped. That latter represents
141 // some form of error and the application cannot proceed with a stopped
142 // service. Secondly, asiolink::IOService does not provide the poll
143 // method. This is a handy method which runs all ready handlers without
144 // blocking.
145
146 // Poll runs all that are ready. If none are ready it returns immediately
147 // with a count of zero.
148 size_t cnt = getIOService()->poll();
149 if (!cnt) {
150 // Poll ran no handlers either none are ready or the service has been
151 // stopped. Either way, call runOne to wait for a IO event. If the
152 // service is stopped it will return immediately with a cnt of zero.
153 cnt = getIOService()->runOne();
154 }
155 return (cnt);
156}
157
158bool
160 bool all_clear = false;
161
162 // If we have been told to shutdown, find out if we are ready to do so.
163 if (shouldShutdown()) {
164 switch (shutdown_type_) {
165 case SD_NORMAL:
166 // For a normal shutdown we need to stop the queue manager but
167 // wait until we have finished all the transactions in progress.
168 all_clear = (((queue_mgr_->getMgrState() != D2QueueMgr::RUNNING) &&
169 (queue_mgr_->getMgrState() != D2QueueMgr::STOPPING))
170 && (update_mgr_->getTransactionCount() == 0));
171 break;
172
173 case SD_DRAIN_FIRST:
174 // For a drain first shutdown we need to stop the queue manager but
175 // process all of the requests in the receive queue first.
176 all_clear = (((queue_mgr_->getMgrState() != D2QueueMgr::RUNNING) &&
177 (queue_mgr_->getMgrState() != D2QueueMgr::STOPPING))
178 && (queue_mgr_->getQueueSize() == 0)
179 && (update_mgr_->getTransactionCount() == 0));
180 break;
181
182 case SD_NOW:
183 // Get out right now, no niceties.
184 all_clear = true;
185 break;
186
187 default:
188 // shutdown_type_ is an enum and should only be one of the above.
189 // if its getting through to this, something is whacked.
190 break;
191 }
192
193 if (all_clear) {
196 .arg(getShutdownTypeStr(shutdown_type_));
197 }
198 }
199
200 return (all_clear);
201}
202
207 .arg(args ? args->str() : "(no arguments)");
208
209 // Default shutdown type is normal.
210 std::string type_str(getShutdownTypeStr(SD_NORMAL));
211 shutdown_type_ = SD_NORMAL;
212
213 if (args) {
214 if ((args->getType() == isc::data::Element::map) &&
215 args->contains("type")) {
216 type_str = args->get("type")->stringValue();
217
218 if (type_str == getShutdownTypeStr(SD_NORMAL)) {
219 shutdown_type_ = SD_NORMAL;
220 } else if (type_str == getShutdownTypeStr(SD_DRAIN_FIRST)) {
221 shutdown_type_ = SD_DRAIN_FIRST;
222 } else if (type_str == getShutdownTypeStr(SD_NOW)) {
223 shutdown_type_ = SD_NOW;
224 } else {
225 setShutdownFlag(false);
227 "Invalid Shutdown type: " +
228 type_str));
229 }
230 }
231 }
232
233 // Set the base class's shutdown flag.
234 setShutdownFlag(true);
236 "Shutdown initiated, type is: " +
237 type_str));
238}
239
243 .arg(check_only ? "check" : "update")
244 .arg(getD2CfgMgr()->redactConfig(config_set)->str());
245
247 answer = getCfgMgr()->simpleParseConfig(config_set, check_only,
248 std::bind(&D2Process::reconfigureCommandChannel, this));
249 if (check_only) {
250 return (answer);
251 }
252
253 int rcode = 0;
255 comment = isc::config::parseAnswer(rcode, answer);
256
257 if (rcode) {
258 // Non-zero means we got an invalid configuration, take no further
259 // action. In integrated mode, this will send a failed response back
260 // to the configuration backend.
261 reconf_queue_flag_ = false;
262 return (answer);
263 }
264
265 // Set the reconf_queue_flag to indicate that we need to reconfigure
266 // the queue manager. Reconfiguring the queue manager may be asynchronous
267 // and require one or more events to occur, therefore we set a flag
268 // indicating it needs to be done but we cannot do it here. It must
269 // be done over time, while events are being processed. Remember that
270 // the method we are in now is invoked as part of the configuration event
271 // callback. This means you can't wait for events here, you are already
272 // in one.
276 reconf_queue_flag_ = true;
277
278 // This hook point notifies hooks libraries that the configuration of the
279 // D2 server has completed. It provides the hook library with the pointer
280 // to the common IO service object, new server configuration in the JSON
281 // format and with the pointer to the configuration storage where the
282 // parsed configuration is stored.
283 std::string error("");
284 if (HooksManager::calloutsPresent(Hooks.hooks_index_d2_srv_configured_)) {
286
287 callout_handle->setArgument("io_context", getIOService());
288 callout_handle->setArgument("json_config", config_set);
289 callout_handle->setArgument("server_config",
290 getD2CfgMgr()->getD2CfgContext());
291 callout_handle->setArgument("error", error);
292
293 HooksManager::callCallouts(Hooks.hooks_index_d2_srv_configured_,
294 *callout_handle);
295
296 // The config can be rejected by a hook.
297 if (callout_handle->getStatus() == CalloutHandle::NEXT_STEP_DROP) {
298 callout_handle->getArgument("error", error);
300 .arg(error);
301 reconf_queue_flag_ = false;
303 return (answer);
304 }
305 }
306
308 try {
309 // Handle events registered by hooks using external IOService objects.
311 } catch (const std::exception& ex) {
312 std::ostringstream err;
313 err << "Error initializing hooks: "
314 << ex.what();
316 }
317
318 // If we are here, configuration was valid, at least it parsed correctly
319 // and therefore contained no invalid values.
320 // Return the success answer from above.
321 return (answer);
322}
323
324void
326 switch (queue_mgr_->getMgrState()){
328 if (reconf_queue_flag_ || shouldShutdown()) {
333 try {
336 .arg(reconf_queue_flag_ ? "reconfiguration" : "shutdown");
337 queue_mgr_->stopListening();
338 } catch (const isc::Exception& ex) {
339 // It is very unlikely that we would experience an error
340 // here, but theoretically possible.
342 .arg(ex.what());
343 }
344 }
345 break;
346
351 size_t threshold = (((queue_mgr_->getMaxQueueSize()
352 * QUEUE_RESTART_PERCENT)) / 100);
353 if (queue_mgr_->getQueueSize() <= threshold) {
355 .arg(threshold).arg(queue_mgr_->getMaxQueueSize());
356 try {
357 queue_mgr_->startListening();
358 } catch (const isc::Exception& ex) {
360 .arg(ex.what());
361 }
362 }
363
364 break;
365 }
366
375 if (!shouldShutdown()) {
378 }
379 break;
380
386 break;
387
388 default:
389 // If the reconfigure flag is set, then we are in a state now where
390 // we can do the reconfigure. In other words, we aren't RUNNING or
391 // STOPPING.
392 if (reconf_queue_flag_) {
396 }
397 break;
398 }
399}
400
401void
403 // Set reconfigure flag to false. We are only here because we have
404 // a valid configuration to work with so if we fail below, it will be
405 // an operational issue, such as a busy IP address. That will leave
406 // queue manager in INITTED state, which is fine.
407 // What we don't want is to continually attempt to reconfigure so set
408 // the flag false now.
412 reconf_queue_flag_ = false;
413 try {
414 // Wipe out the current listener.
415 queue_mgr_->removeListener();
416
417 // Get the configuration parameters that affect Queue Manager.
418 const D2ParamsPtr& d2_params = getD2CfgMgr()->getD2Params();
419
422 std::string ip_address = d2_params->getIpAddress().toText();
423 if (ip_address == "0.0.0.0" || ip_address == "::") {
425 } else if (ip_address != "127.0.0.1" && ip_address != "::1") {
427 }
428
429 // Instantiate the listener.
430 if (d2_params->getNcrProtocol() == dhcp_ddns::NCR_UDP) {
431 queue_mgr_->initUDPListener(d2_params->getIpAddress(),
432 d2_params->getPort(),
433 d2_params->getNcrFormat(), true);
434 } else {
436 // We should never get this far but if we do deal with it.
437 isc_throw(DProcessBaseError, "Unsupported NCR listener protocol:"
438 << dhcp_ddns::ncrProtocolToString(d2_params->
439 getNcrProtocol()));
440 }
441
442 // Now start it. This assumes that starting is a synchronous,
443 // blocking call that executes quickly.
446 queue_mgr_->startListening();
447 } catch (const isc::Exception& ex) {
448 // Queue manager failed to initialize and therefore not listening.
449 // This is most likely due to an unavailable IP address or port,
450 // which is a configuration issue.
452 }
453}
454
456 queue_mgr_->stopListening();
457 getIOService()->stopAndPoll();
458 queue_mgr_->removeListener();
459}
460
463 // The base class gives a base class pointer to our configuration manager.
464 // Since we are D2, and we need D2 specific extensions, we need a pointer
465 // to D2CfgMgr for some things.
466 return (boost::dynamic_pointer_cast<D2CfgMgr>(getCfgMgr()));
467}
468
470 const char* str;
471 switch (type) {
472 case SD_NORMAL:
473 str = "normal";
474 break;
475 case SD_DRAIN_FIRST:
476 str = "drain_first";
477 break;
478 case SD_NOW:
479 str = "now";
480 break;
481 default:
482 str = "invalid";
483 break;
484 }
485
486 return (str);
487}
488
489void
491 // Get new socket configuration.
492 isc::data::ConstElementPtr sock_cfg = getD2CfgMgr()->getControlSocketInfo();
493
494 // Determine if the socket configuration has changed. It has if
495 // both old and new configuration is specified but respective
496 // data elements aren't equal.
497 bool sock_changed = (sock_cfg && current_control_socket_ &&
498 !sock_cfg->equals(*current_control_socket_));
499
500 // If the previous or new socket configuration doesn't exist or
501 // the new configuration differs from the old configuration we
502 // close the existing socket and open a new socket as appropriate.
503 // Note that closing an existing socket means the client will not
504 // receive the configuration result.
505 if (!sock_cfg || !current_control_socket_ || sock_changed) {
506 // Close the existing socket.
507 if (current_control_socket_) {
509 current_control_socket_.reset();
510 }
511
512 // Open the new socket.
513 if (sock_cfg) {
515 }
516 }
517
518 // Commit the new socket configuration.
519 current_control_socket_ = sock_cfg;
520}
521
522} // namespace isc::d2
523} // namespace isc
CtrlAgentHooks Hooks
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.
void closeCommandSocket()
Shuts down any open control sockets.
Definition: command_mgr.cc:624
static CommandMgr & instance()
CommandMgr is a singleton class.
Definition: command_mgr.cc:646
void setIOService(const asiolink::IOServicePtr &io_service)
Sets IO service to be used by the command manager.
Definition: command_mgr.cc:652
void openCommandSocket(const isc::data::ConstElementPtr &socket_info)
Opens control socket with parameters specified in socket_info.
Definition: command_mgr.cc:620
DHCP-DDNS Configuration Manager.
Definition: d2_cfg_mgr.h:161
static process::DControllerBasePtr & instance()
Static singleton instance method.
D2Process(const char *name, const asiolink::IOServicePtr &io_service)
Constructor.
Definition: d2_process.cc:54
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.
Definition: d2_process.cc:159
virtual void checkQueueStatus()
Monitors current queue manager state, takes action accordingly.
Definition: d2_process.cc:325
virtual ~D2Process()
Destructor.
Definition: d2_process.cc:455
virtual void run()
Implements the process's event loop.
Definition: d2_process.cc:82
virtual void init()
Called after instantiation to perform initialization unique to D2.
Definition: d2_process.cc:76
D2CfgMgrPtr getD2CfgMgr()
Returns a pointer to the configuration manager.
Definition: d2_process.cc:462
virtual isc::data::ConstElementPtr configure(isc::data::ConstElementPtr config_set, bool check_only=false)
Processes the given configuration.
Definition: d2_process.cc:241
void reconfigureCommandChannel()
(Re-)Configure the command channel.
Definition: d2_process.cc:490
virtual void reconfigureQueueMgr()
Initializes then starts the queue manager.
Definition: d2_process.cc:402
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.
Definition: d2_process.cc:204
static const char * getShutdownTypeStr(const ShutdownType &type)
Returns a text label for the given shutdown type.
Definition: d2_process.cc:469
virtual size_t runIO()
Allows IO processing to run until at least callback is invoked.
Definition: d2_process.cc:133
D2QueueMgr creates and manages a queue of DNS update requests.
Definition: d2_queue_mgr.h:130
static void init()
Initialize D2 statistics.
Definition: d2_stats.cc:46
D2UpdateMgr creates and manages update transactions.
Definition: d2_update_mgr.h:65
@ NEXT_STEP_DROP
drop the packet
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
Application Process Interface.
Definition: d_process.h:75
void setShutdownFlag(bool value)
Sets the process shut down flag to the given value.
Definition: d_process.h:162
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
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
const int CONTROL_RESULT_ERROR
Status code indicating a general failure.
ConstElementPtr createAnswer()
Creates a standard config/command level success answer message (i.e.
ConstElementPtr parseAnswer(int &rcode, const ConstElementPtr &msg)
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:334
const isc::log::MessageID DHCP_DDNS_QUEUE_MGR_RECOVERING
Definition: d2_messages.h:59
const isc::log::MessageID DHCP_DDNS_QUEUE_MGR_STOP_ERROR
Definition: d2_messages.h:67
const isc::log::MessageID DHCP_DDNS_FAILED
Definition: d2_messages.h:22
const isc::log::MessageID DHCP_DDNS_LISTENING_ON_ALL_INTERFACES
Definition: d2_messages.h:49
const isc::log::MessageID DHCP_DDNS_QUEUE_MGR_START_ERROR
Definition: d2_messages.h:64
const isc::log::MessageID DHCP_DDNS_SHUTDOWN_COMMAND
Definition: d2_messages.h:87
const isc::log::MessageID DHCP_DDNS_CONFIGURE
Definition: d2_messages.h:17
const isc::log::MessageID DHCP_DDNS_CLEARED_FOR_SHUTDOWN
Definition: d2_messages.h:15
const isc::log::MessageID DHCP_DDNS_QUEUE_MGR_RECONFIGURING
Definition: d2_messages.h:58
const isc::log::MessageID DHCP_DDNS_RUN_EXIT
Definition: d2_messages.h:86
const isc::log::MessageID DHCP_DDNS_CONFIGURED_CALLOUT_DROP
Definition: d2_messages.h:18
const isc::log::MessageID DHCP_DDNS_QUEUE_MGR_RESUME_ERROR
Definition: d2_messages.h:61
const isc::log::MessageID DHCP_DDNS_STARTED
Definition: d2_messages.h:88
boost::shared_ptr< D2Controller > D2ControllerPtr
Pointer to a process controller.
Definition: d2_controller.h:17
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:62
boost::shared_ptr< D2Params > D2ParamsPtr
Defines a pointer for D2Params instances.
Definition: d2_config.h:255
const isc::log::MessageID DHCP_DDNS_QUEUE_MGR_STOPPING
Definition: d2_messages.h:66
const isc::log::MessageID DHCP_DDNS_NOT_ON_LOOPBACK
Definition: d2_messages.h:50
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.
Definition: log_dbglevels.h:69
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...
Definition: log_dbglevels.h:50
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)
Redact a configuration.
Defines the logger used by the top-level component of kea-lfc.