<p dir="ltr"></p>
<p dir="ltr">On Aug 18, 2016 5:01 AM, "Michal Privoznik" <<a href="mailto:mprivozn@redhat.com">mprivozn@redhat.com</a>> wrote:<br>
><br>
> On 12.08.2016 04:41, Laine Stump wrote:<br>
> > If you define a libvirt virtual network with one or more IP addresses,<br>
> > it starts up an instance of dnsmasq. It's always been possible to<br>
> > avoid dnsmasq's dhcp server (simply don't include a <dhcp> element),<br>
> > but until now it wasn't possible to avoid having the DNS server<br>
> > listening; even if the network has no <dns> element, it is started<br>
> > using default settings.<br>
> ><br>
> > This patch adds a new attribute to <dns>: enable='yes|no'. For<br>
> > backward compatibility, it defaults to 'yes', but if you don't want a<br>
> > DNS server created for the network, you can simply add:<br>
> ><br>
> >    <dns enable='no'/><br>
> ><br>
> > to the network configuration, and next time the network is started<br>
> > there will be no dns server created (if there is dhcp configuration,<br>
> > dnsmasq will be started with "port=0" which disables the DNS server;<br>
> > if there is no dhcp configuration, dnsmasq won't be started at all).<br>
> > ---<br>
> >  docs/<a href="http://formatnetwork.html.in">formatnetwork.html.in</a>                         |  12 ++<br>
> >  docs/schemas/network.rng                           |   5 +<br>
> >  src/conf/network_conf.c                            |  36 ++++-<br>
> >  src/conf/network_conf.h                            |   1 +<br>
> >  src/network/bridge_driver.c                        | 146 ++++++++++++---------<br>
> >  .../networkxml2confdata/routed-network-no-dns.conf |  11 ++<br>
> >  .../networkxml2confdata/routed-network-no-dns.xml  |  10 ++<br>
> >  tests/networkxml2conftest.c                        |   1 +<br>
> >  tests/networkxml2xmlin/routed-network-no-dns.xml   |  10 ++<br>
> >  tests/networkxml2xmlout/routed-network-no-dns.xml  |  12 ++<br>
> >  tests/networkxml2xmltest.c                         |   1 +<br>
> >  11 files changed, 179 insertions(+), 66 deletions(-)<br>
> >  create mode 100644 tests/networkxml2confdata/routed-network-no-dns.conf<br>
> >  create mode 100644 tests/networkxml2confdata/routed-network-no-dns.xml<br>
> >  create mode 100644 tests/networkxml2xmlin/routed-network-no-dns.xml<br>
> >  create mode 100644 tests/networkxml2xmlout/routed-network-no-dns.xml<br>
> ><br>
> > diff --git a/docs/<a href="http://formatnetwork.html.in">formatnetwork.html.in</a> b/docs/<a href="http://formatnetwork.html.in">formatnetwork.html.in</a><br>
> > index 12d1bed..e103dd7 100644<br>
> > --- a/docs/<a href="http://formatnetwork.html.in">formatnetwork.html.in</a><br>
> > +++ b/docs/<a href="http://formatnetwork.html.in">formatnetwork.html.in</a><br>
> > @@ -886,6 +886,18 @@<br>
> >          server <span class="since">Since 0.9.3</span>.<br>
> ><br>
> >          <p><br>
> > +          The dns element can have an optional <code>enable</code><br>
> > +          attribute <span class="since">Since 2.2.0</span>.<br>
> > +          If <code>enable</code> is "no", then no DNS server will be<br>
> > +          setup by libvirt for this network (and any other<br>
> > +          configuration in <code>&lt;dns&gt;</code> will be ignored).<br>
> > +          If <code>enable</code> is "yes" or unspecified (including<br>
> > +          the complete absence of any <code>&lt;dns&gt;</code><br>
> > +          element) then a DNS server will be setup by libvirt to<br>
> > +          listen on all IP addresses specified in the network's<br>
> > +          configuration.<br>
> > +        </p><br>
><br>
> Le sigh. I wish that we could just disable dns if the tag is not present<br>
> in the nework XML. But we can't do that, can we?<br>
></p>
<p dir="ltr">Nope :-(. Backward compatibility and all that.<br></p>
<p dir="ltr">> > +        <p><br>
> >            The dns element<br>
> >            can have an optional <code>forwardPlainNames</code><br>
> >            attribute <span class="since">Since 1.1.2</span>.<br>
><br>
> > diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c<br>
> > index 6820bde..490574f 100644<br>
> > --- a/src/conf/network_conf.c<br>
> > +++ b/src/conf/network_conf.c<br>
> > @@ -1335,6 +1335,7 @@ virNetworkDNSDefParseXML(const char *networkName,<br>
> >      xmlNodePtr *txtNodes = NULL;<br>
> >      xmlNodePtr *fwdNodes = NULL;<br>
> >      char *forwardPlainNames = NULL;<br>
> > +    char *enable = NULL;<br>
> >      int nhosts, nsrvs, ntxts, nfwds;<br>
> >      size_t i;<br>
> >      int ret = -1;<br>
> > @@ -1342,6 +1343,18 @@ virNetworkDNSDefParseXML(const char *networkName,<br>
> ><br>
> >      ctxt->node = node;<br>
> ><br>
> > +    enable = virXPathString("string(./@enable)", ctxt);<br>
> > +    if (enable) {<br>
> > +        def->enable = virTristateBoolTypeFromString(enable);<br>
> > +        if (def->enable <= 0) {<br>
> > +            virReportError(VIR_ERR_XML_ERROR,<br>
> > +                           _("Invalid dns enable setting '%s' "<br>
> > +                             "in network '%s'"),<br>
> > +                           enable, networkName);<br>
> > +            goto cleanup;<br>
> > +        }<br>
> > +    }<br>
> > +<br>
> >      forwardPlainNames = virXPathString("string(./@forwardPlainNames)", ctxt);<br>
> >      if (forwardPlainNames) {<br>
> >          def->forwardPlainNames = virTristateBoolTypeFromString(forwardPlainNames);<br>
> > @@ -1440,6 +1453,7 @@ virNetworkDNSDefParseXML(const char *networkName,<br>
> ><br>
> >      ret = 0;<br>
> >   cleanup:<br>
> > +    VIR_FREE(enable);<br>
> >      VIR_FREE(forwardPlainNames);<br>
> >      VIR_FREE(fwdNodes);<br>
> >      VIR_FREE(hostNodes);<br>
> > @@ -2496,12 +2510,22 @@ virNetworkDNSDefFormat(virBufferPtr buf,<br>
> >  {<br>
> >      size_t i, j;<br>
> ><br>
> > -    if (!(def->forwardPlainNames || def->nfwds || def->nhosts ||<br>
> > +    if (!(def->enable || def->forwardPlainNames || def->nfwds || def->nhosts ||<br>
> >            def->nsrvs || def->ntxts))<br>
> >          return 0;<br>
> ><br>
> >      virBufferAddLit(buf, "<dns");<br>
> > -    /* default to "yes", but don't format it in the XML */<br>
> > +    if (def->enable) {<br>
> > +        const char *fwd = virTristateBoolTypeToString(def->enable);<br>
> > +<br>
> > +        if (!fwd) {<br>
> > +            virReportError(VIR_ERR_INTERNAL_ERROR,<br>
> > +                           _("Unknown enable type %d in network"),<br>
> > +                           def->enable);<br>
> > +            return -1;<br>
><br>
> I don't think check is needed. We've validated the forward mode when<br>
> parsing the XML.<br>
></p>
<p dir="ltr">Depends on your philosophy. If you trust that nothing could have modified the value from the time it was parsed until the time it's formated, then yes. If you don't trust that (or are uncomfortable that a pointer from a function that could potentially return NULL is used as an argument to an sprintf-like function without checking for NULL), then no.</p>
<p dir="ltr">Libvirt code disagrees about this a lot, even within code written by the same person (e.g. me). These days I tend mor towards the "defensive programming" style of checking everything unless something in the direct call chain guarantees that it has been validated. <br></p>
<p dir="ltr">> Also, I think that we need slightly different approach here. I mean, for<br>
> "<dns enable='no'/>" case we just want to put that string into XML and<br>
> nothing more. With this code I'm able to get the following which makes<br>
> not much sense to me:<br>
><br>
>   <dns enable='no'><br>
>     <txt name='example' value='example value'/><br>
>   </dns></p>
<p dir="ltr">I thought about that before making it this way. The idea is to allow temporarily turning off the DNS server without losing all the config. I'm not married to that approach though (especially since we don't do that for other configures AFAIK). So I can modify the behavior <br>
><br>
><br>
> > +        }<br>
> > +        virBufferAsprintf(buf, " enable='%s'", fwd);<br>
> > +    }<br>
> >      if (def->forwardPlainNames) {<br>
> >          const char *fwd = virTristateBoolTypeToString(def->forwardPlainNames);<br>
> ><br>
><br>
> The rest of the patch looks okay. ACK if you fix the XML formatting issue.<br>
><br>
> Michal<br>
></p>