]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network: drop redundant list of prefixes and route prefixes
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 29 Sep 2020 20:54:19 +0000 (05:54 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 6 Oct 2020 17:44:42 +0000 (02:44 +0900)
src/network/networkd-link.c
src/network/networkd-network.c
src/network/networkd-network.h
src/network/networkd-radv.c
src/network/networkd-radv.h

index b161f1801eb0ce1426a98c01f68b6b620a5325ae..5c29af2c2d0aca4cd72617d920e3ec3514ba29a6 100644 (file)
@@ -1239,7 +1239,6 @@ static int static_address_configure(Address *address, Link *link, bool update) {
 
 static int link_request_set_addresses(Link *link) {
         Address *ad;
-        Prefix *p;
         int r;
 
         assert(link);
@@ -1286,8 +1285,10 @@ static int link_request_set_addresses(Link *link) {
                         return r;
         }
 
-        if (link->network->router_prefix_delegation & RADV_PREFIX_DELEGATION_STATIC)
-                LIST_FOREACH(prefixes, p, link->network->static_prefixes) {
+        if (link->network->router_prefix_delegation & RADV_PREFIX_DELEGATION_STATIC) {
+                Prefix *p;
+
+                HASHMAP_FOREACH(p, link->network->prefixes_by_section) {
                         _cleanup_(address_freep) Address *address = NULL;
 
                         if (!p->assign)
@@ -1310,6 +1311,7 @@ static int link_request_set_addresses(Link *link) {
                         if (r < 0)
                                 return r;
                 }
+        }
 
         r = link_set_address_labels(link);
         if (r < 0)
index 4d094b107c69bc02a2556ff29fb6c055b0e98f5a..348be6ad0bf9161958b724b44147729a9d2bb5fc 100644 (file)
@@ -154,9 +154,9 @@ static int network_resolve_stacked_netdevs(Network *network) {
 }
 
 int network_verify(Network *network) {
-        RoutePrefix *route_prefix, *route_prefix_next;
+        RoutePrefix *route_prefix;
         Address *address, *address_next;
-        Prefix *prefix, *prefix_next;
+        Prefix *prefix;
         Route *route, *route_next;
         TrafficControl *tc;
         SRIOV *sr_iov;
@@ -309,11 +309,11 @@ int network_verify(Network *network) {
         network_verify_neighbors(network);
         network_verify_address_labels(network);
 
-        LIST_FOREACH_SAFE(prefixes, prefix, prefix_next, network->static_prefixes)
+        HASHMAP_FOREACH(prefix, network->prefixes_by_section)
                 if (section_is_invalid(prefix->section))
                         prefix_free(prefix);
 
-        LIST_FOREACH_SAFE(route_prefixes, route_prefix, route_prefix_next, network->static_route_prefixes)
+        HASHMAP_FOREACH(route_prefix, network->route_prefixes_by_section)
                 if (section_is_invalid(route_prefix->section))
                         route_prefix_free(route_prefix);
 
@@ -633,9 +633,7 @@ failure:
 }
 
 static Network *network_free(Network *network) {
-        RoutePrefix *route_prefix;
         Address *address;
-        Prefix *prefix;
         Route *route;
 
         if (!network)
@@ -699,12 +697,6 @@ static Network *network_free(Network *network) {
         while ((address = network->static_addresses))
                 address_free(address);
 
-        while ((prefix = network->static_prefixes))
-                prefix_free(prefix);
-
-        while ((route_prefix = network->static_route_prefixes))
-                route_prefix_free(route_prefix);
-
         set_free_free(network->ipv6_proxy_ndp_addresses);
         hashmap_free(network->addresses_by_section);
         hashmap_free(network->routes_by_section);
@@ -713,8 +705,8 @@ static Network *network_free(Network *network) {
         hashmap_free_with_destructor(network->mdb_entries_by_section, mdb_entry_free);
         hashmap_free_with_destructor(network->neighbors_by_section, neighbor_free);
         hashmap_free_with_destructor(network->address_labels_by_section, address_label_free);
-        hashmap_free(network->prefixes_by_section);
-        hashmap_free(network->route_prefixes_by_section);
+        hashmap_free_with_destructor(network->prefixes_by_section, prefix_free);
+        hashmap_free_with_destructor(network->route_prefixes_by_section, route_prefix_free);
         hashmap_free_with_destructor(network->rules_by_section, routing_policy_rule_free);
         ordered_hashmap_free_with_destructor(network->sr_iov_by_section, sr_iov_free);
         ordered_hashmap_free_with_destructor(network->tc_by_section, traffic_control_free);
@@ -851,7 +843,7 @@ bool network_has_static_ipv6_configurations(Network *network) {
         if (!hashmap_isempty(network->address_labels_by_section))
                 return true;
 
-        if (!LIST_IS_EMPTY(network->static_prefixes))
+        if (!hashmap_isempty(network->prefixes_by_section))
                 return true;
 
         return false;
index bdfef34badd5048792456ce677dca09cb9667e7a..b7a93fb942cabe0404ea57a120128fbd95eb7cc2 100644 (file)
@@ -281,13 +281,9 @@ struct Network {
 
         LIST_HEAD(Address, static_addresses);
         LIST_HEAD(Route, static_routes);
-        LIST_HEAD(Prefix, static_prefixes);
-        LIST_HEAD(RoutePrefix, static_route_prefixes);
 
         unsigned n_static_addresses;
         unsigned n_static_routes;
-        unsigned n_static_prefixes;
-        unsigned n_static_route_prefixes;
 
         Hashmap *addresses_by_section;
         Hashmap *routes_by_section;
index 3382fa1f5a68d4731117cb56ff33fbaec8a6d276..f5e1926afb7153c4a16385eff9fb552086266397 100644 (file)
@@ -21,13 +21,8 @@ Prefix *prefix_free(Prefix *prefix) {
                 return NULL;
 
         if (prefix->network) {
-                LIST_REMOVE(prefixes, prefix->network->static_prefixes, prefix);
-                assert(prefix->network->n_static_prefixes > 0);
-                prefix->network->n_static_prefixes--;
-
-                if (prefix->section)
-                        hashmap_remove(prefix->network->prefixes_by_section,
-                                       prefix->section);
+                assert(prefix->section);
+                hashmap_remove(prefix->network->prefixes_by_section, prefix->section);
         }
 
         network_config_section_free(prefix->section);
@@ -61,21 +56,17 @@ static int prefix_new_static(Network *network, const char *filename,
 
         assert(network);
         assert(ret);
-        assert(!!filename == (section_line > 0));
-
-        if (filename) {
-                r = network_config_section_new(filename, section_line, &n);
-                if (r < 0)
-                        return r;
+        assert(filename);
+        assert(section_line > 0);
 
-                if (section_line) {
-                        prefix = hashmap_get(network->prefixes_by_section, n);
-                        if (prefix) {
-                                *ret = TAKE_PTR(prefix);
+        r = network_config_section_new(filename, section_line, &n);
+        if (r < 0)
+                return r;
 
-                                return 0;
-                        }
-                }
+        prefix = hashmap_get(network->prefixes_by_section, n);
+        if (prefix) {
+                *ret = TAKE_PTR(prefix);
+                return 0;
         }
 
         r = prefix_new(&prefix);
@@ -83,20 +74,15 @@ static int prefix_new_static(Network *network, const char *filename,
                 return r;
 
         prefix->network = network;
-        LIST_APPEND(prefixes, network->static_prefixes, prefix);
-        network->n_static_prefixes++;
-
-        if (filename) {
-                prefix->section = TAKE_PTR(n);
+        prefix->section = TAKE_PTR(n);
 
-                r = hashmap_ensure_allocated(&network->prefixes_by_section, &network_config_hash_ops);
-                if (r < 0)
-                        return r;
+        r = hashmap_ensure_allocated(&network->prefixes_by_section, &network_config_hash_ops);
+        if (r < 0)
+                return r;
 
-                r = hashmap_put(network->prefixes_by_section, prefix->section, prefix);
-                if (r < 0)
-                        return r;
-        }
+        r = hashmap_put(network->prefixes_by_section, prefix->section, prefix);
+        if (r < 0)
+                return r;
 
         *ret = TAKE_PTR(prefix);
 
@@ -108,13 +94,8 @@ RoutePrefix *route_prefix_free(RoutePrefix *prefix) {
                 return NULL;
 
         if (prefix->network) {
-                LIST_REMOVE(route_prefixes, prefix->network->static_route_prefixes, prefix);
-                assert(prefix->network->n_static_route_prefixes > 0);
-                prefix->network->n_static_route_prefixes--;
-
-                if (prefix->section)
-                        hashmap_remove(prefix->network->route_prefixes_by_section,
-                                       prefix->section);
+                assert(prefix->section);
+                hashmap_remove(prefix->network->route_prefixes_by_section, prefix->section);
         }
 
         network_config_section_free(prefix->section);
@@ -148,21 +129,17 @@ static int route_prefix_new_static(Network *network, const char *filename,
 
         assert(network);
         assert(ret);
-        assert(!!filename == (section_line > 0));
-
-        if (filename) {
-                r = network_config_section_new(filename, section_line, &n);
-                if (r < 0)
-                        return r;
+        assert(filename);
+        assert(section_line > 0);
 
-                if (section_line) {
-                        prefix = hashmap_get(network->route_prefixes_by_section, n);
-                        if (prefix) {
-                                *ret = TAKE_PTR(prefix);
+        r = network_config_section_new(filename, section_line, &n);
+        if (r < 0)
+                return r;
 
-                                return 0;
-                        }
-                }
+        prefix = hashmap_get(network->route_prefixes_by_section, n);
+        if (prefix) {
+                *ret = TAKE_PTR(prefix);
+                return 0;
         }
 
         r = route_prefix_new(&prefix);
@@ -170,20 +147,15 @@ static int route_prefix_new_static(Network *network, const char *filename,
                 return r;
 
         prefix->network = network;
-        LIST_APPEND(route_prefixes, network->static_route_prefixes, prefix);
-        network->n_static_route_prefixes++;
-
-        if (filename) {
-                prefix->section = TAKE_PTR(n);
+        prefix->section = TAKE_PTR(n);
 
-                r = hashmap_ensure_allocated(&network->route_prefixes_by_section, &network_config_hash_ops);
-                if (r < 0)
-                        return r;
+        r = hashmap_ensure_allocated(&network->route_prefixes_by_section, &network_config_hash_ops);
+        if (r < 0)
+                return r;
 
-                r = hashmap_put(network->route_prefixes_by_section, prefix->section, prefix);
-                if (r < 0)
-                        return r;
-        }
+        r = hashmap_put(network->route_prefixes_by_section, prefix->section, prefix);
+        if (r < 0)
+                return r;
 
         *ret = TAKE_PTR(prefix);
 
@@ -609,8 +581,6 @@ int radv_emit_dns(Link *link) {
 }
 
 int radv_configure(Link *link) {
-        RoutePrefix *q;
-        Prefix *p;
         int r;
 
         assert(link);
@@ -655,7 +625,10 @@ int radv_configure(Link *link) {
         }
 
         if (link->network->router_prefix_delegation & RADV_PREFIX_DELEGATION_STATIC) {
-                LIST_FOREACH(prefixes, p, link->network->static_prefixes) {
+                RoutePrefix *q;
+                Prefix *p;
+
+                HASHMAP_FOREACH(p, link->network->prefixes_by_section) {
                         r = sd_radv_add_prefix(link->radv, p->radv_prefix, false);
                         if (r == -EEXIST)
                                 continue;
@@ -667,7 +640,7 @@ int radv_configure(Link *link) {
                                 return r;
                 }
 
-                LIST_FOREACH(route_prefixes, q, link->network->static_route_prefixes) {
+                HASHMAP_FOREACH(q, link->network->route_prefixes_by_section) {
                         r = sd_radv_add_route_prefix(link->radv, q->radv_route_prefix, false);
                         if (r == -EEXIST)
                                 continue;
index 7addf45e94be09eab1ded4e6023602feb6bf8071..6fe1c947bab11d9646a1b57bfb0aa2ad4a21dfdc 100644 (file)
@@ -29,8 +29,6 @@ struct Prefix {
         sd_radv_prefix *radv_prefix;
 
         bool assign;
-
-        LIST_FIELDS(Prefix, prefixes);
 };
 
 struct RoutePrefix {
@@ -38,8 +36,6 @@ struct RoutePrefix {
         NetworkConfigSection *section;
 
         sd_radv_route_prefix *radv_route_prefix;
-
-        LIST_FIELDS(RoutePrefix, route_prefixes);
 };
 
 Prefix *prefix_free(Prefix *prefix);