qemu-iotests: Add test for bz #1745922
[qemu/ar7.git] / tests / migration-test.c
blob258aa064d48d43234fde892c7a0a7ebf10cf5665
1 /*
2 * QTest testcase for migration
4 * Copyright (c) 2016-2018 Red Hat, Inc. and/or its affiliates
5 * based on the vhost-user-test.c that is:
6 * Copyright (c) 2014 Virtual Open Systems Sarl.
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.
13 #include "qemu/osdep.h"
15 #include "libqtest.h"
16 #include "qapi/qmp/qdict.h"
17 #include "qapi/qmp/qjson.h"
18 #include "qemu/module.h"
19 #include "qemu/option.h"
20 #include "qemu/range.h"
21 #include "qemu/sockets.h"
22 #include "chardev/char.h"
23 #include "qapi/qapi-visit-sockets.h"
24 #include "qapi/qobject-input-visitor.h"
25 #include "qapi/qobject-output-visitor.h"
27 #include "migration/migration-test.h"
29 /* TODO actually test the results and get rid of this */
30 #define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__))
32 unsigned start_address;
33 unsigned end_address;
34 bool got_stop;
35 static bool uffd_feature_thread_id;
37 #if defined(__linux__)
38 #include <sys/syscall.h>
39 #include <sys/vfs.h>
40 #endif
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>
47 static bool ufd_version_check(void)
49 struct uffdio_api api_struct;
50 uint64_t ioctl_mask;
52 int ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
54 if (ufd == -1) {
55 g_test_message("Skipping test: userfaultfd not available");
56 return false;
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;
65 uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
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;
74 return true;
77 #else
78 static bool ufd_version_check(void)
80 g_test_message("Skipping test: Userfault not available (builtdtime)");
81 return false;
84 #endif
86 static const char *tmpfs;
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.
91 #include "tests/migration/i386/a-b-bootblock.h"
92 #include "tests/migration/aarch64/a-b-kernel.h"
93 #include "tests/migration/s390x/a-b-bios.h"
95 static void init_bootfile(const char *bootpath, void *content, size_t len)
97 FILE *bootfile = fopen(bootpath, "wb");
99 g_assert_cmpint(fwrite(content, len, 1, bootfile), ==, 1);
100 fclose(bootfile);
104 * Wait for some output in the serial output file,
105 * we get an 'A' followed by an endless string of 'B's
106 * but on the destination we won't have the A.
108 static void wait_for_serial(const char *side)
110 char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
111 FILE *serialfile = fopen(serialpath, "r");
112 const char *arch = qtest_get_arch();
113 int started = (strcmp(side, "src_serial") == 0 &&
114 strcmp(arch, "ppc64") == 0) ? 0 : 1;
116 g_free(serialpath);
117 do {
118 int readvalue = fgetc(serialfile);
120 if (!started) {
121 /* SLOF prints its banner before starting test,
122 * to ignore it, mark the start of the test with '_',
123 * ignore all characters until this marker
125 switch (readvalue) {
126 case '_':
127 started = 1;
128 break;
129 case EOF:
130 fseek(serialfile, 0, SEEK_SET);
131 usleep(1000);
132 break;
134 continue;
136 switch (readvalue) {
137 case 'A':
138 /* Fine */
139 break;
141 case 'B':
142 /* It's alive! */
143 fclose(serialfile);
144 return;
146 case EOF:
147 started = (strcmp(side, "src_serial") == 0 &&
148 strcmp(arch, "ppc64") == 0) ? 0 : 1;
149 fseek(serialfile, 0, SEEK_SET);
150 usleep(1000);
151 break;
153 default:
154 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
155 g_assert_not_reached();
157 } while (true);
160 static void stop_cb(void *opaque, const char *name, QDict *data)
162 if (!strcmp(name, "STOP")) {
163 got_stop = true;
168 * Events can get in the way of responses we are actually waiting for.
170 GCC_FMT_ATTR(3, 4)
171 static QDict *wait_command_fd(QTestState *who, int fd, const char *command, ...)
173 va_list ap;
175 va_start(ap, command);
176 qtest_qmp_vsend_fds(who, &fd, 1, command, ap);
177 va_end(ap);
179 return qtest_qmp_receive_success(who, stop_cb, NULL);
183 * Events can get in the way of responses we are actually waiting for.
185 GCC_FMT_ATTR(2, 3)
186 static QDict *wait_command(QTestState *who, const char *command, ...)
188 va_list ap;
190 va_start(ap, command);
191 qtest_qmp_vsend(who, command, ap);
192 va_end(ap);
194 return qtest_qmp_receive_success(who, stop_cb, NULL);
198 * Note: caller is responsible to free the returned object via
199 * qobject_unref() after use
201 static QDict *migrate_query(QTestState *who)
203 return wait_command(who, "{ 'execute': 'query-migrate' }");
207 * Note: caller is responsible to free the returned object via
208 * g_free() after use
210 static gchar *migrate_query_status(QTestState *who)
212 QDict *rsp_return = migrate_query(who);
213 gchar *status = g_strdup(qdict_get_str(rsp_return, "status"));
215 g_assert(status);
216 qobject_unref(rsp_return);
218 return status;
222 * It's tricky to use qemu's migration event capability with qtest,
223 * events suddenly appearing confuse the qmp()/hmp() responses.
226 static int64_t read_ram_property_int(QTestState *who, const char *property)
228 QDict *rsp_return, *rsp_ram;
229 int64_t result;
231 rsp_return = migrate_query(who);
232 if (!qdict_haskey(rsp_return, "ram")) {
233 /* Still in setup */
234 result = 0;
235 } else {
236 rsp_ram = qdict_get_qdict(rsp_return, "ram");
237 result = qdict_get_try_int(rsp_ram, property, 0);
239 qobject_unref(rsp_return);
240 return result;
243 static uint64_t get_migration_pass(QTestState *who)
245 return read_ram_property_int(who, "dirty-sync-count");
248 static void read_blocktime(QTestState *who)
250 QDict *rsp_return;
252 rsp_return = migrate_query(who);
253 g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
254 qobject_unref(rsp_return);
257 static void wait_for_migration_status(QTestState *who,
258 const char *goal)
260 while (true) {
261 bool completed;
262 char *status;
264 status = migrate_query_status(who);
265 completed = strcmp(status, goal) == 0;
266 g_assert_cmpstr(status, !=, "failed");
267 g_free(status);
268 if (completed) {
269 return;
271 usleep(1000);
275 static void wait_for_migration_complete(QTestState *who)
277 wait_for_migration_status(who, "completed");
280 static void wait_for_migration_pass(QTestState *who)
282 uint64_t initial_pass = get_migration_pass(who);
283 uint64_t pass;
285 /* Wait for the 1st sync */
286 while (!got_stop && !initial_pass) {
287 usleep(1000);
288 initial_pass = get_migration_pass(who);
291 do {
292 usleep(1000);
293 pass = get_migration_pass(who);
294 } while (pass == initial_pass && !got_stop);
297 static void check_guests_ram(QTestState *who)
299 /* Our ASM test will have been incrementing one byte from each page from
300 * start_address to < end_address in order. This gives us a constraint
301 * that any page's byte should be equal or less than the previous pages
302 * byte (mod 256); and they should all be equal except for one transition
303 * at the point where we meet the incrementer. (We're running this with
304 * the guest stopped).
306 unsigned address;
307 uint8_t first_byte;
308 uint8_t last_byte;
309 bool hit_edge = false;
310 int bad = 0;
312 qtest_memread(who, start_address, &first_byte, 1);
313 last_byte = first_byte;
315 for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
316 address += TEST_MEM_PAGE_SIZE)
318 uint8_t b;
319 qtest_memread(who, address, &b, 1);
320 if (b != last_byte) {
321 if (((b + 1) % 256) == last_byte && !hit_edge) {
322 /* This is OK, the guest stopped at the point of
323 * incrementing the previous page but didn't get
324 * to us yet.
326 hit_edge = true;
327 last_byte = b;
328 } else {
329 bad++;
330 if (bad <= 10) {
331 fprintf(stderr, "Memory content inconsistency at %x"
332 " first_byte = %x last_byte = %x current = %x"
333 " hit_edge = %x\n",
334 address, first_byte, last_byte, b, hit_edge);
339 if (bad >= 10) {
340 fprintf(stderr, "and in another %d pages", bad - 10);
342 g_assert(bad == 0);
345 static void cleanup(const char *filename)
347 char *path = g_strdup_printf("%s/%s", tmpfs, filename);
349 unlink(path);
350 g_free(path);
353 static char *get_shmem_opts(const char *mem_size, const char *shmem_path)
355 return g_strdup_printf("-object memory-backend-file,id=mem0,size=%s"
356 ",mem-path=%s,share=on -numa node,memdev=mem0",
357 mem_size, shmem_path);
360 static char *SocketAddress_to_str(SocketAddress *addr)
362 switch (addr->type) {
363 case SOCKET_ADDRESS_TYPE_INET:
364 return g_strdup_printf("tcp:%s:%s",
365 addr->u.inet.host,
366 addr->u.inet.port);
367 case SOCKET_ADDRESS_TYPE_UNIX:
368 return g_strdup_printf("unix:%s",
369 addr->u.q_unix.path);
370 case SOCKET_ADDRESS_TYPE_FD:
371 return g_strdup_printf("fd:%s", addr->u.fd.str);
372 case SOCKET_ADDRESS_TYPE_VSOCK:
373 return g_strdup_printf("tcp:%s:%s",
374 addr->u.vsock.cid,
375 addr->u.vsock.port);
376 default:
377 return g_strdup("unknown address type");
381 static char *migrate_get_socket_address(QTestState *who, const char *parameter)
383 QDict *rsp;
384 char *result;
385 Error *local_err = NULL;
386 SocketAddressList *addrs;
387 Visitor *iv = NULL;
388 QObject *object;
390 rsp = migrate_query(who);
391 object = qdict_get(rsp, parameter);
393 iv = qobject_input_visitor_new(object);
394 visit_type_SocketAddressList(iv, NULL, &addrs, &local_err);
395 visit_free(iv);
397 /* we are only using a single address */
398 result = SocketAddress_to_str(addrs->value);
400 qapi_free_SocketAddressList(addrs);
401 qobject_unref(rsp);
402 return result;
405 static long long migrate_get_parameter_int(QTestState *who,
406 const char *parameter)
408 QDict *rsp;
409 long long result;
411 rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
412 result = qdict_get_int(rsp, parameter);
413 qobject_unref(rsp);
414 return result;
417 static void migrate_check_parameter_int(QTestState *who, const char *parameter,
418 long long value)
420 long long result;
422 result = migrate_get_parameter_int(who, parameter);
423 g_assert_cmpint(result, ==, value);
426 static void migrate_set_parameter_int(QTestState *who, const char *parameter,
427 long long value)
429 QDict *rsp;
431 rsp = qtest_qmp(who,
432 "{ 'execute': 'migrate-set-parameters',"
433 "'arguments': { %s: %lld } }",
434 parameter, value);
435 g_assert(qdict_haskey(rsp, "return"));
436 qobject_unref(rsp);
437 migrate_check_parameter_int(who, parameter, value);
440 static void migrate_pause(QTestState *who)
442 QDict *rsp;
444 rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
445 qobject_unref(rsp);
448 static void migrate_recover(QTestState *who, const char *uri)
450 QDict *rsp;
452 rsp = wait_command(who,
453 "{ 'execute': 'migrate-recover', "
454 " 'id': 'recover-cmd', "
455 " 'arguments': { 'uri': %s } }",
456 uri);
457 qobject_unref(rsp);
460 static void migrate_set_capability(QTestState *who, const char *capability,
461 bool value)
463 QDict *rsp;
465 rsp = qtest_qmp(who,
466 "{ 'execute': 'migrate-set-capabilities',"
467 "'arguments': { "
468 "'capabilities': [ { "
469 "'capability': %s, 'state': %i } ] } }",
470 capability, value);
471 g_assert(qdict_haskey(rsp, "return"));
472 qobject_unref(rsp);
476 * Send QMP command "migrate".
477 * Arguments are built from @fmt... (formatted like
478 * qobject_from_jsonf_nofail()) with "uri": @uri spliced in.
480 GCC_FMT_ATTR(3, 4)
481 static void migrate(QTestState *who, const char *uri, const char *fmt, ...)
483 va_list ap;
484 QDict *args, *rsp;
486 va_start(ap, fmt);
487 args = qdict_from_vjsonf_nofail(fmt, ap);
488 va_end(ap);
490 g_assert(!qdict_haskey(args, "uri"));
491 qdict_put_str(args, "uri", uri);
493 rsp = qtest_qmp(who, "{ 'execute': 'migrate', 'arguments': %p}", args);
495 g_assert(qdict_haskey(rsp, "return"));
496 qobject_unref(rsp);
499 static void migrate_postcopy_start(QTestState *from, QTestState *to)
501 QDict *rsp;
503 rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
504 qobject_unref(rsp);
506 if (!got_stop) {
507 qtest_qmp_eventwait(from, "STOP");
510 qtest_qmp_eventwait(to, "RESUME");
513 static int test_migrate_start(QTestState **from, QTestState **to,
514 const char *uri, bool hide_stderr,
515 bool use_shmem, const char *opts_src,
516 const char *opts_dst)
518 gchar *cmd_src, *cmd_dst;
519 char *bootpath = NULL;
520 char *extra_opts = NULL;
521 char *shmem_path = NULL;
522 const char *arch = qtest_get_arch();
523 const char *accel = "kvm:tcg";
525 opts_src = opts_src ? opts_src : "";
526 opts_dst = opts_dst ? opts_dst : "";
528 if (use_shmem) {
529 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
530 g_test_skip("/dev/shm is not supported");
531 return -1;
533 shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
536 got_stop = false;
537 bootpath = g_strdup_printf("%s/bootsect", tmpfs);
538 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
539 /* the assembled x86 boot sector should be exactly one sector large */
540 assert(sizeof(x86_bootsect) == 512);
541 init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect));
542 extra_opts = use_shmem ? get_shmem_opts("150M", shmem_path) : NULL;
543 cmd_src = g_strdup_printf("-machine accel=%s -m 150M"
544 " -name source,debug-threads=on"
545 " -serial file:%s/src_serial"
546 " -drive file=%s,format=raw %s %s",
547 accel, tmpfs, bootpath,
548 extra_opts ? extra_opts : "", opts_src);
549 cmd_dst = g_strdup_printf("-machine accel=%s -m 150M"
550 " -name target,debug-threads=on"
551 " -serial file:%s/dest_serial"
552 " -drive file=%s,format=raw"
553 " -incoming %s %s %s",
554 accel, tmpfs, bootpath, uri,
555 extra_opts ? extra_opts : "", opts_dst);
556 start_address = X86_TEST_MEM_START;
557 end_address = X86_TEST_MEM_END;
558 } else if (g_str_equal(arch, "s390x")) {
559 init_bootfile(bootpath, s390x_elf, sizeof(s390x_elf));
560 extra_opts = use_shmem ? get_shmem_opts("128M", shmem_path) : NULL;
561 cmd_src = g_strdup_printf("-machine accel=%s -m 128M"
562 " -name source,debug-threads=on"
563 " -serial file:%s/src_serial -bios %s %s %s",
564 accel, tmpfs, bootpath,
565 extra_opts ? extra_opts : "", opts_src);
566 cmd_dst = g_strdup_printf("-machine accel=%s -m 128M"
567 " -name target,debug-threads=on"
568 " -serial file:%s/dest_serial -bios %s"
569 " -incoming %s %s %s",
570 accel, tmpfs, bootpath, uri,
571 extra_opts ? extra_opts : "", opts_dst);
572 start_address = S390_TEST_MEM_START;
573 end_address = S390_TEST_MEM_END;
574 } else if (strcmp(arch, "ppc64") == 0) {
575 extra_opts = use_shmem ? get_shmem_opts("256M", shmem_path) : NULL;
576 cmd_src = g_strdup_printf("-machine accel=%s -m 256M -nodefaults"
577 " -name source,debug-threads=on"
578 " -serial file:%s/src_serial"
579 " -prom-env 'use-nvramrc?=true' -prom-env "
580 "'nvramrc=hex .\" _\" begin %x %x "
581 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
582 "until' %s %s", accel, tmpfs, end_address,
583 start_address, extra_opts ? extra_opts : "",
584 opts_src);
585 cmd_dst = g_strdup_printf("-machine accel=%s -m 256M"
586 " -name target,debug-threads=on"
587 " -serial file:%s/dest_serial"
588 " -incoming %s %s %s",
589 accel, tmpfs, uri,
590 extra_opts ? extra_opts : "", opts_dst);
592 start_address = PPC_TEST_MEM_START;
593 end_address = PPC_TEST_MEM_END;
594 } else if (strcmp(arch, "aarch64") == 0) {
595 init_bootfile(bootpath, aarch64_kernel, sizeof(aarch64_kernel));
596 extra_opts = use_shmem ? get_shmem_opts("150M", shmem_path) : NULL;
597 cmd_src = g_strdup_printf("-machine virt,accel=%s,gic-version=max "
598 "-name vmsource,debug-threads=on -cpu max "
599 "-m 150M -serial file:%s/src_serial "
600 "-kernel %s %s %s",
601 accel, tmpfs, bootpath,
602 extra_opts ? extra_opts : "", opts_src);
603 cmd_dst = g_strdup_printf("-machine virt,accel=%s,gic-version=max "
604 "-name vmdest,debug-threads=on -cpu max "
605 "-m 150M -serial file:%s/dest_serial "
606 "-kernel %s "
607 "-incoming %s %s %s",
608 accel, tmpfs, bootpath, uri,
609 extra_opts ? extra_opts : "", opts_dst);
611 start_address = ARM_TEST_MEM_START;
612 end_address = ARM_TEST_MEM_END;
614 g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
615 } else {
616 g_assert_not_reached();
619 g_free(bootpath);
620 g_free(extra_opts);
622 if (hide_stderr) {
623 gchar *tmp;
624 tmp = g_strdup_printf("%s 2>/dev/null", cmd_src);
625 g_free(cmd_src);
626 cmd_src = tmp;
628 tmp = g_strdup_printf("%s 2>/dev/null", cmd_dst);
629 g_free(cmd_dst);
630 cmd_dst = tmp;
633 *from = qtest_init(cmd_src);
634 g_free(cmd_src);
636 *to = qtest_init(cmd_dst);
637 g_free(cmd_dst);
640 * Remove shmem file immediately to avoid memory leak in test failed case.
641 * It's valid becase QEMU has already opened this file
643 if (use_shmem) {
644 unlink(shmem_path);
645 g_free(shmem_path);
648 return 0;
651 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
653 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
655 qtest_quit(from);
657 if (test_dest) {
658 qtest_memread(to, start_address, &dest_byte_a, 1);
660 /* Destination still running, wait for a byte to change */
661 do {
662 qtest_memread(to, start_address, &dest_byte_b, 1);
663 usleep(1000 * 10);
664 } while (dest_byte_a == dest_byte_b);
666 qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
668 /* With it stopped, check nothing changes */
669 qtest_memread(to, start_address, &dest_byte_c, 1);
670 usleep(1000 * 200);
671 qtest_memread(to, start_address, &dest_byte_d, 1);
672 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
674 check_guests_ram(to);
677 qtest_quit(to);
679 cleanup("bootsect");
680 cleanup("migsocket");
681 cleanup("src_serial");
682 cleanup("dest_serial");
685 static void deprecated_set_downtime(QTestState *who, const double value)
687 QDict *rsp;
689 rsp = qtest_qmp(who,
690 "{ 'execute': 'migrate_set_downtime',"
691 " 'arguments': { 'value': %f } }", value);
692 g_assert(qdict_haskey(rsp, "return"));
693 qobject_unref(rsp);
694 migrate_check_parameter_int(who, "downtime-limit", value * 1000);
697 static void deprecated_set_speed(QTestState *who, long long value)
699 QDict *rsp;
701 rsp = qtest_qmp(who, "{ 'execute': 'migrate_set_speed',"
702 "'arguments': { 'value': %lld } }", value);
703 g_assert(qdict_haskey(rsp, "return"));
704 qobject_unref(rsp);
705 migrate_check_parameter_int(who, "max-bandwidth", value);
708 static void deprecated_set_cache_size(QTestState *who, long long value)
710 QDict *rsp;
712 rsp = qtest_qmp(who, "{ 'execute': 'migrate-set-cache-size',"
713 "'arguments': { 'value': %lld } }", value);
714 g_assert(qdict_haskey(rsp, "return"));
715 qobject_unref(rsp);
716 migrate_check_parameter_int(who, "xbzrle-cache-size", value);
719 static void test_deprecated(void)
721 QTestState *from;
723 from = qtest_init("-machine none");
725 deprecated_set_downtime(from, 0.12345);
726 deprecated_set_speed(from, 12345);
727 deprecated_set_cache_size(from, 4096);
729 qtest_quit(from);
732 static int migrate_postcopy_prepare(QTestState **from_ptr,
733 QTestState **to_ptr,
734 bool hide_error)
736 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
737 QTestState *from, *to;
739 if (test_migrate_start(&from, &to, uri, hide_error, false, NULL, NULL)) {
740 return -1;
743 migrate_set_capability(from, "postcopy-ram", true);
744 migrate_set_capability(to, "postcopy-ram", true);
745 migrate_set_capability(to, "postcopy-blocktime", true);
747 /* We want to pick a speed slow enough that the test completes
748 * quickly, but that it doesn't complete precopy even on a slow
749 * machine, so also set the downtime.
751 migrate_set_parameter_int(from, "max-bandwidth", 100000000);
752 migrate_set_parameter_int(from, "downtime-limit", 1);
754 /* Wait for the first serial output from the source */
755 wait_for_serial("src_serial");
757 migrate(from, uri, "{}");
758 g_free(uri);
760 wait_for_migration_pass(from);
762 *from_ptr = from;
763 *to_ptr = to;
765 return 0;
768 static void migrate_postcopy_complete(QTestState *from, QTestState *to)
770 wait_for_migration_complete(from);
772 /* Make sure we get at least one "B" on destination */
773 wait_for_serial("dest_serial");
775 if (uffd_feature_thread_id) {
776 read_blocktime(to);
779 test_migrate_end(from, to, true);
782 static void test_postcopy(void)
784 QTestState *from, *to;
786 if (migrate_postcopy_prepare(&from, &to, false)) {
787 return;
789 migrate_postcopy_start(from, to);
790 migrate_postcopy_complete(from, to);
793 static void test_postcopy_recovery(void)
795 QTestState *from, *to;
796 char *uri;
798 if (migrate_postcopy_prepare(&from, &to, true)) {
799 return;
802 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
803 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
805 /* Now we start the postcopy */
806 migrate_postcopy_start(from, to);
809 * Wait until postcopy is really started; we can only run the
810 * migrate-pause command during a postcopy
812 wait_for_migration_status(from, "postcopy-active");
815 * Manually stop the postcopy migration. This emulates a network
816 * failure with the migration socket
818 migrate_pause(from);
821 * Wait for destination side to reach postcopy-paused state. The
822 * migrate-recover command can only succeed if destination machine
823 * is in the paused state
825 wait_for_migration_status(to, "postcopy-paused");
828 * Create a new socket to emulate a new channel that is different
829 * from the broken migration channel; tell the destination to
830 * listen to the new port
832 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
833 migrate_recover(to, uri);
836 * Try to rebuild the migration channel using the resume flag and
837 * the newly created channel
839 wait_for_migration_status(from, "postcopy-paused");
840 migrate(from, uri, "{'resume': true}");
841 g_free(uri);
843 /* Restore the postcopy bandwidth to unlimited */
844 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
846 migrate_postcopy_complete(from, to);
849 static void wait_for_migration_fail(QTestState *from, bool allow_active)
851 QDict *rsp_return;
852 char *status;
853 bool failed;
855 do {
856 status = migrate_query_status(from);
857 g_assert(!strcmp(status, "setup") || !strcmp(status, "failed") ||
858 (allow_active && !strcmp(status, "active")));
859 failed = !strcmp(status, "failed");
860 g_free(status);
861 } while (!failed);
863 /* Is the machine currently running? */
864 rsp_return = wait_command(from, "{ 'execute': 'query-status' }");
865 g_assert(qdict_haskey(rsp_return, "running"));
866 g_assert(qdict_get_bool(rsp_return, "running"));
867 qobject_unref(rsp_return);
870 static void test_baddest(void)
872 QTestState *from, *to;
874 if (test_migrate_start(&from, &to, "tcp:0:0", true, false, NULL, NULL)) {
875 return;
877 migrate(from, "tcp:0:0", "{}");
878 wait_for_migration_fail(from, false);
879 test_migrate_end(from, to, false);
882 static void test_precopy_unix(void)
884 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
885 QTestState *from, *to;
887 if (test_migrate_start(&from, &to, uri, false, false, NULL, NULL)) {
888 return;
891 /* We want to pick a speed slow enough that the test completes
892 * quickly, but that it doesn't complete precopy even on a slow
893 * machine, so also set the downtime.
895 /* 1 ms should make it not converge*/
896 migrate_set_parameter_int(from, "downtime-limit", 1);
897 /* 1GB/s */
898 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
900 /* Wait for the first serial output from the source */
901 wait_for_serial("src_serial");
903 migrate(from, uri, "{}");
905 wait_for_migration_pass(from);
907 /* 300 ms should converge */
908 migrate_set_parameter_int(from, "downtime-limit", 300);
910 if (!got_stop) {
911 qtest_qmp_eventwait(from, "STOP");
914 qtest_qmp_eventwait(to, "RESUME");
916 wait_for_serial("dest_serial");
917 wait_for_migration_complete(from);
919 test_migrate_end(from, to, true);
920 g_free(uri);
923 #if 0
924 /* Currently upset on aarch64 TCG */
925 static void test_ignore_shared(void)
927 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
928 QTestState *from, *to;
930 if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) {
931 return;
934 migrate_set_capability(from, "x-ignore-shared", true);
935 migrate_set_capability(to, "x-ignore-shared", true);
937 /* Wait for the first serial output from the source */
938 wait_for_serial("src_serial");
940 migrate(from, uri, "{}");
942 wait_for_migration_pass(from);
944 if (!got_stop) {
945 qtest_qmp_eventwait(from, "STOP");
948 qtest_qmp_eventwait(to, "RESUME");
950 wait_for_serial("dest_serial");
951 wait_for_migration_complete(from);
953 /* Check whether shared RAM has been really skipped */
954 g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
956 test_migrate_end(from, to, true);
957 g_free(uri);
959 #endif
961 static void test_xbzrle(const char *uri)
963 QTestState *from, *to;
965 if (test_migrate_start(&from, &to, uri, false, false, NULL, NULL)) {
966 return;
970 * We want to pick a speed slow enough that the test completes
971 * quickly, but that it doesn't complete precopy even on a slow
972 * machine, so also set the downtime.
974 /* 1 ms should make it not converge*/
975 migrate_set_parameter_int(from, "downtime-limit", 1);
976 /* 1GB/s */
977 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
979 migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
981 migrate_set_capability(from, "xbzrle", "true");
982 migrate_set_capability(to, "xbzrle", "true");
983 /* Wait for the first serial output from the source */
984 wait_for_serial("src_serial");
986 migrate(from, uri, "{}");
988 wait_for_migration_pass(from);
990 /* 300ms should converge */
991 migrate_set_parameter_int(from, "downtime-limit", 300);
993 if (!got_stop) {
994 qtest_qmp_eventwait(from, "STOP");
996 qtest_qmp_eventwait(to, "RESUME");
998 wait_for_serial("dest_serial");
999 wait_for_migration_complete(from);
1001 test_migrate_end(from, to, true);
1004 static void test_xbzrle_unix(void)
1006 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1008 test_xbzrle(uri);
1009 g_free(uri);
1012 static void test_precopy_tcp(void)
1014 char *uri;
1015 QTestState *from, *to;
1017 if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", false, false,
1018 NULL, NULL)) {
1019 return;
1023 * We want to pick a speed slow enough that the test completes
1024 * quickly, but that it doesn't complete precopy even on a slow
1025 * machine, so also set the downtime.
1027 /* 1 ms should make it not converge*/
1028 migrate_set_parameter_int(from, "downtime-limit", 1);
1029 /* 1GB/s */
1030 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
1032 /* Wait for the first serial output from the source */
1033 wait_for_serial("src_serial");
1035 uri = migrate_get_socket_address(to, "socket-address");
1037 migrate(from, uri, "{}");
1039 wait_for_migration_pass(from);
1041 /* 300ms should converge */
1042 migrate_set_parameter_int(from, "downtime-limit", 300);
1044 if (!got_stop) {
1045 qtest_qmp_eventwait(from, "STOP");
1047 qtest_qmp_eventwait(to, "RESUME");
1049 wait_for_serial("dest_serial");
1050 wait_for_migration_complete(from);
1052 test_migrate_end(from, to, true);
1053 g_free(uri);
1056 static void test_migrate_fd_proto(void)
1058 QTestState *from, *to;
1059 int ret;
1060 int pair[2];
1061 QDict *rsp;
1062 const char *error_desc;
1064 if (test_migrate_start(&from, &to, "defer", false, false, NULL, NULL)) {
1065 return;
1069 * We want to pick a speed slow enough that the test completes
1070 * quickly, but that it doesn't complete precopy even on a slow
1071 * machine, so also set the downtime.
1073 /* 1 ms should make it not converge */
1074 migrate_set_parameter_int(from, "downtime-limit", 1);
1075 /* 1GB/s */
1076 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
1078 /* Wait for the first serial output from the source */
1079 wait_for_serial("src_serial");
1081 /* Create two connected sockets for migration */
1082 ret = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
1083 g_assert_cmpint(ret, ==, 0);
1085 /* Send the 1st socket to the target */
1086 rsp = wait_command_fd(to, pair[0],
1087 "{ 'execute': 'getfd',"
1088 " 'arguments': { 'fdname': 'fd-mig' }}");
1089 qobject_unref(rsp);
1090 close(pair[0]);
1092 /* Start incoming migration from the 1st socket */
1093 rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1094 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1095 qobject_unref(rsp);
1097 /* Send the 2nd socket to the target */
1098 rsp = wait_command_fd(from, pair[1],
1099 "{ 'execute': 'getfd',"
1100 " 'arguments': { 'fdname': 'fd-mig' }}");
1101 qobject_unref(rsp);
1102 close(pair[1]);
1104 /* Start migration to the 2nd socket*/
1105 migrate(from, "fd:fd-mig", "{}");
1107 wait_for_migration_pass(from);
1109 /* 300ms should converge */
1110 migrate_set_parameter_int(from, "downtime-limit", 300);
1112 if (!got_stop) {
1113 qtest_qmp_eventwait(from, "STOP");
1115 qtest_qmp_eventwait(to, "RESUME");
1117 /* Test closing fds */
1118 /* We assume, that QEMU removes named fd from its list,
1119 * so this should fail */
1120 rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
1121 " 'arguments': { 'fdname': 'fd-mig' }}");
1122 g_assert_true(qdict_haskey(rsp, "error"));
1123 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1124 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1125 qobject_unref(rsp);
1127 rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
1128 " 'arguments': { 'fdname': 'fd-mig' }}");
1129 g_assert_true(qdict_haskey(rsp, "error"));
1130 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1131 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1132 qobject_unref(rsp);
1134 /* Complete migration */
1135 wait_for_serial("dest_serial");
1136 wait_for_migration_complete(from);
1137 test_migrate_end(from, to, true);
1140 static void do_test_validate_uuid(const char *uuid_arg_src,
1141 const char *uuid_arg_dst,
1142 bool should_fail, bool hide_stderr)
1144 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1145 QTestState *from, *to;
1147 if (test_migrate_start(&from, &to, uri, hide_stderr, false,
1148 uuid_arg_src, uuid_arg_dst)) {
1149 return;
1153 * UUID validation is at the begin of migration. So, the main process of
1154 * migration is not interesting for us here. Thus, set huge downtime for
1155 * very fast migration.
1157 migrate_set_parameter_int(from, "downtime-limit", 1000000);
1158 migrate_set_capability(from, "validate-uuid", true);
1160 /* Wait for the first serial output from the source */
1161 wait_for_serial("src_serial");
1163 migrate(from, uri, "{}");
1165 if (should_fail) {
1166 qtest_set_expected_status(to, 1);
1167 wait_for_migration_fail(from, true);
1168 } else {
1169 wait_for_migration_complete(from);
1172 test_migrate_end(from, to, false);
1173 g_free(uri);
1176 static void test_validate_uuid(void)
1178 do_test_validate_uuid("-uuid 11111111-1111-1111-1111-111111111111",
1179 "-uuid 11111111-1111-1111-1111-111111111111",
1180 false, false);
1183 static void test_validate_uuid_error(void)
1185 do_test_validate_uuid("-uuid 11111111-1111-1111-1111-111111111111",
1186 "-uuid 22222222-2222-2222-2222-222222222222",
1187 true, true);
1190 static void test_validate_uuid_src_not_set(void)
1192 do_test_validate_uuid(NULL, "-uuid 11111111-1111-1111-1111-111111111111",
1193 false, true);
1196 static void test_validate_uuid_dst_not_set(void)
1198 do_test_validate_uuid("-uuid 11111111-1111-1111-1111-111111111111", NULL,
1199 false, true);
1202 int main(int argc, char **argv)
1204 char template[] = "/tmp/migration-test-XXXXXX";
1205 int ret;
1207 g_test_init(&argc, &argv, NULL);
1209 if (!ufd_version_check()) {
1210 return g_test_run();
1214 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
1215 * is touchy due to race conditions on dirty bits (especially on PPC for
1216 * some reason)
1218 if (g_str_equal(qtest_get_arch(), "ppc64") &&
1219 access("/sys/module/kvm_hv", F_OK)) {
1220 g_test_message("Skipping test: kvm_hv not available");
1221 return g_test_run();
1225 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
1226 * there until the problems are resolved
1228 if (g_str_equal(qtest_get_arch(), "s390x")) {
1229 #if defined(HOST_S390X)
1230 if (access("/dev/kvm", R_OK | W_OK)) {
1231 g_test_message("Skipping test: kvm not available");
1232 return g_test_run();
1234 #else
1235 g_test_message("Skipping test: Need s390x host to work properly");
1236 return g_test_run();
1237 #endif
1240 tmpfs = mkdtemp(template);
1241 if (!tmpfs) {
1242 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno));
1244 g_assert(tmpfs);
1246 module_call_init(MODULE_INIT_QOM);
1248 qtest_add_func("/migration/postcopy/unix", test_postcopy);
1249 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery);
1250 qtest_add_func("/migration/deprecated", test_deprecated);
1251 qtest_add_func("/migration/bad_dest", test_baddest);
1252 qtest_add_func("/migration/precopy/unix", test_precopy_unix);
1253 qtest_add_func("/migration/precopy/tcp", test_precopy_tcp);
1254 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
1255 qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix);
1256 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto);
1257 qtest_add_func("/migration/validate_uuid", test_validate_uuid);
1258 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error);
1259 qtest_add_func("/migration/validate_uuid_src_not_set",
1260 test_validate_uuid_src_not_set);
1261 qtest_add_func("/migration/validate_uuid_dst_not_set",
1262 test_validate_uuid_dst_not_set);
1264 ret = g_test_run();
1266 g_assert_cmpint(ret, ==, 0);
1268 ret = rmdir(tmpfs);
1269 if (ret != 0) {
1270 g_test_message("unable to rmdir: path (%s): %s",
1271 tmpfs, strerror(errno));
1274 return ret;