master
c 1,146 lines 34 KB
Raw
1 /*
2 * Copyright (c) 2016-2018 Red Hat, Inc. and/or its affiliates
3 * based on the vhost-user-test.c that is:
4 * Copyright (c) 2014 Virtual Open Systems Sarl.
5 *
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
8 *
9 */
10
11 #include "qemu/osdep.h"
12
13 #include "chardev/char.h"
14 #include "crypto/tlscredspsk.h"
15 #include "libqtest.h"
16 #include "migration/bootfile.h"
17 #include "migration/framework.h"
18 #include "migration/migration-qmp.h"
19 #include "migration/migration-util.h"
20 #include "ppc-util.h"
21 #include "qapi/error.h"
22 #include "qobject/qjson.h"
23 #include "qemu/bswap.h"
24 #include "qemu/module.h"
25 #include "qemu/option.h"
26 #include "qemu/range.h"
27 #include "qemu/sockets.h"
28
29
30 #define QEMU_VM_FILE_MAGIC 0x5145564d
31 #define QEMU_ENV_SRC "QTEST_QEMU_BINARY_SRC"
32 #define QEMU_ENV_DST "QTEST_QEMU_BINARY_DST"
33 #define MULTIFD_TEST_CHANNELS 4
34
35 unsigned start_address;
36 unsigned end_address;
37 static QTestMigrationState src_state;
38 static QTestMigrationState dst_state;
39 static char *tmpfs;
40
41 /*
42 * An initial 3 MB offset is used as that corresponds
43 * to ~1 sec of data transfer with our bandwidth setting.
44 */
45 #define MAGIC_OFFSET_BASE (3 * 1024 * 1024)
46 /*
47 * A further 1k is added to ensure we're not a multiple
48 * of TEST_MEM_PAGE_SIZE, thus avoid clash with writes
49 * from the migration guest workload.
50 */
51 #define MAGIC_OFFSET_SHUFFLE 1024
52 #define MAGIC_OFFSET (MAGIC_OFFSET_BASE + MAGIC_OFFSET_SHUFFLE)
53 #define MAGIC_MARKER 0xFEED12345678CAFEULL
54
55
56 /*
57 * Wait for some output in the serial output file,
58 * we get an 'A' followed by an endless string of 'B's
59 * but on the destination we won't have the A (unless we enabled suspend/resume)
60 */
61 void wait_for_serial(const char *side)
62 {
63 g_autofree char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
64 FILE *serialfile = fopen(serialpath, "r");
65
66 do {
67 int readvalue = fgetc(serialfile);
68
69 switch (readvalue) {
70 case 'A':
71 /* Fine */
72 break;
73
74 case 'B':
75 /* It's alive! */
76 fclose(serialfile);
77 return;
78
79 case EOF:
80 fseek(serialfile, 0, SEEK_SET);
81 usleep(1000);
82 break;
83
84 default:
85 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
86 g_assert_not_reached();
87 }
88 } while (true);
89 }
90
91 void migrate_prepare_for_dirty_mem(QTestState *from)
92 {
93 /*
94 * The guest workflow iterates from start_address to
95 * end_address, writing 1 byte every TEST_MEM_PAGE_SIZE
96 * bytes.
97 *
98 * IOW, if we write to mem at a point which is NOT
99 * a multiple of TEST_MEM_PAGE_SIZE, our write won't
100 * conflict with the migration workflow.
101 *
102 * We put in a marker here, that we'll use to determine
103 * when the data has been transferred to the dst.
104 */
105 qtest_writeq(from, start_address + MAGIC_OFFSET, MAGIC_MARKER);
106 }
107
108 void migrate_wait_for_dirty_mem(QTestState *from, QTestState *to)
109 {
110 uint64_t watch_address = start_address + MAGIC_OFFSET_BASE;
111 uint64_t marker_address = start_address + MAGIC_OFFSET;
112 uint8_t watch_byte;
113
114 /*
115 * Wait for the MAGIC_MARKER to get transferred, as an
116 * indicator that a migration pass has made some known
117 * amount of progress.
118 */
119 do {
120 usleep(1000 * 10);
121 } while (qtest_readq(to, marker_address) != MAGIC_MARKER);
122
123
124 /* If suspended, src only iterates once, and watch_byte may never change */
125 if (src_state.suspend_me) {
126 return;
127 }
128
129 /*
130 * Now ensure that already transferred bytes are
131 * dirty again from the guest workload. Note the
132 * guest byte value will wrap around and by chance
133 * match the original watch_byte. This is harmless
134 * as we'll eventually see a different value if we
135 * keep watching
136 */
137 watch_byte = qtest_readb(from, watch_address);
138 do {
139 usleep(1000 * 10);
140 } while (qtest_readb(from, watch_address) == watch_byte);
141 }
142
143 static void check_guests_ram(QTestState *who)
144 {
145 /*
146 * Our ASM test will have been incrementing one byte from each page from
147 * start_address to < end_address in order. This gives us a constraint
148 * that any page's byte should be equal or less than the previous pages
149 * byte (mod 256); and they should all be equal except for one transition
150 * at the point where we meet the incrementer. (We're running this with
151 * the guest stopped).
152 */
153 unsigned address;
154 uint8_t first_byte;
155 uint8_t last_byte;
156 bool hit_edge = false;
157 int bad = 0;
158
159 qtest_memread(who, start_address, &first_byte, 1);
160 last_byte = first_byte;
161
162 for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
163 address += TEST_MEM_PAGE_SIZE)
164 {
165 uint8_t b;
166 qtest_memread(who, address, &b, 1);
167 if (b != last_byte) {
168 if (((b + 1) % 256) == last_byte && !hit_edge) {
169 /*
170 * This is OK, the guest stopped at the point of
171 * incrementing the previous page but didn't get
172 * to us yet.
173 */
174 hit_edge = true;
175 last_byte = b;
176 } else {
177 bad++;
178 if (bad <= 10) {
179 fprintf(stderr, "Memory content inconsistency at %x"
180 " first_byte = %x last_byte = %x current = %x"
181 " hit_edge = %x\n",
182 address, first_byte, last_byte, b, hit_edge);
183 }
184 }
185 }
186 }
187 if (bad >= 10) {
188 fprintf(stderr, "and in another %d pages", bad - 10);
189 }
190 g_assert(bad == 0);
191 }
192
193 static void cleanup(const char *filename)
194 {
195 g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, filename);
196
197 unlink(path);
198 }
199
200 static QList *migrate_start_get_qmp_capabilities(const MigrateStart *args)
201 {
202 QList *capabilities = NULL;
203
204 if (args->oob) {
205 capabilities = qlist_new();
206 qlist_append_str(capabilities, "oob");
207 }
208 return capabilities;
209 }
210
211 static void migrate_start_set_capabilities(QTestState *from, QTestState *to,
212 MigrateStart *args)
213 {
214 /*
215 * MigrationCapability_lookup and MIGRATION_CAPABILITY_ constants
216 * are from qapi-types-migration.h.
217 */
218
219 /*
220 * Enable return path first, since other features depend on it.
221 */
222 if (args->caps[MIGRATION_CAPABILITY_RETURN_PATH]) {
223 if (from) {
224 migrate_set_capability(from, "return-path", true);
225 }
226 if (to) {
227 migrate_set_capability(to, "return-path", true);
228 }
229 }
230
231 for (uint8_t i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
232 if (!args->caps[i]) {
233 continue;
234 }
235 if (from) {
236 migrate_set_capability(from,
237 MigrationCapability_lookup.array[i], true);
238 }
239 if (to) {
240 migrate_set_capability(to,
241 MigrationCapability_lookup.array[i], true);
242 }
243 }
244
245 /*
246 * Always enable migration events. Libvirt always uses it, let's try
247 * to mimic as closer as that.
248 */
249 migrate_set_capability(from, "events", true);
250 if (!args->defer_target_connect && to) {
251 migrate_set_capability(to, "events", true);
252 }
253
254 /*
255 * Default number of channels should be fine for most
256 * tests. Individual tests can override by calling
257 * migrate_set_parameter() directly.
258 */
259 if (args->caps[MIGRATION_CAPABILITY_MULTIFD]) {
260 migrate_set_parameter_int(from, "multifd-channels",
261 MULTIFD_TEST_CHANNELS);
262 if (to) {
263 migrate_set_parameter_int(to, "multifd-channels",
264 MULTIFD_TEST_CHANNELS);
265 }
266 }
267
268 return;
269 }
270
271 static char *test_shmem_path(void)
272 {
273 return g_strdup_printf("/dev/shm/qemu-%d", getpid());
274 }
275
276 #define MIG_MEM_ID "mig.mem"
277
278 /* NOTE: caller is responsbile to free the string if returned */
279 static char *migrate_mem_type_get_opts(MemType type, const char *memory_size)
280 {
281 g_autofree char *shmem_path = NULL;
282 g_autofree char *backend = NULL;
283 bool share = true;
284 char *opts;
285
286 switch (type) {
287 case MEM_TYPE_ANON:
288 backend = g_strdup("-object memory-backend-ram");
289 share = false;
290 break;
291 case MEM_TYPE_SHMEM:
292 shmem_path = test_shmem_path();
293 backend = g_strdup_printf("-object memory-backend-file,mem-path=%s",
294 shmem_path);
295 break;
296 case MEM_TYPE_MEMFD:
297 backend = g_strdup("-object memory-backend-memfd");
298 break;
299 default:
300 g_assert_not_reached();
301 break;
302 }
303
304 opts = g_strdup_printf("%s,id=%s,size=%s,share=%s",
305 backend, MIG_MEM_ID, memory_size,
306 share ? "on" : "off");
307
308 return opts;
309 }
310
311 int migrate_args(char **from, char **to, MigrateStart *args)
312 {
313 /* options for source and target */
314 g_autofree gchar *arch_opts = NULL;
315 gchar *cmd_source = NULL;
316 gchar *cmd_target = NULL;
317 const gchar *ignore_stderr;
318 g_autofree char *mem_object = NULL;
319 const char *kvm_opts = NULL;
320 const char *arch = qtest_get_arch();
321 const char *memory_size;
322 const char *machine_alias, *machine_opts = "";
323 g_autofree char *machine = NULL;
324 const char *bootpath = bootfile_get();
325 g_autofree char *memory_backend = NULL;
326 const char *events;
327
328 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
329 memory_size = "150M";
330
331 if (g_str_equal(arch, "i386")) {
332 machine_alias = "pc";
333 } else {
334 machine_alias = "q35";
335 }
336 arch_opts = g_strdup_printf(
337 "-drive if=none,id=d0,file=%s,format=raw "
338 "-device ide-hd,drive=d0,secs=1,cyls=1,heads=1", bootpath);
339 start_address = X86_TEST_MEM_START;
340 end_address = X86_TEST_MEM_END;
341 } else if (g_str_equal(arch, "s390x")) {
342 memory_size = "128M";
343 machine_alias = "s390-ccw-virtio";
344 arch_opts = g_strdup_printf("-bios %s", bootpath);
345 start_address = S390_TEST_MEM_START;
346 end_address = S390_TEST_MEM_END;
347 } else if (strcmp(arch, "ppc64") == 0) {
348 memory_size = "256M";
349 start_address = PPC_TEST_MEM_START;
350 end_address = PPC_TEST_MEM_END;
351 machine_alias = "pseries";
352 machine_opts = "vsmt=8";
353 arch_opts = g_strdup_printf(
354 "-nodefaults -machine " PSERIES_DEFAULT_CAPABILITIES " "
355 "-bios %s", bootpath);
356 } else if (strcmp(arch, "aarch64") == 0) {
357 memory_size = "150M";
358 machine_alias = "virt";
359 machine_opts = "gic-version=3";
360 arch_opts = g_strdup_printf("-cpu max -kernel %s", bootpath);
361 start_address = ARM_TEST_MEM_START;
362 end_address = ARM_TEST_MEM_END;
363 } else if (strcmp(arch, "loongarch64") == 0) {
364 memory_size = "256M";
365 machine_alias = "virt";
366 arch_opts = g_strdup_printf("-bios %s", bootpath);
367 start_address = LOONGARCH_TEST_MEM_START;
368 end_address = LOONGARCH_TEST_MEM_END;
369 } else {
370 g_assert_not_reached();
371 }
372
373 if (!qtest_verbose("test") && args->hide_stderr) {
374 #ifndef _WIN32
375 ignore_stderr = "2>/dev/null";
376 #else
377 /*
378 * On Windows the QEMU executable is created via CreateProcess() and
379 * IO redirection does not work, so don't bother adding IO redirection
380 * to the command line.
381 */
382 ignore_stderr = "";
383 #endif
384 } else {
385 ignore_stderr = "";
386 }
387
388 mem_object = migrate_mem_type_get_opts(args->mem_type, memory_size);
389 memory_backend = g_strdup_printf("-machine memory-backend=%s %s",
390 MIG_MEM_ID, mem_object);
391
392 if (args->use_dirty_ring) {
393 kvm_opts = ",dirty-ring-size=4096";
394 }
395
396 if (!qtest_has_machine(machine_alias)) {
397 g_autofree char *msg = g_strdup_printf("machine %s not supported", machine_alias);
398 g_test_skip(msg);
399 return -1;
400 }
401
402 machine = resolve_machine_version(machine_alias, QEMU_ENV_SRC,
403 QEMU_ENV_DST);
404
405 g_test_message("Using machine type: %s", machine);
406
407 cmd_source = g_strdup_printf("-accel kvm%s -accel tcg "
408 "-machine %s,%s "
409 "-name source,debug-threads=on "
410 "%s "
411 "-serial file:%s/src_serial "
412 "%s %s %s",
413 kvm_opts ? kvm_opts : "",
414 machine, machine_opts,
415 memory_backend, tmpfs,
416 arch_opts ? arch_opts : "",
417 args->opts_source ? args->opts_source : "",
418 ignore_stderr);
419
420 /*
421 * If the monitor connection is deferred, enable events on the command line
422 * so none are missed. This is for testing only, do not set migration
423 * options like this in general.
424 */
425 events = args->defer_target_connect ? "-global migration.x-events=on" : "";
426
427 cmd_target = g_strdup_printf("-accel kvm%s -accel tcg "
428 "-machine %s,%s "
429 "-name target,debug-threads=on "
430 "%s "
431 "-serial file:%s/dest_serial "
432 "-incoming defer "
433 "%s %s %s %s",
434 kvm_opts ? kvm_opts : "",
435 machine, machine_opts,
436 memory_backend, tmpfs,
437 events,
438 arch_opts ? arch_opts : "",
439 args->opts_target ? args->opts_target : "",
440 ignore_stderr);
441
442 *from = cmd_source;
443 *to = cmd_target;
444 return 0;
445 }
446
447 static bool migrate_mem_type_prepare(MemType type)
448 {
449 switch (type) {
450 case MEM_TYPE_SHMEM:
451 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
452 g_test_skip("/dev/shm is not supported");
453 return false;
454 }
455 break;
456 default:
457 break;
458 }
459
460 return true;
461 }
462
463 static void migrate_mem_type_cleanup(MemType type)
464 {
465 g_autofree char *shmem_path = NULL;
466
467 switch (type) {
468 case MEM_TYPE_SHMEM:
469
470 /*
471 * Remove shmem file immediately to avoid memory leak in test
472 * failed case. It's valid because QEMU has already opened this
473 * file
474 */
475 shmem_path = test_shmem_path();
476 unlink(shmem_path);
477 break;
478 default:
479 break;
480 }
481 }
482
483 int migrate_start(QTestState **from, QTestState **to, MigrateStart *args)
484 {
485 g_autofree gchar *cmd_source = NULL;
486 g_autofree gchar *cmd_target = NULL;
487 g_autoptr(QList) capabilities = migrate_start_get_qmp_capabilities(args);
488
489 if (!migrate_mem_type_prepare(args->mem_type)) {
490 return -1;
491 }
492
493 dst_state = (QTestMigrationState) { };
494 src_state = (QTestMigrationState) { };
495 bootfile_create(qtest_get_arch(), tmpfs, args->suspend_me);
496 src_state.suspend_me = args->suspend_me;
497
498 if (migrate_args(&cmd_source, &cmd_target, args)) {
499 return -1;
500 }
501
502 if (!args->only_target) {
503 *from = qtest_init_ext(QEMU_ENV_SRC, cmd_source, capabilities, true);
504 qtest_qmp_set_event_callback(*from,
505 migrate_watch_for_events,
506 &src_state);
507 }
508
509 if (!args->only_source) {
510 *to = qtest_init_ext(QEMU_ENV_DST, cmd_target, capabilities,
511 !args->defer_target_connect);
512 qtest_qmp_set_event_callback(*to,
513 migrate_watch_for_events,
514 &dst_state);
515 }
516
517 migrate_mem_type_cleanup(args->mem_type);
518 migrate_start_set_capabilities(*from,
519 args->only_source ? NULL : *to,
520 args);
521
522 return 0;
523 }
524
525 void migrate_end(QTestState *from, QTestState *to, bool test_dest)
526 {
527 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
528
529 qtest_quit(from);
530
531 if (test_dest) {
532 qtest_memread(to, start_address, &dest_byte_a, 1);
533
534 /* Destination still running, wait for a byte to change */
535 do {
536 qtest_memread(to, start_address, &dest_byte_b, 1);
537 usleep(1000 * 10);
538 } while (dest_byte_a == dest_byte_b);
539
540 qtest_qmp_assert_success(to, "{ 'execute' : 'stop'}");
541
542 /* With it stopped, check nothing changes */
543 qtest_memread(to, start_address, &dest_byte_c, 1);
544 usleep(1000 * 200);
545 qtest_memread(to, start_address, &dest_byte_d, 1);
546 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
547
548 check_guests_ram(to);
549 }
550
551 qtest_quit(to);
552
553 cleanup("migsocket");
554 cleanup("cpr.sock");
555 cleanup("src_serial");
556 cleanup("dest_serial");
557 cleanup(FILE_TEST_FILENAME);
558 }
559
560 static int migrate_postcopy_prepare(QTestState **from_ptr,
561 QTestState **to_ptr,
562 void **hook_data,
563 MigrateCommon *args)
564 {
565 QTestState *from, *to;
566
567 /* set postcopy capabilities */
568 args->start.caps[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME] = true;
569 args->start.caps[MIGRATION_CAPABILITY_POSTCOPY_RAM] = true;
570
571 if (migrate_start(&from, &to, &args->start)) {
572 return -1;
573 }
574
575 if (args->start_hook) {
576 *hook_data = args->start_hook(from, to);
577 }
578
579 migrate_ensure_non_converge(from);
580 migrate_prepare_for_dirty_mem(from);
581 qtest_qmp_assert_success(to, "{ 'execute': 'migrate-incoming',"
582 " 'arguments': { "
583 " 'exit-on-error': false,"
584 " 'channels': [ { 'channel-type': 'main',"
585 " 'addr': { 'transport': 'socket',"
586 " 'type': 'inet',"
587 " 'host': '127.0.0.1',"
588 " 'port': '0' } } ] } }");
589
590 /* Wait for the first serial output from the source */
591 wait_for_serial("src_serial");
592 wait_for_suspend(from, &src_state);
593
594 migrate_qmp(from, to, NULL, NULL, "{}");
595
596 migrate_wait_for_dirty_mem(from, to);
597
598 *from_ptr = from;
599 *to_ptr = to;
600
601 return 0;
602 }
603
604 static void migrate_postcopy_complete(QTestState *from, QTestState *to,
605 void *hook_data, MigrateCommon *args)
606 {
607 MigrationTestEnv *env = migration_get_env();
608
609 wait_for_migration_complete(from);
610
611 if (args->start.suspend_me) {
612 /* wakeup succeeds only if guest is suspended */
613 qtest_qmp_assert_success(to, "{'execute': 'system_wakeup'}");
614 }
615
616 /* Make sure we get at least one "B" on destination */
617 wait_for_serial("dest_serial");
618
619 if (env->uffd_feature_thread_id) {
620 read_blocktime(to);
621 }
622
623 if (args->end_hook) {
624 args->end_hook(from, to, hook_data);
625 }
626
627 migrate_end(from, to, true);
628 }
629
630 void test_postcopy_common(MigrateCommon *args)
631 {
632 void *hook_data = NULL;
633 QTestState *from, *to;
634
635 if (migrate_postcopy_prepare(&from, &to, &hook_data, args)) {
636 return;
637 }
638 migrate_postcopy_start(from, to, &src_state);
639 migrate_postcopy_complete(from, to, hook_data, args);
640 }
641
642 static void wait_for_postcopy_status(QTestState *one, const char *status)
643 {
644 wait_for_migration_status(one, status,
645 (const char * []) {
646 "failed", "active",
647 "completed", NULL
648 });
649 }
650
651 static void postcopy_recover_fail(QTestState *from, QTestState *to,
652 PostcopyRecoveryFailStage stage)
653 {
654 #ifndef _WIN32
655 bool fail_early = (stage == POSTCOPY_FAIL_CHANNEL_ESTABLISH);
656 int ret, pair1[2], pair2[2];
657 char c;
658
659 g_assert(stage > POSTCOPY_FAIL_NONE && stage < POSTCOPY_FAIL_MAX);
660
661 /* Create two unrelated socketpairs */
662 ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair1);
663 g_assert_cmpint(ret, ==, 0);
664
665 ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair2);
666 g_assert_cmpint(ret, ==, 0);
667
668 /*
669 * Give the guests unpaired ends of the sockets, so they'll all blocked
670 * at reading. This mimics a wrong channel established.
671 */
672 qtest_qmp_fds_assert_success(from, &pair1[0], 1,
673 "{ 'execute': 'getfd',"
674 " 'arguments': { 'fdname': 'fd-mig' }}");
675 qtest_qmp_fds_assert_success(to, &pair2[0], 1,
676 "{ 'execute': 'getfd',"
677 " 'arguments': { 'fdname': 'fd-mig' }}");
678
679 /*
680 * Write the 1st byte as QEMU_VM_COMMAND (0x8) for the dest socket, to
681 * emulate the 1st byte of a real recovery, but stops from there to
682 * keep dest QEMU in RECOVER. This is needed so that we can kick off
683 * the recover process on dest QEMU (by triggering the G_IO_IN event).
684 *
685 * NOTE: this trick is not needed on src QEMUs, because src doesn't
686 * rely on an pre-existing G_IO_IN event, so it will always trigger the
687 * upcoming recovery anyway even if it can read nothing.
688 */
689 #define QEMU_VM_COMMAND 0x08
690 c = QEMU_VM_COMMAND;
691 ret = send(pair2[1], &c, 1, 0);
692 g_assert_cmpint(ret, ==, 1);
693
694 if (stage == POSTCOPY_FAIL_CHANNEL_ESTABLISH) {
695 /*
696 * This will make src QEMU to fail at an early stage when trying to
697 * resume later, where it shouldn't reach RECOVER stage at all.
698 */
699 close(pair1[1]);
700 }
701
702 migrate_recover(to, "fd:fd-mig");
703 migrate_qmp(from, to, "fd:fd-mig", NULL, "{'resume': true}");
704
705 /*
706 * Source QEMU has an extra RECOVER_SETUP phase, dest doesn't have it.
707 * Make sure it appears along the way.
708 */
709 migration_event_wait(from, "postcopy-recover-setup");
710
711 if (fail_early) {
712 /*
713 * When fails at reconnection, src QEMU will automatically goes
714 * back to PAUSED state. Making sure there is an event in this
715 * case: Libvirt relies on this to detect early reconnection
716 * errors.
717 */
718 migration_event_wait(from, "postcopy-paused");
719 } else {
720 /*
721 * We want to test "fail later" at RECOVER stage here. Make sure
722 * both QEMU instances will go into RECOVER stage first, then test
723 * kicking them out using migrate-pause.
724 *
725 * Explicitly check the RECOVER event on src, that's what Libvirt
726 * relies on, rather than polling.
727 */
728 migration_event_wait(from, "postcopy-recover");
729 wait_for_postcopy_status(from, "postcopy-recover");
730
731 /* Need an explicit kick on src QEMU in this case */
732 migrate_pause(from);
733 }
734
735 /*
736 * For all failure cases, we'll reach such states on both sides now.
737 * Check them.
738 */
739 wait_for_postcopy_status(from, "postcopy-paused");
740 wait_for_postcopy_status(to, "postcopy-recover");
741
742 /*
743 * Kick dest QEMU out too. This is normally not needed in reality
744 * because when the channel is shutdown it should also happen on src.
745 * However here we used separate socket pairs so we need to do that
746 * explicitly.
747 */
748 migrate_pause(to);
749 wait_for_postcopy_status(to, "postcopy-paused");
750
751 close(pair1[0]);
752 close(pair2[0]);
753 close(pair2[1]);
754
755 if (stage != POSTCOPY_FAIL_CHANNEL_ESTABLISH) {
756 close(pair1[1]);
757 }
758 #endif
759 }
760
761 void test_postcopy_recovery_common(MigrateCommon *args,
762 PostcopyRecoveryFailStage fail_stage)
763 {
764 QTestState *from, *to;
765 g_autofree char *uri = NULL;
766 void *hook_data = NULL;
767
768 /*
769 * Always enable OOB QMP capability for recovery tests, migrate-recover is
770 * executed out-of-band
771 */
772 args->start.oob = true;
773
774 /* Always hide errors for postcopy recover tests since they're expected */
775 args->start.hide_stderr = true;
776
777 if (migrate_postcopy_prepare(&from, &to, &hook_data, args)) {
778 return;
779 }
780
781 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
782 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
783
784 /* Now we start the postcopy */
785 migrate_postcopy_start(from, to, &src_state);
786
787 /*
788 * Wait until postcopy is really started; we can only run the
789 * migrate-pause command during a postcopy
790 */
791 wait_for_migration_status(from, "postcopy-active", NULL);
792
793 /*
794 * Manually stop the postcopy migration. This emulates a network
795 * failure with the migration socket
796 */
797 migrate_pause(from);
798
799 /*
800 * Wait for destination side to reach postcopy-paused state. The
801 * migrate-recover command can only succeed if destination machine
802 * is in the paused state
803 */
804 wait_for_postcopy_status(to, "postcopy-paused");
805 wait_for_postcopy_status(from, "postcopy-paused");
806
807 if (fail_stage) {
808 /*
809 * Test when a wrong socket specified for recover, and then the
810 * ability to kick it out, and continue with a correct socket.
811 */
812 postcopy_recover_fail(from, to, fail_stage);
813 /* continue with a good recovery */
814 }
815
816 /*
817 * Create a new socket to emulate a new channel that is different
818 * from the broken migration channel; tell the destination to
819 * listen to the new port
820 */
821 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
822 migrate_recover(to, uri);
823
824 /*
825 * Try to rebuild the migration channel using the resume flag and
826 * the newly created channel
827 */
828 migrate_qmp(from, to, uri, NULL, "{'resume': true}");
829
830 /* Restore the postcopy bandwidth to unlimited */
831 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
832
833 migrate_postcopy_complete(from, to, hook_data, args);
834 }
835
836 int test_precopy_common(MigrateCommon *args)
837 {
838 QTestState *from, *to;
839 void *data_hook = NULL;
840 QObject *channels = NULL;
841 const char *listen_uri = args->uri ?: "tcp:127.0.0.1:0";
842
843 if (migrate_start(&from, &to, &args->start)) {
844 return -1;
845 }
846
847 if (args->start_hook) {
848 data_hook = args->start_hook(from, to);
849 }
850
851 migrate_incoming_qmp(to, listen_uri, NULL, "{}");
852
853 /* Wait for the first serial output from the source */
854 if (args->result == MIG_TEST_SUCCEED) {
855 wait_for_serial("src_serial");
856 wait_for_suspend(from, &src_state);
857 }
858
859 if (args->live) {
860 migrate_ensure_non_converge(from);
861 migrate_prepare_for_dirty_mem(from);
862 } else {
863 /*
864 * Testing non-live migration, we allow it to run at
865 * full speed to ensure short test case duration.
866 * For tests expected to fail, we don't need to
867 * change anything.
868 */
869 if (args->result == MIG_TEST_SUCCEED) {
870 qtest_qmp_assert_success(from, "{ 'execute' : 'stop'}");
871 wait_for_stop(from, &src_state);
872 migrate_ensure_converge(from);
873 }
874 }
875
876 if (args->connect_channels) {
877 channels = qobject_from_json(args->connect_channels, &error_abort);
878 }
879
880 if (args->result == MIG_TEST_QMP_ERROR) {
881 migrate_qmp_fail(from, args->uri, channels, "{}");
882 goto finish;
883 }
884
885 migrate_qmp(from, to, args->uri, channels, "{}");
886
887 if (args->result != MIG_TEST_SUCCEED) {
888 bool allow_active = args->result == MIG_TEST_FAIL;
889 wait_for_migration_fail(from, allow_active);
890 } else {
891 if (args->live) {
892 /*
893 * For initial iteration(s) we must do a full pass,
894 * but for the final iteration, we need only wait
895 * for some dirty mem before switching to converge
896 */
897 while (args->iterations > 1) {
898 wait_for_migration_pass(from, &src_state);
899 args->iterations--;
900 }
901 migrate_wait_for_dirty_mem(from, to);
902
903 migrate_ensure_converge(from);
904
905 /*
906 * We do this first, as it has a timeout to stop us
907 * hanging forever if migration didn't converge
908 */
909 wait_for_migration_complete(from);
910
911 wait_for_stop(from, &src_state);
912
913 } else {
914 wait_for_migration_complete(from);
915 /*
916 * Must wait for dst to finish reading all incoming
917 * data on the socket before issuing 'cont' otherwise
918 * it'll be ignored
919 */
920 wait_for_migration_complete(to);
921
922 qtest_qmp_assert_success(to, "{ 'execute' : 'cont'}");
923 }
924
925 wait_for_resume(to, &dst_state);
926
927 if (args->start.suspend_me) {
928 /* wakeup succeeds only if guest is suspended */
929 qtest_qmp_assert_success(to, "{'execute': 'system_wakeup'}");
930 }
931
932 wait_for_serial("dest_serial");
933 }
934
935 finish:
936 if (args->end_hook) {
937 args->end_hook(from, to, data_hook);
938 }
939
940 migrate_end(from, to, args->result == MIG_TEST_SUCCEED);
941
942 return 0;
943 }
944
945 void test_precopy_unix_common(MigrateCommon *args)
946 {
947 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
948
949 args->uri = uri;
950 test_precopy_common(args);
951 }
952
953 static void file_dirty_offset_region(void)
954 {
955 g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, FILE_TEST_FILENAME);
956 size_t size = FILE_TEST_OFFSET;
957 g_autofree char *data = g_new0(char, size);
958
959 memset(data, FILE_TEST_MARKER, size);
960 g_assert(g_file_set_contents(path, data, size, NULL));
961 }
962
963 static void file_check_offset_region(void)
964 {
965 g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, FILE_TEST_FILENAME);
966 size_t size = FILE_TEST_OFFSET;
967 g_autofree char *expected = g_new0(char, size);
968 g_autofree char *actual = NULL;
969 uint64_t *stream_start;
970
971 /*
972 * Ensure the skipped offset region's data has not been touched
973 * and the migration stream starts at the right place.
974 */
975
976 memset(expected, FILE_TEST_MARKER, size);
977
978 g_assert(g_file_get_contents(path, &actual, NULL, NULL));
979 g_assert(!memcmp(actual, expected, size));
980
981 stream_start = (uint64_t *)(actual + size);
982 g_assert_cmpint(cpu_to_be64(*stream_start) >> 32, ==, QEMU_VM_FILE_MAGIC);
983 }
984
985 void test_file_common(MigrateCommon *args, bool stop_src)
986 {
987 QTestState *from, *to;
988 void *data_hook = NULL;
989 bool check_offset = false;
990 g_autofree char *uri = NULL;
991
992 if (migrate_start(&from, &to, &args->start)) {
993 return;
994 }
995
996 if (!args->uri) {
997 uri = g_strdup_printf("file:%s/%s", tmpfs, FILE_TEST_FILENAME);
998 args->uri = uri;
999 }
1000
1001 /*
1002 * File migration is never live. We can keep the source VM running
1003 * during migration, but the destination will not be running
1004 * concurrently.
1005 */
1006 g_assert_false(args->live);
1007
1008 if (g_strrstr(args->uri, "offset=")) {
1009 check_offset = true;
1010 /*
1011 * This comes before the start_hook because it's equivalent to
1012 * a management application creating the file and writing to
1013 * it so hooks should expect the file to be already present.
1014 */
1015 file_dirty_offset_region();
1016 }
1017
1018 if (args->start_hook) {
1019 data_hook = args->start_hook(from, to);
1020 }
1021
1022 migrate_ensure_converge(from);
1023 wait_for_serial("src_serial");
1024
1025 if (stop_src) {
1026 qtest_qmp_assert_success(from, "{ 'execute' : 'stop'}");
1027 wait_for_stop(from, &src_state);
1028 }
1029
1030 if (args->result == MIG_TEST_QMP_ERROR) {
1031 migrate_qmp_fail(from, args->uri, NULL, "{}");
1032 goto finish;
1033 }
1034
1035 migrate_qmp(from, to, args->uri, NULL, "{}");
1036 wait_for_migration_complete(from);
1037
1038 /*
1039 * We need to wait for the source to finish before starting the
1040 * destination.
1041 */
1042 migrate_incoming_qmp(to, args->uri, NULL, "{}");
1043 wait_for_migration_complete(to);
1044
1045 if (stop_src) {
1046 qtest_qmp_assert_success(to, "{ 'execute' : 'cont'}");
1047 }
1048 wait_for_resume(to, &dst_state);
1049
1050 wait_for_serial("dest_serial");
1051
1052 if (check_offset) {
1053 file_check_offset_region();
1054 }
1055
1056 finish:
1057 if (args->end_hook) {
1058 args->end_hook(from, to, data_hook);
1059 }
1060
1061 migrate_end(from, to, args->result == MIG_TEST_SUCCEED);
1062 }
1063
1064 QTestMigrationState *get_src(void)
1065 {
1066 return &src_state;
1067 }
1068
1069 QTestMigrationState *get_dst(void)
1070 {
1071 return &dst_state;
1072 }
1073
1074 MigrationTestEnv *migration_get_env(void)
1075 {
1076 static MigrationTestEnv *env;
1077 g_autoptr(GError) err = NULL;
1078
1079 if (env) {
1080 return env;
1081 }
1082
1083 env = g_new0(MigrationTestEnv, 1);
1084 env->qemu_src = getenv(QEMU_ENV_SRC);
1085 env->qemu_dst = getenv(QEMU_ENV_DST);
1086
1087 /*
1088 * The default QTEST_QEMU_BINARY must always be provided because
1089 * that is what helpers use to query the accel type and
1090 * architecture.
1091 */
1092 if (env->qemu_src && env->qemu_dst) {
1093 g_test_message("Only one of %s, %s is allowed",
1094 QEMU_ENV_SRC, QEMU_ENV_DST);
1095 exit(1);
1096 }
1097
1098 env->has_kvm = qtest_has_accel("kvm");
1099 env->has_hvf = qtest_has_accel("hvf");
1100 env->has_tcg = qtest_has_accel("tcg");
1101
1102 if (!env->has_tcg && !env->has_kvm) {
1103 g_test_skip("No KVM or TCG accelerator available");
1104 return env;
1105 }
1106
1107 env->has_dirty_ring = env->has_kvm && kvm_dirty_ring_supported();
1108 env->has_uffd = ufd_version_check(&env->uffd_feature_thread_id);
1109 env->arch = qtest_get_arch();
1110 env->is_x86 = !strcmp(env->arch, "i386") || !strcmp(env->arch, "x86_64");
1111
1112 env->tmpfs = g_dir_make_tmp("migration-test-XXXXXX", &err);
1113 if (!env->tmpfs) {
1114 g_test_message("Can't create temporary directory in %s: %s",
1115 g_get_tmp_dir(), err->message);
1116 }
1117 g_assert(env->tmpfs);
1118
1119 tmpfs = env->tmpfs;
1120
1121 return env;
1122 }
1123
1124 int migration_env_clean(MigrationTestEnv *env)
1125 {
1126 char *tmpfs;
1127 int ret = 0;
1128
1129 if (!env) {
1130 return ret;
1131 }
1132
1133 bootfile_delete();
1134
1135 tmpfs = env->tmpfs;
1136 ret = rmdir(tmpfs);
1137 if (ret != 0) {
1138 g_test_message("unable to rmdir: path (%s): %s",
1139 tmpfs, strerror(errno));
1140 }
1141 g_free(tmpfs);
1142
1143 migration_tests_free();
1144
1145 return ret;
1146 }