Kea 3.1.1
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
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
18namespace isc {
19namespace limits {
20
26template <typename limit_t>
27static 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
41using LeaseLimit = uint32_t;
42
44struct RateLimit {
48
51 RateLimit(std::string const& text);
52
55
58 std::chrono::seconds time_unit_;
59
62 std::string text_;
63};
64
66template <typename limit_t>
69 virtual ~Configuration() = default;
70
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
104 parseUserContext(isc::data::ConstElementPtr const& user_context) const {
105 if (!user_context) {
107 }
108 isc::data::ConstElementPtr const& limits(user_context->get("limits"));
109 if (!limits) {
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
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
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
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
Defines elements for storing the names of client classes.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
std::string ClientClass
Defines a single class name.
Definition classify.h:44
boost::shared_ptr< Subnet4 > Subnet4Ptr
A pointer to a Subnet4 object.
Definition subnet.h:461
boost::shared_ptr< Subnet6 > Subnet6Ptr
A pointer to a Subnet6 object.
Definition subnet.h:626
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
boost::shared_ptr< ClientClassDef > ClientClassDefPtr
a pointer to an ClientClassDef
boost::shared_ptr< SrvConfig > SrvConfigPtr
Non-const pointer to the SrvConfig.
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
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition subnet_id.h:25
boost::shared_ptr< ClientClassDefList > ClientClassDefListPtr
Defines a pointer to a ClientClassDefList.
uint32_t LeaseLimit
a single lease-limiting entry configured as an integer
Defines the logger used by the top-level component of kea-lfc.
the configuration manager for address limiting
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.
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.
const std::string & key() const override
Returns the key required to configure the limit in the user context.
a datastore used for configuring functionally dedicated parts of the limits library
virtual const std::string & key() const =0
Returns the key required to configure the limit in the user context.
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::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...
void parse(isc::dhcp::SrvConfigPtr const &config)
Handles all the parsing of the Kea configuration required for limiting functionality.
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.
virtual ~Configuration()=default
Destructor. Declared only to be marked as virtual.
the configuration manager for prefix limiting
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.
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.
const std::string & key() const override
Returns the key required to configure the limit in the user context.
the configuration manager for rate limiting
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.
const std::string & key() const override
Returns the key required to configure the limit in the user context.
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.
std::string text_
a string representation of the rate limit as specified in the configuration used for logging purposes
RateLimit()
Constructor.
std::chrono::seconds time_unit_
Seconds of one time unit's worth.
uint32_t allowed_packets_
the configured limit