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