79b470
From cfe170216accf60938ff4ea9440a4ac78b0bd83f Mon Sep 17 00:00:00 2001
79b470
Message-Id: <cfe170216accf60938ff4ea9440a4ac78b0bd83f@dist-git>
79b470
From: Dmytro Linkin <dlinkin@nvidia.com>
79b470
Date: Thu, 28 Jan 2021 23:17:29 -0500
79b470
Subject: [PATCH] util: Add phys_port_name support on virPCIGetNetName
79b470
79b470
virPCIGetNetName is used to get the name of the netdev associated with
79b470
a particular PCI device. This is used when we have a VF name, but need
79b470
the PF name in order to send a netlink command (e.g. in order to
79b470
get/set the MAC address of the VF).
79b470
79b470
In simple cases there is a single netdev associated with any PCI
79b470
device, so it is easy to figure out the PF netdev for a VF - just look
79b470
for the PCI device that has the VF listed in its "virtfns" directory;
79b470
the only name in the "net" subdirectory of that PCI device's sysfs
79b470
directory is the PF netdev that is upstream of the VF in question.
79b470
79b470
In some cases there can be more than one netdev in a PCI device's net
79b470
directory though. In the past, the only case of this was for SR-IOV
79b470
NICs that could have multiple PF's per PCI device. In this case, all
79b470
PF netdevs associated with a PCI address would be listed in the "net"
79b470
subdirectory of the PCI device's directory in sysfs. At the same time,
79b470
all VF netdevs and all PF netdevs have a phys_port_id in their sysfs,
79b470
so the way to learn the correct PF netdev for a particular VF netdev
79b470
is to search through the list of devices in the net subdirectory of
79b470
the PF's PCI device, looking for the one netdev with a "phys_port_id"
79b470
matching that of the VF netdev.
79b470
79b470
But starting in kernel 5.8, the NVIDIA Mellanox driver began linking
79b470
the VFs' representor netdevs to the PF PCI address [1], and so the VF
79b470
representor netdevs would also show up in the net
79b470
subdirectory. However, all of the devices that do so also only have a
79b470
single PF netdev for any given PCI address.
79b470
79b470
This means that the net directory of the PCI device can still hold
79b470
multiple net devices, but only one of them will be the PF netdev (the
79b470
others are VF representors):
79b470
79b470
$ ls '/sys/bus/pci/devices/0000:82:00.0/net'
79b470
ens1f0  eth0  eth1
79b470
79b470
In this case the way to find the PF device is to look at the
79b470
"phys_port_name" attribute of each netdev in sysfs. All PF devices
79b470
have a phys_port_name matching a particular regex
79b470
79b470
  (p[0-9]+$)|(p[0-9]+s[0-9]+$)
79b470
79b470
Since there can only be one PF in the entire list of devices, once we
79b470
match that regex, we've found the PF netdev.
79b470
79b470
[1] - https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net.git/
79b470
      commit/?id=123f0f53dd64b67e34142485fe866a8a581f12f1
79b470
79b470
Resolves: https://bugzilla.redhat.com/1918708
79b470
Co-Authored-by: Moshe Levi <moshele@nvidia.com>
79b470
Signed-off-by: Dmytro Linkin <dlinkin@nvidia.com>
79b470
Reviewed-by: Adrian Chiris <adrianc@nvidia.com>
79b470
Reviewed-by: Laine Stump <laine@redhat.com>
79b470
(cherry picked from commit 5b1c525b1f3608156884aed0dc5e925306c1e260)
79b470
79b470
Conflicts: src/util/virpci.c - upstream all DIR* were converted to use
79b470
    g_autoptr, which permitted virPCIGetNetName() to be
79b470
    simplified. Unfortunately, backporting this refactor would require
79b470
    backporting an ever-ballooning set of patches, making the
79b470
    possibility of causing a regression a very real danger. Instead,
79b470
    one small refactor of virPCIGetName() that didn't affect any other
79b470
    functions was backported, and this patch (adding phys_port_name
79b470
    support) resolved the remaining conflicts by mimicking the current
