Re: [libvirt PATCH v4 2/5] cpu: Wire in XML validation

2020-10-07 Thread Peter Krempa
On Wed, Oct 07, 2020 at 10:54:55 +0200, Tim Wiederhake wrote:
> This adds a new value to virConnectCompareCPUFlags,
> "VIR_CONNECT_CPU_VALIDATE_XML", that governs XML document validation in
> virCPUDefParseXML.
> 
> In src/conf/cpu_conf.c, include configmake.h for PKGDATADIR and
> virfile.h for virFileFindResource.
> 
> Signed-off-by: Tim Wiederhake 
> ---

Reviewed-by: Peter Krempa 



[libvirt PATCH v4 2/5] cpu: Wire in XML validation

2020-10-07 Thread Tim Wiederhake
This adds a new value to virConnectCompareCPUFlags,
"VIR_CONNECT_CPU_VALIDATE_XML", that governs XML document validation in
virCPUDefParseXML.

In src/conf/cpu_conf.c, include configmake.h for PKGDATADIR and
virfile.h for virFileFindResource.

Signed-off-by: Tim Wiederhake 
---
 include/libvirt/libvirt-host.h   |  2 ++
 src/bhyve/bhyve_driver.c |  7 +--
 src/conf/cpu_conf.c  | 25 +
 src/conf/cpu_conf.h  |  6 --
 src/conf/domain_conf.c   |  3 ++-
 src/cpu/cpu.c|  5 +++--
 src/cpu/cpu.h|  3 ++-
 src/libxl/libxl_driver.c |  7 +--
 src/qemu/qemu_domain.c   |  5 +++--
 src/qemu/qemu_driver.c   | 18 +-
 src/qemu/qemu_migration_cookie.c |  3 ++-
 tests/cputest.c  |  5 +++--
 12 files changed, 65 insertions(+), 24 deletions(-)

diff --git a/include/libvirt/libvirt-host.h b/include/libvirt/libvirt-host.h
index 6972834175..4caed94a77 100644
--- a/include/libvirt/libvirt-host.h
+++ b/include/libvirt/libvirt-host.h
@@ -754,6 +754,8 @@ typedef enum {
 typedef enum {
 VIR_CONNECT_COMPARE_CPU_FAIL_INCOMPATIBLE = (1 << 0), /* treat incompatible
  CPUs as failure */
+VIR_CONNECT_COMPARE_CPU_VALIDATE_XML = (1 << 1), /* validate the xml
+document */
 } virConnectCompareCPUFlags;
 
 int virConnectCompareCPU(virConnectPtr conn,
diff --git a/src/bhyve/bhyve_driver.c b/src/bhyve/bhyve_driver.c
index 91f41aa238..78c3241293 100644
--- a/src/bhyve/bhyve_driver.c
+++ b/src/bhyve/bhyve_driver.c
@@ -1441,14 +1441,17 @@ bhyveConnectCompareCPU(virConnectPtr conn,
 int ret = VIR_CPU_COMPARE_ERROR;
 virCapsPtr caps = NULL;
 bool failIncompatible;
+bool validateXML;
 
-virCheckFlags(VIR_CONNECT_COMPARE_CPU_FAIL_INCOMPATIBLE,
+virCheckFlags(VIR_CONNECT_COMPARE_CPU_FAIL_INCOMPATIBLE |
+  VIR_CONNECT_COMPARE_CPU_VALIDATE_XML,
   VIR_CPU_COMPARE_ERROR);
 
 if (virConnectCompareCPUEnsureACL(conn) < 0)
 goto cleanup;
 
 failIncompatible = !!(flags & VIR_CONNECT_COMPARE_CPU_FAIL_INCOMPATIBLE);
+validateXML = !!(flags & VIR_CONNECT_COMPARE_CPU_VALIDATE_XML);
 
 if (!(caps = bhyveDriverGetCapabilities(driver)))
 goto cleanup;
@@ -1464,7 +1467,7 @@ bhyveConnectCompareCPU(virConnectPtr conn,
 }
 } else {
 ret = virCPUCompareXML(caps->host.arch, caps->host.cpu,
-   xmlDesc, failIncompatible);
+   xmlDesc, failIncompatible, validateXML);
 }
 
  cleanup:
diff --git a/src/conf/cpu_conf.c b/src/conf/cpu_conf.c
index dea950ce68..1910470836 100644
--- a/src/conf/cpu_conf.c
+++ b/src/conf/cpu_conf.c
@@ -20,9 +20,11 @@
 
 #include 
 
+#include "configmake.h"
 #include "virerror.h"
 #include "viralloc.h"
 #include "virbuffer.h"
+#include "virfile.h"
 #include "cpu_conf.h"
 #include "domain_conf.h"
 #include "virstring.h"
@@ -281,7 +283,8 @@ virCPUDefCopy(const virCPUDef *cpu)
 int
 virCPUDefParseXMLString(const char *xml,
 virCPUType type,
-virCPUDefPtr *cpu)
+virCPUDefPtr *cpu,
+bool validateXML)
 {
 xmlDocPtr doc = NULL;
 xmlXPathContextPtr ctxt = NULL;
@@ -295,7 +298,7 @@ virCPUDefParseXMLString(const char *xml,
 if (!(doc = virXMLParseStringCtxt(xml, _("(CPU_definition)"), )))
 goto cleanup;
 
-if (virCPUDefParseXML(ctxt, NULL, type, cpu) < 0)
+if (virCPUDefParseXML(ctxt, NULL, type, cpu, validateXML) < 0)
 goto cleanup;
 
 ret = 0;
@@ -323,7 +326,8 @@ int
 virCPUDefParseXML(xmlXPathContextPtr ctxt,
   const char *xpath,
   virCPUType type,
-  virCPUDefPtr *cpu)
+  virCPUDefPtr *cpu,
+  bool validateXML)
 {
 g_autoptr(virCPUDef) def = NULL;
 g_autofree xmlNodePtr *nodes = NULL;
@@ -348,6 +352,19 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt,
 return -1;
 }
 
+if (validateXML) {
+g_autofree char *schemafile = NULL;
+
+if (!(schemafile = virFileFindResource("cpu.rng",
+   abs_top_srcdir "/docs/schemas",
+   PKGDATADIR "/schemas")))
+return -1;
+
+if (virXMLValidateNodeAgainstSchema(schemafile, ctxt->doc,
+ctxt->node) < 0)
+return -1;
+}
+
 def = virCPUDefNew();
 
 if (type == VIR_CPU_TYPE_AUTO) {
@@ -1146,7 +1163,7 @@ virCPUDefListParse(const char **xmlCPUs,
 if (!(doc = virXMLParseStringCtxt(xmlCPUs[i], _("(CPU_definition)"), 
)))
 goto error;
 
-if (virCPUDefParseXML(ctxt, NULL, cpuType, [i]) < 0)
+if (virCPUDefParseXML(ctxt, NULL, cpuType,