From nobody Tue Feb 10 20:09:13 2026 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=fail(p=none dis=none) header.from=riseup.net ARC-Seal: i=1; a=rsa-sha256; t=1696959406; cv=none; d=zohomail.com; s=zohoarc; b=ha2yKfv1IoKt/0ASE9FwlyYxCZ8/oATFpiXzzj0vW9izVii71uY9WESPrVZgP74Q657axdv+xwBiSeikbHsxDANuNFjh/Y9JsVgZKbwkdLm5TjAKKH7ZYHu6NvQ7blLmB1PleI0gnhSOudbdprXTWnO81n1yNAzZ86z1ZI0SsdQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1696959406; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Mz0hPIvFw8UXnbSWKk6RGSnD636ftqFoYiiunFwt42w=; b=TarkU1n68tKiDNUtl4AI1HcIHstmNh4EMAnyfQFHXL38iabaDfKD1l3CdbRXfMh9s7Nx5ubjWae/n8u1V97Bswjae69QpYZIKz0/14IJ8uYlecGnC0qhtmIrbiWxjlTcjxFiUoKlslnhZJehZizl3cCR1eHwX+9u8PSjF6gsT98= 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=fail 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 1696959406619802.7107317359784; Tue, 10 Oct 2023 10:36:46 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-413-cntisv6DN9S0LFdDehrAnA-1; Tue, 10 Oct 2023 13:36:35 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4A55518188C8; Tue, 10 Oct 2023 17:36:32 +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 3357840C6CA0; Tue, 10 Oct 2023 17:36:32 +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 24B8A19465BB; Tue, 10 Oct 2023 17:36:32 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 53AB41946597 for ; Tue, 10 Oct 2023 17:36:31 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 3665A40C6CA0; Tue, 10 Oct 2023 17:36:31 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast07.extmail.prod.ext.rdu2.redhat.com [10.11.55.23]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2F01A40C6CA2 for ; Tue, 10 Oct 2023 17:36:31 +0000 (UTC) Received: from us-smtp-inbound-delivery-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 04F283C1478E for ; Tue, 10 Oct 2023 17:36:31 +0000 (UTC) Received: from mx1.riseup.net (mx1.riseup.net [198.252.153.129]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-615-CMwpL33xM3KZ8vQUB4g2yA-1; Tue, 10 Oct 2023 13:36:18 -0400 Received: from fews02-sea.riseup.net (fews02-sea-pn.riseup.net [10.0.1.112]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx1.riseup.net (Postfix) with ESMTPS id 4S4jW11F1BzDqG3 for ; Tue, 10 Oct 2023 17:26:37 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by fews02-sea.riseup.net (Postfix) with ESMTPSA id 4S4jW001w0zFpcn; Tue, 10 Oct 2023 17:26:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1696959405; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc: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=Mz0hPIvFw8UXnbSWKk6RGSnD636ftqFoYiiunFwt42w=; b=QFHnWhuCBeF/1NcprQTXwhYE9cr7T0r4qhGyr5WUecI9/xJfeTQlIs1NPt6+3MsJi34N+g tdTqCRTNvAMOihxn/974oUHdDZJru1zlsDXmfEmcjTvFdLamMcCef2dzGwgvVScSvy65Ts 658aplMH2EFnjMUOmW+AWJppTO0ghdQ= X-MC-Unique: cntisv6DN9S0LFdDehrAnA-1 X-Original-To: libvir-list@listman.corp.redhat.com X-MC-Unique: CMwpL33xM3KZ8vQUB4g2yA-1 X-Riseup-User-ID: 38AA3841077A15C0EB1A21AD6797215471605F2782708CB0E57B4FBD497234EE From: K Shiva Kiran To: libvir-list@redhat.com Subject: [PATCH v2 04/11] virsh: Add new command `nwfilter-desc` Date: Tue, 10 Oct 2023 22:48:36 +0530 Message-ID: <20231010172509.103884-5-shiva_kr@riseup.net> In-Reply-To: <20231010172509.103884-1-shiva_kr@riseup.net> References: <20231010172509.103884-1-shiva_kr@riseup.net> MIME-Version: 1.0 X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 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: , Cc: K Shiva Kiran Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: riseup.net Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1696959408547100001 Content-Type: text/plain; charset="utf-8"; x-default="true" This command can be used to view/modify the `` and `<description>` fields of the Network filter object. Signed-off-by: K Shiva Kiran <shiva_kr@riseup.net> --- docs/manpages/virsh.rst | 40 ++++++++ tools/virsh-nwfilter.c | 209 ++++++++++++++++++++++++++++++++++++++++ tools/virsh-util.c | 25 +++++ tools/virsh-util.h | 9 ++ 4 files changed, 283 insertions(+) diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst index 4ae3bb4d93..3c7cbf1e11 100644 --- a/docs/manpages/virsh.rst +++ b/docs/manpages/virsh.rst @@ -8134,6 +8134,46 @@ The editor used can be supplied by the ``$VISUAL`` o= r ``$EDITOR`` environment variables, and defaults to ``vi``. =20 =20 +nwfilter-desc +------------- + +**Syntax:** + +:: + + nwfilter-desc [--nwfilter] nwfilter-name + [--title] [--edit] [--remove] + [--new-desc new-value] + +Show or modify description and title of a network filter. + +These values are user fields that allow storing arbitrary textual data to +allow easy identification of network filters. +Title should be short, although it's not enforced. +(See also ``nwfilter-metadata`` that works with XML based network filter m= etadata.) + +- *--title* + + Specifies to operate on the title field instead of description. + +- *--edit* + + Opens an editor with the current title or description. + Modifications to the contents will be saved back. + Alternatively, the new contents can be provided via the *--new-desc* opt= ion. + +- *--remove* + + Removes the title or description field. + +- *--new-desc* + + Stores the provided title/description string. + +If neither of *--edit* or *--new-desc* are specified, the title or descrip= tion +is displayed instead of being modified. + + NWFILTER BINDING COMMANDS =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D =20 diff --git a/tools/virsh-nwfilter.c b/tools/virsh-nwfilter.c index 92b2b7b3bc..615d126def 100644 --- a/tools/virsh-nwfilter.c +++ b/tools/virsh-nwfilter.c @@ -26,6 +26,7 @@ #include "viralloc.h" #include "virfile.h" #include "vsh-table.h" +#include "virxml.h" =20 virNWFilterPtr virshCommandOptNWFilterBy(vshControl *ctl, const vshCmd *cmd, @@ -345,6 +346,53 @@ virshNWFilterListCollect(vshControl *ctl, return list; } =20 +/* extract description or title from nwfilter xml */ +static char * +virshGetNWFilterDescription(vshControl *ctl, virNWFilterPtr nwfilter, + bool title, unsigned int flags, + unsigned int queryflags) +{ + char *desc =3D NULL; + g_autoptr(xmlDoc) doc =3D NULL; + g_autoptr(xmlXPathContext) ctxt =3D NULL; + int type; + + if (title) + type =3D VIR_NWFILTER_METADATA_TITLE; + else + type =3D VIR_NWFILTER_METADATA_DESCRIPTION; + + if ((desc =3D virNWFilterGetMetadata(nwfilter, type, NULL, flags))) { + return desc; + } else { + int errCode =3D virGetLastErrorCode(); + + if (errCode =3D=3D VIR_ERR_NO_NWFILTER_METADATA) { + desc =3D g_strdup(""); + vshResetLibvirtError(); + return desc; + } + + if (errCode !=3D VIR_ERR_NO_SUPPORT) + return desc; + } + + /* fall back to xml */ + if (virshNWFilterGetXMLFromNWFilter(ctl, nwfilter, queryflags, &doc, &= ctxt) < 0) + return NULL; + + if (title) + desc =3D virXPathString("string(./title[1])", ctxt); + else + desc =3D virXPathString("string(./description[1])", ctxt); + + if (!desc) + desc =3D g_strdup(""); + + return desc; +} + + /* * "nwfilter-list" command */ @@ -768,6 +816,161 @@ cmdNWFilterBindingList(vshControl *ctl, const vshCmd = *cmd G_GNUC_UNUSED) } =20 =20 +/* + * "nwfilter-desc" command + */ +static const vshCmdInfo info_nwfilter_desc[] =3D { + {.name =3D "help", + .data =3D N_("show or set network filter's description or title") + }, + {.name =3D "desc", + .data =3D N_("Allows setting or modifying the description or title of= a network filter.") + }, + {.name =3D NULL} +}; + +static const vshCmdOptDef opts_nwfilter_desc[] =3D { + {.name =3D "nwfilter", + .type =3D VSH_OT_DATA, + .flags =3D VSH_OFLAG_REQ, + .help =3D N_("network filter name or uuid"), + .completer =3D virshNWFilterNameCompleter, + }, + {.name =3D "title", + .type =3D VSH_OT_BOOL, + .help =3D N_("modify/get the title instead of description") + }, + {.name =3D "edit", + .type =3D VSH_OT_BOOL, + .help =3D N_("open an editor to modify the description") + }, + {.name =3D "remove", + .type =3D VSH_OT_BOOL, + .help =3D N_("remove the element") + }, + {.name =3D "new-desc", + .type =3D VSH_OT_ARGV, + .help =3D N_("message") + }, + {.name =3D NULL} +}; + +static bool +cmdNWFilterDesc(vshControl *ctl, const vshCmd *cmd) +{ + g_autoptr(virshNWFilter) nwfilter =3D NULL; + bool title =3D vshCommandOptBool(cmd, "title"); + bool edit =3D vshCommandOptBool(cmd, "edit"); + bool remove =3D vshCommandOptBool(cmd, "remove"); + int type; + g_autofree char *descArg =3D NULL; + const vshCmdOpt *opt =3D NULL; + g_auto(virBuffer) buf =3D VIR_BUFFER_INITIALIZER; + unsigned int flags =3D 0; + unsigned int queryflags =3D 0; + + VSH_EXCLUSIVE_OPTIONS("remove", "edit"); + + if (!(nwfilter =3D virshCommandOptNWFilter(ctl, cmd, NULL))) + return false; + + if (title) + type =3D VIR_NWFILTER_METADATA_TITLE; + else + type =3D VIR_NWFILTER_METADATA_DESCRIPTION; + + + while ((opt =3D vshCommandOptArgv(ctl, cmd, opt))) + virBufferAsprintf(&buf, "%s ", opt->data); + + virBufferTrim(&buf, " "); + + descArg =3D virBufferContentAndReset(&buf); + + if (remove) { + + if (descArg) { + vshPrintExtra(ctl, "unexpected data: \'%s\'", descArg); + return false; + } + + if (virNWFilterSetMetadata(nwfilter, type, "", NULL, NULL, flags) = < 0) + goto error; + + vshPrintExtra(ctl, "%s removed successfully", title ? "Title" : "D= escription"); + + } else if (edit || descArg) { + + g_autofree char *descNWFilter =3D NULL; + g_autofree char *descNew =3D NULL; + + if (!(descNWFilter =3D virshGetNWFilterDescription(ctl, nwfilter, + title, flags, que= ryflags))) + return false; + + if (!descArg) + descArg =3D g_strdup(descNWFilter); + + if (edit) { + g_autoptr(vshTempFile) tmp =3D NULL; + g_autofree char *desc_edited =3D NULL; + char *tmpstr; + + /* Create and open a temporary file. */ + if (!(tmp =3D vshEditWriteToTempFile(ctl, descArg))) + return false; + + /* Start the editor. */ + if (vshEditFile(ctl, tmp) =3D=3D -1) + return false; + + /* Read back the edited file. */ + if (!(desc_edited =3D vshEditReadBackFile(ctl, tmp))) + return false; + + /* strip a possible newline at the end */ + if (title && + (tmpstr =3D strrchr(desc_edited, '\n')) && + *(tmpstr+1) =3D=3D '\0') + *tmpstr =3D '\0'; + + /* Check whether XML has changed */ + if (STREQ(descNWFilter, desc_edited)) { + vshPrintExtra(ctl, "Network filter %s has not changed", ti= tle ? "title" : "description"); + return true; + } + + descNew =3D g_steal_pointer(&desc_edited); + + } else { + descNew =3D g_steal_pointer(&descArg); + } + + if (virNWFilterSetMetadata(nwfilter, type, descNew, NULL, NULL, fl= ags) < 0) + goto error; + + vshPrintExtra(ctl, "Network filter %s updated successfully", title= ? "title" : "description"); + + } else { + g_autofree char *desc =3D virshGetNWFilterDescription(ctl, nwfilte= r, title, flags, queryflags); + if (!desc) + return false; + + if (strlen(desc) > 0) { + vshPrint(ctl, "%s", desc); + } else { + vshPrintExtra(ctl, _("No %1$s for network filter: %2$s"), titl= e ? "title" : "description", virNWFilterGetName(nwfilter)); + } + } + + return true; + + error: + vshError(ctl, "Failed to set %s for network filter", title ? "titl= e" : "description"); + return false; +} + + const vshCmdDef nwfilterCmds[] =3D { {.name =3D "nwfilter-define", .handler =3D cmdNWFilterDefine, @@ -823,5 +1026,11 @@ const vshCmdDef nwfilterCmds[] =3D { .info =3D info_nwfilter_binding_list, .flags =3D 0 }, + {.name =3D "nwfilter-desc", + .handler =3D cmdNWFilterDesc, + .opts =3D opts_nwfilter_desc, + .info =3D info_nwfilter_desc, + .flags =3D 0 + }, {.name =3D NULL} }; diff --git a/tools/virsh-util.c b/tools/virsh-util.c index fb6327613a..c3af770c29 100644 --- a/tools/virsh-util.c +++ b/tools/virsh-util.c @@ -423,6 +423,31 @@ virshNetworkGetXMLFromNet(vshControl *ctl, } =20 =20 +int +virshNWFilterGetXMLFromNWFilter(vshControl *ctl, + virNWFilterPtr nwfilter, + unsigned int flags, + xmlDocPtr *xml, + xmlXPathContextPtr *ctxt) +{ + g_autofree char *desc =3D NULL; + + if (!(desc =3D virNWFilterGetXMLDesc(nwfilter, flags))) { + vshError(ctl, _("Failed to get nwfilter description xml")); + return -1; + } + + *xml =3D virXMLParseStringCtxt(desc, _("(nwfilter_definition)"), ctxt); + + if (!(*xml)) { + vshError(ctl, _("Failed to parse nwfilter description xml")); + return -1; + } + + return 0; +} + + int virshDomainGetXML(vshControl *ctl, const vshCmd *cmd, diff --git a/tools/virsh-util.h b/tools/virsh-util.h index 2386847072..4cad3d7eb9 100644 --- a/tools/virsh-util.h +++ b/tools/virsh-util.h @@ -152,6 +152,15 @@ virshNetworkGetXMLFromNet(vshControl *ctl, ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(4) ATTRIBUTE_NONNULL(5) G_GNUC_WARN_UNUSED_RESULT; =20 +int +virshNWFilterGetXMLFromNWFilter(vshControl *ctl, + virNWFilterPtr nwfilter, + unsigned int flags, + xmlDocPtr *xml, + xmlXPathContextPtr *ctxt) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(4) + ATTRIBUTE_NONNULL(5) G_GNUC_WARN_UNUSED_RESULT; + int virshDomainGetXML(vshControl *ctl, const vshCmd *cmd, --=20 2.42.0