blob: 7f5dd5f85a787701b5941fe9833c2f83370ca04b [file] [log] [blame]
Andreas Färber7fe55c32015-03-13 17:21:11 +01001/*
Thomas Huth152e0392017-10-06 12:13:51 +02002 * QTest testcase for CPU plugging
Andreas Färber7fe55c32015-03-13 17:21:11 +01003 *
4 * Copyright (c) 2015 SUSE Linux GmbH
5 *
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
8 */
9
Peter Maydell681c28a2016-02-08 18:08:51 +000010#include "qemu/osdep.h"
Andreas Färber7fe55c32015-03-13 17:21:11 +010011
Thomas Huthdd210742019-09-03 07:50:26 +020012#include "libqtest-single.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010013#include "qapi/qmp/qdict.h"
Igor Mammedov021a0072019-08-30 07:07:23 -040014#include "qapi/qmp/qlist.h"
Andreas Färber7fe55c32015-03-13 17:21:11 +010015
Thomas Huth152e0392017-10-06 12:13:51 +020016struct PlugTestData {
Marc-André Lureau34e46f62016-07-18 14:56:51 +040017 char *machine;
Andreas Färber7fe55c32015-03-13 17:21:11 +010018 const char *cpu_model;
Thomas Huth80b8c0b2017-10-06 13:32:13 +020019 char *device_model;
Andreas Färber7fe55c32015-03-13 17:21:11 +010020 unsigned sockets;
21 unsigned cores;
22 unsigned threads;
23 unsigned maxcpus;
24};
Thomas Huth152e0392017-10-06 12:13:51 +020025typedef struct PlugTestData PlugTestData;
Andreas Färber7fe55c32015-03-13 17:21:11 +010026
Igor Mammedov021a0072019-08-30 07:07:23 -040027static void test_plug_with_device_add(gconstpointer data)
Thomas Huth80b8c0b2017-10-06 13:32:13 +020028{
29 const PlugTestData *td = data;
30 char *args;
Thomas Huthe5758de2019-07-22 17:10:55 +020031 QTestState *qts;
Igor Mammedov021a0072019-08-30 07:07:23 -040032 QDict *resp;
33 QList *cpus;
34 QObject *e;
35 int hotplugged = 0;
Thomas Huth80b8c0b2017-10-06 13:32:13 +020036
37 args = g_strdup_printf("-machine %s -cpu %s "
Igor Mammedovbc1fb852018-09-13 13:06:01 +020038 "-smp 1,sockets=%u,cores=%u,threads=%u,maxcpus=%u",
Thomas Huth80b8c0b2017-10-06 13:32:13 +020039 td->machine, td->cpu_model,
40 td->sockets, td->cores, td->threads, td->maxcpus);
Thomas Huthe5758de2019-07-22 17:10:55 +020041 qts = qtest_init(args);
Thomas Huth80b8c0b2017-10-06 13:32:13 +020042
Igor Mammedov021a0072019-08-30 07:07:23 -040043 resp = qtest_qmp(qts, "{ 'execute': 'query-hotpluggable-cpus'}");
44 g_assert(qdict_haskey(resp, "return"));
45 cpus = qdict_get_qlist(resp, "return");
46 g_assert(cpus);
47
48 while ((e = qlist_pop(cpus))) {
49 const QDict *cpu, *props;
50
51 cpu = qobject_to(QDict, e);
52 if (qdict_haskey(cpu, "qom-path")) {
Marc-André Lureau74130912019-11-07 23:27:31 +040053 qobject_unref(e);
Igor Mammedov021a0072019-08-30 07:07:23 -040054 continue;
Thomas Huth80b8c0b2017-10-06 13:32:13 +020055 }
Igor Mammedov021a0072019-08-30 07:07:23 -040056
57 g_assert(qdict_haskey(cpu, "props"));
58 props = qdict_get_qdict(cpu, "props");
59
60 qtest_qmp_device_add_qdict(qts, td->device_model, props);
61 hotplugged++;
Marc-André Lureau74130912019-11-07 23:27:31 +040062 qobject_unref(e);
Thomas Huth80b8c0b2017-10-06 13:32:13 +020063 }
64
Igor Mammedov021a0072019-08-30 07:07:23 -040065 /* make sure that there were hotplugged CPUs */
66 g_assert(hotplugged);
67 qobject_unref(resp);
Thomas Huthe5758de2019-07-22 17:10:55 +020068 qtest_quit(qts);
Thomas Huth73a7d312017-10-06 15:53:19 +020069 g_free(args);
70}
71
Marc-André Lureau34e46f62016-07-18 14:56:51 +040072static void test_data_free(gpointer data)
73{
Thomas Huth152e0392017-10-06 12:13:51 +020074 PlugTestData *pc = data;
Marc-André Lureau34e46f62016-07-18 14:56:51 +040075
76 g_free(pc->machine);
Thomas Huth80b8c0b2017-10-06 13:32:13 +020077 g_free(pc->device_model);
Marc-André Lureau34e46f62016-07-18 14:56:51 +040078 g_free(pc);
79}
80
Thomas Huth02ef6e82017-03-30 09:50:06 +020081static void add_pc_test_case(const char *mname)
Andreas Färber7fe55c32015-03-13 17:21:11 +010082{
Marc-André Lureau34e46f62016-07-18 14:56:51 +040083 char *path;
Thomas Huth152e0392017-10-06 12:13:51 +020084 PlugTestData *data;
Andreas Färber7fe55c32015-03-13 17:21:11 +010085
Thomas Huth02ef6e82017-03-30 09:50:06 +020086 if (!g_str_has_prefix(mname, "pc-")) {
87 return;
Andreas Färber7fe55c32015-03-13 17:21:11 +010088 }
Thomas Huth152e0392017-10-06 12:13:51 +020089 data = g_new(PlugTestData, 1);
Thomas Huth02ef6e82017-03-30 09:50:06 +020090 data->machine = g_strdup(mname);
91 data->cpu_model = "Haswell"; /* 1.3+ theoretically */
Thomas Huth80b8c0b2017-10-06 13:32:13 +020092 data->device_model = g_strdup_printf("%s-%s-cpu", data->cpu_model,
93 qtest_get_arch());
Thomas Huth02ef6e82017-03-30 09:50:06 +020094 data->sockets = 1;
95 data->cores = 3;
96 data->threads = 2;
Igor Mammedovbc1fb852018-09-13 13:06:01 +020097 data->maxcpus = data->sockets * data->cores * data->threads;
Thomas Huth80b8c0b2017-10-06 13:32:13 +020098
Igor Mammedov63e79832020-09-15 08:04:03 -040099 path = g_strdup_printf("cpu-plug/%s/device-add/%ux%ux%u&maxcpus=%u",
100 mname, data->sockets, data->cores,
101 data->threads, data->maxcpus);
102 qtest_add_data_func_full(path, data, test_plug_with_device_add,
103 test_data_free);
104 g_free(path);
Andreas Färber7fe55c32015-03-13 17:21:11 +0100105}
106
Thomas Huth73a7d312017-10-06 15:53:19 +0200107static void add_pseries_test_case(const char *mname)
108{
109 char *path;
110 PlugTestData *data;
111
112 if (!g_str_has_prefix(mname, "pseries-") ||
113 (g_str_has_prefix(mname, "pseries-2.") && atoi(&mname[10]) < 7)) {
114 return;
115 }
116 data = g_new(PlugTestData, 1);
117 data->machine = g_strdup(mname);
118 data->cpu_model = "power8_v2.0";
119 data->device_model = g_strdup("power8_v2.0-spapr-cpu-core");
120 data->sockets = 2;
121 data->cores = 3;
122 data->threads = 1;
Igor Mammedovbc1fb852018-09-13 13:06:01 +0200123 data->maxcpus = data->sockets * data->cores * data->threads;
Thomas Huth73a7d312017-10-06 15:53:19 +0200124
125 path = g_strdup_printf("cpu-plug/%s/device-add/%ux%ux%u&maxcpus=%u",
126 mname, data->sockets, data->cores,
127 data->threads, data->maxcpus);
Igor Mammedov021a0072019-08-30 07:07:23 -0400128 qtest_add_data_func_full(path, data, test_plug_with_device_add,
Thomas Huth73a7d312017-10-06 15:53:19 +0200129 test_data_free);
130 g_free(path);
131}
132
Thomas Huth7d8b00f2017-10-17 13:39:15 +0200133static void add_s390x_test_case(const char *mname)
134{
135 char *path;
Igor Mammedov63e79832020-09-15 08:04:03 -0400136 PlugTestData *data;
Thomas Huth7d8b00f2017-10-17 13:39:15 +0200137
138 if (!g_str_has_prefix(mname, "s390-ccw-virtio-")) {
139 return;
140 }
141
142 data = g_new(PlugTestData, 1);
143 data->machine = g_strdup(mname);
144 data->cpu_model = "qemu";
145 data->device_model = g_strdup("qemu-s390x-cpu");
146 data->sockets = 1;
147 data->cores = 3;
148 data->threads = 1;
Igor Mammedovbc1fb852018-09-13 13:06:01 +0200149 data->maxcpus = data->sockets * data->cores * data->threads;
Thomas Huth7d8b00f2017-10-17 13:39:15 +0200150
Igor Mammedov63e79832020-09-15 08:04:03 -0400151 path = g_strdup_printf("cpu-plug/%s/device-add/%ux%ux%u&maxcpus=%u",
Thomas Huth7d8b00f2017-10-17 13:39:15 +0200152 mname, data->sockets, data->cores,
153 data->threads, data->maxcpus);
Igor Mammedov63e79832020-09-15 08:04:03 -0400154 qtest_add_data_func_full(path, data, test_plug_with_device_add,
Thomas Huth7d8b00f2017-10-17 13:39:15 +0200155 test_data_free);
156 g_free(path);
157}
158
Andreas Färber7fe55c32015-03-13 17:21:11 +0100159int main(int argc, char **argv)
160{
161 const char *arch = qtest_get_arch();
162
163 g_test_init(&argc, &argv, NULL);
164
165 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
Thomas Huth1f4a0d82018-08-16 13:35:55 +0200166 qtest_cb_for_every_machine(add_pc_test_case, g_test_quick());
Thomas Huth73a7d312017-10-06 15:53:19 +0200167 } else if (g_str_equal(arch, "ppc64")) {
Thomas Huth1f4a0d82018-08-16 13:35:55 +0200168 qtest_cb_for_every_machine(add_pseries_test_case, g_test_quick());
Thomas Huth7d8b00f2017-10-17 13:39:15 +0200169 } else if (g_str_equal(arch, "s390x")) {
Thomas Huth1f4a0d82018-08-16 13:35:55 +0200170 qtest_cb_for_every_machine(add_s390x_test_case, g_test_quick());
Andreas Färber7fe55c32015-03-13 17:21:11 +0100171 }
172
173 return g_test_run();
174}