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
332 fprintf(stderr
, "Memory content inconsistency at %x"
333 " first_byte = %x last_byte = %x current = %x"
335 address
, first_byte
, last_byte
, b
, hit_edge
);
341 fprintf(stderr
, "and in another %d pages", bad
- 10);
346 static void cleanup(const char *filename
)
348 char *path
= g_strdup_printf("%s/%s", tmpfs
, filename
);
354 static char *get_shmem_opts(const char *mem_size
, const char *shmem_path
)
356 return g_strdup_printf("-object memory-backend-file,id=mem0,size=%s"
357 ",mem-path=%s,share=on -numa node,memdev=mem0",
358 mem_size
, shmem_path
);
361 static char *SocketAddress_to_str(SocketAddress
*addr
)
363 switch (addr
->type
) {
364 case SOCKET_ADDRESS_TYPE_INET
:
365 return g_strdup_printf("tcp:%s:%s",
368 case SOCKET_ADDRESS_TYPE_UNIX
:
369 return g_strdup_printf("unix:%s",
370 addr
->u
.q_unix
.path
);
371 case SOCKET_ADDRESS_TYPE_FD
:
372 return g_strdup_printf("fd:%s", addr
->u
.fd
.str
);
373 case SOCKET_ADDRESS_TYPE_VSOCK
:
374 return g_strdup_printf("tcp:%s:%s",
378 return g_strdup("unknown address type");
382 static char *migrate_get_socket_address(QTestState
*who
, const char *parameter
)
386 Error
*local_err
= NULL
;
387 SocketAddressList
*addrs
;
391 rsp
= migrate_query(who
);
392 object
= qdict_get(rsp
, parameter
);
394 iv
= qobject_input_visitor_new(object
);
395 visit_type_SocketAddressList(iv
, NULL
, &addrs
, &local_err
);
398 /* we are only using a single address */
399 result
= SocketAddress_to_str(addrs
->value
);
401 qapi_free_SocketAddressList(addrs
);
406 static long long migrate_get_parameter_int(QTestState
*who
,
407 const char *parameter
)
412 rsp
= wait_command(who
, "{ 'execute': 'query-migrate-parameters' }");
413 result
= qdict_get_int(rsp
, parameter
);
418 static void migrate_check_parameter_int(QTestState
*who
, const char *parameter
,
423 result
= migrate_get_parameter_int(who
, parameter
);
424 g_assert_cmpint(result
, ==, value
);
427 static void migrate_set_parameter_int(QTestState
*who
, const char *parameter
,
433 "{ 'execute': 'migrate-set-parameters',"
434 "'arguments': { %s: %lld } }",
436 g_assert(qdict_haskey(rsp
, "return"));
438 migrate_check_parameter_int(who
, parameter
, value
);
441 static void migrate_pause(QTestState
*who
)
445 rsp
= wait_command(who
, "{ 'execute': 'migrate-pause' }");
449 static void migrate_recover(QTestState
*who
, const char *uri
)
453 rsp
= wait_command(who
,
454 "{ 'execute': 'migrate-recover', "
455 " 'id': 'recover-cmd', "
456 " 'arguments': { 'uri': %s } }",
461 static void migrate_set_capability(QTestState
*who
, const char *capability
,
467 "{ 'execute': 'migrate-set-capabilities',"
469 "'capabilities': [ { "
470 "'capability': %s, 'state': %i } ] } }",
472 g_assert(qdict_haskey(rsp
, "return"));
477 * Send QMP command "migrate".
478 * Arguments are built from @fmt... (formatted like
479 * qobject_from_jsonf_nofail()) with "uri": @uri spliced in.
482 static void migrate(QTestState
*who
, const char *uri
, const char *fmt
, ...)
488 args
= qdict_from_vjsonf_nofail(fmt
, ap
);
491 g_assert(!qdict_haskey(args
, "uri"));
492 qdict_put_str(args
, "uri", uri
);
494 rsp
= qmp("{ 'execute': 'migrate', 'arguments': %p}", args
);
496 g_assert(qdict_haskey(rsp
, "return"));
500 static void migrate_postcopy_start(QTestState
*from
, QTestState
*to
)
504 rsp
= wait_command(from
, "{ 'execute': 'migrate-start-postcopy' }");
508 qtest_qmp_eventwait(from
, "STOP");
511 qtest_qmp_eventwait(to
, "RESUME");
514 static int test_migrate_start(QTestState
**from
, QTestState
**to
,
515 const char *uri
, bool hide_stderr
,
518 gchar
*cmd_src
, *cmd_dst
;
519 char *bootpath
= NULL
;
520 char *extra_opts
= NULL
;
521 char *shmem_path
= NULL
;
522 const char *arch
= qtest_get_arch();
523 const char *accel
= "kvm:tcg";
526 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR
)) {
527 g_test_skip("/dev/shm is not supported");
530 shmem_path
= g_strdup_printf("/dev/shm/qemu-%d", getpid());
534 bootpath
= g_strdup_printf("%s/bootsect", tmpfs
);
535 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
536 /* the assembled x86 boot sector should be exactly one sector large */
537 assert(sizeof(x86_bootsect
) == 512);
538 init_bootfile(bootpath
, x86_bootsect
, sizeof(x86_bootsect
));
539 extra_opts
= use_shmem
? get_shmem_opts("150M", shmem_path
) : NULL
;
540 cmd_src
= g_strdup_printf("-machine accel=%s -m 150M"
541 " -name source,debug-threads=on"
542 " -serial file:%s/src_serial"
543 " -drive file=%s,format=raw %s",
544 accel
, tmpfs
, bootpath
,
545 extra_opts
? extra_opts
: "");
546 cmd_dst
= g_strdup_printf("-machine accel=%s -m 150M"
547 " -name target,debug-threads=on"
548 " -serial file:%s/dest_serial"
549 " -drive file=%s,format=raw"
551 accel
, tmpfs
, bootpath
, uri
,
552 extra_opts
? extra_opts
: "");
553 start_address
= X86_TEST_MEM_START
;
554 end_address
= X86_TEST_MEM_END
;
555 } else if (g_str_equal(arch
, "s390x")) {
556 init_bootfile(bootpath
, s390x_elf
, sizeof(s390x_elf
));
557 extra_opts
= use_shmem
? get_shmem_opts("128M", shmem_path
) : NULL
;
558 cmd_src
= g_strdup_printf("-machine accel=%s -m 128M"
559 " -name source,debug-threads=on"
560 " -serial file:%s/src_serial -bios %s %s",
561 accel
, tmpfs
, bootpath
,
562 extra_opts
? extra_opts
: "");
563 cmd_dst
= g_strdup_printf("-machine accel=%s -m 128M"
564 " -name target,debug-threads=on"
565 " -serial file:%s/dest_serial -bios %s"
567 accel
, tmpfs
, bootpath
, uri
,
568 extra_opts
? extra_opts
: "");
569 start_address
= S390_TEST_MEM_START
;
570 end_address
= S390_TEST_MEM_END
;
571 } else if (strcmp(arch
, "ppc64") == 0) {
572 extra_opts
= use_shmem
? get_shmem_opts("256M", shmem_path
) : NULL
;
573 cmd_src
= g_strdup_printf("-machine accel=%s -m 256M -nodefaults"
574 " -name source,debug-threads=on"
575 " -serial file:%s/src_serial"
576 " -prom-env 'use-nvramrc?=true' -prom-env "
577 "'nvramrc=hex .\" _\" begin %x %x "
578 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
579 "until' %s", accel
, tmpfs
, end_address
,
580 start_address
, extra_opts
? extra_opts
: "");
581 cmd_dst
= g_strdup_printf("-machine accel=%s -m 256M"
582 " -name target,debug-threads=on"
583 " -serial file:%s/dest_serial"
586 extra_opts
? extra_opts
: "");
588 start_address
= PPC_TEST_MEM_START
;
589 end_address
= PPC_TEST_MEM_END
;
590 } else if (strcmp(arch
, "aarch64") == 0) {
591 init_bootfile(bootpath
, aarch64_kernel
, sizeof(aarch64_kernel
));
592 extra_opts
= use_shmem
? get_shmem_opts("150M", shmem_path
) : NULL
;
593 cmd_src
= g_strdup_printf("-machine virt,accel=%s,gic-version=max "
594 "-name vmsource,debug-threads=on -cpu max "
595 "-m 150M -serial file:%s/src_serial "
597 accel
, tmpfs
, bootpath
,
598 extra_opts
? extra_opts
: "");
599 cmd_dst
= g_strdup_printf("-machine virt,accel=%s,gic-version=max "
600 "-name vmdest,debug-threads=on -cpu max "
601 "-m 150M -serial file:%s/dest_serial "
604 accel
, tmpfs
, bootpath
, uri
,
605 extra_opts
? extra_opts
: "");
607 start_address
= ARM_TEST_MEM_START
;
608 end_address
= ARM_TEST_MEM_END
;
610 g_assert(sizeof(aarch64_kernel
) <= ARM_TEST_MAX_KERNEL_SIZE
);
612 g_assert_not_reached();
620 tmp
= g_strdup_printf("%s 2>/dev/null", cmd_src
);
624 tmp
= g_strdup_printf("%s 2>/dev/null", cmd_dst
);
629 *from
= qtest_start(cmd_src
);
632 *to
= qtest_init(cmd_dst
);
636 * Remove shmem file immediately to avoid memory leak in test failed case.
637 * It's valid becase QEMU has already opened this file
647 static void test_migrate_end(QTestState
*from
, QTestState
*to
, bool test_dest
)
649 unsigned char dest_byte_a
, dest_byte_b
, dest_byte_c
, dest_byte_d
;
654 qtest_memread(to
, start_address
, &dest_byte_a
, 1);
656 /* Destination still running, wait for a byte to change */
658 qtest_memread(to
, start_address
, &dest_byte_b
, 1);
660 } while (dest_byte_a
== dest_byte_b
);
662 qtest_qmp_discard_response(to
, "{ 'execute' : 'stop'}");
664 /* With it stopped, check nothing changes */
665 qtest_memread(to
, start_address
, &dest_byte_c
, 1);
667 qtest_memread(to
, start_address
, &dest_byte_d
, 1);
668 g_assert_cmpint(dest_byte_c
, ==, dest_byte_d
);
670 check_guests_ram(to
);
676 cleanup("migsocket");
677 cleanup("src_serial");
678 cleanup("dest_serial");
681 static void deprecated_set_downtime(QTestState
*who
, const double value
)
686 "{ 'execute': 'migrate_set_downtime',"
687 " 'arguments': { 'value': %f } }", value
);
688 g_assert(qdict_haskey(rsp
, "return"));
690 migrate_check_parameter_int(who
, "downtime-limit", value
* 1000);
693 static void deprecated_set_speed(QTestState
*who
, long long value
)
697 rsp
= qtest_qmp(who
, "{ 'execute': 'migrate_set_speed',"
698 "'arguments': { 'value': %lld } }", value
);
699 g_assert(qdict_haskey(rsp
, "return"));
701 migrate_check_parameter_int(who
, "max-bandwidth", value
);
704 static void deprecated_set_cache_size(QTestState
*who
, long long value
)
708 rsp
= qtest_qmp(who
, "{ 'execute': 'migrate-set-cache-size',"
709 "'arguments': { 'value': %lld } }", value
);
710 g_assert(qdict_haskey(rsp
, "return"));
712 migrate_check_parameter_int(who
, "xbzrle-cache-size", value
);
715 static void test_deprecated(void)
719 from
= qtest_start("-machine none");
721 deprecated_set_downtime(from
, 0.12345);
722 deprecated_set_speed(from
, 12345);
723 deprecated_set_cache_size(from
, 4096);
728 static int migrate_postcopy_prepare(QTestState
**from_ptr
,
732 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
733 QTestState
*from
, *to
;
735 if (test_migrate_start(&from
, &to
, uri
, hide_error
, false)) {
739 migrate_set_capability(from
, "postcopy-ram", true);
740 migrate_set_capability(to
, "postcopy-ram", true);
741 migrate_set_capability(to
, "postcopy-blocktime", true);
743 /* We want to pick a speed slow enough that the test completes
744 * quickly, but that it doesn't complete precopy even on a slow
745 * machine, so also set the downtime.
747 migrate_set_parameter_int(from
, "max-bandwidth", 100000000);
748 migrate_set_parameter_int(from
, "downtime-limit", 1);
750 /* Wait for the first serial output from the source */
751 wait_for_serial("src_serial");
753 migrate(from
, uri
, "{}");
756 wait_for_migration_pass(from
);
764 static void migrate_postcopy_complete(QTestState
*from
, QTestState
*to
)
766 wait_for_migration_complete(from
);
768 /* Make sure we get at least one "B" on destination */
769 wait_for_serial("dest_serial");
771 if (uffd_feature_thread_id
) {
775 test_migrate_end(from
, to
, true);
778 static void test_postcopy(void)
780 QTestState
*from
, *to
;
782 if (migrate_postcopy_prepare(&from
, &to
, false)) {
785 migrate_postcopy_start(from
, to
);
786 migrate_postcopy_complete(from
, to
);
789 static void test_postcopy_recovery(void)
791 QTestState
*from
, *to
;
794 if (migrate_postcopy_prepare(&from
, &to
, true)) {
798 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
799 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 4096);
801 /* Now we start the postcopy */
802 migrate_postcopy_start(from
, to
);
805 * Wait until postcopy is really started; we can only run the
806 * migrate-pause command during a postcopy
808 wait_for_migration_status(from
, "postcopy-active");
811 * Manually stop the postcopy migration. This emulates a network
812 * failure with the migration socket
817 * Wait for destination side to reach postcopy-paused state. The
818 * migrate-recover command can only succeed if destination machine
819 * is in the paused state
821 wait_for_migration_status(to
, "postcopy-paused");
824 * Create a new socket to emulate a new channel that is different
825 * from the broken migration channel; tell the destination to
826 * listen to the new port
828 uri
= g_strdup_printf("unix:%s/migsocket-recover", tmpfs
);
829 migrate_recover(to
, uri
);
832 * Try to rebuild the migration channel using the resume flag and
833 * the newly created channel
835 wait_for_migration_status(from
, "postcopy-paused");
836 migrate(from
, uri
, "{'resume': true}");
839 /* Restore the postcopy bandwidth to unlimited */
840 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 0);
842 migrate_postcopy_complete(from
, to
);
845 static void test_baddest(void)
847 QTestState
*from
, *to
;
852 if (test_migrate_start(&from
, &to
, "tcp:0:0", true, false)) {
855 migrate(from
, "tcp:0:0", "{}");
857 status
= migrate_query_status(from
);
858 g_assert(!strcmp(status
, "setup") || !(strcmp(status
, "failed")));
859 failed
= !strcmp(status
, "failed");
863 /* Is the machine currently running? */
864 rsp_return
= wait_command(from
, "{ 'execute': 'query-status' }");
865 g_assert(qdict_haskey(rsp_return
, "running"));
866 g_assert(qdict_get_bool(rsp_return
, "running"));
867 qobject_unref(rsp_return
);
869 test_migrate_end(from
, to
, false);
872 static void test_precopy_unix(void)
874 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
875 QTestState
*from
, *to
;
877 if (test_migrate_start(&from
, &to
, uri
, false, false)) {
881 /* We want to pick a speed slow enough that the test completes
882 * quickly, but that it doesn't complete precopy even on a slow
883 * machine, so also set the downtime.
885 /* 1 ms should make it not converge*/
886 migrate_set_parameter_int(from
, "downtime-limit", 1);
888 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
890 /* Wait for the first serial output from the source */
891 wait_for_serial("src_serial");
893 migrate(from
, uri
, "{}");
895 wait_for_migration_pass(from
);
897 /* 300 ms should converge */
898 migrate_set_parameter_int(from
, "downtime-limit", 300);
901 qtest_qmp_eventwait(from
, "STOP");
904 qtest_qmp_eventwait(to
, "RESUME");
906 wait_for_serial("dest_serial");
907 wait_for_migration_complete(from
);
909 test_migrate_end(from
, to
, true);
914 /* Currently upset on aarch64 TCG */
915 static void test_ignore_shared(void)
917 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
918 QTestState
*from
, *to
;
920 if (test_migrate_start(&from
, &to
, uri
, false, true)) {
924 migrate_set_capability(from
, "x-ignore-shared", true);
925 migrate_set_capability(to
, "x-ignore-shared", true);
927 /* Wait for the first serial output from the source */
928 wait_for_serial("src_serial");
930 migrate(from
, uri
, "{}");
932 wait_for_migration_pass(from
);
935 qtest_qmp_eventwait(from
, "STOP");
938 qtest_qmp_eventwait(to
, "RESUME");
940 wait_for_serial("dest_serial");
941 wait_for_migration_complete(from
);
943 /* Check whether shared RAM has been really skipped */
944 g_assert_cmpint(read_ram_property_int(from
, "transferred"), <, 1024 * 1024);
946 test_migrate_end(from
, to
, true);
951 static void test_xbzrle(const char *uri
)
953 QTestState
*from
, *to
;
955 if (test_migrate_start(&from
, &to
, uri
, false, false)) {
960 * We want to pick a speed slow enough that the test completes
961 * quickly, but that it doesn't complete precopy even on a slow
962 * machine, so also set the downtime.
964 /* 1 ms should make it not converge*/
965 migrate_set_parameter_int(from
, "downtime-limit", 1);
967 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
969 migrate_set_parameter_int(from
, "xbzrle-cache-size", 33554432);
971 migrate_set_capability(from
, "xbzrle", "true");
972 migrate_set_capability(to
, "xbzrle", "true");
973 /* Wait for the first serial output from the source */
974 wait_for_serial("src_serial");
976 migrate(from
, uri
, "{}");
978 wait_for_migration_pass(from
);
980 /* 300ms should converge */
981 migrate_set_parameter_int(from
, "downtime-limit", 300);
984 qtest_qmp_eventwait(from
, "STOP");
986 qtest_qmp_eventwait(to
, "RESUME");
988 wait_for_serial("dest_serial");
989 wait_for_migration_complete(from
);
991 test_migrate_end(from
, to
, true);
994 static void test_xbzrle_unix(void)
996 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1002 static void test_precopy_tcp(void)
1005 QTestState
*from
, *to
;
1007 if (test_migrate_start(&from
, &to
, "tcp:127.0.0.1:0", false, false)) {
1012 * We want to pick a speed slow enough that the test completes
1013 * quickly, but that it doesn't complete precopy even on a slow
1014 * machine, so also set the downtime.
1016 /* 1 ms should make it not converge*/
1017 migrate_set_parameter_int(from
, "downtime-limit", 1);
1019 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
1021 /* Wait for the first serial output from the source */
1022 wait_for_serial("src_serial");
1024 uri
= migrate_get_socket_address(to
, "socket-address");
1026 migrate(from
, uri
, "{}");
1028 wait_for_migration_pass(from
);
1030 /* 300ms should converge */
1031 migrate_set_parameter_int(from
, "downtime-limit", 300);
1034 qtest_qmp_eventwait(from
, "STOP");
1036 qtest_qmp_eventwait(to
, "RESUME");
1038 wait_for_serial("dest_serial");
1039 wait_for_migration_complete(from
);
1041 test_migrate_end(from
, to
, true);
1045 static void test_migrate_fd_proto(void)
1047 QTestState
*from
, *to
;
1051 const char *error_desc
;
1053 if (test_migrate_start(&from
, &to
, "defer", false, false)) {
1058 * We want to pick a speed slow enough that the test completes
1059 * quickly, but that it doesn't complete precopy even on a slow
1060 * machine, so also set the downtime.
1062 /* 1 ms should make it not converge */
1063 migrate_set_parameter_int(from
, "downtime-limit", 1);
1065 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
1067 /* Wait for the first serial output from the source */
1068 wait_for_serial("src_serial");
1070 /* Create two connected sockets for migration */
1071 ret
= socketpair(PF_LOCAL
, SOCK_STREAM
, 0, pair
);
1072 g_assert_cmpint(ret
, ==, 0);
1074 /* Send the 1st socket to the target */
1075 rsp
= wait_command_fd(to
, pair
[0],
1076 "{ 'execute': 'getfd',"
1077 " 'arguments': { 'fdname': 'fd-mig' }}");
1081 /* Start incoming migration from the 1st socket */
1082 rsp
= wait_command(to
, "{ 'execute': 'migrate-incoming',"
1083 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1086 /* Send the 2nd socket to the target */
1087 rsp
= wait_command_fd(from
, pair
[1],
1088 "{ 'execute': 'getfd',"
1089 " 'arguments': { 'fdname': 'fd-mig' }}");
1093 /* Start migration to the 2nd socket*/
1094 migrate(from
, "fd:fd-mig", "{}");
1096 wait_for_migration_pass(from
);
1098 /* 300ms should converge */
1099 migrate_set_parameter_int(from
, "downtime-limit", 300);
1102 qtest_qmp_eventwait(from
, "STOP");
1104 qtest_qmp_eventwait(to
, "RESUME");
1106 /* Test closing fds */
1107 /* We assume, that QEMU removes named fd from its list,
1108 * so this should fail */
1109 rsp
= qtest_qmp(from
, "{ 'execute': 'closefd',"
1110 " 'arguments': { 'fdname': 'fd-mig' }}");
1111 g_assert_true(qdict_haskey(rsp
, "error"));
1112 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
1113 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
1116 rsp
= qtest_qmp(to
, "{ 'execute': 'closefd',"
1117 " 'arguments': { 'fdname': 'fd-mig' }}");
1118 g_assert_true(qdict_haskey(rsp
, "error"));
1119 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
1120 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
1123 /* Complete migration */
1124 wait_for_serial("dest_serial");
1125 wait_for_migration_complete(from
);
1126 test_migrate_end(from
, to
, true);
1129 int main(int argc
, char **argv
)
1131 char template[] = "/tmp/migration-test-XXXXXX";
1134 g_test_init(&argc
, &argv
, NULL
);
1136 if (!ufd_version_check()) {
1137 return g_test_run();
1141 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
1142 * is touchy due to race conditions on dirty bits (especially on PPC for
1145 if (g_str_equal(qtest_get_arch(), "ppc64") &&
1146 access("/sys/module/kvm_hv", F_OK
)) {
1147 g_test_message("Skipping test: kvm_hv not available");
1148 return g_test_run();
1152 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
1153 * there until the problems are resolved
1155 if (g_str_equal(qtest_get_arch(), "s390x")) {
1156 #if defined(HOST_S390X)
1157 if (access("/dev/kvm", R_OK
| W_OK
)) {
1158 g_test_message("Skipping test: kvm not available");
1159 return g_test_run();
1162 g_test_message("Skipping test: Need s390x host to work properly");
1163 return g_test_run();
1167 tmpfs
= mkdtemp(template);
1169 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno
));
1173 module_call_init(MODULE_INIT_QOM
);
1175 qtest_add_func("/migration/postcopy/unix", test_postcopy
);
1176 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery
);
1177 qtest_add_func("/migration/deprecated", test_deprecated
);
1178 qtest_add_func("/migration/bad_dest", test_baddest
);
1179 qtest_add_func("/migration/precopy/unix", test_precopy_unix
);
1180 qtest_add_func("/migration/precopy/tcp", test_precopy_tcp
);
1181 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
1182 qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix
);
1183 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto
);
1187 g_assert_cmpint(ret
, ==, 0);
1191 g_test_message("unable to rmdir: path (%s): %s",
1192 tmpfs
, strerror(errno
));