79b470
    upstream version of the function, but with all "return 0" replaced
79b470
    by "ret = 0; goto cleanup;" and all "return -1" replaced by "goto
79b470
    cleanup;" (the code at cleanup: just closes the DIR* and returns
79b470
    the current value of ret). This will assure identical behavior to
79b470
    upstream.
79b470
Signed-off-by: Laine Stump <laine@redhat.com>
79b470
Message-Id: <20210129041729.1076345-4-laine@redhat.com>
79b470
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
79b470
---
79b470
 src/util/virpci.c | 93 ++++++++++++++++++++++++++++-------------------
79b470
 src/util/virpci.h |  5 +++
79b470
 2 files changed, 61 insertions(+), 37 deletions(-)
79b470
79b470
diff --git a/src/util/virpci.c b/src/util/virpci.c
79b470
index 00377eed31..d5c038b7fe 100644
79b470
--- a/src/util/virpci.c
79b470
+++ b/src/util/virpci.c
79b470
@@ -2424,9 +2424,9 @@ virPCIDeviceAddressGetSysfsFile(virPCIDeviceAddressPtr addr,
79b470
  * virPCIGetNetName:
79b470
  * @device_link_sysfs_path: sysfs path to the PCI device
79b470
  * @idx: used to choose which netdev when there are several
79b470
- *       (ignored if physPortID is set)
79b470
+ *       (ignored if physPortID is set or physPortName is available)
79b470
  * @physPortID: match this string in the netdev's phys_port_id
79b470
- *       (or NULL to ignore and use idx instead)
79b470
+ *       (or NULL to ignore and use phys_port_name or idx instead)
79b470
  * @netname: used to return the name of the netdev
79b470
  *       (set to NULL (but returns success) if there is no netdev)
79b470
  *
79b470
@@ -2460,6 +2460,14 @@ virPCIGetNetName(const char *device_link_sysfs_path,
79b470
     }
79b470
 
79b470
     while (virDirRead(dir, &entry, pcidev_sysfs_net_path) > 0) {
79b470
+        /* save the first entry we find to use as a failsafe
79b470
+         * in case we don't match the phys_port_id. This is
79b470
+         * needed because some NIC drivers (e.g. i40e)
79b470
+         * implement phys_port_id for PFs, but not for VFs
79b470
+         */
79b470
+        if (!firstEntryName)
79b470
+            firstEntryName = g_strdup(entry->d_name);
79b470
+
79b470
         /* if the caller sent a physPortID, compare it to the
79b470
          * physportID of this netdev. If not, look for entry[idx].
79b470
          */
79b470
@@ -2470,50 +2478,61 @@ virPCIGetNetName(const char *device_link_sysfs_path,
79b470
                 goto cleanup;
79b470
 
79b470
             /* if this one doesn't match, keep looking */
79b470
-            if (STRNEQ_NULLABLE(physPortID, thisPhysPortID)) {
79b470
-                /* save the first entry we find to use as a failsafe
79b470
-                 * in case we don't match the phys_port_id. This is
79b470
-                 * needed because some NIC drivers (e.g. i40e)
79b470
-                 * implement phys_port_id for PFs, but not for VFs
79b470
-                 */
79b470
-                if (!firstEntryName)
79b470
-                    firstEntryName = g_strdup(entry->d_name);
79b470
-
79b470
+            if (STRNEQ_NULLABLE(physPortID, thisPhysPortID))
79b470
                 continue;
79b470
-            }
79b470
+
79b470
         } else {
79b470
-            if (i++ < idx)
79b470
-                continue;
79b470
-        }
79b470
+            /* Most switch devices use phys_port_name instead of
79b470
+             * phys_port_id.
79b470
+             * NOTE: VFs' representors net devices can be linked to PF's PCI
79b470
+             * device, which mean that there'll be multiple net devices
79b470
+             * instances and to get a proper net device need to match on
79b470
+             * specific regex.
79b470
+             * To get PF netdev, for ex., used following regex:
79b470
+             * "(p[0-9]+$)|(p[0-9]+s[0-9]+$)"
79b470
+             * or to get exact VF's netdev next regex is used:
79b470
+             * "pf0vf1$"
79b470
+             */
79b470
+            g_autofree char *thisPhysPortName = NULL;
79b470
 
79b470
-        *netname = g_strdup(entry->d_name);
79b470
+            if (virNetDevGetPhysPortName(entry->d_name, &thisPhysPortName) < 0)
79b470
+                goto cleanup;
79b470
 
79b470
-        ret = 0;
79b470
-        break;
79b470
-    }
79b470
+            if (thisPhysPortName) {
79b470
+
79b470
+                /* if this one doesn't match, keep looking */
79b470
+                if (!virStringMatch(thisPhysPortName, VIR_PF_PHYS_PORT_NAME_REGEX))
79b470
+                    continue;
79b470
 
79b470
-    if (ret < 0) {
79b470
-        if (physPortID) {
79b470
-            if (firstEntryName) {
79b470
-                /* we didn't match the provided phys_port_id, but this
79b470
-                 * is probably because phys_port_id isn't implemented
79b470
-                 * for this NIC driver, so just return the first
79b470
-                 * (probably only) netname we found.
79b470
-                 */
79b470
-                *netname = firstEntryName;
79b470
-                firstEntryName = NULL;
79b470
-                ret = 0;
79b470
             } else {
79b470
-                virReportError(VIR_ERR_INTERNAL_ERROR,
79b470
-                               _("Could not find network device with "
79b470
-                                 "phys_port_id '%s' under PCI device at %s"),
79b470
-                               physPortID, device_link_sysfs_path);
79b470
+
79b470
+                if (i++ < idx)
79b470
+                    continue;
79b470
             }
79b470
-        } else {
79b470
-            ret = 0; /* no netdev at the given index is *not* an error */
79b470
         }
79b470
+
79b470
+        *netname = g_strdup(entry->d_name);
79b470
+        ret = 0;
79b470
+        goto cleanup;
79b470
     }
79b470
- cleanup:
79b470
+
79b470
+    if (firstEntryName) {
79b470
+        /* we didn't match the provided phys_port_id / find a
79b470
+         * phys_port_name matching VIR_PF_PHYS_PORT_NAME_REGEX / find
79b470
+         * as many net devices as the value of idx, but this is
79b470
+         * probably because phys_port_id / phys_port_name isn't
79b470
+         * implemented for this NIC driver, so just return the first
79b470
+         * (probably only) netname we found.
79b470
+         */
79b470
+        *netname = g_steal_pointer(&firstEntryName);
79b470
+        ret = 0;
79b470
+        goto cleanup;
79b470
+    }
79b470
+
79b470
+    virReportError(VIR_ERR_INTERNAL_ERROR,
79b470
+                   _("Could not find any network device under PCI device at %s"),
79b470
+                   device_link_sysfs_path);
79b470
+cleanup:
79b470
     VIR_DIR_CLOSE(dir);
79b470
     return ret;
79b470
 }
79b470
diff --git a/src/util/virpci.h b/src/util/virpci.h
79b470
index f6796fc422..e47c766918 100644
79b470
--- a/src/util/virpci.h
79b470
+++ b/src/util/virpci.h
79b470
@@ -49,6 +49,11 @@ struct _virZPCIDeviceAddress {
79b470
 
79b470
 #define VIR_PCI_DEVICE_ADDRESS_FMT "%04x:%02x:%02x.%d"
79b470
 
79b470
+/* Represents format of PF's phys_port_name in switchdev mode:
79b470
+ * 'p%u' or 'p%us%u'. New line checked since value is readed from sysfs file.
79b470
+ */
79b470
+#define VIR_PF_PHYS_PORT_NAME_REGEX  "(p[0-9]+$)|(p[0-9]+s[0-9]+$)"
79b470
+
79b470
 struct _virPCIDeviceAddress {
79b470
     unsigned int domain;
79b470
     unsigned int bus;
79b470
-- 
79b470
2.30.0
79b470