]> git.proxmox.com Git - mirror_qemu.git/blob - tests/migration-test.c
migration-test: Make wait_command() cope with '%'
[mirror_qemu.git] / tests / migration-test.c
1 /*
2 * QTest testcase for migration
3 *
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.
7 *
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.
10 *
11 */
12
13 #include "qemu/osdep.h"
14
15 #include "libqtest.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
23 /* TODO actually test the results and get rid of this */
24 #define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__))
25
26 const unsigned start_address = 1024 * 1024;
27 const unsigned end_address = 100 * 1024 * 1024;
28 bool got_stop;
29 static bool uffd_feature_thread_id;
30
31 #if defined(__linux__)
32 #include <sys/syscall.h>
33 #include <sys/vfs.h>
34 #endif
35
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>
40
41 static bool ufd_version_check(void)
42 {
43 struct uffdio_api api_struct;
44 uint64_t ioctl_mask;
45
46 int ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
47
48 if (ufd == -1) {
49 g_test_message("Skipping test: userfaultfd not available");
50 return false;
51 }
52
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");
57 return false;
58 }
59 uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
60
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");
65 return false;
66 }
67
68 return true;
69 }
70
71 #else
72 static bool ufd_version_check(void)
73 {
74 g_test_message("Skipping test: Userfault not available (builtdtime)");
75 return false;
76 }
77
78 #endif
79
80 static const char *tmpfs;
81
82 /* A simple PC boot sector that modifies memory (1-100MB) quickly
83 * outputting a 'B' every so often if it's still running.
84 */
85 #include "tests/migration/x86-a-b-bootblock.h"
86
87 static void init_bootfile_x86(const char *bootpath)
88 {
89 FILE *bootfile = fopen(bootpath, "wb");
90
91 g_assert_cmpint(fwrite(x86_bootsect, 512, 1, bootfile), ==, 1);
92 fclose(bootfile);
93 }
94
95 /*
96 * Wait for some output in the serial output file,
97 * we get an 'A' followed by an endless string of 'B's
98 * but on the destination we won't have the A.
99 */
100 static void wait_for_serial(const char *side)
101 {
102 char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
103 FILE *serialfile = fopen(serialpath, "r");
104 const char *arch = qtest_get_arch();
105 int started = (strcmp(side, "src_serial") == 0 &&
106 strcmp(arch, "ppc64") == 0) ? 0 : 1;
107
108 g_free(serialpath);
109 do {
110 int readvalue = fgetc(serialfile);
111
112 if (!started) {
113 /* SLOF prints its banner before starting test,
114 * to ignore it, mark the start of the test with '_',
115 * ignore all characters until this marker
116 */
117 switch (readvalue) {
118 case '_':
119 started = 1;
120 break;
121 case EOF:
122 fseek(serialfile, 0, SEEK_SET);
123 usleep(1000);
124 break;
125 }
126 continue;
127 }
128 switch (readvalue) {
129 case 'A':
130 /* Fine */
131 break;
132
133 case 'B':
134 /* It's alive! */
135 fclose(serialfile);
136 return;
137
138 case EOF:
139 started = (strcmp(side, "src_serial") == 0 &&
140 strcmp(arch, "ppc64") == 0) ? 0 : 1;
141 fseek(serialfile, 0, SEEK_SET);
142 usleep(1000);
143 break;
144
145 default:
146 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
147 g_assert_not_reached();
148 }
149 } while (true);
150 }
151
152 static void stop_cb(void *opaque, const char *name, QDict *data)
153 {
154 if (!strcmp(name, "STOP")) {
155 got_stop = true;
156 }
157 }
158
159 /*
160 * Events can get in the way of responses we are actually waiting for.
161 */
162 static QDict *wait_command(QTestState *who, const char *command, ...)
163 {
164 va_list ap;
165
166 va_start(ap, command);
167 qtest_qmp_vsend(who, command, ap);
168 va_end(ap);
169
170 return qtest_qmp_receive_success(who, stop_cb, NULL);
171 }
172
173 /*
174 * Note: caller is responsible to free the returned object via
175 * qobject_unref() after use
176 */
177 static QDict *migrate_query(QTestState *who)
178 {
179 return wait_command(who, "{ 'execute': 'query-migrate' }");
180 }
181
182 /*
183 * Note: caller is responsible to free the returned object via
184 * g_free() after use
185 */
186 static gchar *migrate_query_status(QTestState *who)
187 {
188 QDict *rsp_return = migrate_query(who);
189 gchar *status = g_strdup(qdict_get_str(rsp_return, "status"));
190
191 g_assert(status);
192 qobject_unref(rsp_return);
193
194 return status;
195 }
196
197 /*
198 * It's tricky to use qemu's migration event capability with qtest,
199 * events suddenly appearing confuse the qmp()/hmp() responses.
200 */
201
202 static uint64_t get_migration_pass(QTestState *who)
203 {
204 QDict *rsp_return, *rsp_ram;
205 uint64_t result;
206
207 rsp_return = migrate_query(who);
208 if (!qdict_haskey(rsp_return, "ram")) {
209 /* Still in setup */
210 result = 0;
211 } else {
212 rsp_ram = qdict_get_qdict(rsp_return, "ram");
213 result = qdict_get_try_int(rsp_ram, "dirty-sync-count", 0);
214 }
215 qobject_unref(rsp_return);
216 return result;
217 }
218
219 static void read_blocktime(QTestState *who)
220 {
221 QDict *rsp_return;
222
223 rsp_return = migrate_query(who);
224 g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
225 qobject_unref(rsp_return);
226 }
227
228 static void wait_for_migration_status(QTestState *who,
229 const char *goal)
230 {
231 while (true) {
232 bool completed;
233 char *status;
234
235 status = migrate_query_status(who);
236 completed = strcmp(status, goal) == 0;
237 g_assert_cmpstr(status, !=, "failed");
238 g_free(status);
239 if (completed) {
240 return;
241 }
242 usleep(1000);
243 }
244 }
245
246 static void wait_for_migration_complete(QTestState *who)
247 {
248 wait_for_migration_status(who, "completed");
249 }
250
251 static void wait_for_migration_pass(QTestState *who)
252 {
253 uint64_t initial_pass = get_migration_pass(who);
254 uint64_t pass;
255
256 /* Wait for the 1st sync */
257 while (!got_stop && !initial_pass) {
258 usleep(1000);
259 initial_pass = get_migration_pass(who);
260 }
261
262 do {
263 usleep(1000);
264 pass = get_migration_pass(who);
265 } while (pass == initial_pass && !got_stop);
266 }
267
268 static void check_guests_ram(QTestState *who)
269 {
270 /* Our ASM test will have been incrementing one byte from each page from
271 * 1MB to <100MB in order.
272 * This gives us a constraint that any page's byte should be equal or less
273 * than the previous pages byte (mod 256); and they should all be equal
274 * except for one transition at the point where we meet the incrementer.
275 * (We're running this with the guest stopped).
276 */
277 unsigned address;
278 uint8_t first_byte;
279 uint8_t last_byte;
280 bool hit_edge = false;
281 bool bad = false;
282
283 qtest_memread(who, start_address, &first_byte, 1);
284 last_byte = first_byte;
285
286 for (address = start_address + 4096; address < end_address; address += 4096)
287 {
288 uint8_t b;
289 qtest_memread(who, address, &b, 1);
290 if (b != last_byte) {
291 if (((b + 1) % 256) == last_byte && !hit_edge) {
292 /* This is OK, the guest stopped at the point of
293 * incrementing the previous page but didn't get
294 * to us yet.
295 */
296 hit_edge = true;
297 last_byte = b;
298 } else {
299 fprintf(stderr, "Memory content inconsistency at %x"
300 " first_byte = %x last_byte = %x current = %x"
301 " hit_edge = %x\n",
302 address, first_byte, last_byte, b, hit_edge);
303 bad = true;
304 }
305 }
306 }
307 g_assert_false(bad);
308 }
309
310 static void cleanup(const char *filename)
311 {
312 char *path = g_strdup_printf("%s/%s", tmpfs, filename);
313
314 unlink(path);
315 g_free(path);
316 }
317
318 static void migrate_check_parameter(QTestState *who, const char *parameter,
319 const char *value)
320 {
321 QDict *rsp_return;
322 char *result;
323
324 rsp_return = wait_command(who,
325 "{ 'execute': 'query-migrate-parameters' }");
326 result = g_strdup_printf("%" PRId64,
327 qdict_get_try_int(rsp_return, parameter, -1));
328 g_assert_cmpstr(result, ==, value);
329 g_free(result);
330 qobject_unref(rsp_return);
331 }
332
333 static void migrate_set_parameter(QTestState *who, const char *parameter,
334 const char *value)
335 {
336 QDict *rsp;
337 gchar *cmd;
338
339 cmd = g_strdup_printf("{ 'execute': 'migrate-set-parameters',"
340 "'arguments': { '%s': %s } }",
341 parameter, value);
342 rsp = qtest_qmp(who, cmd);
343 g_free(cmd);
344 g_assert(qdict_haskey(rsp, "return"));
345 qobject_unref(rsp);
346 migrate_check_parameter(who, parameter, value);
347 }
348
349 static void migrate_pause(QTestState *who)
350 {
351 QDict *rsp;
352
353 rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
354 qobject_unref(rsp);
355 }
356
357 static void migrate_recover(QTestState *who, const char *uri)
358 {
359 QDict *rsp;
360 gchar *cmd = g_strdup_printf(
361 "{ 'execute': 'migrate-recover', "
362 " 'id': 'recover-cmd', "
363 " 'arguments': { 'uri': '%s' } }", uri);
364
365 rsp = wait_command(who, cmd);
366 g_free(cmd);
367 qobject_unref(rsp);
368 }
369
370 static void migrate_set_capability(QTestState *who, const char *capability,
371 const char *value)
372 {
373 QDict *rsp;
374 gchar *cmd;
375
376 cmd = g_strdup_printf("{ 'execute': 'migrate-set-capabilities',"
377 "'arguments': { "
378 "'capabilities': [ { "
379 "'capability': '%s', 'state': %s } ] } }",
380 capability, value);
381 rsp = qtest_qmp(who, cmd);
382 g_free(cmd);
383 g_assert(qdict_haskey(rsp, "return"));
384 qobject_unref(rsp);
385 }
386
387 static void migrate(QTestState *who, const char *uri, const char *extra)
388 {
389 QDict *rsp;
390 gchar *cmd;
391
392 cmd = g_strdup_printf("{ 'execute': 'migrate',"
393 " 'arguments': { 'uri': '%s' %s } }",
394 uri, extra ? extra : "");
395 rsp = qtest_qmp(who, cmd);
396 g_free(cmd);
397 g_assert(qdict_haskey(rsp, "return"));
398 qobject_unref(rsp);
399 }
400
401 static void migrate_postcopy_start(QTestState *from, QTestState *to)
402 {
403 QDict *rsp;
404
405 rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
406 qobject_unref(rsp);
407
408 if (!got_stop) {
409 qtest_qmp_eventwait(from, "STOP");
410 }
411
412 qtest_qmp_eventwait(to, "RESUME");
413 }
414
415 static int test_migrate_start(QTestState **from, QTestState **to,
416 const char *uri, bool hide_stderr)
417 {
418 gchar *cmd_src, *cmd_dst;
419 char *bootpath = g_strdup_printf("%s/bootsect", tmpfs);
420 const char *arch = qtest_get_arch();
421 const char *accel = "kvm:tcg";
422
423 got_stop = false;
424
425 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
426 init_bootfile_x86(bootpath);
427 cmd_src = g_strdup_printf("-machine accel=%s -m 150M"
428 " -name source,debug-threads=on"
429 " -serial file:%s/src_serial"
430 " -drive file=%s,format=raw",
431 accel, tmpfs, bootpath);
432 cmd_dst = g_strdup_printf("-machine accel=%s -m 150M"
433 " -name target,debug-threads=on"
434 " -serial file:%s/dest_serial"
435 " -drive file=%s,format=raw"
436 " -incoming %s",
437 accel, tmpfs, bootpath, uri);
438 } else if (strcmp(arch, "ppc64") == 0) {
439
440 /* On ppc64, the test only works with kvm-hv, but not with kvm-pr
441 * and TCG is touchy due to race conditions on dirty bits
442 * (especially on PPC for some reason)
443 */
444 if (access("/sys/module/kvm_hv", F_OK)) {
445 g_print("Skipping test: kvm_hv not available ");
446 return -1;
447 }
448 cmd_src = g_strdup_printf("-machine accel=%s -m 256M"
449 " -name source,debug-threads=on"
450 " -serial file:%s/src_serial"
451 " -prom-env '"
452 "boot-command=hex .\" _\" begin %x %x "
453 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
454 "until'", accel, tmpfs, end_address,
455 start_address);
456 cmd_dst = g_strdup_printf("-machine accel=%s -m 256M"
457 " -name target,debug-threads=on"
458 " -serial file:%s/dest_serial"
459 " -incoming %s",
460 accel, tmpfs, uri);
461 } else {
462 g_assert_not_reached();
463 }
464
465 g_free(bootpath);
466
467 if (hide_stderr) {
468 gchar *tmp;
469 tmp = g_strdup_printf("%s 2>/dev/null", cmd_src);
470 g_free(cmd_src);
471 cmd_src = tmp;
472
473 tmp = g_strdup_printf("%s 2>/dev/null", cmd_dst);
474 g_free(cmd_dst);
475 cmd_dst = tmp;
476 }
477
478 *from = qtest_start(cmd_src);
479 g_free(cmd_src);
480
481 *to = qtest_init(cmd_dst);
482 g_free(cmd_dst);
483 return 0;
484 }
485
486 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
487 {
488 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
489
490 qtest_quit(from);
491
492 if (test_dest) {
493 qtest_memread(to, start_address, &dest_byte_a, 1);
494
495 /* Destination still running, wait for a byte to change */
496 do {
497 qtest_memread(to, start_address, &dest_byte_b, 1);
498 usleep(1000 * 10);
499 } while (dest_byte_a == dest_byte_b);
500
501 qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
502
503 /* With it stopped, check nothing changes */
504 qtest_memread(to, start_address, &dest_byte_c, 1);
505 usleep(1000 * 200);
506 qtest_memread(to, start_address, &dest_byte_d, 1);
507 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
508
509 check_guests_ram(to);
510 }
511
512 qtest_quit(to);
513
514 cleanup("bootsect");
515 cleanup("migsocket");
516 cleanup("src_serial");
517 cleanup("dest_serial");
518 }
519
520 static void deprecated_set_downtime(QTestState *who, const double value)
521 {
522 QDict *rsp;
523 char *expected;
524 int64_t result_int;
525
526 rsp = qtest_qmp(who,
527 "{ 'execute': 'migrate_set_downtime',"
528 " 'arguments': { 'value': %f } }", value);
529 g_assert(qdict_haskey(rsp, "return"));
530 qobject_unref(rsp);
531 result_int = value * 1000L;
532 expected = g_strdup_printf("%" PRId64, result_int);
533 migrate_check_parameter(who, "downtime-limit", expected);
534 g_free(expected);
535 }
536
537 static void deprecated_set_speed(QTestState *who, const char *value)
538 {
539 QDict *rsp;
540 gchar *cmd;
541
542 cmd = g_strdup_printf("{ 'execute': 'migrate_set_speed',"
543 "'arguments': { 'value': %s } }", value);
544 rsp = qtest_qmp(who, cmd);
545 g_free(cmd);
546 g_assert(qdict_haskey(rsp, "return"));
547 qobject_unref(rsp);
548 migrate_check_parameter(who, "max-bandwidth", value);
549 }
550
551 static void test_deprecated(void)
552 {
553 QTestState *from;
554
555 from = qtest_start("");
556
557 deprecated_set_downtime(from, 0.12345);
558 deprecated_set_speed(from, "12345");
559
560 qtest_quit(from);
561 }
562
563 static int migrate_postcopy_prepare(QTestState **from_ptr,
564 QTestState **to_ptr,
565 bool hide_error)
566 {
567 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
568 QTestState *from, *to;
569
570 if (test_migrate_start(&from, &to, uri, hide_error)) {
571 return -1;
572 }
573
574 migrate_set_capability(from, "postcopy-ram", "true");
575 migrate_set_capability(to, "postcopy-ram", "true");
576 migrate_set_capability(to, "postcopy-blocktime", "true");
577
578 /* We want to pick a speed slow enough that the test completes
579 * quickly, but that it doesn't complete precopy even on a slow
580 * machine, so also set the downtime.
581 */
582 migrate_set_parameter(from, "max-bandwidth", "100000000");
583 migrate_set_parameter(from, "downtime-limit", "1");
584
585 /* Wait for the first serial output from the source */
586 wait_for_serial("src_serial");
587
588 migrate(from, uri, NULL);
589 g_free(uri);
590
591 wait_for_migration_pass(from);
592
593 *from_ptr = from;
594 *to_ptr = to;
595
596 return 0;
597 }
598
599 static void migrate_postcopy_complete(QTestState *from, QTestState *to)
600 {
601 wait_for_migration_complete(from);
602
603 /* Make sure we get at least one "B" on destination */
604 wait_for_serial("dest_serial");
605
606 if (uffd_feature_thread_id) {
607 read_blocktime(to);
608 }
609
610 test_migrate_end(from, to, true);
611 }
612
613 static void test_postcopy(void)
614 {
615 QTestState *from, *to;
616
617 if (migrate_postcopy_prepare(&from, &to, false)) {
618 return;
619 }
620 migrate_postcopy_start(from, to);
621 migrate_postcopy_complete(from, to);
622 }
623
624 static void test_postcopy_recovery(void)
625 {
626 QTestState *from, *to;
627 char *uri;
628
629 if (migrate_postcopy_prepare(&from, &to, true)) {
630 return;
631 }
632
633 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
634 migrate_set_parameter(from, "max-postcopy-bandwidth", "4096");
635
636 /* Now we start the postcopy */
637 migrate_postcopy_start(from, to);
638
639 /*
640 * Wait until postcopy is really started; we can only run the
641 * migrate-pause command during a postcopy
642 */
643 wait_for_migration_status(from, "postcopy-active");
644
645 /*
646 * Manually stop the postcopy migration. This emulates a network
647 * failure with the migration socket
648 */
649 migrate_pause(from);
650
651 /*
652 * Wait for destination side to reach postcopy-paused state. The
653 * migrate-recover command can only succeed if destination machine
654 * is in the paused state
655 */
656 wait_for_migration_status(to, "postcopy-paused");
657
658 /*
659 * Create a new socket to emulate a new channel that is different
660 * from the broken migration channel; tell the destination to
661 * listen to the new port
662 */
663 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
664 migrate_recover(to, uri);
665
666 /*
667 * Try to rebuild the migration channel using the resume flag and
668 * the newly created channel
669 */
670 wait_for_migration_status(from, "postcopy-paused");
671 migrate(from, uri, ", 'resume': true");
672 g_free(uri);
673
674 /* Restore the postcopy bandwidth to unlimited */
675 migrate_set_parameter(from, "max-postcopy-bandwidth", "0");
676
677 migrate_postcopy_complete(from, to);
678 }
679
680 static void test_baddest(void)
681 {
682 QTestState *from, *to;
683 QDict *rsp_return;
684 char *status;
685 bool failed;
686
687 if (test_migrate_start(&from, &to, "tcp:0:0", true)) {
688 return;
689 }
690 migrate(from, "tcp:0:0", NULL);
691 do {
692 status = migrate_query_status(from);
693 g_assert(!strcmp(status, "setup") || !(strcmp(status, "failed")));
694 failed = !strcmp(status, "failed");
695 g_free(status);
696 } while (!failed);
697
698 /* Is the machine currently running? */
699 rsp_return = wait_command(from, "{ 'execute': 'query-status' }");
700 g_assert(qdict_haskey(rsp_return, "running"));
701 g_assert(qdict_get_bool(rsp_return, "running"));
702 qobject_unref(rsp_return);
703
704 test_migrate_end(from, to, false);
705 }
706
707 static void test_precopy_unix(void)
708 {
709 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
710 QTestState *from, *to;
711
712 if (test_migrate_start(&from, &to, uri, false)) {
713 return;
714 }
715
716 /* We want to pick a speed slow enough that the test completes
717 * quickly, but that it doesn't complete precopy even on a slow
718 * machine, so also set the downtime.
719 */
720 /* 1 ms should make it not converge*/
721 migrate_set_parameter(from, "downtime-limit", "1");
722 /* 1GB/s */
723 migrate_set_parameter(from, "max-bandwidth", "1000000000");
724
725 /* Wait for the first serial output from the source */
726 wait_for_serial("src_serial");
727
728 migrate(from, uri, NULL);
729
730 wait_for_migration_pass(from);
731
732 /* 300 ms should converge */
733 migrate_set_parameter(from, "downtime-limit", "300");
734
735 if (!got_stop) {
736 qtest_qmp_eventwait(from, "STOP");
737 }
738
739 qtest_qmp_eventwait(to, "RESUME");
740
741 wait_for_serial("dest_serial");
742 wait_for_migration_complete(from);
743
744 test_migrate_end(from, to, true);
745 g_free(uri);
746 }
747
748 int main(int argc, char **argv)
749 {
750 char template[] = "/tmp/migration-test-XXXXXX";
751 int ret;
752
753 g_test_init(&argc, &argv, NULL);
754
755 if (!ufd_version_check()) {
756 return 0;
757 }
758
759 tmpfs = mkdtemp(template);
760 if (!tmpfs) {
761 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno));
762 }
763 g_assert(tmpfs);
764
765 module_call_init(MODULE_INIT_QOM);
766
767 qtest_add_func("/migration/postcopy/unix", test_postcopy);
768 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery);
769 qtest_add_func("/migration/deprecated", test_deprecated);
770 qtest_add_func("/migration/bad_dest", test_baddest);
771 qtest_add_func("/migration/precopy/unix", test_precopy_unix);
772
773 ret = g_test_run();
774
775 g_assert_cmpint(ret, ==, 0);
776
777 ret = rmdir(tmpfs);
778 if (ret != 0) {
779 g_test_message("unable to rmdir: path (%s): %s\n",
780 tmpfs, strerror(errno));
781 }
782
783 return ret;
784 }