network/route: introduce reverse map for route with nexthop ID

It is not used in this commit, but will be used later.
Preparation for later commits.

This is the one for routes of 531c724682.
This commit is contained in:
Yu Watanabe
2024-01-15 13:02:16 +09:00
parent 97979ece0e
commit 6f09031e4d
6 changed files with 58 additions and 1 deletions

View File

@@ -97,6 +97,7 @@ static NextHop* nexthop_free(NextHop *nexthop) {
config_section_free(nexthop->section);
hashmap_free_free(nexthop->group);
set_free(nexthop->nexthops);
set_free(nexthop->routes);
return mfree(nexthop);
}

View File

@@ -41,8 +41,9 @@ typedef struct NextHop {
/* Only used in conf parser and nexthop_section_verify(). */
int onlink;
/* For managing nexthops that depend on this nexthop. */
/* For managing routes and nexthops that depend on this nexthop. */
Set *nexthops;
Set *routes;
} NextHop;
NextHop* nexthop_ref(NextHop *nexthop);

View File

@@ -5,6 +5,7 @@
#include "alloc-util.h"
#include "extract-word.h"
#include "netlink-util.h"
#include "networkd-manager.h"
#include "networkd-network.h"
#include "networkd-nexthop.h"
#include "networkd-route.h"
@@ -13,6 +14,48 @@
#include "parse-util.h"
#include "string-util.h"
void route_detach_from_nexthop(Route *route) {
NextHop *nh;
assert(route);
assert(route->manager);
if (route->nexthop_id == 0)
return;
if (nexthop_get_by_id(route->manager, route->nexthop_id, &nh) < 0)
return;
route_unref(set_remove(nh->routes, route));
}
void route_attach_to_nexthop(Route *route) {
NextHop *nh;
int r;
assert(route);
assert(route->manager);
if (route->nexthop_id == 0)
return;
r = nexthop_get_by_id(route->manager, route->nexthop_id, &nh);
if (r < 0) {
if (route->manager->manage_foreign_nexthops)
log_debug_errno(r, "Route has unknown nexthop ID (%"PRIu32"), ignoring.",
route->nexthop_id);
return;
}
r = set_ensure_put(&nh->routes, &route_hash_ops_unref, route);
if (r < 0)
return (void) log_debug_errno(r, "Failed to save route to nexthop, ignoring: %m");
if (r == 0)
return (void) log_debug("Duplicated route assigned to nexthop, ignoring.");
route_ref(route);
}
static void route_nexthop_done(RouteNextHop *nh) {
assert(nh);

View File

@@ -26,6 +26,9 @@ typedef struct RouteNextHop {
#define ROUTE_NEXTHOP_NULL ((const RouteNextHop) {})
void route_detach_from_nexthop(Route *route);
void route_attach_to_nexthop(Route *route);
RouteNextHop* route_nexthop_free(RouteNextHop *nh);
DEFINE_TRIVIAL_CLEANUP_FUNC(RouteNextHop*, route_nexthop_free);

View File

@@ -33,6 +33,7 @@ static Route* route_detach_impl(Route *route) {
}
if (route->manager) {
route_detach_from_nexthop(route);
set_remove(route->manager->routes, route);
route->manager = NULL;
return route;
@@ -220,6 +221,13 @@ DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(
route_compare_func,
route_detach);
DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(
route_hash_ops_unref,
Route,
route_hash_func,
route_compare_func,
route_unref);
DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
route_section_hash_ops,
ConfigSection,

View File

@@ -81,6 +81,7 @@ struct Route {
};
extern const struct hash_ops route_hash_ops;
extern const struct hash_ops route_hash_ops_unref;
Route* route_ref(Route *route);
Route* route_unref(Route *route);