1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-22 17:35:35 +03:00

Merge pull request #30717 from yuwata/network-ref-unref

network: introduce address_ref() and friends
This commit is contained in:
Luca Boccassi 2024-01-10 19:34:48 +00:00 committed by GitHub
commit d6b39152a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 274 additions and 147 deletions

View File

@ -133,14 +133,40 @@ void link_get_address_states(
*ret_all = address_state_from_scope(MIN(ipv4_scope, ipv6_scope));
}
static void address_hash_func(const Address *a, struct siphash *state);
static int address_compare_func(const Address *a1, const Address *a2);
static void address_detach(Address *address);
DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(
address_hash_ops_detach,
Address,
address_hash_func,
address_compare_func,
address_detach);
DEFINE_HASH_OPS(
address_hash_ops,
Address,
address_hash_func,
address_compare_func);
DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
address_section_hash_ops,
ConfigSection,
config_section_hash_func,
config_section_compare_func,
Address,
address_detach);
int address_new(Address **ret) {
_cleanup_(address_freep) Address *address = NULL;
_cleanup_(address_unrefp) Address *address = NULL;
address = new(Address, 1);
if (!address)
return -ENOMEM;
*address = (Address) {
.n_ref = 1,
.family = AF_UNSPEC,
.scope = RT_SCOPE_UNIVERSE,
.lifetime_valid_usec = USEC_INFINITY,
@ -155,7 +181,7 @@ int address_new(Address **ret) {
int address_new_static(Network *network, const char *filename, unsigned section_line, Address **ret) {
_cleanup_(config_section_freep) ConfigSection *n = NULL;
_cleanup_(address_freep) Address *address = NULL;
_cleanup_(address_unrefp) Address *address = NULL;
int r;
assert(network);
@ -186,7 +212,7 @@ int address_new_static(Network *network, const char *filename, unsigned section_
/* This will be adjusted in address_section_verify(). */
address->duplicate_address_detection = _ADDRESS_FAMILY_INVALID;
r = ordered_hashmap_ensure_put(&network->addresses_by_section, &config_section_hash_ops, address->section, address);
r = ordered_hashmap_ensure_put(&network->addresses_by_section, &address_section_hash_ops, address->section, address);
if (r < 0)
return r;
@ -194,9 +220,9 @@ int address_new_static(Network *network, const char *filename, unsigned section_
return 0;
}
Address *address_free(Address *address) {
if (!address)
return NULL;
static Address* address_detach_impl(Address *address) {
assert(address);
assert(!address->link || !address->network);
if (address->network) {
assert(address->section);
@ -204,11 +230,33 @@ Address *address_free(Address *address) {
if (address->network->dhcp_server_address == address)
address->network->dhcp_server_address = NULL;
address->network = NULL;
return address;
}
if (address->link)
if (address->link) {
set_remove(address->link->addresses, address);
address->link = NULL;
return address;
}
return NULL;
}
static void address_detach(Address *address) {
assert(address);
address_unref(address_detach_impl(address));
}
static Address* address_free(Address *address) {
if (!address)
return NULL;
address_detach_impl(address);
config_section_free(address->section);
free(address->label);
free(address->netlabel);
@ -216,6 +264,8 @@ Address *address_free(Address *address) {
return mfree(address);
}
DEFINE_TRIVIAL_REF_UNREF_FUNC(Address, address, address_free);
static bool address_lifetime_is_valid(const Address *a) {
assert(a);
@ -466,19 +516,6 @@ static int address_compare_func(const Address *a1, const Address *a2) {
}
}
DEFINE_HASH_OPS(
address_hash_ops,
Address,
address_hash_func,
address_compare_func);
DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(
address_hash_ops_free,
Address,
address_hash_func,
address_compare_func,
address_free);
static bool address_can_update(const Address *la, const Address *na) {
assert(la);
assert(la->link);
@ -557,7 +594,7 @@ static bool address_can_update(const Address *la, const Address *na) {
}
int address_dup(const Address *src, Address **ret) {
_cleanup_(address_freep) Address *dest = NULL;
_cleanup_(address_unrefp) Address *dest = NULL;
int r;
assert(src);
@ -567,7 +604,8 @@ int address_dup(const Address *src, Address **ret) {
if (!dest)
return -ENOMEM;
/* clear all pointers */
/* clear the reference counter and all pointers */
dest->n_ref = 1;
dest->network = NULL;
dest->section = NULL;
dest->link = NULL;
@ -708,19 +746,21 @@ static void address_modify_nft_set(Address *address, bool add) {
}
}
static int address_add(Link *link, Address *address) {
static int address_attach(Link *link, Address *address) {
int r;
assert(link);
assert(address);
assert(!address->link);
r = set_ensure_put(&link->addresses, &address_hash_ops_free, address);
r = set_ensure_put(&link->addresses, &address_hash_ops_detach, address);
if (r < 0)
return r;
if (r == 0)
return -EEXIST;
address->link = link;
address_ref(address);
return 0;
}
@ -781,7 +821,7 @@ static int address_drop(Address *address) {
ipv4acd_detach(link, address);
address_free(address);
address_detach(address);
link_update_operstate(link, /* also_update_master = */ true);
link_check_ready(link);
@ -910,7 +950,7 @@ int link_get_address(Link *link, int family, const union in_addr_union *address,
* arbitrary prefixlen will be returned. */
if (family == AF_INET6 || prefixlen != 0) {
_cleanup_(address_freep) Address *tmp = NULL;
_cleanup_(address_unrefp) Address *tmp = NULL;
/* In this case, we can use address_get(). */
@ -1211,7 +1251,7 @@ int link_drop_ipv6ll_addresses(Link *link) {
return r;
for (sd_netlink_message *addr = reply; addr; addr = sd_netlink_message_next(addr)) {
_cleanup_(address_freep) Address *a = NULL;
_cleanup_(address_unrefp) Address *a = NULL;
unsigned char flags, prefixlen;
struct in6_addr address;
int ifindex;
@ -1356,7 +1396,7 @@ void link_foreignize_addresses(Link *link) {
}
static int address_acquire(Link *link, const Address *original, Address **ret) {
_cleanup_(address_freep) Address *na = NULL;
_cleanup_(address_unrefp) Address *na = NULL;
union in_addr_union in_addr;
int r;
@ -1523,7 +1563,7 @@ int link_request_address(
address_netlink_handler_t netlink_handler,
Request **ret) {
_cleanup_(address_freep) Address *tmp = NULL;
_cleanup_(address_unrefp) Address *tmp = NULL;
Address *existing = NULL;
int r;
@ -1568,7 +1608,7 @@ int link_request_address(
log_address_debug(tmp, "Requesting", link);
r = link_queue_request_safe(link, REQUEST_TYPE_ADDRESS,
tmp,
address_free,
address_unref,
address_hash_func,
address_compare_func,
address_process_request,
@ -1644,7 +1684,7 @@ int link_request_static_addresses(Link *link) {
}
int manager_rtnl_process_address(sd_netlink *rtnl, sd_netlink_message *message, Manager *m) {
_cleanup_(address_freep) Address *tmp = NULL;
_cleanup_(address_unrefp) Address *tmp = NULL;
struct ifa_cacheinfo cinfo;
Link *link;
uint16_t type;
@ -1781,13 +1821,13 @@ int manager_rtnl_process_address(sd_netlink *rtnl, sd_netlink_message *message,
if (!address) {
/* If we did not know the address, then save it. */
r = address_add(link, tmp);
r = address_attach(link, tmp);
if (r < 0) {
log_link_warning_errno(link, r, "Failed to save received address %s, ignoring: %m",
IN_ADDR_PREFIX_TO_STRING(tmp->family, &tmp->in_addr, tmp->prefixlen));
return 0;
}
address = TAKE_PTR(tmp);
address = tmp;
is_new = true;
@ -1887,7 +1927,7 @@ int config_parse_broadcast(
void *userdata) {
Network *network = userdata;
_cleanup_(address_free_or_set_invalidp) Address *n = NULL;
_cleanup_(address_unref_or_set_invalidp) Address *n = NULL;
union in_addr_union u;
int r;
@ -1964,7 +2004,7 @@ int config_parse_address(
void *userdata) {
Network *network = userdata;
_cleanup_(address_free_or_set_invalidp) Address *n = NULL;
_cleanup_(address_unref_or_set_invalidp) Address *n = NULL;
union in_addr_union buffer;
unsigned char prefixlen;
int r, f;
@ -1978,7 +2018,7 @@ int config_parse_address(
if (streq(section, "Network")) {
if (isempty(rvalue)) {
/* If an empty string specified in [Network] section, clear previously assigned addresses. */
network->addresses_by_section = ordered_hashmap_free_with_destructor(network->addresses_by_section, address_free);
network->addresses_by_section = ordered_hashmap_free(network->addresses_by_section);
return 0;
}
@ -2051,7 +2091,7 @@ int config_parse_label(
void *data,
void *userdata) {
_cleanup_(address_free_or_set_invalidp) Address *n = NULL;
_cleanup_(address_unref_or_set_invalidp) Address *n = NULL;
Network *network = userdata;
int r;
@ -2103,7 +2143,7 @@ int config_parse_lifetime(
void *userdata) {
Network *network = userdata;
_cleanup_(address_free_or_set_invalidp) Address *n = NULL;
_cleanup_(address_unref_or_set_invalidp) Address *n = NULL;
usec_t k;
int r;
@ -2152,7 +2192,7 @@ int config_parse_address_flags(
void *userdata) {
Network *network = userdata;
_cleanup_(address_free_or_set_invalidp) Address *n = NULL;
_cleanup_(address_unref_or_set_invalidp) Address *n = NULL;
int r;
assert(filename);
@ -2199,7 +2239,7 @@ int config_parse_address_scope(
void *userdata) {
Network *network = userdata;
_cleanup_(address_free_or_set_invalidp) Address *n = NULL;
_cleanup_(address_unref_or_set_invalidp) Address *n = NULL;
int r;
assert(filename);
@ -2243,7 +2283,7 @@ int config_parse_address_route_metric(
void *userdata) {
Network *network = userdata;
_cleanup_(address_free_or_set_invalidp) Address *n = NULL;
_cleanup_(address_unref_or_set_invalidp) Address *n = NULL;
int r;
assert(filename);
@ -2285,7 +2325,7 @@ int config_parse_duplicate_address_detection(
void *userdata) {
Network *network = userdata;
_cleanup_(address_free_or_set_invalidp) Address *n = NULL;
_cleanup_(address_unref_or_set_invalidp) Address *n = NULL;
int r;
assert(filename);
@ -2339,7 +2379,7 @@ int config_parse_address_netlabel(
void *userdata) {
Network *network = userdata;
_cleanup_(address_free_or_set_invalidp) Address *n = NULL;
_cleanup_(address_unref_or_set_invalidp) Address *n = NULL;
int r;
assert(filename);
@ -2481,8 +2521,8 @@ int network_drop_invalid_addresses(Network *network) {
if (address_section_verify(address) < 0) {
/* Drop invalid [Address] sections or Address= settings in [Network].
* Note that address_free() will drop the address from addresses_by_section. */
address_free(address);
* Note that address_detach() will drop the address from addresses_by_section. */
address_detach(address);
continue;
}
@ -2495,12 +2535,13 @@ int network_drop_invalid_addresses(Network *network) {
IN_ADDR_PREFIX_TO_STRING(address->family, &address->in_addr, address->prefixlen),
address->section->line,
dup->section->line, dup->section->line);
/* address_free() will drop the address from addresses_by_section. */
address_free(dup);
/* address_detach() will drop the address from addresses_by_section. */
address_detach(dup);
}
/* Use address_hash_ops, instead of address_hash_ops_free. Otherwise, the Address objects
* will be freed. */
/* Use address_hash_ops, instead of address_hash_ops_detach. Otherwise, the Address objects
* will be detached. */
r = set_ensure_put(&addresses, &address_hash_ops, address);
if (r < 0)
return log_oom();
@ -2527,7 +2568,7 @@ int config_parse_address_ip_nft_set(
void *userdata) {
Network *network = userdata;
_cleanup_(address_free_or_set_invalidp) Address *n = NULL;
_cleanup_(address_unref_or_set_invalidp) Address *n = NULL;
int r;
assert(filename);

View File

@ -34,6 +34,8 @@ struct Address {
NetworkConfigState state;
union in_addr_union provider; /* DHCP server or router address */
unsigned n_ref;
int family;
unsigned char prefixlen;
unsigned char scope;
@ -83,9 +85,11 @@ void link_get_address_states(
extern const struct hash_ops address_hash_ops;
Address* address_ref(Address *address);
Address* address_unref(Address *address);
int address_new(Address **ret);
int address_new_static(Network *network, const char *filename, unsigned section_line, Address **ret);
Address* address_free(Address *address);
int address_get(Link *link, const Address *in, Address **ret);
int address_get_harder(Link *link, const Address *in, Address **ret);
int address_configure_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, const char *error_msg);
@ -95,7 +99,7 @@ int address_dup(const Address *src, Address **ret);
bool address_is_ready(const Address *a);
bool link_check_addresses_ready(Link *link, NetworkConfigSource source);
DEFINE_SECTION_CLEANUP_FUNCTIONS(Address, address_free);
DEFINE_SECTION_CLEANUP_FUNCTIONS(Address, address_unref);
int link_drop_managed_addresses(Link *link);
int link_drop_foreign_addresses(Link *link);

View File

@ -366,7 +366,7 @@ static int dhcp_pd_request_address(
return log_link_warning_errno(link, r, "Failed to generate addresses for acquired DHCP delegated prefix: %m");
SET_FOREACH(a, addresses) {
_cleanup_(address_freep) Address *address = NULL;
_cleanup_(address_unrefp) Address *address = NULL;
Address *existing;
r = address_new(&address);

View File

@ -92,7 +92,7 @@ int network_adjust_dhcp_server(Network *network, Set **addresses) {
}
} else {
_cleanup_(address_freep) Address *a = NULL;
_cleanup_(address_unrefp) Address *a = NULL;
Address *existing;
unsigned line;

View File

@ -883,7 +883,7 @@ static int dhcp4_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Reques
}
static int dhcp4_request_address(Link *link, bool announce) {
_cleanup_(address_freep) Address *addr = NULL;
_cleanup_(address_unrefp) Address *addr = NULL;
struct in_addr address, server;
uint8_t prefixlen;
Address *existing;

View File

@ -191,7 +191,7 @@ static int dhcp6_request_address(
usec_t lifetime_preferred_usec,
usec_t lifetime_valid_usec) {
_cleanup_(address_freep) Address *addr = NULL;
_cleanup_(address_unrefp) Address *addr = NULL;
Address *existing;
int r;

View File

@ -28,7 +28,7 @@ bool link_ipv4ll_enabled(Link *link) {
}
static int address_new_from_ipv4ll(Link *link, Address **ret) {
_cleanup_(address_freep) Address *address = NULL;
_cleanup_(address_unrefp) Address *address = NULL;
struct in_addr addr;
int r;
@ -56,7 +56,7 @@ static int address_new_from_ipv4ll(Link *link, Address **ret) {
}
static int ipv4ll_address_lost(Link *link) {
_cleanup_(address_freep) Address *address = NULL;
_cleanup_(address_unrefp) Address *address = NULL;
int r;
assert(link);
@ -92,7 +92,7 @@ static int ipv4ll_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Reque
}
static int ipv4ll_address_claimed(sd_ipv4ll *ll, Link *link) {
_cleanup_(address_freep) Address *address = NULL;
_cleanup_(address_unrefp) Address *address = NULL;
int r;
assert(ll);

View File

@ -247,7 +247,7 @@ static int ndisc_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Reques
}
static int ndisc_request_address(Address *in, Link *link, sd_ndisc_router *rt) {
_cleanup_(address_freep) Address *address = in;
_cleanup_(address_unrefp) Address *address = in;
struct in6_addr router;
bool is_new;
int r;
@ -435,7 +435,7 @@ static int ndisc_router_process_autonomous_prefix(Link *link, sd_ndisc_router *r
return log_link_warning_errno(link, r, "Failed to generate SLAAC addresses: %m");
SET_FOREACH(a, addresses) {
_cleanup_(address_freep) Address *address = NULL;
_cleanup_(address_unrefp) Address *address = NULL;
r = address_new(&address);
if (r < 0)

View File

@ -10,28 +10,87 @@
#include "networkd-queue.h"
#include "set.h"
Neighbor *neighbor_free(Neighbor *neighbor) {
if (!neighbor)
return NULL;
static Neighbor* neighbor_detach_impl(Neighbor *neighbor) {
assert(neighbor);
assert(!neighbor->link || !neighbor->network);
if (neighbor->network) {
assert(neighbor->section);
ordered_hashmap_remove(neighbor->network->neighbors_by_section, neighbor->section);
neighbor->network = NULL;
return neighbor;
}
config_section_free(neighbor->section);
if (neighbor->link)
if (neighbor->link) {
set_remove(neighbor->link->neighbors, neighbor);
neighbor->link = NULL;
return neighbor;
}
return NULL;
}
static void neighbor_detach(Neighbor *neighbor) {
neighbor_unref(neighbor_detach_impl(neighbor));
}
static Neighbor* neighbor_free(Neighbor *neighbor) {
if (!neighbor)
return NULL;
neighbor_detach_impl(neighbor);
config_section_free(neighbor->section);
return mfree(neighbor);
}
DEFINE_SECTION_CLEANUP_FUNCTIONS(Neighbor, neighbor_free);
DEFINE_TRIVIAL_REF_UNREF_FUNC(Neighbor, neighbor, neighbor_free);
DEFINE_SECTION_CLEANUP_FUNCTIONS(Neighbor, neighbor_unref);
static void neighbor_hash_func(const Neighbor *neighbor, struct siphash *state);
static int neighbor_compare_func(const Neighbor *a, const Neighbor *b);
DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(
neighbor_hash_ops_detach,
Neighbor,
neighbor_hash_func,
neighbor_compare_func,
neighbor_detach);
DEFINE_PRIVATE_HASH_OPS(
neighbor_hash_ops,
Neighbor,
neighbor_hash_func,
neighbor_compare_func);
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
neighbor_section_hash_ops,
ConfigSection,
config_section_hash_func,
config_section_compare_func,
Neighbor,
neighbor_detach);
static int neighbor_new(Neighbor **ret) {
Neighbor *neighbor;
assert(ret);
neighbor = new(Neighbor, 1);
if (!neighbor)
return -ENOMEM;
*neighbor = (Neighbor) {
.n_ref = 1,
};
*ret = TAKE_PTR(neighbor);
return 0;
}
static int neighbor_new_static(Network *network, const char *filename, unsigned section_line, Neighbor **ret) {
_cleanup_(config_section_freep) ConfigSection *n = NULL;
_cleanup_(neighbor_freep) Neighbor *neighbor = NULL;
_cleanup_(neighbor_unrefp) Neighbor *neighbor = NULL;
int r;
assert(network);
@ -49,18 +108,15 @@ static int neighbor_new_static(Network *network, const char *filename, unsigned
return 0;
}
neighbor = new(Neighbor, 1);
if (!neighbor)
return -ENOMEM;
r = neighbor_new(&neighbor);
if (r < 0)
return r;
*neighbor = (Neighbor) {
.network = network,
.family = AF_UNSPEC,
.section = TAKE_PTR(n),
.source = NETWORK_CONFIG_SOURCE_STATIC,
};
neighbor->network = network;
neighbor->section = TAKE_PTR(n);
neighbor->source = NETWORK_CONFIG_SOURCE_STATIC;
r = ordered_hashmap_ensure_put(&network->neighbors_by_section, &config_section_hash_ops, neighbor->section, neighbor);
r = ordered_hashmap_ensure_put(&network->neighbors_by_section, &neighbor_section_hash_ops, neighbor->section, neighbor);
if (r < 0)
return r;
@ -69,7 +125,7 @@ static int neighbor_new_static(Network *network, const char *filename, unsigned
}
static int neighbor_dup(const Neighbor *neighbor, Neighbor **ret) {
_cleanup_(neighbor_freep) Neighbor *dest = NULL;
_cleanup_(neighbor_unrefp) Neighbor *dest = NULL;
assert(neighbor);
assert(ret);
@ -78,7 +134,8 @@ static int neighbor_dup(const Neighbor *neighbor, Neighbor **ret) {
if (!dest)
return -ENOMEM;
/* Unset all pointers */
/* Clear the reference counter and all pointers */
dest->n_ref = 1;
dest->link = NULL;
dest->network = NULL;
dest->section = NULL;
@ -115,19 +172,6 @@ static int neighbor_compare_func(const Neighbor *a, const Neighbor *b) {
return memcmp(&a->in_addr, &b->in_addr, FAMILY_ADDRESS_SIZE(a->family));
}
DEFINE_PRIVATE_HASH_OPS(
neighbor_hash_ops,
Neighbor,
neighbor_hash_func,
neighbor_compare_func);
DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(
neighbor_hash_ops_free,
Neighbor,
neighbor_hash_func,
neighbor_compare_func,
neighbor_free);
static int neighbor_get_request(Link *link, const Neighbor *neighbor, Request **ret) {
Request *req;
@ -167,19 +211,21 @@ int neighbor_get(Link *link, const Neighbor *in, Neighbor **ret) {
return 0;
}
static int neighbor_add(Link *link, Neighbor *neighbor) {
static int neighbor_attach(Link *link, Neighbor *neighbor) {
int r;
assert(link);
assert(neighbor);
assert(!neighbor->link);
r = set_ensure_put(&link->neighbors, &neighbor_hash_ops_free, neighbor);
r = set_ensure_put(&link->neighbors, &neighbor_hash_ops_detach, neighbor);
if (r < 0)
return r;
if (r == 0)
return -EEXIST;
neighbor->link = link;
neighbor_ref(neighbor);
return 0;
}
@ -279,7 +325,7 @@ static int static_neighbor_configure_handler(sd_netlink *rtnl, sd_netlink_messag
}
static int link_request_neighbor(Link *link, const Neighbor *neighbor) {
_cleanup_(neighbor_freep) Neighbor *tmp = NULL;
_cleanup_(neighbor_unrefp) Neighbor *tmp = NULL;
Neighbor *existing = NULL;
int r;
@ -308,7 +354,7 @@ static int link_request_neighbor(Link *link, const Neighbor *neighbor) {
log_neighbor_debug(tmp, "Requesting", link);
r = link_queue_request_safe(link, REQUEST_TYPE_NEIGHBOR,
tmp,
neighbor_free,
neighbor_unref,
neighbor_hash_func,
neighbor_compare_func,
neighbor_process_request,
@ -470,7 +516,7 @@ void link_foreignize_neighbors(Link *link) {
}
int manager_rtnl_process_neighbor(sd_netlink *rtnl, sd_netlink_message *message, Manager *m) {
_cleanup_(neighbor_freep) Neighbor *tmp = NULL;
_cleanup_(neighbor_unrefp) Neighbor *tmp = NULL;
Neighbor *neighbor = NULL;
Request *req = NULL;
uint16_t type, state;
@ -522,8 +568,8 @@ int manager_rtnl_process_neighbor(sd_netlink *rtnl, sd_netlink_message *message,
* kernel sends messages about neighbors after a link is removed. So, just ignore it. */
return 0;
tmp = new0(Neighbor, 1);
if (!tmp)
r = neighbor_new(&tmp);
if (r < 0)
return log_oom();
/* First, retrieve the fundamental information about the neighbor. */
@ -553,7 +599,7 @@ int manager_rtnl_process_neighbor(sd_netlink *rtnl, sd_netlink_message *message,
if (neighbor) {
neighbor_enter_removed(neighbor);
log_neighbor_debug(neighbor, "Forgetting removed", link);
neighbor_free(neighbor);
neighbor_detach(neighbor);
} else
log_neighbor_debug(tmp, "Kernel removed unknown", link);
@ -565,12 +611,12 @@ int manager_rtnl_process_neighbor(sd_netlink *rtnl, sd_netlink_message *message,
/* If we did not know the neighbor, then save it. */
if (!neighbor) {
r = neighbor_add(link, tmp);
r = neighbor_attach(link, tmp);
if (r < 0) {
log_link_warning_errno(link, r, "Failed to save received neighbor, ignoring: %m");
return 0;
}
neighbor = TAKE_PTR(tmp);
neighbor = tmp;
is_new = true;
}
@ -631,9 +677,9 @@ int network_drop_invalid_neighbors(Network *network) {
Neighbor *dup;
if (neighbor_section_verify(neighbor) < 0) {
/* Drop invalid [Neighbor] sections. Note that neighbor_free() will drop the
/* Drop invalid [Neighbor] sections. Note that neighbor_detach() will drop the
* neighbor from neighbors_by_section. */
neighbor_free(neighbor);
neighbor_detach(neighbor);
continue;
}
@ -646,12 +692,12 @@ int network_drop_invalid_neighbors(Network *network) {
IN_ADDR_TO_STRING(neighbor->family, &neighbor->in_addr),
neighbor->section->line,
dup->section->line, dup->section->line);
/* neighbor_free() will drop the neighbor from neighbors_by_section. */
neighbor_free(dup);
/* neighbor_detach() will drop the neighbor from neighbors_by_section. */
neighbor_detach(dup);
}
/* Use neighbor_hash_ops, instead of neighbor_hash_ops_free. Otherwise, the Neighbor objects
* will be freed. */
/* Use neighbor_hash_ops, instead of neighbor_hash_ops_detach. Otherwise, the Neighbor objects
* will be detached. */
r = set_ensure_put(&neighbors, &neighbor_hash_ops, neighbor);
if (r < 0)
return log_oom();
@ -674,7 +720,7 @@ int config_parse_neighbor_address(
void *data,
void *userdata) {
_cleanup_(neighbor_free_or_set_invalidp) Neighbor *n = NULL;
_cleanup_(neighbor_unref_or_set_invalidp) Neighbor *n = NULL;
Network *network = ASSERT_PTR(userdata);
int r;
@ -717,7 +763,7 @@ int config_parse_neighbor_lladdr(
void *data,
void *userdata) {
_cleanup_(neighbor_free_or_set_invalidp) Neighbor *n = NULL;
_cleanup_(neighbor_unref_or_set_invalidp) Neighbor *n = NULL;
Network *network = ASSERT_PTR(userdata);
int r;

View File

@ -21,12 +21,15 @@ typedef struct Neighbor {
NetworkConfigSource source;
NetworkConfigState state;
unsigned n_ref;
int family;
union in_addr_union in_addr;
struct hw_addr_data ll_addr;
} Neighbor;
Neighbor *neighbor_free(Neighbor *neighbor);
Neighbor* neighbor_ref(Neighbor *neighbor);
Neighbor* neighbor_unref(Neighbor *neighbor);
int neighbor_get(Link *link, const Neighbor *in, Neighbor **ret);
int neighbor_remove(Neighbor *neighbor, Link *link);

View File

@ -187,7 +187,7 @@ int network_verify(Network *network) {
log_warning("%s: Cannot set routes when Bond= is specified, ignoring routes.",
network->filename);
network->addresses_by_section = ordered_hashmap_free_with_destructor(network->addresses_by_section, address_free);
network->addresses_by_section = ordered_hashmap_free(network->addresses_by_section);
network->routes_by_section = hashmap_free_with_destructor(network->routes_by_section, route_free);
}
@ -780,12 +780,12 @@ static Network *network_free(Network *network) {
/* static configs */
set_free_free(network->ipv6_proxy_ndp_addresses);
ordered_hashmap_free_with_destructor(network->addresses_by_section, address_free);
ordered_hashmap_free(network->addresses_by_section);
hashmap_free_with_destructor(network->routes_by_section, route_free);
ordered_hashmap_free_with_destructor(network->nexthops_by_section, nexthop_free);
ordered_hashmap_free(network->nexthops_by_section);
hashmap_free_with_destructor(network->bridge_fdb_entries_by_section, bridge_fdb_free);
hashmap_free_with_destructor(network->bridge_mdb_entries_by_section, bridge_mdb_free);
ordered_hashmap_free_with_destructor(network->neighbors_by_section, neighbor_free);
ordered_hashmap_free(network->neighbors_by_section);
hashmap_free_with_destructor(network->address_labels_by_section, address_label_free);
hashmap_free_with_destructor(network->prefixes_by_section, prefix_free);
hashmap_free_with_destructor(network->route_prefixes_by_section, route_prefix_free);

View File

@ -18,28 +18,45 @@
#include "stdio-util.h"
#include "string-util.h"
NextHop *nexthop_free(NextHop *nexthop) {
if (!nexthop)
return NULL;
static NextHop* nexthop_detach_impl(NextHop *nexthop) {
assert(nexthop);
assert(!nexthop->manager || !nexthop->network);
if (nexthop->network) {
assert(nexthop->section);
ordered_hashmap_remove(nexthop->network->nexthops_by_section, nexthop->section);
nexthop->network = NULL;
return nexthop;
}
config_section_free(nexthop->section);
if (nexthop->manager) {
assert(nexthop->id > 0);
hashmap_remove(nexthop->manager->nexthops_by_id, UINT32_TO_PTR(nexthop->id));
nexthop->manager = NULL;
return nexthop;
}
return NULL;
}
static void nexthop_detach(NextHop *nexthop) {
nexthop_unref(nexthop_detach_impl(nexthop));
}
static NextHop* nexthop_free(NextHop *nexthop) {
if (!nexthop)
return NULL;
nexthop_detach_impl(nexthop);
config_section_free(nexthop->section);
hashmap_free_free(nexthop->group);
return mfree(nexthop);
}
DEFINE_SECTION_CLEANUP_FUNCTIONS(NextHop, nexthop_free);
DEFINE_TRIVIAL_REF_UNREF_FUNC(NextHop, nexthop, nexthop_free);
DEFINE_SECTION_CLEANUP_FUNCTIONS(NextHop, nexthop_unref);
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
nexthop_hash_ops,
@ -47,17 +64,25 @@ DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
trivial_hash_func,
trivial_compare_func,
NextHop,
nexthop_free);
nexthop_detach);
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
nexthop_section_hash_ops,
ConfigSection,
config_section_hash_func,
config_section_compare_func,
NextHop,
nexthop_detach);
static int nexthop_new(NextHop **ret) {
_cleanup_(nexthop_freep) NextHop *nexthop = NULL;
_cleanup_(nexthop_unrefp) NextHop *nexthop = NULL;
nexthop = new(NextHop, 1);
if (!nexthop)
return -ENOMEM;
*nexthop = (NextHop) {
.family = AF_UNSPEC,
.n_ref = 1,
.onlink = -1,
};
@ -68,7 +93,7 @@ static int nexthop_new(NextHop **ret) {
static int nexthop_new_static(Network *network, const char *filename, unsigned section_line, NextHop **ret) {
_cleanup_(config_section_freep) ConfigSection *n = NULL;
_cleanup_(nexthop_freep) NextHop *nexthop = NULL;
_cleanup_(nexthop_unrefp) NextHop *nexthop = NULL;
int r;
assert(network);
@ -95,7 +120,7 @@ static int nexthop_new_static(Network *network, const char *filename, unsigned s
nexthop->section = TAKE_PTR(n);
nexthop->source = NETWORK_CONFIG_SOURCE_STATIC;
r = ordered_hashmap_ensure_put(&network->nexthops_by_section, &config_section_hash_ops, nexthop->section, nexthop);
r = ordered_hashmap_ensure_put(&network->nexthops_by_section, &nexthop_section_hash_ops, nexthop->section, nexthop);
if (r < 0)
return r;
@ -171,7 +196,7 @@ static int nexthop_compare_full(const NextHop *a, const NextHop *b) {
}
static int nexthop_dup(const NextHop *src, NextHop **ret) {
_cleanup_(nexthop_freep) NextHop *dest = NULL;
_cleanup_(nexthop_unrefp) NextHop *dest = NULL;
struct nexthop_grp *nhg;
int r;
@ -182,7 +207,8 @@ static int nexthop_dup(const NextHop *src, NextHop **ret) {
if (!dest)
return -ENOMEM;
/* unset all pointers */
/* clear the reference counter and all pointers */
dest->n_ref = 1;
dest->manager = NULL;
dest->network = NULL;
dest->section = NULL;
@ -329,7 +355,7 @@ static int nexthop_get_request(Link *link, const NextHop *in, Request **ret) {
}
static int nexthop_add_new(Manager *manager, uint32_t id, NextHop **ret) {
_cleanup_(nexthop_freep) NextHop *nexthop = NULL;
_cleanup_(nexthop_unrefp) NextHop *nexthop = NULL;
int r;
assert(manager);
@ -632,7 +658,7 @@ static int nexthop_process_request(Request *req, Link *link, NextHop *nexthop) {
}
static int link_request_nexthop(Link *link, const NextHop *nexthop) {
_cleanup_(nexthop_freep) NextHop *tmp = NULL;
_cleanup_(nexthop_unrefp) NextHop *tmp = NULL;
NextHop *existing = NULL;
int r;
@ -667,7 +693,7 @@ static int link_request_nexthop(Link *link, const NextHop *nexthop) {
log_nexthop_debug(tmp, "Requesting", link->manager);
r = link_queue_request_safe(link, REQUEST_TYPE_NEXTHOP,
tmp,
nexthop_free,
nexthop_unref,
nexthop_hash_func,
nexthop_compare_func,
nexthop_process_request,
@ -925,7 +951,7 @@ int manager_rtnl_process_nexthop(sd_netlink *rtnl, sd_netlink_message *message,
if (nexthop) {
nexthop_enter_removed(nexthop);
log_nexthop_debug(nexthop, "Forgetting removed", m);
nexthop_free(nexthop);
nexthop_detach(nexthop);
} else
log_nexthop_debug(&(const NextHop) { .id = id }, "Kernel removed unknown", m);
@ -1088,7 +1114,7 @@ int network_drop_invalid_nexthops(Network *network) {
ORDERED_HASHMAP_FOREACH(nh, network->nexthops_by_section) {
if (nexthop_section_verify(nh) < 0) {
nexthop_free(nh);
nexthop_detach(nh);
continue;
}
@ -1103,8 +1129,8 @@ int network_drop_invalid_nexthops(Network *network) {
dup->section->filename,
nh->id, nh->section->line,
dup->section->line, dup->section->line);
/* nexthop_free() will drop the nexthop from nexthops_by_section. */
nexthop_free(dup);
/* nexthop_detach() will drop the nexthop from nexthops_by_section. */
nexthop_detach(dup);
}
r = hashmap_ensure_put(&nexthops, NULL, UINT32_TO_PTR(nh->id), nh);
@ -1155,7 +1181,7 @@ int config_parse_nexthop_id(
void *data,
void *userdata) {
_cleanup_(nexthop_free_or_set_invalidp) NextHop *n = NULL;
_cleanup_(nexthop_unref_or_set_invalidp) NextHop *n = NULL;
Network *network = userdata;
uint32_t id;
int r;
@ -1205,7 +1231,7 @@ int config_parse_nexthop_gateway(
void *data,
void *userdata) {
_cleanup_(nexthop_free_or_set_invalidp) NextHop *n = NULL;
_cleanup_(nexthop_unref_or_set_invalidp) NextHop *n = NULL;
Network *network = userdata;
int r;
@ -1250,7 +1276,7 @@ int config_parse_nexthop_family(
void *data,
void *userdata) {
_cleanup_(nexthop_free_or_set_invalidp) NextHop *n = NULL;
_cleanup_(nexthop_unref_or_set_invalidp) NextHop *n = NULL;
Network *network = userdata;
AddressFamily a;
int r;
@ -1316,7 +1342,7 @@ int config_parse_nexthop_onlink(
void *data,
void *userdata) {
_cleanup_(nexthop_free_or_set_invalidp) NextHop *n = NULL;
_cleanup_(nexthop_unref_or_set_invalidp) NextHop *n = NULL;
Network *network = userdata;
int r;
@ -1353,7 +1379,7 @@ int config_parse_nexthop_blackhole(
void *data,
void *userdata) {
_cleanup_(nexthop_free_or_set_invalidp) NextHop *n = NULL;
_cleanup_(nexthop_unref_or_set_invalidp) NextHop *n = NULL;
Network *network = userdata;
int r;
@ -1392,7 +1418,7 @@ int config_parse_nexthop_group(
void *data,
void *userdata) {
_cleanup_(nexthop_free_or_set_invalidp) NextHop *n = NULL;
_cleanup_(nexthop_unref_or_set_invalidp) NextHop *n = NULL;
Network *network = userdata;
int r;

View File

@ -24,6 +24,8 @@ typedef struct NextHop {
NetworkConfigSource source;
NetworkConfigState state;
unsigned n_ref;
uint8_t protocol;
int ifindex;
uint32_t id;
@ -35,7 +37,8 @@ typedef struct NextHop {
Hashmap *group;
} NextHop;
NextHop *nexthop_free(NextHop *nexthop);
NextHop* nexthop_ref(NextHop *nexthop);
NextHop* nexthop_unref(NextHop *nexthop);
int nexthop_remove(NextHop *nexthop, Manager *manager);

View File

@ -258,7 +258,7 @@ int link_request_radv_addresses(Link *link) {
return r;
SET_FOREACH(a, addresses) {
_cleanup_(address_freep) Address *address = NULL;
_cleanup_(address_unrefp) Address *address = NULL;
r = address_new(&address);
if (r < 0)

View File

@ -795,12 +795,12 @@ bool stats_by_path_equal(Hashmap *a, Hashmap *b) {
return true;
}
static void config_section_hash_func(const ConfigSection *c, struct siphash *state) {
void config_section_hash_func(const ConfigSection *c, struct siphash *state) {
siphash24_compress_string(c->filename, state);
siphash24_compress_typesafe(c->line, state);
}
static int config_section_compare_func(const ConfigSection *x, const ConfigSection *y) {
int config_section_compare_func(const ConfigSection *x, const ConfigSection *y) {
int r;
r = strcmp(x->filename, y->filename);

View File

@ -137,7 +137,11 @@ static inline ConfigSection* config_section_free(ConfigSection *cs) {
DEFINE_TRIVIAL_CLEANUP_FUNC(ConfigSection*, config_section_free);
int config_section_new(const char *filename, unsigned line, ConfigSection **ret);
void config_section_hash_func(const ConfigSection *c, struct siphash *state);
int config_section_compare_func(const ConfigSection *x, const ConfigSection *y);
extern const struct hash_ops config_section_hash_ops;
int _hashmap_by_section_find_unused_line(
HashmapBase *entries_by_section,
const char *filename,