]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network: drop list of static nexthops
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 29 Sep 2020 08:37:56 +0000 (17:37 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 6 Oct 2020 17:39:51 +0000 (02:39 +0900)
[NextHop] sections are managed by both LIST and Hashmap.
Let's drop list, as they store the completely same information.

src/network/networkd-network.c
src/network/networkd-network.h
src/network/networkd-nexthop.c
src/network/networkd-nexthop.h

index c642e86fda3359a35af5e418e65a8ba536ef1d18..d8831561c4f46314c186db9c858ce33bf670c0eb 100644 (file)
@@ -152,7 +152,7 @@ int network_verify(Network *network) {
         RoutePrefix *route_prefix, *route_prefix_next;
         Neighbor *neighbor, *neighbor_next;
         AddressLabel *label, *label_next;
-        NextHop *nexthop, *nextnop_next;
+        NextHop *nexthop;
         Address *address, *address_next;
         Prefix *prefix, *prefix_next;
         Route *route, *route_next;
@@ -298,7 +298,7 @@ int network_verify(Network *network) {
                 if (route_section_verify(route, network) < 0)
                         route_free(route);
 
-        LIST_FOREACH_SAFE(nexthops, nexthop, nextnop_next, network->static_nexthops)
+        HASHMAP_FOREACH(nexthop, network->nexthops_by_section)
                 if (nexthop_section_verify(nexthop) < 0)
                         nexthop_free(nexthop);
 
@@ -649,7 +649,6 @@ static Network *network_free(Network *network) {
         MdbEntry *mdb_entry;
         Neighbor *neighbor;
         Address *address;
-        NextHop *nexthop;
         Prefix *prefix;
         Route *route;
 
@@ -711,9 +710,6 @@ static Network *network_free(Network *network) {
         while ((route = network->static_routes))
                 route_free(route);
 
-        while ((nexthop = network->static_nexthops))
-                nexthop_free(nexthop);
-
         while ((address = network->static_addresses))
                 address_free(address);
 
@@ -740,7 +736,7 @@ static Network *network_free(Network *network) {
 
         hashmap_free(network->addresses_by_section);
         hashmap_free(network->routes_by_section);
-        hashmap_free(network->nexthops_by_section);
+        hashmap_free_with_destructor(network->nexthops_by_section, nexthop_free);
         hashmap_free(network->fdb_entries_by_section);
         hashmap_free(network->mdb_entries_by_section);
         hashmap_free(network->neighbors_by_section);
index bc006944d73dff3b14f527e6e9fab69b662a95f8..c92b7518661d9a17e6c0350f8efd41b8286700a7 100644 (file)
@@ -286,7 +286,6 @@ struct Network {
 
         LIST_HEAD(Address, static_addresses);
         LIST_HEAD(Route, static_routes);
-        LIST_HEAD(NextHop, static_nexthops);
         LIST_HEAD(FdbEntry, static_fdb_entries);
         LIST_HEAD(MdbEntry, static_mdb_entries);
         LIST_HEAD(IPv6ProxyNDPAddress, ipv6_proxy_ndp_addresses);
@@ -297,7 +296,6 @@ struct Network {
 
         unsigned n_static_addresses;
         unsigned n_static_routes;
-        unsigned n_static_nexthops;
         unsigned n_static_fdb_entries;
         unsigned n_static_mdb_entries;
         unsigned n_ipv6_proxy_ndp_addresses;
index 6ff616d906c51ac7a61011245f854f559e94896e..6c9d1c57dd69fac5a5457f5bb1901b0b8eb5d891 100644 (file)
@@ -20,13 +20,8 @@ void nexthop_free(NextHop *nexthop) {
                 return;
 
         if (nexthop->network) {
-                LIST_REMOVE(nexthops, nexthop->network->static_nexthops, nexthop);
-
-                assert(nexthop->network->n_static_nexthops > 0);
-                nexthop->network->n_static_nexthops--;
-
-                if (nexthop->section)
-                        hashmap_remove(nexthop->network->nexthops_by_section, nexthop->section);
+                assert(nexthop->section);
+                hashmap_remove(nexthop->network->nexthops_by_section, nexthop->section);
         }
 
         network_config_section_free(nexthop->section);
@@ -64,19 +59,17 @@ static int nexthop_new_static(Network *network, const char *filename, unsigned s
 
         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);
 
-                nexthop = hashmap_get(network->nexthops_by_section, n);
-                if (nexthop) {
-                        *ret = TAKE_PTR(nexthop);
+        r = network_config_section_new(filename, section_line, &n);
+        if (r < 0)
+                return r;
 
-                        return 0;
-                }
+        nexthop = hashmap_get(network->nexthops_by_section, n);
+        if (nexthop) {
+                *ret = TAKE_PTR(nexthop);
+                return 0;
         }
 
         r = nexthop_new(&nexthop);
@@ -85,23 +78,17 @@ static int nexthop_new_static(Network *network, const char *filename, unsigned s
 
         nexthop->protocol = RTPROT_STATIC;
         nexthop->network = network;
-        LIST_PREPEND(nexthops, network->static_nexthops, nexthop);
-        network->n_static_nexthops++;
+        nexthop->section = TAKE_PTR(n);
 
-        if (filename) {
-                nexthop->section = TAKE_PTR(n);
-
-                r = hashmap_ensure_allocated(&network->nexthops_by_section, &network_config_hash_ops);
-                if (r < 0)
-                        return r;
+        r = hashmap_ensure_allocated(&network->nexthops_by_section, &network_config_hash_ops);
+        if (r < 0)
+                return r;
 
-                r = hashmap_put(network->nexthops_by_section, nexthop->section, nexthop);
-                if (r < 0)
-                        return r;
-        }
+        r = hashmap_put(network->nexthops_by_section, nexthop->section, nexthop);
+        if (r < 0)
+                return r;
 
         *ret = TAKE_PTR(nexthop);
-
         return 0;
 }
 
@@ -343,12 +330,12 @@ int link_set_nexthop(Link *link) {
 
         link->static_nexthops_configured = false;
 
-        LIST_FOREACH(nexthops, nh, link->network->static_nexthops) {
+        HASHMAP_FOREACH(nh, link->network->nexthops_by_section) {
                 r = nexthop_configure(nh, link);
                 if (r < 0)
                         return log_link_warning_errno(link, r, "Could not set nexthop: %m");
-                if (r > 0)
-                        link->nexthop_messages++;
+
+                link->nexthop_messages++;
         }
 
         if (link->nexthop_messages == 0) {
index af6bf7b0c2df189bb92df01aa06efe23d989981c..2ff8b4b81e8c27598e9e9b3de49f0c665a5b2060 100644 (file)
@@ -26,8 +26,6 @@ struct NextHop {
         uint32_t id;
 
         union in_addr_union gw;
-
-        LIST_FIELDS(NextHop, nexthops);
 };
 
 void nexthop_free(NextHop *nexthop);