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 "libqos/libqtest.h"
16 #include "qapi/error.h"
17 #include "qapi/qmp/qdict.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-helpers.h"
28 #include "tests/migration/migration-test.h"
30 #if defined(__linux__)
31 #include "linux/kvm.h"
34 /* TODO actually test the results and get rid of this */
35 #define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__))
37 unsigned start_address
;
39 static bool uffd_feature_thread_id
;
41 /* A downtime where the test really should converge */
42 #define CONVERGE_DOWNTIME 1000
44 #if defined(__linux__)
45 #include <sys/syscall.h>
49 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
50 #include <sys/eventfd.h>
51 #include <sys/ioctl.h>
52 #include <linux/userfaultfd.h>
54 static bool ufd_version_check(void)
56 struct uffdio_api api_struct
;
59 int ufd
= syscall(__NR_userfaultfd
, O_CLOEXEC
);
62 g_test_message("Skipping test: userfaultfd not available");
66 api_struct
.api
= UFFD_API
;
67 api_struct
.features
= 0;
68 if (ioctl(ufd
, UFFDIO_API
, &api_struct
)) {
69 g_test_message("Skipping test: UFFDIO_API failed");
72 uffd_feature_thread_id
= api_struct
.features
& UFFD_FEATURE_THREAD_ID
;
74 ioctl_mask
= (__u64
)1 << _UFFDIO_REGISTER
|
75 (__u64
)1 << _UFFDIO_UNREGISTER
;
76 if ((api_struct
.ioctls
& ioctl_mask
) != ioctl_mask
) {
77 g_test_message("Skipping test: Missing userfault feature");
85 static bool ufd_version_check(void)
87 g_test_message("Skipping test: Userfault not available (builtdtime)");
93 static const char *tmpfs
;
95 /* The boot file modifies memory area in [start_address, end_address)
96 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
98 #include "tests/migration/i386/a-b-bootblock.h"
99 #include "tests/migration/aarch64/a-b-kernel.h"
100 #include "tests/migration/s390x/a-b-bios.h"
102 static void init_bootfile(const char *bootpath
, void *content
, size_t len
)
104 FILE *bootfile
= fopen(bootpath
, "wb");
106 g_assert_cmpint(fwrite(content
, 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 g_autofree
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;
124 int readvalue
= fgetc(serialfile
);
127 /* SLOF prints its banner before starting test,
128 * to ignore it, mark the start of the test with '_',
129 * ignore all characters until this marker
136 fseek(serialfile
, 0, SEEK_SET
);
153 started
= (strcmp(side
, "src_serial") == 0 &&
154 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
155 fseek(serialfile
, 0, SEEK_SET
);
160 fprintf(stderr
, "Unexpected %d on %s serial\n", readvalue
, side
);
161 g_assert_not_reached();
167 * It's tricky to use qemu's migration event capability with qtest,
168 * events suddenly appearing confuse the qmp()/hmp() responses.
171 static int64_t read_ram_property_int(QTestState
*who
, const char *property
)
173 QDict
*rsp_return
, *rsp_ram
;
176 rsp_return
= migrate_query(who
);
177 if (!qdict_haskey(rsp_return
, "ram")) {
181 rsp_ram
= qdict_get_qdict(rsp_return
, "ram");
182 result
= qdict_get_try_int(rsp_ram
, property
, 0);
184 qobject_unref(rsp_return
);
188 static int64_t read_migrate_property_int(QTestState
*who
, const char *property
)
193 rsp_return
= migrate_query(who
);
194 result
= qdict_get_try_int(rsp_return
, property
, 0);
195 qobject_unref(rsp_return
);
199 static uint64_t get_migration_pass(QTestState
*who
)
201 return read_ram_property_int(who
, "dirty-sync-count");
204 static void read_blocktime(QTestState
*who
)
208 rsp_return
= migrate_query(who
);
209 g_assert(qdict_haskey(rsp_return
, "postcopy-blocktime"));
210 qobject_unref(rsp_return
);
213 static void wait_for_migration_pass(QTestState
*who
)
215 uint64_t initial_pass
= get_migration_pass(who
);
218 /* Wait for the 1st sync */
219 while (!got_stop
&& !initial_pass
) {
221 initial_pass
= get_migration_pass(who
);
226 pass
= get_migration_pass(who
);
227 } while (pass
== initial_pass
&& !got_stop
);
230 static void check_guests_ram(QTestState
*who
)
232 /* Our ASM test will have been incrementing one byte from each page from
233 * start_address to < end_address in order. This gives us a constraint
234 * that any page's byte should be equal or less than the previous pages
235 * byte (mod 256); and they should all be equal except for one transition
236 * at the point where we meet the incrementer. (We're running this with
237 * the guest stopped).
242 bool hit_edge
= false;
245 qtest_memread(who
, start_address
, &first_byte
, 1);
246 last_byte
= first_byte
;
248 for (address
= start_address
+ TEST_MEM_PAGE_SIZE
; address
< end_address
;
249 address
+= TEST_MEM_PAGE_SIZE
)
252 qtest_memread(who
, address
, &b
, 1);
253 if (b
!= last_byte
) {
254 if (((b
+ 1) % 256) == last_byte
&& !hit_edge
) {
255 /* This is OK, the guest stopped at the point of
256 * incrementing the previous page but didn't get
264 fprintf(stderr
, "Memory content inconsistency at %x"
265 " first_byte = %x last_byte = %x current = %x"
267 address
, first_byte
, last_byte
, b
, hit_edge
);
273 fprintf(stderr
, "and in another %d pages", bad
- 10);
278 static void cleanup(const char *filename
)
280 g_autofree
char *path
= g_strdup_printf("%s/%s", tmpfs
, filename
);
285 static char *SocketAddress_to_str(SocketAddress
*addr
)
287 switch (addr
->type
) {
288 case SOCKET_ADDRESS_TYPE_INET
:
289 return g_strdup_printf("tcp:%s:%s",
292 case SOCKET_ADDRESS_TYPE_UNIX
:
293 return g_strdup_printf("unix:%s",
294 addr
->u
.q_unix
.path
);
295 case SOCKET_ADDRESS_TYPE_FD
:
296 return g_strdup_printf("fd:%s", addr
->u
.fd
.str
);
297 case SOCKET_ADDRESS_TYPE_VSOCK
:
298 return g_strdup_printf("tcp:%s:%s",
302 return g_strdup("unknown address type");
306 static char *migrate_get_socket_address(QTestState
*who
, const char *parameter
)
310 SocketAddressList
*addrs
;
314 rsp
= migrate_query(who
);
315 object
= qdict_get(rsp
, parameter
);
317 iv
= qobject_input_visitor_new(object
);
318 visit_type_SocketAddressList(iv
, NULL
, &addrs
, &error_abort
);
321 /* we are only using a single address */
322 result
= SocketAddress_to_str(addrs
->value
);
324 qapi_free_SocketAddressList(addrs
);
329 static long long migrate_get_parameter_int(QTestState
*who
,
330 const char *parameter
)
335 rsp
= wait_command(who
, "{ 'execute': 'query-migrate-parameters' }");
336 result
= qdict_get_int(rsp
, parameter
);
341 static void migrate_check_parameter_int(QTestState
*who
, const char *parameter
,
346 result
= migrate_get_parameter_int(who
, parameter
);
347 g_assert_cmpint(result
, ==, value
);
350 static void migrate_set_parameter_int(QTestState
*who
, const char *parameter
,
356 "{ 'execute': 'migrate-set-parameters',"
357 "'arguments': { %s: %lld } }",
359 g_assert(qdict_haskey(rsp
, "return"));
361 migrate_check_parameter_int(who
, parameter
, value
);
364 static char *migrate_get_parameter_str(QTestState
*who
,
365 const char *parameter
)
370 rsp
= wait_command(who
, "{ 'execute': 'query-migrate-parameters' }");
371 result
= g_strdup(qdict_get_str(rsp
, parameter
));
376 static void migrate_check_parameter_str(QTestState
*who
, const char *parameter
,
379 g_autofree
char *result
= migrate_get_parameter_str(who
, parameter
);
380 g_assert_cmpstr(result
, ==, value
);
383 static void migrate_set_parameter_str(QTestState
*who
, const char *parameter
,
389 "{ 'execute': 'migrate-set-parameters',"
390 "'arguments': { %s: %s } }",
392 g_assert(qdict_haskey(rsp
, "return"));
394 migrate_check_parameter_str(who
, parameter
, value
);
397 static void migrate_pause(QTestState
*who
)
401 rsp
= wait_command(who
, "{ 'execute': 'migrate-pause' }");
405 static void migrate_continue(QTestState
*who
, const char *state
)
409 rsp
= wait_command(who
,
410 "{ 'execute': 'migrate-continue',"
411 " 'arguments': { 'state': %s } }",
416 static void migrate_recover(QTestState
*who
, const char *uri
)
420 rsp
= wait_command(who
,
421 "{ 'execute': 'migrate-recover', "
422 " 'id': 'recover-cmd', "
423 " 'arguments': { 'uri': %s } }",
428 static void migrate_cancel(QTestState
*who
)
432 rsp
= wait_command(who
, "{ 'execute': 'migrate_cancel' }");
436 static void migrate_set_capability(QTestState
*who
, const char *capability
,
442 "{ 'execute': 'migrate-set-capabilities',"
444 "'capabilities': [ { "
445 "'capability': %s, 'state': %i } ] } }",
447 g_assert(qdict_haskey(rsp
, "return"));
451 static void migrate_postcopy_start(QTestState
*from
, QTestState
*to
)
455 rsp
= wait_command(from
, "{ 'execute': 'migrate-start-postcopy' }");
459 qtest_qmp_eventwait(from
, "STOP");
462 qtest_qmp_eventwait(to
, "RESUME");
467 * QTEST_LOG=1 may override this. When QTEST_LOG=1, we always dump errors
468 * unconditionally, because it means the user would like to be verbose.
472 /* only launch the target process */
474 /* Use dirty ring if true; dirty logging otherwise */
480 static MigrateStart
*migrate_start_new(void)
482 MigrateStart
*args
= g_new0(MigrateStart
, 1);
484 args
->opts_source
= g_strdup("");
485 args
->opts_target
= g_strdup("");
489 static void migrate_start_destroy(MigrateStart
*args
)
491 g_free(args
->opts_source
);
492 g_free(args
->opts_target
);
496 static int test_migrate_start(QTestState
**from
, QTestState
**to
,
497 const char *uri
, MigrateStart
*args
)
499 g_autofree gchar
*arch_source
= NULL
;
500 g_autofree gchar
*arch_target
= NULL
;
501 g_autofree gchar
*cmd_source
= NULL
;
502 g_autofree gchar
*cmd_target
= NULL
;
503 const gchar
*ignore_stderr
;
504 g_autofree
char *bootpath
= NULL
;
505 g_autofree
char *shmem_opts
= NULL
;
506 g_autofree
char *shmem_path
= NULL
;
507 const char *arch
= qtest_get_arch();
508 const char *machine_opts
= NULL
;
509 const char *memory_size
;
512 if (args
->use_shmem
) {
513 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR
)) {
514 g_test_skip("/dev/shm is not supported");
521 bootpath
= g_strdup_printf("%s/bootsect", tmpfs
);
522 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
523 /* the assembled x86 boot sector should be exactly one sector large */
524 assert(sizeof(x86_bootsect
) == 512);
525 init_bootfile(bootpath
, x86_bootsect
, sizeof(x86_bootsect
));
526 memory_size
= "150M";
527 arch_source
= g_strdup_printf("-drive file=%s,format=raw", bootpath
);
528 arch_target
= g_strdup(arch_source
);
529 start_address
= X86_TEST_MEM_START
;
530 end_address
= X86_TEST_MEM_END
;
531 } else if (g_str_equal(arch
, "s390x")) {
532 init_bootfile(bootpath
, s390x_elf
, sizeof(s390x_elf
));
533 memory_size
= "128M";
534 arch_source
= g_strdup_printf("-bios %s", bootpath
);
535 arch_target
= g_strdup(arch_source
);
536 start_address
= S390_TEST_MEM_START
;
537 end_address
= S390_TEST_MEM_END
;
538 } else if (strcmp(arch
, "ppc64") == 0) {
539 machine_opts
= "vsmt=8";
540 memory_size
= "256M";
541 start_address
= PPC_TEST_MEM_START
;
542 end_address
= PPC_TEST_MEM_END
;
543 arch_source
= g_strdup_printf("-nodefaults "
544 "-prom-env 'use-nvramrc?=true' -prom-env "
545 "'nvramrc=hex .\" _\" begin %x %x "
546 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
547 "until'", end_address
, start_address
);
548 arch_target
= g_strdup("");
549 } else if (strcmp(arch
, "aarch64") == 0) {
550 init_bootfile(bootpath
, aarch64_kernel
, sizeof(aarch64_kernel
));
551 machine_opts
= "virt,gic-version=max";
552 memory_size
= "150M";
553 arch_source
= g_strdup_printf("-cpu max "
556 arch_target
= g_strdup(arch_source
);
557 start_address
= ARM_TEST_MEM_START
;
558 end_address
= ARM_TEST_MEM_END
;
560 g_assert(sizeof(aarch64_kernel
) <= ARM_TEST_MAX_KERNEL_SIZE
);
562 g_assert_not_reached();
565 if (!getenv("QTEST_LOG") && args
->hide_stderr
) {
566 ignore_stderr
= "2>/dev/null";
571 if (args
->use_shmem
) {
572 shmem_path
= g_strdup_printf("/dev/shm/qemu-%d", getpid());
573 shmem_opts
= g_strdup_printf(
574 "-object memory-backend-file,id=mem0,size=%s"
575 ",mem-path=%s,share=on -numa node,memdev=mem0",
576 memory_size
, shmem_path
);
579 shmem_opts
= g_strdup("");
582 cmd_source
= g_strdup_printf("-accel kvm%s -accel tcg%s%s "
583 "-name source,debug-threads=on "
585 "-serial file:%s/src_serial "
587 args
->use_dirty_ring
?
588 ",dirty-ring-size=4096" : "",
589 machine_opts
? " -machine " : "",
590 machine_opts
? machine_opts
: "",
592 arch_source
, shmem_opts
, args
->opts_source
,
594 if (!args
->only_target
) {
595 *from
= qtest_init(cmd_source
);
598 cmd_target
= g_strdup_printf("-accel kvm%s -accel tcg%s%s "
599 "-name target,debug-threads=on "
601 "-serial file:%s/dest_serial "
604 args
->use_dirty_ring
?
605 ",dirty-ring-size=4096" : "",
606 machine_opts
? " -machine " : "",
607 machine_opts
? machine_opts
: "",
608 memory_size
, tmpfs
, uri
,
609 arch_target
, shmem_opts
,
610 args
->opts_target
, ignore_stderr
);
611 *to
= qtest_init(cmd_target
);
614 * Remove shmem file immediately to avoid memory leak in test failed case.
615 * It's valid becase QEMU has already opened this file
617 if (args
->use_shmem
) {
622 migrate_start_destroy(args
);
626 static void test_migrate_end(QTestState
*from
, QTestState
*to
, bool test_dest
)
628 unsigned char dest_byte_a
, dest_byte_b
, dest_byte_c
, dest_byte_d
;
633 qtest_memread(to
, start_address
, &dest_byte_a
, 1);
635 /* Destination still running, wait for a byte to change */
637 qtest_memread(to
, start_address
, &dest_byte_b
, 1);
639 } while (dest_byte_a
== dest_byte_b
);
641 qtest_qmp_discard_response(to
, "{ 'execute' : 'stop'}");
643 /* With it stopped, check nothing changes */
644 qtest_memread(to
, start_address
, &dest_byte_c
, 1);
646 qtest_memread(to
, start_address
, &dest_byte_d
, 1);
647 g_assert_cmpint(dest_byte_c
, ==, dest_byte_d
);
649 check_guests_ram(to
);
655 cleanup("migsocket");
656 cleanup("src_serial");
657 cleanup("dest_serial");
660 static int migrate_postcopy_prepare(QTestState
**from_ptr
,
664 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
665 QTestState
*from
, *to
;
667 if (test_migrate_start(&from
, &to
, uri
, args
)) {
671 migrate_set_capability(from
, "postcopy-ram", true);
672 migrate_set_capability(to
, "postcopy-ram", true);
673 migrate_set_capability(to
, "postcopy-blocktime", true);
675 /* We want to pick a speed slow enough that the test completes
676 * quickly, but that it doesn't complete precopy even on a slow
677 * machine, so also set the downtime.
679 migrate_set_parameter_int(from
, "max-bandwidth", 30000000);
680 migrate_set_parameter_int(from
, "downtime-limit", 1);
682 /* Wait for the first serial output from the source */
683 wait_for_serial("src_serial");
685 migrate_qmp(from
, uri
, "{}");
687 wait_for_migration_pass(from
);
695 static void migrate_postcopy_complete(QTestState
*from
, QTestState
*to
)
697 wait_for_migration_complete(from
);
699 /* Make sure we get at least one "B" on destination */
700 wait_for_serial("dest_serial");
702 if (uffd_feature_thread_id
) {
706 test_migrate_end(from
, to
, true);
709 static void test_postcopy(void)
711 MigrateStart
*args
= migrate_start_new();
712 QTestState
*from
, *to
;
714 if (migrate_postcopy_prepare(&from
, &to
, args
)) {
717 migrate_postcopy_start(from
, to
);
718 migrate_postcopy_complete(from
, to
);
721 static void test_postcopy_recovery(void)
723 MigrateStart
*args
= migrate_start_new();
724 QTestState
*from
, *to
;
725 g_autofree
char *uri
= NULL
;
727 args
->hide_stderr
= true;
729 if (migrate_postcopy_prepare(&from
, &to
, args
)) {
733 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
734 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 4096);
736 /* Now we start the postcopy */
737 migrate_postcopy_start(from
, to
);
740 * Wait until postcopy is really started; we can only run the
741 * migrate-pause command during a postcopy
743 wait_for_migration_status(from
, "postcopy-active", NULL
);
746 * Manually stop the postcopy migration. This emulates a network
747 * failure with the migration socket
752 * Wait for destination side to reach postcopy-paused state. The
753 * migrate-recover command can only succeed if destination machine
754 * is in the paused state
756 wait_for_migration_status(to
, "postcopy-paused",
757 (const char * []) { "failed", "active",
758 "completed", NULL
});
761 * Create a new socket to emulate a new channel that is different
762 * from the broken migration channel; tell the destination to
763 * listen to the new port
765 uri
= g_strdup_printf("unix:%s/migsocket-recover", tmpfs
);
766 migrate_recover(to
, uri
);
769 * Try to rebuild the migration channel using the resume flag and
770 * the newly created channel
772 wait_for_migration_status(from
, "postcopy-paused",
773 (const char * []) { "failed", "active",
774 "completed", NULL
});
775 migrate_qmp(from
, uri
, "{'resume': true}");
777 /* Restore the postcopy bandwidth to unlimited */
778 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 0);
780 migrate_postcopy_complete(from
, to
);
783 static void test_baddest(void)
785 MigrateStart
*args
= migrate_start_new();
786 QTestState
*from
, *to
;
788 args
->hide_stderr
= true;
790 if (test_migrate_start(&from
, &to
, "tcp:0:0", args
)) {
793 migrate_qmp(from
, "tcp:0:0", "{}");
794 wait_for_migration_fail(from
, false);
795 test_migrate_end(from
, to
, false);
798 static void test_precopy_unix_common(bool dirty_ring
)
800 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
801 MigrateStart
*args
= migrate_start_new();
802 QTestState
*from
, *to
;
804 args
->use_dirty_ring
= dirty_ring
;
806 if (test_migrate_start(&from
, &to
, uri
, args
)) {
810 /* We want to pick a speed slow enough that the test completes
811 * quickly, but that it doesn't complete precopy even on a slow
812 * machine, so also set the downtime.
814 /* 1 ms should make it not converge*/
815 migrate_set_parameter_int(from
, "downtime-limit", 1);
817 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
819 /* Wait for the first serial output from the source */
820 wait_for_serial("src_serial");
822 migrate_qmp(from
, uri
, "{}");
824 wait_for_migration_pass(from
);
826 migrate_set_parameter_int(from
, "downtime-limit", CONVERGE_DOWNTIME
);
829 qtest_qmp_eventwait(from
, "STOP");
832 qtest_qmp_eventwait(to
, "RESUME");
834 wait_for_serial("dest_serial");
835 wait_for_migration_complete(from
);
837 test_migrate_end(from
, to
, true);
840 static void test_precopy_unix(void)
842 /* Using default dirty logging */
843 test_precopy_unix_common(false);
846 static void test_precopy_unix_dirty_ring(void)
848 /* Using dirty ring tracking */
849 test_precopy_unix_common(true);
853 /* Currently upset on aarch64 TCG */
854 static void test_ignore_shared(void)
856 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
857 QTestState
*from
, *to
;
859 if (test_migrate_start(&from
, &to
, uri
, false, true, NULL
, NULL
)) {
863 migrate_set_capability(from
, "x-ignore-shared", true);
864 migrate_set_capability(to
, "x-ignore-shared", true);
866 /* Wait for the first serial output from the source */
867 wait_for_serial("src_serial");
869 migrate_qmp(from
, uri
, "{}");
871 wait_for_migration_pass(from
);
874 qtest_qmp_eventwait(from
, "STOP");
877 qtest_qmp_eventwait(to
, "RESUME");
879 wait_for_serial("dest_serial");
880 wait_for_migration_complete(from
);
882 /* Check whether shared RAM has been really skipped */
883 g_assert_cmpint(read_ram_property_int(from
, "transferred"), <, 1024 * 1024);
885 test_migrate_end(from
, to
, true);
889 static void test_xbzrle(const char *uri
)
891 MigrateStart
*args
= migrate_start_new();
892 QTestState
*from
, *to
;
894 if (test_migrate_start(&from
, &to
, uri
, args
)) {
899 * We want to pick a speed slow enough that the test completes
900 * quickly, but that it doesn't complete precopy even on a slow
901 * machine, so also set the downtime.
903 /* 1 ms should make it not converge*/
904 migrate_set_parameter_int(from
, "downtime-limit", 1);
906 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
908 migrate_set_parameter_int(from
, "xbzrle-cache-size", 33554432);
910 migrate_set_capability(from
, "xbzrle", true);
911 migrate_set_capability(to
, "xbzrle", true);
912 /* Wait for the first serial output from the source */
913 wait_for_serial("src_serial");
915 migrate_qmp(from
, uri
, "{}");
917 wait_for_migration_pass(from
);
918 /* Make sure we have 2 passes, so the xbzrle cache gets a workout */
919 wait_for_migration_pass(from
);
921 /* 1000ms should converge */
922 migrate_set_parameter_int(from
, "downtime-limit", 1000);
925 qtest_qmp_eventwait(from
, "STOP");
927 qtest_qmp_eventwait(to
, "RESUME");
929 wait_for_serial("dest_serial");
930 wait_for_migration_complete(from
);
932 test_migrate_end(from
, to
, true);
935 static void test_xbzrle_unix(void)
937 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
942 static void test_precopy_tcp(void)
944 MigrateStart
*args
= migrate_start_new();
945 g_autofree
char *uri
= NULL
;
946 QTestState
*from
, *to
;
948 if (test_migrate_start(&from
, &to
, "tcp:127.0.0.1:0", args
)) {
953 * We want to pick a speed slow enough that the test completes
954 * quickly, but that it doesn't complete precopy even on a slow
955 * machine, so also set the downtime.
957 /* 1 ms should make it not converge*/
958 migrate_set_parameter_int(from
, "downtime-limit", 1);
960 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
962 /* Wait for the first serial output from the source */
963 wait_for_serial("src_serial");
965 uri
= migrate_get_socket_address(to
, "socket-address");
967 migrate_qmp(from
, uri
, "{}");
969 wait_for_migration_pass(from
);
971 migrate_set_parameter_int(from
, "downtime-limit", CONVERGE_DOWNTIME
);
974 qtest_qmp_eventwait(from
, "STOP");
976 qtest_qmp_eventwait(to
, "RESUME");
978 wait_for_serial("dest_serial");
979 wait_for_migration_complete(from
);
981 test_migrate_end(from
, to
, true);
984 static void test_migrate_fd_proto(void)
986 MigrateStart
*args
= migrate_start_new();
987 QTestState
*from
, *to
;
991 const char *error_desc
;
993 if (test_migrate_start(&from
, &to
, "defer", args
)) {
998 * We want to pick a speed slow enough that the test completes
999 * quickly, but that it doesn't complete precopy even on a slow
1000 * machine, so also set the downtime.
1002 /* 1 ms should make it not converge */
1003 migrate_set_parameter_int(from
, "downtime-limit", 1);
1005 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
1007 /* Wait for the first serial output from the source */
1008 wait_for_serial("src_serial");
1010 /* Create two connected sockets for migration */
1011 ret
= socketpair(PF_LOCAL
, SOCK_STREAM
, 0, pair
);
1012 g_assert_cmpint(ret
, ==, 0);
1014 /* Send the 1st socket to the target */
1015 rsp
= wait_command_fd(to
, pair
[0],
1016 "{ 'execute': 'getfd',"
1017 " 'arguments': { 'fdname': 'fd-mig' }}");
1021 /* Start incoming migration from the 1st socket */
1022 rsp
= wait_command(to
, "{ 'execute': 'migrate-incoming',"
1023 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1026 /* Send the 2nd socket to the target */
1027 rsp
= wait_command_fd(from
, pair
[1],
1028 "{ 'execute': 'getfd',"
1029 " 'arguments': { 'fdname': 'fd-mig' }}");
1033 /* Start migration to the 2nd socket*/
1034 migrate_qmp(from
, "fd:fd-mig", "{}");
1036 wait_for_migration_pass(from
);
1038 migrate_set_parameter_int(from
, "downtime-limit", CONVERGE_DOWNTIME
);
1041 qtest_qmp_eventwait(from
, "STOP");
1043 qtest_qmp_eventwait(to
, "RESUME");
1045 /* Test closing fds */
1046 /* We assume, that QEMU removes named fd from its list,
1047 * so this should fail */
1048 rsp
= qtest_qmp(from
, "{ 'execute': 'closefd',"
1049 " 'arguments': { 'fdname': 'fd-mig' }}");
1050 g_assert_true(qdict_haskey(rsp
, "error"));
1051 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
1052 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
1055 rsp
= qtest_qmp(to
, "{ 'execute': 'closefd',"
1056 " 'arguments': { 'fdname': 'fd-mig' }}");
1057 g_assert_true(qdict_haskey(rsp
, "error"));
1058 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
1059 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
1062 /* Complete migration */
1063 wait_for_serial("dest_serial");
1064 wait_for_migration_complete(from
);
1065 test_migrate_end(from
, to
, true);
1068 static void do_test_validate_uuid(MigrateStart
*args
, bool should_fail
)
1070 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1071 QTestState
*from
, *to
;
1073 if (test_migrate_start(&from
, &to
, uri
, args
)) {
1078 * UUID validation is at the begin of migration. So, the main process of
1079 * migration is not interesting for us here. Thus, set huge downtime for
1080 * very fast migration.
1082 migrate_set_parameter_int(from
, "downtime-limit", 1000000);
1083 migrate_set_capability(from
, "validate-uuid", true);
1085 /* Wait for the first serial output from the source */
1086 wait_for_serial("src_serial");
1088 migrate_qmp(from
, uri
, "{}");
1091 qtest_set_expected_status(to
, 1);
1092 wait_for_migration_fail(from
, true);
1094 wait_for_migration_complete(from
);
1097 test_migrate_end(from
, to
, false);
1100 static void test_validate_uuid(void)
1102 MigrateStart
*args
= migrate_start_new();
1104 g_free(args
->opts_source
);
1105 g_free(args
->opts_target
);
1106 args
->opts_source
= g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1107 args
->opts_target
= g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1108 do_test_validate_uuid(args
, false);
1111 static void test_validate_uuid_error(void)
1113 MigrateStart
*args
= migrate_start_new();
1115 g_free(args
->opts_source
);
1116 g_free(args
->opts_target
);
1117 args
->opts_source
= g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1118 args
->opts_target
= g_strdup("-uuid 22222222-2222-2222-2222-222222222222");
1119 args
->hide_stderr
= true;
1120 do_test_validate_uuid(args
, true);
1123 static void test_validate_uuid_src_not_set(void)
1125 MigrateStart
*args
= migrate_start_new();
1127 g_free(args
->opts_target
);
1128 args
->opts_target
= g_strdup("-uuid 22222222-2222-2222-2222-222222222222");
1129 args
->hide_stderr
= true;
1130 do_test_validate_uuid(args
, false);
1133 static void test_validate_uuid_dst_not_set(void)
1135 MigrateStart
*args
= migrate_start_new();
1137 g_free(args
->opts_source
);
1138 args
->opts_source
= g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1139 args
->hide_stderr
= true;
1140 do_test_validate_uuid(args
, false);
1143 static void test_migrate_auto_converge(void)
1145 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1146 MigrateStart
*args
= migrate_start_new();
1147 QTestState
*from
, *to
;
1148 int64_t remaining
, percentage
;
1151 * We want the test to be stable and as fast as possible.
1152 * E.g., with 1Gb/s bandwith migration may pass without throttling,
1153 * so we need to decrease a bandwidth.
1155 const int64_t init_pct
= 5, inc_pct
= 50, max_pct
= 95;
1156 const int64_t max_bandwidth
= 400000000; /* ~400Mb/s */
1157 const int64_t downtime_limit
= 250; /* 250ms */
1159 * We migrate through unix-socket (> 500Mb/s).
1160 * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
1161 * So, we can predict expected_threshold
1163 const int64_t expected_threshold
= max_bandwidth
* downtime_limit
/ 1000;
1165 if (test_migrate_start(&from
, &to
, uri
, args
)) {
1169 migrate_set_capability(from
, "auto-converge", true);
1170 migrate_set_parameter_int(from
, "cpu-throttle-initial", init_pct
);
1171 migrate_set_parameter_int(from
, "cpu-throttle-increment", inc_pct
);
1172 migrate_set_parameter_int(from
, "max-cpu-throttle", max_pct
);
1175 * Set the initial parameters so that the migration could not converge
1176 * without throttling.
1178 migrate_set_parameter_int(from
, "downtime-limit", 1);
1179 migrate_set_parameter_int(from
, "max-bandwidth", 100000000); /* ~100Mb/s */
1181 /* To check remaining size after precopy */
1182 migrate_set_capability(from
, "pause-before-switchover", true);
1184 /* Wait for the first serial output from the source */
1185 wait_for_serial("src_serial");
1187 migrate_qmp(from
, uri
, "{}");
1189 /* Wait for throttling begins */
1191 while (percentage
== 0) {
1192 percentage
= read_migrate_property_int(from
, "cpu-throttle-percentage");
1194 g_assert_false(got_stop
);
1196 /* The first percentage of throttling should be equal to init_pct */
1197 g_assert_cmpint(percentage
, ==, init_pct
);
1198 /* Now, when we tested that throttling works, let it converge */
1199 migrate_set_parameter_int(from
, "downtime-limit", downtime_limit
);
1200 migrate_set_parameter_int(from
, "max-bandwidth", max_bandwidth
);
1203 * Wait for pre-switchover status to check last throttle percentage
1204 * and remaining. These values will be zeroed later
1206 wait_for_migration_status(from
, "pre-switchover", NULL
);
1208 /* The final percentage of throttling shouldn't be greater than max_pct */
1209 percentage
= read_migrate_property_int(from
, "cpu-throttle-percentage");
1210 g_assert_cmpint(percentage
, <=, max_pct
);
1212 remaining
= read_ram_property_int(from
, "remaining");
1213 g_assert_cmpint(remaining
, <,
1214 (expected_threshold
+ expected_threshold
/ 100));
1216 migrate_continue(from
, "pre-switchover");
1218 qtest_qmp_eventwait(to
, "RESUME");
1220 wait_for_serial("dest_serial");
1221 wait_for_migration_complete(from
);
1224 test_migrate_end(from
, to
, true);
1227 static void test_multifd_tcp(const char *method
)
1229 MigrateStart
*args
= migrate_start_new();
1230 QTestState
*from
, *to
;
1232 g_autofree
char *uri
= NULL
;
1234 if (test_migrate_start(&from
, &to
, "defer", args
)) {
1239 * We want to pick a speed slow enough that the test completes
1240 * quickly, but that it doesn't complete precopy even on a slow
1241 * machine, so also set the downtime.
1243 /* 1 ms should make it not converge*/
1244 migrate_set_parameter_int(from
, "downtime-limit", 1);
1246 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
1248 migrate_set_parameter_int(from
, "multifd-channels", 16);
1249 migrate_set_parameter_int(to
, "multifd-channels", 16);
1251 migrate_set_parameter_str(from
, "multifd-compression", method
);
1252 migrate_set_parameter_str(to
, "multifd-compression", method
);
1254 migrate_set_capability(from
, "multifd", true);
1255 migrate_set_capability(to
, "multifd", true);
1257 /* Start incoming migration from the 1st socket */
1258 rsp
= wait_command(to
, "{ 'execute': 'migrate-incoming',"
1259 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1262 /* Wait for the first serial output from the source */
1263 wait_for_serial("src_serial");
1265 uri
= migrate_get_socket_address(to
, "socket-address");
1267 migrate_qmp(from
, uri
, "{}");
1269 wait_for_migration_pass(from
);
1271 migrate_set_parameter_int(from
, "downtime-limit", CONVERGE_DOWNTIME
);
1274 qtest_qmp_eventwait(from
, "STOP");
1276 qtest_qmp_eventwait(to
, "RESUME");
1278 wait_for_serial("dest_serial");
1279 wait_for_migration_complete(from
);
1280 test_migrate_end(from
, to
, true);
1283 static void test_multifd_tcp_none(void)
1285 test_multifd_tcp("none");
1288 static void test_multifd_tcp_zlib(void)
1290 test_multifd_tcp("zlib");
1294 static void test_multifd_tcp_zstd(void)
1296 test_multifd_tcp("zstd");
1306 * launch another target
1309 * And see that it works
1311 static void test_multifd_tcp_cancel(void)
1313 MigrateStart
*args
= migrate_start_new();
1314 QTestState
*from
, *to
, *to2
;
1316 g_autofree
char *uri
= NULL
;
1318 args
->hide_stderr
= true;
1320 if (test_migrate_start(&from
, &to
, "defer", args
)) {
1325 * We want to pick a speed slow enough that the test completes
1326 * quickly, but that it doesn't complete precopy even on a slow
1327 * machine, so also set the downtime.
1329 /* 1 ms should make it not converge*/
1330 migrate_set_parameter_int(from
, "downtime-limit", 1);
1332 migrate_set_parameter_int(from
, "max-bandwidth", 30000000);
1334 migrate_set_parameter_int(from
, "multifd-channels", 16);
1335 migrate_set_parameter_int(to
, "multifd-channels", 16);
1337 migrate_set_capability(from
, "multifd", true);
1338 migrate_set_capability(to
, "multifd", true);
1340 /* Start incoming migration from the 1st socket */
1341 rsp
= wait_command(to
, "{ 'execute': 'migrate-incoming',"
1342 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1345 /* Wait for the first serial output from the source */
1346 wait_for_serial("src_serial");
1348 uri
= migrate_get_socket_address(to
, "socket-address");
1350 migrate_qmp(from
, uri
, "{}");
1352 wait_for_migration_pass(from
);
1354 migrate_cancel(from
);
1356 args
= migrate_start_new();
1357 args
->only_target
= true;
1359 if (test_migrate_start(&from
, &to2
, "defer", args
)) {
1363 migrate_set_parameter_int(to2
, "multifd-channels", 16);
1365 migrate_set_capability(to2
, "multifd", true);
1367 /* Start incoming migration from the 1st socket */
1368 rsp
= wait_command(to2
, "{ 'execute': 'migrate-incoming',"
1369 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1373 uri
= migrate_get_socket_address(to2
, "socket-address");
1375 wait_for_migration_status(from
, "cancelled", NULL
);
1377 /* 300ms it should converge */
1378 migrate_set_parameter_int(from
, "downtime-limit", 300);
1380 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
1382 migrate_qmp(from
, uri
, "{}");
1384 wait_for_migration_pass(from
);
1387 qtest_qmp_eventwait(from
, "STOP");
1389 qtest_qmp_eventwait(to2
, "RESUME");
1391 wait_for_serial("dest_serial");
1392 wait_for_migration_complete(from
);
1393 test_migrate_end(from
, to2
, true);
1396 static bool kvm_dirty_ring_supported(void)
1398 #if defined(__linux__)
1399 int ret
, kvm_fd
= open("/dev/kvm", O_RDONLY
);
1405 ret
= ioctl(kvm_fd
, KVM_CHECK_EXTENSION
, KVM_CAP_DIRTY_LOG_RING
);
1408 /* We test with 4096 slots */
1419 int main(int argc
, char **argv
)
1421 char template[] = "/tmp/migration-test-XXXXXX";
1424 g_test_init(&argc
, &argv
, NULL
);
1426 if (!ufd_version_check()) {
1427 return g_test_run();
1431 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
1432 * is touchy due to race conditions on dirty bits (especially on PPC for
1435 if (g_str_equal(qtest_get_arch(), "ppc64") &&
1436 (access("/sys/module/kvm_hv", F_OK
) ||
1437 access("/dev/kvm", R_OK
| W_OK
))) {
1438 g_test_message("Skipping test: kvm_hv not available");
1439 return g_test_run();
1443 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
1444 * there until the problems are resolved
1446 if (g_str_equal(qtest_get_arch(), "s390x")) {
1447 #if defined(HOST_S390X)
1448 if (access("/dev/kvm", R_OK
| W_OK
)) {
1449 g_test_message("Skipping test: kvm not available");
1450 return g_test_run();
1453 g_test_message("Skipping test: Need s390x host to work properly");
1454 return g_test_run();
1458 tmpfs
= mkdtemp(template);
1460 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno
));
1464 module_call_init(MODULE_INIT_QOM
);
1466 qtest_add_func("/migration/postcopy/unix", test_postcopy
);
1467 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery
);
1468 qtest_add_func("/migration/bad_dest", test_baddest
);
1469 qtest_add_func("/migration/precopy/unix", test_precopy_unix
);
1470 qtest_add_func("/migration/precopy/tcp", test_precopy_tcp
);
1471 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
1472 qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix
);
1473 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto
);
1474 qtest_add_func("/migration/validate_uuid", test_validate_uuid
);
1475 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error
);
1476 qtest_add_func("/migration/validate_uuid_src_not_set",
1477 test_validate_uuid_src_not_set
);
1478 qtest_add_func("/migration/validate_uuid_dst_not_set",
1479 test_validate_uuid_dst_not_set
);
1481 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge
);
1482 qtest_add_func("/migration/multifd/tcp/none", test_multifd_tcp_none
);
1483 qtest_add_func("/migration/multifd/tcp/cancel", test_multifd_tcp_cancel
);
1484 qtest_add_func("/migration/multifd/tcp/zlib", test_multifd_tcp_zlib
);
1486 qtest_add_func("/migration/multifd/tcp/zstd", test_multifd_tcp_zstd
);
1489 if (kvm_dirty_ring_supported()) {
1490 qtest_add_func("/migration/dirty_ring",
1491 test_precopy_unix_dirty_ring
);
1496 g_assert_cmpint(ret
, ==, 0);
1500 g_test_message("unable to rmdir: path (%s): %s",
1501 tmpfs
, strerror(errno
));