]>
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"
22 #include "hw/nvram/chrp_nvram.h"
24 #define MIN_NVRAM_SIZE 8192 /* from spapr_nvram.c */
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);
95 static void init_bootfile_ppc(const char *bootpath
)
98 char buf
[MIN_NVRAM_SIZE
];
99 ChrpNvramPartHdr
*header
= (ChrpNvramPartHdr
*)buf
;
101 memset(buf
, 0, MIN_NVRAM_SIZE
);
103 /* Create a "common" partition in nvram to store boot-command property */
105 header
->signature
= CHRP_NVPART_SYSTEM
;
106 memcpy(header
->name
, "common", 6);
107 chrp_nvram_finish_partition(header
, MIN_NVRAM_SIZE
);
109 /* FW_MAX_SIZE is 4MB, but slof.bin is only 900KB,
110 * so let's modify memory between 1MB and 100MB
111 * to do like PC bootsector
115 "boot-command=hex .\" _\" begin %x %x do i c@ 1 + i c! 1000 +loop "
116 ".\" B\" 0 until", end_address
, start_address
);
118 /* Write partition to the NVRAM file */
120 bootfile
= fopen(bootpath
, "wb");
121 g_assert_cmpint(fwrite(buf
, MIN_NVRAM_SIZE
, 1, bootfile
), ==, 1);
126 * Wait for some output in the serial output file,
127 * we get an 'A' followed by an endless string of 'B's
128 * but on the destination we won't have the A.
130 static void wait_for_serial(const char *side
)
132 char *serialpath
= g_strdup_printf("%s/%s", tmpfs
, side
);
133 FILE *serialfile
= fopen(serialpath
, "r");
134 const char *arch
= qtest_get_arch();
135 int started
= (strcmp(side
, "src_serial") == 0 &&
136 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
140 int readvalue
= fgetc(serialfile
);
143 /* SLOF prints its banner before starting test,
144 * to ignore it, mark the start of the test with '_',
145 * ignore all characters until this marker
152 fseek(serialfile
, 0, SEEK_SET
);
169 started
= (strcmp(side
, "src_serial") == 0 &&
170 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
171 fseek(serialfile
, 0, SEEK_SET
);
176 fprintf(stderr
, "Unexpected %d on %s serial\n", readvalue
, side
);
177 g_assert_not_reached();
183 * Events can get in the way of responses we are actually waiting for.
185 static QDict
*wait_command(QTestState
*who
, const char *command
)
187 const char *event_string
;
190 response
= qtest_qmp(who
, command
);
192 while (qdict_haskey(response
, "event")) {
193 /* OK, it was an event */
194 event_string
= qdict_get_str(response
, "event");
195 if (!strcmp(event_string
, "STOP")) {
198 qobject_unref(response
);
199 response
= qtest_qmp_receive(who
);
206 * It's tricky to use qemu's migration event capability with qtest,
207 * events suddenly appearing confuse the qmp()/hmp() responses.
210 static uint64_t get_migration_pass(QTestState
*who
)
212 QDict
*rsp
, *rsp_return
, *rsp_ram
;
215 rsp
= wait_command(who
, "{ 'execute': 'query-migrate' }");
216 rsp_return
= qdict_get_qdict(rsp
, "return");
217 if (!qdict_haskey(rsp_return
, "ram")) {
221 rsp_ram
= qdict_get_qdict(rsp_return
, "ram");
222 result
= qdict_get_try_int(rsp_ram
, "dirty-sync-count", 0);
228 static void read_blocktime(QTestState
*who
)
230 QDict
*rsp
, *rsp_return
;
232 rsp
= wait_command(who
, "{ 'execute': 'query-migrate' }");
233 rsp_return
= qdict_get_qdict(rsp
, "return");
234 g_assert(qdict_haskey(rsp_return
, "postcopy-blocktime"));
238 static void wait_for_migration_complete(QTestState
*who
)
241 QDict
*rsp
, *rsp_return
;
245 rsp
= wait_command(who
, "{ 'execute': 'query-migrate' }");
246 rsp_return
= qdict_get_qdict(rsp
, "return");
247 status
= qdict_get_str(rsp_return
, "status");
248 completed
= strcmp(status
, "completed") == 0;
249 g_assert_cmpstr(status
, !=, "failed");
258 static void wait_for_migration_pass(QTestState
*who
)
260 uint64_t initial_pass
= get_migration_pass(who
);
263 /* Wait for the 1st sync */
264 while (!got_stop
&& !initial_pass
) {
266 initial_pass
= get_migration_pass(who
);
271 pass
= get_migration_pass(who
);
272 } while (pass
== initial_pass
&& !got_stop
);
275 static void check_guests_ram(QTestState
*who
)
277 /* Our ASM test will have been incrementing one byte from each page from
278 * 1MB to <100MB in order.
279 * This gives us a constraint that any page's byte should be equal or less
280 * than the previous pages byte (mod 256); and they should all be equal
281 * except for one transition at the point where we meet the incrementer.
282 * (We're running this with the guest stopped).
287 bool hit_edge
= false;
290 qtest_memread(who
, start_address
, &first_byte
, 1);
291 last_byte
= first_byte
;
293 for (address
= start_address
+ 4096; address
< end_address
; address
+= 4096)
296 qtest_memread(who
, address
, &b
, 1);
297 if (b
!= last_byte
) {
298 if (((b
+ 1) % 256) == last_byte
&& !hit_edge
) {
299 /* This is OK, the guest stopped at the point of
300 * incrementing the previous page but didn't get
305 fprintf(stderr
, "Memory content inconsistency at %x"
306 " first_byte = %x last_byte = %x current = %x"
308 address
, first_byte
, last_byte
, b
, hit_edge
);
317 static void cleanup(const char *filename
)
319 char *path
= g_strdup_printf("%s/%s", tmpfs
, filename
);
325 static void migrate_check_parameter(QTestState
*who
, const char *parameter
,
328 QDict
*rsp
, *rsp_return
;
331 rsp
= wait_command(who
, "{ 'execute': 'query-migrate-parameters' }");
332 rsp_return
= qdict_get_qdict(rsp
, "return");
333 result
= g_strdup_printf("%" PRId64
,
334 qdict_get_try_int(rsp_return
, parameter
, -1));
335 g_assert_cmpstr(result
, ==, value
);
340 static void migrate_set_parameter(QTestState
*who
, const char *parameter
,
346 cmd
= g_strdup_printf("{ 'execute': 'migrate-set-parameters',"
347 "'arguments': { '%s': %s } }",
349 rsp
= qtest_qmp(who
, cmd
);
351 g_assert(qdict_haskey(rsp
, "return"));
353 migrate_check_parameter(who
, parameter
, value
);
356 static void migrate_set_capability(QTestState
*who
, const char *capability
,
362 cmd
= g_strdup_printf("{ 'execute': 'migrate-set-capabilities',"
364 "'capabilities': [ { "
365 "'capability': '%s', 'state': %s } ] } }",
367 rsp
= qtest_qmp(who
, cmd
);
369 g_assert(qdict_haskey(rsp
, "return"));
373 static void migrate(QTestState
*who
, const char *uri
)
378 cmd
= g_strdup_printf("{ 'execute': 'migrate',"
379 "'arguments': { 'uri': '%s' } }",
381 rsp
= qtest_qmp(who
, cmd
);
383 g_assert(qdict_haskey(rsp
, "return"));
387 static void migrate_start_postcopy(QTestState
*who
)
391 rsp
= wait_command(who
, "{ 'execute': 'migrate-start-postcopy' }");
392 g_assert(qdict_haskey(rsp
, "return"));
396 static void test_migrate_start(QTestState
**from
, QTestState
**to
,
397 const char *uri
, bool hide_stderr
)
399 gchar
*cmd_src
, *cmd_dst
;
400 char *bootpath
= g_strdup_printf("%s/bootsect", tmpfs
);
401 const char *arch
= qtest_get_arch();
402 const char *accel
= "kvm:tcg";
406 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
407 init_bootfile_x86(bootpath
);
408 cmd_src
= g_strdup_printf("-machine accel=%s -m 150M"
409 " -name source,debug-threads=on"
410 " -serial file:%s/src_serial"
411 " -drive file=%s,format=raw",
412 accel
, tmpfs
, bootpath
);
413 cmd_dst
= g_strdup_printf("-machine accel=%s -m 150M"
414 " -name target,debug-threads=on"
415 " -serial file:%s/dest_serial"
416 " -drive file=%s,format=raw"
418 accel
, tmpfs
, bootpath
, uri
);
419 } else if (strcmp(arch
, "ppc64") == 0) {
421 /* On ppc64, the test only works with kvm-hv, but not with kvm-pr */
422 if (access("/sys/module/kvm_hv", F_OK
)) {
425 init_bootfile_ppc(bootpath
);
426 cmd_src
= g_strdup_printf("-machine accel=%s -m 256M"
427 " -name source,debug-threads=on"
428 " -serial file:%s/src_serial"
429 " -drive file=%s,if=pflash,format=raw",
430 accel
, tmpfs
, bootpath
);
431 cmd_dst
= g_strdup_printf("-machine accel=%s -m 256M"
432 " -name target,debug-threads=on"
433 " -serial file:%s/dest_serial"
437 g_assert_not_reached();
444 tmp
= g_strdup_printf("%s 2>/dev/null", cmd_src
);
448 tmp
= g_strdup_printf("%s 2>/dev/null", cmd_dst
);
453 *from
= qtest_start(cmd_src
);
456 *to
= qtest_init(cmd_dst
);
460 static void test_migrate_end(QTestState
*from
, QTestState
*to
, bool test_dest
)
462 unsigned char dest_byte_a
, dest_byte_b
, dest_byte_c
, dest_byte_d
;
467 qtest_memread(to
, start_address
, &dest_byte_a
, 1);
469 /* Destination still running, wait for a byte to change */
471 qtest_memread(to
, start_address
, &dest_byte_b
, 1);
473 } while (dest_byte_a
== dest_byte_b
);
475 qtest_qmp_discard_response(to
, "{ 'execute' : 'stop'}");
477 /* With it stopped, check nothing changes */
478 qtest_memread(to
, start_address
, &dest_byte_c
, 1);
480 qtest_memread(to
, start_address
, &dest_byte_d
, 1);
481 g_assert_cmpint(dest_byte_c
, ==, dest_byte_d
);
483 check_guests_ram(to
);
489 cleanup("migsocket");
490 cleanup("src_serial");
491 cleanup("dest_serial");
494 static void deprecated_set_downtime(QTestState
*who
, const double value
)
501 cmd
= g_strdup_printf("{ 'execute': 'migrate_set_downtime',"
502 "'arguments': { 'value': %g } }", value
);
503 rsp
= qtest_qmp(who
, cmd
);
505 g_assert(qdict_haskey(rsp
, "return"));
507 result_int
= value
* 1000L;
508 expected
= g_strdup_printf("%" PRId64
, result_int
);
509 migrate_check_parameter(who
, "downtime-limit", expected
);
513 static void deprecated_set_speed(QTestState
*who
, const char *value
)
518 cmd
= g_strdup_printf("{ 'execute': 'migrate_set_speed',"
519 "'arguments': { 'value': %s } }", value
);
520 rsp
= qtest_qmp(who
, cmd
);
522 g_assert(qdict_haskey(rsp
, "return"));
524 migrate_check_parameter(who
, "max-bandwidth", value
);
527 static void test_deprecated(void)
531 from
= qtest_start("");
533 deprecated_set_downtime(from
, 0.12345);
534 deprecated_set_speed(from
, "12345");
539 static void test_migrate(void)
541 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
542 QTestState
*from
, *to
;
544 test_migrate_start(&from
, &to
, uri
, false);
546 migrate_set_capability(from
, "postcopy-ram", "true");
547 migrate_set_capability(to
, "postcopy-ram", "true");
548 migrate_set_capability(to
, "postcopy-blocktime", "true");
550 /* We want to pick a speed slow enough that the test completes
551 * quickly, but that it doesn't complete precopy even on a slow
552 * machine, so also set the downtime.
554 migrate_set_parameter(from
, "max-bandwidth", "100000000");
555 migrate_set_parameter(from
, "downtime-limit", "1");
557 /* Wait for the first serial output from the source */
558 wait_for_serial("src_serial");
562 wait_for_migration_pass(from
);
564 migrate_start_postcopy(from
);
567 qtest_qmp_eventwait(from
, "STOP");
570 qtest_qmp_eventwait(to
, "RESUME");
572 wait_for_serial("dest_serial");
573 wait_for_migration_complete(from
);
575 if (uffd_feature_thread_id
) {
580 test_migrate_end(from
, to
, true);
583 static void test_baddest(void)
585 QTestState
*from
, *to
;
586 QDict
*rsp
, *rsp_return
;
590 test_migrate_start(&from
, &to
, "tcp:0:0", true);
591 migrate(from
, "tcp:0:0");
593 rsp
= wait_command(from
, "{ 'execute': 'query-migrate' }");
594 rsp_return
= qdict_get_qdict(rsp
, "return");
596 status
= qdict_get_str(rsp_return
, "status");
598 g_assert(!strcmp(status
, "setup") || !(strcmp(status
, "failed")));
599 failed
= !strcmp(status
, "failed");
603 /* Is the machine currently running? */
604 rsp
= wait_command(from
, "{ 'execute': 'query-status' }");
605 g_assert(qdict_haskey(rsp
, "return"));
606 rsp_return
= qdict_get_qdict(rsp
, "return");
607 g_assert(qdict_haskey(rsp_return
, "running"));
608 g_assert(qdict_get_bool(rsp_return
, "running"));
611 test_migrate_end(from
, to
, false);
614 int main(int argc
, char **argv
)
616 char template[] = "/tmp/migration-test-XXXXXX";
619 g_test_init(&argc
, &argv
, NULL
);
621 if (!ufd_version_check()) {
625 tmpfs
= mkdtemp(template);
627 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno
));
631 module_call_init(MODULE_INIT_QOM
);
633 qtest_add_func("/migration/postcopy/unix", test_migrate
);
634 qtest_add_func("/migration/deprecated", test_deprecated
);
635 qtest_add_func("/migration/bad_dest", test_baddest
);
639 g_assert_cmpint(ret
, ==, 0);
643 g_test_message("unable to rmdir: path (%s): %s\n",
644 tmpfs
, strerror(errno
));