render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
Blob Blame History Raw
From 8d2d51fab95e51c9e4625ef4f5424db2f6a548eb Mon Sep 17 00:00:00 2001
Message-Id: <8d2d51fab95e51c9e4625ef4f5424db2f6a548eb.1383922566.git.jdenemar@redhat.com>
From: Jiri Denemark <jdenemar@redhat.com>
Date: Fri, 8 Nov 2013 12:33:31 +0100
Subject: [PATCH] qemu: Add monitor APIs to fetch CPUID data from QEMU

https://bugzilla.redhat.com/show_bug.cgi?id=1008989

The qemu monitor supports retrieval of actual CPUID bits presented to
the guest using QMP monitor. Add APIs to extract these information and
tests for them.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
(cherry picked from commit 3afde0756ff1b87912f36268fd80617c34f1164a)

    Conflicts:
    tests/qemumonitorjsontest.c: recent changes to the testsuite changed
    arguments for virTestRun and instantiation of the monitor test
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
---
 src/qemu/qemu_monitor.c                            |  31 +++++
 src/qemu/qemu_monitor.h                            |   4 +
 src/qemu/qemu_monitor_json.c                       | 133 +++++++++++++++++++++
 src/qemu/qemu_monitor_json.h                       |   2 +
 tests/Makefile.am                                  |   1 +
 .../qemumonitorjson-getcpu-full.data               |   5 +
 .../qemumonitorjson-getcpu-full.json               |  46 +++++++
 .../qemumonitorjson-getcpu-host.data               |   6 +
 .../qemumonitorjson-getcpu-host.json               |  45 +++++++
 tests/qemumonitorjsontest.c                        |  75 ++++++++++++
 10 files changed, 348 insertions(+)
 create mode 100644 tests/qemumonitorjsondata/qemumonitorjson-getcpu-full.data
 create mode 100644 tests/qemumonitorjsondata/qemumonitorjson-getcpu-full.json
 create mode 100644 tests/qemumonitorjsondata/qemumonitorjson-getcpu-host.data
 create mode 100644 tests/qemumonitorjsondata/qemumonitorjson-getcpu-host.json

diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
index cf4f954..4384ab4 100644
--- a/src/qemu/qemu_monitor.c
+++ b/src/qemu/qemu_monitor.c
@@ -3922,3 +3922,34 @@ qemuMonitorSetDomainLog(qemuMonitorPtr mon, int logfd)
 
     return 0;
 }
+
+
+/**
+ * qemuMonitorJSONGetGuestCPU:
+ * @mon: Pointer to the monitor
+ * @arch: arch of the guest
+ *
+ * Retrieve the definition of the guest CPU from a running qemu instance.
+ *
+ * Returns the cpu definition object. On error returns NULL.
+ */
+virCPUDataPtr
+qemuMonitorGetGuestCPU(qemuMonitorPtr mon,
+                       virArch arch)
+{
+    VIR_DEBUG("mon=%p, arch='%s'", mon, virArchToString(arch));
+
+    if (!mon) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
+                       _("monitor must not be NULL"));
+        return NULL;
+    }
+
+    if (!mon->json) {
+        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
+                       _("JSON monitor is required"));
+        return NULL;
+    }
+
+    return qemuMonitorJSONGetGuestCPU(mon, arch);
+}
diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h
index 85ae533..3e113f3 100644
--- a/src/qemu/qemu_monitor.h
+++ b/src/qemu/qemu_monitor.h
@@ -32,6 +32,7 @@
 # include "virhash.h"
 # include "virjson.h"
 # include "device_conf.h"
+# include "cpu/cpu.h"
 
 typedef struct _qemuMonitor qemuMonitor;
 typedef qemuMonitor *qemuMonitorPtr;
