network: drop list of static routing policy rules

[RoutingPolicyRule] sections are managed by both LIST and Hashmap.
Let's drop list.
This commit is contained in:
Yu Watanabe 2020-09-29 15:29:56 +09:00
parent 02e9f4e536
commit ca183bf8fd
7 changed files with 36 additions and 63 deletions

View 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"

View 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"

View 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);

View 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;

View 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));
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);

View 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);

View 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"