On 09/04/2012 11:55 AM, Osier Yang wrote:
This is to list the network objects, supported filtering flags
are: active|inactive, persistent|transient, autostart|no-autostart.
include/libvirt/libvirt.h.in: Declare enum virConnectListAllNetworkFlags
and virConnectListAllNetworks.
python/generator.py: Skip auto-generating
src/driver.h: (virDrvConnectListAllNetworks)
src/libvirt.c: Implement the public API
src/libvirt_public.syms: Export the symbol to public
---
include/libvirt/libvirt.h.in | 20 ++++++++++
python/generator.py | 1 +
src/driver.h | 5 ++
src/libvirt.c | 86 +++++++++++++++++++++++++++++++++++++++++-
src/libvirt_public.syms | 1 +
5 files changed, 111 insertions(+), 2 deletions(-)
diff --git a/include/libvirt/libvirt.h.in b/include/libvirt/libvirt.h.in
index 58cd6ff..50d865c 100644
--- a/include/libvirt/libvirt.h.in
+++ b/include/libvirt/libvirt.h.in
@@ -2279,6 +2279,26 @@ int virConnectNumOfDefinedNetworks
(virConnectPtr conn);
int virConnectListDefinedNetworks (virConnectPtr conn,
char **const names,
int maxnames);
+/*
+ * virConnectListAllNetworks:
+ *
+ * Flags used to filter the returned networks. Flags in each group
+ * are exclusive attributes of a network.
+ */
+typedef enum {
+ VIR_CONNECT_LIST_NETWORKS_INACTIVE = 1 << 0,
+ VIR_CONNECT_LIST_NETWORKS_ACTIVE = 1 << 1,
+
+ VIR_CONNECT_LIST_NETWORKS_PERSISTENT = 1 << 2,
+ VIR_CONNECT_LIST_NETWORKS_TRANSIENT = 1 << 3,
+
+ VIR_CONNECT_LIST_NETWORKS_AUTOSTART = 1 << 4,
+ VIR_CONNECT_LIST_NETWORKS_NO_AUTOSTART = 1 << 5,
+} virConnectListAllNetworksFlags;
+
+int virConnectListAllNetworks (virConnectPtr conn,
+ virNetworkPtr **nets,
+ unsigned int flags);
/*
* Lookup network by name or uuid
diff --git a/python/generator.py b/python/generator.py
index 276b4ff..a9a401b 100755
--- a/python/generator.py
+++ b/python/generator.py
@@ -462,6 +462,7 @@ skip_function = (
'virDomainSnapshotListAllChildren', # overridden in virDomainSnapshot.py
'virConnectListAllStoragePools', # overridden in virConnect.py
'virStoragePoolListAllVolumes', # overridden in virStoragePool.py
+ 'virConnectListAllNetworks', # 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 1bdfd29..534da05 100644
--- a/src/driver.h
+++ b/src/driver.h
@@ -1082,6 +1082,10 @@ typedef int
(*virDrvListDefinedNetworks) (virConnectPtr conn,
char **const names,
int maxnames);
+typedef int
+ (*virDrvListAllNetworks) (virConnectPtr conn,
+ virNetworkPtr **nets,
+ unsigned int flags);
typedef virNetworkPtr
(*virDrvNetworkLookupByUUID) (virConnectPtr conn,
const unsigned char *uuid);
@@ -1140,6 +1144,7 @@ struct _virNetworkDriver {
virDrvListNetworks listNetworks;
virDrvNumOfDefinedNetworks numOfDefinedNetworks;
virDrvListDefinedNetworks listDefinedNetworks;
+ virDrvListAllNetworks listAllNetworks;
virDrvNetworkLookupByUUID networkLookupByUUID;
virDrvNetworkLookupByName networkLookupByName;
virDrvNetworkCreateXML networkCreateXML;
diff --git a/src/libvirt.c b/src/libvirt.c
index 700dbef..686af8a 100644
--- a/src/libvirt.c
+++ b/src/libvirt.c
@@ -9799,6 +9799,76 @@ virNetworkGetConnect (virNetworkPtr net)
}
/**
+ * virConnectListAllNetworks:
+ * @conn: Pointer to the hypervisor connection.
+ * @nets: Pointer to a variable to store the array containing the network
+ * objects or NULL if the list is not required (just returns number
+ * of networks).
+ * @flags: bitwise-OR of virConnectListAllNetworksFlags.
+ *
+ * Collect the list of networks, and allocate an array to store those
+ * objects. This API solves the race inherent between virConnectListNetworks
+ * and virConnectListDefinedNetworks.
+ *
+ * Normally, all networks are returned; however, @flags can be used to
+ * filter the results for a smaller list of targeted networks. The valid
+ * flags are divided into groups, where each group contains bits that
+ * describe mutually exclusive attributes of a network, and where all bits
+ * within a group describe all possible networks.
+ *
+ * The first group of @flags is VIR_CONNECT_LIST_NETWORKS_ACTIVE (up) and
+ * VIR_CONNECT_LIST_NETWORKS_INACTIVE (down) to filter the networks by state.
+ *
+ * The second group of @flags is VIR_CONNECT_LIST_NETWORKS_PERSISTENT (defined)
+ * and VIR_CONNECT_LIST_NETWORKS_TRANSIENT (running but not defined), to filter
+ * the networks by whether they have persistent config or not.
+ *
+ * The third group of @flags is VIR_CONNECT_LIST_NETWORKS_AUTOSTART
+ * and VIR_CONNECT_LIST_NETWORKS_NO_AUTOSTART, to filter the networks by
+ * whether they are marked as autostart or not.
+ *
+ * Returns the number of networks found or -1 and sets @nets to NULL in case
+ * of error. On success, the array stored into @nets 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
+ * virNetworkFree() on each array element, then calling free() on @nets.
+ */
+int
+virConnectListAllNetworks(virConnectPtr conn,
+ virNetworkPtr **nets,
+ unsigned int flags)
+{
+ VIR_DEBUG("conn=%p, nets=%p, flags=%x", conn, nets, flags);
+
+ virResetLastError();
+
+ if (nets)
+ *nets = NULL;
+
+ if (!VIR_IS_CONNECT(conn)) {
+ virLibConnError(VIR_ERR_INVALID_CONN, __FUNCTION__);
+ virDispatchError(NULL);
+ return -1;
+ }
+
+ if (conn->networkDriver &&
+ conn->networkDriver->listAllNetworks) {
+ int ret;
+ ret = conn->networkDriver->listAllNetworks(conn, nets, flags);
+ if (ret < 0)
+ goto error;
+ return ret;
+ }
+
+ virLibConnError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
+
+error:
+ virDispatchError(conn);
+ return -1;
+}
+
+
+/**
* virConnectNumOfNetworks:
* @conn: pointer to the hypervisor connection
*
@@ -9842,7 +9912,13 @@ error:
*
* Collect the list of active networks, and store their names in @names
*
- * Returns the number of networks found or -1 in case of error
+ * For more control over the results, see virConnectListAllNetworks().
+ *
+ * Returns the number of networks found or -1 in case of error. Note that
+ * this command is inherently racy; a network can be started between a call
+ * to virConnectNumOfNetworks() and this call; you are only guaranteed that
+ * all currently active networks were listed if the return is less than
+ * @maxnames.
*/
int
virConnectListNetworks(virConnectPtr conn, char **const names, int maxnames)
@@ -9919,7 +9995,13 @@ error:
*
* list the inactive networks, stores the pointers to the names in @names
*
- * Returns the number of names provided in the array or -1 in case of error
+ * For more control over the results, see virConnectListAllNetworks().
+ *
+ * Returns the number of names provided in the array or -1 in case of error.
+ * Note that this command is inherently racy; a network can be defined between
+ * a call to virConnectNumOfDefinedNetworks() and this call; you are only
+ * guaranteed that all currently defined networks were listed if the return
+ * is less than @maxnames. The client must call free() on each returned name.
*/
int
virConnectListDefinedNetworks(virConnectPtr conn, char **const names,
diff --git a/src/libvirt_public.syms b/src/libvirt_public.syms
index 53db37f..6ff5a77 100644
--- a/src/libvirt_public.syms
+++ b/src/libvirt_public.syms
@@ -556,6 +556,7 @@ LIBVIRT_0.10.0 {
LIBVIRT_0.10.2 {
global:
+ virConnectListAllNetworks;
virConnectListAllStoragePools;
virStoragePoolListAllVolumes;
} LIBVIRT_0.10.0;
ACK. I think this horse has been beaten enough :-)