[PATCH v3 4/8] qemu: probe mshv capabilities

Praveen K Paladugu posted 8 patches 5 months, 1 week ago
There is a newer version of this series
[PATCH v3 4/8] qemu: probe mshv capabilities
Posted by Praveen K Paladugu 5 months, 1 week ago
Probe mshv capabilities from qemu with QMP commands. Limit probing only
to x86_64 architecture with newer versions of QEMU.

Signed-off-by: Praveen K Paladugu <prapal@linux.microsoft.com>
---
 src/qemu/qemu_capabilities.c | 21 +++++++++++++++++++++
 src/qemu/qemu_monitor.c      | 12 ++++++++++++
 src/qemu/qemu_monitor.h      | 12 +++++++++---
 src/qemu/qemu_monitor_json.c | 32 ++++++++++++++++++++++++++------
 src/qemu/qemu_monitor_json.h | 13 +++++++++----
 5 files changed, 77 insertions(+), 13 deletions(-)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 1e069eb0e5..cc7774a58a 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -3488,6 +3488,22 @@ virQEMUCapsProbeQMPKVMState(virQEMUCaps *qemuCaps,
     return 0;
 }
 
+static int
+virQEMUCapsProbeQMPMSHVState(virQEMUCaps *qemuCaps,
+                            qemuMonitor *mon)
+{
+    bool enabled = false;
+    bool present = false;
+
+    if (qemuMonitorGetMSHVState(mon, &enabled, &present) < 0)
+        return -1;
+
+    if (present && enabled)
+        virQEMUCapsSet(qemuCaps, QEMU_CAPS_MSHV);
+
+    return 0;
+}
+
 #ifdef __APPLE__
 bool
 virQEMUCapsProbeHVF(virQEMUCaps *qemuCaps)
