From 69e244e321e4c51ebd266555a5ec286b117ad5c6 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 5 Feb 2021 12:01:07 +0900 Subject: [PATCH] network: also manage nexthops by ID It will be used in later commits. --- src/network/networkd-manager.c | 2 ++ src/network/networkd-manager.h | 3 +++ src/network/networkd-nexthop.c | 16 ++++++++++++---- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c index a6f343a1f63..8f8aff602a0 100644 --- a/src/network/networkd-manager.c +++ b/src/network/networkd-manager.c @@ -894,6 +894,8 @@ Manager* manager_free(Manager *m) { m->routes = set_free(m->routes); m->routes_foreign = set_free(m->routes_foreign); + m->nexthops_by_id = hashmap_free(m->nexthops_by_id); + sd_event_source_unref(m->speed_meter_event_source); sd_event_unref(m->event); diff --git a/src/network/networkd-manager.h b/src/network/networkd-manager.h index 8b5f8a9dc0f..3075b8f7076 100644 --- a/src/network/networkd-manager.h +++ b/src/network/networkd-manager.h @@ -61,6 +61,9 @@ struct Manager { Set *rules; Set *rules_foreign; + /* Manage nexthops by id. */ + Hashmap *nexthops_by_id; + /* Manager stores routes without RTA_OIF attribute. */ Set *routes; Set *routes_foreign; diff --git a/src/network/networkd-nexthop.c b/src/network/networkd-nexthop.c index 028aaad313a..9f94b641072 100644 --- a/src/network/networkd-nexthop.c +++ b/src/network/networkd-nexthop.c @@ -28,6 +28,9 @@ NextHop *nexthop_free(NextHop *nexthop) { if (nexthop->link) { set_remove(nexthop->link->nexthops, nexthop); set_remove(nexthop->link->nexthops_foreign, nexthop); + + if (nexthop->link->manager && nexthop->id > 0) + hashmap_remove(nexthop->link->manager->nexthops_by_id, UINT32_TO_PTR(nexthop->id)); } return mfree(nexthop); @@ -221,14 +224,18 @@ static int nexthop_update(Link *link, NextHop *nexthop, const NextHop *in) { int r; assert(link); + assert(link->manager); assert(nexthop); assert(in); assert(in->id > 0); - /* Currently, this only updates ID. */ + /* This updates nexthop ID if necessary, and register the nexthop to Manager. */ - if (nexthop->id > 0) - return nexthop->id == in->id ? 0 : -EINVAL; + if (nexthop->id > 0) { + if (nexthop->id == in->id) + goto set_manager; + return -EINVAL; + } nexthop = set_remove(link->nexthops, nexthop); if (!nexthop) @@ -251,7 +258,8 @@ static int nexthop_update(Link *link, NextHop *nexthop, const NextHop *in) { return r < 0 ? r : -EEXIST; } - return 0; +set_manager: + return hashmap_ensure_put(&link->manager->nexthops_by_id, NULL, UINT32_TO_PTR(nexthop->id), nexthop); } static void log_nexthop_debug(const NextHop *nexthop, uint32_t id, const char *str, const Link *link) {