Skip to content

Commit b82663d

Browse files
committed
network: drop list of static nexthops
[NextHop] sections are managed by both LIST and Hashmap. Let's drop list, as they store the completely same information.
1 parent 8d7b137 commit b82663d

File tree

4 files changed

+24
-45
lines changed

4 files changed

+24
-45
lines changed

src/network/networkd-network.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ int network_verify(Network *network) {
152152
RoutePrefix *route_prefix, *route_prefix_next;
153153
Neighbor *neighbor, *neighbor_next;
154154
AddressLabel *label, *label_next;
155-
NextHop *nexthop, *nextnop_next;
155+
NextHop *nexthop;
156156
Address *address, *address_next;
157157
Prefix *prefix, *prefix_next;
158158
Route *route, *route_next;
@@ -298,7 +298,7 @@ int network_verify(Network *network) {
298298
if (route_section_verify(route, network) < 0)
299299
route_free(route);
300300

301-
LIST_FOREACH_SAFE(nexthops, nexthop, nextnop_next, network->static_nexthops)
301+
HASHMAP_FOREACH(nexthop, network->nexthops_by_section)
302302
if (nexthop_section_verify(nexthop) < 0)
303303
nexthop_free(nexthop);
304304

@@ -649,7 +649,6 @@ static Network *network_free(Network *network) {
649649
MdbEntry *mdb_entry;
650650
Neighbor *neighbor;
651651
Address *address;
652-
NextHop *nexthop;
653652
Prefix *prefix;
654653
Route *route;
655654

@@ -711,9 +710,6 @@ static Network *network_free(Network *network) {
711710
while ((route = network->static_routes))
712711
route_free(route);
713712

714-
while ((nexthop = network->static_nexthops))
715-
nexthop_free(nexthop);
716-
717713
while ((address = network->static_addresses))
718714
address_free(address);
719715

@@ -740,7 +736,7 @@ static Network *network_free(Network *network) {
740736

741737
hashmap_free(network->addresses_by_section);
742738
hashmap_free(network->routes_by_section);
743-
hashmap_free(network->nexthops_by_section);
739+
hashmap_free_with_destructor(network->nexthops_by_section, nexthop_free);
744740
hashmap_free(network->fdb_entries_by_section);
745741
hashmap_free(network->mdb_entries_by_section);
746742
hashmap_free(network->neighbors_by_section);

src/network/networkd-network.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,6 @@ struct Network {
286286

287287
LIST_HEAD(Address, static_addresses);
288288
LIST_HEAD(Route, static_routes);
289-
LIST_HEAD(NextHop, static_nexthops);
290289
LIST_HEAD(FdbEntry, static_fdb_entries);
291290
LIST_HEAD(MdbEntry, static_mdb_entries);
292291
LIST_HEAD(IPv6ProxyNDPAddress, ipv6_proxy_ndp_addresses);
@@ -297,7 +296,6 @@ struct Network {
297296

298297
unsigned n_static_addresses;
299298
unsigned n_static_routes;
300-
unsigned n_static_nexthops;
301299
unsigned n_static_fdb_entries;
302300
unsigned n_static_mdb_entries;
303301
unsigned n_ipv6_proxy_ndp_addresses;

src/network/networkd-nexthop.c

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,8 @@ void nexthop_free(NextHop *nexthop) {
2020
return;
2121

2222
if (nexthop->network) {
23-
LIST_REMOVE(nexthops, nexthop->network->static_nexthops, nexthop);
24-
25-
assert(nexthop->network->n_static_nexthops > 0);
26-
nexthop->network->n_static_nexthops--;
27-
28-
if (nexthop->section)
29-
hashmap_remove(nexthop->network->nexthops_by_section, nexthop->section);
23+
assert(nexthop->section);
24+
hashmap_remove(nexthop->network->nexthops_by_section, nexthop->section);
3025
}
3126

3227
network_config_section_free(nexthop->section);
@@ -64,19 +59,17 @@ static int nexthop_new_static(Network *network, const char *filename, unsigned s
6459

6560
assert(network);
6661
assert(ret);
67-
assert(!!filename == (section_line > 0));
68-
69-
if (filename) {
70-
r = network_config_section_new(filename, section_line, &n);
71-
if (r < 0)
72-
return r;
62+
assert(filename);
63+
assert(section_line > 0);
7364

74-
nexthop = hashmap_get(network->nexthops_by_section, n);
75-
if (nexthop) {
76-
*ret = TAKE_PTR(nexthop);
65+
r = network_config_section_new(filename, section_line, &n);
66+
if (r < 0)
67+
return r;
7768

78-
return 0;
79-
}
69+
nexthop = hashmap_get(network->nexthops_by_section, n);
70+
if (nexthop) {
71+
*ret = TAKE_PTR(nexthop);
72+
return 0;
8073
}
8174

8275
r = nexthop_new(&nexthop);
@@ -85,23 +78,17 @@ static int nexthop_new_static(Network *network, const char *filename, unsigned s
8578

8679
nexthop->protocol = RTPROT_STATIC;
8780
nexthop->network = network;
88-
LIST_PREPEND(nexthops, network->static_nexthops, nexthop);
89-
network->n_static_nexthops++;
81+
nexthop->section = TAKE_PTR(n);
9082

91-
if (filename) {
92-
nexthop->section = TAKE_PTR(n);
93-
94-
r = hashmap_ensure_allocated(&network->nexthops_by_section, &network_config_hash_ops);
95-
if (r < 0)
96-
return r;
83+
r = hashmap_ensure_allocated(&network->nexthops_by_section, &network_config_hash_ops);
84+
if (r < 0)
85+
return r;
9786

98-
r = hashmap_put(network->nexthops_by_section, nexthop->section, nexthop);
99-
if (r < 0)
100-
return r;
101-
}
87+
r = hashmap_put(network->nexthops_by_section, nexthop->section, nexthop);
88+
if (r < 0)
89+
return r;
10290

10391
*ret = TAKE_PTR(nexthop);
104-
10592
return 0;
10693
}
10794

@@ -343,12 +330,12 @@ int link_set_nexthop(Link *link) {
343330

344331
link->static_nexthops_configured = false;
345332

346-
LIST_FOREACH(nexthops, nh, link->network->static_nexthops) {
333+
HASHMAP_FOREACH(nh, link->network->nexthops_by_section) {
347334
r = nexthop_configure(nh, link);
348335
if (r < 0)
349336
return log_link_warning_errno(link, r, "Could not set nexthop: %m");
350-
if (r > 0)
351-
link->nexthop_messages++;
337+
338+
link->nexthop_messages++;
352339
}
353340

354341
if (link->nexthop_messages == 0) {

src/network/networkd-nexthop.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ struct NextHop {
2626
uint32_t id;
2727

2828
union in_addr_union gw;
29-
30-
LIST_FIELDS(NextHop, nexthops);
3129
};
3230

3331
void nexthop_free(NextHop *nexthop);

0 commit comments

Comments
 (0)