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