Skip to contentSkip to conversation

qapi: [PATCH v2] Implement query-usbhost QMP command

Qemu-devel Alexander Kappner 3 participants 5 messages Apr 14, 2018 – Apr 17, 2018

lore
Share & tools
lore
#0 lore raw

Implement a QMP command similar to the HMP's "info usbhost" command. This allows a QMP client to query which USB devices may be available for redirection. Because the availability of the command needs to depend on the target's (not the build host's) USB configuration, a stub function in host-stub.c is provided for targets without USB support.

v2 of this patch resolves build failure under some configurations without libusb.

---
 hw/usb/host-libusb.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/usb/host-stub.c   |  9 ++++++++
 qapi/misc.json       | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 135 insertions(+)
hw/usb/host-libusb.c +64 -0
index 1b0be07..efdf577 100644
@@ -40,6 +40,7 @@
 #include <libusb.h>
 
 #include "qapi/error.h"
+#include "qapi/qapi-commands-misc.h"
 #include "qemu-common.h"
 #include "monitor/monitor.h"
 #include "qemu/error-report.h"
@@ -1743,6 +1744,69 @@ bool usb_host_dev_is_scsi_storage(USBDevice *ud)
     return is_scsi_storage;
 }
 
+UsbDeviceInfoList *qmp_query_usbhost(Error **errp)
+{
+    UsbDeviceInfoList *usb_list = NULL;
+    UsbDeviceInfoList *info;
+    libusb_device **devs = NULL;
+    struct libusb_device_descriptor ddesc;
+    char port[16];
+    int i, n;
+
+    if (usb_host_init() != 0) {
+        return NULL;
+    }
+    n = libusb_get_device_list(ctx, &devs);
+
+    for (i = 0; i < n; i++) {
+        if (libusb_get_device_descriptor(devs[i], &ddesc) != 0) {
+            continue;
+        }
+        if (ddesc.bDeviceClass == LIBUSB_CLASS_HUB) {
+            continue;
+        }
+        usb_host_get_port(devs[i], port, sizeof(port));
+        info = g_new0(UsbDeviceInfoList, 1);
+        info->value = g_new0(UsbDeviceInfo, 1);
+        info->value->id_vendor = ddesc.idVendor;
+        info->value->bus_num = libusb_get_bus_number(devs[i]);
+        info->value->dev_addr = libusb_get_device_address(devs[i]);
+        info->value->id_product = ddesc.idProduct;
+        info->value->b_device_class = ddesc.bDeviceClass;
+        info->value->speed = libusb_get_device_speed(devs[i]);
+        info->next = usb_list;
+        usb_list = info;
+
+        if (ddesc.iProduct) {
+            libusb_device_handle *handle;
+            if (libusb_open(devs[i], &handle) == 0) {
+                unsigned char name[64] = "";
+                libusb_get_string_descriptor_ascii(handle,
+                                                   ddesc.iProduct,
+                                                   name, sizeof(name));
+                libusb_close(handle);
+                info->value->str_product = g_strdup((gchar *)name);
+            }
+        } else
+            info->value->str_product = NULL;
+
+        if (ddesc.iManufacturer) {
+            libusb_device_handle *handle;
+            if (libusb_open(devs[i], &handle) == 0) {
+                unsigned char name[64] = "";
+                libusb_get_string_descriptor_ascii(handle,
+                                                   ddesc.iManufacturer,
+                                                   name, sizeof(name));
+                libusb_close(handle);
+                info->value->str_manufacturer = g_strdup((gchar *)name);
+            }
+        } else
+        info->value->str_manufacturer = NULL;
+    }
+    libusb_free_device_list(devs, 1);
+    return usb_list;
+}
+
 void hmp_info_usbhost(Monitor *mon, const QDict *qdict)
 {
     libusb_device **devs = NULL;
hw/usb/host-stub.c +9 -0
index 41d93ec..fd227c4 100644
@@ -35,6 +35,9 @@
 #include "ui/console.h"
 #include "hw/usb.h"
 #include "monitor/monitor.h"
+#include "qapi/error.h"
+#include "qapi/qapi-commands-misc.h"
+#include "qapi/qmp/qerror.h"
 
 void hmp_info_usbhost(Monitor *mon, const QDict *qdict)
 {
@@ -45,3 +48,9 @@ bool usb_host_dev_is_scsi_storage(USBDevice *ud)
 {
     return false;
 }
+
+UsbDeviceInfoList *qmp_query_usbhost(Error **errp)
+{
+    error_setg(errp, QERR_FEATURE_DISABLED, "usb");
+    return NULL;
+};
qapi/misc.json +62 -0

74 lines not on this page · raw message

Signature · 1 line
2.1.4
#1 lore raw
Quoted 1 line · Alexander Kappner

Implement a QMP command similar to the HMP's "info usbhost" command.

The hmp version of the command should be switched over to call the qmp_query_usbhost() and pretty-print the qapi struct then, to make sure hmp and qmp versions of the command deliver consistent results.

Quoted 2 lines

This allows a QMP client to query which USB devices may be available for redirection.

I'm wondering what the use case is?

At least libvirt sandboxes qemu, for security reasons, and you wouldn't get any useful results because qemu hasn't the permissions needed to scan the usb bus.

qapi/misc.json line 280

Quoted 9 lines

##

qapi/misc.json +8 -0
+# @query-usbhost:
+#
+# Returns information about USB devices available on the host
+#
+# Returns: a [UsbDeviceInfo]. Returns an error if compiled without
+# CONFIG_USB_LIBUSB
+#
+# Since: TODO (maintainer insert version number if mainlined)

Will not make it into 2.12, so 2.13.

qapi/misc.json line 921

Quoted 3 lines
qapi/misc.json +3 -0
+# @UsbDeviceInfo:
+#
+# @speed: the speed

Use an enum for this?

qapi/misc.json line 933

Quoted 11 lines
qapi/misc.json +11 -0
+# @id_vendor: idVendor field from device descriptor
+#
+# @id_product: idProduct field from device descriptor
+#
+# @str_product: string descriptor referenced by iProduct index, if any
+#
+# @str_manufacturer: string descriptor referenced by iManufacturer index, if any
+#
+# @dev_addr: address on bus that device is connected to
+#
+# @bus_num: bus number device is connected to

No port?

cheers,
Gerd

#2 lore raw

Hi Gerd,

thanks for reviewing. I'll follow up with a v3 of the patch addressing your proposed changes.

Quoted 6 lines
Quoted 2 lines

This allows a QMP client to query which USB devices may be available for redirection.

At least libvirt sandboxes qemu, for security reasons, and you wouldn't get any useful results because qemu hasn't the permissions needed to scan the usb bus.

Then at least this tells you that you can't make anything available via redirection either, which is also useful information.

Quoted 1 line

I'm wondering what the use case is?

I'm working on a smartphone client that talks to a QEMU instance via QMP, and that allows the user to switch USB devices between host and QEMU guest. This removes the need for a separate set of input devices permanently assigned to the host (since if I pass through a keyboard/mouse to the VM, I can't use that same keyboard/mouse to run a command that re-attaches those devices to the host, so I need some external channel). Basically using the phone as a KVM switch.

Best
Alexander

Quoted 51 lines · Gerd Hoffmann
Quoted 1 line · Alexander Kappner

Implement a QMP command similar to the HMP's "info usbhost" command.

The hmp version of the command should be switched over to call the qmp_query_usbhost() and pretty-print the qapi struct then, to make sure hmp and qmp versions of the command deliver consistent results.

Quoted 2 lines

This allows a QMP client to query which USB devices may be available for redirection.

I'm wondering what the use case is?

At least libvirt sandboxes qemu, for security reasons, and you wouldn't get any useful results because qemu hasn't the permissions needed to scan the usb bus.

qapi/misc.json line 280

Quoted 9 lines

##

qapi/misc.json +8 -0
+# @query-usbhost:
+#
+# Returns information about USB devices available on the host
+#
+# Returns: a [UsbDeviceInfo]. Returns an error if compiled without
+# CONFIG_USB_LIBUSB
+#
+# Since: TODO (maintainer insert version number if mainlined)

Will not make it into 2.12, so 2.13.

qapi/misc.json line 921

Quoted 3 lines
qapi/misc.json +3 -0
+# @UsbDeviceInfo:
+#
+# @speed: the speed

Use an enum for this?

qapi/misc.json line 933

Quoted 11 lines
qapi/misc.json +11 -0
+# @id_vendor: idVendor field from device descriptor
+#
+# @id_product: idProduct field from device descriptor
+#
+# @str_product: string descriptor referenced by iProduct index, if any
+#
+# @str_manufacturer: string descriptor referenced by iManufacturer index, if any
+#
+# @dev_addr: address on bus that device is connected to
+#
+# @bus_num: bus number device is connected to

No port?

cheers,
Gerd

#3 lore raw
Quoted 11 lines · Alexander Kappner

Hi Gerd,

thanks for reviewing. I'll follow up with a v3 of the patch addressing your proposed changes.

Quoted 6 lines
Quoted 2 lines

This allows a QMP client to query which USB devices may be available for redirection.

At least libvirt sandboxes qemu, for security reasons, and you wouldn't get any useful results because qemu hasn't the permissions needed to scan the usb bus.

Then at least this tells you that you can't make anything available via redirection either, which is also useful information.

Well, you can, but you have to go through libvirt instead. Libvirt will whitelist the usb device you want assign then (and only that one) so qemu can access it.

Quoted 9 lines
Quoted 1 line

I'm wondering what the use case is?

I'm working on a smartphone client that talks to a QEMU instance via QMP, and that allows the user to switch USB devices between host and QEMU guest. This removes the need for a separate set of input devices permanently assigned to the host (since if I pass through a keyboard/mouse to the VM, I can't use that same keyboard/mouse to run a command that re-attaches those devices to the host, so I need some external channel). Basically using the phone as a KVM switch.

