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