]> git.proxmox.com Git - mirror_qemu.git/blame - tests/migration-test.c
migration: Add capabilities validation
[mirror_qemu.git] / tests / migration-test.c
CommitLineData
ea0c6d62 1/*
2656bfd9 2 * QTest testcase for migration
ea0c6d62 3 *
17ca7746 4 * Copyright (c) 2016-2018 Red Hat, Inc. and/or its affiliates
ea0c6d62
DDAG
5 * based on the vhost-user-test.c that is:
6 * Copyright (c) 2014 Virtual Open Systems Sarl.
7 *
8 * This work is licensed under the terms of the GNU GPL, version 2 or later.
9 * See the COPYING file in the top-level directory.
10 *
11 */
12
13#include "qemu/osdep.h"
ea0c6d62
DDAG
14
15#include "libqtest.h"
452fcdbc 16#include "qapi/qmp/qdict.h"
b5bbd3f3 17#include "qapi/qmp/qjson.h"
ea0c6d62
DDAG
18#include "qemu/option.h"
19#include "qemu/range.h"
a9c94277 20#include "qemu/sockets.h"
8228e353 21#include "chardev/char.h"
ea0c6d62
DDAG
22#include "sysemu/sysemu.h"
23
e51e711b
WH
24#include "migration/migration-test.h"
25
055a1efc
MA
26/* TODO actually test the results and get rid of this */
27#define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__))
28
e51e711b
WH
29unsigned start_address;
30unsigned end_address;
ea0c6d62 31bool got_stop;
346f3dab 32static bool uffd_feature_thread_id;
ea0c6d62
DDAG
33
34#if defined(__linux__)
ea0c6d62
DDAG
35#include <sys/syscall.h>
36#include <sys/vfs.h>
37#endif
38
39#if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
40#include <sys/eventfd.h>
41#include <sys/ioctl.h>
42#include <linux/userfaultfd.h>
43
44static bool ufd_version_check(void)
45{
46 struct uffdio_api api_struct;
47 uint64_t ioctl_mask;
48
e1ae9fb6 49 int ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
ea0c6d62
DDAG
50
51 if (ufd == -1) {
52 g_test_message("Skipping test: userfaultfd not available");
53 return false;
54 }
55
56 api_struct.api = UFFD_API;
57 api_struct.features = 0;
58 if (ioctl(ufd, UFFDIO_API, &api_struct)) {
59 g_test_message("Skipping test: UFFDIO_API failed");
60 return false;
61 }
346f3dab 62 uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
ea0c6d62
DDAG
63
64 ioctl_mask = (__u64)1 << _UFFDIO_REGISTER |
65 (__u64)1 << _UFFDIO_UNREGISTER;
66 if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
67 g_test_message("Skipping test: Missing userfault feature");
68 return false;
69 }
70
71 return true;
72}
73
74#else
75static bool ufd_version_check(void)
76{
77 g_test_message("Skipping test: Userfault not available (builtdtime)");
78 return false;
79}
80
81#endif
82
83static const char *tmpfs;
84
e51e711b
WH
85/* The boot file modifies memory area in [start_address, end_address)
86 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
ea0c6d62 87 */
d54927ef 88#include "tests/migration/i386/a-b-bootblock.h"
c02b3781 89#include "tests/migration/aarch64/a-b-kernel.h"
ea0c6d62 90
c02b3781 91static void init_bootfile(const char *bootpath, void *content)
aaf89c8a 92{
93 FILE *bootfile = fopen(bootpath, "wb");
94
c02b3781 95 g_assert_cmpint(fwrite(content, 512, 1, bootfile), ==, 1);
aaf89c8a 96 fclose(bootfile);
97}
98
5571dc82
TH
99#include "tests/migration/s390x/a-b-bios.h"
100
101static void init_bootfile_s390x(const char *bootpath)
102{
103 FILE *bootfile = fopen(bootpath, "wb");
104 size_t len = sizeof(s390x_elf);
105
106 g_assert_cmpint(fwrite(s390x_elf, len, 1, bootfile), ==, 1);
107 fclose(bootfile);
108}
109
ea0c6d62
DDAG
110/*
111 * Wait for some output in the serial output file,
112 * we get an 'A' followed by an endless string of 'B's
113 * but on the destination we won't have the A.
114 */
115static void wait_for_serial(const char *side)
116{
117 char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
118 FILE *serialfile = fopen(serialpath, "r");
aaf89c8a 119 const char *arch = qtest_get_arch();
120 int started = (strcmp(side, "src_serial") == 0 &&
121 strcmp(arch, "ppc64") == 0) ? 0 : 1;
ea0c6d62 122
e2dd21e5 123 g_free(serialpath);
ea0c6d62
DDAG
124 do {
125 int readvalue = fgetc(serialfile);
126
aaf89c8a 127 if (!started) {
128 /* SLOF prints its banner before starting test,
129 * to ignore it, mark the start of the test with '_',
130 * ignore all characters until this marker
131 */
132 switch (readvalue) {
133 case '_':
134 started = 1;
135 break;
136 case EOF:
137 fseek(serialfile, 0, SEEK_SET);
138 usleep(1000);
139 break;
140 }
141 continue;
142 }
ea0c6d62
DDAG
143 switch (readvalue) {
144 case 'A':
145 /* Fine */
146 break;
147
148 case 'B':
149 /* It's alive! */
150 fclose(serialfile);
ea0c6d62
DDAG
151 return;
152
153 case EOF:
aaf89c8a 154 started = (strcmp(side, "src_serial") == 0 &&
155 strcmp(arch, "ppc64") == 0) ? 0 : 1;
ea0c6d62
DDAG
156 fseek(serialfile, 0, SEEK_SET);
157 usleep(1000);
158 break;
159
160 default:
161 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
162 g_assert_not_reached();
163 }
164 } while (true);
165}
166
3cd46d42
MA
167static void stop_cb(void *opaque, const char *name, QDict *data)
168{
169 if (!strcmp(name, "STOP")) {
170 got_stop = true;
171 }
172}
173
ea0c6d62
DDAG
174/*
175 * Events can get in the way of responses we are actually waiting for.
176 */
b7281c69 177GCC_FMT_ATTR(2, 3)
4399596b 178static QDict *wait_command(QTestState *who, const char *command, ...)
ea0c6d62 179{
4399596b
MA
180 va_list ap;
181
182 va_start(ap, command);
183 qtest_qmp_vsend(who, command, ap);
184 va_end(ap);
185
3cd46d42 186 return qtest_qmp_receive_success(who, stop_cb, NULL);
ea0c6d62
DDAG
187}
188
2f7074c6
PX
189/*
190 * Note: caller is responsible to free the returned object via
191 * qobject_unref() after use
192 */
193static QDict *migrate_query(QTestState *who)
194{
e1454165 195 return wait_command(who, "{ 'execute': 'query-migrate' }");
2f7074c6
PX
196}
197
198/*
199 * Note: caller is responsible to free the returned object via
200 * g_free() after use
201 */
202static gchar *migrate_query_status(QTestState *who)
203{
204 QDict *rsp_return = migrate_query(who);
205 gchar *status = g_strdup(qdict_get_str(rsp_return, "status"));
206
207 g_assert(status);
208 qobject_unref(rsp_return);
209
210 return status;
211}
ea0c6d62
DDAG
212
213/*
214 * It's tricky to use qemu's migration event capability with qtest,
215 * events suddenly appearing confuse the qmp()/hmp() responses.
ea0c6d62
DDAG
216 */
217
660a9b68 218static int64_t read_ram_property_int(QTestState *who, const char *property)
ea0c6d62 219{
2f7074c6 220 QDict *rsp_return, *rsp_ram;
660a9b68 221 int64_t result;
ea0c6d62 222
2f7074c6 223 rsp_return = migrate_query(who);
ea0c6d62
DDAG
224 if (!qdict_haskey(rsp_return, "ram")) {
225 /* Still in setup */
226 result = 0;
227 } else {
228 rsp_ram = qdict_get_qdict(rsp_return, "ram");
660a9b68 229 result = qdict_get_try_int(rsp_ram, property, 0);
ea0c6d62 230 }
2f7074c6 231 qobject_unref(rsp_return);
ea0c6d62
DDAG
232 return result;
233}
234
660a9b68
YK
235static uint64_t get_migration_pass(QTestState *who)
236{
237 return read_ram_property_int(who, "dirty-sync-count");
238}
239
346f3dab
AP
240static void read_blocktime(QTestState *who)
241{
2f7074c6 242 QDict *rsp_return;
346f3dab 243
2f7074c6 244 rsp_return = migrate_query(who);
346f3dab 245 g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
2f7074c6 246 qobject_unref(rsp_return);
346f3dab
AP
247}
248
2f6d3138
PX
249static void wait_for_migration_status(QTestState *who,
250 const char *goal)
ea0c6d62 251{
6a7724e9 252 while (true) {
6a7724e9 253 bool completed;
2f7074c6 254 char *status;
ea0c6d62 255
2f7074c6 256 status = migrate_query_status(who);
2f6d3138 257 completed = strcmp(status, goal) == 0;
ea0c6d62 258 g_assert_cmpstr(status, !=, "failed");
2f7074c6 259 g_free(status);
6a7724e9
JQ
260 if (completed) {
261 return;
262 }
263 usleep(1000);
264 }
ea0c6d62
DDAG
265}
266
2f6d3138
PX
267static void wait_for_migration_complete(QTestState *who)
268{
269 wait_for_migration_status(who, "completed");
270}
271
863e27a8 272static void wait_for_migration_pass(QTestState *who)
ea0c6d62 273{
863e27a8 274 uint64_t initial_pass = get_migration_pass(who);
ea0c6d62
DDAG
275 uint64_t pass;
276
277 /* Wait for the 1st sync */
6a7724e9
JQ
278 while (!got_stop && !initial_pass) {
279 usleep(1000);
863e27a8 280 initial_pass = get_migration_pass(who);
6a7724e9 281 }
ea0c6d62
DDAG
282
283 do {
6a7724e9 284 usleep(1000);
863e27a8 285 pass = get_migration_pass(who);
ea0c6d62
DDAG
286 } while (pass == initial_pass && !got_stop);
287}
288
7195a871 289static void check_guests_ram(QTestState *who)
ea0c6d62
DDAG
290{
291 /* Our ASM test will have been incrementing one byte from each page from
e51e711b
WH
292 * start_address to < end_address in order. This gives us a constraint
293 * that any page's byte should be equal or less than the previous pages
294 * byte (mod 256); and they should all be equal except for one transition
295 * at the point where we meet the incrementer. (We're running this with
296 * the guest stopped).
ea0c6d62
DDAG
297 */
298 unsigned address;
299 uint8_t first_byte;
300 uint8_t last_byte;
301 bool hit_edge = false;
302 bool bad = false;
303
7195a871 304 qtest_memread(who, start_address, &first_byte, 1);
ea0c6d62
DDAG
305 last_byte = first_byte;
306
e51e711b
WH
307 for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
308 address += TEST_MEM_PAGE_SIZE)
ea0c6d62
DDAG
309 {
310 uint8_t b;
7195a871 311 qtest_memread(who, address, &b, 1);
ea0c6d62
DDAG
312 if (b != last_byte) {
313 if (((b + 1) % 256) == last_byte && !hit_edge) {
314 /* This is OK, the guest stopped at the point of
315 * incrementing the previous page but didn't get
316 * to us yet.
317 */
318 hit_edge = true;
829db8b4 319 last_byte = b;
ea0c6d62
DDAG
320 } else {
321 fprintf(stderr, "Memory content inconsistency at %x"
322 " first_byte = %x last_byte = %x current = %x"
323 " hit_edge = %x\n",
324 address, first_byte, last_byte, b, hit_edge);
325 bad = true;
326 }
327 }
ea0c6d62
DDAG
328 }
329 g_assert_false(bad);
330}
331
332static void cleanup(const char *filename)
333{
334 char *path = g_strdup_printf("%s/%s", tmpfs, filename);
335
336 unlink(path);
e2dd21e5 337 g_free(path);
ea0c6d62
DDAG
338}
339
660a9b68
YK
340static char *get_shmem_opts(const char *mem_size, const char *shmem_path)
341{
342 return g_strdup_printf("-object memory-backend-file,id=mem0,size=%s"
343 ",mem-path=%s,share=on -numa node,memdev=mem0",
344 mem_size, shmem_path);
345}
346
56b4a42a 347static void migrate_check_parameter(QTestState *who, const char *parameter,
c44a56d8 348 long long value)
56b4a42a 349{
e1454165 350 QDict *rsp_return;
56b4a42a 351
e1454165
MA
352 rsp_return = wait_command(who,
353 "{ 'execute': 'query-migrate-parameters' }");
c44a56d8 354 g_assert_cmpint(qdict_get_int(rsp_return, parameter), ==, value);
e1454165 355 qobject_unref(rsp_return);
56b4a42a
JQ
356}
357
1f90d797 358static void migrate_set_parameter(QTestState *who, const char *parameter,
c44a56d8 359 long long value)
d62fbe60
JQ
360{
361 QDict *rsp;
d62fbe60 362
c44a56d8
MA
363 rsp = qtest_qmp(who,
364 "{ 'execute': 'migrate-set-parameters',"
365 "'arguments': { %s: %lld } }",
366 parameter, value);
d62fbe60 367 g_assert(qdict_haskey(rsp, "return"));
cb3e7f08 368 qobject_unref(rsp);
1f90d797 369 migrate_check_parameter(who, parameter, value);
d62fbe60
JQ
370}
371
d5f49640
PX
372static void migrate_pause(QTestState *who)
373{
374 QDict *rsp;
375
376 rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
d5f49640
PX
377 qobject_unref(rsp);
378}
379
380static void migrate_recover(QTestState *who, const char *uri)
381{
382 QDict *rsp;
d5f49640 383
b7281c69
MA
384 rsp = wait_command(who,
385 "{ 'execute': 'migrate-recover', "
386 " 'id': 'recover-cmd', "
387 " 'arguments': { 'uri': %s } }",
388 uri);
d5f49640
PX
389 qobject_unref(rsp);
390}
391
d62fbe60 392static void migrate_set_capability(QTestState *who, const char *capability,
c44a56d8 393 bool value)
d62fbe60
JQ
394{
395 QDict *rsp;
c44a56d8
MA
396
397 rsp = qtest_qmp(who,
398 "{ 'execute': 'migrate-set-capabilities',"
399 "'arguments': { "
400 "'capabilities': [ { "
401 "'capability': %s, 'state': %i } ] } }",
402 capability, value);
d62fbe60 403 g_assert(qdict_haskey(rsp, "return"));
cb3e7f08 404 qobject_unref(rsp);
d62fbe60
JQ
405}
406
b5bbd3f3
MA
407/*
408 * Send QMP command "migrate".
409 * Arguments are built from @fmt... (formatted like
410 * qobject_from_jsonf_nofail()) with "uri": @uri spliced in.
411 */
412GCC_FMT_ATTR(3, 4)
413static void migrate(QTestState *who, const char *uri, const char *fmt, ...)
d62fbe60 414{
b5bbd3f3
MA
415 va_list ap;
416 QDict *args, *rsp;
d62fbe60 417
b5bbd3f3
MA
418 va_start(ap, fmt);
419 args = qdict_from_vjsonf_nofail(fmt, ap);
420 va_end(ap);
421
422 g_assert(!qdict_haskey(args, "uri"));
423 qdict_put_str(args, "uri", uri);
424
425 rsp = qmp("{ 'execute': 'migrate', 'arguments': %p}", args);
d62fbe60 426 g_assert(qdict_haskey(rsp, "return"));
cb3e7f08 427 qobject_unref(rsp);
d62fbe60
JQ
428}
429
d131662a 430static void migrate_postcopy_start(QTestState *from, QTestState *to)
eb665d7d
JQ
431{
432 QDict *rsp;
433
d131662a 434 rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
cb3e7f08 435 qobject_unref(rsp);
d131662a
PX
436
437 if (!got_stop) {
438 qtest_qmp_eventwait(from, "STOP");
439 }
440
441 qtest_qmp_eventwait(to, "RESUME");
eb665d7d
JQ
442}
443
5fd4a9c9 444static int test_migrate_start(QTestState **from, QTestState **to,
660a9b68
YK
445 const char *uri, bool hide_stderr,
446 bool use_shmem)
ea0c6d62 447{
d62fbe60 448 gchar *cmd_src, *cmd_dst;
660a9b68
YK
449 char *bootpath = NULL;
450 char *extra_opts = NULL;
451 char *shmem_path = NULL;
aaf89c8a 452 const char *arch = qtest_get_arch();
63b2d935 453 const char *accel = "kvm:tcg";
ea0c6d62 454
660a9b68
YK
455 if (use_shmem) {
456 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
457 g_test_skip("/dev/shm is not supported");
458 return -1;
459 }
460 shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
461 }
ea0c6d62 462
660a9b68
YK
463 got_stop = false;
464 bootpath = g_strdup_printf("%s/bootsect", tmpfs);
aaf89c8a 465 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
c02b3781 466 init_bootfile(bootpath, x86_bootsect);
660a9b68 467 extra_opts = use_shmem ? get_shmem_opts("150M", shmem_path) : NULL;
63b2d935 468 cmd_src = g_strdup_printf("-machine accel=%s -m 150M"
31a6bb74 469 " -name source,debug-threads=on"
aaf89c8a 470 " -serial file:%s/src_serial"
660a9b68
YK
471 " -drive file=%s,format=raw %s",
472 accel, tmpfs, bootpath,
473 extra_opts ? extra_opts : "");
63b2d935 474 cmd_dst = g_strdup_printf("-machine accel=%s -m 150M"
31a6bb74 475 " -name target,debug-threads=on"
aaf89c8a 476 " -serial file:%s/dest_serial"
477 " -drive file=%s,format=raw"
660a9b68
YK
478 " -incoming %s %s",
479 accel, tmpfs, bootpath, uri,
480 extra_opts ? extra_opts : "");
e51e711b
WH
481 start_address = X86_TEST_MEM_START;
482 end_address = X86_TEST_MEM_END;
5571dc82
TH
483 } else if (g_str_equal(arch, "s390x")) {
484 init_bootfile_s390x(bootpath);
660a9b68 485 extra_opts = use_shmem ? get_shmem_opts("128M", shmem_path) : NULL;
5571dc82
TH
486 cmd_src = g_strdup_printf("-machine accel=%s -m 128M"
487 " -name source,debug-threads=on"
660a9b68
YK
488 " -serial file:%s/src_serial -bios %s %s",
489 accel, tmpfs, bootpath,
490 extra_opts ? extra_opts : "");
5571dc82
TH
491 cmd_dst = g_strdup_printf("-machine accel=%s -m 128M"
492 " -name target,debug-threads=on"
493 " -serial file:%s/dest_serial -bios %s"
660a9b68
YK
494 " -incoming %s %s",
495 accel, tmpfs, bootpath, uri,
496 extra_opts ? extra_opts : "");
5571dc82
TH
497 start_address = S390_TEST_MEM_START;
498 end_address = S390_TEST_MEM_END;
aaf89c8a 499 } else if (strcmp(arch, "ppc64") == 0) {
660a9b68 500 extra_opts = use_shmem ? get_shmem_opts("256M", shmem_path) : NULL;
fc71e3e5 501 cmd_src = g_strdup_printf("-machine accel=%s -m 256M -nodefaults"
31a6bb74 502 " -name source,debug-threads=on"
aaf89c8a 503 " -serial file:%s/src_serial"
fc71e3e5
TH
504 " -prom-env 'use-nvramrc?=true' -prom-env "
505 "'nvramrc=hex .\" _\" begin %x %x "
cdf33815 506 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
660a9b68
YK
507 "until' %s", accel, tmpfs, end_address,
508 start_address, extra_opts ? extra_opts : "");
171da9d5 509 cmd_dst = g_strdup_printf("-machine accel=%s -m 256M"
31a6bb74 510 " -name target,debug-threads=on"
aaf89c8a 511 " -serial file:%s/dest_serial"
660a9b68
YK
512 " -incoming %s %s",
513 accel, tmpfs, uri,
514 extra_opts ? extra_opts : "");
e51e711b
WH
515
516 start_address = PPC_TEST_MEM_START;
517 end_address = PPC_TEST_MEM_END;
c02b3781
WH
518 } else if (strcmp(arch, "aarch64") == 0) {
519 init_bootfile(bootpath, aarch64_kernel);
660a9b68 520 extra_opts = use_shmem ? get_shmem_opts("150M", shmem_path) : NULL;
c02b3781
WH
521 cmd_src = g_strdup_printf("-machine virt,accel=%s,gic-version=max "
522 "-name vmsource,debug-threads=on -cpu max "
523 "-m 150M -serial file:%s/src_serial "
660a9b68
YK
524 "-kernel %s %s",
525 accel, tmpfs, bootpath,
526 extra_opts ? extra_opts : "");
c02b3781
WH
527 cmd_dst = g_strdup_printf("-machine virt,accel=%s,gic-version=max "
528 "-name vmdest,debug-threads=on -cpu max "
529 "-m 150M -serial file:%s/dest_serial "
530 "-kernel %s "
660a9b68
YK
531 "-incoming %s %s",
532 accel, tmpfs, bootpath, uri,
533 extra_opts ? extra_opts : "");
c02b3781
WH
534
535 start_address = ARM_TEST_MEM_START;
536 end_address = ARM_TEST_MEM_END;
537
538 g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
aaf89c8a 539 } else {
540 g_assert_not_reached();
541 }
542
e2dd21e5 543 g_free(bootpath);
660a9b68 544 g_free(extra_opts);
e2dd21e5 545
f96d6651
DDAG
546 if (hide_stderr) {
547 gchar *tmp;
548 tmp = g_strdup_printf("%s 2>/dev/null", cmd_src);
549 g_free(cmd_src);
550 cmd_src = tmp;
551
552 tmp = g_strdup_printf("%s 2>/dev/null", cmd_dst);
553 g_free(cmd_dst);
554 cmd_dst = tmp;
555 }
556
7195a871 557 *from = qtest_start(cmd_src);
aaf89c8a 558 g_free(cmd_src);
559
7195a871 560 *to = qtest_init(cmd_dst);
aaf89c8a 561 g_free(cmd_dst);
660a9b68
YK
562
563 /*
564 * Remove shmem file immediately to avoid memory leak in test failed case.
565 * It's valid becase QEMU has already opened this file
566 */
567 if (use_shmem) {
568 unlink(shmem_path);
569 g_free(shmem_path);
570 }
571
5fd4a9c9 572 return 0;
7195a871
JQ
573}
574
2c9bb297 575static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
7195a871
JQ
576{
577 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
578
579 qtest_quit(from);
580
2c9bb297
DDAG
581 if (test_dest) {
582 qtest_memread(to, start_address, &dest_byte_a, 1);
7195a871 583
2c9bb297
DDAG
584 /* Destination still running, wait for a byte to change */
585 do {
586 qtest_memread(to, start_address, &dest_byte_b, 1);
587 usleep(1000 * 10);
588 } while (dest_byte_a == dest_byte_b);
589
590 qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
7195a871 591
2c9bb297
DDAG
592 /* With it stopped, check nothing changes */
593 qtest_memread(to, start_address, &dest_byte_c, 1);
594 usleep(1000 * 200);
595 qtest_memread(to, start_address, &dest_byte_d, 1);
596 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
7195a871 597
2c9bb297
DDAG
598 check_guests_ram(to);
599 }
7195a871
JQ
600
601 qtest_quit(to);
602
603 cleanup("bootsect");
604 cleanup("migsocket");
605 cleanup("src_serial");
606 cleanup("dest_serial");
607}
608
4c27486d
JQ
609static void deprecated_set_downtime(QTestState *who, const double value)
610{
611 QDict *rsp;
4c27486d 612
015715f5
MA
613 rsp = qtest_qmp(who,
614 "{ 'execute': 'migrate_set_downtime',"
615 " 'arguments': { 'value': %f } }", value);
4c27486d 616 g_assert(qdict_haskey(rsp, "return"));
cb3e7f08 617 qobject_unref(rsp);
c44a56d8 618 migrate_check_parameter(who, "downtime-limit", value * 1000);
4c27486d
JQ
619}
620
c44a56d8 621static void deprecated_set_speed(QTestState *who, long long value)
4c27486d
JQ
622{
623 QDict *rsp;
4c27486d 624
c44a56d8
MA
625 rsp = qtest_qmp(who, "{ 'execute': 'migrate_set_speed',"
626 "'arguments': { 'value': %lld } }", value);
4c27486d 627 g_assert(qdict_haskey(rsp, "return"));
cb3e7f08 628 qobject_unref(rsp);
4c27486d
JQ
629 migrate_check_parameter(who, "max-bandwidth", value);
630}
631
632static void test_deprecated(void)
633{
634 QTestState *from;
635
c02b3781 636 from = qtest_start("-machine none");
4c27486d
JQ
637
638 deprecated_set_downtime(from, 0.12345);
c44a56d8 639 deprecated_set_speed(from, 12345);
4c27486d
JQ
640
641 qtest_quit(from);
642}
643
d131662a 644static int migrate_postcopy_prepare(QTestState **from_ptr,
3e81f73c
PX
645 QTestState **to_ptr,
646 bool hide_error)
7195a871
JQ
647{
648 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
863e27a8 649 QTestState *from, *to;
7195a871 650
660a9b68 651 if (test_migrate_start(&from, &to, uri, hide_error, false)) {
d131662a 652 return -1;
5fd4a9c9 653 }
ea0c6d62 654
c44a56d8
MA
655 migrate_set_capability(from, "postcopy-ram", true);
656 migrate_set_capability(to, "postcopy-ram", true);
657 migrate_set_capability(to, "postcopy-blocktime", true);
ea0c6d62
DDAG
658
659 /* We want to pick a speed slow enough that the test completes
660 * quickly, but that it doesn't complete precopy even on a slow
661 * machine, so also set the downtime.
662 */
c44a56d8
MA
663 migrate_set_parameter(from, "max-bandwidth", 100000000);
664 migrate_set_parameter(from, "downtime-limit", 1);
ea0c6d62
DDAG
665
666 /* Wait for the first serial output from the source */
667 wait_for_serial("src_serial");
668
b5bbd3f3 669 migrate(from, uri, "{}");
d131662a 670 g_free(uri);
ea0c6d62 671
863e27a8 672 wait_for_migration_pass(from);
ea0c6d62 673
d131662a
PX
674 *from_ptr = from;
675 *to_ptr = to;
ea0c6d62 676
d131662a
PX
677 return 0;
678}
ea0c6d62 679
d131662a
PX
680static void migrate_postcopy_complete(QTestState *from, QTestState *to)
681{
682 wait_for_migration_complete(from);
ea0c6d62 683
d131662a 684 /* Make sure we get at least one "B" on destination */
ea0c6d62 685 wait_for_serial("dest_serial");
ea0c6d62 686
346f3dab
AP
687 if (uffd_feature_thread_id) {
688 read_blocktime(to);
689 }
ea0c6d62 690
2c9bb297
DDAG
691 test_migrate_end(from, to, true);
692}
693
d131662a
PX
694static void test_postcopy(void)
695{
696 QTestState *from, *to;
697
3e81f73c 698 if (migrate_postcopy_prepare(&from, &to, false)) {
d131662a
PX
699 return;
700 }
701 migrate_postcopy_start(from, to);
702 migrate_postcopy_complete(from, to);
703}
704
d5f49640
PX
705static void test_postcopy_recovery(void)
706{
707 QTestState *from, *to;
708 char *uri;
709
3e81f73c 710 if (migrate_postcopy_prepare(&from, &to, true)) {
d5f49640
PX
711 return;
712 }
713
714 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
c44a56d8 715 migrate_set_parameter(from, "max-postcopy-bandwidth", 4096);
d5f49640
PX
716
717 /* Now we start the postcopy */
718 migrate_postcopy_start(from, to);
719
720 /*
721 * Wait until postcopy is really started; we can only run the
722 * migrate-pause command during a postcopy
723 */
724 wait_for_migration_status(from, "postcopy-active");
725
726 /*
727 * Manually stop the postcopy migration. This emulates a network
728 * failure with the migration socket
729 */
730 migrate_pause(from);
731
732 /*
733 * Wait for destination side to reach postcopy-paused state. The
734 * migrate-recover command can only succeed if destination machine
735 * is in the paused state
736 */
737 wait_for_migration_status(to, "postcopy-paused");
738
739 /*
740 * Create a new socket to emulate a new channel that is different
741 * from the broken migration channel; tell the destination to
742 * listen to the new port
743 */
744 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
745 migrate_recover(to, uri);
746
747 /*
748 * Try to rebuild the migration channel using the resume flag and
749 * the newly created channel
750 */
751 wait_for_migration_status(from, "postcopy-paused");
b5bbd3f3 752 migrate(from, uri, "{'resume': true}");
d5f49640
PX
753 g_free(uri);
754
755 /* Restore the postcopy bandwidth to unlimited */
c44a56d8 756 migrate_set_parameter(from, "max-postcopy-bandwidth", 0);
d5f49640
PX
757
758 migrate_postcopy_complete(from, to);
759}
760
2c9bb297
DDAG
761static void test_baddest(void)
762{
763 QTestState *from, *to;
e1454165 764 QDict *rsp_return;
2f7074c6 765 char *status;
2c9bb297
DDAG
766 bool failed;
767
660a9b68 768 if (test_migrate_start(&from, &to, "tcp:0:0", true, false)) {
5fd4a9c9
DDAG
769 return;
770 }
b5bbd3f3 771 migrate(from, "tcp:0:0", "{}");
2c9bb297 772 do {
2f7074c6 773 status = migrate_query_status(from);
2c9bb297
DDAG
774 g_assert(!strcmp(status, "setup") || !(strcmp(status, "failed")));
775 failed = !strcmp(status, "failed");
2f7074c6 776 g_free(status);
2c9bb297
DDAG
777 } while (!failed);
778
779 /* Is the machine currently running? */
e1454165 780 rsp_return = wait_command(from, "{ 'execute': 'query-status' }");
2c9bb297
DDAG
781 g_assert(qdict_haskey(rsp_return, "running"));
782 g_assert(qdict_get_bool(rsp_return, "running"));
e1454165 783 qobject_unref(rsp_return);
2c9bb297
DDAG
784
785 test_migrate_end(from, to, false);
ea0c6d62
DDAG
786}
787
2884100c
JQ
788static void test_precopy_unix(void)
789{
790 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
791 QTestState *from, *to;
792
660a9b68 793 if (test_migrate_start(&from, &to, uri, false, false)) {
5fd4a9c9
DDAG
794 return;
795 }
2884100c
JQ
796
797 /* We want to pick a speed slow enough that the test completes
798 * quickly, but that it doesn't complete precopy even on a slow
799 * machine, so also set the downtime.
800 */
801 /* 1 ms should make it not converge*/
c44a56d8 802 migrate_set_parameter(from, "downtime-limit", 1);
2884100c 803 /* 1GB/s */
c44a56d8 804 migrate_set_parameter(from, "max-bandwidth", 1000000000);
2884100c
JQ
805
806 /* Wait for the first serial output from the source */
807 wait_for_serial("src_serial");
808
b5bbd3f3 809 migrate(from, uri, "{}");
2884100c
JQ
810
811 wait_for_migration_pass(from);
812
813 /* 300 ms should converge */
c44a56d8 814 migrate_set_parameter(from, "downtime-limit", 300);
2884100c
JQ
815
816 if (!got_stop) {
817 qtest_qmp_eventwait(from, "STOP");
818 }
819
820 qtest_qmp_eventwait(to, "RESUME");
821
822 wait_for_serial("dest_serial");
823 wait_for_migration_complete(from);
824
825 test_migrate_end(from, to, true);
826 g_free(uri);
827}
828
660a9b68
YK
829#if 0
830/* Currently upset on aarch64 TCG */
831static void test_ignore_shared(void)
832{
833 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
834 QTestState *from, *to;
835
836 if (test_migrate_start(&from, &to, uri, false, true)) {
837 return;
838 }
839
840 migrate_set_capability(from, "x-ignore-shared", true);
841 migrate_set_capability(to, "x-ignore-shared", true);
842
843 /* Wait for the first serial output from the source */
844 wait_for_serial("src_serial");
845
846 migrate(from, uri, "{}");
847
848 wait_for_migration_pass(from);
849
850 if (!got_stop) {
851 qtest_qmp_eventwait(from, "STOP");
852 }
853
854 qtest_qmp_eventwait(to, "RESUME");
855
856 wait_for_serial("dest_serial");
857 wait_for_migration_complete(from);
858
859 /* Check whether shared RAM has been really skipped */
860 g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
861
862 test_migrate_end(from, to, true);
863 g_free(uri);
864}
865#endif
866
ea0c6d62
DDAG
867int main(int argc, char **argv)
868{
2656bfd9 869 char template[] = "/tmp/migration-test-XXXXXX";
ea0c6d62
DDAG
870 int ret;
871
872 g_test_init(&argc, &argv, NULL);
873
874 if (!ufd_version_check()) {
4848cb3d 875 return g_test_run();
ea0c6d62
DDAG
876 }
877
880b169a
TH
878 /*
879 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
880 * is touchy due to race conditions on dirty bits (especially on PPC for
881 * some reason)
882 */
883 if (g_str_equal(qtest_get_arch(), "ppc64") &&
884 access("/sys/module/kvm_hv", F_OK)) {
885 g_test_message("Skipping test: kvm_hv not available");
4848cb3d 886 return g_test_run();
880b169a
TH
887 }
888
d254b392
TH
889 /*
890 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
891 * there until the problems are resolved
892 */
893 if (g_str_equal(qtest_get_arch(), "s390x")) {
894#if defined(HOST_S390X)
895 if (access("/dev/kvm", R_OK | W_OK)) {
896 g_test_message("Skipping test: kvm not available");
4848cb3d 897 return g_test_run();
d254b392
TH
898 }
899#else
900 g_test_message("Skipping test: Need s390x host to work properly");
4848cb3d 901 return g_test_run();
d254b392
TH
902#endif
903 }
904
ea0c6d62
DDAG
905 tmpfs = mkdtemp(template);
906 if (!tmpfs) {
907 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno));
908 }
909 g_assert(tmpfs);
910
911 module_call_init(MODULE_INIT_QOM);
912
2884100c 913 qtest_add_func("/migration/postcopy/unix", test_postcopy);
d5f49640 914 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery);
4c27486d 915 qtest_add_func("/migration/deprecated", test_deprecated);
2c9bb297 916 qtest_add_func("/migration/bad_dest", test_baddest);
2884100c 917 qtest_add_func("/migration/precopy/unix", test_precopy_unix);
660a9b68 918 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
ea0c6d62
DDAG
919
920 ret = g_test_run();
921
922 g_assert_cmpint(ret, ==, 0);
923
924 ret = rmdir(tmpfs);
925 if (ret != 0) {
926 g_test_message("unable to rmdir: path (%s): %s\n",
927 tmpfs, strerror(errno));
928 }
929
930 return ret;
931}