[RFC 12/12] Add usb-redir device

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

 



---
 docs/formatdomain.html.in                          |   15 ++++-
 docs/schemas/domain.rng                            |   66 ++++++++++++--------
 src/conf/domain_conf.c                             |   49 +++++++++++++--
 src/conf/domain_conf.h                             |    2 +
 src/qemu/qemu_command.c                            |   36 +++++++++--
 tests/qemuxml2argvdata/qemuxml2argv-usb-redir.args |    8 +++
 tests/qemuxml2argvdata/qemuxml2argv-usb-redir.xml  |   33 ++++++++++
 tests/qemuxml2argvtest.c                           |    5 ++
 8 files changed, 174 insertions(+), 40 deletions(-)
 create mode 100644 tests/qemuxml2argvdata/qemuxml2argv-usb-redir.args
 create mode 100644 tests/qemuxml2argvdata/qemuxml2argv-usb-redir.xml

diff --git a/docs/formatdomain.html.in b/docs/formatdomain.html.in
index 7b04972..c702075 100644
--- a/docs/formatdomain.html.in
+++ b/docs/formatdomain.html.in
@@ -1312,6 +1312,12 @@
       0.4.4 for USB and 0.6.0 for PCI (KVM only)</span>:
     </p>
 
+    <p>
+      Device redirection through a character device is
+      supported <span class="since">since after 0.9.5 for USB (KVM
+      only)</span>:
+    </p>
+
 <pre>
   ...
   &lt;devices&gt;
@@ -1346,14 +1352,19 @@
         "subsystem" and <code>type</code> is "usb" for a USB device and "pci"
         for a PCI device. When <code>managed</code> is "yes" for a PCI
         device, it is detached from the host before being passed on to
-        the guest.</dd>
+        the guest. Redirection through a character device is enable by
+        specifying the <code>redirection</code> character device
+        type.</dd>
       <dt><code>source</code></dt>
       <dd>The source element describes the device as seen from the host.
       The USB device can either be addressed by vendor / product id using the
       <code>vendor</code> and <code>product</code> elements or by the device's
       address on the hosts using the <code>address</code> element.
       PCI devices on the other hand can only be described by their
-      <code>address</code></dd>
+      <code>address</code>
+      In case of device redirection, the source element describes the
+      character device to redirect from.
+      </dd>
       <dt><code>vendor</code>, <code>product</code></dt>
       <dd>The <code>vendor</code> and <code>product</code> elements each have an
       <code>id</code> attribute that specifies the USB vendor and product id.
diff --git a/docs/schemas/domain.rng b/docs/schemas/domain.rng
index e17cc7a..ce2ae52 100644
--- a/docs/schemas/domain.rng
+++ b/docs/schemas/domain.rng
@@ -1722,21 +1722,25 @@
     </element>
   </define>
 
+  <define name="qemucdevSrcTypeChoice">
+    <choice>
+      <value>dev</value>
+      <value>file</value>
+      <value>pipe</value>
+      <value>unix</value>
+      <value>tcp</value>
+      <value>udp</value>
+      <value>null</value>
+      <value>stdio</value>
+      <value>vc</value>
+      <value>pty</value>
+      <value>spicevmc</value>
+    </choice>
+  </define>
+
   <define name="qemucdevSrcType">
     <attribute name="type">
-      <choice>
-        <value>dev</value>
-        <value>file</value>
-        <value>pipe</value>
-        <value>unix</value>
-        <value>tcp</value>
-        <value>udp</value>
-        <value>null</value>
-        <value>stdio</value>
-        <value>vc</value>
-        <value>pty</value>
-        <value>spicevmc</value>
-      </choice>
+      <ref name="qemucdevSrcTypeChoice"/>
     </attribute>
   </define>
   <define name="qemucdevSrcDef">
@@ -1997,21 +2001,29 @@
           </choice>
         </attribute>
       </optional>
+      <optional>
+        <attribute name="redirection">
+          <ref name="qemucdevSrcTypeChoice"/>
+        </attribute>
+      </optional>
       <group>