That wouldn't work for any libvirt-managed guests.

Also: Do you know input-linux (see ui/input-linux.c)? That allows switching input devices between host and (one) guest by keyboard hotkey.

cheers,
Gerd

#4 lore raw
In reply to Alexander Kappner
Quoted 19 lines · Alexander Kappner

Implement a QMP command similar to the HMP's "info usbhost" command. This allows a QMP client to query which USB devices may be available for redirection. Because the availability of the command needs to depend on the target's (not the build host's) USB configuration, a stub function in host-stub.c is provided for targets without USB support.

v2 of this patch resolves build failure under some configurations without libusb.

---
 hw/usb/host-libusb.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/usb/host-stub.c   |  9 ++++++++
 qapi/misc.json       | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 135 insertions(+)

+++ b/qapi/misc.json

qapi/misc.json line 278

Quoted 9 lines
qapi/misc.json +6 -0
@@ -270,6 +270,46 @@
 { 'command': 'query-kvm', 'returns': 'KvmInfo' }
 
 ##
+# @query-usbhost:
+#
+# Returns information about USB devices available on the host
+#
+# Returns: a [UsbDeviceInfo]. Returns an error if compiled without
+# CONFIG_USB_LIBUSB

s/a [UsbDeviceInfo]/a list of UsbDeviceInfo

Review line 280

Quoted 2 lines
(quoted diff) +2 -0
+#
+# Since: TODO (maintainer insert version number if mainlined)

2.13 (it's easier to guess the next version, on the likely chance that it does not need tweaking, than it is to force the maintainer to have to tweak the patch on acceptance)

Quoted 8 lines
qapi/misc.json +4 -0
+##
+{ 'command': 'query-usbhost', 'returns': ['UsbDeviceInfo'] }
+
+##
 # @UuidInfo:
 #
 # Guest UUID information (Universally Unique Identifier).

qapi/misc.json line 923

Quoted 8 lines
qapi/misc.json +5 -0
@@ -876,6 +916,28 @@
            'regions': ['PciMemoryRegion']} }
 
 ##
