[PATCH 05/11] conf: Fix type of @present in _virDomainTimerDef struct

Michal Privoznik mprivozn at redhat.com
Mon Jan 24 10:07:27 UTC 2022


In the _virDomainTimerDef structure we have @present member which
is like virTristateBool, except it's an integer and has values
shifted by one. This is harder to read. Retype the member to
virTristateBool which we are familiar with.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 src/conf/domain_conf.c   | 27 ++++++++++-----------------
 src/conf/domain_conf.h   |  2 +-
 src/libxl/libxl_conf.c   |  2 +-
 src/libxl/xen_common.c   | 13 +++++++++----
 src/lxc/lxc_cgroup.c     |  2 +-
 src/lxc/lxc_controller.c |  2 +-
 src/qemu/qemu_command.c  | 20 ++++++++++----------
 src/qemu/qemu_validate.c |  6 +++---
 8 files changed, 36 insertions(+), 38 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index bb887d4a3b..fbe21c4fd2 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -11998,7 +11998,6 @@ virDomainTimerDefParseXML(xmlNodePtr node,
     xmlNodePtr catchup;
     int ret;
     g_autofree char *name = NULL;
-    g_autofree char *present = NULL;
     g_autofree char *tickpolicy = NULL;
     g_autofree char *track = NULL;
     g_autofree char *mode = NULL;
@@ -12019,16 +12018,10 @@ virDomainTimerDefParseXML(xmlNodePtr node,
         goto error;
     }
 
-    def->present = -1; /* unspecified */
-    if ((present = virXMLPropString(node, "present")) != NULL) {
-        bool state = false;
-        if (virStringParseYesNo(present, &state) < 0) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("unknown timer present value '%s'"), present);
-            goto error;
-        }
-        def->present = state ? 1 : 0;
-    }
+    if (virXMLPropTristateBool(node, "present",
+                               VIR_XML_PROP_NONE,
+                               &def->present) < 0)
+        goto error;
 
     def->tickpolicy = -1;
     tickpolicy = virXMLPropString(node, "tickpolicy");
@@ -20481,8 +20474,9 @@ virDomainTimerDefCheckABIStability(virDomainTimerDef *src,
 
     if (src->present != dst->present) {
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                       _("Target timer presence %d does not match source %d"),
-                       dst->present, src->present);
+                       _("Target timer presence %s does not match source %s"),
+                       virTristateBoolTypeToString(dst->present),
+                       virTristateBoolTypeToString(src->present));
         return false;
     }
 
@@ -26119,10 +26113,9 @@ virDomainTimerDefFormat(virBuffer *buf,
     }
     virBufferAsprintf(buf, "<timer name='%s'", name);
 
