blob: 30e514bbfbd6bb433a7ee1c05273304c3932e893 [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
12#include "qemu-common.h"
Thomas Huthdd210742019-09-03 07:50:26 +020013#include "libqtest-single.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010014#include "qapi/qmp/qdict.h"
Igor Mammedov021a0072019-08-30 07:07:23 -040015#include "qapi/qmp/qlist.h"
Andreas Färber7fe55c32015-03-13 17:21:11 +010016
Thomas Huth152e0392017-10-06 12:13:51 +020017struct PlugTestData {
Marc-André Lureau34e46f62016-07-18 14:56:51 +040018 char *machine;
Andreas Färber7fe55c32015-03-13 17:21:11 +010019 const char *cpu_model;
Thomas Huth80b8c0b2017-10-06 13:32:13 +020020 char *device_model;
Andreas Färber7fe55c32015-03-13 17:21:11 +010021 unsigned sockets;
22 unsigned cores;
23 unsigned threads;
24 unsigned maxcpus;
25};
Thomas Huth152e0392017-10-06 12:13:51 +020026typedef struct PlugTestData PlugTestData;
Andreas Färber7fe55c32015-03-13 17:21:11 +010027
Thomas Huth152e0392017-10-06 12:13:51 +020028static void test_plug_with_cpu_add(gconstpointer data)
Andreas Färber7fe55c32015-03-13 17:21:11 +010029{
Thomas Huth152e0392017-10-06 12:13:51 +020030 const PlugTestData *s = data;
Andreas Färber7fe55c32015-03-13 17:21:11 +010031 char *args;
32 QDict *response;
33 unsigned int i;
34
35 args = g_strdup_printf("-machine %s -cpu %s "
Igor Mammedovbc1fb852018-09-13 13:06:01 +020036 "-smp 1,sockets=%u,cores=%u,threads=%u,maxcpus=%u",
Andreas Färber7fe55c32015-03-13 17:21:11 +010037 s->machine, s->cpu_model,
38 s->sockets, s->cores, s->threads, s->maxcpus);
39 qtest_start(args);
40
Igor Mammedovbc1fb852018-09-13 13:06:01 +020041 for (i = 1; i < s->maxcpus; i++) {
Andreas Färber7fe55c32015-03-13 17:21:11 +010042 response = qmp("{ 'execute': 'cpu-add',"
43 " 'arguments': { 'id': %d } }", i);
44 g_assert(response);
45 g_assert(!qdict_haskey(response, "error"));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +020046 qobject_unref(response);
Andreas Färber7fe55c32015-03-13 17:21:11 +010047 }
48
49 qtest_end();
50 g_free(args);
51}
52
Thomas Huth152e0392017-10-06 12:13:51 +020053static void test_plug_without_cpu_add(gconstpointer data)
Andreas Färber7fe55c32015-03-13 17:21:11 +010054{
Thomas Huth152e0392017-10-06 12:13:51 +020055 const PlugTestData *s = data;
Andreas Färber7fe55c32015-03-13 17:21:11 +010056 char *args;
57 QDict *response;
58
59 args = g_strdup_printf("-machine %s -cpu %s "
Igor Mammedovbc1fb852018-09-13 13:06:01 +020060 "-smp 1,sockets=%u,cores=%u,threads=%u,maxcpus=%u",
Andreas Färber7fe55c32015-03-13 17:21:11 +010061 s->machine, s->cpu_model,
62 s->sockets, s->cores, s->threads, s->maxcpus);
63 qtest_start(args);
64
65 response = qmp("{ 'execute': 'cpu-add',"
66 " 'arguments': { 'id': %d } }",
67 s->sockets * s->cores * s->threads);
68 g_assert(response);
69 g_assert(qdict_haskey(response, "error"));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +020070 qobject_unref(response);
Andreas Färber7fe55c32015-03-13 17:21:11 +010071
72 qtest_end();
73 g_free(args);
74}
75
Igor Mammedov021a0072019-08-30 07:07:23 -040076static void test_plug_with_device_add(gconstpointer data)
Thomas Huth80b8c0b2017-10-06 13:32:13 +020077{
78 const PlugTestData *td = data;
79 char *args;
Thomas Huthe5758de2019-07-22 17:10:55 +020080 QTestState *qts;
Igor Mammedov021a0072019-08-30 07:07:23 -040081 QDict *resp;
82 QList *cpus;
83 QObject *e;
84 int hotplugged = 0;
Thomas Huth80b8c0b2017-10-06 13:32:13 +020085
86 args = g_strdup_printf("-machine %s -cpu %s "
Igor Mammedovbc1fb852018-09-13 13:06:01 +020087 "-smp 1,sockets=%u,cores=%u,threads=%u,maxcpus=%u",
Thomas Huth80b8c0b2017-10-06 13:32:13 +020088 td->machine, td->cpu_model,
89 td->sockets, td->cores, td->threads, td->maxcpus);
Thomas Huthe5758de2019-07-22 17:10:55 +020090 qts = qtest_init(args);
Thomas Huth80b8c0b2017-10-06 13:32:13 +020091
Igor Mammedov021a0072019-08-30 07:07:23 -040092 resp = qtest_qmp(qts, "{ 'execute': 'query-hotpluggable-cpus'}");
93 g_assert(qdict_haskey(resp, "return"));
94 cpus = qdict_get_qlist(resp, "return");
95 g_assert(cpus);
96
97 while ((e = qlist_pop(cpus))) {
98 const QDict *cpu, *props;
99
100 cpu = qobject_to(QDict, e);
101 if (qdict_haskey(cpu, "qom-path")) {
Marc-André Lureau74130912019-11-07 23:27:31 +0400102 qobject_unref(e);
Igor Mammedov021a0072019-08-30 07:07:23 -0400103 continue;
Thomas Huth80b8c0b2017-10-06 13:32:13 +0200104 }
Igor Mammedov021a0072019-08-30 07:07:23 -0400105
106 g_assert(qdict_haskey(cpu, "props"));
107 props = qdict_get_qdict(cpu, "props");
108
109 qtest_qmp_device_add_qdict(qts, td->device_model, props);
110 hotplugged++;
Marc-André Lureau74130912019-11-07 23:27:31 +0400111 qobject_unref(e);
Thomas Huth80b8c0b2017-10-06 13:32:13 +0200112 }
113
Igor Mammedov021a0072019-08-30 07:07:23 -0400114 /* make sure that there were hotplugged CPUs */
115 g_assert(hotplugged);
116 qobject_unref(resp);
Thomas Huthe5758de2019-07-22 17:10:55 +0200117 qtest_quit(qts);
Thomas Huth73a7d312017-10-06 15:53:19 +0200118 g_free(args);
119}
120
Marc-André Lureau34e46f62016-07-18 14:56:51 +0400121static void test_data_free(gpointer data)
122{
Thomas Huth152e0392017-10-06 12:13:51 +0200123 PlugTestData *pc = data;
Marc-André Lureau34e46f62016-07-18 14:56:51 +0400124
125 g_free(pc->machine);
Thomas Huth80b8c0b2017-10-06 13:32:13 +0200126 g_free(pc->device_model);
Marc-André Lureau34e46f62016-07-18 14:56:51 +0400127 g_free(pc);
128}
129
Thomas Huth02ef6e82017-03-30 09:50:06 +0200130static void add_pc_test_case(const char *mname)
Andreas Färber7fe55c32015-03-13 17:21:11 +0100131{
Marc-André Lureau34e46f62016-07-18 14:56:51 +0400132 char *path;
Thomas Huth152e0392017-10-06 12:13:51 +0200133 PlugTestData *data;
Andreas Färber7fe55c32015-03-13 17:21:11 +0100134
Thomas Huth02ef6e82017-03-30 09:50:06 +0200135 if (!g_str_has_prefix(mname, "pc-")) {
136 return;
Andreas Färber7fe55c32015-03-13 17:21:11 +0100137 }
Thomas Huth152e0392017-10-06 12:13:51 +0200138 data = g_new(PlugTestData, 1);
Thomas Huth02ef6e82017-03-30 09:50:06 +0200139 data->machine = g_strdup(mname);
140 data->cpu_model = "Haswell"; /* 1.3+ theoretically */
Thomas Huth80b8c0b2017-10-06 13:32:13 +0200141 data->device_model = g_strdup_printf("%s-%s-cpu", data->cpu_model,
142 qtest_get_arch());
Thomas Huth02ef6e82017-03-30 09:50:06 +0200143 data->sockets = 1;
144 data->cores = 3;
145 data->threads = 2;
Igor Mammedovbc1fb852018-09-13 13:06:01 +0200146 data->maxcpus = data->sockets * data->cores * data->threads;
Thomas Huth02ef6e82017-03-30 09:50:06 +0200147 if (g_str_has_suffix(mname, "-1.4") ||
148 (strcmp(mname, "pc-1.3") == 0) ||
149 (strcmp(mname, "pc-1.2") == 0) ||
150 (strcmp(mname, "pc-1.1") == 0) ||
151 (strcmp(mname, "pc-1.0") == 0) ||
152 (strcmp(mname, "pc-0.15") == 0) ||
153 (strcmp(mname, "pc-0.14") == 0) ||
154 (strcmp(mname, "pc-0.13") == 0) ||
Thomas Huthcc425b52018-12-17 17:57:37 +0100155 (strcmp(mname, "pc-0.12") == 0)) {
Thomas Huth80b8c0b2017-10-06 13:32:13 +0200156 path = g_strdup_printf("cpu-plug/%s/init/%ux%ux%u&maxcpus=%u",
Thomas Huth02ef6e82017-03-30 09:50:06 +0200157 mname, data->sockets, data->cores,
158 data->threads, data->maxcpus);
Thomas Huth152e0392017-10-06 12:13:51 +0200159 qtest_add_data_func_full(path, data, test_plug_without_cpu_add,
Thomas Huth02ef6e82017-03-30 09:50:06 +0200160 test_data_free);
161 g_free(path);
162 } else {
Thomas Huth80b8c0b2017-10-06 13:32:13 +0200163 PlugTestData *data2 = g_memdup(data, sizeof(PlugTestData));
164
165 data2->machine = g_strdup(data->machine);
166 data2->device_model = g_strdup(data->device_model);
167
168 path = g_strdup_printf("cpu-plug/%s/cpu-add/%ux%ux%u&maxcpus=%u",
Thomas Huth02ef6e82017-03-30 09:50:06 +0200169 mname, data->sockets, data->cores,
170 data->threads, data->maxcpus);
Thomas Huth152e0392017-10-06 12:13:51 +0200171 qtest_add_data_func_full(path, data, test_plug_with_cpu_add,
Thomas Huth02ef6e82017-03-30 09:50:06 +0200172 test_data_free);
173 g_free(path);
Thomas Huth80b8c0b2017-10-06 13:32:13 +0200174 path = g_strdup_printf("cpu-plug/%s/device-add/%ux%ux%u&maxcpus=%u",
175 mname, data2->sockets, data2->cores,
176 data2->threads, data2->maxcpus);
Igor Mammedov021a0072019-08-30 07:07:23 -0400177 qtest_add_data_func_full(path, data2, test_plug_with_device_add,
Thomas Huth80b8c0b2017-10-06 13:32:13 +0200178 test_data_free);
179 g_free(path);
Thomas Huth02ef6e82017-03-30 09:50:06 +0200180 }
Andreas Färber7fe55c32015-03-13 17:21:11 +0100181}
182
Thomas Huth73a7d312017-10-06 15:53:19 +0200183static void add_pseries_test_case(const char *mname)
184{
185 char *path;
186 PlugTestData *data;
187
188 if (!g_str_has_prefix(mname, "pseries-") ||
189 (g_str_has_prefix(mname, "pseries-2.") && atoi(&mname[10]) < 7)) {
190 return;
191 }
192 data = g_new(PlugTestData, 1);
193 data->machine = g_strdup(mname);
194 data->cpu_model = "power8_v2.0";
195 data->device_model = g_strdup("power8_v2.0-spapr-cpu-core");
196 data->sockets = 2;
197 data->cores = 3;
198 data->threads = 1;
Igor Mammedovbc1fb852018-09-13 13:06:01 +0200199 data->maxcpus = data->sockets * data->cores * data->threads;
Thomas Huth73a7d312017-10-06 15:53:19 +0200200
201 path = g_strdup_printf("cpu-plug/%s/device-add/%ux%ux%u&maxcpus=%u",
202 mname, data->sockets, data->cores,
203 data->threads, data->maxcpus);
Igor Mammedov021a0072019-08-30 07:07:23 -0400204 qtest_add_data_func_full(path, data, test_plug_with_device_add,
Thomas Huth73a7d312017-10-06 15:53:19 +0200205 test_data_free);
206 g_free(path);
207}
208
Thomas Huth7d8b00f2017-10-17 13:39:15 +0200209static void add_s390x_test_case(const char *mname)
210{
211 char *path;
212 PlugTestData *data, *data2;
213
214 if (!g_str_has_prefix(mname, "s390-ccw-virtio-")) {
215 return;
216 }
217
218 data = g_new(PlugTestData, 1);
219 data->machine = g_strdup(mname);
220 data->cpu_model = "qemu";
221 data->device_model = g_strdup("qemu-s390x-cpu");
222 data->sockets = 1;
223 data->cores = 3;
224 data->threads = 1;
Igor Mammedovbc1fb852018-09-13 13:06:01 +0200225 data->maxcpus = data->sockets * data->cores * data->threads;
Thomas Huth7d8b00f2017-10-17 13:39:15 +0200226
227 data2 = g_memdup(data, sizeof(PlugTestData));
228 data2->machine = g_strdup(data->machine);
229 data2->device_model = g_strdup(data->device_model);
230
231 path = g_strdup_printf("cpu-plug/%s/cpu-add/%ux%ux%u&maxcpus=%u",
232 mname, data->sockets, data->cores,
233 data->threads, data->maxcpus);
234 qtest_add_data_func_full(path, data, test_plug_with_cpu_add,
235 test_data_free);
236 g_free(path);
237
238 path = g_strdup_printf("cpu-plug/%s/device-add/%ux%ux%u&maxcpus=%u",
239 mname, data2->sockets, data2->cores,
240 data2->threads, data2->maxcpus);
Igor Mammedov021a0072019-08-30 07:07:23 -0400241 qtest_add_data_func_full(path, data2, test_plug_with_device_add,
Thomas Huth7d8b00f2017-10-17 13:39:15 +0200242 test_data_free);
243 g_free(path);
244}
245
Andreas Färber7fe55c32015-03-13 17:21:11 +0100246int main(int argc, char **argv)
247{
248 const char *arch = qtest_get_arch();
249
250 g_test_init(&argc, &argv, NULL);
251
252 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
Thomas Huth1f4a0d82018-08-16 13:35:55 +0200253 qtest_cb_for_every_machine(add_pc_test_case, g_test_quick());
Thomas Huth73a7d312017-10-06 15:53:19 +0200254 } else if (g_str_equal(arch, "ppc64")) {
Thomas Huth1f4a0d82018-08-16 13:35:55 +0200255 qtest_cb_for_every_machine(add_pseries_test_case, g_test_quick());
Thomas Huth7d8b00f2017-10-17 13:39:15 +0200256 } else if (g_str_equal(arch, "s390x")) {
Thomas Huth1f4a0d82018-08-16 13:35:55 +0200257 qtest_cb_for_every_machine(add_s390x_test_case, g_test_quick());
Andreas Färber7fe55c32015-03-13 17:21:11 +0100258 }
259
260 return g_test_run();
261}