xref: /qemu/tests/qtest/migration-test.c (revision 727385c4)
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 "libqos/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 
27 #include "migration-helpers.h"
28 #include "tests/migration/migration-test.h"
29 
30 /* For dirty ring test; so far only x86_64 is supported */
31 #if defined(__linux__) && defined(HOST_X86_64)
32 #include "linux/kvm.h"
33 #endif
34 
35 /* TODO actually test the results and get rid of this */
36 #define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__))
37 
38 unsigned start_address;
39 unsigned end_address;
40 static bool uffd_feature_thread_id;
41 
42 /* A downtime where the test really should converge */
43 #define CONVERGE_DOWNTIME 1000
44 
45 #if defined(__linux__)
46 #include <sys/syscall.h>
47 #include <sys/vfs.h>
48 #endif
49 
50 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
51 #include <sys/eventfd.h>
52 #include <sys/ioctl.h>
53 #include <linux/userfaultfd.h>
54 
55 static bool ufd_version_check(void)
56 {
57     struct uffdio_api api_struct;
58     uint64_t ioctl_mask;
59 
60     int ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
61 
62     if (ufd == -1) {
63         g_test_message("Skipping test: userfaultfd not available");
64         return false;
65     }
66 
67     api_struct.api = UFFD_API;
68     api_struct.features = 0;
69     if (ioctl(ufd, UFFDIO_API, &api_struct)) {
70         g_test_message("Skipping test: UFFDIO_API failed");
71         return false;
72     }
73     uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
74 
75     ioctl_mask = (__u64)1 << _UFFDIO_REGISTER |
76                  (__u64)1 << _UFFDIO_UNREGISTER;
77     if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
78         g_test_message("Skipping test: Missing userfault feature");
79         return false;
80     }
81 
82     return true;
83 }
84 
85 #else
86 static bool ufd_version_check(void)
87 {
88     g_test_message("Skipping test: Userfault not available (builtdtime)");
89     return false;
90 }
91 
92 #endif
93 
94 static const char *tmpfs;
95 
96 /* The boot file modifies memory area in [start_address, end_address)
97  * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
98  */
99 #include "tests/migration/i386/a-b-bootblock.h"
100 #include "tests/migration/aarch64/a-b-kernel.h"
101 #include "tests/migration/s390x/a-b-bios.h"
102 
103 static void init_bootfile(const char *bootpath, void *content, size_t len)
104 {
105     FILE *bootfile = fopen(bootpath, "wb");
106 
107     g_assert_cmpint(fwrite(content, len, 1, bootfile), ==, 1);
108     fclose(bootfile);
109 }
110 
111 /*
112  * Wait for some output in the serial output file,
113  * we get an 'A' followed by an endless string of 'B's
114  * but on the destination we won't have the A.
115  */
116 static void wait_for_serial(const char *side)
117 {
118     g_autofree char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
119     FILE *serialfile = fopen(serialpath, "r");
120     const char *arch = qtest_get_arch();
121     int started = (strcmp(side, "src_serial") == 0 &&
122                    strcmp(arch, "ppc64") == 0) ? 0 : 1;
123 
124     do {
125         int readvalue = fgetc(serialfile);
126 
127         if (!started) {
128             /* SLOF prints its banner before starting test,
129              * to ignore it, mark the start of the test with '_',
130              * ignore all characters until this marker
131              */
132             switch (readvalue) {
133             case '_':
134                 started = 1;
135                 break;
136             case EOF:
137                 fseek(serialfile, 0, SEEK_SET);
138                 usleep(1000);
139                 break;
140             }
141             continue;
142         }
143         switch (readvalue) {
144         case 'A':
145             /* Fine */
146             break;
147 
148         case 'B':
149             /* It's alive! */
150             fclose(serialfile);
151             return;
152 
153         case EOF:
154             started = (strcmp(side, "src_serial") == 0 &&
155                        strcmp(arch, "ppc64") == 0) ? 0 : 1;
156             fseek(serialfile, 0, SEEK_SET);
157             usleep(1000);
158             break;
159 
160         default:
161             fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
162             g_assert_not_reached();
163         }
164     } while (true);
165 }
166 
167 /*
168  * It's tricky to use qemu's migration event capability with qtest,
169  * events suddenly appearing confuse the qmp()/hmp() responses.
170  */
171 
172 static int64_t read_ram_property_int(QTestState *who, const char *property)
173 {
174     QDict *rsp_return, *rsp_ram;
175     int64_t result;
176 
177     rsp_return = migrate_query(who);
178     if (!qdict_haskey(rsp_return, "ram")) {
179         /* Still in setup */
180         result = 0;
181     } else {
182         rsp_ram = qdict_get_qdict(rsp_return, "ram");
183         result = qdict_get_try_int(rsp_ram, property, 0);
184     }
185     qobject_unref(rsp_return);
186     return result;
187 }
188 
189 static int64_t read_migrate_property_int(QTestState *who, const char *property)
190 {
191     QDict *rsp_return;
192     int64_t result;
193 
194     rsp_return = migrate_query(who);
195     result = qdict_get_try_int(rsp_return, property, 0);
196     qobject_unref(rsp_return);
197     return result;
198 }
199 
200 static uint64_t get_migration_pass(QTestState *who)
201 {
202     return read_ram_property_int(who, "dirty-sync-count");
203 }
204 
205 static void read_blocktime(QTestState *who)
206 {
207     QDict *rsp_return;
208 
209     rsp_return = migrate_query(who);
210     g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
211     qobject_unref(rsp_return);
212 }
213 
214 static void wait_for_migration_pass(QTestState *who)
215 {
216     uint64_t initial_pass = get_migration_pass(who);
217     uint64_t pass;
218 
219     /* Wait for the 1st sync */
220     while (!got_stop && !initial_pass) {
221         usleep(1000);
222         initial_pass = get_migration_pass(who);
223     }
224 
225     do {
226         usleep(1000);
227         pass = get_migration_pass(who);
228     } while (pass == initial_pass && !got_stop);
229 }
230 
231 static void check_guests_ram(QTestState *who)
232 {
233     /* Our ASM test will have been incrementing one byte from each page from
234      * start_address to < end_address in order. This gives us a constraint
235      * that any page's byte should be equal or less than the previous pages
236      * byte (mod 256); and they should all be equal except for one transition
237      * at the point where we meet the incrementer. (We're running this with
238      * the guest stopped).
239      */
240     unsigned address;
241     uint8_t first_byte;
242     uint8_t last_byte;
243     bool hit_edge = false;
244     int bad = 0;
245 
246     qtest_memread(who, start_address, &first_byte, 1);
247     last_byte = first_byte;
248 
249     for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
250          address += TEST_MEM_PAGE_SIZE)
251     {
252         uint8_t b;
253         qtest_memread(who, address, &b, 1);
254         if (b != last_byte) {
255             if (((b + 1) % 256) == last_byte && !hit_edge) {
256                 /* This is OK, the guest stopped at the point of
257                  * incrementing the previous page but didn't get
258                  * to us yet.
259                  */
260                 hit_edge = true;
261                 last_byte = b;
262             } else {
263                 bad++;
264                 if (bad <= 10) {
265                     fprintf(stderr, "Memory content inconsistency at %x"
266                             " first_byte = %x last_byte = %x current = %x"
267                             " hit_edge = %x\n",
268                             address, first_byte, last_byte, b, hit_edge);
269                 }
270             }
271         }
272     }
273     if (bad >= 10) {
274         fprintf(stderr, "and in another %d pages", bad - 10);
275     }
276     g_assert(bad == 0);
277 }
278 
279 static void cleanup(const char *filename)
280 {
281     g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, filename);
282 
283     unlink(path);
284 }
285 
286 static char *SocketAddress_to_str(SocketAddress *addr)
287 {
288     switch (addr->type) {
289     case SOCKET_ADDRESS_TYPE_INET:
290         return g_strdup_printf("tcp:%s:%s",
291                                addr->u.inet.host,
292                                addr->u.inet.port);
293     case SOCKET_ADDRESS_TYPE_UNIX:
294         return g_strdup_printf("unix:%s",
295                                addr->u.q_unix.path);
296     case SOCKET_ADDRESS_TYPE_FD:
297         return g_strdup_printf("fd:%s", addr->u.fd.str);
298     case SOCKET_ADDRESS_TYPE_VSOCK:
299         return g_strdup_printf("tcp:%s:%s",
300                                addr->u.vsock.cid,
301                                addr->u.vsock.port);
302     default:
303         return g_strdup("unknown address type");
304     }
305 }
306 
307 static char *migrate_get_socket_address(QTestState *who, const char *parameter)
308 {
309     QDict *rsp;
310     char *result;
311     SocketAddressList *addrs;
312     Visitor *iv = NULL;
313     QObject *object;
314 
315     rsp = migrate_query(who);
316     object = qdict_get(rsp, parameter);
317 
318     iv = qobject_input_visitor_new(object);
319     visit_type_SocketAddressList(iv, NULL, &addrs, &error_abort);
320     visit_free(iv);
321 
322     /* we are only using a single address */
323     result = SocketAddress_to_str(addrs->value);
324 
325     qapi_free_SocketAddressList(addrs);
326     qobject_unref(rsp);
327     return result;
328 }
329 
330 static long long migrate_get_parameter_int(QTestState *who,
331                                            const char *parameter)
332 {
333     QDict *rsp;
334     long long result;
335 
336     rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
337     result = qdict_get_int(rsp, parameter);
338     qobject_unref(rsp);
339     return result;
340 }
341 
342 static void migrate_check_parameter_int(QTestState *who, const char *parameter,
343                                         long long value)
344 {
345     long long result;
346 
347     result = migrate_get_parameter_int(who, parameter);
348     g_assert_cmpint(result, ==, value);
349 }
350 
351 static void migrate_set_parameter_int(QTestState *who, const char *parameter,
352                                       long long value)
353 {
354     QDict *rsp;
355 
356     rsp = qtest_qmp(who,
357                     "{ 'execute': 'migrate-set-parameters',"
358                     "'arguments': { %s: %lld } }",
359                     parameter, value);
360     g_assert(qdict_haskey(rsp, "return"));
361     qobject_unref(rsp);
362     migrate_check_parameter_int(who, parameter, value);
363 }
364 
365 static char *migrate_get_parameter_str(QTestState *who,
366                                        const char *parameter)
367 {
368     QDict *rsp;
369     char *result;
370 
371     rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
372     result = g_strdup(qdict_get_str(rsp, parameter));
373     qobject_unref(rsp);
374     return result;
375 }
376 
377 static void migrate_check_parameter_str(QTestState *who, const char *parameter,
378                                         const char *value)
379 {
380     g_autofree char *result = migrate_get_parameter_str(who, parameter);
381     g_assert_cmpstr(result, ==, value);
382 }
383 
384 static void migrate_set_parameter_str(QTestState *who, const char *parameter,
385                                       const char *value)
386 {
387     QDict *rsp;
388 
389     rsp = qtest_qmp(who,
390                     "{ 'execute': 'migrate-set-parameters',"
391                     "'arguments': { %s: %s } }",
392                     parameter, value);
393     g_assert(qdict_haskey(rsp, "return"));
394     qobject_unref(rsp);
395     migrate_check_parameter_str(who, parameter, value);
396 }
397 
398 static void migrate_pause(QTestState *who)
399 {
400     QDict *rsp;
401 
402     rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
403     qobject_unref(rsp);
404 }
405 
406 static void migrate_continue(QTestState *who, const char *state)
407 {
408     QDict *rsp;
409 
410     rsp = wait_command(who,
411                        "{ 'execute': 'migrate-continue',"
412                        "  'arguments': { 'state': %s } }",
413                        state);
414     qobject_unref(rsp);
415 }
416 
417 static void migrate_recover(QTestState *who, const char *uri)
418 {
419     QDict *rsp;
420 
421     rsp = wait_command(who,
422                        "{ 'execute': 'migrate-recover', "
423                        "  'id': 'recover-cmd', "
424                        "  'arguments': { 'uri': %s } }",
425                        uri);
426     qobject_unref(rsp);
427 }
428 
429 static void migrate_cancel(QTestState *who)
430 {
431     QDict *rsp;
432 
433     rsp = wait_command(who, "{ 'execute': 'migrate_cancel' }");
434     qobject_unref(rsp);
435 }
436 
437 static void migrate_set_capability(QTestState *who, const char *capability,
438                                    bool value)
439 {
440     QDict *rsp;
441 
442     rsp = qtest_qmp(who,
443                     "{ 'execute': 'migrate-set-capabilities',"
444                     "'arguments': { "
445                     "'capabilities': [ { "
446                     "'capability': %s, 'state': %i } ] } }",
447                     capability, value);
448     g_assert(qdict_haskey(rsp, "return"));
449     qobject_unref(rsp);
450 }
451 
452 static void migrate_postcopy_start(QTestState *from, QTestState *to)
453 {
454     QDict *rsp;
455 
456     rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
457     qobject_unref(rsp);
458 
459     if (!got_stop) {
460         qtest_qmp_eventwait(from, "STOP");
461     }
462 
463     qtest_qmp_eventwait(to, "RESUME");
464 }
465 
466 typedef struct {
467     /*
468      * QTEST_LOG=1 may override this.  When QTEST_LOG=1, we always dump errors
469      * unconditionally, because it means the user would like to be verbose.
470      */
471     bool hide_stderr;
472     bool use_shmem;
473     /* only launch the target process */
474     bool only_target;
475     /* Use dirty ring if true; dirty logging otherwise */
476     bool use_dirty_ring;
477     char *opts_source;
478     char *opts_target;
479 } MigrateStart;
480 
481 static MigrateStart *migrate_start_new(void)
482 {
483     MigrateStart *args = g_new0(MigrateStart, 1);
484 
485     args->opts_source = g_strdup("");
486     args->opts_target = g_strdup("");
487     return args;
488 }
489 
490 static void migrate_start_destroy(MigrateStart *args)
491 {
492     g_free(args->opts_source);
493     g_free(args->opts_target);
494     g_free(args);
495 }
496 
497 static int test_migrate_start(QTestState **from, QTestState **to,
498                               const char *uri, MigrateStart *args)
499 {
500     g_autofree gchar *arch_source = NULL;
501     g_autofree gchar *arch_target = NULL;
502     g_autofree gchar *cmd_source = NULL;
503     g_autofree gchar *cmd_target = NULL;
504     const gchar *ignore_stderr;
505     g_autofree char *bootpath = NULL;
506     g_autofree char *shmem_opts = NULL;
507     g_autofree char *shmem_path = NULL;
508     const char *arch = qtest_get_arch();
509     const char *machine_opts = NULL;
510     const char *memory_size;
511     int ret = 0;
512 
513     if (args->use_shmem) {
514         if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
515             g_test_skip("/dev/shm is not supported");
516             ret = -1;
517             goto out;
518         }
519     }
520 
521     got_stop = false;
522     bootpath = g_strdup_printf("%s/bootsect", tmpfs);
523     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
524         /* the assembled x86 boot sector should be exactly one sector large */
525         assert(sizeof(x86_bootsect) == 512);
526         init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect));
527         memory_size = "150M";
528         arch_source = g_strdup_printf("-drive file=%s,format=raw", bootpath);
529         arch_target = g_strdup(arch_source);
530         start_address = X86_TEST_MEM_START;
531         end_address = X86_TEST_MEM_END;
532     } else if (g_str_equal(arch, "s390x")) {
533         init_bootfile(bootpath, s390x_elf, sizeof(s390x_elf));
534         memory_size = "128M";
535         arch_source = g_strdup_printf("-bios %s", bootpath);
536         arch_target = g_strdup(arch_source);
537         start_address = S390_TEST_MEM_START;
538         end_address = S390_TEST_MEM_END;
539     } else if (strcmp(arch, "ppc64") == 0) {
540         machine_opts = "vsmt=8";
541         memory_size = "256M";
542         start_address = PPC_TEST_MEM_START;
543         end_address = PPC_TEST_MEM_END;
544         arch_source = g_strdup_printf("-nodefaults "
545                                       "-prom-env 'use-nvramrc?=true' -prom-env "
546                                       "'nvramrc=hex .\" _\" begin %x %x "
547                                       "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
548                                       "until'", end_address, start_address);
549         arch_target = g_strdup("");
550     } else if (strcmp(arch, "aarch64") == 0) {
551         init_bootfile(bootpath, aarch64_kernel, sizeof(aarch64_kernel));
552         machine_opts = "virt,gic-version=max";
553         memory_size = "150M";
554         arch_source = g_strdup_printf("-cpu max "
555                                       "-kernel %s",
556                                       bootpath);
557         arch_target = g_strdup(arch_source);
558         start_address = ARM_TEST_MEM_START;
559         end_address = ARM_TEST_MEM_END;
560 
561         g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
562     } else {
563         g_assert_not_reached();
564     }
565 
566     if (!getenv("QTEST_LOG") && args->hide_stderr) {
567         ignore_stderr = "2>/dev/null";
568     } else {
569         ignore_stderr = "";
570     }
571 
572     if (args->use_shmem) {
573         shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
574         shmem_opts = g_strdup_printf(
575             "-object memory-backend-file,id=mem0,size=%s"
576             ",mem-path=%s,share=on -numa node,memdev=mem0",
577             memory_size, shmem_path);
578     } else {
579         shmem_path = NULL;
580         shmem_opts = g_strdup("");
581     }
582 
583     cmd_source = g_strdup_printf("-accel kvm%s -accel tcg%s%s "
584                                  "-name source,debug-threads=on "
585                                  "-m %s "
586                                  "-serial file:%s/src_serial "
587                                  "%s %s %s %s",
588                                  args->use_dirty_ring ?
589                                  ",dirty-ring-size=4096" : "",
590                                  machine_opts ? " -machine " : "",
591                                  machine_opts ? machine_opts : "",
592                                  memory_size, tmpfs,
593                                  arch_source, shmem_opts, args->opts_source,
594                                  ignore_stderr);
595     if (!args->only_target) {
596         *from = qtest_init(cmd_source);
597     }
598 
599     cmd_target = g_strdup_printf("-accel kvm%s -accel tcg%s%s "
600                                  "-name target,debug-threads=on "
601                                  "-m %s "
602                                  "-serial file:%s/dest_serial "
603                                  "-incoming %s "
604                                  "%s %s %s %s",
605                                  args->use_dirty_ring ?
606                                  ",dirty-ring-size=4096" : "",
607                                  machine_opts ? " -machine " : "",
608                                  machine_opts ? machine_opts : "",
609                                  memory_size, tmpfs, uri,
610                                  arch_target, shmem_opts,
611                                  args->opts_target, ignore_stderr);
612     *to = qtest_init(cmd_target);
613 
614     /*
615      * Remove shmem file immediately to avoid memory leak in test failed case.
616      * It's valid becase QEMU has already opened this file
617      */
618     if (args->use_shmem) {
619         unlink(shmem_path);
620     }
621 
622 out:
623     migrate_start_destroy(args);
624     return ret;
625 }
626 
627 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
628 {
629     unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
630 
631     qtest_quit(from);
632 
633     if (test_dest) {
634         qtest_memread(to, start_address, &dest_byte_a, 1);
635 
636         /* Destination still running, wait for a byte to change */
637         do {
638             qtest_memread(to, start_address, &dest_byte_b, 1);
639             usleep(1000 * 10);
640         } while (dest_byte_a == dest_byte_b);
641 
642         qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
643 
644         /* With it stopped, check nothing changes */
645         qtest_memread(to, start_address, &dest_byte_c, 1);
646         usleep(1000 * 200);
647         qtest_memread(to, start_address, &dest_byte_d, 1);
648         g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
649 
650         check_guests_ram(to);
651     }
652 
653     qtest_quit(to);
654 
655     cleanup("bootsect");
656     cleanup("migsocket");
657     cleanup("src_serial");
658     cleanup("dest_serial");
659 }
660 
661 static int migrate_postcopy_prepare(QTestState **from_ptr,
662                                     QTestState **to_ptr,
663                                     MigrateStart *args)
664 {
665     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
666     QTestState *from, *to;
667 
668     if (test_migrate_start(&from, &to, uri, args)) {
669         return -1;
670     }
671 
672     migrate_set_capability(from, "postcopy-ram", true);
673     migrate_set_capability(to, "postcopy-ram", true);
674     migrate_set_capability(to, "postcopy-blocktime", true);
675 
676     /* We want to pick a speed slow enough that the test completes
677      * quickly, but that it doesn't complete precopy even on a slow
678      * machine, so also set the downtime.
679      */
680     migrate_set_parameter_int(from, "max-bandwidth", 30000000);
681     migrate_set_parameter_int(from, "downtime-limit", 1);
682 
683     /* Wait for the first serial output from the source */
684     wait_for_serial("src_serial");
685 
686     migrate_qmp(from, uri, "{}");
687 
688     wait_for_migration_pass(from);
689 
690     *from_ptr = from;
691     *to_ptr = to;
692 
693     return 0;
694 }
695 
696 static void migrate_postcopy_complete(QTestState *from, QTestState *to)
697 {
698     wait_for_migration_complete(from);
699 
700     /* Make sure we get at least one "B" on destination */
701     wait_for_serial("dest_serial");
702 
703     if (uffd_feature_thread_id) {
704         read_blocktime(to);
705     }
706 
707     test_migrate_end(from, to, true);
708 }
709 
710 static void test_postcopy(void)
711 {
712     MigrateStart *args = migrate_start_new();
713     QTestState *from, *to;
714 
715     if (migrate_postcopy_prepare(&from, &to, args)) {
716         return;
717     }
718     migrate_postcopy_start(from, to);
719     migrate_postcopy_complete(from, to);
720 }
721 
722 static void test_postcopy_recovery(void)
723 {
724     MigrateStart *args = migrate_start_new();
725     QTestState *from, *to;
726     g_autofree char *uri = NULL;
727 
728     args->hide_stderr = true;
729 
730     if (migrate_postcopy_prepare(&from, &to, args)) {
731         return;
732     }
733 
734     /* Turn postcopy speed down, 4K/s is slow enough on any machines */
735     migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
736 
737     /* Now we start the postcopy */
738     migrate_postcopy_start(from, to);
739 
740     /*
741      * Wait until postcopy is really started; we can only run the
742      * migrate-pause command during a postcopy
743      */
744     wait_for_migration_status(from, "postcopy-active", NULL);
745 
746     /*
747      * Manually stop the postcopy migration. This emulates a network
748      * failure with the migration socket
749      */
750     migrate_pause(from);
751 
752     /*
753      * Wait for destination side to reach postcopy-paused state.  The
754      * migrate-recover command can only succeed if destination machine
755      * is in the paused state
756      */
757     wait_for_migration_status(to, "postcopy-paused",
758                               (const char * []) { "failed", "active",
759                                                   "completed", NULL });
760 
761     /*
762      * Create a new socket to emulate a new channel that is different
763      * from the broken migration channel; tell the destination to
764      * listen to the new port
765      */
766     uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
767     migrate_recover(to, uri);
768 
769     /*
770      * Try to rebuild the migration channel using the resume flag and
771      * the newly created channel
772      */
773     wait_for_migration_status(from, "postcopy-paused",
774                               (const char * []) { "failed", "active",
775                                                   "completed", NULL });
776     migrate_qmp(from, uri, "{'resume': true}");
777 
778     /* Restore the postcopy bandwidth to unlimited */
779     migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
780 
781     migrate_postcopy_complete(from, to);
782 }
783 
784 static void test_baddest(void)
785 {
786     MigrateStart *args = migrate_start_new();
787     QTestState *from, *to;
788 
789     args->hide_stderr = true;
790 
791     if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", args)) {
792         return;
793     }
794     migrate_qmp(from, "tcp:127.0.0.1:0", "{}");
795     wait_for_migration_fail(from, false);
796     test_migrate_end(from, to, false);
797 }
798 
799 static void test_precopy_unix_common(bool dirty_ring)
800 {
801     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
802     MigrateStart *args = migrate_start_new();
803     QTestState *from, *to;
804 
805     args->use_dirty_ring = dirty_ring;
806 
807     if (test_migrate_start(&from, &to, uri, args)) {
808         return;
809     }
810 
811     /* We want to pick a speed slow enough that the test completes
812      * quickly, but that it doesn't complete precopy even on a slow
813      * machine, so also set the downtime.
814      */
815     /* 1 ms should make it not converge*/
816     migrate_set_parameter_int(from, "downtime-limit", 1);
817     /* 1GB/s */
818     migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
819 
820     /* Wait for the first serial output from the source */
821     wait_for_serial("src_serial");
822 
823     migrate_qmp(from, uri, "{}");
824 
825     wait_for_migration_pass(from);
826 
827     migrate_set_parameter_int(from, "downtime-limit", CONVERGE_DOWNTIME);
828 
829     if (!got_stop) {
830         qtest_qmp_eventwait(from, "STOP");
831     }
832 
833     qtest_qmp_eventwait(to, "RESUME");
834 
835     wait_for_serial("dest_serial");
836     wait_for_migration_complete(from);
837 
838     test_migrate_end(from, to, true);
839 }
840 
841 static void test_precopy_unix(void)
842 {
843     /* Using default dirty logging */
844     test_precopy_unix_common(false);
845 }
846 
847 static void test_precopy_unix_dirty_ring(void)
848 {
849     /* Using dirty ring tracking */
850     test_precopy_unix_common(true);
851 }
852 
853 #if 0
854 /* Currently upset on aarch64 TCG */
855 static void test_ignore_shared(void)
856 {
857     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
858     QTestState *from, *to;
859 
860     if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) {
861         return;
862     }
863 
864     migrate_set_capability(from, "x-ignore-shared", true);
865     migrate_set_capability(to, "x-ignore-shared", true);
866 
867     /* Wait for the first serial output from the source */
868     wait_for_serial("src_serial");
869 
870     migrate_qmp(from, uri, "{}");
871 
872     wait_for_migration_pass(from);
873 
874     if (!got_stop) {
875         qtest_qmp_eventwait(from, "STOP");
876     }
877 
878     qtest_qmp_eventwait(to, "RESUME");
879 
880     wait_for_serial("dest_serial");
881     wait_for_migration_complete(from);
882 
883     /* Check whether shared RAM has been really skipped */
884     g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
885 
886     test_migrate_end(from, to, true);
887 }
888 #endif
889 
890 static void test_xbzrle(const char *uri)
891 {
892     MigrateStart *args = migrate_start_new();
893     QTestState *from, *to;
894 
895     if (test_migrate_start(&from, &to, uri, args)) {
896         return;
897     }
898 
899     /*
900      * We want to pick a speed slow enough that the test completes
901      * quickly, but that it doesn't complete precopy even on a slow
902      * machine, so also set the downtime.
903      */
904     /* 1 ms should make it not converge*/
905     migrate_set_parameter_int(from, "downtime-limit", 1);
906     /* 1GB/s */
907     migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
908 
909     migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
910 
911     migrate_set_capability(from, "xbzrle", true);
912     migrate_set_capability(to, "xbzrle", true);
913     /* Wait for the first serial output from the source */
914     wait_for_serial("src_serial");
915 
916     migrate_qmp(from, uri, "{}");
917 
918     wait_for_migration_pass(from);
919     /* Make sure we have 2 passes, so the xbzrle cache gets a workout */
920     wait_for_migration_pass(from);
921 
922     /* 1000ms should converge */
923     migrate_set_parameter_int(from, "downtime-limit", 1000);
924 
925     if (!got_stop) {
926         qtest_qmp_eventwait(from, "STOP");
927     }
928     qtest_qmp_eventwait(to, "RESUME");
929 
930     wait_for_serial("dest_serial");
931     wait_for_migration_complete(from);
932 
933     test_migrate_end(from, to, true);
934 }
935 
936 static void test_xbzrle_unix(void)
937 {
938     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
939 
940     test_xbzrle(uri);
941 }
942 
943 static void test_precopy_tcp(void)
944 {
945     MigrateStart *args = migrate_start_new();
946     g_autofree char *uri = NULL;
947     QTestState *from, *to;
948 
949     if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", args)) {
950         return;
951     }
952 
953     /*
954      * We want to pick a speed slow enough that the test completes
955      * quickly, but that it doesn't complete precopy even on a slow
956      * machine, so also set the downtime.
957      */
958     /* 1 ms should make it not converge*/
959     migrate_set_parameter_int(from, "downtime-limit", 1);
960     /* 1GB/s */
961     migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
962 
963     /* Wait for the first serial output from the source */
964     wait_for_serial("src_serial");
965 
966     uri = migrate_get_socket_address(to, "socket-address");
967 
968     migrate_qmp(from, uri, "{}");
969 
970     wait_for_migration_pass(from);
971 
972     migrate_set_parameter_int(from, "downtime-limit", CONVERGE_DOWNTIME);
973 
974     if (!got_stop) {
975         qtest_qmp_eventwait(from, "STOP");
976     }
977     qtest_qmp_eventwait(to, "RESUME");
978 
979     wait_for_serial("dest_serial");
980     wait_for_migration_complete(from);
981 
982     test_migrate_end(from, to, true);
983 }
984 
985 static void test_migrate_fd_proto(void)
986 {
987     MigrateStart *args = migrate_start_new();
988     QTestState *from, *to;
989     int ret;
990     int pair[2];
991     QDict *rsp;
992     const char *error_desc;
993 
994     if (test_migrate_start(&from, &to, "defer", args)) {
995         return;
996     }
997 
998     /*
999      * We want to pick a speed slow enough that the test completes
1000      * quickly, but that it doesn't complete precopy even on a slow
1001      * machine, so also set the downtime.
1002      */
1003     /* 1 ms should make it not converge */
1004     migrate_set_parameter_int(from, "downtime-limit", 1);
1005     /* 1GB/s */
1006     migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
1007 
1008     /* Wait for the first serial output from the source */
1009     wait_for_serial("src_serial");
1010 
1011     /* Create two connected sockets for migration */
1012     ret = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
1013     g_assert_cmpint(ret, ==, 0);
1014 
1015     /* Send the 1st socket to the target */
1016     rsp = wait_command_fd(to, pair[0],
1017                           "{ 'execute': 'getfd',"
1018                           "  'arguments': { 'fdname': 'fd-mig' }}");
1019     qobject_unref(rsp);
1020     close(pair[0]);
1021 
1022     /* Start incoming migration from the 1st socket */
1023     rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1024                            "  'arguments': { 'uri': 'fd:fd-mig' }}");
1025     qobject_unref(rsp);
1026 
1027     /* Send the 2nd socket to the target */
1028     rsp = wait_command_fd(from, pair[1],
1029                           "{ 'execute': 'getfd',"
1030                           "  'arguments': { 'fdname': 'fd-mig' }}");
1031     qobject_unref(rsp);
1032     close(pair[1]);
1033 
1034     /* Start migration to the 2nd socket*/
1035     migrate_qmp(from, "fd:fd-mig", "{}");
1036 
1037     wait_for_migration_pass(from);
1038 
1039     migrate_set_parameter_int(from, "downtime-limit", CONVERGE_DOWNTIME);
1040 
1041     if (!got_stop) {
1042         qtest_qmp_eventwait(from, "STOP");
1043     }
1044     qtest_qmp_eventwait(to, "RESUME");
1045 
1046     /* Test closing fds */
1047     /* We assume, that QEMU removes named fd from its list,
1048      * so this should fail */
1049     rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
1050                           "  'arguments': { 'fdname': 'fd-mig' }}");
1051     g_assert_true(qdict_haskey(rsp, "error"));
1052     error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1053     g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1054     qobject_unref(rsp);
1055 
1056     rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
1057                         "  'arguments': { 'fdname': 'fd-mig' }}");
1058     g_assert_true(qdict_haskey(rsp, "error"));
1059     error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1060     g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1061     qobject_unref(rsp);
1062 
1063     /* Complete migration */
1064     wait_for_serial("dest_serial");
1065     wait_for_migration_complete(from);
1066     test_migrate_end(from, to, true);
1067 }
1068 
1069 static void do_test_validate_uuid(MigrateStart *args, bool should_fail)
1070 {
1071     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1072     QTestState *from, *to;
1073 
1074     if (test_migrate_start(&from, &to, uri, args)) {
1075         return;
1076     }
1077 
1078     /*
1079      * UUID validation is at the begin of migration. So, the main process of
1080      * migration is not interesting for us here. Thus, set huge downtime for
1081      * very fast migration.
1082      */
1083     migrate_set_parameter_int(from, "downtime-limit", 1000000);
1084     migrate_set_capability(from, "validate-uuid", true);
1085 
1086     /* Wait for the first serial output from the source */
1087     wait_for_serial("src_serial");
1088 
1089     migrate_qmp(from, uri, "{}");
1090 
1091     if (should_fail) {
1092         qtest_set_expected_status(to, 1);
1093         wait_for_migration_fail(from, true);
1094     } else {
1095         wait_for_migration_complete(from);
1096     }
1097 
1098     test_migrate_end(from, to, false);
1099 }
1100 
1101 static void test_validate_uuid(void)
1102 {
1103     MigrateStart *args = migrate_start_new();
1104 
1105     g_free(args->opts_source);
1106     g_free(args->opts_target);
1107     args->opts_source = g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1108     args->opts_target = g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1109     do_test_validate_uuid(args, false);
1110 }
1111 
1112 static void test_validate_uuid_error(void)
1113 {
1114     MigrateStart *args = migrate_start_new();
1115 
1116     g_free(args->opts_source);
1117     g_free(args->opts_target);
1118     args->opts_source = g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1119     args->opts_target = g_strdup("-uuid 22222222-2222-2222-2222-222222222222");
1120     args->hide_stderr = true;
1121     do_test_validate_uuid(args, true);
1122 }
1123 
1124 static void test_validate_uuid_src_not_set(void)
1125 {
1126     MigrateStart *args = migrate_start_new();
1127 
1128     g_free(args->opts_target);
1129     args->opts_target = g_strdup("-uuid 22222222-2222-2222-2222-222222222222");
1130     args->hide_stderr = true;
1131     do_test_validate_uuid(args, false);
1132 }
1133 
1134 static void test_validate_uuid_dst_not_set(void)
1135 {
1136     MigrateStart *args = migrate_start_new();
1137 
1138     g_free(args->opts_source);
1139     args->opts_source = g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1140     args->hide_stderr = true;
1141     do_test_validate_uuid(args, false);
1142 }
1143 
1144 static void test_migrate_auto_converge(void)
1145 {
1146     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1147     MigrateStart *args = migrate_start_new();
1148     QTestState *from, *to;
1149     int64_t remaining, percentage;
1150 
1151     /*
1152      * We want the test to be stable and as fast as possible.
1153      * E.g., with 1Gb/s bandwith migration may pass without throttling,
1154      * so we need to decrease a bandwidth.
1155      */
1156     const int64_t init_pct = 5, inc_pct = 50, max_pct = 95;
1157     const int64_t max_bandwidth = 400000000; /* ~400Mb/s */
1158     const int64_t downtime_limit = 250; /* 250ms */
1159     /*
1160      * We migrate through unix-socket (> 500Mb/s).
1161      * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
1162      * So, we can predict expected_threshold
1163      */
1164     const int64_t expected_threshold = max_bandwidth * downtime_limit / 1000;
1165 
1166     if (test_migrate_start(&from, &to, uri, args)) {
1167         return;
1168     }
1169 
1170     migrate_set_capability(from, "auto-converge", true);
1171     migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct);
1172     migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct);
1173     migrate_set_parameter_int(from, "max-cpu-throttle", max_pct);
1174 
1175     /*
1176      * Set the initial parameters so that the migration could not converge
1177      * without throttling.
1178      */
1179     migrate_set_parameter_int(from, "downtime-limit", 1);
1180     migrate_set_parameter_int(from, "max-bandwidth", 100000000); /* ~100Mb/s */
1181 
1182     /* To check remaining size after precopy */
1183     migrate_set_capability(from, "pause-before-switchover", true);
1184 
1185     /* Wait for the first serial output from the source */
1186     wait_for_serial("src_serial");
1187 
1188     migrate_qmp(from, uri, "{}");
1189 
1190     /* Wait for throttling begins */
1191     percentage = 0;
1192     while (percentage == 0) {
1193         percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1194         usleep(100);
1195         g_assert_false(got_stop);
1196     }
1197     /* The first percentage of throttling should be equal to init_pct */
1198     g_assert_cmpint(percentage, ==, init_pct);
1199     /* Now, when we tested that throttling works, let it converge */
1200     migrate_set_parameter_int(from, "downtime-limit", downtime_limit);
1201     migrate_set_parameter_int(from, "max-bandwidth", max_bandwidth);
1202 
1203     /*
1204      * Wait for pre-switchover status to check last throttle percentage
1205      * and remaining. These values will be zeroed later
1206      */
1207     wait_for_migration_status(from, "pre-switchover", NULL);
1208 
1209     /* The final percentage of throttling shouldn't be greater than max_pct */
1210     percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1211     g_assert_cmpint(percentage, <=, max_pct);
1212 
1213     remaining = read_ram_property_int(from, "remaining");
1214     g_assert_cmpint(remaining, <,
1215                     (expected_threshold + expected_threshold / 100));
1216 
1217     migrate_continue(from, "pre-switchover");
1218 
1219     qtest_qmp_eventwait(to, "RESUME");
1220 
1221     wait_for_serial("dest_serial");
1222     wait_for_migration_complete(from);
1223 
1224 
1225     test_migrate_end(from, to, true);
1226 }
1227 
1228 static void test_multifd_tcp(const char *method)
1229 {
1230     MigrateStart *args = migrate_start_new();
1231     QTestState *from, *to;
1232     QDict *rsp;
1233     g_autofree char *uri = NULL;
1234 
1235     if (test_migrate_start(&from, &to, "defer", args)) {
1236         return;
1237     }
1238 
1239     /*
1240      * We want to pick a speed slow enough that the test completes
1241      * quickly, but that it doesn't complete precopy even on a slow
1242      * machine, so also set the downtime.
1243      */
1244     /* 1 ms should make it not converge*/
1245     migrate_set_parameter_int(from, "downtime-limit", 1);
1246     /* 1GB/s */
1247     migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
1248 
1249     migrate_set_parameter_int(from, "multifd-channels", 16);
1250     migrate_set_parameter_int(to, "multifd-channels", 16);
1251 
1252     migrate_set_parameter_str(from, "multifd-compression", method);
1253     migrate_set_parameter_str(to, "multifd-compression", method);
1254 
1255     migrate_set_capability(from, "multifd", true);
1256     migrate_set_capability(to, "multifd", true);
1257 
1258     /* Start incoming migration from the 1st socket */
1259     rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1260                            "  'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1261     qobject_unref(rsp);
1262 
1263     /* Wait for the first serial output from the source */
1264     wait_for_serial("src_serial");
1265 
1266     uri = migrate_get_socket_address(to, "socket-address");
1267 
1268     migrate_qmp(from, uri, "{}");
1269 
1270     wait_for_migration_pass(from);
1271 
1272     migrate_set_parameter_int(from, "downtime-limit", CONVERGE_DOWNTIME);
1273 
1274     if (!got_stop) {
1275         qtest_qmp_eventwait(from, "STOP");
1276     }
1277     qtest_qmp_eventwait(to, "RESUME");
1278 
1279     wait_for_serial("dest_serial");
1280     wait_for_migration_complete(from);
1281     test_migrate_end(from, to, true);
1282 }
1283 
1284 static void test_multifd_tcp_none(void)
1285 {
1286     test_multifd_tcp("none");
1287 }
1288 
1289 static void test_multifd_tcp_zlib(void)
1290 {
1291     test_multifd_tcp("zlib");
1292 }
1293 
1294 #ifdef CONFIG_ZSTD
1295 static void test_multifd_tcp_zstd(void)
1296 {
1297     test_multifd_tcp("zstd");
1298 }
1299 #endif
1300 
1301 /*
1302  * This test does:
1303  *  source               target
1304  *                       migrate_incoming
1305  *     migrate
1306  *     migrate_cancel
1307  *                       launch another target
1308  *     migrate
1309  *
1310  *  And see that it works
1311  */
1312 static void test_multifd_tcp_cancel(void)
1313 {
1314     MigrateStart *args = migrate_start_new();
1315     QTestState *from, *to, *to2;
1316     QDict *rsp;
1317     g_autofree char *uri = NULL;
1318 
1319     args->hide_stderr = true;
1320 
1321     if (test_migrate_start(&from, &to, "defer", args)) {
1322         return;
1323     }
1324 
1325     /*
1326      * We want to pick a speed slow enough that the test completes
1327      * quickly, but that it doesn't complete precopy even on a slow
1328      * machine, so also set the downtime.
1329      */
1330     /* 1 ms should make it not converge*/
1331     migrate_set_parameter_int(from, "downtime-limit", 1);
1332     /* 300MB/s */
1333     migrate_set_parameter_int(from, "max-bandwidth", 30000000);
1334 
1335     migrate_set_parameter_int(from, "multifd-channels", 16);
1336     migrate_set_parameter_int(to, "multifd-channels", 16);
1337 
1338     migrate_set_capability(from, "multifd", true);
1339     migrate_set_capability(to, "multifd", true);
1340 
1341     /* Start incoming migration from the 1st socket */
1342     rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1343                            "  'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1344     qobject_unref(rsp);
1345 
1346     /* Wait for the first serial output from the source */
1347     wait_for_serial("src_serial");
1348 
1349     uri = migrate_get_socket_address(to, "socket-address");
1350 
1351     migrate_qmp(from, uri, "{}");
1352 
1353     wait_for_migration_pass(from);
1354 
1355     migrate_cancel(from);
1356 
1357     args = migrate_start_new();
1358     args->only_target = true;
1359 
1360     if (test_migrate_start(&from, &to2, "defer", args)) {
1361         return;
1362     }
1363 
1364     migrate_set_parameter_int(to2, "multifd-channels", 16);
1365 
1366     migrate_set_capability(to2, "multifd", true);
1367 
1368     /* Start incoming migration from the 1st socket */
1369     rsp = wait_command(to2, "{ 'execute': 'migrate-incoming',"
1370                             "  'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1371     qobject_unref(rsp);
1372 
1373     g_free(uri);
1374     uri = migrate_get_socket_address(to2, "socket-address");
1375 
1376     wait_for_migration_status(from, "cancelled", NULL);
1377 
1378     /* 300ms it should converge */
1379     migrate_set_parameter_int(from, "downtime-limit", 300);
1380     /* 1GB/s */
1381     migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
1382 
1383     migrate_qmp(from, uri, "{}");
1384 
1385     wait_for_migration_pass(from);
1386 
1387     if (!got_stop) {
1388         qtest_qmp_eventwait(from, "STOP");
1389     }
1390     qtest_qmp_eventwait(to2, "RESUME");
1391 
1392     wait_for_serial("dest_serial");
1393     wait_for_migration_complete(from);
1394     test_migrate_end(from, to2, true);
1395 }
1396 
1397 static bool kvm_dirty_ring_supported(void)
1398 {
1399 #if defined(__linux__) && defined(HOST_X86_64)
1400     int ret, kvm_fd = open("/dev/kvm", O_RDONLY);
1401 
1402     if (kvm_fd < 0) {
1403         return false;
1404     }
1405 
1406     ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, KVM_CAP_DIRTY_LOG_RING);
1407     close(kvm_fd);
1408 
1409     /* We test with 4096 slots */
1410     if (ret < 4096) {
1411         return false;
1412     }
1413 
1414     return true;
1415 #else
1416     return false;
1417 #endif
1418 }
1419 
1420 int main(int argc, char **argv)
1421 {
1422     char template[] = "/tmp/migration-test-XXXXXX";
1423     const bool has_kvm = qtest_has_accel("kvm");
1424     int ret;
1425 
1426     g_test_init(&argc, &argv, NULL);
1427 
1428     if (!ufd_version_check()) {
1429         return g_test_run();
1430     }
1431 
1432     /*
1433      * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
1434      * is touchy due to race conditions on dirty bits (especially on PPC for
1435      * some reason)
1436      */
1437     if (g_str_equal(qtest_get_arch(), "ppc64") &&
1438         (!has_kvm || access("/sys/module/kvm_hv", F_OK))) {
1439         g_test_message("Skipping test: kvm_hv not available");
1440         return g_test_run();
1441     }
1442 
1443     /*
1444      * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
1445      * there until the problems are resolved
1446      */
1447     if (g_str_equal(qtest_get_arch(), "s390x") && !has_kvm) {
1448         g_test_message("Skipping test: s390x host with KVM is required");
1449         return g_test_run();
1450     }
1451 
1452     tmpfs = mkdtemp(template);
1453     if (!tmpfs) {
1454         g_test_message("mkdtemp on path (%s): %s", template, strerror(errno));
1455     }
1456     g_assert(tmpfs);
1457 
1458     module_call_init(MODULE_INIT_QOM);
1459 
1460     qtest_add_func("/migration/postcopy/unix", test_postcopy);
1461     qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery);
1462     qtest_add_func("/migration/bad_dest", test_baddest);
1463     qtest_add_func("/migration/precopy/unix", test_precopy_unix);
1464     qtest_add_func("/migration/precopy/tcp", test_precopy_tcp);
1465     /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
1466     qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix);
1467     qtest_add_func("/migration/fd_proto", test_migrate_fd_proto);
1468     qtest_add_func("/migration/validate_uuid", test_validate_uuid);
1469     qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error);
1470     qtest_add_func("/migration/validate_uuid_src_not_set",
1471                    test_validate_uuid_src_not_set);
1472     qtest_add_func("/migration/validate_uuid_dst_not_set",
1473                    test_validate_uuid_dst_not_set);
1474 
1475     qtest_add_func("/migration/auto_converge", test_migrate_auto_converge);
1476     qtest_add_func("/migration/multifd/tcp/none", test_multifd_tcp_none);
1477     qtest_add_func("/migration/multifd/tcp/cancel", test_multifd_tcp_cancel);
1478     qtest_add_func("/migration/multifd/tcp/zlib", test_multifd_tcp_zlib);
1479 #ifdef CONFIG_ZSTD
1480     qtest_add_func("/migration/multifd/tcp/zstd", test_multifd_tcp_zstd);
1481 #endif
1482 
1483     if (kvm_dirty_ring_supported()) {
1484         qtest_add_func("/migration/dirty_ring",
1485                        test_precopy_unix_dirty_ring);
1486     }
1487 
1488     ret = g_test_run();
1489 
1490     g_assert_cmpint(ret, ==, 0);
1491 
1492     ret = rmdir(tmpfs);
1493     if (ret != 0) {
1494         g_test_message("unable to rmdir: path (%s): %s",
1495                        tmpfs, strerror(errno));
1496     }
1497 
1498     return ret;
1499 }
1500