From nobody Fri May 3 13:33:39 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; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.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=1616448657; cv=none; d=zohomail.com; s=zohoarc; b=h9oaEaCvpzq8cKwX6vFFMNljfwbyVpCEdGKwP1x/04DNrac4I+doo+aByh/ld21kEkWnk2or3CIaa7AC+aAkQHzWX0eG+3kqstNJrEsCLpeGw1BzYVTQrd2UoSenVe2BXduxBLEA8mu9J8BAOZkykNQivbFAppxdYyA7cwQCkxo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1616448657; h=Content-Type:Content-Transfer-Encoding:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=f/G323dZ76aWkFs7jzdZmC1uaXtlaw4zMeUgERW15Y8=; b=ZmdLn5Sf/BgV0/rjT5pP9YQHkzfqXi2CNnPOhWn9xIISg/jsd7vq1PWMzyWj0bDKT6IlS2qRhunphx6BVCc2a7Hop/PEUi5rtgNTE3AMEXLb838Y/pGuIEJOf34pN1gqmiBAwCW/3bmeEChTy6AIiiPPEZoLpdMkDF9703DPfn4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.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 [216.205.24.124]) by mx.zohomail.com with SMTPS id 1616448657470290.30157220506567; Mon, 22 Mar 2021 14:30:57 -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-433-TarVDiWtOJmNEuWal-VzSQ-1; Mon, 22 Mar 2021 17:30:54 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 66F4A180FCA6; Mon, 22 Mar 2021 21:30:45 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id CD53219CBA; Mon, 22 Mar 2021 21:30:44 +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 E11B84BB7C; Mon, 22 Mar 2021 21:30:40 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 12MLUchi010775 for ; Mon, 22 Mar 2021 17:30:38 -0400 Received: by smtp.corp.redhat.com (Postfix) id A0D2517195; Mon, 22 Mar 2021 21:30:38 +0000 (UTC) Received: from vhost2.router.laine.org (ovpn-114-23.phx2.redhat.com [10.3.114.23]) by smtp.corp.redhat.com (Postfix) with ESMTP id 57FD361D2E for ; Mon, 22 Mar 2021 21:30:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1616448656; 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:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=f/G323dZ76aWkFs7jzdZmC1uaXtlaw4zMeUgERW15Y8=; b=Wd/fwUbx0o5OkfApBBabhTIQ7wHcaqzbxMH6N9agiRGVrGb0diM46Lh3RbTUT6ATw5rFvH ZWGrF9mkgav0OEt2OdMa+55gyCPdCii1oYWO8wpAr1kfl9i2+BD0ZemHzcT1931b9Lgdr+ 5T4OFQQUyc+0chlmzzbH7FfV0Afjnpc= X-MC-Unique: TarVDiWtOJmNEuWal-VzSQ-1 From: Laine Stump To: libvir-list@redhat.com Subject: [libvirt PATCH] conf: allow for a partial match when searching for an Date: Mon, 22 Mar 2021 17:30:34 -0400 Message-Id: <20210322213034.884154-1-laine@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 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.11 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-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Commit 114e3b42 modified virDomainNetFindIdx() to compare the alias name of the being matched (in addition to already-existing match of MAC address and PCI/CCW address). This was done in response to https://bugzilla.redhat.com/1926190 which complained that it wasn't possible to unplug an interface that had the same MAC address as another (which is the way interfaces are setup when using - the "team" is identified in the guest virtio-net driver by looking for another interface with the same MAC as the virtio-net device) - the reporter of that bug did not have PCI addresses of the devices easily available when attempting to unplug one of the two devices, and so needed some other way to disambiguate the two interfaces with matching MACs. Unfortunately, this caused a regression which was caught during QE testing - in the past it had been possible to use virDomainUpdateDevice (which also calls virDomainNetFindIdx()) to modify the alias name of an existing interface - with the change in commit 114e3b42 this was no longer possible (since we would try to match the new alias, which would of course always fail). The solution to this regression is to allow mismatched alias names when calling virDomainNetFindIdx() for purposes of updating a device (indicated by the new bool argument "partialMatch"). When calling for unplug the old behavior is maintained - in that case the alias name must still match. Because we need to keep track of potentially multiple "partial" matches so that we can go back later and try to disambiguate when necessary, I needed an array to hold the indexes of all the matches during the "first round". There is guidance in glib-adoption.rst saying that new libvirt code should prefer GArray/GPtrArray. A couple of adventurous souls have used GPtrArray, but so far nobody has used GArray, and I decided this was a good chance to try that out. It went okay. Reported-by: Yalan Zhang Signed-off-by: Laine Stump Reviewed-by: Michal Privoznik --- I realized while writing this patch that an update-device test case in qemuhotplugtest would have caught this regression and so I probably should add such a test case to prevent it happening again, but the testing harness for update-device was only ever made to work for graphics devices, meaning there's some unknown amount of investigating and rejiggering that needs to be done to make such a test work (a quick attempt failed). Since someone is waiting on the fix for this regression, I'm hoping that I can get a reprieve on the "add a test case to catch thre regression" part that should accompany a bugfix like this in exchange for a promise that I will start looking into that immediately after I get this pushed (and then backported so testing of the bugzilla above can be completed) src/conf/domain_conf.c | 207 +++++++++++++++++++++++++-------------- src/conf/domain_conf.h | 2 +- src/libxl/libxl_driver.c | 4 +- src/lxc/lxc_driver.c | 6 +- src/qemu/qemu_driver.c | 6 +- src/qemu/qemu_hotplug.c | 4 +- 6 files changed, 145 insertions(+), 84 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 95602ae57e..f071bf93d0 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -16871,109 +16871,170 @@ int virDomainNetInsert(virDomainDefPtr def, vir= DomainNetDefPtr net) * virDomainNetFindIdx: * @def: domain definition * @net: interface definition + * @matchPartial: true if mismatched alias is still a match * - * Lookup domain's network interface based on passed @net - * definition. If @net's MAC address was auto generated, - * the MAC comparison is ignored. + * Find a network interface from the domain's nets list by looking for + * a match to @net. The following attributes are compared when + * specified in @net: MAC address, PCI/CCW address, and alias name. + * + * If matchPartial is true, then an entry with mismatched alias name + * still counts as a match (as long as everything else that was + * specified matches). + * + * If @net matches multiple items in nets, that is an error. * * Return: index of match if unique match found, * -1 otherwise and an error is logged. */ int -virDomainNetFindIdx(virDomainDefPtr def, virDomainNetDefPtr net) +virDomainNetFindIdx(virDomainDefPtr def, + virDomainNetDefPtr net, + bool matchPartial) { + guint gi; size_t i; - int matchidx =3D -1; + g_autoptr(GArray) matches + =3D g_array_sized_new(false, false, sizeof(size_t), def->nnets); char mac[VIR_MAC_STRING_BUFLEN]; bool MACAddrSpecified =3D !net->mac_generated; bool PCIAddrSpecified =3D virDomainDeviceAddressIsValid(&net->info, VIR_DOMAIN_DEVIC= E_ADDRESS_TYPE_PCI); bool CCWAddrSpecified =3D virDomainDeviceAddressIsValid(&net->info, VIR_DOMAIN_DEVIC= E_ADDRESS_TYPE_CCW); + bool ambiguousPartial =3D false; + g_auto(virBuffer) errBuf =3D VIR_BUFFER_INITIALIZER; + + /* initialize the matches array to contain the indexes for all + * NetDefs. We will then go through this array for each field we + * want to match on, removing the indexes of NetDefs that don't + * match for that field. At the end we'll have no match, one + * match, or ambiguous (multiple) matches. + */ + for (i =3D 0; i < def->nnets; i++) + g_array_append_val(matches, i); + + for (gi =3D 0; gi < matches->len;) { + + i =3D g_array_index(matches, size_t, gi); + /* + * mac/pci/ccw addresses must always match if they are + * specified in the search template object + */ =20 - for (i =3D 0; i < def->nnets; i++) { if (MACAddrSpecified && - virMacAddrCmp(&def->nets[i]->mac, &net->mac) !=3D 0) + virMacAddrCmp(&def->nets[i]->mac, &net->mac) !=3D 0) { + g_array_remove_index(matches, gi); continue; + } =20 if (PCIAddrSpecified && !virPCIDeviceAddressEqual(&def->nets[i]->info.addr.pci, - &net->info.addr.pci)) + &net->info.addr.pci)) { + g_array_remove_index(matches, gi); continue; + } =20 if (CCWAddrSpecified && !virDomainDeviceCCWAddressEqual(&def->nets[i]->info.addr.ccw, - &net->info.addr.ccw)) - continue; - - if (net->info.alias && - STRNEQ_NULLABLE(def->nets[i]->info.alias, net->info.alias)) { - continue; + &net->info.addr.ccw)) { + g_array_remove_index(matches, gi); + continue; } =20 - if (matchidx >=3D 0) { - /* there were multiple matches on mac address, and no - * qualifying guest-side PCI/CCW address was given, so we must - * fail (NB: a USB address isn't adequate, since it may - * specify only vendor and product ID, and there may be - * multiples of those. - */ - if (MACAddrSpecified) { - virReportError(VIR_ERR_OPERATION_FAILED, - _("multiple devices matching MAC address %s= found"), - virMacAddrFormat(&net->mac, mac)); - } else { - virReportError(VIR_ERR_OPERATION_FAILED, "%s", - _("multiple matching devices found")); - } + /* this NetDef matches all the mandatory fields, so keep it for no= w */ + gi++; + } =20 - return -1; - } + if (matches->len =3D=3D 0) + goto cleanup; =20 - matchidx =3D i; - } - - if (matchidx < 0) { - if (MACAddrSpecified && PCIAddrSpecified) { - virReportError(VIR_ERR_DEVICE_MISSING, - _("no device matching MAC address %s found on " - VIR_PCI_DEVICE_ADDRESS_FMT), - virMacAddrFormat(&net->mac, mac), - net->info.addr.pci.domain, - net->info.addr.pci.bus, - net->info.addr.pci.slot, - net->info.addr.pci.function); - } else if (MACAddrSpecified && CCWAddrSpecified) { - virReportError(VIR_ERR_DEVICE_MISSING, - _("no device matching MAC address %s found on " - VIR_CCW_DEVICE_ADDRESS_FMT), - virMacAddrFormat(&net->mac, mac), - net->info.addr.ccw.cssid, - net->info.addr.ccw.ssid, - net->info.addr.ccw.devno); - } else if (PCIAddrSpecified) { - virReportError(VIR_ERR_DEVICE_MISSING, - _("no device found on " VIR_PCI_DEVICE_ADDRESS_= FMT), - net->info.addr.pci.domain, - net->info.addr.pci.bus, - net->info.addr.pci.slot, - net->info.addr.pci.function); - } else if (CCWAddrSpecified) { - virReportError(VIR_ERR_DEVICE_MISSING, - _("no device found on " VIR_CCW_DEVICE_ADDRESS_= FMT), - net->info.addr.ccw.cssid, - net->info.addr.ccw.ssid, - net->info.addr.ccw.devno); - } else if (MACAddrSpecified) { - virReportError(VIR_ERR_DEVICE_MISSING, - _("no device matching MAC address %s found"), - virMacAddrFormat(&net->mac, mac)); - } else { - virReportError(VIR_ERR_DEVICE_MISSING, "%s", - _("no matching device found")); + /* A "partial match" is when everything but alias matches. When + * we've already determined that exactly one NetDef matches + * everything else that was specified in the object we're trying + * to match, counting this "partial match" as a success allows for + * finding an interface based on MAC/pci address, with the intent + * to update the alias (i.e. the alias name doesn't match because + * it's the *new* alias name we want to change to). + * + * We can't *always* ignore the alias though, since it is + * sometimes used to disambiguate between multiple NetDefs when + * searching for a NetDef to remove (rather than update) - in + * particular if there are multiple NetDefs with the same MAC + * address, and the caller didn't specify PCI address (because + * they don't know it). Obviously when it's necessary to look at + * the alias in order to disambiguate between two NetDefs, it + * won't be possible to update the alias; updating other + * attributes, or unplugging the device, shouldn't be a problem + * though. + */ + if (matchPartial) { + if (matches->len =3D=3D 1) + goto cleanup; + + /* by the time we've checked the multiple remaining NetDefs + * for a match to the alias, we'll no longer have the + * information about ambiguity in the match prior to checking + * aliases, but we'll need to know that in order to report the + * proper error, so save that fact now. + */ + ambiguousPartial =3D true; + } + + if (net->info.alias) { + for (gi =3D 0; gi < matches->len;) { + i =3D g_array_index(matches, size_t, gi); + + if (STRNEQ_NULLABLE(def->nets[i]->info.alias, net->info.alias)) + g_array_remove_index(matches, gi); + else + gi++; } } - return matchidx; + + cleanup: + if (matches->len =3D=3D 1) + return g_array_index(matches, size_t, 0); + + /* make a string describing all the match terms */ + if (MACAddrSpecified) + virBufferAsprintf(&errBuf, "MAC address '%s' ", virMacAddrFormat(&= net->mac, mac)); + + if (PCIAddrSpecified) { + g_autofree char *pciAddrStr =3D virPCIDeviceAddressAsString(&net->= info.addr.pci); + + virBufferAsprintf(&errBuf, "PCI address '%s' ", pciAddrStr); + } + + if (CCWAddrSpecified) { + virBufferAsprintf(&errBuf, "CCW address '" VIR_CCW_DEVICE_ADDRESS_= FMT "' ", + net->info.addr.ccw.cssid, + net->info.addr.ccw.ssid, + net->info.addr.ccw.devno); + } + + if (net->info.alias) + virBufferAsprintf(&errBuf, "alias name '%s' ", net->info.alias); + + virBufferTrim(&errBuf, " "); + + /* we either matched too many... */ + + if (matches->len > 1 || ambiguousPartial) { + virReportError(VIR_ERR_OPERATION_FAILED, + _("found multiple devices matching requested (%s)"), + virBufferContentAndReset(&errBuf)); + return -1; + } + + + /* ...or not enough + * (at this point we know matches->len =3D=3D 0, no need to check) + */ + + virReportError(VIR_ERR_OPERATION_FAILED, + _("found no devices matching requested (%s)= "), + virBufferContentAndReset(&errBuf)); + return -1; } =20 bool diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 87bc7e8625..da32016b01 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -3554,7 +3554,7 @@ virDomainDiskRemove(virDomainDefPtr def, size_t i); virDomainDiskDefPtr virDomainDiskRemoveByName(virDomainDefPtr def, const char *name); =20 -int virDomainNetFindIdx(virDomainDefPtr def, virDomainNetDefPtr net); +int virDomainNetFindIdx(virDomainDefPtr def, virDomainNetDefPtr net, bool = matchPartial); virDomainNetDefPtr virDomainNetFind(virDomainDefPtr def, const char *devic= e); virDomainNetDefPtr virDomainNetFindByName(virDomainDefPtr def, const char = *ifname); bool virDomainHasNet(virDomainDefPtr def, virDomainNetDefPtr net); diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c index 830634b2bd..bfbfdf94e6 100644 --- a/src/libxl/libxl_driver.c +++ b/src/libxl/libxl_driver.c @@ -3861,7 +3861,7 @@ libxlDomainDetachNetDevice(libxlDriverPrivatePtr driv= er, =20 libxl_device_nic_init(&nic); =20 - if ((detachidx =3D virDomainNetFindIdx(vm->def, net)) < 0) + if ((detachidx =3D virDomainNetFindIdx(vm->def, net, false)) < 0) goto cleanup; =20 detach =3D vm->def->nets[detachidx]; @@ -3990,7 +3990,7 @@ libxlDomainDetachDeviceConfig(virDomainDefPtr vmdef, = virDomainDeviceDefPtr dev) =20 case VIR_DOMAIN_DEVICE_NET: net =3D dev->data.net; - if ((idx =3D virDomainNetFindIdx(vmdef, net)) < 0) + if ((idx =3D virDomainNetFindIdx(vmdef, net, false)) < 0) return -1; =20 /* this is guaranteed to succeed */ diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index 8e0ec82e0b..d96a6f5f2e 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -3096,7 +3096,7 @@ lxcDomainUpdateDeviceConfig(virDomainDefPtr vmdef, switch (dev->type) { case VIR_DOMAIN_DEVICE_NET: net =3D dev->data.net; - if ((idx =3D virDomainNetFindIdx(vmdef, net)) < 0) + if ((idx =3D virDomainNetFindIdx(vmdef, net, true)) < 0) return -1; =20 oldDev.data.net =3D vmdef->nets[idx]; @@ -3150,7 +3150,7 @@ lxcDomainDetachDeviceConfig(virDomainDefPtr vmdef, =20 case VIR_DOMAIN_DEVICE_NET: net =3D dev->data.net; - if ((idx =3D virDomainNetFindIdx(vmdef, net)) < 0) + if ((idx =3D virDomainNetFindIdx(vmdef, net, false)) < 0) return -1; =20 /* this is guaranteed to succeed */ @@ -3962,7 +3962,7 @@ lxcDomainDetachDeviceNetLive(virDomainObjPtr vm, const virNetDevVPortProfile *vport =3D NULL; virErrorPtr save_err =3D NULL; =20 - if ((detachidx =3D virDomainNetFindIdx(vm->def, dev->data.net)) < 0) + if ((detachidx =3D virDomainNetFindIdx(vm->def, dev->data.net, false))= < 0) goto cleanup; =20 detach =3D vm->def->nets[detachidx]; diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index f3f8caab44..df69782f9c 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -7159,7 +7159,7 @@ qemuDomainUpdateDeviceLive(virDomainObjPtr vm, break; =20 case VIR_DOMAIN_DEVICE_NET: - if ((idx =3D virDomainNetFindIdx(vm->def, dev->data.net)) >=3D 0) { + if ((idx =3D virDomainNetFindIdx(vm->def, dev->data.net, true)) >= =3D 0) { oldDev.data.net =3D vm->def->nets[idx]; if (virDomainDefCompatibleDevice(vm->def, dev, &oldDev, VIR_DOMAIN_DEVICE_ACTION_UPDA= TE, @@ -7459,7 +7459,7 @@ qemuDomainDetachDeviceConfig(virDomainDefPtr vmdef, =20 case VIR_DOMAIN_DEVICE_NET: net =3D dev->data.net; - if ((idx =3D virDomainNetFindIdx(vmdef, net)) < 0) + if ((idx =3D virDomainNetFindIdx(vmdef, net, false)) < 0) return -1; =20 /* this is guaranteed to succeed */ @@ -7686,7 +7686,7 @@ qemuDomainUpdateDeviceConfig(virDomainDefPtr vmdef, =20 case VIR_DOMAIN_DEVICE_NET: net =3D dev->data.net; - if ((pos =3D virDomainNetFindIdx(vmdef, net)) < 0) + if ((pos =3D virDomainNetFindIdx(vmdef, net, true)) < 0) return -1; =20 oldDev.data.net =3D vmdef->nets[pos]; diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c index 58d2abb862..f0cf75e547 100644 --- a/src/qemu/qemu_hotplug.c +++ b/src/qemu/qemu_hotplug.c @@ -3512,7 +3512,7 @@ qemuDomainChangeNet(virQEMUDriverPtr driver, g_autoptr(virConnect) conn =3D NULL; virErrorPtr save_err =3D NULL; =20 - if ((changeidx =3D virDomainNetFindIdx(vm->def, newdev)) < 0) + if ((changeidx =3D virDomainNetFindIdx(vm->def, newdev, true)) < 0) goto cleanup; devslot =3D &vm->def->nets[changeidx]; olddev =3D *devslot; @@ -5664,7 +5664,7 @@ qemuDomainDetachPrepNet(virDomainObjPtr vm, { int detachidx; =20 - if ((detachidx =3D virDomainNetFindIdx(vm->def, match)) < 0) + if ((detachidx =3D virDomainNetFindIdx(vm->def, match, false)) < 0) return -1; =20 *detach =3D vm->def->nets[detachidx]; --=20 2.30.2