From nobody Thu Nov 21 17:37:41 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 1716189578230186.0028156755326; Mon, 20 May 2024 00:19:38 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 2967C1DBC; Mon, 20 May 2024 03:19:37 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id B79301E72; Mon, 20 May 2024 03:09:49 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 2618819CE; Wed, 15 May 2024 06:13:09 -0400 (EDT) Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (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 17F7919D1 for ; Wed, 15 May 2024 06:13:07 -0400 (EDT) Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-6f44b5e7f07so5445218b3a.2 for ; Wed, 15 May 2024 03:13:07 -0700 (PDT) Received: from iuseotherswifi.notpua ([218.185.248.66]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-6f4d2b255a0sm10662504b3a.198.2024.05.15.03.13.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 03:13:04 -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=1715767985; x=1716372785; 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=ULtDz86KSzgUM9pcViwYOthQisvVPBJqB0Ikf7rml4uDGiqHnriq6F+mBHV7FnVIQu QQ/fx2jujwyYrr7wBwZhJdQiIlIGZ2JwFMLqL/N7IWFosrArYnOVrSZUCDEqjinpuVcR KRmFYcEyWjr1/Ee/X1d1Jeuc2DXzmudmZ+IQQ3m902oYOi6EqeQA2ZkMoCfCkLBYFGos jtvjoGRrG4bwh4YPzoj5HMmsmcWeTZ6ASl6PWrjxefa0IjoC3jWyRYN7f1otk8QZRWFL y935snphj8Y2lLpbST5He91MIVwqwviM9GBbZj+xRrSManXqlpo9F6l0GZ6SKnqXK73o tm+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715767985; x=1716372785; 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=k86FSwzfhEA1WtuXWz8BQCzHWl0KJfpFxSyPjqQSeCY8tsZZ2clt/izW4S3Behdp6m xZdI4X4vZzp4LU/IFYWcwGn3HAsCgBpfjyRE8jLnZGeBeMw7m0dfwJphbmYsYI/m8shN G245gJDfsyFsMOO+jZaRBKZ4x9g28mHt4R0rKa5nAiXEZrIfOxt9Qw8BCff2mdQaLpGZ 8jmONBsNUKfH/13XrFccJbayOs1A4x/wf/1X5tSNVYqL2DhFn4oQyRxHZS7zj/GLtjiK 8o4p9P/KU82RonQzH79ISiewtCLZzYqq1pWjyjCOyVWIzMxzzUr6kSLRqy6ib3UOinJz zJTQ== X-Gm-Message-State: AOJu0YxhFS2KDq7NRDYyhPDTjIBI/4CzPjtV6UyVdNslfh33vKoEQIPc biAo5HTCRk7MHI8coEOWovKMLx0qRaDXlrLopnGcl/b18co6DAO0Ltcs6gcW X-Google-Smtp-Source: AGHT+IF4yeMKpQA0uNyD49YpFvCDPOp3HlQSvCFJ7aGLviOGEhTfUtV6MRp7aD5lnZb7+o75L8E/jw== X-Received: by 2002:a05:6a00:1397:b0:6e6:89ad:1233 with SMTP id d2e1a72fcca58-6f4e02a6150mr20170369b3a.2.1715767985311; Wed, 15 May 2024 03:13:05 -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:41:36 +0530 Message-ID: <20240515101136.19564-1-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: DFJJ6GF43PUYNJZ4IBHF6FVEI6OL4D57 X-Message-ID-Hash: DFJJ6GF43PUYNJZ4IBHF6FVEI6OL4D57 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: 1716189580004100001 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