]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udev: read more attributes through netlink and cache them 20771/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 15 Sep 2021 10:57:38 +0000 (19:57 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 17 Sep 2021 05:19:36 +0000 (14:19 +0900)
src/udev/test-udev-netlink.c
src/udev/udev-builtin-net_id.c
src/udev/udev-netlink.c
src/udev/udev-netlink.h

index bda713ecdc38f2e6fdeb8fbdce3eb2f8521f4ac5..d177b095677737ea474b6d2aab93189829a43d0f 100644 (file)
@@ -11,8 +11,8 @@
 static void test_link_info_one(sd_netlink *rtnl, int ifindex) {
         _cleanup_(link_info_clear) LinkInfo info = LINK_INFO_NULL;
         _cleanup_(sd_device_unrefp) sd_device *dev = NULL, *dev_with_netlink = NULL;
-        unsigned iftype, iflink;
         const char *s, *t;
+        unsigned u;
 
         log_debug("/* %s(ifindex=%i) */", __func__, ifindex);
 
@@ -23,11 +23,20 @@ static void test_link_info_one(sd_netlink *rtnl, int ifindex) {
         /* check iftype */
         log_debug("iftype: %"PRIu16" (%s)", info.iftype, strna(arphrd_to_name(info.iftype)));
         assert_se(sd_device_get_sysattr_value(dev, "type", &s) >= 0);
-        assert_se(safe_atou(s, &iftype) >= 0);
-        assert_se(iftype == info.iftype);
+        assert_se(safe_atou(s, &u) >= 0);
+        assert_se(u == info.iftype);
         assert_se(device_get_sysattr_value_maybe_from_netlink(dev_with_netlink, &rtnl, "type", &s) >= 0);
-        assert_se(safe_atou(s, &iftype) >= 0);
-        assert_se(iftype == info.iftype);
+        assert_se(safe_atou(s, &u) >= 0);
+        assert_se(u == info.iftype);
+
+        /* check hardware address length */
+        log_debug("hardware address length: %zu", info.hw_addr.length);
+        assert_se(sd_device_get_sysattr_value(dev, "addr_len", &s) >= 0);
+        assert_se(safe_atou(s, &u) >= 0);
+        assert_se(u == info.hw_addr.length);
+        assert_se(device_get_sysattr_value_maybe_from_netlink(dev_with_netlink, &rtnl, "addr_len", &s) >= 0);
+        assert_se(safe_atou(s, &u) >= 0);
+        assert_se(u == info.hw_addr.length);
 
         /* check hardware address */
         log_debug("hardware address: %s", HW_ADDR_TO_STR(&info.hw_addr));
@@ -36,6 +45,13 @@ static void test_link_info_one(sd_netlink *rtnl, int ifindex) {
         assert_se(device_get_sysattr_value_maybe_from_netlink(dev_with_netlink, &rtnl, "address", &s) >= 0);
         assert_se(streq(s, HW_ADDR_TO_STR(&info.hw_addr)));
 
+        /* check broadcast address */
+        log_debug("broadcast address: %s", HW_ADDR_TO_STR(&info.broadcast));
+        assert_se(sd_device_get_sysattr_value(dev, "broadcast", &s) >= 0);
+        assert_se(streq(s, HW_ADDR_TO_STR(&info.broadcast)));
+        assert_se(device_get_sysattr_value_maybe_from_netlink(dev_with_netlink, &rtnl, "broadcast", &s) >= 0);
+        assert_se(streq(s, HW_ADDR_TO_STR(&info.broadcast)));
+
         /* check ifname */
         log_debug("ifname: %s", info.ifname);
         assert_se(sd_device_get_sysname(dev, &s) >= 0);
@@ -43,24 +59,74 @@ static void test_link_info_one(sd_netlink *rtnl, int ifindex) {
         assert_se(sd_device_get_sysname(dev_with_netlink, &s) >= 0);
         assert_se(streq(s, info.ifname));
 
+        /* check mtu */
+        log_debug("mtu: %"PRIu32, info.mtu);
+        assert_se(sd_device_get_sysattr_value(dev, "mtu", &s) >= 0);
+        assert_se(safe_atou(s, &u) >= 0);
+        assert_se(u == info.mtu);
+        assert_se(device_get_sysattr_value_maybe_from_netlink(dev_with_netlink, &rtnl, "mtu", &s) >= 0);
+        assert_se(safe_atou(s, &u) >= 0);
+        assert_se(u == info.mtu);
+
         /* check iflink */
         log_debug("iflink: %"PRIu32, info.iflink);
         assert_se(sd_device_get_sysattr_value(dev, "iflink", &s) >= 0);
-        assert_se(safe_atou(s, &iflink) >= 0);
-        assert_se(iflink == info.iflink);
+        assert_se(safe_atou(s, &u) >= 0);
+        assert_se(u == info.iflink);
         assert_se(device_get_sysattr_value_maybe_from_netlink(dev_with_netlink, &rtnl, "iflink", &s) >= 0);
-        assert_se(safe_atou(s, &iflink) >= 0);
-        assert_se(iflink == info.iflink);
+        assert_se(safe_atou(s, &u) >= 0);
+        assert_se(u == info.iflink);
+
+        /* check link_mode */
+        log_debug("link_mode: %"PRIu8, info.link_mode);
+        assert_se(sd_device_get_sysattr_value(dev, "link_mode", &s) >= 0);
+        assert_se(safe_atou(s, &u) >= 0);
+        assert_se(u == info.link_mode);
+        assert_se(device_get_sysattr_value_maybe_from_netlink(dev_with_netlink, &rtnl, "link_mode", &s) >= 0);
+        assert_se(safe_atou(s, &u) >= 0);
+        assert_se(u == info.link_mode);
+
+        /* check ifalias */
+        log_debug("ifalias: %s", strna(info.ifalias));
+        assert_se(sd_device_get_sysattr_value(dev, "ifalias", &s) >= 0);
+        assert_se(streq(s, strempty(info.ifalias)));
+        assert_se(device_get_sysattr_value_maybe_from_netlink(dev_with_netlink, &rtnl, "ifalias", &s) >= 0);
+        assert_se(streq(s, strempty(info.ifalias)));
+
+        /* check netdev_group */
+        log_debug("netdev_group: %"PRIu32, info.group);
+        assert_se(sd_device_get_sysattr_value(dev, "netdev_group", &s) >= 0);
+        assert_se(safe_atou(s, &u) >= 0);
+        assert_se(u == info.group);
+        assert_se(device_get_sysattr_value_maybe_from_netlink(dev_with_netlink, &rtnl, "netdev_group", &s) >= 0);
+        assert_se(safe_atou(s, &u) >= 0);
+        assert_se(u == info.group);
+
+        /* check phys_port_id */
+        log_debug("phys_port_id: (%s)",
+                  info.phys_port_id_supported ? "supported" : "unsupported");
+        s = t = NULL;
+        (void) sd_device_get_sysattr_value(dev, "phys_port_id", &s);
+        (void) device_get_sysattr_value_maybe_from_netlink(dev_with_netlink, &rtnl, "phys_port_id", &t);
+        assert_se(streq_ptr(s, t));
+
+        /* check phys_switch_id */
+        log_debug("phys_switch_id: (%s)",
+                  info.phys_switch_id_supported ? "supported" : "unsupported");
+        s = t = NULL;
+        (void) sd_device_get_sysattr_value(dev, "phys_switch_id", &s);
+        (void) device_get_sysattr_value_maybe_from_netlink(dev_with_netlink, &rtnl, "phys_switch_id", &t);
+        assert_se(streq_ptr(s, t));
 
         /* check phys_port_name */
         log_debug("phys_port_name: %s (%s)",
                   strna(info.phys_port_name),
-                  info.support_phys_port_name ? "supported" : "unsupported");
+                  info.phys_port_name_supported ? "supported" : "unsupported");
         s = t = NULL;
         (void) sd_device_get_sysattr_value(dev, "phys_port_name", &s);
         (void) device_get_sysattr_value_maybe_from_netlink(dev_with_netlink, &rtnl, "phys_port_name", &t);
         assert_se(streq_ptr(s, t));
-        if (info.support_phys_port_name) {
+        if (info.phys_port_name_supported) {
                 assert_se(streq_ptr(s, info.phys_port_name));
                 assert_se(streq_ptr(t, info.phys_port_name));
         }
index eaa7060fe9ba6df99c4c6e9fcb4e4dd9f87b2a17..bbf84dea020cc475c8d0633e744c947bf9ac293e 100644 (file)
@@ -853,7 +853,7 @@ static int builtin_net_id(sd_device *dev, sd_netlink **rtnl, int argc, char *arg
         if (r < 0)
                 return r;
 
-        if (!info.support_phys_port_name) {
+        if (!info.phys_port_name_supported) {
                 const char *s;
 
                 r = sd_device_get_sysattr_value(dev, "phys_port_name", &s);
index 0e78eed485d42e5bd969d2661bcc56e1a2a52905..4ab36786f3477369f1301f78913bf0e581f48251 100644 (file)
@@ -10,13 +10,16 @@ void link_info_clear(LinkInfo *info) {
                 return;
 
         info->ifname = mfree(info->ifname);
+        info->ifalias = mfree(info->ifalias);
+        info->phys_port_id = mfree(info->phys_port_id);
+        info->phys_switch_id = mfree(info->phys_switch_id);
         info->phys_port_name = mfree(info->phys_port_name);
 }
 
 int link_info_get(sd_netlink **rtnl, int ifindex, LinkInfo *ret) {
         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL, *reply = NULL;
         _cleanup_(link_info_clear) LinkInfo info = LINK_INFO_NULL;
-        uint16_t nlmsg_type;
+        uint16_t nlmsg_type, max_attr;
         int r;
 
         assert(rtnl);
@@ -59,35 +62,160 @@ int link_info_get(sd_netlink **rtnl, int ifindex, LinkInfo *ret) {
         if (r < 0 && r != -ENODATA)
                 return r;
 
+        r = netlink_message_read_hw_addr(reply, IFLA_BROADCAST, &info.broadcast);
+        if (r < 0 && r != -ENODATA)
+                return r;
+
         r = sd_netlink_message_read_string_strdup(reply, IFLA_IFNAME, &info.ifname);
         if (r < 0)
                 return r;
 
+        r = sd_netlink_message_read_u32(reply, IFLA_MTU, &info.mtu);
+        if (r < 0)
+                return r;
+
         r = sd_netlink_message_read_u32(reply, IFLA_LINK, &info.iflink);
         if (r == -ENODATA)
                 info.iflink = info.ifindex;
         else if (r < 0)
                 return r;
 
-        r = sd_netlink_message_read_string_strdup(reply, IFLA_PHYS_PORT_NAME, &info.phys_port_name);
-        if (r == -ENODATA) {
-                uint16_t max_attr;
+        r = sd_netlink_message_read_u8(reply, IFLA_LINKMODE, &info.link_mode);
+        if (r < 0)
+                return r;
 
-                r = sd_netlink_message_get_max_attribute(reply, &max_attr);
-                if (r < 0)
-                        return r;
+        r = sd_netlink_message_read_string_strdup(reply, IFLA_IFALIAS, &info.ifalias);
+        if (r < 0 && r != -ENODATA)
+                return r;
+
+        r = sd_netlink_message_read_u32(reply, IFLA_GROUP, &info.group);
+        if (r < 0)
+                return r;
+
+        r = sd_netlink_message_read_data(reply, IFLA_PHYS_PORT_ID,
+                                         &info.phys_port_id_len, (void**) &info.phys_port_id);
+        if (r < 0 && r != -ENODATA)
+                return r;
 
-                info.support_phys_port_name = max_attr >= IFLA_PHYS_PORT_NAME;
-        } else if (r >= 0)
-                info.support_phys_port_name = true;
-        else
+        r = sd_netlink_message_read_data(reply, IFLA_PHYS_SWITCH_ID,
+                                         &info.phys_switch_id_len, (void**) &info.phys_switch_id);
+        if (r < 0 && r != -ENODATA)
                 return r;
 
+        r = sd_netlink_message_read_string_strdup(reply, IFLA_PHYS_PORT_NAME, &info.phys_port_name);
+        if (r < 0 && r != -ENODATA)
+                return r;
+
+        r = sd_netlink_message_get_max_attribute(reply, &max_attr);
+        if (r < 0)
+                return r;
+
+        info.phys_port_id_supported = max_attr >= IFLA_PHYS_PORT_ID;
+        info.phys_switch_id_supported = max_attr >= IFLA_PHYS_SWITCH_ID;
+        info.phys_port_name_supported = max_attr >= IFLA_PHYS_PORT_NAME;
+
         *ret = info;
         info = LINK_INFO_NULL;
         return 0;
 }
 
+static int cache_unsigned(sd_device *device, const char *attr, uint64_t val) {
+        _cleanup_free_ char *str = NULL;
+        int r;
+
+        assert(device);
+        assert(attr);
+
+        if (device_get_cached_sysattr_value(device, attr, NULL) != -ENODATA)
+                return 0;
+
+        if (asprintf(&str, "%"PRIu64, val) < 0)
+                return -ENOMEM;
+
+        r = device_cache_sysattr_value(device, attr, str);
+        if (r < 0)
+                return r;
+
+        TAKE_PTR(str);
+        return 0;
+}
+
+static int cache_hw_addr(sd_device *device, const char *attr, const struct hw_addr_data *hw_addr) {
+        _cleanup_free_ char *str = NULL;
+        int r;
+
+        assert(device);
+        assert(attr);
+        assert(hw_addr);
+
+        if (device_get_cached_sysattr_value(device, attr, NULL) != -ENODATA)
+                return 0;
+
+        str = new(char, HW_ADDR_TO_STRING_MAX);
+        if (!str)
+                return -ENOMEM;
+
+        r = device_cache_sysattr_value(device, attr, hw_addr_to_string(hw_addr, str));
+        if (r < 0)
+                return r;
+
+        TAKE_PTR(str);
+        return 0;
+}
+
+static int cache_binary(sd_device *device, const char *attr, size_t len, const uint8_t *data) {
+        _cleanup_free_ char *str = NULL;
+        int r;
+
+        assert(device);
+        assert(attr);
+
+        if (device_get_cached_sysattr_value(device, attr, NULL) != -ENODATA)
+                return 0;
+
+        if (data) {
+                str = new(char, len * 2 + 1);
+                if (!str)
+                        return -ENOMEM;
+
+                for (size_t i = 0; i < len; i++)
+                        sprintf(&str[i * 2], "%02"PRIx8, data[i]);
+
+                str[len * 2] = '\0';
+        }
+
+        r = device_cache_sysattr_value(device, attr, str);
+        if (r < 0)
+                return r;
+
+        TAKE_PTR(str);
+        return 0;
+}
+
+static int cache_string(sd_device *device, const char *attr, const char *val) {
+        _cleanup_free_ char *str = NULL;
+        int r;
+
+        assert(device);
+        assert(attr);
+
+        if (device_get_cached_sysattr_value(device, attr, NULL) != -ENODATA)
+                return 0;
+
+        if (val) {
+                str = strdup(val);
+                if (!str)
+                        return -ENOMEM;
+        }
+
+        r = device_cache_sysattr_value(device, attr, str);
+        if (r < 0)
+                return r;
+
+        TAKE_PTR(str);
+        return 0;
+}
+
 int device_cache_sysattr_from_link_info(sd_device *device, LinkInfo *info) {
         int ifindex, r;
 
@@ -101,61 +229,58 @@ int device_cache_sysattr_from_link_info(sd_device *device, LinkInfo *info) {
         if (ifindex != info->ifindex)
                 return -EINVAL;
 
-        if (device_get_cached_sysattr_value(device, "type", NULL) == -ENODATA) {
-                _cleanup_free_ char *str = NULL;
-
-                if (asprintf(&str, "%"PRIu16, info->iftype) < 0)
-                        return -ENOMEM;
+        r = cache_unsigned(device, "type", info->iftype);
+        if (r < 0)
+                return r;
 
-                r = device_cache_sysattr_value(device, "type", str);
-                if (r < 0)
-                        return r;
+        r = cache_unsigned(device, "addr_len", info->hw_addr.length);
+        if (r < 0)
+                return r;
 
-                TAKE_PTR(str);
-        }
+        r = cache_hw_addr(device, "address", &info->hw_addr);
+        if (r < 0)
+                return r;
 
-        if (device_get_cached_sysattr_value(device, "address", NULL) == -ENODATA) {
-                _cleanup_free_ char *str = NULL;
+        r = cache_hw_addr(device, "broadcast", &info->broadcast);
+        if (r < 0)
+                return r;
 
-                str = new(char, HW_ADDR_TO_STRING_MAX);
-                if (!str)
-                        return -ENOMEM;
+        r = cache_unsigned(device, "mtu", info->mtu);
+        if (r < 0)
+                return r;
 
-                r = device_cache_sysattr_value(device, "address", hw_addr_to_string(&info->hw_addr, str));
-                if (r < 0)
-                        return r;
+        r = cache_unsigned(device, "iflink", info->iflink);
+        if (r < 0)
+                return r;
 
-                TAKE_PTR(str);
-        }
+        r = cache_unsigned(device, "link_mode", info->link_mode);
+        if (r < 0)
+                return r;
 
-        if (device_get_cached_sysattr_value(device, "iflink", NULL) == -ENODATA) {
-                _cleanup_free_ char *str = NULL;
+        r = cache_string(device, "ifalias", strempty(info->ifalias));
+        if (r < 0)
+                return r;
 
-                if (asprintf(&str, "%"PRIu32, info->iflink) < 0)
-                        return -ENOMEM;
+        r = cache_unsigned(device, "netdev_group", info->group);
+        if (r < 0)
+                return r;
 
-                r = device_cache_sysattr_value(device, "iflink", str);
+        if (info->phys_port_id_supported) {
+                r = cache_binary(device, "phys_port_id", info->phys_port_id_len, info->phys_port_id);
                 if (r < 0)
                         return r;
-
-                TAKE_PTR(str);
         }
 
-        if (info->support_phys_port_name &&
-            device_get_cached_sysattr_value(device, "phys_port_name", NULL) == -ENODATA) {
-                _cleanup_free_ char *str = NULL;
-
-                if (info->phys_port_name) {
-                        str = strdup(info->phys_port_name);
-                        if (!str)
-                                return -ENOMEM;
-                }
-
-                r = device_cache_sysattr_value(device, "phys_port_name", str);
+        if (info->phys_switch_id_supported) {
+                r = cache_binary(device, "phys_switch_id", info->phys_switch_id_len, info->phys_switch_id);
                 if (r < 0)
                         return r;
+        }
 
-                TAKE_PTR(str);
+        if (info->phys_port_name_supported) {
+                r = cache_string(device, "phys_port_name", info->phys_port_name);
+                if (r < 0)
+                        return r;
         }
 
         return 0;
@@ -177,7 +302,9 @@ int device_get_sysattr_value_maybe_from_netlink(
         if (sd_device_get_ifindex(device, &ifindex) < 0)
                 return sd_device_get_sysattr_value(device, sysattr, ret_value);
 
-        if (!STR_IN_SET(sysattr, "type", "address", "iflink", "phys_port_name"))
+        if (!STR_IN_SET(sysattr,
+                        "type", "addr_len", "address", "broadcast", "mtu", "iflink", "linkmode",
+                        "ifalias", "group", "phys_port_id", "phys_switch_id", "phys_port_name"))
                 return sd_device_get_sysattr_value(device, sysattr, ret_value);
 
         r = device_get_cached_sysattr_value(device, sysattr, ret_value);
index a6ea0d5fe21758eed95105745eb6f5e64829ff3b..a82352dd61ed12b3cf7a9f7acb676a65969ac6e4 100644 (file)
@@ -8,14 +8,25 @@
 
 typedef struct LinkInfo {
         int ifindex;
-        uint16_t iftype;             /* ARPHRD_* */
+        uint16_t iftype;             /* ARPHRD_* (type) */
 
-        struct hw_addr_data hw_addr; /* IFLA_ADDRESS */
-        char *ifname;                /* IFLA_IFNAME */
-        uint32_t iflink;             /* IFLA_LINK */
-        char *phys_port_name;        /* IFLA_PHYS_PORT_NAME */
+        struct hw_addr_data hw_addr;   /* IFLA_ADDRESS (address, addr_len) */
+        struct hw_addr_data broadcast; /* IFLA_BROADCAST (broadcast) */
+        char *ifname;                  /* IFLA_IFNAME */
+        uint32_t mtu;                  /* IFLA_MTU (mtu) */
+        uint32_t iflink;               /* IFLA_LINK (iflink) */
+        uint8_t link_mode;             /* IFLA_LINKMODE (link_mode) */
+        char *ifalias;                 /* IFLA_IFALIAS (ifalias) */
+        uint32_t group;                /* IFLA_GROUP (netdev_group) */
+        uint8_t *phys_port_id;         /* IFLA_PHYS_PORT_ID (phys_port_id) */
+        size_t phys_port_id_len;
+        uint8_t *phys_switch_id;       /* IFLA_PHYS_SWITCH_ID (phys_switch_id) */
+        size_t phys_switch_id_len;
+        char *phys_port_name;          /* IFLA_PHYS_PORT_NAME (phys_port_name) */
 
-        bool support_phys_port_name;
+        bool phys_port_id_supported;
+        bool phys_switch_id_supported;
+        bool phys_port_name_supported;
 } LinkInfo;
 
 #define LINK_INFO_NULL ((LinkInfo) {})