]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network: drop list of static routing policy rules
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 29 Sep 2020 06:29:56 +0000 (15:29 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 6 Oct 2020 17:36:29 +0000 (02:36 +0900)
[RoutingPolicyRule] sections are managed by both LIST and Hashmap.
Let's drop list.

src/network/networkd-manager.c
src/network/networkd-network-gperf.gperf
src/network/networkd-network.c
src/network/networkd-network.h
src/network/networkd-routing-policy-rule.c
src/network/networkd-routing-policy-rule.h
src/network/test-routing-policy-rule.c

index dbbc6b64bcc753d2f14dc81270e82b9b99d4c960..9ded5fae0a069839ea608013e842134f984e7c41 100644 (file)
@@ -30,6 +30,7 @@
 #include "networkd-manager-bus.h"
 #include "networkd-manager.h"
 #include "networkd-network-bus.h"
+#include "networkd-routing-policy-rule.h"
 #include "networkd-speed-meter.h"
 #include "ordered-set.h"
 #include "path-lookup.h"
index 1258203adf6889c815aefb531350ee34e5d55d9c..b3f3a22d0724169ce3428706bb68cd4ab7232b61 100644 (file)
@@ -15,6 +15,7 @@ _Pragma("GCC diagnostic ignored \"-Wimplicit-fallthrough\"")
 #include "networkd-ipv4ll.h"
 #include "networkd-ndisc.h"
 #include "networkd-network.h"
+#include "networkd-routing-policy-rule.h"
 #include "networkd-sriov.h"
 #include "qdisc.h"
 #include "tclass.h"
index 49e6d62abb53688071a09050dba5f97d4067cdcb..78ccd9311d90694514f8783c01cd89931afc9f23 100644 (file)
@@ -16,6 +16,7 @@
 #include "network-internal.h"
 #include "networkd-manager.h"
 #include "networkd-network.h"
+#include "networkd-routing-policy-rule.h"
 #include "networkd-sriov.h"
 #include "parse-util.h"
 #include "path-lookup.h"
@@ -149,7 +150,7 @@ static int network_resolve_stacked_netdevs(Network *network) {
 
 int network_verify(Network *network) {
         RoutePrefix *route_prefix, *route_prefix_next;
-        RoutingPolicyRule *rule, *rule_next;
+        RoutingPolicyRule *rule;
         Neighbor *neighbor, *neighbor_next;
         AddressLabel *label, *label_next;
         NextHop *nexthop, *nextnop_next;
@@ -326,7 +327,7 @@ int network_verify(Network *network) {
                 if (section_is_invalid(route_prefix->section))
                         route_prefix_free(route_prefix);
 
-        LIST_FOREACH_SAFE(rules, rule, rule_next, network->rules)
+        HASHMAP_FOREACH(rule, network->rules_by_section)
                 if (routing_policy_rule_section_verify(rule) < 0)
                         routing_policy_rule_free(rule);
 
@@ -646,7 +647,6 @@ failure:
 static Network *network_free(Network *network) {
         IPv6ProxyNDPAddress *ipv6_proxy_ndp_address;
         RoutePrefix *route_prefix;
-        RoutingPolicyRule *rule;
         AddressLabel *label;
         FdbEntry *fdb_entry;
         MdbEntry *mdb_entry;
@@ -741,9 +741,6 @@ static Network *network_free(Network *network) {
         while ((route_prefix = network->static_route_prefixes))
                 route_prefix_free(route_prefix);
 
-        while ((rule = network->rules))
-                routing_policy_rule_free(rule);
-
         hashmap_free(network->addresses_by_section);
         hashmap_free(network->routes_by_section);
         hashmap_free(network->nexthops_by_section);
@@ -753,7 +750,7 @@ static Network *network_free(Network *network) {
         hashmap_free(network->address_labels_by_section);
         hashmap_free(network->prefixes_by_section);
         hashmap_free(network->route_prefixes_by_section);
-        hashmap_free(network->rules_by_section);
+        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);
 
index 5ba0bc705d2f326437ddb1294ad97929846cd16f..bc006944d73dff3b14f527e6e9fab69b662a95f8 100644 (file)
@@ -29,7 +29,6 @@
 #include "networkd-nexthop.h"
 #include "networkd-radv.h"
 #include "networkd-route.h"
-#include "networkd-routing-policy-rule.h"
 #include "networkd-util.h"
 #include "ordered-set.h"
 #include "resolve-util.h"
@@ -295,7 +294,6 @@ struct Network {
         LIST_HEAD(AddressLabel, address_labels);
         LIST_HEAD(Prefix, static_prefixes);
         LIST_HEAD(RoutePrefix, static_route_prefixes);
-        LIST_HEAD(RoutingPolicyRule, rules);
 
         unsigned n_static_addresses;
         unsigned n_static_routes;
@@ -307,7 +305,6 @@ struct Network {
         unsigned n_address_labels;
         unsigned n_static_prefixes;
         unsigned n_static_route_prefixes;
-        unsigned n_rules;
 
         Hashmap *addresses_by_section;
         Hashmap *routes_by_section;
index 45efe2c77f8070fe12b91b47ef06486e4b35d85c..79568b7e148a909b2e2d32f3884e57cfee1ef59e 100644 (file)
@@ -9,9 +9,9 @@
 #include "fileio.h"
 #include "format-util.h"
 #include "ip-protocol-list.h"
-#include "networkd-routing-policy-rule.h"
 #include "netlink-util.h"
 #include "networkd-manager.h"
+#include "networkd-routing-policy-rule.h"
 #include "networkd-util.h"
 #include "parse-util.h"
 #include "socket-util.h"
@@ -44,19 +44,17 @@ static int routing_policy_rule_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);
 
-                rule = hashmap_get(network->rules_by_section, n);
-                if (rule) {
-                        *ret = TAKE_PTR(rule);
+        r = network_config_section_new(filename, section_line, &n);
+        if (r < 0)
+                return r;
 
-                        return 0;
-                }
+        rule = hashmap_get(network->rules_by_section, n);
+        if (rule) {
+                *ret = TAKE_PTR(rule);
+                return 0;
         }
 
         r = routing_policy_rule_new(&rule);
@@ -64,23 +62,17 @@ static int routing_policy_rule_new_static(Network *network, const char *filename
                 return r;
 
         rule->network = network;
-        LIST_APPEND(rules, network->rules, rule);
-        network->n_rules++;
-
-        if (filename) {
-                rule->section = TAKE_PTR(n);
+        rule->section = TAKE_PTR(n);
 
-                r = hashmap_ensure_allocated(&network->rules_by_section, &network_config_hash_ops);
-                if (r < 0)
-                        return r;
+        r = hashmap_ensure_allocated(&network->rules_by_section, &network_config_hash_ops);
+        if (r < 0)
+                return r;
 
-                r = hashmap_put(network->rules_by_section, rule->section, rule);
-                if (r < 0)
-                        return r;
-        }
+        r = hashmap_put(network->rules_by_section, rule->section, rule);
+        if (r < 0)
+                return r;
 
         *ret = TAKE_PTR(rule);
-
         return 0;
 }
 
@@ -89,12 +81,8 @@ RoutingPolicyRule *routing_policy_rule_free(RoutingPolicyRule *rule) {
                 return NULL;
 
         if (rule->network) {
-                LIST_REMOVE(rules, rule->network->rules, rule);
-                assert(rule->network->n_rules > 0);
-                rule->network->n_rules--;
-
-                if (rule->section)
-                        hashmap_remove(rule->network->rules_by_section, rule->section);
+                assert(rule->section);
+                hashmap_remove(rule->network->rules_by_section, rule->section);
         }
 
         if (rule->manager) {
@@ -618,17 +606,18 @@ static int routing_policy_rule_configure(RoutingPolicyRule *rule, Link *link) {
 }
 
 static bool manager_links_have_routing_policy_rule(Manager *m, RoutingPolicyRule *rule) {
-        RoutingPolicyRule *link_rule;
         Link *link;
 
         assert(m);
         assert(rule);
 
         HASHMAP_FOREACH(link, m->links) {
+                RoutingPolicyRule *link_rule;
+
                 if (!link->network)
                         continue;
 
-                LIST_FOREACH(rules, link_rule, link->network->rules)
+                HASHMAP_FOREACH(link_rule, link->network->rules_by_section)
                         if (routing_policy_rule_compare_func(link_rule, rule) == 0)
                                 return true;
         }
@@ -678,7 +667,7 @@ int link_set_routing_policy_rules(Link *link) {
 
         link->routing_policy_rules_configured = false;
 
-        LIST_FOREACH(rules, rule, link->network->rules) {
+        HASHMAP_FOREACH(rule, link->network->rules_by_section) {
                 RoutingPolicyRule *existing;
 
                 r = routing_policy_rule_get(link->manager, rule, &existing);
index 143711623f98f63cf7795f3fac524715ceed1c92..431a3b6724956ceaefd21c8a18d3d575d4f69254 100644 (file)
@@ -2,25 +2,20 @@
 #pragma once
 
 #include <inttypes.h>
-#include <netinet/in.h>
 #include <linux/fib_rules.h>
 #include <stdbool.h>
+#include <stdio.h>
 
-#include "in-addr-util.h"
 #include "conf-parser.h"
-
-typedef struct RoutingPolicyRule RoutingPolicyRule;
-
-#include "networkd-link.h"
-#include "networkd-network.h"
+#include "in-addr-util.h"
 #include "networkd-util.h"
+#include "set.h"
 
 typedef struct Network Network;
 typedef struct Link Link;
-typedef struct NetworkConfigSection NetworkConfigSection;
 typedef struct Manager Manager;
 
-struct RoutingPolicyRule {
+typedef struct RoutingPolicyRule {
         Manager *manager;
         Network *network;
         Link *link;
@@ -52,9 +47,7 @@ struct RoutingPolicyRule {
         struct fib_rule_uid_range uid_range;
 
         int suppress_prefixlen;
-
-        LIST_FIELDS(RoutingPolicyRule, rules);
-};
+} RoutingPolicyRule;
 
 int routing_policy_rule_new(RoutingPolicyRule **ret);
 RoutingPolicyRule *routing_policy_rule_free(RoutingPolicyRule *rule);
index 78755927c77f3667b305b4ea0052e52a3a9d78b5..40341d607380a43554ed69433f85c4cd9a7b66fd 100644 (file)
@@ -1,13 +1,8 @@
-/***
-  SPDX-License-Identifier: LGPL-2.1+
-***/
+/* SPDX-License-Identifier: LGPL-2.1+ */
 
 #include "fd-util.h"
 #include "fileio.h"
-#include "log.h"
-#include "macro.h"
-#include "network-internal.h"
-#include "networkd-manager.h"
+#include "networkd-routing-policy-rule.h"
 #include "string-util.h"
 #include "tests.h"
 #include "tmpfile-util.h"