xref: /openbmc/qemu/tests/qtest/migration-test.c (revision 637b0aa1)
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 "tests/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 "tests/migration/i386/a-b-bootblock.h"
142 #include "tests/migration/aarch64/a-b-kernel.h"
143 #include "tests/migration/ppc64/a-b-kernel.h"
144 #include "tests/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 (*TestMigrateFinishHook)(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     TestMigrateFinishHook finish_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 test_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 test_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 test_migrate_tls_psk_start_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 test_migrate_tls_psk_start_match(QTestState *from,
968                                  QTestState *to)
969 {
970     return test_migrate_tls_psk_start_common(from, to, false);
971 }
972 
973 static void *
974 test_migrate_tls_psk_start_mismatch(QTestState *from,
975                                     QTestState *to)
976 {
977     return test_migrate_tls_psk_start_common(from, to, true);
978 }
979 
980 static void
981 test_migrate_tls_psk_finish(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 test_migrate_tls_x509_start_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 test_migrate_tls_x509_start_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 test_migrate_tls_x509_start_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 test_migrate_tls_x509_start_override_host(QTestState *from,
1135                                           QTestState *to)
1136 {
1137     TestMigrateTLSX509 args = {
1138         .verifyclient = true,
1139         .clientcert = true,
1140         .certhostname = "qemu.org",
1141     };
1142     return test_migrate_tls_x509_start_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 test_migrate_tls_x509_start_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 test_migrate_tls_x509_start_common(from, to, &args);
1160 }
1161 
1162 static void *
1163 test_migrate_tls_x509_start_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 test_migrate_tls_x509_start_common(from, to, &args);
1173 }
1174 
1175 static void *
1176 test_migrate_tls_x509_start_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 test_migrate_tls_x509_start_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 test_migrate_tls_x509_start_allow_anon_client(QTestState *from,
1195                                               QTestState *to)
1196 {
1197     TestMigrateTLSX509 args = {
1198         .certipaddr = "127.0.0.1",
1199     };
1200     return test_migrate_tls_x509_start_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 test_migrate_tls_x509_start_reject_anon_client(QTestState *from,
1209                                                QTestState *to)
1210 {
1211     TestMigrateTLSX509 args = {
1212         .verifyclient = true,
1213         .certipaddr = "127.0.0.1",
1214     };
1215     return test_migrate_tls_x509_start_common(from, to, &args);
1216 }
1217 
1218 static void
1219 test_migrate_tls_x509_finish(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 (test_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->finish_hook) {
1318         args->finish_hook(from, to, args->postcopy_data);
1319         args->postcopy_data = NULL;
1320     }
1321 
1322     test_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 = test_migrate_tls_psk_start_match,
1366         .finish_hook = test_migrate_tls_psk_finish,
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 = test_migrate_tls_psk_start_match,
1377         .finish_hook = test_migrate_tls_psk_finish,
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 * []) { "failed", "active",
1388                                                   "completed", NULL });
1389 }
1390 
1391 static void postcopy_recover_fail(QTestState *from, QTestState *to,
1392                                   PostcopyRecoveryFailStage stage)
1393 {
1394 #ifndef _WIN32
1395     bool fail_early = (stage == POSTCOPY_FAIL_CHANNEL_ESTABLISH);
1396     int ret, pair1[2], pair2[2];
1397     char c;
1398 
1399     g_assert(stage > POSTCOPY_FAIL_NONE && stage < POSTCOPY_FAIL_MAX);
1400 
1401     /* Create two unrelated socketpairs */
1402     ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair1);
1403     g_assert_cmpint(ret, ==, 0);
1404 
1405     ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair2);
1406     g_assert_cmpint(ret, ==, 0);
1407 
1408     /*
1409      * Give the guests unpaired ends of the sockets, so they'll all blocked
1410      * at reading.  This mimics a wrong channel established.
1411      */
1412     qtest_qmp_fds_assert_success(from, &pair1[0], 1,
1413                                  "{ 'execute': 'getfd',"
1414                                  "  'arguments': { 'fdname': 'fd-mig' }}");
1415     qtest_qmp_fds_assert_success(to, &pair2[0], 1,
1416                                  "{ 'execute': 'getfd',"
1417                                  "  'arguments': { 'fdname': 'fd-mig' }}");
1418 
1419     /*
1420      * Write the 1st byte as QEMU_VM_COMMAND (0x8) for the dest socket, to
1421      * emulate the 1st byte of a real recovery, but stops from there to
1422      * keep dest QEMU in RECOVER.  This is needed so that we can kick off
1423      * the recover process on dest QEMU (by triggering the G_IO_IN event).
1424      *
1425      * NOTE: this trick is not needed on src QEMUs, because src doesn't
1426      * rely on an pre-existing G_IO_IN event, so it will always trigger the
1427      * upcoming recovery anyway even if it can read nothing.
1428      */
1429 #define QEMU_VM_COMMAND              0x08
1430     c = QEMU_VM_COMMAND;
1431     ret = send(pair2[1], &c, 1, 0);
1432     g_assert_cmpint(ret, ==, 1);
1433 
1434     if (stage == POSTCOPY_FAIL_CHANNEL_ESTABLISH) {
1435         /*
1436          * This will make src QEMU to fail at an early stage when trying to
1437          * resume later, where it shouldn't reach RECOVER stage at all.
1438          */
1439         close(pair1[1]);
1440     }
1441 
1442     migrate_recover(to, "fd:fd-mig");
1443     migrate_qmp(from, to, "fd:fd-mig", NULL, "{'resume': true}");
1444 
1445     /*
1446      * Source QEMU has an extra RECOVER_SETUP phase, dest doesn't have it.
1447      * Make sure it appears along the way.
1448      */
1449     migration_event_wait(from, "postcopy-recover-setup");
1450 
1451     if (fail_early) {
1452         /*
1453          * When fails at reconnection, src QEMU will automatically goes
1454          * back to PAUSED state.  Making sure there is an event in this
1455          * case: Libvirt relies on this to detect early reconnection
1456          * errors.
1457          */
1458         migration_event_wait(from, "postcopy-paused");
1459     } else {
1460         /*
1461          * We want to test "fail later" at RECOVER stage here.  Make sure
1462          * both QEMU instances will go into RECOVER stage first, then test
1463          * kicking them out using migrate-pause.
1464          *
1465          * Explicitly check the RECOVER event on src, that's what Libvirt
1466          * relies on, rather than polling.
1467          */
1468         migration_event_wait(from, "postcopy-recover");
1469         wait_for_postcopy_status(from, "postcopy-recover");
1470 
1471         /* Need an explicit kick on src QEMU in this case */
1472         migrate_pause(from);
1473     }
1474 
1475     /*
1476      * For all failure cases, we'll reach such states on both sides now.
1477      * Check them.
1478      */
1479     wait_for_postcopy_status(from, "postcopy-paused");
1480     wait_for_postcopy_status(to, "postcopy-recover");
1481 
1482     /*
1483      * Kick dest QEMU out too. This is normally not needed in reality
1484      * because when the channel is shutdown it should also happen on src.
1485      * However here we used separate socket pairs so we need to do that
1486      * explicitly.
1487      */
1488     migrate_pause(to);
1489     wait_for_postcopy_status(to, "postcopy-paused");
1490 
1491     close(pair1[0]);
1492     close(pair2[0]);
1493     close(pair2[1]);
1494 
1495     if (stage != POSTCOPY_FAIL_CHANNEL_ESTABLISH) {
1496         close(pair1[1]);
1497     }
1498 #endif
1499 }
1500 
1501 static void test_postcopy_recovery_common(MigrateCommon *args)
1502 {
1503     QTestState *from, *to;
1504     g_autofree char *uri = NULL;
1505 
1506     /* Always hide errors for postcopy recover tests since they're expected */
1507     args->start.hide_stderr = true;
1508 
1509     if (migrate_postcopy_prepare(&from, &to, args)) {
1510         return;
1511     }
1512 
1513     /* Turn postcopy speed down, 4K/s is slow enough on any machines */
1514     migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
1515 
1516     /* Now we start the postcopy */
1517     migrate_postcopy_start(from, to);
1518 
1519     /*
1520      * Wait until postcopy is really started; we can only run the
1521      * migrate-pause command during a postcopy
1522      */
1523     wait_for_migration_status(from, "postcopy-active", NULL);
1524 
1525     /*
1526      * Manually stop the postcopy migration. This emulates a network
1527      * failure with the migration socket
1528      */
1529     migrate_pause(from);
1530 
1531     /*
1532      * Wait for destination side to reach postcopy-paused state.  The
1533      * migrate-recover command can only succeed if destination machine
1534      * is in the paused state
1535      */
1536     wait_for_postcopy_status(to, "postcopy-paused");
1537     wait_for_postcopy_status(from, "postcopy-paused");
1538 
1539     if (args->postcopy_recovery_fail_stage) {
1540         /*
1541          * Test when a wrong socket specified for recover, and then the
1542          * ability to kick it out, and continue with a correct socket.
1543          */
1544         postcopy_recover_fail(from, to, args->postcopy_recovery_fail_stage);
1545         /* continue with a good recovery */
1546     }
1547 
1548     /*
1549      * Create a new socket to emulate a new channel that is different
1550      * from the broken migration channel; tell the destination to
1551      * listen to the new port
1552      */
1553     uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
1554     migrate_recover(to, uri);
1555 
1556     /*
1557      * Try to rebuild the migration channel using the resume flag and
1558      * the newly created channel
1559      */
1560     migrate_qmp(from, to, uri, NULL, "{'resume': true}");
1561 
1562     /* Restore the postcopy bandwidth to unlimited */
1563     migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
1564 
1565     migrate_postcopy_complete(from, to, args);
1566 }
1567 
1568 static void test_postcopy_recovery(void)
1569 {
1570     MigrateCommon args = { };
1571 
1572     test_postcopy_recovery_common(&args);
1573 }
1574 
1575 static void test_postcopy_recovery_fail_handshake(void)
1576 {
1577     MigrateCommon args = {
1578         .postcopy_recovery_fail_stage = POSTCOPY_FAIL_RECOVERY,
1579     };
1580 
1581     test_postcopy_recovery_common(&args);
1582 }
1583 
1584 static void test_postcopy_recovery_fail_reconnect(void)
1585 {
1586     MigrateCommon args = {
1587         .postcopy_recovery_fail_stage = POSTCOPY_FAIL_CHANNEL_ESTABLISH,
1588     };
1589 
1590     test_postcopy_recovery_common(&args);
1591 }
1592 
1593 #ifdef CONFIG_GNUTLS
1594 static void test_postcopy_recovery_tls_psk(void)
1595 {
1596     MigrateCommon args = {
1597         .start_hook = test_migrate_tls_psk_start_match,
1598         .finish_hook = test_migrate_tls_psk_finish,
1599     };
1600 
1601     test_postcopy_recovery_common(&args);
1602 }
1603 #endif
1604 
1605 static void test_postcopy_preempt_recovery(void)
1606 {
1607     MigrateCommon args = {
1608         .postcopy_preempt = true,
1609     };
1610 
1611     test_postcopy_recovery_common(&args);
1612 }
1613 
1614 #ifdef CONFIG_GNUTLS
1615 /* This contains preempt+recovery+tls test altogether */
1616 static void test_postcopy_preempt_all(void)
1617 {
1618     MigrateCommon args = {
1619         .postcopy_preempt = true,
1620         .start_hook = test_migrate_tls_psk_start_match,
1621         .finish_hook = test_migrate_tls_psk_finish,
1622     };
1623 
1624     test_postcopy_recovery_common(&args);
1625 }
1626 
1627 #endif
1628 
1629 static void test_baddest(void)
1630 {
1631     MigrateStart args = {
1632         .hide_stderr = true
1633     };
1634     QTestState *from, *to;
1635 
1636     if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", &args)) {
1637         return;
1638     }
1639     migrate_qmp(from, to, "tcp:127.0.0.1:0", NULL, "{}");
1640     wait_for_migration_fail(from, false);
1641     test_migrate_end(from, to, false);
1642 }
1643 
1644 #ifndef _WIN32
1645 static void test_analyze_script(void)
1646 {
1647     MigrateStart args = {
1648         .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
1649     };
1650     QTestState *from, *to;
1651     g_autofree char *uri = NULL;
1652     g_autofree char *file = NULL;
1653     int pid, wstatus;
1654     const char *python = g_getenv("PYTHON");
1655 
1656     if (!python) {
1657         g_test_skip("PYTHON variable not set");
1658         return;
1659     }
1660 
1661     /* dummy url */
1662     if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", &args)) {
1663         return;
1664     }
1665 
1666     /*
1667      * Setting these two capabilities causes the "configuration"
1668      * vmstate to include subsections for them. The script needs to
1669      * parse those subsections properly.
1670      */
1671     migrate_set_capability(from, "validate-uuid", true);
1672     migrate_set_capability(from, "x-ignore-shared", true);
1673 
1674     file = g_strdup_printf("%s/migfile", tmpfs);
1675     uri = g_strdup_printf("exec:cat > %s", file);
1676 
1677     migrate_ensure_converge(from);
1678     migrate_qmp(from, to, uri, NULL, "{}");
1679     wait_for_migration_complete(from);
1680 
1681     pid = fork();
1682     if (!pid) {
1683         close(1);
1684         open("/dev/null", O_WRONLY);
1685         execl(python, python, ANALYZE_SCRIPT, "-f", file, NULL);
1686         g_assert_not_reached();
1687     }
1688 
1689     g_assert(waitpid(pid, &wstatus, 0) == pid);
1690     if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus) != 0) {
1691         g_test_message("Failed to analyze the migration stream");
1692         g_test_fail();
1693     }
1694     test_migrate_end(from, to, false);
1695     cleanup("migfile");
1696 }
1697 #endif
1698 
1699 static void test_precopy_common(MigrateCommon *args)
1700 {
1701     QTestState *from, *to;
1702     void *data_hook = NULL;
1703 
1704     if (test_migrate_start(&from, &to, args->listen_uri, &args->start)) {
1705         return;
1706     }
1707 
1708     if (args->start_hook) {
1709         data_hook = args->start_hook(from, to);
1710     }
1711 
1712     /* Wait for the first serial output from the source */
1713     if (args->result == MIG_TEST_SUCCEED) {
1714         wait_for_serial("src_serial");
1715         wait_for_suspend(from, &src_state);
1716     }
1717 
1718     if (args->live) {
1719         migrate_ensure_non_converge(from);
1720         migrate_prepare_for_dirty_mem(from);
1721     } else {
1722         /*
1723          * Testing non-live migration, we allow it to run at
1724          * full speed to ensure short test case duration.
1725          * For tests expected to fail, we don't need to
1726          * change anything.
1727          */
1728         if (args->result == MIG_TEST_SUCCEED) {
1729             qtest_qmp_assert_success(from, "{ 'execute' : 'stop'}");
1730             wait_for_stop(from, &src_state);
1731             migrate_ensure_converge(from);
1732         }
1733     }
1734 
1735     if (args->result == MIG_TEST_QMP_ERROR) {
1736         migrate_qmp_fail(from, args->connect_uri, args->connect_channels, "{}");
1737         goto finish;
1738     }
1739 
1740     migrate_qmp(from, to, args->connect_uri, args->connect_channels, "{}");
1741 
1742     if (args->result != MIG_TEST_SUCCEED) {
1743         bool allow_active = args->result == MIG_TEST_FAIL;
1744         wait_for_migration_fail(from, allow_active);
1745 
1746         if (args->result == MIG_TEST_FAIL_DEST_QUIT_ERR) {
1747             qtest_set_expected_status(to, EXIT_FAILURE);
1748         }
1749     } else {
1750         if (args->live) {
1751             /*
1752              * For initial iteration(s) we must do a full pass,
1753              * but for the final iteration, we need only wait
1754              * for some dirty mem before switching to converge
1755              */
1756             while (args->iterations > 1) {
1757                 wait_for_migration_pass(from);
1758                 args->iterations--;
1759             }
1760             migrate_wait_for_dirty_mem(from, to);
1761 
1762             migrate_ensure_converge(from);
1763 
1764             /*
1765              * We do this first, as it has a timeout to stop us
1766              * hanging forever if migration didn't converge
1767              */
1768             wait_for_migration_complete(from);
1769 
1770             wait_for_stop(from, &src_state);
1771 
1772         } else {
1773             wait_for_migration_complete(from);
1774             /*
1775              * Must wait for dst to finish reading all incoming
1776              * data on the socket before issuing 'cont' otherwise
1777              * it'll be ignored
1778              */
1779             wait_for_migration_complete(to);
1780 
1781             qtest_qmp_assert_success(to, "{ 'execute' : 'cont'}");
1782         }
1783 
1784         wait_for_resume(to, &dst_state);
1785 
1786         if (args->start.suspend_me) {
1787             /* wakeup succeeds only if guest is suspended */
1788             qtest_qmp_assert_success(to, "{'execute': 'system_wakeup'}");
1789         }
1790 
1791         wait_for_serial("dest_serial");
1792     }
1793 
1794 finish:
1795     if (args->finish_hook) {
1796         args->finish_hook(from, to, data_hook);
1797     }
1798 
1799     test_migrate_end(from, to, args->result == MIG_TEST_SUCCEED);
1800 }
1801 
1802 static void file_dirty_offset_region(void)
1803 {
1804     g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, FILE_TEST_FILENAME);
1805     size_t size = FILE_TEST_OFFSET;
1806     g_autofree char *data = g_new0(char, size);
1807 
1808     memset(data, FILE_TEST_MARKER, size);
1809     g_assert(g_file_set_contents(path, data, size, NULL));
1810 }
1811 
1812 static void file_check_offset_region(void)
1813 {
1814     g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, FILE_TEST_FILENAME);
1815     size_t size = FILE_TEST_OFFSET;
1816     g_autofree char *expected = g_new0(char, size);
1817     g_autofree char *actual = NULL;
1818     uint64_t *stream_start;
1819 
1820     /*
1821      * Ensure the skipped offset region's data has not been touched
1822      * and the migration stream starts at the right place.
1823      */
1824 
1825     memset(expected, FILE_TEST_MARKER, size);
1826 
1827     g_assert(g_file_get_contents(path, &actual, NULL, NULL));
1828     g_assert(!memcmp(actual, expected, size));
1829 
1830     stream_start = (uint64_t *)(actual + size);
1831     g_assert_cmpint(cpu_to_be64(*stream_start) >> 32, ==, QEMU_VM_FILE_MAGIC);
1832 }
1833 
1834 static void test_file_common(MigrateCommon *args, bool stop_src)
1835 {
1836     QTestState *from, *to;
1837     void *data_hook = NULL;
1838     bool check_offset = false;
1839 
1840     if (test_migrate_start(&from, &to, args->listen_uri, &args->start)) {
1841         return;
1842     }
1843 
1844     /*
1845      * File migration is never live. We can keep the source VM running
1846      * during migration, but the destination will not be running
1847      * concurrently.
1848      */
1849     g_assert_false(args->live);
1850 
1851     if (g_strrstr(args->connect_uri, "offset=")) {
1852         check_offset = true;
1853         /*
1854          * This comes before the start_hook because it's equivalent to
1855          * a management application creating the file and writing to
1856          * it so hooks should expect the file to be already present.
1857          */
1858         file_dirty_offset_region();
1859     }
1860 
1861     if (args->start_hook) {
1862         data_hook = args->start_hook(from, to);
1863     }
1864 
1865     migrate_ensure_converge(from);
1866     wait_for_serial("src_serial");
1867 
1868     if (stop_src) {
1869         qtest_qmp_assert_success(from, "{ 'execute' : 'stop'}");
1870         wait_for_stop(from, &src_state);
1871     }
1872 
1873     if (args->result == MIG_TEST_QMP_ERROR) {
1874         migrate_qmp_fail(from, args->connect_uri, NULL, "{}");
1875         goto finish;
1876     }
1877 
1878     migrate_qmp(from, to, args->connect_uri, NULL, "{}");
1879     wait_for_migration_complete(from);
1880 
1881     /*
1882      * We need to wait for the source to finish before starting the
1883      * destination.
1884      */
1885     migrate_incoming_qmp(to, args->connect_uri, "{}");
1886     wait_for_migration_complete(to);
1887 
1888     if (stop_src) {
1889         qtest_qmp_assert_success(to, "{ 'execute' : 'cont'}");
1890     }
1891     wait_for_resume(to, &dst_state);
1892 
1893     wait_for_serial("dest_serial");
1894 
1895     if (check_offset) {
1896         file_check_offset_region();
1897     }
1898 
1899 finish:
1900     if (args->finish_hook) {
1901         args->finish_hook(from, to, data_hook);
1902     }
1903 
1904     test_migrate_end(from, to, args->result == MIG_TEST_SUCCEED);
1905 }
1906 
1907 static void test_precopy_unix_plain(void)
1908 {
1909     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1910     MigrateCommon args = {
1911         .listen_uri = uri,
1912         .connect_uri = uri,
1913         /*
1914          * The simplest use case of precopy, covering smoke tests of
1915          * get-dirty-log dirty tracking.
1916          */
1917         .live = true,
1918     };
1919 
1920     test_precopy_common(&args);
1921 }
1922 
1923 static void test_precopy_unix_suspend_live(void)
1924 {
1925     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1926     MigrateCommon args = {
1927         .listen_uri = uri,
1928         .connect_uri = uri,
1929         /*
1930          * despite being live, the test is fast because the src
1931          * suspends immediately.
1932          */
1933         .live = true,
1934         .start.suspend_me = true,
1935     };
1936 
1937     test_precopy_common(&args);
1938 }
1939 
1940 static void test_precopy_unix_suspend_notlive(void)
1941 {
1942     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1943     MigrateCommon args = {
1944         .listen_uri = uri,
1945         .connect_uri = uri,
1946         .start.suspend_me = true,
1947     };
1948 
1949     test_precopy_common(&args);
1950 }
1951 
1952 static void test_precopy_unix_dirty_ring(void)
1953 {
1954     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1955     MigrateCommon args = {
1956         .start = {
1957             .use_dirty_ring = true,
1958         },
1959         .listen_uri = uri,
1960         .connect_uri = uri,
1961         /*
1962          * Besides the precopy/unix basic test, cover dirty ring interface
1963          * rather than get-dirty-log.
1964          */
1965         .live = true,
1966     };
1967 
1968     test_precopy_common(&args);
1969 }
1970 
1971 #ifdef CONFIG_GNUTLS
1972 static void test_precopy_unix_tls_psk(void)
1973 {
1974     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1975     MigrateCommon args = {
1976         .connect_uri = uri,
1977         .listen_uri = uri,
1978         .start_hook = test_migrate_tls_psk_start_match,
1979         .finish_hook = test_migrate_tls_psk_finish,
1980     };
1981 
1982     test_precopy_common(&args);
1983 }
1984 
1985 #ifdef CONFIG_TASN1
1986 static void test_precopy_unix_tls_x509_default_host(void)
1987 {
1988     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1989     MigrateCommon args = {
1990         .start = {
1991             .hide_stderr = true,
1992         },
1993         .connect_uri = uri,
1994         .listen_uri = uri,
1995         .start_hook = test_migrate_tls_x509_start_default_host,
1996         .finish_hook = test_migrate_tls_x509_finish,
1997         .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
1998     };
1999 
2000     test_precopy_common(&args);
2001 }
2002 
2003 static void test_precopy_unix_tls_x509_override_host(void)
2004 {
2005     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
2006     MigrateCommon args = {
2007         .connect_uri = uri,
2008         .listen_uri = uri,
2009         .start_hook = test_migrate_tls_x509_start_override_host,
2010         .finish_hook = test_migrate_tls_x509_finish,
2011     };
2012 
2013     test_precopy_common(&args);
2014 }
2015 #endif /* CONFIG_TASN1 */
2016 #endif /* CONFIG_GNUTLS */
2017 
2018 #if 0
2019 /* Currently upset on aarch64 TCG */
2020 static void test_ignore_shared(void)
2021 {
2022     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
2023     QTestState *from, *to;
2024 
2025     if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) {
2026         return;
2027     }
2028 
2029     migrate_ensure_non_converge(from);
2030     migrate_prepare_for_dirty_mem(from);
2031 
2032     migrate_set_capability(from, "x-ignore-shared", true);
2033     migrate_set_capability(to, "x-ignore-shared", true);
2034 
2035     /* Wait for the first serial output from the source */
2036     wait_for_serial("src_serial");
2037 
2038     migrate_qmp(from, to, uri, NULL, "{}");
2039 
2040     migrate_wait_for_dirty_mem(from, to);
2041 
2042     wait_for_stop(from, &src_state);
2043 
2044     qtest_qmp_eventwait(to, "RESUME");
2045 
2046     wait_for_serial("dest_serial");
2047     wait_for_migration_complete(from);
2048 
2049     /* Check whether shared RAM has been really skipped */
2050     g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
2051 
2052     test_migrate_end(from, to, true);
2053 }
2054 #endif
2055 
2056 static void *
2057 test_migrate_xbzrle_start(QTestState *from,
2058                           QTestState *to)
2059 {
2060     migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
2061 
2062     migrate_set_capability(from, "xbzrle", true);
2063     migrate_set_capability(to, "xbzrle", true);
2064 
2065     return NULL;
2066 }
2067 
2068 static void test_precopy_unix_xbzrle(void)
2069 {
2070     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
2071     MigrateCommon args = {
2072         .connect_uri = uri,
2073         .listen_uri = uri,
2074         .start_hook = test_migrate_xbzrle_start,
2075         .iterations = 2,
2076         /*
2077          * XBZRLE needs pages to be modified when doing the 2nd+ round
2078          * iteration to have real data pushed to the stream.
2079          */
2080         .live = true,
2081     };
2082 
2083     test_precopy_common(&args);
2084 }
2085 
2086 static void test_precopy_file(void)
2087 {
2088     g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
2089                                            FILE_TEST_FILENAME);
2090     MigrateCommon args = {
2091         .connect_uri = uri,
2092         .listen_uri = "defer",
2093     };
2094 
2095     test_file_common(&args, true);
2096 }
2097 
2098 #ifndef _WIN32
2099 static void fdset_add_fds(QTestState *qts, const char *file, int flags,
2100                           int num_fds, bool direct_io)
2101 {
2102     for (int i = 0; i < num_fds; i++) {
2103         int fd;
2104 
2105 #ifdef O_DIRECT
2106         /* only secondary channels can use direct-io */
2107         if (direct_io && i != 0) {
2108             flags |= O_DIRECT;
2109         }
2110 #endif
2111 
2112         fd = open(file, flags, 0660);
2113         assert(fd != -1);
2114 
2115         qtest_qmp_fds_assert_success(qts, &fd, 1, "{'execute': 'add-fd', "
2116                                      "'arguments': {'fdset-id': 1}}");
2117         close(fd);
2118     }
2119 }
2120 
2121 static void *file_offset_fdset_start_hook(QTestState *from, QTestState *to)
2122 {
2123     g_autofree char *file = g_strdup_printf("%s/%s", tmpfs, FILE_TEST_FILENAME);
2124 
2125     fdset_add_fds(from, file, O_WRONLY, 1, false);
2126     fdset_add_fds(to, file, O_RDONLY, 1, false);
2127 
2128     return NULL;
2129 }
2130 
2131 static void test_precopy_file_offset_fdset(void)
2132 {
2133     g_autofree char *uri = g_strdup_printf("file:/dev/fdset/1,offset=%d",
2134                                            FILE_TEST_OFFSET);
2135     MigrateCommon args = {
2136         .connect_uri = uri,
2137         .listen_uri = "defer",
2138         .start_hook = file_offset_fdset_start_hook,
2139     };
2140 
2141     test_file_common(&args, false);
2142 }
2143 #endif
2144 
2145 static void test_precopy_file_offset(void)
2146 {
2147     g_autofree char *uri = g_strdup_printf("file:%s/%s,offset=%d", tmpfs,
2148                                            FILE_TEST_FILENAME,
2149                                            FILE_TEST_OFFSET);
2150     MigrateCommon args = {
2151         .connect_uri = uri,
2152         .listen_uri = "defer",
2153     };
2154 
2155     test_file_common(&args, false);
2156 }
2157 
2158 static void test_precopy_file_offset_bad(void)
2159 {
2160     /* using a value not supported by qemu_strtosz() */
2161     g_autofree char *uri = g_strdup_printf("file:%s/%s,offset=0x20M",
2162                                            tmpfs, FILE_TEST_FILENAME);
2163     MigrateCommon args = {
2164         .connect_uri = uri,
2165         .listen_uri = "defer",
2166         .result = MIG_TEST_QMP_ERROR,
2167     };
2168 
2169     test_file_common(&args, false);
2170 }
2171 
2172 static void *test_mode_reboot_start(QTestState *from, QTestState *to)
2173 {
2174     migrate_set_parameter_str(from, "mode", "cpr-reboot");
2175     migrate_set_parameter_str(to, "mode", "cpr-reboot");
2176 
2177     migrate_set_capability(from, "x-ignore-shared", true);
2178     migrate_set_capability(to, "x-ignore-shared", true);
2179 
2180     return NULL;
2181 }
2182 
2183 static void *migrate_mapped_ram_start(QTestState *from, QTestState *to)
2184 {
2185     migrate_set_capability(from, "mapped-ram", true);
2186     migrate_set_capability(to, "mapped-ram", true);
2187 
2188     return NULL;
2189 }
2190 
2191 static void test_mode_reboot(void)
2192 {
2193     g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
2194                                            FILE_TEST_FILENAME);
2195     MigrateCommon args = {
2196         .start.use_shmem = true,
2197         .connect_uri = uri,
2198         .listen_uri = "defer",
2199         .start_hook = test_mode_reboot_start
2200     };
2201 
2202     test_file_common(&args, true);
2203 }
2204 
2205 static void test_precopy_file_mapped_ram_live(void)
2206 {
2207     g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
2208                                            FILE_TEST_FILENAME);
2209     MigrateCommon args = {
2210         .connect_uri = uri,
2211         .listen_uri = "defer",
2212         .start_hook = migrate_mapped_ram_start,
2213     };
2214 
2215     test_file_common(&args, false);
2216 }
2217 
2218 static void test_precopy_file_mapped_ram(void)
2219 {
2220     g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
2221                                            FILE_TEST_FILENAME);
2222     MigrateCommon args = {
2223         .connect_uri = uri,
2224         .listen_uri = "defer",
2225         .start_hook = migrate_mapped_ram_start,
2226     };
2227 
2228     test_file_common(&args, true);
2229 }
2230 
2231 static void *migrate_multifd_mapped_ram_start(QTestState *from, QTestState *to)
2232 {
2233     migrate_mapped_ram_start(from, to);
2234 
2235     migrate_set_parameter_int(from, "multifd-channels", 4);
2236     migrate_set_parameter_int(to, "multifd-channels", 4);
2237 
2238     migrate_set_capability(from, "multifd", true);
2239     migrate_set_capability(to, "multifd", true);
2240 
2241     return NULL;
2242 }
2243 
2244 static void test_multifd_file_mapped_ram_live(void)
2245 {
2246     g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
2247                                            FILE_TEST_FILENAME);
2248     MigrateCommon args = {
2249         .connect_uri = uri,
2250         .listen_uri = "defer",
2251         .start_hook = migrate_multifd_mapped_ram_start,
2252     };
2253 
2254     test_file_common(&args, false);
2255 }
2256 
2257 static void test_multifd_file_mapped_ram(void)
2258 {
2259     g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
2260                                            FILE_TEST_FILENAME);
2261     MigrateCommon args = {
2262         .connect_uri = uri,
2263         .listen_uri = "defer",
2264         .start_hook = migrate_multifd_mapped_ram_start,
2265     };
2266 
2267     test_file_common(&args, true);
2268 }
2269 
2270 static void *multifd_mapped_ram_dio_start(QTestState *from, QTestState *to)
2271 {
2272     migrate_multifd_mapped_ram_start(from, to);
2273 
2274     migrate_set_parameter_bool(from, "direct-io", true);
2275     migrate_set_parameter_bool(to, "direct-io", true);
2276 
2277     return NULL;
2278 }
2279 
2280 static void test_multifd_file_mapped_ram_dio(void)
2281 {
2282     g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
2283                                            FILE_TEST_FILENAME);
2284     MigrateCommon args = {
2285         .connect_uri = uri,
2286         .listen_uri = "defer",
2287         .start_hook = multifd_mapped_ram_dio_start,
2288     };
2289 
2290     if (!probe_o_direct_support(tmpfs)) {
2291         g_test_skip("Filesystem does not support O_DIRECT");
2292         return;
2293     }
2294 
2295     test_file_common(&args, true);
2296 }
2297 
2298 #ifndef _WIN32
2299 static void multifd_mapped_ram_fdset_end(QTestState *from, QTestState *to,
2300                                          void *opaque)
2301 {
2302     QDict *resp;
2303     QList *fdsets;
2304 
2305     /*
2306      * Remove the fdsets after migration, otherwise a second migration
2307      * would fail due fdset reuse.
2308      */
2309     qtest_qmp_assert_success(from, "{'execute': 'remove-fd', "
2310                              "'arguments': { 'fdset-id': 1}}");
2311 
2312     /*
2313      * Make sure no fdsets are left after migration, otherwise a
2314      * second migration would fail due fdset reuse.
2315      */
2316     resp = qtest_qmp(from, "{'execute': 'query-fdsets', "
2317                      "'arguments': {}}");
2318     g_assert(qdict_haskey(resp, "return"));
2319     fdsets = qdict_get_qlist(resp, "return");
2320     g_assert(fdsets && qlist_empty(fdsets));
2321     qobject_unref(resp);
2322 }
2323 
2324 static void *multifd_mapped_ram_fdset_dio(QTestState *from, QTestState *to)
2325 {
2326     g_autofree char *file = g_strdup_printf("%s/%s", tmpfs, FILE_TEST_FILENAME);
2327 
2328     fdset_add_fds(from, file, O_WRONLY, 2, true);
2329     fdset_add_fds(to, file, O_RDONLY, 2, true);
2330 
2331     migrate_multifd_mapped_ram_start(from, to);
2332     migrate_set_parameter_bool(from, "direct-io", true);
2333     migrate_set_parameter_bool(to, "direct-io", true);
2334 
2335     return NULL;
2336 }
2337 
2338 static void *multifd_mapped_ram_fdset(QTestState *from, QTestState *to)
2339 {
2340     g_autofree char *file = g_strdup_printf("%s/%s", tmpfs, FILE_TEST_FILENAME);
2341 
2342     fdset_add_fds(from, file, O_WRONLY, 2, false);
2343     fdset_add_fds(to, file, O_RDONLY, 2, false);
2344 
2345     migrate_multifd_mapped_ram_start(from, to);
2346 
2347     return NULL;
2348 }
2349 
2350 static void test_multifd_file_mapped_ram_fdset(void)
2351 {
2352     g_autofree char *uri = g_strdup_printf("file:/dev/fdset/1,offset=%d",
2353                                            FILE_TEST_OFFSET);
2354     MigrateCommon args = {
2355         .connect_uri = uri,
2356         .listen_uri = "defer",
2357         .start_hook = multifd_mapped_ram_fdset,
2358         .finish_hook = multifd_mapped_ram_fdset_end,
2359     };
2360 
2361     test_file_common(&args, true);
2362 }
2363 
2364 static void test_multifd_file_mapped_ram_fdset_dio(void)
2365 {
2366     g_autofree char *uri = g_strdup_printf("file:/dev/fdset/1,offset=%d",
2367                                            FILE_TEST_OFFSET);
2368     MigrateCommon args = {
2369         .connect_uri = uri,
2370         .listen_uri = "defer",
2371         .start_hook = multifd_mapped_ram_fdset_dio,
2372         .finish_hook = multifd_mapped_ram_fdset_end,
2373     };
2374 
2375     if (!probe_o_direct_support(tmpfs)) {
2376         g_test_skip("Filesystem does not support O_DIRECT");
2377         return;
2378     }
2379 
2380     test_file_common(&args, true);
2381 }
2382 #endif /* !_WIN32 */
2383 
2384 static void test_precopy_tcp_plain(void)
2385 {
2386     MigrateCommon args = {
2387         .listen_uri = "tcp:127.0.0.1:0",
2388     };
2389 
2390     test_precopy_common(&args);
2391 }
2392 
2393 static void *test_migrate_switchover_ack_start(QTestState *from, QTestState *to)
2394 {
2395 
2396     migrate_set_capability(from, "return-path", true);
2397     migrate_set_capability(to, "return-path", true);
2398 
2399     migrate_set_capability(from, "switchover-ack", true);
2400     migrate_set_capability(to, "switchover-ack", true);
2401 
2402     return NULL;
2403 }
2404 
2405 static void test_precopy_tcp_switchover_ack(void)
2406 {
2407     MigrateCommon args = {
2408         .listen_uri = "tcp:127.0.0.1:0",
2409         .start_hook = test_migrate_switchover_ack_start,
2410         /*
2411          * Source VM must be running in order to consider the switchover ACK
2412          * when deciding to do switchover or not.
2413          */
2414         .live = true,
2415     };
2416 
2417     test_precopy_common(&args);
2418 }
2419 
2420 #ifdef CONFIG_GNUTLS
2421 static void test_precopy_tcp_tls_psk_match(void)
2422 {
2423     MigrateCommon args = {
2424         .listen_uri = "tcp:127.0.0.1:0",
2425         .start_hook = test_migrate_tls_psk_start_match,
2426         .finish_hook = test_migrate_tls_psk_finish,
2427     };
2428 
2429     test_precopy_common(&args);
2430 }
2431 
2432 static void test_precopy_tcp_tls_psk_mismatch(void)
2433 {
2434     MigrateCommon args = {
2435         .start = {
2436             .hide_stderr = true,
2437         },
2438         .listen_uri = "tcp:127.0.0.1:0",
2439         .start_hook = test_migrate_tls_psk_start_mismatch,
2440         .finish_hook = test_migrate_tls_psk_finish,
2441         .result = MIG_TEST_FAIL,
2442     };
2443 
2444     test_precopy_common(&args);
2445 }
2446 
2447 #ifdef CONFIG_TASN1
2448 static void test_precopy_tcp_tls_x509_default_host(void)
2449 {
2450     MigrateCommon args = {
2451         .listen_uri = "tcp:127.0.0.1:0",
2452         .start_hook = test_migrate_tls_x509_start_default_host,
2453         .finish_hook = test_migrate_tls_x509_finish,
2454     };
2455 
2456     test_precopy_common(&args);
2457 }
2458 
2459 static void test_precopy_tcp_tls_x509_override_host(void)
2460 {
2461     MigrateCommon args = {
2462         .listen_uri = "tcp:127.0.0.1:0",
2463         .start_hook = test_migrate_tls_x509_start_override_host,
2464         .finish_hook = test_migrate_tls_x509_finish,
2465     };
2466 
2467     test_precopy_common(&args);
2468 }
2469 
2470 static void test_precopy_tcp_tls_x509_mismatch_host(void)
2471 {
2472     MigrateCommon args = {
2473         .start = {
2474             .hide_stderr = true,
2475         },
2476         .listen_uri = "tcp:127.0.0.1:0",
2477         .start_hook = test_migrate_tls_x509_start_mismatch_host,
2478         .finish_hook = test_migrate_tls_x509_finish,
2479         .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
2480     };
2481 
2482     test_precopy_common(&args);
2483 }
2484 
2485 static void test_precopy_tcp_tls_x509_friendly_client(void)
2486 {
2487     MigrateCommon args = {
2488         .listen_uri = "tcp:127.0.0.1:0",
2489         .start_hook = test_migrate_tls_x509_start_friendly_client,
2490         .finish_hook = test_migrate_tls_x509_finish,
2491     };
2492 
2493     test_precopy_common(&args);
2494 }
2495 
2496 static void test_precopy_tcp_tls_x509_hostile_client(void)
2497 {
2498     MigrateCommon args = {
2499         .start = {
2500             .hide_stderr = true,
2501         },
2502         .listen_uri = "tcp:127.0.0.1:0",
2503         .start_hook = test_migrate_tls_x509_start_hostile_client,
2504         .finish_hook = test_migrate_tls_x509_finish,
2505         .result = MIG_TEST_FAIL,
2506     };
2507 
2508     test_precopy_common(&args);
2509 }
2510 
2511 static void test_precopy_tcp_tls_x509_allow_anon_client(void)
2512 {
2513     MigrateCommon args = {
2514         .listen_uri = "tcp:127.0.0.1:0",
2515         .start_hook = test_migrate_tls_x509_start_allow_anon_client,
2516         .finish_hook = test_migrate_tls_x509_finish,
2517     };
2518 
2519     test_precopy_common(&args);
2520 }
2521 
2522 static void test_precopy_tcp_tls_x509_reject_anon_client(void)
2523 {
2524     MigrateCommon args = {
2525         .start = {
2526             .hide_stderr = true,
2527         },
2528         .listen_uri = "tcp:127.0.0.1:0",
2529         .start_hook = test_migrate_tls_x509_start_reject_anon_client,
2530         .finish_hook = test_migrate_tls_x509_finish,
2531         .result = MIG_TEST_FAIL,
2532     };
2533 
2534     test_precopy_common(&args);
2535 }
2536 #endif /* CONFIG_TASN1 */
2537 #endif /* CONFIG_GNUTLS */
2538 
2539 #ifndef _WIN32
2540 static void *test_migrate_fd_start_hook(QTestState *from,
2541                                         QTestState *to)
2542 {
2543     int ret;
2544     int pair[2];
2545 
2546     /* Create two connected sockets for migration */
2547     ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
2548     g_assert_cmpint(ret, ==, 0);
2549 
2550     /* Send the 1st socket to the target */
2551     qtest_qmp_fds_assert_success(to, &pair[0], 1,
2552                                  "{ 'execute': 'getfd',"
2553                                  "  'arguments': { 'fdname': 'fd-mig' }}");
2554     close(pair[0]);
2555 
2556     /* Start incoming migration from the 1st socket */
2557     migrate_incoming_qmp(to, "fd:fd-mig", "{}");
2558 
2559     /* Send the 2nd socket to the target */
2560     qtest_qmp_fds_assert_success(from, &pair[1], 1,
2561                                  "{ 'execute': 'getfd',"
2562                                  "  'arguments': { 'fdname': 'fd-mig' }}");
2563     close(pair[1]);
2564 
2565     return NULL;
2566 }
2567 
2568 static void test_migrate_fd_finish_hook(QTestState *from,
2569                                         QTestState *to,
2570                                         void *opaque)
2571 {
2572     QDict *rsp;
2573     const char *error_desc;
2574 
2575     /* Test closing fds */
2576     /* We assume, that QEMU removes named fd from its list,
2577      * so this should fail */
2578     rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
2579                           "  'arguments': { 'fdname': 'fd-mig' }}");
2580     g_assert_true(qdict_haskey(rsp, "error"));
2581     error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
2582     g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
2583     qobject_unref(rsp);
2584 
2585     rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
2586                         "  'arguments': { 'fdname': 'fd-mig' }}");
2587     g_assert_true(qdict_haskey(rsp, "error"));
2588     error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
2589     g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
2590     qobject_unref(rsp);
2591 }
2592 
2593 static void test_migrate_precopy_fd_socket(void)
2594 {
2595     MigrateCommon args = {
2596         .listen_uri = "defer",
2597         .connect_uri = "fd:fd-mig",
2598         .start_hook = test_migrate_fd_start_hook,
2599         .finish_hook = test_migrate_fd_finish_hook
2600     };
2601     test_precopy_common(&args);
2602 }
2603 
2604 static void *migrate_precopy_fd_file_start(QTestState *from, QTestState *to)
2605 {
2606     g_autofree char *file = g_strdup_printf("%s/%s", tmpfs, FILE_TEST_FILENAME);
2607     int src_flags = O_CREAT | O_RDWR;
2608     int dst_flags = O_CREAT | O_RDWR;
2609     int fds[2];
2610 
2611     fds[0] = open(file, src_flags, 0660);
2612     assert(fds[0] != -1);
2613 
2614     fds[1] = open(file, dst_flags, 0660);
2615     assert(fds[1] != -1);
2616 
2617 
2618     qtest_qmp_fds_assert_success(to, &fds[0], 1,
2619                                  "{ 'execute': 'getfd',"
2620                                  "  'arguments': { 'fdname': 'fd-mig' }}");
2621 
2622     qtest_qmp_fds_assert_success(from, &fds[1], 1,
2623                                  "{ 'execute': 'getfd',"
2624                                  "  'arguments': { 'fdname': 'fd-mig' }}");
2625 
2626     close(fds[0]);
2627     close(fds[1]);
2628 
2629     return NULL;
2630 }
2631 
2632 static void test_migrate_precopy_fd_file(void)
2633 {
2634     MigrateCommon args = {
2635         .listen_uri = "defer",
2636         .connect_uri = "fd:fd-mig",
2637         .start_hook = migrate_precopy_fd_file_start,
2638         .finish_hook = test_migrate_fd_finish_hook
2639     };
2640     test_file_common(&args, true);
2641 }
2642 #endif /* _WIN32 */
2643 
2644 static void do_test_validate_uuid(MigrateStart *args, bool should_fail)
2645 {
2646     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
2647     QTestState *from, *to;
2648 
2649     if (test_migrate_start(&from, &to, uri, args)) {
2650         return;
2651     }
2652 
2653     /*
2654      * UUID validation is at the begin of migration. So, the main process of
2655      * migration is not interesting for us here. Thus, set huge downtime for
2656      * very fast migration.
2657      */
2658     migrate_set_parameter_int(from, "downtime-limit", 1000000);
2659     migrate_set_capability(from, "validate-uuid", true);
2660 
2661     /* Wait for the first serial output from the source */
2662     wait_for_serial("src_serial");
2663 
2664     migrate_qmp(from, to, uri, NULL, "{}");
2665 
2666     if (should_fail) {
2667         qtest_set_expected_status(to, EXIT_FAILURE);
2668         wait_for_migration_fail(from, true);
2669     } else {
2670         wait_for_migration_complete(from);
2671     }
2672 
2673     test_migrate_end(from, to, false);
2674 }
2675 
2676 static void test_validate_uuid(void)
2677 {
2678     MigrateStart args = {
2679         .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
2680         .opts_target = "-uuid 11111111-1111-1111-1111-111111111111",
2681     };
2682 
2683     do_test_validate_uuid(&args, false);
2684 }
2685 
2686 static void test_validate_uuid_error(void)
2687 {
2688     MigrateStart args = {
2689         .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
2690         .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
2691         .hide_stderr = true,
2692     };
2693 
2694     do_test_validate_uuid(&args, true);
2695 }
2696 
2697 static void test_validate_uuid_src_not_set(void)
2698 {
2699     MigrateStart args = {
2700         .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
2701         .hide_stderr = true,
2702     };
2703 
2704     do_test_validate_uuid(&args, false);
2705 }
2706 
2707 static void test_validate_uuid_dst_not_set(void)
2708 {
2709     MigrateStart args = {
2710         .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
2711         .hide_stderr = true,
2712     };
2713 
2714     do_test_validate_uuid(&args, false);
2715 }
2716 
2717 static void do_test_validate_uri_channel(MigrateCommon *args)
2718 {
2719     QTestState *from, *to;
2720 
2721     if (test_migrate_start(&from, &to, args->listen_uri, &args->start)) {
2722         return;
2723     }
2724 
2725     /* Wait for the first serial output from the source */
2726     wait_for_serial("src_serial");
2727 
2728     /*
2729      * 'uri' and 'channels' validation is checked even before the migration
2730      * starts.
2731      */
2732     migrate_qmp_fail(from, args->connect_uri, args->connect_channels, "{}");
2733     test_migrate_end(from, to, false);
2734 }
2735 
2736 static void test_validate_uri_channels_both_set(void)
2737 {
2738     MigrateCommon args = {
2739         .start = {
2740             .hide_stderr = true,
2741         },
2742         .listen_uri = "defer",
2743         .connect_uri = "tcp:127.0.0.1:0",
2744         .connect_channels = "[ { 'channel-type': 'main',"
2745                             "    'addr': { 'transport': 'socket',"
2746                             "              'type': 'inet',"
2747                             "              'host': '127.0.0.1',"
2748                             "              'port': '0' } } ]",
2749     };
2750 
2751     do_test_validate_uri_channel(&args);
2752 }
2753 
2754 static void test_validate_uri_channels_none_set(void)
2755 {
2756     MigrateCommon args = {
2757         .start = {
2758             .hide_stderr = true,
2759         },
2760         .listen_uri = "defer",
2761     };
2762 
2763     do_test_validate_uri_channel(&args);
2764 }
2765 
2766 /*
2767  * The way auto_converge works, we need to do too many passes to
2768  * run this test.  Auto_converge logic is only run once every
2769  * three iterations, so:
2770  *
2771  * - 3 iterations without auto_converge enabled
2772  * - 3 iterations with pct = 5
2773  * - 3 iterations with pct = 30
2774  * - 3 iterations with pct = 55
2775  * - 3 iterations with pct = 80
2776  * - 3 iterations with pct = 95 (max(95, 80 + 25))
2777  *
2778  * To make things even worse, we need to run the initial stage at
2779  * 3MB/s so we enter autoconverge even when host is (over)loaded.
2780  */
2781 static void test_migrate_auto_converge(void)
2782 {
2783     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
2784     MigrateStart args = {};
2785     QTestState *from, *to;
2786     int64_t percentage;
2787 
2788     /*
2789      * We want the test to be stable and as fast as possible.
2790      * E.g., with 1Gb/s bandwidth migration may pass without throttling,
2791      * so we need to decrease a bandwidth.
2792      */
2793     const int64_t init_pct = 5, inc_pct = 25, max_pct = 95;
2794 
2795     if (test_migrate_start(&from, &to, uri, &args)) {
2796         return;
2797     }
2798 
2799     migrate_set_capability(from, "auto-converge", true);
2800     migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct);
2801     migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct);
2802     migrate_set_parameter_int(from, "max-cpu-throttle", max_pct);
2803 
2804     /*
2805      * Set the initial parameters so that the migration could not converge
2806      * without throttling.
2807      */
2808     migrate_ensure_non_converge(from);
2809 
2810     /* To check remaining size after precopy */
2811     migrate_set_capability(from, "pause-before-switchover", true);
2812 
2813     /* Wait for the first serial output from the source */
2814     wait_for_serial("src_serial");
2815 
2816     migrate_qmp(from, to, uri, NULL, "{}");
2817 
2818     /* Wait for throttling begins */
2819     percentage = 0;
2820     do {
2821         percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
2822         if (percentage != 0) {
2823             break;
2824         }
2825         usleep(20);
2826         g_assert_false(src_state.stop_seen);
2827     } while (true);
2828     /* The first percentage of throttling should be at least init_pct */
2829     g_assert_cmpint(percentage, >=, init_pct);
2830     /* Now, when we tested that throttling works, let it converge */
2831     migrate_ensure_converge(from);
2832 
2833     /*
2834      * Wait for pre-switchover status to check last throttle percentage
2835      * and remaining. These values will be zeroed later
2836      */
2837     wait_for_migration_status(from, "pre-switchover", NULL);
2838 
2839     /* The final percentage of throttling shouldn't be greater than max_pct */
2840     percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
2841     g_assert_cmpint(percentage, <=, max_pct);
2842     migrate_continue(from, "pre-switchover");
2843 
2844     qtest_qmp_eventwait(to, "RESUME");
2845 
2846     wait_for_serial("dest_serial");
2847     wait_for_migration_complete(from);
2848 
2849     test_migrate_end(from, to, true);
2850 }
2851 
2852 static void *
2853 test_migrate_precopy_tcp_multifd_start_common(QTestState *from,
2854                                               QTestState *to,
2855                                               const char *method)
2856 {
2857     migrate_set_parameter_int(from, "multifd-channels", 16);
2858     migrate_set_parameter_int(to, "multifd-channels", 16);
2859 
2860     migrate_set_parameter_str(from, "multifd-compression", method);
2861     migrate_set_parameter_str(to, "multifd-compression", method);
2862 
2863     migrate_set_capability(from, "multifd", true);
2864     migrate_set_capability(to, "multifd", true);
2865 
2866     /* Start incoming migration from the 1st socket */
2867     migrate_incoming_qmp(to, "tcp:127.0.0.1:0", "{}");
2868 
2869     return NULL;
2870 }
2871 
2872 static void *
2873 test_migrate_precopy_tcp_multifd_start(QTestState *from,
2874                                        QTestState *to)
2875 {
2876     return test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2877 }
2878 
2879 static void *
2880 test_migrate_precopy_tcp_multifd_start_zero_page_legacy(QTestState *from,
2881                                                         QTestState *to)
2882 {
2883     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2884     migrate_set_parameter_str(from, "zero-page-detection", "legacy");
2885     return NULL;
2886 }
2887 
2888 static void *
2889 test_migration_precopy_tcp_multifd_start_no_zero_page(QTestState *from,
2890                                                       QTestState *to)
2891 {
2892     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2893     migrate_set_parameter_str(from, "zero-page-detection", "none");
2894     return NULL;
2895 }
2896 
2897 static void *
2898 test_migrate_precopy_tcp_multifd_zlib_start(QTestState *from,
2899                                             QTestState *to)
2900 {
2901     /*
2902      * Overloading this test to also check that set_parameter does not error.
2903      * This is also done in the tests for the other compression methods.
2904      */
2905     migrate_set_parameter_int(from, "multifd-zlib-level", 2);
2906     migrate_set_parameter_int(to, "multifd-zlib-level", 2);
2907 
2908     return test_migrate_precopy_tcp_multifd_start_common(from, to, "zlib");
2909 }
2910 
2911 #ifdef CONFIG_ZSTD
2912 static void *
2913 test_migrate_precopy_tcp_multifd_zstd_start(QTestState *from,
2914                                             QTestState *to)
2915 {
2916     migrate_set_parameter_int(from, "multifd-zstd-level", 2);
2917     migrate_set_parameter_int(to, "multifd-zstd-level", 2);
2918 
2919     return test_migrate_precopy_tcp_multifd_start_common(from, to, "zstd");
2920 }
2921 #endif /* CONFIG_ZSTD */
2922 
2923 #ifdef CONFIG_QPL
2924 static void *
2925 test_migrate_precopy_tcp_multifd_qpl_start(QTestState *from,
2926                                             QTestState *to)
2927 {
2928     return test_migrate_precopy_tcp_multifd_start_common(from, to, "qpl");
2929 }
2930 #endif /* CONFIG_QPL */
2931 #ifdef CONFIG_UADK
2932 static void *
2933 test_migrate_precopy_tcp_multifd_uadk_start(QTestState *from,
2934                                             QTestState *to)
2935 {
2936     return test_migrate_precopy_tcp_multifd_start_common(from, to, "uadk");
2937 }
2938 #endif /* CONFIG_UADK */
2939 
2940 static void test_multifd_tcp_uri_none(void)
2941 {
2942     MigrateCommon args = {
2943         .listen_uri = "defer",
2944         .start_hook = test_migrate_precopy_tcp_multifd_start,
2945         /*
2946          * Multifd is more complicated than most of the features, it
2947          * directly takes guest page buffers when sending, make sure
2948          * everything will work alright even if guest page is changing.
2949          */
2950         .live = true,
2951     };
2952     test_precopy_common(&args);
2953 }
2954 
2955 static void test_multifd_tcp_zero_page_legacy(void)
2956 {
2957     MigrateCommon args = {
2958         .listen_uri = "defer",
2959         .start_hook = test_migrate_precopy_tcp_multifd_start_zero_page_legacy,
2960         /*
2961          * Multifd is more complicated than most of the features, it
2962          * directly takes guest page buffers when sending, make sure
2963          * everything will work alright even if guest page is changing.
2964          */
2965         .live = true,
2966     };
2967     test_precopy_common(&args);
2968 }
2969 
2970 static void test_multifd_tcp_no_zero_page(void)
2971 {
2972     MigrateCommon args = {
2973         .listen_uri = "defer",
2974         .start_hook = test_migration_precopy_tcp_multifd_start_no_zero_page,
2975         /*
2976          * Multifd is more complicated than most of the features, it
2977          * directly takes guest page buffers when sending, make sure
2978          * everything will work alright even if guest page is changing.
2979          */
2980         .live = true,
2981     };
2982     test_precopy_common(&args);
2983 }
2984 
2985 static void test_multifd_tcp_channels_none(void)
2986 {
2987     MigrateCommon args = {
2988         .listen_uri = "defer",
2989         .start_hook = test_migrate_precopy_tcp_multifd_start,
2990         .live = true,
2991         .connect_channels = "[ { 'channel-type': 'main',"
2992                             "    'addr': { 'transport': 'socket',"
2993                             "              'type': 'inet',"
2994                             "              'host': '127.0.0.1',"
2995                             "              'port': '0' } } ]",
2996     };
2997     test_precopy_common(&args);
2998 }
2999 
3000 static void test_multifd_tcp_zlib(void)
3001 {
3002     MigrateCommon args = {
3003         .listen_uri = "defer",
3004         .start_hook = test_migrate_precopy_tcp_multifd_zlib_start,
3005     };
3006     test_precopy_common(&args);
3007 }
3008 
3009 #ifdef CONFIG_ZSTD
3010 static void test_multifd_tcp_zstd(void)
3011 {
3012     MigrateCommon args = {
3013         .listen_uri = "defer",
3014         .start_hook = test_migrate_precopy_tcp_multifd_zstd_start,
3015     };
3016     test_precopy_common(&args);
3017 }
3018 #endif
3019 
3020 #ifdef CONFIG_QPL
3021 static void test_multifd_tcp_qpl(void)
3022 {
3023     MigrateCommon args = {
3024         .listen_uri = "defer",
3025         .start_hook = test_migrate_precopy_tcp_multifd_qpl_start,
3026     };
3027     test_precopy_common(&args);
3028 }
3029 #endif
3030 
3031 #ifdef CONFIG_UADK
3032 static void test_multifd_tcp_uadk(void)
3033 {
3034     MigrateCommon args = {
3035         .listen_uri = "defer",
3036         .start_hook = test_migrate_precopy_tcp_multifd_uadk_start,
3037     };
3038     test_precopy_common(&args);
3039 }
3040 #endif
3041 
3042 #ifdef CONFIG_GNUTLS
3043 static void *
3044 test_migrate_multifd_tcp_tls_psk_start_match(QTestState *from,
3045                                              QTestState *to)
3046 {
3047     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
3048     return test_migrate_tls_psk_start_match(from, to);
3049 }
3050 
3051 static void *
3052 test_migrate_multifd_tcp_tls_psk_start_mismatch(QTestState *from,
3053                                                 QTestState *to)
3054 {
3055     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
3056     return test_migrate_tls_psk_start_mismatch(from, to);
3057 }
3058 
3059 #ifdef CONFIG_TASN1
3060 static void *
3061 test_migrate_multifd_tls_x509_start_default_host(QTestState *from,
3062                                                  QTestState *to)
3063 {
3064     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
3065     return test_migrate_tls_x509_start_default_host(from, to);
3066 }
3067 
3068 static void *
3069 test_migrate_multifd_tls_x509_start_override_host(QTestState *from,
3070                                                   QTestState *to)
3071 {
3072     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
3073     return test_migrate_tls_x509_start_override_host(from, to);
3074 }
3075 
3076 static void *
3077 test_migrate_multifd_tls_x509_start_mismatch_host(QTestState *from,
3078                                                   QTestState *to)
3079 {
3080     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
3081     return test_migrate_tls_x509_start_mismatch_host(from, to);
3082 }
3083 
3084 static void *
3085 test_migrate_multifd_tls_x509_start_allow_anon_client(QTestState *from,
3086                                                       QTestState *to)
3087 {
3088     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
3089     return test_migrate_tls_x509_start_allow_anon_client(from, to);
3090 }
3091 
3092 static void *
3093 test_migrate_multifd_tls_x509_start_reject_anon_client(QTestState *from,
3094                                                        QTestState *to)
3095 {
3096     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
3097     return test_migrate_tls_x509_start_reject_anon_client(from, to);
3098 }
3099 #endif /* CONFIG_TASN1 */
3100 
3101 static void test_multifd_tcp_tls_psk_match(void)
3102 {
3103     MigrateCommon args = {
3104         .listen_uri = "defer",
3105         .start_hook = test_migrate_multifd_tcp_tls_psk_start_match,
3106         .finish_hook = test_migrate_tls_psk_finish,
3107     };
3108     test_precopy_common(&args);
3109 }
3110 
3111 static void test_multifd_tcp_tls_psk_mismatch(void)
3112 {
3113     MigrateCommon args = {
3114         .start = {
3115             .hide_stderr = true,
3116         },
3117         .listen_uri = "defer",
3118         .start_hook = test_migrate_multifd_tcp_tls_psk_start_mismatch,
3119         .finish_hook = test_migrate_tls_psk_finish,
3120         .result = MIG_TEST_FAIL,
3121     };
3122     test_precopy_common(&args);
3123 }
3124 
3125 #ifdef CONFIG_TASN1
3126 static void test_multifd_tcp_tls_x509_default_host(void)
3127 {
3128     MigrateCommon args = {
3129         .listen_uri = "defer",
3130         .start_hook = test_migrate_multifd_tls_x509_start_default_host,
3131         .finish_hook = test_migrate_tls_x509_finish,
3132     };
3133     test_precopy_common(&args);
3134 }
3135 
3136 static void test_multifd_tcp_tls_x509_override_host(void)
3137 {
3138     MigrateCommon args = {
3139         .listen_uri = "defer",
3140         .start_hook = test_migrate_multifd_tls_x509_start_override_host,
3141         .finish_hook = test_migrate_tls_x509_finish,
3142     };
3143     test_precopy_common(&args);
3144 }
3145 
3146 static void test_multifd_tcp_tls_x509_mismatch_host(void)
3147 {
3148     /*
3149      * This has different behaviour to the non-multifd case.
3150      *
3151      * In non-multifd case when client aborts due to mismatched
3152      * cert host, the server has already started trying to load
3153      * migration state, and so it exits with I/O failure.
3154      *
3155      * In multifd case when client aborts due to mismatched
3156      * cert host, the server is still waiting for the other
3157      * multifd connections to arrive so hasn't started trying
3158      * to load migration state, and thus just aborts the migration
3159      * without exiting.
3160      */
3161     MigrateCommon args = {
3162         .start = {
3163             .hide_stderr = true,
3164         },
3165         .listen_uri = "defer",
3166         .start_hook = test_migrate_multifd_tls_x509_start_mismatch_host,
3167         .finish_hook = test_migrate_tls_x509_finish,
3168         .result = MIG_TEST_FAIL,
3169     };
3170     test_precopy_common(&args);
3171 }
3172 
3173 static void test_multifd_tcp_tls_x509_allow_anon_client(void)
3174 {
3175     MigrateCommon args = {
3176         .listen_uri = "defer",
3177         .start_hook = test_migrate_multifd_tls_x509_start_allow_anon_client,
3178         .finish_hook = test_migrate_tls_x509_finish,
3179     };
3180     test_precopy_common(&args);
3181 }
3182 
3183 static void test_multifd_tcp_tls_x509_reject_anon_client(void)
3184 {
3185     MigrateCommon args = {
3186         .start = {
3187             .hide_stderr = true,
3188         },
3189         .listen_uri = "defer",
3190         .start_hook = test_migrate_multifd_tls_x509_start_reject_anon_client,
3191         .finish_hook = test_migrate_tls_x509_finish,
3192         .result = MIG_TEST_FAIL,
3193     };
3194     test_precopy_common(&args);
3195 }
3196 #endif /* CONFIG_TASN1 */
3197 #endif /* CONFIG_GNUTLS */
3198 
3199 /*
3200  * This test does:
3201  *  source               target
3202  *                       migrate_incoming
3203  *     migrate
3204  *     migrate_cancel
3205  *                       launch another target
3206  *     migrate
3207  *
3208  *  And see that it works
3209  */
3210 static void test_multifd_tcp_cancel(void)
3211 {
3212     MigrateStart args = {
3213         .hide_stderr = true,
3214     };
3215     QTestState *from, *to, *to2;
3216 
3217     if (test_migrate_start(&from, &to, "defer", &args)) {
3218         return;
3219     }
3220 
3221     migrate_ensure_non_converge(from);
3222     migrate_prepare_for_dirty_mem(from);
3223 
3224     migrate_set_parameter_int(from, "multifd-channels", 16);
3225     migrate_set_parameter_int(to, "multifd-channels", 16);
3226 
3227     migrate_set_capability(from, "multifd", true);
3228     migrate_set_capability(to, "multifd", true);
3229 
3230     /* Start incoming migration from the 1st socket */
3231     migrate_incoming_qmp(to, "tcp:127.0.0.1:0", "{}");
3232 
3233     /* Wait for the first serial output from the source */
3234     wait_for_serial("src_serial");
3235 
3236     migrate_qmp(from, to, NULL, NULL, "{}");
3237 
3238     migrate_wait_for_dirty_mem(from, to);
3239 
3240     migrate_cancel(from);
3241 
3242     /* Make sure QEMU process "to" exited */
3243     qtest_set_expected_status(to, EXIT_FAILURE);
3244     qtest_wait_qemu(to);
3245     qtest_quit(to);
3246 
3247     args = (MigrateStart){
3248         .only_target = true,
3249     };
3250 
3251     if (test_migrate_start(&from, &to2, "defer", &args)) {
3252         return;
3253     }
3254 
3255     migrate_set_parameter_int(to2, "multifd-channels", 16);
3256 
3257     migrate_set_capability(to2, "multifd", true);
3258 
3259     /* Start incoming migration from the 1st socket */
3260     migrate_incoming_qmp(to2, "tcp:127.0.0.1:0", "{}");
3261 
3262     wait_for_migration_status(from, "cancelled", NULL);
3263 
3264     migrate_ensure_non_converge(from);
3265 
3266     migrate_qmp(from, to2, NULL, NULL, "{}");
3267 
3268     migrate_wait_for_dirty_mem(from, to2);
3269 
3270     migrate_ensure_converge(from);
3271 
3272     wait_for_stop(from, &src_state);
3273     qtest_qmp_eventwait(to2, "RESUME");
3274 
3275     wait_for_serial("dest_serial");
3276     wait_for_migration_complete(from);
3277     test_migrate_end(from, to2, true);
3278 }
3279 
3280 static void calc_dirty_rate(QTestState *who, uint64_t calc_time)
3281 {
3282     qtest_qmp_assert_success(who,
3283                              "{ 'execute': 'calc-dirty-rate',"
3284                              "'arguments': { "
3285                              "'calc-time': %" PRIu64 ","
3286                              "'mode': 'dirty-ring' }}",
3287                              calc_time);
3288 }
3289 
3290 static QDict *query_dirty_rate(QTestState *who)
3291 {
3292     return qtest_qmp_assert_success_ref(who,
3293                                         "{ 'execute': 'query-dirty-rate' }");
3294 }
3295 
3296 static void dirtylimit_set_all(QTestState *who, uint64_t dirtyrate)
3297 {
3298     qtest_qmp_assert_success(who,
3299                              "{ 'execute': 'set-vcpu-dirty-limit',"
3300                              "'arguments': { "
3301                              "'dirty-rate': %" PRIu64 " } }",
3302                              dirtyrate);
3303 }
3304 
3305 static void cancel_vcpu_dirty_limit(QTestState *who)
3306 {
3307     qtest_qmp_assert_success(who,
3308                              "{ 'execute': 'cancel-vcpu-dirty-limit' }");
3309 }
3310 
3311 static QDict *query_vcpu_dirty_limit(QTestState *who)
3312 {
3313     QDict *rsp;
3314 
3315     rsp = qtest_qmp(who, "{ 'execute': 'query-vcpu-dirty-limit' }");
3316     g_assert(!qdict_haskey(rsp, "error"));
3317     g_assert(qdict_haskey(rsp, "return"));
3318 
3319     return rsp;
3320 }
3321 
3322 static bool calc_dirtyrate_ready(QTestState *who)
3323 {
3324     QDict *rsp_return;
3325     const char *status;
3326     bool ready;
3327 
3328     rsp_return = query_dirty_rate(who);
3329     g_assert(rsp_return);
3330 
3331     status = qdict_get_str(rsp_return, "status");
3332     g_assert(status);
3333     ready = g_strcmp0(status, "measuring");
3334     qobject_unref(rsp_return);
3335 
3336     return ready;
3337 }
3338 
3339 static void wait_for_calc_dirtyrate_complete(QTestState *who,
3340                                              int64_t time_s)
3341 {
3342     int max_try_count = 10000;
3343     usleep(time_s * 1000000);
3344 
3345     while (!calc_dirtyrate_ready(who) && max_try_count--) {
3346         usleep(1000);
3347     }
3348 
3349     /*
3350      * Set the timeout with 10 s(max_try_count * 1000us),
3351      * if dirtyrate measurement not complete, fail test.
3352      */
3353     g_assert_cmpint(max_try_count, !=, 0);
3354 }
3355 
3356 static int64_t get_dirty_rate(QTestState *who)
3357 {
3358     QDict *rsp_return;
3359     const char *status;
3360     QList *rates;
3361     const QListEntry *entry;
3362     QDict *rate;
3363     int64_t dirtyrate;
3364 
3365     rsp_return = query_dirty_rate(who);
3366     g_assert(rsp_return);
3367 
3368     status = qdict_get_str(rsp_return, "status");
3369     g_assert(status);
3370     g_assert_cmpstr(status, ==, "measured");
3371 
3372     rates = qdict_get_qlist(rsp_return, "vcpu-dirty-rate");
3373     g_assert(rates && !qlist_empty(rates));
3374 
3375     entry = qlist_first(rates);
3376     g_assert(entry);
3377 
3378     rate = qobject_to(QDict, qlist_entry_obj(entry));
3379     g_assert(rate);
3380 
3381     dirtyrate = qdict_get_try_int(rate, "dirty-rate", -1);
3382 
3383     qobject_unref(rsp_return);
3384     return dirtyrate;
3385 }
3386 
3387 static int64_t get_limit_rate(QTestState *who)
3388 {
3389     QDict *rsp_return;
3390     QList *rates;
3391     const QListEntry *entry;
3392     QDict *rate;
3393     int64_t dirtyrate;
3394 
3395     rsp_return = query_vcpu_dirty_limit(who);
3396     g_assert(rsp_return);
3397 
3398     rates = qdict_get_qlist(rsp_return, "return");
3399     g_assert(rates && !qlist_empty(rates));
3400 
3401     entry = qlist_first(rates);
3402     g_assert(entry);
3403 
3404     rate = qobject_to(QDict, qlist_entry_obj(entry));
3405     g_assert(rate);
3406 
3407     dirtyrate = qdict_get_try_int(rate, "limit-rate", -1);
3408 
3409     qobject_unref(rsp_return);
3410     return dirtyrate;
3411 }
3412 
3413 static QTestState *dirtylimit_start_vm(void)
3414 {
3415     QTestState *vm = NULL;
3416     g_autofree gchar *cmd = NULL;
3417 
3418     bootfile_create(tmpfs, false);
3419     cmd = g_strdup_printf("-accel kvm,dirty-ring-size=4096 "
3420                           "-name dirtylimit-test,debug-threads=on "
3421                           "-m 150M -smp 1 "
3422                           "-serial file:%s/vm_serial "
3423                           "-drive file=%s,format=raw ",
3424                           tmpfs, bootpath);
3425 
3426     vm = qtest_init(cmd);
3427     return vm;
3428 }
3429 
3430 static void dirtylimit_stop_vm(QTestState *vm)
3431 {
3432     qtest_quit(vm);
3433     cleanup("vm_serial");
3434 }
3435 
3436 static void test_vcpu_dirty_limit(void)
3437 {
3438     QTestState *vm;
3439     int64_t origin_rate;
3440     int64_t quota_rate;
3441     int64_t rate ;
3442     int max_try_count = 20;
3443     int hit = 0;
3444 
3445     /* Start vm for vcpu dirtylimit test */
3446     vm = dirtylimit_start_vm();
3447 
3448     /* Wait for the first serial output from the vm*/
3449     wait_for_serial("vm_serial");
3450 
3451     /* Do dirtyrate measurement with calc time equals 1s */
3452     calc_dirty_rate(vm, 1);
3453 
3454     /* Sleep calc time and wait for calc dirtyrate complete */
3455     wait_for_calc_dirtyrate_complete(vm, 1);
3456 
3457     /* Query original dirty page rate */
3458     origin_rate = get_dirty_rate(vm);
3459 
3460     /* VM booted from bootsect should dirty memory steadily */
3461     assert(origin_rate != 0);
3462 
3463     /* Setup quota dirty page rate at half of origin */
3464     quota_rate = origin_rate / 2;
3465 
3466     /* Set dirtylimit */
3467     dirtylimit_set_all(vm, quota_rate);
3468 
3469     /*
3470      * Check if set-vcpu-dirty-limit and query-vcpu-dirty-limit
3471      * works literally
3472      */
3473     g_assert_cmpint(quota_rate, ==, get_limit_rate(vm));
3474 
3475     /* Sleep a bit to check if it take effect */
3476     usleep(2000000);
3477 
3478     /*
3479      * Check if dirtylimit take effect realistically, set the
3480      * timeout with 20 s(max_try_count * 1s), if dirtylimit
3481      * doesn't take effect, fail test.
3482      */
3483     while (--max_try_count) {
3484         calc_dirty_rate(vm, 1);
3485         wait_for_calc_dirtyrate_complete(vm, 1);
3486         rate = get_dirty_rate(vm);
3487 
3488         /*
3489          * Assume hitting if current rate is less
3490          * than quota rate (within accepting error)
3491          */
3492         if (rate < (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) {
3493             hit = 1;
3494             break;
3495         }
3496     }
3497 
3498     g_assert_cmpint(hit, ==, 1);
3499 
3500     hit = 0;
3501     max_try_count = 20;
3502 
3503     /* Check if dirtylimit cancellation take effect */
3504     cancel_vcpu_dirty_limit(vm);
3505     while (--max_try_count) {
3506         calc_dirty_rate(vm, 1);
3507         wait_for_calc_dirtyrate_complete(vm, 1);
3508         rate = get_dirty_rate(vm);
3509 
3510         /*
3511          * Assume dirtylimit be canceled if current rate is
3512          * greater than quota rate (within accepting error)
3513          */
3514         if (rate > (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) {
3515             hit = 1;
3516             break;
3517         }
3518     }
3519 
3520     g_assert_cmpint(hit, ==, 1);
3521     dirtylimit_stop_vm(vm);
3522 }
3523 
3524 static void migrate_dirty_limit_wait_showup(QTestState *from,
3525                                             const int64_t period,
3526                                             const int64_t value)
3527 {
3528     /* Enable dirty limit capability */
3529     migrate_set_capability(from, "dirty-limit", true);
3530 
3531     /* Set dirty limit parameters */
3532     migrate_set_parameter_int(from, "x-vcpu-dirty-limit-period", period);
3533     migrate_set_parameter_int(from, "vcpu-dirty-limit", value);
3534 
3535     /* Make sure migrate can't converge */
3536     migrate_ensure_non_converge(from);
3537 
3538     /* To check limit rate after precopy */
3539     migrate_set_capability(from, "pause-before-switchover", true);
3540 
3541     /* Wait for the serial output from the source */
3542     wait_for_serial("src_serial");
3543 }
3544 
3545 /*
3546  * This test does:
3547  *  source                          destination
3548  *  start vm
3549  *                                  start incoming vm
3550  *  migrate
3551  *  wait dirty limit to begin
3552  *  cancel migrate
3553  *  cancellation check
3554  *                                  restart incoming vm
3555  *  migrate
3556  *  wait dirty limit to begin
3557  *  wait pre-switchover event
3558  *  convergence condition check
3559  *
3560  * And see if dirty limit migration works correctly.
3561  * This test case involves many passes, so it runs in slow mode only.
3562  */
3563 static void test_migrate_dirty_limit(void)
3564 {
3565     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
3566     QTestState *from, *to;
3567     int64_t remaining;
3568     uint64_t throttle_us_per_full;
3569     /*
3570      * We want the test to be stable and as fast as possible.
3571      * E.g., with 1Gb/s bandwidth migration may pass without dirty limit,
3572      * so we need to decrease a bandwidth.
3573      */
3574     const int64_t dirtylimit_period = 1000, dirtylimit_value = 50;
3575     const int64_t max_bandwidth = 400000000; /* ~400Mb/s */
3576     const int64_t downtime_limit = 250; /* 250ms */
3577     /*
3578      * We migrate through unix-socket (> 500Mb/s).
3579      * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
3580      * So, we can predict expected_threshold
3581      */
3582     const int64_t expected_threshold = max_bandwidth * downtime_limit / 1000;
3583     int max_try_count = 10;
3584     MigrateCommon args = {
3585         .start = {
3586             .hide_stderr = true,
3587             .use_dirty_ring = true,
3588         },
3589         .listen_uri = uri,
3590         .connect_uri = uri,
3591     };
3592 
3593     /* Start src, dst vm */
3594     if (test_migrate_start(&from, &to, args.listen_uri, &args.start)) {
3595         return;
3596     }
3597 
3598     /* Prepare for dirty limit migration and wait src vm show up */
3599     migrate_dirty_limit_wait_showup(from, dirtylimit_period, dirtylimit_value);
3600 
3601     /* Start migrate */
3602     migrate_qmp(from, to, args.connect_uri, NULL, "{}");
3603 
3604     /* Wait for dirty limit throttle begin */
3605     throttle_us_per_full = 0;
3606     while (throttle_us_per_full == 0) {
3607         throttle_us_per_full =
3608         read_migrate_property_int(from, "dirty-limit-throttle-time-per-round");
3609         usleep(100);
3610         g_assert_false(src_state.stop_seen);
3611     }
3612 
3613     /* Now cancel migrate and wait for dirty limit throttle switch off */
3614     migrate_cancel(from);
3615     wait_for_migration_status(from, "cancelled", NULL);
3616 
3617     /* Check if dirty limit throttle switched off, set timeout 1ms */
3618     do {
3619         throttle_us_per_full =
3620         read_migrate_property_int(from, "dirty-limit-throttle-time-per-round");
3621         usleep(100);
3622         g_assert_false(src_state.stop_seen);
3623     } while (throttle_us_per_full != 0 && --max_try_count);
3624 
3625     /* Assert dirty limit is not in service */
3626     g_assert_cmpint(throttle_us_per_full, ==, 0);
3627 
3628     args = (MigrateCommon) {
3629         .start = {
3630             .only_target = true,
3631             .use_dirty_ring = true,
3632         },
3633         .listen_uri = uri,
3634         .connect_uri = uri,
3635     };
3636 
3637     /* Restart dst vm, src vm already show up so we needn't wait anymore */
3638     if (test_migrate_start(&from, &to, args.listen_uri, &args.start)) {
3639         return;
3640     }
3641 
3642     /* Start migrate */
3643     migrate_qmp(from, to, args.connect_uri, NULL, "{}");
3644 
3645     /* Wait for dirty limit throttle begin */
3646     throttle_us_per_full = 0;
3647     while (throttle_us_per_full == 0) {
3648         throttle_us_per_full =
3649         read_migrate_property_int(from, "dirty-limit-throttle-time-per-round");
3650         usleep(100);
3651         g_assert_false(src_state.stop_seen);
3652     }
3653 
3654     /*
3655      * The dirty limit rate should equals the return value of
3656      * query-vcpu-dirty-limit if dirty limit cap set
3657      */
3658     g_assert_cmpint(dirtylimit_value, ==, get_limit_rate(from));
3659 
3660     /* Now, we have tested if dirty limit works, let it converge */
3661     migrate_set_parameter_int(from, "downtime-limit", downtime_limit);
3662     migrate_set_parameter_int(from, "max-bandwidth", max_bandwidth);
3663 
3664     /*
3665      * Wait for pre-switchover status to check if migration
3666      * satisfy the convergence condition
3667      */
3668     wait_for_migration_status(from, "pre-switchover", NULL);
3669 
3670     remaining = read_ram_property_int(from, "remaining");
3671     g_assert_cmpint(remaining, <,
3672                     (expected_threshold + expected_threshold / 100));
3673 
3674     migrate_continue(from, "pre-switchover");
3675 
3676     qtest_qmp_eventwait(to, "RESUME");
3677 
3678     wait_for_serial("dest_serial");
3679     wait_for_migration_complete(from);
3680 
3681     test_migrate_end(from, to, true);
3682 }
3683 
3684 static bool kvm_dirty_ring_supported(void)
3685 {
3686 #if defined(__linux__) && defined(HOST_X86_64)
3687     int ret, kvm_fd = open("/dev/kvm", O_RDONLY);
3688 
3689     if (kvm_fd < 0) {
3690         return false;
3691     }
3692 
3693     ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, KVM_CAP_DIRTY_LOG_RING);
3694     close(kvm_fd);
3695 
3696     /* We test with 4096 slots */
3697     if (ret < 4096) {
3698         return false;
3699     }
3700 
3701     return true;
3702 #else
3703     return false;
3704 #endif
3705 }
3706 
3707 int main(int argc, char **argv)
3708 {
3709     bool has_kvm, has_tcg;
3710     bool has_uffd, is_x86;
3711     const char *arch;
3712     g_autoptr(GError) err = NULL;
3713     const char *qemu_src = getenv(QEMU_ENV_SRC);
3714     const char *qemu_dst = getenv(QEMU_ENV_DST);
3715     int ret;
3716 
3717     g_test_init(&argc, &argv, NULL);
3718 
3719     /*
3720      * The default QTEST_QEMU_BINARY must always be provided because
3721      * that is what helpers use to query the accel type and
3722      * architecture.
3723      */
3724     if (qemu_src && qemu_dst) {
3725         g_test_message("Only one of %s, %s is allowed",
3726                        QEMU_ENV_SRC, QEMU_ENV_DST);
3727         exit(1);
3728     }
3729 
3730     has_kvm = qtest_has_accel("kvm");
3731     has_tcg = qtest_has_accel("tcg");
3732 
3733     if (!has_tcg && !has_kvm) {
3734         g_test_skip("No KVM or TCG accelerator available");
3735         return 0;
3736     }
3737 
3738     has_uffd = ufd_version_check();
3739     arch = qtest_get_arch();
3740     is_x86 = !strcmp(arch, "i386") || !strcmp(arch, "x86_64");
3741 
3742     tmpfs = g_dir_make_tmp("migration-test-XXXXXX", &err);
3743     if (!tmpfs) {
3744         g_test_message("Can't create temporary directory in %s: %s",
3745                        g_get_tmp_dir(), err->message);
3746     }
3747     g_assert(tmpfs);
3748 
3749     module_call_init(MODULE_INIT_QOM);
3750 
3751     migration_test_add("/migration/bad_dest", test_baddest);
3752 #ifndef _WIN32
3753     migration_test_add("/migration/analyze-script", test_analyze_script);
3754 #endif
3755 
3756     if (is_x86) {
3757         migration_test_add("/migration/precopy/unix/suspend/live",
3758                            test_precopy_unix_suspend_live);
3759         migration_test_add("/migration/precopy/unix/suspend/notlive",
3760                            test_precopy_unix_suspend_notlive);
3761     }
3762 
3763     if (has_uffd) {
3764         migration_test_add("/migration/postcopy/plain", test_postcopy);
3765         migration_test_add("/migration/postcopy/recovery/plain",
3766                            test_postcopy_recovery);
3767         migration_test_add("/migration/postcopy/preempt/plain",
3768                            test_postcopy_preempt);
3769         migration_test_add("/migration/postcopy/preempt/recovery/plain",
3770                            test_postcopy_preempt_recovery);
3771         migration_test_add("/migration/postcopy/recovery/double-failures/handshake",
3772                            test_postcopy_recovery_fail_handshake);
3773         migration_test_add("/migration/postcopy/recovery/double-failures/reconnect",
3774                            test_postcopy_recovery_fail_reconnect);
3775         if (is_x86) {
3776             migration_test_add("/migration/postcopy/suspend",
3777                                test_postcopy_suspend);
3778         }
3779     }
3780 
3781     migration_test_add("/migration/precopy/unix/plain",
3782                        test_precopy_unix_plain);
3783     migration_test_add("/migration/precopy/unix/xbzrle",
3784                        test_precopy_unix_xbzrle);
3785     migration_test_add("/migration/precopy/file",
3786                        test_precopy_file);
3787     migration_test_add("/migration/precopy/file/offset",
3788                        test_precopy_file_offset);
3789 #ifndef _WIN32
3790     migration_test_add("/migration/precopy/file/offset/fdset",
3791                        test_precopy_file_offset_fdset);
3792 #endif
3793     migration_test_add("/migration/precopy/file/offset/bad",
3794                        test_precopy_file_offset_bad);
3795 
3796     /*
3797      * Our CI system has problems with shared memory.
3798      * Don't run this test until we find a workaround.
3799      */
3800     if (getenv("QEMU_TEST_FLAKY_TESTS")) {
3801         migration_test_add("/migration/mode/reboot", test_mode_reboot);
3802     }
3803 
3804     migration_test_add("/migration/precopy/file/mapped-ram",
3805                        test_precopy_file_mapped_ram);
3806     migration_test_add("/migration/precopy/file/mapped-ram/live",
3807                        test_precopy_file_mapped_ram_live);
3808 
3809     migration_test_add("/migration/multifd/file/mapped-ram",
3810                        test_multifd_file_mapped_ram);
3811     migration_test_add("/migration/multifd/file/mapped-ram/live",
3812                        test_multifd_file_mapped_ram_live);
3813 
3814     migration_test_add("/migration/multifd/file/mapped-ram/dio",
3815                        test_multifd_file_mapped_ram_dio);
3816 
3817 #ifndef _WIN32
3818     migration_test_add("/migration/multifd/file/mapped-ram/fdset",
3819                        test_multifd_file_mapped_ram_fdset);
3820     migration_test_add("/migration/multifd/file/mapped-ram/fdset/dio",
3821                        test_multifd_file_mapped_ram_fdset_dio);
3822 #endif
3823 
3824 #ifdef CONFIG_GNUTLS
3825     migration_test_add("/migration/precopy/unix/tls/psk",
3826                        test_precopy_unix_tls_psk);
3827 
3828     if (has_uffd) {
3829         /*
3830          * NOTE: psk test is enough for postcopy, as other types of TLS
3831          * channels are tested under precopy.  Here what we want to test is the
3832          * general postcopy path that has TLS channel enabled.
3833          */
3834         migration_test_add("/migration/postcopy/tls/psk",
3835                            test_postcopy_tls_psk);
3836         migration_test_add("/migration/postcopy/recovery/tls/psk",
3837                            test_postcopy_recovery_tls_psk);
3838         migration_test_add("/migration/postcopy/preempt/tls/psk",
3839                            test_postcopy_preempt_tls_psk);
3840         migration_test_add("/migration/postcopy/preempt/recovery/tls/psk",
3841                            test_postcopy_preempt_all);
3842     }
3843 #ifdef CONFIG_TASN1
3844     migration_test_add("/migration/precopy/unix/tls/x509/default-host",
3845                        test_precopy_unix_tls_x509_default_host);
3846     migration_test_add("/migration/precopy/unix/tls/x509/override-host",
3847                        test_precopy_unix_tls_x509_override_host);
3848 #endif /* CONFIG_TASN1 */
3849 #endif /* CONFIG_GNUTLS */
3850 
3851     migration_test_add("/migration/precopy/tcp/plain", test_precopy_tcp_plain);
3852 
3853     migration_test_add("/migration/precopy/tcp/plain/switchover-ack",
3854                        test_precopy_tcp_switchover_ack);
3855 
3856 #ifdef CONFIG_GNUTLS
3857     migration_test_add("/migration/precopy/tcp/tls/psk/match",
3858                        test_precopy_tcp_tls_psk_match);
3859     migration_test_add("/migration/precopy/tcp/tls/psk/mismatch",
3860                        test_precopy_tcp_tls_psk_mismatch);
3861 #ifdef CONFIG_TASN1
3862     migration_test_add("/migration/precopy/tcp/tls/x509/default-host",
3863                        test_precopy_tcp_tls_x509_default_host);
3864     migration_test_add("/migration/precopy/tcp/tls/x509/override-host",
3865                        test_precopy_tcp_tls_x509_override_host);
3866     migration_test_add("/migration/precopy/tcp/tls/x509/mismatch-host",
3867                        test_precopy_tcp_tls_x509_mismatch_host);
3868     migration_test_add("/migration/precopy/tcp/tls/x509/friendly-client",
3869                        test_precopy_tcp_tls_x509_friendly_client);
3870     migration_test_add("/migration/precopy/tcp/tls/x509/hostile-client",
3871                        test_precopy_tcp_tls_x509_hostile_client);
3872     migration_test_add("/migration/precopy/tcp/tls/x509/allow-anon-client",
3873                        test_precopy_tcp_tls_x509_allow_anon_client);
3874     migration_test_add("/migration/precopy/tcp/tls/x509/reject-anon-client",
3875                        test_precopy_tcp_tls_x509_reject_anon_client);
3876 #endif /* CONFIG_TASN1 */
3877 #endif /* CONFIG_GNUTLS */
3878 
3879     /* migration_test_add("/migration/ignore_shared", test_ignore_shared); */
3880 #ifndef _WIN32
3881     migration_test_add("/migration/precopy/fd/tcp",
3882                        test_migrate_precopy_fd_socket);
3883     migration_test_add("/migration/precopy/fd/file",
3884                        test_migrate_precopy_fd_file);
3885 #endif
3886     migration_test_add("/migration/validate_uuid", test_validate_uuid);
3887     migration_test_add("/migration/validate_uuid_error",
3888                        test_validate_uuid_error);
3889     migration_test_add("/migration/validate_uuid_src_not_set",
3890                        test_validate_uuid_src_not_set);
3891     migration_test_add("/migration/validate_uuid_dst_not_set",
3892                        test_validate_uuid_dst_not_set);
3893     migration_test_add("/migration/validate_uri/channels/both_set",
3894                        test_validate_uri_channels_both_set);
3895     migration_test_add("/migration/validate_uri/channels/none_set",
3896                        test_validate_uri_channels_none_set);
3897     /*
3898      * See explanation why this test is slow on function definition
3899      */
3900     if (g_test_slow()) {
3901         migration_test_add("/migration/auto_converge",
3902                            test_migrate_auto_converge);
3903         if (g_str_equal(arch, "x86_64") &&
3904             has_kvm && kvm_dirty_ring_supported()) {
3905             migration_test_add("/migration/dirty_limit",
3906                                test_migrate_dirty_limit);
3907         }
3908     }
3909     migration_test_add("/migration/multifd/tcp/uri/plain/none",
3910                        test_multifd_tcp_uri_none);
3911     migration_test_add("/migration/multifd/tcp/channels/plain/none",
3912                        test_multifd_tcp_channels_none);
3913     migration_test_add("/migration/multifd/tcp/plain/zero-page/legacy",
3914                        test_multifd_tcp_zero_page_legacy);
3915     migration_test_add("/migration/multifd/tcp/plain/zero-page/none",
3916                        test_multifd_tcp_no_zero_page);
3917     migration_test_add("/migration/multifd/tcp/plain/cancel",
3918                        test_multifd_tcp_cancel);
3919     migration_test_add("/migration/multifd/tcp/plain/zlib",
3920                        test_multifd_tcp_zlib);
3921 #ifdef CONFIG_ZSTD
3922     migration_test_add("/migration/multifd/tcp/plain/zstd",
3923                        test_multifd_tcp_zstd);
3924 #endif
3925 #ifdef CONFIG_QPL
3926     migration_test_add("/migration/multifd/tcp/plain/qpl",
3927                        test_multifd_tcp_qpl);
3928 #endif
3929 #ifdef CONFIG_UADK
3930     migration_test_add("/migration/multifd/tcp/plain/uadk",
3931                        test_multifd_tcp_uadk);
3932 #endif
3933 #ifdef CONFIG_GNUTLS
3934     migration_test_add("/migration/multifd/tcp/tls/psk/match",
3935                        test_multifd_tcp_tls_psk_match);
3936     migration_test_add("/migration/multifd/tcp/tls/psk/mismatch",
3937                        test_multifd_tcp_tls_psk_mismatch);
3938 #ifdef CONFIG_TASN1
3939     migration_test_add("/migration/multifd/tcp/tls/x509/default-host",
3940                        test_multifd_tcp_tls_x509_default_host);
3941     migration_test_add("/migration/multifd/tcp/tls/x509/override-host",
3942                        test_multifd_tcp_tls_x509_override_host);
3943     migration_test_add("/migration/multifd/tcp/tls/x509/mismatch-host",
3944                        test_multifd_tcp_tls_x509_mismatch_host);
3945     migration_test_add("/migration/multifd/tcp/tls/x509/allow-anon-client",
3946                        test_multifd_tcp_tls_x509_allow_anon_client);
3947     migration_test_add("/migration/multifd/tcp/tls/x509/reject-anon-client",
3948                        test_multifd_tcp_tls_x509_reject_anon_client);
3949 #endif /* CONFIG_TASN1 */
3950 #endif /* CONFIG_GNUTLS */
3951 
3952     if (g_str_equal(arch, "x86_64") && has_kvm && kvm_dirty_ring_supported()) {
3953         migration_test_add("/migration/dirty_ring",
3954                            test_precopy_unix_dirty_ring);
3955         if (qtest_has_machine("pc")) {
3956             migration_test_add("/migration/vcpu_dirty_limit",
3957                                test_vcpu_dirty_limit);
3958         }
3959     }
3960 
3961     ret = g_test_run();
3962 
3963     g_assert_cmpint(ret, ==, 0);
3964 
3965     bootfile_delete();
3966     ret = rmdir(tmpfs);
3967     if (ret != 0) {
3968         g_test_message("unable to rmdir: path (%s): %s",
3969                        tmpfs, strerror(errno));
3970     }
3971     g_free(tmpfs);
3972 
3973     return ret;
3974 }
3975