ui: fix virtual timers
[qemu/ar7.git] / tests / migration-test.c
blob20f38f1930ca4f0fc3e3c066729c00eb8e404b78
1 /*
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"
15 #include "libqtest.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;
30 unsigned end_address;
31 bool got_stop;
32 static bool uffd_feature_thread_id;
34 #if defined(__linux__)
35 #include <sys/syscall.h>
36 #include <sys/vfs.h>
37 #endif
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;
47 uint64_t ioctl_mask;
49 int ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
51 if (ufd == -1) {
52 g_test_message("Skipping test: userfaultfd not available");
53 return false;
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");
60 return false;
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");
68 return false;
71 return true;
74 #else
75 static bool ufd_version_check(void)
77 g_test_message("Skipping test: Userfault not available (builtdtime)");
78 return false;
81 #endif
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);
95 fclose(bootfile);
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;
111 g_free(serialpath);
112 do {
113 int readvalue = fgetc(serialfile);
115 if (!started) {
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
120 switch (readvalue) {
121 case '_':
122 started = 1;
123 break;
124 case EOF:
125 fseek(serialfile, 0, SEEK_SET);
126 usleep(1000);
127 break;
129 continue;
131 switch (readvalue) {
132 case 'A':
133 /* Fine */
134 break;
136 case 'B':
137 /* It's alive! */
138 fclose(serialfile);
139 return;
141 case EOF:
142 started = (strcmp(side, "src_serial") == 0 &&
143 strcmp(arch, "ppc64") == 0) ? 0 : 1;
144 fseek(serialfile, 0, SEEK_SET);
145 usleep(1000);
146 break;
148 default:
149 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
150 g_assert_not_reached();
152 } while (true);
155 static void stop_cb(void *opaque, const char *name, QDict *data)
157 if (!strcmp(name, "STOP")) {
158 got_stop = true;
163 * Events can get in the way of responses we are actually waiting for.
165 GCC_FMT_ATTR(2, 3)
166 static QDict *wait_command(QTestState *who, const char *command, ...)
168 va_list ap;
170 va_start(ap, command);
171 qtest_qmp_vsend(who, command, ap);
172 va_end(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
188 * g_free() after use
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"));
195 g_assert(status);
196 qobject_unref(rsp_return);
198 return status;
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;
209 uint64_t result;
211 rsp_return = migrate_query(who);
212 if (!qdict_haskey(rsp_return, "ram")) {
213 /* Still in setup */
214 result = 0;
215 } else {
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);
220 return result;
223 static void read_blocktime(QTestState *who)
225 QDict *rsp_return;
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,
233 const char *goal)
235 while (true) {
236 bool completed;
237 char *status;
239 status = migrate_query_status(who);
240 completed = strcmp(status, goal) == 0;
241 g_assert_cmpstr(status, !=, "failed");
242 g_free(status);
243 if (completed) {
244 return;
246 usleep(1000);
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);
258 uint64_t pass;
260 /* Wait for the 1st sync */
261 while (!got_stop && !initial_pass) {
262 usleep(1000);
263 initial_pass = get_migration_pass(who);
266 do {
267 usleep(1000);
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).
281 unsigned address;
282 uint8_t first_byte;
283 uint8_t last_byte;
284 bool hit_edge = false;
285 bool bad = 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)
293 uint8_t b;
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
299 * to us yet.
301 hit_edge = true;
302 last_byte = b;
303 } else {
304 fprintf(stderr, "Memory content inconsistency at %x"
305 " first_byte = %x last_byte = %x current = %x"
306 " hit_edge = %x\n",
307 address, first_byte, last_byte, b, hit_edge);
308 bad = true;
312 g_assert_false(bad);
315 static void cleanup(const char *filename)
317 char *path = g_strdup_printf("%s/%s", tmpfs, filename);
319 unlink(path);
320 g_free(path);
323 static void migrate_check_parameter(QTestState *who, const char *parameter,
324 long long value)
326 QDict *rsp_return;
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,
335 long long value)
337 QDict *rsp;
339 rsp = qtest_qmp(who,
340 "{ 'execute': 'migrate-set-parameters',"
341 "'arguments': { %s: %lld } }",
342 parameter, value);
343 g_assert(qdict_haskey(rsp, "return"));
344 qobject_unref(rsp);
345 migrate_check_parameter(who, parameter, value);
348 static void migrate_pause(QTestState *who)
350 QDict *rsp;
352 rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
353 qobject_unref(rsp);
356 static void migrate_recover(QTestState *who, const char *uri)
358 QDict *rsp;
360 rsp = wait_command(who,
361 "{ 'execute': 'migrate-recover', "
362 " 'id': 'recover-cmd', "
363 " 'arguments': { 'uri': %s } }",
364 uri);
365 qobject_unref(rsp);
368 static void migrate_set_capability(QTestState *who, const char *capability,
369 bool value)
371 QDict *rsp;
373 rsp = qtest_qmp(who,
374 "{ 'execute': 'migrate-set-capabilities',"
375 "'arguments': { "
376 "'capabilities': [ { "
377 "'capability': %s, 'state': %i } ] } }",
378 capability, value);
379 g_assert(qdict_haskey(rsp, "return"));
380 qobject_unref(rsp);
384 * Send QMP command "migrate".
385 * Arguments are built from @fmt... (formatted like
386 * qobject_from_jsonf_nofail()) with "uri": @uri spliced in.
388 GCC_FMT_ATTR(3, 4)
389 static void migrate(QTestState *who, const char *uri, const char *fmt, ...)
391 va_list ap;
392 QDict *args, *rsp;
394 va_start(ap, fmt);
395 args = qdict_from_vjsonf_nofail(fmt, ap);
396 va_end(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"));
403 qobject_unref(rsp);
406 static void migrate_postcopy_start(QTestState *from, QTestState *to)
408 QDict *rsp;
410 rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
411 qobject_unref(rsp);
413 if (!got_stop) {
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";
428 got_stop = false;
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"
441 " -incoming %s",
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,
453 start_address);
454 cmd_dst = g_strdup_printf("-machine accel=%s -m 256M"
455 " -name target,debug-threads=on"
456 " -serial file:%s/dest_serial"
457 " -incoming %s",
458 accel, tmpfs, uri);
460 start_address = PPC_TEST_MEM_START;
461 end_address = PPC_TEST_MEM_END;
462 } else {
463 g_assert_not_reached();
466 g_free(bootpath);
468 if (hide_stderr) {
469 gchar *tmp;
470 tmp = g_strdup_printf("%s 2>/dev/null", cmd_src);
471 g_free(cmd_src);
472 cmd_src = tmp;
474 tmp = g_strdup_printf("%s 2>/dev/null", cmd_dst);
475 g_free(cmd_dst);
476 cmd_dst = tmp;
479 *from = qtest_start(cmd_src);
480 g_free(cmd_src);
482 *to = qtest_init(cmd_dst);
483 g_free(cmd_dst);
484 return 0;
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;
491 qtest_quit(from);
493 if (test_dest) {
494 qtest_memread(to, start_address, &dest_byte_a, 1);
496 /* Destination still running, wait for a byte to change */
497 do {
498 qtest_memread(to, start_address, &dest_byte_b, 1);
499 usleep(1000 * 10);
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);
506 usleep(1000 * 200);
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);
513 qtest_quit(to);
515 cleanup("bootsect");
516 cleanup("migsocket");
517 cleanup("src_serial");
518 cleanup("dest_serial");
521 static void deprecated_set_downtime(QTestState *who, const double value)
523 QDict *rsp;
525 rsp = qtest_qmp(who,
526 "{ 'execute': 'migrate_set_downtime',"
527 " 'arguments': { 'value': %f } }", value);
528 g_assert(qdict_haskey(rsp, "return"));
529 qobject_unref(rsp);
530 migrate_check_parameter(who, "downtime-limit", value * 1000);
533 static void deprecated_set_speed(QTestState *who, long long value)
535 QDict *rsp;
537 rsp = qtest_qmp(who, "{ 'execute': 'migrate_set_speed',"
538 "'arguments': { 'value': %lld } }", value);
539 g_assert(qdict_haskey(rsp, "return"));
540 qobject_unref(rsp);
541 migrate_check_parameter(who, "max-bandwidth", value);
544 static void test_deprecated(void)
546 QTestState *from;
548 from = qtest_start("");
550 deprecated_set_downtime(from, 0.12345);
551 deprecated_set_speed(from, 12345);
553 qtest_quit(from);
556 static int migrate_postcopy_prepare(QTestState **from_ptr,
557 QTestState **to_ptr,
558 bool hide_error)
560 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
561 QTestState *from, *to;
563 if (test_migrate_start(&from, &to, uri, hide_error)) {
564 return -1;
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, "{}");
582 g_free(uri);
584 wait_for_migration_pass(from);
586 *from_ptr = from;
587 *to_ptr = to;
589 return 0;
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) {
600 read_blocktime(to);
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)) {
611 return;
613 migrate_postcopy_start(from, to);
614 migrate_postcopy_complete(from, to);
617 static void test_postcopy_recovery(void)
619 QTestState *from, *to;
620 char *uri;
622 if (migrate_postcopy_prepare(&from, &to, true)) {
623 return;
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
642 migrate_pause(from);
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}");
665 g_free(uri);
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;
676 QDict *rsp_return;
677 char *status;
678 bool failed;
680 if (test_migrate_start(&from, &to, "tcp:0:0", true)) {
681 return;
683 migrate(from, "tcp:0:0", "{}");
684 do {
685 status = migrate_query_status(from);
686 g_assert(!strcmp(status, "setup") || !(strcmp(status, "failed")));
687 failed = !strcmp(status, "failed");
688 g_free(status);
689 } while (!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)) {
706 return;
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);
715 /* 1GB/s */
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);
728 if (!got_stop) {
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);
738 g_free(uri);
741 int main(int argc, char **argv)
743 char template[] = "/tmp/migration-test-XXXXXX";
744 int ret;
746 g_test_init(&argc, &argv, NULL);
748 if (!ufd_version_check()) {
749 return 0;
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
755 * some reason)
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");
760 return 0;
763 tmpfs = mkdtemp(template);
764 if (!tmpfs) {
765 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno));
767 g_assert(tmpfs);
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);
777 ret = g_test_run();
779 g_assert_cmpint(ret, ==, 0);
781 ret = rmdir(tmpfs);
782 if (ret != 0) {
783 g_test_message("unable to rmdir: path (%s): %s\n",
784 tmpfs, strerror(errno));
787 return ret;