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