@@ -5795,6 +5811,11 @@ virQEMUCapsInitQMPMonitor(virQEMUCaps *qemuCaps,
     if (virQEMUCapsProbeQMPKVMState(qemuCaps, mon) < 0)
         return -1;
 
+    if (qemuCaps->arch == VIR_ARCH_X86_64 && qemuCaps->version >= 10000092) {
+        if (virQEMUCapsProbeQMPMSHVState(qemuCaps, mon) < 0)
+            return -1;
+    }
+
     if (virQEMUCapsProbeHVF(qemuCaps))
         virQEMUCapsSet(qemuCaps, QEMU_CAPS_HVF);
 
diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
index c1fef8d5de..5acff4125d 100644
--- a/src/qemu/qemu_monitor.c
+++ b/src/qemu/qemu_monitor.c
@@ -3434,6 +3434,18 @@ qemuMonitorGetKVMState(qemuMonitor *mon,
     return qemuMonitorJSONGetKVMState(mon, enabled, present);
 }
 
+int
+qemuMonitorGetMSHVState(qemuMonitor *mon,
+                        bool *enabled,
+                        bool *present)
+{
+    VIR_DEBUG("enabled=%p present=%p", enabled, present);
+
+    QEMU_CHECK_MONITOR(mon);
+
+    return qemuMonitorJSONGetMSHVState(mon, enabled, present);
+}
+
 
 int
 qemuMonitorGetObjectTypes(qemuMonitor *mon,
diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h
index 755f347e17..e31a00de56 100644
--- a/src/qemu/qemu_monitor.h
+++ b/src/qemu/qemu_monitor.h
@@ -1220,9 +1220,15 @@ qemuMonitorCPUModelInfoCopy(const qemuMonitorCPUModelInfo *orig);
 
 GHashTable *qemuMonitorGetCommandLineOptions(qemuMonitor *mon);
 
-int qemuMonitorGetKVMState(qemuMonitor *mon,
-                           bool *enabled,
-                           bool *present);
+int
+qemuMonitorGetMSHVState(qemuMonitor *mon,
+                        bool *enabled,
+                        bool *present);
+
+int
+qemuMonitorGetKVMState(qemuMonitor *mon,
+                       bool *enabled,
+                       bool *present);
 
 int qemuMonitorGetObjectTypes(qemuMonitor *mon,
                               char ***types);
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index 9f51421478..2b29399d3b 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -5520,9 +5520,11 @@ qemuMonitorJSONGetCommandLineOptions(qemuMonitor *mon)
 }
 
 
-int qemuMonitorJSONGetKVMState(qemuMonitor *mon,
-                               bool *enabled,
-                               bool *present)
+static int
+qemuMonitorJSONGetHypervisorState(qemuMonitor *mon,
+                                  const char *query_cmd,
+                                  bool *enabled,
+                                  bool *present)
 {
     g_autoptr(virJSONValue) cmd = NULL;
     g_autoptr(virJSONValue) reply = NULL;
@@ -5531,7 +5533,7 @@ int qemuMonitorJSONGetKVMState(qemuMonitor *mon,
     /* Safe defaults */
     *enabled = *present = false;
 
-    if (!(cmd = qemuMonitorJSONMakeCommand("query-kvm", NULL)))
+    if (!(cmd = qemuMonitorJSONMakeCommand(query_cmd, NULL)))
         return -1;
 
     if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0)
@@ -5542,8 +5544,8 @@ int qemuMonitorJSONGetKVMState(qemuMonitor *mon,
 
     if (virJSONValueObjectGetBoolean(data, "enabled", enabled) < 0 ||
         virJSONValueObjectGetBoolean(data, "present", present) < 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                       _("query-kvm replied unexpected data"));
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("%1$s replied unexpected data"), query_cmd);
         return -1;
     }
 
@@ -5551,6 +5553,24 @@ int qemuMonitorJSONGetKVMState(qemuMonitor *mon,
 }
 
 
+int qemuMonitorJSONGetKVMState(qemuMonitor *mon,
+                               bool *enabled,
+                               bool *present)
+{
+    return qemuMonitorJSONGetHypervisorState(mon, "query-kvm",
+                                             enabled, present);
+}
+
+
+int qemuMonitorJSONGetMSHVState(qemuMonitor *mon,
+                               bool *enabled,
+                               bool *present)
+{
+    return qemuMonitorJSONGetHypervisorState(mon, "query-mshv",
+                                             enabled, present);
+}
+
+
 int
 qemuMonitorJSONGetObjectTypes(qemuMonitor *mon,
                               char ***types)
diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h
index bd437f7938..5c1d626f97 100644
--- a/src/qemu/qemu_monitor_json.h
+++ b/src/qemu/qemu_monitor_json.h
@@ -451,10 +451,15 @@ qemuMonitorJSONGetCPUModelComparison(qemuMonitor *mon,
 GHashTable *
 qemuMonitorJSONGetCommandLineOptions(qemuMonitor *mon);
 
-int
-qemuMonitorJSONGetKVMState(qemuMonitor *mon,
-                           bool *enabled,
-                           bool *present)
+
+int qemuMonitorJSONGetKVMState(qemuMonitor *mon,
+                               bool *enabled,
+                               bool *present)
+    ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
+
+int qemuMonitorJSONGetMSHVState(qemuMonitor *mon,
+                                bool *enabled,
+                                bool *present)
     ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
 
 int
-- 
2.50.1
Re: [PATCH v3 4/8] qemu: probe mshv capabilities
Posted by Daniel P. Berrangé via Devel 4 months, 1 week ago
On Fri, Aug 29, 2025 at 03:28:34PM -0500, Praveen K Paladugu wrote:
> Probe mshv capabilities from qemu with QMP commands. Limit probing only
> to x86_64 architecture with newer versions of QEMU.
> 
> Signed-off-by: Praveen K Paladugu <prapal@linux.microsoft.com>
> ---
>  src/qemu/qemu_capabilities.c | 21 +++++++++++++++++++++
>  src/qemu/qemu_monitor.c      | 12 ++++++++++++
>  src/qemu/qemu_monitor.h      | 12 +++++++++---
>  src/qemu/qemu_monitor_json.c | 32 ++++++++++++++++++++++++++------
>  src/qemu/qemu_monitor_json.h | 13 +++++++++----
>  5 files changed, 77 insertions(+), 13 deletions(-)
> 
> diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
> index 1e069eb0e5..cc7774a58a 100644
> --- a/src/qemu/qemu_capabilities.c
> +++ b/src/qemu/qemu_capabilities.c
> @@ -3488,6 +3488,22 @@ virQEMUCapsProbeQMPKVMState(virQEMUCaps *qemuCaps,
>      return 0;
>  }
>  
> +static int
> +virQEMUCapsProbeQMPMSHVState(virQEMUCaps *qemuCaps,
> +                            qemuMonitor *mon)
> +{
> +    bool enabled = false;
> +    bool present = false;
> +
> +    if (qemuMonitorGetMSHVState(mon, &enabled, &present) < 0)
> +        return -1;
> +
> +    if (present && enabled)
> +        virQEMUCapsSet(qemuCaps, QEMU_CAPS_MSHV);
> +
> +    return 0;
> +}
> +
>  #ifdef __APPLE__
>  bool
>  virQEMUCapsProbeHVF(virQEMUCaps *qemuCaps)
> @@ -5795,6 +5811,11 @@ virQEMUCapsInitQMPMonitor(virQEMUCaps *qemuCaps,
>      if (virQEMUCapsProbeQMPKVMState(qemuCaps, mon) < 0)
>          return -1;
>  
> +    if (qemuCaps->arch == VIR_ARCH_X86_64 && qemuCaps->version >= 10000092) {
> +        if (virQEMUCapsProbeQMPMSHVState(qemuCaps, mon) < 0)
> +            return -1;
> +    }

This change causes the test suite to break at this point in the series.

  TEST: qemucapabilitiestest
        ........................................
  wrong expected command in /var/home/berrange/src/virt/libvirt/tests/qemucapabilitiesdata/caps_10.1.0_x86_64.replies:24767: : {"execute":"query-mshv","id":"libvirt-6"} expected {"execute":"qom-list-types","id":"libvirt-6"}

In order to ensure that it is possible to use 'git bisect' without
spurious failures, we aim to have tests pass at every patch in a
series.

More importantly, we generally aim to avoid having version number
checks and instead rely on detected features.

If you introduce a new QEMU_CAPS_QUERY_MSHV capability flag, you
can update the 'virQEMUCapsCommands' array to match 'query-mshv'.

Then at this point in the code you can drop the arch check and
the version check, and instead check QEMU_CAPS_QUERY_MSHV as
a witness for being able to call virQEMUCapsProbeQMPMSHVState.

Possibly still need some updates to the qemucapabilitiesdata
files after doing that in this commit, but not 100% sure.

With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Re: [PATCH v3 4/8] qemu: probe mshv capabilities
Posted by Praveen K Paladugu 4 months, 1 week ago

On 10/1/2025 4:37 AM, Daniel P. Berrangé wrote:
> On Fri, Aug 29, 2025 at 03:28:34PM -0500, Praveen K Paladugu wrote:
>> Probe mshv capabilities from qemu with QMP commands. Limit probing only
>> to x86_64 architecture with newer versions of QEMU.
>>
>> Signed-off-by: Praveen K Paladugu <prapal@linux.microsoft.com>
>> ---
>>   src/qemu/qemu_capabilities.c | 21 +++++++++++++++++++++
>>   src/qemu/qemu_monitor.c      | 12 ++++++++++++
>>   src/qemu/qemu_monitor.h      | 12 +++++++++---
>>   src/qemu/qemu_monitor_json.c | 32 ++++++++++++++++++++++++++------
>>   src/qemu/qemu_monitor_json.h | 13 +++++++++----
>>   5 files changed, 77 insertions(+), 13 deletions(-)
>>
>> diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
>> index 1e069eb0e5..cc7774a58a 100644
>> --- a/src/qemu/qemu_capabilities.c
>> +++ b/src/qemu/qemu_capabilities.c
>> @@ -3488,6 +3488,22 @@ virQEMUCapsProbeQMPKVMState(virQEMUCaps *qemuCaps,
>>       return 0;
>>   }
>>   
>> +static int
>> +virQEMUCapsProbeQMPMSHVState(virQEMUCaps *qemuCaps,
>> +                            qemuMonitor *mon)
>> +{
>> +    bool enabled = false;
>> +    bool present = false;
>> +
>> +    if (qemuMonitorGetMSHVState(mon, &enabled, &present) < 0)
>> +        return -1;
>> +
>> +    if (present && enabled)
>> +        virQEMUCapsSet(qemuCaps, QEMU_CAPS_MSHV);
>> +
>> +    return 0;
>> +}
>> +
>>   #ifdef __APPLE__
>>   bool
>>   virQEMUCapsProbeHVF(virQEMUCaps *qemuCaps)
>> @@ -5795,6 +5811,11 @@ virQEMUCapsInitQMPMonitor(virQEMUCaps *qemuCaps,
>>       if (virQEMUCapsProbeQMPKVMState(qemuCaps, mon) < 0)
>>           return -1;
>>   
>> +    if (qemuCaps->arch == VIR_ARCH_X86_64 && qemuCaps->version >= 10000092) {
>> +        if (virQEMUCapsProbeQMPMSHVState(qemuCaps, mon) < 0)
>> +            return -1;
>> +    }
> 
> This change causes the test suite to break at this point in the series.
> 
>    TEST: qemucapabilitiestest
>          ........................................
>    wrong expected command in /var/home/berrange/src/virt/libvirt/tests/qemucapabilitiesdata/caps_10.1.0_x86_64.replies:24767: : {"execute":"query-mshv","id":"libvirt-6"} expected {"execute":"qom-list-types","id":"libvirt-6"}
> 
> In order to ensure that it is possible to use 'git bisect' without
> spurious failures, we aim to have tests pass at every patch in a
> series.
> 
> More importantly, we generally aim to avoid having version number
> checks and instead rely on detected features.
> 
> If you introduce a new QEMU_CAPS_QUERY_MSHV capability flag, you
> can update the 'virQEMUCapsCommands' array to match 'query-mshv'.
> 
> Then at this point in the code you can drop the arch check and
> the version check, and instead check QEMU_CAPS_QUERY_MSHV as
> a witness for being able to call virQEMUCapsProbeQMPMSHVState.
> 
> Possibly still need some updates to the qemucapabilitiesdata
> files after doing that in this commit, but not 100% sure.
> 
> With regards,
> Daniel

Thanks for the feedback Daniel. I will address this bug and rest of the 
comments in my next update.

-- 
Regards,
Praveen K Paladugu