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 "qemu/module.h"
18 #include "qemu/option.h"
19 #include "qemu/range.h"
20 #include "qemu/sockets.h"
21 #include "chardev/char.h"
22 #include "qapi/qapi-visit-sockets.h"
23 #include "qapi/qobject-input-visitor.h"
24 #include "qapi/qobject-output-visitor.h"
26 #include "migration-helpers.h"
27 #include "migration/migration-test.h"
29 /* TODO actually test the results and get rid of this */
30 #define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__))
32 unsigned start_address
;
34 static bool uffd_feature_thread_id
;
36 #if defined(__linux__)
37 #include <sys/syscall.h>
41 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
42 #include <sys/eventfd.h>
43 #include <sys/ioctl.h>
44 #include <linux/userfaultfd.h>
46 static bool ufd_version_check(void)
48 struct uffdio_api api_struct
;
51 int ufd
= syscall(__NR_userfaultfd
, O_CLOEXEC
);
54 g_test_message("Skipping test: userfaultfd not available");
58 api_struct
.api
= UFFD_API
;
59 api_struct
.features
= 0;
60 if (ioctl(ufd
, UFFDIO_API
, &api_struct
)) {
61 g_test_message("Skipping test: UFFDIO_API failed");
64 uffd_feature_thread_id
= api_struct
.features
& UFFD_FEATURE_THREAD_ID
;
66 ioctl_mask
= (__u64
)1 << _UFFDIO_REGISTER
|
67 (__u64
)1 << _UFFDIO_UNREGISTER
;
68 if ((api_struct
.ioctls
& ioctl_mask
) != ioctl_mask
) {
69 g_test_message("Skipping test: Missing userfault feature");
77 static bool ufd_version_check(void)
79 g_test_message("Skipping test: Userfault not available (builtdtime)");
85 static const char *tmpfs
;
87 /* The boot file modifies memory area in [start_address, end_address)
88 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
90 #include "tests/migration/i386/a-b-bootblock.h"
91 #include "tests/migration/aarch64/a-b-kernel.h"
92 #include "tests/migration/s390x/a-b-bios.h"
94 static void init_bootfile(const char *bootpath
, void *content
, size_t len
)
96 FILE *bootfile
= fopen(bootpath
, "wb");
98 g_assert_cmpint(fwrite(content
, len
, 1, bootfile
), ==, 1);
103 * Wait for some output in the serial output file,
104 * we get an 'A' followed by an endless string of 'B's
105 * but on the destination we won't have the A.
107 static void wait_for_serial(const char *side
)
109 char *serialpath
= g_strdup_printf("%s/%s", tmpfs
, side
);
110 FILE *serialfile
= fopen(serialpath
, "r");
111 const char *arch
= qtest_get_arch();
112 int started
= (strcmp(side
, "src_serial") == 0 &&
113 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
117 int readvalue
= fgetc(serialfile
);
120 /* SLOF prints its banner before starting test,
121 * to ignore it, mark the start of the test with '_',
122 * ignore all characters until this marker
129 fseek(serialfile
, 0, SEEK_SET
);
146 started
= (strcmp(side
, "src_serial") == 0 &&
147 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
148 fseek(serialfile
, 0, SEEK_SET
);
153 fprintf(stderr
, "Unexpected %d on %s serial\n", readvalue
, side
);
154 g_assert_not_reached();
160 * It's tricky to use qemu's migration event capability with qtest,
161 * events suddenly appearing confuse the qmp()/hmp() responses.
164 static int64_t read_ram_property_int(QTestState
*who
, const char *property
)
166 QDict
*rsp_return
, *rsp_ram
;
169 rsp_return
= migrate_query(who
);
170 if (!qdict_haskey(rsp_return
, "ram")) {
174 rsp_ram
= qdict_get_qdict(rsp_return
, "ram");
175 result
= qdict_get_try_int(rsp_ram
, property
, 0);
177 qobject_unref(rsp_return
);
181 static int64_t read_migrate_property_int(QTestState
*who
, const char *property
)
186 rsp_return
= migrate_query(who
);
187 result
= qdict_get_try_int(rsp_return
, property
, 0);
188 qobject_unref(rsp_return
);
192 static uint64_t get_migration_pass(QTestState
*who
)
194 return read_ram_property_int(who
, "dirty-sync-count");
197 static void read_blocktime(QTestState
*who
)
201 rsp_return
= migrate_query(who
);
202 g_assert(qdict_haskey(rsp_return
, "postcopy-blocktime"));
203 qobject_unref(rsp_return
);
206 static void wait_for_migration_pass(QTestState
*who
)
208 uint64_t initial_pass
= get_migration_pass(who
);
211 /* Wait for the 1st sync */
212 while (!got_stop
&& !initial_pass
) {
214 initial_pass
= get_migration_pass(who
);
219 pass
= get_migration_pass(who
);
220 } while (pass
== initial_pass
&& !got_stop
);
223 static void check_guests_ram(QTestState
*who
)
225 /* Our ASM test will have been incrementing one byte from each page from
226 * start_address to < end_address in order. This gives us a constraint
227 * that any page's byte should be equal or less than the previous pages
228 * byte (mod 256); and they should all be equal except for one transition
229 * at the point where we meet the incrementer. (We're running this with
230 * the guest stopped).
235 bool hit_edge
= false;
238 qtest_memread(who
, start_address
, &first_byte
, 1);
239 last_byte
= first_byte
;
241 for (address
= start_address
+ TEST_MEM_PAGE_SIZE
; address
< end_address
;
242 address
+= TEST_MEM_PAGE_SIZE
)
245 qtest_memread(who
, address
, &b
, 1);
246 if (b
!= last_byte
) {
247 if (((b
+ 1) % 256) == last_byte
&& !hit_edge
) {
248 /* This is OK, the guest stopped at the point of
249 * incrementing the previous page but didn't get
257 fprintf(stderr
, "Memory content inconsistency at %x"
258 " first_byte = %x last_byte = %x current = %x"
260 address
, first_byte
, last_byte
, b
, hit_edge
);
266 fprintf(stderr
, "and in another %d pages", bad
- 10);
271 static void cleanup(const char *filename
)
273 char *path
= g_strdup_printf("%s/%s", tmpfs
, filename
);
279 static char *SocketAddress_to_str(SocketAddress
*addr
)
281 switch (addr
->type
) {
282 case SOCKET_ADDRESS_TYPE_INET
:
283 return g_strdup_printf("tcp:%s:%s",
286 case SOCKET_ADDRESS_TYPE_UNIX
:
287 return g_strdup_printf("unix:%s",
288 addr
->u
.q_unix
.path
);
289 case SOCKET_ADDRESS_TYPE_FD
:
290 return g_strdup_printf("fd:%s", addr
->u
.fd
.str
);
291 case SOCKET_ADDRESS_TYPE_VSOCK
:
292 return g_strdup_printf("tcp:%s:%s",
296 return g_strdup("unknown address type");
300 static char *migrate_get_socket_address(QTestState
*who
, const char *parameter
)
304 Error
*local_err
= NULL
;
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
, &local_err
);
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 __attribute__((unused
))
382 static void migrate_set_parameter_str(QTestState
*who
, const char *parameter
,
388 "{ 'execute': 'migrate-set-parameters',"
389 "'arguments': { %s: %s } }",
391 g_assert(qdict_haskey(rsp
, "return"));
393 migrate_check_parameter_str(who
, parameter
, value
);
396 static void migrate_pause(QTestState
*who
)
400 rsp
= wait_command(who
, "{ 'execute': 'migrate-pause' }");
404 static void migrate_continue(QTestState
*who
, const char *state
)
408 rsp
= wait_command(who
,
409 "{ 'execute': 'migrate-continue',"
410 " 'arguments': { 'state': %s } }",
415 static void migrate_recover(QTestState
*who
, const char *uri
)
419 rsp
= wait_command(who
,
420 "{ 'execute': 'migrate-recover', "
421 " 'id': 'recover-cmd', "
422 " 'arguments': { 'uri': %s } }",
427 static void migrate_cancel(QTestState
*who
)
431 rsp
= wait_command(who
, "{ 'execute': 'migrate_cancel' }");
435 static void migrate_set_capability(QTestState
*who
, const char *capability
,
441 "{ 'execute': 'migrate-set-capabilities',"
443 "'capabilities': [ { "
444 "'capability': %s, 'state': %i } ] } }",
446 g_assert(qdict_haskey(rsp
, "return"));
450 static void migrate_postcopy_start(QTestState
*from
, QTestState
*to
)
454 rsp
= wait_command(from
, "{ 'execute': 'migrate-start-postcopy' }");
458 qtest_qmp_eventwait(from
, "STOP");
461 qtest_qmp_eventwait(to
, "RESUME");
467 /* only launch the target process */
473 static MigrateStart
*migrate_start_new(void)
475 MigrateStart
*args
= g_new0(MigrateStart
, 1);
477 args
->opts_source
= g_strdup("");
478 args
->opts_target
= g_strdup("");
482 static void migrate_start_destroy(MigrateStart
*args
)
484 g_free(args
->opts_source
);
485 g_free(args
->opts_target
);
489 static int test_migrate_start(QTestState
**from
, QTestState
**to
,
490 const char *uri
, MigrateStart
*args
)
492 gchar
*arch_source
, *arch_target
;
493 gchar
*cmd_source
, *cmd_target
;
494 const gchar
*ignore_stderr
;
495 char *bootpath
= NULL
;
498 const char *arch
= qtest_get_arch();
499 const char *machine_opts
= NULL
;
500 const char *memory_size
;
503 if (args
->use_shmem
) {
504 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR
)) {
505 g_test_skip("/dev/shm is not supported");
512 bootpath
= g_strdup_printf("%s/bootsect", tmpfs
);
513 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
514 /* the assembled x86 boot sector should be exactly one sector large */
515 assert(sizeof(x86_bootsect
) == 512);
516 init_bootfile(bootpath
, x86_bootsect
, sizeof(x86_bootsect
));
517 memory_size
= "150M";
518 arch_source
= g_strdup_printf("-drive file=%s,format=raw", bootpath
);
519 arch_target
= g_strdup(arch_source
);
520 start_address
= X86_TEST_MEM_START
;
521 end_address
= X86_TEST_MEM_END
;
522 } else if (g_str_equal(arch
, "s390x")) {
523 init_bootfile(bootpath
, s390x_elf
, sizeof(s390x_elf
));
524 memory_size
= "128M";
525 arch_source
= g_strdup_printf("-bios %s", bootpath
);
526 arch_target
= g_strdup(arch_source
);
527 start_address
= S390_TEST_MEM_START
;
528 end_address
= S390_TEST_MEM_END
;
529 } else if (strcmp(arch
, "ppc64") == 0) {
530 machine_opts
= "vsmt=8";
531 memory_size
= "256M";
532 start_address
= PPC_TEST_MEM_START
;
533 end_address
= PPC_TEST_MEM_END
;
534 arch_source
= g_strdup_printf("-nodefaults "
535 "-prom-env 'use-nvramrc?=true' -prom-env "
536 "'nvramrc=hex .\" _\" begin %x %x "
537 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
538 "until'", end_address
, start_address
);
539 arch_target
= g_strdup("");
540 } else if (strcmp(arch
, "aarch64") == 0) {
541 init_bootfile(bootpath
, aarch64_kernel
, sizeof(aarch64_kernel
));
542 machine_opts
= "virt,gic-version=max";
543 memory_size
= "150M";
544 arch_source
= g_strdup_printf("-cpu max "
547 arch_target
= g_strdup(arch_source
);
548 start_address
= ARM_TEST_MEM_START
;
549 end_address
= ARM_TEST_MEM_END
;
551 g_assert(sizeof(aarch64_kernel
) <= ARM_TEST_MAX_KERNEL_SIZE
);
553 g_assert_not_reached();
558 if (args
->hide_stderr
) {
559 ignore_stderr
= "2>/dev/null";
564 if (args
->use_shmem
) {
565 shmem_path
= g_strdup_printf("/dev/shm/qemu-%d", getpid());
566 shmem_opts
= g_strdup_printf(
567 "-object memory-backend-file,id=mem0,size=%s"
568 ",mem-path=%s,share=on -numa node,memdev=mem0",
569 memory_size
, shmem_path
);
572 shmem_opts
= g_strdup("");
575 cmd_source
= g_strdup_printf("-accel kvm -accel tcg%s%s "
576 "-name source,debug-threads=on "
578 "-serial file:%s/src_serial "
580 machine_opts
? " -machine " : "",
581 machine_opts
? machine_opts
: "",
583 arch_source
, shmem_opts
, args
->opts_source
,
586 if (!args
->only_target
) {
587 *from
= qtest_init(cmd_source
);
591 cmd_target
= g_strdup_printf("-accel kvm -accel tcg%s%s "
592 "-name target,debug-threads=on "
594 "-serial file:%s/dest_serial "
597 machine_opts
? " -machine " : "",
598 machine_opts
? machine_opts
: "",
599 memory_size
, tmpfs
, uri
,
600 arch_target
, shmem_opts
,
601 args
->opts_target
, ignore_stderr
);
603 *to
= qtest_init(cmd_target
);
608 * Remove shmem file immediately to avoid memory leak in test failed case.
609 * It's valid becase QEMU has already opened this file
611 if (args
->use_shmem
) {
617 migrate_start_destroy(args
);
621 static void test_migrate_end(QTestState
*from
, QTestState
*to
, bool test_dest
)
623 unsigned char dest_byte_a
, dest_byte_b
, dest_byte_c
, dest_byte_d
;
628 qtest_memread(to
, start_address
, &dest_byte_a
, 1);
630 /* Destination still running, wait for a byte to change */
632 qtest_memread(to
, start_address
, &dest_byte_b
, 1);
634 } while (dest_byte_a
== dest_byte_b
);
636 qtest_qmp_discard_response(to
, "{ 'execute' : 'stop'}");
638 /* With it stopped, check nothing changes */
639 qtest_memread(to
, start_address
, &dest_byte_c
, 1);
641 qtest_memread(to
, start_address
, &dest_byte_d
, 1);
642 g_assert_cmpint(dest_byte_c
, ==, dest_byte_d
);
644 check_guests_ram(to
);
650 cleanup("migsocket");
651 cleanup("src_serial");
652 cleanup("dest_serial");
655 static void deprecated_set_downtime(QTestState
*who
, const double value
)
660 "{ 'execute': 'migrate_set_downtime',"
661 " 'arguments': { 'value': %f } }", value
);
662 g_assert(qdict_haskey(rsp
, "return"));
664 migrate_check_parameter_int(who
, "downtime-limit", value
* 1000);
667 static void deprecated_set_speed(QTestState
*who
, long long value
)
671 rsp
= qtest_qmp(who
, "{ 'execute': 'migrate_set_speed',"
672 "'arguments': { 'value': %lld } }", value
);
673 g_assert(qdict_haskey(rsp
, "return"));
675 migrate_check_parameter_int(who
, "max-bandwidth", value
);
678 static void deprecated_set_cache_size(QTestState
*who
, long long value
)
682 rsp
= qtest_qmp(who
, "{ 'execute': 'migrate-set-cache-size',"
683 "'arguments': { 'value': %lld } }", value
);
684 g_assert(qdict_haskey(rsp
, "return"));
686 migrate_check_parameter_int(who
, "xbzrle-cache-size", value
);
689 static void test_deprecated(void)
693 from
= qtest_init("-machine none");
695 deprecated_set_downtime(from
, 0.12345);
696 deprecated_set_speed(from
, 12345);
697 deprecated_set_cache_size(from
, 4096);
702 static int migrate_postcopy_prepare(QTestState
**from_ptr
,
706 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
707 QTestState
*from
, *to
;
709 if (test_migrate_start(&from
, &to
, uri
, args
)) {
713 migrate_set_capability(from
, "postcopy-ram", true);
714 migrate_set_capability(to
, "postcopy-ram", true);
715 migrate_set_capability(to
, "postcopy-blocktime", true);
717 /* We want to pick a speed slow enough that the test completes
718 * quickly, but that it doesn't complete precopy even on a slow
719 * machine, so also set the downtime.
721 migrate_set_parameter_int(from
, "max-bandwidth", 30000000);
722 migrate_set_parameter_int(from
, "downtime-limit", 1);
724 /* Wait for the first serial output from the source */
725 wait_for_serial("src_serial");
727 migrate_qmp(from
, uri
, "{}");
730 wait_for_migration_pass(from
);
738 static void migrate_postcopy_complete(QTestState
*from
, QTestState
*to
)
740 wait_for_migration_complete(from
);
742 /* Make sure we get at least one "B" on destination */
743 wait_for_serial("dest_serial");
745 if (uffd_feature_thread_id
) {
749 test_migrate_end(from
, to
, true);
752 static void test_postcopy(void)
754 MigrateStart
*args
= migrate_start_new();
755 QTestState
*from
, *to
;
757 if (migrate_postcopy_prepare(&from
, &to
, args
)) {
760 migrate_postcopy_start(from
, to
);
761 migrate_postcopy_complete(from
, to
);
764 static void test_postcopy_recovery(void)
766 MigrateStart
*args
= migrate_start_new();
767 QTestState
*from
, *to
;
770 args
->hide_stderr
= true;
772 if (migrate_postcopy_prepare(&from
, &to
, args
)) {
776 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
777 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 4096);
779 /* Now we start the postcopy */
780 migrate_postcopy_start(from
, to
);
783 * Wait until postcopy is really started; we can only run the
784 * migrate-pause command during a postcopy
786 wait_for_migration_status(from
, "postcopy-active", NULL
);
789 * Manually stop the postcopy migration. This emulates a network
790 * failure with the migration socket
795 * Wait for destination side to reach postcopy-paused state. The
796 * migrate-recover command can only succeed if destination machine
797 * is in the paused state
799 wait_for_migration_status(to
, "postcopy-paused",
800 (const char * []) { "failed", "active",
801 "completed", NULL
});
804 * Create a new socket to emulate a new channel that is different
805 * from the broken migration channel; tell the destination to
806 * listen to the new port
808 uri
= g_strdup_printf("unix:%s/migsocket-recover", tmpfs
);
809 migrate_recover(to
, uri
);
812 * Try to rebuild the migration channel using the resume flag and
813 * the newly created channel
815 wait_for_migration_status(from
, "postcopy-paused",
816 (const char * []) { "failed", "active",
817 "completed", NULL
});
818 migrate_qmp(from
, uri
, "{'resume': true}");
821 /* Restore the postcopy bandwidth to unlimited */
822 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 0);
824 migrate_postcopy_complete(from
, to
);
827 static void test_baddest(void)
829 MigrateStart
*args
= migrate_start_new();
830 QTestState
*from
, *to
;
832 args
->hide_stderr
= true;
834 if (test_migrate_start(&from
, &to
, "tcp:0:0", args
)) {
837 migrate_qmp(from
, "tcp:0:0", "{}");
838 wait_for_migration_fail(from
, false);
839 test_migrate_end(from
, to
, false);
842 static void test_precopy_unix(void)
844 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
845 MigrateStart
*args
= migrate_start_new();
846 QTestState
*from
, *to
;
848 if (test_migrate_start(&from
, &to
, uri
, args
)) {
852 /* We want to pick a speed slow enough that the test completes
853 * quickly, but that it doesn't complete precopy even on a slow
854 * machine, so also set the downtime.
856 /* 1 ms should make it not converge*/
857 migrate_set_parameter_int(from
, "downtime-limit", 1);
859 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
861 /* Wait for the first serial output from the source */
862 wait_for_serial("src_serial");
864 migrate_qmp(from
, uri
, "{}");
866 wait_for_migration_pass(from
);
868 /* 300 ms should converge */
869 migrate_set_parameter_int(from
, "downtime-limit", 300);
872 qtest_qmp_eventwait(from
, "STOP");
875 qtest_qmp_eventwait(to
, "RESUME");
877 wait_for_serial("dest_serial");
878 wait_for_migration_complete(from
);
880 test_migrate_end(from
, to
, true);
885 /* Currently upset on aarch64 TCG */
886 static void test_ignore_shared(void)
888 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
889 QTestState
*from
, *to
;
891 if (test_migrate_start(&from
, &to
, uri
, false, true, NULL
, NULL
)) {
895 migrate_set_capability(from
, "x-ignore-shared", true);
896 migrate_set_capability(to
, "x-ignore-shared", true);
898 /* Wait for the first serial output from the source */
899 wait_for_serial("src_serial");
901 migrate_qmp(from
, uri
, "{}");
903 wait_for_migration_pass(from
);
906 qtest_qmp_eventwait(from
, "STOP");
909 qtest_qmp_eventwait(to
, "RESUME");
911 wait_for_serial("dest_serial");
912 wait_for_migration_complete(from
);
914 /* Check whether shared RAM has been really skipped */
915 g_assert_cmpint(read_ram_property_int(from
, "transferred"), <, 1024 * 1024);
917 test_migrate_end(from
, to
, true);
922 static void test_xbzrle(const char *uri
)
924 MigrateStart
*args
= migrate_start_new();
925 QTestState
*from
, *to
;
927 if (test_migrate_start(&from
, &to
, uri
, args
)) {
932 * We want to pick a speed slow enough that the test completes
933 * quickly, but that it doesn't complete precopy even on a slow
934 * machine, so also set the downtime.
936 /* 1 ms should make it not converge*/
937 migrate_set_parameter_int(from
, "downtime-limit", 1);
939 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
941 migrate_set_parameter_int(from
, "xbzrle-cache-size", 33554432);
943 migrate_set_capability(from
, "xbzrle", "true");
944 migrate_set_capability(to
, "xbzrle", "true");
945 /* Wait for the first serial output from the source */
946 wait_for_serial("src_serial");
948 migrate_qmp(from
, uri
, "{}");
950 wait_for_migration_pass(from
);
952 /* 300ms should converge */
953 migrate_set_parameter_int(from
, "downtime-limit", 300);
956 qtest_qmp_eventwait(from
, "STOP");
958 qtest_qmp_eventwait(to
, "RESUME");
960 wait_for_serial("dest_serial");
961 wait_for_migration_complete(from
);
963 test_migrate_end(from
, to
, true);
966 static void test_xbzrle_unix(void)
968 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
974 static void test_precopy_tcp(void)
976 MigrateStart
*args
= migrate_start_new();
978 QTestState
*from
, *to
;
980 if (test_migrate_start(&from
, &to
, "tcp:127.0.0.1:0", args
)) {
985 * We want to pick a speed slow enough that the test completes
986 * quickly, but that it doesn't complete precopy even on a slow
987 * machine, so also set the downtime.
989 /* 1 ms should make it not converge*/
990 migrate_set_parameter_int(from
, "downtime-limit", 1);
992 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
994 /* Wait for the first serial output from the source */
995 wait_for_serial("src_serial");
997 uri
= migrate_get_socket_address(to
, "socket-address");
999 migrate_qmp(from
, uri
, "{}");
1001 wait_for_migration_pass(from
);
1003 /* 300ms should converge */
1004 migrate_set_parameter_int(from
, "downtime-limit", 300);
1007 qtest_qmp_eventwait(from
, "STOP");
1009 qtest_qmp_eventwait(to
, "RESUME");
1011 wait_for_serial("dest_serial");
1012 wait_for_migration_complete(from
);
1014 test_migrate_end(from
, to
, true);
1018 static void test_migrate_fd_proto(void)
1020 MigrateStart
*args
= migrate_start_new();
1021 QTestState
*from
, *to
;
1025 const char *error_desc
;
1027 if (test_migrate_start(&from
, &to
, "defer", args
)) {
1032 * We want to pick a speed slow enough that the test completes
1033 * quickly, but that it doesn't complete precopy even on a slow
1034 * machine, so also set the downtime.
1036 /* 1 ms should make it not converge */
1037 migrate_set_parameter_int(from
, "downtime-limit", 1);
1039 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
1041 /* Wait for the first serial output from the source */
1042 wait_for_serial("src_serial");
1044 /* Create two connected sockets for migration */
1045 ret
= socketpair(PF_LOCAL
, SOCK_STREAM
, 0, pair
);
1046 g_assert_cmpint(ret
, ==, 0);
1048 /* Send the 1st socket to the target */
1049 rsp
= wait_command_fd(to
, pair
[0],
1050 "{ 'execute': 'getfd',"
1051 " 'arguments': { 'fdname': 'fd-mig' }}");
1055 /* Start incoming migration from the 1st socket */
1056 rsp
= wait_command(to
, "{ 'execute': 'migrate-incoming',"
1057 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1060 /* Send the 2nd socket to the target */
1061 rsp
= wait_command_fd(from
, pair
[1],
1062 "{ 'execute': 'getfd',"
1063 " 'arguments': { 'fdname': 'fd-mig' }}");
1067 /* Start migration to the 2nd socket*/
1068 migrate_qmp(from
, "fd:fd-mig", "{}");
1070 wait_for_migration_pass(from
);
1072 /* 300ms should converge */
1073 migrate_set_parameter_int(from
, "downtime-limit", 300);
1076 qtest_qmp_eventwait(from
, "STOP");
1078 qtest_qmp_eventwait(to
, "RESUME");
1080 /* Test closing fds */
1081 /* We assume, that QEMU removes named fd from its list,
1082 * so this should fail */
1083 rsp
= qtest_qmp(from
, "{ 'execute': 'closefd',"
1084 " 'arguments': { 'fdname': 'fd-mig' }}");
1085 g_assert_true(qdict_haskey(rsp
, "error"));
1086 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
1087 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
1090 rsp
= qtest_qmp(to
, "{ 'execute': 'closefd',"
1091 " 'arguments': { 'fdname': 'fd-mig' }}");
1092 g_assert_true(qdict_haskey(rsp
, "error"));
1093 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
1094 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
1097 /* Complete migration */
1098 wait_for_serial("dest_serial");
1099 wait_for_migration_complete(from
);
1100 test_migrate_end(from
, to
, true);
1103 static void do_test_validate_uuid(MigrateStart
*args
, bool should_fail
)
1105 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1106 QTestState
*from
, *to
;
1108 if (test_migrate_start(&from
, &to
, uri
, args
)) {
1113 * UUID validation is at the begin of migration. So, the main process of
1114 * migration is not interesting for us here. Thus, set huge downtime for
1115 * very fast migration.
1117 migrate_set_parameter_int(from
, "downtime-limit", 1000000);
1118 migrate_set_capability(from
, "validate-uuid", true);
1120 /* Wait for the first serial output from the source */
1121 wait_for_serial("src_serial");
1123 migrate_qmp(from
, uri
, "{}");
1126 qtest_set_expected_status(to
, 1);
1127 wait_for_migration_fail(from
, true);
1129 wait_for_migration_complete(from
);
1132 test_migrate_end(from
, to
, false);
1136 static void test_validate_uuid(void)
1138 MigrateStart
*args
= migrate_start_new();
1140 g_free(args
->opts_source
);
1141 g_free(args
->opts_target
);
1142 args
->opts_source
= g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1143 args
->opts_target
= g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1144 do_test_validate_uuid(args
, false);
1147 static void test_validate_uuid_error(void)
1149 MigrateStart
*args
= migrate_start_new();
1151 g_free(args
->opts_source
);
1152 g_free(args
->opts_target
);
1153 args
->opts_source
= g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1154 args
->opts_target
= g_strdup("-uuid 22222222-2222-2222-2222-222222222222");
1155 args
->hide_stderr
= true;
1156 do_test_validate_uuid(args
, true);
1159 static void test_validate_uuid_src_not_set(void)
1161 MigrateStart
*args
= migrate_start_new();
1163 g_free(args
->opts_target
);
1164 args
->opts_target
= g_strdup("-uuid 22222222-2222-2222-2222-222222222222");
1165 args
->hide_stderr
= true;
1166 do_test_validate_uuid(args
, false);
1169 static void test_validate_uuid_dst_not_set(void)
1171 MigrateStart
*args
= migrate_start_new();
1173 g_free(args
->opts_source
);
1174 args
->opts_source
= g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1175 args
->hide_stderr
= true;
1176 do_test_validate_uuid(args
, false);
1179 static void test_migrate_auto_converge(void)
1181 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1182 MigrateStart
*args
= migrate_start_new();
1183 QTestState
*from
, *to
;
1184 int64_t remaining
, percentage
;
1187 * We want the test to be stable and as fast as possible.
1188 * E.g., with 1Gb/s bandwith migration may pass without throttling,
1189 * so we need to decrease a bandwidth.
1191 const int64_t init_pct
= 5, inc_pct
= 50, max_pct
= 95;
1192 const int64_t max_bandwidth
= 400000000; /* ~400Mb/s */
1193 const int64_t downtime_limit
= 250; /* 250ms */
1195 * We migrate through unix-socket (> 500Mb/s).
1196 * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
1197 * So, we can predict expected_threshold
1199 const int64_t expected_threshold
= max_bandwidth
* downtime_limit
/ 1000;
1201 if (test_migrate_start(&from
, &to
, uri
, args
)) {
1205 migrate_set_capability(from
, "auto-converge", true);
1206 migrate_set_parameter_int(from
, "cpu-throttle-initial", init_pct
);
1207 migrate_set_parameter_int(from
, "cpu-throttle-increment", inc_pct
);
1208 migrate_set_parameter_int(from
, "max-cpu-throttle", max_pct
);
1211 * Set the initial parameters so that the migration could not converge
1212 * without throttling.
1214 migrate_set_parameter_int(from
, "downtime-limit", 1);
1215 migrate_set_parameter_int(from
, "max-bandwidth", 100000000); /* ~100Mb/s */
1217 /* To check remaining size after precopy */
1218 migrate_set_capability(from
, "pause-before-switchover", true);
1220 /* Wait for the first serial output from the source */
1221 wait_for_serial("src_serial");
1223 migrate_qmp(from
, uri
, "{}");
1225 /* Wait for throttling begins */
1227 while (percentage
== 0) {
1228 percentage
= read_migrate_property_int(from
, "cpu-throttle-percentage");
1230 g_assert_false(got_stop
);
1232 /* The first percentage of throttling should be equal to init_pct */
1233 g_assert_cmpint(percentage
, ==, init_pct
);
1234 /* Now, when we tested that throttling works, let it converge */
1235 migrate_set_parameter_int(from
, "downtime-limit", downtime_limit
);
1236 migrate_set_parameter_int(from
, "max-bandwidth", max_bandwidth
);
1239 * Wait for pre-switchover status to check last throttle percentage
1240 * and remaining. These values will be zeroed later
1242 wait_for_migration_status(from
, "pre-switchover", NULL
);
1244 /* The final percentage of throttling shouldn't be greater than max_pct */
1245 percentage
= read_migrate_property_int(from
, "cpu-throttle-percentage");
1246 g_assert_cmpint(percentage
, <=, max_pct
);
1248 remaining
= read_ram_property_int(from
, "remaining");
1249 g_assert_cmpint(remaining
, <,
1250 (expected_threshold
+ expected_threshold
/ 100));
1252 migrate_continue(from
, "pre-switchover");
1254 qtest_qmp_eventwait(to
, "RESUME");
1256 wait_for_serial("dest_serial");
1257 wait_for_migration_complete(from
);
1261 test_migrate_end(from
, to
, true);
1264 static void test_multifd_tcp(void)
1266 MigrateStart
*args
= migrate_start_new();
1267 QTestState
*from
, *to
;
1271 if (test_migrate_start(&from
, &to
, "defer", args
)) {
1276 * We want to pick a speed slow enough that the test completes
1277 * quickly, but that it doesn't complete precopy even on a slow
1278 * machine, so also set the downtime.
1280 /* 1 ms should make it not converge*/
1281 migrate_set_parameter_int(from
, "downtime-limit", 1);
1283 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
1285 migrate_set_parameter_int(from
, "multifd-channels", 16);
1286 migrate_set_parameter_int(to
, "multifd-channels", 16);
1288 migrate_set_capability(from
, "multifd", "true");
1289 migrate_set_capability(to
, "multifd", "true");
1291 /* Start incoming migration from the 1st socket */
1292 rsp
= wait_command(to
, "{ 'execute': 'migrate-incoming',"
1293 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1296 /* Wait for the first serial output from the source */
1297 wait_for_serial("src_serial");
1299 uri
= migrate_get_socket_address(to
, "socket-address");
1301 migrate_qmp(from
, uri
, "{}");
1303 wait_for_migration_pass(from
);
1305 /* 300ms it should converge */
1306 migrate_set_parameter_int(from
, "downtime-limit", 300);
1309 qtest_qmp_eventwait(from
, "STOP");
1311 qtest_qmp_eventwait(to
, "RESUME");
1313 wait_for_serial("dest_serial");
1314 wait_for_migration_complete(from
);
1315 test_migrate_end(from
, to
, true);
1325 * launch another target
1328 * And see that it works
1331 static void test_multifd_tcp_cancel(void)
1333 MigrateStart
*args
= migrate_start_new();
1334 QTestState
*from
, *to
, *to2
;
1338 args
->hide_stderr
= true;
1340 if (test_migrate_start(&from
, &to
, "defer", args
)) {
1345 * We want to pick a speed slow enough that the test completes
1346 * quickly, but that it doesn't complete precopy even on a slow
1347 * machine, so also set the downtime.
1349 /* 1 ms should make it not converge*/
1350 migrate_set_parameter_int(from
, "downtime-limit", 1);
1352 migrate_set_parameter_int(from
, "max-bandwidth", 30000000);
1354 migrate_set_parameter_int(from
, "multifd-channels", 16);
1355 migrate_set_parameter_int(to
, "multifd-channels", 16);
1357 migrate_set_capability(from
, "multifd", "true");
1358 migrate_set_capability(to
, "multifd", "true");
1360 /* Start incoming migration from the 1st socket */
1361 rsp
= wait_command(to
, "{ 'execute': 'migrate-incoming',"
1362 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1365 /* Wait for the first serial output from the source */
1366 wait_for_serial("src_serial");
1368 uri
= migrate_get_socket_address(to
, "socket-address");
1370 migrate_qmp(from
, uri
, "{}");
1372 wait_for_migration_pass(from
);
1374 migrate_cancel(from
);
1376 args
= migrate_start_new();
1377 args
->only_target
= true;
1379 if (test_migrate_start(&from
, &to2
, "defer", args
)) {
1383 migrate_set_parameter_int(to2
, "multifd-channels", 16);
1385 migrate_set_capability(to2
, "multifd", "true");
1387 /* Start incoming migration from the 1st socket */
1388 rsp
= wait_command(to2
, "{ 'execute': 'migrate-incoming',"
1389 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1393 uri
= migrate_get_socket_address(to2
, "socket-address");
1395 wait_for_migration_status(from
, "cancelled", NULL
);
1397 /* 300ms it should converge */
1398 migrate_set_parameter_int(from
, "downtime-limit", 300);
1400 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
1402 migrate_qmp(from
, uri
, "{}");
1404 wait_for_migration_pass(from
);
1407 qtest_qmp_eventwait(from
, "STOP");
1409 qtest_qmp_eventwait(to2
, "RESUME");
1411 wait_for_serial("dest_serial");
1412 wait_for_migration_complete(from
);
1413 test_migrate_end(from
, to2
, true);
1417 int main(int argc
, char **argv
)
1419 char template[] = "/tmp/migration-test-XXXXXX";
1422 g_test_init(&argc
, &argv
, NULL
);
1424 if (!ufd_version_check()) {
1425 return g_test_run();
1429 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
1430 * is touchy due to race conditions on dirty bits (especially on PPC for
1433 if (g_str_equal(qtest_get_arch(), "ppc64") &&
1434 (access("/sys/module/kvm_hv", F_OK
) ||
1435 access("/dev/kvm", R_OK
| W_OK
))) {
1436 g_test_message("Skipping test: kvm_hv not available");
1437 return g_test_run();
1441 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
1442 * there until the problems are resolved
1444 if (g_str_equal(qtest_get_arch(), "s390x")) {
1445 #if defined(HOST_S390X)
1446 if (access("/dev/kvm", R_OK
| W_OK
)) {
1447 g_test_message("Skipping test: kvm not available");
1448 return g_test_run();
1451 g_test_message("Skipping test: Need s390x host to work properly");
1452 return g_test_run();
1456 tmpfs
= mkdtemp(template);
1458 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno
));
1462 module_call_init(MODULE_INIT_QOM
);
1464 qtest_add_func("/migration/postcopy/unix", test_postcopy
);
1465 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery
);
1466 qtest_add_func("/migration/deprecated", test_deprecated
);
1467 qtest_add_func("/migration/bad_dest", test_baddest
);
1468 qtest_add_func("/migration/precopy/unix", test_precopy_unix
);
1469 qtest_add_func("/migration/precopy/tcp", test_precopy_tcp
);
1470 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
1471 qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix
);
1472 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto
);
1473 qtest_add_func("/migration/validate_uuid", test_validate_uuid
);
1474 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error
);
1475 qtest_add_func("/migration/validate_uuid_src_not_set",
1476 test_validate_uuid_src_not_set
);
1477 qtest_add_func("/migration/validate_uuid_dst_not_set",
1478 test_validate_uuid_dst_not_set
);
1480 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge
);
1481 qtest_add_func("/migration/multifd/tcp", test_multifd_tcp
);
1482 qtest_add_func("/migration/multifd/tcp/cancel", test_multifd_tcp_cancel
);
1486 g_assert_cmpint(ret
, ==, 0);
1490 g_test_message("unable to rmdir: path (%s): %s",
1491 tmpfs
, strerror(errno
));