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