| Kea 3.0.2-git

allocator.cc
Go to the documentation of this file.
1// Copyright (C) 2022-2024 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#include <config.h>
8#include <dhcpsrv/allocator.h>
9#include <dhcpsrv/lease_mgr_factory.h>
10#include <dhcpsrv/dhcpsrv_log.h>
11
12 using namespace isc::util;
13
14 namespace isc {
15 namespace dhcp {
16
17 Allocator::Allocator(Lease::Type type, const WeakSubnetPtr& subnet)
18 : inited_(false),
19 pool_type_(type),
20 subnet_id_(0),
21 subnet_(subnet) {
22 // Remember subnet ID in a separate variable. It may be needed in
23 // the destructor where the subnet weak pointer is unavailable.
24 subnet_id_ = subnet_.lock()->getID();
25}
26
27 Allocator::~Allocator() {
28 if (!LeaseMgrFactory::haveInstance()) {
29 // If there is no lease manager instance, the callbacks are
30 // gone already anyway.
31 return;
32 }
33 // Remove the callbacks.
34 LeaseMgrFactory::instance().unregisterCallbacks(subnet_id_, pool_type_);
35}
36
37 bool
38 Allocator::isValidPrefixPool(Allocator::PrefixLenMatchType prefix_length_match,
39 PoolPtr pool, uint8_t hint_prefix_length) {
40 auto pool6 = boost::dynamic_pointer_cast<Pool6>(pool);
41 if (!pool6) {
42 return (false);
43 }
44
45 if (!hint_prefix_length) {
46 return (true);
47 }
48
49 if (prefix_length_match == Allocator::PREFIX_LEN_EQUAL &&
50 pool6->getLength() != hint_prefix_length) {
51 return (false);
52 }
53
54 if (prefix_length_match == Allocator::PREFIX_LEN_LOWER &&
55 pool6->getLength() >= hint_prefix_length) {
56 return (false);
57 }
58
59 if (prefix_length_match == Allocator::PREFIX_LEN_HIGHER &&
60 pool6->getLength() <= hint_prefix_length) {
61 return (false);
62 }
63
64 return (true);
65}
66
67 void
68 Allocator::initAfterConfigure() {
69 if (inited_) {
70 return;
71 }
72 auto subnet = subnet_.lock();
73 LOG_INFO(dhcpsrv_logger, DHCPSRV_CFGMGR_USE_ALLOCATOR)
74 .arg(getType())
75 .arg(Lease::typeToText(pool_type_))
76 .arg(subnet->toText());
77 initAfterConfigureInternal();
78 inited_ = true;
79}
80
81}
82}
allocator.h
isc::dhcp::Allocator::initAfterConfigure
void initAfterConfigure()
Performs allocator initialization after server's reconfiguration.
Definition allocator.cc:68
isc::dhcp::Allocator::subnet_id_
SubnetID subnet_id_
ID of a subnet to which the allocator belongs.
Definition allocator.h:229
isc::dhcp::Allocator::initAfterConfigureInternal
virtual void initAfterConfigureInternal()
Allocator-specific initialization function.
Definition allocator.h:172
isc::dhcp::Allocator::pool_type_
Lease::Type pool_type_
Defines pool type allocation.
Definition allocator.h:226
isc::dhcp::Allocator::PrefixLenMatchType
PrefixLenMatchType
Type of preferred PD-pool prefix length selection criteria.
Definition allocator.h:61
isc::dhcp::Allocator::PREFIX_LEN_EQUAL
@ PREFIX_LEN_EQUAL
Definition allocator.h:62
isc::dhcp::Allocator::PREFIX_LEN_LOWER
@ PREFIX_LEN_LOWER
Definition allocator.h:63
isc::dhcp::Allocator::PREFIX_LEN_HIGHER
@ PREFIX_LEN_HIGHER
Definition allocator.h:64
isc::dhcp::Allocator::inited_
bool inited_
Indicates if the allocator has been initialized.
Definition allocator.h:223
isc::dhcp::Allocator::Allocator
Allocator(Lease::Type type, const WeakSubnetPtr &subnet)
Constructor.
Definition allocator.cc:17
isc::dhcp::Allocator::~Allocator
virtual ~Allocator()
Virtual destructor.
Definition allocator.cc:27
isc::dhcp::Allocator::subnet_
WeakSubnetPtr subnet_
Weak pointer to the subnet owning the allocator.
Definition allocator.h:232
isc::dhcp::Allocator::getType
virtual std::string getType() const =0
Returns allocator type string.
isc::dhcp::Allocator::isValidPrefixPool
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
isc::dhcp::LeaseMgrFactory::instance
static TrackingLeaseMgr & instance()
Return current lease manager.
Definition lease_mgr_factory.cc:124
isc::dhcp::LeaseMgrFactory::haveInstance
static bool haveInstance()
Indicates if the lease manager has been instantiated.
Definition lease_mgr_factory.cc:119
isc::dhcp::TrackingLeaseMgr::unregisterCallbacks
void unregisterCallbacks(SubnetID subnet_id, Lease::Type lease_type)
Unregisters all callbacks for a given subnet identifier.
Definition tracking_lease_mgr.cc:93
dhcpsrv_log.h
lease_mgr_factory.h
LOG_INFO
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition macros.h:20
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::dhcpsrv_logger
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
Definition dhcpsrv_log.h:56
isc::dhcp::WeakSubnetPtr
boost::weak_ptr< Subnet > WeakSubnetPtr
Weak pointer to the Subnet.
Definition allocator.h:32
isc::dhcp::DHCPSRV_CFGMGR_USE_ALLOCATOR
const isc::log::MessageID DHCPSRV_CFGMGR_USE_ALLOCATOR
Definition dhcpsrv_messages.h:48
isc::dhcp::PoolPtr
boost::shared_ptr< Pool > PoolPtr
a pointer to either IPv4 or IPv6 Pool
Definition pool.h:726
isc::util
Definition time_utils.cc:43
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
isc::dhcp::Lease::Type
Type
Type of lease or pool.
Definition lease.h:46
isc::dhcp::Lease::typeToText
static std::string typeToText(Type type)
returns text representation of a lease type
Definition lease.cc:56