[libvirt] [PATCH 29/50] list: Define new API virConnectListAllInterfaces

Osier Yang jyang at redhat.com
Thu Jul 19 16:41:00 UTC 2012


This is to list the interface objects, supported filtering flags
are: active|inactive.

include/libvirt/libvirt.h.in: Declare enum virConnectListAllInterfaceFlags
                              and virConnectListAllInterfaces.
python/generator.py: Skip auto-generating
src/driver.h: (virDrvConnectListAllInterfaces)
src/libvirt.c: Implement the public API
src/libvirt_public.syms: Export the symbol to public
---
 include/libvirt/libvirt.h.in |   13 +++++++++
 python/generator.py          |    1 +
 src/driver.h                 |    5 +++
 src/libvirt.c                |   59 ++++++++++++++++++++++++++++++++++++++++++
 src/libvirt_public.syms      |    1 +
 5 files changed, 79 insertions(+), 0 deletions(-)

diff --git a/include/libvirt/libvirt.h.in b/include/libvirt/libvirt.h.in
index b6199cc..2b8106e 100644
--- a/include/libvirt/libvirt.h.in
+++ b/include/libvirt/libvirt.h.in
@@ -2303,6 +2303,19 @@ int                     virConnectNumOfDefinedInterfaces (virConnectPtr conn);
 int                     virConnectListDefinedInterfaces  (virConnectPtr conn,
                                                           char **const names,
                                                           int maxnames);
+/*
+ * virConnectListAllInterfaces:
+ *
+ * Flags used to filter the returned interfaces.
+ */
+typedef enum {
+    VIR_CONNECT_LIST_INTERFACES_INACTIVE      = 1 << 0,
+    VIR_CONNECT_LIST_INTERFACES_ACTIVE        = 1 << 1,
+} virConnectListAllInterfacesFlags;
+
+int                     virConnectListAllInterfaces (virConnectPtr conn,
+                                                     virInterfacePtr **ifaces,
+                                                     unsigned int flags);
 
 virInterfacePtr         virInterfaceLookupByName  (virConnectPtr conn,
                                                    const char *name);
diff --git a/python/generator.py b/python/generator.py
index 44b56c2..51c3fce 100755
--- a/python/generator.py
+++ b/python/generator.py
@@ -459,6 +459,7 @@ skip_function = (
     'virConnectListAllStoragePools', # overridden in virConnect.py
     'virStoragePoolListAllVolumes', # overridden in virStoragePool.py
     'virConnectListAllNetworks', # overridden in virConnect.py
+    'virConnectListAllInterfaces', # overridden in virConnect.py
 
     'virStreamRecvAll', # Pure python libvirt-override-virStream.py
     'virStreamSendAll', # Pure python libvirt-override-virStream.py
diff --git a/src/driver.h b/src/driver.h
index 68a1f04..335ee86 100644
--- a/src/driver.h
+++ b/src/driver.h
@@ -1146,6 +1146,10 @@ typedef int
         (*virDrvListDefinedInterfaces)  (virConnectPtr conn,
                                          char **const names,
                                          int maxnames);
+typedef int
+        (*virDrvListAllInterfaces)      (virConnectPtr conn,
+                                         virInterfacePtr **ifaces,
+                                         unsigned int flags);
 typedef virInterfacePtr
         (*virDrvInterfaceLookupByName)  (virConnectPtr conn,
                                          const char *name);
@@ -1204,6 +1208,7 @@ struct _virInterfaceDriver {
     virDrvListInterfaces             listInterfaces;
     virDrvNumOfDefinedInterfaces     numOfDefinedInterfaces;
     virDrvListDefinedInterfaces      listDefinedInterfaces;
+    virDrvListAllInterfaces          listAllInterfaces;
     virDrvInterfaceLookupByName      interfaceLookupByName;
     virDrvInterfaceLookupByMACString interfaceLookupByMACString;
     virDrvInterfaceGetXMLDesc        interfaceGetXMLDesc;
diff --git a/src/libvirt.c b/src/libvirt.c
index 622a590..c50bb3d 100644
--- a/src/libvirt.c
+++ b/src/libvirt.c
@@ -10507,6 +10507,65 @@ virInterfaceGetConnect (virInterfacePtr iface)
 }
 
 /**
+ * virConnectListAllInterfaces:
+ * @conn: Pointer to the hypervisor connection.
+ * @ifaces: Pointer to a variable to store the array containing the interface
+ *          objects or NULL if the list is not required (just returns number
+ *          of interfaces).
+ * @flags: bitwise-OR of virConnectListAllInterfacesFlags.
+ *
+ * Collect the list of interfaces, and allocate an array to store those
+ * objects. This API solves the race inherent between virConnectListInterfaces
+ * and virConnectListDefinedInterfaces.
+ *
+ * By default, this API covers all interfaces; it is also possible to return
+ * the filtered objects with flags. Filters are provided in groups, where each
+ * group contains bits that describe mutually exclusive attributes of a interface.
+ *
+ * The only one group of @flags is VIR_CONNECT_LIST_INTERFACES_INACTIVE and
+ * VIR_CONNECT_LIST_INTERFACES_INACTIVE to fitler the interfaces by state.
+ *
+ * Returns the number of interfaces found or -1 and sets @ifaces to  NULL in case
+ * of error.  On success, the array stored into @ifaces is guaranteed to have an
+ * extra allocated element set to NULL but not included in the return count,
+ * to make iteration easier.  The caller is responsible for calling
+ * virStorageInterfaceFree() on each array element, then calling free() on @ifaces.
+ */
+int
+virConnectListAllInterfaces(virConnectPtr conn,
+                            virInterfacePtr **ifaces,
+                            unsigned int flags)
+{
+    VIR_DEBUG("conn=%p, ifaces=%p, flags=%x", conn, ifaces, flags);
+
+    virResetLastError();
+
+    if (ifaces)
+        *ifaces = NULL;
+
+    if (!VIR_IS_CONNECT(conn)) {
+        virLibConnError(VIR_ERR_INVALID_CONN, __FUNCTION__);
+        virDispatchError(NULL);
+        return -1;
+    }
+
+    if (conn->interfaceDriver &&
+        conn->interfaceDriver->listAllInterfaces) {
+        int ret;
+        ret = conn->interfaceDriver->listAllInterfaces(conn, ifaces, flags);
+        if (ret < 0)
+            goto error;
+        return ret;
+    }
+
+    virLibConnError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
+
+error:
+    virDispatchError(conn);
+    return -1;
+}
+
+/**
  * virConnectNumOfInterfaces:
  * @conn: pointer to the hypervisor connection
  *
diff --git a/src/libvirt_public.syms b/src/libvirt_public.syms
index 0ce120a..298eaac 100644
--- a/src/libvirt_public.syms
+++ b/src/libvirt_public.syms
@@ -549,6 +549,7 @@ LIBVIRT_0.9.14 {
         virConnectListAllStoragePools;
         virStoragePoolListAllVolumes;
         virConnectListAllNetworks;
+        virConnectListAllInterfaces;
 } LIBVIRT_0.9.13;
 
 # .... define new API here using predicted next version number ....
-- 
1.7.7.3




More information about the libvir-list mailing list