]> git.proxmox.com Git - mirror_qemu.git/blob - tests/migration-test.c
migration-test: Add a test for fd protocol
[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(3, 4)
181 static QDict *wait_command_fd(QTestState *who, int fd, const char *command, ...)
182 {
183 va_list ap;
184
185 va_start(ap, command);
186 qtest_qmp_vsend_fds(who, &fd, 1, command, ap);
187 va_end(ap);
188
189 return qtest_qmp_receive_success(who, stop_cb, NULL);
190 }
191
192 /*
193 * Events can get in the way of responses we are actually waiting for.
194 */
195 GCC_FMT_ATTR(2, 3)
196 static QDict *wait_command(QTestState *who, const char *command, ...)
197 {
198 va_list ap;
199
200 va_start(ap, command);
201 qtest_qmp_vsend(who, command, ap);
202 va_end(ap);
203
204 return qtest_qmp_receive_success(who, stop_cb, NULL);
205 }
206
207 /*
208 * Note: caller is responsible to free the returned object via
209 * qobject_unref() after use
210 */
211 static QDict *migrate_query(QTestState *who)
212 {
213 return wait_command(who, "{ 'execute': 'query-migrate' }");
214 }
215
216 /*
217 * Note: caller is responsible to free the returned object via
218 * g_free() after use
219 */
220 static gchar *migrate_query_status(QTestState *who)
221 {
222 QDict *rsp_return = migrate_query(who);
223 gchar *status = g_strdup(qdict_get_str(rsp_return, "status"));
224
225 g_assert(status);
226 qobject_unref(rsp_return);
227
228 return status;
229 }
230
231 /*
232 * It's tricky to use qemu's migration event capability with qtest,
233 * events suddenly appearing confuse the qmp()/hmp() responses.
234 */
235
236 static int64_t read_ram_property_int(QTestState *who, const char *property)
237 {
238 QDict *rsp_return, *rsp_ram;
239 int64_t result;
240
241 rsp_return = migrate_query(who);
242 if (!qdict_haskey(rsp_return, "ram")) {
243 /* Still in setup */
244 result = 0;
245 } else {
246 rsp_ram = qdict_get_qdict(rsp_return, "ram");
247 result = qdict_get_try_int(rsp_ram, property, 0);
248 }
249 qobject_unref(rsp_return);
250 return result;
251 }
252
253 static uint64_t get_migration_pass(QTestState *who)
254 {
255 return read_ram_property_int(who, "dirty-sync-count");
256 }
257
258 static void read_blocktime(QTestState *who)
259 {
260 QDict *rsp_return;
261
262 rsp_return = migrate_query(who);
263 g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
264 qobject_unref(rsp_return);
265 }
266
267 static void wait_for_migration_status(QTestState *who,
268 const char *goal)
269 {
270 while (true) {
271 bool completed;
272 char *status;
273
274 status = migrate_query_status(who);
275 completed = strcmp(status, goal) == 0;
276 g_assert_cmpstr(status, !=, "failed");
277 g_free(status);
278 if (completed) {
279 return;
280 }
281 usleep(1000);
282 }
283 }
284
285 static void wait_for_migration_complete(QTestState *who)
286 {
287 wait_for_migration_status(who, "completed");
288 }
289
290 static void wait_for_migration_pass(QTestState *who)
291 {
292 uint64_t initial_pass = get_migration_pass(who);
293 uint64_t pass;
294
295 /* Wait for the 1st sync */
296 while (!got_stop && !initial_pass) {
297 usleep(1000);
298 initial_pass = get_migration_pass(who);
299 }
300
301 do {
302 usleep(1000);
303 pass = get_migration_pass(who);
304 } while (pass == initial_pass && !got_stop);
305 }
306
307 static void check_guests_ram(QTestState *who)
308 {
309 /* Our ASM test will have been incrementing one byte from each page from
310 * start_address to < end_address in order. This gives us a constraint
311 * that any page's byte should be equal or less than the previous pages
312 * byte (mod 256); and they should all be equal except for one transition
313 * at the point where we meet the incrementer. (We're running this with
314 * the guest stopped).
315 */
316 unsigned address;
317 uint8_t first_byte;
318 uint8_t last_byte;
319 bool hit_edge = false;
320 bool bad = false;
321
322 qtest_memread(who, start_address, &first_byte, 1);
323 last_byte = first_byte;
324
325 for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
326 address += TEST_MEM_PAGE_SIZE)
327 {
328 uint8_t b;
329 qtest_memread(who, address, &b, 1);
330 if (b != last_byte) {
331 if (((b + 1) % 256) == last_byte && !hit_edge) {
332 /* This is OK, the guest stopped at the point of
333 * incrementing the previous page but didn't get
334 * to us yet.
335 */
336 hit_edge = true;
337 last_byte = b;
338 } else {
339 fprintf(stderr, "Memory content inconsistency at %x"
340 " first_byte = %x last_byte = %x current = %x"
341 " hit_edge = %x\n",
342 address, first_byte, last_byte, b, hit_edge);
343 bad = true;
344 }
345 }
346 }
347 g_assert_false(bad);
348 }
349
350 static void cleanup(const char *filename)
351 {
352 char *path = g_strdup_printf("%s/%s", tmpfs, filename);
353
354 unlink(path);
355 g_free(path);
356 }
357
358 static char *get_shmem_opts(const char *mem_size, const char *shmem_path)
359 {
360 return g_strdup_printf("-object memory-backend-file,id=mem0,size=%s"
361 ",mem-path=%s,share=on -numa node,memdev=mem0",
362 mem_size, shmem_path);
363 }
364
365 static char *SocketAddress_to_str(SocketAddress *addr)
366 {
367 switch (addr->type) {
368 case SOCKET_ADDRESS_TYPE_INET:
369 return g_strdup_printf("tcp:%s:%s",
370 addr->u.inet.host,
371 addr->u.inet.port);
372 case SOCKET_ADDRESS_TYPE_UNIX:
373 return g_strdup_printf("unix:%s",
374 addr->u.q_unix.path);
375 case SOCKET_ADDRESS_TYPE_FD:
376 return g_strdup_printf("fd:%s", addr->u.fd.str);
377 case SOCKET_ADDRESS_TYPE_VSOCK:
378 return g_strdup_printf("tcp:%s:%s",
379 addr->u.vsock.cid,
380 addr->u.vsock.port);
381 default:
382 return g_strdup("unknown address type");
383 }
384 }
385
386 static char *migrate_get_socket_address(QTestState *who, const char *parameter)
387 {
388 QDict *rsp;
389 char *result;
390 Error *local_err = NULL;
391 SocketAddressList *addrs;
392 Visitor *iv = NULL;
393 QObject *object;
394
395 rsp = migrate_query(who);
396 object = qdict_get(rsp, parameter);
397
398 iv = qobject_input_visitor_new(object);
399 visit_type_SocketAddressList(iv, NULL, &addrs, &local_err);
400 visit_free(iv);
401
402 /* we are only using a single address */
403 result = SocketAddress_to_str(addrs->value);
404
405 qapi_free_SocketAddressList(addrs);
406 qobject_unref(rsp);
407 return result;
408 }
409
410 static long long migrate_get_parameter(QTestState *who, const char *parameter)
411 {
412 QDict *rsp;
413 long long result;
414
415 rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
416 result = qdict_get_int(rsp, parameter);
417 qobject_unref(rsp);
418 return result;
419 }
420
421 static void migrate_check_parameter(QTestState *who, const char *parameter,
422 long long value)
423 {
424 long long result;
425
426 result = migrate_get_parameter(who, parameter);
427 g_assert_cmpint(result, ==, value);
428 }
429
430 static void migrate_set_parameter(QTestState *who, const char *parameter,
431 long long value)
432 {
433 QDict *rsp;
434
435 rsp = qtest_qmp(who,
436 "{ 'execute': 'migrate-set-parameters',"
437 "'arguments': { %s: %lld } }",
438 parameter, value);
439 g_assert(qdict_haskey(rsp, "return"));
440 qobject_unref(rsp);
441 migrate_check_parameter(who, parameter, value);
442 }
443
444 static void migrate_pause(QTestState *who)
445 {
446 QDict *rsp;
447
448 rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
449 qobject_unref(rsp);
450 }
451
452 static void migrate_recover(QTestState *who, const char *uri)
453 {
454 QDict *rsp;
455
456 rsp = wait_command(who,
457 "{ 'execute': 'migrate-recover', "
458 " 'id': 'recover-cmd', "
459 " 'arguments': { 'uri': %s } }",
460 uri);
461 qobject_unref(rsp);
462 }
463
464 static void migrate_set_capability(QTestState *who, const char *capability,
465 bool value)
466 {
467 QDict *rsp;
468
469 rsp = qtest_qmp(who,
470 "{ 'execute': 'migrate-set-capabilities',"
471 "'arguments': { "
472 "'capabilities': [ { "
473 "'capability': %s, 'state': %i } ] } }",
474 capability, value);
475 g_assert(qdict_haskey(rsp, "return"));
476 qobject_unref(rsp);
477 }
478
479 /*
480 * Send QMP command "migrate".
481 * Arguments are built from @fmt... (formatted like
482 * qobject_from_jsonf_nofail()) with "uri": @uri spliced in.
483 */
484 GCC_FMT_ATTR(3, 4)
485 static void migrate(QTestState *who, const char *uri, const char *fmt, ...)
486 {
487 va_list ap;
488 QDict *args, *rsp;
489
490 va_start(ap, fmt);
491 args = qdict_from_vjsonf_nofail(fmt, ap);
492 va_end(ap);
493
494 g_assert(!qdict_haskey(args, "uri"));
495 qdict_put_str(args, "uri", uri);
496
497 rsp = qmp("{ 'execute': 'migrate', 'arguments': %p}", args);
498
499 g_assert(qdict_haskey(rsp, "return"));
500 qobject_unref(rsp);
501 }
502
503 static void migrate_postcopy_start(QTestState *from, QTestState *to)
504 {
505 QDict *rsp;
506
507 rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
508 qobject_unref(rsp);
509
510 if (!got_stop) {
511 qtest_qmp_eventwait(from, "STOP");
512 }
513
514 qtest_qmp_eventwait(to, "RESUME");
515 }
516
517 static int test_migrate_start(QTestState **from, QTestState **to,
518 const char *uri, bool hide_stderr,
519 bool use_shmem)
520 {
521 gchar *cmd_src, *cmd_dst;
522 char *bootpath = NULL;
523 char *extra_opts = NULL;
524 char *shmem_path = NULL;
525 const char *arch = qtest_get_arch();
526 const char *accel = "kvm:tcg";
527
528 if (use_shmem) {
529 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
530 g_test_skip("/dev/shm is not supported");
531 return -1;
532 }
533 shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
534 }
535
536 got_stop = false;
537 bootpath = g_strdup_printf("%s/bootsect", tmpfs);
538 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
539 init_bootfile(bootpath, x86_bootsect);
540 extra_opts = use_shmem ? get_shmem_opts("150M", shmem_path) : NULL;
541 cmd_src = g_strdup_printf("-machine accel=%s -m 150M"
542 " -name source,debug-threads=on"
543 " -serial file:%s/src_serial"
544 " -drive file=%s,format=raw %s",
545 accel, tmpfs, bootpath,
546 extra_opts ? extra_opts : "");
547 cmd_dst = g_strdup_printf("-machine accel=%s -m 150M"
548 " -name target,debug-threads=on"
549 " -serial file:%s/dest_serial"
550 " -drive file=%s,format=raw"
551 " -incoming %s %s",
552 accel, tmpfs, bootpath, uri,
553 extra_opts ? extra_opts : "");
554 start_address = X86_TEST_MEM_START;
555 end_address = X86_TEST_MEM_END;
556 } else if (g_str_equal(arch, "s390x")) {
557 init_bootfile_s390x(bootpath);
558 extra_opts = use_shmem ? get_shmem_opts("128M", shmem_path) : NULL;
559 cmd_src = g_strdup_printf("-machine accel=%s -m 128M"
560 " -name source,debug-threads=on"
561 " -serial file:%s/src_serial -bios %s %s",
562 accel, tmpfs, bootpath,
563 extra_opts ? extra_opts : "");
564 cmd_dst = g_strdup_printf("-machine accel=%s -m 128M"
565 " -name target,debug-threads=on"
566 " -serial file:%s/dest_serial -bios %s"
567 " -incoming %s %s",
568 accel, tmpfs, bootpath, uri,
569 extra_opts ? extra_opts : "");
570 start_address = S390_TEST_MEM_START;
571 end_address = S390_TEST_MEM_END;
572 } else if (strcmp(arch, "ppc64") == 0) {
573 extra_opts = use_shmem ? get_shmem_opts("256M", shmem_path) : NULL;
574 cmd_src = g_strdup_printf("-machine accel=%s -m 256M -nodefaults"
575 " -name source,debug-threads=on"
576 " -serial file:%s/src_serial"
577 " -prom-env 'use-nvramrc?=true' -prom-env "
578 "'nvramrc=hex .\" _\" begin %x %x "
579 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
580 "until' %s", accel, tmpfs, end_address,
581 start_address, extra_opts ? extra_opts : "");
582 cmd_dst = g_strdup_printf("-machine accel=%s -m 256M"
583 " -name target,debug-threads=on"
584 " -serial file:%s/dest_serial"
585 " -incoming %s %s",
586 accel, tmpfs, uri,
587 extra_opts ? extra_opts : "");
588
589 start_address = PPC_TEST_MEM_START;
590 end_address = PPC_TEST_MEM_END;
591 } else if (strcmp(arch, "aarch64") == 0) {
592 init_bootfile(bootpath, aarch64_kernel);
593 extra_opts = use_shmem ? get_shmem_opts("150M", shmem_path) : NULL;
594 cmd_src = g_strdup_printf("-machine virt,accel=%s,gic-version=max "
595 "-name vmsource,debug-threads=on -cpu max "
596 "-m 150M -serial file:%s/src_serial "
597 "-kernel %s %s",
598 accel, tmpfs, bootpath,
599 extra_opts ? extra_opts : "");
600 cmd_dst = g_strdup_printf("-machine virt,accel=%s,gic-version=max "
601 "-name vmdest,debug-threads=on -cpu max "
602 "-m 150M -serial file:%s/dest_serial "
603 "-kernel %s "
604 "-incoming %s %s",
605 accel, tmpfs, bootpath, uri,
606 extra_opts ? extra_opts : "");
607
608 start_address = ARM_TEST_MEM_START;
609 end_address = ARM_TEST_MEM_END;
610
611 g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
612 } else {
613 g_assert_not_reached();
614 }
615
616 g_free(bootpath);
617 g_free(extra_opts);
618
619 if (hide_stderr) {
620 gchar *tmp;
621 tmp = g_strdup_printf("%s 2>/dev/null", cmd_src);
622 g_free(cmd_src);
623 cmd_src = tmp;
624
625 tmp = g_strdup_printf("%s 2>/dev/null", cmd_dst);
626 g_free(cmd_dst);
627 cmd_dst = tmp;
628 }
629
630 *from = qtest_start(cmd_src);
631 g_free(cmd_src);
632
633 *to = qtest_init(cmd_dst);
634 g_free(cmd_dst);
635
636 /*
637 * Remove shmem file immediately to avoid memory leak in test failed case.
638 * It's valid becase QEMU has already opened this file
639 */
640 if (use_shmem) {
641 unlink(shmem_path);
642 g_free(shmem_path);
643 }
644
645 return 0;
646 }
647
648 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
649 {
650 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
651
652 qtest_quit(from);
653
654 if (test_dest) {
655 qtest_memread(to, start_address, &dest_byte_a, 1);
656
657 /* Destination still running, wait for a byte to change */
658 do {
659 qtest_memread(to, start_address, &dest_byte_b, 1);
660 usleep(1000 * 10);
661 } while (dest_byte_a == dest_byte_b);
662
663 qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
664
665 /* With it stopped, check nothing changes */
666 qtest_memread(to, start_address, &dest_byte_c, 1);
667 usleep(1000 * 200);
668 qtest_memread(to, start_address, &dest_byte_d, 1);
669 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
670
671 check_guests_ram(to);
672 }
673
674 qtest_quit(to);
675
676 cleanup("bootsect");
677 cleanup("migsocket");
678 cleanup("src_serial");
679 cleanup("dest_serial");
680 }
681
682 static void deprecated_set_downtime(QTestState *who, const double value)
683 {
684 QDict *rsp;
685
686 rsp = qtest_qmp(who,
687 "{ 'execute': 'migrate_set_downtime',"
688 " 'arguments': { 'value': %f } }", value);
689 g_assert(qdict_haskey(rsp, "return"));
690 qobject_unref(rsp);
691 migrate_check_parameter(who, "downtime-limit", value * 1000);
692 }
693
694 static void deprecated_set_speed(QTestState *who, long long value)
695 {
696 QDict *rsp;
697
698 rsp = qtest_qmp(who, "{ 'execute': 'migrate_set_speed',"
699 "'arguments': { 'value': %lld } }", value);
700 g_assert(qdict_haskey(rsp, "return"));
701 qobject_unref(rsp);
702 migrate_check_parameter(who, "max-bandwidth", value);
703 }
704
705 static void deprecated_set_cache_size(QTestState *who, long long value)
706 {
707 QDict *rsp;
708
709 rsp = qtest_qmp(who, "{ 'execute': 'migrate-set-cache-size',"
710 "'arguments': { 'value': %lld } }", value);
711 g_assert(qdict_haskey(rsp, "return"));
712 qobject_unref(rsp);
713 migrate_check_parameter(who, "xbzrle-cache-size", value);
714 }
715
716 static void test_deprecated(void)
717 {
718 QTestState *from;
719
720 from = qtest_start("-machine none");
721
722 deprecated_set_downtime(from, 0.12345);
723 deprecated_set_speed(from, 12345);
724 deprecated_set_cache_size(from, 4096);
725
726 qtest_quit(from);
727 }
728
729 static int migrate_postcopy_prepare(QTestState **from_ptr,
730 QTestState **to_ptr,
731 bool hide_error)
732 {
733 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
734 QTestState *from, *to;
735
736 if (test_migrate_start(&from, &to, uri, hide_error, false)) {
737 return -1;
738 }
739
740 migrate_set_capability(from, "postcopy-ram", true);
741 migrate_set_capability(to, "postcopy-ram", true);
742 migrate_set_capability(to, "postcopy-blocktime", true);
743
744 /* We want to pick a speed slow enough that the test completes
745 * quickly, but that it doesn't complete precopy even on a slow
746 * machine, so also set the downtime.
747 */
748 migrate_set_parameter(from, "max-bandwidth", 100000000);
749 migrate_set_parameter(from, "downtime-limit", 1);
750
751 /* Wait for the first serial output from the source */
752 wait_for_serial("src_serial");
753
754 migrate(from, uri, "{}");
755 g_free(uri);
756
757 wait_for_migration_pass(from);
758
759 *from_ptr = from;
760 *to_ptr = to;
761
762 return 0;
763 }
764
765 static void migrate_postcopy_complete(QTestState *from, QTestState *to)
766 {
767 wait_for_migration_complete(from);
768
769 /* Make sure we get at least one "B" on destination */
770 wait_for_serial("dest_serial");
771
772 if (uffd_feature_thread_id) {
773 read_blocktime(to);
774 }
775
776 test_migrate_end(from, to, true);
777 }
778
779 static void test_postcopy(void)
780 {
781 QTestState *from, *to;
782
783 if (migrate_postcopy_prepare(&from, &to, false)) {
784 return;
785 }
786 migrate_postcopy_start(from, to);
787 migrate_postcopy_complete(from, to);
788 }
789
790 static void test_postcopy_recovery(void)
791 {
792 QTestState *from, *to;
793 char *uri;
794
795 if (migrate_postcopy_prepare(&from, &to, true)) {
796 return;
797 }
798
799 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
800 migrate_set_parameter(from, "max-postcopy-bandwidth", 4096);
801
802 /* Now we start the postcopy */
803 migrate_postcopy_start(from, to);
804
805 /*
806 * Wait until postcopy is really started; we can only run the
807 * migrate-pause command during a postcopy
808 */
809 wait_for_migration_status(from, "postcopy-active");
810
811 /*
812 * Manually stop the postcopy migration. This emulates a network
813 * failure with the migration socket
814 */
815 migrate_pause(from);
816
817 /*
818 * Wait for destination side to reach postcopy-paused state. The
819 * migrate-recover command can only succeed if destination machine
820 * is in the paused state
821 */
822 wait_for_migration_status(to, "postcopy-paused");
823
824 /*
825 * Create a new socket to emulate a new channel that is different
826 * from the broken migration channel; tell the destination to
827 * listen to the new port
828 */
829 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
830 migrate_recover(to, uri);
831
832 /*
833 * Try to rebuild the migration channel using the resume flag and
834 * the newly created channel
835 */
836 wait_for_migration_status(from, "postcopy-paused");
837 migrate(from, uri, "{'resume': true}");
838 g_free(uri);
839
840 /* Restore the postcopy bandwidth to unlimited */
841 migrate_set_parameter(from, "max-postcopy-bandwidth", 0);
842
843 migrate_postcopy_complete(from, to);
844 }
845
846 static void test_baddest(void)
847 {
848 QTestState *from, *to;
849 QDict *rsp_return;
850 char *status;
851 bool failed;
852
853 if (test_migrate_start(&from, &to, "tcp:0:0", true, false)) {
854 return;
855 }
856 migrate(from, "tcp:0:0", "{}");
857 do {
858 status = migrate_query_status(from);
859 g_assert(!strcmp(status, "setup") || !(strcmp(status, "failed")));
860 failed = !strcmp(status, "failed");
861 g_free(status);
862 } while (!failed);
863
864 /* Is the machine currently running? */
865 rsp_return = wait_command(from, "{ 'execute': 'query-status' }");
866 g_assert(qdict_haskey(rsp_return, "running"));
867 g_assert(qdict_get_bool(rsp_return, "running"));
868 qobject_unref(rsp_return);
869
870 test_migrate_end(from, to, false);
871 }
872
873 static void test_precopy_unix(void)
874 {
875 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
876 QTestState *from, *to;
877
878 if (test_migrate_start(&from, &to, uri, false, false)) {
879 return;
880 }
881
882 /* We want to pick a speed slow enough that the test completes
883 * quickly, but that it doesn't complete precopy even on a slow
884 * machine, so also set the downtime.
885 */
886 /* 1 ms should make it not converge*/
887 migrate_set_parameter(from, "downtime-limit", 1);
888 /* 1GB/s */
889 migrate_set_parameter(from, "max-bandwidth", 1000000000);
890
891 /* Wait for the first serial output from the source */
892 wait_for_serial("src_serial");
893
894 migrate(from, uri, "{}");
895
896 wait_for_migration_pass(from);
897
898 /* 300 ms should converge */
899 migrate_set_parameter(from, "downtime-limit", 300);
900
901 if (!got_stop) {
902 qtest_qmp_eventwait(from, "STOP");
903 }
904
905 qtest_qmp_eventwait(to, "RESUME");
906
907 wait_for_serial("dest_serial");
908 wait_for_migration_complete(from);
909
910 test_migrate_end(from, to, true);
911 g_free(uri);
912 }
913
914 #if 0
915 /* Currently upset on aarch64 TCG */
916 static void test_ignore_shared(void)
917 {
918 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
919 QTestState *from, *to;
920
921 if (test_migrate_start(&from, &to, uri, false, true)) {
922 return;
923 }
924
925 migrate_set_capability(from, "x-ignore-shared", true);
926 migrate_set_capability(to, "x-ignore-shared", true);
927
928 /* Wait for the first serial output from the source */
929 wait_for_serial("src_serial");
930
931 migrate(from, uri, "{}");
932
933 wait_for_migration_pass(from);
934
935 if (!got_stop) {
936 qtest_qmp_eventwait(from, "STOP");
937 }
938
939 qtest_qmp_eventwait(to, "RESUME");
940
941 wait_for_serial("dest_serial");
942 wait_for_migration_complete(from);
943
944 /* Check whether shared RAM has been really skipped */
945 g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
946
947 test_migrate_end(from, to, true);
948 g_free(uri);
949 }
950 #endif
951
952 static void test_xbzrle(const char *uri)
953 {
954 QTestState *from, *to;
955
956 if (test_migrate_start(&from, &to, uri, false, false)) {
957 return;
958 }
959
960 /*
961 * We want to pick a speed slow enough that the test completes
962 * quickly, but that it doesn't complete precopy even on a slow
963 * machine, so also set the downtime.
964 */
965 /* 1 ms should make it not converge*/
966 migrate_set_parameter(from, "downtime-limit", 1);
967 /* 1GB/s */
968 migrate_set_parameter(from, "max-bandwidth", 1000000000);
969
970 migrate_set_parameter(from, "xbzrle-cache-size", 33554432);
971
972 migrate_set_capability(from, "xbzrle", "true");
973 migrate_set_capability(to, "xbzrle", "true");
974 /* Wait for the first serial output from the source */
975 wait_for_serial("src_serial");
976
977 migrate(from, uri, "{}");
978
979 wait_for_migration_pass(from);
980
981 /* 300ms should converge */
982 migrate_set_parameter(from, "downtime-limit", 300);
983
984 if (!got_stop) {
985 qtest_qmp_eventwait(from, "STOP");
986 }
987 qtest_qmp_eventwait(to, "RESUME");
988
989 wait_for_serial("dest_serial");
990 wait_for_migration_complete(from);
991
992 test_migrate_end(from, to, true);
993 }
994
995 static void test_xbzrle_unix(void)
996 {
997 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
998
999 test_xbzrle(uri);
1000 g_free(uri);
1001 }
1002
1003 static void test_precopy_tcp(void)
1004 {
1005 char *uri;
1006 QTestState *from, *to;
1007
1008 if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", false, false)) {
1009 return;
1010 }
1011
1012 /*
1013 * We want to pick a speed slow enough that the test completes
1014 * quickly, but that it doesn't complete precopy even on a slow
1015 * machine, so also set the downtime.
1016 */
1017 /* 1 ms should make it not converge*/
1018 migrate_set_parameter(from, "downtime-limit", 1);
1019 /* 1GB/s */
1020 migrate_set_parameter(from, "max-bandwidth", 1000000000);
1021
1022 /* Wait for the first serial output from the source */
1023 wait_for_serial("src_serial");
1024
1025 uri = migrate_get_socket_address(to, "socket-address");
1026
1027 migrate(from, uri, "{}");
1028
1029 wait_for_migration_pass(from);
1030
1031 /* 300ms should converge */
1032 migrate_set_parameter(from, "downtime-limit", 300);
1033
1034 if (!got_stop) {
1035 qtest_qmp_eventwait(from, "STOP");
1036 }
1037 qtest_qmp_eventwait(to, "RESUME");
1038
1039 wait_for_serial("dest_serial");
1040 wait_for_migration_complete(from);
1041
1042 test_migrate_end(from, to, true);
1043 g_free(uri);
1044 }
1045
1046 static void test_migrate_fd_proto(void)
1047 {
1048 QTestState *from, *to;
1049 int ret;
1050 int pair[2];
1051 QDict *rsp;
1052 const char *error_desc;
1053
1054 if (test_migrate_start(&from, &to, "defer", false, false)) {
1055 return;
1056 }
1057
1058 /*
1059 * We want to pick a speed slow enough that the test completes
1060 * quickly, but that it doesn't complete precopy even on a slow
1061 * machine, so also set the downtime.
1062 */
1063 /* 1 ms should make it not converge */
1064 migrate_set_parameter(from, "downtime-limit", 1);
1065 /* 1GB/s */
1066 migrate_set_parameter(from, "max-bandwidth", 1000000000);
1067
1068 /* Wait for the first serial output from the source */
1069 wait_for_serial("src_serial");
1070
1071 /* Create two connected sockets for migration */
1072 ret = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
1073 g_assert_cmpint(ret, ==, 0);
1074
1075 /* Send the 1st socket to the target */
1076 rsp = wait_command_fd(to, pair[0],
1077 "{ 'execute': 'getfd',"
1078 " 'arguments': { 'fdname': 'fd-mig' }}");
1079 qobject_unref(rsp);
1080 close(pair[0]);
1081
1082 /* Start incoming migration from the 1st socket */
1083 rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1084 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1085 qobject_unref(rsp);
1086
1087 /* Send the 2nd socket to the target */
1088 rsp = wait_command_fd(from, pair[1],
1089 "{ 'execute': 'getfd',"
1090 " 'arguments': { 'fdname': 'fd-mig' }}");
1091 qobject_unref(rsp);
1092 close(pair[1]);
1093
1094 /* Start migration to the 2nd socket*/
1095 migrate(from, "fd:fd-mig", "{}");
1096
1097 wait_for_migration_pass(from);
1098
1099 /* 300ms should converge */
1100 migrate_set_parameter(from, "downtime-limit", 300);
1101
1102 if (!got_stop) {
1103 qtest_qmp_eventwait(from, "STOP");
1104 }
1105 qtest_qmp_eventwait(to, "RESUME");
1106
1107 /* Test closing fds */
1108 /* We assume, that QEMU removes named fd from its list,
1109 * so this should fail */
1110 rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
1111 " 'arguments': { 'fdname': 'fd-mig' }}");
1112 g_assert_true(qdict_haskey(rsp, "error"));
1113 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1114 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1115 qobject_unref(rsp);
1116
1117 rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
1118 " 'arguments': { 'fdname': 'fd-mig' }}");
1119 g_assert_true(qdict_haskey(rsp, "error"));
1120 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1121 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1122 qobject_unref(rsp);
1123
1124 /* Complete migration */
1125 wait_for_serial("dest_serial");
1126 wait_for_migration_complete(from);
1127 test_migrate_end(from, to, true);
1128 }
1129
1130 int main(int argc, char **argv)
1131 {
1132 char template[] = "/tmp/migration-test-XXXXXX";
1133 int ret;
1134
1135 g_test_init(&argc, &argv, NULL);
1136
1137 if (!ufd_version_check()) {
1138 return g_test_run();
1139 }
1140
1141 /*
1142 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
1143 * is touchy due to race conditions on dirty bits (especially on PPC for
1144 * some reason)
1145 */
1146 if (g_str_equal(qtest_get_arch(), "ppc64") &&
1147 access("/sys/module/kvm_hv", F_OK)) {
1148 g_test_message("Skipping test: kvm_hv not available");
1149 return g_test_run();
1150 }
1151
1152 /*
1153 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
1154 * there until the problems are resolved
1155 */
1156 if (g_str_equal(qtest_get_arch(), "s390x")) {
1157 #if defined(HOST_S390X)
1158 if (access("/dev/kvm", R_OK | W_OK)) {
1159 g_test_message("Skipping test: kvm not available");
1160 return g_test_run();
1161 }
1162 #else
1163 g_test_message("Skipping test: Need s390x host to work properly");
1164 return g_test_run();
1165 #endif
1166 }
1167
1168 tmpfs = mkdtemp(template);
1169 if (!tmpfs) {
1170 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno));
1171 }
1172 g_assert(tmpfs);
1173
1174 module_call_init(MODULE_INIT_QOM);
1175
1176 qtest_add_func("/migration/postcopy/unix", test_postcopy);
1177 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery);
1178 qtest_add_func("/migration/deprecated", test_deprecated);
1179 qtest_add_func("/migration/bad_dest", test_baddest);
1180 qtest_add_func("/migration/precopy/unix", test_precopy_unix);
1181 qtest_add_func("/migration/precopy/tcp", test_precopy_tcp);
1182 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
1183 qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix);
1184 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto);
1185
1186 ret = g_test_run();
1187
1188 g_assert_cmpint(ret, ==, 0);
1189
1190 ret = rmdir(tmpfs);
1191 if (ret != 0) {
1192 g_test_message("unable to rmdir: path (%s): %s",
1193 tmpfs, strerror(errno));
1194 }
1195
1196 return ret;
1197 }