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 "qobject/qlist.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 */
wait_for_serial(const char * side)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
migrate_prepare_for_dirty_mem(QTestState * from)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
migrate_wait_for_dirty_mem(QTestState * from,QTestState * to)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
check_guests_ram(QTestState * who)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
cleanup(const char * filename)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
migrate_start_get_qmp_capabilities(const MigrateStart * args)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
migrate_start_set_capabilities(QTestState * from,QTestState * to,MigrateStart * args)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 for (uint8_t i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
219 if (!args->caps[i]) {
220 continue;
221 }
222 if (from) {
223 migrate_set_capability(from,
224 MigrationCapability_lookup.array[i], true);
225 }
226 if (to) {
227 migrate_set_capability(to,
228 MigrationCapability_lookup.array[i], true);
229 }
230 }
231
232 /*
233 * Always enable migration events. Libvirt always uses it, let's try
234 * to mimic as closer as that.
235 */
236 migrate_set_capability(from, "events", true);
237 if (!args->defer_target_connect) {
238 migrate_set_capability(to, "events", true);
239 }
240
241 /*
242 * Default number of channels should be fine for most
243 * tests. Individual tests can override by calling
244 * migrate_set_parameter() directly.
245 */
246 if (args->caps[MIGRATION_CAPABILITY_MULTIFD]) {
247 migrate_set_parameter_int(from, "multifd-channels",
248 MULTIFD_TEST_CHANNELS);
249 migrate_set_parameter_int(to, "multifd-channels",
250 MULTIFD_TEST_CHANNELS);
251 }
252
253 return;
254 }
255
migrate_start(QTestState ** from,QTestState ** to,const char * uri,MigrateStart * args)256 int migrate_start(QTestState **from, QTestState **to, const char *uri,
257 MigrateStart *args)
258 {
259 /* options for source and target */
260 g_autofree gchar *arch_opts = NULL;
261 g_autofree gchar *cmd_source = NULL;
262 g_autofree gchar *cmd_target = NULL;
263 const gchar *ignore_stderr;
264 g_autofree char *shmem_opts = NULL;
265 g_autofree char *shmem_path = NULL;
266 const char *kvm_opts = NULL;
267 const char *arch = qtest_get_arch();
268 const char *memory_size;
269 const char *machine_alias, *machine_opts = "";
270 g_autofree char *machine = NULL;
271 const char *bootpath;
272 g_autoptr(QList) capabilities = migrate_start_get_qmp_capabilities(args);
273 g_autofree char *memory_backend = NULL;
274 const char *events;
275
276 if (args->use_shmem) {
277 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
278 g_test_skip("/dev/shm is not supported");
279 return -1;
280 }
281 }
282
283 dst_state = (QTestMigrationState) { };
284 src_state = (QTestMigrationState) { };
285 bootpath = bootfile_create(arch, tmpfs, args->suspend_me);
286 src_state.suspend_me = args->suspend_me;
287
288 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
289 memory_size = "150M";
290
291 if (g_str_equal(arch, "i386")) {
292 machine_alias = "pc";
293 } else {
294 machine_alias = "q35";
295 }
296 arch_opts = g_strdup_printf(
297 "-drive if=none,id=d0,file=%s,format=raw "
298 "-device ide-hd,drive=d0,secs=1,cyls=1,heads=1", bootpath);
299 start_address = X86_TEST_MEM_START;
300 end_address = X86_TEST_MEM_END;
301 } else if (g_str_equal(arch, "s390x")) {
302 memory_size = "128M";
303 machine_alias = "s390-ccw-virtio";
304 arch_opts = g_strdup_printf("-bios %s", bootpath);
305 start_address = S390_TEST_MEM_START;
306 end_address = S390_TEST_MEM_END;
307 } else if (strcmp(arch, "ppc64") == 0) {
308 memory_size = "256M";
309 start_address = PPC_TEST_MEM_START;
310 end_address = PPC_TEST_MEM_END;
311 machine_alias = "pseries";
312 machine_opts = "vsmt=8";
313 arch_opts = g_strdup_printf(
314 "-nodefaults -machine " PSERIES_DEFAULT_CAPABILITIES " "
315 "-bios %s", bootpath);
316 } else if (strcmp(arch, "aarch64") == 0) {
317 memory_size = "150M";
318 machine_alias = "virt";
319 machine_opts = "gic-version=3";
320 arch_opts = g_strdup_printf("-cpu max -kernel %s", bootpath);
321 start_address = ARM_TEST_MEM_START;
322 end_address = ARM_TEST_MEM_END;
323 } else {
324 g_assert_not_reached();
325 }
326
327 if (!getenv("QTEST_LOG") && args->hide_stderr) {
328 #ifndef _WIN32
329 ignore_stderr = "2>/dev/null";
330 #else
331 /*
332 * On Windows the QEMU executable is created via CreateProcess() and
333 * IO redirection does not work, so don't bother adding IO redirection
334 * to the command line.
335 */
336 ignore_stderr = "";
337 #endif
338 } else {
339 ignore_stderr = "";
340 }
341
342 if (args->use_shmem) {
343 shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
344 shmem_opts = g_strdup_printf(
345 "-object memory-backend-file,id=mem0,size=%s"
346 ",mem-path=%s,share=on -numa node,memdev=mem0",
347 memory_size, shmem_path);
348 }
349
350 if (args->memory_backend) {
351 memory_backend = g_strdup_printf(args->memory_backend, memory_size);
352 } else {
353 memory_backend = g_strdup_printf("-m %s ", memory_size);
354 }
355
356 if (args->use_dirty_ring) {
357 kvm_opts = ",dirty-ring-size=4096";
358 }
359
360 if (!qtest_has_machine(machine_alias)) {
361 g_autofree char *msg = g_strdup_printf("machine %s not supported", machine_alias);
362 g_test_skip(msg);
363 return -1;
364 }
365
366 machine = resolve_machine_version(machine_alias, QEMU_ENV_SRC,
367 QEMU_ENV_DST);
368
369 g_test_message("Using machine type: %s", machine);
370
371 cmd_source = g_strdup_printf("-accel kvm%s -accel tcg "
372 "-machine %s,%s "
373 "-name source,debug-threads=on "
374 "%s "
375 "-serial file:%s/src_serial "
376 "%s %s %s %s",
377 kvm_opts ? kvm_opts : "",
378 machine, machine_opts,
379 memory_backend, tmpfs,
380 arch_opts ? arch_opts : "",
381 shmem_opts ? shmem_opts : "",
382 args->opts_source ? args->opts_source : "",
383 ignore_stderr);
384 if (!args->only_target) {
385 *from = qtest_init_ext(QEMU_ENV_SRC, cmd_source, capabilities, true);
386 qtest_qmp_set_event_callback(*from,
387 migrate_watch_for_events,
388 &src_state);
389 }
390
391 /*
392 * If the monitor connection is deferred, enable events on the command line
393 * so none are missed. This is for testing only, do not set migration
394 * options like this in general.
395 */
396 events = args->defer_target_connect ? "-global migration.x-events=on" : "";
397
398 cmd_target = g_strdup_printf("-accel kvm%s -accel tcg "
399 "-machine %s,%s "
400 "-name target,debug-threads=on "
401 "%s "
402 "-serial file:%s/dest_serial "
403 "-incoming %s "
404 "%s %s %s %s %s",
405 kvm_opts ? kvm_opts : "",
406 machine, machine_opts,
407 memory_backend, tmpfs, uri,
408 events,
409 arch_opts ? arch_opts : "",
410 shmem_opts ? shmem_opts : "",
411 args->opts_target ? args->opts_target : "",
412 ignore_stderr);
413 *to = qtest_init_ext(QEMU_ENV_DST, cmd_target, capabilities,
414 !args->defer_target_connect);
415 qtest_qmp_set_event_callback(*to,
416 migrate_watch_for_events,
417 &dst_state);
418
419 /*
420 * Remove shmem file immediately to avoid memory leak in test failed case.
421 * It's valid because QEMU has already opened this file
422 */
423 if (args->use_shmem) {
424 unlink(shmem_path);
425 }
426
427 migrate_start_set_capabilities(*from, *to, args);
428
429 return 0;
430 }
431
migrate_end(QTestState * from,QTestState * to,bool test_dest)432 void migrate_end(QTestState *from, QTestState *to, bool test_dest)
433 {
434 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
435
436 qtest_quit(from);
437
438 if (test_dest) {
439 qtest_memread(to, start_address, &dest_byte_a, 1);
440
441 /* Destination still running, wait for a byte to change */
442 do {
443 qtest_memread(to, start_address, &dest_byte_b, 1);
444 usleep(1000 * 10);
445 } while (dest_byte_a == dest_byte_b);
446
447 qtest_qmp_assert_success(to, "{ 'execute' : 'stop'}");
448
449 /* With it stopped, check nothing changes */
450 qtest_memread(to, start_address, &dest_byte_c, 1);
451 usleep(1000 * 200);
452 qtest_memread(to, start_address, &dest_byte_d, 1);
453 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
454
455 check_guests_ram(to);
456 }
457
458 qtest_quit(to);
459
460 cleanup("migsocket");
461 cleanup("cpr.sock");
462 cleanup("src_serial");
463 cleanup("dest_serial");
464 cleanup(FILE_TEST_FILENAME);
465 }
466
migrate_postcopy_prepare(QTestState ** from_ptr,QTestState ** to_ptr,MigrateCommon * args)467 static int migrate_postcopy_prepare(QTestState **from_ptr,
468 QTestState **to_ptr,
469 MigrateCommon *args)
470 {
471 QTestState *from, *to;
472
473 /* set postcopy capabilities */
474 args->start.caps[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME] = true;
475 args->start.caps[MIGRATION_CAPABILITY_POSTCOPY_RAM] = true;
476
477 if (migrate_start(&from, &to, "defer", &args->start)) {
478 return -1;
479 }
480
481 if (args->start_hook) {
482 args->postcopy_data = args->start_hook(from, to);
483 }
484
485 migrate_ensure_non_converge(from);
486 migrate_prepare_for_dirty_mem(from);
487 qtest_qmp_assert_success(to, "{ 'execute': 'migrate-incoming',"
488 " 'arguments': { "
489 " 'channels': [ { 'channel-type': 'main',"
490 " 'addr': { 'transport': 'socket',"
491 " 'type': 'inet',"
492 " 'host': '127.0.0.1',"
493 " 'port': '0' } } ] } }");
494
495 /* Wait for the first serial output from the source */
496 wait_for_serial("src_serial");
497 wait_for_suspend(from, &src_state);
498
499 migrate_qmp(from, to, NULL, NULL, "{}");
500
501 migrate_wait_for_dirty_mem(from, to);
502
503 *from_ptr = from;
504 *to_ptr = to;
505
506 return 0;
507 }
508
migrate_postcopy_complete(QTestState * from,QTestState * to,MigrateCommon * args)509 static void migrate_postcopy_complete(QTestState *from, QTestState *to,
510 MigrateCommon *args)
511 {
512 MigrationTestEnv *env = migration_get_env();
513
514 wait_for_migration_complete(from);
515
516 if (args->start.suspend_me) {
517 /* wakeup succeeds only if guest is suspended */
518 qtest_qmp_assert_success(to, "{'execute': 'system_wakeup'}");
519 }
520
521 /* Make sure we get at least one "B" on destination */
522 wait_for_serial("dest_serial");
523
524 if (env->uffd_feature_thread_id) {
525 read_blocktime(to);
526 }
527
528 if (args->end_hook) {
529 args->end_hook(from, to, args->postcopy_data);
530 args->postcopy_data = NULL;
531 }
532
533 migrate_end(from, to, true);
534 }
535
test_postcopy_common(MigrateCommon * args)536 void test_postcopy_common(MigrateCommon *args)
537 {
538 QTestState *from, *to;
539
540 if (migrate_postcopy_prepare(&from, &to, args)) {
541 return;
542 }
543 migrate_postcopy_start(from, to, &src_state);
544 migrate_postcopy_complete(from, to, args);
545 }
546
wait_for_postcopy_status(QTestState * one,const char * status)547 static void wait_for_postcopy_status(QTestState *one, const char *status)
548 {
549 wait_for_migration_status(one, status,
550 (const char * []) {
551 "failed", "active",
552 "completed", NULL
553 });
554 }
555
postcopy_recover_fail(QTestState * from,QTestState * to,PostcopyRecoveryFailStage stage)556 static void postcopy_recover_fail(QTestState *from, QTestState *to,
557 PostcopyRecoveryFailStage stage)
558 {
559 #ifndef _WIN32
560 bool fail_early = (stage == POSTCOPY_FAIL_CHANNEL_ESTABLISH);
561 int ret, pair1[2], pair2[2];
562 char c;
563
564 g_assert(stage > POSTCOPY_FAIL_NONE && stage < POSTCOPY_FAIL_MAX);
565
566 /* Create two unrelated socketpairs */
567 ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair1);
568 g_assert_cmpint(ret, ==, 0);
569
570 ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair2);
571 g_assert_cmpint(ret, ==, 0);
572
573 /*
574 * Give the guests unpaired ends of the sockets, so they'll all blocked
575 * at reading. This mimics a wrong channel established.
576 */
577 qtest_qmp_fds_assert_success(from, &pair1[0], 1,
578 "{ 'execute': 'getfd',"
579 " 'arguments': { 'fdname': 'fd-mig' }}");
580 qtest_qmp_fds_assert_success(to, &pair2[0], 1,
581 "{ 'execute': 'getfd',"
582 " 'arguments': { 'fdname': 'fd-mig' }}");
583
584 /*
585 * Write the 1st byte as QEMU_VM_COMMAND (0x8) for the dest socket, to
586 * emulate the 1st byte of a real recovery, but stops from there to
587 * keep dest QEMU in RECOVER. This is needed so that we can kick off
588 * the recover process on dest QEMU (by triggering the G_IO_IN event).
589 *
590 * NOTE: this trick is not needed on src QEMUs, because src doesn't
591 * rely on an pre-existing G_IO_IN event, so it will always trigger the
592 * upcoming recovery anyway even if it can read nothing.
593 */
594 #define QEMU_VM_COMMAND 0x08
595 c = QEMU_VM_COMMAND;
596 ret = send(pair2[1], &c, 1, 0);
597 g_assert_cmpint(ret, ==, 1);
598
599 if (stage == POSTCOPY_FAIL_CHANNEL_ESTABLISH) {
600 /*
601 * This will make src QEMU to fail at an early stage when trying to
602 * resume later, where it shouldn't reach RECOVER stage at all.
603 */
604 close(pair1[1]);
605 }
606
607 migrate_recover(to, "fd:fd-mig");
608 migrate_qmp(from, to, "fd:fd-mig", NULL, "{'resume': true}");
609
610 /*
611 * Source QEMU has an extra RECOVER_SETUP phase, dest doesn't have it.
612 * Make sure it appears along the way.
613 */
614 migration_event_wait(from, "postcopy-recover-setup");
615
616 if (fail_early) {
617 /*
618 * When fails at reconnection, src QEMU will automatically goes
619 * back to PAUSED state. Making sure there is an event in this
620 * case: Libvirt relies on this to detect early reconnection
621 * errors.
622 */
623 migration_event_wait(from, "postcopy-paused");
624 } else {
625 /*
626 * We want to test "fail later" at RECOVER stage here. Make sure
627 * both QEMU instances will go into RECOVER stage first, then test
628 * kicking them out using migrate-pause.
629 *
630 * Explicitly check the RECOVER event on src, that's what Libvirt
631 * relies on, rather than polling.
632 */
633 migration_event_wait(from, "postcopy-recover");
634 wait_for_postcopy_status(from, "postcopy-recover");
635
636 /* Need an explicit kick on src QEMU in this case */
637 migrate_pause(from);
638 }
639
640 /*
641 * For all failure cases, we'll reach such states on both sides now.
642 * Check them.
643 */
644 wait_for_postcopy_status(from, "postcopy-paused");
645 wait_for_postcopy_status(to, "postcopy-recover");
646
647 /*
648 * Kick dest QEMU out too. This is normally not needed in reality
649 * because when the channel is shutdown it should also happen on src.
650 * However here we used separate socket pairs so we need to do that
651 * explicitly.
652 */
653 migrate_pause(to);
654 wait_for_postcopy_status(to, "postcopy-paused");
655
656 close(pair1[0]);
657 close(pair2[0]);
658 close(pair2[1]);
659
660 if (stage != POSTCOPY_FAIL_CHANNEL_ESTABLISH) {
661 close(pair1[1]);
662 }
663 #endif
664 }
665
test_postcopy_recovery_common(MigrateCommon * args)666 void test_postcopy_recovery_common(MigrateCommon *args)
667 {
668 QTestState *from, *to;
669 g_autofree char *uri = NULL;
670
671 /*
672 * Always enable OOB QMP capability for recovery tests, migrate-recover is
673 * executed out-of-band
674 */
675 args->start.oob = true;
676
677 /* Always hide errors for postcopy recover tests since they're expected */
678 args->start.hide_stderr = true;
679
680 if (migrate_postcopy_prepare(&from, &to, args)) {
681 return;
682 }
683
684 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
685 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
686
687 /* Now we start the postcopy */
688 migrate_postcopy_start(from, to, &src_state);
689
690 /*
691 * Wait until postcopy is really started; we can only run the
692 * migrate-pause command during a postcopy
693 */
694 wait_for_migration_status(from, "postcopy-active", NULL);
695
696 /*
697 * Manually stop the postcopy migration. This emulates a network
698 * failure with the migration socket
699 */
700 migrate_pause(from);
701
702 /*
703 * Wait for destination side to reach postcopy-paused state. The
704 * migrate-recover command can only succeed if destination machine
705 * is in the paused state
706 */
707 wait_for_postcopy_status(to, "postcopy-paused");
708 wait_for_postcopy_status(from, "postcopy-paused");
709
710 if (args->postcopy_recovery_fail_stage) {
711 /*
712 * Test when a wrong socket specified for recover, and then the
713 * ability to kick it out, and continue with a correct socket.
714 */
715 postcopy_recover_fail(from, to, args->postcopy_recovery_fail_stage);
716 /* continue with a good recovery */
717 }
718
719 /*
720 * Create a new socket to emulate a new channel that is different
721 * from the broken migration channel; tell the destination to
722 * listen to the new port
723 */
724 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
725 migrate_recover(to, uri);
726
727 /*
728 * Try to rebuild the migration channel using the resume flag and
729 * the newly created channel
730 */
731 migrate_qmp(from, to, uri, NULL, "{'resume': true}");
732
733 /* Restore the postcopy bandwidth to unlimited */
734 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
735
736 migrate_postcopy_complete(from, to, args);
737 }
738
test_precopy_common(MigrateCommon * args)739 void test_precopy_common(MigrateCommon *args)
740 {
741 QTestState *from, *to;
742 void *data_hook = NULL;
743 QObject *in_channels = NULL;
744 QObject *out_channels = NULL;
745
746 g_assert(!args->cpr_channel || args->connect_channels);
747
748 if (migrate_start(&from, &to, args->listen_uri, &args->start)) {
749 return;
750 }
751
752 if (args->start_hook) {
753 data_hook = args->start_hook(from, to);
754 }
755
756 /* Wait for the first serial output from the source */
757 if (args->result == MIG_TEST_SUCCEED) {
758 wait_for_serial("src_serial");
759 wait_for_suspend(from, &src_state);
760 }
761
762 if (args->live) {
763 migrate_ensure_non_converge(from);
764 migrate_prepare_for_dirty_mem(from);
765 } else {
766 /*
767 * Testing non-live migration, we allow it to run at
768 * full speed to ensure short test case duration.
769 * For tests expected to fail, we don't need to
770 * change anything.
771 */
772 if (args->result == MIG_TEST_SUCCEED) {
773 qtest_qmp_assert_success(from, "{ 'execute' : 'stop'}");
774 wait_for_stop(from, &src_state);
775 migrate_ensure_converge(from);
776 }
777 }
778
779 /*
780 * The cpr channel must be included in outgoing channels, but not in
781 * migrate-incoming channels.
782 */
783 if (args->connect_channels) {
784 if (args->start.defer_target_connect &&
785 !strcmp(args->listen_uri, "defer")) {
786 in_channels = qobject_from_json(args->connect_channels,
787 &error_abort);
788 }
789 out_channels = qobject_from_json(args->connect_channels, &error_abort);
790
791 if (args->cpr_channel) {
792 QList *channels_list = qobject_to(QList, out_channels);
793 QObject *obj = migrate_str_to_channel(args->cpr_channel);
794
795 qlist_append(channels_list, obj);
796 }
797 }
798
799 if (args->result == MIG_TEST_QMP_ERROR) {
800 migrate_qmp_fail(from, args->connect_uri, out_channels, "{}");
801 goto finish;
802 }
803
804 migrate_qmp(from, to, args->connect_uri, out_channels, "{}");
805
806 if (args->start.defer_target_connect) {
807 qtest_connect(to);
808 qtest_qmp_handshake(to, NULL);
809 if (!strcmp(args->listen_uri, "defer")) {
810 migrate_incoming_qmp(to, args->connect_uri, in_channels, "{}");
811 }
812 }
813
814 if (args->result != MIG_TEST_SUCCEED) {
815 bool allow_active = args->result == MIG_TEST_FAIL;
816 wait_for_migration_fail(from, allow_active);
817
818 if (args->result == MIG_TEST_FAIL_DEST_QUIT_ERR) {
819 qtest_set_expected_status(to, EXIT_FAILURE);
820 }
821 } else {
822 if (args->live) {
823 /*
824 * For initial iteration(s) we must do a full pass,
825 * but for the final iteration, we need only wait
826 * for some dirty mem before switching to converge
827 */
828 while (args->iterations > 1) {
829 wait_for_migration_pass(from, &src_state);
830 args->iterations--;
831 }
832 migrate_wait_for_dirty_mem(from, to);
833
834 migrate_ensure_converge(from);
835
836 /*
837 * We do this first, as it has a timeout to stop us
838 * hanging forever if migration didn't converge
839 */
840 wait_for_migration_complete(from);
841
842 wait_for_stop(from, &src_state);
843
844 } else {
845 wait_for_migration_complete(from);
846 /*
847 * Must wait for dst to finish reading all incoming
848 * data on the socket before issuing 'cont' otherwise
849 * it'll be ignored
850 */
851 wait_for_migration_complete(to);
852
853 qtest_qmp_assert_success(to, "{ 'execute' : 'cont'}");
854 }
855
856 wait_for_resume(to, &dst_state);
857
858 if (args->start.suspend_me) {
859 /* wakeup succeeds only if guest is suspended */
860 qtest_qmp_assert_success(to, "{'execute': 'system_wakeup'}");
861 }
862
863 wait_for_serial("dest_serial");
864 }
865
866 finish:
867 if (args->end_hook) {
868 args->end_hook(from, to, data_hook);
869 }
870
871 migrate_end(from, to, args->result == MIG_TEST_SUCCEED);
872 }
873
file_dirty_offset_region(void)874 static void file_dirty_offset_region(void)
875 {
876 g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, FILE_TEST_FILENAME);
877 size_t size = FILE_TEST_OFFSET;
878 g_autofree char *data = g_new0(char, size);
879
880 memset(data, FILE_TEST_MARKER, size);
881 g_assert(g_file_set_contents(path, data, size, NULL));
882 }
883
file_check_offset_region(void)884 static void file_check_offset_region(void)
885 {
886 g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, FILE_TEST_FILENAME);
887 size_t size = FILE_TEST_OFFSET;
888 g_autofree char *expected = g_new0(char, size);
889 g_autofree char *actual = NULL;
890 uint64_t *stream_start;
891
892 /*
893 * Ensure the skipped offset region's data has not been touched
894 * and the migration stream starts at the right place.
895 */
896
897 memset(expected, FILE_TEST_MARKER, size);
898
899 g_assert(g_file_get_contents(path, &actual, NULL, NULL));
900 g_assert(!memcmp(actual, expected, size));
901
902 stream_start = (uint64_t *)(actual + size);
903 g_assert_cmpint(cpu_to_be64(*stream_start) >> 32, ==, QEMU_VM_FILE_MAGIC);
904 }
905
test_file_common(MigrateCommon * args,bool stop_src)906 void test_file_common(MigrateCommon *args, bool stop_src)
907 {
908 QTestState *from, *to;
909 void *data_hook = NULL;
910 bool check_offset = false;
911
912 if (migrate_start(&from, &to, args->listen_uri, &args->start)) {
913 return;
914 }
915
916 /*
917 * File migration is never live. We can keep the source VM running
918 * during migration, but the destination will not be running
919 * concurrently.
920 */
921 g_assert_false(args->live);
922
923 if (g_strrstr(args->connect_uri, "offset=")) {
924 check_offset = true;
925 /*
926 * This comes before the start_hook because it's equivalent to
927 * a management application creating the file and writing to
928 * it so hooks should expect the file to be already present.
929 */
930 file_dirty_offset_region();
931 }
932
933 if (args->start_hook) {
934 data_hook = args->start_hook(from, to);
935 }
936
937 migrate_ensure_converge(from);
938 wait_for_serial("src_serial");
939
940 if (stop_src) {
941 qtest_qmp_assert_success(from, "{ 'execute' : 'stop'}");
942 wait_for_stop(from, &src_state);
943 }
944
945 if (args->result == MIG_TEST_QMP_ERROR) {
946 migrate_qmp_fail(from, args->connect_uri, NULL, "{}");
947 goto finish;
948 }
949
950 migrate_qmp(from, to, args->connect_uri, NULL, "{}");
951 wait_for_migration_complete(from);
952
953 /*
954 * We need to wait for the source to finish before starting the
955 * destination.
956 */
957 migrate_incoming_qmp(to, args->connect_uri, NULL, "{}");
958 wait_for_migration_complete(to);
959
960 if (stop_src) {
961 qtest_qmp_assert_success(to, "{ 'execute' : 'cont'}");
962 }
963 wait_for_resume(to, &dst_state);
964
965 wait_for_serial("dest_serial");
966
967 if (check_offset) {
968 file_check_offset_region();
969 }
970
971 finish:
972 if (args->end_hook) {
973 args->end_hook(from, to, data_hook);
974 }
975
976 migrate_end(from, to, args->result == MIG_TEST_SUCCEED);
977 }
978
migrate_hook_start_precopy_tcp_multifd_common(QTestState * from,QTestState * to,const char * method)979 void *migrate_hook_start_precopy_tcp_multifd_common(QTestState *from,
980 QTestState *to,
981 const char *method)
982 {
983 migrate_set_parameter_str(from, "multifd-compression", method);
984 migrate_set_parameter_str(to, "multifd-compression", method);
985
986 /* Start incoming migration from the 1st socket */
987 migrate_incoming_qmp(to, "tcp:127.0.0.1:0", NULL, "{}");
988
989 return NULL;
990 }
991
get_src(void)992 QTestMigrationState *get_src(void)
993 {
994 return &src_state;
995 }
996
migration_get_env(void)997 MigrationTestEnv *migration_get_env(void)
998 {
999 static MigrationTestEnv *env;
1000 g_autoptr(GError) err = NULL;
1001
1002 if (env) {
1003 return env;
1004 }
1005
1006 env = g_new0(MigrationTestEnv, 1);
1007 env->qemu_src = getenv(QEMU_ENV_SRC);
1008 env->qemu_dst = getenv(QEMU_ENV_DST);
1009
1010 /*
1011 * The default QTEST_QEMU_BINARY must always be provided because
1012 * that is what helpers use to query the accel type and
1013 * architecture.
1014 */
1015 if (env->qemu_src && env->qemu_dst) {
1016 g_test_message("Only one of %s, %s is allowed",
1017 QEMU_ENV_SRC, QEMU_ENV_DST);
1018 exit(1);
1019 }
1020
1021 env->has_kvm = qtest_has_accel("kvm");
1022 env->has_tcg = qtest_has_accel("tcg");
1023
1024 if (!env->has_tcg && !env->has_kvm) {
1025 g_test_skip("No KVM or TCG accelerator available");
1026 return env;
1027 }
1028
1029 env->has_dirty_ring = kvm_dirty_ring_supported();
1030 env->has_uffd = ufd_version_check(&env->uffd_feature_thread_id);
1031 env->arch = qtest_get_arch();
1032 env->is_x86 = !strcmp(env->arch, "i386") || !strcmp(env->arch, "x86_64");
1033
1034 env->tmpfs = g_dir_make_tmp("migration-test-XXXXXX", &err);
1035 if (!env->tmpfs) {
1036 g_test_message("Can't create temporary directory in %s: %s",
1037 g_get_tmp_dir(), err->message);
1038 }
1039 g_assert(env->tmpfs);
1040
1041 tmpfs = env->tmpfs;
1042
1043 return env;
1044 }
1045
migration_env_clean(MigrationTestEnv * env)1046 int migration_env_clean(MigrationTestEnv *env)
1047 {
1048 char *tmpfs;
1049 int ret = 0;
1050
1051 if (!env) {
1052 return ret;
1053 }
1054
1055 bootfile_delete();
1056
1057 tmpfs = env->tmpfs;
1058 ret = rmdir(tmpfs);
1059 if (ret != 0) {
1060 g_test_message("unable to rmdir: path (%s): %s",
1061 tmpfs, strerror(errno));
1062 }
1063 g_free(tmpfs);
1064
1065 return ret;
1066 }
1067