7548c0
From 0ae283a1cb5224f3eb4fa32706e9b9c212577e51 Mon Sep 17 00:00:00 2001
7548c0
Message-Id: <0ae283a1cb5224f3eb4fa32706e9b9c212577e51@dist-git>
7548c0
From: Michal Privoznik <mprivozn@redhat.com>
7548c0
Date: Wed, 7 Oct 2020 18:45:40 +0200
7548c0
Subject: [PATCH] conf: Validate NUMA HMAT configuration
7548c0
MIME-Version: 1.0
7548c0
Content-Type: text/plain; charset=UTF-8
7548c0
Content-Transfer-Encoding: 8bit
7548c0
7548c0
There are several restrictions, for instance @initiator and
7548c0
@target have to refer to existing NUMA nodes (daa), @cache has to
7548c0
refer to a defined cache level and so on.
7548c0
7548c0
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
7548c0
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
7548c0
(cherry picked from commit f0611fe8830543d03d1871422f8c542453f0c8db)
7548c0
7548c0
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1749518
7548c0
7548c0
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
7548c0
Message-Id: <e8488a2e49fa251dd0e2ab51f5ab627e3b265440.1602087923.git.mprivozn@redhat.com>
7548c0
Reviewed-by: Ján Tomko <jtomko@redhat.com>
7548c0
---
7548c0
 src/conf/domain_conf.c |  3 ++
7548c0
 src/conf/numa_conf.c   | 99 ++++++++++++++++++++++++++++++++++++++++++
7548c0
 src/conf/numa_conf.h   |  1 +
7548c0
 3 files changed, 103 insertions(+)
7548c0
7548c0
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
7548c0
index 3229d5ec95..f41559f33e 100644
7548c0
--- a/src/conf/domain_conf.c
7548c0
+++ b/src/conf/domain_conf.c
7548c0
@@ -7144,6 +7144,9 @@ virDomainDefValidateInternal(const virDomainDef *def,
7548c0
     if (virDomainDefCputuneValidate(def) < 0)
7548c0
         return -1;
7548c0
 
7548c0
+    if (virDomainNumaDefValidate(def->numa) < 0)
7548c0
+        return -1;
7548c0
+
7548c0
     return 0;
7548c0
 }
7548c0
 
7548c0
diff --git a/src/conf/numa_conf.c b/src/conf/numa_conf.c
7548c0
index 5c764190c3..c90fb01bb6 100644
7548c0
--- a/src/conf/numa_conf.c
7548c0
+++ b/src/conf/numa_conf.c
7548c0
@@ -1365,6 +1365,105 @@ virDomainNumaDefFormatXML(virBufferPtr buf,
7548c0
 }
7548c0
 
7548c0
 
