6d3351
From 7667b332ec942112270da554ded58658a78860b9 Mon Sep 17 00:00:00 2001
6d3351
Message-Id: <7667b332ec942112270da554ded58658a78860b9@dist-git>
6d3351
From: =?UTF-8?q?J=C3=A1n=20Tomko?= <jtomko@redhat.com>
6d3351
Date: Fri, 9 Jun 2017 12:49:00 +0200
6d3351
Subject: [PATCH] Add virtio-related options to interfaces
6d3351
MIME-Version: 1.0
6d3351
Content-Type: text/plain; charset=UTF-8
6d3351
Content-Transfer-Encoding: 8bit
6d3351
6d3351
<interface type='user'>
6d3351
  <mac address='52:54:56:5a:5c:5e'/>
6d3351
  <model type='virtio'/>
6d3351
  <driver iommu='on' ats='on'/>
6d3351
</interface>
6d3351
6d3351
https://bugzilla.redhat.com/show_bug.cgi?id=1283251
6d3351
6d3351
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
6d3351
(cherry picked from commit fd518643402d8233ceffe4ef28279bcce53284f6)
6d3351
Signed-off-by: Ján Tomko <jtomko@redhat.com>
6d3351
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
6d3351
---
6d3351
 docs/formatdomain.html.in                          |  19 ++++
6d3351
 docs/schemas/domaincommon.rng                      |  12 ++
6d3351
 src/conf/domain_conf.c                             | 121 +++++++++++++++++++++
6d3351
 src/conf/domain_conf.h                             |  10 ++
6d3351
 .../qemuxml2argv-virtio-options.xml                |   1 +
6d3351
 5 files changed, 163 insertions(+)
6d3351
6d3351
diff --git a/docs/formatdomain.html.in b/docs/formatdomain.html.in
6d3351
index e8a3367bac..23546daae9 100644
6d3351
--- a/docs/formatdomain.html.in
6d3351
+++ b/docs/formatdomain.html.in
6d3351
@@ -3448,6 +3448,19 @@
6d3351
       
6d3351
     
6d3351
 
6d3351
+    

Virtio-related options

6d3351
+
6d3351
+    

6d3351
+      QEMU's virtio devices have some attributes related to the virtio transport under
6d3351
+      the driver element:
6d3351
+      The iommu attribute enables the use of emulated IOMMU
6d3351
+      by the device. The attribute ats controls the Address
6d3351
+      Translation Service support for PCIe devices. This is needed to make use
6d3351
+      of IOTLB support (see IOMMU device).
6d3351
+      Possible values are on or off.
6d3351
+      Since 3.5.0
6d3351
+    

6d3351
+
6d3351
     

Controllers

6d3351
 
6d3351
     

6d3351
@@ -5139,6 +5152,12 @@ qemu-kvm -net nic,model=? /dev/null
6d3351
         In general you should leave this option alone, unless you
6d3351
         are very certain you know what you are doing.
6d3351
       
6d3351
+      
virtio options
6d3351
+      
6d3351
+        For virtio interfaces,
6d3351
+        Virtio-specific options can also be
6d3351
+        set. (Since 3.5.0)
6d3351
+      
6d3351
     
6d3351
     

6d3351
       Offloading options for the host and guest can be configured using
6d3351
diff --git a/docs/schemas/domaincommon.rng b/docs/schemas/domaincommon.rng
6d3351
index 1b66362f17..9e68bbc52d 100644
6d3351
--- a/docs/schemas/domaincommon.rng
6d3351
+++ b/docs/schemas/domaincommon.rng
6d3351
@@ -2632,6 +2632,7 @@
6d3351
               </optional>
6d3351
             </group>
6d3351
           </choice>
6d3351
+          <ref name="virtioOptions"/>
6d3351
           <interleave>
6d3351
             <optional>
6d3351
               <element name='host'>
6d3351
@@ -4952,6 +4953,17 @@
6d3351
     </element>
6d3351
   </define>
6d3351
 
