Skip to content

Commit bfbf150

Browse files
committed
network: manage address pools by OrderedSet
1 parent 3fe721c commit bfbf150

File tree

5 files changed

+18
-34
lines changed

5 files changed

+18
-34
lines changed

src/network/networkd-address-pool.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ static int address_pool_new(
1515
const union in_addr_union *u,
1616
unsigned prefixlen) {
1717

18-
AddressPool *p;
18+
_cleanup_free_ AddressPool *p = NULL;
19+
int r;
1920

2021
assert(m);
2122
assert(u);
@@ -31,8 +32,11 @@ static int address_pool_new(
3132
.in_addr = *u,
3233
};
3334

34-
LIST_PREPEND(address_pools, m->address_pools, p);
35+
r = ordered_set_ensure_put(&m->address_pools, NULL, p);
36+
if (r < 0)
37+
return r;
3538

39+
TAKE_PTR(p);
3640
return 0;
3741
}
3842

@@ -55,17 +59,6 @@ static int address_pool_new_from_string(
5559
return address_pool_new(m, family, &u, prefixlen);
5660
}
5761

58-
void address_pool_free(AddressPool *p) {
59-
60-
if (!p)
61-
return;
62-
63-
if (p->manager)
64-
LIST_REMOVE(address_pools, p->manager->address_pools, p);
65-
66-
free(p);
67-
}
68-
6962
int address_pool_setup_default(Manager *m) {
7063
int r;
7164

@@ -76,15 +69,15 @@ int address_pool_setup_default(Manager *m) {
7669
if (r < 0)
7770
return r;
7871

79-
r = address_pool_new_from_string(m, AF_INET, "10.0.0.0", 8);
72+
r = address_pool_new_from_string(m, AF_INET, "192.168.0.0", 16);
8073
if (r < 0)
8174
return r;
8275

8376
r = address_pool_new_from_string(m, AF_INET, "172.16.0.0", 12);
8477
if (r < 0)
8578
return r;
8679

87-
r = address_pool_new_from_string(m, AF_INET, "192.168.0.0", 16);
80+
r = address_pool_new_from_string(m, AF_INET, "10.0.0.0", 8);
8881
if (r < 0)
8982
return r;
9083

@@ -187,7 +180,7 @@ int address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_ad
187180
assert(prefixlen > 0);
188181
assert(found);
189182

190-
LIST_FOREACH(address_pools, p, m->address_pools) {
183+
ORDERED_SET_FOREACH(p, m->address_pools) {
191184
r = address_pool_acquire_one(p, family, prefixlen, found);
192185
if (r != 0)
193186
return r;
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
11
/* SPDX-License-Identifier: LGPL-2.1+ */
22
#pragma once
33

4-
typedef struct AddressPool AddressPool;
5-
64
#include "in-addr-util.h"
7-
#include "list.h"
85

96
typedef struct Manager Manager;
107

11-
struct AddressPool {
8+
typedef struct AddressPool {
129
Manager *manager;
1310

1411
int family;
1512
unsigned prefixlen;
16-
1713
union in_addr_union in_addr;
18-
19-
LIST_FIELDS(AddressPool, address_pools);
20-
};
21-
22-
void address_pool_free(AddressPool *p);
14+
} AddressPool;
2315

2416
int address_pool_setup_default(Manager *m);
2517
int address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found);

src/network/networkd-address.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "firewall-util.h"
77
#include "memory-util.h"
88
#include "netlink-util.h"
9+
#include "networkd-address-pool.h"
910
#include "networkd-address.h"
1011
#include "networkd-manager.h"
1112
#include "networkd-network.h"

src/network/networkd-manager.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "local-addresses.h"
2525
#include "netlink-util.h"
2626
#include "network-internal.h"
27+
#include "networkd-address-pool.h"
2728
#include "networkd-dhcp-server-bus.h"
2829
#include "networkd-dhcp6.h"
2930
#include "networkd-link-bus.h"
@@ -855,7 +856,6 @@ int manager_new(Manager **ret) {
855856
}
856857

857858
void manager_free(Manager *m) {
858-
AddressPool *pool;
859859
Link *link;
860860

861861
if (!m)
@@ -878,8 +878,7 @@ void manager_free(Manager *m) {
878878

879879
m->netdevs = hashmap_free_with_destructor(m->netdevs, netdev_unref);
880880

881-
while ((pool = m->address_pools))
882-
address_pool_free(pool);
881+
ordered_set_free_free(m->address_pools);
883882

884883
/* routing_policy_rule_free() access m->rules and m->rules_foreign.
885884
* So, it is necessary to set NULL after the sets are freed. */

src/network/networkd-manager.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@
1010

1111
#include "dhcp-identifier.h"
1212
#include "hashmap.h"
13-
#include "list.h"
14-
#include "time-util.h"
15-
16-
#include "networkd-address-pool.h"
1713
#include "networkd-link.h"
1814
#include "networkd-network.h"
15+
#include "ordered-set.h"
16+
#include "set.h"
17+
#include "time-util.h"
1918

2019
struct Manager {
2120
sd_netlink *rtnl;
@@ -45,7 +44,7 @@ struct Manager {
4544
OrderedHashmap *networks;
4645
Hashmap *dhcp6_prefixes;
4746
Set *dhcp6_pd_prefixes;
48-
LIST_HEAD(AddressPool, address_pools);
47+
OrderedSet *address_pools;
4948

5049
usec_t network_dirs_ts_usec;
5150

0 commit comments

Comments
 (0)