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"
16 #include "qapi/qmp/qdict.h"
17 #include "qapi/qmp/qjson.h"
18 #include "qemu/option.h"
19 #include "qemu/range.h"
20 #include "qemu/sockets.h"
21 #include "chardev/char.h"
22 #include "sysemu/sysemu.h"
24 #include "migration/migration-test.h"
26 /* TODO actually test the results and get rid of this */
27 #define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__))
29 unsigned start_address;
32 static bool uffd_feature_thread_id;
34 #if defined(__linux__)
35 #include <sys/syscall.h>
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>
44 static bool ufd_version_check(void)
46 struct uffdio_api api_struct;
49 int ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
52 g_test_message("Skipping test: userfaultfd not available");
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");
62 uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
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");
75 static bool ufd_version_check(void)
77 g_test_message("Skipping test: Userfault not available (builtdtime)");
83 static const char *tmpfs;
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.
88 #include "tests/migration/i386/a-b-bootblock.h"
89 #include "tests/migration/aarch64/a-b-kernel.h"
91 static void init_bootfile(const char *bootpath, void *content)
93 FILE *bootfile = fopen(bootpath, "wb");
95 g_assert_cmpint(fwrite(content, 512, 1, bootfile), ==, 1);
99 #include "tests/migration/s390x/a-b-bios.h"
101 static void init_bootfile_s390x(const char *bootpath)
103 FILE *bootfile = fopen(bootpath, "wb");
104 size_t len = sizeof(s390x_elf);
106 g_assert_cmpint(fwrite(s390x_elf, len, 1, bootfile), ==, 1);
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.
115 static void wait_for_serial(const char *side)
117 char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
118 FILE *serialfile = fopen(serialpath, "r");
119 const char *arch = qtest_get_arch();
120 int started = (strcmp(side, "src_serial") == 0 &&
121 strcmp(arch, "ppc64") == 0) ? 0 : 1;
125 int readvalue = fgetc(serialfile);
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
137 fseek(serialfile, 0, SEEK_SET);
154 started = (strcmp(side, "src_serial") == 0 &&
155 strcmp(arch, "ppc64") == 0) ? 0 : 1;
156 fseek(serialfile, 0, SEEK_SET);
161 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
162 g_assert_not_reached();
167 static void stop_cb(void *opaque, const char *name, QDict *data)
169 if (!strcmp(name, "STOP")) {
175 * Events can get in the way of responses we are actually waiting for.
178 static QDict *wait_command(QTestState *who, const char *command, ...)
182 va_start(ap, command);
183 qtest_qmp_vsend(who, command, ap);
186 return qtest_qmp_receive_success(who, stop_cb, NULL);
190 * Note: caller is responsible to free the returned object via
191 * qobject_unref() after use
193 static QDict *migrate_query(QTestState *who)
195 return wait_command(who, "{ 'execute': 'query-migrate' }");
199 * Note: caller is responsible to free the returned object via
202 static gchar *migrate_query_status(QTestState *who)
204 QDict *rsp_return = migrate_query(who);
205 gchar *status = g_strdup(qdict_get_str(rsp_return, "status"));
208 qobject_unref(rsp_return);
214 * It's tricky to use qemu's migration event capability with qtest,
215 * events suddenly appearing confuse the qmp()/hmp() responses.
218 static int64_t read_ram_property_int(QTestState *who, const char *property)
220 QDict *rsp_return, *rsp_ram;
223 rsp_return = migrate_query(who);
224 if (!qdict_haskey(rsp_return, "ram")) {
228 rsp_ram = qdict_get_qdict(rsp_return, "ram");
229 result = qdict_get_try_int(rsp_ram, property, 0);
231 qobject_unref(rsp_return);
235 static uint64_t get_migration_pass(QTestState *who)
237 return read_ram_property_int(who, "dirty-sync-count");
240 static void read_blocktime(QTestState *who)
244 rsp_return = migrate_query(who);
245 g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
246 qobject_unref(rsp_return);
249 static void wait_for_migration_status(QTestState *who,
256 status = migrate_query_status(who);
257 completed = strcmp(status, goal) == 0;
258 g_assert_cmpstr(status, !=, "failed");
267 static void wait_for_migration_complete(QTestState *who)
269 wait_for_migration_status(who, "completed");
272 static void wait_for_migration_pass(QTestState *who)
274 uint64_t initial_pass = get_migration_pass(who);
277 /* Wait for the 1st sync */
278 while (!got_stop && !initial_pass) {
280 initial_pass = get_migration_pass(who);
285 pass = get_migration_pass(who);
286 } while (pass == initial_pass && !got_stop);
289 static void check_guests_ram(QTestState *who)
291 /* Our ASM test will have been incrementing one byte from each page from
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).
301 bool hit_edge = false;
304 qtest_memread(who, start_address, &first_byte, 1);
305 last_byte = first_byte;
307 for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
308 address += TEST_MEM_PAGE_SIZE)
311 qtest_memread(who, address, &b, 1);
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
321 fprintf(stderr, "Memory content inconsistency at %x"
322 " first_byte = %x last_byte = %x current = %x"
324 address, first_byte, last_byte, b, hit_edge);
332 static void cleanup(const char *filename)
334 char *path = g_strdup_printf("%s/%s", tmpfs, filename);
340 static char *get_shmem_opts(const char *mem_size, const char *shmem_path)
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);
347 static void migrate_check_parameter(QTestState *who, const char *parameter,
352 rsp_return = wait_command(who,
353 "{ 'execute': 'query-migrate-parameters' }");
354 g_assert_cmpint(qdict_get_int(rsp_return, parameter), ==, value);
355 qobject_unref(rsp_return);
358 static void migrate_set_parameter(QTestState *who, const char *parameter,
364 "{ 'execute': 'migrate-set-parameters',"
365 "'arguments': { %s: %lld } }",
367 g_assert(qdict_haskey(rsp, "return"));
369 migrate_check_parameter(who, parameter, value);
372 static void migrate_pause(QTestState *who)
376 rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
380 static void migrate_recover(QTestState *who, const char *uri)
384 rsp = wait_command(who,
385 "{ 'execute': 'migrate-recover', "
386 " 'id': 'recover-cmd', "
387 " 'arguments': { 'uri': %s } }",
392 static void migrate_set_capability(QTestState *who, const char *capability,
398 "{ 'execute': 'migrate-set-capabilities',"
400 "'capabilities': [ { "
401 "'capability': %s, 'state': %i } ] } }",
403 g_assert(qdict_haskey(rsp, "return"));
408 * Send QMP command "migrate".
409 * Arguments are built from @fmt... (formatted like
410 * qobject_from_jsonf_nofail()) with "uri": @uri spliced in.
413 static void migrate(QTestState *who, const char *uri, const char *fmt, ...)
419 args = qdict_from_vjsonf_nofail(fmt, ap);
422 g_assert(!qdict_haskey(args, "uri"));
423 qdict_put_str(args, "uri", uri);
425 rsp = qmp("{ 'execute': 'migrate', 'arguments': %p}", args);
426 g_assert(qdict_haskey(rsp, "return"));
430 static void migrate_postcopy_start(QTestState *from, QTestState *to)
434 rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
438 qtest_qmp_eventwait(from, "STOP");
441 qtest_qmp_eventwait(to, "RESUME");
444 static int test_migrate_start(QTestState **from, QTestState **to,
445 const char *uri, bool hide_stderr,
448 gchar *cmd_src, *cmd_dst;
449 char *bootpath = NULL;
450 char *extra_opts = NULL;
451 char *shmem_path = NULL;
452 const char *arch = qtest_get_arch();
453 const char *accel = "kvm:tcg";
456 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
457 g_test_skip("/dev/shm is not supported");
460 shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
464 bootpath = g_strdup_printf("%s/bootsect", tmpfs);
465 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
466 init_bootfile(bootpath, x86_bootsect);
467 extra_opts = use_shmem ? get_shmem_opts("150M", shmem_path) : NULL;
468 cmd_src = g_strdup_printf("-machine accel=%s -m 150M"
469 " -name source,debug-threads=on"
470 " -serial file:%s/src_serial"
471 " -drive file=%s,format=raw %s",
472 accel, tmpfs, bootpath,
473 extra_opts ? extra_opts : "");
474 cmd_dst = g_strdup_printf("-machine accel=%s -m 150M"
475 " -name target,debug-threads=on"
476 " -serial file:%s/dest_serial"
477 " -drive file=%s,format=raw"
479 accel, tmpfs, bootpath, uri,
480 extra_opts ? extra_opts : "");
481 start_address = X86_TEST_MEM_START;
482 end_address = X86_TEST_MEM_END;
483 } else if (g_str_equal(arch, "s390x")) {
484 init_bootfile_s390x(bootpath);
485 extra_opts = use_shmem ? get_shmem_opts("128M", shmem_path) : NULL;
486 cmd_src = g_strdup_printf("-machine accel=%s -m 128M"
487 " -name source,debug-threads=on"
488 " -serial file:%s/src_serial -bios %s %s",
489 accel, tmpfs, bootpath,
490 extra_opts ? extra_opts : "");
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"
495 accel, tmpfs, bootpath, uri,
496 extra_opts ? extra_opts : "");
497 start_address = S390_TEST_MEM_START;
498 end_address = S390_TEST_MEM_END;
499 } else if (strcmp(arch, "ppc64") == 0) {
500 extra_opts = use_shmem ? get_shmem_opts("256M", shmem_path) : NULL;
501 cmd_src = g_strdup_printf("-machine accel=%s -m 256M -nodefaults"
502 " -name source,debug-threads=on"
503 " -serial file:%s/src_serial"
504 " -prom-env 'use-nvramrc?=true' -prom-env "
505 "'nvramrc=hex .\" _\" begin %x %x "
506 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
507 "until' %s", accel, tmpfs, end_address,
508 start_address, extra_opts ? extra_opts : "");
509 cmd_dst = g_strdup_printf("-machine accel=%s -m 256M"
510 " -name target,debug-threads=on"
511 " -serial file:%s/dest_serial"
514 extra_opts ? extra_opts : "");
516 start_address = PPC_TEST_MEM_START;
517 end_address = PPC_TEST_MEM_END;
518 } else if (strcmp(arch, "aarch64") == 0) {
519 init_bootfile(bootpath, aarch64_kernel);
520 extra_opts = use_shmem ? get_shmem_opts("150M", shmem_path) : NULL;
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 "
525 accel, tmpfs, bootpath,
526 extra_opts ? extra_opts : "");
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 "
532 accel, tmpfs, bootpath, uri,
533 extra_opts ? extra_opts : "");
535 start_address = ARM_TEST_MEM_START;
536 end_address = ARM_TEST_MEM_END;
538 g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
540 g_assert_not_reached();
548 tmp = g_strdup_printf("%s 2>/dev/null", cmd_src);
552 tmp = g_strdup_printf("%s 2>/dev/null", cmd_dst);
557 *from = qtest_start(cmd_src);
560 *to = qtest_init(cmd_dst);
564 * Remove shmem file immediately to avoid memory leak in test failed case.
565 * It's valid becase QEMU has already opened this file
575 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
577 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
582 qtest_memread(to, start_address, &dest_byte_a, 1);
584 /* Destination still running, wait for a byte to change */
586 qtest_memread(to, start_address, &dest_byte_b, 1);
588 } while (dest_byte_a == dest_byte_b);
590 qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
592 /* With it stopped, check nothing changes */
593 qtest_memread(to, start_address, &dest_byte_c, 1);
595 qtest_memread(to, start_address, &dest_byte_d, 1);
596 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
598 check_guests_ram(to);
604 cleanup("migsocket");
605 cleanup("src_serial");
606 cleanup("dest_serial");
609 static void deprecated_set_downtime(QTestState *who, const double value)
614 "{ 'execute': 'migrate_set_downtime',"
615 " 'arguments': { 'value': %f } }", value);
616 g_assert(qdict_haskey(rsp, "return"));
618 migrate_check_parameter(who, "downtime-limit", value * 1000);
621 static void deprecated_set_speed(QTestState *who, long long value)
625 rsp = qtest_qmp(who, "{ 'execute': 'migrate_set_speed',"
626 "'arguments': { 'value': %lld } }", value);
627 g_assert(qdict_haskey(rsp, "return"));
629 migrate_check_parameter(who, "max-bandwidth", value);
632 static void test_deprecated(void)
636 from = qtest_start("-machine none");
638 deprecated_set_downtime(from, 0.12345);
639 deprecated_set_speed(from, 12345);
644 static int migrate_postcopy_prepare(QTestState **from_ptr,
648 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
649 QTestState *from, *to;
651 if (test_migrate_start(&from, &to, uri, hide_error, false)) {
655 migrate_set_capability(from, "postcopy-ram", true);
656 migrate_set_capability(to, "postcopy-ram", true);
657 migrate_set_capability(to, "postcopy-blocktime", true);
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.
663 migrate_set_parameter(from, "max-bandwidth", 100000000);
664 migrate_set_parameter(from, "downtime-limit", 1);
666 /* Wait for the first serial output from the source */
667 wait_for_serial("src_serial");
669 migrate(from, uri, "{}");
672 wait_for_migration_pass(from);
680 static void migrate_postcopy_complete(QTestState *from, QTestState *to)
682 wait_for_migration_complete(from);
684 /* Make sure we get at least one "B" on destination */
685 wait_for_serial("dest_serial");
687 if (uffd_feature_thread_id) {
691 test_migrate_end(from, to, true);
694 static void test_postcopy(void)
696 QTestState *from, *to;
698 if (migrate_postcopy_prepare(&from, &to, false)) {
701 migrate_postcopy_start(from, to);
702 migrate_postcopy_complete(from, to);
705 static void test_postcopy_recovery(void)
707 QTestState *from, *to;
710 if (migrate_postcopy_prepare(&from, &to, true)) {
714 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
715 migrate_set_parameter(from, "max-postcopy-bandwidth", 4096);
717 /* Now we start the postcopy */
718 migrate_postcopy_start(from, to);
721 * Wait until postcopy is really started; we can only run the
722 * migrate-pause command during a postcopy
724 wait_for_migration_status(from, "postcopy-active");
727 * Manually stop the postcopy migration. This emulates a network
728 * failure with the migration socket
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
737 wait_for_migration_status(to, "postcopy-paused");
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
744 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
745 migrate_recover(to, uri);
748 * Try to rebuild the migration channel using the resume flag and
749 * the newly created channel
751 wait_for_migration_status(from, "postcopy-paused");
752 migrate(from, uri, "{'resume': true}");
755 /* Restore the postcopy bandwidth to unlimited */
756 migrate_set_parameter(from, "max-postcopy-bandwidth", 0);
758 migrate_postcopy_complete(from, to);
761 static void test_baddest(void)
763 QTestState *from, *to;
768 if (test_migrate_start(&from, &to, "tcp:0:0", true, false)) {
771 migrate(from, "tcp:0:0", "{}");
773 status = migrate_query_status(from);
774 g_assert(!strcmp(status, "setup") || !(strcmp(status, "failed")));
775 failed = !strcmp(status, "failed");
779 /* Is the machine currently running? */
780 rsp_return = wait_command(from, "{ 'execute': 'query-status' }");
781 g_assert(qdict_haskey(rsp_return, "running"));
782 g_assert(qdict_get_bool(rsp_return, "running"));
783 qobject_unref(rsp_return);
785 test_migrate_end(from, to, false);
788 static void test_precopy_unix(void)
790 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
791 QTestState *from, *to;
793 if (test_migrate_start(&from, &to, uri, false, false)) {
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.
801 /* 1 ms should make it not converge*/
802 migrate_set_parameter(from, "downtime-limit", 1);
804 migrate_set_parameter(from, "max-bandwidth", 1000000000);
806 /* Wait for the first serial output from the source */
807 wait_for_serial("src_serial");
809 migrate(from, uri, "{}");
811 wait_for_migration_pass(from);
813 /* 300 ms should converge */
814 migrate_set_parameter(from, "downtime-limit", 300);
817 qtest_qmp_eventwait(from, "STOP");
820 qtest_qmp_eventwait(to, "RESUME");
822 wait_for_serial("dest_serial");
823 wait_for_migration_complete(from);
825 test_migrate_end(from, to, true);
830 /* Currently upset on aarch64 TCG */
831 static void test_ignore_shared(void)
833 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
834 QTestState *from, *to;
836 if (test_migrate_start(&from, &to, uri, false, true)) {
840 migrate_set_capability(from, "x-ignore-shared", true);
841 migrate_set_capability(to, "x-ignore-shared", true);
843 /* Wait for the first serial output from the source */
844 wait_for_serial("src_serial");
846 migrate(from, uri, "{}");
848 wait_for_migration_pass(from);
851 qtest_qmp_eventwait(from, "STOP");
854 qtest_qmp_eventwait(to, "RESUME");
856 wait_for_serial("dest_serial");
857 wait_for_migration_complete(from);
859 /* Check whether shared RAM has been really skipped */
860 g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
862 test_migrate_end(from, to, true);
867 int main(int argc, char **argv)
869 char template[] = "/tmp/migration-test-XXXXXX";
872 g_test_init(&argc, &argv, NULL);
874 if (!ufd_version_check()) {
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
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");
890 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
891 * there until the problems are resolved
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");
900 g_test_message("Skipping test: Need s390x host to work properly");
905 tmpfs = mkdtemp(template);
907 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno));
911 module_call_init(MODULE_INIT_QOM);
913 qtest_add_func("/migration/postcopy/unix", test_postcopy);
914 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery);
915 qtest_add_func("/migration/deprecated", test_deprecated);
916 qtest_add_func("/migration/bad_dest", test_baddest);
917 qtest_add_func("/migration/precopy/unix", test_precopy_unix);
918 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
922 g_assert_cmpint(ret, ==, 0);
926 g_test_message("unable to rmdir: path (%s): %s\n",
927 tmpfs, strerror(errno));