From: Yu Watanabe Date: Mon, 15 Jan 2024 03:39:19 +0000 (+0900) Subject: network/nexthop: introduce a reverse map of nexthop group members X-Git-Tag: v256-rc1~1146^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=531c7246829a41dd7e51847bd4d77aa012ff478f;p=thirdparty%2Fsystemd.git network/nexthop: introduce a reverse map of nexthop group members It is not used in this commit, but will be used later. Preparation for later commits. --- diff --git a/src/network/networkd-nexthop.c b/src/network/networkd-nexthop.c index f440187c310..49856c483a7 100644 --- a/src/network/networkd-nexthop.c +++ b/src/network/networkd-nexthop.c @@ -18,6 +18,48 @@ #include "stdio-util.h" #include "string-util.h" +static void nexthop_detach_from_group_members(NextHop *nexthop) { + assert(nexthop); + assert(nexthop->manager); + assert(nexthop->id > 0); + + struct nexthop_grp *nhg; + HASHMAP_FOREACH(nhg, nexthop->group) { + NextHop *nh; + + if (nexthop_get_by_id(nexthop->manager, nhg->id, &nh) < 0) + continue; + + set_remove(nh->nexthops, UINT32_TO_PTR(nexthop->id)); + } +} + +static void nexthop_attach_to_group_members(NextHop *nexthop) { + int r; + + assert(nexthop); + assert(nexthop->manager); + assert(nexthop->id > 0); + + struct nexthop_grp *nhg; + HASHMAP_FOREACH(nhg, nexthop->group) { + NextHop *nh; + + r = nexthop_get_by_id(nexthop->manager, nhg->id, &nh); + if (r < 0) { + if (nexthop->manager->manage_foreign_nexthops) + log_debug_errno(r, "Nexthop (id=%"PRIu32") has unknown group member (%"PRIu32"), ignoring.", + nexthop->id, nhg->id); + continue; + } + + r = set_ensure_put(&nh->nexthops, NULL, UINT32_TO_PTR(nexthop->id)); + if (r < 0) + log_debug_errno(r, "Failed to save nexthop ID (%"PRIu32") to group member (%"PRIu32"), ignoring: %m", + nexthop->id, nhg->id); + } +} + static NextHop* nexthop_detach_impl(NextHop *nexthop) { assert(nexthop); assert(!nexthop->manager || !nexthop->network); @@ -31,6 +73,9 @@ static NextHop* nexthop_detach_impl(NextHop *nexthop) { if (nexthop->manager) { assert(nexthop->id > 0); + + nexthop_detach_from_group_members(nexthop); + hashmap_remove(nexthop->manager->nexthops_by_id, UINT32_TO_PTR(nexthop->id)); nexthop->manager = NULL; return nexthop; @@ -51,6 +96,7 @@ static NextHop* nexthop_free(NextHop *nexthop) { config_section_free(nexthop->section); hashmap_free_free(nexthop->group); + set_free(nexthop->nexthops); return mfree(nexthop); } @@ -874,6 +920,8 @@ static int nexthop_update_group(NextHop *nexthop, sd_netlink_message *message) { if (r < 0 && r != -ENODATA) return log_debug_errno(r, "rtnl: could not get NHA_GROUP attribute, ignoring: %m"); + nexthop_detach_from_group_members(nexthop); + if (size % sizeof(struct nexthop_grp) != 0) return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "rtnl: received nexthop message with invalid nexthop group size, ignoring."); @@ -913,6 +961,8 @@ static int nexthop_update_group(NextHop *nexthop, sd_netlink_message *message) { hashmap_free_free(nexthop->group); nexthop->group = TAKE_PTR(h); + + nexthop_attach_to_group_members(nexthop); return 0; } diff --git a/src/network/networkd-nexthop.h b/src/network/networkd-nexthop.h index 18b30e28be8..74b23bd7720 100644 --- a/src/network/networkd-nexthop.h +++ b/src/network/networkd-nexthop.h @@ -40,6 +40,9 @@ typedef struct NextHop { /* Only used in conf parser and nexthop_section_verify(). */ int onlink; + + /* For managing nexthops that depend on this nexthop. */ + Set *nexthops; } NextHop; NextHop* nexthop_ref(NextHop *nexthop);