From 715288515d2c6eb2c6d036b8068e3a6b15d1adf3 Mon Sep 17 00:00:00 2001
From: Michal Privoznik <mprivozn@redhat.com>
Date: Tue, 14 Dec 2021 10:24:30 +0100
Subject: [PATCH] conf: Turn virDomainDef.kvm_features into a struct
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

In future commits we will need to store not just an array of
VIR_TRISTATE_SWITCH_* but also an additional integer. Follow the
example of TCG and introduce a structure where both the array an
integer can live.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: J谩n Tomko <jtomko@redhat.com>
Signed-off-by: Shaokun Wei <weishaokun@kylinos.cn>
---
 src/conf/domain_conf.c  | 20 +++++++++++++-------
 src/conf/domain_conf.h  |  7 ++++++-
 src/qemu/qemu_command.c |  4 ++--
 3 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index e884d94ef7..e94ae4ea17 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -3476,6 +3476,7 @@ void virDomainDefFree(virDomainDefPtr def)
     VIR_FREE(def->emulator);
     VIR_FREE(def->description);
     VIR_FREE(def->title);
+    VIR_FREE(def->kvm_features);
     VIR_FREE(def->hyperv_vendor_id);
 
     virBlkioDeviceArrayClear(def->blkio.devices,
@@ -20596,7 +20597,9 @@ static int
 virDomainFeaturesKVMDefParse(virDomainDef *def,
                              xmlNodePtr node)
 {
-    def->features[VIR_DOMAIN_FEATURE_KVM] = VIR_TRISTATE_SWITCH_ON;
+    g_autofree virDomainFeatureKVM *kvm = NULL;
+
+    kvm = g_new0(virDomainFeatureKVM, 1);
 
     node = xmlFirstElementChild(node);
     while (node) {
@@ -20615,11 +20618,14 @@ virDomainFeaturesKVMDefParse(virDomainDef *def,
                                      &value) < 0)
             return -1;
 
-        def->kvm_features[feature] = value;
+        kvm->features[feature] = value;
 
         node = xmlNextElementSibling(node);
     }
 
+    def->features[VIR_DOMAIN_FEATURE_KVM] = VIR_TRISTATE_SWITCH_ON;
+    def->kvm_features = g_steal_pointer(&kvm);
+
     return 0;
 }
 
@@ -23584,13 +23590,13 @@ virDomainDefFeaturesCheckABIStability(virDomainDefPtr src,
             switch ((virDomainKVM) i) {
             case VIR_DOMAIN_KVM_HIDDEN:
             case VIR_DOMAIN_KVM_DEDICATED:
-                if (src->kvm_features[i] != dst->kvm_features[i]) {
+                if (src->kvm_features->features[i] != dst->kvm_features->features[i]) {
                     virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                                    _("State of KVM feature '%s' differs: "
                                      "source: '%s', destination: '%s'"),
                                    virDomainKVMTypeToString(i),
-                                   virTristateSwitchTypeToString(src->kvm_features[i]),
-                                   virTristateSwitchTypeToString(dst->kvm_features[i]));
+                                   virTristateSwitchTypeToString(src->kvm_features->features[i]),
+                                   virTristateSwitchTypeToString(dst->kvm_features->features[i]));
                     return false;
                 }
 
@@ -29201,11 +29207,11 @@ virDomainDefFormatFeatures(virBufferPtr buf,
                 switch ((virDomainKVM) j) {
                 case VIR_DOMAIN_KVM_HIDDEN:
                 case VIR_DOMAIN_KVM_DEDICATED:
-                    if (def->kvm_features[j])
+                    if (def->kvm_features->features[j])
                         virBufferAsprintf(&childBuf, "<%s state='%s'/>\n",
                                           virDomainKVMTypeToString(j),
                                           virTristateSwitchTypeToString(
-                                              def->kvm_features[j]));
+                                              def->kvm_features->features[j]));
                     break;
 
                 /* coverity[dead_error_begin] */
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index 6d56ef0282..d75d06d7b8 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -2297,6 +2297,11 @@ struct _virDomainResctrlDef {
     size_t nmonitors;
 };
 
+typedef struct _virDomainFeatureKVM virDomainFeatureKVM;
+struct _virDomainFeatureKVM {
+    int features[VIR_DOMAIN_KVM_LAST];
+};
+
 
 struct _virDomainVcpuDef {
     bool online;
@@ -2479,7 +2484,7 @@ struct _virDomainDef {
     int features[VIR_DOMAIN_FEATURE_LAST];
     int caps_features[VIR_DOMAIN_PROCES_CAPS_FEATURE_LAST];
     int hyperv_features[VIR_DOMAIN_HYPERV_LAST];
-    int kvm_features[VIR_DOMAIN_KVM_LAST];
+    virDomainFeatureKVM *kvm_features;
     int msrs_features[VIR_DOMAIN_MSRS_LAST];
     unsigned int hyperv_spinlocks;
     int hyperv_stimer_direct;
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index d7db30d19b..ed843315dc 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -6911,12 +6911,12 @@ qemuBuildCpuCommandLine(virCommandPtr cmd,
         for (i = 0; i < VIR_DOMAIN_KVM_LAST; i++) {
             switch ((virDomainKVM) i) {
             case VIR_DOMAIN_KVM_HIDDEN:
-                if (def->kvm_features[i] == VIR_TRISTATE_SWITCH_ON)
+                if (def->kvm_features->features[i] == VIR_TRISTATE_SWITCH_ON)
                     virBufferAddLit(&buf, ",kvm=off");
                 break;
 
             case VIR_DOMAIN_KVM_DEDICATED:
-                if (def->kvm_features[i] == VIR_TRISTATE_SWITCH_ON)
+                if (def->kvm_features->features[i] == VIR_TRISTATE_SWITCH_ON)
                     virBufferAddLit(&buf, ",kvm-hint-dedicated=on");
                 break;
 
-- 
2.27.0

