Kea 2.6.0
network_state.cc
Go to the documentation of this file.
1// Copyright (C) 2017-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>
8
11#include <dhcpsrv/timer_mgr.h>
13#include <boost/enable_shared_from_this.hpp>
14#include <functional>
15#include <sstream>
16#include <string>
17#include <unordered_set>
18
19using namespace isc::util;
20
21namespace isc {
22namespace dhcp {
23
25class NetworkStateImpl : public boost::enable_shared_from_this<NetworkStateImpl> {
26public:
27
30 : server_type_(server_type), globally_disabled_(false),
34 }
35
38 for (auto const& origin : disabled_by_origin_) {
39 destroyTimer(origin);
40 }
41 }
42
55 void setDisableService(const bool disable, unsigned int origin) {
56 if (disable) {
57 // Disable the service for any flag.
58 globally_disabled_ = true;
59 if (origin == NetworkState::DB_CONNECTION) {
61 } else {
62 disabled_by_origin_.insert(origin);
63 }
64 } else {
65 if (origin == NetworkState::DB_CONNECTION) {
66 // Never go below 0 (using unsigned type).
67 // This should never happen anyway.
70 }
71 } else {
72 disabled_by_origin_.erase(origin);
73 }
74 // Enable the service only if all flags have been cleared.
76 globally_disabled_ = false;
77 }
78 }
79 }
80
86 if (disabled_by_origin_.empty()) {
87 globally_disabled_ = false;
88 }
89 }
90
96 void delayedEnable(unsigned int origin) {
97 setDisableService(false, origin);
98 destroyTimer(origin);
99 }
100
110 void createTimer(const unsigned int seconds, unsigned int origin) {
111 destroyTimer(origin);
112 if (origin == NetworkState::DB_CONNECTION) {
113 isc_throw(BadValue, "DB connection does not support delayed enable");
114 }
115 auto timer_name = getTimerName(origin);
116 timer_mgr_->registerTimer(timer_name,
118 shared_from_this(), origin),
119 seconds * 1000,
121 timer_mgr_->setup(timer_name);
122 }
123
127 void destroyTimer(unsigned int origin) {
128 if (origin == NetworkState::DB_CONNECTION) {
129 return;
130 }
131 auto timer_name = getTimerName(origin);
132 if (timer_mgr_->isTimerRegistered(timer_name)) {
133 timer_mgr_->unregisterTimer(timer_name);
134 }
135 }
136
141 std::string getTimerName(unsigned int origin) const {
142 std::ostringstream timer_name;
143 timer_name << "network-state-timer-" << origin;
144 return (timer_name.str());
145 }
146
149
152
155
158
164
166 std::unordered_set<unsigned int> disabled_by_origin_;
167
171};
172
174 : impl_(new NetworkStateImpl(server_type)), mutex_(new std::mutex()) {
175}
176
177void
178NetworkState::disableService(unsigned int origin) {
179 MultiThreadingLock lock(*mutex_);
180 impl_->setDisableService(true, origin);
181}
182
183void
184NetworkState::enableService(unsigned int origin) {
185 MultiThreadingLock lock(*mutex_);
186 impl_->delayedEnable(origin);
187}
188
189void
191 MultiThreadingLock lock(*mutex_);
192 impl_->resetForDbConnection();
193}
194
195void
196NetworkState::delayedEnableService(const unsigned int seconds, unsigned int origin) {
197 MultiThreadingLock lock(*mutex_);
198 impl_->createTimer(seconds, origin);
199}
200
201bool
203 MultiThreadingLock lock(*mutex_);
204 return (!impl_->globally_disabled_);
205}
206
207bool
209 for (auto const& origin : impl_->disabled_by_origin_) {
210 if (TimerMgr::instance()->isTimerRegistered(impl_->getTimerName(origin))) {
211 return (true);
212 }
213 }
214 return (false);
215}
216
217void
219 isc_throw(NotImplemented, "selectiveDisableService is not implemented");
220}
221
222void
224 isc_throw(NotImplemented, "selectiveDisableService is not implemented");
225}
226
227void
229 isc_throw(NotImplemented, "selectiveEnableService is not implemented");
230}
231
232void
234 isc_throw(NotImplemented, "selectiveEnableService is not implemented");
235}
236
237} // end of namespace isc::dhcp
238} // end of namespace isc
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown when a function is not implemented.
Implementation of the NetworkState class.
void resetForDbConnection()
Reset internal counters for a database connection origin.
std::string getTimerName(unsigned int origin) const
Creates a unique timer name from the origin.
void setDisableService(const bool disable, unsigned int origin)
Sets appropriate disabled or enabled DHCP service state for the respective origin.
NetworkState::ServerType server_type_
Server type.
NetworkState::Subnets disabled_subnets_
A list of subnets for which the DHCP service has been disabled.
NetworkStateImpl(const NetworkState::ServerType &server_type)
Constructor.
bool globally_disabled_
A flag indicating if DHCP service is globally disabled.
TimerMgrPtr timer_mgr_
A pointer to the common timer manager.
void delayedEnable(unsigned int origin)
Enables DHCP service for an origin.
uint32_t disabled_by_db_connection_
Flag which indicates the state has been disabled by a DB connection loss.
std::unordered_set< unsigned int > disabled_by_origin_
A set of requests to disable the service by origin.
NetworkState::Networks disabled_networks_
A list of networks for which the DHCP service has been disabled.
void createTimer(const unsigned int seconds, unsigned int origin)
Creates a timer counting the time when delayedEnable should be automatically called.
void destroyTimer(unsigned int origin)
Destroys a timer if present.
static const unsigned int DB_CONNECTION
The network state is being altered by the DB connection recovery mechanics.
void selectiveDisable(const NetworkState::Subnets &subnets)
Disable DHCP service for selected subnets.
std::set< SubnetID > Subnets
Type of the container holding collection of subnet identifiers.
std::set< std::string > Networks
Type of the container holding collection of shared network names.
bool isDelayedEnableService() const
Checks if delayed enabling of DHCP services is scheduled.
void resetForDbConnection()
Reset internal counters for database connection.
void selectiveEnable(const NetworkState::Subnets &subnets)
Enable DHCP service for selected subnets.
NetworkState(const ServerType &server_type)
Constructor.
void enableService(unsigned int origin)
Enable the DHCP service state for respective transition origin.
ServerType
DHCP server type.
Definition: network_state.h:75
void disableService(unsigned int origin)
Disable the DHCP service state for respective transition origin.
bool isServiceEnabled() const
Checks if the DHCP service is globally enabled.
void delayedEnableService(const unsigned int seconds, unsigned int origin)
Schedules enabling DHCP service in the future.
Manages a pool of asynchronous interval timers.
Definition: timer_mgr.h:62
static const TimerMgrPtr & instance()
Returns pointer to the sole instance of the TimerMgr.
Definition: timer_mgr.cc:446
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< TimerMgr > TimerMgrPtr
Type definition of the shared pointer to TimerMgr.
Definition: timer_mgr.h:27
Defines the logger used by the top-level component of kea-lfc.
RAII lock object to protect the code in the same scope with a mutex.