]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: add virNetDevGetPhysPortName
authorMoshe Levi <moshele@nvidia.com>
Thu, 21 Jan 2021 12:15:21 +0000 (14:15 +0200)
committerLaine Stump <laine@redhat.com>
Tue, 26 Jan 2021 01:27:38 +0000 (20:27 -0500)
This commit add virNetDevGetPhysPortName to read netdevice
phys_port_name from sysfs. It also refactor the code so
virNetDevGetPhysPortName and virNetDevGetPhysPortID will use
same method to read the netdevice sysfs.

Signed-off-by: Moshe Levi <moshele@nvidia.com>
Reviewed-by: Laine Stump <laine@redhat.com>
src/util/virnetdev.c
src/util/virnetdev.h

index a73e5f72f1f03283f94fe58494d0134966046830..2485718b48fc769546441df8b0f0b2619745b981 100644 (file)
@@ -1147,6 +1147,29 @@ virNetDevGetPCIDevice(const char *devName)
 # endif
 
 
+/* A wrapper to get content of file from ifname SYSFS_NET_DIR
+ */
+static int
+virNetDevGetSysfsFileValue(const char *ifname,
+                           const char *fileName,
+                           char **sysfsFileData)
+{
+    g_autofree char *sysfsFile = NULL;
+
+    *sysfsFileData = NULL;
+
+    if (virNetDevSysfsFile(&sysfsFile, ifname, fileName) < 0)
+        return -1;
+
+    /* a failure to read just means the driver doesn't support
+     * <fileName>, so set success now and ignore the return from
+     * virFileReadAllQuiet().
+     */
+
+    ignore_value(virFileReadAllQuiet(sysfsFile, 1024, sysfsFileData));
+    return 0;
+}
+
 /**
  * virNetDevGetPhysPortID:
  *
@@ -1165,20 +1188,29 @@ int
 virNetDevGetPhysPortID(const char *ifname,
                        char **physPortID)
 {
-    g_autofree char *physPortIDFile = NULL;
-
-    *physPortID = NULL;
-
-    if (virNetDevSysfsFile(&physPortIDFile, ifname, "phys_port_id") < 0)
-        return -1;
+    return virNetDevGetSysfsFileValue(ifname, "phys_port_id", physPortID);
+}
 
-    /* a failure to read just means the driver doesn't support
-     * phys_port_id, so set success now and ignore the return from
-     * virFileReadAllQuiet().
-     */
 
-    ignore_value(virFileReadAllQuiet(physPortIDFile, 1024, physPortID));
-    return 0;
+/**
+ * virNetDevGetPhysPortName:
+ *
+ * @ifname: name of a netdev
+ *
+ * @physPortName: pointer to char* that will receive @ifname's
+ *                phys_port_name from sysfs (null terminated
+ *                string). Could be NULL if @ifname's net driver doesn't
+ *                support phys_port_name (most netdev drivers
+ *                don't). Caller is responsible for freeing the string
+ *                when finished.
+ *
+ * Returns 0 on success or -1 on failure.
+ */
+int
+virNetDevGetPhysPortName(const char *ifname,
+                         char **physPortName)
+{
+    return virNetDevGetSysfsFileValue(ifname, "phys_port_name", physPortName);
 }
 
 
@@ -1433,6 +1465,17 @@ virNetDevGetPhysPortID(const char *ifname G_GNUC_UNUSED,
     return 0;
 }
 
+int
+virNetDevGetPhysPortName(const char *ifname G_GNUC_UNUSED,
+                       char **physPortName)
+{
+    /* this actually should never be called, and is just here to
+     * satisfy the linker.
+     */
+    *physPortName = NULL;
+    return 0;
+}
+
 int
 virNetDevGetVirtualFunctions(const char *pfname G_GNUC_UNUSED,
                              char ***vfname G_GNUC_UNUSED,
index f01601271880909cc987978e3241942ca583e670..e9349e7f593a141abfb1e95df9b48abcfe9b3bbd 100644 (file)
@@ -250,6 +250,10 @@ int virNetDevGetPhysPortID(const char *ifname,
                            char **physPortID)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
     G_GNUC_WARN_UNUSED_RESULT;
+int virNetDevGetPhysPortName(const char *ifname,
+                           char **physPortName)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
+    G_GNUC_WARN_UNUSED_RESULT;
 
 int virNetDevGetVirtualFunctions(const char *pfname,
                                  char ***vfname,