7548c0
+int
7548c0
+virDomainNumaDefValidate(const virDomainNuma *def)
7548c0
+{
7548c0
+    size_t i;
7548c0
+    size_t j;
7548c0
+
7548c0
+    if (!def)
7548c0
+        return 0;
7548c0
+
7548c0
+    for (i = 0; i < def->nmem_nodes; i++) {
7548c0
+        const virDomainNumaNode *node = &def->mem_nodes[i];
7548c0
+        g_autoptr(virBitmap) levelsSeen = virBitmapNewEmpty();
7548c0
+
7548c0
+        for (j = 0; j < node->ncaches; j++) {
7548c0
+            const virDomainNumaCache *cache = &node->caches[j];
7548c0
+
7548c0
+            /* Relax this if there's ever fourth layer of cache */
7548c0
+            if (cache->level > 3) {
7548c0
+                virReportError(VIR_ERR_XML_ERROR, "%s",
7548c0
+                               _("Ain't nobody heard of that much cache level"));
7548c0
+                return -1;
7548c0
+            }
7548c0
+
7548c0
+            if (virBitmapIsBitSet(levelsSeen, cache->level)) {
7548c0
+                virReportError(VIR_ERR_XML_ERROR,
7548c0
+                               _("Cache level '%u' already defined"),
7548c0
+                               cache->level);
7548c0
+                return -1;
7548c0
+            }
7548c0
+
7548c0
+            if (virBitmapSetBitExpand(levelsSeen, cache->level))
7548c0
+                return -1;
7548c0
+        }
7548c0
+    }
7548c0
+
7548c0
+    for (i = 0; i < def->ninterconnects; i++) {
7548c0
+        const virDomainNumaInterconnect *l = &def->interconnects[i];
7548c0
+
7548c0
+        if (l->initiator >= def->nmem_nodes) {
7548c0
+            virReportError(VIR_ERR_XML_ERROR, "%s",
7548c0
+                           _("'initiator' refers to a non-existent NUMA node"));
7548c0
+            return -1;
7548c0
+        }
7548c0
+
7548c0
+        if (l->target >= def->nmem_nodes) {
7548c0
+            virReportError(VIR_ERR_XML_ERROR, "%s",
7548c0
+                           _("'target' refers to a non-existent NUMA node"));
7548c0
+            return -1;
7548c0
+        }
7548c0
+
7548c0
+        if (!def->mem_nodes[l->initiator].cpumask) {
7548c0
+            virReportError(VIR_ERR_XML_ERROR, "%s",
7548c0
+                           _("NUMA nodes without CPUs can't be initiator"));
7548c0
+            return -1;
7548c0
+        }
7548c0
+
7548c0
+        if (l->cache > 0) {
7548c0
+            for (j = 0; j < def->mem_nodes[l->target].ncaches; j++) {
7548c0
+                const virDomainNumaCache *cache = def->mem_nodes[l->target].caches;
7548c0
+
7548c0
+                if (l->cache == cache->level)
7548c0
+                    break;
7548c0
+            }
7548c0
+
7548c0
+            if (j == def->mem_nodes[l->target].ncaches) {
7548c0
+                virReportError(VIR_ERR_XML_ERROR, "%s",
7548c0
+                               _("'cache' refers to a non-existent NUMA node cache"));
7548c0
+                return -1;
7548c0
+            }
7548c0
+        }
7548c0
+
7548c0
+        for (j = 0; j < i; j++) {
7548c0
+            const virDomainNumaInterconnect *ll = &def->interconnects[j];
7548c0
+
7548c0
+            if (l->type == ll->type &&
7548c0
+                l->initiator == ll->initiator &&
7548c0
+                l->target == ll->target &&
7548c0
+                l->cache == ll->cache &&
7548c0
+                l->accessType == ll->accessType) {
7548c0
+                virReportError(VIR_ERR_XML_ERROR, "%s",
7548c0
+                               _("Duplicate info for NUMA latencies"));
7548c0
+                return -1;
7548c0
+            }
7548c0
+
7548c0
+
7548c0
+            if (l->initiator != l->target &&
7548c0
+                l->initiator == ll->target &&
7548c0
+                l->target == ll->initiator) {
7548c0
+                virReportError(VIR_ERR_XML_ERROR, "%s",
7548c0
+                               _("Link already defined"));
7548c0
+                return -1;
7548c0
+            }
7548c0
+        }
7548c0
+    }
7548c0
+
7548c0
+    return 0;
7548c0
+}
7548c0
+
7548c0
+
7548c0
 unsigned int
7548c0
 virDomainNumaGetCPUCountTotal(virDomainNumaPtr numa)
7548c0
 {
7548c0
diff --git a/src/conf/numa_conf.h b/src/conf/numa_conf.h
7548c0
index 5043c5a6d4..2963004c94 100644
7548c0
--- a/src/conf/numa_conf.h
7548c0
+++ b/src/conf/numa_conf.h
7548c0
@@ -217,5 +217,6 @@ bool virDomainNumatuneNodeSpecified(virDomainNumaPtr numatune,
7548c0
 
7548c0
 int virDomainNumaDefParseXML(virDomainNumaPtr def, xmlXPathContextPtr ctxt);
7548c0
 int virDomainNumaDefFormatXML(virBufferPtr buf, virDomainNumaPtr def);
7548c0
+int virDomainNumaDefValidate(const virDomainNuma *def);
7548c0
 
7548c0
 unsigned int virDomainNumaGetCPUCountTotal(virDomainNumaPtr numa);
7548c0
-- 
7548c0
2.29.2
7548c0