]> xenbits.xensource.com Git - libvirt.git/commitdiff
conf: node_device: fix mdev_types format and XML parsing code to match schema
authorBoris Fiuczynski <fiuczy@linux.ibm.com>
Fri, 23 Oct 2020 17:31:43 +0000 (19:31 +0200)
committerJán Tomko <jtomko@redhat.com>
Wed, 4 Nov 2020 18:11:46 +0000 (19:11 +0100)
The nodedev schema defines that a mdev_types capability must have
one or more type elements. The XML parsing and the format allows to
accept and to write mdev_types capability without any type element.
This patches fixes this.

Signed-off-by: Boris Fiuczynski <fiuczy@linux.ibm.com>
Reviewed-by: Bjoern Walk <bwalk@linux.ibm.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Signed-off-by: Ján Tomko <jtomko@redhat.com>
src/conf/node_device_conf.c

index 9e75f6f3a2ede205d478a9abac896501f0ae80e0..77dcd1635923f89fb9d0950ba0927884b58e75ee 100644 (file)
@@ -277,25 +277,27 @@ virNodeDeviceCapPCIDefFormat(virBufferPtr buf,
                           virPCIHeaderTypeToString(data->pci_dev.hdrType));
     }
     if (data->pci_dev.flags & VIR_NODE_DEV_CAP_FLAG_PCI_MDEV) {
-        virBufferAddLit(buf, "<capability type='mdev_types'>\n");
-        virBufferAdjustIndent(buf, 2);
-        for (i = 0; i < data->pci_dev.nmdev_types; i++) {
-            virMediatedDeviceTypePtr type = data->pci_dev.mdev_types[i];
-            virBufferEscapeString(buf, "<type id='%s'>\n", type->id);
+        if (data->pci_dev.nmdev_types > 0) {
+            virBufferAddLit(buf, "<capability type='mdev_types'>\n");
             virBufferAdjustIndent(buf, 2);
-            if (type->name)
-                virBufferEscapeString(buf, "<name>%s</name>\n",
-                                      type->name);
-            virBufferEscapeString(buf, "<deviceAPI>%s</deviceAPI>\n",
-                                  type->device_api);
-            virBufferAsprintf(buf,
-                              "<availableInstances>%u</availableInstances>\n",
-                              type->available_instances);
+            for (i = 0; i < data->pci_dev.nmdev_types; i++) {
+                virMediatedDeviceTypePtr type = data->pci_dev.mdev_types[i];
+                virBufferEscapeString(buf, "<type id='%s'>\n", type->id);
+                virBufferAdjustIndent(buf, 2);
+                if (type->name)
+                    virBufferEscapeString(buf, "<name>%s</name>\n",
+                                          type->name);
+                virBufferEscapeString(buf, "<deviceAPI>%s</deviceAPI>\n",
+                                      type->device_api);
+                virBufferAsprintf(buf,
+                                  "<availableInstances>%u</availableInstances>\n",
+                                  type->available_instances);
+                virBufferAdjustIndent(buf, -2);
+                virBufferAddLit(buf, "</type>\n");
+            }
             virBufferAdjustIndent(buf, -2);
-            virBufferAddLit(buf, "</type>\n");
+            virBufferAddLit(buf, "</capability>\n");
         }
-        virBufferAdjustIndent(buf, -2);
-        virBufferAddLit(buf, "</capability>\n");
     }
     if (data->pci_dev.nIommuGroupDevices) {
         virBufferAsprintf(buf, "<iommuGroup number='%d'>\n",
@@ -1531,6 +1533,12 @@ virNodeDevPCICapMdevTypesParseXML(xmlXPathContextPtr ctxt,
     if ((nmdev_types = virXPathNodeSet("./type", ctxt, &nodes)) < 0)
         goto cleanup;
 
+    if (nmdev_types == 0) {
+        virReportError(VIR_ERR_XML_ERROR, "%s",
+                       _("missing <type> element in <capability> element"));
+        goto cleanup;
+    }
+
     orignode = ctxt->node;
     for (i = 0; i < nmdev_types; i++) {
         ctxt->node = nodes[i];