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