network: drop redundant list of prefixes and route prefixes

This commit is contained in:
Yu Watanabe 2020-09-30 05:54:19 +09:00
parent 064dfb05f0
commit ecb0e85ea9
5 changed files with 53 additions and 94 deletions

View File

@ -1239,7 +1239,6 @@ static int static_address_configure(Address *address, Link *link, bool update) {
static int link_request_set_addresses(Link *link) {
Address *ad;
Prefix *p;
int r;
assert(link);
@ -1286,8 +1285,10 @@ static int link_request_set_addresses(Link *link) {
return r;
}
if (link->network->router_prefix_delegation & RADV_PREFIX_DELEGATION_STATIC)
LIST_FOREACH(prefixes, p, link->network->static_prefixes) {
if (link->network->router_prefix_delegation & RADV_PREFIX_DELEGATION_STATIC) {
Prefix *p;
HASHMAP_FOREACH(p, link->network->prefixes_by_section) {
_cleanup_(address_freep) Address *address = NULL;
if (!p->assign)
@ -1310,6 +1311,7 @@ static int link_request_set_addresses(Link *link) {
if (r < 0)
return r;
}
}
r = link_set_address_labels(link);
if (r < 0)

View File

@ -154,9 +154,9 @@ static int network_resolve_stacked_netdevs(Network *network) {
}
int network_verify(Network *network) {
RoutePrefix *route_prefix, *route_prefix_next;
RoutePrefix *route_prefix;
Address *address, *address_next;
Prefix *prefix, *prefix_next;
Prefix *prefix;
Route *route, *route_next;
TrafficControl *tc;
SRIOV *sr_iov;
@ -309,11 +309,11 @@ int network_verify(Network *network) {
network_verify_neighbors(network);
network_verify_address_labels(network);
LIST_FOREACH_SAFE(prefixes, prefix, prefix_next, network->static_prefixes)
HASHMAP_FOREACH(prefix, network->prefixes_by_section)
if (section_is_invalid(prefix->section))
prefix_free(prefix);
LIST_FOREACH_SAFE(route_prefixes, route_prefix, route_prefix_next, network->static_route_prefixes)
HASHMAP_FOREACH(route_prefix, network->route_prefixes_by_section)
if (section_is_invalid(route_prefix->section))
route_prefix_free(route_prefix);
@ -633,9 +633,7 @@ failure:
}
static Network *network_free(Network *network) {
RoutePrefix *route_prefix;
Address *address;
Prefix *prefix;
Route *route;
if (!network)
@ -699,12 +697,6 @@ static Network *network_free(Network *network) {
while ((address = network->static_addresses))
address_free(address);
while ((prefix = network->static_prefixes))
prefix_free(prefix);
while ((route_prefix = network->static_route_prefixes))
route_prefix_free(route_prefix);
set_free_free(network->ipv6_proxy_ndp_addresses);
hashmap_free(network->addresses_by_section);
hashmap_free(network->routes_by_section);
@ -713,8 +705,8 @@ static Network *network_free(Network *network) {
hashmap_free_with_destructor(network->mdb_entries_by_section, mdb_entry_free);
hashmap_free_with_destructor(network->neighbors_by_section, neighbor_free);
hashmap_free_with_destructor(network->address_labels_by_section, address_label_free);
hashmap_free(network->prefixes_by_section);
hashmap_free(network->route_prefixes_by_section);
hashmap_free_with_destructor(network->prefixes_by_section, prefix_free);
hashmap_free_with_destructor(network->route_prefixes_by_section, route_prefix_free);
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);
@ -851,7 +843,7 @@ bool network_has_static_ipv6_configurations(Network *network) {
if (!hashmap_isempty(network->address_labels_by_section))
return true;
if (!LIST_IS_EMPTY(network->static_prefixes))
if (!hashmap_isempty(network->prefixes_by_section))
return true;
return false;

View File

@ -281,13 +281,9 @@ struct Network {
LIST_HEAD(Address, static_addresses);
LIST_HEAD(Route, static_routes);
LIST_HEAD(Prefix, static_prefixes);
LIST_HEAD(RoutePrefix, static_route_prefixes);
unsigned n_static_addresses;
unsigned n_static_routes;
unsigned n_static_prefixes;
unsigned n_static_route_prefixes;
Hashmap *addresses_by_section;
Hashmap *routes_by_section;

View File

@ -21,13 +21,8 @@ Prefix *prefix_free(Prefix *prefix) {
return NULL;
if (prefix->network) {
LIST_REMOVE(prefixes, prefix->network->static_prefixes, prefix);
assert(prefix->network->n_static_prefixes > 0);
prefix->network->n_static_prefixes--;
if (prefix->section)
hashmap_remove(prefix->network->prefixes_by_section,
prefix->section);
assert(prefix->section);
hashmap_remove(prefix->network->prefixes_by_section, prefix->section);
}
network_config_section_free(prefix->section);
@ -61,21 +56,17 @@ static int prefix_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;
if (section_line) {
prefix = hashmap_get(network->prefixes_by_section, n);
if (prefix) {
*ret = TAKE_PTR(prefix);
return 0;
}
}
prefix = hashmap_get(network->prefixes_by_section, n);
if (prefix) {
*ret = TAKE_PTR(prefix);
return 0;
}
r = prefix_new(&prefix);
@ -83,20 +74,15 @@ static int prefix_new_static(Network *network, const char *filename,
return r;
prefix->network = network;
LIST_APPEND(prefixes, network->static_prefixes, prefix);
network->n_static_prefixes++;
prefix->section = TAKE_PTR(n);
if (filename) {
prefix->section = TAKE_PTR(n);
r = hashmap_ensure_allocated(&network->prefixes_by_section, &network_config_hash_ops);
if (r < 0)
return r;
r = hashmap_ensure_allocated(&network->prefixes_by_section, &network_config_hash_ops);
if (r < 0)
return r;
r = hashmap_put(network->prefixes_by_section, prefix->section, prefix);
if (r < 0)
return r;
}
r = hashmap_put(network->prefixes_by_section, prefix->section, prefix);
if (r < 0)
return r;
*ret = TAKE_PTR(prefix);
@ -108,13 +94,8 @@ RoutePrefix *route_prefix_free(RoutePrefix *prefix) {
return NULL;
if (prefix->network) {
LIST_REMOVE(route_prefixes, prefix->network->static_route_prefixes, prefix);
assert(prefix->network->n_static_route_prefixes > 0);
prefix->network->n_static_route_prefixes--;
if (prefix->section)
hashmap_remove(prefix->network->route_prefixes_by_section,
prefix->section);
assert(prefix->section);
hashmap_remove(prefix->network->route_prefixes_by_section, prefix->section);
}
network_config_section_free(prefix->section);
@ -148,21 +129,17 @@ static int route_prefix_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;
if (section_line) {
prefix = hashmap_get(network->route_prefixes_by_section, n);
if (prefix) {
*ret = TAKE_PTR(prefix);
return 0;
}
}
prefix = hashmap_get(network->route_prefixes_by_section, n);
if (prefix) {
*ret = TAKE_PTR(prefix);
return 0;
}
r = route_prefix_new(&prefix);
@ -170,20 +147,15 @@ static int route_prefix_new_static(Network *network, const char *filename,
return r;
prefix->network = network;
LIST_APPEND(route_prefixes, network->static_route_prefixes, prefix);
network->n_static_route_prefixes++;
prefix->section = TAKE_PTR(n);
if (filename) {
prefix->section = TAKE_PTR(n);
r = hashmap_ensure_allocated(&network->route_prefixes_by_section, &network_config_hash_ops);
if (r < 0)
return r;
r = hashmap_ensure_allocated(&network->route_prefixes_by_section, &network_config_hash_ops);
if (r < 0)
return r;
r = hashmap_put(network->route_prefixes_by_section, prefix->section, prefix);
if (r < 0)
return r;
}
r = hashmap_put(network->route_prefixes_by_section, prefix->section, prefix);
if (r < 0)
return r;
*ret = TAKE_PTR(prefix);
@ -609,8 +581,6 @@ int radv_emit_dns(Link *link) {
}
int radv_configure(Link *link) {
RoutePrefix *q;
Prefix *p;
int r;
assert(link);
@ -655,7 +625,10 @@ int radv_configure(Link *link) {
}
if (link->network->router_prefix_delegation & RADV_PREFIX_DELEGATION_STATIC) {
LIST_FOREACH(prefixes, p, link->network->static_prefixes) {
RoutePrefix *q;
Prefix *p;
HASHMAP_FOREACH(p, link->network->prefixes_by_section) {
r = sd_radv_add_prefix(link->radv, p->radv_prefix, false);
if (r == -EEXIST)
continue;
@ -667,7 +640,7 @@ int radv_configure(Link *link) {
return r;
}
LIST_FOREACH(route_prefixes, q, link->network->static_route_prefixes) {
HASHMAP_FOREACH(q, link->network->route_prefixes_by_section) {
r = sd_radv_add_route_prefix(link->radv, q->radv_route_prefix, false);
if (r == -EEXIST)
continue;

View File

@ -29,8 +29,6 @@ struct Prefix {
sd_radv_prefix *radv_prefix;
bool assign;
LIST_FIELDS(Prefix, prefixes);
};
struct RoutePrefix {
@ -38,8 +36,6 @@ struct RoutePrefix {
NetworkConfigSection *section;
sd_radv_route_prefix *radv_route_prefix;
LIST_FIELDS(RoutePrefix, route_prefixes);
};
Prefix *prefix_free(Prefix *prefix);