2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-08-22 18:08:16 +00:00

[#226] Simplified getOccupancyRate

This commit is contained in:
Francis Dupont 2025-08-12 11:09:04 +02:00
parent 233e393735
commit 02ede08a7f
7 changed files with 83 additions and 64 deletions

View File

@ -150,11 +150,9 @@ public:
/// ///
/// @param addr the address. /// @param addr the address.
/// @param client_classes list of classes client belongs to. /// @param client_classes list of classes client belongs to.
/// @param count_me the address is still marked as free.
virtual double virtual double
getOccupancyRate(const asiolink::IOAddress& addr, getOccupancyRate(const asiolink::IOAddress& addr,
const ClientClasses& client_classes, const ClientClasses& client_classes) const {
const bool count_me) const {
return (0.); return (0.);
} }
@ -169,12 +167,10 @@ public:
/// @param pref the prefix. /// @param pref the prefix.
/// @param plen the prefix length. /// @param plen the prefix length.
/// @param client_classes list of classes client belongs to. /// @param client_classes list of classes client belongs to.
/// @param count_me the prefix is still marked as free.
virtual double virtual double
getOccupancyRate(const asiolink::IOAddress& pref, getOccupancyRate(const asiolink::IOAddress& pref,
const uint8_t plen, const uint8_t plen,
const ClientClasses& client_classes, const ClientClasses& client_classes) const {
const bool count_me) const {
return (0.); return (0.);
} }

View File

@ -53,6 +53,17 @@ PoolFreeLeaseQueueAllocationState::deleteFreeLease(const asiolink::IOAddress& ad
} }
} }
bool
PoolFreeLeaseQueueAllocationState::isFreeLease(const asiolink::IOAddress& address) const {
if (free_lease4_queue_) {
auto const& idx = free_lease4_queue_->get<1>();
return (idx.count(address.toUint32()) > 0);
} else {
auto const& idx = free_lease6_queue_->get<1>();
return (idx.count(address) > 0);
}
}
IOAddress IOAddress
PoolFreeLeaseQueueAllocationState::offerFreeLease() { PoolFreeLeaseQueueAllocationState::offerFreeLease() {
if (free_lease4_queue_) { if (free_lease4_queue_) {
@ -84,4 +95,3 @@ PoolFreeLeaseQueueAllocationState::getFreeLeaseCount() const {
} // end of namespace isc::dhcp } // end of namespace isc::dhcp
} // end of namespace isc } // end of namespace isc

View File

@ -55,6 +55,11 @@ public:
/// @param address lease address. /// @param address lease address.
void deleteFreeLease(const asiolink::IOAddress& address); void deleteFreeLease(const asiolink::IOAddress& address);
/// @brief Check if a lease is in the queue.
///
/// @param address lease address.
bool isFreeLease(const asiolink::IOAddress& address) const;
/// @brief Returns next available lease. /// @brief Returns next available lease.
/// ///
/// @return next free lease address or IPv4/IPv6 zero address when /// @return next free lease address or IPv4/IPv6 zero address when

View File

@ -137,8 +137,7 @@ FreeLeaseQueueAllocator::pickPrefixInternal(const ClientClasses& client_classes,
double double
FreeLeaseQueueAllocator::getOccupancyRate(const IOAddress& addr, FreeLeaseQueueAllocator::getOccupancyRate(const IOAddress& addr,
const ClientClasses& client_classes, const ClientClasses& client_classes) const {
const bool count_me) const {
// Sanity. // Sanity.
if (!addr.isV4()) { if (!addr.isV4()) {
return (0.); return (0.);
@ -164,7 +163,7 @@ FreeLeaseQueueAllocator::getOccupancyRate(const IOAddress& addr,
uint128_t free_cnt = pool_state->getFreeLeaseCount(); uint128_t free_cnt = pool_state->getFreeLeaseCount();
if (!found && pool->inRange(addr)) { if (!found && pool->inRange(addr)) {
found = true; found = true;
if (count_me && (free_cnt > 0)) { if ((free_cnt > 0) && pool_state->isFreeLease(addr)) {
--free_cnt; --free_cnt;
} }
} }
@ -186,8 +185,7 @@ FreeLeaseQueueAllocator::getOccupancyRate(const IOAddress& addr,
double double
FreeLeaseQueueAllocator::getOccupancyRate(const IOAddress& pref, FreeLeaseQueueAllocator::getOccupancyRate(const IOAddress& pref,
const uint8_t plen, const uint8_t plen,
const ClientClasses& client_classes, const ClientClasses& client_classes) const {
const bool count_me) const {
// Sanity. // Sanity.
if (!pref.isV6()) { if (!pref.isV6()) {
return (0.); return (0.);
@ -217,7 +215,7 @@ FreeLeaseQueueAllocator::getOccupancyRate(const IOAddress& pref,
uint128_t free_cnt = pool_state->getFreeLeaseCount(); uint128_t free_cnt = pool_state->getFreeLeaseCount();
if (!found && pool->inRange(pref)) { if (!found && pool->inRange(pref)) {
found = true; found = true;
if (count_me && (free_cnt > 0)) { if ((free_cnt > 0) && pool_state->isFreeLease(pref)) {
--free_cnt; --free_cnt;
} }
} }

View File

@ -61,11 +61,9 @@ public:
/// ///
/// @param addr the address. /// @param addr the address.
/// @param client_classes list of classes client belongs to. /// @param client_classes list of classes client belongs to.
/// @param count_me the address is still marked as free.
virtual double virtual double
getOccupancyRate(const asiolink::IOAddress& addr, getOccupancyRate(const asiolink::IOAddress& addr,
const ClientClasses& client_classes, const ClientClasses& client_classes) const;
const bool count_me) const;
/// @brief Returns the occupancy rate (v6 prefixes). /// @brief Returns the occupancy rate (v6 prefixes).
/// ///
@ -78,12 +76,10 @@ public:
/// @param pref the prefix. /// @param pref the prefix.
/// @param plen the prefix length. /// @param plen the prefix length.
/// @param client_classes list of classes client belongs to. /// @param client_classes list of classes client belongs to.
/// @param count_me the prefix is still marked as free.
virtual double virtual double
getOccupancyRate(const asiolink::IOAddress& pref, getOccupancyRate(const asiolink::IOAddress& pref,
const uint8_t plen, const uint8_t plen,
const ClientClasses& client_classes, const ClientClasses& client_classes) const;
const bool count_me) const;
private: private:

View File

@ -39,12 +39,15 @@ TEST(PoolFreeLeaseAllocationState, addDeleteFreeLeaseV4) {
// Add the first free lease. The pool should now have one free lease // Add the first free lease. The pool should now have one free lease
// that is always offered. // that is always offered.
EXPECT_FALSE(state->isFreeLease(IOAddress("192.0.2.1")));
state->addFreeLease(IOAddress("192.0.2.1")); state->addFreeLease(IOAddress("192.0.2.1"));
EXPECT_FALSE(state->exhausted()); EXPECT_FALSE(state->exhausted());
EXPECT_EQ(1, state->getFreeLeaseCount()); EXPECT_EQ(1, state->getFreeLeaseCount());
EXPECT_TRUE(state->isFreeLease(IOAddress("192.0.2.1")));
// The same lease is always offered. // The same lease is always offered.
EXPECT_EQ("192.0.2.1", state->offerFreeLease().toText()); EXPECT_EQ("192.0.2.1", state->offerFreeLease().toText());
EXPECT_EQ("192.0.2.1", state->offerFreeLease().toText()); EXPECT_EQ("192.0.2.1", state->offerFreeLease().toText());
EXPECT_TRUE(state->isFreeLease(IOAddress("192.0.2.1")));
// Add another free lease. We should now have two free leases. // Add another free lease. We should now have two free leases.
state->addFreeLease(IOAddress("192.0.2.3")); state->addFreeLease(IOAddress("192.0.2.3"));
@ -55,9 +58,12 @@ TEST(PoolFreeLeaseAllocationState, addDeleteFreeLeaseV4) {
EXPECT_EQ("192.0.2.1", state->offerFreeLease().toText()); EXPECT_EQ("192.0.2.1", state->offerFreeLease().toText());
// Now, the second lease should be offered. // Now, the second lease should be offered.
EXPECT_EQ("192.0.2.3", state->offerFreeLease().toText()); EXPECT_EQ("192.0.2.3", state->offerFreeLease().toText());
EXPECT_TRUE(state->isFreeLease(IOAddress("192.0.2.1")));
EXPECT_TRUE(state->isFreeLease(IOAddress("192.0.2.3")));
// Try to delete a non-existing lease. It should not affect the // Try to delete a non-existing lease. It should not affect the
// existing leases. // existing leases.
EXPECT_FALSE(state->isFreeLease(IOAddress("192.0.2.2")));
state->deleteFreeLease(IOAddress("192.0.2.2")); state->deleteFreeLease(IOAddress("192.0.2.2"));
EXPECT_FALSE(state->exhausted()); EXPECT_FALSE(state->exhausted());
EXPECT_EQ(2, state->getFreeLeaseCount()); EXPECT_EQ(2, state->getFreeLeaseCount());
@ -126,12 +132,15 @@ TEST(PoolFreeLeaseAllocationState, addDeleteFreeLeaseNA) {
// Add the first free lease. The pool should now have one free lease // Add the first free lease. The pool should now have one free lease
// that is always offered. // that is always offered.
EXPECT_FALSE(state->isFreeLease(IOAddress("2001:db8:1::1")));
state->addFreeLease(IOAddress("2001:db8:1::1")); state->addFreeLease(IOAddress("2001:db8:1::1"));
EXPECT_FALSE(state->exhausted()); EXPECT_FALSE(state->exhausted());
EXPECT_EQ(1, state->getFreeLeaseCount()); EXPECT_EQ(1, state->getFreeLeaseCount());
EXPECT_TRUE(state->isFreeLease(IOAddress("2001:db8:1::1")));
// The same lease is always offered. // The same lease is always offered.
EXPECT_EQ("2001:db8:1::1", state->offerFreeLease().toText()); EXPECT_EQ("2001:db8:1::1", state->offerFreeLease().toText());
EXPECT_EQ("2001:db8:1::1", state->offerFreeLease().toText()); EXPECT_EQ("2001:db8:1::1", state->offerFreeLease().toText());
EXPECT_TRUE(state->isFreeLease(IOAddress("2001:db8:1::1")));
// Add another free lease. We should now have two free leases. // Add another free lease. We should now have two free leases.
state->addFreeLease(IOAddress("2001:db8:1::3")); state->addFreeLease(IOAddress("2001:db8:1::3"));
@ -142,9 +151,12 @@ TEST(PoolFreeLeaseAllocationState, addDeleteFreeLeaseNA) {
EXPECT_EQ("2001:db8:1::1", state->offerFreeLease().toText()); EXPECT_EQ("2001:db8:1::1", state->offerFreeLease().toText());
// Now, the second lease should be offered. // Now, the second lease should be offered.
EXPECT_EQ("2001:db8:1::3", state->offerFreeLease().toText()); EXPECT_EQ("2001:db8:1::3", state->offerFreeLease().toText());
EXPECT_TRUE(state->isFreeLease(IOAddress("2001:db8:1::1")));
EXPECT_TRUE(state->isFreeLease(IOAddress("2001:db8:1::3")));
// Try to delete a non-existing lease. It should not affect the // Try to delete a non-existing lease. It should not affect the
// existing leases. // existing leases.
EXPECT_FALSE(state->isFreeLease(IOAddress("2001:db8:1::2")));
state->deleteFreeLease(IOAddress("2001:db8:1::2")); state->deleteFreeLease(IOAddress("2001:db8:1::2"));
EXPECT_FALSE(state->exhausted()); EXPECT_FALSE(state->exhausted());
EXPECT_EQ(2, state->getFreeLeaseCount()); EXPECT_EQ(2, state->getFreeLeaseCount());
@ -211,11 +223,15 @@ TEST(PoolFreeLeaseAllocationState, addDeleteFreeLeasePD) {
// Add the first free lease. The pool should now have one free lease // Add the first free lease. The pool should now have one free lease
// that is always offered. // that is always offered.
EXPECT_FALSE(state->isFreeLease(IOAddress("3000::5600")));
state->addFreeLease(IOAddress("3000::5600")); state->addFreeLease(IOAddress("3000::5600"));
EXPECT_FALSE(state->exhausted()); EXPECT_FALSE(state->exhausted());
EXPECT_EQ(1, state->getFreeLeaseCount()); EXPECT_EQ(1, state->getFreeLeaseCount());
EXPECT_TRUE(state->isFreeLease(IOAddress("3000::5600")));
// The same lease is always offered. // The same lease is always offered.
EXPECT_EQ("3000::5600", state->offerFreeLease().toText()); EXPECT_EQ("3000::5600", state->offerFreeLease().toText());
EXPECT_EQ("3000::5600", state->offerFreeLease().toText());
EXPECT_TRUE(state->isFreeLease(IOAddress("3000::5600")));
// Add another free lease. We should now have two free leases. // Add another free lease. We should now have two free leases.
state->addFreeLease(IOAddress("3000::7800")); state->addFreeLease(IOAddress("3000::7800"));
@ -226,9 +242,12 @@ TEST(PoolFreeLeaseAllocationState, addDeleteFreeLeasePD) {
EXPECT_EQ("3000::5600", state->offerFreeLease().toText()); EXPECT_EQ("3000::5600", state->offerFreeLease().toText());
// Now, the second lease should be offered. // Now, the second lease should be offered.
EXPECT_EQ("3000::7800", state->offerFreeLease().toText()); EXPECT_EQ("3000::7800", state->offerFreeLease().toText());
EXPECT_TRUE(state->isFreeLease(IOAddress("3000::5600")));
EXPECT_TRUE(state->isFreeLease(IOAddress("3000::7800")));
// Try to delete a non-existing lease. It should not affect the // Try to delete a non-existing lease. It should not affect the
// existing leases. // existing leases.
EXPECT_FALSE(state->isFreeLease(IOAddress("3000::6400")));
state->deleteFreeLease(IOAddress("3000::6400")); state->deleteFreeLease(IOAddress("3000::6400"));
EXPECT_FALSE(state->exhausted()); EXPECT_FALSE(state->exhausted());
EXPECT_EQ(2, state->getFreeLeaseCount()); EXPECT_EQ(2, state->getFreeLeaseCount());
@ -274,5 +293,4 @@ TEST(PoolFreeLeaseAllocationState, addFreeLeasPDSeveralTimes) {
EXPECT_TRUE(state->exhausted()); EXPECT_TRUE(state->exhausted());
} }
} // end of anonymous namespace } // end of anonymous namespace

View File

@ -65,11 +65,9 @@ TEST_F(FreeLeaseQueueAllocatorTest4, populateFreeAddressLeases) {
ASSERT_TRUE(pool_state); ASSERT_TRUE(pool_state);
EXPECT_FALSE(pool_state->exhausted()); EXPECT_FALSE(pool_state->exhausted());
double r = alloc.getOccupancyRate(IOAddress("192.0.2.101"), cc_, false); double r = alloc.getOccupancyRate(IOAddress("192.0.2.101"), cc_);
EXPECT_EQ(.5, r);
r = alloc.getOccupancyRate(IOAddress("192.0.2.101"), cc_, true);
EXPECT_EQ(.6, r); EXPECT_EQ(.6, r);
r = alloc.getOccupancyRate(IOAddress("192.0.2.1"), cc_, false); r = alloc.getOccupancyRate(IOAddress("192.0.2.1"), cc_);
EXPECT_EQ(0., r); EXPECT_EQ(0., r);
std::set<IOAddress> addresses; std::set<IOAddress> addresses;
@ -131,7 +129,7 @@ TEST_F(FreeLeaseQueueAllocatorTest4, singlePoolWithAllocations) {
IOAddress candidate = alloc.pickAddress(cc_, clientid_, IOAddress("0.0.0.0")); IOAddress candidate = alloc.pickAddress(cc_, clientid_, IOAddress("0.0.0.0"));
EXPECT_TRUE(candidate.isV4Zero()); EXPECT_TRUE(candidate.isV4Zero());
double r = alloc.getOccupancyRate(IOAddress("192.0.2.100"), cc_, false); double r = alloc.getOccupancyRate(IOAddress("192.0.2.100"), cc_);
EXPECT_EQ(1., r); EXPECT_EQ(1., r);
auto i = 0; auto i = 0;
@ -142,7 +140,7 @@ TEST_F(FreeLeaseQueueAllocatorTest4, singlePoolWithAllocations) {
++i; ++i;
} }
r = alloc.getOccupancyRate(IOAddress("192.0.2.100"), cc_, false); r = alloc.getOccupancyRate(IOAddress("192.0.2.100"), cc_);
EXPECT_EQ(.5, r); EXPECT_EQ(.5, r);
for (auto j = 0; j < 5; ++j) { for (auto j = 0; j < 5; ++j) {
@ -156,7 +154,7 @@ TEST_F(FreeLeaseQueueAllocatorTest4, singlePoolWithAllocations) {
candidate = alloc.pickAddress(cc_, clientid_, IOAddress("0.0.0.0")); candidate = alloc.pickAddress(cc_, clientid_, IOAddress("0.0.0.0"));
EXPECT_TRUE(candidate.isV4Zero()); EXPECT_TRUE(candidate.isV4Zero());
r = alloc.getOccupancyRate(IOAddress("192.0.2.100"), cc_, false); r = alloc.getOccupancyRate(IOAddress("192.0.2.100"), cc_);
EXPECT_EQ(1., r); EXPECT_EQ(1., r);
} }
@ -186,7 +184,7 @@ TEST_F(FreeLeaseQueueAllocatorTest4, singlePoolWithReclamations) {
IOAddress candidate = alloc.pickAddress(cc_, clientid_, IOAddress("0.0.0.0")); IOAddress candidate = alloc.pickAddress(cc_, clientid_, IOAddress("0.0.0.0"));
EXPECT_TRUE(candidate.isV4Zero()); EXPECT_TRUE(candidate.isV4Zero());
double r = alloc.getOccupancyRate(IOAddress("192.0.2.100"), cc_, false); double r = alloc.getOccupancyRate(IOAddress("192.0.2.100"), cc_);
EXPECT_EQ(1., r); EXPECT_EQ(1., r);
auto i = 0; auto i = 0;
@ -198,7 +196,7 @@ TEST_F(FreeLeaseQueueAllocatorTest4, singlePoolWithReclamations) {
} }
++i; ++i;
} }
r = alloc.getOccupancyRate(IOAddress("192.0.2.100"), cc_, false); r = alloc.getOccupancyRate(IOAddress("192.0.2.100"), cc_);
EXPECT_EQ(.5, r); EXPECT_EQ(.5, r);
for (auto j = 0; j < 5; ++j) { for (auto j = 0; j < 5; ++j) {
@ -212,7 +210,7 @@ TEST_F(FreeLeaseQueueAllocatorTest4, singlePoolWithReclamations) {
candidate = alloc.pickAddress(cc_, clientid_, IOAddress("0.0.0.0")); candidate = alloc.pickAddress(cc_, clientid_, IOAddress("0.0.0.0"));
EXPECT_TRUE(candidate.isV4Zero()); EXPECT_TRUE(candidate.isV4Zero());
r = alloc.getOccupancyRate(IOAddress("192.0.2.100"), cc_, false); r = alloc.getOccupancyRate(IOAddress("192.0.2.100"), cc_);
EXPECT_EQ(1., r); EXPECT_EQ(1., r);
} }
@ -237,8 +235,9 @@ TEST_F(FreeLeaseQueueAllocatorTest4, manyPools) {
auto& lease_mgr = LeaseMgrFactory::instance(); auto& lease_mgr = LeaseMgrFactory::instance();
double r = alloc.getOccupancyRate(IOAddress("192.0.2.100"), cc_, false); double r = alloc.getOccupancyRate(IOAddress("192.0.2.100"), cc_);
EXPECT_EQ(0., r); // 1/100
EXPECT_EQ(.01, r);
std::set<IOAddress> addresses_set; std::set<IOAddress> addresses_set;
std::vector<IOAddress> addresses_vector; std::vector<IOAddress> addresses_vector;
@ -259,7 +258,7 @@ TEST_F(FreeLeaseQueueAllocatorTest4, manyPools) {
// Make sure that unique addresses have been returned. // Make sure that unique addresses have been returned.
EXPECT_EQ(total, addresses_set.size()); EXPECT_EQ(total, addresses_set.size());
r = alloc.getOccupancyRate(IOAddress("192.0.2.100"), cc_, false); r = alloc.getOccupancyRate(IOAddress("192.0.2.100"), cc_);
EXPECT_EQ(1., r); EXPECT_EQ(1., r);
// Verify that the addresses are returned in the random order. // Verify that the addresses are returned in the random order.
@ -301,7 +300,7 @@ TEST_F(FreeLeaseQueueAllocatorTest4, noPools) {
EXPECT_TRUE(candidate.isV4Zero()); EXPECT_TRUE(candidate.isV4Zero());
// rate is 0. because of the address can't be found, not from 0./0.... // rate is 0. because of the address can't be found, not from 0./0....
double r = alloc.getOccupancyRate(IOAddress("192.0.2.100"), cc_, false); double r = alloc.getOccupancyRate(IOAddress("192.0.2.100"), cc_);
EXPECT_EQ(0., r); EXPECT_EQ(0., r);
} }
@ -338,8 +337,9 @@ TEST_F(FreeLeaseQueueAllocatorTest4, clientClasses) {
// Simulate client's request belonging to the class bar. // Simulate client's request belonging to the class bar.
cc_.insert("bar"); cc_.insert("bar");
double r = alloc.getOccupancyRate(IOAddress("192.0.2.120"), cc_, false); double r = alloc.getOccupancyRate(IOAddress("192.0.2.120"), cc_);
EXPECT_EQ(0., r); // 1/20
EXPECT_EQ(.05, r);
for (auto i = 0; i < 20; ++i) { for (auto i = 0; i < 20; ++i) {
// Allocate random addresses and make sure they belong to the // Allocate random addresses and make sure they belong to the
// pools associated with the class bar. // pools associated with the class bar.
@ -351,7 +351,7 @@ TEST_F(FreeLeaseQueueAllocatorTest4, clientClasses) {
} }
EXPECT_EQ(20, addresses_set.size()); EXPECT_EQ(20, addresses_set.size());
r = alloc.getOccupancyRate(IOAddress("192.0.2.120"), cc_, false); r = alloc.getOccupancyRate(IOAddress("192.0.2.120"), cc_);
EXPECT_EQ(1., r); EXPECT_EQ(1., r);
addresses_set.clear(); addresses_set.clear();
@ -359,8 +359,9 @@ TEST_F(FreeLeaseQueueAllocatorTest4, clientClasses) {
// Simulate the case that the client also belongs to the class foo. // Simulate the case that the client also belongs to the class foo.
// All pools should now be available. // All pools should now be available.
cc_.insert("foo"); cc_.insert("foo");
r = alloc.getOccupancyRate(IOAddress("192.0.2.100"), cc_, false); r = alloc.getOccupancyRate(IOAddress("192.0.2.100"), cc_);
EXPECT_EQ(.5, r); // 21/40
EXPECT_EQ(.525, r);
for (auto i = 0; i < 20; ++i) { for (auto i = 0; i < 20; ++i) {
IOAddress candidate = alloc.pickAddress(cc_, clientid_, IOAddress("0.0.0.0")); IOAddress candidate = alloc.pickAddress(cc_, clientid_, IOAddress("0.0.0.0"));
addresses_set.insert(candidate); addresses_set.insert(candidate);
@ -368,7 +369,7 @@ TEST_F(FreeLeaseQueueAllocatorTest4, clientClasses) {
EXPECT_TRUE(subnet_->inRange(candidate)); EXPECT_TRUE(subnet_->inRange(candidate));
} }
EXPECT_EQ(20, addresses_set.size()); EXPECT_EQ(20, addresses_set.size());
r = alloc.getOccupancyRate(IOAddress("192.0.2.100"), cc_, false); r = alloc.getOccupancyRate(IOAddress("192.0.2.100"), cc_);
EXPECT_EQ(1., r); EXPECT_EQ(1., r);
// When the client does not belong to any client class the allocator // When the client does not belong to any client class the allocator
@ -376,7 +377,7 @@ TEST_F(FreeLeaseQueueAllocatorTest4, clientClasses) {
cc_.clear(); cc_.clear();
IOAddress candidate = alloc.pickAddress(cc_, clientid_, IOAddress("0.0.0.0")); IOAddress candidate = alloc.pickAddress(cc_, clientid_, IOAddress("0.0.0.0"));
EXPECT_TRUE(candidate.isV4Zero()); EXPECT_TRUE(candidate.isV4Zero());
r = alloc.getOccupancyRate(IOAddress("192.0.2.100"), cc_, false); r = alloc.getOccupancyRate(IOAddress("192.0.2.100"), cc_);
EXPECT_EQ(0., r); EXPECT_EQ(0., r);
} }
@ -428,7 +429,7 @@ TEST_F(FreeLeaseQueueAllocatorTest6, populateFreeAddressLeases) {
EXPECT_NO_THROW(alloc.initAfterConfigure()); EXPECT_NO_THROW(alloc.initAfterConfigure());
// Address getOccupancyRate is for IPv4 only. // Address getOccupancyRate is for IPv4 only.
double r = alloc.getOccupancyRate(IOAddress("2001:db8:1::10"), cc_, false); double r = alloc.getOccupancyRate(IOAddress("2001:db8:1::10"), cc_);
EXPECT_EQ(0., r); EXPECT_EQ(0., r);
auto pool_state = boost::dynamic_pointer_cast<PoolFreeLeaseQueueAllocationState>(pool_->getAllocationState()); auto pool_state = boost::dynamic_pointer_cast<PoolFreeLeaseQueueAllocationState>(pool_->getAllocationState());
@ -735,8 +736,7 @@ TEST_F(FreeLeaseQueueAllocatorTest6, populateFreePrefixDelegationLeases) {
ASSERT_TRUE(pool_state); ASSERT_TRUE(pool_state);
EXPECT_FALSE(pool_state->exhausted()); EXPECT_FALSE(pool_state->exhausted());
double r = alloc.getOccupancyRate(IOAddress("2001:db8:2::"), double r = alloc.getOccupancyRate(IOAddress("2001:db8:2::"), 128, cc_);
128, cc_, false);
EXPECT_EQ(5. / 256., r); EXPECT_EQ(5. / 256., r);
std::set<IOAddress> addresses; std::set<IOAddress> addresses;
@ -775,8 +775,7 @@ TEST_F(FreeLeaseQueueAllocatorTest6, singlePdPool) {
// The pool comprises 65536 prefixes. All should be returned. // The pool comprises 65536 prefixes. All should be returned.
EXPECT_EQ(65536, prefixes.size()); EXPECT_EQ(65536, prefixes.size());
double r = alloc.getOccupancyRate(IOAddress("2001:db8:1:2::"), double r = alloc.getOccupancyRate(IOAddress("2001:db8:1:2::"), 128, cc_);
128, cc_, false);
EXPECT_EQ(1., r); EXPECT_EQ(1., r);
} }
@ -814,7 +813,7 @@ TEST_F(FreeLeaseQueueAllocatorTest6, singlePdPoolWithAllocations) {
IOAddress candidate = alloc.pickPrefix(cc_, pool, duid_, Allocator::PREFIX_LEN_HIGHER, IOAddress("::"), 0); IOAddress candidate = alloc.pickPrefix(cc_, pool, duid_, Allocator::PREFIX_LEN_HIGHER, IOAddress("::"), 0);
EXPECT_TRUE(candidate.isV6Zero()); EXPECT_TRUE(candidate.isV6Zero());
double r = alloc.getOccupancyRate(IOAddress("3000::"), 128, cc_, false); double r = alloc.getOccupancyRate(IOAddress("3000::"), 128, cc_);
EXPECT_EQ(1., r); EXPECT_EQ(1., r);
auto i = 0; auto i = 0;
@ -824,10 +823,8 @@ TEST_F(FreeLeaseQueueAllocatorTest6, singlePdPoolWithAllocations) {
} }
++i; ++i;
} }
r = alloc.getOccupancyRate(IOAddress("3000::"), 128, cc_, false); r = alloc.getOccupancyRate(IOAddress("3000::"), 128, cc_);
EXPECT_EQ(.5, r); EXPECT_EQ(.5, r);
r = alloc.getOccupancyRate(IOAddress("3000::"), 128, cc_, true);
EXPECT_EQ(129. / 256., r);
for (auto j = 0; j < 128; ++j) { for (auto j = 0; j < 128; ++j) {
candidate = alloc.pickPrefix(cc_, pool, duid_, Allocator::PREFIX_LEN_HIGHER, IOAddress("::"), 0); candidate = alloc.pickPrefix(cc_, pool, duid_, Allocator::PREFIX_LEN_HIGHER, IOAddress("::"), 0);
@ -840,7 +837,7 @@ TEST_F(FreeLeaseQueueAllocatorTest6, singlePdPoolWithAllocations) {
candidate = alloc.pickPrefix(cc_, pool, duid_, Allocator::PREFIX_LEN_HIGHER, IOAddress("::"), 0); candidate = alloc.pickPrefix(cc_, pool, duid_, Allocator::PREFIX_LEN_HIGHER, IOAddress("::"), 0);
EXPECT_TRUE(candidate.isV6Zero()); EXPECT_TRUE(candidate.isV6Zero());
r = alloc.getOccupancyRate(IOAddress("3000::"), 128, cc_, false); r = alloc.getOccupancyRate(IOAddress("3000::"), 128, cc_);
EXPECT_EQ(1., r); EXPECT_EQ(1., r);
} }
@ -878,7 +875,7 @@ TEST_F(FreeLeaseQueueAllocatorTest6, singlePdPoolWithReclamations) {
IOAddress candidate = alloc.pickPrefix(cc_, pool, duid_, Allocator::PREFIX_LEN_HIGHER, IOAddress("::"), 0); IOAddress candidate = alloc.pickPrefix(cc_, pool, duid_, Allocator::PREFIX_LEN_HIGHER, IOAddress("::"), 0);
EXPECT_TRUE(candidate.isV6Zero()); EXPECT_TRUE(candidate.isV6Zero());
double r = alloc.getOccupancyRate(IOAddress("3000::"), 128, cc_, false); double r = alloc.getOccupancyRate(IOAddress("3000::"), 128, cc_);
EXPECT_EQ(1., r); EXPECT_EQ(1., r);
auto i = 0; auto i = 0;
@ -890,7 +887,7 @@ TEST_F(FreeLeaseQueueAllocatorTest6, singlePdPoolWithReclamations) {
} }
++i; ++i;
} }
r = alloc.getOccupancyRate(IOAddress("3000::"), 128, cc_, false); r = alloc.getOccupancyRate(IOAddress("3000::"), 128, cc_);
EXPECT_EQ(.5, r); EXPECT_EQ(.5, r);
for (auto j = 0; j < 128; ++j) { for (auto j = 0; j < 128; ++j) {
@ -905,7 +902,7 @@ TEST_F(FreeLeaseQueueAllocatorTest6, singlePdPoolWithReclamations) {
candidate = alloc.pickPrefix(cc_, pool, duid_, Allocator::PREFIX_LEN_HIGHER, IOAddress("::"), 0); candidate = alloc.pickPrefix(cc_, pool, duid_, Allocator::PREFIX_LEN_HIGHER, IOAddress("::"), 0);
EXPECT_TRUE(candidate.isV6Zero()); EXPECT_TRUE(candidate.isV6Zero());
r = alloc.getOccupancyRate(IOAddress("3000::"), 128, cc_, false); r = alloc.getOccupancyRate(IOAddress("3000::"), 128, cc_);
EXPECT_EQ(1., r); EXPECT_EQ(1., r);
} }
@ -942,7 +939,7 @@ TEST_F(FreeLeaseQueueAllocatorTest6, manyPdPools) {
// Make sure that unique prefixes have been returned. // Make sure that unique prefixes have been returned.
EXPECT_EQ(total, prefixes.size()); EXPECT_EQ(total, prefixes.size());
double r = alloc.getOccupancyRate(IOAddress("3001::"), 128, cc_, false); double r = alloc.getOccupancyRate(IOAddress("3001::"), 128, cc_);
EXPECT_EQ(1., r); EXPECT_EQ(1., r);
} }
@ -979,12 +976,11 @@ TEST_F(FreeLeaseQueueAllocatorTest6, manyPdPoolsPreferLower) {
// Make sure that unique prefixes have been returned. // Make sure that unique prefixes have been returned.
EXPECT_EQ(total, prefixes.size()); EXPECT_EQ(total, prefixes.size());
double r = alloc.getOccupancyRate(IOAddress("2001:db8:1:2::"), double r = alloc.getOccupancyRate(IOAddress("2001:db8:1:2::"), 120, cc_);
120, cc_, false);
EXPECT_EQ(1., r); EXPECT_EQ(1., r);
r = alloc.getOccupancyRate(IOAddress("2001:db8:1:2::"), 128, cc_, false); r = alloc.getOccupancyRate(IOAddress("2001:db8:1:2::"), 128, cc_);
EXPECT_EQ(65536. / 68096., r); EXPECT_EQ(65536. / 68096., r);
r = alloc.getOccupancyRate(IOAddress("2001:db8:1:2::"), 64, cc_, false); r = alloc.getOccupancyRate(IOAddress("2001:db8:1:2::"), 64, cc_);
EXPECT_EQ(0., r); EXPECT_EQ(0., r);
} }
@ -1020,7 +1016,7 @@ TEST_F(FreeLeaseQueueAllocatorTest6, manyPdPoolsPreferEqual) {
} }
// Make sure that unique prefixes have been returned. // Make sure that unique prefixes have been returned.
EXPECT_EQ(total, prefixes.size()); EXPECT_EQ(total, prefixes.size());
double r = alloc.getOccupancyRate(IOAddress("3001::"), 128, cc_, false); double r = alloc.getOccupancyRate(IOAddress("3001::"), 128, cc_);
EXPECT_EQ(2560. / 68096., r); EXPECT_EQ(2560. / 68096., r);
} }
@ -1056,7 +1052,7 @@ TEST_F(FreeLeaseQueueAllocatorTest6, manyPdPoolsPreferHigher) {
} }
// Make sure that unique prefixes have been returned. // Make sure that unique prefixes have been returned.
EXPECT_EQ(total, prefixes.size()); EXPECT_EQ(total, prefixes.size());
double r = alloc.getOccupancyRate(IOAddress("3001::"), 128, cc_, false); double r = alloc.getOccupancyRate(IOAddress("3001::"), 128, cc_);
EXPECT_EQ(2560. / 68096., r); EXPECT_EQ(2560. / 68096., r);
} }
@ -1095,13 +1091,13 @@ TEST_F(FreeLeaseQueueAllocatorTest6, pdPoolsClientClasses) {
candidate = alloc.pickPrefix(cc_, pool, duid_, Allocator::PREFIX_LEN_HIGHER, IOAddress("::"), 64); candidate = alloc.pickPrefix(cc_, pool, duid_, Allocator::PREFIX_LEN_HIGHER, IOAddress("::"), 64);
EXPECT_TRUE(candidate.isV6Zero()); EXPECT_TRUE(candidate.isV6Zero());
double r = alloc.getOccupancyRate(IOAddress("3000:1::"), 128, cc_, false); double r = alloc.getOccupancyRate(IOAddress("3000:1::"), 128, cc_);
EXPECT_EQ(1., r); EXPECT_EQ(1., r);
cc_.insert("foo"); cc_.insert("foo");
r = alloc.getOccupancyRate(IOAddress("3000:1::"), 128, cc_, false); r = alloc.getOccupancyRate(IOAddress("3000:1::"), 128, cc_);
EXPECT_EQ(256. / 65792., r); EXPECT_EQ(256. / 65792., r);
cc_.clear(); cc_.clear();
r = alloc.getOccupancyRate(IOAddress("3000:1::"), 128, cc_, false); r = alloc.getOccupancyRate(IOAddress("3000:1::"), 128, cc_);
EXPECT_EQ(0., r); EXPECT_EQ(0., r);
} }