[libvirt] [PATCH v2 05/31] qemu: Enable KVM when probing capabilities

Jiri Denemark jdenemar at redhat.com
Sun Nov 20 23:21:01 UTC 2016


CPU related capabilities may differ depending on accelerator used when
probing. Let's use KVM if available when probing QEMU and fall back to
TCG. The created capabilities already contain all we need to distinguish
whether KVM or TCG was used:

    - KVM was used when probing capabilities:
        QEMU_CAPS_KVM is set
        QEMU_CAPS_ENABLE_KVM is not set

    - TCG was used and QEMU supports KVM, but it failed (e.g., missing
      kernel module or wrong /dev/kvm permissions)
        QEMU_CAPS_KVM is not set
        QEMU_CAPS_ENABLE_KVM is set

    - KVM was not used and QEMU does not support it
        QEMU_CAPS_KVM is not set
        QEMU_CAPS_ENABLE_KVM is not set

Signed-off-by: Jiri Denemark <jdenemar at redhat.com>
---
 src/qemu/qemu_capabilities.c | 45 +++++++++++++++++++++++++++++++++++++++++---
 src/qemu/qemu_capabilities.h |  3 ---
 src/qemu/qemu_capspriv.h     |  8 ++++++++
 tests/qemucapabilitiestest.c |  5 +++++
 4 files changed, 55 insertions(+), 6 deletions(-)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index fe0705f..590b802 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -4026,6 +4026,25 @@ virQEMUCapsInitQMPMonitor(virQEMUCapsPtr qemuCaps,
 }
 
 
+int
+virQEMUCapsInitQMPMonitorTCG(virQEMUCapsPtr qemuCaps ATTRIBUTE_UNUSED,
+                             qemuMonitorPtr mon)
+{
+    int ret = -1;
+
+    if (qemuMonitorSetCapabilities(mon) < 0) {
+        VIR_DEBUG("Failed to set monitor capabilities %s",
+                  virGetLastErrorMessage());
+        ret = 0;
+        goto cleanup;
+    }
+
+    ret = 0;
+ cleanup:
+    return ret;
+}
+
+
 typedef struct _virQEMUCapsInitQMPCommand virQEMUCapsInitQMPCommand;
 typedef virQEMUCapsInitQMPCommand *virQEMUCapsInitQMPCommandPtr;
 struct _virQEMUCapsInitQMPCommand {
@@ -4146,12 +4165,20 @@ virQEMUCapsInitQMPCommandNew(char *binary,
 
 static qemuMonitorPtr
 virQEMUCapsInitQMPCommandRun(virQEMUCapsInitQMPCommandPtr cmd,
+                             bool forceTCG,
                              bool *qemuFailed)
 {
     virDomainXMLOptionPtr xmlopt = NULL;
+    const char *machine;
     int status = 0;
 
-    VIR_DEBUG("Try to probe capabilities of '%s' via QMP", cmd->binary);
+    if (forceTCG)
+        machine = "none,accel=tcg";
+    else
+        machine = "none,accel=kvm:tcg";
+
+    VIR_DEBUG("Try to probe capabilities of '%s' via QMP, machine %s",
+              cmd->binary, machine);
 
     /*
      * We explicitly need to use -daemonize here, rather than
@@ -4165,7 +4192,7 @@ virQEMUCapsInitQMPCommandRun(virQEMUCapsInitQMPCommandPtr cmd,
                                     "-no-user-config",
                                     "-nodefaults",
                                     "-nographic",
-                                    "-machine", "none",
+                                    "-machine", machine,
                                     "-qmp", cmd->monarg,
                                     "-pidfile", cmd->pidfile,
                                     "-daemonize",
@@ -4233,7 +4260,7 @@ virQEMUCapsInitQMP(virQEMUCapsPtr qemuCaps,
                                              runUid, runGid, qmperr)))
         goto cleanup;
 
-    if (!(mon = virQEMUCapsInitQMPCommandRun(cmd, &qemuFailed))) {
+    if (!(mon = virQEMUCapsInitQMPCommandRun(cmd, false, &qemuFailed))) {
         if (qemuFailed)
             ret = 0;
         goto cleanup;
@@ -4242,6 +4269,18 @@ virQEMUCapsInitQMP(virQEMUCapsPtr qemuCaps,
     if (virQEMUCapsInitQMPMonitor(qemuCaps, mon) < 0)
         goto cleanup;
 
+    if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_KVM)) {
+        virQEMUCapsInitQMPCommandAbort(cmd);
+        if (!(mon = virQEMUCapsInitQMPCommandRun(cmd, true, &qemuFailed))) {
+            if (qemuFailed)
+                ret = 0;
+            goto cleanup;
+        }
+
+        if (virQEMUCapsInitQMPMonitorTCG(qemuCaps, mon) < 0)
+            goto cleanup;
+    }
+
     ret = 0;
 
  cleanup:
diff --git a/src/qemu/qemu_capabilities.h b/src/qemu/qemu_capabilities.h
index 9163572..bd7ff29 100644
--- a/src/qemu/qemu_capabilities.h
+++ b/src/qemu/qemu_capabilities.h
@@ -399,9 +399,6 @@ typedef virQEMUCapsCache *virQEMUCapsCachePtr;
 
 virQEMUCapsPtr virQEMUCapsNew(void);
 
-int virQEMUCapsInitQMPMonitor(virQEMUCapsPtr qemuCaps,
-                              qemuMonitorPtr mon);
-
 int virQEMUCapsProbeQMP(virQEMUCapsPtr qemuCaps,
                         qemuMonitorPtr mon);
 
diff --git a/src/qemu/qemu_capspriv.h b/src/qemu/qemu_capspriv.h
index f8ee47e..38b971e 100644
--- a/src/qemu/qemu_capspriv.h
+++ b/src/qemu/qemu_capspriv.h
@@ -57,6 +57,14 @@ char *virQEMUCapsFormatCache(virQEMUCapsPtr qemuCaps,
                              time_t selfCTime,
                              unsigned long selfVersion);
 
+int
+virQEMUCapsInitQMPMonitor(virQEMUCapsPtr qemuCaps,
+                          qemuMonitorPtr mon);
+
+int
+virQEMUCapsInitQMPMonitorTCG(virQEMUCapsPtr qemuCaps,
+                             qemuMonitorPtr mon);
+
 void
 virQEMUCapsSetArch(virQEMUCapsPtr qemuCaps,
                    virArch arch);
diff --git a/tests/qemucapabilitiestest.c b/tests/qemucapabilitiestest.c
index 10cfa43..fe41fc4 100644
--- a/tests/qemucapabilitiestest.c
+++ b/tests/qemucapabilitiestest.c
@@ -61,6 +61,11 @@ testQemuCaps(const void *opaque)
                                   qemuMonitorTestGetMonitor(mon)) < 0)
         goto cleanup;
 
+    if (virQEMUCapsGet(capsActual, QEMU_CAPS_KVM) &&
+        virQEMUCapsInitQMPMonitorTCG(capsActual,
+                                     qemuMonitorTestGetMonitor(mon)) < 0)
+        goto cleanup;
+
     if (!(actual = virQEMUCapsFormatCache(capsActual, 0, 0)))
         goto cleanup;
 
-- 
2.10.2




More information about the libvir-list mailing list