[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH 2/2] xl: Add hvm-host-usb-add and hvm-host-usb-del commands



Add commands to add and remove host USB to HVM guests.

Signed-off-by: George Dunlap <george.dunlap@xxxxxxxxxxxxx>
---
 docs/man/xl.pod.1         |   45 ++++++++++
 tools/libxl/xl.h          |    2 +
 tools/libxl/xl_cmdimpl.c  |  208 +++++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/xl_cmdtable.c |   10 +++
 4 files changed, 265 insertions(+)

diff --git a/docs/man/xl.pod.1 b/docs/man/xl.pod.1
index a0e298e..9c582e9 100644
--- a/docs/man/xl.pod.1
+++ b/docs/man/xl.pod.1
@@ -1110,6 +1110,51 @@ List virtual network interfaces for a domain.
 
 =back
 
+=head2 HVM DEVICES
+
+=over 4
+
+=item B<hvm-host-usb-add> [I<-i devname>] I<domain-id> I<device-specifier>
+
+Passes through the host USB device specified by I<device-specifier> to the 
+HVM domain I<domain-id>.  Device-specifier can be one of the following:
+
+=over 4
+
+=item <hostbus>.<hostaddr>
+
+=item <vendorid>:<productid>
+
+=item <hostbus>.<hostaddr>:<vendorid>:<productid>
+
+=back
+
+The best way to find out the information for the device is typically using
+lsusb.
+
+Using the I<-i> option, you can specify a I<devname> for the
+device. This is an arbitrary string that can be used by
+I<hvm-host-usb-del> to remove the device later.  If no name is
+specified, then one will be chosen automatically.  In any case the
+devname will be printed to stdout if the device is successfully added.
+
+This command is only available for domains using qemu-xen, not
+qemu-traditional.
+
+=item B<hvm-host-usb-del> I<domain-id> I<devname>
+
+Remove the host USB device from I<domain-id> which is specified by I<devname>.
+This I<devname> is a string that was specified when the device was inserted
+by B<hvm-host-usb-add>.
+
+Devices specified in the config file do not have an associated
+devname, and thus cannot be removed using this command.
+
+This command is only available for domains using qemu-xen, not
+qemu-traditional.
+
+=back
+
 =head2 VTPM DEVICES
 
 =over 4
diff --git a/tools/libxl/xl.h b/tools/libxl/xl.h
index b881f92..8eb368c 100644
--- a/tools/libxl/xl.h
+++ b/tools/libxl/xl.h
@@ -35,6 +35,8 @@ 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_hvm_host_usb_add(int argc, char **argv);
+int main_hvm_host_usb_del(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);
diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c
index 2d40f8f..ca2f023 100644
--- a/tools/libxl/xl_cmdimpl.c
+++ b/tools/libxl/xl_cmdimpl.c
@@ -2584,6 +2584,214 @@ int main_cd_insert(int argc, char **argv)
     return 0;
 }
 
