From nobody Thu Nov 21 16:46:49 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1716189437048812.7571250182547; Mon, 20 May 2024 00:17:17 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id F34DE1E57; Mon, 20 May 2024 03:17:15 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 179081D67; Mon, 20 May 2024 03:09:44 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 25F8419D1; Wed, 15 May 2024 06:11:26 -0400 (EDT) Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id C2C7B19CE for ; Wed, 15 May 2024 06:11:24 -0400 (EDT) Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-1f08442b7bcso15232415ad.1 for ; Wed, 15 May 2024 03:11:24 -0700 (PDT) Received: from iuseotherswifi.notpua ([218.185.248.66]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0bf31989sm114493035ad.173.2024.05.15.03.11.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 03:11:21 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.5 required=5.0 tests=DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715767883; x=1716372683; darn=lists.libvirt.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=aiegmvpMCprd272q0Fh4SpzYk9NQLKjIaXB96DiC0AU=; b=k5lVByx3nwk1BOwA1HPHUNMVoV5TnxIZe5xVLqER96QfLLrZ3DZvMyN8aECaleIp4N gPqIJO3U3G9jRFrWTokfLwCVitwYCo4wvs+ByOfuyRELiWuL+hxaBdk8ufoOr2oiWW0Z 0Kj8o+xZbBBodSfQo7pTRkB+4a39xC4ckGNWLd0PUNkLqHSGHsKl0agkGASCAUhfvkbW C6ReSboSiu5tMtMQ8O0ggp8BIEDtudYdHZEuqHyDDU7dDe01pHPBTxR+x15MlnEYDtwH Fa+Yy2/7H/1sgErD/3qyLFobuh2Tz13+YtLAohxb8Rumck1IVQHsVN9etejuzz4lQeZS 4K7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715767883; x=1716372683; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=aiegmvpMCprd272q0Fh4SpzYk9NQLKjIaXB96DiC0AU=; b=rzdLEiTn6ewhpfM7Mjc0QrcqP7tdoB7eum5wAkO7Kzhft5iCqnrZVeH8xRXok+XuJR hHHfEugbBqkBcUt1pD2EjED2odwQ0rDpRYUNzC4crcnPj68/UivfNqIoHa2p3T3zNdtr KGfW6/bhzXQaGlN6mI6qDuaFz7mVjxh9EMGOXj3nbU3k2rza47EcOx4r24bJhCkd/Hi4 twgRaK18Ns0Ylu0J8ODsDsq/bt4dREkR64puMpvhiWWvTXKLubRmMJ5sew0BRQTIZpKe zYivUxu+mipSMsY3PG7WurCu3+QJMoYIL2LOZ5e7Dw28k24ucYapQH4Ln53yq8t5uZ9m picg== X-Gm-Message-State: AOJu0YzLOjhO6WEXacgATv4N9GFigOLuV8U4ru55S0ypLBeoGWeVqqjg 1RGoQNSmVJcD+qwr5nulntHdzMPjDE4t/oV7GfDmh10lK7Br2sA+L09rOY6G X-Google-Smtp-Source: AGHT+IERPaipDVMvbiXmTens42l9SAjlKUWNtOYf4siD/Mqpt4JJn8pXquke4fLu7iZqY89R505Bsw== X-Received: by 2002:a17:902:8a95:b0:1e5:10e5:b66e with SMTP id d9443c01a7336-1ef43d29924mr137640935ad.27.1715767882501; Wed, 15 May 2024 03:11:22 -0700 (PDT) From: Abhiram Tilak To: devel@lists.libvirt.org Subject: [PATCH v2] network: add modify-or-add feature to net-update Date: Wed, 15 May 2024 15:38:55 +0530 Message-ID: <20240515100854.17602-2-atp.exp@gmail.com> X-Mailer: git-send-email 2.42.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-MailFrom: atp.exp@gmail.com X-Mailman-Rule-Hits: nonmember-moderation X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0 Message-ID-Hash: SODUU7ZITT5OH4665B6YBY3L7JIBWXGY X-Message-ID-Hash: SODUU7ZITT5OH4665B6YBY3L7JIBWXGY X-Mailman-Approved-At: Mon, 20 May 2024 07:09:27 -0400 CC: Abhiram Tilak X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1716189437254100001 Content-Type: text/plain; charset="utf-8" The current way of updating a network configuration uses `virsh net-update` to add, delete or modify entries. But with such a mechansim one should know if an entry with current info already exists. Adding modify-or-add option automatically performs either modify or add depending on the current state. Resolves: https://gitlab.com/libvirt/libvirt/-/issues/363 Signed-off-by: Abhiram Tilak --- Changes in v2:=20 - Removed the modify-or-add functionality for sections where modify is not applicable. - Changed the existing implementation of `UpdateIPDHCPHost`, to avoid code duplication. - Changed the implementation of modify-or-delete to reassign the `command` variable instead of using multiple nested conditions. docs/manpages/virsh.rst | 5 +- include/libvirt/libvirt-network.h | 12 +-- src/conf/network_conf.c | 134 +++++++++++++++++++++--------- tools/virsh-network.c | 6 +- 4 files changed, 110 insertions(+), 47 deletions(-) diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst index 115b802c45..0da3827f6b 100644 --- a/docs/manpages/virsh.rst +++ b/docs/manpages/virsh.rst @@ -5908,7 +5908,10 @@ changes optionally taking effect immediately, withou= t needing to destroy and re-start the network. =20 *command* is one of "add-first", "add-last", "add" (a synonym for -add-last), "delete", or "modify". +add-last), "delete", "modify", "modify-or-add" (modify + add-last), or +"modify-or-add-first". The 'modify-or-add*' commands perform modify or +add operation depending on the given state, and can be useful for +scripting. =20 *section* is one of "bridge", "domain", "ip", "ip-dhcp-host", "ip-dhcp-range", "forward", "forward-interface", "forward-pf", diff --git a/include/libvirt/libvirt-network.h b/include/libvirt/libvirt-ne= twork.h index 58591be7ac..bb4468b160 100644 --- a/include/libvirt/libvirt-network.h +++ b/include/libvirt/libvirt-network.h @@ -176,11 +176,13 @@ int virNetworkUndefine (virN= etworkPtr network); * Since: 0.10.2 */ typedef enum { - VIR_NETWORK_UPDATE_COMMAND_NONE =3D 0, /* invalid (Since: 0.10.2)= */ - VIR_NETWORK_UPDATE_COMMAND_MODIFY =3D 1, /* modify an existing elem= ent (Since: 0.10.2) */ - VIR_NETWORK_UPDATE_COMMAND_DELETE =3D 2, /* delete an existing elem= ent (Since: 0.10.2) */ - VIR_NETWORK_UPDATE_COMMAND_ADD_LAST =3D 3, /* add an element at end o= f list (Since: 0.10.2) */ - VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST =3D 4, /* add an element at start= of list (Since: 0.10.2) */ + VIR_NETWORK_UPDATE_COMMAND_NONE =3D 0, /* invalid (Since: = 0.10.2) */ + VIR_NETWORK_UPDATE_COMMAND_MODIFY =3D 1, /* modify an existi= ng element (Since: 0.10.2) */ + VIR_NETWORK_UPDATE_COMMAND_DELETE =3D 2, /* delete an existi= ng element (Since: 0.10.2) */ + VIR_NETWORK_UPDATE_COMMAND_ADD_LAST =3D 3, /* add an element a= t end of list (Since: 0.10.2) */ + VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST =3D 4, /* add an element a= t start of list (Since: 0.10.2) */ + VIR_NETWORK_UPDATE_COMMAND_MODIFY_ADD_LAST =3D 5, /* conditionally mo= dify or add an element at end (Since: 10.4.0) */ + VIR_NETWORK_UPDATE_COMMAND_MODIFY_ADD_FIRST =3D 6, /* conditionally mo= dify or add an element at start (Since: 10.4.0) */ # ifdef VIR_ENUM_SENTINELS VIR_NETWORK_UPDATE_COMMAND_LAST /* (Since: 0.10.2) */ # endif diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c index cc92ed0b03..a7c3dea163 100644 --- a/src/conf/network_conf.c +++ b/src/conf/network_conf.c @@ -2720,6 +2720,7 @@ virNetworkDefUpdateIPDHCPHost(virNetworkDef *def, virNetworkIPDef *ipdef =3D virNetworkIPDefByIndex(def, parentIndex); virNetworkDHCPHostDef host =3D { 0 }; bool partialOkay =3D (command =3D=3D VIR_NETWORK_UPDATE_COMMAND_DELETE= ); + int foundMatchedEntry =3D -1, foundExactEntry =3D -1; =20 if (virNetworkDefUpdateCheckElementName(def, ctxt->node, "host") < 0) goto cleanup; @@ -2740,22 +2741,47 @@ virNetworkDefUpdateIPDHCPHost(virNetworkDef *def, goto cleanup; } =20 - if (command =3D=3D VIR_NETWORK_UPDATE_COMMAND_MODIFY) { + /* check if the entry already exsits */ + for (i =3D 0; i < ipdef->nhosts; i++) { =20 - /* search for the entry with this (ip|mac|name), - * and update the IP+(mac|name) */ - for (i =3D 0; i < ipdef->nhosts; i++) { - if ((host.mac && ipdef->hosts[i].mac && - !virMacAddrCompare(host.mac, ipdef->hosts[i].mac)) || - (VIR_SOCKET_ADDR_VALID(&host.ip) && - virSocketAddrEqual(&host.ip, &ipdef->hosts[i].ip)) || - (host.name && - STREQ_NULLABLE(host.name, ipdef->hosts[i].name))) { - break; - } + /* try to match any of (ip|mac|name) attributes */ + if ((host.mac && ipdef->hosts[i].mac && + !virMacAddrCompare(host.mac, ipdef->hosts[i].mac)) || + (VIR_SOCKET_ADDR_VALID(&host.ip) && + virSocketAddrEqual(&host.ip, &ipdef->hosts[i].ip)) || + (host.name && + STREQ_NULLABLE(host.name, ipdef->hosts[i].name))) { + foundMatchedEntry =3D i; + } + + /* find exact entry - all specified attributes must match */ + if ((!host.mac || !ipdef->hosts[i].mac || + !virMacAddrCompare(host.mac, ipdef->hosts[i].mac)) && + (!host.name || + STREQ_NULLABLE(host.name, ipdef->hosts[i].name)) && + (!VIR_SOCKET_ADDR_VALID(&host.ip) || + virSocketAddrEqual(&host.ip, &ipdef->hosts[i].ip))) { + foundExactEntry =3D i; + break; } + } + + /* modify-or-add: convert command to add or modify based on foundEntry= */ + if (foundEntry =3D=3D -1) { + if (command =3D=3D VIR_NETWORK_UPDATE_COMMAND_MODIFY_ADD_FIRST) + command =3D VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST; + else if (command =3D=3D VIR_NETWORK_UPDATE_COMMAND_MODIFY_ADD_LAST) + command =3D VIR_NETWORK_UPDATE_COMMAND_ADD_LAST; + } else if (foundEntry >=3D 0) { + if (command =3D=3D VIR_NETWORK_UPDATE_COMMAND_MODIFY_ADD_FIRST || + command =3D=3D VIR_NETWORK_UPDATE_COMMAND_MODIFY_ADD_LAST) + command =3D VIR_NETWORK_UPDATE_COMMAND_MODIFY; + } + + if (command =3D=3D VIR_NETWORK_UPDATE_COMMAND_MODIFY) { =20 - if (i =3D=3D ipdef->nhosts) { + /* log error if no such entry exists to be modified */ + if (foundMatchedEntry =3D=3D -1) { g_autofree char *ip =3D virSocketAddrFormat(&host.ip); virReportError(VIR_ERR_OPERATION_INVALID, _("couldn't locate an existing dhcp host entry = with \"mac=3D'%1$s'\" \"name=3D'%2$s'\" \"ip=3D'%3$s'\" in network '%4$s'"), @@ -2779,21 +2805,13 @@ virNetworkDefUpdateIPDHCPHost(virNetworkDef *def, goto cleanup; =20 /* log error if an entry with same name/address/ip already exists = */ - for (i =3D 0; i < ipdef->nhosts; i++) { - if ((host.mac && ipdef->hosts[i].mac && - !virMacAddrCompare(host.mac, ipdef->hosts[i].mac)) || - (host.name && - STREQ_NULLABLE(host.name, ipdef->hosts[i].name)) || - (VIR_SOCKET_ADDR_VALID(&host.ip) && - virSocketAddrEqual(&host.ip, &ipdef->hosts[i].ip))) { - g_autofree char *ip =3D virSocketAddrFormat(&host.ip); + if (foundMatchedEntry >=3D 0) { + virReportError(VIR_ERR_OPERATION_INVALID, + _("there is an existing dhcp host entry in netw= ork '%1$s' that matches \"\""= ), + def->name, host.mac ? host.mac : _("unknown"), + host.name, ip ? ip : _("unknown")); + goto cleanup; =20 - virReportError(VIR_ERR_OPERATION_INVALID, - _("there is an existing dhcp host entry in = network '%1$s' that matches \"\""), - def->name, host.mac ? host.mac : _("unknown= "), - host.name, ip ? ip : _("unknown")); - goto cleanup; - } } =20 /* add to beginning/end of list */ @@ -2804,18 +2822,8 @@ virNetworkDefUpdateIPDHCPHost(virNetworkDef *def, goto cleanup; } else if (command =3D=3D VIR_NETWORK_UPDATE_COMMAND_DELETE) { =20 - /* find matching entry - all specified attributes must match */ - for (i =3D 0; i < ipdef->nhosts; i++) { - if ((!host.mac || !ipdef->hosts[i].mac || - !virMacAddrCompare(host.mac, ipdef->hosts[i].mac)) && - (!host.name || - STREQ_NULLABLE(host.name, ipdef->hosts[i].name)) && - (!VIR_SOCKET_ADDR_VALID(&host.ip) || - virSocketAddrEqual(&host.ip, &ipdef->hosts[i].ip))) { - break; - } - } - if (i =3D=3D ipdef->nhosts) { + /* log error if there is no entry with exact match*/ + if (foundExactEntry =3D=3D -1) { virReportError(VIR_ERR_OPERATION_INVALID, _("couldn't locate a matching dhcp host entry i= n network '%1$s'"), def->name); @@ -2865,6 +2873,14 @@ virNetworkDefUpdateIPDHCPRange(virNetworkDef *def, return -1; } =20 + if (command =3D=3D VIR_NETWORK_UPDATE_COMMAND_MODIFY_ADD_FIRST || + command =3D=3D VIR_NETWORK_UPDATE_COMMAND_MODIFY_ADD_LAST) { + + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("dhcp ranges cannot be modified, use add command = instead of modify-or-add")); + return -1; + } + if (virNetworkDHCPRangeDefParseXML(def->name, ipdef, ctxt->node, &rang= e) < 0) return -1; =20 @@ -2964,6 +2980,13 @@ virNetworkDefUpdateForwardInterface(virNetworkDef *d= ef, goto cleanup; } =20 + if (command =3D=3D VIR_NETWORK_UPDATE_COMMAND_MODIFY_ADD_FIRST || + command =3D=3D VIR_NETWORK_UPDATE_COMMAND_MODIFY_ADD_LAST) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("forward interface entries cannot be modified, us= e add command instead of modify-or-add")); + goto cleanup; + } + /* parsing this is so simple that it doesn't have its own function */ iface.type =3D VIR_NETWORK_FORWARD_HOSTDEV_DEVICE_NETDEV; if (!(iface.device.dev =3D virXMLPropString(ctxt->node, "dev"))) { @@ -3085,6 +3108,18 @@ virNetworkDefUpdatePortGroup(virNetworkDef *def, goto cleanup; } =20 + /* modify-or-add: convert command to add or modify based on foundName = */ + if (foundName =3D=3D -1) { + if (command =3D=3D VIR_NETWORK_UPDATE_COMMAND_MODIFY_ADD_FIRST) + command =3D VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST; + else if (command =3D=3D VIR_NETWORK_UPDATE_COMMAND_MODIFY_ADD_LAST) + command =3D VIR_NETWORK_UPDATE_COMMAND_ADD_LAST; + } else if (foundName >=3D 0) { + if (command =3D=3D VIR_NETWORK_UPDATE_COMMAND_MODIFY_ADD_FIRST || + command =3D=3D VIR_NETWORK_UPDATE_COMMAND_MODIFY_ADD_LAST) + command =3D VIR_NETWORK_UPDATE_COMMAND_MODIFY; + } + /* if there is already a different default, we can't make this * one the default. */ @@ -3153,6 +3188,13 @@ virNetworkDefUpdateDNSHost(virNetworkDef *def, goto cleanup; } =20 + if (command =3D=3D VIR_NETWORK_UPDATE_COMMAND_MODIFY_ADD_FIRST || + command =3D=3D VIR_NETWORK_UPDATE_COMMAND_MODIFY_ADD_LAST) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("DNS HOST records cannot be modified, use add ins= tead of modify-or-add")); + goto cleanup; + } + if (virNetworkDefUpdateCheckElementName(def, ctxt->node, "host") < 0) goto cleanup; =20 @@ -3249,6 +3291,13 @@ virNetworkDefUpdateDNSSrv(virNetworkDef *def, goto cleanup; } =20 + if (command =3D=3D VIR_NETWORK_UPDATE_COMMAND_MODIFY_ADD_FIRST || + command =3D=3D VIR_NETWORK_UPDATE_COMMAND_MODIFY_ADD_LAST) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("DNS SRV records cannot be modified, use add comm= and instead of modify-or-add")); + goto cleanup; + } + if (virNetworkDefUpdateCheckElementName(def, ctxt->node, "srv") < 0) goto cleanup; =20 @@ -3330,6 +3379,13 @@ virNetworkDefUpdateDNSTxt(virNetworkDef *def, goto cleanup; } =20 + if (command =3D=3D VIR_NETWORK_UPDATE_COMMAND_MODIFY_ADD_FIRST || + command =3D=3D VIR_NETWORK_UPDATE_COMMAND_MODIFY_ADD_LAST) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("DNS TXT records cannot be modified, use add comm= and instead of modify-or-add")); + goto cleanup; + } + if (virNetworkDefUpdateCheckElementName(def, ctxt->node, "txt") < 0) goto cleanup; =20 diff --git a/tools/virsh-network.c b/tools/virsh-network.c index 597e3d4530..d7dc5df5a8 100644 --- a/tools/virsh-network.c +++ b/tools/virsh-network.c @@ -1231,7 +1231,8 @@ static const vshCmdOptDef opts_network_update[] =3D { .positional =3D true, .required =3D true, .completer =3D virshNetworkUpdateCommandCompleter, - .help =3D N_("type of update (add-first, add-last (add), delete, or m= odify)") + .help =3D N_("type of update (add-first, add-last (add), delete, modi= fy," + "modify-or-add, or modify-or-add-first)") }, {.name =3D "section", .type =3D VSH_OT_STRING, @@ -1260,7 +1261,8 @@ static const vshCmdOptDef opts_network_update[] =3D { =20 VIR_ENUM_IMPL(virshNetworkUpdateCommand, VIR_NETWORK_UPDATE_COMMAND_LAST, - "none", "modify", "delete", "add-last", "add-first"); + "none", "modify", "delete", "add-last", "add-first", "modify= -or-add", + "modify-or-add-first"); =20 VIR_ENUM_IMPL(virshNetworkSection, VIR_NETWORK_SECTION_LAST, --=20 2.42.1