b7dd4d
From 2e7f41bd0632312d00d472a73a312218a29ce65b Mon Sep 17 00:00:00 2001
b7dd4d
From: Viktor Mihajlovski <mihajlov@linux.ibm.com>
b7dd4d
Date: Thu, 18 Mar 2021 11:03:34 +0100
b7dd4d
Subject: [PATCH] udev: fix slot based network names on s390
b7dd4d
b7dd4d
The s390 PCI driver assigns the hotplug slot name from the
b7dd4d
function_id attribute of the PCI device using a 8 char hexadecimal
b7dd4d
format to match the underlying firmware/hypervisor notation.
b7dd4d
b7dd4d
Further, there's always a one-to-one mapping between a PCI
b7dd4d
function and a hotplug slot, as individual functions can
b7dd4d
hot plugged even for multi-function devices.
b7dd4d
b7dd4d
As the generic matching code will always try to parse the slot
b7dd4d
name in /sys/bus/pci/slots as a positive decimal number, either
b7dd4d
a wrong value might be produced for ID_NET_NAME_SLOT if
b7dd4d
the slot name consists of decimal numbers only, or none at all
b7dd4d
if a character in the range from 'a' to 'f' is encountered.
b7dd4d
b7dd4d
Additionally, the generic code assumes that two interfaces
b7dd4d
share a hotplug slot, if they differ only in the function part
b7dd4d
of the PCI address. E.g., for an interface with the PCI address
b7dd4d
dddd:bb:aa.f, it will match the device to the first slot with
b7dd4d
an address dddd:bb:aa. As more than one slot may have this address
b7dd4d
for the s390 PCI driver, the wrong slot may be selected.
b7dd4d
b7dd4d
To resolve this we're adding a new naming schema version with the
b7dd4d
flag NAMING_SLOT_FUNCTION_ID, which enables the correct matching
b7dd4d
of hotplug slots if the device has an attribute named function_id.
b7dd4d
The ID_NET_NAME_SLOT property will only be produced if there's
b7dd4d
a file /sys/bus/pci/slots/<slotname> where <slotname> matches
b7dd4d
the value of /sys/bus/pci/devices/.../function_id in 8 char
b7dd4d
hex notation.
b7dd4d
b7dd4d
Fixes #19016
b7dd4d
See also #19078
b7dd4d
b7dd4d
Related: #1939914
b7dd4d
b7dd4d
(cherry picked from commit a496a238e8ee66ce25ad13a3f46549b2e2e979fc)
b7dd4d
---
b7dd4d
 man/systemd.net-naming-scheme.xml | 10 +++++++++
b7dd4d
 src/udev/udev-builtin-net_id.c    | 34 +++++++++++++++++++++++++++++++
b7dd4d
 2 files changed, 44 insertions(+)
b7dd4d
b7dd4d
diff --git a/man/systemd.net-naming-scheme.xml b/man/systemd.net-naming-scheme.xml
b7dd4d
index fe1aa4b654..e42c93eaad 100644
b7dd4d
--- a/man/systemd.net-naming-scheme.xml
b7dd4d
+++ b/man/systemd.net-naming-scheme.xml
b7dd4d
@@ -313,6 +313,16 @@
b7dd4d
           <para>Same as naming scheme <constant>rhel-8.4</constant>.</para>
b7dd4d
         </varlistentry>
b7dd4d
 
b7dd4d
+        <varlistentry>
b7dd4d
+         <term><constant>rhel-8.7</constant></term>
b7dd4d
+
b7dd4d
+          <listitem><para>PCI hotplug slot names for the s390 PCI driver are a hexadecimal representation
b7dd4d
+          of the <filename>function_id</filename> device attribute. This attribute is now used to build the
b7dd4d
+          <varname>ID_NET_NAME_SLOT</varname>. Before that, all slot names were parsed as decimal
b7dd4d
+          numbers, which could either result in an incorrect value of the <varname>ID_NET_NAME_SLOT</varname>
b7dd4d
+          property or none at all.</para></listitem>
b7dd4d
+        </varlistentry>
b7dd4d
+
b7dd4d
         <para>Note that <constant>latest</constant> may be used to denote the latest scheme known to this
b7dd4d
         particular version of systemd.</para>
b7dd4d
     </variablelist>
