From nobody Sat May 18 07:48:38 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=fail(p=none dis=none) header.from=riseup.net ARC-Seal: i=1; a=rsa-sha256; t=1692211697; cv=none; d=zohomail.com; s=zohoarc; b=ntxATeiBWhoJh6DhYdYYquQOTHgX2juiz6B8S6dovIcsSKPiU/Lyhj+8U14TjLh3cN9bNSJLcr7HCAy0f7cjdSTq6QDGDEx3Ur1XIiJaPvPCS9Xsd14YD3sqeyjLAGOBVnNXSzs05tD7MBP0RRhabI4qi71NgM532FjLgqo9ghg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692211697; h=Content-Type:Content-Transfer-Encoding:Cc: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=8BviNIy9GjBuF/xCdYXjX8VtIY9ucgJCxD6el8Rzp2g=; b=ZxcDh0me0rK0kWHBsc3jDEPvmVVVZdAvMY0uiv+dPCTyLuNQTQ5eEilLJtgE/M12BTWxCThUBxCM4/LNztQYLSLe3cgrexBhbCtIPXCmG4pUkJzhfg5MhRUdhlDZi6eTdJMKuSZzogjKuidHTBTRZ5gtOvKoCoMBhqI16A2Is4s= 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=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.133.124]) by mx.zohomail.com with SMTPS id 1692211697711637.4908795393656; Wed, 16 Aug 2023 11:48:17 -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-112-U59yUkjJP12uPq2tjEN9Hg-1; Wed, 16 Aug 2023 14:48:12 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6F31E80557A; Wed, 16 Aug 2023 18:48:10 +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 5C7FD6403D; Wed, 16 Aug 2023 18:48:10 +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 2891F1946A62; Wed, 16 Aug 2023 18:47:59 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 6EE32194658C for ; Wed, 16 Aug 2023 18:47:37 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id AD7EE2026D76; Wed, 16 Aug 2023 18:47:37 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast03.extmail.prod.ext.rdu2.redhat.com [10.11.55.19]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A599B2026D2A for ; Wed, 16 Aug 2023 18:47:37 +0000 (UTC) Received: from us-smtp-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 847E680CC4A for ; Wed, 16 Aug 2023 18:47:37 +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-643-vF3aosyHPkK2DT5dUO6bIQ-1; Wed, 16 Aug 2023 14:47:35 -0400 Received: from fews01-sea.riseup.net (fews01-sea-pn.riseup.net [10.0.1.109]) (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 4RQxvn6dyXzDqfk for ; Wed, 16 Aug 2023 18:47:33 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by fews01-sea.riseup.net (Postfix) with ESMTPSA id 4RQxvm5ZgrzJnDN; Wed, 16 Aug 2023 18:47:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692211696; 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=8BviNIy9GjBuF/xCdYXjX8VtIY9ucgJCxD6el8Rzp2g=; b=UnQh/Ylo/J+w5pjm2U88pfhcftztYP2F3fDWiRwOb+mRFtPMVWZ9E1yTtgb4oyQzYKhm8E hQblHGEW2v9Z48bT0MT1BWyuZ/VEhZXYO+syH8hlrPQ0YsYhJE5hfOySBXwXZFPylPJHMa oQIT45Wz4hOTuY2KDGz2LnRc8X+7pHk= X-MC-Unique: U59yUkjJP12uPq2tjEN9Hg-1 X-Original-To: libvir-list@listman.corp.redhat.com X-MC-Unique: vF3aosyHPkK2DT5dUO6bIQ-1 X-Riseup-User-ID: 7B99EEC3BB529A63299C3C95D2B360AD11FAF18612ED43AE902AA9EC1A9E32B5 From: K Shiva Kiran To: libvir-list@redhat.com Subject: [libvirt PATCH v2 1/7] Add and <description> for Network Objects Date: Thu, 17 Aug 2023 00:17:09 +0530 Message-ID: <20230816184715.101243-2-shiva_kr@riseup.net> In-Reply-To: <20230816184715.101243-1-shiva_kr@riseup.net> References: <20230816184715.101243-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.1 on 10.11.54.4 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools <libvir-list.redhat.com> List-Unsubscribe: <https://listman.redhat.com/mailman/options/libvir-list>, <mailto:libvir-list-request@redhat.com?subject=unsubscribe> List-Archive: <http://listman.redhat.com/archives/libvir-list/> List-Post: <mailto:libvir-list@redhat.com> List-Help: <mailto:libvir-list-request@redhat.com?subject=help> List-Subscribe: <https://listman.redhat.com/mailman/listinfo/libvir-list>, <mailto:libvir-list-request@redhat.com?subject=subscribe> Cc: K Shiva Kiran <shiva_kr@riseup.net> Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" <libvir-list-bounces@redhat.com> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 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: 1692211699607100009 Content-Type: text/plain; charset="utf-8"; x-default="true" This patch adds new elements <title> and <description> to the Network XML. - The <title> attribute holds a short title defined by the user and cannot contain newlines. - The <description> attribute holds any documentation that the user wants to store. - Schema definitions of <title> and <description> have been moved from domaincommon.rng to basictypes.rng for use by network and future objects. - Added Network XML parser logic for the above. Signed-off-by: K Shiva Kiran <shiva_kr@riseup.net> Reviewed-by: Michal Privoznik <mprivozn@redhat.com> --- docs/formatnetwork.rst | 11 +++++++++++ src/conf/network_conf.c | 18 ++++++++++++++++++ src/conf/network_conf.h | 2 ++ src/conf/schemas/basictypes.rng | 15 +++++++++++++++ src/conf/schemas/domaincommon.rng | 15 --------------- src/conf/schemas/network.rng | 10 ++++++++++ 6 files changed, 56 insertions(+), 15 deletions(-) diff --git a/docs/formatnetwork.rst b/docs/formatnetwork.rst index b5dc29db07..c54d53070a 100644 --- a/docs/formatnetwork.rst +++ b/docs/formatnetwork.rst @@ -30,6 +30,8 @@ The first elements provide basic metadata about the virtu= al network. <network ipv6=3D'yes' trustGuestRxFilters=3D'no'> <name>default</name> <uuid>3e3fce45-4f53-4fa7-bb32-11f34168b82b</uuid> + <title>A short description - title - of the network + Some human readable description .. .. @@ -47,6 +49,7 @@ The first elements provide basic metadata about the virtu= al network. the virtual network. The format must be RFC 4122 compliant, eg ``3e3fce45-4f53-4fa7-bb32-11f34168b82b``. If omitted when defining/crea= ting a new network, a random UUID is generated. :since:`Since 0.3.0` +``metadata`` The ``metadata`` node can be used by applications to store custom metad= ata in the form of XML nodes/trees. Applications must use custom namespaces on= their XML nodes/trees, with only one top-level element per namespace (if the @@ -65,6 +68,14 @@ The first elements provide basic metadata about the virt= ual network. documentation for more details. Note that an explicit setting of this attribute in a portgroup or the individual domain interface will overri= de the setting in the network. +``title`` + The optional element ``title`` provides space for a short description o= f the + network. The title should not contain any newlines. :since:`Since 9.7.0= ` . +``description`` + The content of the ``description`` element provides a human readable + description of the network. This data is not used by libvirt in any + way, it can contain any information the user wants. :since:`Since 9.7.0` + =20 Connectivity ~~~~~~~~~~~~ diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c index 3e137cb562..262e9a9fc7 100644 --- a/src/conf/network_conf.c +++ b/src/conf/network_conf.c @@ -281,6 +281,8 @@ virNetworkDefFree(virNetworkDef *def) virNetDevBandwidthFree(def->bandwidth); virNetDevVlanClear(&def->vlan); =20 + g_free(def->title); + g_free(def->description); xmlFreeNode(def->metadata); =20 if (def->namespaceData && def->ns.free) @@ -1599,6 +1601,17 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt, def->uuid_specified =3D true; } =20 + /* Extract short description of network (title) */ + def->title =3D virXPathString("string(./title[1])", ctxt); + if (def->title && strchr(def->title, '\n')) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Network title can't contain newlines")); + return NULL; + } + + /* Extract documentation if present */ + def->description =3D virXPathString("string(./description[1])", ctxt); + /* check if definitions with no IPv6 gateway addresses is to * allow guest-to-guest communications. */ @@ -2311,6 +2324,11 @@ virNetworkDefFormatBuf(virBuffer *buf, virUUIDFormat(uuid, uuidstr); virBufferAsprintf(buf, "%s\n", uuidstr); =20 + virBufferEscapeString(buf, "%s\n", def->title); + + virBufferEscapeString(buf, "%s\n", + def->description); + if (virXMLFormatMetadata(buf, def->metadata) < 0) return -1; =20 diff --git a/src/conf/network_conf.h b/src/conf/network_conf.h index 2b2e9d15f0..5a1bdb1284 100644 --- a/src/conf/network_conf.h +++ b/src/conf/network_conf.h @@ -249,6 +249,8 @@ struct _virNetworkDef { unsigned char uuid[VIR_UUID_BUFLEN]; bool uuid_specified; char *name; + char *title; + char *description; int connections; /* # of guest interfaces connected to this network = */ =20 char *bridge; /* Name of bridge device */ diff --git a/src/conf/schemas/basictypes.rng b/src/conf/schemas/basictypes.= rng index 2d6f1a2c84..26eb538077 100644 --- a/src/conf/schemas/basictypes.rng +++ b/src/conf/schemas/basictypes.rng @@ -610,6 +610,21 @@ =20 + + + + + + + + + + + + + diff --git a/src/conf/schemas/domaincommon.rng b/src/conf/schemas/domaincom= mon.rng index c2f56b0490..2556ac01ed 100644 --- a/src/conf/schemas/domaincommon.rng +++ b/src/conf/schemas/domaincommon.rng @@ -8,21 +8,6 @@ =20 - - - - - - - - - - - - - 0[0-7]{3}|[0-7]{1,3} diff --git a/src/conf/schemas/network.rng b/src/conf/schemas/network.rng index 4317572208..cda174ab4b 100644 --- a/src/conf/schemas/network.rng +++ b/src/conf/schemas/network.rng @@ -37,6 +37,16 @@ =20 + + + + + + + + + + --=20 2.41.0 From nobody Sat May 18 07:48:38 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=fail(p=none dis=none) header.from=riseup.net ARC-Seal: i=1; a=rsa-sha256; t=1692211680; cv=none; d=zohomail.com; s=zohoarc; b=bjNINPXu9RcrAdwA6/5sZFJcWhsrg41/S9lskGahxdlUTKKlwprQiMox9cbW6/naJY3wTCoewRyxy4gMYplpXiJLsKLHDCUqoPcX71GQwjj7ij9cHficzUPiYfHl/QQzXxzyKQ9qeyzFoF1pcgz+aMB089n8MGX3BV66yhnTxhU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692211680; h=Content-Type:Content-Transfer-Encoding:Cc: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=T5yRYcPVV9F8CVy766G53Gix2THsmsPUxEPcZCiJKzM=; b=TcrI3QVkw7zvJRig4ExYNdSEN0oRXEdiNl96vLMt65oXZXaflBCPdbiGtU6b0l4SGEbEdHsCJB4Ht3KZMm1KuhCaNPmrPWSGob330uGNmpi4WwpHcQW/3YLIEowflhoF5LwbVFob31Xu7p6DcmNxNM28Bnj0xx1W5eOvZ2TKdvY= 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 1692211680616411.43462342844293; Wed, 16 Aug 2023 11:48:00 -0700 (PDT) Received: from mimecast-mx02.redhat.com (66.187.233.73 [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-403-7m-df6_qPMK2iDI-drp0zw-1; Wed, 16 Aug 2023 14:47:54 -0400 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1669A3C0BE28; Wed, 16 Aug 2023 18:47:52 +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 DE3E14A9004; Wed, 16 Aug 2023 18:47:48 +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 4996F1946588; Wed, 16 Aug 2023 18:47:43 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 6CA8A1946588 for ; Wed, 16 Aug 2023 18:47:40 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 5882E2166B2E; Wed, 16 Aug 2023 18:47:40 +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 50A8C2166B2D for ; Wed, 16 Aug 2023 18:47:40 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-2.mimecast.com [207.211.31.81]) (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 1B16F3C0BE31 for ; Wed, 16 Aug 2023 18:47:40 +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-573-JuSon8pgNg6YxG6PlnmtfA-1; Wed, 16 Aug 2023 14:47:37 -0400 Received: from fews01-sea.riseup.net (fews01-sea-pn.riseup.net [10.0.1.109]) (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 4RQxvq3vlnzDq89 for ; Wed, 16 Aug 2023 18:47:35 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by fews01-sea.riseup.net (Postfix) with ESMTPSA id 4RQxvp2n3zzJnDN; Wed, 16 Aug 2023 18:47:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692211679; 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=T5yRYcPVV9F8CVy766G53Gix2THsmsPUxEPcZCiJKzM=; b=fgGDiVmfuJwEYmeW9pY6R426puWJLB5b3QVH6Sdry9j6J+B+1/p4Re8wbCJAm/ZkELy6+w E+WySK8HFZsXRxHEU3tMF+mIht0sUUzQGYwDsFV0J3a69IYtNbeZq9xrtaQ6Qd/MIUHsoF W3AodxXQBN+3Guk7kjtPsiRi0TxMaKo= X-MC-Unique: 7m-df6_qPMK2iDI-drp0zw-1 X-Original-To: libvir-list@listman.corp.redhat.com X-MC-Unique: JuSon8pgNg6YxG6PlnmtfA-1 X-Riseup-User-ID: 8FCB799E7EBDBCB126248AC9112DBD04C1AB4D14B678A6C62081F450AE27B5C5 From: K Shiva Kiran To: libvir-list@redhat.com Subject: [libvirt PATCH v2 2/7] Adding Public Get and Set APIs for Network Metadata Date: Thu, 17 Aug 2023 00:17:10 +0530 Message-ID: <20230816184715.101243-3-shiva_kr@riseup.net> In-Reply-To: <20230816184715.101243-1-shiva_kr@riseup.net> References: <20230816184715.101243-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.1 on 10.11.54.6 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.1 on 10.11.54.10 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: 1692211682701100001 Content-Type: text/plain; charset="utf-8"; x-default="true" This patch introduces public Get and Set APIs for modifying , <description> and <metadata> elements of the Network object. - Added enum virNetworkMetadataType to select one of the above elements to operate on. - Added error code and messages for missing metadata. - Added public API implementation. - Added driver support. Signed-off-by: K Shiva Kiran <shiva_kr@riseup.net> Reviewed-by: Michal Privoznik <mprivozn@redhat.com> --- include/libvirt/libvirt-network.h | 29 ++++++ include/libvirt/virterror.h | 1 + src/driver-network.h | 16 +++ src/libvirt-network.c | 167 ++++++++++++++++++++++++++++++ src/libvirt_public.syms | 6 ++ src/util/virerror.c | 3 + 6 files changed, 222 insertions(+) diff --git a/include/libvirt/libvirt-network.h b/include/libvirt/libvirt-ne= twork.h index 90cde0cf24..0f7ad8300f 100644 --- a/include/libvirt/libvirt-network.h +++ b/include/libvirt/libvirt-network.h @@ -547,4 +547,33 @@ virNetworkPortFree(virNetworkPortPtr port); int virNetworkPortRef(virNetworkPortPtr port); =20 +/** + * virNetworkMetadataType: + * + * Since: 9.7.0 + */ +typedef enum { + VIR_NETWORK_METADATA_DESCRIPTION =3D 0, /* Operate on <description> (S= ince: 9.7.0) */ + VIR_NETWORK_METADATA_TITLE =3D 1, /* Operate on <title> (Since: = 9.7.0) */ + VIR_NETWORK_METADATA_ELEMENT =3D 2, /* Operate on <metadata> (Sinc= e: 9.7.0) */ + +# ifdef VIR_ENUM_SENTINELS + VIR_NETWORK_METADATA_LAST /* (Since: 9.7.0) */ +# endif +} virNetworkMetadataType; + +int +virNetworkSetMetadata(virNetworkPtr network, + int type, + const char *metadata, + const char *key, + const char *uri, + unsigned int flags); + +char * +virNetworkGetMetadata(virNetworkPtr network, + int type, + const char *uri, + unsigned int flags); + #endif /* LIBVIRT_NETWORK_H */ diff --git a/include/libvirt/virterror.h b/include/libvirt/virterror.h index df13e4f11e..224eddc9e4 100644 --- a/include/libvirt/virterror.h +++ b/include/libvirt/virterror.h @@ -348,6 +348,7 @@ typedef enum { VIR_ERR_NO_HOSTNAME =3D 108, /* no domain's hostname found (S= ince: 6.1.0) */ VIR_ERR_CHECKPOINT_INCONSISTENT =3D 109, /* checkpoint can't be used (= Since: 6.10.0) */ VIR_ERR_MULTIPLE_DOMAINS =3D 110, /* more than one matching domain= found (Since: 7.1.0) */ + VIR_ERR_NO_NETWORK_METADATA =3D 111, /* Network metadata is not prese= nt (Since: 9.7.0) */ =20 # ifdef VIR_ENUM_SENTINELS VIR_ERR_NUMBER_LAST /* (Since: 5.0.0) */ diff --git a/src/driver-network.h b/src/driver-network.h index 99efd4c8aa..1d19b013c9 100644 --- a/src/driver-network.h +++ b/src/driver-network.h @@ -161,6 +161,20 @@ typedef int virNetworkPortPtr **ports, unsigned int flags); =20 +typedef int +(*virDrvNetworkSetMetadata)(virNetworkPtr network, + int type, + const char *metadata, + const char *key, + const char *uri, + unsigned int flags); + +typedef char * +(*virDrvNetworkGetMetadata)(virNetworkPtr network, + int type, + const char *uri, + unsigned int flags); + typedef struct _virNetworkDriver virNetworkDriver; =20 /** @@ -202,4 +216,6 @@ struct _virNetworkDriver { virDrvNetworkPortGetParameters networkPortGetParameters; virDrvNetworkPortDelete networkPortDelete; virDrvNetworkListAllPorts networkListAllPorts; + virDrvNetworkSetMetadata networkSetMetadata; + virDrvNetworkGetMetadata networkGetMetadata; }; diff --git a/src/libvirt-network.c b/src/libvirt-network.c index 236dfe2f5d..c0daea3a60 100644 --- a/src/libvirt-network.c +++ b/src/libvirt-network.c @@ -1915,3 +1915,170 @@ virNetworkPortRef(virNetworkPortPtr port) virObjectRef(port); return 0; } + + +/** + * virNetworkSetMetadata: + * @network: a network object + * @type: type of metadata, from virNetworkMetadataType + * @metadata: new metadata text + * @key: XML namespace key, or NULL + * @uri: XML namespace URI, or NULL + * @flags: bitwise-OR of virNetworkUpdateFlags + * + * Sets the appropriate network element given by @type to the + * value of @metadata. A @type of VIR_NETWORK_METADATA_DESCRIPTION + * is free-form text; VIR_NETWORK_METADATA_TITLE is free-form, but no + * newlines are permitted, and should be short (although the length is + * not enforced). For these two options @key and @uri are irrelevant and + * must be set to NULL. + * + * For type VIR_NETWORK_METADATA_ELEMENT @metadata must be well-formed + * XML belonging to namespace defined by @uri with local name @key. + * + * Passing NULL for @metadata says to remove that element from the + * network XML (passing the empty string leaves the element present). + * + * The resulting metadata will be present in virNetworkGetXMLDesc(), + * as well as quick access through virNetworkGetMetadata(). + * + * @flags controls whether the live network state, persistent configuratio= n, + * or both will be modified. + * + * Returns 0 on success, -1 in case of failure. + * + * Since: 9.7.0 + */ +int +virNetworkSetMetadata(virNetworkPtr network, + int type, + const char *metadata, + const char *key, + const char *uri, + unsigned int flags) +{ + virConnectPtr conn; + + VIR_DEBUG("network=3D%p, type=3D%d, metadata=3D'%s', key=3D'%s', uri= =3D'%s', flags=3D0x%x", + network, type, NULLSTR(metadata), NULLSTR(key), NULLSTR(uri), + flags); + + virResetLastError(); + + virCheckNetworkReturn(network, -1); + conn =3D network->conn; + + virCheckReadOnlyGoto(conn->flags, error); + + switch (type) { + case VIR_NETWORK_METADATA_TITLE: + if (metadata && strchr(metadata, '\n')) { + virReportInvalidArg(metadata, "%s", + _("metadata title can't contain " + "newlines")); + goto error; + } + G_GNUC_FALLTHROUGH; + case VIR_NETWORK_METADATA_DESCRIPTION: + virCheckNullArgGoto(uri, error); + virCheckNullArgGoto(key, error); + break; + case VIR_NETWORK_METADATA_ELEMENT: + virCheckNonNullArgGoto(uri, error); + if (metadata) + virCheckNonNullArgGoto(key, error); + break; + default: + /* For future expansion */ + break; + } + + if (conn->networkDriver->networkSetMetadata) { + int ret; + ret =3D conn->networkDriver->networkSetMetadata(network, type, met= adata, key, uri, + flags); + if (ret < 0) + goto error; + return ret; + } + + virReportUnsupportedError(); + + error: + virDispatchError(network->conn); + return -1; +} + + +/** + * virNetworkGetMetadata: + * @network: a network object + * @type: type of metadata, from virNetworkMetadataType + * @uri: XML namespace identifier + * @flags: bitwise-OR of virNetworkUpdateFlags + * + * Retrieves the appropriate network element given by @type. + * If VIR_NETWORK_METADATA_ELEMENT is requested parameter @uri + * must be set to the name of the namespace the requested elements + * belong to, otherwise must be NULL. + * + * If an element of the network XML is not present, the resulting + * error will be VIR_ERR_NO_NETWORK_METADATA. This method forms + * a shortcut for seeing information from virNetworkSetMetadata() + * without having to go through virNetworkGetXMLDesc(). + * + * @flags controls whether the live network state or persistent + * configuration will be queried. + * + * Returns the metadata string on success (caller must free), + * or NULL in case of failure. + * + * Since: 9.7.0 + */ +char * +virNetworkGetMetadata(virNetworkPtr network, + int type, + const char *uri, + unsigned int flags) +{ + virConnectPtr conn; + + VIR_DEBUG("network=3D%p, type=3D%d, uri=3D'%s', flags=3D0x%x", + network, type, NULLSTR(uri), flags); + + virResetLastError(); + + virCheckNetworkReturn(network, NULL); + + VIR_EXCLUSIVE_FLAGS_GOTO(VIR_NETWORK_UPDATE_AFFECT_LIVE, + VIR_NETWORK_UPDATE_AFFECT_CONFIG, + error); + + switch (type) { + case VIR_NETWORK_METADATA_TITLE: + case VIR_NETWORK_METADATA_DESCRIPTION: + virCheckNullArgGoto(uri, error); + break; + case VIR_NETWORK_METADATA_ELEMENT: + virCheckNonNullArgGoto(uri, error); + break; + default: + /* For future expansion */ + break; + } + + conn =3D network->conn; + + if (conn->networkDriver->networkGetMetadata) { + char *ret; + if (!(ret =3D conn->networkDriver->networkGetMetadata(network, typ= e, uri, flags))) + goto error; + return ret; + } + + virReportUnsupportedError(); + + error: + virDispatchError(network->conn); + return NULL; +} diff --git a/src/libvirt_public.syms b/src/libvirt_public.syms index 80742f268e..bd1e916d2a 100644 --- a/src/libvirt_public.syms +++ b/src/libvirt_public.syms @@ -932,4 +932,10 @@ LIBVIRT_9.0.0 { virDomainFDAssociate; } LIBVIRT_8.5.0; =20 +LIBVIRT_9.7.0 { + global: + virNetworkGetMetadata; + virNetworkSetMetadata; +} LIBVIRT_9.0.0; + # .... define new API here using predicted next version number .... diff --git a/src/util/virerror.c b/src/util/virerror.c index 453f19514e..227a182417 100644 --- a/src/util/virerror.c +++ b/src/util/virerror.c @@ -1287,6 +1287,9 @@ static const virErrorMsgTuple virErrorMsgStrings[] = =3D { [VIR_ERR_MULTIPLE_DOMAINS] =3D { N_("multiple matching domains found"), N_("multiple matching domains found: %1$s") }, + [VIR_ERR_NO_NETWORK_METADATA] =3D { + N_("metadata not found"), + N_("metadata not found: %1$s") }, }; =20 G_STATIC_ASSERT(G_N_ELEMENTS(virErrorMsgStrings) =3D=3D VIR_ERR_NUMBER_LAS= T); --=20 2.41.0 From nobody Sat May 18 07:48:38 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=fail(p=none dis=none) header.from=riseup.net ARC-Seal: i=1; a=rsa-sha256; t=1692211692; cv=none; d=zohomail.com; s=zohoarc; b=TBoQ25ZtNV1cwmX+PlmlHTlb9MPEtJ9mPoiYJ8fPqA6vNIZfKimoE8rj1TXBTNsWtdVe3D25rgaFy/yo82LCMj0C/+pbulqn4rPTm5LtdP/NsqTiTRXbMylqEzubs3vSN5/l7wGqi801nq9ESIfrDs9iR7D7DZxYFMlzD2o3di0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692211692; h=Content-Type:Content-Transfer-Encoding:Cc: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=482Ydq02VtrgSoFh9RvF8cbc1hdaT98MwacO5OQrRo8=; b=jeBxsH8/R3jc/bGBhkOVwxy+rgVt9+iT+coJhj+KoOKN8HXVAHos1g8Vosy5YOPS9psOL0zXJ6oMK8gNw8wESTlF48jMywGkh2ju1vI1kkIj6nTrt1Cr3vAngJdmAuVhllr5C286iDxA1HVaGiZsELNswOJzgOBLhKUSTHdf4dM= 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=fail header.from=<shiva_kr@riseup.net> (p=none dis=none) Return-Path: <libvir-list-bounces@redhat.com> 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 1692211692933668.8239614858172; Wed, 16 Aug 2023 11:48:12 -0700 (PDT) Received: from mimecast-mx02.redhat.com (66.187.233.73 [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-314-4m77JwVUNIme646pq094dw-1; Wed, 16 Aug 2023 14:48:06 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E3D501C07834; Wed, 16 Aug 2023 18:48:02 +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 CCCB81121314; Wed, 16 Aug 2023 18:48:02 +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 E4EA91946A43; Wed, 16 Aug 2023 18:47:58 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 6A1751946587 for <libvir-list@listman.corp.redhat.com>; Wed, 16 Aug 2023 18:47:40 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 8D0B74A9004; Wed, 16 Aug 2023 18:47:40 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast06.extmail.prod.ext.rdu2.redhat.com [10.11.55.22]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 855F9401E6A for <libvir-list@redhat.com>; Wed, 16 Aug 2023 18:47:40 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) (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 68510185A793 for <libvir-list@redhat.com>; Wed, 16 Aug 2023 18:47:40 +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-568-7dRfJy8JPjqNUdbK8h-xIg-1; Wed, 16 Aug 2023 14:47:38 -0400 Received: from fews01-sea.riseup.net (fews01-sea-pn.riseup.net [10.0.1.109]) (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 4RQxvs1RCtzDqfk for <libvir-list@redhat.com>; Wed, 16 Aug 2023 18:47:37 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by fews01-sea.riseup.net (Postfix) with ESMTPSA id 4RQxvr033pzJnDN; Wed, 16 Aug 2023 18:47:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692211692; 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=482Ydq02VtrgSoFh9RvF8cbc1hdaT98MwacO5OQrRo8=; b=b5UdqD3FT6qvUR5cv29CKPF95Gez1fhxxRrHfZKFsBl1uGGuJ7NZuhtfm42VI2N19jriBl 8Z5KZ9FL1t3/2Jk2pGQSD8LZT0jE6t0GAP2/AdECLXuPuwdV6vlh2/8pqDtn17rHCvRjZu ykgx02Yhkn/oTqqWWz5jwRMC9J66hV0= X-MC-Unique: 4m77JwVUNIme646pq094dw-1 X-Original-To: libvir-list@listman.corp.redhat.com X-MC-Unique: 7dRfJy8JPjqNUdbK8h-xIg-1 X-Riseup-User-ID: 1852A432E8446823867780F15D2C593A36E8AB515A707F023F4FB4C7E0839049 From: K Shiva Kiran <shiva_kr@riseup.net> To: libvir-list@redhat.com Subject: [libvirt PATCH v2 3/7] Implementing Remote Protocol for Network Metadata Date: Thu, 17 Aug 2023 00:17:11 +0530 Message-ID: <20230816184715.101243-4-shiva_kr@riseup.net> In-Reply-To: <20230816184715.101243-1-shiva_kr@riseup.net> References: <20230816184715.101243-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.1 on 10.11.54.10 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools <libvir-list.redhat.com> List-Unsubscribe: <https://listman.redhat.com/mailman/options/libvir-list>, <mailto:libvir-list-request@redhat.com?subject=unsubscribe> List-Archive: <http://listman.redhat.com/archives/libvir-list/> List-Post: <mailto:libvir-list@redhat.com> List-Help: <mailto:libvir-list-request@redhat.com?subject=help> List-Subscribe: <https://listman.redhat.com/mailman/listinfo/libvir-list>, <mailto:libvir-list-request@redhat.com?subject=subscribe> Cc: K Shiva Kiran <shiva_kr@riseup.net> Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" <libvir-list-bounces@redhat.com> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 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: 1692211694566100003 Content-Type: text/plain; charset="utf-8"; x-default="true" - Defines wire protocol format. - Implements remote driver. Signed-off-by: K Shiva Kiran <shiva_kr@riseup.net> Reviewed-by: Michal Privoznik <mprivozn@redhat.com> --- src/remote/remote_driver.c | 2 ++ src/remote/remote_protocol.x | 36 +++++++++++++++++++++++++++++++++++- src/remote_protocol-structs | 19 +++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index faad7292ed..b81c9bc611 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -8153,6 +8153,8 @@ static virNetworkDriver network_driver =3D { .networkPortSetParameters =3D remoteNetworkPortSetParameters, /* 5.5.0= */ .networkPortGetParameters =3D remoteNetworkPortGetParameters, /* 5.5.0= */ .networkPortDelete =3D remoteNetworkPortDelete, /* 5.5.0 */ + .networkSetMetadata =3D remoteNetworkSetMetadata, /* 9.7.0 */ + .networkGetMetadata =3D remoteNetworkGetMetadata, /* 9.7.0 */ }; =20 static virInterfaceDriver interface_driver =3D { diff --git a/src/remote/remote_protocol.x b/src/remote/remote_protocol.x index 5d86a51116..7ff059e393 100644 --- a/src/remote/remote_protocol.x +++ b/src/remote/remote_protocol.x @@ -3323,6 +3323,26 @@ struct remote_network_event_lifecycle_msg { int detail; }; =20 +struct remote_network_set_metadata_args { + remote_nonnull_network network; + int type; + remote_string metadata; + remote_string key; + remote_string uri; + unsigned int flags; +}; + +struct remote_network_get_metadata_args { + remote_nonnull_network network; + int type; + remote_string uri; + unsigned int flags; +}; + +struct remote_network_get_metadata_ret { + remote_nonnull_string metadata; +}; + struct remote_connect_storage_pool_event_register_any_args { int eventID; remote_storage_pool pool; @@ -6974,5 +6994,19 @@ enum remote_procedure { * @generate: none * @acl: domain:write */ - REMOTE_PROC_DOMAIN_FD_ASSOCIATE =3D 443 + REMOTE_PROC_DOMAIN_FD_ASSOCIATE =3D 443, + + /** + * @generate: both + * @acl: network:write + * @acl: network:save:!VIR_NETWORK_UPDATE_AFFECT_CONFIG|VIR_NETWORK_UP= DATE_AFFECT_LIVE + * @acl: network:save:VIR_NETWORK_UPDATE_AFFECT_CONFIG + */ + REMOTE_PROC_NETWORK_SET_METADATA =3D 444, + + /** + * @generate: both + * @acl: network:read + */ + REMOTE_PROC_NETWORK_GET_METADATA =3D 445 }; diff --git a/src/remote_protocol-structs b/src/remote_protocol-structs index 3c6c230a16..c07e0af1e6 100644 --- a/src/remote_protocol-structs +++ b/src/remote_protocol-structs @@ -2687,6 +2687,23 @@ struct remote_network_event_lifecycle_msg { int event; int detail; }; +struct remote_network_set_metadata_args { + remote_nonnull_network network; + int type; + remote_string metadata; + remote_string key; + remote_string uri; + u_int flags; +}; +struct remote_network_get_metadata_args { + remote_nonnull_network network; + int type; + remote_string uri; + u_int flags; +}; +struct remote_network_get_metadata_ret { + remote_nonnull_string metadata; +}; struct remote_connect_storage_pool_event_register_any_args { int eventID; remote_storage_pool pool; @@ -3717,4 +3734,6 @@ enum remote_procedure { REMOTE_PROC_DOMAIN_RESTORE_PARAMS =3D 441, REMOTE_PROC_DOMAIN_ABORT_JOB_FLAGS =3D 442, REMOTE_PROC_DOMAIN_FD_ASSOCIATE =3D 443, + REMOTE_PROC_NETWORK_SET_METADATA =3D 444, + REMOTE_PROC_NETWORK_GET_METADATA =3D 445, }; --=20 2.41.0 From nobody Sat May 18 07:48:38 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=fail(p=none dis=none) header.from=riseup.net ARC-Seal: i=1; a=rsa-sha256; t=1692211683; cv=none; d=zohomail.com; s=zohoarc; b=ACQIikIyk2jm4jEtcdW/1kyD1mHSncp/wlDRlQkoM9YDBAysBXwDFke5kNE9vWwvMTq3cIxZax+l5Cg1MrX6KUoCAx1bnvi/5SMbYoovC7MwcxX2k3cZw6xcyhRES2IiAiQ8vIrjCZqFX/BZd8WaGhiV99xHUl+oVV17kgtXsBw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692211683; h=Content-Type:Content-Transfer-Encoding:Cc: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=DBTAmTBhYA3RHbDJSMaCsBuY2JgyoYVtBtHqZKItAXI=; b=OxTKV7qAl/7+xj9HUiAKBJEJYSjhwEGkYax7DhUm0zWSBjbWoAIIKHc64MraGwudq3e2EwuWLaMDRBk1/K0x067hVORpzfVJcku7oEZ6ZmkDTEBKMvG3BUdf8JUcFEsqbYzRwnxlZ0NclF3/W60qLHXBAshWoDsRXsYpLQGrNJI= 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=fail header.from=<shiva_kr@riseup.net> (p=none dis=none) Return-Path: <libvir-list-bounces@redhat.com> 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 1692211683556748.245749855467; Wed, 16 Aug 2023 11:48:03 -0700 (PDT) Received: from mimecast-mx02.redhat.com (66.187.233.73 [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-16-NMqnNHUkNk2KTlgsf5HoSw-1; Wed, 16 Aug 2023 14:47:55 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 173C93C0BE36; Wed, 16 Aug 2023 18:47:52 +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 C8883C15BBA; Wed, 16 Aug 2023 18:47:50 +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 AA20119465BA; Wed, 16 Aug 2023 18:47:45 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 7132E194658F for <libvir-list@listman.corp.redhat.com>; Wed, 16 Aug 2023 18:47:42 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 503AEC15BAE; Wed, 16 Aug 2023 18:47:42 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast05.extmail.prod.ext.rdu2.redhat.com [10.11.55.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 48280C15BAD for <libvir-list@redhat.com>; Wed, 16 Aug 2023 18:47:42 +0000 (UTC) Received: from us-smtp-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 CD66A85CBE7 for <libvir-list@redhat.com>; Wed, 16 Aug 2023 18:47:41 +0000 (UTC) Received: from mx0.riseup.net (mx0.riseup.net [198.252.153.6]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-253-BjBcpML2PTaX-Ud06toR_Q-1; Wed, 16 Aug 2023 14:47:39 -0400 Received: from fews01-sea.riseup.net (fews01-sea-pn.riseup.net [10.0.1.109]) (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 mx0.riseup.net (Postfix) with ESMTPS id 4RQxvt50Yfz9t6Y for <libvir-list@redhat.com>; Wed, 16 Aug 2023 18:47:38 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by fews01-sea.riseup.net (Postfix) with ESMTPSA id 4RQxvs4HZ5zJntv; Wed, 16 Aug 2023 18:47:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692211682; 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=DBTAmTBhYA3RHbDJSMaCsBuY2JgyoYVtBtHqZKItAXI=; b=Vb1XvvPgh96fXj4DjKQjfm/+0a0JiJGmZZY1+LxjQhmbprFHhYtqmSiiohcpkbTPH/duNZ x+4TaPUdw3LgHaGNP+W1MYrjDvaeA+3Kc+ZUJR5mIr93yInewY60gAkiJyLGlQryjOeZgJ ZU2OHkaeg31/DJev4OhBvrsQXQ0ja7k= X-MC-Unique: NMqnNHUkNk2KTlgsf5HoSw-1 X-Original-To: libvir-list@listman.corp.redhat.com X-MC-Unique: BjBcpML2PTaX-Ud06toR_Q-1 X-Riseup-User-ID: C0BBC1E67D0BE7D9C7AB42E84BB5CE3800A92E379807DA5FA44D4F3062241FD1 From: K Shiva Kiran <shiva_kr@riseup.net> To: libvir-list@redhat.com Subject: [libvirt PATCH v2 4/7] virsh exposure of Network Metadata APIs Date: Thu, 17 Aug 2023 00:17:12 +0530 Message-ID: <20230816184715.101243-5-shiva_kr@riseup.net> In-Reply-To: <20230816184715.101243-1-shiva_kr@riseup.net> References: <20230816184715.101243-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.1 on 10.11.54.8 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools <libvir-list.redhat.com> List-Unsubscribe: <https://listman.redhat.com/mailman/options/libvir-list>, <mailto:libvir-list-request@redhat.com?subject=unsubscribe> List-Archive: <http://listman.redhat.com/archives/libvir-list/> List-Post: <mailto:libvir-list@redhat.com> List-Help: <mailto:libvir-list-request@redhat.com?subject=help> List-Subscribe: <https://listman.redhat.com/mailman/listinfo/libvir-list>, <mailto:libvir-list-request@redhat.com?subject=subscribe> Cc: K Shiva Kiran <shiva_kr@riseup.net> Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" <libvir-list-bounces@redhat.com> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 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: 1692211685101100005 Content-Type: text/plain; charset="utf-8"; x-default="true" Adds two new commands and a new option: - 'net-desc' to show/modify network title and description. - 'net-metadata' to show/modify network metadata. - Option '--title' for 'net-list' to print corresponding network titles in an additional column. - Documentation for all the above. - XML Fallback function `virshNetworkGetXMLFromNet` for title and description for compatibility with hosts running older versions of libvirtd. Signed-off-by: K Shiva Kiran <shiva_kr@riseup.net> Reviewed-by: Michal Privoznik <mprivozn@redhat.com> --- docs/manpages/virsh.rst | 77 ++++++++ tools/virsh-network.c | 411 ++++++++++++++++++++++++++++++++++++++-- tools/virsh-util.c | 25 +++ tools/virsh-util.h | 9 + 4 files changed, 511 insertions(+), 11 deletions(-) diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst index f4e5a0bd62..673812036d 100644 --- a/docs/manpages/virsh.rst +++ b/docs/manpages/virsh.rst @@ -5566,6 +5566,7 @@ to get a description of the XML network format used b= y libvirt. Optionally, the format of the input XML file can be validated against an internal RNG schema with *--validate*. =20 + net-define ---------- =20 @@ -5581,6 +5582,38 @@ Optionally, the format of the input XML file can be = validated against an internal RNG schema with *--validate*. =20 =20 +net-desc +-------- + +**Syntax:** + +:: + + net-desc network [[--live] [--config] | + [--current]] [--title] [--edit] [--new-desc + New description or title message] + +Show or modify description and title of a network. These values are user +fields that allow storing arbitrary textual data to allow easy +identification of networks. Title should be short, although it's not enfor= ced. +(See also ``net-metadata`` that works with XML based network metadata.) + +Flags *--live* or *--config* select whether this command works on live +or persistent definitions of the network. If both *--live* and *--config* +are specified, the *--config* option takes precedence on getting the curre= nt +description and both live configuration and config are updated while setti= ng +the description. *--current* is exclusive and implied if none of these was +specified. + +Flag *--edit* specifies that an editor with the contents of current +description or title should be opened and the contents saved back afterwar= ds. + +Flag *--title* selects operation on the title field instead of description. + +If neither of *--edit* and *--new-desc* are specified the note or descript= ion +is displayed instead of being modified. + + net-destroy ----------- =20 @@ -5689,6 +5722,7 @@ net-list { [--table] | --name | --uuid } [--persistent] [<--transient>] [--autostart] [<--no-autostart>] + [--title] =20 Returns the list of active networks, if *--all* is specified this will also include defined but inactive networks, if *--inactive* is specified only t= he @@ -5703,12 +5737,55 @@ instead of names. Flag *--table* specifies that the= legacy table-formatted output should be used. This is the default. All of these are mutually exclusive. =20 +If *--title* is specified, then the short network description (title) is +printed in an extra column. This flag is usable only with the default +*--table* output. + NOTE: When talking to older servers, this command is forced to use a serie= s of API calls with an inherent race, where a pool might not be listed or might= appear more than once if it changed state between calls while the list was being collected. Newer servers do not have this problem. =20 =20 +net-metadata +------------ + +**Syntax:** + +:: + + net-metadata network [[--live] [--config] | [--current]] + [--edit] [uri] [key] [set] [--remove] + +Show or modify custom XML metadata of a network. The metadata is a user +defined XML that allows storing arbitrary XML data in the network definiti= on. +Multiple separate custom metadata pieces can be stored in the network XML. +The pieces are identified by a private XML namespace provided via the +*uri* argument. (See also ``net-desc`` that works with textual metadata of +a network, such as title and description.) + +Flags *--live* or *--config* select whether this command works on live +or persistent definitions of the network. If both *--live* and *--config* +are specified, the *--config* option takes precedence on getting the curre= nt +description and both live configuration and config are updated while setti= ng +the description. *--current* is exclusive and implied if none of these was +specified. + +Flag *--remove* specifies that the metadata element specified by the *uri* +argument should be removed rather than updated. + +Flag *--edit* specifies that an editor with the metadata identified by the +*uri* argument should be opened and the contents saved back afterwards. +Otherwise the new contents can be provided via the *set* argument. + +When setting metadata via *--edit* or *set* the *key* argument must be +specified and is used to prefix the custom elements to bind them +to the private namespace. + +If neither of *--edit* and *set* are specified the XML metadata correspond= ing +to the *uri* namespace is displayed instead of being modified. + + net-name -------- =20 diff --git a/tools/virsh-network.c b/tools/virsh-network.c index 42b7dba761..5a5cd15e24 100644 --- a/tools/virsh-network.c +++ b/tools/virsh-network.c @@ -330,6 +330,353 @@ cmdNetworkDestroy(vshControl *ctl, const vshCmd *cmd) return ret; } =20 +/* + * "net-desc" command + */ +static const vshCmdInfo info_network_desc[] =3D { + {.name =3D "help", + .data =3D N_("show or set network's description or title") + }, + {.name =3D "desc", + .data =3D N_("Allows setting or modifying the description or title of= a network.") + }, + {.name =3D NULL} +}; + +static const vshCmdOptDef opts_network_desc[] =3D { + VIRSH_COMMON_OPT_NETWORK_FULL(0), + VIRSH_COMMON_OPT_LIVE(N_("modify/get running state")), + VIRSH_COMMON_OPT_CONFIG(N_("modify/get persistent configuration")), + VIRSH_COMMON_OPT_CURRENT(N_("modify/get current state configuration")), + {.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 "new-desc", + .type =3D VSH_OT_ARGV, + .help =3D N_("message") + }, + {.name =3D NULL} +}; + +/* extract description or title from network xml */ +static char * +virshGetNetworkDescription(vshControl *ctl, virNetworkPtr net, + bool title, unsigned int flags) +{ + char *desc =3D NULL; + g_autoptr(xmlDoc) doc =3D NULL; + g_autoptr(xmlXPathContext) ctxt =3D NULL; + int type; + + if (title) + type =3D VIR_NETWORK_METADATA_TITLE; + else + type =3D VIR_NETWORK_METADATA_DESCRIPTION; + + if ((desc =3D virNetworkGetMetadata(net, type, NULL, flags))) { + return desc; + } else { + int errCode =3D virGetLastErrorCode(); + + if (errCode =3D=3D VIR_ERR_NO_NETWORK_METADATA) { + desc =3D g_strdup(""); + vshResetLibvirtError(); + return desc; + } + + if (errCode !=3D VIR_ERR_NO_SUPPORT) + return desc; + } + + /* fall back to xml */ + if (virshNetworkGetXMLFromNet(ctl, net, flags, &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; +} + +static bool +cmdNetworkDesc(vshControl *ctl, const vshCmd *cmd) +{ + g_autoptr(virshNetwork) net =3D NULL; + bool config =3D vshCommandOptBool(cmd, "config"); + bool live =3D vshCommandOptBool(cmd, "live"); + bool current =3D vshCommandOptBool(cmd, "current"); + + bool title =3D vshCommandOptBool(cmd, "title"); + bool edit =3D vshCommandOptBool(cmd, "edit"); + + 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 VIR_NETWORK_UPDATE_AFFECT_CURRENT; + unsigned int queryflags =3D 0; + + VSH_EXCLUSIVE_OPTIONS_VAR(current, live); + VSH_EXCLUSIVE_OPTIONS_VAR(current, config); + + if (config) { + flags |=3D VIR_NETWORK_UPDATE_AFFECT_CONFIG; + queryflags |=3D VIR_NETWORK_XML_INACTIVE; + } + if (live) + flags |=3D VIR_NETWORK_UPDATE_AFFECT_LIVE; + + if (!(net =3D virshCommandOptNetwork(ctl, cmd, NULL))) + return false; + + if (title) + type =3D VIR_NETWORK_METADATA_TITLE; + else + type =3D VIR_NETWORK_METADATA_DESCRIPTION; + + while ((opt =3D vshCommandOptArgv(ctl, cmd, opt))) + virBufferAsprintf(&buf, "%s ", opt->data); + + virBufferTrim(&buf, " "); + + descArg =3D virBufferContentAndReset(&buf); + + if (edit || descArg) { + g_autofree char *descNet =3D NULL; + g_autofree char *descNew =3D NULL; + + if (!(descNet =3D virshGetNetworkDescription(ctl, net, title, quer= yflags))) + return false; + + if (!descArg) + descArg =3D g_strdup(descNet); + + if (edit) { + g_autoptr(vshTempFile) tmp =3D NULL; + g_autofree char *desc_edited =3D NULL; + char *tmpstr; + + /* Create and open the 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 of file; some + * editors enforce a newline, this makes editing the title + * more convenient */ + if (title && + (tmpstr =3D strrchr(desc_edited, '\n')) && + *(tmpstr+1) =3D=3D '\0') + *tmpstr =3D '\0'; + + /* Compare original XML with edited. Has it changed at all? */ + if (STREQ(descNet, desc_edited)) { + if (title) + vshPrintExtra(ctl, "%s", _("Network title not changed\= n")); + else + vshPrintExtra(ctl, "%s", _("Network description not ch= anged\n")); + + return true; + } + + descNew =3D g_steal_pointer(&desc_edited); + } else { + descNew =3D g_steal_pointer(&descArg); + } + + if (virNetworkSetMetadata(net, type, descNew, NULL, NULL, flags) <= 0) { + if (title) + vshError(ctl, "%s", _("Failed to set new network title")); + else + vshError(ctl, "%s", _("Failed to set new network descripti= on")); + + return false; + } + + if (title) + vshPrintExtra(ctl, "%s", _("Network title updated successfully= ")); + else + vshPrintExtra(ctl, "%s", _("Network description updated succes= sfully")); + + } else { + g_autofree char *desc =3D virshGetNetworkDescription(ctl, net, tit= le, queryflags); + if (!desc) + return false; + + if (strlen(desc) > 0) { + vshPrint(ctl, "%s", desc); + } else { + if (title) + vshPrintExtra(ctl, _("No title for network: %1$s"), virNet= workGetName(net)); + else + vshPrintExtra(ctl, _("No description for network: %1$s"), = virNetworkGetName(net)); + } + } + + return true; +} + +/* + * "net-metadata" command + */ +static const vshCmdInfo info_network_metadata[] =3D { + {.name =3D "help", + .data =3D N_("show or set network's custom XML metadata") + }, + {.name =3D "desc", + .data =3D N_("Shows or modifies the XML metadata of a network.") + }, + {.name =3D NULL} +}; + +static const vshCmdOptDef opts_network_metadata[] =3D { + VIRSH_COMMON_OPT_NETWORK_FULL(0), + {.name =3D "uri", + .type =3D VSH_OT_DATA, + .flags =3D VSH_OFLAG_REQ, + .help =3D N_("URI of the namespace") + }, + VIRSH_COMMON_OPT_LIVE(N_("modify/get running state")), + VIRSH_COMMON_OPT_CONFIG(N_("modify/get persistent configuration")), + VIRSH_COMMON_OPT_CURRENT(N_("modify/get current state configuration")), + {.name =3D "edit", + .type =3D VSH_OT_BOOL, + .help =3D N_("use an editor to change the metadata") + }, + {.name =3D "key", + .type =3D VSH_OT_STRING, + .help =3D N_("key to be used as a namespace identifier"), + }, + {.name =3D "set", + .type =3D VSH_OT_STRING, + .completer =3D virshCompleteEmpty, + .help =3D N_("new metadata to set"), + }, + {.name =3D "remove", + .type =3D VSH_OT_BOOL, + .help =3D N_("remove the metadata corresponding to an uri") + }, + {.name =3D NULL} +}; + +/* helper to add new metadata using the --edit option */ +static char * +virshNetworkGetEditMetadata(vshControl *ctl G_GNUC_UNUSED, + virNetworkPtr net, + const char *uri, + unsigned int flags) +{ + char *ret; + + if (!(ret =3D virNetworkGetMetadata(net, VIR_NETWORK_METADATA_ELEMENT, + uri, flags))) { + vshResetLibvirtError(); + ret =3D g_strdup("\n"); + } + + return ret; +} + +static bool +cmdNetworkMetadata(vshControl *ctl, const vshCmd *cmd) +{ + g_autoptr(virshNetwork) net =3D NULL; + g_autoptr(xmlXPathContext) ctxt =3D NULL; + bool config =3D vshCommandOptBool(cmd, "config"); + bool live =3D vshCommandOptBool(cmd, "live"); + bool current =3D vshCommandOptBool(cmd, "current"); + bool edit =3D vshCommandOptBool(cmd, "edit"); + bool rem =3D vshCommandOptBool(cmd, "remove"); + const char *set =3D NULL; + const char *uri =3D NULL; + const char *key =3D NULL; + unsigned int flags =3D VIR_NETWORK_UPDATE_AFFECT_CURRENT; + bool ret =3D false; + + VSH_EXCLUSIVE_OPTIONS_VAR(current, live); + VSH_EXCLUSIVE_OPTIONS_VAR(current, config); + VSH_EXCLUSIVE_OPTIONS("edit", "set"); + VSH_EXCLUSIVE_OPTIONS("remove", "set"); + VSH_EXCLUSIVE_OPTIONS("remove", "edit"); + + if (config) + flags |=3D VIR_NETWORK_UPDATE_AFFECT_CONFIG; + if (live) + flags |=3D VIR_NETWORK_UPDATE_AFFECT_LIVE; + + if (!(net =3D virshCommandOptNetwork(ctl, cmd, NULL))) + return false; + + if (vshCommandOptStringReq(ctl, cmd, "uri", &uri) < 0 || + vshCommandOptStringReq(ctl, cmd, "key", &key) < 0 || + vshCommandOptStringReq(ctl, cmd, "set", &set) < 0) + return false; + + if ((set || edit) && !key) { + vshError(ctl, "%s", + _("namespace key is required when modifying metadata")); + return false; + } + + if (set || rem) { + if (virNetworkSetMetadata(net, VIR_NETWORK_METADATA_ELEMENT, + set, key, uri, flags)) + return false; + + if (rem) + vshPrintExtra(ctl, "%s\n", _("Metadata removed")); + else + vshPrintExtra(ctl, "%s\n", _("Metadata modified")); + } else if (edit) { +#define EDIT_GET_XML \ + virshNetworkGetEditMetadata(ctl, net, uri, flags) +#define EDIT_NOT_CHANGED \ + do { \ + vshPrintExtra(ctl, "%s", _("Metadata not changed")); \ + ret =3D true; \ + goto edit_cleanup; \ + } while (0) + +#define EDIT_DEFINE \ + (virNetworkSetMetadata(net, VIR_NETWORK_METADATA_ELEMENT, doc_edit= ed, \ + key, uri, flags) =3D=3D 0) +#include "virsh-edit.c" + + vshPrintExtra(ctl, "%s\n", _("Metadata modified")); + } else { + g_autofree char *data =3D NULL; + g_autoptr(xmlDoc) doc =3D NULL; + /* get */ + if (!(data =3D virNetworkGetMetadata(net, VIR_NETWORK_METADATA_ELE= MENT, + uri, flags))) + return false; + + vshPrint(ctl, "%s\n", data); + } + + ret =3D true; + + cleanup: + return ret; +} + /* * "net-dumpxml" command */ @@ -708,6 +1055,10 @@ static const vshCmdOptDef opts_network_list[] =3D { .type =3D VSH_OT_BOOL, .help =3D N_("list table (default)") }, + {.name =3D "title", + .type =3D VSH_OT_BOOL, + .help =3D N_("show network title") + }, {.name =3D NULL} }; =20 @@ -721,6 +1072,7 @@ cmdNetworkList(vshControl *ctl, const vshCmd *cmd G_GN= UC_UNUSED) size_t i; bool ret =3D false; bool optName =3D vshCommandOptBool(cmd, "name"); + bool optTitle =3D vshCommandOptBool(cmd, "title"); bool optTable =3D vshCommandOptBool(cmd, "table"); bool optUUID =3D vshCommandOptBool(cmd, "uuid"); char uuid[VIR_UUID_STRING_BUFLEN]; @@ -754,8 +1106,12 @@ cmdNetworkList(vshControl *ctl, const vshCmd *cmd G_G= NUC_UNUSED) return false; =20 if (optTable) { - table =3D vshTableNew(_("Name"), _("State"), _("Autostart"), - _("Persistent"), NULL); + if (optTitle) + table =3D vshTableNew(_("Name"), _("State"), _("Autostart"), + _("Persistent"), _("Title"), NULL); + else + table =3D vshTableNew(_("Name"), _("State"), _("Autostart"), + _("Persistent"), NULL); if (!table) goto cleanup; } @@ -771,16 +1127,37 @@ cmdNetworkList(vshControl *ctl, const vshCmd *cmd G_= GNUC_UNUSED) else autostartStr =3D is_autostart ? _("yes") : _("no"); =20 - if (vshTableRowAppend(table, - virNetworkGetName(network), - virNetworkIsActive(network) ? - _("active") : _("inactive"), - autostartStr, - virNetworkIsPersistent(network) ? - _("yes") : _("no"), - NULL) < 0) - goto cleanup; + if (optTitle) { + g_autofree char *title =3D NULL; + + if (!(title =3D virshGetNetworkDescription(ctl, network, t= rue, 0))) + goto cleanup; + if (vshTableRowAppend(table, + virNetworkGetName(network), + virNetworkIsActive(network) ? + _("active") : _("inactive"), + autostartStr, + virNetworkIsPersistent(network) ? + _("yes") : _("no"), + title, + NULL) < 0) + goto cleanup; + + } else { + if (vshTableRowAppend(table, + virNetworkGetName(network), + virNetworkIsActive(network) ? + _("active") : _("inactive"), + autostartStr, + virNetworkIsPersistent(network) ? + _("yes") : _("no"), + NULL) < 0) + goto cleanup; + + } + } else if (optUUID) { + if (virNetworkGetUUIDString(network, uuid) < 0) { vshError(ctl, "%s", _("Failed to get network's UUID")); goto cleanup; @@ -1825,6 +2202,12 @@ const vshCmdDef networkCmds[] =3D { .info =3D info_network_define, .flags =3D 0 }, + {.name =3D "net-desc", + .handler =3D cmdNetworkDesc, + .opts =3D opts_network_desc, + .info =3D info_network_desc, + .flags =3D 0 + }, {.name =3D "net-destroy", .handler =3D cmdNetworkDestroy, .opts =3D opts_network_destroy, @@ -1867,6 +2250,12 @@ const vshCmdDef networkCmds[] =3D { .info =3D info_network_list, .flags =3D 0 }, + {.name =3D "net-metadata", + .handler =3D cmdNetworkMetadata, + .opts =3D opts_network_metadata, + .info =3D info_network_metadata, + .flags =3D 0 + }, {.name =3D "net-name", .handler =3D cmdNetworkName, .opts =3D opts_network_name, diff --git a/tools/virsh-util.c b/tools/virsh-util.c index 61e403a636..fb6327613a 100644 --- a/tools/virsh-util.c +++ b/tools/virsh-util.c @@ -398,6 +398,31 @@ virshDomainGetXMLFromDom(vshControl *ctl, } =20 =20 +int +virshNetworkGetXMLFromNet(vshControl *ctl, + virNetworkPtr net, + unsigned int flags, + xmlDocPtr *xml, + xmlXPathContextPtr *ctxt) +{ + g_autofree char *desc =3D NULL; + + if (!(desc =3D virNetworkGetXMLDesc(net, flags))) { + vshError(ctl, _("Failed to get network description xml")); + return -1; + } + + *xml =3D virXMLParseStringCtxt(desc, _("(network_definition)"), ctxt); + + if (!(*xml)) { + vshError(ctl, _("Failed to parse network 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 0f81a2771b..2386847072 100644 --- a/tools/virsh-util.h +++ b/tools/virsh-util.h @@ -143,6 +143,15 @@ virshDomainGetXMLFromDom(vshControl *ctl, ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(4) ATTRIBUTE_NONNULL(5) G_GNUC_WARN_UNUSED_RESULT; =20 +int +virshNetworkGetXMLFromNet(vshControl *ctl, + virNetworkPtr net, + 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.41.0 From nobody Sat May 18 07:48:38 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=fail(p=none dis=none) header.from=riseup.net ARC-Seal: i=1; a=rsa-sha256; t=1692211682; cv=none; d=zohomail.com; s=zohoarc; b=U9AZ8uxxGtVh/xjQlMwxf7hh1X6MJtCFRFfRyQRlcDfBzwOMrjYrKUCVnAbG5WO8j8PbNoC47BrsSG4QjYyW3lPU2OR9ijw3p+HaozS4Lu0ed+hWGlI6iEhrRK8egYMAikucI2zH3OLxcPGtxpKId/RlkJdaLAyEFIKWOF4Iu6g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692211682; h=Content-Type:Content-Transfer-Encoding:Cc: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=GMUsx1YypMK7lfcSiiPp8dF+FsDwLTCVPvUw1Difjho=; b=VwihgH9hy57sreqCpsqxQEfZwyRX3BEzFmlCsXBknt12UaLVvYZW0CwUX5AomNK9PCzT+3EEV81CP3/3lVHdrlKGw6l4a7NQXNU3OERhAAfM4DTUrvBl82ojWjD/bFVLhVzxorme2/bn8XNRyN8ko9kLqhivjju3QXbsYS9gJFc= 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=<shiva_kr@riseup.net> (p=none dis=none) Return-Path: <libvir-list-bounces@redhat.com> 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 169221168202777.92261400094446; Wed, 16 Aug 2023 11:48:02 -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-435-AlqqI_gnP6GvRk1qV7zdeQ-1; Wed, 16 Aug 2023 14:47:55 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C514F185A794; Wed, 16 Aug 2023 18:47:52 +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 ADFB4C15BAD; Wed, 16 Aug 2023 18:47:52 +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 015B9194658F; Wed, 16 Aug 2023 18:47:51 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id C592819465BA for <libvir-list@listman.corp.redhat.com>; Wed, 16 Aug 2023 18:47:44 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id A4A472026D68; Wed, 16 Aug 2023 18:47:44 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast03.extmail.prod.ext.rdu2.redhat.com [10.11.55.19]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9C78C2026D4B for <libvir-list@redhat.com>; Wed, 16 Aug 2023 18:47:44 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-inbound-delivery-1.mimecast.com [205.139.110.61]) (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 6AA8B810C2A for <libvir-list@redhat.com>; Wed, 16 Aug 2023 18:47:44 +0000 (UTC) Received: from mx0.riseup.net (mx0.riseup.net [198.252.153.6]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-592-MFdGPvSIPvWtyVchjS2DNg-1; Wed, 16 Aug 2023 14:47:42 -0400 Received: from fews01-sea.riseup.net (fews01-sea-pn.riseup.net [10.0.1.109]) (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 mx0.riseup.net (Postfix) with ESMTPS id 4RQxvw2HB7z9t9L for <libvir-list@redhat.com>; Wed, 16 Aug 2023 18:47:40 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by fews01-sea.riseup.net (Postfix) with ESMTPSA id 4RQxvv1YWDzJnDN; Wed, 16 Aug 2023 18:47:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692211680; 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=GMUsx1YypMK7lfcSiiPp8dF+FsDwLTCVPvUw1Difjho=; b=I0Vwwl34+1/5aZQcDnOZY+efguoFOQ9zAQafiHk8vJ15n7ts3is9/HQSquni3pf3JNi+rd GHaOGH8oAAVo5EzUX/tfOerQ0iKgsCye9HMcF3OQnNXzdSxj4GVoMQGxAK5BnudlyNaX2U 27y7KDyEGxACQ1FPqHcEXYRoFKA3q/E= X-MC-Unique: AlqqI_gnP6GvRk1qV7zdeQ-1 X-Original-To: libvir-list@listman.corp.redhat.com X-MC-Unique: MFdGPvSIPvWtyVchjS2DNg-1 X-Riseup-User-ID: EDECC3EE305E022E3B80FDC3F1C9AF7401C7E07FE5D661094A3A3191E71B4278 From: K Shiva Kiran <shiva_kr@riseup.net> To: libvir-list@redhat.com Subject: [libvirt PATCH v2 5/7] Add virNetworkObj Get and Set Methods for Metadata Date: Thu, 17 Aug 2023 00:17:13 +0530 Message-ID: <20230816184715.101243-6-shiva_kr@riseup.net> In-Reply-To: <20230816184715.101243-1-shiva_kr@riseup.net> References: <20230816184715.101243-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.1 on 10.11.54.4 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools <libvir-list.redhat.com> List-Unsubscribe: <https://listman.redhat.com/mailman/options/libvir-list>, <mailto:libvir-list-request@redhat.com?subject=unsubscribe> List-Archive: <http://listman.redhat.com/archives/libvir-list/> List-Post: <mailto:libvir-list@redhat.com> List-Help: <mailto:libvir-list-request@redhat.com?subject=help> List-Subscribe: <https://listman.redhat.com/mailman/listinfo/libvir-list>, <mailto:libvir-list-request@redhat.com?subject=subscribe> Cc: K Shiva Kiran <shiva_kr@riseup.net> Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" <libvir-list-bounces@redhat.com> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 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: 1692211683392100003 Content-Type: text/plain; charset="utf-8"; x-default="true" - Introduces virNetworkObjGetMetadata() and virNetworkObjSetMetadata(). - These functions implement common behaviour that can be reused by network drivers. - Introduces virNetworkObjUpdateModificationImpact() among other helper functions that resolve the live/persistent state of the network before setting metadata. - Eliminates redundant call of virNetworkObjSetDefTransient() in virNetworkConfigChangeSetup() among others. - Substituted redundant logic in networkUpdate() with a call to virNetworkObjUpdateModificationImpact(). Signed-off-by: K Shiva Kiran <shiva_kr@riseup.net> Reviewed-by: Michal Privoznik <mprivozn@redhat.com> --- src/conf/virnetworkobj.c | 329 ++++++++++++++++++++++++++++++++++-- src/conf/virnetworkobj.h | 21 +++ src/libvirt_private.syms | 3 + src/network/bridge_driver.c | 14 +- src/test/test_driver.c | 16 +- 5 files changed, 347 insertions(+), 36 deletions(-) diff --git a/src/conf/virnetworkobj.c b/src/conf/virnetworkobj.c index b8b86da06f..20ee8eb58a 100644 --- a/src/conf/virnetworkobj.c +++ b/src/conf/virnetworkobj.c @@ -725,7 +725,6 @@ virNetworkObjReplacePersistentDef(virNetworkObj *obj, */ static int virNetworkObjConfigChangeSetup(virNetworkObj *obj, - virNetworkXMLOption *xmlopt, unsigned int flags) { bool isActive; @@ -738,17 +737,10 @@ virNetworkObjConfigChangeSetup(virNetworkObj *obj, return -1; } =20 - if (flags & VIR_NETWORK_UPDATE_AFFECT_CONFIG) { - if (!obj->persistent) { + if ((flags & VIR_NETWORK_UPDATE_AFFECT_CONFIG) && + !obj->persistent) { virReportError(VIR_ERR_OPERATION_INVALID, "%s", - _("cannot change persistent config of a " - "transient network")); - return -1; - } - /* this should already have been done by the driver, but do it - * anyway just in case. - */ - if (isActive && (virNetworkObjSetDefTransient(obj, false, xmlopt) = < 0)) + _("cannot change persistent config of a transie= nt network")); return -1; } =20 @@ -1187,7 +1179,7 @@ virNetworkObjUpdate(virNetworkObj *obj, g_autoptr(virNetworkDef) configdef =3D NULL; =20 /* normalize config data, and check for common invalid requests. */ - if (virNetworkObjConfigChangeSetup(obj, xmlopt, flags) < 0) + if (virNetworkObjConfigChangeSetup(obj, flags) < 0) return -1; =20 if (flags & VIR_NETWORK_UPDATE_AFFECT_LIVE) { @@ -1822,3 +1814,316 @@ virNetworkObjLoadAllPorts(virNetworkObj *net, =20 return 0; } + + +/** + * virNetworkObjUpdateModificationImpact: + * + * @obj: network object + * @flags: flags to update the modification impact on + * + * Resolves virNetworkUpdateFlags in @flags so that they correctly + * apply to the actual state of @obj. @flags may be modified after call to= this + * function. + * + * Returns 0 on success if @flags point to a valid combination for @obj or= -1 on + * error. + */ +int +virNetworkObjUpdateModificationImpact(virNetworkObj *obj, + unsigned int *flags) +{ + bool isActive =3D virNetworkObjIsActive(obj); + + if ((*flags & (VIR_NETWORK_UPDATE_AFFECT_LIVE | VIR_NETWORK_UPDATE_AFF= ECT_CONFIG)) =3D=3D + VIR_NETWORK_UPDATE_AFFECT_CURRENT) { + if (isActive) + *flags |=3D VIR_NETWORK_UPDATE_AFFECT_LIVE; + else + *flags |=3D VIR_NETWORK_UPDATE_AFFECT_CONFIG; + } + + if (virNetworkObjConfigChangeSetup(obj, *flags) < 0) + return -1; + + return 0; +} + + +/** + * virNetworkObjGetDefs: + * + * @net: network object + * @flags: for virNetworkUpdateFlags + * @liveDef: Set the pointer to the live definition of @net. + * @persDef: Set the pointer to the config definition of @net. + * + * Helper function to resolve @flags and retrieve correct network pointer + * objects. This function should be used only when the network driver + * creates net->newDef once the network has started. + * + * If @liveDef or @persDef are set it implies that @flags request modifica= tion + * thereof. + * + * Returns 0 on success and sets @liveDef and @persDef; -1 if @flags are + * inappropriate. + */ +static int +virNetworkObjGetDefs(virNetworkObj *net, + unsigned int flags, + virNetworkDef **liveDef, + virNetworkDef **persDef) +{ + if (liveDef) + *liveDef =3D NULL; + + if (persDef) + *persDef =3D NULL; + + if (virNetworkObjUpdateModificationImpact(net, &flags) < 0) + return -1; + + if (virNetworkObjIsActive(net)) { + if (liveDef && (flags & VIR_NETWORK_UPDATE_AFFECT_LIVE)) + *liveDef =3D net->def; + + if (persDef && (flags & VIR_NETWORK_UPDATE_AFFECT_CONFIG)) + *persDef =3D net->newDef; + } else { + if (persDef) + *persDef =3D net->def; + } + + return 0; +} + + +/** + * virNetworkObjGetOneDefState: + * + * @net: Network object + * @flags: for virNetworkUpdateFlags + * @live: set to true if live config was returned (may be omitted) + * + * Helper function to resolve @flags and return the correct network pointer + * object. This function returns one of @net->def or @net->persistentDef + * according to @flags. @live is set to true if the live net config will be + * returned. This helper should be used only in APIs that guarantee + * that @flags contains exactly one of VIR_NETWORK_UPDATE_AFFECT_LIVE or + * VIR_NETWORK_UPDATE_AFFECT_CONFIG and not both. + * + * Returns the correct definition pointer or NULL on error. + */ +static virNetworkDef * +virNetworkObjGetOneDefState(virNetworkObj *net, + unsigned int flags, + bool *live) +{ + if (flags & VIR_NETWORK_UPDATE_AFFECT_LIVE && + flags & VIR_NETWORK_UPDATE_AFFECT_CONFIG) { + virReportInvalidArg(flags, "%s", + _("Flags 'VIR_NETWORK_UPDATE_AFFECT_LIVE' and = 'VIR_NETWORK_UPDATE_AFFECT_CONFIG' are mutually exclusive")); + return NULL; + } + + if (virNetworkObjUpdateModificationImpact(net, &flags) < 0) + return NULL; + + if (live) + *live =3D flags & VIR_NETWORK_UPDATE_AFFECT_LIVE; + + if (virNetworkObjIsActive(net) && flags & VIR_NETWORK_UPDATE_AFFECT_CO= NFIG) + return net->newDef; + + return net->def; +} + + +/** + * virNetworkObjGetOneDef: + * + * @net: Network object + * @flags: for virNetworkUpdateFlags + * + * Helper function to resolve @flags and return the correct network pointer + * object. This function returns one of @net->def or @net->persistentDef + * according to @flags. This helper should be used only in APIs that guara= ntee + * that @flags contains exactly one of VIR_NETWORK_UPDATE_AFFECT_LIVE or + * VIR_NETWORK_UPDATE_AFFECT_CONFIG and not both. + * + * Returns the correct definition pointer or NULL on error. + */ +static virNetworkDef * +virNetworkObjGetOneDef(virNetworkObj *net, + unsigned int flags) +{ + return virNetworkObjGetOneDefState(net, flags, NULL); +} + + +char * +virNetworkObjGetMetadata(virNetworkObj *net, + int type, + const char *uri, + unsigned int flags) +{ + virNetworkDef *def; + char *ret =3D NULL; + + virCheckFlags(VIR_NETWORK_UPDATE_AFFECT_LIVE | + VIR_NETWORK_UPDATE_AFFECT_CONFIG, NULL); + + if (type >=3D VIR_NETWORK_METADATA_LAST) { + virReportError(VIR_ERR_INVALID_ARG, + _("unknown metadata type '%1$d'"), type); + return NULL; + } + + if (!(def =3D virNetworkObjGetOneDef(net, flags))) + return NULL; + + switch ((virNetworkMetadataType) type) { + case VIR_NETWORK_METADATA_DESCRIPTION: + ret =3D g_strdup(def->description); + break; + + case VIR_NETWORK_METADATA_TITLE: + ret =3D g_strdup(def->title); + break; + + case VIR_NETWORK_METADATA_ELEMENT: + if (!def->metadata) + break; + + if (virXMLExtractNamespaceXML(def->metadata, uri, &ret) < 0) + return NULL; + break; + + case VIR_NETWORK_METADATA_LAST: + break; + } + + if (!ret) + virReportError(VIR_ERR_NO_NETWORK_METADATA, "%s", + _("Requested metadata element is not present")); + + return ret; +} + + +static int +virNetworkDefSetMetadata(virNetworkDef *def, + int type, + const char *metadata, + const char *key, + const char *uri) +{ + g_autoptr(xmlDoc) doc =3D NULL; + xmlNodePtr old; + g_autoptr(xmlNode) new =3D NULL; + + if (type >=3D VIR_NETWORK_METADATA_LAST) { + virReportError(VIR_ERR_INVALID_ARG, + _("unknown metadata type '%1$d'"), type); + return -1; + } + + switch ((virNetworkMetadataType) type) { + case VIR_NETWORK_METADATA_DESCRIPTION: + g_clear_pointer(&def->description, g_free); + + if (STRNEQ_NULLABLE(metadata, "")) + def->description =3D g_strdup(metadata); + break; + + case VIR_NETWORK_METADATA_TITLE: + g_clear_pointer(&def->title, g_free); + + if (STRNEQ_NULLABLE(metadata, "")) + def->title =3D g_strdup(metadata); + break; + + case VIR_NETWORK_METADATA_ELEMENT: + if (metadata) { + + /* parse and modify the xml from the user */ + if (!(doc =3D virXMLParseStringCtxt(metadata, _("(metadata_xml= )"), NULL))) + return -1; + + if (virXMLInjectNamespace(doc->children, uri, key) < 0) + return -1; + + /* create the root node if needed */ + if (!def->metadata) + def->metadata =3D virXMLNewNode(NULL, "metadata"); + + if (!(new =3D xmlCopyNode(doc->children, 1))) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Failed to copy XML node")); + return -1; + } + } + + /* remove possible other nodes sharing the namespace */ + while ((old =3D virXMLFindChildNodeByNs(def->metadata, uri))) { + xmlUnlinkNode(old); + xmlFreeNode(old); + } + + if (new) { + if (!(xmlAddChild(def->metadata, new))) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("failed to add metadata to XML document")= ); + return -1; + } + new =3D NULL; + } + break; + + case VIR_NETWORK_METADATA_LAST: + break; + } + + return 0; +} + + +int +virNetworkObjSetMetadata(virNetworkObj *net, + int type, + const char *metadata, + const char *key, + const char *uri, + virNetworkXMLOption *xmlopt, + const char *stateDir, + const char *configDir, + unsigned int flags) +{ + virNetworkDef *def; + virNetworkDef *persistentDef; + + virCheckFlags(VIR_NETWORK_UPDATE_AFFECT_LIVE | + VIR_NETWORK_UPDATE_AFFECT_CONFIG, -1); + + if (virNetworkObjGetDefs(net, flags, &def, &persistentDef) < 0) + return -1; + + if (def) { + if (virNetworkDefSetMetadata(def, type, metadata, key, uri) < 0) + return -1; + + if (virNetworkObjSaveStatus(stateDir, net, xmlopt) < 0) + return -1; + } + + if (persistentDef) { + if (virNetworkDefSetMetadata(persistentDef, type, metadata, key, + uri) < 0) + return -1; + + if (virNetworkSaveConfig(configDir, persistentDef, xmlopt) < 0) + return -1; + } + + return 0; +} diff --git a/src/conf/virnetworkobj.h b/src/conf/virnetworkobj.h index 7d34fa3204..d3847d3422 100644 --- a/src/conf/virnetworkobj.h +++ b/src/conf/virnetworkobj.h @@ -258,3 +258,24 @@ virNetworkObjListNumOfNetworks(virNetworkObjList *nets, void virNetworkObjListPrune(virNetworkObjList *nets, unsigned int flags); + +int +virNetworkObjUpdateModificationImpact(virNetworkObj *obj, + unsigned int *flags); + +char * +virNetworkObjGetMetadata(virNetworkObj *network, + int type, + const char *uri, + unsigned int flags); + +int +virNetworkObjSetMetadata(virNetworkObj *network, + int type, + const char *metadata, + const char *key, + const char *uri, + virNetworkXMLOption *xmlopt, + const char *stateDir, + const char *configDir, + unsigned int flags); diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index da60c965dd..015384c0ec 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1291,6 +1291,7 @@ virNetworkObjGetDef; virNetworkObjGetDnsmasqPid; virNetworkObjGetFloorSum; virNetworkObjGetMacMap; +virNetworkObjGetMetadata; virNetworkObjGetNewDef; virNetworkObjGetPersistentDef; virNetworkObjGetPortStatusDir; @@ -1321,11 +1322,13 @@ virNetworkObjSetDefTransient; virNetworkObjSetDnsmasqPid; virNetworkObjSetFloorSum; virNetworkObjSetMacMap; +virNetworkObjSetMetadata; virNetworkObjTaint; virNetworkObjUnrefMacMap; virNetworkObjUnsetDefTransient; virNetworkObjUpdate; virNetworkObjUpdateAssignDef; +virNetworkObjUpdateModificationImpact; =20 =20 # conf/virnetworkportdef.h diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c index 9eb543a0a3..e776d86c73 100644 --- a/src/network/bridge_driver.c +++ b/src/network/bridge_driver.c @@ -3210,18 +3210,10 @@ networkUpdate(virNetworkPtr net, } } =20 - /* VIR_NETWORK_UPDATE_AFFECT_CURRENT means "change LIVE if network - * is active, else change CONFIG - */ + if (virNetworkObjUpdateModificationImpact(obj, &flags) < 0) + goto cleanup; + isActive =3D virNetworkObjIsActive(obj); - if ((flags & (VIR_NETWORK_UPDATE_AFFECT_LIVE | - VIR_NETWORK_UPDATE_AFFECT_CONFIG)) =3D=3D - VIR_NETWORK_UPDATE_AFFECT_CURRENT) { - if (isActive) - flags |=3D VIR_NETWORK_UPDATE_AFFECT_LIVE; - else - flags |=3D VIR_NETWORK_UPDATE_AFFECT_CONFIG; - } =20 if (isActive && (flags & VIR_NETWORK_UPDATE_AFFECT_LIVE)) { /* Take care of anything that must be done before updating the diff --git a/src/test/test_driver.c b/src/test/test_driver.c index 4b8e02c684..ff9d0fabaa 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -5658,7 +5658,7 @@ testNetworkUpdate(virNetworkPtr net, { testDriver *privconn =3D net->conn->privateData; virNetworkObj *obj =3D NULL; - int isActive, ret =3D -1; + int ret =3D -1; =20 virCheckFlags(VIR_NETWORK_UPDATE_AFFECT_LIVE | VIR_NETWORK_UPDATE_AFFECT_CONFIG, @@ -5667,18 +5667,8 @@ testNetworkUpdate(virNetworkPtr net, if (!(obj =3D testNetworkObjFindByUUID(privconn, net->uuid))) goto cleanup; =20 - /* VIR_NETWORK_UPDATE_AFFECT_CURRENT means "change LIVE if network - * is active, else change CONFIG - */ - isActive =3D virNetworkObjIsActive(obj); - if ((flags & (VIR_NETWORK_UPDATE_AFFECT_LIVE - | VIR_NETWORK_UPDATE_AFFECT_CONFIG)) =3D=3D - VIR_NETWORK_UPDATE_AFFECT_CURRENT) { - if (isActive) - flags |=3D VIR_NETWORK_UPDATE_AFFECT_LIVE; - else - flags |=3D VIR_NETWORK_UPDATE_AFFECT_CONFIG; - } + if (virNetworkObjUpdateModificationImpact(obj, &flags) < 0) + goto cleanup; =20 /* update the network config in memory/on disk */ if (virNetworkObjUpdate(obj, command, section, --=20 2.41.0 From nobody Sat May 18 07:48:38 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=fail(p=none dis=none) header.from=riseup.net ARC-Seal: i=1; a=rsa-sha256; t=1692211692; cv=none; d=zohomail.com; s=zohoarc; b=AzXjQ99AUGC0sTt6Xps4GXJZ+ogVJQu2Ur0LT4RRPvpoWPbRCSCeHuMttKyOrnlbkVR1NhGF81NPAaIdjAVQkuY1sdttyLH/Ii7uk89m1YtcWhH97cBIiEd91fTIJ86Icw73fbVYiC7LnPr56RHwn2YkWPmmWhg+oR4o+kKhXKM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692211692; h=Content-Type:Content-Transfer-Encoding:Cc: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=Uar0xXfD2b5Hq0+CexiPqUuG377xOHOosdU9BoSeHZQ=; b=R2Y26WTlL7lYIDy8p0zAOPv1dRl/SbhqmC8Nk8uDy41jqXqM0XCYKGpPRuvOUji+1aZAx2yNOQrJsFsY7IW+lxN6YVehRhQWlh5XI9navgDmEuGbQdNcPJydEHdyiGjAeyIekf+9rtkjGBFqZVoUIQDus/PNoQwmaq8/VxFNi8k= 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=fail header.from=<shiva_kr@riseup.net> (p=none dis=none) Return-Path: <libvir-list-bounces@redhat.com> 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 16922116923194.846084454142556; Wed, 16 Aug 2023 11:48:12 -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-204-Ql-dFrvUOBGxS4XVf3AZfA-1; Wed, 16 Aug 2023 14:48:07 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3BCFE85CBF1; Wed, 16 Aug 2023 18:48:04 +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 229EA492C14; Wed, 16 Aug 2023 18:48:04 +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 463CE1946589; Wed, 16 Aug 2023 18:47:56 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id C324A1946A42 for <libvir-list@listman.corp.redhat.com>; Wed, 16 Aug 2023 18:47:45 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id A68F3140E963; Wed, 16 Aug 2023 18:47:45 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast03.extmail.prod.ext.rdu2.redhat.com [10.11.55.19]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9CF6A140E962 for <libvir-list@redhat.com>; Wed, 16 Aug 2023 18:47:45 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-inbound-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 7AB5C8DC665 for <libvir-list@redhat.com>; Wed, 16 Aug 2023 18:47:45 +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-375-cfJi7JRePYaklT4ejdQNsg-1; Wed, 16 Aug 2023 14:47:43 -0400 Received: from fews01-sea.riseup.net (fews01-sea-pn.riseup.net [10.0.1.109]) (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 4RQxvx6mHjzDqfn for <libvir-list@redhat.com>; Wed, 16 Aug 2023 18:47:41 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by fews01-sea.riseup.net (Postfix) with ESMTPSA id 4RQxvw5nmnzJnDN; Wed, 16 Aug 2023 18:47:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692211691; 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=Uar0xXfD2b5Hq0+CexiPqUuG377xOHOosdU9BoSeHZQ=; b=JNK7mJaaicJQiR0My7o1gS1WlldP8V62Ls3zsHSPEaayqoQ8GDKqrHZtcNP4hSu/givTF4 p18g2+vUEYTwLWnHwTArk9Gc3g4vEx73JHcZ0HFfxHiAteXcVDG8FUIHsDAXXDhZ+x6wZP e/AhT5HAwtKWINm/L+B/Gu+s33zxOPk= X-MC-Unique: Ql-dFrvUOBGxS4XVf3AZfA-1 X-Original-To: libvir-list@listman.corp.redhat.com X-MC-Unique: cfJi7JRePYaklT4ejdQNsg-1 X-Riseup-User-ID: 0B4214D6E6E44FCABFB514778BAE97B4EDDD5D0060F508BE623113CE858CB2EE From: K Shiva Kiran <shiva_kr@riseup.net> To: libvir-list@redhat.com Subject: [libvirt PATCH v2 6/7] Add Test driver and testcase for Network Metadata change APIs Date: Thu, 17 Aug 2023 00:17:14 +0530 Message-ID: <20230816184715.101243-7-shiva_kr@riseup.net> In-Reply-To: <20230816184715.101243-1-shiva_kr@riseup.net> References: <20230816184715.101243-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.1 on 10.11.54.7 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools <libvir-list.redhat.com> List-Unsubscribe: <https://listman.redhat.com/mailman/options/libvir-list>, <mailto:libvir-list-request@redhat.com?subject=unsubscribe> List-Archive: <http://listman.redhat.com/archives/libvir-list/> List-Post: <mailto:libvir-list@redhat.com> List-Help: <mailto:libvir-list-request@redhat.com?subject=help> List-Subscribe: <https://listman.redhat.com/mailman/listinfo/libvir-list>, <mailto:libvir-list-request@redhat.com?subject=subscribe> Cc: K Shiva Kiran <shiva_kr@riseup.net> Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" <libvir-list-bounces@redhat.com> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 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: 1692211693570100001 Content-Type: text/plain; charset="utf-8"; x-default="true" This commit implements the newly defined Network Metadata Get and Set APIs into the test driver. It also adds a new testcase "networkmetadatatest" to test the APIs. Signed-off-by: K Shiva Kiran <shiva_kr@riseup.net> Reviewed-by: Michal Privoznik <mprivozn@redhat.com> --- src/conf/network_conf.c | 3 + src/test/test_driver.c | 67 ++++++++ tests/meson.build | 1 + tests/networkmetadatatest.c | 297 ++++++++++++++++++++++++++++++++++++ 4 files changed, 368 insertions(+) create mode 100644 tests/networkmetadatatest.c diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c index 262e9a9fc7..e4c8c5fd4d 100644 --- a/src/conf/network_conf.c +++ b/src/conf/network_conf.c @@ -2564,6 +2564,9 @@ virNetworkSaveXML(const char *configDir, char uuidstr[VIR_UUID_STRING_BUFLEN]; g_autofree char *configFile =3D NULL; =20 + if (!configDir) + return 0; + if ((configFile =3D virNetworkConfigFile(configDir, def->name)) =3D=3D= NULL) return -1; =20 diff --git a/src/test/test_driver.c b/src/test/test_driver.c index ff9d0fabaa..d1a5eaff7e 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -633,6 +633,25 @@ static int testStoragePoolObjSetDefaults(virStoragePoo= lObj *obj); static int testNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info); static virNetworkObj *testNetworkObjFindByName(testDriver *privconn, const= char *name); =20 +static virNetworkObj * +testNetworkObjFromNetwork(virNetworkPtr network) +{ + virNetworkObj *net; + testDriver *driver =3D network->conn->privateData; + char uuidstr[VIR_UUID_STRING_BUFLEN]; + + net =3D virNetworkObjFindByUUID(driver->networks, network->uuid); + if (!net) { + virUUIDFormat(network->uuid, uuidstr); + virReportError(VIR_ERR_NO_NETWORK, + _("no network with matching uuid '%1$s' (%2$s)"), + uuidstr, network->name); + } + + return net; +} + + static virDomainObj * testDomObjFromDomain(virDomainPtr domain) { @@ -9941,6 +9960,52 @@ testConnectGetAllDomainStats(virConnectPtr conn, return ret; } =20 +static char * +testNetworkGetMetadata(virNetworkPtr net, + int type, + const char *uri, + unsigned int flags) +{ + virNetworkObj *privnet; + char *ret; + + virCheckFlags(VIR_NETWORK_UPDATE_AFFECT_LIVE | + VIR_NETWORK_UPDATE_AFFECT_CONFIG, NULL); + + if (!(privnet =3D testNetworkObjFromNetwork(net))) + return NULL; + + ret =3D virNetworkObjGetMetadata(privnet, type, uri, flags); + + virNetworkObjEndAPI(&privnet); + return ret; +} + +static int +testNetworkSetMetadata(virNetworkPtr net, + int type, + const char *metadata, + const char *key, + const char *uri, + unsigned int flags) +{ + virNetworkObj *privnet; + int ret; + + virCheckFlags(VIR_NETWORK_UPDATE_AFFECT_LIVE | + VIR_NETWORK_UPDATE_AFFECT_CONFIG, -1); + + if (!(privnet =3D testNetworkObjFromNetwork(net))) + return -1; + + ret =3D virNetworkObjSetMetadata(privnet, type, metadata, + key, uri, NULL, + NULL, NULL, flags); + + virNetworkObjEndAPI(&privnet); + return ret; +} + /* * Test driver */ @@ -10134,6 +10199,8 @@ static virNetworkDriver testNetworkDriver =3D { .networkSetAutostart =3D testNetworkSetAutostart, /* 0.3.2 */ .networkIsActive =3D testNetworkIsActive, /* 0.7.3 */ .networkIsPersistent =3D testNetworkIsPersistent, /* 0.7.3 */ + .networkSetMetadata =3D testNetworkSetMetadata, /* 9.7.0 */ + .networkGetMetadata =3D testNetworkGetMetadata, /* 9.7.0 */ }; =20 static virInterfaceDriver testInterfaceDriver =3D { diff --git a/tests/meson.build b/tests/meson.build index e6589ec555..e76289da62 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -258,6 +258,7 @@ tests +=3D [ { 'name': 'genericxml2xmltest' }, { 'name': 'interfacexml2xmltest' }, { 'name': 'metadatatest' }, + { 'name': 'networkmetadatatest' }, { 'name': 'networkxml2xmlupdatetest' }, { 'name': 'nodedevxml2xmltest' }, { 'name': 'nwfilterxml2xmltest' }, diff --git a/tests/networkmetadatatest.c b/tests/networkmetadatatest.c new file mode 100644 index 0000000000..c309fb29b0 --- /dev/null +++ b/tests/networkmetadatatest.c @@ -0,0 +1,297 @@ +/* + * Copyright (C) 2013 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; If not, see + * <http://www.gnu.org/licenses/>. + */ + +#include <config.h> + +#include "testutils.h" + +#include "virerror.h" +#include "virxml.h" + +#define VIR_FROM_THIS VIR_FROM_NONE + +static const char metadata1[] =3D +"<derp xmlns:foobar=3D'http://foo.bar/'>\n" +" <bar>foobar</bar>\n" +" <foo fooish=3D'blurb'>foofoo</foo>\n" +" <foobar:baz>zomg</foobar:baz>\n" +"</derp>"; + + +static const char metadata1_ns[] =3D +"<herp:derp xmlns:foobar=3D'http://foo.bar/' xmlns:herp=3D'http://herp.der= p/'>\n" +" <herp:bar>foobar</herp:bar>\n" +" <herp:foo fooish=3D'blurb'>foofoo</herp:foo>\n" +" <foobar:baz>zomg</foobar:baz>\n" +"</herp:derp>"; + + +static const char metadata2[] =3D +"<foo>\n" +" <bar>baz</bar>\n" +"</foo>"; + + +static const char metadata2_ns[] =3D +"<blurb:foo xmlns:blurb=3D'http://herp.derp/'>\n" +" <blurb:bar>baz</blurb:bar>\n" +"</blurb:foo>"; + + +static char * +getMetadataFromXML(virNetworkPtr net) +{ + g_autoptr(xmlDoc) doc =3D NULL; + g_autoptr(xmlXPathContext) ctxt =3D NULL; + xmlNodePtr node; + + g_autofree char *xml =3D NULL; + + if (!(xml =3D virNetworkGetXMLDesc(net, 0))) + return NULL; + + if (!(doc =3D virXMLParseStringCtxt(xml, "(network_definition)", &ctxt= ))) + return NULL; + + if (!(node =3D virXPathNode("//metadata/*", ctxt))) + return NULL; + + return virXMLNodeToString(node->doc, node); +} + + +static void +metadataXMLConvertApostrophe(char *str) +{ + do { + if (*str =3D=3D '\"') + *str =3D '\''; + } while ((*++str) !=3D '\0'); +} + + +static bool +verifyMetadata(virNetworkPtr net, + const char *expectXML, + const char *expectAPI, + const char *uri) +{ + g_autofree char *metadataXML =3D NULL; + g_autofree char *metadataAPI =3D NULL; + + if (!expectAPI) { + if ((metadataAPI =3D virNetworkGetMetadata(net, + VIR_NETWORK_METADATA_ELEM= ENT, + uri, 0))) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "expected no metadata in API, but got:\n[%s]", + metadataAPI); + return false; + } + } else { + if (!(metadataAPI =3D virNetworkGetMetadata(net, + VIR_NETWORK_METADATA_ELE= MENT, + uri, 0))) + return false; + + metadataXMLConvertApostrophe(metadataAPI); + + if (STRNEQ(metadataAPI, expectAPI)) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "XML metadata in API doesn't match expected met= adata: " + "expected:\n[%s]\ngot:\n[%s]", + expectAPI, metadataAPI); + return false; + } + + } + + if (!expectXML) { + if ((metadataXML =3D getMetadataFromXML(net))) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "expected no metadata in XML, but got:\n[%s]", + metadataXML); + return false; + } + } else { + if (!(metadataXML =3D getMetadataFromXML(net))) + return false; + + metadataXMLConvertApostrophe(metadataXML); + + if (STRNEQ(metadataXML, expectXML)) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "XML in dump doesn't match expected metadata: " + "expected:\n[%s]\ngot:\n[%s]", + expectXML, metadataXML); + return false; + } + } + + return true; +} + + +struct metadataTest { + virConnectPtr conn; + virNetworkPtr net; + + const char *data; + const char *expect; + int type; + bool fail; +}; + + +static int +testAssignMetadata(const void *data) +{ + const struct metadataTest *test =3D data; + + if (virNetworkSetMetadata(test->net, VIR_NETWORK_METADATA_ELEMENT, + metadata1, "herp", "http://herp.derp/", 0) <= 0) + return -1; + + if (!verifyMetadata(test->net, metadata1_ns, metadata1, "http://herp.d= erp/")) + return -1; + + return 0; +} + +static int +testRewriteMetadata(const void *data) +{ + const struct metadataTest *test =3D data; + + if (virNetworkSetMetadata(test->net, VIR_NETWORK_METADATA_ELEMENT, + metadata2, "blurb", "http://herp.derp/", 0) = < 0) + return -1; + + if (!verifyMetadata(test->net, metadata2_ns, metadata2, "http://herp.d= erp/")) + return -1; + + return 0; +} + +static int +testEraseMetadata(const void *data) +{ + const struct metadataTest *test =3D data; + + if (virNetworkSetMetadata(test->net, VIR_NETWORK_METADATA_ELEMENT, + NULL, NULL, "http://herp.derp/", 0) < 0) + return -1; + + if (!verifyMetadata(test->net, NULL, NULL, "http://herp.derp/")) + return -1; + + return 0; +} + +static int +testTextMetadata(const void *data) +{ + const struct metadataTest *test =3D data; + g_autofree char *actual =3D NULL; + + if (virNetworkSetMetadata(test->net, test->type, test->data, NULL, NUL= L, 0) < 0) { + if (test->fail) + return 0; + return -1; + } + + actual =3D virNetworkGetMetadata(test->net, test->type, NULL, 0); + + if (STRNEQ_NULLABLE(test->expect, actual)) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "expected metadata doesn't match actual: " + "expected:'%s'\ngot: '%s'", + NULLSTR(test->data), NULLSTR(actual)); + return -1; + } + + return 0; +} + +#define TEST_TEXT_METADATA(INDEX, TYPE, DATA, EXPECT, FAIL) \ + do { \ + test.type =3D VIR_NETWORK_METADATA_ ## TYPE; \ + test.data =3D DATA; \ + test.expect =3D EXPECT; \ + test.fail =3D FAIL; \ + \ + if (virTestRun("text metadata: " #TYPE " " INDEX " ", \ + testTextMetadata, &test) < 0) \ + ret =3D EXIT_FAILURE; \ + } while (0) + +#define TEST_TITLE(INDEX, DATA) \ + TEST_TEXT_METADATA(INDEX, TITLE, DATA, DATA, false) +#define TEST_TITLE_EXPECT(INDEX, DATA, EXPECT) \ + TEST_TEXT_METADATA(INDEX, TITLE, DATA, EXPECT, false) +#define TEST_TITLE_FAIL(INDEX, DATA) \ + TEST_TEXT_METADATA(INDEX, TITLE, DATA, DATA, true) +#define TEST_DESCR(INDEX, DATA) \ + TEST_TEXT_METADATA(INDEX, DESCRIPTION, DATA, DATA, false) +#define TEST_DESCR_EXPECT(INDEX, DATA, EXPECT) \ + TEST_TEXT_METADATA(INDEX, DESCRIPTION, DATA, EXPECT, false) + +static int +mymain(void) +{ + struct metadataTest test =3D { 0 }; + int ret =3D EXIT_SUCCESS; + + if (!(test.conn =3D virConnectOpen("test:///default"))) + return EXIT_FAILURE; + + if (!(test.net =3D virNetworkLookupByName(test.conn, "default"))) { + virConnectClose(test.conn); + return EXIT_FAILURE; + } + + virTestQuiesceLibvirtErrors(false); + + if (virTestRun("Assign metadata ", testAssignMetadata, &test) < 0) + ret =3D EXIT_FAILURE; + if (virTestRun("Rewrite Metadata ", testRewriteMetadata, &test) < 0) + ret =3D EXIT_FAILURE; + if (virTestRun("Erase metadata ", testEraseMetadata, &test) < 0) + ret =3D EXIT_FAILURE; + + TEST_TITLE("1", "qwert"); + TEST_TITLE("2", NULL); + TEST_TITLE("3", "blah"); + TEST_TITLE_FAIL("4", "qwe\nrt"); + TEST_TITLE_EXPECT("5", "", NULL); + TEST_TITLE_FAIL("6", "qwert\n"); + TEST_TITLE_FAIL("7", "\n"); + + TEST_DESCR("1", "qwert\nqwert"); + TEST_DESCR("2", NULL); + TEST_DESCR("3", "qwert"); + TEST_DESCR("4", "\n"); + TEST_DESCR_EXPECT("5", "", NULL); + + virNetworkFree(test.net); + virConnectClose(test.conn); + + return ret; +} + +VIR_TEST_MAIN(mymain) --=20 2.41.0 From nobody Sat May 18 07:48:38 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=fail(p=none dis=none) header.from=riseup.net ARC-Seal: i=1; a=rsa-sha256; t=1692211697; cv=none; d=zohomail.com; s=zohoarc; b=QSRR2xqEOcedRpClEzQyax/kvKLAGIDXVZSPbC/shNeTrAqanVJjmU5OmW+9oOd4RbOgaqVjmuDBB3rvtmV39GYRCZ/cc4cbqz5XYZvag3Z4jqgjTouHZ30noQWKGfCsxA4hJaD7bmrPV+TYWDcuySEfEDkilWFfnjiLw6wQdDE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692211697; h=Content-Type:Content-Transfer-Encoding:Cc: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=p1zr9TvyKCjUl1CerhG9bDuV+RzOMCgOwUqTwrrw38A=; b=a2tICgIl+gND8F7p7AG3leDWzpZp0BSyh7yI3N14ICxd2Onh0xjjEH2wH8UNyXgB8KOOUoDEeonl3selHva3ch5GpcpSuADFid94BclLuEufv1qCgKr7dsKzMdltrnguDEX/CnA+e+hz4TSwILrgH2bYUWkChIh4oUqIA9YPxbA= 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=<shiva_kr@riseup.net> (p=none dis=none) Return-Path: <libvir-list-bounces@redhat.com> 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 1692211697160901.1425329423987; Wed, 16 Aug 2023 11:48:17 -0700 (PDT) Received: from mimecast-mx02.redhat.com (66.187.233.73 [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-209-HzSoV9wJPZCIDyZ60Ef63A-1; Wed, 16 Aug 2023 14:48:09 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 62AC31C07829; Wed, 16 Aug 2023 18:48: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 4BA382026D4B; Wed, 16 Aug 2023 18:48:07 +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 9DE631946594; Wed, 16 Aug 2023 18:48:01 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id C1B931946A42 for <libvir-list@listman.corp.redhat.com>; Wed, 16 Aug 2023 18:47:46 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id B1DAE1121315; Wed, 16 Aug 2023 18:47:46 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast10.extmail.prod.ext.rdu2.redhat.com [10.11.55.26]) by smtp.corp.redhat.com (Postfix) with ESMTPS id AA4161121314 for <libvir-list@redhat.com>; Wed, 16 Aug 2023 18:47:46 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-1.mimecast.com [207.211.31.81]) (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 8CBBE1C0782D for <libvir-list@redhat.com>; Wed, 16 Aug 2023 18:47:46 +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-397-UhRlrgWaO1KDJVXI4N4PCA-1; Wed, 16 Aug 2023 14:47:44 -0400 Received: from fews01-sea.riseup.net (fews01-sea-pn.riseup.net [10.0.1.109]) (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 4RQxvz42lxzDqfk for <libvir-list@redhat.com>; Wed, 16 Aug 2023 18:47:43 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by fews01-sea.riseup.net (Postfix) with ESMTPSA id 4RQxvy2ydLzJnDN; Wed, 16 Aug 2023 18:47:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692211696; 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=p1zr9TvyKCjUl1CerhG9bDuV+RzOMCgOwUqTwrrw38A=; b=a7i/H5QpTBZwOhupeqybAsKRtAL/jOWO7qW9bq7z0FvINs8qlrUEXwNvGBKecGOF5V96O3 M8HsdA8qKPRr0acCY/toy3UovRI3cJ4sSBdlYpPFO4CJz57IGEA7mqLq+x194sDfqLGiAY uoQJqUKqc2lGSO7d7N0cRY6R2H4diE8= X-MC-Unique: HzSoV9wJPZCIDyZ60Ef63A-1 X-Original-To: libvir-list@listman.corp.redhat.com X-MC-Unique: UhRlrgWaO1KDJVXI4N4PCA-1 X-Riseup-User-ID: 1CBD65D8B6F4788FBBCBC5EA757DB5B4D8B62DE5377F97B97E0D596957D5B1F0 From: K Shiva Kiran <shiva_kr@riseup.net> To: libvir-list@redhat.com Subject: [libvirt PATCH v2 7/7] Added bridge driver implementation Date: Thu, 17 Aug 2023 00:17:15 +0530 Message-ID: <20230816184715.101243-8-shiva_kr@riseup.net> In-Reply-To: <20230816184715.101243-1-shiva_kr@riseup.net> References: <20230816184715.101243-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.1 on 10.11.54.3 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools <libvir-list.redhat.com> List-Unsubscribe: <https://listman.redhat.com/mailman/options/libvir-list>, <mailto:libvir-list-request@redhat.com?subject=unsubscribe> List-Archive: <http://listman.redhat.com/archives/libvir-list/> List-Post: <mailto:libvir-list@redhat.com> List-Help: <mailto:libvir-list-request@redhat.com?subject=help> List-Subscribe: <https://listman.redhat.com/mailman/listinfo/libvir-list>, <mailto:libvir-list-request@redhat.com?subject=subscribe> Cc: K Shiva Kiran <shiva_kr@riseup.net> Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" <libvir-list-bounces@redhat.com> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 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: 1692211697565100007 Content-Type: text/plain; charset="utf-8"; x-default="true" Signed-off-by: K Shiva Kiran <shiva_kr@riseup.net> Reviewed-by: Michal Privoznik <mprivozn@redhat.com> --- src/network/bridge_driver.c | 64 +++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c index e776d86c73..caad085192 100644 --- a/src/network/bridge_driver.c +++ b/src/network/bridge_driver.c @@ -5119,6 +5119,68 @@ networkListAllPorts(virNetworkPtr net, } =20 =20 +static int +networkSetMetadata(virNetworkPtr net, + int type, + const char *metadata, + const char *key, + const char *uri, + unsigned int flags) +{ + virNetworkDriverState *driver =3D networkGetDriver(); + virNetworkObj *obj =3D NULL; + virNetworkDef *def =3D NULL; + g_autoptr(virNetworkDriverConfig) cfg =3D NULL; + int ret =3D -1; + + virCheckFlags(VIR_NETWORK_UPDATE_AFFECT_LIVE | + VIR_NETWORK_UPDATE_AFFECT_CONFIG, -1); + + if (!(obj =3D networkObjFromNetwork(net))) + return -1; + + cfg =3D virNetworkDriverGetConfig(driver); + def =3D virNetworkObjGetDef(obj); + + if (virNetworkSetMetadataEnsureACL(net->conn, def, flags) < 0) + goto cleanup; + + ret =3D virNetworkObjSetMetadata(obj, type, metadata, key, uri, + driver->xmlopt, cfg->stateDir, + cfg->networkConfigDir, flags); + + cleanup: + virNetworkObjEndAPI(&obj); + return ret; +} + + +static char * +networkGetMetadata(virNetworkPtr net, + int type, + const char *uri, + unsigned int flags) +{ + virNetworkObj *obj =3D NULL; + virNetworkDef *def =3D NULL; + char *ret =3D NULL; + + if (!(obj =3D networkObjFromNetwork(net))) + return NULL; + + def =3D virNetworkObjGetDef(obj); + + if (virNetworkGetMetadataEnsureACL(net->conn, def) < 0) + goto cleanup; + + ret =3D virNetworkObjGetMetadata(obj, type, uri, flags); + + cleanup: + virNetworkObjEndAPI(&obj); + return ret; +} + + static virNetworkDriver networkDriver =3D { .name =3D "bridge", .connectNumOfNetworks =3D networkConnectNumOfNetworks, /* 0.2.0 */ @@ -5152,6 +5214,8 @@ static virNetworkDriver networkDriver =3D { .networkListAllPorts =3D networkListAllPorts, /* 5.5.0 */ .networkPortGetParameters =3D networkPortGetParameters, /* 5.5.0 */ .networkPortSetParameters =3D networkPortSetParameters, /* 5.5.0 */ + .networkGetMetadata =3D networkGetMetadata, /* 9.7.0 */ + .networkSetMetadata =3D networkSetMetadata, /* 9.7.0 */ }; =20 =20 --=20 2.41.0