Kea: flq_allocation_state.cc Source File

| Kea 3.2.0-git

flq_allocation_state.cc
Go to the documentation of this file.
1// Copyright (C) 2023-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#include <config.h>
8#include <dhcpsrv/flq_allocation_state.h>
9#include <boost/make_shared.hpp>
10
11using namespace isc::asiolink;
12
13namespace isc {
14namespace dhcp {
15
16 PoolFreeLeaseQueueAllocationStatePtr
17PoolFreeLeaseQueueAllocationState::create(const PoolPtr& pool) {
18 return (boost::make_shared<PoolFreeLeaseQueueAllocationState>(pool->getType()));
19}
20
21PoolFreeLeaseQueueAllocationState::PoolFreeLeaseQueueAllocationState(Lease::Type type)
22 : AllocationState(), free_lease4_queue_(), free_lease6_queue_() {
23 if (type == Lease::TYPE_V4) {
24 free_lease4_queue_ = boost::make_shared<FreeLeaseQueue<uint32_t>>();
25 } else {
26 free_lease6_queue_ = boost::make_shared<FreeLeaseQueue<IOAddress>>();
27 }
28}
29
30bool
31PoolFreeLeaseQueueAllocationState::exhausted() const {
32 return ((free_lease4_queue_ && free_lease4_queue_->empty()) ||
33 (free_lease6_queue_ && free_lease6_queue_->empty()));
34}
35
36void
37PoolFreeLeaseQueueAllocationState::addFreeLease(const asiolink::IOAddress& address) {
38 if (free_lease4_queue_) {
39 free_lease4_queue_->push_back(address.toUint32());
40 } else {
41 free_lease6_queue_->push_back(address);
42 }
43}
44
45void
46PoolFreeLeaseQueueAllocationState::deleteFreeLease(const asiolink::IOAddress& address) {
47 if (free_lease4_queue_) {
48 auto& idx = free_lease4_queue_->get<1>();
49 idx.erase(address.toUint32());
50 } else {
51 auto& idx = free_lease6_queue_->get<1>();
52 idx.erase(address);
53 }
54}
55
56bool
57PoolFreeLeaseQueueAllocationState::isFreeLease(const asiolink::IOAddress& address) const {
58 if (free_lease4_queue_) {
59 auto const& idx = free_lease4_queue_->get<1>();
60 return (idx.count(address.toUint32()) > 0);
61 } else {
62 auto const& idx = free_lease6_queue_->get<1>();
63 return (idx.count(address) > 0);
64 }
65}
66
67 IOAddress
68PoolFreeLeaseQueueAllocationState::offerFreeLease() {
69 if (free_lease4_queue_) {
70 if (free_lease4_queue_->empty()) {
71 return (IOAddress::IPV4_ZERO_ADDRESS());
72 }
73 uint32_t lease = free_lease4_queue_->front();
74 free_lease4_queue_->pop_front();
75 free_lease4_queue_->push_back(lease);
76 return (IOAddress(lease));
77 }
78
79 if (free_lease6_queue_->empty()) {
80 return (IOAddress::IPV6_ZERO_ADDRESS());
81 }
82 IOAddress lease = free_lease6_queue_->front();
83 free_lease6_queue_->pop_front();
84 free_lease6_queue_->push_back(lease);
85 return (lease);
86}
87
88size_t
89PoolFreeLeaseQueueAllocationState::getFreeLeaseCount() const {
90 if (free_lease4_queue_) {
91 return (free_lease4_queue_->size());
92 }
93 return (free_lease6_queue_->size());
94}
95
96} // end of namespace isc::dhcp
97} // end of namespace isc
isc::asiolink::IOAddress
The IOAddress class represents an IP addresses (version agnostic).
Definition io_address.h:53
isc::asiolink::IOAddress::IPV4_ZERO_ADDRESS
static const IOAddress & IPV4_ZERO_ADDRESS()
Returns an address set to all zeros.
Definition io_address.h:285
isc::asiolink::IOAddress::toUint32
uint32_t toUint32() const
Converts IPv4 address to uint32_t.
Definition io_address.cc:121
isc::asiolink::IOAddress::IPV6_ZERO_ADDRESS
static const IOAddress & IPV6_ZERO_ADDRESS()
Returns an IPv6 zero address.
Definition io_address.h:297
isc::dhcp::AllocationState
Base class for representing allocation state in pools and subnets.
Definition allocation_state.h:31
isc::dhcp::PoolFreeLeaseQueueAllocationState::isFreeLease
bool isFreeLease(const asiolink::IOAddress &address) const
Check if a lease is in the queue.
Definition flq_allocation_state.cc:57
isc::dhcp::PoolFreeLeaseQueueAllocationState::offerFreeLease
asiolink::IOAddress offerFreeLease()
Returns next available lease.
Definition flq_allocation_state.cc:68
isc::dhcp::PoolFreeLeaseQueueAllocationState::PoolFreeLeaseQueueAllocationState
PoolFreeLeaseQueueAllocationState(Lease::Type type)
Constructor.
Definition flq_allocation_state.cc:21
isc::dhcp::PoolFreeLeaseQueueAllocationState::exhausted
bool exhausted() const
Checks if the pool has run out of free leases.
Definition flq_allocation_state.cc:31
isc::dhcp::PoolFreeLeaseQueueAllocationState::deleteFreeLease
void deleteFreeLease(const asiolink::IOAddress &address)
Deletes free lease from the queue.
Definition flq_allocation_state.cc:46
isc::dhcp::PoolFreeLeaseQueueAllocationState::addFreeLease
void addFreeLease(const asiolink::IOAddress &address)
Adds a free lease to the queue.
Definition flq_allocation_state.cc:37
isc::dhcp::PoolFreeLeaseQueueAllocationState::create
static PoolFreeLeaseQueueAllocationStatePtr create(const PoolPtr &pool)
Factory function creating the state instance from a pool.
Definition flq_allocation_state.cc:17
isc::dhcp::PoolFreeLeaseQueueAllocationState::getFreeLeaseCount
size_t getFreeLeaseCount() const
Returns the current number of free leases in the queue.
Definition flq_allocation_state.cc:89
flq_allocation_state.h
isc::asiolink
Definition addr_utilities.cc:170
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::PoolFreeLeaseQueueAllocationStatePtr
boost::shared_ptr< PoolFreeLeaseQueueAllocationState > PoolFreeLeaseQueueAllocationStatePtr
Type of the pointer to the PoolFreeLeaseQueueAllocationState.
Definition flq_allocation_state.h:23
isc::dhcp::PoolPtr
boost::shared_ptr< Pool > PoolPtr
a pointer to either IPv4 or IPv6 Pool
Definition pool.h:726
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
isc::dhcp::Lease::Type
Type
Type of lease or pool.
Definition lease.h:46
isc::dhcp::Lease::TYPE_V4
@ TYPE_V4
IPv4 lease.
Definition lease.h:50