From nobody Sun Feb 8 17:03:25 2026 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=1653311835; cv=none; d=zohomail.com; s=zohoarc; b=n4pfFFAOwtMaiJk0zT6i98AQRlfeawK/6XWBsDXGzsRPEWaZocVqEkGZ3ruBisbQCgDY7NorrrQe6WvZyQ9A9Gv/fsMj4NZ/9mFmW5WdCA0kmDW/Am4iI+K3nUZiNi/z6XjsnDLUnGpvQDDuhVqYYJfpCY4JZPAs+VXNK2gd3kw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1653311835; 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=nvHPy4uNdWTwPbkuArN3MUky2JqOIKcAAB+K+ZA6ye0=; b=JCGhfvQg8WNibRq3+tkgL0m4ViUMrw2dvOEf24YyqLTNMdhR+rAzID3bzDHBv44ZERrsPnlxUVSpyEwEHL6VwvAqwHMp292bWTWbRf7kq5IbdMPATuTkiSp0/5apdguIEBDE+JBeZPO/IyX7iddno2lhQelHnUKZ1GXxPF70+68= 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 1653311835441223.93008315918235; Mon, 23 May 2022 06:17:15 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-488--h1u8asPOY2mxhiSIJDa8A-1; Mon, 23 May 2022 09:17:09 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 112D938025E2; Mon, 23 May 2022 13:17:07 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id EB8372166B2D; Mon, 23 May 2022 13:17:06 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 2F471194705D; Mon, 23 May 2022 13:17:06 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id B91AE1947067 for ; Mon, 23 May 2022 13:17:05 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 9E55D407E1C1; Mon, 23 May 2022 13:17:05 +0000 (UTC) Received: from maggie.redhat.com (unknown [10.43.2.180]) by smtp.corp.redhat.com (Postfix) with ESMTP id 28C3C407E1C0 for ; Mon, 23 May 2022 13:17:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1653311834; 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=nvHPy4uNdWTwPbkuArN3MUky2JqOIKcAAB+K+ZA6ye0=; b=IPnIzowCA9VhSHdl5HHpAw2YRYFc+ZIrcnj7StvyFUaax+CuK1vXAoos5PItzvK+Z7r4HO dL4XEm0zKGlGmKpqReeXgD9uyWEV789elL2wsF/buarVbqcpVuYYHHyuVNGLd5uoII8c10 1k62OuMv13Yx+tU3mZ+rXPWSCr/j0F8= X-MC-Unique: -h1u8asPOY2mxhiSIJDa8A-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 2/3] network: Separate DHCP config generator into a function Date: Mon, 23 May 2022 15:17:01 +0200 Message-Id: <800221d1c84626ccbf5e6926d5accfc9ebe128d0.1653311757.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.11.54.1 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 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: 1653311835738100004 Content-Type: text/plain; charset="utf-8"; x-default="true" 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 10099571c2..f087e07c52 100644 --- a/src/network/bridge_driver.c +++ b/src/network/bridge_driver.c @@ -994,6 +994,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, @@ -1248,113 +1365,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.35.1