From patchwork Mon Jul 15 00:04:00 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cole Robinson X-Patchwork-Id: 168977 Delivered-To: patch@linaro.org Received: by 2002:a92:4782:0:0:0:0:0 with SMTP id e2csp3689431ilk; Sun, 14 Jul 2019 17:05:17 -0700 (PDT) X-Google-Smtp-Source: APXvYqzvxLuIxFKPiDuwXRVn7tiKUXhdfveLzCrVd7uMhPpFhv7EhvVhbSclk4kIQL3siLOJIb44 X-Received: by 2002:a37:a851:: with SMTP id r78mr14667087qke.120.1563149117329; Sun, 14 Jul 2019 17:05:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563149117; cv=none; d=google.com; s=arc-20160816; b=mZIrDKzJSlBTpGXhQdgWLCVsXTlAh9mxt+YaP8VI6+EU3izkFAaKYdcW8fmtXKWO0a zL/D9yf+Rrn4r+AGpvKaebOkvHoQwxNHHAdaXaga1OBJvAeoX41ZDz8qIrBslmH23I91 UUPz0225YdGUXbLCdoHI2cvdRgNtv8y0Zpvd5B1UIFRX8hkdq4rvTOa/1T95sD5xuqiw 73S8jkk9AErCd4oNbv26OYiEAruGv9zBkLnx5LztT4SE2ZW+ZBUY4rUIA02elKRlMWhY DGLUNBhRDlfhQJAoVuU9iEp2N6M32hu+9uC/yYwurZV0UkGhnXlZnYAMDyKhf8Dj45CF Qo1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:sender:content-transfer-encoding:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:references:in-reply-to:message-id:date:to:from :delivered-to; bh=nzeTWafsGElKfHdQTbWAzCyAnZJtslieIU6kDox9oO0=; b=xyhGXB9B5tG39qgk8bbkKnmEn8jPTE/U5ofWyVfTEnpT50s+DkSEjJvKHvTDJ5SUE8 PctaKecB1dZK/MhLAQUae5OZczJDX4lW+fJ0D6bQWVBFW8k7UDdz5ESF1Y8JdCO5NY+1 uGsV8Fh/ZMkfOsbge8LayX3llQswOacryYrxwXtA/f5LlBcz8KjdFdHqwYLftSvnro8F zBovxILIpzCBH6Lkt6ATXVxD60XJvvsyxZo+rRa5UOwJQal6vluCaSmDD0qzf2wwhRQ0 mTNZ1x4cOh6PSIf+Qcc7ykhZ1eQFwzhXC4OKOi7UBts3RF5515OI6TushznSI7/jYLG+ STLg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of libvir-list-bounces@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com. [209.132.183.28]) by mx.google.com with ESMTPS id n57si5532184qtk.282.2019.07.14.17.05.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 14 Jul 2019 17:05:17 -0700 (PDT) Received-SPF: pass (google.com: domain of libvir-list-bounces@redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; Authentication-Results: mx.google.com; spf=pass (google.com: domain of libvir-list-bounces@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9523F20264; Mon, 15 Jul 2019 00:05:15 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6CD866012E; Mon, 15 Jul 2019 00:05:15 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 322B08E1F; Mon, 15 Jul 2019 00:05:15 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x6F04JhP030319 for ; Sun, 14 Jul 2019 20:04:19 -0400 Received: by smtp.corp.redhat.com (Postfix) id 2723660C44; Mon, 15 Jul 2019 00:04:19 +0000 (UTC) Delivered-To: libvirt-list@redhat.com Received: from worklaptop.redhat.com (ovpn-121-2.rdu2.redhat.com [10.10.121.2]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9FC1B60BFB; Mon, 15 Jul 2019 00:04:18 +0000 (UTC) From: Cole Robinson To: libvirt-list@redhat.com Date: Sun, 14 Jul 2019 20:04:00 -0400 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 4/5] network: wire up dnsmasq option xmlns X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Mon, 15 Jul 2019 00:05:16 +0000 (UTC) This maps to XML like: ... To dnsmasq config options ... foo=bar cname=*.foo.example.com,master.example.com Signed-off-by: Cole Robinson --- docs/schemas/network.rng | 11 ++ src/network/bridge_driver.c | 130 +++++++++++++++++- src/network/bridge_driver.h | 12 ++ tests/Makefile.am | 14 +- .../networkxml2confdata/dnsmasq-options.conf | 18 +++ tests/networkxml2confdata/dnsmasq-options.xml | 15 ++ tests/networkxml2conftest.c | 8 +- tests/networkxml2xmlin/dnsmasq-options.xml | 15 ++ tests/networkxml2xmlout/dnsmasq-options.xml | 17 +++ tests/networkxml2xmltest.c | 11 +- 10 files changed, 239 insertions(+), 12 deletions(-) create mode 100644 tests/networkxml2confdata/dnsmasq-options.conf create mode 100644 tests/networkxml2confdata/dnsmasq-options.xml create mode 100644 tests/networkxml2xmlin/dnsmasq-options.xml create mode 100644 tests/networkxml2xmlout/dnsmasq-options.xml -- 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list diff --git a/docs/schemas/network.rng b/docs/schemas/network.rng index 2a6e3358fd..56937d6a4e 100644 --- a/docs/schemas/network.rng +++ b/docs/schemas/network.rng @@ -405,6 +405,17 @@ + + + + + + + + + + + diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c index 1a4d6e7f7b..41fa89a4af 100644 --- a/src/network/bridge_driver.c +++ b/src/network/bridge_driver.c @@ -69,6 +69,8 @@ #include "virjson.h" #include "virnetworkportdef.h" +#include + #define VIR_FROM_THIS VIR_FROM_NETWORK #define MAX_BRIDGE_ID 256 @@ -83,6 +85,8 @@ VIR_LOG_INIT("network.bridge_driver"); +#define DNSMASQ_NAMESPACE_HREF "http://libvirt.org/schemas/network/dnsmasq/1.0" + static virNetworkDriverStatePtr network_driver; @@ -136,10 +140,126 @@ networkDnsmasqCapsRefresh(virNetworkDriverStatePtr driver) return 0; } -static virNetworkXMLOptionPtr + +static void +networkDnsmasqDefNamespaceFree(void *nsdata) +{ + networkDnsmasqXmlNsDefPtr def = nsdata; + if (!def) + return; + + virStringListFreeCount(def->options, def->noptions); + + VIR_FREE(def); +} + + +static int +networkDnsmasqDefNamespaceParseOptions(networkDnsmasqXmlNsDefPtr nsdef, + xmlXPathContextPtr ctxt) +{ + VIR_AUTOFREE(xmlNodePtr *) nodes = NULL; + ssize_t nnodes; + size_t i; + + if ((nnodes = virXPathNodeSet("./dnsmasq:options/dnsmasq:option", + ctxt, &nodes)) < 0) + return -1; + + if (nnodes == 0) + return 0; + + if (VIR_ALLOC_N(nsdef->options, nnodes) < 0) + return -1; + + for (i = 0; i < nnodes; i++) { + if (!(nsdef->options[nsdef->noptions++] = virXMLPropString(nodes[i], "value"))) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("No dnsmasq options value specified")); + return -1; + } + } + + return 0; +} + + +static int +networkDnsmasqDefNamespaceParse(xmlXPathContextPtr ctxt, + void **data) +{ + networkDnsmasqXmlNsDefPtr nsdata = NULL; + int ret = -1; + + if (xmlXPathRegisterNs(ctxt, BAD_CAST "dnsmasq", + BAD_CAST DNSMASQ_NAMESPACE_HREF) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Failed to register xml namespace '%s'"), + DNSMASQ_NAMESPACE_HREF); + return -1; + } + + if (VIR_ALLOC(nsdata) < 0) + return -1; + + if (networkDnsmasqDefNamespaceParseOptions(nsdata, ctxt)) + goto cleanup; + + if (nsdata->noptions > 0) + VIR_STEAL_PTR(*data, nsdata); + + ret = 0; + + cleanup: + networkDnsmasqDefNamespaceFree(nsdata); + return ret; +} + + +static int +networkDnsmasqDefNamespaceFormatXML(virBufferPtr buf, + void *nsdata) +{ + networkDnsmasqXmlNsDefPtr def = nsdata; + size_t i; + + if (!def->noptions) + return 0; + + virBufferAddLit(buf, "\n"); + virBufferAdjustIndent(buf, 2); + + for (i = 0; i < def->noptions; i++) { + virBufferEscapeString(buf, "\n", + def->options[i]); + } + + virBufferAdjustIndent(buf, -2); + virBufferAddLit(buf, "\n"); + + return 0; +} + + +static const char * +networkDnsmasqDefNamespaceHref(void) +{ + return "xmlns:dnsmasq='" DNSMASQ_NAMESPACE_HREF "'"; +} + + +virNetworkXMLNamespace networkDnsmasqXMLNamespace = { + .parse = networkDnsmasqDefNamespaceParse, + .free = networkDnsmasqDefNamespaceFree, + .format = networkDnsmasqDefNamespaceFormatXML, + .href = networkDnsmasqDefNamespaceHref, +}; + + +virNetworkXMLOptionPtr networkDnsmasqCreateXMLConf(void) { - return virNetworkXMLOptionNew(NULL); + return virNetworkXMLOptionNew(&networkDnsmasqXMLNamespace); } @@ -1480,6 +1600,12 @@ networkDnsmasqConfContents(virNetworkObjPtr obj, } } + if (def->namespaceData) { + networkDnsmasqXmlNsDefPtr dnsmasqxmlns = def->namespaceData; + for (i = 0; i < dnsmasqxmlns->noptions; i++) + virBufferAsprintf(&configbuf, "%s\n", dnsmasqxmlns->options[i]); + } + if (!(*configstr = virBufferContentAndReset(&configbuf))) goto cleanup; diff --git a/src/network/bridge_driver.h b/src/network/bridge_driver.h index 7357c1754c..b095388a0b 100644 --- a/src/network/bridge_driver.h +++ b/src/network/bridge_driver.h @@ -27,6 +27,18 @@ #include "virdnsmasq.h" #include "virnetworkobj.h" +extern virNetworkXMLNamespace networkDnsmasqXMLNamespace; + +typedef struct _networkDnsmasqXmlNsDef networkDnsmasqXmlNsDef; +typedef networkDnsmasqXmlNsDef *networkDnsmasqXmlNsDefPtr; +struct _networkDnsmasqXmlNsDef { + size_t noptions; + char **options; +}; + +virNetworkXMLOptionPtr +networkDnsmasqCreateXMLConf(void); + int networkRegister(void); diff --git a/tests/Makefile.am b/tests/Makefile.am index 107f2de859..65192bac8e 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -331,13 +331,13 @@ test_programs += virjsontest endif WITH_YAJL test_programs += \ - networkxml2xmltest \ networkxml2xmlupdatetest \ virnetworkportxml2xmltest \ $(NULL) if WITH_NETWORK test_programs += \ + networkxml2xmltest \ networkxml2conftest \ networkxml2firewalltest \ $(NULL) @@ -806,11 +806,6 @@ EXTRA_DIST += \ bhyveargv2xmlmock.c endif ! WITH_BHYVE -networkxml2xmltest_SOURCES = \ - networkxml2xmltest.c \ - testutils.c testutils.h -networkxml2xmltest_LDADD = $(LDADDS) - networkxml2xmlupdatetest_SOURCES = \ networkxml2xmlupdatetest.c \ testutils.c testutils.h @@ -822,6 +817,11 @@ virnetworkportxml2xmltest_SOURCES = \ virnetworkportxml2xmltest_LDADD = $(LDADDS) if WITH_NETWORK +networkxml2xmltest_SOURCES = \ + networkxml2xmltest.c \ + testutils.c testutils.h +networkxml2xmltest_LDADD = ../src/libvirt_driver_network_impl.la $(LDADDS) + networkxml2conftest_SOURCES = \ networkxml2conftest.c \ testutils.c testutils.h @@ -833,7 +833,7 @@ networkxml2firewalltest_SOURCES = \ networkxml2firewalltest_LDADD = ../src/libvirt_driver_network_impl.la $(LDADDS) else ! WITH_NETWORK -EXTRA_DIST += networkxml2conftest.c +EXTRA_DIST += networkxml2xmltest.c networkxml2conftest.c endif ! WITH_NETWORK if WITH_STORAGE_SHEEPDOG diff --git a/tests/networkxml2confdata/dnsmasq-options.conf b/tests/networkxml2confdata/dnsmasq-options.conf new file mode 100644 index 0000000000..867f355c79 --- /dev/null +++ b/tests/networkxml2confdata/dnsmasq-options.conf @@ -0,0 +1,18 @@ +##WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE +##OVERWRITTEN AND LOST. Changes to this configuration should be made using: +## virsh net-edit default +## or other application using the libvirt API. +## +## dnsmasq conf file created by libvirt +strict-order +except-interface=lo +bind-dynamic +interface=virbr0 +dhcp-range=192.168.122.2,192.168.122.254,255.255.255.0 +dhcp-no-override +dhcp-authoritative +dhcp-lease-max=253 +dhcp-hostsfile=/var/lib/libvirt/dnsmasq/default.hostsfile +addn-hosts=/var/lib/libvirt/dnsmasq/default.addnhosts +foo=bar +cname=*.cloudapps.example.com,master.example.com diff --git a/tests/networkxml2confdata/dnsmasq-options.xml b/tests/networkxml2confdata/dnsmasq-options.xml new file mode 100644 index 0000000000..35a87b8e3e --- /dev/null +++ b/tests/networkxml2confdata/dnsmasq-options.xml @@ -0,0 +1,15 @@ + + default + 81ff0d90-c91e-6742-64da-4a736edb9a9b + + + + + + + + + + + + diff --git a/tests/networkxml2conftest.c b/tests/networkxml2conftest.c index c445551099..dcb99aad6e 100644 --- a/tests/networkxml2conftest.c +++ b/tests/networkxml2conftest.c @@ -25,8 +25,12 @@ testCompareXMLToConfFiles(const char *inxml, const char *outconf, dnsmasqCapsPtr virCommandPtr cmd = NULL; char *pidfile = NULL; dnsmasqContext *dctx = NULL; + virNetworkXMLOptionPtr xmlopt = NULL; - if (!(def = virNetworkDefParseFile(inxml, NULL))) + if (!(xmlopt = networkDnsmasqCreateXMLConf())) + goto fail; + + if (!(def = virNetworkDefParseFile(inxml, xmlopt))) goto fail; if (!(obj = virNetworkObjNew())) @@ -63,6 +67,7 @@ testCompareXMLToConfFiles(const char *inxml, const char *outconf, dnsmasqCapsPtr VIR_FREE(actual); VIR_FREE(pidfile); virCommandFree(cmd); + virObjectUnref(xmlopt); virNetworkObjEndAPI(&obj); dnsmasqContextFree(dctx); return ret; @@ -141,6 +146,7 @@ mymain(void) DO_TEST("dhcp6-nat-network", dhcpv6); DO_TEST("dhcp6host-routed-network", dhcpv6); DO_TEST("ptr-domains-auto", dhcpv6); + DO_TEST("dnsmasq-options", dhcpv6); virObjectUnref(dhcpv6); virObjectUnref(full); diff --git a/tests/networkxml2xmlin/dnsmasq-options.xml b/tests/networkxml2xmlin/dnsmasq-options.xml new file mode 100644 index 0000000000..35a87b8e3e --- /dev/null +++ b/tests/networkxml2xmlin/dnsmasq-options.xml @@ -0,0 +1,15 @@ + + default + 81ff0d90-c91e-6742-64da-4a736edb9a9b + + + + + + + + + + + + diff --git a/tests/networkxml2xmlout/dnsmasq-options.xml b/tests/networkxml2xmlout/dnsmasq-options.xml new file mode 100644 index 0000000000..856a018f25 --- /dev/null +++ b/tests/networkxml2xmlout/dnsmasq-options.xml @@ -0,0 +1,17 @@ + + default + 81ff0d90-c91e-6742-64da-4a736edb9a9b + + + + + + + + + + + + + + diff --git a/tests/networkxml2xmltest.c b/tests/networkxml2xmltest.c index cd76ce5375..3d90023445 100644 --- a/tests/networkxml2xmltest.c +++ b/tests/networkxml2xmltest.c @@ -10,6 +10,7 @@ #include "network_conf.h" #include "testutilsqemu.h" #include "virstring.h" +#include "network/bridge_driver.h" #define VIR_FROM_THIS VIR_FROM_NONE @@ -29,15 +30,19 @@ testCompareXMLToXMLFiles(const char *inxml, const char *outxml, int ret; testCompareNetXML2XMLResult result = TEST_COMPARE_NET_XML2XML_RESULT_SUCCESS; virNetworkDefPtr dev = NULL; + virNetworkXMLOptionPtr xmlopt = NULL; - if (!(dev = virNetworkDefParseFile(inxml, NULL))) { + if (!(xmlopt = networkDnsmasqCreateXMLConf())) + goto cleanup; + + if (!(dev = virNetworkDefParseFile(inxml, xmlopt))) { result = TEST_COMPARE_NET_XML2XML_RESULT_FAIL_PARSE; goto cleanup; } if (expectResult == TEST_COMPARE_NET_XML2XML_RESULT_FAIL_PARSE) goto cleanup; - if (!(actual = virNetworkDefFormat(dev, NULL, flags))) { + if (!(actual = virNetworkDefFormat(dev, xmlopt, flags))) { result = TEST_COMPARE_NET_XML2XML_RESULT_FAIL_FORMAT; goto cleanup; } @@ -67,6 +72,7 @@ testCompareXMLToXMLFiles(const char *inxml, const char *outxml, VIR_FREE(actual); virNetworkDefFree(dev); + virObjectUnref(xmlopt); return ret; } @@ -158,6 +164,7 @@ mymain(void) DO_TEST_PARSE_ERROR("passthrough-duplicate"); DO_TEST("metadata"); DO_TEST("set-mtu"); + DO_TEST("dnsmasq-options"); return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; }