]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
nodedev: add support for mdev attributes
authorJonathon Jongsma <jjongsma@redhat.com>
Thu, 18 Jun 2020 21:05:56 +0000 (16:05 -0500)
committerErik Skultety <eskultet@redhat.com>
Fri, 19 Jun 2020 08:39:54 +0000 (10:39 +0200)
Mediated devices support arbitrary vendor-specific attributes that can
be attached to a mediated device. These attributes are ordered, and are
written to sysfs in order after a device is created. This patch adds
support for these attributes to the mdev data types and XML schema.

Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
docs/formatnode.html.in
docs/schemas/nodedev.rng
src/conf/node_device_conf.c
src/conf/node_device_conf.h
src/libvirt_private.syms
src/util/virmdev.c
src/util/virmdev.h

index 4ed43ec0cb1a6bba7104dbbd1826febd34239765..0637d457ee2a2ed1e156f0304a17601bace666a2 100644 (file)
                 belongs. This is a read-only field that is reported by the
                 device driver.
               </dd>
+              <dt><code>attr</code></dt>
+              <dd>
+                This optional element can occur multiple times. It represents a
+                vendor-specific attribute that is used to configure this
+                mediated device. It has two required attributes:
+                <code>name</code> and <code>value</code>.
+              </dd>
             </dl>
           </dd>
           <dt><code>ccw</code></dt>
index ca3a79db87ed5b6d232a92e0f62491a49bc7f404..4b2b350fd8e70e08eb7b2d5d3af74a753d935632 100644 (file)
         </attribute>
       </element>
     </optional>
+    <zeroOrMore>
+      <element name="attr">
+        <attribute name="name"/>
+        <attribute name="value"/>
+      </element>
+    </zeroOrMore>
   </define>
 
   <define name='capccwdev'>
index 2ef4514f05e2afce04e0e743758cd5a563249fc3..623a2cb79c3d6814cdb0acc235a1e999eeef6d99 100644 (file)
@@ -500,6 +500,22 @@ virNodeDeviceCapStorageDefFormat(virBufferPtr buf,
         virBufferAddLit(buf, "<capability type='hotpluggable'/>\n");
 }
 
+static void
+virNodeDeviceCapMdevDefFormat(virBufferPtr buf,
+                              const virNodeDevCapData *data)
+{
+    size_t i;
+
+    virBufferEscapeString(buf, "<type id='%s'/>\n", data->mdev.type);
+    virBufferAsprintf(buf, "<iommuGroup number='%u'/>\n",
+                      data->mdev.iommuGroupNumber);
+
+    for (i = 0; i < data->mdev.nattributes; i++) {
+        virMediatedDeviceAttrPtr attr = data->mdev.attributes[i];
+        virBufferAsprintf(buf, "<attr name='%s' value='%s'/>\n",
+                          attr->name, attr->value);
+    }
+}
 
 char *
 virNodeDeviceDefFormat(const virNodeDeviceDef *def)
@@ -583,9 +599,7 @@ virNodeDeviceDefFormat(const virNodeDeviceDef *def)
             virBufferEscapeString(&buf, "<type>%s</type>\n", virNodeDevDRMTypeToString(data->drm.type));
             break;
         case VIR_NODE_DEV_CAP_MDEV:
