From 4212d6a1223fde8cc08a272776270c920317cae1 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 22 Aug 2023 12:14:40 +0900 Subject: [PATCH 1/2] network/address-pool: split-out address_intersect() No functional change, just refactoring and preparation for later commits. --- src/network/networkd-address-pool.c | 46 +++++++++++++++-------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/src/network/networkd-address-pool.c b/src/network/networkd-address-pool.c index 51f092ac69c..c8c6b82fc1f 100644 --- a/src/network/networkd-address-pool.c +++ b/src/network/networkd-address-pool.c @@ -84,42 +84,44 @@ int address_pool_setup_default(Manager *m) { return 0; } +static bool address_intersect( + const Address *a, + int family, + const union in_addr_union *u, + unsigned prefixlen) { + + assert(a); + assert(u); + + if (a->family != family) + return false; + + return in_addr_prefix_intersect(family, u, prefixlen, &a->in_addr, a->prefixlen); +} + static bool address_pool_prefix_is_taken( AddressPool *p, const union in_addr_union *u, unsigned prefixlen) { + Address *a; Link *l; Network *n; assert(p); assert(u); - HASHMAP_FOREACH(l, p->manager->links_by_index) { - Address *a; - - /* Don't clash with assigned addresses */ - SET_FOREACH(a, l->addresses) { - if (a->family != p->family) - continue; - - if (in_addr_prefix_intersect(p->family, u, prefixlen, &a->in_addr, a->prefixlen)) + /* Don't clash with assigned addresses. */ + HASHMAP_FOREACH(l, p->manager->links_by_index) + SET_FOREACH(a, l->addresses) + if (address_intersect(a, p->family, u, prefixlen)) return true; - } - } - /* And don't clash with configured but un-assigned addresses either */ - ORDERED_HASHMAP_FOREACH(n, p->manager->networks) { - Address *a; - - ORDERED_HASHMAP_FOREACH(a, n->addresses_by_section) { - if (a->family != p->family) - continue; - - if (in_addr_prefix_intersect(p->family, u, prefixlen, &a->in_addr, a->prefixlen)) + /* And don't clash with configured but un-assigned addresses either. */ + ORDERED_HASHMAP_FOREACH(n, p->manager->networks) + ORDERED_HASHMAP_FOREACH(a, n->addresses_by_section) + if (address_intersect(a, p->family, u, prefixlen)) return true; - } - } return false; } From 826a46fc336122731b4914a5cf37c7f634dcb3af Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 22 Aug 2023 12:22:13 +0900 Subject: [PATCH 2/2] inetwork/address-pool: also check queued addresses After 0a0c2672dbd22dc85d660e5baa7e1bef701beb88, acquired and queued addresses are not directly saved into Link object, but it will be saved later the addresses are really assigned to the interface. So, when searching free address space, we also need to check the queued addresses. --- src/network/networkd-address-pool.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/network/networkd-address-pool.c b/src/network/networkd-address-pool.c index c8c6b82fc1f..d9ac78a1a9f 100644 --- a/src/network/networkd-address-pool.c +++ b/src/network/networkd-address-pool.c @@ -4,7 +4,7 @@ #include "networkd-address-pool.h" #include "networkd-address.h" #include "networkd-manager.h" -#include "set.h" +#include "networkd-queue.h" #include "string-util.h" #define RANDOM_PREFIX_TRIAL_MAX 1024 @@ -107,6 +107,7 @@ static bool address_pool_prefix_is_taken( Address *a; Link *l; Network *n; + Request *req; assert(p); assert(u); @@ -123,6 +124,15 @@ static bool address_pool_prefix_is_taken( if (address_intersect(a, p->family, u, prefixlen)) return true; + /* Also check queued addresses. */ + ORDERED_SET_FOREACH(req, p->manager->request_queue) { + if (req->type != REQUEST_TYPE_ADDRESS) + continue; + + if (address_intersect(req->userdata, p->family, u, prefixlen)) + return true; + } + return false; }