render / rpms / libvirt

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