usb, xen: add pvUSB backend
v3 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 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 | 60 +++ 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, 1197 insertions(+), 50 deletions(-) create mode 100644 hw/usb/xen-usb.c
Signature · 1 line
2.6.6
PATCH v3 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
@@ -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
@@ -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 · 1 line
2.6.6
PATCH v3 2/3 xen: write information about supported backends
In reply to Juergen GrossAdd 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.
---
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 | 60 ++++++++++++++++++++++++++++++++++++++++++++ hw/xen/xen_devconfig.c | 52 ++------------------------------------ include/hw/xen/xen_backend.h | 2 ++ 3 files changed, 64 insertions(+), 50 deletions(-)
hw/xen/xen_backend.c +60 -0
@@ -42,11 +42,35 @@ 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 +99,28 @@ 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 +772,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_READ ); +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
@@ -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 · 1 line
2.6.6
Re: [Qemu-devel] [PATCH v3 2/3] xen: write information about supported backends
Quoted 106 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.txtIn order to reuse the Xenstore directory creation already present in
hw/xen/xen_devconfig.cmove the related functions tohw/xen/xen_backend.cwhere they fit better.Signed-off-by Juergen Gross <[email protected]>---
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 | 60 ++++++++++++++++++++++++++++++++++++++++++++ hw/xen/xen_devconfig.c | 52 ++------------------------------------ include/hw/xen/xen_backend.h | 2 ++ 3 files changed, 64 insertions(+), 50 deletions(-)hw/xen/xen_backend.c +49
-0@@ -42,11 +42,35 @@ 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 +99,28 @@ 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 +772,20 @@ err:int xen_be_register (const char *type,struct XenDevOps *ops) { +char path[50 ]; +int rc; +
hw/xen/xen_backend.c err: · line 787
Quoted 10 lines
hw/xen/xen_backend.c +10
-0+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_READ );
Do you actually need the guest to be able to read those paths?
Quoted 37 lines
hw/xen/xen_backend.c +1
-0+return xenstore_scan (type, xen_domid, ops); }hw/xen/xen_devconfig.c +0
-25@@ -5,54 +5,6 @@/* ------------------------------------------------------------- */ -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 void xen_config_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); - } -} - -/* ------------------------------------------------------------- */
hw/xen/xen_devconfig.c line 42
Quoted 10 lines
hw/xen/xen_devconfig.c +0
-10- -static int xen_config_dev_mkdir (char *dev,int p) -{ -struct xs_permissions perms[2 ] = {{ - .id =0 ,/* set owner: dom0 */ - },{ - .id = xen_domid, - .perms = p, - }}; -
The function looks like it as been tailored to mkdir for config of backends. So it does not seems out of place.
Quoted 50 lines
hw/xen/xen_devconfig.c +2
-15-if (!xs_mkdir (xenstore,0 , dev)) { -xen_be_printf (NULL ,0 ,"xs_mkdir %s: failed\n" , dev); -return -1 ; - } -xen_config_cleanup_dir (g_strdup (dev)); - -if (!xs_set_permissions (xenstore,0 , dev, perms,2 )) { -xen_be_printf (NULL ,0 ,"xs_set_permissions %s: failed\n" , dev); -return -1 ; - } -return 0 ; -} -static int xen_config_dev_dirs (const char *ftype,const char *btype,int vdev,char *fe,char *be,int len) {@@ -66,8 +18,8 @@ static int xen_config_dev_dirs(const char *ftype, const char *btype, int vdev,snprintf (be, len,"%s/backend/%s/%d/%d" , dom, btype, xen_domid, vdev);free (dom); -xen_config_dev_mkdir (fe,XS_PERM_READ |XS_PERM_WRITE ); -xen_config_dev_mkdir (be,XS_PERM_READ ); +xenstore_mkdir (fe,XS_PERM_READ |XS_PERM_WRITE ); +xenstore_mkdir (be,XS_PERM_READ );return 0 ; }include/hw/xen/xen_backend.h +2
-0@@ -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 · 1 line
2.6.6
Signature · 6 lines
Anthony PERARD _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
Re: [Qemu-devel] [PATCH v3 2/3] xen: write information about supported backends
Quoted 46 lines · Anthony PERARD
Quoted 45 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.txtIn order to reuse the Xenstore directory creation already present in
hw/xen/xen_devconfig.cmove the related functions tohw/xen/xen_backend.cwhere they fit better.Signed-off-by Juergen Gross <[email protected]>---
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 | 60 ++++++++++++++++++++++++++++++++++++++++++++ hw/xen/xen_devconfig.c | 52 ++------------------------------------ include/hw/xen/xen_backend.h | 2 ++ 3 files changed, 64 insertions(+), 50 deletions(-)hw/xen/xen_backend.c +7
-0@@ -726,6 +772,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;
Quoted 8 lines
hw/xen/xen_backend.c err: · line 787
Quoted 6 lines
hw/xen/xen_backend.c +6
-0+ } + } + +snprintf (path,sizeof (path),"device-model/%u/backends/%s" , xen_domid, + type); +xenstore_mkdir (path,XS_PERM_READ );Do you actually need the guest to be able to read those paths?
No, you are right.
Quoted 38 lines
Quoted 38 lines
hw/xen/xen_devconfig.c +0
-30@@ -5,54 +5,6 @@/* ------------------------------------------------------------- */ -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 void xen_config_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); - } -} - -/* ------------------------------------------------------------- */ - -static int xen_config_dev_mkdir (char *dev,int p) -{ -struct xs_permissions perms[2 ] = {{ - .id =0 ,/* set owner: dom0 */
Quoted 8 lines
hw/xen/xen_devconfig.c line 42
Quoted 5 lines
hw/xen/xen_devconfig.c +0
-5- },{ - .id = xen_domid, - .perms = p, - }}; -The function looks like it as been tailored to mkdir for config of backends. So it does not seems out of place.
It has been tailored for config of _devices_ by the backend.
I still think it would fit better now into xen_backend.c, OTOH in case you like it better in xen_devconfig.c I won't argue against it.
Juergen
Re: [PATCH v3 2/3] xen: write information about supported backends
Quoted 15 lines · Juergen Gross
Quoted 13 lines · Anthony PERARD
hw/xen/xen_devconfig.c line 42
Quoted 9 lines · Juergen Gross
hw/xen/xen_devconfig.c +0
-9-static int xen_config_dev_mkdir (char *dev,int p) -{ -struct xs_permissions perms[2 ] = {{ - .id =0 ,/* set owner: dom0 */ - },{ - .id = xen_domid, - .perms = p, - }}; -The function looks like it as been tailored to mkdir for config of backends. So it does not seems out of place.
Quoted 4 lines
It has been tailored for config of _devices_ by the backend.
I still think it would fit better now into
xen_backend.c, OTOH in case you like it better inxen_devconfig.cI won't argue against it.
I guess the function looks generic enough. So I'm now fine with moving it to xen_backend.c.
Signature · 6 lines
Anthony PERARD _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
PATCH v3 3/3 xen: add pvUSB backend
In reply to Juergen GrossAdd 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
@@ -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
@@ -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
@@ -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
@@ -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
Re: [Qemu-devel] [PATCH v3 3/3] xen: add pvUSB backend
Quoted 6 lines · 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.
Signed-off-by Juergen Gross <[email protected]>
Thanks,
Signature · 1 line
Anthony PERARD