Re: [libvirt] [PATCH 1/2] qemu: Enhance QMP detection of KVM state

2012-10-31 Thread Martin Kletzander
On 10/31/2012 12:44 AM, Eric Blake wrote:
 On 10/28/2012 06:55 AM, Martin Kletzander wrote:
 When there is no 'qemu-kvm' binary and the emulator used for a machine
 is, for example, 'qemu-system-x86_64' that, by default, runs without
 kvm enabled, libvirt still supplies '-no-kvm' option to this process,
 even though it does not recognize such option (making the start of a
 domain fail in that case).

 This patch adds QMP querying for KVM state using 'query-kvm' state,
 but does not set any of QEMU_CAPS_KVM and QEMU_CAPS_ENABLE_KVM flags.
 That functionality is done in different patch in order to be able to
 compare two possibilities and chose the better one without looking at
 the part of the code that's exactly the same for both of them (this
 patch).
 

 +static int
 +qemuCapsProbeQMPKVMState(qemuCapsPtr caps,
 + qemuMonitorPtr mon)
 +{
 +bool enabled = false;
 +bool present = false;
 +
 +if (!qemuCapsGet(caps, QEMU_CAPS_KVM))
 +return 0;
 +
 +if (qemuMonitorGetKVMState(mon, enabled, present)  0)
 +return -1;
 +
 +/* Youre right, this code does nothing, you must have checked out
 + * some weird commit.  Go back to your room and think about what
 + * you've done, young (wo)man. */
 
 Since this cute comment disappears in either 2/2 approach, I would
 probably rather see this series squashed into a single commit when
 finally going upstream.  That would also mean deleting the second
 paragraph of the commit message.
 
 
 +int qemuMonitorJSONGetKVMState(qemuMonitorPtr mon,
 +   bool *enabled,
 +   bool *present)
 +{
 +int ret;
 +virJSONValuePtr cmd = NULL;
 +virJSONValuePtr reply = NULL;
 +virJSONValuePtr data = NULL;
 +
 +if (!(cmd = qemuMonitorJSONMakeCommand(query-kvm, NULL)))
 +return -1;
 +
 +ret = qemuMonitorJSONCommand(mon, cmd, reply);
 +
 +if (ret == 0) {
 +if (qemuMonitorJSONHasError(reply, CommandNotFound))
 +goto cleanup;
 
 This relies on the caller to pre-initialize *enabled and *present to
 false; it might be better to explicitly repeat that setting here so that
 this function guarantees that the values are always correct on
 successful return even if the caller forgot to initialize.  But right
 now, the only caller happens to pre-initialize, so it's not a show-stopper.
 
 ACK, once I pick which of the 2/2 variants I like best :)
 

I squashed both in, fixed the huge amount of typos, added the explicit
setting to false for the bools in qemuMonitorJSONGetKVMState(), double
checked and pushed.  Thanks very much.

Martin

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH 1/2] qemu: Enhance QMP detection of KVM state

2012-10-30 Thread Eric Blake
On 10/28/2012 06:55 AM, Martin Kletzander wrote:
 When there is no 'qemu-kvm' binary and the emulator used for a machine
 is, for example, 'qemu-system-x86_64' that, by default, runs without
 kvm enabled, libvirt still supplies '-no-kvm' option to this process,
 even though it does not recognize such option (making the start of a
 domain fail in that case).
 
 This patch adds QMP querying for KVM state using 'query-kvm' state,
 but does not set any of QEMU_CAPS_KVM and QEMU_CAPS_ENABLE_KVM flags.
 That functionality is done in different patch in order to be able to
 compare two possibilities and chose the better one without looking at
 the part of the code that's exactly the same for both of them (this
 patch).

 
 +static int
 +qemuCapsProbeQMPKVMState(qemuCapsPtr caps,
 + qemuMonitorPtr mon)
 +{
 +bool enabled = false;
 +bool present = false;
 +
 +if (!qemuCapsGet(caps, QEMU_CAPS_KVM))
 +return 0;
 +
 +if (qemuMonitorGetKVMState(mon, enabled, present)  0)
 +return -1;
 +
 +/* Youre right, this code does nothing, you must have checked out
 + * some weird commit.  Go back to your room and think about what
 + * you've done, young (wo)man. */

Since this cute comment disappears in either 2/2 approach, I would
probably rather see this series squashed into a single commit when
finally going upstream.  That would also mean deleting the second
paragraph of the commit message.


 +int qemuMonitorJSONGetKVMState(qemuMonitorPtr mon,
 +   bool *enabled,
 +   bool *present)
 +{
 +int ret;
 +virJSONValuePtr cmd = NULL;
 +virJSONValuePtr reply = NULL;
 +virJSONValuePtr data = NULL;
 +
 +if (!(cmd = qemuMonitorJSONMakeCommand(query-kvm, NULL)))
 +return -1;
 +
 +ret = qemuMonitorJSONCommand(mon, cmd, reply);
 +
 +if (ret == 0) {
 +if (qemuMonitorJSONHasError(reply, CommandNotFound))
 +goto cleanup;

This relies on the caller to pre-initialize *enabled and *present to
false; it might be better to explicitly repeat that setting here so that
this function guarantees that the values are always correct on
successful return even if the caller forgot to initialize.  But right
now, the only caller happens to pre-initialize, so it's not a show-stopper.

ACK, once I pick which of the 2/2 variants I like best :)

-- 
Eric Blake   ebl...@redhat.com+1-919-301-3266
Libvirt virtualization library http://libvirt.org



signature.asc
Description: OpenPGP digital signature
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

[libvirt] [PATCH 1/2] qemu: Enhance QMP detection of KVM state

2012-10-28 Thread Martin Kletzander
When there is no 'qemu-kvm' binary and the emulator used for a machine
is, for example, 'qemu-system-x86_64' that, by default, runs without
kvm enabled, libvirt still supplies '-no-kvm' option to this process,
even though it does not recognize such option (making the start of a
domain fail in that case).

This patch adds QMP querying for KVM state using 'query-kvm' state,
but does not set any of QEMU_CAPS_KVM and QEMU_CAPS_ENABLE_KVM flags.
That functionality is done in different patch in order to be able to
compare two possibilities and chose the better one without looking at
the part of the code that's exactly the same for both of them (this
patch).
---
 src/qemu/qemu_capabilities.c | 23 +
 src/qemu/qemu_monitor.c  | 23 +
 src/qemu/qemu_monitor.h  |  4 
 src/qemu/qemu_monitor_json.c | 48 
 src/qemu/qemu_monitor_json.h |  5 +
 5 files changed, 103 insertions(+)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 115a054..fe462e9 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -2026,6 +2026,27 @@ qemuCapsProbeQMPCPUDefinitions(qemuCapsPtr caps,
 }


+static int
+qemuCapsProbeQMPKVMState(qemuCapsPtr caps,
+ qemuMonitorPtr mon)
+{
+bool enabled = false;
+bool present = false;
+
+if (!qemuCapsGet(caps, QEMU_CAPS_KVM))
+return 0;
+
+if (qemuMonitorGetKVMState(mon, enabled, present)  0)
+return -1;
+
+/* Youre right, this code does nothing, you must have checked out
+ * some weird commit.  Go back to your room and think about what
+ * you've done, young (wo)man. */
+
+return 0;
+}
+
+
 int qemuCapsProbeQMP(qemuCapsPtr caps,
  qemuMonitorPtr mon)
 {
@@ -2318,6 +2339,8 @@ qemuCapsInitQMP(qemuCapsPtr caps,
 goto cleanup;
 if (qemuCapsProbeQMPCPUDefinitions(caps, mon)  0)
 goto cleanup;
+if (qemuCapsProbeQMPKVMState(caps, mon)  0)
+goto cleanup;

 ret = 0;

diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
index 2d9c44c..2ce52f6 100644
--- a/src/qemu/qemu_monitor.c
+++ b/src/qemu/qemu_monitor.c
@@ -3183,6 +3183,29 @@ int qemuMonitorGetEvents(qemuMonitorPtr mon,
 }


+int qemuMonitorGetKVMState(qemuMonitorPtr mon,
+   bool *enabled,
+   bool *present)
+{
+VIR_DEBUG(mon=%p enabled=%p present=%p,
+  mon, enabled, present);
+
+if (!mon) {
+virReportError(VIR_ERR_INVALID_ARG, %s,
+   _(monitor must not be NULL));
+return -1;
+}
+
+if (!mon-json) {
+virReportError(VIR_ERR_CONFIG_UNSUPPORTED, %s,
+   _(JSON monitor is required));
+return -1;
+}
+
+return qemuMonitorJSONGetKVMState(mon, enabled, present);
+}
+
+
 int qemuMonitorGetObjectTypes(qemuMonitorPtr mon,
   char ***types)
 {
diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h
index 8856d9f..ccc3ab6 100644
--- a/src/qemu/qemu_monitor.h
+++ b/src/qemu/qemu_monitor.h
@@ -608,6 +608,10 @@ int qemuMonitorGetCommands(qemuMonitorPtr mon,
 int qemuMonitorGetEvents(qemuMonitorPtr mon,
  char ***events);

+int qemuMonitorGetKVMState(qemuMonitorPtr mon,
+   bool *enabled,
+   bool *present);
+
 int qemuMonitorGetObjectTypes(qemuMonitorPtr mon,
   char ***types);
 int qemuMonitorGetObjectProps(qemuMonitorPtr mon,
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index e495c0a..b32cec2 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -4218,6 +4218,54 @@ cleanup:
 }


+int qemuMonitorJSONGetKVMState(qemuMonitorPtr mon,
+   bool *enabled,
+   bool *present)
+{
+int ret;
+virJSONValuePtr cmd = NULL;
+virJSONValuePtr reply = NULL;
+virJSONValuePtr data = NULL;
+
+if (!(cmd = qemuMonitorJSONMakeCommand(query-kvm, NULL)))
+return -1;
+
+ret = qemuMonitorJSONCommand(mon, cmd, reply);
+
+if (ret == 0) {
+if (qemuMonitorJSONHasError(reply, CommandNotFound))
+goto cleanup;
+
+ret = qemuMonitorJSONCheckError(cmd, reply);
+}
+
+if (ret  0)
+goto cleanup;
+
+ret = -1;
+
+if (!(data = virJSONValueObjectGet(reply, return))) {
+virReportError(VIR_ERR_INTERNAL_ERROR, %s,
+   _(query-kvm reply was missing return data));
+goto cleanup;
+}
+
+if (virJSONValueObjectGetBoolean(data, enabled, enabled)  0 ||
+virJSONValueObjectGetBoolean(data, present, present)  0) {
+virReportError(VIR_ERR_INTERNAL_ERROR, %s,
+   _(query-kvm replied unexpected data));
+goto cleanup;
+}
+
+ret = 0;
+
+cleanup:
+virJSONValueFree(cmd);
+