Kea: cfgmgr.cc Source File

| Kea 3.2.0-git

cfgmgr.cc
Go to the documentation of this file.
1// Copyright (C) 2012-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>
8#include <asiolink/io_address.h>
9#include <dhcp/iface_mgr.h>
10#include <dhcp/libdhcp++.h>
11#include <dhcpsrv/cfgmgr.h>
12#include <dhcpsrv/dhcpsrv_log.h>
13#include <sstream>
14#include <string>
15
16using namespace isc::asiolink;
17using namespace isc::util;
18
19namespace isc {
20namespace dhcp {
21
22CfgMgr::CfgMgr()
23 : data_dir_checker_(new file::PathChecker(DHCP_DATA_DIR, "KEA_DHCP_DATA_DIR")),
24 d2_client_mgr_(new D2ClientMgr()),
25 configuration_(new SrvConfig()), family_(AF_INET) {
26}
27
28 CfgMgr&
29CfgMgr::instance() {
30 static CfgMgr cfg_mgr;
31 return (cfg_mgr);
32}
33
34std::string
35CfgMgr::getDataDir(bool reset /* = false */, const std::string explicit_path /* = "" */) {
36 return (data_dir_checker_->getPath(reset, explicit_path));
37}
38
39std::string
40CfgMgr::validatePath(const std::string data_path) const {
41 return (data_dir_checker_->validatePath(data_path));
42}
43
44void
45CfgMgr::setD2ClientConfig(D2ClientConfigPtr& new_config) {
46 // Note that D2ClientMgr::setD2Config() actually attempts to apply the
47 // configuration by stopping its sender and opening a new one and so
48 // forth per the new configuration.
49 d2_client_mgr_->setD2ClientConfig(new_config);
50
51 // Manager will throw if the set fails, if it succeeds
52 // we'll update our SrvConfig, configuration_, with the D2ClientConfig
53 // used. This is largely bookkeeping in case we ever want to compare
54 // configuration_ to another SrvConfig.
55 configuration_->setD2ClientConfig(new_config);
56}
57
58bool
59CfgMgr::ddnsEnabled() {
60 return (d2_client_mgr_->ddnsEnabled());
61}
62
63const D2ClientConfigPtr&
64CfgMgr::getD2ClientConfig() const {
65 return (d2_client_mgr_->getD2ClientConfig());
66}
67
68 D2ClientMgr&
69CfgMgr::getD2ClientMgr() {
70 return (*d2_client_mgr_);
71}
72
73void
74CfgMgr::clear() {
75 if (staging_configuration_) {
76 staging_configuration_.reset();
77 }
78 if (configuration_) {
79 configuration_->removeStatistics();
80 configuration_.reset(new SrvConfig());
81 }
82 external_configs_.clear();
83 D2ClientConfigPtr d2_default_conf(new D2ClientConfig());
84 setD2ClientConfig(d2_default_conf);
85}
86
87void
88CfgMgr::clearStagingConfiguration() {
89 staging_configuration_.reset();
90}
91
92void
93CfgMgr::commit() {
94 // First we need to remove statistics. The new configuration can have fewer
95 // subnets. Also, it may change subnet-ids. So we need to remove them all
96 // and add them back.
97 configuration_->removeStatistics();
98
99 if (staging_configuration_ && !configuration_->sequenceEquals(*staging_configuration_)) {
100 // Promote the staging configuration to the current configuration.
101 configuration_ = staging_configuration_;
102 staging_configuration_.reset();
103 }
104
105 // Set the last commit timestamp.
106 auto now = boost::posix_time::second_clock::universal_time();
107 configuration_->setLastCommitTime(now);
108
109 // Now we need to set the statistics back.
110 configuration_->updateStatistics();
111
112 configuration_->configureLowerLevelLibraries();
113}
114
115 SrvConfigPtr
116CfgMgr::getCurrentCfg() {
117 return (configuration_);
118}
119
120 SrvConfigPtr
121CfgMgr::getStagingCfg() {
122 if (!staging_configuration_ || configuration_->sequenceEquals(*staging_configuration_)) {
123 uint32_t sequence = configuration_->getSequence();
124 staging_configuration_ = SrvConfigPtr(new SrvConfig(++sequence));
125 }
126 return (staging_configuration_);
127}
128
129 SrvConfigPtr
130CfgMgr::createExternalCfg() {
131 uint32_t seq = 0;
132
133 if (!external_configs_.empty()) {
134 seq = external_configs_.rbegin()->second->getSequence() + 1;
135 }
136
137 SrvConfigPtr srv_config(new SrvConfig(seq));
138 external_configs_[seq] = srv_config;
139 return (srv_config);
140}
141
142void
143CfgMgr::mergeIntoStagingCfg(const uint32_t seq) {
144 mergeIntoCfg(getStagingCfg(), seq);
145}
146
147void
148CfgMgr::mergeIntoCurrentCfg(const uint32_t seq) {
149 try {
150 // First we need to remove statistics.
151 getCurrentCfg()->removeStatistics();
152 mergeIntoCfg(getCurrentCfg(), seq);
153 LibDHCP::setRuntimeOptionDefs(getCurrentCfg()->getCfgOptionDef()->getContainer());
154
155 } catch (...) {
156 // Make sure the statistics is updated even if the merge failed.
157 getCurrentCfg()->updateStatistics();
158 throw;
159 }
160 getCurrentCfg()->updateStatistics();
161}
162
163void
164CfgMgr::mergeIntoCfg(const SrvConfigPtr& target_config, const uint32_t seq) {
165 auto source_config = external_configs_.find(seq);
166 if (source_config != external_configs_.end()) {
167 target_config->merge(*source_config->second);
168 external_configs_.erase(source_config);
169
170 } else {
171 isc_throw(BadValue, "the external configuration with the sequence number "
172 "of " << seq << " was not found");
173 }
174}
175
176} // end of isc::dhcp namespace
177} // end of isc namespace
cfgmgr.h
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition exceptions/exceptions.h:132
isc::dhcp::CfgMgr
Configuration Manager.
Definition cfgmgr.h:71
isc::dhcp::CfgMgr::CfgMgr
CfgMgr()
Protected constructor.
Definition cfgmgr.cc:22
isc::dhcp::CfgMgr::getD2ClientConfig
const D2ClientConfigPtr & getD2ClientConfig() const
Fetches the DHCP-DDNS configuration pointer.
Definition cfgmgr.cc:64
isc::dhcp::CfgMgr::getD2ClientMgr
D2ClientMgr & getD2ClientMgr()
Fetches the DHCP-DDNS manager.
Definition cfgmgr.cc:69
isc::dhcp::CfgMgr::validatePath
std::string validatePath(const std::string data_path) const
Validates a file path against the supported directory for DHCP data.
Definition cfgmgr.cc:40
isc::dhcp::CfgMgr::clear
void clear()
Remove current, staging, and external configurations.
Definition cfgmgr.cc:74
isc::dhcp::CfgMgr::createExternalCfg
SrvConfigPtr createExternalCfg()
Creates an external configuration and returns pointer to it.
Definition cfgmgr.cc:130
isc::dhcp::CfgMgr::setD2ClientConfig
void setD2ClientConfig(D2ClientConfigPtr &new_config)
Updates the DHCP-DDNS client configuration to the given value.
Definition cfgmgr.cc:45
isc::dhcp::CfgMgr::instance
static CfgMgr & instance()
returns a single instance of Configuration Manager
Definition cfgmgr.cc:29
isc::dhcp::CfgMgr::mergeIntoStagingCfg
void mergeIntoStagingCfg(const uint32_t seq)
Merges external configuration with the given sequence number into the staging configuration.
Definition cfgmgr.cc:143
isc::dhcp::CfgMgr::mergeIntoCurrentCfg
void mergeIntoCurrentCfg(const uint32_t seq)
Merges external configuration with the given sequence number into the current configuration.
Definition cfgmgr.cc:148
isc::dhcp::CfgMgr::getStagingCfg
SrvConfigPtr getStagingCfg()
Returns a pointer to the staging configuration.
Definition cfgmgr.cc:121
isc::dhcp::CfgMgr::ddnsEnabled
bool ddnsEnabled()
Convenience method for checking if DHCP-DDNS updates are enabled.
Definition cfgmgr.cc:59
isc::dhcp::CfgMgr::getDataDir
std::string getDataDir(bool reset=false, const std::string explicit_path="")
Fetches the supported DHCP data directory.
Definition cfgmgr.cc:35
isc::dhcp::CfgMgr::commit
void commit()
Commits the staging configuration.
Definition cfgmgr.cc:93
isc::dhcp::CfgMgr::clearStagingConfiguration
void clearStagingConfiguration()
Remove staging configuration.
Definition cfgmgr.cc:88
isc::dhcp::CfgMgr::getCurrentCfg
SrvConfigPtr getCurrentCfg()
Returns a pointer to the current configuration.
Definition cfgmgr.cc:116
isc::dhcp::D2ClientConfig
Acts as a storage vault for D2 client configuration.
Definition d2_client_cfg.h:57
isc::dhcp::D2ClientMgr
D2ClientMgr isolates Kea from the details of being a D2 client.
Definition d2_client_mgr.h:89
isc::dhcp::LibDHCP::setRuntimeOptionDefs
static void setRuntimeOptionDefs(const OptionDefSpaceContainer &defs)
Copies option definitions created at runtime.
Definition libdhcp++.cc:224
isc::dhcp::SrvConfig
Specifies current DHCP configuration.
Definition srv_config.h:50
isc::util::file::PathChecker
Embodies a supported path against which file paths can be validated.
Definition filesystem.h:222
dhcpsrv_log.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
iface_mgr.h
io_address.h
libdhcp++.h
isc::asiolink
Definition addr_utilities.cc:170
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::D2ClientConfigPtr
boost::shared_ptr< D2ClientConfig > D2ClientConfigPtr
Defines a pointer for D2ClientConfig instances.
Definition d2_client_cfg.h:236
isc::dhcp::SrvConfigPtr
boost::shared_ptr< SrvConfig > SrvConfigPtr
Non-const pointer to the SrvConfig.
Definition srv_config.h:1148
isc::util::file
Definition filesystem.cc:29
isc::util
Definition time_utils.cc:43
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18