usbredir: Do not detach usb if backend chardev disconnect
PATCH 1/1 usbredir: Do not detach usb if backend chardev disconnect
If the network between qemu and usbredirserver is temporarily disconnected, the USB device in the VM will be unplugged. If the reconnect parameter is configured for the backend chardev, the device will be reconnected later. But from the inside of the VM, this USB device has experienced unplug and re-plug, if the USB storage device has been mounted in the VM before, the drive letter will change after the device is re-plugged.
So in this case, we no longer unplug the device, and operations to the USB is returned immediately at this point.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1305
--- hw/usb/redirect.c | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-)
hw/usb/redirect.c +35 -2
@@ -379,6 +379,11 @@ static void usbredir_cancel_packet(USBDevice *udev, USBPacket *p)USBRedirDevice *dev =USB_REDIRECT (udev);int i =USBEP2I (p->ep); +if (!dev->parser) { +ERROR ("usbredir parser is not available, need reconnect.\n" ); +return ; + } +if (p->combined) {usb_combined_packet_cancel (udev, p);return ;
@@ -519,6 +524,11 @@ static void usbredir_handle_reset(USBDevice *udev) {USBRedirDevice *dev =USB_REDIRECT (udev); +if (!dev->parser) { +ERROR ("usbredir parser is not available, need reconnect.\n" ); +return ; + } +DPRINTF ("reset device\n" );usbredirparser_send_reset (dev->parser);usbredirparser_do_write (dev->parser);
@@ -959,6 +969,11 @@ static void usbredir_handle_data(USBDevice *udev, USBPacket *p)USBRedirDevice *dev =USB_REDIRECT (udev);uint8_t ep; +if (!dev->parser) { +ERROR ("usbredir parser is not available, need reconnect.\n" ); +return ; + } + ep = p->ep->nr;if (p->pid ==USB_TOKEN_IN ) { ep |=USB_DIR_IN ;
@@ -1027,6 +1042,11 @@ static void usbredir_ep_stopped(USBDevice *udev, USBEndpoint *uep) {USBRedirDevice *dev =USB_REDIRECT (udev); +if (!dev->parser) { +ERROR ("usbredir parser is not available, need reconnect.\n" ); +return ; + } +usbredir_stop_ep (dev,USBEP2I (uep));usbredirparser_do_write (dev->parser); }
@@ -1098,6 +1118,11 @@ static void usbredir_handle_control(USBDevice *udev, USBPacket *p,USBRedirDevice *dev =USB_REDIRECT (udev);struct usb_redir_control_packet_header control_packet; +if (!dev->parser) { +ERROR ("usbredir parser is not available, need reconnect.\n" ); +return ; + } +if (usbredir_already_in_flight (dev, p->id)) { p->status =USB_RET_ASYNC ;return ;
@@ -1155,6 +1180,11 @@ static int usbredir_alloc_streams(USBDevice *udev, USBEndpoint **eps,struct usb_redir_alloc_bulk_streams_header alloc_streams;int i; +if (!dev->parser) { +ERROR ("usbredir parser is not available, need reconnect.\n" ); +return -1 ; + } +if (!usbredirparser_peer_has_cap (dev->parser, usb_redir_cap_bulk_streams)) {ERROR ("peer does not support streams\n" );
@@ -1193,6 +1223,11 @@ static void usbredir_free_streams(USBDevice *udev, USBEndpoint **eps,struct usb_redir_free_bulk_streams_header free_streams;int i; +if (!dev->parser) { +ERROR ("usbredir parser is not available, need reconnect.\n" ); +return ; + } +if (!usbredirparser_peer_has_cap (dev->parser, usb_redir_cap_bulk_streams)) {return ;
@@ -1219,8 +1254,6 @@ static void usbredir_chardev_close_bh(void *opaque)USBRedirDevice *dev = opaque;qemu_bh_cancel (dev->device_reject_bh); -usbredir_device_disconnect (dev); -if (dev->parser) {DPRINTF ("destroying usbredirparser\n" );usbredirparser_destroy (dev->parser);
Signature · 1 line
2.27.0
ping !
please review this patch : [PATCH] usbredir: Do not detach usb if backend chardev disconnect - minglei.liu (kernel.org)
<https://lore.kernel.org/qemu-devel/[email protected]/>
Quoted 123 lines · minglei.liu <[email protected]> 于2022年11月9日周三 19:56写道:
If the network between qemu and usbredirserver is temporarily disconnected, the USB device in the VM will be unplugged. If the reconnect parameter is configured for the backend chardev, the device will be reconnected later. But from the inside of the VM, this USB device has experienced unplug and re-plug, if the USB storage device has been mounted in the VM before, the drive letter will change after the device is re-plugged.
So in this case, we no longer unplug the device, and operations to the USB is returned immediately at this point.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1305
Signed-off-by minglei.liu <[email protected]>--- hw/usb/redirect.c | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-)hw/usb/redirect.c +5
-0@@ -379,6 +379,11 @@ static void usbredir_cancel_packet(USBDevice *udev, USBPacket *p) USBRedirDevice *dev = USB_REDIRECT(udev);int i =USBEP2I (p->ep); +if (!dev->parser) { +ERROR ("usbredir parser is not available, need reconnect.\n" ); +return ; + } +if (p->combined) {usb_combined_packet_cancel (udev, p);return;hw/usb/redirect.c +10
-0@@ -519,6 +524,11 @@ static void usbredir_handle_reset(USBDevice *udev) {USBRedirDevice *dev =USB_REDIRECT (udev); +if (!dev->parser) { +ERROR ("usbredir parser is not available, need reconnect.\n" ); +return ; + } +DPRINTF ("reset device\n" );usbredirparser_send_reset (dev->parser);usbredirparser_do_write (dev->parser);@@ -959,6 +969,11 @@ static void usbredir_handle_data(USBDevice *udev, USBPacket *p) USBRedirDevice *dev = USB_REDIRECT(udev);uint8_t ep; +if (!dev->parser) { +ERROR ("usbredir parser is not available, need reconnect.\n" ); +return ; + } + ep = p->ep->nr;if (p->pid ==USB_TOKEN_IN ) {ep |= USB_DIR_IN;hw/usb/redirect.c +5
-0@@ -1027,6 +1042,11 @@ static void usbredir_ep_stopped(USBDevice *udev, USBEndpoint *uep) {USBRedirDevice *dev =USB_REDIRECT (udev); +if (!dev->parser) { +ERROR ("usbredir parser is not available, need reconnect.\n" ); +return ; + } +usbredir_stop_ep (dev,USBEP2I (uep));usbredirparser_do_write (dev->parser);}
hw/usb/redirect.c +5
-0@@ -1098,6 +1118,11 @@ static void usbredir_handle_control(USBDevice *udev, USBPacket *p, USBRedirDevice *dev =USB_REDIRECT (udev);struct usb_redir_control_packet_header control_packet; +if (!dev->parser) { +ERROR ("usbredir parser is not available, need reconnect.\n" ); +return ; + } +if (usbredir_already_in_flight (dev, p->id)) { p->status =USB_RET_ASYNC ;return;hw/usb/redirect.c +5
-0@@ -1155,6 +1180,11 @@ static int usbredir_alloc_streams(USBDevice *udev,USBEndpoint **eps, struct usb_redir_alloc_bulk_streams_header alloc_streams;int i; +if (!dev->parser) { +ERROR ("usbredir parser is not available, need reconnect.\n" ); +return -1 ; + } +if (!usbredirparser_peer_has_cap (dev->parser, usb_redir_cap_bulk_streams)) {ERROR("peer does not support streams\n");hw/usb/redirect.c +5
-0@@ -1193,6 +1223,11 @@ static void usbredir_free_streams(USBDevice *udev,USBEndpoint **eps, struct usb_redir_free_bulk_streams_header free_streams;int i; +if (!dev->parser) { +ERROR ("usbredir parser is not available, need reconnect.\n" ); +return ; + } +if (!usbredirparser_peer_has_cap (dev->parser, usb_redir_cap_bulk_streams)) {return;hw/usb/redirect.c +0
-3@@ -1219,8 +1254,6 @@ static void usbredir_chardev_close_bh(void *opaque)USBRedirDevice *dev = opaque;qemu_bh_cancel (dev->device_reject_bh); -usbredir_device_disconnect (dev); -if (dev->parser) {DPRINTF ("destroying usbredirparser\n" );usbredirparser_destroy (dev->parser); --2.27.0
Signature · 1 line
刘明磊 Smartx 软件工程师
Quoted 5 lines · Minglei Liu
ping !
please review this patch : [PATCH] usbredir: Do not detach usb if backend chardev disconnect - minglei.liu (kernel.org)
<https://lore.kernel.org/qemu-devel/[email protected]/>
Quoted 11 lines
Quoted 9 lines · minglei.liu <[email protected]> 于2022年11月9日周三 19:56写道:
If the network between qemu and usbredirserver is temporarily disconnected, the USB device in the VM will be unplugged. If the reconnect parameter is configured for the backend chardev, the device will be reconnected later. But from the inside of the VM, this USB device has experienced unplug and re-plug, if the USB storage device has been mounted in the VM before, the drive letter will change after the device is re-plugged.
So in this case, we no longer unplug the device, and operations to the USB is returned immediately at this point.
If you are lucky, and it also depends on the kind of device.
I don't think this is a safe thing to do in general. You just don't know what state the usb device is in when you are re-connecting, and you also don't know whenever a re-connect will ever happen.
take care,
Gerd