From d096f80b27580fc124e7c0551f73e8432e836a05 Mon Sep 17 00:00:00 2001 From: dramforever Date: Fri, 19 Sep 2025 21:52:00 +0800 Subject: [PATCH 1/2] udev-builtin-net_id: Refactor names_devicetree() to avoid hardcoding Refactor most of the logic in names_devicetree() into a helper names_devicetree_alias_prefix() that takes an alias_prefix instead of hardcoding "ethernet". The return value names_devicetree_alias_prefix() will be used in further commits to allow for alias_prefix fallback. --- src/udev/udev-builtin-net_id.c | 46 +++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/src/udev/udev-builtin-net_id.c b/src/udev/udev-builtin-net_id.c index 36691b1f2d..fd80538638 100644 --- a/src/udev/udev-builtin-net_id.c +++ b/src/udev/udev-builtin-net_id.c @@ -788,20 +788,20 @@ static int names_platform(UdevEvent *event, const char *prefix) { return 0; } -static int names_devicetree(UdevEvent *event, const char *prefix) { +static int names_devicetree_alias_prefix(UdevEvent *event, const char *prefix, const char *alias_prefix) { sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev); _cleanup_(sd_device_unrefp) sd_device *aliases_dev = NULL, *ofnode_dev = NULL, *devicetree_dev = NULL; const char *ofnode_path, *ofnode_syspath, *devicetree_syspath; int r; assert(prefix); + assert(alias_prefix); - if (!naming_scheme_has(NAMING_DEVICETREE_ALIASES)) - return 0; + /* Returns 1 if found, 0 if not found, negative if error */ - /* only ethernet supported for now */ - if (!streq(prefix, "en")) - return -EOPNOTSUPP; + _cleanup_free_ char *alias_prefix_0 = strjoin(alias_prefix, "0"); + if (!alias_prefix_0) + return log_oom_debug(); /* check if the device itself has an of_node */ if (naming_scheme_has(NAMING_DEVICETREE_PORT_ALIASES)) { @@ -860,7 +860,7 @@ static int names_devicetree(UdevEvent *event, const char *prefix) { const char *alias_path, *alias_index, *conflict; unsigned i; - alias_index = startswith(alias, "ethernet"); + alias_index = startswith(alias, alias_prefix); if (!alias_index) continue; @@ -873,31 +873,53 @@ static int names_devicetree(UdevEvent *event, const char *prefix) { /* If there's no index, we default to 0... */ if (isempty(alias_index)) { i = 0; - conflict = "ethernet0"; + conflict = alias_prefix_0; } else { r = safe_atou(alias_index, &i); if (r < 0) return log_device_debug_errno(dev, r, "Could not get index of alias %s: %m", alias); - conflict = "ethernet"; + conflict = alias_prefix; } /* ...but make sure we don't have an alias conflict */ if (i == 0 && device_get_sysattr_value_filtered(aliases_dev, conflict, NULL) >= 0) return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EEXIST), - "Ethernet alias conflict: ethernet and ethernet0 both exist."); + "DeviceTree alias conflict: %s and %s both exist.", + alias_prefix, alias_prefix_0); char str[ALTIFNAMSIZ]; if (snprintf_ok(str, sizeof str, "%sd%u", prefix, i)) udev_builtin_add_property(event, "ID_NET_NAME_ONBOARD", str); log_device_debug(dev, "DeviceTree identifier: alias_index=%u %s \"%s\"", i, glyph(GLYPH_ARROW_RIGHT), str + strlen(prefix)); - return 0; + return 1; } - return -ENOENT; + return 0; } +static int names_devicetree(UdevEvent *event, const char *prefix) { + int r; + + assert(event); + assert(prefix); + + if (!naming_scheme_has(NAMING_DEVICETREE_ALIASES)) + return 0; + + if (streq(prefix, "en")) + r = names_devicetree_alias_prefix(event, prefix, "ethernet"); + else + return -EOPNOTSUPP; /* Unsupported interface type */ + if (r < 0) + return r; + if (r == 0) /* Not found */ + return -ENOENT; + + return 0; /* Found */ +}; + static int names_pci(UdevEvent *event, const char *prefix) { sd_device *parent, *dev = ASSERT_PTR(ASSERT_PTR(event)->dev); _cleanup_(sd_device_unrefp) sd_device *physfn_pcidev = NULL; From 20693ffcd8582af8bf225fbd86192d8928ca01a4 Mon Sep 17 00:00:00 2001 From: dramforever Date: Fri, 19 Sep 2025 21:52:00 +0800 Subject: [PATCH 2/2] udev-builtin-net_id: Add DeviceTree-based names for WLAN devices Add support for generating names like wldN based on DeviceTree aliases. DeviceTree alias names follow de facto conventions. As of writing, there are so far two ways WLAN devices are represented in DeviceTree aliases in upstream Linux DTS files: - Firstly, as wifi0, used for example in t600x-j314-j316.dtsi - Secondly, as ethernet0 or ethernet1, used for example in sun8i-q8-common.dtsi, with a comment saying the reason is to "Make u-boot set mac-address for wifi without an eeprom" Therefore for prefix "wl", try alias_prefix "wifi" first, and if that was not found, fall back to alias_prefix "ethernet" Since this is a naming scheme change, also gate this behind NAMING_DEVICETREE_ALIASES_WLAN and NAMING_V259, and document this change. --- man/systemd.net-naming-scheme.xml | 9 +++++++++ src/shared/netif-naming-scheme.c | 1 + src/shared/netif-naming-scheme.h | 2 ++ src/udev/udev-builtin-net_id.c | 8 +++++++- 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/man/systemd.net-naming-scheme.xml b/man/systemd.net-naming-scheme.xml index d21b858a5d..9211e25aa1 100644 --- a/man/systemd.net-naming-scheme.xml +++ b/man/systemd.net-naming-scheme.xml @@ -552,6 +552,15 @@ + + + v259 + + The naming scheme based on devicetree aliases was extended to support WLAN devices. + + + + Note that latest may be used to denote the latest scheme known (to this diff --git a/src/shared/netif-naming-scheme.c b/src/shared/netif-naming-scheme.c index afa5b9d842..df16e69fb6 100644 --- a/src/shared/netif-naming-scheme.c +++ b/src/shared/netif-naming-scheme.c @@ -29,6 +29,7 @@ static const NamingScheme naming_schemes[] = { { "v255", NAMING_V255 }, { "v257", NAMING_V257 }, { "v258", NAMING_V258 }, + { "v259", NAMING_V259 }, /* … add more schemes here, as the logic to name devices is updated … */ EXTRA_NET_NAMING_MAP diff --git a/src/shared/netif-naming-scheme.h b/src/shared/netif-naming-scheme.h index 18696726c3..504ffec535 100644 --- a/src/shared/netif-naming-scheme.h +++ b/src/shared/netif-naming-scheme.h @@ -42,6 +42,7 @@ typedef enum NamingSchemeFlags { NAMING_FIRMWARE_NODE_SUN = 1 << 18, /* Use firmware_node/sun to get PCI slot number */ NAMING_DEVICETREE_PORT_ALIASES = 1 << 19, /* Include aliases of OF nodes of a netdev itself, not just its parent. See PR #33958. */ NAMING_USE_INTERFACE_PROPERTY = 1 << 20, /* Use INTERFACE udev property, rather than sysname, when no renaming is requested. */ + NAMING_DEVICETREE_ALIASES_WLAN = 1 << 21, /* Generate names from devicetree aliases for WLAN devices */ /* And now the masks that combine the features above */ NAMING_V238 = 0, @@ -63,6 +64,7 @@ typedef enum NamingSchemeFlags { NAMING_V255 = NAMING_V254 & ~NAMING_BRIDGE_MULTIFUNCTION_SLOT, NAMING_V257 = NAMING_V255 | NAMING_FIRMWARE_NODE_SUN | NAMING_DEVICETREE_PORT_ALIASES, NAMING_V258 = NAMING_V257 | NAMING_USE_INTERFACE_PROPERTY, + NAMING_V259 = NAMING_V258 | NAMING_DEVICETREE_ALIASES_WLAN, EXTRA_NET_NAMING_SCHEMES diff --git a/src/udev/udev-builtin-net_id.c b/src/udev/udev-builtin-net_id.c index fd80538638..0724de8241 100644 --- a/src/udev/udev-builtin-net_id.c +++ b/src/udev/udev-builtin-net_id.c @@ -910,7 +910,13 @@ static int names_devicetree(UdevEvent *event, const char *prefix) { if (streq(prefix, "en")) r = names_devicetree_alias_prefix(event, prefix, "ethernet"); - else + else if (naming_scheme_has(NAMING_DEVICETREE_ALIASES_WLAN) && + streq(prefix, "wl")) { + r = names_devicetree_alias_prefix(event, prefix, "wifi"); + /* Sometimes DeviceTrees have WLAN devices with alias ethernetN, fall back to those */ + if (r == 0) + r = names_devicetree_alias_prefix(event, prefix, "ethernet"); + } else return -EOPNOTSUPP; /* Unsupported interface type */ if (r < 0) return r;