]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udev/net: fix assignment of ID_NET_NAME=
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 5 Mar 2025 22:25:28 +0000 (07:25 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 12 Mar 2025 16:48:33 +0000 (01:48 +0900)
E.g. sd_device object of network interface 'hoge!foo' has sysname 'hoge/foo'.
So, previously udevd assigned 'hoge/foo' rather than 'hoge!foo' to ID_NET_NAME,
hence even when renaming is not requested, such interface was renamed to 'hoge_foo'
(note '/' cannot be used in network interface name, hence escaped to underbar).

man/systemd.net-naming-scheme.xml
src/shared/netif-naming-scheme.c
src/shared/netif-naming-scheme.h
src/udev/net/link-config.c
src/udev/udev-builtin-net_setup_link.c
test/units/TEST-17-UDEV.netif-INTERFACE-property.sh [new file with mode: 0755]

index 6e77af99293937c15c770b59c950ea0d3f69f04b..96688ed3c91c39d3dd09aec7b406f743552e5d75 100644 (file)
           <xi:include href="version-info.xml" xpointer="v257"/>
           </listitem>
         </varlistentry>
+
+        <varlistentry>
+          <term><constant>v258</constant></term>
+
+          <listitem><para>When no renaming is requested, <varname>ID_NET_NAME</varname> property is now
+          equivalent to <varname>INTERFACE</varname> property.</para>
+
+          <xi:include href="version-info.xml" xpointer="v258"/>
+          </listitem>
+        </varlistentry>
       </variablelist>
 
     <para>Note that <constant>latest</constant> may be used to denote the latest scheme known (to this
index 67b7eb4d904d2df47203616e4cce4489f57276f9..8e05598c5e8eef652b0dc21d810d53df62f5ba61 100644 (file)
@@ -25,6 +25,7 @@ static const NamingScheme naming_schemes[] = {
         { "v254", NAMING_V254 },
         { "v255", NAMING_V255 },
         { "v257", NAMING_V257 },
+        { "v258", NAMING_V258 },
         /* … add more schemes here, as the logic to name devices is updated … */
 
         EXTRA_NET_NAMING_MAP
index 3ab1d752c87820a5fe7ed1b866b6d68402801162..5ead1f85d780783ce93d8b7c8659429559464d8c 100644 (file)
@@ -45,6 +45,7 @@ typedef enum NamingSchemeFlags {
         NAMING_SR_IOV_R                  = 1 << 17, /* Use "r" suffix for SR-IOV VF representors */
         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. */
 
         /* And now the masks that combine the features above */
         NAMING_V238 = 0,
@@ -65,6 +66,7 @@ typedef enum NamingSchemeFlags {
                                                        * systemd version 255, naming scheme "v255". */
         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,
 
         EXTRA_NET_NAMING_SCHEMES
 
index 3400286566fbb9bf29f31113a274476c05f27c14..b3bb164bbe5cd2619c5c29905305790c97f53e5d 100644 (file)
@@ -400,7 +400,7 @@ int link_new(LinkConfigContext *ctx, UdevEvent *event, Link **ret) {
                 .event = udev_event_ref(event),
         };
 
-        r = sd_device_get_sysname(dev, &link->ifname);
+        r = device_get_ifname(dev, &link->ifname);
         if (r < 0)
                 return r;
 
@@ -804,6 +804,9 @@ static int link_generate_new_name(Link *link) {
 
         log_link_debug(link, "Policies didn't yield a name and Name= is not given, not renaming.");
 no_rename:
+        if (!naming_scheme_has(NAMING_USE_INTERFACE_PROPERTY))
+                return sd_device_get_sysname(device, &link->new_name);
+
         link->new_name = link->ifname;
         return 0;
 }
index 0062a85db58a5f157d773779d142cb3b143b6e1b..0d59a72a062a03556d008f1996650d940cb9ea33 100644 (file)
@@ -35,7 +35,11 @@ static int builtin_net_setup_link(UdevEvent *event, int argc, char **argv) {
 
                 /* Set ID_NET_NAME= with the current interface name. */
                 const char *value;
-                if (sd_device_get_sysname(dev, &value) >= 0)
+                if (naming_scheme_has(NAMING_USE_INTERFACE_PROPERTY))
+                        r = device_get_ifname(dev, &value);
+                else
+                        r = sd_device_get_sysname(dev, &value);
+                if (r >= 0)
                         (void) udev_builtin_add_property(event, "ID_NET_NAME", value);
 
                 return 0;
diff --git a/test/units/TEST-17-UDEV.netif-INTERFACE-property.sh b/test/units/TEST-17-UDEV.netif-INTERFACE-property.sh
new file mode 100755 (executable)
index 0000000..deb1156
--- /dev/null
@@ -0,0 +1,46 @@
+#!/usr/bin/env bash
+# SPDX-License-Identifier: LGPL-2.1-or-later
+set -ex
+set -o pipefail
+
+# shellcheck source=test/units/util.sh
+. "$(dirname "$0")"/util.sh
+
+mkdir -p /run/systemd/network/
+cat >/run/systemd/network/10-rename-test.link <<EOF
+[Match]
+OriginalName=testif
+
+[Link]
+Name=te!st!if
+EOF
+
+udevadm control --log-level=debug --reload
+
+# Check if any interfaces originally named with '!' in their name have been renamed unexpectedly.
+ip link add 'hoge!foo' type dummy
+udevadm wait --settle --timeout=30 '/sys/class/net/hoge!foo'
+output=$(udevadm info --query property '/sys/class/net/hoge!foo')
+assert_in 'INTERFACE=hoge!foo' "$output"
+assert_in 'ID_NET_DRIVER=dummy' "$output"
+assert_in 'ID_NET_NAME=hoge!foo' "$output"
+assert_not_in 'ID_RENAMING=' "$output"
+ip link show dev 'hoge!foo'
+ip link del dev 'hoge!foo'
+
+# Check if the interface renamed to include '!' as expected.
+ip link add 'testif' type dummy
+udevadm wait --settle --timeout=30 '/sys/class/net/te!st!if'
+output=$(udevadm info --query property '/sys/class/net/te!st!if')
+assert_in 'INTERFACE=te!st!if' "$output"
+assert_in 'ID_NET_DRIVER=dummy' "$output"
+assert_in 'ID_NET_NAME=te!st!if' "$output"
+assert_not_in 'ID_RENAMING=' "$output"
+ip link show dev 'te!st!if'
+ip link del dev 'te!st!if'
+
+# cleanup
+rm -f /run/systemd/network/10-rename-test.link
+udevadm control --log-level=info --reload
+
+exit 0