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
,
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();
430 *from
= qtest_start(cmd_src
);
433 *to
= qtest_init(cmd_dst
);
437 static void test_migrate_end(QTestState
*from
, QTestState
*to
, bool test_dest
)
439 unsigned char dest_byte_a
, dest_byte_b
, dest_byte_c
, dest_byte_d
;
444 qtest_memread(to
, start_address
, &dest_byte_a
, 1);
446 /* Destination still running, wait for a byte to change */
448 qtest_memread(to
, start_address
, &dest_byte_b
, 1);
450 } while (dest_byte_a
== dest_byte_b
);
452 qtest_qmp_discard_response(to
, "{ 'execute' : 'stop'}");
454 /* With it stopped, check nothing changes */
455 qtest_memread(to
, start_address
, &dest_byte_c
, 1);
457 qtest_memread(to
, start_address
, &dest_byte_d
, 1);
458 g_assert_cmpint(dest_byte_c
, ==, dest_byte_d
);
460 check_guests_ram(to
);
466 cleanup("migsocket");
467 cleanup("src_serial");
468 cleanup("dest_serial");
471 static void deprecated_set_downtime(QTestState
*who
, const double value
)
478 cmd
= g_strdup_printf("{ 'execute': 'migrate_set_downtime',"
479 "'arguments': { 'value': %g } }", value
);
480 rsp
= qtest_qmp(who
, cmd
);
482 g_assert(qdict_haskey(rsp
, "return"));
484 result_int
= value
* 1000L;
485 expected
= g_strdup_printf("%" PRId64
, result_int
);
486 migrate_check_parameter(who
, "downtime-limit", expected
);
490 static void deprecated_set_speed(QTestState
*who
, const char *value
)
495 cmd
= g_strdup_printf("{ 'execute': 'migrate_set_speed',"
496 "'arguments': { 'value': %s } }", value
);
497 rsp
= qtest_qmp(who
, cmd
);
499 g_assert(qdict_haskey(rsp
, "return"));
501 migrate_check_parameter(who
, "max-bandwidth", value
);
504 static void test_deprecated(void)
508 from
= qtest_start("");
510 deprecated_set_downtime(from
, 0.12345);
511 deprecated_set_speed(from
, "12345");
516 static void test_migrate(void)
518 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
519 QTestState
*from
, *to
;
521 test_migrate_start(&from
, &to
, uri
);
523 migrate_set_capability(from
, "postcopy-ram", "true");
524 migrate_set_capability(to
, "postcopy-ram", "true");
526 /* We want to pick a speed slow enough that the test completes
527 * quickly, but that it doesn't complete precopy even on a slow
528 * machine, so also set the downtime.
530 migrate_set_parameter(from
, "max-bandwidth", "100000000");
531 migrate_set_parameter(from
, "downtime-limit", "1");
533 /* Wait for the first serial output from the source */
534 wait_for_serial("src_serial");
538 wait_for_migration_pass(from
);
540 migrate_start_postcopy(from
);
543 qtest_qmp_eventwait(from
, "STOP");
546 qtest_qmp_eventwait(to
, "RESUME");
548 wait_for_serial("dest_serial");
549 wait_for_migration_complete(from
);
553 test_migrate_end(from
, to
, true);
556 static void test_baddest(void)
558 QTestState
*from
, *to
;
559 QDict
*rsp
, *rsp_return
;
563 test_migrate_start(&from
, &to
, "tcp:0:0");
564 migrate(from
, "tcp:0:0");
566 rsp
= wait_command(from
, "{ 'execute': 'query-migrate' }");
567 rsp_return
= qdict_get_qdict(rsp
, "return");
569 status
= qdict_get_str(rsp_return
, "status");
571 g_assert(!strcmp(status
, "setup") || !(strcmp(status
, "failed")));
572 failed
= !strcmp(status
, "failed");
576 /* Is the machine currently running? */
577 rsp
= wait_command(from
, "{ 'execute': 'query-status' }");
578 g_assert(qdict_haskey(rsp
, "return"));
579 rsp_return
= qdict_get_qdict(rsp
, "return");
580 g_assert(qdict_haskey(rsp_return
, "running"));
581 g_assert(qdict_get_bool(rsp_return
, "running"));
584 test_migrate_end(from
, to
, false);
587 int main(int argc
, char **argv
)
589 char template[] = "/tmp/migration-test-XXXXXX";
592 g_test_init(&argc
, &argv
, NULL
);
594 if (!ufd_version_check()) {
598 tmpfs
= mkdtemp(template);
600 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno
));
604 module_call_init(MODULE_INIT_QOM
);
606 qtest_add_func("/migration/postcopy/unix", test_migrate
);
607 qtest_add_func("/migration/deprecated", test_deprecated
);
608 qtest_add_func("/migration/bad_dest", test_baddest
);
612 g_assert_cmpint(ret
, ==, 0);
616 g_test_message("unable to rmdir: path (%s): %s\n",
617 tmpfs
, strerror(errno
));