@@ -719,6 +720,9 @@ int qemuMonitorGetDeviceAliases(qemuMonitorPtr mon,
 
 int qemuMonitorSetDomainLog(qemuMonitorPtr mon, int logfd);
 
+virCPUDataPtr qemuMonitorGetGuestCPU(qemuMonitorPtr mon,
+                                     virArch arch);
+
 /**
  * When running two dd process and using <> redirection, we need a
  * shell that will not truncate files.  These two strings serve that
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index 12f7e69..ef5c08a 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -42,6 +42,7 @@
 #include "virerror.h"
 #include "virjson.h"
 #include "virstring.h"
+#include "cpu/cpu_x86.h"
 
 #ifdef WITH_DTRACE_PROBES
 # include "libvirt_qemu_probes.h"
@@ -49,6 +50,7 @@
 
 #define VIR_FROM_THIS VIR_FROM_QEMU
 
+#define QOM_CPU_PATH  "/machine/unattached/device[0]"
 
 #define LINE_ENDING "\r\n"
 
@@ -5453,3 +5455,134 @@ cleanup:
     VIR_FREE(paths);
     return ret;
 }
+
+
+static int
+qemuMonitorJSONParseCPUx86FeatureWord(virJSONValuePtr data,
+                                      virCPUx86CPUID *cpuid)
+{
+    const char *reg;
+    unsigned long long fun;
+    unsigned long long features;
+
+    memset(cpuid, 0, sizeof(*cpuid));
+
+    if (!(reg = virJSONValueObjectGetString(data, "cpuid-register"))) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("missing cpuid-register in CPU data"));
+        return -1;
+    }
+    if (virJSONValueObjectGetNumberUlong(data, "cpuid-input-eax", &fun) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("missing or invalid cpuid-input-eax in CPU data"));
+        return -1;
+    }
+    if (virJSONValueObjectGetNumberUlong(data, "features", &features) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("missing or invalid features in CPU data"));
+        return -1;
+    }
+
+    cpuid->function = fun;
+    if (STREQ(reg, "EAX")) {
+        cpuid->eax = features;
+    } else if (STREQ(reg, "EBX")) {
+        cpuid->ebx = features;
+    } else if (STREQ(reg, "ECX")) {
+        cpuid->ecx = features;
+    } else if (STREQ(reg, "EDX")) {
+        cpuid->edx = features;
+    } else {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("unknown CPU register '%s'"), reg);
+        return -1;
+    }
+
+    return 0;
+}
+
+
+static virCPUDataPtr
+qemuMonitorJSONGetCPUx86Data(qemuMonitorPtr mon,
+                             const char *property)
+{
+    virJSONValuePtr cmd;
+    virJSONValuePtr reply = NULL;
+    virJSONValuePtr data;
+    virCPUx86Data *x86Data = NULL;
+    virCPUx86CPUID cpuid;
+    size_t i;
+    virCPUDataPtr ret = NULL;
+    int n;
+
+    if (!(cmd = qemuMonitorJSONMakeCommand("qom-get",
+                                           "s:path", QOM_CPU_PATH,
+                                           "s:property", property,
+                                           NULL)))
+        return NULL;
+
+    if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0)
+        goto cleanup;
+
+    if (qemuMonitorJSONCheckError(cmd, reply))
+        goto cleanup;
+
+    if (!(data = virJSONValueObjectGet(reply, "return"))) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("qom-get reply was missing return data"));
+        goto cleanup;
+    }
+
+    if ((n = virJSONValueArraySize(data)) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("%s CPU property did not return an array"),
+                       property);
+        goto cleanup;
+    }
+
+    if (VIR_ALLOC(x86Data) < 0)
+        goto cleanup;
+
+    for (i = 0; i < n; i++) {
+        if (qemuMonitorJSONParseCPUx86FeatureWord(virJSONValueArrayGet(data, i),
+                                                  &cpuid) < 0 ||
+            virCPUx86DataAddCPUID(x86Data, &cpuid) < 0)
+            goto cleanup;
+    }
+
+    if (!(ret = virCPUx86MakeData(VIR_ARCH_X86_64, &x86Data)))
+        goto cleanup;
+
+cleanup:
+    virJSONValueFree(cmd);
+    virJSONValueFree(reply);
+    virCPUx86DataFree(x86Data);
+    return ret;
+}
+
+
+/**
+ * qemuMonitorJSONGetGuestCPU:
+ * @mon: Pointer to the monitor
+ * @arch: arch of the guest
+ *
+ * Retrieve the definition of the guest CPU from a running qemu instance.
+ *
+ * Returns the cpu definition object. On error returns NULL.
+ */
+virCPUDataPtr
+qemuMonitorJSONGetGuestCPU(qemuMonitorPtr mon,
+                           virArch arch)
+{
+    switch (arch) {
+    case VIR_ARCH_X86_64:
+    case VIR_ARCH_I686:
+        return qemuMonitorJSONGetCPUx86Data(mon, "feature-words");
+
+    default:
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("CPU definition retrieval isn't supported for '%s'"),
+                       virArchToString(arch));
+        return NULL;
+    }
+}
diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h
index 51cf19c..2fdc28f 100644
--- a/src/qemu/qemu_monitor_json.h
+++ b/src/qemu/qemu_monitor_json.h
@@ -29,6 +29,7 @@
 
 # include "qemu_monitor.h"
 # include "virbitmap.h"