-            virBufferEscapeString(&buf, "<type id='%s'/>\n", data->mdev.type);
-            virBufferAsprintf(&buf, "<iommuGroup number='%u'/>\n",
-                              data->mdev.iommuGroupNumber);
+            virNodeDeviceCapMdevDefFormat(&buf, data);
             break;
         case VIR_NODE_DEV_CAP_CCW_DEV:
             virBufferAsprintf(&buf, "<cssid>0x%x</cssid>\n",
@@ -1757,6 +1771,27 @@ virNodeDevCapSystemParseXML(xmlXPathContextPtr ctxt,
     return ret;
 }
 
+static int
+virNodeDevCapMdevAttributeParseXML(xmlXPathContextPtr ctxt,
+                                   xmlNodePtr node,
+                                   virNodeDevCapMdevPtr mdev)
+{
+    VIR_XPATH_NODE_AUTORESTORE(ctxt);
+    g_autoptr(virMediatedDeviceAttr) attr = virMediatedDeviceAttrNew();
+
+    ctxt->node = node;
+    attr->name = virXPathString("string(./@name)", ctxt);
+    attr->value = virXPathString("string(./@value)", ctxt);
+    if (!attr->name || !attr->value) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("mdev attribute missing name or value"));
+        return -1;
+    }
+
+    return VIR_APPEND_ELEMENT(mdev->attributes,
+                              mdev->nattributes,
+                              attr);
+}
 
 static int
 virNodeDevCapMdevParseXML(xmlXPathContextPtr ctxt,
@@ -1766,6 +1801,9 @@ virNodeDevCapMdevParseXML(xmlXPathContextPtr ctxt,
 {
     VIR_XPATH_NODE_AUTORESTORE(ctxt);
     int ret = -1;
+    int nattrs = 0;
+    g_autofree xmlNodePtr *attrs = NULL;
+    size_t i;
 
     ctxt->node = node;
 
@@ -1785,6 +1823,12 @@ virNodeDevCapMdevParseXML(xmlXPathContextPtr ctxt,
         goto out;
     }
 
+    if ((nattrs = virXPathNodeSet("./attr", ctxt, &attrs)) < 0)
+        goto out;
+
+    for (i = 0; i < nattrs; i++)
+        virNodeDevCapMdevAttributeParseXML(ctxt, attrs[i], mdev);
+
     ret = 0;
  out:
     return ret;
@@ -2174,6 +2218,9 @@ virNodeDevCapsDefFree(virNodeDevCapsDefPtr caps)
         break;
     case VIR_NODE_DEV_CAP_MDEV:
         VIR_FREE(data->mdev.type);
+        for (i = 0; i < data->mdev.nattributes; i++)
+            virMediatedDeviceAttrFree(data->mdev.attributes[i]);
+        VIR_FREE(data->mdev.attributes);
         break;
     case VIR_NODE_DEV_CAP_MDEV_TYPES:
     case VIR_NODE_DEV_CAP_DRM:
index 9e4b0847fbb3903d4558fc7204f05bf69bd69dfa..e3e1e788d4d3d68256b44614d80178b4e684d552 100644 (file)
@@ -141,6 +141,8 @@ typedef virNodeDevCapMdev *virNodeDevCapMdevPtr;
 struct _virNodeDevCapMdev {
     char *type;
     unsigned int iommuGroupNumber;
+    virMediatedDeviceAttrPtr *attributes;
+    size_t nattributes;
 };
 
 typedef struct _virNodeDevCapPCIDev virNodeDevCapPCIDev;
index 284c6c3880328d51999ecab9c35fde2c50bf124a..22bd8b9c175b6616982557100736c559aa362b21 100644 (file)
@@ -2487,6 +2487,8 @@ virMacMapRemove;
 virMacMapWriteFile;
 
 # util/virmdev.h
+virMediatedDeviceAttrFree;
+virMediatedDeviceAttrNew;
 virMediatedDeviceFree;
 virMediatedDeviceGetIOMMUGroupDev;
 virMediatedDeviceGetIOMMUGroupNum;
index 51a88a91d7b357bdc75e555d824224b364f4bfbe..b8023dd99116d00c50d495c5d6f8ef91bb0a4b08 100644 (file)
@@ -512,3 +512,15 @@ virMediatedDeviceTypeReadAttrs(const char *sysfspath,
 
     return 0;
 }
+
+virMediatedDeviceAttrPtr virMediatedDeviceAttrNew(void)
+{
+    return g_new0(virMediatedDeviceAttr, 1);
+}
+
+void virMediatedDeviceAttrFree(virMediatedDeviceAttrPtr attr)
+{
+    g_free(attr->name);
+    g_free(attr->value);
+    g_free(attr);
+}
index 51f7f608a2ae77c28cceb3c32c7fc16069e17294..eb167ccb481f93a27436742fdca156481647af05 100644 (file)
@@ -37,6 +37,17 @@ typedef struct _virMediatedDevice virMediatedDevice;
 typedef virMediatedDevice *virMediatedDevicePtr;
 typedef struct _virMediatedDeviceList virMediatedDeviceList;
 typedef virMediatedDeviceList *virMediatedDeviceListPtr;
+typedef struct _virMediatedDeviceAttr virMediatedDeviceAttr;
+typedef virMediatedDeviceAttr *virMediatedDeviceAttrPtr;
+
+struct _virMediatedDeviceAttr {
+    char *name;
+    char *value;
+};
+
+virMediatedDeviceAttrPtr virMediatedDeviceAttrNew(void);
+void virMediatedDeviceAttrFree(virMediatedDeviceAttrPtr attr);
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(virMediatedDeviceAttr, virMediatedDeviceAttrFree);
 
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virMediatedDeviceList, virObjectUnref);