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 "sysemu/sysemu.h"
24 #include "qapi/qapi-visit-sockets.h"
25 #include "qapi/qobject-input-visitor.h"
26 #include "qapi/qobject-output-visitor.h"
28 #include "migration/migration-test.h"
30 /* TODO actually test the results and get rid of this */
31 #define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__))
33 unsigned start_address;
36 static bool uffd_feature_thread_id;
38 #if defined(__linux__)
39 #include <sys/syscall.h>
43 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
44 #include <sys/eventfd.h>
45 #include <sys/ioctl.h>
46 #include <linux/userfaultfd.h>
48 static bool ufd_version_check(void)
50 struct uffdio_api api_struct;
53 int ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
56 g_test_message("Skipping test: userfaultfd not available");
60 api_struct.api = UFFD_API;
61 api_struct.features = 0;
62 if (ioctl(ufd, UFFDIO_API, &api_struct)) {
63 g_test_message("Skipping test: UFFDIO_API failed");
66 uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
68 ioctl_mask = (__u64)1 << _UFFDIO_REGISTER |
69 (__u64)1 << _UFFDIO_UNREGISTER;
70 if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
71 g_test_message("Skipping test: Missing userfault feature");
79 static bool ufd_version_check(void)
81 g_test_message("Skipping test: Userfault not available (builtdtime)");
87 static const char *tmpfs;
89 /* The boot file modifies memory area in [start_address, end_address)
90 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
92 #include "tests/migration/i386/a-b-bootblock.h"
93 #include "tests/migration/aarch64/a-b-kernel.h"
95 static void init_bootfile(const char *bootpath, void *content)
97 FILE *bootfile = fopen(bootpath, "wb");
99 g_assert_cmpint(fwrite(content, 512, 1, bootfile), ==, 1);
103 #include "tests/migration/s390x/a-b-bios.h"
105 static void init_bootfile_s390x(const char *bootpath)
107 FILE *bootfile = fopen(bootpath, "wb");
108 size_t len = sizeof(s390x_elf);
110 g_assert_cmpint(fwrite(s390x_elf, len, 1, bootfile), ==, 1);
115 * Wait for some output in the serial output file,
116 * we get an 'A' followed by an endless string of 'B's
117 * but on the destination we won't have the A.
119 static void wait_for_serial(const char *side)
121 char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
122 FILE *serialfile = fopen(serialpath, "r");
123 const char *arch = qtest_get_arch();
124 int started = (strcmp(side, "src_serial") == 0 &&
125 strcmp(arch, "ppc64") == 0) ? 0 : 1;
129 int readvalue = fgetc(serialfile);
132 /* SLOF prints its banner before starting test,
133 * to ignore it, mark the start of the test with '_',
134 * ignore all characters until this marker
141 fseek(serialfile, 0, SEEK_SET);
158 started = (strcmp(side, "src_serial") == 0 &&
159 strcmp(arch, "ppc64") == 0) ? 0 : 1;
160 fseek(serialfile, 0, SEEK_SET);
165 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
166 g_assert_not_reached();
171 static void stop_cb(void *opaque, const char *name, QDict *data)
173 if (!strcmp(name, "STOP")) {
179 * Events can get in the way of responses we are actually waiting for.
182 static QDict *wait_command_fd(QTestState *who, int fd, const char *command, ...)
186 va_start(ap, command);
187 qtest_qmp_vsend_fds(who, &fd, 1, command, ap);
190 return qtest_qmp_receive_success(who, stop_cb, NULL);
194 * Events can get in the way of responses we are actually waiting for.
197 static QDict *wait_command(QTestState *who, const char *command, ...)
201 va_start(ap, command);
202 qtest_qmp_vsend(who, command, ap);
205 return qtest_qmp_receive_success(who, stop_cb, NULL);
209 * Note: caller is responsible to free the returned object via
210 * qobject_unref() after use
212 static QDict *migrate_query(QTestState *who)
214 return wait_command(who, "{ 'execute': 'query-migrate' }");
218 * Note: caller is responsible to free the returned object via
221 static gchar *migrate_query_status(QTestState *who)
223 QDict *rsp_return = migrate_query(who);
224 gchar *status = g_strdup(qdict_get_str(rsp_return, "status"));
227 qobject_unref(rsp_return);
233 * It's tricky to use qemu's migration event capability with qtest,
234 * events suddenly appearing confuse the qmp()/hmp() responses.
237 static int64_t read_ram_property_int(QTestState *who, const char *property)
239 QDict *rsp_return, *rsp_ram;
242 rsp_return = migrate_query(who);
243 if (!qdict_haskey(rsp_return, "ram")) {
247 rsp_ram = qdict_get_qdict(rsp_return, "ram");
248 result = qdict_get_try_int(rsp_ram, 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 void wait_for_migration_status(QTestState *who,
275 status = migrate_query_status(who);
276 completed = strcmp(status, goal) == 0;
277 g_assert_cmpstr(status, !=, "failed");
286 static void wait_for_migration_complete(QTestState *who)
288 wait_for_migration_status(who, "completed");
291 static void wait_for_migration_pass(QTestState *who)
293 uint64_t initial_pass = get_migration_pass(who);
296 /* Wait for the 1st sync */
297 while (!got_stop && !initial_pass) {
299 initial_pass = get_migration_pass(who);
304 pass = get_migration_pass(who);
305 } while (pass == initial_pass && !got_stop);
308 static void check_guests_ram(QTestState *who)
310 /* Our ASM test will have been incrementing one byte from each page from
311 * start_address to < end_address in order. This gives us a constraint
312 * that any page's byte should be equal or less than the previous pages
313 * byte (mod 256); and they should all be equal except for one transition
314 * at the point where we meet the incrementer. (We're running this with
315 * the guest stopped).
320 bool hit_edge = false;
323 qtest_memread(who, start_address, &first_byte, 1);
324 last_byte = first_byte;
326 for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
327 address += TEST_MEM_PAGE_SIZE)
330 qtest_memread(who, address, &b, 1);
331 if (b != last_byte) {
332 if (((b + 1) % 256) == last_byte && !hit_edge) {
333 /* This is OK, the guest stopped at the point of
334 * incrementing the previous page but didn't get
340 fprintf(stderr, "Memory content inconsistency at %x"
341 " first_byte = %x last_byte = %x current = %x"
343 address, first_byte, last_byte, b, hit_edge);
351 static void cleanup(const char *filename)
353 char *path = g_strdup_printf("%s/%s", tmpfs, filename);
359 static char *get_shmem_opts(const char *mem_size, const char *shmem_path)
361 return g_strdup_printf("-object memory-backend-file,id=mem0,size=%s"
362 ",mem-path=%s,share=on -numa node,memdev=mem0",
363 mem_size, shmem_path);
366 static char *SocketAddress_to_str(SocketAddress *addr)
368 switch (addr->type) {
369 case SOCKET_ADDRESS_TYPE_INET:
370 return g_strdup_printf("tcp:%s:%s",
373 case SOCKET_ADDRESS_TYPE_UNIX:
374 return g_strdup_printf("unix:%s",
375 addr->u.q_unix.path);
376 case SOCKET_ADDRESS_TYPE_FD:
377 return g_strdup_printf("fd:%s", addr->u.fd.str);
378 case SOCKET_ADDRESS_TYPE_VSOCK:
379 return g_strdup_printf("tcp:%s:%s",
383 return g_strdup("unknown address type");
387 static char *migrate_get_socket_address(QTestState *who, const char *parameter)
391 Error *local_err = NULL;
392 SocketAddressList *addrs;
396 rsp = migrate_query(who);
397 object = qdict_get(rsp, parameter);
399 iv = qobject_input_visitor_new(object);
400 visit_type_SocketAddressList(iv, NULL, &addrs, &local_err);
403 /* we are only using a single address */
404 result = SocketAddress_to_str(addrs->value);
406 qapi_free_SocketAddressList(addrs);
411 static long long migrate_get_parameter(QTestState *who, const char *parameter)
416 rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
417 result = qdict_get_int(rsp, parameter);
422 static void migrate_check_parameter(QTestState *who, const char *parameter,
427 result = migrate_get_parameter(who, parameter);
428 g_assert_cmpint(result, ==, value);
431 static void migrate_set_parameter(QTestState *who, const char *parameter,
437 "{ 'execute': 'migrate-set-parameters',"
438 "'arguments': { %s: %lld } }",
440 g_assert(qdict_haskey(rsp, "return"));
442 migrate_check_parameter(who, parameter, value);
445 static void migrate_pause(QTestState *who)
449 rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
453 static void migrate_recover(QTestState *who, const char *uri)
457 rsp = wait_command(who,
458 "{ 'execute': 'migrate-recover', "
459 " 'id': 'recover-cmd', "
460 " 'arguments': { 'uri': %s } }",
465 static void migrate_set_capability(QTestState *who, const char *capability,
471 "{ 'execute': 'migrate-set-capabilities',"
473 "'capabilities': [ { "
474 "'capability': %s, 'state': %i } ] } }",
476 g_assert(qdict_haskey(rsp, "return"));
481 * Send QMP command "migrate".
482 * Arguments are built from @fmt... (formatted like
483 * qobject_from_jsonf_nofail()) with "uri": @uri spliced in.
486 static void migrate(QTestState *who, const char *uri, const char *fmt, ...)
492 args = qdict_from_vjsonf_nofail(fmt, ap);
495 g_assert(!qdict_haskey(args, "uri"));
496 qdict_put_str(args, "uri", uri);
498 rsp = qmp("{ 'execute': 'migrate', 'arguments': %p}", args);
500 g_assert(qdict_haskey(rsp, "return"));
504 static void migrate_postcopy_start(QTestState *from, QTestState *to)
508 rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
512 qtest_qmp_eventwait(from, "STOP");
515 qtest_qmp_eventwait(to, "RESUME");
518 static int test_migrate_start(QTestState **from, QTestState **to,
519 const char *uri, bool hide_stderr,
522 gchar *cmd_src, *cmd_dst;
523 char *bootpath = NULL;
524 char *extra_opts = NULL;
525 char *shmem_path = NULL;
526 const char *arch = qtest_get_arch();
527 const char *accel = "kvm:tcg";
530 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
531 g_test_skip("/dev/shm is not supported");
534 shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
538 bootpath = g_strdup_printf("%s/bootsect", tmpfs);
539 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
540 init_bootfile(bootpath, x86_bootsect);
541 extra_opts = use_shmem ? get_shmem_opts("150M", shmem_path) : NULL;
542 cmd_src = g_strdup_printf("-machine accel=%s -m 150M"
543 " -name source,debug-threads=on"
544 " -serial file:%s/src_serial"
545 " -drive file=%s,format=raw %s",
546 accel, tmpfs, bootpath,
547 extra_opts ? extra_opts : "");
548 cmd_dst = g_strdup_printf("-machine accel=%s -m 150M"
549 " -name target,debug-threads=on"
550 " -serial file:%s/dest_serial"
551 " -drive file=%s,format=raw"
553 accel, tmpfs, bootpath, uri,
554 extra_opts ? extra_opts : "");
555 start_address = X86_TEST_MEM_START;
556 end_address = X86_TEST_MEM_END;
557 } else if (g_str_equal(arch, "s390x")) {
558 init_bootfile_s390x(bootpath);
559 extra_opts = use_shmem ? get_shmem_opts("128M", shmem_path) : NULL;
560 cmd_src = g_strdup_printf("-machine accel=%s -m 128M"
561 " -name source,debug-threads=on"
562 " -serial file:%s/src_serial -bios %s %s",
563 accel, tmpfs, bootpath,
564 extra_opts ? extra_opts : "");
565 cmd_dst = g_strdup_printf("-machine accel=%s -m 128M"
566 " -name target,debug-threads=on"
567 " -serial file:%s/dest_serial -bios %s"
569 accel, tmpfs, bootpath, uri,
570 extra_opts ? extra_opts : "");
571 start_address = S390_TEST_MEM_START;
572 end_address = S390_TEST_MEM_END;
573 } else if (strcmp(arch, "ppc64") == 0) {
574 extra_opts = use_shmem ? get_shmem_opts("256M", shmem_path) : NULL;
575 cmd_src = g_strdup_printf("-machine accel=%s -m 256M -nodefaults"
576 " -name source,debug-threads=on"
577 " -serial file:%s/src_serial"
578 " -prom-env 'use-nvramrc?=true' -prom-env "
579 "'nvramrc=hex .\" _\" begin %x %x "
580 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
581 "until' %s", accel, tmpfs, end_address,
582 start_address, extra_opts ? extra_opts : "");
583 cmd_dst = g_strdup_printf("-machine accel=%s -m 256M"
584 " -name target,debug-threads=on"
585 " -serial file:%s/dest_serial"
588 extra_opts ? extra_opts : "");
590 start_address = PPC_TEST_MEM_START;
591 end_address = PPC_TEST_MEM_END;
592 } else if (strcmp(arch, "aarch64") == 0) {
593 init_bootfile(bootpath, aarch64_kernel);
594 extra_opts = use_shmem ? get_shmem_opts("150M", shmem_path) : NULL;
595 cmd_src = g_strdup_printf("-machine virt,accel=%s,gic-version=max "
596 "-name vmsource,debug-threads=on -cpu max "
597 "-m 150M -serial file:%s/src_serial "
599 accel, tmpfs, bootpath,
600 extra_opts ? extra_opts : "");
601 cmd_dst = g_strdup_printf("-machine virt,accel=%s,gic-version=max "
602 "-name vmdest,debug-threads=on -cpu max "
603 "-m 150M -serial file:%s/dest_serial "
606 accel, tmpfs, bootpath, uri,
607 extra_opts ? extra_opts : "");
609 start_address = ARM_TEST_MEM_START;
610 end_address = ARM_TEST_MEM_END;
612 g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
614 g_assert_not_reached();
622 tmp = g_strdup_printf("%s 2>/dev/null", cmd_src);
626 tmp = g_strdup_printf("%s 2>/dev/null", cmd_dst);
631 *from = qtest_start(cmd_src);
634 *to = qtest_init(cmd_dst);
638 * Remove shmem file immediately to avoid memory leak in test failed case.
639 * It's valid becase QEMU has already opened this file
649 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
651 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
656 qtest_memread(to, start_address, &dest_byte_a, 1);
658 /* Destination still running, wait for a byte to change */
660 qtest_memread(to, start_address, &dest_byte_b, 1);
662 } while (dest_byte_a == dest_byte_b);
664 qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
666 /* With it stopped, check nothing changes */
667 qtest_memread(to, start_address, &dest_byte_c, 1);
669 qtest_memread(to, start_address, &dest_byte_d, 1);
670 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
672 check_guests_ram(to);
678 cleanup("migsocket");
679 cleanup("src_serial");
680 cleanup("dest_serial");
683 static void deprecated_set_downtime(QTestState *who, const double value)
688 "{ 'execute': 'migrate_set_downtime',"
689 " 'arguments': { 'value': %f } }", value);
690 g_assert(qdict_haskey(rsp, "return"));
692 migrate_check_parameter(who, "downtime-limit", value * 1000);
695 static void deprecated_set_speed(QTestState *who, long long value)
699 rsp = qtest_qmp(who, "{ 'execute': 'migrate_set_speed',"
700 "'arguments': { 'value': %lld } }", value);
701 g_assert(qdict_haskey(rsp, "return"));
703 migrate_check_parameter(who, "max-bandwidth", value);
706 static void deprecated_set_cache_size(QTestState *who, long long value)
710 rsp = qtest_qmp(who, "{ 'execute': 'migrate-set-cache-size',"
711 "'arguments': { 'value': %lld } }", value);
712 g_assert(qdict_haskey(rsp, "return"));
714 migrate_check_parameter(who, "xbzrle-cache-size", value);
717 static void test_deprecated(void)
721 from = qtest_start("-machine none");
723 deprecated_set_downtime(from, 0.12345);
724 deprecated_set_speed(from, 12345);
725 deprecated_set_cache_size(from, 4096);
730 static int migrate_postcopy_prepare(QTestState **from_ptr,
734 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
735 QTestState *from, *to;
737 if (test_migrate_start(&from, &to, uri, hide_error, false)) {
741 migrate_set_capability(from, "postcopy-ram", true);
742 migrate_set_capability(to, "postcopy-ram", true);
743 migrate_set_capability(to, "postcopy-blocktime", true);
745 /* We want to pick a speed slow enough that the test completes
746 * quickly, but that it doesn't complete precopy even on a slow
747 * machine, so also set the downtime.
749 migrate_set_parameter(from, "max-bandwidth", 100000000);
750 migrate_set_parameter(from, "downtime-limit", 1);
752 /* Wait for the first serial output from the source */
753 wait_for_serial("src_serial");
755 migrate(from, uri, "{}");
758 wait_for_migration_pass(from);
766 static void migrate_postcopy_complete(QTestState *from, QTestState *to)
768 wait_for_migration_complete(from);
770 /* Make sure we get at least one "B" on destination */
771 wait_for_serial("dest_serial");
773 if (uffd_feature_thread_id) {
777 test_migrate_end(from, to, true);
780 static void test_postcopy(void)
782 QTestState *from, *to;
784 if (migrate_postcopy_prepare(&from, &to, false)) {
787 migrate_postcopy_start(from, to);
788 migrate_postcopy_complete(from, to);
791 static void test_postcopy_recovery(void)
793 QTestState *from, *to;
796 if (migrate_postcopy_prepare(&from, &to, true)) {
800 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
801 migrate_set_parameter(from, "max-postcopy-bandwidth", 4096);
803 /* Now we start the postcopy */
804 migrate_postcopy_start(from, to);
807 * Wait until postcopy is really started; we can only run the
808 * migrate-pause command during a postcopy
810 wait_for_migration_status(from, "postcopy-active");
813 * Manually stop the postcopy migration. This emulates a network
814 * failure with the migration socket
819 * Wait for destination side to reach postcopy-paused state. The
820 * migrate-recover command can only succeed if destination machine
821 * is in the paused state
823 wait_for_migration_status(to, "postcopy-paused");
826 * Create a new socket to emulate a new channel that is different
827 * from the broken migration channel; tell the destination to
828 * listen to the new port
830 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
831 migrate_recover(to, uri);
834 * Try to rebuild the migration channel using the resume flag and
835 * the newly created channel
837 wait_for_migration_status(from, "postcopy-paused");
838 migrate(from, uri, "{'resume': true}");
841 /* Restore the postcopy bandwidth to unlimited */
842 migrate_set_parameter(from, "max-postcopy-bandwidth", 0);
844 migrate_postcopy_complete(from, to);
847 static void test_baddest(void)
849 QTestState *from, *to;
854 if (test_migrate_start(&from, &to, "tcp:0:0", true, false)) {
857 migrate(from, "tcp:0:0", "{}");
859 status = migrate_query_status(from);
860 g_assert(!strcmp(status, "setup") || !(strcmp(status, "failed")));
861 failed = !strcmp(status, "failed");
865 /* Is the machine currently running? */
866 rsp_return = wait_command(from, "{ 'execute': 'query-status' }");
867 g_assert(qdict_haskey(rsp_return, "running"));
868 g_assert(qdict_get_bool(rsp_return, "running"));
869 qobject_unref(rsp_return);
871 test_migrate_end(from, to, false);
874 static void test_precopy_unix(void)
876 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
877 QTestState *from, *to;
879 if (test_migrate_start(&from, &to, uri, false, false)) {
883 /* We want to pick a speed slow enough that the test completes
884 * quickly, but that it doesn't complete precopy even on a slow
885 * machine, so also set the downtime.
887 /* 1 ms should make it not converge*/
888 migrate_set_parameter(from, "downtime-limit", 1);
890 migrate_set_parameter(from, "max-bandwidth", 1000000000);
892 /* Wait for the first serial output from the source */
893 wait_for_serial("src_serial");
895 migrate(from, uri, "{}");
897 wait_for_migration_pass(from);
899 /* 300 ms should converge */
900 migrate_set_parameter(from, "downtime-limit", 300);
903 qtest_qmp_eventwait(from, "STOP");
906 qtest_qmp_eventwait(to, "RESUME");
908 wait_for_serial("dest_serial");
909 wait_for_migration_complete(from);
911 test_migrate_end(from, to, true);
916 /* Currently upset on aarch64 TCG */
917 static void test_ignore_shared(void)
919 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
920 QTestState *from, *to;
922 if (test_migrate_start(&from, &to, uri, false, true)) {
926 migrate_set_capability(from, "x-ignore-shared", true);
927 migrate_set_capability(to, "x-ignore-shared", true);
929 /* Wait for the first serial output from the source */
930 wait_for_serial("src_serial");
932 migrate(from, uri, "{}");
934 wait_for_migration_pass(from);
937 qtest_qmp_eventwait(from, "STOP");
940 qtest_qmp_eventwait(to, "RESUME");
942 wait_for_serial("dest_serial");
943 wait_for_migration_complete(from);
945 /* Check whether shared RAM has been really skipped */
946 g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
948 test_migrate_end(from, to, true);
953 static void test_xbzrle(const char *uri)
955 QTestState *from, *to;
957 if (test_migrate_start(&from, &to, uri, false, false)) {
962 * We want to pick a speed slow enough that the test completes
963 * quickly, but that it doesn't complete precopy even on a slow
964 * machine, so also set the downtime.
966 /* 1 ms should make it not converge*/
967 migrate_set_parameter(from, "downtime-limit", 1);
969 migrate_set_parameter(from, "max-bandwidth", 1000000000);
971 migrate_set_parameter(from, "xbzrle-cache-size", 33554432);
973 migrate_set_capability(from, "xbzrle", "true");
974 migrate_set_capability(to, "xbzrle", "true");
975 /* Wait for the first serial output from the source */
976 wait_for_serial("src_serial");
978 migrate(from, uri, "{}");
980 wait_for_migration_pass(from);
982 /* 300ms should converge */
983 migrate_set_parameter(from, "downtime-limit", 300);
986 qtest_qmp_eventwait(from, "STOP");
988 qtest_qmp_eventwait(to, "RESUME");
990 wait_for_serial("dest_serial");
991 wait_for_migration_complete(from);
993 test_migrate_end(from, to, true);
996 static void test_xbzrle_unix(void)
998 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1004 static void test_precopy_tcp(void)
1007 QTestState *from, *to;
1009 if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", false, false)) {
1014 * We want to pick a speed slow enough that the test completes
1015 * quickly, but that it doesn't complete precopy even on a slow
1016 * machine, so also set the downtime.
1018 /* 1 ms should make it not converge*/
1019 migrate_set_parameter(from, "downtime-limit", 1);
1021 migrate_set_parameter(from, "max-bandwidth", 1000000000);
1023 /* Wait for the first serial output from the source */
1024 wait_for_serial("src_serial");
1026 uri = migrate_get_socket_address(to, "socket-address");
1028 migrate(from, uri, "{}");
1030 wait_for_migration_pass(from);
1032 /* 300ms should converge */
1033 migrate_set_parameter(from, "downtime-limit", 300);
1036 qtest_qmp_eventwait(from, "STOP");
1038 qtest_qmp_eventwait(to, "RESUME");
1040 wait_for_serial("dest_serial");
1041 wait_for_migration_complete(from);
1043 test_migrate_end(from, to, true);
1047 static void test_migrate_fd_proto(void)
1049 QTestState *from, *to;
1053 const char *error_desc;
1055 if (test_migrate_start(&from, &to, "defer", false, false)) {
1060 * We want to pick a speed slow enough that the test completes
1061 * quickly, but that it doesn't complete precopy even on a slow
1062 * machine, so also set the downtime.
1064 /* 1 ms should make it not converge */
1065 migrate_set_parameter(from, "downtime-limit", 1);
1067 migrate_set_parameter(from, "max-bandwidth", 1000000000);
1069 /* Wait for the first serial output from the source */
1070 wait_for_serial("src_serial");
1072 /* Create two connected sockets for migration */
1073 ret = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
1074 g_assert_cmpint(ret, ==, 0);
1076 /* Send the 1st socket to the target */
1077 rsp = wait_command_fd(to, pair[0],
1078 "{ 'execute': 'getfd',"
1079 " 'arguments': { 'fdname': 'fd-mig' }}");
1083 /* Start incoming migration from the 1st socket */
1084 rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1085 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1088 /* Send the 2nd socket to the target */
1089 rsp = wait_command_fd(from, pair[1],
1090 "{ 'execute': 'getfd',"
1091 " 'arguments': { 'fdname': 'fd-mig' }}");
1095 /* Start migration to the 2nd socket*/
1096 migrate(from, "fd:fd-mig", "{}");
1098 wait_for_migration_pass(from);
1100 /* 300ms should converge */
1101 migrate_set_parameter(from, "downtime-limit", 300);
1104 qtest_qmp_eventwait(from, "STOP");
1106 qtest_qmp_eventwait(to, "RESUME");
1108 /* Test closing fds */
1109 /* We assume, that QEMU removes named fd from its list,
1110 * so this should fail */
1111 rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
1112 " 'arguments': { 'fdname': 'fd-mig' }}");
1113 g_assert_true(qdict_haskey(rsp, "error"));
1114 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1115 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1118 rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
1119 " 'arguments': { 'fdname': 'fd-mig' }}");
1120 g_assert_true(qdict_haskey(rsp, "error"));
1121 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1122 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1125 /* Complete migration */
1126 wait_for_serial("dest_serial");
1127 wait_for_migration_complete(from);
1128 test_migrate_end(from, to, true);
1131 int main(int argc, char **argv)
1133 char template[] = "/tmp/migration-test-XXXXXX";
1136 g_test_init(&argc, &argv, NULL);
1138 if (!ufd_version_check()) {
1139 return g_test_run();
1143 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
1144 * is touchy due to race conditions on dirty bits (especially on PPC for
1147 if (g_str_equal(qtest_get_arch(), "ppc64") &&
1148 access("/sys/module/kvm_hv", F_OK)) {
1149 g_test_message("Skipping test: kvm_hv not available");
1150 return g_test_run();
1154 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
1155 * there until the problems are resolved
1157 if (g_str_equal(qtest_get_arch(), "s390x")) {
1158 #if defined(HOST_S390X)
1159 if (access("/dev/kvm", R_OK | W_OK)) {
1160 g_test_message("Skipping test: kvm not available");
1161 return g_test_run();
1164 g_test_message("Skipping test: Need s390x host to work properly");
1165 return g_test_run();
1169 tmpfs = mkdtemp(template);
1171 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno));
1175 module_call_init(MODULE_INIT_QOM);
1177 qtest_add_func("/migration/postcopy/unix", test_postcopy);
1178 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery);
1179 qtest_add_func("/migration/deprecated", test_deprecated);
1180 qtest_add_func("/migration/bad_dest", test_baddest);
1181 qtest_add_func("/migration/precopy/unix", test_precopy_unix);
1182 qtest_add_func("/migration/precopy/tcp", test_precopy_tcp);
1183 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
1184 qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix);
1185 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto);
1189 g_assert_cmpint(ret, ==, 0);
1193 g_test_message("unable to rmdir: path (%s): %s",
1194 tmpfs, strerror(errno));