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/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 "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
;
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();
161 * It's tricky to use qemu's migration event capability with qtest,
162 * events suddenly appearing confuse the qmp()/hmp() responses.
165 static int64_t read_ram_property_int(QTestState
*who
, const char *property
)
167 QDict
*rsp_return
, *rsp_ram
;
170 rsp_return
= migrate_query(who
);
171 if (!qdict_haskey(rsp_return
, "ram")) {
175 rsp_ram
= qdict_get_qdict(rsp_return
, "ram");
176 result
= qdict_get_try_int(rsp_ram
, property
, 0);
178 qobject_unref(rsp_return
);
182 static int64_t read_migrate_property_int(QTestState
*who
, const char *property
)
187 rsp_return
= migrate_query(who
);
188 result
= qdict_get_try_int(rsp_return
, property
, 0);
189 qobject_unref(rsp_return
);
193 static uint64_t get_migration_pass(QTestState
*who
)
195 return read_ram_property_int(who
, "dirty-sync-count");
198 static void read_blocktime(QTestState
*who
)
202 rsp_return
= migrate_query(who
);
203 g_assert(qdict_haskey(rsp_return
, "postcopy-blocktime"));
204 qobject_unref(rsp_return
);
207 static void wait_for_migration_pass(QTestState
*who
)
209 uint64_t initial_pass
= get_migration_pass(who
);
212 /* Wait for the 1st sync */
213 while (!got_stop
&& !initial_pass
) {
215 initial_pass
= get_migration_pass(who
);
220 pass
= get_migration_pass(who
);
221 } while (pass
== initial_pass
&& !got_stop
);
224 static void check_guests_ram(QTestState
*who
)
226 /* Our ASM test will have been incrementing one byte from each page from
227 * start_address to < end_address in order. This gives us a constraint
228 * that any page's byte should be equal or less than the previous pages
229 * byte (mod 256); and they should all be equal except for one transition
230 * at the point where we meet the incrementer. (We're running this with
231 * the guest stopped).
236 bool hit_edge
= false;
239 qtest_memread(who
, start_address
, &first_byte
, 1);
240 last_byte
= first_byte
;
242 for (address
= start_address
+ TEST_MEM_PAGE_SIZE
; address
< end_address
;
243 address
+= TEST_MEM_PAGE_SIZE
)
246 qtest_memread(who
, address
, &b
, 1);
247 if (b
!= last_byte
) {
248 if (((b
+ 1) % 256) == last_byte
&& !hit_edge
) {
249 /* This is OK, the guest stopped at the point of
250 * incrementing the previous page but didn't get
258 fprintf(stderr
, "Memory content inconsistency at %x"
259 " first_byte = %x last_byte = %x current = %x"
261 address
, first_byte
, last_byte
, b
, hit_edge
);
267 fprintf(stderr
, "and in another %d pages", bad
- 10);
272 static void cleanup(const char *filename
)
274 char *path
= g_strdup_printf("%s/%s", tmpfs
, filename
);
280 static char *SocketAddress_to_str(SocketAddress
*addr
)
282 switch (addr
->type
) {
283 case SOCKET_ADDRESS_TYPE_INET
:
284 return g_strdup_printf("tcp:%s:%s",
287 case SOCKET_ADDRESS_TYPE_UNIX
:
288 return g_strdup_printf("unix:%s",
289 addr
->u
.q_unix
.path
);
290 case SOCKET_ADDRESS_TYPE_FD
:
291 return g_strdup_printf("fd:%s", addr
->u
.fd
.str
);
292 case SOCKET_ADDRESS_TYPE_VSOCK
:
293 return g_strdup_printf("tcp:%s:%s",
297 return g_strdup("unknown address type");
301 static char *migrate_get_socket_address(QTestState
*who
, const char *parameter
)
305 SocketAddressList
*addrs
;
309 rsp
= migrate_query(who
);
310 object
= qdict_get(rsp
, parameter
);
312 iv
= qobject_input_visitor_new(object
);
313 visit_type_SocketAddressList(iv
, NULL
, &addrs
, &error_abort
);
316 /* we are only using a single address */
317 result
= SocketAddress_to_str(addrs
->value
);
319 qapi_free_SocketAddressList(addrs
);
324 static long long migrate_get_parameter_int(QTestState
*who
,
325 const char *parameter
)
330 rsp
= wait_command(who
, "{ 'execute': 'query-migrate-parameters' }");
331 result
= qdict_get_int(rsp
, parameter
);
336 static void migrate_check_parameter_int(QTestState
*who
, const char *parameter
,
341 result
= migrate_get_parameter_int(who
, parameter
);
342 g_assert_cmpint(result
, ==, value
);
345 static void migrate_set_parameter_int(QTestState
*who
, const char *parameter
,
351 "{ 'execute': 'migrate-set-parameters',"
352 "'arguments': { %s: %lld } }",
354 g_assert(qdict_haskey(rsp
, "return"));
356 migrate_check_parameter_int(who
, parameter
, value
);
359 static char *migrate_get_parameter_str(QTestState
*who
,
360 const char *parameter
)
365 rsp
= wait_command(who
, "{ 'execute': 'query-migrate-parameters' }");
366 result
= g_strdup(qdict_get_str(rsp
, parameter
));
371 static void migrate_check_parameter_str(QTestState
*who
, const char *parameter
,
376 result
= migrate_get_parameter_str(who
, parameter
);
377 g_assert_cmpstr(result
, ==, value
);
381 static void migrate_set_parameter_str(QTestState
*who
, const char *parameter
,
387 "{ 'execute': 'migrate-set-parameters',"
388 "'arguments': { %s: %s } }",
390 g_assert(qdict_haskey(rsp
, "return"));
392 migrate_check_parameter_str(who
, parameter
, value
);
395 static void migrate_pause(QTestState
*who
)
399 rsp
= wait_command(who
, "{ 'execute': 'migrate-pause' }");
403 static void migrate_continue(QTestState
*who
, const char *state
)
407 rsp
= wait_command(who
,
408 "{ 'execute': 'migrate-continue',"
409 " 'arguments': { 'state': %s } }",
414 static void migrate_recover(QTestState
*who
, const char *uri
)
418 rsp
= wait_command(who
,
419 "{ 'execute': 'migrate-recover', "
420 " 'id': 'recover-cmd', "
421 " 'arguments': { 'uri': %s } }",
426 static void migrate_cancel(QTestState
*who
)
430 rsp
= wait_command(who
, "{ 'execute': 'migrate_cancel' }");
434 static void migrate_set_capability(QTestState
*who
, const char *capability
,
440 "{ 'execute': 'migrate-set-capabilities',"
442 "'capabilities': [ { "
443 "'capability': %s, 'state': %i } ] } }",
445 g_assert(qdict_haskey(rsp
, "return"));
449 static void migrate_postcopy_start(QTestState
*from
, QTestState
*to
)
453 rsp
= wait_command(from
, "{ 'execute': 'migrate-start-postcopy' }");
457 qtest_qmp_eventwait(from
, "STOP");
460 qtest_qmp_eventwait(to
, "RESUME");
466 /* only launch the target process */
472 static MigrateStart
*migrate_start_new(void)
474 MigrateStart
*args
= g_new0(MigrateStart
, 1);
476 args
->opts_source
= g_strdup("");
477 args
->opts_target
= g_strdup("");
481 static void migrate_start_destroy(MigrateStart
*args
)
483 g_free(args
->opts_source
);
484 g_free(args
->opts_target
);
488 static int test_migrate_start(QTestState
**from
, QTestState
**to
,
489 const char *uri
, MigrateStart
*args
)
491 gchar
*arch_source
, *arch_target
;
492 gchar
*cmd_source
, *cmd_target
;
493 const gchar
*ignore_stderr
;
494 char *bootpath
= NULL
;
497 const char *arch
= qtest_get_arch();
498 const char *machine_opts
= NULL
;
499 const char *memory_size
;
502 if (args
->use_shmem
) {
503 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR
)) {
504 g_test_skip("/dev/shm is not supported");
511 bootpath
= g_strdup_printf("%s/bootsect", tmpfs
);
512 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
513 /* the assembled x86 boot sector should be exactly one sector large */
514 assert(sizeof(x86_bootsect
) == 512);
515 init_bootfile(bootpath
, x86_bootsect
, sizeof(x86_bootsect
));
516 memory_size
= "150M";
517 arch_source
= g_strdup_printf("-drive file=%s,format=raw", bootpath
);
518 arch_target
= g_strdup(arch_source
);
519 start_address
= X86_TEST_MEM_START
;
520 end_address
= X86_TEST_MEM_END
;
521 } else if (g_str_equal(arch
, "s390x")) {
522 init_bootfile(bootpath
, s390x_elf
, sizeof(s390x_elf
));
523 memory_size
= "128M";
524 arch_source
= g_strdup_printf("-bios %s", bootpath
);
525 arch_target
= g_strdup(arch_source
);
526 start_address
= S390_TEST_MEM_START
;
527 end_address
= S390_TEST_MEM_END
;
528 } else if (strcmp(arch
, "ppc64") == 0) {
529 machine_opts
= "vsmt=8";
530 memory_size
= "256M";
531 start_address
= PPC_TEST_MEM_START
;
532 end_address
= PPC_TEST_MEM_END
;
533 arch_source
= g_strdup_printf("-nodefaults "
534 "-prom-env 'use-nvramrc?=true' -prom-env "
535 "'nvramrc=hex .\" _\" begin %x %x "
536 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
537 "until'", end_address
, start_address
);
538 arch_target
= g_strdup("");
539 } else if (strcmp(arch
, "aarch64") == 0) {
540 init_bootfile(bootpath
, aarch64_kernel
, sizeof(aarch64_kernel
));
541 machine_opts
= "virt,gic-version=max";
542 memory_size
= "150M";
543 arch_source
= g_strdup_printf("-cpu max "
546 arch_target
= g_strdup(arch_source
);
547 start_address
= ARM_TEST_MEM_START
;
548 end_address
= ARM_TEST_MEM_END
;
550 g_assert(sizeof(aarch64_kernel
) <= ARM_TEST_MAX_KERNEL_SIZE
);
552 g_assert_not_reached();
557 if (args
->hide_stderr
) {
558 ignore_stderr
= "2>/dev/null";
563 if (args
->use_shmem
) {
564 shmem_path
= g_strdup_printf("/dev/shm/qemu-%d", getpid());
565 shmem_opts
= g_strdup_printf(
566 "-object memory-backend-file,id=mem0,size=%s"
567 ",mem-path=%s,share=on -numa node,memdev=mem0",
568 memory_size
, shmem_path
);
571 shmem_opts
= g_strdup("");
574 cmd_source
= g_strdup_printf("-accel kvm -accel tcg%s%s "
575 "-name source,debug-threads=on "
577 "-serial file:%s/src_serial "
579 machine_opts
? " -machine " : "",
580 machine_opts
? machine_opts
: "",
582 arch_source
, shmem_opts
, args
->opts_source
,
585 if (!args
->only_target
) {
586 *from
= qtest_init(cmd_source
);
590 cmd_target
= g_strdup_printf("-accel kvm -accel tcg%s%s "
591 "-name target,debug-threads=on "
593 "-serial file:%s/dest_serial "
596 machine_opts
? " -machine " : "",
597 machine_opts
? machine_opts
: "",
598 memory_size
, tmpfs
, uri
,
599 arch_target
, shmem_opts
,
600 args
->opts_target
, ignore_stderr
);
602 *to
= qtest_init(cmd_target
);
607 * Remove shmem file immediately to avoid memory leak in test failed case.
608 * It's valid becase QEMU has already opened this file
610 if (args
->use_shmem
) {
616 migrate_start_destroy(args
);
620 static void test_migrate_end(QTestState
*from
, QTestState
*to
, bool test_dest
)
622 unsigned char dest_byte_a
, dest_byte_b
, dest_byte_c
, dest_byte_d
;
627 qtest_memread(to
, start_address
, &dest_byte_a
, 1);
629 /* Destination still running, wait for a byte to change */
631 qtest_memread(to
, start_address
, &dest_byte_b
, 1);
633 } while (dest_byte_a
== dest_byte_b
);
635 qtest_qmp_discard_response(to
, "{ 'execute' : 'stop'}");
637 /* With it stopped, check nothing changes */
638 qtest_memread(to
, start_address
, &dest_byte_c
, 1);
640 qtest_memread(to
, start_address
, &dest_byte_d
, 1);
641 g_assert_cmpint(dest_byte_c
, ==, dest_byte_d
);
643 check_guests_ram(to
);
649 cleanup("migsocket");
650 cleanup("src_serial");
651 cleanup("dest_serial");
654 static void deprecated_set_downtime(QTestState
*who
, const double value
)
659 "{ 'execute': 'migrate_set_downtime',"
660 " 'arguments': { 'value': %f } }", value
);
661 g_assert(qdict_haskey(rsp
, "return"));
663 migrate_check_parameter_int(who
, "downtime-limit", value
* 1000);
666 static void deprecated_set_speed(QTestState
*who
, long long value
)
670 rsp
= qtest_qmp(who
, "{ 'execute': 'migrate_set_speed',"
671 "'arguments': { 'value': %lld } }", value
);
672 g_assert(qdict_haskey(rsp
, "return"));
674 migrate_check_parameter_int(who
, "max-bandwidth", value
);
677 static void deprecated_set_cache_size(QTestState
*who
, long long value
)
681 rsp
= qtest_qmp(who
, "{ 'execute': 'migrate-set-cache-size',"
682 "'arguments': { 'value': %lld } }", value
);
683 g_assert(qdict_haskey(rsp
, "return"));
685 migrate_check_parameter_int(who
, "xbzrle-cache-size", value
);
688 static void test_deprecated(void)
692 from
= qtest_init("-machine none");
694 deprecated_set_downtime(from
, 0.12345);
695 deprecated_set_speed(from
, 12345);
696 deprecated_set_cache_size(from
, 4096);
701 static int migrate_postcopy_prepare(QTestState
**from_ptr
,
705 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
706 QTestState
*from
, *to
;
708 if (test_migrate_start(&from
, &to
, uri
, args
)) {
712 migrate_set_capability(from
, "postcopy-ram", true);
713 migrate_set_capability(to
, "postcopy-ram", true);
714 migrate_set_capability(to
, "postcopy-blocktime", true);
716 /* We want to pick a speed slow enough that the test completes
717 * quickly, but that it doesn't complete precopy even on a slow
718 * machine, so also set the downtime.
720 migrate_set_parameter_int(from
, "max-bandwidth", 30000000);
721 migrate_set_parameter_int(from
, "downtime-limit", 1);
723 /* Wait for the first serial output from the source */
724 wait_for_serial("src_serial");
726 migrate_qmp(from
, uri
, "{}");
729 wait_for_migration_pass(from
);
737 static void migrate_postcopy_complete(QTestState
*from
, QTestState
*to
)
739 wait_for_migration_complete(from
);
741 /* Make sure we get at least one "B" on destination */
742 wait_for_serial("dest_serial");
744 if (uffd_feature_thread_id
) {
748 test_migrate_end(from
, to
, true);
751 static void test_postcopy(void)
753 MigrateStart
*args
= migrate_start_new();
754 QTestState
*from
, *to
;
756 if (migrate_postcopy_prepare(&from
, &to
, args
)) {
759 migrate_postcopy_start(from
, to
);
760 migrate_postcopy_complete(from
, to
);
763 static void test_postcopy_recovery(void)
765 MigrateStart
*args
= migrate_start_new();
766 QTestState
*from
, *to
;
769 args
->hide_stderr
= true;
771 if (migrate_postcopy_prepare(&from
, &to
, args
)) {
775 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
776 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 4096);
778 /* Now we start the postcopy */
779 migrate_postcopy_start(from
, to
);
782 * Wait until postcopy is really started; we can only run the
783 * migrate-pause command during a postcopy
785 wait_for_migration_status(from
, "postcopy-active", NULL
);
788 * Manually stop the postcopy migration. This emulates a network
789 * failure with the migration socket
794 * Wait for destination side to reach postcopy-paused state. The
795 * migrate-recover command can only succeed if destination machine
796 * is in the paused state
798 wait_for_migration_status(to
, "postcopy-paused",
799 (const char * []) { "failed", "active",
800 "completed", NULL
});
803 * Create a new socket to emulate a new channel that is different
804 * from the broken migration channel; tell the destination to
805 * listen to the new port
807 uri
= g_strdup_printf("unix:%s/migsocket-recover", tmpfs
);
808 migrate_recover(to
, uri
);
811 * Try to rebuild the migration channel using the resume flag and
812 * the newly created channel
814 wait_for_migration_status(from
, "postcopy-paused",
815 (const char * []) { "failed", "active",
816 "completed", NULL
});
817 migrate_qmp(from
, uri
, "{'resume': true}");
820 /* Restore the postcopy bandwidth to unlimited */
821 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 0);
823 migrate_postcopy_complete(from
, to
);
826 static void test_baddest(void)
828 MigrateStart
*args
= migrate_start_new();
829 QTestState
*from
, *to
;
831 args
->hide_stderr
= true;
833 if (test_migrate_start(&from
, &to
, "tcp:0:0", args
)) {
836 migrate_qmp(from
, "tcp:0:0", "{}");
837 wait_for_migration_fail(from
, false);
838 test_migrate_end(from
, to
, false);
841 static void test_precopy_unix(void)
843 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
844 MigrateStart
*args
= migrate_start_new();
845 QTestState
*from
, *to
;
847 if (test_migrate_start(&from
, &to
, uri
, args
)) {
851 /* We want to pick a speed slow enough that the test completes
852 * quickly, but that it doesn't complete precopy even on a slow
853 * machine, so also set the downtime.
855 /* 1 ms should make it not converge*/
856 migrate_set_parameter_int(from
, "downtime-limit", 1);
858 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
860 /* Wait for the first serial output from the source */
861 wait_for_serial("src_serial");
863 migrate_qmp(from
, uri
, "{}");
865 wait_for_migration_pass(from
);
867 /* 300 ms should converge */
868 migrate_set_parameter_int(from
, "downtime-limit", 300);
871 qtest_qmp_eventwait(from
, "STOP");
874 qtest_qmp_eventwait(to
, "RESUME");
876 wait_for_serial("dest_serial");
877 wait_for_migration_complete(from
);
879 test_migrate_end(from
, to
, true);
884 /* Currently upset on aarch64 TCG */
885 static void test_ignore_shared(void)
887 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
888 QTestState
*from
, *to
;
890 if (test_migrate_start(&from
, &to
, uri
, false, true, NULL
, NULL
)) {
894 migrate_set_capability(from
, "x-ignore-shared", true);
895 migrate_set_capability(to
, "x-ignore-shared", true);
897 /* Wait for the first serial output from the source */
898 wait_for_serial("src_serial");
900 migrate_qmp(from
, uri
, "{}");
902 wait_for_migration_pass(from
);
905 qtest_qmp_eventwait(from
, "STOP");
908 qtest_qmp_eventwait(to
, "RESUME");
910 wait_for_serial("dest_serial");
911 wait_for_migration_complete(from
);
913 /* Check whether shared RAM has been really skipped */
914 g_assert_cmpint(read_ram_property_int(from
, "transferred"), <, 1024 * 1024);
916 test_migrate_end(from
, to
, true);
921 static void test_xbzrle(const char *uri
)
923 MigrateStart
*args
= migrate_start_new();
924 QTestState
*from
, *to
;
926 if (test_migrate_start(&from
, &to
, uri
, args
)) {
931 * We want to pick a speed slow enough that the test completes
932 * quickly, but that it doesn't complete precopy even on a slow
933 * machine, so also set the downtime.
935 /* 1 ms should make it not converge*/
936 migrate_set_parameter_int(from
, "downtime-limit", 1);
938 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
940 migrate_set_parameter_int(from
, "xbzrle-cache-size", 33554432);
942 migrate_set_capability(from
, "xbzrle", "true");
943 migrate_set_capability(to
, "xbzrle", "true");
944 /* Wait for the first serial output from the source */
945 wait_for_serial("src_serial");
947 migrate_qmp(from
, uri
, "{}");
949 wait_for_migration_pass(from
);
951 /* 300ms should converge */
952 migrate_set_parameter_int(from
, "downtime-limit", 300);
955 qtest_qmp_eventwait(from
, "STOP");
957 qtest_qmp_eventwait(to
, "RESUME");
959 wait_for_serial("dest_serial");
960 wait_for_migration_complete(from
);
962 test_migrate_end(from
, to
, true);
965 static void test_xbzrle_unix(void)
967 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
973 static void test_precopy_tcp(void)
975 MigrateStart
*args
= migrate_start_new();
977 QTestState
*from
, *to
;
979 if (test_migrate_start(&from
, &to
, "tcp:127.0.0.1:0", args
)) {
984 * We want to pick a speed slow enough that the test completes
985 * quickly, but that it doesn't complete precopy even on a slow
986 * machine, so also set the downtime.
988 /* 1 ms should make it not converge*/
989 migrate_set_parameter_int(from
, "downtime-limit", 1);
991 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
993 /* Wait for the first serial output from the source */
994 wait_for_serial("src_serial");
996 uri
= migrate_get_socket_address(to
, "socket-address");
998 migrate_qmp(from
, uri
, "{}");
1000 wait_for_migration_pass(from
);
1002 /* 300ms should converge */
1003 migrate_set_parameter_int(from
, "downtime-limit", 300);
1006 qtest_qmp_eventwait(from
, "STOP");
1008 qtest_qmp_eventwait(to
, "RESUME");
1010 wait_for_serial("dest_serial");
1011 wait_for_migration_complete(from
);
1013 test_migrate_end(from
, to
, true);
1017 static void test_migrate_fd_proto(void)
1019 MigrateStart
*args
= migrate_start_new();
1020 QTestState
*from
, *to
;
1024 const char *error_desc
;
1026 if (test_migrate_start(&from
, &to
, "defer", args
)) {
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 /* Wait for the first serial output from the source */
1041 wait_for_serial("src_serial");
1043 /* Create two connected sockets for migration */
1044 ret
= socketpair(PF_LOCAL
, SOCK_STREAM
, 0, pair
);
1045 g_assert_cmpint(ret
, ==, 0);
1047 /* Send the 1st socket to the target */
1048 rsp
= wait_command_fd(to
, pair
[0],
1049 "{ 'execute': 'getfd',"
1050 " 'arguments': { 'fdname': 'fd-mig' }}");
1054 /* Start incoming migration from the 1st socket */
1055 rsp
= wait_command(to
, "{ 'execute': 'migrate-incoming',"
1056 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1059 /* Send the 2nd socket to the target */
1060 rsp
= wait_command_fd(from
, pair
[1],
1061 "{ 'execute': 'getfd',"
1062 " 'arguments': { 'fdname': 'fd-mig' }}");
1066 /* Start migration to the 2nd socket*/
1067 migrate_qmp(from
, "fd:fd-mig", "{}");
1069 wait_for_migration_pass(from
);
1071 /* 300ms should converge */
1072 migrate_set_parameter_int(from
, "downtime-limit", 300);
1075 qtest_qmp_eventwait(from
, "STOP");
1077 qtest_qmp_eventwait(to
, "RESUME");
1079 /* Test closing fds */
1080 /* We assume, that QEMU removes named fd from its list,
1081 * so this should fail */
1082 rsp
= qtest_qmp(from
, "{ 'execute': 'closefd',"
1083 " 'arguments': { 'fdname': 'fd-mig' }}");
1084 g_assert_true(qdict_haskey(rsp
, "error"));
1085 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
1086 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
1089 rsp
= qtest_qmp(to
, "{ 'execute': 'closefd',"
1090 " 'arguments': { 'fdname': 'fd-mig' }}");
1091 g_assert_true(qdict_haskey(rsp
, "error"));
1092 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
1093 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
1096 /* Complete migration */
1097 wait_for_serial("dest_serial");
1098 wait_for_migration_complete(from
);
1099 test_migrate_end(from
, to
, true);
1102 static void do_test_validate_uuid(MigrateStart
*args
, bool should_fail
)
1104 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1105 QTestState
*from
, *to
;
1107 if (test_migrate_start(&from
, &to
, uri
, args
)) {
1112 * UUID validation is at the begin of migration. So, the main process of
1113 * migration is not interesting for us here. Thus, set huge downtime for
1114 * very fast migration.
1116 migrate_set_parameter_int(from
, "downtime-limit", 1000000);
1117 migrate_set_capability(from
, "validate-uuid", true);
1119 /* Wait for the first serial output from the source */
1120 wait_for_serial("src_serial");
1122 migrate_qmp(from
, uri
, "{}");
1125 qtest_set_expected_status(to
, 1);
1126 wait_for_migration_fail(from
, true);
1128 wait_for_migration_complete(from
);
1131 test_migrate_end(from
, to
, false);
1135 static void test_validate_uuid(void)
1137 MigrateStart
*args
= migrate_start_new();
1139 g_free(args
->opts_source
);
1140 g_free(args
->opts_target
);
1141 args
->opts_source
= g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1142 args
->opts_target
= g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1143 do_test_validate_uuid(args
, false);
1146 static void test_validate_uuid_error(void)
1148 MigrateStart
*args
= migrate_start_new();
1150 g_free(args
->opts_source
);
1151 g_free(args
->opts_target
);
1152 args
->opts_source
= g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1153 args
->opts_target
= g_strdup("-uuid 22222222-2222-2222-2222-222222222222");
1154 args
->hide_stderr
= true;
1155 do_test_validate_uuid(args
, true);
1158 static void test_validate_uuid_src_not_set(void)
1160 MigrateStart
*args
= migrate_start_new();
1162 g_free(args
->opts_target
);
1163 args
->opts_target
= g_strdup("-uuid 22222222-2222-2222-2222-222222222222");
1164 args
->hide_stderr
= true;
1165 do_test_validate_uuid(args
, false);
1168 static void test_validate_uuid_dst_not_set(void)
1170 MigrateStart
*args
= migrate_start_new();
1172 g_free(args
->opts_source
);
1173 args
->opts_source
= g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1174 args
->hide_stderr
= true;
1175 do_test_validate_uuid(args
, false);
1178 static void test_migrate_auto_converge(void)
1180 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1181 MigrateStart
*args
= migrate_start_new();
1182 QTestState
*from
, *to
;
1183 int64_t remaining
, percentage
;
1186 * We want the test to be stable and as fast as possible.
1187 * E.g., with 1Gb/s bandwith migration may pass without throttling,
1188 * so we need to decrease a bandwidth.
1190 const int64_t init_pct
= 5, inc_pct
= 50, max_pct
= 95;
1191 const int64_t max_bandwidth
= 400000000; /* ~400Mb/s */
1192 const int64_t downtime_limit
= 250; /* 250ms */
1194 * We migrate through unix-socket (> 500Mb/s).
1195 * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
1196 * So, we can predict expected_threshold
1198 const int64_t expected_threshold
= max_bandwidth
* downtime_limit
/ 1000;
1200 if (test_migrate_start(&from
, &to
, uri
, args
)) {
1204 migrate_set_capability(from
, "auto-converge", true);
1205 migrate_set_parameter_int(from
, "cpu-throttle-initial", init_pct
);
1206 migrate_set_parameter_int(from
, "cpu-throttle-increment", inc_pct
);
1207 migrate_set_parameter_int(from
, "max-cpu-throttle", max_pct
);
1210 * Set the initial parameters so that the migration could not converge
1211 * without throttling.
1213 migrate_set_parameter_int(from
, "downtime-limit", 1);
1214 migrate_set_parameter_int(from
, "max-bandwidth", 100000000); /* ~100Mb/s */
1216 /* To check remaining size after precopy */
1217 migrate_set_capability(from
, "pause-before-switchover", true);
1219 /* Wait for the first serial output from the source */
1220 wait_for_serial("src_serial");
1222 migrate_qmp(from
, uri
, "{}");
1224 /* Wait for throttling begins */
1226 while (percentage
== 0) {
1227 percentage
= read_migrate_property_int(from
, "cpu-throttle-percentage");
1229 g_assert_false(got_stop
);
1231 /* The first percentage of throttling should be equal to init_pct */
1232 g_assert_cmpint(percentage
, ==, init_pct
);
1233 /* Now, when we tested that throttling works, let it converge */
1234 migrate_set_parameter_int(from
, "downtime-limit", downtime_limit
);
1235 migrate_set_parameter_int(from
, "max-bandwidth", max_bandwidth
);
1238 * Wait for pre-switchover status to check last throttle percentage
1239 * and remaining. These values will be zeroed later
1241 wait_for_migration_status(from
, "pre-switchover", NULL
);
1243 /* The final percentage of throttling shouldn't be greater than max_pct */
1244 percentage
= read_migrate_property_int(from
, "cpu-throttle-percentage");
1245 g_assert_cmpint(percentage
, <=, max_pct
);
1247 remaining
= read_ram_property_int(from
, "remaining");
1248 g_assert_cmpint(remaining
, <,
1249 (expected_threshold
+ expected_threshold
/ 100));
1251 migrate_continue(from
, "pre-switchover");
1253 qtest_qmp_eventwait(to
, "RESUME");
1255 wait_for_serial("dest_serial");
1256 wait_for_migration_complete(from
);
1260 test_migrate_end(from
, to
, true);
1263 static void test_multifd_tcp(const char *method
)
1265 MigrateStart
*args
= migrate_start_new();
1266 QTestState
*from
, *to
;
1270 if (test_migrate_start(&from
, &to
, "defer", args
)) {
1275 * We want to pick a speed slow enough that the test completes
1276 * quickly, but that it doesn't complete precopy even on a slow
1277 * machine, so also set the downtime.
1279 /* 1 ms should make it not converge*/
1280 migrate_set_parameter_int(from
, "downtime-limit", 1);
1282 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
1284 migrate_set_parameter_int(from
, "multifd-channels", 16);
1285 migrate_set_parameter_int(to
, "multifd-channels", 16);
1287 migrate_set_parameter_str(from
, "multifd-compression", method
);
1288 migrate_set_parameter_str(to
, "multifd-compression", method
);
1290 migrate_set_capability(from
, "multifd", "true");
1291 migrate_set_capability(to
, "multifd", "true");
1293 /* Start incoming migration from the 1st socket */
1294 rsp
= wait_command(to
, "{ 'execute': 'migrate-incoming',"
1295 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1298 /* Wait for the first serial output from the source */
1299 wait_for_serial("src_serial");
1301 uri
= migrate_get_socket_address(to
, "socket-address");
1303 migrate_qmp(from
, uri
, "{}");
1305 wait_for_migration_pass(from
);
1307 /* 300ms it should converge */
1308 migrate_set_parameter_int(from
, "downtime-limit", 300);
1311 qtest_qmp_eventwait(from
, "STOP");
1313 qtest_qmp_eventwait(to
, "RESUME");
1315 wait_for_serial("dest_serial");
1316 wait_for_migration_complete(from
);
1317 test_migrate_end(from
, to
, true);
1321 static void test_multifd_tcp_none(void)
1323 test_multifd_tcp("none");
1326 static void test_multifd_tcp_zlib(void)
1328 test_multifd_tcp("zlib");
1332 static void test_multifd_tcp_zstd(void)
1334 test_multifd_tcp("zstd");
1344 * launch another target
1347 * And see that it works
1349 static void test_multifd_tcp_cancel(void)
1351 MigrateStart
*args
= migrate_start_new();
1352 QTestState
*from
, *to
, *to2
;
1356 args
->hide_stderr
= true;
1358 if (test_migrate_start(&from
, &to
, "defer", args
)) {
1363 * We want to pick a speed slow enough that the test completes
1364 * quickly, but that it doesn't complete precopy even on a slow
1365 * machine, so also set the downtime.
1367 /* 1 ms should make it not converge*/
1368 migrate_set_parameter_int(from
, "downtime-limit", 1);
1370 migrate_set_parameter_int(from
, "max-bandwidth", 30000000);
1372 migrate_set_parameter_int(from
, "multifd-channels", 16);
1373 migrate_set_parameter_int(to
, "multifd-channels", 16);
1375 migrate_set_capability(from
, "multifd", "true");
1376 migrate_set_capability(to
, "multifd", "true");
1378 /* Start incoming migration from the 1st socket */
1379 rsp
= wait_command(to
, "{ 'execute': 'migrate-incoming',"
1380 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1383 /* Wait for the first serial output from the source */
1384 wait_for_serial("src_serial");
1386 uri
= migrate_get_socket_address(to
, "socket-address");
1388 migrate_qmp(from
, uri
, "{}");
1390 wait_for_migration_pass(from
);
1392 migrate_cancel(from
);
1394 args
= migrate_start_new();
1395 args
->only_target
= true;
1397 if (test_migrate_start(&from
, &to2
, "defer", args
)) {
1401 migrate_set_parameter_int(to2
, "multifd-channels", 16);
1403 migrate_set_capability(to2
, "multifd", "true");
1405 /* Start incoming migration from the 1st socket */
1406 rsp
= wait_command(to2
, "{ 'execute': 'migrate-incoming',"
1407 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1411 uri
= migrate_get_socket_address(to2
, "socket-address");
1413 wait_for_migration_status(from
, "cancelled", NULL
);
1415 /* 300ms it should converge */
1416 migrate_set_parameter_int(from
, "downtime-limit", 300);
1418 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
1420 migrate_qmp(from
, uri
, "{}");
1422 wait_for_migration_pass(from
);
1425 qtest_qmp_eventwait(from
, "STOP");
1427 qtest_qmp_eventwait(to2
, "RESUME");
1429 wait_for_serial("dest_serial");
1430 wait_for_migration_complete(from
);
1431 test_migrate_end(from
, to2
, true);
1435 int main(int argc
, char **argv
)
1437 char template[] = "/tmp/migration-test-XXXXXX";
1440 g_test_init(&argc
, &argv
, NULL
);
1442 if (!ufd_version_check()) {
1443 return g_test_run();
1447 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
1448 * is touchy due to race conditions on dirty bits (especially on PPC for
1451 if (g_str_equal(qtest_get_arch(), "ppc64") &&
1452 (access("/sys/module/kvm_hv", F_OK
) ||
1453 access("/dev/kvm", R_OK
| W_OK
))) {
1454 g_test_message("Skipping test: kvm_hv not available");
1455 return g_test_run();
1459 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
1460 * there until the problems are resolved
1462 if (g_str_equal(qtest_get_arch(), "s390x")) {
1463 #if defined(HOST_S390X)
1464 if (access("/dev/kvm", R_OK
| W_OK
)) {
1465 g_test_message("Skipping test: kvm not available");
1466 return g_test_run();
1469 g_test_message("Skipping test: Need s390x host to work properly");
1470 return g_test_run();
1474 tmpfs
= mkdtemp(template);
1476 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno
));
1480 module_call_init(MODULE_INIT_QOM
);
1482 qtest_add_func("/migration/postcopy/unix", test_postcopy
);
1483 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery
);
1484 qtest_add_func("/migration/deprecated", test_deprecated
);
1485 qtest_add_func("/migration/bad_dest", test_baddest
);
1486 qtest_add_func("/migration/precopy/unix", test_precopy_unix
);
1487 qtest_add_func("/migration/precopy/tcp", test_precopy_tcp
);
1488 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
1489 qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix
);
1490 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto
);
1491 qtest_add_func("/migration/validate_uuid", test_validate_uuid
);
1492 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error
);
1493 qtest_add_func("/migration/validate_uuid_src_not_set",
1494 test_validate_uuid_src_not_set
);
1495 qtest_add_func("/migration/validate_uuid_dst_not_set",
1496 test_validate_uuid_dst_not_set
);
1498 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge
);
1499 qtest_add_func("/migration/multifd/tcp/none", test_multifd_tcp_none
);
1500 qtest_add_func("/migration/multifd/tcp/cancel", test_multifd_tcp_cancel
);
1501 qtest_add_func("/migration/multifd/tcp/zlib", test_multifd_tcp_zlib
);
1503 qtest_add_func("/migration/multifd/tcp/zstd", test_multifd_tcp_zstd
);
1508 g_assert_cmpint(ret
, ==, 0);
1512 g_test_message("unable to rmdir: path (%s): %s",
1513 tmpfs
, strerror(errno
));