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