xref: /qemu/tests/qtest/migration/framework.h (revision 476d6e4c9c4965734d6f47ee299ac9f84440a9b3)
1 /*
2  * Copyright (c) 2016-2018 Red Hat, Inc. and/or its affiliates
3  *   based on the vhost-user-test.c that is:
4  *      Copyright (c) 2014 Virtual Open Systems Sarl.
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2 or later.
7  * See the COPYING file in the top-level directory.
8  *
9  */
10 
11 #ifndef TEST_FRAMEWORK_H
12 #define TEST_FRAMEWORK_H
13 
14 #include "libqtest.h"
15 
16 #define FILE_TEST_FILENAME "migfile"
17 #define FILE_TEST_OFFSET 0x1000
18 #define FILE_TEST_MARKER 'X'
19 
20 typedef struct MigrationTestEnv {
21     bool has_kvm;
22     bool has_tcg;
23     bool has_uffd;
24     bool uffd_feature_thread_id;
25     bool has_dirty_ring;
26     bool is_x86;
27     const char *arch;
28     const char *qemu_src;
29     const char *qemu_dst;
30     char *tmpfs;
31 } MigrationTestEnv;
32 
33 MigrationTestEnv *migration_get_env(void);
34 int migration_env_clean(MigrationTestEnv *env);
35 
36 /*
37  * A hook that runs after the src and dst QEMUs have been
38  * created, but before the migration is started. This can
39  * be used to set migration parameters and capabilities.
40  *
41  * Returns: NULL, or a pointer to opaque state to be
42  *          later passed to the TestMigrateEndHook
43  */
44 typedef void * (*TestMigrateStartHook)(QTestState *from,
45                                        QTestState *to);
46 
47 /*
48  * A hook that runs after the migration has finished,
49  * regardless of whether it succeeded or failed, but
50  * before QEMU has terminated (unless it self-terminated
51  * due to migration error)
52  *
53  * @opaque is a pointer to state previously returned
54  * by the TestMigrateStartHook if any, or NULL.
55  */
56 typedef void (*TestMigrateEndHook)(QTestState *from,
57                                    QTestState *to,
58                                    void *opaque);
59 
60 /*
61  * Our goal is to ensure that we run a single full migration
62  * iteration, and also dirty memory, ensuring that at least
63  * one further iteration is required.
64  *
65  * We can't directly synchronize with the start of a migration
66  * so we have to apply some tricks monitoring memory that is
67  * transferred.
68  *
69  * Initially we set the migration bandwidth to an insanely
70  * low value, with tiny max downtime too. This basically
71  * guarantees migration will never complete.
72  *
73  * This will result in a test that is unacceptably slow though,
74  * so we can't let the entire migration pass run at this speed.
75  * Our intent is to let it run just long enough that we can
76  * prove data prior to the marker has been transferred *AND*
77  * also prove this transferred data is dirty again.
78  *
79  * Before migration starts, we write a 64-bit magic marker
80  * into a fixed location in the src VM RAM.
81  *
82  * Then watch dst memory until the marker appears. This is
83  * proof that start_address -> MAGIC_OFFSET_BASE has been
84  * transferred.
85  *
86  * Finally we go back to the source and read a byte just
87  * before the marker until we see it flip in value. This
88  * is proof that start_address -> MAGIC_OFFSET_BASE
89  * is now dirty again.
90  *
91  * IOW, we're guaranteed at least a 2nd migration pass
92  * at this point.
93  *
94  * We can now let migration run at full speed to finish
95  * the test
96  */
97 typedef struct {
98     /*
99      * QTEST_LOG=1 may override this.  When QTEST_LOG=1, we always dump errors
100      * unconditionally, because it means the user would like to be verbose.
101      */
102     bool hide_stderr;
103     bool use_shmem;
104     /* only launch the target process */
105     bool only_target;
106     /* Use dirty ring if true; dirty logging otherwise */
107     bool use_dirty_ring;
108     const char *opts_source;
109     const char *opts_target;
110     /* suspend the src before migrating to dest. */
111     bool suspend_me;
112     /* enable OOB QMP capability */
113     bool oob;
114     /*
115      * Format string for the main memory backend, containing one %s where the
116      * size is plugged in.  If omitted, "-m %s" is used.
117      */
118     const char *memory_backend;
119 
120     /* Do not connect to target monitor and qtest sockets in qtest_init */
121     bool defer_target_connect;
122 } MigrateStart;
123 
124 typedef enum PostcopyRecoveryFailStage {
125     /*
126      * "no failure" must be 0 as it's the default.  OTOH, real failure
127      * cases must be >0 to make sure they trigger by a "if" test.
128      */
129     POSTCOPY_FAIL_NONE = 0,
130     POSTCOPY_FAIL_CHANNEL_ESTABLISH,
131     POSTCOPY_FAIL_RECOVERY,
132     POSTCOPY_FAIL_MAX
133 } PostcopyRecoveryFailStage;
134 
135 typedef struct {
136     /* Optional: fine tune start parameters */
137     MigrateStart start;
138 
139     /* Required: the URI for the dst QEMU to listen on */
140     const char *listen_uri;
141 
142     /*
143      * Optional: the URI for the src QEMU to connect to
144      * If NULL, then it will query the dst QEMU for its actual
145      * listening address and use that as the connect address.
146      * This allows for dynamically picking a free TCP port.
147      */
148     const char *connect_uri;
149 
150     /*
151      * Optional: JSON-formatted list of src QEMU URIs. If a port is
152      * defined as '0' in any QDict key a value of '0' will be
153      * automatically converted to the correct destination port.
154      */
155     const char *connect_channels;
156 
157     /* Optional: the cpr migration channel, in JSON or dotted keys format */
158     const char *cpr_channel;
159 
160     /* Optional: callback to run at start to set migration parameters */
161     TestMigrateStartHook start_hook;
162     /* Optional: callback to run at finish to cleanup */
163     TestMigrateEndHook end_hook;
164 
165     /*
166      * Optional: normally we expect the migration process to complete.
167      *
168      * There can be a variety of reasons and stages in which failure
169      * can happen during tests.
170      *
171      * If a failure is expected to happen at time of establishing
172      * the connection, then MIG_TEST_FAIL will indicate that the dst
173      * QEMU is expected to stay running and accept future migration
174      * connections.
175      *
176      * If a failure is expected to happen while processing the
177      * migration stream, then MIG_TEST_FAIL_DEST_QUIT_ERR will indicate
178      * that the dst QEMU is expected to quit with non-zero exit status
179      */
180     enum {
181         /* This test should succeed, the default */
182         MIG_TEST_SUCCEED = 0,
183         /* This test should fail, dest qemu should keep alive */
184         MIG_TEST_FAIL,
185         /* This test should fail, dest qemu should fail with abnormal status */
186         MIG_TEST_FAIL_DEST_QUIT_ERR,
187         /* The QMP command for this migration should fail with an error */
188         MIG_TEST_QMP_ERROR,
189     } result;
190 
191     /*
192      * Optional: set number of migration passes to wait for, if live==true.
193      * If zero, then merely wait for a few MB of dirty data
194      */
195     unsigned int iterations;
196 
197     /*
198      * Optional: whether the guest CPUs should be running during a precopy
199      * migration test.  We used to always run with live but it took much
200      * longer so we reduced live tests to only the ones that have solid
201      * reason to be tested live-only.  For each of the new test cases for
202      * precopy please provide justifications to use live explicitly (please
203      * refer to existing ones with live=true), or use live=off by default.
204      */
205     bool live;
206 
207     /* Postcopy specific fields */
208     void *postcopy_data;
209     bool postcopy_preempt;
210     PostcopyRecoveryFailStage postcopy_recovery_fail_stage;
211 } MigrateCommon;
212 
213 void wait_for_serial(const char *side);
214 void migrate_prepare_for_dirty_mem(QTestState *from);
215 void migrate_wait_for_dirty_mem(QTestState *from, QTestState *to);
216 int migrate_start(QTestState **from, QTestState **to, const char *uri,
217                   MigrateStart *args);
218 void migrate_end(QTestState *from, QTestState *to, bool test_dest);
219 
220 void test_postcopy_common(MigrateCommon *args);
221 void test_postcopy_recovery_common(MigrateCommon *args);
222 void test_precopy_common(MigrateCommon *args);
223 void test_file_common(MigrateCommon *args, bool stop_src);
224 void *migrate_hook_start_precopy_tcp_multifd_common(QTestState *from,
225                                                     QTestState *to,
226                                                     const char *method);
227 
228 typedef struct QTestMigrationState QTestMigrationState;
229 QTestMigrationState *get_src(void);
230 
231 #ifdef CONFIG_GNUTLS
232 void migration_test_add_tls(MigrationTestEnv *env);
233 #else
234 static inline void migration_test_add_tls(MigrationTestEnv *env) {};
235 #endif
236 void migration_test_add_compression(MigrationTestEnv *env);
237 void migration_test_add_postcopy(MigrationTestEnv *env);
238 void migration_test_add_file(MigrationTestEnv *env);
239 void migration_test_add_precopy(MigrationTestEnv *env);
240 void migration_test_add_cpr(MigrationTestEnv *env);
241 void migration_test_add_misc(MigrationTestEnv *env);
242 
243 #endif /* TEST_FRAMEWORK_H */
244