397dc2
From 1b2014e2206b30a743a75908a129e444761726a7 Mon Sep 17 00:00:00 2001
397dc2
Message-Id: <1b2014e2206b30a743a75908a129e444761726a7@dist-git>
397dc2
From: Moshe Levi <moshele@nvidia.com>
397dc2
Date: Thu, 28 Jan 2021 23:17:27 -0500
397dc2
Subject: [PATCH] util: add virNetDevGetPhysPortName
397dc2
397dc2
This commit add virNetDevGetPhysPortName to read netdevice
397dc2
phys_port_name from sysfs. It also refactor the code so
397dc2
virNetDevGetPhysPortName and virNetDevGetPhysPortID will use
397dc2
same method to read the netdevice sysfs.
397dc2
397dc2
https://bugzilla.redhat.com/1918708
397dc2
Signed-off-by: Moshe Levi <moshele@nvidia.com>
397dc2
Reviewed-by: Laine Stump <laine@redhat.com>
397dc2
(cherry picked from commit 97ebb982453bc23759c5f180799d6f2207b81c80)
397dc2
397dc2
Conflicts: src/util/virnetdev.c was converted to use g_autofree upstream.
397dc2
    This patch removes the need for the g_autofreed variable.
397dc2
Signed-off-by: Laine Stump <laine@redhat.com>
397dc2
Message-Id: <20210129041729.1076345-2-laine@redhat.com>
397dc2
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
397dc2
---
397dc2
 src/util/virnetdev.c | 72 +++++++++++++++++++++++++++++++++-----------
397dc2
 src/util/virnetdev.h |  4 +++
397dc2
 2 files changed, 59 insertions(+), 17 deletions(-)
397dc2
397dc2
diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c
397dc2
index e2aad07c24..52c9343d63 100644
397dc2
--- a/src/util/virnetdev.c
397dc2
+++ b/src/util/virnetdev.c
397dc2
@@ -1143,6 +1143,29 @@ virNetDevGetPCIDevice(const char *devName)
397dc2
 }
397dc2
 
397dc2
 
397dc2
+/* A wrapper to get content of file from ifname SYSFS_NET_DIR
397dc2
+ */
397dc2
+static int
397dc2
+virNetDevGetSysfsFileValue(const char *ifname,
397dc2
+                           const char *fileName,
397dc2
+                           char **sysfsFileData)
397dc2
+{
397dc2
+    g_autofree char *sysfsFile = NULL;
397dc2
+
397dc2
+    *sysfsFileData = NULL;
397dc2
+
397dc2
+    if (virNetDevSysfsFile(&sysfsFile, ifname, fileName) < 0)
397dc2
+        return -1;
397dc2
+
397dc2
+    /* a failure to read just means the driver doesn't support
397dc2
+     * <fileName>, so set success now and ignore the return from
397dc2
+     * virFileReadAllQuiet().
397dc2
+     */
397dc2
+
397dc2
+    ignore_value(virFileReadAllQuiet(sysfsFile, 1024, sysfsFileData));
397dc2
+    return 0;
397dc2
+}
397dc2
+
397dc2
 /**
397dc2
  * virNetDevGetPhysPortID:
397dc2
  *
397dc2
@@ -1161,25 +1184,29 @@ int
397dc2
 virNetDevGetPhysPortID(const char *ifname,
397dc2
                        char **physPortID)
397dc2
 {
397dc2
-    int ret = -1;
397dc2
-    char *physPortIDFile = NULL;
397dc2
-
397dc2
-    *physPortID = NULL;
397dc2
-
397dc2
-    if (virNetDevSysfsFile(&physPortIDFile, ifname, "phys_port_id") < 0)
397dc2
-        goto cleanup;
397dc2
-
397dc2
-    /* a failure to read just means the driver doesn't support
397dc2
-     * phys_port_id, so set success now and ignore the return from
397dc2
-     * virFileReadAllQuiet().
397dc2
-     */
397dc2
-    ret = 0;
397dc2
+    return virNetDevGetSysfsFileValue(ifname, "phys_port_id", physPortID);
397dc2
+}
397dc2
 
397dc2
-    ignore_value(virFileReadAllQuiet(physPortIDFile, 1024, physPortID));
397dc2
 
397dc2
- cleanup:
397dc2
-    VIR_FREE(physPortIDFile);
397dc2
-    return ret;
397dc2
+/**
397dc2
+ * virNetDevGetPhysPortName:
397dc2
+ *
397dc2
+ * @ifname: name of a netdev
397dc2
+ *
397dc2
+ * @physPortName: pointer to char* that will receive @ifname's
397dc2
+ *                phys_port_name from sysfs (null terminated
397dc2
+ *                string). Could be NULL if @ifname's net driver doesn't
397dc2
+ *                support phys_port_name (most netdev drivers
397dc2
+ *                don't). Caller is responsible for freeing the string
397dc2
+ *                when finished.
397dc2
+ *
397dc2
+ * Returns 0 on success or -1 on failure.
397dc2
+ */
397dc2
+int
397dc2
+virNetDevGetPhysPortName(const char *ifname,
397dc2
+                         char **physPortName)
397dc2
+{
397dc2
+    return virNetDevGetSysfsFileValue(ifname, "phys_port_name", physPortName);
397dc2
 }
397dc2
 
397dc2
 
397dc2
@@ -1461,6 +1488,17 @@ virNetDevGetPhysPortID(const char *ifname G_GNUC_UNUSED,
397dc2
     return 0;
397dc2
 }
397dc2
 
397dc2
+int
397dc2
+virNetDevGetPhysPortName(const char *ifname G_GNUC_UNUSED,
397dc2
+                       char **physPortName)
397dc2
+{
397dc2
+    /* this actually should never be called, and is just here to
397dc2
+     * satisfy the linker.
397dc2
+     */
397dc2
+    *physPortName = NULL;
397dc2
+    return 0;
397dc2
+}
397dc2
+
397dc2
 int
397dc2
 virNetDevGetVirtualFunctions(const char *pfname G_GNUC_UNUSED,
397dc2
                              char ***vfname G_GNUC_UNUSED,
397dc2
diff --git a/src/util/virnetdev.h b/src/util/virnetdev.h
397dc2
index 24b41498ed..26fe76cc2c 100644
397dc2
--- a/src/util/virnetdev.h
397dc2
+++ b/src/util/virnetdev.h
397dc2
@@ -227,6 +227,10 @@ int virNetDevGetPhysPortID(const char *ifname,
397dc2
                            char **physPortID)
397dc2
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
397dc2
     G_GNUC_WARN_UNUSED_RESULT;
397dc2
+int virNetDevGetPhysPortName(const char *ifname,
397dc2
+                           char **physPortName)
397dc2
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
397dc2
+    G_GNUC_WARN_UNUSED_RESULT;
397dc2
 
397dc2
 int virNetDevGetVirtualFunctions(const char *pfname,
397dc2
                                  char ***vfname,
397dc2
-- 
397dc2
2.30.0
397dc2