Skip to contentSkip to conversation

usb, xen: add pvUSB backend

PATCH In review Juergen Gross 3 participants 7 messages May 12, 2016 – May 13, 2016

lore
Share & tools
lore
#0 lore raw

v5 cover usb, xen: add pvUSB backend

This series adds a Xen pvUSB backend driver to qemu. USB devices connected to the host can be passed through to a Xen guest. The devices are specified via Xenstore. Access to the devices is done via host-libusb.c

I've tested the backend with various USB devices (memory sticks, keyboard, ...).

Changes in V5:
- patch 2: silence checkpatch errors/warnings

Changes in V4:
- patch 2: don't allow guest reading the new directory as requested by Anthony Perard

Changes in V3:
- added new patch 2 (was sent formerly on it's own)
- patch 3 (was 2): multiple small changes as requested by Anthony Perard
- patch 3 (was 2): check for full ring in case of hotplug events as requested by Anthony Perard
- patch 3 (was 2): use RING_COPY_REQUEST() instead of RING_GET_REQUEST()
- patch 3 (was 2): add check for required header version and disable backend in case header is too old

Changes in V2:
- rebased to current qemu master
- removed support for isoc requests in this series, as the optimal way to support those isn't clear yet. Will be added in a followup series.
- several changes as requested by Stefano Stabellini and Konrad Wilk

Changes since RFC:
- open device via qdev_device_add(), making patch 1 obsolete
- modify patch 2 to use isoc passthrough via libusb instead of one job per frame

Juergen Gross (3):
xen: introduce dummy system device
xen: write information about supported backends
xen: add pvUSB backend

 hw/usb/Makefile.objs         |    4 +
 hw/usb/xen-usb.c             | 1080 ++++++++++++++++++++++++++++++++++++++++++
 hw/xen/xen_backend.c         |   63 +++
 hw/xen/xen_devconfig.c       |   52 +-
 hw/xenpv/xen_machine_pv.c    |   43 ++
 include/hw/xen/xen_backend.h |    6 +
 include/hw/xen/xen_common.h  |    2 +
 7 files changed, 1200 insertions(+), 50 deletions(-)
 create mode 100644 hw/usb/xen-usb.c
Signature · 7 lines
2.6.6


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#1 lore raw

PATCH v5 1/3 xen: introduce dummy system device

Introduce a new dummy system device serving as parent for virtual buses. This will enable new pv backends to introduce virtual buses which are removable again opposed to system buses which are meant to stay once added.

---
V2: NOT changed, even if requested by Stefano Stabellini: the xen dummy
system device is needed by virtfs for Xen PV(H) guests, too

---
 hw/xenpv/xen_machine_pv.c    | 40 ++++++++++++++++++++++++++++++++++++++++
 include/hw/xen/xen_backend.h |  1 +
 2 files changed, 41 insertions(+)
hw/xenpv/xen_machine_pv.c +40 -0
index fc13535..48d5bc6 100644
@@ -25,10 +25,15 @@
 #include "qemu/osdep.h"
 #include "hw/hw.h"
 #include "hw/boards.h"
+#include "hw/sysbus.h"
 #include "hw/xen/xen_backend.h"
 #include "xen_domainbuild.h"
 #include "sysemu/block-backend.h"
 
+#define TYPE_XENSYSDEV "xensysdev"
+
+DeviceState *xen_sysdev;
+
 static void xen_init_pv(MachineState *machine)
 {
     DriveInfo *dinfo;
@@ -67,6 +72,9 @@ static void xen_init_pv(MachineState *machine)
         break;
     }
 
+    xen_sysdev = qdev_create(NULL, TYPE_XENSYSDEV);
+    qdev_init_nofail(xen_sysdev);
+
     xen_be_register("console", &xen_console_ops);
     xen_be_register("vkbd", &xen_kbdmouse_ops);
     xen_be_register("vfb", &xen_framebuffer_ops);
@@ -101,6 +109,38 @@ static void xen_init_pv(MachineState *machine)
     xen_init_display(xen_domid);
 }
 
