From mboxrd@z Thu Jan 1 00:00:00 1970 From: Chunyan Liu Subject: [PATCH V4 5/7] xl: add pvusb commands Date: Wed, 10 Jun 2015 11:20:39 +0800 Message-ID: <1433906441-3280-6-git-send-email-cyliu@suse.com> References: <1433906441-3280-1-git-send-email-cyliu@suse.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <1433906441-3280-1-git-send-email-cyliu@suse.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel@lists.xen.org Cc: wei.liu2@citrix.com, ian.campbell@citrix.com, george.dunlap@eu.citrix.com, Ian.Jackson@eu.citrix.com, Chunyan Liu , Simon Cao List-Id: xen-devel@lists.xenproject.org Add pvusb commands: usb-ctrl-attach, usb-ctrl-detach, usb-list, usb-attach and usb-detach. To attach a usb device to guest through pvusb, one could follow following example: #xl usb-ctrl-attach test_vm version=1 num_ports=8 #xl usb-list test_vm will show the usb controllers and port usage under the domain. #xl usb-assignable-list will list assignable USB devices #xl usb-attach test_vm 1.6 will find the first usable controller:port, and attach usb device whose bus address is 1.6 (busnum is 1, devnum is 6) to it. One could also specify which and which . #xl usb-detach test_vm 1.6 #xl usb-ctrl-detach test_vm dev_id will destroy the controller with specified dev_id. Dev_id can be traced in usb-list info. Signed-off-by: Chunyan Liu Signed-off-by: Simon Cao --- docs/man/xl.pod.1 | 38 +++++++ tools/libxl/xl.h | 5 + tools/libxl/xl_cmdimpl.c | 251 ++++++++++++++++++++++++++++++++++++++++++++++ tools/libxl/xl_cmdtable.c | 25 +++++ 4 files changed, 319 insertions(+) diff --git a/docs/man/xl.pod.1 b/docs/man/xl.pod.1 index 4eb929d..0259b43 100644 --- a/docs/man/xl.pod.1 +++ b/docs/man/xl.pod.1 @@ -1387,6 +1387,44 @@ List pass-through pci devices for a domain. =back +=head1 USB PASS-THROUGH + +=over 4 + +=item B I [I] [I] + +Create a new USB controller for the specified domain. +B is the usb controller version, could be 1 (USB1.1) or 2 (USB2.0). +B is the total ports of the usb controller. +By default, it will create a USB2.0 controller with 8 ports. + +=item B I I + +Destroy a USB controller from the specified domain. +B is devid of the USB controller. + +=item B I I [I [I]] + +Hot-plug a new pass-through USB device to the specified domain. +B is the busnum.devnum of the physical USB device to pass-through. +B B is the USB controller:port to hotplug the +USB device to. By default, it will find the first available controller:port +and use it; if there is no controller, it will create one. + +=item B I I + +Hot-unplug a previously assigned USB device from a domain. B is +busnum.devnum of the physical USB device to be removed from the guest domain. + +If B<-f> is specified, B is going to forcefully remove the device even +without guest's collaboration. + +=item B I + +List pass-through usb devices for a domain. + +=back + =head1 TMEM =over 4 diff --git a/tools/libxl/xl.h b/tools/libxl/xl.h index 5bc138c..2d57ee3 100644 --- a/tools/libxl/xl.h +++ b/tools/libxl/xl.h @@ -86,6 +86,11 @@ int main_blockdetach(int argc, char **argv); int main_vtpmattach(int argc, char **argv); int main_vtpmlist(int argc, char **argv); int main_vtpmdetach(int argc, char **argv); +int main_usbctrl_attach(int argc, char **argv); +int main_usbctrl_detach(int argc, char **argv); +int main_usbattach(int argc, char **argv); +int main_usbdetach(int argc, char **argv); +int main_usblist(int argc, char **argv); int main_uptime(int argc, char **argv); int main_claims(int argc, char **argv); int main_tmem_list(int argc, char **argv); diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c index c858068..b29d0fc 100644 --- a/tools/libxl/xl_cmdimpl.c +++ b/tools/libxl/xl_cmdimpl.c @@ -3219,6 +3219,257 @@ int main_cd_insert(int argc, char **argv) return 0; } +static void usbinfo_print(libxl_device_usb *usbs, int num) { + int i; + if (usbs == NULL) + return; + for (i = 0; i < num; i++) { + libxl_usbinfo usbinfo; + libxl_usbinfo_init(&usbinfo); + + if (usbs[i].port) + printf(" Port %d:", usbs[i].port); + if (!libxl_device_usb_getinfo(ctx, &usbs[i], &usbinfo)) { + printf(" Bus %03x Device %03x: ID %04x:%04x %s %s\n", + usbinfo.busnum, usbinfo.devnum, + usbinfo.idVendor, usbinfo.idProduct, + usbinfo.manuf ?: "", usbinfo.prod ?: ""); + } + libxl_usbinfo_dispose(&usbinfo); + } +} + +int main_usbctrl_attach(int argc, char **argv) +{ + uint32_t domid; + int opt; + char *oparg; + libxl_device_usbctrl usbctrl; + + SWITCH_FOREACH_OPT(opt, "", NULL, "usb-ctrl-attach", 1) { + /* No options */ + } + + domid = find_domain(argv[optind++]); + + libxl_device_usbctrl_init(&usbctrl); + + while (argc > optind) { + if (MATCH_OPTION("type", argv[optind], oparg)) { + if (!strcmp(oparg, "pv")) { + usbctrl.protocol = LIBXL_USB_PROTOCOL_PV; + } else { + fprintf(stderr, "unsupported type `%s'\n", oparg); + exit(-1); + } + } else if (MATCH_OPTION("version", argv[optind], oparg)) { + usbctrl.version = atoi(oparg); + } else if (MATCH_OPTION("ports", argv[optind], oparg)) { + usbctrl.ports = atoi(oparg); + } else { + fprintf(stderr, "unrecognized argument `%s'\n", argv[optind]); + exit(-1); + } + optind++; + } + + if (usbctrl.protocol == LIBXL_USB_PROTOCOL_AUTO) + usbctrl.protocol = LIBXL_USB_PROTOCOL_PV; + + if (dryrun_only) { + char* json = libxl_device_usbctrl_to_json(ctx, &usbctrl); + printf("usb controller: %s\n", json); + free(json); + libxl_device_usbctrl_dispose(&usbctrl); + if (ferror(stdout) || fflush(stdout)) { + perror("stdout"); + exit(-1); + } + return 0; + } + + if (libxl_device_usbctrl_add(ctx, domid, &usbctrl, 0)) { + fprintf(stderr, "libxl_device_usbctrl_add failed.\n"); + exit(-1); + } + libxl_device_usbctrl_dispose(&usbctrl); + return 0; +} + +int main_usbctrl_detach(int argc, char **argv) +{ + uint32_t domid; + int opt; + libxl_device_usbctrl usbctrl; + + SWITCH_FOREACH_OPT(opt, "", NULL, "usb-ctrl-detach", 2) { + /* No options */ + } + + domid = find_domain(argv[optind]); + + libxl_device_usbctrl_init(&usbctrl); + usbctrl.devid = atoi(argv[optind+1]); + + if (libxl_device_usbctrl_remove(ctx, domid, &usbctrl, 0)) { + fprintf(stderr, "libxl_device_usbctrl_remove failed.\n"); + exit(-1); + } + libxl_device_usbctrl_dispose(&usbctrl); + return 0; + +} + +int main_usbattach(int argc, char **argv) +{ + uint32_t domid; + char *devname, *p; + int opt; + char *oparg; + libxl_device_usb usb; + + SWITCH_FOREACH_OPT(opt, "", NULL, "usb-attach", 2) { + /* No options */ + } + + libxl_device_usb_init(&usb); + + domid = find_domain(argv[optind++]); + devname = argv[optind++]; + p = strchr(devname, '.'); + if (p) { + usb.hostbus = strtoul(devname, NULL, 0); + usb.hostaddr = strtoul(p + 1, NULL, 0); + } + + if (usb.hostbus < 1 || usb.hostaddr < 1) { + fprintf(stderr, "Invalid usb device.\n"); + exit(-1); + } + + while (argc > optind) { + if (MATCH_OPTION("controller", argv[optind], oparg)) { + usb.ctrl = atoi(oparg); + } else if (MATCH_OPTION("port", argv[optind], oparg)) { + usb.port = atoi(oparg); + } else { + fprintf(stderr, "unrecognized argument `%s'\n", argv[optind]); + exit(-1); + } + optind++; + } + + if (dryrun_only) { + char *json = libxl_device_usb_to_json(ctx, &usb); + printf("usb: %s\n", json); + free(json); + libxl_device_usb_dispose(&usb); + if (ferror(stdout) || fflush(stdout)) { + perror("stdout"); + exit(-1); + } + return 0; + } + + if (libxl_device_usb_add(ctx, domid, &usb, 0)) { + fprintf(stderr, "libxl_device_usb_add failed.\n"); + exit(-1); + } + + libxl_device_usb_dispose(&usb); + return 0; +} + +int main_usbdetach(int argc, char **argv) +{ + uint32_t domid; + char *devname, *p; + int opt; + libxl_device_usb usb; + + SWITCH_FOREACH_OPT(opt, "", NULL, "usb-detach", 2) { + /* No options */ + } + + libxl_device_usb_init(&usb); + + domid = find_domain(argv[optind++]); + devname = argv[optind++]; + + if (argc > optind) { + fprintf(stderr, "Invalid arguments.\n"); + exit(-1); + } + + p = strchr(devname, '.'); + if (p) { + usb.hostbus = strtoul(devname, NULL, 0); + usb.hostaddr = strtoul(p + 1, NULL, 0); + } + + if (usb.hostbus < 1 || usb.hostaddr < 1) { + fprintf(stderr, "Invalid usb device.\n"); + exit(-1); + } + + if (libxl_device_usb_remove(ctx, domid, &usb, 0) ) { + fprintf(stderr, "libxl_device_usb_remove failed.\n"); + exit(-1); + } + libxl_device_usb_dispose(&usb); + return 0; +} + +int main_usblist(int argc, char **argv) +{ + uint32_t domid; + libxl_device_usbctrl *usbctrls; + libxl_device_usb *usbs; + libxl_usbctrlinfo usbctrlinfo; + int numctrl, numusb, i, opt; + + SWITCH_FOREACH_OPT(opt, "", NULL, "usb-list", 1) { + /* No options */ + } + + domid = find_domain(argv[optind++]); + + if (argc > optind) { + fprintf(stderr, "Invalid arguments.\n"); + exit(-1); + } + + usbctrls = libxl_device_usbctrl_list(ctx, domid, &numctrl); + if (!usbctrls) { + return 0; + } + + for (i = 0; i < numctrl; ++i) { + printf("%-6s %-6s %-3s %-5s %-7s %-5s %-30s\n", + "Devid", "Type", "BE", "state", "usb-ver", "ports", "BE-path"); + + if (!libxl_device_usbctrl_getinfo(ctx, domid, + &usbctrls[i], &usbctrlinfo)) { + printf("%-6d %-6s %-3d %-5d %-7d %-5d %-30s\n", + usbctrlinfo.devid, + libxl_usb_protocol_to_string(usbctrlinfo.protocol), + usbctrlinfo.backend_id, usbctrlinfo.state, + usbctrlinfo.version, usbctrlinfo.ports, + usbctrlinfo.backend); + + usbs = libxl_device_usb_list_per_usbctrl(ctx, domid, + usbctrlinfo.devid, &numusb); + usbinfo_print(usbs, numusb); + + libxl_usbctrlinfo_dispose(&usbctrlinfo); + } + libxl_device_usbctrl_dispose(&usbctrls[i]); + } + + free(usbctrls); + return 0; +} + int main_console(int argc, char **argv) { uint32_t domid; diff --git a/tools/libxl/xl_cmdtable.c b/tools/libxl/xl_cmdtable.c index 7f4759b..f1c5b43 100644 --- a/tools/libxl/xl_cmdtable.c +++ b/tools/libxl/xl_cmdtable.c @@ -545,6 +545,31 @@ struct cmd_spec cmd_table[] = { "\"local_mem_bandwidth\": Show local memory bandwidth(KB/s)\n", }, #endif + { "usb-ctrl-attach", + &main_usbctrl_attach, 1, 1, + "Create a virtual USB controller for a domain", + " [type=pv] [version=] [ports=]", + }, + { "usb-ctrl-detach", + &main_usbctrl_detach, 0, 1, + "Remove the virtual USB controller specified by for a domain", + " ", + }, + { "usb-attach", + &main_usbattach, 1, 2, + "Attach a USB device to a domain", + " [controller= [port=]]", + }, + { "usb-detach", + &main_usbdetach, 0, 1, + "Detach a USB device from a domain", + " ", + }, + { "usb-list", + &main_usblist, 0, 0, + "List information about USB devices for a domain", + "", + }, }; int cmdtable_len = sizeof(cmd_table)/sizeof(struct cmd_spec); -- 2.1.4