]> git.proxmox.com Git - mirror_qemu.git/blob - tests/migration-test.c
Merge remote-tracking branch 'remotes/bkoppelmann/tags/pull-tricore-2019-03-08' into...
[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 "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"
23 #include "qapi/qapi-visit-sockets.h"
24 #include "qapi/qobject-input-visitor.h"
25 #include "qapi/qobject-output-visitor.h"
26
27 #include "migration/migration-test.h"
28
29 /* TODO actually test the results and get rid of this */
30 #define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__))
31
32 unsigned start_address;
33 unsigned end_address;
34 bool got_stop;
35 static bool uffd_feature_thread_id;
36
37 #if defined(__linux__)
38 #include <sys/syscall.h>
39 #include <sys/vfs.h>
40 #endif
41
42 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
43 #include <sys/eventfd.h>
44 #include <sys/ioctl.h>
45 #include <linux/userfaultfd.h>
46
47 static bool ufd_version_check(void)
48 {
49 struct uffdio_api api_struct;
50 uint64_t ioctl_mask;
51
52 int ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
53
54 if (ufd == -1) {
55 g_test_message("Skipping test: userfaultfd not available");
56 return false;
57 }
58
59 api_struct.api = UFFD_API;
60 api_struct.features = 0;
61 if (ioctl(ufd, UFFDIO_API, &api_struct)) {
62 g_test_message("Skipping test: UFFDIO_API failed");
63 return false;
64 }
65 uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
66
67 ioctl_mask = (__u64)1 << _UFFDIO_REGISTER |
68 (__u64)1 << _UFFDIO_UNREGISTER;
69 if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
70 g_test_message("Skipping test: Missing userfault feature");
71 return false;
72 }
73
74 return true;
75 }
76
77 #else
78 static bool ufd_version_check(void)
79 {
80 g_test_message("Skipping test: Userfault not available (builtdtime)");
81 return false;
82 }
83
84 #endif
85
86 static const char *tmpfs;
87
88 /* The boot file modifies memory area in [start_address, end_address)
89 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
90 */
91 #include "tests/migration/i386/a-b-bootblock.h"
92 #include "tests/migration/aarch64/a-b-kernel.h"
93
94 static void init_bootfile(const char *bootpath, void *content)
95 {
96 FILE *bootfile = fopen(bootpath, "wb");
97
98 g_assert_cmpint(fwrite(content, 512, 1, bootfile), ==, 1);
99 fclose(bootfile);
100 }
101
102 #include "tests/migration/s390x/a-b-bios.h"
103
104 static void init_bootfile_s390x(const char *bootpath)
105 {
106 FILE *bootfile = fopen(bootpath, "wb");
107 size_t len = sizeof(s390x_elf);
108
109 g_assert_cmpint(fwrite(s390x_elf, len, 1, bootfile), ==, 1);
110 fclose(bootfile);
111 }
112
113 /*
114 * Wait for some output in the serial output file,
115 * we get an 'A' followed by an endless string of 'B's
116 * but on the destination we won't have the A.
117 */
118 static void wait_for_serial(const char *side)
119 {
120 char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
121 FILE *serialfile = fopen(serialpath, "r");
122 const char *arch = qtest_get_arch();
123 int started = (strcmp(side, "src_serial") == 0 &&
124 strcmp(arch, "ppc64") == 0) ? 0 : 1;
125
126 g_free(serialpath);
127 do {
128 int readvalue = fgetc(serialfile);
129
130 if (!started) {
131 /* SLOF prints its banner before starting test,
132 * to ignore it, mark the start of the test with '_',
133 * ignore all characters until this marker
134 */
135 switch (readvalue) {
136 case '_':
137 started = 1;
138 break;
139 case EOF:
140 fseek(serialfile, 0, SEEK_SET);
141 usleep(1000);
142 break;
143 }
144 continue;
145 }
146 switch (readvalue) {
147 case 'A':
148 /* Fine */
149 break;
150
151 case 'B':
152 /* It's alive! */
153 fclose(serialfile);
154 return;
155
156 case EOF:
157 started = (strcmp(side, "src_serial") == 0 &&
158 strcmp(arch, "ppc64") == 0) ? 0 : 1;
159 fseek(serialfile, 0, SEEK_SET);
160 usleep(1000);
161 break;
162
163 default:
164 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
165 g_assert_not_reached();
166 }
167 } while (true);
168 }
169
170 static void stop_cb(void *opaque, const char *name, QDict *data)
171 {
172 if (!strcmp(name, "STOP")) {
173 got_stop = true;
174 }
175 }
176
177 /*
178 * Events can get in the way of responses we are actually waiting for.
179 */
180 GCC_FMT_ATTR(2, 3)
181 static QDict *wait_command(QTestState *who, const char *command, ...)
182 {
183 va_list ap;
184
185 va_start(ap, command);
186 qtest_qmp_vsend(who, command, ap);
187 va_end(ap);
188
189 return qtest_qmp_receive_success(who, stop_cb, NULL);
190 }
191
192 /*
193 * Note: caller is responsible to free the returned object via
194 * qobject_unref() after use
195 */
196 static QDict *migrate_query(QTestState *who)
197 {
198 return wait_command(who, "{ 'execute': 'query-migrate' }");
199 }
200
201 /*
202 * Note: caller is responsible to free the returned object via
203 * g_free() after use
204 */
205 static gchar *migrate_query_status(QTestState *who)
206 {
207 QDict *rsp_return = migrate_query(who);
208 gchar *status = g_strdup(qdict_get_str(rsp_return, "status"));
209
210 g_assert(status);
211 qobject_unref(rsp_return);
212
213 return status;
214 }
215
216 /*
217 * It's tricky to use qemu's migration event capability with qtest,
218 * events suddenly appearing confuse the qmp()/hmp() responses.
219 */
220
221 static int64_t read_ram_property_int(QTestState *who, const char *property)
222 {
223 QDict *rsp_return, *rsp_ram;
224 int64_t result;
225
226 rsp_return = migrate_query(who);
227 if (!qdict_haskey(rsp_return, "ram")) {
228 /* Still in setup */
229 result = 0;
230 } else {
231 rsp_ram = qdict_get_qdict(rsp_return, "ram");
232 result = qdict_get_try_int(rsp_ram, property, 0);
233 }
234 qobject_unref(rsp_return);
235 return result;
236 }
237
238 static uint64_t get_migration_pass(QTestState *who)
239 {
240 return read_ram_property_int(who, "dirty-sync-count");
241 }
242
243 static void read_blocktime(QTestState *who)
244 {
245 QDict *rsp_return;
246
247 rsp_return = migrate_query(who);
248 g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
249 qobject_unref(rsp_return);
250 }
251
252 static void wait_for_migration_status(QTestState *who,
253 const char *goal)
254 {
255 while (true) {
256 bool completed;
257 char *status;
258
259 status = migrate_query_status(who);
260 completed = strcmp(status, goal) == 0;
261 g_assert_cmpstr(status, !=, "failed");
262 g_free(status);
263 if (completed) {
264 return;
265 }
266 usleep(1000);
267 }
268 }
269
270 static void wait_for_migration_complete(QTestState *who)
271 {
272 wait_for_migration_status(who, "completed");
273 }
274
275 static void wait_for_migration_pass(QTestState *who)
276 {
277 uint64_t initial_pass = get_migration_pass(who);
278 uint64_t pass;
279
280 /* Wait for the 1st sync */
281 while (!got_stop && !initial_pass) {
282 usleep(1000);
283 initial_pass = get_migration_pass(who);
284 }
285
286 do {
287 usleep(1000);
288 pass = get_migration_pass(who);
289 } while (pass == initial_pass && !got_stop);
290 }
291
292 static void check_guests_ram(QTestState *who)
293 {
294 /* Our ASM test will have been incrementing one byte from each page from
295 * start_address to < end_address in order. This gives us a constraint
296 * that any page's byte should be equal or less than the previous pages
297 * byte (mod 256); and they should all be equal except for one transition
298 * at the point where we meet the incrementer. (We're running this with
299 * the guest stopped).
300 */
301 unsigned address;
302 uint8_t first_byte;
303 uint8_t last_byte;
304 bool hit_edge = false;
305 bool bad = false;
306
307 qtest_memread(who, start_address, &first_byte, 1);
308 last_byte = first_byte;
309
310 for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
311 address += TEST_MEM_PAGE_SIZE)
312 {
313 uint8_t b;
314 qtest_memread(who, address, &b, 1);
315 if (b != last_byte) {
316 if (((b + 1) % 256) == last_byte && !hit_edge) {
317 /* This is OK, the guest stopped at the point of
318 * incrementing the previous page but didn't get
319 * to us yet.
320 */
321 hit_edge = true;
322 last_byte = b;
323 } else {
324 fprintf(stderr, "Memory content inconsistency at %x"
325 " first_byte = %x last_byte = %x current = %x"
326 " hit_edge = %x\n",
327 address, first_byte, last_byte, b, hit_edge);
328 bad = true;
329 }
330 }
331 }
332 g_assert_false(bad);
333 }
334
335 static void cleanup(const char *filename)
336 {
337 char *path = g_strdup_printf("%s/%s", tmpfs, filename);
338
339 unlink(path);
340 g_free(path);
341 }
342
343 static char *get_shmem_opts(const char *mem_size, const char *shmem_path)
344 {
345 return g_strdup_printf("-object memory-backend-file,id=mem0,size=%s"
346 ",mem-path=%s,share=on -numa node,memdev=mem0",
347 mem_size, shmem_path);
348 }
349
350 static char *SocketAddress_to_str(SocketAddress *addr)
351 {
352 switch (addr->type) {
353 case SOCKET_ADDRESS_TYPE_INET:
354 return g_strdup_printf("tcp:%s:%s",
355 addr->u.inet.host,
356 addr->u.inet.port);
357 case SOCKET_ADDRESS_TYPE_UNIX:
358 return g_strdup_printf("unix:%s",
359 addr->u.q_unix.path);
360 case SOCKET_ADDRESS_TYPE_FD:
361 return g_strdup_printf("fd:%s", addr->u.fd.str);
362 case SOCKET_ADDRESS_TYPE_VSOCK:
363 return g_strdup_printf("tcp:%s:%s",
364 addr->u.vsock.cid,
365 addr->u.vsock.port);
366 default:
367 return g_strdup("unknown address type");
368 }
369 }
370
371 static char *migrate_get_socket_address(QTestState *who, const char *parameter)
372 {
373 QDict *rsp;
374 char *result;
375 Error *local_err = NULL;
376 SocketAddressList *addrs;
377 Visitor *iv = NULL;
378 QObject *object;
379
380 rsp = migrate_query(who);
381 object = qdict_get(rsp, parameter);
382
383 iv = qobject_input_visitor_new(object);
384 visit_type_SocketAddressList(iv, NULL, &addrs, &local_err);
385
386 /* we are only using a single address */
387 result = g_strdup_printf("%s", SocketAddress_to_str(addrs->value));
388
389 qapi_free_SocketAddressList(addrs);
390 qobject_unref(rsp);
391 return result;
392 }
393
394 static long long migrate_get_parameter(QTestState *who, const char *parameter)
395 {
396 QDict *rsp;
397 long long result;
398
399 rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
400 result = qdict_get_int(rsp, parameter);
401 qobject_unref(rsp);
402 return result;
403 }
404
405 static void migrate_check_parameter(QTestState *who, const char *parameter,
406 long long value)
407 {
408 long long result;
409
410 result = migrate_get_parameter(who, parameter);
411 g_assert_cmpint(result, ==, value);
412 }
413
414 static void migrate_set_parameter(QTestState *who, const char *parameter,
415 long long value)
416 {
417 QDict *rsp;
418
419 rsp = qtest_qmp(who,
420 "{ 'execute': 'migrate-set-parameters',"
421 "'arguments': { %s: %lld } }",
422 parameter, value);
423 g_assert(qdict_haskey(rsp, "return"));
424 qobject_unref(rsp);
425 migrate_check_parameter(who, parameter, value);
426 }
427
428 static void migrate_pause(QTestState *who)
429 {
430 QDict *rsp;
431
432 rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
433 qobject_unref(rsp);
434 }
435
436 static void migrate_recover(QTestState *who, const char *uri)
437 {
438 QDict *rsp;
439
440 rsp = wait_command(who,
441 "{ 'execute': 'migrate-recover', "
442 " 'id': 'recover-cmd', "
443 " 'arguments': { 'uri': %s } }",
444 uri);
445 qobject_unref(rsp);
446 }
447
448 static void migrate_set_capability(QTestState *who, const char *capability,
449 bool value)
450 {
451 QDict *rsp;
452
453 rsp = qtest_qmp(who,
454 "{ 'execute': 'migrate-set-capabilities',"
455 "'arguments': { "
456 "'capabilities': [ { "
457 "'capability': %s, 'state': %i } ] } }",
458 capability, value);
459 g_assert(qdict_haskey(rsp, "return"));
460 qobject_unref(rsp);
461 }
462
463 /*
464 * Send QMP command "migrate".
465 * Arguments are built from @fmt... (formatted like
466 * qobject_from_jsonf_nofail()) with "uri": @uri spliced in.
467 */
468 GCC_FMT_ATTR(3, 4)
469 static void migrate(QTestState *who, const char *uri, const char *fmt, ...)
470 {
471 va_list ap;
472 QDict *args, *rsp;
473
474 va_start(ap, fmt);
475 args = qdict_from_vjsonf_nofail(fmt, ap);
476 va_end(ap);
477
478 g_assert(!qdict_haskey(args, "uri"));
479 qdict_put_str(args, "uri", uri);
480
481 rsp = qmp("{ 'execute': 'migrate', 'arguments': %p}", args);
482 g_assert(qdict_haskey(rsp, "return"));
483 qobject_unref(rsp);
484 }
485
486 static void migrate_postcopy_start(QTestState *from, QTestState *to)
487 {
488 QDict *rsp;
489
490 rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
491 qobject_unref(rsp);
492
493 if (!got_stop) {
494 qtest_qmp_eventwait(from, "STOP");
495 }
496
497 qtest_qmp_eventwait(to, "RESUME");
498 }
499
500 static int test_migrate_start(QTestState **from, QTestState **to,
501 const char *uri, bool hide_stderr,
502 bool use_shmem)
503 {
504 gchar *cmd_src, *cmd_dst;
505 char *bootpath = NULL;
506 char *extra_opts = NULL;
507 char *shmem_path = NULL;
508 const char *arch = qtest_get_arch();
509 const char *accel = "kvm:tcg";
510
511 if (use_shmem) {
512 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
513 g_test_skip("/dev/shm is not supported");
514 return -1;
515 }
516 shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
517 }
518
519 got_stop = false;
520 bootpath = g_strdup_printf("%s/bootsect", tmpfs);
521 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
522 init_bootfile(bootpath, x86_bootsect);
523 extra_opts = use_shmem ? get_shmem_opts("150M", shmem_path) : NULL;
524 cmd_src = g_strdup_printf("-machine accel=%s -m 150M"
525 " -name source,debug-threads=on"
526 " -serial file:%s/src_serial"
527 " -drive file=%s,format=raw %s",
528 accel, tmpfs, bootpath,
529 extra_opts ? extra_opts : "");
530 cmd_dst = g_strdup_printf("-machine accel=%s -m 150M"
531 " -name target,debug-threads=on"
532 " -serial file:%s/dest_serial"
533 " -drive file=%s,format=raw"
534 " -incoming %s %s",
535 accel, tmpfs, bootpath, uri,
536 extra_opts ? extra_opts : "");
537 start_address = X86_TEST_MEM_START;
538 end_address = X86_TEST_MEM_END;
539 } else if (g_str_equal(arch, "s390x")) {
540 init_bootfile_s390x(bootpath);
541 extra_opts = use_shmem ? get_shmem_opts("128M", shmem_path) : NULL;
542 cmd_src = g_strdup_printf("-machine accel=%s -m 128M"
543 " -name source,debug-threads=on"
544 " -serial file:%s/src_serial -bios %s %s",
545 accel, tmpfs, bootpath,
546 extra_opts ? extra_opts : "");
547 cmd_dst = g_strdup_printf("-machine accel=%s -m 128M"
548 " -name target,debug-threads=on"
549 " -serial file:%s/dest_serial -bios %s"
550 " -incoming %s %s",
551 accel, tmpfs, bootpath, uri,
552 extra_opts ? extra_opts : "");
553 start_address = S390_TEST_MEM_START;
554 end_address = S390_TEST_MEM_END;
555 } else if (strcmp(arch, "ppc64") == 0) {
556 extra_opts = use_shmem ? get_shmem_opts("256M", shmem_path) : NULL;
557 cmd_src = g_strdup_printf("-machine accel=%s -m 256M -nodefaults"
558 " -name source,debug-threads=on"
559 " -serial file:%s/src_serial"
560 " -prom-env 'use-nvramrc?=true' -prom-env "
561 "'nvramrc=hex .\" _\" begin %x %x "
562 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
563 "until' %s", accel, tmpfs, end_address,
564 start_address, extra_opts ? extra_opts : "");
565 cmd_dst = g_strdup_printf("-machine accel=%s -m 256M"
566 " -name target,debug-threads=on"
567 " -serial file:%s/dest_serial"
568 " -incoming %s %s",
569 accel, tmpfs, uri,
570 extra_opts ? extra_opts : "");
571
572 start_address = PPC_TEST_MEM_START;
573 end_address = PPC_TEST_MEM_END;
574 } else if (strcmp(arch, "aarch64") == 0) {
575 init_bootfile(bootpath, aarch64_kernel);
576 extra_opts = use_shmem ? get_shmem_opts("150M", shmem_path) : NULL;
577 cmd_src = g_strdup_printf("-machine virt,accel=%s,gic-version=max "
578 "-name vmsource,debug-threads=on -cpu max "
579 "-m 150M -serial file:%s/src_serial "
580 "-kernel %s %s",
581 accel, tmpfs, bootpath,
582 extra_opts ? extra_opts : "");
583 cmd_dst = g_strdup_printf("-machine virt,accel=%s,gic-version=max "
584 "-name vmdest,debug-threads=on -cpu max "
585 "-m 150M -serial file:%s/dest_serial "
586 "-kernel %s "
587 "-incoming %s %s",
588 accel, tmpfs, bootpath, uri,
589 extra_opts ? extra_opts : "");
590
591 start_address = ARM_TEST_MEM_START;
592 end_address = ARM_TEST_MEM_END;
593
594 g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
595 } else {
596 g_assert_not_reached();
597 }
598
599 g_free(bootpath);
600 g_free(extra_opts);
601
602 if (hide_stderr) {
603 gchar *tmp;
604 tmp = g_strdup_printf("%s 2>/dev/null", cmd_src);
605 g_free(cmd_src);
606 cmd_src = tmp;
607
608 tmp = g_strdup_printf("%s 2>/dev/null", cmd_dst);
609 g_free(cmd_dst);
610 cmd_dst = tmp;
611 }
612
613 *from = qtest_start(cmd_src);
614 g_free(cmd_src);
615
616 *to = qtest_init(cmd_dst);
617 g_free(cmd_dst);
618
619 /*
620 * Remove shmem file immediately to avoid memory leak in test failed case.
621 * It's valid becase QEMU has already opened this file
622 */
623 if (use_shmem) {
624 unlink(shmem_path);
625 g_free(shmem_path);
626 }
627
628 return 0;
629 }
630
631 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
632 {
633 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
634
635 qtest_quit(from);
636
637 if (test_dest) {
638 qtest_memread(to, start_address, &dest_byte_a, 1);
639
640 /* Destination still running, wait for a byte to change */
641 do {
642 qtest_memread(to, start_address, &dest_byte_b, 1);
643 usleep(1000 * 10);
644 } while (dest_byte_a == dest_byte_b);
645
646 qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
647
648 /* With it stopped, check nothing changes */
649 qtest_memread(to, start_address, &dest_byte_c, 1);
650 usleep(1000 * 200);
651 qtest_memread(to, start_address, &dest_byte_d, 1);
652 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
653
654 check_guests_ram(to);
655 }
656
657 qtest_quit(to);
658
659 cleanup("bootsect");
660 cleanup("migsocket");
661 cleanup("src_serial");
662 cleanup("dest_serial");
663 }
664
665 static void deprecated_set_downtime(QTestState *who, const double value)
666 {
667 QDict *rsp;
668
669 rsp = qtest_qmp(who,
670 "{ 'execute': 'migrate_set_downtime',"
671 " 'arguments': { 'value': %f } }", value);
672 g_assert(qdict_haskey(rsp, "return"));
673 qobject_unref(rsp);
674 migrate_check_parameter(who, "downtime-limit", value * 1000);
675 }
676
677 static void deprecated_set_speed(QTestState *who, long long value)
678 {
679 QDict *rsp;
680
681 rsp = qtest_qmp(who, "{ 'execute': 'migrate_set_speed',"
682 "'arguments': { 'value': %lld } }", value);
683 g_assert(qdict_haskey(rsp, "return"));
684 qobject_unref(rsp);
685 migrate_check_parameter(who, "max-bandwidth", value);
686 }
687
688 static void deprecated_set_cache_size(QTestState *who, long long value)
689 {
690 QDict *rsp;
691
692 rsp = qtest_qmp(who, "{ 'execute': 'migrate-set-cache-size',"
693 "'arguments': { 'value': %lld } }", value);
694 g_assert(qdict_haskey(rsp, "return"));
695 qobject_unref(rsp);
696 migrate_check_parameter(who, "xbzrle-cache-size", value);
697 }
698
699 static void test_deprecated(void)
700 {
701 QTestState *from;
702
703 from = qtest_start("-machine none");
704
705 deprecated_set_downtime(from, 0.12345);
706 deprecated_set_speed(from, 12345);
707 deprecated_set_cache_size(from, 4096);
708
709 qtest_quit(from);
710 }
711
712 static int migrate_postcopy_prepare(QTestState **from_ptr,
713 QTestState **to_ptr,
714 bool hide_error)
715 {
716 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
717 QTestState *from, *to;
718
719 if (test_migrate_start(&from, &to, uri, hide_error, false)) {
720 return -1;
721 }
722
723 migrate_set_capability(from, "postcopy-ram", true);
724 migrate_set_capability(to, "postcopy-ram", true);
725 migrate_set_capability(to, "postcopy-blocktime", true);
726
727 /* We want to pick a speed slow enough that the test completes
728 * quickly, but that it doesn't complete precopy even on a slow
729 * machine, so also set the downtime.
730 */
731 migrate_set_parameter(from, "max-bandwidth", 100000000);
732 migrate_set_parameter(from, "downtime-limit", 1);
733
734 /* Wait for the first serial output from the source */
735 wait_for_serial("src_serial");
736
737 migrate(from, uri, "{}");
738 g_free(uri);
739
740 wait_for_migration_pass(from);
741
742 *from_ptr = from;
743 *to_ptr = to;
744
745 return 0;
746 }
747
748 static void migrate_postcopy_complete(QTestState *from, QTestState *to)
749 {
750 wait_for_migration_complete(from);
751
752 /* Make sure we get at least one "B" on destination */
753 wait_for_serial("dest_serial");
754
755 if (uffd_feature_thread_id) {
756 read_blocktime(to);
757 }
758
759 test_migrate_end(from, to, true);
760 }
761
762 static void test_postcopy(void)
763 {
764 QTestState *from, *to;
765
766 if (migrate_postcopy_prepare(&from, &to, false)) {
767 return;
768 }
769 migrate_postcopy_start(from, to);
770 migrate_postcopy_complete(from, to);
771 }
772
773 static void test_postcopy_recovery(void)
774 {
775 QTestState *from, *to;
776 char *uri;
777
778 if (migrate_postcopy_prepare(&from, &to, true)) {
779 return;
780 }
781
782 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
783 migrate_set_parameter(from, "max-postcopy-bandwidth", 4096);
784
785 /* Now we start the postcopy */
786 migrate_postcopy_start(from, to);
787
788 /*
789 * Wait until postcopy is really started; we can only run the
790 * migrate-pause command during a postcopy
791 */
792 wait_for_migration_status(from, "postcopy-active");
793
794 /*
795 * Manually stop the postcopy migration. This emulates a network
796 * failure with the migration socket
797 */
798 migrate_pause(from);
799
800 /*
801 * Wait for destination side to reach postcopy-paused state. The
802 * migrate-recover command can only succeed if destination machine
803 * is in the paused state
804 */
805 wait_for_migration_status(to, "postcopy-paused");
806
807 /*
808 * Create a new socket to emulate a new channel that is different
809 * from the broken migration channel; tell the destination to
810 * listen to the new port
811 */
812 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
813 migrate_recover(to, uri);
814
815 /*
816 * Try to rebuild the migration channel using the resume flag and
817 * the newly created channel
818 */
819 wait_for_migration_status(from, "postcopy-paused");
820 migrate(from, uri, "{'resume': true}");
821 g_free(uri);
822
823 /* Restore the postcopy bandwidth to unlimited */
824 migrate_set_parameter(from, "max-postcopy-bandwidth", 0);
825
826 migrate_postcopy_complete(from, to);
827 }
828
829 static void test_baddest(void)
830 {
831 QTestState *from, *to;
832 QDict *rsp_return;
833 char *status;
834 bool failed;
835
836 if (test_migrate_start(&from, &to, "tcp:0:0", true, false)) {
837 return;
838 }
839 migrate(from, "tcp:0:0", "{}");
840 do {
841 status = migrate_query_status(from);
842 g_assert(!strcmp(status, "setup") || !(strcmp(status, "failed")));
843 failed = !strcmp(status, "failed");
844 g_free(status);
845 } while (!failed);
846
847 /* Is the machine currently running? */
848 rsp_return = wait_command(from, "{ 'execute': 'query-status' }");
849 g_assert(qdict_haskey(rsp_return, "running"));
850 g_assert(qdict_get_bool(rsp_return, "running"));
851 qobject_unref(rsp_return);
852
853 test_migrate_end(from, to, false);
854 }
855
856 static void test_precopy_unix(void)
857 {
858 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
859 QTestState *from, *to;
860
861 if (test_migrate_start(&from, &to, uri, false, false)) {
862 return;
863 }
864
865 /* We want to pick a speed slow enough that the test completes
866 * quickly, but that it doesn't complete precopy even on a slow
867 * machine, so also set the downtime.
868 */
869 /* 1 ms should make it not converge*/
870 migrate_set_parameter(from, "downtime-limit", 1);
871 /* 1GB/s */
872 migrate_set_parameter(from, "max-bandwidth", 1000000000);
873
874 /* Wait for the first serial output from the source */
875 wait_for_serial("src_serial");
876
877 migrate(from, uri, "{}");
878
879 wait_for_migration_pass(from);
880
881 /* 300 ms should converge */
882 migrate_set_parameter(from, "downtime-limit", 300);
883
884 if (!got_stop) {
885 qtest_qmp_eventwait(from, "STOP");
886 }
887
888 qtest_qmp_eventwait(to, "RESUME");
889
890 wait_for_serial("dest_serial");
891 wait_for_migration_complete(from);
892
893 test_migrate_end(from, to, true);
894 g_free(uri);
895 }
896
897 #if 0
898 /* Currently upset on aarch64 TCG */
899 static void test_ignore_shared(void)
900 {
901 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
902 QTestState *from, *to;
903
904 if (test_migrate_start(&from, &to, uri, false, true)) {
905 return;
906 }
907
908 migrate_set_capability(from, "x-ignore-shared", true);
909 migrate_set_capability(to, "x-ignore-shared", true);
910
911 /* Wait for the first serial output from the source */
912 wait_for_serial("src_serial");
913
914 migrate(from, uri, "{}");
915
916 wait_for_migration_pass(from);
917
918 if (!got_stop) {
919 qtest_qmp_eventwait(from, "STOP");
920 }
921
922 qtest_qmp_eventwait(to, "RESUME");
923
924 wait_for_serial("dest_serial");
925 wait_for_migration_complete(from);
926
927 /* Check whether shared RAM has been really skipped */
928 g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
929
930 test_migrate_end(from, to, true);
931 g_free(uri);
932 }
933 #endif
934
935 static void test_xbzrle(const char *uri)
936 {
937 QTestState *from, *to;
938
939 if (test_migrate_start(&from, &to, uri, false, false)) {
940 return;
941 }
942
943 /*
944 * We want to pick a speed slow enough that the test completes
945 * quickly, but that it doesn't complete precopy even on a slow
946 * machine, so also set the downtime.
947 */
948 /* 1 ms should make it not converge*/
949 migrate_set_parameter(from, "downtime-limit", 1);
950 /* 1GB/s */
951 migrate_set_parameter(from, "max-bandwidth", 1000000000);
952
953 migrate_set_parameter(from, "xbzrle-cache-size", 33554432);
954
955 migrate_set_capability(from, "xbzrle", "true");
956 migrate_set_capability(to, "xbzrle", "true");
957 /* Wait for the first serial output from the source */
958 wait_for_serial("src_serial");
959
960 migrate(from, uri, "{}");
961
962 wait_for_migration_pass(from);
963
964 /* 300ms should converge */
965 migrate_set_parameter(from, "downtime-limit", 300);
966
967 if (!got_stop) {
968 qtest_qmp_eventwait(from, "STOP");
969 }
970 qtest_qmp_eventwait(to, "RESUME");
971
972 wait_for_serial("dest_serial");
973 wait_for_migration_complete(from);
974
975 test_migrate_end(from, to, true);
976 }
977
978 static void test_xbzrle_unix(void)
979 {
980 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
981
982 test_xbzrle(uri);
983 g_free(uri);
984 }
985
986 static void test_precopy_tcp(void)
987 {
988 char *uri;
989 QTestState *from, *to;
990
991 if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", false, false)) {
992 return;
993 }
994
995 /*
996 * We want to pick a speed slow enough that the test completes
997 * quickly, but that it doesn't complete precopy even on a slow
998 * machine, so also set the downtime.
999 */
1000 /* 1 ms should make it not converge*/
1001 migrate_set_parameter(from, "downtime-limit", 1);
1002 /* 1GB/s */
1003 migrate_set_parameter(from, "max-bandwidth", 1000000000);
1004
1005 /* Wait for the first serial output from the source */
1006 wait_for_serial("src_serial");
1007
1008 uri = migrate_get_socket_address(to, "socket-address");
1009
1010 migrate(from, uri, "{}");
1011
1012 wait_for_migration_pass(from);
1013
1014 /* 300ms should converge */
1015 migrate_set_parameter(from, "downtime-limit", 300);
1016
1017 if (!got_stop) {
1018 qtest_qmp_eventwait(from, "STOP");
1019 }
1020 qtest_qmp_eventwait(to, "RESUME");
1021
1022 wait_for_serial("dest_serial");
1023 wait_for_migration_complete(from);
1024
1025 test_migrate_end(from, to, true);
1026 g_free(uri);
1027 }
1028
1029 int main(int argc, char **argv)
1030 {
1031 char template[] = "/tmp/migration-test-XXXXXX";
1032 int ret;
1033
1034 g_test_init(&argc, &argv, NULL);
1035
1036 if (!ufd_version_check()) {
1037 return g_test_run();
1038 }
1039
1040 /*
1041 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
1042 * is touchy due to race conditions on dirty bits (especially on PPC for
1043 * some reason)
1044 */
1045 if (g_str_equal(qtest_get_arch(), "ppc64") &&
1046 access("/sys/module/kvm_hv", F_OK)) {
1047 g_test_message("Skipping test: kvm_hv not available");
1048 return g_test_run();
1049 }
1050
1051 /*
1052 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
1053 * there until the problems are resolved
1054 */
1055 if (g_str_equal(qtest_get_arch(), "s390x")) {
1056 #if defined(HOST_S390X)
1057 if (access("/dev/kvm", R_OK | W_OK)) {
1058 g_test_message("Skipping test: kvm not available");
1059 return g_test_run();
1060 }
1061 #else
1062 g_test_message("Skipping test: Need s390x host to work properly");
1063 return g_test_run();
1064 #endif
1065 }
1066
1067 tmpfs = mkdtemp(template);
1068 if (!tmpfs) {
1069 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno));
1070 }
1071 g_assert(tmpfs);
1072
1073 module_call_init(MODULE_INIT_QOM);
1074
1075 qtest_add_func("/migration/postcopy/unix", test_postcopy);
1076 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery);
1077 qtest_add_func("/migration/deprecated", test_deprecated);
1078 qtest_add_func("/migration/bad_dest", test_baddest);
1079 qtest_add_func("/migration/precopy/unix", test_precopy_unix);
1080 qtest_add_func("/migration/precopy/tcp", test_precopy_tcp);
1081 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
1082 qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix);
1083
1084 ret = g_test_run();
1085
1086 g_assert_cmpint(ret, ==, 0);
1087
1088 ret = rmdir(tmpfs);
1089 if (ret != 0) {
1090 g_test_message("unable to rmdir: path (%s): %s",
1091 tmpfs, strerror(errno));
1092 }
1093
1094 return ret;
1095 }