-    if (def->present == 0) {
-        virBufferAddLit(buf, " present='no'");
-    } else if (def->present == 1) {
-        virBufferAddLit(buf, " present='yes'");
+    if (def->present) {
+        virBufferAsprintf(buf, " present='%s'",
+                          virTristateBoolTypeToString(def->present));
     }
 
     if (def->tickpolicy != -1) {
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index 764e025448..f995cbc045 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -2409,7 +2409,7 @@ struct _virDomainTimerCatchupDef {
 
 struct _virDomainTimerDef {
     int name;
-    int present;    /* unspecified = -1, no = 0, yes = 1 */
+    virTristateBool present;
     int tickpolicy; /* none|catchup|merge|discard */
 
     virDomainTimerCatchupDef catchup;
diff --git a/src/libxl/libxl_conf.c b/src/libxl/libxl_conf.c
index 561171126c..5d87b999f2 100644
--- a/src/libxl/libxl_conf.c
+++ b/src/libxl/libxl_conf.c
@@ -423,7 +423,7 @@ libxlMakeDomBuildInfo(virDomainDef *def,
                                virDomainTimerNameTypeToString(clock.timers[i]->name));
                 return -1;
             }
-            if (clock.timers[i]->present == 1)
+            if (clock.timers[i]->present == VIR_TRISTATE_BOOL_YES)
                 libxl_defbool_set(&b_info->u.hvm.hpet, 1);
             break;
 
diff --git a/src/libxl/xen_common.c b/src/libxl/xen_common.c
index c3fa98b71d..0679f441cc 100644
--- a/src/libxl/xen_common.c
+++ b/src/libxl/xen_common.c
@@ -553,7 +553,7 @@ xenParseHypervisorFeatures(virConf *conf, virDomainDef *def)
 
         timer = g_new0(virDomainTimerDef, 1);
         timer->name = VIR_DOMAIN_TIMER_NAME_TSC;
-        timer->present = 1;
+        timer->present = VIR_TRISTATE_BOOL_YES;
         timer->tickpolicy = -1;
         timer->mode = VIR_DOMAIN_TIMER_MODE_AUTO;
         timer->track = -1;
@@ -625,7 +625,11 @@ xenParseHypervisorFeatures(virConf *conf, virDomainDef *def)
 
             timer = g_new0(virDomainTimerDef, 1);
             timer->name = VIR_DOMAIN_TIMER_NAME_HPET;
-            timer->present = val;
+            if (val == 1) {
+                timer->present = VIR_TRISTATE_BOOL_YES;
+            } else {
+                timer->present = VIR_TRISTATE_BOOL_NO;
+            }
             timer->tickpolicy = -1;
             timer->mode = -1;
             timer->track = -1;
@@ -2112,9 +2116,10 @@ xenFormatHypervisorFeatures(virConf *conf, virDomainDef *def)
 
         case VIR_DOMAIN_TIMER_NAME_HPET:
             if (hvm) {
-                int enable_hpet = def->clock.timers[i]->present != 0;
+                int enable_hpet = def->clock.timers[i]->present != VIR_TRISTATE_BOOL_NO;
 
-                /* disable hpet if 'present' is 0, enable otherwise */
+                /* disable hpet if 'present' is VIR_TRISTATE_BOOL_NO, enable
+                 * otherwise */
                 if (xenConfigSetInt(conf, "hpet", enable_hpet) < 0)
                     return -1;
             } else {
diff --git a/src/lxc/lxc_cgroup.c b/src/lxc/lxc_cgroup.c
index 736b2000ff..d31fff5f98 100644
--- a/src/lxc/lxc_cgroup.c
+++ b/src/lxc/lxc_cgroup.c
@@ -332,7 +332,7 @@ static int virLXCCgroupSetupDeviceACL(virDomainDef *def,
         const char *dev = NULL;
 
         /* Check if "present" is set to "no" otherwise enable it. */
-        if (!timer->present)
+        if (timer->present == VIR_TRISTATE_BOOL_NO)
             continue;
 
         switch ((virDomainTimerNameType)timer->name) {
diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c
index 3c930eaacd..c4e3b66751 100644
--- a/src/lxc/lxc_controller.c
+++ b/src/lxc/lxc_controller.c
@@ -1510,7 +1510,7 @@ virLXCControllerSetupTimers(virLXCController *ctrl)
         dev_t dev;
 
         /* Check if "present" is set to "no" otherwise enable it. */
-        if (!timer->present)
+        if (timer->present == VIR_TRISTATE_BOOL_NO)
             continue;
 
         switch ((virDomainTimerNameType)timer->name) {
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index 52ea148a0f..a62d3f36ae 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -6293,15 +6293,14 @@ qemuBuildClockCommandLine(virCommand *cmd,
             break;
 
         case VIR_DOMAIN_TIMER_NAME_HPET:
-            /* the only meaningful attribute for hpet is "present". If
-             * present is -1, that means it wasn't specified, and
-             * should be left at the default for the
-             * hypervisor. "default" when -no-hpet exists is "yes",
-             * and when -no-hpet doesn't exist is "no". "confusing"?
-             * "yes"! */
+            /* the only meaningful attribute for hpet is "present". If present
+             * is VIR_TRISTATE_BOOL_ABSENT, that means it wasn't specified, and
+             * should be left at the default for the hypervisor. "default" when
+             * -no-hpet exists is VIR_TRISTATE_BOOL_YES, and when -no-hpet
+             *  doesn't exist is VIR_TRISTATE_BOOL_NO. "confusing"?  "yes"! */
 
             if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_NO_HPET)) {
-                if (def->clock.timers[i]->present == 0)
+                if (def->clock.timers[i]->present == VIR_TRISTATE_BOOL_NO)
                     virCommandAddArg(cmd, "-no-hpet");
             }
             break;
@@ -6638,13 +6637,14 @@ qemuBuildCpuCommandLine(virCommand *cmd,
 
         switch ((virDomainTimerNameType)timer->name) {
         case VIR_DOMAIN_TIMER_NAME_KVMCLOCK:
-            if (timer->present != -1) {
+            if (timer->present != VIR_TRISTATE_BOOL_ABSENT) {
+                /* QEMU expects on/off -> virTristateSwitch. */
                 virBufferAsprintf(&buf, ",kvmclock=%s",
-                                  timer->present ? "on" : "off");
+                                  virTristateSwitchTypeToString(timer->present));
             }
             break;
         case VIR_DOMAIN_TIMER_NAME_HYPERVCLOCK:
-            if (timer->present == 1)
+            if (timer->present == VIR_TRISTATE_BOOL_YES)
                 virBufferAddLit(&buf, ",hv-time=on");
             break;
         case VIR_DOMAIN_TIMER_NAME_TSC:
diff --git a/src/qemu/qemu_validate.c b/src/qemu/qemu_validate.c
index 0a879f0115..b62e49a5bc 100644
--- a/src/qemu/qemu_validate.c
+++ b/src/qemu/qemu_validate.c
@@ -411,7 +411,7 @@ qemuValidateDomainDefClockTimers(const virDomainDef *def,
         case VIR_DOMAIN_TIMER_NAME_TSC:
         case VIR_DOMAIN_TIMER_NAME_KVMCLOCK:
         case VIR_DOMAIN_TIMER_NAME_HYPERVCLOCK:
-            if (!ARCH_IS_X86(def->os.arch) && timer->present == 1) {
+            if (!ARCH_IS_X86(def->os.arch) && timer->present == VIR_TRISTATE_BOOL_YES) {
                 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                                _("Configuring the '%s' timer is not supported "
                                  "for virtType=%s arch=%s machine=%s guests"),
@@ -489,7 +489,7 @@ qemuValidateDomainDefClockTimers(const virDomainDef *def,
             /* no hpet timer available. The only possible action
               is to raise an error if present="yes" */
             if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_NO_HPET) &&
-                timer->present == 1) {
+                timer->present == VIR_TRISTATE_BOOL_YES) {
                 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                                "%s", _("hpet timer is not supported"));
                 return -1;
@@ -508,7 +508,7 @@ qemuValidateDomainDefClockTimers(const virDomainDef *def,
                                def->os.machine);
                 return -1;
             }
-            if (timer->present == 0) {
+            if (timer->present == VIR_TRISTATE_BOOL_NO) {
                 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                                _("The '%s' timer can't be disabled"),
                                virDomainTimerNameTypeToString(timer->name));
-- 
2.34.1




More information about the libvir-list mailing list