Re: [libvirt PATCH] esx: implement connectListAllNetworks

2020-09-14 Thread Pino Toscano
On Monday, 14 September 2020 17:16:50 CEST Michal Privoznik wrote:
> On 9/14/20 11:10 AM, Pino Toscano wrote:
> > Implement the .connectListAllNetworks networks API in the esx network
> > driver.
> > 
> > Signed-off-by: Pino Toscano 
> > ---
> >   src/esx/esx_network_driver.c | 64 
> >   1 file changed, 64 insertions(+)
> > 
> > diff --git a/src/esx/esx_network_driver.c b/src/esx/esx_network_driver.c
> > index 88843aae2f..5d9c1e3c2c 100644
> > --- a/src/esx/esx_network_driver.c
> > +++ b/src/esx/esx_network_driver.c
> > @@ -863,6 +863,69 @@ esxNetworkIsPersistent(virNetworkPtr network 
> > G_GNUC_UNUSED)
> >   
> >   
> >   
> > +#define MATCH(FLAG) (flags & (FLAG))
> > +static int
> > +esxConnectListAllNetworks(virConnectPtr conn,
> > +  virNetworkPtr **nets,
> > +  unsigned int flags)
> > +{
> > +int ret = -1;
> > +esxPrivate *priv = conn->privateData;
> > +esxVI_HostVirtualSwitch *hostVirtualSwitchList = NULL;
> > +esxVI_HostVirtualSwitch *hostVirtualSwitch = NULL;
> > +size_t count = 0;
> > +size_t i;
> > +
> > +virCheckFlags(VIR_CONNECT_LIST_NETWORKS_FILTERS_ALL, -1);
> > +
> > +/*
> > + * ESX networks are always active, persistent, and
> > + * autostarted, so return zero elements in case we are asked
> > + * for networks different than that.
> > + */
> > +if (MATCH(VIR_CONNECT_LIST_NETWORKS_FILTERS_ACTIVE) &&
> > +!(MATCH(VIR_CONNECT_LIST_NETWORKS_ACTIVE)))
> > +return 0;
> > +if (MATCH(VIR_CONNECT_LIST_NETWORKS_FILTERS_PERSISTENT) &&
> > +!(MATCH(VIR_CONNECT_LIST_NETWORKS_PERSISTENT)))
> > +return 0;
> > +if (MATCH(VIR_CONNECT_LIST_NETWORKS_FILTERS_AUTOSTART) &&
> > +!(MATCH(VIR_CONNECT_LIST_NETWORKS_AUTOSTART)))
> > +return 0;
> > +
> > +if (esxVI_EnsureSession(priv->primary) < 0 ||
> > +esxVI_LookupHostVirtualSwitchList(priv->primary,
> > +  ) < 0) {
> > +return -1;
> > +}
> > +
> > +for (hostVirtualSwitch = hostVirtualSwitchList; hostVirtualSwitch;
> > + hostVirtualSwitch = hostVirtualSwitch->_next) {
> > +virNetworkPtr net = virtualswitchToNetwork(conn, 
> > hostVirtualSwitch);
> > +
> > +if (VIR_APPEND_ELEMENT(*nets, count, net) < 0)
> 
> The virConnectListAllNetworks() documents that @nets can be NULL if the 
> caller is interested only in the count of networks.

Ah, I missed that bit, thanks. I will send v2 with a tweaked version.

Thanks,
-- 
Pino Toscano

signature.asc
Description: This is a digitally signed message part.


Re: [libvirt PATCH] esx: implement connectListAllNetworks

2020-09-14 Thread Michal Privoznik

On 9/14/20 11:10 AM, Pino Toscano wrote:

Implement the .connectListAllNetworks networks API in the esx network
driver.

Signed-off-by: Pino Toscano 
---
  src/esx/esx_network_driver.c | 64 
  1 file changed, 64 insertions(+)

diff --git a/src/esx/esx_network_driver.c b/src/esx/esx_network_driver.c
index 88843aae2f..5d9c1e3c2c 100644
--- a/src/esx/esx_network_driver.c
+++ b/src/esx/esx_network_driver.c
@@ -863,6 +863,69 @@ esxNetworkIsPersistent(virNetworkPtr network G_GNUC_UNUSED)
  
  
  
+#define MATCH(FLAG) (flags & (FLAG))

+static int
+esxConnectListAllNetworks(virConnectPtr conn,
+  virNetworkPtr **nets,
+  unsigned int flags)
+{
+int ret = -1;
+esxPrivate *priv = conn->privateData;
+esxVI_HostVirtualSwitch *hostVirtualSwitchList = NULL;
+esxVI_HostVirtualSwitch *hostVirtualSwitch = NULL;
+size_t count = 0;
+size_t i;
+
+virCheckFlags(VIR_CONNECT_LIST_NETWORKS_FILTERS_ALL, -1);
+
+/*
+ * ESX networks are always active, persistent, and
+ * autostarted, so return zero elements in case we are asked
+ * for networks different than that.
+ */
+if (MATCH(VIR_CONNECT_LIST_NETWORKS_FILTERS_ACTIVE) &&
+!(MATCH(VIR_CONNECT_LIST_NETWORKS_ACTIVE)))
+return 0;
+if (MATCH(VIR_CONNECT_LIST_NETWORKS_FILTERS_PERSISTENT) &&
+!(MATCH(VIR_CONNECT_LIST_NETWORKS_PERSISTENT)))
+return 0;
+if (MATCH(VIR_CONNECT_LIST_NETWORKS_FILTERS_AUTOSTART) &&
+!(MATCH(VIR_CONNECT_LIST_NETWORKS_AUTOSTART)))
+return 0;
+
+if (esxVI_EnsureSession(priv->primary) < 0 ||
+esxVI_LookupHostVirtualSwitchList(priv->primary,
+  ) < 0) {
+return -1;
+}
+
+for (hostVirtualSwitch = hostVirtualSwitchList; hostVirtualSwitch;
+ hostVirtualSwitch = hostVirtualSwitch->_next) {
+virNetworkPtr net = virtualswitchToNetwork(conn, hostVirtualSwitch);
+
+if (VIR_APPEND_ELEMENT(*nets, count, net) < 0)


The virConnectListAllNetworks() documents that @nets can be NULL if the 
caller is interested only in the count of networks. I think that direct 
dereference will lead to a crash. I think we want this body look a bit 
different then:


if (nets) {
if (VIR_APPEND_ELEMENT(*nets, count, net) < 0)
goto cleanup;
} else {
count = ++count;
}



+goto cleanup;
+}
+
+ret = count;
+
+ cleanup:
+if (ret < 0) {
+if (*nets) {


And this needs to be: if (nets && *nets) ...


+for (i = 0; i < count; ++i)
+VIR_FREE((*nets)[i]);
+VIR_FREE(*nets);
+}
+}
+
+esxVI_HostVirtualSwitch_Free();
+
+return ret;
+}
+#undef MATCH
+
+
+
  virNetworkDriver esxNetworkDriver = {
  .connectNumOfNetworks = esxConnectNumOfNetworks, /* 0.10.0 */
  .connectListNetworks = esxConnectListNetworks, /* 0.10.0 */
@@ -877,4 +940,5 @@ virNetworkDriver esxNetworkDriver = {
  .networkSetAutostart = esxNetworkSetAutostart, /* 0.10.0 */
  .networkIsActive = esxNetworkIsActive, /* 0.10.0 */
  .networkIsPersistent = esxNetworkIsPersistent, /* 0.10.0 */
+.connectListAllNetworks = esxConnectListAllNetworks, /* 6.8.0 */
  };



Reviewed-by: Michal Privoznik 

Michal



[libvirt PATCH] esx: implement connectListAllNetworks

2020-09-14 Thread Pino Toscano
Implement the .connectListAllNetworks networks API in the esx network
driver.

Signed-off-by: Pino Toscano 
---
 src/esx/esx_network_driver.c | 64 
 1 file changed, 64 insertions(+)

diff --git a/src/esx/esx_network_driver.c b/src/esx/esx_network_driver.c
index 88843aae2f..5d9c1e3c2c 100644
--- a/src/esx/esx_network_driver.c
+++ b/src/esx/esx_network_driver.c
@@ -863,6 +863,69 @@ esxNetworkIsPersistent(virNetworkPtr network G_GNUC_UNUSED)
 
 
 
+#define MATCH(FLAG) (flags & (FLAG))
+static int
+esxConnectListAllNetworks(virConnectPtr conn,
+  virNetworkPtr **nets,
+  unsigned int flags)
+{
+int ret = -1;
+esxPrivate *priv = conn->privateData;
+esxVI_HostVirtualSwitch *hostVirtualSwitchList = NULL;
+esxVI_HostVirtualSwitch *hostVirtualSwitch = NULL;
+size_t count = 0;
+size_t i;
+
+virCheckFlags(VIR_CONNECT_LIST_NETWORKS_FILTERS_ALL, -1);
+
+/*
+ * ESX networks are always active, persistent, and
+ * autostarted, so return zero elements in case we are asked
+ * for networks different than that.
+ */
+if (MATCH(VIR_CONNECT_LIST_NETWORKS_FILTERS_ACTIVE) &&
+!(MATCH(VIR_CONNECT_LIST_NETWORKS_ACTIVE)))
+return 0;
+if (MATCH(VIR_CONNECT_LIST_NETWORKS_FILTERS_PERSISTENT) &&
+!(MATCH(VIR_CONNECT_LIST_NETWORKS_PERSISTENT)))
+return 0;
+if (MATCH(VIR_CONNECT_LIST_NETWORKS_FILTERS_AUTOSTART) &&
+!(MATCH(VIR_CONNECT_LIST_NETWORKS_AUTOSTART)))
+return 0;
+
+if (esxVI_EnsureSession(priv->primary) < 0 ||
+esxVI_LookupHostVirtualSwitchList(priv->primary,
+  ) < 0) {
+return -1;
+}
+
+for (hostVirtualSwitch = hostVirtualSwitchList; hostVirtualSwitch;
+ hostVirtualSwitch = hostVirtualSwitch->_next) {
+virNetworkPtr net = virtualswitchToNetwork(conn, hostVirtualSwitch);
+
+if (VIR_APPEND_ELEMENT(*nets, count, net) < 0)
+goto cleanup;
+}
+
+ret = count;
+
+ cleanup:
+if (ret < 0) {
+if (*nets) {
+for (i = 0; i < count; ++i)
+VIR_FREE((*nets)[i]);
+VIR_FREE(*nets);
+}
+}
+
+esxVI_HostVirtualSwitch_Free();
+
+return ret;
+}
+#undef MATCH
+
+
+
 virNetworkDriver esxNetworkDriver = {
 .connectNumOfNetworks = esxConnectNumOfNetworks, /* 0.10.0 */
 .connectListNetworks = esxConnectListNetworks, /* 0.10.0 */
@@ -877,4 +940,5 @@ virNetworkDriver esxNetworkDriver = {
 .networkSetAutostart = esxNetworkSetAutostart, /* 0.10.0 */
 .networkIsActive = esxNetworkIsActive, /* 0.10.0 */
 .networkIsPersistent = esxNetworkIsPersistent, /* 0.10.0 */
+.connectListAllNetworks = esxConnectListAllNetworks, /* 6.8.0 */
 };
-- 
2.26.2