+static int xen_sysdev_init(SysBusDevice *dev)
+{
+    return 0;
+}
+
+static Property xen_sysdev_properties[] = {
+    {/* end of property list */},
+};
+
+static void xen_sysdev_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
+
+    k->init = xen_sysdev_init;
+    dc->props = xen_sysdev_properties;
+}
+
+static const TypeInfo xensysdev_info = {
+    .name          = TYPE_XENSYSDEV,
+    .parent        = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(SysBusDevice),
+    .class_init    = xen_sysdev_class_init,
+};
+
+static void xenpv_register_types(void)
+{
+    type_register_static(&xensysdev_info);
+}
+
+type_init(xenpv_register_types);
+
 static void xenpv_machine_init(MachineClass *mc)
 {
     mc->desc = "Xen Para-virtualized PC";
include/hw/xen/xen_backend.h +1 -0
index c839eeb..b4b4ff0 100644
@@ -60,6 +60,7 @@ extern xc_interface *xen_xc;
 extern xenforeignmemory_handle *xen_fmem;
 extern struct xs_handle *xenstore;
 extern const char *xen_protocol;
+extern DeviceState *xen_sysdev;
 
 /* xenstore helper functions */
 int xenstore_write_str(const char *base, const char *node, const char *val);
Signature · 7 lines
2.6.6


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Reviewed-by on v5 1/3
#2 lore raw

Re: [Qemu-devel] [Xen-devel] [PATCH v5 1/3] xen: introduce dummy system device

Quoted 7 lines · Juergen Gross

Introduce a new dummy system device serving as parent for virtual buses. This will enable new pv backends to introduce virtual buses which are removable again opposed to system buses which are meant to stay once added.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#3 lore raw

PATCH v5 2/3 xen: write information about supported backends

In reply to Juergen Gross

Add a Xenstore directory for each supported pv backend. This will allow Xen tools to decide which backend type to use in case there are multiple possibilities.

The information is added under /local/domain/<backend-domid>/device-model/<domid>/backends before the "running" state is written to Xenstore. Using a directory for each backend enables us to add parameters for specific backends in the future.

This interface is documented in the Xen source repository in the file docs/misc/qemu-backends.txt

In order to reuse the Xenstore directory creation already present in hw/xen/xen_devconfig.c move the related functions to hw/xen/xen_backend.c where they fit better.

---
V4: don't allow guest reading the new directory as requested by
Anthony Perard

V3: Added .backend_register function to XenDevOps in order to have a
way to let the backend decide whether all prerequisites are met for support.

V2: update commit message as requested by Stefano

---
 hw/xen/xen_backend.c         | 63 ++++++++++++++++++++++++++++++++++++++++++++
 hw/xen/xen_devconfig.c       | 52 ++----------------------------------
 include/hw/xen/xen_backend.h |  2 ++
 3 files changed, 67 insertions(+), 50 deletions(-)
hw/xen/xen_backend.c +63 -0
index 60575ad..c63f9df 100644
@@ -42,11 +42,36 @@ struct xs_handle *xenstore = NULL;
 const char *xen_protocol;
 
 /* private */
+struct xs_dirs {
+    char *xs_dir;
+    QTAILQ_ENTRY(xs_dirs) list;
+};
+static QTAILQ_HEAD(xs_dirs_head, xs_dirs) xs_cleanup =
+    QTAILQ_HEAD_INITIALIZER(xs_cleanup);
+
 static QTAILQ_HEAD(XenDeviceHead, XenDevice) xendevs = QTAILQ_HEAD_INITIALIZER(xendevs);
 static int debug = 0;
 
 /* ------------------------------------------------------------- */
 
+static void xenstore_cleanup_dir(char *dir)
+{
+    struct xs_dirs *d;
+
+    d = g_malloc(sizeof(*d));
+    d->xs_dir = dir;
+    QTAILQ_INSERT_TAIL(&xs_cleanup, d, list);
+}
+
+void xen_config_cleanup(void)
+{
+    struct xs_dirs *d;
+
+    QTAILQ_FOREACH(d, &xs_cleanup, list) {
+        xs_rm(xenstore, 0, d->xs_dir);
+    }
+}
+
 int xenstore_write_str(const char *base, const char *node, const char *val)
 {
     char abspath[XEN_BUFSIZE];
@@ -75,6 +100,30 @@ char *xenstore_read_str(const char *base, const char *node)
     return ret;
 }
 
+int xenstore_mkdir(char *path, int p)
+{
+    struct xs_permissions perms[2] = {
+        {
+            .id    = 0, /* set owner: dom0 */
+        }, {
+            .id    = xen_domid,
+            .perms = p,
+        }
+    };
+
+    if (!xs_mkdir(xenstore, 0, path)) {
+        xen_be_printf(NULL, 0, "xs_mkdir %s: failed\n", path);
+        return -1;
+    }
+    xenstore_cleanup_dir(g_strdup(path));
+
+    if (!xs_set_permissions(xenstore, 0, path, perms, 2)) {
+        xen_be_printf(NULL, 0, "xs_set_permissions %s: failed\n", path);
+        return -1;
+    }
+    return 0;
+}
+
 int xenstore_write_int(const char *base, const char *node, int ival)
 {
     char val[12];
@@ -726,6 +775,20 @@ err:
 
 int xen_be_register(const char *type, struct XenDevOps *ops)
 {
+    char path[50];
+    int rc;
+
+    if (ops->backend_register) {
+        rc = ops->backend_register();
+        if (rc) {
+            return rc;
+        }
+    }
+
+    snprintf(path, sizeof(path), "device-model/%u/backends/%s", xen_domid,
+             type);
+    xenstore_mkdir(path, XS_PERM_NONE);
+
     return xenstore_scan(type, xen_domid, ops);
 }
 
hw/xen/xen_devconfig.c +2 -50

64 lines not on this page · raw message

include/hw/xen/xen_backend.h +2 -0
index b4b4ff0..63364f7 100644
@@ -28,6 +28,7 @@ struct XenDevOps {
     int       (*free)(struct XenDevice *xendev);
     void      (*backend_changed)(struct XenDevice *xendev, const char *node);
     void      (*frontend_changed)(struct XenDevice *xendev, const char *node);
+    int       (*backend_register)(void);
 };
 
 struct XenDevice {
@@ -63,6 +64,7 @@ extern const char *xen_protocol;
 extern DeviceState *xen_sysdev;
 
 /* xenstore helper functions */
+int xenstore_mkdir(char *path, int p);
 int xenstore_write_str(const char *base, const char *node, const char *val);
 int xenstore_write_int(const char *base, const char *node, int ival);
 int xenstore_write_int64(const char *base, const char *node, int64_t ival);
Signature · 7 lines
2.6.6


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Reviewed-by on v5 2/3
#4 lore raw

Re: [PATCH v5 2/3] xen: write information about supported backends

Quoted 13 lines · Juergen Gross

Add a Xenstore directory for each supported pv backend. This will allow Xen tools to decide which backend type to use in case there are multiple possibilities.

The information is added under /local/domain/<backend-domid>/device-model/<domid>/backends before the "running" state is written to Xenstore. Using a directory for each backend enables us to add parameters for specific backends in the future.

This interface is documented in the Xen source repository in the file docs/misc/qemu-backends.txt

Quoted 6 lines

In order to reuse the Xenstore directory creation already present in hw/xen/xen_devconfig.c move the related functions to hw/xen/xen_backend.c where they fit better.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#5 lore raw

PATCH v5 3/3 xen: add pvUSB backend

In reply to Juergen Gross

Add a backend for para-virtualized USB devices for xen domains.

The backend is using host-libusb to forward USB requests from a domain via libusb to the real device(s) passed through.

---
V3: multiple small changes as requested by Anthony Perard
check for full ring in case of hotplug events as requested by Anthony Perard
use RING_COPY_REQUEST() instead of RING_GET_REQUEST() add check for required header version and disable backend in case header is too old

V2: use xen_be_printf() instead of fprintf() for diagnostic prints as
requested by Stefano Stabellini
lots of small changes requested by Konrad Wilk
removed isoc request support, will be in separate patch

---
 hw/usb/Makefile.objs         |    4 +
 hw/usb/xen-usb.c             | 1080 ++++++++++++++++++++++++++++++++++++++++++
 hw/xenpv/xen_machine_pv.c    |    3 +
 include/hw/xen/xen_backend.h |    3 +
 include/hw/xen/xen_common.h  |    2 +
 5 files changed, 1092 insertions(+)
 create mode 100644 hw/usb/xen-usb.c
hw/usb/Makefile.objs +4 -0
index 2717027..98b5c9d 100644
@@ -38,3 +38,7 @@ common-obj-$(CONFIG_USB_REDIR) += redirect.o quirks.o
 
 # usb pass-through
 common-obj-y += $(patsubst %,host-%.o,$(HOST_USB))
+
+ifeq ($(CONFIG_USB_LIBUSB),y)
+common-obj-$(CONFIG_XEN_BACKEND) += xen-usb.o
+endif
hw/usb/xen-usb.c new file +1080 -0

1,080 lines not on this page · raw message

hw/xenpv/xen_machine_pv.c +3 -0
index 48d5bc6..f68cf48 100644
@@ -80,6 +80,9 @@ static void xen_init_pv(MachineState *machine)
     xen_be_register("vfb", &xen_framebuffer_ops);
     xen_be_register("qdisk", &xen_blkdev_ops);
     xen_be_register("qnic", &xen_netdev_ops);
+#ifdef CONFIG_USB_LIBUSB
+    xen_be_register("qusb", &xen_usb_ops);
+#endif
 
     /* configure framebuffer */
     if (xenfb_enabled) {
include/hw/xen/xen_backend.h +3 -0
index 63364f7..6e18a46 100644
@@ -101,6 +101,9 @@ extern struct XenDevOps xen_kbdmouse_ops;     /* xen_framebuffer.c */
 extern struct XenDevOps xen_framebuffer_ops;  /* xen_framebuffer.c */
 extern struct XenDevOps xen_blkdev_ops;       /* xen_disk.c        */
 extern struct XenDevOps xen_netdev_ops;       /* xen_nic.c         */
+#ifdef CONFIG_USB_LIBUSB
+extern struct XenDevOps xen_usb_ops;          /* xen-usb.c         */
+#endif
 
 void xen_init_display(int domid);
 
include/hw/xen/xen_common.h +2 -0
index bd65e67..d010cee 100644
@@ -49,6 +49,8 @@ typedef xc_gnttab xengnttab_handle;
 #define xengnttab_unmap(h, a, n) xc_gnttab_munmap(h, a, n)
 #define xengnttab_map_grant_refs(h, c, d, r, p) \
     xc_gnttab_map_grant_refs(h, c, d, r, p)
+#define xengnttab_map_domain_grant_refs(h, c, d, r, p) \
+    xc_gnttab_map_domain_grant_refs(h, c, d, r, p)
 
 #define xenforeignmemory_open(l, f) xen_xc
 
Signature · 7 lines
2.6.6


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#6 lore raw
In reply to Juergen Gross
Quoted 11 lines · Juergen Gross

This series adds a Xen pvUSB backend driver to qemu. USB devices connected to the host can be passed through to a Xen guest. The devices are specified via Xenstore. Access to the devices is done via host-libusb.c

I've tested the backend with various USB devices (memory sticks, keyboard, ...).

Changes in V5:
- patch 2: silence checkpatch errors/warnings

Added to usb patch queue.

thanks,
Gerd