Re: [libvirt] [PATCH] replace 'if' type conditions with 'switch' for VIR_NETWORK_FORWARD_*

2018-07-20 Thread Shi Lei



On Fri, July 20, 2018 at 4:52 PM, Erik wrote:
> On Fri, Jul 20, 2018 at 12:00:53PM +0800, Shi Lei wrote:
> > Hi, everyone!
> > For VIR_NETWORK_FORWARD_*, I try to replace 'if' type conditions
> > with typed 'switch()'.
> > It might be more clear.
> >
> > Signed-off-by: Shi Lei 
> >
> > ---
> 
> ...
> 
> > +networkRemoveFirewallRules(def);
> > +if (networkAddFirewallRules(def) < 0) {
> > +/* failed to add but already logged */
> > +}
> 
> ^This if should go away, just call networkAddFirewallRules(def);

OK. 

> 
> ...
> 
> Conceptually, I agree with your changes, however, since you're already doing
> the convert, recently we've changed our practice on how we approach the
> switches. First, you should typecast def->forward.type to 
> virNetworkForwardType
> explicitly, so that we can utilize compile time checks. Second, we use the
> default case only to report an out of range error with virReportEnumRangeError
> (mainly because the values you care about are all already enumerated), you can
> find plenty of examples throughout the code base if you look for the function 
> I
> just mentioned. No other objections from my side.
> 
> Regards,
> Erik
> 

Thanks for your reviewing and directions, Erik!
And I have found examples you mentioned.
Have a nice day!

Shi Lei

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH] replace 'if' type conditions with 'switch' for VIR_NETWORK_FORWARD_*

2018-07-20 Thread Erik Skultety
On Fri, Jul 20, 2018 at 12:00:53PM +0800, Shi Lei wrote:
> Hi, everyone!
> For VIR_NETWORK_FORWARD_*, I try to replace 'if' type conditions
> with typed 'switch()'.
> It might be more clear.
>
> Signed-off-by: Shi Lei 
>
> ---

...

> +networkRemoveFirewallRules(def);
> +if (networkAddFirewallRules(def) < 0) {
> +/* failed to add but already logged */
> +}

^This if should go away, just call networkAddFirewallRules(def);

...

Conceptually, I agree with your changes, however, since you're already doing
the convert, recently we've changed our practice on how we approach the
switches. First, you should typecast def->forward.type to virNetworkForwardType
explicitly, so that we can utilize compile time checks. Second, we use the
default case only to report an out of range error with virReportEnumRangeError
(mainly because the values you care about are all already enumerated), you can
find plenty of examples throughout the code base if you look for the function I
just mentioned. No other objections from my side.

Regards,
Erik

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH] replace 'if' type conditions with 'switch' for VIR_NETWORK_FORWARD_*

2018-07-19 Thread Shi Lei
Hi, everyone!
For VIR_NETWORK_FORWARD_*, I try to replace 'if' type conditions
with typed 'switch()'.
It might be more clear.

Signed-off-by: Shi Lei 

---
 src/conf/domain_conf.c   |  46 +-
 src/conf/network_conf.c  |  49 ++-
 src/conf/virnetworkobj.c |  15 ++--
 src/esx/esx_network_driver.c |   8 +-
 src/network/bridge_driver.c  | 203 +++
 5 files changed, 178 insertions(+), 143 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 178c6d2..c02543f 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -29975,40 +29975,44 @@ virDomainNetResolveActualType(virDomainNetDefPtr 
iface)
 if (!(def = virNetworkDefParseString(xml)))
 goto cleanup;
 
