]>
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 | ||
2f7074c6 PX |
171 | /* |
172 | * Note: caller is responsible to free the returned object via | |
173 | * qobject_unref() after use | |
174 | */ | |
175 | static QDict *migrate_query(QTestState *who) | |
176 | { | |
177 | QDict *rsp, *rsp_return; | |
178 | ||
179 | rsp = wait_command(who, "{ 'execute': 'query-migrate' }"); | |
180 | rsp_return = qdict_get_qdict(rsp, "return"); | |
181 | g_assert(rsp_return); | |
182 | qobject_ref(rsp_return); | |
183 | qobject_unref(rsp); | |
184 | ||
185 | return rsp_return; | |
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 | ||
863e27a8 | 234 | static void wait_for_migration_complete(QTestState *who) |
ea0c6d62 | 235 | { |
6a7724e9 | 236 | while (true) { |
6a7724e9 | 237 | bool completed; |
2f7074c6 | 238 | char *status; |
ea0c6d62 | 239 | |
2f7074c6 | 240 | status = migrate_query_status(who); |
ea0c6d62 DDAG |
241 | completed = strcmp(status, "completed") == 0; |
242 | g_assert_cmpstr(status, !=, "failed"); | |
2f7074c6 | 243 | g_free(status); |
6a7724e9 JQ |
244 | if (completed) { |
245 | return; | |
246 | } | |
247 | usleep(1000); | |
248 | } | |
ea0c6d62 DDAG |
249 | } |
250 | ||
863e27a8 | 251 | static void wait_for_migration_pass(QTestState *who) |
ea0c6d62 | 252 | { |
863e27a8 | 253 | uint64_t initial_pass = get_migration_pass(who); |
ea0c6d62 DDAG |
254 | uint64_t pass; |
255 | ||
256 | /* Wait for the 1st sync */ | |
6a7724e9 JQ |
257 | while (!got_stop && !initial_pass) { |
258 | usleep(1000); | |
863e27a8 | 259 | initial_pass = get_migration_pass(who); |
6a7724e9 | 260 | } |
ea0c6d62 DDAG |
261 | |
262 | do { | |
6a7724e9 | 263 | usleep(1000); |
863e27a8 | 264 | pass = get_migration_pass(who); |
ea0c6d62 DDAG |
265 | } while (pass == initial_pass && !got_stop); |
266 | } | |
267 | ||
7195a871 | 268 | static void check_guests_ram(QTestState *who) |
ea0c6d62 DDAG |
269 | { |
270 | /* Our ASM test will have been incrementing one byte from each page from | |
271 | * 1MB to <100MB in order. | |
272 | * This gives us a constraint that any page's byte should be equal or less | |
273 | * than the previous pages byte (mod 256); and they should all be equal | |
274 | * except for one transition at the point where we meet the incrementer. | |
275 | * (We're running this with the guest stopped). | |
276 | */ | |
277 | unsigned address; | |
278 | uint8_t first_byte; | |
279 | uint8_t last_byte; | |
280 | bool hit_edge = false; | |
281 | bool bad = false; | |
282 | ||
7195a871 | 283 | qtest_memread(who, start_address, &first_byte, 1); |
ea0c6d62 DDAG |
284 | last_byte = first_byte; |
285 | ||
286 | for (address = start_address + 4096; address < end_address; address += 4096) | |
287 | { | |
288 | uint8_t b; | |
7195a871 | 289 | qtest_memread(who, address, &b, 1); |
ea0c6d62 DDAG |
290 | if (b != last_byte) { |
291 | if (((b + 1) % 256) == last_byte && !hit_edge) { | |
292 | /* This is OK, the guest stopped at the point of | |
293 | * incrementing the previous page but didn't get | |
294 | * to us yet. | |
295 | */ | |
296 | hit_edge = true; | |
297 | } else { | |
298 | fprintf(stderr, "Memory content inconsistency at %x" | |
299 | " first_byte = %x last_byte = %x current = %x" | |
300 | " hit_edge = %x\n", | |
301 | address, first_byte, last_byte, b, hit_edge); | |
302 | bad = true; | |
303 | } | |
304 | } | |
305 | last_byte = b; | |
306 | } | |
307 | g_assert_false(bad); | |
308 | } | |
309 | ||
310 | static void cleanup(const char *filename) | |
311 | { | |
312 | char *path = g_strdup_printf("%s/%s", tmpfs, filename); | |
313 | ||
314 | unlink(path); | |
e2dd21e5 | 315 | g_free(path); |
ea0c6d62 DDAG |
316 | } |
317 | ||
56b4a42a JQ |
318 | static void migrate_check_parameter(QTestState *who, const char *parameter, |
319 | const char *value) | |
320 | { | |
321 | QDict *rsp, *rsp_return; | |
9c43435d | 322 | char *result; |
56b4a42a JQ |
323 | |
324 | rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }"); | |
325 | rsp_return = qdict_get_qdict(rsp, "return"); | |
326 | result = g_strdup_printf("%" PRId64, | |
327 | qdict_get_try_int(rsp_return, parameter, -1)); | |
328 | g_assert_cmpstr(result, ==, value); | |
9c43435d | 329 | g_free(result); |
cb3e7f08 | 330 | qobject_unref(rsp); |
56b4a42a JQ |
331 | } |
332 | ||
1f90d797 JQ |
333 | static void migrate_set_parameter(QTestState *who, const char *parameter, |
334 | const char *value) | |
d62fbe60 JQ |
335 | { |
336 | QDict *rsp; | |
337 | gchar *cmd; | |
338 | ||
1f90d797 JQ |
339 | cmd = g_strdup_printf("{ 'execute': 'migrate-set-parameters'," |
340 | "'arguments': { '%s': %s } }", | |
341 | parameter, value); | |
d62fbe60 JQ |
342 | rsp = qtest_qmp(who, cmd); |
343 | g_free(cmd); | |
344 | g_assert(qdict_haskey(rsp, "return")); | |
cb3e7f08 | 345 | qobject_unref(rsp); |
1f90d797 | 346 | migrate_check_parameter(who, parameter, value); |
d62fbe60 JQ |
347 | } |
348 | ||
349 | static void migrate_set_capability(QTestState *who, const char *capability, | |
350 | const char *value) | |
351 | { | |
352 | QDict *rsp; | |
353 | gchar *cmd; | |
354 | ||
355 | cmd = g_strdup_printf("{ 'execute': 'migrate-set-capabilities'," | |
356 | "'arguments': { " | |
357 | "'capabilities': [ { " | |
358 | "'capability': '%s', 'state': %s } ] } }", | |
359 | capability, value); | |
360 | rsp = qtest_qmp(who, cmd); | |
361 | g_free(cmd); | |
362 | g_assert(qdict_haskey(rsp, "return")); | |
cb3e7f08 | 363 | qobject_unref(rsp); |
d62fbe60 JQ |
364 | } |
365 | ||
7e1d7427 | 366 | static void migrate(QTestState *who, const char *uri, const char *extra) |
d62fbe60 JQ |
367 | { |
368 | QDict *rsp; | |
369 | gchar *cmd; | |
370 | ||
371 | cmd = g_strdup_printf("{ 'execute': 'migrate'," | |
7e1d7427 PX |
372 | " 'arguments': { 'uri': '%s' %s } }", |
373 | uri, extra ? extra : ""); | |
d62fbe60 JQ |
374 | rsp = qtest_qmp(who, cmd); |
375 | g_free(cmd); | |
376 | g_assert(qdict_haskey(rsp, "return")); | |
cb3e7f08 | 377 | qobject_unref(rsp); |
d62fbe60 JQ |
378 | } |
379 | ||
d131662a | 380 | static void migrate_postcopy_start(QTestState *from, QTestState *to) |
eb665d7d JQ |
381 | { |
382 | QDict *rsp; | |
383 | ||
d131662a | 384 | rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }"); |
eb665d7d | 385 | g_assert(qdict_haskey(rsp, "return")); |
cb3e7f08 | 386 | qobject_unref(rsp); |
d131662a PX |
387 | |
388 | if (!got_stop) { | |
389 | qtest_qmp_eventwait(from, "STOP"); | |
390 | } | |
391 | ||
392 | qtest_qmp_eventwait(to, "RESUME"); | |
eb665d7d JQ |
393 | } |
394 | ||
5fd4a9c9 | 395 | static int test_migrate_start(QTestState **from, QTestState **to, |
f96d6651 | 396 | const char *uri, bool hide_stderr) |
ea0c6d62 | 397 | { |
d62fbe60 | 398 | gchar *cmd_src, *cmd_dst; |
ea0c6d62 | 399 | char *bootpath = g_strdup_printf("%s/bootsect", tmpfs); |
aaf89c8a | 400 | const char *arch = qtest_get_arch(); |
63b2d935 | 401 | const char *accel = "kvm:tcg"; |
ea0c6d62 DDAG |
402 | |
403 | got_stop = false; | |
ea0c6d62 | 404 | |
aaf89c8a | 405 | if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { |
406 | init_bootfile_x86(bootpath); | |
63b2d935 | 407 | cmd_src = g_strdup_printf("-machine accel=%s -m 150M" |
31a6bb74 | 408 | " -name source,debug-threads=on" |
aaf89c8a | 409 | " -serial file:%s/src_serial" |
410 | " -drive file=%s,format=raw", | |
63b2d935 JQ |
411 | accel, tmpfs, bootpath); |
412 | cmd_dst = g_strdup_printf("-machine accel=%s -m 150M" | |
31a6bb74 | 413 | " -name target,debug-threads=on" |
aaf89c8a | 414 | " -serial file:%s/dest_serial" |
415 | " -drive file=%s,format=raw" | |
416 | " -incoming %s", | |
63b2d935 | 417 | accel, tmpfs, bootpath, uri); |
aaf89c8a | 418 | } else if (strcmp(arch, "ppc64") == 0) { |
171da9d5 | 419 | |
5fd4a9c9 DDAG |
420 | /* On ppc64, the test only works with kvm-hv, but not with kvm-pr |
421 | * and TCG is touchy due to race conditions on dirty bits | |
422 | * (especially on PPC for some reason) | |
423 | */ | |
63b2d935 | 424 | if (access("/sys/module/kvm_hv", F_OK)) { |
5fd4a9c9 DDAG |
425 | g_print("Skipping test: kvm_hv not available "); |
426 | return -1; | |
63b2d935 | 427 | } |
171da9d5 | 428 | cmd_src = g_strdup_printf("-machine accel=%s -m 256M" |
31a6bb74 | 429 | " -name source,debug-threads=on" |
aaf89c8a | 430 | " -serial file:%s/src_serial" |
cdf33815 JQ |
431 | " -prom-env '" |
432 | "boot-command=hex .\" _\" begin %x %x " | |
433 | "do i c@ 1 + i c! 1000 +loop .\" B\" 0 " | |
434 | "until'", accel, tmpfs, end_address, | |
435 | start_address); | |
171da9d5 | 436 | cmd_dst = g_strdup_printf("-machine accel=%s -m 256M" |
31a6bb74 | 437 | " -name target,debug-threads=on" |
aaf89c8a | 438 | " -serial file:%s/dest_serial" |
439 | " -incoming %s", | |
171da9d5 | 440 | accel, tmpfs, uri); |
aaf89c8a | 441 | } else { |
442 | g_assert_not_reached(); | |
443 | } | |
444 | ||
e2dd21e5 MAL |
445 | g_free(bootpath); |
446 | ||
f96d6651 DDAG |
447 | if (hide_stderr) { |
448 | gchar *tmp; | |
449 | tmp = g_strdup_printf("%s 2>/dev/null", cmd_src); | |
450 | g_free(cmd_src); | |
451 | cmd_src = tmp; | |
452 | ||
453 | tmp = g_strdup_printf("%s 2>/dev/null", cmd_dst); | |
454 | g_free(cmd_dst); | |
455 | cmd_dst = tmp; | |
456 | } | |
457 | ||
7195a871 | 458 | *from = qtest_start(cmd_src); |
aaf89c8a | 459 | g_free(cmd_src); |
460 | ||
7195a871 | 461 | *to = qtest_init(cmd_dst); |
aaf89c8a | 462 | g_free(cmd_dst); |
5fd4a9c9 | 463 | return 0; |
7195a871 JQ |
464 | } |
465 | ||
2c9bb297 | 466 | static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest) |
7195a871 JQ |
467 | { |
468 | unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d; | |
469 | ||
470 | qtest_quit(from); | |
471 | ||
2c9bb297 DDAG |
472 | if (test_dest) { |
473 | qtest_memread(to, start_address, &dest_byte_a, 1); | |
7195a871 | 474 | |
2c9bb297 DDAG |
475 | /* Destination still running, wait for a byte to change */ |
476 | do { | |
477 | qtest_memread(to, start_address, &dest_byte_b, 1); | |
478 | usleep(1000 * 10); | |
479 | } while (dest_byte_a == dest_byte_b); | |
480 | ||
481 | qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}"); | |
7195a871 | 482 | |
2c9bb297 DDAG |
483 | /* With it stopped, check nothing changes */ |
484 | qtest_memread(to, start_address, &dest_byte_c, 1); | |
485 | usleep(1000 * 200); | |
486 | qtest_memread(to, start_address, &dest_byte_d, 1); | |
487 | g_assert_cmpint(dest_byte_c, ==, dest_byte_d); | |
7195a871 | 488 | |
2c9bb297 DDAG |
489 | check_guests_ram(to); |
490 | } | |
7195a871 JQ |
491 | |
492 | qtest_quit(to); | |
493 | ||
494 | cleanup("bootsect"); | |
495 | cleanup("migsocket"); | |
496 | cleanup("src_serial"); | |
497 | cleanup("dest_serial"); | |
498 | } | |
499 | ||
4c27486d JQ |
500 | static void deprecated_set_downtime(QTestState *who, const double value) |
501 | { | |
502 | QDict *rsp; | |
503 | gchar *cmd; | |
504 | char *expected; | |
505 | int64_t result_int; | |
506 | ||
507 | cmd = g_strdup_printf("{ 'execute': 'migrate_set_downtime'," | |
508 | "'arguments': { 'value': %g } }", value); | |
509 | rsp = qtest_qmp(who, cmd); | |
510 | g_free(cmd); | |
511 | g_assert(qdict_haskey(rsp, "return")); | |
cb3e7f08 | 512 | qobject_unref(rsp); |
4c27486d JQ |
513 | result_int = value * 1000L; |
514 | expected = g_strdup_printf("%" PRId64, result_int); | |
515 | migrate_check_parameter(who, "downtime-limit", expected); | |
516 | g_free(expected); | |
517 | } | |
518 | ||
519 | static void deprecated_set_speed(QTestState *who, const char *value) | |
520 | { | |
521 | QDict *rsp; | |
522 | gchar *cmd; | |
523 | ||
524 | cmd = g_strdup_printf("{ 'execute': 'migrate_set_speed'," | |
525 | "'arguments': { 'value': %s } }", value); | |
526 | rsp = qtest_qmp(who, cmd); | |
527 | g_free(cmd); | |
528 | g_assert(qdict_haskey(rsp, "return")); | |
cb3e7f08 | 529 | qobject_unref(rsp); |
4c27486d JQ |
530 | migrate_check_parameter(who, "max-bandwidth", value); |
531 | } | |
532 | ||
533 | static void test_deprecated(void) | |
534 | { | |
535 | QTestState *from; | |
536 | ||
537 | from = qtest_start(""); | |
538 | ||
539 | deprecated_set_downtime(from, 0.12345); | |
540 | deprecated_set_speed(from, "12345"); | |
541 | ||
542 | qtest_quit(from); | |
543 | } | |
544 | ||
d131662a PX |
545 | static int migrate_postcopy_prepare(QTestState **from_ptr, |
546 | QTestState **to_ptr) | |
7195a871 JQ |
547 | { |
548 | char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); | |
863e27a8 | 549 | QTestState *from, *to; |
7195a871 | 550 | |
5fd4a9c9 | 551 | if (test_migrate_start(&from, &to, uri, false)) { |
d131662a | 552 | return -1; |
5fd4a9c9 | 553 | } |
ea0c6d62 | 554 | |
d62fbe60 JQ |
555 | migrate_set_capability(from, "postcopy-ram", "true"); |
556 | migrate_set_capability(to, "postcopy-ram", "true"); | |
346f3dab | 557 | migrate_set_capability(to, "postcopy-blocktime", "true"); |
ea0c6d62 DDAG |
558 | |
559 | /* We want to pick a speed slow enough that the test completes | |
560 | * quickly, but that it doesn't complete precopy even on a slow | |
561 | * machine, so also set the downtime. | |
562 | */ | |
1f90d797 JQ |
563 | migrate_set_parameter(from, "max-bandwidth", "100000000"); |
564 | migrate_set_parameter(from, "downtime-limit", "1"); | |
ea0c6d62 DDAG |
565 | |
566 | /* Wait for the first serial output from the source */ | |
567 | wait_for_serial("src_serial"); | |
568 | ||
7e1d7427 | 569 | migrate(from, uri, NULL); |
d131662a | 570 | g_free(uri); |
ea0c6d62 | 571 | |
863e27a8 | 572 | wait_for_migration_pass(from); |
ea0c6d62 | 573 | |
d131662a PX |
574 | *from_ptr = from; |
575 | *to_ptr = to; | |
ea0c6d62 | 576 | |
d131662a PX |
577 | return 0; |
578 | } | |
ea0c6d62 | 579 | |
d131662a PX |
580 | static void migrate_postcopy_complete(QTestState *from, QTestState *to) |
581 | { | |
582 | wait_for_migration_complete(from); | |
ea0c6d62 | 583 | |
d131662a | 584 | /* Make sure we get at least one "B" on destination */ |
ea0c6d62 | 585 | wait_for_serial("dest_serial"); |
ea0c6d62 | 586 | |
346f3dab AP |
587 | if (uffd_feature_thread_id) { |
588 | read_blocktime(to); | |
589 | } | |
ea0c6d62 | 590 | |
2c9bb297 DDAG |
591 | test_migrate_end(from, to, true); |
592 | } | |
593 | ||
d131662a PX |
594 | static void test_postcopy(void) |
595 | { | |
596 | QTestState *from, *to; | |
597 | ||
598 | if (migrate_postcopy_prepare(&from, &to)) { | |
599 | return; | |
600 | } | |
601 | migrate_postcopy_start(from, to); | |
602 | migrate_postcopy_complete(from, to); | |
603 | } | |
604 | ||
2c9bb297 DDAG |
605 | static void test_baddest(void) |
606 | { | |
607 | QTestState *from, *to; | |
608 | QDict *rsp, *rsp_return; | |
2f7074c6 | 609 | char *status; |
2c9bb297 DDAG |
610 | bool failed; |
611 | ||
5fd4a9c9 DDAG |
612 | if (test_migrate_start(&from, &to, "tcp:0:0", true)) { |
613 | return; | |
614 | } | |
7e1d7427 | 615 | migrate(from, "tcp:0:0", NULL); |
2c9bb297 | 616 | do { |
2f7074c6 | 617 | status = migrate_query_status(from); |
2c9bb297 DDAG |
618 | g_assert(!strcmp(status, "setup") || !(strcmp(status, "failed"))); |
619 | failed = !strcmp(status, "failed"); | |
2f7074c6 | 620 | g_free(status); |
2c9bb297 DDAG |
621 | } while (!failed); |
622 | ||
623 | /* Is the machine currently running? */ | |
624 | rsp = wait_command(from, "{ 'execute': 'query-status' }"); | |
625 | g_assert(qdict_haskey(rsp, "return")); | |
626 | rsp_return = qdict_get_qdict(rsp, "return"); | |
627 | g_assert(qdict_haskey(rsp_return, "running")); | |
628 | g_assert(qdict_get_bool(rsp_return, "running")); | |
cb3e7f08 | 629 | qobject_unref(rsp); |
2c9bb297 DDAG |
630 | |
631 | test_migrate_end(from, to, false); | |
ea0c6d62 DDAG |
632 | } |
633 | ||
2884100c JQ |
634 | static void test_precopy_unix(void) |
635 | { | |
636 | char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); | |
637 | QTestState *from, *to; | |
638 | ||
5fd4a9c9 DDAG |
639 | if (test_migrate_start(&from, &to, uri, false)) { |
640 | return; | |
641 | } | |
2884100c JQ |
642 | |
643 | /* We want to pick a speed slow enough that the test completes | |
644 | * quickly, but that it doesn't complete precopy even on a slow | |
645 | * machine, so also set the downtime. | |
646 | */ | |
647 | /* 1 ms should make it not converge*/ | |
648 | migrate_set_parameter(from, "downtime-limit", "1"); | |
649 | /* 1GB/s */ | |
650 | migrate_set_parameter(from, "max-bandwidth", "1000000000"); | |
651 | ||
652 | /* Wait for the first serial output from the source */ | |
653 | wait_for_serial("src_serial"); | |
654 | ||
7e1d7427 | 655 | migrate(from, uri, NULL); |
2884100c JQ |
656 | |
657 | wait_for_migration_pass(from); | |
658 | ||
659 | /* 300 ms should converge */ | |
660 | migrate_set_parameter(from, "downtime-limit", "300"); | |
661 | ||
662 | if (!got_stop) { | |
663 | qtest_qmp_eventwait(from, "STOP"); | |
664 | } | |
665 | ||
666 | qtest_qmp_eventwait(to, "RESUME"); | |
667 | ||
668 | wait_for_serial("dest_serial"); | |
669 | wait_for_migration_complete(from); | |
670 | ||
671 | test_migrate_end(from, to, true); | |
672 | g_free(uri); | |
673 | } | |
674 | ||
ea0c6d62 DDAG |
675 | int main(int argc, char **argv) |
676 | { | |
2656bfd9 | 677 | char template[] = "/tmp/migration-test-XXXXXX"; |
ea0c6d62 DDAG |
678 | int ret; |
679 | ||
680 | g_test_init(&argc, &argv, NULL); | |
681 | ||
682 | if (!ufd_version_check()) { | |
683 | return 0; | |
684 | } | |
685 | ||
686 | tmpfs = mkdtemp(template); | |
687 | if (!tmpfs) { | |
688 | g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno)); | |
689 | } | |
690 | g_assert(tmpfs); | |
691 | ||
692 | module_call_init(MODULE_INIT_QOM); | |
693 | ||
2884100c | 694 | qtest_add_func("/migration/postcopy/unix", test_postcopy); |
4c27486d | 695 | qtest_add_func("/migration/deprecated", test_deprecated); |
2c9bb297 | 696 | qtest_add_func("/migration/bad_dest", test_baddest); |
2884100c | 697 | qtest_add_func("/migration/precopy/unix", test_precopy_unix); |
ea0c6d62 DDAG |
698 | |
699 | ret = g_test_run(); | |
700 | ||
701 | g_assert_cmpint(ret, ==, 0); | |
702 | ||
703 | ret = rmdir(tmpfs); | |
704 | if (ret != 0) { | |
705 | g_test_message("unable to rmdir: path (%s): %s\n", | |
706 | tmpfs, strerror(errno)); | |
707 | } | |
708 | ||
709 | return ret; | |
710 | } |