From: Michael Tremer Date: Fri, 9 Jun 2023 14:44:06 +0000 (+0000) Subject: link: Add device stuff to JSON output X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=cf75e1dbc997b8a785e5f4d75e6a3d0efd594cf5;p=network.git link: Add device stuff to JSON output Signed-off-by: Michael Tremer --- diff --git a/src/networkd/link.c b/src/networkd/link.c index 60abd466..23918cba 100644 --- a/src/networkd/link.c +++ b/src/networkd/link.c @@ -24,6 +24,7 @@ #include #include +#include #include #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; }