network: allocate hashmap objects when they are required

This commit is contained in:
Yu Watanabe 2018-11-12 15:24:11 +09:00
parent a0e8e4cf3f
commit 3e5700428e
11 changed files with 66 additions and 67 deletions

View File

@ -729,6 +729,10 @@ int netdev_load_one(Manager *manager, const char *filename) {
return log_error_errno(r, "Failed to generate predictable MAC address for %s: %m", netdev->ifname);
}
r = hashmap_ensure_allocated(&netdev->manager->netdevs, &string_hash_ops);
if (r < 0)
return r;
r = hashmap_put(netdev->manager->netdevs, netdev->ifname, netdev);
if (r < 0)
return r;

View File

@ -65,6 +65,10 @@ static int address_label_new_static(Network *network, const char *filename, unsi
if (filename) {
label->section = TAKE_PTR(n);
r = hashmap_ensure_allocated(&network->address_labels_by_section, &network_config_hash_ops);
if (r < 0)
return r;
r = hashmap_put(network->address_labels_by_section, label->section, label);
if (r < 0)
return r;

View File

@ -74,6 +74,10 @@ int address_new_static(Network *network, const char *filename, unsigned section_
if (filename) {
address->section = TAKE_PTR(n);
r = hashmap_ensure_allocated(&network->addresses_by_section, &network_config_hash_ops);
if (r < 0)
return r;
r = hashmap_put(network->addresses_by_section, address->section, address);
if (r < 0)
return r;

View File

@ -72,6 +72,10 @@ int fdb_entry_new_static(
if (filename) {
fdb_entry->section = TAKE_PTR(n);
r = hashmap_ensure_allocated(&network->fdb_entries_by_section, &network_config_hash_ops);
if (r < 0)
return r;
r = hashmap_put(network->fdb_entries_by_section, fdb_entry->section, fdb_entry);
if (r < 0)
return r;

View File

@ -1213,7 +1213,6 @@ static int manager_dirty_handler(sd_event_source *s, void *userdata) {
Link *manager_dhcp6_prefix_get(Manager *m, struct in6_addr *addr) {
assert_return(m, NULL);
assert_return(m->dhcp6_prefixes, NULL);
assert_return(addr, NULL);
return hashmap_get(m->dhcp6_prefixes, addr);
@ -1232,13 +1231,31 @@ static int dhcp6_route_add_handler(sd_netlink *nl, sd_netlink_message *m, void *
return 0;
}
static void dhcp6_prefixes_hash_func(const void *p, struct siphash *state) {
const struct in6_addr *addr = p;
assert(p);
siphash24_compress(addr, sizeof(*addr), state);
}
static int dhcp6_prefixes_compare_func(const void *_a, const void *_b) {
const struct in6_addr *a = _a, *b = _b;
return memcmp(a, b, sizeof(*a));
}
static const struct hash_ops dhcp6_prefixes_hash_ops = {
.hash = dhcp6_prefixes_hash_func,
.compare = dhcp6_prefixes_compare_func,
};
int manager_dhcp6_prefix_add(Manager *m, struct in6_addr *addr, Link *link) {
int r;
Route *route;
_cleanup_free_ char *buf = NULL;
Route *route;
int r;
assert_return(m, -EINVAL);
assert_return(m->dhcp6_prefixes, -ENODATA);
assert_return(addr, -EINVAL);
r = route_add(link, AF_INET6, (union in_addr_union *) addr, 64,
@ -1253,6 +1270,10 @@ int manager_dhcp6_prefix_add(Manager *m, struct in6_addr *addr, Link *link) {
(void) in_addr_to_string(AF_INET6, (union in_addr_union *) addr, &buf);
log_link_debug(link, "Adding prefix route %s/64", strnull(buf));
r = hashmap_ensure_allocated(&m->dhcp6_prefixes, &dhcp6_prefixes_hash_ops);
if (r < 0)
return r;
return hashmap_put(m->dhcp6_prefixes, addr, link);
}
@ -1270,13 +1291,12 @@ static int dhcp6_route_remove_handler(sd_netlink *nl, sd_netlink_message *m, voi
}
static int manager_dhcp6_prefix_remove(Manager *m, struct in6_addr *addr) {
_cleanup_free_ char *buf = NULL;
Route *route;
Link *l;
int r;
Route *route;
_cleanup_free_ char *buf = NULL;
assert_return(m, -EINVAL);
assert_return(m->dhcp6_prefixes, -ENODATA);
assert_return(addr, -EINVAL);
l = hashmap_remove(m->dhcp6_prefixes, addr);
@ -1300,9 +1320,9 @@ static int manager_dhcp6_prefix_remove(Manager *m, struct in6_addr *addr) {
}
int manager_dhcp6_prefix_remove_all(Manager *m, Link *link) {
struct in6_addr *addr;
Iterator i;
Link *l;
struct in6_addr *addr;
assert_return(m, -EINVAL);
assert_return(link, -EINVAL);
@ -1317,25 +1337,6 @@ int manager_dhcp6_prefix_remove_all(Manager *m, Link *link) {
return 0;
}
static void dhcp6_prefixes_hash_func(const void *p, struct siphash *state) {
const struct in6_addr *addr = p;
assert(p);
siphash24_compress(addr, sizeof(*addr), state);
}
static int dhcp6_prefixes_compare_func(const void *_a, const void *_b) {
const struct in6_addr *a = _a, *b = _b;
return memcmp(a, b, sizeof(*a));
}
static const struct hash_ops dhcp6_prefixes_hash_ops = {
.hash = dhcp6_prefixes_hash_func,
.compare = dhcp6_prefixes_compare_func,
};
int manager_new(Manager **ret) {
_cleanup_(manager_freep) Manager *m = NULL;
int r;
@ -1372,10 +1373,6 @@ int manager_new(Manager **ret) {
if (r < 0)
return r;
m->netdevs = hashmap_new(&string_hash_ops);
if (!m->netdevs)
return -ENOMEM;
LIST_HEAD_INIT(m->networks);
r = sd_resolve_default(&m->resolve);
@ -1390,10 +1387,6 @@ int manager_new(Manager **ret) {
if (r < 0)
return r;
m->dhcp6_prefixes = hashmap_new(&dhcp6_prefixes_hash_ops);
if (!m->dhcp6_prefixes)
return -ENOMEM;
m->duid.type = DUID_TYPE_EN;
(void) routing_policy_load_rules(m->state_file, &m->rules_saved);

View File

@ -193,34 +193,6 @@ int network_load_one(Manager *manager, const char *filename) {
.ipv6_accept_ra_route_table = RT_TABLE_MAIN,
};
network->stacked_netdevs = hashmap_new(&string_hash_ops);
if (!network->stacked_netdevs)
return log_oom();
network->addresses_by_section = hashmap_new(&network_config_hash_ops);
if (!network->addresses_by_section)
return log_oom();
network->routes_by_section = hashmap_new(&network_config_hash_ops);
if (!network->routes_by_section)
return log_oom();
network->fdb_entries_by_section = hashmap_new(&network_config_hash_ops);
if (!network->fdb_entries_by_section)
return log_oom();
network->address_labels_by_section = hashmap_new(&network_config_hash_ops);
if (!network->address_labels_by_section)
log_oom();
network->prefixes_by_section = hashmap_new(&network_config_hash_ops);
if (!network->prefixes_by_section)
return log_oom();
network->rules_by_section = hashmap_new(&network_config_hash_ops);
if (!network->rules_by_section)
return log_oom();
network->filename = strdup(filename);
if (!network->filename)
return log_oom();
@ -615,6 +587,10 @@ int config_parse_netdev(const char *unit,
case NETDEV_KIND_IPVLAN:
case NETDEV_KIND_VXLAN:
case NETDEV_KIND_VCAN:
r = hashmap_ensure_allocated(&network->stacked_netdevs, &string_hash_ops);
if (r < 0)
return log_oom();
r = hashmap_put(network->stacked_netdevs, netdev->ifname, netdev);
if (r < 0) {
log_syntax(unit, LOG_ERR, filename, line, r, "Cannot add NetDev '%s' to network: %m", rvalue);
@ -753,6 +729,10 @@ int config_parse_tunnel(const char *unit,
return 0;
}
r = hashmap_ensure_allocated(&network->stacked_netdevs, &string_hash_ops);
if (r < 0)
return log_oom();
r = hashmap_put(network->stacked_netdevs, netdev->ifname, netdev);
if (r < 0) {
log_syntax(unit, LOG_ERR, filename, line, r, "Cannot add VLAN '%s' to network, ignoring: %m", rvalue);

View File

@ -89,8 +89,8 @@ typedef struct NetworkConfigSection {
int network_config_section_new(const char *filename, unsigned line, NetworkConfigSection **s);
void network_config_section_free(NetworkConfigSection *network);
DEFINE_TRIVIAL_CLEANUP_FUNC(NetworkConfigSection*, network_config_section_free);
extern const struct hash_ops network_config_hash_ops;
typedef struct Manager Manager;

View File

@ -160,8 +160,11 @@ int prefix_new_static(Network *network, const char *filename,
if (filename) {
prefix->section = TAKE_PTR(n);
r = hashmap_put(network->prefixes_by_section, prefix->section,
prefix);
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;
}

View File

@ -102,6 +102,10 @@ int route_new_static(Network *network, const char *filename, unsigned section_li
if (filename) {
route->section = TAKE_PTR(n);
r = hashmap_ensure_allocated(&network->routes_by_section, &network_config_hash_ops);
if (r < 0)
return r;
r = hashmap_put(network->routes_by_section, route->section, route);
if (r < 0)
return r;

View File

@ -412,6 +412,10 @@ static int routing_policy_rule_new_static(Network *network, const char *filename
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_put(network->rules_by_section, rule->section, rule);
if (r < 0)
return r;

View File

@ -172,7 +172,6 @@ static void test_config_parse_address_one(const char *rvalue, int family, unsign
_cleanup_(network_freep) Network *network = NULL;
assert_se(network = new0(Network, 1));
assert_se(network->addresses_by_section = hashmap_new(NULL));
assert_se(config_parse_address("network", "filename", 1, "section", 1, "Address", 0, rvalue, network, network) == 0);
assert_se(network->n_static_addresses == n_addresses);
if (n_addresses > 0) {