]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network/route: introduce reverse map for route with nexthop ID
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 15 Jan 2024 04:02:16 +0000 (13:02 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 25 Jan 2024 07:43:44 +0000 (16:43 +0900)
It is not used in this commit, but will be used later.
Preparation for later commits.

This is the one for routes of 531c7246829a41dd7e51847bd4d77aa012ff478f.

src/network/networkd-nexthop.c
src/network/networkd-nexthop.h
src/network/networkd-route-nexthop.c
src/network/networkd-route-nexthop.h
src/network/networkd-route.c
src/network/networkd-route.h

index cf62e0e82dc666450a42faf5bf2d87ec4c1bc980..67eba509d3364e3eccbb501c5fcf705063baaad9 100644 (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);
 }
index 74b23bd77203f0349e0f9ea98248256c1ba476ee..9ce36c6cd9b18d3f19bc8b73da1e555d8264cb79 100644 (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);
index 8ccd4e66e87e14ccfb442e9c7085ab95b823c890..f7a2201b6b0a0b7ed70a50b777d1328e95488c7d 100644 (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"
 #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);
 
index 5e4602d3cd369d3808b68f2cb6a524192a651b61..f9a147839db9c5eec4d80033e6731a288b10f505 100644 (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);
 
index 147d9cb14fde5a7c40a2791e38cc148b3faf6558..80e20a286b0ba7a139e80ae68b2850d0fc4b6577 100644 (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,
index 004295e5998ebd297ff03b7775f86b0b629659ed..c5c2693ac4ff425c8066042ab521ce5b5cda5879 100644 (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);