]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network: also manage nexthops by ID
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 5 Feb 2021 03:01:07 +0000 (12:01 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 19 Feb 2021 13:42:39 +0000 (22:42 +0900)
It will be used in later commits.

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

index a6f343a1f63709d9fa3bb481d6c3f3b5a444ae08..8f8aff602a0c0851fd3a97812f3008b6bf40f549 100644 (file)
@@ -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);
 
index 8b5f8a9dc0fce0e944741cf4a58a30e368fc7520..3075b8f70761e072d16e95251f704f23b31c211d 100644 (file)
@@ -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;
index 028aaad313aefe7876a021c573eaa46ecb1dd177..9f94b64107224ad41007a75e1ad0dbc353a6c6fb 100644 (file)
@@ -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) {