]> git.ipfire.org Git - people/ms/network.git/commitdiff
networkd: Enumerate all links on startup
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 9 Feb 2023 20:41:02 +0000 (20:41 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 9 Feb 2023 20:41:02 +0000 (20:41 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/daemon.c
src/networkd/daemon.h
src/networkd/link.c
src/networkd/link.h
src/networkd/links.c

index 27b118e9f197b9fd0716adf400cef2402d8d532b..88ce8e71929c2f6aefdea09071c95bf10f51594d 100644 (file)
@@ -31,6 +31,7 @@
 #include "config.h"
 #include "daemon.h"
 #include "devmon.h"
+#include "link.h"
 #include "links.h"
 #include "logging.h"
 #include "zone.h"
@@ -193,12 +194,6 @@ static int nw_start_device_monitor(struct nw_daemon* daemon) {
        return 0;
 }
 
-static int _rtnl_dummy(sd_netlink* rtnl, sd_netlink_message* message, void* data) {
-       DEBUG("_rtnl_dummy called\n");
-
-       return 0;
-}
-
 static int nw_daemon_connect_rtnl(struct nw_daemon* daemon, int fd) {
        int r;
 
@@ -224,7 +219,7 @@ static int nw_daemon_connect_rtnl(struct nw_daemon* daemon, int fd) {
        }
 
        // Register callback for new interfaces
-       r = sd_netlink_add_match(daemon->rtnl, NULL, RTM_NEWLINK, _rtnl_dummy, NULL,
+       r = sd_netlink_add_match(daemon->rtnl, NULL, RTM_NEWLINK, nw_link_process, NULL,
                        daemon, "networkd-RTM_NEWLINK");
        if (r < 0) {
                ERROR("Could not register RTM_NEWLINK: %m\n");
@@ -394,6 +389,13 @@ int nw_daemon_reload(struct nw_daemon* daemon) {
        return 0;
 }
 
+/*
+       Netlink
+*/
+sd_netlink* nw_daemon_get_rtnl(struct nw_daemon* daemon) {
+       return daemon->rtnl;
+}
+
 /*
        Zones
 */
index 891c297ab1bce6c815b5e990dd378e0cdd6a937d..543685f5b7c40d337aff80aa461bf0254d4c2c11 100644 (file)
@@ -21,6 +21,8 @@
 #ifndef NETWORKD_DAEMON_H
 #define NETWORKD_DAEMON_H
 
+#include <systemd/sd-netlink.h>
+
 #include "zone.h"
 
 struct nw_daemon;
@@ -34,6 +36,11 @@ int nw_daemon_run(struct nw_daemon* daemon);
 
 int nw_daemon_reload(struct nw_daemon* daemon);
 
+/*
+       Netlink
+*/
+sd_netlink* nw_daemon_get_rtnl(struct nw_daemon* daemon);
+
 /*
        Zones
 */
index 06e31017a065ee2a78a51155b8042ce3af2c8581..ec0593b0ba766b364800c9c5785e99bc96b7566a 100644 (file)
@@ -23,6 +23,7 @@
 
 #include "daemon.h"
 #include "link.h"
+#include "logging.h"
 
 struct nw_link {
        struct nw_daemon* daemon;
@@ -70,3 +71,9 @@ struct nw_link* nw_link_unref(struct nw_link* link) {
        nw_link_free(link);
        return NULL;
 }
+
+int nw_link_process(sd_netlink* rtnl, sd_netlink_message* message, void* data) {
+       DEBUG("nw_link_process called\n");
+
+       return 0;
+}
index 6ffe843ce2307c552fd8ae606b28ac53fd86f6c3..6003133d5119b610dc29d428e8352c657c7c6317 100644 (file)
@@ -30,4 +30,6 @@ int nw_link_create(struct nw_link** link, struct nw_daemon* daemon, int ifindex)
 struct nw_link* nw_link_ref(struct nw_link* link);
 struct nw_link* nw_link_unref(struct nw_link* link);
 
+int nw_link_process(sd_netlink* rtnl, sd_netlink_message* message, void* data);
+
 #endif /* NETWORKD_LINK_H */
index b81d6a59d9a883738671bd6325291952f1b3cde7..22584f2f632c3dfe4fcf328da6db4b17a406b930 100644 (file)
@@ -99,7 +99,42 @@ struct nw_links* nw_links_unref(struct nw_links* links) {
 }
 
 int nw_links_enumerate(struct nw_links* links) {
-       // TODO
+       sd_netlink_message* req = NULL;
+       sd_netlink_message* res = NULL;
+       int r;
 
-       return 0;
+       sd_netlink* rtnl = nw_daemon_get_rtnl(links->daemon);
+       if (!rtnl)
+               return 1;
+
+       r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, 0);
+       if (r < 0)
+               return 1;
+
+       r = sd_netlink_message_set_request_dump(req, 1);
+       if (r < 0)
+               return 1;
+
+       r = sd_netlink_call(rtnl, req, 0, &res);
+       if (r < 0)
+               return 1;
+
+       sd_netlink_message* m = res;
+
+       // Iterate through all replies
+       do {
+               r = nw_link_process(rtnl, m, links->daemon);
+               if (r)
+                       goto ERROR;
+       } while ((m = sd_netlink_message_next(m)));
+
+ERROR:
+       if (m)
+               sd_netlink_message_unref(m);
+       if (req)
+               sd_netlink_message_unref(req);
+       if (res)
+               sd_netlink_message_unref(res);
+
+       return r;
 }