1 /*
2 * QTest testcases for CPR
3 *
4 * Copyright (c) 2016-2018 Red Hat, Inc. and/or its affiliates
5 * based on the vhost-user-test.c that is:
6 * Copyright (c) 2014 Virtual Open Systems Sarl.
7 *
8 * This work is licensed under the terms of the GNU GPL, version 2 or later.
9 * See the COPYING file in the top-level directory.
10 *
11 */
12
13 #include "qemu/osdep.h"
14 #include "libqtest.h"
15 #include "migration/framework.h"
16 #include "migration/migration-qmp.h"
17 #include "migration/migration-util.h"
18
19
20 static char *tmpfs;
21
migrate_hook_start_mode_reboot(QTestState * from,QTestState * to)22 static void *migrate_hook_start_mode_reboot(QTestState *from, QTestState *to)
23 {
24 migrate_set_parameter_str(from, "mode", "cpr-reboot");
25 migrate_set_parameter_str(to, "mode", "cpr-reboot");
26
27 return NULL;
28 }
29
test_mode_reboot(void)30 static void test_mode_reboot(void)
31 {
32 g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
33 FILE_TEST_FILENAME);
34 MigrateCommon args = {
35 .start.use_shmem = true,
36 .connect_uri = uri,
37 .listen_uri = "defer",
38 .start_hook = migrate_hook_start_mode_reboot,
39 .start = {
40 .caps[MIGRATION_CAPABILITY_X_IGNORE_SHARED] = true,
41 },
42 };
43
44 test_file_common(&args, true);
45 }
46
test_mode_transfer_start(QTestState * from,QTestState * to)47 static void *test_mode_transfer_start(QTestState *from, QTestState *to)
48 {
49 migrate_set_parameter_str(from, "mode", "cpr-transfer");
50 return NULL;
51 }
52
53 /*
54 * cpr-transfer mode cannot use the target monitor prior to starting the
55 * migration, and cannot connect synchronously to the monitor, so defer
56 * the target connection.
57 */
test_mode_transfer_common(bool incoming_defer)58 static void test_mode_transfer_common(bool incoming_defer)
59 {
60 g_autofree char *cpr_path = g_strdup_printf("%s/cpr.sock", tmpfs);
61 g_autofree char *mig_path = g_strdup_printf("%s/migsocket", tmpfs);
62 g_autofree char *uri = g_strdup_printf("unix:%s", mig_path);
63 g_autofree char *opts_target = NULL;
64
65 const char *opts = "-machine aux-ram-share=on -nodefaults";
66 g_autofree const char *cpr_channel = g_strdup_printf(
67 "cpr,addr.transport=socket,addr.type=unix,addr.path=%s",
68 cpr_path);
69
70 g_autofree char *connect_channels = g_strdup_printf(
71 "[ { 'channel-type': 'main',"
72 " 'addr': { 'transport': 'socket',"
73 " 'type': 'unix',"
74 " 'path': '%s' } } ]",
75 mig_path);
76
77 /*
78 * Set up a UNIX domain socket for the CPR channel before
79 * launching the destination VM, to avoid timing issues
80 * during connection setup.
81 */
82 int cpr_sockfd = qtest_socket_server(cpr_path);
83 g_assert(cpr_sockfd >= 0);
84
85 opts_target = g_strdup_printf("-incoming cpr,addr.transport=socket,"
86 "addr.type=fd,addr.str=%d %s",
87 cpr_sockfd, opts);
88 MigrateCommon args = {
89 .start.opts_source = opts,
90 .start.opts_target = opts_target,
91 .start.defer_target_connect = true,
92 .start.memory_backend = "-object memory-backend-memfd,id=pc.ram,size=%s"
93 " -machine memory-backend=pc.ram",
94 .listen_uri = incoming_defer ? "defer" : uri,
95 .connect_channels = connect_channels,
96 .cpr_channel = cpr_channel,
97 .start_hook = test_mode_transfer_start,
98 };
99
100 test_precopy_common(&args);
101 }
102
test_mode_transfer(void)103 static void test_mode_transfer(void)
104 {
105 test_mode_transfer_common(NULL);
106 }
107
test_mode_transfer_defer(void)108 static void test_mode_transfer_defer(void)
109 {
110 test_mode_transfer_common(true);
111 }
112
migration_test_add_cpr(MigrationTestEnv * env)113 void migration_test_add_cpr(MigrationTestEnv *env)
114 {
115 tmpfs = env->tmpfs;
116
117 /* no tests in the smoke set for now */
118
119 if (!env->full_set) {
120 return;
121 }
122
123 /*
124 * Our CI system has problems with shared memory.
125 * Don't run this test until we find a workaround.
126 */
127 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
128 migration_test_add("/migration/mode/reboot", test_mode_reboot);
129 }
130
131 if (env->has_kvm) {
132 migration_test_add("/migration/mode/transfer", test_mode_transfer);
133 migration_test_add("/migration/mode/transfer/defer",
134 test_mode_transfer_defer);
135 }
136 }
137