From nobody Mon Apr 29 13:35:28 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1630301829121658.473839246217; Sun, 29 Aug 2021 22:37:09 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-537-94yUTclyNXKhrVmf-lTnVg-1; Mon, 30 Aug 2021 01:37:06 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E7D7B1008066; Mon, 30 Aug 2021 05:37:00 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 868E117A98; Mon, 30 Aug 2021 05:36:58 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 9100F181A0F2; Mon, 30 Aug 2021 05:36:53 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 17U5a2e7019822 for ; Mon, 30 Aug 2021 01:36:02 -0400 Received: by smtp.corp.redhat.com (Postfix) id 0576B1111C74; Mon, 30 Aug 2021 05:36:02 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast04.extmail.prod.ext.rdu2.redhat.com [10.11.55.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id F3D891111C6A for ; Mon, 30 Aug 2021 05:35:59 +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 4AF9E100B8C0 for ; Mon, 30 Aug 2021 05:35:59 +0000 (UTC) Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-213-QvW_SVJCMfqbMMguYzSVjA-1; Mon, 30 Aug 2021 01:35:54 -0400 Received: by mail-pg1-f181.google.com with SMTP id n18so12311006pgm.12 for ; Sun, 29 Aug 2021 22:35:54 -0700 (PDT) Received: from localhost-live.zelin.local ([103.97.201.4]) by smtp.gmail.com with ESMTPSA id x8sm3060874pfj.128.2021.08.29.22.35.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Aug 2021 22:35:52 -0700 (PDT) X-MC-Unique: 94yUTclyNXKhrVmf-lTnVg-1 X-MC-Unique: QvW_SVJCMfqbMMguYzSVjA-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=suGrh2N7nXktkeM0VcQkHezrW77hgBAyYB4apb6iPCc=; b=Dk87Ta71gPM+W59h36ln2aUicZWpRlTcZkkyk/B/AaUsfK0WwzBJsYL6Ilh3W20A6j IAzMKVg0iKs7GUdN58b5gb2cso4PnXJqhRuhVbK6bZ6A8BazHVxqP/4vp7CuIW2FWBSB bw1HnhvgZt87tN6H4/NyoGM5ZdmlN8SKV/D40TVaHzny8vr3T/MfZgxmp8d8epLx8fsS mXTUSl537ATdy9NwomvRHa8VDjjasbXWRmzBRcWKU8i9dzDwEOcYpWlKA1gnwPUXp4cF hhaW7fYi019xUkjjVbLv1N06OZJ9Ru6NGYfUH52kB4IYaUuL+nVLl03TugEOATRyxOpn pcFg== X-Gm-Message-State: AOAM5314JTUBdpBRlQ3GadsNhz/BwMlUnEdmYOXFDj+NgXUI+WOaOJ4U oZbvK5wxEV7vl/829LhIDCg6GLYYzK4= X-Google-Smtp-Source: ABdhPJxvWglO+8WyuX3Ua/eQVb75uFx04ESndRVvHM2MhkXaQq0IXcwncfBVagsy16bapIsaH2oQ/A== X-Received: by 2002:a63:fc5b:: with SMTP id r27mr20129997pgk.22.1630301752668; Sun, 29 Aug 2021 22:35:52 -0700 (PDT) From: scuzhanglei To: libvirt-list@redhat.com Subject: [PATCH] qemu_driver:report guest interface informations Date: Mon, 30 Aug 2021 01:35:38 -0400 Message-Id: <20210830053538.452371-1-greatzhanglei@gmail.com> In-Reply-To: <20210827034233.407481-1-greatzhanglei@gmail.com> References: <20210827034233.407481-1-greatzhanglei@gmail.com> 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 2.78 on 10.11.54.3 X-loop: libvir-list@redhat.com Cc: scuzhanglei X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1630301831226100001 Content-Type: text/plain; charset="utf-8" Signed-off-by: scuzhanglei --- NEWS.rst | 5 ++ docs/manpages/virsh.rst | 12 ++++- include/libvirt/libvirt-domain.h | 1 + src/libvirt-domain.c | 12 +++++ src/qemu/qemu_agent.c | 9 ++-- src/qemu/qemu_agent.h | 3 +- src/qemu/qemu_driver.c | 88 +++++++++++++++++++++++++++++++- tests/qemuagenttest.c | 2 +- tools/virsh-domain.c | 6 +++ 9 files changed, 129 insertions(+), 9 deletions(-) diff --git a/NEWS.rst b/NEWS.rst index 1af934e068..8666b318d8 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -45,6 +45,11 @@ v7.7.0 (unreleased) The genid attribute is now reported for VMX guests. Libvirt can now properly process super wide SCSI bus (64 units). =20 + * qemu: Report guest interfaces information in ``virDomainGetGuestInfo`` + + Libvirt is now able to report interfaces from the guest's + perspective (using guest agent). + * **Bug fixes** =20 * qemu: Open chardev logfile on behalf of QEMU diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst index 2204bed3bb..9f878d400c 100644 --- a/docs/manpages/virsh.rst +++ b/docs/manpages/virsh.rst @@ -2700,7 +2700,7 @@ guestinfo :: =20 guestinfo domain [--user] [--os] [--timezone] [--hostname] [--filesyste= m] - [--disk] + [--disk] [--interface] =20 Print information about the guest from the point of view of the guest agen= t. Note that this command requires a guest agent to be configured and running= in @@ -2711,7 +2711,7 @@ are supported by the guest agent. You can limit the t= ypes of information that are returned by specifying one or more flags. If a requested information type is not supported, the processes will provide an exit code of 1. Available information types flags are *--user*, *--os*, -*--timezone*, *--hostname*, *--filesystem* and *--disk*. +*--timezone*, *--hostname*, *--filesystem*, *--disk* and *--interface*. =20 Note that depending on the hypervisor type and the version of the guest ag= ent running within the domain, not all of the following information may be @@ -2779,6 +2779,14 @@ returned: * ``disk..alias`` - the device alias of the disk (e.g. sda) * ``disk..guest_alias`` - optional alias assigned to the disk =20 +*--interface* returns: +* ``if.count`` - the number of interfaces defined on this domain +* ``if..name`` - name in the guest (e.g. ``eth0``) for interface +* ``if..hwaddr`` - hardware address in the guest for interface +* ``if..addr.count`` - the number of IP addresses of interface +* ``if..addr..type`` - the type of IP address (e.g. ipv4) +* ``if..addr..addr`` - the address of IP address +* ``if..addr..prefix`` - the prefix of IP address =20 guestvcpus ---------- diff --git a/include/libvirt/libvirt-domain.h b/include/libvirt/libvirt-dom= ain.h index 7ef8ac51e5..b088847725 100644 --- a/include/libvirt/libvirt-domain.h +++ b/include/libvirt/libvirt-domain.h @@ -5074,6 +5074,7 @@ typedef enum { VIR_DOMAIN_GUEST_INFO_HOSTNAME =3D (1 << 3), /* return hostname inform= ation */ VIR_DOMAIN_GUEST_INFO_FILESYSTEM =3D (1 << 4), /* return filesystem in= formation */ VIR_DOMAIN_GUEST_INFO_DISKS =3D (1 << 5), /* return disks information = */ + VIR_DOMAIN_GUEST_INFO_INTERFACES =3D (1 << 6), /* return interfaces in= formation */ } virDomainGuestInfoTypes; =20 int virDomainGetGuestInfo(virDomainPtr domain, diff --git a/src/libvirt-domain.c b/src/libvirt-domain.c index a8a386e839..dfd84b4744 100644 --- a/src/libvirt-domain.c +++ b/src/libvirt-domain.c @@ -12586,6 +12586,18 @@ virDomainSetVcpu(virDomainPtr domain, * virTypedParamsFree to free memory returned in @params. * * Returns 0 on success, -1 on error. + * VIR_DOMAIN_GUEST_INFO_INTERFACES: + * Returns information about the interfaces within the domain. the typed + * parameter keys are in this format: + * + * "if.count" - the number of interfaces defined on this domain + * as an unsigned int + * "if..name" - interface name in the guest + * "if..hwaddr" - hardware address of interface + * "if..addr.count - the number of ip addresses of interface + * "if..addr..type" - the type of ip address(e.g. ipv4) + * "if..addr..addr" - the ip address + * "if..addr..prefix" - the number of bits of ip address pr= efix */ int virDomainGetGuestInfo(virDomainPtr domain, unsigned int types, diff --git a/src/qemu/qemu_agent.c b/src/qemu/qemu_agent.c index 5f421be6f6..a7f943e0dc 100644 --- a/src/qemu/qemu_agent.c +++ b/src/qemu/qemu_agent.c @@ -2245,17 +2245,20 @@ qemuAgentGetAllInterfaceAddresses(virDomainInterfac= ePtr **ifaces_ret, */ int qemuAgentGetInterfaces(qemuAgent *agent, - virDomainInterfacePtr **ifaces) + virDomainInterfacePtr **ifaces, + bool report_unsupported) { g_autoptr(virJSONValue) cmd =3D NULL; g_autoptr(virJSONValue) reply =3D NULL; virJSONValue *ret_array =3D NULL; + int rc; =20 if (!(cmd =3D qemuAgentMakeCommand("guest-network-get-interfaces", NUL= L))) return -1; =20 - if (qemuAgentCommand(agent, cmd, &reply, agent->timeout) < 0) - return -1; + if ((rc =3D qemuAgentCommandFull(agent, cmd, &reply, agent->timeout, + report_unsupported)) < 0) + return rc; =20 if (!(ret_array =3D virJSONValueObjectGetArray(reply, "return"))) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", diff --git a/src/qemu/qemu_agent.h b/src/qemu/qemu_agent.h index 81b45b8e5d..94eab9de9f 100644 --- a/src/qemu/qemu_agent.h +++ b/src/qemu/qemu_agent.h @@ -151,7 +151,8 @@ int qemuAgentSetTime(qemuAgent *mon, bool sync); =20 int qemuAgentGetInterfaces(qemuAgent *mon, - virDomainInterfacePtr **ifaces); + virDomainInterfacePtr **ifaces, + bool report_unsupported); =20 int qemuAgentSetUserPassword(qemuAgent *mon, const char *user, diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index f1f961c51c..0b803c392b 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -18957,7 +18957,7 @@ qemuDomainInterfaceAddresses(virDomainPtr dom, goto endjob; =20 agent =3D qemuDomainObjEnterAgent(vm); - ret =3D qemuAgentGetInterfaces(agent, ifaces); + ret =3D qemuAgentGetInterfaces(agent, ifaces, true); qemuDomainObjExitAgent(vm, agent); =20 endjob: @@ -19903,7 +19903,8 @@ static const unsigned int qemuDomainGetGuestInfoSup= portedTypes =3D VIR_DOMAIN_GUEST_INFO_TIMEZONE | VIR_DOMAIN_GUEST_INFO_HOSTNAME | VIR_DOMAIN_GUEST_INFO_FILESYSTEM | - VIR_DOMAIN_GUEST_INFO_DISKS; + VIR_DOMAIN_GUEST_INFO_DISKS | + VIR_DOMAIN_GUEST_INFO_INTERFACES; =20 static int qemuDomainGetGuestInfoCheckSupport(unsigned int types, @@ -20102,6 +20103,69 @@ qemuAgentFSInfoFormatParams(qemuAgentFSInfo **fsin= fo, } } =20 +static void +virDomainInterfaceFormatParams(virDomainInterfacePtr *ifaces, +int nifaces, +virTypedParameterPtr *params, +int *nparams, int * maxparams) +{ + size_t i, j; + const char *type =3D NULL; + + if (virTypedParamsAddUInt(params, nparams, maxparams, + "if.count", nifaces) < 0) + return; + + for (i =3D 0; i < nifaces; i++) { + char param_name[VIR_TYPED_PARAM_FIELD_LENGTH]; + g_snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH, + "if.%zu.name", i); + if (virTypedParamsAddString(params, nparams, maxparams, + param_name, ifaces[i]->name) < 0) + return; + + g_snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH, + "if.%zu.hwaddr", i); + if (virTypedParamsAddString(params, nparams, maxparams, + param_name, ifaces[i]->hwaddr) < 0) + return; + + g_snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH, + "if.%zu.addr.count", i); + if (virTypedParamsAddUInt(params, nparams, maxparams, + param_name, ifaces[i]->naddrs) < 0) + return; + + for (j =3D 0; j < ifaces[i]->naddrs; j++) { + switch (ifaces[i]->addrs[j].type) { + case VIR_IP_ADDR_TYPE_IPV4: + type =3D "ipv4"; + break; + case VIR_IP_ADDR_TYPE_IPV6: + type =3D "ipv6"; + break; + } + + g_snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH, + "if.%zu.addr.%zu.type", i, j); + if (virTypedParamsAddString(params, nparams, maxparams, + param_name, type) < 0) + return; + + g_snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH, + "if.%zu.addr.%zu.addr", i, j); + if (virTypedParamsAddString(params, nparams, maxparams, + param_name, ifaces[i]->addrs[j].addr) = < 0) + return; + + g_snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH, + "if.%zu.addr.%zu.prefix", i, j); + if (virTypedParamsAddUInt(params, nparams, maxparams, + param_name, ifaces[i]->addrs[j].prefix= ) < 0) + return; + } + } +} =20 static int qemuDomainGetGuestInfo(virDomainPtr dom, @@ -20123,6 +20187,8 @@ qemuDomainGetGuestInfo(virDomainPtr dom, qemuAgentFSInfo **agentfsinfo =3D NULL; size_t ndisks =3D 0; qemuAgentDiskInfo **agentdiskinfo =3D NULL; + virDomainInterfacePtr *ifaces =3D NULL; + size_t nifaces =3D 0; size_t i; =20 virCheckFlags(0, -1); @@ -20188,6 +20254,15 @@ qemuDomainGetGuestInfo(virDomainPtr dom, } } =20 + if (supportedTypes & VIR_DOMAIN_GUEST_INFO_INTERFACES) { + rc =3D qemuAgentGetInterfaces(agent, &ifaces, report_unsupported); + if (rc =3D=3D -1) { + goto exitagent; + } else if (rc >=3D 0) { + nifaces =3D rc; + } + } + qemuDomainObjExitAgent(vm, agent); qemuDomainObjEndAgentJob(vm); =20 @@ -20210,6 +20285,10 @@ qemuDomainGetGuestInfo(virDomainPtr dom, qemuDomainObjEndJob(driver, vm); } =20 + if (nifaces > 0) { + virDomainInterfaceFormatParams(ifaces, nifaces, params, nparams, &= maxparams); + } + ret =3D 0; =20 cleanup: @@ -20219,6 +20298,11 @@ qemuDomainGetGuestInfo(virDomainPtr dom, for (i =3D 0; i < ndisks; i++) qemuAgentDiskInfoFree(agentdiskinfo[i]); g_free(agentdiskinfo); + if (ifaces && nifaces > 0) { + for (i =3D 0; i < nifaces; i++) + virDomainInterfaceFree(ifaces[i]); + } + g_free(ifaces); =20 virDomainObjEndAPI(&vm); return ret; diff --git a/tests/qemuagenttest.c b/tests/qemuagenttest.c index a447c93494..e0b22c042a 100644 --- a/tests/qemuagenttest.c +++ b/tests/qemuagenttest.c @@ -920,7 +920,7 @@ testQemuAgentGetInterfaces(const void *data) goto cleanup; =20 if ((ifaces_count =3D qemuAgentGetInterfaces(qemuMonitorTestGetAgent(t= est), - &ifaces)) < 0) + &ifaces, true)) < 0) goto cleanup; =20 if (ifaces_count !=3D 4) { diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index e5bd1fdd75..5ba5bd6b8c 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -13829,6 +13829,10 @@ static const vshCmdOptDef opts_guestinfo[] =3D { .type =3D VSH_OT_BOOL, .help =3D N_("report disk information"), }, + {.name =3D "interface", + .type =3D VSH_OT_BOOL, + .help =3D N_("report interface information"), + }, {.name =3D NULL} }; =20 @@ -13854,6 +13858,8 @@ cmdGuestInfo(vshControl *ctl, const vshCmd *cmd) types |=3D VIR_DOMAIN_GUEST_INFO_FILESYSTEM; if (vshCommandOptBool(cmd, "disk")) types |=3D VIR_DOMAIN_GUEST_INFO_DISKS; + if (vshCommandOptBool(cmd, "interface")) + types |=3D VIR_DOMAIN_GUEST_INFO_INTERFACES; =20 if (!(dom =3D virshCommandOptDomain(ctl, cmd, NULL))) return false; --=20 2.31.1