On 09/04/2012 11:55 AM, Osier Yang wrote: > src/conf/network_conf.c: Add virNetworkMatch to filter the networks; > and virNetworkList to iterate over all the networks with the filter. > > src/conf/network_conf.h: Declare virNetworkList and define the macros > for filters. > > src/libvirt_private.syms: Export virNetworkList. > --- > src/conf/network_conf.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++ > src/conf/network_conf.h | 22 +++++++++++ > src/libvirt_private.syms | 1 + > 3 files changed, 114 insertions(+), 0 deletions(-) > > diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c > index 9d53d8e..1ee0951 100644 > --- a/src/conf/network_conf.c > +++ b/src/conf/network_conf.c > @@ -2056,3 +2056,94 @@ void virNetworkObjUnlock(virNetworkObjPtr obj) > { > virMutexUnlock(&obj->lock); > } > + > +#define MATCH(FLAG) (flags & (FLAG)) > +static bool > +virNetworkMatch (virNetworkObjPtr netobj, > + unsigned int flags) > +{ > + /* filter by active state */ > + if (MATCH(VIR_CONNECT_LIST_NETWORKS_FILTERS_ACTIVE) && > + !((MATCH(VIR_CONNECT_LIST_NETWORKS_ACTIVE) && > + virNetworkObjIsActive(netobj)) || > + (MATCH(VIR_CONNECT_LIST_NETWORKS_INACTIVE) && > + !virNetworkObjIsActive(netobj)))) > + return false; > + > + /* filter by persistence */ > + if (MATCH(VIR_CONNECT_LIST_NETWORKS_FILTERS_PERSISTENT) && > + !((MATCH(VIR_CONNECT_LIST_NETWORKS_PERSISTENT) && > + netobj->persistent) || > + (MATCH(VIR_CONNECT_LIST_NETWORKS_TRANSIENT) && > + !netobj->persistent))) > + return false; > + > + /* filter by autostart option */ > + if (MATCH(VIR_CONNECT_LIST_NETWORKS_FILTERS_AUTOSTART) && > + !((MATCH(VIR_CONNECT_LIST_NETWORKS_AUTOSTART) && > + netobj->autostart) || > + (MATCH(VIR_CONNECT_LIST_NETWORKS_NO_AUTOSTART) && > + !netobj->autostart))) > + return false; > + > + return true; > +} > +#undef MATCH > + > +int > +virNetworkList(virConnectPtr conn, > + virNetworkObjList netobjs, > + virNetworkPtr **nets, > + unsigned int flags) > +{ > + virNetworkPtr *tmp_nets = NULL; > + virNetworkPtr net = NULL; > + int nnets = 0; > + int ret = -1; > + int i; > + > + if (nets) { > + if (VIR_ALLOC_N(tmp_nets, netobjs.count + 1) < 0) { > + virReportOOMError(); > + goto cleanup; > + } > + } > + > + for (i = 0; i < netobjs.count; i++) { > + virNetworkObjPtr netobj = netobjs.objs[i]; > + virNetworkObjLock(netobj); > + if (virNetworkMatch(netobj, flags)) { > + if (nets) { > + if (!(net = virGetNetwork(conn, > + netobj->def->name, > + netobj->def->uuid))) { > + virNetworkObjUnlock(netobj); > + goto cleanup; > + } > + tmp_nets[nnets] = net; > + } > + nnets++; > + } > + virNetworkObjUnlock(netobj); > + } > + > + if (tmp_nets) { > + /* trim the array to the final size */ > + ignore_value(VIR_REALLOC_N(tmp_nets, nnets + 1)); > + *nets = tmp_nets; > + tmp_nets = NULL; > + } > + > + ret = nnets; > + > +cleanup: > + if (tmp_nets) { > + for (i = 0; i < nnets; i++) { > + if (tmp_nets[i]) > + virNetworkFree(tmp_nets[i]); > + } > + } > + > + VIR_FREE(tmp_nets); > + return ret; > +} > diff --git a/src/conf/network_conf.h b/src/conf/network_conf.h > index f49c367..5dbf50d 100644 > --- a/src/conf/network_conf.h > +++ b/src/conf/network_conf.h > @@ -321,4 +321,26 @@ void virNetworkObjUnlock(virNetworkObjPtr obj); > > VIR_ENUM_DECL(virNetworkForward) > > +# define VIR_CONNECT_LIST_NETWORKS_FILTERS_ACTIVE \ > + (VIR_CONNECT_LIST_NETWORKS_ACTIVE | \ > + VIR_CONNECT_LIST_NETWORKS_INACTIVE) > + > +# define VIR_CONNECT_LIST_NETWORKS_FILTERS_PERSISTENT \ > + (VIR_CONNECT_LIST_NETWORKS_PERSISTENT | \ > + VIR_CONNECT_LIST_NETWORKS_TRANSIENT) > + > +# define VIR_CONNECT_LIST_NETWORKS_FILTERS_AUTOSTART \ > + (VIR_CONNECT_LIST_NETWORKS_AUTOSTART | \ > + VIR_CONNECT_LIST_NETWORKS_NO_AUTOSTART) > + > +# define VIR_CONNECT_LIST_NETWORKS_FILTERS_ALL \ > + (VIR_CONNECT_LIST_NETWORKS_FILTERS_ACTIVE | \ > + VIR_CONNECT_LIST_NETWORKS_FILTERS_PERSISTENT | \ > + VIR_CONNECT_LIST_NETWORKS_FILTERS_AUTOSTART) > + > +int virNetworkList(virConnectPtr conn, > + virNetworkObjList netobjs, > + virNetworkPtr **nets, > + unsigned int flags); > + > #endif /* __NETWORK_CONF_H__ */ > diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms > index abfee47..7464c59 100644 > --- a/src/libvirt_private.syms > +++ b/src/libvirt_private.syms > @@ -837,6 +837,7 @@ virNetworkFindByName; > virNetworkFindByUUID; > virNetworkIpDefNetmask; > virNetworkIpDefPrefix; > +virNetworkList; > virNetworkLoadAllConfigs; > virNetworkObjIsDuplicate; > virNetworkObjListFree; ACK. -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list