]> git.ipfire.org Git - people/ms/network.git/commitdiff
link: Add device stuff to JSON output
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 9 Jun 2023 14:44:06 +0000 (14:44 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 9 Jun 2023 14:44:06 +0000 (14:44 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/link.c

index 60abd466dd5c7edb2472fc14c1f2517a813d6961..23918cba8dfdccd60476c5831f0bc063db096e89 100644 (file)
@@ -24,6 +24,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include <systemd/sd-device.h>
 #include <systemd/sd-netlink.h>
 
 #include "daemon.h"
@@ -48,6 +49,9 @@ struct nw_link {
                NW_LINK_DESTROYED,
        } state;
 
+       // Device
+       struct sd_device* device;
+
        // Stats
        struct rtnl_link_stats64 stats64;
 
@@ -61,11 +65,35 @@ struct nw_link {
        uint8_t operstate;
 };
 
+static int nw_link_setup_device(nw_link* link) {
+       int r;
+
+       // Fetch sd-device
+       r = sd_device_new_from_ifindex(&link->device, link->ifindex);
+       if (r < 0) {
+               ERROR("Could not fetch sd-device for link %d: %s\n", link->ifindex, strerror(-r));
+               return r;
+       }
+
+       return 0;
+}
+
+static void nw_link_free(nw_link* link) {
+       DEBUG("Freeing link (ifindex = %d)\n", link->ifindex);
+
+       if (link->device)
+               sd_device_unref(link->device);
+       if (link->daemon)
+               nw_daemon_unref(link->daemon);
+}
+
 int nw_link_create(nw_link** link, nw_daemon* daemon, int ifindex) {
+       int r;
+
        // Allocate a new object
        nw_link* l = calloc(1, sizeof(*l));
        if (!l)
-               return 1;
+               return -errno;
 
        // Store a reference to the daemon
        l->daemon = nw_daemon_ref(daemon);
@@ -78,16 +106,16 @@ int nw_link_create(nw_link** link, nw_daemon* daemon, int ifindex) {
 
        DEBUG("New link allocated (ifindex = %d)\n", l->ifindex);
 
-       *link = l;
+       r = nw_link_setup_device(l);
+       if (r < 0)
+               goto ERROR;
 
+       *link = l;
        return 0;
-}
-
-static void nw_link_free(nw_link* link) {
-       DEBUG("Freeing link (ifindex = %d)\n", link->ifindex);
 
-       if (link->daemon)
-               nw_daemon_unref(link->daemon);
+ERROR:
+       nw_link_free(l);
+       return r;
 }
 
 nw_link* nw_link_ref(nw_link* link) {
@@ -126,6 +154,19 @@ const char* nw_link_ifname(nw_link* link) {
        return link->ifname;
 }
 
+static int nw_link_get_sd_device(nw_link* link, struct sd_device** device) {
+       int r;
+
+       // Fetch sd-device
+       r = sd_device_new_from_ifindex(device, link->ifindex);
+       if (r < 0) {
+               ERROR("Could not fetch sd-device for link %d: %s\n", link->ifindex, strerror(-r));
+               return r;
+       }
+
+       return 0;
+}
+
 // Stats
 
 const struct rtnl_link_stats64* nw_link_get_stats64(nw_link* link) {
@@ -533,6 +574,57 @@ ERROR:
 
 // JSON
 
+static int nw_link_device_to_json(nw_link* link, struct json_object* o) {
+       const char* driver = NULL;
+       const char* vendor = NULL;
+       const char* model = NULL;
+       int r;
+
+       // Fetch driver
+       r = sd_device_get_driver(link->device, &driver);
+       if (r < 0 && r != -ENOENT)
+               return r;
+
+       // Add driver
+       if (driver) {
+               r = json_object_add_string(o, "Driver", driver);
+               if (r < 0)
+                       return r;
+       }
+
+       // Fetch vendor
+       r = sd_device_get_property_value(link->device, "ID_VENDOR_FROM_DATABASE", &vendor);
+       if (r < 0) {
+               r = sd_device_get_property_value(link->device, "ID_VENDOR", &vendor);
+               if (r < 0 && r != -ENOENT)
+                       return r;
+       }
+
+       // Add vendor
+       if (vendor) {
+               r = json_object_add_string(o, "Vendor", vendor);
+               if (r < 0)
+                       return r;
+       }
+
+       // Fetch model
+       r = sd_device_get_property_value(link->device, "ID_MODEL_FROM_DATABASE", &model);
+       if (r < 0) {
+               r = sd_device_get_property_value(link->device, "ID_MODEL", &model);
+               if (r < 0 && r != -ENOENT)
+                       return r;
+       }
+
+       // Add model
+       if (model) {
+               r = json_object_add_string(o, "Model", model);
+               if (r < 0)
+                       return r;
+       }
+
+       return 0;
+}
+
 int nw_link_to_json(nw_link* link, struct json_object* o) {
        int r;
 
@@ -541,5 +633,10 @@ int nw_link_to_json(nw_link* link, struct json_object* o) {
        if (r < 0)
                return r;
 
+       // Add sd-device stuff
+       r = nw_link_device_to_json(link, o);
+       if (r < 0)
+               return r;
+
        return 0;
 }