xref: /qemu/tests/qtest/migration-test.c (revision 212c19331b0c53ab299ae3d646409fad2da90602)
1 /*
2  * QTest testcase for migration
3  *
4  * Copyright (c) 2016-2018 Red Hat, Inc. and/or its affiliates
5  *   based on the vhost-user-test.c that is:
6  *      Copyright (c) 2014 Virtual Open Systems Sarl.
7  *
8  * This work is licensed under the terms of the GNU GPL, version 2 or later.
9  * See the COPYING file in the top-level directory.
10  *
11  */
12 
13 #include "qemu/osdep.h"
14 
15 #include "libqtest.h"
16 #include "qapi/qmp/qdict.h"
17 #include "qemu/module.h"
18 #include "qemu/option.h"
19 #include "qemu/range.h"
20 #include "qemu/sockets.h"
21 #include "chardev/char.h"
22 #include "crypto/tlscredspsk.h"
23 #include "qapi/qmp/qlist.h"
24 #include "ppc-util.h"
25 
26 #include "migration-helpers.h"
27 #include "migration/migration-test.h"
28 #ifdef CONFIG_GNUTLS
29 # include "tests/unit/crypto-tls-psk-helpers.h"
30 # ifdef CONFIG_TASN1
31 #  include "tests/unit/crypto-tls-x509-helpers.h"
32 # endif /* CONFIG_TASN1 */
33 #endif /* CONFIG_GNUTLS */
34 
35 /* For dirty ring test; so far only x86_64 is supported */
36 #if defined(__linux__) && defined(HOST_X86_64)
37 #include "linux/kvm.h"
38 #endif
39 
40 unsigned start_address;
41 unsigned end_address;
42 static bool uffd_feature_thread_id;
43 static QTestMigrationState src_state;
44 static QTestMigrationState dst_state;
45 
46 /*
47  * An initial 3 MB offset is used as that corresponds
48  * to ~1 sec of data transfer with our bandwidth setting.
49  */
50 #define MAGIC_OFFSET_BASE (3 * 1024 * 1024)
51 /*
52  * A further 1k is added to ensure we're not a multiple
53  * of TEST_MEM_PAGE_SIZE, thus avoid clash with writes
54  * from the migration guest workload.
55  */
56 #define MAGIC_OFFSET_SHUFFLE 1024
57 #define MAGIC_OFFSET (MAGIC_OFFSET_BASE + MAGIC_OFFSET_SHUFFLE)
58 #define MAGIC_MARKER 0xFEED12345678CAFEULL
59 
60 /*
61  * Dirtylimit stop working if dirty page rate error
62  * value less than DIRTYLIMIT_TOLERANCE_RANGE
63  */
64 #define DIRTYLIMIT_TOLERANCE_RANGE  25  /* MB/s */
65 
66 #define ANALYZE_SCRIPT "scripts/analyze-migration.py"
67 
68 #define QEMU_VM_FILE_MAGIC 0x5145564d
69 #define FILE_TEST_FILENAME "migfile"
70 #define FILE_TEST_OFFSET 0x1000
71 #define FILE_TEST_MARKER 'X'
72 #define QEMU_ENV_SRC "QTEST_QEMU_BINARY_SRC"
73 #define QEMU_ENV_DST "QTEST_QEMU_BINARY_DST"
74 
75 typedef enum PostcopyRecoveryFailStage {
76     /*
77      * "no failure" must be 0 as it's the default.  OTOH, real failure
78      * cases must be >0 to make sure they trigger by a "if" test.
79      */
80     POSTCOPY_FAIL_NONE = 0,
81     POSTCOPY_FAIL_CHANNEL_ESTABLISH,
82     POSTCOPY_FAIL_RECOVERY,
83     POSTCOPY_FAIL_MAX
84 } PostcopyRecoveryFailStage;
85 
86 #if defined(__linux__)
87 #include <sys/syscall.h>
88 #include <sys/vfs.h>
89 #endif
90 
91 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
92 #include <sys/eventfd.h>
93 #include <sys/ioctl.h>
94 #include "qemu/userfaultfd.h"
95 
96 static bool ufd_version_check(void)
97 {
98     struct uffdio_api api_struct;
99     uint64_t ioctl_mask;
100 
101     int ufd = uffd_open(O_CLOEXEC);
102 
103     if (ufd == -1) {
104         g_test_message("Skipping test: userfaultfd not available");
105         return false;
106     }
107 
108     api_struct.api = UFFD_API;
109     api_struct.features = 0;
110     if (ioctl(ufd, UFFDIO_API, &api_struct)) {
111         g_test_message("Skipping test: UFFDIO_API failed");
112         return false;
113     }
114     uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
115 
116     ioctl_mask = (1ULL << _UFFDIO_REGISTER |
117                   1ULL << _UFFDIO_UNREGISTER);
118     if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
119         g_test_message("Skipping test: Missing userfault feature");
120         return false;
121     }
122 
123     return true;
124 }
125 
126 #else
127 static bool ufd_version_check(void)
128 {
129     g_test_message("Skipping test: Userfault not available (builtdtime)");
130     return false;
131 }
132 
133 #endif
134 
135 static char *tmpfs;
136 static char *bootpath;
137 
138 /* The boot file modifies memory area in [start_address, end_address)
139  * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
140  */
141 #include "migration/i386/a-b-bootblock.h"
142 #include "migration/aarch64/a-b-kernel.h"
143 #include "migration/ppc64/a-b-kernel.h"
144 #include "migration/s390x/a-b-bios.h"
145 
146 static void bootfile_delete(void)
147 {
148     if (!bootpath) {
149         return;
150     }
151     unlink(bootpath);
152     g_free(bootpath);
153     bootpath = NULL;
154 }
155 
156 static void bootfile_create(char *dir, bool suspend_me)
157 {
158     const char *arch = qtest_get_arch();
159     unsigned char *content;
160     size_t len;
161 
162     bootfile_delete();
163     bootpath = g_strdup_printf("%s/bootsect", dir);
164     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
165         /* the assembled x86 boot sector should be exactly one sector large */
166         g_assert(sizeof(x86_bootsect) == 512);
167         x86_bootsect[SYM_suspend_me - SYM_start] = suspend_me;
168         content = x86_bootsect;
169         len = sizeof(x86_bootsect);
170     } else if (g_str_equal(arch, "s390x")) {
171         content = s390x_elf;
172         len = sizeof(s390x_elf);
173     } else if (strcmp(arch, "ppc64") == 0) {
174         content = ppc64_kernel;
175         len = sizeof(ppc64_kernel);
176     } else if (strcmp(arch, "aarch64") == 0) {
177         content = aarch64_kernel;
178         len = sizeof(aarch64_kernel);
179         g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
180     } else {
181         g_assert_not_reached();
182     }
183 
184     FILE *bootfile = fopen(bootpath, "wb");
185 
186     g_assert_cmpint(fwrite(content, len, 1, bootfile), ==, 1);
187     fclose(bootfile);
188 }
189 
190 /*
191  * Wait for some output in the serial output file,
192  * we get an 'A' followed by an endless string of 'B's
193  * but on the destination we won't have the A (unless we enabled suspend/resume)
194  */
195 static void wait_for_serial(const char *side)
196 {
197     g_autofree char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
198     FILE *serialfile = fopen(serialpath, "r");
199 
200     do {
201         int readvalue = fgetc(serialfile);
202 
203         switch (readvalue) {
204         case 'A':
205             /* Fine */
206             break;
207 
208         case 'B':
209             /* It's alive! */
210             fclose(serialfile);
211             return;
212 
213         case EOF:
214             fseek(serialfile, 0, SEEK_SET);
215             usleep(1000);
216             break;
217 
218         default:
219             fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
220             g_assert_not_reached();
221         }
222     } while (true);
223 }
224 
225 static void wait_for_stop(QTestState *who, QTestMigrationState *state)
226 {
227     if (!state->stop_seen) {
228         qtest_qmp_eventwait(who, "STOP");
229     }
230 }
231 
232 static void wait_for_resume(QTestState *who, QTestMigrationState *state)
233 {
234     if (!state->resume_seen) {
235         qtest_qmp_eventwait(who, "RESUME");
236     }
237 }
238 
239 static void wait_for_suspend(QTestState *who, QTestMigrationState *state)
240 {
241     if (state->suspend_me && !state->suspend_seen) {
242         qtest_qmp_eventwait(who, "SUSPEND");
243     }
244 }
245 
246 /*
247  * It's tricky to use qemu's migration event capability with qtest,
248  * events suddenly appearing confuse the qmp()/hmp() responses.
249  */
250 
251 static int64_t read_ram_property_int(QTestState *who, const char *property)
252 {
253     QDict *rsp_return, *rsp_ram;
254     int64_t result;
255 
256     rsp_return = migrate_query_not_failed(who);
257     if (!qdict_haskey(rsp_return, "ram")) {
258         /* Still in setup */
259         result = 0;
260     } else {
261         rsp_ram = qdict_get_qdict(rsp_return, "ram");
262         result = qdict_get_try_int(rsp_ram, property, 0);
263     }
264     qobject_unref(rsp_return);
265     return result;
266 }
267 
268 static int64_t read_migrate_property_int(QTestState *who, const char *property)
269 {
270     QDict *rsp_return;
271     int64_t result;
272 
273     rsp_return = migrate_query_not_failed(who);
274     result = qdict_get_try_int(rsp_return, property, 0);
275     qobject_unref(rsp_return);
276     return result;
277 }
278 
279 static uint64_t get_migration_pass(QTestState *who)
280 {
281     return read_ram_property_int(who, "dirty-sync-count");
282 }
283 
284 static void read_blocktime(QTestState *who)
285 {
286     QDict *rsp_return;
287 
288     rsp_return = migrate_query_not_failed(who);
289     g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
290     qobject_unref(rsp_return);
291 }
292 
293 /*
294  * Wait for two changes in the migration pass count, but bail if we stop.
295  */
296 static void wait_for_migration_pass(QTestState *who)
297 {
298     uint64_t pass, prev_pass = 0, changes = 0;
299 
300     while (changes < 2 && !src_state.stop_seen && !src_state.suspend_seen) {
301         usleep(1000);
302         pass = get_migration_pass(who);
303         changes += (pass != prev_pass);
304         prev_pass = pass;
305     }
306 }
307 
308 static void check_guests_ram(QTestState *who)
309 {
310     /* Our ASM test will have been incrementing one byte from each page from
311      * start_address to < end_address in order. This gives us a constraint
312      * that any page's byte should be equal or less than the previous pages
313      * byte (mod 256); and they should all be equal except for one transition
314      * at the point where we meet the incrementer. (We're running this with
315      * the guest stopped).
316      */
317     unsigned address;
318     uint8_t first_byte;
319     uint8_t last_byte;
320     bool hit_edge = false;
321     int bad = 0;
322 
323     qtest_memread(who, start_address, &first_byte, 1);
324     last_byte = first_byte;
325 
326     for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
327          address += TEST_MEM_PAGE_SIZE)
328     {
329         uint8_t b;
330         qtest_memread(who, address, &b, 1);
331         if (b != last_byte) {
332             if (((b + 1) % 256) == last_byte && !hit_edge) {
333                 /* This is OK, the guest stopped at the point of
334                  * incrementing the previous page but didn't get
335                  * to us yet.
336                  */
337                 hit_edge = true;
338                 last_byte = b;
339             } else {
340                 bad++;
341                 if (bad <= 10) {
342                     fprintf(stderr, "Memory content inconsistency at %x"
343                             " first_byte = %x last_byte = %x current = %x"
344                             " hit_edge = %x\n",
345                             address, first_byte, last_byte, b, hit_edge);
346                 }
347             }
348         }
349     }
350     if (bad >= 10) {
351         fprintf(stderr, "and in another %d pages", bad - 10);
352     }
353     g_assert(bad == 0);
354 }
355 
356 static void cleanup(const char *filename)
357 {
358     g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, filename);
359 
360     unlink(path);
361 }
362 
363 static long long migrate_get_parameter_int(QTestState *who,
364                                            const char *parameter)
365 {
366     QDict *rsp;
367     long long result;
368 
369     rsp = qtest_qmp_assert_success_ref(
370         who, "{ 'execute': 'query-migrate-parameters' }");
371     result = qdict_get_int(rsp, parameter);
372     qobject_unref(rsp);
373     return result;
374 }
375 
376 static void migrate_check_parameter_int(QTestState *who, const char *parameter,
377                                         long long value)
378 {
379     long long result;
380 
381     result = migrate_get_parameter_int(who, parameter);
382     g_assert_cmpint(result, ==, value);
383 }
384 
385 static void migrate_set_parameter_int(QTestState *who, const char *parameter,
386                                       long long value)
387 {
388     qtest_qmp_assert_success(who,
389                              "{ 'execute': 'migrate-set-parameters',"
390                              "'arguments': { %s: %lld } }",
391                              parameter, value);
392     migrate_check_parameter_int(who, parameter, value);
393 }
394 
395 static char *migrate_get_parameter_str(QTestState *who,
396                                        const char *parameter)
397 {
398     QDict *rsp;
399     char *result;
400 
401     rsp = qtest_qmp_assert_success_ref(
402         who, "{ 'execute': 'query-migrate-parameters' }");
403     result = g_strdup(qdict_get_str(rsp, parameter));
404     qobject_unref(rsp);
405     return result;
406 }
407 
408 static void migrate_check_parameter_str(QTestState *who, const char *parameter,
409                                         const char *value)
410 {
411     g_autofree char *result = migrate_get_parameter_str(who, parameter);
412     g_assert_cmpstr(result, ==, value);
413 }
414 
415 static void migrate_set_parameter_str(QTestState *who, const char *parameter,
416                                       const char *value)
417 {
418     qtest_qmp_assert_success(who,
419                              "{ 'execute': 'migrate-set-parameters',"
420                              "'arguments': { %s: %s } }",
421                              parameter, value);
422     migrate_check_parameter_str(who, parameter, value);
423 }
424 
425 static long long migrate_get_parameter_bool(QTestState *who,
426                                             const char *parameter)
427 {
428     QDict *rsp;
429     int result;
430 
431     rsp = qtest_qmp_assert_success_ref(
432         who, "{ 'execute': 'query-migrate-parameters' }");
433     result = qdict_get_bool(rsp, parameter);
434     qobject_unref(rsp);
435     return !!result;
436 }
437 
438 static void migrate_check_parameter_bool(QTestState *who, const char *parameter,
439                                          int value)
440 {
441     int result;
442 
443     result = migrate_get_parameter_bool(who, parameter);
444     g_assert_cmpint(result, ==, value);
445 }
446 
447 static void migrate_set_parameter_bool(QTestState *who, const char *parameter,
448                                        int value)
449 {
450     qtest_qmp_assert_success(who,
451                              "{ 'execute': 'migrate-set-parameters',"
452                              "'arguments': { %s: %i } }",
453                              parameter, value);
454     migrate_check_parameter_bool(who, parameter, value);
455 }
456 
457 static void migrate_ensure_non_converge(QTestState *who)
458 {
459     /* Can't converge with 1ms downtime + 3 mbs bandwidth limit */
460     migrate_set_parameter_int(who, "max-bandwidth", 3 * 1000 * 1000);
461     migrate_set_parameter_int(who, "downtime-limit", 1);
462 }
463 
464 static void migrate_ensure_converge(QTestState *who)
465 {
466     /* Should converge with 30s downtime + 1 gbs bandwidth limit */
467     migrate_set_parameter_int(who, "max-bandwidth", 1 * 1000 * 1000 * 1000);
468     migrate_set_parameter_int(who, "downtime-limit", 30 * 1000);
469 }
470 
471 /*
472  * Our goal is to ensure that we run a single full migration
473  * iteration, and also dirty memory, ensuring that at least
474  * one further iteration is required.
475  *
476  * We can't directly synchronize with the start of a migration
477  * so we have to apply some tricks monitoring memory that is
478  * transferred.
479  *
480  * Initially we set the migration bandwidth to an insanely
481  * low value, with tiny max downtime too. This basically
482  * guarantees migration will never complete.
483  *
484  * This will result in a test that is unacceptably slow though,
485  * so we can't let the entire migration pass run at this speed.
486  * Our intent is to let it run just long enough that we can
487  * prove data prior to the marker has been transferred *AND*
488  * also prove this transferred data is dirty again.
489  *
490  * Before migration starts, we write a 64-bit magic marker
491  * into a fixed location in the src VM RAM.
492  *
493  * Then watch dst memory until the marker appears. This is
494  * proof that start_address -> MAGIC_OFFSET_BASE has been
495  * transferred.
496  *
497  * Finally we go back to the source and read a byte just
498  * before the marker until we see it flip in value. This
499  * is proof that start_address -> MAGIC_OFFSET_BASE
500  * is now dirty again.
501  *
502  * IOW, we're guaranteed at least a 2nd migration pass
503  * at this point.
504  *
505  * We can now let migration run at full speed to finish
506  * the test
507  */
508 static void migrate_prepare_for_dirty_mem(QTestState *from)
509 {
510     /*
511      * The guest workflow iterates from start_address to
512      * end_address, writing 1 byte every TEST_MEM_PAGE_SIZE
513      * bytes.
514      *
515      * IOW, if we write to mem at a point which is NOT
516      * a multiple of TEST_MEM_PAGE_SIZE, our write won't
517      * conflict with the migration workflow.
518      *
519      * We put in a marker here, that we'll use to determine
520      * when the data has been transferred to the dst.
521      */
522     qtest_writeq(from, start_address + MAGIC_OFFSET, MAGIC_MARKER);
523 }
524 
525 static void migrate_wait_for_dirty_mem(QTestState *from,
526                                        QTestState *to)
527 {
528     uint64_t watch_address = start_address + MAGIC_OFFSET_BASE;
529     uint64_t marker_address = start_address + MAGIC_OFFSET;
530     uint8_t watch_byte;
531 
532     /*
533      * Wait for the MAGIC_MARKER to get transferred, as an
534      * indicator that a migration pass has made some known
535      * amount of progress.
536      */
537     do {
538         usleep(1000 * 10);
539     } while (qtest_readq(to, marker_address) != MAGIC_MARKER);
540 
541 
542     /* If suspended, src only iterates once, and watch_byte may never change */
543     if (src_state.suspend_me) {
544         return;
545     }
546 
547     /*
548      * Now ensure that already transferred bytes are
549      * dirty again from the guest workload. Note the
550      * guest byte value will wrap around and by chance
551      * match the original watch_byte. This is harmless
552      * as we'll eventually see a different value if we
553      * keep watching
554      */
555     watch_byte = qtest_readb(from, watch_address);
556     do {
557         usleep(1000 * 10);
558     } while (qtest_readb(from, watch_address) == watch_byte);
559 }
560 
561 
562 static void migrate_pause(QTestState *who)
563 {
564     qtest_qmp_assert_success(who, "{ 'execute': 'migrate-pause' }");
565 }
566 
567 static void migrate_continue(QTestState *who, const char *state)
568 {
569     qtest_qmp_assert_success(who,
570                              "{ 'execute': 'migrate-continue',"
571                              "  'arguments': { 'state': %s } }",
572                              state);
573 }
574 
575 static void migrate_recover(QTestState *who, const char *uri)
576 {
577     qtest_qmp_assert_success(who,
578                              "{ 'execute': 'migrate-recover', "
579                              "  'id': 'recover-cmd', "
580                              "  'arguments': { 'uri': %s } }",
581                              uri);
582 }
583 
584 static void migrate_cancel(QTestState *who)
585 {
586     qtest_qmp_assert_success(who, "{ 'execute': 'migrate_cancel' }");
587 }
588 
589 static void migrate_postcopy_start(QTestState *from, QTestState *to)
590 {
591     qtest_qmp_assert_success(from, "{ 'execute': 'migrate-start-postcopy' }");
592 
593     wait_for_stop(from, &src_state);
594     qtest_qmp_eventwait(to, "RESUME");
595 }
596 
597 typedef struct {
598     /*
599      * QTEST_LOG=1 may override this.  When QTEST_LOG=1, we always dump errors
600      * unconditionally, because it means the user would like to be verbose.
601      */
602     bool hide_stderr;
603     bool use_shmem;
604     /* only launch the target process */
605     bool only_target;
606     /* Use dirty ring if true; dirty logging otherwise */
607     bool use_dirty_ring;
608     const char *opts_source;
609     const char *opts_target;
610     /* suspend the src before migrating to dest. */
611     bool suspend_me;
612 } MigrateStart;
613 
614 /*
615  * A hook that runs after the src and dst QEMUs have been
616  * created, but before the migration is started. This can
617  * be used to set migration parameters and capabilities.
618  *
619  * Returns: NULL, or a pointer to opaque state to be
620  *          later passed to the TestMigrateFinishHook
621  */
622 typedef void * (*TestMigrateStartHook)(QTestState *from,
623                                        QTestState *to);
624 
625 /*
626  * A hook that runs after the migration has finished,
627  * regardless of whether it succeeded or failed, but
628  * before QEMU has terminated (unless it self-terminated
629  * due to migration error)
630  *
631  * @opaque is a pointer to state previously returned
632  * by the TestMigrateStartHook if any, or NULL.
633  */
634 typedef void (*TestMigrateEndHook)(QTestState *from,
635                                    QTestState *to,
636                                    void *opaque);
637 
638 typedef struct {
639     /* Optional: fine tune start parameters */
640     MigrateStart start;
641 
642     /* Required: the URI for the dst QEMU to listen on */
643     const char *listen_uri;
644 
645     /*
646      * Optional: the URI for the src QEMU to connect to
647      * If NULL, then it will query the dst QEMU for its actual
648      * listening address and use that as the connect address.
649      * This allows for dynamically picking a free TCP port.
650      */
651     const char *connect_uri;
652 
653     /*
654      * Optional: JSON-formatted list of src QEMU URIs. If a port is
655      * defined as '0' in any QDict key a value of '0' will be
656      * automatically converted to the correct destination port.
657      */
658     const char *connect_channels;
659 
660     /* Optional: callback to run at start to set migration parameters */
661     TestMigrateStartHook start_hook;
662     /* Optional: callback to run at finish to cleanup */
663     TestMigrateEndHook end_hook;
664 
665     /*
666      * Optional: normally we expect the migration process to complete.
667      *
668      * There can be a variety of reasons and stages in which failure
669      * can happen during tests.
670      *
671      * If a failure is expected to happen at time of establishing
672      * the connection, then MIG_TEST_FAIL will indicate that the dst
673      * QEMU is expected to stay running and accept future migration
674      * connections.
675      *
676      * If a failure is expected to happen while processing the
677      * migration stream, then MIG_TEST_FAIL_DEST_QUIT_ERR will indicate
678      * that the dst QEMU is expected to quit with non-zero exit status
679      */
680     enum {
681         /* This test should succeed, the default */
682         MIG_TEST_SUCCEED = 0,
683         /* This test should fail, dest qemu should keep alive */
684         MIG_TEST_FAIL,
685         /* This test should fail, dest qemu should fail with abnormal status */
686         MIG_TEST_FAIL_DEST_QUIT_ERR,
687         /* The QMP command for this migration should fail with an error */
688         MIG_TEST_QMP_ERROR,
689     } result;
690 
691     /*
692      * Optional: set number of migration passes to wait for, if live==true.
693      * If zero, then merely wait for a few MB of dirty data
694      */
695     unsigned int iterations;
696 
697     /*
698      * Optional: whether the guest CPUs should be running during a precopy
699      * migration test.  We used to always run with live but it took much
700      * longer so we reduced live tests to only the ones that have solid
701      * reason to be tested live-only.  For each of the new test cases for
702      * precopy please provide justifications to use live explicitly (please
703      * refer to existing ones with live=true), or use live=off by default.
704      */
705     bool live;
706 
707     /* Postcopy specific fields */
708     void *postcopy_data;
709     bool postcopy_preempt;
710     PostcopyRecoveryFailStage postcopy_recovery_fail_stage;
711 } MigrateCommon;
712 
713 static int migrate_start(QTestState **from, QTestState **to,
714                          const char *uri, MigrateStart *args)
715 {
716     g_autofree gchar *arch_source = NULL;
717     g_autofree gchar *arch_target = NULL;
718     /* options for source and target */
719     g_autofree gchar *arch_opts = NULL;
720     g_autofree gchar *cmd_source = NULL;
721     g_autofree gchar *cmd_target = NULL;
722     const gchar *ignore_stderr;
723     g_autofree char *shmem_opts = NULL;
724     g_autofree char *shmem_path = NULL;
725     const char *kvm_opts = NULL;
726     const char *arch = qtest_get_arch();
727     const char *memory_size;
728     const char *machine_alias, *machine_opts = "";
729     g_autofree char *machine = NULL;
730 
731     if (args->use_shmem) {
732         if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
733             g_test_skip("/dev/shm is not supported");
734             return -1;
735         }
736     }
737 
738     dst_state = (QTestMigrationState) { };
739     src_state = (QTestMigrationState) { };
740     bootfile_create(tmpfs, args->suspend_me);
741     src_state.suspend_me = args->suspend_me;
742 
743     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
744         memory_size = "150M";
745 
746         if (g_str_equal(arch, "i386")) {
747             machine_alias = "pc";
748         } else {
749             machine_alias = "q35";
750         }
751         arch_opts = g_strdup_printf(
752             "-drive if=none,id=d0,file=%s,format=raw "
753             "-device ide-hd,drive=d0,secs=1,cyls=1,heads=1", bootpath);
754         start_address = X86_TEST_MEM_START;
755         end_address = X86_TEST_MEM_END;
756     } else if (g_str_equal(arch, "s390x")) {
757         memory_size = "128M";
758         machine_alias = "s390-ccw-virtio";
759         arch_opts = g_strdup_printf("-bios %s", bootpath);
760         start_address = S390_TEST_MEM_START;
761         end_address = S390_TEST_MEM_END;
762     } else if (strcmp(arch, "ppc64") == 0) {
763         memory_size = "256M";
764         start_address = PPC_TEST_MEM_START;
765         end_address = PPC_TEST_MEM_END;
766         machine_alias = "pseries";
767         machine_opts = "vsmt=8";
768         arch_opts = g_strdup_printf(
769             "-nodefaults -machine " PSERIES_DEFAULT_CAPABILITIES " "
770             "-bios %s", bootpath);
771     } else if (strcmp(arch, "aarch64") == 0) {
772         memory_size = "150M";
773         machine_alias = "virt";
774         machine_opts = "gic-version=3";
775         arch_opts = g_strdup_printf("-cpu max -kernel %s", bootpath);
776         start_address = ARM_TEST_MEM_START;
777         end_address = ARM_TEST_MEM_END;
778     } else {
779         g_assert_not_reached();
780     }
781 
782     if (!getenv("QTEST_LOG") && args->hide_stderr) {
783 #ifndef _WIN32
784         ignore_stderr = "2>/dev/null";
785 #else
786         /*
787          * On Windows the QEMU executable is created via CreateProcess() and
788          * IO redirection does not work, so don't bother adding IO redirection
789          * to the command line.
790          */
791         ignore_stderr = "";
792 #endif
793     } else {
794         ignore_stderr = "";
795     }
796 
797     if (args->use_shmem) {
798         shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
799         shmem_opts = g_strdup_printf(
800             "-object memory-backend-file,id=mem0,size=%s"
801             ",mem-path=%s,share=on -numa node,memdev=mem0",
802             memory_size, shmem_path);
803     }
804 
805     if (args->use_dirty_ring) {
806         kvm_opts = ",dirty-ring-size=4096";
807     }
808 
809     if (!qtest_has_machine(machine_alias)) {
810         g_autofree char *msg = g_strdup_printf("machine %s not supported", machine_alias);
811         g_test_skip(msg);
812         return -1;
813     }
814 
815     machine = resolve_machine_version(machine_alias, QEMU_ENV_SRC,
816                                       QEMU_ENV_DST);
817 
818     g_test_message("Using machine type: %s", machine);
819 
820     cmd_source = g_strdup_printf("-accel kvm%s -accel tcg "
821                                  "-machine %s,%s "
822                                  "-name source,debug-threads=on "
823                                  "-m %s "
824                                  "-serial file:%s/src_serial "
825                                  "%s %s %s %s %s",
826                                  kvm_opts ? kvm_opts : "",
827                                  machine, machine_opts,
828                                  memory_size, tmpfs,
829                                  arch_opts ? arch_opts : "",
830                                  arch_source ? arch_source : "",
831                                  shmem_opts ? shmem_opts : "",
832                                  args->opts_source ? args->opts_source : "",
833                                  ignore_stderr);
834     if (!args->only_target) {
835         *from = qtest_init_with_env(QEMU_ENV_SRC, cmd_source);
836         qtest_qmp_set_event_callback(*from,
837                                      migrate_watch_for_events,
838                                      &src_state);
839     }
840 
841     cmd_target = g_strdup_printf("-accel kvm%s -accel tcg "
842                                  "-machine %s,%s "
843                                  "-name target,debug-threads=on "
844                                  "-m %s "
845                                  "-serial file:%s/dest_serial "
846                                  "-incoming %s "
847                                  "%s %s %s %s %s",
848                                  kvm_opts ? kvm_opts : "",
849                                  machine, machine_opts,
850                                  memory_size, tmpfs, uri,
851                                  arch_opts ? arch_opts : "",
852                                  arch_target ? arch_target : "",
853                                  shmem_opts ? shmem_opts : "",
854                                  args->opts_target ? args->opts_target : "",
855                                  ignore_stderr);
856     *to = qtest_init_with_env(QEMU_ENV_DST, cmd_target);
857     qtest_qmp_set_event_callback(*to,
858                                  migrate_watch_for_events,
859                                  &dst_state);
860 
861     /*
862      * Remove shmem file immediately to avoid memory leak in test failed case.
863      * It's valid because QEMU has already opened this file
864      */
865     if (args->use_shmem) {
866         unlink(shmem_path);
867     }
868 
869     /*
870      * Always enable migration events.  Libvirt always uses it, let's try
871      * to mimic as closer as that.
872      */
873     migrate_set_capability(*from, "events", true);
874     migrate_set_capability(*to, "events", true);
875 
876     return 0;
877 }
878 
879 static void migrate_end(QTestState *from, QTestState *to, bool test_dest)
880 {
881     unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
882 
883     qtest_quit(from);
884 
885     if (test_dest) {
886         qtest_memread(to, start_address, &dest_byte_a, 1);
887 
888         /* Destination still running, wait for a byte to change */
889         do {
890             qtest_memread(to, start_address, &dest_byte_b, 1);
891             usleep(1000 * 10);
892         } while (dest_byte_a == dest_byte_b);
893 
894         qtest_qmp_assert_success(to, "{ 'execute' : 'stop'}");
895 
896         /* With it stopped, check nothing changes */
897         qtest_memread(to, start_address, &dest_byte_c, 1);
898         usleep(1000 * 200);
899         qtest_memread(to, start_address, &dest_byte_d, 1);
900         g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
901 
902         check_guests_ram(to);
903     }
904 
905     qtest_quit(to);
906 
907     cleanup("migsocket");
908     cleanup("src_serial");
909     cleanup("dest_serial");
910     cleanup(FILE_TEST_FILENAME);
911 }
912 
913 #ifdef CONFIG_GNUTLS
914 struct TestMigrateTLSPSKData {
915     char *workdir;
916     char *workdiralt;
917     char *pskfile;
918     char *pskfilealt;
919 };
920 
921 static void *
922 migrate_hook_start_tls_psk_common(QTestState *from,
923                                   QTestState *to,
924                                   bool mismatch)
925 {
926     struct TestMigrateTLSPSKData *data =
927         g_new0(struct TestMigrateTLSPSKData, 1);
928 
929     data->workdir = g_strdup_printf("%s/tlscredspsk0", tmpfs);
930     data->pskfile = g_strdup_printf("%s/%s", data->workdir,
931                                     QCRYPTO_TLS_CREDS_PSKFILE);
932     g_mkdir_with_parents(data->workdir, 0700);
933     test_tls_psk_init(data->pskfile);
934 
935     if (mismatch) {
936         data->workdiralt = g_strdup_printf("%s/tlscredspskalt0", tmpfs);
937         data->pskfilealt = g_strdup_printf("%s/%s", data->workdiralt,
938                                            QCRYPTO_TLS_CREDS_PSKFILE);
939         g_mkdir_with_parents(data->workdiralt, 0700);
940         test_tls_psk_init_alt(data->pskfilealt);
941     }
942 
943     qtest_qmp_assert_success(from,
944                              "{ 'execute': 'object-add',"
945                              "  'arguments': { 'qom-type': 'tls-creds-psk',"
946                              "                 'id': 'tlscredspsk0',"
947                              "                 'endpoint': 'client',"
948                              "                 'dir': %s,"
949                              "                 'username': 'qemu'} }",
950                              data->workdir);
951 
952     qtest_qmp_assert_success(to,
953                              "{ 'execute': 'object-add',"
954                              "  'arguments': { 'qom-type': 'tls-creds-psk',"
955                              "                 'id': 'tlscredspsk0',"
956                              "                 'endpoint': 'server',"
957                              "                 'dir': %s } }",
958                              mismatch ? data->workdiralt : data->workdir);
959 
960     migrate_set_parameter_str(from, "tls-creds", "tlscredspsk0");
961     migrate_set_parameter_str(to, "tls-creds", "tlscredspsk0");
962 
963     return data;
964 }
965 
966 static void *
967 migrate_hook_start_tls_psk_match(QTestState *from,
968                                  QTestState *to)
969 {
970     return migrate_hook_start_tls_psk_common(from, to, false);
971 }
972 
973 static void *
974 migrate_hook_start_tls_psk_mismatch(QTestState *from,
975                                     QTestState *to)
976 {
977     return migrate_hook_start_tls_psk_common(from, to, true);
978 }
979 
980 static void
981 migrate_hook_end_tls_psk(QTestState *from,
982                          QTestState *to,
983                          void *opaque)
984 {
985     struct TestMigrateTLSPSKData *data = opaque;
986 
987     test_tls_psk_cleanup(data->pskfile);
988     if (data->pskfilealt) {
989         test_tls_psk_cleanup(data->pskfilealt);
990     }
991     rmdir(data->workdir);
992     if (data->workdiralt) {
993         rmdir(data->workdiralt);
994     }
995 
996     g_free(data->workdiralt);
997     g_free(data->pskfilealt);
998     g_free(data->workdir);
999     g_free(data->pskfile);
1000     g_free(data);
1001 }
1002 
1003 #ifdef CONFIG_TASN1
1004 typedef struct {
1005     char *workdir;
1006     char *keyfile;
1007     char *cacert;
1008     char *servercert;
1009     char *serverkey;
1010     char *clientcert;
1011     char *clientkey;
1012 } TestMigrateTLSX509Data;
1013 
1014 typedef struct {
1015     bool verifyclient;
1016     bool clientcert;
1017     bool hostileclient;
1018     bool authzclient;
1019     const char *certhostname;
1020     const char *certipaddr;
1021 } TestMigrateTLSX509;
1022 
1023 static void *
1024 migrate_hook_start_tls_x509_common(QTestState *from,
1025                                    QTestState *to,
1026                                    TestMigrateTLSX509 *args)
1027 {
1028     TestMigrateTLSX509Data *data = g_new0(TestMigrateTLSX509Data, 1);
1029 
1030     data->workdir = g_strdup_printf("%s/tlscredsx5090", tmpfs);
1031     data->keyfile = g_strdup_printf("%s/key.pem", data->workdir);
1032 
1033     data->cacert = g_strdup_printf("%s/ca-cert.pem", data->workdir);
1034     data->serverkey = g_strdup_printf("%s/server-key.pem", data->workdir);
1035     data->servercert = g_strdup_printf("%s/server-cert.pem", data->workdir);
1036     if (args->clientcert) {
1037         data->clientkey = g_strdup_printf("%s/client-key.pem", data->workdir);
1038         data->clientcert = g_strdup_printf("%s/client-cert.pem", data->workdir);
1039     }
1040 
1041     g_mkdir_with_parents(data->workdir, 0700);
1042 
1043     test_tls_init(data->keyfile);
1044 #ifndef _WIN32
1045     g_assert(link(data->keyfile, data->serverkey) == 0);
1046 #else
1047     g_assert(CreateHardLink(data->serverkey, data->keyfile, NULL) != 0);
1048 #endif
1049     if (args->clientcert) {
1050 #ifndef _WIN32
1051         g_assert(link(data->keyfile, data->clientkey) == 0);
1052 #else
1053         g_assert(CreateHardLink(data->clientkey, data->keyfile, NULL) != 0);
1054 #endif
1055     }
1056 
1057     TLS_ROOT_REQ_SIMPLE(cacertreq, data->cacert);
1058     if (args->clientcert) {
1059         TLS_CERT_REQ_SIMPLE_CLIENT(servercertreq, cacertreq,
1060                                    args->hostileclient ?
1061                                    QCRYPTO_TLS_TEST_CLIENT_HOSTILE_NAME :
1062                                    QCRYPTO_TLS_TEST_CLIENT_NAME,
1063                                    data->clientcert);
1064         test_tls_deinit_cert(&servercertreq);
1065     }
1066 
1067     TLS_CERT_REQ_SIMPLE_SERVER(clientcertreq, cacertreq,
1068                                data->servercert,
1069                                args->certhostname,
1070                                args->certipaddr);
1071     test_tls_deinit_cert(&clientcertreq);
1072     test_tls_deinit_cert(&cacertreq);
1073 
1074     qtest_qmp_assert_success(from,
1075                              "{ 'execute': 'object-add',"
1076                              "  'arguments': { 'qom-type': 'tls-creds-x509',"
1077                              "                 'id': 'tlscredsx509client0',"
1078                              "                 'endpoint': 'client',"
1079                              "                 'dir': %s,"
1080                              "                 'sanity-check': true,"
1081                              "                 'verify-peer': true} }",
1082                              data->workdir);
1083     migrate_set_parameter_str(from, "tls-creds", "tlscredsx509client0");
1084     if (args->certhostname) {
1085         migrate_set_parameter_str(from, "tls-hostname", args->certhostname);
1086     }
1087 
1088     qtest_qmp_assert_success(to,
1089                              "{ 'execute': 'object-add',"
1090                              "  'arguments': { 'qom-type': 'tls-creds-x509',"
1091                              "                 'id': 'tlscredsx509server0',"
1092                              "                 'endpoint': 'server',"
1093                              "                 'dir': %s,"
1094                              "                 'sanity-check': true,"
1095                              "                 'verify-peer': %i} }",
1096                              data->workdir, args->verifyclient);
1097     migrate_set_parameter_str(to, "tls-creds", "tlscredsx509server0");
1098 
1099     if (args->authzclient) {
1100         qtest_qmp_assert_success(to,
1101                                  "{ 'execute': 'object-add',"
1102                                  "  'arguments': { 'qom-type': 'authz-simple',"
1103                                  "                 'id': 'tlsauthz0',"
1104                                  "                 'identity': %s} }",
1105                                  "CN=" QCRYPTO_TLS_TEST_CLIENT_NAME);
1106         migrate_set_parameter_str(to, "tls-authz", "tlsauthz0");
1107     }
1108 
1109     return data;
1110 }
1111 
1112 /*
1113  * The normal case: match server's cert hostname against
1114  * whatever host we were telling QEMU to connect to (if any)
1115  */
1116 static void *
1117 migrate_hook_start_tls_x509_default_host(QTestState *from,
1118                                          QTestState *to)
1119 {
1120     TestMigrateTLSX509 args = {
1121         .verifyclient = true,
1122         .clientcert = true,
1123         .certipaddr = "127.0.0.1"
1124     };
1125     return migrate_hook_start_tls_x509_common(from, to, &args);
1126 }
1127 
1128 /*
1129  * The unusual case: the server's cert is different from
1130  * the address we're telling QEMU to connect to (if any),
1131  * so we must give QEMU an explicit hostname to validate
1132  */
1133 static void *
1134 migrate_hook_start_tls_x509_override_host(QTestState *from,
1135                                           QTestState *to)
1136 {
1137     TestMigrateTLSX509 args = {
1138         .verifyclient = true,
1139         .clientcert = true,
1140         .certhostname = "qemu.org",
1141     };
1142     return migrate_hook_start_tls_x509_common(from, to, &args);
1143 }
1144 
1145 /*
1146  * The unusual case: the server's cert is different from
1147  * the address we're telling QEMU to connect to, and so we
1148  * expect the client to reject the server
1149  */
1150 static void *
1151 migrate_hook_start_tls_x509_mismatch_host(QTestState *from,
1152                                           QTestState *to)
1153 {
1154     TestMigrateTLSX509 args = {
1155         .verifyclient = true,
1156         .clientcert = true,
1157         .certipaddr = "10.0.0.1",
1158     };
1159     return migrate_hook_start_tls_x509_common(from, to, &args);
1160 }
1161 
1162 static void *
1163 migrate_hook_start_tls_x509_friendly_client(QTestState *from,
1164                                             QTestState *to)
1165 {
1166     TestMigrateTLSX509 args = {
1167         .verifyclient = true,
1168         .clientcert = true,
1169         .authzclient = true,
1170         .certipaddr = "127.0.0.1",
1171     };
1172     return migrate_hook_start_tls_x509_common(from, to, &args);
1173 }
1174 
1175 static void *
1176 migrate_hook_start_tls_x509_hostile_client(QTestState *from,
1177                                            QTestState *to)
1178 {
1179     TestMigrateTLSX509 args = {
1180         .verifyclient = true,
1181         .clientcert = true,
1182         .hostileclient = true,
1183         .authzclient = true,
1184         .certipaddr = "127.0.0.1",
1185     };
1186     return migrate_hook_start_tls_x509_common(from, to, &args);
1187 }
1188 
1189 /*
1190  * The case with no client certificate presented,
1191  * and no server verification
1192  */
1193 static void *
1194 migrate_hook_start_tls_x509_allow_anon_client(QTestState *from,
1195                                               QTestState *to)
1196 {
1197     TestMigrateTLSX509 args = {
1198         .certipaddr = "127.0.0.1",
1199     };
1200     return migrate_hook_start_tls_x509_common(from, to, &args);
1201 }
1202 
1203 /*
1204  * The case with no client certificate presented,
1205  * and server verification rejecting
1206  */
1207 static void *
1208 migrate_hook_start_tls_x509_reject_anon_client(QTestState *from,
1209                                                QTestState *to)
1210 {
1211     TestMigrateTLSX509 args = {
1212         .verifyclient = true,
1213         .certipaddr = "127.0.0.1",
1214     };
1215     return migrate_hook_start_tls_x509_common(from, to, &args);
1216 }
1217 
1218 static void
1219 migrate_hook_end_tls_x509(QTestState *from,
1220                           QTestState *to,
1221                           void *opaque)
1222 {
1223     TestMigrateTLSX509Data *data = opaque;
1224 
1225     test_tls_cleanup(data->keyfile);
1226     g_free(data->keyfile);
1227 
1228     unlink(data->cacert);
1229     g_free(data->cacert);
1230     unlink(data->servercert);
1231     g_free(data->servercert);
1232     unlink(data->serverkey);
1233     g_free(data->serverkey);
1234 
1235     if (data->clientcert) {
1236         unlink(data->clientcert);
1237         g_free(data->clientcert);
1238     }
1239     if (data->clientkey) {
1240         unlink(data->clientkey);
1241         g_free(data->clientkey);
1242     }
1243 
1244     rmdir(data->workdir);
1245     g_free(data->workdir);
1246 
1247     g_free(data);
1248 }
1249 #endif /* CONFIG_TASN1 */
1250 #endif /* CONFIG_GNUTLS */
1251 
1252 static int migrate_postcopy_prepare(QTestState **from_ptr,
1253                                     QTestState **to_ptr,
1254                                     MigrateCommon *args)
1255 {
1256     QTestState *from, *to;
1257 
1258     if (migrate_start(&from, &to, "defer", &args->start)) {
1259         return -1;
1260     }
1261 
1262     if (args->start_hook) {
1263         args->postcopy_data = args->start_hook(from, to);
1264     }
1265 
1266     migrate_set_capability(from, "postcopy-ram", true);
1267     migrate_set_capability(to, "postcopy-ram", true);
1268     migrate_set_capability(to, "postcopy-blocktime", true);
1269 
1270     if (args->postcopy_preempt) {
1271         migrate_set_capability(from, "postcopy-preempt", true);
1272         migrate_set_capability(to, "postcopy-preempt", true);
1273     }
1274 
1275     migrate_ensure_non_converge(from);
1276 
1277     migrate_prepare_for_dirty_mem(from);
1278     qtest_qmp_assert_success(to, "{ 'execute': 'migrate-incoming',"
1279                              "  'arguments': { "
1280                              "      'channels': [ { 'channel-type': 'main',"
1281                              "      'addr': { 'transport': 'socket',"
1282                              "                'type': 'inet',"
1283                              "                'host': '127.0.0.1',"
1284                              "                'port': '0' } } ] } }");
1285 
1286     /* Wait for the first serial output from the source */
1287     wait_for_serial("src_serial");
1288     wait_for_suspend(from, &src_state);
1289 
1290     migrate_qmp(from, to, NULL, NULL, "{}");
1291 
1292     migrate_wait_for_dirty_mem(from, to);
1293 
1294     *from_ptr = from;
1295     *to_ptr = to;
1296 
1297     return 0;
1298 }
1299 
1300 static void migrate_postcopy_complete(QTestState *from, QTestState *to,
1301                                       MigrateCommon *args)
1302 {
1303     wait_for_migration_complete(from);
1304 
1305     if (args->start.suspend_me) {
1306         /* wakeup succeeds only if guest is suspended */
1307         qtest_qmp_assert_success(to, "{'execute': 'system_wakeup'}");
1308     }
1309 
1310     /* Make sure we get at least one "B" on destination */
1311     wait_for_serial("dest_serial");
1312 
1313     if (uffd_feature_thread_id) {
1314         read_blocktime(to);
1315     }
1316 
1317     if (args->end_hook) {
1318         args->end_hook(from, to, args->postcopy_data);
1319         args->postcopy_data = NULL;
1320     }
1321 
1322     migrate_end(from, to, true);
1323 }
1324 
1325 static void test_postcopy_common(MigrateCommon *args)
1326 {
1327     QTestState *from, *to;
1328 
1329     if (migrate_postcopy_prepare(&from, &to, args)) {
1330         return;
1331     }
1332     migrate_postcopy_start(from, to);
1333     migrate_postcopy_complete(from, to, args);
1334 }
1335 
1336 static void test_postcopy(void)
1337 {
1338     MigrateCommon args = { };
1339 
1340     test_postcopy_common(&args);
1341 }
1342 
1343 static void test_postcopy_suspend(void)
1344 {
1345     MigrateCommon args = {
1346         .start.suspend_me = true,
1347     };
1348 
1349     test_postcopy_common(&args);
1350 }
1351 
1352 static void test_postcopy_preempt(void)
1353 {
1354     MigrateCommon args = {
1355         .postcopy_preempt = true,
1356     };
1357 
1358     test_postcopy_common(&args);
1359 }
1360 
1361 #ifdef CONFIG_GNUTLS
1362 static void test_postcopy_tls_psk(void)
1363 {
1364     MigrateCommon args = {
1365         .start_hook = migrate_hook_start_tls_psk_match,
1366         .end_hook = migrate_hook_end_tls_psk,
1367     };
1368 
1369     test_postcopy_common(&args);
1370 }
1371 
1372 static void test_postcopy_preempt_tls_psk(void)
1373 {
1374     MigrateCommon args = {
1375         .postcopy_preempt = true,
1376         .start_hook = migrate_hook_start_tls_psk_match,
1377         .end_hook = migrate_hook_end_tls_psk,
1378     };
1379 
1380     test_postcopy_common(&args);
1381 }
1382 #endif
1383 
1384 static void wait_for_postcopy_status(QTestState *one, const char *status)
1385 {
1386     wait_for_migration_status(one, status,
1387                               (const char * []) {
1388                                   "failed", "active",
1389                                   "completed", NULL
1390                               });
1391 }
1392 
1393 static void postcopy_recover_fail(QTestState *from, QTestState *to,
1394                                   PostcopyRecoveryFailStage stage)
1395 {
1396 #ifndef _WIN32
1397     bool fail_early = (stage == POSTCOPY_FAIL_CHANNEL_ESTABLISH);
1398     int ret, pair1[2], pair2[2];
1399     char c;
1400 
1401     g_assert(stage > POSTCOPY_FAIL_NONE && stage < POSTCOPY_FAIL_MAX);
1402 
1403     /* Create two unrelated socketpairs */
1404     ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair1);
1405     g_assert_cmpint(ret, ==, 0);
1406 
1407     ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair2);
1408     g_assert_cmpint(ret, ==, 0);
1409 
1410     /*
1411      * Give the guests unpaired ends of the sockets, so they'll all blocked
1412      * at reading.  This mimics a wrong channel established.
1413      */
1414     qtest_qmp_fds_assert_success(from, &pair1[0], 1,
1415                                  "{ 'execute': 'getfd',"
1416                                  "  'arguments': { 'fdname': 'fd-mig' }}");
1417     qtest_qmp_fds_assert_success(to, &pair2[0], 1,
1418                                  "{ 'execute': 'getfd',"
1419                                  "  'arguments': { 'fdname': 'fd-mig' }}");
1420 
1421     /*
1422      * Write the 1st byte as QEMU_VM_COMMAND (0x8) for the dest socket, to
1423      * emulate the 1st byte of a real recovery, but stops from there to
1424      * keep dest QEMU in RECOVER.  This is needed so that we can kick off
1425      * the recover process on dest QEMU (by triggering the G_IO_IN event).
1426      *
1427      * NOTE: this trick is not needed on src QEMUs, because src doesn't
1428      * rely on an pre-existing G_IO_IN event, so it will always trigger the
1429      * upcoming recovery anyway even if it can read nothing.
1430      */
1431 #define QEMU_VM_COMMAND              0x08
1432     c = QEMU_VM_COMMAND;
1433     ret = send(pair2[1], &c, 1, 0);
1434     g_assert_cmpint(ret, ==, 1);
1435 
1436     if (stage == POSTCOPY_FAIL_CHANNEL_ESTABLISH) {
1437         /*
1438          * This will make src QEMU to fail at an early stage when trying to
1439          * resume later, where it shouldn't reach RECOVER stage at all.
1440          */
1441         close(pair1[1]);
1442     }
1443 
1444     migrate_recover(to, "fd:fd-mig");
1445     migrate_qmp(from, to, "fd:fd-mig", NULL, "{'resume': true}");
1446 
1447     /*
1448      * Source QEMU has an extra RECOVER_SETUP phase, dest doesn't have it.
1449      * Make sure it appears along the way.
1450      */
1451     migration_event_wait(from, "postcopy-recover-setup");
1452 
1453     if (fail_early) {
1454         /*
1455          * When fails at reconnection, src QEMU will automatically goes
1456          * back to PAUSED state.  Making sure there is an event in this
1457          * case: Libvirt relies on this to detect early reconnection
1458          * errors.
1459          */
1460         migration_event_wait(from, "postcopy-paused");
1461     } else {
1462         /*
1463          * We want to test "fail later" at RECOVER stage here.  Make sure
1464          * both QEMU instances will go into RECOVER stage first, then test
1465          * kicking them out using migrate-pause.
1466          *
1467          * Explicitly check the RECOVER event on src, that's what Libvirt
1468          * relies on, rather than polling.
1469          */
1470         migration_event_wait(from, "postcopy-recover");
1471         wait_for_postcopy_status(from, "postcopy-recover");
1472 
1473         /* Need an explicit kick on src QEMU in this case */
1474         migrate_pause(from);
1475     }
1476 
1477     /*
1478      * For all failure cases, we'll reach such states on both sides now.
1479      * Check them.
1480      */
1481     wait_for_postcopy_status(from, "postcopy-paused");
1482     wait_for_postcopy_status(to, "postcopy-recover");
1483 
1484     /*
1485      * Kick dest QEMU out too. This is normally not needed in reality
1486      * because when the channel is shutdown it should also happen on src.
1487      * However here we used separate socket pairs so we need to do that
1488      * explicitly.
1489      */
1490     migrate_pause(to);
1491     wait_for_postcopy_status(to, "postcopy-paused");
1492 
1493     close(pair1[0]);
1494     close(pair2[0]);
1495     close(pair2[1]);
1496 
1497     if (stage != POSTCOPY_FAIL_CHANNEL_ESTABLISH) {
1498         close(pair1[1]);
1499     }
1500 #endif
1501 }
1502 
1503 static void test_postcopy_recovery_common(MigrateCommon *args)
1504 {
1505     QTestState *from, *to;
1506     g_autofree char *uri = NULL;
1507 
1508     /* Always hide errors for postcopy recover tests since they're expected */
1509     args->start.hide_stderr = true;
1510 
1511     if (migrate_postcopy_prepare(&from, &to, args)) {
1512         return;
1513     }
1514 
1515     /* Turn postcopy speed down, 4K/s is slow enough on any machines */
1516     migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
1517 
1518     /* Now we start the postcopy */
1519     migrate_postcopy_start(from, to);
1520 
1521     /*
1522      * Wait until postcopy is really started; we can only run the
1523      * migrate-pause command during a postcopy
1524      */
1525     wait_for_migration_status(from, "postcopy-active", NULL);
1526 
1527     /*
1528      * Manually stop the postcopy migration. This emulates a network
1529      * failure with the migration socket
1530      */
1531     migrate_pause(from);
1532 
1533     /*
1534      * Wait for destination side to reach postcopy-paused state.  The
1535      * migrate-recover command can only succeed if destination machine
1536      * is in the paused state
1537      */
1538     wait_for_postcopy_status(to, "postcopy-paused");
1539     wait_for_postcopy_status(from, "postcopy-paused");
1540 
1541     if (args->postcopy_recovery_fail_stage) {
1542         /*
1543          * Test when a wrong socket specified for recover, and then the
1544          * ability to kick it out, and continue with a correct socket.
1545          */
1546         postcopy_recover_fail(from, to, args->postcopy_recovery_fail_stage);
1547         /* continue with a good recovery */
1548     }
1549 
1550     /*
1551      * Create a new socket to emulate a new channel that is different
1552      * from the broken migration channel; tell the destination to
1553      * listen to the new port
1554      */
1555     uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
1556     migrate_recover(to, uri);
1557 
1558     /*
1559      * Try to rebuild the migration channel using the resume flag and
1560      * the newly created channel
1561      */
1562     migrate_qmp(from, to, uri, NULL, "{'resume': true}");
1563 
1564     /* Restore the postcopy bandwidth to unlimited */
1565     migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
1566 
1567     migrate_postcopy_complete(from, to, args);
1568 }
1569 
1570 static void test_postcopy_recovery(void)
1571 {
1572     MigrateCommon args = { };
1573 
1574     test_postcopy_recovery_common(&args);
1575 }
1576 
1577 static void test_postcopy_recovery_fail_handshake(void)
1578 {
1579     MigrateCommon args = {
1580         .postcopy_recovery_fail_stage = POSTCOPY_FAIL_RECOVERY,
1581     };
1582 
1583     test_postcopy_recovery_common(&args);
1584 }
1585 
1586 static void test_postcopy_recovery_fail_reconnect(void)
1587 {
1588     MigrateCommon args = {
1589         .postcopy_recovery_fail_stage = POSTCOPY_FAIL_CHANNEL_ESTABLISH,
1590     };
1591 
1592     test_postcopy_recovery_common(&args);
1593 }
1594 
1595 #ifdef CONFIG_GNUTLS
1596 static void test_postcopy_recovery_tls_psk(void)
1597 {
1598     MigrateCommon args = {
1599         .start_hook = migrate_hook_start_tls_psk_match,
1600         .end_hook = migrate_hook_end_tls_psk,
1601     };
1602 
1603     test_postcopy_recovery_common(&args);
1604 }
1605 #endif
1606 
1607 static void test_postcopy_preempt_recovery(void)
1608 {
1609     MigrateCommon args = {
1610         .postcopy_preempt = true,
1611     };
1612 
1613     test_postcopy_recovery_common(&args);
1614 }
1615 
1616 #ifdef CONFIG_GNUTLS
1617 /* This contains preempt+recovery+tls test altogether */
1618 static void test_postcopy_preempt_all(void)
1619 {
1620     MigrateCommon args = {
1621         .postcopy_preempt = true,
1622         .start_hook = migrate_hook_start_tls_psk_match,
1623         .end_hook = migrate_hook_end_tls_psk,
1624     };
1625 
1626     test_postcopy_recovery_common(&args);
1627 }
1628 
1629 #endif
1630 
1631 static void test_baddest(void)
1632 {
1633     MigrateStart args = {
1634         .hide_stderr = true
1635     };
1636     QTestState *from, *to;
1637 
1638     if (migrate_start(&from, &to, "tcp:127.0.0.1:0", &args)) {
1639         return;
1640     }
1641     migrate_qmp(from, to, "tcp:127.0.0.1:0", NULL, "{}");
1642     wait_for_migration_fail(from, false);
1643     migrate_end(from, to, false);
1644 }
1645 
1646 #ifndef _WIN32
1647 static void test_analyze_script(void)
1648 {
1649     MigrateStart args = {
1650         .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
1651     };
1652     QTestState *from, *to;
1653     g_autofree char *uri = NULL;
1654     g_autofree char *file = NULL;
1655     int pid, wstatus;
1656     const char *python = g_getenv("PYTHON");
1657 
1658     if (!python) {
1659         g_test_skip("PYTHON variable not set");
1660         return;
1661     }
1662 
1663     /* dummy url */
1664     if (migrate_start(&from, &to, "tcp:127.0.0.1:0", &args)) {
1665         return;
1666     }
1667 
1668     /*
1669      * Setting these two capabilities causes the "configuration"
1670      * vmstate to include subsections for them. The script needs to
1671      * parse those subsections properly.
1672      */
1673     migrate_set_capability(from, "validate-uuid", true);
1674     migrate_set_capability(from, "x-ignore-shared", true);
1675 
1676     file = g_strdup_printf("%s/migfile", tmpfs);
1677     uri = g_strdup_printf("exec:cat > %s", file);
1678 
1679     migrate_ensure_converge(from);
1680     migrate_qmp(from, to, uri, NULL, "{}");
1681     wait_for_migration_complete(from);
1682 
1683     pid = fork();
1684     if (!pid) {
1685         close(1);
1686         open("/dev/null", O_WRONLY);
1687         execl(python, python, ANALYZE_SCRIPT, "-f", file, NULL);
1688         g_assert_not_reached();
1689     }
1690 
1691     g_assert(waitpid(pid, &wstatus, 0) == pid);
1692     if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus) != 0) {
1693         g_test_message("Failed to analyze the migration stream");
1694         g_test_fail();
1695     }
1696     migrate_end(from, to, false);
1697     cleanup("migfile");
1698 }
1699 #endif
1700 
1701 static void test_precopy_common(MigrateCommon *args)
1702 {
1703     QTestState *from, *to;
1704     void *data_hook = NULL;
1705 
1706     if (migrate_start(&from, &to, args->listen_uri, &args->start)) {
1707         return;
1708     }
1709 
1710     if (args->start_hook) {
1711         data_hook = args->start_hook(from, to);
1712     }
1713 
1714     /* Wait for the first serial output from the source */
1715     if (args->result == MIG_TEST_SUCCEED) {
1716         wait_for_serial("src_serial");
1717         wait_for_suspend(from, &src_state);
1718     }
1719 
1720     if (args->live) {
1721         migrate_ensure_non_converge(from);
1722         migrate_prepare_for_dirty_mem(from);
1723     } else {
1724         /*
1725          * Testing non-live migration, we allow it to run at
1726          * full speed to ensure short test case duration.
1727          * For tests expected to fail, we don't need to
1728          * change anything.
1729          */
1730         if (args->result == MIG_TEST_SUCCEED) {
1731             qtest_qmp_assert_success(from, "{ 'execute' : 'stop'}");
1732             wait_for_stop(from, &src_state);
1733             migrate_ensure_converge(from);
1734         }
1735     }
1736 
1737     if (args->result == MIG_TEST_QMP_ERROR) {
1738         migrate_qmp_fail(from, args->connect_uri, args->connect_channels, "{}");
1739         goto finish;
1740     }
1741 
1742     migrate_qmp(from, to, args->connect_uri, args->connect_channels, "{}");
1743 
1744     if (args->result != MIG_TEST_SUCCEED) {
1745         bool allow_active = args->result == MIG_TEST_FAIL;
1746         wait_for_migration_fail(from, allow_active);
1747 
1748         if (args->result == MIG_TEST_FAIL_DEST_QUIT_ERR) {
1749             qtest_set_expected_status(to, EXIT_FAILURE);
1750         }
1751     } else {
1752         if (args->live) {
1753             /*
1754              * For initial iteration(s) we must do a full pass,
1755              * but for the final iteration, we need only wait
1756              * for some dirty mem before switching to converge
1757              */
1758             while (args->iterations > 1) {
1759                 wait_for_migration_pass(from);
1760                 args->iterations--;
1761             }
1762             migrate_wait_for_dirty_mem(from, to);
1763 
1764             migrate_ensure_converge(from);
1765 
1766             /*
1767              * We do this first, as it has a timeout to stop us
1768              * hanging forever if migration didn't converge
1769              */
1770             wait_for_migration_complete(from);
1771 
1772             wait_for_stop(from, &src_state);
1773 
1774         } else {
1775             wait_for_migration_complete(from);
1776             /*
1777              * Must wait for dst to finish reading all incoming
1778              * data on the socket before issuing 'cont' otherwise
1779              * it'll be ignored
1780              */
1781             wait_for_migration_complete(to);
1782 
1783             qtest_qmp_assert_success(to, "{ 'execute' : 'cont'}");
1784         }
1785 
1786         wait_for_resume(to, &dst_state);
1787 
1788         if (args->start.suspend_me) {
1789             /* wakeup succeeds only if guest is suspended */
1790             qtest_qmp_assert_success(to, "{'execute': 'system_wakeup'}");
1791         }
1792 
1793         wait_for_serial("dest_serial");
1794     }
1795 
1796 finish:
1797     if (args->end_hook) {
1798         args->end_hook(from, to, data_hook);
1799     }
1800 
1801     migrate_end(from, to, args->result == MIG_TEST_SUCCEED);
1802 }
1803 
1804 static void file_dirty_offset_region(void)
1805 {
1806     g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, FILE_TEST_FILENAME);
1807     size_t size = FILE_TEST_OFFSET;
1808     g_autofree char *data = g_new0(char, size);
1809 
1810     memset(data, FILE_TEST_MARKER, size);
1811     g_assert(g_file_set_contents(path, data, size, NULL));
1812 }
1813 
1814 static void file_check_offset_region(void)
1815 {
1816     g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, FILE_TEST_FILENAME);
1817     size_t size = FILE_TEST_OFFSET;
1818     g_autofree char *expected = g_new0(char, size);
1819     g_autofree char *actual = NULL;
1820     uint64_t *stream_start;
1821 
1822     /*
1823      * Ensure the skipped offset region's data has not been touched
1824      * and the migration stream starts at the right place.
1825      */
1826 
1827     memset(expected, FILE_TEST_MARKER, size);
1828 
1829     g_assert(g_file_get_contents(path, &actual, NULL, NULL));
1830     g_assert(!memcmp(actual, expected, size));
1831 
1832     stream_start = (uint64_t *)(actual + size);
1833     g_assert_cmpint(cpu_to_be64(*stream_start) >> 32, ==, QEMU_VM_FILE_MAGIC);
1834 }
1835 
1836 static void test_file_common(MigrateCommon *args, bool stop_src)
1837 {
1838     QTestState *from, *to;
1839     void *data_hook = NULL;
1840     bool check_offset = false;
1841 
1842     if (migrate_start(&from, &to, args->listen_uri, &args->start)) {
1843         return;
1844     }
1845 
1846     /*
1847      * File migration is never live. We can keep the source VM running
1848      * during migration, but the destination will not be running
1849      * concurrently.
1850      */
1851     g_assert_false(args->live);
1852 
1853     if (g_strrstr(args->connect_uri, "offset=")) {
1854         check_offset = true;
1855         /*
1856          * This comes before the start_hook because it's equivalent to
1857          * a management application creating the file and writing to
1858          * it so hooks should expect the file to be already present.
1859          */
1860         file_dirty_offset_region();
1861     }
1862 
1863     if (args->start_hook) {
1864         data_hook = args->start_hook(from, to);
1865     }
1866 
1867     migrate_ensure_converge(from);
1868     wait_for_serial("src_serial");
1869 
1870     if (stop_src) {
1871         qtest_qmp_assert_success(from, "{ 'execute' : 'stop'}");
1872         wait_for_stop(from, &src_state);
1873     }
1874 
1875     if (args->result == MIG_TEST_QMP_ERROR) {
1876         migrate_qmp_fail(from, args->connect_uri, NULL, "{}");
1877         goto finish;
1878     }
1879 
1880     migrate_qmp(from, to, args->connect_uri, NULL, "{}");
1881     wait_for_migration_complete(from);
1882 
1883     /*
1884      * We need to wait for the source to finish before starting the
1885      * destination.
1886      */
1887     migrate_incoming_qmp(to, args->connect_uri, "{}");
1888     wait_for_migration_complete(to);
1889 
1890     if (stop_src) {
1891         qtest_qmp_assert_success(to, "{ 'execute' : 'cont'}");
1892     }
1893     wait_for_resume(to, &dst_state);
1894 
1895     wait_for_serial("dest_serial");
1896 
1897     if (check_offset) {
1898         file_check_offset_region();
1899     }
1900 
1901 finish:
1902     if (args->end_hook) {
1903         args->end_hook(from, to, data_hook);
1904     }
1905 
1906     migrate_end(from, to, args->result == MIG_TEST_SUCCEED);
1907 }
1908 
1909 static void test_precopy_unix_plain(void)
1910 {
1911     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1912     MigrateCommon args = {
1913         .listen_uri = uri,
1914         .connect_uri = uri,
1915         /*
1916          * The simplest use case of precopy, covering smoke tests of
1917          * get-dirty-log dirty tracking.
1918          */
1919         .live = true,
1920     };
1921 
1922     test_precopy_common(&args);
1923 }
1924 
1925 static void test_precopy_unix_suspend_live(void)
1926 {
1927     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1928     MigrateCommon args = {
1929         .listen_uri = uri,
1930         .connect_uri = uri,
1931         /*
1932          * despite being live, the test is fast because the src
1933          * suspends immediately.
1934          */
1935         .live = true,
1936         .start.suspend_me = true,
1937     };
1938 
1939     test_precopy_common(&args);
1940 }
1941 
1942 static void test_precopy_unix_suspend_notlive(void)
1943 {
1944     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1945     MigrateCommon args = {
1946         .listen_uri = uri,
1947         .connect_uri = uri,
1948         .start.suspend_me = true,
1949     };
1950 
1951     test_precopy_common(&args);
1952 }
1953 
1954 static void test_precopy_unix_dirty_ring(void)
1955 {
1956     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1957     MigrateCommon args = {
1958         .start = {
1959             .use_dirty_ring = true,
1960         },
1961         .listen_uri = uri,
1962         .connect_uri = uri,
1963         /*
1964          * Besides the precopy/unix basic test, cover dirty ring interface
1965          * rather than get-dirty-log.
1966          */
1967         .live = true,
1968     };
1969 
1970     test_precopy_common(&args);
1971 }
1972 
1973 #ifdef CONFIG_GNUTLS
1974 static void test_precopy_unix_tls_psk(void)
1975 {
1976     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1977     MigrateCommon args = {
1978         .connect_uri = uri,
1979         .listen_uri = uri,
1980         .start_hook = migrate_hook_start_tls_psk_match,
1981         .end_hook = migrate_hook_end_tls_psk,
1982     };
1983 
1984     test_precopy_common(&args);
1985 }
1986 
1987 #ifdef CONFIG_TASN1
1988 static void test_precopy_unix_tls_x509_default_host(void)
1989 {
1990     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1991     MigrateCommon args = {
1992         .start = {
1993             .hide_stderr = true,
1994         },
1995         .connect_uri = uri,
1996         .listen_uri = uri,
1997         .start_hook = migrate_hook_start_tls_x509_default_host,
1998         .end_hook = migrate_hook_end_tls_x509,
1999         .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
2000     };
2001 
2002     test_precopy_common(&args);
2003 }
2004 
2005 static void test_precopy_unix_tls_x509_override_host(void)
2006 {
2007     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
2008     MigrateCommon args = {
2009         .connect_uri = uri,
2010         .listen_uri = uri,
2011         .start_hook = migrate_hook_start_tls_x509_override_host,
2012         .end_hook = migrate_hook_end_tls_x509,
2013     };
2014 
2015     test_precopy_common(&args);
2016 }
2017 #endif /* CONFIG_TASN1 */
2018 #endif /* CONFIG_GNUTLS */
2019 
2020 #if 0
2021 /* Currently upset on aarch64 TCG */
2022 static void test_ignore_shared(void)
2023 {
2024     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
2025     QTestState *from, *to;
2026 
2027     if (migrate_start(&from, &to, uri, false, true, NULL, NULL)) {
2028         return;
2029     }
2030 
2031     migrate_ensure_non_converge(from);
2032     migrate_prepare_for_dirty_mem(from);
2033 
2034     migrate_set_capability(from, "x-ignore-shared", true);
2035     migrate_set_capability(to, "x-ignore-shared", true);
2036 
2037     /* Wait for the first serial output from the source */
2038     wait_for_serial("src_serial");
2039 
2040     migrate_qmp(from, to, uri, NULL, "{}");
2041 
2042     migrate_wait_for_dirty_mem(from, to);
2043 
2044     wait_for_stop(from, &src_state);
2045 
2046     qtest_qmp_eventwait(to, "RESUME");
2047 
2048     wait_for_serial("dest_serial");
2049     wait_for_migration_complete(from);
2050 
2051     /* Check whether shared RAM has been really skipped */
2052     g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
2053 
2054     migrate_end(from, to, true);
2055 }
2056 #endif
2057 
2058 static void *
2059 migrate_hook_start_xbzrle(QTestState *from,
2060                           QTestState *to)
2061 {
2062     migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
2063 
2064     migrate_set_capability(from, "xbzrle", true);
2065     migrate_set_capability(to, "xbzrle", true);
2066 
2067     return NULL;
2068 }
2069 
2070 static void test_precopy_unix_xbzrle(void)
2071 {
2072     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
2073     MigrateCommon args = {
2074         .connect_uri = uri,
2075         .listen_uri = uri,
2076         .start_hook = migrate_hook_start_xbzrle,
2077         .iterations = 2,
2078         /*
2079          * XBZRLE needs pages to be modified when doing the 2nd+ round
2080          * iteration to have real data pushed to the stream.
2081          */
2082         .live = true,
2083     };
2084 
2085     test_precopy_common(&args);
2086 }
2087 
2088 static void test_precopy_file(void)
2089 {
2090     g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
2091                                            FILE_TEST_FILENAME);
2092     MigrateCommon args = {
2093         .connect_uri = uri,
2094         .listen_uri = "defer",
2095     };
2096 
2097     test_file_common(&args, true);
2098 }
2099 
2100 #ifndef _WIN32
2101 static void fdset_add_fds(QTestState *qts, const char *file, int flags,
2102                           int num_fds, bool direct_io)
2103 {
2104     for (int i = 0; i < num_fds; i++) {
2105         int fd;
2106 
2107 #ifdef O_DIRECT
2108         /* only secondary channels can use direct-io */
2109         if (direct_io && i != 0) {
2110             flags |= O_DIRECT;
2111         }
2112 #endif
2113 
2114         fd = open(file, flags, 0660);
2115         assert(fd != -1);
2116 
2117         qtest_qmp_fds_assert_success(qts, &fd, 1, "{'execute': 'add-fd', "
2118                                      "'arguments': {'fdset-id': 1}}");
2119         close(fd);
2120     }
2121 }
2122 
2123 static void *migrate_hook_start_file_offset_fdset(QTestState *from,
2124                                                   QTestState *to)
2125 {
2126     g_autofree char *file = g_strdup_printf("%s/%s", tmpfs, FILE_TEST_FILENAME);
2127 
2128     fdset_add_fds(from, file, O_WRONLY, 1, false);
2129     fdset_add_fds(to, file, O_RDONLY, 1, false);
2130 
2131     return NULL;
2132 }
2133 
2134 static void test_precopy_file_offset_fdset(void)
2135 {
2136     g_autofree char *uri = g_strdup_printf("file:/dev/fdset/1,offset=%d",
2137                                            FILE_TEST_OFFSET);
2138     MigrateCommon args = {
2139         .connect_uri = uri,
2140         .listen_uri = "defer",
2141         .start_hook = migrate_hook_start_file_offset_fdset,
2142     };
2143 
2144     test_file_common(&args, false);
2145 }
2146 #endif
2147 
2148 static void test_precopy_file_offset(void)
2149 {
2150     g_autofree char *uri = g_strdup_printf("file:%s/%s,offset=%d", tmpfs,
2151                                            FILE_TEST_FILENAME,
2152                                            FILE_TEST_OFFSET);
2153     MigrateCommon args = {
2154         .connect_uri = uri,
2155         .listen_uri = "defer",
2156     };
2157 
2158     test_file_common(&args, false);
2159 }
2160 
2161 static void test_precopy_file_offset_bad(void)
2162 {
2163     /* using a value not supported by qemu_strtosz() */
2164     g_autofree char *uri = g_strdup_printf("file:%s/%s,offset=0x20M",
2165                                            tmpfs, FILE_TEST_FILENAME);
2166     MigrateCommon args = {
2167         .connect_uri = uri,
2168         .listen_uri = "defer",
2169         .result = MIG_TEST_QMP_ERROR,
2170     };
2171 
2172     test_file_common(&args, false);
2173 }
2174 
2175 static void *migrate_hook_start_mode_reboot(QTestState *from, QTestState *to)
2176 {
2177     migrate_set_parameter_str(from, "mode", "cpr-reboot");
2178     migrate_set_parameter_str(to, "mode", "cpr-reboot");
2179 
2180     migrate_set_capability(from, "x-ignore-shared", true);
2181     migrate_set_capability(to, "x-ignore-shared", true);
2182 
2183     return NULL;
2184 }
2185 
2186 static void *migrate_hook_start_mapped_ram(QTestState *from, QTestState *to)
2187 {
2188     migrate_set_capability(from, "mapped-ram", true);
2189     migrate_set_capability(to, "mapped-ram", true);
2190 
2191     return NULL;
2192 }
2193 
2194 static void test_mode_reboot(void)
2195 {
2196     g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
2197                                            FILE_TEST_FILENAME);
2198     MigrateCommon args = {
2199         .start.use_shmem = true,
2200         .connect_uri = uri,
2201         .listen_uri = "defer",
2202         .start_hook = migrate_hook_start_mode_reboot,
2203     };
2204 
2205     test_file_common(&args, true);
2206 }
2207 
2208 static void test_precopy_file_mapped_ram_live(void)
2209 {
2210     g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
2211                                            FILE_TEST_FILENAME);
2212     MigrateCommon args = {
2213         .connect_uri = uri,
2214         .listen_uri = "defer",
2215         .start_hook = migrate_hook_start_mapped_ram,
2216     };
2217 
2218     test_file_common(&args, false);
2219 }
2220 
2221 static void test_precopy_file_mapped_ram(void)
2222 {
2223     g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
2224                                            FILE_TEST_FILENAME);
2225     MigrateCommon args = {
2226         .connect_uri = uri,
2227         .listen_uri = "defer",
2228         .start_hook = migrate_hook_start_mapped_ram,
2229     };
2230 
2231     test_file_common(&args, true);
2232 }
2233 
2234 static void *migrate_hook_start_multifd_mapped_ram(QTestState *from,
2235                                                    QTestState *to)
2236 {
2237     migrate_hook_start_mapped_ram(from, to);
2238 
2239     migrate_set_parameter_int(from, "multifd-channels", 4);
2240     migrate_set_parameter_int(to, "multifd-channels", 4);
2241 
2242     migrate_set_capability(from, "multifd", true);
2243     migrate_set_capability(to, "multifd", true);
2244 
2245     return NULL;
2246 }
2247 
2248 static void test_multifd_file_mapped_ram_live(void)
2249 {
2250     g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
2251                                            FILE_TEST_FILENAME);
2252     MigrateCommon args = {
2253         .connect_uri = uri,
2254         .listen_uri = "defer",
2255         .start_hook = migrate_hook_start_multifd_mapped_ram,
2256     };
2257 
2258     test_file_common(&args, false);
2259 }
2260 
2261 static void test_multifd_file_mapped_ram(void)
2262 {
2263     g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
2264                                            FILE_TEST_FILENAME);
2265     MigrateCommon args = {
2266         .connect_uri = uri,
2267         .listen_uri = "defer",
2268         .start_hook = migrate_hook_start_multifd_mapped_ram,
2269     };
2270 
2271     test_file_common(&args, true);
2272 }
2273 
2274 static void *migrate_hook_start_multifd_mapped_ram_dio(QTestState *from,
2275                                                        QTestState *to)
2276 {
2277     migrate_hook_start_multifd_mapped_ram(from, to);
2278 
2279     migrate_set_parameter_bool(from, "direct-io", true);
2280     migrate_set_parameter_bool(to, "direct-io", true);
2281 
2282     return NULL;
2283 }
2284 
2285 static void test_multifd_file_mapped_ram_dio(void)
2286 {
2287     g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
2288                                            FILE_TEST_FILENAME);
2289     MigrateCommon args = {
2290         .connect_uri = uri,
2291         .listen_uri = "defer",
2292         .start_hook = migrate_hook_start_multifd_mapped_ram_dio,
2293     };
2294 
2295     if (!probe_o_direct_support(tmpfs)) {
2296         g_test_skip("Filesystem does not support O_DIRECT");
2297         return;
2298     }
2299 
2300     test_file_common(&args, true);
2301 }
2302 
2303 #ifndef _WIN32
2304 static void migrate_hook_end_multifd_mapped_ram_fdset(QTestState *from,
2305                                                       QTestState *to,
2306                                                       void *opaque)
2307 {
2308     QDict *resp;
2309     QList *fdsets;
2310 
2311     /*
2312      * Remove the fdsets after migration, otherwise a second migration
2313      * would fail due fdset reuse.
2314      */
2315     qtest_qmp_assert_success(from, "{'execute': 'remove-fd', "
2316                              "'arguments': { 'fdset-id': 1}}");
2317 
2318     /*
2319      * Make sure no fdsets are left after migration, otherwise a
2320      * second migration would fail due fdset reuse.
2321      */
2322     resp = qtest_qmp(from, "{'execute': 'query-fdsets', "
2323                      "'arguments': {}}");
2324     g_assert(qdict_haskey(resp, "return"));
2325     fdsets = qdict_get_qlist(resp, "return");
2326     g_assert(fdsets && qlist_empty(fdsets));
2327     qobject_unref(resp);
2328 }
2329 
2330 static void *migrate_hook_start_multifd_mapped_ram_fdset_dio(QTestState *from,
2331                                                              QTestState *to)
2332 {
2333     g_autofree char *file = g_strdup_printf("%s/%s", tmpfs, FILE_TEST_FILENAME);
2334 
2335     fdset_add_fds(from, file, O_WRONLY, 2, true);
2336     fdset_add_fds(to, file, O_RDONLY, 2, true);
2337 
2338     migrate_hook_start_multifd_mapped_ram(from, to);
2339     migrate_set_parameter_bool(from, "direct-io", true);
2340     migrate_set_parameter_bool(to, "direct-io", true);
2341 
2342     return NULL;
2343 }
2344 
2345 static void *migrate_hook_start_multifd_mapped_ram_fdset(QTestState *from,
2346                                                          QTestState *to)
2347 {
2348     g_autofree char *file = g_strdup_printf("%s/%s", tmpfs, FILE_TEST_FILENAME);
2349 
2350     fdset_add_fds(from, file, O_WRONLY, 2, false);
2351     fdset_add_fds(to, file, O_RDONLY, 2, false);
2352 
2353     migrate_hook_start_multifd_mapped_ram(from, to);
2354 
2355     return NULL;
2356 }
2357 
2358 static void test_multifd_file_mapped_ram_fdset(void)
2359 {
2360     g_autofree char *uri = g_strdup_printf("file:/dev/fdset/1,offset=%d",
2361                                            FILE_TEST_OFFSET);
2362     MigrateCommon args = {
2363         .connect_uri = uri,
2364         .listen_uri = "defer",
2365         .start_hook = migrate_hook_start_multifd_mapped_ram_fdset,
2366         .end_hook = migrate_hook_end_multifd_mapped_ram_fdset,
2367     };
2368 
2369     test_file_common(&args, true);
2370 }
2371 
2372 static void test_multifd_file_mapped_ram_fdset_dio(void)
2373 {
2374     g_autofree char *uri = g_strdup_printf("file:/dev/fdset/1,offset=%d",
2375                                            FILE_TEST_OFFSET);
2376     MigrateCommon args = {
2377         .connect_uri = uri,
2378         .listen_uri = "defer",
2379         .start_hook = migrate_hook_start_multifd_mapped_ram_fdset_dio,
2380         .end_hook = migrate_hook_end_multifd_mapped_ram_fdset,
2381     };
2382 
2383     if (!probe_o_direct_support(tmpfs)) {
2384         g_test_skip("Filesystem does not support O_DIRECT");
2385         return;
2386     }
2387 
2388     test_file_common(&args, true);
2389 }
2390 #endif /* !_WIN32 */
2391 
2392 static void test_precopy_tcp_plain(void)
2393 {
2394     MigrateCommon args = {
2395         .listen_uri = "tcp:127.0.0.1:0",
2396     };
2397 
2398     test_precopy_common(&args);
2399 }
2400 
2401 static void *migrate_hook_start_switchover_ack(QTestState *from, QTestState *to)
2402 {
2403 
2404     migrate_set_capability(from, "return-path", true);
2405     migrate_set_capability(to, "return-path", true);
2406 
2407     migrate_set_capability(from, "switchover-ack", true);
2408     migrate_set_capability(to, "switchover-ack", true);
2409 
2410     return NULL;
2411 }
2412 
2413 static void test_precopy_tcp_switchover_ack(void)
2414 {
2415     MigrateCommon args = {
2416         .listen_uri = "tcp:127.0.0.1:0",
2417         .start_hook = migrate_hook_start_switchover_ack,
2418         /*
2419          * Source VM must be running in order to consider the switchover ACK
2420          * when deciding to do switchover or not.
2421          */
2422         .live = true,
2423     };
2424 
2425     test_precopy_common(&args);
2426 }
2427 
2428 #ifdef CONFIG_GNUTLS
2429 static void test_precopy_tcp_tls_psk_match(void)
2430 {
2431     MigrateCommon args = {
2432         .listen_uri = "tcp:127.0.0.1:0",
2433         .start_hook = migrate_hook_start_tls_psk_match,
2434         .end_hook = migrate_hook_end_tls_psk,
2435     };
2436 
2437     test_precopy_common(&args);
2438 }
2439 
2440 static void test_precopy_tcp_tls_psk_mismatch(void)
2441 {
2442     MigrateCommon args = {
2443         .start = {
2444             .hide_stderr = true,
2445         },
2446         .listen_uri = "tcp:127.0.0.1:0",
2447         .start_hook = migrate_hook_start_tls_psk_mismatch,
2448         .end_hook = migrate_hook_end_tls_psk,
2449         .result = MIG_TEST_FAIL,
2450     };
2451 
2452     test_precopy_common(&args);
2453 }
2454 
2455 #ifdef CONFIG_TASN1
2456 static void test_precopy_tcp_tls_x509_default_host(void)
2457 {
2458     MigrateCommon args = {
2459         .listen_uri = "tcp:127.0.0.1:0",
2460         .start_hook = migrate_hook_start_tls_x509_default_host,
2461         .end_hook = migrate_hook_end_tls_x509,
2462     };
2463 
2464     test_precopy_common(&args);
2465 }
2466 
2467 static void test_precopy_tcp_tls_x509_override_host(void)
2468 {
2469     MigrateCommon args = {
2470         .listen_uri = "tcp:127.0.0.1:0",
2471         .start_hook = migrate_hook_start_tls_x509_override_host,
2472         .end_hook = migrate_hook_end_tls_x509,
2473     };
2474 
2475     test_precopy_common(&args);
2476 }
2477 
2478 static void test_precopy_tcp_tls_x509_mismatch_host(void)
2479 {
2480     MigrateCommon args = {
2481         .start = {
2482             .hide_stderr = true,
2483         },
2484         .listen_uri = "tcp:127.0.0.1:0",
2485         .start_hook = migrate_hook_start_tls_x509_mismatch_host,
2486         .end_hook = migrate_hook_end_tls_x509,
2487         .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
2488     };
2489 
2490     test_precopy_common(&args);
2491 }
2492 
2493 static void test_precopy_tcp_tls_x509_friendly_client(void)
2494 {
2495     MigrateCommon args = {
2496         .listen_uri = "tcp:127.0.0.1:0",
2497         .start_hook = migrate_hook_start_tls_x509_friendly_client,
2498         .end_hook = migrate_hook_end_tls_x509,
2499     };
2500 
2501     test_precopy_common(&args);
2502 }
2503 
2504 static void test_precopy_tcp_tls_x509_hostile_client(void)
2505 {
2506     MigrateCommon args = {
2507         .start = {
2508             .hide_stderr = true,
2509         },
2510         .listen_uri = "tcp:127.0.0.1:0",
2511         .start_hook = migrate_hook_start_tls_x509_hostile_client,
2512         .end_hook = migrate_hook_end_tls_x509,
2513         .result = MIG_TEST_FAIL,
2514     };
2515 
2516     test_precopy_common(&args);
2517 }
2518 
2519 static void test_precopy_tcp_tls_x509_allow_anon_client(void)
2520 {
2521     MigrateCommon args = {
2522         .listen_uri = "tcp:127.0.0.1:0",
2523         .start_hook = migrate_hook_start_tls_x509_allow_anon_client,
2524         .end_hook = migrate_hook_end_tls_x509,
2525     };
2526 
2527     test_precopy_common(&args);
2528 }
2529 
2530 static void test_precopy_tcp_tls_x509_reject_anon_client(void)
2531 {
2532     MigrateCommon args = {
2533         .start = {
2534             .hide_stderr = true,
2535         },
2536         .listen_uri = "tcp:127.0.0.1:0",
2537         .start_hook = migrate_hook_start_tls_x509_reject_anon_client,
2538         .end_hook = migrate_hook_end_tls_x509,
2539         .result = MIG_TEST_FAIL,
2540     };
2541 
2542     test_precopy_common(&args);
2543 }
2544 #endif /* CONFIG_TASN1 */
2545 #endif /* CONFIG_GNUTLS */
2546 
2547 #ifndef _WIN32
2548 static void *migrate_hook_start_fd(QTestState *from,
2549                                    QTestState *to)
2550 {
2551     int ret;
2552     int pair[2];
2553 
2554     /* Create two connected sockets for migration */
2555     ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
2556     g_assert_cmpint(ret, ==, 0);
2557 
2558     /* Send the 1st socket to the target */
2559     qtest_qmp_fds_assert_success(to, &pair[0], 1,
2560                                  "{ 'execute': 'getfd',"
2561                                  "  'arguments': { 'fdname': 'fd-mig' }}");
2562     close(pair[0]);
2563 
2564     /* Start incoming migration from the 1st socket */
2565     migrate_incoming_qmp(to, "fd:fd-mig", "{}");
2566 
2567     /* Send the 2nd socket to the target */
2568     qtest_qmp_fds_assert_success(from, &pair[1], 1,
2569                                  "{ 'execute': 'getfd',"
2570                                  "  'arguments': { 'fdname': 'fd-mig' }}");
2571     close(pair[1]);
2572 
2573     return NULL;
2574 }
2575 
2576 static void migrate_hook_end_fd(QTestState *from,
2577                                 QTestState *to,
2578                                 void *opaque)
2579 {
2580     QDict *rsp;
2581     const char *error_desc;
2582 
2583     /* Test closing fds */
2584     /* We assume, that QEMU removes named fd from its list,
2585      * so this should fail */
2586     rsp = qtest_qmp(from,
2587                     "{ 'execute': 'closefd',"
2588                     "  'arguments': { 'fdname': 'fd-mig' }}");
2589     g_assert_true(qdict_haskey(rsp, "error"));
2590     error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
2591     g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
2592     qobject_unref(rsp);
2593 
2594     rsp = qtest_qmp(to,
2595                     "{ 'execute': 'closefd',"
2596                     "  'arguments': { 'fdname': 'fd-mig' }}");
2597     g_assert_true(qdict_haskey(rsp, "error"));
2598     error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
2599     g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
2600     qobject_unref(rsp);
2601 }
2602 
2603 static void test_precopy_fd_socket(void)
2604 {
2605     MigrateCommon args = {
2606         .listen_uri = "defer",
2607         .connect_uri = "fd:fd-mig",
2608         .start_hook = migrate_hook_start_fd,
2609         .end_hook = migrate_hook_end_fd,
2610     };
2611     test_precopy_common(&args);
2612 }
2613 
2614 static void *migrate_hook_start_precopy_fd_file(QTestState *from, QTestState *to)
2615 {
2616     g_autofree char *file = g_strdup_printf("%s/%s", tmpfs, FILE_TEST_FILENAME);
2617     int src_flags = O_CREAT | O_RDWR;
2618     int dst_flags = O_CREAT | O_RDWR;
2619     int fds[2];
2620 
2621     fds[0] = open(file, src_flags, 0660);
2622     assert(fds[0] != -1);
2623 
2624     fds[1] = open(file, dst_flags, 0660);
2625     assert(fds[1] != -1);
2626 
2627 
2628     qtest_qmp_fds_assert_success(to, &fds[0], 1,
2629                                  "{ 'execute': 'getfd',"
2630                                  "  'arguments': { 'fdname': 'fd-mig' }}");
2631 
2632     qtest_qmp_fds_assert_success(from, &fds[1], 1,
2633                                  "{ 'execute': 'getfd',"
2634                                  "  'arguments': { 'fdname': 'fd-mig' }}");
2635 
2636     close(fds[0]);
2637     close(fds[1]);
2638 
2639     return NULL;
2640 }
2641 
2642 static void test_precopy_fd_file(void)
2643 {
2644     MigrateCommon args = {
2645         .listen_uri = "defer",
2646         .connect_uri = "fd:fd-mig",
2647         .start_hook = migrate_hook_start_precopy_fd_file,
2648         .end_hook = migrate_hook_end_fd,
2649     };
2650     test_file_common(&args, true);
2651 }
2652 #endif /* _WIN32 */
2653 
2654 static void do_test_validate_uuid(MigrateStart *args, bool should_fail)
2655 {
2656     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
2657     QTestState *from, *to;
2658 
2659     if (migrate_start(&from, &to, uri, args)) {
2660         return;
2661     }
2662 
2663     /*
2664      * UUID validation is at the begin of migration. So, the main process of
2665      * migration is not interesting for us here. Thus, set huge downtime for
2666      * very fast migration.
2667      */
2668     migrate_set_parameter_int(from, "downtime-limit", 1000000);
2669     migrate_set_capability(from, "validate-uuid", true);
2670 
2671     /* Wait for the first serial output from the source */
2672     wait_for_serial("src_serial");
2673 
2674     migrate_qmp(from, to, uri, NULL, "{}");
2675 
2676     if (should_fail) {
2677         qtest_set_expected_status(to, EXIT_FAILURE);
2678         wait_for_migration_fail(from, true);
2679     } else {
2680         wait_for_migration_complete(from);
2681     }
2682 
2683     migrate_end(from, to, false);
2684 }
2685 
2686 static void test_validate_uuid(void)
2687 {
2688     MigrateStart args = {
2689         .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
2690         .opts_target = "-uuid 11111111-1111-1111-1111-111111111111",
2691     };
2692 
2693     do_test_validate_uuid(&args, false);
2694 }
2695 
2696 static void test_validate_uuid_error(void)
2697 {
2698     MigrateStart args = {
2699         .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
2700         .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
2701         .hide_stderr = true,
2702     };
2703 
2704     do_test_validate_uuid(&args, true);
2705 }
2706 
2707 static void test_validate_uuid_src_not_set(void)
2708 {
2709     MigrateStart args = {
2710         .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
2711         .hide_stderr = true,
2712     };
2713 
2714     do_test_validate_uuid(&args, false);
2715 }
2716 
2717 static void test_validate_uuid_dst_not_set(void)
2718 {
2719     MigrateStart args = {
2720         .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
2721         .hide_stderr = true,
2722     };
2723 
2724     do_test_validate_uuid(&args, false);
2725 }
2726 
2727 static void do_test_validate_uri_channel(MigrateCommon *args)
2728 {
2729     QTestState *from, *to;
2730 
2731     if (migrate_start(&from, &to, args->listen_uri, &args->start)) {
2732         return;
2733     }
2734 
2735     /* Wait for the first serial output from the source */
2736     wait_for_serial("src_serial");
2737 
2738     /*
2739      * 'uri' and 'channels' validation is checked even before the migration
2740      * starts.
2741      */
2742     migrate_qmp_fail(from, args->connect_uri, args->connect_channels, "{}");
2743     migrate_end(from, to, false);
2744 }
2745 
2746 static void test_validate_uri_channels_both_set(void)
2747 {
2748     MigrateCommon args = {
2749         .start = {
2750             .hide_stderr = true,
2751         },
2752         .listen_uri = "defer",
2753         .connect_uri = "tcp:127.0.0.1:0",
2754         .connect_channels = ("[ { ""'channel-type': 'main',"
2755                              "    'addr': { 'transport': 'socket',"
2756                              "              'type': 'inet',"
2757                              "              'host': '127.0.0.1',"
2758                              "              'port': '0' } } ]"),
2759     };
2760 
2761     do_test_validate_uri_channel(&args);
2762 }
2763 
2764 static void test_validate_uri_channels_none_set(void)
2765 {
2766     MigrateCommon args = {
2767         .start = {
2768             .hide_stderr = true,
2769         },
2770         .listen_uri = "defer",
2771     };
2772 
2773     do_test_validate_uri_channel(&args);
2774 }
2775 
2776 /*
2777  * The way auto_converge works, we need to do too many passes to
2778  * run this test.  Auto_converge logic is only run once every
2779  * three iterations, so:
2780  *
2781  * - 3 iterations without auto_converge enabled
2782  * - 3 iterations with pct = 5
2783  * - 3 iterations with pct = 30
2784  * - 3 iterations with pct = 55
2785  * - 3 iterations with pct = 80
2786  * - 3 iterations with pct = 95 (max(95, 80 + 25))
2787  *
2788  * To make things even worse, we need to run the initial stage at
2789  * 3MB/s so we enter autoconverge even when host is (over)loaded.
2790  */
2791 static void test_auto_converge(void)
2792 {
2793     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
2794     MigrateStart args = {};
2795     QTestState *from, *to;
2796     int64_t percentage;
2797 
2798     /*
2799      * We want the test to be stable and as fast as possible.
2800      * E.g., with 1Gb/s bandwidth migration may pass without throttling,
2801      * so we need to decrease a bandwidth.
2802      */
2803     const int64_t init_pct = 5, inc_pct = 25, max_pct = 95;
2804     uint64_t prev_dirty_sync_cnt, dirty_sync_cnt;
2805     int max_try_count, hit = 0;
2806 
2807     if (migrate_start(&from, &to, uri, &args)) {
2808         return;
2809     }
2810 
2811     migrate_set_capability(from, "auto-converge", true);
2812     migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct);
2813     migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct);
2814     migrate_set_parameter_int(from, "max-cpu-throttle", max_pct);
2815 
2816     /*
2817      * Set the initial parameters so that the migration could not converge
2818      * without throttling.
2819      */
2820     migrate_ensure_non_converge(from);
2821 
2822     /* To check remaining size after precopy */
2823     migrate_set_capability(from, "pause-before-switchover", true);
2824 
2825     /* Wait for the first serial output from the source */
2826     wait_for_serial("src_serial");
2827 
2828     migrate_qmp(from, to, uri, NULL, "{}");
2829 
2830     /* Wait for throttling begins */
2831     percentage = 0;
2832     do {
2833         percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
2834         if (percentage != 0) {
2835             break;
2836         }
2837         usleep(20);
2838         g_assert_false(src_state.stop_seen);
2839     } while (true);
2840     /* The first percentage of throttling should be at least init_pct */
2841     g_assert_cmpint(percentage, >=, init_pct);
2842 
2843     /*
2844      * End the loop when the dirty sync count greater than 1.
2845      */
2846     while ((dirty_sync_cnt = get_migration_pass(from)) < 2) {
2847         usleep(1000 * 1000);
2848     }
2849 
2850     prev_dirty_sync_cnt = dirty_sync_cnt;
2851 
2852     /*
2853      * The RAMBlock dirty sync count must changes in 5 seconds, here we set
2854      * the timeout to 10 seconds to ensure it changes.
2855      *
2856      * Note that migrate_ensure_non_converge set the max-bandwidth to 3MB/s,
2857      * while the qtest mem is >= 100MB, one iteration takes at least 33s (100/3)
2858      * to complete; this ensures that the RAMBlock dirty sync occurs.
2859      */
2860     max_try_count = 10;
2861     while (--max_try_count) {
2862         dirty_sync_cnt = get_migration_pass(from);
2863         if (dirty_sync_cnt != prev_dirty_sync_cnt) {
2864             hit = 1;
2865             break;
2866         }
2867         prev_dirty_sync_cnt = dirty_sync_cnt;
2868         sleep(1);
2869     }
2870     g_assert_cmpint(hit, ==, 1);
2871 
2872     /* Now, when we tested that throttling works, let it converge */
2873     migrate_ensure_converge(from);
2874 
2875     /*
2876      * Wait for pre-switchover status to check last throttle percentage
2877      * and remaining. These values will be zeroed later
2878      */
2879     wait_for_migration_status(from, "pre-switchover", NULL);
2880 
2881     /* The final percentage of throttling shouldn't be greater than max_pct */
2882     percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
2883     g_assert_cmpint(percentage, <=, max_pct);
2884     migrate_continue(from, "pre-switchover");
2885 
2886     qtest_qmp_eventwait(to, "RESUME");
2887 
2888     wait_for_serial("dest_serial");
2889     wait_for_migration_complete(from);
2890 
2891     migrate_end(from, to, true);
2892 }
2893 
2894 static void *
2895 migrate_hook_start_precopy_tcp_multifd_common(QTestState *from,
2896                                               QTestState *to,
2897                                               const char *method)
2898 {
2899     migrate_set_parameter_int(from, "multifd-channels", 16);
2900     migrate_set_parameter_int(to, "multifd-channels", 16);
2901 
2902     migrate_set_parameter_str(from, "multifd-compression", method);
2903     migrate_set_parameter_str(to, "multifd-compression", method);
2904 
2905     migrate_set_capability(from, "multifd", true);
2906     migrate_set_capability(to, "multifd", true);
2907 
2908     /* Start incoming migration from the 1st socket */
2909     migrate_incoming_qmp(to, "tcp:127.0.0.1:0", "{}");
2910 
2911     return NULL;
2912 }
2913 
2914 static void *
2915 migrate_hook_start_precopy_tcp_multifd(QTestState *from,
2916                                        QTestState *to)
2917 {
2918     return migrate_hook_start_precopy_tcp_multifd_common(from, to, "none");
2919 }
2920 
2921 static void *
2922 migrate_hook_start_precopy_tcp_multifd_zero_page_legacy(QTestState *from,
2923                                                         QTestState *to)
2924 {
2925     migrate_hook_start_precopy_tcp_multifd_common(from, to, "none");
2926     migrate_set_parameter_str(from, "zero-page-detection", "legacy");
2927     return NULL;
2928 }
2929 
2930 static void *
2931 migrate_hook_start_precopy_tcp_multifd_no_zero_page(QTestState *from,
2932                                                     QTestState *to)
2933 {
2934     migrate_hook_start_precopy_tcp_multifd_common(from, to, "none");
2935     migrate_set_parameter_str(from, "zero-page-detection", "none");
2936     return NULL;
2937 }
2938 
2939 static void *
2940 migrate_hook_start_precopy_tcp_multifd_zlib(QTestState *from,
2941                                             QTestState *to)
2942 {
2943     /*
2944      * Overloading this test to also check that set_parameter does not error.
2945      * This is also done in the tests for the other compression methods.
2946      */
2947     migrate_set_parameter_int(from, "multifd-zlib-level", 2);
2948     migrate_set_parameter_int(to, "multifd-zlib-level", 2);
2949 
2950     return migrate_hook_start_precopy_tcp_multifd_common(from, to, "zlib");
2951 }
2952 
2953 #ifdef CONFIG_ZSTD
2954 static void *
2955 migrate_hook_start_precopy_tcp_multifd_zstd(QTestState *from,
2956                                             QTestState *to)
2957 {
2958     migrate_set_parameter_int(from, "multifd-zstd-level", 2);
2959     migrate_set_parameter_int(to, "multifd-zstd-level", 2);
2960 
2961     return migrate_hook_start_precopy_tcp_multifd_common(from, to, "zstd");
2962 }
2963 #endif /* CONFIG_ZSTD */
2964 
2965 #ifdef CONFIG_QATZIP
2966 static void *
2967 migrate_hook_start_precopy_tcp_multifd_qatzip(QTestState *from,
2968                                               QTestState *to)
2969 {
2970     migrate_set_parameter_int(from, "multifd-qatzip-level", 2);
2971     migrate_set_parameter_int(to, "multifd-qatzip-level", 2);
2972 
2973     return migrate_hook_start_precopy_tcp_multifd_common(from, to, "qatzip");
2974 }
2975 #endif
2976 
2977 #ifdef CONFIG_QPL
2978 static void *
2979 migrate_hook_start_precopy_tcp_multifd_qpl(QTestState *from,
2980                                            QTestState *to)
2981 {
2982     return migrate_hook_start_precopy_tcp_multifd_common(from, to, "qpl");
2983 }
2984 #endif /* CONFIG_QPL */
2985 #ifdef CONFIG_UADK
2986 static void *
2987 migrate_hook_start_precopy_tcp_multifd_uadk(QTestState *from,
2988                                             QTestState *to)
2989 {
2990     return migrate_hook_start_precopy_tcp_multifd_common(from, to, "uadk");
2991 }
2992 #endif /* CONFIG_UADK */
2993 
2994 static void test_multifd_tcp_uri_none(void)
2995 {
2996     MigrateCommon args = {
2997         .listen_uri = "defer",
2998         .start_hook = migrate_hook_start_precopy_tcp_multifd,
2999         /*
3000          * Multifd is more complicated than most of the features, it
3001          * directly takes guest page buffers when sending, make sure
3002          * everything will work alright even if guest page is changing.
3003          */
3004         .live = true,
3005     };
3006     test_precopy_common(&args);
3007 }
3008 
3009 static void test_multifd_tcp_zero_page_legacy(void)
3010 {
3011     MigrateCommon args = {
3012         .listen_uri = "defer",
3013         .start_hook = migrate_hook_start_precopy_tcp_multifd_zero_page_legacy,
3014         /*
3015          * Multifd is more complicated than most of the features, it
3016          * directly takes guest page buffers when sending, make sure
3017          * everything will work alright even if guest page is changing.
3018          */
3019         .live = true,
3020     };
3021     test_precopy_common(&args);
3022 }
3023 
3024 static void test_multifd_tcp_no_zero_page(void)
3025 {
3026     MigrateCommon args = {
3027         .listen_uri = "defer",
3028         .start_hook = migrate_hook_start_precopy_tcp_multifd_no_zero_page,
3029         /*
3030          * Multifd is more complicated than most of the features, it
3031          * directly takes guest page buffers when sending, make sure
3032          * everything will work alright even if guest page is changing.
3033          */
3034         .live = true,
3035     };
3036     test_precopy_common(&args);
3037 }
3038 
3039 static void test_multifd_tcp_channels_none(void)
3040 {
3041     MigrateCommon args = {
3042         .listen_uri = "defer",
3043         .start_hook = migrate_hook_start_precopy_tcp_multifd,
3044         .live = true,
3045         .connect_channels = ("[ { 'channel-type': 'main',"
3046                              "    'addr': { 'transport': 'socket',"
3047                              "              'type': 'inet',"
3048                              "              'host': '127.0.0.1',"
3049                              "              'port': '0' } } ]"),
3050     };
3051     test_precopy_common(&args);
3052 }
3053 
3054 static void test_multifd_tcp_zlib(void)
3055 {
3056     MigrateCommon args = {
3057         .listen_uri = "defer",
3058         .start_hook = migrate_hook_start_precopy_tcp_multifd_zlib,
3059     };
3060     test_precopy_common(&args);
3061 }
3062 
3063 #ifdef CONFIG_ZSTD
3064 static void test_multifd_tcp_zstd(void)
3065 {
3066     MigrateCommon args = {
3067         .listen_uri = "defer",
3068         .start_hook = migrate_hook_start_precopy_tcp_multifd_zstd,
3069     };
3070     test_precopy_common(&args);
3071 }
3072 #endif
3073 
3074 #ifdef CONFIG_QATZIP
3075 static void test_multifd_tcp_qatzip(void)
3076 {
3077     MigrateCommon args = {
3078         .listen_uri = "defer",
3079         .start_hook = migrate_hook_start_precopy_tcp_multifd_qatzip,
3080     };
3081     test_precopy_common(&args);
3082 }
3083 #endif
3084 
3085 #ifdef CONFIG_QPL
3086 static void test_multifd_tcp_qpl(void)
3087 {
3088     MigrateCommon args = {
3089         .listen_uri = "defer",
3090         .start_hook = migrate_hook_start_precopy_tcp_multifd_qpl,
3091     };
3092     test_precopy_common(&args);
3093 }
3094 #endif
3095 
3096 #ifdef CONFIG_UADK
3097 static void test_multifd_tcp_uadk(void)
3098 {
3099     MigrateCommon args = {
3100         .listen_uri = "defer",
3101         .start_hook = migrate_hook_start_precopy_tcp_multifd_uadk,
3102     };
3103     test_precopy_common(&args);
3104 }
3105 #endif
3106 
3107 #ifdef CONFIG_GNUTLS
3108 static void *
3109 migrate_hook_start_multifd_tcp_tls_psk_match(QTestState *from,
3110                                              QTestState *to)
3111 {
3112     migrate_hook_start_precopy_tcp_multifd_common(from, to, "none");
3113     return migrate_hook_start_tls_psk_match(from, to);
3114 }
3115 
3116 static void *
3117 migrate_hook_start_multifd_tcp_tls_psk_mismatch(QTestState *from,
3118                                                 QTestState *to)
3119 {
3120     migrate_hook_start_precopy_tcp_multifd_common(from, to, "none");
3121     return migrate_hook_start_tls_psk_mismatch(from, to);
3122 }
3123 
3124 #ifdef CONFIG_TASN1
3125 static void *
3126 migrate_hook_start_multifd_tls_x509_default_host(QTestState *from,
3127                                                  QTestState *to)
3128 {
3129     migrate_hook_start_precopy_tcp_multifd_common(from, to, "none");
3130     return migrate_hook_start_tls_x509_default_host(from, to);
3131 }
3132 
3133 static void *
3134 migrate_hook_start_multifd_tls_x509_override_host(QTestState *from,
3135                                                   QTestState *to)
3136 {
3137     migrate_hook_start_precopy_tcp_multifd_common(from, to, "none");
3138     return migrate_hook_start_tls_x509_override_host(from, to);
3139 }
3140 
3141 static void *
3142 migrate_hook_start_multifd_tls_x509_mismatch_host(QTestState *from,
3143                                                   QTestState *to)
3144 {
3145     migrate_hook_start_precopy_tcp_multifd_common(from, to, "none");
3146     return migrate_hook_start_tls_x509_mismatch_host(from, to);
3147 }
3148 
3149 static void *
3150 migrate_hook_start_multifd_tls_x509_allow_anon_client(QTestState *from,
3151                                                       QTestState *to)
3152 {
3153     migrate_hook_start_precopy_tcp_multifd_common(from, to, "none");
3154     return migrate_hook_start_tls_x509_allow_anon_client(from, to);
3155 }
3156 
3157 static void *
3158 migrate_hook_start_multifd_tls_x509_reject_anon_client(QTestState *from,
3159                                                        QTestState *to)
3160 {
3161     migrate_hook_start_precopy_tcp_multifd_common(from, to, "none");
3162     return migrate_hook_start_tls_x509_reject_anon_client(from, to);
3163 }
3164 #endif /* CONFIG_TASN1 */
3165 
3166 static void test_multifd_tcp_tls_psk_match(void)
3167 {
3168     MigrateCommon args = {
3169         .listen_uri = "defer",
3170         .start_hook = migrate_hook_start_multifd_tcp_tls_psk_match,
3171         .end_hook = migrate_hook_end_tls_psk,
3172     };
3173     test_precopy_common(&args);
3174 }
3175 
3176 static void test_multifd_tcp_tls_psk_mismatch(void)
3177 {
3178     MigrateCommon args = {
3179         .start = {
3180             .hide_stderr = true,
3181         },
3182         .listen_uri = "defer",
3183         .start_hook = migrate_hook_start_multifd_tcp_tls_psk_mismatch,
3184         .end_hook = migrate_hook_end_tls_psk,
3185         .result = MIG_TEST_FAIL,
3186     };
3187     test_precopy_common(&args);
3188 }
3189 
3190 #ifdef CONFIG_TASN1
3191 static void test_multifd_tcp_tls_x509_default_host(void)
3192 {
3193     MigrateCommon args = {
3194         .listen_uri = "defer",
3195         .start_hook = migrate_hook_start_multifd_tls_x509_default_host,
3196         .end_hook = migrate_hook_end_tls_x509,
3197     };
3198     test_precopy_common(&args);
3199 }
3200 
3201 static void test_multifd_tcp_tls_x509_override_host(void)
3202 {
3203     MigrateCommon args = {
3204         .listen_uri = "defer",
3205         .start_hook = migrate_hook_start_multifd_tls_x509_override_host,
3206         .end_hook = migrate_hook_end_tls_x509,
3207     };
3208     test_precopy_common(&args);
3209 }
3210 
3211 static void test_multifd_tcp_tls_x509_mismatch_host(void)
3212 {
3213     /*
3214      * This has different behaviour to the non-multifd case.
3215      *
3216      * In non-multifd case when client aborts due to mismatched
3217      * cert host, the server has already started trying to load
3218      * migration state, and so it exits with I/O failure.
3219      *
3220      * In multifd case when client aborts due to mismatched
3221      * cert host, the server is still waiting for the other
3222      * multifd connections to arrive so hasn't started trying
3223      * to load migration state, and thus just aborts the migration
3224      * without exiting.
3225      */
3226     MigrateCommon args = {
3227         .start = {
3228             .hide_stderr = true,
3229         },
3230         .listen_uri = "defer",
3231         .start_hook = migrate_hook_start_multifd_tls_x509_mismatch_host,
3232         .end_hook = migrate_hook_end_tls_x509,
3233         .result = MIG_TEST_FAIL,
3234     };
3235     test_precopy_common(&args);
3236 }
3237 
3238 static void test_multifd_tcp_tls_x509_allow_anon_client(void)
3239 {
3240     MigrateCommon args = {
3241         .listen_uri = "defer",
3242         .start_hook = migrate_hook_start_multifd_tls_x509_allow_anon_client,
3243         .end_hook = migrate_hook_end_tls_x509,
3244     };
3245     test_precopy_common(&args);
3246 }
3247 
3248 static void test_multifd_tcp_tls_x509_reject_anon_client(void)
3249 {
3250     MigrateCommon args = {
3251         .start = {
3252             .hide_stderr = true,
3253         },
3254         .listen_uri = "defer",
3255         .start_hook = migrate_hook_start_multifd_tls_x509_reject_anon_client,
3256         .end_hook = migrate_hook_end_tls_x509,
3257         .result = MIG_TEST_FAIL,
3258     };
3259     test_precopy_common(&args);
3260 }
3261 #endif /* CONFIG_TASN1 */
3262 #endif /* CONFIG_GNUTLS */
3263 
3264 /*
3265  * This test does:
3266  *  source               target
3267  *                       migrate_incoming
3268  *     migrate
3269  *     migrate_cancel
3270  *                       launch another target
3271  *     migrate
3272  *
3273  *  And see that it works
3274  */
3275 static void test_multifd_tcp_cancel(void)
3276 {
3277     MigrateStart args = {
3278         .hide_stderr = true,
3279     };
3280     QTestState *from, *to, *to2;
3281 
3282     if (migrate_start(&from, &to, "defer", &args)) {
3283         return;
3284     }
3285 
3286     migrate_ensure_non_converge(from);
3287     migrate_prepare_for_dirty_mem(from);
3288 
3289     migrate_set_parameter_int(from, "multifd-channels", 16);
3290     migrate_set_parameter_int(to, "multifd-channels", 16);
3291 
3292     migrate_set_capability(from, "multifd", true);
3293     migrate_set_capability(to, "multifd", true);
3294 
3295     /* Start incoming migration from the 1st socket */
3296     migrate_incoming_qmp(to, "tcp:127.0.0.1:0", "{}");
3297 
3298     /* Wait for the first serial output from the source */
3299     wait_for_serial("src_serial");
3300 
3301     migrate_qmp(from, to, NULL, NULL, "{}");
3302 
3303     migrate_wait_for_dirty_mem(from, to);
3304 
3305     migrate_cancel(from);
3306 
3307     /* Make sure QEMU process "to" exited */
3308     qtest_set_expected_status(to, EXIT_FAILURE);
3309     qtest_wait_qemu(to);
3310     qtest_quit(to);
3311 
3312     /*
3313      * Ensure the source QEMU finishes its cancellation process before we
3314      * proceed with the setup of the next migration. The migrate_start()
3315      * function and others might want to interact with the source in a way that
3316      * is not possible while the migration is not canceled properly. For
3317      * example, setting migration capabilities when the migration is still
3318      * running leads to an error.
3319      */
3320     wait_for_migration_status(from, "cancelled", NULL);
3321 
3322     args = (MigrateStart){
3323         .only_target = true,
3324     };
3325 
3326     if (migrate_start(&from, &to2, "defer", &args)) {
3327         return;
3328     }
3329 
3330     migrate_set_parameter_int(to2, "multifd-channels", 16);
3331 
3332     migrate_set_capability(to2, "multifd", true);
3333 
3334     /* Start incoming migration from the 1st socket */
3335     migrate_incoming_qmp(to2, "tcp:127.0.0.1:0", "{}");
3336 
3337     migrate_ensure_non_converge(from);
3338 
3339     migrate_qmp(from, to2, NULL, NULL, "{}");
3340 
3341     migrate_wait_for_dirty_mem(from, to2);
3342 
3343     migrate_ensure_converge(from);
3344 
3345     wait_for_stop(from, &src_state);
3346     qtest_qmp_eventwait(to2, "RESUME");
3347 
3348     wait_for_serial("dest_serial");
3349     wait_for_migration_complete(from);
3350     migrate_end(from, to2, true);
3351 }
3352 
3353 static void calc_dirty_rate(QTestState *who, uint64_t calc_time)
3354 {
3355     qtest_qmp_assert_success(who,
3356                              "{ 'execute': 'calc-dirty-rate',"
3357                              "'arguments': { "
3358                              "'calc-time': %" PRIu64 ","
3359                              "'mode': 'dirty-ring' }}",
3360                              calc_time);
3361 }
3362 
3363 static QDict *query_dirty_rate(QTestState *who)
3364 {
3365     return qtest_qmp_assert_success_ref(who,
3366                                         "{ 'execute': 'query-dirty-rate' }");
3367 }
3368 
3369 static void dirtylimit_set_all(QTestState *who, uint64_t dirtyrate)
3370 {
3371     qtest_qmp_assert_success(who,
3372                              "{ 'execute': 'set-vcpu-dirty-limit',"
3373                              "'arguments': { "
3374                              "'dirty-rate': %" PRIu64 " } }",
3375                              dirtyrate);
3376 }
3377 
3378 static void cancel_vcpu_dirty_limit(QTestState *who)
3379 {
3380     qtest_qmp_assert_success(who,
3381                              "{ 'execute': 'cancel-vcpu-dirty-limit' }");
3382 }
3383 
3384 static QDict *query_vcpu_dirty_limit(QTestState *who)
3385 {
3386     QDict *rsp;
3387 
3388     rsp = qtest_qmp(who, "{ 'execute': 'query-vcpu-dirty-limit' }");
3389     g_assert(!qdict_haskey(rsp, "error"));
3390     g_assert(qdict_haskey(rsp, "return"));
3391 
3392     return rsp;
3393 }
3394 
3395 static bool calc_dirtyrate_ready(QTestState *who)
3396 {
3397     QDict *rsp_return;
3398     const char *status;
3399     bool ready;
3400 
3401     rsp_return = query_dirty_rate(who);
3402     g_assert(rsp_return);
3403 
3404     status = qdict_get_str(rsp_return, "status");
3405     g_assert(status);
3406     ready = g_strcmp0(status, "measuring");
3407     qobject_unref(rsp_return);
3408 
3409     return ready;
3410 }
3411 
3412 static void wait_for_calc_dirtyrate_complete(QTestState *who,
3413                                              int64_t time_s)
3414 {
3415     int max_try_count = 10000;
3416     usleep(time_s * 1000000);
3417 
3418     while (!calc_dirtyrate_ready(who) && max_try_count--) {
3419         usleep(1000);
3420     }
3421 
3422     /*
3423      * Set the timeout with 10 s(max_try_count * 1000us),
3424      * if dirtyrate measurement not complete, fail test.
3425      */
3426     g_assert_cmpint(max_try_count, !=, 0);
3427 }
3428 
3429 static int64_t get_dirty_rate(QTestState *who)
3430 {
3431     QDict *rsp_return;
3432     const char *status;
3433     QList *rates;
3434     const QListEntry *entry;
3435     QDict *rate;
3436     int64_t dirtyrate;
3437 
3438     rsp_return = query_dirty_rate(who);
3439     g_assert(rsp_return);
3440 
3441     status = qdict_get_str(rsp_return, "status");
3442     g_assert(status);
3443     g_assert_cmpstr(status, ==, "measured");
3444 
3445     rates = qdict_get_qlist(rsp_return, "vcpu-dirty-rate");
3446     g_assert(rates && !qlist_empty(rates));
3447 
3448     entry = qlist_first(rates);
3449     g_assert(entry);
3450 
3451     rate = qobject_to(QDict, qlist_entry_obj(entry));
3452     g_assert(rate);
3453 
3454     dirtyrate = qdict_get_try_int(rate, "dirty-rate", -1);
3455 
3456     qobject_unref(rsp_return);
3457     return dirtyrate;
3458 }
3459 
3460 static int64_t get_limit_rate(QTestState *who)
3461 {
3462     QDict *rsp_return;
3463     QList *rates;
3464     const QListEntry *entry;
3465     QDict *rate;
3466     int64_t dirtyrate;
3467 
3468     rsp_return = query_vcpu_dirty_limit(who);
3469     g_assert(rsp_return);
3470 
3471     rates = qdict_get_qlist(rsp_return, "return");
3472     g_assert(rates && !qlist_empty(rates));
3473 
3474     entry = qlist_first(rates);
3475     g_assert(entry);
3476 
3477     rate = qobject_to(QDict, qlist_entry_obj(entry));
3478     g_assert(rate);
3479 
3480     dirtyrate = qdict_get_try_int(rate, "limit-rate", -1);
3481 
3482     qobject_unref(rsp_return);
3483     return dirtyrate;
3484 }
3485 
3486 static QTestState *dirtylimit_start_vm(void)
3487 {
3488     QTestState *vm = NULL;
3489     g_autofree gchar *cmd = NULL;
3490 
3491     bootfile_create(tmpfs, false);
3492     cmd = g_strdup_printf("-accel kvm,dirty-ring-size=4096 "
3493                           "-name dirtylimit-test,debug-threads=on "
3494                           "-m 150M -smp 1 "
3495                           "-serial file:%s/vm_serial "
3496                           "-drive file=%s,format=raw ",
3497                           tmpfs, bootpath);
3498 
3499     vm = qtest_init(cmd);
3500     return vm;
3501 }
3502 
3503 static void dirtylimit_stop_vm(QTestState *vm)
3504 {
3505     qtest_quit(vm);
3506     cleanup("vm_serial");
3507 }
3508 
3509 static void test_vcpu_dirty_limit(void)
3510 {
3511     QTestState *vm;
3512     int64_t origin_rate;
3513     int64_t quota_rate;
3514     int64_t rate ;
3515     int max_try_count = 20;
3516     int hit = 0;
3517 
3518     /* Start vm for vcpu dirtylimit test */
3519     vm = dirtylimit_start_vm();
3520 
3521     /* Wait for the first serial output from the vm*/
3522     wait_for_serial("vm_serial");
3523 
3524     /* Do dirtyrate measurement with calc time equals 1s */
3525     calc_dirty_rate(vm, 1);
3526 
3527     /* Sleep calc time and wait for calc dirtyrate complete */
3528     wait_for_calc_dirtyrate_complete(vm, 1);
3529 
3530     /* Query original dirty page rate */
3531     origin_rate = get_dirty_rate(vm);
3532 
3533     /* VM booted from bootsect should dirty memory steadily */
3534     assert(origin_rate != 0);
3535 
3536     /* Setup quota dirty page rate at half of origin */
3537     quota_rate = origin_rate / 2;
3538 
3539     /* Set dirtylimit */
3540     dirtylimit_set_all(vm, quota_rate);
3541 
3542     /*
3543      * Check if set-vcpu-dirty-limit and query-vcpu-dirty-limit
3544      * works literally
3545      */
3546     g_assert_cmpint(quota_rate, ==, get_limit_rate(vm));
3547 
3548     /* Sleep a bit to check if it take effect */
3549     usleep(2000000);
3550 
3551     /*
3552      * Check if dirtylimit take effect realistically, set the
3553      * timeout with 20 s(max_try_count * 1s), if dirtylimit
3554      * doesn't take effect, fail test.
3555      */
3556     while (--max_try_count) {
3557         calc_dirty_rate(vm, 1);
3558         wait_for_calc_dirtyrate_complete(vm, 1);
3559         rate = get_dirty_rate(vm);
3560 
3561         /*
3562          * Assume hitting if current rate is less
3563          * than quota rate (within accepting error)
3564          */
3565         if (rate < (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) {
3566             hit = 1;
3567             break;
3568         }
3569     }
3570 
3571     g_assert_cmpint(hit, ==, 1);
3572 
3573     hit = 0;
3574     max_try_count = 20;
3575 
3576     /* Check if dirtylimit cancellation take effect */
3577     cancel_vcpu_dirty_limit(vm);
3578     while (--max_try_count) {
3579         calc_dirty_rate(vm, 1);
3580         wait_for_calc_dirtyrate_complete(vm, 1);
3581         rate = get_dirty_rate(vm);
3582 
3583         /*
3584          * Assume dirtylimit be canceled if current rate is
3585          * greater than quota rate (within accepting error)
3586          */
3587         if (rate > (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) {
3588             hit = 1;
3589             break;
3590         }
3591     }
3592 
3593     g_assert_cmpint(hit, ==, 1);
3594     dirtylimit_stop_vm(vm);
3595 }
3596 
3597 static void migrate_dirty_limit_wait_showup(QTestState *from,
3598                                             const int64_t period,
3599                                             const int64_t value)
3600 {
3601     /* Enable dirty limit capability */
3602     migrate_set_capability(from, "dirty-limit", true);
3603 
3604     /* Set dirty limit parameters */
3605     migrate_set_parameter_int(from, "x-vcpu-dirty-limit-period", period);
3606     migrate_set_parameter_int(from, "vcpu-dirty-limit", value);
3607 
3608     /* Make sure migrate can't converge */
3609     migrate_ensure_non_converge(from);
3610 
3611     /* To check limit rate after precopy */
3612     migrate_set_capability(from, "pause-before-switchover", true);
3613 
3614     /* Wait for the serial output from the source */
3615     wait_for_serial("src_serial");
3616 }
3617 
3618 /*
3619  * This test does:
3620  *  source                          destination
3621  *  start vm
3622  *                                  start incoming vm
3623  *  migrate
3624  *  wait dirty limit to begin
3625  *  cancel migrate
3626  *  cancellation check
3627  *                                  restart incoming vm
3628  *  migrate
3629  *  wait dirty limit to begin
3630  *  wait pre-switchover event
3631  *  convergence condition check
3632  *
3633  * And see if dirty limit migration works correctly.
3634  * This test case involves many passes, so it runs in slow mode only.
3635  */
3636 static void test_dirty_limit(void)
3637 {
3638     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
3639     QTestState *from, *to;
3640     int64_t remaining;
3641     uint64_t throttle_us_per_full;
3642     /*
3643      * We want the test to be stable and as fast as possible.
3644      * E.g., with 1Gb/s bandwidth migration may pass without dirty limit,
3645      * so we need to decrease a bandwidth.
3646      */
3647     const int64_t dirtylimit_period = 1000, dirtylimit_value = 50;
3648     const int64_t max_bandwidth = 400000000; /* ~400Mb/s */
3649     const int64_t downtime_limit = 250; /* 250ms */
3650     /*
3651      * We migrate through unix-socket (> 500Mb/s).
3652      * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
3653      * So, we can predict expected_threshold
3654      */
3655     const int64_t expected_threshold = max_bandwidth * downtime_limit / 1000;
3656     int max_try_count = 10;
3657     MigrateCommon args = {
3658         .start = {
3659             .hide_stderr = true,
3660             .use_dirty_ring = true,
3661         },
3662         .listen_uri = uri,
3663         .connect_uri = uri,
3664     };
3665 
3666     /* Start src, dst vm */
3667     if (migrate_start(&from, &to, args.listen_uri, &args.start)) {
3668         return;
3669     }
3670 
3671     /* Prepare for dirty limit migration and wait src vm show up */
3672     migrate_dirty_limit_wait_showup(from, dirtylimit_period, dirtylimit_value);
3673 
3674     /* Start migrate */
3675     migrate_qmp(from, to, args.connect_uri, NULL, "{}");
3676 
3677     /* Wait for dirty limit throttle begin */
3678     throttle_us_per_full = 0;
3679     while (throttle_us_per_full == 0) {
3680         throttle_us_per_full =
3681             read_migrate_property_int(from,
3682                                       "dirty-limit-throttle-time-per-round");
3683         usleep(100);
3684         g_assert_false(src_state.stop_seen);
3685     }
3686 
3687     /* Now cancel migrate and wait for dirty limit throttle switch off */
3688     migrate_cancel(from);
3689     wait_for_migration_status(from, "cancelled", NULL);
3690 
3691     /* Check if dirty limit throttle switched off, set timeout 1ms */
3692     do {
3693         throttle_us_per_full =
3694             read_migrate_property_int(from,
3695                                       "dirty-limit-throttle-time-per-round");
3696         usleep(100);
3697         g_assert_false(src_state.stop_seen);
3698     } while (throttle_us_per_full != 0 && --max_try_count);
3699 
3700     /* Assert dirty limit is not in service */
3701     g_assert_cmpint(throttle_us_per_full, ==, 0);
3702 
3703     args = (MigrateCommon) {
3704         .start = {
3705             .only_target = true,
3706             .use_dirty_ring = true,
3707         },
3708         .listen_uri = uri,
3709         .connect_uri = uri,
3710     };
3711 
3712     /* Restart dst vm, src vm already show up so we needn't wait anymore */
3713     if (migrate_start(&from, &to, args.listen_uri, &args.start)) {
3714         return;
3715     }
3716 
3717     /* Start migrate */
3718     migrate_qmp(from, to, args.connect_uri, NULL, "{}");
3719 
3720     /* Wait for dirty limit throttle begin */
3721     throttle_us_per_full = 0;
3722     while (throttle_us_per_full == 0) {
3723         throttle_us_per_full =
3724             read_migrate_property_int(from,
3725                                       "dirty-limit-throttle-time-per-round");
3726         usleep(100);
3727         g_assert_false(src_state.stop_seen);
3728     }
3729 
3730     /*
3731      * The dirty limit rate should equals the return value of
3732      * query-vcpu-dirty-limit if dirty limit cap set
3733      */
3734     g_assert_cmpint(dirtylimit_value, ==, get_limit_rate(from));
3735 
3736     /* Now, we have tested if dirty limit works, let it converge */
3737     migrate_set_parameter_int(from, "downtime-limit", downtime_limit);
3738     migrate_set_parameter_int(from, "max-bandwidth", max_bandwidth);
3739 
3740     /*
3741      * Wait for pre-switchover status to check if migration
3742      * satisfy the convergence condition
3743      */
3744     wait_for_migration_status(from, "pre-switchover", NULL);
3745 
3746     remaining = read_ram_property_int(from, "remaining");
3747     g_assert_cmpint(remaining, <,
3748                     (expected_threshold + expected_threshold / 100));
3749 
3750     migrate_continue(from, "pre-switchover");
3751 
3752     qtest_qmp_eventwait(to, "RESUME");
3753 
3754     wait_for_serial("dest_serial");
3755     wait_for_migration_complete(from);
3756 
3757     migrate_end(from, to, true);
3758 }
3759 
3760 static bool kvm_dirty_ring_supported(void)
3761 {
3762 #if defined(__linux__) && defined(HOST_X86_64)
3763     int ret, kvm_fd = open("/dev/kvm", O_RDONLY);
3764 
3765     if (kvm_fd < 0) {
3766         return false;
3767     }
3768 
3769     ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, KVM_CAP_DIRTY_LOG_RING);
3770     close(kvm_fd);
3771 
3772     /* We test with 4096 slots */
3773     if (ret < 4096) {
3774         return false;
3775     }
3776 
3777     return true;
3778 #else
3779     return false;
3780 #endif
3781 }
3782 
3783 int main(int argc, char **argv)
3784 {
3785     bool has_kvm, has_tcg;
3786     bool has_uffd, is_x86;
3787     const char *arch;
3788     g_autoptr(GError) err = NULL;
3789     const char *qemu_src = getenv(QEMU_ENV_SRC);
3790     const char *qemu_dst = getenv(QEMU_ENV_DST);
3791     int ret;
3792 
3793     g_test_init(&argc, &argv, NULL);
3794 
3795     /*
3796      * The default QTEST_QEMU_BINARY must always be provided because
3797      * that is what helpers use to query the accel type and
3798      * architecture.
3799      */
3800     if (qemu_src && qemu_dst) {
3801         g_test_message("Only one of %s, %s is allowed",
3802                        QEMU_ENV_SRC, QEMU_ENV_DST);
3803         exit(1);
3804     }
3805 
3806     has_kvm = qtest_has_accel("kvm");
3807     has_tcg = qtest_has_accel("tcg");
3808 
3809     if (!has_tcg && !has_kvm) {
3810         g_test_skip("No KVM or TCG accelerator available");
3811         return 0;
3812     }
3813 
3814     has_uffd = ufd_version_check();
3815     arch = qtest_get_arch();
3816     is_x86 = !strcmp(arch, "i386") || !strcmp(arch, "x86_64");
3817 
3818     tmpfs = g_dir_make_tmp("migration-test-XXXXXX", &err);
3819     if (!tmpfs) {
3820         g_test_message("Can't create temporary directory in %s: %s",
3821                        g_get_tmp_dir(), err->message);
3822     }
3823     g_assert(tmpfs);
3824 
3825     module_call_init(MODULE_INIT_QOM);
3826 
3827     migration_test_add("/migration/bad_dest", test_baddest);
3828 #ifndef _WIN32
3829     migration_test_add("/migration/analyze-script", test_analyze_script);
3830 #endif
3831 
3832     if (is_x86) {
3833         migration_test_add("/migration/precopy/unix/suspend/live",
3834                            test_precopy_unix_suspend_live);
3835         migration_test_add("/migration/precopy/unix/suspend/notlive",
3836                            test_precopy_unix_suspend_notlive);
3837     }
3838 
3839     if (has_uffd) {
3840         migration_test_add("/migration/postcopy/plain", test_postcopy);
3841         migration_test_add("/migration/postcopy/recovery/plain",
3842                            test_postcopy_recovery);
3843         migration_test_add("/migration/postcopy/preempt/plain",
3844                            test_postcopy_preempt);
3845         migration_test_add("/migration/postcopy/preempt/recovery/plain",
3846                            test_postcopy_preempt_recovery);
3847         migration_test_add("/migration/postcopy/recovery/double-failures/handshake",
3848                            test_postcopy_recovery_fail_handshake);
3849         migration_test_add("/migration/postcopy/recovery/double-failures/reconnect",
3850                            test_postcopy_recovery_fail_reconnect);
3851         if (is_x86) {
3852             migration_test_add("/migration/postcopy/suspend",
3853                                test_postcopy_suspend);
3854         }
3855     }
3856 
3857     migration_test_add("/migration/precopy/unix/plain",
3858                        test_precopy_unix_plain);
3859     if (g_test_slow()) {
3860         migration_test_add("/migration/precopy/unix/xbzrle",
3861                            test_precopy_unix_xbzrle);
3862     }
3863     migration_test_add("/migration/precopy/file",
3864                        test_precopy_file);
3865     migration_test_add("/migration/precopy/file/offset",
3866                        test_precopy_file_offset);
3867 #ifndef _WIN32
3868     migration_test_add("/migration/precopy/file/offset/fdset",
3869                        test_precopy_file_offset_fdset);
3870 #endif
3871     migration_test_add("/migration/precopy/file/offset/bad",
3872                        test_precopy_file_offset_bad);
3873 
3874     /*
3875      * Our CI system has problems with shared memory.
3876      * Don't run this test until we find a workaround.
3877      */
3878     if (getenv("QEMU_TEST_FLAKY_TESTS")) {
3879         migration_test_add("/migration/mode/reboot", test_mode_reboot);
3880     }
3881 
3882     migration_test_add("/migration/precopy/file/mapped-ram",
3883                        test_precopy_file_mapped_ram);
3884     migration_test_add("/migration/precopy/file/mapped-ram/live",
3885                        test_precopy_file_mapped_ram_live);
3886 
3887     migration_test_add("/migration/multifd/file/mapped-ram",
3888                        test_multifd_file_mapped_ram);
3889     migration_test_add("/migration/multifd/file/mapped-ram/live",
3890                        test_multifd_file_mapped_ram_live);
3891 
3892     migration_test_add("/migration/multifd/file/mapped-ram/dio",
3893                        test_multifd_file_mapped_ram_dio);
3894 
3895 #ifndef _WIN32
3896     migration_test_add("/migration/multifd/file/mapped-ram/fdset",
3897                        test_multifd_file_mapped_ram_fdset);
3898     migration_test_add("/migration/multifd/file/mapped-ram/fdset/dio",
3899                        test_multifd_file_mapped_ram_fdset_dio);
3900 #endif
3901 
3902 #ifdef CONFIG_GNUTLS
3903     migration_test_add("/migration/precopy/unix/tls/psk",
3904                        test_precopy_unix_tls_psk);
3905 
3906     if (has_uffd) {
3907         /*
3908          * NOTE: psk test is enough for postcopy, as other types of TLS
3909          * channels are tested under precopy.  Here what we want to test is the
3910          * general postcopy path that has TLS channel enabled.
3911          */
3912         migration_test_add("/migration/postcopy/tls/psk",
3913                            test_postcopy_tls_psk);
3914         migration_test_add("/migration/postcopy/recovery/tls/psk",
3915                            test_postcopy_recovery_tls_psk);
3916         migration_test_add("/migration/postcopy/preempt/tls/psk",
3917                            test_postcopy_preempt_tls_psk);
3918         migration_test_add("/migration/postcopy/preempt/recovery/tls/psk",
3919                            test_postcopy_preempt_all);
3920     }
3921 #ifdef CONFIG_TASN1
3922     migration_test_add("/migration/precopy/unix/tls/x509/default-host",
3923                        test_precopy_unix_tls_x509_default_host);
3924     migration_test_add("/migration/precopy/unix/tls/x509/override-host",
3925                        test_precopy_unix_tls_x509_override_host);
3926 #endif /* CONFIG_TASN1 */
3927 #endif /* CONFIG_GNUTLS */
3928 
3929     migration_test_add("/migration/precopy/tcp/plain", test_precopy_tcp_plain);
3930 
3931     migration_test_add("/migration/precopy/tcp/plain/switchover-ack",
3932                        test_precopy_tcp_switchover_ack);
3933 
3934 #ifdef CONFIG_GNUTLS
3935     migration_test_add("/migration/precopy/tcp/tls/psk/match",
3936                        test_precopy_tcp_tls_psk_match);
3937     migration_test_add("/migration/precopy/tcp/tls/psk/mismatch",
3938                        test_precopy_tcp_tls_psk_mismatch);
3939 #ifdef CONFIG_TASN1
3940     migration_test_add("/migration/precopy/tcp/tls/x509/default-host",
3941                        test_precopy_tcp_tls_x509_default_host);
3942     migration_test_add("/migration/precopy/tcp/tls/x509/override-host",
3943                        test_precopy_tcp_tls_x509_override_host);
3944     migration_test_add("/migration/precopy/tcp/tls/x509/mismatch-host",
3945                        test_precopy_tcp_tls_x509_mismatch_host);
3946     migration_test_add("/migration/precopy/tcp/tls/x509/friendly-client",
3947                        test_precopy_tcp_tls_x509_friendly_client);
3948     migration_test_add("/migration/precopy/tcp/tls/x509/hostile-client",
3949                        test_precopy_tcp_tls_x509_hostile_client);
3950     migration_test_add("/migration/precopy/tcp/tls/x509/allow-anon-client",
3951                        test_precopy_tcp_tls_x509_allow_anon_client);
3952     migration_test_add("/migration/precopy/tcp/tls/x509/reject-anon-client",
3953                        test_precopy_tcp_tls_x509_reject_anon_client);
3954 #endif /* CONFIG_TASN1 */
3955 #endif /* CONFIG_GNUTLS */
3956 
3957     /* migration_test_add("/migration/ignore_shared", test_ignore_shared); */
3958 #ifndef _WIN32
3959     migration_test_add("/migration/precopy/fd/tcp",
3960                        test_precopy_fd_socket);
3961     migration_test_add("/migration/precopy/fd/file",
3962                        test_precopy_fd_file);
3963 #endif
3964     migration_test_add("/migration/validate_uuid", test_validate_uuid);
3965     migration_test_add("/migration/validate_uuid_error",
3966                        test_validate_uuid_error);
3967     migration_test_add("/migration/validate_uuid_src_not_set",
3968                        test_validate_uuid_src_not_set);
3969     migration_test_add("/migration/validate_uuid_dst_not_set",
3970                        test_validate_uuid_dst_not_set);
3971     migration_test_add("/migration/validate_uri/channels/both_set",
3972                        test_validate_uri_channels_both_set);
3973     migration_test_add("/migration/validate_uri/channels/none_set",
3974                        test_validate_uri_channels_none_set);
3975     /*
3976      * See explanation why this test is slow on function definition
3977      */
3978     if (g_test_slow()) {
3979         migration_test_add("/migration/auto_converge",
3980                            test_auto_converge);
3981         if (g_str_equal(arch, "x86_64") &&
3982             has_kvm && kvm_dirty_ring_supported()) {
3983             migration_test_add("/migration/dirty_limit",
3984                                test_dirty_limit);
3985         }
3986     }
3987     migration_test_add("/migration/multifd/tcp/uri/plain/none",
3988                        test_multifd_tcp_uri_none);
3989     migration_test_add("/migration/multifd/tcp/channels/plain/none",
3990                        test_multifd_tcp_channels_none);
3991     migration_test_add("/migration/multifd/tcp/plain/zero-page/legacy",
3992                        test_multifd_tcp_zero_page_legacy);
3993     migration_test_add("/migration/multifd/tcp/plain/zero-page/none",
3994                        test_multifd_tcp_no_zero_page);
3995     migration_test_add("/migration/multifd/tcp/plain/cancel",
3996                        test_multifd_tcp_cancel);
3997     migration_test_add("/migration/multifd/tcp/plain/zlib",
3998                        test_multifd_tcp_zlib);
3999 #ifdef CONFIG_ZSTD
4000     migration_test_add("/migration/multifd/tcp/plain/zstd",
4001                        test_multifd_tcp_zstd);
4002 #endif
4003 #ifdef CONFIG_QATZIP
4004     migration_test_add("/migration/multifd/tcp/plain/qatzip",
4005                        test_multifd_tcp_qatzip);
4006 #endif
4007 #ifdef CONFIG_QPL
4008     migration_test_add("/migration/multifd/tcp/plain/qpl",
4009                        test_multifd_tcp_qpl);
4010 #endif
4011 #ifdef CONFIG_UADK
4012     migration_test_add("/migration/multifd/tcp/plain/uadk",
4013                        test_multifd_tcp_uadk);
4014 #endif
4015 #ifdef CONFIG_GNUTLS
4016     migration_test_add("/migration/multifd/tcp/tls/psk/match",
4017                        test_multifd_tcp_tls_psk_match);
4018     migration_test_add("/migration/multifd/tcp/tls/psk/mismatch",
4019                        test_multifd_tcp_tls_psk_mismatch);
4020 #ifdef CONFIG_TASN1
4021     migration_test_add("/migration/multifd/tcp/tls/x509/default-host",
4022                        test_multifd_tcp_tls_x509_default_host);
4023     migration_test_add("/migration/multifd/tcp/tls/x509/override-host",
4024                        test_multifd_tcp_tls_x509_override_host);
4025     migration_test_add("/migration/multifd/tcp/tls/x509/mismatch-host",
4026                        test_multifd_tcp_tls_x509_mismatch_host);
4027     migration_test_add("/migration/multifd/tcp/tls/x509/allow-anon-client",
4028                        test_multifd_tcp_tls_x509_allow_anon_client);
4029     migration_test_add("/migration/multifd/tcp/tls/x509/reject-anon-client",
4030                        test_multifd_tcp_tls_x509_reject_anon_client);
4031 #endif /* CONFIG_TASN1 */
4032 #endif /* CONFIG_GNUTLS */
4033 
4034     if (g_str_equal(arch, "x86_64") && has_kvm && kvm_dirty_ring_supported()) {
4035         migration_test_add("/migration/dirty_ring",
4036                            test_precopy_unix_dirty_ring);
4037         if (qtest_has_machine("pc") && g_test_slow()) {
4038             migration_test_add("/migration/vcpu_dirty_limit",
4039                                test_vcpu_dirty_limit);
4040         }
4041     }
4042 
4043     ret = g_test_run();
4044 
4045     g_assert_cmpint(ret, ==, 0);
4046 
4047     bootfile_delete();
4048     ret = rmdir(tmpfs);
4049     if (ret != 0) {
4050         g_test_message("unable to rmdir: path (%s): %s",
4051                        tmpfs, strerror(errno));
4052     }
4053     g_free(tmpfs);
4054 
4055     return ret;
4056 }
4057