From ca183bf8fd875499c65ef69b4b49e62c381daccc Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 29 Sep 2020 15:29:56 +0900 Subject: [PATCH] network: drop list of static routing policy rules [RoutingPolicyRule] sections are managed by both LIST and Hashmap. Let's drop list. --- src/network/networkd-manager.c | 1 + src/network/networkd-network-gperf.gperf | 1 + src/network/networkd-network.c | 11 ++--- src/network/networkd-network.h | 3 -- src/network/networkd-routing-policy-rule.c | 57 +++++++++------------- src/network/networkd-routing-policy-rule.h | 17 ++----- src/network/test-routing-policy-rule.c | 9 +--- 7 files changed, 36 insertions(+), 63 deletions(-) diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c index dbbc6b64bc..9ded5fae0a 100644 --- a/src/network/networkd-manager.c +++ b/src/network/networkd-manager.c @@ -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" diff --git a/src/network/networkd-network-gperf.gperf b/src/network/networkd-network-gperf.gperf index 1258203adf..b3f3a22d07 100644 --- a/src/network/networkd-network-gperf.gperf +++ b/src/network/networkd-network-gperf.gperf @@ -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" diff --git a/src/network/networkd-network.c b/src/network/networkd-network.c index 49e6d62abb..78ccd9311d 100644 --- a/src/network/networkd-network.c +++ b/src/network/networkd-network.c @@ -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); diff --git a/src/network/networkd-network.h b/src/network/networkd-network.h index 5ba0bc705d..bc006944d7 100644 --- a/src/network/networkd-network.h +++ b/src/network/networkd-network.h @@ -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; diff --git a/src/network/networkd-routing-policy-rule.c b/src/network/networkd-routing-policy-rule.c index 45efe2c77f..79568b7e14 100644 --- a/src/network/networkd-routing-policy-rule.c +++ b/src/network/networkd-routing-policy-rule.c @@ -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)); + assert(filename); + assert(section_line > 0); - if (filename) { - r = network_config_section_new(filename, section_line, &n); - if (r < 0) - return r; + r = network_config_section_new(filename, section_line, &n); + if (r < 0) + return r; - rule = hashmap_get(network->rules_by_section, n); - if (rule) { - *ret = TAKE_PTR(rule); - - 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++; + rule->section = TAKE_PTR(n); - if (filename) { - 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); diff --git a/src/network/networkd-routing-policy-rule.h b/src/network/networkd-routing-policy-rule.h index 143711623f..431a3b6724 100644 --- a/src/network/networkd-routing-policy-rule.h +++ b/src/network/networkd-routing-policy-rule.h @@ -2,25 +2,20 @@ #pragma once #include -#include #include #include +#include -#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); diff --git a/src/network/test-routing-policy-rule.c b/src/network/test-routing-policy-rule.c index 78755927c7..40341d6073 100644 --- a/src/network/test-routing-policy-rule.c +++ b/src/network/test-routing-policy-rule.c @@ -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"