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