]>
Commit | Line | Data |
---|---|---|
ea0c6d62 | 1 | /* |
2656bfd9 | 2 | * QTest testcase for migration |
ea0c6d62 | 3 | * |
17ca7746 | 4 | * Copyright (c) 2016-2018 Red Hat, Inc. and/or its affiliates |
ea0c6d62 DDAG |
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" | |
ea0c6d62 DDAG |
14 | |
15 | #include "libqtest.h" | |
452fcdbc | 16 | #include "qapi/qmp/qdict.h" |
b5bbd3f3 | 17 | #include "qapi/qmp/qjson.h" |
ea0c6d62 DDAG |
18 | #include "qemu/option.h" |
19 | #include "qemu/range.h" | |
a9c94277 | 20 | #include "qemu/sockets.h" |
8228e353 | 21 | #include "chardev/char.h" |
ea0c6d62 DDAG |
22 | #include "sysemu/sysemu.h" |
23 | ||
e51e711b WH |
24 | #include "migration/migration-test.h" |
25 | ||
055a1efc MA |
26 | /* TODO actually test the results and get rid of this */ |
27 | #define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__)) | |
28 | ||
e51e711b WH |
29 | unsigned start_address; |
30 | unsigned end_address; | |
ea0c6d62 | 31 | bool got_stop; |
346f3dab | 32 | static bool uffd_feature_thread_id; |
ea0c6d62 DDAG |
33 | |
34 | #if defined(__linux__) | |
ea0c6d62 DDAG |
35 | #include <sys/syscall.h> |
36 | #include <sys/vfs.h> | |
37 | #endif | |
38 | ||
39 | #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD) | |
40 | #include <sys/eventfd.h> | |
41 | #include <sys/ioctl.h> | |
42 | #include <linux/userfaultfd.h> | |
43 | ||
44 | static bool ufd_version_check(void) | |
45 | { | |
46 | struct uffdio_api api_struct; | |
47 | uint64_t ioctl_mask; | |
48 | ||
e1ae9fb6 | 49 | int ufd = syscall(__NR_userfaultfd, O_CLOEXEC); |
ea0c6d62 DDAG |
50 | |
51 | if (ufd == -1) { | |
52 | g_test_message("Skipping test: userfaultfd not available"); | |
53 | return false; | |
54 | } | |
55 | ||
56 | api_struct.api = UFFD_API; | |
57 | api_struct.features = 0; | |
58 | if (ioctl(ufd, UFFDIO_API, &api_struct)) { | |
59 | g_test_message("Skipping test: UFFDIO_API failed"); | |
60 | return false; | |
61 | } | |
346f3dab | 62 | uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID; |
ea0c6d62 DDAG |
63 | |
64 | ioctl_mask = (__u64)1 << _UFFDIO_REGISTER | | |
65 | (__u64)1 << _UFFDIO_UNREGISTER; | |
66 | if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) { | |
67 | g_test_message("Skipping test: Missing userfault feature"); | |
68 | return false; | |
69 | } | |
70 | ||
71 | return true; | |
72 | } | |
73 | ||
74 | #else | |
75 | static bool ufd_version_check(void) | |
76 | { | |
77 | g_test_message("Skipping test: Userfault not available (builtdtime)"); | |
78 | return false; | |
79 | } | |
80 | ||
81 | #endif | |
82 | ||
83 | static const char *tmpfs; | |
84 | ||
e51e711b WH |
85 | /* The boot file modifies memory area in [start_address, end_address) |
86 | * repeatedly. It outputs a 'B' at a fixed rate while it's still running. | |
ea0c6d62 | 87 | */ |
d54927ef | 88 | #include "tests/migration/i386/a-b-bootblock.h" |
ea0c6d62 | 89 | |
aaf89c8a | 90 | static void init_bootfile_x86(const char *bootpath) |
91 | { | |
92 | FILE *bootfile = fopen(bootpath, "wb"); | |
93 | ||
17ca7746 | 94 | g_assert_cmpint(fwrite(x86_bootsect, 512, 1, bootfile), ==, 1); |
aaf89c8a | 95 | fclose(bootfile); |
96 | } | |
97 | ||
ea0c6d62 DDAG |
98 | /* |
99 | * Wait for some output in the serial output file, | |
100 | * we get an 'A' followed by an endless string of 'B's | |
101 | * but on the destination we won't have the A. | |
102 | */ | |
103 | static void wait_for_serial(const char *side) | |
104 | { | |
105 | char *serialpath = g_strdup_printf("%s/%s", tmpfs, side); | |
106 | FILE *serialfile = fopen(serialpath, "r"); | |
aaf89c8a | 107 | const char *arch = qtest_get_arch(); |
108 | int started = (strcmp(side, "src_serial") == 0 && | |
109 | strcmp(arch, "ppc64") == 0) ? 0 : 1; | |
ea0c6d62 | 110 | |
e2dd21e5 | 111 | g_free(serialpath); |
ea0c6d62 DDAG |
112 | do { |
113 | int readvalue = fgetc(serialfile); | |
114 | ||
aaf89c8a | 115 | if (!started) { |
116 | /* SLOF prints its banner before starting test, | |
117 | * to ignore it, mark the start of the test with '_', | |
118 | * ignore all characters until this marker | |
119 | */ | |
120 | switch (readvalue) { | |
121 | case '_': | |
122 | started = 1; | |
123 | break; | |
124 | case EOF: | |
125 | fseek(serialfile, 0, SEEK_SET); | |
126 | usleep(1000); | |
127 | break; | |
128 | } | |
129 | continue; | |
130 | } | |
ea0c6d62 DDAG |
131 | switch (readvalue) { |
132 | case 'A': | |
133 | /* Fine */ | |
134 | break; | |
135 | ||
136 | case 'B': | |
137 | /* It's alive! */ | |
138 | fclose(serialfile); | |
ea0c6d62 DDAG |
139 | return; |
140 | ||
141 | case EOF: | |
aaf89c8a | 142 | started = (strcmp(side, "src_serial") == 0 && |
143 | strcmp(arch, "ppc64") == 0) ? 0 : 1; | |
ea0c6d62 DDAG |
144 | fseek(serialfile, 0, SEEK_SET); |
145 | usleep(1000); | |
146 | break; | |
147 | ||
148 | default: | |
149 | fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side); | |
150 | g_assert_not_reached(); | |
151 | } | |
152 | } while (true); | |
153 | } | |
154 | ||
3cd46d42 MA |
155 | static void stop_cb(void *opaque, const char *name, QDict *data) |
156 | { | |
157 | if (!strcmp(name, "STOP")) { | |
158 | got_stop = true; | |
159 | } | |
160 | } | |
161 | ||
ea0c6d62 DDAG |
162 | /* |
163 | * Events can get in the way of responses we are actually waiting for. | |
164 | */ | |
b7281c69 | 165 | GCC_FMT_ATTR(2, 3) |
4399596b | 166 | static QDict *wait_command(QTestState *who, const char *command, ...) |
ea0c6d62 | 167 | { |
4399596b MA |
168 | va_list ap; |
169 | ||
170 | va_start(ap, command); | |
171 | qtest_qmp_vsend(who, command, ap); | |
172 | va_end(ap); | |
173 | ||
3cd46d42 | 174 | return qtest_qmp_receive_success(who, stop_cb, NULL); |
ea0c6d62 DDAG |
175 | } |
176 | ||
2f7074c6 PX |
177 | /* |
178 | * Note: caller is responsible to free the returned object via | |
179 | * qobject_unref() after use | |
180 | */ | |
181 | static QDict *migrate_query(QTestState *who) | |
182 | { | |
e1454165 | 183 | return wait_command(who, "{ 'execute': 'query-migrate' }"); |
2f7074c6 PX |
184 | } |
185 | ||
186 | /* | |
187 | * Note: caller is responsible to free the returned object via | |
188 | * g_free() after use | |
189 | */ | |
190 | static gchar *migrate_query_status(QTestState *who) | |
191 | { | |
192 | QDict *rsp_return = migrate_query(who); | |
193 | gchar *status = g_strdup(qdict_get_str(rsp_return, "status")); | |
194 | ||
195 | g_assert(status); | |
196 | qobject_unref(rsp_return); | |
197 | ||
198 | return status; | |
199 | } | |
ea0c6d62 DDAG |
200 | |
201 | /* | |
202 | * It's tricky to use qemu's migration event capability with qtest, | |
203 | * events suddenly appearing confuse the qmp()/hmp() responses. | |
ea0c6d62 DDAG |
204 | */ |
205 | ||
863e27a8 | 206 | static uint64_t get_migration_pass(QTestState *who) |
ea0c6d62 | 207 | { |
2f7074c6 | 208 | QDict *rsp_return, *rsp_ram; |
ea0c6d62 DDAG |
209 | uint64_t result; |
210 | ||
2f7074c6 | 211 | rsp_return = migrate_query(who); |
ea0c6d62 DDAG |
212 | if (!qdict_haskey(rsp_return, "ram")) { |
213 | /* Still in setup */ | |
214 | result = 0; | |
215 | } else { | |
216 | rsp_ram = qdict_get_qdict(rsp_return, "ram"); | |
217 | result = qdict_get_try_int(rsp_ram, "dirty-sync-count", 0); | |
ea0c6d62 | 218 | } |
2f7074c6 | 219 | qobject_unref(rsp_return); |
ea0c6d62 DDAG |
220 | return result; |
221 | } | |
222 | ||
346f3dab AP |
223 | static void read_blocktime(QTestState *who) |
224 | { | |
2f7074c6 | 225 | QDict *rsp_return; |
346f3dab | 226 | |
2f7074c6 | 227 | rsp_return = migrate_query(who); |
346f3dab | 228 | g_assert(qdict_haskey(rsp_return, "postcopy-blocktime")); |
2f7074c6 | 229 | qobject_unref(rsp_return); |
346f3dab AP |
230 | } |
231 | ||
2f6d3138 PX |
232 | static void wait_for_migration_status(QTestState *who, |
233 | const char *goal) | |
ea0c6d62 | 234 | { |
6a7724e9 | 235 | while (true) { |
6a7724e9 | 236 | bool completed; |
2f7074c6 | 237 | char *status; |
ea0c6d62 | 238 | |
2f7074c6 | 239 | status = migrate_query_status(who); |
2f6d3138 | 240 | completed = strcmp(status, goal) == 0; |
ea0c6d62 | 241 | g_assert_cmpstr(status, !=, "failed"); |
2f7074c6 | 242 | g_free(status); |
6a7724e9 JQ |
243 | if (completed) { |
244 | return; | |
245 | } | |
246 | usleep(1000); | |
247 | } | |
ea0c6d62 DDAG |
248 | } |
249 | ||
2f6d3138 PX |
250 | static void wait_for_migration_complete(QTestState *who) |
251 | { | |
252 | wait_for_migration_status(who, "completed"); | |
253 | } | |
254 | ||
863e27a8 | 255 | static void wait_for_migration_pass(QTestState *who) |
ea0c6d62 | 256 | { |
863e27a8 | 257 | uint64_t initial_pass = get_migration_pass(who); |
ea0c6d62 DDAG |
258 | uint64_t pass; |
259 | ||
260 | /* Wait for the 1st sync */ | |
6a7724e9 JQ |
261 | while (!got_stop && !initial_pass) { |
262 | usleep(1000); | |
863e27a8 | 263 | initial_pass = get_migration_pass(who); |
6a7724e9 | 264 | } |
ea0c6d62 DDAG |
265 | |
266 | do { | |
6a7724e9 | 267 | usleep(1000); |
863e27a8 | 268 | pass = get_migration_pass(who); |
ea0c6d62 DDAG |
269 | } while (pass == initial_pass && !got_stop); |
270 | } | |
271 | ||
7195a871 | 272 | static void check_guests_ram(QTestState *who) |
ea0c6d62 DDAG |
273 | { |
274 | /* Our ASM test will have been incrementing one byte from each page from | |
e51e711b WH |
275 | * start_address to < end_address in order. This gives us a constraint |
276 | * that any page's byte should be equal or less than the previous pages | |
277 | * byte (mod 256); and they should all be equal except for one transition | |
278 | * at the point where we meet the incrementer. (We're running this with | |
279 | * the guest stopped). | |
ea0c6d62 DDAG |
280 | */ |
281 | unsigned address; | |
282 | uint8_t first_byte; | |
283 | uint8_t last_byte; | |
284 | bool hit_edge = false; | |
285 | bool bad = false; | |
286 | ||
7195a871 | 287 | qtest_memread(who, start_address, &first_byte, 1); |
ea0c6d62 DDAG |
288 | last_byte = first_byte; |
289 | ||
e51e711b WH |
290 | for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address; |
291 | address += TEST_MEM_PAGE_SIZE) | |
ea0c6d62 DDAG |
292 | { |
293 | uint8_t b; | |
7195a871 | 294 | qtest_memread(who, address, &b, 1); |
ea0c6d62 DDAG |
295 | if (b != last_byte) { |
296 | if (((b + 1) % 256) == last_byte && !hit_edge) { | |
297 | /* This is OK, the guest stopped at the point of | |
298 | * incrementing the previous page but didn't get | |
299 | * to us yet. | |
300 | */ | |
301 | hit_edge = true; | |
829db8b4 | 302 | last_byte = b; |
ea0c6d62 DDAG |
303 | } else { |
304 | fprintf(stderr, "Memory content inconsistency at %x" | |
305 | " first_byte = %x last_byte = %x current = %x" | |
306 | " hit_edge = %x\n", | |
307 | address, first_byte, last_byte, b, hit_edge); | |
308 | bad = true; | |
309 | } | |
310 | } | |
ea0c6d62 DDAG |
311 | } |
312 | g_assert_false(bad); | |
313 | } | |
314 | ||
315 | static void cleanup(const char *filename) | |
316 | { | |
317 | char *path = g_strdup_printf("%s/%s", tmpfs, filename); | |
318 | ||
319 | unlink(path); | |
e2dd21e5 | 320 | g_free(path); |
ea0c6d62 DDAG |
321 | } |
322 | ||
56b4a42a | 323 | static void migrate_check_parameter(QTestState *who, const char *parameter, |
c44a56d8 | 324 | long long value) |
56b4a42a | 325 | { |
e1454165 | 326 | QDict *rsp_return; |
56b4a42a | 327 | |
e1454165 MA |
328 | rsp_return = wait_command(who, |
329 | "{ 'execute': 'query-migrate-parameters' }"); | |
c44a56d8 | 330 | g_assert_cmpint(qdict_get_int(rsp_return, parameter), ==, value); |
e1454165 | 331 | qobject_unref(rsp_return); |
56b4a42a JQ |
332 | } |
333 | ||
1f90d797 | 334 | static void migrate_set_parameter(QTestState *who, const char *parameter, |
c44a56d8 | 335 | long long value) |
d62fbe60 JQ |
336 | { |
337 | QDict *rsp; | |
d62fbe60 | 338 | |
c44a56d8 MA |
339 | rsp = qtest_qmp(who, |
340 | "{ 'execute': 'migrate-set-parameters'," | |
341 | "'arguments': { %s: %lld } }", | |
342 | parameter, value); | |
d62fbe60 | 343 | g_assert(qdict_haskey(rsp, "return")); |
cb3e7f08 | 344 | qobject_unref(rsp); |
1f90d797 | 345 | migrate_check_parameter(who, parameter, value); |
d62fbe60 JQ |
346 | } |
347 | ||
d5f49640 PX |
348 | static void migrate_pause(QTestState *who) |
349 | { | |
350 | QDict *rsp; | |
351 | ||
352 | rsp = wait_command(who, "{ 'execute': 'migrate-pause' }"); | |
d5f49640 PX |
353 | qobject_unref(rsp); |
354 | } | |
355 | ||
356 | static void migrate_recover(QTestState *who, const char *uri) | |
357 | { | |
358 | QDict *rsp; | |
d5f49640 | 359 | |
b7281c69 MA |
360 | rsp = wait_command(who, |
361 | "{ 'execute': 'migrate-recover', " | |
362 | " 'id': 'recover-cmd', " | |
363 | " 'arguments': { 'uri': %s } }", | |
364 | uri); | |
d5f49640 PX |
365 | qobject_unref(rsp); |
366 | } | |
367 | ||
d62fbe60 | 368 | static void migrate_set_capability(QTestState *who, const char *capability, |
c44a56d8 | 369 | bool value) |
d62fbe60 JQ |
370 | { |
371 | QDict *rsp; | |
c44a56d8 MA |
372 | |
373 | rsp = qtest_qmp(who, | |
374 | "{ 'execute': 'migrate-set-capabilities'," | |
375 | "'arguments': { " | |
376 | "'capabilities': [ { " | |
377 | "'capability': %s, 'state': %i } ] } }", | |
378 | capability, value); | |
d62fbe60 | 379 | g_assert(qdict_haskey(rsp, "return")); |
cb3e7f08 | 380 | qobject_unref(rsp); |
d62fbe60 JQ |
381 | } |
382 | ||
b5bbd3f3 MA |
383 | /* |
384 | * Send QMP command "migrate". | |
385 | * Arguments are built from @fmt... (formatted like | |
386 | * qobject_from_jsonf_nofail()) with "uri": @uri spliced in. | |
387 | */ | |
388 | GCC_FMT_ATTR(3, 4) | |
389 | static void migrate(QTestState *who, const char *uri, const char *fmt, ...) | |
d62fbe60 | 390 | { |
b5bbd3f3 MA |
391 | va_list ap; |
392 | QDict *args, *rsp; | |
d62fbe60 | 393 | |
b5bbd3f3 MA |
394 | va_start(ap, fmt); |
395 | args = qdict_from_vjsonf_nofail(fmt, ap); | |
396 | va_end(ap); | |
397 | ||
398 | g_assert(!qdict_haskey(args, "uri")); | |
399 | qdict_put_str(args, "uri", uri); | |
400 | ||
401 | rsp = qmp("{ 'execute': 'migrate', 'arguments': %p}", args); | |
d62fbe60 | 402 | g_assert(qdict_haskey(rsp, "return")); |
cb3e7f08 | 403 | qobject_unref(rsp); |
d62fbe60 JQ |
404 | } |
405 | ||
d131662a | 406 | static void migrate_postcopy_start(QTestState *from, QTestState *to) |
eb665d7d JQ |
407 | { |
408 | QDict *rsp; | |
409 | ||
d131662a | 410 | rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }"); |
cb3e7f08 | 411 | qobject_unref(rsp); |
d131662a PX |
412 | |
413 | if (!got_stop) { | |
414 | qtest_qmp_eventwait(from, "STOP"); | |
415 | } | |
416 | ||
417 | qtest_qmp_eventwait(to, "RESUME"); | |
eb665d7d JQ |
418 | } |
419 | ||
5fd4a9c9 | 420 | static int test_migrate_start(QTestState **from, QTestState **to, |
f96d6651 | 421 | const char *uri, bool hide_stderr) |
ea0c6d62 | 422 | { |
d62fbe60 | 423 | gchar *cmd_src, *cmd_dst; |
ea0c6d62 | 424 | char *bootpath = g_strdup_printf("%s/bootsect", tmpfs); |
aaf89c8a | 425 | const char *arch = qtest_get_arch(); |
63b2d935 | 426 | const char *accel = "kvm:tcg"; |
ea0c6d62 DDAG |
427 | |
428 | got_stop = false; | |
ea0c6d62 | 429 | |
aaf89c8a | 430 | if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { |
431 | init_bootfile_x86(bootpath); | |
63b2d935 | 432 | cmd_src = g_strdup_printf("-machine accel=%s -m 150M" |
31a6bb74 | 433 | " -name source,debug-threads=on" |
aaf89c8a | 434 | " -serial file:%s/src_serial" |
435 | " -drive file=%s,format=raw", | |
63b2d935 JQ |
436 | accel, tmpfs, bootpath); |
437 | cmd_dst = g_strdup_printf("-machine accel=%s -m 150M" | |
31a6bb74 | 438 | " -name target,debug-threads=on" |
aaf89c8a | 439 | " -serial file:%s/dest_serial" |
440 | " -drive file=%s,format=raw" | |
441 | " -incoming %s", | |
63b2d935 | 442 | accel, tmpfs, bootpath, uri); |
e51e711b WH |
443 | start_address = X86_TEST_MEM_START; |
444 | end_address = X86_TEST_MEM_END; | |
aaf89c8a | 445 | } else if (strcmp(arch, "ppc64") == 0) { |
fc71e3e5 | 446 | cmd_src = g_strdup_printf("-machine accel=%s -m 256M -nodefaults" |
31a6bb74 | 447 | " -name source,debug-threads=on" |
aaf89c8a | 448 | " -serial file:%s/src_serial" |
fc71e3e5 TH |
449 | " -prom-env 'use-nvramrc?=true' -prom-env " |
450 | "'nvramrc=hex .\" _\" begin %x %x " | |
cdf33815 JQ |
451 | "do i c@ 1 + i c! 1000 +loop .\" B\" 0 " |
452 | "until'", accel, tmpfs, end_address, | |
453 | start_address); | |
171da9d5 | 454 | cmd_dst = g_strdup_printf("-machine accel=%s -m 256M" |
31a6bb74 | 455 | " -name target,debug-threads=on" |
aaf89c8a | 456 | " -serial file:%s/dest_serial" |
457 | " -incoming %s", | |
171da9d5 | 458 | accel, tmpfs, uri); |
e51e711b WH |
459 | |
460 | start_address = PPC_TEST_MEM_START; | |
461 | end_address = PPC_TEST_MEM_END; | |
aaf89c8a | 462 | } else { |
463 | g_assert_not_reached(); | |
464 | } | |
465 | ||
e2dd21e5 MAL |
466 | g_free(bootpath); |
467 | ||
f96d6651 DDAG |
468 | if (hide_stderr) { |
469 | gchar *tmp; | |
470 | tmp = g_strdup_printf("%s 2>/dev/null", cmd_src); | |
471 | g_free(cmd_src); | |
472 | cmd_src = tmp; | |
473 | ||
474 | tmp = g_strdup_printf("%s 2>/dev/null", cmd_dst); | |
475 | g_free(cmd_dst); | |
476 | cmd_dst = tmp; | |
477 | } | |
478 | ||
7195a871 | 479 | *from = qtest_start(cmd_src); |
aaf89c8a | 480 | g_free(cmd_src); |
481 | ||
7195a871 | 482 | *to = qtest_init(cmd_dst); |
aaf89c8a | 483 | g_free(cmd_dst); |
5fd4a9c9 | 484 | return 0; |
7195a871 JQ |
485 | } |
486 | ||
2c9bb297 | 487 | static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest) |
7195a871 JQ |
488 | { |
489 | unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d; | |
490 | ||
491 | qtest_quit(from); | |
492 | ||
2c9bb297 DDAG |
493 | if (test_dest) { |
494 | qtest_memread(to, start_address, &dest_byte_a, 1); | |
7195a871 | 495 | |
2c9bb297 DDAG |
496 | /* Destination still running, wait for a byte to change */ |
497 | do { | |
498 | qtest_memread(to, start_address, &dest_byte_b, 1); | |
499 | usleep(1000 * 10); | |
500 | } while (dest_byte_a == dest_byte_b); | |
501 | ||
502 | qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}"); | |
7195a871 | 503 | |
2c9bb297 DDAG |
504 | /* With it stopped, check nothing changes */ |
505 | qtest_memread(to, start_address, &dest_byte_c, 1); | |
506 | usleep(1000 * 200); | |
507 | qtest_memread(to, start_address, &dest_byte_d, 1); | |
508 | g_assert_cmpint(dest_byte_c, ==, dest_byte_d); | |
7195a871 | 509 | |
2c9bb297 DDAG |
510 | check_guests_ram(to); |
511 | } | |
7195a871 JQ |
512 | |
513 | qtest_quit(to); | |
514 | ||
515 | cleanup("bootsect"); | |
516 | cleanup("migsocket"); | |
517 | cleanup("src_serial"); | |
518 | cleanup("dest_serial"); | |
519 | } | |
520 | ||
4c27486d JQ |
521 | static void deprecated_set_downtime(QTestState *who, const double value) |
522 | { | |
523 | QDict *rsp; | |
4c27486d | 524 | |
015715f5 MA |
525 | rsp = qtest_qmp(who, |
526 | "{ 'execute': 'migrate_set_downtime'," | |
527 | " 'arguments': { 'value': %f } }", value); | |
4c27486d | 528 | g_assert(qdict_haskey(rsp, "return")); |
cb3e7f08 | 529 | qobject_unref(rsp); |
c44a56d8 | 530 | migrate_check_parameter(who, "downtime-limit", value * 1000); |
4c27486d JQ |
531 | } |
532 | ||
c44a56d8 | 533 | static void deprecated_set_speed(QTestState *who, long long value) |
4c27486d JQ |
534 | { |
535 | QDict *rsp; | |
4c27486d | 536 | |
c44a56d8 MA |
537 | rsp = qtest_qmp(who, "{ 'execute': 'migrate_set_speed'," |
538 | "'arguments': { 'value': %lld } }", value); | |
4c27486d | 539 | g_assert(qdict_haskey(rsp, "return")); |
cb3e7f08 | 540 | qobject_unref(rsp); |
4c27486d JQ |
541 | migrate_check_parameter(who, "max-bandwidth", value); |
542 | } | |
543 | ||
544 | static void test_deprecated(void) | |
545 | { | |
546 | QTestState *from; | |
547 | ||
548 | from = qtest_start(""); | |
549 | ||
550 | deprecated_set_downtime(from, 0.12345); | |
c44a56d8 | 551 | deprecated_set_speed(from, 12345); |
4c27486d JQ |
552 | |
553 | qtest_quit(from); | |
554 | } | |
555 | ||
d131662a | 556 | static int migrate_postcopy_prepare(QTestState **from_ptr, |
3e81f73c PX |
557 | QTestState **to_ptr, |
558 | bool hide_error) | |
7195a871 JQ |
559 | { |
560 | char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); | |
863e27a8 | 561 | QTestState *from, *to; |
7195a871 | 562 | |
3e81f73c | 563 | if (test_migrate_start(&from, &to, uri, hide_error)) { |
d131662a | 564 | return -1; |
5fd4a9c9 | 565 | } |
ea0c6d62 | 566 | |
c44a56d8 MA |
567 | migrate_set_capability(from, "postcopy-ram", true); |
568 | migrate_set_capability(to, "postcopy-ram", true); | |
569 | migrate_set_capability(to, "postcopy-blocktime", true); | |
ea0c6d62 DDAG |
570 | |
571 | /* We want to pick a speed slow enough that the test completes | |
572 | * quickly, but that it doesn't complete precopy even on a slow | |
573 | * machine, so also set the downtime. | |
574 | */ | |
c44a56d8 MA |
575 | migrate_set_parameter(from, "max-bandwidth", 100000000); |
576 | migrate_set_parameter(from, "downtime-limit", 1); | |
ea0c6d62 DDAG |
577 | |
578 | /* Wait for the first serial output from the source */ | |
579 | wait_for_serial("src_serial"); | |
580 | ||
b5bbd3f3 | 581 | migrate(from, uri, "{}"); |
d131662a | 582 | g_free(uri); |
ea0c6d62 | 583 | |
863e27a8 | 584 | wait_for_migration_pass(from); |
ea0c6d62 | 585 | |
d131662a PX |
586 | *from_ptr = from; |
587 | *to_ptr = to; | |
ea0c6d62 | 588 | |
d131662a PX |
589 | return 0; |
590 | } | |
ea0c6d62 | 591 | |
d131662a PX |
592 | static void migrate_postcopy_complete(QTestState *from, QTestState *to) |
593 | { | |
594 | wait_for_migration_complete(from); | |
ea0c6d62 | 595 | |
d131662a | 596 | /* Make sure we get at least one "B" on destination */ |
ea0c6d62 | 597 | wait_for_serial("dest_serial"); |
ea0c6d62 | 598 | |
346f3dab AP |
599 | if (uffd_feature_thread_id) { |
600 | read_blocktime(to); | |
601 | } | |
ea0c6d62 | 602 | |
2c9bb297 DDAG |
603 | test_migrate_end(from, to, true); |
604 | } | |
605 | ||
d131662a PX |
606 | static void test_postcopy(void) |
607 | { | |
608 | QTestState *from, *to; | |
609 | ||
3e81f73c | 610 | if (migrate_postcopy_prepare(&from, &to, false)) { |
d131662a PX |
611 | return; |
612 | } | |
613 | migrate_postcopy_start(from, to); | |
614 | migrate_postcopy_complete(from, to); | |
615 | } | |
616 | ||
d5f49640 PX |
617 | static void test_postcopy_recovery(void) |
618 | { | |
619 | QTestState *from, *to; | |
620 | char *uri; | |
621 | ||
3e81f73c | 622 | if (migrate_postcopy_prepare(&from, &to, true)) { |
d5f49640 PX |
623 | return; |
624 | } | |
625 | ||
626 | /* Turn postcopy speed down, 4K/s is slow enough on any machines */ | |
c44a56d8 | 627 | migrate_set_parameter(from, "max-postcopy-bandwidth", 4096); |
d5f49640 PX |
628 | |
629 | /* Now we start the postcopy */ | |
630 | migrate_postcopy_start(from, to); | |
631 | ||
632 | /* | |
633 | * Wait until postcopy is really started; we can only run the | |
634 | * migrate-pause command during a postcopy | |
635 | */ | |
636 | wait_for_migration_status(from, "postcopy-active"); | |
637 | ||
638 | /* | |
639 | * Manually stop the postcopy migration. This emulates a network | |
640 | * failure with the migration socket | |
641 | */ | |
642 | migrate_pause(from); | |
643 | ||
644 | /* | |
645 | * Wait for destination side to reach postcopy-paused state. The | |
646 | * migrate-recover command can only succeed if destination machine | |
647 | * is in the paused state | |
648 | */ | |
649 | wait_for_migration_status(to, "postcopy-paused"); | |
650 | ||
651 | /* | |
652 | * Create a new socket to emulate a new channel that is different | |
653 | * from the broken migration channel; tell the destination to | |
654 | * listen to the new port | |
655 | */ | |
656 | uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs); | |
657 | migrate_recover(to, uri); | |
658 | ||
659 | /* | |
660 | * Try to rebuild the migration channel using the resume flag and | |
661 | * the newly created channel | |
662 | */ | |
663 | wait_for_migration_status(from, "postcopy-paused"); | |
b5bbd3f3 | 664 | migrate(from, uri, "{'resume': true}"); |
d5f49640 PX |
665 | g_free(uri); |
666 | ||
667 | /* Restore the postcopy bandwidth to unlimited */ | |
c44a56d8 | 668 | migrate_set_parameter(from, "max-postcopy-bandwidth", 0); |
d5f49640 PX |
669 | |
670 | migrate_postcopy_complete(from, to); | |
671 | } | |
672 | ||
2c9bb297 DDAG |
673 | static void test_baddest(void) |
674 | { | |
675 | QTestState *from, *to; | |
e1454165 | 676 | QDict *rsp_return; |
2f7074c6 | 677 | char *status; |
2c9bb297 DDAG |
678 | bool failed; |
679 | ||
5fd4a9c9 DDAG |
680 | if (test_migrate_start(&from, &to, "tcp:0:0", true)) { |
681 | return; | |
682 | } | |
b5bbd3f3 | 683 | migrate(from, "tcp:0:0", "{}"); |
2c9bb297 | 684 | do { |
2f7074c6 | 685 | status = migrate_query_status(from); |
2c9bb297 DDAG |
686 | g_assert(!strcmp(status, "setup") || !(strcmp(status, "failed"))); |
687 | failed = !strcmp(status, "failed"); | |
2f7074c6 | 688 | g_free(status); |
2c9bb297 DDAG |
689 | } while (!failed); |
690 | ||
691 | /* Is the machine currently running? */ | |
e1454165 | 692 | rsp_return = wait_command(from, "{ 'execute': 'query-status' }"); |
2c9bb297 DDAG |
693 | g_assert(qdict_haskey(rsp_return, "running")); |
694 | g_assert(qdict_get_bool(rsp_return, "running")); | |
e1454165 | 695 | qobject_unref(rsp_return); |
2c9bb297 DDAG |
696 | |
697 | test_migrate_end(from, to, false); | |
ea0c6d62 DDAG |
698 | } |
699 | ||
2884100c JQ |
700 | static void test_precopy_unix(void) |
701 | { | |
702 | char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); | |
703 | QTestState *from, *to; | |
704 | ||
5fd4a9c9 DDAG |
705 | if (test_migrate_start(&from, &to, uri, false)) { |
706 | return; | |
707 | } | |
2884100c JQ |
708 | |
709 | /* We want to pick a speed slow enough that the test completes | |
710 | * quickly, but that it doesn't complete precopy even on a slow | |
711 | * machine, so also set the downtime. | |
712 | */ | |
713 | /* 1 ms should make it not converge*/ | |
c44a56d8 | 714 | migrate_set_parameter(from, "downtime-limit", 1); |
2884100c | 715 | /* 1GB/s */ |
c44a56d8 | 716 | migrate_set_parameter(from, "max-bandwidth", 1000000000); |
2884100c JQ |
717 | |
718 | /* Wait for the first serial output from the source */ | |
719 | wait_for_serial("src_serial"); | |
720 | ||
b5bbd3f3 | 721 | migrate(from, uri, "{}"); |
2884100c JQ |
722 | |
723 | wait_for_migration_pass(from); | |
724 | ||
725 | /* 300 ms should converge */ | |
c44a56d8 | 726 | migrate_set_parameter(from, "downtime-limit", 300); |
2884100c JQ |
727 | |
728 | if (!got_stop) { | |
729 | qtest_qmp_eventwait(from, "STOP"); | |
730 | } | |
731 | ||
732 | qtest_qmp_eventwait(to, "RESUME"); | |
733 | ||
734 | wait_for_serial("dest_serial"); | |
735 | wait_for_migration_complete(from); | |
736 | ||
737 | test_migrate_end(from, to, true); | |
738 | g_free(uri); | |
739 | } | |
740 | ||
ea0c6d62 DDAG |
741 | int main(int argc, char **argv) |
742 | { | |
2656bfd9 | 743 | char template[] = "/tmp/migration-test-XXXXXX"; |
ea0c6d62 DDAG |
744 | int ret; |
745 | ||
746 | g_test_init(&argc, &argv, NULL); | |
747 | ||
748 | if (!ufd_version_check()) { | |
749 | return 0; | |
750 | } | |
751 | ||
880b169a TH |
752 | /* |
753 | * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG | |
754 | * is touchy due to race conditions on dirty bits (especially on PPC for | |
755 | * some reason) | |
756 | */ | |
757 | if (g_str_equal(qtest_get_arch(), "ppc64") && | |
758 | access("/sys/module/kvm_hv", F_OK)) { | |
759 | g_test_message("Skipping test: kvm_hv not available"); | |
760 | return 0; | |
761 | } | |
762 | ||
ea0c6d62 DDAG |
763 | tmpfs = mkdtemp(template); |
764 | if (!tmpfs) { | |
765 | g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno)); | |
766 | } | |
767 | g_assert(tmpfs); | |
768 | ||
769 | module_call_init(MODULE_INIT_QOM); | |
770 | ||
2884100c | 771 | qtest_add_func("/migration/postcopy/unix", test_postcopy); |
d5f49640 | 772 | qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery); |
4c27486d | 773 | qtest_add_func("/migration/deprecated", test_deprecated); |
2c9bb297 | 774 | qtest_add_func("/migration/bad_dest", test_baddest); |
2884100c | 775 | qtest_add_func("/migration/precopy/unix", test_precopy_unix); |
ea0c6d62 DDAG |
776 | |
777 | ret = g_test_run(); | |
778 | ||
779 | g_assert_cmpint(ret, ==, 0); | |
780 | ||
781 | ret = rmdir(tmpfs); | |
782 | if (ret != 0) { | |
783 | g_test_message("unable to rmdir: path (%s): %s\n", | |
784 | tmpfs, strerror(errno)); | |
785 | } | |
786 | ||
787 | return ret; | |
788 | } |