xref: /openbmc/qemu/tests/qtest/migration-test.c (revision 0ba4e76b)
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 **pargs)
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     MigrateStart *args = *pargs;
511     const char *memory_size;
512     int ret = 0;
513 
514     if (args->use_shmem) {
515         if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
516             g_test_skip("/dev/shm is not supported");
517             ret = -1;
518             goto out;
519         }
520     }
521 
522     got_stop = false;
523     bootpath = g_strdup_printf("%s/bootsect", tmpfs);
524     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
525         /* the assembled x86 boot sector should be exactly one sector large */
526         assert(sizeof(x86_bootsect) == 512);
527         init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect));
528         memory_size = "150M";
529         arch_source = g_strdup_printf("-drive file=%s,format=raw", bootpath);
530         arch_target = g_strdup(arch_source);
531         start_address = X86_TEST_MEM_START;
532         end_address = X86_TEST_MEM_END;
533     } else if (g_str_equal(arch, "s390x")) {
534         init_bootfile(bootpath, s390x_elf, sizeof(s390x_elf));
535         memory_size = "128M";
536         arch_source = g_strdup_printf("-bios %s", bootpath);
537         arch_target = g_strdup(arch_source);
538         start_address = S390_TEST_MEM_START;
539         end_address = S390_TEST_MEM_END;
540     } else if (strcmp(arch, "ppc64") == 0) {
541         machine_opts = "vsmt=8";
542         memory_size = "256M";
543         start_address = PPC_TEST_MEM_START;
544         end_address = PPC_TEST_MEM_END;
545         arch_source = g_strdup_printf("-nodefaults "
546                                       "-prom-env 'use-nvramrc?=true' -prom-env "
547                                       "'nvramrc=hex .\" _\" begin %x %x "
548                                       "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
549                                       "until'", end_address, start_address);
550         arch_target = g_strdup("");
551     } else if (strcmp(arch, "aarch64") == 0) {
552         init_bootfile(bootpath, aarch64_kernel, sizeof(aarch64_kernel));
553         machine_opts = "virt,gic-version=max";
554         memory_size = "150M";
555         arch_source = g_strdup_printf("-cpu max "
556                                       "-kernel %s",
557                                       bootpath);
558         arch_target = g_strdup(arch_source);
559         start_address = ARM_TEST_MEM_START;
560         end_address = ARM_TEST_MEM_END;
561 
562         g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
563     } else {
564         g_assert_not_reached();
565     }
566 
567     if (!getenv("QTEST_LOG") && args->hide_stderr) {
568         ignore_stderr = "2>/dev/null";
569     } else {
570         ignore_stderr = "";
571     }
572 
573     if (args->use_shmem) {
574         shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
575         shmem_opts = g_strdup_printf(
576             "-object memory-backend-file,id=mem0,size=%s"
577             ",mem-path=%s,share=on -numa node,memdev=mem0",
578             memory_size, shmem_path);
579     } else {
580         shmem_path = NULL;
581         shmem_opts = g_strdup("");
582     }
583 
584     cmd_source = g_strdup_printf("-accel kvm%s -accel tcg%s%s "
585                                  "-name source,debug-threads=on "
586                                  "-m %s "
587                                  "-serial file:%s/src_serial "
588                                  "%s %s %s %s",
589                                  args->use_dirty_ring ?
590                                  ",dirty-ring-size=4096" : "",
591                                  machine_opts ? " -machine " : "",
592                                  machine_opts ? machine_opts : "",
593                                  memory_size, tmpfs,
594                                  arch_source, shmem_opts, args->opts_source,
595                                  ignore_stderr);
596     if (!args->only_target) {
597         *from = qtest_init(cmd_source);
598     }
599 
600     cmd_target = g_strdup_printf("-accel kvm%s -accel tcg%s%s "
601                                  "-name target,debug-threads=on "
602                                  "-m %s "
603                                  "-serial file:%s/dest_serial "
604                                  "-incoming %s "
605                                  "%s %s %s %s",
606                                  args->use_dirty_ring ?
607                                  ",dirty-ring-size=4096" : "",
608                                  machine_opts ? " -machine " : "",
609                                  machine_opts ? machine_opts : "",
610                                  memory_size, tmpfs, uri,
611                                  arch_target, shmem_opts,
612                                  args->opts_target, ignore_stderr);
613     *to = qtest_init(cmd_target);
614 
615     /*
616      * Remove shmem file immediately to avoid memory leak in test failed case.
617      * It's valid becase QEMU has already opened this file
618      */
619     if (args->use_shmem) {
620         unlink(shmem_path);
621     }
622 
623 out:
624     migrate_start_destroy(args);
625     /* This tells the caller that this structure is gone */
626     *pargs = NULL;
627     return ret;
628 }
629 
630 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
631 {
632     unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
633 
634     qtest_quit(from);
635 
636     if (test_dest) {
637         qtest_memread(to, start_address, &dest_byte_a, 1);
638 
639         /* Destination still running, wait for a byte to change */
640         do {
641             qtest_memread(to, start_address, &dest_byte_b, 1);
642             usleep(1000 * 10);
643         } while (dest_byte_a == dest_byte_b);
644 
645         qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
646 
647         /* With it stopped, check nothing changes */
648         qtest_memread(to, start_address, &dest_byte_c, 1);
649         usleep(1000 * 200);
650         qtest_memread(to, start_address, &dest_byte_d, 1);
651         g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
652 
653         check_guests_ram(to);
654     }
655 
656     qtest_quit(to);
657 
658     cleanup("bootsect");
659     cleanup("migsocket");
660     cleanup("src_serial");
661     cleanup("dest_serial");
662 }
663 
664 static int migrate_postcopy_prepare(QTestState **from_ptr,
665                                     QTestState **to_ptr,
666                                     MigrateStart *args)
667 {
668     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
669     QTestState *from, *to;
670 
671     if (test_migrate_start(&from, &to, uri, &args)) {
672         return -1;
673     }
674 
675     migrate_set_capability(from, "postcopy-ram", true);
676     migrate_set_capability(to, "postcopy-ram", true);
677     migrate_set_capability(to, "postcopy-blocktime", true);
678 
679     /* We want to pick a speed slow enough that the test completes
680      * quickly, but that it doesn't complete precopy even on a slow
681      * machine, so also set the downtime.
682      */
683     migrate_set_parameter_int(from, "max-bandwidth", 30000000);
684     migrate_set_parameter_int(from, "downtime-limit", 1);
685 
686     /* Wait for the first serial output from the source */
687     wait_for_serial("src_serial");
688 
689     migrate_qmp(from, uri, "{}");
690 
691     wait_for_migration_pass(from);
692 
693     *from_ptr = from;
694     *to_ptr = to;
695 
696     return 0;
697 }
698 
699 static void migrate_postcopy_complete(QTestState *from, QTestState *to)
700 {
701     wait_for_migration_complete(from);
702 
703     /* Make sure we get at least one "B" on destination */
704     wait_for_serial("dest_serial");
705 
706     if (uffd_feature_thread_id) {
707         read_blocktime(to);
708     }
709 
710     test_migrate_end(from, to, true);
711 }
712 
713 static void test_postcopy(void)
714 {
715     MigrateStart *args = migrate_start_new();
716     QTestState *from, *to;
717 
718     if (migrate_postcopy_prepare(&from, &to, args)) {
719         return;
720     }
721     migrate_postcopy_start(from, to);
722     migrate_postcopy_complete(from, to);
723 }
724 
725 static void test_postcopy_recovery(void)
726 {
727     MigrateStart *args = migrate_start_new();
728     QTestState *from, *to;
729     g_autofree char *uri = NULL;
730 
731     args->hide_stderr = true;
732 
733     if (migrate_postcopy_prepare(&from, &to, args)) {
734         return;
735     }
736 
737     /* Turn postcopy speed down, 4K/s is slow enough on any machines */
738     migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
739 
740     /* Now we start the postcopy */
741     migrate_postcopy_start(from, to);
742 
743     /*
744      * Wait until postcopy is really started; we can only run the
745      * migrate-pause command during a postcopy
746      */
747     wait_for_migration_status(from, "postcopy-active", NULL);
748 
749     /*
750      * Manually stop the postcopy migration. This emulates a network
751      * failure with the migration socket
752      */
753     migrate_pause(from);
754 
755     /*
756      * Wait for destination side to reach postcopy-paused state.  The
757      * migrate-recover command can only succeed if destination machine
758      * is in the paused state
759      */
760     wait_for_migration_status(to, "postcopy-paused",
761                               (const char * []) { "failed", "active",
762                                                   "completed", NULL });
763 
764     /*
765      * Create a new socket to emulate a new channel that is different
766      * from the broken migration channel; tell the destination to
767      * listen to the new port
768      */
769     uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
770     migrate_recover(to, uri);
771 
772     /*
773      * Try to rebuild the migration channel using the resume flag and
774      * the newly created channel
775      */
776     wait_for_migration_status(from, "postcopy-paused",
777                               (const char * []) { "failed", "active",
778                                                   "completed", NULL });
779     migrate_qmp(from, uri, "{'resume': true}");
780 
781     /* Restore the postcopy bandwidth to unlimited */
782     migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
783 
784     migrate_postcopy_complete(from, to);
785 }
786 
787 static void test_baddest(void)
788 {
789     MigrateStart *args = migrate_start_new();
790     QTestState *from, *to;
791 
792     args->hide_stderr = true;
793 
794     if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", &args)) {
795         return;
796     }
797     migrate_qmp(from, "tcp:127.0.0.1:0", "{}");
798     wait_for_migration_fail(from, false);
799     test_migrate_end(from, to, false);
800 }
801 
802 static void test_precopy_unix_common(bool dirty_ring)
803 {
804     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
805     MigrateStart *args = migrate_start_new();
806     QTestState *from, *to;
807 
808     args->use_dirty_ring = dirty_ring;
809 
810     if (test_migrate_start(&from, &to, uri, &args)) {
811         return;
812     }
813 
814     /* We want to pick a speed slow enough that the test completes
815      * quickly, but that it doesn't complete precopy even on a slow
816      * machine, so also set the downtime.
817      */
818     /* 1 ms should make it not converge*/
819     migrate_set_parameter_int(from, "downtime-limit", 1);
820     /* 1GB/s */
821     migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
822 
823     /* Wait for the first serial output from the source */
824     wait_for_serial("src_serial");
825 
826     migrate_qmp(from, uri, "{}");
827 
828     wait_for_migration_pass(from);
829 
830     migrate_set_parameter_int(from, "downtime-limit", CONVERGE_DOWNTIME);
831 
832     if (!got_stop) {
833         qtest_qmp_eventwait(from, "STOP");
834     }
835 
836     qtest_qmp_eventwait(to, "RESUME");
837 
838     wait_for_serial("dest_serial");
839     wait_for_migration_complete(from);
840 
841     test_migrate_end(from, to, true);
842 }
843 
844 static void test_precopy_unix(void)
845 {
846     /* Using default dirty logging */
847     test_precopy_unix_common(false);
848 }
849 
850 static void test_precopy_unix_dirty_ring(void)
851 {
852     /* Using dirty ring tracking */
853     test_precopy_unix_common(true);
854 }
855 
856 #if 0
857 /* Currently upset on aarch64 TCG */
858 static void test_ignore_shared(void)
859 {
860     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
861     QTestState *from, *to;
862 
863     if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) {
864         return;
865     }
866 
867     migrate_set_capability(from, "x-ignore-shared", true);
868     migrate_set_capability(to, "x-ignore-shared", true);
869 
870     /* Wait for the first serial output from the source */
871     wait_for_serial("src_serial");
872 
873     migrate_qmp(from, uri, "{}");
874 
875     wait_for_migration_pass(from);
876 
877     if (!got_stop) {
878         qtest_qmp_eventwait(from, "STOP");
879     }
880 
881     qtest_qmp_eventwait(to, "RESUME");
882 
883     wait_for_serial("dest_serial");
884     wait_for_migration_complete(from);
885 
886     /* Check whether shared RAM has been really skipped */
887     g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
888 
889     test_migrate_end(from, to, true);
890 }
891 #endif
892 
893 static void test_xbzrle(const char *uri)
894 {
895     MigrateStart *args = migrate_start_new();
896     QTestState *from, *to;
897 
898     if (test_migrate_start(&from, &to, uri, &args)) {
899         return;
900     }
901 
902     /*
903      * We want to pick a speed slow enough that the test completes
904      * quickly, but that it doesn't complete precopy even on a slow
905      * machine, so also set the downtime.
906      */
907     /* 1 ms should make it not converge*/
908     migrate_set_parameter_int(from, "downtime-limit", 1);
909     /* 1GB/s */
910     migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
911 
912     migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
913 
914     migrate_set_capability(from, "xbzrle", true);
915     migrate_set_capability(to, "xbzrle", true);
916     /* Wait for the first serial output from the source */
917     wait_for_serial("src_serial");
918 
919     migrate_qmp(from, uri, "{}");
920 
921     wait_for_migration_pass(from);
922     /* Make sure we have 2 passes, so the xbzrle cache gets a workout */
923     wait_for_migration_pass(from);
924 
925     /* 1000ms should converge */
926     migrate_set_parameter_int(from, "downtime-limit", 1000);
927 
928     if (!got_stop) {
929         qtest_qmp_eventwait(from, "STOP");
930     }
931     qtest_qmp_eventwait(to, "RESUME");
932 
933     wait_for_serial("dest_serial");
934     wait_for_migration_complete(from);
935 
936     test_migrate_end(from, to, true);
937 }
938 
939 static void test_xbzrle_unix(void)
940 {
941     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
942 
943     test_xbzrle(uri);
944 }
945 
946 static void test_precopy_tcp(void)
947 {
948     MigrateStart *args = migrate_start_new();
949     g_autofree char *uri = NULL;
950     QTestState *from, *to;
951 
952     if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", &args)) {
953         return;
954     }
955 
956     /*
957      * We want to pick a speed slow enough that the test completes
958      * quickly, but that it doesn't complete precopy even on a slow
959      * machine, so also set the downtime.
960      */
961     /* 1 ms should make it not converge*/
962     migrate_set_parameter_int(from, "downtime-limit", 1);
963     /* 1GB/s */
964     migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
965 
966     /* Wait for the first serial output from the source */
967     wait_for_serial("src_serial");
968 
969     uri = migrate_get_socket_address(to, "socket-address");
970 
971     migrate_qmp(from, uri, "{}");
972 
973     wait_for_migration_pass(from);
974 
975     migrate_set_parameter_int(from, "downtime-limit", CONVERGE_DOWNTIME);
976 
977     if (!got_stop) {
978         qtest_qmp_eventwait(from, "STOP");
979     }
980     qtest_qmp_eventwait(to, "RESUME");
981 
982     wait_for_serial("dest_serial");
983     wait_for_migration_complete(from);
984 
985     test_migrate_end(from, to, true);
986 }
987 
988 static void test_migrate_fd_proto(void)
989 {
990     MigrateStart *args = migrate_start_new();
991     QTestState *from, *to;
992     int ret;
993     int pair[2];
994     QDict *rsp;
995     const char *error_desc;
996 
997     if (test_migrate_start(&from, &to, "defer", &args)) {
998         return;
999     }
1000 
1001     /*
1002      * We want to pick a speed slow enough that the test completes
1003      * quickly, but that it doesn't complete precopy even on a slow
1004      * machine, so also set the downtime.
1005      */
1006     /* 1 ms should make it not converge */
1007     migrate_set_parameter_int(from, "downtime-limit", 1);
1008     /* 1GB/s */
1009     migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
1010 
1011     /* Wait for the first serial output from the source */
1012     wait_for_serial("src_serial");
1013 
1014     /* Create two connected sockets for migration */
1015     ret = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
1016     g_assert_cmpint(ret, ==, 0);
1017 
1018     /* Send the 1st socket to the target */
1019     rsp = wait_command_fd(to, pair[0],
1020                           "{ 'execute': 'getfd',"
1021                           "  'arguments': { 'fdname': 'fd-mig' }}");
1022     qobject_unref(rsp);
1023     close(pair[0]);
1024 
1025     /* Start incoming migration from the 1st socket */
1026     rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1027                            "  'arguments': { 'uri': 'fd:fd-mig' }}");
1028     qobject_unref(rsp);
1029 
1030     /* Send the 2nd socket to the target */
1031     rsp = wait_command_fd(from, pair[1],
1032                           "{ 'execute': 'getfd',"
1033                           "  'arguments': { 'fdname': 'fd-mig' }}");
1034     qobject_unref(rsp);
1035     close(pair[1]);
1036 
1037     /* Start migration to the 2nd socket*/
1038     migrate_qmp(from, "fd:fd-mig", "{}");
1039 
1040     wait_for_migration_pass(from);
1041 
1042     migrate_set_parameter_int(from, "downtime-limit", CONVERGE_DOWNTIME);
1043 
1044     if (!got_stop) {
1045         qtest_qmp_eventwait(from, "STOP");
1046     }
1047     qtest_qmp_eventwait(to, "RESUME");
1048 
1049     /* Test closing fds */
1050     /* We assume, that QEMU removes named fd from its list,
1051      * so this should fail */
1052     rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
1053                           "  'arguments': { 'fdname': 'fd-mig' }}");
1054     g_assert_true(qdict_haskey(rsp, "error"));
1055     error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1056     g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1057     qobject_unref(rsp);
1058 
1059     rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
1060                         "  'arguments': { 'fdname': 'fd-mig' }}");
1061     g_assert_true(qdict_haskey(rsp, "error"));
1062     error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1063     g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1064     qobject_unref(rsp);
1065 
1066     /* Complete migration */
1067     wait_for_serial("dest_serial");
1068     wait_for_migration_complete(from);
1069     test_migrate_end(from, to, true);
1070 }
1071 
1072 static void do_test_validate_uuid(MigrateStart *args, bool should_fail)
1073 {
1074     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1075     QTestState *from, *to;
1076 
1077     if (test_migrate_start(&from, &to, uri, &args)) {
1078         return;
1079     }
1080 
1081     /*
1082      * UUID validation is at the begin of migration. So, the main process of
1083      * migration is not interesting for us here. Thus, set huge downtime for
1084      * very fast migration.
1085      */
1086     migrate_set_parameter_int(from, "downtime-limit", 1000000);
1087     migrate_set_capability(from, "validate-uuid", true);
1088 
1089     /* Wait for the first serial output from the source */
1090     wait_for_serial("src_serial");
1091 
1092     migrate_qmp(from, uri, "{}");
1093 
1094     if (should_fail) {
1095         qtest_set_expected_status(to, 1);
1096         wait_for_migration_fail(from, true);
1097     } else {
1098         wait_for_migration_complete(from);
1099     }
1100 
1101     test_migrate_end(from, to, false);
1102 }
1103 
1104 static void test_validate_uuid(void)
1105 {
1106     MigrateStart *args = migrate_start_new();
1107 
1108     g_free(args->opts_source);
1109     g_free(args->opts_target);
1110     args->opts_source = g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1111     args->opts_target = g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1112     do_test_validate_uuid(args, false);
1113 }
1114 
1115 static void test_validate_uuid_error(void)
1116 {
1117     MigrateStart *args = migrate_start_new();
1118 
1119     g_free(args->opts_source);
1120     g_free(args->opts_target);
1121     args->opts_source = g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1122     args->opts_target = g_strdup("-uuid 22222222-2222-2222-2222-222222222222");
1123     args->hide_stderr = true;
1124     do_test_validate_uuid(args, true);
1125 }
1126 
1127 static void test_validate_uuid_src_not_set(void)
1128 {
1129     MigrateStart *args = migrate_start_new();
1130 
1131     g_free(args->opts_target);
1132     args->opts_target = g_strdup("-uuid 22222222-2222-2222-2222-222222222222");
1133     args->hide_stderr = true;
1134     do_test_validate_uuid(args, false);
1135 }
1136 
1137 static void test_validate_uuid_dst_not_set(void)
1138 {
1139     MigrateStart *args = migrate_start_new();
1140 
1141     g_free(args->opts_source);
1142     args->opts_source = g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1143     args->hide_stderr = true;
1144     do_test_validate_uuid(args, false);
1145 }
1146 
1147 static void test_migrate_auto_converge(void)
1148 {
1149     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1150     MigrateStart *args = migrate_start_new();
1151     QTestState *from, *to;
1152     int64_t remaining, percentage;
1153 
1154     /*
1155      * We want the test to be stable and as fast as possible.
1156      * E.g., with 1Gb/s bandwith migration may pass without throttling,
1157      * so we need to decrease a bandwidth.
1158      */
1159     const int64_t init_pct = 5, inc_pct = 50, max_pct = 95;
1160     const int64_t max_bandwidth = 400000000; /* ~400Mb/s */
1161     const int64_t downtime_limit = 250; /* 250ms */
1162     /*
1163      * We migrate through unix-socket (> 500Mb/s).
1164      * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
1165      * So, we can predict expected_threshold
1166      */
1167     const int64_t expected_threshold = max_bandwidth * downtime_limit / 1000;
1168 
1169     if (test_migrate_start(&from, &to, uri, &args)) {
1170         return;
1171     }
1172 
1173     migrate_set_capability(from, "auto-converge", true);
1174     migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct);
1175     migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct);
1176     migrate_set_parameter_int(from, "max-cpu-throttle", max_pct);
1177 
1178     /*
1179      * Set the initial parameters so that the migration could not converge
1180      * without throttling.
1181      */
1182     migrate_set_parameter_int(from, "downtime-limit", 1);
1183     migrate_set_parameter_int(from, "max-bandwidth", 100000000); /* ~100Mb/s */
1184 
1185     /* To check remaining size after precopy */
1186     migrate_set_capability(from, "pause-before-switchover", true);
1187 
1188     /* Wait for the first serial output from the source */
1189     wait_for_serial("src_serial");
1190 
1191     migrate_qmp(from, uri, "{}");
1192 
1193     /* Wait for throttling begins */
1194     percentage = 0;
1195     while (percentage == 0) {
1196         percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1197         usleep(100);
1198         g_assert_false(got_stop);
1199     }
1200     /* The first percentage of throttling should be equal to init_pct */
1201     g_assert_cmpint(percentage, ==, init_pct);
1202     /* Now, when we tested that throttling works, let it converge */
1203     migrate_set_parameter_int(from, "downtime-limit", downtime_limit);
1204     migrate_set_parameter_int(from, "max-bandwidth", max_bandwidth);
1205 
1206     /*
1207      * Wait for pre-switchover status to check last throttle percentage
1208      * and remaining. These values will be zeroed later
1209      */
1210     wait_for_migration_status(from, "pre-switchover", NULL);
1211 
1212     /* The final percentage of throttling shouldn't be greater than max_pct */
1213     percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1214     g_assert_cmpint(percentage, <=, max_pct);
1215 
1216     remaining = read_ram_property_int(from, "remaining");
1217     g_assert_cmpint(remaining, <,
1218                     (expected_threshold + expected_threshold / 100));
1219 
1220     migrate_continue(from, "pre-switchover");
1221 
1222     qtest_qmp_eventwait(to, "RESUME");
1223 
1224     wait_for_serial("dest_serial");
1225     wait_for_migration_complete(from);
1226 
1227 
1228     test_migrate_end(from, to, true);
1229 }
1230 
1231 static void test_multifd_tcp(const char *method)
1232 {
1233     MigrateStart *args = migrate_start_new();
1234     QTestState *from, *to;
1235     QDict *rsp;
1236     g_autofree char *uri = NULL;
1237 
1238     if (test_migrate_start(&from, &to, "defer", &args)) {
1239         return;
1240     }
1241 
1242     /*
1243      * We want to pick a speed slow enough that the test completes
1244      * quickly, but that it doesn't complete precopy even on a slow
1245      * machine, so also set the downtime.
1246      */
1247     /* 1 ms should make it not converge*/
1248     migrate_set_parameter_int(from, "downtime-limit", 1);
1249     /* 1GB/s */
1250     migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
1251 
1252     migrate_set_parameter_int(from, "multifd-channels", 16);
1253     migrate_set_parameter_int(to, "multifd-channels", 16);
1254 
1255     migrate_set_parameter_str(from, "multifd-compression", method);
1256     migrate_set_parameter_str(to, "multifd-compression", method);
1257 
1258     migrate_set_capability(from, "multifd", true);
1259     migrate_set_capability(to, "multifd", true);
1260 
1261     /* Start incoming migration from the 1st socket */
1262     rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1263                            "  'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1264     qobject_unref(rsp);
1265 
1266     /* Wait for the first serial output from the source */
1267     wait_for_serial("src_serial");
1268 
1269     uri = migrate_get_socket_address(to, "socket-address");
1270 
1271     migrate_qmp(from, uri, "{}");
1272 
1273     wait_for_migration_pass(from);
1274 
1275     migrate_set_parameter_int(from, "downtime-limit", CONVERGE_DOWNTIME);
1276 
1277     if (!got_stop) {
1278         qtest_qmp_eventwait(from, "STOP");
1279     }
1280     qtest_qmp_eventwait(to, "RESUME");
1281 
1282     wait_for_serial("dest_serial");
1283     wait_for_migration_complete(from);
1284     test_migrate_end(from, to, true);
1285 }
1286 
1287 static void test_multifd_tcp_none(void)
1288 {
1289     test_multifd_tcp("none");
1290 }
1291 
1292 static void test_multifd_tcp_zlib(void)
1293 {
1294     test_multifd_tcp("zlib");
1295 }
1296 
1297 #ifdef CONFIG_ZSTD
1298 static void test_multifd_tcp_zstd(void)
1299 {
1300     test_multifd_tcp("zstd");
1301 }
1302 #endif
1303 
1304 /*
1305  * This test does:
1306  *  source               target
1307  *                       migrate_incoming
1308  *     migrate
1309  *     migrate_cancel
1310  *                       launch another target
1311  *     migrate
1312  *
1313  *  And see that it works
1314  */
1315 static void test_multifd_tcp_cancel(void)
1316 {
1317     MigrateStart *args = migrate_start_new();
1318     QTestState *from, *to, *to2;
1319     QDict *rsp;
1320     g_autofree char *uri = NULL;
1321 
1322     args->hide_stderr = true;
1323 
1324     if (test_migrate_start(&from, &to, "defer", &args)) {
1325         return;
1326     }
1327 
1328     /*
1329      * We want to pick a speed slow enough that the test completes
1330      * quickly, but that it doesn't complete precopy even on a slow
1331      * machine, so also set the downtime.
1332      */
1333     /* 1 ms should make it not converge*/
1334     migrate_set_parameter_int(from, "downtime-limit", 1);
1335     /* 300MB/s */
1336     migrate_set_parameter_int(from, "max-bandwidth", 30000000);
1337 
1338     migrate_set_parameter_int(from, "multifd-channels", 16);
1339     migrate_set_parameter_int(to, "multifd-channels", 16);
1340 
1341     migrate_set_capability(from, "multifd", true);
1342     migrate_set_capability(to, "multifd", true);
1343 
1344     /* Start incoming migration from the 1st socket */
1345     rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1346                            "  'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1347     qobject_unref(rsp);
1348 
1349     /* Wait for the first serial output from the source */
1350     wait_for_serial("src_serial");
1351 
1352     uri = migrate_get_socket_address(to, "socket-address");
1353 
1354     migrate_qmp(from, uri, "{}");
1355 
1356     wait_for_migration_pass(from);
1357 
1358     migrate_cancel(from);
1359 
1360     args = migrate_start_new();
1361     args->only_target = true;
1362 
1363     if (test_migrate_start(&from, &to2, "defer", &args)) {
1364         return;
1365     }
1366 
1367     migrate_set_parameter_int(to2, "multifd-channels", 16);
1368 
1369     migrate_set_capability(to2, "multifd", true);
1370 
1371     /* Start incoming migration from the 1st socket */
1372     rsp = wait_command(to2, "{ 'execute': 'migrate-incoming',"
1373                             "  'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1374     qobject_unref(rsp);
1375 
1376     g_free(uri);
1377     uri = migrate_get_socket_address(to2, "socket-address");
1378 
1379     wait_for_migration_status(from, "cancelled", NULL);
1380 
1381     /* 300ms it should converge */
1382     migrate_set_parameter_int(from, "downtime-limit", 300);
1383     /* 1GB/s */
1384     migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
1385 
1386     migrate_qmp(from, uri, "{}");
1387 
1388     wait_for_migration_pass(from);
1389 
1390     if (!got_stop) {
1391         qtest_qmp_eventwait(from, "STOP");
1392     }
1393     qtest_qmp_eventwait(to2, "RESUME");
1394 
1395     wait_for_serial("dest_serial");
1396     wait_for_migration_complete(from);
1397     test_migrate_end(from, to2, true);
1398 }
1399 
1400 static bool kvm_dirty_ring_supported(void)
1401 {
1402 #if defined(__linux__) && defined(HOST_X86_64)
1403     int ret, kvm_fd = open("/dev/kvm", O_RDONLY);
1404 
1405     if (kvm_fd < 0) {
1406         return false;
1407     }
1408 
1409     ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, KVM_CAP_DIRTY_LOG_RING);
1410     close(kvm_fd);
1411 
1412     /* We test with 4096 slots */
1413     if (ret < 4096) {
1414         return false;
1415     }
1416 
1417     return true;
1418 #else
1419     return false;
1420 #endif
1421 }
1422 
1423 int main(int argc, char **argv)
1424 {
1425     char template[] = "/tmp/migration-test-XXXXXX";
1426     const bool has_kvm = qtest_has_accel("kvm");
1427     int ret;
1428 
1429     g_test_init(&argc, &argv, NULL);
1430 
1431     if (!ufd_version_check()) {
1432         return g_test_run();
1433     }
1434 
1435     /*
1436      * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
1437      * is touchy due to race conditions on dirty bits (especially on PPC for
1438      * some reason)
1439      */
1440     if (g_str_equal(qtest_get_arch(), "ppc64") &&
1441         (!has_kvm || access("/sys/module/kvm_hv", F_OK))) {
1442         g_test_message("Skipping test: kvm_hv not available");
1443         return g_test_run();
1444     }
1445 
1446     /*
1447      * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
1448      * there until the problems are resolved
1449      */
1450     if (g_str_equal(qtest_get_arch(), "s390x") && !has_kvm) {
1451         g_test_message("Skipping test: s390x host with KVM is required");
1452         return g_test_run();
1453     }
1454 
1455     tmpfs = mkdtemp(template);
1456     if (!tmpfs) {
1457         g_test_message("mkdtemp on path (%s): %s", template, strerror(errno));
1458     }
1459     g_assert(tmpfs);
1460 
1461     module_call_init(MODULE_INIT_QOM);
1462 
1463     qtest_add_func("/migration/postcopy/unix", test_postcopy);
1464     qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery);
1465     qtest_add_func("/migration/bad_dest", test_baddest);
1466     qtest_add_func("/migration/precopy/unix", test_precopy_unix);
1467     qtest_add_func("/migration/precopy/tcp", test_precopy_tcp);
1468     /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
1469     qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix);
1470     qtest_add_func("/migration/fd_proto", test_migrate_fd_proto);
1471     qtest_add_func("/migration/validate_uuid", test_validate_uuid);
1472     qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error);
1473     qtest_add_func("/migration/validate_uuid_src_not_set",
1474                    test_validate_uuid_src_not_set);
1475     qtest_add_func("/migration/validate_uuid_dst_not_set",
1476                    test_validate_uuid_dst_not_set);
1477 
1478     qtest_add_func("/migration/auto_converge", test_migrate_auto_converge);
1479     qtest_add_func("/migration/multifd/tcp/none", test_multifd_tcp_none);
1480     qtest_add_func("/migration/multifd/tcp/cancel", test_multifd_tcp_cancel);
1481     qtest_add_func("/migration/multifd/tcp/zlib", test_multifd_tcp_zlib);
1482 #ifdef CONFIG_ZSTD
1483     qtest_add_func("/migration/multifd/tcp/zstd", test_multifd_tcp_zstd);
1484 #endif
1485 
1486     if (kvm_dirty_ring_supported()) {
1487         qtest_add_func("/migration/dirty_ring",
1488                        test_precopy_unix_dirty_ring);
1489     }
1490 
1491     ret = g_test_run();
1492 
1493     g_assert_cmpint(ret, ==, 0);
1494 
1495     ret = rmdir(tmpfs);
1496     if (ret != 0) {
1497         g_test_message("unable to rmdir: path (%s): %s",
1498                        tmpfs, strerror(errno));
1499     }
1500 
1501     return ret;
1502 }
1503