6d3351
+  <define name="virtioOptions">
6d3351
+    <optional>
6d3351
+      <attribute name="iommu">
6d3351
+        <ref name="virOnOff"/>
6d3351
+      </attribute>
6d3351
+      <attribute name="ats">
6d3351
+        <ref name="virOnOff"/>
6d3351
+      </attribute>
6d3351
+    </optional>
6d3351
+  </define>
6d3351
+
6d3351
   <define name="usbmaster">
6d3351
     <element name="master">
6d3351
       <attribute name="startport">
6d3351
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
6d3351
index 4652e1c72b..bba50cf3fd 100644
6d3351
--- a/src/conf/domain_conf.c
6d3351
+++ b/src/conf/domain_conf.c
6d3351
@@ -1112,6 +1112,46 @@ virDomainXMLOptionGetNamespace(virDomainXMLOptionPtr xmlopt)
6d3351
     return &xmlopt->ns;
6d3351
 }
6d3351
 
6d3351
+static int
6d3351
+virDomainVirtioOptionsParseXML(xmlXPathContextPtr ctxt,
6d3351
+                               virDomainVirtioOptionsPtr *virtio)
6d3351
+{
6d3351
+    char *str = NULL;
6d3351
+    int ret = -1;
6d3351
+    int val;
6d3351
+    virDomainVirtioOptionsPtr res;
6d3351
+
6d3351
+    if (VIR_ALLOC(*virtio) < 0)
6d3351
+        return -1;
6d3351
+
6d3351
+    res = *virtio;
6d3351
+
6d3351
+    if ((str = virXPathString("string(./driver/@iommu)", ctxt))) {
6d3351
+        if ((val = virTristateSwitchTypeFromString(str)) <= 0) {
6d3351
+            virReportError(VIR_ERR_XML_ERROR, "%s",
6d3351
+                           _("invalid iommu value"));
6d3351
+            goto cleanup;
6d3351
+        }
6d3351
+        res->iommu = val;
6d3351
+    }
6d3351
+    VIR_FREE(str);
6d3351
+
6d3351
+    if ((str = virXPathString("string(./driver/@ats)", ctxt))) {
6d3351
+        if ((val = virTristateSwitchTypeFromString(str)) <= 0) {
6d3351
+            virReportError(VIR_ERR_XML_ERROR, "%s",
6d3351
+                           _("invalid ats value"));
6d3351
+            goto cleanup;
6d3351
+        }
6d3351
+        res->ats = val;
6d3351
+    }
6d3351
+
6d3351
+    ret = 0;
6d3351
+
6d3351
+ cleanup:
6d3351
+    VIR_FREE(str);
6d3351
+    return ret;
6d3351
+}
6d3351
+
6d3351
 
6d3351
 virSaveCookieCallbacksPtr
6d3351
 virDomainXMLOptionGetSaveCookie(virDomainXMLOptionPtr xmlopt)
6d3351
@@ -1953,6 +1993,7 @@ virDomainNetDefClear(virDomainNetDefPtr def)
6d3351
     VIR_FREE(def->ifname);
6d3351
     VIR_FREE(def->ifname_guest);
6d3351
     VIR_FREE(def->ifname_guest_actual);
6d3351
+    VIR_FREE(def->virtio);
6d3351
 
6d3351
     virNetDevIPInfoClear(&def->guestIP);
6d3351
     virNetDevIPInfoClear(&def->hostIP);
6d3351
@@ -4313,6 +4354,28 @@ virDomainHostdevDefPostParse(virDomainHostdevDefPtr dev,
6d3351
 
6d3351
 
6d3351
 static int
6d3351
+virDomainCheckVirtioOptions(virDomainVirtioOptionsPtr virtio)
6d3351
+{
6d3351
+    if (!virtio)
6d3351
+        return 0;
6d3351
+
6d3351
+    if (virtio->iommu != VIR_TRISTATE_SWITCH_ABSENT) {
6d3351
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
6d3351
+                       _("iommu driver option is only supported "
6d3351
+                         "for virtio devices"));
6d3351
+        return -1;
6d3351
+    }
6d3351
+    if (virtio->ats != VIR_TRISTATE_SWITCH_ABSENT) {
6d3351
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
6d3351
+                       _("ats driver option is only supported "
6d3351
+                         "for virtio devices"));
6d3351
+        return -1;
6d3351
+    }
6d3351
+    return 0;
6d3351
+}
6d3351
+
6d3351
+
6d3351
+static int
6d3351
 virDomainDeviceDefPostParseInternal(virDomainDeviceDefPtr dev,
6d3351
                                     const virDomainDef *def,
6d3351
                                     virCapsPtr caps ATTRIBUTE_UNUSED,
6d3351
@@ -4410,6 +4473,13 @@ virDomainDeviceDefPostParseInternal(virDomainDeviceDefPtr dev,
6d3351
         }
6d3351
     }
6d3351
 
6d3351
+    if (dev->type == VIR_DOMAIN_DEVICE_NET) {
6d3351
+        virDomainNetDefPtr net = dev->data.net;
6d3351
+        if (STRNEQ_NULLABLE(net->model, "virtio") &&
6d3351
+            virDomainCheckVirtioOptions(net->virtio) < 0)
6d3351
+            return -1;
6d3351
+    }
6d3351
+
6d3351
     return 0;
6d3351
 }
6d3351
 
6d3351
@@ -5208,6 +5278,24 @@ virDomainDefValidate(virDomainDefPtr def,
6d3351
 }
6d3351
 
6d3351
 
6d3351
+static void
6d3351
+virDomainVirtioOptionsFormat(virBufferPtr buf,
6d3351
+                             virDomainVirtioOptionsPtr virtio)
6d3351
+{
6d3351
+    if (!virtio)
6d3351
+        return;
6d3351
+
6d3351
+    if (virtio->iommu != VIR_TRISTATE_SWITCH_ABSENT) {
6d3351
+        virBufferAsprintf(buf, " iommu='%s'",
6d3351
+                          virTristateSwitchTypeToString(virtio->iommu));
6d3351
+    }
6d3351
+    if (virtio->ats != VIR_TRISTATE_SWITCH_ABSENT) {
6d3351
+        virBufferAsprintf(buf, " ats='%s'",
6d3351
+                          virTristateSwitchTypeToString(virtio->ats));
6d3351
+    }
6d3351
+}
6d3351
+
6d3351
+
6d3351
 /* Generate a string representation of a device address
6d3351
  * @info address Device address to stringify
6d3351
  */
6d3351
@@ -10354,6 +10442,9 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt,
6d3351
             goto error;
6d3351
     }
6d3351
 
6d3351
+    if (virDomainVirtioOptionsParseXML(ctxt, &def->virtio) < 0)
6d3351
+        goto error;
6d3351
+
6d3351
  cleanup:
6d3351
     ctxt->node = oldnode;
6d3351
     VIR_FREE(macaddr);
6d3351
@@ -18979,6 +19070,30 @@ virDomainDeviceInfoCheckABIStability(virDomainDeviceInfoPtr src,
6d3351
 
6d3351
 
6d3351
 static bool
6d3351
+virDomainVirtioOptionsCheckABIStability(virDomainVirtioOptionsPtr src,
6d3351
+                                        virDomainVirtioOptionsPtr dst)
6d3351
+{
6d3351
+    if (src->iommu != dst->iommu) {
6d3351
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
6d3351
+                       _("Target device iommu option '%s' does not "
6d3351
+                         "match source '%s'"),
6d3351
+                       virTristateSwitchTypeToString(dst->iommu),
6d3351
+                       virTristateSwitchTypeToString(src->iommu));
6d3351
+        return false;
6d3351
+    }
6d3351
+    if (src->ats != dst->ats) {
6d3351
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
6d3351
+                       _("Target device ats option '%s' does not "
6d3351
+                         "match source '%s'"),
6d3351
+                       virTristateSwitchTypeToString(dst->ats),
6d3351
+                       virTristateSwitchTypeToString(src->ats));
6d3351
+        return false;
6d3351
+    }
6d3351
+    return true;
6d3351
+}
6d3351
+
6d3351
+
6d3351
+static bool
6d3351
 virDomainDiskDefCheckABIStability(virDomainDiskDefPtr src,
6d3351
                                   virDomainDiskDefPtr dst)
6d3351
 {
6d3351
@@ -19137,6 +19252,10 @@ virDomainNetDefCheckABIStability(virDomainNetDefPtr src,
6d3351
         return false;
6d3351
     }
6d3351
 
6d3351
+    if (src->virtio && dst->virtio &&
6d3351
+        !virDomainVirtioOptionsCheckABIStability(src->virtio, dst->virtio))
6d3351
+        return false;
6d3351
+
6d3351
     if (!virDomainDeviceInfoCheckABIStability(&src->info, &dst->info))
6d3351
         return false;
6d3351
 
6d3351
@@ -22074,6 +22193,8 @@ virDomainVirtioNetDriverFormat(char **outstr,
6d3351
         virBufferAsprintf(&buf, " rx_queue_size='%u'",
6d3351
                           def->driver.virtio.rx_queue_size);
6d3351
 
6d3351
+    virDomainVirtioOptionsFormat(&buf, def->virtio);
6d3351
+
6d3351
     if (virBufferCheckError(&buf) < 0)
6d3351
         return -1;
6d3351
 
6d3351
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
6d3351
index e6c20a9e1e..603d35bd50 100644
6d3351
--- a/src/conf/domain_conf.h
6d3351
+++ b/src/conf/domain_conf.h
6d3351
@@ -156,6 +156,9 @@ typedef virDomainTPMDef *virDomainTPMDefPtr;
6d3351
 typedef struct _virDomainIOMMUDef virDomainIOMMUDef;
6d3351
 typedef virDomainIOMMUDef *virDomainIOMMUDefPtr;
6d3351
 
6d3351
+typedef struct _virDomainVirtioOptions virDomainVirtioOptions;
6d3351
+typedef virDomainVirtioOptions *virDomainVirtioOptionsPtr;
6d3351
+
6d3351
 /* Flags for the 'type' field in virDomainDeviceDef */
6d3351
 typedef enum {
6d3351
     VIR_DOMAIN_DEVICE_NONE = 0,
6d3351
@@ -1040,6 +1043,7 @@ struct _virDomainNetDef {
6d3351
     int linkstate;
6d3351
     unsigned int mtu;
6d3351
     virNetDevCoalescePtr coalesce;
6d3351
+    virDomainVirtioOptionsPtr virtio;
6d3351
 };
6d3351
 
6d3351
 typedef enum {
6d3351
@@ -2215,6 +2219,12 @@ struct _virDomainIOMMUDef {
6d3351
     virTristateSwitch eim;
6d3351
     virTristateSwitch iotlb;
6d3351
 };
6d3351
+
6d3351
+struct _virDomainVirtioOptions {
6d3351
+    virTristateSwitch iommu;
6d3351
+    virTristateSwitch ats;
6d3351
+};
6d3351
+
6d3351
 /*
6d3351
  * Guest VM main configuration
6d3351
  *
6d3351
diff --git a/tests/qemuxml2argvdata/qemuxml2argv-virtio-options.xml b/tests/qemuxml2argvdata/qemuxml2argv-virtio-options.xml
6d3351
index c88cf649b1..3357bc6d1b 100644
6d3351
--- a/tests/qemuxml2argvdata/qemuxml2argv-virtio-options.xml
6d3351
+++ b/tests/qemuxml2argvdata/qemuxml2argv-virtio-options.xml
6d3351
@@ -47,6 +47,7 @@
6d3351
     <interface type='user'>
6d3351
       <mac address='52:54:56:58:5a:5c'/>
6d3351
       <model type='virtio'/>
6d3351
+      <driver iommu='on' ats='on'/>
6d3351
       <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
6d3351
     </interface>
6d3351
     <input type='mouse' bus='virtio'>
6d3351
-- 
6d3351
2.13.1
6d3351