libxl: Introduce functions to add and remove USB devices to an HVM guest
PATCH v4 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.
--- tools/libxl/Makefile | 2 +- tools/libxl/libxl.h | 35 +++ tools/libxl/libxl_create.c | 12 +- tools/libxl/libxl_internal.h | 18 ++ tools/libxl/libxl_qmp.c | 66 +++++ tools/libxl/libxl_types.idl | 22 ++ tools/libxl/libxl_usb.c | 531 ++++++++++++++++++++++++++++++++++++++++ tools/ocaml/libs/xl/genwrap.py | 1 + 8 files changed, 685 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 +35 -0
@@ -75,6 +75,11 @@#define LIBXL_HAVE_FIRMWARE_PASSTHROUGH 1/* +* FIXME: Update comment +*/ +#define LIBXL_HAVE_USB 1 + +/* * libxl ABI compatibility * * The only guarantee which libxl makes regarding ABI compatibility
@@ -735,6 +740,36 @@ 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 three 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. +* +*/ +#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 +11 -1
@@ -391,7 +391,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 ];
@@ -452,6 +452,13 @@ int libxl__domain_make(libxl__gc *gc, libxl_domain_create_info *info, goto out; } + libxl_usb_path =libxl__sprintf (gc,"%s/usb" , libxl_path); +if (!libxl_usb_path) { +LIBXL__LOG (ctx,LIBXL__LOG_ERROR ,"cannot allocate create paths" ); + rc =ERROR_FAIL ; + goto out; + } + noperm[0 ].id =0 ; noperm[0 ].perms =XS_PERM_NONE ;
@@ -475,6 +482,9 @@ retry_transaction:xs_rm (ctx->xsh, t, libxl_path);libxl__xs_mkdir (gc, t, libxl_path, noperm,ARRAY_SIZE (noperm)); +xs_rm (ctx->xsh, t, libxl_usb_path); +libxl__xs_mkdir (gc, t, libxl_usb_path, noperm,ARRAY_SIZE (noperm)); +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
@@ -407,6 +407,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 +531 -0
531 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 v4 2/2 xl: Add commands for usb hot-plug
--- docs/man/xl.pod.1 | 30 +++++++ tools/libxl/xl.h | 3 + tools/libxl/xl_cmdimpl.c | 219 +++++++++++++++++++++++++++++++++++++++++++++ tools/libxl/xl_cmdtable.c | 15 ++++ 4 files changed, 267 insertions(+)
docs/man/xl.pod.1 +30 -0
@@ -1110,6 +1110,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<domain-id> + +Show host USB devices assigned to the guest. + +=back + =head2 VTPM DEVICES =over 4
tools/libxl/xl.h +3 -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);
tools/libxl/xl_cmdimpl.c +219 -0
225 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" , +"<Domain>" , + }, { "mem-max", &main_memmax,0 ,1 , "Set the maximum amount reservation for a domain",
Signature · 1 line
1.7.9.5
Re: [PATCH v4 2/2] xl: Add commands for usb hot-plug
Quoted 75 lines · George Dunlap
Signed-off-by George Dunlap <[email protected]>CC Ian Jackson <[email protected]>CC Roger Pau Monne <[email protected]>--- docs/man/xl.pod.1 | 30 +++++++ tools/libxl/xl.h | 3 + tools/libxl/xl_cmdimpl.c | 219 +++++++++++++++++++++++++++++++++++++++++++++ tools/libxl/xl_cmdtable.c | 15 ++++ 4 files changed, 267 insertions(+)docs/man/xl.pod.1 +30
-0@@ -1110,6 +1110,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<domain-id> + +Show host USB devices assigned to the guest. + +=back + =head2 VTPM DEVICES =over 4tools/libxl/xl.h +3
-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);tools/libxl/xl_cmdimpl.c +1
-0@@ -2600,6 +2600,225 @@ int main_cd_insert(int argc, char **argv)return 0 ; } +
tools/libxl/xl_cmdimpl.c int main_cd_insert(int argc, char **argv) · line 2613
Quoted 10 lines
tools/libxl/xl_cmdimpl.c +10
-0+ +static int parse_usb_hostdev_specifier (libxl_device_usb *dev,const char *s) +{ +const char * hostbus, *hostaddr, *p; + + hostbus = s; + hostaddr=NULL ; + +#define is_dec (_c) ((_c) >= '0' && (_c) <= '9') +#define is_hex (_c) (is_dec(_c) || ((_c) >= 'a' && (_c) <= 'f'))
This are kind of general macros, that could be used elsewhere, might be suitable to put them outside of this function and name them CHAR_IS_DEC and CHAR_IS_HEX.
Quoted 51 lines
tools/libxl/xl_cmdimpl.c +51
-0+ +/* Match [0-9]+\.[0-9] */ +if (!is_dec (*hostbus)) +return -1 ; + +for (p=s; *p; p++) { +if (*p =='.' ) { +if ( !hostaddr ) + hostaddr = p+1 ; +else { +return -1 ; + } + }else if (!is_dec (*p)) { +return -1 ; + } + } +if (!hostaddr || !is_dec (*hostaddr)) +return -1 ; + dev->u.hostdev.hostbus =strtoul (hostbus,NULL ,10 ); + dev->u.hostdev.hostaddr =strtoul (hostaddr,NULL ,10 ); +#undef is_dec +#undef is_hex + +return 0 ; +} + +static int usb_add (uint32_t domid,libxl_device_usb_type type, +const char * device) +{ +libxl_device_usb usbdev; +int rc; + +libxl_device_usb_init (&usbdev); + + usbdev.type = type; + +switch (type) { +case LIBXL_DEVICE_USB_TYPE_HOSTDEV : +if (parse_usb_hostdev_specifier (&usbdev, device) <0 ) { + rc =ERROR_FAIL ; + goto out; + } +break ; +default : +fprintf (stderr,"INTERNAL ERROR: Unimplemented type.\n" ); + rc =ERROR_FAIL ; + goto out; + } + +if ( (rc =libxl_device_usb_add (ctx, domid, &usbdev,NULL )) <0 ) +fprintf (stderr,"libxl_usb_add failed.\n" );
tools/libxl/xl_cmdimpl.c int main_cd_insert(int argc, char **argv) · line 2674
Quoted 10 lines
tools/libxl/xl_cmdimpl.c +10
-0+ +libxl_device_usb_dispose (&usbdev); + +out: +return rc; +} + +int main_usb_add (int argc,char **argv) +{ +uint32_t domid =-1 ;
You could use INVALID_DOMID that is defined early in the file.
Quoted 58 lines
tools/libxl/xl_cmdimpl.c +58
-0+int opt =0 , rc; +const char *device =NULL ; +int type =0 ; + + SWITCH_FOREACH_OPT(opt, "d:v:", NULL, "usb-add", 0) { +case 'd' : + domid =find_domain (optarg); +break ; +case 'v' : + type =LIBXL_DEVICE_USB_TYPE_HOSTDEV ; + device = optarg; +break ; + } + +if ( domid ==-1 ) { +fprintf (stderr,"Must specify domid\n\n" ); +help ("usb-add" ); +return 2 ; + } + +if ( !device ) { +fprintf (stderr,"Must specify a device\n\n" ); +help ("usb-add" ); +return 2 ; + } + + rc =usb_add (domid, type, device); +if ( rc <0 ) +return 1 ; +else +return 0 ; +} + +static int usb_remove (uint32_t domid,libxl_device_usb_type type, +const char * device) +{ +libxl_device_usb usbdev; +int rc; + +libxl_device_usb_init (&usbdev); + + usbdev.type = type; + +switch (type) { +case LIBXL_DEVICE_USB_TYPE_HOSTDEV : +if (parse_usb_hostdev_specifier (&usbdev, device) <0 ) { + rc =ERROR_FAIL ; + goto out; + } +break ; +default : +fprintf (stderr,"INTERNAL ERROR: Unimplemented type.\n" ); + rc =ERROR_FAIL ; + goto out; + } + +if ( (rc =libxl_device_usb_remove (ctx, domid, &usbdev,NULL )) <0 ) +fprintf (stderr,"libxl_usb_remove failed.\n" );
tools/libxl/xl_cmdimpl.c int main_cd_insert(int argc, char **argv) · line 2742
Quoted 10 lines
tools/libxl/xl_cmdimpl.c +10
-0+ +libxl_device_usb_dispose (&usbdev); + +out: +return rc; +} + +int main_usb_remove (int argc,char **argv) +{ +uint32_t domid =-1 ;
INVALID_DOMID
Quoted 49 lines
tools/libxl/xl_cmdimpl.c +49
-0+int opt =0 , rc; +const char *device =NULL ; +int type =0 ; + + SWITCH_FOREACH_OPT(opt, "d:v:", NULL, "usb-remove", 0) { +case 'd' : + domid =find_domain (optarg); +break ; +case 'v' : + type =LIBXL_DEVICE_USB_TYPE_HOSTDEV ; + device = optarg; +break ; + } + +if ( domid ==-1 ) { +fprintf (stderr,"Must specify domid\n\n" ); +help ("usb-remove" ); +return 2 ; + } + +if ( !device ) { +fprintf (stderr,"Must specify a device\n\n" ); +help ("usb-remove" ); +return 2 ; + } + + rc =usb_remove (domid, type, device); +if ( rc <0 ) +return 1 ; +else +return 0 ; +} + +static void usb_list (uint32_t domid) +{ +libxl_device_usb *dev; +int num, i; + + dev =libxl_device_usb_list (ctx, domid, &num); +if (dev ==NULL ) +return ; +printf ("protocol backend type device\n" ); +for (i =0 ; i < num; i++) { +printf ("%8s " , (dev[i].protocol==LIBXL_USB_PROTOCOL_PV )?"pv" :"dm" ); +printf ("%7d " , dev[i].backend_domid); +printf ("%7s " , (dev[i].type==LIBXL_DEVICE_USB_TYPE_HOSTDEV )?"hostdev" :"unknown" ); +if (dev[i].type ==LIBXL_DEVICE_USB_TYPE_HOSTDEV ) + printf("%03d.%03d", + dev[i].u.hostdev.hostbus,
tools/libxl/xl_cmdimpl.c int main_cd_insert(int argc, char **argv) · line 2801
Quoted 10 lines
tools/libxl/xl_cmdimpl.c +10
-0+ dev[i].u.hostdev.hostaddr); +printf ("\n" ); + } +free (dev); +} + + +int main_usb_list (int argc,char **argv) +{ +uint32_t domid =-1 ;
INVALID_DOMID
Quoted 50 lines
tools/libxl/xl_cmdimpl.c +20
-0+int opt; + + SWITCH_FOREACH_OPT(opt, "d:", NULL, "usb-list", 0) { +case 'd' : + domid =find_domain (optarg); +break ; + } + +if ( domid ==-1 ) { +fprintf (stderr,"Must specify domid\n\n" ); +help ("usb-remove" ); +return 2 ; + } + +usb_list (domid); +return 0 ; +} + + +int main_console (int argc,char **argv) {uint32_t domid;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" , +"<Domain>" , + }, { "mem-max", &main_memmax,0 ,1 , "Set the maximum amount reservation for a domain",
Re: [PATCH v4 2/2] xl: Add commands for usb hot-plug
Quoted 75 lines · Roger Pau Monne
Quoted 74 lines · George Dunlap
Signed-off-by George Dunlap <[email protected]>CC Ian Jackson <[email protected]>CC Roger Pau Monne <[email protected]>--- docs/man/xl.pod.1 | 30 +++++++ tools/libxl/xl.h | 3 + tools/libxl/xl_cmdimpl.c | 219 +++++++++++++++++++++++++++++++++++++++++++++ tools/libxl/xl_cmdtable.c | 15 ++++ 4 files changed, 267 insertions(+)docs/man/xl.pod.1 +30
-0@@ -1110,6 +1110,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<domain-id> + +Show host USB devices assigned to the guest. + +=back + =head2 VTPM DEVICES =over 4tools/libxl/xl.h +3
-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);tools/libxl/xl_cmdimpl.c +0
-0@@ -2600,6 +2600,225 @@ int main_cd_insert(int argc, char **argv)return 0 ; }
Quoted 14 lines
tools/libxl/xl_cmdimpl.c int main_cd_insert(int argc, char **argv) · line 2613
Quoted 11 lines
tools/libxl/xl_cmdimpl.c +11
-0+ + +static int parse_usb_hostdev_specifier (libxl_device_usb *dev,const char *s) +{ +const char * hostbus, *hostaddr, *p; + + hostbus = s; + hostaddr=NULL ; + +#define is_dec (_c) ((_c) >= '0' && (_c) <= '9') +#define is_hex (_c) (is_dec(_c) || ((_c) >= 'a' && (_c) <= 'f'))This are kind of general macros, that could be used elsewhere, might be suitable to put them outside of this function and name them
CHAR_IS_DECandCHAR_IS_HEX.
Ack
Quoted 55 lines
Quoted 55 lines
tools/libxl/xl_cmdimpl.c +54
-0+ +/* Match [0-9]+\.[0-9] */ +if (!is_dec (*hostbus)) +return -1 ; + +for (p=s; *p; p++) { +if (*p =='.' ) { +if ( !hostaddr ) + hostaddr = p+1 ; +else { +return -1 ; + } + }else if (!is_dec (*p)) { +return -1 ; + } + } +if (!hostaddr || !is_dec (*hostaddr)) +return -1 ; + dev->u.hostdev.hostbus =strtoul (hostbus,NULL ,10 ); + dev->u.hostdev.hostaddr =strtoul (hostaddr,NULL ,10 ); +#undef is_dec +#undef is_hex + +return 0 ; +} + +static int usb_add (uint32_t domid,libxl_device_usb_type type, +const char * device) +{ +libxl_device_usb usbdev; +int rc; + +libxl_device_usb_init (&usbdev); + + usbdev.type = type; + +switch (type) { +case LIBXL_DEVICE_USB_TYPE_HOSTDEV : +if (parse_usb_hostdev_specifier (&usbdev, device) <0 ) { + rc =ERROR_FAIL ; + goto out; + } +break ; +default : +fprintf (stderr,"INTERNAL ERROR: Unimplemented type.\n" ); + rc =ERROR_FAIL ; + goto out; + } + +if ( (rc =libxl_device_usb_add (ctx, domid, &usbdev,NULL )) <0 ) +fprintf (stderr,"libxl_usb_add failed.\n" ); + +libxl_device_usb_dispose (&usbdev); +
Quoted 8 lines
tools/libxl/xl_cmdimpl.c int main_cd_insert(int argc, char **argv) · line 2674
Quoted 7 lines
tools/libxl/xl_cmdimpl.c +7
-0+out: +return rc; +} + +int main_usb_add (int argc,char **argv) +{ +uint32_t domid =-1 ;You could use
INVALID_DOMIDthat is defined early in the file.
Sorry, should have done that to begin with.
Thanks,
-George
Re: [PATCH v4 2/2] xl: Add commands for usb hot-plug
In reply to Roger Pau MonnéQuoted 6 lines · Roger Pau Monné
tools/libxl/xl_cmdimpl.c int main_cd_insert(int argc, char **argv) · line 2613
Quoted 2 lines
tools/libxl/xl_cmdimpl.c +2
-0+#define is_dec (_c) ((_c) >= '0' && (_c) <= '9') +#define is_hex (_c) (is_dec(_c) || ((_c) >= 'a' && (_c) <= 'f'))This are kind of general macros, that could be used elsewhere, might be suitable to put them outside of this function and name them
CHAR_IS_DECandCHAR_IS_HEX.
ctype.h already provides isdigit() and isxdigit(), no need for our own. IMHO.
Ian.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v4 2/2] xl: Add commands for usb hot-plug
Quoted 9 lines · Ian Campbell
Quoted 5 lines · Roger Pau Monné
tools/libxl/xl_cmdimpl.c int main_cd_insert(int argc, char **argv) · line 2613
Quoted 2 lines
tools/libxl/xl_cmdimpl.c +2
-0+#define is_dec (_c) ((_c) >= '0' && (_c) <= '9') +#define is_hex (_c) (is_dec(_c) || ((_c) >= 'a' && (_c) <= 'f'))This are kind of general macros, that could be used elsewhere, might be suitable to put them outside of this function and name them
CHAR_IS_DECandCHAR_IS_HEX.
ctype.halready providesisdigit()andisxdigit(), no need for our own. IMHO.
Oh right -- sorry, when I was looking at these I didn't see isxdigit(). That's much better.
-George
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v4 2/2] xl: Add commands for usb hot-plug
In reply to Ian CampbellIan Campbell writes ("Re: [Xen-devel] [PATCH v4 2/2] xl: Add commands for usb hot-plug"):
Quoted 7 lines · Roger Pau Monné
Quoted 3 lines
This are kind of general macros, that could be used elsewhere, might be suitable to put them outside of this function and name them
CHAR_IS_DECandCHAR_IS_HEX.
ctype.halready providesisdigit()andisxdigit(), no need for our own. IMHO.
These functions' behaviour depends on the locale. That may or may not be desirable.
If you do use isdigit, be sure to invoke it via the CTYPE macro in libxl_internal.h:
/*
* 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)))
Ian.
Re: [PATCH v4 2/2] xl: Add commands for usb hot-plug
In reply to Roger Pau MonnéRoger Pau Monne writes ("Re: [PATCH v4 2/2] xl: Add commands for usb hot-plug"):
Quoted 1 line · George Dunlap
...
Quoted 6 lines
tools/libxl/xl_cmdimpl.c int main_cd_insert(int argc, char **argv) · line 2613
Quoted 2 lines
tools/libxl/xl_cmdimpl.c +2
-0+#define is_dec (_c) ((_c) >= '0' && (_c) <= '9') +#define is_hex (_c) (is_dec(_c) || ((_c) >= 'a' && (_c) <= 'f'))This are kind of general macros, that could be used elsewhere, might be suitable to put them outside of this function and name them
CHAR_IS_DECandCHAR_IS_HEX.
Also, while we're discussing this: the _'s at the start of the macro formal parameter names are unnecessary, and not usual in libxl.
Ian.
Quoted 52 lines · George Dunlap
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.
Signed-off-by George Dunlap <[email protected]>CC Ian Jackson <[email protected]>CC Roger Pau Monne <[email protected]>--- tools/libxl/Makefile | 2 +- tools/libxl/libxl.h | 35 +++ tools/libxl/libxl_create.c | 12 +- tools/libxl/libxl_internal.h | 18 ++ tools/libxl/libxl_qmp.c | 66 +++++ tools/libxl/libxl_types.idl | 22 ++ tools/libxl/libxl_usb.c | 531 ++++++++++++++++++++++++++++++++++++++++ tools/ocaml/libs/xl/genwrap.py | 1 + 8 files changed, 685 insertions(+), 2 deletions(-)create mode 100644
tools/libxl/libxl_usb.ctools/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.htools/libxl/libxl.h +0
-0
tools/libxl/libxl.h line 79
Quoted 5 lines
tools/libxl/libxl.h +2
-0@@ -75,6 +75,11 @@#define LIBXL_HAVE_FIRMWARE_PASSTHROUGH 1/* +* FIXME: Update comment +*/
Dangit! Well, I'll fix this in v5...
-G
Quoted 778 lines
tools/libxl/libxl.h +33
-0+#define LIBXL_HAVE_USB 1 + +/* * libxl ABI compatibility * * The only guarantee which libxl makes regarding ABI compatibility @@ -735,6 +740,36 @@ 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 three 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. +* +*/ +#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 +11
-1@@ -391,7 +391,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 ];@@ -452,6 +452,13 @@ int libxl__domain_make(libxl__gc *gc, libxl_domain_create_info *info, goto out; } + libxl_usb_path =libxl__sprintf (gc,"%s/usb" , libxl_path); +if (!libxl_usb_path) { +LIBXL__LOG (ctx,LIBXL__LOG_ERROR ,"cannot allocate create paths" ); + rc =ERROR_FAIL ; + goto out; + } + noperm[0 ].id =0 ; noperm[0 ].perms =XS_PERM_NONE ;@@ -475,6 +482,9 @@ retry_transaction:xs_rm (ctx->xsh, t, libxl_path);libxl__xs_mkdir (gc, t, libxl_path, noperm,ARRAY_SIZE (noperm)); +xs_rm (ctx->xsh, t, libxl_usb_path); +libxl__xs_mkdir (gc, t, libxl_usb_path, noperm,ARRAY_SIZE (noperm)); +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@@ -42,6 +42,7 @@#define QMP_RECEIVE_BUFFER_SIZE 4096#define PCI_PT_QDEV_ID "pci-pt-%02x_%02x.%01x" +#define HOST_USB_QDEV_ID "usb-hostdev-%04x.%04x" typedef int (*qmp_callback_t)(libxl__qmp_handler *qmp, const libxl__json_object *tree,@@ -929,6 +930,71 @@ int libxl__qmp_insert_cdrom(libxl__gc *gc, int domid, } } +static int libxl__qmp_usb_hostdev_add (libxl__gc *gc,int domid, +libxl__device_usb *dev) +{ +libxl__json_object *args =NULL ; +char *id; + + id =libxl__sprintf (NOGC ,HOST_USB_QDEV_ID , + (uint16_t ) dev->u.hostdev.hostbus, + (uint16_t ) dev->u.hostdev.hostaddr); + +qmp_parameters_add_string (gc, &args,"driver" ,"usb-host" ); +QMP_PARAMETERS_SPRINTF (&args,"hostbus" ,"0x%x" , dev->u.hostdev.hostbus); +QMP_PARAMETERS_SPRINTF (&args,"hostaddr" ,"0x%x" , dev->u.hostdev.hostaddr); + +qmp_parameters_add_string (gc, &args,"id" , id); + +return qmp_run_command (gc, domid,"device_add" , args,NULL ,NULL ); +} + +int libxl__qmp_usb_add (libxl__gc *gc,int domid, +libxl__device_usb *usbdev) +{ +int rc; +switch (usbdev->type) { +case LIBXL_DEVICE_USB_TYPE_HOSTDEV : + rc =libxl__qmp_usb_hostdev_add (gc, domid, usbdev); +break ; +default : +return ERROR_INVAL ; + } +return rc; +} + + +static int libxl__qmp_usb_hostdev_remove (libxl__gc *gc,int domid, +libxl__device_usb *dev) +{ +libxl__json_object *args =NULL ; +char *id; + + id =libxl__sprintf (NOGC ,HOST_USB_QDEV_ID , + (uint16_t ) dev->u.hostdev.hostbus, + (uint16_t ) dev->u.hostdev.hostaddr); + +qmp_parameters_add_string (gc, &args,"id" , id); + +return qmp_run_command (gc, domid,"device_del" , args,NULL ,NULL ); +} + +int libxl__qmp_usb_remove (libxl__gc *gc,int domid, +libxl__device_usb *usbdev) +{ +int rc; +switch (usbdev->type) { +case LIBXL_DEVICE_USB_TYPE_HOSTDEV : + rc =libxl__qmp_usb_hostdev_remove (gc, domid, usbdev); +break ; +default : +return ERROR_INVAL ; + } +return rc; +} + + +int libxl__qmp_initializations (libxl__gc *gc,uint32_t domid,const libxl_domain_config *guest_config) {tools/libxl/libxl_types.idl +22
-0@@ -407,6 +407,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 +531
-0@@ -0,0 +1,531 @@ +/* +* Copyright (C) 2009 Citrix Ltd. +* Author Vincent Hanquez <[email protected]> +* Author Stefano Stabellini <[email protected]> +* +* 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. +*/ + +#include "libxl_osdeps.h" /* must come before any other headers */ + +#include "libxl_internal.h" + +/* +* /libxl/<domid>/usb/<devid>/{type, protocol, backend, [typeinfo]} +*/ +#define USB_INFO_PATH "%s/usb" +#define USB_HOSTDEV_ID "hostdev-%04x-%04x" + +/* +* Just use plain numbers for now. Replace these with strings at some point. +*/ +#define protocol_to_str (gc, t) libxl__sprintf((gc), "%d", (t)) +#define str_to_protocol (s) atoi((s)) +#define type_to_str (gc, t) libxl__sprintf((gc), "%d", (t)) +#define str_to_typeprotocol (s) atoi((s)) + +static char *hostdev_xs_path (libxl__gc *gc,uint32_t domid, +libxl__device_usb *usbdev) +{ +return libxl__sprintf (gc,USB_INFO_PATH "/%s" , +libxl__xs_libxl_path (gc, domid), +libxl__sprintf (gc,USB_HOSTDEV_ID , + (uint16_t )usbdev->u.hostdev.hostbus, + (uint16_t )usbdev->u.hostdev.hostaddr)); +} + +static void hostdev_xs_append_params (libxl__gc *gc,libxl__device_usb *usbdev, +flexarray_t *a) +{ +flexarray_append_pair (a,"hostbus" , +libxl__sprintf (gc,"%d" , + usbdev->u.hostdev.hostbus)); +flexarray_append_pair (a,"hostaddr" , +libxl__sprintf (gc,"%d" , + usbdev->u.hostdev.hostaddr)); +} + +static int read_hostdev_xenstore_entry (libxl__gc *gc,const char * path, +libxl__device_usb *usbdev) +{ +int rc =0 ; +char * val; + + val =libxl__xs_read (gc,XBT_NULL , +libxl__sprintf (gc,"%s/hostbus" , path)); +if (!val) { +LOG (ERROR ,"Internal error (missing hostbus)" ); + rc =ERROR_FAIL ; + goto out; + } + usbdev->u.hostdev.hostbus =atoi (val); + + val =libxl__xs_read (gc,XBT_NULL , +libxl__sprintf (gc,"%s/hostaddr" , path)); +if (!val) { +LOG (ERROR ,"Internal error (missing hostaddr)" ); + rc =ERROR_FAIL ; + goto out; + } + usbdev->u.hostdev.hostaddr =atoi (val); + +out: +return rc; +} + +static int usb_read_xenstore (libxl__gc *gc,const char * path, +libxl__device_usb *usbdev) +{ +char *val; +int rc =0 ; + + val =libxl__xs_read (gc,XBT_NULL , +libxl__sprintf (gc,"%s/protocol" , path)); +if (!val) { +LOG (ERROR ,"Internal error (missing protocol)" ); + rc =ERROR_FAIL ; + goto out; + } + usbdev->protocol =atoi (val); + + val =libxl__xs_read (gc,XBT_NULL , +libxl__sprintf (gc,"%s/backend_domid" , path)); +if (!val) { +LOG (ERROR ,"Internal error (missing backend_domid)" ); + rc =ERROR_FAIL ; + goto out; + } + usbdev->backend_domid =atoi (val); + + val =libxl__xs_read (gc,XBT_NULL ,libxl__sprintf (gc,"%s/type" , path)); +if (!val) { +LOG (ERROR ,"Internal error (missing type)" ); + rc =ERROR_FAIL ; + goto out; + } + usbdev->type =atoi (val); + +switch (usbdev->type) { +case LIBXL_DEVICE_USB_TYPE_HOSTDEV : +if ((rc =read_hostdev_xenstore_entry (gc, path, usbdev)) <0 ) + goto out; +break ; +default : +LOG (ERROR ,"Internal error (unimplemented type)" ); + rc =ERROR_FAIL ; + goto out; + } + +out: +return rc; +} + +static int usb_add_xenstore (libxl__gc *gc,uint32_t domid, +libxl__device_usb *usbdev) +{ +libxl_ctx *ctx =libxl__gc_owner (gc); +flexarray_t *dev; +char *dev_path; +xs_transaction_t t; +struct xs_permissions noperm[1 ]; + + noperm[0 ].id =0 ; + noperm[0 ].perms =XS_PERM_NONE ; + + dev =flexarray_make (gc,16 ,1 ); + +flexarray_append_pair (dev,"protocol" , +libxl__sprintf (gc,"%d" , usbdev->protocol)); + +flexarray_append_pair (dev,"backend_domid" , +libxl__sprintf (gc,"%d" , usbdev->backend_domid)); + +flexarray_append_pair (dev,"type" , +libxl__sprintf (gc,"%d" , usbdev->type)); + +switch (usbdev->type) { +case LIBXL_DEVICE_USB_TYPE_HOSTDEV : + dev_path =hostdev_xs_path (gc, domid, usbdev); +hostdev_xs_append_params (gc, usbdev, dev); +break ; +default : +LOG (ERROR ,"Invalid device type: %d" , usbdev->type); +return ERROR_FAIL ; + } + +LIBXL__LOG (ctx,LIBXL__LOG_DEBUG ,"Adding new usb device to xenstore" ); + +retry_transaction: + t =xs_transaction_start (ctx->xsh); +libxl__xs_mkdir (gc, t, dev_path, noperm,ARRAY_SIZE (noperm)); +libxl__xs_writev (gc, t, dev_path, +libxl__xs_kvs_of_flexarray (gc, dev, dev->count)); +if (!xs_transaction_end (ctx->xsh, t,0 )) +if (errno ==EAGAIN ) + goto retry_transaction; + +return 0 ; +} + +static int usb_remove_xenstore (libxl__gc *gc,uint32_t domid, +libxl__device_usb *usbdev) +{ +libxl_ctx *ctx =libxl__gc_owner (gc); +char *dev_path; + +LIBXL__LOG (ctx,LIBXL__LOG_DEBUG ,"Removing device from xenstore" ); + +switch (usbdev->type) { +case LIBXL_DEVICE_USB_TYPE_HOSTDEV : + dev_path =hostdev_xs_path (gc, domid, usbdev); +break ; +default : +LOG (ERROR ,"Invalid device type: %d" , usbdev->type); +return ERROR_FAIL ; + } + +xs_rm (ctx->xsh,XBT_NULL , dev_path); + +return 0 ; +} + +static int get_assigned_devices (libxl__gc *gc,uint32_t domid, +libxl__device_usb **list,int *num) +{ +char **devlist, *dompath; +unsigned int nd =0 ; +int i, rc =0 ; + + *list =NULL ; + *num =0 ; + + dompath =libxl__sprintf (gc,USB_INFO_PATH , +libxl__xs_libxl_path (gc, domid)); + + devlist =libxl__xs_directory (gc,XBT_NULL , dompath, &nd); +if ( !devlist ) + goto out; + + *list =calloc (nd,sizeof (libxl__device_usb)); + +for (i =0 ; i < nd; i++) { +char *path; + + path =libxl__sprintf (gc,"%s/%s" , dompath, devlist[i]); +if (usb_read_xenstore (gc, path, (*list) + i) ) { + rc =ERROR_INVAL ; +free (*list); + *list =NULL ; + *num =0 ; + goto out; + } + } + + *num = nd; + +libxl__ptr_add (gc, *list); + +out: +return rc; +} + +static int is_usbdev_type_hostdev_equal (libxl__device_usb *a, +libxl__device_usb *b) +{ +if ( !memcmp (&a->u.hostdev, &b->u.hostdev,sizeof (a->u.hostdev) ) ) +return 1 ; +else +return 0 ; +} + +static int is_usbdev_in_array (libxl__device_usb *assigned,int num_assigned, +libxl__device_usb *dev) +{ +int i; + +/* Devices are the same if: +* - They have the same backend_domid +* - They are of the same type +* - Their types match +* In theory, someone could try to pass the same device through +* via both PVUSB and qemu; not comparing protocol will prevent that. +*/ +for (i =0 ; i < num_assigned; i++) { +if ( assigned[i].type != dev->type + || assigned[i].backend_domid != dev->backend_domid ) +continue ; + +switch (dev->type) { +case LIBXL_DEVICE_USB_TYPE_HOSTDEV : +if (!is_usbdev_type_hostdev_equal (dev, assigned+i)) +continue ; + } + +return 1 ; + } + +return 0 ; +} + +static void usbdev_ext_to_int (libxl__device_usb *dev_int, +libxl_device_usb *dev_ext) +{ + dev_int->protocol = dev_ext->protocol; + +if (dev_ext->backend_domid ==LIBXL_DEVICE_USB_BACKEND_DEFAULT ) + dev_int->backend_domid =0 ; +else + dev_int->backend_domid = dev_ext->backend_domid; + + dev_int->type = dev_ext->type; +memcpy (&dev_int->u, &dev_ext->u,sizeof (dev_ext->u)); +} + +static void usbdev_int_to_ext (libxl_device_usb *dev_ext, +libxl__device_usb *dev_int) +{ + dev_ext->protocol = dev_int->protocol; + + dev_ext->backend_domid = dev_int->backend_domid; + + dev_ext->type = dev_int->type; +memcpy (&dev_ext->u, &dev_int->u,sizeof (dev_ext->u)); +} + +/* +* USB add +*/ +static int do_usb_add (libxl__gc *gc,uint32_t domid, +libxl__device_usb *usbdev) +{ +int rc; + +switch (usbdev->protocol) { +case LIBXL_USB_PROTOCOL_DEVICEMODEL : +switch (libxl__device_model_version_running (gc, domid)) { +case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL : +LOG (ERROR ,"usb-add not yet implemented for qemu-traditional" ); +return ERROR_INVAL ; +case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN : +if ( (rc =libxl__qmp_usb_add (gc, domid, usbdev)) <0 ) + goto out; +break ; +default : +return ERROR_INVAL ; + } +break ; +default : +return ERROR_FAIL ; + } + + rc =usb_add_xenstore (gc, domid, usbdev); +out: +return rc; +} + + + +static int libxl__device_usb_add (libxl__gc *gc,uint32_t domid, +libxl_device_usb *dev_ext) +{ +libxl_ctx *ctx =libxl__gc_owner (gc); +libxl__device_usb *assigned, _usbdev, *usbdev; +int rc =ERROR_FAIL , num_assigned; +libxl_domain_type domtype =libxl__domain_type (gc, domid); + +/* Interpret incoming */ + usbdev = &_usbdev; + + usbdev->target_domid = domid; + usbdev->dm_domid =libxl_get_stubdom_id (ctx, domid); + +usbdev_ext_to_int (usbdev, dev_ext); + +if ( usbdev->protocol ==LIBXL_USB_PROTOCOL_AUTO ) { +if ( domtype ==LIBXL_DOMAIN_TYPE_PV ) { + usbdev->protocol =LIBXL_USB_PROTOCOL_PV ; + }else if (domtype ==LIBXL_DOMAIN_TYPE_HVM ) { +/* FIXME: See if we can detect PV frontend */ + usbdev->protocol =LIBXL_USB_PROTOCOL_DEVICEMODEL ; + } + } + +/* Check to make sure we're doing something that's impemented */ +if ( usbdev->protocol !=LIBXL_USB_PROTOCOL_DEVICEMODEL ) { + rc =ERROR_FAIL ; +LOG (ERROR ,"Protocol not implemented" ); + goto out; + } + +if ( usbdev->dm_domid !=0 ) { + rc =ERROR_FAIL ; +LOG (ERROR ,"Stubdoms not yet supported" ); + goto out; + } + +/* Double-check to make sure it's not already assigned */ + rc =get_assigned_devices (gc, domid, &assigned, &num_assigned); +if ( rc ) { +LOG (ERROR ,"cannot determine if device is assigned, refusing to continue" ); + goto out; + } +if (is_usbdev_in_array (assigned, num_assigned, usbdev) ) { +LOG (ERROR ,"USB device already attached to a domain" ); + rc =ERROR_FAIL ; + goto out; + } + +/* Do the add */ +if (do_usb_add (gc, domid, usbdev) ) + rc =ERROR_FAIL ; + +out: +return rc; +} + +int libxl_device_usb_add (libxl_ctx *ctx,uint32_t domid, +libxl_device_usb *usbdev, +const libxl_asyncop_how *ao_how) +{ +AO_CREATE (ctx, domid, ao_how); +int rc; + rc =libxl__device_usb_add (gc, domid, usbdev); +libxl__ao_complete (egc, ao, rc); +return AO_INPROGRESS ; +} + +/* +* USB remove +*/ +static int do_usb_remove (libxl__gc *gc,uint32_t domid, +libxl__device_usb *usbdev) +{ +int rc; + +switch (usbdev->protocol) { +case LIBXL_USB_PROTOCOL_DEVICEMODEL : +switch (libxl__device_model_version_running (gc, domid)) { +case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL : +LOG (ERROR ,"usb-remove not yet implemented for qemu-traditional" ); +return ERROR_INVAL ; +case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN : +if ( (rc =libxl__qmp_usb_remove (gc, domid, usbdev)) <0 ) + goto out; +break ; +default : +return ERROR_INVAL ; + } +break ; +default : +return ERROR_FAIL ; + } + + rc =usb_remove_xenstore (gc, domid, usbdev); +out: +return rc; +} +static int libxl__device_usb_remove (libxl__gc *gc,uint32_t domid, +libxl_device_usb *dev_ext) +{ +libxl_ctx *ctx =libxl__gc_owner (gc); +libxl__device_usb *assigned, _usbdev, *usbdev; +int rc =ERROR_FAIL , num_assigned; +libxl_domain_type domtype =libxl__domain_type (gc, domid); + +/* Interpret incoming */ + usbdev = &_usbdev; + + usbdev->target_domid = domid; + usbdev->dm_domid =libxl_get_stubdom_id (ctx, domid); + +usbdev_ext_to_int (usbdev, dev_ext); + +if ( usbdev->protocol ==LIBXL_USB_PROTOCOL_AUTO ) { +if ( domtype ==LIBXL_DOMAIN_TYPE_PV ) { + usbdev->protocol =LIBXL_USB_PROTOCOL_PV ; + }else if (domtype ==LIBXL_DOMAIN_TYPE_HVM ) { +/* FIXME: See if we can detect PV frontend */ + usbdev->protocol =LIBXL_USB_PROTOCOL_DEVICEMODEL ; + } + } + +/* Check to make sure we're doing something that's impemented */ +if ( usbdev->protocol !=LIBXL_USB_PROTOCOL_DEVICEMODEL ) { + rc =ERROR_FAIL ; +LOG (ERROR ,"Protocol not implemented" ); + goto out; + } + +if ( usbdev->dm_domid !=0 ) { + rc =ERROR_FAIL ; +LOG (ERROR ,"Stubdoms not yet supported" ); + goto out; + } + +/* Double-check to make sure it's not already assigned */ + rc =get_assigned_devices (gc, domid, &assigned, &num_assigned); +if ( rc ) { +LOG (ERROR ,"cannot determine if device is assigned, refusing to continue" ); + goto out; + } +if ( !is_usbdev_in_array (assigned, num_assigned, usbdev) ) { +LOG (ERROR ,"USB device doesn't seem to be attached to the domain" ); + rc =ERROR_FAIL ; + goto out; + } + +/* Do the remove */ +if (do_usb_remove (gc, domid, usbdev) ) + rc =ERROR_FAIL ; + +out: +return rc; +} + +int libxl_device_usb_remove (libxl_ctx *ctx,uint32_t domid, +libxl_device_usb *usbdev, +const libxl_asyncop_how *ao_how) +{ +AO_CREATE (ctx, domid, ao_how); +int rc; + rc =libxl__device_usb_remove (gc, domid, usbdev); +libxl__ao_complete (egc, ao, rc); +return AO_INPROGRESS ; +} + + +libxl_device_usb *libxl_device_usb_list (libxl_ctx *ctx, +uint32_t domid,int *num) +{ +GC_INIT (ctx); +libxl__device_usb *devint; +libxl_device_usb *devext =NULL ; +int n =0 , i, rc; + + rc =get_assigned_devices (gc, domid, &devint, &n); +if ( rc ) { +LOG (ERROR ,"Could not get assigned devices" ); + goto out; + } + +if (n ==0 ) + goto out; + + devext =calloc (n,sizeof (libxl_device_usb)); + +for (i =0 ; i < n; i++) +usbdev_int_to_ext (devext + i, devint + i); + + *num = n; +out: +GC_FREE ; +return devext; +}tools/ocaml/libs/xl/genwrap.py +1
-1@@ -286,6 +286,7 @@ if __name__ == '__main__': "domain_config", "vcpuinfo", "event", + "device_usb" ] for t in blacklist: --1.7.9.5
Quoted 15 lines · George Dunlap
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.
Quoted 7 lines
This uses the qmp functionality, and is thus only available for qemu-xen, not qemu-traditional.
Signed-off-by George Dunlap <[email protected]>CC Ian Jackson <[email protected]>CC Roger Pau Monne <[email protected]>
Roger, Ian, any feedback on this? It would be nice to get this in this week if possible...
-George
Quoted 15 lines · George Dunlap
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.
Quoted 7 lines
This uses the qmp functionality, and is thus only available for qemu-xen, not qemu-traditional.
Signed-off-by George Dunlap <[email protected]>CC Ian Jackson <[email protected]>CC Roger Pau Monne <[email protected]>
I'm not familiar with the QMP interface, so I just commented the general libxl parts of the patch.
Quoted 92 lines
--- tools/libxl/Makefile | 2 +- tools/libxl/libxl.h | 35 +++ tools/libxl/libxl_create.c | 12 +- tools/libxl/libxl_internal.h | 18 ++ tools/libxl/libxl_qmp.c | 66 +++++ tools/libxl/libxl_types.idl | 22 ++ tools/libxl/libxl_usb.c | 531 ++++++++++++++++++++++++++++++++++++++++ tools/ocaml/libs/xl/genwrap.py | 1 + 8 files changed, 685 insertions(+), 2 deletions(-) create mode 100644 tools/libxl/libxl_usb.ctools/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.htools/libxl/libxl.h +35
-0@@ -75,6 +75,11 @@#define LIBXL_HAVE_FIRMWARE_PASSTHROUGH 1/* +* FIXME: Update comment +*/ +#define LIBXL_HAVE_USB 1 + +/* * libxl ABI compatibility * * The only guarantee which libxl makes regarding ABI compatibility @@ -735,6 +740,36 @@ 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 three 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. +* +*/ +#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 +1
-1@@ -391,7 +391,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 ];
tools/libxl/libxl_create.c int libxl__domain_make(libxl__gc *gc, libxl_domain_create_info *info, · line 455
Quoted 4 lines
tools/libxl/libxl_create.c +1
-0@@ -452,6 +452,13 @@ int libxl__domain_make(libxl__gc *gc, libxl_domain_create_info *info, goto out; } + libxl_usb_path =libxl__sprintf (gc,"%s/usb" , libxl_path);
I would recommend to use GCSPRINTF instead, it already checks for errors and allows for smaller lines (altought you don't have to split the line here)
Quoted 10 lines
tools/libxl/libxl_create.c +6
-0+if (!libxl_usb_path) { +LIBXL__LOG (ctx,LIBXL__LOG_ERROR ,"cannot allocate create paths" ); + rc =ERROR_FAIL ; + goto out; + } + noperm[0 ].id =0 ; noperm[0 ].perms =XS_PERM_NONE ;
tools/libxl/libxl_create.c retry_transaction: · line 485
Quoted 4 lines
tools/libxl/libxl_create.c +1
-0@@ -475,6 +482,9 @@ retry_transaction:xs_rm (ctx->xsh, t, libxl_path);libxl__xs_mkdir (gc, t, libxl_path, noperm,ARRAY_SIZE (noperm)); +xs_rm (ctx->xsh, t, libxl_usb_path);
You are missing the error check, there's also a helper for xs_rm, libxl__xs_rm_checked.
tools/libxl/libxl_create.c retry_transaction: · line 486
Quoted 1 line
tools/libxl/libxl_create.c +1
-0+libxl__xs_mkdir (gc, t, libxl_usb_path, noperm,ARRAY_SIZE (noperm));
Error checking
Quoted 48 lines
tools/libxl/libxl_create.c +1
-0+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 +1
-0@@ -42,6 +42,7 @@#define QMP_RECEIVE_BUFFER_SIZE 4096#define PCI_PT_QDEV_ID "pci-pt-%02x_%02x.%01x" +#define HOST_USB_QDEV_ID "usb-hostdev-%04x.%04x" typedef int (*qmp_callback_t)(libxl__qmp_handler *qmp, const libxl__json_object *tree,@@ -929,6 +930,71 @@ int libxl__qmp_insert_cdrom(libxl__gc *gc, int domid, } }
tools/libxl/libxl_qmp.c int libxl__qmp_insert_cdrom(libxl__gc *gc, int domid, · line 941
Quoted 10 lines
tools/libxl/libxl_qmp.c +9
-0+static int libxl__qmp_usb_hostdev_add (libxl__gc *gc,int domid, +libxl__device_usb *dev) +{ +libxl__json_object *args =NULL ; +char *id; + + id =libxl__sprintf (NOGC ,HOST_USB_QDEV_ID , + (uint16_t ) dev->u.hostdev.hostbus, + (uint16_t ) dev->u.hostdev.hostaddr);
Are you sure you need to use NOGC here? id is only used to generate the "args" string, which in turn is using the GC.
Quoted 24 lines
tools/libxl/libxl_qmp.c +24
-0+ +qmp_parameters_add_string (gc, &args,"driver" ,"usb-host" ); +QMP_PARAMETERS_SPRINTF (&args,"hostbus" ,"0x%x" , dev->u.hostdev.hostbus); +QMP_PARAMETERS_SPRINTF (&args,"hostaddr" ,"0x%x" , dev->u.hostdev.hostaddr); + +qmp_parameters_add_string (gc, &args,"id" , id); + +return qmp_run_command (gc, domid,"device_add" , args,NULL ,NULL ); +} + +int libxl__qmp_usb_add (libxl__gc *gc,int domid, +libxl__device_usb *usbdev) +{ +int rc; +switch (usbdev->type) { +case LIBXL_DEVICE_USB_TYPE_HOSTDEV : + rc =libxl__qmp_usb_hostdev_add (gc, domid, usbdev); +break ; +default : +return ERROR_INVAL ; + } +return rc; +} +
tools/libxl/libxl_qmp.c int libxl__qmp_insert_cdrom(libxl__gc *gc, int domid, · line 975
Quoted 10 lines
tools/libxl/libxl_qmp.c +10
-0+ +static int libxl__qmp_usb_hostdev_remove (libxl__gc *gc,int domid, +libxl__device_usb *dev) +{ +libxl__json_object *args =NULL ; +char *id; + + id =libxl__sprintf (NOGC ,HOST_USB_QDEV_ID , + (uint16_t ) dev->u.hostdev.hostbus, + (uint16_t ) dev->u.hostdev.hostaddr);
The same here, I think you can safely use the GC (GCSPRINTF).
Quoted 86 lines
tools/libxl/libxl_qmp.c +22
-0+ +qmp_parameters_add_string (gc, &args,"id" , id); + +return qmp_run_command (gc, domid,"device_del" , args,NULL ,NULL ); +} + +int libxl__qmp_usb_remove (libxl__gc *gc,int domid, +libxl__device_usb *usbdev) +{ +int rc; +switch (usbdev->type) { +case LIBXL_DEVICE_USB_TYPE_HOSTDEV : + rc =libxl__qmp_usb_hostdev_remove (gc, domid, usbdev); +break ; +default : +return ERROR_INVAL ; + } +return rc; +} + + +int libxl__qmp_initializations (libxl__gc *gc,uint32_t domid,const libxl_domain_config *guest_config) {tools/libxl/libxl_types.idl +22
-0@@ -407,6 +407,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 +22
-0@@ -0,0 +1,531 @@ +/* +* Copyright (C) 2009 Citrix Ltd. +* Author Vincent Hanquez <[email protected]> +* Author Stefano Stabellini <[email protected]> +* +* 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. +*/ + +#include "libxl_osdeps.h" /* must come before any other headers */ + +#include "libxl_internal.h" + +/* +* /libxl/<domid>/usb/<devid>/{type, protocol, backend, [typeinfo]}
tools/libxl/libxl_usb.c line 32
Quoted 10 lines
tools/libxl/libxl_usb.c new file +10
-0+*/ +#define USB_INFO_PATH "%s/usb" +#define USB_HOSTDEV_ID "hostdev-%04x-%04x" + +/* +* Just use plain numbers for now. Replace these with strings at some point. +*/ +#define protocol_to_str (gc, t) libxl__sprintf((gc), "%d", (t)) +#define str_to_protocol (s) atoi((s)) +#define type_to_str (gc, t) libxl__sprintf((gc), "%d", (t))
GCSPRINTF, although this macros are so simple that you might also replace them entirely with GCSPRINTF (altough it might make the code harder to understand).
tools/libxl/libxl_usb.c line 42
Quoted 10 lines
tools/libxl/libxl_usb.c new file +10
-0+#define str_to_typeprotocol (s) atoi((s)) + +static char *hostdev_xs_path (libxl__gc *gc,uint32_t domid, +libxl__device_usb *usbdev) +{ +return libxl__sprintf (gc,USB_INFO_PATH "/%s" , +libxl__xs_libxl_path (gc, domid), +libxl__sprintf (gc,USB_HOSTDEV_ID , + (uint16_t )usbdev->u.hostdev.hostbus, + (uint16_t )usbdev->u.hostdev.hostaddr));
I will stop repeating every time I see libxl__sprintf to replace it with GCSPRINTF.
Quoted 11 lines
tools/libxl/libxl_usb.c new file +11
-0+} + +static void hostdev_xs_append_params (libxl__gc *gc,libxl__device_usb *usbdev, +flexarray_t *a) +{ +flexarray_append_pair (a,"hostbus" , +libxl__sprintf (gc,"%d" , + usbdev->u.hostdev.hostbus)); +flexarray_append_pair (a,"hostaddr" , +libxl__sprintf (gc,"%d" , + usbdev->u.hostdev.hostaddr));
tools/libxl/libxl_usb.c line 63
Quoted 10 lines
tools/libxl/libxl_usb.c new file +10
-0+} + +static int read_hostdev_xenstore_entry (libxl__gc *gc,const char * path, +libxl__device_usb *usbdev) +{ +int rc =0 ; +char * val; + + val =libxl__xs_read (gc,XBT_NULL , +libxl__sprintf (gc,"%s/hostbus" , path));
libxl__xs_read_checked will also print an error message if the read fails.
tools/libxl/libxl_usb.c line 72
Quoted 9 lines
tools/libxl/libxl_usb.c new file +9
-0+if (!val) { +LOG (ERROR ,"Internal error (missing hostbus)" ); + rc =ERROR_FAIL ; + goto out; + } + usbdev->u.hostdev.hostbus =atoi (val); + + val =libxl__xs_read (gc,XBT_NULL , +libxl__sprintf (gc,"%s/hostaddr" , path));
Same
Quoted 86 lines
tools/libxl/libxl_usb.c new file +86
-0+if (!val) { +LOG (ERROR ,"Internal error (missing hostaddr)" ); + rc =ERROR_FAIL ; + goto out; + } + usbdev->u.hostdev.hostaddr =atoi (val); + +out: +return rc; +} + +static int usb_read_xenstore (libxl__gc *gc,const char * path, +libxl__device_usb *usbdev) +{ +char *val; +int rc =0 ; + + val =libxl__xs_read (gc,XBT_NULL , +libxl__sprintf (gc,"%s/protocol" , path)); +if (!val) { +LOG (ERROR ,"Internal error (missing protocol)" ); + rc =ERROR_FAIL ; + goto out; + } + usbdev->protocol =atoi (val); + + val =libxl__xs_read (gc,XBT_NULL , +libxl__sprintf (gc,"%s/backend_domid" , path)); +if (!val) { +LOG (ERROR ,"Internal error (missing backend_domid)" ); + rc =ERROR_FAIL ; + goto out; + } + usbdev->backend_domid =atoi (val); + + val =libxl__xs_read (gc,XBT_NULL ,libxl__sprintf (gc,"%s/type" , path)); +if (!val) { +LOG (ERROR ,"Internal error (missing type)" ); + rc =ERROR_FAIL ; + goto out; + } + usbdev->type =atoi (val); + +switch (usbdev->type) { +case LIBXL_DEVICE_USB_TYPE_HOSTDEV : +if ((rc =read_hostdev_xenstore_entry (gc, path, usbdev)) <0 ) + goto out; +break ; +default : +LOG (ERROR ,"Internal error (unimplemented type)" ); + rc =ERROR_FAIL ; + goto out; + } + +out: +return rc; +} + +static int usb_add_xenstore (libxl__gc *gc,uint32_t domid, +libxl__device_usb *usbdev) +{ +libxl_ctx *ctx =libxl__gc_owner (gc); +flexarray_t *dev; +char *dev_path; +xs_transaction_t t; +struct xs_permissions noperm[1 ]; + + noperm[0 ].id =0 ; + noperm[0 ].perms =XS_PERM_NONE ; + + dev =flexarray_make (gc,16 ,1 ); + +flexarray_append_pair (dev,"protocol" , +libxl__sprintf (gc,"%d" , usbdev->protocol)); + +flexarray_append_pair (dev,"backend_domid" , +libxl__sprintf (gc,"%d" , usbdev->backend_domid)); + +flexarray_append_pair (dev,"type" , +libxl__sprintf (gc,"%d" , usbdev->type)); + +switch (usbdev->type) { +case LIBXL_DEVICE_USB_TYPE_HOSTDEV : + dev_path =hostdev_xs_path (gc, domid, usbdev); +hostdev_xs_append_params (gc, usbdev, dev); +break ;
tools/libxl/libxl_usb.c line 168
Quoted 10 lines
tools/libxl/libxl_usb.c new file +10
-0+default : +LOG (ERROR ,"Invalid device type: %d" , usbdev->type); +return ERROR_FAIL ; + } + +LIBXL__LOG (ctx,LIBXL__LOG_DEBUG ,"Adding new usb device to xenstore" ); + +retry_transaction: + t =xs_transaction_start (ctx->xsh); +libxl__xs_mkdir (gc, t, dev_path, noperm,ARRAY_SIZE (noperm));
Error checking
tools/libxl/libxl_usb.c line 173
Quoted 5 lines
tools/libxl/libxl_usb.c new file +5
-0+libxl__xs_writev (gc, t, dev_path, +libxl__xs_kvs_of_flexarray (gc, dev, dev->count)); +if (!xs_transaction_end (ctx->xsh, t,0 )) +if (errno ==EAGAIN ) + goto retry_transaction;
Ian Jackson (if I remember correctly) added some helpers for transactions, that make the logic a little bit easier (take a look at libxl__device_destroy for example in libxl_device.c):
for (;;) {
rc = libxl__xs_transaction_start(gc, &t);
if (rc) goto out;
/* Do stuff */
rc = libxl__xs_transaction_commit(gc, &t);
if (!rc) break;
if (rc < 0) goto out;
}out:libxl__xs_transaction_abort(gc, &t);
Quoted 34 lines
tools/libxl/libxl_usb.c new file +34
-0+ +return 0 ; +} + +static int usb_remove_xenstore (libxl__gc *gc,uint32_t domid, +libxl__device_usb *usbdev) +{ +libxl_ctx *ctx =libxl__gc_owner (gc); +char *dev_path; + +LIBXL__LOG (ctx,LIBXL__LOG_DEBUG ,"Removing device from xenstore" ); + +switch (usbdev->type) { +case LIBXL_DEVICE_USB_TYPE_HOSTDEV : + dev_path =hostdev_xs_path (gc, domid, usbdev); +break ; +default : +LOG (ERROR ,"Invalid device type: %d" , usbdev->type); +return ERROR_FAIL ; + } + +xs_rm (ctx->xsh,XBT_NULL , dev_path); + +return 0 ; +} + +static int get_assigned_devices (libxl__gc *gc,uint32_t domid, +libxl__device_usb **list,int *num) +{ +char **devlist, *dompath; +unsigned int nd =0 ; +int i, rc =0 ; + + *list =NULL ;
tools/libxl/libxl_usb.c line 217
Quoted 10 lines
tools/libxl/libxl_usb.c new file +10
-0+ *num =0 ; + + dompath =libxl__sprintf (gc,USB_INFO_PATH , +libxl__xs_libxl_path (gc, domid)); + + devlist =libxl__xs_directory (gc,XBT_NULL , dompath, &nd); +if ( !devlist ) + goto out; + + *list =calloc (nd,sizeof (libxl__device_usb));
libxl__calloc
Quoted 36 lines
tools/libxl/libxl_usb.c new file +36
-0+ +for (i =0 ; i < nd; i++) { +char *path; + + path =libxl__sprintf (gc,"%s/%s" , dompath, devlist[i]); +if (usb_read_xenstore (gc, path, (*list) + i) ) { + rc =ERROR_INVAL ; +free (*list); + *list =NULL ; + *num =0 ; + goto out; + } + } + + *num = nd; + +libxl__ptr_add (gc, *list); + +out: +return rc; +} + +static int is_usbdev_type_hostdev_equal (libxl__device_usb *a, +libxl__device_usb *b) +{ +if ( !memcmp (&a->u.hostdev, &b->u.hostdev,sizeof (a->u.hostdev) ) ) +return 1 ; +else +return 0 ; +} + +static int is_usbdev_in_array (libxl__device_usb *assigned,int num_assigned, +libxl__device_usb *dev) +{ +int i; +
tools/libxl/libxl_usb.c line 263
Quoted 10 lines
tools/libxl/libxl_usb.c new file +10
-0+/* Devices are the same if: +* - They have the same backend_domid +* - They are of the same type +* - Their types match +* In theory, someone could try to pass the same device through +* via both PVUSB and qemu; not comparing protocol will prevent that. +*/ +for (i =0 ; i < num_assigned; i++) { +if ( assigned[i].type != dev->type + || assigned[i].backend_domid != dev->backend_domid )
No spaces between "(" and the condition.
Quoted 115 lines
tools/libxl/libxl_usb.c new file +115
-0+continue ; + +switch (dev->type) { +case LIBXL_DEVICE_USB_TYPE_HOSTDEV : +if (!is_usbdev_type_hostdev_equal (dev, assigned+i)) +continue ; + } + +return 1 ; + } + +return 0 ; +} + +static void usbdev_ext_to_int (libxl__device_usb *dev_int, +libxl_device_usb *dev_ext) +{ + dev_int->protocol = dev_ext->protocol; + +if (dev_ext->backend_domid ==LIBXL_DEVICE_USB_BACKEND_DEFAULT ) + dev_int->backend_domid =0 ; +else + dev_int->backend_domid = dev_ext->backend_domid; + + dev_int->type = dev_ext->type; +memcpy (&dev_int->u, &dev_ext->u,sizeof (dev_ext->u)); +} + +static void usbdev_int_to_ext (libxl_device_usb *dev_ext, +libxl__device_usb *dev_int) +{ + dev_ext->protocol = dev_int->protocol; + + dev_ext->backend_domid = dev_int->backend_domid; + + dev_ext->type = dev_int->type; +memcpy (&dev_ext->u, &dev_int->u,sizeof (dev_ext->u)); +} + +/* +* USB add +*/ +static int do_usb_add (libxl__gc *gc,uint32_t domid, +libxl__device_usb *usbdev) +{ +int rc; + +switch (usbdev->protocol) { +case LIBXL_USB_PROTOCOL_DEVICEMODEL : +switch (libxl__device_model_version_running (gc, domid)) { +case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL : +LOG (ERROR ,"usb-add not yet implemented for qemu-traditional" ); +return ERROR_INVAL ; +case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN : +if ( (rc =libxl__qmp_usb_add (gc, domid, usbdev)) <0 ) + goto out; +break ; +default : +return ERROR_INVAL ; + } +break ; +default : +return ERROR_FAIL ; + } + + rc =usb_add_xenstore (gc, domid, usbdev); +out: +return rc; +} + + + +static int libxl__device_usb_add (libxl__gc *gc,uint32_t domid, +libxl_device_usb *dev_ext) +{ +libxl_ctx *ctx =libxl__gc_owner (gc); +libxl__device_usb *assigned, _usbdev, *usbdev; +int rc =ERROR_FAIL , num_assigned; +libxl_domain_type domtype =libxl__domain_type (gc, domid); + +/* Interpret incoming */ + usbdev = &_usbdev; + + usbdev->target_domid = domid; + usbdev->dm_domid =libxl_get_stubdom_id (ctx, domid); + +usbdev_ext_to_int (usbdev, dev_ext); + +if ( usbdev->protocol ==LIBXL_USB_PROTOCOL_AUTO ) { +if ( domtype ==LIBXL_DOMAIN_TYPE_PV ) { + usbdev->protocol =LIBXL_USB_PROTOCOL_PV ; + }else if (domtype ==LIBXL_DOMAIN_TYPE_HVM ) { +/* FIXME: See if we can detect PV frontend */ + usbdev->protocol =LIBXL_USB_PROTOCOL_DEVICEMODEL ; + } + } + +/* Check to make sure we're doing something that's impemented */ +if ( usbdev->protocol !=LIBXL_USB_PROTOCOL_DEVICEMODEL ) { + rc =ERROR_FAIL ; +LOG (ERROR ,"Protocol not implemented" ); + goto out; + } + +if ( usbdev->dm_domid !=0 ) { + rc =ERROR_FAIL ; +LOG (ERROR ,"Stubdoms not yet supported" ); + goto out; + } + +/* Double-check to make sure it's not already assigned */ + rc =get_assigned_devices (gc, domid, &assigned, &num_assigned); +if ( rc ) { +LOG (ERROR ,"cannot determine if device is assigned, refusing to continue" ); + goto out;
tools/libxl/libxl_usb.c line 388
Quoted 10 lines
tools/libxl/libxl_usb.c new file +10
-0+ } +if (is_usbdev_in_array (assigned, num_assigned, usbdev) ) { +LOG (ERROR ,"USB device already attached to a domain" ); + rc =ERROR_FAIL ; + goto out; + } + +/* Do the add */ +if (do_usb_add (gc, domid, usbdev) ) + rc =ERROR_FAIL ;
Spaces between "(" and conditions in this function.
Quoted 22 lines
tools/libxl/libxl_usb.c new file +22
-0+ +out: +return rc; +} + +int libxl_device_usb_add (libxl_ctx *ctx,uint32_t domid, +libxl_device_usb *usbdev, +const libxl_asyncop_how *ao_how) +{ +AO_CREATE (ctx, domid, ao_how); +int rc; + rc =libxl__device_usb_add (gc, domid, usbdev); +libxl__ao_complete (egc, ao, rc); +return AO_INPROGRESS ; +} + +/* +* USB remove +*/ +static int do_usb_remove (libxl__gc *gc,uint32_t domid, +libxl__device_usb *usbdev) +{
tools/libxl/libxl_usb.c line 420
Quoted 10 lines
tools/libxl/libxl_usb.c new file +10
-0+int rc; + +switch (usbdev->protocol) { +case LIBXL_USB_PROTOCOL_DEVICEMODEL : +switch (libxl__device_model_version_running (gc, domid)) { +case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL : +LOG (ERROR ,"usb-remove not yet implemented for qemu-traditional" ); +return ERROR_INVAL ; +case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN : +if ( (rc =libxl__qmp_usb_remove (gc, domid, usbdev)) <0 )
Spaces
Quoted 58 lines
tools/libxl/libxl_usb.c new file +58
-0+ goto out; +break ; +default : +return ERROR_INVAL ; + } +break ; +default : +return ERROR_FAIL ; + } + + rc =usb_remove_xenstore (gc, domid, usbdev); +out: +return rc; +} +static int libxl__device_usb_remove (libxl__gc *gc,uint32_t domid, +libxl_device_usb *dev_ext) +{ +libxl_ctx *ctx =libxl__gc_owner (gc); +libxl__device_usb *assigned, _usbdev, *usbdev; +int rc =ERROR_FAIL , num_assigned; +libxl_domain_type domtype =libxl__domain_type (gc, domid); + +/* Interpret incoming */ + usbdev = &_usbdev; + + usbdev->target_domid = domid; + usbdev->dm_domid =libxl_get_stubdom_id (ctx, domid); + +usbdev_ext_to_int (usbdev, dev_ext); + +if ( usbdev->protocol ==LIBXL_USB_PROTOCOL_AUTO ) { +if ( domtype ==LIBXL_DOMAIN_TYPE_PV ) { + usbdev->protocol =LIBXL_USB_PROTOCOL_PV ; + }else if (domtype ==LIBXL_DOMAIN_TYPE_HVM ) { +/* FIXME: See if we can detect PV frontend */ + usbdev->protocol =LIBXL_USB_PROTOCOL_DEVICEMODEL ; + } + } + +/* Check to make sure we're doing something that's impemented */ +if ( usbdev->protocol !=LIBXL_USB_PROTOCOL_DEVICEMODEL ) { + rc =ERROR_FAIL ; +LOG (ERROR ,"Protocol not implemented" ); + goto out; + } + +if ( usbdev->dm_domid !=0 ) { + rc =ERROR_FAIL ; +LOG (ERROR ,"Stubdoms not yet supported" ); + goto out; + } + +/* Double-check to make sure it's not already assigned */ + rc =get_assigned_devices (gc, domid, &assigned, &num_assigned); +if ( rc ) { +LOG (ERROR ,"cannot determine if device is assigned, refusing to continue" ); + goto out; + }
tools/libxl/libxl_usb.c line 488
Quoted 10 lines
tools/libxl/libxl_usb.c new file +10
-0+if ( !is_usbdev_in_array (assigned, num_assigned, usbdev) ) { +LOG (ERROR ,"USB device doesn't seem to be attached to the domain" ); + rc =ERROR_FAIL ; + goto out; + } + +/* Do the remove */ +if (do_usb_remove (gc, domid, usbdev) ) + rc =ERROR_FAIL ; +
Again spaces in almost the whole function
Quoted 24 lines
tools/libxl/libxl_usb.c new file +24
-0+out: +return rc; +} + +int libxl_device_usb_remove (libxl_ctx *ctx,uint32_t domid, +libxl_device_usb *usbdev, +const libxl_asyncop_how *ao_how) +{ +AO_CREATE (ctx, domid, ao_how); +int rc; + rc =libxl__device_usb_remove (gc, domid, usbdev); +libxl__ao_complete (egc, ao, rc); +return AO_INPROGRESS ; +} + + +libxl_device_usb *libxl_device_usb_list (libxl_ctx *ctx, +uint32_t domid,int *num) +{ +GC_INIT (ctx); +libxl__device_usb *devint; +libxl_device_usb *devext =NULL ; +int n =0 , i, rc; +
tools/libxl/libxl_usb.c line 522
Quoted 10 lines
tools/libxl/libxl_usb.c new file +10
-0+ rc =get_assigned_devices (gc, domid, &devint, &n); +if ( rc ) { +LOG (ERROR ,"Could not get assigned devices" ); + goto out; + } + +if (n ==0 ) + goto out; + + devext =calloc (n,sizeof (libxl_device_usb));
libxl__calloc, also you seem to leak this allocation, which will be solved by the use of libxl__calloc.
Quoted 24 lines
tools/libxl/libxl_usb.c new file +9
-0+ +for (i =0 ; i < n; i++) +usbdev_int_to_ext (devext + i, devint + i); + + *num = n; +out: +GC_FREE ; +return devext; +}tools/ocaml/libs/xl/genwrap.py +1
-1@@ -286,6 +286,7 @@ if __name__ == '__main__': "domain_config", "vcpuinfo", "event", + "device_usb" ] for t in blacklist: --1.7.9.5
Quoted 19 lines · Roger Pau Monne
Quoted 18 lines · George Dunlap
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.
Quoted 6 lines
Quoted 4 lines
Signed-off-by George Dunlap <[email protected]>CC Ian Jackson <[email protected]>CC Roger Pau Monne <[email protected]>I'm not familiar with the QMP interface, so I just commented the general libxl parts of the patch.
Thanks -- this will be helpful.
Quoted 96 lines
Quoted 96 lines
--- tools/libxl/Makefile | 2 +- tools/libxl/libxl.h | 35 +++ tools/libxl/libxl_create.c | 12 +- tools/libxl/libxl_internal.h | 18 ++ tools/libxl/libxl_qmp.c | 66 +++++ tools/libxl/libxl_types.idl | 22 ++ tools/libxl/libxl_usb.c | 531 ++++++++++++++++++++++++++++++++++++++++ tools/ocaml/libs/xl/genwrap.py | 1 + 8 files changed, 685 insertions(+), 2 deletions(-)create mode 100644
tools/libxl/libxl_usb.ctools/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.htools/libxl/libxl.h +35
-0@@ -75,6 +75,11 @@#define LIBXL_HAVE_FIRMWARE_PASSTHROUGH 1/* +* FIXME: Update comment +*/ +#define LIBXL_HAVE_USB 1 + +/* * libxl ABI compatibility * * The only guarantee which libxl makes regarding ABI compatibility @@ -735,6 +740,36 @@ 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 three 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. +* +*/ +#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 +1
-1@@ -391,7 +391,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 ];@@ -452,6 +452,13 @@ int libxl__domain_make(libxl__gc *gc, libxl_domain_create_info *info, goto out; }
Quoted 4 lines
tools/libxl/libxl_create.c int libxl__domain_make(libxl__gc *gc, libxl_domain_create_info *info, · line 455
Quoted 1 line
tools/libxl/libxl_create.c +1
-0+ libxl_usb_path =libxl__sprintf (gc,"%s/usb" , libxl_path);I would recommend to use GCSPRINTF instead, it already checks for errors and allows for smaller lines (altought you don't have to split the line here)
I'm not a fan of ugly macros, but sure I can do that. :-)
Quoted 10 lines
Quoted 10 lines
tools/libxl/libxl_create.c +6
-0+if (!libxl_usb_path) { +LIBXL__LOG (ctx,LIBXL__LOG_ERROR ,"cannot allocate create paths" ); + rc =ERROR_FAIL ; + goto out; + } + noperm[0 ].id =0 ; noperm[0 ].perms =XS_PERM_NONE ;
Quoted 7 lines
tools/libxl/libxl_create.c retry_transaction: · line 485
Quoted 5 lines
tools/libxl/libxl_create.c +1
-0@@ -475,6 +482,9 @@ retry_transaction:xs_rm (ctx->xsh, t, libxl_path);libxl__xs_mkdir (gc, t, libxl_path, noperm,ARRAY_SIZE (noperm)); +xs_rm (ctx->xsh, t, libxl_usb_path);You are missing the error check, there's also a helper for
xs_rm,libxl__xs_rm_checked.
I'm just following suit with what all the other xs_rm's do here. I think it's actually expected that there will *not* be such a path, but that it's just cleaning up to be sure.
Quoted 53 lines
tools/libxl/libxl_create.c retry_transaction: · line 486
Quoted 2 lines
tools/libxl/libxl_create.c +1
-0+libxl__xs_mkdir (gc, t, libxl_usb_path, noperm,ARRAY_SIZE (noperm));Error checking
Quoted 49 lines
tools/libxl/libxl_create.c +1
-0+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 +1
-0@@ -42,6 +42,7 @@#define QMP_RECEIVE_BUFFER_SIZE 4096#define PCI_PT_QDEV_ID "pci-pt-%02x_%02x.%01x" +#define HOST_USB_QDEV_ID "usb-hostdev-%04x.%04x" typedef int (*qmp_callback_t)(libxl__qmp_handler *qmp, const libxl__json_object *tree,@@ -929,6 +930,71 @@ int libxl__qmp_insert_cdrom(libxl__gc *gc, int domid, } }
Quoted 11 lines
tools/libxl/libxl_qmp.c int libxl__qmp_insert_cdrom(libxl__gc *gc, int domid, · line 941
Quoted 9 lines
tools/libxl/libxl_qmp.c +9
-0+static int libxl__qmp_usb_hostdev_add (libxl__gc *gc,int domid, +libxl__device_usb *dev) +{ +libxl__json_object *args =NULL ; +char *id; + + id =libxl__sprintf (NOGC ,HOST_USB_QDEV_ID , + (uint16_t ) dev->u.hostdev.hostbus, + (uint16_t ) dev->u.hostdev.hostaddr);Are you sure you need to use NOGC here? id is only used to generate the "args" string, which in turn is using the GC.
Ah no, good catch. Originally 'id' was passed back in the libxl_device_usb structure, and cleaned up with the ..._destroy() function. That no longer being the case, we definitely need to GC it.
Quoted 128 lines
Quoted 25 lines
tools/libxl/libxl_qmp.c +24
-0+ +qmp_parameters_add_string (gc, &args,"driver" ,"usb-host" ); +QMP_PARAMETERS_SPRINTF (&args,"hostbus" ,"0x%x" , dev->u.hostdev.hostbus); +QMP_PARAMETERS_SPRINTF (&args,"hostaddr" ,"0x%x" , dev->u.hostdev.hostaddr); + +qmp_parameters_add_string (gc, &args,"id" , id); + +return qmp_run_command (gc, domid,"device_add" , args,NULL ,NULL ); +} + +int libxl__qmp_usb_add (libxl__gc *gc,int domid, +libxl__device_usb *usbdev) +{ +int rc; +switch (usbdev->type) { +case LIBXL_DEVICE_USB_TYPE_HOSTDEV : + rc =libxl__qmp_usb_hostdev_add (gc, domid, usbdev); +break ; +default : +return ERROR_INVAL ; + } +return rc; +} +tools/libxl/libxl_qmp.c int libxl__qmp_insert_cdrom(libxl__gc *gc, int domid, · line 975
Quoted 10 lines
tools/libxl/libxl_qmp.c +10
-0+ +static int libxl__qmp_usb_hostdev_remove (libxl__gc *gc,int domid, +libxl__device_usb *dev) +{ +libxl__json_object *args =NULL ; +char *id; + + id =libxl__sprintf (NOGC ,HOST_USB_QDEV_ID , + (uint16_t ) dev->u.hostdev.hostbus, + (uint16_t ) dev->u.hostdev.hostaddr);The same here, I think you can safely use the GC (GCSPRINTF).
Quoted 91 lines
tools/libxl/libxl_qmp.c +22
-0+ +qmp_parameters_add_string (gc, &args,"id" , id); + +return qmp_run_command (gc, domid,"device_del" , args,NULL ,NULL ); +} + +int libxl__qmp_usb_remove (libxl__gc *gc,int domid, +libxl__device_usb *usbdev) +{ +int rc; +switch (usbdev->type) { +case LIBXL_DEVICE_USB_TYPE_HOSTDEV : + rc =libxl__qmp_usb_hostdev_remove (gc, domid, usbdev); +break ; +default : +return ERROR_INVAL ; + } +return rc; +} + + +int libxl__qmp_initializations (libxl__gc *gc,uint32_t domid,const libxl_domain_config *guest_config) {tools/libxl/libxl_types.idl +22
-0@@ -407,6 +407,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 +27
-0@@ -0,0 +1,531 @@ +/* +* Copyright (C) 2009 Citrix Ltd. +* Author Vincent Hanquez <[email protected]> +* Author Stefano Stabellini <[email protected]> +* +* 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. +*/ + +#include "libxl_osdeps.h" /* must come before any other headers */ + +#include "libxl_internal.h" + +/* +* /libxl/<domid>/usb/<devid>/{type, protocol, backend, [typeinfo]} +*/ +#define USB_INFO_PATH "%s/usb" +#define USB_HOSTDEV_ID "hostdev-%04x-%04x" + +/*
Quoted 8 lines
tools/libxl/libxl_usb.c line 32
Quoted 5 lines
tools/libxl/libxl_usb.c new file +5
-0+* Just use plain numbers for now. Replace these with strings at some point. +*/ +#define protocol_to_str (gc, t) libxl__sprintf((gc), "%d", (t)) +#define str_to_protocol (s) atoi((s)) +#define type_to_str (gc, t) libxl__sprintf((gc), "%d", (t))GCSPRINTF, although this macros are so simple that you might also replace them entirely with GCSPRINTF (altough it might make the code harder to understand).
I might do that -- my original plan had actually been to actually print "pv" for LIBXL_DEVICE_USB_BACKEND_PV, "dm" for _DEVICEMODEL, and so on. Doing all the extra parsing was more work than I wanted to try to get done for the code freeze, but I left the functions so I could easily replaced them later.
Anyway, we'll see.
Quoted 28 lines
tools/libxl/libxl_usb.c line 42
Quoted 11 lines
tools/libxl/libxl_usb.c new file +10
-0+#define str_to_typeprotocol (s) atoi((s)) + +static char *hostdev_xs_path (libxl__gc *gc,uint32_t domid, +libxl__device_usb *usbdev) +{ +return libxl__sprintf (gc,USB_INFO_PATH "/%s" , +libxl__xs_libxl_path (gc, domid), +libxl__sprintf (gc,USB_HOSTDEV_ID , + (uint16_t )usbdev->u.hostdev.hostbus, + (uint16_t )usbdev->u.hostdev.hostaddr));I will stop repeating every time I see
libxl__sprintfto replace it with GCSPRINTF.Quoted 14 lines
tools/libxl/libxl_usb.c new file +14
-0+} + +static void hostdev_xs_append_params (libxl__gc *gc,libxl__device_usb *usbdev, +flexarray_t *a) +{ +flexarray_append_pair (a,"hostbus" , +libxl__sprintf (gc,"%d" , + usbdev->u.hostdev.hostbus)); +flexarray_append_pair (a,"hostaddr" , +libxl__sprintf (gc,"%d" , + usbdev->u.hostdev.hostaddr)); +} + +static int read_hostdev_xenstore_entry(libxl__gc *gc, const char * path,
Quoted 8 lines
tools/libxl/libxl_usb.c line 63
Quoted 7 lines
tools/libxl/libxl_usb.c new file +7
-0+ libxl__device_usb *usbdev) +{ +int rc =0 ; +char * val; + + val =libxl__xs_read (gc,XBT_NULL , +libxl__sprintf (gc,"%s/hostbus" , path));
libxl__xs_read_checkedwill also print an error message if the read fails.
Sounds good
Quoted 170 lines
tools/libxl/libxl_usb.c line 72
Quoted 10 lines
tools/libxl/libxl_usb.c new file +9
-0+if (!val) { +LOG (ERROR ,"Internal error (missing hostbus)" ); + rc =ERROR_FAIL ; + goto out; + } + usbdev->u.hostdev.hostbus =atoi (val); + + val =libxl__xs_read (gc,XBT_NULL , +libxl__sprintf (gc,"%s/hostaddr" , path));Same
Quoted 86 lines
tools/libxl/libxl_usb.c new file +86
-0+if (!val) { +LOG (ERROR ,"Internal error (missing hostaddr)" ); + rc =ERROR_FAIL ; + goto out; + } + usbdev->u.hostdev.hostaddr =atoi (val); + +out: +return rc; +} + +static int usb_read_xenstore (libxl__gc *gc,const char * path, +libxl__device_usb *usbdev) +{ +char *val; +int rc =0 ; + + val =libxl__xs_read (gc,XBT_NULL , +libxl__sprintf (gc,"%s/protocol" , path)); +if (!val) { +LOG (ERROR ,"Internal error (missing protocol)" ); + rc =ERROR_FAIL ; + goto out; + } + usbdev->protocol =atoi (val); + + val =libxl__xs_read (gc,XBT_NULL , +libxl__sprintf (gc,"%s/backend_domid" , path)); +if (!val) { +LOG (ERROR ,"Internal error (missing backend_domid)" ); + rc =ERROR_FAIL ; + goto out; + } + usbdev->backend_domid =atoi (val); + + val =libxl__xs_read (gc,XBT_NULL ,libxl__sprintf (gc,"%s/type" , path)); +if (!val) { +LOG (ERROR ,"Internal error (missing type)" ); + rc =ERROR_FAIL ; + goto out; + } + usbdev->type =atoi (val); + +switch (usbdev->type) { +case LIBXL_DEVICE_USB_TYPE_HOSTDEV : +if ((rc =read_hostdev_xenstore_entry (gc, path, usbdev)) <0 ) + goto out; +break ; +default : +LOG (ERROR ,"Internal error (unimplemented type)" ); + rc =ERROR_FAIL ; + goto out; + } + +out: +return rc; +} + +static int usb_add_xenstore (libxl__gc *gc,uint32_t domid, +libxl__device_usb *usbdev) +{ +libxl_ctx *ctx =libxl__gc_owner (gc); +flexarray_t *dev; +char *dev_path; +xs_transaction_t t; +struct xs_permissions noperm[1 ]; + + noperm[0 ].id =0 ; + noperm[0 ].perms =XS_PERM_NONE ; + + dev =flexarray_make (gc,16 ,1 ); + +flexarray_append_pair (dev,"protocol" , +libxl__sprintf (gc,"%d" , usbdev->protocol)); + +flexarray_append_pair (dev,"backend_domid" , +libxl__sprintf (gc,"%d" , usbdev->backend_domid)); + +flexarray_append_pair (dev,"type" , +libxl__sprintf (gc,"%d" , usbdev->type)); + +switch (usbdev->type) { +case LIBXL_DEVICE_USB_TYPE_HOSTDEV : + dev_path =hostdev_xs_path (gc, domid, usbdev); +hostdev_xs_append_params (gc, usbdev, dev); +break ;tools/libxl/libxl_usb.c line 168
Quoted 10 lines
tools/libxl/libxl_usb.c new file +10
-0+default : +LOG (ERROR ,"Invalid device type: %d" , usbdev->type); +return ERROR_FAIL ; + } + +LIBXL__LOG (ctx,LIBXL__LOG_DEBUG ,"Adding new usb device to xenstore" ); + +retry_transaction: + t =xs_transaction_start (ctx->xsh); +libxl__xs_mkdir (gc, t, dev_path, noperm,ARRAY_SIZE (noperm));Error checking
tools/libxl/libxl_usb.c line 173
Quoted 5 lines
tools/libxl/libxl_usb.c new file +5
-0+libxl__xs_writev (gc, t, dev_path, +libxl__xs_kvs_of_flexarray (gc, dev, dev->count)); +if (!xs_transaction_end (ctx->xsh, t,0 )) +if (errno ==EAGAIN ) + goto retry_transaction;Ian Jackson (if I remember correctly) added some helpers for transactions, that make the logic a little bit easier (take a look at
libxl__device_destroyfor example inlibxl_device.c):for (;;) { rc = libxl__xs_transaction_start(gc, &t); if (rc) goto out; /* Do stuff */ rc = libxl__xs_transaction_commit(gc, &t); if (!rc) break; if (rc < 0) goto out; }out:
libxl__xs_transaction_abort(gc, &t);Quoted 37 lines
tools/libxl/libxl_usb.c new file +37
-0+ +return 0 ; +} + +static int usb_remove_xenstore (libxl__gc *gc,uint32_t domid, +libxl__device_usb *usbdev) +{ +libxl_ctx *ctx =libxl__gc_owner (gc); +char *dev_path; + +LIBXL__LOG (ctx,LIBXL__LOG_DEBUG ,"Removing device from xenstore" ); + +switch (usbdev->type) { +case LIBXL_DEVICE_USB_TYPE_HOSTDEV : + dev_path =hostdev_xs_path (gc, domid, usbdev); +break ; +default : +LOG (ERROR ,"Invalid device type: %d" , usbdev->type); +return ERROR_FAIL ; + } + +xs_rm (ctx->xsh,XBT_NULL , dev_path); + +return 0 ; +} + +static int get_assigned_devices (libxl__gc *gc,uint32_t domid, +libxl__device_usb **list,int *num) +{ +char **devlist, *dompath; +unsigned int nd =0 ; +int i, rc =0 ; + + *list =NULL ; + *num =0 ; + + dompath = libxl__sprintf(gc, USB_INFO_PATH,
Quoted 8 lines
tools/libxl/libxl_usb.c line 217
Quoted 7 lines
tools/libxl/libxl_usb.c new file +7
-0+ libxl__xs_libxl_path(gc, domid)); + + devlist =libxl__xs_directory (gc,XBT_NULL , dompath, &nd); +if ( !devlist ) + goto out; + + *list =calloc (nd,sizeof (libxl__device_usb));
libxl__calloc
...which adds it to the GC automatically, I take it? Ack.
Quoted 201 lines
Quoted 37 lines
tools/libxl/libxl_usb.c new file +36
-0+ +for (i =0 ; i < nd; i++) { +char *path; + + path =libxl__sprintf (gc,"%s/%s" , dompath, devlist[i]); +if (usb_read_xenstore (gc, path, (*list) + i) ) { + rc =ERROR_INVAL ; +free (*list); + *list =NULL ; + *num =0 ; + goto out; + } + } + + *num = nd; + +libxl__ptr_add (gc, *list); + +out: +return rc; +} + +static int is_usbdev_type_hostdev_equal (libxl__device_usb *a, +libxl__device_usb *b) +{ +if ( !memcmp (&a->u.hostdev, &b->u.hostdev,sizeof (a->u.hostdev) ) ) +return 1 ; +else +return 0 ; +} + +static int is_usbdev_in_array (libxl__device_usb *assigned,int num_assigned, +libxl__device_usb *dev) +{ +int i; +tools/libxl/libxl_usb.c line 263
Quoted 10 lines
tools/libxl/libxl_usb.c new file +10
-0+/* Devices are the same if: +* - They have the same backend_domid +* - They are of the same type +* - Their types match +* In theory, someone could try to pass the same device through +* via both PVUSB and qemu; not comparing protocol will prevent that. +*/ +for (i =0 ; i < num_assigned; i++) { +if ( assigned[i].type != dev->type + || assigned[i].backend_domid != dev->backend_domid )No spaces between "(" and the condition.
Quoted 115 lines
tools/libxl/libxl_usb.c new file +115
-0+continue ; + +switch (dev->type) { +case LIBXL_DEVICE_USB_TYPE_HOSTDEV : +if (!is_usbdev_type_hostdev_equal (dev, assigned+i)) +continue ; + } + +return 1 ; + } + +return 0 ; +} + +static void usbdev_ext_to_int (libxl__device_usb *dev_int, +libxl_device_usb *dev_ext) +{ + dev_int->protocol = dev_ext->protocol; + +if (dev_ext->backend_domid ==LIBXL_DEVICE_USB_BACKEND_DEFAULT ) + dev_int->backend_domid =0 ; +else + dev_int->backend_domid = dev_ext->backend_domid; + + dev_int->type = dev_ext->type; +memcpy (&dev_int->u, &dev_ext->u,sizeof (dev_ext->u)); +} + +static void usbdev_int_to_ext (libxl_device_usb *dev_ext, +libxl__device_usb *dev_int) +{ + dev_ext->protocol = dev_int->protocol; + + dev_ext->backend_domid = dev_int->backend_domid; + + dev_ext->type = dev_int->type; +memcpy (&dev_ext->u, &dev_int->u,sizeof (dev_ext->u)); +} + +/* +* USB add +*/ +static int do_usb_add (libxl__gc *gc,uint32_t domid, +libxl__device_usb *usbdev) +{ +int rc; + +switch (usbdev->protocol) { +case LIBXL_USB_PROTOCOL_DEVICEMODEL : +switch (libxl__device_model_version_running (gc, domid)) { +case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL : +LOG (ERROR ,"usb-add not yet implemented for qemu-traditional" ); +return ERROR_INVAL ; +case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN : +if ( (rc =libxl__qmp_usb_add (gc, domid, usbdev)) <0 ) + goto out; +break ; +default : +return ERROR_INVAL ; + } +break ; +default : +return ERROR_FAIL ; + } + + rc =usb_add_xenstore (gc, domid, usbdev); +out: +return rc; +} + + + +static int libxl__device_usb_add (libxl__gc *gc,uint32_t domid, +libxl_device_usb *dev_ext) +{ +libxl_ctx *ctx =libxl__gc_owner (gc); +libxl__device_usb *assigned, _usbdev, *usbdev; +int rc =ERROR_FAIL , num_assigned; +libxl_domain_type domtype =libxl__domain_type (gc, domid); + +/* Interpret incoming */ + usbdev = &_usbdev; + + usbdev->target_domid = domid; + usbdev->dm_domid =libxl_get_stubdom_id (ctx, domid); + +usbdev_ext_to_int (usbdev, dev_ext); + +if ( usbdev->protocol ==LIBXL_USB_PROTOCOL_AUTO ) { +if ( domtype ==LIBXL_DOMAIN_TYPE_PV ) { + usbdev->protocol =LIBXL_USB_PROTOCOL_PV ; + }else if (domtype ==LIBXL_DOMAIN_TYPE_HVM ) { +/* FIXME: See if we can detect PV frontend */ + usbdev->protocol =LIBXL_USB_PROTOCOL_DEVICEMODEL ; + } + } + +/* Check to make sure we're doing something that's impemented */ +if ( usbdev->protocol !=LIBXL_USB_PROTOCOL_DEVICEMODEL ) { + rc =ERROR_FAIL ; +LOG (ERROR ,"Protocol not implemented" ); + goto out; + } + +if ( usbdev->dm_domid !=0 ) { + rc =ERROR_FAIL ; +LOG (ERROR ,"Stubdoms not yet supported" ); + goto out; + } + +/* Double-check to make sure it's not already assigned */ + rc =get_assigned_devices (gc, domid, &assigned, &num_assigned); +if ( rc ) { +LOG (ERROR ,"cannot determine if device is assigned, refusing to continue" ); + goto out;tools/libxl/libxl_usb.c line 388
Quoted 10 lines
tools/libxl/libxl_usb.c new file +10
-0+ } +if (is_usbdev_in_array (assigned, num_assigned, usbdev) ) { +LOG (ERROR ,"USB device already attached to a domain" ); + rc =ERROR_FAIL ; + goto out; + } + +/* Do the add */ +if (do_usb_add (gc, domid, usbdev) ) + rc =ERROR_FAIL ;Spaces between "(" and conditions in this function.
Quoted 25 lines
tools/libxl/libxl_usb.c new file +25
-0+ +out: +return rc; +} + +int libxl_device_usb_add (libxl_ctx *ctx,uint32_t domid, +libxl_device_usb *usbdev, +const libxl_asyncop_how *ao_how) +{ +AO_CREATE (ctx, domid, ao_how); +int rc; + rc =libxl__device_usb_add (gc, domid, usbdev); +libxl__ao_complete (egc, ao, rc); +return AO_INPROGRESS ; +} + +/* +* USB remove +*/ +static int do_usb_remove (libxl__gc *gc,uint32_t domid, +libxl__device_usb *usbdev) +{ +int rc; + +switch (usbdev->protocol) {
Quoted 8 lines
tools/libxl/libxl_usb.c line 420
Quoted 7 lines
tools/libxl/libxl_usb.c new file +7
-0+case LIBXL_USB_PROTOCOL_DEVICEMODEL : +switch (libxl__device_model_version_running (gc, domid)) { +case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL : +LOG (ERROR ,"usb-remove not yet implemented for qemu-traditional" ); +return ERROR_INVAL ; +case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN : +if ( (rc =libxl__qmp_usb_remove (gc, domid, usbdev)) <0 )Spaces
How important are the spaces? Most of the time I think it makes it easier to read, in this case particularly so.
Quoted 99 lines
Quoted 59 lines
tools/libxl/libxl_usb.c new file +58
-0+ goto out; +break ; +default : +return ERROR_INVAL ; + } +break ; +default : +return ERROR_FAIL ; + } + + rc =usb_remove_xenstore (gc, domid, usbdev); +out: +return rc; +} +static int libxl__device_usb_remove (libxl__gc *gc,uint32_t domid, +libxl_device_usb *dev_ext) +{ +libxl_ctx *ctx =libxl__gc_owner (gc); +libxl__device_usb *assigned, _usbdev, *usbdev; +int rc =ERROR_FAIL , num_assigned; +libxl_domain_type domtype =libxl__domain_type (gc, domid); + +/* Interpret incoming */ + usbdev = &_usbdev; + + usbdev->target_domid = domid; + usbdev->dm_domid =libxl_get_stubdom_id (ctx, domid); + +usbdev_ext_to_int (usbdev, dev_ext); + +if ( usbdev->protocol ==LIBXL_USB_PROTOCOL_AUTO ) { +if ( domtype ==LIBXL_DOMAIN_TYPE_PV ) { + usbdev->protocol =LIBXL_USB_PROTOCOL_PV ; + }else if (domtype ==LIBXL_DOMAIN_TYPE_HVM ) { +/* FIXME: See if we can detect PV frontend */ + usbdev->protocol =LIBXL_USB_PROTOCOL_DEVICEMODEL ; + } + } + +/* Check to make sure we're doing something that's impemented */ +if ( usbdev->protocol !=LIBXL_USB_PROTOCOL_DEVICEMODEL ) { + rc =ERROR_FAIL ; +LOG (ERROR ,"Protocol not implemented" ); + goto out; + } + +if ( usbdev->dm_domid !=0 ) { + rc =ERROR_FAIL ; +LOG (ERROR ,"Stubdoms not yet supported" ); + goto out; + } + +/* Double-check to make sure it's not already assigned */ + rc =get_assigned_devices (gc, domid, &assigned, &num_assigned); +if ( rc ) { +LOG (ERROR ,"cannot determine if device is assigned, refusing to continue" ); + goto out; + }tools/libxl/libxl_usb.c line 488
Quoted 10 lines
tools/libxl/libxl_usb.c new file +10
-0+if ( !is_usbdev_in_array (assigned, num_assigned, usbdev) ) { +LOG (ERROR ,"USB device doesn't seem to be attached to the domain" ); + rc =ERROR_FAIL ; + goto out; + } + +/* Do the remove */ +if (do_usb_remove (gc, domid, usbdev) ) + rc =ERROR_FAIL ; +Again spaces in almost the whole function
Quoted 28 lines
tools/libxl/libxl_usb.c new file +28
-0+out: +return rc; +} + +int libxl_device_usb_remove (libxl_ctx *ctx,uint32_t domid, +libxl_device_usb *usbdev, +const libxl_asyncop_how *ao_how) +{ +AO_CREATE (ctx, domid, ao_how); +int rc; + rc =libxl__device_usb_remove (gc, domid, usbdev); +libxl__ao_complete (egc, ao, rc); +return AO_INPROGRESS ; +} + + +libxl_device_usb *libxl_device_usb_list (libxl_ctx *ctx, +uint32_t domid,int *num) +{ +GC_INIT (ctx); +libxl__device_usb *devint; +libxl_device_usb *devext =NULL ; +int n =0 , i, rc; + + rc =get_assigned_devices (gc, domid, &devint, &n); +if ( rc ) { +LOG (ERROR ,"Could not get assigned devices" ); + goto out;
Quoted 8 lines
tools/libxl/libxl_usb.c line 522
Quoted 6 lines
tools/libxl/libxl_usb.c new file +6
-0+ } + +if (n ==0 ) + goto out; + + devext =calloc (n,sizeof (libxl_device_usb));
libxl__calloc, also you seem to leak this allocation, which will be solved by the use oflibxl__calloc.
This isn't a leak -- this is giving the list to an external caller, who is responsible to free the list. This follows suit with other libxl_device_.*_list() functions, which must free() the return value themselves. (See e.g., libxl_device_pci_list(), libxl_device_pci_assignable_list(), libxl_device_nic_list(), &c.)
Thanks,
-George
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
tools/libxl/libxl_create.c +6
-0+if (!libxl_usb_path) { +LIBXL__LOG (ctx,LIBXL__LOG_ERROR ,"cannot allocate create paths" ); + rc =ERROR_FAIL ; + goto out; + } + noperm[0 ].id =0 ; noperm[0 ].perms =XS_PERM_NONE ;@@ -475,6 +482,9 @@ retry_transaction:xs_rm (ctx->xsh, t, libxl_path);libxl__xs_mkdir (gc, t, libxl_path, noperm,ARRAY_SIZE (noperm));
Quoted 7 lines
Quoted 3 lines
tools/libxl/libxl_create.c retry_transaction: · line 485
Quoted 1 line
tools/libxl/libxl_create.c +1
-0+xs_rm (ctx->xsh, t, libxl_usb_path);You are missing the error check, there's also a helper for
xs_rm,libxl__xs_rm_checked.I'm just following suit with what all the other xs_rm's do here. I think it's actually expected that there will *not* be such a path, but that it's just cleaning up to be sure.
libxl__xs_rm_checked will not return an error if xs_rm errno is ENOENT, it will only return an error if something really bad happened.
Quoted 34 lines
Quoted 25 lines
Quoted 25 lines
tools/libxl/libxl_usb.c new file +25
-0+ +out: +return rc; +} + +int libxl_device_usb_add (libxl_ctx *ctx,uint32_t domid, +libxl_device_usb *usbdev, +const libxl_asyncop_how *ao_how) +{ +AO_CREATE (ctx, domid, ao_how); +int rc; + rc =libxl__device_usb_add (gc, domid, usbdev); +libxl__ao_complete (egc, ao, rc); +return AO_INPROGRESS ; +} + +/* +* USB remove +*/ +static int do_usb_remove (libxl__gc *gc,uint32_t domid, +libxl__device_usb *usbdev) +{ +int rc; + +switch (usbdev->protocol) {Quoted 8 lines
tools/libxl/libxl_usb.c line 420
Quoted 7 lines
tools/libxl/libxl_usb.c new file +7
-0+case LIBXL_USB_PROTOCOL_DEVICEMODEL : +switch (libxl__device_model_version_running (gc, domid)) { +case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL : +LOG (ERROR ,"usb-remove not yet implemented for qemu-traditional" ); +return ERROR_INVAL ; +case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN : +if ( (rc =libxl__qmp_usb_remove (gc, domid, usbdev)) <0 )Spaces
Quoted 2 lines
How important are the spaces? Most of the time I think it makes it easier to read, in this case particularly so.
It's in libxl CODING_STYLE, since you are already caching the return value, why don't you split the line:
rc = libxl__qmp_usb_remove(gc, domid, usbdev);
if (rc < 0)
...
This makes it even easier to read IMHO.
Quoted 37 lines
Quoted 28 lines
Quoted 28 lines
tools/libxl/libxl_usb.c new file +28
-0+out: +return rc; +} + +int libxl_device_usb_remove (libxl_ctx *ctx,uint32_t domid, +libxl_device_usb *usbdev, +const libxl_asyncop_how *ao_how) +{ +AO_CREATE (ctx, domid, ao_how); +int rc; + rc =libxl__device_usb_remove (gc, domid, usbdev); +libxl__ao_complete (egc, ao, rc); +return AO_INPROGRESS ; +} + + +libxl_device_usb *libxl_device_usb_list (libxl_ctx *ctx, +uint32_t domid,int *num) +{ +GC_INIT (ctx); +libxl__device_usb *devint; +libxl_device_usb *devext =NULL ; +int n =0 , i, rc; + + rc =get_assigned_devices (gc, domid, &devint, &n); +if ( rc ) { +LOG (ERROR ,"Could not get assigned devices" ); + goto out;Quoted 8 lines
tools/libxl/libxl_usb.c line 522
Quoted 6 lines
tools/libxl/libxl_usb.c new file +6
-0+ } + +if (n ==0 ) + goto out; + + devext =calloc (n,sizeof (libxl_device_usb));
libxl__calloc, also you seem to leak this allocation, which will be solved by the use oflibxl__calloc.
Quoted 5 lines
This isn't a leak -- this is giving the list to an external caller, who is responsible to free the list. This follows suit with other libxl_device_.*_list() functions, which must
free()the return value themselves. (See e.g.,libxl_device_pci_list(),libxl_device_pci_assignable_list(),libxl_device_nic_list(), &c.)
OK, then you can use libxl__calloc with NOGC.
Quoted 13 lines · Roger Pau Monne
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
tools/libxl/libxl_create.c +6
-0+if (!libxl_usb_path) { +LIBXL__LOG (ctx,LIBXL__LOG_ERROR ,"cannot allocate create paths" ); + rc =ERROR_FAIL ; + goto out; + } + noperm[0 ].id =0 ; noperm[0 ].perms =XS_PERM_NONE ;@@ -475,6 +482,9 @@ retry_transaction:xs_rm (ctx->xsh, t, libxl_path);libxl__xs_mkdir (gc, t, libxl_path, noperm,ARRAY_SIZE (noperm));
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
tools/libxl/libxl_create.c retry_transaction: · line 485
Quoted 1 line
tools/libxl/libxl_create.c +1
-0+xs_rm (ctx->xsh, t, libxl_usb_path);You are missing the error check, there's also a helper for
xs_rm,libxl__xs_rm_checked.I'm just following suit with what all the other xs_rm's do here. I think it's actually expected that there will *not* be such a path, but that it's just cleaning up to be sure.
libxl__xs_rm_checkedwill not return an error ifxs_rmerrno is ENOENT, it will only return an error if something really bad happened.
OK -- well, if you look at that whole function, there are dozens of things removed and added with no checking. I think it's counter-productive to add checking in only one place -- it gives people the impression that this is something new and different, when in fact it's exactly the same as everything else.
The alternate would be to add a patch to add error-checking to every single one. But I think at this point in the release cycle, that's too risky. It's a lot of code churn for no clear benefit, and there's the possibility we'll introduce a bug that will be discovered at the 11th hour (or in fact after the release).
I can add cleaning up this function in my to-do list for the 4.4 dev cycle if you want.
Quoted 83 lines
Quoted 34 lines
Quoted 26 lines
Quoted 26 lines
tools/libxl/libxl_usb.c new file +25
-0+ +out: +return rc; +} + +int libxl_device_usb_add (libxl_ctx *ctx,uint32_t domid, +libxl_device_usb *usbdev, +const libxl_asyncop_how *ao_how) +{ +AO_CREATE (ctx, domid, ao_how); +int rc; + rc =libxl__device_usb_add (gc, domid, usbdev); +libxl__ao_complete (egc, ao, rc); +return AO_INPROGRESS ; +} + +/* +* USB remove +*/ +static int do_usb_remove (libxl__gc *gc,uint32_t domid, +libxl__device_usb *usbdev) +{ +int rc; + +switch (usbdev->protocol) {Quoted 8 lines
tools/libxl/libxl_usb.c line 420
Quoted 7 lines
tools/libxl/libxl_usb.c new file +7
-0+case LIBXL_USB_PROTOCOL_DEVICEMODEL : +switch (libxl__device_model_version_running (gc, domid)) { +case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL : +LOG (ERROR ,"usb-remove not yet implemented for qemu-traditional" ); +return ERROR_INVAL ; +case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN : +if ( (rc =libxl__qmp_usb_remove (gc, domid, usbdev)) <0 )Spaces
Quoted 2 lines
How important are the spaces? Most of the time I think it makes it easier to read, in this case particularly so.
It's in libxl
CODING_STYLE, since you are already caching the return value, why don't you split the line:rc =
libxl__qmp_usb_remove(gc, domid, usbdev);
if (rc < 0)...This makes it even easier to read IMHO.
Quoted 38 lines
Quoted 38 lines
Quoted 26 lines
tools/libxl/libxl_usb.c new file +18
-0+out: +return rc; +} + +int libxl_device_usb_remove (libxl_ctx *ctx,uint32_t domid, +libxl_device_usb *usbdev, +const libxl_asyncop_how *ao_how) +{ +AO_CREATE (ctx, domid, ao_how); +int rc; + rc =libxl__device_usb_remove (gc, domid, usbdev); +libxl__ao_complete (egc, ao, rc); +return AO_INPROGRESS ; +} + + +libxl_device_usb *libxl_device_usb_list (libxl_ctx *ctx, + uint32_t domid, int *num)
tools/libxl/libxl_usb.c new file +6
-0+{ +GC_INIT (ctx); +libxl__device_usb *devint; +libxl_device_usb *devext =NULL ; +int n =0 , i, rc; +tools/libxl/libxl_usb.c line 522
Quoted 10 lines
tools/libxl/libxl_usb.c new file +10
-0+ rc =get_assigned_devices (gc, domid, &devint, &n); +if ( rc ) { +LOG (ERROR ,"Could not get assigned devices" ); + goto out; + } + +if (n ==0 ) + goto out; + + devext =calloc (n,sizeof (libxl_device_usb));
libxl__calloc, also you seem to leak this allocation, which will be solved by the use oflibxl__calloc.
Quoted 6 lines
Quoted 5 lines
This isn't a leak -- this is giving the list to an external caller, who is responsible to free the list. This follows suit with other libxl_device_.*_list() functions, which must
free()the return value themselves. (See e.g.,libxl_device_pci_list(),libxl_device_pci_assignable_list(),libxl_device_nic_list(), &c.)OK, then you can use
libxl__callocwith NOGC.
Sure, I guess. :-)
-George
George Dunlap writes ("Re: [PATCH v4 1/2] libxl: Introduce functions to add and remove USB devices to an HVM guest"):
Quoted 9 lines · Roger Pau Monne
Quoted 2 lines
libxl__xs_rm_checkedwill not return an error ifxs_rmerrno is ENOENT, it will only return an error if something really bad happened.OK -- well, if you look at that whole function, there are dozens of things removed and added with no checking. I think it's counter-productive to add checking in only one place -- it gives people the impression that this is something new and different, when in fact it's exactly the same as everything else.
Personally I think it's more important not to add new bugs, than to avoid possibly confusing future readers of the code. It's true that we haven't gone through everywhere and fixed this up, but I think that new code should be correct.
Ian.
Quoted 10 lines · George Dunlap
Quoted 9 lines
tools/libxl/libxl_usb.c line 32
Quoted 6 lines
tools/libxl/libxl_usb.c new file +6
-0+/* +* Just use plain numbers for now. Replace these with strings at some point. +*/ +#define protocol_to_str (gc, t) libxl__sprintf((gc), "%d", (t)) +#define str_to_protocol (s) atoi((s)) +#define type_to_str (gc, t) libxl__sprintf((gc), "%d", (t))GCSPRINTF, although this macros are so simple that you might also replace them entirely with GCSPRINTF (altough it might make the code harder to understand).
Quoted 5 lines
I might do that -- my original plan had actually been to actually print "pv" for
LIBXL_DEVICE_USB_BACKEND_PV, "dm" for_DEVICEMODEL, and so on. Doing all the extra parsing was more work than I wanted to try to get done for the code freeze, but I left the functions so I could easily replaced them later.
These symbols should be coming from the IDL, in which case it will have provided the parsing functions already, please use them instead of inventing your own.
e.g. The libxl_domain_type enum in the idl generates libxl_domain_type_from_string and libxl_domain_type_to_string (and _to_json if that floats your boat). These implement the mapping:
LIBXL_DOMAIN_TYPE_INVALID <=> "invalid"
LIBXL_DOMAIN_TYPE_HVM <=> "hvm"
LIBXL_DOMAIN_TYPE_PV <=> "pv"
(the from function is case insensitive)
"Replace these with strings at some point.", is that an API change, either for the libxl user or the driver side?
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
tools/libxl/libxl_usb.c new file +8
-0+switch (usbdev->protocol) { +case LIBXL_USB_PROTOCOL_DEVICEMODEL : +switch (libxl__device_model_version_running (gc, domid)) { +case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL : +LOG (ERROR ,"usb-remove not yet implemented for qemu-traditional" ); +return ERROR_INVAL ; +case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN : +if ( (rc =libxl__qmp_usb_remove (gc, domid, usbdev)) <0 )
Quoted 4 lines
Quoted 1 line
Spaces
How important are the spaces? Most of the time I think it makes it easier to read, in this case particularly so.
Everyone has their own opinions about these thing but the main purpose of having a Coding Style is consistency and libxl's style is to not include those spaces.
If you want to make it easier to read I would suggest removing the assignment from the condition.
Ian.
Quoted 27 lines · Ian Campbell
Quoted 9 lines · George Dunlap
Quoted 9 lines
tools/libxl/libxl_usb.c line 32
Quoted 6 lines
tools/libxl/libxl_usb.c new file +6
-0+/* +* Just use plain numbers for now. Replace these with strings at some point. +*/ +#define protocol_to_str (gc, t) libxl__sprintf((gc), "%d", (t)) +#define str_to_protocol (s) atoi((s)) +#define type_to_str (gc, t) libxl__sprintf((gc), "%d", (t))GCSPRINTF, although this macros are so simple that you might also replace them entirely with GCSPRINTF (altough it might make the code harder to understand).
Quoted 5 lines
I might do that -- my original plan had actually been to actually print "pv" for
LIBXL_DEVICE_USB_BACKEND_PV, "dm" for_DEVICEMODEL, and so on. Doing all the extra parsing was more work than I wanted to try to get done for the code freeze, but I left the functions so I could easily replaced them later.These symbols should be coming from the IDL, in which case it will have provided the parsing functions already, please use them instead of inventing your own.
e.g. The
libxl_domain_typeenum in the idl generateslibxl_domain_type_from_stringandlibxl_domain_type_to_string(and_to_jsonif that floats your boat). These implement the mapping:
LIBXL_DOMAIN_TYPE_INVALID<=> "invalid"
LIBXL_DOMAIN_TYPE_HVM<=> "hvm"
LIBXL_DOMAIN_TYPE_PV<=> "pv"
(the from function is case insensitive)
Quoted 2 lines
"Replace these with strings at some point.", is that an API change, either for the libxl user or the driver side?
This is just an internal thing for libxl; it doesn't persist across reboots.
Let me take a look at the functions you mentioned -- if they're really easy I'll just drop them in. Otherwise, the numeric values will be just fine, even if for some reason they end up being permanent.
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
tools/libxl/libxl_usb.c line 420
Quoted 9 lines
tools/libxl/libxl_usb.c new file +8
-0+switch (usbdev->protocol) { +case LIBXL_USB_PROTOCOL_DEVICEMODEL : +switch (libxl__device_model_version_running (gc, domid)) { +case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL : +LOG (ERROR ,"usb-remove not yet implemented for qemu-traditional" ); +return ERROR_INVAL ; +case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN : +if ( (rc =libxl__qmp_usb_remove (gc, domid, usbdev)) <0 )Spaces
Quoted 8 lines
Quoted 2 lines
How important are the spaces? Most of the time I think it makes it easier to read, in this case particularly so.
Everyone has their own opinions about these thing but the main purpose of having a Coding Style is consistency and libxl's style is to not include those spaces.
If you want to make it easier to read I would suggest removing the assignment from the condition.
Ack.
-George
George Dunlap writes ("Re: [PATCH v4 1/2] libxl: Introduce functions to add and remove USB devices to an HVM guest"):
Quoted 6 lines
Quoted 3 lines
tools/libxl/libxl_usb.c line 420
Quoted 2 lines
tools/libxl/libxl_usb.c new file +2
-0+case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN : +if ( (rc =libxl__qmp_usb_remove (gc, domid, usbdev)) <0 )Spaces
How important are the spaces? Most of the time I think it makes it easier to read, in this case particularly so.
We would normally write this instead:
+ rc = libxl__qmp_usb_remove(gc, domid, usbdev);
+ if (rc) goto out;
I'm assuming libxl__qmp_usb_remove returns 0 on success. If it doesn't you and you want to throw away the information it does return,
+ if (rc<0) goto out;
Ian.
Quoted 3 lines · Roger Pau Monné
Quoted 1 line
+
libxl_usb_path=libxl__sprintf(gc, "%s/usb", libxl_path);I would recommend to use GCSPRINTF instead, it already checks for errors
GCSPRINTF and libxl__sprintf have the same amount of error checking.
The more important point is that the libxl memory allocation functions/macros all panic on ENOMEM so there is no need for code in general to check for allocation errors.
i.e. This block...
Quoted 5 lines
tools/libxl/libxl_create.c int libxl__domain_make(libxl__gc *gc, libxl_domain_create_info *info, · line 460
Quoted 5 lines
tools/libxl/libxl_create.c +5
-0+if (!libxl_usb_path) { +LIBXL__LOG (ctx,LIBXL__LOG_ERROR ,"cannot allocate create paths" ); + rc =ERROR_FAIL ; + goto out; + }
...is unneeded. I didn't check the rest of the patch but I imagine you are in the habits of checking errors and can now remove a bunch of code ;-)
Ian.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Quoted 11 lines · Ian Campbell
Quoted 2 lines · Roger Pau Monné
Quoted 1 line
+
libxl_usb_path=libxl__sprintf(gc, "%s/usb", libxl_path);I would recommend to use GCSPRINTF instead, it already checks for errors
GCSPRINTF and
libxl__sprintfhave the same amount of error checking.The more important point is that the libxl memory allocation functions/macros all panic on ENOMEM so there is no need for code in general to check for allocation errors.
i.e. This block...
Quoted 8 lines
Quoted 5 lines
tools/libxl/libxl_create.c int libxl__domain_make(libxl__gc *gc, libxl_domain_create_info *info, · line 460
Quoted 5 lines
tools/libxl/libxl_create.c +5
-0+if (!libxl_usb_path) { +LIBXL__LOG (ctx,LIBXL__LOG_ERROR ,"cannot allocate create paths" ); + rc =ERROR_FAIL ; + goto out; + }...is unneeded. I didn't check the rest of the patch but I imagine you are in the habits of checking errors and can now remove a bunch of code ;-)
OK -- well in that block in particular, it's following suit with the rest of the surrounding code which does exactly the same thing. As I said in another e-mail to roger about error checking in this function, I think that it's more important for programmers to be able to see this is exactly the same as all the rest. At some point there should probably be a clean-up to remove the check from all of them, but I don't think that's worth the risk at this point in the release cycle.
I can put cleaning up this function on my to-do list for the 4.4 dev cycle if you want.
-George
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Quoted 17 lines · George Dunlap
Quoted 16 lines · Ian Campbell
Quoted 2 lines · Roger Pau Monné
Quoted 1 line
+
libxl_usb_path=libxl__sprintf(gc, "%s/usb", libxl_path);I would recommend to use GCSPRINTF instead, it already checks for errors
GCSPRINTF and
libxl__sprintfhave the same amount of error checking.The more important point is that the libxl memory allocation functions/macros all panic on ENOMEM so there is no need for code in general to check for allocation errors.
i.e. This block...
Quoted 5 lines
Quoted 5 lines
tools/libxl/libxl_create.c +5
-0+if (!libxl_usb_path) { +LIBXL__LOG (ctx,LIBXL__LOG_ERROR ,"cannot allocate create paths" ); + rc =ERROR_FAIL ; + goto out; + }
Quoted 6 lines
Quoted 3 lines
...is unneeded. I didn't check the rest of the patch but I imagine you are in the habits of checking errors and can now remove a bunch of code ;-)
OK -- well in that block in particular, it's following suit with the rest of the surrounding code which does exactly the same thing.
That code predates the panic on ENOMEM behaviour.
Quoted 6 lines
As I
said in another e-mail to roger about error checking in this function, I think that it's more important for programmers to be able to see this is exactly the same as all the rest. At some point there should probably be a clean-up to remove the check from all of them, but I don't think that's worth the risk at this point in the release cycle.
Rather than having a massive flag day we are taking the approach of fixing code as it is changed and part of that is to not introduce new "incorrect" code.
Quoted 2 lines
I can put cleaning up this function on my to-do list for the 4.4 dev cycle if you want.
That would also be good of course.
Ian.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Quoted 31 lines · Ian Campbell
Quoted 17 lines · George Dunlap
Quoted 16 lines · Ian Campbell
Quoted 2 lines · Roger Pau Monné
Quoted 1 line
+
libxl_usb_path=libxl__sprintf(gc, "%s/usb", libxl_path);I would recommend to use GCSPRINTF instead, it already checks for errors
GCSPRINTF and
libxl__sprintfhave the same amount of error checking.The more important point is that the libxl memory allocation functions/macros all panic on ENOMEM so there is no need for code in general to check for allocation errors.
i.e. This block...
Quoted 5 lines
Quoted 5 lines
tools/libxl/libxl_create.c +5
-0+if (!libxl_usb_path) { +LIBXL__LOG (ctx,LIBXL__LOG_ERROR ,"cannot allocate create paths" ); + rc =ERROR_FAIL ; + goto out; + }Quoted 5 lines
Quoted 3 lines
...is unneeded. I didn't check the rest of the patch but I imagine you are in the habits of checking errors and can now remove a bunch of code ;-)
OK -- well in that block in particular, it's following suit with the rest of the surrounding code which does exactly the same thing.
That code predates the panic on ENOMEM behaviour.
Quoted 6 lines
As I
said in another e-mail to roger about error checking in this function, I think that it's more important for programmers to be able to see this is exactly the same as all the rest. At some point there should probably be a clean-up to remove the check from all of them, but I don't think that's worth the risk at this point in the release cycle.
Quoted 3 lines
Rather than having a massive flag day we are taking the approach of fixing code as it is changed and part of that is to not introduce new "incorrect" code.
I think that makes sense on a function-by-function, or perhaps file-by-file basis, but I personally think it's a bad idea to mix "templates" in one function like that. But if that's the plan, I'll go with it.
-George
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel