Blob Blame History Raw
From bb6114af097da0cd9c5081e42db718559130687f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michal=20Sekleta=CC=81r?= <msekleta@redhat.com>
Date: Mon, 19 Oct 2020 11:10:31 +0200
Subject: [PATCH] udev/net_id: don't generate slot based names if multiple
 devices might claim the same slot

(cherry picked from commit 2c8ec0095e6fd2e72879d4915ff8a9e5c0664d0b)

Resolves: #1827462
---
 man/systemd.net-naming-scheme.xml | 15 ++++++++++-
 src/udev/udev-builtin-net_id.c    | 41 ++++++++++++++++++++++++++-----
 2 files changed, 49 insertions(+), 7 deletions(-)

diff --git a/man/systemd.net-naming-scheme.xml b/man/systemd.net-naming-scheme.xml
index a12cc3c460..10e71dcb15 100644
--- a/man/systemd.net-naming-scheme.xml
+++ b/man/systemd.net-naming-scheme.xml
@@ -176,7 +176,10 @@
 
           <para>SR-IOV virtual devices are named based on the name of the parent interface, with a suffix of
           <constant>v</constant> and the virtual device number, with any leading zeros removed. The bus
-          number is ignored. This device type is found in IBM PowerVMs.</para>
+          number is ignored.</para>
+
+          <para>In some configurations a parent PCI bridge of a given network controller may be associated
+          with a slot. In such case we don't generate this device property to avoid possible naming conflicts.</para>
         </varlistentry>
 
         <varlistentry>
@@ -288,6 +291,16 @@
           <para>Same as naming scheme <constant>rhel-8.0</constant>.</para>
         </varlistentry>
 
+        <varlistentry>
+          <term><constant>rhel-8.4</constant></term>
+
+          <listitem><para>If the PCI slot is assocated with PCI bridge and that has multiple child network
+          controllers then all of them might derive the same value of <varname>ID_NET_NAME_SLOT</varname>
+          property. That could cause naming conflict if the property is selected as a device name. Now, we detect the
+          situation, slot - bridge relation, and we don't produce the <varname>ID_NET_NAME_SLOT</varname> property to
+          avoid possible naming conflict.</para></listitem>
+        </varlistentry>
+
         <para>Note that <constant>latest</constant> may be used to denote the latest scheme known (to this
         particular version of systemd.</para>
     </variablelist>
diff --git a/src/udev/udev-builtin-net_id.c b/src/udev/udev-builtin-net_id.c
index ede24dee41..d8c56b62bb 100644
--- a/src/udev/udev-builtin-net_id.c
+++ b/src/udev/udev-builtin-net_id.c
@@ -124,6 +124,7 @@ typedef enum NamingSchemeFlags {
         /* First, the individual features */
         NAMING_SR_IOV_V        = 1 << 0, /* Use "v" suffix for SR-IOV, see 609948c7043a40008b8299529c978ed8e11de8f6*/
         NAMING_NPAR_ARI        = 1 << 1, /* Use NPAR "ARI", see 6bc04997b6eab35d1cb9fa73889892702c27be09 */
+        NAMING_BRIDGE_NO_SLOT  = 1 << 9, /* Don't use PCI hotplug slot information if the corresponding device is a PCI bridge */
 
         /* And now the masks that combine the features above */
         NAMING_V238 = 0,
@@ -132,6 +133,7 @@ typedef enum NamingSchemeFlags {
         NAMING_RHEL_8_1 = NAMING_V239,
         NAMING_RHEL_8_2 = NAMING_V239,
         NAMING_RHEL_8_3 = NAMING_V239,
+        NAMING_RHEL_8_4 = NAMING_V239|NAMING_BRIDGE_NO_SLOT,
 
         _NAMING_SCHEME_FLAGS_INVALID = -1,
 } NamingSchemeFlags;
@@ -389,6 +391,26 @@ static bool is_pci_ari_enabled(struct udev_device *dev) {
         return streq_ptr(udev_device_get_sysattr_value(dev, "ari_enabled"), "1");
 }
 
+static bool is_pci_bridge(struct udev_device *dev) {
+        const char *v, *p;
+
+        v = udev_device_get_sysattr_value(dev, "modalias");
+        if (!v)
+                return false;
+
+        if (!startswith(v, "pci:"))
+                return false;
+
+        p = strrchr(v, 's');
+        if (!p)
+                return false;
+        if (p[1] != 'c')
+                return false;
+
+        /* PCI device subclass 04 corresponds to PCI bridge */
+        return strneq(p + 2, "04", 2);
+}
+
 static int dev_pci_slot(struct udev_device *dev, struct netnames *names) {
         struct udev *udev = udev_device_get_udev(names->pcidev);
         unsigned domain, bus, slot, func, dev_port = 0;
@@ -461,16 +483,23 @@ static int dev_pci_slot(struct udev_device *dev, struct netnames *names) {
                         if (r < 0 || i <= 0)
                                 continue;
 
+                        /* match slot address with device by stripping the function */
                         if (snprintf_ok(str, sizeof str, "%s/%s/address", slots, dent->d_name) &&
-                            read_one_line_file(str, &address) >= 0)
-                                /* match slot address with device by stripping the function */
-                                if (startswith(udev_device_get_sysname(hotplug_slot_dev), address))
-                                        hotplug_slot = i;
+                            read_one_line_file(str, &address) >= 0 &&
+                            startswith(udev_device_get_sysname(hotplug_slot_dev), address)) {
+                                hotplug_slot = i;
+
+                                /* We found the match between PCI device and slot. However, we won't use the
+                                 * slot index if the device is a PCI bridge, because it can have other child
+                                 * devices that will try to claim the same index and that would create name
+                                 * collision. */
+                                if (naming_scheme_has(NAMING_BRIDGE_NO_SLOT) && is_pci_bridge(hotplug_slot_dev))
+                                        hotplug_slot = 0;
 
-                        if (hotplug_slot > 0)
                                 break;
+                        }
                 }
-                if (hotplug_slot > 0)
+                if (hotplug_slot >= 0)
                         break;
                 rewinddir(dir);
                 hotplug_slot_dev = udev_device_get_parent_with_subsystem_devtype(hotplug_slot_dev, "pci", NULL);