Re: [PATCH 1/3 RFC] Add function to get the network interface name of a pci device

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 08/01/2011 07:57 PM, Roopa Prabhu wrote:
From: Roopa Prabhu<roprabhu@xxxxxxxxx>

This function looks at sysfs "net" directory to get the network interface
name associated with the pci device

Signed-off-by: Roopa Prabhu<roprabhu@xxxxxxxxx>
Signed-off-by: Christian Benvenuti<benve@xxxxxxxxx>
Signed-off-by: David Wang<dwang2@xxxxxxxxx>
---
  src/util/pci.c |   42 ++++++++++++++++++++++++++++++++++++++++++
  src/util/pci.h |    2 ++
  2 files changed, 44 insertions(+), 0 deletions(-)


diff --git a/src/util/pci.c b/src/util/pci.c
index a79c164..d2deeef 100644
--- a/src/util/pci.c
+++ b/src/util/pci.c
@@ -1679,3 +1679,45 @@ int pciDeviceIsAssignable(pciDevice *dev,

      return 1;
  }
+
+/*
+ * This function returns the network device name
+ * of a pci device
+ */
+int
+pciDeviceGetNetName(char *device_link_sysfs_path, char **netname)
+{
+        char *pcidev_sysfs_net_path = NULL;
+        int ret = -1;
+        DIR *dir = NULL;
+        struct dirent *entry = NULL;
+
+        if (virBuildPath(&pcidev_sysfs_net_path, device_link_sysfs_path,
+            "net") == -1) {
+            virReportOOMError();
+            return -1;
+        }
+
+        dir = opendir(pcidev_sysfs_net_path);
+        if (dir == NULL)
+            goto out;
+
+        while ((entry = readdir(dir))) {
+               if (entry->d_name[0] == '.' ||
The above check makes the following one obsolete. If all entries with first letter '.' can be skipped, then you could just keep the first one, otherwise I'd also use !strcmp(entry->d_name, ".").
+                   !strcmp(entry->d_name, ".."))
+                   continue;
+
+               /* Assume a single directory entry */
+               *netname = strdup(entry->d_name);
+               ret = 0;
+               break;
+        }
+
+        if (dir)
+            closedir(dir);
Check for 'dir != NULL) is not necessary due to goto above.
+
+out:
+        VIR_FREE(pcidev_sysfs_net_path);
+
+        return ret;
+}
diff --git a/src/util/pci.h b/src/util/pci.h
index a351baf..fa25472 100644
--- a/src/util/pci.h
+++ b/src/util/pci.h
@@ -74,4 +74,6 @@ int pciDeviceIsAssignable(pciDevice *dev,
                            int strict_acs_check);
  int pciWaitForDeviceCleanup(pciDevice *dev, const char *matcher);

+int pciDeviceGetNetName(char *device_link_sysfs_path, char **netname);
+
  #endif /* __VIR_PCI_H__ */

   Stefan

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]