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
// Copyright (C) 2022-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 <dhcpsrv/random_allocator.h>
#include <dhcpsrv/subnet.h>
#include <algorithm><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <random><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace isc::asiolink;
using namespace std;

namespace isc {
namespace dhcp {

RandomAllocator::RandomAllocator(Lease::Type type, const WeakSubnetPtr& subnet)
    : Allocator(type, subnet), generator_() {
    random_device rd;
    generator_.seed(rd());
}

IOAddress
RandomAllocator::pickAddressInternal(const ClientClasses& client_classes,
                                     const IdentifierBaseTypePtr&,
                                     const IOAddress&) {
    auto subnet = subnet_.lock();
    auto const& pools = subnet->getPools(pool_type_);

    // Let's first iterate over the pools and identify the ones that
    // meet client class criteria. Then, segregate these pools into
    // the ones that still have available addresses and exhausted
    // ones.
    std::vector<uint64_t> available;
    std::vector<uint64_t> exhausted;
    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 state = getPoolState(pools[i]);
            if (state->getPermutation()->exhausted()) {
                // Pool is exhausted. It means that all addresses from
                // this pool have been offered. It doesn't mean that
                // leases are allocated for all these addresses. It
                // only means that all have been picked from the pool.
                exhausted.push_back(i);
            } else {
                // There are still available addresses in this pool. It
                // means that not all of them have been offered.
                available.push_back(i);
            }
        }
    }
    // Find a suitable pool.
    PoolPtr pool;
    if (!available.empty()) {
        // There are pools with available addresses. Let's randomly
        // pick one of these pools and get next available address.
        pool = pools[available[getRandomNumber(available.size() - 1)]];

    } else if (!exhausted.empty()) {
        // All pools have been exhausted. We will start offering the same
        // addresses from these pools. We need to reset the permutations
        // of the exhausted pools.
        for (auto const& e : exhausted) {
            getPoolState(pools[e])->getPermutation()->reset();
        }
        // Get random pool from those we just reset.
        pool = pools[exhausted[getRandomNumber(exhausted.size() - 1)]];
    }

    // If pool has been found, let's get next address.
    if (pool) {
        auto done = false;
        return (getPoolState(pool)->getPermutation()->next(done));
    }

    // No pool available. There are no pools or client classes do
    // not match.
    return (pool_type_ == Lease::TYPE_V4 ? IOAddress::IPV4_ZERO_ADDRESS() : IOAddress::IPV6_ZERO_ADDRESS());
}

IOAddress
RandomAllocator::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_);

    // Let's first iterate over the pools and identify the ones that
    // meet client class criteria. Then, segragate these pools into
    // the ones that still have available addresses and exhausted
    // ones.
    std::vector<uint64_t> available;
    std::vector<uint64_t> exhausted;
    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 state = getPoolState(pools[i]);
            if (state->getPermutation()->exhausted()) {
                // Pool is exhausted. It means that all prefixes from
                // this pool have been offered. It doesn't mean that
                // leases are allocated for all these prefixes. It
                // only means that all have been picked from the pool.
                exhausted.push_back(i);
            } else {
                // There are still available prefixes in this pool. It
                // means that not all of them have been offered.
                available.push_back(i);
            }
        }
    }
    // Find a suitable pool.
    PoolPtr pool;
    if (!available.empty()) {
        // There are pools with available prefixes. Let's randomly
        // pick one of these pools and get next available prefix.
        pool = pools[available[getRandomNumber(available.size() - 1)]];

    } else if (!exhausted.empty()) {
        // All pools have been exhausted. We will start offering the same
        // prefixes from these pools. We need to reset the permutations
        // of the exhausted pools.
        for (auto const& e : exhausted) {
            getPoolState(pools[e])->getPermutation()->reset();
        }
        // Get random pool from those we just reset.
        pool = pools[exhausted[getRandomNumber(exhausted.size() - 1)]];
    }

    // If pool has been found, let's get next prefix.
    if (pool) {
        auto done = false;
        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");
        }
        return (getPoolState(pool)->getPermutation()->next(done));
    }

    // No pool available. There are no pools or client classes do
    // not match.
    return (IOAddress::IPV6_ZERO_ADDRESS());
}

PoolRandomAllocationStatePtr
RandomAllocator::getPoolState(const PoolPtr& pool) const {
    if (!pool->getAllocationState()) {
        pool->setAllocationState(PoolRandomAllocationState::create(pool));
    }
    return (boost::dynamic_pointer_cast<PoolRandomAllocationState>(pool->getAllocationState()));
}

uint64_t
RandomAllocator::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