| Kea 3.0.2-git

configuration.h
Go to the documentation of this file.
1// Copyright (C) 2022-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#ifndef ISC_LIMITS_CONFIGURATION_H
8#define ISC_LIMITS_CONFIGURATION_H
9
10#include <cc/base_stamped_element.h>
11#include <cc/data.h>
12#include <dhcp/classify.h>
13#include <dhcpsrv/srv_config.h>
14#include <dhcpsrv/subnet_id.h>
15
16#include <limits>
17
18 namespace isc {
19 namespace limits {
20
26 template <typename limit_t>
27 static void checkForLimitBoundaries(int64_t const limit) {
28 if (limit < 0) {
29 isc_throw(ConfigError,
30 "expected positive limit configured, got negative limit " << limit);
31 }
32
33 if (std::numeric_limits<limit_t>::max() < limit) {
34 isc_throw(ConfigError, "expected limit in range [0, "
35 << std::numeric_limits<limit_t>::max()
36 << "], got limit " << limit);
37 }
38}
39
41 using LeaseLimit = uint32_t;
42
44 struct RateLimit {
46 RateLimit() : allowed_packets_(0) {
47 }
48
51 RateLimit(std::string const& text);
52
54 uint32_t allowed_packets_;
55
58 std::chrono::seconds time_unit_;
59
62 std::string text_;
63};
64
66 template <typename limit_t>
67 struct Configuration {
69 virtual ~Configuration() = default;
70
74 void parse(isc::dhcp::SrvConfigPtr const& config) {
75 if (config) {
76 // Check limits for all client classes.
77 isc::dhcp::ClientClassDefListPtr const& client_classes(
78 config->getClientClassDictionary()->getClasses());
79 for (isc::dhcp::ClientClassDefPtr const& c : *client_classes) {
80 logClientClassLimit(c->getName(), parseUserContext(c->getContext()));
81 }
82
83 // Check limits for all v4 subnets.
84 isc::dhcp::Subnet4Collection const* const subnets4(config->getCfgSubnets4()->getAll());
85 for (isc::dhcp::Subnet4Ptr const& s : *subnets4) {
86 logSubnetLimit(s->getID(), parseUserContext(s->getContext()));
87 }
88
89 // Check limits for all v6 subnets.
90 isc::dhcp::Subnet6Collection const* const subnets6(config->getCfgSubnets6()->getAll());
91 for (isc::dhcp::Subnet6Ptr const& s : *subnets6) {
92 logSubnetLimit(s->getID(), parseUserContext(s->getContext()));
93 }
94 }
95 }
96
103 isc::data::ConstElementPtr const
104 parseUserContext(isc::data::ConstElementPtr const& user_context) const {
105 if (!user_context) {
106 return (isc::data::ConstElementPtr());
107 }
108 isc::data::ConstElementPtr const& limits(user_context->get("limits"));
109 if (!limits) {
110 return (isc::data::ConstElementPtr());
111 }
112 return (limits->get(key()));
113 }
114
118 virtual const std::string& key() const = 0;
119
124 virtual void logClientClassLimit(isc::dhcp::ClientClass const& client_class,
125 isc::data::ConstElementPtr const& user_context) = 0;
126
131 virtual void logSubnetLimit(isc::dhcp::SubnetID const subnet_id,
132 isc::data::ConstElementPtr const& user_context) = 0;
133};
134
136 struct AddressLimitConfiguration : Configuration<LeaseLimit> {
140 const std::string& key() const override;
141
146 virtual void logClientClassLimit(isc::dhcp::ClientClass const& client_class,
147 isc::data::ConstElementPtr const& user_context) override;
148
153 virtual void logSubnetLimit(isc::dhcp::SubnetID const subnet_id,
154 isc::data::ConstElementPtr const& user_context) override;
155};
156
158 struct PrefixLimitConfiguration : Configuration<LeaseLimit> {
162 const std::string& key() const override;
163
168 virtual void logClientClassLimit(isc::dhcp::ClientClass const& client_class,
169 isc::data::ConstElementPtr const& user_context) override;
170
175 virtual void logSubnetLimit(isc::dhcp::SubnetID const subnet_id,
176 isc::data::ConstElementPtr const& user_context) override;
177};
178
180 struct RateLimitConfiguration : Configuration<RateLimit> {
184 const std::string& key() const override;
185
190 virtual void logClientClassLimit(isc::dhcp::ClientClass const& client_class,
191 isc::data::ConstElementPtr const& user_context) override;
192
197 virtual void logSubnetLimit(isc::dhcp::SubnetID const subnet_id,
198 isc::data::ConstElementPtr const& user_context) override;
199};
200
201} // namespace limits
202} // namespace isc
203
204#endif // ISC_LIMITS_CONFIGURATION_H
base_stamped_element.h
classify.h
Defines elements for storing the names of client classes.
data.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
isc::config
Definition command_interpreter.cc:23
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
isc::dhcp::ClientClass
std::string ClientClass
Defines a single class name.
Definition classify.h:43
isc::dhcp::Subnet4Ptr
boost::shared_ptr< Subnet4 > Subnet4Ptr
A pointer to a Subnet4 object.
Definition subnet.h:461
isc::dhcp::Subnet6Ptr
boost::shared_ptr< Subnet6 > Subnet6Ptr
A pointer to a Subnet6 object.
Definition subnet.h:626
isc::dhcp::Subnet6Collection
boost::multi_index_container< Subnet6Ptr, boost::multi_index::indexed_by< boost::multi_index::ordered_unique< boost::multi_index::tag< SubnetSubnetIdIndexTag >, boost::multi_index::const_mem_fun< Subnet, SubnetID, &Subnet::getID > >, boost::multi_index::ordered_unique< boost::multi_index::tag< SubnetPrefixIndexTag >, boost::multi_index::const_mem_fun< Subnet, std::string, &Subnet::toText > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< SubnetModificationTimeIndexTag >, boost::multi_index::const_mem_fun< data::BaseStampedElement, boost::posix_time::ptime, &data::BaseStampedElement::getModificationTime > > > > Subnet6Collection
A collection of Subnet6 objects.
Definition subnet.h:937
isc::dhcp::ClientClassDefPtr
boost::shared_ptr< ClientClassDef > ClientClassDefPtr
a pointer to an ClientClassDef
Definition client_class_def.h:347
isc::dhcp::SrvConfigPtr
boost::shared_ptr< SrvConfig > SrvConfigPtr
Non-const pointer to the SrvConfig.
Definition srv_config.h:1136
isc::dhcp::Subnet4Collection
boost::multi_index_container< Subnet4Ptr, boost::multi_index::indexed_by< boost::multi_index::ordered_unique< boost::multi_index::tag< SubnetSubnetIdIndexTag >, boost::multi_index::const_mem_fun< Subnet, SubnetID, &Subnet::getID > >, boost::multi_index::ordered_unique< boost::multi_index::tag< SubnetPrefixIndexTag >, boost::multi_index::const_mem_fun< Subnet, std::string, &Subnet::toText > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< SubnetServerIdIndexTag >, boost::multi_index::const_mem_fun< Network4, asiolink::IOAddress, &Network4::getServerId > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< SubnetModificationTimeIndexTag >, boost::multi_index::const_mem_fun< data::BaseStampedElement, boost::posix_time::ptime, &data::BaseStampedElement::getModificationTime > > > > Subnet4Collection
A collection of Subnet4 objects.
Definition subnet.h:866
isc::dhcp::SubnetID
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition subnet_id.h:25
isc::dhcp::ClientClassDefListPtr
boost::shared_ptr< ClientClassDefList > ClientClassDefListPtr
Defines a pointer to a ClientClassDefList.
Definition client_class_def.h:359
isc::limits
Definition configuration.cc:18
isc::limits::LeaseLimit
uint32_t LeaseLimit
a single lease-limiting entry configured as an integer
Definition configuration.h:41
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
srv_config.h
isc::limits::AddressLimitConfiguration
the configuration manager for address limiting
Definition configuration.h:136
isc::limits::AddressLimitConfiguration::logClientClassLimit
virtual void logClientClassLimit(isc::dhcp::ClientClass const &client_class, isc::data::ConstElementPtr const &user_context) override
Method that checks and logs limit which is to be applied to a client class.
Definition configuration.cc:78
isc::limits::AddressLimitConfiguration::logSubnetLimit
virtual void logSubnetLimit(isc::dhcp::SubnetID const subnet_id, isc::data::ConstElementPtr const &user_context) override
Method that checks and logs limit which is to be applied to a client class.
Definition configuration.cc:93
isc::limits::AddressLimitConfiguration::key
const std::string & key() const override
Returns the key required to configure the limit in the user context.
Definition configuration.cc:72
isc::limits::Configuration
a datastore used for configuring functionally dedicated parts of the limits library
Definition configuration.h:67
isc::limits::Configuration::key
virtual const std::string & key() const =0
Returns the key required to configure the limit in the user context.
isc::limits::Configuration::logClientClassLimit
virtual void logClientClassLimit(isc::dhcp::ClientClass const &client_class, isc::data::ConstElementPtr const &user_context)=0
Abstract method that checks and logs limit which is to be applied to a client class.
isc::limits::Configuration::parseUserContext
isc::data::ConstElementPtr const parseUserContext(isc::data::ConstElementPtr const &user_context) const
Abstract method that takes a user context from any configuration element and returns the value of the...
Definition configuration.h:104
isc::limits::Configuration::parse
void parse(isc::dhcp::SrvConfigPtr const &config)
Handles all the parsing of the Kea configuration required for limiting functionality.
Definition configuration.h:74
isc::limits::Configuration::logSubnetLimit
virtual void logSubnetLimit(isc::dhcp::SubnetID const subnet_id, isc::data::ConstElementPtr const &user_context)=0
Abstract method that checks and logs limit which is to be applied to a client class.
isc::limits::Configuration::~Configuration
virtual ~Configuration()=default
Destructor. Declared only to be marked as virtual.
isc::limits::PrefixLimitConfiguration
the configuration manager for prefix limiting
Definition configuration.h:158
isc::limits::PrefixLimitConfiguration::logClientClassLimit
virtual void logClientClassLimit(isc::dhcp::ClientClass const &client_class, isc::data::ConstElementPtr const &user_context) override
Method that checks and logs limit which is to be applied to a client class.
Definition configuration.cc:116
isc::limits::PrefixLimitConfiguration::logSubnetLimit
virtual void logSubnetLimit(isc::dhcp::SubnetID const subnet_id, isc::data::ConstElementPtr const &user_context) override
Method that checks and logs limit which is to be applied to a client class.
Definition configuration.cc:131
isc::limits::PrefixLimitConfiguration::key
const std::string & key() const override
Returns the key required to configure the limit in the user context.
Definition configuration.cc:110
isc::limits::RateLimitConfiguration
the configuration manager for rate limiting
Definition configuration.h:180
isc::limits::RateLimitConfiguration::logClientClassLimit
virtual void logClientClassLimit(isc::dhcp::ClientClass const &client_class, isc::data::ConstElementPtr const &user_context) override
Method that checks and logs limit which is to be applied to a client class.
Definition configuration.cc:154
isc::limits::RateLimitConfiguration::key
const std::string & key() const override
Returns the key required to configure the limit in the user context.
Definition configuration.cc:148
isc::limits::RateLimitConfiguration::logSubnetLimit
virtual void logSubnetLimit(isc::dhcp::SubnetID const subnet_id, isc::data::ConstElementPtr const &user_context) override
Method that checks and logs limit which is to be applied to a client class.
Definition configuration.cc:169
isc::limits::RateLimit::text_
std::string text_
a string representation of the rate limit as specified in the configuration used for logging purposes
Definition configuration.h:62
isc::limits::RateLimit::RateLimit
RateLimit()
Constructor.
Definition configuration.h:46
isc::limits::RateLimit::time_unit_
std::chrono::seconds time_unit_
Seconds of one time unit's worth.
Definition configuration.h:58
isc::limits::RateLimit::allowed_packets_
uint32_t allowed_packets_
the configured limit
Definition configuration.h:54
subnet_id.h