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