network: manage address pools by OrderedSet

This commit is contained in:
Yu Watanabe 2020-10-02 14:46:29 +09:00
parent 3fe721c674
commit bfbf150ee6
5 changed files with 18 additions and 34 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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"

View file

@ -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. */

View file

@ -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;