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