Kea 3.1.1
allocator.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 ALLOCATOR_H
8#define ALLOCATOR_H
9
10#include <asiolink/io_address.h>
11#include <dhcp/classify.h>
12#include <dhcp/duid.h>
14#include <dhcpsrv/lease.h>
15#include <dhcpsrv/subnet_id.h>
16#include <dhcpsrv/pool.h>
18#include <boost/shared_ptr.hpp>
19#include <boost/weak_ptr.hpp>
20#include <mutex>
21
22namespace isc {
23namespace dhcp {
24
29class Subnet;
30
32typedef boost::weak_ptr<Subnet> WeakSubnetPtr;
33
36class AllocFailed : public Exception {
37public:
38
44 AllocFailed(const char* file, size_t line, const char* what)
45 : Exception(file, line, what) {}
46};
47
57class Allocator {
58public:
59
62 PREFIX_LEN_EQUAL, // select PD-pools with specific prefix length
63 PREFIX_LEN_LOWER, // select PD-pools with lower prefix length
64 PREFIX_LEN_HIGHER // select PD-pools with higher prefix length
65 };
66
74 Allocator(Lease::Type type, const WeakSubnetPtr& subnet);
75
79 virtual ~Allocator();
80
84 virtual std::string getType() const = 0;
85
102 pickAddress(const ClientClasses& client_classes,
103 const IdentifierBaseTypePtr& duid,
104 const asiolink::IOAddress& hint) {
106 return (pickAddressInternal(client_classes, duid, hint));
107 }
108
131 pickPrefix(const ClientClasses& client_classes,
132 Pool6Ptr& pool,
133 const IdentifierBaseTypePtr& duid,
134 PrefixLenMatchType prefix_length_match,
135 const asiolink::IOAddress& hint,
136 uint8_t hint_prefix_length) {
138 return (pickPrefixInternal(client_classes, pool, duid,
139 prefix_length_match, hint,
140 hint_prefix_length));
141 }
142
153 virtual double
155 const ClientClasses& client_classes) const;
156
168 virtual double
170 const uint8_t plen,
171 const ClientClasses& client_classes) const;
172
183 static bool isValidPrefixPool(Allocator::PrefixLenMatchType prefix_length_match,
184 PoolPtr pool, uint8_t hint_prefix_length);
185
194 void initAfterConfigure();
195
196protected:
197
203
204private:
205
218 pickAddressInternal(const ClientClasses& client_classes,
219 const IdentifierBaseTypePtr& duid,
220 const isc::asiolink::IOAddress& hint) = 0;
221
240 pickPrefixInternal(const ClientClasses& client_classes,
241 Pool6Ptr& pool,
242 const IdentifierBaseTypePtr& duid,
243 PrefixLenMatchType prefix_length_match,
244 const isc::asiolink::IOAddress& hint,
245 uint8_t hint_prefix_length) = 0;
246
247protected:
248
254
257
260
263
265 std::mutex mutex_;
266};
267
269typedef boost::shared_ptr<Allocator> AllocatorPtr;
270
271} // end of namespace isc::dhcp
272} // end of namespace isc
273
274#endif // ALLOCATOR_H
Defines elements for storing the names of client classes.
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
AllocFailed(const char *file, size_t line, const char *what)
Constructor.
Definition allocator.h:44
void initAfterConfigure()
Performs allocator initialization after server's reconfiguration.
Definition allocator.cc:68
virtual isc::asiolink::IOAddress pickAddress(const ClientClasses &client_classes, const IdentifierBaseTypePtr &duid, const asiolink::IOAddress &hint)
Picks an address.
Definition allocator.h:102
SubnetID subnet_id_
ID of a subnet to which the allocator belongs.
Definition allocator.h:259
virtual void initAfterConfigureInternal()
Allocator-specific initialization function.
Definition allocator.h:202
Lease::Type pool_type_
Defines pool type allocation.
Definition allocator.h:256
PrefixLenMatchType
Type of preferred PD-pool prefix length selection criteria.
Definition allocator.h:61
virtual double getOccupancyRate(const asiolink::IOAddress &addr, const ClientClasses &client_classes) const
Returns the occupancy rate (v4 addresses).
Definition allocator.cc:82
std::mutex mutex_
The mutex to protect the allocated lease.
Definition allocator.h:265
bool inited_
Indicates if the allocator has been initialized.
Definition allocator.h:253
virtual isc::asiolink::IOAddress pickPrefix(const ClientClasses &client_classes, Pool6Ptr &pool, const IdentifierBaseTypePtr &duid, PrefixLenMatchType prefix_length_match, const asiolink::IOAddress &hint, uint8_t hint_prefix_length)
Picks a delegated prefix.
Definition allocator.h:131
Allocator(Lease::Type type, const WeakSubnetPtr &subnet)
Constructor.
Definition allocator.cc:17
virtual ~Allocator()
Virtual destructor.
Definition allocator.cc:27
WeakSubnetPtr subnet_
Weak pointer to the subnet owning the allocator.
Definition allocator.h:262
virtual std::string getType() const =0
Returns allocator type string.
static bool isValidPrefixPool(Allocator::PrefixLenMatchType prefix_length_match, PoolPtr pool, uint8_t hint_prefix_length)
Check if the pool matches the selection criteria relative to the provided hint prefix length.
Definition allocator.cc:38
Container for storing client class names.
Definition classify.h:110
boost::weak_ptr< Subnet > WeakSubnetPtr
Weak pointer to the Subnet.
Definition allocator.h:32
boost::shared_ptr< IdentifierBaseType > IdentifierBaseTypePtr
Shared pointer to a IdentifierType.
Definition duid.h:34
boost::shared_ptr< Pool > PoolPtr
a pointer to either IPv4 or IPv6 Pool
Definition pool.h:726
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition subnet_id.h:25
boost::shared_ptr< Allocator > AllocatorPtr
Defines a pointer to an allocator.
Definition allocator.h:269
boost::shared_ptr< Pool6 > Pool6Ptr
a pointer an IPv6 Pool
Definition pool.h:536
Defines the logger used by the top-level component of kea-lfc.
Type
Type of lease or pool.
Definition lease.h:46
RAII lock object to protect the code in the same scope with a mutex.