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