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

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



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

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

Signed-off-by: Roopa Prabhu<roprabhu cisco com>
Signed-off-by: Christian Benvenuti<benve cisco com>
Signed-off-by: David Wang<dwang2 cisco com>
---
  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


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