libxl: Introduce functions to add and remove USB devices to an HVM guest
PATCH v5 1/2 libxl: Introduce functions to add and remove USB devices to an HVM guest
This patch exposes a generic interface which can be expanded in the future to implement USB for PVUSB, qemu, and stubdoms. It can also be extended to include other types of USB other than host USB (for example, tablets, mice, or keyboards).
For each device removed or added, one of two protocols is available:
* PVUSB
* qemu (DEVICEMODEL)
The caller can additionally specify "AUTO", in which case the library will try to determine the best protocol automatically.
At the moment, the only protocol implemented is DEVICEMODEL, and the only device type impelmented is HOSTDEV.
This uses the qmp functionality, and is thus only available for qemu-xen, not qemu-traditional.
History:
v5:
- fix erroneous use of NOGC in qmp functions
- Don't check return of libxl__sprintf in libxl_create.c
- Check return values of new xs actions in libxl_create.c
- Use updated template for xenstore transactions, do checks
- use xs_read_checked
- Fixed if (x) spacing to match coding style
- use GCSFPRINTF
- use libxl__calloc
- Fix comment for LIBXL_HAVE_USB
- use idl-generated *_from_string() and *_to_string() functions
--- tools/libxl/Makefile | 2 +- tools/libxl/libxl.h | 37 +++ tools/libxl/libxl_create.c | 11 +- tools/libxl/libxl_internal.h | 18 ++ tools/libxl/libxl_qmp.c | 66 +++++ tools/libxl/libxl_types.idl | 22 ++ tools/libxl/libxl_usb.c | 522 ++++++++++++++++++++++++++++++++++++++++ tools/ocaml/libs/xl/genwrap.py | 1 + 8 files changed, 677 insertions(+), 2 deletions(-) create mode 100644 tools/libxl/libxl_usb.c
tools/libxl/Makefile +1 -1
@@ -74,7 +74,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_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_usb.o $(LIBXL_OBJS-y) LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o $(LIBXL_OBJS): CFLAGS += $(CFLAGS_LIBXL) -include $(XEN_ROOT)/tools/config.h
tools/libxl/libxl.h +37 -0
@@ -75,6 +75,12 @@#define LIBXL_HAVE_FIRMWARE_PASSTHROUGH 1/* +* LIBXL_HAVE_USB indicates the functions for doing hot-plug of +* USB devices. +*/ +#define LIBXL_HAVE_USB 1 + +/* * libxl ABI compatibility * * The only guarantee which libxl makes regarding ABI compatibility
@@ -735,6 +741,37 @@ 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 +* +* For each device removed or added, one of these protocols is available: +* - PV (i.e., PVUSB) +* - DEVICEMODEL (i.e, qemu) +* +* PV is available for either PV or HVM domains. DEVICEMODEL is only +* available for HVM domains. The caller can additionally specify +* "AUTO", in which case the library will try to determine the best +* protocol automatically. +* +* At the moment, the only protocol implemented is DEVICEMODEL, and the only +* device type impelmented is HOSTDEV. +* +* This uses the qmp functionality, and is thus only available for +* qemu-xen, not qemu-traditional. +*/ +#define LIBXL_DEVICE_USB_BACKEND_DEFAULT (-1) +int libxl_device_usb_add(libxl_ctx *ctx, uint32_t domid, + libxl_device_usb *dev, + const libxl_asyncop_how *ao_how) + LIBXL_EXTERNAL_CALLERS_ONLY; +int libxl_device_usb_remove(libxl_ctx *ctx, uint32_t domid, + libxl_device_usb *dev, + const libxl_asyncop_how *ao_how) + LIBXL_EXTERNAL_CALLERS_ONLY; +libxl_device_usb *libxl_device_usb_list (libxl_ctx *ctx,uint32_t domid, +int *num) +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_create.c +10 -1
@@ -393,7 +393,7 @@ int libxl__domain_make(libxl__gc *gc, libxl_domain_create_info *info,libxl_ctx *ctx =libxl__gc_owner (gc);int flags, ret, rc, nb_vm;char *uuid_string; -char *dom_path, *vm_path, *libxl_path; +char *dom_path, *vm_path, *libxl_path, *libxl_usb_path;struct xs_permissions roperm[2 ];struct xs_permissions rwperm[1 ];struct xs_permissions noperm[1 ];
@@ -454,6 +454,8 @@ int libxl__domain_make(libxl__gc *gc, libxl_domain_create_info *info, goto out; } + libxl_usb_path =GCSPRINTF ("%s/usb" , libxl_path); + noperm[0 ].id =0 ; noperm[0 ].perms =XS_PERM_NONE ;
@@ -477,6 +479,13 @@ retry_transaction:xs_rm (ctx->xsh, t, libxl_path);libxl__xs_mkdir (gc, t, libxl_path, noperm,ARRAY_SIZE (noperm)); +/* Helpfully, libxl__xs_rm_checked() returns 0 on success... */ + rc =libxl__xs_rm_checked (gc, t, libxl_usb_path); +if (rc) goto out; +/* ...but libxl__xs_mkdir() returns 1 on success, 0 on failure. */ + rc =libxl__xs_mkdir (gc, t, libxl_usb_path, noperm,ARRAY_SIZE (noperm)); +if (!rc) goto out; +xs_write (ctx->xsh, t,libxl__sprintf (gc,"%s/vm" , dom_path), vm_path,strlen (vm_path)); rc =libxl__domain_rename (gc, *domid,0 , info->name, t);if (rc)
tools/libxl/libxl_internal.h +18 -0
@@ -1412,6 +1412,24 @@ _hidden int libxl__qmp_save(libxl__gc *gc, int domid, const char *filename);/* Set dirty bitmap logging status */ _hidden int libxl__qmp_set_global_dirty_log(libxl__gc *gc, int domid, bool enable); _hidden int libxl__qmp_insert_cdrom(libxl__gc *gc, int domid, const libxl_device_disk *disk); +/* Same as normal, but "translated" */ +typedef struct libxl__device_usb { +libxl_usb_protocol protocol; +libxl_domid target_domid; +libxl_domid backend_domid; +libxl_domid dm_domid; +libxl_device_usb_type type; +union { +struct { +int hostbus; +int hostaddr; + } hostdev; + } u; +}libxl__device_usb ; +_hidden int libxl__qmp_usb_add(libxl__gc *gc, int domid, +libxl__device_usb *dev); +_hidden int libxl__qmp_usb_remove(libxl__gc *gc, int domid, +libxl__device_usb *dev);/* close and free the QMP handler */ _hidden void libxl__qmp_close(libxl__qmp_handler *qmp);/* remove the socket file, if the file has already been removed,
tools/libxl/libxl_qmp.c +66 -0
78 lines not on this page · raw message
tools/libxl/libxl_types.idl +22 -0
@@ -408,6 +408,28 @@ libxl_device_vtpm = Struct("device_vtpm", [ ("uuid", libxl_uuid), ]) +libxl_device_usb_protocol = Enumeration("usb_protocol", [ + (0, "AUTO"), + (1, "PV"), + (2, "DEVICEMODEL"), + ]) + +libxl_device_usb_type = Enumeration("device_usb_type", [ + (1, "HOSTDEV"), + ]) + +libxl_device_usb = Struct("device_usb", [ + ("protocol", libxl_device_usb_protocol, + {'init_val': 'LIBXL_USB_PROTOCOL_AUTO'}), + ("backend_domid", libxl_domid, + {'init_val': 'LIBXL_DEVICE_USB_BACKEND_DEFAULT'}), + ("u", KeyedUnion(None, libxl_device_usb_type, "type", + [("hostdev", Struct(None, [ + ("hostbus", integer), + ("hostaddr", integer) ])) + ])) + ]) + libxl_domain_config = Struct("domain_config", [ ("c_info", libxl_domain_create_info), ("b_info", libxl_domain_build_info),
tools/libxl/libxl_usb.c new file +522 -0
522 lines not on this page · raw message
tools/ocaml/libs/xl/genwrap.py +1 -0
@@ -286,6 +286,7 @@ if __name__ == '__main__': "domain_config", "vcpuinfo", "event", + "device_usb" ] for t in blacklist:
Signature · 1 line
1.7.9.5
PATCH v5 2/2 xl: Add commands for usb hot-plug
v5:
- Remove extraneous is_hex
- Replace is_dec with ctype.h isdigit()
- Replace domid -1 with INVALID_DOMID
- Fix up a couple of mistaken function names in strings
- Copy CTYPE macro from libxl_internal.h
- Fix to usb-list manpage (use -d domid)
--- docs/man/xl.pod.1 | 30 +++++++ tools/libxl/xl.h | 21 +++++ tools/libxl/xl_cmdimpl.c | 214 +++++++++++++++++++++++++++++++++++++++++++++ tools/libxl/xl_cmdtable.c | 15 ++++ 4 files changed, 280 insertions(+)
docs/man/xl.pod.1 +30 -0
@@ -1147,6 +1147,36 @@ List virtual network interfaces for a domain. =back +=head2 USB DEVICES + +=over 4 + +=item B<usb-add> I<-d domain-id> I<-v hosbus.hostaddr> + +Passes through the host USB device specified by I<hostbus.hostaddr>. At +the moment this will only work for HVM domains via qemu. + +The best way to find out the information for the device is typically using +lsusb. + +This command is only available for domains using qemu-xen, not +qemu-traditional. + +=item B<usb-remove> I<-d domain-id> I<-v hosbus.hostaddr> + +Remove the host USB device from I<domain-id> which is specified +by <hostbus.hostaddr>. This command only works for devices added +with usb-add; not for those specified in the config file. + +This command is only available for domains using qemu-xen, not +qemu-traditional. + +=item B<usb-list> I<-d domain-id> + +Show host USB devices assigned to the guest. + +=back + =head2 VTPM DEVICES =over 4
tools/libxl/xl.h +21 -0
@@ -35,6 +35,9 @@ int main_info(int argc, char **argv);int main_sharing (int argc,char **argv);int main_cd_eject (int argc,char **argv);int main_cd_insert (int argc,char **argv); +int main_usb_add (int argc,char **argv); +int main_usb_remove (int argc,char **argv); +int main_usb_list (int argc,char **argv);int main_console (int argc,char **argv);int main_vncviewer (int argc,char **argv);int main_pcilist (int argc,char **argv);
@@ -164,6 +167,24 @@ extern void printf_info_sexp(int domid, libxl_domain_config *d_config);#define XL_GLOBAL_CONFIG XEN_CONFIG_DIR "/xl.conf"#define XL_LOCK_FILE XEN_LOCK_DIR "/xl" +/* +* int CTYPE(ISFOO, char c); +* int CTYPE(toupper, char c); +* int CTYPE(tolower, char c); +* +* This is necessary because passing a simple char to a ctype.h +* is forbidden. ctype.h macros take ints derived from _unsigned_ chars. +* +* If you have a char which might be EOF then you should already have +* it in an int representing an unsigned char, and you can use the +* <ctype.h> macros directly. This generally happens only with values +* from fgetc et al. +* +* For any value known to be a character (eg, anything that came from +* a char[]), use CTYPE. +*/ +#define CTYPE (isfoo,c) (isfoo((unsigned char)(c))) +#endif /* XL_H */ /*
tools/libxl/xl_cmdimpl.c +214 -0
220 lines not on this page · raw message
tools/libxl/xl_cmdtable.c +15 -0
@@ -187,6 +187,21 @@ struct cmd_spec cmd_table[] = {"Eject a cdrom from a guest's cd drive" ,"<Domain> <VirtualDevice>" , }, + { "usb-add", + &main_usb_add,1 ,1 , +"Hot-plug a usb device to a domain." , +"-d <Domain> [-v <hostbus.hostaddr>]" , + }, + { "usb-remove", + &main_usb_remove,1 ,1 , +"Hot-unplug a usb device from a domain." , +"-d <Domain> [-v <hostbus.hostaddr>]" , + }, + { "usb-list", + &main_usb_list,0 ,0 , +"List usb devices for a domain" , +"-d <Domain>" , + }, { "mem-max", &main_memmax,0 ,1 , "Set the maximum amount reservation for a domain",
Signature · 1 line
1.7.9.5
Re: [PATCH v5 2/2] xl: Add commands for usb hot-plug
George Dunlap writes ("[PATCH v5 2/2] xl: Add commands for usb hot-plug"):
Quoted 2 lines
+=item B<usb-add> I<-d domain-id> I<-v hosbus.hostaddr> +
Shouldn't these be more like `xl {disk,network}-{attach,detach}' ?
I was imagining something like
xl usb-attach debian.guest.osstest host:046d:c014
? (Perhaps with
xl usb-remove --protocol hvm debian.guest.osstest tablet
coming later at some point.)
Quoted 16 lines
tools/libxl/xl_cmdimpl.c +16
-0+static int parse_usb_hostdev_specifier (libxl_device_usb *dev,const char *s) +{ +const char * hostbus, *hostaddr, *p; + + hostbus = s; + hostaddr=NULL ; + +/* Match [0-9]+\.[0-9] */ +if (!CTYPE (isdigit,*hostbus)) +return -1 ; + +for (p=s; *p; p++) { +if (*p =='.' ) { +if ( !hostaddr ) + hostaddr = p+1 ; +else {
tools/libxl/xl_cmdimpl.c int main_cd_insert(int argc, char **argv) · line 2632
Quoted 10 lines
tools/libxl/xl_cmdimpl.c +10
-0+return -1 ; + } + }else if (!CTYPE (isdigit,*p)) { +return -1 ; + } + } +if (!hostaddr || !CTYPE (isdigit,*hostaddr)) +return -1 ; + dev->u.hostdev.hostbus =strtoul (hostbus,NULL ,10 ); + dev->u.hostdev.hostaddr =strtoul (hostaddr,NULL ,10 );
Perhaps it would be easier to use the 2nd argument to strtoul ? Or strchr ?
Also, some style quibbles: spaces should appear after the keyword in "for (" and "if (" and not inside the parens in "if (!hostaddr)". And it seems odd to write an if without braces for the one-line if branch and with braces for the one-line else branch. You have quite a lot of these...
tools/libxl/xl_cmdimpl.c int main_cd_insert(int argc, char **argv) · line 2638
Quoted 2 lines
tools/libxl/xl_cmdimpl.c +2
-0+static int usb_add (uint32_t domid,libxl_device_usb_type type, +const char * device)
^
This indent is odd.
tools/libxl/xl_cmdimpl.c int main_cd_insert(int argc, char **argv) · line 2661
Quoted 2 lines
tools/libxl/xl_cmdimpl.c +2
-0+if ( (rc =libxl_device_usb_add (ctx, domid, &usbdev,NULL )) <0 ) +fprintf (stderr,"libxl_device_usb_add failed.\n" );
Can't we have the call and the assignment to rc on its own line ?
Ian.
Re: [PATCH v5 2/2] xl: Add commands for usb hot-plug
Quoted 10 lines · Ian Jackson
George Dunlap writes ("[PATCH v5 2/2] xl: Add commands for usb hot-plug"):
Quoted 2 lines
+=item B<usb-add> I<-d domain-id> I<-v hosbus.hostaddr> +
Shouldn't these be more like `xl {disk,network}-{attach,detach}' ?
I was imagining something like
xl usb-attach debian.guest.osstest host:046d:c014
? (Perhaps withxl usb-remove --protocol hvm debian.guest.osstest tabletcoming later at some point.)
I'm not really a fan of the "use position to specify arguments" when there is more than one or two arguments; but that's what block/net/pci do, so we should probably follow suit.
Hmm, a bit strange that the libxl functions are "libxl_device_[foo]_add()", but the xl commands are "[foo]-attach". Ah well -- I'll change that around too.
One of the reasons I didn't do this was because I didn't want to have to implement a parser to figure out which kind of device to implement. But an advantage of doing that is that we could at some point move from the sort of hacky, qemu-specific way of specifying usb devices for HVM guests in the config file (which basically passes them on directly to the qemu command-line via a deprecated interface), and move to a unified way to specify USB devices for either PV or HVM (with, for example, hot-unplug of devices which were added at boot time).
Let me take a look and see if I can steal a parsing function from someone else. Any recommendations? :-)
Quoted 21 lines
Quoted 21 lines
tools/libxl/xl_cmdimpl.c +20
-0+static int parse_usb_hostdev_specifier (libxl_device_usb *dev,const char *s) +{ +const char * hostbus, *hostaddr, *p; + + hostbus = s; + hostaddr=NULL ; + +/* Match [0-9]+\.[0-9] */ +if (!CTYPE (isdigit,*hostbus)) +return -1 ; + +for (p=s; *p; p++) { +if (*p =='.' ) { +if ( !hostaddr ) + hostaddr = p+1 ; +else { +return -1 ; + } + }else if (!CTYPE (isdigit,*p)) { +return -1 ;
Quoted 8 lines
tools/libxl/xl_cmdimpl.c int main_cd_insert(int argc, char **argv) · line 2632
Quoted 6 lines
tools/libxl/xl_cmdimpl.c +6
-0+ } + } +if (!hostaddr || !CTYPE (isdigit,*hostaddr)) +return -1 ; + dev->u.hostdev.hostbus =strtoul (hostbus,NULL ,10 ); + dev->u.hostdev.hostaddr =strtoul (hostaddr,NULL ,10 );Perhaps it would be easier to use the 2nd argument to strtoul ? Or strchr ?
I'll see about that when looking at the parsing functions.
Quoted 6 lines
Also, some style quibbles: spaces should appear after the keyword in "for (" and "if (" and not inside the parens in "if (!hostaddr)". And it seems odd to write an if without braces for the one-line if branch and with braces for the one-line else branch. You have quite a lot of these...
Ack. Sorry, for some reason didn't go through and check for stylistic things in this patch.
Quoted 5 lines
tools/libxl/xl_cmdimpl.c int main_cd_insert(int argc, char **argv) · line 2638
Quoted 3 lines
tools/libxl/xl_cmdimpl.c +2
-0+static int usb_add (uint32_t domid,libxl_device_usb_type type, +const char * device)^This indent is odd.
Ack. Didn't re-indent after removing "hvm_host_" from the function name.
Quoted 4 lines
tools/libxl/xl_cmdimpl.c int main_cd_insert(int argc, char **argv) · line 2661
Quoted 3 lines
tools/libxl/xl_cmdimpl.c +2
-0+if ( (rc =libxl_device_usb_add (ctx, domid, &usbdev,NULL )) <0 ) +fprintf (stderr,"libxl_device_usb_add failed.\n" );Can't we have the call and the assignment to rc on its own line ?
If you like. :-)
-George
George Dunlap writes ("[PATCH v5 1/2] libxl: Introduce functions to add and remove USB devices to an HVM guest"):
tools/libxl/libxl_create.c retry_transaction: · line 487
Quoted 6 lines
tools/libxl/libxl_create.c +6
-0+/* Helpfully, libxl__xs_rm_checked() returns 0 on success... */ + rc =libxl__xs_rm_checked (gc, t, libxl_usb_path); +if (rc) goto out; +/* ...but libxl__xs_mkdir() returns 1 on success, 0 on failure. */ + rc =libxl__xs_mkdir (gc, t, libxl_usb_path, noperm,ARRAY_SIZE (noperm)); +if (!rc) goto out;
Is this really right ? If mkdir fails, you cause libxl__domain_make to immediately return success. The out label expects the error code to be in rc. I think it's a stylistic error to assign the (deprecated) boolean return value from libxl__xs_mkdir to rc; rc would normally contain a libxl error code.
tools/libxl/libxl_qmp.c int libxl__qmp_insert_cdrom(libxl__gc *gc, int domid, · line 941
Quoted 3 lines
tools/libxl/libxl_qmp.c +3
-0+ id =GCSPRINTF (HOST_USB_QDEV_ID , + (uint16_t ) dev->u.hostdev.hostbus, + (uint16_t ) dev->u.hostdev.hostaddr);
I think the style in libxl is to cuddle casts.
Quoted 1 line
+
switch(usbdev->type){
Missing space. (etc.) And some overly long lines.
Most of the rest of this looks plausible although I wish you wouldn't write things like this:
tools/libxl/libxl_usb.c line 98
Quoted 4 lines
tools/libxl/libxl_usb.c new file +4
-0+switch (usbdev->type) { +case LIBXL_DEVICE_USB_TYPE_HOSTDEV : +if ((rc =read_hostdev_xenstore_entry (gc, path, usbdev)) <0 ) + goto out;
I guess old habits are hard to break :-).
I haven't checked the semantics and timing of the xenstore/qmp protocol in detail. But let me ask a question:
Suppose attempted attach or remove of a usb device fails halfway through (eg, the user presses ^C and xl just stops since it doesn't handle SIGINT). Is the situation now (a) reported in the usb device listing (b) capable of being cleaned up with a remove operation (c) tidied up on domain destroy ? (Is this true at all times - ie does it never go through a state where things won't be cleaned up?)
Ian.
Quoted 7 lines · Ian Jackson
George Dunlap writes ("[PATCH v5 1/2] libxl: Introduce functions to add and remove USB devices to an HVM guest"):
tools/libxl/libxl_create.c retry_transaction: · line 487
Quoted 6 lines
tools/libxl/libxl_create.c +6
-0+/* Helpfully, libxl__xs_rm_checked() returns 0 on success... */ + rc =libxl__xs_rm_checked (gc, t, libxl_usb_path); +if (rc) goto out; +/* ...but libxl__xs_mkdir() returns 1 on success, 0 on failure. */ + rc =libxl__xs_mkdir (gc, t, libxl_usb_path, noperm,ARRAY_SIZE (noperm)); +if (!rc) goto out;
Quoted 5 lines
Is this really right ? If mkdir fails, you cause
libxl__domain_maketo immediately return success. The out label expects the error code to be in rc. I think it's a stylistic error to assign the (deprecated) boolean return value fromlibxl__xs_mkdirto rc; rc would normally contain a libxl error code.
Yes, you're right on both points. Good catch.
Quoted 5 lines
tools/libxl/libxl_qmp.c int libxl__qmp_insert_cdrom(libxl__gc *gc, int domid, · line 941
Quoted 4 lines
tools/libxl/libxl_qmp.c +3
-0+ id =GCSPRINTF (HOST_USB_QDEV_ID , + (uint16_t ) dev->u.hostdev.hostbus, + (uint16_t ) dev->u.hostdev.hostaddr);I think the style in libxl is to cuddle casts.
Ack. I've never thought of variables as cuddly before...
Quoted 3 lines
Quoted 2 lines
+
switch(usbdev->type){Missing space. (etc.) And some overly long lines.
Ack.
Quoted 9 lines
Most of the rest of this looks plausible although I wish you wouldn't write things like this:
tools/libxl/libxl_usb.c line 98
Quoted 4 lines
tools/libxl/libxl_usb.c new file +4
-0+switch (usbdev->type) { +case LIBXL_DEVICE_USB_TYPE_HOSTDEV : +if ((rc =read_hostdev_xenstore_entry (gc, path, usbdev)) <0 ) + goto out;I guess old habits are hard to break :-).
Well I prefer that way -- having the 'if' on a separate line actually annoys me (mildly). "Do this and handle the error if any" seems like one operation in my mind, not two. But I don't mind changing it.
Quoted 9 lines
I haven't checked the semantics and timing of the xenstore/qmp protocol in detail. But let me ask a question:
Suppose attempted attach or remove of a usb device fails halfway through (eg, the user presses ^C and xl just stops since it doesn't handle SIGINT). Is the situation now (a) reported in the usb device listing (b) capable of being cleaned up with a remove operation (c) tidied up on domain destroy ? (Is this true at all times - ie does it never go through a state where things won't be cleaned up?)
As far as I could determine, there is absolutely no way to query qemu to find out what devices are actually attached at the moment; that's the whole reason for storing stuff in xenstore in the first place. So yes -- if you kill the process after the qmp command has been issued but before writing something in xenstore, then usb-list will not show the device, usb-remove will return an error if you attempt to remove the device. usb-add will issue the qmp command, which will then fail.
I think this is probably true of most of the add/remove functions.
We could add an option that says "attempt to do the remove even if you don't see the device in xenstore" I suppose. But I'm not really sure how to document that in a useful way without basically describing the internal mechanics of the command.
There's nothing we can really do about usb-list though.
On the whole I think I'd rather just say, "Don't do that."
-George
Quoted 9 lines · George Dunlap
We could add an option that says "attempt to do the remove even if you
don't see the device in xenstore" I suppose. But I'm not really sure how to document that in a useful way without basically describing the internal mechanics of the command.There's nothing we can really do about usb-list though.
You could write it to xenstore before attempting the qmp commands. That just relies on the qmp remove failing gracefully for devices which aren't actually attached.
You might also be able to come up with some complicated, prepare, do it, commit in xenstore like approach -- but that's probably a) difficult (if not impossibly due to races) and b) unnecessary.
Ian.
Quoted 8 lines · Ian Campbell
Quoted 7 lines · George Dunlap
We could add an option that says "attempt to do the remove even if you
don't see the device in xenstore" I suppose. But I'm not really sure how to document that in a useful way without basically describing the internal mechanics of the command.
Quoted 4 lines
Quoted 1 line
There's nothing we can really do about usb-list though.
You could write it to xenstore before attempting the qmp commands. That just relies on the qmp remove failing gracefully for devices which aren't actually attached.
qmp remove will just return an error if you attempt to remove a device id it doesn't know about. I suppose that could work. It's also a bit more robust against, say, the xenstore add failing for whatever reason.
Quoted 3 lines
You might also be able to come up with some complicated, prepare, do it, commit in xenstore like approach -- but that's probably a) difficult (if not impossibly due to races) and b) unnecessary.
Well if the failure mode is the process executing the libxl function being killed at a random point, I don't think we can do a transaction thing -- it would have to coordinate between qmp and xenstore. AFAIK qmp doesn't have any transaction support, so who is going to do the cleanup? The process that was just killed? If it could, it could just finish whatever it was doing anyway.
-George
Quoted 8 lines · George Dunlap
Quoted 7 lines · Ian Campbell
Quoted 6 lines · George Dunlap
We could add an option that says "attempt to do the remove even if you
don't see the device in xenstore" I suppose. But I'm not really sure how to document that in a useful way without basically describing the internal mechanics of the command.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
There's nothing we can really do about usb-list though.
You could write it to xenstore before attempting the qmp commands. That just relies on the qmp remove failing gracefully for devices which aren't actually attached.
qmp remove will just return an error if you attempt to remove a device id it doesn't know about. I suppose that could work. It's also a bit more robust against, say, the xenstore add failing for whatever reason.
Hmm, if the qmp_remove fails, we don't know if that's because because the device didn't exist, or because it failed for some other reason. If qmp remove fails, we shouldn't touch the xenstore entry.
So I think you get in to the same basic problem here, except that you get an un-removeable xenstore entry instead of an unremoveable USB device.
-George
Quoted 33 lines · Ian Jackson
George Dunlap writes ("[PATCH v5 1/2] libxl: Introduce functions to add and remove USB devices to an HVM guest"):
tools/libxl/libxl_create.c retry_transaction: · line 487
Quoted 6 lines
tools/libxl/libxl_create.c +6
-0+/* Helpfully, libxl__xs_rm_checked() returns 0 on success... */ + rc =libxl__xs_rm_checked (gc, t, libxl_usb_path); +if (rc) goto out; +/* ...but libxl__xs_mkdir() returns 1 on success, 0 on failure. */ + rc =libxl__xs_mkdir (gc, t, libxl_usb_path, noperm,ARRAY_SIZE (noperm)); +if (!rc) goto out;Is this really right ? If mkdir fails, you cause
libxl__domain_maketo immediately return success. The out label expects the error code to be in rc. I think it's a stylistic error to assign the (deprecated) boolean return value fromlibxl__xs_mkdirto rc; rc would normally contain a libxl error code.tools/libxl/libxl_qmp.c int libxl__qmp_insert_cdrom(libxl__gc *gc, int domid, · line 941
Quoted 3 lines
tools/libxl/libxl_qmp.c +3
-0+ id =GCSPRINTF (HOST_USB_QDEV_ID , + (uint16_t ) dev->u.hostdev.hostbus, + (uint16_t ) dev->u.hostdev.hostaddr);I think the style in libxl is to cuddle casts.
Quoted 1 line
+
switch(usbdev->type){Missing space. (etc.) And some overly long lines.
Most of the rest of this looks plausible although I wish you wouldn't write things like this:
tools/libxl/libxl_usb.c line 98
Quoted 4 lines
tools/libxl/libxl_usb.c new file +4
-0+switch (usbdev->type) { +case LIBXL_DEVICE_USB_TYPE_HOSTDEV : +if ((rc =read_hostdev_xenstore_entry (gc, path, usbdev)) <0 ) + goto out;I guess old habits are hard to break :-).
I haven't checked the semantics and timing of the xenstore/qmp protocol in detail. But let me ask a question:
Quoted 6 lines
Suppose attempted attach or remove of a usb device fails halfway through (eg, the user presses ^C and xl just stops since it doesn't handle SIGINT). Is the situation now (a) reported in the usb device listing (b) capable of being cleaned up with a remove operation (c) tidied up on domain destroy ? (Is this true at all times - ie does it never go through a state where things won't be cleaned up?)
Re domain destroy: AFAIK everything is completely cleaned up on domain destroy. All lf the xenstore directories are removed, and qemu will release the device when it shuts down. This actually has been tested when I messed up something in storing the xenstore stuff; I didn't have to reboot the host, just shutdown the guest, re-build the library, start the guest again and re-test.
-George