hw/usb: Add a usbredir server transport
Subsystems (2)
v2 cover hw/usb: Add a usbredir server transport
This series adds a usbredir server transport to redirect a USB device emulated in one QEMU instance to a USB host controller emulated in another one over the usbredir protocol.
The v1 series included both the usbredir server transport and the ASPEED AST1030 USB Device Controller (UDC) support. Following review feedback, this series contains only the usbredir server transport. The ASPEED AST1030 UDC support will be sent separately.
This work is part of a larger plan to model USB device-side support on ASPEED BMC/BIC SoCs, which has three goals:
1. Model the ASPEED UDC (AST2600 / AST1030). The AST2600 also has USB host (EHCI) controllers, so that work targets the AST2600 UDC: its gadget can be attached to the SoC's own EHCI bus, letting the guest enumerate its own gadget and exercise the UDC end-to-end. [Done]
https://lore.kernel.org/qemu-devel/[email protected]/
2. AST1030 UDC. The AST1030 has no USB host controller, so testing its UDC needs a second QEMU instance. The UDC gadget is redirected out of the guest with libusbredir and attached to another QEMU that runs a USB host (a VMM, or an AST2600 / AST2700 guest). [this series]
3. ASPEED vHub, as a longer-term goal towards BMC KVM / Virtual Media support in QEMU. [future]
This series implements goal 2.
The transport
=============
usb-redir-server exports a locally emulated USB device to a remote USB host over the usbredir protocol, so a device emulated in one QEMU instance can be enumerated by a host controller emulated in another one.
The left column is a request going to the device. The right column is the answer coming back. The middle hop carries usbredir messages over a socket. The top and bottom hops carry USBPackets inside QEMU.
remote QEMU: guest driver -> EHCI/XHCI
| ^
USBPacket | | USBPacket
v |
"usb-redir" (the client)
| ^
usbredir | chardev socket | usbredir
v |
usb-redir-server (the server, this series)
| ^
USBPacket | | USBPacket
v |
any USBDevice, "-device <dev>,bus=<id>.0""usb-redir" (hw/usb/redirect.c) is the client:
- it takes a USBPacket from the remote guest and writes it to the socket as a usbredir message
- it reads the answer from the socket and completes the USBPacket
usb-redir-server is the server. It does the same thing, but backwards:
- it reads a usbredir message from the socket and runs it as a USBPacket on the bus below
- it takes the result of that USBPacket and writes it back to the same socket as a usbredir message, for the client to read
A USB device has to sit on a USB bus, and in QEMU a USB bus is always made by a host controller. So usb-redir-server makes one and acts as the host controller on this side. It models no real chip: its cable is the chardev socket. The real host is in the other QEMU.
usbredir carries one device, not a bus. A hub cannot be exported: the protocol has no device address field. To export several devices, run one usb-redir-server per device, each with its own chardev.
Testing
=======
A plain usb-storage device was used to test the transport. The storage device is attached to usb-redir-server and exported over a Unix socket:
$ qemu-system-aarch64 ... -chardev socket,id=usbredir0,path=/tmp/usbredir0.sock,server=on,wait=off -device usb-redir-server,id=usbredir0,chardev=usbredir0 -drive id=usbdisk,if=none,file=image0.ext4,format=raw -device usb-storage,bus=usbredir0.0,id=mystorage,drive=usbdisk
The exported device can then be attached to EHCI bus 3 of an AST2700 guest using the existing usb-redir client:
-chardev socket,id=storage,path=/tmp/usbredir0.sock,reconnect-ms=1000 \
-device usb-redir,chardev=storage,bus=usb-bus.3The AST2700 Linux guest enumerates the redirected USB storage device:
root@ast2700-default:~# lsusb unable to initialize usb spec Bus 001 Device 001: ID 1d6b:0001 Linux 6.18.36-v00.08.03-gaf2f426c5786 uhci_hcd Generic UHCI Host Controller Bus 002 Device 001: ID 1d6b:0002 Linux 6.18.36-v00.08.03-gaf2f426c5786 ehci_hcd EHCI Host Controller Bus 002 Device 002: ID 46f4:0001 QEMU QEMU USB HARDDRIVE
This also demonstrates that usb-redir-server is not specific to the ASPEED UDC. Any USBDevice can be attached to its USB bus and exported to a host controller in another QEMU instance.
v1:
1. Add a usbredir server device
2. Announce the exported device
3. Implement control transfers
4. Implement bulk and interrupt transfers
5. Stream interrupt IN endpoints
v2:
1. Split the original series into USB and ASPEED parts as suggested during review.
2. Keep this series focused on the usbredir server transport.
3. Move the ASPEED UDC changes, AST1030 UDC support and its functional test to a separate series.
Jamin Lin (8):
hw/usb/bus: Let a bus opt out of automatic hub insertion hw/usb/redirect-server: Add a usbredir server device hw/usb/redirect-server: Connect usbredirparser to a chardev hw/usb/redirect-server: Announce the exported device hw/usb/redirect-server: Implement control transfers hw/usb/redirect-server: Implement bulk and interrupt transfers hw/usb/redirect-server: Stream interrupt IN endpoints hw/arm/aspeed: Enable the usbredir server transport
include/hw/usb/redirect-server.h | 116 +++ include/hw/usb/usb.h | 1 + hw/arm/aspeed.c | 6 + hw/usb/bus.c | 3 +- hw/usb/redirect-server.c | 1604 ++++++++++++++++++++++++++++++ hw/usb/meson.build | 3 +- hw/usb/trace-events | 27 + 7 files changed, 1758 insertions(+), 2 deletions(-) create mode 100644 include/hw/usb/redirect-server.h create mode 100644 hw/usb/redirect-server.c
Signature · 1 line
2.43.0
PATCH v2 1/8 hw/usb/bus: Let a bus opt out of automatic hub insertion
usb_claim_port() inserts a usb-hub when a device is plugged in and only one port is still free, so that more devices can follow. That is what a host controller with root ports wants.
It is wrong for a transport that carries exactly one device. The hub takes the last port, and the device that was being plugged in ends up behind the hub instead of on the port the controller watches.
Add no_auto_hub for such a bus. It keeps the ports it registered, and -device fails with "no free ports" once they are used up.
--- include/hw/usb/usb.h | 1 + hw/usb/bus.c | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-)
include/hw/usb/usb.h +1 -0
@@ -484,6 +484,7 @@ struct USBBus {int busnr;int nfree;int nused; +bool no_auto_hub; QTAILQ_HEAD(, USBPort) free; QTAILQ_HEAD(, USBPort) used;QTAILQ_ENTRY (USBBus) next;
hw/usb/bus.c +2 -1
@@ -413,7 +413,8 @@ void usb_claim_port(USBDevice *dev, Error **errp)return ; } }else { -if (bus->nfree ==1 &&strcmp (object_get_typename (OBJECT (dev)),"usb-hub" ) !=0 ) { +if (bus->nfree ==1 && !bus->no_auto_hub && +strcmp (object_get_typename (OBJECT (dev)),"usb-hub" ) !=0 ) {/* Create a new hub and chain it on */ hub =USB_DEVICE (qdev_try_new ("usb-hub" ));if (hub) {
Signature · 1 line
2.43.0
Re: [PATCH v2 1/8] hw/usb/bus: Let a bus opt out of automatic hub insertion
Quoted 9 lines · Jamin Lin
usb_claim_port()inserts a usb-hub when a device is plugged in and only one port is still free, so that more devices can follow. That is what a host controller with root ports wants.It is wrong for a transport that carries exactly one device. The hub takes the last port, and the device that was being plugged in ends up behind the hub instead of on the port the controller watches.
[...]
Signature · 1 line
Marc-André Lureau <[email protected]>
PATCH v2 2/8 hw/usb/redirect-server: Add a usbredir server device
In reply to Jamin LinAdd "usb-redir-server". It exports a USB device from this QEMU to a USB host in another QEMU, using the usbredir protocol. The other side is the existing "usb-redir" device.
The exported device is plugged in with "-device <dev>,bus=<id>.0". So we have to provide a USB bus. To find that bus, qdev starts at the main system bus and walks down the tree. A device that is not on a bus is not in the tree, so the bus it provides cannot be found. We have to sit on the main system bus. That makes this a sysbus device, even though it has no MMIO and no IRQ.
usbredir carries one device, not a bus. A hub cannot be exported because the protocol has no device address field, so only the device on the first port is redirected. To export several devices, run one usb-redir-server per device, each with its own chardev.
The link to the remote host is a chardev, so this device cannot be migrated. Its VMStateDescription says so with unmigratable.
--- include/hw/usb/redirect-server.h | 27 +++++++ hw/usb/redirect-server.c | 130 +++++++++++++++++++++++++++++++ hw/usb/meson.build | 3 +- 3 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 include/hw/usb/redirect-server.h create mode 100644 hw/usb/redirect-server.c
include/hw/usb/redirect-server.h new file +27 -0
@@ -0,0 +1,27 @@ +/* +* USB redirector, server side +* +* Copyright (c) 2026 ASPEED Technology Inc. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef HW_USB_REDIRECT_SERVER_H +#define HW_USB_REDIRECT_SERVER_H + +#include "hw/core/sysbus.h" +#include "hw/usb/usb.h" +#include "qom/object.h" + +#define TYPE_USB_REDIR_SERVER "usb-redir-server" +OBJECT_DECLARE_SIMPLE_TYPE(USBRedirServer, USB_REDIR_SERVER) + +struct USBRedirServer { +SysBusDevice parent_obj; + +/* USB bus */ +USBBus bus; +USBPort port; +}; + +#endif /* HW_USB_REDIRECT_SERVER_H */
hw/usb/redirect-server.c new file +130 -0
130 lines not on this page · raw message
hw/usb/meson.build +2 -1
@@ -78,7 +78,8 @@ endif if usbredir.found() usbredir_ss = ss.source_set() usbredir_ss.add(when: 'CONFIG_USB', - if_true: [usbredir, files('redirect.c', 'quirks.c')]) + if_true: [usbredir, files('redirect.c', 'quirks.c', + 'redirect-server.c')]) hw_usb_modules += {'redirect': usbredir_ss} endif
Signature · 1 line
2.43.0
Re: [PATCH v2 2/8] hw/usb/redirect-server: Add a usbredir server device
include/hw/usb/redirect-server.h
Quoted 173 lines
Add "usb-redir-server". It exports a USB device from this QEMU to a USB host in another QEMU, using the usbredir protocol. The other side is the existing "usb-redir" device.
The exported device is plugged in with "-device <dev>,bus=<id>.0". So we have to provide a USB bus. To find that bus, qdev starts at the main system bus and walks down the tree. A device that is not on a bus is not in the tree, so the bus it provides cannot be found. We have to sit on the main system bus. That makes this a sysbus device, even though it has no MMIO and no IRQ.
usbredir carries one device, not a bus. A hub cannot be exported because the protocol has no device address field, so only the device on the first port is redirected. To export several devices, run one usb-redir-server per device, each with its own chardev.
The link to the remote host is a chardev, so this device cannot be migrated. Its VMStateDescription says so with unmigratable.
Signed-off-by Jamin Lin <[email protected]>Message-ID: <[email protected]>
hw/usb/meson.build +2
-1@@ -78,7 +78,8 @@ endif if usbredir.found() usbredir_ss = ss.source_set() usbredir_ss.add(when: 'CONFIG_USB', - if_true: [usbredir, files('redirect.c', 'quirks.c')]) + if_true: [usbredir, files('redirect.c', 'quirks.c', + 'redirect-server.c')]) hw_usb_modules += {'redirect': usbredir_ss} endifhw/usb/redirect-server.c new file +130
-0@@ -0,0 +1,130 @@ +/* +* USB redirector, server side +* +* Copyright (c) 2026 ASPEED Technology Inc. +* +* SPDX-License-Identifier: GPL-2.0-or-later +* +* Exports a locally emulated USB device to a remote USB host over the +* usbredir protocol, so a device emulated in one QEMU instance can be +* enumerated by a host controller emulated in another one. +* +* Architecture. The left column is a request going to the device. The +* right column is the answer coming back. The middle hop carries +* usbredir messages over a socket. The top and bottom hops carry +* USBPackets inside QEMU. +* +* remote QEMU: guest driver -> EHCI/XHCI +* | ^ +* USBPacket | | USBPacket +* v | +* "usb-redir" (the client) +* | ^ +* usbredir | chardev socket | usbredir +* v | +* usb-redir-server (the server, this file) +* | ^ +* USBPacket | | USBPacket +* v | +* any USBDevice, "-device <dev>,bus=<id>.0" +* +* "usb-redir" (hw/usb/redirect.c) is the client: +* - it takes a USBPacket from the remote guest and writes it to the +* socket as a usbredir message +* - it reads the answer from the socket and completes the USBPacket +* +* This file is the server. It does the same thing, but backwards: +* - it reads a usbredir message from the socket and runs it as a +* USBPacket on the bus below +* - it takes the result of that USBPacket and writes it back to the +* same socket as a usbredir message, for the client to read +* +* A USB device has to sit on a USB bus, and in QEMU a USB bus is always +* made by a host controller. So this file makes one and acts as the +* host controller on this side. It models no real chip: its cable is +* the chardev socket. The real host is in the other QEMU. +* +* usbredir carries one device, not a bus. A hub cannot be exported: +* the protocol has no device address field. To export several devices, +* run one usb-redir-server per device, each with its own chardev. +*/ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qemu/module.h" +#include "migration/vmstate.h" +#include "hw/usb/redirect-server.h" + +/* +* USB port ops +*/ + +static void usbredir_server_port_child_detach (USBPort *port,USBDevice *child) +{ +/* We only export the device on our own port. Nothing to do. */ +} + +static void usbredir_server_port_wakeup (USBPort *port) +{ +/* We do not pass remote wakeup to the host. Nothing to do. */ +} + +static USBPortOps usbredir_server_port_ops = { + .child_detach = usbredir_server_port_child_detach, + .wakeup = usbredir_server_port_wakeup, +}; + +/* +* USB bus ops +*/ + +static USBBusOps usbredir_server_bus_ops = { +}; + +/* +* Device registration +*/ + +static void usbredir_server_realize (DeviceState *dev,Error **errp) +{ +USBRedirServer *s =USB_REDIR_SERVER (dev); + +/* One port: usbredir carries a single device. */ +usb_bus_new (&s->bus,sizeof (s->bus), &usbredir_server_bus_ops, dev); + s->bus.no_auto_hub = true; +usb_register_port (&s->bus, &s->port, s,0 , &usbredir_server_port_ops, +USB_SPEED_MASK_LOW |USB_SPEED_MASK_FULL | +USB_SPEED_MASK_HIGH ); +} + +/* +* The link to the remote host is a chardev, and we cannot migrate +* that. So this device cannot be migrated either. +*/ +static const VMStateDescription vmstate_usbredir_server = { + .name =TYPE_USB_REDIR_SERVER , + .unmigratable =1 , +}; + +static void usbredir_server_class_init (ObjectClass *klass,const void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); + + dc->desc ="USB Redirection Server" ; + dc->realize = usbredir_server_realize; + dc->vmsd = &vmstate_usbredir_server; +set_bit (DEVICE_CATEGORY_USB , dc->categories); +} + +static const TypeInfo usbredir_server_types[] = { + { + .name =TYPE_USB_REDIR_SERVER , + .parent =TYPE_DYNAMIC_SYS_BUS_DEVICE , + .instance_size =sizeof (USBRedirServer), + .class_init = usbredir_server_class_init, + }, +}; +module_obj (TYPE_USB_REDIR_SERVER ); +module_kconfig (USB ); + +DEFINE_TYPES (usbredir_server_types)include/hw/usb/redirect-server.h +0
-0
What do you need a header for? If there is nothing to share with other units, better keep it all in .c.
Signature · 1 line
Marc-André Lureau <[email protected]>
RE: [PATCH v2 2/8] hw/usb/redirect-server: Add a usbredir server device
Hi Marc-André Lureau
Quoted 8 lines
include/hw/usb/redirect-server.h
Quoted 2 lines
include/hw/usb/redirect-server.h +0
-0What do you need a header for? If there is nothing to share with other units, better keep it all in .c.
--
Marc-André Lureau <[email protected]>
Thanks for your suggestion and review.
Nothing else includes it. The main reason for having a separate header file is readability. redirect-server.c is already about 1600 lines, so I thought keeping the state structure and constants in a separate file would make the code easier to follow.
Would hw/usb/redirect-server.h be OK instead?
If you still prefer to keep everything in the .c file, I will move it there for v3.
Thanks,
Jamin
Re: [PATCH v2 2/8] hw/usb/redirect-server: Add a usbredir server device
Hi
Quoted 12 lines · Jamin Lin
Hi Marc-André Lureau
Quoted 8 lines
include/hw/usb/redirect-server.h
Quoted 2 lines
include/hw/usb/redirect-server.h +0
-0What do you need a header for? If there is nothing to share with other units, better keep it all in .c.
--
Marc-André Lureau <[email protected]>
Quoted 7 lines
Thanks for your suggestion and review.
Nothing else includes it. The main reason for having a separate header file is readability.
redirect-server.cis already about 1600 lines, so I thought keeping the state structure and constants in a separate file would make the code easier to follow.Would
hw/usb/redirect-server.hbe OK instead?
Yes - I don't think we have strict rules about headers
Quoted 1 line
If you still prefer to keep everything in the .c file, I will move it there for v3.
That would be "my" preference. I don't mind large files as long as they don't mix various concerns/domains/levels.
RE: [PATCH v2 2/8] hw/usb/redirect-server: Add a usbredir server device
Hi Marc-André Lureau
Quoted 27 lines
Subject: Re: [PATCH v2 2/8] hw/usb/redirect-server: Add a usbredir server device
Hi
Quoted 9 lines · Jamin Lin
Hi Marc-André Lureau
Quoted 6 lines
include/hw/usb/redirect-server.h
Quoted 2 lines
include/hw/usb/redirect-server.h +0
-0What do you need a header for? If there is nothing to share with other units, better keep it all in .c.
Quoted 6 lines
Quoted 2 lines
--
Marc-André Lureau <[email protected]>Thanks for your suggestion and review.
Nothing else includes it. The main reason for having a separate header file is
readability.
Quoted 2 lines
redirect-server.cis already about 1600 lines, so I thought keeping the state structure and constants in a separate file would make the codeeasier to follow.
Quoted 1 line
Quoted 8 lines
Quoted 1 line
Would
hw/usb/redirect-server.hbe OK instead?Yes - I don't think we have strict rules about headers
Quoted 1 line
If you still prefer to keep everything in the .c file, I will move it there for v3.
That would be "my" preference. I don't mind large files as long as they don't mix various concerns/domains/levels.
Thanks for the review and suggestion.
Will do.
Jamin
PATCH v2 3/8 hw/usb/redirect-server: Connect usbredirparser to a chardev
In reply to Jamin LinCreate a usbredirparser when the chardev opens. Feed it from the chardev read handler. Destroy it when the chardev closes.
The side that exports a device sets usbredirparser_fl_usb_host. This side drives the device, so it is the USB host for it. redirect.c is the other side and does not set the flag. The library uses it to decide which direction a packet may travel.
The chardev handling follows hw/usb/redirect.c: take no data before the parser exists, do not write to a closed backend, do not re-enter the writer, and let a close that is still queued finish before a new connection opens.
--- include/hw/usb/redirect-server.h | 15 ++ hw/usb/redirect-server.c | 268 +++++++++++++++++++++++++++++++ hw/usb/trace-events | 6 + 3 files changed, 289 insertions(+)
include/hw/usb/redirect-server.h +15 -0
@@ -11,8 +11,11 @@#include "hw/core/sysbus.h" #include "hw/usb/usb.h" +#include "chardev/char-fe.h" #include "qom/object.h" +#include <usbredirparser.h> +#define TYPE_USB_REDIR_SERVER "usb-redir-server"OBJECT_DECLARE_SIMPLE_TYPE (USBRedirServer,USB_REDIR_SERVER )
@@ -22,6 +25,18 @@ struct USBRedirServer {/* USB bus */ USBBus bus;USBPort port; + +/* Properties */ +CharFrontend cs; + +/* usbredir over the chardev */ +struct usbredirparser *parser; +QEMUBH *chardev_close_bh; +const uint8_t *read_buf; +int read_buf_size; +bool in_write; +guint watch; + bool host_connected; };#endif /* HW_USB_REDIRECT_SERVER_H */
hw/usb/redirect-server.c +268 -0
306 lines not on this page · raw message
hw/usb/trace-events +6 -0
@@ -393,3 +393,9 @@ aspeed_udc_handle_data(int ep_nr, const char *dir, uint32_t iov, int ep_idx) "ep aspeed_udc_ep_data_in(unsigned ep, uint32_t rptr, uint32_t wptr, uint32_t iov) "ep %u, rptr %u, wptr %u, iov %u" aspeed_udc_ep_data_out(unsigned ep, uint32_t wptr, uint32_t avail, uint32_t iov) "ep %u, wptr %u, avail %u, iov %u" aspeed_udc_ep_ack(unsigned ep) "ep %u" + +# redirect-server.c +usbredir_server_chardev_open(void) "chardev opened" +usbredir_server_chardev_close(void) "chardev closed" +usbredir_server_write_recursion(void) "recursive write, leaving it queued" +usbredir_server_log(const char *msg) "%s"
Signature · 1 line
2.43.0
Re: [PATCH v2 3/8] hw/usb/redirect-server: Connect usbredirparser to a chardev
Quoted 9 lines · Jamin Lin
Create a usbredirparser when the chardev opens. Feed it from the chardev read handler. Destroy it when the chardev closes.
The side that exports a device sets
usbredirparser_fl_usb_host. This side drives the device, so it is the USB host for it.redirect.cis the other side and does not set the flag. The library uses it to decide which direction a packet may travel.[...]
Signature · 1 line
Marc-André Lureau <[email protected]>
PATCH v2 4/8 hw/usb/redirect-server: Announce the exported device
In reply to Jamin LinSend device_connect when a device is attached and the host has greeted us, device_disconnect when it goes away, and handle the bus reset the host sends before enumerating.
Wait a short time before sending device_connect. Firmware often toggles the USB pull-up a few times while it starts its controller. If the host starts to enumerate while the device is off, we cannot answer its first request, and it only retries after a control timeout of several seconds.
device_connect carries no identity. The peer asks the device itself with GET_DESCRIPTOR as soon as it enumerates, so anything we put there would be made up. The speed is real: it comes from the device on our port.
--- include/hw/usb/redirect-server.h | 2 + hw/usb/redirect-server.c | 166 ++++++++++++++++++++++++++++++- hw/usb/trace-events | 7 ++ 3 files changed, 174 insertions(+), 1 deletion(-)
include/hw/usb/redirect-server.h +2 -0
@@ -31,12 +31,14 @@ struct USBRedirServer {/* usbredir over the chardev */ struct usbredirparser *parser; +QEMUTimer *announce_timer;QEMUBH *chardev_close_bh;const uint8_t *read_buf;int read_buf_size;bool in_write;guint watch;bool host_connected; + bool device_announced; };#endif /* HW_USB_REDIRECT_SERVER_H */
hw/usb/redirect-server.c +165 -1
225 lines not on this page · raw message
hw/usb/trace-events +7 -0
@@ -399,3 +399,10 @@ usbredir_server_chardev_open(void) "chardev opened" usbredir_server_chardev_close(void) "chardev closed" usbredir_server_write_recursion(void) "recursive write, leaving it queued" usbredir_server_log(const char *msg) "%s" +usbredir_server_hello(const char *version) "peer is %s" +usbredir_server_attach(void) "device attached" +usbredir_server_detach(bool announced) "device detached, was announced %d" +usbredir_server_announce(uint8_t speed) "device_connect speed %u" +usbredir_server_disconnect(void) "device_disconnect sent" +usbredir_server_bus_reset(bool attached) "bus reset, attached %d" +usbredir_server_filter_reject(void) "host rejected our device"
Signature · 1 line
2.43.0
Re: [PATCH v2 4/8] hw/usb/redirect-server: Announce the exported device
Quoted 8 lines
Send
device_connectwhen a device is attached and the host has greeted us,device_disconnectwhen it goes away, and handle the bus reset the host sends before enumerating.Wait a short time before sending
device_connect. Firmware often toggles the USB pull-up a few times while it starts its controller. If the host starts to enumerate while the device is off, we cannot answer its first request, and it only retries after a control timeout of several seconds.
sigh, this is going to hit us as machine may behave differently. I think we should make the redirect-server machine-agnostic then.
Signature · 1 line
Marc-André Lureau <[email protected]>
RE: [PATCH v2 4/8] hw/usb/redirect-server: Announce the exported device
Hi Marc-André Lureau
Quoted 7 lines
Subject: Re: [PATCH v2 4/8] hw/usb/redirect-server: Announce the exported device
Quoted 4 lines
Send
device_connectwhen a device is attached and the host has greeted us,device_disconnectwhen it goes away, and handle the bus reset the host sends before enumerating.
Quoted 8 lines
Quoted 4 lines
Wait a short time before sending
device_connect. Firmware often toggles the USB pull-up a few times while it starts its controller. If the host starts to enumerate while the device is off, we cannot answer its first request, and it only retries after a control timeout of several seconds.sigh, this is going to hit us as machine may behave differently. I think we should make the redirect-server machine-agnostic then.
Thanks for the review and suggestion.
I added the 10 ms timer to avoid announcing the device before the firmware had finished its USB initialization.
I went back and measured it. On both gadgets I tested, the port attaches only once and does not detach during initialization:
Zephyr on AST1030, "usb enable": 1 attach, 0 detach
Linux on AST2600, gadget bind: 1 attach, 0 detach
So the delay is not needed for these cases.
I will drop the announce timer. When the device is attached, the redirect server will send device_connect immediately. This keeps the redirect server independent of machine-specific timing.
Thanks,
Jamin
Quoted 2 lines
--
Marc-André Lureau <[email protected]>
PATCH v2 5/8 hw/usb/redirect-server: Implement control transfers
In reply to Jamin LinThe host sends a control_packet. Turn it into the SETUP, DATA and STATUS tokens the QEMU USB core wants, run them on the device, and send the answer back.
The device may answer later, so we have to remember the packets we sent. Each one stays on a list until it comes back. We drop the whole list on a bus reset or a chardev close. When the host asks us to cancel a packet, we answer it and drop it.
usbredir has its own messages for set_configuration, get_configuration, set_alt_setting and get_alt_setting. Each one is a control transfer, so run it like one and send back the status message.
usbredir wants an ep_info message. It says what kind each endpoint is:
bulk, interrupt or control. QEMU cannot tell us. It only knows after it has read the device's descriptors, and it never reads them for a device that just forwards transfers. So we read them ourselves: the configuration descriptor passes through us on its way to the host.
--- include/hw/usb/redirect-server.h | 46 +++ hw/usb/redirect-server.c | 603 ++++++++++++++++++++++++++++++- hw/usb/trace-events | 5 + 3 files changed, 653 insertions(+), 1 deletion(-)
include/hw/usb/redirect-server.h +46 -0
@@ -15,10 +15,46 @@#include "qom/object.h" #include <usbredirparser.h> +#include <usbredirproto.h> #define TYPE_USB_REDIR_SERVER "usb-redir-server"OBJECT_DECLARE_SIMPLE_TYPE (USBRedirServer,USB_REDIR_SERVER ) +/* +* The usbredir ep_info message has one slot per endpoint and direction. +* Index 0-15 are the OUT endpoints, 16-31 the IN ones. + */ +#define USBREDIR_SERVER_MAX_EP 32 +#define USBREDIR_SERVER_EP_IN_BASE 16 + +#define USBREDIR_SERVER_CTRL_SETUP 0 +#define USBREDIR_SERVER_CTRL_STATUS 1 + +/* Which message answers the host when a control transfer ends. */ +typedef enum { +USBREDIR_SERVER_REPLY_CONTROL , +USBREDIR_SERVER_REPLY_CONFIG , +USBREDIR_SERVER_REPLY_ALT , +}USBRedirServerReply ; + +typedef struct USBRedirServerPkt { +USBPacket pkt; + +/* Saved headers for the usbredir response */ +struct usb_redir_control_packet_header ctrl_hdr; + +/* +* The IOV points here. IN data lands in it, OUT data is copied in. +* Allocated and freed with the packet; data_size is how big it is. +*/ +uint8_t *data; +int data_size; +QTAILQ_ENTRY (USBRedirServerPkt) next; +USBRedirServerReply reply; +uint64_t redir_id; +int type; +}USBRedirServerPkt ; +struct USBRedirServer {SysBusDevice parent_obj;
@@ -39,6 +75,16 @@ struct USBRedirServer {guint watch;bool host_connected;bool device_announced; + +/* In-flight packet tracking */ + QTAILQ_HEAD(, USBRedirServerPkt) inflight; +uint64_t next_id; + +/* Endpoint tables for the usbredir ep_info message. */ +uint8_t ep_type[USBREDIR_SERVER_MAX_EP ]; +uint16_t ep_max_packet[USBREDIR_SERVER_MAX_EP ]; +uint8_t ep_interval[USBREDIR_SERVER_MAX_EP ]; + uint8_t ep_iface[USBREDIR_SERVER_MAX_EP]; };#endif /* HW_USB_REDIRECT_SERVER_H */
hw/usb/redirect-server.c +602 -1
697 lines not on this page · raw message
hw/usb/trace-events +5 -0
@@ -406,3 +406,8 @@ usbredir_server_announce(uint8_t speed) "device_connect speed %u" usbredir_server_disconnect(void) "device_disconnect sent" usbredir_server_bus_reset(bool attached) "bus reset, attached %d" usbredir_server_filter_reject(void) "host rejected our device" +usbredir_server_cancel(uint64_t id, bool found) "id %" PRIu64 " still in flight %d" +usbredir_server_ep_info(unsigned idx, uint8_t type, uint16_t mps, uint8_t interval, uint8_t iface) "ep_info[%u] type %u mps %u interval %u iface %u" +usbredir_server_control(uint64_t id, uint8_t requesttype, uint8_t request, uint16_t value, uint16_t index, uint16_t length) "id %" PRIu64 " type 0x%02x request 0x%02x value 0x%04x index 0x%04x length %u" +usbredir_server_ctrl_setup_complete(uint64_t id, int status, int actual) "id %" PRIu64 " status %d actual %d" +usbredir_server_ctrl_status_complete(uint64_t id, int status) "id %" PRIu64 " status %d"
Signature · 1 line
2.43.0
PATCH v2 6/8 hw/usb/redirect-server: Implement bulk and interrupt transfers
In reply to Jamin LinSend bulk and interrupt packets to the device and return the result to the host. Each one is a request and an answer, so they reuse the packet tracking added for control transfers.
A bulk request carries a 32 bit length, so the host can ask us to allocate up to 4 GB. Refuse a request that big. Do not answer it with fewer bytes instead: the host counts the bytes it gets back, and it resets the device when some are missing.
--- include/hw/usb/redirect-server.h | 11 ++ hw/usb/redirect-server.c | 193 +++++++++++++++++++++++++++++++ hw/usb/trace-events | 5 + 3 files changed, 209 insertions(+)
include/hw/usb/redirect-server.h +11 -0
@@ -9,6 +9,7 @@#ifndef HW_USB_REDIRECT_SERVER_H #define HW_USB_REDIRECT_SERVER_H +#include "qemu/units.h" #include "hw/core/sysbus.h" #include "hw/usb/usb.h" #include "chardev/char-fe.h"
@@ -27,8 +28,16 @@ OBJECT_DECLARE_SIMPLE_TYPE(USBRedirServer, USB_REDIR_SERVER)#define USBREDIR_SERVER_MAX_EP 32#define USBREDIR_SERVER_EP_IN_BASE 16 +/* +* The bulk length field is 32 bits, so the host can ask for up to 4 GB. +* This is the largest transfer accepted. +*/ +#define USBREDIR_SERVER_MAX_BULK (1 * MiB) +#define USBREDIR_SERVER_CTRL_SETUP 0#define USBREDIR_SERVER_CTRL_STATUS 1 +#define USBREDIR_SERVER_BULK 2 +#define USBREDIR_SERVER_INTR 3/* Which message answers the host when a control transfer ends. */ typedef enum {
@@ -41,7 +50,9 @@ typedef struct USBRedirServerPkt {USBPacket pkt;/* Saved headers for the usbredir response */ +struct usb_redir_interrupt_packet_header intr_hdr;struct usb_redir_control_packet_header ctrl_hdr; +struct usb_redir_bulk_packet_header bulk_hdr;/* * The IOV points here. IN data lands in it, OUT data is copied in.
hw/usb/redirect-server.c +193 -0
243 lines not on this page · raw message
hw/usb/trace-events +5 -0
@@ -411,3 +411,8 @@ usbredir_server_ep_info(unsigned idx, uint8_t type, uint16_t mps, uint8_t interv usbredir_server_control(uint64_t id, uint8_t requesttype, uint8_t request, uint16_t value, uint16_t index, uint16_t length) "id %" PRIu64 " type 0x%02x request 0x%02x value 0x%04x index 0x%04x length %u" usbredir_server_ctrl_setup_complete(uint64_t id, int status, int actual) "id %" PRIu64 " status %d actual %d" usbredir_server_ctrl_status_complete(uint64_t id, int status) "id %" PRIu64 " status %d" +usbredir_server_bulk(uint64_t id, uint8_t ep, size_t len) "id %" PRIu64 " ep 0x%02x len %zu" +usbredir_server_bulk_complete(uint64_t id, uint8_t ep, int status, int actual) "id %" PRIu64 " ep 0x%02x status %d actual %d" +usbredir_server_bulk_too_big(uint64_t id, uint8_t ep, uint32_t len) "id %" PRIu64 " ep 0x%02x len %u" +usbredir_server_interrupt(uint64_t id, uint8_t ep, size_t len) "id %" PRIu64 " ep 0x%02x len %zu" +usbredir_server_intr_complete(uint64_t id, uint8_t ep, int status, int actual) "id %" PRIu64 " ep 0x%02x status %d actual %d"
Signature · 1 line
2.43.0
PATCH v2 7/8 hw/usb/redirect-server: Stream interrupt IN endpoints
In reply to Jamin LinInterrupt IN is not a request and an answer in usbredir. The host asks once with start_interrupt_receiving. After that it expects a packet every time the device has data.
Park one IN packet on the device for each streaming endpoint. A device that answers USB_RET_ASYNC holds the packet and completes it when it has data. A device that answers USB_RET_NAK gives the packet back at once, so park a new one when the bus reports a wakeup on that endpoint. A slow timer parks one too, for a device that does not report a wakeup.
--- include/hw/usb/redirect-server.h | 15 ++ hw/usb/redirect-server.c | 248 +++++++++++++++++++++++++++++++ hw/usb/trace-events | 4 + 3 files changed, 267 insertions(+)
include/hw/usb/redirect-server.h +15 -0
@@ -28,16 +28,23 @@ OBJECT_DECLARE_SIMPLE_TYPE(USBRedirServer, USB_REDIR_SERVER)#define USBREDIR_SERVER_MAX_EP 32#define USBREDIR_SERVER_EP_IN_BASE 16 +/* An endpoint number is 4 bits, so 0 to 15. */ +#define USBREDIR_SERVER_MAX_EP_NR 16 +/* * The bulk length field is 32 bits, so the host can ask for up to 4 GB. * This is the largest transfer accepted. */ #define USBREDIR_SERVER_MAX_BULK (1 * MiB) +/* Buffer size for an interrupt IN endpoint before its descriptor is seen. */ +#define USBREDIR_SERVER_INTR_DEFAULT_LEN 64 +#define USBREDIR_SERVER_CTRL_SETUP 0#define USBREDIR_SERVER_CTRL_STATUS 1#define USBREDIR_SERVER_BULK 2#define USBREDIR_SERVER_INTR 3 +#define USBREDIR_SERVER_INTR_STREAM 4/* Which message answers the host when a control transfer ends. */ typedef enum {
@@ -87,6 +94,14 @@ struct USBRedirServer {bool host_connected;bool device_announced; +/* +* Interrupt IN streaming, indexed by endpoint number. intr_bh asks the +* device again; intr_retry does the same after a delay on NAK. +*/ +bool intr_in_started[USBREDIR_SERVER_MAX_EP_NR ]; +QEMUBH *intr_bh; +QEMUTimer *intr_retry; +/* In-flight packet tracking */ QTAILQ_HEAD(, USBRedirServerPkt) inflight;uint64_t next_id;
hw/usb/redirect-server.c +248 -0
309 lines not on this page · raw message
hw/usb/trace-events +4 -0
@@ -416,3 +416,7 @@ usbredir_server_bulk_complete(uint64_t id, uint8_t ep, int status, int actual) " usbredir_server_bulk_too_big(uint64_t id, uint8_t ep, uint32_t len) "id %" PRIu64 " ep 0x%02x len %u" usbredir_server_interrupt(uint64_t id, uint8_t ep, size_t len) "id %" PRIu64 " ep 0x%02x len %zu" usbredir_server_intr_complete(uint64_t id, uint8_t ep, int status, int actual) "id %" PRIu64 " ep 0x%02x status %d actual %d" +usbredir_server_intr_start(uint8_t ep, int status) "start_interrupt_receiving ep 0x%02x -> status %d" +usbredir_server_intr_stop(uint8_t ep) "stop_interrupt_receiving ep 0x%02x" +usbredir_server_intr_park(unsigned ep_nr, int len) "parked IN packet ep %u len %d" +usbredir_server_intr_stream_complete(unsigned ep_nr, int status, int actual) "ep %u status %d actual %d"
Signature · 1 line
2.43.0
PATCH v2 8/8 hw/arm/aspeed: Enable the usbredir server transport
In reply to Jamin LinLet the ASPEED machines accept "usb-redir-server" with -device, so a UDC gadget can be exported to a USB host running in another QEMU:
-device usb-redir-server,id=redirsrv,chardev=<chr> -device aspeed.udc-gadget,bus=redirsrv.0,udc=/machine/soc/udc
usb-redir-server sits on the main system bus so that the USB bus it provides can be found by "bus=<id>.0". That makes it a sysbus device, and a sysbus device can only be created with -device if the machine lists it.
--- hw/arm/aspeed.c | 6 ++++++ 1 file changed, 6 insertions(+)
hw/arm/aspeed.c +6 -0
@@ -409,6 +409,12 @@ static void aspeed_machine_class_init(ObjectClass *oc, const void *data) mc->no_cdrom =1 ; mc->no_parallel =1 ; mc->default_ram_id ="ram" ; +/* +* Lets a UDC gadget be exported to another QEMU instance over usbredir. +* The type name is spelled out rather than used through its macro so +* that this still builds when the usbredir library is absent. +*/ +machine_class_allow_dynamic_sysbus_dev (mc,"usb-redir-server" ); amc->macs_mask =ASPEED_MAC0_ON ; amc->uart_default =ASPEED_DEV_UART5 ;
Signature · 1 line
2.43.0
Hi
Quoted 58 lines
This series adds a usbredir server transport to redirect a USB device emulated in one QEMU instance to a USB host controller emulated in another one over the usbredir protocol.
The v1 series included both the usbredir server transport and the ASPEED AST1030 USB Device Controller (UDC) support. Following review feedback, this series contains only the usbredir server transport. The ASPEED AST1030 UDC support will be sent separately.
This work is part of a larger plan to model USB device-side support on ASPEED BMC/BIC SoCs, which has three goals:
1. Model the ASPEED UDC (AST2600 / AST1030). The AST2600 also has USB host (EHCI) controllers, so that work targets the AST2600 UDC: its gadget can be attached to the SoC's own EHCI bus, letting the guest enumerate its own gadget and exercise the UDC end-to-end. [Done]
https://lore.kernel.org/qemu-devel/[email protected]/2. AST1030 UDC. The AST1030 has no USB host controller, so testing its UDC needs a second QEMU instance. The UDC gadget is redirected out of the guest with libusbredir and attached to another QEMU that runs a USB host (a VMM, or an AST2600 / AST2700 guest). [this series]
3. ASPEED vHub, as a longer-term goal towards BMC KVM / Virtual Media support in QEMU. [future]
This series implements goal 2.
The transport
=============usb-redir-server exports a locally emulated USB device to a remote USB host over the usbredir protocol, so a device emulated in one QEMU instance can be enumerated by a host controller emulated in another one.
The left column is a request going to the device. The right column is the answer coming back. The middle hop carries usbredir messages over a socket. The top and bottom hops carry USBPackets inside QEMU.
remote QEMU: guest driver -> EHCI/XHCI | ^ USBPacket | | USBPacket v | "usb-redir" (the client) | ^ usbredir | chardev socket | usbredir v | usb-redir-server (the server, this series) | ^ USBPacket | | USBPacket v | any USBDevice, "-device <dev>,bus=<id>.0""usb-redir" (
hw/usb/redirect.c) is the client:
- it takes a USBPacket from the remote guest and writes it to the socket as a usbredir message
- it reads the answer from the socket and completes the USBPacket
Quoted 8 lines
usb-redir-server is the server. It does the same thing, but backwards:
- it reads a usbredir message from the socket and runs it as a USBPacket on the bus below
- it takes the result of that USBPacket and writes it back to the same socket as a usbredir message, for the client to readA USB device has to sit on a USB bus, and in QEMU a USB bus is always made by a host controller. So usb-redir-server makes one and acts as the host
That's a bit awkward, but makes sense. However I am not sure it sure it should also require a machine and sit on the sysbus. We may also want to build a specialized binary that doesn't cary any of the machine code etc and is target-free. something like "qemu-usb", not necessarily with this series though
Patch 4 already works around firmware initialization.. I don't whether this is acceptable.. Perhaps we can accept it for now, but I'd mark the device "experimental" at this point.
Quoted 32 lines
controller on this side. It models no real chip: its cable is the chardev socket. The real host is in the other QEMU.
usbredir carries one device, not a bus. A hub cannot be exported: the protocol has no device address field. To export several devices, run one usb-redir-server per device, each with its own chardev.
Testing
=======
A plain usb-storage device was used to test the transport. The storage device is attached to usb-redir-server and exported over a Unix socket:$ qemu-system-aarch64 ...(quoted diff) +0
-4-chardev socket,id=usbredir0,path=/tmp/usbredir0.sock,server=on,wait=off -device usb-redir-server,id=usbredir0,chardev=usbredir0 -drive id=usbdisk,if=none,file=image0.ext4,format=raw -device usb-storage,bus=usbredir0.0,id=mystorage,drive=usbdiskThe exported device can then be attached to EHCI bus 3 of an AST2700 guest using the existing usb-redir client:
-chardev socket,id=storage,path=/tmp/usbredir0.sock,reconnect-ms=1000 \ -device usb-redir,chardev=storage,bus=usb-bus.3The AST2700 Linux guest enumerates the redirected USB storage device:
root@ast2700-default:~# lsusb unable to initialize usb spec Bus 001 Device 001: ID 1d6b:0001 Linux 6.18.36-v00.08.03-gaf2f426c5786 uhci_hcd Generic UHCI Host Controller Bus 002 Device 001: ID 1d6b:0002 Linux 6.18.36-v00.08.03-gaf2f426c5786 ehci_hcd EHCI Host Controller Bus 002 Device 002: ID 46f4:0001 QEMU QEMU USB HARDDRIVE
Quoted 3 lines
This also demonstrates that usb-redir-server is not specific to the ASPEED UDC. Any USBDevice can be attached to its USB bus and exported to a host controller in another QEMU instance.
something I have long wished for, pretty nice
Signature · 1 line
Marc-André Lureau <[email protected]>
Hi Marc-André Lureau
Quoted 74 lines
Subject: Re: [PATCH v2 0/8] hw/usb: Add a usbredir server transport
Hi
Quoted 11 lines
This series adds a usbredir server transport to redirect a USB device emulated in one QEMU instance to a USB host controller emulated in another one over the usbredir protocol.
The v1 series included both the usbredir server transport and the ASPEED
AST1030 USB Device Controller (UDC) support. Following review feedback, this series contains only the usbredir server transport. The ASPEED
AST1030 UDC support will be sent separately.Quoted 4 lines
This work is part of a larger plan to model USB device-side support on ASPEED BMC/BIC SoCs, which has three goals:
1. Model the ASPEED UDC (AST2600 / AST1030). The AST2600 also has USB
host
Quoted 10 lines
(EHCI) controllers, so that work targets the AST2600 UDC: its gadget can be attached to the SoC's own EHCI bus, letting the guest enumerate its own gadget and exercise the UDC end-to-end. [Done]
https://lore.kernel.org/qemu-devel/20260902021542.3194812-1-jamin_lin@
aspeedtech.com/2. AST1030 UDC. The AST1030 has no USB host controller, so testing its UDC needs a second QEMU instance. The UDC gadget is redirected out of the guest with libusbredir and attached to another QEMU that runs a USB
host
Quoted 43 lines
(a VMM, or an AST2600 / AST2700 guest). [this series]3. ASPEED vHub, as a longer-term goal towards BMC KVM / Virtual Media support in QEMU. [future]
This series implements goal 2.
The transport
=============usb-redir-server exports a locally emulated USB device to a remote USB host over the usbredir protocol, so a device emulated in one QEMU instance can be enumerated by a host controller emulated in another one.
The left column is a request going to the device. The right column is the answer coming back. The middle hop carries usbredir messages over a socket. The top and bottom hops carry USBPackets inside QEMU.
remote QEMU: guest driver -> EHCI/XHCI | ^ USBPacket | | USBPacket v | "usb-redir" (the client) | ^ usbredir | chardev socket | usbredir v | usb-redir-server (the server, this series) | ^ USBPacket | | USBPacket v | any USBDevice, "-device <dev>,bus=<id>.0""usb-redir" (
hw/usb/redirect.c) is the client:
- it takes a USBPacket from the remote guest and writes it to the socket as a usbredir message
- it reads the answer from the socket and completes the USBPacketusb-redir-server is the server. It does the same thing, but backwards:
- it reads a usbredir message from the socket and runs it as a USBPacket on the bus below
- it takes the result of that USBPacket and writes it back to the same socket as a usbredir message, for the client to read
Quoted 9 lines
Quoted 3 lines
A USB device has to sit on a USB bus, and in QEMU a USB bus is always made by a host controller. So usb-redir-server makes one and acts as the host
That's a bit awkward, but makes sense. However I am not sure it sure it should also require a machine and sit on the sysbus. We may also want to build a specialized binary that doesn't cary any of the machine code etc and is target-free. something like "qemu-usb", not necessarily with this series though
I do not know yet how a "qemu-usb" binary would work, so v3 keeps the sysbus design. I also did not find any other way to fix "Bus not found" for "-device <dev>,bus=<id>.0". The lookup starts at the sysbus, so a device that does not sit there cannot offer a bus.
Because of that I will mark the device experimental and rename the type to "x-usb-redir-server". The file name stays redirect-server.c, like the other x- types in the tree.
If you have a better idea, I am happy to try it.
Quoted 4 lines
Patch 4 already works around firmware initialization.. I don't whether this is acceptable.. Perhaps we can accept it for now, but I'd mark the device "experimental" at this point.
That part is solved in v3, see my reply to patch 4
Thanks,
Jamin
Quoted 43 lines
Quoted 19 lines
controller on this side. It models no real chip: its cable is the chardev socket. The real host is in the other QEMU.
usbredir carries one device, not a bus. A hub cannot be exported: the protocol has no device address field. To export several devices, run one usb-redir-server per device, each with its own chardev.
Testing
=======
A plain usb-storage device was used to test the transport. The storage device is attached to usb-redir-server and exported over a Unix socket:$ qemu-system-aarch64 ... -chardev socket,id=usbredir0,path=/tmp/usbredir0.sock,server=on,wait=off(quoted diff) +0
-3-device usb-redir-server,id=usbredir0,chardev=usbredir0 -drive id=usbdisk,if=none,file=image0.ext4,format=raw -device usb-storage,bus=usbredir0.0,id=mystorage,drive=usbdiskQuoted 4 lines
The exported device can then be attached to EHCI bus 3 of an AST2700 guest using the existing usb-redir client:
-chardev
socket,id=storage,path=/tmp/usbredir0.sock,reconnect-ms=1000 \
Quoted 11 lines
-device usb-redir,chardev=storage,bus=usb-bus.3
The AST2700 Linux guest enumerates the redirected USB storage device:
root@ast2700-default:~# lsusb unable to initialize usb spec Bus 001 Device 001: ID 1d6b:0001 Linux 6.18.36-v00.08.03-gaf2f426c5786 uhci_hcd Generic UHCI Host Controller Bus 002 Device 001: ID 1d6b:0002 Linux 6.18.36-v00.08.03-gaf2f426c5786 ehci_hcd EHCI Host Controller Bus 002 Device 002: ID 46f4:0001 QEMU QEMU USB HARDDRIVEQuoted 3 lines
This also demonstrates that usb-redir-server is not specific to the ASPEED UDC. Any USBDevice can be attached to its USB bus and exported to a host controller in another QEMU instance.
something I have long wished for, pretty nice
--
Marc-André Lureau <[email protected]>
Quoted 96 lines
Subject: RE: [PATCH v2 0/8] hw/usb: Add a usbredir server transport
Hi Marc-André Lureau
Quoted 15 lines
Subject: Re: [PATCH v2 0/8] hw/usb: Add a usbredir server transport
Hi
Quoted 11 lines
This series adds a usbredir server transport to redirect a USB device emulated in one QEMU instance to a USB host controller emulated in another one over the usbredir protocol.
The v1 series included both the usbredir server transport and the ASPEED
AST1030 USB Device Controller (UDC) support. Following review feedback, this series contains only the usbredir server transport. The ASPEED
AST1030 UDC support will be sent separately.Quoted 7 lines
Quoted 5 lines
This work is part of a larger plan to model USB device-side support on ASPEED BMC/BIC SoCs, which has three goals:
1. Model the ASPEED UDC (AST2600 / AST1030). The AST2600 also has USB
host
Quoted 1 line
(EHCI) controllers, so that work targets the AST2600 UDC: its gadget
can
Quoted 8 lines
Quoted 8 lines
be attached to the SoC's own EHCI bus, letting the guest enumerate its own gadget and exercise the UDC end-to-end. [Done]
https://lore.kernel.org/qemu-devel/20260902021542.3194812-1-jamin_li
n@
aspeedtech.com/2. AST1030 UDC. The AST1030 has no USB host controller, so testing its
UDC
Quoted 1 line
Quoted 1 line
needs a second QEMU instance. The UDC gadget is redirected out of
the
Quoted 16 lines
Quoted 2 lines
guest with libusbredir and attached to another QEMU that runs a
USBhost
Quoted 13 lines
(a VMM, or an AST2600 / AST2700 guest). [this series]3. ASPEED vHub, as a longer-term goal towards BMC KVM / Virtual Media support in QEMU. [future]
This series implements goal 2.
The transport
=============usb-redir-server exports a locally emulated USB device to a remote USB host over the usbredir protocol, so a device emulated in one QEMU instance can be enumerated by a host controller emulated in
another one.
Quoted 34 lines
Quoted 30 lines
The left column is a request going to the device. The right column is the answer coming back. The middle hop carries usbredir messages over a socket. The top and bottom hops carry USBPackets inside QEMU.
remote QEMU: guest driver -> EHCI/XHCI | ^ USBPacket | | USBPacket v | "usb-redir" (the client) | ^ usbredir | chardev socket | usbredir v | usb-redir-server (the server, this series) | ^ USBPacket | | USBPacket v | any USBDevice, "-device <dev>,bus=<id>.0""usb-redir" (
hw/usb/redirect.c) is the client:
- it takes a USBPacket from the remote guest and writes it to the socket as a usbredir message
- it reads the answer from the socket and completes the USBPacketusb-redir-server is the server. It does the same thing, but backwards:
- it reads a usbredir message from the socket and runs it as a USBPacket on the bus below
- it takes the result of that USBPacket and writes it back to the same socket as a usbredir message, for the client to readQuoted 3 lines
A USB device has to sit on a USB bus, and in QEMU a USB bus is always made by a host controller. So usb-redir-server makes one and acts as the host
Quoted 6 lines
That's a bit awkward, but makes sense. However I am not sure it sure it should also require a machine and sit on the sysbus. We may also want to build a specialized binary that doesn't cary any of the machine code etc and is target-free. something like "qemu-usb", not necessarily with this series though
Quoted 5 lines
I do not know yet how a "qemu-usb" binary would work, so v3 keeps the sysbus design. I also did not find any other way to fix "Bus not found" for "-device <dev>,bus=<id>.0". The lookup starts at the sysbus, so a device that does not sit there cannot offer a bus.
Sorry to come back to this. I looked at the qemu-usb idea again, and there is one more thing worth mentioning.
There are two kinds of USB device here.
1. A self-contained device, like usb-storage or usb-kbd, is emulated by QEMU on its own. It needs no CPU and no machine, so a target-free binary would host it well.
2. A SoC-backed device is different. Its behavior comes from the guest firmware driving a UDC through MMIO, interrupts and DMA, so it needs a CPU, a machine and guest memory. The ASPEED UDC gadget is one of these, and it is what this series is for:
https://patchwork.kernel.org/project/qemu-devel/cover/[email protected]/
QEMU #1 -M ast1030-evb QEMU #2 -M ast2700a2-evb
+-----------------------------+ +-----------------------------+
| Zephyr usb_dc_aspeed | | Linux ehci-platform |
+--------------+--------------+ +--------------+--------------+
| MMIO, IRQ, DMA | MMIO
+--------------v--------------+ +--------------v--------------+
| aspeed.udc | | EHCI3 (usb-bus.3) |
+--------------+--------------+ +--------------+--------------+
| UDC registers, IRQ, DMA | USBPacket
+--------------v--------------+ +--------------v--------------+
| aspeed.udc-gadget | | usb-redir |
+--------------+--------------+ +--------------+--------------+
| USBPacket |
+--------------v--------------+ |
| x-usb-redir-server | |
+--------------+--------------+ |
| |
+=========== unix socket ============+usbredir protocol
A target-free binary would help the first kind. It would not help the second, which is why this device still needs a machine.
Jamin
Quoted 62 lines
Because of that I will mark the device experimental and rename the type to "x-usb-redir-server". The file name stays
redirect-server.c, like the other x- types in the tree.If you have a better idea, I am happy to try it.
Quoted 4 lines
Patch 4 already works around firmware initialization.. I don't whether this is acceptable.. Perhaps we can accept it for now, but I'd mark the device "experimental" at this point.
That part is solved in v3, see my reply to patch 4
Thanks,
JaminQuoted 11 lines
Quoted 11 lines
controller on this side. It models no real chip: its cable is the chardev socket. The real host is in the other QEMU.
usbredir carries one device, not a bus. A hub cannot be exported:
the protocol has no device address field. To export several devices, run one usb-redir-server per device, each with its own chardev.Testing
=======
A plain usb-storage device was used to test the transport. The storage device is attached to usb-redir-server and exported over a Unixsocket:
Quoted 33 lines
Quoted 8 lines
$ qemu-system-aarch64 ... -chardev socket,id=usbredir0,path=/tmp/usbredir0.sock,server=on,wait=off(quoted diff) +0
-3-device usb-redir-server,id=usbredir0,chardev=usbredir0 -drive id=usbdisk,if=none,file=image0.ext4,format=raw -device usb-storage,bus=usbredir0.0,id=mystorage,drive=usbdiskQuoted 4 lines
The exported device can then be attached to EHCI bus 3 of an AST2700 guest using the existing usb-redir client:
-chardev
socket,id=storage,path=/tmp/usbredir0.sock,reconnect-ms=1000 \
Quoted 12 lines
-device usb-redir,chardev=storage,bus=usb-bus.3
The AST2700 Linux guest enumerates the redirected USB storage device:
root@ast2700-default:~# lsusb unable to initialize usb spec Bus 001 Device 001: ID 1d6b:0001 Linux 6.18.36-v00.08.03-gaf2f426c5786 uhci_hcd Generic UHCI Host Controller Bus 002 Device 001: ID 1d6b:0002 Linux 6.18.36-v00.08.03-gaf2f426c5786 ehci_hcd EHCI Host Controller Bus 002 Device 002: ID 46f4:0001 QEMU QEMU USB HARDDRIVEQuoted 3 lines
This also demonstrates that usb-redir-server is not specific to the ASPEED UDC. Any USBDevice can be attached to its USB bus and exported to a host controller in another QEMU instance.
something I have long wished for, pretty nice
--
Marc-André Lureau <[email protected]>