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/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;
35 static bool uffd_feature_thread_id;
37 #if defined(__linux__)
38 #include <sys/syscall.h>
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;
52 int ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
55 g_test_message("Skipping test: userfaultfd not available");
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");
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");
78 static bool ufd_version_check(void)
80 g_test_message("Skipping test: Userfault not available (builtdtime)");
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);
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;
118 int readvalue = fgetc(serialfile);
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
130 fseek(serialfile, 0, SEEK_SET);
147 started = (strcmp(side, "src_serial") == 0 &&
148 strcmp(arch, "ppc64") == 0) ? 0 : 1;
149 fseek(serialfile, 0, SEEK_SET);
154 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
155 g_assert_not_reached();
160 static void stop_cb(void *opaque, const char *name, QDict *data)
162 if (!strcmp(name, "STOP")) {
168 * Events can get in the way of responses we are actually waiting for.
171 static QDict *wait_command_fd(QTestState *who, int fd, const char *command, ...)
175 va_start(ap, command);
176 qtest_qmp_vsend_fds(who, &fd, 1, command, 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.
186 static QDict *wait_command(QTestState *who, const char *command, ...)
190 va_start(ap, command);
191 qtest_qmp_vsend(who, command, 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
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"));
216 qobject_unref(rsp_return);
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;
231 rsp_return = migrate_query(who);
232 if (!qdict_haskey(rsp_return, "ram")) {
236 rsp_ram = qdict_get_qdict(rsp_return, "ram");
237 result = qdict_get_try_int(rsp_ram, property, 0);
239 qobject_unref(rsp_return);
243 static int64_t read_migrate_property_int(QTestState *who, const char *property)
248 rsp_return = migrate_query(who);
249 result = qdict_get_try_int(rsp_return, property, 0);
250 qobject_unref(rsp_return);
254 static uint64_t get_migration_pass(QTestState *who)
256 return read_ram_property_int(who, "dirty-sync-count");
259 static void read_blocktime(QTestState *who)
263 rsp_return = migrate_query(who);
264 g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
265 qobject_unref(rsp_return);
268 static bool check_migration_status(QTestState *who, const char *goal,
269 const char **ungoals)
272 char *current_status;
275 current_status = migrate_query_status(who);
276 ready = strcmp(current_status, goal) == 0;
278 g_assert_cmpstr(current_status, !=, "failed");
280 * If looking for a state other than completed,
281 * completion of migration would cause the test to
284 if (strcmp(goal, "completed") != 0) {
285 g_assert_cmpstr(current_status, !=, "completed");
288 for (ungoal = ungoals; *ungoal; ungoal++) {
289 g_assert_cmpstr(current_status, !=, *ungoal);
292 g_free(current_status);
296 static void wait_for_migration_status(QTestState *who,
298 const char **ungoals)
300 while (!check_migration_status(who, goal, ungoals)) {
305 static void wait_for_migration_complete(QTestState *who)
307 wait_for_migration_status(who, "completed", NULL);
310 static void wait_for_migration_pass(QTestState *who)
312 uint64_t initial_pass = get_migration_pass(who);
315 /* Wait for the 1st sync */
316 while (!got_stop && !initial_pass) {
318 initial_pass = get_migration_pass(who);
323 pass = get_migration_pass(who);
324 } while (pass == initial_pass && !got_stop);
327 static void check_guests_ram(QTestState *who)
329 /* Our ASM test will have been incrementing one byte from each page from
330 * start_address to < end_address in order. This gives us a constraint
331 * that any page's byte should be equal or less than the previous pages
332 * byte (mod 256); and they should all be equal except for one transition
333 * at the point where we meet the incrementer. (We're running this with
334 * the guest stopped).
339 bool hit_edge = false;
342 qtest_memread(who, start_address, &first_byte, 1);
343 last_byte = first_byte;
345 for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
346 address += TEST_MEM_PAGE_SIZE)
349 qtest_memread(who, address, &b, 1);
350 if (b != last_byte) {
351 if (((b + 1) % 256) == last_byte && !hit_edge) {
352 /* This is OK, the guest stopped at the point of
353 * incrementing the previous page but didn't get
361 fprintf(stderr, "Memory content inconsistency at %x"
362 " first_byte = %x last_byte = %x current = %x"
364 address, first_byte, last_byte, b, hit_edge);
370 fprintf(stderr, "and in another %d pages", bad - 10);
375 static void cleanup(const char *filename)
377 char *path = g_strdup_printf("%s/%s", tmpfs, filename);
383 static char *get_shmem_opts(const char *mem_size, const char *shmem_path)
385 return g_strdup_printf("-object memory-backend-file,id=mem0,size=%s"
386 ",mem-path=%s,share=on -numa node,memdev=mem0",
387 mem_size, shmem_path);
390 static char *SocketAddress_to_str(SocketAddress *addr)
392 switch (addr->type) {
393 case SOCKET_ADDRESS_TYPE_INET:
394 return g_strdup_printf("tcp:%s:%s",
397 case SOCKET_ADDRESS_TYPE_UNIX:
398 return g_strdup_printf("unix:%s",
399 addr->u.q_unix.path);
400 case SOCKET_ADDRESS_TYPE_FD:
401 return g_strdup_printf("fd:%s", addr->u.fd.str);
402 case SOCKET_ADDRESS_TYPE_VSOCK:
403 return g_strdup_printf("tcp:%s:%s",
407 return g_strdup("unknown address type");
411 static char *migrate_get_socket_address(QTestState *who, const char *parameter)
415 Error *local_err = NULL;
416 SocketAddressList *addrs;
420 rsp = migrate_query(who);
421 object = qdict_get(rsp, parameter);
423 iv = qobject_input_visitor_new(object);
424 visit_type_SocketAddressList(iv, NULL, &addrs, &local_err);
427 /* we are only using a single address */
428 result = SocketAddress_to_str(addrs->value);
430 qapi_free_SocketAddressList(addrs);
435 static long long migrate_get_parameter_int(QTestState *who,
436 const char *parameter)
441 rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
442 result = qdict_get_int(rsp, parameter);
447 static void migrate_check_parameter_int(QTestState *who, const char *parameter,
452 result = migrate_get_parameter_int(who, parameter);
453 g_assert_cmpint(result, ==, value);
456 static void migrate_set_parameter_int(QTestState *who, const char *parameter,
462 "{ 'execute': 'migrate-set-parameters',"
463 "'arguments': { %s: %lld } }",
465 g_assert(qdict_haskey(rsp, "return"));
467 migrate_check_parameter_int(who, parameter, value);
470 static void migrate_pause(QTestState *who)
474 rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
478 static void migrate_continue(QTestState *who, const char *state)
482 rsp = wait_command(who,
483 "{ 'execute': 'migrate-continue',"
484 " 'arguments': { 'state': %s } }",
489 static void migrate_recover(QTestState *who, const char *uri)
493 rsp = wait_command(who,
494 "{ 'execute': 'migrate-recover', "
495 " 'id': 'recover-cmd', "
496 " 'arguments': { 'uri': %s } }",
501 static void migrate_set_capability(QTestState *who, const char *capability,
507 "{ 'execute': 'migrate-set-capabilities',"
509 "'capabilities': [ { "
510 "'capability': %s, 'state': %i } ] } }",
512 g_assert(qdict_haskey(rsp, "return"));
517 * Send QMP command "migrate".
518 * Arguments are built from @fmt... (formatted like
519 * qobject_from_jsonf_nofail()) with "uri": @uri spliced in.
522 static void migrate(QTestState *who, const char *uri, const char *fmt, ...)
528 args = qdict_from_vjsonf_nofail(fmt, ap);
531 g_assert(!qdict_haskey(args, "uri"));
532 qdict_put_str(args, "uri", uri);
534 rsp = qtest_qmp(who, "{ 'execute': 'migrate', 'arguments': %p}", args);
536 g_assert(qdict_haskey(rsp, "return"));
540 static void migrate_postcopy_start(QTestState *from, QTestState *to)
544 rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
548 qtest_qmp_eventwait(from, "STOP");
551 qtest_qmp_eventwait(to, "RESUME");
554 static int test_migrate_start(QTestState **from, QTestState **to,
555 const char *uri, bool hide_stderr,
556 bool use_shmem, const char *opts_src,
557 const char *opts_dst)
559 gchar *cmd_src, *cmd_dst;
560 gchar *cmd_source, *cmd_target;
561 const gchar *ignore_stderr;
562 char *bootpath = NULL;
563 char *extra_opts = NULL;
564 char *shmem_path = NULL;
565 const char *arch = qtest_get_arch();
566 const char *machine_type;
567 const char *machine_args;
569 opts_src = opts_src ? opts_src : "";
570 opts_dst = opts_dst ? opts_dst : "";
573 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
574 g_test_skip("/dev/shm is not supported");
577 shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
581 bootpath = g_strdup_printf("%s/bootsect", tmpfs);
582 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
583 /* the assembled x86 boot sector should be exactly one sector large */
584 assert(sizeof(x86_bootsect) == 512);
585 init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect));
588 extra_opts = use_shmem ? get_shmem_opts("150M", shmem_path) : NULL;
589 cmd_src = g_strdup_printf("-m 150M"
590 " -name source,debug-threads=on"
591 " -serial file:%s/src_serial"
592 " -drive file=%s,format=raw %s",
594 extra_opts ? extra_opts : "");
595 cmd_dst = g_strdup_printf("-m 150M"
596 " -name target,debug-threads=on"
597 " -serial file:%s/dest_serial"
598 " -drive file=%s,format=raw"
600 tmpfs, bootpath, uri,
601 extra_opts ? extra_opts : "");
602 start_address = X86_TEST_MEM_START;
603 end_address = X86_TEST_MEM_END;
604 } else if (g_str_equal(arch, "s390x")) {
605 init_bootfile(bootpath, s390x_elf, sizeof(s390x_elf));
608 extra_opts = use_shmem ? get_shmem_opts("128M", shmem_path) : NULL;
609 cmd_src = g_strdup_printf("-m 128M"
610 " -name source,debug-threads=on"
611 " -serial file:%s/src_serial -bios %s %s",
613 extra_opts ? extra_opts : "");
614 cmd_dst = g_strdup_printf("-m 128M"
615 " -name target,debug-threads=on"
616 " -serial file:%s/dest_serial -bios %s"
618 tmpfs, bootpath, uri,
619 extra_opts ? extra_opts : "");
620 start_address = S390_TEST_MEM_START;
621 end_address = S390_TEST_MEM_END;
622 } else if (strcmp(arch, "ppc64") == 0) {
624 machine_args = ",vsmt=8";
625 extra_opts = use_shmem ? get_shmem_opts("256M", shmem_path) : NULL;
626 cmd_src = g_strdup_printf("-m 256M -nodefaults"
627 " -name source,debug-threads=on"
628 " -serial file:%s/src_serial"
629 " -prom-env 'use-nvramrc?=true' -prom-env "
630 "'nvramrc=hex .\" _\" begin %x %x "
631 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
632 "until' %s", tmpfs, end_address,
633 start_address, extra_opts ? extra_opts : "");
634 cmd_dst = g_strdup_printf("-m 256M"
635 " -name target,debug-threads=on"
636 " -serial file:%s/dest_serial"
639 extra_opts ? extra_opts : "");
641 start_address = PPC_TEST_MEM_START;
642 end_address = PPC_TEST_MEM_END;
643 } else if (strcmp(arch, "aarch64") == 0) {
644 init_bootfile(bootpath, aarch64_kernel, sizeof(aarch64_kernel));
645 machine_type = "virt,";
646 machine_args = "gic-version=max";
647 extra_opts = use_shmem ? get_shmem_opts("150M", shmem_path) : NULL;
648 cmd_src = g_strdup_printf("-name vmsource,debug-threads=on -cpu max "
649 "-m 150M -serial file:%s/src_serial "
652 extra_opts ? extra_opts : "");
653 cmd_dst = g_strdup_printf("-name vmdest,debug-threads=on -cpu max "
654 "-m 150M -serial file:%s/dest_serial "
657 tmpfs, bootpath, uri,
658 extra_opts ? extra_opts : "");
660 start_address = ARM_TEST_MEM_START;
661 end_address = ARM_TEST_MEM_END;
663 g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
665 g_assert_not_reached();
672 ignore_stderr = "2>/dev/null";
677 cmd_source = g_strdup_printf("-machine %saccel=kvm:tcg%s %s %s %s",
678 machine_type, machine_args,
679 cmd_src, opts_src, ignore_stderr);
681 *from = qtest_init(cmd_source);
684 cmd_target = g_strdup_printf("-machine %saccel=kvm:tcg%s %s %s %s",
685 machine_type, machine_args,
686 cmd_dst, opts_dst, ignore_stderr);
688 *to = qtest_init(cmd_target);
692 * Remove shmem file immediately to avoid memory leak in test failed case.
693 * It's valid becase QEMU has already opened this file
703 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
705 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
710 qtest_memread(to, start_address, &dest_byte_a, 1);
712 /* Destination still running, wait for a byte to change */
714 qtest_memread(to, start_address, &dest_byte_b, 1);
716 } while (dest_byte_a == dest_byte_b);
718 qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
720 /* With it stopped, check nothing changes */
721 qtest_memread(to, start_address, &dest_byte_c, 1);
723 qtest_memread(to, start_address, &dest_byte_d, 1);
724 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
726 check_guests_ram(to);
732 cleanup("migsocket");
733 cleanup("src_serial");
734 cleanup("dest_serial");
737 static void deprecated_set_downtime(QTestState *who, const double value)
742 "{ 'execute': 'migrate_set_downtime',"
743 " 'arguments': { 'value': %f } }", value);
744 g_assert(qdict_haskey(rsp, "return"));
746 migrate_check_parameter_int(who, "downtime-limit", value * 1000);
749 static void deprecated_set_speed(QTestState *who, long long value)
753 rsp = qtest_qmp(who, "{ 'execute': 'migrate_set_speed',"
754 "'arguments': { 'value': %lld } }", value);
755 g_assert(qdict_haskey(rsp, "return"));
757 migrate_check_parameter_int(who, "max-bandwidth", value);
760 static void deprecated_set_cache_size(QTestState *who, long long value)
764 rsp = qtest_qmp(who, "{ 'execute': 'migrate-set-cache-size',"
765 "'arguments': { 'value': %lld } }", value);
766 g_assert(qdict_haskey(rsp, "return"));
768 migrate_check_parameter_int(who, "xbzrle-cache-size", value);
771 static void test_deprecated(void)
775 from = qtest_init("-machine none");
777 deprecated_set_downtime(from, 0.12345);
778 deprecated_set_speed(from, 12345);
779 deprecated_set_cache_size(from, 4096);
784 static int migrate_postcopy_prepare(QTestState **from_ptr,
788 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
789 QTestState *from, *to;
791 if (test_migrate_start(&from, &to, uri, hide_error, false, NULL, NULL)) {
795 migrate_set_capability(from, "postcopy-ram", true);
796 migrate_set_capability(to, "postcopy-ram", true);
797 migrate_set_capability(to, "postcopy-blocktime", true);
799 /* We want to pick a speed slow enough that the test completes
800 * quickly, but that it doesn't complete precopy even on a slow
801 * machine, so also set the downtime.
803 migrate_set_parameter_int(from, "max-bandwidth", 30000000);
804 migrate_set_parameter_int(from, "downtime-limit", 1);
806 /* Wait for the first serial output from the source */
807 wait_for_serial("src_serial");
809 migrate(from, uri, "{}");
812 wait_for_migration_pass(from);
820 static void migrate_postcopy_complete(QTestState *from, QTestState *to)
822 wait_for_migration_complete(from);
824 /* Make sure we get at least one "B" on destination */
825 wait_for_serial("dest_serial");
827 if (uffd_feature_thread_id) {
831 test_migrate_end(from, to, true);
834 static void test_postcopy(void)
836 QTestState *from, *to;
838 if (migrate_postcopy_prepare(&from, &to, false)) {
841 migrate_postcopy_start(from, to);
842 migrate_postcopy_complete(from, to);
845 static void test_postcopy_recovery(void)
847 QTestState *from, *to;
850 if (migrate_postcopy_prepare(&from, &to, true)) {
854 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
855 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
857 /* Now we start the postcopy */
858 migrate_postcopy_start(from, to);
861 * Wait until postcopy is really started; we can only run the
862 * migrate-pause command during a postcopy
864 wait_for_migration_status(from, "postcopy-active", NULL);
867 * Manually stop the postcopy migration. This emulates a network
868 * failure with the migration socket
873 * Wait for destination side to reach postcopy-paused state. The
874 * migrate-recover command can only succeed if destination machine
875 * is in the paused state
877 wait_for_migration_status(to, "postcopy-paused",
878 (const char * []) { "failed", "active",
879 "completed", NULL });
882 * Create a new socket to emulate a new channel that is different
883 * from the broken migration channel; tell the destination to
884 * listen to the new port
886 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
887 migrate_recover(to, uri);
890 * Try to rebuild the migration channel using the resume flag and
891 * the newly created channel
893 wait_for_migration_status(from, "postcopy-paused",
894 (const char * []) { "failed", "active",
895 "completed", NULL });
896 migrate(from, uri, "{'resume': true}");
899 /* Restore the postcopy bandwidth to unlimited */
900 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
902 migrate_postcopy_complete(from, to);
905 static void wait_for_migration_fail(QTestState *from, bool allow_active)
912 status = migrate_query_status(from);
913 bool result = !strcmp(status, "setup") || !strcmp(status, "failed") ||
914 (allow_active && !strcmp(status, "active"));
916 fprintf(stderr, "%s: unexpected status status=%s allow_active=%d\n",
917 __func__, status, allow_active);
920 failed = !strcmp(status, "failed");
924 /* Is the machine currently running? */
925 rsp_return = wait_command(from, "{ 'execute': 'query-status' }");
926 g_assert(qdict_haskey(rsp_return, "running"));
927 g_assert(qdict_get_bool(rsp_return, "running"));
928 qobject_unref(rsp_return);
931 static void test_baddest(void)
933 QTestState *from, *to;
935 if (test_migrate_start(&from, &to, "tcp:0:0", true, false, NULL, NULL)) {
938 migrate(from, "tcp:0:0", "{}");
939 wait_for_migration_fail(from, false);
940 test_migrate_end(from, to, false);
943 static void test_precopy_unix(void)
945 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
946 QTestState *from, *to;
948 if (test_migrate_start(&from, &to, uri, false, false, NULL, NULL)) {
952 /* We want to pick a speed slow enough that the test completes
953 * quickly, but that it doesn't complete precopy even on a slow
954 * machine, so also set the downtime.
956 /* 1 ms should make it not converge*/
957 migrate_set_parameter_int(from, "downtime-limit", 1);
959 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
961 /* Wait for the first serial output from the source */
962 wait_for_serial("src_serial");
964 migrate(from, uri, "{}");
966 wait_for_migration_pass(from);
968 /* 300 ms should converge */
969 migrate_set_parameter_int(from, "downtime-limit", 300);
972 qtest_qmp_eventwait(from, "STOP");
975 qtest_qmp_eventwait(to, "RESUME");
977 wait_for_serial("dest_serial");
978 wait_for_migration_complete(from);
980 test_migrate_end(from, to, true);
985 /* Currently upset on aarch64 TCG */
986 static void test_ignore_shared(void)
988 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
989 QTestState *from, *to;
991 if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) {
995 migrate_set_capability(from, "x-ignore-shared", true);
996 migrate_set_capability(to, "x-ignore-shared", true);
998 /* Wait for the first serial output from the source */
999 wait_for_serial("src_serial");
1001 migrate(from, uri, "{}");
1003 wait_for_migration_pass(from);
1006 qtest_qmp_eventwait(from, "STOP");
1009 qtest_qmp_eventwait(to, "RESUME");
1011 wait_for_serial("dest_serial");
1012 wait_for_migration_complete(from);
1014 /* Check whether shared RAM has been really skipped */
1015 g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
1017 test_migrate_end(from, to, true);
1022 static void test_xbzrle(const char *uri)
1024 QTestState *from, *to;
1026 if (test_migrate_start(&from, &to, uri, false, false, NULL, NULL)) {
1031 * We want to pick a speed slow enough that the test completes
1032 * quickly, but that it doesn't complete precopy even on a slow
1033 * machine, so also set the downtime.
1035 /* 1 ms should make it not converge*/
1036 migrate_set_parameter_int(from, "downtime-limit", 1);
1038 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
1040 migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
1042 migrate_set_capability(from, "xbzrle", "true");
1043 migrate_set_capability(to, "xbzrle", "true");
1044 /* Wait for the first serial output from the source */
1045 wait_for_serial("src_serial");
1047 migrate(from, uri, "{}");
1049 wait_for_migration_pass(from);
1051 /* 300ms should converge */
1052 migrate_set_parameter_int(from, "downtime-limit", 300);
1055 qtest_qmp_eventwait(from, "STOP");
1057 qtest_qmp_eventwait(to, "RESUME");
1059 wait_for_serial("dest_serial");
1060 wait_for_migration_complete(from);
1062 test_migrate_end(from, to, true);
1065 static void test_xbzrle_unix(void)
1067 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1073 static void test_precopy_tcp(void)
1076 QTestState *from, *to;
1078 if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", false, false,
1084 * We want to pick a speed slow enough that the test completes
1085 * quickly, but that it doesn't complete precopy even on a slow
1086 * machine, so also set the downtime.
1088 /* 1 ms should make it not converge*/
1089 migrate_set_parameter_int(from, "downtime-limit", 1);
1091 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
1093 /* Wait for the first serial output from the source */
1094 wait_for_serial("src_serial");
1096 uri = migrate_get_socket_address(to, "socket-address");
1098 migrate(from, uri, "{}");
1100 wait_for_migration_pass(from);
1102 /* 300ms should converge */
1103 migrate_set_parameter_int(from, "downtime-limit", 300);
1106 qtest_qmp_eventwait(from, "STOP");
1108 qtest_qmp_eventwait(to, "RESUME");
1110 wait_for_serial("dest_serial");
1111 wait_for_migration_complete(from);
1113 test_migrate_end(from, to, true);
1117 static void test_migrate_fd_proto(void)
1119 QTestState *from, *to;
1123 const char *error_desc;
1125 if (test_migrate_start(&from, &to, "defer", false, false, NULL, NULL)) {
1130 * We want to pick a speed slow enough that the test completes
1131 * quickly, but that it doesn't complete precopy even on a slow
1132 * machine, so also set the downtime.
1134 /* 1 ms should make it not converge */
1135 migrate_set_parameter_int(from, "downtime-limit", 1);
1137 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
1139 /* Wait for the first serial output from the source */
1140 wait_for_serial("src_serial");
1142 /* Create two connected sockets for migration */
1143 ret = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
1144 g_assert_cmpint(ret, ==, 0);
1146 /* Send the 1st socket to the target */
1147 rsp = wait_command_fd(to, pair[0],
1148 "{ 'execute': 'getfd',"
1149 " 'arguments': { 'fdname': 'fd-mig' }}");
1153 /* Start incoming migration from the 1st socket */
1154 rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1155 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1158 /* Send the 2nd socket to the target */
1159 rsp = wait_command_fd(from, pair[1],
1160 "{ 'execute': 'getfd',"
1161 " 'arguments': { 'fdname': 'fd-mig' }}");
1165 /* Start migration to the 2nd socket*/
1166 migrate(from, "fd:fd-mig", "{}");
1168 wait_for_migration_pass(from);
1170 /* 300ms should converge */
1171 migrate_set_parameter_int(from, "downtime-limit", 300);
1174 qtest_qmp_eventwait(from, "STOP");
1176 qtest_qmp_eventwait(to, "RESUME");
1178 /* Test closing fds */
1179 /* We assume, that QEMU removes named fd from its list,
1180 * so this should fail */
1181 rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
1182 " 'arguments': { 'fdname': 'fd-mig' }}");
1183 g_assert_true(qdict_haskey(rsp, "error"));
1184 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1185 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1188 rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
1189 " 'arguments': { 'fdname': 'fd-mig' }}");
1190 g_assert_true(qdict_haskey(rsp, "error"));
1191 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1192 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1195 /* Complete migration */
1196 wait_for_serial("dest_serial");
1197 wait_for_migration_complete(from);
1198 test_migrate_end(from, to, true);
1201 static void do_test_validate_uuid(const char *uuid_arg_src,
1202 const char *uuid_arg_dst,
1203 bool should_fail, bool hide_stderr)
1205 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1206 QTestState *from, *to;
1208 if (test_migrate_start(&from, &to, uri, hide_stderr, false,
1209 uuid_arg_src, uuid_arg_dst)) {
1214 * UUID validation is at the begin of migration. So, the main process of
1215 * migration is not interesting for us here. Thus, set huge downtime for
1216 * very fast migration.
1218 migrate_set_parameter_int(from, "downtime-limit", 1000000);
1219 migrate_set_capability(from, "validate-uuid", true);
1221 /* Wait for the first serial output from the source */
1222 wait_for_serial("src_serial");
1224 migrate(from, uri, "{}");
1227 qtest_set_expected_status(to, 1);
1228 wait_for_migration_fail(from, true);
1230 wait_for_migration_complete(from);
1233 test_migrate_end(from, to, false);
1237 static void test_validate_uuid(void)
1239 do_test_validate_uuid("-uuid 11111111-1111-1111-1111-111111111111",
1240 "-uuid 11111111-1111-1111-1111-111111111111",
1244 static void test_validate_uuid_error(void)
1246 do_test_validate_uuid("-uuid 11111111-1111-1111-1111-111111111111",
1247 "-uuid 22222222-2222-2222-2222-222222222222",
1251 static void test_validate_uuid_src_not_set(void)
1253 do_test_validate_uuid(NULL, "-uuid 11111111-1111-1111-1111-111111111111",
1257 static void test_validate_uuid_dst_not_set(void)
1259 do_test_validate_uuid("-uuid 11111111-1111-1111-1111-111111111111", NULL,
1263 static void test_migrate_auto_converge(void)
1265 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1266 QTestState *from, *to;
1267 int64_t remaining, percentage;
1270 * We want the test to be stable and as fast as possible.
1271 * E.g., with 1Gb/s bandwith migration may pass without throttling,
1272 * so we need to decrease a bandwidth.
1274 const int64_t init_pct = 5, inc_pct = 50, max_pct = 95;
1275 const int64_t max_bandwidth = 400000000; /* ~400Mb/s */
1276 const int64_t downtime_limit = 250; /* 250ms */
1278 * We migrate through unix-socket (> 500Mb/s).
1279 * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
1280 * So, we can predict expected_threshold
1282 const int64_t expected_threshold = max_bandwidth * downtime_limit / 1000;
1284 if (test_migrate_start(&from, &to, uri, false, false, NULL, NULL)) {
1288 migrate_set_capability(from, "auto-converge", true);
1289 migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct);
1290 migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct);
1291 migrate_set_parameter_int(from, "max-cpu-throttle", max_pct);
1294 * Set the initial parameters so that the migration could not converge
1295 * without throttling.
1297 migrate_set_parameter_int(from, "downtime-limit", 1);
1298 migrate_set_parameter_int(from, "max-bandwidth", 100000000); /* ~100Mb/s */
1300 /* To check remaining size after precopy */
1301 migrate_set_capability(from, "pause-before-switchover", true);
1303 /* Wait for the first serial output from the source */
1304 wait_for_serial("src_serial");
1306 migrate(from, uri, "{}");
1308 /* Wait for throttling begins */
1310 while (percentage == 0) {
1311 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1313 g_assert_false(got_stop);
1315 /* The first percentage of throttling should be equal to init_pct */
1316 g_assert_cmpint(percentage, ==, init_pct);
1317 /* Now, when we tested that throttling works, let it converge */
1318 migrate_set_parameter_int(from, "downtime-limit", downtime_limit);
1319 migrate_set_parameter_int(from, "max-bandwidth", max_bandwidth);
1322 * Wait for pre-switchover status to check last throttle percentage
1323 * and remaining. These values will be zeroed later
1325 wait_for_migration_status(from, "pre-switchover", NULL);
1327 /* The final percentage of throttling shouldn't be greater than max_pct */
1328 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1329 g_assert_cmpint(percentage, <=, max_pct);
1331 remaining = read_ram_property_int(from, "remaining");
1332 g_assert_cmpint(remaining, <, expected_threshold);
1334 migrate_continue(from, "pre-switchover");
1336 qtest_qmp_eventwait(to, "RESUME");
1338 wait_for_serial("dest_serial");
1339 wait_for_migration_complete(from);
1343 test_migrate_end(from, to, true);
1346 int main(int argc, char **argv)
1348 char template[] = "/tmp/migration-test-XXXXXX";
1351 g_test_init(&argc, &argv, NULL);
1353 if (!ufd_version_check()) {
1354 return g_test_run();
1358 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
1359 * is touchy due to race conditions on dirty bits (especially on PPC for
1362 if (g_str_equal(qtest_get_arch(), "ppc64") &&
1363 (access("/sys/module/kvm_hv", F_OK) ||
1364 access("/dev/kvm", R_OK | W_OK))) {
1365 g_test_message("Skipping test: kvm_hv not available");
1366 return g_test_run();
1370 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
1371 * there until the problems are resolved
1373 if (g_str_equal(qtest_get_arch(), "s390x")) {
1374 #if defined(HOST_S390X)
1375 if (access("/dev/kvm", R_OK | W_OK)) {
1376 g_test_message("Skipping test: kvm not available");
1377 return g_test_run();
1380 g_test_message("Skipping test: Need s390x host to work properly");
1381 return g_test_run();
1385 tmpfs = mkdtemp(template);
1387 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno));
1391 module_call_init(MODULE_INIT_QOM);
1393 qtest_add_func("/migration/postcopy/unix", test_postcopy);
1394 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery);
1395 qtest_add_func("/migration/deprecated", test_deprecated);
1396 qtest_add_func("/migration/bad_dest", test_baddest);
1397 qtest_add_func("/migration/precopy/unix", test_precopy_unix);
1398 qtest_add_func("/migration/precopy/tcp", test_precopy_tcp);
1399 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
1400 qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix);
1401 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto);
1402 qtest_add_func("/migration/validate_uuid", test_validate_uuid);
1403 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error);
1404 qtest_add_func("/migration/validate_uuid_src_not_set",
1405 test_validate_uuid_src_not_set);
1406 qtest_add_func("/migration/validate_uuid_dst_not_set",
1407 test_validate_uuid_dst_not_set);
1409 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge);
1413 g_assert_cmpint(ret, ==, 0);
1417 g_test_message("unable to rmdir: path (%s): %s",
1418 tmpfs, strerror(errno));