Skip to content

Commit 10d7864

Browse files
committed
network/radv: use hash_ops with destructor for managing prefixes
1 parent d3af116 commit 10d7864

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

src/network/networkd-network.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -831,9 +831,9 @@ static Network *network_free(Network *network) {
831831
hashmap_free_with_destructor(network->bridge_mdb_entries_by_section, bridge_mdb_free);
832832
ordered_hashmap_free(network->neighbors_by_section);
833833
hashmap_free(network->address_labels_by_section);
834-
hashmap_free_with_destructor(network->prefixes_by_section, prefix_free);
835-
hashmap_free_with_destructor(network->route_prefixes_by_section, route_prefix_free);
836-
hashmap_free_with_destructor(network->pref64_prefixes_by_section, prefix64_free);
834+
hashmap_free(network->prefixes_by_section);
835+
hashmap_free(network->route_prefixes_by_section);
836+
hashmap_free(network->pref64_prefixes_by_section);
837837
hashmap_free(network->rules_by_section);
838838
hashmap_free_with_destructor(network->dhcp_static_leases_by_section, dhcp_static_lease_free);
839839
ordered_hashmap_free(network->sr_iov_by_section);

src/network/networkd-radv.c

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ bool link_radv_enabled(Link *link) {
3535
return link->network->router_prefix_delegation;
3636
}
3737

38-
Prefix* prefix_free(Prefix *prefix) {
38+
static Prefix* prefix_free(Prefix *prefix) {
3939
if (!prefix)
4040
return NULL;
4141

@@ -52,6 +52,11 @@ Prefix* prefix_free(Prefix *prefix) {
5252

5353
DEFINE_SECTION_CLEANUP_FUNCTIONS(Prefix, prefix_free);
5454

55+
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
56+
prefix_hash_ops_by_section,
57+
ConfigSection, config_section_hash_func, config_section_compare_func,
58+
Prefix, prefix_free);
59+
5560
static int prefix_new_static(Network *network, const char *filename, unsigned section_line, Prefix **ret) {
5661
_cleanup_(config_section_freep) ConfigSection *n = NULL;
5762
_cleanup_(prefix_freep) Prefix *prefix = NULL;
@@ -87,15 +92,15 @@ static int prefix_new_static(Network *network, const char *filename, unsigned se
8792
.prefix.preferred_until = USEC_INFINITY,
8893
};
8994

90-
r = hashmap_ensure_put(&network->prefixes_by_section, &config_section_hash_ops, prefix->section, prefix);
95+
r = hashmap_ensure_put(&network->prefixes_by_section, &prefix_hash_ops_by_section, prefix->section, prefix);
9196
if (r < 0)
9297
return r;
9398

9499
*ret = TAKE_PTR(prefix);
95100
return 0;
96101
}
97102

98-
RoutePrefix* route_prefix_free(RoutePrefix *prefix) {
103+
static RoutePrefix* route_prefix_free(RoutePrefix *prefix) {
99104
if (!prefix)
100105
return NULL;
101106

@@ -111,6 +116,11 @@ RoutePrefix* route_prefix_free(RoutePrefix *prefix) {
111116

112117
DEFINE_SECTION_CLEANUP_FUNCTIONS(RoutePrefix, route_prefix_free);
113118

119+
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
120+
route_prefix_hash_ops_by_section,
121+
ConfigSection, config_section_hash_func, config_section_compare_func,
122+
RoutePrefix, route_prefix_free);
123+
114124
static int route_prefix_new_static(Network *network, const char *filename, unsigned section_line, RoutePrefix **ret) {
115125
_cleanup_(config_section_freep) ConfigSection *n = NULL;
116126
_cleanup_(route_prefix_freep) RoutePrefix *prefix = NULL;
@@ -143,15 +153,15 @@ static int route_prefix_new_static(Network *network, const char *filename, unsig
143153
.route.valid_until = USEC_INFINITY,
144154
};
145155

146-
r = hashmap_ensure_put(&network->route_prefixes_by_section, &config_section_hash_ops, prefix->section, prefix);
156+
r = hashmap_ensure_put(&network->route_prefixes_by_section, &route_prefix_hash_ops_by_section, prefix->section, prefix);
147157
if (r < 0)
148158
return r;
149159

150160
*ret = TAKE_PTR(prefix);
151161
return 0;
152162
}
153163

154-
Prefix64* prefix64_free(Prefix64 *prefix) {
164+
static Prefix64* prefix64_free(Prefix64 *prefix) {
155165
if (!prefix)
156166
return NULL;
157167

@@ -167,6 +177,11 @@ Prefix64* prefix64_free(Prefix64 *prefix) {
167177

168178
DEFINE_SECTION_CLEANUP_FUNCTIONS(Prefix64, prefix64_free);
169179

180+
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
181+
prefix64_hash_ops_by_section,
182+
ConfigSection, config_section_hash_func, config_section_compare_func,
183+
Prefix64, prefix64_free);
184+
170185
static int prefix64_new_static(Network *network, const char *filename, unsigned section_line, Prefix64 **ret) {
171186
_cleanup_(config_section_freep) ConfigSection *n = NULL;
172187
_cleanup_(prefix64_freep) Prefix64 *prefix = NULL;
@@ -199,7 +214,7 @@ static int prefix64_new_static(Network *network, const char *filename, unsigned
199214
.prefix64.valid_until = USEC_INFINITY,
200215
};
201216

202-
r = hashmap_ensure_put(&network->pref64_prefixes_by_section, &config_section_hash_ops, prefix->section, prefix);
217+
r = hashmap_ensure_put(&network->pref64_prefixes_by_section, &prefix64_hash_ops_by_section, prefix->section, prefix);
203218
if (r < 0)
204219
return r;
205220

@@ -809,9 +824,9 @@ void network_adjust_radv(Network *network) {
809824
}
810825

811826
if (!FLAGS_SET(network->router_prefix_delegation, RADV_PREFIX_DELEGATION_STATIC)) {
812-
network->prefixes_by_section = hashmap_free_with_destructor(network->prefixes_by_section, prefix_free);
813-
network->route_prefixes_by_section = hashmap_free_with_destructor(network->route_prefixes_by_section, route_prefix_free);
814-
network->pref64_prefixes_by_section = hashmap_free_with_destructor(network->pref64_prefixes_by_section, prefix64_free);
827+
network->prefixes_by_section = hashmap_free(network->prefixes_by_section);
828+
network->route_prefixes_by_section = hashmap_free(network->route_prefixes_by_section);
829+
network->pref64_prefixes_by_section = hashmap_free(network->pref64_prefixes_by_section);
815830
}
816831

817832
if (!network->router_prefix_delegation)

src/network/networkd-radv.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ typedef struct Prefix64 {
5252
sd_ndisc_prefix64 prefix64;
5353
} Prefix64;
5454

55-
Prefix* prefix_free(Prefix *prefix);
56-
RoutePrefix* route_prefix_free(RoutePrefix *prefix);
57-
Prefix64* prefix64_free(Prefix64 *prefix);
58-
5955
void network_adjust_radv(Network *network);
6056

6157
int link_request_radv_addresses(Link *link);

0 commit comments

Comments
 (0)