xen pvusb toolstack work
v9 cover xen pvusb toolstack work
This patch series is to add pvusb toolstack work, supporting hot add|remove USB device to|from guest and specify USB device in domain configuration file.
Changes to V8:
* lots of changes in libxl pvusb API (patch 3/7)
* update 2/7 to write separate read_sysfs_file function
* address all other comments
V8:
http://lists.xen.org/archives/html/xen-devel/2015-10/msg02178.html
V7:
http://lists.xen.org/archives/html/xen-devel/2015-09/msg03115.html
V6:
http://lists.xen.org/archives/html/xen-devel/2015-08/msg00750.html
V5:
http://lists.xen.org/archives/html/xen-devel/2015-06/msg04052.html
V4:
http://lists.xenproject.org/archives/html/xen-devel/2015-06/msg01327.html
Related Discussion Threads:
http://www.redhat.com/archives/libvir-list/2014-June/msg00038.html
http://lists.xen.org/archives/html/xen-devel/2014-06/msg00086.html
<<< pvusb work introduction >>>
1. Overview
There are two general methods for passing through individual host devices to a guest. The first is via an emulated USB device controller; the second is PVUSB.
Additionally, there are two ways to add USB devices to a guest: via the config file at domain creation time, and via hot-plug while the VM is running.
* Emulated USB
In emulated USB, the device model (qemu) presents an emulated USB controller to the guest. The device model process then grabs control of the device from domain 0 and and passes the USB commands between the guest OS and the host USB device.
This method is only available to HVM domains, and is not available for domains running with device model stubdomains.
* PVUSB
PVUSB uses a paravirtialized front-end/back-end interface, similar to the traditional Xen PV network and disk protocols. In order to use PVUSB, you need usbfront in your guest OS, and usbback in dom0 (or your USB driver domain).
2. Specifying a host USB device
QEMU qmp commands allows USB devices to be specified either by their bus address (in the form bus.device) or their device tag (in the form vendorid:deviceid).
Each way of specifying has its advantages:
Specifying by device tag will always get the same device,
regardless of where the device ends up in the USB bus topology. However, if there are two identical devices, it will not allow you to specify which one.
Specifying by bus address will always allow you to choose a
specific device, even if you have duplicates. However, the bus address may change depending on which port you plugged the device into, and possibly also after a reboot.
To avoid duplication of vendorid:deviceid, we'll use bus address to specify host USB device in xl toolstack.
You can use lsusb to list the USB devices on the system:
Bus 001 Device 003: ID 0424:2514 Standard Microsystems Corp. USB 2.0 Hub
Bus 003 Device 002: ID f617:0905
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 001 Device 004: ID 0424:2640 Standard Microsystems Corp. USB 2.0 Hub
Bus 001 Device 005: ID 0424:4060 Standard Microsystems Corp. Ultra Fast Media Reader
Bus 001 Device 006: ID 046d:c016 Logitech, Inc. Optical Wheel Mouse
To pass through the Logitec mouse, for instance, you could specify 1.6 (remove leading zeroes).
Note: USB hubs can not be assigned to guest.
3. PVUSB toolstack
* Specify USB device in xl config file
You can just specify usb devices, like:
usbdev=['1.6']
Then it will create a USB controller automatically and attach the USB device to the first available USB controller:port.
or, you can explicitly specify usb controllers and usb devices, like:
usbctrl=['verison=1, ports=4', 'version=2, ports=8', ] usbdev=['1.6, controller=0, port=1']
Then it will create two USB controllers as you specified. And if controller and port are specified in usb config, then it will attach the USB device to that controller:port. About the controller and port value:
Each USB controller has a index (or called devid) based on 0. The 1st controller has index 0, the 2nd controller has index 1, ... Under controller, each port has a port number based on 1. In above configuration, the 1st controller will have port 1,2,3,4.
* Hot-Plug USB device
To attach a USB device, you should first create a USB controller. e.g.
xl usb-ctrl-attach domain [version=1|2] [ports=value] By default, it will create a USB2.0 controller with 8 ports.
Then you could attach a USB device.
e.g.
xl usb-attach domain 1.6 [controller=index port=number] By default, it will find the 1st available controller:port to attach the USB device.
You could view USB device status of the domain by usb-list. e.g.
xl usb-list domain
It will list USB controllers and USB devices under each controller.
You could detach a USB device with usb-detach command. e.g.
xl usb-detach domain 1.6
You can also remove the whole USB controller by usb-ctrl-detach command.
e.g.
xl usb-ctrl-detach domain 0
It will remove the USB controller with index 0 and all USB devices under it.
4. PVUSB Libxl implementation
* usb-ctrl-attach
To create a usb controller, we need:
1) generate usb controler related information
2) write usb controller frontend/backend info to xenstore PVUSB frontend and backend driver will probe xenstore paths and build connection between frontend and backend.
* usb-ctrl-detach
To remove a usb controller, we need:
1) check if the usb controller exists or not
2) remove all usb devices under controller
3) remove usb controller info from xenstore
* usb-attach
To attach a usb device, we need:
1) check if the usb device type is assignable
2) check if the usb device is already assigned to a domain
3) add 'busid' of the usb device to xenstore contoller/port/. PVUSB driver watches the xenstore changes and detects that, and needs to use 'busid' to do following work.
4) unbind usb device from original driver and bind to usbback. If usb device has many interfaces, then:
- unbind each interface from its original driver and bind to usbback.
- store the original driver to xenstore for later rebinding when detaching the device.
* usb-detach
To detach a usb device, we need:
1) check if the usb device is assigned to the domain
2) remove the usb device from xenstore controller/port.
3) unbind usb device from usbback and rebind to its original driver. If usb device has many interfaces, do it to each interface.
* usb-list
List all USB controllers and USB devices under each controller.
5. PVUSB xenstore information
PVUSB xenstore information includes three parts: frontend, backend and /libxl part.
A USB controller is corresponding to a "vusb" device in xenstore. Adding a USB controller will add a new "vusb" device, removing a USB controller will delete the related "vusb" device.
Following is an example xenstore values of a USB controller. Backend:
backend = ""
vusb = ""
1 = ""
0 = ""
frontend = "/local/domain/1/device/vusb/0"
frontend-id = "1"
online = "1"
state = "4"
type = "pv"
usb-ver = "1"
num-ports = "4"
port = ""
1 = ""
2 = ""
3 = ""
4 = ""Frontend:
device = ""
vusb = ""
0 = ""
backend = "/local/domain/0/backend/vusb/1/0"
backend-id = "0"
state = "4"
urb-ring-ref = "348"
conn-ring-ref = "346"
event-channel = "20"Adding a USB device won't create a new "vusb" device, but only write the USB device busid to one port of USB controller.
For example, attaching a USB device (busid is 2-1.6) to above USB controller port 1, it only need write 2-1.6 to port 1 of this USB controller:
Backend:
backend = ""
vusb = ""
1 = ""
0 = ""
frontend = "/local/domain/1/device/vusb/0"
frontend-id = "1"
online = "1"
state = "4"
type = "pv"
usb-ver = "1"
num-ports = "4"
port = ""
1 = "2-1.6"
2 = ""
3 = ""
4 = ""Frontend doesn't change.
Since assign a host USB device to guest, we'll unbind USB interfaces from their original drivers and bind them to usbback. After detaching this USB device from guest, one would hope the USB interfaces could be rebind to their original drivers, so there should some place to get the original driver info. To support that, when attaching a USB device to guest, we'll save the original driver info in xenstore too, the place is /libxl/usbback, for example:
libxl = ""
1 = ""
dm-version = "qemu_xen"
usbback = ""
3-11 = ""
3-11@1_0 = ""
driver_path = "/sys/bus/usb/drivers/btusb"
In this example, USB device (busid is 3-11, /sys/bus/usb/devices/3-11). It has interface 3-11:1.0, whose original dirver is btusb. Since xenstore doesn't allow ':' and '.' in a key, so we encode the interface by changing ':' to '@' and changing '.' to '_'.
When detaching the USB device from guest, we can rebind 3-11:1.0 to btusb driver.
Chunyan Liu (7):
libxl: export some functions for pvusb use
libxl_utils: add internal function to read sysfs file contents
libxl: add pvusb API
libxl: add libxl_device_usb_assignable_list API
xl: add pvusb commands
xl: add usb-assignable-list command
domcreate: support pvusb in configuration file
docs/man/xl.cfg.pod.5 | 84 ++ docs/man/xl.pod.1 | 41 + tools/libxl/Makefile | 2 +- tools/libxl/libxl.c | 55 +- tools/libxl/libxl.h | 77 ++ tools/libxl/libxl_create.c | 73 +- tools/libxl/libxl_device.c | 9 +- tools/libxl/libxl_internal.h | 34 + tools/libxl/libxl_osdeps.h | 13 + tools/libxl/libxl_pvusb.c | 1584 ++++++++++++++++++++++++++++++++++ tools/libxl/libxl_types.idl | 46 + tools/libxl/libxl_types_internal.idl | 1 + tools/libxl/libxl_utils.c | 95 ++ tools/libxl/libxl_utils.h | 5 + tools/libxl/xl.h | 6 + tools/libxl/xl_cmdimpl.c | 322 ++++++- tools/libxl/xl_cmdtable.c | 29 + 17 files changed, 2466 insertions(+), 10 deletions(-) create mode 100644 tools/libxl/libxl_pvusb.c
Signature · 1 line
2.1.4
PATCH v9 1/7 libxl: export some functions for pvusb use
---
Changes:
* fix indentation
tools/libxl/libxl.c | 5 ++--- tools/libxl/libxl_internal.h | 3 +++ 2 files changed, 5 insertions(+), 3 deletions(-)
tools/libxl/libxl.c +2 -3
@@ -2035,7 +2035,7 @@ out: }/* common function to get next device id */ -static int libxl__device_nextid (libxl__gc *gc,uint32_t domid,char *device) +int libxl__device_nextid (libxl__gc *gc,uint32_t domid,char *device) {char *dompath, **l;unsigned int nb;
@@ -2054,8 +2054,7 @@ static int libxl__device_nextid(libxl__gc *gc, uint32_t domid, char *device)return nextid; } -static int libxl__resolve_domid (libxl__gc *gc,const char *name, -uint32_t *domid) +int libxl__resolve_domid (libxl__gc *gc,const char *name,uint32_t *domid) {if (!name)return 0 ;
tools/libxl/libxl_internal.h +3 -0
@@ -1165,6 +1165,9 @@ _hidden int libxl__init_console_from_channel(libxl__gc *gc,libxl__device_console *console, int dev_num, libxl_device_channel *channel); +_hidden int libxl__device_nextid(libxl__gc *gc, uint32_t domid, char *device); +_hidden int libxl__resolve_domid(libxl__gc *gc, const char *name, +uint32_t *domid);/* * For each aggregate type which can be used as an input we provide:
Signature · 1 line
2.1.4
PATCH v9 2/7 libxl_utils: add internal function to read sysfs file contents
In reply to Chunyan LiuAdd a new function libxl_read_sysfs_file_contents to handle sysfs file specially. It would be used in later pvusb work.
---
Changes:
- write a separate function libxl__read_sysfs_file_contents, no longer mix with libxl_read_file_contents
tools/libxl/libxl_internal.h | 5 +++ tools/libxl/libxl_utils.c | 77 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+)
tools/libxl/libxl_internal.h +5 -0
@@ -4021,6 +4021,11 @@ void libxl__bitmap_copy_best_effort(libxl__gc *gc, libxl_bitmap *dptr, const libxl_bitmap *sptr);int libxl__count_physical_sockets (libxl__gc *gc,int *sockets); + +_hidden int libxl__read_sysfs_file_contents(libxl__gc *gc, +const char *filename, +void **data_r, +int *datalen_r);#endif /*
tools/libxl/libxl_utils.c +77 -0
83 lines not on this page · raw message
Signature · 1 line
2.1.4
PATCH v9 3/7 libxl: add pvusb API
In reply to Chunyan LiuAdd pvusb APIs, including:
- attach/detach (create/destroy) virtual usb controller.
- attach/detach usb device
- list usb controller and usb devices
- some other helper functions
---
changes:
- update DEFINE_DEVICE_REMOVE instead of creating a new DEFINE_DEVICE_REMOVE_EXT
- use libxl__xs_read_checked instead of libxl__xs_read
- update local READ_SUBPATH(_INT) macros to include more common codes
- save drvpath before unbind
- get_assigned_devices: call libxl__device_usb_list_for_ctrl instead of doing all things from scratch
- usb_interface_xenstore_encode: use special char to avoid confusion
- usb readdir_r instead of readdir
- check syscall errno
- remove usbinfo definition
- address other comments except:
libxl__device_usb_add/remove and do_usb_add/remove, in previous discussion, we'd like to get usbctrlinfo once and pass that as parameter to do_usb_add/remove. However, during update, found that in adding usb process, still need to try twice to get usbctrlinfo. (Before set_default, if usbctrl doesn't exist it doesn't doing getting usbctrlinfo actually; after set_default, needs to get usbctrlinfo then). So, finally, just change codes to make adding/removing process symmetrical by getting usbctrlinfo twice.
tools/libxl/Makefile | 2 +- tools/libxl/libxl.c | 50 +- tools/libxl/libxl.h | 74 ++ tools/libxl/libxl_device.c | 5 +- tools/libxl/libxl_internal.h | 18 + tools/libxl/libxl_osdeps.h | 13 + tools/libxl/libxl_pvusb.c | 1523 ++++++++++++++++++++++++++++++++++ tools/libxl/libxl_types.idl | 46 + tools/libxl/libxl_types_internal.idl | 1 + tools/libxl/libxl_utils.c | 18 + tools/libxl/libxl_utils.h | 5 + 11 files changed, 1752 insertions(+), 3 deletions(-) create mode 100644 tools/libxl/libxl_pvusb.c
tools/libxl/Makefile +1 -1
@@ -103,7 +103,7 @@ LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o libxl_pci.o \ libxl_stream_read.o libxl_stream_write.o \ libxl_save_callout.o _libxl_save_msgs_callout.o \ libxl_qmp.o libxl_event.o libxl_fork.o \ - libxl_dom_suspend.o $(LIBXL_OBJS-y) + libxl_dom_suspend.o libxl_pvusb.o $(LIBXL_OBJS-y) LIBXL_OBJS += libxl_genid.o LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o
tools/libxl/libxl.c +49 -1
92 lines not on this page · raw message
tools/libxl/libxl.h +74 -0
86 lines not on this page · raw message
tools/libxl/libxl_device.c +4 -1
@@ -676,7 +676,10 @@ void libxl__devices_destroy(libxl__egc *egc, libxl__devices_remove_state *drs) aodev->action =LIBXL__DEVICE_ACTION_REMOVE ; aodev->dev = dev; aodev->force = drs->force; -libxl__initiate_device_remove (egc, aodev); +if (dev->backend_kind ==LIBXL__DEVICE_KIND_VUSB ) +libxl__initiate_device_usbctrl_remove (egc, aodev); +else +libxl__initiate_device_remove (egc, aodev); } } }
tools/libxl/libxl_internal.h +18 -0
@@ -2569,6 +2569,14 @@ _hidden void libxl__device_vtpm_add(libxl__egc *egc, uint32_t domid, libxl_device_vtpm *vtpm, libxl__ao_device *aodev); +_hidden void libxl__device_usbctrl_add(libxl__egc *egc, uint32_t domid, +libxl_device_usbctrl *usbctrl, +libxl__ao_device *aodev); + +_hidden void libxl__device_usb_add(libxl__egc *egc, uint32_t domid, +libxl_device_usb *usb, +libxl__ao_device *aodev); +/* Internal function to connect a vkb device */ _hidden int libxl__device_vkb_add(libxl__gc *gc, uint32_t domid,libxl_device_vkb *vkb);
@@ -2601,6 +2609,13 @@ _hidden void libxl__wait_device_connection(libxl__egc*, _hidden void libxl__initiate_device_remove(libxl__egc *egc,libxl__ao_device *aodev); +_hidden int libxl__device_from_usbctrl(libxl__gc *gc, uint32_t domid, +libxl_device_usbctrl *usbctrl, +libxl__device *device); + +_hidden void libxl__initiate_device_usbctrl_remove(libxl__egc *egc, +libxl__ao_device *aodev); +/* * libxl__get_hotplug_script_info returns the args and env that should * be passed to the hotplug script for the requested device.
@@ -3958,6 +3973,9 @@ static inline void libxl__update_config_vtpm(libxl__gc *gc,#define COMPARE_PCI (a, b) ((a)->func == (b)->func && \ (a)->bus == (b)->bus && \ (a)->dev == (b)->dev) +#define COMPARE_USB (a, b) ((a)->ctrl == (b)->ctrl && \ + (a)->port == (b)->port) +#define COMPARE_USBCTRL (a, b) ((a)->devid == (b)->devid)/* DEVICE_ADD *
tools/libxl/libxl_osdeps.h +13 -0
@@ -24,6 +24,8 @@#define _GNU_SOURCE#if defined(__NetBSD__) +#define SYSFS_USB_DEV "/sys/bus/usb/devices" +#define SYSFS_USBBACK_DRIVER "/kern/xen/usb"#define SYSFS_PCI_DEV "/sys/bus/pci/devices"#define SYSFS_PCIBACK_DRIVER "/kern/xen/pci"#define NETBACK_NIC_NAME "xvif%ui%d"
@@ -31,6 +33,8 @@#elif defined(__OpenBSD__)#include <util.h> #elif defined(__linux__) +#define SYSFS_USB_DEV "/sys/bus/usb/devices" +#define SYSFS_USBBACK_DRIVER "/sys/bus/usb/drivers/usbback"#define SYSFS_PCI_DEV "/sys/bus/pci/devices"#define SYSFS_PCIBACK_DRIVER "/sys/bus/pci/drivers/pciback"#define NETBACK_NIC_NAME "vif%u.%d"
@@ -38,6 +42,8 @@#elif defined(__sun__)#include <stropts.h> #elif defined(__FreeBSD__) +#define SYSFS_USB_DEV "/dev/null" +#define SYSFS_USBBACK_DRIVER "/dev/null"#define SYSFS_PCI_DEV "/dev/null"#define SYSFS_PCIBACK_DRIVER "/dev/null"#define NETBACK_NIC_NAME "xnb%u.%d"
@@ -45,6 +51,13 @@#include <sys/endian.h> #endif +#ifndef SYSFS_USBBACK_DRIVER +#error define SYSFS_USBBACK_DRIVER for your platform +#endif +#ifndef SYSFS_USB_DEV +#error define SYSFS_USB_DEV for your platform +#endif +#ifndef SYSFS_PCIBACK_DRIVER #error define SYSFS_PCIBACK_DRIVER for your platform#endif
tools/libxl/libxl_pvusb.c new file +1523 -0
1,523 lines not on this page · raw message
tools/libxl/libxl_types.idl +46 -0
64 lines not on this page · raw message
tools/libxl/libxl_types_internal.idl +1 -0
@@ -22,6 +22,7 @@ libxl__device_kind = Enumeration("device_kind", [ (6, "VKBD"), (7, "CONSOLE"), (8, "VTPM"), + (9, "VUSB"), ]) libxl__console_backend = Enumeration("console_backend", [
tools/libxl/libxl_utils.c +18 -0
@@ -1314,6 +1314,24 @@ int libxl__random_bytes(libxl__gc *gc, uint8_t *buf, size_t len)return ret; } +void libxl_device_usbctrl_list_free (libxl_device_usbctrl *list,int nr) +{ +int i; + +for (i =0 ; i < nr; i++) +libxl_device_usbctrl_dispose (&list[i]); +free (list); +} + +void libxl_device_usb_list_free (libxl_device_usb *list,int nr) +{ +int i; + +for (i =0 ; i < nr; i++) +libxl_device_usb_dispose (&list[i]); +free (list); +} +/* * Local variables: * mode: C
tools/libxl/libxl_utils.h +5 -0
@@ -76,6 +76,11 @@ int libxl_uuid_to_device_vtpm(libxl_ctx *ctx, uint32_t domid, libxl_uuid *uuid, libxl_device_vtpm *vtpm);int libxl_devid_to_device_vtpm (libxl_ctx *ctx,uint32_t domid,int devid,libxl_device_vtpm *vtpm); +int libxl_devid_to_device_usbctrl (libxl_ctx *ctx,uint32_t domid, +int devid,libxl_device_usbctrl *usbctrl); +int libxl_ctrlport_to_device_usb (libxl_ctx *ctx,uint32_t domid, +int ctrl,int port, +libxl_device_usb *usb);int libxl_bitmap_alloc (libxl_ctx *ctx,libxl_bitmap *bitmap,int n_bits);/* Allocated bimap is from malloc, libxl_bitmap_dispose() to be
Signature · 1 line
2.1.4
PATCH v9 4/7 libxl: add libxl_device_usb_assignable_list API
In reply to Chunyan LiuAdd API for listing assignable USB devices info. Assignable USB device means the USB device type is assignable and it's not assigned to any guest yet.
---
This could be squashed with previous patch. Split because there is some dispute on this. If this is acceptable, could be squashed, otherwise could be removed.
tools/libxl/libxl.h | 3 +++ tools/libxl/libxl_pvusb.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+)
tools/libxl/libxl.h +3 -0
@@ -1472,6 +1472,9 @@ int libxl_device_usbctrl_getinfo(libxl_ctx *ctx, uint32_t domid, libxl_usbctrlinfo *usbctrlinfo);/* USB Devices */ +libxl_device_usb * +libxl_device_usb_assignable_list (libxl_ctx *ctx,int *num); + int libxl_device_usb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_usb *usb, const libxl_asyncop_how *ao_how) LIBXL_EXTERNAL_CALLERS_ONLY;
tools/libxl/libxl_pvusb.c +61 -0
67 lines not on this page · raw message
Signature · 1 line
2.1.4
PATCH v9 5/7 xl: add pvusb commands
In reply to Chunyan LiuAdd pvusb commands: usb-ctrl-attach, usb-ctrl-detach, usb-list, usb-attach and usb-detach.
To attach a usb device to guest through pvusb, one could follow following example:
#xl usbctrl-attach test_vm version=1 ports=8
#xl usb-list test_vm
will show the usb controllers and port usage under the domain.
#xl usb-attach test_vm hostbus=1 hostaddr=2
will find the first usable controller:port, and attach usb device whose busnum is 1 and devnum is 6.
One could also specify which <controller> and which <port>.
#xl usb-detach test_vm 0 1
will detach USB device under controller 0 port 1.
#xl usbctrl-detach test_vm dev_id
will destroy the controller with specified dev_id. Dev_id can be traced in usb-list info.
---
Changes:
- use libxl_usbdev/usbctrl_type_from_string instead of comparing oparg string mannually.
- update docs as George suggested
docs/man/xl.pod.1 | 41 ++++++++ tools/libxl/xl.h | 5 + tools/libxl/xl_cmdimpl.c | 241 ++++++++++++++++++++++++++++++++++++++++++++++ tools/libxl/xl_cmdtable.c | 25 +++++ 4 files changed, 312 insertions(+)
docs/man/xl.pod.1 +41 -0
@@ -1345,6 +1345,47 @@ List pass-through pci devices for a domain. =back +=head1 USB PASS-THROUGH + +=over 4 + +=item B<usbctrl-attach> I<domain-id> I[<type=val>] [I<version=val>] [I<ports=number>] + +Create a new USB controller for the specified domain. +B<type=val> is the usb controller type. Currently only 'pv' and 'auto' +are supported. +B<version=val> is the usb controller version. Possible values include +1 (USB1.1) and 2 (USB2.0). +B<ports=number> is the total ports of the usb controller. The maximum +number is 31. +By default, it will create a USB2.0 controller with 8 ports. + +=item B<usbctrl-detach> I<domain-id> I<devid> + +Destroy a USB controller from the specified domain. +B<devid> is devid of the USB controller. + +=item B<usb-attach> I<domain-id> I<hostbus=busnum> I<hostaddr=devnum> [I<controller=devid> [I<port=number>]] + +Hot-plug a new pass-through USB device to the specified domain. +B<hostbus> and B<hostaddr> are the bus and device number of the physical +USB device to pass through. +B<controller=devid> B<port=number> is the USB controller:port to hotplug the +USB device to. By default, it will find the first available controller:port +and use it; if there is no controller, a new controller will be created. + +=item B<usb-detach> I<domain-id> I<controller=devid> I<port=number> + +Hot-unplug a previously assigned USB device from a domain. +B<controller=devid> and B<port=number> is USB controller:port in guest where the +USB device is attached to. + +=item B<usb-list> I<domain-id> + +List pass-through usb devices for a domain. + +=back + =head1 TMEM =over 4
tools/libxl/xl.h +5 -0
@@ -85,6 +85,11 @@ int main_blockdetach(int argc, char **argv);int main_vtpmattach (int argc,char **argv);int main_vtpmlist (int argc,char **argv);int main_vtpmdetach (int argc,char **argv); +int main_usbctrl_attach (int argc,char **argv); +int main_usbctrl_detach (int argc,char **argv); +int main_usbattach (int argc,char **argv); +int main_usbdetach (int argc,char **argv); +int main_usblist (int argc,char **argv);int main_uptime (int argc,char **argv);int main_claims (int argc,char **argv);int main_tmem_list (int argc,char **argv);
tools/libxl/xl_cmdimpl.c +241 -0
253 lines not on this page · raw message
tools/libxl/xl_cmdtable.c +25 -0
@@ -551,6 +551,31 @@ struct cmd_spec cmd_table[] = { },#endif + {"usbctrl-attach" , + &main_usbctrl_attach,0 ,1 , +"Create a virtual USB controller for a domain" , +"<Domain> [type=pv] [version=<version>] [ports=<number>]" , + }, + { "usbctrl-detach", + &main_usbctrl_detach,0 ,1 , +"Remove the virtual USB controller specified by <DevId> for a domain" , +"<Domain> <DevId>" , + }, + { "usb-attach", + &main_usbattach,0 ,1 , +"Attach a USB device to a domain" , +"<Domain> hostbus=<busnum> hostaddr=<devnum> [controller=<DevId> [port=<port>]]" , + }, + { "usb-detach", + &main_usbdetach,0 ,1 , +"Detach a USB device from a domain" , +"<Domain> <controller> <port>" , + }, + { "usb-list", + &main_usblist,0 ,0 , +"List information about USB devices for a domain" , + "<Domain>", + }, };int cmdtable_len =sizeof (cmd_table)/sizeof (struct cmd_spec );
Signature · 1 line
2.1.4
PATCH v9 6/7 xl: add usb-assignable-list command
In reply to Chunyan LiuAdd xl usb-assignable-list command to list assignable USB devices. Assignable USB device means the USB device type is assignable and it's not assigned to any guest yet.
---
Same as "libxl: add libxl_device_usb_assignable_list API" patch, this patch could be sqaushed to previous one. Split because of some dispute. Could be squashed if acceptable, otherwise could be removed.
tools/libxl/xl.h | 1 + tools/libxl/xl_cmdimpl.c | 27 +++++++++++++++++++++++++++ tools/libxl/xl_cmdtable.c | 4 ++++ 3 files changed, 32 insertions(+)
tools/libxl/xl.h +1 -0
@@ -85,6 +85,7 @@ int main_blockdetach(int argc, char **argv);int main_vtpmattach (int argc,char **argv);int main_vtpmlist (int argc,char **argv);int main_vtpmdetach (int argc,char **argv); +int main_usbassignable_list (int argc,char **argv);int main_usbctrl_attach (int argc,char **argv);int main_usbctrl_detach (int argc,char **argv);int main_usbattach (int argc,char **argv);
tools/libxl/xl_cmdimpl.c +27 -0
@@ -3448,6 +3448,33 @@ int main_cd_insert(int argc, char **argv)return 0 ; } +static void usb_assignable_list (void ) +{ +libxl_device_usb *usbs; +int num, i; + + usbs =libxl_device_usb_assignable_list (ctx, &num); + +for (i =0 ; i < num; i++) { +printf ("%d.%d\n" , usbs[i].u.hostdev.hostbus, + usbs[i].u.hostdev.hostaddr); + } + +libxl_device_usb_list_free (usbs, num); +} + +int main_usbassignable_list (int argc,char **argv) +{ +int opt; + + SWITCH_FOREACH_OPT(opt, "", NULL, "usb-assignable-list", 0) { +/* No options */ + } + +usb_assignable_list (); +return 0 ; +} +int main_usbctrl_attach (int argc,char **argv) {uint32_t domid;
tools/libxl/xl_cmdtable.c +4 -0
@@ -576,6 +576,10 @@ struct cmd_spec cmd_table[] = { "List information about USB devices for a domain", "<Domain>", }, + { "usb-assignable-list", + &main_usbassignable_list, 0, 0, + "List all assignable USB devices", + }, };int cmdtable_len =sizeof (cmd_table)/sizeof (struct cmd_spec );
Signature · 1 line
2.1.4
PATCH v9 7/7 domcreate: support pvusb in configuration file
In reply to Chunyan LiuAdd code to support pvusb in domain config file. One could specify usbctrl and usb in domain's configuration file and create domain, then usb controllers will be created and usb device would be attached to guest automatically.
One could specify usb controllers and usb devices in config file like this:
usbctrl=['version=2,ports=4', 'version=1, ports=4', ] usbdev=['hostbus=2, hostaddr=1, controller=0,port=1', ]
---
changes:
- doc update
Not changed in this version:
- naming 'usbdev' or 'usb' for usb device.
If decided, all places including libxl API and all in this patch should be changed all together. See how we decide.
docs/man/xl.cfg.pod.5 | 84 ++++++++++++++++++++++++++++++++++++++++++++ tools/libxl/libxl_create.c | 73 ++++++++++++++++++++++++++++++++++++-- tools/libxl/libxl_device.c | 4 +++ tools/libxl/libxl_internal.h | 8 +++++ tools/libxl/xl_cmdimpl.c | 54 +++++++++++++++++++++++++++- 5 files changed, 219 insertions(+), 4 deletions(-)
docs/man/xl.cfg.pod.5 +84 -0
@@ -722,6 +722,90 @@ Note this may be overridden by rdm_policy option in PCI device configuration. =back +=item B<usbctrl=[ "USBCTRL_SPEC_STRING", "USBCTRL_SPEC_STRING", ... ]> + +Specifies the USB controllers created for this guest. Each +B<USB_SPEC_STRING> has the form C<KEY=VALUE,KEY=VALUE,...> where: + +=over 4 + +=item B<KEY=VALUE> + +Possible B<KEY>s are: + +=over 4 + +=item B<type=TYPE> + +Specifies the usb controller type. Currently only 'pv' and 'auto' +are supported. + +=item B<version=VERSION> + +Specifies the usb controller version. Possible values include +1 (USB1.1) and 2 (USB2.0). Default is 2 (USB2.0). + +=item B<ports=PORTS> + +Specifies the total ports of the usb controller. The maximum +number is 31. Default is 8. + +USB controler ids start from 0. In line with the USB spec, however, +ports on a controller start from 1. + +E.g. +usbctrl=["version=1,ports=4", "version=2,ports=8",] +The first controller has: +controller id = 0, and port 1,2,3,4. +The second controller has: +controller id = 1, and port 1,2,3,4,5,6,7,8. + +=back + +=back + +=item B<usbdev=[ "USB_SPEC_STRING", "USB_SPEC_STRING", ... ]> + +Specifiec the USB devices to be attached to the guest at boot. Each +B<USB_SPEC_STRING> has the form C<KEY=VALUE,KEY=VALUE,...> where: + +=over 4 + +=item B<KEY=VALUE> + +Possible B<KEY>s are: + +=over 4 + +=item B<devtype=hostdev> + +Specifies USB device type. Currently only support 'hostdev'. + +=item B<hostbus=busnum> + +Specifies busnum of the USB device from the host perspective. + +=item B<hostaddr=devnum> + +Specifies devnum of the USB device from the host perspective. + +=item B<controller=CONTROLLER> + +Specifies USB controller id, to which controller the USB device is attached. + +=item B<port=PORT> + +Specifies USB port, to which port the USB device is attached. B<port=PORT> +is valid only when B<controller=CONTROLLER> is specified. + +=back + +If no controller is specified, an available controller:port combination +will be used. If there are no available controller:port options, +a new controller will be created. + +=back + =item B<pci=[ "PCI_SPEC_STRING", "PCI_SPEC_STRING", ... ]> Specifies the host PCI devices to passthrough to this guest. Each B<PCI_SPEC_STRING>
tools/libxl/libxl_create.c +70 -3
102 lines not on this page · raw message
tools/libxl/libxl_device.c +4 -0
@@ -544,6 +544,8 @@ void libxl__multidev_prepared(libxl__egc *egc,* libxl__add_disks * libxl__add_nics * libxl__add_vtpms +* libxl__add_usbctrls +* libxl__add_usbs */ #define DEFINE_DEVICES_ADD (type) \
@@ -563,6 +565,8 @@ void libxl__multidev_prepared(libxl__egc *egc, DEFINE_DEVICES_ADD(disk) DEFINE_DEVICES_ADD(nic) DEFINE_DEVICES_ADD(vtpm) +DEFINE_DEVICES_ADD(usbctrl) +DEFINE_DEVICES_ADD(usb) #undef DEFINE_DEVICES_ADD
tools/libxl/libxl_internal.h +8 -0
@@ -3302,6 +3302,14 @@ _hidden void libxl__add_vtpms(libxl__egc *egc, libxl__ao *ao, uint32_t domid, libxl_domain_config *d_config, libxl__multidev *multidev); +_hidden void libxl__add_usbctrls(libxl__egc *egc, libxl__ao *ao, +uint32_t domid,libxl_domain_config *d_config, +libxl__multidev *multidev); + +_hidden void libxl__add_usbs(libxl__egc *egc, libxl__ao *ao, +uint32_t domid,libxl_domain_config *d_config, +libxl__multidev *multidev); +/*----- device model creation -----*/ /* First layer; wraps libxl__spawn_spawn. */
tools/libxl/xl_cmdimpl.c +53 -1
66 lines not on this page · raw message
Signature · 1 line
2.1.4
According to current active discussion:
libxl: Introduce a template for devices with a controller
https://www.mail-archive.com/[email protected]/msg46720.html
Will update naming and RESEND.
- Chunyan
Quoted 1 line
Quoted 1 line
Quoted 1 line
On 11/24/2015 at 04:35 PM, in message
<[email protected]>, Chunyan Liu
Quoted 300 lines · <[email protected]> wrote:
This patch series is to add pvusb toolstack work, supporting hot add|remove USB device to|from guest and specify USB device in domain configuration file.
Changes to V8:
* lots of changes in libxl pvusb API (patch 3/7)
* update 2/7 to write separateread_sysfs_filefunction
* address all other commentsV8:
http://lists.xen.org/archives/html/xen-devel/2015-10/msg02178.htmlV7:
http://lists.xen.org/archives/html/xen-devel/2015-09/msg03115.htmlV6:
http://lists.xen.org/archives/html/xen-devel/2015-08/msg00750.htmlV5:
http://lists.xen.org/archives/html/xen-devel/2015-06/msg04052.htmlV4:
http://lists.xenproject.org/archives/html/xen-devel/2015-06/msg01327.htmlRelated Discussion Threads:
http://www.redhat.com/archives/libvir-list/2014-June/msg00038.html
http://lists.xen.org/archives/html/xen-devel/2014-06/msg00086.html<<< pvusb work introduction >>>
1. Overview
There are two general methods for passing through individual host devices to a guest. The first is via an emulated USB device controller; the second is PVUSB.
Additionally, there are two ways to add USB devices to a guest: via the config file at domain creation time, and via hot-plug while the VM is running.
* Emulated USB
In emulated USB, the device model (qemu) presents an emulated USB controller to the guest. The device model process then grabs control of the device from domain 0 and and passes the USB commands between the guest OS and the host USB device.
This method is only available to HVM domains, and is not available for domains running with device model stubdomains.
* PVUSB
PVUSB uses a paravirtialized front-end/back-end interface, similar to the traditional Xen PV network and disk protocols. In order to use PVUSB, you need usbfront in your guest OS, and usbback in dom0 (or your USB driver domain).
2. Specifying a host USB device
QEMU qmp commands allows USB devices to be specified either by their bus address (in the form bus.device) or their device tag (in the form vendorid:deviceid).
Each way of specifying has its advantages:
Specifying by device tag will always get the same device,
regardless of where the device ends up in the USB bus topology. However, if there are two identical devices, it will not allow you to specify which one.Specifying by bus address will always allow you to choose a
specific device, even if you have duplicates. However, the bus address may change depending on which port you plugged the device into, and possibly also after a reboot.To avoid duplication of vendorid:deviceid, we'll use bus address to specify host USB device in xl toolstack.
You can use lsusb to list the USB devices on the system:
Bus 001 Device 003: ID 0424:2514 Standard Microsystems Corp. USB 2.0 Hub
Bus 003 Device 002: ID f617:0905
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 001 Device 004: ID 0424:2640 Standard Microsystems Corp. USB 2.0 Hub
Bus 001 Device 005: ID 0424:4060 Standard Microsystems Corp. Ultra Fast Media Reader
Bus 001 Device 006: ID 046d:c016 Logitech, Inc. Optical Wheel MouseTo pass through the Logitec mouse, for instance, you could specify 1.6 (remove leading zeroes).
Note: USB hubs can not be assigned to guest.
3. PVUSB toolstack
* Specify USB device in xl config file
You can just specify usb devices, like:
usbdev=['1.6']Then it will create a USB controller automatically and attach the USB device to the first available USB controller:port.
or, you can explicitly specify usb controllers and usb devices, like:
usbctrl=['verison=1, ports=4', 'version=2, ports=8', ] usbdev=['1.6, controller=0, port=1']Then it will create two USB controllers as you specified. And if controller and port are specified in usb config, then it will attach the USB device to that controller:port. About the controller and port value:
Each USB controller has a index (or called devid) based on 0. The 1st controller has index 0, the 2nd controller has index 1, ... Under controller, each port has a port number based on 1. In above configuration, the 1st controller will have port 1,2,3,4.* Hot-Plug USB device
To attach a USB device, you should first create a USB controller. e.g.
xl usb-ctrl-attach domain [version=1|2] [ports=value] By default, it will create a USB2.0 controller with 8 ports.Then you could attach a USB device.
e.g.
xl usb-attach domain 1.6 [controller=index port=number] By default, it will find the 1st available controller:port to attach the USB device.You could view USB device status of the domain by usb-list. e.g.
xl usb-list domain
It will list USB controllers and USB devices under each controller.You could detach a USB device with usb-detach command. e.g.
xl usb-detach domain 1.6You can also remove the whole USB controller by usb-ctrl-detach command.
e.g.
xl usb-ctrl-detach domain 0
It will remove the USB controller with index 0 and all USB devices under it.4. PVUSB Libxl implementation
* usb-ctrl-attach
To create a usb controller, we need:
1) generate usb controler related information
2) write usb controller frontend/backend info to xenstore PVUSB frontend and backend driver will probe xenstore paths and build connection between frontend and backend.* usb-ctrl-detach
To remove a usb controller, we need:
1) check if the usb controller exists or not
2) remove all usb devices under controller
3) remove usb controller info from xenstore* usb-attach
To attach a usb device, we need:
1) check if the usb device type is assignable
2) check if the usb device is already assigned to a domain
3) add 'busid' of the usb device to xenstore contoller/port/. PVUSB driver watches the xenstore changes and detects that, and needs to use 'busid' to do following work.
4) unbind usb device from original driver and bind to usbback. If usb device has many interfaces, then:
- unbind each interface from its original driver and bind to usbback.
- store the original driver to xenstore for later rebinding when detaching the device.* usb-detach
To detach a usb device, we need:
1) check if the usb device is assigned to the domain
2) remove the usb device from xenstore controller/port.
3) unbind usb device from usbback and rebind to its original driver. If usb device has many interfaces, do it to each interface.* usb-list
List all USB controllers and USB devices under each controller.5. PVUSB xenstore information
PVUSB xenstore information includes three parts: frontend, backend and /libxl part.
A USB controller is corresponding to a "vusb" device in xenstore. Adding a USB controller will add a new "vusb" device, removing a USB controller will delete the related "vusb" device.
Following is an example xenstore values of a USB controller. Backend:
backend = "" vusb = "" 1 = "" 0 = "" frontend = "/local/domain/1/device/vusb/0" frontend-id = "1" online = "1" state = "4" type = "pv" usb-ver = "1" num-ports = "4" port = "" 1 = "" 2 = "" 3 = "" 4 = ""Frontend:
device = "" vusb = "" 0 = "" backend = "/local/domain/0/backend/vusb/1/0" backend-id = "0" state = "4" urb-ring-ref = "348" conn-ring-ref = "346" event-channel = "20"Adding a USB device won't create a new "vusb" device, but only write the USB device busid to one port of USB controller.
For example, attaching a USB device (busid is 2-1.6) to above USB controller port 1, it only need write 2-1.6 to port 1 of this USB controller:
Backend:backend = "" vusb = "" 1 = "" 0 = "" frontend = "/local/domain/1/device/vusb/0" frontend-id = "1" online = "1" state = "4" type = "pv" usb-ver = "1" num-ports = "4" port = "" 1 = "2-1.6" 2 = "" 3 = "" 4 = ""Frontend doesn't change.
Since assign a host USB device to guest, we'll unbind USB interfaces from their original drivers and bind them to usbback. After detaching this USB device from guest, one would hope the USB interfaces could be rebind to their original drivers, so there should some place to get the original driver info. To support that, when attaching a USB device to guest, we'll save the original driver info in xenstore too, the place is /libxl/usbback, for example:
libxl = ""
1 = ""
dm-version = "qemu_xen"
usbback = ""
3-11 = ""
3-11@1_0 = ""
driver_path= "/sys/bus/usb/drivers/btusb"In this example, USB device (busid is 3-11,
/sys/bus/usb/devices/3-11). It has interface 3-11:1.0, whose original dirver is btusb. Since xenstore doesn't allow ':' and '.' in a key, so we encode the interface by changing ':' to '@' and changing '.' to '_'.When detaching the USB device from guest, we can rebind 3-11:1.0 to btusb driver.
Chunyan Liu (7):
libxl: export some functions for pvusb use
libxl_utils: add internal function to read sysfs file contents
libxl: add pvusb API
libxl: addlibxl_device_usb_assignable_listAPI
xl: add pvusb commands
xl: add usb-assignable-list command
domcreate: support pvusb in configuration filedocs/man/xl.cfg.pod.5 | 84 ++ docs/man/xl.pod.1 | 41 + tools/libxl/Makefile | 2 +- tools/libxl/libxl.c | 55 +- tools/libxl/libxl.h | 77 ++ tools/libxl/libxl_create.c | 73 +- tools/libxl/libxl_device.c | 9 +- tools/libxl/libxl_internal.h | 34 + tools/libxl/libxl_osdeps.h | 13 + tools/libxl/libxl_pvusb.c | 1584 ++++++++++++++++++++++++++++++++++ tools/libxl/libxl_types.idl | 46 + tools/libxl/libxl_types_internal.idl | 1 + tools/libxl/libxl_utils.c | 95 ++ tools/libxl/libxl_utils.h | 5 + tools/libxl/xl.h | 6 + tools/libxl/xl_cmdimpl.c | 322 ++++++- tools/libxl/xl_cmdtable.c | 29 + 17 files changed, 2466 insertions(+), 10 deletions(-) create mode 100644 tools/libxl/libxl_pvusb.c
Quoted 5 lines · Chun Yan Liu
According to current active discussion:
libxl: Introduce a template for devices with a controller
https://www.mail-archive.com/[email protected]/msg46720.htmlWill update naming and RESEND.
Maybe give it a day or two to allow for review of that proposal, in case something changes?
Ian.
Quoted 310 lines
- Chunyan
Quoted 1 line
Quoted 1 line
Quoted 1 line
On 11/24/2015 at 04:35 PM, in message
<[email protected]>, Chunyan Liu
Quoted 302 lines · <[email protected]> wrote:
This patch series is to add pvusb toolstack work, supporting hot add|remove
USB device to|from guest and specify USB device in domain configuration
file.
Changes to V8:
* lots of changes in libxl pvusb API (patch 3/7)
* update 2/7 to write separateread_sysfs_filefunction
* address all other comments
V8:
http://lists.xen.org/archives/html/xen-devel/2015-10/msg02178.html
V7:
http://lists.xen.org/archives/html/xen-devel/2015-09/msg03115.html
V6:
http://lists.xen.org/archives/html/xen-devel/2015-08/msg00750.html
V5:
http://lists.xen.org/archives/html/xen-devel/2015-06/msg04052.html
V4:
http://lists.xenproject.org/archives/html/xen-devel/2015-06/msg01327.ht
ml
Related Discussion Threads:
http://www.redhat.com/archives/libvir-list/2014-June/msg00038.html
http://lists.xen.org/archives/html/xen-devel/2014-06/msg00086.html
<<< pvusb work introduction >>>
1. Overview
There are two general methods for passing through individual host devices to a guest. The first is via an emulated USB device controller; the second is PVUSB.
Additionally, there are two ways to add USB devices to a guest: via the config file at domain creation time, and via hot-plug while the VM is running.
* Emulated USB
In emulated USB, the device model (qemu) presents an emulated USB controller to the guest. The device model process then grabs control of the device from domain 0 and and passes the USB commands between the guest OS and the host USB device.
This method is only available to HVM domains, and is not available for domains running with device model stubdomains.
* PVUSB
PVUSB uses a paravirtialized front-end/back-end interface, similar to the traditional Xen PV network and disk protocols. In order to use PVUSB, you need usbfront in your guest OS, and usbback in dom0 (or your USB driver domain).
2. Specifying a host USB device
QEMU qmp commands allows USB devices to be specified either by their bus address (in the form bus.device) or their device tag (in the form vendorid:deviceid).
Each way of specifying has its advantages:
Specifying by device tag will always get the same device, regardless of where the device ends up in the USB bus topology. However, if there are two identical devices, it will not allow you to specify which one.
Specifying by bus address will always allow you to choose a specific device, even if you have duplicates. However, the bus address may change depending on which port you plugged the device into, and possibly also after a reboot.
To avoid duplication of vendorid:deviceid, we'll use bus address to specify host USB device in xl toolstack.
You can use lsusb to list the USB devices on the system:
Bus 001 Device 003: ID 0424:2514 Standard Microsystems Corp. USB 2.0 Hub
Bus 003 Device 002: ID f617:0905
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 001 Device 004: ID 0424:2640 Standard Microsystems Corp. USB 2.0 Hub
Bus 001 Device 005: ID 0424:4060 Standard Microsystems Corp. Ultra Fast Media Reader
Bus 001 Device 006: ID 046d:c016 Logitech, Inc. Optical Wheel Mouse
To pass through the Logitec mouse, for instance, you could specify 1.6 (remove leading zeroes).
Note: USB hubs can not be assigned to guest.
3. PVUSB toolstack
* Specify USB device in xl config file
You can just specify usb devices, like:
usbdev=['1.6']
Then it will create a USB controller automatically and attach the USB device to the first available USB controller:port.
or, you can explicitly specify usb controllers and usb devices, like:
usbctrl=['verison=1, ports=4', 'version=2, ports=8', ] usbdev=['1.6, controller=0, port=1']
Then it will create two USB controllers as you specified. And if controller and port are specified in usb config, then it will attach the USB device to that controller:port. About the controller and port value:
Each USB controller has a index (or called devid) based on 0. The 1st controller has index 0, the 2nd controller has index 1, ... Under controller, each port has a port number based on 1. In above configuration, the 1st controller will have port 1,2,3,4.
* Hot-Plug USB device
To attach a USB device, you should first create a USB controller. e.g.
xl usb-ctrl-attach domain [version=1|2] [ports=value]
By default, it will create a USB2.0 controller with 8 ports.
Then you could attach a USB device.
e.g.
xl usb-attach domain 1.6 [controller=index port=number]
By default, it will find the 1st available controller:port to attach the USB device.
You could view USB device status of the domain by usb-list. e.g.
xl usb-list domain
It will list USB controllers and USB devices under each controller.
You could detach a USB device with usb-detach command.
e.g.
xl usb-detach domain 1.6
You can also remove the whole USB controller by usb-ctrl-detach command.
e.g.
xl usb-ctrl-detach domain 0
It will remove the USB controller with index 0 and all USB devices under it.
4. PVUSB Libxl implementation
* usb-ctrl-attach
To create a usb controller, we need:
1) generate usb controler related information
2) write usb controller frontend/backend info to xenstore PVUSB frontend and backend driver will probe xenstore paths and build connection between frontend and backend.
* usb-ctrl-detach
To remove a usb controller, we need:
1) check if the usb controller exists or not
2) remove all usb devices under controller
3) remove usb controller info from xenstore
* usb-attach
To attach a usb device, we need:
1) check if the usb device type is assignable
2) check if the usb device is already assigned to a domain
3) add 'busid' of the usb device to xenstore contoller/port/. PVUSB driver watches the xenstore changes and detects that, and needs to use 'busid' to do following work.
4) unbind usb device from original driver and bind to usbback. If usb device has many interfaces, then:
- unbind each interface from its original driver and bind to usbback.
- store the original driver to xenstore for later rebinding when detaching the device.
* usb-detach
To detach a usb device, we need:
1) check if the usb device is assigned to the domain
2) remove the usb device from xenstore controller/port.
3) unbind usb device from usbback and rebind to its original driver. If usb device has many interfaces, do it to each interface.
* usb-list
List all USB controllers and USB devices under each controller.
5. PVUSB xenstore information
PVUSB xenstore information includes three parts: frontend, backend and /libxl part.
A USB controller is corresponding to a "vusb" device in xenstore. Adding a USB controller will add a new "vusb" device, removing a USB controller will delete the related "vusb" device.
Following is an example xenstore values of a USB controller. Backend:
backend = ""
vusb = ""
1 = ""
0 = ""
frontend = "/local/domain/1/device/vusb/0" frontend-id = "1"
online = "1"
state = "4"
type = "pv"
usb-ver = "1"
num-ports = "4"
port = ""
1 = ""
2 = ""
3 = ""
4 = ""
Frontend:
device = ""
vusb = ""
0 = ""
backend = "/local/domain/0/backend/vusb/1/0" backend-id = "0"
state = "4"
urb-ring-ref = "348"
conn-ring-ref = "346"
event-channel = "20"
Adding a USB device won't create a new "vusb" device, but only write the USB device busid to one port of USB controller.
For example, attaching a USB device (busid is 2-1.6) to above USB controller port 1, it only need write 2-1.6 to port 1 of this USB controller:
Backend:
backend = ""
vusb = ""
1 = ""
0 = ""
frontend = "/local/domain/1/device/vusb/0" frontend-id = "1"
online = "1"
state = "4"
type = "pv"
usb-ver = "1"
num-ports = "4"
port = ""
1 = "2-1.6"
2 = ""
3 = ""
4 = ""
Frontend doesn't change.
Since assign a host USB device to guest, we'll unbind USB interfaces from their original drivers and bind them to usbback. After detaching this USB device from guest, one would hope the USB interfaces could be rebind to their original drivers, so there should some place to get the original driver info. To support that, when attaching a USB device to guest, we'll save the original driver info in xenstore too, the place is /libxl/usbback, for example:
libxl = ""
1 = ""
dm-version = "qemu_xen"
usbback = ""
3-11 = ""
3-11@1_0 = ""
driver_path = "/sys/bus/usb/drivers/btusb"
In this example, USB device (busid is 3-11,/sys/bus/usb/devices/3-11). It has interface 3-11:1.0, whose original dirver is btusb. Since xenstore doesn't allow ':' and '.' in a key, so we encode the interface by changing ':' to '@' and changing '.' to '_'.
When detaching the USB device from guest, we can rebind 3-11:1.0 to btusb driver.
Chunyan Liu (7):
libxl: export some functions for pvusb use
libxl_utils: add internal function to read sysfs file contents
libxl: add pvusb API
libxl: addlibxl_device_usb_assignable_listAPI
xl: add pvusb commands
xl: add usb-assignable-list command
domcreate: support pvusb in configuration file
docs/man/xl.cfg.pod.5 | 84 ++ docs/man/xl.pod.1 | 41 + tools/libxl/Makefile | 2 +- tools/libxl/libxl.c | 55 +- tools/libxl/libxl.h | 77 ++ tools/libxl/libxl_create.c | 73 +- tools/libxl/libxl_device.c | 9 +- tools/libxl/libxl_internal.h | 34 + tools/libxl/libxl_osdeps.h | 13 + tools/libxl/libxl_pvusb.c | 1584
++++++++++++++++++++++++++++++++++
tools/libxl/libxl_types.idl | 46 +tools/libxl/libxl_types_internal.idl| 1 + tools/libxl/libxl_utils.c | 95 ++ tools/libxl/libxl_utils.h | 5 + tools/libxl/xl.h | 6 + tools/libxl/xl_cmdimpl.c | 322 ++++++- tools/libxl/xl_cmdtable.c | 29 +Quoted 2 lines
17 files changed, 2466 insertions(+), 10 deletions(-)create mode 100644
tools/libxl/libxl_pvusb.c
Quoted 2 lines
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel