From owner-freebsd-hackers@freebsd.org Wed May 12 19:24:44 2021 Return-Path: Delivered-To: freebsd-hackers@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 45FC064A1AF for ; Wed, 12 May 2021 19:24:44 +0000 (UTC) (envelope-from hps@selasky.org) Received: from mail.turbocat.net (turbocat.net [IPv6:2a01:4f8:c17:6c4b::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4FgPqv2Nm6z3FmG for ; Wed, 12 May 2021 19:24:42 +0000 (UTC) (envelope-from hps@selasky.org) Received: from hps2020.home.selasky.org (unknown [178.17.145.105]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mail.turbocat.net (Postfix) with ESMTPSA id BBEB2260556; Wed, 12 May 2021 21:24:34 +0200 (CEST) Subject: Re: Indexing problem with address number of a USB device To: Naina Patrascu , freebsd-hackers@freebsd.org, lucian_ioan.popescu@stud.acs.upb.ro, Elena Mihailescu References: <20210512191448.GK14975@funkthat.com> From: Hans Petter Selasky Message-ID: Date: Wed, 12 May 2021 21:23:15 +0200 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:78.0) Gecko/20100101 Thunderbird/78.10.0 MIME-Version: 1.0 In-Reply-To: <20210512191448.GK14975@funkthat.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 4FgPqv2Nm6z3FmG X-Spamd-Bar: - Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=none; spf=pass (mx1.freebsd.org: domain of hps@selasky.org designates 2a01:4f8:c17:6c4b::2 as permitted sender) smtp.mailfrom=hps@selasky.org X-Spamd-Result: default: False [-1.80 / 15.00]; RCVD_TLS_ALL(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; MID_RHS_MATCH_FROM(0.00)[]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_THREE(0.00)[4]; TO_DN_SOME(0.00)[]; R_SPF_ALLOW(-0.20)[+a:mail.turbocat.net]; TAGGED_RCPT(0.00)[]; MIME_GOOD(-0.10)[text/plain]; ARC_NA(0.00)[]; DMARC_NA(0.00)[selasky.org]; SPAMHAUS_ZRD(0.00)[2a01:4f8:c17:6c4b::2:from:127.0.2.255]; TO_MATCH_ENVRCPT_SOME(0.00)[]; RBL_DBL_DONT_QUERY_IPS(0.00)[2a01:4f8:c17:6c4b::2:from]; NEURAL_HAM_LONG(-1.00)[-1.000]; NEURAL_HAM_SHORT(-1.00)[-1.000]; NEURAL_HAM_MEDIUM(-1.00)[-0.999]; FREEMAIL_TO(0.00)[gmail.com,freebsd.org,stud.acs.upb.ro]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; ASN(0.00)[asn:24940, ipnet:2a01:4f8::/32, country:DE]; RCVD_COUNT_TWO(0.00)[2]; MAILMAN_DEST(0.00)[freebsd-hackers]; SUSPICIOUS_RECIPS(1.50)[] X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Technical discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 12 May 2021 19:24:44 -0000 On 5/12/21 9:14 PM, John-Mark Gurney wrote: > Naina Patrascu wrote this message on Tue, May 11, 2021 at 18:47 +0300: >> We are working on the USB passthrough functionality for bhyve. We are >> currently focusing on the passthrough of a USB stick and a USB keyboard. >> >> We are facing an indexing problem when trying to assign a USB device to a >> virtual machine. >> For example, we have the USB keyboard at busnum=1 and devaddr=2 on the >> FreeBSD host virtual machine: >> >> # usbconfig -d ugen1.2 >> ugen1.2: at usbus1, cfg=0 md=HOST spd=LOW (1.5Mbps) >> pwr=ON (100mA) >> >> We specify to "bhyve" command to pass through the device to a guest virtual >> machine: -s 30,xhci,1-2 >> >> As part of the enumeration process, the host controller executes the >> internal scan to discover the physical devices and to compare them with the >> ones assigned with the bhyve command. >> But the USB device is discovered at bus 1 address 1, and we have to adjust >> accordingly when parsing the bus-port to match the port number: >> >> 04f3:152e 1-1 connecting. >> 04f3:152e 1-1 belong to this vm. >> 04F3:152E 1-1 is attached to virtual port 5. >> >> We hardcoded when parsing bus-port: port -= 1. >> When trying to pass through a USB stick, we have to increase the port >> number to match the one discovered in the internal scan: >> >> # usbconfig -d ugen3.2 >> ugen3.2: at usbus3, cfg=0 md=HOST spd=HIGH >> (480Mbps) pwr=ON (300mA) >> >> 0951:1666 3-3 connecting. >> 0951:1666 3-3 belong to this vm. >> 0951:1666 3-3 is attached to virtual port 1. >> >> In our last investigation, we discovered that when increasing the number of >> USB devices on the FreeBSD host vm, the port indexes of the devices >> discovered by the internal scan, also varies. >> >> In function [1] we get the physical devices using libusb library and then >> we execute the internal scan using function [2]. We get the information >> about the physical device in function [3], also using libusb library. >> >> Can you help us understand where this problem could come from and how to >> debug it? > > Yes, the ugen named does not map to port naming. I ran across this when > I did the work to make serial devices consistent based upon the port > number. The review for that work is here: > https://reviews.freebsd.org/D21886#554613 > > The device address is a different enumeration from the port address > information. The script linked in the review creates a mapping from > the device name to the proper port number mapping. > > To see this, you can look at devinfo -v, and look at the port value > vs the devaddr (device address assigned at enumeration). > > Hope this helps. > > Hi, LibUSB in FreeBSD has a helper function to compute the full port topology: libusb_get_port_numbers() and: libusb20_dev_get_port_path() --HPS