libxl pvusb toolstack work
v3 cover libxl 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.
1/6~5/6 implement clean pvusb work. 6/6 is an independent patch to refactor codes for merging QEMU emulated USB code later.
1/6 is to export some functions for later usage
2/6 fixes read_file_content limit when reading sysfs file, for later usage. 3/6 libxl pvusb API implementation
4/6 xl pvusb hotplug commands implementation
5/6 usb from config file implementation
6/6 changing codes to unify qemu and pvusb.
Changes to v2:
* change xl interfaces from specifing usb device by busid to specifing usb device by bus.addr, so that pvusb and qemu can use same interface
* remove usb-assignable-list interface
* call existing helper functions to read sysfs file content to get usb device info.
* reorganize patches, first implement clean pvusb work, then in last patch changing codes for pvusb and qemu merging.
* Many fixes to address coding style, build on different platform, etc. Codes adjustment to better view.
V2 is here:
http://lists.xen.org/archives/html/xen-devel/2015-01/threads.html#03636
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
For a better understanding, a summary is posted in following.
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).
Additionally, for easy use of PVUSB, you need support in the toolstack to get the two sides to talk to each other.
2. Specifying a host USB device
QEMU hmp 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 Hub could not be assigned to guest.
3. PVUSB toolstack
* Specify USB device in xl config file
You can just specify usb devices, like:
usb=['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', ] usb=['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.
To be compatible with QEMU emulated USB, this interface could be extended to:
usb=['1.6, type=pv|qemu',]
type=pv means attaching the USB device in pvusb way; type=qemu means attaching USB device in QEMU emulated way.
* 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.
To be compatible with QEMU emulated USB, these interfaces could be extended to:
xl usb-attach domain 1.6 type=pv|qemu
xl usb-detach domain 1.6 type=pv|qemu
QEMU emulated USB won't handle USB controller create/delete through xl, so usb-ctrl-attach|detach operations are currently pvusb only.
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.
Chunyan Liu (6):
libxl: export some functions for pvusb use
libxl_read_file_contents: fix reading sysfs file
libxl: add pvusb API
xl: add pvusb commands
domcreate: support pvusb in configuration file refactor codes to unify pvusb and qemu emulated usb
docs/man/xl.cfg.pod.5 | 70 +++ docs/man/xl.pod.1 | 38 ++ tools/libxl/Makefile | 2 +- tools/libxl/libxl.c | 8 +- tools/libxl/libxl.h | 45 ++ tools/libxl/libxl_create.c | 73 ++- tools/libxl/libxl_internal.h | 20 +- tools/libxl/libxl_osdeps.h | 13 + tools/libxl/libxl_pvusb.c | 1042 ++++++++++++++++++++++++++++++++++ tools/libxl/libxl_types.idl | 52 ++ tools/libxl/libxl_types_internal.idl | 1 + tools/libxl/libxl_usb.c | 224 ++++++++ tools/libxl/libxl_utils.c | 5 +- tools/libxl/xl.h | 5 + tools/libxl/xl_cmdimpl.c | 364 +++++++++++- tools/libxl/xl_cmdtable.c | 25 + 16 files changed, 1974 insertions(+), 13 deletions(-) create mode 100644 tools/libxl/libxl_pvusb.c create mode 100644 tools/libxl/libxl_usb.c
Signature · 1 line
1.8.5.2
PATCH v3 1/6 libxl: export some functions for pvusb use
--- tools/libxl/libxl.c | 6 +++--- tools/libxl/libxl_internal.h | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-)
tools/libxl/libxl.c +3 -3
@@ -1963,7 +1963,7 @@ out:/******************************************************************************/ /* generic callback for devices that only need to set ao_complete */ -static void device_addrm_aocomplete (libxl__egc *egc,libxl__ao_device *aodev) +void device_addrm_aocomplete (libxl__egc *egc,libxl__ao_device *aodev) {STATE_AO_GC (aodev->ao);
@@ -1986,7 +1986,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;
@@ -2005,7 +2005,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, +int libxl__resolve_domid (libxl__gc *gc,const char *name,uint32_t *domid) {if (!name)
tools/libxl/libxl_internal.h +5 -0
@@ -1080,6 +1080,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:
@@ -2208,6 +2211,8 @@ struct libxl__ao_device {/* Starts preparing to add/remove a bunch of devices. */ _hidden void libxl__multidev_begin(libxl__ao *ao, libxl__multidev*); +/* generic callback for devices that only need to set ao_complete */ +_hidden void device_addrm_aocomplete(libxl__egc *egc, libxl__ao_device *aodev);/* Prepares to add/remove one of many devices. * Calls libxl__prepare_ao_device on libxl__ao_device argument provided and
Signature · 1 line
1.8.5.2
Re: [PATCH V3 1/6] libxl: export some functions for pvusb use
tools/libxl/libxl_internal.h _hidden int libxl__init_console_from_channel(libxl__gc *gc,
Quoted 8 lines · Chunyan Liu
+++
b/tools/libxl/libxl_internal.htools/libxl/libxl_internal.h +5
-0+_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); +/* generic callback for devices that only need to set ao_complete */ +_hidden void device_addrm_aocomplete(libxl__egc *egc, libxl__ao_device *aodev);
If that goes in I may move some or all of the vscsi code in libxl.c into libxl_vscsi.c.
Olaf
Re: [PATCH V3 1/6] libxl: export some functions for pvusb use
Quoted 8 lines · Olaf Hering
Quoted 5 lines · Chunyan Liu
+++
b/tools/libxl/libxl_internal.htools/libxl/libxl_internal.h +3
-0+_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);
Quoted 5 lines
tools/libxl/libxl_internal.h struct libxl__ao_device { · line 2215
Quoted 2 lines
tools/libxl/libxl_internal.h +2
-0+/* generic callback for devices that only need to set ao_complete */ +_hidden void device_addrm_aocomplete(libxl__egc *egc, libxl__ao_device *aodev);If that goes in I may move some or all of the vscsi code in
libxl.cintolibxl_vscsi.c.
It sounds like this would be useful independent of the pvusb stuff.
-George
Re: [PATCH V3 1/6] libxl: export some functions for pvusb use
In reply to Chunyan LiuQuoted 2 lines · Chunyan Liu
Signed-off-by Chunyan Liu <[email protected]>Signed-off-by Simon Cao <[email protected]>
On the basis that this can help reduce the length of libxl.c and improve maintainability by moving stuff out of libxl.c to dedicated files.
Quoted 61 lines
--- tools/libxl/libxl.c | 6 +++--- tools/libxl/libxl_internal.h | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-)tools/libxl/libxl.c +3
-3@@ -1963,7 +1963,7 @@ out:/******************************************************************************/ /* generic callback for devices that only need to set ao_complete */ -static void device_addrm_aocomplete (libxl__egc *egc,libxl__ao_device *aodev) +void device_addrm_aocomplete (libxl__egc *egc,libxl__ao_device *aodev) {STATE_AO_GC (aodev->ao);@@ -1986,7 +1986,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;@@ -2005,7 +2005,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, +int libxl__resolve_domid (libxl__gc *gc,const char *name,uint32_t *domid) {if (!name)tools/libxl/libxl_internal.h +5
-0@@ -1080,6 +1080,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: @@ -2208,6 +2211,8 @@ struct libxl__ao_device {/* Starts preparing to add/remove a bunch of devices. */ _hidden void libxl__multidev_begin(libxl__ao *ao, libxl__multidev*); +/* generic callback for devices that only need to set ao_complete */ +_hidden void device_addrm_aocomplete(libxl__egc *egc, libxl__ao_device *aodev);/* Prepares to add/remove one of many devices. * Calls libxl__prepare_ao_device on libxl__ao_device argument provided and Signature · 1 line
1.8.5.2
PATCH v3 2/6 libxl_read_file_contents: fix reading sysfs file
In reply to Chunyan LiuSysfs file has size=4096 but actual file content is less than that. Current libxl_read_file_contents will treat it as error when file size and actual file content differs, so reading sysfs file content with this function always fails. Fix it so that we can reuse this function to get sysfs file content in later pvusb work.
--- tools/libxl/libxl_utils.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
tools/libxl/libxl_utils.c +1 -4
@@ -363,12 +363,9 @@ int libxl_read_file_contents(libxl_ctx *ctx, const char *filename,if (!data) goto xe; rs =fread (data,1 , datalen, f); -if (rs != datalen) { +if (rs != datalen && !feof (f)) {if (ferror (f))LOGE (ERROR ,"failed to read %s" , filename); -else if (feof (f)) -LOG (ERROR ,"%s changed size while we were reading it" , - filename);else abort (); goto xe;
Signature · 1 line
1.8.5.2
Re: [PATCH V3 2/6] libxl_read_file_contents: fix reading sysfs file
Chunyan Liu writes ("[PATCH V3 2/6] libxl_read_file_contents: fix reading sysfs file"):
Quoted 1 line
Sysfs file has size=4096 but actual file content is less than that.
Wow.
Is there any danger that the actual size might be >4096 ?
Quoted 4 lines
Current
libxl_read_file_contentswill treat it as error when file size and actual file content differs, so reading sysfs file content with this function always fails. Fix it so that we can reuse this function to get sysfs file content in later pvusb work.
I'm uncomfortable with removing an error check from this function for all its call sites.
I think, sadly, that we are going to need a new function - at least, a new entrypoint.
We don't want to repeat the whole of libxl__read_file_contents.
Perhaps the bulk should be made into libxl__read_file_contents_core which takes a boolean instructing whether to tolerate magically shrinking files ?
Setting that boolean probably ought to arrange to insist that the function gets eof, in case the file is actually bigger rather than smaller than the size.
Ian, Wei ?
Ian.
Re: [PATCH V3 2/6] libxl_read_file_contents: fix reading sysfs file
Quoted 10 lines · Ian Jackson
Perhaps the bulk should be made into
libxl__read_file_contents_corewhich takes a boolean instructing whether to tolerate magically shrinking files ?Setting that boolean probably ought to arrange to insist that the function gets eof, in case the file is actually bigger rather than smaller than the size.
Ian, Wei ?
Sounds ok to me.
Re: [PATCH V3 2/6] libxl_read_file_contents: fix reading sysfs file
In reply to Ian JacksonQuoted 26 lines · Ian Jackson
Chunyan Liu writes ("[PATCH V3 2/6]
libxl_read_file_contents: fix reading sysfs file"):Quoted 1 line
Sysfs file has size=4096 but actual file content is less than that.
Wow.
Is there any danger that the actual size might be >4096 ?
Quoted 4 lines
Current
libxl_read_file_contentswill treat it as error when file size and actual file content differs, so reading sysfs file content with this function always fails. Fix it so that we can reuse this function to get sysfs file content in later pvusb work.I'm uncomfortable with removing an error check from this function for all its call sites.
I think, sadly, that we are going to need a new function - at least, a new entrypoint.
We don't want to repeat the whole of
libxl__read_file_contents.Perhaps the bulk should be made into
libxl__read_file_contents_corewhich takes a boolean instructing whether to tolerate magically shrinking files ?
Quoted 7 lines
Setting that boolean probably ought to arrange to insist that the function gets eof, in case the file is actually bigger rather than smaller than the size.
Ian, Wei ?
Yes, we need a new entry point.
Wei.
Quoted 1 line
Ian.
Re: [PATCH V3 2/6] libxl_read_file_contents: fix reading sysfs file
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
On 5/18/2015 at 10:30 PM, in message
<[email protected]>, Wei Liu <[email protected]>
Quoted 33 lines · wrote:
Quoted 1 line · Ian Jackson
Chunyan Liu writes ("[PATCH V3 2/6]
libxl_read_file_contents: fix readingsysfs file"):
Quoted 30 lines
Quoted 1 line
Sysfs file has size=4096 but actual file content is less than that.
Wow.
Is there any danger that the actual size might be >4096 ?
Quoted 4 lines
Current
libxl_read_file_contentswill treat it as error when file size and actual file content differs, so reading sysfs file content with this function always fails. Fix it so that we can reuse this function to get sysfs file content in later pvusb work.I'm uncomfortable with removing an error check from this function for all its call sites.
I think, sadly, that we are going to need a new function - at least, a new entrypoint.
We don't want to repeat the whole of
libxl__read_file_contents.Perhaps the bulk should be made into
libxl__read_file_contents_corewhich takes a boolean instructing whether to tolerate magically shrinking files ?Setting that boolean probably ought to arrange to insist that the function gets eof, in case the file is actually bigger rather than smaller than the size.
Quoted 4 lines
Quoted 2 lines
Ian, Wei ?
Yes, we need a new entry point.
Will update. Thanks!
Quoted 6 lines
Wei.
Quoted 1 line
Ian.
Re: [PATCH V3 2/6] libxl_read_file_contents: fix reading sysfs file
In reply to Chunyan LiuQuoted 6 lines · Chunyan Liu
Sysfs file has size=4096 but actual file content is less than that. Current
libxl_read_file_contentswill treat it as error when file size and actual file content differs, so reading sysfs file content with this function always fails. Fix it so that we can reuse this function to get sysfs file content in later pvusb work.
I'm not sure if I should classify this as a bug in Linux's sysfs interface.
In any case, we would still like to detect the error case that file size is changed under our feet.
I have a dumb idea of having a dedicated function that is used to read sysfs, but I'm not sure if it is too dumb.
I will wait for Ian and Ian's input on this.
Wei.
Quoted 25 lines
Signed-off-by Chunyan Liu <[email protected]>--- tools/libxl/libxl_utils.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)tools/libxl/libxl_utils.c +1
-4@@ -363,12 +363,9 @@ int libxl_read_file_contents(libxl_ctx *ctx, const char *filename,if (!data) goto xe; rs =fread (data,1 , datalen, f); -if (rs != datalen) { +if (rs != datalen && !feof (f)) {if (ferror (f))LOGE (ERROR ,"failed to read %s" , filename); -else if (feof (f)) -LOG (ERROR ,"%s changed size while we were reading it" , - filename);else abort (); goto xe;Signature · 1 line
1.8.5.2
PATCH v3 3/6 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 to v2:
* remove qemu emulated usb related definitions, keep the work pure pvusb. In last patch, will do all refactor work to unify pvusb and qemu emulated usb.
* use bus.addr as user interface instead of busid to do usb-attach|detach
* remove usb-assignable-list APIs and some other unnecessary APIs
* reuse libxl_read_file_contents function instead of another new function to handle getting sysfs file content
* fix build on different platforms as pci does
* fix many coding style problems
* address other comments in last version
* adjust codes to let it look better
tools/libxl/Makefile | 2 +- tools/libxl/libxl.c | 2 + tools/libxl/libxl.h | 45 ++ tools/libxl/libxl_internal.h | 11 +- tools/libxl/libxl_osdeps.h | 13 + tools/libxl/libxl_pvusb.c | 1201 ++++++++++++++++++++++++++++++++++ tools/libxl/libxl_types.idl | 41 ++ tools/libxl/libxl_types_internal.idl | 1 + 8 files changed, 1314 insertions(+), 2 deletions(-) create mode 100644 tools/libxl/libxl_pvusb.c
tools/libxl/Makefile +1 -1
@@ -95,7 +95,7 @@ LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o libxl_pci.o \ libxl_internal.o libxl_utils.o libxl_uuid.o \ libxl_json.o libxl_aoutils.o libxl_numa.o libxl_vnuma.o \ libxl_save_callout.o _libxl_save_msgs_callout.o \ - libxl_qmp.o libxl_event.o libxl_fork.o $(LIBXL_OBJS-y) + libxl_qmp.o libxl_event.o libxl_fork.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 +2 -0
@@ -1611,6 +1611,8 @@ void libxl__destroy_domid(libxl__egc *egc, libxl__destroy_domid_state *dis)if (libxl__device_pci_destroy_all (gc, domid) <0 )LIBXL__LOG (ctx,LIBXL__LOG_ERROR ,"pci shutdown failed for domid %d" , domid); +if (libxl__device_usb_destroy_all (gc, domid) <0 ) +LIBXL__LOG (ctx,LIBXL__LOG_ERROR ,"usb shutdown failed for domid %d" , domid); rc =xc_domain_pause (ctx->xch, domid);if (rc <0 ) {LIBXL__LOG_ERRNOVAL (ctx,LIBXL__LOG_ERROR , rc,"xc_domain_pause failed for %d" , domid);
tools/libxl/libxl.h +45 -0
57 lines not on this page · raw message
tools/libxl/libxl_internal.h +10 -1
@@ -2422,6 +2422,13 @@ _hidden void libxl__device_vtpm_add(libxl__egc *egc, uint32_t domid, libxl_device_vtpm *vtpm, libxl__ao_device *aodev); +/* from libxl_usb */ +_hidden int libxl__device_usbctrl_add(libxl__gc *gc, uint32_t domid, +libxl_device_usbctrl *usbctrl); +_hidden int libxl__device_usb_add(libxl__gc *gc, uint32_t domid, +libxl_device_usb *usb); +_hidden int libxl__device_usb_destroy_all(libxl__gc *gc, uint32_t domid); +/* Internal function to connect a vkb device */ _hidden int libxl__device_vkb_add(libxl__gc *gc, uint32_t domid,libxl_device_vkb *vkb);
@@ -3628,7 +3635,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) (!strcmp((a)->busid, (b)->busid)) +#define COMPARE_USBCTRL (a, b) ((a)->devid == (b)->devid) +/* DEVICE_ADD * * Add a device in libxl_domain_config structure
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,12 +42,21 @@#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"#include <libutil.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 +1201 -0
1,201 lines not on this page · raw message
tools/libxl/libxl_types.idl +41 -0
59 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", [
Signature · 1 line
1.8.5.2
Re: [PATCH V3 3/6] libxl: add pvusb API
Quoted 33 lines · Chunyan Liu
Add pvusb APIs, including:
- attach/detach (create/destroy) virtual usb controller.
- attach/detach usb device
- list usb controller and usb devices
- some other helper functionsSigned-off-by Chunyan Liu <[email protected]>Signed-off-by Simon Cao <[email protected]>---
Changes to v2:
* remove qemu emulated usb related definitions, keep the work pure pvusb. In last patch, will do all refactor work to unify pvusb and qemu emulated usb.
* use bus.addr as user interface instead of busid to do usb-attach|detach
* remove usb-assignable-list APIs and some other unnecessary APIs
* reuselibxl_read_file_contentsfunction instead of another new function to handle getting sysfs file content
* fix build on different platforms as pci does
* fix many coding style problems
* address other comments in last version
* adjust codes to let it look bettertools/libxl/Makefile | 2 +- tools/libxl/libxl.c | 2 + tools/libxl/libxl.h | 45 ++ tools/libxl/libxl_internal.h | 11 +- tools/libxl/libxl_osdeps.h | 13 + tools/libxl/libxl_pvusb.c | 1201 ++++++++++++++++++++++++++++++++++ tools/libxl/libxl_types.idl | 41 ++ tools/libxl/libxl_types_internal.idl | 1 + 8 files changed, 1314 insertions(+), 2 deletions(-)create mode 100644
tools/libxl/libxl_pvusb.c
...
Quoted 5 lines
tools/libxl/libxl_pvusb.c new file +0
-0
...
Quoted 54 lines
tools/libxl/libxl_pvusb.c new file +54
-0+static int libxl__usbctrl_add_xenstore (libxl__gc *gc,uint32_t domid, +libxl_device_usbctrl *usbctrl) +{ +flexarray_t *front; +flexarray_t *back; +libxl__device *device; +xs_transaction_t t =XBT_NULL ; +int rc =0 ; +libxl_domain_config d_config; +libxl_device_usbctrl usbctrl_saved; +libxl__domain_userdata_lock *lock =NULL ; + +libxl_domain_config_init (&d_config); +libxl_device_usbctrl_init (&usbctrl_saved); +libxl_device_usbctrl_copy (CTX , &usbctrl_saved, usbctrl); + +GCNEW (device); +libxl__device_from_usbctrl (gc, domid, usbctrl, device); + + front =flexarray_make (gc,4 ,1 ); + back =flexarray_make (gc,12 ,1 ); + +flexarray_append_pair (back,"frontend-id" ,GCSPRINTF ("%d" , domid)); +flexarray_append_pair (back,"online" ,"1" ); +flexarray_append_pair (back,"state" ,"1" ); +flexarray_append_pair (back,"usb-ver" ,GCSPRINTF ("%d" , usbctrl->version)); +flexarray_append_pair (back,"num-ports" ,GCSPRINTF ("%d" , usbctrl->ports)); +flexarray_append_pair (front,"backend-id" ,GCSPRINTF ("%d" , usbctrl->backend_domid)); +flexarray_append_pair (front,"state" ,"1" ); + + lock =libxl__lock_domain_userdata (gc, domid); +if (!lock) { + rc =ERROR_LOCK_FAIL ; + goto out; + } + + rc =libxl__get_domain_configuration (gc, domid, &d_config); +if (rc) goto out; + +DEVICE_ADD (usbctrl, usbctrls, domid, &usbctrl_saved,COMPARE_USBCTRL , &d_config); + +for (;;) { + rc =libxl__xs_transaction_start (gc, &t); +if (rc) goto out; + + rc =libxl__device_exists (gc, t, device); +if (rc <0 ) goto out; +if (rc ==1 ) { +/* already exists in xenstore */ +LOG (ERROR ,"device already exists in xenstore" ); + rc =ERROR_DEVICE_EXISTS ; + goto out; + } +
tools/libxl/libxl_pvusb.c line 133
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ rc =libxl__set_domain_configuration (gc, domid, &d_config); +if (rc) goto out; + +libxl__device_generic_add (gc, t, device, +libxl__xs_kvs_of_flexarray (gc, back, + back->count), +libxl__xs_kvs_of_flexarray (gc, front, + front->count), +NULL ); +libxl__usbport_add_xenstore (gc, t, domid, usbctrl);
Still no rc check.
Quoted 11 lines
tools/libxl/libxl_pvusb.c new file +11
-0+ rc =libxl__xs_transaction_commit (gc, &t); +if (!rc)break ; +if (rc <0 ) goto out; + } + +out: +if (lock)libxl__unlock_domain_userdata (lock); +libxl_device_usbctrl_dispose (&usbctrl_saved); +libxl_domain_config_dispose (&d_config); +return rc; +}
...
Quoted 57 lines
tools/libxl/libxl_pvusb.c new file +57
-0+static int +libxl__device_usbctrl_remove_common (libxl_ctx *ctx,uint32_t domid, +libxl_device_usbctrl *usbctrl, +const libxl_asyncop_how *ao_how, +int force) +{ +AO_CREATE (ctx, domid, ao_how); +libxl__device *device; +libxl__ao_device *aodev; +libxl_device_usbctrl *usbctrls =NULL ; +libxl_device_usbctrl *usbctrl_find =NULL ; +int numctrl =0 ; +libxl_device_usb *usbs =NULL ; +int numusb =0 ; +int i, rc; + +assert (usbctrl->devid !=-1 ); + + usbctrls =libxl_device_usbctrl_list (ctx, domid, &numctrl); +if (!numctrl) { +LOG (ERROR ,"No USB controller exists in this domain" ); + rc =ERROR_FAIL ; + goto out; + } + +for (i =0 ; i < numctrl; i++) { +if (usbctrl->devid == usbctrls[i].devid) { + usbctrl_find = usbctrls + i; +break ; + } + } + +if (!usbctrl_find) { +LOG (ERROR ,"USB controller %d is not attached to this domain" , + usbctrl->devid); + rc =ERROR_FAIL ; + goto out; + } + + usbctrl = usbctrl_find; + +GCNEW (device); +libxl__device_from_usbctrl (gc, domid, usbctrl, device); + +/* Remove usb devives first */ + rc =libxl__device_usb_list (gc, domid, usbctrl->devid, &usbs, &numusb); +if (rc) goto out; +for (i =0 ; i < numusb; i++) { +if (do_usb_remove (gc, domid, &usbs[i])) { +LOG (ERROR ,"do_usb_remove failed" ); + rc =ERROR_FAIL ; + goto out; + } + } +/* remove usbctrl */ +GCNEW (aodev); +libxl__prepare_ao_device (ao, aodev);
tools/libxl/libxl_pvusb.c line 255
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ aodev->action =LIBXL__DEVICE_ACTION_REMOVE ; + aodev->dev = device; + aodev->callback = device_addrm_aocomplete; + aodev->force = force; +libxl__initiate_device_remove (egc, aodev); + +out: +free (usbctrls); +free (usbs); +if (rc)return AO_ABORT (rc);
Coding style.
Quoted 2 lines
tools/libxl/libxl_pvusb.c new file +2
-0+return AO_INPROGRESS ; +}
...
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+libxl_device_usbctrl * +libxl_device_usbctrl_list (libxl_ctx *ctx,uint32_t domid,int *num) +{ +GC_INIT (ctx); +libxl_device_usbctrl *usbctrls =NULL ; +char *fe_path =NULL ; +char **dir =NULL ; +unsigned int ndirs =0 ; + + *num =0 ;
tools/libxl/libxl_pvusb.c line 292
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ + fe_path =GCSPRINTF ("%s/device/vusb" , +libxl__xs_get_dompath (gc, domid)); + dir =libxl__xs_directory (gc,XBT_NULL , fe_path, &ndirs); + +if (dir && ndirs) { + usbctrls =malloc (sizeof (*usbctrls) * ndirs); +libxl_device_usbctrl * usbctrl; +libxl_device_usbctrl * end = usbctrls + ndirs; +for (usbctrl = usbctrls; usbctrl < end; usbctrl++, dir++, (*num)++) {
Coding style.
Quoted 35 lines
tools/libxl/libxl_pvusb.c new file +35
-0+char *tmp; +const char *be_path =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/%s/backend" , fe_path, *dir)); + +libxl_device_usbctrl_init (usbctrl); + usbctrl->devid =atoi (*dir); + + tmp =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/%s/backend-id" , fe_path, *dir)); +if (!tmp) goto outerr; + usbctrl->backend_domid =atoi (tmp); + + tmp =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/usb-ver" , be_path)); +if (!tmp) goto outerr; + usbctrl->version =atoi (tmp); + + tmp =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/num-ports" , be_path)); +if (!tmp) goto outerr; + usbctrl->ports =atoi (tmp); + } + } + +return usbctrls; + +outerr: +LOG (ERROR ,"Unable to list USB Controllers" ); +for (int i =0 ; i < *num; i++) { +libxl_device_usbctrl_dispose (usbctrls + i); + } +free (usbctrls); + *num =0 ; +return NULL ; +}
...
Quoted 14 lines
tools/libxl/libxl_pvusb.c new file +14
-0+/* get all usb devices of the domain */ +static libxl_device_usb * +libxl_device_usb_list_all (libxl__gc *gc,uint32_t domid,int *num) +{ +char **usbctrls; +unsigned int nd, i, j; +char *be_path; +int rc; +libxl_device_usb *usbs =NULL ; + + *num =0 ; + + be_path =GCSPRINTF ("/local/domain/%d/backend/vusb/%d" , +LIBXL_TOOLSTACK_DOMID , domid);
tools/libxl/libxl_pvusb.c line 553
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ usbctrls =libxl__xs_directory (gc,XBT_NULL , be_path, &nd); + +for (i =0 ; i < nd; i++) { +int nc =0 ; +libxl_device_usb *tmp =NULL ; + rc =libxl__device_usb_list (gc, domid,atoi (usbctrls[i]), &tmp, &nc); +if (!nc)continue ; + + usbs =realloc (usbs,sizeof (libxl_device_usb)*((*num) + nc)); +for (j =0 ; j < nc; j++) {
Coding style.
Quoted 9 lines
tools/libxl/libxl_pvusb.c new file +9
-0+ usbs[*num].ctrl = tmp[j].ctrl; + usbs[*num].port = tmp[j].port; + usbs[*num].busid =strdup (tmp[j].busid); + (*num)++; + } +free (tmp); + } +return usbs; +}
...
Quoted 5 lines
tools/libxl/libxl_pvusb.c new file +5
-0+/* set default value */ +static char *usb_busaddr_to_busid (libxl__gc *gc,int bus,int addr) +{ +libxl_ctx *ctx =libxl__gc_owner (gc); +struct dirent *de;
tools/libxl/libxl_pvusb.c line 590
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+DIR *dir; +char *busid =NULL ; + +if (bus <1 || addr <1 ) +return NULL ; + +if (!(dir =opendir (SYSFS_USB_DEV ))) +return NULL ; + +while ((de =readdir (dir))) {
Coding style.
Quoted 29 lines
tools/libxl/libxl_pvusb.c new file +29
-0+char *filename; +void *buf; +int busnum =-1 ; +int devnum =-1 ; + +if (!de->d_name) +continue ; + + filename =GCSPRINTF (SYSFS_USB_DEV "/%s/devnum" , de->d_name); +if (!libxl_read_file_contents (ctx, filename, &buf,NULL )) +sscanf (buf,"%x" , &devnum); + + filename =GCSPRINTF (SYSFS_USB_DEV "/%s/busnum" , de->d_name); +if (!libxl_read_file_contents (ctx, filename, &buf,NULL )) +sscanf (buf,"%x" , &busnum); + +if (bus == busnum && addr == devnum) { + busid =strdup (de->d_name); +break ; + } + } + +closedir (dir); +return busid; +} + +/* find first unused controller:port and give that to usb device */ +static int +libxl__device_usb_set_default_usbctrl(libxl__gc *gc, uint32_t domid,
tools/libxl/libxl_pvusb.c line 629
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ libxl_device_usb *usb) +{ +libxl_ctx *ctx =CTX ; +libxl_device_usbctrl *usbctrls; +libxl_device_usb *usbs =NULL ; +int numctrl, numusb, i, j, rc =-1 ; +char *be_path, *tmp; + + usbctrls =libxl_device_usbctrl_list (ctx, domid, &numctrl); +if ( !numctrl)
Coding style.
Quoted 46 lines
tools/libxl/libxl_pvusb.c new file +46
-0+ goto out; + +for (i =0 ; i < numctrl; i++) { + rc =libxl__device_usb_list (gc, domid, usbctrls[i].devid, + &usbs, &numusb); +if (rc)continue ; + +if (!usbctrls[i].ports || numusb == usbctrls[i].ports) +continue ; + +for (j =1 ; i <= numusb; j++) { + be_path =GCSPRINTF ("%s/backend/vusb/%d/%d/port/%d" , +libxl__xs_get_dompath (gc,0 ), domid, + usbctrls[i].devid, j); + tmp =libxl__xs_read (gc,XBT_NULL , be_path); +if (tmp && !strcmp ( tmp,"" )) { + usb->ctrl = usbctrls[i].devid; + usb->port = j; +break ; + } + } + } + + rc =0 ; + +out: +if (usbctrls) +free (usbctrls); +if (usbs) +free (usbs); +return rc; +} + +static int libxl__device_usb_setdefault (libxl__gc *gc,uint32_t domid, +libxl_device_usb *usb) +{ +char *be_path, *tmp; + +if (usb->ctrl ==-1 ) { +int ret =libxl__device_usb_set_default_usbctrl (gc, domid, usb); +/* If no existing ctrl to host this usb device, setup a new one */ +if (ret) { +libxl_device_usbctrl usbctrl; +libxl_device_usbctrl_init (&usbctrl); +libxl__device_usbctrl_add (gc, domid, &usbctrl); + usb->ctrl = usbctrl.devid;
tools/libxl/libxl_pvusb.c line 685
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ usb->port =1 ; +libxl_device_usbctrl_dispose (&usbctrl); + } + } + + be_path =GCSPRINTF ("%s/backend/vusb/%d/%d/port/%d" , +libxl__xs_get_dompath (gc,0 ), + domid, usb->ctrl, usb->port); + tmp =libxl__xs_read (gc,XBT_NULL , be_path); +if (!tmp ||strcmp (tmp,"" ) ){
Coding style.
Quoted 6 lines
tools/libxl/libxl_pvusb.c new file +6
-0+LOG (ERROR ,"The controller port isn't available" ); +return ERROR_INVAL ; + } + +return 0 ; +}
...
tools/libxl/libxl_pvusb.c line 792
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+static int bind_usb_intf (libxl__gc *gc,char *intf,char *drvpath) +{ +char *path; +struct stat st; +int fd, rc =0 ; + + path =GCSPRINTF ("%s/%s" , drvpath, intf); + rc =lstat (path, &st); +/* already bind, return */ +if (rc ==0 )
Coding style.
Quoted 16 lines
tools/libxl/libxl_pvusb.c new file +16
-0+return 0 ; + + path =GCSPRINTF ("%s/bind" , drvpath); + fd =open (path,O_WRONLY ); +if (fd <0 ) +return ERROR_FAIL ; + + rc =write (fd, intf,strlen (intf)); +close (fd); +if (rc <0 ) +return ERROR_FAIL ; + +return 0 ; +} + +/* Is usb interface bound to usbback? */
tools/libxl/libxl_pvusb.c line 818
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+static int usb_intf_is_assigned (libxl__gc *gc,char *intf) +{ +char *spath; +int rc; +struct stat st; + + spath =GCSPRINTF (SYSFS_USBBACK_DRIVER "/%s" , intf); + rc =lstat (spath, &st); + +if (rc ==0 )
Coding style.
tools/libxl/libxl_pvusb.c line 824
Quoted 6 lines
tools/libxl/libxl_pvusb.c new file +6
-0+return 1 ; +if (rc <0 && errno ==ENOENT ) +return 0 ; +LOGE (ERROR ,"Accessing %s" , spath); +return -1 ; +}
Juergen
Re: [PATCH V3 3/6] libxl: add pvusb API
In reply to Chunyan Liutools/libxl/libxl_pvusb.c line 98
Quoted 7 lines · Chunyan Liu
tools/libxl/libxl_pvusb.c new file +5
-0+static int libxl__usbctrl_add_xenstore (libxl__gc *gc,uint32_t domid, +libxl_device_usbctrl *usbctrl) +{ +flexarray_append_pair (back,"state" ,"1" ); +flexarray_append_pair (front,"state" ,"1" );
This (and perhaps other places) should be converted to xenbus_state, see commit 25519c75b7e05fd82d7f2959aaa85518b5564cc3 ("libxl: convert strings and ints to xenbus_state").
Olaf
Re: [PATCH V3 3/6] libxl: add pvusb API
In reply to Chunyan Liu(I look at overall code structure this pass. I haven't done a line by line review.)
Quoted 22 lines · Chunyan Liu
Add pvusb APIs, including:
- attach/detach (create/destroy) virtual usb controller.
- attach/detach usb device
- list usb controller and usb devices
- some other helper functionsSigned-off-by Chunyan Liu <[email protected]>Signed-off-by Simon Cao <[email protected]>---
Changes to v2:
* remove qemu emulated usb related definitions, keep the work pure pvusb. In last patch, will do all refactor work to unify pvusb and qemu emulated usb.
* use bus.addr as user interface instead of busid to do usb-attach|detach
* remove usb-assignable-list APIs and some other unnecessary APIs
* reuselibxl_read_file_contentsfunction instead of another new function to handle getting sysfs file content
* fix build on different platforms as pci does
* fix many coding style problems
* address other comments in last version
* adjust codes to let it look better
Quoted 8 lines
tools/libxl/Makefile | 2 +- tools/libxl/libxl.c | 2 + tools/libxl/libxl.h | 45 ++ tools/libxl/libxl_internal.h | 11 +- tools/libxl/libxl_osdeps.h | 13 + tools/libxl/libxl_pvusb.c | 1201 ++++++++++++++++++++++++++++++++++ tools/libxl/libxl_types.idl | 41 ++ tools/libxl/libxl_types_internal.idl | 1 +
You also need to document the xenstore keys and values somewhere under docs directory.
And you forgot to update libxl_retrieve_domain_configuration function.
Quoted 181 lines
8 files changed, 1314 insertions(+), 2 deletions(-) create mode 100644 tools/libxl/libxl_pvusb.ctools/libxl/Makefile +1
-1@@ -95,7 +95,7 @@ LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o libxl_pci.o \ libxl_internal.o libxl_utils.o libxl_uuid.o \ libxl_json.o libxl_aoutils.o libxl_numa.o libxl_vnuma.o \ libxl_save_callout.o _libxl_save_msgs_callout.o \ - libxl_qmp.o libxl_event.o libxl_fork.o $(LIBXL_OBJS-y) + libxl_qmp.o libxl_event.o libxl_fork.o libxl_pvusb.o $(LIBXL_OBJS-y) LIBXL_OBJS += libxl_genid.o LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.otools/libxl/libxl.c +2
-0@@ -1611,6 +1611,8 @@ void libxl__destroy_domid(libxl__egc *egc, libxl__destroy_domid_state *dis)if (libxl__device_pci_destroy_all (gc, domid) <0 )LIBXL__LOG (ctx,LIBXL__LOG_ERROR ,"pci shutdown failed for domid %d" , domid); +if (libxl__device_usb_destroy_all (gc, domid) <0 ) +LIBXL__LOG (ctx,LIBXL__LOG_ERROR ,"usb shutdown failed for domid %d" , domid); rc =xc_domain_pause (ctx->xch, domid);if (rc <0 ) {LIBXL__LOG_ERRNOVAL (ctx,LIBXL__LOG_ERROR , rc,"xc_domain_pause failed for %d" , domid);tools/libxl/libxl.h +45
-0@@ -114,6 +114,12 @@#define LIBXL_HAVE_DOMAIN_NODEAFFINITY 1/* +* LIBXL_HAVE_PVUSB indicates the functions for doing hot-plug of +* USB devices through pvusb. +*/ +#define LIBXL_HAVE_PVUSB 1 + +/* * LIBXL_HAVE_BUILDINFO_HVM_VENDOR_DEVICE indicates that the * libxl_vendor_device field is present in the hvm sections of * libxl_domain_build_info. This field tells libxl which @@ -1224,6 +1230,45 @@ int libxl_cdrom_insert(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk, const libxl_asyncop_how *ao_how)LIBXL_EXTERNAL_CALLERS_ONLY ; +/* USB Controllers*/ +int libxl_device_usbctrl_add(libxl_ctx *ctx, uint32_t domid, + libxl_device_usbctrl *usbctrl, + const libxl_asyncop_how *ao_how) + LIBXL_EXTERNAL_CALLERS_ONLY; + +int libxl_device_usbctrl_remove(libxl_ctx *ctx, uint32_t domid, + libxl_device_usbctrl *usbctrl, + const libxl_asyncop_how *ao_how) + LIBXL_EXTERNAL_CALLERS_ONLY; + +int libxl_device_usbctrl_destroy(libxl_ctx *ctx, uint32_t domid, + libxl_device_usbctrl *usbctrl, + const libxl_asyncop_how *ao_how) + LIBXL_EXTERNAL_CALLERS_ONLY; + +libxl_device_usbctrl *libxl_device_usbctrl_list (libxl_ctx *ctx, +uint32_t domid,int *num); + + +int libxl_device_usbctrl_getinfo(libxl_ctx *ctx, uint32_t domid, + libxl_device_usbctrl *usbctrl, + libxl_usbctrlinfo *usbctrlinfo) + LIBXL_EXTERNAL_CALLERS_ONLY; + +/* USB Devices */ +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; + +int libxl_device_usb_remove(libxl_ctx *ctx, uint32_t domid, libxl_device_usb *usb, + const libxl_asyncop_how *ao_how) + LIBXL_EXTERNAL_CALLERS_ONLY; + +libxl_device_usb *libxl_device_usb_list (libxl_ctx *ctx,uint32_t domid, +int usbctrl,int *num); + +int libxl_device_usb_getinfo(libxl_ctx *ctx, char *intf, libxl_usbinfo *usbinfo) + LIBXL_EXTERNAL_CALLERS_ONLY;/* Network Interfaces */ int libxl_device_nic_add (libxl_ctx *ctx,uint32_t domid,libxl_device_nic *nic,const libxl_asyncop_how *ao_how)tools/libxl/libxl_internal.h +10
-1@@ -2422,6 +2422,13 @@ _hidden void libxl__device_vtpm_add(libxl__egc *egc, uint32_t domid, libxl_device_vtpm *vtpm, libxl__ao_device *aodev); +/* from libxl_usb */ +_hidden int libxl__device_usbctrl_add(libxl__gc *gc, uint32_t domid, +libxl_device_usbctrl *usbctrl); +_hidden int libxl__device_usb_add(libxl__gc *gc, uint32_t domid, +libxl_device_usb *usb); +_hidden int libxl__device_usb_destroy_all(libxl__gc *gc, uint32_t domid); +/* Internal function to connect a vkb device */ _hidden int libxl__device_vkb_add(libxl__gc *gc, uint32_t domid,libxl_device_vkb *vkb);@@ -3628,7 +3635,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) (!strcmp((a)->busid, (b)->busid)) +#define COMPARE_USBCTRL (a, b) ((a)->devid == (b)->devid) +/* DEVICE_ADD * * Add a device in libxl_domain_config structure 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,12 +42,21 @@#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"#include <libutil.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 +10
-0@@ -0,0 +1,1201 @@ +/* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License as published +* by the Free Software Foundation; version 2.1 only. with the special +* exception on linking described in file LICENSE. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License for more details.
tools/libxl/libxl_pvusb.c line 20
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+*/ + +#include "libxl_osdeps.h" /* must come before any other headers */ + +#include "libxl_internal.h" + +#define USBBACK_INFO_PATH "/libxl/usbback" + +#define USBHUB_CLASS_CODE 0x09 +
I'm not very familiar with how USB is supposed to work. Can you explain why this particular value is chosen?
tools/libxl/libxl_pvusb.c line 28
Quoted 8 lines
tools/libxl/libxl_pvusb.c new file +8
-0+static int libxl__device_usbctrl_setdefault (libxl__gc *gc,uint32_t domid, +libxl_device_usbctrl *usbctrl) +{ +int rc; + +if (!usbctrl->version) + usbctrl->version =2 ; +
How hard would it be to implement USB 3? I assume this depends on QEMU's support? I.e. we just need to specify the version to 3 and it should just work? Just curious.
Quoted 59 lines
tools/libxl/libxl_pvusb.c new file +59
-0+if (!usbctrl->ports) + usbctrl->ports =8 ; + + rc =libxl__resolve_domid (gc, usbctrl->backend_domname, + &usbctrl->backend_domid); +return rc; +} + +static void libxl__device_from_usbctrl (libxl__gc *gc,uint32_t domid, +libxl_device_usbctrl *usbctrl, +libxl__device *device) +{ + device->backend_devid = usbctrl->devid; + device->backend_domid = usbctrl->backend_domid; + device->backend_kind =LIBXL__DEVICE_KIND_VUSB ; + device->devid = usbctrl->devid; + device->domid = domid; + device->kind =LIBXL__DEVICE_KIND_VUSB ; +} + +static int libxl__usbport_add_xenstore (libxl__gc *gc, +xs_transaction_t tran, +uint32_t domid, +libxl_device_usbctrl *usbctrl) +{ +char *path; +int i; + + path =GCSPRINTF ("%s/backend/vusb/%d/%d/port" , +libxl__xs_get_dompath (gc,0 ), domid, usbctrl->devid); + +libxl__xs_mkdir (gc, tran, path,NULL ,0 ); + +for (i =1 ; i <= usbctrl->ports; i++) { +if (libxl__xs_write_checked (gc, tran,GCSPRINTF ("%s/%d" , path, i),"" )) +return ERROR_FAIL ; + } + +return 0 ; +} + +static int libxl__usbctrl_add_xenstore (libxl__gc *gc,uint32_t domid, +libxl_device_usbctrl *usbctrl) +{ +flexarray_t *front; +flexarray_t *back; +libxl__device *device; +xs_transaction_t t =XBT_NULL ; +int rc =0 ; +libxl_domain_config d_config; +libxl_device_usbctrl usbctrl_saved; +libxl__domain_userdata_lock *lock =NULL ; + +libxl_domain_config_init (&d_config); +libxl_device_usbctrl_init (&usbctrl_saved); +libxl_device_usbctrl_copy (CTX , &usbctrl_saved, usbctrl); + +GCNEW (device); +libxl__device_from_usbctrl (gc, domid, usbctrl, device);
tools/libxl/libxl_pvusb.c line 97
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ + front =flexarray_make (gc,4 ,1 ); + back =flexarray_make (gc,12 ,1 ); + +flexarray_append_pair (back,"frontend-id" ,GCSPRINTF ("%d" , domid)); +flexarray_append_pair (back,"online" ,"1" ); +flexarray_append_pair (back,"state" ,"1" ); +flexarray_append_pair (back,"usb-ver" ,GCSPRINTF ("%d" , usbctrl->version)); +flexarray_append_pair (back,"num-ports" ,GCSPRINTF ("%d" , usbctrl->ports)); +flexarray_append_pair (front,"backend-id" ,GCSPRINTF ("%d" , usbctrl->backend_domid));
Line too long.
tools/libxl/libxl_pvusb.c line 110
Quoted 13 lines
tools/libxl/libxl_pvusb.c new file +13
-0+flexarray_append_pair (front,"state" ,"1" ); + + lock =libxl__lock_domain_userdata (gc, domid); +if (!lock) { + rc =ERROR_LOCK_FAIL ; + goto out; + } + + rc =libxl__get_domain_configuration (gc, domid, &d_config); +if (rc) goto out; + +DEVICE_ADD (usbctrl, usbctrls, domid, &usbctrl_saved,COMPARE_USBCTRL , &d_config); +
Line too long.
Quoted 18 lines
tools/libxl/libxl_pvusb.c new file +18
-0+for (;;) { + rc =libxl__xs_transaction_start (gc, &t); +if (rc) goto out; + + rc =libxl__device_exists (gc, t, device); +if (rc <0 ) goto out; +if (rc ==1 ) { +/* already exists in xenstore */ +LOG (ERROR ,"device already exists in xenstore" ); + rc =ERROR_DEVICE_EXISTS ; + goto out; + } + + rc =libxl__set_domain_configuration (gc, domid, &d_config); +if (rc) goto out; + + libxl__device_generic_add(gc, t, device, + libxl__xs_kvs_of_flexarray(gc, back,
tools/libxl/libxl_pvusb.c line 138
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ back->count), +libxl__xs_kvs_of_flexarray (gc, front, + front->count), + NULL); +libxl__usbport_add_xenstore (gc, t, domid, usbctrl); + rc =libxl__xs_transaction_commit (gc, &t); +if (!rc)break ; +if (rc <0 ) goto out; + } +
You don't have aodev so you cannot check update_json. Maybe you need aodev?
That field update_json is set to true when doing hotplug. It's set to false during domain creation.
The same comment applies to other add functions as well.
Quoted 30 lines
tools/libxl/libxl_pvusb.c new file +30
-0+out: +if (lock)libxl__unlock_domain_userdata (lock); +libxl_device_usbctrl_dispose (&usbctrl_saved); +libxl_domain_config_dispose (&d_config); +return rc; +} + +int libxl__device_usbctrl_add (libxl__gc *gc,uint32_t domid, +libxl_device_usbctrl *usbctrl) +{ +int rc =0 ; + + rc =libxl__device_usbctrl_setdefault (gc, domid, usbctrl); +if (rc <0 ) goto out; + +if (usbctrl->devid ==-1 ) { + usbctrl->devid =libxl__device_nextid (gc, domid,"vusb" ); +if (usbctrl->devid <0 ) { + rc =ERROR_FAIL ; + goto out; + } + } + +if (libxl__usbctrl_add_xenstore (gc, domid, usbctrl) <0 ){ + rc =ERROR_FAIL ; + goto out; + } + +out: +return rc;
tools/libxl/libxl_pvusb.c line 178
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+} + +int libxl_device_usbctrl_add (libxl_ctx *ctx,uint32_t domid, +libxl_device_usbctrl *usbctrl, +const libxl_asyncop_how *ao_how) +{ +AO_CREATE (ctx, domid, ao_how); +int rc; + + rc =libxl__device_usbctrl_add (gc, domid, usbctrl);
Hmm... Your remove function is async while this one is sync, why?
Quoted 136 lines
tools/libxl/libxl_pvusb.c new file +136
-0+libxl__ao_complete (egc, ao, rc); +return AO_INPROGRESS ; +} + +static int libxl__device_usb_list (libxl__gc *gc,uint32_t domid,int usbctrl, +libxl_device_usb **usbs,int *num); + +static int do_usb_remove (libxl__gc *gc,uint32_t domid, +libxl_device_usb *usb); + +static int +libxl__device_usbctrl_remove_common (libxl_ctx *ctx,uint32_t domid, +libxl_device_usbctrl *usbctrl, +const libxl_asyncop_how *ao_how, +int force) +{ +AO_CREATE (ctx, domid, ao_how); +libxl__device *device; +libxl__ao_device *aodev; +libxl_device_usbctrl *usbctrls =NULL ; +libxl_device_usbctrl *usbctrl_find =NULL ; +int numctrl =0 ; +libxl_device_usb *usbs =NULL ; +int numusb =0 ; +int i, rc; + +assert (usbctrl->devid !=-1 ); + + usbctrls =libxl_device_usbctrl_list (ctx, domid, &numctrl); +if (!numctrl) { +LOG (ERROR ,"No USB controller exists in this domain" ); + rc =ERROR_FAIL ; + goto out; + } + +for (i =0 ; i < numctrl; i++) { +if (usbctrl->devid == usbctrls[i].devid) { + usbctrl_find = usbctrls + i; +break ; + } + } + +if (!usbctrl_find) { +LOG (ERROR ,"USB controller %d is not attached to this domain" , + usbctrl->devid); + rc =ERROR_FAIL ; + goto out; + } + + usbctrl = usbctrl_find; + +GCNEW (device); +libxl__device_from_usbctrl (gc, domid, usbctrl, device); + +/* Remove usb devives first */ + rc =libxl__device_usb_list (gc, domid, usbctrl->devid, &usbs, &numusb); +if (rc) goto out; +for (i =0 ; i < numusb; i++) { +if (do_usb_remove (gc, domid, &usbs[i])) { +LOG (ERROR ,"do_usb_remove failed" ); + rc =ERROR_FAIL ; + goto out; + } + } +/* remove usbctrl */ +GCNEW (aodev); +libxl__prepare_ao_device (ao, aodev); + aodev->action =LIBXL__DEVICE_ACTION_REMOVE ; + aodev->dev = device; + aodev->callback = device_addrm_aocomplete; + aodev->force = force; +libxl__initiate_device_remove (egc, aodev); + +out: +free (usbctrls); +free (usbs); +if (rc)return AO_ABORT (rc); +return AO_INPROGRESS ; +} + +int libxl_device_usbctrl_remove (libxl_ctx *ctx,uint32_t domid, +libxl_device_usbctrl *usbctrl, +const libxl_asyncop_how *ao_how) +{ +return libxl__device_usbctrl_remove_common (ctx, domid, usbctrl, ao_how,0 ); +} + +int libxl_device_usbctrl_destroy (libxl_ctx *ctx,uint32_t domid, +libxl_device_usbctrl *usbctrl, +const libxl_asyncop_how *ao_how) +{ +return libxl__device_usbctrl_remove_common (ctx, domid, usbctrl, ao_how,1 ); +} + +libxl_device_usbctrl * +libxl_device_usbctrl_list (libxl_ctx *ctx,uint32_t domid,int *num) +{ +GC_INIT (ctx); +libxl_device_usbctrl *usbctrls =NULL ; +char *fe_path =NULL ; +char **dir =NULL ; +unsigned int ndirs =0 ; + + *num =0 ; + + fe_path =GCSPRINTF ("%s/device/vusb" , +libxl__xs_get_dompath (gc, domid)); + dir =libxl__xs_directory (gc,XBT_NULL , fe_path, &ndirs); + +if (dir && ndirs) { + usbctrls =malloc (sizeof (*usbctrls) * ndirs); +libxl_device_usbctrl * usbctrl; +libxl_device_usbctrl * end = usbctrls + ndirs; +for (usbctrl = usbctrls; usbctrl < end; usbctrl++, dir++, (*num)++) { +char *tmp; +const char *be_path =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/%s/backend" , fe_path, *dir)); + +libxl_device_usbctrl_init (usbctrl); + usbctrl->devid =atoi (*dir); + + tmp =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/%s/backend-id" , fe_path, *dir)); +if (!tmp) goto outerr; + usbctrl->backend_domid =atoi (tmp); + + tmp =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/usb-ver" , be_path)); +if (!tmp) goto outerr; + usbctrl->version =atoi (tmp); + + tmp =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/num-ports" , be_path)); +if (!tmp) goto outerr; + usbctrl->ports =atoi (tmp); + }
tools/libxl/libxl_pvusb.c line 324
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ } + +return usbctrls; + +outerr: +LOG (ERROR ,"Unable to list USB Controllers" ); +for (int i =0 ; i < *num; i++) { +libxl_device_usbctrl_dispose (usbctrls + i); + } +free (usbctrls);
It might be useful to provide a function to free the whole list of device. There are similar functions in libxl.
Quoted 90 lines
tools/libxl/libxl_pvusb.c new file +90
-0+ *num =0 ; +return NULL ; +} + +int libxl_device_usbctrl_getinfo (libxl_ctx *ctx,uint32_t domid, +libxl_device_usbctrl *usbctrl, +libxl_usbctrlinfo *usbctrlinfo) +{ +GC_INIT (ctx); +char *dompath, *usbctrlpath; +char *val; +int rc =0 ; + + usbctrlinfo->devid = usbctrl->devid; + usbctrlinfo->ports = usbctrl->ports; + usbctrlinfo->version = usbctrl->version; + + dompath =libxl__xs_get_dompath (gc, domid); + usbctrlpath =GCSPRINTF ("%s/device/vusb/%d" , dompath, usbctrlinfo->devid); + val =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/backend" , usbctrlpath)); + usbctrlinfo->backend =libxl__strdup (NOGC , val); +if (!usbctrlinfo->backend) { + rc =ERROR_FAIL ; + goto out; + } + + val =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/backend-id" , usbctrlpath)); + usbctrlinfo->backend_id = val ?strtoul (val,NULL ,10 ) :-1 ; + + val =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/state" , usbctrlpath)); + usbctrlinfo->state = val ?strtoul (val,NULL ,10 ) :-1 ; + + val =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/event-channel" , usbctrlpath)); + usbctrlinfo->evtch = val ?strtoul (val,NULL ,10 ) :-1 ; + + val =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/urb-ring-ref" , usbctrlpath)); + usbctrlinfo->ref_urb = val ?strtoul (val,NULL ,10 ) :-1 ; + + val =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/conn-ring-ref" , usbctrlpath)); + usbctrlinfo->ref_conn= val ?strtoul (val,NULL ,10 ) :-1 ; + + val =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/frontend" , usbctrlinfo->backend)); + usbctrlinfo->frontend =libxl__strdup (NOGC , val); + + val =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/frontend-id" , usbctrlinfo->backend)); + usbctrlinfo->frontend_id = val ?strtoul (val,NULL ,10 ) :-1 ; + +out: +GC_FREE ; +return rc; +} + +/* usb device functions */ + +static int +libxl__device_usb_assigned_list (libxl__gc *gc, +libxl_device_usb **list,int *num) +{ +char **domlist; +unsigned int nd =0 , i, j; +char *be_path; +libxl_device_usb *usb; + + *list =NULL ; + *num =0 ; + + domlist =libxl__xs_directory (gc,XBT_NULL ,"/local/domain" , &nd); + be_path =GCSPRINTF ("/local/domain/%d/backend/vusb" ,LIBXL_TOOLSTACK_DOMID ); +for (i =0 ; i < nd; i++) { +char *path, *num_ports, **ctrl_list; +unsigned int nc =0 ; + path =GCSPRINTF ("%s/%s" , be_path, domlist[i]); + ctrl_list =libxl__xs_directory (gc,XBT_NULL , path , &nc); + +for (j =0 ; j < nc; j++) { + path =GCSPRINTF ("%s/%s/%s/num-ports" , be_path, + domlist[i], ctrl_list[j]); + num_ports =libxl__xs_read (gc,XBT_NULL , path); +if (num_ports) { +int nport =atoi (num_ports), k; +char *devpath, *busid; +
tools/libxl/libxl_pvusb.c line 424
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+for (k =1 ; k <= nport; k++) { + devpath =GCSPRINTF ("%s/%s/%s/port/%u" , be_path, + domlist[i], ctrl_list[j], k); + busid =libxl__xs_read (gc,XBT_NULL , devpath); +/* If there are USB device attached, add it to list */ +if (busid &&strcmp (busid,"" ) ) { + *list =realloc (*list, +sizeof (libxl_device_usb) * ((*num) +1 )); +if (*list ==NULL ) +return ERROR_NOMEM ;
Use GCREALLOC_ARRAY.
tools/libxl/libxl_pvusb.c line 436
Quoted 12 lines
tools/libxl/libxl_pvusb.c new file +12
-0+ usb = *list + *num; + usb->ctrl =atoi (ctrl_list[j]); + usb->port = k; + usb->busid =strdup (busid); + (*num)++; + } + } + } + } + } +libxl__ptr_add (gc, *list); +
No need to do this if you use GCREALLOC_ARRAY.
Quoted 65 lines
tools/libxl/libxl_pvusb.c new file +65
-0+return 0 ; +} + +static bool is_usb_in_array (libxl_device_usb *usbs,int num, +libxl_device_usb *usb) +{ +int i; + +for (i =0 ; i < num; i++) { +if (!strcmp (usbs[i].busid, usb->busid) ) +return true; + } + +return false; +} + +/* check if USB device is already assigned to a domain */ +static bool is_usb_assigned (libxl__gc *gc,libxl_device_usb *usb) +{ +libxl_device_usb *usbs; +int rc, num; + + rc =libxl__device_usb_assigned_list (gc, &usbs, &num); +if (rc) { +LOG (ERROR ,"Fail to get assigned usb list" ); +return true; + } + +if (is_usb_in_array (usbs, num, usb)) +return true; + +return false; +} + +/* check if USB device type is assignable */ +static bool is_usb_assignable (libxl__gc *gc,libxl_device_usb *usb) +{ +libxl_ctx *ctx =libxl__gc_owner (gc); +int classcode; +char *filename; +void *buf; + +assert (usb->busid); + + filename =GCSPRINTF (SYSFS_USB_DEV "/%s/bDeviceClass" , usb->busid); +if (libxl_read_file_contents (ctx, filename, &buf,NULL ) <0 ) +return false; + +sscanf (buf,"%x" , &classcode); +if (classcode ==USBHUB_CLASS_CODE ) +return false; + +return true; +} + +/* get usb devices under certain usb controller */ +static int libxl__device_usb_list (libxl__gc *gc,uint32_t domid,int usbctrl, +libxl_device_usb **usbs,int *num) +{ +char *be_path, *num_devs; +int n, i; + + *usbs =NULL ; + *num =0 ; +
tools/libxl/libxl_pvusb.c line 511
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ be_path =GCSPRINTF ("%s/backend/vusb/%d/%d" , +libxl__xs_get_dompath (gc,0 ), domid, usbctrl); + num_devs =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/num-ports" , be_path)); +if (!num_devs) +return 0 ; + + n =atoi (num_devs); + *usbs =calloc (n,sizeof (libxl_device_usb)); +
libxl__calloc(NOGC, ...)
Quoted 31 lines
tools/libxl/libxl_pvusb.c new file +31
-0+for (i =0 ; i < n; i++) { +char *busid; +libxl_device_usb *usb =NULL ; + + busid =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/port/%d" , be_path, i +1 )); +if (busid &&strcmp (busid,"" )) { + usb = *usbs + *num; + usb->ctrl = usbctrl; + usb->port = i +1 ; + usb->busid =strdup (busid); + (*num)++; + } + } + +return 0 ; +} + +/* get all usb devices of the domain */ +static libxl_device_usb * +libxl_device_usb_list_all (libxl__gc *gc,uint32_t domid,int *num) +{ +char **usbctrls; +unsigned int nd, i, j; +char *be_path; +int rc; +libxl_device_usb *usbs =NULL ; + + *num =0 ; + + be_path = GCSPRINTF("/local/domain/%d/backend/vusb/%d",
tools/libxl/libxl_pvusb.c line 552
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ LIBXL_TOOLSTACK_DOMID, domid); + usbctrls =libxl__xs_directory (gc,XBT_NULL , be_path, &nd); + +for (i =0 ; i < nd; i++) { +int nc =0 ; +libxl_device_usb *tmp =NULL ; + rc =libxl__device_usb_list (gc, domid,atoi (usbctrls[i]), &tmp, &nc); +if (!nc)continue ; + + usbs =realloc (usbs,sizeof (libxl_device_usb)*((*num) + nc));
libxl__realloc
Quoted 166 lines
tools/libxl/libxl_pvusb.c new file +166
-0+for (j =0 ; j < nc; j++) { + usbs[*num].ctrl = tmp[j].ctrl; + usbs[*num].port = tmp[j].port; + usbs[*num].busid =strdup (tmp[j].busid); + (*num)++; + } +free (tmp); + } +return usbs; +} + +libxl_device_usb *libxl_device_usb_list (libxl_ctx *ctx,uint32_t domid, +int usbctrl,int *num) +{ +GC_INIT (ctx); +libxl_device_usb *usbs =NULL ; + +libxl__device_usb_list (gc, domid, usbctrl, &usbs, num); + +GC_FREE ; +return usbs; +} + +/* set default value */ +static char *usb_busaddr_to_busid (libxl__gc *gc,int bus,int addr) +{ +libxl_ctx *ctx =libxl__gc_owner (gc); +struct dirent *de; +DIR *dir; +char *busid =NULL ; + +if (bus <1 || addr <1 ) +return NULL ; + +if (!(dir =opendir (SYSFS_USB_DEV ))) +return NULL ; + +while ((de =readdir (dir))) { +char *filename; +void *buf; +int busnum =-1 ; +int devnum =-1 ; + +if (!de->d_name) +continue ; + + filename =GCSPRINTF (SYSFS_USB_DEV "/%s/devnum" , de->d_name); +if (!libxl_read_file_contents (ctx, filename, &buf,NULL )) +sscanf (buf,"%x" , &devnum); + + filename =GCSPRINTF (SYSFS_USB_DEV "/%s/busnum" , de->d_name); +if (!libxl_read_file_contents (ctx, filename, &buf,NULL )) +sscanf (buf,"%x" , &busnum); + +if (bus == busnum && addr == devnum) { + busid =strdup (de->d_name); +break ; + } + } + +closedir (dir); +return busid; +} + +/* find first unused controller:port and give that to usb device */ +static int +libxl__device_usb_set_default_usbctrl (libxl__gc *gc,uint32_t domid, +libxl_device_usb *usb) +{ +libxl_ctx *ctx =CTX ; +libxl_device_usbctrl *usbctrls; +libxl_device_usb *usbs =NULL ; +int numctrl, numusb, i, j, rc =-1 ; +char *be_path, *tmp; + + usbctrls =libxl_device_usbctrl_list (ctx, domid, &numctrl); +if ( !numctrl) + goto out; + +for (i =0 ; i < numctrl; i++) { + rc =libxl__device_usb_list (gc, domid, usbctrls[i].devid, + &usbs, &numusb); +if (rc)continue ; + +if (!usbctrls[i].ports || numusb == usbctrls[i].ports) +continue ; + +for (j =1 ; i <= numusb; j++) { + be_path =GCSPRINTF ("%s/backend/vusb/%d/%d/port/%d" , +libxl__xs_get_dompath (gc,0 ), domid, + usbctrls[i].devid, j); + tmp =libxl__xs_read (gc,XBT_NULL , be_path); +if (tmp && !strcmp ( tmp,"" )) { + usb->ctrl = usbctrls[i].devid; + usb->port = j; +break ; + } + } + } + + rc =0 ; + +out: +if (usbctrls) +free (usbctrls); +if (usbs) +free (usbs); +return rc; +} + +static int libxl__device_usb_setdefault (libxl__gc *gc,uint32_t domid, +libxl_device_usb *usb) +{ +char *be_path, *tmp; + +if (usb->ctrl ==-1 ) { +int ret =libxl__device_usb_set_default_usbctrl (gc, domid, usb); +/* If no existing ctrl to host this usb device, setup a new one */ +if (ret) { +libxl_device_usbctrl usbctrl; +libxl_device_usbctrl_init (&usbctrl); +libxl__device_usbctrl_add (gc, domid, &usbctrl); + usb->ctrl = usbctrl.devid; + usb->port =1 ; +libxl_device_usbctrl_dispose (&usbctrl); + } + } + + be_path =GCSPRINTF ("%s/backend/vusb/%d/%d/port/%d" , +libxl__xs_get_dompath (gc,0 ), + domid, usb->ctrl, usb->port); + tmp =libxl__xs_read (gc,XBT_NULL , be_path); +if (!tmp ||strcmp (tmp,"" ) ){ +LOG (ERROR ,"The controller port isn't available" ); +return ERROR_INVAL ; + } + +return 0 ; +} + +/* xenstore usb data */ +static int libxl__device_usb_add_xenstore (libxl__gc *gc,uint32_t domid, +libxl_device_usb *usb) +{ +char *be_path; +int rc; +libxl_domain_config d_config; +libxl_device_usb usb_saved; +libxl__domain_userdata_lock *lock =NULL ; + +libxl_domain_config_init (&d_config); +libxl_device_usb_init (&usb_saved); +libxl_device_usb_copy (CTX , &usb_saved, usb); + + lock =libxl__lock_domain_userdata (gc, domid); +if (!lock) { + rc =ERROR_LOCK_FAIL ; + goto out; + } + + rc =libxl__get_domain_configuration (gc, domid, &d_config); +if (rc) goto out; + +DEVICE_ADD (usb, usbs, domid, &usb_saved,COMPARE_USB , &d_config); + + rc =libxl__set_domain_configuration (gc, domid, &d_config);
tools/libxl/libxl_pvusb.c line 728
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+if (rc) goto out; + + be_path =GCSPRINTF ("%s/backend/vusb/%d/%d/port/%d" , +libxl__xs_get_dompath (gc,0 ), domid, usb->ctrl, usb->port); +LOG (DEBUG ,"Adding new usb device to xenstore" ); +if (libxl__xs_write_checked (gc,XBT_NULL , be_path, usb->busid)) { + rc =ERROR_FAIL ; + goto out; + } +
This is not right. See libxl_internal.h, search for "lock json config" to see the pattern. In short, you should use a xenstore transaction instead of libxl__xs_write_checked with XBT_NULL.
Also you need to use aodev->update_json.
Quoted 124 lines
tools/libxl/libxl_pvusb.c new file +124
-0+ rc =0 ; + +out: +if (lock)libxl__unlock_domain_userdata (lock); +libxl_device_usb_dispose (&usb_saved); +libxl_domain_config_dispose (&d_config); +return rc; +} + +static int libxl__device_usb_remove_xenstore (libxl__gc *gc,uint32_t domid, +libxl_device_usb *usb) +{ +char *be_path; + + be_path =GCSPRINTF ("%s/backend/vusb/%d/%d/port/%d" , +libxl__xs_get_dompath (gc,0 ), + domid, usb->ctrl, usb->port); +LOG (DEBUG ,"Removing USB device from xenstore" ); +if (libxl__xs_write_checked (gc,XBT_NULL , be_path,"" )) +return ERROR_FAIL ; + +return 0 ; +} + +/* bind/unbind usb device interface */ +static int unbind_usb_intf (libxl__gc *gc,char *intf,char **drvpath) +{ +char *path, *spath, *dp =NULL ; +int fd =-1 ; +int rc =0 ; +struct stat st; + + spath =GCSPRINTF (SYSFS_USB_DEV "/%s/driver" , intf); +if (!lstat (spath, &st)) { +/* Find the canonical path to the driver. */ + dp =libxl__zalloc (gc,PATH_MAX ); + dp =realpath (spath, dp); + + path =GCSPRINTF ("%s/unbind" , spath); + fd =open (path,O_WRONLY ); +if (fd <0 ) +return ERROR_FAIL ; + rc =write (fd, intf,strlen (intf)); +close (fd); +if (rc <0 ) +return ERROR_FAIL ; + } + +if (drvpath) + *drvpath = dp; + +return 0 ; +} + +static int bind_usb_intf (libxl__gc *gc,char *intf,char *drvpath) +{ +char *path; +struct stat st; +int fd, rc =0 ; + + path =GCSPRINTF ("%s/%s" , drvpath, intf); + rc =lstat (path, &st); +/* already bind, return */ +if (rc ==0 ) +return 0 ; + + path =GCSPRINTF ("%s/bind" , drvpath); + fd =open (path,O_WRONLY ); +if (fd <0 ) +return ERROR_FAIL ; + + rc =write (fd, intf,strlen (intf)); +close (fd); +if (rc <0 ) +return ERROR_FAIL ; + +return 0 ; +} + +/* Is usb interface bound to usbback? */ +static int usb_intf_is_assigned (libxl__gc *gc,char *intf) +{ +char *spath; +int rc; +struct stat st; + + spath =GCSPRINTF (SYSFS_USBBACK_DRIVER "/%s" , intf); + rc =lstat (spath, &st); + +if (rc ==0 ) +return 1 ; +if (rc <0 && errno ==ENOENT ) +return 0 ; +LOGE (ERROR ,"Accessing %s" , spath); +return -1 ; +} + +static int usb_get_all_interfaces (libxl__gc *gc,libxl_device_usb *usb, +char ***intfs,int *num) +{ +DIR *dir; +struct dirent *entry; +char *buf; +int rc =0 ; + + *intfs =NULL ; + *num =0 ; + + buf =GCSPRINTF ("%s:" , usb->busid); + +if (!(dir =opendir (SYSFS_USB_DEV ))) { + rc =ERROR_FAIL ; + goto out; + } + +while ((entry =readdir (dir)) !=NULL ) { +if (!strncmp (entry->d_name, buf,strlen (buf))){ + *intfs =realloc (*intfs,sizeof (char *) * (*num +1 )); +if (*intfs ==NULL ) { + rc =ERROR_FAIL ; + goto out; + } + (*intfs)[*num] =strdup (entry->d_name); + (*num)++;
tools/libxl/libxl_pvusb.c line 862
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ } + } + +closedir (dir); + +out: +return rc; +} + +/* Cann't write '.' or ':' into Xenstore as key. So, change '.' to '_',
"Can't"
And this behaviour needs to be documented.
tools/libxl/libxl_pvusb.c line 873
Quoted 11 lines
tools/libxl/libxl_pvusb.c new file +11
-0+* change ':' to '-'. +*/ +static char *usb_interface_encode (char *busid) +{ +char *str =strdup (busid); +int i, len =strlen (str); + +for (i =0 ; i < len; i++) { +if (str[i] =='.' ) + str[i] ='_' ; +if (str[i] ==':' )
Indentation.
Quoted 171 lines
tools/libxl/libxl_pvusb.c new file +171
-0+ str[i] ='-' ; + } +return str; +} + +/* unbind usb device from usbback driver, if there are many interfaces +* under the usb device, then check each interface, unbind from usbback +* driver and rebind to original driver +*/ +static int usbback_dev_unassign (libxl__gc *gc,libxl_device_usb *usb) +{ +char **intfs =NULL ; +char *path; +int num =0 , i; +int rc =0 ; +char *usb_encode =NULL ; + +if (usb_get_all_interfaces (gc, usb, &intfs, &num) <0 ) +return ERROR_FAIL ; + + usb_encode =usb_interface_encode (usb->busid); + +for (i =0 ; i < num; i++){ +char *intf = intfs[i]; +char *drvpath =NULL ; + +if (usb_intf_is_assigned (gc, intf) >0 ) { +/* unbind interface from usbback driver */ +if (unbind_usb_intf (gc, intf,NULL ) <0 ) { + rc =ERROR_FAIL ; + goto out; + } + } + +/* bind interface to its originial driver */ + drvpath =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF (USBBACK_INFO_PATH "/%s/%s/driver_path" , + usb_encode,usb_interface_encode (intf))); +if (drvpath) { +if (bind_usb_intf (gc, intf, drvpath) <0 ) { +LOGE (ERROR ,"Couldn't bind %s to %s" , intf, drvpath); + rc =ERROR_FAIL ; + goto out; + } + } + } + +/* finally, remove xs driver path */ + path =GCSPRINTF (USBBACK_INFO_PATH "/%s" , usb_encode); +libxl__xs_rm_checked (gc,XBT_NULL , path); + +out: +if (intfs) { +for (i =0 ; i < num; i++) +free (intfs[i]); +free (intfs); + } +free (usb_encode); +return rc; +} + +/* bind usb device to "usbback" driver, if there are many interfaces +* under the usb device, check each interface, unbind from original +* driver and bind to usbback driver. +*/ +static int usbback_dev_assign (libxl__gc *gc,libxl_device_usb *usb) +{ +char **intfs =NULL ; +int num =0 , i; +int rc =0 ; +char *usb_encode =NULL ; + +if (usb_get_all_interfaces (gc, usb, &intfs, &num) <0 ) +return ERROR_FAIL ; + + usb_encode =usb_interface_encode (usb->busid); + +for (i =0 ; i < num; i++){ +char *intf = intfs[i]; +char *path =NULL ; +char *drvpath =NULL ; + +/* already assigned to usbback */ +if (usb_intf_is_assigned (gc, intf) >0 ) +continue ; + +/* unbind interface from original driver */ +if (unbind_usb_intf (gc, intf, &drvpath) <0 ) { + rc =ERROR_FAIL ; + goto out_rebind; + } + +if (drvpath) { +/* write driver path to xenstore for later rebinding */ + path =GCSPRINTF (USBBACK_INFO_PATH "/%s/%s/driver_path" , + usb_encode,usb_interface_encode (intf)); +if (libxl__xs_write_checked (gc,XBT_NULL , path, drvpath) <0 ) { +LOG (WARN ,"Write of %s to node %s failed" , drvpath, path); + } + } + +/* bind interface to usbback */ +if (bind_usb_intf (gc, intf,SYSFS_USBBACK_DRIVER ) <0 ){ +LOGE (ERROR ,"Couldn't bind %s to %s" , intf,SYSFS_USBBACK_DRIVER ); + rc =ERROR_FAIL ; + goto out_rebind; + } + } + + goto out; + +out_rebind: +/* some interfaces might be bound to usbback, unbind it then and +* rebind to its original driver +*/ +usbback_dev_unassign (gc, usb); +out: +if (intfs) { +for (i =0 ; i < num; i++) +free (intfs[i]); +free (intfs); + } +free (usb_encode); +return rc; +} + +static int do_usb_add (libxl__gc *gc,uint32_t domid,libxl_device_usb *usb) +{ +int rc =0 ; + + rc =libxl__device_usb_add_xenstore (gc, domid, usb); +if (rc) goto out; + + rc =usbback_dev_assign (gc, usb); +if (rc) +libxl__device_usb_remove_xenstore (gc, domid, usb); + +out: +return rc; +} + +int libxl__device_usb_add (libxl__gc *gc,uint32_t domid,libxl_device_usb *usb) +{ +int rc; + + usb->busid =usb_busaddr_to_busid (gc, usb->hostbus, usb->hostaddr); +if (!usb->busid) { +LOG (ERROR ,"USB device doesn't exist in sysfs" ); +return ERROR_INVAL ; + } + +if (!is_usb_assignable (gc, usb)) { +LOG (ERROR ,"USB device is not assignable." ); + rc =ERROR_FAIL ; + goto out; + } + +/* check usb device is already assigned by pvusb */ +if (is_usb_assigned (gc, usb)) { +LOG (ERROR ,"USB device is already attached to a domain." ); + rc =ERROR_FAIL ; + goto out; + } + + rc =libxl__device_usb_setdefault (gc, domid, usb); +if (rc) goto out; + + rc =do_usb_add (gc, domid, usb); + +out: +return rc;
tools/libxl/libxl_pvusb.c line 1054
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+} + +int libxl_device_usb_add (libxl_ctx *ctx,uint32_t domid, +libxl_device_usb *usb, +const libxl_asyncop_how *ao_how) +{ +AO_CREATE (ctx, domid, ao_how); +int rc; + + rc =libxl__device_usb_add (gc, domid, usb);
Hmm... Your remove function is async and this one is sync. See comment of usbctrl_add.
Quoted 56 lines
tools/libxl/libxl_pvusb.c new file +56
-0+libxl__ao_complete (egc, ao, rc); +return AO_INPROGRESS ; +} + +static int do_usb_remove (libxl__gc *gc,uint32_t domid, +libxl_device_usb *usb) +{ +if (libxl__device_usb_remove_xenstore (gc, domid, usb)) +return -1 ; + +usbback_dev_unassign (gc, usb); + +return 0 ; +} + +static int libxl__device_usb_remove (libxl__gc *gc,uint32_t domid, +libxl_device_usb *usb) +{ +libxl_device_usb *usbs =NULL ; +libxl_device_usb *usb_find =NULL ; +int i, num, rc; + + usb->busid =usb_busaddr_to_busid (gc, usb->hostbus, usb->hostaddr); +if (!usb->busid) { +LOG (ERROR ,"USB device doesn't exist in sysfs" ); +return ERROR_INVAL ; + } + + usbs =libxl_device_usb_list_all (gc, domid, &num); +if (!usbs) { +LOG (ERROR ,"No USB device attached to this domain" ); +return ERROR_FAIL ; + } + +for (i =0 ; i < num; i++) { +if (!strcmp (usb->busid, usbs[i].busid)) { + usb_find = usbs + i; +break ; + } + } + +/* doesn't find the usb device in domain's usb device list*/ +if (!usb_find) { +LOG (ERROR ,"USB device %x.%x is not attached to this domain" , + usb->hostbus, usb->hostaddr); + rc =ERROR_FAIL ; + goto out; + } + + rc =do_usb_remove (gc, domid, usb_find); + +out: +free (usbs); +return rc; +} +
tools/libxl/libxl_pvusb.c line 1120
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+int libxl_device_usb_remove (libxl_ctx *ctx,uint32_t domid, +libxl_device_usb *usb, +const libxl_asyncop_how *ao_how) + +{ +AO_CREATE (ctx, domid, ao_how); +int rc; + + rc =libxl__device_usb_remove (gc, domid, usb); +
Same here.
Wei.
Re: [PATCH V3 3/6] libxl: add pvusb API
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
On 5/19/2015 at 02:07 AM, in message
<[email protected]>, Wei Liu <[email protected]>
Quoted 33 lines · wrote:
(I look at overall code structure this pass. I haven't done a line by line review.)
Quoted 6 lines · Chunyan Liu
Add pvusb APIs, including:
- attach/detach (create/destroy) virtual usb controller.
- attach/detach usb device
- list usb controller and usb devices
- some other helper functionsQuoted 6 lines
Signed-off-by Chunyan Liu <[email protected]>Signed-off-by Simon Cao <[email protected]>---
Changes to v2:
* remove qemu emulated usb related definitions, keep the work pure pvusb. In last patch, will do all refactor work to unify pvusb and qemuemulated
Quoted 10 lines
usb.
* use bus.addr as user interface instead of busid to do usb-attach|detach
* remove usb-assignable-list APIs and some other unnecessary APIs
* reuselibxl_read_file_contentsfunction instead of another new function to handle getting sysfs file content
* fix build on different platforms as pci does
* fix many coding style problems
* address other comments in last version
* adjust codes to let it look betterQuoted 6 lines
tools/libxl/Makefile | 2 +- tools/libxl/libxl.c | 2 + tools/libxl/libxl.h | 45 ++ tools/libxl/libxl_internal.h | 11 +- tools/libxl/libxl_osdeps.h | 13 + tools/libxl/libxl_pvusb.c | 1201
Quoted 6 lines
++++++++++++++++++++++++++++++++++
Quoted 2 lines
tools/libxl/libxl_types.idl | 41 ++ tools/libxl/libxl_types_internal.idl | 1 +You also need to document the xenstore keys and values somewhere under docs directory.
Thanks, will add that.
Quoted 2 lines
And you forgot to update
libxl_retrieve_domain_configurationfunction.
Updating.
Quoted 200 lines
tools/libxl/Makefile LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o
Quoted 9 lines
8 files changed, 1314 insertions(+), 2 deletions(-) create mode 100644 tools/libxl/libxl_pvusb.ctools/libxl/Makefile +0
-0@@ -95,7 +95,7 @@ LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.olibxl_pci.o \
tools/libxl/libxl.c void libxl__destroy_domid(libxl__egc *egc,
Quoted 13 lines
tools/libxl/Makefile +1
-1libxl_internal.o libxl_utils.o libxl_uuid.o \ libxl_json.o libxl_aoutils.o libxl_numa.o libxl_vnuma.o \ libxl_save_callout.o _libxl_save_msgs_callout.o \ - libxl_qmp.o libxl_event.o libxl_fork.o $(LIBXL_OBJS-y) + libxl_qmp.o libxl_event.o libxl_fork.o libxl_pvusb.o $(LIBXL_OBJS-y) LIBXL_OBJS += libxl_genid.o LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.otools/libxl/libxl.c +0
-0@@ -1611,6 +1611,8 @@ void libxl__destroy_domid(libxl__egc *egc,
libxl__destroy_domid_state*dis)tools/libxl/libxl.c void libxl__destroy_domid(libxl__egc *egc, · line 1612
Quoted 3 lines
tools/libxl/libxl.c +0
-0if (libxl__device_pci_destroy_all (gc, domid) <0 ) LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "pci shutdown failed for domid%d", domid);
tools/libxl/libxl.c void libxl__destroy_domid(libxl__egc *egc, · line 1614
Quoted 2 lines
tools/libxl/libxl.c +2
-0+if (libxl__device_usb_destroy_all (gc, domid) <0 ) + LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "usb shutdown failed for domid%d", domid);
tools/libxl/libxl.c void libxl__destroy_domid(libxl__egc *egc, libxl__destroy_domid_state *dis)
Quoted 3 lines
tools/libxl/libxl.c +0
-0rc =xc_domain_pause (ctx->xch, domid);if (rc <0 ) { LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc, "xc_domain_pausefailed for %d", domid);
tools/libxl/libxl.h int libxl_cdrom_insert(libxl_ctx *ctx, uint32_t
Quoted 18 lines
tools/libxl/libxl.h +6
-0@@ -114,6 +114,12 @@#define LIBXL_HAVE_DOMAIN_NODEAFFINITY 1/* +* LIBXL_HAVE_PVUSB indicates the functions for doing hot-plug of +* USB devices through pvusb. +*/ +#define LIBXL_HAVE_PVUSB 1 + +/* * LIBXL_HAVE_BUILDINFO_HVM_VENDOR_DEVICE indicates that the * libxl_vendor_device field is present in the hvm sections of * libxl_domain_build_info. This field tells libxl which @@ -1224,6 +1230,45 @@ int libxl_cdrom_insert(libxl_ctx *ctx, uint32_tdomid,
libxl_device_disk*disk,Quoted 20 lines
tools/libxl/libxl.h +17
-0const libxl_asyncop_how *ao_how)LIBXL_EXTERNAL_CALLERS_ONLY ; +/* USB Controllers*/ +int libxl_device_usbctrl_add(libxl_ctx *ctx, uint32_t domid, + libxl_device_usbctrl *usbctrl, + const libxl_asyncop_how *ao_how) + LIBXL_EXTERNAL_CALLERS_ONLY; + +int libxl_device_usbctrl_remove(libxl_ctx *ctx, uint32_t domid, + libxl_device_usbctrl *usbctrl, + const libxl_asyncop_how *ao_how) + LIBXL_EXTERNAL_CALLERS_ONLY; + +int libxl_device_usbctrl_destroy(libxl_ctx *ctx, uint32_t domid, + libxl_device_usbctrl *usbctrl, + const libxl_asyncop_how *ao_how) + LIBXL_EXTERNAL_CALLERS_ONLY; + +libxl_device_usbctrl *libxl_device_usbctrl_list(libxl_ctx *ctx,tools/libxl/libxl.h int libxl_cdrom_insert(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk,
Quoted 10 lines
tools/libxl/libxl.h +10
-0+ uint32_t domid, int *num); + + +int libxl_device_usbctrl_getinfo(libxl_ctx *ctx, uint32_t domid, + libxl_device_usbctrl *usbctrl, + libxl_usbctrlinfo *usbctrlinfo) + LIBXL_EXTERNAL_CALLERS_ONLY; + +/* USB Devices */ +int libxl_device_usb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_usb*usb,
tools/libxl/libxl.h int libxl_cdrom_insert(libxl_ctx *ctx, uint32_t · line 1263
Quoted 4 lines
tools/libxl/libxl.h +4
-0+ const libxl_asyncop_how *ao_how) +LIBXL_EXTERNAL_CALLERS_ONLY ; + +int libxl_device_usb_remove(libxl_ctx *ctx, uint32_t domid,
libxl_device_usb*usb,tools/libxl/libxl.h int libxl_cdrom_insert(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk,
Quoted 7 lines
tools/libxl/libxl.h +7
-0+ const libxl_asyncop_how *ao_how) +LIBXL_EXTERNAL_CALLERS_ONLY ; + +libxl_device_usb *libxl_device_usb_list (libxl_ctx *ctx,uint32_t domid, +int usbctrl,int *num); + +int libxl_device_usb_getinfo(libxl_ctx *ctx, char *intf, libxl_usbinfo*usbinfo)
tools/libxl/libxl.h int libxl_cdrom_insert(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk,
Quoted 3 lines
tools/libxl/libxl.h +1
-0+LIBXL_EXTERNAL_CALLERS_ONLY ;/* Network Interfaces */ int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic*nic,
tools/libxl/libxl_internal.h _hidden void libxl__device_vtpm_add(libxl__egc *egc,
Quoted 6 lines
tools/libxl/libxl.h +0
-0const libxl_asyncop_how *ao_how)tools/libxl/libxl_internal.h +0
-0@@ -2422,6 +2422,13 @@ _hidden void libxl__device_vtpm_add(libxl__egc *egc,
uint32_tdomid,tools/libxl/libxl_internal.h static inline void libxl__update_config_vtpm(libxl__gc
Quoted 14 lines
tools/libxl/libxl_internal.h +7
-0libxl_device_vtpm *vtpm, libxl__ao_device *aodev); +/* from libxl_usb */ +_hidden int libxl__device_usbctrl_add(libxl__gc *gc, uint32_t domid, +libxl_device_usbctrl *usbctrl); +_hidden int libxl__device_usb_add(libxl__gc *gc, uint32_t domid, +libxl_device_usb *usb); +_hidden int libxl__device_usb_destroy_all(libxl__gc *gc, uint32_t domid); +/* Internal function to connect a vkb device */ _hidden int libxl__device_vkb_add(libxl__gc *gc, uint32_t domid,libxl_device_vkb *vkb);@@ -3628,7 +3635,9 @@ static inline void libxl__update_config_vtpm(libxl__gc*gc,
Quoted 60 lines
tools/libxl/libxl_internal.h +3
-1#define COMPARE_PCI (a, b) ((a)->func == (b)->func && \ (a)->bus == (b)->bus && \ (a)->dev == (b)->dev) - +#define COMPARE_USB (a, b) (!strcmp((a)->busid, (b)->busid)) +#define COMPARE_USBCTRL (a, b) ((a)->devid == (b)->devid) +/* DEVICE_ADD * * Add a device in libxl_domain_config structure 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,12 +42,21 @@#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"#include <libutil.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 +0
-0tools/libxl/libxl_pvusb.c line 3
Quoted 3 lines
tools/libxl/libxl_pvusb.c new file +3
-0@@ -0,0 +1,1201 @@ +/* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License as published
Quoted 12 lines
tools/libxl/libxl_pvusb.c new file +12
-0+* by the Free Software Foundation; version 2.1 only. with the special +* exception on linking described in file LICENSE. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License for more details. +*/ + +#include "libxl_osdeps.h" /* must come before any other headers */ + +#include "libxl_internal.h"
Quoted 8 lines
tools/libxl/libxl_pvusb.c line 20
Quoted 5 lines
tools/libxl/libxl_pvusb.c new file +5
-0+ +#define USBBACK_INFO_PATH "/libxl/usbback" + +#define USBHUB_CLASS_CODE 0x09 +I'm not very familiar with how USB is supposed to work. Can you explain why this particular value is chosen?
0x09 is the USB class code for USB HUB defined in USB Spec. USB Class code is stored under Linux sysfs too. Since USB HUB could not be assigned to guest, we need to check that.
Quoted 13 lines
tools/libxl/libxl_pvusb.c line 28
Quoted 9 lines
tools/libxl/libxl_pvusb.c new file +8
-0+static int libxl__device_usbctrl_setdefault (libxl__gc *gc,uint32_t domid, +libxl_device_usbctrl *usbctrl) +{ +int rc; + +if (!usbctrl->version) + usbctrl->version =2 ; +How hard would it be to implement USB 3? I assume this depends on QEMU's support? I.e. we just need to specify the version to 3 and it should just work? Just curious.
This implementation is coworked with PVUSB driver, so it depends on PVUSB driver's support. If PVUSB driver supports that, just specify the version to 3 and can work.
QEMU emulated USB device depends on QEMU's support.
Quoted 123 lines
Quoted 26 lines
tools/libxl/libxl_pvusb.c new file +25
-0+if (!usbctrl->ports) + usbctrl->ports =8 ; + + rc =libxl__resolve_domid (gc, usbctrl->backend_domname, + &usbctrl->backend_domid); +return rc; +} + +static void libxl__device_from_usbctrl (libxl__gc *gc,uint32_t domid, +libxl_device_usbctrl *usbctrl, +libxl__device *device) +{ + device->backend_devid = usbctrl->devid; + device->backend_domid = usbctrl->backend_domid; + device->backend_kind =LIBXL__DEVICE_KIND_VUSB ; + device->devid = usbctrl->devid; + device->domid = domid; + device->kind =LIBXL__DEVICE_KIND_VUSB ; +} + +static int libxl__usbport_add_xenstore (libxl__gc *gc, +xs_transaction_t tran, +uint32_t domid, +libxl_device_usbctrl *usbctrl) +{tools/libxl/libxl_pvusb.c
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+char *path; +int i; + + path =GCSPRINTF ("%s/backend/vusb/%d/%d/port" , +libxl__xs_get_dompath (gc,0 ), domid, usbctrl->devid); + +libxl__xs_mkdir (gc, tran, path,NULL ,0 ); + +for (i =1 ; i <= usbctrl->ports; i++) { + if (libxl__xs_write_checked(gc, tran, GCSPRINTF("%s/%d", path, i),""))
Quoted 22 lines
tools/libxl/libxl_pvusb.c new file +22
-0+return ERROR_FAIL ; + } + +return 0 ; +} + +static int libxl__usbctrl_add_xenstore (libxl__gc *gc,uint32_t domid, +libxl_device_usbctrl *usbctrl) +{ +flexarray_t *front; +flexarray_t *back; +libxl__device *device; +xs_transaction_t t =XBT_NULL ; +int rc =0 ; +libxl_domain_config d_config; +libxl_device_usbctrl usbctrl_saved; +libxl__domain_userdata_lock *lock =NULL ; + +libxl_domain_config_init (&d_config); +libxl_device_usbctrl_init (&usbctrl_saved); +libxl_device_usbctrl_copy (CTX , &usbctrl_saved, usbctrl); +tools/libxl/libxl_pvusb.c
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+GCNEW (device); +libxl__device_from_usbctrl (gc, domid, usbctrl, device); + + front =flexarray_make (gc,4 ,1 ); + back =flexarray_make (gc,12 ,1 ); + +flexarray_append_pair (back,"frontend-id" ,GCSPRINTF ("%d" , domid)); +flexarray_append_pair (back,"online" ,"1" ); +flexarray_append_pair (back,"state" ,"1" ); + flexarray_append_pair(back, "usb-ver", GCSPRINTF("%d",
usbctrl->version));tools/libxl/libxl_pvusb.c line 96
Quoted 1 line
tools/libxl/libxl_pvusb.c new file +1
-0+ flexarray_append_pair(back, "num-ports", GCSPRINTF("%d",
usbctrl->ports));tools/libxl/libxl_pvusb.c line 97
Quoted 1 line
tools/libxl/libxl_pvusb.c new file +1
-0+ flexarray_append_pair(front, "backend-id", GCSPRINTF("%d",
usbctrl->backend_domid));Line too long.
tools/libxl/libxl_pvusb.c
Quoted 12 lines
tools/libxl/libxl_pvusb.c new file +12
-0+flexarray_append_pair (front,"state" ,"1" ); + + lock =libxl__lock_domain_userdata (gc, domid); +if (!lock) { + rc =ERROR_LOCK_FAIL ; + goto out; + } + + rc =libxl__get_domain_configuration (gc, domid, &d_config); +if (rc) goto out; + + DEVICE_ADD(usbctrl, usbctrls, domid, &usbctrl_saved, COMPARE_USBCTRL,
&d_config);tools/libxl/libxl_pvusb.c line 110
Quoted 1 line
tools/libxl/libxl_pvusb.c new file +1
-0+Line too long.
Quoted 18 lines
tools/libxl/libxl_pvusb.c new file +18
-0+for (;;) { + rc =libxl__xs_transaction_start (gc, &t); +if (rc) goto out; + + rc =libxl__device_exists (gc, t, device); +if (rc <0 ) goto out; +if (rc ==1 ) { +/* already exists in xenstore */ +LOG (ERROR ,"device already exists in xenstore" ); + rc =ERROR_DEVICE_EXISTS ; + goto out; + } + + rc =libxl__set_domain_configuration (gc, domid, &d_config); +if (rc) goto out; + + libxl__device_generic_add(gc, t, device, + libxl__xs_kvs_of_flexarray(gc, back,tools/libxl/libxl_pvusb.c line 138
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ back->count), +libxl__xs_kvs_of_flexarray (gc, front, + front->count), + NULL); +libxl__usbport_add_xenstore (gc, t, domid, usbctrl); + rc =libxl__xs_transaction_commit (gc, &t); +if (!rc)break ; +if (rc <0 ) goto out; + } +
Quoted 7 lines
You don't have aodev so you cannot check
update_json. Maybe you need aodev?That field
update_jsonis set to true when doing hotplug. It's set to false during domain creation.The same comment applies to other add functions as well.
Thanks, I'm updating that. But maybe like pci_add and pci_remove functions, will add a 'starting' flag to indicate hotplug or creation. Looking at DEFINE_DEVICE_ADD and DEFINE_DEVICE_REMOVE, usbctrl_add and usb_add can use DEFINE_DEVICE_ADD; but usbctrl_remove and usb_remove cannot use DEFINE_DEVICE_REMOVE directly, need some extra handling. So, finally turned to not use these macros but refer to pci functions.
Quoted 35 lines
Quoted 35 lines
tools/libxl/libxl_pvusb.c new file +34
-0+out: +if (lock)libxl__unlock_domain_userdata (lock); +libxl_device_usbctrl_dispose (&usbctrl_saved); +libxl_domain_config_dispose (&d_config); +return rc; +} + +int libxl__device_usbctrl_add (libxl__gc *gc,uint32_t domid, +libxl_device_usbctrl *usbctrl) +{ +int rc =0 ; + + rc =libxl__device_usbctrl_setdefault (gc, domid, usbctrl); +if (rc <0 ) goto out; + +if (usbctrl->devid ==-1 ) { + usbctrl->devid =libxl__device_nextid (gc, domid,"vusb" ); +if (usbctrl->devid <0 ) { + rc =ERROR_FAIL ; + goto out; + } + } + +if (libxl__usbctrl_add_xenstore (gc, domid, usbctrl) <0 ){ + rc =ERROR_FAIL ; + goto out; + } + +out: +return rc; +} + +int libxl_device_usbctrl_add(libxl_ctx *ctx, uint32_t domid, + libxl_device_usbctrl *usbctrl,
Quoted 8 lines
tools/libxl/libxl_pvusb.c line 178
Quoted 6 lines
tools/libxl/libxl_pvusb.c new file +6
-0+ const libxl_asyncop_how *ao_how) +{ +AO_CREATE (ctx, domid, ao_how); +int rc; + + rc =libxl__device_usbctrl_add (gc, domid, usbctrl);Hmm... Your remove function is async while this one is sync, why?
Hmm, maybe not proper here, just referred to pci_add implementation. Current calling places only use sync mode.
Quoted 154 lines
tools/libxl/libxl_pvusb.c
Quoted 6 lines
tools/libxl/libxl_pvusb.c new file +5
-0+libxl__ao_complete (egc, ao, rc); +return AO_INPROGRESS ; +} + +static int libxl__device_usb_list(libxl__gc *gc, uint32_t domid, intusbctrl,
Quoted 70 lines
tools/libxl/libxl_pvusb.c new file +70
-0+ libxl_device_usb **usbs, int *num); + +static int do_usb_remove (libxl__gc *gc,uint32_t domid, +libxl_device_usb *usb); + +static int +libxl__device_usbctrl_remove_common (libxl_ctx *ctx,uint32_t domid, +libxl_device_usbctrl *usbctrl, +const libxl_asyncop_how *ao_how, +int force) +{ +AO_CREATE (ctx, domid, ao_how); +libxl__device *device; +libxl__ao_device *aodev; +libxl_device_usbctrl *usbctrls =NULL ; +libxl_device_usbctrl *usbctrl_find =NULL ; +int numctrl =0 ; +libxl_device_usb *usbs =NULL ; +int numusb =0 ; +int i, rc; + +assert (usbctrl->devid !=-1 ); + + usbctrls =libxl_device_usbctrl_list (ctx, domid, &numctrl); +if (!numctrl) { +LOG (ERROR ,"No USB controller exists in this domain" ); + rc =ERROR_FAIL ; + goto out; + } + +for (i =0 ; i < numctrl; i++) { +if (usbctrl->devid == usbctrls[i].devid) { + usbctrl_find = usbctrls + i; +break ; + } + } + +if (!usbctrl_find) { +LOG (ERROR ,"USB controller %d is not attached to this domain" , + usbctrl->devid); + rc =ERROR_FAIL ; + goto out; + } + + usbctrl = usbctrl_find; + +GCNEW (device); +libxl__device_from_usbctrl (gc, domid, usbctrl, device); + +/* Remove usb devives first */ + rc =libxl__device_usb_list (gc, domid, usbctrl->devid, &usbs, &numusb); +if (rc) goto out; +for (i =0 ; i < numusb; i++) { +if (do_usb_remove (gc, domid, &usbs[i])) { +LOG (ERROR ,"do_usb_remove failed" ); + rc =ERROR_FAIL ; + goto out; + } + } +/* remove usbctrl */ +GCNEW (aodev); +libxl__prepare_ao_device (ao, aodev); + aodev->action =LIBXL__DEVICE_ACTION_REMOVE ; + aodev->dev = device; + aodev->callback = device_addrm_aocomplete; + aodev->force = force; +libxl__initiate_device_remove (egc, aodev); + +out: +free (usbctrls);tools/libxl/libxl_pvusb.c
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+free (usbs); +if (rc)return AO_ABORT (rc); +return AO_INPROGRESS ; +} + +int libxl_device_usbctrl_remove (libxl_ctx *ctx,uint32_t domid, +libxl_device_usbctrl *usbctrl, +const libxl_asyncop_how *ao_how) +{ + return libxl__device_usbctrl_remove_common(ctx, domid, usbctrl,
ao_how, 0);tools/libxl/libxl_pvusb.c
Quoted 7 lines
tools/libxl/libxl_pvusb.c new file +7
-0+} + +int libxl_device_usbctrl_destroy (libxl_ctx *ctx,uint32_t domid, +libxl_device_usbctrl *usbctrl, +const libxl_asyncop_how *ao_how) +{ + return libxl__device_usbctrl_remove_common(ctx, domid, usbctrl,
ao_how, 1);Quoted 12 lines
tools/libxl/libxl_pvusb.c new file +12
-0+} + +libxl_device_usbctrl * +libxl_device_usbctrl_list (libxl_ctx *ctx,uint32_t domid,int *num) +{ +GC_INIT (ctx); +libxl_device_usbctrl *usbctrls =NULL ; +char *fe_path =NULL ; +char **dir =NULL ; +unsigned int ndirs =0 ; + + *num =0 ;tools/libxl/libxl_pvusb.c
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ + fe_path =GCSPRINTF ("%s/device/vusb" , +libxl__xs_get_dompath (gc, domid)); + dir =libxl__xs_directory (gc,XBT_NULL , fe_path, &ndirs); + +if (dir && ndirs) { + usbctrls =malloc (sizeof (*usbctrls) * ndirs); +libxl_device_usbctrl * usbctrl; +libxl_device_usbctrl * end = usbctrls + ndirs; +for (usbctrl = usbctrls; usbctrl < end; usbctrl++, dir++, (*num)++){
tools/libxl/libxl_pvusb.c
Quoted 3 lines
tools/libxl/libxl_pvusb.c new file +3
-0+char *tmp; + const char *be_path = libxl__xs_read(gc, XBT_NULL, + GCSPRINTF("%s/%s/backend", fe_path,*dir));
tools/libxl/libxl_pvusb.c
Quoted 6 lines
tools/libxl/libxl_pvusb.c new file +6
-0+ +libxl_device_usbctrl_init (usbctrl); + usbctrl->devid =atoi (*dir); + + tmp = libxl__xs_read(gc, XBT_NULL, + GCSPRINTF("%s/%s/backend-id", fe_path,*dir));
Quoted 13 lines
tools/libxl/libxl_pvusb.c new file +13
-0+if (!tmp) goto outerr; + usbctrl->backend_domid =atoi (tmp); + + tmp =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/usb-ver" , be_path)); +if (!tmp) goto outerr; + usbctrl->version =atoi (tmp); + + tmp =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/num-ports" , be_path)); +if (!tmp) goto outerr; + usbctrl->ports =atoi (tmp); + }tools/libxl/libxl_pvusb.c line 324
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ } + +return usbctrls; + +outerr: +LOG (ERROR ,"Unable to list USB Controllers" ); +for (int i =0 ; i < *num; i++) { +libxl_device_usbctrl_dispose (usbctrls + i); + } +free (usbctrls);
Quoted 2 lines
It might be useful to provide a function to free the whole list of device. There are similar functions in libxl.
Yeah, that would be useful.
Quoted 101 lines
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +9
-0+ *num =0 ; +return NULL ; +} + +int libxl_device_usbctrl_getinfo (libxl_ctx *ctx,uint32_t domid, +libxl_device_usbctrl *usbctrl, +libxl_usbctrlinfo *usbctrlinfo) +{ +GC_INIT (ctx);tools/libxl/libxl_pvusb.c
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+char *dompath, *usbctrlpath; +char *val; +int rc =0 ; + + usbctrlinfo->devid = usbctrl->devid; + usbctrlinfo->ports = usbctrl->ports; + usbctrlinfo->version = usbctrl->version; + + dompath =libxl__xs_get_dompath (gc, domid); + usbctrlpath = GCSPRINTF("%s/device/vusb/%d", dompath,
usbctrlinfo->devid);Quoted 47 lines
tools/libxl/libxl_pvusb.c new file +47
-0+ val =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/backend" , usbctrlpath)); + usbctrlinfo->backend =libxl__strdup (NOGC , val); +if (!usbctrlinfo->backend) { + rc =ERROR_FAIL ; + goto out; + } + + val =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/backend-id" , usbctrlpath)); + usbctrlinfo->backend_id = val ?strtoul (val,NULL ,10 ) :-1 ; + + val =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/state" , usbctrlpath)); + usbctrlinfo->state = val ?strtoul (val,NULL ,10 ) :-1 ; + + val =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/event-channel" , usbctrlpath)); + usbctrlinfo->evtch = val ?strtoul (val,NULL ,10 ) :-1 ; + + val =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/urb-ring-ref" , usbctrlpath)); + usbctrlinfo->ref_urb = val ?strtoul (val,NULL ,10 ) :-1 ; + + val =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/conn-ring-ref" , usbctrlpath)); + usbctrlinfo->ref_conn= val ?strtoul (val,NULL ,10 ) :-1 ; + + val =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/frontend" , usbctrlinfo->backend)); + usbctrlinfo->frontend =libxl__strdup (NOGC , val); + + val =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/frontend-id" , usbctrlinfo->backend)); + usbctrlinfo->frontend_id = val ?strtoul (val,NULL ,10 ) :-1 ; + +out: +GC_FREE ; +return rc; +} + +/* usb device functions */ + +static int +libxl__device_usb_assigned_list (libxl__gc *gc, +libxl_device_usb **list,int *num) +{tools/libxl/libxl_pvusb.c
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+char **domlist; +unsigned int nd =0 , i, j; +char *be_path; +libxl_device_usb *usb; + + *list =NULL ; + *num =0 ; + + domlist =libxl__xs_directory (gc,XBT_NULL ,"/local/domain" , &nd); + be_path = GCSPRINTF("/local/domain/%d/backend/vusb",
LIBXL_TOOLSTACK_DOMID);Quoted 12 lines
tools/libxl/libxl_pvusb.c new file +12
-0+for (i =0 ; i < nd; i++) { +char *path, *num_ports, **ctrl_list; +unsigned int nc =0 ; + path =GCSPRINTF ("%s/%s" , be_path, domlist[i]); + ctrl_list =libxl__xs_directory (gc,XBT_NULL , path , &nc); + +for (j =0 ; j < nc; j++) { + path =GCSPRINTF ("%s/%s/%s/num-ports" , be_path, + domlist[i], ctrl_list[j]); + num_ports =libxl__xs_read (gc,XBT_NULL , path); +if (num_ports) { +int nport =atoi (num_ports), k;tools/libxl/libxl_pvusb.c
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+char *devpath, *busid; + +for (k =1 ; k <= nport; k++) { + devpath =GCSPRINTF ("%s/%s/%s/port/%u" , be_path, + domlist[i], ctrl_list[j], k); + busid =libxl__xs_read (gc,XBT_NULL , devpath); +/* If there are USB device attached, add it to list */ +if (busid &&strcmp (busid,"" ) ) { + *list = realloc(*list, + sizeof(libxl_device_usb) * ((*num) +
Quoted 5 lines
1));
tools/libxl/libxl_pvusb.c line 424
Quoted 2 lines
tools/libxl/libxl_pvusb.c new file +2
-0+if (*list ==NULL ) +return ERROR_NOMEM ;Use
GCREALLOC_ARRAY.
Thanks, will update.
Quoted 93 lines
tools/libxl/libxl_pvusb.c line 436
Quoted 13 lines
tools/libxl/libxl_pvusb.c new file +12
-0+ usb = *list + *num; + usb->ctrl =atoi (ctrl_list[j]); + usb->port = k; + usb->busid =strdup (busid); + (*num)++; + } + } + } + } + } +libxl__ptr_add (gc, *list); +No need to do this if you use
GCREALLOC_ARRAY.Quoted 47 lines
tools/libxl/libxl_pvusb.c new file +47
-0+return 0 ; +} + +static bool is_usb_in_array (libxl_device_usb *usbs,int num, +libxl_device_usb *usb) +{ +int i; + +for (i =0 ; i < num; i++) { +if (!strcmp (usbs[i].busid, usb->busid) ) +return true; + } + +return false; +} + +/* check if USB device is already assigned to a domain */ +static bool is_usb_assigned (libxl__gc *gc,libxl_device_usb *usb) +{ +libxl_device_usb *usbs; +int rc, num; + + rc =libxl__device_usb_assigned_list (gc, &usbs, &num); +if (rc) { +LOG (ERROR ,"Fail to get assigned usb list" ); +return true; + } + +if (is_usb_in_array (usbs, num, usb)) +return true; + +return false; +} + +/* check if USB device type is assignable */ +static bool is_usb_assignable (libxl__gc *gc,libxl_device_usb *usb) +{ +libxl_ctx *ctx =libxl__gc_owner (gc); +int classcode; +char *filename; +void *buf; + +assert (usb->busid); + + filename =GCSPRINTF (SYSFS_USB_DEV "/%s/bDeviceClass" , usb->busid); +if (libxl_read_file_contents (ctx, filename, &buf,NULL ) <0 ) +return false;tools/libxl/libxl_pvusb.c
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ +sscanf (buf,"%x" , &classcode); +if (classcode ==USBHUB_CLASS_CODE ) +return false; + +return true; +} + +/* get usb devices under certain usb controller */ +static int libxl__device_usb_list(libxl__gc *gc, uint32_t domid, intusbctrl,
Quoted 8 lines
tools/libxl/libxl_pvusb.c new file +8
-0+ libxl_device_usb **usbs, int *num) +{ +char *be_path, *num_devs; +int n, i; + + *usbs =NULL ; + *num =0 ; +tools/libxl/libxl_pvusb.c line 511
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ be_path =GCSPRINTF ("%s/backend/vusb/%d/%d" , +libxl__xs_get_dompath (gc,0 ), domid, usbctrl); + num_devs =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/num-ports" , be_path)); +if (!num_devs) +return 0 ; + + n =atoi (num_devs); + *usbs =calloc (n,sizeof (libxl_device_usb)); +
Quoted 1 line
libxl__calloc(NOGC, ...)
Will update.
Quoted 643 lines
Quoted 29 lines
tools/libxl/libxl_pvusb.c new file +28
-0+for (i =0 ; i < n; i++) { +char *busid; +libxl_device_usb *usb =NULL ; + + busid =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/port/%d" , be_path, i +1 )); +if (busid &&strcmp (busid,"" )) { + usb = *usbs + *num; + usb->ctrl = usbctrl; + usb->port = i +1 ; + usb->busid =strdup (busid); + (*num)++; + } + } + +return 0 ; +} + +/* get all usb devices of the domain */ +static libxl_device_usb * +libxl_device_usb_list_all (libxl__gc *gc,uint32_t domid,int *num) +{ +char **usbctrls; +unsigned int nd, i, j; +char *be_path; +int rc; +libxl_device_usb *usbs =NULL ; +tools/libxl/libxl_pvusb.c
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ *num =0 ; + + be_path =GCSPRINTF ("/local/domain/%d/backend/vusb/%d" , +LIBXL_TOOLSTACK_DOMID , domid); + usbctrls =libxl__xs_directory (gc,XBT_NULL , be_path, &nd); + +for (i =0 ; i < nd; i++) { +int nc =0 ; +libxl_device_usb *tmp =NULL ; + rc = libxl__device_usb_list(gc, domid, atoi(usbctrls[i]), &tmp,&nc);
tools/libxl/libxl_pvusb.c line 552
Quoted 3 lines
tools/libxl/libxl_pvusb.c new file +3
-0+if (!nc)continue ; + + usbs =realloc (usbs,sizeof (libxl_device_usb)*((*num) + nc));
libxl__reallocQuoted 166 lines
tools/libxl/libxl_pvusb.c new file +166
-0+for (j =0 ; j < nc; j++) { + usbs[*num].ctrl = tmp[j].ctrl; + usbs[*num].port = tmp[j].port; + usbs[*num].busid =strdup (tmp[j].busid); + (*num)++; + } +free (tmp); + } +return usbs; +} + +libxl_device_usb *libxl_device_usb_list (libxl_ctx *ctx,uint32_t domid, +int usbctrl,int *num) +{ +GC_INIT (ctx); +libxl_device_usb *usbs =NULL ; + +libxl__device_usb_list (gc, domid, usbctrl, &usbs, num); + +GC_FREE ; +return usbs; +} + +/* set default value */ +static char *usb_busaddr_to_busid (libxl__gc *gc,int bus,int addr) +{ +libxl_ctx *ctx =libxl__gc_owner (gc); +struct dirent *de; +DIR *dir; +char *busid =NULL ; + +if (bus <1 || addr <1 ) +return NULL ; + +if (!(dir =opendir (SYSFS_USB_DEV ))) +return NULL ; + +while ((de =readdir (dir))) { +char *filename; +void *buf; +int busnum =-1 ; +int devnum =-1 ; + +if (!de->d_name) +continue ; + + filename =GCSPRINTF (SYSFS_USB_DEV "/%s/devnum" , de->d_name); +if (!libxl_read_file_contents (ctx, filename, &buf,NULL )) +sscanf (buf,"%x" , &devnum); + + filename =GCSPRINTF (SYSFS_USB_DEV "/%s/busnum" , de->d_name); +if (!libxl_read_file_contents (ctx, filename, &buf,NULL )) +sscanf (buf,"%x" , &busnum); + +if (bus == busnum && addr == devnum) { + busid =strdup (de->d_name); +break ; + } + } + +closedir (dir); +return busid; +} + +/* find first unused controller:port and give that to usb device */ +static int +libxl__device_usb_set_default_usbctrl (libxl__gc *gc,uint32_t domid, +libxl_device_usb *usb) +{ +libxl_ctx *ctx =CTX ; +libxl_device_usbctrl *usbctrls; +libxl_device_usb *usbs =NULL ; +int numctrl, numusb, i, j, rc =-1 ; +char *be_path, *tmp; + + usbctrls =libxl_device_usbctrl_list (ctx, domid, &numctrl); +if ( !numctrl) + goto out; + +for (i =0 ; i < numctrl; i++) { + rc =libxl__device_usb_list (gc, domid, usbctrls[i].devid, + &usbs, &numusb); +if (rc)continue ; + +if (!usbctrls[i].ports || numusb == usbctrls[i].ports) +continue ; + +for (j =1 ; i <= numusb; j++) { + be_path =GCSPRINTF ("%s/backend/vusb/%d/%d/port/%d" , +libxl__xs_get_dompath (gc,0 ), domid, + usbctrls[i].devid, j); + tmp =libxl__xs_read (gc,XBT_NULL , be_path); +if (tmp && !strcmp ( tmp,"" )) { + usb->ctrl = usbctrls[i].devid; + usb->port = j; +break ; + } + } + } + + rc =0 ; + +out: +if (usbctrls) +free (usbctrls); +if (usbs) +free (usbs); +return rc; +} + +static int libxl__device_usb_setdefault (libxl__gc *gc,uint32_t domid, +libxl_device_usb *usb) +{ +char *be_path, *tmp; + +if (usb->ctrl ==-1 ) { +int ret =libxl__device_usb_set_default_usbctrl (gc, domid, usb); +/* If no existing ctrl to host this usb device, setup a new one */ +if (ret) { +libxl_device_usbctrl usbctrl; +libxl_device_usbctrl_init (&usbctrl); +libxl__device_usbctrl_add (gc, domid, &usbctrl); + usb->ctrl = usbctrl.devid; + usb->port =1 ; +libxl_device_usbctrl_dispose (&usbctrl); + } + } + + be_path =GCSPRINTF ("%s/backend/vusb/%d/%d/port/%d" , +libxl__xs_get_dompath (gc,0 ), + domid, usb->ctrl, usb->port); + tmp =libxl__xs_read (gc,XBT_NULL , be_path); +if (!tmp ||strcmp (tmp,"" ) ){ +LOG (ERROR ,"The controller port isn't available" ); +return ERROR_INVAL ; + } + +return 0 ; +} + +/* xenstore usb data */ +static int libxl__device_usb_add_xenstore (libxl__gc *gc,uint32_t domid, +libxl_device_usb *usb) +{ +char *be_path; +int rc; +libxl_domain_config d_config; +libxl_device_usb usb_saved; +libxl__domain_userdata_lock *lock =NULL ; + +libxl_domain_config_init (&d_config); +libxl_device_usb_init (&usb_saved); +libxl_device_usb_copy (CTX , &usb_saved, usb); + + lock =libxl__lock_domain_userdata (gc, domid); +if (!lock) { + rc =ERROR_LOCK_FAIL ; + goto out; + } + + rc =libxl__get_domain_configuration (gc, domid, &d_config); +if (rc) goto out; + +DEVICE_ADD (usb, usbs, domid, &usb_saved,COMPARE_USB , &d_config); + + rc =libxl__set_domain_configuration (gc, domid, &d_config);tools/libxl/libxl_pvusb.c line 728
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+if (rc) goto out; + + be_path =GCSPRINTF ("%s/backend/vusb/%d/%d/port/%d" , +libxl__xs_get_dompath (gc,0 ), domid, usb->ctrl, usb->port); +LOG (DEBUG ,"Adding new usb device to xenstore" ); +if (libxl__xs_write_checked (gc,XBT_NULL , be_path, usb->busid)) { + rc =ERROR_FAIL ; + goto out; + } +This is not right. See
libxl_internal.h, search for "lock json config" to see the pattern. In short, you should use a xenstore transaction instead oflibxl__xs_write_checkedwithXBT_NULL.Also you need to use
aodev->update_json.tools/libxl/libxl_pvusb.c
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ rc =0 ; + +out: +if (lock)libxl__unlock_domain_userdata (lock); +libxl_device_usb_dispose (&usb_saved); +libxl_domain_config_dispose (&d_config); +return rc; +} + +static int libxl__device_usb_remove_xenstore(libxl__gc *gc, uint32_tdomid,
Quoted 114 lines
tools/libxl/libxl_pvusb.c new file +114
-0+ libxl_device_usb *usb) +{ +char *be_path; + + be_path =GCSPRINTF ("%s/backend/vusb/%d/%d/port/%d" , +libxl__xs_get_dompath (gc,0 ), + domid, usb->ctrl, usb->port); +LOG (DEBUG ,"Removing USB device from xenstore" ); +if (libxl__xs_write_checked (gc,XBT_NULL , be_path,"" )) +return ERROR_FAIL ; + +return 0 ; +} + +/* bind/unbind usb device interface */ +static int unbind_usb_intf (libxl__gc *gc,char *intf,char **drvpath) +{ +char *path, *spath, *dp =NULL ; +int fd =-1 ; +int rc =0 ; +struct stat st; + + spath =GCSPRINTF (SYSFS_USB_DEV "/%s/driver" , intf); +if (!lstat (spath, &st)) { +/* Find the canonical path to the driver. */ + dp =libxl__zalloc (gc,PATH_MAX ); + dp =realpath (spath, dp); + + path =GCSPRINTF ("%s/unbind" , spath); + fd =open (path,O_WRONLY ); +if (fd <0 ) +return ERROR_FAIL ; + rc =write (fd, intf,strlen (intf)); +close (fd); +if (rc <0 ) +return ERROR_FAIL ; + } + +if (drvpath) + *drvpath = dp; + +return 0 ; +} + +static int bind_usb_intf (libxl__gc *gc,char *intf,char *drvpath) +{ +char *path; +struct stat st; +int fd, rc =0 ; + + path =GCSPRINTF ("%s/%s" , drvpath, intf); + rc =lstat (path, &st); +/* already bind, return */ +if (rc ==0 ) +return 0 ; + + path =GCSPRINTF ("%s/bind" , drvpath); + fd =open (path,O_WRONLY ); +if (fd <0 ) +return ERROR_FAIL ; + + rc =write (fd, intf,strlen (intf)); +close (fd); +if (rc <0 ) +return ERROR_FAIL ; + +return 0 ; +} + +/* Is usb interface bound to usbback? */ +static int usb_intf_is_assigned (libxl__gc *gc,char *intf) +{ +char *spath; +int rc; +struct stat st; + + spath =GCSPRINTF (SYSFS_USBBACK_DRIVER "/%s" , intf); + rc =lstat (spath, &st); + +if (rc ==0 ) +return 1 ; +if (rc <0 && errno ==ENOENT ) +return 0 ; +LOGE (ERROR ,"Accessing %s" , spath); +return -1 ; +} + +static int usb_get_all_interfaces (libxl__gc *gc,libxl_device_usb *usb, +char ***intfs,int *num) +{ +DIR *dir; +struct dirent *entry; +char *buf; +int rc =0 ; + + *intfs =NULL ; + *num =0 ; + + buf =GCSPRINTF ("%s:" , usb->busid); + +if (!(dir =opendir (SYSFS_USB_DEV ))) { + rc =ERROR_FAIL ; + goto out; + } + +while ((entry =readdir (dir)) !=NULL ) { +if (!strncmp (entry->d_name, buf,strlen (buf))){ + *intfs =realloc (*intfs,sizeof (char *) * (*num +1 )); +if (*intfs ==NULL ) { + rc =ERROR_FAIL ; + goto out; + } + (*intfs)[*num] =strdup (entry->d_name); + (*num)++;tools/libxl/libxl_pvusb.c line 862
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ } + } + +closedir (dir); + +out: +return rc; +} + +/* Cann't write '.' or ':' into Xenstore as key. So, change '.' to '_', "Can't"
And this behaviour needs to be documented.
tools/libxl/libxl_pvusb.c line 873
Quoted 11 lines
tools/libxl/libxl_pvusb.c new file +11
-0+* change ':' to '-'. +*/ +static char *usb_interface_encode (char *busid) +{ +char *str =strdup (busid); +int i, len =strlen (str); + +for (i =0 ; i < len; i++) { +if (str[i] =='.' ) + str[i] ='_' ; +if (str[i] ==':' )Indentation.
Quoted 94 lines
tools/libxl/libxl_pvusb.c new file +94
-0+ str[i] ='-' ; + } +return str; +} + +/* unbind usb device from usbback driver, if there are many interfaces +* under the usb device, then check each interface, unbind from usbback +* driver and rebind to original driver +*/ +static int usbback_dev_unassign (libxl__gc *gc,libxl_device_usb *usb) +{ +char **intfs =NULL ; +char *path; +int num =0 , i; +int rc =0 ; +char *usb_encode =NULL ; + +if (usb_get_all_interfaces (gc, usb, &intfs, &num) <0 ) +return ERROR_FAIL ; + + usb_encode =usb_interface_encode (usb->busid); + +for (i =0 ; i < num; i++){ +char *intf = intfs[i]; +char *drvpath =NULL ; + +if (usb_intf_is_assigned (gc, intf) >0 ) { +/* unbind interface from usbback driver */ +if (unbind_usb_intf (gc, intf,NULL ) <0 ) { + rc =ERROR_FAIL ; + goto out; + } + } + +/* bind interface to its originial driver */ + drvpath =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF (USBBACK_INFO_PATH "/%s/%s/driver_path" , + usb_encode,usb_interface_encode (intf))); +if (drvpath) { +if (bind_usb_intf (gc, intf, drvpath) <0 ) { +LOGE (ERROR ,"Couldn't bind %s to %s" , intf, drvpath); + rc =ERROR_FAIL ; + goto out; + } + } + } + +/* finally, remove xs driver path */ + path =GCSPRINTF (USBBACK_INFO_PATH "/%s" , usb_encode); +libxl__xs_rm_checked (gc,XBT_NULL , path); + +out: +if (intfs) { +for (i =0 ; i < num; i++) +free (intfs[i]); +free (intfs); + } +free (usb_encode); +return rc; +} + +/* bind usb device to "usbback" driver, if there are many interfaces +* under the usb device, check each interface, unbind from original +* driver and bind to usbback driver. +*/ +static int usbback_dev_assign (libxl__gc *gc,libxl_device_usb *usb) +{ +char **intfs =NULL ; +int num =0 , i; +int rc =0 ; +char *usb_encode =NULL ; + +if (usb_get_all_interfaces (gc, usb, &intfs, &num) <0 ) +return ERROR_FAIL ; + + usb_encode =usb_interface_encode (usb->busid); + +for (i =0 ; i < num; i++){ +char *intf = intfs[i]; +char *path =NULL ; +char *drvpath =NULL ; + +/* already assigned to usbback */ +if (usb_intf_is_assigned (gc, intf) >0 ) +continue ; + +/* unbind interface from original driver */ +if (unbind_usb_intf (gc, intf, &drvpath) <0 ) { + rc =ERROR_FAIL ; + goto out_rebind; + } + +if (drvpath) { +/* write driver path to xenstore for later rebinding */ tools/libxl/libxl_pvusb.c
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ path =GCSPRINTF (USBBACK_INFO_PATH "/%s/%s/driver_path" , + usb_encode,usb_interface_encode (intf)); +if (libxl__xs_write_checked (gc,XBT_NULL , path, drvpath) <0 ) { +LOG (WARN ,"Write of %s to node %s failed" , drvpath, path); + } + } + +/* bind interface to usbback */ +if (bind_usb_intf (gc, intf,SYSFS_USBBACK_DRIVER ) <0 ){ + LOGE(ERROR, "Couldn't bind %s to %s", intf,
SYSFS_USBBACK_DRIVER);Quoted 13 lines
tools/libxl/libxl_pvusb.c new file +13
-0+ rc =ERROR_FAIL ; + goto out_rebind; + } + } + + goto out; + +out_rebind: +/* some interfaces might be bound to usbback, unbind it then and +* rebind to its original driver +*/ +usbback_dev_unassign (gc, usb); +out:tools/libxl/libxl_pvusb.c
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+if (intfs) { +for (i =0 ; i < num; i++) +free (intfs[i]); +free (intfs); + } +free (usb_encode); +return rc; +} + +static int do_usb_add(libxl__gc *gc, uint32_t domid, libxl_device_usb*usb)
Quoted 5 lines
tools/libxl/libxl_pvusb.c new file +5
-0+{ +int rc =0 ; + + rc =libxl__device_usb_add_xenstore (gc, domid, usb); +if (rc) goto out;tools/libxl/libxl_pvusb.c
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ + rc =usbback_dev_assign (gc, usb); +if (rc) +libxl__device_usb_remove_xenstore (gc, domid, usb); + +out: +return rc; +} + +int libxl__device_usb_add(libxl__gc *gc, uint32_t domid, libxl_device_usb*usb)
Quoted 29 lines
tools/libxl/libxl_pvusb.c new file +29
-0+{ +int rc; + + usb->busid =usb_busaddr_to_busid (gc, usb->hostbus, usb->hostaddr); +if (!usb->busid) { +LOG (ERROR ,"USB device doesn't exist in sysfs" ); +return ERROR_INVAL ; + } + +if (!is_usb_assignable (gc, usb)) { +LOG (ERROR ,"USB device is not assignable." ); + rc =ERROR_FAIL ; + goto out; + } + +/* check usb device is already assigned by pvusb */ +if (is_usb_assigned (gc, usb)) { +LOG (ERROR ,"USB device is already attached to a domain." ); + rc =ERROR_FAIL ; + goto out; + } + + rc =libxl__device_usb_setdefault (gc, domid, usb); +if (rc) goto out; + + rc =do_usb_add (gc, domid, usb); + +out: +return rc;tools/libxl/libxl_pvusb.c line 1054
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+} + +int libxl_device_usb_add (libxl_ctx *ctx,uint32_t domid, +libxl_device_usb *usb, +const libxl_asyncop_how *ao_how) +{ +AO_CREATE (ctx, domid, ao_how); +int rc; + + rc =libxl__device_usb_add (gc, domid, usb);Hmm... Your remove function is async and this one is sync. See comment of
usbctrl_add.Quoted 56 lines
tools/libxl/libxl_pvusb.c new file +56
-0+libxl__ao_complete (egc, ao, rc); +return AO_INPROGRESS ; +} + +static int do_usb_remove (libxl__gc *gc,uint32_t domid, +libxl_device_usb *usb) +{ +if (libxl__device_usb_remove_xenstore (gc, domid, usb)) +return -1 ; + +usbback_dev_unassign (gc, usb); + +return 0 ; +} + +static int libxl__device_usb_remove (libxl__gc *gc,uint32_t domid, +libxl_device_usb *usb) +{ +libxl_device_usb *usbs =NULL ; +libxl_device_usb *usb_find =NULL ; +int i, num, rc; + + usb->busid =usb_busaddr_to_busid (gc, usb->hostbus, usb->hostaddr); +if (!usb->busid) { +LOG (ERROR ,"USB device doesn't exist in sysfs" ); +return ERROR_INVAL ; + } + + usbs =libxl_device_usb_list_all (gc, domid, &num); +if (!usbs) { +LOG (ERROR ,"No USB device attached to this domain" ); +return ERROR_FAIL ; + } + +for (i =0 ; i < num; i++) { +if (!strcmp (usb->busid, usbs[i].busid)) { + usb_find = usbs + i; +break ; + } + } + +/* doesn't find the usb device in domain's usb device list*/ +if (!usb_find) { +LOG (ERROR ,"USB device %x.%x is not attached to this domain" , + usb->hostbus, usb->hostaddr); + rc =ERROR_FAIL ; + goto out; + } + + rc =do_usb_remove (gc, domid, usb_find); + +out: +free (usbs); +return rc; +} +tools/libxl/libxl_pvusb.c line 1120
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+int libxl_device_usb_remove (libxl_ctx *ctx,uint32_t domid, +libxl_device_usb *usb, +const libxl_asyncop_how *ao_how) + +{ +AO_CREATE (ctx, domid, ao_how); +int rc; + + rc =libxl__device_usb_remove (gc, domid, usb); +Same here.
Wei.
Re: [PATCH V3 3/6] libxl: add pvusb API
Quoted 14 lines · Chun Yan Liu
Quoted 13 lines
tools/libxl/libxl_pvusb.c line 28
Quoted 9 lines
tools/libxl/libxl_pvusb.c new file +8
-0+static int libxl__device_usbctrl_setdefault (libxl__gc *gc,uint32_t domid, +libxl_device_usbctrl *usbctrl) +{ +int rc; + +if (!usbctrl->version) + usbctrl->version =2 ; +How hard would it be to implement USB 3? I assume this depends on QEMU's support? I.e. we just need to specify the version to 3 and it should just work? Just curious.
Quoted 3 lines
This implementation is coworked with PVUSB driver, so it depends on PVUSB driver's support. If PVUSB driver supports that, just specify the version to 3 and can work.
I sort of got the idea that from the pvusb driver's perspective, the 1/2/3 thing was mostly just a number -- one that's important to the USB device itself and the USB driver for those devices in the guest, but less so for the pvusb layer. Is that not the case?
-George
Re: [PATCH V3 3/6] libxl: add pvusb API
Quoted 15 lines · George Dunlap
Quoted 14 lines · Chun Yan Liu
Quoted 13 lines
tools/libxl/libxl_pvusb.c line 28
Quoted 9 lines
tools/libxl/libxl_pvusb.c new file +8
-0+static int libxl__device_usbctrl_setdefault (libxl__gc *gc,uint32_t domid, +libxl_device_usbctrl *usbctrl) +{ +int rc; + +if (!usbctrl->version) + usbctrl->version =2 ; +How hard would it be to implement USB 3? I assume this depends on QEMU's support? I.e. we just need to specify the version to 3 and it should just work? Just curious.
Quoted 8 lines
Quoted 3 lines
This implementation is coworked with PVUSB driver, so it depends on PVUSB driver's support. If PVUSB driver supports that, just specify the version to 3 and can work.
I sort of got the idea that from the pvusb driver's perspective, the 1/2/3 thing was mostly just a number -- one that's important to the USB device itself and the USB driver for those devices in the guest, but less so for the pvusb layer. Is that not the case?
I think at least for support of USB3.0 streams there is some work to do. I haven't done any thorough research on this topic, but as libusb had to be changed for support of USB3.0 as well, I'd expect this to be the case for the pvusb interface, too.
Juergen
Re: [PATCH V3 3/6] libxl: add pvusb API
In reply to Chun Yan LiuOn Mon, May 18, 2015 at 09:20:43PM -0600, Chun Yan Liu wrote:
[...]
Quoted 36 lines
Quoted 36 lines
Quoted 19 lines
tools/libxl/libxl_pvusb.c new file +18
-0+for (;;) { + rc =libxl__xs_transaction_start (gc, &t); +if (rc) goto out; + + rc =libxl__device_exists (gc, t, device); +if (rc <0 ) goto out; +if (rc ==1 ) { +/* already exists in xenstore */ +LOG (ERROR ,"device already exists in xenstore" ); + rc =ERROR_DEVICE_EXISTS ; + goto out; + } + + rc =libxl__set_domain_configuration (gc, domid, &d_config); +if (rc) goto out; + + libxl__device_generic_add(gc, t, device, + libxl__xs_kvs_of_flexarray(gc, back,tools/libxl/libxl_pvusb.c line 138
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ back->count), +libxl__xs_kvs_of_flexarray (gc, front, + front->count), + NULL); +libxl__usbport_add_xenstore (gc, t, domid, usbctrl); + rc =libxl__xs_transaction_commit (gc, &t); +if (!rc)break ; +if (rc <0 ) goto out; + } +You don't have aodev so you cannot check
update_json. Maybe you need aodev?That field
update_jsonis set to true when doing hotplug. It's set to false during domain creation.
Quoted 9 lines
Quoted 1 line
The same comment applies to other add functions as well.
Thanks, I'm updating that. But maybe like
pci_addandpci_removefunctions, will add a 'starting' flag to indicate hotplug or creation. Looking atDEFINE_DEVICE_ADDandDEFINE_DEVICE_REMOVE,usbctrl_addandusb_addcan useDEFINE_DEVICE_ADD; butusbctrl_removeandusb_removecannot useDEFINE_DEVICE_REMOVEdirectly, need some extra handling. So, finally turned to not use these macros but refer to pci functions.
TBH I prefer to have only one way to deal with devices. I personally prefer the async style that every other devices use. Maybe that's just because I mostly worked with those.
I don't know the full history of libxl_pci.c so I will wait for Ian and Ian's comments on which way to go.
I think one merit of determining whether to use sync or async is that whether the operation is long running (slow). Long running one should be asyn. I guess usb passthrough is not slow so we are probably fine in this regard.
BTW if you find the macros can't meet your need you can either extend them or not use them.
Quoted 44 lines
Quoted 35 lines
Quoted 35 lines
tools/libxl/libxl_pvusb.c new file +34
-0+out: +if (lock)libxl__unlock_domain_userdata (lock); +libxl_device_usbctrl_dispose (&usbctrl_saved); +libxl_domain_config_dispose (&d_config); +return rc; +} + +int libxl__device_usbctrl_add (libxl__gc *gc,uint32_t domid, +libxl_device_usbctrl *usbctrl) +{ +int rc =0 ; + + rc =libxl__device_usbctrl_setdefault (gc, domid, usbctrl); +if (rc <0 ) goto out; + +if (usbctrl->devid ==-1 ) { + usbctrl->devid =libxl__device_nextid (gc, domid,"vusb" ); +if (usbctrl->devid <0 ) { + rc =ERROR_FAIL ; + goto out; + } + } + +if (libxl__usbctrl_add_xenstore (gc, domid, usbctrl) <0 ){ + rc =ERROR_FAIL ; + goto out; + } + +out: +return rc; +} + +int libxl_device_usbctrl_add(libxl_ctx *ctx, uint32_t domid, + libxl_device_usbctrl *usbctrl,Quoted 8 lines
tools/libxl/libxl_pvusb.c line 178
Quoted 6 lines
tools/libxl/libxl_pvusb.c new file +6
-0+ const libxl_asyncop_how *ao_how) +{ +AO_CREATE (ctx, domid, ao_how); +int rc; + + rc =libxl__device_usbctrl_add (gc, domid, usbctrl);Hmm... Your remove function is async while this one is sync, why?
Quoted 3 lines
Hmm, maybe not proper here, just referred to
pci_addimplementation. Current calling places only use sync mode.
Yeah, I only ask for consistency.
Wei.
Re: [PATCH V3 3/6] libxl: add pvusb API
Quoted 48 lines · Wei Liu
On Mon, May 18, 2015 at 09:20:43PM -0600, Chun Yan Liu wrote:
[...]Quoted 36 lines
Quoted 36 lines
Quoted 19 lines
tools/libxl/libxl_pvusb.c new file +18
-0+for (;;) { + rc =libxl__xs_transaction_start (gc, &t); +if (rc) goto out; + + rc =libxl__device_exists (gc, t, device); +if (rc <0 ) goto out; +if (rc ==1 ) { +/* already exists in xenstore */ +LOG (ERROR ,"device already exists in xenstore" ); + rc =ERROR_DEVICE_EXISTS ; + goto out; + } + + rc =libxl__set_domain_configuration (gc, domid, &d_config); +if (rc) goto out; + + libxl__device_generic_add(gc, t, device, + libxl__xs_kvs_of_flexarray(gc, back,tools/libxl/libxl_pvusb.c line 138
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ back->count), +libxl__xs_kvs_of_flexarray (gc, front, + front->count), + NULL); +libxl__usbport_add_xenstore (gc, t, domid, usbctrl); + rc =libxl__xs_transaction_commit (gc, &t); +if (!rc)break ; +if (rc <0 ) goto out; + } +You don't have aodev so you cannot check
update_json. Maybe you need aodev?That field
update_jsonis set to true when doing hotplug. It's set to false during domain creation.Quoted 9 lines
Quoted 1 line
The same comment applies to other add functions as well.
Thanks, I'm updating that. But maybe like
pci_addandpci_removefunctions, will add a 'starting' flag to indicate hotplug or creation. Looking atDEFINE_DEVICE_ADDandDEFINE_DEVICE_REMOVE,usbctrl_addandusb_addcan useDEFINE_DEVICE_ADD; butusbctrl_removeandusb_removecannot useDEFINE_DEVICE_REMOVEdirectly, need some extra handling. So, finally turned to not use these macros but refer to pci functions.
Quoted 6 lines
TBH I prefer to have only one way to deal with devices. I personally prefer the async style that every other devices use. Maybe that's just because I mostly worked with those.
I don't know the full history of
libxl_pci.cso I will wait for Ian and Ian's comments on which way to go.
libxl_pci.c should not be used as an example of the right way to do things (to say the least).
Quoted 4 lines
I think one merit of determining whether to use sync or async is that whether the operation is long running (slow). Long running one should be asyn. I guess usb passthrough is not slow so we are probably fine in this regard.
Device add/remove is expected to be async at least at the public API level , all the others appear to be and it seems logical to me that they would be.
Re: [PATCH V3 3/6] libxl: add pvusb API
In reply to Wei LiuQuoted 3 lines
Quoted 3 lines
Quoted 3 lines
On 5/20/2015 at 05:04 PM, in message
<[email protected]>, Wei Liu <[email protected]>
Quoted 57 lines · wrote:
On Mon, May 18, 2015 at 09:20:43PM -0600, Chun Yan Liu wrote:
[...]Quoted 21 lines
Quoted 20 lines
Quoted 20 lines
tools/libxl/libxl_pvusb.c new file +19
-0+for (;;) { + rc =libxl__xs_transaction_start (gc, &t); +if (rc) goto out; + + rc =libxl__device_exists (gc, t, device); +if (rc <0 ) goto out; +if (rc ==1 ) { +/* already exists in xenstore */ +LOG (ERROR ,"device already exists in xenstore" ); + rc =ERROR_DEVICE_EXISTS ; + goto out; + } + + rc =libxl__set_domain_configuration (gc, domid, &d_config); +if (rc) goto out; + + libxl__device_generic_add(gc, t, device, + libxl__xs_kvs_of_flexarray(gc, back, + back->count),Quoted 2 lines
Quoted 2 lines
tools/libxl/libxl_pvusb.c line 138
Quoted 2 lines
tools/libxl/libxl_pvusb.c new file +2
-0+ libxl__xs_kvs_of_flexarray(gc, front, +
front->count),Quoted 14 lines
Quoted 14 lines
tools/libxl/libxl_pvusb.c line 138
Quoted 7 lines
tools/libxl/libxl_pvusb.c new file +7
-0+ NULL); +libxl__usbport_add_xenstore (gc, t, domid, usbctrl); + rc =libxl__xs_transaction_commit (gc, &t); +if (!rc)break ; +if (rc <0 ) goto out; + } +You don't have aodev so you cannot check
update_json. Maybe you need aodev?That field
update_jsonis set to true when doing hotplug. It's set to false during domain creation.Quoted 9 lines
Quoted 1 line
The same comment applies to other add functions as well.
Thanks, I'm updating that. But maybe like
pci_addandpci_removefunctions, will add a 'starting' flag to indicate hotplug or creation. Looking atDEFINE_DEVICE_ADDandDEFINE_DEVICE_REMOVE,usbctrl_addandusb_addcan useDEFINE_DEVICE_ADD; butusbctrl_removeandusb_removecannot useDEFINE_DEVICE_REMOVEdirectly, need some extra handling. So, finally turned to not use these macros but refer to pci functions.TBH I prefer to have only one way to deal with devices. I personally prefer the async style that every other devices use. Maybe that's just because I mostly worked with those.
I don't know the full history of
libxl_pci.cso I will wait for Ian and Ian's comments on which way to go.
Quoted 7 lines
I think one merit of determining whether to use sync or async is that whether the operation is long running (slow). Long running one should be asyn. I guess usb passthrough is not slow so we are probably fine in this regard.
BTW if you find the macros can't meet your need you can either extend them or not use them.
Got you and Ian. I'll update codes then.
Chunyan
Quoted 54 lines
Quoted 45 lines
Quoted 36 lines
Quoted 36 lines
tools/libxl/libxl_pvusb.c new file +34
-0+out: +if (lock)libxl__unlock_domain_userdata (lock); +libxl_device_usbctrl_dispose (&usbctrl_saved); +libxl_domain_config_dispose (&d_config); +return rc; +} + +int libxl__device_usbctrl_add (libxl__gc *gc,uint32_t domid, +libxl_device_usbctrl *usbctrl) +{ +int rc =0 ; + + rc =libxl__device_usbctrl_setdefault (gc, domid, usbctrl); +if (rc <0 ) goto out; + +if (usbctrl->devid ==-1 ) { + usbctrl->devid =libxl__device_nextid (gc, domid,"vusb" ); +if (usbctrl->devid <0 ) { + rc =ERROR_FAIL ; + goto out; + } + } + +if (libxl__usbctrl_add_xenstore (gc, domid, usbctrl) <0 ){ + rc =ERROR_FAIL ; + goto out; + } + +out: +return rc; +} + +int libxl_device_usbctrl_add(libxl_ctx *ctx, uint32_t domid, + libxl_device_usbctrl *usbctrl,Quoted 8 lines
tools/libxl/libxl_pvusb.c line 178
Quoted 6 lines
tools/libxl/libxl_pvusb.c new file +6
-0+ const libxl_asyncop_how *ao_how) +{ +AO_CREATE (ctx, domid, ao_how); +int rc; + + rc =libxl__device_usbctrl_add (gc, domid, usbctrl);Hmm... Your remove function is async while this one is sync, why?
Quoted 3 lines
Hmm, maybe not proper here, just referred to
pci_addimplementation. Current calling places only use sync mode.Yeah, I only ask for consistency.
Wei.
Re: [PATCH V3 3/6] libxl: add pvusb API
In reply to Wei LiuWei Liu writes ("Re: [PATCH V3 3/6] libxl: add pvusb API"):
Quoted 8 lines · Chun Yan Liu
Quoted 6 lines
Thanks, I'm updating that. But maybe like
pci_addandpci_removefunctions, will add a 'starting' flag to indicate hotplug or creation. Looking atDEFINE_DEVICE_ADDandDEFINE_DEVICE_REMOVE,usbctrl_addandusb_addcan useDEFINE_DEVICE_ADD; butusbctrl_removeandusb_removecannot useDEFINE_DEVICE_REMOVEdirectly, need some extra handling. So, finally turned to not use these macros but refer to pci functions.
Quoted 6 lines
TBH I prefer to have only one way to deal with devices. I personally prefer the async style that every other devices use. Maybe that's just because I mostly worked with those.
I don't know the full history of
libxl_pci.cso I will wait for Ian and Ian's comments on which way to go.
libxl_pci.c is rather unfortunate in a few respects. It does a number of things things synchronously that it shouldn't.
Certainly the external API for all device add/remove functions should be async.
Ian.
Re: [PATCH V3 3/6] libxl: add pvusb API
In reply to Chunyan LiuQuoted 8 lines · Chunyan Liu
Add pvusb APIs, including:
- attach/detach (create/destroy) virtual usb controller.
- attach/detach usb device
- list usb controller and usb devices
- some other helper functionsSigned-off-by Chunyan Liu <[email protected]>Signed-off-by Simon Cao <[email protected]>
OK, getting closer. :-)
A number of comments:
tools/libxl/libxl.h line 120
Quoted 12 lines
tools/libxl/libxl.h +4
-0@@ -114,6 +114,12 @@#define LIBXL_HAVE_DOMAIN_NODEAFFINITY 1/* +* LIBXL_HAVE_PVUSB indicates the functions for doing hot-plug of +* USB devices through pvusb. +*/ +#define LIBXL_HAVE_PVUSB 1
It seems like we should document somewhere how we expect these to be used -- namely the connection between usbctrl and usb devices. In particular, that you can either add a usbctrl, and then add a usb device to it specifically; or if you don't specify a usbctrl when calling usb_add, it will find the most reasonable one to add it to, even creating one for you if you didn't have one.
Quoted 5 lines
tools/libxl/libxl_pvusb.c new file +0
-0
tools/libxl/libxl_pvusb.c line 154
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +9
-0+int libxl__device_usbctrl_add (libxl__gc *gc,uint32_t domid, +libxl_device_usbctrl *usbctrl) +{ +int rc =0 ; + + rc =libxl__device_usbctrl_setdefault (gc, domid, usbctrl); +if (rc <0 ) goto out; + +if (usbctrl->devid ==-1 ) {
Hmm, I was doing to say that this shouldn't be a magic constant, but it looks like that's what all the other devices do :-/
tools/libxl/libxl_pvusb.c line 446
Quoted 7 lines
tools/libxl/libxl_pvusb.c new file +7
-0+static bool is_usb_in_array (libxl_device_usb *usbs,int num, +libxl_device_usb *usb) +{ +int i; + +for (i =0 ; i < num; i++) { +if (!strcmp (usbs[i].busid, usb->busid) )
Here (and elsewhere) you should probably use the COMPARE_USB() macro you define in this patch.
Quoted 9 lines
tools/libxl/libxl_pvusb.c new file +9
-0+/* check if USB device type is assignable */ +static bool is_usb_assignable (libxl__gc *gc,libxl_device_usb *usb) +{ +libxl_ctx *ctx =libxl__gc_owner (gc); +int classcode; +char *filename; +void *buf; + +assert (usb->busid);
tools/libxl/libxl_pvusb.c line 489
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ + filename =GCSPRINTF (SYSFS_USB_DEV "/%s/bDeviceClass" , usb->busid); +if (libxl_read_file_contents (ctx, filename, &buf,NULL ) <0 ) +return false; + +sscanf (buf,"%x" , &classcode); +if (classcode ==USBHUB_CLASS_CODE ) +return false; + +return true;
Just checking, is it really the case that *all* USB classes are assignable, *except* for hubs?
This is a minor thing, but this might be simper to do this:
return classcode != USBHUB_CLASS_CODE;
Quoted 21 lines
tools/libxl/libxl_pvusb.c new file +21
-0+/* get usb devices under certain usb controller */ +static int libxl__device_usb_list (libxl__gc *gc,uint32_t domid,int usbctrl, +libxl_device_usb **usbs,int *num) +{ +char *be_path, *num_devs; +int n, i; + + *usbs =NULL ; + *num =0 ; + + be_path =GCSPRINTF ("%s/backend/vusb/%d/%d" , +libxl__xs_get_dompath (gc,0 ), domid, usbctrl); + num_devs =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/num-ports" , be_path)); +if (!num_devs) +return 0 ; + + n =atoi (num_devs); + *usbs =calloc (n,sizeof (libxl_device_usb)); + +for (i =0 ; i < n; i++) {
tools/libxl/libxl_pvusb.c line 522
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+char *busid; +libxl_device_usb *usb =NULL ; + + busid =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/port/%d" , be_path, i +1 )); +if (busid &&strcmp (busid,"" )) { + usb = *usbs + *num; + usb->ctrl = usbctrl; + usb->port = i +1 ; + usb->busid =strdup (busid);
This needs to populate the hostbus / hostaddr as well; busid is pretty useless to users / external callers.
Quoted 17 lines
tools/libxl/libxl_pvusb.c new file +17
-0+/* get all usb devices of the domain */ +static libxl_device_usb * +libxl_device_usb_list_all (libxl__gc *gc,uint32_t domid,int *num) +{ +char **usbctrls; +unsigned int nd, i, j; +char *be_path; +int rc; +libxl_device_usb *usbs =NULL ; + + *num =0 ; + + be_path =GCSPRINTF ("/local/domain/%d/backend/vusb/%d" , +LIBXL_TOOLSTACK_DOMID , domid); + usbctrls =libxl__xs_directory (gc,XBT_NULL , be_path, &nd); + +for (i =0 ; i < nd; i++) {
tools/libxl/libxl_pvusb.c line 556
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+int nc =0 ; +libxl_device_usb *tmp =NULL ; + rc =libxl__device_usb_list (gc, domid,atoi (usbctrls[i]), &tmp, &nc); +if (!nc)continue ; + + usbs =realloc (usbs,sizeof (libxl_device_usb)*((*num) + nc)); +for (j =0 ; j < nc; j++) { + usbs[*num].ctrl = tmp[j].ctrl; + usbs[*num].port = tmp[j].port; + usbs[*num].busid =strdup (tmp[j].busid);
This needs to copy the hostaddr and busaddr as well, as these are primarily what an external caller will want.
Quoted 14 lines
tools/libxl/libxl_pvusb.c new file +14
-0+/* find first unused controller:port and give that to usb device */ +static int +libxl__device_usb_set_default_usbctrl (libxl__gc *gc,uint32_t domid, +libxl_device_usb *usb) +{ +libxl_ctx *ctx =CTX ; +libxl_device_usbctrl *usbctrls; +libxl_device_usb *usbs =NULL ; +int numctrl, numusb, i, j, rc =-1 ; +char *be_path, *tmp; + + usbctrls =libxl_device_usbctrl_list (ctx, domid, &numctrl); +if ( !numctrl) + goto out;
tools/libxl/libxl_pvusb.c line 640
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ +for (i =0 ; i < numctrl; i++) { + rc =libxl__device_usb_list (gc, domid, usbctrls[i].devid, + &usbs, &numusb); +if (rc)continue ; + +if (!usbctrls[i].ports || numusb == usbctrls[i].ports) +continue ; + +for (j =1 ; i <= numusb; j++) {
Port numbers start at 1, do they? Interesting...
Er, but isn't the middle thing just plain wrong? For one, you want to be comparing j not i. I can't see that i is updated inside the loop, so ATM this will loop forever.
For two, you want to compare to usbctrls[i].ports (the total number of ports), not to numusb (the number of currently assigned devices).
tools/libxl/libxl_pvusb.c line 668
Quoted 6 lines
tools/libxl/libxl_pvusb.c new file +6
-0+static int libxl__device_usb_setdefault (libxl__gc *gc,uint32_t domid, +libxl_device_usb *usb) +{ +char *be_path, *tmp; + +if (usb->ctrl ==-1 ) {
Oh, right -- libxl_devid's default to -1, don't they? I'll save the grumbling about the lack of a #define here then. (Or rather, I'll grumble at the library rather than you.)
tools/libxl/libxl_pvusb.c line 674
Quoted 6 lines
tools/libxl/libxl_pvusb.c new file +6
-0+int ret =libxl__device_usb_set_default_usbctrl (gc, domid, usb); +/* If no existing ctrl to host this usb device, setup a new one */ +if (ret) { +libxl_device_usbctrl usbctrl; +libxl_device_usbctrl_init (&usbctrl); +libxl__device_usbctrl_add (gc, domid, &usbctrl);
I think in the previous round I asked what would happen if this failed, and you said you would fail later, but that you'd change it to check for an error here and bail out earlier.
tools/libxl/libxl_pvusb.c line 865
Quoted 4 lines
tools/libxl/libxl_pvusb.c new file +4
-0+/* Cann't write '.' or ':' into Xenstore as key. So, change '.' to '_', +* change ':' to '-'. +*/ +static char *usb_interface_encode (char *busid)
Maybe usb_interface_xenstore_encode, to make it clear that you're just encoding it to store in xenstore?
Quoted 24 lines
tools/libxl/libxl_pvusb.c new file +24
-0+static int usbback_dev_unassign (libxl__gc *gc,libxl_device_usb *usb) +{ +char **intfs =NULL ; +char *path; +int num =0 , i; +int rc =0 ; +char *usb_encode =NULL ; + +if (usb_get_all_interfaces (gc, usb, &intfs, &num) <0 ) +return ERROR_FAIL ; + + usb_encode =usb_interface_encode (usb->busid); + +for (i =0 ; i < num; i++){ +char *intf = intfs[i]; +char *drvpath =NULL ; + +if (usb_intf_is_assigned (gc, intf) >0 ) { +/* unbind interface from usbback driver */ +if (unbind_usb_intf (gc, intf,NULL ) <0 ) { + rc =ERROR_FAIL ; + goto out; + } + }
tools/libxl/libxl_pvusb.c line 916
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ +/* bind interface to its originial driver */ + drvpath =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF (USBBACK_INFO_PATH "/%s/%s/driver_path" , + usb_encode,usb_interface_encode (intf))); +if (drvpath) { +if (bind_usb_intf (gc, intf, drvpath) <0 ) { +LOGE (ERROR ,"Couldn't bind %s to %s" , intf, drvpath); + rc =ERROR_FAIL ; + goto out;
I think this function probably shouldn't fail if it can't re-bind to the original driver. If nothing else, this will be bad because now the USB device has at least one of its interfaces unbound from usbback, but the other ones still bound to it. All interfaces should be unbound from usbback regardless.
I also think that while a warning should be logged, that the function as a whole should return success if it managed to unbind, even if it didn't manage to rebind. (But feel free to argue otherwise.)
Quoted 10 lines
tools/libxl/libxl_types.idl +2
-0@@ -533,6 +533,22 @@ libxl_device_pci = Struct("device_pci", [ ("seize", bool), ]) +libxl_device_usbctrl = Struct("device_usbctrl", [ + ("devid", libxl_devid),
tools/libxl/libxl_types.idl libxl_device_pci = Struct("device_pci", [ · line 547
Quoted 10 lines
tools/libxl/libxl_types.idl +10
-0+ ("version", integer), + ("ports", integer), + ("backend_domid", libxl_domid), + ("backend_domname", string), + ]) + +libxl_device_usb = Struct("device_usb", [ + ("ctrl", libxl_devid), + ("port", integer), + ("busid", string),
So it looks like "busid" is purely an internal string that you're putting in the device struct for convenience, so that you don't have to either keep translating bus:addr into the sysfs node, or pass around the second string as well. Is that right?
I think libxl does something similar to this with "backend_domname" and "backend_domid"; but in that case I believe you *can* specify the backend_domid if you want to. In this case, you're exposing a struct element which is clobbered immediately, at least by usb_add and usb_remove.
Maintainers, what do you think?
Other than that, I think this patch as-is looks in pretty good shape (haven't taken a close look at the rest in the series yet); the primary things are making the "hostbus:hostaddr" actually the primary interface; at a minimum filling in that information when doing queries. I'd ideally getting rid of "busid" from the external interface altogether.
-George
Re: [PATCH V3 3/6] libxl: add pvusb API
Quoted 7 lines · George Dunlap
Quoted 7 lines
tools/libxl/libxl.h +0
-0@@ -114,6 +114,12 @@#define LIBXL_HAVE_DOMAIN_NODEAFFINITY 1
Quoted 12 lines
tools/libxl/libxl.h line 120
Quoted 5 lines
tools/libxl/libxl.h +4
-0/* +* LIBXL_HAVE_PVUSB indicates the functions for doing hot-plug of +* USB devices through pvusb. +*/ +#define LIBXL_HAVE_PVUSB 1It seems like we should document somewhere how we expect these to be used -- namely the connection between usbctrl and usb devices. In particular, that you can either add a usbctrl, and then add a usb device to it specifically; or if you don't specify a usbctrl when calling
usb_add, it will find the most reasonable one to add it to, even creating one for you if you didn't have one.
(I've not checked if this exists in the series yet, but...)
I'd like to see some docs added to libxl.h as an addendum to the existing:
* Devices
* =======
*
* Each device is represented by a libxl_device_<TYPE> data structure
...
stuff which describes the general form of interfaces used for those classes of device which distinguish the controller from the devices themselves in the libxl API.
The new docs should then be applicable to e..g the pvscsi stuff as well.
The goal is to avoid having numerous subtly different style of device interaction in the libxl API by grouping them into the least number of logical groups (thus far 2: ones with explicit controllers and those without) each with a consistent API.
Ian.
答复: Re: [PATCH V3 3/6] libxl: add pvusb API
In reply to George DunlapQuoted 3 lines
Quoted 3 lines
Quoted 3 lines
在 2:06 的 2015/5/20 上,在讯息
<CAFLBxZZHFFWo6Tm7602y3+x5kX65-w4obFS1VdVb8KqnzdAmDg@mail.gmail.com> 中,George Dunlap <[email protected]> 写入:
Quoted 7 lines · Chunyan Liu
Quoted 6 lines
Add pvusb APIs, including:
- attach/detach (create/destroy) virtual usb controller.
- attach/detach usb device
- list usb controller and usb devices
- some other helper functions
Quoted 6 lines
Quoted 2 lines
Signed-off-by Chunyan Liu <[email protected]>Signed-off-by Simon Cao <[email protected]>OK, getting closer. :-)
A number of comments:
Hi, George, thanks very much!
I'm so sorry for missing the message and not reply immediately. Before sending new version, I'm answering some of your questions here. And there are a couple of comments, I still have some hesitation to follow. All others, I agree and will update as you suggested.
Quoted 66 lines
tools/libxl/libxl.h line 120
Quoted 13 lines
tools/libxl/libxl.h +4
-0@@ -114,6 +114,12 @@#define LIBXL_HAVE_DOMAIN_NODEAFFINITY 1/* +* LIBXL_HAVE_PVUSB indicates the functions for doing hot-plug of +* USB devices through pvusb. +*/ +#define LIBXL_HAVE_PVUSB 1It seems like we should document somewhere how we expect these to be used -- namely the connection between usbctrl and usb devices. In particular, that you can either add a usbctrl, and then add a usb device to it specifically; or if you don't specify a usbctrl when calling
usb_add, it will find the most reasonable one to add it to, even creating one for you if you didn't have one.Quoted 5 lines
tools/libxl/libxl_pvusb.c new file +0
-0tools/libxl/libxl_pvusb.c line 154
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +9
-0+int libxl__device_usbctrl_add (libxl__gc *gc,uint32_t domid, +libxl_device_usbctrl *usbctrl) +{ +int rc =0 ; + + rc =libxl__device_usbctrl_setdefault (gc, domid, usbctrl); +if (rc <0 ) goto out; + +if (usbctrl->devid ==-1 ) {Hmm, I was doing to say that this shouldn't be a magic constant, but it looks like that's what all the other devices do :-/
tools/libxl/libxl_pvusb.c line 446
Quoted 7 lines
tools/libxl/libxl_pvusb.c new file +7
-0+static bool is_usb_in_array (libxl_device_usb *usbs,int num, +libxl_device_usb *usb) +{ +int i; + +for (i =0 ; i < num; i++) { +if (!strcmp (usbs[i].busid, usb->busid) )Here (and elsewhere) you should probably use the
COMPARE_USB()macro you define in this patch.Quoted 14 lines
tools/libxl/libxl_pvusb.c new file +14
-0+/* check if USB device type is assignable */ +static bool is_usb_assignable (libxl__gc *gc,libxl_device_usb *usb) +{ +libxl_ctx *ctx =libxl__gc_owner (gc); +int classcode; +char *filename; +void *buf; + +assert (usb->busid); + + filename =GCSPRINTF (SYSFS_USB_DEV "/%s/bDeviceClass" , usb->busid); +if (libxl_read_file_contents (ctx, filename, &buf,NULL ) <0 ) +return false; +
Quoted 8 lines
tools/libxl/libxl_pvusb.c line 489
Quoted 5 lines
tools/libxl/libxl_pvusb.c new file +5
-0+sscanf (buf,"%x" , &classcode); +if (classcode ==USBHUB_CLASS_CODE ) +return false; + +return true;Just checking, is it really the case that *all* USB classes are assignable, *except* for hubs?
Referring to xm pvusb implementation, only HUB is excluded, so I just keep the same.
Quoted 32 lines
This is a minor thing, but this might be simper to do this:
return classcode !=
USBHUB_CLASS_CODE;tools/libxl/libxl_pvusb.c
Quoted 2 lines
tools/libxl/libxl_pvusb.c new file +2
-0+/* get usb devices under certain usb controller */ +static int libxl__device_usb_list(libxl__gc *gc, uint32_t domid, intusbctrl,
Quoted 24 lines
tools/libxl/libxl_pvusb.c new file +24
-0+ libxl_device_usb **usbs, int *num) +{ +char *be_path, *num_devs; +int n, i; + + *usbs =NULL ; + *num =0 ; + + be_path =GCSPRINTF ("%s/backend/vusb/%d/%d" , +libxl__xs_get_dompath (gc,0 ), domid, usbctrl); + num_devs =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/num-ports" , be_path)); +if (!num_devs) +return 0 ; + + n =atoi (num_devs); + *usbs =calloc (n,sizeof (libxl_device_usb)); + +for (i =0 ; i < n; i++) { +char *busid; +libxl_device_usb *usb =NULL ; + + busid =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF ("%s/port/%d" , be_path, i +1 ));
Quoted 8 lines
tools/libxl/libxl_pvusb.c line 522
Quoted 5 lines
tools/libxl/libxl_pvusb.c new file +5
-0+if (busid &&strcmp (busid,"" )) { + usb = *usbs + *num; + usb->ctrl = usbctrl; + usb->port = i +1 ; + usb->busid =strdup (busid);This needs to populate the hostbus / hostaddr as well; busid is pretty useless to users / external callers.
I thought about that when implementing, but finally not added to codes considering:
* for all libxl pvusb internal usage, busid is enough.
* for toolstack usage, if we want to expose users useful information about bus:addr, vendorID:devieID, we have libxl_device_usb_getinfo API, with this API callers can get all information including hostbus, hostaddr.
If that couldn't satisfy qemu usage, I can add a translating to fill in hostbus and hostaddr too.
Quoted 30 lines
Quoted 11 lines
tools/libxl/libxl_pvusb.c new file +10
-0+/* get all usb devices of the domain */ +static libxl_device_usb * +libxl_device_usb_list_all (libxl__gc *gc,uint32_t domid,int *num) +{ +char **usbctrls; +unsigned int nd, i, j; +char *be_path; +int rc; +libxl_device_usb *usbs =NULL ; +tools/libxl/libxl_pvusb.c
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ *num =0 ; + + be_path =GCSPRINTF ("/local/domain/%d/backend/vusb/%d" , +LIBXL_TOOLSTACK_DOMID , domid); + usbctrls =libxl__xs_directory (gc,XBT_NULL , be_path, &nd); + +for (i =0 ; i < nd; i++) { +int nc =0 ; +libxl_device_usb *tmp =NULL ; + rc = libxl__device_usb_list(gc, domid, atoi(usbctrls[i]), &tmp,&nc);
tools/libxl/libxl_pvusb.c line 556
Quoted 7 lines
tools/libxl/libxl_pvusb.c new file +7
-0+if (!nc)continue ; + + usbs =realloc (usbs,sizeof (libxl_device_usb)*((*num) + nc)); +for (j =0 ; j < nc; j++) { + usbs[*num].ctrl = tmp[j].ctrl; + usbs[*num].port = tmp[j].port; + usbs[*num].busid =strdup (tmp[j].busid);
Quoted 2 lines
This needs to copy the hostaddr and busaddr as well, as these are primarily what an external caller will want.
Same to above.
Quoted 19 lines
Quoted 19 lines
tools/libxl/libxl_pvusb.c new file +18
-0+/* find first unused controller:port and give that to usb device */ +static int +libxl__device_usb_set_default_usbctrl (libxl__gc *gc,uint32_t domid, +libxl_device_usb *usb) +{ +libxl_ctx *ctx =CTX ; +libxl_device_usbctrl *usbctrls; +libxl_device_usb *usbs =NULL ; +int numctrl, numusb, i, j, rc =-1 ; +char *be_path, *tmp; + + usbctrls =libxl_device_usbctrl_list (ctx, domid, &numctrl); +if ( !numctrl) + goto out; + +for (i =0 ; i < numctrl; i++) { + rc =libxl__device_usb_list (gc, domid, usbctrls[i].devid, + &usbs, &numusb);
Quoted 8 lines
tools/libxl/libxl_pvusb.c line 640
Quoted 6 lines
tools/libxl/libxl_pvusb.c new file +6
-0+if (rc)continue ; + +if (!usbctrls[i].ports || numusb == usbctrls[i].ports) +continue ; + +for (j =1 ; i <= numusb; j++) {Port numbers start at 1, do they? Interesting...
Keep same as xm implementation. I think that should be proved right.
Quoted 100 lines
Er, but isn't the middle thing just plain wrong? For one, you want to be comparing j not i. I can't see that i is updated inside the loop, so ATM this will loop forever.
For two, you want to compare to usbctrls[i].ports (the total number of ports), not to numusb (the number of currently assigned devices).
tools/libxl/libxl_pvusb.c line 668
Quoted 6 lines
tools/libxl/libxl_pvusb.c new file +6
-0+static int libxl__device_usb_setdefault (libxl__gc *gc,uint32_t domid, +libxl_device_usb *usb) +{ +char *be_path, *tmp; + +if (usb->ctrl ==-1 ) {Oh, right -- libxl_devid's default to -1, don't they? I'll save the grumbling about the lack of a #define here then. (Or rather, I'll grumble at the library rather than you.)
tools/libxl/libxl_pvusb.c line 674
Quoted 6 lines
tools/libxl/libxl_pvusb.c new file +6
-0+int ret =libxl__device_usb_set_default_usbctrl (gc, domid, usb); +/* If no existing ctrl to host this usb device, setup a new one */ +if (ret) { +libxl_device_usbctrl usbctrl; +libxl_device_usbctrl_init (&usbctrl); +libxl__device_usbctrl_add (gc, domid, &usbctrl);I think in the previous round I asked what would happen if this failed, and you said you would fail later, but that you'd change it to check for an error here and bail out earlier.
tools/libxl/libxl_pvusb.c line 865
Quoted 4 lines
tools/libxl/libxl_pvusb.c new file +4
-0+/* Cann't write '.' or ':' into Xenstore as key. So, change '.' to '_', +* change ':' to '-'. +*/ +static char *usb_interface_encode (char *busid)Maybe
usb_interface_xenstore_encode, to make it clear that you're just encoding it to store in xenstore?Quoted 24 lines
tools/libxl/libxl_pvusb.c new file +24
-0+static int usbback_dev_unassign (libxl__gc *gc,libxl_device_usb *usb) +{ +char **intfs =NULL ; +char *path; +int num =0 , i; +int rc =0 ; +char *usb_encode =NULL ; + +if (usb_get_all_interfaces (gc, usb, &intfs, &num) <0 ) +return ERROR_FAIL ; + + usb_encode =usb_interface_encode (usb->busid); + +for (i =0 ; i < num; i++){ +char *intf = intfs[i]; +char *drvpath =NULL ; + +if (usb_intf_is_assigned (gc, intf) >0 ) { +/* unbind interface from usbback driver */ +if (unbind_usb_intf (gc, intf,NULL ) <0 ) { + rc =ERROR_FAIL ; + goto out; + } + }tools/libxl/libxl_pvusb.c line 916
Quoted 10 lines
tools/libxl/libxl_pvusb.c new file +10
-0+ +/* bind interface to its originial driver */ + drvpath =libxl__xs_read (gc,XBT_NULL , +GCSPRINTF (USBBACK_INFO_PATH "/%s/%s/driver_path" , + usb_encode,usb_interface_encode (intf))); +if (drvpath) { +if (bind_usb_intf (gc, intf, drvpath) <0 ) { +LOGE (ERROR ,"Couldn't bind %s to %s" , intf, drvpath); + rc =ERROR_FAIL ; + goto out;I think this function probably shouldn't fail if it can't re-bind to the original driver. If nothing else, this will be bad because now the USB device has at least one of its interfaces unbound from usbback, but the other ones still bound to it. All interfaces should be unbound from usbback regardless.
I also think that while a warning should be logged, that the function as a whole should return success if it managed to unbind, even if it didn't manage to rebind. (But feel free to argue otherwise.)
Quoted 17 lines
tools/libxl/libxl_types.idl +9
-0@@ -533,6 +533,22 @@ libxl_device_pci = Struct("device_pci", [ ("seize", bool), ]) +libxl_device_usbctrl = Struct("device_usbctrl", [ + ("devid", libxl_devid), + ("version", integer), + ("ports", integer), + ("backend_domid", libxl_domid), + ("backend_domname", string), + ]) + +libxl_device_usb = Struct("device_usb", [
Quoted 8 lines
tools/libxl/libxl_types.idl libxl_device_pci = Struct("device_pci", [ · line 547
Quoted 3 lines
tools/libxl/libxl_types.idl +3
-0+ ("ctrl", libxl_devid), + ("port", integer), + ("busid", string),So it looks like "busid" is purely an internal string that you're putting in the device struct for convenience, so that you don't have to either keep translating bus:addr into the sysfs node, or pass around the second string as well. Is that right?
Yes, right. Actually in libxl pvusb, internally it uses busid, hostbus:hostaddr is only a toolstack interface.
It is 'busid' saved in xenstore for pvusb driver's usage, and everywhere comparing work (e.g. check usb device existence before removing a usb device) it uses 'busid' to compare. So having a struct element is very convenient, otherwise, there are many places that need to translate from busid to/from bus:addr.
Personally I still prefer to keep 'busid', but if that's really unacceptable, I can also update.
- Chunyan
Quoted 16 lines
I think libxl does something similar to this with "
backend_domname" and "backend_domid"; but in that case I believe you *can* specify thebackend_domidif you want to. In this case, you're exposing a struct element which is clobbered immediately, at least byusb_addandusb_remove.Maintainers, what do you think?
Other than that, I think this patch as-is looks in pretty good shape (haven't taken a close look at the rest in the series yet); the primary things are making the "hostbus:hostaddr" actually the primary interface; at a minimum filling in that information when doing queries. I'd ideally getting rid of "busid" from the external interface altogether.
Quoted 7 lines
-George
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH V3 3/6] libxl: add pvusb API
In reply to Chunyan Liutools/libxl/libxl_pvusb.c line 1153
Quoted 2 lines · Chunyan Liu
tools/libxl/libxl_pvusb.c new file +2
-0+int libxl_device_usb_getinfo (libxl_ctx *ctx,char *busid,libxl_usbinfo *usbinfo) +{
Sorry, missed something. This function is wrong. It gives you information about a *host* USB device; but should properly give you information about a *guest* USB device. The libxl_device_disk_getinfo() counterpart, for example, takes a domid and a virtual device (from within a libxl_disk structure) and returns information about that virtual device.
Hrm... which makes me wonder whether we should use <ctrl,port> as a unique "handle" for usb devices in the interface, or have a separate devid for the devices themselves.
-George
答复: Re: [PATCH V3 3/6] libxl: add pvusb API
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
在 2:16 的 2015/5/20 上,在讯息
<CAFLBxZaw3zCsQ-8tJPH+3cVG4xS-J15_NA930hcRy8x4gRbinw@mail.gmail.com> 中,George Dunlap <[email protected]> 写入:
Quoted 11 lines · Chunyan Liu
Quoted 1 line
+int libxl_device_usb_getinfo(libxl_ctx *ctx, char *busid,
libxl_usbinfo*usbinfo)
Quoted 1 line
+{
Sorry, missed something. This function is wrong. It gives you information about a *host* USB device; but should properly give you information about a *guest* USB device. The
libxl_device_disk_getinfo()counterpart, for example, takes a domid and a virtual device (from within alibxl_diskstructure) and returns information about that virtual device.
Maybe the name should be changed as libxl_device_usb_host_info. Returning *host* USB device info itself is what we need, should not changed.
<ctrl, port> is the most important information to the virtual device. But like in 'xl list', it will first list USB controller, and then each port, USB device appears in some port, it doesn't need to show <ctrl, port> info again, but host information is more useful. e.g.:
#xl usb-list test
Devid Type BE state usb-ver ports BE-path
0 pv 0 4 1 4 /local/domain/0/backend/vusb/1/0
port 1: Bus 003 Device 005 ID: 8087:07dc Intel Corp.
- Chunyan
Quoted 7 lines
Hrm... which makes me wonder whether we should use <ctrl,port> as a unique "handle" for usb devices in the interface, or have a separate devid for the devices themselves.
-George
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH V3 3/6] libxl: add pvusb API
In reply to Chunyan Liutools/libxl/libxl_pvusb.c line 340
Quoted 12 lines · Chunyan Liu
tools/libxl/libxl_pvusb.c new file +12
-0+int libxl_device_usbctrl_getinfo (libxl_ctx *ctx,uint32_t domid, +libxl_device_usbctrl *usbctrl, +libxl_usbctrlinfo *usbctrlinfo) +{ +GC_INIT (ctx); +char *dompath, *usbctrlpath; +char *val; +int rc =0 ; + + usbctrlinfo->devid = usbctrl->devid; + usbctrlinfo->ports = usbctrl->ports; + usbctrlinfo->version = usbctrl->version;
...and following on from the previous mail, it looks like the other device_*_getinfo() functions *only* read devid -- which means you just have to make the struct and fill in devid, and getinfo() will tell *you* the ports, the version, and so on.
-George
PATCH v3 4/6 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 usb-ctrl-attach test_vm version=1 num_ports=8
#xl usb-list test_vm
will show the usb controllers and port usage under the domain.
#xl usb-attach test_vm 1.6
will find the first usable controller:port, and attach usb device whose bus address is 1.6 (busnum is 1, devnum is 6) to it. One could also specify which <controller> and which <port>.
#xl usb-detach test_vm 1.6
#xl usb-ctrl-detach test_vm dev_id
will destroy the controller with specified dev_id. Dev_id can be traced in usb-list info.
---
Changes to v2:
* use bus.addr as user interface instead of busid in usb-attach|detach
* remove usb-assignable-list interface
* add documentation
docs/man/xl.pod.1 | 38 ++++++++ tools/libxl/xl.h | 5 + tools/libxl/xl_cmdimpl.c | 238 ++++++++++++++++++++++++++++++++++++++++++++++ tools/libxl/xl_cmdtable.c | 25 +++++ 4 files changed, 306 insertions(+)
docs/man/xl.pod.1 +38 -0
@@ -1380,6 +1380,44 @@ List pass-through pci devices for a domain. =back +=head1 USB PASS-THROUGH + +=over 4 + +=item B<usb-ctrl-attach> I<domain-id> [I<version=val>] [I<ports=number>] + +Create a new USB controller for the specified domain. +B<version=val> is the usb controller version, could be 1 (USB1.1) or 2 (USB2.0). +B<ports=number> is the total ports of the usb controller. +By default, it will create a USB2.0 controller with 8 ports. + +=item B<usb-ctrl-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<bus.addr> [I<controller=devid> [I<port=number>]] + +Hot-plug a new pass-through USB device to the specified domain. +B<bus.addr> is the busnum.devnum 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, it will create one. + +=item B<usb-detach> I<domain-id> I<bus.addr> + +Hot-unplug a previously assigned USB device from a domain. B<bus.addr> is +busnum.devnum of the physical USB device to be removed from the guest domain. + +If B<-f> is specified, B<xl> is going to forcefully remove the device even +without guest's collaboration. + +=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
@@ -86,6 +86,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 +238 -0
244 lines not on this page · raw message
tools/libxl/xl_cmdtable.c +25 -0
@@ -544,6 +544,31 @@ struct cmd_spec cmd_table[] = {"\"local_mem_bandwidth\": Show local memory bandwidth(KB/s)\n" , }, #endif + { "usb-ctrl-attach", + &main_usbctrl_attach,1 ,1 , +"Create a virtual USB controller for a domain" , +"<Domain> [version=<version>] [ports=<number>]" , + }, + { "usb-ctrl-detach", + &main_usbctrl_detach,0 ,1 , +"Remove the virtual USB controller specified by <DevId> for a domain" , +"<Domain> <DevId>" , + }, + { "usb-attach", + &main_usbattach,1 ,2 , +"Attach a USB device to a domain" , +"<Domain> <bus.addr> [controller=<DevId> [port=<port>]]" , + }, + { "usb-detach", + &main_usbdetach,0 ,1 , +"Detach a USB device from a domain" , +"<Domain> <bus.addr>" , + }, + { "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
1.8.5.2
Re: [PATCH V3 4/6] xl: add pvusb commands
Quoted 22 lines · Chunyan Liu
Add 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 usb-ctrl-attach
test_vmversion=1 num_ports=8#xl usb-list
test_vm
will show the usb controllers and port usage under the domain.#xl usb-attach
test_vm1.6
will find the first usable controller:port, and attach usb device whose bus address is 1.6 (busnum is 1, devnum is 6) to it. One could also specify which <controller> and which <port>.#xl usb-detach
test_vm1.6#xl usb-ctrl-detach
test_vmdev_id
will destroy the controller with specifieddev_id.Dev_idcan be traced in usb-list info.
Quoted 6 lines
Signed-off-by Chunyan Liu <[email protected]>Signed-off-by Simon Cao <[email protected]>---
Changes to v2:
* use bus.addr as user interface instead of busid in usb-attach|detach
* remove usb-assignable-list interface
Why? While lsusb in combination with xl usb-list for each domain will give the same information, having to iterate through all domains can be quite annoying.
An alternative would be to accept omitting the domain for xl usb-list and list all domains with assigned usb devices in this case.
Quoted 8 lines
* add documentation
docs/man/xl.pod.1 | 38 ++++++++ tools/libxl/xl.h | 5 + tools/libxl/xl_cmdimpl.c | 238 ++++++++++++++++++++++++++++++++++++++++++++++ tools/libxl/xl_cmdtable.c | 25 +++++ 4 files changed, 306 insertions(+)
...
tools/libxl/xl_cmdimpl.c int main_cd_insert(int argc, char **argv) · line 3201
Quoted 9 lines
tools/libxl/xl_cmdimpl.c +3
-0@@ -3196,6 +3196,244 @@ int main_cd_insert(int argc, char **argv)return 0 ; } +static void usbinfo_print (libxl_device_usb *usbs,int num) { +int i; +if ( usbs ==NULL )
Coding style.
tools/libxl/xl_cmdimpl.c int main_cd_insert(int argc, char **argv) · line 3207
Quoted 6 lines
tools/libxl/xl_cmdimpl.c +6
-0+return ; +for (i =0 ; i < num; i++) { +libxl_usbinfo usbinfo; +libxl_usbinfo_init (&usbinfo); + +if (usbs[i].port )
Coding style.
Quoted 23 lines
tools/libxl/xl_cmdimpl.c +23
-0+printf (" Port %d:" , usbs[i].port); +if (!libxl_device_usb_getinfo (ctx, usbs[i].busid, &usbinfo)) { +printf (" Bus %03x Device %03x: ID %04x:%04x %s %s\n" , + usbinfo.busnum, usbinfo.devnum, usbinfo.idVendor, + usbinfo.idProduct, usbinfo.manuf, usbinfo.prod); + } +libxl_usbinfo_dispose (&usbinfo); + } +} + +int main_usbctrl_attach (int argc,char **argv) +{ +uint32_t domid; +int opt; +char *oparg; +libxl_device_usbctrl usbctrl; + + SWITCH_FOREACH_OPT(opt, "", NULL, "usb-ctrl-attach", 1) { +/* No options */ + } + + domid =find_domain (argv[optind++]); +
tools/libxl/xl_cmdimpl.c int main_cd_insert(int argc, char **argv) · line 3240
Quoted 10 lines
tools/libxl/xl_cmdimpl.c +10
-0+libxl_device_usbctrl_init (&usbctrl); + +while (argc > optind) { +if (MATCH_OPTION ("version" , argv[optind], oparg)) { + usbctrl.version =atoi (oparg); + }else if (MATCH_OPTION ("ports" , argv[optind], oparg)) { + usbctrl.ports =atoi (oparg); + }else { +fprintf (stderr,"unrecognized argument `%s'\n" , argv[optind]); +exit (-1 );
I don't think this is the preferred way of error handling. Returning with an appropriate error code would be better.
Same applies to all other uses of exit() below.
Quoted 30 lines
tools/libxl/xl_cmdimpl.c +30
-0+ } + optind++; + } + +if (dryrun_only) { +char * json =libxl_device_usbctrl_to_json (ctx, &usbctrl); +printf ("usb controller: %s\n" , json); +free (json); +libxl_device_usbctrl_dispose (&usbctrl); +if (ferror (stdout) ||fflush (stdout)) { +perror ("stdout" ); +exit (-1 ); + } +return 0 ; + } + +if (libxl_device_usbctrl_add (ctx, domid, &usbctrl,0 )) { +fprintf (stderr,"libxl_device_usbctrl_add failed.\n" ); +exit (-1 ); + } +libxl_device_usbctrl_dispose (&usbctrl); +return 0 ; +} + +int main_usbctrl_detach (int argc,char **argv) +{ +uint32_t domid; +int opt; +libxl_device_usbctrl usbctrl; +
tools/libxl/xl_cmdimpl.c int main_cd_insert(int argc, char **argv) · line 3280
Quoted 10 lines
tools/libxl/xl_cmdimpl.c +10
-0+ SWITCH_FOREACH_OPT(opt, "", NULL, "usb-ctrl-detach", 2) { +/* No options */ + } + + domid =find_domain (argv[optind]); + +libxl_device_usbctrl_init (&usbctrl); + usbctrl.devid =atoi (argv[optind+1 ]); + +if (libxl_device_usbctrl_remove (ctx, domid, &usbctrl,0 )) {
Coding style.
tools/libxl/xl_cmdimpl.c int main_cd_insert(int argc, char **argv) · line 3273
Quoted 7 lines
tools/libxl/xl_cmdimpl.c +7
-0+fprintf (stderr,"libxl_device_usbctrl_add failed.\n" ); +exit (-1 ); + } +libxl_device_usbctrl_dispose (&usbctrl); +return 0 ; + +}
Juergen
Re: [PATCH V3 4/6] xl: add pvusb commands
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
On 4/20/2015 at 04:12 PM, in message <[email protected]>, Juergen Gross
Quoted 30 lines · <[email protected]> wrote:
Quoted 22 lines · Chunyan Liu
Add 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 usb-ctrl-attach
test_vmversion=1 num_ports=8#xl usb-list
test_vm
will show the usb controllers and port usage under the domain.#xl usb-attach
test_vm1.6
will find the first usable controller:port, and attach usb device whose bus address is 1.6 (busnum is 1, devnum is 6) to it. One could also specify which <controller> and which <port>.#xl usb-detach
test_vm1.6#xl usb-ctrl-detach
test_vmdev_id
will destroy the controller with specifieddev_id.Dev_idcan be traced in usb-list info.Quoted 6 lines
Signed-off-by Chunyan Liu <[email protected]>Signed-off-by Simon Cao <[email protected]>---
Changes to v2:
* use bus.addr as user interface instead of busid in usb-attach|detach
* remove usb-assignable-list interface
Quoted 6 lines
Why? While lsusb in combination with xl usb-list for each domain will give the same information, having to iterate through all domains can be quite annoying.
An alternative would be to accept omitting the domain for xl usb-list and list all domains with assigned usb devices in this case.
Ian & George, how do you think?
Quoted 131 lines
Quoted 6 lines
* add documentation
docs/man/xl.pod.1 | 38 ++++++++ tools/libxl/xl.h | 5 + tools/libxl/xl_cmdimpl.c | 238++++++++++++++++++++++++++++++++++++++++++++++
Quoted 3 lines
tools/libxl/xl_cmdtable.c | 25 +++++ 4 files changed, 306 insertions(+)...
tools/libxl/xl_cmdimpl.c int main_cd_insert(int argc, char **argv) · line 3201
Quoted 9 lines
tools/libxl/xl_cmdimpl.c +3
-0@@ -3196,6 +3196,244 @@ int main_cd_insert(int argc, char **argv)return 0 ; } +static void usbinfo_print (libxl_device_usb *usbs,int num) { +int i; +if ( usbs ==NULL )Coding style.
tools/libxl/xl_cmdimpl.c int main_cd_insert(int argc, char **argv) · line 3207
Quoted 6 lines
tools/libxl/xl_cmdimpl.c +6
-0+return ; +for (i =0 ; i < num; i++) { +libxl_usbinfo usbinfo; +libxl_usbinfo_init (&usbinfo); + +if (usbs[i].port )Coding style.
Quoted 23 lines
tools/libxl/xl_cmdimpl.c +23
-0+printf (" Port %d:" , usbs[i].port); +if (!libxl_device_usb_getinfo (ctx, usbs[i].busid, &usbinfo)) { +printf (" Bus %03x Device %03x: ID %04x:%04x %s %s\n" , + usbinfo.busnum, usbinfo.devnum, usbinfo.idVendor, + usbinfo.idProduct, usbinfo.manuf, usbinfo.prod); + } +libxl_usbinfo_dispose (&usbinfo); + } +} + +int main_usbctrl_attach (int argc,char **argv) +{ +uint32_t domid; +int opt; +char *oparg; +libxl_device_usbctrl usbctrl; + + SWITCH_FOREACH_OPT(opt, "", NULL, "usb-ctrl-attach", 1) { +/* No options */ + } + + domid =find_domain (argv[optind++]); +tools/libxl/xl_cmdimpl.c int main_cd_insert(int argc, char **argv) · line 3240
Quoted 10 lines
tools/libxl/xl_cmdimpl.c +10
-0+libxl_device_usbctrl_init (&usbctrl); + +while (argc > optind) { +if (MATCH_OPTION ("version" , argv[optind], oparg)) { + usbctrl.version =atoi (oparg); + }else if (MATCH_OPTION ("ports" , argv[optind], oparg)) { + usbctrl.ports =atoi (oparg); + }else { +fprintf (stderr,"unrecognized argument `%s'\n" , argv[optind]); +exit (-1 );I don't think this is the preferred way of error handling. Returning with an appropriate error code would be better.
Same applies to all other uses of
exit()below.Quoted 30 lines
tools/libxl/xl_cmdimpl.c +30
-0+ } + optind++; + } + +if (dryrun_only) { +char * json =libxl_device_usbctrl_to_json (ctx, &usbctrl); +printf ("usb controller: %s\n" , json); +free (json); +libxl_device_usbctrl_dispose (&usbctrl); +if (ferror (stdout) ||fflush (stdout)) { +perror ("stdout" ); +exit (-1 ); + } +return 0 ; + } + +if (libxl_device_usbctrl_add (ctx, domid, &usbctrl,0 )) { +fprintf (stderr,"libxl_device_usbctrl_add failed.\n" ); +exit (-1 ); + } +libxl_device_usbctrl_dispose (&usbctrl); +return 0 ; +} + +int main_usbctrl_detach (int argc,char **argv) +{ +uint32_t domid; +int opt; +libxl_device_usbctrl usbctrl; +tools/libxl/xl_cmdimpl.c int main_cd_insert(int argc, char **argv) · line 3280
Quoted 10 lines
tools/libxl/xl_cmdimpl.c +10
-0+ SWITCH_FOREACH_OPT(opt, "", NULL, "usb-ctrl-detach", 2) { +/* No options */ + } + + domid =find_domain (argv[optind]); + +libxl_device_usbctrl_init (&usbctrl); + usbctrl.devid =atoi (argv[optind+1 ]); + +if (libxl_device_usbctrl_remove (ctx, domid, &usbctrl,0 )) {Coding style.
tools/libxl/xl_cmdimpl.c int main_cd_insert(int argc, char **argv) · line 3273
Quoted 7 lines
tools/libxl/xl_cmdimpl.c +7
-0+fprintf (stderr,"libxl_device_usbctrl_add failed.\n" ); +exit (-1 ); + } +libxl_device_usbctrl_dispose (&usbctrl); +return 0 ; + +}Juergen
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH V3 4/6] xl: add pvusb commands
In reply to Juergen GrossQuoted 32 lines · Juergen Gross
Quoted 23 lines · Chunyan Liu
Add 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 usb-ctrl-attach
test_vmversion=1 num_ports=8#xl usb-list
test_vm
will show the usb controllers and port usage under the domain.#xl usb-attach
test_vm1.6
will find the first usable controller:port, and attach usb device whose bus address is 1.6 (busnum is 1, devnum is 6) to it. One could also specify which <controller> and which <port>.#xl usb-detach
test_vm1.6#xl usb-ctrl-detach
test_vmdev_id
will destroy the controller with specifieddev_id.Dev_idcan be traced in usb-list info.Quoted 6 lines
Signed-off-by Chunyan Liu <[email protected]>Signed-off-by Simon Cao <[email protected]>---
Changes to v2:
* use bus.addr as user interface instead of busid in usb-attach|detach
* remove usb-assignable-list interface
Quoted 6 lines
Why? While lsusb in combination with xl usb-list for each domain will give the same information, having to iterate through all domains can be quite annoying.
An alternative would be to accept omitting the domain for xl usb-list and list all domains with assigned usb devices in this case.
I don't understand what information it is that you want. Do you want a list of devices *not already assigned* to domains?
-George
Re: [PATCH V3 4/6] xl: add pvusb commands
Quoted 37 lines · George Dunlap
Quoted 36 lines · Juergen Gross
Quoted 23 lines · Chunyan Liu
Add 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 usb-ctrl-attach
test_vmversion=1 num_ports=8#xl usb-list
test_vm
will show the usb controllers and port usage under the domain.#xl usb-attach
test_vm1.6
will find the first usable controller:port, and attach usb device whose bus address is 1.6 (busnum is 1, devnum is 6) to it. One could also specify which <controller> and which <port>.#xl usb-detach
test_vm1.6#xl usb-ctrl-detach
test_vmdev_id
will destroy the controller with specifieddev_id.Dev_idcan be traced in usb-list info.Quoted 6 lines
Signed-off-by Chunyan Liu <[email protected]>Signed-off-by Simon Cao <[email protected]>---
Changes to v2:
* use bus.addr as user interface instead of busid in usb-attach|detach
* remove usb-assignable-list interfaceWhy? While lsusb in combination with xl usb-list for each domain will give the same information, having to iterate through all domains can be quite annoying.
Quoted 5 lines
Quoted 2 lines
An alternative would be to accept omitting the domain for xl usb-list and list all domains with assigned usb devices in this case.
I don't understand what information it is that you want. Do you want a list of devices *not already assigned* to domains?
Yes.
Juergen
Re: [PATCH V3 4/6] xl: add pvusb commands
Quoted 47 lines · Juergen Gross
Quoted 46 lines · George Dunlap
Quoted 42 lines · Juergen Gross
Quoted 24 lines · Chunyan Liu
Add 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 usb-ctrl-attach
test_vmversion=1 num_ports=8#xl usb-list
test_vm
will show the usb controllers and port usage under the domain.#xl usb-attach
test_vm1.6
will find the first usable controller:port, and attach usb device whose bus address is 1.6 (busnum is 1, devnum is 6) to it. One could also specify which <controller> and which <port>.#xl usb-detach
test_vm1.6#xl usb-ctrl-detach
test_vmdev_id
will destroy the controller with specifieddev_id.Dev_idcan be traced in usb-list info.Quoted 7 lines
Signed-off-by Chunyan Liu <[email protected]>Signed-off-by Simon Cao <[email protected]>---
Changes to v2:
* use bus.addr as user interface instead of busid in
usb-attach|detach
* remove usb-assignable-list interfaceWhy? While lsusb in combination with xl usb-list for each domain will give the same information, having to iterate through all domains can be quite annoying.
An alternative would be to accept omitting the domain for xl usb-list and list all domains with assigned usb devices in this case.
Quoted 5 lines
Quoted 2 lines
I don't understand what information it is that you want. Do you want a list of devices *not already assigned* to domains?
Yes.
...and why do you need that, instead of just remembering what you'd assigned to whom?
We don't really have the equivalent for pci either. That is, if a device shows up in "lspci" but not in "pci-assignable-list", that may be either because 1) I hasn't yet been assigned to pciback (and this is available to be assigned to a domain), or 2) because it's already been assigned to a domain. Someone new coming to the system would need to check all VMs to see which devices hadn't yet been assigned.
-George
Re: [PATCH V3 4/6] xl: add pvusb commands
Quoted 57 lines · George Dunlap
Quoted 47 lines · Juergen Gross
Quoted 46 lines · George Dunlap
Quoted 42 lines · Juergen Gross
Quoted 24 lines · Chunyan Liu
Add 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 usb-ctrl-attach
test_vmversion=1 num_ports=8#xl usb-list
test_vm
will show the usb controllers and port usage under the domain.#xl usb-attach
test_vm1.6
will find the first usable controller:port, and attach usb device whose bus address is 1.6 (busnum is 1, devnum is 6) to it. One could also specify which <controller> and which <port>.#xl usb-detach
test_vm1.6#xl usb-ctrl-detach
test_vmdev_id
will destroy the controller with specifieddev_id.Dev_idcan be traced in usb-list info.Quoted 7 lines
Signed-off-by Chunyan Liu <[email protected]>Signed-off-by Simon Cao <[email protected]>---
Changes to v2:
* use bus.addr as user interface instead of busid in
usb-attach|detach
* remove usb-assignable-list interfaceWhy? While lsusb in combination with xl usb-list for each domain will give the same information, having to iterate through all domains can be quite annoying.
An alternative would be to accept omitting the domain for xl usb-list and list all domains with assigned usb devices in this case.
Quoted 5 lines
Quoted 2 lines
I don't understand what information it is that you want. Do you want a list of devices *not already assigned* to domains?
Yes.
...and why do you need that, instead of just remembering what you'd assigned to whom?
Quoted 6 lines
We don't really have the equivalent for pci either. That is, if a device shows up in "lspci" but not in "pci-assignable-list", that may be either because 1) I hasn't yet been assigned to pciback (and this is available to be assigned to a domain), or 2) because it's already been assigned to a domain. Someone new coming to the system would need to check all VMs to see which devices hadn't yet been assigned.
So this is a problem of pci-assignable-list, which isn't present for USB devices. Any USB device not already assigned to a VM would be listed as before with "xm usb-assignable-list".
Additionally all systems support hotplug of USB devices - the list of available USB-devices can change rather often. If you unplug e.g. a memory stick which has been assigned to a VM and stick it in again it might show up under a different address and has to be reassigned. Remembering having it already assigned won't help in this case as much as a simple command to list the devices ready for assignment.
Juergen
Re: [PATCH V3 4/6] xl: add pvusb commands
Quoted 71 lines · Juergen Gross
Quoted 59 lines · George Dunlap
Quoted 49 lines · Juergen Gross
Quoted 48 lines · George Dunlap
Quoted 44 lines · Juergen Gross
Quoted 25 lines · Chunyan Liu
Add 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 usb-ctrl-attach
test_vmversion=1 num_ports=8#xl usb-list
test_vm
will show the usb controllers and port usage under the domain.#xl usb-attach
test_vm1.6
will find the first usable controller:port, and attach usb device whose bus address is 1.6 (busnum is 1, devnum is 6) to it. One could also specify which <controller> and which
<port>.#xl usb-detach
test_vm1.6#xl usb-ctrl-detach
test_vmdev_id
will destroy the controller with specifieddev_id.Dev_idcan be traced in usb-list info.Quoted 7 lines
Signed-off-by Chunyan Liu <[email protected]>Signed-off-by Simon Cao <[email protected]>---
Changes to v2:
* use bus.addr as user interface instead of busid in
usb-attach|detach
* remove usb-assignable-list interfaceWhy? While lsusb in combination with xl usb-list for each domain will give the same information, having to iterate through all domains can be
quite annoying.An alternative would be to accept omitting the domain for xl usb-list and list all domains with assigned usb devices in this case.
Quoted 5 lines
Quoted 2 lines
I don't understand what information it is that you want. Do you want a list of devices *not already assigned* to domains?
Yes.
...and why do you need that, instead of just remembering what you'd assigned to whom?
Quoted 6 lines
We don't really have the equivalent for pci either. That is, if a device shows up in "lspci" but not in "pci-assignable-list", that may be either because 1) I hasn't yet been assigned to pciback (and this is available to be assigned to a domain), or 2) because it's already been assigned to a domain. Someone new coming to the system would need to check all VMs to see which devices hadn't yet been assigned.
So this is a problem of pci-assignable-list, which isn't present for USB devices. Any USB device not already assigned to a VM would be listed as before with "xm usb-assignable-list".
Quoted 6 lines
Additionally all systems support hotplug of USB devices - the list of available USB-devices can change rather often. If you unplug e.g. a memory stick which has been assigned to a VM and stick it in again it might show up under a different address and has to be reassigned. Remembering having it already assigned won't help in this case as much as a simple command to list the devices ready for assignment.
OK. :-) Yes, I can see that having USB devices disappear and appear as a different bus:host would make such a command particularly useful.
But then we have the problem that "assignable" means something different in each case. In pci-assignable-list, it means "Devices which have been assigned to pciback but not yet been attached to a domain". In your suggested command, it means "Devices which have not yet been assigned either to pvusbback or to a domain."
When I introduced pci-assignable-* I didn't realize that "assignable" was already in one of the xm commands with a different meaning. I introduced it because until that point, neither xm nor xl had a way of attaching a device to pciback.
So we basically have three options:
1. Keep both names the same
2. Rename usb-assignable-list to something else (usb-available-list?)
3. Rename pci-assignable-* to something else
#1 would be the most backwards-compatible. But I think it's really bad going forward, since you have two commands that look like they should do similar things that don't.
#2 and #3 would both solve the potential confusion issue.
#2 would be the easiest on current xl users. It's a bit annoying interface-wise going forward, since "assignable" is probably the best word for what Juergen wants; and it's not 100% backwards-compatible with the previous xm usb commands.
#3 would give us probably the most consistent naming thing going forward, but would be a pretty major breakage for current xl users.
I'm inclined to suggest #2 as the best balance between not disrupting current users and not confusing future users.
Wei / Ian, any opinions?
-George
Re: [PATCH V3 4/6] xl: add pvusb commands
Quoted 79 lines · George Dunlap
Quoted 71 lines · Juergen Gross
Quoted 59 lines · George Dunlap
Quoted 49 lines · Juergen Gross
Quoted 48 lines · George Dunlap
Quoted 44 lines · Juergen Gross
Quoted 25 lines · Chunyan Liu
Add 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 usb-ctrl-attach
test_vmversion=1 num_ports=8#xl usb-list
test_vm
will show the usb controllers and port usage under the domain.#xl usb-attach
test_vm1.6
will find the first usable controller:port, and attach usb device whose bus address is 1.6 (busnum is 1, devnum is 6) to it. One could also specify which <controller> and which
<port>.#xl usb-detach
test_vm1.6#xl usb-ctrl-detach
test_vmdev_id
will destroy the controller with specifieddev_id.Dev_idcan be traced in usb-list info.Quoted 7 lines
Signed-off-by Chunyan Liu <[email protected]>Signed-off-by Simon Cao <[email protected]>---
Changes to v2:
* use bus.addr as user interface instead of busid in
usb-attach|detach
* remove usb-assignable-list interfaceWhy? While lsusb in combination with xl usb-list for each domain will give the same information, having to iterate through all domains can be
quite annoying.An alternative would be to accept omitting the domain for xl usb-list and list all domains with assigned usb devices in this case.
Quoted 5 lines
Quoted 2 lines
I don't understand what information it is that you want. Do you want a list of devices *not already assigned* to domains?
Yes.
...and why do you need that, instead of just remembering what you'd assigned to whom?
Quoted 6 lines
We don't really have the equivalent for pci either. That is, if a device shows up in "lspci" but not in "pci-assignable-list", that may be either because 1) I hasn't yet been assigned to pciback (and this is available to be assigned to a domain), or 2) because it's already been assigned to a domain. Someone new coming to the system would need to check all VMs to see which devices hadn't yet been assigned.
So this is a problem of pci-assignable-list, which isn't present for USB devices. Any USB device not already assigned to a VM would be listed as before with "xm usb-assignable-list".
Quoted 6 lines
Additionally all systems support hotplug of USB devices - the list of available USB-devices can change rather often. If you unplug e.g. a memory stick which has been assigned to a VM and stick it in again it might show up under a different address and has to be reassigned. Remembering having it already assigned won't help in this case as much as a simple command to list the devices ready for assignment.
Quoted 8 lines
OK. :-) Yes, I can see that having USB devices disappear and appear as a different bus:host would make such a command particularly useful.
But then we have the problem that "assignable" means something different in each case. In pci-assignable-list, it means "Devices which have been assigned to pciback but not yet been attached to a domain". In your suggested command, it means "Devices which have not yet been assigned either to pvusbback or to a domain."
For USB devices there isn't such an action as "assign it to pvusbback". You just assign a USB device to a domain. One command, no steps to prepare that action (besides the possibility to define a USB controller first).
Quoted 5 lines
When I introduced pci-assignable-* I didn't realize that "assignable" was already in one of the xm commands with a different meaning. I introduced it because until that point, neither xm nor xl had a way of attaching a device to pciback.
Quoted 8 lines
So we basically have three options:
1. Keep both names the same
2. Rename usb-assignable-list to something else (usb-available-list?)
3. Rename pci-assignable-* to something else#1 would be the most backwards-compatible. But I think it's really bad going forward, since you have two commands that look like they should do similar things that don't.
Actually they do. :-)
Juergen
Quoted 19 lines
#2 and #3 would both solve the potential confusion issue.
#2 would be the easiest on current xl users. It's a bit annoying interface-wise going forward, since "assignable" is probably the best word for what Juergen wants; and it's not 100% backwards-compatible with the previous xm usb commands.
#3 would give us probably the most consistent naming thing going forward, but would be a pretty major breakage for current xl users.
I'm inclined to suggest #2 as the best balance between not disrupting current users and not confusing future users.
Wei / Ian, any opinions?
-George
Re: [PATCH V3 4/6] xl: add pvusb commands
Quoted 92 lines · Juergen Gross
Quoted 82 lines · George Dunlap
Quoted 74 lines · Juergen Gross
Quoted 62 lines · George Dunlap
Quoted 52 lines · Juergen Gross
Quoted 51 lines · George Dunlap
Quoted 46 lines · Juergen Gross
Quoted 25 lines · Chunyan Liu
Add 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 usb-ctrl-attach
test_vmversion=1 num_ports=8#xl usb-list
test_vm
will show the usb controllers and port usage under the domain.#xl usb-attach
test_vm1.6
will find the first usable controller:port, and attach usb device whose bus address is 1.6 (busnum is 1, devnum is 6) to it. One could also specify which <controller> and which
<port>.#xl usb-detach
test_vm1.6#xl usb-ctrl-detach
test_vmdev_id
will destroy the controller with specifieddev_id.Dev_idcan be traced in usb-list info.Quoted 7 lines
Signed-off-by Chunyan Liu <[email protected]>Signed-off-by Simon Cao <[email protected]>---
Changes to v2:
* use bus.addr as user interface instead of busid in
usb-attach|detach
* remove usb-assignable-list interfaceWhy? While lsusb in combination with xl usb-list for each domain will
give the same information, having to iterate through all domains can be
quite annoying.An alternative would be to accept omitting the domain for xl usb-list
and list all domains with assigned usb devices in this case.Quoted 5 lines
Quoted 2 lines
I don't understand what information it is that you want. Do you want a list of devices *not already assigned* to domains?
Yes.
...and why do you need that, instead of just remembering what you'd assigned to whom?
Quoted 6 lines
We don't really have the equivalent for pci either. That is, if a device shows up in "lspci" but not in "pci-assignable-list", that may be either because 1) I hasn't yet been assigned to pciback (and this is available to be assigned to a domain), or 2) because it's already been assigned to a domain. Someone new coming to the system would need to check all VMs to see which devices hadn't yet been assigned.
So this is a problem of pci-assignable-list, which isn't present for USB devices. Any USB device not already assigned to a VM would be listed as before with "xm usb-assignable-list".
Quoted 6 lines
Additionally all systems support hotplug of USB devices - the list of available USB-devices can change rather often. If you unplug e.g. a memory stick which has been assigned to a VM and stick it in again it might show up under a different address and has to be reassigned. Remembering having it already assigned won't help in this case as much as a simple command to list the devices ready for assignment.
Quoted 8 lines
OK. :-) Yes, I can see that having USB devices disappear and appear as a different bus:host would make such a command particularly useful.
But then we have the problem that "assignable" means something different in each case. In pci-assignable-list, it means "Devices which have been assigned to pciback but not yet been attached to a domain". In your suggested command, it means "Devices which have not yet been assigned either to pvusbback or to a domain."
Quoted 4 lines
For USB devices there isn't such an action as "assign it to pvusbback". You just assign a USB device to a domain. One command, no steps to prepare that action (besides the possibility to define a USB controller first).
You can do the same thing (now) for pci devices, by setting "seize=1" in the device spec (which will cause xl to first assign them to pciback, then to the guest, just as the usb code does now).
Quoted 11 lines
Quoted 11 lines
When I introduced pci-assignable-* I didn't realize that "assignable" was already in one of the xm commands with a different meaning. I introduced it because until that point, neither xm nor xl had a way of attaching a device to pciback.
So we basically have three options:
1. Keep both names the same
2. Rename usb-assignable-list to something else (usb-available-list?)
3. Rename pci-assignable-* to something else
Quoted 5 lines
Quoted 3 lines
#1 would be the most backwards-compatible. But I think it's really bad going forward, since you have two commands that look like they should do similar things that don't.
Actually they do. :-)
Actually, they don't. :-)
Quoted 1 line
From your description, "xm usb-assignable-list" would list *all* USB
devices on the system which were available to be assigned to a guest.
"xl pci-assignable-list" does *not* list all PCI devices on a system which are available to be assigned. It lists the ones which can be assigned without the "seize=1" parameter -- the ones you've already done something with. It won't tell you about the other devices on the system which have not yet been assigned to pciback.
Yes, from a pedantic perspective, both will tell you on which devices you can run "X-attach" without any extra arguments. But from a practical perspective, "xm usb-assignable-list" tells you something practical about the state of devices on the whole system; and "xl pci-assignable-list" tells you a technical quirk about devices are in a half-way state between not being assigned and actually being assigned.
(I'm about 45% of the opinion that we should make pci behave like the proposed USB patches -- i.e., make seize=1 the default, and just deprecate the whole "pci-assignable" state altogether. My only hesitation is that it removes one level of safety check against doing something like unplugging the host's main disk controller or something.)
-George
Re: [PATCH V3 4/6] xl: add pvusb commands
Quoted 121 lines · George Dunlap
Quoted 92 lines · Juergen Gross
Quoted 82 lines · George Dunlap
Quoted 74 lines · Juergen Gross
Quoted 62 lines · George Dunlap
Quoted 52 lines · Juergen Gross
Quoted 51 lines · George Dunlap
Quoted 46 lines · Juergen Gross
Quoted 25 lines · Chunyan Liu
Add 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 usb-ctrl-attach
test_vmversion=1 num_ports=8#xl usb-list
test_vm
will show the usb controllers and port usage under the domain.#xl usb-attach
test_vm1.6
will find the first usable controller:port, and attach usb device whose bus address is 1.6 (busnum is 1, devnum is 6) to it. One could also specify which <controller> and which
<port>.#xl usb-detach
test_vm1.6#xl usb-ctrl-detach
test_vmdev_id
will destroy the controller with specifieddev_id.Dev_idcan be traced in usb-list info.Quoted 7 lines
Signed-off-by Chunyan Liu <[email protected]>Signed-off-by Simon Cao <[email protected]>---
Changes to v2:
* use bus.addr as user interface instead of busid in
usb-attach|detach
* remove usb-assignable-list interfaceWhy? While lsusb in combination with xl usb-list for each domain will
give the same information, having to iterate through all domains can be
quite annoying.An alternative would be to accept omitting the domain for xl usb-list
and list all domains with assigned usb devices in this case.Quoted 5 lines
Quoted 2 lines
I don't understand what information it is that you want. Do you want a list of devices *not already assigned* to domains?
Yes.
...and why do you need that, instead of just remembering what you'd assigned to whom?
Quoted 6 lines
We don't really have the equivalent for pci either. That is, if a device shows up in "lspci" but not in "pci-assignable-list", that may be either because 1) I hasn't yet been assigned to pciback (and this is available to be assigned to a domain), or 2) because it's already been assigned to a domain. Someone new coming to the system would need to check all VMs to see which devices hadn't yet been assigned.
So this is a problem of pci-assignable-list, which isn't present for USB devices. Any USB device not already assigned to a VM would be listed as before with "xm usb-assignable-list".
Quoted 6 lines
Additionally all systems support hotplug of USB devices - the list of available USB-devices can change rather often. If you unplug e.g. a memory stick which has been assigned to a VM and stick it in again it might show up under a different address and has to be reassigned. Remembering having it already assigned won't help in this case as much as a simple command to list the devices ready for assignment.
Quoted 8 lines
OK. :-) Yes, I can see that having USB devices disappear and appear as a different bus:host would make such a command particularly useful.
But then we have the problem that "assignable" means something different in each case. In pci-assignable-list, it means "Devices which have been assigned to pciback but not yet been attached to a domain". In your suggested command, it means "Devices which have not yet been assigned either to pvusbback or to a domain."
Quoted 4 lines
For USB devices there isn't such an action as "assign it to pvusbback". You just assign a USB device to a domain. One command, no steps to prepare that action (besides the possibility to define a USB controller first).
You can do the same thing (now) for pci devices, by setting "seize=1" in the device spec (which will cause xl to first assign them to pciback, then to the guest, just as the usb code does now).
Quoted 11 lines
Quoted 11 lines
When I introduced pci-assignable-* I didn't realize that "assignable" was already in one of the xm commands with a different meaning. I introduced it because until that point, neither xm nor xl had a way of attaching a device to pciback.
So we basically have three options:
1. Keep both names the same
2. Rename usb-assignable-list to something else (usb-available-list?)
3. Rename pci-assignable-* to something elseQuoted 5 lines
Quoted 3 lines
#1 would be the most backwards-compatible. But I think it's really bad going forward, since you have two commands that look like they should do similar things that don't.
Actually they do. :-)
Actually, they don't. :-)
Quoted 8 lines
From your description, "xm usb-assignable-list" would list *all* USB
devices on the system which were available to be assigned to a guest."xl pci-assignable-list" does *not* list all PCI devices on a system which are available to be assigned. It lists the ones which can be assigned without the "seize=1" parameter -- the ones you've already done something with. It won't tell you about the other devices on the system which have not yet been assigned to pciback.
So "xl pci-assignable-list" is suppressing some of the PCI devices which in theory could be assigned. I don't think this "weird" behaviour should be mimicked by "xl usb-assignable-list".
Quoted 6 lines
Yes, from a pedantic perspective, both will tell you on which devices you can run "X-attach" without any extra arguments. But from a practical perspective, "xm usb-assignable-list" tells you something practical about the state of devices on the whole system; and "xl pci-assignable-list" tells you a technical quirk about devices are in a half-way state between not being assigned and actually being assigned.
I can't believe you are suggesting to use "a technical quirk" as a good example for future development. Just because a user interface isn't perfect shouldn't result in other interfaces to behave in the same imperfect way.
Quoted 5 lines
(I'm about 45% of the opinion that we should make pci behave like the proposed USB patches -- i.e., make seize=1 the default, and just deprecate the whole "pci-assignable" state altogether. My only hesitation is that it removes one level of safety check against doing something like unplugging the host's main disk controller or something.)
You could add a configuration item in xl.conf for setting the default behaviour, defaulting to "default_seize=1". :-)
Juergen
Re: [PATCH V3 4/6] xl: add pvusb commands
Quoted 9 lines · Juergen Gross
Quoted 8 lines
From your description, "xm usb-assignable-list" would list *all* USB
devices on the system which were available to be assigned to a guest."xl pci-assignable-list" does *not* list all PCI devices on a system which are available to be assigned. It lists the ones which can be assigned without the "seize=1" parameter -- the ones you've already done something with. It won't tell you about the other devices on the system which have not yet been assigned to pciback.
Quoted 3 lines
So "xl pci-assignable-list" is suppressing some of the PCI devices which in theory could be assigned. I don't think this "weird" behaviour should be mimicked by "xl usb-assignable-list".
That's not really an accurate characterization. I introduced the concept of "assignable" because under xm, you had to manually much about in sysfs yourself to assign the device to pciback before attaching it to a guest. So "pci-assignable-add" takes a device and assigns it to pciback; "pci-attach" attaches an assignable device to the guest. "pci-assignable-list" lists the devices which have been made "assignable" under this new definition.
Quoted 8 lines
Quoted 7 lines
Yes, from a pedantic perspective, both will tell you on which devices you can run "X-attach" without any extra arguments. But from a practical perspective, "xm usb-assignable-list" tells you something practical about the state of devices on the whole system; and "xl pci-assignable-list" tells you a technical quirk about devices are in a half-way state between not being assigned and actually being assigned.
Quoted 4 lines
I can't believe you are suggesting to use "a technical quirk" as a good example for future development. Just because a user interface isn't perfect shouldn't result in other interfaces to behave in the same imperfect way.
You seem to have missed in my tone that I think "xm usb-assignable-list" behavior is more useful. I said that "xm usb-assignable-list" gave you practical information, and I said that "xl pci-assignable-list" gives you about a technical quirk. I also said that "pci-assignable" a state half-way in between being assigned and not assigned, which I personally think portrays it as rather clunky.
I never claimed that we should make the new usb-*-list command mimic pci-assignable-list. What I'm responding to is your claim that they do similar things, and so implying that it should be OK for them to have similar names. They do not do the similar things, and therefore they must not have similar names.
So, as I said in the previous e-mail:
* I think that it would definitely be useful to have the "xm usb-assignable-list" functionality.
* But we cannot give it the same name as the current "xl pci-assignable-list" functionality, since they behave differently
* I think "assignable" is the best name for what "xm usb-assignable-list" does; however,
* We have existing users to consider; I think choosing a different name (like "xl usb-available-list") will have the lowest negative impact on existing users.
Quoted 8 lines
Quoted 5 lines
(I'm about 45% of the opinion that we should make pci behave like the proposed USB patches -- i.e., make seize=1 the default, and just deprecate the whole "pci-assignable" state altogether. My only hesitation is that it removes one level of safety check against doing something like unplugging the host's main disk controller or something.)
You could add a configuration item in xl.conf for setting the default behaviour, defaulting to "default_seize=1". :-)
I think we have that already, actually. (If not at least we discussed it.) But unfortunately that doesn't help us in the current situation, as the "pci-assignable" state still exists, and we still need commands to deal with it.
-George
Re: [PATCH V3 4/6] xl: add pvusb commands
Quoted 35 lines · George Dunlap
Quoted 9 lines · Juergen Gross
Quoted 8 lines
From your description, "xm usb-assignable-list" would list *all* USB
devices on the system which were available to be assigned to a guest."xl pci-assignable-list" does *not* list all PCI devices on a system which are available to be assigned. It lists the ones which can be assigned without the "seize=1" parameter -- the ones you've already done something with. It won't tell you about the other devices on the system which have not yet been assigned to pciback.
Quoted 3 lines
So "xl pci-assignable-list" is suppressing some of the PCI devices which in theory could be assigned. I don't think this "weird" behaviour should be mimicked by "xl usb-assignable-list".
That's not really an accurate characterization. I introduced the concept of "assignable" because under xm, you had to manually much about in sysfs yourself to assign the device to pciback before attaching it to a guest. So "pci-assignable-add" takes a device and assigns it to pciback; "pci-attach" attaches an assignable device to the guest. "pci-assignable-list" lists the devices which have been made "assignable" under this new definition.
Quoted 8 lines
Quoted 7 lines
Yes, from a pedantic perspective, both will tell you on which devices you can run "X-attach" without any extra arguments. But from a practical perspective, "xm usb-assignable-list" tells you something practical about the state of devices on the whole system; and "xl pci-assignable-list" tells you a technical quirk about devices are in a half-way state between not being assigned and actually being assigned.
Quoted 4 lines
I can't believe you are suggesting to use "a technical quirk" as a good example for future development. Just because a user interface isn't perfect shouldn't result in other interfaces to behave in the same imperfect way.
Quoted 6 lines
You seem to have missed in my tone that I think "xm usb-assignable-list" behavior is more useful. I said that "xm usb-assignable-list" gave you practical information, and I said that "xl pci-assignable-list" gives you about a technical quirk. I also said that "pci-assignable" a state half-way in between being assigned and not assigned, which I personally think portrays it as rather clunky.
I didn't want to offend you, sorry if you felt that way.
Quoted 6 lines
I never claimed that we should make the new usb-*-list command mimic pci-assignable-list. What I'm responding to is your claim that they do similar things, and so implying that it should be OK for them to have similar names. They do not do the similar things, and therefore they must not have similar names.
Quoted 10 lines
So, as I said in the previous e-mail:
* I think that it would definitely be useful to have the "xm usb-assignable-list" functionality.
* But we cannot give it the same name as the current "xl pci-assignable-list" functionality, since they behave differently
* I think "assignable" is the best name for what "xm usb-assignable-list" does; however,
* We have existing users to consider; I think choosing a different name (like "xl usb-available-list") will have the lowest negative impact on existing users.
There might be existing users who know about "xm usb-assignable-list".
OTOH I don't care giving it another name, as long as the functionality is available.
Juergen
Re: [PATCH V3 4/6] xl: add pvusb commands
Quoted 12 lines · Juergen Gross
Quoted 12 lines
Quoted 11 lines
Quoted 6 lines
Yes, from a pedantic perspective, both will tell you on which devices you can run "X-attach" without any extra arguments. But from a practical perspective, "xm usb-assignable-list" tells you something practical about the state of devices on the whole system; and "xl pci-assignable-list" tells you a technical quirk about devices are in a half-way state between not being assigned and actually being assigned.
I can't believe you are suggesting to use "a technical quirk" as a good example for future development. Just because a user interface isn't perfect shouldn't result in other interfaces to behave in the same imperfect way.
Quoted 8 lines
Quoted 6 lines
You seem to have missed in my tone that I think "xm usb-assignable-list" behavior is more useful. I said that "xm usb-assignable-list" gave you practical information, and I said that "xl pci-assignable-list" gives you about a technical quirk. I also said that "pci-assignable" a state half-way in between being assigned and not assigned, which I personally think portrays it as rather clunky.
I didn't want to offend you, sorry if you felt that way.
Misunderstandings happen -- I certainly misread what people write sometimes. It's mildly annoying but no harm done. :-)
Quoted 17 lines
Quoted 6 lines
I never claimed that we should make the new usb-*-list command mimic pci-assignable-list. What I'm responding to is your claim that they do similar things, and so implying that it should be OK for them to have similar names. They do not do the similar things, and therefore they must not have similar names.
Quoted 10 lines
So, as I said in the previous e-mail:
* I think that it would definitely be useful to have the "xm usb-assignable-list" functionality.
* But we cannot give it the same name as the current "xl pci-assignable-list" functionality, since they behave differently
* I think "assignable" is the best name for what "xm usb-assignable-list" does; however,
* We have existing users to consider; I think choosing a different name (like "xl usb-available-list") will have the lowest negative impact on existing users.
Quoted 1 line
There might be existing users who know about "xm usb-assignable-list".
Yes -- unfortunately something has to give: either we confuse new users with two names that sound similar but do something different, or we confuse former xm users by renaming their function, or we confuse current xl users by renaming their function. There's badness to be had whichever one we choose. I think renaming the xm function is the least bad of all the options.
Quoted 2 lines
OTOH I don't care giving it another name, as long as the functionality is available.
OK -- what about "usb-available-list"? Any objections / alternate suggestions from anyone?
-George
Re: [PATCH V3 4/6] xl: add pvusb commands
Quoted 10 lines · George Dunlap
Quoted 1 line · Juergen Gross
There might be existing users who know about "xm usb-assignable-list".
Yes -- unfortunately something has to give: either we confuse new users with two names that sound similar but do something different, or we confuse former xm users by renaming their function, or we confuse current xl users by renaming their function. There's badness to be had whichever one we choose. I think renaming the xm function is the least bad of all the options.
Quoted 5 lines
Quoted 2 lines
OTOH I don't care giving it another name, as long as the functionality is available.
OK -- what about "usb-available-list"? Any objections / alternate suggestions from anyone?
Hmm, just another idea:
xl usb-list -a
could list all domains with assigned USB-devices and the currently not assigned devices as well. This would avoid the need for another command name. It would even be possible to omit the "-a".
Juergen
Re: [PATCH V3 4/6] xl: add pvusb commands
Quoted 17 lines · Juergen Gross
Quoted 10 lines · George Dunlap
Quoted 1 line · Juergen Gross
There might be existing users who know about "xm usb-assignable-list".
Yes -- unfortunately something has to give: either we confuse new users with two names that sound similar but do something different, or we confuse former xm users by renaming their function, or we confuse current xl users by renaming their function. There's badness to be had whichever one we choose. I think renaming the xm function is the least bad of all the options.
Quoted 5 lines
Quoted 2 lines
OTOH I don't care giving it another name, as long as the functionality is available.
OK -- what about "usb-available-list"? Any objections / alternate suggestions from anyone?
Quoted 7 lines
Hmm, just another idea:
xl usb-list -a
could list all domains with assigned USB-devices and the currently not assigned devices as well. This would avoid the need for another command name. It would even be possible to omit the "-a".
That works for me too, I guess.
Can I suggest, though, that work on that functionality be detached from getting the core pvusb functionality in? I can't really effectively do the qemu side until it is in due to the cost of rebasing, and I would really like to have both in for 4.6 if possible.
-George
Re: [PATCH V3 4/6] xl: add pvusb commands
Quoted 26 lines · George Dunlap
Quoted 17 lines · Juergen Gross
Quoted 10 lines · George Dunlap
Quoted 1 line · Juergen Gross
There might be existing users who know about "xm usb-assignable-list".
Yes -- unfortunately something has to give: either we confuse new users with two names that sound similar but do something different, or we confuse former xm users by renaming their function, or we confuse current xl users by renaming their function. There's badness to be had whichever one we choose. I think renaming the xm function is the least bad of all the options.
Quoted 5 lines
Quoted 2 lines
OTOH I don't care giving it another name, as long as the functionality is available.
OK -- what about "usb-available-list"? Any objections / alternate suggestions from anyone?
Quoted 7 lines
Hmm, just another idea:
xl usb-list -a
could list all domains with assigned USB-devices and the currently not assigned devices as well. This would avoid the need for another command name. It would even be possible to omit the "-a".
Quoted 6 lines
That works for me too, I guess.
Can I suggest, though, that work on that functionality be detached from getting the core pvusb functionality in? I can't really effectively do the qemu side until it is in due to the cost of rebasing, and I would really like to have both in for 4.6 if possible.
Sure.
I hope to have my pvusb backend in qemu ready until then, too. :-) In case the performance isn't too bad I'll have to make some changes to the libxl part as well, but this will result in some deletions only (qemu will do driver unbinding, this will no longer be required to be done by libxl).
Juergen
Re: [PATCH V3 4/6] xl: add pvusb commands
Quoted 34 lines · Juergen Gross
Quoted 26 lines · George Dunlap
Quoted 17 lines · Juergen Gross
Quoted 10 lines · George Dunlap
Quoted 1 line · Juergen Gross
There might be existing users who know about "xm usb-assignable-list".
Yes -- unfortunately something has to give: either we confuse new users with two names that sound similar but do something different, or we confuse former xm users by renaming their function, or we confuse current xl users by renaming their function. There's badness to be had whichever one we choose. I think renaming the xm function is the least bad of all the options.
Quoted 5 lines
Quoted 2 lines
OTOH I don't care giving it another name, as long as the functionality is available.
OK -- what about "usb-available-list"? Any objections / alternate suggestions from anyone?
Quoted 7 lines
Hmm, just another idea:
xl usb-list -a
could list all domains with assigned USB-devices and the currently not assigned devices as well. This would avoid the need for another command name. It would even be possible to omit the "-a".
Quoted 6 lines
That works for me too, I guess.
Can I suggest, though, that work on that functionality be detached from getting the core pvusb functionality in? I can't really effectively do the qemu side until it is in due to the cost of rebasing, and I would really like to have both in for 4.6 if possible.
Quoted 7 lines
Sure.
I hope to have my pvusb backend in qemu ready until then, too. :-) In case the performance isn't too bad I'll have to make some changes to the libxl part as well, but this will result in some deletions only (qemu will do driver unbinding, this will no longer be required to be done by libxl).
Right -- just like it's not necessary when qemu does emulated device passthrough. :-)
Out of curiosity, what's the motivation for doing pvusb in qemu? Will there be an option, for instance, to switch usb devices from emulated over to pv when the magic port is written to (as is done with disks & network currently)?
-George
Re: [PATCH V3 4/6] xl: add pvusb commands
Quoted 43 lines · George Dunlap
Quoted 34 lines · Juergen Gross
Quoted 26 lines · George Dunlap
Quoted 17 lines · Juergen Gross
Quoted 10 lines · George Dunlap
Quoted 1 line · Juergen Gross
There might be existing users who know about "xm usb-assignable-list".
Yes -- unfortunately something has to give: either we confuse new users with two names that sound similar but do something different, or we confuse former xm users by renaming their function, or we confuse current xl users by renaming their function. There's badness to be had whichever one we choose. I think renaming the xm function is the least bad of all the options.
Quoted 5 lines
Quoted 2 lines
OTOH I don't care giving it another name, as long as the functionality is available.
OK -- what about "usb-available-list"? Any objections / alternate suggestions from anyone?
Quoted 7 lines
Hmm, just another idea:
xl usb-list -a
could list all domains with assigned USB-devices and the currently not assigned devices as well. This would avoid the need for another command name. It would even be possible to omit the "-a".
Quoted 6 lines
That works for me too, I guess.
Can I suggest, though, that work on that functionality be detached from getting the core pvusb functionality in? I can't really effectively do the qemu side until it is in due to the cost of rebasing, and I would really like to have both in for 4.6 if possible.
Quoted 7 lines
Sure.
I hope to have my pvusb backend in qemu ready until then, too. :-) In case the performance isn't too bad I'll have to make some changes to the libxl part as well, but this will result in some deletions only (qemu will do driver unbinding, this will no longer be required to be done by libxl).
Quoted 7 lines
Right -- just like it's not necessary when qemu does emulated device passthrough. :-)
Out of curiosity, what's the motivation for doing pvusb in qemu? Will there be an option, for instance, to switch usb devices from emulated over to pv when the magic port is written to (as is done with disks & network currently)?
Doing it in qemu was a request when I sent my patches to do it in the kernel. :-)
Doing pvusb in qemu really has advantages. Just by using a newer Xen you'll have pvusb backend support in Dom0. No need for a kernel update.
Up to now I have no plan doing a switch of emulated USB devices to pv. TBH I don't think this is an important scenario. In case someone wants to add support for it I won't object. :-)
Juergen
Re: [PATCH V3 4/6] xl: add pvusb commands
In reply to Chunyan LiuQuoted 16 lines · Chunyan Liu
Add 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 usb-ctrl-attach
test_vmversion=1 num_ports=8#xl usb-list
test_vm
will show the usb controllers and port usage under the domain.#xl usb-attach
test_vm1.6
will find the first usable controller:port, and attach usb device whose bus address is 1.6 (busnum is 1, devnum is 6) to it. One could also specify which <controller> and which <port>.
Quoted 8 lines
#xl usb-detach
test_vm1.6#xl usb-ctrl-detach
test_vmdev_id
will destroy the controller with specifieddev_id.Dev_idcan be traced in usb-list info.Signed-off-by Chunyan Liu <[email protected]>Signed-off-by Simon Cao <[email protected]>
Thanks, Chunyan. The interface looks good to me, and a skim through the code doesn't turn up any obvious problems. Someone more familiar with the libxl coding conventions will have to give it a stricter review.
I'll wait on the ack until we've discussed Juergen's point.
-George
Re: [PATCH V3 4/6] xl: add pvusb commands
Quoted 23 lines · George Dunlap
Quoted 22 lines · Chunyan Liu
Add 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 usb-ctrl-attach
test_vmversion=1 num_ports=8#xl usb-list
test_vm
will show the usb controllers and port usage under the domain.#xl usb-attach
test_vm1.6
will find the first usable controller:port, and attach usb device whose bus address is 1.6 (busnum is 1, devnum is 6) to it. One could also specify which <controller> and which <port>.#xl usb-detach
test_vm1.6#xl usb-ctrl-detach
test_vmdev_id
will destroy the controller with specifieddev_id.Dev_idcan be traced in usb-list info.
Quoted 7 lines
Quoted 2 lines
Signed-off-by Chunyan Liu <[email protected]>Signed-off-by Simon Cao <[email protected]>Thanks, Chunyan. The interface looks good to me, and a skim through the code doesn't turn up any obvious problems. Someone more familiar with the libxl coding conventions will have to give it a stricter review.
Actually, sorry to take this back. I looked at what I wrote in v2 of the series, and I think what I suggested there was better. :-)
I pointed out that most devices -- disk, vif, pci, &c -- take the same thing in their command-line arguments as you put in the config file. They have parse_disk_config(), parse_vif_config(), and xlu_parse_bdf(), each of which are called from both parse_config_data() and the respective add commands.
-George
Re: [PATCH V3 4/6] xl: add pvusb commands
In reply to Chunyan LiuQuoted 7 lines · Chunyan Liu
Add 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 usb-ctrl-attach
test_vmversion=1 num_ports=8
One more nit: this shuold be "ports=8", not "num_ports=8".
(It looks like it's only wrong here in the description; it's consistently "ports" throughout the rest of this patch and the next one.)
-George
PATCH v3 5/6 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', ] usb=['2.1,controller=0,port=1', ]
---
Changes to v2:
*add 'usbctrls' to domain_config
*add 'usbctrl' to xl.cfg to allow user specify USB controllers in config file *add documentation
docs/man/xl.cfg.pod.5 | 70 ++++++++++++++++++++++++++++++++++++ tools/libxl/libxl_create.c | 73 ++++++++++++++++++++++++++++++++++++-- tools/libxl/xl_cmdimpl.c | 88 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 227 insertions(+), 4 deletions(-)
docs/man/xl.cfg.pod.5 +70 -0
@@ -645,6 +645,76 @@ assigned slave device. =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<version=VERSION> + +Specifies version of the USB controller, could be 1 (USB1.1) or 2 (USB2.0). +Default is 2 (USB2.0). + +=item B<ports=PORTS> + +Specifies port number of the USB controller. Default is 8. + +Each USB controller will have an index starting from 0. On the same +controller, each port will have an index starting from 1. + +E.g. +usbctrl=["version=1,ports=4", "version=2,port2=8",] +The first controller has: +controller index = 0, and port 1,2,3,4. +The second controller has: +controller index = 1, and port 1,2,3,4,5,6,7,8. + +=back + +=back + +=item B<usb=[ "USB_SPEC_STRING", "USB_SPEC_STRING", ... ]> + +Specifies the host USB devices to passthrough to this guest. Each +B<USB_SPEC_STRING> has the form C<bus.addr,KEY=VALUE,KEY=VALUE,...> where: + +=over 4 + +=item B<bus.addr> + +Identifies the busnum.devnum of the USB device from the host perspective. +This is the same scheme as used in the output of C<lsusb> for the device in +question. + +=item B<KEY=VALUE> + +Possible B<KEY>s are: + +=over 4 + +=item B<controller=CONTROLLER> + +Specifies USB controller index, to which controller the USB device is attached. + +=item B<port=PORT> + +Specifies USB port index, to which port the USB device is attached. B<port=PORT> +is valid only when B<controller=CONTROLLER> is specified. Without +B<controller=CONTROLLER>, it will find the first available USB controller:port +and use it. If there is no controller at all, it will create one. + +=back + +=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/xl_cmdimpl.c +87 -1
100 lines not on this page · raw message
Signature · 1 line
1.8.5.2
PATCH v3 6/6 refactor codes to unify pvusb and qemu emulated usb
In reply to Chunyan LiuNow we have pvusb implementation. To merge with future qemu emulated usb work, refactor codes:
- define 'protocol' (type) to indicate pvusb or qemu, add 'protocol' to
usb controller and usb device structures, add 'type' to xl interface usb-attach|detach.
- extract common codes for both qemu and pvusb from libxl_pvusb.c tolibxl_usb.c, and adjust libxl_pvusb.c codes accordingly.
--- tools/libxl/Makefile | 2 +- tools/libxl/libxl_internal.h | 4 + tools/libxl/libxl_pvusb.c | 169 +------------------------------- tools/libxl/libxl_types.idl | 11 +++ tools/libxl/libxl_usb.c | 224 +++++++++++++++++++++++++++++++++++++++++++ tools/libxl/xl_cmdimpl.c | 54 +++++++++-- tools/libxl/xl_cmdtable.c | 4 +- 7 files changed, 293 insertions(+), 175 deletions(-) create mode 100644 tools/libxl/libxl_usb.c
tools/libxl/Makefile +1 -1
@@ -95,7 +95,7 @@ LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o libxl_pci.o \ libxl_internal.o libxl_utils.o libxl_uuid.o \ libxl_json.o libxl_aoutils.o libxl_numa.o libxl_vnuma.o \ libxl_save_callout.o _libxl_save_msgs_callout.o \ - libxl_qmp.o libxl_event.o libxl_fork.o libxl_pvusb.o $(LIBXL_OBJS-y) + libxl_qmp.o libxl_event.o libxl_fork.o libxl_pvusb.o libxl_usb.o $(LIBXL_OBJS-y) LIBXL_OBJS += libxl_genid.o LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o
tools/libxl/libxl_internal.h +4 -0
@@ -2428,6 +2428,10 @@ _hidden int libxl__device_usbctrl_add(libxl__gc *gc, uint32_t domid, _hidden int libxl__device_usb_add(libxl__gc *gc, uint32_t domid,libxl_device_usb *usb); _hidden int libxl__device_usb_destroy_all(libxl__gc *gc, uint32_t domid); +_hidden bool pv_is_usb_assigned(libxl__gc *gc, libxl_device_usb *usb); +_hidden int pv_usb_add(libxl__gc *gc, uint32_t domid, libxl_device_usb *usb); +_hidden int pv_usb_remove(libxl__gc *gc, uint32_t domid, libxl_device_usb *usb); +_hidden int pv_usb_destroy_all(libxl__gc *gc, uint32_t domid);/* Internal function to connect a vkb device */ _hidden int libxl__device_vkb_add(libxl__gc *gc, uint32_t domid,
tools/libxl/libxl_pvusb.c +5 -164
237 lines not on this page · raw message
tools/libxl/libxl_types.idl +11 -0
@@ -533,7 +533,15 @@ libxl_device_pci = Struct("device_pci", [ ("seize", bool), ]) +libxl_usb_protocol = Enumeration("usb_protocol", [ + (0, "AUTO"), + (1, "PV"), + (2, "DEVICEMODEL"), + ], init_val = "LIBXL_USB_PROTOCOL_PV") + + libxl_device_usbctrl = Struct("device_usbctrl", [ + ("protocol", libxl_usb_protocol), ("devid", libxl_devid), ("version", integer), ("ports", integer),
@@ -542,6 +550,7 @@ libxl_device_usbctrl = Struct("device_usbctrl", [ ]) libxl_device_usb = Struct("device_usb", [ + ("protocol", libxl_usb_protocol), ("ctrl", libxl_devid), ("port", integer), ("busid", string),
@@ -626,6 +635,7 @@ libxl_vtpminfo = Struct("vtpminfo", [ ], dir=DIR_OUT) libxl_usbctrlinfo = Struct("usbctrlinfo", [ + ("protocol", libxl_usb_protocol), ("devid", libxl_devid), ("version", integer), ("ports", integer),
@@ -640,6 +650,7 @@ libxl_usbctrlinfo = Struct("usbctrlinfo", [ ], dir=DIR_OUT) libxl_usbinfo = Struct("usbinfo", [ + ("protocol", libxl_usb_protocol), ("busnum", integer), ("devnum", integer), ("idVendor", integer),
tools/libxl/libxl_usb.c new file +224 -0
224 lines not on this page · raw message
tools/libxl/xl_cmdimpl.c +46 -8
100 lines not on this page · raw message
tools/libxl/xl_cmdtable.c +2 -2
@@ -557,12 +557,12 @@ struct cmd_spec cmd_table[] = { {"usb-attach" , &main_usbattach,1 ,2 ,"Attach a USB device to a domain" , -"<Domain> <bus.addr> [controller=<DevId> [port=<port>]]" , +"<Domain> <bus.addr> [type=pv|qemu] [controller=<DevId> [port=<port>]]" , }, { "usb-detach", &main_usbdetach,0 ,1 ,"Detach a USB device from a domain" , -"<Domain> <bus.addr>" , +"<Domain> <bus.addr> [type=pv|qemu]" , }, { "usb-list", &main_usblist, 0, 0,
Signature · 1 line
1.8.5.2
Re: [PATCH V3 6/6] refactor codes to unify pvusb and qemu emulated usb
Quoted 9 lines · Chunyan Liu
Now we have pvusb implementation. To merge with future qemu emulated usb work, refactor codes:
- define 'protocol' (type) to indicate pvusb or qemu, add 'protocol' to
usb controller and usb device structures, add 'type' to xl interface usb-attach|detach.
- extract common codes for both qemu and pvusb fromlibxl_pvusb.ctolibxl_usb.c, and adjustlibxl_pvusb.ccodes accordingly.Signed-off-by Chunyan Liu <[email protected]>
Thank you for doing this, Chunyan.
A couple of things.
First, I'm not really a fan of having a patch in a series go through and rearrange things like this; it seems like it would be better to fold these changes into patches 3-5 of this series.
Second, I've got a number of comments on the "refactoring" bit -- what you move into libxl_usb.c and how. But the refactoring bit isn't really for your feature, and I'll probably have to adjust it anyway when I actually go to add the qemu side; it seems a bit unfair and a bit inefficient to ask you to redo the refactoring at this point.
On the other hand, I don't like the idea of checking in an interface that I know I don't want set in stone, particularly with less than 2 months left until the code freeze.
So what about this: Take the interface-related changes from this patch (with my comments) and move them back to the earlier patches in the series; and then just put the minimum amount of code in those patches to handle them. The interface is an enum, so if there's only one option, you should be able to assume that protocol==PV; and it should be OK internally to assume that all controllers and devices are actually PV. Leave the file named "libxl_pvusb.c", but don't bother (yet) adding a libxl_usb.c. I'll do that when I add the HVM side of things.
What do you think?
Further comments below...
Quoted 53 lines
--- tools/libxl/Makefile | 2 +- tools/libxl/libxl_internal.h | 4 + tools/libxl/libxl_pvusb.c | 169 +------------------------------- tools/libxl/libxl_types.idl | 11 +++ tools/libxl/libxl_usb.c | 224 +++++++++++++++++++++++++++++++++++++++++++ tools/libxl/xl_cmdimpl.c | 54 +++++++++-- tools/libxl/xl_cmdtable.c | 4 +- 7 files changed, 293 insertions(+), 175 deletions(-) create mode 100644 tools/libxl/libxl_usb.ctools/libxl/Makefile +1
-1@@ -95,7 +95,7 @@ LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o libxl_pci.o \ libxl_internal.o libxl_utils.o libxl_uuid.o \ libxl_json.o libxl_aoutils.o libxl_numa.o libxl_vnuma.o \ libxl_save_callout.o _libxl_save_msgs_callout.o \ - libxl_qmp.o libxl_event.o libxl_fork.o libxl_pvusb.o $(LIBXL_OBJS-y) + libxl_qmp.o libxl_event.o libxl_fork.o libxl_pvusb.o libxl_usb.o $(LIBXL_OBJS-y) LIBXL_OBJS += libxl_genid.o LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.otools/libxl/libxl_internal.h +4
-0@@ -2428,6 +2428,10 @@ _hidden int libxl__device_usbctrl_add(libxl__gc *gc, uint32_t domid, _hidden int libxl__device_usb_add(libxl__gc *gc, uint32_t domid,libxl_device_usb *usb); _hidden int libxl__device_usb_destroy_all(libxl__gc *gc, uint32_t domid); +_hidden bool pv_is_usb_assigned(libxl__gc *gc, libxl_device_usb *usb); +_hidden int pv_usb_add(libxl__gc *gc, uint32_t domid, libxl_device_usb *usb); +_hidden int pv_usb_remove(libxl__gc *gc, uint32_t domid, libxl_device_usb *usb); +_hidden int pv_usb_destroy_all(libxl__gc *gc, uint32_t domid);/* Internal function to connect a vkb device */ _hidden int libxl__device_vkb_add(libxl__gc *gc, uint32_t domid,tools/libxl/libxl_pvusb.c +0
-2@@ -16,8 +16,6 @@#define USBBACK_INFO_PATH "/libxl/usbback" -#define USBHUB_CLASS_CODE 0x09 -static int libxl__device_usbctrl_setdefault (libxl__gc *gc,uint32_t domid,libxl_device_usbctrl *usbctrl) {
tools/libxl/libxl_pvusb.c int libxl_device_usbctrl_getinfo(libxl_ctx *ctx, uint32_t domid, · line 339
Quoted 4 lines
tools/libxl/libxl_pvusb.c +1
-0@@ -338,6 +336,7 @@ int libxl_device_usbctrl_getinfo(libxl_ctx *ctx, uint32_t domid, usbctrlinfo->devid = usbctrl->devid; usbctrlinfo->ports = usbctrl->ports; usbctrlinfo->version = usbctrl->version; + usbctrlinfo->protocol = usbctrl->protocol;
Simillar to my comment on patch 3/6: this should only read usbctrl->devid. Here you can assume protocol=PV for the time being.
tools/libxl/libxl_types.idl libxl_device_pci = Struct("device_pci", [ · line 540
Quoted 13 lines
tools/libxl/libxl_types.idl +5
-0@@ -533,7 +533,15 @@ libxl_device_pci = Struct("device_pci", [ ("seize", bool), ]) +libxl_usb_protocol = Enumeration("usb_protocol", [ + (0, "AUTO"), + (1, "PV"), + (2, "DEVICEMODEL"), + ], init_val = "LIBXL_USB_PROTOCOL_PV")
I think this should probably default to AUTO, and then have something which changes AUTO to PV unconditionally (which I'll refactor later).
Quoted 8 lines
tools/libxl/libxl_types.idl +3
-0+ + libxl_device_usbctrl = Struct("device_usbctrl", [ + ("protocol", libxl_usb_protocol), ("devid", libxl_devid), ("version", integer), ("ports", integer),
tools/libxl/libxl_types.idl libxl_device_usbctrl = Struct("device_usbctrl", [ · line 556
Quoted 7 lines
tools/libxl/libxl_types.idl +1
-0@@ -542,6 +550,7 @@ libxl_device_usbctrl = Struct("device_usbctrl", [ ]) libxl_device_usb = Struct("device_usb", [ + ("protocol", libxl_usb_protocol), ("ctrl", libxl_devid), ("port", integer), ("busid", string),
I don't think we need a protocol for usb -- just for usbctrl. I think the protocol for usb should be defined by the controller it's using.
Quoted 9 lines
tools/libxl/libxl_types.idl +1
-0@@ -626,6 +635,7 @@ libxl_vtpminfo = Struct("vtpminfo", [ ], dir=DIR_OUT) libxl_usbctrlinfo = Struct("usbctrlinfo", [ + ("protocol", libxl_usb_protocol), ("devid", libxl_devid), ("version", integer), ("ports", integer),
tools/libxl/libxl_types.idl libxl_usbctrlinfo = Struct("usbctrlinfo", [ · line 656
Quoted 7 lines
tools/libxl/libxl_types.idl +1
-0@@ -640,6 +650,7 @@ libxl_usbctrlinfo = Struct("usbctrlinfo", [ ], dir=DIR_OUT) libxl_usbinfo = Struct("usbinfo", [ + ("protocol", libxl_usb_protocol), ("busnum", integer), ("devnum", integer), ("idVendor", integer),
Same here -- I don't think we need the protocol for "usbinfo" if we have the controller id.
Quoted 8 lines
tools/libxl/xl_cmdimpl.c +0
-0@@ -3400,7 +3400,16 @@ int main_usbattach(int argc, char **argv) }while (argc > optind) {
tools/libxl/xl_cmdimpl.c int main_usbattach(int argc, char **argv) · line 3411
Quoted 10 lines
tools/libxl/xl_cmdimpl.c +9
-1-if (MATCH_OPTION ("controller" , argv[optind], oparg)) { +if (MATCH_OPTION ("type" , argv[optind], oparg)) { +if (!strcmp (oparg,"pv" )) { + usb.protocol =LIBXL_USB_PROTOCOL_PV ; + }else if (!strcmp (oparg,"qemu" )) { + usb.protocol =LIBXL_USB_PROTOCOL_DEVICEMODEL ; + }else { +fprintf (stderr,"unrecognized type `%s'\n" , oparg); +exit (-1 ); + }
We want to move this from main_usbattach to main_usbctrl_attach. No need to specify the type on detach -- libxl should be able to look that up.
And here you want to use libxl_usb_protocol_from_string(), which is generated automatically by the idl.
tools/libxl/xl_cmdimpl.c int main_usbdetach(int argc, char **argv) · line 3506
Quoted 11 lines
tools/libxl/xl_cmdimpl.c +11
-0+static const char *get_usb_protocol_string (libxl_usb_protocol type) +{ +switch (type) { +case LIBXL_USB_PROTOCOL_PV : +return "pv" ; +case LIBXL_USB_PROTOCOL_DEVICEMODEL : +return "qemu" ; +default : +return "invalid" ; + } +}
And you don't need this, because the IDL will automatically generate libxl_usb_protocol_to_string()...
tools/libxl/xl_cmdimpl.c int main_usblist(int argc, char **argv) · line 3544
Quoted 9 lines
if (!libxl_device_usbctrl_getinfo(ctx, domid,
&usbctrls[i], &usbctrlinfo)) {tools/libxl/xl_cmdimpl.c +2
-1-printf ("%-6d %-3d %-5d %-7d %-5d %-30s\n" , +printf ("%-6d %-6s %-3d %-5d %-7d %-5d %-30s\n" , usbctrlinfo.devid, +get_usb_protocol_string (usbctrlinfo.protocol), usbctrlinfo.backend_id, usbctrlinfo.state, usbctrlinfo.version, usbctrlinfo.ports, usbctrlinfo.backend);
...which you can use here.
tools/libxl/xl_cmdimpl.c int main_usblist(int argc, char **argv) · line 3557
Quoted 7 lines
tools/libxl/xl_cmdimpl.c +3
-0@@ -3517,6 +3552,9 @@ int main_usblist(int argc, char **argv) }free (usbctrls); + +/* TODO list qemu usb device info*/ +
No need for this.
tools/libxl/xl_cmdtable.c struct cmd_spec cmd_table[] = { · line 560
Quoted 13 lines
tools/libxl/xl_cmdimpl.c +0
-0return 0 ; }tools/libxl/xl_cmdtable.c +1
-1@@ -557,12 +557,12 @@ struct cmd_spec cmd_table[] = { {"usb-attach" , &main_usbattach,1 ,2 ,"Attach a USB device to a domain" , - "<Domain> <bus.addr> [controller=<DevId> [port=<port>]]", + "<Domain> <bus.addr> [type=pv|qemu] [controller=<DevId> [port=<port>]]",
I think for now you should just leave this as "pv". Only advertise types that are actually supported.
-George