-        <element name="source">
-          <choice>
-            <group>
-              <ref name="usbproduct"/>
-              <optional>
-                <ref name="usbaddress"/>
-              </optional>
-            </group>
-            <ref name="usbaddress"/>
-            <element name="address">
-              <ref name="pciaddress"/>
-            </element>
-          </choice>
-        </element>
+        <choice>
+          <ref name="qemucdevSrcDef"/>
+          <element name="source">
+            <choice>
+              <group>
+                <ref name="usbproduct"/>
+                <optional>
+                  <ref name="usbaddress"/>
+                </optional>
+              </group>
+              <ref name="usbaddress"/>
+              <element name="address">
+                <ref name="pciaddress"/>
+              </element>
+            </choice>
+          </element>
+        </choice>
       </group>
       <optional>
         <ref name="deviceBoot"/>
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 6362ef3..046a2b9 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -5159,18 +5159,26 @@ error:
 
 static int
 virDomainHostdevSubsysUsbDefParseXML(const xmlNodePtr node,
-                                     virDomainHostdevDefPtr def)
+                                     virDomainHostdevDefPtr def,
+                                     unsigned int flags)
 {
 
     int ret = -1;
     int got_product, got_vendor;
     xmlNodePtr cur;
+    int remaining;
 
     /* Product can validly be 0, so we need some extra help to determine
      * if it is uninitialized*/
     got_product = 0;
     got_vendor = 0;
 
+    if (def->redirection) {
+        remaining = virDomainChrSourceDefParseXML(&def->source.subsys.u.chr, node, flags);
+        if (remaining < 0)
+            goto out;
+    }
+
     cur = node->children;
     while (cur != NULL) {
         if (cur->type == XML_ELEMENT_NODE) {
@@ -5335,7 +5343,7 @@ virDomainHostdevDefParseXML(const xmlNodePtr node,
 
     xmlNodePtr cur;
     virDomainHostdevDefPtr def;
-    char *mode, *type = NULL, *managed = NULL;
+    char *mode, *type = NULL, *managed = NULL, *redirection = NULL;
 
     if (VIR_ALLOC(def) < 0) {
         virReportOOMError();
@@ -5373,14 +5381,30 @@ virDomainHostdevDefParseXML(const xmlNodePtr node,
         VIR_FREE(managed);
     }
 
+    redirection = virXMLPropString(node, "redirection");
+    if (redirection != NULL) {
+        def->redirection = 1;
+        if ((def->source.subsys.u.chr.type = virDomainChrTypeFromString(redirection)) < 0) {
+            virDomainReportError(VIR_ERR_INTERNAL_ERROR,
+                                 _("unknown redirection character device type '%s'"),
+                                 redirection);
+            goto error;
+        }
+        if (def->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) {
+            virDomainReportError(VIR_ERR_INTERNAL_ERROR,
+                                 _("only usb redirection is supported"));
+            goto error;
+        }
+    }
+
     cur = node->children;
     while (cur != NULL) {
         if (cur->type == XML_ELEMENT_NODE) {
             if (xmlStrEqual(cur->name, BAD_CAST "source")) {
                 if (def->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
                     def->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) {
-                        if (virDomainHostdevSubsysUsbDefParseXML(cur, def) < 0)
-                            goto error;
+                    if (virDomainHostdevSubsysUsbDefParseXML(cur, def, flags) < 0)
+                        goto error;
                 }
                 if (def->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
                     def->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI) {
@@ -10087,6 +10111,7 @@ virDomainHostdevDefFormat(virBufferPtr buf,
 {
     const char *mode = virDomainHostdevModeTypeToString(def->mode);
     const char *type;
+    const char *redirection = NULL;
 
     if (!mode || def->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS) {
         virDomainReportError(VIR_ERR_INTERNAL_ERROR,
@@ -10102,8 +10127,22 @@ virDomainHostdevDefFormat(virBufferPtr buf,
         return -1;
     }
 
-    virBufferAsprintf(buf, "    <hostdev mode='%s' type='%s' managed='%s'>\n",
+    if (def->redirection) {
+        redirection = virDomainChrTypeToString(def->source.subsys.u.chr.type);
+        if (!redirection) {
+            virDomainReportError(VIR_ERR_INTERNAL_ERROR,
+                                 _("unexpected redirection type %d"),
+                                 def->source.subsys.u.chr.type);
+            return -1;
+        }
+    }
+
+    virBufferAsprintf(buf, "    <hostdev mode='%s' type='%s' managed='%s'",
                       mode, type, def->managed ? "yes" : "no");
+    if (redirection != NULL) {
+        virBufferAsprintf(buf, " redirection='%s'", redirection);
+    }
+    virBufferAddLit(buf, ">\n");
     virBufferAddLit(buf, "      <source>\n");
 
     if (def->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) {
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index fa883f4..a7fcf2d 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -908,6 +908,7 @@ typedef virDomainHostdevDef *virDomainHostdevDefPtr;
 struct _virDomainHostdevDef {
     int mode; /* enum virDomainHostdevMode */
     unsigned int managed : 1;
+    unsigned int redirection : 1;
     union {
         struct {
             int type; /* enum virDomainHostdevBusType */
@@ -920,6 +921,7 @@ struct _virDomainHostdevDef {
                     unsigned product;
                 } usb;
                 virDomainDevicePCIAddress pci; /* host address */
+                virDomainChrSourceDef chr;
             } u;
         } subsys;
         struct {
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index c424ec1..586453c 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -584,12 +584,14 @@ qemuAssignDeviceNetAlias(virDomainDefPtr def, virDomainNetDefPtr net, int idx)
 int
 qemuAssignDeviceHostdevAlias(virDomainDefPtr def, virDomainHostdevDefPtr hostdev, int idx)
 {
+    const char *prefix = hostdev->redirection ? "usbredir" : "hostdev";
+
     if (idx == -1) {
         int i;
         idx = 0;
         for (i = 0 ; i < def->nhostdevs ; i++) {
             int thisidx;
-            if ((thisidx = qemuDomainDeviceAliasIndex(&def->hostdevs[i]->info, "hostdev")) < 0) {
+            if ((thisidx = qemuDomainDeviceAliasIndex(&def->hostdevs[i]->info, prefix)) < 0) {
                 qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                                 _("Unable to determine device index for hostdev device"));
                 return -1;
@@ -599,7 +601,7 @@ qemuAssignDeviceHostdevAlias(virDomainDefPtr def, virDomainHostdevDefPtr hostdev
         }
     }
 
-    if (virAsprintf(&hostdev->info.alias, "hostdev%d", idx) < 0) {
+    if (virAsprintf(&hostdev->info.alias, "%s%d", prefix, idx) < 0) {
         virReportOOMError();
         return -1;
     }
@@ -2332,10 +2334,16 @@ qemuBuildUSBHostdevDevStr(virDomainHostdevDefPtr dev,
         return NULL;
     }
 
-    virBufferAsprintf(&buf, "usb-host,hostbus=%d,hostaddr=%d,id=%s",
-                      dev->source.subsys.u.usb.bus,
-                      dev->source.subsys.u.usb.device,
-                      dev->info.alias);
+    if (dev->redirection) {
+        virBufferAsprintf(&buf, "usb-redir,chardev=char%s,id=%s",
+                          dev->info.alias,
+                          dev->info.alias);
+    } else {
+        virBufferAsprintf(&buf, "usb-host,hostbus=%d,hostaddr=%d,id=%s",
+                          dev->source.subsys.u.usb.bus,
+                          dev->source.subsys.u.usb.device,
+                          dev->info.alias);
+    }
 
     if (qemuBuildDeviceAddressStr(&buf, &dev->info, qemuCaps) < 0)
         goto error;
@@ -4834,6 +4842,22 @@ qemuBuildCommandLine(virConnectPtr conn,
         if (hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
             hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) {
 
+            if (hostdev->redirection) {
+                if (!qemuCapsGet(qemuCaps, QEMU_CAPS_USB_REDIR)) {
+                    qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                                    _("USB redirection is not supported "
+                                      "by this version of QEMU"));
+                    goto error;
+                }
+                virCommandAddArg(cmd, "-chardev");
+                if (!(devstr = qemuBuildChrChardevStr(&hostdev->source.subsys.u.chr,
+                                                      hostdev->info.alias,
+                                                      qemuCaps))) {
+                    goto error;
+                }
+                virCommandAddArg(cmd, devstr);
+            }
+
             if (qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
                 virCommandAddArg(cmd, "-device");
                 if (!(devstr = qemuBuildUSBHostdevDevStr(hostdev, qemuCaps)))
diff --git a/tests/qemuxml2argvdata/qemuxml2argv-usb-redir.args b/tests/qemuxml2argvdata/qemuxml2argv-usb-redir.args
new file mode 100644
index 0000000..661edce
--- /dev/null
+++ b/tests/qemuxml2argvdata/qemuxml2argv-usb-redir.args
@@ -0,0 +1,8 @@
+LC_ALL=C PATH=/bin HOME=/home/test USER=test LOGNAME=test /usr/bin/qemu -S -M pc -m 214 -smp 1 -nographic -nodefconfig -nodefaults -chardev socket,id=charmonitor,path=/tmp/test-monitor,server,nowait -mon chardev=charmonitor,id=monitor,mode=readline -no-acpi -boot c \
+-device ich9-usb-ehci1,id=usb,bus=pci.0,multifunction=on,addr=0x4.0x7 \
+-device ich9-usb-uhci1,id=usb1,masterbus=usb.0,firstport=0,bus=pci.0,multifunction=on,addr=0x4.0x0 \
+-device ich9-usb-uhci2,id=usb2,masterbus=usb.0,firstport=2,bus=pci.0,multifunction=on,addr=0x4.0x1 \
+-device ich9-usb-uhci3,id=usb3,masterbus=usb.0,firstport=4,bus=pci.0,multifunction=on,addr=0x4.0x2 \
+-chardev socket,id=charusbredir0,host=localhost,port=4000 \
+-device usb-redir,chardev=charusbredir0,id=usbredir0 \
+-device virtio-balloon-pci,id=balloon0,bus=pci.0,multifunction=on,addr=0x3.0x0
diff --git a/tests/qemuxml2argvdata/qemuxml2argv-usb-redir.xml b/tests/qemuxml2argvdata/qemuxml2argv-usb-redir.xml
new file mode 100644
index 0000000..6393ac4
--- /dev/null
+++ b/tests/qemuxml2argvdata/qemuxml2argv-usb-redir.xml
@@ -0,0 +1,33 @@
+<domain type='qemu'>
+  <name>QEMUGuest1</name>
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
+  <memory>219136</memory>
+  <currentMemory>219200</currentMemory>
+  <vcpu>1</vcpu>
+  <os>
+    <type arch='i686' machine='pc'>hvm</type>
+    <boot dev='hd'/>
+  </os>
+  <devices>
+    <emulator>/usr/bin/qemu</emulator>
+    <controller type='usb' index='0' model='ich9-ehci1'>
+      <address type='pci' domain='0' bus='0' slot='4' function='7'/>
+    </controller>
+    <controller type='usb' index='1' model='ich9-uhci1'>
+      <master bus='0' startport='0'/>
+      <address type='pci' domain='0' bus='0' slot='4' function='0'/>
+    </controller>
+    <controller type='usb' index='2' model='ich9-uhci2'>
+      <master bus='0' startport='2'/>
+      <address type='pci' domain='0' bus='0' slot='4' function='1'/>
+    </controller>
+    <controller type='usb' index='3' model='ich9-uhci3'>
+      <master bus='0' startport='4'/>
+      <address type='pci' domain='0' bus='0' slot='4' function='2'/>
+    </controller>
+    <hostdev mode='subsystem' type='usb' redirection='tcp'>
+      <source mode='connect' host='localhost' service='4000'/>
+    </hostdev>
+    <memballoon model='virtio'/>
+  </devices>
+</domain>
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
index 923937b..f848d0e 100644
--- a/tests/qemuxml2argvtest.c
+++ b/tests/qemuxml2argvtest.c
@@ -512,6 +512,11 @@ mymain(void)
             QEMU_CAPS_USB_HUB, QEMU_CAPS_ICH9_USB_EHCI1,
             QEMU_CAPS_ICH9_USB_UHCI1, QEMU_CAPS_ICH9_USB_UHCI2,
             QEMU_CAPS_ICH9_USB_UHCI3);
+    DO_TEST("usb-redir", false,
+            QEMU_CAPS_CHARDEV, QEMU_CAPS_DEVICE, QEMU_CAPS_NODEFCONFIG,
+            QEMU_CAPS_PCI_MULTIFUNCTION, QEMU_CAPS_USB_HUB, QEMU_CAPS_ICH9_USB_EHCI1,
+            QEMU_CAPS_ICH9_USB_UHCI1, QEMU_CAPS_ICH9_USB_UHCI2,
+            QEMU_CAPS_ICH9_USB_UHCI3, QEMU_CAPS_USB_REDIR);
 
     DO_TEST("smbios", false, QEMU_CAPS_SMBIOS_TYPE);
 
-- 
1.7.6

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]