Kea  2.3.5-git
allocator.h
Go to the documentation of this file.
1 // Copyright (C) 2022-2023 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 <exceptions/exceptions.h>
14 #include <dhcpsrv/lease.h>
15 #include <dhcpsrv/pool.h>
17 #include <boost/shared_ptr.hpp>
18 #include <boost/weak_ptr.hpp>
19 #include <mutex>
20 
21 namespace isc {
22 namespace dhcp {
23 
28 class Subnet;
29 
31 typedef boost::weak_ptr<Subnet> WeakSubnetPtr;
32 
35 class AllocFailed : public Exception {
36 public:
37 
43  AllocFailed(const char* file, size_t line, const char* what)
44  : Exception(file, line, what) {}
45 };
46 
56 class Allocator {
57 public:
58 
61  PREFIX_LEN_EQUAL, // select PD-pools with specific prefix length
62  PREFIX_LEN_LOWER, // select PD-pools with lower prefix length
63  PREFIX_LEN_HIGHER // select PD-pools with higher prefix length
64  };
65 
73  Allocator(Lease::Type type, const WeakSubnetPtr& subnet)
74  : pool_type_(type),
75  subnet_(subnet) {
76  }
77 
79  virtual ~Allocator() = default;
80 
97  pickAddress(const ClientClasses& client_classes,
98  const DuidPtr& duid,
99  const asiolink::IOAddress& hint) {
100  util::MultiThreadingLock lock(mutex_);
101  return (pickAddressInternal(client_classes, duid, hint));
102  }
103 
126  pickPrefix(const ClientClasses& client_classes,
127  Pool6Ptr& pool,
128  const DuidPtr& duid,
129  PrefixLenMatchType prefix_length_match,
130  const asiolink::IOAddress& hint,
131  uint8_t hint_prefix_length) {
132  util::MultiThreadingLock lock(mutex_);
133  return (pickPrefixInternal(client_classes, pool, duid,
134  prefix_length_match, hint,
135  hint_prefix_length));
136  }
137 
148  static bool isValidPrefixPool(Allocator::PrefixLenMatchType prefix_length_match,
149  PoolPtr pool, uint8_t hint_prefix_length);
150 
151 private:
152 
165  pickAddressInternal(const ClientClasses& client_classes,
166  const DuidPtr& duid,
167  const isc::asiolink::IOAddress& hint) = 0;
168 
187  pickPrefixInternal(const ClientClasses& client_classes,
188  Pool6Ptr& pool,
189  const DuidPtr& duid,
190  PrefixLenMatchType prefix_length_match,
191  const isc::asiolink::IOAddress& hint,
192  uint8_t hint_prefix_length) = 0;
193 
194 protected:
195 
198 
200  WeakSubnetPtr subnet_;
201 
202 private:
203 
205  std::mutex mutex_;
206 };
207 
209 typedef boost::shared_ptr<Allocator> AllocatorPtr;
210 
211 } // end of namespace isc::dhcp
212 } // end of namespace isc
213 
214 #endif // ALLOCATOR_H
boost::shared_ptr< DUID > DuidPtr
Definition: duid.h:20
Base class for all address/prefix allocation algorithms.
Definition: allocator.h:56
AllocFailed(const char *file, size_t line, const char *what)
Constructor.
Definition: allocator.h:43
boost::shared_ptr< Pool6 > Pool6Ptr
a pointer an IPv6 Pool
Definition: pool.h:298
Defines elements for storing the names of client classes.
boost::shared_ptr< Pool > PoolPtr
a pointer to either IPv4 or IPv6 Pool
Definition: pool.h:491
Allocator(Lease::Type type, const WeakSubnetPtr &subnet)
Constructor.
Definition: allocator.h:73
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
An exception that is thrown when allocation module fails (e.g.
Definition: allocator.h:35
PrefixLenMatchType
Type of preferred PD-pool prefix length selection criteria.
Definition: allocator.h:60
boost::weak_ptr< Subnet > WeakSubnetPtr
Weak pointer to the Subnet.
Definition: allocator.h:28
virtual isc::asiolink::IOAddress pickPrefix(const ClientClasses &client_classes, Pool6Ptr &pool, const DuidPtr &duid, PrefixLenMatchType prefix_length_match, const asiolink::IOAddress &hint, uint8_t hint_prefix_length)
Picks a delegated prefix.
Definition: allocator.h:126
virtual isc::asiolink::IOAddress pickAddress(const ClientClasses &client_classes, const DuidPtr &duid, const asiolink::IOAddress &hint)
Picks an address.
Definition: allocator.h:97
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-lfc.
Lease::Type pool_type_
Defines pool type allocation.
Definition: allocator.h:197
Type
Type of lease or pool.
Definition: lease.h:46
WeakSubnetPtr subnet_
Weak pointer to the subnet owning the allocator.
Definition: allocator.h:200
boost::shared_ptr< Allocator > AllocatorPtr
Defines a pointer to an allocator.
Definition: allocator.h:209
Container for storing client class names.
Definition: classify.h:108
RAII lock object to protect the code in the same scope with a mutex.