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/option.h"
19 #include "qemu/range.h"
20 #include "qemu/sockets.h"
21 #include "chardev/char.h"
22 #include "sysemu/sysemu.h"
24 #include "migration/migration-test.h"
26 /* TODO actually test the results and get rid of this */
27 #define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__))
29 unsigned start_address
;
32 static bool uffd_feature_thread_id
;
34 #if defined(__linux__)
35 #include <sys/syscall.h>
39 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
40 #include <sys/eventfd.h>
41 #include <sys/ioctl.h>
42 #include <linux/userfaultfd.h>
44 static bool ufd_version_check(void)
46 struct uffdio_api api_struct
;
49 int ufd
= syscall(__NR_userfaultfd
, O_CLOEXEC
);
52 g_test_message("Skipping test: userfaultfd not available");
56 api_struct
.api
= UFFD_API
;
57 api_struct
.features
= 0;
58 if (ioctl(ufd
, UFFDIO_API
, &api_struct
)) {
59 g_test_message("Skipping test: UFFDIO_API failed");
62 uffd_feature_thread_id
= api_struct
.features
& UFFD_FEATURE_THREAD_ID
;
64 ioctl_mask
= (__u64
)1 << _UFFDIO_REGISTER
|
65 (__u64
)1 << _UFFDIO_UNREGISTER
;
66 if ((api_struct
.ioctls
& ioctl_mask
) != ioctl_mask
) {
67 g_test_message("Skipping test: Missing userfault feature");
75 static bool ufd_version_check(void)
77 g_test_message("Skipping test: Userfault not available (builtdtime)");
83 static const char *tmpfs
;
85 /* The boot file modifies memory area in [start_address, end_address)
86 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
88 #include "tests/migration/i386/a-b-bootblock.h"
90 static void init_bootfile_x86(const char *bootpath
)
92 FILE *bootfile
= fopen(bootpath
, "wb");
94 g_assert_cmpint(fwrite(x86_bootsect
, 512, 1, bootfile
), ==, 1);
99 * Wait for some output in the serial output file,
100 * we get an 'A' followed by an endless string of 'B's
101 * but on the destination we won't have the A.
103 static void wait_for_serial(const char *side
)
105 char *serialpath
= g_strdup_printf("%s/%s", tmpfs
, side
);
106 FILE *serialfile
= fopen(serialpath
, "r");
107 const char *arch
= qtest_get_arch();
108 int started
= (strcmp(side
, "src_serial") == 0 &&
109 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
113 int readvalue
= fgetc(serialfile
);
116 /* SLOF prints its banner before starting test,
117 * to ignore it, mark the start of the test with '_',
118 * ignore all characters until this marker
125 fseek(serialfile
, 0, SEEK_SET
);
142 started
= (strcmp(side
, "src_serial") == 0 &&
143 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
144 fseek(serialfile
, 0, SEEK_SET
);
149 fprintf(stderr
, "Unexpected %d on %s serial\n", readvalue
, side
);
150 g_assert_not_reached();
155 static void stop_cb(void *opaque
, const char *name
, QDict
*data
)
157 if (!strcmp(name
, "STOP")) {
163 * Events can get in the way of responses we are actually waiting for.
166 static QDict
*wait_command(QTestState
*who
, const char *command
, ...)
170 va_start(ap
, command
);
171 qtest_qmp_vsend(who
, command
, ap
);
174 return qtest_qmp_receive_success(who
, stop_cb
, NULL
);
178 * Note: caller is responsible to free the returned object via
179 * qobject_unref() after use
181 static QDict
*migrate_query(QTestState
*who
)
183 return wait_command(who
, "{ 'execute': 'query-migrate' }");
187 * Note: caller is responsible to free the returned object via
190 static gchar
*migrate_query_status(QTestState
*who
)
192 QDict
*rsp_return
= migrate_query(who
);
193 gchar
*status
= g_strdup(qdict_get_str(rsp_return
, "status"));
196 qobject_unref(rsp_return
);
202 * It's tricky to use qemu's migration event capability with qtest,
203 * events suddenly appearing confuse the qmp()/hmp() responses.
206 static uint64_t get_migration_pass(QTestState
*who
)
208 QDict
*rsp_return
, *rsp_ram
;
211 rsp_return
= migrate_query(who
);
212 if (!qdict_haskey(rsp_return
, "ram")) {
216 rsp_ram
= qdict_get_qdict(rsp_return
, "ram");
217 result
= qdict_get_try_int(rsp_ram
, "dirty-sync-count", 0);
219 qobject_unref(rsp_return
);
223 static void read_blocktime(QTestState
*who
)
227 rsp_return
= migrate_query(who
);
228 g_assert(qdict_haskey(rsp_return
, "postcopy-blocktime"));
229 qobject_unref(rsp_return
);
232 static void wait_for_migration_status(QTestState
*who
,
239 status
= migrate_query_status(who
);
240 completed
= strcmp(status
, goal
) == 0;
241 g_assert_cmpstr(status
, !=, "failed");
250 static void wait_for_migration_complete(QTestState
*who
)
252 wait_for_migration_status(who
, "completed");
255 static void wait_for_migration_pass(QTestState
*who
)
257 uint64_t initial_pass
= get_migration_pass(who
);
260 /* Wait for the 1st sync */
261 while (!got_stop
&& !initial_pass
) {
263 initial_pass
= get_migration_pass(who
);
268 pass
= get_migration_pass(who
);
269 } while (pass
== initial_pass
&& !got_stop
);
272 static void check_guests_ram(QTestState
*who
)
274 /* Our ASM test will have been incrementing one byte from each page from
275 * start_address to < end_address in order. This gives us a constraint
276 * that any page's byte should be equal or less than the previous pages
277 * byte (mod 256); and they should all be equal except for one transition
278 * at the point where we meet the incrementer. (We're running this with
279 * the guest stopped).
284 bool hit_edge
= false;
287 qtest_memread(who
, start_address
, &first_byte
, 1);
288 last_byte
= first_byte
;
290 for (address
= start_address
+ TEST_MEM_PAGE_SIZE
; address
< end_address
;
291 address
+= TEST_MEM_PAGE_SIZE
)
294 qtest_memread(who
, address
, &b
, 1);
295 if (b
!= last_byte
) {
296 if (((b
+ 1) % 256) == last_byte
&& !hit_edge
) {
297 /* This is OK, the guest stopped at the point of
298 * incrementing the previous page but didn't get
304 fprintf(stderr
, "Memory content inconsistency at %x"
305 " first_byte = %x last_byte = %x current = %x"
307 address
, first_byte
, last_byte
, b
, hit_edge
);
315 static void cleanup(const char *filename
)
317 char *path
= g_strdup_printf("%s/%s", tmpfs
, filename
);
323 static void migrate_check_parameter(QTestState
*who
, const char *parameter
,
328 rsp_return
= wait_command(who
,
329 "{ 'execute': 'query-migrate-parameters' }");
330 g_assert_cmpint(qdict_get_int(rsp_return
, parameter
), ==, value
);
331 qobject_unref(rsp_return
);
334 static void migrate_set_parameter(QTestState
*who
, const char *parameter
,
340 "{ 'execute': 'migrate-set-parameters',"
341 "'arguments': { %s: %lld } }",
343 g_assert(qdict_haskey(rsp
, "return"));
345 migrate_check_parameter(who
, parameter
, value
);
348 static void migrate_pause(QTestState
*who
)
352 rsp
= wait_command(who
, "{ 'execute': 'migrate-pause' }");
356 static void migrate_recover(QTestState
*who
, const char *uri
)
360 rsp
= wait_command(who
,
361 "{ 'execute': 'migrate-recover', "
362 " 'id': 'recover-cmd', "
363 " 'arguments': { 'uri': %s } }",
368 static void migrate_set_capability(QTestState
*who
, const char *capability
,
374 "{ 'execute': 'migrate-set-capabilities',"
376 "'capabilities': [ { "
377 "'capability': %s, 'state': %i } ] } }",
379 g_assert(qdict_haskey(rsp
, "return"));
384 * Send QMP command "migrate".
385 * Arguments are built from @fmt... (formatted like
386 * qobject_from_jsonf_nofail()) with "uri": @uri spliced in.
389 static void migrate(QTestState
*who
, const char *uri
, const char *fmt
, ...)
395 args
= qdict_from_vjsonf_nofail(fmt
, ap
);
398 g_assert(!qdict_haskey(args
, "uri"));
399 qdict_put_str(args
, "uri", uri
);
401 rsp
= qmp("{ 'execute': 'migrate', 'arguments': %p}", args
);
402 g_assert(qdict_haskey(rsp
, "return"));
406 static void migrate_postcopy_start(QTestState
*from
, QTestState
*to
)
410 rsp
= wait_command(from
, "{ 'execute': 'migrate-start-postcopy' }");
414 qtest_qmp_eventwait(from
, "STOP");
417 qtest_qmp_eventwait(to
, "RESUME");
420 static int test_migrate_start(QTestState
**from
, QTestState
**to
,
421 const char *uri
, bool hide_stderr
)
423 gchar
*cmd_src
, *cmd_dst
;
424 char *bootpath
= g_strdup_printf("%s/bootsect", tmpfs
);
425 const char *arch
= qtest_get_arch();
426 const char *accel
= "kvm:tcg";
430 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
431 init_bootfile_x86(bootpath
);
432 cmd_src
= g_strdup_printf("-machine accel=%s -m 150M"
433 " -name source,debug-threads=on"
434 " -serial file:%s/src_serial"
435 " -drive file=%s,format=raw",
436 accel
, tmpfs
, bootpath
);
437 cmd_dst
= g_strdup_printf("-machine accel=%s -m 150M"
438 " -name target,debug-threads=on"
439 " -serial file:%s/dest_serial"
440 " -drive file=%s,format=raw"
442 accel
, tmpfs
, bootpath
, uri
);
443 start_address
= X86_TEST_MEM_START
;
444 end_address
= X86_TEST_MEM_END
;
445 } else if (strcmp(arch
, "ppc64") == 0) {
446 cmd_src
= g_strdup_printf("-machine accel=%s -m 256M -nodefaults"
447 " -name source,debug-threads=on"
448 " -serial file:%s/src_serial"
449 " -prom-env 'use-nvramrc?=true' -prom-env "
450 "'nvramrc=hex .\" _\" begin %x %x "
451 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
452 "until'", accel
, tmpfs
, end_address
,
454 cmd_dst
= g_strdup_printf("-machine accel=%s -m 256M"
455 " -name target,debug-threads=on"
456 " -serial file:%s/dest_serial"
460 start_address
= PPC_TEST_MEM_START
;
461 end_address
= PPC_TEST_MEM_END
;
463 g_assert_not_reached();
470 tmp
= g_strdup_printf("%s 2>/dev/null", cmd_src
);
474 tmp
= g_strdup_printf("%s 2>/dev/null", cmd_dst
);
479 *from
= qtest_start(cmd_src
);
482 *to
= qtest_init(cmd_dst
);
487 static void test_migrate_end(QTestState
*from
, QTestState
*to
, bool test_dest
)
489 unsigned char dest_byte_a
, dest_byte_b
, dest_byte_c
, dest_byte_d
;
494 qtest_memread(to
, start_address
, &dest_byte_a
, 1);
496 /* Destination still running, wait for a byte to change */
498 qtest_memread(to
, start_address
, &dest_byte_b
, 1);
500 } while (dest_byte_a
== dest_byte_b
);
502 qtest_qmp_discard_response(to
, "{ 'execute' : 'stop'}");
504 /* With it stopped, check nothing changes */
505 qtest_memread(to
, start_address
, &dest_byte_c
, 1);
507 qtest_memread(to
, start_address
, &dest_byte_d
, 1);
508 g_assert_cmpint(dest_byte_c
, ==, dest_byte_d
);
510 check_guests_ram(to
);
516 cleanup("migsocket");
517 cleanup("src_serial");
518 cleanup("dest_serial");
521 static void deprecated_set_downtime(QTestState
*who
, const double value
)
526 "{ 'execute': 'migrate_set_downtime',"
527 " 'arguments': { 'value': %f } }", value
);
528 g_assert(qdict_haskey(rsp
, "return"));
530 migrate_check_parameter(who
, "downtime-limit", value
* 1000);
533 static void deprecated_set_speed(QTestState
*who
, long long value
)
537 rsp
= qtest_qmp(who
, "{ 'execute': 'migrate_set_speed',"
538 "'arguments': { 'value': %lld } }", value
);
539 g_assert(qdict_haskey(rsp
, "return"));
541 migrate_check_parameter(who
, "max-bandwidth", value
);
544 static void test_deprecated(void)
548 from
= qtest_start("");
550 deprecated_set_downtime(from
, 0.12345);
551 deprecated_set_speed(from
, 12345);
556 static int migrate_postcopy_prepare(QTestState
**from_ptr
,
560 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
561 QTestState
*from
, *to
;
563 if (test_migrate_start(&from
, &to
, uri
, hide_error
)) {
567 migrate_set_capability(from
, "postcopy-ram", true);
568 migrate_set_capability(to
, "postcopy-ram", true);
569 migrate_set_capability(to
, "postcopy-blocktime", true);
571 /* We want to pick a speed slow enough that the test completes
572 * quickly, but that it doesn't complete precopy even on a slow
573 * machine, so also set the downtime.
575 migrate_set_parameter(from
, "max-bandwidth", 100000000);
576 migrate_set_parameter(from
, "downtime-limit", 1);
578 /* Wait for the first serial output from the source */
579 wait_for_serial("src_serial");
581 migrate(from
, uri
, "{}");
584 wait_for_migration_pass(from
);
592 static void migrate_postcopy_complete(QTestState
*from
, QTestState
*to
)
594 wait_for_migration_complete(from
);
596 /* Make sure we get at least one "B" on destination */
597 wait_for_serial("dest_serial");
599 if (uffd_feature_thread_id
) {
603 test_migrate_end(from
, to
, true);
606 static void test_postcopy(void)
608 QTestState
*from
, *to
;
610 if (migrate_postcopy_prepare(&from
, &to
, false)) {
613 migrate_postcopy_start(from
, to
);
614 migrate_postcopy_complete(from
, to
);
617 static void test_postcopy_recovery(void)
619 QTestState
*from
, *to
;
622 if (migrate_postcopy_prepare(&from
, &to
, true)) {
626 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
627 migrate_set_parameter(from
, "max-postcopy-bandwidth", 4096);
629 /* Now we start the postcopy */
630 migrate_postcopy_start(from
, to
);
633 * Wait until postcopy is really started; we can only run the
634 * migrate-pause command during a postcopy
636 wait_for_migration_status(from
, "postcopy-active");
639 * Manually stop the postcopy migration. This emulates a network
640 * failure with the migration socket
645 * Wait for destination side to reach postcopy-paused state. The
646 * migrate-recover command can only succeed if destination machine
647 * is in the paused state
649 wait_for_migration_status(to
, "postcopy-paused");
652 * Create a new socket to emulate a new channel that is different
653 * from the broken migration channel; tell the destination to
654 * listen to the new port
656 uri
= g_strdup_printf("unix:%s/migsocket-recover", tmpfs
);
657 migrate_recover(to
, uri
);
660 * Try to rebuild the migration channel using the resume flag and
661 * the newly created channel
663 wait_for_migration_status(from
, "postcopy-paused");
664 migrate(from
, uri
, "{'resume': true}");
667 /* Restore the postcopy bandwidth to unlimited */
668 migrate_set_parameter(from
, "max-postcopy-bandwidth", 0);
670 migrate_postcopy_complete(from
, to
);
673 static void test_baddest(void)
675 QTestState
*from
, *to
;
680 if (test_migrate_start(&from
, &to
, "tcp:0:0", true)) {
683 migrate(from
, "tcp:0:0", "{}");
685 status
= migrate_query_status(from
);
686 g_assert(!strcmp(status
, "setup") || !(strcmp(status
, "failed")));
687 failed
= !strcmp(status
, "failed");
691 /* Is the machine currently running? */
692 rsp_return
= wait_command(from
, "{ 'execute': 'query-status' }");
693 g_assert(qdict_haskey(rsp_return
, "running"));
694 g_assert(qdict_get_bool(rsp_return
, "running"));
695 qobject_unref(rsp_return
);
697 test_migrate_end(from
, to
, false);
700 static void test_precopy_unix(void)
702 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
703 QTestState
*from
, *to
;
705 if (test_migrate_start(&from
, &to
, uri
, false)) {
709 /* We want to pick a speed slow enough that the test completes
710 * quickly, but that it doesn't complete precopy even on a slow
711 * machine, so also set the downtime.
713 /* 1 ms should make it not converge*/
714 migrate_set_parameter(from
, "downtime-limit", 1);
716 migrate_set_parameter(from
, "max-bandwidth", 1000000000);
718 /* Wait for the first serial output from the source */
719 wait_for_serial("src_serial");
721 migrate(from
, uri
, "{}");
723 wait_for_migration_pass(from
);
725 /* 300 ms should converge */
726 migrate_set_parameter(from
, "downtime-limit", 300);
729 qtest_qmp_eventwait(from
, "STOP");
732 qtest_qmp_eventwait(to
, "RESUME");
734 wait_for_serial("dest_serial");
735 wait_for_migration_complete(from
);
737 test_migrate_end(from
, to
, true);
741 int main(int argc
, char **argv
)
743 char template[] = "/tmp/migration-test-XXXXXX";
746 g_test_init(&argc
, &argv
, NULL
);
748 if (!ufd_version_check()) {
753 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
754 * is touchy due to race conditions on dirty bits (especially on PPC for
757 if (g_str_equal(qtest_get_arch(), "ppc64") &&
758 access("/sys/module/kvm_hv", F_OK
)) {
759 g_test_message("Skipping test: kvm_hv not available");
763 tmpfs
= mkdtemp(template);
765 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno
));
769 module_call_init(MODULE_INIT_QOM
);
771 qtest_add_func("/migration/postcopy/unix", test_postcopy
);
772 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery
);
773 qtest_add_func("/migration/deprecated", test_deprecated
);
774 qtest_add_func("/migration/bad_dest", test_baddest
);
775 qtest_add_func("/migration/precopy/unix", test_precopy_unix
);
779 g_assert_cmpint(ret
, ==, 0);
783 g_test_message("unable to rmdir: path (%s): %s\n",
784 tmpfs
, strerror(errno
));