Kea 3.1.1
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>
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
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
28CfgMgr&
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
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
60 return (d2_client_mgr_->ddnsEnabled());
61}
62
65 return (d2_client_mgr_->getD2ClientConfig());
66}
67
70 return (*d2_client_mgr_);
71}
72
73void
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
89 staging_configuration_.reset();
90}
91
92void
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
117 return (configuration_);
118}
119
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
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
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Configuration Manager.
Definition cfgmgr.h:71
CfgMgr()
Protected constructor.
Definition cfgmgr.cc:22
const D2ClientConfigPtr & getD2ClientConfig() const
Fetches the DHCP-DDNS configuration pointer.
Definition cfgmgr.cc:64
D2ClientMgr & getD2ClientMgr()
Fetches the DHCP-DDNS manager.
Definition cfgmgr.cc:69
std::string validatePath(const std::string data_path) const
Validates a file path against the supported directory for DHDP data.
Definition cfgmgr.cc:40
void clear()
Remove current, staging, and external configurations.
Definition cfgmgr.cc:74
SrvConfigPtr createExternalCfg()
Creates an external configuration and returns pointer to it.
Definition cfgmgr.cc:130
void setD2ClientConfig(D2ClientConfigPtr &new_config)
Updates the DHCP-DDNS client configuration to the given value.
Definition cfgmgr.cc:45
static CfgMgr & instance()
returns a single instance of Configuration Manager
Definition cfgmgr.cc:29
void mergeIntoStagingCfg(const uint32_t seq)
Merges external configuration with the given sequence number into the staging configuration.
Definition cfgmgr.cc:143
void mergeIntoCurrentCfg(const uint32_t seq)
Merges external configuration with the given sequence number into the current configuration.
Definition cfgmgr.cc:148
SrvConfigPtr getStagingCfg()
Returns a pointer to the staging configuration.
Definition cfgmgr.cc:121
bool ddnsEnabled()
Convenience method for checking if DHCP-DDNS updates are enabled.
Definition cfgmgr.cc:59
std::string getDataDir(bool reset=false, const std::string explicit_path="")
Fetches the supported DHCP data directory.
Definition cfgmgr.cc:35
void commit()
Commits the staging configuration.
Definition cfgmgr.cc:93
void clearStagingConfiguration()
Remove staging configuration.
Definition cfgmgr.cc:88
SrvConfigPtr getCurrentCfg()
Returns a pointer to the current configuration.
Definition cfgmgr.cc:116
Acts as a storage vault for D2 client configuration.
D2ClientMgr isolates Kea from the details of being a D2 client.
static void setRuntimeOptionDefs(const OptionDefSpaceContainer &defs)
Copies option definitions created at runtime.
Definition libdhcp++.cc:224
Specifies current DHCP configuration.
Definition srv_config.h:50
Embodies a supported path against which file paths can be validated.
Definition filesystem.h:203
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< D2ClientConfig > D2ClientConfigPtr
Defines a pointer for D2ClientConfig instances.
boost::shared_ptr< SrvConfig > SrvConfigPtr
Non-const pointer to the SrvConfig.
Defines the logger used by the top-level component of kea-lfc.