network: drop list of static neighbors
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 29 Sep 2020 14:31:38 +0000 (23:31 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 6 Oct 2020 17:44:42 +0000 (02:44 +0900)
[Neighbor] sections are managed by both LIST and Hashmap.
Let's drop list, as they store the completely same information.

src/network/networkd-link.c
src/network/networkd-neighbor.c
src/network/networkd-neighbor.h
src/network/networkd-network.c
src/network/networkd-network.h

index 6df7ce74a0122c255e047ac02f29982d53616d8c..48cf83a75323b96d56e9abf94ceacda8f33a5d9d 100644 (file)
@@ -2602,7 +2602,7 @@ static bool link_is_neighbor_configured(Link *link, Neighbor *neighbor) {
         if (!link->network)
                 return false;
 
-        LIST_FOREACH(neighbors, net_neighbor, link->network->neighbors)
+        HASHMAP_FOREACH(net_neighbor, link->network->neighbors_by_section)
                 if (neighbor_equal(net_neighbor, neighbor))
                         return true;
 
index 3eec9655333a74b1c4ec239a5b4a51e53a355115..0b4ceafb31ff6aa21ecde280ca4b3b6fa62b460e 100644 (file)
@@ -18,12 +18,8 @@ void neighbor_free(Neighbor *neighbor) {
                 return;
 
         if (neighbor->network) {
-                LIST_REMOVE(neighbors, neighbor->network->neighbors, neighbor);
-                assert(neighbor->network->n_neighbors > 0);
-                neighbor->network->n_neighbors--;
-
-                if (neighbor->section)
-                        hashmap_remove(neighbor->network->neighbors_by_section, neighbor->section);
+                assert(neighbor->section);
+                hashmap_remove(neighbor->network->neighbors_by_section, neighbor->section);
         }
 
         network_config_section_free(neighbor->section);
@@ -43,19 +39,17 @@ static int neighbor_new_static(Network *network, const char *filename, unsigned
 
         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);
 
-                neighbor = hashmap_get(network->neighbors_by_section, n);
-                if (neighbor) {
-                        *ret = TAKE_PTR(neighbor);
+        r = network_config_section_new(filename, section_line, &n);
+        if (r < 0)
+                return r;
 
-                        return 0;
-                }
+        neighbor = hashmap_get(network->neighbors_by_section, n);
+        if (neighbor) {
+                *ret = TAKE_PTR(neighbor);
+                return 0;
         }
 
         neighbor = new(Neighbor, 1);
@@ -65,25 +59,18 @@ static int neighbor_new_static(Network *network, const char *filename, unsigned
         *neighbor = (Neighbor) {
                 .network = network,
                 .family = AF_UNSPEC,
+                .section = TAKE_PTR(n),
         };
 
-        LIST_APPEND(neighbors, network->neighbors, neighbor);
-        network->n_neighbors++;
-
-        if (filename) {
-                neighbor->section = TAKE_PTR(n);
-
-                r = hashmap_ensure_allocated(&network->neighbors_by_section, &network_config_hash_ops);
-                if (r < 0)
-                        return r;
+        r = hashmap_ensure_allocated(&network->neighbors_by_section, &network_config_hash_ops);
+        if (r < 0)
+                return r;
 
-                r = hashmap_put(network->neighbors_by_section, neighbor->section, neighbor);
-                if (r < 0)
-                        return r;
-        }
+        r = hashmap_put(network->neighbors_by_section, neighbor->section, neighbor);
+        if (r < 0)
+                return r;
 
         *ret = TAKE_PTR(neighbor);
-
         return 0;
 }
 
@@ -320,7 +307,7 @@ int link_set_neighbors(Link *link) {
 
         link->neighbors_configured = false;
 
-        LIST_FOREACH(neighbors, neighbor, link->network->neighbors) {
+        HASHMAP_FOREACH(neighbor, link->network->neighbors_by_section) {
                 r = neighbor_configure(neighbor, link, NULL);
                 if (r < 0)
                         return log_link_warning_errno(link, r, "Could not set neighbor: %m");
index 3f21a25c2f77ec6f6caf7ed96d75d386884be520..34083cf5267039bbb8b66980fab44abae515336b 100644 (file)
@@ -29,8 +29,6 @@ struct Neighbor {
         union in_addr_union in_addr;
         union lladdr_union lladdr;
         size_t lladdr_size;
-
-        LIST_FIELDS(Neighbor, neighbors);
 };
 
 void neighbor_free(Neighbor *neighbor);
index de150745506c6f971685bec858b933f8e2bb22ad..f4b03beeabe3d5b63127cbb7213c2b7e9419d435 100644 (file)
@@ -151,7 +151,7 @@ static int network_resolve_stacked_netdevs(Network *network) {
 
 int network_verify(Network *network) {
         RoutePrefix *route_prefix, *route_prefix_next;
-        Neighbor *neighbor, *neighbor_next;
+        Neighbor *neighbor;
         AddressLabel *label, *label_next;
         Address *address, *address_next;
         Prefix *prefix, *prefix_next;
@@ -308,7 +308,7 @@ int network_verify(Network *network) {
                 if (mdb_entry_verify(mdb) < 0)
                         mdb_entry_free(mdb);
 
-        LIST_FOREACH_SAFE(neighbors, neighbor, neighbor_next, network->neighbors)
+        HASHMAP_FOREACH(neighbor, network->neighbors_by_section)
                 if (neighbor_section_verify(neighbor) < 0)
                         neighbor_free(neighbor);
 
@@ -645,7 +645,6 @@ static Network *network_free(Network *network) {
         AddressLabel *label;
         FdbEntry *fdb_entry;
         MdbEntry *mdb_entry;
-        Neighbor *neighbor;
         Address *address;
         Prefix *prefix;
         Route *route;
@@ -720,9 +719,6 @@ static Network *network_free(Network *network) {
         while ((ipv6_proxy_ndp_address = network->ipv6_proxy_ndp_addresses))
                 ipv6_proxy_ndp_address_free(ipv6_proxy_ndp_address);
 
-        while ((neighbor = network->neighbors))
-                neighbor_free(neighbor);
-
         while ((label = network->address_labels))
                 address_label_free(label);
 
@@ -737,7 +733,7 @@ static Network *network_free(Network *network) {
         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);
+        hashmap_free_with_destructor(network->neighbors_by_section, neighbor_free);
         hashmap_free(network->address_labels_by_section);
         hashmap_free(network->prefixes_by_section);
         hashmap_free(network->route_prefixes_by_section);
@@ -870,7 +866,7 @@ bool network_has_static_ipv6_configurations(Network *network) {
                 if (mdb->family == AF_INET6)
                         return true;
 
-        LIST_FOREACH(neighbors, neighbor, network->neighbors)
+        HASHMAP_FOREACH(neighbor, network->neighbors_by_section)
                 if (neighbor->family == AF_INET6)
                         return true;
 
index 12ec6ef1b2ba56637805f20b175a363643d49b7d..5ab609bcc0cf4054c987af94394c439ce460bd5d 100644 (file)
@@ -288,7 +288,6 @@ struct Network {
         LIST_HEAD(FdbEntry, static_fdb_entries);
         LIST_HEAD(MdbEntry, static_mdb_entries);
         LIST_HEAD(IPv6ProxyNDPAddress, ipv6_proxy_ndp_addresses);
-        LIST_HEAD(Neighbor, neighbors);
         LIST_HEAD(AddressLabel, address_labels);
         LIST_HEAD(Prefix, static_prefixes);
         LIST_HEAD(RoutePrefix, static_route_prefixes);
@@ -298,7 +297,6 @@ struct Network {
         unsigned n_static_fdb_entries;
         unsigned n_static_mdb_entries;
         unsigned n_ipv6_proxy_ndp_addresses;
-        unsigned n_neighbors;
         unsigned n_address_labels;
         unsigned n_static_prefixes;
         unsigned n_static_route_prefixes;