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