b7dd4d
diff --git a/src/udev/udev-builtin-net_id.c b/src/udev/udev-builtin-net_id.c
b7dd4d
index 386d74ca5e..b57227a09f 100644
b7dd4d
--- a/src/udev/udev-builtin-net_id.c
b7dd4d
+++ b/src/udev/udev-builtin-net_id.c
b7dd4d
@@ -126,6 +126,7 @@ typedef enum NamingSchemeFlags {
b7dd4d
         NAMING_SR_IOV_V        = 1 << 0, /* Use "v" suffix for SR-IOV, see 609948c7043a40008b8299529c978ed8e11de8f6*/
b7dd4d
         NAMING_NPAR_ARI        = 1 << 1, /* Use NPAR "ARI", see 6bc04997b6eab35d1cb9fa73889892702c27be09 */
b7dd4d
         NAMING_BRIDGE_NO_SLOT  = 1 << 9, /* Don't use PCI hotplug slot information if the corresponding device is a PCI bridge */
b7dd4d
+        NAMING_SLOT_FUNCTION_ID = 1 << 10, /* Use function_id if present to identify PCI hotplug slots */
b7dd4d
 
b7dd4d
         /* And now the masks that combine the features above */
b7dd4d
         NAMING_V238 = 0,
b7dd4d
@@ -137,6 +138,7 @@ typedef enum NamingSchemeFlags {
b7dd4d
         NAMING_RHEL_8_4 = NAMING_V239|NAMING_BRIDGE_NO_SLOT,
b7dd4d
         NAMING_RHEL_8_5 = NAMING_RHEL_8_4,
b7dd4d
         NAMING_RHEL_8_6 = NAMING_RHEL_8_4,
b7dd4d
+        NAMING_RHEL_8_7 = NAMING_RHEL_8_4|NAMING_SLOT_FUNCTION_ID,
b7dd4d
 
b7dd4d
         _NAMING_SCHEME_FLAGS_INVALID = -1,
b7dd4d
 } NamingSchemeFlags;
b7dd4d
@@ -156,6 +158,7 @@ static const NamingScheme naming_schemes[] = {
b7dd4d
         { "rhel-8.4", NAMING_RHEL_8_4 },
b7dd4d
         { "rhel-8.5", NAMING_RHEL_8_5 },
b7dd4d
         { "rhel-8.6", NAMING_RHEL_8_6 },
b7dd4d
+        { "rhel-8.7", NAMING_RHEL_8_7 },
b7dd4d
         /* … add more schemes here, as the logic to name devices is updated … */
b7dd4d
 };
b7dd4d
 
b7dd4d
@@ -477,6 +480,37 @@ static int dev_pci_slot(struct udev_device *dev, struct netnames *names) {
b7dd4d
 
b7dd4d
         hotplug_slot_dev = names->pcidev;
b7dd4d
         while (hotplug_slot_dev) {
b7dd4d
+                if (!udev_device_get_sysname(hotplug_slot_dev))
b7dd4d
+                        continue;
b7dd4d
+
b7dd4d
+                /*  The <sysname>/function_id attribute is unique to the s390 PCI driver.
b7dd4d
+                    If present, we know that the slot's directory name for this device is
b7dd4d
+                    /sys/bus/pci/XXXXXXXX/ where XXXXXXXX is the fixed length 8 hexadecimal
b7dd4d
+                    character string representation of function_id.
b7dd4d
+                    Therefore we can short cut here and just check for the existence of
b7dd4d
+                    the slot directory. As this directory has to exist, we're emitting a
b7dd4d
+                    debug message for the unlikely case it's not found.
b7dd4d
+                    Note that the domain part of doesn't belong to the slot name here
b7dd4d
+                    because there's a 1-to-1 relationship between PCI function and its hotplug
b7dd4d
+                    slot.
b7dd4d
+                 */
b7dd4d
+                if (naming_scheme_has(NAMING_SLOT_FUNCTION_ID)) {
b7dd4d
+                        attr = udev_device_get_sysattr_value(hotplug_slot_dev, "function_id");
b7dd4d
+                        if (attr) {
b7dd4d
+                                int function_id;
b7dd4d
+                                _cleanup_free_ char *str;
b7dd4d
+
b7dd4d
+                                if (safe_atoi(attr, &function_id) >= 0 &&
b7dd4d
+                                    asprintf(&str, "%s/%08x/", slots, function_id) >= 0 &&
b7dd4d
+                                    access(str, R_OK) == 0) {
b7dd4d
+                                        hotplug_slot = function_id;
b7dd4d
+                                        domain = 0;
b7dd4d
+                                } else
b7dd4d
+                                        log_debug("No matching slot for function_id (%s).", attr);
b7dd4d
+                                break;
b7dd4d
+                        }
b7dd4d
+                }
b7dd4d
+
b7dd4d
                 FOREACH_DIRENT_ALL(dent, dir, break) {
b7dd4d
                         int i, r;
b7dd4d
                         char str[PATH_MAX];