]>
git.proxmox.com Git - mirror_qemu.git/blob - tests/migration-test.c
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/option.h"
18 #include "qemu/range.h"
19 #include "qemu/sockets.h"
20 #include "chardev/char.h"
21 #include "sysemu/sysemu.h"
23 /* TODO actually test the results and get rid of this */
24 #define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__))
26 const unsigned start_address
= 1024 * 1024;
27 const unsigned end_address
= 100 * 1024 * 1024;
29 static bool uffd_feature_thread_id
;
31 #if defined(__linux__)
32 #include <sys/syscall.h>
36 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
37 #include <sys/eventfd.h>
38 #include <sys/ioctl.h>
39 #include <linux/userfaultfd.h>
41 static bool ufd_version_check(void)
43 struct uffdio_api api_struct
;
46 int ufd
= syscall(__NR_userfaultfd
, O_CLOEXEC
);
49 g_test_message("Skipping test: userfaultfd not available");
53 api_struct
.api
= UFFD_API
;
54 api_struct
.features
= 0;
55 if (ioctl(ufd
, UFFDIO_API
, &api_struct
)) {
56 g_test_message("Skipping test: UFFDIO_API failed");
59 uffd_feature_thread_id
= api_struct
.features
& UFFD_FEATURE_THREAD_ID
;
61 ioctl_mask
= (__u64
)1 << _UFFDIO_REGISTER
|
62 (__u64
)1 << _UFFDIO_UNREGISTER
;
63 if ((api_struct
.ioctls
& ioctl_mask
) != ioctl_mask
) {
64 g_test_message("Skipping test: Missing userfault feature");
72 static bool ufd_version_check(void)
74 g_test_message("Skipping test: Userfault not available (builtdtime)");
80 static const char *tmpfs
;
82 /* A simple PC boot sector that modifies memory (1-100MB) quickly
83 * outputting a 'B' every so often if it's still running.
85 #include "tests/migration/x86-a-b-bootblock.h"
87 static void init_bootfile_x86(const char *bootpath
)
89 FILE *bootfile
= fopen(bootpath
, "wb");
91 g_assert_cmpint(fwrite(x86_bootsect
, 512, 1, bootfile
), ==, 1);
96 * Wait for some output in the serial output file,
97 * we get an 'A' followed by an endless string of 'B's
98 * but on the destination we won't have the A.
100 static void wait_for_serial(const char *side
)
102 char *serialpath
= g_strdup_printf("%s/%s", tmpfs
, side
);
103 FILE *serialfile
= fopen(serialpath
, "r");
104 const char *arch
= qtest_get_arch();
105 int started
= (strcmp(side
, "src_serial") == 0 &&
106 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
110 int readvalue
= fgetc(serialfile
);
113 /* SLOF prints its banner before starting test,
114 * to ignore it, mark the start of the test with '_',
115 * ignore all characters until this marker
122 fseek(serialfile
, 0, SEEK_SET
);
139 started
= (strcmp(side
, "src_serial") == 0 &&
140 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
141 fseek(serialfile
, 0, SEEK_SET
);
146 fprintf(stderr
, "Unexpected %d on %s serial\n", readvalue
, side
);
147 g_assert_not_reached();
152 static void stop_cb(void *opaque
, const char *name
, QDict
*data
)
154 if (!strcmp(name
, "STOP")) {
160 * Events can get in the way of responses we are actually waiting for.
162 static QDict
*wait_command(QTestState
*who
, const char *command
, ...)
166 va_start(ap
, command
);
167 qtest_qmp_vsend(who
, command
, ap
);
170 return qtest_qmp_receive_success(who
, stop_cb
, NULL
);
174 * Note: caller is responsible to free the returned object via
175 * qobject_unref() after use
177 static QDict
*migrate_query(QTestState
*who
)
179 return wait_command(who
, "{ 'execute': 'query-migrate' }");
183 * Note: caller is responsible to free the returned object via
186 static gchar
*migrate_query_status(QTestState
*who
)
188 QDict
*rsp_return
= migrate_query(who
);
189 gchar
*status
= g_strdup(qdict_get_str(rsp_return
, "status"));
192 qobject_unref(rsp_return
);
198 * It's tricky to use qemu's migration event capability with qtest,
199 * events suddenly appearing confuse the qmp()/hmp() responses.
202 static uint64_t get_migration_pass(QTestState
*who
)
204 QDict
*rsp_return
, *rsp_ram
;
207 rsp_return
= migrate_query(who
);
208 if (!qdict_haskey(rsp_return
, "ram")) {
212 rsp_ram
= qdict_get_qdict(rsp_return
, "ram");
213 result
= qdict_get_try_int(rsp_ram
, "dirty-sync-count", 0);
215 qobject_unref(rsp_return
);
219 static void read_blocktime(QTestState
*who
)
223 rsp_return
= migrate_query(who
);
224 g_assert(qdict_haskey(rsp_return
, "postcopy-blocktime"));
225 qobject_unref(rsp_return
);
228 static void wait_for_migration_status(QTestState
*who
,
235 status
= migrate_query_status(who
);
236 completed
= strcmp(status
, goal
) == 0;
237 g_assert_cmpstr(status
, !=, "failed");
246 static void wait_for_migration_complete(QTestState
*who
)
248 wait_for_migration_status(who
, "completed");
251 static void wait_for_migration_pass(QTestState
*who
)
253 uint64_t initial_pass
= get_migration_pass(who
);
256 /* Wait for the 1st sync */
257 while (!got_stop
&& !initial_pass
) {
259 initial_pass
= get_migration_pass(who
);
264 pass
= get_migration_pass(who
);
265 } while (pass
== initial_pass
&& !got_stop
);
268 static void check_guests_ram(QTestState
*who
)
270 /* Our ASM test will have been incrementing one byte from each page from
271 * 1MB to <100MB in order.
272 * This gives us a constraint that any page's byte should be equal or less
273 * than the previous pages byte (mod 256); and they should all be equal
274 * except for one transition at the point where we meet the incrementer.
275 * (We're running this with the guest stopped).
280 bool hit_edge
= false;
283 qtest_memread(who
, start_address
, &first_byte
, 1);
284 last_byte
= first_byte
;
286 for (address
= start_address
+ 4096; address
< end_address
; address
+= 4096)
289 qtest_memread(who
, address
, &b
, 1);
290 if (b
!= last_byte
) {
291 if (((b
+ 1) % 256) == last_byte
&& !hit_edge
) {
292 /* This is OK, the guest stopped at the point of
293 * incrementing the previous page but didn't get
299 fprintf(stderr
, "Memory content inconsistency at %x"
300 " first_byte = %x last_byte = %x current = %x"
302 address
, first_byte
, last_byte
, b
, hit_edge
);
310 static void cleanup(const char *filename
)
312 char *path
= g_strdup_printf("%s/%s", tmpfs
, filename
);
318 static void migrate_check_parameter(QTestState
*who
, const char *parameter
,
324 rsp_return
= wait_command(who
,
325 "{ 'execute': 'query-migrate-parameters' }");
326 result
= g_strdup_printf("%" PRId64
,
327 qdict_get_try_int(rsp_return
, parameter
, -1));
328 g_assert_cmpstr(result
, ==, value
);
330 qobject_unref(rsp_return
);
333 static void migrate_set_parameter(QTestState
*who
, const char *parameter
,
339 cmd
= g_strdup_printf("{ 'execute': 'migrate-set-parameters',"
340 "'arguments': { '%s': %s } }",
342 rsp
= qtest_qmp(who
, cmd
);
344 g_assert(qdict_haskey(rsp
, "return"));
346 migrate_check_parameter(who
, parameter
, value
);
349 static void migrate_pause(QTestState
*who
)
353 rsp
= wait_command(who
, "{ 'execute': 'migrate-pause' }");
357 static void migrate_recover(QTestState
*who
, const char *uri
)
360 gchar
*cmd
= g_strdup_printf(
361 "{ 'execute': 'migrate-recover', "
362 " 'id': 'recover-cmd', "
363 " 'arguments': { 'uri': '%s' } }", uri
);
365 rsp
= wait_command(who
, cmd
);
370 static void migrate_set_capability(QTestState
*who
, const char *capability
,
376 cmd
= g_strdup_printf("{ 'execute': 'migrate-set-capabilities',"
378 "'capabilities': [ { "
379 "'capability': '%s', 'state': %s } ] } }",
381 rsp
= qtest_qmp(who
, cmd
);
383 g_assert(qdict_haskey(rsp
, "return"));
387 static void migrate(QTestState
*who
, const char *uri
, const char *extra
)
392 cmd
= g_strdup_printf("{ 'execute': 'migrate',"
393 " 'arguments': { 'uri': '%s' %s } }",
394 uri
, extra
? extra
: "");
395 rsp
= qtest_qmp(who
, cmd
);
397 g_assert(qdict_haskey(rsp
, "return"));
401 static void migrate_postcopy_start(QTestState
*from
, QTestState
*to
)
405 rsp
= wait_command(from
, "{ 'execute': 'migrate-start-postcopy' }");
409 qtest_qmp_eventwait(from
, "STOP");
412 qtest_qmp_eventwait(to
, "RESUME");
415 static int test_migrate_start(QTestState
**from
, QTestState
**to
,
416 const char *uri
, bool hide_stderr
)
418 gchar
*cmd_src
, *cmd_dst
;
419 char *bootpath
= g_strdup_printf("%s/bootsect", tmpfs
);
420 const char *arch
= qtest_get_arch();
421 const char *accel
= "kvm:tcg";
425 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
426 init_bootfile_x86(bootpath
);
427 cmd_src
= g_strdup_printf("-machine accel=%s -m 150M"
428 " -name source,debug-threads=on"
429 " -serial file:%s/src_serial"
430 " -drive file=%s,format=raw",
431 accel
, tmpfs
, bootpath
);
432 cmd_dst
= g_strdup_printf("-machine accel=%s -m 150M"
433 " -name target,debug-threads=on"
434 " -serial file:%s/dest_serial"
435 " -drive file=%s,format=raw"
437 accel
, tmpfs
, bootpath
, uri
);
438 } else if (strcmp(arch
, "ppc64") == 0) {
440 /* On ppc64, the test only works with kvm-hv, but not with kvm-pr
441 * and TCG is touchy due to race conditions on dirty bits
442 * (especially on PPC for some reason)
444 if (access("/sys/module/kvm_hv", F_OK
)) {
445 g_print("Skipping test: kvm_hv not available ");
448 cmd_src
= g_strdup_printf("-machine accel=%s -m 256M"
449 " -name source,debug-threads=on"
450 " -serial file:%s/src_serial"
452 "boot-command=hex .\" _\" begin %x %x "
453 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
454 "until'", accel
, tmpfs
, end_address
,
456 cmd_dst
= g_strdup_printf("-machine accel=%s -m 256M"
457 " -name target,debug-threads=on"
458 " -serial file:%s/dest_serial"
462 g_assert_not_reached();
469 tmp
= g_strdup_printf("%s 2>/dev/null", cmd_src
);
473 tmp
= g_strdup_printf("%s 2>/dev/null", cmd_dst
);
478 *from
= qtest_start(cmd_src
);
481 *to
= qtest_init(cmd_dst
);
486 static void test_migrate_end(QTestState
*from
, QTestState
*to
, bool test_dest
)
488 unsigned char dest_byte_a
, dest_byte_b
, dest_byte_c
, dest_byte_d
;
493 qtest_memread(to
, start_address
, &dest_byte_a
, 1);
495 /* Destination still running, wait for a byte to change */
497 qtest_memread(to
, start_address
, &dest_byte_b
, 1);
499 } while (dest_byte_a
== dest_byte_b
);
501 qtest_qmp_discard_response(to
, "{ 'execute' : 'stop'}");
503 /* With it stopped, check nothing changes */
504 qtest_memread(to
, start_address
, &dest_byte_c
, 1);
506 qtest_memread(to
, start_address
, &dest_byte_d
, 1);
507 g_assert_cmpint(dest_byte_c
, ==, dest_byte_d
);
509 check_guests_ram(to
);
515 cleanup("migsocket");
516 cleanup("src_serial");
517 cleanup("dest_serial");
520 static void deprecated_set_downtime(QTestState
*who
, const double value
)
527 "{ 'execute': 'migrate_set_downtime',"
528 " 'arguments': { 'value': %f } }", value
);
529 g_assert(qdict_haskey(rsp
, "return"));
531 result_int
= value
* 1000L;
532 expected
= g_strdup_printf("%" PRId64
, result_int
);
533 migrate_check_parameter(who
, "downtime-limit", expected
);
537 static void deprecated_set_speed(QTestState
*who
, const char *value
)
542 cmd
= g_strdup_printf("{ 'execute': 'migrate_set_speed',"
543 "'arguments': { 'value': %s } }", value
);
544 rsp
= qtest_qmp(who
, cmd
);
546 g_assert(qdict_haskey(rsp
, "return"));
548 migrate_check_parameter(who
, "max-bandwidth", value
);
551 static void test_deprecated(void)
555 from
= qtest_start("");
557 deprecated_set_downtime(from
, 0.12345);
558 deprecated_set_speed(from
, "12345");
563 static int migrate_postcopy_prepare(QTestState
**from_ptr
,
567 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
568 QTestState
*from
, *to
;
570 if (test_migrate_start(&from
, &to
, uri
, hide_error
)) {
574 migrate_set_capability(from
, "postcopy-ram", "true");
575 migrate_set_capability(to
, "postcopy-ram", "true");
576 migrate_set_capability(to
, "postcopy-blocktime", "true");
578 /* We want to pick a speed slow enough that the test completes
579 * quickly, but that it doesn't complete precopy even on a slow
580 * machine, so also set the downtime.
582 migrate_set_parameter(from
, "max-bandwidth", "100000000");
583 migrate_set_parameter(from
, "downtime-limit", "1");
585 /* Wait for the first serial output from the source */
586 wait_for_serial("src_serial");
588 migrate(from
, uri
, NULL
);
591 wait_for_migration_pass(from
);
599 static void migrate_postcopy_complete(QTestState
*from
, QTestState
*to
)
601 wait_for_migration_complete(from
);
603 /* Make sure we get at least one "B" on destination */
604 wait_for_serial("dest_serial");
606 if (uffd_feature_thread_id
) {
610 test_migrate_end(from
, to
, true);
613 static void test_postcopy(void)
615 QTestState
*from
, *to
;
617 if (migrate_postcopy_prepare(&from
, &to
, false)) {
620 migrate_postcopy_start(from
, to
);
621 migrate_postcopy_complete(from
, to
);
624 static void test_postcopy_recovery(void)
626 QTestState
*from
, *to
;
629 if (migrate_postcopy_prepare(&from
, &to
, true)) {
633 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
634 migrate_set_parameter(from
, "max-postcopy-bandwidth", "4096");
636 /* Now we start the postcopy */
637 migrate_postcopy_start(from
, to
);
640 * Wait until postcopy is really started; we can only run the
641 * migrate-pause command during a postcopy
643 wait_for_migration_status(from
, "postcopy-active");
646 * Manually stop the postcopy migration. This emulates a network
647 * failure with the migration socket
652 * Wait for destination side to reach postcopy-paused state. The
653 * migrate-recover command can only succeed if destination machine
654 * is in the paused state
656 wait_for_migration_status(to
, "postcopy-paused");
659 * Create a new socket to emulate a new channel that is different
660 * from the broken migration channel; tell the destination to
661 * listen to the new port
663 uri
= g_strdup_printf("unix:%s/migsocket-recover", tmpfs
);
664 migrate_recover(to
, uri
);
667 * Try to rebuild the migration channel using the resume flag and
668 * the newly created channel
670 wait_for_migration_status(from
, "postcopy-paused");
671 migrate(from
, uri
, ", 'resume': true");
674 /* Restore the postcopy bandwidth to unlimited */
675 migrate_set_parameter(from
, "max-postcopy-bandwidth", "0");
677 migrate_postcopy_complete(from
, to
);
680 static void test_baddest(void)
682 QTestState
*from
, *to
;
687 if (test_migrate_start(&from
, &to
, "tcp:0:0", true)) {
690 migrate(from
, "tcp:0:0", NULL
);
692 status
= migrate_query_status(from
);
693 g_assert(!strcmp(status
, "setup") || !(strcmp(status
, "failed")));
694 failed
= !strcmp(status
, "failed");
698 /* Is the machine currently running? */
699 rsp_return
= wait_command(from
, "{ 'execute': 'query-status' }");
700 g_assert(qdict_haskey(rsp_return
, "running"));
701 g_assert(qdict_get_bool(rsp_return
, "running"));
702 qobject_unref(rsp_return
);
704 test_migrate_end(from
, to
, false);
707 static void test_precopy_unix(void)
709 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
710 QTestState
*from
, *to
;
712 if (test_migrate_start(&from
, &to
, uri
, false)) {
716 /* We want to pick a speed slow enough that the test completes
717 * quickly, but that it doesn't complete precopy even on a slow
718 * machine, so also set the downtime.
720 /* 1 ms should make it not converge*/
721 migrate_set_parameter(from
, "downtime-limit", "1");
723 migrate_set_parameter(from
, "max-bandwidth", "1000000000");
725 /* Wait for the first serial output from the source */
726 wait_for_serial("src_serial");
728 migrate(from
, uri
, NULL
);
730 wait_for_migration_pass(from
);
732 /* 300 ms should converge */
733 migrate_set_parameter(from
, "downtime-limit", "300");
736 qtest_qmp_eventwait(from
, "STOP");
739 qtest_qmp_eventwait(to
, "RESUME");
741 wait_for_serial("dest_serial");
742 wait_for_migration_complete(from
);
744 test_migrate_end(from
, to
, true);
748 int main(int argc
, char **argv
)
750 char template[] = "/tmp/migration-test-XXXXXX";
753 g_test_init(&argc
, &argv
, NULL
);
755 if (!ufd_version_check()) {
759 tmpfs
= mkdtemp(template);
761 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno
));
765 module_call_init(MODULE_INIT_QOM
);
767 qtest_add_func("/migration/postcopy/unix", test_postcopy
);
768 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery
);
769 qtest_add_func("/migration/deprecated", test_deprecated
);
770 qtest_add_func("/migration/bad_dest", test_baddest
);
771 qtest_add_func("/migration/precopy/unix", test_precopy_unix
);
775 g_assert_cmpint(ret
, ==, 0);
779 g_test_message("unable to rmdir: path (%s): %s\n",
780 tmpfs
, strerror(errno
));