From nobody Thu May 2 22:49:03 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1639066728; cv=none; d=zohomail.com; s=zohoarc; b=KN6QdJxA+Fi425EQwI0d5iwzYfLdUfOZELPkx9aHUhEMGhaDVUrWcmk1URNamUdTwPiC5CB9T1Xlp3KnSMmUlF1UvRNXOq3xxfPQnC9O+9T3oBQomZF+kNdf0IG/4aVLLaXCNU6fOqkbo04ikNchWilC+fm169gcIfa8aDfdmaU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1639066728; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=V4uYRY+Q5kWJ4Wlk2wiQuCJJW6nHVjjeY+taNKRe5xc=; b=b+7K6W5pMFcbMkCqg3C9cttcMMvr4gPT8Wc+SSaMTqr4RLc+JzcdMF/RTuh9WpOKtKdYx21R9G/nxkB2QJnCB9Fzn1b7rUxAWI0A10PKg6bBifzEF3L2/RSLjb2EJe+RqxydLFXPAAwlDSb9i8PCRaM8SSXuJIysz6Cg/jefKSI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1639066728112135.37712098247152; Thu, 9 Dec 2021 08:18:48 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-520-92v4uapXPZOUiHuqWJnCug-1; Thu, 09 Dec 2021 11:18:45 -0500 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 mimecast-mx01.redhat.com (Postfix) with ESMTPS id E6DCE18C89D9; Thu, 9 Dec 2021 16:18:40 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C7EAE45D87; Thu, 9 Dec 2021 16:18:40 +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 7E5071809CB9; Thu, 9 Dec 2021 16:18:40 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 1B9GI5aG014587 for ; Thu, 9 Dec 2021 11:18:05 -0500 Received: by smtp.corp.redhat.com (Postfix) id 9FECA19D9F; Thu, 9 Dec 2021 16:18:05 +0000 (UTC) Received: from maggie.redhat.com (unknown [10.43.2.64]) by smtp.corp.redhat.com (Postfix) with ESMTP id 285D119C59 for ; Thu, 9 Dec 2021 16:18:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1639066727; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=V4uYRY+Q5kWJ4Wlk2wiQuCJJW6nHVjjeY+taNKRe5xc=; b=P3YjI8xI1LpP1MmZdEPvwJLwnEfLGFgkgz/zGXs2b/RrOAwVwi7OYXR1/ZAr0woxMPmn5f tD2lqPzegC2tWBN5zmNZc9p5nh2vOinF3ZGlYA6n8ahfkAHGXpsBzHac7V3sMFcs5ZGt6M d1ieJuJVC+h0JNv01G5eag+u5keGglY= X-MC-Unique: 92v4uapXPZOUiHuqWJnCug-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 1/3] network: Initialize variables in networkDnsmasqConfContents() Date: Thu, 9 Dec 2021 17:17:48 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-loop: libvir-list@redhat.com 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 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1639066730731100001 Content-Type: text/plain; charset="utf-8" In networkDnsmasqConfContents() there's a for() loop which initializes some variables in its initialization block. This makes both the loop() statement and variable declaration block look needlessly ugly. Speaking of variable declaration, also move some variables which are used only within blocks into their respective blocks. Signed-off-by: Michal Privoznik --- src/network/bridge_driver.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c index 7e5fab630b..2d8bebdd2f 100644 --- a/src/network/bridge_driver.c +++ b/src/network/bridge_driver.c @@ -1057,16 +1057,14 @@ networkDnsmasqConfContents(virNetworkObj *obj, { virNetworkDef *def =3D virNetworkObjGetDef(obj); g_auto(virBuffer) configbuf =3D VIR_BUFFER_INITIALIZER; - int r; int nbleases =3D 0; size_t i; virNetworkDNSDef *dns =3D &def->dns; bool wantDNS =3D dns->enable !=3D VIR_TRISTATE_BOOL_NO; - virNetworkIPDef *tmpipdef; - virNetworkIPDef *ipdef; - virNetworkIPDef *ipv4def; - virNetworkIPDef *ipv6def; - bool ipv6SLAAC; + virNetworkIPDef *ipdef =3D NULL; + virNetworkIPDef *ipv4def =3D NULL; + virNetworkIPDef *ipv6def =3D NULL; + bool ipv6SLAAC =3D false; =20 *configstr =3D NULL; =20 @@ -1186,6 +1184,8 @@ networkDnsmasqConfContents(virNetworkObj *obj, "interface=3D%s\n", def->bridge); } else { + virNetworkIPDef *tmpipdef =3D NULL; + virBufferAddLit(&configbuf, "bind-interfaces\n"); /* * --interface does not actually work with dnsmasq < 2.47, @@ -1312,9 +1312,7 @@ networkDnsmasqConfContents(virNetworkObj *obj, } =20 /* Find the first dhcp for both IPv4 and IPv6 */ - for (i =3D 0, ipv4def =3D NULL, ipv6def =3D NULL, ipv6SLAAC =3D false; - (ipdef =3D virNetworkDefGetIPByIndex(def, AF_UNSPEC, i)); - i++) { + for (i =3D 0; (ipdef =3D virNetworkDefGetIPByIndex(def, AF_UNSPEC, i))= ; i++) { if (VIR_SOCKET_ADDR_IS_FAMILY(&ipdef->address, AF_INET)) { if (ipdef->nranges || ipdef->nhosts) { if (ipv4def) { @@ -1370,6 +1368,7 @@ networkDnsmasqConfContents(virNetworkObj *obj, =20 while (ipdef) { int prefix; + int r; =20 prefix =3D virNetworkIPDefPrefix(ipdef); if (prefix < 0) { --=20 2.32.0 From nobody Thu May 2 22:49:03 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1639066731; cv=none; d=zohomail.com; s=zohoarc; b=Vinl86+vOriVf+cx1hhgvkyvi4486QKNPC2OetKkNKvpbBud8qFPQItfpr//TpSOpujR/l6CHLy0Qkk80rkL5BrGOMLzeAeXAHspVQtyaiekf+ZqtkJuhFaFsvEiix0GijhSBBtFyB5ycL/iag/BGeI8ffiLASMG+XjUv31rZMY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1639066731; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=kBFegyvhrZeJyqgJ4AyK8/mFB8Iw+jP4hmGJVLAXZdE=; b=Od6VZseMQJ7ey0FLTfgzYDwZnyLoOthYcHEDnksVfZnB7aL8rq79UOLNXOYETwvSNFAtZQBzobDeUCs8kuc5xUAPd3NumaXckUT3iR9jqmYdZT5pxGPEfG/wfDj0FwdY+98DP9dl64nr/2sYOMMSCPDp76JU1Pl5xKfU4dSAPko= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.zohomail.com with SMTPS id 1639066731204564.1252820840002; Thu, 9 Dec 2021 08:18:51 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-101-IJAeFjawOGOw1V2RjNyCzw-1; Thu, 09 Dec 2021 11:18:46 -0500 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 mimecast-mx01.redhat.com (Postfix) with ESMTPS id DF0D0801B0F; Thu, 9 Dec 2021 16:18:40 +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 C22A260657; Thu, 9 Dec 2021 16:18:40 +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 71A8F4A7C9; Thu, 9 Dec 2021 16:18:40 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 1B9GIcKu014635 for ; Thu, 9 Dec 2021 11:18:38 -0500 Received: by smtp.corp.redhat.com (Postfix) id 941BA1F42E; Thu, 9 Dec 2021 16:18:38 +0000 (UTC) Received: from maggie.redhat.com (unknown [10.43.2.64]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1C10C19724 for ; Thu, 9 Dec 2021 16:18:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1639066729; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=kBFegyvhrZeJyqgJ4AyK8/mFB8Iw+jP4hmGJVLAXZdE=; b=fewrJK/oKkksoPAv2ryOrQzkG0GcbhqMgpWENKVsxOtehXNLAKEOu6/cx9r6vGkClrRZRx YqE579r5Akfwz32Ej4/Qgzs5ChWmICfKfdf6OcnYrYOUDd5vDAL4LmvgZRpOEjZcSaXtH0 7FdwORy+Nb4xTKUKqaae3VtBQ4DuR0o= X-MC-Unique: IJAeFjawOGOw1V2RjNyCzw-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 2/3] network: Separate DHCP config generator into a function Date: Thu, 9 Dec 2021 17:17:51 +0100 Message-Id: <34c6faa84e04e1ded859974e1c336c858145184c.1639066644.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-loop: libvir-list@redhat.com 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 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1639066732628100003 Content-Type: text/plain; charset="utf-8" Generating configuration file for dnsmasq is done in networkDnsmasqConfContents() which is this big, self-contained function. Separate at least DHCP part into its own function for better readability. Signed-off-by: Michal Privoznik --- src/network/bridge_driver.c | 227 +++++++++++++++++++----------------- 1 file changed, 120 insertions(+), 107 deletions(-) diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c index 2d8bebdd2f..0338ef502f 100644 --- a/src/network/bridge_driver.c +++ b/src/network/bridge_driver.c @@ -1047,6 +1047,123 @@ networkDnsmasqConfLocalPTRs(virBuffer *buf, } =20 =20 +static int +networkDnsmasqConfDHCP(virBuffer *buf, + virNetworkIPDef *ipdef, + const char *bridge, + int *nbleases, + dnsmasqContext *dctx) +{ + int r; + int prefix; + + if (!ipdef) + return 0; + + prefix =3D virNetworkIPDefPrefix(ipdef); + if (prefix < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("bridge '%s' has an invalid prefix"), + bridge); + return -1; + } + for (r =3D 0; r < ipdef->nranges; r++) { + int thisRange; + virNetworkDHCPRangeDef range =3D ipdef->ranges[r]; + g_autofree char *leasetime =3D NULL; + g_autofree char *saddr =3D NULL; + g_autofree char *eaddr =3D NULL; + + if (!(saddr =3D virSocketAddrFormat(&range.addr.start)) || + !(eaddr =3D virSocketAddrFormat(&range.addr.end))) + return -1; + + if (VIR_SOCKET_ADDR_IS_FAMILY(&ipdef->address, AF_INET6)) { + virBufferAsprintf(buf, "dhcp-range=3D%s,%s,%d", + saddr, eaddr, prefix); + } else { + /* IPv4 - dnsmasq requires a netmask rather than prefix */ + virSocketAddr netmask; + g_autofree char *netmaskStr =3D NULL; + + if (virSocketAddrPrefixToNetmask(prefix, &netmask, AF_INET) < = 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Failed to translate bridge '%s' " + "prefix %d to netmask"), + bridge, prefix); + return -1; + } + + if (!(netmaskStr =3D virSocketAddrFormat(&netmask))) + return -1; + virBufferAsprintf(buf, "dhcp-range=3D%s,%s,%s", + saddr, eaddr, netmaskStr); + } + + if ((leasetime =3D networkBuildDnsmasqLeaseTime(range.lease))) + virBufferAsprintf(buf, ",%s", leasetime); + + virBufferAddLit(buf, "\n"); + + thisRange =3D virSocketAddrGetRange(&range.addr.start, + &range.addr.end, + &ipdef->address, + virNetworkIPDefPrefix(ipdef)); + if (thisRange < 0) + return -1; + *nbleases +=3D thisRange; + } + + /* + * For static-only DHCP, i.e. with no range but at least one + * host element, we have to add a special --dhcp-range option + * to enable the service in dnsmasq. (this is for dhcp-hosts=3D + * support) + */ + if (!ipdef->nranges && ipdef->nhosts) { + g_autofree char *bridgeaddr =3D virSocketAddrFormat(&ipdef->addres= s); + if (!bridgeaddr) + return -1; + virBufferAsprintf(buf, "dhcp-range=3D%s,static", + bridgeaddr); + if (VIR_SOCKET_ADDR_IS_FAMILY(&ipdef->address, AF_INET6)) + virBufferAsprintf(buf, ",%d", prefix); + virBufferAddLit(buf, "\n"); + } + + if (networkBuildDnsmasqDhcpHostsList(dctx, ipdef) < 0) + return -1; + + /* Note: the following is IPv4 only */ + if (VIR_SOCKET_ADDR_IS_FAMILY(&ipdef->address, AF_INET)) { + if (ipdef->nranges || ipdef->nhosts) { + virBufferAddLit(buf, "dhcp-no-override\n"); + virBufferAddLit(buf, "dhcp-authoritative\n"); + } + + if (ipdef->tftproot) { + virBufferAddLit(buf, "enable-tftp\n"); + virBufferAsprintf(buf, "tftp-root=3D%s\n", ipdef->tftproot); + } + + if (ipdef->bootfile) { + if (VIR_SOCKET_ADDR_VALID(&ipdef->bootserver)) { + g_autofree char *bootserver =3D virSocketAddrFormat(&ipdef= ->bootserver); + + if (!bootserver) + return -1; + virBufferAsprintf(buf, "dhcp-boot=3D%s%s%s\n", + ipdef->bootfile, ",,", bootserver); + } else { + virBufferAsprintf(buf, "dhcp-boot=3D%s\n", ipdef->bootfile= ); + } + } + } + + return 0; +} + + int networkDnsmasqConfContents(virNetworkObj *obj, const char *pidfile, @@ -1364,113 +1481,9 @@ networkDnsmasqConfContents(virNetworkObj *obj, "on the same network interface."); } =20 - ipdef =3D ipv4def ? ipv4def : ipv6def; - - while (ipdef) { - int prefix; - int r; - - prefix =3D virNetworkIPDefPrefix(ipdef); - if (prefix < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("bridge '%s' has an invalid prefix"), - def->bridge); - return -1; - } - for (r =3D 0; r < ipdef->nranges; r++) { - int thisRange; - virNetworkDHCPRangeDef range =3D ipdef->ranges[r]; - g_autofree char *leasetime =3D NULL; - g_autofree char *saddr =3D NULL; - g_autofree char *eaddr =3D NULL; - - if (!(saddr =3D virSocketAddrFormat(&range.addr.start)) || - !(eaddr =3D virSocketAddrFormat(&range.addr.end))) - return -1; - - if (VIR_SOCKET_ADDR_IS_FAMILY(&ipdef->address, AF_INET6)) { - virBufferAsprintf(&configbuf, "dhcp-range=3D%s,%s,%d", - saddr, eaddr, prefix); - } else { - /* IPv4 - dnsmasq requires a netmask rather than prefix */ - virSocketAddr netmask; - g_autofree char *netmaskStr =3D NULL; - - if (virSocketAddrPrefixToNetmask(prefix, &netmask, AF_INET= ) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Failed to translate bridge '%s' " - "prefix %d to netmask"), - def->bridge, prefix); - return -1; - } - - if (!(netmaskStr =3D virSocketAddrFormat(&netmask))) - return -1; - virBufferAsprintf(&configbuf, "dhcp-range=3D%s,%s,%s", - saddr, eaddr, netmaskStr); - } - - if ((leasetime =3D networkBuildDnsmasqLeaseTime(range.lease))) - virBufferAsprintf(&configbuf, ",%s", leasetime); - - virBufferAddLit(&configbuf, "\n"); - - thisRange =3D virSocketAddrGetRange(&range.addr.start, - &range.addr.end, - &ipdef->address, - virNetworkIPDefPrefix(ipdef)= ); - if (thisRange < 0) - return -1; - nbleases +=3D thisRange; - } - - /* - * For static-only DHCP, i.e. with no range but at least one - * host element, we have to add a special --dhcp-range option - * to enable the service in dnsmasq. (this is for dhcp-hosts=3D - * support) - */ - if (!ipdef->nranges && ipdef->nhosts) { - g_autofree char *bridgeaddr =3D virSocketAddrFormat(&ipdef->ad= dress); - if (!bridgeaddr) - return -1; - virBufferAsprintf(&configbuf, "dhcp-range=3D%s,static", - bridgeaddr); - if (VIR_SOCKET_ADDR_IS_FAMILY(&ipdef->address, AF_INET6)) - virBufferAsprintf(&configbuf, ",%d", prefix); - virBufferAddLit(&configbuf, "\n"); - } - - if (networkBuildDnsmasqDhcpHostsList(dctx, ipdef) < 0) - return -1; - - /* Note: the following is IPv4 only */ - if (VIR_SOCKET_ADDR_IS_FAMILY(&ipdef->address, AF_INET)) { - if (ipdef->nranges || ipdef->nhosts) { - virBufferAddLit(&configbuf, "dhcp-no-override\n"); - virBufferAddLit(&configbuf, "dhcp-authoritative\n"); - } - - if (ipdef->tftproot) { - virBufferAddLit(&configbuf, "enable-tftp\n"); - virBufferAsprintf(&configbuf, "tftp-root=3D%s\n", ipdef->t= ftproot); - } - - if (ipdef->bootfile) { - if (VIR_SOCKET_ADDR_VALID(&ipdef->bootserver)) { - g_autofree char *bootserver =3D virSocketAddrFormat(&i= pdef->bootserver); - - if (!bootserver) - return -1; - virBufferAsprintf(&configbuf, "dhcp-boot=3D%s%s%s\n", - ipdef->bootfile, ",,", bootserver); - } else { - virBufferAsprintf(&configbuf, "dhcp-boot=3D%s\n", ipde= f->bootfile); - } - } - } - ipdef =3D (ipdef =3D=3D ipv6def) ? NULL : ipv6def; - } + if (networkDnsmasqConfDHCP(&configbuf, ipv4def, def->bridge, &nbleases= , dctx) < 0 || + networkDnsmasqConfDHCP(&configbuf, ipv6def, def->bridge, &nbleases= , dctx) < 0) + return -1; =20 if (nbleases > 0) virBufferAsprintf(&configbuf, "dhcp-lease-max=3D%d\n", nbleases); --=20 2.32.0 From nobody Thu May 2 22:49:03 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1639066734; cv=none; d=zohomail.com; s=zohoarc; b=NBFI/8JHTsDZC6/vVjE/y7Y9M9ElhlbIguxueoFiqgrdPYXmJPiSf1SU3lyCeao8qUI97qXQS1WzCR8xOh87ayt+Elw8ryLzsZT0l7z6kwaiM36RKhPbLPd2jX1U4PhL6dvDqqhiSmCX1rG/cINFkxedkuHcBEzeLf1+/ww/iKI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1639066734; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=XZqNZGbFNJs7aIe787f+beE5Ad/EhGivhq+x5xKc1pw=; b=M/wxz+LLXcFn0qZhBJHHILbb3zWvr2FcetVnHKzvoyGXlbNPyy4sXIbAac9bX1lxloOWWqK49D422yjuTwiAYpLoBCOqDbJ3kvmgh0J3JEddLZwZLfp7h9V7RCEqKZKwqir3nD3tMrzHEXHlDrw+PTdcg8mMQsiXRRBXJXdVr2I= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.zohomail.com with SMTPS id 1639066734271443.50703048205776; Thu, 9 Dec 2021 08:18:54 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-485-WS2RhOq1PNCI_VHLHS4TsQ-1; Thu, 09 Dec 2021 11:18:51 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 1DF32801AAB; Thu, 9 Dec 2021 16:18:44 +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 F04C85BE14; Thu, 9 Dec 2021 16:18:43 +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 C4A4D4A7CD; Thu, 9 Dec 2021 16:18:43 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 1B9GIdvs014645 for ; Thu, 9 Dec 2021 11:18:39 -0500 Received: by smtp.corp.redhat.com (Postfix) id 673E41F42E; Thu, 9 Dec 2021 16:18:39 +0000 (UTC) Received: from maggie.redhat.com (unknown [10.43.2.64]) by smtp.corp.redhat.com (Postfix) with ESMTP id E2D9D19724 for ; Thu, 9 Dec 2021 16:18:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1639066733; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=XZqNZGbFNJs7aIe787f+beE5Ad/EhGivhq+x5xKc1pw=; b=VoNvl3+LjaQjW6h9a0vuktJcVjCc8BlCqneVu+p/Aq8SN1Eh/yp0ZrhWBZDIXavQCh75wY 3Oy/vhbGkc+RsVXTPK6Z3nxzzab/3BV7RsnR4Kq4qqKQ0arfIWsxIQWOUKhmDbmBKPdW1u 5g/Q8y+5GWpsmdUvw371tUN1nWdrMac= X-MC-Unique: WS2RhOq1PNCI_VHLHS4TsQ-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 3/3] network: Generate TFTP config regardless of DHCP Date: Thu, 9 Dec 2021 17:17:52 +0100 Message-Id: <6d4c8798a50672f173271c278c7c7cf3ed86b048.1639066644.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-loop: libvir-list@redhat.com 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.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1639066735479100001 Content-Type: text/plain; charset="utf-8" We already allow users to provide TFTP root path in network XML and not specify any DHCP. This makes sense, because dnsmasq is not only DHCP server but also TFTP server and users might have a DHCP server configured on their own, outside of libvirt's control and want just the TFTP part. By moving TFTP config generator out of DHCP generator and calling it for every IPv4 range, users can finally enable just TFTP. Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=3D2026765 Signed-off-by: Michal Privoznik --- src/network/bridge_driver.c | 30 ++++++++++++++----- .../networkxml2confdata/netboot-network.conf | 4 +-- tests/networkxml2confdata/netboot-tftp.conf | 13 ++++++++ tests/networkxml2confdata/netboot-tftp.xml | 9 ++++++ tests/networkxml2conftest.c | 1 + tests/networkxml2xmlin/netboot-tftp.xml | 1 + tests/networkxml2xmlout/netboot-tftp.xml | 1 + tests/networkxml2xmltest.c | 1 + 8 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 tests/networkxml2confdata/netboot-tftp.conf create mode 100644 tests/networkxml2confdata/netboot-tftp.xml create mode 120000 tests/networkxml2xmlin/netboot-tftp.xml create mode 120000 tests/networkxml2xmlout/netboot-tftp.xml diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c index 0338ef502f..b26b44ac01 100644 --- a/src/network/bridge_driver.c +++ b/src/network/bridge_driver.c @@ -1141,11 +1141,6 @@ networkDnsmasqConfDHCP(virBuffer *buf, virBufferAddLit(buf, "dhcp-authoritative\n"); } =20 - if (ipdef->tftproot) { - virBufferAddLit(buf, "enable-tftp\n"); - virBufferAsprintf(buf, "tftp-root=3D%s\n", ipdef->tftproot); - } - if (ipdef->bootfile) { if (VIR_SOCKET_ADDR_VALID(&ipdef->bootserver)) { g_autofree char *bootserver =3D virSocketAddrFormat(&ipdef= ->bootserver); @@ -1164,6 +1159,22 @@ networkDnsmasqConfDHCP(virBuffer *buf, } =20 =20 +static void +networkDnsmasqConfTFTP(virBuffer *buf, + virNetworkIPDef *ipdef, + bool *enableTFTP) +{ + if (!ipdef->tftproot) + return; + + if (!*enableTFTP) { + virBufferAddLit(buf, "enable-tftp\n"); + *enableTFTP =3D true; + } + virBufferAsprintf(buf, "tftp-root=3D%s\n", ipdef->tftproot); +} + + int networkDnsmasqConfContents(virNetworkObj *obj, const char *pidfile, @@ -1182,6 +1193,7 @@ networkDnsmasqConfContents(virNetworkObj *obj, virNetworkIPDef *ipv4def =3D NULL; virNetworkIPDef *ipv6def =3D NULL; bool ipv6SLAAC =3D false; + bool enableTFTP =3D false; =20 *configstr =3D NULL; =20 @@ -1441,6 +1453,8 @@ networkDnsmasqConfContents(virNetworkObj *obj, ipv4def =3D ipdef; } } + + networkDnsmasqConfTFTP(&configbuf, ipdef, &enableTFTP); } if (VIR_SOCKET_ADDR_IS_FAMILY(&ipdef->address, AF_INET6)) { if (ipdef->nranges || ipdef->nhosts) { @@ -1619,7 +1633,7 @@ networkStartDhcpDaemon(virNetworkDriverState *driver, i =3D 0; while ((ipdef =3D virNetworkDefGetIPByIndex(def, AF_UNSPEC, i))) { i++; - if (ipdef->nranges || ipdef->nhosts) + if (ipdef->nranges || ipdef->nhosts || ipdef->tftproot) needDnsmasq =3D true; } =20 @@ -3667,7 +3681,7 @@ networkUpdate(virNetworkPtr net, for (i =3D 0; (ipdef =3D virNetworkDefGetIPByIndex(def, AF_INET, i)); i++) { - if (ipdef->nranges || ipdef->nhosts) { + if (ipdef->nranges || ipdef->nhosts || ipdef->tftproot) { oldDhcpActive =3D true; break; } @@ -3782,7 +3796,7 @@ networkUpdate(virNetworkPtr net, =20 for (i =3D 0; (ipdef =3D virNetworkDefGetIPByIndex(def, AF_INE= T, i)); i++) { - if (ipdef->nranges || ipdef->nhosts) { + if (ipdef->nranges || ipdef->nhosts || ipdef->tftproot) { newDhcpActive =3D true; break; } diff --git a/tests/networkxml2confdata/netboot-network.conf b/tests/network= xml2confdata/netboot-network.conf index 99272b9d68..a429663f8b 100644 --- a/tests/networkxml2confdata/netboot-network.conf +++ b/tests/networkxml2confdata/netboot-network.conf @@ -10,11 +10,11 @@ expand-hosts except-interface=3Dlo bind-interfaces listen-address=3D192.168.122.1 +enable-tftp +tftp-root=3D/var/lib/tftproot dhcp-range=3D192.168.122.2,192.168.122.254,255.255.255.0 dhcp-no-override dhcp-authoritative -enable-tftp -tftp-root=3D/var/lib/tftproot dhcp-boot=3Dpxeboot.img dhcp-lease-max=3D253 dhcp-hostsfile=3D/var/lib/libvirt/dnsmasq/netboot.hostsfile diff --git a/tests/networkxml2confdata/netboot-tftp.conf b/tests/networkxml= 2confdata/netboot-tftp.conf new file mode 100644 index 0000000000..45615f3c33 --- /dev/null +++ b/tests/networkxml2confdata/netboot-tftp.conf @@ -0,0 +1,13 @@ +##WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE +##OVERWRITTEN AND LOST. Changes to this configuration should be made usin= g: +## virsh net-edit tftp-only +## or other application using the libvirt API. +## +## dnsmasq conf file created by libvirt +strict-order +except-interface=3Dlo +bind-dynamic +interface=3Dvirbr0 +enable-tftp +tftp-root=3D/var/lib/tftproot +addn-hosts=3D/var/lib/libvirt/dnsmasq/tftp-only.addnhosts diff --git a/tests/networkxml2confdata/netboot-tftp.xml b/tests/networkxml2= confdata/netboot-tftp.xml new file mode 100644 index 0000000000..297f5a7ba1 --- /dev/null +++ b/tests/networkxml2confdata/netboot-tftp.xml @@ -0,0 +1,9 @@ + + tftp-only + eb486e5c-4df5-42ee-ae4a-ad8557998d00 + + + + + + diff --git a/tests/networkxml2conftest.c b/tests/networkxml2conftest.c index b76d72793a..a19449ea2b 100644 --- a/tests/networkxml2conftest.c +++ b/tests/networkxml2conftest.c @@ -137,6 +137,7 @@ mymain(void) DO_TEST("isolated-network", restricted); DO_TEST("netboot-network", restricted); DO_TEST("netboot-proxy-network", restricted); + DO_TEST("netboot-tftp", full); DO_TEST("nat-network-dns-srv-record-minimal", restricted); DO_TEST("nat-network-name-with-quotes", restricted); DO_TEST("routed-network", full); diff --git a/tests/networkxml2xmlin/netboot-tftp.xml b/tests/networkxml2xml= in/netboot-tftp.xml new file mode 120000 index 0000000000..1487de558b --- /dev/null +++ b/tests/networkxml2xmlin/netboot-tftp.xml @@ -0,0 +1 @@ +../networkxml2confdata/netboot-tftp.xml \ No newline at end of file diff --git a/tests/networkxml2xmlout/netboot-tftp.xml b/tests/networkxml2xm= lout/netboot-tftp.xml new file mode 120000 index 0000000000..1487de558b --- /dev/null +++ b/tests/networkxml2xmlout/netboot-tftp.xml @@ -0,0 +1 @@ +../networkxml2confdata/netboot-tftp.xml \ No newline at end of file diff --git a/tests/networkxml2xmltest.c b/tests/networkxml2xmltest.c index ca24305ace..9e8d675a10 100644 --- a/tests/networkxml2xmltest.c +++ b/tests/networkxml2xmltest.c @@ -127,6 +127,7 @@ mymain(void) DO_TEST("nat-network"); DO_TEST("netboot-network"); DO_TEST("netboot-proxy-network"); + DO_TEST("netboot-tftp"); DO_TEST("nat-network-dns-txt-record"); DO_TEST("nat-network-dns-srv-record"); DO_TEST("nat-network-dns-srv-records"); --=20 2.32.0