Kea 2.7.6
cfgmgr.cc
Go to the documentation of this file.
1// Copyright (C) 2012-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>
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 : datadir_(DHCP_DATA_DIR, true), d2_client_mgr_(new D2ClientMgr()),
24 configuration_(new SrvConfig()), family_(AF_INET) {
25}
26
27CfgMgr&
29 static CfgMgr cfg_mgr;
30 return (cfg_mgr);
31}
32
35 return (datadir_);
36}
37
38void
39CfgMgr::setDataDir(const std::string& datadir, bool unspecified) {
40 datadir_ = Optional<std::string>(datadir, unspecified);
41}
42
43void
45 // Note that D2ClientMgr::setD2Config() actually attempts to apply the
46 // configuration by stopping its sender and opening a new one and so
47 // forth per the new configuration.
48 d2_client_mgr_->setD2ClientConfig(new_config);
49
50 // Manager will throw if the set fails, if it succeeds
51 // we'll update our SrvConfig, configuration_, with the D2ClientConfig
52 // used. This is largely bookkeeping in case we ever want to compare
53 // configuration_ to another SrvConfig.
54 configuration_->setD2ClientConfig(new_config);
55}
56
57bool
59 return (d2_client_mgr_->ddnsEnabled());
60}
61
64 return (d2_client_mgr_->getD2ClientConfig());
65}
66
69 return (*d2_client_mgr_);
70}
71
72void
74 if (staging_configuration_) {
75 staging_configuration_.reset();
76 }
77 if (configuration_) {
78 configuration_->removeStatistics();
79 configuration_.reset(new SrvConfig());
80 }
81 external_configs_.clear();
82 D2ClientConfigPtr d2_default_conf(new D2ClientConfig());
83 setD2ClientConfig(d2_default_conf);
84}
85
86void
88 staging_configuration_.reset();
89}
90
91void
93 // First we need to remove statistics. The new configuration can have fewer
94 // subnets. Also, it may change subnet-ids. So we need to remove them all
95 // and add them back.
96 configuration_->removeStatistics();
97
98 if (staging_configuration_ && !configuration_->sequenceEquals(*staging_configuration_)) {
99 // Promote the staging configuration to the current configuration.
100 configuration_ = staging_configuration_;
101 staging_configuration_.reset();
102 }
103
104 // Set the last commit timestamp.
105 auto now = boost::posix_time::second_clock::universal_time();
106 configuration_->setLastCommitTime(now);
107
108 // Now we need to set the statistics back.
109 configuration_->updateStatistics();
110
111 configuration_->configureLowerLevelLibraries();
112}
113
116 return (configuration_);
117}
118
121 if (!staging_configuration_ || configuration_->sequenceEquals(*staging_configuration_)) {
122 uint32_t sequence = configuration_->getSequence();
123 staging_configuration_ = SrvConfigPtr(new SrvConfig(++sequence));
124 }
125 return (staging_configuration_);
126}
127
130 uint32_t seq = 0;
131
132 if (!external_configs_.empty()) {
133 seq = external_configs_.rbegin()->second->getSequence() + 1;
134 }
135
136 SrvConfigPtr srv_config(new SrvConfig(seq));
137 external_configs_[seq] = srv_config;
138 return (srv_config);
139}
140
141void
142CfgMgr::mergeIntoStagingCfg(const uint32_t seq) {
143 mergeIntoCfg(getStagingCfg(), seq);
144}
145
146void
147CfgMgr::mergeIntoCurrentCfg(const uint32_t seq) {
148 try {
149 // First we need to remove statistics.
150 getCurrentCfg()->removeStatistics();
151 mergeIntoCfg(getCurrentCfg(), seq);
152 LibDHCP::setRuntimeOptionDefs(getCurrentCfg()->getCfgOptionDef()->getContainer());
153
154 } catch (...) {
155 // Make sure the statistics is updated even if the merge failed.
156 getCurrentCfg()->updateStatistics();
157 throw;
158 }
159 getCurrentCfg()->updateStatistics();
160}
161
162void
163CfgMgr::mergeIntoCfg(const SrvConfigPtr& target_config, const uint32_t seq) {
164 auto source_config = external_configs_.find(seq);
165 if (source_config != external_configs_.end()) {
166 target_config->merge(*source_config->second);
167 external_configs_.erase(source_config);
168
169 } else {
170 isc_throw(BadValue, "the external configuration with the sequence number "
171 "of " << seq << " was not found");
172 }
173}
174
175} // end of isc::dhcp namespace
176} // 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:70
util::Optional< std::string > getDataDir() const
returns path do the data directory
Definition cfgmgr.cc:34
CfgMgr()
Protected constructor.
Definition cfgmgr.cc:22
const D2ClientConfigPtr & getD2ClientConfig() const
Fetches the DHCP-DDNS configuration pointer.
Definition cfgmgr.cc:63
D2ClientMgr & getD2ClientMgr()
Fetches the DHCP-DDNS manager.
Definition cfgmgr.cc:68
void clear()
Remove current, staging, and external configurations.
Definition cfgmgr.cc:73
SrvConfigPtr createExternalCfg()
Creates an external configuration and returns pointer to it.
Definition cfgmgr.cc:129
void setD2ClientConfig(D2ClientConfigPtr &new_config)
Updates the DHCP-DDNS client configuration to the given value.
Definition cfgmgr.cc:44
static CfgMgr & instance()
returns a single instance of Configuration Manager
Definition cfgmgr.cc:28
void mergeIntoStagingCfg(const uint32_t seq)
Merges external configuration with the given sequence number into the staging configuration.
Definition cfgmgr.cc:142
void mergeIntoCurrentCfg(const uint32_t seq)
Merges external configuration with the given sequence number into the current configuration.
Definition cfgmgr.cc:147
void setDataDir(const std::string &datadir, bool unspecified=true)
Sets new data directory.
Definition cfgmgr.cc:39
SrvConfigPtr getStagingCfg()
Returns a pointer to the staging configuration.
Definition cfgmgr.cc:120
bool ddnsEnabled()
Convenience method for checking if DHCP-DDNS updates are enabled.
Definition cfgmgr.cc:58
void commit()
Commits the staging configuration.
Definition cfgmgr.cc:92
void clearStagingConfiguration()
Remove staging configuration.
Definition cfgmgr.cc:87
SrvConfigPtr getCurrentCfg()
Returns a pointer to the current configuration.
Definition cfgmgr.cc:115
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:209
A template representing an optional value.
Definition optional.h:36
#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.