diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2020-10-02 07:46:29 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2020-10-06 19:50:50 +0200 |
commit | bfbf150ee6a985e11bae580f5778ff1b02b17dba (patch) | |
tree | 7a41d4375fe0422405cad1f2d85bf256ec3a4811 | |
parent | network: drop unused argument (diff) | |
download | systemd-bfbf150ee6a985e11bae580f5778ff1b02b17dba.tar.xz systemd-bfbf150ee6a985e11bae580f5778ff1b02b17dba.zip |
network: manage address pools by OrderedSet
-rw-r--r-- | src/network/networkd-address-pool.c | 25 | ||||
-rw-r--r-- | src/network/networkd-address-pool.h | 12 | ||||
-rw-r--r-- | src/network/networkd-address.c | 1 | ||||
-rw-r--r-- | src/network/networkd-manager.c | 5 | ||||
-rw-r--r-- | src/network/networkd-manager.h | 9 |
5 files changed, 18 insertions, 34 deletions
diff --git a/src/network/networkd-address-pool.c b/src/network/networkd-address-pool.c index 44fab4dd1b..c732b6c56e 100644 --- a/src/network/networkd-address-pool.c +++ b/src/network/networkd-address-pool.c @@ -15,7 +15,8 @@ static int address_pool_new( const union in_addr_union *u, unsigned prefixlen) { - AddressPool *p; + _cleanup_free_ AddressPool *p = NULL; + int r; assert(m); assert(u); @@ -31,8 +32,11 @@ static int address_pool_new( .in_addr = *u, }; - LIST_PREPEND(address_pools, m->address_pools, p); + r = ordered_set_ensure_put(&m->address_pools, NULL, p); + if (r < 0) + return r; + TAKE_PTR(p); return 0; } @@ -55,17 +59,6 @@ static int address_pool_new_from_string( return address_pool_new(m, family, &u, prefixlen); } -void address_pool_free(AddressPool *p) { - - if (!p) - return; - - if (p->manager) - LIST_REMOVE(address_pools, p->manager->address_pools, p); - - free(p); -} - int address_pool_setup_default(Manager *m) { int r; @@ -76,7 +69,7 @@ int address_pool_setup_default(Manager *m) { if (r < 0) return r; - r = address_pool_new_from_string(m, AF_INET, "10.0.0.0", 8); + r = address_pool_new_from_string(m, AF_INET, "192.168.0.0", 16); if (r < 0) return r; @@ -84,7 +77,7 @@ int address_pool_setup_default(Manager *m) { if (r < 0) return r; - r = address_pool_new_from_string(m, AF_INET, "192.168.0.0", 16); + r = address_pool_new_from_string(m, AF_INET, "10.0.0.0", 8); if (r < 0) return r; @@ -187,7 +180,7 @@ int address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_ad assert(prefixlen > 0); assert(found); - LIST_FOREACH(address_pools, p, m->address_pools) { + ORDERED_SET_FOREACH(p, m->address_pools) { r = address_pool_acquire_one(p, family, prefixlen, found); if (r != 0) return r; diff --git a/src/network/networkd-address-pool.h b/src/network/networkd-address-pool.h index 0c5cd5cedd..c53fe7407f 100644 --- a/src/network/networkd-address-pool.h +++ b/src/network/networkd-address-pool.h @@ -1,25 +1,17 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once -typedef struct AddressPool AddressPool; - #include "in-addr-util.h" -#include "list.h" typedef struct Manager Manager; -struct AddressPool { +typedef struct AddressPool { Manager *manager; int family; unsigned prefixlen; - union in_addr_union in_addr; - - LIST_FIELDS(AddressPool, address_pools); -}; - -void address_pool_free(AddressPool *p); +} AddressPool; int address_pool_setup_default(Manager *m); int address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found); diff --git a/src/network/networkd-address.c b/src/network/networkd-address.c index 0efb3eafb2..82be47aae2 100644 --- a/src/network/networkd-address.c +++ b/src/network/networkd-address.c @@ -6,6 +6,7 @@ #include "firewall-util.h" #include "memory-util.h" #include "netlink-util.h" +#include "networkd-address-pool.h" #include "networkd-address.h" #include "networkd-manager.h" #include "networkd-network.h" diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c index 137424929f..524dbedb1e 100644 --- a/src/network/networkd-manager.c +++ b/src/network/networkd-manager.c @@ -24,6 +24,7 @@ #include "local-addresses.h" #include "netlink-util.h" #include "network-internal.h" +#include "networkd-address-pool.h" #include "networkd-dhcp-server-bus.h" #include "networkd-dhcp6.h" #include "networkd-link-bus.h" @@ -855,7 +856,6 @@ int manager_new(Manager **ret) { } void manager_free(Manager *m) { - AddressPool *pool; Link *link; if (!m) @@ -878,8 +878,7 @@ void manager_free(Manager *m) { m->netdevs = hashmap_free_with_destructor(m->netdevs, netdev_unref); - while ((pool = m->address_pools)) - address_pool_free(pool); + ordered_set_free_free(m->address_pools); /* routing_policy_rule_free() access m->rules and m->rules_foreign. * So, it is necessary to set NULL after the sets are freed. */ diff --git a/src/network/networkd-manager.h b/src/network/networkd-manager.h index c4e44421e2..2e250f1509 100644 --- a/src/network/networkd-manager.h +++ b/src/network/networkd-manager.h @@ -10,12 +10,11 @@ #include "dhcp-identifier.h" #include "hashmap.h" -#include "list.h" -#include "time-util.h" - -#include "networkd-address-pool.h" #include "networkd-link.h" #include "networkd-network.h" +#include "ordered-set.h" +#include "set.h" +#include "time-util.h" struct Manager { sd_netlink *rtnl; @@ -45,7 +44,7 @@ struct Manager { OrderedHashmap *networks; Hashmap *dhcp6_prefixes; Set *dhcp6_pd_prefixes; - LIST_HEAD(AddressPool, address_pools); + OrderedSet *address_pools; usec_t network_dirs_ts_usec; |