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