From nobody Sat May 2 13:13:09 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail header.i=maximilian_martin@gmx.de; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass(p=reject dis=none) header.from=lists.libvirt.org ARC-Seal: i=1; a=rsa-sha256; t=1745264505; cv=none; d=zohomail.com; s=zohoarc; b=I/dcQlQb0YMs0uq9qtZ9TwPKTYsGXRAn25a2P+CNSh8x0l9CFXdCCQHRDm2tOyi14rfrPED9EmfU4trhzKZCnyt76z4pU4YOU4/EG/FRlJio5Vpt3EuQSSxOve/0hiX+3QMVrW2Sn7gCN2r2oCYx1jnpkcHx0ZmkITyHyEzKukw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1745264505; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Subject:Subject:To:To:Message-Id; bh=JZxbFyuFGP2fiGUyh/jFF16l2R5O5phA9RWe6DFvMSU=; b=MZ1l1Sm7jxQKNOO4mZZ7N5bln2nk4qMTYsQYoi+JAA2uxSBSd+lKGd+ax/j+KIt7jYIG8YI8DSjbmoSrBq3U+aAMN9lMpWU0AatA+TDGR3XoDwReX6sJ1eF+kQJe3nR+YrWwdl1QZntTzeJHD9m/ZJ9qvF1QWUV9JerPvNa112s= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail header.i=maximilian_martin@gmx.de; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1745264505505786.6871604923718; Mon, 21 Apr 2025 12:41:45 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 7925DE76; Mon, 21 Apr 2025 15:41:44 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 5D5B9C95; Mon, 21 Apr 2025 15:39:12 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 0287FC91; Mon, 21 Apr 2025 15:38:59 -0400 (EDT) Received: from mout.gmx.net (mout.gmx.net [212.227.15.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 71E61E90 for ; Mon, 21 Apr 2025 15:38:47 -0400 (EDT) Received: from cu-host.fritz.box ([185.17.205.183]) by mail.gmx.net (mrgmx004 [212.227.17.190]) with ESMTPSA (Nemesis) id 1M3UUy-1u6OkG16jC-00G5gu; Mon, 21 Apr 2025 21:38:46 +0200 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-1.5 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.de; s=s31663417; t=1745264326; x=1745869126; i=maximilian_martin@gmx.de; bh=JZxbFyuFGP2fiGUyh/jFF16l2R5O5phA9RWe6DFvMSU=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:Message-Id:In-Reply-To: References:MIME-Version:Content-Transfer-Encoding:cc: content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=R1+o6jL0oI2jfXg3h0sOMFNnmTmRg575bgeHC1DCMQfZkFYn7n7rJay6XKt7hXij BhL4rZA3UmLOtc/LX6iVLIanTFNDonwVZz/L1cQsTMc7fCSSn0T48yEDHR0JolS5T f/6WaokOanrIdCq8f4lat1JO/lTSQoXJZQKUdGlNCz2KYrutkgr1BQ4R45XRbL9Tx bAFFWUsvpQS9rZMuiDN/S0ODNKBOjM726sIh3MgIG1IySWnQRtipzs1OV9mzUEGkM sCyAMQFzQMXXh7Pb8LL3c7hmyLgn3KZjKcJCFfpAK1foFg1DkBSpkWi+vN/19LoxR iuBNgI8KpDisMzrSvQ== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a To: devel@lists.libvirt.org Subject: [PATCH 2/4] domain_conf, virhostdev, virusb, virusb test: add bus/port matching Date: Mon, 21 Apr 2025 21:38:36 +0200 Message-Id: <20250421193838.1934-3-maximilian_martin@gmx.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250421193838.1934-1-maximilian_martin@gmx.de> References: <20250421193838.1934-1-maximilian_martin@gmx.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:CLVeSQtaFNYpwSLEWXeN/EIKzqDHFYs0xRDgUy7bFned9Eykiv6 OV/1DeFhM/gXMmFIwDo5n54KsDIydabCn2PQjFP012nUYwG8VGP/7SL/1a1TICrlBYeEO0I GNR2DsDF1pHw/Q7TbIX92i8mi1+XvTUKv3n6CZthEPA/nh8HIIW65ATMm9B4afVrmChpnk5 nFRWs/cmQSo77RAPm+17A== UI-OutboundReport: notjunk:1;M01:P0:XRy5nTY/0fg=;pyYs81xqxotaruiyLRfqBBAHO4S 0jyiazNukK9tPelKhRy1+xPG8lVBrFFEYiel+afLpiMrnkxWtkyhq+9dvyyNMN61e+3ePFxs9 mXJBSgD9CbBksppiK7uRmYZmw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-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header CC: Maximilian Martin X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: From: Maximilian Martin via Devel Reply-To: Maximilian Martin X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1745264508383019100 Content-Type: text/plain; charset="utf-8" From: Maximilian Martin This patch implements USB bus/port matching. In addition to vendor/product and bus/device matching, bus/port matching is implemented. This involves additions and refactorings in the domain configuration, host device handling, and USB helpers. Also, test methods are refactored and extended. Signed-off-by: Maximilian Martin --- src/conf/domain_conf.c | 69 ++++++++++++++-- src/conf/domain_conf.h | 1 + src/hypervisor/virhostdev.c | 131 +++++++++++++++++------------- src/libvirt_private.syms | 2 - src/util/virusb.c | 156 ++++++++++++------------------------ src/util/virusb.h | 32 ++++---- tests/virusbtest.c | 149 ++++++++++++++++++++++++---------- 7 files changed, 312 insertions(+), 228 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 542d6ade91..423cad99e5 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -2670,6 +2670,15 @@ virDomainHostdevSubsysSCSIClear(virDomainHostdevSubs= ysSCSI *scsisrc) } } =20 +static void +virDomainHostdevSubsysUSBClear(virDomainHostdevSubsysUSB *usbsrc) +{ + if (!usbsrc) + return; + + VIR_FREE(usbsrc->port); +} + =20 static void virDomainHostdevDefClear(virDomainHostdevDef *def) @@ -2713,6 +2722,8 @@ virDomainHostdevDefClear(virDomainHostdevDef *def) g_clear_pointer(&def->source.subsys.u.pci.origstates, virBitma= pFree); break; case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: + virDomainHostdevSubsysUSBClear(&def->source.subsys.u.usb); + break; case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV: case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST: break; @@ -5945,13 +5956,47 @@ virDomainHostdevSubsysUSBDefParseXML(xmlNodePtr nod= e, } =20 if ((addressNode =3D virXPathNode("./address", ctxt))) { - if (virXMLPropUInt(addressNode, "bus", 0, - VIR_XML_PROP_REQUIRED, &usbsrc->bus) < 0) + bool found_device =3D false; + bool found_port =3D false; + char *port =3D NULL; + int ret =3D -1; + + ret =3D virXMLPropUInt(addressNode, "bus", 0, + VIR_XML_PROP_REQUIRED, &usbsrc->bus); + if (ret < 0) { + return -1; + } else if (ret =3D=3D 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "%s", _("missing bus")); return -1; + } =20 - if (virXMLPropUInt(addressNode, "device", 0, - VIR_XML_PROP_REQUIRED, &usbsrc->device) < 0) + ret =3D virXMLPropUInt(addressNode, "device", 0, + VIR_XML_PROP_NONE, &usbsrc->device); + if (ret < 0) return -1; + else if (ret > 0) + found_device =3D true; + + port =3D virXMLPropString(addressNode, "port"); + if (port) { + if (*port) { + usbsrc->port =3D port; + found_port =3D true; + } else { + VIR_FREE(port); + } + } + + if (!found_device && !found_port) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "%s", _("usb address needs either device id or port")); + return -1; + } else if (found_device && found_port) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "%s", _("found both device id and port in usb address (amb= iguous setting)")); + return -1; + } } =20 return 0; @@ -14774,8 +14819,13 @@ virDomainHostdevMatchSubsysUSB(virDomainHostdevDef= *first, virDomainHostdevSubsysUSB *first_usbsrc =3D &first->source.subsys.u.us= b; virDomainHostdevSubsysUSB *second_usbsrc =3D &second->source.subsys.u.= usb; =20 - if (first_usbsrc->bus && first_usbsrc->device) { - /* specified by bus location on host */ + if (first_usbsrc->bus && first_usbsrc->port) { + /* specified by bus and port on host */ + if (first_usbsrc->bus =3D=3D second_usbsrc->bus && + STREQ_NULLABLE(first_usbsrc->port, second_usbsrc->port)) + return 1; + } else if (first_usbsrc->bus && first_usbsrc->device) { + /* specified by bus and device id on host */ if (first_usbsrc->bus =3D=3D second_usbsrc->bus && first_usbsrc->device =3D=3D second_usbsrc->device) return 1; @@ -24345,10 +24395,15 @@ virDomainHostdevDefFormatSubsysUSB(virBuffer *buf, virBufferAsprintf(&sourceChildBuf, "\n", u= sbsrc->product); } =20 - if (usbsrc->bus || usbsrc->device) + if (usbsrc->bus && usbsrc->port) { + virBufferAsprintf(&sourceChildBuf, "
\n", + includeTypeInAddr ? "type=3D'usb' " : "", + usbsrc->bus, usbsrc->port); + } else if (usbsrc->bus || usbsrc->device) { virBufferAsprintf(&sourceChildBuf, "
\n", includeTypeInAddr ? "type=3D'usb' " : "", usbsrc->bus, usbsrc->device); + } =20 virXMLFormatElement(buf, "source", &sourceAttrBuf, &sourceChildBuf); } diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 58b97a2b54..aaa8b8573a 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -228,6 +228,7 @@ struct _virDomainHostdevSubsysUSB { on vendor/product */ unsigned bus; unsigned device; + char *port; =20 unsigned vendor; unsigned product; diff --git a/src/hypervisor/virhostdev.c b/src/hypervisor/virhostdev.c index 0a1d8500d4..9e77477a9b 100644 --- a/src/hypervisor/virhostdev.c +++ b/src/hypervisor/virhostdev.c @@ -1358,6 +1358,42 @@ virHostdevMarkUSBDevices(virHostdevManager *mgr, return -1; } =20 +static int +virHostdevFindUSBDeviceWithFlags(virDomainHostdevDef *hostdev, + bool mandatory, + unsigned int flags, + virUSBDevice **usb) +{ + virDomainHostdevSubsysUSB *usbsrc =3D &hostdev->source.subsys.u.usb; + unsigned vendor =3D usbsrc->vendor; + unsigned product =3D usbsrc->product; + unsigned bus =3D usbsrc->bus; + char *port =3D usbsrc->port; + unsigned device =3D usbsrc->device; + virUSBDeviceList *devs; + int rc; + + rc =3D virUSBDeviceFind(vendor, product, bus, device, port, NULL, + mandatory, flags, &devs); + if (rc < 0) + return -1; + + if (rc =3D=3D 1) { + *usb =3D virUSBDeviceListGet(devs, 0); + virUSBDeviceListSteal(devs, *usb); + } + virObjectUnref(devs); + + if (rc > 1) { + virReportError(VIR_ERR_OPERATION_FAILED, + _("Multiple USB devices for %1$x:%2$x, use to specify one"), + vendor, product); + return -1; + } + + return rc; +} + =20 int virHostdevFindUSBDevice(virDomainHostdevDef *hostdev, @@ -1366,77 +1402,64 @@ virHostdevFindUSBDevice(virDomainHostdevDef *hostde= v, { virDomainHostdevSubsysUSB *usbsrc =3D &hostdev->source.subsys.u.usb; unsigned vendor =3D usbsrc->vendor; - unsigned product =3D usbsrc->product; unsigned bus =3D usbsrc->bus; unsigned device =3D usbsrc->device; + char *port =3D usbsrc->port; bool autoAddress =3D usbsrc->autoAddress; + unsigned int flags =3D 0; int rc; =20 *usb =3D NULL; =20 - if (vendor && bus) { - rc =3D virUSBDeviceFind(vendor, product, bus, device, - NULL, - autoAddress ? false : mandatory, - usb); - if (rc < 0) { - return -1; - } else if (!autoAddress) { - goto out; - } else { - VIR_INFO("USB device %x:%x could not be found at previous" - " address (bus:%u device:%u)", - vendor, product, bus, device); - } + if (vendor) + flags |=3D USB_DEVICE_FIND_BY_VENDOR; + if (device) + flags |=3D USB_DEVICE_FIND_BY_DEVICE; + if (port) + flags |=3D USB_DEVICE_FIND_BY_PORT; + + /* Rule out invalid cases. */ + if (vendor && device && port) { + VIR_WARN("Cannot match USB device on vendor/product, bus/device, a= nd bus/port at once. Ignoring bus/device."); + flags &=3D ~((unsigned int) USB_DEVICE_FIND_BY_DEVICE); + } else if (device && port) { + VIR_WARN("Cannot match USB device on bus/device and bus/port at on= ce. Ignoring bus/device."); + flags &=3D ~((unsigned int) USB_DEVICE_FIND_BY_DEVICE); + } else if (!vendor && !device && !port) { + VIR_WARN("No matching fields for USB device found. Vendor/product,= bus/device, or bus/port required."); + return -1; } =20 - /* When vendor is specified, its USB address is either unspecified or = the - * device could not be found at the USB device where it had been - * automatically found before. - */ - if (vendor) { - g_autoptr(virUSBDeviceList) devs =3D NULL; + /* First attempt, matching on all valid fields. */ + rc =3D virHostdevFindUSBDeviceWithFlags(hostdev, + autoAddress ? false : mandatory, + flags, usb); + if (rc < 0) + return -1; =20 - rc =3D virUSBDeviceFindByVendor(vendor, product, NULL, mandatory, = &devs); - if (rc < 0) { - return -1; - } else if (rc =3D=3D 0) { - goto out; - } else if (rc > 1) { - if (autoAddress) { - virReportError(VIR_ERR_OPERATION_FAILED, - _("Multiple USB devices for %1$x:%2$x were = found, but none of them is at bus:%3$u device:%4$u"), - vendor, product, bus, device); - } else { - virReportError(VIR_ERR_OPERATION_FAILED, - _("Multiple USB devices for %1$x:%2$x, use =
to specify one"), - vendor, product); - } + if (rc !=3D 1 && autoAddress && device) { + VIR_INFO("USB device could not be found at previous address " + "(bus:%u device:%u)", bus, device); + + /* Second attempt, for when the device number has changed. */ + flags &=3D ~((unsigned int) USB_DEVICE_FIND_BY_DEVICE); + usbsrc->device =3D 0; + + rc =3D virHostdevFindUSBDeviceWithFlags(hostdev, mandatory, + flags, usb); + if (rc < 0) return -1; - } =20 - *usb =3D virUSBDeviceListGet(devs, 0); - virUSBDeviceListSteal(devs, *usb); + usbsrc->autoAddress =3D true; + } =20 + if (!*usb) { + hostdev->missing =3D true; + } else if (!usbsrc->bus || !usbsrc->device) { usbsrc->bus =3D virUSBDeviceGetBus(*usb); usbsrc->device =3D virUSBDeviceGetDevno(*usb); - usbsrc->autoAddress =3D true; - - if (autoAddress) { - VIR_INFO("USB device %x:%x found at bus:%u device:%u (moved" - " from bus:%u device:%u)", - vendor, product, - usbsrc->bus, usbsrc->device, - bus, device); - } - } else if (bus) { - if (virUSBDeviceFindByBus(bus, device, NULL, mandatory, usb) < 0) - return -1; } =20 - out: - if (!*usb) - hostdev->missing =3D true; return 0; } =20 diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index a8ebf9efd8..cd7d00674f 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3679,8 +3679,6 @@ virURIResolveAlias; # util/virusb.h virUSBDeviceFileIterate; virUSBDeviceFind; -virUSBDeviceFindByBus; -virUSBDeviceFindByVendor; virUSBDeviceFree; virUSBDeviceGetBus; virUSBDeviceGetDevno; diff --git a/src/util/virusb.c b/src/util/virusb.c index cf3461f80a..8aca190c4c 100644 --- a/src/util/virusb.c +++ b/src/util/virusb.c @@ -61,12 +61,6 @@ struct _virUSBDeviceList { virUSBDevice **devs; }; =20 -typedef enum { - USB_DEVICE_ALL =3D 0, - USB_DEVICE_FIND_BY_VENDOR =3D 1 << 0, - USB_DEVICE_FIND_BY_BUS =3D 1 << 1, -} virUSBDeviceFindFlags; - static virClass *virUSBDeviceListClass; =20 static void virUSBDeviceListDispose(void *obj); @@ -102,11 +96,27 @@ static int virUSBSysReadFile(const char *f_name, const= char *d_name, return 0; } =20 +static int virUSBSysReadFileStr(const char *f_name, const char *d_name, + char **value) +{ + char *buf =3D NULL; + g_autofree char *filename =3D NULL; + + filename =3D g_strdup_printf(USB_SYSFS "/devices/%s/%s", d_name, f_nam= e); + + if (virFileReadAll(filename, 1024, &buf) < 0) + return -1; + + *value =3D buf; + return 0; +} + static virUSBDeviceList * virUSBDeviceSearch(unsigned int vendor, unsigned int product, unsigned int bus, unsigned int devno, + const char *port, const char *vroot, unsigned int flags) { @@ -127,6 +137,8 @@ virUSBDeviceSearch(unsigned int vendor, =20 while ((direrr =3D virDirRead(dir, &de, USB_SYSFS "/devices")) > 0) { unsigned int found_prod, found_vend, found_bus, found_devno; + char *found_port; + bool port_matches; char *tmpstr =3D de->d_name; =20 if (strchr(de->d_name, ':')) @@ -154,16 +166,32 @@ virUSBDeviceSearch(unsigned int vendor, 10, &found_devno) < 0) goto cleanup; =20 - if ((flags & USB_DEVICE_FIND_BY_VENDOR) && - (found_prod !=3D product || found_vend !=3D vendor)) - continue; + if (virUSBSysReadFileStr("devpath", de->d_name, + &found_port) < 0) { + goto cleanup; + } else { + found_port[strlen(found_port) - 1] =3D '\0'; /* remove newline= */ + port_matches =3D STREQ_NULLABLE(found_port, port); + VIR_FREE(found_port); + } =20 - if (flags & USB_DEVICE_FIND_BY_BUS) { + if (flags & USB_DEVICE_FIND_BY_VENDOR) { + if (found_prod !=3D product || found_vend !=3D vendor) + continue; + } + + if (flags & USB_DEVICE_FIND_BY_DEVICE) { if (found_bus !=3D bus || found_devno !=3D devno) continue; found =3D true; } =20 + if (flags & USB_DEVICE_FIND_BY_PORT) { + if (found_bus !=3D bus || !port_matches) + continue; + found =3D true; + } + usb =3D virUSBDeviceNew(found_bus, found_devno, vroot); =20 if (!usb) @@ -186,36 +214,38 @@ virUSBDeviceSearch(unsigned int vendor, } =20 int -virUSBDeviceFindByVendor(unsigned int vendor, - unsigned int product, - const char *vroot, - bool mandatory, - virUSBDeviceList **devices) +virUSBDeviceFind(unsigned int vendor, + unsigned int product, + unsigned int bus, + unsigned int devno, + const char *port, + const char *vroot, + bool mandatory, + unsigned int flags, + virUSBDeviceList **devices) { virUSBDeviceList *list; int count; =20 - if (!(list =3D virUSBDeviceSearch(vendor, product, 0, 0, - vroot, - USB_DEVICE_FIND_BY_VENDOR))) + if (!(list =3D virUSBDeviceSearch(vendor, product, bus, devno, port, + vroot, flags))) return -1; =20 - if (list->count =3D=3D 0) { + count =3D list->count; + if (count =3D=3D 0) { virObjectUnref(list); if (!mandatory) { - VIR_DEBUG("Did not find USB device %04x:%04x", - vendor, product); if (devices) *devices =3D NULL; return 0; } =20 virReportError(VIR_ERR_INTERNAL_ERROR, - _("Did not find USB device %1$04x:%2$04x"), vendor,= product); + _("Did not find matching USB device: vid:%1$04x, pid:%2$04x, b= us:%3$u, device:%4$u, port:%5$s"), + vendor, product, bus, devno, port ? port : ""); return -1; } =20 - count =3D list->count; if (devices) *devices =3D list; else @@ -224,86 +254,6 @@ virUSBDeviceFindByVendor(unsigned int vendor, return count; } =20 -int -virUSBDeviceFindByBus(unsigned int bus, - unsigned int devno, - const char *vroot, - bool mandatory, - virUSBDevice **usb) -{ - virUSBDeviceList *list; - - if (!(list =3D virUSBDeviceSearch(0, 0, bus, devno, - vroot, - USB_DEVICE_FIND_BY_BUS))) - return -1; - - if (list->count =3D=3D 0) { - virObjectUnref(list); - if (!mandatory) { - VIR_DEBUG("Did not find USB device bus:%u device:%u", - bus, devno); - if (usb) - *usb =3D NULL; - return 0; - } - - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Did not find USB device bus:%1$u device:%2$u"), - bus, devno); - return -1; - } - - if (usb) { - *usb =3D virUSBDeviceListGet(list, 0); - virUSBDeviceListSteal(list, *usb); - } - virObjectUnref(list); - - return 0; -} - -int -virUSBDeviceFind(unsigned int vendor, - unsigned int product, - unsigned int bus, - unsigned int devno, - const char *vroot, - bool mandatory, - virUSBDevice **usb) -{ - virUSBDeviceList *list; - - unsigned int flags =3D USB_DEVICE_FIND_BY_VENDOR|USB_DEVICE_FIND_BY_BU= S; - if (!(list =3D virUSBDeviceSearch(vendor, product, bus, devno, - vroot, flags))) - return -1; - - if (list->count =3D=3D 0) { - virObjectUnref(list); - if (!mandatory) { - VIR_DEBUG("Did not find USB device %04x:%04x bus:%u device:%u", - vendor, product, bus, devno); - if (usb) - *usb =3D NULL; - return 0; - } - - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Did not find USB device %1$04x:%2$04x bus:%3$u d= evice:%4$u"), - vendor, product, bus, devno); - return -1; - } - - if (usb) { - *usb =3D virUSBDeviceListGet(list, 0); - virUSBDeviceListSteal(list, *usb); - } - virObjectUnref(list); - - return 0; -} - virUSBDevice * virUSBDeviceNew(unsigned int bus, unsigned int devno, diff --git a/src/util/virusb.h b/src/util/virusb.h index d2b3f69942..cfd1ad51cc 100644 --- a/src/util/virusb.h +++ b/src/util/virusb.h @@ -30,30 +30,26 @@ typedef struct _virUSBDeviceList virUSBDeviceList; =20 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virUSBDeviceList, virObjectUnref); =20 +typedef enum { + USB_DEVICE_ALL =3D 0, + USB_DEVICE_FIND_BY_VENDOR =3D 1 << 0, + USB_DEVICE_FIND_BY_DEVICE =3D 1 << 1, + USB_DEVICE_FIND_BY_PORT =3D 1 << 2, +} virUSBDeviceFindFlags; =20 virUSBDevice *virUSBDeviceNew(unsigned int bus, unsigned int devno, const char *vroot); =20 -int virUSBDeviceFindByBus(unsigned int bus, - unsigned int devno, - const char *vroot, - bool mandatory, - virUSBDevice **usb); - -int virUSBDeviceFindByVendor(unsigned int vendor, - unsigned int product, - const char *vroot, - bool mandatory, - virUSBDeviceList **devices); - int virUSBDeviceFind(unsigned int vendor, - unsigned int product, - unsigned int bus, - unsigned int devno, - const char *vroot, - bool mandatory, - virUSBDevice **usb); + unsigned int product, + unsigned int bus, + unsigned int devno, + const char *port, + const char *vroot, + bool mandatory, + unsigned int flags, + virUSBDeviceList **devices); =20 void virUSBDeviceFree(virUSBDevice *dev); int virUSBDeviceSetUsedBy(virUSBDevice *dev, diff --git a/tests/virusbtest.c b/tests/virusbtest.c index 870e136321..62b7b8bc29 100644 --- a/tests/virusbtest.c +++ b/tests/virusbtest.c @@ -26,9 +26,11 @@ #define VIR_FROM_THIS VIR_FROM_NONE =20 typedef enum { - FIND_BY_ALL, FIND_BY_VENDOR, - FIND_BY_BUS + FIND_BY_DEVICE, + FIND_BY_PORT, + FIND_BY_VENDOR_AND_DEVICE, + FIND_BY_VENDOR_AND_PORT } testUSBFindFlags; =20 struct findTestInfo { @@ -37,6 +39,7 @@ struct findTestInfo { unsigned int product; unsigned int bus; unsigned int devno; + const char *port; const char *vroot; bool mandatory; int how; @@ -70,25 +73,34 @@ static int testDeviceFind(const void *opaque) g_autoptr(virUSBDeviceList) devs =3D NULL; int rv =3D 0; size_t i, ndevs =3D 0; + unsigned int flags =3D 0; =20 switch (info->how) { - case FIND_BY_ALL: - rv =3D virUSBDeviceFind(info->vendor, info->product, - info->bus, info->devno, - info->vroot, info->mandatory, &dev); - break; case FIND_BY_VENDOR: - rv =3D virUSBDeviceFindByVendor(info->vendor, info->product, - info->vroot, info->mandatory, &devs); + flags =3D USB_DEVICE_FIND_BY_VENDOR; + break; + case FIND_BY_DEVICE: + flags =3D USB_DEVICE_FIND_BY_DEVICE; + break; + case FIND_BY_PORT: + flags =3D USB_DEVICE_FIND_BY_PORT; break; - case FIND_BY_BUS: - rv =3D virUSBDeviceFindByBus(info->bus, info->devno, - info->vroot, info->mandatory, &dev); + case FIND_BY_VENDOR_AND_DEVICE: + flags =3D USB_DEVICE_FIND_BY_VENDOR | + USB_DEVICE_FIND_BY_DEVICE; + break; + case FIND_BY_VENDOR_AND_PORT: + flags =3D USB_DEVICE_FIND_BY_VENDOR | + USB_DEVICE_FIND_BY_PORT; break; } =20 + rv =3D virUSBDeviceFind(info->vendor, info->product, + info->bus, info->devno, info->port, + info->vroot, info->mandatory, flags, &devs); + if (info->expectFailure) { - if (rv =3D=3D 0) { + if (rv >=3D 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", "unexpected success"); } else { @@ -99,9 +111,20 @@ static int testDeviceFind(const void *opaque) goto cleanup; } =20 + if (info->how !=3D FIND_BY_VENDOR) { + if (rv =3D=3D 1) { + dev =3D virUSBDeviceListGet(devs, 0); + virUSBDeviceListSteal(devs, dev); + } else { + goto cleanup; + } + } + switch (info->how) { - case FIND_BY_ALL: - case FIND_BY_BUS: + case FIND_BY_DEVICE: + case FIND_BY_PORT: + case FIND_BY_VENDOR_AND_DEVICE: + case FIND_BY_VENDOR_AND_PORT: if (virUSBDeviceFileIterate(dev, testDeviceFileActor, NULL) < 0) goto cleanup; break; @@ -146,14 +169,17 @@ testUSBList(const void *opaque G_GNUC_UNUSED) virUSBDeviceList *list =3D NULL; virUSBDeviceList *devlist =3D NULL; virUSBDevice *dev =3D NULL; + virUSBDeviceList *devs =3D NULL; int ret =3D -1; + int rv; size_t i, ndevs; =20 if (!(list =3D virUSBDeviceListNew())) goto cleanup; =20 #define EXPECTED_NDEVS_ONE 3 - if (virUSBDeviceFindByVendor(0x1d6b, 0x0002, NULL, true, &devlist) < 0) + if (virUSBDeviceFind(0x1d6b, 0x0002, 0, 0, NULL, NULL, true, + USB_DEVICE_FIND_BY_VENDOR, &devlist) < 0) goto cleanup; =20 ndevs =3D virUSBDeviceListCount(devlist); @@ -176,7 +202,8 @@ testUSBList(const void *opaque G_GNUC_UNUSED) goto cleanup; =20 #define EXPECTED_NDEVS_TWO 3 - if (virUSBDeviceFindByVendor(0x18d1, 0x4e22, NULL, true, &devlist) < 0) + if (virUSBDeviceFind(0x18d1, 0x4e22, 0, 0, NULL, NULL, true, + USB_DEVICE_FIND_BY_VENDOR, &devlist) < 0) goto cleanup; =20 ndevs =3D virUSBDeviceListCount(devlist); @@ -196,8 +223,16 @@ testUSBList(const void *opaque G_GNUC_UNUSED) EXPECTED_NDEVS_ONE + EXPECTED_NDEVS_TWO) < 0) goto cleanup; =20 - if (virUSBDeviceFind(0x18d1, 0x4e22, 1, 20, NULL, true, &dev) < 0) + rv =3D virUSBDeviceFind(0x18d1, 0x4e22, 1, 20, NULL, NULL, true, + USB_DEVICE_FIND_BY_VENDOR | + USB_DEVICE_FIND_BY_DEVICE, &devs); + if (rv !=3D 1) { goto cleanup; + } else { + dev =3D virUSBDeviceListGet(devs, 0); + virUSBDeviceListSteal(devs, dev); + } + virObjectUnref(devs); =20 if (!virUSBDeviceListFind(list, dev)) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -229,49 +264,75 @@ mymain(void) { int rv =3D 0; =20 -#define DO_TEST_FIND_FULL(name, vend, prod, bus, devno, vroot, mand, how, = fail) \ +#define DO_TEST_FIND_FULL(name, vend, prod, bus, devno, \ + port, vroot, mand, how, fail) \ do { \ struct findTestInfo data =3D { name, vend, prod, bus, \ - devno, vroot, mand, how, fail \ + devno, port, vroot, mand, how, fail \ }; \ if (virTestRun("USBDeviceFind " name, testDeviceFind, &data) < 0) \ rv =3D -1; \ } while (0) =20 -#define DO_TEST_FIND(name, vend, prod, bus, devno) \ - DO_TEST_FIND_FULL(name, vend, prod, bus, devno, NULL, true, \ - FIND_BY_ALL, false) -#define DO_TEST_FIND_FAIL(name, vend, prod, bus, devno) \ - DO_TEST_FIND_FULL(name, vend, prod, bus, devno, NULL, true, \ - FIND_BY_ALL, true) - -#define DO_TEST_FIND_BY_BUS(name, bus, devno) \ - DO_TEST_FIND_FULL(name, 101, 202, bus, devno, NULL, true, \ - FIND_BY_BUS, false) -#define DO_TEST_FIND_BY_BUS_FAIL(name, bus, devno) \ - DO_TEST_FIND_FULL(name, 101, 202, bus, devno, NULL, true, \ - FIND_BY_BUS, true) - #define DO_TEST_FIND_BY_VENDOR(name, vend, prod) \ - DO_TEST_FIND_FULL(name, vend, prod, 123, 456, NULL, true, \ + DO_TEST_FIND_FULL(name, vend, prod, 123, 456, NULL, NULL, true, \ FIND_BY_VENDOR, false) #define DO_TEST_FIND_BY_VENDOR_FAIL(name, vend, prod) \ - DO_TEST_FIND_FULL(name, vend, prod, 123, 456, NULL, true, \ + DO_TEST_FIND_FULL(name, vend, prod, 123, 456, NULL, NULL, true, \ FIND_BY_VENDOR, true) =20 - DO_TEST_FIND("Nexus", 0x18d1, 0x4e22, 1, 20); - DO_TEST_FIND_FAIL("Nexus wrong devnum", 0x18d1, 0x4e22, 1, 25); - DO_TEST_FIND_FAIL("Bogus", 0xf00d, 0xbeef, 1024, 768); - - DO_TEST_FIND_BY_BUS("integrated camera", 1, 5); - DO_TEST_FIND_BY_BUS_FAIL("wrong bus/devno combination", 2, 20); - DO_TEST_FIND_BY_BUS_FAIL("missing bus", 5, 20); - DO_TEST_FIND_BY_BUS_FAIL("missing devnum", 1, 158); +#define DO_TEST_FIND_BY_DEVICE(name, bus, devno) \ + DO_TEST_FIND_FULL(name, 0x1010, 0x2020, bus, devno, NULL, NULL, true, \ + FIND_BY_DEVICE, false) +#define DO_TEST_FIND_BY_DEVICE_FAIL(name, bus, devno) \ + DO_TEST_FIND_FULL(name, 0x1010, 0x2020, bus, devno, NULL, NULL, true, \ + FIND_BY_DEVICE, true) + +#define DO_TEST_FIND_BY_PORT(name, bus, port) \ + DO_TEST_FIND_FULL(name, 0x1010, 0x2020, bus, 456, port, NULL, true, \ + FIND_BY_PORT, false) +#define DO_TEST_FIND_BY_PORT_FAIL(name, bus, port) \ + DO_TEST_FIND_FULL(name, 0x1010, 0x2020, bus, 456, port, NULL, true, \ + FIND_BY_PORT, true) + +#define DO_TEST_FIND_BY_VENDOR_AND_DEVICE(name, vend, prod, bus, devno) \ + DO_TEST_FIND_FULL(name, vend, prod, bus, devno, NULL, NULL, true, \ + FIND_BY_VENDOR_AND_DEVICE, false) +#define DO_TEST_FIND_BY_VENDOR_AND_DEVICE_FAIL(name, vend, prod, bus, devn= o) \ + DO_TEST_FIND_FULL(name, vend, prod, bus, devno, NULL, NULL, true, \ + FIND_BY_VENDOR_AND_DEVICE, true) + +#define DO_TEST_FIND_BY_VENDOR_AND_PORT(name, vend, prod, bus, port) \ + DO_TEST_FIND_FULL(name, vend, prod, bus, 456, port, NULL, true, \ + FIND_BY_VENDOR_AND_PORT, false) +#define DO_TEST_FIND_BY_VENDOR_AND_PORT_FAIL(name, vend, prod, bus, port) \ + DO_TEST_FIND_FULL(name, vend, prod, bus, 456, port, NULL, true, \ + FIND_BY_VENDOR_AND_PORT, true) + + DO_TEST_FIND_BY_DEVICE("integrated camera", 1, 5); + DO_TEST_FIND_BY_DEVICE_FAIL("wrong bus/devno combination", 2, 20); + DO_TEST_FIND_BY_DEVICE_FAIL("missing bus", 5, 20); + DO_TEST_FIND_BY_DEVICE_FAIL("missing devnum", 1, 158); =20 DO_TEST_FIND_BY_VENDOR("Nexus (multiple results)", 0x18d1, 0x4e22); DO_TEST_FIND_BY_VENDOR_FAIL("Bogus vendor and product", 0xf00d, 0xbeef= ); DO_TEST_FIND_BY_VENDOR_FAIL("Valid vendor", 0x1d6b, 0xbeef); =20 + DO_TEST_FIND_BY_PORT("Logitech mouse", 1, "1.5.3.3"); + DO_TEST_FIND_BY_PORT_FAIL("wrong bus/port combination", 2, "1.5.3.3"); + DO_TEST_FIND_BY_PORT_FAIL("missing bus", 5, "1.5.3.3"); + DO_TEST_FIND_BY_PORT_FAIL("missing port", 1, "8.2.5"); + + DO_TEST_FIND_BY_VENDOR_AND_DEVICE("Nexus", 0x18d1, 0x4e22, 1, 20); + DO_TEST_FIND_BY_VENDOR_AND_DEVICE_FAIL("Bogus vendor and product", 0xf= 00d, 0xbeef, 1, 25); + DO_TEST_FIND_BY_VENDOR_AND_DEVICE_FAIL("Nexus wrong devnum", 0x18d1, 0= x4e22, 1, 25); + DO_TEST_FIND_BY_VENDOR_AND_DEVICE_FAIL("Bogus", 0xf00d, 0xbeef, 1024, = 768); + + DO_TEST_FIND_BY_VENDOR_AND_PORT("Nexus", 0x046d, 0xc069, 1, "1.5.3.3"); + DO_TEST_FIND_BY_VENDOR_AND_PORT_FAIL("Bogus vendor and product", 0xf00= d, 0xbeef, 1, "1.5.3.3"); + DO_TEST_FIND_BY_VENDOR_AND_PORT_FAIL("Nexus wrong port", 0x18d1, 0x4e2= 2, 1, "8.2.5"); + DO_TEST_FIND_BY_VENDOR_AND_PORT_FAIL("Bogus", 0xf00d, 0xbeef, 1024, "1= .1.1.1"); + if (virTestRun("USB List test", testUSBList, NULL) < 0) rv =3D -1; =20 --=20 2.39.5