+
+
+static int parse_usb_specifier_hbhavipi(libxl_device_host_usb *dev, const char 
*s)
+{
+    const char * vid, *pid, *p;
+    const char * hostbus, *hostaddr;
+
+    hostbus = s;
+    hostaddr = vid = pid = NULL;
+
+#define is_dec(_c) ((_c) >= '0' && (_c) <= '9')
+#define is_hex(_c) (is_dec(_c) || ((_c) >= 'a' && (_c) <= 'f'))
+
+    /* Match [0-9]+\.[0-9]:[0-9a-f]+:[0-9a-f] */
+
+    /* First look for [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 (*p == ':') {
+            break;
+        } else if (!is_dec(*p)) {
+            return -1;
+        }
+    }
+    if ( !hostaddr || !is_dec(*hostaddr) )
+        return -1;
+    if ( *p != ':' )
+        return -1;
+
+    p++;
+    /* Now match [0-9a-f]+:[0-9a-f] */
+    vid = p;
+    if ( !is_hex(*vid) )
+        return -1;
+
+    for(; *p; p++) {
+        if(*p == ':') {
+            if ( !pid )
+                pid = p+1;
+            else
+                return -1;
+        } else if (!is_hex(*p)) {
+            return -1;
+        }
+    }
+    if (!pid || !is_hex(*pid))
+        return -1;
+
+    dev->hostbus  = strtoul(hostbus,  NULL, 10);
+    dev->hostaddr = strtoul(hostaddr, NULL, 10);
+    dev->vendorid  = strtoul(vid, NULL, 16);
+    dev->productid = strtoul(pid, NULL, 16);
+
+    return 0;
+}
+
+static int parse_usb_specifier_vipi(libxl_device_host_usb *dev, const char *s)
+{
+    const char * vid, *pid, *p;
+
+    vid = s;
+    pid = NULL;
+
+    /* Match [0-9a-f]+:[0-9a-f] */
+    if ( !is_hex(*vid) )
+        return -1;
+
+    for(p=s; *p; p++) {
+        if(*p == ':') {
+            if ( !pid )
+                pid = p+1;
+            else
+                return -1;
+        } else if (!is_hex(*p)) {
+            return -1;
+        }
+    }
+    if (!pid || !is_hex(*pid))
+        return -1;
+    dev->vendorid  = strtoul(vid, NULL, 16);
+    dev->productid = strtoul(pid, NULL, 16);
+
+    return 0;
+}
+
+static int parse_usb_specifier_hbha(libxl_device_host_usb *dev, const char *s)
+{
+    const char * hostbus, *hostaddr, *p;
+
+    hostbus = s;
+    hostaddr=NULL;
+
+    /* 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->hostbus  = strtoul(hostbus,  NULL, 10);
+    dev->hostaddr = strtoul(hostaddr, NULL, 10);
+
+    return 0;
+}
+
+static int parse_usb_specifier(libxl_device_host_usb *dev, const char *s)
+{
+    /*
+     * Acceptable formats:
+     * - hostbus.hostaddr
+     * - vendorid:productid
+     * - hostbus.hostaddr:vendorid:productid
+     */
+    if ( !parse_usb_specifier_hbha(dev, s) )
+        return 0;
+    if ( !parse_usb_specifier_vipi(dev, s) )
+        return 0;
+    if ( !parse_usb_specifier_hbhavipi(dev, s) )
+        return 0;
+
+    return -1;
+}
+
+#undef is_dec
+#undef is_hex
+
+static int hvm_host_usb_add(uint32_t domid, const char * device, char * id)
+{
+    libxl_device_host_usb usbdev;
+    int rc;
+
+    libxl_device_host_usb_init(&usbdev);
+
+    if ( id )
+        usbdev.id = strdup(id);
+
+    if ( parse_usb_specifier(&usbdev, device) < 0 )
+        return -1;
+
+    if ( (rc = libxl_hvm_host_usb_add(ctx, domid, &usbdev)) >= 0 )
+        printf("Added device with name %s\n", usbdev.id);
+
+    libxl_device_host_usb_dispose(&usbdev);
+
+    return rc;
+}
+
+int main_hvm_host_usb_add(int argc, char **argv)
+{
+    uint32_t domid;
+    int opt = 0, rc;
+    const char *device;
+    char *id = NULL;
+
+    SWITCH_FOREACH_OPT(opt, "i:", NULL, "hvm-host-usb-add", 2) {
+    case 'i':
+        id = optarg;
+        break;
+    }
+
+    domid = find_domain(argv[optind]);
+    device = argv[optind + 1];
+
+    rc = hvm_host_usb_add(domid, device, id);
+    if ( rc < 0 )
+        return -1;
+    else
+        return 0;
+}
+
+int main_hvm_host_usb_del(int argc, char **argv)
+{
+    uint32_t domid;
+    char * id;
+    int opt = 0, rc;
+
+    SWITCH_FOREACH_OPT(opt, "", NULL, "hvm-host-usb-del", 2) {
+        /* No options */
+    }
+
+    domid = find_domain(argv[optind]);
+    id = argv[optind + 1];
+
+    rc = libxl_hvm_host_usb_del(ctx, domid, id);
+    if ( rc < 0 )
+        return -1;
+    else
+        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 b4a87ca..0c57364 100644
--- a/tools/libxl/xl_cmdtable.c
+++ b/tools/libxl/xl_cmdtable.c
@@ -187,6 +187,16 @@ struct cmd_spec cmd_table[] = {
       "Eject a cdrom from a guest's cd drive",
       "<Domain> <VirtualDevice>",
     },
+    { "hvm-host-usb-add",
+      &main_hvm_host_usb_add, 1, 1,
+      "Hot-plug a host usb device to an HVM domain.",
+      "<Domain> [-i devname] (<hostbus>.<hostaddr>|<vendorid>:<productid>)",
+    },
+    { "hvm-host-usb-del",
+      &main_hvm_host_usb_del, 1, 1,
+      "Hot-unplug the named device from an HVM domain.",
+      "<Domain> <devname>",
+    },
     { "mem-max",
       &main_memmax, 0, 1,
       "Set the maximum amount reservation for a domain",
-- 
1.7.9.5


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
http://lists.xen.org/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.