xref: /qemu/tests/qtest/migration-test.c (revision bb302772)
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/error.h"
17 #include "qapi/qmp/qdict.h"
18 #include "qemu/module.h"
19 #include "qemu/option.h"
20 #include "qemu/range.h"
21 #include "qemu/sockets.h"
22 #include "chardev/char.h"
23 #include "qapi/qapi-visit-sockets.h"
24 #include "qapi/qobject-input-visitor.h"
25 #include "qapi/qobject-output-visitor.h"
26 #include "crypto/tlscredspsk.h"
27 #include "qapi/qmp/qlist.h"
28 
29 #include "migration-helpers.h"
30 #include "tests/migration/migration-test.h"
31 #ifdef CONFIG_GNUTLS
32 # include "tests/unit/crypto-tls-psk-helpers.h"
33 # ifdef CONFIG_TASN1
34 #  include "tests/unit/crypto-tls-x509-helpers.h"
35 # endif /* CONFIG_TASN1 */
36 #endif /* CONFIG_GNUTLS */
37 
38 /* For dirty ring test; so far only x86_64 is supported */
39 #if defined(__linux__) && defined(HOST_X86_64)
40 #include "linux/kvm.h"
41 #endif
42 
43 unsigned start_address;
44 unsigned end_address;
45 static bool uffd_feature_thread_id;
46 static bool got_src_stop;
47 static bool got_dst_resume;
48 
49 /*
50  * An initial 3 MB offset is used as that corresponds
51  * to ~1 sec of data transfer with our bandwidth setting.
52  */
53 #define MAGIC_OFFSET_BASE (3 * 1024 * 1024)
54 /*
55  * A further 1k is added to ensure we're not a multiple
56  * of TEST_MEM_PAGE_SIZE, thus avoid clash with writes
57  * from the migration guest workload.
58  */
59 #define MAGIC_OFFSET_SHUFFLE 1024
60 #define MAGIC_OFFSET (MAGIC_OFFSET_BASE + MAGIC_OFFSET_SHUFFLE)
61 #define MAGIC_MARKER 0xFEED12345678CAFEULL
62 
63 /*
64  * Dirtylimit stop working if dirty page rate error
65  * value less than DIRTYLIMIT_TOLERANCE_RANGE
66  */
67 #define DIRTYLIMIT_TOLERANCE_RANGE  25  /* MB/s */
68 
69 #if defined(__linux__)
70 #include <sys/syscall.h>
71 #include <sys/vfs.h>
72 #endif
73 
74 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
75 #include <sys/eventfd.h>
76 #include <sys/ioctl.h>
77 #include "qemu/userfaultfd.h"
78 
79 static bool ufd_version_check(void)
80 {
81     struct uffdio_api api_struct;
82     uint64_t ioctl_mask;
83 
84     int ufd = uffd_open(O_CLOEXEC);
85 
86     if (ufd == -1) {
87         g_test_message("Skipping test: userfaultfd not available");
88         return false;
89     }
90 
91     api_struct.api = UFFD_API;
92     api_struct.features = 0;
93     if (ioctl(ufd, UFFDIO_API, &api_struct)) {
94         g_test_message("Skipping test: UFFDIO_API failed");
95         return false;
96     }
97     uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
98 
99     ioctl_mask = (__u64)1 << _UFFDIO_REGISTER |
100                  (__u64)1 << _UFFDIO_UNREGISTER;
101     if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
102         g_test_message("Skipping test: Missing userfault feature");
103         return false;
104     }
105 
106     return true;
107 }
108 
109 #else
110 static bool ufd_version_check(void)
111 {
112     g_test_message("Skipping test: Userfault not available (builtdtime)");
113     return false;
114 }
115 
116 #endif
117 
118 static char *tmpfs;
119 static char *bootpath;
120 
121 /* The boot file modifies memory area in [start_address, end_address)
122  * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
123  */
124 #include "tests/migration/i386/a-b-bootblock.h"
125 #include "tests/migration/aarch64/a-b-kernel.h"
126 #include "tests/migration/s390x/a-b-bios.h"
127 
128 static void bootfile_create(char *dir)
129 {
130     const char *arch = qtest_get_arch();
131     unsigned char *content;
132     size_t len;
133 
134     bootpath = g_strdup_printf("%s/bootsect", dir);
135     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
136         /* the assembled x86 boot sector should be exactly one sector large */
137         g_assert(sizeof(x86_bootsect) == 512);
138         content = x86_bootsect;
139         len = sizeof(x86_bootsect);
140     } else if (g_str_equal(arch, "s390x")) {
141         content = s390x_elf;
142         len = sizeof(s390x_elf);
143     } else if (strcmp(arch, "ppc64") == 0) {
144         /*
145          * sane architectures can be programmed at the boot prompt
146          */
147         return;
148     } else if (strcmp(arch, "aarch64") == 0) {
149         content = aarch64_kernel;
150         len = sizeof(aarch64_kernel);
151         g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
152     } else {
153         g_assert_not_reached();
154     }
155 
156     FILE *bootfile = fopen(bootpath, "wb");
157 
158     g_assert_cmpint(fwrite(content, len, 1, bootfile), ==, 1);
159     fclose(bootfile);
160 }
161 
162 static void bootfile_delete(void)
163 {
164     unlink(bootpath);
165     g_free(bootpath);
166     bootpath = NULL;
167 }
168 
169 /*
170  * Wait for some output in the serial output file,
171  * we get an 'A' followed by an endless string of 'B's
172  * but on the destination we won't have the A.
173  */
174 static void wait_for_serial(const char *side)
175 {
176     g_autofree char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
177     FILE *serialfile = fopen(serialpath, "r");
178     const char *arch = qtest_get_arch();
179     int started = (strcmp(side, "src_serial") == 0 &&
180                    strcmp(arch, "ppc64") == 0) ? 0 : 1;
181 
182     do {
183         int readvalue = fgetc(serialfile);
184 
185         if (!started) {
186             /* SLOF prints its banner before starting test,
187              * to ignore it, mark the start of the test with '_',
188              * ignore all characters until this marker
189              */
190             switch (readvalue) {
191             case '_':
192                 started = 1;
193                 break;
194             case EOF:
195                 fseek(serialfile, 0, SEEK_SET);
196                 usleep(1000);
197                 break;
198             }
199             continue;
200         }
201         switch (readvalue) {
202         case 'A':
203             /* Fine */
204             break;
205 
206         case 'B':
207             /* It's alive! */
208             fclose(serialfile);
209             return;
210 
211         case EOF:
212             started = (strcmp(side, "src_serial") == 0 &&
213                        strcmp(arch, "ppc64") == 0) ? 0 : 1;
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 /*
226  * It's tricky to use qemu's migration event capability with qtest,
227  * events suddenly appearing confuse the qmp()/hmp() responses.
228  */
229 
230 static int64_t read_ram_property_int(QTestState *who, const char *property)
231 {
232     QDict *rsp_return, *rsp_ram;
233     int64_t result;
234 
235     rsp_return = migrate_query_not_failed(who);
236     if (!qdict_haskey(rsp_return, "ram")) {
237         /* Still in setup */
238         result = 0;
239     } else {
240         rsp_ram = qdict_get_qdict(rsp_return, "ram");
241         result = qdict_get_try_int(rsp_ram, property, 0);
242     }
243     qobject_unref(rsp_return);
244     return result;
245 }
246 
247 static int64_t read_migrate_property_int(QTestState *who, const char *property)
248 {
249     QDict *rsp_return;
250     int64_t result;
251 
252     rsp_return = migrate_query_not_failed(who);
253     result = qdict_get_try_int(rsp_return, property, 0);
254     qobject_unref(rsp_return);
255     return result;
256 }
257 
258 static uint64_t get_migration_pass(QTestState *who)
259 {
260     return read_ram_property_int(who, "dirty-sync-count");
261 }
262 
263 static void read_blocktime(QTestState *who)
264 {
265     QDict *rsp_return;
266 
267     rsp_return = migrate_query_not_failed(who);
268     g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
269     qobject_unref(rsp_return);
270 }
271 
272 static void wait_for_migration_pass(QTestState *who)
273 {
274     uint64_t initial_pass = get_migration_pass(who);
275     uint64_t pass;
276 
277     /* Wait for the 1st sync */
278     while (!got_src_stop && !initial_pass) {
279         usleep(1000);
280         initial_pass = get_migration_pass(who);
281     }
282 
283     do {
284         usleep(1000);
285         pass = get_migration_pass(who);
286     } while (pass == initial_pass && !got_src_stop);
287 }
288 
289 static void check_guests_ram(QTestState *who)
290 {
291     /* Our ASM test will have been incrementing one byte from each page from
292      * start_address to < end_address in order. This gives us a constraint
293      * that any page's byte should be equal or less than the previous pages
294      * byte (mod 256); and they should all be equal except for one transition
295      * at the point where we meet the incrementer. (We're running this with
296      * the guest stopped).
297      */
298     unsigned address;
299     uint8_t first_byte;
300     uint8_t last_byte;
301     bool hit_edge = false;
302     int bad = 0;
303 
304     qtest_memread(who, start_address, &first_byte, 1);
305     last_byte = first_byte;
306 
307     for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
308          address += TEST_MEM_PAGE_SIZE)
309     {
310         uint8_t b;
311         qtest_memread(who, address, &b, 1);
312         if (b != last_byte) {
313             if (((b + 1) % 256) == last_byte && !hit_edge) {
314                 /* This is OK, the guest stopped at the point of
315                  * incrementing the previous page but didn't get
316                  * to us yet.
317                  */
318                 hit_edge = true;
319                 last_byte = b;
320             } else {
321                 bad++;
322                 if (bad <= 10) {
323                     fprintf(stderr, "Memory content inconsistency at %x"
324                             " first_byte = %x last_byte = %x current = %x"
325                             " hit_edge = %x\n",
326                             address, first_byte, last_byte, b, hit_edge);
327                 }
328             }
329         }
330     }
331     if (bad >= 10) {
332         fprintf(stderr, "and in another %d pages", bad - 10);
333     }
334     g_assert(bad == 0);
335 }
336 
337 static void cleanup(const char *filename)
338 {
339     g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, filename);
340 
341     unlink(path);
342 }
343 
344 static char *SocketAddress_to_str(SocketAddress *addr)
345 {
346     switch (addr->type) {
347     case SOCKET_ADDRESS_TYPE_INET:
348         return g_strdup_printf("tcp:%s:%s",
349                                addr->u.inet.host,
350                                addr->u.inet.port);
351     case SOCKET_ADDRESS_TYPE_UNIX:
352         return g_strdup_printf("unix:%s",
353                                addr->u.q_unix.path);
354     case SOCKET_ADDRESS_TYPE_FD:
355         return g_strdup_printf("fd:%s", addr->u.fd.str);
356     case SOCKET_ADDRESS_TYPE_VSOCK:
357         return g_strdup_printf("tcp:%s:%s",
358                                addr->u.vsock.cid,
359                                addr->u.vsock.port);
360     default:
361         return g_strdup("unknown address type");
362     }
363 }
364 
365 static char *migrate_get_socket_address(QTestState *who, const char *parameter)
366 {
367     QDict *rsp;
368     char *result;
369     SocketAddressList *addrs;
370     Visitor *iv = NULL;
371     QObject *object;
372 
373     rsp = migrate_query(who);
374     object = qdict_get(rsp, parameter);
375 
376     iv = qobject_input_visitor_new(object);
377     visit_type_SocketAddressList(iv, NULL, &addrs, &error_abort);
378     visit_free(iv);
379 
380     /* we are only using a single address */
381     result = SocketAddress_to_str(addrs->value);
382 
383     qapi_free_SocketAddressList(addrs);
384     qobject_unref(rsp);
385     return result;
386 }
387 
388 static long long migrate_get_parameter_int(QTestState *who,
389                                            const char *parameter)
390 {
391     QDict *rsp;
392     long long result;
393 
394     rsp = qtest_qmp_assert_success_ref(
395         who, "{ 'execute': 'query-migrate-parameters' }");
396     result = qdict_get_int(rsp, parameter);
397     qobject_unref(rsp);
398     return result;
399 }
400 
401 static void migrate_check_parameter_int(QTestState *who, const char *parameter,
402                                         long long value)
403 {
404     long long result;
405 
406     result = migrate_get_parameter_int(who, parameter);
407     g_assert_cmpint(result, ==, value);
408 }
409 
410 static void migrate_set_parameter_int(QTestState *who, const char *parameter,
411                                       long long value)
412 {
413     qtest_qmp_assert_success(who,
414                              "{ 'execute': 'migrate-set-parameters',"
415                              "'arguments': { %s: %lld } }",
416                              parameter, value);
417     migrate_check_parameter_int(who, parameter, value);
418 }
419 
420 static char *migrate_get_parameter_str(QTestState *who,
421                                        const char *parameter)
422 {
423     QDict *rsp;
424     char *result;
425 
426     rsp = qtest_qmp_assert_success_ref(
427         who, "{ 'execute': 'query-migrate-parameters' }");
428     result = g_strdup(qdict_get_str(rsp, parameter));
429     qobject_unref(rsp);
430     return result;
431 }
432 
433 static void migrate_check_parameter_str(QTestState *who, const char *parameter,
434                                         const char *value)
435 {
436     g_autofree char *result = migrate_get_parameter_str(who, parameter);
437     g_assert_cmpstr(result, ==, value);
438 }
439 
440 static void migrate_set_parameter_str(QTestState *who, const char *parameter,
441                                       const char *value)
442 {
443     qtest_qmp_assert_success(who,
444                              "{ 'execute': 'migrate-set-parameters',"
445                              "'arguments': { %s: %s } }",
446                              parameter, value);
447     migrate_check_parameter_str(who, parameter, value);
448 }
449 
450 static long long migrate_get_parameter_bool(QTestState *who,
451                                            const char *parameter)
452 {
453     QDict *rsp;
454     int result;
455 
456     rsp = qtest_qmp_assert_success_ref(
457         who, "{ 'execute': 'query-migrate-parameters' }");
458     result = qdict_get_bool(rsp, parameter);
459     qobject_unref(rsp);
460     return !!result;
461 }
462 
463 static void migrate_check_parameter_bool(QTestState *who, const char *parameter,
464                                         int value)
465 {
466     int result;
467 
468     result = migrate_get_parameter_bool(who, parameter);
469     g_assert_cmpint(result, ==, value);
470 }
471 
472 static void migrate_set_parameter_bool(QTestState *who, const char *parameter,
473                                       int value)
474 {
475     qtest_qmp_assert_success(who,
476                              "{ 'execute': 'migrate-set-parameters',"
477                              "'arguments': { %s: %i } }",
478                              parameter, value);
479     migrate_check_parameter_bool(who, parameter, value);
480 }
481 
482 static void migrate_ensure_non_converge(QTestState *who)
483 {
484     /* Can't converge with 1ms downtime + 3 mbs bandwidth limit */
485     migrate_set_parameter_int(who, "max-bandwidth", 3 * 1000 * 1000);
486     migrate_set_parameter_int(who, "downtime-limit", 1);
487 }
488 
489 static void migrate_ensure_converge(QTestState *who)
490 {
491     /* Should converge with 30s downtime + 1 gbs bandwidth limit */
492     migrate_set_parameter_int(who, "max-bandwidth", 1 * 1000 * 1000 * 1000);
493     migrate_set_parameter_int(who, "downtime-limit", 30 * 1000);
494 }
495 
496 /*
497  * Our goal is to ensure that we run a single full migration
498  * iteration, and also dirty memory, ensuring that at least
499  * one further iteration is required.
500  *
501  * We can't directly synchronize with the start of a migration
502  * so we have to apply some tricks monitoring memory that is
503  * transferred.
504  *
505  * Initially we set the migration bandwidth to an insanely
506  * low value, with tiny max downtime too. This basically
507  * guarantees migration will never complete.
508  *
509  * This will result in a test that is unacceptably slow though,
510  * so we can't let the entire migration pass run at this speed.
511  * Our intent is to let it run just long enough that we can
512  * prove data prior to the marker has been transferred *AND*
513  * also prove this transferred data is dirty again.
514  *
515  * Before migration starts, we write a 64-bit magic marker
516  * into a fixed location in the src VM RAM.
517  *
518  * Then watch dst memory until the marker appears. This is
519  * proof that start_address -> MAGIC_OFFSET_BASE has been
520  * transferred.
521  *
522  * Finally we go back to the source and read a byte just
523  * before the marker until we see it flip in value. This
524  * is proof that start_address -> MAGIC_OFFSET_BASE
525  * is now dirty again.
526  *
527  * IOW, we're guaranteed at least a 2nd migration pass
528  * at this point.
529  *
530  * We can now let migration run at full speed to finish
531  * the test
532  */
533 static void migrate_prepare_for_dirty_mem(QTestState *from)
534 {
535     /*
536      * The guest workflow iterates from start_address to
537      * end_address, writing 1 byte every TEST_MEM_PAGE_SIZE
538      * bytes.
539      *
540      * IOW, if we write to mem at a point which is NOT
541      * a multiple of TEST_MEM_PAGE_SIZE, our write won't
542      * conflict with the migration workflow.
543      *
544      * We put in a marker here, that we'll use to determine
545      * when the data has been transferred to the dst.
546      */
547     qtest_writeq(from, start_address + MAGIC_OFFSET, MAGIC_MARKER);
548 }
549 
550 static void migrate_wait_for_dirty_mem(QTestState *from,
551                                        QTestState *to)
552 {
553     uint64_t watch_address = start_address + MAGIC_OFFSET_BASE;
554     uint64_t marker_address = start_address + MAGIC_OFFSET;
555     uint8_t watch_byte;
556 
557     /*
558      * Wait for the MAGIC_MARKER to get transferred, as an
559      * indicator that a migration pass has made some known
560      * amount of progress.
561      */
562     do {
563         usleep(1000 * 10);
564     } while (qtest_readq(to, marker_address) != MAGIC_MARKER);
565 
566     /*
567      * Now ensure that already transferred bytes are
568      * dirty again from the guest workload. Note the
569      * guest byte value will wrap around and by chance
570      * match the original watch_byte. This is harmless
571      * as we'll eventually see a different value if we
572      * keep watching
573      */
574     watch_byte = qtest_readb(from, watch_address);
575     do {
576         usleep(1000 * 10);
577     } while (qtest_readb(from, watch_address) == watch_byte);
578 }
579 
580 
581 static void migrate_pause(QTestState *who)
582 {
583     qtest_qmp_assert_success(who, "{ 'execute': 'migrate-pause' }");
584 }
585 
586 static void migrate_continue(QTestState *who, const char *state)
587 {
588     qtest_qmp_assert_success(who,
589                              "{ 'execute': 'migrate-continue',"
590                              "  'arguments': { 'state': %s } }",
591                              state);
592 }
593 
594 static void migrate_recover(QTestState *who, const char *uri)
595 {
596     qtest_qmp_assert_success(who,
597                              "{ 'execute': 'migrate-recover', "
598                              "  'id': 'recover-cmd', "
599                              "  'arguments': { 'uri': %s } }",
600                              uri);
601 }
602 
603 static void migrate_cancel(QTestState *who)
604 {
605     qtest_qmp_assert_success(who, "{ 'execute': 'migrate_cancel' }");
606 }
607 
608 static void migrate_set_capability(QTestState *who, const char *capability,
609                                    bool value)
610 {
611     qtest_qmp_assert_success(who,
612                              "{ 'execute': 'migrate-set-capabilities',"
613                              "'arguments': { "
614                              "'capabilities': [ { "
615                              "'capability': %s, 'state': %i } ] } }",
616                              capability, value);
617 }
618 
619 static void migrate_postcopy_start(QTestState *from, QTestState *to)
620 {
621     qtest_qmp_assert_success(from, "{ 'execute': 'migrate-start-postcopy' }");
622 
623     if (!got_src_stop) {
624         qtest_qmp_eventwait(from, "STOP");
625     }
626 
627     qtest_qmp_eventwait(to, "RESUME");
628 }
629 
630 typedef struct {
631     /*
632      * QTEST_LOG=1 may override this.  When QTEST_LOG=1, we always dump errors
633      * unconditionally, because it means the user would like to be verbose.
634      */
635     bool hide_stderr;
636     bool use_shmem;
637     /* only launch the target process */
638     bool only_target;
639     /* Use dirty ring if true; dirty logging otherwise */
640     bool use_dirty_ring;
641     const char *opts_source;
642     const char *opts_target;
643 } MigrateStart;
644 
645 /*
646  * A hook that runs after the src and dst QEMUs have been
647  * created, but before the migration is started. This can
648  * be used to set migration parameters and capabilities.
649  *
650  * Returns: NULL, or a pointer to opaque state to be
651  *          later passed to the TestMigrateFinishHook
652  */
653 typedef void * (*TestMigrateStartHook)(QTestState *from,
654                                        QTestState *to);
655 
656 /*
657  * A hook that runs after the migration has finished,
658  * regardless of whether it succeeded or failed, but
659  * before QEMU has terminated (unless it self-terminated
660  * due to migration error)
661  *
662  * @opaque is a pointer to state previously returned
663  * by the TestMigrateStartHook if any, or NULL.
664  */
665 typedef void (*TestMigrateFinishHook)(QTestState *from,
666                                       QTestState *to,
667                                       void *opaque);
668 
669 typedef struct {
670     /* Optional: fine tune start parameters */
671     MigrateStart start;
672 
673     /* Required: the URI for the dst QEMU to listen on */
674     const char *listen_uri;
675 
676     /*
677      * Optional: the URI for the src QEMU to connect to
678      * If NULL, then it will query the dst QEMU for its actual
679      * listening address and use that as the connect address.
680      * This allows for dynamically picking a free TCP port.
681      */
682     const char *connect_uri;
683 
684     /* Optional: callback to run at start to set migration parameters */
685     TestMigrateStartHook start_hook;
686     /* Optional: callback to run at finish to cleanup */
687     TestMigrateFinishHook finish_hook;
688 
689     /*
690      * Optional: normally we expect the migration process to complete.
691      *
692      * There can be a variety of reasons and stages in which failure
693      * can happen during tests.
694      *
695      * If a failure is expected to happen at time of establishing
696      * the connection, then MIG_TEST_FAIL will indicate that the dst
697      * QEMU is expected to stay running and accept future migration
698      * connections.
699      *
700      * If a failure is expected to happen while processing the
701      * migration stream, then MIG_TEST_FAIL_DEST_QUIT_ERR will indicate
702      * that the dst QEMU is expected to quit with non-zero exit status
703      */
704     enum {
705         /* This test should succeed, the default */
706         MIG_TEST_SUCCEED = 0,
707         /* This test should fail, dest qemu should keep alive */
708         MIG_TEST_FAIL,
709         /* This test should fail, dest qemu should fail with abnormal status */
710         MIG_TEST_FAIL_DEST_QUIT_ERR,
711     } result;
712 
713     /*
714      * Optional: set number of migration passes to wait for, if live==true.
715      * If zero, then merely wait for a few MB of dirty data
716      */
717     unsigned int iterations;
718 
719     /*
720      * Optional: whether the guest CPUs should be running during a precopy
721      * migration test.  We used to always run with live but it took much
722      * longer so we reduced live tests to only the ones that have solid
723      * reason to be tested live-only.  For each of the new test cases for
724      * precopy please provide justifications to use live explicitly (please
725      * refer to existing ones with live=true), or use live=off by default.
726      */
727     bool live;
728 
729     /* Postcopy specific fields */
730     void *postcopy_data;
731     bool postcopy_preempt;
732 } MigrateCommon;
733 
734 static int test_migrate_start(QTestState **from, QTestState **to,
735                               const char *uri, MigrateStart *args)
736 {
737     g_autofree gchar *arch_source = NULL;
738     g_autofree gchar *arch_target = NULL;
739     /* options for source and target */
740     g_autofree gchar *arch_opts = NULL;
741     g_autofree gchar *cmd_source = NULL;
742     g_autofree gchar *cmd_target = NULL;
743     const gchar *ignore_stderr;
744     g_autofree char *shmem_opts = NULL;
745     g_autofree char *shmem_path = NULL;
746     const char *kvm_opts = NULL;
747     const char *arch = qtest_get_arch();
748     const char *memory_size;
749 
750     if (args->use_shmem) {
751         if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
752             g_test_skip("/dev/shm is not supported");
753             return -1;
754         }
755     }
756 
757     got_src_stop = false;
758     got_dst_resume = false;
759     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
760         memory_size = "150M";
761         arch_opts = g_strdup_printf("-drive file=%s,format=raw", bootpath);
762         start_address = X86_TEST_MEM_START;
763         end_address = X86_TEST_MEM_END;
764     } else if (g_str_equal(arch, "s390x")) {
765         memory_size = "128M";
766         arch_opts = g_strdup_printf("-bios %s", bootpath);
767         start_address = S390_TEST_MEM_START;
768         end_address = S390_TEST_MEM_END;
769     } else if (strcmp(arch, "ppc64") == 0) {
770         memory_size = "256M";
771         start_address = PPC_TEST_MEM_START;
772         end_address = PPC_TEST_MEM_END;
773         arch_source = g_strdup_printf("-prom-env 'use-nvramrc?=true' -prom-env "
774                                       "'nvramrc=hex .\" _\" begin %x %x "
775                                       "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
776                                       "until'", end_address, start_address);
777         arch_opts = g_strdup("-nodefaults -machine vsmt=8");
778     } else if (strcmp(arch, "aarch64") == 0) {
779         memory_size = "150M";
780         arch_opts = g_strdup_printf("-machine virt,gic-version=max -cpu max "
781                                     "-kernel %s", bootpath);
782         start_address = ARM_TEST_MEM_START;
783         end_address = ARM_TEST_MEM_END;
784     } else {
785         g_assert_not_reached();
786     }
787 
788     if (!getenv("QTEST_LOG") && args->hide_stderr) {
789 #ifndef _WIN32
790         ignore_stderr = "2>/dev/null";
791 #else
792         /*
793          * On Windows the QEMU executable is created via CreateProcess() and
794          * IO redirection does not work, so don't bother adding IO redirection
795          * to the command line.
796          */
797         ignore_stderr = "";
798 #endif
799     } else {
800         ignore_stderr = "";
801     }
802 
803     if (args->use_shmem) {
804         shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
805         shmem_opts = g_strdup_printf(
806             "-object memory-backend-file,id=mem0,size=%s"
807             ",mem-path=%s,share=on -numa node,memdev=mem0",
808             memory_size, shmem_path);
809     }
810 
811     if (args->use_dirty_ring) {
812         kvm_opts = ",dirty-ring-size=4096";
813     }
814 
815     cmd_source = g_strdup_printf("-accel kvm%s -accel tcg "
816                                  "-name source,debug-threads=on "
817                                  "-m %s "
818                                  "-serial file:%s/src_serial "
819                                  "%s %s %s %s %s",
820                                  kvm_opts ? kvm_opts : "",
821                                  memory_size, tmpfs,
822                                  arch_opts ? arch_opts : "",
823                                  arch_source ? arch_source : "",
824                                  shmem_opts ? shmem_opts : "",
825                                  args->opts_source ? args->opts_source : "",
826                                  ignore_stderr);
827     if (!args->only_target) {
828         *from = qtest_init(cmd_source);
829         qtest_qmp_set_event_callback(*from,
830                                      migrate_watch_for_stop,
831                                      &got_src_stop);
832     }
833 
834     cmd_target = g_strdup_printf("-accel kvm%s -accel tcg "
835                                  "-name target,debug-threads=on "
836                                  "-m %s "
837                                  "-serial file:%s/dest_serial "
838                                  "-incoming %s "
839                                  "%s %s %s %s %s",
840                                  kvm_opts ? kvm_opts : "",
841                                  memory_size, tmpfs, uri,
842                                  arch_opts ? arch_opts : "",
843                                  arch_target ? arch_target : "",
844                                  shmem_opts ? shmem_opts : "",
845                                  args->opts_target ? args->opts_target : "",
846                                  ignore_stderr);
847     *to = qtest_init(cmd_target);
848     qtest_qmp_set_event_callback(*to,
849                                  migrate_watch_for_resume,
850                                  &got_dst_resume);
851 
852     /*
853      * Remove shmem file immediately to avoid memory leak in test failed case.
854      * It's valid because QEMU has already opened this file
855      */
856     if (args->use_shmem) {
857         unlink(shmem_path);
858     }
859 
860     return 0;
861 }
862 
863 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
864 {
865     unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
866 
867     qtest_quit(from);
868 
869     if (test_dest) {
870         qtest_memread(to, start_address, &dest_byte_a, 1);
871 
872         /* Destination still running, wait for a byte to change */
873         do {
874             qtest_memread(to, start_address, &dest_byte_b, 1);
875             usleep(1000 * 10);
876         } while (dest_byte_a == dest_byte_b);
877 
878         qtest_qmp_assert_success(to, "{ 'execute' : 'stop'}");
879 
880         /* With it stopped, check nothing changes */
881         qtest_memread(to, start_address, &dest_byte_c, 1);
882         usleep(1000 * 200);
883         qtest_memread(to, start_address, &dest_byte_d, 1);
884         g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
885 
886         check_guests_ram(to);
887     }
888 
889     qtest_quit(to);
890 
891     cleanup("migsocket");
892     cleanup("src_serial");
893     cleanup("dest_serial");
894 }
895 
896 #ifdef CONFIG_GNUTLS
897 struct TestMigrateTLSPSKData {
898     char *workdir;
899     char *workdiralt;
900     char *pskfile;
901     char *pskfilealt;
902 };
903 
904 static void *
905 test_migrate_tls_psk_start_common(QTestState *from,
906                                   QTestState *to,
907                                   bool mismatch)
908 {
909     struct TestMigrateTLSPSKData *data =
910         g_new0(struct TestMigrateTLSPSKData, 1);
911 
912     data->workdir = g_strdup_printf("%s/tlscredspsk0", tmpfs);
913     data->pskfile = g_strdup_printf("%s/%s", data->workdir,
914                                     QCRYPTO_TLS_CREDS_PSKFILE);
915     g_mkdir_with_parents(data->workdir, 0700);
916     test_tls_psk_init(data->pskfile);
917 
918     if (mismatch) {
919         data->workdiralt = g_strdup_printf("%s/tlscredspskalt0", tmpfs);
920         data->pskfilealt = g_strdup_printf("%s/%s", data->workdiralt,
921                                            QCRYPTO_TLS_CREDS_PSKFILE);
922         g_mkdir_with_parents(data->workdiralt, 0700);
923         test_tls_psk_init_alt(data->pskfilealt);
924     }
925 
926     qtest_qmp_assert_success(from,
927                              "{ 'execute': 'object-add',"
928                              "  'arguments': { 'qom-type': 'tls-creds-psk',"
929                              "                 'id': 'tlscredspsk0',"
930                              "                 'endpoint': 'client',"
931                              "                 'dir': %s,"
932                              "                 'username': 'qemu'} }",
933                              data->workdir);
934 
935     qtest_qmp_assert_success(to,
936                              "{ 'execute': 'object-add',"
937                              "  'arguments': { 'qom-type': 'tls-creds-psk',"
938                              "                 'id': 'tlscredspsk0',"
939                              "                 'endpoint': 'server',"
940                              "                 'dir': %s } }",
941                              mismatch ? data->workdiralt : data->workdir);
942 
943     migrate_set_parameter_str(from, "tls-creds", "tlscredspsk0");
944     migrate_set_parameter_str(to, "tls-creds", "tlscredspsk0");
945 
946     return data;
947 }
948 
949 static void *
950 test_migrate_tls_psk_start_match(QTestState *from,
951                                  QTestState *to)
952 {
953     return test_migrate_tls_psk_start_common(from, to, false);
954 }
955 
956 static void *
957 test_migrate_tls_psk_start_mismatch(QTestState *from,
958                                     QTestState *to)
959 {
960     return test_migrate_tls_psk_start_common(from, to, true);
961 }
962 
963 static void
964 test_migrate_tls_psk_finish(QTestState *from,
965                             QTestState *to,
966                             void *opaque)
967 {
968     struct TestMigrateTLSPSKData *data = opaque;
969 
970     test_tls_psk_cleanup(data->pskfile);
971     if (data->pskfilealt) {
972         test_tls_psk_cleanup(data->pskfilealt);
973     }
974     rmdir(data->workdir);
975     if (data->workdiralt) {
976         rmdir(data->workdiralt);
977     }
978 
979     g_free(data->workdiralt);
980     g_free(data->pskfilealt);
981     g_free(data->workdir);
982     g_free(data->pskfile);
983     g_free(data);
984 }
985 
986 #ifdef CONFIG_TASN1
987 typedef struct {
988     char *workdir;
989     char *keyfile;
990     char *cacert;
991     char *servercert;
992     char *serverkey;
993     char *clientcert;
994     char *clientkey;
995 } TestMigrateTLSX509Data;
996 
997 typedef struct {
998     bool verifyclient;
999     bool clientcert;
1000     bool hostileclient;
1001     bool authzclient;
1002     const char *certhostname;
1003     const char *certipaddr;
1004 } TestMigrateTLSX509;
1005 
1006 static void *
1007 test_migrate_tls_x509_start_common(QTestState *from,
1008                                    QTestState *to,
1009                                    TestMigrateTLSX509 *args)
1010 {
1011     TestMigrateTLSX509Data *data = g_new0(TestMigrateTLSX509Data, 1);
1012 
1013     data->workdir = g_strdup_printf("%s/tlscredsx5090", tmpfs);
1014     data->keyfile = g_strdup_printf("%s/key.pem", data->workdir);
1015 
1016     data->cacert = g_strdup_printf("%s/ca-cert.pem", data->workdir);
1017     data->serverkey = g_strdup_printf("%s/server-key.pem", data->workdir);
1018     data->servercert = g_strdup_printf("%s/server-cert.pem", data->workdir);
1019     if (args->clientcert) {
1020         data->clientkey = g_strdup_printf("%s/client-key.pem", data->workdir);
1021         data->clientcert = g_strdup_printf("%s/client-cert.pem", data->workdir);
1022     }
1023 
1024     g_mkdir_with_parents(data->workdir, 0700);
1025 
1026     test_tls_init(data->keyfile);
1027 #ifndef _WIN32
1028     g_assert(link(data->keyfile, data->serverkey) == 0);
1029 #else
1030     g_assert(CreateHardLink(data->serverkey, data->keyfile, NULL) != 0);
1031 #endif
1032     if (args->clientcert) {
1033 #ifndef _WIN32
1034         g_assert(link(data->keyfile, data->clientkey) == 0);
1035 #else
1036         g_assert(CreateHardLink(data->clientkey, data->keyfile, NULL) != 0);
1037 #endif
1038     }
1039 
1040     TLS_ROOT_REQ_SIMPLE(cacertreq, data->cacert);
1041     if (args->clientcert) {
1042         TLS_CERT_REQ_SIMPLE_CLIENT(servercertreq, cacertreq,
1043                                    args->hostileclient ?
1044                                    QCRYPTO_TLS_TEST_CLIENT_HOSTILE_NAME :
1045                                    QCRYPTO_TLS_TEST_CLIENT_NAME,
1046                                    data->clientcert);
1047     }
1048 
1049     TLS_CERT_REQ_SIMPLE_SERVER(clientcertreq, cacertreq,
1050                                data->servercert,
1051                                args->certhostname,
1052                                args->certipaddr);
1053 
1054     qtest_qmp_assert_success(from,
1055                              "{ 'execute': 'object-add',"
1056                              "  'arguments': { 'qom-type': 'tls-creds-x509',"
1057                              "                 'id': 'tlscredsx509client0',"
1058                              "                 'endpoint': 'client',"
1059                              "                 'dir': %s,"
1060                              "                 'sanity-check': true,"
1061                              "                 'verify-peer': true} }",
1062                              data->workdir);
1063     migrate_set_parameter_str(from, "tls-creds", "tlscredsx509client0");
1064     if (args->certhostname) {
1065         migrate_set_parameter_str(from, "tls-hostname", args->certhostname);
1066     }
1067 
1068     qtest_qmp_assert_success(to,
1069                              "{ 'execute': 'object-add',"
1070                              "  'arguments': { 'qom-type': 'tls-creds-x509',"
1071                              "                 'id': 'tlscredsx509server0',"
1072                              "                 'endpoint': 'server',"
1073                              "                 'dir': %s,"
1074                              "                 'sanity-check': true,"
1075                              "                 'verify-peer': %i} }",
1076                              data->workdir, args->verifyclient);
1077     migrate_set_parameter_str(to, "tls-creds", "tlscredsx509server0");
1078 
1079     if (args->authzclient) {
1080         qtest_qmp_assert_success(to,
1081                                  "{ 'execute': 'object-add',"
1082                                  "  'arguments': { 'qom-type': 'authz-simple',"
1083                                  "                 'id': 'tlsauthz0',"
1084                                  "                 'identity': %s} }",
1085                                  "CN=" QCRYPTO_TLS_TEST_CLIENT_NAME);
1086         migrate_set_parameter_str(to, "tls-authz", "tlsauthz0");
1087     }
1088 
1089     return data;
1090 }
1091 
1092 /*
1093  * The normal case: match server's cert hostname against
1094  * whatever host we were telling QEMU to connect to (if any)
1095  */
1096 static void *
1097 test_migrate_tls_x509_start_default_host(QTestState *from,
1098                                          QTestState *to)
1099 {
1100     TestMigrateTLSX509 args = {
1101         .verifyclient = true,
1102         .clientcert = true,
1103         .certipaddr = "127.0.0.1"
1104     };
1105     return test_migrate_tls_x509_start_common(from, to, &args);
1106 }
1107 
1108 /*
1109  * The unusual case: the server's cert is different from
1110  * the address we're telling QEMU to connect to (if any),
1111  * so we must give QEMU an explicit hostname to validate
1112  */
1113 static void *
1114 test_migrate_tls_x509_start_override_host(QTestState *from,
1115                                           QTestState *to)
1116 {
1117     TestMigrateTLSX509 args = {
1118         .verifyclient = true,
1119         .clientcert = true,
1120         .certhostname = "qemu.org",
1121     };
1122     return test_migrate_tls_x509_start_common(from, to, &args);
1123 }
1124 
1125 /*
1126  * The unusual case: the server's cert is different from
1127  * the address we're telling QEMU to connect to, and so we
1128  * expect the client to reject the server
1129  */
1130 static void *
1131 test_migrate_tls_x509_start_mismatch_host(QTestState *from,
1132                                           QTestState *to)
1133 {
1134     TestMigrateTLSX509 args = {
1135         .verifyclient = true,
1136         .clientcert = true,
1137         .certipaddr = "10.0.0.1",
1138     };
1139     return test_migrate_tls_x509_start_common(from, to, &args);
1140 }
1141 
1142 static void *
1143 test_migrate_tls_x509_start_friendly_client(QTestState *from,
1144                                             QTestState *to)
1145 {
1146     TestMigrateTLSX509 args = {
1147         .verifyclient = true,
1148         .clientcert = true,
1149         .authzclient = true,
1150         .certipaddr = "127.0.0.1",
1151     };
1152     return test_migrate_tls_x509_start_common(from, to, &args);
1153 }
1154 
1155 static void *
1156 test_migrate_tls_x509_start_hostile_client(QTestState *from,
1157                                            QTestState *to)
1158 {
1159     TestMigrateTLSX509 args = {
1160         .verifyclient = true,
1161         .clientcert = true,
1162         .hostileclient = true,
1163         .authzclient = true,
1164         .certipaddr = "127.0.0.1",
1165     };
1166     return test_migrate_tls_x509_start_common(from, to, &args);
1167 }
1168 
1169 /*
1170  * The case with no client certificate presented,
1171  * and no server verification
1172  */
1173 static void *
1174 test_migrate_tls_x509_start_allow_anon_client(QTestState *from,
1175                                               QTestState *to)
1176 {
1177     TestMigrateTLSX509 args = {
1178         .certipaddr = "127.0.0.1",
1179     };
1180     return test_migrate_tls_x509_start_common(from, to, &args);
1181 }
1182 
1183 /*
1184  * The case with no client certificate presented,
1185  * and server verification rejecting
1186  */
1187 static void *
1188 test_migrate_tls_x509_start_reject_anon_client(QTestState *from,
1189                                                QTestState *to)
1190 {
1191     TestMigrateTLSX509 args = {
1192         .verifyclient = true,
1193         .certipaddr = "127.0.0.1",
1194     };
1195     return test_migrate_tls_x509_start_common(from, to, &args);
1196 }
1197 
1198 static void
1199 test_migrate_tls_x509_finish(QTestState *from,
1200                              QTestState *to,
1201                              void *opaque)
1202 {
1203     TestMigrateTLSX509Data *data = opaque;
1204 
1205     test_tls_cleanup(data->keyfile);
1206     g_free(data->keyfile);
1207 
1208     unlink(data->cacert);
1209     g_free(data->cacert);
1210     unlink(data->servercert);
1211     g_free(data->servercert);
1212     unlink(data->serverkey);
1213     g_free(data->serverkey);
1214 
1215     if (data->clientcert) {
1216         unlink(data->clientcert);
1217         g_free(data->clientcert);
1218     }
1219     if (data->clientkey) {
1220         unlink(data->clientkey);
1221         g_free(data->clientkey);
1222     }
1223 
1224     rmdir(data->workdir);
1225     g_free(data->workdir);
1226 
1227     g_free(data);
1228 }
1229 #endif /* CONFIG_TASN1 */
1230 #endif /* CONFIG_GNUTLS */
1231 
1232 static void *
1233 test_migrate_compress_start(QTestState *from,
1234                             QTestState *to)
1235 {
1236     migrate_set_parameter_int(from, "compress-level", 1);
1237     migrate_set_parameter_int(from, "compress-threads", 4);
1238     migrate_set_parameter_bool(from, "compress-wait-thread", true);
1239     migrate_set_parameter_int(to, "decompress-threads", 4);
1240 
1241     migrate_set_capability(from, "compress", true);
1242     migrate_set_capability(to, "compress", true);
1243 
1244     return NULL;
1245 }
1246 
1247 static void *
1248 test_migrate_compress_nowait_start(QTestState *from,
1249                                    QTestState *to)
1250 {
1251     migrate_set_parameter_int(from, "compress-level", 9);
1252     migrate_set_parameter_int(from, "compress-threads", 1);
1253     migrate_set_parameter_bool(from, "compress-wait-thread", false);
1254     migrate_set_parameter_int(to, "decompress-threads", 1);
1255 
1256     migrate_set_capability(from, "compress", true);
1257     migrate_set_capability(to, "compress", true);
1258 
1259     return NULL;
1260 }
1261 
1262 static int migrate_postcopy_prepare(QTestState **from_ptr,
1263                                     QTestState **to_ptr,
1264                                     MigrateCommon *args)
1265 {
1266     QTestState *from, *to;
1267 
1268     if (test_migrate_start(&from, &to, "defer", &args->start)) {
1269         return -1;
1270     }
1271 
1272     if (args->start_hook) {
1273         args->postcopy_data = args->start_hook(from, to);
1274     }
1275 
1276     migrate_set_capability(from, "postcopy-ram", true);
1277     migrate_set_capability(to, "postcopy-ram", true);
1278     migrate_set_capability(to, "postcopy-blocktime", true);
1279 
1280     if (args->postcopy_preempt) {
1281         migrate_set_capability(from, "postcopy-preempt", true);
1282         migrate_set_capability(to, "postcopy-preempt", true);
1283     }
1284 
1285     migrate_ensure_non_converge(from);
1286 
1287     migrate_prepare_for_dirty_mem(from);
1288     qtest_qmp_assert_success(to, "{ 'execute': 'migrate-incoming',"
1289                              "  'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1290 
1291     /* Wait for the first serial output from the source */
1292     wait_for_serial("src_serial");
1293 
1294     g_autofree char *uri = migrate_get_socket_address(to, "socket-address");
1295     migrate_qmp(from, uri, "{}");
1296 
1297     migrate_wait_for_dirty_mem(from, to);
1298 
1299     *from_ptr = from;
1300     *to_ptr = to;
1301 
1302     return 0;
1303 }
1304 
1305 static void migrate_postcopy_complete(QTestState *from, QTestState *to,
1306                                       MigrateCommon *args)
1307 {
1308     wait_for_migration_complete(from);
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_compress(void)
1344 {
1345     MigrateCommon args = {
1346         .start_hook = test_migrate_compress_start
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 test_postcopy_recovery_common(MigrateCommon *args)
1385 {
1386     QTestState *from, *to;
1387     g_autofree char *uri = NULL;
1388 
1389     /* Always hide errors for postcopy recover tests since they're expected */
1390     args->start.hide_stderr = true;
1391 
1392     if (migrate_postcopy_prepare(&from, &to, args)) {
1393         return;
1394     }
1395 
1396     /* Turn postcopy speed down, 4K/s is slow enough on any machines */
1397     migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
1398 
1399     /* Now we start the postcopy */
1400     migrate_postcopy_start(from, to);
1401 
1402     /*
1403      * Wait until postcopy is really started; we can only run the
1404      * migrate-pause command during a postcopy
1405      */
1406     wait_for_migration_status(from, "postcopy-active", NULL);
1407 
1408     /*
1409      * Manually stop the postcopy migration. This emulates a network
1410      * failure with the migration socket
1411      */
1412     migrate_pause(from);
1413 
1414     /*
1415      * Wait for destination side to reach postcopy-paused state.  The
1416      * migrate-recover command can only succeed if destination machine
1417      * is in the paused state
1418      */
1419     wait_for_migration_status(to, "postcopy-paused",
1420                               (const char * []) { "failed", "active",
1421                                                   "completed", NULL });
1422 
1423     /*
1424      * Create a new socket to emulate a new channel that is different
1425      * from the broken migration channel; tell the destination to
1426      * listen to the new port
1427      */
1428     uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
1429     migrate_recover(to, uri);
1430 
1431     /*
1432      * Try to rebuild the migration channel using the resume flag and
1433      * the newly created channel
1434      */
1435     wait_for_migration_status(from, "postcopy-paused",
1436                               (const char * []) { "failed", "active",
1437                                                   "completed", NULL });
1438     migrate_qmp(from, uri, "{'resume': true}");
1439 
1440     /* Restore the postcopy bandwidth to unlimited */
1441     migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
1442 
1443     migrate_postcopy_complete(from, to, args);
1444 }
1445 
1446 static void test_postcopy_recovery(void)
1447 {
1448     MigrateCommon args = { };
1449 
1450     test_postcopy_recovery_common(&args);
1451 }
1452 
1453 static void test_postcopy_recovery_compress(void)
1454 {
1455     MigrateCommon args = {
1456         .start_hook = test_migrate_compress_start
1457     };
1458 
1459     test_postcopy_recovery_common(&args);
1460 }
1461 
1462 #ifdef CONFIG_GNUTLS
1463 static void test_postcopy_recovery_tls_psk(void)
1464 {
1465     MigrateCommon args = {
1466         .start_hook = test_migrate_tls_psk_start_match,
1467         .finish_hook = test_migrate_tls_psk_finish,
1468     };
1469 
1470     test_postcopy_recovery_common(&args);
1471 }
1472 #endif
1473 
1474 static void test_postcopy_preempt_recovery(void)
1475 {
1476     MigrateCommon args = {
1477         .postcopy_preempt = true,
1478     };
1479 
1480     test_postcopy_recovery_common(&args);
1481 }
1482 
1483 #ifdef CONFIG_GNUTLS
1484 /* This contains preempt+recovery+tls test altogether */
1485 static void test_postcopy_preempt_all(void)
1486 {
1487     MigrateCommon args = {
1488         .postcopy_preempt = true,
1489         .start_hook = test_migrate_tls_psk_start_match,
1490         .finish_hook = test_migrate_tls_psk_finish,
1491     };
1492 
1493     test_postcopy_recovery_common(&args);
1494 }
1495 
1496 #endif
1497 
1498 static void test_baddest(void)
1499 {
1500     MigrateStart args = {
1501         .hide_stderr = true
1502     };
1503     QTestState *from, *to;
1504 
1505     if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", &args)) {
1506         return;
1507     }
1508     migrate_qmp(from, "tcp:127.0.0.1:0", "{}");
1509     wait_for_migration_fail(from, false);
1510     test_migrate_end(from, to, false);
1511 }
1512 
1513 static void test_precopy_common(MigrateCommon *args)
1514 {
1515     QTestState *from, *to;
1516     void *data_hook = NULL;
1517 
1518     if (test_migrate_start(&from, &to, args->listen_uri, &args->start)) {
1519         return;
1520     }
1521 
1522     if (args->start_hook) {
1523         data_hook = args->start_hook(from, to);
1524     }
1525 
1526     /* Wait for the first serial output from the source */
1527     if (args->result == MIG_TEST_SUCCEED) {
1528         wait_for_serial("src_serial");
1529     }
1530 
1531     if (args->live) {
1532         migrate_ensure_non_converge(from);
1533         migrate_prepare_for_dirty_mem(from);
1534     } else {
1535         /*
1536          * Testing non-live migration, we allow it to run at
1537          * full speed to ensure short test case duration.
1538          * For tests expected to fail, we don't need to
1539          * change anything.
1540          */
1541         if (args->result == MIG_TEST_SUCCEED) {
1542             qtest_qmp_assert_success(from, "{ 'execute' : 'stop'}");
1543             if (!got_src_stop) {
1544                 qtest_qmp_eventwait(from, "STOP");
1545             }
1546             migrate_ensure_converge(from);
1547         }
1548     }
1549 
1550     if (!args->connect_uri) {
1551         g_autofree char *local_connect_uri =
1552             migrate_get_socket_address(to, "socket-address");
1553         migrate_qmp(from, local_connect_uri, "{}");
1554     } else {
1555         migrate_qmp(from, args->connect_uri, "{}");
1556     }
1557 
1558 
1559     if (args->result != MIG_TEST_SUCCEED) {
1560         bool allow_active = args->result == MIG_TEST_FAIL;
1561         wait_for_migration_fail(from, allow_active);
1562 
1563         if (args->result == MIG_TEST_FAIL_DEST_QUIT_ERR) {
1564             qtest_set_expected_status(to, EXIT_FAILURE);
1565         }
1566     } else {
1567         if (args->live) {
1568             /*
1569              * For initial iteration(s) we must do a full pass,
1570              * but for the final iteration, we need only wait
1571              * for some dirty mem before switching to converge
1572              */
1573             while (args->iterations > 1) {
1574                 wait_for_migration_pass(from);
1575                 args->iterations--;
1576             }
1577             migrate_wait_for_dirty_mem(from, to);
1578 
1579             migrate_ensure_converge(from);
1580 
1581             /*
1582              * We do this first, as it has a timeout to stop us
1583              * hanging forever if migration didn't converge
1584              */
1585             wait_for_migration_complete(from);
1586 
1587             if (!got_src_stop) {
1588                 qtest_qmp_eventwait(from, "STOP");
1589             }
1590         } else {
1591             wait_for_migration_complete(from);
1592             /*
1593              * Must wait for dst to finish reading all incoming
1594              * data on the socket before issuing 'cont' otherwise
1595              * it'll be ignored
1596              */
1597             wait_for_migration_complete(to);
1598 
1599             qtest_qmp_assert_success(to, "{ 'execute' : 'cont'}");
1600         }
1601 
1602         if (!got_dst_resume) {
1603             qtest_qmp_eventwait(to, "RESUME");
1604         }
1605 
1606         wait_for_serial("dest_serial");
1607     }
1608 
1609     if (args->finish_hook) {
1610         args->finish_hook(from, to, data_hook);
1611     }
1612 
1613     test_migrate_end(from, to, args->result == MIG_TEST_SUCCEED);
1614 }
1615 
1616 static void test_precopy_unix_plain(void)
1617 {
1618     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1619     MigrateCommon args = {
1620         .listen_uri = uri,
1621         .connect_uri = uri,
1622         /*
1623          * The simplest use case of precopy, covering smoke tests of
1624          * get-dirty-log dirty tracking.
1625          */
1626         .live = true,
1627     };
1628 
1629     test_precopy_common(&args);
1630 }
1631 
1632 
1633 static void test_precopy_unix_dirty_ring(void)
1634 {
1635     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1636     MigrateCommon args = {
1637         .start = {
1638             .use_dirty_ring = true,
1639         },
1640         .listen_uri = uri,
1641         .connect_uri = uri,
1642         /*
1643          * Besides the precopy/unix basic test, cover dirty ring interface
1644          * rather than get-dirty-log.
1645          */
1646         .live = true,
1647     };
1648 
1649     test_precopy_common(&args);
1650 }
1651 
1652 #ifdef CONFIG_GNUTLS
1653 static void test_precopy_unix_tls_psk(void)
1654 {
1655     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1656     MigrateCommon args = {
1657         .connect_uri = uri,
1658         .listen_uri = uri,
1659         .start_hook = test_migrate_tls_psk_start_match,
1660         .finish_hook = test_migrate_tls_psk_finish,
1661     };
1662 
1663     test_precopy_common(&args);
1664 }
1665 
1666 #ifdef CONFIG_TASN1
1667 static void test_precopy_unix_tls_x509_default_host(void)
1668 {
1669     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1670     MigrateCommon args = {
1671         .start = {
1672             .hide_stderr = true,
1673         },
1674         .connect_uri = uri,
1675         .listen_uri = uri,
1676         .start_hook = test_migrate_tls_x509_start_default_host,
1677         .finish_hook = test_migrate_tls_x509_finish,
1678         .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
1679     };
1680 
1681     test_precopy_common(&args);
1682 }
1683 
1684 static void test_precopy_unix_tls_x509_override_host(void)
1685 {
1686     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1687     MigrateCommon args = {
1688         .connect_uri = uri,
1689         .listen_uri = uri,
1690         .start_hook = test_migrate_tls_x509_start_override_host,
1691         .finish_hook = test_migrate_tls_x509_finish,
1692     };
1693 
1694     test_precopy_common(&args);
1695 }
1696 #endif /* CONFIG_TASN1 */
1697 #endif /* CONFIG_GNUTLS */
1698 
1699 #if 0
1700 /* Currently upset on aarch64 TCG */
1701 static void test_ignore_shared(void)
1702 {
1703     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1704     QTestState *from, *to;
1705 
1706     if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) {
1707         return;
1708     }
1709 
1710     migrate_ensure_non_converge(from);
1711     migrate_prepare_for_dirty_mem(from);
1712 
1713     migrate_set_capability(from, "x-ignore-shared", true);
1714     migrate_set_capability(to, "x-ignore-shared", true);
1715 
1716     /* Wait for the first serial output from the source */
1717     wait_for_serial("src_serial");
1718 
1719     migrate_qmp(from, uri, "{}");
1720 
1721     migrate_wait_for_dirty_mem(from, to);
1722 
1723     if (!got_src_stop) {
1724         qtest_qmp_eventwait(from, "STOP");
1725     }
1726 
1727     qtest_qmp_eventwait(to, "RESUME");
1728 
1729     wait_for_serial("dest_serial");
1730     wait_for_migration_complete(from);
1731 
1732     /* Check whether shared RAM has been really skipped */
1733     g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
1734 
1735     test_migrate_end(from, to, true);
1736 }
1737 #endif
1738 
1739 static void *
1740 test_migrate_xbzrle_start(QTestState *from,
1741                           QTestState *to)
1742 {
1743     migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
1744 
1745     migrate_set_capability(from, "xbzrle", true);
1746     migrate_set_capability(to, "xbzrle", true);
1747 
1748     return NULL;
1749 }
1750 
1751 static void test_precopy_unix_xbzrle(void)
1752 {
1753     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1754     MigrateCommon args = {
1755         .connect_uri = uri,
1756         .listen_uri = uri,
1757         .start_hook = test_migrate_xbzrle_start,
1758         .iterations = 2,
1759         /*
1760          * XBZRLE needs pages to be modified when doing the 2nd+ round
1761          * iteration to have real data pushed to the stream.
1762          */
1763         .live = true,
1764     };
1765 
1766     test_precopy_common(&args);
1767 }
1768 
1769 static void test_precopy_unix_compress(void)
1770 {
1771     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1772     MigrateCommon args = {
1773         .connect_uri = uri,
1774         .listen_uri = uri,
1775         .start_hook = test_migrate_compress_start,
1776         /*
1777          * Test that no invalid thread state is left over from
1778          * the previous iteration.
1779          */
1780         .iterations = 2,
1781         /*
1782          * We make sure the compressor can always work well even if guest
1783          * memory is changing.  See commit 34ab9e9743 where we used to fix
1784          * a bug when only trigger-able with guest memory changing.
1785          */
1786         .live = true,
1787     };
1788 
1789     test_precopy_common(&args);
1790 }
1791 
1792 static void test_precopy_unix_compress_nowait(void)
1793 {
1794     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1795     MigrateCommon args = {
1796         .connect_uri = uri,
1797         .listen_uri = uri,
1798         .start_hook = test_migrate_compress_nowait_start,
1799         /*
1800          * Test that no invalid thread state is left over from
1801          * the previous iteration.
1802          */
1803         .iterations = 2,
1804         /* Same reason for the wait version of precopy compress test */
1805         .live = true,
1806     };
1807 
1808     test_precopy_common(&args);
1809 }
1810 
1811 static void test_precopy_tcp_plain(void)
1812 {
1813     MigrateCommon args = {
1814         .listen_uri = "tcp:127.0.0.1:0",
1815     };
1816 
1817     test_precopy_common(&args);
1818 }
1819 
1820 static void *test_migrate_switchover_ack_start(QTestState *from, QTestState *to)
1821 {
1822 
1823     migrate_set_capability(from, "return-path", true);
1824     migrate_set_capability(to, "return-path", true);
1825 
1826     migrate_set_capability(from, "switchover-ack", true);
1827     migrate_set_capability(to, "switchover-ack", true);
1828 
1829     return NULL;
1830 }
1831 
1832 static void test_precopy_tcp_switchover_ack(void)
1833 {
1834     MigrateCommon args = {
1835         .listen_uri = "tcp:127.0.0.1:0",
1836         .start_hook = test_migrate_switchover_ack_start,
1837         /*
1838          * Source VM must be running in order to consider the switchover ACK
1839          * when deciding to do switchover or not.
1840          */
1841         .live = true,
1842     };
1843 
1844     test_precopy_common(&args);
1845 }
1846 
1847 #ifdef CONFIG_GNUTLS
1848 static void test_precopy_tcp_tls_psk_match(void)
1849 {
1850     MigrateCommon args = {
1851         .listen_uri = "tcp:127.0.0.1:0",
1852         .start_hook = test_migrate_tls_psk_start_match,
1853         .finish_hook = test_migrate_tls_psk_finish,
1854     };
1855 
1856     test_precopy_common(&args);
1857 }
1858 
1859 static void test_precopy_tcp_tls_psk_mismatch(void)
1860 {
1861     MigrateCommon args = {
1862         .start = {
1863             .hide_stderr = true,
1864         },
1865         .listen_uri = "tcp:127.0.0.1:0",
1866         .start_hook = test_migrate_tls_psk_start_mismatch,
1867         .finish_hook = test_migrate_tls_psk_finish,
1868         .result = MIG_TEST_FAIL,
1869     };
1870 
1871     test_precopy_common(&args);
1872 }
1873 
1874 #ifdef CONFIG_TASN1
1875 static void test_precopy_tcp_tls_x509_default_host(void)
1876 {
1877     MigrateCommon args = {
1878         .listen_uri = "tcp:127.0.0.1:0",
1879         .start_hook = test_migrate_tls_x509_start_default_host,
1880         .finish_hook = test_migrate_tls_x509_finish,
1881     };
1882 
1883     test_precopy_common(&args);
1884 }
1885 
1886 static void test_precopy_tcp_tls_x509_override_host(void)
1887 {
1888     MigrateCommon args = {
1889         .listen_uri = "tcp:127.0.0.1:0",
1890         .start_hook = test_migrate_tls_x509_start_override_host,
1891         .finish_hook = test_migrate_tls_x509_finish,
1892     };
1893 
1894     test_precopy_common(&args);
1895 }
1896 
1897 static void test_precopy_tcp_tls_x509_mismatch_host(void)
1898 {
1899     MigrateCommon args = {
1900         .start = {
1901             .hide_stderr = true,
1902         },
1903         .listen_uri = "tcp:127.0.0.1:0",
1904         .start_hook = test_migrate_tls_x509_start_mismatch_host,
1905         .finish_hook = test_migrate_tls_x509_finish,
1906         .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
1907     };
1908 
1909     test_precopy_common(&args);
1910 }
1911 
1912 static void test_precopy_tcp_tls_x509_friendly_client(void)
1913 {
1914     MigrateCommon args = {
1915         .listen_uri = "tcp:127.0.0.1:0",
1916         .start_hook = test_migrate_tls_x509_start_friendly_client,
1917         .finish_hook = test_migrate_tls_x509_finish,
1918     };
1919 
1920     test_precopy_common(&args);
1921 }
1922 
1923 static void test_precopy_tcp_tls_x509_hostile_client(void)
1924 {
1925     MigrateCommon args = {
1926         .start = {
1927             .hide_stderr = true,
1928         },
1929         .listen_uri = "tcp:127.0.0.1:0",
1930         .start_hook = test_migrate_tls_x509_start_hostile_client,
1931         .finish_hook = test_migrate_tls_x509_finish,
1932         .result = MIG_TEST_FAIL,
1933     };
1934 
1935     test_precopy_common(&args);
1936 }
1937 
1938 static void test_precopy_tcp_tls_x509_allow_anon_client(void)
1939 {
1940     MigrateCommon args = {
1941         .listen_uri = "tcp:127.0.0.1:0",
1942         .start_hook = test_migrate_tls_x509_start_allow_anon_client,
1943         .finish_hook = test_migrate_tls_x509_finish,
1944     };
1945 
1946     test_precopy_common(&args);
1947 }
1948 
1949 static void test_precopy_tcp_tls_x509_reject_anon_client(void)
1950 {
1951     MigrateCommon args = {
1952         .start = {
1953             .hide_stderr = true,
1954         },
1955         .listen_uri = "tcp:127.0.0.1:0",
1956         .start_hook = test_migrate_tls_x509_start_reject_anon_client,
1957         .finish_hook = test_migrate_tls_x509_finish,
1958         .result = MIG_TEST_FAIL,
1959     };
1960 
1961     test_precopy_common(&args);
1962 }
1963 #endif /* CONFIG_TASN1 */
1964 #endif /* CONFIG_GNUTLS */
1965 
1966 #ifndef _WIN32
1967 static void *test_migrate_fd_start_hook(QTestState *from,
1968                                         QTestState *to)
1969 {
1970     int ret;
1971     int pair[2];
1972 
1973     /* Create two connected sockets for migration */
1974     ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
1975     g_assert_cmpint(ret, ==, 0);
1976 
1977     /* Send the 1st socket to the target */
1978     qtest_qmp_fds_assert_success(to, &pair[0], 1,
1979                                  "{ 'execute': 'getfd',"
1980                                  "  'arguments': { 'fdname': 'fd-mig' }}");
1981     close(pair[0]);
1982 
1983     /* Start incoming migration from the 1st socket */
1984     qtest_qmp_assert_success(to, "{ 'execute': 'migrate-incoming',"
1985                              "  'arguments': { 'uri': 'fd:fd-mig' }}");
1986 
1987     /* Send the 2nd socket to the target */
1988     qtest_qmp_fds_assert_success(from, &pair[1], 1,
1989                                  "{ 'execute': 'getfd',"
1990                                  "  'arguments': { 'fdname': 'fd-mig' }}");
1991     close(pair[1]);
1992 
1993     return NULL;
1994 }
1995 
1996 static void test_migrate_fd_finish_hook(QTestState *from,
1997                                         QTestState *to,
1998                                         void *opaque)
1999 {
2000     QDict *rsp;
2001     const char *error_desc;
2002 
2003     /* Test closing fds */
2004     /* We assume, that QEMU removes named fd from its list,
2005      * so this should fail */
2006     rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
2007                           "  'arguments': { 'fdname': 'fd-mig' }}");
2008     g_assert_true(qdict_haskey(rsp, "error"));
2009     error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
2010     g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
2011     qobject_unref(rsp);
2012 
2013     rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
2014                         "  'arguments': { 'fdname': 'fd-mig' }}");
2015     g_assert_true(qdict_haskey(rsp, "error"));
2016     error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
2017     g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
2018     qobject_unref(rsp);
2019 }
2020 
2021 static void test_migrate_fd_proto(void)
2022 {
2023     MigrateCommon args = {
2024         .listen_uri = "defer",
2025         .connect_uri = "fd:fd-mig",
2026         .start_hook = test_migrate_fd_start_hook,
2027         .finish_hook = test_migrate_fd_finish_hook
2028     };
2029     test_precopy_common(&args);
2030 }
2031 #endif /* _WIN32 */
2032 
2033 static void do_test_validate_uuid(MigrateStart *args, bool should_fail)
2034 {
2035     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
2036     QTestState *from, *to;
2037 
2038     if (test_migrate_start(&from, &to, uri, args)) {
2039         return;
2040     }
2041 
2042     /*
2043      * UUID validation is at the begin of migration. So, the main process of
2044      * migration is not interesting for us here. Thus, set huge downtime for
2045      * very fast migration.
2046      */
2047     migrate_set_parameter_int(from, "downtime-limit", 1000000);
2048     migrate_set_capability(from, "validate-uuid", true);
2049 
2050     /* Wait for the first serial output from the source */
2051     wait_for_serial("src_serial");
2052 
2053     migrate_qmp(from, uri, "{}");
2054 
2055     if (should_fail) {
2056         qtest_set_expected_status(to, EXIT_FAILURE);
2057         wait_for_migration_fail(from, true);
2058     } else {
2059         wait_for_migration_complete(from);
2060     }
2061 
2062     test_migrate_end(from, to, false);
2063 }
2064 
2065 static void test_validate_uuid(void)
2066 {
2067     MigrateStart args = {
2068         .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
2069         .opts_target = "-uuid 11111111-1111-1111-1111-111111111111",
2070     };
2071 
2072     do_test_validate_uuid(&args, false);
2073 }
2074 
2075 static void test_validate_uuid_error(void)
2076 {
2077     MigrateStart args = {
2078         .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
2079         .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
2080         .hide_stderr = true,
2081     };
2082 
2083     do_test_validate_uuid(&args, true);
2084 }
2085 
2086 static void test_validate_uuid_src_not_set(void)
2087 {
2088     MigrateStart args = {
2089         .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
2090         .hide_stderr = true,
2091     };
2092 
2093     do_test_validate_uuid(&args, false);
2094 }
2095 
2096 static void test_validate_uuid_dst_not_set(void)
2097 {
2098     MigrateStart args = {
2099         .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
2100         .hide_stderr = true,
2101     };
2102 
2103     do_test_validate_uuid(&args, false);
2104 }
2105 
2106 /*
2107  * The way auto_converge works, we need to do too many passes to
2108  * run this test.  Auto_converge logic is only run once every
2109  * three iterations, so:
2110  *
2111  * - 3 iterations without auto_converge enabled
2112  * - 3 iterations with pct = 5
2113  * - 3 iterations with pct = 30
2114  * - 3 iterations with pct = 55
2115  * - 3 iterations with pct = 80
2116  * - 3 iterations with pct = 95 (max(95, 80 + 25))
2117  *
2118  * To make things even worse, we need to run the initial stage at
2119  * 3MB/s so we enter autoconverge even when host is (over)loaded.
2120  */
2121 static void test_migrate_auto_converge(void)
2122 {
2123     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
2124     MigrateStart args = {};
2125     QTestState *from, *to;
2126     int64_t percentage;
2127 
2128     /*
2129      * We want the test to be stable and as fast as possible.
2130      * E.g., with 1Gb/s bandwidth migration may pass without throttling,
2131      * so we need to decrease a bandwidth.
2132      */
2133     const int64_t init_pct = 5, inc_pct = 25, max_pct = 95;
2134 
2135     if (test_migrate_start(&from, &to, uri, &args)) {
2136         return;
2137     }
2138 
2139     migrate_set_capability(from, "auto-converge", true);
2140     migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct);
2141     migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct);
2142     migrate_set_parameter_int(from, "max-cpu-throttle", max_pct);
2143 
2144     /*
2145      * Set the initial parameters so that the migration could not converge
2146      * without throttling.
2147      */
2148     migrate_ensure_non_converge(from);
2149 
2150     /* To check remaining size after precopy */
2151     migrate_set_capability(from, "pause-before-switchover", true);
2152 
2153     /* Wait for the first serial output from the source */
2154     wait_for_serial("src_serial");
2155 
2156     migrate_qmp(from, uri, "{}");
2157 
2158     /* Wait for throttling begins */
2159     percentage = 0;
2160     do {
2161         percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
2162         if (percentage != 0) {
2163             break;
2164         }
2165         usleep(20);
2166         g_assert_false(got_src_stop);
2167     } while (true);
2168     /* The first percentage of throttling should be at least init_pct */
2169     g_assert_cmpint(percentage, >=, init_pct);
2170     /* Now, when we tested that throttling works, let it converge */
2171     migrate_ensure_converge(from);
2172 
2173     /*
2174      * Wait for pre-switchover status to check last throttle percentage
2175      * and remaining. These values will be zeroed later
2176      */
2177     wait_for_migration_status(from, "pre-switchover", NULL);
2178 
2179     /* The final percentage of throttling shouldn't be greater than max_pct */
2180     percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
2181     g_assert_cmpint(percentage, <=, max_pct);
2182     migrate_continue(from, "pre-switchover");
2183 
2184     qtest_qmp_eventwait(to, "RESUME");
2185 
2186     wait_for_serial("dest_serial");
2187     wait_for_migration_complete(from);
2188 
2189     test_migrate_end(from, to, true);
2190 }
2191 
2192 static void *
2193 test_migrate_precopy_tcp_multifd_start_common(QTestState *from,
2194                                               QTestState *to,
2195                                               const char *method)
2196 {
2197     migrate_set_parameter_int(from, "multifd-channels", 16);
2198     migrate_set_parameter_int(to, "multifd-channels", 16);
2199 
2200     migrate_set_parameter_str(from, "multifd-compression", method);
2201     migrate_set_parameter_str(to, "multifd-compression", method);
2202 
2203     migrate_set_capability(from, "multifd", true);
2204     migrate_set_capability(to, "multifd", true);
2205 
2206     /* Start incoming migration from the 1st socket */
2207     qtest_qmp_assert_success(to, "{ 'execute': 'migrate-incoming',"
2208                              "  'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2209 
2210     return NULL;
2211 }
2212 
2213 static void *
2214 test_migrate_precopy_tcp_multifd_start(QTestState *from,
2215                                        QTestState *to)
2216 {
2217     return test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2218 }
2219 
2220 static void *
2221 test_migrate_precopy_tcp_multifd_zlib_start(QTestState *from,
2222                                             QTestState *to)
2223 {
2224     return test_migrate_precopy_tcp_multifd_start_common(from, to, "zlib");
2225 }
2226 
2227 #ifdef CONFIG_ZSTD
2228 static void *
2229 test_migrate_precopy_tcp_multifd_zstd_start(QTestState *from,
2230                                             QTestState *to)
2231 {
2232     return test_migrate_precopy_tcp_multifd_start_common(from, to, "zstd");
2233 }
2234 #endif /* CONFIG_ZSTD */
2235 
2236 static void test_multifd_tcp_none(void)
2237 {
2238     MigrateCommon args = {
2239         .listen_uri = "defer",
2240         .start_hook = test_migrate_precopy_tcp_multifd_start,
2241         /*
2242          * Multifd is more complicated than most of the features, it
2243          * directly takes guest page buffers when sending, make sure
2244          * everything will work alright even if guest page is changing.
2245          */
2246         .live = true,
2247     };
2248     test_precopy_common(&args);
2249 }
2250 
2251 static void test_multifd_tcp_zlib(void)
2252 {
2253     MigrateCommon args = {
2254         .listen_uri = "defer",
2255         .start_hook = test_migrate_precopy_tcp_multifd_zlib_start,
2256     };
2257     test_precopy_common(&args);
2258 }
2259 
2260 #ifdef CONFIG_ZSTD
2261 static void test_multifd_tcp_zstd(void)
2262 {
2263     MigrateCommon args = {
2264         .listen_uri = "defer",
2265         .start_hook = test_migrate_precopy_tcp_multifd_zstd_start,
2266     };
2267     test_precopy_common(&args);
2268 }
2269 #endif
2270 
2271 #ifdef CONFIG_GNUTLS
2272 static void *
2273 test_migrate_multifd_tcp_tls_psk_start_match(QTestState *from,
2274                                              QTestState *to)
2275 {
2276     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2277     return test_migrate_tls_psk_start_match(from, to);
2278 }
2279 
2280 static void *
2281 test_migrate_multifd_tcp_tls_psk_start_mismatch(QTestState *from,
2282                                                 QTestState *to)
2283 {
2284     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2285     return test_migrate_tls_psk_start_mismatch(from, to);
2286 }
2287 
2288 #ifdef CONFIG_TASN1
2289 static void *
2290 test_migrate_multifd_tls_x509_start_default_host(QTestState *from,
2291                                                  QTestState *to)
2292 {
2293     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2294     return test_migrate_tls_x509_start_default_host(from, to);
2295 }
2296 
2297 static void *
2298 test_migrate_multifd_tls_x509_start_override_host(QTestState *from,
2299                                                   QTestState *to)
2300 {
2301     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2302     return test_migrate_tls_x509_start_override_host(from, to);
2303 }
2304 
2305 static void *
2306 test_migrate_multifd_tls_x509_start_mismatch_host(QTestState *from,
2307                                                   QTestState *to)
2308 {
2309     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2310     return test_migrate_tls_x509_start_mismatch_host(from, to);
2311 }
2312 
2313 static void *
2314 test_migrate_multifd_tls_x509_start_allow_anon_client(QTestState *from,
2315                                                       QTestState *to)
2316 {
2317     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2318     return test_migrate_tls_x509_start_allow_anon_client(from, to);
2319 }
2320 
2321 static void *
2322 test_migrate_multifd_tls_x509_start_reject_anon_client(QTestState *from,
2323                                                        QTestState *to)
2324 {
2325     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2326     return test_migrate_tls_x509_start_reject_anon_client(from, to);
2327 }
2328 #endif /* CONFIG_TASN1 */
2329 
2330 static void test_multifd_tcp_tls_psk_match(void)
2331 {
2332     MigrateCommon args = {
2333         .listen_uri = "defer",
2334         .start_hook = test_migrate_multifd_tcp_tls_psk_start_match,
2335         .finish_hook = test_migrate_tls_psk_finish,
2336     };
2337     test_precopy_common(&args);
2338 }
2339 
2340 static void test_multifd_tcp_tls_psk_mismatch(void)
2341 {
2342     MigrateCommon args = {
2343         .start = {
2344             .hide_stderr = true,
2345         },
2346         .listen_uri = "defer",
2347         .start_hook = test_migrate_multifd_tcp_tls_psk_start_mismatch,
2348         .finish_hook = test_migrate_tls_psk_finish,
2349         .result = MIG_TEST_FAIL,
2350     };
2351     test_precopy_common(&args);
2352 }
2353 
2354 #ifdef CONFIG_TASN1
2355 static void test_multifd_tcp_tls_x509_default_host(void)
2356 {
2357     MigrateCommon args = {
2358         .listen_uri = "defer",
2359         .start_hook = test_migrate_multifd_tls_x509_start_default_host,
2360         .finish_hook = test_migrate_tls_x509_finish,
2361     };
2362     test_precopy_common(&args);
2363 }
2364 
2365 static void test_multifd_tcp_tls_x509_override_host(void)
2366 {
2367     MigrateCommon args = {
2368         .listen_uri = "defer",
2369         .start_hook = test_migrate_multifd_tls_x509_start_override_host,
2370         .finish_hook = test_migrate_tls_x509_finish,
2371     };
2372     test_precopy_common(&args);
2373 }
2374 
2375 static void test_multifd_tcp_tls_x509_mismatch_host(void)
2376 {
2377     /*
2378      * This has different behaviour to the non-multifd case.
2379      *
2380      * In non-multifd case when client aborts due to mismatched
2381      * cert host, the server has already started trying to load
2382      * migration state, and so it exits with I/O failure.
2383      *
2384      * In multifd case when client aborts due to mismatched
2385      * cert host, the server is still waiting for the other
2386      * multifd connections to arrive so hasn't started trying
2387      * to load migration state, and thus just aborts the migration
2388      * without exiting.
2389      */
2390     MigrateCommon args = {
2391         .start = {
2392             .hide_stderr = true,
2393         },
2394         .listen_uri = "defer",
2395         .start_hook = test_migrate_multifd_tls_x509_start_mismatch_host,
2396         .finish_hook = test_migrate_tls_x509_finish,
2397         .result = MIG_TEST_FAIL,
2398     };
2399     test_precopy_common(&args);
2400 }
2401 
2402 static void test_multifd_tcp_tls_x509_allow_anon_client(void)
2403 {
2404     MigrateCommon args = {
2405         .listen_uri = "defer",
2406         .start_hook = test_migrate_multifd_tls_x509_start_allow_anon_client,
2407         .finish_hook = test_migrate_tls_x509_finish,
2408     };
2409     test_precopy_common(&args);
2410 }
2411 
2412 static void test_multifd_tcp_tls_x509_reject_anon_client(void)
2413 {
2414     MigrateCommon args = {
2415         .start = {
2416             .hide_stderr = true,
2417         },
2418         .listen_uri = "defer",
2419         .start_hook = test_migrate_multifd_tls_x509_start_reject_anon_client,
2420         .finish_hook = test_migrate_tls_x509_finish,
2421         .result = MIG_TEST_FAIL,
2422     };
2423     test_precopy_common(&args);
2424 }
2425 #endif /* CONFIG_TASN1 */
2426 #endif /* CONFIG_GNUTLS */
2427 
2428 /*
2429  * This test does:
2430  *  source               target
2431  *                       migrate_incoming
2432  *     migrate
2433  *     migrate_cancel
2434  *                       launch another target
2435  *     migrate
2436  *
2437  *  And see that it works
2438  */
2439 static void test_multifd_tcp_cancel(void)
2440 {
2441     MigrateStart args = {
2442         .hide_stderr = true,
2443     };
2444     QTestState *from, *to, *to2;
2445     g_autofree char *uri = NULL;
2446 
2447     if (test_migrate_start(&from, &to, "defer", &args)) {
2448         return;
2449     }
2450 
2451     migrate_ensure_non_converge(from);
2452     migrate_prepare_for_dirty_mem(from);
2453 
2454     migrate_set_parameter_int(from, "multifd-channels", 16);
2455     migrate_set_parameter_int(to, "multifd-channels", 16);
2456 
2457     migrate_set_capability(from, "multifd", true);
2458     migrate_set_capability(to, "multifd", true);
2459 
2460     /* Start incoming migration from the 1st socket */
2461     qtest_qmp_assert_success(to, "{ 'execute': 'migrate-incoming',"
2462                              "  'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2463 
2464     /* Wait for the first serial output from the source */
2465     wait_for_serial("src_serial");
2466 
2467     uri = migrate_get_socket_address(to, "socket-address");
2468 
2469     migrate_qmp(from, uri, "{}");
2470 
2471     migrate_wait_for_dirty_mem(from, to);
2472 
2473     migrate_cancel(from);
2474 
2475     /* Make sure QEMU process "to" exited */
2476     qtest_set_expected_status(to, EXIT_FAILURE);
2477     qtest_wait_qemu(to);
2478 
2479     args = (MigrateStart){
2480         .only_target = true,
2481     };
2482 
2483     if (test_migrate_start(&from, &to2, "defer", &args)) {
2484         return;
2485     }
2486 
2487     migrate_set_parameter_int(to2, "multifd-channels", 16);
2488 
2489     migrate_set_capability(to2, "multifd", true);
2490 
2491     /* Start incoming migration from the 1st socket */
2492     qtest_qmp_assert_success(to2, "{ 'execute': 'migrate-incoming',"
2493                              "  'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2494 
2495     g_free(uri);
2496     uri = migrate_get_socket_address(to2, "socket-address");
2497 
2498     wait_for_migration_status(from, "cancelled", NULL);
2499 
2500     migrate_ensure_non_converge(from);
2501 
2502     migrate_qmp(from, uri, "{}");
2503 
2504     migrate_wait_for_dirty_mem(from, to2);
2505 
2506     migrate_ensure_converge(from);
2507 
2508     if (!got_src_stop) {
2509         qtest_qmp_eventwait(from, "STOP");
2510     }
2511     qtest_qmp_eventwait(to2, "RESUME");
2512 
2513     wait_for_serial("dest_serial");
2514     wait_for_migration_complete(from);
2515     test_migrate_end(from, to2, true);
2516 }
2517 
2518 static void calc_dirty_rate(QTestState *who, uint64_t calc_time)
2519 {
2520     qtest_qmp_assert_success(who,
2521                              "{ 'execute': 'calc-dirty-rate',"
2522                              "'arguments': { "
2523                              "'calc-time': %" PRIu64 ","
2524                              "'mode': 'dirty-ring' }}",
2525                              calc_time);
2526 }
2527 
2528 static QDict *query_dirty_rate(QTestState *who)
2529 {
2530     return qtest_qmp_assert_success_ref(who,
2531                                         "{ 'execute': 'query-dirty-rate' }");
2532 }
2533 
2534 static void dirtylimit_set_all(QTestState *who, uint64_t dirtyrate)
2535 {
2536     qtest_qmp_assert_success(who,
2537                              "{ 'execute': 'set-vcpu-dirty-limit',"
2538                              "'arguments': { "
2539                              "'dirty-rate': %" PRIu64 " } }",
2540                              dirtyrate);
2541 }
2542 
2543 static void cancel_vcpu_dirty_limit(QTestState *who)
2544 {
2545     qtest_qmp_assert_success(who,
2546                              "{ 'execute': 'cancel-vcpu-dirty-limit' }");
2547 }
2548 
2549 static QDict *query_vcpu_dirty_limit(QTestState *who)
2550 {
2551     QDict *rsp;
2552 
2553     rsp = qtest_qmp(who, "{ 'execute': 'query-vcpu-dirty-limit' }");
2554     g_assert(!qdict_haskey(rsp, "error"));
2555     g_assert(qdict_haskey(rsp, "return"));
2556 
2557     return rsp;
2558 }
2559 
2560 static bool calc_dirtyrate_ready(QTestState *who)
2561 {
2562     QDict *rsp_return;
2563     gchar *status;
2564 
2565     rsp_return = query_dirty_rate(who);
2566     g_assert(rsp_return);
2567 
2568     status = g_strdup(qdict_get_str(rsp_return, "status"));
2569     g_assert(status);
2570 
2571     return g_strcmp0(status, "measuring");
2572 }
2573 
2574 static void wait_for_calc_dirtyrate_complete(QTestState *who,
2575                                              int64_t time_s)
2576 {
2577     int max_try_count = 10000;
2578     usleep(time_s * 1000000);
2579 
2580     while (!calc_dirtyrate_ready(who) && max_try_count--) {
2581         usleep(1000);
2582     }
2583 
2584     /*
2585      * Set the timeout with 10 s(max_try_count * 1000us),
2586      * if dirtyrate measurement not complete, fail test.
2587      */
2588     g_assert_cmpint(max_try_count, !=, 0);
2589 }
2590 
2591 static int64_t get_dirty_rate(QTestState *who)
2592 {
2593     QDict *rsp_return;
2594     gchar *status;
2595     QList *rates;
2596     const QListEntry *entry;
2597     QDict *rate;
2598     int64_t dirtyrate;
2599 
2600     rsp_return = query_dirty_rate(who);
2601     g_assert(rsp_return);
2602 
2603     status = g_strdup(qdict_get_str(rsp_return, "status"));
2604     g_assert(status);
2605     g_assert_cmpstr(status, ==, "measured");
2606 
2607     rates = qdict_get_qlist(rsp_return, "vcpu-dirty-rate");
2608     g_assert(rates && !qlist_empty(rates));
2609 
2610     entry = qlist_first(rates);
2611     g_assert(entry);
2612 
2613     rate = qobject_to(QDict, qlist_entry_obj(entry));
2614     g_assert(rate);
2615 
2616     dirtyrate = qdict_get_try_int(rate, "dirty-rate", -1);
2617 
2618     qobject_unref(rsp_return);
2619     return dirtyrate;
2620 }
2621 
2622 static int64_t get_limit_rate(QTestState *who)
2623 {
2624     QDict *rsp_return;
2625     QList *rates;
2626     const QListEntry *entry;
2627     QDict *rate;
2628     int64_t dirtyrate;
2629 
2630     rsp_return = query_vcpu_dirty_limit(who);
2631     g_assert(rsp_return);
2632 
2633     rates = qdict_get_qlist(rsp_return, "return");
2634     g_assert(rates && !qlist_empty(rates));
2635 
2636     entry = qlist_first(rates);
2637     g_assert(entry);
2638 
2639     rate = qobject_to(QDict, qlist_entry_obj(entry));
2640     g_assert(rate);
2641 
2642     dirtyrate = qdict_get_try_int(rate, "limit-rate", -1);
2643 
2644     qobject_unref(rsp_return);
2645     return dirtyrate;
2646 }
2647 
2648 static QTestState *dirtylimit_start_vm(void)
2649 {
2650     QTestState *vm = NULL;
2651     g_autofree gchar *
2652     cmd = g_strdup_printf("-accel kvm,dirty-ring-size=4096 "
2653                           "-name dirtylimit-test,debug-threads=on "
2654                           "-m 150M -smp 1 "
2655                           "-serial file:%s/vm_serial "
2656                           "-drive file=%s,format=raw ",
2657                           tmpfs, bootpath);
2658 
2659     vm = qtest_init(cmd);
2660     return vm;
2661 }
2662 
2663 static void dirtylimit_stop_vm(QTestState *vm)
2664 {
2665     qtest_quit(vm);
2666     cleanup("vm_serial");
2667 }
2668 
2669 static void test_vcpu_dirty_limit(void)
2670 {
2671     QTestState *vm;
2672     int64_t origin_rate;
2673     int64_t quota_rate;
2674     int64_t rate ;
2675     int max_try_count = 20;
2676     int hit = 0;
2677 
2678     /* Start vm for vcpu dirtylimit test */
2679     vm = dirtylimit_start_vm();
2680 
2681     /* Wait for the first serial output from the vm*/
2682     wait_for_serial("vm_serial");
2683 
2684     /* Do dirtyrate measurement with calc time equals 1s */
2685     calc_dirty_rate(vm, 1);
2686 
2687     /* Sleep calc time and wait for calc dirtyrate complete */
2688     wait_for_calc_dirtyrate_complete(vm, 1);
2689 
2690     /* Query original dirty page rate */
2691     origin_rate = get_dirty_rate(vm);
2692 
2693     /* VM booted from bootsect should dirty memory steadily */
2694     assert(origin_rate != 0);
2695 
2696     /* Setup quota dirty page rate at half of origin */
2697     quota_rate = origin_rate / 2;
2698 
2699     /* Set dirtylimit */
2700     dirtylimit_set_all(vm, quota_rate);
2701 
2702     /*
2703      * Check if set-vcpu-dirty-limit and query-vcpu-dirty-limit
2704      * works literally
2705      */
2706     g_assert_cmpint(quota_rate, ==, get_limit_rate(vm));
2707 
2708     /* Sleep a bit to check if it take effect */
2709     usleep(2000000);
2710 
2711     /*
2712      * Check if dirtylimit take effect realistically, set the
2713      * timeout with 20 s(max_try_count * 1s), if dirtylimit
2714      * doesn't take effect, fail test.
2715      */
2716     while (--max_try_count) {
2717         calc_dirty_rate(vm, 1);
2718         wait_for_calc_dirtyrate_complete(vm, 1);
2719         rate = get_dirty_rate(vm);
2720 
2721         /*
2722          * Assume hitting if current rate is less
2723          * than quota rate (within accepting error)
2724          */
2725         if (rate < (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) {
2726             hit = 1;
2727             break;
2728         }
2729     }
2730 
2731     g_assert_cmpint(hit, ==, 1);
2732 
2733     hit = 0;
2734     max_try_count = 20;
2735 
2736     /* Check if dirtylimit cancellation take effect */
2737     cancel_vcpu_dirty_limit(vm);
2738     while (--max_try_count) {
2739         calc_dirty_rate(vm, 1);
2740         wait_for_calc_dirtyrate_complete(vm, 1);
2741         rate = get_dirty_rate(vm);
2742 
2743         /*
2744          * Assume dirtylimit be canceled if current rate is
2745          * greater than quota rate (within accepting error)
2746          */
2747         if (rate > (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) {
2748             hit = 1;
2749             break;
2750         }
2751     }
2752 
2753     g_assert_cmpint(hit, ==, 1);
2754     dirtylimit_stop_vm(vm);
2755 }
2756 
2757 static bool kvm_dirty_ring_supported(void)
2758 {
2759 #if defined(__linux__) && defined(HOST_X86_64)
2760     int ret, kvm_fd = open("/dev/kvm", O_RDONLY);
2761 
2762     if (kvm_fd < 0) {
2763         return false;
2764     }
2765 
2766     ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, KVM_CAP_DIRTY_LOG_RING);
2767     close(kvm_fd);
2768 
2769     /* We test with 4096 slots */
2770     if (ret < 4096) {
2771         return false;
2772     }
2773 
2774     return true;
2775 #else
2776     return false;
2777 #endif
2778 }
2779 
2780 int main(int argc, char **argv)
2781 {
2782     bool has_kvm, has_tcg;
2783     bool has_uffd;
2784     const char *arch;
2785     g_autoptr(GError) err = NULL;
2786     int ret;
2787 
2788     g_test_init(&argc, &argv, NULL);
2789 
2790     has_kvm = qtest_has_accel("kvm");
2791     has_tcg = qtest_has_accel("tcg");
2792 
2793     if (!has_tcg && !has_kvm) {
2794         g_test_skip("No KVM or TCG accelerator available");
2795         return 0;
2796     }
2797 
2798     has_uffd = ufd_version_check();
2799     arch = qtest_get_arch();
2800 
2801     /*
2802      * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
2803      * is touchy due to race conditions on dirty bits (especially on PPC for
2804      * some reason)
2805      */
2806     if (g_str_equal(arch, "ppc64") &&
2807         (!has_kvm || access("/sys/module/kvm_hv", F_OK))) {
2808         g_test_message("Skipping test: kvm_hv not available");
2809         return g_test_run();
2810     }
2811 
2812     /*
2813      * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
2814      * there until the problems are resolved
2815      */
2816     if (g_str_equal(arch, "s390x") && !has_kvm) {
2817         g_test_message("Skipping test: s390x host with KVM is required");
2818         return g_test_run();
2819     }
2820 
2821     tmpfs = g_dir_make_tmp("migration-test-XXXXXX", &err);
2822     if (!tmpfs) {
2823         g_test_message("Can't create temporary directory in %s: %s",
2824                        g_get_tmp_dir(), err->message);
2825     }
2826     g_assert(tmpfs);
2827     bootfile_create(tmpfs);
2828 
2829     module_call_init(MODULE_INIT_QOM);
2830 
2831     if (has_uffd) {
2832         qtest_add_func("/migration/postcopy/plain", test_postcopy);
2833         qtest_add_func("/migration/postcopy/recovery/plain",
2834                        test_postcopy_recovery);
2835         qtest_add_func("/migration/postcopy/preempt/plain", test_postcopy_preempt);
2836         qtest_add_func("/migration/postcopy/preempt/recovery/plain",
2837                        test_postcopy_preempt_recovery);
2838         if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2839             qtest_add_func("/migration/postcopy/compress/plain",
2840                            test_postcopy_compress);
2841             qtest_add_func("/migration/postcopy/recovery/compress/plain",
2842                            test_postcopy_recovery_compress);
2843         }
2844     }
2845 
2846     qtest_add_func("/migration/bad_dest", test_baddest);
2847     qtest_add_func("/migration/precopy/unix/plain", test_precopy_unix_plain);
2848     qtest_add_func("/migration/precopy/unix/xbzrle", test_precopy_unix_xbzrle);
2849     /*
2850      * Compression fails from time to time.
2851      * Put test here but don't enable it until everything is fixed.
2852      */
2853     if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2854         qtest_add_func("/migration/precopy/unix/compress/wait",
2855                        test_precopy_unix_compress);
2856         qtest_add_func("/migration/precopy/unix/compress/nowait",
2857                        test_precopy_unix_compress_nowait);
2858     }
2859 #ifdef CONFIG_GNUTLS
2860     qtest_add_func("/migration/precopy/unix/tls/psk",
2861                    test_precopy_unix_tls_psk);
2862 
2863     if (has_uffd) {
2864         /*
2865          * NOTE: psk test is enough for postcopy, as other types of TLS
2866          * channels are tested under precopy.  Here what we want to test is the
2867          * general postcopy path that has TLS channel enabled.
2868          */
2869         qtest_add_func("/migration/postcopy/tls/psk", test_postcopy_tls_psk);
2870         qtest_add_func("/migration/postcopy/recovery/tls/psk",
2871                        test_postcopy_recovery_tls_psk);
2872         qtest_add_func("/migration/postcopy/preempt/tls/psk",
2873                        test_postcopy_preempt_tls_psk);
2874         qtest_add_func("/migration/postcopy/preempt/recovery/tls/psk",
2875                        test_postcopy_preempt_all);
2876     }
2877 #ifdef CONFIG_TASN1
2878     qtest_add_func("/migration/precopy/unix/tls/x509/default-host",
2879                    test_precopy_unix_tls_x509_default_host);
2880     qtest_add_func("/migration/precopy/unix/tls/x509/override-host",
2881                    test_precopy_unix_tls_x509_override_host);
2882 #endif /* CONFIG_TASN1 */
2883 #endif /* CONFIG_GNUTLS */
2884 
2885     qtest_add_func("/migration/precopy/tcp/plain", test_precopy_tcp_plain);
2886 
2887     qtest_add_func("/migration/precopy/tcp/plain/switchover-ack",
2888                    test_precopy_tcp_switchover_ack);
2889 
2890 #ifdef CONFIG_GNUTLS
2891     qtest_add_func("/migration/precopy/tcp/tls/psk/match",
2892                    test_precopy_tcp_tls_psk_match);
2893     qtest_add_func("/migration/precopy/tcp/tls/psk/mismatch",
2894                    test_precopy_tcp_tls_psk_mismatch);
2895 #ifdef CONFIG_TASN1
2896     qtest_add_func("/migration/precopy/tcp/tls/x509/default-host",
2897                    test_precopy_tcp_tls_x509_default_host);
2898     qtest_add_func("/migration/precopy/tcp/tls/x509/override-host",
2899                    test_precopy_tcp_tls_x509_override_host);
2900     qtest_add_func("/migration/precopy/tcp/tls/x509/mismatch-host",
2901                    test_precopy_tcp_tls_x509_mismatch_host);
2902     qtest_add_func("/migration/precopy/tcp/tls/x509/friendly-client",
2903                    test_precopy_tcp_tls_x509_friendly_client);
2904     qtest_add_func("/migration/precopy/tcp/tls/x509/hostile-client",
2905                    test_precopy_tcp_tls_x509_hostile_client);
2906     qtest_add_func("/migration/precopy/tcp/tls/x509/allow-anon-client",
2907                    test_precopy_tcp_tls_x509_allow_anon_client);
2908     qtest_add_func("/migration/precopy/tcp/tls/x509/reject-anon-client",
2909                    test_precopy_tcp_tls_x509_reject_anon_client);
2910 #endif /* CONFIG_TASN1 */
2911 #endif /* CONFIG_GNUTLS */
2912 
2913     /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
2914 #ifndef _WIN32
2915     qtest_add_func("/migration/fd_proto", test_migrate_fd_proto);
2916 #endif
2917     qtest_add_func("/migration/validate_uuid", test_validate_uuid);
2918     qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error);
2919     qtest_add_func("/migration/validate_uuid_src_not_set",
2920                    test_validate_uuid_src_not_set);
2921     qtest_add_func("/migration/validate_uuid_dst_not_set",
2922                    test_validate_uuid_dst_not_set);
2923     /*
2924      * See explanation why this test is slow on function definition
2925      */
2926     if (g_test_slow()) {
2927         qtest_add_func("/migration/auto_converge", test_migrate_auto_converge);
2928     }
2929     qtest_add_func("/migration/multifd/tcp/plain/none",
2930                    test_multifd_tcp_none);
2931     /*
2932      * This test is flaky and sometimes fails in CI and otherwise:
2933      * don't run unless user opts in via environment variable.
2934      */
2935     if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2936         qtest_add_func("/migration/multifd/tcp/plain/cancel",
2937                        test_multifd_tcp_cancel);
2938     }
2939     qtest_add_func("/migration/multifd/tcp/plain/zlib",
2940                    test_multifd_tcp_zlib);
2941 #ifdef CONFIG_ZSTD
2942     qtest_add_func("/migration/multifd/tcp/plain/zstd",
2943                    test_multifd_tcp_zstd);
2944 #endif
2945 #ifdef CONFIG_GNUTLS
2946     qtest_add_func("/migration/multifd/tcp/tls/psk/match",
2947                    test_multifd_tcp_tls_psk_match);
2948     qtest_add_func("/migration/multifd/tcp/tls/psk/mismatch",
2949                    test_multifd_tcp_tls_psk_mismatch);
2950 #ifdef CONFIG_TASN1
2951     qtest_add_func("/migration/multifd/tcp/tls/x509/default-host",
2952                    test_multifd_tcp_tls_x509_default_host);
2953     qtest_add_func("/migration/multifd/tcp/tls/x509/override-host",
2954                    test_multifd_tcp_tls_x509_override_host);
2955     qtest_add_func("/migration/multifd/tcp/tls/x509/mismatch-host",
2956                    test_multifd_tcp_tls_x509_mismatch_host);
2957     qtest_add_func("/migration/multifd/tcp/tls/x509/allow-anon-client",
2958                    test_multifd_tcp_tls_x509_allow_anon_client);
2959     qtest_add_func("/migration/multifd/tcp/tls/x509/reject-anon-client",
2960                    test_multifd_tcp_tls_x509_reject_anon_client);
2961 #endif /* CONFIG_TASN1 */
2962 #endif /* CONFIG_GNUTLS */
2963 
2964     if (g_str_equal(arch, "x86_64") && has_kvm && kvm_dirty_ring_supported()) {
2965         qtest_add_func("/migration/dirty_ring",
2966                        test_precopy_unix_dirty_ring);
2967         qtest_add_func("/migration/vcpu_dirty_limit",
2968                        test_vcpu_dirty_limit);
2969     }
2970 
2971     ret = g_test_run();
2972 
2973     g_assert_cmpint(ret, ==, 0);
2974 
2975     bootfile_delete();
2976     ret = rmdir(tmpfs);
2977     if (ret != 0) {
2978         g_test_message("unable to rmdir: path (%s): %s",
2979                        tmpfs, strerror(errno));
2980     }
2981     g_free(tmpfs);
2982 
2983     return ret;
2984 }
2985