]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network: manage address pools by OrderedSet
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 2 Oct 2020 05:46:29 +0000 (14:46 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 6 Oct 2020 17:50:50 +0000 (02:50 +0900)
src/network/networkd-address-pool.c
src/network/networkd-address-pool.h
src/network/networkd-address.c
src/network/networkd-manager.c
src/network/networkd-manager.h

index 44fab4dd1beecd9b164fdde9d8acdf23bc1eca24..c732b6c56e44d4817e3a3914cb666b73ad2a9e21 100644 (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;
index 0c5cd5cedd35a42476a43bfeb3b3f1204d0a0b0c..c53fe7407febbd3438444b985f2d840a1fc57aae 100644 (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);
index 0efb3eafb2088863e009987cf166a2cd54b0fd43..82be47aae2d5d9105120b133bbf821572145326e 100644 (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"
index 137424929fcf8a1000dfd8ff20e14aaa1ad7da51..524dbedb1ee19987b2195a93e2ebd5f5e2cd9a91 100644 (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. */
index c4e44421e254789c86a03f9acdaad4a6e928fb2e..2e250f1509aacb0596ec9c8c8baa5d6652077fc5 100644 (file)
 
 #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;