From patchwork Tue Dec 22 07:01:11 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chunyan Liu X-Patchwork-Id: 7902211 Return-Path: X-Original-To: patchwork-xen-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id A75F69F349 for ; Tue, 22 Dec 2015 07:04:12 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 70D122053E for ; Tue, 22 Dec 2015 07:04:11 +0000 (UTC) Received: from lists.xen.org (lists.xenproject.org [50.57.142.19]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id F2B2B205B1 for ; Tue, 22 Dec 2015 07:04:09 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xen.org) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1aBGxD-0004aS-BN; Tue, 22 Dec 2015 07:01:51 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1aBGxB-0004ZQ-Vu for xen-devel@lists.xen.org; Tue, 22 Dec 2015 07:01:50 +0000 Received: from [85.158.139.211] by server-12.bemta-5.messagelabs.com id BD/A8-12831-D55F8765; Tue, 22 Dec 2015 07:01:49 +0000 X-Env-Sender: cyliu@suse.com X-Msg-Ref: server-7.tower-206.messagelabs.com!1450767706!11886535!1 X-Originating-IP: [137.65.250.26] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 7.35.1; banners=-,-,- X-VirusChecked: Checked Received: (qmail 13051 invoked from network); 22 Dec 2015 07:01:48 -0000 Received: from victor.provo.novell.com (HELO prv3-mh.provo.novell.com) (137.65.250.26) by server-7.tower-206.messagelabs.com with DHE-RSA-AES256-SHA encrypted SMTP; 22 Dec 2015 07:01:48 -0000 Received: from linux-3hvf.site (prv-ext-foundry1int.gns.novell.com [137.65.251.240]) by prv3-mh.provo.novell.com with ESMTP (TLS encrypted); Tue, 22 Dec 2015 00:01:35 -0700 From: Chunyan Liu To: xen-devel@lists.xen.org Date: Tue, 22 Dec 2015 15:01:11 +0800 Message-Id: <1450767672-21035-5-git-send-email-cyliu@suse.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1450767672-21035-1-git-send-email-cyliu@suse.com> References: <1450767672-21035-1-git-send-email-cyliu@suse.com> Cc: jgross@suse.com, wei.liu2@citrix.com, ian.campbell@citrix.com, george.dunlap@eu.citrix.com, Ian.Jackson@eu.citrix.com, Chunyan Liu , jfehlig@suse.com, Simon Cao Subject: [Xen-devel] [PATCH V12 4/5] xl: add pvusb commands X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add pvusb commands: usbctrl-attach, usbctrl-detach, usb-list, usbdev-attach and usbdev-detach. To attach a usb device to guest through pvusb, one could follow following example: #xl usbctrl-attach test_vm version=1 ports=8 #xl usb-list test_vm will show the usb controllers and port usage under the domain. #xl usbdev-attach test_vm hostbus=1 hostaddr=2 will find the first usable controller:port, and attach usb device whose busnum is 1 and devnum is 6. One could also specify which and which . #xl usbdev-detach test_vm 0 1 will detach USB device under controller 0 port 1. #xl usbctrl-detach test_vm dev_id will destroy the controller with specified dev_id. Dev_id can be traced in usb-list info. Signed-off-by: Chunyan Liu Signed-off-by: Simon Cao Reviewed-by: George Dunlap --- docs/man/xl.pod.1 | 41 ++++++++ tools/libxl/xl.h | 5 + tools/libxl/xl_cmdimpl.c | 243 ++++++++++++++++++++++++++++++++++++++++++++++ tools/libxl/xl_cmdtable.c | 25 +++++ 4 files changed, 314 insertions(+) diff --git a/docs/man/xl.pod.1 b/docs/man/xl.pod.1 index 4279c7c..746f49f 100644 --- a/docs/man/xl.pod.1 +++ b/docs/man/xl.pod.1 @@ -1345,6 +1345,47 @@ List pass-through pci devices for a domain. =back +=head1 USB PASS-THROUGH + +=over 4 + +=item B I I[] [I] [I] + +Create a new USB controller for the specified domain. +B is the usb controller type. Currently only 'pv' and 'auto' +are supported. +B is the usb controller version. Possible values include +1 (USB1.1) and 2 (USB2.0). +B is the total ports of the usb controller. The maximum +number is 31. +By default, it will create a USB2.0 controller with 8 ports. + +=item B I I + +Destroy a USB controller from the specified domain. +B is devid of the USB controller. + +=item B I I I [I [I]] + +Hot-plug a new pass-through USB device to the specified domain. +B and B are the bus and device number 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, a new controller will be created. + +=item B I I I + +Hot-unplug a previously assigned USB device from a domain. +B and B is USB controller:port in guest where the +USB device is attached to. + +=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 bdab125..309627a 100644 --- a/tools/libxl/xl.h +++ b/tools/libxl/xl.h @@ -92,6 +92,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_usbdev_attach(int argc, char **argv); +int main_usbdev_detach(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 f9933cb..b84ec98d 100644 --- a/tools/libxl/xl_cmdimpl.c +++ b/tools/libxl/xl_cmdimpl.c @@ -1255,6 +1255,58 @@ static void parse_vnuma_config(const XLU_Config *config, free(vcpu_parsed); } +/* Parses usbctrl data and adds info into usbctrl + * Returns 1 if the input token does not match one of the keys + * or parsed values are not correct. Successful parse returns 0 */ +static int parse_usbctrl_config(libxl_device_usbctrl *usbctrl, char *token) +{ + char *oparg; + + if (MATCH_OPTION("type", token, oparg)) { + if (libxl_usbctrl_type_from_string(oparg, &usbctrl->type)) { + fprintf(stderr, "Invalid usb controller type '%s'\n", oparg); + return 1; + } + } else if (MATCH_OPTION("version", token, oparg)) { + usbctrl->version = atoi(oparg); + } else if (MATCH_OPTION("ports", token, oparg)) { + usbctrl->ports = atoi(oparg); + } else { + fprintf(stderr, "Unknown string `%s' in usbctrl spec\n", token); + return 1; + } + + return 0; +} + +/* Parses usbdev data and adds info into usbdev + * Returns 1 if the input token does not match one of the keys + * or parsed values are not correct. Successful parse returns 0 */ +static int parse_usbdev_config(libxl_device_usbdev *usbdev, char *token) +{ + char *oparg; + + if (MATCH_OPTION("type", token, oparg)) { + if (libxl_usbdev_type_from_string(oparg, &usbdev->type)) { + fprintf(stderr, "Invalid usb device type: %s\n", optarg); + return 1; + } + } else if (MATCH_OPTION("hostbus", token, oparg)) { + usbdev->u.hostdev.hostbus = strtoul(oparg, NULL, 0); + } else if (MATCH_OPTION("hostaddr", token, oparg)) { + usbdev->u.hostdev.hostaddr = strtoul(oparg, NULL, 0); + } else if (MATCH_OPTION("controller", token, oparg)) { + usbdev->ctrl = atoi(oparg); + } else if (MATCH_OPTION("port", token, oparg)) { + usbdev->port = atoi(oparg); + } else { + fprintf(stderr, "Unknown string `%s' in usbdev spec\n", token); + return 1; + } + + return 0; +} + static void parse_config_data(const char *config_source, const char *config_data, int config_len, @@ -3402,6 +3454,197 @@ int main_cd_insert(int argc, char **argv) return 0; } +int main_usbctrl_attach(int argc, char **argv) +{ + uint32_t domid; + int opt, rc = 0; + libxl_device_usbctrl usbctrl; + + SWITCH_FOREACH_OPT(opt, "", NULL, "usbctrl-attach", 1) { + /* No options */ + } + + domid = find_domain(argv[optind++]); + + libxl_device_usbctrl_init(&usbctrl); + + for (argv += optind, argc -= optind; argc > 0; ++argv, --argc) { + if (parse_usbctrl_config(&usbctrl, *argv)) + return 1; + } + + rc = libxl_device_usbctrl_add(ctx, domid, &usbctrl, 0); + if (rc) { + fprintf(stderr, "libxl_device_usbctrl_add failed.\n"); + rc = 1; + } + + libxl_device_usbctrl_dispose(&usbctrl); + return rc; +} + +int main_usbctrl_detach(int argc, char **argv) +{ + uint32_t domid; + int opt, devid, rc; + libxl_device_usbctrl usbctrl; + + SWITCH_FOREACH_OPT(opt, "", NULL, "usbctrl-detach", 2) { + /* No options */ + } + + domid = find_domain(argv[optind]); + devid = atoi(argv[optind+1]); + + libxl_device_usbctrl_init(&usbctrl); + if (libxl_devid_to_device_usbctrl(ctx, domid, devid, &usbctrl)) { + fprintf(stderr, "Unknown device %s.\n", argv[optind+1]); + return 1; + } + + rc = libxl_device_usbctrl_remove(ctx, domid, &usbctrl, 0); + if (rc) { + fprintf(stderr, "libxl_device_usbctrl_remove failed.\n"); + rc = 1; + } + + libxl_device_usbctrl_dispose(&usbctrl); + return rc; + +} + +int main_usbdev_attach(int argc, char **argv) +{ + uint32_t domid; + int opt, rc; + libxl_device_usbdev usbdev; + + SWITCH_FOREACH_OPT(opt, "", NULL, "usbdev-attach", 2) { + /* No options */ + } + + libxl_device_usbdev_init(&usbdev); + + domid = find_domain(argv[optind++]); + + for (argv += optind, argc -= optind; argc > 0; ++argv, --argc) { + if (parse_usbdev_config(&usbdev, *argv)) + return 1; + } + + rc = libxl_device_usbdev_add(ctx, domid, &usbdev, 0); + if (rc) { + fprintf(stderr, "libxl_device_usbdev_add failed.\n"); + rc = 1; + } + + libxl_device_usbdev_dispose(&usbdev); + return rc; +} + +int main_usbdev_detach(int argc, char **argv) +{ + uint32_t domid; + int ctrl, port; + int opt, rc = 1; + libxl_device_usbdev usbdev; + + SWITCH_FOREACH_OPT(opt, "", NULL, "usbdev-detach", 3) { + /* No options */ + } + + domid = find_domain(argv[optind]); + ctrl = atoi(argv[optind+1]); + port = atoi(argv[optind+2]); + + if (argc - optind > 3) { + fprintf(stderr, "Invalid arguments.\n"); + return 1; + } + + libxl_device_usbdev_init(&usbdev); + if (libxl_ctrlport_to_device_usbdev(ctx, domid, ctrl, port, &usbdev)) { + fprintf(stderr, "Unknown device at controller %d port %d.\n", + ctrl, port); + return 1; + } + + rc = libxl_device_usbdev_remove(ctx, domid, &usbdev, 0); + if (rc) { + fprintf(stderr, "libxl_device_usbdev_remove failed.\n"); + rc = 1; + } + + libxl_device_usbdev_dispose(&usbdev); + return rc; +} + +int main_usblist(int argc, char **argv) +{ + uint32_t domid; + libxl_device_usbctrl *usbctrls; + libxl_usbctrlinfo usbctrlinfo; + int numctrl, i, j, 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"); + + libxl_usbctrlinfo_init(&usbctrlinfo); + + if (!libxl_device_usbctrl_getinfo(ctx, domid, + &usbctrls[i], &usbctrlinfo)) { + printf("%-6d %-6s %-3d %-5d %-7d %-5d %-30s\n", + usbctrlinfo.devid, + libxl_usbctrl_type_to_string(usbctrlinfo.type), + usbctrlinfo.backend_id, usbctrlinfo.state, + usbctrlinfo.version, usbctrlinfo.ports, + usbctrlinfo.backend); + + for (j = 1; j <= usbctrlinfo.ports; j++) { + libxl_device_usbdev usbdev; + + libxl_device_usbdev_init(&usbdev); + + printf(" Port %d:", j); + + if (!libxl_ctrlport_to_device_usbdev(ctx, domid, + usbctrlinfo.devid, + j, &usbdev)) { + printf(" Bus %03x Device %03x\n", + usbdev.u.hostdev.hostbus, + usbdev.u.hostdev.hostaddr); + } else { + printf("\n"); + } + + libxl_device_usbdev_dispose(&usbdev); + } + } + + libxl_usbctrlinfo_dispose(&usbctrlinfo); + } + + libxl_device_usbctrl_list_free(usbctrls, numctrl); + 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 fdc1ac6..b14b881 100644 --- a/tools/libxl/xl_cmdtable.c +++ b/tools/libxl/xl_cmdtable.c @@ -553,6 +553,31 @@ struct cmd_spec cmd_table[] = { }, #endif + { "usbctrl-attach", + &main_usbctrl_attach, 0, 1, + "Create a virtual USB controller for a domain", + " [type=pv] [version=] [ports=]", + }, + { "usbctrl-detach", + &main_usbctrl_detach, 0, 1, + "Remove the virtual USB controller specified by for a domain", + " ", + }, + { "usbdev-attach", + &main_usbdev_attach, 0, 1, + "Attach a USB device to a domain", + " hostbus= hostaddr= [controller= [port=]]", + }, + { "usbdev-detach", + &main_usbdev_detach, 0, 1, + "Detach a USB device from a domain", + " ", + }, + { "usb-list", + &main_usblist, 0, 0, + "List information about all USB controllers and devices for a domain", + "", + }, }; int cmdtable_len = sizeof(cmd_table)/sizeof(struct cmd_spec);