1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361 | // Copyright (C) 2023-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <asiolink/addr_utilities.h>
#include <dhcpsrv/dhcpsrv_log.h>
#include <dhcpsrv/flq_allocator.h>
#include <dhcpsrv/ip_range_permutation.h>
#include <dhcpsrv/lease_mgr_factory.h>
#include <dhcpsrv/subnet.h>
#include <util/stopwatch.h>
#include <unordered_set><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
using namespace isc::asiolink;
using namespace isc::util;
using namespace std;
namespace {
/// @brief An owner string used in the callbacks installed in
/// the lease manager.
const string FLQ_OWNER = "flq";
}
namespace isc {
namespace dhcp {
FreeLeaseQueueAllocator::FreeLeaseQueueAllocator(Lease::Type type, const WeakSubnetPtr& subnet)
: Allocator(type, subnet), generator_() {
random_device rd;
generator_.seed(rd());
}
IOAddress
FreeLeaseQueueAllocator::pickAddressInternal(const ClientClasses& client_classes,
const IdentifierBaseTypePtr&,
const IOAddress&) {
auto subnet = subnet_.lock();
auto const& pools = subnet->getPools(pool_type_);
if (pools.empty()) {
// No pools, no allocation.
return (pool_type_ == Lease::TYPE_V4 ? IOAddress::IPV4_ZERO_ADDRESS() : IOAddress::IPV6_ZERO_ADDRESS());
}
// Let's first iterate over the pools and identify the ones that
// meet client class criteria and are not exhausted.
std::vector<uint64_t> available;
for (auto i = 0; i < pools.size(); ++i) {
// Check if the pool is allowed for the client's classes.
if (pools[i]->clientSupported(client_classes)) {
// Get or create the pool state.
auto pool_state = getPoolState(pools[i]);
if (!pool_state->exhausted()) {
// There are still available addresses in this pool.
available.push_back(i);
}
}
}
if (available.empty()) {
// No pool meets the client class criteria or all are exhausted.
return (pool_type_ == Lease::TYPE_V4 ? IOAddress::IPV4_ZERO_ADDRESS() : IOAddress::IPV6_ZERO_ADDRESS());
}
// Get a random pool from the available ones.
auto const& pool = pools[available[getRandomNumber(available.size() - 1)]];
// Get or create the pool state.
auto pool_state = getPoolState(pool);
// The pool should still offer some leases.
auto free_lease = pool_state->offerFreeLease();
// It shouldn't happen, but let's be safe.
if (!free_lease.isV4Zero() && !free_lease.isV6Zero()) {
return (free_lease);
}
// No address available.
return (pool_type_ == Lease::TYPE_V4 ? IOAddress::IPV4_ZERO_ADDRESS() : IOAddress::IPV6_ZERO_ADDRESS());
}
IOAddress
FreeLeaseQueueAllocator::pickPrefixInternal(const ClientClasses& client_classes,
Pool6Ptr& pool6,
const IdentifierBaseTypePtr&,
PrefixLenMatchType prefix_length_match,
const IOAddress&,
uint8_t hint_prefix_length) {
auto subnet = subnet_.lock();
auto const& pools = subnet->getPools(pool_type_);
if (pools.empty()) {
// No pool, no allocation.
return (IOAddress::IPV6_ZERO_ADDRESS());
}
// Let's first iterate over the pools and identify the ones that
// meet client class criteria and are not exhausted.
std::vector<uint64_t> available;
for (auto i = 0; i < pools.size(); ++i) {
// Check if the pool is allowed for the client's classes.
if (pools[i]->clientSupported(client_classes)) {
if (!Allocator::isValidPrefixPool(prefix_length_match, pools[i],
hint_prefix_length)) {
continue;
}
// Get or create the pool state.
auto pool_state = getPoolState(pools[i]);
if (!pool_state->exhausted()) {
// There are still available prefixes in this pool.
available.push_back(i);
}
}
}
if (available.empty()) {
// No pool meets the client class criteria or all are exhausted.
return (IOAddress::IPV6_ZERO_ADDRESS());
}
// Get a random pool from the available ones.
auto const& pool = pools[available[getRandomNumber(available.size() - 1)]];
pool6 = boost::dynamic_pointer_cast<Pool6>(pool);
if (!pool6) {
// Something is gravely wrong here
isc_throw(Unexpected, "Wrong type of pool: "
<< (pool)->toText()
<< " is not Pool6");
}
// Get or create the pool state.
auto pool_state = getPoolState(pool);
// The pool should still offer some leases.
auto free_lease = pool_state->offerFreeLease();
// It shouldn't happen, but let's be safe.
if (!free_lease.isV6Zero()) {
return (free_lease);
}
// No prefix available.
return (IOAddress::IPV6_ZERO_ADDRESS());
}
void
FreeLeaseQueueAllocator::initAfterConfigureInternal() {
auto subnet = subnet_.lock();
auto const& pools = subnet->getPools(pool_type_);
if (pools.empty()) {
// If there are no pools there is nothing to do.
return;
}
Lease4Collection leases4;
Lease6Collection leases6;
switch (pool_type_) {
case Lease::TYPE_V4:
leases4 = LeaseMgrFactory::instance().getLeases4(subnet->getID());
populateFreeAddressLeases(leases4, pools);
break;
case Lease::TYPE_NA:
case Lease::TYPE_TA:
leases6 = LeaseMgrFactory::instance().getLeases6(subnet->getID());
populateFreeAddressLeases(leases6, pools);
break;
case Lease::TYPE_PD:
leases6 = LeaseMgrFactory::instance().getLeases6(subnet->getID());
populateFreePrefixDelegationLeases(leases6, pools);
break;
default:
;
}
// Install the callbacks for lease add, update and delete in the interface manager.
// These callbacks will ensure that we have up-to-date free lease queue.
auto& lease_mgr = LeaseMgrFactory::instance();
lease_mgr.registerCallback(TrackingLeaseMgr::TRACK_ADD_LEASE, FLQ_OWNER, subnet->getID(), pool_type_,
std::bind(&FreeLeaseQueueAllocator::addLeaseCallback, this,
std::placeholders::_1));
lease_mgr.registerCallback(TrackingLeaseMgr::TRACK_UPDATE_LEASE, FLQ_OWNER, subnet->getID(), pool_type_,
std::bind(&FreeLeaseQueueAllocator::updateLeaseCallback, this,
std::placeholders::_1));
lease_mgr.registerCallback(TrackingLeaseMgr::TRACK_DELETE_LEASE, FLQ_OWNER, subnet->getID(), pool_type_,
std::bind(&FreeLeaseQueueAllocator::deleteLeaseCallback, this,
std::placeholders::_1));
}
template<typename LeaseCollectionType>
void
FreeLeaseQueueAllocator::populateFreeAddressLeases(const LeaseCollectionType& leases,
const PoolCollection& pools) {
auto subnet = subnet_.lock();
LOG_INFO(dhcpsrv_logger, DHCPSRV_CFGMGR_FLQ_POPULATE_FREE_ADDRESS_LEASES)
.arg(subnet->toText());
Stopwatch stopwatch;
// Let's iterate over the lease queue and index them with the
// unordered_set. Also, elminate the expired leases and those
// in the expired-reclaimed state.
unordered_set<IOAddress, IOAddress::Hash> leased_addresses;
for (auto const& lease : leases) {
if ((lease->getType() == pool_type_) && (!lease->expired()) && (!lease->stateExpiredReclaimed())) {
leased_addresses.insert(lease->addr_);
}
}
// For each pool, check if the address is in the leases list.
size_t free_lease_count = 0;
for (auto const& pool : pools) {
// Create the pool permutation so the resulting lease queue is no
// particular order.
IPRangePermutation perm(AddressRange(pool->getFirstAddress(), pool->getLastAddress()));
auto pool_state = getPoolState(pool);
auto done = false;
while (!done) {
auto address = perm.next(done);
if (address.isV4Zero() || address.isV6Zero()) {
continue;
}
if (leased_addresses.count(address) == 0) {
// No lease for this address, so add it to the free leases queue.
pool_state->addFreeLease(address);
}
}
free_lease_count += pool_state->getFreeLeaseCount();
}
stopwatch.stop();
LOG_INFO(dhcpsrv_logger, DHCPSRV_CFGMGR_FLQ_POPULATE_FREE_ADDRESS_LEASES_DONE)
.arg(free_lease_count)
.arg(subnet->toText())
.arg(stopwatch.logFormatLastDuration());
}
void
FreeLeaseQueueAllocator::populateFreePrefixDelegationLeases(const Lease6Collection& leases,
const PoolCollection& pools) {
auto subnet = subnet_.lock();
LOG_INFO(dhcpsrv_logger, DHCPSRV_CFGMGR_FLQ_POPULATE_FREE_PREFIX_LEASES)
.arg(subnet->toText());
Stopwatch stopwatch;
// Let's iterate over the lease queue and index them with the
// unordered_set. Also, elminate the expired leases and those
// in the expired-reclaimed state.
unordered_set<IOAddress, IOAddress::Hash> leased_prefixes;
for (auto const& lease : leases) {
if ((lease->getType() == Lease::TYPE_PD) && (!lease->expired()) && (!lease->stateExpiredReclaimed())) {
leased_prefixes.insert(lease->addr_);
}
}
// For each pool, check if the prefix is in the leases list.
size_t free_lease_count = 0;
for (auto const& pool : pools) {
auto pool6 = boost::dynamic_pointer_cast<Pool6>(pool);
if (!pool6) {
continue;
}
// Create the pool permutation so the resulting lease queue is no
// particular order.
IPRangePermutation perm(PrefixRange(pool->getFirstAddress(),
pool->getLastAddress(),
pool6->getLength()));
auto pool_state = getPoolState(pool);
auto done = false;
while (!done) {
auto prefix = perm.next(done);
if (prefix.isV4Zero() || prefix.isV6Zero()) {
continue;
}
if (leased_prefixes.count(prefix) == 0) {
// No lease for this prefix, so add it to the free leases queue.
pool_state->addFreeLease(prefix);
}
}
free_lease_count += pool_state->getFreeLeaseCount();
}
stopwatch.stop();
LOG_INFO(dhcpsrv_logger, DHCPSRV_CFGMGR_FLQ_POPULATE_FREE_PREFIX_LEASES_DONE)
.arg(free_lease_count)
.arg(subnet->toText())
.arg(stopwatch.logFormatLastDuration());
}
PoolFreeLeaseQueueAllocationStatePtr
FreeLeaseQueueAllocator::getPoolState(const PoolPtr& pool) const {
if (!pool->getAllocationState()) {
pool->setAllocationState(PoolFreeLeaseQueueAllocationState::create(pool));
}
return (boost::dynamic_pointer_cast<PoolFreeLeaseQueueAllocationState>(pool->getAllocationState()));
}
PoolPtr
FreeLeaseQueueAllocator::getLeasePool(const LeasePtr& lease) const {
auto subnet = subnet_.lock();
if (!subnet) {
return (PoolPtr());
}
auto pool = subnet->getPool(pool_type_, lease->addr_, false);
return (pool);
}
void
FreeLeaseQueueAllocator::addLeaseCallback(LeasePtr lease) {
MultiThreadingLock lock(mutex_);
addLeaseCallbackInternal(lease);
}
void
FreeLeaseQueueAllocator::addLeaseCallbackInternal(LeasePtr lease) {
if (lease->expired()) {
return;
}
auto pool = getLeasePool(lease);
if (!pool) {
return;
}
getPoolState(pool)->deleteFreeLease(lease->addr_);
}
void
FreeLeaseQueueAllocator::updateLeaseCallback(LeasePtr lease) {
MultiThreadingLock lock(mutex_);
updateLeaseCallbackInternal(lease);
}
void
FreeLeaseQueueAllocator::updateLeaseCallbackInternal(LeasePtr lease) {
auto pool = getLeasePool(lease);
if (!pool) {
return;
}
auto pool_state = getPoolState(pool);
if (lease->stateExpiredReclaimed() || (lease->expired())) {
pool_state->addFreeLease(lease->addr_);
} else {
pool_state->deleteFreeLease(lease->addr_);
}
}
void
FreeLeaseQueueAllocator::deleteLeaseCallback(LeasePtr lease) {
MultiThreadingLock lock(mutex_);
deleteLeaseCallbackInternal(lease);
}
void
FreeLeaseQueueAllocator::deleteLeaseCallbackInternal(LeasePtr lease) {
auto pool = getLeasePool(lease);
if (!pool) {
return;
}
getPoolState(pool)->addFreeLease(lease->addr_);
}
uint64_t
FreeLeaseQueueAllocator::getRandomNumber(uint64_t limit) {
// Take the short path if there is only one number to randomize from.
if (limit == 0) {
return (0);
}
std::uniform_int_distribution<uint64_t> dist(0, limit);
return (dist(generator_));
}
} // end of namespace isc::dhcp
} // end of namespace isc
|