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;
30 #if defined(__linux__)
31 #include <sys/syscall.h>
35 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
36 #include <sys/eventfd.h>
37 #include <sys/ioctl.h>
38 #include <linux/userfaultfd.h>
40 static bool ufd_version_check(void)
42 struct uffdio_api api_struct
;
45 int ufd
= syscall(__NR_userfaultfd
, O_CLOEXEC
);
48 g_test_message("Skipping test: userfaultfd not available");
52 api_struct
.api
= UFFD_API
;
53 api_struct
.features
= 0;
54 if (ioctl(ufd
, UFFDIO_API
, &api_struct
)) {
55 g_test_message("Skipping test: UFFDIO_API failed");
59 ioctl_mask
= (__u64
)1 << _UFFDIO_REGISTER
|
60 (__u64
)1 << _UFFDIO_UNREGISTER
;
61 if ((api_struct
.ioctls
& ioctl_mask
) != ioctl_mask
) {
62 g_test_message("Skipping test: Missing userfault feature");
70 static bool ufd_version_check(void)
72 g_test_message("Skipping test: Userfault not available (builtdtime)");
78 static const char *tmpfs
;
80 /* A simple PC boot sector that modifies memory (1-100MB) quickly
81 * outputting a 'B' every so often if it's still running.
83 #include "tests/migration/x86-a-b-bootblock.h"
85 static void init_bootfile_x86(const char *bootpath
)
87 FILE *bootfile
= fopen(bootpath
, "wb");
89 g_assert_cmpint(fwrite(x86_bootsect
, 512, 1, bootfile
), ==, 1);
93 static void init_bootfile_ppc(const char *bootpath
)
96 char buf
[MIN_NVRAM_SIZE
];
97 ChrpNvramPartHdr
*header
= (ChrpNvramPartHdr
*)buf
;
99 memset(buf
, 0, MIN_NVRAM_SIZE
);
101 /* Create a "common" partition in nvram to store boot-command property */
103 header
->signature
= CHRP_NVPART_SYSTEM
;
104 memcpy(header
->name
, "common", 6);
105 chrp_nvram_finish_partition(header
, MIN_NVRAM_SIZE
);
107 /* FW_MAX_SIZE is 4MB, but slof.bin is only 900KB,
108 * so let's modify memory between 1MB and 100MB
109 * to do like PC bootsector
113 "boot-command=hex .\" _\" begin %x %x do i c@ 1 + i c! 1000 +loop "
114 ".\" B\" 0 until", end_address
, start_address
);
116 /* Write partition to the NVRAM file */
118 bootfile
= fopen(bootpath
, "wb");
119 g_assert_cmpint(fwrite(buf
, MIN_NVRAM_SIZE
, 1, bootfile
), ==, 1);
124 * Wait for some output in the serial output file,
125 * we get an 'A' followed by an endless string of 'B's
126 * but on the destination we won't have the A.
128 static void wait_for_serial(const char *side
)
130 char *serialpath
= g_strdup_printf("%s/%s", tmpfs
, side
);
131 FILE *serialfile
= fopen(serialpath
, "r");
132 const char *arch
= qtest_get_arch();
133 int started
= (strcmp(side
, "src_serial") == 0 &&
134 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
138 int readvalue
= fgetc(serialfile
);
141 /* SLOF prints its banner before starting test,
142 * to ignore it, mark the start of the test with '_',
143 * ignore all characters until this marker
150 fseek(serialfile
, 0, SEEK_SET
);
167 started
= (strcmp(side
, "src_serial") == 0 &&
168 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
169 fseek(serialfile
, 0, SEEK_SET
);
174 fprintf(stderr
, "Unexpected %d on %s serial\n", readvalue
, side
);
175 g_assert_not_reached();
181 * Events can get in the way of responses we are actually waiting for.
183 static QDict
*wait_command(QTestState
*who
, const char *command
)
185 const char *event_string
;
188 response
= qtest_qmp(who
, command
);
190 while (qdict_haskey(response
, "event")) {
191 /* OK, it was an event */
192 event_string
= qdict_get_str(response
, "event");
193 if (!strcmp(event_string
, "STOP")) {
197 response
= qtest_qmp_receive(who
);
204 * It's tricky to use qemu's migration event capability with qtest,
205 * events suddenly appearing confuse the qmp()/hmp() responses.
208 static uint64_t get_migration_pass(QTestState
*who
)
210 QDict
*rsp
, *rsp_return
, *rsp_ram
;
213 rsp
= wait_command(who
, "{ 'execute': 'query-migrate' }");
214 rsp_return
= qdict_get_qdict(rsp
, "return");
215 if (!qdict_haskey(rsp_return
, "ram")) {
219 rsp_ram
= qdict_get_qdict(rsp_return
, "ram");
220 result
= qdict_get_try_int(rsp_ram
, "dirty-sync-count", 0);
226 static void wait_for_migration_complete(QTestState
*who
)
229 QDict
*rsp
, *rsp_return
;
233 rsp
= wait_command(who
, "{ 'execute': 'query-migrate' }");
234 rsp_return
= qdict_get_qdict(rsp
, "return");
235 status
= qdict_get_str(rsp_return
, "status");
236 completed
= strcmp(status
, "completed") == 0;
237 g_assert_cmpstr(status
, !=, "failed");
246 static void wait_for_migration_pass(QTestState
*who
)
248 uint64_t initial_pass
= get_migration_pass(who
);
251 /* Wait for the 1st sync */
252 while (!got_stop
&& !initial_pass
) {
254 initial_pass
= get_migration_pass(who
);
259 pass
= get_migration_pass(who
);
260 } while (pass
== initial_pass
&& !got_stop
);
263 static void check_guests_ram(QTestState
*who
)
265 /* Our ASM test will have been incrementing one byte from each page from
266 * 1MB to <100MB in order.
267 * This gives us a constraint that any page's byte should be equal or less
268 * than the previous pages byte (mod 256); and they should all be equal
269 * except for one transition at the point where we meet the incrementer.
270 * (We're running this with the guest stopped).
275 bool hit_edge
= false;
278 qtest_memread(who
, start_address
, &first_byte
, 1);
279 last_byte
= first_byte
;
281 for (address
= start_address
+ 4096; address
< end_address
; address
+= 4096)
284 qtest_memread(who
, address
, &b
, 1);
285 if (b
!= last_byte
) {
286 if (((b
+ 1) % 256) == last_byte
&& !hit_edge
) {
287 /* This is OK, the guest stopped at the point of
288 * incrementing the previous page but didn't get
293 fprintf(stderr
, "Memory content inconsistency at %x"
294 " first_byte = %x last_byte = %x current = %x"
296 address
, first_byte
, last_byte
, b
, hit_edge
);
305 static void cleanup(const char *filename
)
307 char *path
= g_strdup_printf("%s/%s", tmpfs
, filename
);
313 static void migrate_check_parameter(QTestState
*who
, const char *parameter
,
316 QDict
*rsp
, *rsp_return
;
319 rsp
= wait_command(who
, "{ 'execute': 'query-migrate-parameters' }");
320 rsp_return
= qdict_get_qdict(rsp
, "return");
321 result
= g_strdup_printf("%" PRId64
,
322 qdict_get_try_int(rsp_return
, parameter
, -1));
323 g_assert_cmpstr(result
, ==, value
);
328 static void migrate_set_parameter(QTestState
*who
, const char *parameter
,
334 cmd
= g_strdup_printf("{ 'execute': 'migrate-set-parameters',"
335 "'arguments': { '%s': %s } }",
337 rsp
= qtest_qmp(who
, cmd
);
339 g_assert(qdict_haskey(rsp
, "return"));
341 migrate_check_parameter(who
, parameter
, value
);
344 static void migrate_set_capability(QTestState
*who
, const char *capability
,
350 cmd
= g_strdup_printf("{ 'execute': 'migrate-set-capabilities',"
352 "'capabilities': [ { "
353 "'capability': '%s', 'state': %s } ] } }",
355 rsp
= qtest_qmp(who
, cmd
);
357 g_assert(qdict_haskey(rsp
, "return"));
361 static void migrate(QTestState
*who
, const char *uri
)
366 cmd
= g_strdup_printf("{ 'execute': 'migrate',"
367 "'arguments': { 'uri': '%s' } }",
369 rsp
= qtest_qmp(who
, cmd
);
371 g_assert(qdict_haskey(rsp
, "return"));
375 static void migrate_start_postcopy(QTestState
*who
)
379 rsp
= wait_command(who
, "{ 'execute': 'migrate-start-postcopy' }");
380 g_assert(qdict_haskey(rsp
, "return"));
384 static void test_migrate_start(QTestState
**from
, QTestState
**to
,
385 const char *uri
, bool hide_stderr
)
387 gchar
*cmd_src
, *cmd_dst
;
388 char *bootpath
= g_strdup_printf("%s/bootsect", tmpfs
);
389 const char *arch
= qtest_get_arch();
390 const char *accel
= "kvm:tcg";
394 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
395 init_bootfile_x86(bootpath
);
396 cmd_src
= g_strdup_printf("-machine accel=%s -m 150M"
397 " -name source,debug-threads=on"
398 " -serial file:%s/src_serial"
399 " -drive file=%s,format=raw",
400 accel
, tmpfs
, bootpath
);
401 cmd_dst
= g_strdup_printf("-machine accel=%s -m 150M"
402 " -name target,debug-threads=on"
403 " -serial file:%s/dest_serial"
404 " -drive file=%s,format=raw"
406 accel
, tmpfs
, bootpath
, uri
);
407 } else if (strcmp(arch
, "ppc64") == 0) {
409 /* On ppc64, the test only works with kvm-hv, but not with kvm-pr */
410 if (access("/sys/module/kvm_hv", F_OK
)) {
413 init_bootfile_ppc(bootpath
);
414 cmd_src
= g_strdup_printf("-machine accel=%s -m 256M"
415 " -name source,debug-threads=on"
416 " -serial file:%s/src_serial"
417 " -drive file=%s,if=pflash,format=raw",
418 accel
, tmpfs
, bootpath
);
419 cmd_dst
= g_strdup_printf("-machine accel=%s -m 256M"
420 " -name target,debug-threads=on"
421 " -serial file:%s/dest_serial"
425 g_assert_not_reached();
432 tmp
= g_strdup_printf("%s 2>/dev/null", cmd_src
);
436 tmp
= g_strdup_printf("%s 2>/dev/null", cmd_dst
);
441 *from
= qtest_start(cmd_src
);
444 *to
= qtest_init(cmd_dst
);
448 static void test_migrate_end(QTestState
*from
, QTestState
*to
, bool test_dest
)
450 unsigned char dest_byte_a
, dest_byte_b
, dest_byte_c
, dest_byte_d
;
455 qtest_memread(to
, start_address
, &dest_byte_a
, 1);
457 /* Destination still running, wait for a byte to change */
459 qtest_memread(to
, start_address
, &dest_byte_b
, 1);
461 } while (dest_byte_a
== dest_byte_b
);
463 qtest_qmp_discard_response(to
, "{ 'execute' : 'stop'}");
465 /* With it stopped, check nothing changes */
466 qtest_memread(to
, start_address
, &dest_byte_c
, 1);
468 qtest_memread(to
, start_address
, &dest_byte_d
, 1);
469 g_assert_cmpint(dest_byte_c
, ==, dest_byte_d
);
471 check_guests_ram(to
);
477 cleanup("migsocket");
478 cleanup("src_serial");
479 cleanup("dest_serial");
482 static void deprecated_set_downtime(QTestState
*who
, const double value
)
489 cmd
= g_strdup_printf("{ 'execute': 'migrate_set_downtime',"
490 "'arguments': { 'value': %g } }", value
);
491 rsp
= qtest_qmp(who
, cmd
);
493 g_assert(qdict_haskey(rsp
, "return"));
495 result_int
= value
* 1000L;
496 expected
= g_strdup_printf("%" PRId64
, result_int
);
497 migrate_check_parameter(who
, "downtime-limit", expected
);
501 static void deprecated_set_speed(QTestState
*who
, const char *value
)
506 cmd
= g_strdup_printf("{ 'execute': 'migrate_set_speed',"
507 "'arguments': { 'value': %s } }", value
);
508 rsp
= qtest_qmp(who
, cmd
);
510 g_assert(qdict_haskey(rsp
, "return"));
512 migrate_check_parameter(who
, "max-bandwidth", value
);
515 static void test_deprecated(void)
519 from
= qtest_start("");
521 deprecated_set_downtime(from
, 0.12345);
522 deprecated_set_speed(from
, "12345");
527 static void test_migrate(void)
529 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
530 QTestState
*from
, *to
;
532 test_migrate_start(&from
, &to
, uri
, false);
534 migrate_set_capability(from
, "postcopy-ram", "true");
535 migrate_set_capability(to
, "postcopy-ram", "true");
537 /* We want to pick a speed slow enough that the test completes
538 * quickly, but that it doesn't complete precopy even on a slow
539 * machine, so also set the downtime.
541 migrate_set_parameter(from
, "max-bandwidth", "100000000");
542 migrate_set_parameter(from
, "downtime-limit", "1");
544 /* Wait for the first serial output from the source */
545 wait_for_serial("src_serial");
549 wait_for_migration_pass(from
);
551 migrate_start_postcopy(from
);
554 qtest_qmp_eventwait(from
, "STOP");
557 qtest_qmp_eventwait(to
, "RESUME");
559 wait_for_serial("dest_serial");
560 wait_for_migration_complete(from
);
564 test_migrate_end(from
, to
, true);
567 static void test_baddest(void)
569 QTestState
*from
, *to
;
570 QDict
*rsp
, *rsp_return
;
574 test_migrate_start(&from
, &to
, "tcp:0:0", true);
575 migrate(from
, "tcp:0:0");
577 rsp
= wait_command(from
, "{ 'execute': 'query-migrate' }");
578 rsp_return
= qdict_get_qdict(rsp
, "return");
580 status
= qdict_get_str(rsp_return
, "status");
582 g_assert(!strcmp(status
, "setup") || !(strcmp(status
, "failed")));
583 failed
= !strcmp(status
, "failed");
587 /* Is the machine currently running? */
588 rsp
= wait_command(from
, "{ 'execute': 'query-status' }");
589 g_assert(qdict_haskey(rsp
, "return"));
590 rsp_return
= qdict_get_qdict(rsp
, "return");
591 g_assert(qdict_haskey(rsp_return
, "running"));
592 g_assert(qdict_get_bool(rsp_return
, "running"));
595 test_migrate_end(from
, to
, false);
598 int main(int argc
, char **argv
)
600 char template[] = "/tmp/migration-test-XXXXXX";
603 g_test_init(&argc
, &argv
, NULL
);
605 if (!ufd_version_check()) {
609 tmpfs
= mkdtemp(template);
611 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno
));
615 module_call_init(MODULE_INIT_QOM
);
617 qtest_add_func("/migration/postcopy/unix", test_migrate
);
618 qtest_add_func("/migration/deprecated", test_deprecated
);
619 qtest_add_func("/migration/bad_dest", test_baddest
);
623 g_assert_cmpint(ret
, ==, 0);
627 g_test_message("unable to rmdir: path (%s): %s\n",
628 tmpfs
, strerror(errno
));