2 * QTest testcase for migration
4 * Copyright (c) 2016-2018 Red Hat, Inc. and/or its affiliates
5 * based on the vhost-user-test.c that is:
6 * Copyright (c) 2014 Virtual Open Systems Sarl.
8 * This work is licensed under the terms of the GNU GPL, version 2 or later.
9 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
16 #include "qapi/qmp/qdict.h"
17 #include "qapi/qmp/qjson.h"
18 #include "qemu/module.h"
19 #include "qemu/option.h"
20 #include "qemu/range.h"
21 #include "qemu/sockets.h"
22 #include "chardev/char.h"
23 #include "sysemu/sysemu.h"
24 #include "qapi/qapi-visit-sockets.h"
25 #include "qapi/qobject-input-visitor.h"
26 #include "qapi/qobject-output-visitor.h"
28 #include "migration/migration-test.h"
30 /* TODO actually test the results and get rid of this */
31 #define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__))
33 unsigned start_address
;
36 static bool uffd_feature_thread_id
;
38 #if defined(__linux__)
39 #include <sys/syscall.h>
43 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
44 #include <sys/eventfd.h>
45 #include <sys/ioctl.h>
46 #include <linux/userfaultfd.h>
48 static bool ufd_version_check(void)
50 struct uffdio_api api_struct
;
53 int ufd
= syscall(__NR_userfaultfd
, O_CLOEXEC
);
56 g_test_message("Skipping test: userfaultfd not available");
60 api_struct
.api
= UFFD_API
;
61 api_struct
.features
= 0;
62 if (ioctl(ufd
, UFFDIO_API
, &api_struct
)) {
63 g_test_message("Skipping test: UFFDIO_API failed");
66 uffd_feature_thread_id
= api_struct
.features
& UFFD_FEATURE_THREAD_ID
;
68 ioctl_mask
= (__u64
)1 << _UFFDIO_REGISTER
|
69 (__u64
)1 << _UFFDIO_UNREGISTER
;
70 if ((api_struct
.ioctls
& ioctl_mask
) != ioctl_mask
) {
71 g_test_message("Skipping test: Missing userfault feature");
79 static bool ufd_version_check(void)
81 g_test_message("Skipping test: Userfault not available (builtdtime)");
87 static const char *tmpfs
;
89 /* The boot file modifies memory area in [start_address, end_address)
90 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
92 #include "tests/migration/i386/a-b-bootblock.h"
93 #include "tests/migration/aarch64/a-b-kernel.h"
94 #include "tests/migration/s390x/a-b-bios.h"
96 static void init_bootfile(const char *bootpath
, void *content
, size_t len
)
98 FILE *bootfile
= fopen(bootpath
, "wb");
100 g_assert_cmpint(fwrite(content
, len
, 1, bootfile
), ==, 1);
105 * Wait for some output in the serial output file,
106 * we get an 'A' followed by an endless string of 'B's
107 * but on the destination we won't have the A.
109 static void wait_for_serial(const char *side
)
111 char *serialpath
= g_strdup_printf("%s/%s", tmpfs
, side
);
112 FILE *serialfile
= fopen(serialpath
, "r");
113 const char *arch
= qtest_get_arch();
114 int started
= (strcmp(side
, "src_serial") == 0 &&
115 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
119 int readvalue
= fgetc(serialfile
);
122 /* SLOF prints its banner before starting test,
123 * to ignore it, mark the start of the test with '_',
124 * ignore all characters until this marker
131 fseek(serialfile
, 0, SEEK_SET
);
148 started
= (strcmp(side
, "src_serial") == 0 &&
149 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
150 fseek(serialfile
, 0, SEEK_SET
);
155 fprintf(stderr
, "Unexpected %d on %s serial\n", readvalue
, side
);
156 g_assert_not_reached();
161 static void stop_cb(void *opaque
, const char *name
, QDict
*data
)
163 if (!strcmp(name
, "STOP")) {
169 * Events can get in the way of responses we are actually waiting for.
172 static QDict
*wait_command_fd(QTestState
*who
, int fd
, const char *command
, ...)
176 va_start(ap
, command
);
177 qtest_qmp_vsend_fds(who
, &fd
, 1, command
, ap
);
180 return qtest_qmp_receive_success(who
, stop_cb
, NULL
);
184 * Events can get in the way of responses we are actually waiting for.
187 static QDict
*wait_command(QTestState
*who
, const char *command
, ...)
191 va_start(ap
, command
);
192 qtest_qmp_vsend(who
, command
, ap
);
195 return qtest_qmp_receive_success(who
, stop_cb
, NULL
);
199 * Note: caller is responsible to free the returned object via
200 * qobject_unref() after use
202 static QDict
*migrate_query(QTestState
*who
)
204 return wait_command(who
, "{ 'execute': 'query-migrate' }");
208 * Note: caller is responsible to free the returned object via
211 static gchar
*migrate_query_status(QTestState
*who
)
213 QDict
*rsp_return
= migrate_query(who
);
214 gchar
*status
= g_strdup(qdict_get_str(rsp_return
, "status"));
217 qobject_unref(rsp_return
);
223 * It's tricky to use qemu's migration event capability with qtest,
224 * events suddenly appearing confuse the qmp()/hmp() responses.
227 static int64_t read_ram_property_int(QTestState
*who
, const char *property
)
229 QDict
*rsp_return
, *rsp_ram
;
232 rsp_return
= migrate_query(who
);
233 if (!qdict_haskey(rsp_return
, "ram")) {
237 rsp_ram
= qdict_get_qdict(rsp_return
, "ram");
238 result
= qdict_get_try_int(rsp_ram
, property
, 0);
240 qobject_unref(rsp_return
);
244 static uint64_t get_migration_pass(QTestState
*who
)
246 return read_ram_property_int(who
, "dirty-sync-count");
249 static void read_blocktime(QTestState
*who
)
253 rsp_return
= migrate_query(who
);
254 g_assert(qdict_haskey(rsp_return
, "postcopy-blocktime"));
255 qobject_unref(rsp_return
);
258 static void wait_for_migration_status(QTestState
*who
,
265 status
= migrate_query_status(who
);
266 completed
= strcmp(status
, goal
) == 0;
267 g_assert_cmpstr(status
, !=, "failed");
276 static void wait_for_migration_complete(QTestState
*who
)
278 wait_for_migration_status(who
, "completed");
281 static void wait_for_migration_pass(QTestState
*who
)
283 uint64_t initial_pass
= get_migration_pass(who
);
286 /* Wait for the 1st sync */
287 while (!got_stop
&& !initial_pass
) {
289 initial_pass
= get_migration_pass(who
);
294 pass
= get_migration_pass(who
);
295 } while (pass
== initial_pass
&& !got_stop
);
298 static void check_guests_ram(QTestState
*who
)
300 /* Our ASM test will have been incrementing one byte from each page from
301 * start_address to < end_address in order. This gives us a constraint
302 * that any page's byte should be equal or less than the previous pages
303 * byte (mod 256); and they should all be equal except for one transition
304 * at the point where we meet the incrementer. (We're running this with
305 * the guest stopped).
310 bool hit_edge
= false;
313 qtest_memread(who
, start_address
, &first_byte
, 1);
314 last_byte
= first_byte
;
316 for (address
= start_address
+ TEST_MEM_PAGE_SIZE
; address
< end_address
;
317 address
+= TEST_MEM_PAGE_SIZE
)
320 qtest_memread(who
, address
, &b
, 1);
321 if (b
!= last_byte
) {
322 if (((b
+ 1) % 256) == last_byte
&& !hit_edge
) {
323 /* This is OK, the guest stopped at the point of
324 * incrementing the previous page but didn't get
330 fprintf(stderr
, "Memory content inconsistency at %x"
331 " first_byte = %x last_byte = %x current = %x"
333 address
, first_byte
, last_byte
, b
, hit_edge
);
341 static void cleanup(const char *filename
)
343 char *path
= g_strdup_printf("%s/%s", tmpfs
, filename
);
349 static char *get_shmem_opts(const char *mem_size
, const char *shmem_path
)
351 return g_strdup_printf("-object memory-backend-file,id=mem0,size=%s"
352 ",mem-path=%s,share=on -numa node,memdev=mem0",
353 mem_size
, shmem_path
);
356 static char *SocketAddress_to_str(SocketAddress
*addr
)
358 switch (addr
->type
) {
359 case SOCKET_ADDRESS_TYPE_INET
:
360 return g_strdup_printf("tcp:%s:%s",
363 case SOCKET_ADDRESS_TYPE_UNIX
:
364 return g_strdup_printf("unix:%s",
365 addr
->u
.q_unix
.path
);
366 case SOCKET_ADDRESS_TYPE_FD
:
367 return g_strdup_printf("fd:%s", addr
->u
.fd
.str
);
368 case SOCKET_ADDRESS_TYPE_VSOCK
:
369 return g_strdup_printf("tcp:%s:%s",
373 return g_strdup("unknown address type");
377 static char *migrate_get_socket_address(QTestState
*who
, const char *parameter
)
381 Error
*local_err
= NULL
;
382 SocketAddressList
*addrs
;
386 rsp
= migrate_query(who
);
387 object
= qdict_get(rsp
, parameter
);
389 iv
= qobject_input_visitor_new(object
);
390 visit_type_SocketAddressList(iv
, NULL
, &addrs
, &local_err
);
393 /* we are only using a single address */
394 result
= SocketAddress_to_str(addrs
->value
);
396 qapi_free_SocketAddressList(addrs
);
401 static long long migrate_get_parameter_int(QTestState
*who
,
402 const char *parameter
)
407 rsp
= wait_command(who
, "{ 'execute': 'query-migrate-parameters' }");
408 result
= qdict_get_int(rsp
, parameter
);
413 static void migrate_check_parameter_int(QTestState
*who
, const char *parameter
,
418 result
= migrate_get_parameter_int(who
, parameter
);
419 g_assert_cmpint(result
, ==, value
);
422 static void migrate_set_parameter_int(QTestState
*who
, const char *parameter
,
428 "{ 'execute': 'migrate-set-parameters',"
429 "'arguments': { %s: %lld } }",
431 g_assert(qdict_haskey(rsp
, "return"));
433 migrate_check_parameter_int(who
, parameter
, value
);
436 static void migrate_pause(QTestState
*who
)
440 rsp
= wait_command(who
, "{ 'execute': 'migrate-pause' }");
444 static void migrate_recover(QTestState
*who
, const char *uri
)
448 rsp
= wait_command(who
,
449 "{ 'execute': 'migrate-recover', "
450 " 'id': 'recover-cmd', "
451 " 'arguments': { 'uri': %s } }",
456 static void migrate_set_capability(QTestState
*who
, const char *capability
,
462 "{ 'execute': 'migrate-set-capabilities',"
464 "'capabilities': [ { "
465 "'capability': %s, 'state': %i } ] } }",
467 g_assert(qdict_haskey(rsp
, "return"));
472 * Send QMP command "migrate".
473 * Arguments are built from @fmt... (formatted like
474 * qobject_from_jsonf_nofail()) with "uri": @uri spliced in.
477 static void migrate(QTestState
*who
, const char *uri
, const char *fmt
, ...)
483 args
= qdict_from_vjsonf_nofail(fmt
, ap
);
486 g_assert(!qdict_haskey(args
, "uri"));
487 qdict_put_str(args
, "uri", uri
);
489 rsp
= qmp("{ 'execute': 'migrate', 'arguments': %p}", args
);
491 g_assert(qdict_haskey(rsp
, "return"));
495 static void migrate_postcopy_start(QTestState
*from
, QTestState
*to
)
499 rsp
= wait_command(from
, "{ 'execute': 'migrate-start-postcopy' }");
503 qtest_qmp_eventwait(from
, "STOP");
506 qtest_qmp_eventwait(to
, "RESUME");
509 static int test_migrate_start(QTestState
**from
, QTestState
**to
,
510 const char *uri
, bool hide_stderr
,
513 gchar
*cmd_src
, *cmd_dst
;
514 char *bootpath
= NULL
;
515 char *extra_opts
= NULL
;
516 char *shmem_path
= NULL
;
517 const char *arch
= qtest_get_arch();
518 const char *accel
= "kvm:tcg";
521 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR
)) {
522 g_test_skip("/dev/shm is not supported");
525 shmem_path
= g_strdup_printf("/dev/shm/qemu-%d", getpid());
529 bootpath
= g_strdup_printf("%s/bootsect", tmpfs
);
530 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
531 /* the assembled x86 boot sector should be exactly one sector large */
532 assert(sizeof(x86_bootsect
) == 512);
533 init_bootfile(bootpath
, x86_bootsect
, sizeof(x86_bootsect
));
534 extra_opts
= use_shmem
? get_shmem_opts("150M", shmem_path
) : NULL
;
535 cmd_src
= g_strdup_printf("-machine accel=%s -m 150M"
536 " -name source,debug-threads=on"
537 " -serial file:%s/src_serial"
538 " -drive file=%s,format=raw %s",
539 accel
, tmpfs
, bootpath
,
540 extra_opts
? extra_opts
: "");
541 cmd_dst
= g_strdup_printf("-machine accel=%s -m 150M"
542 " -name target,debug-threads=on"
543 " -serial file:%s/dest_serial"
544 " -drive file=%s,format=raw"
546 accel
, tmpfs
, bootpath
, uri
,
547 extra_opts
? extra_opts
: "");
548 start_address
= X86_TEST_MEM_START
;
549 end_address
= X86_TEST_MEM_END
;
550 } else if (g_str_equal(arch
, "s390x")) {
551 init_bootfile(bootpath
, s390x_elf
, sizeof(s390x_elf
));
552 extra_opts
= use_shmem
? get_shmem_opts("128M", shmem_path
) : NULL
;
553 cmd_src
= g_strdup_printf("-machine accel=%s -m 128M"
554 " -name source,debug-threads=on"
555 " -serial file:%s/src_serial -bios %s %s",
556 accel
, tmpfs
, bootpath
,
557 extra_opts
? extra_opts
: "");
558 cmd_dst
= g_strdup_printf("-machine accel=%s -m 128M"
559 " -name target,debug-threads=on"
560 " -serial file:%s/dest_serial -bios %s"
562 accel
, tmpfs
, bootpath
, uri
,
563 extra_opts
? extra_opts
: "");
564 start_address
= S390_TEST_MEM_START
;
565 end_address
= S390_TEST_MEM_END
;
566 } else if (strcmp(arch
, "ppc64") == 0) {
567 extra_opts
= use_shmem
? get_shmem_opts("256M", shmem_path
) : NULL
;
568 cmd_src
= g_strdup_printf("-machine accel=%s -m 256M -nodefaults"
569 " -name source,debug-threads=on"
570 " -serial file:%s/src_serial"
571 " -prom-env 'use-nvramrc?=true' -prom-env "
572 "'nvramrc=hex .\" _\" begin %x %x "
573 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
574 "until' %s", accel
, tmpfs
, end_address
,
575 start_address
, extra_opts
? extra_opts
: "");
576 cmd_dst
= g_strdup_printf("-machine accel=%s -m 256M"
577 " -name target,debug-threads=on"
578 " -serial file:%s/dest_serial"
581 extra_opts
? extra_opts
: "");
583 start_address
= PPC_TEST_MEM_START
;
584 end_address
= PPC_TEST_MEM_END
;
585 } else if (strcmp(arch
, "aarch64") == 0) {
586 init_bootfile(bootpath
, aarch64_kernel
, sizeof(aarch64_kernel
));
587 extra_opts
= use_shmem
? get_shmem_opts("150M", shmem_path
) : NULL
;
588 cmd_src
= g_strdup_printf("-machine virt,accel=%s,gic-version=max "
589 "-name vmsource,debug-threads=on -cpu max "
590 "-m 150M -serial file:%s/src_serial "
592 accel
, tmpfs
, bootpath
,
593 extra_opts
? extra_opts
: "");
594 cmd_dst
= g_strdup_printf("-machine virt,accel=%s,gic-version=max "
595 "-name vmdest,debug-threads=on -cpu max "
596 "-m 150M -serial file:%s/dest_serial "
599 accel
, tmpfs
, bootpath
, uri
,
600 extra_opts
? extra_opts
: "");
602 start_address
= ARM_TEST_MEM_START
;
603 end_address
= ARM_TEST_MEM_END
;
605 g_assert(sizeof(aarch64_kernel
) <= ARM_TEST_MAX_KERNEL_SIZE
);
607 g_assert_not_reached();
615 tmp
= g_strdup_printf("%s 2>/dev/null", cmd_src
);
619 tmp
= g_strdup_printf("%s 2>/dev/null", cmd_dst
);
624 *from
= qtest_start(cmd_src
);
627 *to
= qtest_init(cmd_dst
);
631 * Remove shmem file immediately to avoid memory leak in test failed case.
632 * It's valid becase QEMU has already opened this file
642 static void test_migrate_end(QTestState
*from
, QTestState
*to
, bool test_dest
)
644 unsigned char dest_byte_a
, dest_byte_b
, dest_byte_c
, dest_byte_d
;
649 qtest_memread(to
, start_address
, &dest_byte_a
, 1);
651 /* Destination still running, wait for a byte to change */
653 qtest_memread(to
, start_address
, &dest_byte_b
, 1);
655 } while (dest_byte_a
== dest_byte_b
);
657 qtest_qmp_discard_response(to
, "{ 'execute' : 'stop'}");
659 /* With it stopped, check nothing changes */
660 qtest_memread(to
, start_address
, &dest_byte_c
, 1);
662 qtest_memread(to
, start_address
, &dest_byte_d
, 1);
663 g_assert_cmpint(dest_byte_c
, ==, dest_byte_d
);
665 check_guests_ram(to
);
671 cleanup("migsocket");
672 cleanup("src_serial");
673 cleanup("dest_serial");
676 static void deprecated_set_downtime(QTestState
*who
, const double value
)
681 "{ 'execute': 'migrate_set_downtime',"
682 " 'arguments': { 'value': %f } }", value
);
683 g_assert(qdict_haskey(rsp
, "return"));
685 migrate_check_parameter_int(who
, "downtime-limit", value
* 1000);
688 static void deprecated_set_speed(QTestState
*who
, long long value
)
692 rsp
= qtest_qmp(who
, "{ 'execute': 'migrate_set_speed',"
693 "'arguments': { 'value': %lld } }", value
);
694 g_assert(qdict_haskey(rsp
, "return"));
696 migrate_check_parameter_int(who
, "max-bandwidth", value
);
699 static void deprecated_set_cache_size(QTestState
*who
, long long value
)
703 rsp
= qtest_qmp(who
, "{ 'execute': 'migrate-set-cache-size',"
704 "'arguments': { 'value': %lld } }", value
);
705 g_assert(qdict_haskey(rsp
, "return"));
707 migrate_check_parameter_int(who
, "xbzrle-cache-size", value
);
710 static void test_deprecated(void)
714 from
= qtest_start("-machine none");
716 deprecated_set_downtime(from
, 0.12345);
717 deprecated_set_speed(from
, 12345);
718 deprecated_set_cache_size(from
, 4096);
723 static int migrate_postcopy_prepare(QTestState
**from_ptr
,
727 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
728 QTestState
*from
, *to
;
730 if (test_migrate_start(&from
, &to
, uri
, hide_error
, false)) {
734 migrate_set_capability(from
, "postcopy-ram", true);
735 migrate_set_capability(to
, "postcopy-ram", true);
736 migrate_set_capability(to
, "postcopy-blocktime", true);
738 /* We want to pick a speed slow enough that the test completes
739 * quickly, but that it doesn't complete precopy even on a slow
740 * machine, so also set the downtime.
742 migrate_set_parameter_int(from
, "max-bandwidth", 100000000);
743 migrate_set_parameter_int(from
, "downtime-limit", 1);
745 /* Wait for the first serial output from the source */
746 wait_for_serial("src_serial");
748 migrate(from
, uri
, "{}");
751 wait_for_migration_pass(from
);
759 static void migrate_postcopy_complete(QTestState
*from
, QTestState
*to
)
761 wait_for_migration_complete(from
);
763 /* Make sure we get at least one "B" on destination */
764 wait_for_serial("dest_serial");
766 if (uffd_feature_thread_id
) {
770 test_migrate_end(from
, to
, true);
773 static void test_postcopy(void)
775 QTestState
*from
, *to
;
777 if (migrate_postcopy_prepare(&from
, &to
, false)) {
780 migrate_postcopy_start(from
, to
);
781 migrate_postcopy_complete(from
, to
);
784 static void test_postcopy_recovery(void)
786 QTestState
*from
, *to
;
789 if (migrate_postcopy_prepare(&from
, &to
, true)) {
793 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
794 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 4096);
796 /* Now we start the postcopy */
797 migrate_postcopy_start(from
, to
);
800 * Wait until postcopy is really started; we can only run the
801 * migrate-pause command during a postcopy
803 wait_for_migration_status(from
, "postcopy-active");
806 * Manually stop the postcopy migration. This emulates a network
807 * failure with the migration socket
812 * Wait for destination side to reach postcopy-paused state. The
813 * migrate-recover command can only succeed if destination machine
814 * is in the paused state
816 wait_for_migration_status(to
, "postcopy-paused");
819 * Create a new socket to emulate a new channel that is different
820 * from the broken migration channel; tell the destination to
821 * listen to the new port
823 uri
= g_strdup_printf("unix:%s/migsocket-recover", tmpfs
);
824 migrate_recover(to
, uri
);
827 * Try to rebuild the migration channel using the resume flag and
828 * the newly created channel
830 wait_for_migration_status(from
, "postcopy-paused");
831 migrate(from
, uri
, "{'resume': true}");
834 /* Restore the postcopy bandwidth to unlimited */
835 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 0);
837 migrate_postcopy_complete(from
, to
);
840 static void test_baddest(void)
842 QTestState
*from
, *to
;
847 if (test_migrate_start(&from
, &to
, "tcp:0:0", true, false)) {
850 migrate(from
, "tcp:0:0", "{}");
852 status
= migrate_query_status(from
);
853 g_assert(!strcmp(status
, "setup") || !(strcmp(status
, "failed")));
854 failed
= !strcmp(status
, "failed");
858 /* Is the machine currently running? */
859 rsp_return
= wait_command(from
, "{ 'execute': 'query-status' }");
860 g_assert(qdict_haskey(rsp_return
, "running"));
861 g_assert(qdict_get_bool(rsp_return
, "running"));
862 qobject_unref(rsp_return
);
864 test_migrate_end(from
, to
, false);
867 static void test_precopy_unix(void)
869 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
870 QTestState
*from
, *to
;
872 if (test_migrate_start(&from
, &to
, uri
, false, false)) {
876 /* We want to pick a speed slow enough that the test completes
877 * quickly, but that it doesn't complete precopy even on a slow
878 * machine, so also set the downtime.
880 /* 1 ms should make it not converge*/
881 migrate_set_parameter_int(from
, "downtime-limit", 1);
883 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
885 /* Wait for the first serial output from the source */
886 wait_for_serial("src_serial");
888 migrate(from
, uri
, "{}");
890 wait_for_migration_pass(from
);
892 /* 300 ms should converge */
893 migrate_set_parameter_int(from
, "downtime-limit", 300);
896 qtest_qmp_eventwait(from
, "STOP");
899 qtest_qmp_eventwait(to
, "RESUME");
901 wait_for_serial("dest_serial");
902 wait_for_migration_complete(from
);
904 test_migrate_end(from
, to
, true);
909 /* Currently upset on aarch64 TCG */
910 static void test_ignore_shared(void)
912 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
913 QTestState
*from
, *to
;
915 if (test_migrate_start(&from
, &to
, uri
, false, true)) {
919 migrate_set_capability(from
, "x-ignore-shared", true);
920 migrate_set_capability(to
, "x-ignore-shared", true);
922 /* Wait for the first serial output from the source */
923 wait_for_serial("src_serial");
925 migrate(from
, uri
, "{}");
927 wait_for_migration_pass(from
);
930 qtest_qmp_eventwait(from
, "STOP");
933 qtest_qmp_eventwait(to
, "RESUME");
935 wait_for_serial("dest_serial");
936 wait_for_migration_complete(from
);
938 /* Check whether shared RAM has been really skipped */
939 g_assert_cmpint(read_ram_property_int(from
, "transferred"), <, 1024 * 1024);
941 test_migrate_end(from
, to
, true);
946 static void test_xbzrle(const char *uri
)
948 QTestState
*from
, *to
;
950 if (test_migrate_start(&from
, &to
, uri
, false, false)) {
955 * We want to pick a speed slow enough that the test completes
956 * quickly, but that it doesn't complete precopy even on a slow
957 * machine, so also set the downtime.
959 /* 1 ms should make it not converge*/
960 migrate_set_parameter_int(from
, "downtime-limit", 1);
962 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
964 migrate_set_parameter_int(from
, "xbzrle-cache-size", 33554432);
966 migrate_set_capability(from
, "xbzrle", "true");
967 migrate_set_capability(to
, "xbzrle", "true");
968 /* Wait for the first serial output from the source */
969 wait_for_serial("src_serial");
971 migrate(from
, uri
, "{}");
973 wait_for_migration_pass(from
);
975 /* 300ms should converge */
976 migrate_set_parameter_int(from
, "downtime-limit", 300);
979 qtest_qmp_eventwait(from
, "STOP");
981 qtest_qmp_eventwait(to
, "RESUME");
983 wait_for_serial("dest_serial");
984 wait_for_migration_complete(from
);
986 test_migrate_end(from
, to
, true);
989 static void test_xbzrle_unix(void)
991 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
997 static void test_precopy_tcp(void)
1000 QTestState
*from
, *to
;
1002 if (test_migrate_start(&from
, &to
, "tcp:127.0.0.1:0", false, false)) {
1007 * We want to pick a speed slow enough that the test completes
1008 * quickly, but that it doesn't complete precopy even on a slow
1009 * machine, so also set the downtime.
1011 /* 1 ms should make it not converge*/
1012 migrate_set_parameter_int(from
, "downtime-limit", 1);
1014 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
1016 /* Wait for the first serial output from the source */
1017 wait_for_serial("src_serial");
1019 uri
= migrate_get_socket_address(to
, "socket-address");
1021 migrate(from
, uri
, "{}");
1023 wait_for_migration_pass(from
);
1025 /* 300ms should converge */
1026 migrate_set_parameter_int(from
, "downtime-limit", 300);
1029 qtest_qmp_eventwait(from
, "STOP");
1031 qtest_qmp_eventwait(to
, "RESUME");
1033 wait_for_serial("dest_serial");
1034 wait_for_migration_complete(from
);
1036 test_migrate_end(from
, to
, true);
1040 static void test_migrate_fd_proto(void)
1042 QTestState
*from
, *to
;
1046 const char *error_desc
;
1048 if (test_migrate_start(&from
, &to
, "defer", false, false)) {
1053 * We want to pick a speed slow enough that the test completes
1054 * quickly, but that it doesn't complete precopy even on a slow
1055 * machine, so also set the downtime.
1057 /* 1 ms should make it not converge */
1058 migrate_set_parameter_int(from
, "downtime-limit", 1);
1060 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
1062 /* Wait for the first serial output from the source */
1063 wait_for_serial("src_serial");
1065 /* Create two connected sockets for migration */
1066 ret
= socketpair(PF_LOCAL
, SOCK_STREAM
, 0, pair
);
1067 g_assert_cmpint(ret
, ==, 0);
1069 /* Send the 1st socket to the target */
1070 rsp
= wait_command_fd(to
, pair
[0],
1071 "{ 'execute': 'getfd',"
1072 " 'arguments': { 'fdname': 'fd-mig' }}");
1076 /* Start incoming migration from the 1st socket */
1077 rsp
= wait_command(to
, "{ 'execute': 'migrate-incoming',"
1078 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1081 /* Send the 2nd socket to the target */
1082 rsp
= wait_command_fd(from
, pair
[1],
1083 "{ 'execute': 'getfd',"
1084 " 'arguments': { 'fdname': 'fd-mig' }}");
1088 /* Start migration to the 2nd socket*/
1089 migrate(from
, "fd:fd-mig", "{}");
1091 wait_for_migration_pass(from
);
1093 /* 300ms should converge */
1094 migrate_set_parameter_int(from
, "downtime-limit", 300);
1097 qtest_qmp_eventwait(from
, "STOP");
1099 qtest_qmp_eventwait(to
, "RESUME");
1101 /* Test closing fds */
1102 /* We assume, that QEMU removes named fd from its list,
1103 * so this should fail */
1104 rsp
= qtest_qmp(from
, "{ 'execute': 'closefd',"
1105 " 'arguments': { 'fdname': 'fd-mig' }}");
1106 g_assert_true(qdict_haskey(rsp
, "error"));
1107 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
1108 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
1111 rsp
= qtest_qmp(to
, "{ 'execute': 'closefd',"
1112 " 'arguments': { 'fdname': 'fd-mig' }}");
1113 g_assert_true(qdict_haskey(rsp
, "error"));
1114 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
1115 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
1118 /* Complete migration */
1119 wait_for_serial("dest_serial");
1120 wait_for_migration_complete(from
);
1121 test_migrate_end(from
, to
, true);
1124 int main(int argc
, char **argv
)
1126 char template[] = "/tmp/migration-test-XXXXXX";
1129 g_test_init(&argc
, &argv
, NULL
);
1131 if (!ufd_version_check()) {
1132 return g_test_run();
1136 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
1137 * is touchy due to race conditions on dirty bits (especially on PPC for
1140 if (g_str_equal(qtest_get_arch(), "ppc64") &&
1141 access("/sys/module/kvm_hv", F_OK
)) {
1142 g_test_message("Skipping test: kvm_hv not available");
1143 return g_test_run();
1147 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
1148 * there until the problems are resolved
1150 if (g_str_equal(qtest_get_arch(), "s390x")) {
1151 #if defined(HOST_S390X)
1152 if (access("/dev/kvm", R_OK
| W_OK
)) {
1153 g_test_message("Skipping test: kvm not available");
1154 return g_test_run();
1157 g_test_message("Skipping test: Need s390x host to work properly");
1158 return g_test_run();
1162 tmpfs
= mkdtemp(template);
1164 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno
));
1168 module_call_init(MODULE_INIT_QOM
);
1170 qtest_add_func("/migration/postcopy/unix", test_postcopy
);
1171 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery
);
1172 qtest_add_func("/migration/deprecated", test_deprecated
);
1173 qtest_add_func("/migration/bad_dest", test_baddest
);
1174 qtest_add_func("/migration/precopy/unix", test_precopy_unix
);
1175 qtest_add_func("/migration/precopy/tcp", test_precopy_tcp
);
1176 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
1177 qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix
);
1178 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto
);
1182 g_assert_cmpint(ret
, ==, 0);
1186 g_test_message("unable to rmdir: path (%s): %s",
1187 tmpfs
, strerror(errno
));