+# @UsbDeviceInfo:
+#
+# @speed: the speed
+#
+# @id_vendor: idVendor field from device descriptor

Please, new QMP interfaces should favor '-' over '_' in naming; so this should be id-vendor, and so on.

qapi/misc.json line 927

Quoted 4 lines
qapi/misc.json +4 -0
+#
+# @id_product: idProduct field from device descriptor
+#
+# @str_product: string descriptor referenced by iProduct index, if any

What's the difference between idProduct and iProduct in the text here?

Review line 929

Quoted 2 lines
(quoted diff) +2 -0
+#
+# @str_manufacturer: string descriptor referenced by iManufacturer index, if any

Is the 'str_' (or my preferred switch to 'str-') prefix necessary; or can this field just be 'manufacturer'?

qapi/misc.json line 938

Quoted 9 lines
qapi/misc.json +9 -0
+#
+# @dev_addr: address on bus that device is connected to
+#
+# @bus_num: bus number device is connected to
+##
+{ 'struct': 'UsbDeviceInfo',
+  'data':
+  {'speed': 'int', 'id_vendor': 'int', 'id_product' : 'int', 'str_product': 'str',
+   'b_device_class': 'int', 'str_manufacturer' : 'str', 'dev_addr' : 'int', 'bus_num' : 'int'} }

Long line; please fit things within 80 columns.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org