xref: /openbmc/qemu/tests/qtest/migration-test.c (revision 8053feaa)
11e8a1faeSThomas Huth /*
21e8a1faeSThomas Huth  * QTest testcase for migration
31e8a1faeSThomas Huth  *
41e8a1faeSThomas Huth  * Copyright (c) 2016-2018 Red Hat, Inc. and/or its affiliates
51e8a1faeSThomas Huth  *   based on the vhost-user-test.c that is:
61e8a1faeSThomas Huth  *      Copyright (c) 2014 Virtual Open Systems Sarl.
71e8a1faeSThomas Huth  *
81e8a1faeSThomas Huth  * This work is licensed under the terms of the GNU GPL, version 2 or later.
91e8a1faeSThomas Huth  * See the COPYING file in the top-level directory.
101e8a1faeSThomas Huth  *
111e8a1faeSThomas Huth  */
121e8a1faeSThomas Huth 
131e8a1faeSThomas Huth #include "qemu/osdep.h"
141e8a1faeSThomas Huth 
15907b5105SMarc-André Lureau #include "libqtest.h"
16d0112758SMarkus Armbruster #include "qapi/error.h"
171e8a1faeSThomas Huth #include "qapi/qmp/qdict.h"
181e8a1faeSThomas Huth #include "qemu/module.h"
191e8a1faeSThomas Huth #include "qemu/option.h"
201e8a1faeSThomas Huth #include "qemu/range.h"
211e8a1faeSThomas Huth #include "qemu/sockets.h"
221e8a1faeSThomas Huth #include "chardev/char.h"
231e8a1faeSThomas Huth #include "qapi/qapi-visit-sockets.h"
241e8a1faeSThomas Huth #include "qapi/qobject-input-visitor.h"
251e8a1faeSThomas Huth #include "qapi/qobject-output-visitor.h"
2658d25e97SDaniel P. Berrangé #include "crypto/tlscredspsk.h"
278aff6f50SHyman Huang(黄勇) #include "qapi/qmp/qlist.h"
281e8a1faeSThomas Huth 
291e8a1faeSThomas Huth #include "migration-helpers.h"
30a2ce7dbdSPaolo Bonzini #include "tests/migration/migration-test.h"
3158d25e97SDaniel P. Berrangé #ifdef CONFIG_GNUTLS
3258d25e97SDaniel P. Berrangé # include "tests/unit/crypto-tls-psk-helpers.h"
33d47b83b1SDaniel P. Berrangé # ifdef CONFIG_TASN1
34d47b83b1SDaniel P. Berrangé #  include "tests/unit/crypto-tls-x509-helpers.h"
35d47b83b1SDaniel P. Berrangé # endif /* CONFIG_TASN1 */
3658d25e97SDaniel P. Berrangé #endif /* CONFIG_GNUTLS */
371e8a1faeSThomas Huth 
3861c32485SPeter Xu /* For dirty ring test; so far only x86_64 is supported */
3961c32485SPeter Xu #if defined(__linux__) && defined(HOST_X86_64)
401f546b70SPeter Xu #include "linux/kvm.h"
411f546b70SPeter Xu #endif
421f546b70SPeter Xu 
431e8a1faeSThomas Huth unsigned start_address;
441e8a1faeSThomas Huth unsigned end_address;
451e8a1faeSThomas Huth static bool uffd_feature_thread_id;
4695014994SDaniel P. Berrangé static bool got_src_stop;
4795014994SDaniel P. Berrangé static bool got_dst_resume;
481e8a1faeSThomas Huth 
498aff6f50SHyman Huang(黄勇) /*
50e02f56e3SDaniel P. Berrangé  * An initial 3 MB offset is used as that corresponds
51e02f56e3SDaniel P. Berrangé  * to ~1 sec of data transfer with our bandwidth setting.
52e02f56e3SDaniel P. Berrangé  */
53e02f56e3SDaniel P. Berrangé #define MAGIC_OFFSET_BASE (3 * 1024 * 1024)
54e02f56e3SDaniel P. Berrangé /*
55e02f56e3SDaniel P. Berrangé  * A further 1k is added to ensure we're not a multiple
56e02f56e3SDaniel P. Berrangé  * of TEST_MEM_PAGE_SIZE, thus avoid clash with writes
57e02f56e3SDaniel P. Berrangé  * from the migration guest workload.
58e02f56e3SDaniel P. Berrangé  */
59e02f56e3SDaniel P. Berrangé #define MAGIC_OFFSET_SHUFFLE 1024
60e02f56e3SDaniel P. Berrangé #define MAGIC_OFFSET (MAGIC_OFFSET_BASE + MAGIC_OFFSET_SHUFFLE)
61e02f56e3SDaniel P. Berrangé #define MAGIC_MARKER 0xFEED12345678CAFEULL
62e02f56e3SDaniel P. Berrangé 
63e02f56e3SDaniel P. Berrangé /*
648aff6f50SHyman Huang(黄勇)  * Dirtylimit stop working if dirty page rate error
658aff6f50SHyman Huang(黄勇)  * value less than DIRTYLIMIT_TOLERANCE_RANGE
668aff6f50SHyman Huang(黄勇)  */
678aff6f50SHyman Huang(黄勇) #define DIRTYLIMIT_TOLERANCE_RANGE  25  /* MB/s */
688aff6f50SHyman Huang(黄勇) 
69d864756eSFabiano Rosas #define ANALYZE_SCRIPT "scripts/analyze-migration.py"
70d864756eSFabiano Rosas 
713dc35470SFabiano Rosas #define QEMU_VM_FILE_MAGIC 0x5145564d
723dc35470SFabiano Rosas #define FILE_TEST_FILENAME "migfile"
733dc35470SFabiano Rosas #define FILE_TEST_OFFSET 0x1000
745050ad2aSFabiano Rosas #define QEMU_ENV_SRC "QTEST_QEMU_BINARY_SRC"
755050ad2aSFabiano Rosas #define QEMU_ENV_DST "QTEST_QEMU_BINARY_DST"
763dc35470SFabiano Rosas 
771e8a1faeSThomas Huth #if defined(__linux__)
781e8a1faeSThomas Huth #include <sys/syscall.h>
791e8a1faeSThomas Huth #include <sys/vfs.h>
801e8a1faeSThomas Huth #endif
811e8a1faeSThomas Huth 
821e8a1faeSThomas Huth #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
831e8a1faeSThomas Huth #include <sys/eventfd.h>
841e8a1faeSThomas Huth #include <sys/ioctl.h>
85d5890ea0SPeter Xu #include "qemu/userfaultfd.h"
861e8a1faeSThomas Huth 
ufd_version_check(void)871e8a1faeSThomas Huth static bool ufd_version_check(void)
881e8a1faeSThomas Huth {
891e8a1faeSThomas Huth     struct uffdio_api api_struct;
901e8a1faeSThomas Huth     uint64_t ioctl_mask;
911e8a1faeSThomas Huth 
92d5890ea0SPeter Xu     int ufd = uffd_open(O_CLOEXEC);
931e8a1faeSThomas Huth 
941e8a1faeSThomas Huth     if (ufd == -1) {
951e8a1faeSThomas Huth         g_test_message("Skipping test: userfaultfd not available");
961e8a1faeSThomas Huth         return false;
971e8a1faeSThomas Huth     }
981e8a1faeSThomas Huth 
991e8a1faeSThomas Huth     api_struct.api = UFFD_API;
1001e8a1faeSThomas Huth     api_struct.features = 0;
1011e8a1faeSThomas Huth     if (ioctl(ufd, UFFDIO_API, &api_struct)) {
1021e8a1faeSThomas Huth         g_test_message("Skipping test: UFFDIO_API failed");
1031e8a1faeSThomas Huth         return false;
1041e8a1faeSThomas Huth     }
1051e8a1faeSThomas Huth     uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
1061e8a1faeSThomas Huth 
1071e8a1faeSThomas Huth     ioctl_mask = (__u64)1 << _UFFDIO_REGISTER |
1081e8a1faeSThomas Huth                  (__u64)1 << _UFFDIO_UNREGISTER;
1091e8a1faeSThomas Huth     if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
1101e8a1faeSThomas Huth         g_test_message("Skipping test: Missing userfault feature");
1111e8a1faeSThomas Huth         return false;
1121e8a1faeSThomas Huth     }
1131e8a1faeSThomas Huth 
1141e8a1faeSThomas Huth     return true;
1151e8a1faeSThomas Huth }
1161e8a1faeSThomas Huth 
1171e8a1faeSThomas Huth #else
ufd_version_check(void)1181e8a1faeSThomas Huth static bool ufd_version_check(void)
1191e8a1faeSThomas Huth {
1201e8a1faeSThomas Huth     g_test_message("Skipping test: Userfault not available (builtdtime)");
1211e8a1faeSThomas Huth     return false;
1221e8a1faeSThomas Huth }
1231e8a1faeSThomas Huth 
1241e8a1faeSThomas Huth #endif
1251e8a1faeSThomas Huth 
126e5553c1bSBin Meng static char *tmpfs;
127877cec63SJuan Quintela static char *bootpath;
1281e8a1faeSThomas Huth 
1291e8a1faeSThomas Huth /* The boot file modifies memory area in [start_address, end_address)
1301e8a1faeSThomas Huth  * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
1311e8a1faeSThomas Huth  */
1321e8a1faeSThomas Huth #include "tests/migration/i386/a-b-bootblock.h"
1331e8a1faeSThomas Huth #include "tests/migration/aarch64/a-b-kernel.h"
1341e8a1faeSThomas Huth #include "tests/migration/s390x/a-b-bios.h"
1351e8a1faeSThomas Huth 
bootfile_create(char * dir)1360c690d3eSJuan Quintela static void bootfile_create(char *dir)
1371e8a1faeSThomas Huth {
1380c690d3eSJuan Quintela     const char *arch = qtest_get_arch();
1390c690d3eSJuan Quintela     unsigned char *content;
1400c690d3eSJuan Quintela     size_t len;
1410c690d3eSJuan Quintela 
1420c690d3eSJuan Quintela     bootpath = g_strdup_printf("%s/bootsect", dir);
1430c690d3eSJuan Quintela     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
1440c690d3eSJuan Quintela         /* the assembled x86 boot sector should be exactly one sector large */
1450c690d3eSJuan Quintela         g_assert(sizeof(x86_bootsect) == 512);
1460c690d3eSJuan Quintela         content = x86_bootsect;
1470c690d3eSJuan Quintela         len = sizeof(x86_bootsect);
1480c690d3eSJuan Quintela     } else if (g_str_equal(arch, "s390x")) {
1490c690d3eSJuan Quintela         content = s390x_elf;
1500c690d3eSJuan Quintela         len = sizeof(s390x_elf);
1510c690d3eSJuan Quintela     } else if (strcmp(arch, "ppc64") == 0) {
1520c690d3eSJuan Quintela         /*
1530c690d3eSJuan Quintela          * sane architectures can be programmed at the boot prompt
1540c690d3eSJuan Quintela          */
1550c690d3eSJuan Quintela         return;
1560c690d3eSJuan Quintela     } else if (strcmp(arch, "aarch64") == 0) {
1570c690d3eSJuan Quintela         content = aarch64_kernel;
1580c690d3eSJuan Quintela         len = sizeof(aarch64_kernel);
1590c690d3eSJuan Quintela         g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
1600c690d3eSJuan Quintela     } else {
1610c690d3eSJuan Quintela         g_assert_not_reached();
1620c690d3eSJuan Quintela     }
1630c690d3eSJuan Quintela 
1641e8a1faeSThomas Huth     FILE *bootfile = fopen(bootpath, "wb");
1651e8a1faeSThomas Huth 
1661e8a1faeSThomas Huth     g_assert_cmpint(fwrite(content, len, 1, bootfile), ==, 1);
1671e8a1faeSThomas Huth     fclose(bootfile);
1681e8a1faeSThomas Huth }
1691e8a1faeSThomas Huth 
bootfile_delete(void)1700c690d3eSJuan Quintela static void bootfile_delete(void)
1710c690d3eSJuan Quintela {
1720c690d3eSJuan Quintela     unlink(bootpath);
1730c690d3eSJuan Quintela     g_free(bootpath);
1740c690d3eSJuan Quintela     bootpath = NULL;
1750c690d3eSJuan Quintela }
1760c690d3eSJuan Quintela 
1771e8a1faeSThomas Huth /*
1781e8a1faeSThomas Huth  * Wait for some output in the serial output file,
1791e8a1faeSThomas Huth  * we get an 'A' followed by an endless string of 'B's
1801e8a1faeSThomas Huth  * but on the destination we won't have the A.
1811e8a1faeSThomas Huth  */
wait_for_serial(const char * side)1821e8a1faeSThomas Huth static void wait_for_serial(const char *side)
1831e8a1faeSThomas Huth {
184ff7b9b56SPeter Maydell     g_autofree char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
1851e8a1faeSThomas Huth     FILE *serialfile = fopen(serialpath, "r");
1861e8a1faeSThomas Huth     const char *arch = qtest_get_arch();
1871e8a1faeSThomas Huth     int started = (strcmp(side, "src_serial") == 0 &&
1881e8a1faeSThomas Huth                    strcmp(arch, "ppc64") == 0) ? 0 : 1;
1891e8a1faeSThomas Huth 
1901e8a1faeSThomas Huth     do {
1911e8a1faeSThomas Huth         int readvalue = fgetc(serialfile);
1921e8a1faeSThomas Huth 
1931e8a1faeSThomas Huth         if (!started) {
1941e8a1faeSThomas Huth             /* SLOF prints its banner before starting test,
1951e8a1faeSThomas Huth              * to ignore it, mark the start of the test with '_',
1961e8a1faeSThomas Huth              * ignore all characters until this marker
1971e8a1faeSThomas Huth              */
1981e8a1faeSThomas Huth             switch (readvalue) {
1991e8a1faeSThomas Huth             case '_':
2001e8a1faeSThomas Huth                 started = 1;
2011e8a1faeSThomas Huth                 break;
2021e8a1faeSThomas Huth             case EOF:
2031e8a1faeSThomas Huth                 fseek(serialfile, 0, SEEK_SET);
2041e8a1faeSThomas Huth                 usleep(1000);
2051e8a1faeSThomas Huth                 break;
2061e8a1faeSThomas Huth             }
2071e8a1faeSThomas Huth             continue;
2081e8a1faeSThomas Huth         }
2091e8a1faeSThomas Huth         switch (readvalue) {
2101e8a1faeSThomas Huth         case 'A':
2111e8a1faeSThomas Huth             /* Fine */
2121e8a1faeSThomas Huth             break;
2131e8a1faeSThomas Huth 
2141e8a1faeSThomas Huth         case 'B':
2151e8a1faeSThomas Huth             /* It's alive! */
2161e8a1faeSThomas Huth             fclose(serialfile);
2171e8a1faeSThomas Huth             return;
2181e8a1faeSThomas Huth 
2191e8a1faeSThomas Huth         case EOF:
2201e8a1faeSThomas Huth             started = (strcmp(side, "src_serial") == 0 &&
2211e8a1faeSThomas Huth                        strcmp(arch, "ppc64") == 0) ? 0 : 1;
2221e8a1faeSThomas Huth             fseek(serialfile, 0, SEEK_SET);
2231e8a1faeSThomas Huth             usleep(1000);
2241e8a1faeSThomas Huth             break;
2251e8a1faeSThomas Huth 
2261e8a1faeSThomas Huth         default:
2271e8a1faeSThomas Huth             fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
2281e8a1faeSThomas Huth             g_assert_not_reached();
2291e8a1faeSThomas Huth         }
2301e8a1faeSThomas Huth     } while (true);
2311e8a1faeSThomas Huth }
2321e8a1faeSThomas Huth 
2331e8a1faeSThomas Huth /*
2341e8a1faeSThomas Huth  * It's tricky to use qemu's migration event capability with qtest,
2351e8a1faeSThomas Huth  * events suddenly appearing confuse the qmp()/hmp() responses.
2361e8a1faeSThomas Huth  */
2371e8a1faeSThomas Huth 
read_ram_property_int(QTestState * who,const char * property)2381e8a1faeSThomas Huth static int64_t read_ram_property_int(QTestState *who, const char *property)
2391e8a1faeSThomas Huth {
2401e8a1faeSThomas Huth     QDict *rsp_return, *rsp_ram;
2411e8a1faeSThomas Huth     int64_t result;
2421e8a1faeSThomas Huth 
243fd3540adSDaniel P. Berrangé     rsp_return = migrate_query_not_failed(who);
2441e8a1faeSThomas Huth     if (!qdict_haskey(rsp_return, "ram")) {
2451e8a1faeSThomas Huth         /* Still in setup */
2461e8a1faeSThomas Huth         result = 0;
2471e8a1faeSThomas Huth     } else {
2481e8a1faeSThomas Huth         rsp_ram = qdict_get_qdict(rsp_return, "ram");
2491e8a1faeSThomas Huth         result = qdict_get_try_int(rsp_ram, property, 0);
2501e8a1faeSThomas Huth     }
2511e8a1faeSThomas Huth     qobject_unref(rsp_return);
2521e8a1faeSThomas Huth     return result;
2531e8a1faeSThomas Huth }
2541e8a1faeSThomas Huth 
read_migrate_property_int(QTestState * who,const char * property)2551e8a1faeSThomas Huth static int64_t read_migrate_property_int(QTestState *who, const char *property)
2561e8a1faeSThomas Huth {
2571e8a1faeSThomas Huth     QDict *rsp_return;
2581e8a1faeSThomas Huth     int64_t result;
2591e8a1faeSThomas Huth 
260fd3540adSDaniel P. Berrangé     rsp_return = migrate_query_not_failed(who);
2611e8a1faeSThomas Huth     result = qdict_get_try_int(rsp_return, property, 0);
2621e8a1faeSThomas Huth     qobject_unref(rsp_return);
2631e8a1faeSThomas Huth     return result;
2641e8a1faeSThomas Huth }
2651e8a1faeSThomas Huth 
get_migration_pass(QTestState * who)2661e8a1faeSThomas Huth static uint64_t get_migration_pass(QTestState *who)
2671e8a1faeSThomas Huth {
2681e8a1faeSThomas Huth     return read_ram_property_int(who, "dirty-sync-count");
2691e8a1faeSThomas Huth }
2701e8a1faeSThomas Huth 
read_blocktime(QTestState * who)2711e8a1faeSThomas Huth static void read_blocktime(QTestState *who)
2721e8a1faeSThomas Huth {
2731e8a1faeSThomas Huth     QDict *rsp_return;
2741e8a1faeSThomas Huth 
275fd3540adSDaniel P. Berrangé     rsp_return = migrate_query_not_failed(who);
2761e8a1faeSThomas Huth     g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
2771e8a1faeSThomas Huth     qobject_unref(rsp_return);
2781e8a1faeSThomas Huth }
2791e8a1faeSThomas Huth 
wait_for_migration_pass(QTestState * who)2801e8a1faeSThomas Huth static void wait_for_migration_pass(QTestState *who)
2811e8a1faeSThomas Huth {
2821e8a1faeSThomas Huth     uint64_t initial_pass = get_migration_pass(who);
2831e8a1faeSThomas Huth     uint64_t pass;
2841e8a1faeSThomas Huth 
2851e8a1faeSThomas Huth     /* Wait for the 1st sync */
28695014994SDaniel P. Berrangé     while (!got_src_stop && !initial_pass) {
2871e8a1faeSThomas Huth         usleep(1000);
2881e8a1faeSThomas Huth         initial_pass = get_migration_pass(who);
2891e8a1faeSThomas Huth     }
2901e8a1faeSThomas Huth 
2911e8a1faeSThomas Huth     do {
2921e8a1faeSThomas Huth         usleep(1000);
2931e8a1faeSThomas Huth         pass = get_migration_pass(who);
29495014994SDaniel P. Berrangé     } while (pass == initial_pass && !got_src_stop);
2951e8a1faeSThomas Huth }
2961e8a1faeSThomas Huth 
check_guests_ram(QTestState * who)2971e8a1faeSThomas Huth static void check_guests_ram(QTestState *who)
2981e8a1faeSThomas Huth {
2991e8a1faeSThomas Huth     /* Our ASM test will have been incrementing one byte from each page from
3001e8a1faeSThomas Huth      * start_address to < end_address in order. This gives us a constraint
3011e8a1faeSThomas Huth      * that any page's byte should be equal or less than the previous pages
3021e8a1faeSThomas Huth      * byte (mod 256); and they should all be equal except for one transition
3031e8a1faeSThomas Huth      * at the point where we meet the incrementer. (We're running this with
3041e8a1faeSThomas Huth      * the guest stopped).
3051e8a1faeSThomas Huth      */
3061e8a1faeSThomas Huth     unsigned address;
3071e8a1faeSThomas Huth     uint8_t first_byte;
3081e8a1faeSThomas Huth     uint8_t last_byte;
3091e8a1faeSThomas Huth     bool hit_edge = false;
3101e8a1faeSThomas Huth     int bad = 0;
3111e8a1faeSThomas Huth 
3121e8a1faeSThomas Huth     qtest_memread(who, start_address, &first_byte, 1);
3131e8a1faeSThomas Huth     last_byte = first_byte;
3141e8a1faeSThomas Huth 
3151e8a1faeSThomas Huth     for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
3161e8a1faeSThomas Huth          address += TEST_MEM_PAGE_SIZE)
3171e8a1faeSThomas Huth     {
3181e8a1faeSThomas Huth         uint8_t b;
3191e8a1faeSThomas Huth         qtest_memread(who, address, &b, 1);
3201e8a1faeSThomas Huth         if (b != last_byte) {
3211e8a1faeSThomas Huth             if (((b + 1) % 256) == last_byte && !hit_edge) {
3221e8a1faeSThomas Huth                 /* This is OK, the guest stopped at the point of
3231e8a1faeSThomas Huth                  * incrementing the previous page but didn't get
3241e8a1faeSThomas Huth                  * to us yet.
3251e8a1faeSThomas Huth                  */
3261e8a1faeSThomas Huth                 hit_edge = true;
3271e8a1faeSThomas Huth                 last_byte = b;
3281e8a1faeSThomas Huth             } else {
3291e8a1faeSThomas Huth                 bad++;
3301e8a1faeSThomas Huth                 if (bad <= 10) {
3311e8a1faeSThomas Huth                     fprintf(stderr, "Memory content inconsistency at %x"
3321e8a1faeSThomas Huth                             " first_byte = %x last_byte = %x current = %x"
3331e8a1faeSThomas Huth                             " hit_edge = %x\n",
3341e8a1faeSThomas Huth                             address, first_byte, last_byte, b, hit_edge);
3351e8a1faeSThomas Huth                 }
3361e8a1faeSThomas Huth             }
3371e8a1faeSThomas Huth         }
3381e8a1faeSThomas Huth     }
3391e8a1faeSThomas Huth     if (bad >= 10) {
3401e8a1faeSThomas Huth         fprintf(stderr, "and in another %d pages", bad - 10);
3411e8a1faeSThomas Huth     }
3421e8a1faeSThomas Huth     g_assert(bad == 0);
3431e8a1faeSThomas Huth }
3441e8a1faeSThomas Huth 
cleanup(const char * filename)3451e8a1faeSThomas Huth static void cleanup(const char *filename)
3461e8a1faeSThomas Huth {
347ff7b9b56SPeter Maydell     g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, filename);
3481e8a1faeSThomas Huth 
3491e8a1faeSThomas Huth     unlink(path);
3501e8a1faeSThomas Huth }
3511e8a1faeSThomas Huth 
SocketAddress_to_str(SocketAddress * addr)3521e8a1faeSThomas Huth static char *SocketAddress_to_str(SocketAddress *addr)
3531e8a1faeSThomas Huth {
3541e8a1faeSThomas Huth     switch (addr->type) {
3551e8a1faeSThomas Huth     case SOCKET_ADDRESS_TYPE_INET:
3561e8a1faeSThomas Huth         return g_strdup_printf("tcp:%s:%s",
3571e8a1faeSThomas Huth                                addr->u.inet.host,
3581e8a1faeSThomas Huth                                addr->u.inet.port);
3591e8a1faeSThomas Huth     case SOCKET_ADDRESS_TYPE_UNIX:
3601e8a1faeSThomas Huth         return g_strdup_printf("unix:%s",
3611e8a1faeSThomas Huth                                addr->u.q_unix.path);
3621e8a1faeSThomas Huth     case SOCKET_ADDRESS_TYPE_FD:
3631e8a1faeSThomas Huth         return g_strdup_printf("fd:%s", addr->u.fd.str);
3641e8a1faeSThomas Huth     case SOCKET_ADDRESS_TYPE_VSOCK:
3651e8a1faeSThomas Huth         return g_strdup_printf("tcp:%s:%s",
3661e8a1faeSThomas Huth                                addr->u.vsock.cid,
3671e8a1faeSThomas Huth                                addr->u.vsock.port);
3681e8a1faeSThomas Huth     default:
3691e8a1faeSThomas Huth         return g_strdup("unknown address type");
3701e8a1faeSThomas Huth     }
3711e8a1faeSThomas Huth }
3721e8a1faeSThomas Huth 
migrate_get_socket_address(QTestState * who,const char * parameter)3731e8a1faeSThomas Huth static char *migrate_get_socket_address(QTestState *who, const char *parameter)
3741e8a1faeSThomas Huth {
3751e8a1faeSThomas Huth     QDict *rsp;
3761e8a1faeSThomas Huth     char *result;
3771e8a1faeSThomas Huth     SocketAddressList *addrs;
3781e8a1faeSThomas Huth     Visitor *iv = NULL;
3791e8a1faeSThomas Huth     QObject *object;
3801e8a1faeSThomas Huth 
3811e8a1faeSThomas Huth     rsp = migrate_query(who);
3821e8a1faeSThomas Huth     object = qdict_get(rsp, parameter);
3831e8a1faeSThomas Huth 
3841e8a1faeSThomas Huth     iv = qobject_input_visitor_new(object);
385d0112758SMarkus Armbruster     visit_type_SocketAddressList(iv, NULL, &addrs, &error_abort);
3861e8a1faeSThomas Huth     visit_free(iv);
3871e8a1faeSThomas Huth 
3881e8a1faeSThomas Huth     /* we are only using a single address */
3891e8a1faeSThomas Huth     result = SocketAddress_to_str(addrs->value);
3901e8a1faeSThomas Huth 
3911e8a1faeSThomas Huth     qapi_free_SocketAddressList(addrs);
3921e8a1faeSThomas Huth     qobject_unref(rsp);
3931e8a1faeSThomas Huth     return result;
3941e8a1faeSThomas Huth }
3951e8a1faeSThomas Huth 
migrate_get_parameter_int(QTestState * who,const char * parameter)3961e8a1faeSThomas Huth static long long migrate_get_parameter_int(QTestState *who,
3971e8a1faeSThomas Huth                                            const char *parameter)
3981e8a1faeSThomas Huth {
3991e8a1faeSThomas Huth     QDict *rsp;
4001e8a1faeSThomas Huth     long long result;
4011e8a1faeSThomas Huth 
402aca04069SDaniel P. Berrangé     rsp = qtest_qmp_assert_success_ref(
403aca04069SDaniel P. Berrangé         who, "{ 'execute': 'query-migrate-parameters' }");
4041e8a1faeSThomas Huth     result = qdict_get_int(rsp, parameter);
4051e8a1faeSThomas Huth     qobject_unref(rsp);
4061e8a1faeSThomas Huth     return result;
4071e8a1faeSThomas Huth }
4081e8a1faeSThomas Huth 
migrate_check_parameter_int(QTestState * who,const char * parameter,long long value)4091e8a1faeSThomas Huth static void migrate_check_parameter_int(QTestState *who, const char *parameter,
4101e8a1faeSThomas Huth                                         long long value)
4111e8a1faeSThomas Huth {
4121e8a1faeSThomas Huth     long long result;
4131e8a1faeSThomas Huth 
4141e8a1faeSThomas Huth     result = migrate_get_parameter_int(who, parameter);
4151e8a1faeSThomas Huth     g_assert_cmpint(result, ==, value);
4161e8a1faeSThomas Huth }
4171e8a1faeSThomas Huth 
migrate_set_parameter_int(QTestState * who,const char * parameter,long long value)4181e8a1faeSThomas Huth static void migrate_set_parameter_int(QTestState *who, const char *parameter,
4191e8a1faeSThomas Huth                                       long long value)
4201e8a1faeSThomas Huth {
42111936f0eSDaniel P. Berrangé     qtest_qmp_assert_success(who,
4221e8a1faeSThomas Huth                              "{ 'execute': 'migrate-set-parameters',"
4231e8a1faeSThomas Huth                              "'arguments': { %s: %lld } }",
4241e8a1faeSThomas Huth                              parameter, value);
4251e8a1faeSThomas Huth     migrate_check_parameter_int(who, parameter, value);
4261e8a1faeSThomas Huth }
4271e8a1faeSThomas Huth 
migrate_get_parameter_str(QTestState * who,const char * parameter)4286a22c544SJuan Quintela static char *migrate_get_parameter_str(QTestState *who,
4296a22c544SJuan Quintela                                        const char *parameter)
4306a22c544SJuan Quintela {
4316a22c544SJuan Quintela     QDict *rsp;
4326a22c544SJuan Quintela     char *result;
4336a22c544SJuan Quintela 
434aca04069SDaniel P. Berrangé     rsp = qtest_qmp_assert_success_ref(
435aca04069SDaniel P. Berrangé         who, "{ 'execute': 'query-migrate-parameters' }");
4366a22c544SJuan Quintela     result = g_strdup(qdict_get_str(rsp, parameter));
4376a22c544SJuan Quintela     qobject_unref(rsp);
4386a22c544SJuan Quintela     return result;
4396a22c544SJuan Quintela }
4406a22c544SJuan Quintela 
migrate_check_parameter_str(QTestState * who,const char * parameter,const char * value)4416a22c544SJuan Quintela static void migrate_check_parameter_str(QTestState *who, const char *parameter,
4426a22c544SJuan Quintela                                         const char *value)
4436a22c544SJuan Quintela {
444ff7b9b56SPeter Maydell     g_autofree char *result = migrate_get_parameter_str(who, parameter);
4456a22c544SJuan Quintela     g_assert_cmpstr(result, ==, value);
4466a22c544SJuan Quintela }
4476a22c544SJuan Quintela 
migrate_set_parameter_str(QTestState * who,const char * parameter,const char * value)4486a22c544SJuan Quintela static void migrate_set_parameter_str(QTestState *who, const char *parameter,
4496a22c544SJuan Quintela                                       const char *value)
4506a22c544SJuan Quintela {
45111936f0eSDaniel P. Berrangé     qtest_qmp_assert_success(who,
4526a22c544SJuan Quintela                              "{ 'execute': 'migrate-set-parameters',"
4536a22c544SJuan Quintela                              "'arguments': { %s: %s } }",
4546a22c544SJuan Quintela                              parameter, value);
4556a22c544SJuan Quintela     migrate_check_parameter_str(who, parameter, value);
4566a22c544SJuan Quintela }
4576a22c544SJuan Quintela 
migrate_get_parameter_bool(QTestState * who,const char * parameter)4581536d1daSLukas Straub static long long migrate_get_parameter_bool(QTestState *who,
4591536d1daSLukas Straub                                            const char *parameter)
4601536d1daSLukas Straub {
4611536d1daSLukas Straub     QDict *rsp;
4621536d1daSLukas Straub     int result;
4631536d1daSLukas Straub 
464aca04069SDaniel P. Berrangé     rsp = qtest_qmp_assert_success_ref(
465aca04069SDaniel P. Berrangé         who, "{ 'execute': 'query-migrate-parameters' }");
4661536d1daSLukas Straub     result = qdict_get_bool(rsp, parameter);
4671536d1daSLukas Straub     qobject_unref(rsp);
4681536d1daSLukas Straub     return !!result;
4691536d1daSLukas Straub }
4701536d1daSLukas Straub 
migrate_check_parameter_bool(QTestState * who,const char * parameter,int value)4711536d1daSLukas Straub static void migrate_check_parameter_bool(QTestState *who, const char *parameter,
4721536d1daSLukas Straub                                         int value)
4731536d1daSLukas Straub {
4741536d1daSLukas Straub     int result;
4751536d1daSLukas Straub 
4761536d1daSLukas Straub     result = migrate_get_parameter_bool(who, parameter);
4771536d1daSLukas Straub     g_assert_cmpint(result, ==, value);
4781536d1daSLukas Straub }
4791536d1daSLukas Straub 
migrate_set_parameter_bool(QTestState * who,const char * parameter,int value)4801536d1daSLukas Straub static void migrate_set_parameter_bool(QTestState *who, const char *parameter,
4811536d1daSLukas Straub                                       int value)
4821536d1daSLukas Straub {
48311936f0eSDaniel P. Berrangé     qtest_qmp_assert_success(who,
4841536d1daSLukas Straub                              "{ 'execute': 'migrate-set-parameters',"
4851536d1daSLukas Straub                              "'arguments': { %s: %i } }",
4861536d1daSLukas Straub                              parameter, value);
4871536d1daSLukas Straub     migrate_check_parameter_bool(who, parameter, value);
4881536d1daSLukas Straub }
4891536d1daSLukas Straub 
migrate_ensure_non_converge(QTestState * who)490886dfe9dSDaniel P. Berrangé static void migrate_ensure_non_converge(QTestState *who)
491886dfe9dSDaniel P. Berrangé {
4921bfc8ddeSDr. David Alan Gilbert     /* Can't converge with 1ms downtime + 3 mbs bandwidth limit */
4931bfc8ddeSDr. David Alan Gilbert     migrate_set_parameter_int(who, "max-bandwidth", 3 * 1000 * 1000);
494886dfe9dSDaniel P. Berrangé     migrate_set_parameter_int(who, "downtime-limit", 1);
495886dfe9dSDaniel P. Berrangé }
496886dfe9dSDaniel P. Berrangé 
migrate_ensure_converge(QTestState * who)497886dfe9dSDaniel P. Berrangé static void migrate_ensure_converge(QTestState *who)
498886dfe9dSDaniel P. Berrangé {
499886dfe9dSDaniel P. Berrangé     /* Should converge with 30s downtime + 1 gbs bandwidth limit */
500886dfe9dSDaniel P. Berrangé     migrate_set_parameter_int(who, "max-bandwidth", 1 * 1000 * 1000 * 1000);
501886dfe9dSDaniel P. Berrangé     migrate_set_parameter_int(who, "downtime-limit", 30 * 1000);
502886dfe9dSDaniel P. Berrangé }
503886dfe9dSDaniel P. Berrangé 
504e02f56e3SDaniel P. Berrangé /*
505e02f56e3SDaniel P. Berrangé  * Our goal is to ensure that we run a single full migration
506e02f56e3SDaniel P. Berrangé  * iteration, and also dirty memory, ensuring that at least
507e02f56e3SDaniel P. Berrangé  * one further iteration is required.
508e02f56e3SDaniel P. Berrangé  *
509e02f56e3SDaniel P. Berrangé  * We can't directly synchronize with the start of a migration
510e02f56e3SDaniel P. Berrangé  * so we have to apply some tricks monitoring memory that is
511e02f56e3SDaniel P. Berrangé  * transferred.
512e02f56e3SDaniel P. Berrangé  *
513e02f56e3SDaniel P. Berrangé  * Initially we set the migration bandwidth to an insanely
514e02f56e3SDaniel P. Berrangé  * low value, with tiny max downtime too. This basically
515e02f56e3SDaniel P. Berrangé  * guarantees migration will never complete.
516e02f56e3SDaniel P. Berrangé  *
517e02f56e3SDaniel P. Berrangé  * This will result in a test that is unacceptably slow though,
518e02f56e3SDaniel P. Berrangé  * so we can't let the entire migration pass run at this speed.
519e02f56e3SDaniel P. Berrangé  * Our intent is to let it run just long enough that we can
520e02f56e3SDaniel P. Berrangé  * prove data prior to the marker has been transferred *AND*
521e02f56e3SDaniel P. Berrangé  * also prove this transferred data is dirty again.
522e02f56e3SDaniel P. Berrangé  *
523e02f56e3SDaniel P. Berrangé  * Before migration starts, we write a 64-bit magic marker
524e02f56e3SDaniel P. Berrangé  * into a fixed location in the src VM RAM.
525e02f56e3SDaniel P. Berrangé  *
526e02f56e3SDaniel P. Berrangé  * Then watch dst memory until the marker appears. This is
527e02f56e3SDaniel P. Berrangé  * proof that start_address -> MAGIC_OFFSET_BASE has been
528e02f56e3SDaniel P. Berrangé  * transferred.
529e02f56e3SDaniel P. Berrangé  *
530e02f56e3SDaniel P. Berrangé  * Finally we go back to the source and read a byte just
53196420a30SMichael Tokarev  * before the marker until we see it flip in value. This
532e02f56e3SDaniel P. Berrangé  * is proof that start_address -> MAGIC_OFFSET_BASE
533e02f56e3SDaniel P. Berrangé  * is now dirty again.
534e02f56e3SDaniel P. Berrangé  *
535e02f56e3SDaniel P. Berrangé  * IOW, we're guaranteed at least a 2nd migration pass
536e02f56e3SDaniel P. Berrangé  * at this point.
537e02f56e3SDaniel P. Berrangé  *
538e02f56e3SDaniel P. Berrangé  * We can now let migration run at full speed to finish
539e02f56e3SDaniel P. Berrangé  * the test
540e02f56e3SDaniel P. Berrangé  */
migrate_prepare_for_dirty_mem(QTestState * from)541e02f56e3SDaniel P. Berrangé static void migrate_prepare_for_dirty_mem(QTestState *from)
542e02f56e3SDaniel P. Berrangé {
543e02f56e3SDaniel P. Berrangé     /*
544e02f56e3SDaniel P. Berrangé      * The guest workflow iterates from start_address to
545e02f56e3SDaniel P. Berrangé      * end_address, writing 1 byte every TEST_MEM_PAGE_SIZE
546e02f56e3SDaniel P. Berrangé      * bytes.
547e02f56e3SDaniel P. Berrangé      *
548e02f56e3SDaniel P. Berrangé      * IOW, if we write to mem at a point which is NOT
549e02f56e3SDaniel P. Berrangé      * a multiple of TEST_MEM_PAGE_SIZE, our write won't
550e02f56e3SDaniel P. Berrangé      * conflict with the migration workflow.
551e02f56e3SDaniel P. Berrangé      *
552e02f56e3SDaniel P. Berrangé      * We put in a marker here, that we'll use to determine
553e02f56e3SDaniel P. Berrangé      * when the data has been transferred to the dst.
554e02f56e3SDaniel P. Berrangé      */
555e02f56e3SDaniel P. Berrangé     qtest_writeq(from, start_address + MAGIC_OFFSET, MAGIC_MARKER);
556e02f56e3SDaniel P. Berrangé }
557e02f56e3SDaniel P. Berrangé 
migrate_wait_for_dirty_mem(QTestState * from,QTestState * to)558e02f56e3SDaniel P. Berrangé static void migrate_wait_for_dirty_mem(QTestState *from,
559e02f56e3SDaniel P. Berrangé                                        QTestState *to)
560e02f56e3SDaniel P. Berrangé {
561e02f56e3SDaniel P. Berrangé     uint64_t watch_address = start_address + MAGIC_OFFSET_BASE;
562e02f56e3SDaniel P. Berrangé     uint64_t marker_address = start_address + MAGIC_OFFSET;
563e02f56e3SDaniel P. Berrangé     uint8_t watch_byte;
564e02f56e3SDaniel P. Berrangé 
565e02f56e3SDaniel P. Berrangé     /*
566e02f56e3SDaniel P. Berrangé      * Wait for the MAGIC_MARKER to get transferred, as an
567e02f56e3SDaniel P. Berrangé      * indicator that a migration pass has made some known
568e02f56e3SDaniel P. Berrangé      * amount of progress.
569e02f56e3SDaniel P. Berrangé      */
570e02f56e3SDaniel P. Berrangé     do {
571e02f56e3SDaniel P. Berrangé         usleep(1000 * 10);
572e02f56e3SDaniel P. Berrangé     } while (qtest_readq(to, marker_address) != MAGIC_MARKER);
573e02f56e3SDaniel P. Berrangé 
574e02f56e3SDaniel P. Berrangé     /*
575e02f56e3SDaniel P. Berrangé      * Now ensure that already transferred bytes are
576e02f56e3SDaniel P. Berrangé      * dirty again from the guest workload. Note the
577e02f56e3SDaniel P. Berrangé      * guest byte value will wrap around and by chance
578e02f56e3SDaniel P. Berrangé      * match the original watch_byte. This is harmless
579e02f56e3SDaniel P. Berrangé      * as we'll eventually see a different value if we
580e02f56e3SDaniel P. Berrangé      * keep watching
581e02f56e3SDaniel P. Berrangé      */
582e02f56e3SDaniel P. Berrangé     watch_byte = qtest_readb(from, watch_address);
583e02f56e3SDaniel P. Berrangé     do {
584e02f56e3SDaniel P. Berrangé         usleep(1000 * 10);
585e02f56e3SDaniel P. Berrangé     } while (qtest_readb(from, watch_address) == watch_byte);
586e02f56e3SDaniel P. Berrangé }
587e02f56e3SDaniel P. Berrangé 
588e02f56e3SDaniel P. Berrangé 
migrate_pause(QTestState * who)5891e8a1faeSThomas Huth static void migrate_pause(QTestState *who)
5901e8a1faeSThomas Huth {
591aca04069SDaniel P. Berrangé     qtest_qmp_assert_success(who, "{ 'execute': 'migrate-pause' }");
5921e8a1faeSThomas Huth }
5931e8a1faeSThomas Huth 
migrate_continue(QTestState * who,const char * state)5941e8a1faeSThomas Huth static void migrate_continue(QTestState *who, const char *state)
5951e8a1faeSThomas Huth {
596aca04069SDaniel P. Berrangé     qtest_qmp_assert_success(who,
5971e8a1faeSThomas Huth                              "{ 'execute': 'migrate-continue',"
5981e8a1faeSThomas Huth                              "  'arguments': { 'state': %s } }",
5991e8a1faeSThomas Huth                              state);
6001e8a1faeSThomas Huth }
6011e8a1faeSThomas Huth 
migrate_recover(QTestState * who,const char * uri)6021e8a1faeSThomas Huth static void migrate_recover(QTestState *who, const char *uri)
6031e8a1faeSThomas Huth {
604aca04069SDaniel P. Berrangé     qtest_qmp_assert_success(who,
6051e8a1faeSThomas Huth                              "{ 'execute': 'migrate-recover', "
6061e8a1faeSThomas Huth                              "  'id': 'recover-cmd', "
6071e8a1faeSThomas Huth                              "  'arguments': { 'uri': %s } }",
6081e8a1faeSThomas Huth                              uri);
6091e8a1faeSThomas Huth }
6101e8a1faeSThomas Huth 
migrate_cancel(QTestState * who)611d795f474SJuan Quintela static void migrate_cancel(QTestState *who)
612d795f474SJuan Quintela {
613aca04069SDaniel P. Berrangé     qtest_qmp_assert_success(who, "{ 'execute': 'migrate_cancel' }");
614d795f474SJuan Quintela }
615d795f474SJuan Quintela 
migrate_postcopy_start(QTestState * from,QTestState * to)6161e8a1faeSThomas Huth static void migrate_postcopy_start(QTestState *from, QTestState *to)
6171e8a1faeSThomas Huth {
618aca04069SDaniel P. Berrangé     qtest_qmp_assert_success(from, "{ 'execute': 'migrate-start-postcopy' }");
6191e8a1faeSThomas Huth 
62095014994SDaniel P. Berrangé     if (!got_src_stop) {
6211e8a1faeSThomas Huth         qtest_qmp_eventwait(from, "STOP");
6221e8a1faeSThomas Huth     }
6231e8a1faeSThomas Huth 
6241e8a1faeSThomas Huth     qtest_qmp_eventwait(to, "RESUME");
6251e8a1faeSThomas Huth }
6261e8a1faeSThomas Huth 
6271e8a1faeSThomas Huth typedef struct {
628a4729501SPeter Xu     /*
629a4729501SPeter Xu      * QTEST_LOG=1 may override this.  When QTEST_LOG=1, we always dump errors
630a4729501SPeter Xu      * unconditionally, because it means the user would like to be verbose.
631a4729501SPeter Xu      */
6321e8a1faeSThomas Huth     bool hide_stderr;
6331e8a1faeSThomas Huth     bool use_shmem;
634d795f474SJuan Quintela     /* only launch the target process */
635d795f474SJuan Quintela     bool only_target;
6361f546b70SPeter Xu     /* Use dirty ring if true; dirty logging otherwise */
6371f546b70SPeter Xu     bool use_dirty_ring;
63819da6edfSDaniel P. Berrangé     const char *opts_source;
63919da6edfSDaniel P. Berrangé     const char *opts_target;
6401e8a1faeSThomas Huth } MigrateStart;
6411e8a1faeSThomas Huth 
642312e9dd0SPeter Xu /*
643312e9dd0SPeter Xu  * A hook that runs after the src and dst QEMUs have been
644312e9dd0SPeter Xu  * created, but before the migration is started. This can
645312e9dd0SPeter Xu  * be used to set migration parameters and capabilities.
646312e9dd0SPeter Xu  *
647312e9dd0SPeter Xu  * Returns: NULL, or a pointer to opaque state to be
648312e9dd0SPeter Xu  *          later passed to the TestMigrateFinishHook
649312e9dd0SPeter Xu  */
650312e9dd0SPeter Xu typedef void * (*TestMigrateStartHook)(QTestState *from,
651312e9dd0SPeter Xu                                        QTestState *to);
652312e9dd0SPeter Xu 
653312e9dd0SPeter Xu /*
654312e9dd0SPeter Xu  * A hook that runs after the migration has finished,
655312e9dd0SPeter Xu  * regardless of whether it succeeded or failed, but
656312e9dd0SPeter Xu  * before QEMU has terminated (unless it self-terminated
657312e9dd0SPeter Xu  * due to migration error)
658312e9dd0SPeter Xu  *
659312e9dd0SPeter Xu  * @opaque is a pointer to state previously returned
660312e9dd0SPeter Xu  * by the TestMigrateStartHook if any, or NULL.
661312e9dd0SPeter Xu  */
662312e9dd0SPeter Xu typedef void (*TestMigrateFinishHook)(QTestState *from,
663312e9dd0SPeter Xu                                       QTestState *to,
664312e9dd0SPeter Xu                                       void *opaque);
665312e9dd0SPeter Xu 
666312e9dd0SPeter Xu typedef struct {
667312e9dd0SPeter Xu     /* Optional: fine tune start parameters */
668312e9dd0SPeter Xu     MigrateStart start;
669312e9dd0SPeter Xu 
670312e9dd0SPeter Xu     /* Required: the URI for the dst QEMU to listen on */
671312e9dd0SPeter Xu     const char *listen_uri;
672312e9dd0SPeter Xu 
673312e9dd0SPeter Xu     /*
674312e9dd0SPeter Xu      * Optional: the URI for the src QEMU to connect to
675312e9dd0SPeter Xu      * If NULL, then it will query the dst QEMU for its actual
676312e9dd0SPeter Xu      * listening address and use that as the connect address.
677312e9dd0SPeter Xu      * This allows for dynamically picking a free TCP port.
678312e9dd0SPeter Xu      */
679312e9dd0SPeter Xu     const char *connect_uri;
680312e9dd0SPeter Xu 
681312e9dd0SPeter Xu     /* Optional: callback to run at start to set migration parameters */
682312e9dd0SPeter Xu     TestMigrateStartHook start_hook;
683312e9dd0SPeter Xu     /* Optional: callback to run at finish to cleanup */
684312e9dd0SPeter Xu     TestMigrateFinishHook finish_hook;
685312e9dd0SPeter Xu 
686312e9dd0SPeter Xu     /*
687312e9dd0SPeter Xu      * Optional: normally we expect the migration process to complete.
688312e9dd0SPeter Xu      *
689312e9dd0SPeter Xu      * There can be a variety of reasons and stages in which failure
690312e9dd0SPeter Xu      * can happen during tests.
691312e9dd0SPeter Xu      *
692312e9dd0SPeter Xu      * If a failure is expected to happen at time of establishing
693312e9dd0SPeter Xu      * the connection, then MIG_TEST_FAIL will indicate that the dst
694312e9dd0SPeter Xu      * QEMU is expected to stay running and accept future migration
695312e9dd0SPeter Xu      * connections.
696312e9dd0SPeter Xu      *
697312e9dd0SPeter Xu      * If a failure is expected to happen while processing the
698312e9dd0SPeter Xu      * migration stream, then MIG_TEST_FAIL_DEST_QUIT_ERR will indicate
699312e9dd0SPeter Xu      * that the dst QEMU is expected to quit with non-zero exit status
700312e9dd0SPeter Xu      */
701312e9dd0SPeter Xu     enum {
702312e9dd0SPeter Xu         /* This test should succeed, the default */
703312e9dd0SPeter Xu         MIG_TEST_SUCCEED = 0,
704312e9dd0SPeter Xu         /* This test should fail, dest qemu should keep alive */
705312e9dd0SPeter Xu         MIG_TEST_FAIL,
706312e9dd0SPeter Xu         /* This test should fail, dest qemu should fail with abnormal status */
707312e9dd0SPeter Xu         MIG_TEST_FAIL_DEST_QUIT_ERR,
7085274274cSFabiano Rosas         /* The QMP command for this migration should fail with an error */
7095274274cSFabiano Rosas         MIG_TEST_QMP_ERROR,
710312e9dd0SPeter Xu     } result;
711312e9dd0SPeter Xu 
712e02f56e3SDaniel P. Berrangé     /*
713e02f56e3SDaniel P. Berrangé      * Optional: set number of migration passes to wait for, if live==true.
714e02f56e3SDaniel P. Berrangé      * If zero, then merely wait for a few MB of dirty data
715e02f56e3SDaniel P. Berrangé      */
716312e9dd0SPeter Xu     unsigned int iterations;
717d1a27b16SPeter Xu 
718b861383cSPeter Xu     /*
719b861383cSPeter Xu      * Optional: whether the guest CPUs should be running during a precopy
720b861383cSPeter Xu      * migration test.  We used to always run with live but it took much
721b861383cSPeter Xu      * longer so we reduced live tests to only the ones that have solid
722b861383cSPeter Xu      * reason to be tested live-only.  For each of the new test cases for
723b861383cSPeter Xu      * precopy please provide justifications to use live explicitly (please
724b861383cSPeter Xu      * refer to existing ones with live=true), or use live=off by default.
725b861383cSPeter Xu      */
7263c4fb177SDaniel P. Berrangé     bool live;
7273c4fb177SDaniel P. Berrangé 
728d1a27b16SPeter Xu     /* Postcopy specific fields */
729d1a27b16SPeter Xu     void *postcopy_data;
7308f6fe915SPeter Xu     bool postcopy_preempt;
7317bca2bb7SFabiano Rosas     bool postcopy_recovery_test_fail;
732312e9dd0SPeter Xu } MigrateCommon;
733312e9dd0SPeter Xu 
test_migrate_start(QTestState ** from,QTestState ** to,const char * uri,MigrateStart * args)7341e8a1faeSThomas Huth static int test_migrate_start(QTestState **from, QTestState **to,
73519da6edfSDaniel P. Berrangé                               const char *uri, MigrateStart *args)
7361e8a1faeSThomas Huth {
737ff7b9b56SPeter Maydell     g_autofree gchar *arch_source = NULL;
738ff7b9b56SPeter Maydell     g_autofree gchar *arch_target = NULL;
739832c732cSJuan Quintela     /* options for source and target */
740832c732cSJuan Quintela     g_autofree gchar *arch_opts = NULL;
741ff7b9b56SPeter Maydell     g_autofree gchar *cmd_source = NULL;
742ff7b9b56SPeter Maydell     g_autofree gchar *cmd_target = NULL;
7431e8a1faeSThomas Huth     const gchar *ignore_stderr;
744ff7b9b56SPeter Maydell     g_autofree char *shmem_opts = NULL;
745ff7b9b56SPeter Maydell     g_autofree char *shmem_path = NULL;
74671d36124SJuan Quintela     const char *kvm_opts = NULL;
7471e8a1faeSThomas Huth     const char *arch = qtest_get_arch();
7481e8a1faeSThomas Huth     const char *memory_size;
749c9961391SFabiano Rosas     const char *machine_alias, *machine_opts = "";
7505050ad2aSFabiano Rosas     g_autofree char *machine = NULL;
7511e8a1faeSThomas Huth 
7521e8a1faeSThomas Huth     if (args->use_shmem) {
7531e8a1faeSThomas Huth         if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
7541e8a1faeSThomas Huth             g_test_skip("/dev/shm is not supported");
75519da6edfSDaniel P. Berrangé             return -1;
7561e8a1faeSThomas Huth         }
7571e8a1faeSThomas Huth     }
7581e8a1faeSThomas Huth 
75995014994SDaniel P. Berrangé     got_src_stop = false;
76095014994SDaniel P. Berrangé     got_dst_resume = false;
7611e8a1faeSThomas Huth     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
7621e8a1faeSThomas Huth         memory_size = "150M";
763fa35b0cbSFabiano Rosas 
764fa35b0cbSFabiano Rosas         if (g_str_equal(arch, "i386")) {
765c9961391SFabiano Rosas             machine_alias = "pc";
766fa35b0cbSFabiano Rosas         } else {
767fa35b0cbSFabiano Rosas             machine_alias = "q35";
768fa35b0cbSFabiano Rosas         }
7693cb9c655SFabiano Rosas         arch_opts = g_strdup_printf(
7703cb9c655SFabiano Rosas             "-drive if=none,id=d0,file=%s,format=raw "
7713cb9c655SFabiano Rosas             "-device ide-hd,drive=d0,secs=1,cyls=1,heads=1", bootpath);
7721e8a1faeSThomas Huth         start_address = X86_TEST_MEM_START;
7731e8a1faeSThomas Huth         end_address = X86_TEST_MEM_END;
7741e8a1faeSThomas Huth     } else if (g_str_equal(arch, "s390x")) {
7751e8a1faeSThomas Huth         memory_size = "128M";
776c9961391SFabiano Rosas         machine_alias = "s390-ccw-virtio";
777832c732cSJuan Quintela         arch_opts = g_strdup_printf("-bios %s", bootpath);
7781e8a1faeSThomas Huth         start_address = S390_TEST_MEM_START;
7791e8a1faeSThomas Huth         end_address = S390_TEST_MEM_END;
7801e8a1faeSThomas Huth     } else if (strcmp(arch, "ppc64") == 0) {
7811e8a1faeSThomas Huth         memory_size = "256M";
78216c5c692SLaurent Vivier         start_address = PPC_TEST_MEM_START;
78316c5c692SLaurent Vivier         end_address = PPC_TEST_MEM_END;
784832c732cSJuan Quintela         arch_source = g_strdup_printf("-prom-env 'use-nvramrc?=true' -prom-env "
7851e8a1faeSThomas Huth                                       "'nvramrc=hex .\" _\" begin %x %x "
7861e8a1faeSThomas Huth                                       "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
7871e8a1faeSThomas Huth                                       "until'", end_address, start_address);
788c9961391SFabiano Rosas         machine_alias = "pseries";
789c9961391SFabiano Rosas         machine_opts = "vsmt=8";
790c9961391SFabiano Rosas         arch_opts = g_strdup("-nodefaults");
7911e8a1faeSThomas Huth     } else if (strcmp(arch, "aarch64") == 0) {
7921e8a1faeSThomas Huth         memory_size = "150M";
793c9961391SFabiano Rosas         machine_alias = "virt";
794c9961391SFabiano Rosas         machine_opts = "gic-version=max";
795c9961391SFabiano Rosas         arch_opts = g_strdup_printf("-cpu max -kernel %s", bootpath);
7961e8a1faeSThomas Huth         start_address = ARM_TEST_MEM_START;
7971e8a1faeSThomas Huth         end_address = ARM_TEST_MEM_END;
7981e8a1faeSThomas Huth     } else {
7991e8a1faeSThomas Huth         g_assert_not_reached();
8001e8a1faeSThomas Huth     }
8011e8a1faeSThomas Huth 
802a4729501SPeter Xu     if (!getenv("QTEST_LOG") && args->hide_stderr) {
8034dc8be38SBin Meng #ifndef _WIN32
8041e8a1faeSThomas Huth         ignore_stderr = "2>/dev/null";
8054dc8be38SBin Meng #else
8064dc8be38SBin Meng         /*
8074dc8be38SBin Meng          * On Windows the QEMU executable is created via CreateProcess() and
8084dc8be38SBin Meng          * IO redirection does not work, so don't bother adding IO redirection
8094dc8be38SBin Meng          * to the command line.
8104dc8be38SBin Meng          */
8114dc8be38SBin Meng         ignore_stderr = "";
8124dc8be38SBin Meng #endif
8131e8a1faeSThomas Huth     } else {
8141e8a1faeSThomas Huth         ignore_stderr = "";
8151e8a1faeSThomas Huth     }
8161e8a1faeSThomas Huth 
8171e8a1faeSThomas Huth     if (args->use_shmem) {
8181e8a1faeSThomas Huth         shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
8191e8a1faeSThomas Huth         shmem_opts = g_strdup_printf(
8201e8a1faeSThomas Huth             "-object memory-backend-file,id=mem0,size=%s"
8211e8a1faeSThomas Huth             ",mem-path=%s,share=on -numa node,memdev=mem0",
8221e8a1faeSThomas Huth             memory_size, shmem_path);
8231e8a1faeSThomas Huth     }
8241e8a1faeSThomas Huth 
82571d36124SJuan Quintela     if (args->use_dirty_ring) {
82671d36124SJuan Quintela         kvm_opts = ",dirty-ring-size=4096";
82771d36124SJuan Quintela     }
82871d36124SJuan Quintela 
8296c6d2330SFabiano Rosas     machine = resolve_machine_version(machine_alias, QEMU_ENV_SRC,
8305050ad2aSFabiano Rosas                                       QEMU_ENV_DST);
8316c6d2330SFabiano Rosas 
8325050ad2aSFabiano Rosas     g_test_message("Using machine type: %s", machine);
8335050ad2aSFabiano Rosas 
834bc28a611SJuan Quintela     cmd_source = g_strdup_printf("-accel kvm%s -accel tcg "
835c9961391SFabiano Rosas                                  "-machine %s,%s "
8361e8a1faeSThomas Huth                                  "-name source,debug-threads=on "
8371e8a1faeSThomas Huth                                  "-m %s "
8381e8a1faeSThomas Huth                                  "-serial file:%s/src_serial "
839832c732cSJuan Quintela                                  "%s %s %s %s %s",
84071d36124SJuan Quintela                                  kvm_opts ? kvm_opts : "",
8415050ad2aSFabiano Rosas                                  machine, machine_opts,
8421e8a1faeSThomas Huth                                  memory_size, tmpfs,
843832c732cSJuan Quintela                                  arch_opts ? arch_opts : "",
844832c732cSJuan Quintela                                  arch_source ? arch_source : "",
8450368ace8SJuan Quintela                                  shmem_opts ? shmem_opts : "",
84619da6edfSDaniel P. Berrangé                                  args->opts_source ? args->opts_source : "",
8471e8a1faeSThomas Huth                                  ignore_stderr);
848d795f474SJuan Quintela     if (!args->only_target) {
8495050ad2aSFabiano Rosas         *from = qtest_init_with_env(QEMU_ENV_SRC, cmd_source);
850cdf5ab55SDaniel P. Berrangé         qtest_qmp_set_event_callback(*from,
851cdf5ab55SDaniel P. Berrangé                                      migrate_watch_for_stop,
85295014994SDaniel P. Berrangé                                      &got_src_stop);
853d795f474SJuan Quintela     }
8541e8a1faeSThomas Huth 
855bc28a611SJuan Quintela     cmd_target = g_strdup_printf("-accel kvm%s -accel tcg "
856c9961391SFabiano Rosas                                  "-machine %s,%s "
8571e8a1faeSThomas Huth                                  "-name target,debug-threads=on "
8581e8a1faeSThomas Huth                                  "-m %s "
8591e8a1faeSThomas Huth                                  "-serial file:%s/dest_serial "
8601e8a1faeSThomas Huth                                  "-incoming %s "
861832c732cSJuan Quintela                                  "%s %s %s %s %s",
86271d36124SJuan Quintela                                  kvm_opts ? kvm_opts : "",
8635050ad2aSFabiano Rosas                                  machine, machine_opts,
8641e8a1faeSThomas Huth                                  memory_size, tmpfs, uri,
865832c732cSJuan Quintela                                  arch_opts ? arch_opts : "",
866832c732cSJuan Quintela                                  arch_target ? arch_target : "",
8670368ace8SJuan Quintela                                  shmem_opts ? shmem_opts : "",
86819da6edfSDaniel P. Berrangé                                  args->opts_target ? args->opts_target : "",
86919da6edfSDaniel P. Berrangé                                  ignore_stderr);
8705050ad2aSFabiano Rosas     *to = qtest_init_with_env(QEMU_ENV_DST, cmd_target);
871266ea334SDaniel P. Berrangé     qtest_qmp_set_event_callback(*to,
872266ea334SDaniel P. Berrangé                                  migrate_watch_for_resume,
87395014994SDaniel P. Berrangé                                  &got_dst_resume);
8741e8a1faeSThomas Huth 
8751e8a1faeSThomas Huth     /*
8761e8a1faeSThomas Huth      * Remove shmem file immediately to avoid memory leak in test failed case.
87796420a30SMichael Tokarev      * It's valid because QEMU has already opened this file
8781e8a1faeSThomas Huth      */
8791e8a1faeSThomas Huth     if (args->use_shmem) {
8801e8a1faeSThomas Huth         unlink(shmem_path);
8811e8a1faeSThomas Huth     }
8821e8a1faeSThomas Huth 
88319da6edfSDaniel P. Berrangé     return 0;
8841e8a1faeSThomas Huth }
8851e8a1faeSThomas Huth 
test_migrate_end(QTestState * from,QTestState * to,bool test_dest)8861e8a1faeSThomas Huth static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
8871e8a1faeSThomas Huth {
8881e8a1faeSThomas Huth     unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
8891e8a1faeSThomas Huth 
8901e8a1faeSThomas Huth     qtest_quit(from);
8911e8a1faeSThomas Huth 
8921e8a1faeSThomas Huth     if (test_dest) {
8931e8a1faeSThomas Huth         qtest_memread(to, start_address, &dest_byte_a, 1);
8941e8a1faeSThomas Huth 
8951e8a1faeSThomas Huth         /* Destination still running, wait for a byte to change */
8961e8a1faeSThomas Huth         do {
8971e8a1faeSThomas Huth             qtest_memread(to, start_address, &dest_byte_b, 1);
8981e8a1faeSThomas Huth             usleep(1000 * 10);
8991e8a1faeSThomas Huth         } while (dest_byte_a == dest_byte_b);
9001e8a1faeSThomas Huth 
901855436dbSDaniel P. Berrangé         qtest_qmp_assert_success(to, "{ 'execute' : 'stop'}");
9021e8a1faeSThomas Huth 
9031e8a1faeSThomas Huth         /* With it stopped, check nothing changes */
9041e8a1faeSThomas Huth         qtest_memread(to, start_address, &dest_byte_c, 1);
9051e8a1faeSThomas Huth         usleep(1000 * 200);
9061e8a1faeSThomas Huth         qtest_memread(to, start_address, &dest_byte_d, 1);
9071e8a1faeSThomas Huth         g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
9081e8a1faeSThomas Huth 
9091e8a1faeSThomas Huth         check_guests_ram(to);
9101e8a1faeSThomas Huth     }
9111e8a1faeSThomas Huth 
9121e8a1faeSThomas Huth     qtest_quit(to);
9131e8a1faeSThomas Huth 
9141e8a1faeSThomas Huth     cleanup("migsocket");
9151e8a1faeSThomas Huth     cleanup("src_serial");
9161e8a1faeSThomas Huth     cleanup("dest_serial");
9173dc35470SFabiano Rosas     cleanup(FILE_TEST_FILENAME);
9181e8a1faeSThomas Huth }
9191e8a1faeSThomas Huth 
92058d25e97SDaniel P. Berrangé #ifdef CONFIG_GNUTLS
92158d25e97SDaniel P. Berrangé struct TestMigrateTLSPSKData {
92258d25e97SDaniel P. Berrangé     char *workdir;
92358d25e97SDaniel P. Berrangé     char *workdiralt;
92458d25e97SDaniel P. Berrangé     char *pskfile;
92558d25e97SDaniel P. Berrangé     char *pskfilealt;
92658d25e97SDaniel P. Berrangé };
92758d25e97SDaniel P. Berrangé 
92858d25e97SDaniel P. Berrangé static void *
test_migrate_tls_psk_start_common(QTestState * from,QTestState * to,bool mismatch)92958d25e97SDaniel P. Berrangé test_migrate_tls_psk_start_common(QTestState *from,
93058d25e97SDaniel P. Berrangé                                   QTestState *to,
93158d25e97SDaniel P. Berrangé                                   bool mismatch)
93258d25e97SDaniel P. Berrangé {
93358d25e97SDaniel P. Berrangé     struct TestMigrateTLSPSKData *data =
93458d25e97SDaniel P. Berrangé         g_new0(struct TestMigrateTLSPSKData, 1);
93558d25e97SDaniel P. Berrangé 
93658d25e97SDaniel P. Berrangé     data->workdir = g_strdup_printf("%s/tlscredspsk0", tmpfs);
93758d25e97SDaniel P. Berrangé     data->pskfile = g_strdup_printf("%s/%s", data->workdir,
93858d25e97SDaniel P. Berrangé                                     QCRYPTO_TLS_CREDS_PSKFILE);
939413bebc0SBin Meng     g_mkdir_with_parents(data->workdir, 0700);
94058d25e97SDaniel P. Berrangé     test_tls_psk_init(data->pskfile);
94158d25e97SDaniel P. Berrangé 
94258d25e97SDaniel P. Berrangé     if (mismatch) {
94358d25e97SDaniel P. Berrangé         data->workdiralt = g_strdup_printf("%s/tlscredspskalt0", tmpfs);
94458d25e97SDaniel P. Berrangé         data->pskfilealt = g_strdup_printf("%s/%s", data->workdiralt,
94558d25e97SDaniel P. Berrangé                                            QCRYPTO_TLS_CREDS_PSKFILE);
946413bebc0SBin Meng         g_mkdir_with_parents(data->workdiralt, 0700);
94758d25e97SDaniel P. Berrangé         test_tls_psk_init_alt(data->pskfilealt);
94858d25e97SDaniel P. Berrangé     }
94958d25e97SDaniel P. Berrangé 
950aca04069SDaniel P. Berrangé     qtest_qmp_assert_success(from,
95158d25e97SDaniel P. Berrangé                              "{ 'execute': 'object-add',"
95258d25e97SDaniel P. Berrangé                              "  'arguments': { 'qom-type': 'tls-creds-psk',"
95358d25e97SDaniel P. Berrangé                              "                 'id': 'tlscredspsk0',"
95458d25e97SDaniel P. Berrangé                              "                 'endpoint': 'client',"
95558d25e97SDaniel P. Berrangé                              "                 'dir': %s,"
95658d25e97SDaniel P. Berrangé                              "                 'username': 'qemu'} }",
95758d25e97SDaniel P. Berrangé                              data->workdir);
95858d25e97SDaniel P. Berrangé 
959aca04069SDaniel P. Berrangé     qtest_qmp_assert_success(to,
96058d25e97SDaniel P. Berrangé                              "{ 'execute': 'object-add',"
96158d25e97SDaniel P. Berrangé                              "  'arguments': { 'qom-type': 'tls-creds-psk',"
96258d25e97SDaniel P. Berrangé                              "                 'id': 'tlscredspsk0',"
96358d25e97SDaniel P. Berrangé                              "                 'endpoint': 'server',"
96458d25e97SDaniel P. Berrangé                              "                 'dir': %s } }",
96558d25e97SDaniel P. Berrangé                              mismatch ? data->workdiralt : data->workdir);
96658d25e97SDaniel P. Berrangé 
96758d25e97SDaniel P. Berrangé     migrate_set_parameter_str(from, "tls-creds", "tlscredspsk0");
96858d25e97SDaniel P. Berrangé     migrate_set_parameter_str(to, "tls-creds", "tlscredspsk0");
96958d25e97SDaniel P. Berrangé 
97058d25e97SDaniel P. Berrangé     return data;
97158d25e97SDaniel P. Berrangé }
97258d25e97SDaniel P. Berrangé 
97358d25e97SDaniel P. Berrangé static void *
test_migrate_tls_psk_start_match(QTestState * from,QTestState * to)97458d25e97SDaniel P. Berrangé test_migrate_tls_psk_start_match(QTestState *from,
97558d25e97SDaniel P. Berrangé                                  QTestState *to)
97658d25e97SDaniel P. Berrangé {
97758d25e97SDaniel P. Berrangé     return test_migrate_tls_psk_start_common(from, to, false);
97858d25e97SDaniel P. Berrangé }
97958d25e97SDaniel P. Berrangé 
98058d25e97SDaniel P. Berrangé static void *
test_migrate_tls_psk_start_mismatch(QTestState * from,QTestState * to)98158d25e97SDaniel P. Berrangé test_migrate_tls_psk_start_mismatch(QTestState *from,
98258d25e97SDaniel P. Berrangé                                     QTestState *to)
98358d25e97SDaniel P. Berrangé {
98458d25e97SDaniel P. Berrangé     return test_migrate_tls_psk_start_common(from, to, true);
98558d25e97SDaniel P. Berrangé }
98658d25e97SDaniel P. Berrangé 
98758d25e97SDaniel P. Berrangé static void
test_migrate_tls_psk_finish(QTestState * from,QTestState * to,void * opaque)98858d25e97SDaniel P. Berrangé test_migrate_tls_psk_finish(QTestState *from,
98958d25e97SDaniel P. Berrangé                             QTestState *to,
99058d25e97SDaniel P. Berrangé                             void *opaque)
99158d25e97SDaniel P. Berrangé {
99258d25e97SDaniel P. Berrangé     struct TestMigrateTLSPSKData *data = opaque;
99358d25e97SDaniel P. Berrangé 
99458d25e97SDaniel P. Berrangé     test_tls_psk_cleanup(data->pskfile);
99558d25e97SDaniel P. Berrangé     if (data->pskfilealt) {
99658d25e97SDaniel P. Berrangé         test_tls_psk_cleanup(data->pskfilealt);
99758d25e97SDaniel P. Berrangé     }
99858d25e97SDaniel P. Berrangé     rmdir(data->workdir);
99958d25e97SDaniel P. Berrangé     if (data->workdiralt) {
100058d25e97SDaniel P. Berrangé         rmdir(data->workdiralt);
100158d25e97SDaniel P. Berrangé     }
100258d25e97SDaniel P. Berrangé 
100358d25e97SDaniel P. Berrangé     g_free(data->workdiralt);
100458d25e97SDaniel P. Berrangé     g_free(data->pskfilealt);
100558d25e97SDaniel P. Berrangé     g_free(data->workdir);
100658d25e97SDaniel P. Berrangé     g_free(data->pskfile);
100758d25e97SDaniel P. Berrangé     g_free(data);
100858d25e97SDaniel P. Berrangé }
1009d47b83b1SDaniel P. Berrangé 
1010d47b83b1SDaniel P. Berrangé #ifdef CONFIG_TASN1
1011d47b83b1SDaniel P. Berrangé typedef struct {
1012d47b83b1SDaniel P. Berrangé     char *workdir;
1013d47b83b1SDaniel P. Berrangé     char *keyfile;
1014d47b83b1SDaniel P. Berrangé     char *cacert;
1015d47b83b1SDaniel P. Berrangé     char *servercert;
1016d47b83b1SDaniel P. Berrangé     char *serverkey;
1017d47b83b1SDaniel P. Berrangé     char *clientcert;
1018d47b83b1SDaniel P. Berrangé     char *clientkey;
1019d47b83b1SDaniel P. Berrangé } TestMigrateTLSX509Data;
1020d47b83b1SDaniel P. Berrangé 
1021d47b83b1SDaniel P. Berrangé typedef struct {
1022d47b83b1SDaniel P. Berrangé     bool verifyclient;
1023d47b83b1SDaniel P. Berrangé     bool clientcert;
1024d47b83b1SDaniel P. Berrangé     bool hostileclient;
1025d47b83b1SDaniel P. Berrangé     bool authzclient;
1026d47b83b1SDaniel P. Berrangé     const char *certhostname;
1027d47b83b1SDaniel P. Berrangé     const char *certipaddr;
1028d47b83b1SDaniel P. Berrangé } TestMigrateTLSX509;
1029d47b83b1SDaniel P. Berrangé 
1030d47b83b1SDaniel P. Berrangé static void *
test_migrate_tls_x509_start_common(QTestState * from,QTestState * to,TestMigrateTLSX509 * args)1031d47b83b1SDaniel P. Berrangé test_migrate_tls_x509_start_common(QTestState *from,
1032d47b83b1SDaniel P. Berrangé                                    QTestState *to,
1033d47b83b1SDaniel P. Berrangé                                    TestMigrateTLSX509 *args)
1034d47b83b1SDaniel P. Berrangé {
1035d47b83b1SDaniel P. Berrangé     TestMigrateTLSX509Data *data = g_new0(TestMigrateTLSX509Data, 1);
1036d47b83b1SDaniel P. Berrangé 
1037d47b83b1SDaniel P. Berrangé     data->workdir = g_strdup_printf("%s/tlscredsx5090", tmpfs);
1038d47b83b1SDaniel P. Berrangé     data->keyfile = g_strdup_printf("%s/key.pem", data->workdir);
1039d47b83b1SDaniel P. Berrangé 
1040d47b83b1SDaniel P. Berrangé     data->cacert = g_strdup_printf("%s/ca-cert.pem", data->workdir);
1041d47b83b1SDaniel P. Berrangé     data->serverkey = g_strdup_printf("%s/server-key.pem", data->workdir);
1042d47b83b1SDaniel P. Berrangé     data->servercert = g_strdup_printf("%s/server-cert.pem", data->workdir);
1043d47b83b1SDaniel P. Berrangé     if (args->clientcert) {
1044d47b83b1SDaniel P. Berrangé         data->clientkey = g_strdup_printf("%s/client-key.pem", data->workdir);
1045d47b83b1SDaniel P. Berrangé         data->clientcert = g_strdup_printf("%s/client-cert.pem", data->workdir);
1046d47b83b1SDaniel P. Berrangé     }
1047d47b83b1SDaniel P. Berrangé 
1048413bebc0SBin Meng     g_mkdir_with_parents(data->workdir, 0700);
1049d47b83b1SDaniel P. Berrangé 
1050d47b83b1SDaniel P. Berrangé     test_tls_init(data->keyfile);
10512549f610SBin Meng #ifndef _WIN32
1052d47b83b1SDaniel P. Berrangé     g_assert(link(data->keyfile, data->serverkey) == 0);
10532549f610SBin Meng #else
10542549f610SBin Meng     g_assert(CreateHardLink(data->serverkey, data->keyfile, NULL) != 0);
10552549f610SBin Meng #endif
1056d47b83b1SDaniel P. Berrangé     if (args->clientcert) {
10572549f610SBin Meng #ifndef _WIN32
1058d47b83b1SDaniel P. Berrangé         g_assert(link(data->keyfile, data->clientkey) == 0);
10592549f610SBin Meng #else
10602549f610SBin Meng         g_assert(CreateHardLink(data->clientkey, data->keyfile, NULL) != 0);
10612549f610SBin Meng #endif
1062d47b83b1SDaniel P. Berrangé     }
1063d47b83b1SDaniel P. Berrangé 
1064d47b83b1SDaniel P. Berrangé     TLS_ROOT_REQ_SIMPLE(cacertreq, data->cacert);
1065d47b83b1SDaniel P. Berrangé     if (args->clientcert) {
1066d47b83b1SDaniel P. Berrangé         TLS_CERT_REQ_SIMPLE_CLIENT(servercertreq, cacertreq,
1067d47b83b1SDaniel P. Berrangé                                    args->hostileclient ?
1068d47b83b1SDaniel P. Berrangé                                    QCRYPTO_TLS_TEST_CLIENT_HOSTILE_NAME :
1069d47b83b1SDaniel P. Berrangé                                    QCRYPTO_TLS_TEST_CLIENT_NAME,
1070d47b83b1SDaniel P. Berrangé                                    data->clientcert);
1071d47b83b1SDaniel P. Berrangé     }
1072d47b83b1SDaniel P. Berrangé 
1073d47b83b1SDaniel P. Berrangé     TLS_CERT_REQ_SIMPLE_SERVER(clientcertreq, cacertreq,
1074d47b83b1SDaniel P. Berrangé                                data->servercert,
1075d47b83b1SDaniel P. Berrangé                                args->certhostname,
1076d47b83b1SDaniel P. Berrangé                                args->certipaddr);
1077d47b83b1SDaniel P. Berrangé 
1078aca04069SDaniel P. Berrangé     qtest_qmp_assert_success(from,
1079d47b83b1SDaniel P. Berrangé                              "{ 'execute': 'object-add',"
1080d47b83b1SDaniel P. Berrangé                              "  'arguments': { 'qom-type': 'tls-creds-x509',"
1081d47b83b1SDaniel P. Berrangé                              "                 'id': 'tlscredsx509client0',"
1082d47b83b1SDaniel P. Berrangé                              "                 'endpoint': 'client',"
1083d47b83b1SDaniel P. Berrangé                              "                 'dir': %s,"
1084d47b83b1SDaniel P. Berrangé                              "                 'sanity-check': true,"
1085d47b83b1SDaniel P. Berrangé                              "                 'verify-peer': true} }",
1086d47b83b1SDaniel P. Berrangé                              data->workdir);
1087d47b83b1SDaniel P. Berrangé     migrate_set_parameter_str(from, "tls-creds", "tlscredsx509client0");
1088d47b83b1SDaniel P. Berrangé     if (args->certhostname) {
1089d47b83b1SDaniel P. Berrangé         migrate_set_parameter_str(from, "tls-hostname", args->certhostname);
1090d47b83b1SDaniel P. Berrangé     }
1091d47b83b1SDaniel P. Berrangé 
1092aca04069SDaniel P. Berrangé     qtest_qmp_assert_success(to,
1093d47b83b1SDaniel P. Berrangé                              "{ 'execute': 'object-add',"
1094d47b83b1SDaniel P. Berrangé                              "  'arguments': { 'qom-type': 'tls-creds-x509',"
1095d47b83b1SDaniel P. Berrangé                              "                 'id': 'tlscredsx509server0',"
1096d47b83b1SDaniel P. Berrangé                              "                 'endpoint': 'server',"
1097d47b83b1SDaniel P. Berrangé                              "                 'dir': %s,"
1098d47b83b1SDaniel P. Berrangé                              "                 'sanity-check': true,"
1099d47b83b1SDaniel P. Berrangé                              "                 'verify-peer': %i} }",
1100d47b83b1SDaniel P. Berrangé                              data->workdir, args->verifyclient);
1101d47b83b1SDaniel P. Berrangé     migrate_set_parameter_str(to, "tls-creds", "tlscredsx509server0");
1102d47b83b1SDaniel P. Berrangé 
1103d47b83b1SDaniel P. Berrangé     if (args->authzclient) {
1104aca04069SDaniel P. Berrangé         qtest_qmp_assert_success(to,
1105d47b83b1SDaniel P. Berrangé                                  "{ 'execute': 'object-add',"
1106d47b83b1SDaniel P. Berrangé                                  "  'arguments': { 'qom-type': 'authz-simple',"
1107d47b83b1SDaniel P. Berrangé                                  "                 'id': 'tlsauthz0',"
1108d47b83b1SDaniel P. Berrangé                                  "                 'identity': %s} }",
1109d47b83b1SDaniel P. Berrangé                                  "CN=" QCRYPTO_TLS_TEST_CLIENT_NAME);
1110d47b83b1SDaniel P. Berrangé         migrate_set_parameter_str(to, "tls-authz", "tlsauthz0");
1111d47b83b1SDaniel P. Berrangé     }
1112d47b83b1SDaniel P. Berrangé 
1113d47b83b1SDaniel P. Berrangé     return data;
1114d47b83b1SDaniel P. Berrangé }
1115d47b83b1SDaniel P. Berrangé 
1116d47b83b1SDaniel P. Berrangé /*
1117d47b83b1SDaniel P. Berrangé  * The normal case: match server's cert hostname against
1118d47b83b1SDaniel P. Berrangé  * whatever host we were telling QEMU to connect to (if any)
1119d47b83b1SDaniel P. Berrangé  */
1120d47b83b1SDaniel P. Berrangé static void *
test_migrate_tls_x509_start_default_host(QTestState * from,QTestState * to)1121d47b83b1SDaniel P. Berrangé test_migrate_tls_x509_start_default_host(QTestState *from,
1122d47b83b1SDaniel P. Berrangé                                          QTestState *to)
1123d47b83b1SDaniel P. Berrangé {
1124d47b83b1SDaniel P. Berrangé     TestMigrateTLSX509 args = {
1125d47b83b1SDaniel P. Berrangé         .verifyclient = true,
1126d47b83b1SDaniel P. Berrangé         .clientcert = true,
1127d47b83b1SDaniel P. Berrangé         .certipaddr = "127.0.0.1"
1128d47b83b1SDaniel P. Berrangé     };
1129d47b83b1SDaniel P. Berrangé     return test_migrate_tls_x509_start_common(from, to, &args);
1130d47b83b1SDaniel P. Berrangé }
1131d47b83b1SDaniel P. Berrangé 
1132d47b83b1SDaniel P. Berrangé /*
1133d47b83b1SDaniel P. Berrangé  * The unusual case: the server's cert is different from
1134d47b83b1SDaniel P. Berrangé  * the address we're telling QEMU to connect to (if any),
1135d47b83b1SDaniel P. Berrangé  * so we must give QEMU an explicit hostname to validate
1136d47b83b1SDaniel P. Berrangé  */
1137d47b83b1SDaniel P. Berrangé static void *
test_migrate_tls_x509_start_override_host(QTestState * from,QTestState * to)1138d47b83b1SDaniel P. Berrangé test_migrate_tls_x509_start_override_host(QTestState *from,
1139d47b83b1SDaniel P. Berrangé                                           QTestState *to)
1140d47b83b1SDaniel P. Berrangé {
1141d47b83b1SDaniel P. Berrangé     TestMigrateTLSX509 args = {
1142d47b83b1SDaniel P. Berrangé         .verifyclient = true,
1143d47b83b1SDaniel P. Berrangé         .clientcert = true,
1144d47b83b1SDaniel P. Berrangé         .certhostname = "qemu.org",
1145d47b83b1SDaniel P. Berrangé     };
1146d47b83b1SDaniel P. Berrangé     return test_migrate_tls_x509_start_common(from, to, &args);
1147d47b83b1SDaniel P. Berrangé }
1148d47b83b1SDaniel P. Berrangé 
1149d47b83b1SDaniel P. Berrangé /*
1150d47b83b1SDaniel P. Berrangé  * The unusual case: the server's cert is different from
1151d47b83b1SDaniel P. Berrangé  * the address we're telling QEMU to connect to, and so we
1152d47b83b1SDaniel P. Berrangé  * expect the client to reject the server
1153d47b83b1SDaniel P. Berrangé  */
1154d47b83b1SDaniel P. Berrangé static void *
test_migrate_tls_x509_start_mismatch_host(QTestState * from,QTestState * to)1155d47b83b1SDaniel P. Berrangé test_migrate_tls_x509_start_mismatch_host(QTestState *from,
1156d47b83b1SDaniel P. Berrangé                                           QTestState *to)
1157d47b83b1SDaniel P. Berrangé {
1158d47b83b1SDaniel P. Berrangé     TestMigrateTLSX509 args = {
1159d47b83b1SDaniel P. Berrangé         .verifyclient = true,
1160d47b83b1SDaniel P. Berrangé         .clientcert = true,
1161d47b83b1SDaniel P. Berrangé         .certipaddr = "10.0.0.1",
1162d47b83b1SDaniel P. Berrangé     };
1163d47b83b1SDaniel P. Berrangé     return test_migrate_tls_x509_start_common(from, to, &args);
1164d47b83b1SDaniel P. Berrangé }
1165d47b83b1SDaniel P. Berrangé 
1166d47b83b1SDaniel P. Berrangé static void *
test_migrate_tls_x509_start_friendly_client(QTestState * from,QTestState * to)1167d47b83b1SDaniel P. Berrangé test_migrate_tls_x509_start_friendly_client(QTestState *from,
1168d47b83b1SDaniel P. Berrangé                                             QTestState *to)
1169d47b83b1SDaniel P. Berrangé {
1170d47b83b1SDaniel P. Berrangé     TestMigrateTLSX509 args = {
1171d47b83b1SDaniel P. Berrangé         .verifyclient = true,
1172d47b83b1SDaniel P. Berrangé         .clientcert = true,
1173d47b83b1SDaniel P. Berrangé         .authzclient = true,
1174d47b83b1SDaniel P. Berrangé         .certipaddr = "127.0.0.1",
1175d47b83b1SDaniel P. Berrangé     };
1176d47b83b1SDaniel P. Berrangé     return test_migrate_tls_x509_start_common(from, to, &args);
1177d47b83b1SDaniel P. Berrangé }
1178d47b83b1SDaniel P. Berrangé 
1179d47b83b1SDaniel P. Berrangé static void *
test_migrate_tls_x509_start_hostile_client(QTestState * from,QTestState * to)1180d47b83b1SDaniel P. Berrangé test_migrate_tls_x509_start_hostile_client(QTestState *from,
1181d47b83b1SDaniel P. Berrangé                                            QTestState *to)
1182d47b83b1SDaniel P. Berrangé {
1183d47b83b1SDaniel P. Berrangé     TestMigrateTLSX509 args = {
1184d47b83b1SDaniel P. Berrangé         .verifyclient = true,
1185d47b83b1SDaniel P. Berrangé         .clientcert = true,
1186d47b83b1SDaniel P. Berrangé         .hostileclient = true,
1187d47b83b1SDaniel P. Berrangé         .authzclient = true,
1188d47b83b1SDaniel P. Berrangé         .certipaddr = "127.0.0.1",
1189d47b83b1SDaniel P. Berrangé     };
1190d47b83b1SDaniel P. Berrangé     return test_migrate_tls_x509_start_common(from, to, &args);
1191d47b83b1SDaniel P. Berrangé }
1192d47b83b1SDaniel P. Berrangé 
1193d47b83b1SDaniel P. Berrangé /*
1194d47b83b1SDaniel P. Berrangé  * The case with no client certificate presented,
1195d47b83b1SDaniel P. Berrangé  * and no server verification
1196d47b83b1SDaniel P. Berrangé  */
1197d47b83b1SDaniel P. Berrangé static void *
test_migrate_tls_x509_start_allow_anon_client(QTestState * from,QTestState * to)1198d47b83b1SDaniel P. Berrangé test_migrate_tls_x509_start_allow_anon_client(QTestState *from,
1199d47b83b1SDaniel P. Berrangé                                               QTestState *to)
1200d47b83b1SDaniel P. Berrangé {
1201d47b83b1SDaniel P. Berrangé     TestMigrateTLSX509 args = {
1202d47b83b1SDaniel P. Berrangé         .certipaddr = "127.0.0.1",
1203d47b83b1SDaniel P. Berrangé     };
1204d47b83b1SDaniel P. Berrangé     return test_migrate_tls_x509_start_common(from, to, &args);
1205d47b83b1SDaniel P. Berrangé }
1206d47b83b1SDaniel P. Berrangé 
1207d47b83b1SDaniel P. Berrangé /*
1208d47b83b1SDaniel P. Berrangé  * The case with no client certificate presented,
1209d47b83b1SDaniel P. Berrangé  * and server verification rejecting
1210d47b83b1SDaniel P. Berrangé  */
1211d47b83b1SDaniel P. Berrangé static void *
test_migrate_tls_x509_start_reject_anon_client(QTestState * from,QTestState * to)1212d47b83b1SDaniel P. Berrangé test_migrate_tls_x509_start_reject_anon_client(QTestState *from,
1213d47b83b1SDaniel P. Berrangé                                                QTestState *to)
1214d47b83b1SDaniel P. Berrangé {
1215d47b83b1SDaniel P. Berrangé     TestMigrateTLSX509 args = {
1216d47b83b1SDaniel P. Berrangé         .verifyclient = true,
1217d47b83b1SDaniel P. Berrangé         .certipaddr = "127.0.0.1",
1218d47b83b1SDaniel P. Berrangé     };
1219d47b83b1SDaniel P. Berrangé     return test_migrate_tls_x509_start_common(from, to, &args);
1220d47b83b1SDaniel P. Berrangé }
1221d47b83b1SDaniel P. Berrangé 
1222d47b83b1SDaniel P. Berrangé static void
test_migrate_tls_x509_finish(QTestState * from,QTestState * to,void * opaque)1223d47b83b1SDaniel P. Berrangé test_migrate_tls_x509_finish(QTestState *from,
1224d47b83b1SDaniel P. Berrangé                              QTestState *to,
1225d47b83b1SDaniel P. Berrangé                              void *opaque)
1226d47b83b1SDaniel P. Berrangé {
1227d47b83b1SDaniel P. Berrangé     TestMigrateTLSX509Data *data = opaque;
1228d47b83b1SDaniel P. Berrangé 
1229d47b83b1SDaniel P. Berrangé     test_tls_cleanup(data->keyfile);
1230d47b83b1SDaniel P. Berrangé     g_free(data->keyfile);
12310f0a9e4eSThomas Huth 
12320f0a9e4eSThomas Huth     unlink(data->cacert);
12330f0a9e4eSThomas Huth     g_free(data->cacert);
12340f0a9e4eSThomas Huth     unlink(data->servercert);
12350f0a9e4eSThomas Huth     g_free(data->servercert);
12360f0a9e4eSThomas Huth     unlink(data->serverkey);
12370f0a9e4eSThomas Huth     g_free(data->serverkey);
12380f0a9e4eSThomas Huth 
12390f0a9e4eSThomas Huth     if (data->clientcert) {
12400f0a9e4eSThomas Huth         unlink(data->clientcert);
12410f0a9e4eSThomas Huth         g_free(data->clientcert);
12420f0a9e4eSThomas Huth     }
12430f0a9e4eSThomas Huth     if (data->clientkey) {
12440f0a9e4eSThomas Huth         unlink(data->clientkey);
12450f0a9e4eSThomas Huth         g_free(data->clientkey);
12460f0a9e4eSThomas Huth     }
12470f0a9e4eSThomas Huth 
12480f0a9e4eSThomas Huth     rmdir(data->workdir);
12490f0a9e4eSThomas Huth     g_free(data->workdir);
12500f0a9e4eSThomas Huth 
1251d47b83b1SDaniel P. Berrangé     g_free(data);
1252d47b83b1SDaniel P. Berrangé }
1253d47b83b1SDaniel P. Berrangé #endif /* CONFIG_TASN1 */
125458d25e97SDaniel P. Berrangé #endif /* CONFIG_GNUTLS */
125558d25e97SDaniel P. Berrangé 
1256dc066da8SLukas Straub static void *
test_migrate_compress_start(QTestState * from,QTestState * to)1257dc066da8SLukas Straub test_migrate_compress_start(QTestState *from,
1258dc066da8SLukas Straub                             QTestState *to)
1259dc066da8SLukas Straub {
1260dc066da8SLukas Straub     migrate_set_parameter_int(from, "compress-level", 1);
1261dc066da8SLukas Straub     migrate_set_parameter_int(from, "compress-threads", 4);
1262dc066da8SLukas Straub     migrate_set_parameter_bool(from, "compress-wait-thread", true);
1263dc066da8SLukas Straub     migrate_set_parameter_int(to, "decompress-threads", 4);
1264dc066da8SLukas Straub 
1265dc066da8SLukas Straub     migrate_set_capability(from, "compress", true);
1266dc066da8SLukas Straub     migrate_set_capability(to, "compress", true);
1267dc066da8SLukas Straub 
1268dc066da8SLukas Straub     return NULL;
1269dc066da8SLukas Straub }
1270dc066da8SLukas Straub 
1271dc066da8SLukas Straub static void *
test_migrate_compress_nowait_start(QTestState * from,QTestState * to)1272dc066da8SLukas Straub test_migrate_compress_nowait_start(QTestState *from,
1273dc066da8SLukas Straub                                    QTestState *to)
1274dc066da8SLukas Straub {
1275dc066da8SLukas Straub     migrate_set_parameter_int(from, "compress-level", 9);
1276dc066da8SLukas Straub     migrate_set_parameter_int(from, "compress-threads", 1);
1277dc066da8SLukas Straub     migrate_set_parameter_bool(from, "compress-wait-thread", false);
1278dc066da8SLukas Straub     migrate_set_parameter_int(to, "decompress-threads", 1);
1279dc066da8SLukas Straub 
1280dc066da8SLukas Straub     migrate_set_capability(from, "compress", true);
1281dc066da8SLukas Straub     migrate_set_capability(to, "compress", true);
1282dc066da8SLukas Straub 
1283dc066da8SLukas Straub     return NULL;
1284dc066da8SLukas Straub }
1285dc066da8SLukas Straub 
migrate_postcopy_prepare(QTestState ** from_ptr,QTestState ** to_ptr,MigrateCommon * args)12861e8a1faeSThomas Huth static int migrate_postcopy_prepare(QTestState **from_ptr,
12871e8a1faeSThomas Huth                                     QTestState **to_ptr,
1288d1a27b16SPeter Xu                                     MigrateCommon *args)
12891e8a1faeSThomas Huth {
12901e8a1faeSThomas Huth     QTestState *from, *to;
12911e8a1faeSThomas Huth 
129206c48d6bSWei Wang     if (test_migrate_start(&from, &to, "defer", &args->start)) {
12931e8a1faeSThomas Huth         return -1;
12941e8a1faeSThomas Huth     }
12951e8a1faeSThomas Huth 
1296d1a27b16SPeter Xu     if (args->start_hook) {
1297d1a27b16SPeter Xu         args->postcopy_data = args->start_hook(from, to);
1298d1a27b16SPeter Xu     }
1299d1a27b16SPeter Xu 
13001e8a1faeSThomas Huth     migrate_set_capability(from, "postcopy-ram", true);
13011e8a1faeSThomas Huth     migrate_set_capability(to, "postcopy-ram", true);
13021e8a1faeSThomas Huth     migrate_set_capability(to, "postcopy-blocktime", true);
13031e8a1faeSThomas Huth 
13048f6fe915SPeter Xu     if (args->postcopy_preempt) {
13058f6fe915SPeter Xu         migrate_set_capability(from, "postcopy-preempt", true);
13068f6fe915SPeter Xu         migrate_set_capability(to, "postcopy-preempt", true);
13078f6fe915SPeter Xu     }
13088f6fe915SPeter Xu 
1309886dfe9dSDaniel P. Berrangé     migrate_ensure_non_converge(from);
13101e8a1faeSThomas Huth 
1311e02f56e3SDaniel P. Berrangé     migrate_prepare_for_dirty_mem(from);
131206c48d6bSWei Wang     qtest_qmp_assert_success(to, "{ 'execute': 'migrate-incoming',"
13138e3766eeSHet Gala                              "  'arguments': { "
13148e3766eeSHet Gala                              "      'channels': [ { 'channel-type': 'main',"
13158e3766eeSHet Gala                              "      'addr': { 'transport': 'socket',"
13168e3766eeSHet Gala                              "                'type': 'inet',"
13178e3766eeSHet Gala                              "                'host': '127.0.0.1',"
13188e3766eeSHet Gala                              "                'port': '0' } } ] } }");
1319e02f56e3SDaniel P. Berrangé 
13201e8a1faeSThomas Huth     /* Wait for the first serial output from the source */
13211e8a1faeSThomas Huth     wait_for_serial("src_serial");
13221e8a1faeSThomas Huth 
132306c48d6bSWei Wang     g_autofree char *uri = migrate_get_socket_address(to, "socket-address");
13241e8a1faeSThomas Huth     migrate_qmp(from, uri, "{}");
13251e8a1faeSThomas Huth 
1326e02f56e3SDaniel P. Berrangé     migrate_wait_for_dirty_mem(from, to);
13271e8a1faeSThomas Huth 
13281e8a1faeSThomas Huth     *from_ptr = from;
13291e8a1faeSThomas Huth     *to_ptr = to;
13301e8a1faeSThomas Huth 
13311e8a1faeSThomas Huth     return 0;
13321e8a1faeSThomas Huth }
13331e8a1faeSThomas Huth 
migrate_postcopy_complete(QTestState * from,QTestState * to,MigrateCommon * args)1334d1a27b16SPeter Xu static void migrate_postcopy_complete(QTestState *from, QTestState *to,
1335d1a27b16SPeter Xu                                       MigrateCommon *args)
13361e8a1faeSThomas Huth {
13371e8a1faeSThomas Huth     wait_for_migration_complete(from);
13381e8a1faeSThomas Huth 
13391e8a1faeSThomas Huth     /* Make sure we get at least one "B" on destination */
13401e8a1faeSThomas Huth     wait_for_serial("dest_serial");
13411e8a1faeSThomas Huth 
13421e8a1faeSThomas Huth     if (uffd_feature_thread_id) {
13431e8a1faeSThomas Huth         read_blocktime(to);
13441e8a1faeSThomas Huth     }
13451e8a1faeSThomas Huth 
1346d1a27b16SPeter Xu     if (args->finish_hook) {
1347d1a27b16SPeter Xu         args->finish_hook(from, to, args->postcopy_data);
1348d1a27b16SPeter Xu         args->postcopy_data = NULL;
1349d1a27b16SPeter Xu     }
1350d1a27b16SPeter Xu 
13511e8a1faeSThomas Huth     test_migrate_end(from, to, true);
13521e8a1faeSThomas Huth }
13531e8a1faeSThomas Huth 
test_postcopy_common(MigrateCommon * args)1354d1a27b16SPeter Xu static void test_postcopy_common(MigrateCommon *args)
1355d1a27b16SPeter Xu {
1356d1a27b16SPeter Xu     QTestState *from, *to;
1357d1a27b16SPeter Xu 
1358d1a27b16SPeter Xu     if (migrate_postcopy_prepare(&from, &to, args)) {
1359d1a27b16SPeter Xu         return;
1360d1a27b16SPeter Xu     }
1361d1a27b16SPeter Xu     migrate_postcopy_start(from, to);
1362d1a27b16SPeter Xu     migrate_postcopy_complete(from, to, args);
1363d1a27b16SPeter Xu }
1364d1a27b16SPeter Xu 
test_postcopy(void)13651e8a1faeSThomas Huth static void test_postcopy(void)
13661e8a1faeSThomas Huth {
1367d1a27b16SPeter Xu     MigrateCommon args = { };
13681e8a1faeSThomas Huth 
1369d1a27b16SPeter Xu     test_postcopy_common(&args);
13701e8a1faeSThomas Huth }
1371d1a27b16SPeter Xu 
test_postcopy_compress(void)1372dc066da8SLukas Straub static void test_postcopy_compress(void)
1373dc066da8SLukas Straub {
1374dc066da8SLukas Straub     MigrateCommon args = {
1375dc066da8SLukas Straub         .start_hook = test_migrate_compress_start
1376dc066da8SLukas Straub     };
1377dc066da8SLukas Straub 
1378dc066da8SLukas Straub     test_postcopy_common(&args);
1379dc066da8SLukas Straub }
1380dc066da8SLukas Straub 
test_postcopy_preempt(void)13818f6fe915SPeter Xu static void test_postcopy_preempt(void)
13828f6fe915SPeter Xu {
13838f6fe915SPeter Xu     MigrateCommon args = {
13848f6fe915SPeter Xu         .postcopy_preempt = true,
13858f6fe915SPeter Xu     };
13868f6fe915SPeter Xu 
13878f6fe915SPeter Xu     test_postcopy_common(&args);
13888f6fe915SPeter Xu }
13898f6fe915SPeter Xu 
1390d1a27b16SPeter Xu #ifdef CONFIG_GNUTLS
test_postcopy_tls_psk(void)1391d1a27b16SPeter Xu static void test_postcopy_tls_psk(void)
1392d1a27b16SPeter Xu {
1393d1a27b16SPeter Xu     MigrateCommon args = {
1394d1a27b16SPeter Xu         .start_hook = test_migrate_tls_psk_start_match,
1395d1a27b16SPeter Xu         .finish_hook = test_migrate_tls_psk_finish,
1396d1a27b16SPeter Xu     };
1397d1a27b16SPeter Xu 
1398d1a27b16SPeter Xu     test_postcopy_common(&args);
13991e8a1faeSThomas Huth }
14008f6fe915SPeter Xu 
test_postcopy_preempt_tls_psk(void)14018f6fe915SPeter Xu static void test_postcopy_preempt_tls_psk(void)
14028f6fe915SPeter Xu {
14038f6fe915SPeter Xu     MigrateCommon args = {
14048f6fe915SPeter Xu         .postcopy_preempt = true,
14058f6fe915SPeter Xu         .start_hook = test_migrate_tls_psk_start_match,
14068f6fe915SPeter Xu         .finish_hook = test_migrate_tls_psk_finish,
14078f6fe915SPeter Xu     };
14088f6fe915SPeter Xu 
14098f6fe915SPeter Xu     test_postcopy_common(&args);
14108f6fe915SPeter Xu }
1411d1a27b16SPeter Xu #endif
14121e8a1faeSThomas Huth 
wait_for_postcopy_status(QTestState * one,const char * status)14137bca2bb7SFabiano Rosas static void wait_for_postcopy_status(QTestState *one, const char *status)
14147bca2bb7SFabiano Rosas {
14157bca2bb7SFabiano Rosas     wait_for_migration_status(one, status,
14167bca2bb7SFabiano Rosas                               (const char * []) { "failed", "active",
14177bca2bb7SFabiano Rosas                                                   "completed", NULL });
14187bca2bb7SFabiano Rosas }
14197bca2bb7SFabiano Rosas 
14207bca2bb7SFabiano Rosas #ifndef _WIN32
postcopy_recover_fail(QTestState * from,QTestState * to)14217bca2bb7SFabiano Rosas static void postcopy_recover_fail(QTestState *from, QTestState *to)
14227bca2bb7SFabiano Rosas {
14237bca2bb7SFabiano Rosas     int ret, pair1[2], pair2[2];
14247bca2bb7SFabiano Rosas     char c;
14257bca2bb7SFabiano Rosas 
14267bca2bb7SFabiano Rosas     /* Create two unrelated socketpairs */
14277bca2bb7SFabiano Rosas     ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair1);
14287bca2bb7SFabiano Rosas     g_assert_cmpint(ret, ==, 0);
14297bca2bb7SFabiano Rosas 
14307bca2bb7SFabiano Rosas     ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair2);
14317bca2bb7SFabiano Rosas     g_assert_cmpint(ret, ==, 0);
14327bca2bb7SFabiano Rosas 
14337bca2bb7SFabiano Rosas     /*
14347bca2bb7SFabiano Rosas      * Give the guests unpaired ends of the sockets, so they'll all blocked
14357bca2bb7SFabiano Rosas      * at reading.  This mimics a wrong channel established.
14367bca2bb7SFabiano Rosas      */
14377bca2bb7SFabiano Rosas     qtest_qmp_fds_assert_success(from, &pair1[0], 1,
14387bca2bb7SFabiano Rosas                                  "{ 'execute': 'getfd',"
14397bca2bb7SFabiano Rosas                                  "  'arguments': { 'fdname': 'fd-mig' }}");
14407bca2bb7SFabiano Rosas     qtest_qmp_fds_assert_success(to, &pair2[0], 1,
14417bca2bb7SFabiano Rosas                                  "{ 'execute': 'getfd',"
14427bca2bb7SFabiano Rosas                                  "  'arguments': { 'fdname': 'fd-mig' }}");
14437bca2bb7SFabiano Rosas 
14447bca2bb7SFabiano Rosas     /*
14457bca2bb7SFabiano Rosas      * Write the 1st byte as QEMU_VM_COMMAND (0x8) for the dest socket, to
14467bca2bb7SFabiano Rosas      * emulate the 1st byte of a real recovery, but stops from there to
14477bca2bb7SFabiano Rosas      * keep dest QEMU in RECOVER.  This is needed so that we can kick off
14487bca2bb7SFabiano Rosas      * the recover process on dest QEMU (by triggering the G_IO_IN event).
14497bca2bb7SFabiano Rosas      *
14507bca2bb7SFabiano Rosas      * NOTE: this trick is not needed on src QEMUs, because src doesn't
14517bca2bb7SFabiano Rosas      * rely on an pre-existing G_IO_IN event, so it will always trigger the
14527bca2bb7SFabiano Rosas      * upcoming recovery anyway even if it can read nothing.
14537bca2bb7SFabiano Rosas      */
14547bca2bb7SFabiano Rosas #define QEMU_VM_COMMAND              0x08
14557bca2bb7SFabiano Rosas     c = QEMU_VM_COMMAND;
14567bca2bb7SFabiano Rosas     ret = send(pair2[1], &c, 1, 0);
14577bca2bb7SFabiano Rosas     g_assert_cmpint(ret, ==, 1);
14587bca2bb7SFabiano Rosas 
14597bca2bb7SFabiano Rosas     migrate_recover(to, "fd:fd-mig");
14607bca2bb7SFabiano Rosas     migrate_qmp(from, "fd:fd-mig", "{'resume': true}");
14617bca2bb7SFabiano Rosas 
14627bca2bb7SFabiano Rosas     /*
14637bca2bb7SFabiano Rosas      * Make sure both QEMU instances will go into RECOVER stage, then test
14647bca2bb7SFabiano Rosas      * kicking them out using migrate-pause.
14657bca2bb7SFabiano Rosas      */
14667bca2bb7SFabiano Rosas     wait_for_postcopy_status(from, "postcopy-recover");
14677bca2bb7SFabiano Rosas     wait_for_postcopy_status(to, "postcopy-recover");
14687bca2bb7SFabiano Rosas 
14697bca2bb7SFabiano Rosas     /*
14707bca2bb7SFabiano Rosas      * This would be issued by the admin upon noticing the hang, we should
14717bca2bb7SFabiano Rosas      * make sure we're able to kick this out.
14727bca2bb7SFabiano Rosas      */
14737bca2bb7SFabiano Rosas     migrate_pause(from);
14747bca2bb7SFabiano Rosas     wait_for_postcopy_status(from, "postcopy-paused");
14757bca2bb7SFabiano Rosas 
14767bca2bb7SFabiano Rosas     /* Do the same test on dest */
14777bca2bb7SFabiano Rosas     migrate_pause(to);
14787bca2bb7SFabiano Rosas     wait_for_postcopy_status(to, "postcopy-paused");
14797bca2bb7SFabiano Rosas 
14807bca2bb7SFabiano Rosas     close(pair1[0]);
14817bca2bb7SFabiano Rosas     close(pair1[1]);
14827bca2bb7SFabiano Rosas     close(pair2[0]);
14837bca2bb7SFabiano Rosas     close(pair2[1]);
14847bca2bb7SFabiano Rosas }
14857bca2bb7SFabiano Rosas #endif /* _WIN32 */
14867bca2bb7SFabiano Rosas 
test_postcopy_recovery_common(MigrateCommon * args)1487767fa9cfSPeter Xu static void test_postcopy_recovery_common(MigrateCommon *args)
14881e8a1faeSThomas Huth {
14891e8a1faeSThomas Huth     QTestState *from, *to;
1490ff7b9b56SPeter Maydell     g_autofree char *uri = NULL;
14911e8a1faeSThomas Huth 
1492767fa9cfSPeter Xu     /* Always hide errors for postcopy recover tests since they're expected */
1493767fa9cfSPeter Xu     args->start.hide_stderr = true;
1494767fa9cfSPeter Xu 
1495767fa9cfSPeter Xu     if (migrate_postcopy_prepare(&from, &to, args)) {
14961e8a1faeSThomas Huth         return;
14971e8a1faeSThomas Huth     }
14981e8a1faeSThomas Huth 
14991e8a1faeSThomas Huth     /* Turn postcopy speed down, 4K/s is slow enough on any machines */
15001e8a1faeSThomas Huth     migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
15011e8a1faeSThomas Huth 
15021e8a1faeSThomas Huth     /* Now we start the postcopy */
15031e8a1faeSThomas Huth     migrate_postcopy_start(from, to);
15041e8a1faeSThomas Huth 
15051e8a1faeSThomas Huth     /*
15061e8a1faeSThomas Huth      * Wait until postcopy is really started; we can only run the
15071e8a1faeSThomas Huth      * migrate-pause command during a postcopy
15081e8a1faeSThomas Huth      */
15091e8a1faeSThomas Huth     wait_for_migration_status(from, "postcopy-active", NULL);
15101e8a1faeSThomas Huth 
15111e8a1faeSThomas Huth     /*
15121e8a1faeSThomas Huth      * Manually stop the postcopy migration. This emulates a network
15131e8a1faeSThomas Huth      * failure with the migration socket
15141e8a1faeSThomas Huth      */
15151e8a1faeSThomas Huth     migrate_pause(from);
15161e8a1faeSThomas Huth 
15171e8a1faeSThomas Huth     /*
15181e8a1faeSThomas Huth      * Wait for destination side to reach postcopy-paused state.  The
15191e8a1faeSThomas Huth      * migrate-recover command can only succeed if destination machine
15201e8a1faeSThomas Huth      * is in the paused state
15211e8a1faeSThomas Huth      */
15227bca2bb7SFabiano Rosas     wait_for_postcopy_status(to, "postcopy-paused");
15237bca2bb7SFabiano Rosas     wait_for_postcopy_status(from, "postcopy-paused");
15247bca2bb7SFabiano Rosas 
15257bca2bb7SFabiano Rosas #ifndef _WIN32
15267bca2bb7SFabiano Rosas     if (args->postcopy_recovery_test_fail) {
15277bca2bb7SFabiano Rosas         /*
15287bca2bb7SFabiano Rosas          * Test when a wrong socket specified for recover, and then the
15297bca2bb7SFabiano Rosas          * ability to kick it out, and continue with a correct socket.
15307bca2bb7SFabiano Rosas          */
15317bca2bb7SFabiano Rosas         postcopy_recover_fail(from, to);
15327bca2bb7SFabiano Rosas         /* continue with a good recovery */
15337bca2bb7SFabiano Rosas     }
15347bca2bb7SFabiano Rosas #endif /* _WIN32 */
15351e8a1faeSThomas Huth 
15361e8a1faeSThomas Huth     /*
15371e8a1faeSThomas Huth      * Create a new socket to emulate a new channel that is different
15381e8a1faeSThomas Huth      * from the broken migration channel; tell the destination to
15391e8a1faeSThomas Huth      * listen to the new port
15401e8a1faeSThomas Huth      */
15411e8a1faeSThomas Huth     uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
15421e8a1faeSThomas Huth     migrate_recover(to, uri);
15431e8a1faeSThomas Huth 
15441e8a1faeSThomas Huth     /*
15451e8a1faeSThomas Huth      * Try to rebuild the migration channel using the resume flag and
15461e8a1faeSThomas Huth      * the newly created channel
15471e8a1faeSThomas Huth      */
15481e8a1faeSThomas Huth     migrate_qmp(from, uri, "{'resume': true}");
15491e8a1faeSThomas Huth 
15501e8a1faeSThomas Huth     /* Restore the postcopy bandwidth to unlimited */
15511e8a1faeSThomas Huth     migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
15521e8a1faeSThomas Huth 
1553767fa9cfSPeter Xu     migrate_postcopy_complete(from, to, args);
15541e8a1faeSThomas Huth }
15551e8a1faeSThomas Huth 
test_postcopy_recovery(void)1556767fa9cfSPeter Xu static void test_postcopy_recovery(void)
1557767fa9cfSPeter Xu {
1558767fa9cfSPeter Xu     MigrateCommon args = { };
1559767fa9cfSPeter Xu 
1560767fa9cfSPeter Xu     test_postcopy_recovery_common(&args);
1561767fa9cfSPeter Xu }
1562767fa9cfSPeter Xu 
test_postcopy_recovery_compress(void)1563dc066da8SLukas Straub static void test_postcopy_recovery_compress(void)
1564dc066da8SLukas Straub {
1565dc066da8SLukas Straub     MigrateCommon args = {
1566dc066da8SLukas Straub         .start_hook = test_migrate_compress_start
1567dc066da8SLukas Straub     };
1568dc066da8SLukas Straub 
1569dc066da8SLukas Straub     test_postcopy_recovery_common(&args);
1570dc066da8SLukas Straub }
1571dc066da8SLukas Straub 
15727bca2bb7SFabiano Rosas #ifndef _WIN32
test_postcopy_recovery_double_fail(void)15737bca2bb7SFabiano Rosas static void test_postcopy_recovery_double_fail(void)
15747bca2bb7SFabiano Rosas {
15757bca2bb7SFabiano Rosas     MigrateCommon args = {
15767bca2bb7SFabiano Rosas         .postcopy_recovery_test_fail = true,
15777bca2bb7SFabiano Rosas     };
15787bca2bb7SFabiano Rosas 
15797bca2bb7SFabiano Rosas     test_postcopy_recovery_common(&args);
15807bca2bb7SFabiano Rosas }
15817bca2bb7SFabiano Rosas #endif /* _WIN32 */
15827bca2bb7SFabiano Rosas 
1583767fa9cfSPeter Xu #ifdef CONFIG_GNUTLS
test_postcopy_recovery_tls_psk(void)1584767fa9cfSPeter Xu static void test_postcopy_recovery_tls_psk(void)
1585767fa9cfSPeter Xu {
1586767fa9cfSPeter Xu     MigrateCommon args = {
1587767fa9cfSPeter Xu         .start_hook = test_migrate_tls_psk_start_match,
1588767fa9cfSPeter Xu         .finish_hook = test_migrate_tls_psk_finish,
1589767fa9cfSPeter Xu     };
1590767fa9cfSPeter Xu 
1591767fa9cfSPeter Xu     test_postcopy_recovery_common(&args);
1592767fa9cfSPeter Xu }
1593767fa9cfSPeter Xu #endif
1594767fa9cfSPeter Xu 
test_postcopy_preempt_recovery(void)15958f6fe915SPeter Xu static void test_postcopy_preempt_recovery(void)
15968f6fe915SPeter Xu {
15978f6fe915SPeter Xu     MigrateCommon args = {
15988f6fe915SPeter Xu         .postcopy_preempt = true,
15998f6fe915SPeter Xu     };
16008f6fe915SPeter Xu 
16018f6fe915SPeter Xu     test_postcopy_recovery_common(&args);
16028f6fe915SPeter Xu }
16038f6fe915SPeter Xu 
16048f6fe915SPeter Xu #ifdef CONFIG_GNUTLS
16058f6fe915SPeter Xu /* This contains preempt+recovery+tls test altogether */
test_postcopy_preempt_all(void)16068f6fe915SPeter Xu static void test_postcopy_preempt_all(void)
16078f6fe915SPeter Xu {
16088f6fe915SPeter Xu     MigrateCommon args = {
16098f6fe915SPeter Xu         .postcopy_preempt = true,
16108f6fe915SPeter Xu         .start_hook = test_migrate_tls_psk_start_match,
16118f6fe915SPeter Xu         .finish_hook = test_migrate_tls_psk_finish,
16128f6fe915SPeter Xu     };
16138f6fe915SPeter Xu 
16148f6fe915SPeter Xu     test_postcopy_recovery_common(&args);
16158f6fe915SPeter Xu }
1616dc066da8SLukas Straub 
16178f6fe915SPeter Xu #endif
16188f6fe915SPeter Xu 
test_baddest(void)16191e8a1faeSThomas Huth static void test_baddest(void)
16201e8a1faeSThomas Huth {
162119da6edfSDaniel P. Berrangé     MigrateStart args = {
162219da6edfSDaniel P. Berrangé         .hide_stderr = true
162319da6edfSDaniel P. Berrangé     };
16241e8a1faeSThomas Huth     QTestState *from, *to;
16251e8a1faeSThomas Huth 
16263ff57401SPeter Xu     if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", &args)) {
16271e8a1faeSThomas Huth         return;
16281e8a1faeSThomas Huth     }
16295e32ffd3SDr. David Alan Gilbert     migrate_qmp(from, "tcp:127.0.0.1:0", "{}");
16301e8a1faeSThomas Huth     wait_for_migration_fail(from, false);
16311e8a1faeSThomas Huth     test_migrate_end(from, to, false);
16321e8a1faeSThomas Huth }
16331e8a1faeSThomas Huth 
1634d864756eSFabiano Rosas #ifndef _WIN32
test_analyze_script(void)1635d864756eSFabiano Rosas static void test_analyze_script(void)
1636d864756eSFabiano Rosas {
1637d864756eSFabiano Rosas     MigrateStart args = {
1638d864756eSFabiano Rosas         .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
1639d864756eSFabiano Rosas     };
1640d864756eSFabiano Rosas     QTestState *from, *to;
1641d864756eSFabiano Rosas     g_autofree char *uri = NULL;
1642d864756eSFabiano Rosas     g_autofree char *file = NULL;
1643d864756eSFabiano Rosas     int pid, wstatus;
1644d864756eSFabiano Rosas     const char *python = g_getenv("PYTHON");
1645d864756eSFabiano Rosas 
1646d864756eSFabiano Rosas     if (!python) {
1647d864756eSFabiano Rosas         g_test_skip("PYTHON variable not set");
1648d864756eSFabiano Rosas         return;
1649d864756eSFabiano Rosas     }
1650d864756eSFabiano Rosas 
1651d864756eSFabiano Rosas     /* dummy url */
1652d864756eSFabiano Rosas     if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", &args)) {
1653d864756eSFabiano Rosas         return;
1654d864756eSFabiano Rosas     }
1655d864756eSFabiano Rosas 
1656d864756eSFabiano Rosas     /*
1657d864756eSFabiano Rosas      * Setting these two capabilities causes the "configuration"
1658d864756eSFabiano Rosas      * vmstate to include subsections for them. The script needs to
1659d864756eSFabiano Rosas      * parse those subsections properly.
1660d864756eSFabiano Rosas      */
1661d864756eSFabiano Rosas     migrate_set_capability(from, "validate-uuid", true);
1662d864756eSFabiano Rosas     migrate_set_capability(from, "x-ignore-shared", true);
1663d864756eSFabiano Rosas 
1664d864756eSFabiano Rosas     file = g_strdup_printf("%s/migfile", tmpfs);
1665d864756eSFabiano Rosas     uri = g_strdup_printf("exec:cat > %s", file);
1666d864756eSFabiano Rosas 
1667d864756eSFabiano Rosas     migrate_ensure_converge(from);
1668d864756eSFabiano Rosas     migrate_qmp(from, uri, "{}");
1669d864756eSFabiano Rosas     wait_for_migration_complete(from);
1670d864756eSFabiano Rosas 
1671d864756eSFabiano Rosas     pid = fork();
1672d864756eSFabiano Rosas     if (!pid) {
1673d864756eSFabiano Rosas         close(1);
1674d864756eSFabiano Rosas         open("/dev/null", O_WRONLY);
1675d864756eSFabiano Rosas         execl(python, python, ANALYZE_SCRIPT, "-f", file, NULL);
1676d864756eSFabiano Rosas         g_assert_not_reached();
1677d864756eSFabiano Rosas     }
1678d864756eSFabiano Rosas 
1679d864756eSFabiano Rosas     g_assert(waitpid(pid, &wstatus, 0) == pid);
1680d864756eSFabiano Rosas     if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus) != 0) {
1681d864756eSFabiano Rosas         g_test_message("Failed to analyze the migration stream");
1682d864756eSFabiano Rosas         g_test_fail();
1683d864756eSFabiano Rosas     }
1684d864756eSFabiano Rosas     test_migrate_end(from, to, false);
1685d864756eSFabiano Rosas     cleanup("migfile");
1686d864756eSFabiano Rosas }
1687d864756eSFabiano Rosas #endif
1688d864756eSFabiano Rosas 
test_precopy_common(MigrateCommon * args)1689ffed54f6SDaniel P. Berrangé static void test_precopy_common(MigrateCommon *args)
16901e8a1faeSThomas Huth {
16911e8a1faeSThomas Huth     QTestState *from, *to;
1692b3caa7b5SDaniel P. Berrangé     void *data_hook = NULL;
16935274274cSFabiano Rosas     g_autofree char *connect_uri = NULL;
16941e8a1faeSThomas Huth 
1695ffed54f6SDaniel P. Berrangé     if (test_migrate_start(&from, &to, args->listen_uri, &args->start)) {
16961e8a1faeSThomas Huth         return;
16971e8a1faeSThomas Huth     }
16981e8a1faeSThomas Huth 
1699b3caa7b5SDaniel P. Berrangé     if (args->start_hook) {
1700b3caa7b5SDaniel P. Berrangé         data_hook = args->start_hook(from, to);
1701b3caa7b5SDaniel P. Berrangé     }
1702b3caa7b5SDaniel P. Berrangé 
17031e8a1faeSThomas Huth     /* Wait for the first serial output from the source */
1704e25636a1SThomas Huth     if (args->result == MIG_TEST_SUCCEED) {
17051e8a1faeSThomas Huth         wait_for_serial("src_serial");
1706e25636a1SThomas Huth     }
17071e8a1faeSThomas Huth 
17083c4fb177SDaniel P. Berrangé     if (args->live) {
17093c4fb177SDaniel P. Berrangé         migrate_ensure_non_converge(from);
1710e02f56e3SDaniel P. Berrangé         migrate_prepare_for_dirty_mem(from);
17113c4fb177SDaniel P. Berrangé     } else {
17123c4fb177SDaniel P. Berrangé         /*
17133c4fb177SDaniel P. Berrangé          * Testing non-live migration, we allow it to run at
17143c4fb177SDaniel P. Berrangé          * full speed to ensure short test case duration.
17153c4fb177SDaniel P. Berrangé          * For tests expected to fail, we don't need to
17163c4fb177SDaniel P. Berrangé          * change anything.
17173c4fb177SDaniel P. Berrangé          */
17183c4fb177SDaniel P. Berrangé         if (args->result == MIG_TEST_SUCCEED) {
17193c4fb177SDaniel P. Berrangé             qtest_qmp_assert_success(from, "{ 'execute' : 'stop'}");
17203c4fb177SDaniel P. Berrangé             if (!got_src_stop) {
17213c4fb177SDaniel P. Berrangé                 qtest_qmp_eventwait(from, "STOP");
17223c4fb177SDaniel P. Berrangé             }
17233c4fb177SDaniel P. Berrangé             migrate_ensure_converge(from);
17243c4fb177SDaniel P. Berrangé         }
17253c4fb177SDaniel P. Berrangé     }
17263c4fb177SDaniel P. Berrangé 
1727ffed54f6SDaniel P. Berrangé     if (!args->connect_uri) {
17285274274cSFabiano Rosas         connect_uri = migrate_get_socket_address(to, "socket-address");
1729ffed54f6SDaniel P. Berrangé     } else {
17305274274cSFabiano Rosas         connect_uri = g_strdup(args->connect_uri);
1731ffed54f6SDaniel P. Berrangé     }
1732ffed54f6SDaniel P. Berrangé 
17335274274cSFabiano Rosas     if (args->result == MIG_TEST_QMP_ERROR) {
17345274274cSFabiano Rosas         migrate_qmp_fail(from, connect_uri, "{}");
17355274274cSFabiano Rosas         goto finish;
17365274274cSFabiano Rosas     }
17375274274cSFabiano Rosas 
17385274274cSFabiano Rosas     migrate_qmp(from, connect_uri, "{}");
17391e8a1faeSThomas Huth 
174000fbe7f6SDaniel P. Berrangé     if (args->result != MIG_TEST_SUCCEED) {
174100fbe7f6SDaniel P. Berrangé         bool allow_active = args->result == MIG_TEST_FAIL;
174200fbe7f6SDaniel P. Berrangé         wait_for_migration_fail(from, allow_active);
174300fbe7f6SDaniel P. Berrangé 
174400fbe7f6SDaniel P. Berrangé         if (args->result == MIG_TEST_FAIL_DEST_QUIT_ERR) {
17451b0f1b14SBin Meng             qtest_set_expected_status(to, EXIT_FAILURE);
174600fbe7f6SDaniel P. Berrangé         }
174700fbe7f6SDaniel P. Berrangé     } else {
17483c4fb177SDaniel P. Berrangé         if (args->live) {
1749e02f56e3SDaniel P. Berrangé             /*
1750e02f56e3SDaniel P. Berrangé              * For initial iteration(s) we must do a full pass,
1751e02f56e3SDaniel P. Berrangé              * but for the final iteration, we need only wait
1752e02f56e3SDaniel P. Berrangé              * for some dirty mem before switching to converge
1753e02f56e3SDaniel P. Berrangé              */
1754e02f56e3SDaniel P. Berrangé             while (args->iterations > 1) {
17551e8a1faeSThomas Huth                 wait_for_migration_pass(from);
1756e02f56e3SDaniel P. Berrangé                 args->iterations--;
175783bcba1eSDaniel P. Berrangé             }
1758e02f56e3SDaniel P. Berrangé             migrate_wait_for_dirty_mem(from, to);
17591e8a1faeSThomas Huth 
1760886dfe9dSDaniel P. Berrangé             migrate_ensure_converge(from);
17611e8a1faeSThomas Huth 
17623c4fb177SDaniel P. Berrangé             /*
17633c4fb177SDaniel P. Berrangé              * We do this first, as it has a timeout to stop us
17643c4fb177SDaniel P. Berrangé              * hanging forever if migration didn't converge
17653c4fb177SDaniel P. Berrangé              */
17668d4e897aSDaniel P. Berrangé             wait_for_migration_complete(from);
17678d4e897aSDaniel P. Berrangé 
176895014994SDaniel P. Berrangé             if (!got_src_stop) {
17691e8a1faeSThomas Huth                 qtest_qmp_eventwait(from, "STOP");
17701e8a1faeSThomas Huth             }
17713c4fb177SDaniel P. Berrangé         } else {
17723c4fb177SDaniel P. Berrangé             wait_for_migration_complete(from);
17733c4fb177SDaniel P. Berrangé             /*
17743c4fb177SDaniel P. Berrangé              * Must wait for dst to finish reading all incoming
17753c4fb177SDaniel P. Berrangé              * data on the socket before issuing 'cont' otherwise
17763c4fb177SDaniel P. Berrangé              * it'll be ignored
17773c4fb177SDaniel P. Berrangé              */
17783c4fb177SDaniel P. Berrangé             wait_for_migration_complete(to);
17791e8a1faeSThomas Huth 
17803c4fb177SDaniel P. Berrangé             qtest_qmp_assert_success(to, "{ 'execute' : 'cont'}");
17813c4fb177SDaniel P. Berrangé         }
17823c4fb177SDaniel P. Berrangé 
17833c4fb177SDaniel P. Berrangé         if (!got_dst_resume) {
17841e8a1faeSThomas Huth             qtest_qmp_eventwait(to, "RESUME");
17853c4fb177SDaniel P. Berrangé         }
17861e8a1faeSThomas Huth 
17871e8a1faeSThomas Huth         wait_for_serial("dest_serial");
178800fbe7f6SDaniel P. Berrangé     }
17891e8a1faeSThomas Huth 
17905274274cSFabiano Rosas finish:
1791b3caa7b5SDaniel P. Berrangé     if (args->finish_hook) {
1792b3caa7b5SDaniel P. Berrangé         args->finish_hook(from, to, data_hook);
1793b3caa7b5SDaniel P. Berrangé     }
1794b3caa7b5SDaniel P. Berrangé 
179500fbe7f6SDaniel P. Berrangé     test_migrate_end(from, to, args->result == MIG_TEST_SUCCEED);
17961e8a1faeSThomas Huth }
17971e8a1faeSThomas Huth 
test_file_common(MigrateCommon * args,bool stop_src)17983dc35470SFabiano Rosas static void test_file_common(MigrateCommon *args, bool stop_src)
17993dc35470SFabiano Rosas {
18003dc35470SFabiano Rosas     QTestState *from, *to;
18013dc35470SFabiano Rosas     void *data_hook = NULL;
18023dc35470SFabiano Rosas     g_autofree char *connect_uri = g_strdup(args->connect_uri);
18033dc35470SFabiano Rosas 
18043dc35470SFabiano Rosas     if (test_migrate_start(&from, &to, args->listen_uri, &args->start)) {
18053dc35470SFabiano Rosas         return;
18063dc35470SFabiano Rosas     }
18073dc35470SFabiano Rosas 
18083dc35470SFabiano Rosas     /*
18093dc35470SFabiano Rosas      * File migration is never live. We can keep the source VM running
18103dc35470SFabiano Rosas      * during migration, but the destination will not be running
18113dc35470SFabiano Rosas      * concurrently.
18123dc35470SFabiano Rosas      */
18133dc35470SFabiano Rosas     g_assert_false(args->live);
18143dc35470SFabiano Rosas 
18153dc35470SFabiano Rosas     if (args->start_hook) {
18163dc35470SFabiano Rosas         data_hook = args->start_hook(from, to);
18173dc35470SFabiano Rosas     }
18183dc35470SFabiano Rosas 
18193dc35470SFabiano Rosas     migrate_ensure_converge(from);
18203dc35470SFabiano Rosas     wait_for_serial("src_serial");
18213dc35470SFabiano Rosas 
18223dc35470SFabiano Rosas     if (stop_src) {
18233dc35470SFabiano Rosas         qtest_qmp_assert_success(from, "{ 'execute' : 'stop'}");
18243dc35470SFabiano Rosas         if (!got_src_stop) {
18253dc35470SFabiano Rosas             qtest_qmp_eventwait(from, "STOP");
18263dc35470SFabiano Rosas         }
18273dc35470SFabiano Rosas     }
18283dc35470SFabiano Rosas 
18293dc35470SFabiano Rosas     if (args->result == MIG_TEST_QMP_ERROR) {
18303dc35470SFabiano Rosas         migrate_qmp_fail(from, connect_uri, "{}");
18313dc35470SFabiano Rosas         goto finish;
18323dc35470SFabiano Rosas     }
18333dc35470SFabiano Rosas 
18343dc35470SFabiano Rosas     migrate_qmp(from, connect_uri, "{}");
18353dc35470SFabiano Rosas     wait_for_migration_complete(from);
18363dc35470SFabiano Rosas 
18373dc35470SFabiano Rosas     /*
18383dc35470SFabiano Rosas      * We need to wait for the source to finish before starting the
18393dc35470SFabiano Rosas      * destination.
18403dc35470SFabiano Rosas      */
18413dc35470SFabiano Rosas     migrate_incoming_qmp(to, connect_uri, "{}");
18423dc35470SFabiano Rosas     wait_for_migration_complete(to);
18433dc35470SFabiano Rosas 
18443dc35470SFabiano Rosas     if (stop_src) {
18453dc35470SFabiano Rosas         qtest_qmp_assert_success(to, "{ 'execute' : 'cont'}");
18463dc35470SFabiano Rosas     }
18473dc35470SFabiano Rosas 
18483dc35470SFabiano Rosas     if (!got_dst_resume) {
18493dc35470SFabiano Rosas         qtest_qmp_eventwait(to, "RESUME");
18503dc35470SFabiano Rosas     }
18513dc35470SFabiano Rosas 
18523dc35470SFabiano Rosas     wait_for_serial("dest_serial");
18533dc35470SFabiano Rosas 
18543dc35470SFabiano Rosas finish:
18553dc35470SFabiano Rosas     if (args->finish_hook) {
18563dc35470SFabiano Rosas         args->finish_hook(from, to, data_hook);
18573dc35470SFabiano Rosas     }
18583dc35470SFabiano Rosas 
18593dc35470SFabiano Rosas     test_migrate_end(from, to, args->result == MIG_TEST_SUCCEED);
18603dc35470SFabiano Rosas }
18613dc35470SFabiano Rosas 
test_precopy_unix_plain(void)186258d25e97SDaniel P. Berrangé static void test_precopy_unix_plain(void)
18631f546b70SPeter Xu {
1864ffed54f6SDaniel P. Berrangé     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1865ffed54f6SDaniel P. Berrangé     MigrateCommon args = {
1866ffed54f6SDaniel P. Berrangé         .listen_uri = uri,
1867ffed54f6SDaniel P. Berrangé         .connect_uri = uri,
1868b861383cSPeter Xu         /*
1869b861383cSPeter Xu          * The simplest use case of precopy, covering smoke tests of
1870b861383cSPeter Xu          * get-dirty-log dirty tracking.
1871b861383cSPeter Xu          */
18723c4fb177SDaniel P. Berrangé         .live = true,
1873ffed54f6SDaniel P. Berrangé     };
1874ffed54f6SDaniel P. Berrangé 
1875ffed54f6SDaniel P. Berrangé     test_precopy_common(&args);
18761f546b70SPeter Xu }
18771f546b70SPeter Xu 
187858d25e97SDaniel P. Berrangé 
test_precopy_unix_dirty_ring(void)18791f546b70SPeter Xu static void test_precopy_unix_dirty_ring(void)
18801f546b70SPeter Xu {
1881ffed54f6SDaniel P. Berrangé     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1882ffed54f6SDaniel P. Berrangé     MigrateCommon args = {
1883ffed54f6SDaniel P. Berrangé         .start = {
1884ffed54f6SDaniel P. Berrangé             .use_dirty_ring = true,
1885ffed54f6SDaniel P. Berrangé         },
1886ffed54f6SDaniel P. Berrangé         .listen_uri = uri,
1887ffed54f6SDaniel P. Berrangé         .connect_uri = uri,
1888b861383cSPeter Xu         /*
1889b861383cSPeter Xu          * Besides the precopy/unix basic test, cover dirty ring interface
1890b861383cSPeter Xu          * rather than get-dirty-log.
1891b861383cSPeter Xu          */
18923c4fb177SDaniel P. Berrangé         .live = true,
1893ffed54f6SDaniel P. Berrangé     };
1894ffed54f6SDaniel P. Berrangé 
1895ffed54f6SDaniel P. Berrangé     test_precopy_common(&args);
18961f546b70SPeter Xu }
18971f546b70SPeter Xu 
1898d47b83b1SDaniel P. Berrangé #ifdef CONFIG_GNUTLS
test_precopy_unix_tls_psk(void)1899d47b83b1SDaniel P. Berrangé static void test_precopy_unix_tls_psk(void)
1900d47b83b1SDaniel P. Berrangé {
1901d47b83b1SDaniel P. Berrangé     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1902d47b83b1SDaniel P. Berrangé     MigrateCommon args = {
1903d47b83b1SDaniel P. Berrangé         .connect_uri = uri,
1904d47b83b1SDaniel P. Berrangé         .listen_uri = uri,
1905d47b83b1SDaniel P. Berrangé         .start_hook = test_migrate_tls_psk_start_match,
1906d47b83b1SDaniel P. Berrangé         .finish_hook = test_migrate_tls_psk_finish,
1907d47b83b1SDaniel P. Berrangé     };
1908d47b83b1SDaniel P. Berrangé 
1909d47b83b1SDaniel P. Berrangé     test_precopy_common(&args);
1910d47b83b1SDaniel P. Berrangé }
1911d47b83b1SDaniel P. Berrangé 
1912d47b83b1SDaniel P. Berrangé #ifdef CONFIG_TASN1
test_precopy_unix_tls_x509_default_host(void)1913d47b83b1SDaniel P. Berrangé static void test_precopy_unix_tls_x509_default_host(void)
1914d47b83b1SDaniel P. Berrangé {
1915d47b83b1SDaniel P. Berrangé     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1916d47b83b1SDaniel P. Berrangé     MigrateCommon args = {
1917d47b83b1SDaniel P. Berrangé         .start = {
1918d47b83b1SDaniel P. Berrangé             .hide_stderr = true,
1919d47b83b1SDaniel P. Berrangé         },
1920d47b83b1SDaniel P. Berrangé         .connect_uri = uri,
1921d47b83b1SDaniel P. Berrangé         .listen_uri = uri,
1922d47b83b1SDaniel P. Berrangé         .start_hook = test_migrate_tls_x509_start_default_host,
1923d47b83b1SDaniel P. Berrangé         .finish_hook = test_migrate_tls_x509_finish,
1924d47b83b1SDaniel P. Berrangé         .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
1925d47b83b1SDaniel P. Berrangé     };
1926d47b83b1SDaniel P. Berrangé 
1927d47b83b1SDaniel P. Berrangé     test_precopy_common(&args);
1928d47b83b1SDaniel P. Berrangé }
1929d47b83b1SDaniel P. Berrangé 
test_precopy_unix_tls_x509_override_host(void)1930d47b83b1SDaniel P. Berrangé static void test_precopy_unix_tls_x509_override_host(void)
1931d47b83b1SDaniel P. Berrangé {
1932d47b83b1SDaniel P. Berrangé     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1933d47b83b1SDaniel P. Berrangé     MigrateCommon args = {
1934d47b83b1SDaniel P. Berrangé         .connect_uri = uri,
1935d47b83b1SDaniel P. Berrangé         .listen_uri = uri,
1936d47b83b1SDaniel P. Berrangé         .start_hook = test_migrate_tls_x509_start_override_host,
1937d47b83b1SDaniel P. Berrangé         .finish_hook = test_migrate_tls_x509_finish,
1938d47b83b1SDaniel P. Berrangé     };
1939d47b83b1SDaniel P. Berrangé 
1940d47b83b1SDaniel P. Berrangé     test_precopy_common(&args);
1941d47b83b1SDaniel P. Berrangé }
1942d47b83b1SDaniel P. Berrangé #endif /* CONFIG_TASN1 */
1943d47b83b1SDaniel P. Berrangé #endif /* CONFIG_GNUTLS */
1944d47b83b1SDaniel P. Berrangé 
19451e8a1faeSThomas Huth #if 0
19461e8a1faeSThomas Huth /* Currently upset on aarch64 TCG */
19471e8a1faeSThomas Huth static void test_ignore_shared(void)
19481e8a1faeSThomas Huth {
1949ff7b9b56SPeter Maydell     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
19501e8a1faeSThomas Huth     QTestState *from, *to;
19511e8a1faeSThomas Huth 
19521e8a1faeSThomas Huth     if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) {
19531e8a1faeSThomas Huth         return;
19541e8a1faeSThomas Huth     }
19551e8a1faeSThomas Huth 
1956e02f56e3SDaniel P. Berrangé     migrate_ensure_non_converge(from);
1957e02f56e3SDaniel P. Berrangé     migrate_prepare_for_dirty_mem(from);
1958e02f56e3SDaniel P. Berrangé 
19591e8a1faeSThomas Huth     migrate_set_capability(from, "x-ignore-shared", true);
19601e8a1faeSThomas Huth     migrate_set_capability(to, "x-ignore-shared", true);
19611e8a1faeSThomas Huth 
19621e8a1faeSThomas Huth     /* Wait for the first serial output from the source */
19631e8a1faeSThomas Huth     wait_for_serial("src_serial");
19641e8a1faeSThomas Huth 
19651e8a1faeSThomas Huth     migrate_qmp(from, uri, "{}");
19661e8a1faeSThomas Huth 
1967e02f56e3SDaniel P. Berrangé     migrate_wait_for_dirty_mem(from, to);
19681e8a1faeSThomas Huth 
196995014994SDaniel P. Berrangé     if (!got_src_stop) {
19701e8a1faeSThomas Huth         qtest_qmp_eventwait(from, "STOP");
19711e8a1faeSThomas Huth     }
19721e8a1faeSThomas Huth 
19731e8a1faeSThomas Huth     qtest_qmp_eventwait(to, "RESUME");
19741e8a1faeSThomas Huth 
19751e8a1faeSThomas Huth     wait_for_serial("dest_serial");
19761e8a1faeSThomas Huth     wait_for_migration_complete(from);
19771e8a1faeSThomas Huth 
19781e8a1faeSThomas Huth     /* Check whether shared RAM has been really skipped */
19791e8a1faeSThomas Huth     g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
19801e8a1faeSThomas Huth 
19811e8a1faeSThomas Huth     test_migrate_end(from, to, true);
19821e8a1faeSThomas Huth }
19831e8a1faeSThomas Huth #endif
19841e8a1faeSThomas Huth 
198583bcba1eSDaniel P. Berrangé static void *
test_migrate_xbzrle_start(QTestState * from,QTestState * to)198683bcba1eSDaniel P. Berrangé test_migrate_xbzrle_start(QTestState *from,
198783bcba1eSDaniel P. Berrangé                           QTestState *to)
19881e8a1faeSThomas Huth {
19891e8a1faeSThomas Huth     migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
19901e8a1faeSThomas Huth 
1991a1209bb7SDr. David Alan Gilbert     migrate_set_capability(from, "xbzrle", true);
1992a1209bb7SDr. David Alan Gilbert     migrate_set_capability(to, "xbzrle", true);
19931e8a1faeSThomas Huth 
199483bcba1eSDaniel P. Berrangé     return NULL;
19951e8a1faeSThomas Huth }
19961e8a1faeSThomas Huth 
test_precopy_unix_xbzrle(void)199783bcba1eSDaniel P. Berrangé static void test_precopy_unix_xbzrle(void)
19981e8a1faeSThomas Huth {
1999ff7b9b56SPeter Maydell     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
200083bcba1eSDaniel P. Berrangé     MigrateCommon args = {
200183bcba1eSDaniel P. Berrangé         .connect_uri = uri,
200283bcba1eSDaniel P. Berrangé         .listen_uri = uri,
200383bcba1eSDaniel P. Berrangé         .start_hook = test_migrate_xbzrle_start,
200483bcba1eSDaniel P. Berrangé         .iterations = 2,
2005b861383cSPeter Xu         /*
2006b861383cSPeter Xu          * XBZRLE needs pages to be modified when doing the 2nd+ round
2007b861383cSPeter Xu          * iteration to have real data pushed to the stream.
2008b861383cSPeter Xu          */
20093c4fb177SDaniel P. Berrangé         .live = true,
201083bcba1eSDaniel P. Berrangé     };
201183bcba1eSDaniel P. Berrangé 
201283bcba1eSDaniel P. Berrangé     test_precopy_common(&args);
20131e8a1faeSThomas Huth }
20141e8a1faeSThomas Huth 
test_precopy_unix_compress(void)20151536d1daSLukas Straub static void test_precopy_unix_compress(void)
20161536d1daSLukas Straub {
20171536d1daSLukas Straub     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
20181536d1daSLukas Straub     MigrateCommon args = {
20191536d1daSLukas Straub         .connect_uri = uri,
20201536d1daSLukas Straub         .listen_uri = uri,
20211536d1daSLukas Straub         .start_hook = test_migrate_compress_start,
20221536d1daSLukas Straub         /*
20231536d1daSLukas Straub          * Test that no invalid thread state is left over from
20241536d1daSLukas Straub          * the previous iteration.
20251536d1daSLukas Straub          */
20261536d1daSLukas Straub         .iterations = 2,
2027b861383cSPeter Xu         /*
2028b861383cSPeter Xu          * We make sure the compressor can always work well even if guest
2029b861383cSPeter Xu          * memory is changing.  See commit 34ab9e9743 where we used to fix
2030b861383cSPeter Xu          * a bug when only trigger-able with guest memory changing.
2031b861383cSPeter Xu          */
20323c4fb177SDaniel P. Berrangé         .live = true,
20331536d1daSLukas Straub     };
20341536d1daSLukas Straub 
20351536d1daSLukas Straub     test_precopy_common(&args);
20361536d1daSLukas Straub }
20371536d1daSLukas Straub 
test_precopy_unix_compress_nowait(void)20381536d1daSLukas Straub static void test_precopy_unix_compress_nowait(void)
20391536d1daSLukas Straub {
20401536d1daSLukas Straub     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
20411536d1daSLukas Straub     MigrateCommon args = {
20421536d1daSLukas Straub         .connect_uri = uri,
20431536d1daSLukas Straub         .listen_uri = uri,
20441536d1daSLukas Straub         .start_hook = test_migrate_compress_nowait_start,
20451536d1daSLukas Straub         /*
20461536d1daSLukas Straub          * Test that no invalid thread state is left over from
20471536d1daSLukas Straub          * the previous iteration.
20481536d1daSLukas Straub          */
20491536d1daSLukas Straub         .iterations = 2,
2050b861383cSPeter Xu         /* Same reason for the wait version of precopy compress test */
20513c4fb177SDaniel P. Berrangé         .live = true,
20521536d1daSLukas Straub     };
20531536d1daSLukas Straub 
20541536d1daSLukas Straub     test_precopy_common(&args);
20551536d1daSLukas Straub }
20561536d1daSLukas Straub 
test_precopy_file(void)20573dc35470SFabiano Rosas static void test_precopy_file(void)
20583dc35470SFabiano Rosas {
20593dc35470SFabiano Rosas     g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
20603dc35470SFabiano Rosas                                            FILE_TEST_FILENAME);
20613dc35470SFabiano Rosas     MigrateCommon args = {
20623dc35470SFabiano Rosas         .connect_uri = uri,
20633dc35470SFabiano Rosas         .listen_uri = "defer",
20643dc35470SFabiano Rosas     };
20653dc35470SFabiano Rosas 
20663dc35470SFabiano Rosas     test_file_common(&args, true);
20673dc35470SFabiano Rosas }
20683dc35470SFabiano Rosas 
file_offset_finish_hook(QTestState * from,QTestState * to,void * opaque)20693dc35470SFabiano Rosas static void file_offset_finish_hook(QTestState *from, QTestState *to,
20703dc35470SFabiano Rosas                                     void *opaque)
20713dc35470SFabiano Rosas {
20723dc35470SFabiano Rosas #if defined(__linux__)
20733dc35470SFabiano Rosas     g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, FILE_TEST_FILENAME);
20743dc35470SFabiano Rosas     size_t size = FILE_TEST_OFFSET + sizeof(QEMU_VM_FILE_MAGIC);
20753dc35470SFabiano Rosas     uintptr_t *addr, *p;
20763dc35470SFabiano Rosas     int fd;
20773dc35470SFabiano Rosas 
20783dc35470SFabiano Rosas     fd = open(path, O_RDONLY);
20793dc35470SFabiano Rosas     g_assert(fd != -1);
20803dc35470SFabiano Rosas     addr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
20813dc35470SFabiano Rosas     g_assert(addr != MAP_FAILED);
20823dc35470SFabiano Rosas 
20833dc35470SFabiano Rosas     /*
20843dc35470SFabiano Rosas      * Ensure the skipped offset contains zeros and the migration
20853dc35470SFabiano Rosas      * stream starts at the right place.
20863dc35470SFabiano Rosas      */
20873dc35470SFabiano Rosas     p = addr;
20883dc35470SFabiano Rosas     while (p < addr + FILE_TEST_OFFSET / sizeof(uintptr_t)) {
20893dc35470SFabiano Rosas         g_assert(*p == 0);
20903dc35470SFabiano Rosas         p++;
20913dc35470SFabiano Rosas     }
20923dc35470SFabiano Rosas     g_assert_cmpint(cpu_to_be64(*p) >> 32, ==, QEMU_VM_FILE_MAGIC);
20933dc35470SFabiano Rosas 
20943dc35470SFabiano Rosas     munmap(addr, size);
20953dc35470SFabiano Rosas     close(fd);
20963dc35470SFabiano Rosas #endif
20973dc35470SFabiano Rosas }
20983dc35470SFabiano Rosas 
test_precopy_file_offset(void)20993dc35470SFabiano Rosas static void test_precopy_file_offset(void)
21003dc35470SFabiano Rosas {
21013dc35470SFabiano Rosas     g_autofree char *uri = g_strdup_printf("file:%s/%s,offset=%d", tmpfs,
21023dc35470SFabiano Rosas                                            FILE_TEST_FILENAME,
21033dc35470SFabiano Rosas                                            FILE_TEST_OFFSET);
21043dc35470SFabiano Rosas     MigrateCommon args = {
21053dc35470SFabiano Rosas         .connect_uri = uri,
21063dc35470SFabiano Rosas         .listen_uri = "defer",
21073dc35470SFabiano Rosas         .finish_hook = file_offset_finish_hook,
21083dc35470SFabiano Rosas     };
21093dc35470SFabiano Rosas 
21103dc35470SFabiano Rosas     test_file_common(&args, false);
21113dc35470SFabiano Rosas }
21123dc35470SFabiano Rosas 
test_precopy_file_offset_bad(void)21133dc35470SFabiano Rosas static void test_precopy_file_offset_bad(void)
21143dc35470SFabiano Rosas {
21153dc35470SFabiano Rosas     /* using a value not supported by qemu_strtosz() */
21163dc35470SFabiano Rosas     g_autofree char *uri = g_strdup_printf("file:%s/%s,offset=0x20M",
21173dc35470SFabiano Rosas                                            tmpfs, FILE_TEST_FILENAME);
21183dc35470SFabiano Rosas     MigrateCommon args = {
21193dc35470SFabiano Rosas         .connect_uri = uri,
21203dc35470SFabiano Rosas         .listen_uri = "defer",
21213dc35470SFabiano Rosas         .result = MIG_TEST_QMP_ERROR,
21223dc35470SFabiano Rosas     };
21233dc35470SFabiano Rosas 
21243dc35470SFabiano Rosas     test_file_common(&args, false);
21253dc35470SFabiano Rosas }
21263dc35470SFabiano Rosas 
test_mode_reboot_start(QTestState * from,QTestState * to)2127e7b428d6SSteve Sistare static void *test_mode_reboot_start(QTestState *from, QTestState *to)
2128e7b428d6SSteve Sistare {
2129e7b428d6SSteve Sistare     migrate_set_parameter_str(from, "mode", "cpr-reboot");
2130e7b428d6SSteve Sistare     migrate_set_parameter_str(to, "mode", "cpr-reboot");
2131e7b428d6SSteve Sistare 
2132e7b428d6SSteve Sistare     migrate_set_capability(from, "x-ignore-shared", true);
2133e7b428d6SSteve Sistare     migrate_set_capability(to, "x-ignore-shared", true);
2134e7b428d6SSteve Sistare 
2135e7b428d6SSteve Sistare     return NULL;
2136e7b428d6SSteve Sistare }
2137e7b428d6SSteve Sistare 
test_mode_reboot(void)2138e7b428d6SSteve Sistare static void test_mode_reboot(void)
2139e7b428d6SSteve Sistare {
2140e7b428d6SSteve Sistare     g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
2141e7b428d6SSteve Sistare                                            FILE_TEST_FILENAME);
2142e7b428d6SSteve Sistare     MigrateCommon args = {
2143e7b428d6SSteve Sistare         .start.use_shmem = true,
2144e7b428d6SSteve Sistare         .connect_uri = uri,
2145e7b428d6SSteve Sistare         .listen_uri = "defer",
2146e7b428d6SSteve Sistare         .start_hook = test_mode_reboot_start
2147e7b428d6SSteve Sistare     };
2148e7b428d6SSteve Sistare 
2149e7b428d6SSteve Sistare     test_file_common(&args, true);
2150e7b428d6SSteve Sistare }
2151e7b428d6SSteve Sistare 
test_precopy_tcp_plain(void)215258d25e97SDaniel P. Berrangé static void test_precopy_tcp_plain(void)
21531e8a1faeSThomas Huth {
2154ffed54f6SDaniel P. Berrangé     MigrateCommon args = {
2155ffed54f6SDaniel P. Berrangé         .listen_uri = "tcp:127.0.0.1:0",
2156ffed54f6SDaniel P. Berrangé     };
21571e8a1faeSThomas Huth 
2158ffed54f6SDaniel P. Berrangé     test_precopy_common(&args);
21591e8a1faeSThomas Huth }
21601e8a1faeSThomas Huth 
test_migrate_switchover_ack_start(QTestState * from,QTestState * to)21617e6a5c73SAvihai Horon static void *test_migrate_switchover_ack_start(QTestState *from, QTestState *to)
21627e6a5c73SAvihai Horon {
21637e6a5c73SAvihai Horon 
21647e6a5c73SAvihai Horon     migrate_set_capability(from, "return-path", true);
21657e6a5c73SAvihai Horon     migrate_set_capability(to, "return-path", true);
21667e6a5c73SAvihai Horon 
21677e6a5c73SAvihai Horon     migrate_set_capability(from, "switchover-ack", true);
21687e6a5c73SAvihai Horon     migrate_set_capability(to, "switchover-ack", true);
21697e6a5c73SAvihai Horon 
21707e6a5c73SAvihai Horon     return NULL;
21717e6a5c73SAvihai Horon }
21727e6a5c73SAvihai Horon 
test_precopy_tcp_switchover_ack(void)21737e6a5c73SAvihai Horon static void test_precopy_tcp_switchover_ack(void)
21747e6a5c73SAvihai Horon {
21757e6a5c73SAvihai Horon     MigrateCommon args = {
21767e6a5c73SAvihai Horon         .listen_uri = "tcp:127.0.0.1:0",
21777e6a5c73SAvihai Horon         .start_hook = test_migrate_switchover_ack_start,
21787e6a5c73SAvihai Horon         /*
21797e6a5c73SAvihai Horon          * Source VM must be running in order to consider the switchover ACK
21807e6a5c73SAvihai Horon          * when deciding to do switchover or not.
21817e6a5c73SAvihai Horon          */
21827e6a5c73SAvihai Horon         .live = true,
21837e6a5c73SAvihai Horon     };
21847e6a5c73SAvihai Horon 
21857e6a5c73SAvihai Horon     test_precopy_common(&args);
21867e6a5c73SAvihai Horon }
21877e6a5c73SAvihai Horon 
218858d25e97SDaniel P. Berrangé #ifdef CONFIG_GNUTLS
test_precopy_tcp_tls_psk_match(void)218958d25e97SDaniel P. Berrangé static void test_precopy_tcp_tls_psk_match(void)
219058d25e97SDaniel P. Berrangé {
219158d25e97SDaniel P. Berrangé     MigrateCommon args = {
219258d25e97SDaniel P. Berrangé         .listen_uri = "tcp:127.0.0.1:0",
219358d25e97SDaniel P. Berrangé         .start_hook = test_migrate_tls_psk_start_match,
219458d25e97SDaniel P. Berrangé         .finish_hook = test_migrate_tls_psk_finish,
219558d25e97SDaniel P. Berrangé     };
219658d25e97SDaniel P. Berrangé 
219758d25e97SDaniel P. Berrangé     test_precopy_common(&args);
219858d25e97SDaniel P. Berrangé }
219958d25e97SDaniel P. Berrangé 
test_precopy_tcp_tls_psk_mismatch(void)220058d25e97SDaniel P. Berrangé static void test_precopy_tcp_tls_psk_mismatch(void)
220158d25e97SDaniel P. Berrangé {
220258d25e97SDaniel P. Berrangé     MigrateCommon args = {
220358d25e97SDaniel P. Berrangé         .start = {
220458d25e97SDaniel P. Berrangé             .hide_stderr = true,
220558d25e97SDaniel P. Berrangé         },
220658d25e97SDaniel P. Berrangé         .listen_uri = "tcp:127.0.0.1:0",
220758d25e97SDaniel P. Berrangé         .start_hook = test_migrate_tls_psk_start_mismatch,
220858d25e97SDaniel P. Berrangé         .finish_hook = test_migrate_tls_psk_finish,
220958d25e97SDaniel P. Berrangé         .result = MIG_TEST_FAIL,
221058d25e97SDaniel P. Berrangé     };
221158d25e97SDaniel P. Berrangé 
221258d25e97SDaniel P. Berrangé     test_precopy_common(&args);
221358d25e97SDaniel P. Berrangé }
2214d47b83b1SDaniel P. Berrangé 
2215d47b83b1SDaniel P. Berrangé #ifdef CONFIG_TASN1
test_precopy_tcp_tls_x509_default_host(void)2216d47b83b1SDaniel P. Berrangé static void test_precopy_tcp_tls_x509_default_host(void)
2217d47b83b1SDaniel P. Berrangé {
2218d47b83b1SDaniel P. Berrangé     MigrateCommon args = {
2219d47b83b1SDaniel P. Berrangé         .listen_uri = "tcp:127.0.0.1:0",
2220d47b83b1SDaniel P. Berrangé         .start_hook = test_migrate_tls_x509_start_default_host,
2221d47b83b1SDaniel P. Berrangé         .finish_hook = test_migrate_tls_x509_finish,
2222d47b83b1SDaniel P. Berrangé     };
2223d47b83b1SDaniel P. Berrangé 
2224d47b83b1SDaniel P. Berrangé     test_precopy_common(&args);
2225d47b83b1SDaniel P. Berrangé }
2226d47b83b1SDaniel P. Berrangé 
test_precopy_tcp_tls_x509_override_host(void)2227d47b83b1SDaniel P. Berrangé static void test_precopy_tcp_tls_x509_override_host(void)
2228d47b83b1SDaniel P. Berrangé {
2229d47b83b1SDaniel P. Berrangé     MigrateCommon args = {
2230d47b83b1SDaniel P. Berrangé         .listen_uri = "tcp:127.0.0.1:0",
2231d47b83b1SDaniel P. Berrangé         .start_hook = test_migrate_tls_x509_start_override_host,
2232d47b83b1SDaniel P. Berrangé         .finish_hook = test_migrate_tls_x509_finish,
2233d47b83b1SDaniel P. Berrangé     };
2234d47b83b1SDaniel P. Berrangé 
2235d47b83b1SDaniel P. Berrangé     test_precopy_common(&args);
2236d47b83b1SDaniel P. Berrangé }
2237d47b83b1SDaniel P. Berrangé 
test_precopy_tcp_tls_x509_mismatch_host(void)2238d47b83b1SDaniel P. Berrangé static void test_precopy_tcp_tls_x509_mismatch_host(void)
2239d47b83b1SDaniel P. Berrangé {
2240d47b83b1SDaniel P. Berrangé     MigrateCommon args = {
2241d47b83b1SDaniel P. Berrangé         .start = {
2242d47b83b1SDaniel P. Berrangé             .hide_stderr = true,
2243d47b83b1SDaniel P. Berrangé         },
2244d47b83b1SDaniel P. Berrangé         .listen_uri = "tcp:127.0.0.1:0",
2245d47b83b1SDaniel P. Berrangé         .start_hook = test_migrate_tls_x509_start_mismatch_host,
2246d47b83b1SDaniel P. Berrangé         .finish_hook = test_migrate_tls_x509_finish,
2247d47b83b1SDaniel P. Berrangé         .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
2248d47b83b1SDaniel P. Berrangé     };
2249d47b83b1SDaniel P. Berrangé 
2250d47b83b1SDaniel P. Berrangé     test_precopy_common(&args);
2251d47b83b1SDaniel P. Berrangé }
2252d47b83b1SDaniel P. Berrangé 
test_precopy_tcp_tls_x509_friendly_client(void)2253d47b83b1SDaniel P. Berrangé static void test_precopy_tcp_tls_x509_friendly_client(void)
2254d47b83b1SDaniel P. Berrangé {
2255d47b83b1SDaniel P. Berrangé     MigrateCommon args = {
2256d47b83b1SDaniel P. Berrangé         .listen_uri = "tcp:127.0.0.1:0",
2257d47b83b1SDaniel P. Berrangé         .start_hook = test_migrate_tls_x509_start_friendly_client,
2258d47b83b1SDaniel P. Berrangé         .finish_hook = test_migrate_tls_x509_finish,
2259d47b83b1SDaniel P. Berrangé     };
2260d47b83b1SDaniel P. Berrangé 
2261d47b83b1SDaniel P. Berrangé     test_precopy_common(&args);
2262d47b83b1SDaniel P. Berrangé }
2263d47b83b1SDaniel P. Berrangé 
test_precopy_tcp_tls_x509_hostile_client(void)2264d47b83b1SDaniel P. Berrangé static void test_precopy_tcp_tls_x509_hostile_client(void)
2265d47b83b1SDaniel P. Berrangé {
2266d47b83b1SDaniel P. Berrangé     MigrateCommon args = {
2267d47b83b1SDaniel P. Berrangé         .start = {
2268d47b83b1SDaniel P. Berrangé             .hide_stderr = true,
2269d47b83b1SDaniel P. Berrangé         },
2270d47b83b1SDaniel P. Berrangé         .listen_uri = "tcp:127.0.0.1:0",
2271d47b83b1SDaniel P. Berrangé         .start_hook = test_migrate_tls_x509_start_hostile_client,
2272d47b83b1SDaniel P. Berrangé         .finish_hook = test_migrate_tls_x509_finish,
2273d47b83b1SDaniel P. Berrangé         .result = MIG_TEST_FAIL,
2274d47b83b1SDaniel P. Berrangé     };
2275d47b83b1SDaniel P. Berrangé 
2276d47b83b1SDaniel P. Berrangé     test_precopy_common(&args);
2277d47b83b1SDaniel P. Berrangé }
2278d47b83b1SDaniel P. Berrangé 
test_precopy_tcp_tls_x509_allow_anon_client(void)2279d47b83b1SDaniel P. Berrangé static void test_precopy_tcp_tls_x509_allow_anon_client(void)
2280d47b83b1SDaniel P. Berrangé {
2281d47b83b1SDaniel P. Berrangé     MigrateCommon args = {
2282d47b83b1SDaniel P. Berrangé         .listen_uri = "tcp:127.0.0.1:0",
2283d47b83b1SDaniel P. Berrangé         .start_hook = test_migrate_tls_x509_start_allow_anon_client,
2284d47b83b1SDaniel P. Berrangé         .finish_hook = test_migrate_tls_x509_finish,
2285d47b83b1SDaniel P. Berrangé     };
2286d47b83b1SDaniel P. Berrangé 
2287d47b83b1SDaniel P. Berrangé     test_precopy_common(&args);
2288d47b83b1SDaniel P. Berrangé }
2289d47b83b1SDaniel P. Berrangé 
test_precopy_tcp_tls_x509_reject_anon_client(void)2290d47b83b1SDaniel P. Berrangé static void test_precopy_tcp_tls_x509_reject_anon_client(void)
2291d47b83b1SDaniel P. Berrangé {
2292d47b83b1SDaniel P. Berrangé     MigrateCommon args = {
2293d47b83b1SDaniel P. Berrangé         .start = {
2294d47b83b1SDaniel P. Berrangé             .hide_stderr = true,
2295d47b83b1SDaniel P. Berrangé         },
2296d47b83b1SDaniel P. Berrangé         .listen_uri = "tcp:127.0.0.1:0",
2297d47b83b1SDaniel P. Berrangé         .start_hook = test_migrate_tls_x509_start_reject_anon_client,
2298d47b83b1SDaniel P. Berrangé         .finish_hook = test_migrate_tls_x509_finish,
2299d47b83b1SDaniel P. Berrangé         .result = MIG_TEST_FAIL,
2300d47b83b1SDaniel P. Berrangé     };
2301d47b83b1SDaniel P. Berrangé 
2302d47b83b1SDaniel P. Berrangé     test_precopy_common(&args);
2303d47b83b1SDaniel P. Berrangé }
2304d47b83b1SDaniel P. Berrangé #endif /* CONFIG_TASN1 */
230558d25e97SDaniel P. Berrangé #endif /* CONFIG_GNUTLS */
230658d25e97SDaniel P. Berrangé 
2307d7613ee2SBin Meng #ifndef _WIN32
test_migrate_fd_start_hook(QTestState * from,QTestState * to)2308243e0066SDaniel P. Berrangé static void *test_migrate_fd_start_hook(QTestState *from,
2309243e0066SDaniel P. Berrangé                                         QTestState *to)
23101e8a1faeSThomas Huth {
23111e8a1faeSThomas Huth     int ret;
23121e8a1faeSThomas Huth     int pair[2];
23131e8a1faeSThomas Huth 
23141e8a1faeSThomas Huth     /* Create two connected sockets for migration */
23150038e9a2SGuoyi Tu     ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
23161e8a1faeSThomas Huth     g_assert_cmpint(ret, ==, 0);
23171e8a1faeSThomas Huth 
23181e8a1faeSThomas Huth     /* Send the 1st socket to the target */
2319aca04069SDaniel P. Berrangé     qtest_qmp_fds_assert_success(to, &pair[0], 1,
23201e8a1faeSThomas Huth                                  "{ 'execute': 'getfd',"
23211e8a1faeSThomas Huth                                  "  'arguments': { 'fdname': 'fd-mig' }}");
23221e8a1faeSThomas Huth     close(pair[0]);
23231e8a1faeSThomas Huth 
23241e8a1faeSThomas Huth     /* Start incoming migration from the 1st socket */
23256830e53bSFabiano Rosas     migrate_incoming_qmp(to, "fd:fd-mig", "{}");
23261e8a1faeSThomas Huth 
23271e8a1faeSThomas Huth     /* Send the 2nd socket to the target */
2328aca04069SDaniel P. Berrangé     qtest_qmp_fds_assert_success(from, &pair[1], 1,
23291e8a1faeSThomas Huth                                  "{ 'execute': 'getfd',"
23301e8a1faeSThomas Huth                                  "  'arguments': { 'fdname': 'fd-mig' }}");
23311e8a1faeSThomas Huth     close(pair[1]);
23321e8a1faeSThomas Huth 
2333243e0066SDaniel P. Berrangé     return NULL;
23341e8a1faeSThomas Huth }
2335243e0066SDaniel P. Berrangé 
test_migrate_fd_finish_hook(QTestState * from,QTestState * to,void * opaque)2336243e0066SDaniel P. Berrangé static void test_migrate_fd_finish_hook(QTestState *from,
2337243e0066SDaniel P. Berrangé                                         QTestState *to,
2338243e0066SDaniel P. Berrangé                                         void *opaque)
2339243e0066SDaniel P. Berrangé {
2340243e0066SDaniel P. Berrangé     QDict *rsp;
2341243e0066SDaniel P. Berrangé     const char *error_desc;
23421e8a1faeSThomas Huth 
23431e8a1faeSThomas Huth     /* Test closing fds */
23441e8a1faeSThomas Huth     /* We assume, that QEMU removes named fd from its list,
23451e8a1faeSThomas Huth      * so this should fail */
23461e8a1faeSThomas Huth     rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
23471e8a1faeSThomas Huth                           "  'arguments': { 'fdname': 'fd-mig' }}");
23481e8a1faeSThomas Huth     g_assert_true(qdict_haskey(rsp, "error"));
23491e8a1faeSThomas Huth     error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
23501e8a1faeSThomas Huth     g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
23511e8a1faeSThomas Huth     qobject_unref(rsp);
23521e8a1faeSThomas Huth 
23531e8a1faeSThomas Huth     rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
23541e8a1faeSThomas Huth                         "  'arguments': { 'fdname': 'fd-mig' }}");
23551e8a1faeSThomas Huth     g_assert_true(qdict_haskey(rsp, "error"));
23561e8a1faeSThomas Huth     error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
23571e8a1faeSThomas Huth     g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
23581e8a1faeSThomas Huth     qobject_unref(rsp);
2359243e0066SDaniel P. Berrangé }
23601e8a1faeSThomas Huth 
test_migrate_fd_proto(void)2361243e0066SDaniel P. Berrangé static void test_migrate_fd_proto(void)
2362243e0066SDaniel P. Berrangé {
2363243e0066SDaniel P. Berrangé     MigrateCommon args = {
2364243e0066SDaniel P. Berrangé         .listen_uri = "defer",
2365243e0066SDaniel P. Berrangé         .connect_uri = "fd:fd-mig",
2366243e0066SDaniel P. Berrangé         .start_hook = test_migrate_fd_start_hook,
2367243e0066SDaniel P. Berrangé         .finish_hook = test_migrate_fd_finish_hook
2368243e0066SDaniel P. Berrangé     };
2369243e0066SDaniel P. Berrangé     test_precopy_common(&args);
23701e8a1faeSThomas Huth }
2371d7613ee2SBin Meng #endif /* _WIN32 */
23721e8a1faeSThomas Huth 
do_test_validate_uuid(MigrateStart * args,bool should_fail)23731e8a1faeSThomas Huth static void do_test_validate_uuid(MigrateStart *args, bool should_fail)
23741e8a1faeSThomas Huth {
2375ff7b9b56SPeter Maydell     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
23761e8a1faeSThomas Huth     QTestState *from, *to;
23771e8a1faeSThomas Huth 
237819da6edfSDaniel P. Berrangé     if (test_migrate_start(&from, &to, uri, args)) {
23791e8a1faeSThomas Huth         return;
23801e8a1faeSThomas Huth     }
23811e8a1faeSThomas Huth 
23821e8a1faeSThomas Huth     /*
23831e8a1faeSThomas Huth      * UUID validation is at the begin of migration. So, the main process of
23841e8a1faeSThomas Huth      * migration is not interesting for us here. Thus, set huge downtime for
23851e8a1faeSThomas Huth      * very fast migration.
23861e8a1faeSThomas Huth      */
23871e8a1faeSThomas Huth     migrate_set_parameter_int(from, "downtime-limit", 1000000);
23881e8a1faeSThomas Huth     migrate_set_capability(from, "validate-uuid", true);
23891e8a1faeSThomas Huth 
23901e8a1faeSThomas Huth     /* Wait for the first serial output from the source */
23911e8a1faeSThomas Huth     wait_for_serial("src_serial");
23921e8a1faeSThomas Huth 
23931e8a1faeSThomas Huth     migrate_qmp(from, uri, "{}");
23941e8a1faeSThomas Huth 
23951e8a1faeSThomas Huth     if (should_fail) {
23961b0f1b14SBin Meng         qtest_set_expected_status(to, EXIT_FAILURE);
23971e8a1faeSThomas Huth         wait_for_migration_fail(from, true);
23981e8a1faeSThomas Huth     } else {
23991e8a1faeSThomas Huth         wait_for_migration_complete(from);
24001e8a1faeSThomas Huth     }
24011e8a1faeSThomas Huth 
24021e8a1faeSThomas Huth     test_migrate_end(from, to, false);
24031e8a1faeSThomas Huth }
24041e8a1faeSThomas Huth 
test_validate_uuid(void)24051e8a1faeSThomas Huth static void test_validate_uuid(void)
24061e8a1faeSThomas Huth {
240719da6edfSDaniel P. Berrangé     MigrateStart args = {
240819da6edfSDaniel P. Berrangé         .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
240919da6edfSDaniel P. Berrangé         .opts_target = "-uuid 11111111-1111-1111-1111-111111111111",
241019da6edfSDaniel P. Berrangé     };
24111e8a1faeSThomas Huth 
241219da6edfSDaniel P. Berrangé     do_test_validate_uuid(&args, false);
24131e8a1faeSThomas Huth }
24141e8a1faeSThomas Huth 
test_validate_uuid_error(void)24151e8a1faeSThomas Huth static void test_validate_uuid_error(void)
24161e8a1faeSThomas Huth {
241719da6edfSDaniel P. Berrangé     MigrateStart args = {
241819da6edfSDaniel P. Berrangé         .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
241919da6edfSDaniel P. Berrangé         .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
242019da6edfSDaniel P. Berrangé         .hide_stderr = true,
242119da6edfSDaniel P. Berrangé     };
24221e8a1faeSThomas Huth 
242319da6edfSDaniel P. Berrangé     do_test_validate_uuid(&args, true);
24241e8a1faeSThomas Huth }
24251e8a1faeSThomas Huth 
test_validate_uuid_src_not_set(void)24261e8a1faeSThomas Huth static void test_validate_uuid_src_not_set(void)
24271e8a1faeSThomas Huth {
242819da6edfSDaniel P. Berrangé     MigrateStart args = {
242919da6edfSDaniel P. Berrangé         .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
243019da6edfSDaniel P. Berrangé         .hide_stderr = true,
243119da6edfSDaniel P. Berrangé     };
24321e8a1faeSThomas Huth 
243319da6edfSDaniel P. Berrangé     do_test_validate_uuid(&args, false);
24341e8a1faeSThomas Huth }
24351e8a1faeSThomas Huth 
test_validate_uuid_dst_not_set(void)24361e8a1faeSThomas Huth static void test_validate_uuid_dst_not_set(void)
24371e8a1faeSThomas Huth {
243819da6edfSDaniel P. Berrangé     MigrateStart args = {
243919da6edfSDaniel P. Berrangé         .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
244019da6edfSDaniel P. Berrangé         .hide_stderr = true,
244119da6edfSDaniel P. Berrangé     };
24421e8a1faeSThomas Huth 
244319da6edfSDaniel P. Berrangé     do_test_validate_uuid(&args, false);
24441e8a1faeSThomas Huth }
24451e8a1faeSThomas Huth 
244674902af7SJuan Quintela /*
244774902af7SJuan Quintela  * The way auto_converge works, we need to do too many passes to
244874902af7SJuan Quintela  * run this test.  Auto_converge logic is only run once every
244974902af7SJuan Quintela  * three iterations, so:
245074902af7SJuan Quintela  *
245174902af7SJuan Quintela  * - 3 iterations without auto_converge enabled
245274902af7SJuan Quintela  * - 3 iterations with pct = 5
245374902af7SJuan Quintela  * - 3 iterations with pct = 30
245474902af7SJuan Quintela  * - 3 iterations with pct = 55
245574902af7SJuan Quintela  * - 3 iterations with pct = 80
245674902af7SJuan Quintela  * - 3 iterations with pct = 95 (max(95, 80 + 25))
245774902af7SJuan Quintela  *
245874902af7SJuan Quintela  * To make things even worse, we need to run the initial stage at
245974902af7SJuan Quintela  * 3MB/s so we enter autoconverge even when host is (over)loaded.
246074902af7SJuan Quintela  */
test_migrate_auto_converge(void)24611e8a1faeSThomas Huth static void test_migrate_auto_converge(void)
24621e8a1faeSThomas Huth {
2463ff7b9b56SPeter Maydell     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
246419da6edfSDaniel P. Berrangé     MigrateStart args = {};
24651e8a1faeSThomas Huth     QTestState *from, *to;
2466219044b8SPeter Xu     int64_t percentage;
24671e8a1faeSThomas Huth 
24681e8a1faeSThomas Huth     /*
24691e8a1faeSThomas Huth      * We want the test to be stable and as fast as possible.
247096420a30SMichael Tokarev      * E.g., with 1Gb/s bandwidth migration may pass without throttling,
24711e8a1faeSThomas Huth      * so we need to decrease a bandwidth.
24721e8a1faeSThomas Huth      */
24731bfc8ddeSDr. David Alan Gilbert     const int64_t init_pct = 5, inc_pct = 25, max_pct = 95;
24741e8a1faeSThomas Huth 
24753ff57401SPeter Xu     if (test_migrate_start(&from, &to, uri, &args)) {
24761e8a1faeSThomas Huth         return;
24771e8a1faeSThomas Huth     }
24781e8a1faeSThomas Huth 
24791e8a1faeSThomas Huth     migrate_set_capability(from, "auto-converge", true);
24801e8a1faeSThomas Huth     migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct);
24811e8a1faeSThomas Huth     migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct);
24821e8a1faeSThomas Huth     migrate_set_parameter_int(from, "max-cpu-throttle", max_pct);
24831e8a1faeSThomas Huth 
24841e8a1faeSThomas Huth     /*
24851e8a1faeSThomas Huth      * Set the initial parameters so that the migration could not converge
24861e8a1faeSThomas Huth      * without throttling.
24871e8a1faeSThomas Huth      */
2488886dfe9dSDaniel P. Berrangé     migrate_ensure_non_converge(from);
24891e8a1faeSThomas Huth 
24901e8a1faeSThomas Huth     /* To check remaining size after precopy */
24911e8a1faeSThomas Huth     migrate_set_capability(from, "pause-before-switchover", true);
24921e8a1faeSThomas Huth 
24931e8a1faeSThomas Huth     /* Wait for the first serial output from the source */
24941e8a1faeSThomas Huth     wait_for_serial("src_serial");
24951e8a1faeSThomas Huth 
24961e8a1faeSThomas Huth     migrate_qmp(from, uri, "{}");
24971e8a1faeSThomas Huth 
24981e8a1faeSThomas Huth     /* Wait for throttling begins */
24991e8a1faeSThomas Huth     percentage = 0;
25001bfc8ddeSDr. David Alan Gilbert     do {
25011e8a1faeSThomas Huth         percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
25021bfc8ddeSDr. David Alan Gilbert         if (percentage != 0) {
25031bfc8ddeSDr. David Alan Gilbert             break;
25041e8a1faeSThomas Huth         }
25051bfc8ddeSDr. David Alan Gilbert         usleep(20);
250695014994SDaniel P. Berrangé         g_assert_false(got_src_stop);
25071bfc8ddeSDr. David Alan Gilbert     } while (true);
25081bfc8ddeSDr. David Alan Gilbert     /* The first percentage of throttling should be at least init_pct */
25091bfc8ddeSDr. David Alan Gilbert     g_assert_cmpint(percentage, >=, init_pct);
25101e8a1faeSThomas Huth     /* Now, when we tested that throttling works, let it converge */
2511219044b8SPeter Xu     migrate_ensure_converge(from);
25121e8a1faeSThomas Huth 
25131e8a1faeSThomas Huth     /*
25141e8a1faeSThomas Huth      * Wait for pre-switchover status to check last throttle percentage
25151e8a1faeSThomas Huth      * and remaining. These values will be zeroed later
25161e8a1faeSThomas Huth      */
25171e8a1faeSThomas Huth     wait_for_migration_status(from, "pre-switchover", NULL);
25181e8a1faeSThomas Huth 
25191e8a1faeSThomas Huth     /* The final percentage of throttling shouldn't be greater than max_pct */
25201e8a1faeSThomas Huth     percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
25211e8a1faeSThomas Huth     g_assert_cmpint(percentage, <=, max_pct);
25221e8a1faeSThomas Huth     migrate_continue(from, "pre-switchover");
25231e8a1faeSThomas Huth 
25241e8a1faeSThomas Huth     qtest_qmp_eventwait(to, "RESUME");
25251e8a1faeSThomas Huth 
25261e8a1faeSThomas Huth     wait_for_serial("dest_serial");
25271e8a1faeSThomas Huth     wait_for_migration_complete(from);
25281e8a1faeSThomas Huth 
25291e8a1faeSThomas Huth     test_migrate_end(from, to, true);
25301e8a1faeSThomas Huth }
25311e8a1faeSThomas Huth 
2532490facffSDaniel P. Berrangé static void *
test_migrate_precopy_tcp_multifd_start_common(QTestState * from,QTestState * to,const char * method)2533490facffSDaniel P. Berrangé test_migrate_precopy_tcp_multifd_start_common(QTestState *from,
2534490facffSDaniel P. Berrangé                                               QTestState *to,
2535490facffSDaniel P. Berrangé                                               const char *method)
2536b99784efSJuan Quintela {
2537b99784efSJuan Quintela     migrate_set_parameter_int(from, "multifd-channels", 16);
2538b99784efSJuan Quintela     migrate_set_parameter_int(to, "multifd-channels", 16);
2539b99784efSJuan Quintela 
254096eef042SJuan Quintela     migrate_set_parameter_str(from, "multifd-compression", method);
254196eef042SJuan Quintela     migrate_set_parameter_str(to, "multifd-compression", method);
254296eef042SJuan Quintela 
2543a1209bb7SDr. David Alan Gilbert     migrate_set_capability(from, "multifd", true);
2544a1209bb7SDr. David Alan Gilbert     migrate_set_capability(to, "multifd", true);
2545b99784efSJuan Quintela 
2546b99784efSJuan Quintela     /* Start incoming migration from the 1st socket */
25476830e53bSFabiano Rosas     migrate_incoming_qmp(to, "tcp:127.0.0.1:0", "{}");
2548b99784efSJuan Quintela 
2549490facffSDaniel P. Berrangé     return NULL;
2550b99784efSJuan Quintela }
2551b99784efSJuan Quintela 
2552490facffSDaniel P. Berrangé static void *
test_migrate_precopy_tcp_multifd_start(QTestState * from,QTestState * to)2553490facffSDaniel P. Berrangé test_migrate_precopy_tcp_multifd_start(QTestState *from,
2554490facffSDaniel P. Berrangé                                        QTestState *to)
2555490facffSDaniel P. Berrangé {
2556490facffSDaniel P. Berrangé     return test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2557b99784efSJuan Quintela }
2558b99784efSJuan Quintela 
2559490facffSDaniel P. Berrangé static void *
test_migrate_precopy_tcp_multifd_zlib_start(QTestState * from,QTestState * to)2560490facffSDaniel P. Berrangé test_migrate_precopy_tcp_multifd_zlib_start(QTestState *from,
2561490facffSDaniel P. Berrangé                                             QTestState *to)
2562490facffSDaniel P. Berrangé {
2563490facffSDaniel P. Berrangé     return test_migrate_precopy_tcp_multifd_start_common(from, to, "zlib");
2564490facffSDaniel P. Berrangé }
2565490facffSDaniel P. Berrangé 
2566490facffSDaniel P. Berrangé #ifdef CONFIG_ZSTD
2567490facffSDaniel P. Berrangé static void *
test_migrate_precopy_tcp_multifd_zstd_start(QTestState * from,QTestState * to)2568490facffSDaniel P. Berrangé test_migrate_precopy_tcp_multifd_zstd_start(QTestState *from,
2569490facffSDaniel P. Berrangé                                             QTestState *to)
2570490facffSDaniel P. Berrangé {
2571490facffSDaniel P. Berrangé     return test_migrate_precopy_tcp_multifd_start_common(from, to, "zstd");
2572490facffSDaniel P. Berrangé }
2573490facffSDaniel P. Berrangé #endif /* CONFIG_ZSTD */
2574490facffSDaniel P. Berrangé 
test_multifd_tcp_none(void)257596eef042SJuan Quintela static void test_multifd_tcp_none(void)
257696eef042SJuan Quintela {
2577490facffSDaniel P. Berrangé     MigrateCommon args = {
2578490facffSDaniel P. Berrangé         .listen_uri = "defer",
2579490facffSDaniel P. Berrangé         .start_hook = test_migrate_precopy_tcp_multifd_start,
2580b861383cSPeter Xu         /*
2581b861383cSPeter Xu          * Multifd is more complicated than most of the features, it
2582b861383cSPeter Xu          * directly takes guest page buffers when sending, make sure
2583b861383cSPeter Xu          * everything will work alright even if guest page is changing.
2584b861383cSPeter Xu          */
25853c4fb177SDaniel P. Berrangé         .live = true,
2586490facffSDaniel P. Berrangé     };
2587490facffSDaniel P. Berrangé     test_precopy_common(&args);
258896eef042SJuan Quintela }
258996eef042SJuan Quintela 
test_multifd_tcp_zlib(void)25907ec2c2b3SJuan Quintela static void test_multifd_tcp_zlib(void)
25917ec2c2b3SJuan Quintela {
2592490facffSDaniel P. Berrangé     MigrateCommon args = {
2593490facffSDaniel P. Berrangé         .listen_uri = "defer",
2594490facffSDaniel P. Berrangé         .start_hook = test_migrate_precopy_tcp_multifd_zlib_start,
2595490facffSDaniel P. Berrangé     };
2596490facffSDaniel P. Berrangé     test_precopy_common(&args);
25977ec2c2b3SJuan Quintela }
25987ec2c2b3SJuan Quintela 
259987dc6f5fSJuan Quintela #ifdef CONFIG_ZSTD
test_multifd_tcp_zstd(void)260087dc6f5fSJuan Quintela static void test_multifd_tcp_zstd(void)
260187dc6f5fSJuan Quintela {
2602490facffSDaniel P. Berrangé     MigrateCommon args = {
2603490facffSDaniel P. Berrangé         .listen_uri = "defer",
2604490facffSDaniel P. Berrangé         .start_hook = test_migrate_precopy_tcp_multifd_zstd_start,
2605490facffSDaniel P. Berrangé     };
2606490facffSDaniel P. Berrangé     test_precopy_common(&args);
260787dc6f5fSJuan Quintela }
260887dc6f5fSJuan Quintela #endif
260987dc6f5fSJuan Quintela 
26104d6d2e87SDaniel P. Berrangé #ifdef CONFIG_GNUTLS
26114d6d2e87SDaniel P. Berrangé static void *
test_migrate_multifd_tcp_tls_psk_start_match(QTestState * from,QTestState * to)26124d6d2e87SDaniel P. Berrangé test_migrate_multifd_tcp_tls_psk_start_match(QTestState *from,
26134d6d2e87SDaniel P. Berrangé                                              QTestState *to)
26144d6d2e87SDaniel P. Berrangé {
26154d6d2e87SDaniel P. Berrangé     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
26164d6d2e87SDaniel P. Berrangé     return test_migrate_tls_psk_start_match(from, to);
26174d6d2e87SDaniel P. Berrangé }
26184d6d2e87SDaniel P. Berrangé 
26194d6d2e87SDaniel P. Berrangé static void *
test_migrate_multifd_tcp_tls_psk_start_mismatch(QTestState * from,QTestState * to)26204d6d2e87SDaniel P. Berrangé test_migrate_multifd_tcp_tls_psk_start_mismatch(QTestState *from,
26214d6d2e87SDaniel P. Berrangé                                                 QTestState *to)
26224d6d2e87SDaniel P. Berrangé {
26234d6d2e87SDaniel P. Berrangé     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
26244d6d2e87SDaniel P. Berrangé     return test_migrate_tls_psk_start_mismatch(from, to);
26254d6d2e87SDaniel P. Berrangé }
26264d6d2e87SDaniel P. Berrangé 
2627ff32f1ddSDaniel P. Berrangé #ifdef CONFIG_TASN1
2628ff32f1ddSDaniel P. Berrangé static void *
test_migrate_multifd_tls_x509_start_default_host(QTestState * from,QTestState * to)2629ff32f1ddSDaniel P. Berrangé test_migrate_multifd_tls_x509_start_default_host(QTestState *from,
2630ff32f1ddSDaniel P. Berrangé                                                  QTestState *to)
2631ff32f1ddSDaniel P. Berrangé {
2632ff32f1ddSDaniel P. Berrangé     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2633ff32f1ddSDaniel P. Berrangé     return test_migrate_tls_x509_start_default_host(from, to);
2634ff32f1ddSDaniel P. Berrangé }
2635ff32f1ddSDaniel P. Berrangé 
2636ff32f1ddSDaniel P. Berrangé static void *
test_migrate_multifd_tls_x509_start_override_host(QTestState * from,QTestState * to)2637ff32f1ddSDaniel P. Berrangé test_migrate_multifd_tls_x509_start_override_host(QTestState *from,
2638ff32f1ddSDaniel P. Berrangé                                                   QTestState *to)
2639ff32f1ddSDaniel P. Berrangé {
2640ff32f1ddSDaniel P. Berrangé     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2641ff32f1ddSDaniel P. Berrangé     return test_migrate_tls_x509_start_override_host(from, to);
2642ff32f1ddSDaniel P. Berrangé }
2643ff32f1ddSDaniel P. Berrangé 
2644ff32f1ddSDaniel P. Berrangé static void *
test_migrate_multifd_tls_x509_start_mismatch_host(QTestState * from,QTestState * to)2645ff32f1ddSDaniel P. Berrangé test_migrate_multifd_tls_x509_start_mismatch_host(QTestState *from,
2646ff32f1ddSDaniel P. Berrangé                                                   QTestState *to)
2647ff32f1ddSDaniel P. Berrangé {
2648ff32f1ddSDaniel P. Berrangé     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2649ff32f1ddSDaniel P. Berrangé     return test_migrate_tls_x509_start_mismatch_host(from, to);
2650ff32f1ddSDaniel P. Berrangé }
2651ff32f1ddSDaniel P. Berrangé 
2652ff32f1ddSDaniel P. Berrangé static void *
test_migrate_multifd_tls_x509_start_allow_anon_client(QTestState * from,QTestState * to)2653ff32f1ddSDaniel P. Berrangé test_migrate_multifd_tls_x509_start_allow_anon_client(QTestState *from,
2654ff32f1ddSDaniel P. Berrangé                                                       QTestState *to)
2655ff32f1ddSDaniel P. Berrangé {
2656ff32f1ddSDaniel P. Berrangé     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2657ff32f1ddSDaniel P. Berrangé     return test_migrate_tls_x509_start_allow_anon_client(from, to);
2658ff32f1ddSDaniel P. Berrangé }
2659ff32f1ddSDaniel P. Berrangé 
2660ff32f1ddSDaniel P. Berrangé static void *
test_migrate_multifd_tls_x509_start_reject_anon_client(QTestState * from,QTestState * to)2661ff32f1ddSDaniel P. Berrangé test_migrate_multifd_tls_x509_start_reject_anon_client(QTestState *from,
2662ff32f1ddSDaniel P. Berrangé                                                        QTestState *to)
2663ff32f1ddSDaniel P. Berrangé {
2664ff32f1ddSDaniel P. Berrangé     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2665ff32f1ddSDaniel P. Berrangé     return test_migrate_tls_x509_start_reject_anon_client(from, to);
2666ff32f1ddSDaniel P. Berrangé }
2667ff32f1ddSDaniel P. Berrangé #endif /* CONFIG_TASN1 */
2668ff32f1ddSDaniel P. Berrangé 
test_multifd_tcp_tls_psk_match(void)26694d6d2e87SDaniel P. Berrangé static void test_multifd_tcp_tls_psk_match(void)
26704d6d2e87SDaniel P. Berrangé {
26714d6d2e87SDaniel P. Berrangé     MigrateCommon args = {
26724d6d2e87SDaniel P. Berrangé         .listen_uri = "defer",
26734d6d2e87SDaniel P. Berrangé         .start_hook = test_migrate_multifd_tcp_tls_psk_start_match,
26744d6d2e87SDaniel P. Berrangé         .finish_hook = test_migrate_tls_psk_finish,
26754d6d2e87SDaniel P. Berrangé     };
26764d6d2e87SDaniel P. Berrangé     test_precopy_common(&args);
26774d6d2e87SDaniel P. Berrangé }
26784d6d2e87SDaniel P. Berrangé 
test_multifd_tcp_tls_psk_mismatch(void)26794d6d2e87SDaniel P. Berrangé static void test_multifd_tcp_tls_psk_mismatch(void)
26804d6d2e87SDaniel P. Berrangé {
26814d6d2e87SDaniel P. Berrangé     MigrateCommon args = {
26824d6d2e87SDaniel P. Berrangé         .start = {
26834d6d2e87SDaniel P. Berrangé             .hide_stderr = true,
26844d6d2e87SDaniel P. Berrangé         },
26854d6d2e87SDaniel P. Berrangé         .listen_uri = "defer",
26864d6d2e87SDaniel P. Berrangé         .start_hook = test_migrate_multifd_tcp_tls_psk_start_mismatch,
26874d6d2e87SDaniel P. Berrangé         .finish_hook = test_migrate_tls_psk_finish,
26884d6d2e87SDaniel P. Berrangé         .result = MIG_TEST_FAIL,
26894d6d2e87SDaniel P. Berrangé     };
26904d6d2e87SDaniel P. Berrangé     test_precopy_common(&args);
26914d6d2e87SDaniel P. Berrangé }
2692ff32f1ddSDaniel P. Berrangé 
2693ff32f1ddSDaniel P. Berrangé #ifdef CONFIG_TASN1
test_multifd_tcp_tls_x509_default_host(void)2694ff32f1ddSDaniel P. Berrangé static void test_multifd_tcp_tls_x509_default_host(void)
2695ff32f1ddSDaniel P. Berrangé {
2696ff32f1ddSDaniel P. Berrangé     MigrateCommon args = {
2697ff32f1ddSDaniel P. Berrangé         .listen_uri = "defer",
2698ff32f1ddSDaniel P. Berrangé         .start_hook = test_migrate_multifd_tls_x509_start_default_host,
2699ff32f1ddSDaniel P. Berrangé         .finish_hook = test_migrate_tls_x509_finish,
2700ff32f1ddSDaniel P. Berrangé     };
2701ff32f1ddSDaniel P. Berrangé     test_precopy_common(&args);
2702ff32f1ddSDaniel P. Berrangé }
2703ff32f1ddSDaniel P. Berrangé 
test_multifd_tcp_tls_x509_override_host(void)2704ff32f1ddSDaniel P. Berrangé static void test_multifd_tcp_tls_x509_override_host(void)
2705ff32f1ddSDaniel P. Berrangé {
2706ff32f1ddSDaniel P. Berrangé     MigrateCommon args = {
2707ff32f1ddSDaniel P. Berrangé         .listen_uri = "defer",
2708ff32f1ddSDaniel P. Berrangé         .start_hook = test_migrate_multifd_tls_x509_start_override_host,
2709ff32f1ddSDaniel P. Berrangé         .finish_hook = test_migrate_tls_x509_finish,
2710ff32f1ddSDaniel P. Berrangé     };
2711ff32f1ddSDaniel P. Berrangé     test_precopy_common(&args);
2712ff32f1ddSDaniel P. Berrangé }
2713ff32f1ddSDaniel P. Berrangé 
test_multifd_tcp_tls_x509_mismatch_host(void)2714ff32f1ddSDaniel P. Berrangé static void test_multifd_tcp_tls_x509_mismatch_host(void)
2715ff32f1ddSDaniel P. Berrangé {
2716ff32f1ddSDaniel P. Berrangé     /*
2717ff32f1ddSDaniel P. Berrangé      * This has different behaviour to the non-multifd case.
2718ff32f1ddSDaniel P. Berrangé      *
2719ff32f1ddSDaniel P. Berrangé      * In non-multifd case when client aborts due to mismatched
2720ff32f1ddSDaniel P. Berrangé      * cert host, the server has already started trying to load
2721ff32f1ddSDaniel P. Berrangé      * migration state, and so it exits with I/O failure.
2722ff32f1ddSDaniel P. Berrangé      *
2723ff32f1ddSDaniel P. Berrangé      * In multifd case when client aborts due to mismatched
2724ff32f1ddSDaniel P. Berrangé      * cert host, the server is still waiting for the other
2725ff32f1ddSDaniel P. Berrangé      * multifd connections to arrive so hasn't started trying
2726ff32f1ddSDaniel P. Berrangé      * to load migration state, and thus just aborts the migration
2727ff32f1ddSDaniel P. Berrangé      * without exiting.
2728ff32f1ddSDaniel P. Berrangé      */
2729ff32f1ddSDaniel P. Berrangé     MigrateCommon args = {
2730ff32f1ddSDaniel P. Berrangé         .start = {
2731ff32f1ddSDaniel P. Berrangé             .hide_stderr = true,
2732ff32f1ddSDaniel P. Berrangé         },
2733ff32f1ddSDaniel P. Berrangé         .listen_uri = "defer",
2734ff32f1ddSDaniel P. Berrangé         .start_hook = test_migrate_multifd_tls_x509_start_mismatch_host,
2735ff32f1ddSDaniel P. Berrangé         .finish_hook = test_migrate_tls_x509_finish,
2736ff32f1ddSDaniel P. Berrangé         .result = MIG_TEST_FAIL,
2737ff32f1ddSDaniel P. Berrangé     };
2738ff32f1ddSDaniel P. Berrangé     test_precopy_common(&args);
2739ff32f1ddSDaniel P. Berrangé }
2740ff32f1ddSDaniel P. Berrangé 
test_multifd_tcp_tls_x509_allow_anon_client(void)2741ff32f1ddSDaniel P. Berrangé static void test_multifd_tcp_tls_x509_allow_anon_client(void)
2742ff32f1ddSDaniel P. Berrangé {
2743ff32f1ddSDaniel P. Berrangé     MigrateCommon args = {
2744ff32f1ddSDaniel P. Berrangé         .listen_uri = "defer",
2745ff32f1ddSDaniel P. Berrangé         .start_hook = test_migrate_multifd_tls_x509_start_allow_anon_client,
2746ff32f1ddSDaniel P. Berrangé         .finish_hook = test_migrate_tls_x509_finish,
2747ff32f1ddSDaniel P. Berrangé     };
2748ff32f1ddSDaniel P. Berrangé     test_precopy_common(&args);
2749ff32f1ddSDaniel P. Berrangé }
2750ff32f1ddSDaniel P. Berrangé 
test_multifd_tcp_tls_x509_reject_anon_client(void)2751ff32f1ddSDaniel P. Berrangé static void test_multifd_tcp_tls_x509_reject_anon_client(void)
2752ff32f1ddSDaniel P. Berrangé {
2753ff32f1ddSDaniel P. Berrangé     MigrateCommon args = {
2754ff32f1ddSDaniel P. Berrangé         .start = {
2755ff32f1ddSDaniel P. Berrangé             .hide_stderr = true,
2756ff32f1ddSDaniel P. Berrangé         },
2757ff32f1ddSDaniel P. Berrangé         .listen_uri = "defer",
2758ff32f1ddSDaniel P. Berrangé         .start_hook = test_migrate_multifd_tls_x509_start_reject_anon_client,
2759ff32f1ddSDaniel P. Berrangé         .finish_hook = test_migrate_tls_x509_finish,
2760ff32f1ddSDaniel P. Berrangé         .result = MIG_TEST_FAIL,
2761ff32f1ddSDaniel P. Berrangé     };
2762ff32f1ddSDaniel P. Berrangé     test_precopy_common(&args);
2763ff32f1ddSDaniel P. Berrangé }
2764ff32f1ddSDaniel P. Berrangé #endif /* CONFIG_TASN1 */
27654d6d2e87SDaniel P. Berrangé #endif /* CONFIG_GNUTLS */
27664d6d2e87SDaniel P. Berrangé 
2767d795f474SJuan Quintela /*
2768d795f474SJuan Quintela  * This test does:
2769d795f474SJuan Quintela  *  source               target
2770d795f474SJuan Quintela  *                       migrate_incoming
2771d795f474SJuan Quintela  *     migrate
2772d795f474SJuan Quintela  *     migrate_cancel
2773d795f474SJuan Quintela  *                       launch another target
2774d795f474SJuan Quintela  *     migrate
2775d795f474SJuan Quintela  *
2776d795f474SJuan Quintela  *  And see that it works
2777d795f474SJuan Quintela  */
test_multifd_tcp_cancel(void)2778d795f474SJuan Quintela static void test_multifd_tcp_cancel(void)
2779d795f474SJuan Quintela {
278019da6edfSDaniel P. Berrangé     MigrateStart args = {
278119da6edfSDaniel P. Berrangé         .hide_stderr = true,
278219da6edfSDaniel P. Berrangé     };
2783d795f474SJuan Quintela     QTestState *from, *to, *to2;
2784ff7b9b56SPeter Maydell     g_autofree char *uri = NULL;
2785d795f474SJuan Quintela 
27863ff57401SPeter Xu     if (test_migrate_start(&from, &to, "defer", &args)) {
2787d795f474SJuan Quintela         return;
2788d795f474SJuan Quintela     }
2789d795f474SJuan Quintela 
2790886dfe9dSDaniel P. Berrangé     migrate_ensure_non_converge(from);
2791e02f56e3SDaniel P. Berrangé     migrate_prepare_for_dirty_mem(from);
2792d795f474SJuan Quintela 
2793d795f474SJuan Quintela     migrate_set_parameter_int(from, "multifd-channels", 16);
2794d795f474SJuan Quintela     migrate_set_parameter_int(to, "multifd-channels", 16);
2795d795f474SJuan Quintela 
2796a1209bb7SDr. David Alan Gilbert     migrate_set_capability(from, "multifd", true);
2797a1209bb7SDr. David Alan Gilbert     migrate_set_capability(to, "multifd", true);
2798d795f474SJuan Quintela 
2799d795f474SJuan Quintela     /* Start incoming migration from the 1st socket */
28006830e53bSFabiano Rosas     migrate_incoming_qmp(to, "tcp:127.0.0.1:0", "{}");
2801d795f474SJuan Quintela 
2802d795f474SJuan Quintela     /* Wait for the first serial output from the source */
2803d795f474SJuan Quintela     wait_for_serial("src_serial");
2804d795f474SJuan Quintela 
2805d795f474SJuan Quintela     uri = migrate_get_socket_address(to, "socket-address");
2806d795f474SJuan Quintela 
2807d795f474SJuan Quintela     migrate_qmp(from, uri, "{}");
2808d795f474SJuan Quintela 
2809e02f56e3SDaniel P. Berrangé     migrate_wait_for_dirty_mem(from, to);
2810d795f474SJuan Quintela 
2811d795f474SJuan Quintela     migrate_cancel(from);
2812d795f474SJuan Quintela 
2813f2d063e6SXuzhou Cheng     /* Make sure QEMU process "to" exited */
2814f2d063e6SXuzhou Cheng     qtest_set_expected_status(to, EXIT_FAILURE);
2815f2d063e6SXuzhou Cheng     qtest_wait_qemu(to);
2816f2d063e6SXuzhou Cheng 
281719da6edfSDaniel P. Berrangé     args = (MigrateStart){
281819da6edfSDaniel P. Berrangé         .only_target = true,
281919da6edfSDaniel P. Berrangé     };
2820d795f474SJuan Quintela 
28213ff57401SPeter Xu     if (test_migrate_start(&from, &to2, "defer", &args)) {
2822d795f474SJuan Quintela         return;
2823d795f474SJuan Quintela     }
2824d795f474SJuan Quintela 
2825d795f474SJuan Quintela     migrate_set_parameter_int(to2, "multifd-channels", 16);
2826d795f474SJuan Quintela 
2827a1209bb7SDr. David Alan Gilbert     migrate_set_capability(to2, "multifd", true);
2828d795f474SJuan Quintela 
2829d795f474SJuan Quintela     /* Start incoming migration from the 1st socket */
28306830e53bSFabiano Rosas     migrate_incoming_qmp(to2, "tcp:127.0.0.1:0", "{}");
2831d795f474SJuan Quintela 
2832e022d473SPan Nengyuan     g_free(uri);
2833d795f474SJuan Quintela     uri = migrate_get_socket_address(to2, "socket-address");
2834d795f474SJuan Quintela 
2835d795f474SJuan Quintela     wait_for_migration_status(from, "cancelled", NULL);
2836d795f474SJuan Quintela 
2837e02f56e3SDaniel P. Berrangé     migrate_ensure_non_converge(from);
2838d795f474SJuan Quintela 
2839d795f474SJuan Quintela     migrate_qmp(from, uri, "{}");
2840d795f474SJuan Quintela 
284194aaf6d8SFabiano Rosas     migrate_wait_for_dirty_mem(from, to2);
2842e02f56e3SDaniel P. Berrangé 
2843e02f56e3SDaniel P. Berrangé     migrate_ensure_converge(from);
2844d795f474SJuan Quintela 
284595014994SDaniel P. Berrangé     if (!got_src_stop) {
2846d795f474SJuan Quintela         qtest_qmp_eventwait(from, "STOP");
2847d795f474SJuan Quintela     }
2848d795f474SJuan Quintela     qtest_qmp_eventwait(to2, "RESUME");
2849d795f474SJuan Quintela 
2850d795f474SJuan Quintela     wait_for_serial("dest_serial");
2851d795f474SJuan Quintela     wait_for_migration_complete(from);
2852d795f474SJuan Quintela     test_migrate_end(from, to2, true);
2853d795f474SJuan Quintela }
2854d795f474SJuan Quintela 
calc_dirty_rate(QTestState * who,uint64_t calc_time)28558aff6f50SHyman Huang(黄勇) static void calc_dirty_rate(QTestState *who, uint64_t calc_time)
28568aff6f50SHyman Huang(黄勇) {
2857ffd47275SDaniel P. Berrangé     qtest_qmp_assert_success(who,
28588aff6f50SHyman Huang(黄勇)                              "{ 'execute': 'calc-dirty-rate',"
28598aff6f50SHyman Huang(黄勇)                              "'arguments': { "
2860d46e6bbaSStefan Weil                              "'calc-time': %" PRIu64 ","
28618aff6f50SHyman Huang(黄勇)                              "'mode': 'dirty-ring' }}",
2862ffd47275SDaniel P. Berrangé                              calc_time);
28638aff6f50SHyman Huang(黄勇) }
28648aff6f50SHyman Huang(黄勇) 
query_dirty_rate(QTestState * who)28658aff6f50SHyman Huang(黄勇) static QDict *query_dirty_rate(QTestState *who)
28668aff6f50SHyman Huang(黄勇) {
2867ffd47275SDaniel P. Berrangé     return qtest_qmp_assert_success_ref(who,
2868ffd47275SDaniel P. Berrangé                                         "{ 'execute': 'query-dirty-rate' }");
28698aff6f50SHyman Huang(黄勇) }
28708aff6f50SHyman Huang(黄勇) 
dirtylimit_set_all(QTestState * who,uint64_t dirtyrate)28718aff6f50SHyman Huang(黄勇) static void dirtylimit_set_all(QTestState *who, uint64_t dirtyrate)
28728aff6f50SHyman Huang(黄勇) {
2873ffd47275SDaniel P. Berrangé     qtest_qmp_assert_success(who,
28748aff6f50SHyman Huang(黄勇)                              "{ 'execute': 'set-vcpu-dirty-limit',"
28758aff6f50SHyman Huang(黄勇)                              "'arguments': { "
2876d46e6bbaSStefan Weil                              "'dirty-rate': %" PRIu64 " } }",
2877ffd47275SDaniel P. Berrangé                              dirtyrate);
28788aff6f50SHyman Huang(黄勇) }
28798aff6f50SHyman Huang(黄勇) 
cancel_vcpu_dirty_limit(QTestState * who)28808aff6f50SHyman Huang(黄勇) static void cancel_vcpu_dirty_limit(QTestState *who)
28818aff6f50SHyman Huang(黄勇) {
2882ffd47275SDaniel P. Berrangé     qtest_qmp_assert_success(who,
2883ffd47275SDaniel P. Berrangé                              "{ 'execute': 'cancel-vcpu-dirty-limit' }");
28848aff6f50SHyman Huang(黄勇) }
28858aff6f50SHyman Huang(黄勇) 
query_vcpu_dirty_limit(QTestState * who)28868aff6f50SHyman Huang(黄勇) static QDict *query_vcpu_dirty_limit(QTestState *who)
28878aff6f50SHyman Huang(黄勇) {
28888aff6f50SHyman Huang(黄勇)     QDict *rsp;
28898aff6f50SHyman Huang(黄勇) 
28908aff6f50SHyman Huang(黄勇)     rsp = qtest_qmp(who, "{ 'execute': 'query-vcpu-dirty-limit' }");
28918aff6f50SHyman Huang(黄勇)     g_assert(!qdict_haskey(rsp, "error"));
28928aff6f50SHyman Huang(黄勇)     g_assert(qdict_haskey(rsp, "return"));
28938aff6f50SHyman Huang(黄勇) 
28948aff6f50SHyman Huang(黄勇)     return rsp;
28958aff6f50SHyman Huang(黄勇) }
28968aff6f50SHyman Huang(黄勇) 
calc_dirtyrate_ready(QTestState * who)28978aff6f50SHyman Huang(黄勇) static bool calc_dirtyrate_ready(QTestState *who)
28988aff6f50SHyman Huang(黄勇) {
28998aff6f50SHyman Huang(黄勇)     QDict *rsp_return;
29008aff6f50SHyman Huang(黄勇)     gchar *status;
29018aff6f50SHyman Huang(黄勇) 
29028aff6f50SHyman Huang(黄勇)     rsp_return = query_dirty_rate(who);
29038aff6f50SHyman Huang(黄勇)     g_assert(rsp_return);
29048aff6f50SHyman Huang(黄勇) 
29058aff6f50SHyman Huang(黄勇)     status = g_strdup(qdict_get_str(rsp_return, "status"));
29068aff6f50SHyman Huang(黄勇)     g_assert(status);
29078aff6f50SHyman Huang(黄勇) 
29088aff6f50SHyman Huang(黄勇)     return g_strcmp0(status, "measuring");
29098aff6f50SHyman Huang(黄勇) }
29108aff6f50SHyman Huang(黄勇) 
wait_for_calc_dirtyrate_complete(QTestState * who,int64_t time_s)29118aff6f50SHyman Huang(黄勇) static void wait_for_calc_dirtyrate_complete(QTestState *who,
29128aff6f50SHyman Huang(黄勇)                                              int64_t time_s)
29138aff6f50SHyman Huang(黄勇) {
29148aff6f50SHyman Huang(黄勇)     int max_try_count = 10000;
29158aff6f50SHyman Huang(黄勇)     usleep(time_s * 1000000);
29168aff6f50SHyman Huang(黄勇) 
29178aff6f50SHyman Huang(黄勇)     while (!calc_dirtyrate_ready(who) && max_try_count--) {
29188aff6f50SHyman Huang(黄勇)         usleep(1000);
29198aff6f50SHyman Huang(黄勇)     }
29208aff6f50SHyman Huang(黄勇) 
29218aff6f50SHyman Huang(黄勇)     /*
29228aff6f50SHyman Huang(黄勇)      * Set the timeout with 10 s(max_try_count * 1000us),
29238aff6f50SHyman Huang(黄勇)      * if dirtyrate measurement not complete, fail test.
29248aff6f50SHyman Huang(黄勇)      */
29258aff6f50SHyman Huang(黄勇)     g_assert_cmpint(max_try_count, !=, 0);
29268aff6f50SHyman Huang(黄勇) }
29278aff6f50SHyman Huang(黄勇) 
get_dirty_rate(QTestState * who)29288aff6f50SHyman Huang(黄勇) static int64_t get_dirty_rate(QTestState *who)
29298aff6f50SHyman Huang(黄勇) {
29308aff6f50SHyman Huang(黄勇)     QDict *rsp_return;
29318aff6f50SHyman Huang(黄勇)     gchar *status;
29328aff6f50SHyman Huang(黄勇)     QList *rates;
29338aff6f50SHyman Huang(黄勇)     const QListEntry *entry;
29348aff6f50SHyman Huang(黄勇)     QDict *rate;
29358aff6f50SHyman Huang(黄勇)     int64_t dirtyrate;
29368aff6f50SHyman Huang(黄勇) 
29378aff6f50SHyman Huang(黄勇)     rsp_return = query_dirty_rate(who);
29388aff6f50SHyman Huang(黄勇)     g_assert(rsp_return);
29398aff6f50SHyman Huang(黄勇) 
29408aff6f50SHyman Huang(黄勇)     status = g_strdup(qdict_get_str(rsp_return, "status"));
29418aff6f50SHyman Huang(黄勇)     g_assert(status);
29428aff6f50SHyman Huang(黄勇)     g_assert_cmpstr(status, ==, "measured");
29438aff6f50SHyman Huang(黄勇) 
29448aff6f50SHyman Huang(黄勇)     rates = qdict_get_qlist(rsp_return, "vcpu-dirty-rate");
29458aff6f50SHyman Huang(黄勇)     g_assert(rates && !qlist_empty(rates));
29468aff6f50SHyman Huang(黄勇) 
29478aff6f50SHyman Huang(黄勇)     entry = qlist_first(rates);
29488aff6f50SHyman Huang(黄勇)     g_assert(entry);
29498aff6f50SHyman Huang(黄勇) 
29508aff6f50SHyman Huang(黄勇)     rate = qobject_to(QDict, qlist_entry_obj(entry));
29518aff6f50SHyman Huang(黄勇)     g_assert(rate);
29528aff6f50SHyman Huang(黄勇) 
29538aff6f50SHyman Huang(黄勇)     dirtyrate = qdict_get_try_int(rate, "dirty-rate", -1);
29548aff6f50SHyman Huang(黄勇) 
29558aff6f50SHyman Huang(黄勇)     qobject_unref(rsp_return);
29568aff6f50SHyman Huang(黄勇)     return dirtyrate;
29578aff6f50SHyman Huang(黄勇) }
29588aff6f50SHyman Huang(黄勇) 
get_limit_rate(QTestState * who)29598aff6f50SHyman Huang(黄勇) static int64_t get_limit_rate(QTestState *who)
29608aff6f50SHyman Huang(黄勇) {
29618aff6f50SHyman Huang(黄勇)     QDict *rsp_return;
29628aff6f50SHyman Huang(黄勇)     QList *rates;
29638aff6f50SHyman Huang(黄勇)     const QListEntry *entry;
29648aff6f50SHyman Huang(黄勇)     QDict *rate;
29658aff6f50SHyman Huang(黄勇)     int64_t dirtyrate;
29668aff6f50SHyman Huang(黄勇) 
29678aff6f50SHyman Huang(黄勇)     rsp_return = query_vcpu_dirty_limit(who);
29688aff6f50SHyman Huang(黄勇)     g_assert(rsp_return);
29698aff6f50SHyman Huang(黄勇) 
29708aff6f50SHyman Huang(黄勇)     rates = qdict_get_qlist(rsp_return, "return");
29718aff6f50SHyman Huang(黄勇)     g_assert(rates && !qlist_empty(rates));
29728aff6f50SHyman Huang(黄勇) 
29738aff6f50SHyman Huang(黄勇)     entry = qlist_first(rates);
29748aff6f50SHyman Huang(黄勇)     g_assert(entry);
29758aff6f50SHyman Huang(黄勇) 
29768aff6f50SHyman Huang(黄勇)     rate = qobject_to(QDict, qlist_entry_obj(entry));
29778aff6f50SHyman Huang(黄勇)     g_assert(rate);
29788aff6f50SHyman Huang(黄勇) 
29798aff6f50SHyman Huang(黄勇)     dirtyrate = qdict_get_try_int(rate, "limit-rate", -1);
29808aff6f50SHyman Huang(黄勇) 
29818aff6f50SHyman Huang(黄勇)     qobject_unref(rsp_return);
29828aff6f50SHyman Huang(黄勇)     return dirtyrate;
29838aff6f50SHyman Huang(黄勇) }
29848aff6f50SHyman Huang(黄勇) 
dirtylimit_start_vm(void)29858aff6f50SHyman Huang(黄勇) static QTestState *dirtylimit_start_vm(void)
29868aff6f50SHyman Huang(黄勇) {
29878aff6f50SHyman Huang(黄勇)     QTestState *vm = NULL;
298822d3c6e1SJuan Quintela     g_autofree gchar *
29898aff6f50SHyman Huang(黄勇)     cmd = g_strdup_printf("-accel kvm,dirty-ring-size=4096 "
29908aff6f50SHyman Huang(黄勇)                           "-name dirtylimit-test,debug-threads=on "
29918aff6f50SHyman Huang(黄勇)                           "-m 150M -smp 1 "
29928aff6f50SHyman Huang(黄勇)                           "-serial file:%s/vm_serial "
29938aff6f50SHyman Huang(黄勇)                           "-drive file=%s,format=raw ",
29948aff6f50SHyman Huang(黄勇)                           tmpfs, bootpath);
29958aff6f50SHyman Huang(黄勇) 
29968aff6f50SHyman Huang(黄勇)     vm = qtest_init(cmd);
29978aff6f50SHyman Huang(黄勇)     return vm;
29988aff6f50SHyman Huang(黄勇) }
29998aff6f50SHyman Huang(黄勇) 
dirtylimit_stop_vm(QTestState * vm)30008aff6f50SHyman Huang(黄勇) static void dirtylimit_stop_vm(QTestState *vm)
30018aff6f50SHyman Huang(黄勇) {
30028aff6f50SHyman Huang(黄勇)     qtest_quit(vm);
30038aff6f50SHyman Huang(黄勇)     cleanup("vm_serial");
30048aff6f50SHyman Huang(黄勇) }
30058aff6f50SHyman Huang(黄勇) 
test_vcpu_dirty_limit(void)30068aff6f50SHyman Huang(黄勇) static void test_vcpu_dirty_limit(void)
30078aff6f50SHyman Huang(黄勇) {
30088aff6f50SHyman Huang(黄勇)     QTestState *vm;
30098aff6f50SHyman Huang(黄勇)     int64_t origin_rate;
30108aff6f50SHyman Huang(黄勇)     int64_t quota_rate;
30118aff6f50SHyman Huang(黄勇)     int64_t rate ;
30128aff6f50SHyman Huang(黄勇)     int max_try_count = 20;
30138aff6f50SHyman Huang(黄勇)     int hit = 0;
30148aff6f50SHyman Huang(黄勇) 
30158aff6f50SHyman Huang(黄勇)     /* Start vm for vcpu dirtylimit test */
30168aff6f50SHyman Huang(黄勇)     vm = dirtylimit_start_vm();
30178aff6f50SHyman Huang(黄勇) 
30188aff6f50SHyman Huang(黄勇)     /* Wait for the first serial output from the vm*/
30198aff6f50SHyman Huang(黄勇)     wait_for_serial("vm_serial");
30208aff6f50SHyman Huang(黄勇) 
30218aff6f50SHyman Huang(黄勇)     /* Do dirtyrate measurement with calc time equals 1s */
30228aff6f50SHyman Huang(黄勇)     calc_dirty_rate(vm, 1);
30238aff6f50SHyman Huang(黄勇) 
30248aff6f50SHyman Huang(黄勇)     /* Sleep calc time and wait for calc dirtyrate complete */
30258aff6f50SHyman Huang(黄勇)     wait_for_calc_dirtyrate_complete(vm, 1);
30268aff6f50SHyman Huang(黄勇) 
30278aff6f50SHyman Huang(黄勇)     /* Query original dirty page rate */
30288aff6f50SHyman Huang(黄勇)     origin_rate = get_dirty_rate(vm);
30298aff6f50SHyman Huang(黄勇) 
30308aff6f50SHyman Huang(黄勇)     /* VM booted from bootsect should dirty memory steadily */
30318aff6f50SHyman Huang(黄勇)     assert(origin_rate != 0);
30328aff6f50SHyman Huang(黄勇) 
30338aff6f50SHyman Huang(黄勇)     /* Setup quota dirty page rate at half of origin */
30348aff6f50SHyman Huang(黄勇)     quota_rate = origin_rate / 2;
30358aff6f50SHyman Huang(黄勇) 
30368aff6f50SHyman Huang(黄勇)     /* Set dirtylimit */
30378aff6f50SHyman Huang(黄勇)     dirtylimit_set_all(vm, quota_rate);
30388aff6f50SHyman Huang(黄勇) 
30398aff6f50SHyman Huang(黄勇)     /*
30408aff6f50SHyman Huang(黄勇)      * Check if set-vcpu-dirty-limit and query-vcpu-dirty-limit
30418aff6f50SHyman Huang(黄勇)      * works literally
30428aff6f50SHyman Huang(黄勇)      */
30438aff6f50SHyman Huang(黄勇)     g_assert_cmpint(quota_rate, ==, get_limit_rate(vm));
30448aff6f50SHyman Huang(黄勇) 
30458aff6f50SHyman Huang(黄勇)     /* Sleep a bit to check if it take effect */
30468aff6f50SHyman Huang(黄勇)     usleep(2000000);
30478aff6f50SHyman Huang(黄勇) 
30488aff6f50SHyman Huang(黄勇)     /*
30498aff6f50SHyman Huang(黄勇)      * Check if dirtylimit take effect realistically, set the
30508aff6f50SHyman Huang(黄勇)      * timeout with 20 s(max_try_count * 1s), if dirtylimit
30518aff6f50SHyman Huang(黄勇)      * doesn't take effect, fail test.
30528aff6f50SHyman Huang(黄勇)      */
30538aff6f50SHyman Huang(黄勇)     while (--max_try_count) {
30548aff6f50SHyman Huang(黄勇)         calc_dirty_rate(vm, 1);
30558aff6f50SHyman Huang(黄勇)         wait_for_calc_dirtyrate_complete(vm, 1);
30568aff6f50SHyman Huang(黄勇)         rate = get_dirty_rate(vm);
30578aff6f50SHyman Huang(黄勇) 
30588aff6f50SHyman Huang(黄勇)         /*
30598aff6f50SHyman Huang(黄勇)          * Assume hitting if current rate is less
30608aff6f50SHyman Huang(黄勇)          * than quota rate (within accepting error)
30618aff6f50SHyman Huang(黄勇)          */
30628aff6f50SHyman Huang(黄勇)         if (rate < (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) {
30638aff6f50SHyman Huang(黄勇)             hit = 1;
30648aff6f50SHyman Huang(黄勇)             break;
30658aff6f50SHyman Huang(黄勇)         }
30668aff6f50SHyman Huang(黄勇)     }
30678aff6f50SHyman Huang(黄勇) 
30688aff6f50SHyman Huang(黄勇)     g_assert_cmpint(hit, ==, 1);
30698aff6f50SHyman Huang(黄勇) 
30708aff6f50SHyman Huang(黄勇)     hit = 0;
30718aff6f50SHyman Huang(黄勇)     max_try_count = 20;
30728aff6f50SHyman Huang(黄勇) 
30738aff6f50SHyman Huang(黄勇)     /* Check if dirtylimit cancellation take effect */
30748aff6f50SHyman Huang(黄勇)     cancel_vcpu_dirty_limit(vm);
30758aff6f50SHyman Huang(黄勇)     while (--max_try_count) {
30768aff6f50SHyman Huang(黄勇)         calc_dirty_rate(vm, 1);
30778aff6f50SHyman Huang(黄勇)         wait_for_calc_dirtyrate_complete(vm, 1);
30788aff6f50SHyman Huang(黄勇)         rate = get_dirty_rate(vm);
30798aff6f50SHyman Huang(黄勇) 
30808aff6f50SHyman Huang(黄勇)         /*
30818aff6f50SHyman Huang(黄勇)          * Assume dirtylimit be canceled if current rate is
30828aff6f50SHyman Huang(黄勇)          * greater than quota rate (within accepting error)
30838aff6f50SHyman Huang(黄勇)          */
30848aff6f50SHyman Huang(黄勇)         if (rate > (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) {
30858aff6f50SHyman Huang(黄勇)             hit = 1;
30868aff6f50SHyman Huang(黄勇)             break;
30878aff6f50SHyman Huang(黄勇)         }
30888aff6f50SHyman Huang(黄勇)     }
30898aff6f50SHyman Huang(黄勇) 
30908aff6f50SHyman Huang(黄勇)     g_assert_cmpint(hit, ==, 1);
30918aff6f50SHyman Huang(黄勇)     dirtylimit_stop_vm(vm);
30928aff6f50SHyman Huang(黄勇) }
30938aff6f50SHyman Huang(黄勇) 
migrate_dirty_limit_wait_showup(QTestState * from,const int64_t period,const int64_t value)309417257b90SHyman Huang static void migrate_dirty_limit_wait_showup(QTestState *from,
309517257b90SHyman Huang                                             const int64_t period,
309617257b90SHyman Huang                                             const int64_t value)
309717257b90SHyman Huang {
309817257b90SHyman Huang     /* Enable dirty limit capability */
309917257b90SHyman Huang     migrate_set_capability(from, "dirty-limit", true);
310017257b90SHyman Huang 
310117257b90SHyman Huang     /* Set dirty limit parameters */
310217257b90SHyman Huang     migrate_set_parameter_int(from, "x-vcpu-dirty-limit-period", period);
310317257b90SHyman Huang     migrate_set_parameter_int(from, "vcpu-dirty-limit", value);
310417257b90SHyman Huang 
310517257b90SHyman Huang     /* Make sure migrate can't converge */
310617257b90SHyman Huang     migrate_ensure_non_converge(from);
310717257b90SHyman Huang 
310817257b90SHyman Huang     /* To check limit rate after precopy */
310917257b90SHyman Huang     migrate_set_capability(from, "pause-before-switchover", true);
311017257b90SHyman Huang 
311117257b90SHyman Huang     /* Wait for the serial output from the source */
311217257b90SHyman Huang     wait_for_serial("src_serial");
311317257b90SHyman Huang }
311417257b90SHyman Huang 
311517257b90SHyman Huang /*
311617257b90SHyman Huang  * This test does:
311717257b90SHyman Huang  *  source                          destination
311817257b90SHyman Huang  *  start vm
311917257b90SHyman Huang  *                                  start incoming vm
312017257b90SHyman Huang  *  migrate
312117257b90SHyman Huang  *  wait dirty limit to begin
312217257b90SHyman Huang  *  cancel migrate
312317257b90SHyman Huang  *  cancellation check
312417257b90SHyman Huang  *                                  restart incoming vm
312517257b90SHyman Huang  *  migrate
312617257b90SHyman Huang  *  wait dirty limit to begin
312717257b90SHyman Huang  *  wait pre-switchover event
312817257b90SHyman Huang  *  convergence condition check
312917257b90SHyman Huang  *
313017257b90SHyman Huang  * And see if dirty limit migration works correctly.
313117257b90SHyman Huang  * This test case involves many passes, so it runs in slow mode only.
313217257b90SHyman Huang  */
test_migrate_dirty_limit(void)313317257b90SHyman Huang static void test_migrate_dirty_limit(void)
313417257b90SHyman Huang {
313517257b90SHyman Huang     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
313617257b90SHyman Huang     QTestState *from, *to;
313717257b90SHyman Huang     int64_t remaining;
313817257b90SHyman Huang     uint64_t throttle_us_per_full;
313917257b90SHyman Huang     /*
314017257b90SHyman Huang      * We want the test to be stable and as fast as possible.
3141*8053feaaSMichael Tokarev      * E.g., with 1Gb/s bandwidth migration may pass without dirty limit,
314217257b90SHyman Huang      * so we need to decrease a bandwidth.
314317257b90SHyman Huang      */
314417257b90SHyman Huang     const int64_t dirtylimit_period = 1000, dirtylimit_value = 50;
314517257b90SHyman Huang     const int64_t max_bandwidth = 400000000; /* ~400Mb/s */
314617257b90SHyman Huang     const int64_t downtime_limit = 250; /* 250ms */
314717257b90SHyman Huang     /*
314817257b90SHyman Huang      * We migrate through unix-socket (> 500Mb/s).
314917257b90SHyman Huang      * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
315017257b90SHyman Huang      * So, we can predict expected_threshold
315117257b90SHyman Huang      */
315217257b90SHyman Huang     const int64_t expected_threshold = max_bandwidth * downtime_limit / 1000;
315317257b90SHyman Huang     int max_try_count = 10;
315417257b90SHyman Huang     MigrateCommon args = {
315517257b90SHyman Huang         .start = {
315617257b90SHyman Huang             .hide_stderr = true,
315717257b90SHyman Huang             .use_dirty_ring = true,
315817257b90SHyman Huang         },
315917257b90SHyman Huang         .listen_uri = uri,
316017257b90SHyman Huang         .connect_uri = uri,
316117257b90SHyman Huang     };
316217257b90SHyman Huang 
316317257b90SHyman Huang     /* Start src, dst vm */
316417257b90SHyman Huang     if (test_migrate_start(&from, &to, args.listen_uri, &args.start)) {
316517257b90SHyman Huang         return;
316617257b90SHyman Huang     }
316717257b90SHyman Huang 
316817257b90SHyman Huang     /* Prepare for dirty limit migration and wait src vm show up */
316917257b90SHyman Huang     migrate_dirty_limit_wait_showup(from, dirtylimit_period, dirtylimit_value);
317017257b90SHyman Huang 
317117257b90SHyman Huang     /* Start migrate */
317217257b90SHyman Huang     migrate_qmp(from, uri, "{}");
317317257b90SHyman Huang 
317417257b90SHyman Huang     /* Wait for dirty limit throttle begin */
317517257b90SHyman Huang     throttle_us_per_full = 0;
317617257b90SHyman Huang     while (throttle_us_per_full == 0) {
317717257b90SHyman Huang         throttle_us_per_full =
317817257b90SHyman Huang         read_migrate_property_int(from, "dirty-limit-throttle-time-per-round");
317917257b90SHyman Huang         usleep(100);
318017257b90SHyman Huang         g_assert_false(got_src_stop);
318117257b90SHyman Huang     }
318217257b90SHyman Huang 
318317257b90SHyman Huang     /* Now cancel migrate and wait for dirty limit throttle switch off */
318417257b90SHyman Huang     migrate_cancel(from);
318517257b90SHyman Huang     wait_for_migration_status(from, "cancelled", NULL);
318617257b90SHyman Huang 
318717257b90SHyman Huang     /* Check if dirty limit throttle switched off, set timeout 1ms */
318817257b90SHyman Huang     do {
318917257b90SHyman Huang         throttle_us_per_full =
319017257b90SHyman Huang         read_migrate_property_int(from, "dirty-limit-throttle-time-per-round");
319117257b90SHyman Huang         usleep(100);
319217257b90SHyman Huang         g_assert_false(got_src_stop);
319317257b90SHyman Huang     } while (throttle_us_per_full != 0 && --max_try_count);
319417257b90SHyman Huang 
319517257b90SHyman Huang     /* Assert dirty limit is not in service */
319617257b90SHyman Huang     g_assert_cmpint(throttle_us_per_full, ==, 0);
319717257b90SHyman Huang 
319817257b90SHyman Huang     args = (MigrateCommon) {
319917257b90SHyman Huang         .start = {
320017257b90SHyman Huang             .only_target = true,
320117257b90SHyman Huang             .use_dirty_ring = true,
320217257b90SHyman Huang         },
320317257b90SHyman Huang         .listen_uri = uri,
320417257b90SHyman Huang         .connect_uri = uri,
320517257b90SHyman Huang     };
320617257b90SHyman Huang 
320717257b90SHyman Huang     /* Restart dst vm, src vm already show up so we needn't wait anymore */
320817257b90SHyman Huang     if (test_migrate_start(&from, &to, args.listen_uri, &args.start)) {
320917257b90SHyman Huang         return;
321017257b90SHyman Huang     }
321117257b90SHyman Huang 
321217257b90SHyman Huang     /* Start migrate */
321317257b90SHyman Huang     migrate_qmp(from, uri, "{}");
321417257b90SHyman Huang 
321517257b90SHyman Huang     /* Wait for dirty limit throttle begin */
321617257b90SHyman Huang     throttle_us_per_full = 0;
321717257b90SHyman Huang     while (throttle_us_per_full == 0) {
321817257b90SHyman Huang         throttle_us_per_full =
321917257b90SHyman Huang         read_migrate_property_int(from, "dirty-limit-throttle-time-per-round");
322017257b90SHyman Huang         usleep(100);
322117257b90SHyman Huang         g_assert_false(got_src_stop);
322217257b90SHyman Huang     }
322317257b90SHyman Huang 
322417257b90SHyman Huang     /*
322517257b90SHyman Huang      * The dirty limit rate should equals the return value of
322617257b90SHyman Huang      * query-vcpu-dirty-limit if dirty limit cap set
322717257b90SHyman Huang      */
322817257b90SHyman Huang     g_assert_cmpint(dirtylimit_value, ==, get_limit_rate(from));
322917257b90SHyman Huang 
323017257b90SHyman Huang     /* Now, we have tested if dirty limit works, let it converge */
323117257b90SHyman Huang     migrate_set_parameter_int(from, "downtime-limit", downtime_limit);
323217257b90SHyman Huang     migrate_set_parameter_int(from, "max-bandwidth", max_bandwidth);
323317257b90SHyman Huang 
323417257b90SHyman Huang     /*
323517257b90SHyman Huang      * Wait for pre-switchover status to check if migration
323617257b90SHyman Huang      * satisfy the convergence condition
323717257b90SHyman Huang      */
323817257b90SHyman Huang     wait_for_migration_status(from, "pre-switchover", NULL);
323917257b90SHyman Huang 
324017257b90SHyman Huang     remaining = read_ram_property_int(from, "remaining");
324117257b90SHyman Huang     g_assert_cmpint(remaining, <,
324217257b90SHyman Huang                     (expected_threshold + expected_threshold / 100));
324317257b90SHyman Huang 
324417257b90SHyman Huang     migrate_continue(from, "pre-switchover");
324517257b90SHyman Huang 
324617257b90SHyman Huang     qtest_qmp_eventwait(to, "RESUME");
324717257b90SHyman Huang 
324817257b90SHyman Huang     wait_for_serial("dest_serial");
324917257b90SHyman Huang     wait_for_migration_complete(from);
325017257b90SHyman Huang 
325117257b90SHyman Huang     test_migrate_end(from, to, true);
325217257b90SHyman Huang }
325317257b90SHyman Huang 
kvm_dirty_ring_supported(void)32541f546b70SPeter Xu static bool kvm_dirty_ring_supported(void)
32551f546b70SPeter Xu {
325661c32485SPeter Xu #if defined(__linux__) && defined(HOST_X86_64)
32571f546b70SPeter Xu     int ret, kvm_fd = open("/dev/kvm", O_RDONLY);
32581f546b70SPeter Xu 
32591f546b70SPeter Xu     if (kvm_fd < 0) {
32601f546b70SPeter Xu         return false;
32611f546b70SPeter Xu     }
32621f546b70SPeter Xu 
32631f546b70SPeter Xu     ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, KVM_CAP_DIRTY_LOG_RING);
32641f546b70SPeter Xu     close(kvm_fd);
32651f546b70SPeter Xu 
32661f546b70SPeter Xu     /* We test with 4096 slots */
32671f546b70SPeter Xu     if (ret < 4096) {
32681f546b70SPeter Xu         return false;
32691f546b70SPeter Xu     }
32701f546b70SPeter Xu 
32711f546b70SPeter Xu     return true;
32721f546b70SPeter Xu #else
32731f546b70SPeter Xu     return false;
32741f546b70SPeter Xu #endif
32751f546b70SPeter Xu }
32761f546b70SPeter Xu 
main(int argc,char ** argv)32771e8a1faeSThomas Huth int main(int argc, char **argv)
32781e8a1faeSThomas Huth {
32790c1ae3ffSFabiano Rosas     bool has_kvm, has_tcg;
3280e35b9a2eSDaniel P. Berrangé     bool has_uffd;
3281e35b9a2eSDaniel P. Berrangé     const char *arch;
3282e5553c1bSBin Meng     g_autoptr(GError) err = NULL;
32835050ad2aSFabiano Rosas     const char *qemu_src = getenv(QEMU_ENV_SRC);
32845050ad2aSFabiano Rosas     const char *qemu_dst = getenv(QEMU_ENV_DST);
32851e8a1faeSThomas Huth     int ret;
32861e8a1faeSThomas Huth 
32871e8a1faeSThomas Huth     g_test_init(&argc, &argv, NULL);
32881e8a1faeSThomas Huth 
32895050ad2aSFabiano Rosas     /*
32905050ad2aSFabiano Rosas      * The default QTEST_QEMU_BINARY must always be provided because
32915050ad2aSFabiano Rosas      * that is what helpers use to query the accel type and
32925050ad2aSFabiano Rosas      * architecture.
32935050ad2aSFabiano Rosas      */
32945050ad2aSFabiano Rosas     if (qemu_src && qemu_dst) {
32955050ad2aSFabiano Rosas         g_test_message("Only one of %s, %s is allowed",
32965050ad2aSFabiano Rosas                        QEMU_ENV_SRC, QEMU_ENV_DST);
32975050ad2aSFabiano Rosas         exit(1);
32985050ad2aSFabiano Rosas     }
32995050ad2aSFabiano Rosas 
3300e35b9a2eSDaniel P. Berrangé     has_kvm = qtest_has_accel("kvm");
33010c1ae3ffSFabiano Rosas     has_tcg = qtest_has_accel("tcg");
33020c1ae3ffSFabiano Rosas 
33030c1ae3ffSFabiano Rosas     if (!has_tcg && !has_kvm) {
33040c1ae3ffSFabiano Rosas         g_test_skip("No KVM or TCG accelerator available");
33050c1ae3ffSFabiano Rosas         return 0;
33060c1ae3ffSFabiano Rosas     }
33070c1ae3ffSFabiano Rosas 
3308e35b9a2eSDaniel P. Berrangé     has_uffd = ufd_version_check();
3309e35b9a2eSDaniel P. Berrangé     arch = qtest_get_arch();
3310e35b9a2eSDaniel P. Berrangé 
33111e8a1faeSThomas Huth     /*
33121e8a1faeSThomas Huth      * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
33131e8a1faeSThomas Huth      * is touchy due to race conditions on dirty bits (especially on PPC for
33141e8a1faeSThomas Huth      * some reason)
33151e8a1faeSThomas Huth      */
33161bca64a3SThomas Huth     if (g_str_equal(arch, "ppc64") &&
3317b72c7603SIgor Mammedov         (!has_kvm || access("/sys/module/kvm_hv", F_OK))) {
33181e8a1faeSThomas Huth         g_test_message("Skipping test: kvm_hv not available");
33191e8a1faeSThomas Huth         return g_test_run();
33201e8a1faeSThomas Huth     }
33211e8a1faeSThomas Huth 
33221e8a1faeSThomas Huth     /*
33231e8a1faeSThomas Huth      * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
33241e8a1faeSThomas Huth      * there until the problems are resolved
33251e8a1faeSThomas Huth      */
33261bca64a3SThomas Huth     if (g_str_equal(arch, "s390x") && !has_kvm) {
3327b72c7603SIgor Mammedov         g_test_message("Skipping test: s390x host with KVM is required");
33281e8a1faeSThomas Huth         return g_test_run();
33291e8a1faeSThomas Huth     }
33301e8a1faeSThomas Huth 
3331e5553c1bSBin Meng     tmpfs = g_dir_make_tmp("migration-test-XXXXXX", &err);
33321e8a1faeSThomas Huth     if (!tmpfs) {
3333b1f6208cSBin Meng         g_test_message("Can't create temporary directory in %s: %s",
3334b1f6208cSBin Meng                        g_get_tmp_dir(), err->message);
33351e8a1faeSThomas Huth     }
33361e8a1faeSThomas Huth     g_assert(tmpfs);
33370c690d3eSJuan Quintela     bootfile_create(tmpfs);
33381e8a1faeSThomas Huth 
33391e8a1faeSThomas Huth     module_call_init(MODULE_INIT_QOM);
33401e8a1faeSThomas Huth 
33412649a725SPeter Xu     if (has_uffd) {
33428f6fe915SPeter Xu         qtest_add_func("/migration/postcopy/plain", test_postcopy);
3343767fa9cfSPeter Xu         qtest_add_func("/migration/postcopy/recovery/plain",
3344767fa9cfSPeter Xu                        test_postcopy_recovery);
33458f6fe915SPeter Xu         qtest_add_func("/migration/postcopy/preempt/plain", test_postcopy_preempt);
33468f6fe915SPeter Xu         qtest_add_func("/migration/postcopy/preempt/recovery/plain",
33478f6fe915SPeter Xu                        test_postcopy_preempt_recovery);
3348dc066da8SLukas Straub         if (getenv("QEMU_TEST_FLAKY_TESTS")) {
3349dc066da8SLukas Straub             qtest_add_func("/migration/postcopy/compress/plain",
3350dc066da8SLukas Straub                            test_postcopy_compress);
3351dc066da8SLukas Straub             qtest_add_func("/migration/postcopy/recovery/compress/plain",
3352dc066da8SLukas Straub                            test_postcopy_recovery_compress);
3353dc066da8SLukas Straub         }
33547bca2bb7SFabiano Rosas #ifndef _WIN32
33557bca2bb7SFabiano Rosas         qtest_add_func("/migration/postcopy/recovery/double-failures",
33567bca2bb7SFabiano Rosas                        test_postcopy_recovery_double_fail);
33577bca2bb7SFabiano Rosas #endif /* _WIN32 */
33587bca2bb7SFabiano Rosas 
33592649a725SPeter Xu     }
3360d1a27b16SPeter Xu 
33611e8a1faeSThomas Huth     qtest_add_func("/migration/bad_dest", test_baddest);
3362d864756eSFabiano Rosas #ifndef _WIN32
336304131e00SThomas Huth     if (!g_str_equal(arch, "s390x")) {
3364d864756eSFabiano Rosas         qtest_add_func("/migration/analyze-script", test_analyze_script);
336504131e00SThomas Huth     }
3366d864756eSFabiano Rosas #endif
336758d25e97SDaniel P. Berrangé     qtest_add_func("/migration/precopy/unix/plain", test_precopy_unix_plain);
336883bcba1eSDaniel P. Berrangé     qtest_add_func("/migration/precopy/unix/xbzrle", test_precopy_unix_xbzrle);
33691536d1daSLukas Straub     /*
33701536d1daSLukas Straub      * Compression fails from time to time.
33711536d1daSLukas Straub      * Put test here but don't enable it until everything is fixed.
33721536d1daSLukas Straub      */
33731536d1daSLukas Straub     if (getenv("QEMU_TEST_FLAKY_TESTS")) {
33741536d1daSLukas Straub         qtest_add_func("/migration/precopy/unix/compress/wait",
33751536d1daSLukas Straub                        test_precopy_unix_compress);
33761536d1daSLukas Straub         qtest_add_func("/migration/precopy/unix/compress/nowait",
33771536d1daSLukas Straub                        test_precopy_unix_compress_nowait);
33781536d1daSLukas Straub     }
33793dc35470SFabiano Rosas 
33803dc35470SFabiano Rosas     qtest_add_func("/migration/precopy/file",
33813dc35470SFabiano Rosas                    test_precopy_file);
33823dc35470SFabiano Rosas     qtest_add_func("/migration/precopy/file/offset",
33833dc35470SFabiano Rosas                    test_precopy_file_offset);
33843dc35470SFabiano Rosas     qtest_add_func("/migration/precopy/file/offset/bad",
33853dc35470SFabiano Rosas                    test_precopy_file_offset_bad);
33863dc35470SFabiano Rosas 
3387e7b428d6SSteve Sistare     /*
3388e7b428d6SSteve Sistare      * Our CI system has problems with shared memory.
3389e7b428d6SSteve Sistare      * Don't run this test until we find a workaround.
3390e7b428d6SSteve Sistare      */
3391e7b428d6SSteve Sistare     if (getenv("QEMU_TEST_FLAKY_TESTS")) {
3392e7b428d6SSteve Sistare         qtest_add_func("/migration/mode/reboot", test_mode_reboot);
3393e7b428d6SSteve Sistare     }
3394e7b428d6SSteve Sistare 
339558d25e97SDaniel P. Berrangé #ifdef CONFIG_GNUTLS
339658d25e97SDaniel P. Berrangé     qtest_add_func("/migration/precopy/unix/tls/psk",
339758d25e97SDaniel P. Berrangé                    test_precopy_unix_tls_psk);
33982649a725SPeter Xu 
33992649a725SPeter Xu     if (has_uffd) {
3400d1a27b16SPeter Xu         /*
3401d1a27b16SPeter Xu          * NOTE: psk test is enough for postcopy, as other types of TLS
3402d1a27b16SPeter Xu          * channels are tested under precopy.  Here what we want to test is the
3403d1a27b16SPeter Xu          * general postcopy path that has TLS channel enabled.
3404d1a27b16SPeter Xu          */
3405d1a27b16SPeter Xu         qtest_add_func("/migration/postcopy/tls/psk", test_postcopy_tls_psk);
3406767fa9cfSPeter Xu         qtest_add_func("/migration/postcopy/recovery/tls/psk",
3407767fa9cfSPeter Xu                        test_postcopy_recovery_tls_psk);
34088f6fe915SPeter Xu         qtest_add_func("/migration/postcopy/preempt/tls/psk",
34098f6fe915SPeter Xu                        test_postcopy_preempt_tls_psk);
34108f6fe915SPeter Xu         qtest_add_func("/migration/postcopy/preempt/recovery/tls/psk",
34118f6fe915SPeter Xu                        test_postcopy_preempt_all);
34122649a725SPeter Xu     }
3413d47b83b1SDaniel P. Berrangé #ifdef CONFIG_TASN1
3414d47b83b1SDaniel P. Berrangé     qtest_add_func("/migration/precopy/unix/tls/x509/default-host",
3415d47b83b1SDaniel P. Berrangé                    test_precopy_unix_tls_x509_default_host);
3416d47b83b1SDaniel P. Berrangé     qtest_add_func("/migration/precopy/unix/tls/x509/override-host",
3417d47b83b1SDaniel P. Berrangé                    test_precopy_unix_tls_x509_override_host);
3418d47b83b1SDaniel P. Berrangé #endif /* CONFIG_TASN1 */
341958d25e97SDaniel P. Berrangé #endif /* CONFIG_GNUTLS */
342058d25e97SDaniel P. Berrangé 
342158d25e97SDaniel P. Berrangé     qtest_add_func("/migration/precopy/tcp/plain", test_precopy_tcp_plain);
34227e6a5c73SAvihai Horon 
34237e6a5c73SAvihai Horon     qtest_add_func("/migration/precopy/tcp/plain/switchover-ack",
34247e6a5c73SAvihai Horon                    test_precopy_tcp_switchover_ack);
34257e6a5c73SAvihai Horon 
342658d25e97SDaniel P. Berrangé #ifdef CONFIG_GNUTLS
342758d25e97SDaniel P. Berrangé     qtest_add_func("/migration/precopy/tcp/tls/psk/match",
342858d25e97SDaniel P. Berrangé                    test_precopy_tcp_tls_psk_match);
342958d25e97SDaniel P. Berrangé     qtest_add_func("/migration/precopy/tcp/tls/psk/mismatch",
343058d25e97SDaniel P. Berrangé                    test_precopy_tcp_tls_psk_mismatch);
3431d47b83b1SDaniel P. Berrangé #ifdef CONFIG_TASN1
3432d47b83b1SDaniel P. Berrangé     qtest_add_func("/migration/precopy/tcp/tls/x509/default-host",
3433d47b83b1SDaniel P. Berrangé                    test_precopy_tcp_tls_x509_default_host);
3434d47b83b1SDaniel P. Berrangé     qtest_add_func("/migration/precopy/tcp/tls/x509/override-host",
3435d47b83b1SDaniel P. Berrangé                    test_precopy_tcp_tls_x509_override_host);
3436d47b83b1SDaniel P. Berrangé     qtest_add_func("/migration/precopy/tcp/tls/x509/mismatch-host",
3437d47b83b1SDaniel P. Berrangé                    test_precopy_tcp_tls_x509_mismatch_host);
3438d47b83b1SDaniel P. Berrangé     qtest_add_func("/migration/precopy/tcp/tls/x509/friendly-client",
3439d47b83b1SDaniel P. Berrangé                    test_precopy_tcp_tls_x509_friendly_client);
3440d47b83b1SDaniel P. Berrangé     qtest_add_func("/migration/precopy/tcp/tls/x509/hostile-client",
3441d47b83b1SDaniel P. Berrangé                    test_precopy_tcp_tls_x509_hostile_client);
3442d47b83b1SDaniel P. Berrangé     qtest_add_func("/migration/precopy/tcp/tls/x509/allow-anon-client",
3443d47b83b1SDaniel P. Berrangé                    test_precopy_tcp_tls_x509_allow_anon_client);
3444d47b83b1SDaniel P. Berrangé     qtest_add_func("/migration/precopy/tcp/tls/x509/reject-anon-client",
3445d47b83b1SDaniel P. Berrangé                    test_precopy_tcp_tls_x509_reject_anon_client);
3446d47b83b1SDaniel P. Berrangé #endif /* CONFIG_TASN1 */
344758d25e97SDaniel P. Berrangé #endif /* CONFIG_GNUTLS */
344858d25e97SDaniel P. Berrangé 
34491e8a1faeSThomas Huth     /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
3450d7613ee2SBin Meng #ifndef _WIN32
34511e8a1faeSThomas Huth     qtest_add_func("/migration/fd_proto", test_migrate_fd_proto);
3452d7613ee2SBin Meng #endif
34531e8a1faeSThomas Huth     qtest_add_func("/migration/validate_uuid", test_validate_uuid);
34541e8a1faeSThomas Huth     qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error);
34551e8a1faeSThomas Huth     qtest_add_func("/migration/validate_uuid_src_not_set",
34561e8a1faeSThomas Huth                    test_validate_uuid_src_not_set);
34571e8a1faeSThomas Huth     qtest_add_func("/migration/validate_uuid_dst_not_set",
34581e8a1faeSThomas Huth                    test_validate_uuid_dst_not_set);
345974902af7SJuan Quintela     /*
346074902af7SJuan Quintela      * See explanation why this test is slow on function definition
346174902af7SJuan Quintela      */
346274902af7SJuan Quintela     if (g_test_slow()) {
34631e8a1faeSThomas Huth         qtest_add_func("/migration/auto_converge", test_migrate_auto_converge);
346417257b90SHyman Huang         if (g_str_equal(arch, "x86_64") &&
346517257b90SHyman Huang             has_kvm && kvm_dirty_ring_supported()) {
346617257b90SHyman Huang             qtest_add_func("/migration/dirty_limit", test_migrate_dirty_limit);
346717257b90SHyman Huang         }
346874902af7SJuan Quintela     }
34694d6d2e87SDaniel P. Berrangé     qtest_add_func("/migration/multifd/tcp/plain/none",
34704d6d2e87SDaniel P. Berrangé                    test_multifd_tcp_none);
3471c15d9e23SPeter Maydell     /*
3472c15d9e23SPeter Maydell      * This test is flaky and sometimes fails in CI and otherwise:
3473c15d9e23SPeter Maydell      * don't run unless user opts in via environment variable.
3474c15d9e23SPeter Maydell      */
3475c15d9e23SPeter Maydell     if (getenv("QEMU_TEST_FLAKY_TESTS")) {
34764d6d2e87SDaniel P. Berrangé         qtest_add_func("/migration/multifd/tcp/plain/cancel",
34774d6d2e87SDaniel P. Berrangé                        test_multifd_tcp_cancel);
3478c15d9e23SPeter Maydell     }
34794d6d2e87SDaniel P. Berrangé     qtest_add_func("/migration/multifd/tcp/plain/zlib",
34804d6d2e87SDaniel P. Berrangé                    test_multifd_tcp_zlib);
348187dc6f5fSJuan Quintela #ifdef CONFIG_ZSTD
34824d6d2e87SDaniel P. Berrangé     qtest_add_func("/migration/multifd/tcp/plain/zstd",
34834d6d2e87SDaniel P. Berrangé                    test_multifd_tcp_zstd);
348487dc6f5fSJuan Quintela #endif
34854d6d2e87SDaniel P. Berrangé #ifdef CONFIG_GNUTLS
34864d6d2e87SDaniel P. Berrangé     qtest_add_func("/migration/multifd/tcp/tls/psk/match",
34874d6d2e87SDaniel P. Berrangé                    test_multifd_tcp_tls_psk_match);
34884d6d2e87SDaniel P. Berrangé     qtest_add_func("/migration/multifd/tcp/tls/psk/mismatch",
34894d6d2e87SDaniel P. Berrangé                    test_multifd_tcp_tls_psk_mismatch);
3490ff32f1ddSDaniel P. Berrangé #ifdef CONFIG_TASN1
3491ff32f1ddSDaniel P. Berrangé     qtest_add_func("/migration/multifd/tcp/tls/x509/default-host",
3492ff32f1ddSDaniel P. Berrangé                    test_multifd_tcp_tls_x509_default_host);
3493ff32f1ddSDaniel P. Berrangé     qtest_add_func("/migration/multifd/tcp/tls/x509/override-host",
3494ff32f1ddSDaniel P. Berrangé                    test_multifd_tcp_tls_x509_override_host);
3495ff32f1ddSDaniel P. Berrangé     qtest_add_func("/migration/multifd/tcp/tls/x509/mismatch-host",
3496ff32f1ddSDaniel P. Berrangé                    test_multifd_tcp_tls_x509_mismatch_host);
3497ff32f1ddSDaniel P. Berrangé     qtest_add_func("/migration/multifd/tcp/tls/x509/allow-anon-client",
3498ff32f1ddSDaniel P. Berrangé                    test_multifd_tcp_tls_x509_allow_anon_client);
3499ff32f1ddSDaniel P. Berrangé     qtest_add_func("/migration/multifd/tcp/tls/x509/reject-anon-client",
3500ff32f1ddSDaniel P. Berrangé                    test_multifd_tcp_tls_x509_reject_anon_client);
3501ff32f1ddSDaniel P. Berrangé #endif /* CONFIG_TASN1 */
35024d6d2e87SDaniel P. Berrangé #endif /* CONFIG_GNUTLS */
35031e8a1faeSThomas Huth 
35041bca64a3SThomas Huth     if (g_str_equal(arch, "x86_64") && has_kvm && kvm_dirty_ring_supported()) {
35051f546b70SPeter Xu         qtest_add_func("/migration/dirty_ring",
35061f546b70SPeter Xu                        test_precopy_unix_dirty_ring);
35078aff6f50SHyman Huang(黄勇)         qtest_add_func("/migration/vcpu_dirty_limit",
35088aff6f50SHyman Huang(黄勇)                        test_vcpu_dirty_limit);
35091f546b70SPeter Xu     }
35101f546b70SPeter Xu 
35111e8a1faeSThomas Huth     ret = g_test_run();
35121e8a1faeSThomas Huth 
35131e8a1faeSThomas Huth     g_assert_cmpint(ret, ==, 0);
35141e8a1faeSThomas Huth 
35150c690d3eSJuan Quintela     bootfile_delete();
35161e8a1faeSThomas Huth     ret = rmdir(tmpfs);
35171e8a1faeSThomas Huth     if (ret != 0) {
35181e8a1faeSThomas Huth         g_test_message("unable to rmdir: path (%s): %s",
35191e8a1faeSThomas Huth                        tmpfs, strerror(errno));
35201e8a1faeSThomas Huth     }
3521e5553c1bSBin Meng     g_free(tmpfs);
35221e8a1faeSThomas Huth 
35231e8a1faeSThomas Huth     return ret;
35241e8a1faeSThomas Huth }
3525