From: Yu Watanabe Date: Wed, 15 Sep 2021 10:57:38 +0000 (+0900) Subject: udev: read more attributes through netlink and cache them X-Git-Tag: v250-rc1~539^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1321f675e778c6f36d75054c9fd1b4c921bab222;p=thirdparty%2Fsystemd.git udev: read more attributes through netlink and cache them --- diff --git a/src/udev/test-udev-netlink.c b/src/udev/test-udev-netlink.c index bda713ecdc3..d177b095677 100644 --- a/src/udev/test-udev-netlink.c +++ b/src/udev/test-udev-netlink.c @@ -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)); } diff --git a/src/udev/udev-builtin-net_id.c b/src/udev/udev-builtin-net_id.c index a3cbbe50d57..7c404d783c5 100644 --- a/src/udev/udev-builtin-net_id.c +++ b/src/udev/udev-builtin-net_id.c @@ -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); diff --git a/src/udev/udev-netlink.c b/src/udev/udev-netlink.c index d9afdabb150..3c41f159634 100644 --- a/src/udev/udev-netlink.c +++ b/src/udev/udev-netlink.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "device-private.h" +#include "hexdecoct.h" #include "netlink-util.h" #include "strv.h" #include "udev-netlink.h" @@ -10,13 +11,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,30 +63,58 @@ 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; - 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_PORT_ID, + &info.phys_port_id_len, (void**) &info.phys_port_id); + if (r < 0 && r != -ENODATA) + return r; + + 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; @@ -132,6 +164,39 @@ static int cache_hw_addr(sd_device *device, const char *attr, const struct hw_ad 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) != -ESTALE) + return 0; + + if (data) { + size_t j = 0; + + str = new(char, len * 2 + 1); + if (!str) + return -ENOMEM; + + for (size_t i = 0; i < len; i++) { + str[j++] = hexchar(data[i] >> 4); + str[j++] = hexchar(data[i] & 0x0f); + } + + str[j] = '\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; @@ -173,15 +238,51 @@ int device_cache_sysattr_from_link_info(sd_device *device, LinkInfo *info) { if (r < 0) return r; + r = cache_unsigned(device, "addr_len", info->hw_addr.length); + if (r < 0) + return r; + r = cache_hw_addr(device, "address", &info->hw_addr); if (r < 0) return r; + r = cache_hw_addr(device, "broadcast", &info->broadcast); + if (r < 0) + return r; + + r = cache_unsigned(device, "mtu", info->mtu); + if (r < 0) + return r; + r = cache_unsigned(device, "iflink", info->iflink); if (r < 0) return r; - if (info->support_phys_port_name) { + r = cache_unsigned(device, "link_mode", info->link_mode); + if (r < 0) + return r; + + r = cache_string(device, "ifalias", strempty(info->ifalias)); + if (r < 0) + return r; + + r = cache_unsigned(device, "netdev_group", info->group); + if (r < 0) + return r; + + 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; + } + + 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; + } + + if (info->phys_port_name_supported) { r = cache_string(device, "phys_port_name", info->phys_port_name); if (r < 0) return r; @@ -206,7 +307,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); diff --git a/src/udev/udev-netlink.h b/src/udev/udev-netlink.h index a6ea0d5fe21..a82352dd61e 100644 --- a/src/udev/udev-netlink.h +++ b/src/udev/udev-netlink.h @@ -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) {})