-if ((def->forward.type == VIR_NETWORK_FORWARD_NONE) ||
-(def->forward.type == VIR_NETWORK_FORWARD_NAT) ||
-(def->forward.type == VIR_NETWORK_FORWARD_ROUTE) ||
-(def->forward.type == VIR_NETWORK_FORWARD_OPEN)) {
+switch (def->forward.type) {
+case VIR_NETWORK_FORWARD_NONE:
+case VIR_NETWORK_FORWARD_NAT:
+case VIR_NETWORK_FORWARD_ROUTE:
+case VIR_NETWORK_FORWARD_OPEN:
 /* for these forward types, the actual net type really *is*
  * NETWORK; we just keep the info from the portgroup in
  * iface->data.network.actual
  */
 ret = VIR_DOMAIN_NET_TYPE_NETWORK;
+break;
 
-} else if ((def->forward.type == VIR_NETWORK_FORWARD_BRIDGE) &&
-   def->bridge) {
-
-/*  
- * is VIR_DOMAIN_NET_TYPE_BRIDGE
- */
-
-ret = VIR_DOMAIN_NET_TYPE_BRIDGE;
-
-} else if (def->forward.type == VIR_NETWORK_FORWARD_HOSTDEV) {
-
+case VIR_NETWORK_FORWARD_HOSTDEV:
 ret = VIR_DOMAIN_NET_TYPE_HOSTDEV;
+break;
+
+case VIR_NETWORK_FORWARD_BRIDGE:
+if (def->bridge) {
+/*  
+ * is VIR_DOMAIN_NET_TYPE_BRIDGE
+ */
+ret = VIR_DOMAIN_NET_TYPE_BRIDGE;
+break;
+}
 
-} else if ((def->forward.type == VIR_NETWORK_FORWARD_BRIDGE) ||
-   (def->forward.type == VIR_NETWORK_FORWARD_PRIVATE) ||
-   (def->forward.type == VIR_NETWORK_FORWARD_VEPA) ||
-   (def->forward.type == VIR_NETWORK_FORWARD_PASSTHROUGH)) {
+/* intentionally fall through to the direct case for
+ * VIR_NETWORK_FORWARD_BRIDGE with no bridge device defined
+ */
+ATTRIBUTE_FALLTHROUGH;
 
+case VIR_NETWORK_FORWARD_PRIVATE:
+case VIR_NETWORK_FORWARD_VEPA:
+case VIR_NETWORK_FORWARD_PASSTHROUGH:
 /*  are all
  * VIR_DOMAIN_NET_TYPE_DIRECT.
  */
-
 ret = VIR_DOMAIN_NET_TYPE_DIRECT;
-
+break;
 }
 
  cleanup:
diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index 630a87f..bafc1d3 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -1959,17 +1959,22 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt)
 
 VIR_FREE(stp);
 
-if (def->mtu &&
-(def->forward.type != VIR_NETWORK_FORWARD_NONE &&
- def->forward.type != VIR_NETWORK_FORWARD_NAT &&
- def->forward.type != VIR_NETWORK_FORWARD_ROUTE &&
- def->forward.type != VIR_NETWORK_FORWARD_OPEN)) {
-virReportError(VIR_ERR_XML_ERROR,
-   _("mtu size only allowed in open, route, nat, "
- "and isolated mode, not in %s (network '%s')"),
-   virNetworkForwardTypeToString(def->forward.type),
-   def->name);
-goto error;
+if (def->mtu) {
+switch (def->forward.type) {
+case VIR_NETWORK_FORWARD_NONE:
+case VIR_NETWORK_FORWARD_NAT:
+case VIR_NETWORK_FORWARD_ROUTE:
+case VIR_NETWORK_FORWARD_OPEN:
+break;
+
+default:
+virReportError(VIR_ERR_XML_ERROR,
+   _("mtu size only allowed in open, route, nat, "
+ "and isolated mode, not in %s (network '%s')"),
+   virNetworkForwardTypeToString(def->forward.type),
+   def->name);
+goto error;
+}
 }
 
 /* Extract custom metadata */
@@ -2349,6 +2354,7 @@ virNetworkDefFormatBuf(virBufferPtr buf,
 char uuidstr[VIR_UUID_STRING_BUFLEN];
 size_t i;
 bool shortforward;
+bool hasbridge = false;
 
 virBufferAddLit(buf, "connections > 0))
@@ -2469,22 +2475,21 @@ virNetworkDefFormatBuf(virBufferPtr buf,
 virBufferAddLit(buf, "\n");
 }
 
+switch (def->forward.type) {
+case VIR_NETWORK_FORWARD_NONE:
+case VIR_NETWORK_FORWARD_NAT:
+case VIR_NETWORK_FORWARD_ROUTE:
+case VIR_NETWORK_FORWARD_OPEN:
+hasbridge = true;
+break;
+}
 
-if (def->forward.type == VIR_NETWORK_FORWARD_NONE ||
-def->forward.type == VIR_NETWORK_FORWARD_NAT ||
-def->forward.type ==