From nobody Sun Feb 8 08:48:42 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) client-ip=63.128.21.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 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1602074169; cv=none; d=zohomail.com; s=zohoarc; b=Ea5Af2jSJXlQoANcunNp9iP+1xLyXpK3jhxcKK840BeaqITi+2TyLw35mAL1IGhOL+biFQFCsrqHOH9aRdA2v9o9v6n/ZdjlGTh+I+GSA7ZvqZOPxxMkNqrNb+4v+xsGdBWdcRrdL5zswyhKVfMHU6DCnR8mL4+Fh/TD+vM6twI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1602074169; h=Content-Type:Content-Transfer-Encoding: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=9HktDvJCoTAXX791jB+uQQeFZXrQS9lmKc1LPoDbNIo=; b=nI9ssixEz9LRcZi6F50DLqfFy4Pldnhwy7itKZJoLRoqPDIPV3zxX67i7B/VC2d8CZ7y9o9BjxZi1I5Rvb0jjgnkj0wjcgE0P65BeMbibL4XPLWwHVE12lHm//ZxLYfVbcMqqkldsKl1GOwlRajdxHRxNl2h89RNodCpqY7xFJM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by mx.zohomail.com with SMTPS id 160207416981399.40372385976968; Wed, 7 Oct 2020 05:36: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-336-RFx-jSqGPg2df4yafizhwg-1; Wed, 07 Oct 2020 08:36:01 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3A2D1192AB6F; Wed, 7 Oct 2020 12:35:51 +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 195315D9DD; Wed, 7 Oct 2020 12:35:51 +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 D36AC18408AE; Wed, 7 Oct 2020 12:35:50 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 097CZkSM012460 for ; Wed, 7 Oct 2020 08:35:46 -0400 Received: by smtp.corp.redhat.com (Postfix) id B614C76642; Wed, 7 Oct 2020 12:35:46 +0000 (UTC) Received: from hjs.redhat.com (unknown [10.43.2.12]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3DE04702E7 for ; Wed, 7 Oct 2020 12:35:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1602074168; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to: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=9HktDvJCoTAXX791jB+uQQeFZXrQS9lmKc1LPoDbNIo=; b=bQDYilLnX/Se2C4Yy/2eGMV7DYvl8FaTE6NILWu0mkIkMalIIxz6hKOXJQ8K7B6BnVQxwq O3/DLlVaaMqnVBOeKIYT86t1KOZKTttUnbHbyTskOrgJoQRwsao7a5DSg0YY3+x7ksfiRU HtBwJbbcnGzsLRd5GCQjy+ei/MXoS6o= X-MC-Unique: RFx-jSqGPg2df4yafizhwg-1 From: =?UTF-8?q?J=C3=A1n=20Tomko?= To: libvir-list@redhat.com Subject: [libvirt PATCHv2 09/16] qemu: agent: split out qemuAgentGetInterfaceAddresses Date: Wed, 7 Oct 2020 14:35:30 +0200 Message-Id: <3e0b95db2723669a8433efffccbdad8a756fdc02.1602074066.git.jtomko@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com 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.14 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-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Convert one interface from the "return" array returned by "guest-network-get-interfaces" to virDomainInterface. Due to the functionality of squashing interface aliases together, this is not a pure function - it either: * Adds the interface to ifaces_ret, incrementing ifaces_count and adds a pointer to it into the ifaces_store hash table. * Adds the additional IP addresses from the interface alias to the existing interface entry, found through the hash table. This does not increment ifaces_count or extend the array. Signed-off-by: J=C3=A1n Tomko --- src/qemu/qemu_agent.c | 162 +++++++++++++++++++++++++----------------- 1 file changed, 98 insertions(+), 64 deletions(-) diff --git a/src/qemu/qemu_agent.c b/src/qemu/qemu_agent.c index 721ff55fff..e614fdd7c4 100644 --- a/src/qemu/qemu_agent.c +++ b/src/qemu/qemu_agent.c @@ -2104,6 +2104,101 @@ qemuAgentGetInterfaceOneAddress(virDomainIPAddressP= tr ip_addr, } =20 =20 +/** + * qemuAgentGetInterfaceAddresses: + * @ifaces_ret: the array to put/update the interface in + * @ifaces_count: the number of interfaces in that array + * @ifaces_store: hash table into @ifaces_ret by interface name + * @tmp_iface: one item from the JSON array of interfaces + * + * This function processes @tmp_iface (which represents + * information about a single interface) and adds the information + * into the ifaces_ret array. + * + * If we're processing an interface alias, the suffix is stripped + * and information is appended to the entry found via the @ifaces_store + * hash table. + * + * Otherwise, the next free position in @ifaces_ret is used, + * its address added to @ifaces_store, and @ifaces_count incremented. + */ +static int +qemuAgentGetInterfaceAddresses(virDomainInterfacePtr **ifaces_ret, + size_t *ifaces_count, + virHashTablePtr ifaces_store, + virJSONValuePtr tmp_iface) +{ + virJSONValuePtr ip_addr_arr =3D NULL; + const char *hwaddr, *ifname_s, *name =3D NULL; + virDomainInterfacePtr iface =3D NULL; + g_auto(GStrv) ifname =3D NULL; + size_t addrs_count =3D 0; + size_t j; + + /* interface name is required to be presented */ + name =3D virJSONValueObjectGetString(tmp_iface, "name"); + if (!name) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("qemu agent didn't provide 'name' field")); + return -1; + } + + /* Handle interface alias (:) */ + ifname =3D virStringSplit(name, ":", 2); + ifname_s =3D ifname[0]; + + iface =3D virHashLookup(ifaces_store, ifname_s); + + /* If the hash table doesn't contain this iface, add it */ + if (!iface) { + if (VIR_EXPAND_N(*ifaces_ret, *ifaces_count, 1) < 0) + return -1; + + iface =3D g_new0(virDomainInterface, 1); + (*ifaces_ret)[*ifaces_count - 1] =3D iface; + + if (virHashAddEntry(ifaces_store, ifname_s, iface) < 0) + return -1; + + iface->naddrs =3D 0; + iface->name =3D g_strdup(ifname_s); + + hwaddr =3D virJSONValueObjectGetString(tmp_iface, "hardware-addres= s"); + iface->hwaddr =3D g_strdup(hwaddr); + } + + /* as well as IP address which - moreover - + * can be presented multiple times */ + ip_addr_arr =3D virJSONValueObjectGet(tmp_iface, "ip-addresses"); + if (!ip_addr_arr) + return 0; + + if (!virJSONValueIsArray(ip_addr_arr)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Malformed ip-addresses array")); + return -1; + } + + /* If current iface already exists, continue with the count */ + addrs_count =3D iface->naddrs; + + if (VIR_EXPAND_N(iface->addrs, addrs_count, + virJSONValueArraySize(ip_addr_arr)) < 0) + return -1; + + for (j =3D 0; j < virJSONValueArraySize(ip_addr_arr); j++) { + virJSONValuePtr ip_addr_obj =3D virJSONValueArrayGet(ip_addr_arr, = j); + virDomainIPAddressPtr ip_addr =3D iface->addrs + iface->naddrs; + iface->naddrs++; + + if (qemuAgentGetInterfaceOneAddress(ip_addr, ip_addr_obj, name) < = 0) + return -1; + } + + return 0; +} + + /* * qemuAgentGetInterfaces: * @agent: agent object @@ -2120,7 +2215,7 @@ qemuAgentGetInterfaces(qemuAgentPtr agent, virDomainInterfacePtr **ifaces) { int ret =3D -1; - size_t i, j; + size_t i; virJSONValuePtr cmd =3D NULL; virJSONValuePtr reply =3D NULL; virJSONValuePtr ret_array =3D NULL; @@ -2151,71 +2246,10 @@ qemuAgentGetInterfaces(qemuAgentPtr agent, =20 for (i =3D 0; i < virJSONValueArraySize(ret_array); i++) { virJSONValuePtr tmp_iface =3D virJSONValueArrayGet(ret_array, i); - virJSONValuePtr ip_addr_arr =3D NULL; - const char *hwaddr, *ifname_s, *name =3D NULL; - virDomainInterfacePtr iface =3D NULL; - g_auto(GStrv) ifname =3D NULL; - size_t addrs_count =3D 0; =20 - /* interface name is required to be presented */ - name =3D virJSONValueObjectGetString(tmp_iface, "name"); - if (!name) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("qemu agent didn't provide 'name' field")); + if (qemuAgentGetInterfaceAddresses(&ifaces_ret, &ifaces_count, + ifaces_store, tmp_iface) < 0) goto error; - } - - /* Handle interface alias (:) */ - ifname =3D virStringSplit(name, ":", 2); - ifname_s =3D ifname[0]; - - iface =3D virHashLookup(ifaces_store, ifname_s); - - /* If the hash table doesn't contain this iface, add it */ - if (!iface) { - if (VIR_EXPAND_N(ifaces_ret, ifaces_count, 1) < 0) - goto error; - - iface =3D g_new0(virDomainInterface, 1); - ifaces_ret[ifaces_count - 1] =3D iface; - - if (virHashAddEntry(ifaces_store, ifname_s, iface) < 0) - goto error; - - iface->naddrs =3D 0; - iface->name =3D g_strdup(ifname_s); - - hwaddr =3D virJSONValueObjectGetString(tmp_iface, "hardware-ad= dress"); - iface->hwaddr =3D g_strdup(hwaddr); - } - - /* as well as IP address which - moreover - - * can be presented multiple times */ - ip_addr_arr =3D virJSONValueObjectGet(tmp_iface, "ip-addresses"); - if (!ip_addr_arr) - continue; - - if (!virJSONValueIsArray(ip_addr_arr)) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Malformed ip-addresses array")); - goto error; - } - - /* If current iface already exists, continue with the count */ - addrs_count =3D iface->naddrs; - - if (VIR_EXPAND_N(iface->addrs, addrs_count, - virJSONValueArraySize(ip_addr_arr)) < 0) - goto error; - - for (j =3D 0; j < virJSONValueArraySize(ip_addr_arr); j++) { - virJSONValuePtr ip_addr_obj =3D virJSONValueArrayGet(ip_addr_a= rr, j); - virDomainIPAddressPtr ip_addr =3D iface->addrs + iface->naddrs; - iface->naddrs++; - - if (qemuAgentGetInterfaceOneAddress(ip_addr, ip_addr_obj, name= ) < 0) - goto error; - } } =20 *ifaces =3D g_steal_pointer(&ifaces_ret); --=20 2.26.2