+# include "cpu/cpu.h"
 
 int qemuMonitorJSONIOProcess(qemuMonitorPtr mon,
                              const char *data,
@@ -426,4 +427,5 @@ int qemuMonitorJSONDetachCharDev(qemuMonitorPtr mon,
 int qemuMonitorJSONGetDeviceAliases(qemuMonitorPtr mon,
                                     char ***aliases);
 
+virCPUDataPtr qemuMonitorJSONGetGuestCPU(qemuMonitorPtr mon, virArch arch);
 #endif /* QEMU_MONITOR_JSON_H */
diff --git a/tests/Makefile.am b/tests/Makefile.am
index b794005..d095595 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -82,6 +82,7 @@ EXTRA_DIST =		\
 	oomtrace.pl \
 	qemuhelpdata \
 	qemuhotplugtestdata \
+	qemumonitorjsondata \
 	qemuxml2argvdata \
 	qemuxml2xmloutdata \
 	qemuxmlnsdata \
diff --git a/tests/qemumonitorjsondata/qemumonitorjson-getcpu-full.data b/tests/qemumonitorjsondata/qemumonitorjson-getcpu-full.data
new file mode 100644
index 0000000..bba8d31
--- /dev/null
+++ b/tests/qemumonitorjsondata/qemumonitorjson-getcpu-full.data
@@ -0,0 +1,5 @@
+<cpudata arch='x86'>
+  <cpuid function='0x00000001' eax='0x00000000' ebx='0x00000000' ecx='0x97ba2223' edx='0x078bfbfd'/>
+  <cpuid function='0x40000001' eax='0x0100003b' ebx='0x00000000' ecx='0x00000000' edx='0x00000000'/>
+  <cpuid function='0x80000001' eax='0x00000000' ebx='0x00000000' ecx='0x00000001' edx='0x28100800'/>
+</cpudata>
diff --git a/tests/qemumonitorjsondata/qemumonitorjson-getcpu-full.json b/tests/qemumonitorjsondata/qemumonitorjson-getcpu-full.json
new file mode 100644
index 0000000..29c00b4
--- /dev/null
+++ b/tests/qemumonitorjsondata/qemumonitorjson-getcpu-full.json
@@ -0,0 +1,46 @@
+{
+    "return": [
+        {
+            "cpuid-register": "EDX",
+            "cpuid-input-eax": 2147483658,
+            "features": 0
+        },
+        {
+            "cpuid-register": "EAX",
+            "cpuid-input-eax": 1073741825,
+            "features": 16777275
+        },
+        {
+            "cpuid-register": "EDX",
+            "cpuid-input-eax": 3221225473,
+            "features": 0
+        },
+        {
+            "cpuid-register": "ECX",
+            "cpuid-input-eax": 2147483649,
+            "features": 1
+        },
+        {
+            "cpuid-register": "EDX",
+            "cpuid-input-eax": 2147483649,
+            "features": 672139264
+        },
+        {
+            "cpuid-register": "EBX",
+            "cpuid-input-ecx": 0,
+            "cpuid-input-eax": 7,
+            "features": 0
+        },
+        {
+            "cpuid-register": "ECX",
+            "cpuid-input-eax": 1,
+            "features": 2545558051
+        },
+        {
+            "cpuid-register": "EDX",
+            "cpuid-input-eax": 1,
+            "features": 126614525
+        }
+    ],
+    "id": "libvirt-6"
+}
diff --git a/tests/qemumonitorjsondata/qemumonitorjson-getcpu-host.data b/tests/qemumonitorjsondata/qemumonitorjson-getcpu-host.data
new file mode 100644
index 0000000..98b9e7c
--- /dev/null
+++ b/tests/qemumonitorjsondata/qemumonitorjson-getcpu-host.data
@@ -0,0 +1,6 @@
+<cpudata arch='x86'>
+  <cpuid function='0x00000001' eax='0x00000000' ebx='0x00000000' ecx='0x97ba2223' edx='0x0f8bfbff'/>
+  <cpuid function='0x00000007' eax='0x00000000' ebx='0x00000002' ecx='0x00000000' edx='0x00000000'/>
+  <cpuid function='0x40000001' eax='0x0100007b' ebx='0x00000000' ecx='0x00000000' edx='0x00000000'/>
+  <cpuid function='0x80000001' eax='0x00000000' ebx='0x00000000' ecx='0x00000001' edx='0x2993fbff'/>
+</cpudata>
diff --git a/tests/qemumonitorjsondata/qemumonitorjson-getcpu-host.json b/tests/qemumonitorjsondata/qemumonitorjson-getcpu-host.json
new file mode 100644
index 0000000..b5fb9f3
--- /dev/null
+++ b/tests/qemumonitorjsondata/qemumonitorjson-getcpu-host.json
@@ -0,0 +1,45 @@
+{
+    "return": [
+        {
+            "cpuid-register": "EDX",
+            "cpuid-input-eax": 2147483658,
+            "features": 0
+        },
+        {
+            "cpuid-register": "EAX",
+            "cpuid-input-eax": 1073741825,
+            "features": 16777339
+        },
+        {
+            "cpuid-register": "EDX",
+            "cpuid-input-eax": 3221225473,
+            "features": 0
+        },
+        {
+            "cpuid-register": "ECX",
+            "cpuid-input-eax": 2147483649,
+            "features": 1
+        },
+        {
+            "cpuid-register": "EDX",
+            "cpuid-input-eax": 2147483649,
+            "features": 697564159
+        },
+        {
+            "cpuid-register": "EBX",
+            "cpuid-input-ecx": 0,
+            "cpuid-input-eax": 7,
+            "features": 2
+        },
+        {
+            "cpuid-register": "ECX",
+            "cpuid-input-eax": 1,
+            "features": 2545558051
+        },
+        {
+            "cpuid-register": "EDX",
+            "cpuid-input-eax": 1,
+            "features": 260832255
+        }
+    ]
+}
diff --git a/tests/qemumonitorjsontest.c b/tests/qemumonitorjsontest.c
index 9e66059..f709de7 100644
--- a/tests/qemumonitorjsontest.c
+++ b/tests/qemumonitorjsontest.c
@@ -27,6 +27,7 @@
 #include "virthread.h"
 #include "virerror.h"
 #include "virstring.h"
+#include "cpu/cpu.h"
 
 
 #define VIR_FROM_THIS VIR_FROM_NONE
@@ -943,6 +944,69 @@ cleanup:
 }
 
 
+
+struct testCPUData {
+    const char *name;
+    virDomainXMLOptionPtr xmlopt;
+};
+
+
+static int
+testQemuMonitorJSONGetCPUData(const void *opaque)
+{
+    const struct testCPUData *data = opaque;
+    qemuMonitorTestPtr test = qemuMonitorTestNew(true, data->xmlopt);
+    virCPUDataPtr cpuData = NULL;
+    char *jsonFile = NULL;
+    char *dataFile = NULL;
+    char *jsonStr = NULL;
+    char *expected = NULL;
+    char *actual = NULL;
+    int ret = -1;
+
+    if (!test)
+        return -1;
+
+    if (virAsprintf(&jsonFile,
+                    "%s/qemumonitorjsondata/qemumonitorjson-getcpu-%s.json",
+                    abs_srcdir, data->name) < 0 ||
+        virAsprintf(&dataFile,
+                    "%s/qemumonitorjsondata/qemumonitorjson-getcpu-%s.data",
+                    abs_srcdir, data->name) < 0)
+        goto cleanup;
+
+    if (virtTestLoadFile(jsonFile, &jsonStr) < 0 ||
+        virtTestLoadFile(dataFile, &expected) < 0)
+        goto cleanup;
+
+    if (qemuMonitorTestAddItem(test, "qom-get", jsonStr) < 0)
+        goto cleanup;
+
+    if (!(cpuData = qemuMonitorJSONGetGuestCPU(qemuMonitorTestGetMonitor(test),
+                                               VIR_ARCH_X86_64)))
+        goto cleanup;
+
+    if (!(actual = cpuDataFormat(cpuData)))
+        goto cleanup;
+
+    if (STRNEQ(expected, actual)) {
+        virtTestDifference(stderr, expected, actual);
+        goto cleanup;
+    }
+
+    ret = 0;
+cleanup:
+    VIR_FREE(jsonFile);
+    VIR_FREE(dataFile);
+    VIR_FREE(jsonStr);
+    VIR_FREE(expected);
+    VIR_FREE(actual);
+    cpuDataFree(cpuData);
+    qemuMonitorTestFree(test);
+    return ret;
+}
+
+
 static int
 mymain(void)
 {
@@ -964,6 +1028,14 @@ mymain(void)
     if (virtTestRun(# name, 1, testQemuMonitorJSON ## name, xmlopt) < 0) \
         ret = -1
 
+#define DO_TEST_CPU_DATA(name) \
+    do {                                                                  \
+        struct testCPUData data = { name, xmlopt };                       \
+        const char *label = "GetCPUData(" name ")";                       \
+        if (virtTestRun(label, 1, testQemuMonitorJSONGetCPUData, &data) < 0) \
+            ret = -1;                                                     \
+    } while (0)
+
     DO_TEST(GetStatus);
     DO_TEST(GetVersion);
     DO_TEST(GetMachines);
@@ -978,6 +1050,9 @@ mymain(void)
     DO_TEST(SetObjectProperty);
     DO_TEST(GetDeviceAliases);
 
+    DO_TEST_CPU_DATA("host");
+    DO_TEST_CPU_DATA("full");
+
     virObjectUnref(xmlopt);
 
     return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
-- 
1.8.4.2