xref: /qemu/tests/qtest/migration-test.c (revision 7a21bee2)
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 <linux/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 = syscall(__NR_userfaultfd, 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 const 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 void migrate_ensure_non_converge(QTestState *who)
410 {
411     /* Can't converge with 1ms downtime + 30 mbs bandwidth limit */
412     migrate_set_parameter_int(who, "max-bandwidth", 30 * 1000 * 1000);
413     migrate_set_parameter_int(who, "downtime-limit", 1);
414 }
415 
416 static void migrate_ensure_converge(QTestState *who)
417 {
418     /* Should converge with 30s downtime + 1 gbs bandwidth limit */
419     migrate_set_parameter_int(who, "max-bandwidth", 1 * 1000 * 1000 * 1000);
420     migrate_set_parameter_int(who, "downtime-limit", 30 * 1000);
421 }
422 
423 static void migrate_pause(QTestState *who)
424 {
425     QDict *rsp;
426 
427     rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
428     qobject_unref(rsp);
429 }
430 
431 static void migrate_continue(QTestState *who, const char *state)
432 {
433     QDict *rsp;
434 
435     rsp = wait_command(who,
436                        "{ 'execute': 'migrate-continue',"
437                        "  'arguments': { 'state': %s } }",
438                        state);
439     qobject_unref(rsp);
440 }
441 
442 static void migrate_recover(QTestState *who, const char *uri)
443 {
444     QDict *rsp;
445 
446     rsp = wait_command(who,
447                        "{ 'execute': 'migrate-recover', "
448                        "  'id': 'recover-cmd', "
449                        "  'arguments': { 'uri': %s } }",
450                        uri);
451     qobject_unref(rsp);
452 }
453 
454 static void migrate_cancel(QTestState *who)
455 {
456     QDict *rsp;
457 
458     rsp = wait_command(who, "{ 'execute': 'migrate_cancel' }");
459     qobject_unref(rsp);
460 }
461 
462 static void migrate_set_capability(QTestState *who, const char *capability,
463                                    bool value)
464 {
465     QDict *rsp;
466 
467     rsp = qtest_qmp(who,
468                     "{ 'execute': 'migrate-set-capabilities',"
469                     "'arguments': { "
470                     "'capabilities': [ { "
471                     "'capability': %s, 'state': %i } ] } }",
472                     capability, value);
473     g_assert(qdict_haskey(rsp, "return"));
474     qobject_unref(rsp);
475 }
476 
477 static void migrate_postcopy_start(QTestState *from, QTestState *to)
478 {
479     QDict *rsp;
480 
481     rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
482     qobject_unref(rsp);
483 
484     if (!got_stop) {
485         qtest_qmp_eventwait(from, "STOP");
486     }
487 
488     qtest_qmp_eventwait(to, "RESUME");
489 }
490 
491 typedef struct {
492     /*
493      * QTEST_LOG=1 may override this.  When QTEST_LOG=1, we always dump errors
494      * unconditionally, because it means the user would like to be verbose.
495      */
496     bool hide_stderr;
497     bool use_shmem;
498     /* only launch the target process */
499     bool only_target;
500     /* Use dirty ring if true; dirty logging otherwise */
501     bool use_dirty_ring;
502     const char *opts_source;
503     const char *opts_target;
504 } MigrateStart;
505 
506 /*
507  * A hook that runs after the src and dst QEMUs have been
508  * created, but before the migration is started. This can
509  * be used to set migration parameters and capabilities.
510  *
511  * Returns: NULL, or a pointer to opaque state to be
512  *          later passed to the TestMigrateFinishHook
513  */
514 typedef void * (*TestMigrateStartHook)(QTestState *from,
515                                        QTestState *to);
516 
517 /*
518  * A hook that runs after the migration has finished,
519  * regardless of whether it succeeded or failed, but
520  * before QEMU has terminated (unless it self-terminated
521  * due to migration error)
522  *
523  * @opaque is a pointer to state previously returned
524  * by the TestMigrateStartHook if any, or NULL.
525  */
526 typedef void (*TestMigrateFinishHook)(QTestState *from,
527                                       QTestState *to,
528                                       void *opaque);
529 
530 typedef struct {
531     /* Optional: fine tune start parameters */
532     MigrateStart start;
533 
534     /* Required: the URI for the dst QEMU to listen on */
535     const char *listen_uri;
536 
537     /*
538      * Optional: the URI for the src QEMU to connect to
539      * If NULL, then it will query the dst QEMU for its actual
540      * listening address and use that as the connect address.
541      * This allows for dynamically picking a free TCP port.
542      */
543     const char *connect_uri;
544 
545     /* Optional: callback to run at start to set migration parameters */
546     TestMigrateStartHook start_hook;
547     /* Optional: callback to run at finish to cleanup */
548     TestMigrateFinishHook finish_hook;
549 
550     /*
551      * Optional: normally we expect the migration process to complete.
552      *
553      * There can be a variety of reasons and stages in which failure
554      * can happen during tests.
555      *
556      * If a failure is expected to happen at time of establishing
557      * the connection, then MIG_TEST_FAIL will indicate that the dst
558      * QEMU is expected to stay running and accept future migration
559      * connections.
560      *
561      * If a failure is expected to happen while processing the
562      * migration stream, then MIG_TEST_FAIL_DEST_QUIT_ERR will indicate
563      * that the dst QEMU is expected to quit with non-zero exit status
564      */
565     enum {
566         /* This test should succeed, the default */
567         MIG_TEST_SUCCEED = 0,
568         /* This test should fail, dest qemu should keep alive */
569         MIG_TEST_FAIL,
570         /* This test should fail, dest qemu should fail with abnormal status */
571         MIG_TEST_FAIL_DEST_QUIT_ERR,
572     } result;
573 
574     /* Optional: set number of migration passes to wait for */
575     unsigned int iterations;
576 
577     /* Postcopy specific fields */
578     void *postcopy_data;
579     bool postcopy_preempt;
580 } MigrateCommon;
581 
582 static int test_migrate_start(QTestState **from, QTestState **to,
583                               const char *uri, MigrateStart *args)
584 {
585     g_autofree gchar *arch_source = NULL;
586     g_autofree gchar *arch_target = NULL;
587     g_autofree gchar *cmd_source = NULL;
588     g_autofree gchar *cmd_target = NULL;
589     const gchar *ignore_stderr;
590     g_autofree char *bootpath = NULL;
591     g_autofree char *shmem_opts = NULL;
592     g_autofree char *shmem_path = NULL;
593     const char *arch = qtest_get_arch();
594     const char *machine_opts = NULL;
595     const char *memory_size;
596 
597     if (args->use_shmem) {
598         if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
599             g_test_skip("/dev/shm is not supported");
600             return -1;
601         }
602     }
603 
604     got_stop = false;
605     bootpath = g_strdup_printf("%s/bootsect", tmpfs);
606     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
607         /* the assembled x86 boot sector should be exactly one sector large */
608         assert(sizeof(x86_bootsect) == 512);
609         init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect));
610         memory_size = "150M";
611         arch_source = g_strdup_printf("-drive file=%s,format=raw", bootpath);
612         arch_target = g_strdup(arch_source);
613         start_address = X86_TEST_MEM_START;
614         end_address = X86_TEST_MEM_END;
615     } else if (g_str_equal(arch, "s390x")) {
616         init_bootfile(bootpath, s390x_elf, sizeof(s390x_elf));
617         memory_size = "128M";
618         arch_source = g_strdup_printf("-bios %s", bootpath);
619         arch_target = g_strdup(arch_source);
620         start_address = S390_TEST_MEM_START;
621         end_address = S390_TEST_MEM_END;
622     } else if (strcmp(arch, "ppc64") == 0) {
623         machine_opts = "vsmt=8";
624         memory_size = "256M";
625         start_address = PPC_TEST_MEM_START;
626         end_address = PPC_TEST_MEM_END;
627         arch_source = g_strdup_printf("-nodefaults "
628                                       "-prom-env 'use-nvramrc?=true' -prom-env "
629                                       "'nvramrc=hex .\" _\" begin %x %x "
630                                       "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
631                                       "until'", end_address, start_address);
632         arch_target = g_strdup("");
633     } else if (strcmp(arch, "aarch64") == 0) {
634         init_bootfile(bootpath, aarch64_kernel, sizeof(aarch64_kernel));
635         machine_opts = "virt,gic-version=max";
636         memory_size = "150M";
637         arch_source = g_strdup_printf("-cpu max "
638                                       "-kernel %s",
639                                       bootpath);
640         arch_target = g_strdup(arch_source);
641         start_address = ARM_TEST_MEM_START;
642         end_address = ARM_TEST_MEM_END;
643 
644         g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
645     } else {
646         g_assert_not_reached();
647     }
648 
649     if (!getenv("QTEST_LOG") && args->hide_stderr) {
650         ignore_stderr = "2>/dev/null";
651     } else {
652         ignore_stderr = "";
653     }
654 
655     if (args->use_shmem) {
656         shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
657         shmem_opts = g_strdup_printf(
658             "-object memory-backend-file,id=mem0,size=%s"
659             ",mem-path=%s,share=on -numa node,memdev=mem0",
660             memory_size, shmem_path);
661     } else {
662         shmem_path = NULL;
663         shmem_opts = g_strdup("");
664     }
665 
666     cmd_source = g_strdup_printf("-accel kvm%s -accel tcg%s%s "
667                                  "-name source,debug-threads=on "
668                                  "-m %s "
669                                  "-serial file:%s/src_serial "
670                                  "%s %s %s %s",
671                                  args->use_dirty_ring ?
672                                  ",dirty-ring-size=4096" : "",
673                                  machine_opts ? " -machine " : "",
674                                  machine_opts ? machine_opts : "",
675                                  memory_size, tmpfs,
676                                  arch_source, shmem_opts,
677                                  args->opts_source ? args->opts_source : "",
678                                  ignore_stderr);
679     if (!args->only_target) {
680         *from = qtest_init(cmd_source);
681     }
682 
683     cmd_target = g_strdup_printf("-accel kvm%s -accel tcg%s%s "
684                                  "-name target,debug-threads=on "
685                                  "-m %s "
686                                  "-serial file:%s/dest_serial "
687                                  "-incoming %s "
688                                  "%s %s %s %s",
689                                  args->use_dirty_ring ?
690                                  ",dirty-ring-size=4096" : "",
691                                  machine_opts ? " -machine " : "",
692                                  machine_opts ? machine_opts : "",
693                                  memory_size, tmpfs, uri,
694                                  arch_target, shmem_opts,
695                                  args->opts_target ? args->opts_target : "",
696                                  ignore_stderr);
697     *to = qtest_init(cmd_target);
698 
699     /*
700      * Remove shmem file immediately to avoid memory leak in test failed case.
701      * It's valid becase QEMU has already opened this file
702      */
703     if (args->use_shmem) {
704         unlink(shmem_path);
705     }
706 
707     return 0;
708 }
709 
710 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
711 {
712     unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
713 
714     qtest_quit(from);
715 
716     if (test_dest) {
717         qtest_memread(to, start_address, &dest_byte_a, 1);
718 
719         /* Destination still running, wait for a byte to change */
720         do {
721             qtest_memread(to, start_address, &dest_byte_b, 1);
722             usleep(1000 * 10);
723         } while (dest_byte_a == dest_byte_b);
724 
725         qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
726 
727         /* With it stopped, check nothing changes */
728         qtest_memread(to, start_address, &dest_byte_c, 1);
729         usleep(1000 * 200);
730         qtest_memread(to, start_address, &dest_byte_d, 1);
731         g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
732 
733         check_guests_ram(to);
734     }
735 
736     qtest_quit(to);
737 
738     cleanup("bootsect");
739     cleanup("migsocket");
740     cleanup("src_serial");
741     cleanup("dest_serial");
742 }
743 
744 #ifdef CONFIG_GNUTLS
745 struct TestMigrateTLSPSKData {
746     char *workdir;
747     char *workdiralt;
748     char *pskfile;
749     char *pskfilealt;
750 };
751 
752 static void *
753 test_migrate_tls_psk_start_common(QTestState *from,
754                                   QTestState *to,
755                                   bool mismatch)
756 {
757     struct TestMigrateTLSPSKData *data =
758         g_new0(struct TestMigrateTLSPSKData, 1);
759     QDict *rsp;
760 
761     data->workdir = g_strdup_printf("%s/tlscredspsk0", tmpfs);
762     data->pskfile = g_strdup_printf("%s/%s", data->workdir,
763                                     QCRYPTO_TLS_CREDS_PSKFILE);
764     mkdir(data->workdir, 0700);
765     test_tls_psk_init(data->pskfile);
766 
767     if (mismatch) {
768         data->workdiralt = g_strdup_printf("%s/tlscredspskalt0", tmpfs);
769         data->pskfilealt = g_strdup_printf("%s/%s", data->workdiralt,
770                                            QCRYPTO_TLS_CREDS_PSKFILE);
771         mkdir(data->workdiralt, 0700);
772         test_tls_psk_init_alt(data->pskfilealt);
773     }
774 
775     rsp = wait_command(from,
776                        "{ 'execute': 'object-add',"
777                        "  'arguments': { 'qom-type': 'tls-creds-psk',"
778                        "                 'id': 'tlscredspsk0',"
779                        "                 'endpoint': 'client',"
780                        "                 'dir': %s,"
781                        "                 'username': 'qemu'} }",
782                        data->workdir);
783     qobject_unref(rsp);
784 
785     rsp = wait_command(to,
786                        "{ 'execute': 'object-add',"
787                        "  'arguments': { 'qom-type': 'tls-creds-psk',"
788                        "                 'id': 'tlscredspsk0',"
789                        "                 'endpoint': 'server',"
790                        "                 'dir': %s } }",
791                        mismatch ? data->workdiralt : data->workdir);
792     qobject_unref(rsp);
793 
794     migrate_set_parameter_str(from, "tls-creds", "tlscredspsk0");
795     migrate_set_parameter_str(to, "tls-creds", "tlscredspsk0");
796 
797     return data;
798 }
799 
800 static void *
801 test_migrate_tls_psk_start_match(QTestState *from,
802                                  QTestState *to)
803 {
804     return test_migrate_tls_psk_start_common(from, to, false);
805 }
806 
807 static void *
808 test_migrate_tls_psk_start_mismatch(QTestState *from,
809                                     QTestState *to)
810 {
811     return test_migrate_tls_psk_start_common(from, to, true);
812 }
813 
814 static void
815 test_migrate_tls_psk_finish(QTestState *from,
816                             QTestState *to,
817                             void *opaque)
818 {
819     struct TestMigrateTLSPSKData *data = opaque;
820 
821     test_tls_psk_cleanup(data->pskfile);
822     if (data->pskfilealt) {
823         test_tls_psk_cleanup(data->pskfilealt);
824     }
825     rmdir(data->workdir);
826     if (data->workdiralt) {
827         rmdir(data->workdiralt);
828     }
829 
830     g_free(data->workdiralt);
831     g_free(data->pskfilealt);
832     g_free(data->workdir);
833     g_free(data->pskfile);
834     g_free(data);
835 }
836 
837 #ifdef CONFIG_TASN1
838 typedef struct {
839     char *workdir;
840     char *keyfile;
841     char *cacert;
842     char *servercert;
843     char *serverkey;
844     char *clientcert;
845     char *clientkey;
846 } TestMigrateTLSX509Data;
847 
848 typedef struct {
849     bool verifyclient;
850     bool clientcert;
851     bool hostileclient;
852     bool authzclient;
853     const char *certhostname;
854     const char *certipaddr;
855 } TestMigrateTLSX509;
856 
857 static void *
858 test_migrate_tls_x509_start_common(QTestState *from,
859                                    QTestState *to,
860                                    TestMigrateTLSX509 *args)
861 {
862     TestMigrateTLSX509Data *data = g_new0(TestMigrateTLSX509Data, 1);
863     QDict *rsp;
864 
865     data->workdir = g_strdup_printf("%s/tlscredsx5090", tmpfs);
866     data->keyfile = g_strdup_printf("%s/key.pem", data->workdir);
867 
868     data->cacert = g_strdup_printf("%s/ca-cert.pem", data->workdir);
869     data->serverkey = g_strdup_printf("%s/server-key.pem", data->workdir);
870     data->servercert = g_strdup_printf("%s/server-cert.pem", data->workdir);
871     if (args->clientcert) {
872         data->clientkey = g_strdup_printf("%s/client-key.pem", data->workdir);
873         data->clientcert = g_strdup_printf("%s/client-cert.pem", data->workdir);
874     }
875 
876     mkdir(data->workdir, 0700);
877 
878     test_tls_init(data->keyfile);
879     g_assert(link(data->keyfile, data->serverkey) == 0);
880     if (args->clientcert) {
881         g_assert(link(data->keyfile, data->clientkey) == 0);
882     }
883 
884     TLS_ROOT_REQ_SIMPLE(cacertreq, data->cacert);
885     if (args->clientcert) {
886         TLS_CERT_REQ_SIMPLE_CLIENT(servercertreq, cacertreq,
887                                    args->hostileclient ?
888                                    QCRYPTO_TLS_TEST_CLIENT_HOSTILE_NAME :
889                                    QCRYPTO_TLS_TEST_CLIENT_NAME,
890                                    data->clientcert);
891     }
892 
893     TLS_CERT_REQ_SIMPLE_SERVER(clientcertreq, cacertreq,
894                                data->servercert,
895                                args->certhostname,
896                                args->certipaddr);
897 
898     rsp = wait_command(from,
899                        "{ 'execute': 'object-add',"
900                        "  'arguments': { 'qom-type': 'tls-creds-x509',"
901                        "                 'id': 'tlscredsx509client0',"
902                        "                 'endpoint': 'client',"
903                        "                 'dir': %s,"
904                        "                 'sanity-check': true,"
905                        "                 'verify-peer': true} }",
906                        data->workdir);
907     qobject_unref(rsp);
908     migrate_set_parameter_str(from, "tls-creds", "tlscredsx509client0");
909     if (args->certhostname) {
910         migrate_set_parameter_str(from, "tls-hostname", args->certhostname);
911     }
912 
913     rsp = wait_command(to,
914                        "{ 'execute': 'object-add',"
915                        "  'arguments': { 'qom-type': 'tls-creds-x509',"
916                        "                 'id': 'tlscredsx509server0',"
917                        "                 'endpoint': 'server',"
918                        "                 'dir': %s,"
919                        "                 'sanity-check': true,"
920                        "                 'verify-peer': %i} }",
921                        data->workdir, args->verifyclient);
922     qobject_unref(rsp);
923     migrate_set_parameter_str(to, "tls-creds", "tlscredsx509server0");
924 
925     if (args->authzclient) {
926         rsp = wait_command(to,
927                            "{ 'execute': 'object-add',"
928                            "  'arguments': { 'qom-type': 'authz-simple',"
929                            "                 'id': 'tlsauthz0',"
930                            "                 'identity': %s} }",
931                            "CN=" QCRYPTO_TLS_TEST_CLIENT_NAME);
932         migrate_set_parameter_str(to, "tls-authz", "tlsauthz0");
933     }
934 
935     return data;
936 }
937 
938 /*
939  * The normal case: match server's cert hostname against
940  * whatever host we were telling QEMU to connect to (if any)
941  */
942 static void *
943 test_migrate_tls_x509_start_default_host(QTestState *from,
944                                          QTestState *to)
945 {
946     TestMigrateTLSX509 args = {
947         .verifyclient = true,
948         .clientcert = true,
949         .certipaddr = "127.0.0.1"
950     };
951     return test_migrate_tls_x509_start_common(from, to, &args);
952 }
953 
954 /*
955  * The unusual case: the server's cert is different from
956  * the address we're telling QEMU to connect to (if any),
957  * so we must give QEMU an explicit hostname to validate
958  */
959 static void *
960 test_migrate_tls_x509_start_override_host(QTestState *from,
961                                           QTestState *to)
962 {
963     TestMigrateTLSX509 args = {
964         .verifyclient = true,
965         .clientcert = true,
966         .certhostname = "qemu.org",
967     };
968     return test_migrate_tls_x509_start_common(from, to, &args);
969 }
970 
971 /*
972  * The unusual case: the server's cert is different from
973  * the address we're telling QEMU to connect to, and so we
974  * expect the client to reject the server
975  */
976 static void *
977 test_migrate_tls_x509_start_mismatch_host(QTestState *from,
978                                           QTestState *to)
979 {
980     TestMigrateTLSX509 args = {
981         .verifyclient = true,
982         .clientcert = true,
983         .certipaddr = "10.0.0.1",
984     };
985     return test_migrate_tls_x509_start_common(from, to, &args);
986 }
987 
988 static void *
989 test_migrate_tls_x509_start_friendly_client(QTestState *from,
990                                             QTestState *to)
991 {
992     TestMigrateTLSX509 args = {
993         .verifyclient = true,
994         .clientcert = true,
995         .authzclient = true,
996         .certipaddr = "127.0.0.1",
997     };
998     return test_migrate_tls_x509_start_common(from, to, &args);
999 }
1000 
1001 static void *
1002 test_migrate_tls_x509_start_hostile_client(QTestState *from,
1003                                            QTestState *to)
1004 {
1005     TestMigrateTLSX509 args = {
1006         .verifyclient = true,
1007         .clientcert = true,
1008         .hostileclient = true,
1009         .authzclient = true,
1010         .certipaddr = "127.0.0.1",
1011     };
1012     return test_migrate_tls_x509_start_common(from, to, &args);
1013 }
1014 
1015 /*
1016  * The case with no client certificate presented,
1017  * and no server verification
1018  */
1019 static void *
1020 test_migrate_tls_x509_start_allow_anon_client(QTestState *from,
1021                                               QTestState *to)
1022 {
1023     TestMigrateTLSX509 args = {
1024         .certipaddr = "127.0.0.1",
1025     };
1026     return test_migrate_tls_x509_start_common(from, to, &args);
1027 }
1028 
1029 /*
1030  * The case with no client certificate presented,
1031  * and server verification rejecting
1032  */
1033 static void *
1034 test_migrate_tls_x509_start_reject_anon_client(QTestState *from,
1035                                                QTestState *to)
1036 {
1037     TestMigrateTLSX509 args = {
1038         .verifyclient = true,
1039         .certipaddr = "127.0.0.1",
1040     };
1041     return test_migrate_tls_x509_start_common(from, to, &args);
1042 }
1043 
1044 static void
1045 test_migrate_tls_x509_finish(QTestState *from,
1046                              QTestState *to,
1047                              void *opaque)
1048 {
1049     TestMigrateTLSX509Data *data = opaque;
1050 
1051     test_tls_cleanup(data->keyfile);
1052     unlink(data->cacert);
1053     unlink(data->servercert);
1054     unlink(data->serverkey);
1055     unlink(data->clientcert);
1056     unlink(data->clientkey);
1057     rmdir(data->workdir);
1058 
1059     g_free(data->workdir);
1060     g_free(data->keyfile);
1061     g_free(data);
1062 }
1063 #endif /* CONFIG_TASN1 */
1064 #endif /* CONFIG_GNUTLS */
1065 
1066 static int migrate_postcopy_prepare(QTestState **from_ptr,
1067                                     QTestState **to_ptr,
1068                                     MigrateCommon *args)
1069 {
1070     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1071     QTestState *from, *to;
1072 
1073     if (test_migrate_start(&from, &to, uri, &args->start)) {
1074         return -1;
1075     }
1076 
1077     if (args->start_hook) {
1078         args->postcopy_data = args->start_hook(from, to);
1079     }
1080 
1081     migrate_set_capability(from, "postcopy-ram", true);
1082     migrate_set_capability(to, "postcopy-ram", true);
1083     migrate_set_capability(to, "postcopy-blocktime", true);
1084 
1085     if (args->postcopy_preempt) {
1086         migrate_set_capability(from, "postcopy-preempt", true);
1087         migrate_set_capability(to, "postcopy-preempt", true);
1088     }
1089 
1090     migrate_ensure_non_converge(from);
1091 
1092     /* Wait for the first serial output from the source */
1093     wait_for_serial("src_serial");
1094 
1095     migrate_qmp(from, uri, "{}");
1096 
1097     wait_for_migration_pass(from);
1098 
1099     *from_ptr = from;
1100     *to_ptr = to;
1101 
1102     return 0;
1103 }
1104 
1105 static void migrate_postcopy_complete(QTestState *from, QTestState *to,
1106                                       MigrateCommon *args)
1107 {
1108     wait_for_migration_complete(from);
1109 
1110     /* Make sure we get at least one "B" on destination */
1111     wait_for_serial("dest_serial");
1112 
1113     if (uffd_feature_thread_id) {
1114         read_blocktime(to);
1115     }
1116 
1117     if (args->finish_hook) {
1118         args->finish_hook(from, to, args->postcopy_data);
1119         args->postcopy_data = NULL;
1120     }
1121 
1122     test_migrate_end(from, to, true);
1123 }
1124 
1125 static void test_postcopy_common(MigrateCommon *args)
1126 {
1127     QTestState *from, *to;
1128 
1129     if (migrate_postcopy_prepare(&from, &to, args)) {
1130         return;
1131     }
1132     migrate_postcopy_start(from, to);
1133     migrate_postcopy_complete(from, to, args);
1134 }
1135 
1136 static void test_postcopy(void)
1137 {
1138     MigrateCommon args = { };
1139 
1140     test_postcopy_common(&args);
1141 }
1142 
1143 static void test_postcopy_preempt(void)
1144 {
1145     MigrateCommon args = {
1146         .postcopy_preempt = true,
1147     };
1148 
1149     test_postcopy_common(&args);
1150 }
1151 
1152 #ifdef CONFIG_GNUTLS
1153 static void test_postcopy_tls_psk(void)
1154 {
1155     MigrateCommon args = {
1156         .start_hook = test_migrate_tls_psk_start_match,
1157         .finish_hook = test_migrate_tls_psk_finish,
1158     };
1159 
1160     test_postcopy_common(&args);
1161 }
1162 
1163 static void test_postcopy_preempt_tls_psk(void)
1164 {
1165     MigrateCommon args = {
1166         .postcopy_preempt = true,
1167         .start_hook = test_migrate_tls_psk_start_match,
1168         .finish_hook = test_migrate_tls_psk_finish,
1169     };
1170 
1171     test_postcopy_common(&args);
1172 }
1173 #endif
1174 
1175 static void test_postcopy_recovery_common(MigrateCommon *args)
1176 {
1177     QTestState *from, *to;
1178     g_autofree char *uri = NULL;
1179 
1180     /* Always hide errors for postcopy recover tests since they're expected */
1181     args->start.hide_stderr = true;
1182 
1183     if (migrate_postcopy_prepare(&from, &to, args)) {
1184         return;
1185     }
1186 
1187     /* Turn postcopy speed down, 4K/s is slow enough on any machines */
1188     migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
1189 
1190     /* Now we start the postcopy */
1191     migrate_postcopy_start(from, to);
1192 
1193     /*
1194      * Wait until postcopy is really started; we can only run the
1195      * migrate-pause command during a postcopy
1196      */
1197     wait_for_migration_status(from, "postcopy-active", NULL);
1198 
1199     /*
1200      * Manually stop the postcopy migration. This emulates a network
1201      * failure with the migration socket
1202      */
1203     migrate_pause(from);
1204 
1205     /*
1206      * Wait for destination side to reach postcopy-paused state.  The
1207      * migrate-recover command can only succeed if destination machine
1208      * is in the paused state
1209      */
1210     wait_for_migration_status(to, "postcopy-paused",
1211                               (const char * []) { "failed", "active",
1212                                                   "completed", NULL });
1213 
1214     /*
1215      * Create a new socket to emulate a new channel that is different
1216      * from the broken migration channel; tell the destination to
1217      * listen to the new port
1218      */
1219     uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
1220     migrate_recover(to, uri);
1221 
1222     /*
1223      * Try to rebuild the migration channel using the resume flag and
1224      * the newly created channel
1225      */
1226     wait_for_migration_status(from, "postcopy-paused",
1227                               (const char * []) { "failed", "active",
1228                                                   "completed", NULL });
1229     migrate_qmp(from, uri, "{'resume': true}");
1230 
1231     /* Restore the postcopy bandwidth to unlimited */
1232     migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
1233 
1234     migrate_postcopy_complete(from, to, args);
1235 }
1236 
1237 static void test_postcopy_recovery(void)
1238 {
1239     MigrateCommon args = { };
1240 
1241     test_postcopy_recovery_common(&args);
1242 }
1243 
1244 #ifdef CONFIG_GNUTLS
1245 static void test_postcopy_recovery_tls_psk(void)
1246 {
1247     MigrateCommon args = {
1248         .start_hook = test_migrate_tls_psk_start_match,
1249         .finish_hook = test_migrate_tls_psk_finish,
1250     };
1251 
1252     test_postcopy_recovery_common(&args);
1253 }
1254 #endif
1255 
1256 static void test_postcopy_preempt_recovery(void)
1257 {
1258     MigrateCommon args = {
1259         .postcopy_preempt = true,
1260     };
1261 
1262     test_postcopy_recovery_common(&args);
1263 }
1264 
1265 #ifdef CONFIG_GNUTLS
1266 /* This contains preempt+recovery+tls test altogether */
1267 static void test_postcopy_preempt_all(void)
1268 {
1269     MigrateCommon args = {
1270         .postcopy_preempt = true,
1271         .start_hook = test_migrate_tls_psk_start_match,
1272         .finish_hook = test_migrate_tls_psk_finish,
1273     };
1274 
1275     test_postcopy_recovery_common(&args);
1276 }
1277 #endif
1278 
1279 static void test_baddest(void)
1280 {
1281     MigrateStart args = {
1282         .hide_stderr = true
1283     };
1284     QTestState *from, *to;
1285 
1286     if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", &args)) {
1287         return;
1288     }
1289     migrate_qmp(from, "tcp:127.0.0.1:0", "{}");
1290     wait_for_migration_fail(from, false);
1291     test_migrate_end(from, to, false);
1292 }
1293 
1294 static void test_precopy_common(MigrateCommon *args)
1295 {
1296     QTestState *from, *to;
1297     void *data_hook = NULL;
1298 
1299     if (test_migrate_start(&from, &to, args->listen_uri, &args->start)) {
1300         return;
1301     }
1302 
1303     migrate_ensure_non_converge(from);
1304 
1305     if (args->start_hook) {
1306         data_hook = args->start_hook(from, to);
1307     }
1308 
1309     /* Wait for the first serial output from the source */
1310     wait_for_serial("src_serial");
1311 
1312     if (!args->connect_uri) {
1313         g_autofree char *local_connect_uri =
1314             migrate_get_socket_address(to, "socket-address");
1315         migrate_qmp(from, local_connect_uri, "{}");
1316     } else {
1317         migrate_qmp(from, args->connect_uri, "{}");
1318     }
1319 
1320 
1321     if (args->result != MIG_TEST_SUCCEED) {
1322         bool allow_active = args->result == MIG_TEST_FAIL;
1323         wait_for_migration_fail(from, allow_active);
1324 
1325         if (args->result == MIG_TEST_FAIL_DEST_QUIT_ERR) {
1326             qtest_set_expected_status(to, 1);
1327         }
1328     } else {
1329         if (args->iterations) {
1330             while (args->iterations--) {
1331                 wait_for_migration_pass(from);
1332             }
1333         } else {
1334             wait_for_migration_pass(from);
1335         }
1336 
1337         migrate_ensure_converge(from);
1338 
1339         /* We do this first, as it has a timeout to stop us
1340          * hanging forever if migration didn't converge */
1341         wait_for_migration_complete(from);
1342 
1343         if (!got_stop) {
1344             qtest_qmp_eventwait(from, "STOP");
1345         }
1346 
1347         qtest_qmp_eventwait(to, "RESUME");
1348 
1349         wait_for_serial("dest_serial");
1350     }
1351 
1352     if (args->finish_hook) {
1353         args->finish_hook(from, to, data_hook);
1354     }
1355 
1356     test_migrate_end(from, to, args->result == MIG_TEST_SUCCEED);
1357 }
1358 
1359 static void test_precopy_unix_plain(void)
1360 {
1361     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1362     MigrateCommon args = {
1363         .listen_uri = uri,
1364         .connect_uri = uri,
1365     };
1366 
1367     test_precopy_common(&args);
1368 }
1369 
1370 
1371 static void test_precopy_unix_dirty_ring(void)
1372 {
1373     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1374     MigrateCommon args = {
1375         .start = {
1376             .use_dirty_ring = true,
1377         },
1378         .listen_uri = uri,
1379         .connect_uri = uri,
1380     };
1381 
1382     test_precopy_common(&args);
1383 }
1384 
1385 #ifdef CONFIG_GNUTLS
1386 static void test_precopy_unix_tls_psk(void)
1387 {
1388     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1389     MigrateCommon args = {
1390         .connect_uri = uri,
1391         .listen_uri = uri,
1392         .start_hook = test_migrate_tls_psk_start_match,
1393         .finish_hook = test_migrate_tls_psk_finish,
1394     };
1395 
1396     test_precopy_common(&args);
1397 }
1398 
1399 #ifdef CONFIG_TASN1
1400 static void test_precopy_unix_tls_x509_default_host(void)
1401 {
1402     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1403     MigrateCommon args = {
1404         .start = {
1405             .hide_stderr = true,
1406         },
1407         .connect_uri = uri,
1408         .listen_uri = uri,
1409         .start_hook = test_migrate_tls_x509_start_default_host,
1410         .finish_hook = test_migrate_tls_x509_finish,
1411         .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
1412     };
1413 
1414     test_precopy_common(&args);
1415 }
1416 
1417 static void test_precopy_unix_tls_x509_override_host(void)
1418 {
1419     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1420     MigrateCommon args = {
1421         .connect_uri = uri,
1422         .listen_uri = uri,
1423         .start_hook = test_migrate_tls_x509_start_override_host,
1424         .finish_hook = test_migrate_tls_x509_finish,
1425     };
1426 
1427     test_precopy_common(&args);
1428 }
1429 #endif /* CONFIG_TASN1 */
1430 #endif /* CONFIG_GNUTLS */
1431 
1432 #if 0
1433 /* Currently upset on aarch64 TCG */
1434 static void test_ignore_shared(void)
1435 {
1436     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1437     QTestState *from, *to;
1438 
1439     if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) {
1440         return;
1441     }
1442 
1443     migrate_set_capability(from, "x-ignore-shared", true);
1444     migrate_set_capability(to, "x-ignore-shared", true);
1445 
1446     /* Wait for the first serial output from the source */
1447     wait_for_serial("src_serial");
1448 
1449     migrate_qmp(from, uri, "{}");
1450 
1451     wait_for_migration_pass(from);
1452 
1453     if (!got_stop) {
1454         qtest_qmp_eventwait(from, "STOP");
1455     }
1456 
1457     qtest_qmp_eventwait(to, "RESUME");
1458 
1459     wait_for_serial("dest_serial");
1460     wait_for_migration_complete(from);
1461 
1462     /* Check whether shared RAM has been really skipped */
1463     g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
1464 
1465     test_migrate_end(from, to, true);
1466 }
1467 #endif
1468 
1469 static void *
1470 test_migrate_xbzrle_start(QTestState *from,
1471                           QTestState *to)
1472 {
1473     migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
1474 
1475     migrate_set_capability(from, "xbzrle", true);
1476     migrate_set_capability(to, "xbzrle", true);
1477 
1478     return NULL;
1479 }
1480 
1481 static void test_precopy_unix_xbzrle(void)
1482 {
1483     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1484     MigrateCommon args = {
1485         .connect_uri = uri,
1486         .listen_uri = uri,
1487 
1488         .start_hook = test_migrate_xbzrle_start,
1489 
1490         .iterations = 2,
1491     };
1492 
1493     test_precopy_common(&args);
1494 }
1495 
1496 static void test_precopy_tcp_plain(void)
1497 {
1498     MigrateCommon args = {
1499         .listen_uri = "tcp:127.0.0.1:0",
1500     };
1501 
1502     test_precopy_common(&args);
1503 }
1504 
1505 #ifdef CONFIG_GNUTLS
1506 static void test_precopy_tcp_tls_psk_match(void)
1507 {
1508     MigrateCommon args = {
1509         .listen_uri = "tcp:127.0.0.1:0",
1510         .start_hook = test_migrate_tls_psk_start_match,
1511         .finish_hook = test_migrate_tls_psk_finish,
1512     };
1513 
1514     test_precopy_common(&args);
1515 }
1516 
1517 static void test_precopy_tcp_tls_psk_mismatch(void)
1518 {
1519     MigrateCommon args = {
1520         .start = {
1521             .hide_stderr = true,
1522         },
1523         .listen_uri = "tcp:127.0.0.1:0",
1524         .start_hook = test_migrate_tls_psk_start_mismatch,
1525         .finish_hook = test_migrate_tls_psk_finish,
1526         .result = MIG_TEST_FAIL,
1527     };
1528 
1529     test_precopy_common(&args);
1530 }
1531 
1532 #ifdef CONFIG_TASN1
1533 static void test_precopy_tcp_tls_x509_default_host(void)
1534 {
1535     MigrateCommon args = {
1536         .listen_uri = "tcp:127.0.0.1:0",
1537         .start_hook = test_migrate_tls_x509_start_default_host,
1538         .finish_hook = test_migrate_tls_x509_finish,
1539     };
1540 
1541     test_precopy_common(&args);
1542 }
1543 
1544 static void test_precopy_tcp_tls_x509_override_host(void)
1545 {
1546     MigrateCommon args = {
1547         .listen_uri = "tcp:127.0.0.1:0",
1548         .start_hook = test_migrate_tls_x509_start_override_host,
1549         .finish_hook = test_migrate_tls_x509_finish,
1550     };
1551 
1552     test_precopy_common(&args);
1553 }
1554 
1555 static void test_precopy_tcp_tls_x509_mismatch_host(void)
1556 {
1557     MigrateCommon args = {
1558         .start = {
1559             .hide_stderr = true,
1560         },
1561         .listen_uri = "tcp:127.0.0.1:0",
1562         .start_hook = test_migrate_tls_x509_start_mismatch_host,
1563         .finish_hook = test_migrate_tls_x509_finish,
1564         .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
1565     };
1566 
1567     test_precopy_common(&args);
1568 }
1569 
1570 static void test_precopy_tcp_tls_x509_friendly_client(void)
1571 {
1572     MigrateCommon args = {
1573         .listen_uri = "tcp:127.0.0.1:0",
1574         .start_hook = test_migrate_tls_x509_start_friendly_client,
1575         .finish_hook = test_migrate_tls_x509_finish,
1576     };
1577 
1578     test_precopy_common(&args);
1579 }
1580 
1581 static void test_precopy_tcp_tls_x509_hostile_client(void)
1582 {
1583     MigrateCommon args = {
1584         .start = {
1585             .hide_stderr = true,
1586         },
1587         .listen_uri = "tcp:127.0.0.1:0",
1588         .start_hook = test_migrate_tls_x509_start_hostile_client,
1589         .finish_hook = test_migrate_tls_x509_finish,
1590         .result = MIG_TEST_FAIL,
1591     };
1592 
1593     test_precopy_common(&args);
1594 }
1595 
1596 static void test_precopy_tcp_tls_x509_allow_anon_client(void)
1597 {
1598     MigrateCommon args = {
1599         .listen_uri = "tcp:127.0.0.1:0",
1600         .start_hook = test_migrate_tls_x509_start_allow_anon_client,
1601         .finish_hook = test_migrate_tls_x509_finish,
1602     };
1603 
1604     test_precopy_common(&args);
1605 }
1606 
1607 static void test_precopy_tcp_tls_x509_reject_anon_client(void)
1608 {
1609     MigrateCommon args = {
1610         .start = {
1611             .hide_stderr = true,
1612         },
1613         .listen_uri = "tcp:127.0.0.1:0",
1614         .start_hook = test_migrate_tls_x509_start_reject_anon_client,
1615         .finish_hook = test_migrate_tls_x509_finish,
1616         .result = MIG_TEST_FAIL,
1617     };
1618 
1619     test_precopy_common(&args);
1620 }
1621 #endif /* CONFIG_TASN1 */
1622 #endif /* CONFIG_GNUTLS */
1623 
1624 static void *test_migrate_fd_start_hook(QTestState *from,
1625                                         QTestState *to)
1626 {
1627     QDict *rsp;
1628     int ret;
1629     int pair[2];
1630 
1631     /* Create two connected sockets for migration */
1632     ret = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
1633     g_assert_cmpint(ret, ==, 0);
1634 
1635     /* Send the 1st socket to the target */
1636     rsp = wait_command_fd(to, pair[0],
1637                           "{ 'execute': 'getfd',"
1638                           "  'arguments': { 'fdname': 'fd-mig' }}");
1639     qobject_unref(rsp);
1640     close(pair[0]);
1641 
1642     /* Start incoming migration from the 1st socket */
1643     rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1644                            "  'arguments': { 'uri': 'fd:fd-mig' }}");
1645     qobject_unref(rsp);
1646 
1647     /* Send the 2nd socket to the target */
1648     rsp = wait_command_fd(from, pair[1],
1649                           "{ 'execute': 'getfd',"
1650                           "  'arguments': { 'fdname': 'fd-mig' }}");
1651     qobject_unref(rsp);
1652     close(pair[1]);
1653 
1654     return NULL;
1655 }
1656 
1657 static void test_migrate_fd_finish_hook(QTestState *from,
1658                                         QTestState *to,
1659                                         void *opaque)
1660 {
1661     QDict *rsp;
1662     const char *error_desc;
1663 
1664     /* Test closing fds */
1665     /* We assume, that QEMU removes named fd from its list,
1666      * so this should fail */
1667     rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
1668                           "  'arguments': { 'fdname': 'fd-mig' }}");
1669     g_assert_true(qdict_haskey(rsp, "error"));
1670     error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1671     g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1672     qobject_unref(rsp);
1673 
1674     rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
1675                         "  'arguments': { 'fdname': 'fd-mig' }}");
1676     g_assert_true(qdict_haskey(rsp, "error"));
1677     error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1678     g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1679     qobject_unref(rsp);
1680 }
1681 
1682 static void test_migrate_fd_proto(void)
1683 {
1684     MigrateCommon args = {
1685         .listen_uri = "defer",
1686         .connect_uri = "fd:fd-mig",
1687         .start_hook = test_migrate_fd_start_hook,
1688         .finish_hook = test_migrate_fd_finish_hook
1689     };
1690     test_precopy_common(&args);
1691 }
1692 
1693 static void do_test_validate_uuid(MigrateStart *args, bool should_fail)
1694 {
1695     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1696     QTestState *from, *to;
1697 
1698     if (test_migrate_start(&from, &to, uri, args)) {
1699         return;
1700     }
1701 
1702     /*
1703      * UUID validation is at the begin of migration. So, the main process of
1704      * migration is not interesting for us here. Thus, set huge downtime for
1705      * very fast migration.
1706      */
1707     migrate_set_parameter_int(from, "downtime-limit", 1000000);
1708     migrate_set_capability(from, "validate-uuid", true);
1709 
1710     /* Wait for the first serial output from the source */
1711     wait_for_serial("src_serial");
1712 
1713     migrate_qmp(from, uri, "{}");
1714 
1715     if (should_fail) {
1716         qtest_set_expected_status(to, 1);
1717         wait_for_migration_fail(from, true);
1718     } else {
1719         wait_for_migration_complete(from);
1720     }
1721 
1722     test_migrate_end(from, to, false);
1723 }
1724 
1725 static void test_validate_uuid(void)
1726 {
1727     MigrateStart args = {
1728         .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
1729         .opts_target = "-uuid 11111111-1111-1111-1111-111111111111",
1730     };
1731 
1732     do_test_validate_uuid(&args, false);
1733 }
1734 
1735 static void test_validate_uuid_error(void)
1736 {
1737     MigrateStart args = {
1738         .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
1739         .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
1740         .hide_stderr = true,
1741     };
1742 
1743     do_test_validate_uuid(&args, true);
1744 }
1745 
1746 static void test_validate_uuid_src_not_set(void)
1747 {
1748     MigrateStart args = {
1749         .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
1750         .hide_stderr = true,
1751     };
1752 
1753     do_test_validate_uuid(&args, false);
1754 }
1755 
1756 static void test_validate_uuid_dst_not_set(void)
1757 {
1758     MigrateStart args = {
1759         .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
1760         .hide_stderr = true,
1761     };
1762 
1763     do_test_validate_uuid(&args, false);
1764 }
1765 
1766 static void test_migrate_auto_converge(void)
1767 {
1768     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1769     MigrateStart args = {};
1770     QTestState *from, *to;
1771     int64_t percentage;
1772 
1773     /*
1774      * We want the test to be stable and as fast as possible.
1775      * E.g., with 1Gb/s bandwith migration may pass without throttling,
1776      * so we need to decrease a bandwidth.
1777      */
1778     const int64_t init_pct = 5, inc_pct = 50, max_pct = 95;
1779 
1780     if (test_migrate_start(&from, &to, uri, &args)) {
1781         return;
1782     }
1783 
1784     migrate_set_capability(from, "auto-converge", true);
1785     migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct);
1786     migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct);
1787     migrate_set_parameter_int(from, "max-cpu-throttle", max_pct);
1788 
1789     /*
1790      * Set the initial parameters so that the migration could not converge
1791      * without throttling.
1792      */
1793     migrate_ensure_non_converge(from);
1794 
1795     /* To check remaining size after precopy */
1796     migrate_set_capability(from, "pause-before-switchover", true);
1797 
1798     /* Wait for the first serial output from the source */
1799     wait_for_serial("src_serial");
1800 
1801     migrate_qmp(from, uri, "{}");
1802 
1803     /* Wait for throttling begins */
1804     percentage = 0;
1805     while (percentage == 0) {
1806         percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1807         usleep(100);
1808         g_assert_false(got_stop);
1809     }
1810     /* The first percentage of throttling should be equal to init_pct */
1811     g_assert_cmpint(percentage, ==, init_pct);
1812     /* Now, when we tested that throttling works, let it converge */
1813     migrate_ensure_converge(from);
1814 
1815     /*
1816      * Wait for pre-switchover status to check last throttle percentage
1817      * and remaining. These values will be zeroed later
1818      */
1819     wait_for_migration_status(from, "pre-switchover", NULL);
1820 
1821     /* The final percentage of throttling shouldn't be greater than max_pct */
1822     percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1823     g_assert_cmpint(percentage, <=, max_pct);
1824     migrate_continue(from, "pre-switchover");
1825 
1826     qtest_qmp_eventwait(to, "RESUME");
1827 
1828     wait_for_serial("dest_serial");
1829     wait_for_migration_complete(from);
1830 
1831     test_migrate_end(from, to, true);
1832 }
1833 
1834 static void *
1835 test_migrate_precopy_tcp_multifd_start_common(QTestState *from,
1836                                               QTestState *to,
1837                                               const char *method)
1838 {
1839     QDict *rsp;
1840 
1841     migrate_set_parameter_int(from, "multifd-channels", 16);
1842     migrate_set_parameter_int(to, "multifd-channels", 16);
1843 
1844     migrate_set_parameter_str(from, "multifd-compression", method);
1845     migrate_set_parameter_str(to, "multifd-compression", method);
1846 
1847     migrate_set_capability(from, "multifd", true);
1848     migrate_set_capability(to, "multifd", true);
1849 
1850     /* Start incoming migration from the 1st socket */
1851     rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1852                            "  'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1853     qobject_unref(rsp);
1854 
1855     return NULL;
1856 }
1857 
1858 static void *
1859 test_migrate_precopy_tcp_multifd_start(QTestState *from,
1860                                        QTestState *to)
1861 {
1862     return test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1863 }
1864 
1865 static void *
1866 test_migrate_precopy_tcp_multifd_zlib_start(QTestState *from,
1867                                             QTestState *to)
1868 {
1869     return test_migrate_precopy_tcp_multifd_start_common(from, to, "zlib");
1870 }
1871 
1872 #ifdef CONFIG_ZSTD
1873 static void *
1874 test_migrate_precopy_tcp_multifd_zstd_start(QTestState *from,
1875                                             QTestState *to)
1876 {
1877     return test_migrate_precopy_tcp_multifd_start_common(from, to, "zstd");
1878 }
1879 #endif /* CONFIG_ZSTD */
1880 
1881 static void test_multifd_tcp_none(void)
1882 {
1883     MigrateCommon args = {
1884         .listen_uri = "defer",
1885         .start_hook = test_migrate_precopy_tcp_multifd_start,
1886     };
1887     test_precopy_common(&args);
1888 }
1889 
1890 static void test_multifd_tcp_zlib(void)
1891 {
1892     MigrateCommon args = {
1893         .listen_uri = "defer",
1894         .start_hook = test_migrate_precopy_tcp_multifd_zlib_start,
1895     };
1896     test_precopy_common(&args);
1897 }
1898 
1899 #ifdef CONFIG_ZSTD
1900 static void test_multifd_tcp_zstd(void)
1901 {
1902     MigrateCommon args = {
1903         .listen_uri = "defer",
1904         .start_hook = test_migrate_precopy_tcp_multifd_zstd_start,
1905     };
1906     test_precopy_common(&args);
1907 }
1908 #endif
1909 
1910 #ifdef CONFIG_GNUTLS
1911 static void *
1912 test_migrate_multifd_tcp_tls_psk_start_match(QTestState *from,
1913                                              QTestState *to)
1914 {
1915     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1916     return test_migrate_tls_psk_start_match(from, to);
1917 }
1918 
1919 static void *
1920 test_migrate_multifd_tcp_tls_psk_start_mismatch(QTestState *from,
1921                                                 QTestState *to)
1922 {
1923     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1924     return test_migrate_tls_psk_start_mismatch(from, to);
1925 }
1926 
1927 #ifdef CONFIG_TASN1
1928 static void *
1929 test_migrate_multifd_tls_x509_start_default_host(QTestState *from,
1930                                                  QTestState *to)
1931 {
1932     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1933     return test_migrate_tls_x509_start_default_host(from, to);
1934 }
1935 
1936 static void *
1937 test_migrate_multifd_tls_x509_start_override_host(QTestState *from,
1938                                                   QTestState *to)
1939 {
1940     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1941     return test_migrate_tls_x509_start_override_host(from, to);
1942 }
1943 
1944 static void *
1945 test_migrate_multifd_tls_x509_start_mismatch_host(QTestState *from,
1946                                                   QTestState *to)
1947 {
1948     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1949     return test_migrate_tls_x509_start_mismatch_host(from, to);
1950 }
1951 
1952 static void *
1953 test_migrate_multifd_tls_x509_start_allow_anon_client(QTestState *from,
1954                                                       QTestState *to)
1955 {
1956     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1957     return test_migrate_tls_x509_start_allow_anon_client(from, to);
1958 }
1959 
1960 static void *
1961 test_migrate_multifd_tls_x509_start_reject_anon_client(QTestState *from,
1962                                                        QTestState *to)
1963 {
1964     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1965     return test_migrate_tls_x509_start_reject_anon_client(from, to);
1966 }
1967 #endif /* CONFIG_TASN1 */
1968 
1969 static void test_multifd_tcp_tls_psk_match(void)
1970 {
1971     MigrateCommon args = {
1972         .listen_uri = "defer",
1973         .start_hook = test_migrate_multifd_tcp_tls_psk_start_match,
1974         .finish_hook = test_migrate_tls_psk_finish,
1975     };
1976     test_precopy_common(&args);
1977 }
1978 
1979 static void test_multifd_tcp_tls_psk_mismatch(void)
1980 {
1981     MigrateCommon args = {
1982         .start = {
1983             .hide_stderr = true,
1984         },
1985         .listen_uri = "defer",
1986         .start_hook = test_migrate_multifd_tcp_tls_psk_start_mismatch,
1987         .finish_hook = test_migrate_tls_psk_finish,
1988         .result = MIG_TEST_FAIL,
1989     };
1990     test_precopy_common(&args);
1991 }
1992 
1993 #ifdef CONFIG_TASN1
1994 static void test_multifd_tcp_tls_x509_default_host(void)
1995 {
1996     MigrateCommon args = {
1997         .listen_uri = "defer",
1998         .start_hook = test_migrate_multifd_tls_x509_start_default_host,
1999         .finish_hook = test_migrate_tls_x509_finish,
2000     };
2001     test_precopy_common(&args);
2002 }
2003 
2004 static void test_multifd_tcp_tls_x509_override_host(void)
2005 {
2006     MigrateCommon args = {
2007         .listen_uri = "defer",
2008         .start_hook = test_migrate_multifd_tls_x509_start_override_host,
2009         .finish_hook = test_migrate_tls_x509_finish,
2010     };
2011     test_precopy_common(&args);
2012 }
2013 
2014 static void test_multifd_tcp_tls_x509_mismatch_host(void)
2015 {
2016     /*
2017      * This has different behaviour to the non-multifd case.
2018      *
2019      * In non-multifd case when client aborts due to mismatched
2020      * cert host, the server has already started trying to load
2021      * migration state, and so it exits with I/O failure.
2022      *
2023      * In multifd case when client aborts due to mismatched
2024      * cert host, the server is still waiting for the other
2025      * multifd connections to arrive so hasn't started trying
2026      * to load migration state, and thus just aborts the migration
2027      * without exiting.
2028      */
2029     MigrateCommon args = {
2030         .start = {
2031             .hide_stderr = true,
2032         },
2033         .listen_uri = "defer",
2034         .start_hook = test_migrate_multifd_tls_x509_start_mismatch_host,
2035         .finish_hook = test_migrate_tls_x509_finish,
2036         .result = MIG_TEST_FAIL,
2037     };
2038     test_precopy_common(&args);
2039 }
2040 
2041 static void test_multifd_tcp_tls_x509_allow_anon_client(void)
2042 {
2043     MigrateCommon args = {
2044         .listen_uri = "defer",
2045         .start_hook = test_migrate_multifd_tls_x509_start_allow_anon_client,
2046         .finish_hook = test_migrate_tls_x509_finish,
2047     };
2048     test_precopy_common(&args);
2049 }
2050 
2051 static void test_multifd_tcp_tls_x509_reject_anon_client(void)
2052 {
2053     MigrateCommon args = {
2054         .start = {
2055             .hide_stderr = true,
2056         },
2057         .listen_uri = "defer",
2058         .start_hook = test_migrate_multifd_tls_x509_start_reject_anon_client,
2059         .finish_hook = test_migrate_tls_x509_finish,
2060         .result = MIG_TEST_FAIL,
2061     };
2062     test_precopy_common(&args);
2063 }
2064 #endif /* CONFIG_TASN1 */
2065 #endif /* CONFIG_GNUTLS */
2066 
2067 /*
2068  * This test does:
2069  *  source               target
2070  *                       migrate_incoming
2071  *     migrate
2072  *     migrate_cancel
2073  *                       launch another target
2074  *     migrate
2075  *
2076  *  And see that it works
2077  */
2078 static void test_multifd_tcp_cancel(void)
2079 {
2080     MigrateStart args = {
2081         .hide_stderr = true,
2082     };
2083     QTestState *from, *to, *to2;
2084     QDict *rsp;
2085     g_autofree char *uri = NULL;
2086 
2087     if (test_migrate_start(&from, &to, "defer", &args)) {
2088         return;
2089     }
2090 
2091     migrate_ensure_non_converge(from);
2092 
2093     migrate_set_parameter_int(from, "multifd-channels", 16);
2094     migrate_set_parameter_int(to, "multifd-channels", 16);
2095 
2096     migrate_set_capability(from, "multifd", true);
2097     migrate_set_capability(to, "multifd", true);
2098 
2099     /* Start incoming migration from the 1st socket */
2100     rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
2101                            "  'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2102     qobject_unref(rsp);
2103 
2104     /* Wait for the first serial output from the source */
2105     wait_for_serial("src_serial");
2106 
2107     uri = migrate_get_socket_address(to, "socket-address");
2108 
2109     migrate_qmp(from, uri, "{}");
2110 
2111     wait_for_migration_pass(from);
2112 
2113     migrate_cancel(from);
2114 
2115     args = (MigrateStart){
2116         .only_target = true,
2117     };
2118 
2119     if (test_migrate_start(&from, &to2, "defer", &args)) {
2120         return;
2121     }
2122 
2123     migrate_set_parameter_int(to2, "multifd-channels", 16);
2124 
2125     migrate_set_capability(to2, "multifd", true);
2126 
2127     /* Start incoming migration from the 1st socket */
2128     rsp = wait_command(to2, "{ 'execute': 'migrate-incoming',"
2129                             "  'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2130     qobject_unref(rsp);
2131 
2132     g_free(uri);
2133     uri = migrate_get_socket_address(to2, "socket-address");
2134 
2135     wait_for_migration_status(from, "cancelled", NULL);
2136 
2137     migrate_ensure_converge(from);
2138 
2139     migrate_qmp(from, uri, "{}");
2140 
2141     wait_for_migration_pass(from);
2142 
2143     if (!got_stop) {
2144         qtest_qmp_eventwait(from, "STOP");
2145     }
2146     qtest_qmp_eventwait(to2, "RESUME");
2147 
2148     wait_for_serial("dest_serial");
2149     wait_for_migration_complete(from);
2150     test_migrate_end(from, to2, true);
2151 }
2152 
2153 static void calc_dirty_rate(QTestState *who, uint64_t calc_time)
2154 {
2155     qobject_unref(qmp_command(who,
2156                   "{ 'execute': 'calc-dirty-rate',"
2157                   "'arguments': { "
2158                   "'calc-time': %ld,"
2159                   "'mode': 'dirty-ring' }}",
2160                   calc_time));
2161 }
2162 
2163 static QDict *query_dirty_rate(QTestState *who)
2164 {
2165     return qmp_command(who, "{ 'execute': 'query-dirty-rate' }");
2166 }
2167 
2168 static void dirtylimit_set_all(QTestState *who, uint64_t dirtyrate)
2169 {
2170     qobject_unref(qmp_command(who,
2171                   "{ 'execute': 'set-vcpu-dirty-limit',"
2172                   "'arguments': { "
2173                   "'dirty-rate': %ld } }",
2174                   dirtyrate));
2175 }
2176 
2177 static void cancel_vcpu_dirty_limit(QTestState *who)
2178 {
2179     qobject_unref(qmp_command(who,
2180                   "{ 'execute': 'cancel-vcpu-dirty-limit' }"));
2181 }
2182 
2183 static QDict *query_vcpu_dirty_limit(QTestState *who)
2184 {
2185     QDict *rsp;
2186 
2187     rsp = qtest_qmp(who, "{ 'execute': 'query-vcpu-dirty-limit' }");
2188     g_assert(!qdict_haskey(rsp, "error"));
2189     g_assert(qdict_haskey(rsp, "return"));
2190 
2191     return rsp;
2192 }
2193 
2194 static bool calc_dirtyrate_ready(QTestState *who)
2195 {
2196     QDict *rsp_return;
2197     gchar *status;
2198 
2199     rsp_return = query_dirty_rate(who);
2200     g_assert(rsp_return);
2201 
2202     status = g_strdup(qdict_get_str(rsp_return, "status"));
2203     g_assert(status);
2204 
2205     return g_strcmp0(status, "measuring");
2206 }
2207 
2208 static void wait_for_calc_dirtyrate_complete(QTestState *who,
2209                                              int64_t time_s)
2210 {
2211     int max_try_count = 10000;
2212     usleep(time_s * 1000000);
2213 
2214     while (!calc_dirtyrate_ready(who) && max_try_count--) {
2215         usleep(1000);
2216     }
2217 
2218     /*
2219      * Set the timeout with 10 s(max_try_count * 1000us),
2220      * if dirtyrate measurement not complete, fail test.
2221      */
2222     g_assert_cmpint(max_try_count, !=, 0);
2223 }
2224 
2225 static int64_t get_dirty_rate(QTestState *who)
2226 {
2227     QDict *rsp_return;
2228     gchar *status;
2229     QList *rates;
2230     const QListEntry *entry;
2231     QDict *rate;
2232     int64_t dirtyrate;
2233 
2234     rsp_return = query_dirty_rate(who);
2235     g_assert(rsp_return);
2236 
2237     status = g_strdup(qdict_get_str(rsp_return, "status"));
2238     g_assert(status);
2239     g_assert_cmpstr(status, ==, "measured");
2240 
2241     rates = qdict_get_qlist(rsp_return, "vcpu-dirty-rate");
2242     g_assert(rates && !qlist_empty(rates));
2243 
2244     entry = qlist_first(rates);
2245     g_assert(entry);
2246 
2247     rate = qobject_to(QDict, qlist_entry_obj(entry));
2248     g_assert(rate);
2249 
2250     dirtyrate = qdict_get_try_int(rate, "dirty-rate", -1);
2251 
2252     qobject_unref(rsp_return);
2253     return dirtyrate;
2254 }
2255 
2256 static int64_t get_limit_rate(QTestState *who)
2257 {
2258     QDict *rsp_return;
2259     QList *rates;
2260     const QListEntry *entry;
2261     QDict *rate;
2262     int64_t dirtyrate;
2263 
2264     rsp_return = query_vcpu_dirty_limit(who);
2265     g_assert(rsp_return);
2266 
2267     rates = qdict_get_qlist(rsp_return, "return");
2268     g_assert(rates && !qlist_empty(rates));
2269 
2270     entry = qlist_first(rates);
2271     g_assert(entry);
2272 
2273     rate = qobject_to(QDict, qlist_entry_obj(entry));
2274     g_assert(rate);
2275 
2276     dirtyrate = qdict_get_try_int(rate, "limit-rate", -1);
2277 
2278     qobject_unref(rsp_return);
2279     return dirtyrate;
2280 }
2281 
2282 static QTestState *dirtylimit_start_vm(void)
2283 {
2284     QTestState *vm = NULL;
2285     g_autofree gchar *cmd = NULL;
2286     const char *arch = qtest_get_arch();
2287     g_autofree char *bootpath = NULL;
2288 
2289     assert((strcmp(arch, "x86_64") == 0));
2290     bootpath = g_strdup_printf("%s/bootsect", tmpfs);
2291     assert(sizeof(x86_bootsect) == 512);
2292     init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect));
2293 
2294     cmd = g_strdup_printf("-accel kvm,dirty-ring-size=4096 "
2295                           "-name dirtylimit-test,debug-threads=on "
2296                           "-m 150M -smp 1 "
2297                           "-serial file:%s/vm_serial "
2298                           "-drive file=%s,format=raw ",
2299                           tmpfs, bootpath);
2300 
2301     vm = qtest_init(cmd);
2302     return vm;
2303 }
2304 
2305 static void dirtylimit_stop_vm(QTestState *vm)
2306 {
2307     qtest_quit(vm);
2308     cleanup("bootsect");
2309     cleanup("vm_serial");
2310 }
2311 
2312 static void test_vcpu_dirty_limit(void)
2313 {
2314     QTestState *vm;
2315     int64_t origin_rate;
2316     int64_t quota_rate;
2317     int64_t rate ;
2318     int max_try_count = 20;
2319     int hit = 0;
2320 
2321     /* Start vm for vcpu dirtylimit test */
2322     vm = dirtylimit_start_vm();
2323 
2324     /* Wait for the first serial output from the vm*/
2325     wait_for_serial("vm_serial");
2326 
2327     /* Do dirtyrate measurement with calc time equals 1s */
2328     calc_dirty_rate(vm, 1);
2329 
2330     /* Sleep calc time and wait for calc dirtyrate complete */
2331     wait_for_calc_dirtyrate_complete(vm, 1);
2332 
2333     /* Query original dirty page rate */
2334     origin_rate = get_dirty_rate(vm);
2335 
2336     /* VM booted from bootsect should dirty memory steadily */
2337     assert(origin_rate != 0);
2338 
2339     /* Setup quota dirty page rate at half of origin */
2340     quota_rate = origin_rate / 2;
2341 
2342     /* Set dirtylimit */
2343     dirtylimit_set_all(vm, quota_rate);
2344 
2345     /*
2346      * Check if set-vcpu-dirty-limit and query-vcpu-dirty-limit
2347      * works literally
2348      */
2349     g_assert_cmpint(quota_rate, ==, get_limit_rate(vm));
2350 
2351     /* Sleep a bit to check if it take effect */
2352     usleep(2000000);
2353 
2354     /*
2355      * Check if dirtylimit take effect realistically, set the
2356      * timeout with 20 s(max_try_count * 1s), if dirtylimit
2357      * doesn't take effect, fail test.
2358      */
2359     while (--max_try_count) {
2360         calc_dirty_rate(vm, 1);
2361         wait_for_calc_dirtyrate_complete(vm, 1);
2362         rate = get_dirty_rate(vm);
2363 
2364         /*
2365          * Assume hitting if current rate is less
2366          * than quota rate (within accepting error)
2367          */
2368         if (rate < (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) {
2369             hit = 1;
2370             break;
2371         }
2372     }
2373 
2374     g_assert_cmpint(hit, ==, 1);
2375 
2376     hit = 0;
2377     max_try_count = 20;
2378 
2379     /* Check if dirtylimit cancellation take effect */
2380     cancel_vcpu_dirty_limit(vm);
2381     while (--max_try_count) {
2382         calc_dirty_rate(vm, 1);
2383         wait_for_calc_dirtyrate_complete(vm, 1);
2384         rate = get_dirty_rate(vm);
2385 
2386         /*
2387          * Assume dirtylimit be canceled if current rate is
2388          * greater than quota rate (within accepting error)
2389          */
2390         if (rate > (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) {
2391             hit = 1;
2392             break;
2393         }
2394     }
2395 
2396     g_assert_cmpint(hit, ==, 1);
2397     dirtylimit_stop_vm(vm);
2398 }
2399 
2400 static bool kvm_dirty_ring_supported(void)
2401 {
2402 #if defined(__linux__) && defined(HOST_X86_64)
2403     int ret, kvm_fd = open("/dev/kvm", O_RDONLY);
2404 
2405     if (kvm_fd < 0) {
2406         return false;
2407     }
2408 
2409     ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, KVM_CAP_DIRTY_LOG_RING);
2410     close(kvm_fd);
2411 
2412     /* We test with 4096 slots */
2413     if (ret < 4096) {
2414         return false;
2415     }
2416 
2417     return true;
2418 #else
2419     return false;
2420 #endif
2421 }
2422 
2423 int main(int argc, char **argv)
2424 {
2425     char template[] = "/tmp/migration-test-XXXXXX";
2426     const bool has_kvm = qtest_has_accel("kvm");
2427     const bool has_uffd = ufd_version_check();
2428     int ret;
2429 
2430     g_test_init(&argc, &argv, NULL);
2431 
2432     /*
2433      * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
2434      * is touchy due to race conditions on dirty bits (especially on PPC for
2435      * some reason)
2436      */
2437     if (g_str_equal(qtest_get_arch(), "ppc64") &&
2438         (!has_kvm || access("/sys/module/kvm_hv", F_OK))) {
2439         g_test_message("Skipping test: kvm_hv not available");
2440         return g_test_run();
2441     }
2442 
2443     /*
2444      * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
2445      * there until the problems are resolved
2446      */
2447     if (g_str_equal(qtest_get_arch(), "s390x") && !has_kvm) {
2448         g_test_message("Skipping test: s390x host with KVM is required");
2449         return g_test_run();
2450     }
2451 
2452     tmpfs = mkdtemp(template);
2453     if (!tmpfs) {
2454         g_test_message("mkdtemp on path (%s): %s", template, strerror(errno));
2455     }
2456     g_assert(tmpfs);
2457 
2458     module_call_init(MODULE_INIT_QOM);
2459 
2460     if (has_uffd) {
2461         qtest_add_func("/migration/postcopy/unix", test_postcopy);
2462         qtest_add_func("/migration/postcopy/plain", test_postcopy);
2463         qtest_add_func("/migration/postcopy/recovery/plain",
2464                        test_postcopy_recovery);
2465         qtest_add_func("/migration/postcopy/preempt/plain", test_postcopy_preempt);
2466         qtest_add_func("/migration/postcopy/preempt/recovery/plain",
2467                        test_postcopy_preempt_recovery);
2468     }
2469 
2470     qtest_add_func("/migration/bad_dest", test_baddest);
2471     qtest_add_func("/migration/precopy/unix/plain", test_precopy_unix_plain);
2472     qtest_add_func("/migration/precopy/unix/xbzrle", test_precopy_unix_xbzrle);
2473 #ifdef CONFIG_GNUTLS
2474     qtest_add_func("/migration/precopy/unix/tls/psk",
2475                    test_precopy_unix_tls_psk);
2476 
2477     if (has_uffd) {
2478         /*
2479          * NOTE: psk test is enough for postcopy, as other types of TLS
2480          * channels are tested under precopy.  Here what we want to test is the
2481          * general postcopy path that has TLS channel enabled.
2482          */
2483         qtest_add_func("/migration/postcopy/tls/psk", test_postcopy_tls_psk);
2484         qtest_add_func("/migration/postcopy/recovery/tls/psk",
2485                        test_postcopy_recovery_tls_psk);
2486         qtest_add_func("/migration/postcopy/preempt/tls/psk",
2487                        test_postcopy_preempt_tls_psk);
2488         qtest_add_func("/migration/postcopy/preempt/recovery/tls/psk",
2489                        test_postcopy_preempt_all);
2490     }
2491 #ifdef CONFIG_TASN1
2492     qtest_add_func("/migration/precopy/unix/tls/x509/default-host",
2493                    test_precopy_unix_tls_x509_default_host);
2494     qtest_add_func("/migration/precopy/unix/tls/x509/override-host",
2495                    test_precopy_unix_tls_x509_override_host);
2496 #endif /* CONFIG_TASN1 */
2497 #endif /* CONFIG_GNUTLS */
2498 
2499     qtest_add_func("/migration/precopy/tcp/plain", test_precopy_tcp_plain);
2500 #ifdef CONFIG_GNUTLS
2501     qtest_add_func("/migration/precopy/tcp/tls/psk/match",
2502                    test_precopy_tcp_tls_psk_match);
2503     qtest_add_func("/migration/precopy/tcp/tls/psk/mismatch",
2504                    test_precopy_tcp_tls_psk_mismatch);
2505 #ifdef CONFIG_TASN1
2506     qtest_add_func("/migration/precopy/tcp/tls/x509/default-host",
2507                    test_precopy_tcp_tls_x509_default_host);
2508     qtest_add_func("/migration/precopy/tcp/tls/x509/override-host",
2509                    test_precopy_tcp_tls_x509_override_host);
2510     qtest_add_func("/migration/precopy/tcp/tls/x509/mismatch-host",
2511                    test_precopy_tcp_tls_x509_mismatch_host);
2512     qtest_add_func("/migration/precopy/tcp/tls/x509/friendly-client",
2513                    test_precopy_tcp_tls_x509_friendly_client);
2514     qtest_add_func("/migration/precopy/tcp/tls/x509/hostile-client",
2515                    test_precopy_tcp_tls_x509_hostile_client);
2516     qtest_add_func("/migration/precopy/tcp/tls/x509/allow-anon-client",
2517                    test_precopy_tcp_tls_x509_allow_anon_client);
2518     qtest_add_func("/migration/precopy/tcp/tls/x509/reject-anon-client",
2519                    test_precopy_tcp_tls_x509_reject_anon_client);
2520 #endif /* CONFIG_TASN1 */
2521 #endif /* CONFIG_GNUTLS */
2522 
2523     /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
2524     qtest_add_func("/migration/fd_proto", test_migrate_fd_proto);
2525     qtest_add_func("/migration/validate_uuid", test_validate_uuid);
2526     qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error);
2527     qtest_add_func("/migration/validate_uuid_src_not_set",
2528                    test_validate_uuid_src_not_set);
2529     qtest_add_func("/migration/validate_uuid_dst_not_set",
2530                    test_validate_uuid_dst_not_set);
2531 
2532     qtest_add_func("/migration/auto_converge", test_migrate_auto_converge);
2533     qtest_add_func("/migration/multifd/tcp/plain/none",
2534                    test_multifd_tcp_none);
2535     qtest_add_func("/migration/multifd/tcp/plain/cancel",
2536                    test_multifd_tcp_cancel);
2537     qtest_add_func("/migration/multifd/tcp/plain/zlib",
2538                    test_multifd_tcp_zlib);
2539 #ifdef CONFIG_ZSTD
2540     qtest_add_func("/migration/multifd/tcp/plain/zstd",
2541                    test_multifd_tcp_zstd);
2542 #endif
2543 #ifdef CONFIG_GNUTLS
2544     qtest_add_func("/migration/multifd/tcp/tls/psk/match",
2545                    test_multifd_tcp_tls_psk_match);
2546     qtest_add_func("/migration/multifd/tcp/tls/psk/mismatch",
2547                    test_multifd_tcp_tls_psk_mismatch);
2548 #ifdef CONFIG_TASN1
2549     qtest_add_func("/migration/multifd/tcp/tls/x509/default-host",
2550                    test_multifd_tcp_tls_x509_default_host);
2551     qtest_add_func("/migration/multifd/tcp/tls/x509/override-host",
2552                    test_multifd_tcp_tls_x509_override_host);
2553     qtest_add_func("/migration/multifd/tcp/tls/x509/mismatch-host",
2554                    test_multifd_tcp_tls_x509_mismatch_host);
2555     qtest_add_func("/migration/multifd/tcp/tls/x509/allow-anon-client",
2556                    test_multifd_tcp_tls_x509_allow_anon_client);
2557     qtest_add_func("/migration/multifd/tcp/tls/x509/reject-anon-client",
2558                    test_multifd_tcp_tls_x509_reject_anon_client);
2559 #endif /* CONFIG_TASN1 */
2560 #endif /* CONFIG_GNUTLS */
2561 
2562     if (kvm_dirty_ring_supported()) {
2563         qtest_add_func("/migration/dirty_ring",
2564                        test_precopy_unix_dirty_ring);
2565         qtest_add_func("/migration/vcpu_dirty_limit",
2566                        test_vcpu_dirty_limit);
2567     }
2568 
2569     ret = g_test_run();
2570 
2571     g_assert_cmpint(ret, ==, 0);
2572 
2573     ret = rmdir(tmpfs);
2574     if (ret != 0) {
2575         g_test_message("unable to rmdir: path (%s): %s",
2576                        tmpfs, strerror(errno));
2577     }
2578 
2579     return ret;
2580 }
2581