tests/migration-test: Silence the kvm_hv message by default
[qemu/ar7.git] / tests / migration-test.c
blob0e687b751270a294741c927eb78a58983e211c04
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 /* TODO actually test the results and get rid of this */
25 #define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__))
27 const unsigned start_address = 1024 * 1024;
28 const unsigned end_address = 100 * 1024 * 1024;
29 bool got_stop;
30 static bool uffd_feature_thread_id;
32 #if defined(__linux__)
33 #include <sys/syscall.h>
34 #include <sys/vfs.h>
35 #endif
37 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
38 #include <sys/eventfd.h>
39 #include <sys/ioctl.h>
40 #include <linux/userfaultfd.h>
42 static bool ufd_version_check(void)
44 struct uffdio_api api_struct;
45 uint64_t ioctl_mask;
47 int ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
49 if (ufd == -1) {
50 g_test_message("Skipping test: userfaultfd not available");
51 return false;
54 api_struct.api = UFFD_API;
55 api_struct.features = 0;
56 if (ioctl(ufd, UFFDIO_API, &api_struct)) {
57 g_test_message("Skipping test: UFFDIO_API failed");
58 return false;
60 uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
62 ioctl_mask = (__u64)1 << _UFFDIO_REGISTER |
63 (__u64)1 << _UFFDIO_UNREGISTER;
64 if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
65 g_test_message("Skipping test: Missing userfault feature");
66 return false;
69 return true;
72 #else
73 static bool ufd_version_check(void)
75 g_test_message("Skipping test: Userfault not available (builtdtime)");
76 return false;
79 #endif
81 static const char *tmpfs;
83 /* A simple PC boot sector that modifies memory (1-100MB) quickly
84 * outputting a 'B' every so often if it's still running.
86 #include "tests/migration/x86-a-b-bootblock.h"
88 static void init_bootfile_x86(const char *bootpath)
90 FILE *bootfile = fopen(bootpath, "wb");
92 g_assert_cmpint(fwrite(x86_bootsect, 512, 1, bootfile), ==, 1);
93 fclose(bootfile);
97 * Wait for some output in the serial output file,
98 * we get an 'A' followed by an endless string of 'B's
99 * but on the destination we won't have the A.
101 static void wait_for_serial(const char *side)
103 char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
104 FILE *serialfile = fopen(serialpath, "r");
105 const char *arch = qtest_get_arch();
106 int started = (strcmp(side, "src_serial") == 0 &&
107 strcmp(arch, "ppc64") == 0) ? 0 : 1;
109 g_free(serialpath);
110 do {
111 int readvalue = fgetc(serialfile);
113 if (!started) {
114 /* SLOF prints its banner before starting test,
115 * to ignore it, mark the start of the test with '_',
116 * ignore all characters until this marker
118 switch (readvalue) {
119 case '_':
120 started = 1;
121 break;
122 case EOF:
123 fseek(serialfile, 0, SEEK_SET);
124 usleep(1000);
125 break;
127 continue;
129 switch (readvalue) {
130 case 'A':
131 /* Fine */
132 break;
134 case 'B':
135 /* It's alive! */
136 fclose(serialfile);
137 return;
139 case EOF:
140 started = (strcmp(side, "src_serial") == 0 &&
141 strcmp(arch, "ppc64") == 0) ? 0 : 1;
142 fseek(serialfile, 0, SEEK_SET);
143 usleep(1000);
144 break;
146 default:
147 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
148 g_assert_not_reached();
150 } while (true);
153 static void stop_cb(void *opaque, const char *name, QDict *data)
155 if (!strcmp(name, "STOP")) {
156 got_stop = true;
161 * Events can get in the way of responses we are actually waiting for.
163 GCC_FMT_ATTR(2, 3)
164 static QDict *wait_command(QTestState *who, const char *command, ...)
166 va_list ap;
168 va_start(ap, command);
169 qtest_qmp_vsend(who, command, ap);
170 va_end(ap);
172 return qtest_qmp_receive_success(who, stop_cb, NULL);
176 * Note: caller is responsible to free the returned object via
177 * qobject_unref() after use
179 static QDict *migrate_query(QTestState *who)
181 return wait_command(who, "{ 'execute': 'query-migrate' }");
185 * Note: caller is responsible to free the returned object via
186 * g_free() after use
188 static gchar *migrate_query_status(QTestState *who)
190 QDict *rsp_return = migrate_query(who);
191 gchar *status = g_strdup(qdict_get_str(rsp_return, "status"));
193 g_assert(status);
194 qobject_unref(rsp_return);
196 return status;
200 * It's tricky to use qemu's migration event capability with qtest,
201 * events suddenly appearing confuse the qmp()/hmp() responses.
204 static uint64_t get_migration_pass(QTestState *who)
206 QDict *rsp_return, *rsp_ram;
207 uint64_t result;
209 rsp_return = migrate_query(who);
210 if (!qdict_haskey(rsp_return, "ram")) {
211 /* Still in setup */
212 result = 0;
213 } else {
214 rsp_ram = qdict_get_qdict(rsp_return, "ram");
215 result = qdict_get_try_int(rsp_ram, "dirty-sync-count", 0);
217 qobject_unref(rsp_return);
218 return result;
221 static void read_blocktime(QTestState *who)
223 QDict *rsp_return;
225 rsp_return = migrate_query(who);
226 g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
227 qobject_unref(rsp_return);
230 static void wait_for_migration_status(QTestState *who,
231 const char *goal)
233 while (true) {
234 bool completed;
235 char *status;
237 status = migrate_query_status(who);
238 completed = strcmp(status, goal) == 0;
239 g_assert_cmpstr(status, !=, "failed");
240 g_free(status);
241 if (completed) {
242 return;
244 usleep(1000);
248 static void wait_for_migration_complete(QTestState *who)
250 wait_for_migration_status(who, "completed");
253 static void wait_for_migration_pass(QTestState *who)
255 uint64_t initial_pass = get_migration_pass(who);
256 uint64_t pass;
258 /* Wait for the 1st sync */
259 while (!got_stop && !initial_pass) {
260 usleep(1000);
261 initial_pass = get_migration_pass(who);
264 do {
265 usleep(1000);
266 pass = get_migration_pass(who);
267 } while (pass == initial_pass && !got_stop);
270 static void check_guests_ram(QTestState *who)
272 /* Our ASM test will have been incrementing one byte from each page from
273 * 1MB to <100MB in order.
274 * This gives us a constraint that any page's byte should be equal or less
275 * than the previous pages byte (mod 256); and they should all be equal
276 * except for one transition at the point where we meet the incrementer.
277 * (We're running this with the guest stopped).
279 unsigned address;
280 uint8_t first_byte;
281 uint8_t last_byte;
282 bool hit_edge = false;
283 bool bad = false;
285 qtest_memread(who, start_address, &first_byte, 1);
286 last_byte = first_byte;
288 for (address = start_address + 4096; address < end_address; address += 4096)
290 uint8_t b;
291 qtest_memread(who, address, &b, 1);
292 if (b != last_byte) {
293 if (((b + 1) % 256) == last_byte && !hit_edge) {
294 /* This is OK, the guest stopped at the point of
295 * incrementing the previous page but didn't get
296 * to us yet.
298 hit_edge = true;
299 last_byte = b;
300 } else {
301 fprintf(stderr, "Memory content inconsistency at %x"
302 " first_byte = %x last_byte = %x current = %x"
303 " hit_edge = %x\n",
304 address, first_byte, last_byte, b, hit_edge);
305 bad = true;
309 g_assert_false(bad);
312 static void cleanup(const char *filename)
314 char *path = g_strdup_printf("%s/%s", tmpfs, filename);
316 unlink(path);
317 g_free(path);
320 static void migrate_check_parameter(QTestState *who, const char *parameter,
321 long long value)
323 QDict *rsp_return;
325 rsp_return = wait_command(who,
326 "{ 'execute': 'query-migrate-parameters' }");
327 g_assert_cmpint(qdict_get_int(rsp_return, parameter), ==, value);
328 qobject_unref(rsp_return);
331 static void migrate_set_parameter(QTestState *who, const char *parameter,
332 long long value)
334 QDict *rsp;
336 rsp = qtest_qmp(who,
337 "{ 'execute': 'migrate-set-parameters',"
338 "'arguments': { %s: %lld } }",
339 parameter, value);
340 g_assert(qdict_haskey(rsp, "return"));
341 qobject_unref(rsp);
342 migrate_check_parameter(who, parameter, value);
345 static void migrate_pause(QTestState *who)
347 QDict *rsp;
349 rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
350 qobject_unref(rsp);
353 static void migrate_recover(QTestState *who, const char *uri)
355 QDict *rsp;
357 rsp = wait_command(who,
358 "{ 'execute': 'migrate-recover', "
359 " 'id': 'recover-cmd', "
360 " 'arguments': { 'uri': %s } }",
361 uri);
362 qobject_unref(rsp);
365 static void migrate_set_capability(QTestState *who, const char *capability,
366 bool value)
368 QDict *rsp;
370 rsp = qtest_qmp(who,
371 "{ 'execute': 'migrate-set-capabilities',"
372 "'arguments': { "
373 "'capabilities': [ { "
374 "'capability': %s, 'state': %i } ] } }",
375 capability, value);
376 g_assert(qdict_haskey(rsp, "return"));
377 qobject_unref(rsp);
381 * Send QMP command "migrate".
382 * Arguments are built from @fmt... (formatted like
383 * qobject_from_jsonf_nofail()) with "uri": @uri spliced in.
385 GCC_FMT_ATTR(3, 4)
386 static void migrate(QTestState *who, const char *uri, const char *fmt, ...)
388 va_list ap;
389 QDict *args, *rsp;
391 va_start(ap, fmt);
392 args = qdict_from_vjsonf_nofail(fmt, ap);
393 va_end(ap);
395 g_assert(!qdict_haskey(args, "uri"));
396 qdict_put_str(args, "uri", uri);
398 rsp = qmp("{ 'execute': 'migrate', 'arguments': %p}", args);
399 g_assert(qdict_haskey(rsp, "return"));
400 qobject_unref(rsp);
403 static void migrate_postcopy_start(QTestState *from, QTestState *to)
405 QDict *rsp;
407 rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
408 qobject_unref(rsp);
410 if (!got_stop) {
411 qtest_qmp_eventwait(from, "STOP");
414 qtest_qmp_eventwait(to, "RESUME");
417 static int test_migrate_start(QTestState **from, QTestState **to,
418 const char *uri, bool hide_stderr)
420 gchar *cmd_src, *cmd_dst;
421 char *bootpath = g_strdup_printf("%s/bootsect", tmpfs);
422 const char *arch = qtest_get_arch();
423 const char *accel = "kvm:tcg";
425 got_stop = false;
427 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
428 init_bootfile_x86(bootpath);
429 cmd_src = g_strdup_printf("-machine accel=%s -m 150M"
430 " -name source,debug-threads=on"
431 " -serial file:%s/src_serial"
432 " -drive file=%s,format=raw",
433 accel, tmpfs, bootpath);
434 cmd_dst = g_strdup_printf("-machine accel=%s -m 150M"
435 " -name target,debug-threads=on"
436 " -serial file:%s/dest_serial"
437 " -drive file=%s,format=raw"
438 " -incoming %s",
439 accel, tmpfs, bootpath, uri);
440 } else if (strcmp(arch, "ppc64") == 0) {
441 cmd_src = g_strdup_printf("-machine accel=%s -m 256M"
442 " -name source,debug-threads=on"
443 " -serial file:%s/src_serial"
444 " -prom-env '"
445 "boot-command=hex .\" _\" begin %x %x "
446 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
447 "until'", accel, tmpfs, end_address,
448 start_address);
449 cmd_dst = g_strdup_printf("-machine accel=%s -m 256M"
450 " -name target,debug-threads=on"
451 " -serial file:%s/dest_serial"
452 " -incoming %s",
453 accel, tmpfs, uri);
454 } else {
455 g_assert_not_reached();
458 g_free(bootpath);
460 if (hide_stderr) {
461 gchar *tmp;
462 tmp = g_strdup_printf("%s 2>/dev/null", cmd_src);
463 g_free(cmd_src);
464 cmd_src = tmp;
466 tmp = g_strdup_printf("%s 2>/dev/null", cmd_dst);
467 g_free(cmd_dst);
468 cmd_dst = tmp;
471 *from = qtest_start(cmd_src);
472 g_free(cmd_src);
474 *to = qtest_init(cmd_dst);
475 g_free(cmd_dst);
476 return 0;
479 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
481 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
483 qtest_quit(from);
485 if (test_dest) {
486 qtest_memread(to, start_address, &dest_byte_a, 1);
488 /* Destination still running, wait for a byte to change */
489 do {
490 qtest_memread(to, start_address, &dest_byte_b, 1);
491 usleep(1000 * 10);
492 } while (dest_byte_a == dest_byte_b);
494 qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
496 /* With it stopped, check nothing changes */
497 qtest_memread(to, start_address, &dest_byte_c, 1);
498 usleep(1000 * 200);
499 qtest_memread(to, start_address, &dest_byte_d, 1);
500 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
502 check_guests_ram(to);
505 qtest_quit(to);
507 cleanup("bootsect");
508 cleanup("migsocket");
509 cleanup("src_serial");
510 cleanup("dest_serial");
513 static void deprecated_set_downtime(QTestState *who, const double value)
515 QDict *rsp;
517 rsp = qtest_qmp(who,
518 "{ 'execute': 'migrate_set_downtime',"
519 " 'arguments': { 'value': %f } }", value);
520 g_assert(qdict_haskey(rsp, "return"));
521 qobject_unref(rsp);
522 migrate_check_parameter(who, "downtime-limit", value * 1000);
525 static void deprecated_set_speed(QTestState *who, long long value)
527 QDict *rsp;
529 rsp = qtest_qmp(who, "{ 'execute': 'migrate_set_speed',"
530 "'arguments': { 'value': %lld } }", value);
531 g_assert(qdict_haskey(rsp, "return"));
532 qobject_unref(rsp);
533 migrate_check_parameter(who, "max-bandwidth", value);
536 static void test_deprecated(void)
538 QTestState *from;
540 from = qtest_start("");
542 deprecated_set_downtime(from, 0.12345);
543 deprecated_set_speed(from, 12345);
545 qtest_quit(from);
548 static int migrate_postcopy_prepare(QTestState **from_ptr,
549 QTestState **to_ptr,
550 bool hide_error)
552 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
553 QTestState *from, *to;
555 if (test_migrate_start(&from, &to, uri, hide_error)) {
556 return -1;
559 migrate_set_capability(from, "postcopy-ram", true);
560 migrate_set_capability(to, "postcopy-ram", true);
561 migrate_set_capability(to, "postcopy-blocktime", true);
563 /* We want to pick a speed slow enough that the test completes
564 * quickly, but that it doesn't complete precopy even on a slow
565 * machine, so also set the downtime.
567 migrate_set_parameter(from, "max-bandwidth", 100000000);
568 migrate_set_parameter(from, "downtime-limit", 1);
570 /* Wait for the first serial output from the source */
571 wait_for_serial("src_serial");
573 migrate(from, uri, "{}");
574 g_free(uri);
576 wait_for_migration_pass(from);
578 *from_ptr = from;
579 *to_ptr = to;
581 return 0;
584 static void migrate_postcopy_complete(QTestState *from, QTestState *to)
586 wait_for_migration_complete(from);
588 /* Make sure we get at least one "B" on destination */
589 wait_for_serial("dest_serial");
591 if (uffd_feature_thread_id) {
592 read_blocktime(to);
595 test_migrate_end(from, to, true);
598 static void test_postcopy(void)
600 QTestState *from, *to;
602 if (migrate_postcopy_prepare(&from, &to, false)) {
603 return;
605 migrate_postcopy_start(from, to);
606 migrate_postcopy_complete(from, to);
609 static void test_postcopy_recovery(void)
611 QTestState *from, *to;
612 char *uri;
614 if (migrate_postcopy_prepare(&from, &to, true)) {
615 return;
618 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
619 migrate_set_parameter(from, "max-postcopy-bandwidth", 4096);
621 /* Now we start the postcopy */
622 migrate_postcopy_start(from, to);
625 * Wait until postcopy is really started; we can only run the
626 * migrate-pause command during a postcopy
628 wait_for_migration_status(from, "postcopy-active");
631 * Manually stop the postcopy migration. This emulates a network
632 * failure with the migration socket
634 migrate_pause(from);
637 * Wait for destination side to reach postcopy-paused state. The
638 * migrate-recover command can only succeed if destination machine
639 * is in the paused state
641 wait_for_migration_status(to, "postcopy-paused");
644 * Create a new socket to emulate a new channel that is different
645 * from the broken migration channel; tell the destination to
646 * listen to the new port
648 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
649 migrate_recover(to, uri);
652 * Try to rebuild the migration channel using the resume flag and
653 * the newly created channel
655 wait_for_migration_status(from, "postcopy-paused");
656 migrate(from, uri, "{'resume': true}");
657 g_free(uri);
659 /* Restore the postcopy bandwidth to unlimited */
660 migrate_set_parameter(from, "max-postcopy-bandwidth", 0);
662 migrate_postcopy_complete(from, to);
665 static void test_baddest(void)
667 QTestState *from, *to;
668 QDict *rsp_return;
669 char *status;
670 bool failed;
672 if (test_migrate_start(&from, &to, "tcp:0:0", true)) {
673 return;
675 migrate(from, "tcp:0:0", "{}");
676 do {
677 status = migrate_query_status(from);
678 g_assert(!strcmp(status, "setup") || !(strcmp(status, "failed")));
679 failed = !strcmp(status, "failed");
680 g_free(status);
681 } while (!failed);
683 /* Is the machine currently running? */
684 rsp_return = wait_command(from, "{ 'execute': 'query-status' }");
685 g_assert(qdict_haskey(rsp_return, "running"));
686 g_assert(qdict_get_bool(rsp_return, "running"));
687 qobject_unref(rsp_return);
689 test_migrate_end(from, to, false);
692 static void test_precopy_unix(void)
694 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
695 QTestState *from, *to;
697 if (test_migrate_start(&from, &to, uri, false)) {
698 return;
701 /* We want to pick a speed slow enough that the test completes
702 * quickly, but that it doesn't complete precopy even on a slow
703 * machine, so also set the downtime.
705 /* 1 ms should make it not converge*/
706 migrate_set_parameter(from, "downtime-limit", 1);
707 /* 1GB/s */
708 migrate_set_parameter(from, "max-bandwidth", 1000000000);
710 /* Wait for the first serial output from the source */
711 wait_for_serial("src_serial");
713 migrate(from, uri, "{}");
715 wait_for_migration_pass(from);
717 /* 300 ms should converge */
718 migrate_set_parameter(from, "downtime-limit", 300);
720 if (!got_stop) {
721 qtest_qmp_eventwait(from, "STOP");
724 qtest_qmp_eventwait(to, "RESUME");
726 wait_for_serial("dest_serial");
727 wait_for_migration_complete(from);
729 test_migrate_end(from, to, true);
730 g_free(uri);
733 int main(int argc, char **argv)
735 char template[] = "/tmp/migration-test-XXXXXX";
736 int ret;
738 g_test_init(&argc, &argv, NULL);
740 if (!ufd_version_check()) {
741 return 0;
745 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
746 * is touchy due to race conditions on dirty bits (especially on PPC for
747 * some reason)
749 if (g_str_equal(qtest_get_arch(), "ppc64") &&
750 access("/sys/module/kvm_hv", F_OK)) {
751 g_test_message("Skipping test: kvm_hv not available");
752 return 0;
755 tmpfs = mkdtemp(template);
756 if (!tmpfs) {
757 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno));
759 g_assert(tmpfs);
761 module_call_init(MODULE_INIT_QOM);
763 qtest_add_func("/migration/postcopy/unix", test_postcopy);
764 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery);
765 qtest_add_func("/migration/deprecated", test_deprecated);
766 qtest_add_func("/migration/bad_dest", test_baddest);
767 qtest_add_func("/migration/precopy/unix", test_precopy_unix);
769 ret = g_test_run();
771 g_assert_cmpint(ret, ==, 0);
773 ret = rmdir(tmpfs);
774 if (ret != 0) {
775 g_test_message("unable to rmdir: path (%s): %s\n",
776 tmpfs, strerror(errno));
779 return ret;