Kea 3.3.3
allocator.h
Go to the documentation of this file.
1// Copyright (C) 2022-2026 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>
13#include <dhcpsrv/lease.h>
14#include <dhcpsrv/pool.h>
15#include <dhcpsrv/subnet_id.h>
18
19#include <cstdint>
20#include <mutex>
21
22#include <boost/shared_ptr.hpp>
23#include <boost/weak_ptr.hpp>
24
25namespace isc {
26namespace dhcp {
27
32class Subnet;
33
35typedef boost::weak_ptr<Subnet> WeakSubnetPtr;
36
39class AllocFailed : public Exception {
40public:
41
47 AllocFailed(const char* file, size_t line, const char* what)
48 : Exception(file, line, what) {}
49};
50
60class Allocator {
61public:
62
64 enum PrefixLenMatchType : uint16_t {
65 PREFIX_LEN_EQUAL, // select PD-pools with specific prefix length
66 PREFIX_LEN_LOWER, // select PD-pools with lower prefix length
67 PREFIX_LEN_HIGHER // select PD-pools with higher prefix length
68 };
69
77 Allocator(Lease::Type type, const WeakSubnetPtr& subnet);
78
82 virtual ~Allocator();
83
87 virtual std::string getType() const = 0;
88
105 pickAddress(const ClientClasses& client_classes,
106 const IdentifierBaseTypePtr& duid,
107 const asiolink::IOAddress& hint) {
109 return (pickAddressInternal(client_classes, duid, hint));
110 }
111
134 pickPrefix(const ClientClasses& client_classes,
135 Pool6Ptr& pool,
136 const IdentifierBaseTypePtr& duid,
137 PrefixLenMatchType prefix_length_match,
138 const asiolink::IOAddress& hint,
139 uint8_t hint_prefix_length) {
141 return (pickPrefixInternal(client_classes, pool, duid,
142 prefix_length_match, hint,
143 hint_prefix_length));
144 }
145
156 virtual double
158 const ClientClasses& client_classes);
159
171 virtual double
173 const uint8_t plen,
174 const ClientClasses& client_classes);
175
186 static bool isValidPrefixPool(Allocator::PrefixLenMatchType prefix_length_match,
187 PoolPtr pool, uint8_t hint_prefix_length);
188
197 void initAfterConfigure();
198
199protected:
200
206
207private:
208
221 pickAddressInternal(const ClientClasses& client_classes,
222 const IdentifierBaseTypePtr& duid,
223 const isc::asiolink::IOAddress& hint) = 0;
224
243 pickPrefixInternal(const ClientClasses& client_classes,
244 Pool6Ptr& pool,
245 const IdentifierBaseTypePtr& duid,
246 PrefixLenMatchType prefix_length_match,
247 const isc::asiolink::IOAddress& hint,
248 uint8_t hint_prefix_length) = 0;
249
250protected:
251
257
260
263
266
268 std::mutex mutex_;
269};
270
272typedef boost::shared_ptr<Allocator> AllocatorPtr;
273
274} // end of namespace isc::dhcp
275} // end of namespace isc
276
277#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:47
void initAfterConfigure()
Performs allocator initialization after server's reconfiguration.
Definition allocator.cc:68
PrefixLenMatchType
Type of preferred PD-pool prefix length selection criteria.
Definition allocator.h:64
virtual double getOccupancyRate(const asiolink::IOAddress &addr, const ClientClasses &client_classes)
Returns the occupancy rate (v4 addresses).
Definition allocator.cc:82
virtual isc::asiolink::IOAddress pickAddress(const ClientClasses &client_classes, const IdentifierBaseTypePtr &duid, const asiolink::IOAddress &hint)
Picks an address.
Definition allocator.h:105
SubnetID subnet_id_
ID of a subnet to which the allocator belongs.
Definition allocator.h:262
virtual void initAfterConfigureInternal()
Allocator-specific initialization function.
Definition allocator.h:205
Lease::Type pool_type_
Defines pool type allocation.
Definition allocator.h:259
std::mutex mutex_
The mutex to protect the allocated lease.
Definition allocator.h:268
bool inited_
Indicates if the allocator has been initialized.
Definition allocator.h:256
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:134
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:265
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:111
boost::weak_ptr< Subnet > WeakSubnetPtr
Weak pointer to the Subnet.
Definition allocator.h:35
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:272
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:48
RAII lock object to protect the code in the same scope with a mutex.