]>
Commit | Line | Data |
---|---|---|
ea0c6d62 | 1 | /* |
2656bfd9 | 2 | * QTest testcase for migration |
ea0c6d62 | 3 | * |
17ca7746 | 4 | * Copyright (c) 2016-2018 Red Hat, Inc. and/or its affiliates |
ea0c6d62 DDAG |
5 | * based on the vhost-user-test.c that is: |
6 | * Copyright (c) 2014 Virtual Open Systems Sarl. | |
7 | * | |
8 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
9 | * See the COPYING file in the top-level directory. | |
10 | * | |
11 | */ | |
12 | ||
13 | #include "qemu/osdep.h" | |
ea0c6d62 DDAG |
14 | |
15 | #include "libqtest.h" | |
452fcdbc | 16 | #include "qapi/qmp/qdict.h" |
b5bbd3f3 | 17 | #include "qapi/qmp/qjson.h" |
ea0c6d62 DDAG |
18 | #include "qemu/option.h" |
19 | #include "qemu/range.h" | |
a9c94277 | 20 | #include "qemu/sockets.h" |
8228e353 | 21 | #include "chardev/char.h" |
ea0c6d62 DDAG |
22 | #include "sysemu/sysemu.h" |
23 | ||
e51e711b WH |
24 | #include "migration/migration-test.h" |
25 | ||
055a1efc MA |
26 | /* TODO actually test the results and get rid of this */ |
27 | #define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__)) | |
28 | ||
e51e711b WH |
29 | unsigned start_address; |
30 | unsigned end_address; | |
ea0c6d62 | 31 | bool got_stop; |
346f3dab | 32 | static bool uffd_feature_thread_id; |
ea0c6d62 DDAG |
33 | |
34 | #if defined(__linux__) | |
ea0c6d62 DDAG |
35 | #include <sys/syscall.h> |
36 | #include <sys/vfs.h> | |
37 | #endif | |
38 | ||
39 | #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD) | |
40 | #include <sys/eventfd.h> | |
41 | #include <sys/ioctl.h> | |
42 | #include <linux/userfaultfd.h> | |
43 | ||
44 | static bool ufd_version_check(void) | |
45 | { | |
46 | struct uffdio_api api_struct; | |
47 | uint64_t ioctl_mask; | |
48 | ||
e1ae9fb6 | 49 | int ufd = syscall(__NR_userfaultfd, O_CLOEXEC); |
ea0c6d62 DDAG |
50 | |
51 | if (ufd == -1) { | |
52 | g_test_message("Skipping test: userfaultfd not available"); | |
53 | return false; | |
54 | } | |
55 | ||
56 | api_struct.api = UFFD_API; | |
57 | api_struct.features = 0; | |
58 | if (ioctl(ufd, UFFDIO_API, &api_struct)) { | |
59 | g_test_message("Skipping test: UFFDIO_API failed"); | |
60 | return false; | |
61 | } | |
346f3dab | 62 | uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID; |
ea0c6d62 DDAG |
63 | |
64 | ioctl_mask = (__u64)1 << _UFFDIO_REGISTER | | |
65 | (__u64)1 << _UFFDIO_UNREGISTER; | |
66 | if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) { | |
67 | g_test_message("Skipping test: Missing userfault feature"); | |
68 | return false; | |
69 | } | |
70 | ||
71 | return true; | |
72 | } | |
73 | ||
74 | #else | |
75 | static bool ufd_version_check(void) | |
76 | { | |
77 | g_test_message("Skipping test: Userfault not available (builtdtime)"); | |
78 | return false; | |
79 | } | |
80 | ||
81 | #endif | |
82 | ||
83 | static const char *tmpfs; | |
84 | ||
e51e711b WH |
85 | /* The boot file modifies memory area in [start_address, end_address) |
86 | * repeatedly. It outputs a 'B' at a fixed rate while it's still running. | |
ea0c6d62 | 87 | */ |
d54927ef | 88 | #include "tests/migration/i386/a-b-bootblock.h" |
c02b3781 | 89 | #include "tests/migration/aarch64/a-b-kernel.h" |
ea0c6d62 | 90 | |
c02b3781 | 91 | static void init_bootfile(const char *bootpath, void *content) |
aaf89c8a | 92 | { |
93 | FILE *bootfile = fopen(bootpath, "wb"); | |
94 | ||
c02b3781 | 95 | g_assert_cmpint(fwrite(content, 512, 1, bootfile), ==, 1); |
aaf89c8a | 96 | fclose(bootfile); |
97 | } | |
98 | ||
5571dc82 TH |
99 | #include "tests/migration/s390x/a-b-bios.h" |
100 | ||
101 | static void init_bootfile_s390x(const char *bootpath) | |
102 | { | |
103 | FILE *bootfile = fopen(bootpath, "wb"); | |
104 | size_t len = sizeof(s390x_elf); | |
105 | ||
106 | g_assert_cmpint(fwrite(s390x_elf, len, 1, bootfile), ==, 1); | |
107 | fclose(bootfile); | |
108 | } | |
109 | ||
ea0c6d62 DDAG |
110 | /* |
111 | * Wait for some output in the serial output file, | |
112 | * we get an 'A' followed by an endless string of 'B's | |
113 | * but on the destination we won't have the A. | |
114 | */ | |
115 | static void wait_for_serial(const char *side) | |
116 | { | |
117 | char *serialpath = g_strdup_printf("%s/%s", tmpfs, side); | |
118 | FILE *serialfile = fopen(serialpath, "r"); | |
aaf89c8a | 119 | const char *arch = qtest_get_arch(); |
120 | int started = (strcmp(side, "src_serial") == 0 && | |
121 | strcmp(arch, "ppc64") == 0) ? 0 : 1; | |
ea0c6d62 | 122 | |
e2dd21e5 | 123 | g_free(serialpath); |
ea0c6d62 DDAG |
124 | do { |
125 | int readvalue = fgetc(serialfile); | |
126 | ||
aaf89c8a | 127 | if (!started) { |
128 | /* SLOF prints its banner before starting test, | |
129 | * to ignore it, mark the start of the test with '_', | |
130 | * ignore all characters until this marker | |
131 | */ | |
132 | switch (readvalue) { | |
133 | case '_': | |
134 | started = 1; | |
135 | break; | |
136 | case EOF: | |
137 | fseek(serialfile, 0, SEEK_SET); | |
138 | usleep(1000); | |
139 | break; | |
140 | } | |
141 | continue; | |
142 | } | |
ea0c6d62 DDAG |
143 | switch (readvalue) { |
144 | case 'A': | |
145 | /* Fine */ | |
146 | break; | |
147 | ||
148 | case 'B': | |
149 | /* It's alive! */ | |
150 | fclose(serialfile); | |
ea0c6d62 DDAG |
151 | return; |
152 | ||
153 | case EOF: | |
aaf89c8a | 154 | started = (strcmp(side, "src_serial") == 0 && |
155 | strcmp(arch, "ppc64") == 0) ? 0 : 1; | |
ea0c6d62 DDAG |
156 | fseek(serialfile, 0, SEEK_SET); |
157 | usleep(1000); | |
158 | break; | |
159 | ||
160 | default: | |
161 | fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side); | |
162 | g_assert_not_reached(); | |
163 | } | |
164 | } while (true); | |
165 | } | |
166 | ||
3cd46d42 MA |
167 | static void stop_cb(void *opaque, const char *name, QDict *data) |
168 | { | |
169 | if (!strcmp(name, "STOP")) { | |
170 | got_stop = true; | |
171 | } | |
172 | } | |
173 | ||
ea0c6d62 DDAG |
174 | /* |
175 | * Events can get in the way of responses we are actually waiting for. | |
176 | */ | |
b7281c69 | 177 | GCC_FMT_ATTR(2, 3) |
4399596b | 178 | static QDict *wait_command(QTestState *who, const char *command, ...) |
ea0c6d62 | 179 | { |
4399596b MA |
180 | va_list ap; |
181 | ||
182 | va_start(ap, command); | |
183 | qtest_qmp_vsend(who, command, ap); | |
184 | va_end(ap); | |
185 | ||
3cd46d42 | 186 | return qtest_qmp_receive_success(who, stop_cb, NULL); |
ea0c6d62 DDAG |
187 | } |
188 | ||
2f7074c6 PX |
189 | /* |
190 | * Note: caller is responsible to free the returned object via | |
191 | * qobject_unref() after use | |
192 | */ | |
193 | static QDict *migrate_query(QTestState *who) | |
194 | { | |
e1454165 | 195 | return wait_command(who, "{ 'execute': 'query-migrate' }"); |
2f7074c6 PX |
196 | } |
197 | ||
198 | /* | |
199 | * Note: caller is responsible to free the returned object via | |
200 | * g_free() after use | |
201 | */ | |
202 | static gchar *migrate_query_status(QTestState *who) | |
203 | { | |
204 | QDict *rsp_return = migrate_query(who); | |
205 | gchar *status = g_strdup(qdict_get_str(rsp_return, "status")); | |
206 | ||
207 | g_assert(status); | |
208 | qobject_unref(rsp_return); | |
209 | ||
210 | return status; | |
211 | } | |
ea0c6d62 DDAG |
212 | |
213 | /* | |
214 | * It's tricky to use qemu's migration event capability with qtest, | |
215 | * events suddenly appearing confuse the qmp()/hmp() responses. | |
ea0c6d62 DDAG |
216 | */ |
217 | ||
660a9b68 | 218 | static int64_t read_ram_property_int(QTestState *who, const char *property) |
ea0c6d62 | 219 | { |
2f7074c6 | 220 | QDict *rsp_return, *rsp_ram; |
660a9b68 | 221 | int64_t result; |
ea0c6d62 | 222 | |
2f7074c6 | 223 | rsp_return = migrate_query(who); |
ea0c6d62 DDAG |
224 | if (!qdict_haskey(rsp_return, "ram")) { |
225 | /* Still in setup */ | |
226 | result = 0; | |
227 | } else { | |
228 | rsp_ram = qdict_get_qdict(rsp_return, "ram"); | |
660a9b68 | 229 | result = qdict_get_try_int(rsp_ram, property, 0); |
ea0c6d62 | 230 | } |
2f7074c6 | 231 | qobject_unref(rsp_return); |
ea0c6d62 DDAG |
232 | return result; |
233 | } | |
234 | ||
660a9b68 YK |
235 | static uint64_t get_migration_pass(QTestState *who) |
236 | { | |
237 | return read_ram_property_int(who, "dirty-sync-count"); | |
238 | } | |
239 | ||
346f3dab AP |
240 | static void read_blocktime(QTestState *who) |
241 | { | |
2f7074c6 | 242 | QDict *rsp_return; |
346f3dab | 243 | |
2f7074c6 | 244 | rsp_return = migrate_query(who); |
346f3dab | 245 | g_assert(qdict_haskey(rsp_return, "postcopy-blocktime")); |
2f7074c6 | 246 | qobject_unref(rsp_return); |
346f3dab AP |
247 | } |
248 | ||
2f6d3138 PX |
249 | static void wait_for_migration_status(QTestState *who, |
250 | const char *goal) | |
ea0c6d62 | 251 | { |
6a7724e9 | 252 | while (true) { |
6a7724e9 | 253 | bool completed; |
2f7074c6 | 254 | char *status; |
ea0c6d62 | 255 | |
2f7074c6 | 256 | status = migrate_query_status(who); |
2f6d3138 | 257 | completed = strcmp(status, goal) == 0; |
ea0c6d62 | 258 | g_assert_cmpstr(status, !=, "failed"); |
2f7074c6 | 259 | g_free(status); |
6a7724e9 JQ |
260 | if (completed) { |
261 | return; | |
262 | } | |
263 | usleep(1000); | |
264 | } | |
ea0c6d62 DDAG |
265 | } |
266 | ||
2f6d3138 PX |
267 | static void wait_for_migration_complete(QTestState *who) |
268 | { | |
269 | wait_for_migration_status(who, "completed"); | |
270 | } | |
271 | ||
863e27a8 | 272 | static void wait_for_migration_pass(QTestState *who) |
ea0c6d62 | 273 | { |
863e27a8 | 274 | uint64_t initial_pass = get_migration_pass(who); |
ea0c6d62 DDAG |
275 | uint64_t pass; |
276 | ||
277 | /* Wait for the 1st sync */ | |
6a7724e9 JQ |
278 | while (!got_stop && !initial_pass) { |
279 | usleep(1000); | |
863e27a8 | 280 | initial_pass = get_migration_pass(who); |
6a7724e9 | 281 | } |
ea0c6d62 DDAG |
282 | |
283 | do { | |
6a7724e9 | 284 | usleep(1000); |
863e27a8 | 285 | pass = get_migration_pass(who); |
ea0c6d62 DDAG |
286 | } while (pass == initial_pass && !got_stop); |
287 | } | |
288 | ||
7195a871 | 289 | static void check_guests_ram(QTestState *who) |
ea0c6d62 DDAG |
290 | { |
291 | /* Our ASM test will have been incrementing one byte from each page from | |
e51e711b WH |
292 | * start_address to < end_address in order. This gives us a constraint |
293 | * that any page's byte should be equal or less than the previous pages | |
294 | * byte (mod 256); and they should all be equal except for one transition | |
295 | * at the point where we meet the incrementer. (We're running this with | |
296 | * the guest stopped). | |
ea0c6d62 DDAG |
297 | */ |
298 | unsigned address; | |
299 | uint8_t first_byte; | |
300 | uint8_t last_byte; | |
301 | bool hit_edge = false; | |
302 | bool bad = false; | |
303 | ||
7195a871 | 304 | qtest_memread(who, start_address, &first_byte, 1); |
ea0c6d62 DDAG |
305 | last_byte = first_byte; |
306 | ||
e51e711b WH |
307 | for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address; |
308 | address += TEST_MEM_PAGE_SIZE) | |
ea0c6d62 DDAG |
309 | { |
310 | uint8_t b; | |
7195a871 | 311 | qtest_memread(who, address, &b, 1); |
ea0c6d62 DDAG |
312 | if (b != last_byte) { |
313 | if (((b + 1) % 256) == last_byte && !hit_edge) { | |
314 | /* This is OK, the guest stopped at the point of | |
315 | * incrementing the previous page but didn't get | |
316 | * to us yet. | |
317 | */ | |
318 | hit_edge = true; | |
829db8b4 | 319 | last_byte = b; |
ea0c6d62 DDAG |
320 | } else { |
321 | fprintf(stderr, "Memory content inconsistency at %x" | |
322 | " first_byte = %x last_byte = %x current = %x" | |
323 | " hit_edge = %x\n", | |
324 | address, first_byte, last_byte, b, hit_edge); | |
325 | bad = true; | |
326 | } | |
327 | } | |
ea0c6d62 DDAG |
328 | } |
329 | g_assert_false(bad); | |
330 | } | |
331 | ||
332 | static void cleanup(const char *filename) | |
333 | { | |
334 | char *path = g_strdup_printf("%s/%s", tmpfs, filename); | |
335 | ||
336 | unlink(path); | |
e2dd21e5 | 337 | g_free(path); |
ea0c6d62 DDAG |
338 | } |
339 | ||
660a9b68 YK |
340 | static char *get_shmem_opts(const char *mem_size, const char *shmem_path) |
341 | { | |
342 | return g_strdup_printf("-object memory-backend-file,id=mem0,size=%s" | |
343 | ",mem-path=%s,share=on -numa node,memdev=mem0", | |
344 | mem_size, shmem_path); | |
345 | } | |
346 | ||
56b4a42a | 347 | static void migrate_check_parameter(QTestState *who, const char *parameter, |
c44a56d8 | 348 | long long value) |
56b4a42a | 349 | { |
e1454165 | 350 | QDict *rsp_return; |
56b4a42a | 351 | |
e1454165 MA |
352 | rsp_return = wait_command(who, |
353 | "{ 'execute': 'query-migrate-parameters' }"); | |
c44a56d8 | 354 | g_assert_cmpint(qdict_get_int(rsp_return, parameter), ==, value); |
e1454165 | 355 | qobject_unref(rsp_return); |
56b4a42a JQ |
356 | } |
357 | ||
1f90d797 | 358 | static void migrate_set_parameter(QTestState *who, const char *parameter, |
c44a56d8 | 359 | long long value) |
d62fbe60 JQ |
360 | { |
361 | QDict *rsp; | |
d62fbe60 | 362 | |
c44a56d8 MA |
363 | rsp = qtest_qmp(who, |
364 | "{ 'execute': 'migrate-set-parameters'," | |
365 | "'arguments': { %s: %lld } }", | |
366 | parameter, value); | |
d62fbe60 | 367 | g_assert(qdict_haskey(rsp, "return")); |
cb3e7f08 | 368 | qobject_unref(rsp); |
1f90d797 | 369 | migrate_check_parameter(who, parameter, value); |
d62fbe60 JQ |
370 | } |
371 | ||
d5f49640 PX |
372 | static void migrate_pause(QTestState *who) |
373 | { | |
374 | QDict *rsp; | |
375 | ||
376 | rsp = wait_command(who, "{ 'execute': 'migrate-pause' }"); | |
d5f49640 PX |
377 | qobject_unref(rsp); |
378 | } | |
379 | ||
380 | static void migrate_recover(QTestState *who, const char *uri) | |
381 | { | |
382 | QDict *rsp; | |
d5f49640 | 383 | |
b7281c69 MA |
384 | rsp = wait_command(who, |
385 | "{ 'execute': 'migrate-recover', " | |
386 | " 'id': 'recover-cmd', " | |
387 | " 'arguments': { 'uri': %s } }", | |
388 | uri); | |
d5f49640 PX |
389 | qobject_unref(rsp); |
390 | } | |
391 | ||
d62fbe60 | 392 | static void migrate_set_capability(QTestState *who, const char *capability, |
c44a56d8 | 393 | bool value) |
d62fbe60 JQ |
394 | { |
395 | QDict *rsp; | |
c44a56d8 MA |
396 | |
397 | rsp = qtest_qmp(who, | |
398 | "{ 'execute': 'migrate-set-capabilities'," | |
399 | "'arguments': { " | |
400 | "'capabilities': [ { " | |
401 | "'capability': %s, 'state': %i } ] } }", | |
402 | capability, value); | |
d62fbe60 | 403 | g_assert(qdict_haskey(rsp, "return")); |
cb3e7f08 | 404 | qobject_unref(rsp); |
d62fbe60 JQ |
405 | } |
406 | ||
b5bbd3f3 MA |
407 | /* |
408 | * Send QMP command "migrate". | |
409 | * Arguments are built from @fmt... (formatted like | |
410 | * qobject_from_jsonf_nofail()) with "uri": @uri spliced in. | |
411 | */ | |
412 | GCC_FMT_ATTR(3, 4) | |
413 | static void migrate(QTestState *who, const char *uri, const char *fmt, ...) | |
d62fbe60 | 414 | { |
b5bbd3f3 MA |
415 | va_list ap; |
416 | QDict *args, *rsp; | |
d62fbe60 | 417 | |
b5bbd3f3 MA |
418 | va_start(ap, fmt); |
419 | args = qdict_from_vjsonf_nofail(fmt, ap); | |
420 | va_end(ap); | |
421 | ||
422 | g_assert(!qdict_haskey(args, "uri")); | |
423 | qdict_put_str(args, "uri", uri); | |
424 | ||
425 | rsp = qmp("{ 'execute': 'migrate', 'arguments': %p}", args); | |
d62fbe60 | 426 | g_assert(qdict_haskey(rsp, "return")); |
cb3e7f08 | 427 | qobject_unref(rsp); |
d62fbe60 JQ |
428 | } |
429 | ||
d131662a | 430 | static void migrate_postcopy_start(QTestState *from, QTestState *to) |
eb665d7d JQ |
431 | { |
432 | QDict *rsp; | |
433 | ||
d131662a | 434 | rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }"); |
cb3e7f08 | 435 | qobject_unref(rsp); |
d131662a PX |
436 | |
437 | if (!got_stop) { | |
438 | qtest_qmp_eventwait(from, "STOP"); | |
439 | } | |
440 | ||
441 | qtest_qmp_eventwait(to, "RESUME"); | |
eb665d7d JQ |
442 | } |
443 | ||
5fd4a9c9 | 444 | static int test_migrate_start(QTestState **from, QTestState **to, |
660a9b68 YK |
445 | const char *uri, bool hide_stderr, |
446 | bool use_shmem) | |
ea0c6d62 | 447 | { |
d62fbe60 | 448 | gchar *cmd_src, *cmd_dst; |
660a9b68 YK |
449 | char *bootpath = NULL; |
450 | char *extra_opts = NULL; | |
451 | char *shmem_path = NULL; | |
aaf89c8a | 452 | const char *arch = qtest_get_arch(); |
63b2d935 | 453 | const char *accel = "kvm:tcg"; |
ea0c6d62 | 454 | |
660a9b68 YK |
455 | if (use_shmem) { |
456 | if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) { | |
457 | g_test_skip("/dev/shm is not supported"); | |
458 | return -1; | |
459 | } | |
460 | shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid()); | |
461 | } | |
ea0c6d62 | 462 | |
660a9b68 YK |
463 | got_stop = false; |
464 | bootpath = g_strdup_printf("%s/bootsect", tmpfs); | |
aaf89c8a | 465 | if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { |
c02b3781 | 466 | init_bootfile(bootpath, x86_bootsect); |
660a9b68 | 467 | extra_opts = use_shmem ? get_shmem_opts("150M", shmem_path) : NULL; |
63b2d935 | 468 | cmd_src = g_strdup_printf("-machine accel=%s -m 150M" |
31a6bb74 | 469 | " -name source,debug-threads=on" |
aaf89c8a | 470 | " -serial file:%s/src_serial" |
660a9b68 YK |
471 | " -drive file=%s,format=raw %s", |
472 | accel, tmpfs, bootpath, | |
473 | extra_opts ? extra_opts : ""); | |
63b2d935 | 474 | cmd_dst = g_strdup_printf("-machine accel=%s -m 150M" |
31a6bb74 | 475 | " -name target,debug-threads=on" |
aaf89c8a | 476 | " -serial file:%s/dest_serial" |
477 | " -drive file=%s,format=raw" | |
660a9b68 YK |
478 | " -incoming %s %s", |
479 | accel, tmpfs, bootpath, uri, | |
480 | extra_opts ? extra_opts : ""); | |
e51e711b WH |
481 | start_address = X86_TEST_MEM_START; |
482 | end_address = X86_TEST_MEM_END; | |
5571dc82 TH |
483 | } else if (g_str_equal(arch, "s390x")) { |
484 | init_bootfile_s390x(bootpath); | |
660a9b68 | 485 | extra_opts = use_shmem ? get_shmem_opts("128M", shmem_path) : NULL; |
5571dc82 TH |
486 | cmd_src = g_strdup_printf("-machine accel=%s -m 128M" |
487 | " -name source,debug-threads=on" | |
660a9b68 YK |
488 | " -serial file:%s/src_serial -bios %s %s", |
489 | accel, tmpfs, bootpath, | |
490 | extra_opts ? extra_opts : ""); | |
5571dc82 TH |
491 | cmd_dst = g_strdup_printf("-machine accel=%s -m 128M" |
492 | " -name target,debug-threads=on" | |
493 | " -serial file:%s/dest_serial -bios %s" | |
660a9b68 YK |
494 | " -incoming %s %s", |
495 | accel, tmpfs, bootpath, uri, | |
496 | extra_opts ? extra_opts : ""); | |
5571dc82 TH |
497 | start_address = S390_TEST_MEM_START; |
498 | end_address = S390_TEST_MEM_END; | |
aaf89c8a | 499 | } else if (strcmp(arch, "ppc64") == 0) { |
660a9b68 | 500 | extra_opts = use_shmem ? get_shmem_opts("256M", shmem_path) : NULL; |
fc71e3e5 | 501 | cmd_src = g_strdup_printf("-machine accel=%s -m 256M -nodefaults" |
31a6bb74 | 502 | " -name source,debug-threads=on" |
aaf89c8a | 503 | " -serial file:%s/src_serial" |
fc71e3e5 TH |
504 | " -prom-env 'use-nvramrc?=true' -prom-env " |
505 | "'nvramrc=hex .\" _\" begin %x %x " | |
cdf33815 | 506 | "do i c@ 1 + i c! 1000 +loop .\" B\" 0 " |
660a9b68 YK |
507 | "until' %s", accel, tmpfs, end_address, |
508 | start_address, extra_opts ? extra_opts : ""); | |
171da9d5 | 509 | cmd_dst = g_strdup_printf("-machine accel=%s -m 256M" |
31a6bb74 | 510 | " -name target,debug-threads=on" |
aaf89c8a | 511 | " -serial file:%s/dest_serial" |
660a9b68 YK |
512 | " -incoming %s %s", |
513 | accel, tmpfs, uri, | |
514 | extra_opts ? extra_opts : ""); | |
e51e711b WH |
515 | |
516 | start_address = PPC_TEST_MEM_START; | |
517 | end_address = PPC_TEST_MEM_END; | |
c02b3781 WH |
518 | } else if (strcmp(arch, "aarch64") == 0) { |
519 | init_bootfile(bootpath, aarch64_kernel); | |
660a9b68 | 520 | extra_opts = use_shmem ? get_shmem_opts("150M", shmem_path) : NULL; |
c02b3781 WH |
521 | cmd_src = g_strdup_printf("-machine virt,accel=%s,gic-version=max " |
522 | "-name vmsource,debug-threads=on -cpu max " | |
523 | "-m 150M -serial file:%s/src_serial " | |
660a9b68 YK |
524 | "-kernel %s %s", |
525 | accel, tmpfs, bootpath, | |
526 | extra_opts ? extra_opts : ""); | |
c02b3781 WH |
527 | cmd_dst = g_strdup_printf("-machine virt,accel=%s,gic-version=max " |
528 | "-name vmdest,debug-threads=on -cpu max " | |
529 | "-m 150M -serial file:%s/dest_serial " | |
530 | "-kernel %s " | |
660a9b68 YK |
531 | "-incoming %s %s", |
532 | accel, tmpfs, bootpath, uri, | |
533 | extra_opts ? extra_opts : ""); | |
c02b3781 WH |
534 | |
535 | start_address = ARM_TEST_MEM_START; | |
536 | end_address = ARM_TEST_MEM_END; | |
537 | ||
538 | g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE); | |
aaf89c8a | 539 | } else { |
540 | g_assert_not_reached(); | |
541 | } | |
542 | ||
e2dd21e5 | 543 | g_free(bootpath); |
660a9b68 | 544 | g_free(extra_opts); |
e2dd21e5 | 545 | |
f96d6651 DDAG |
546 | if (hide_stderr) { |
547 | gchar *tmp; | |
548 | tmp = g_strdup_printf("%s 2>/dev/null", cmd_src); | |
549 | g_free(cmd_src); | |
550 | cmd_src = tmp; | |
551 | ||
552 | tmp = g_strdup_printf("%s 2>/dev/null", cmd_dst); | |
553 | g_free(cmd_dst); | |
554 | cmd_dst = tmp; | |
555 | } | |
556 | ||
7195a871 | 557 | *from = qtest_start(cmd_src); |
aaf89c8a | 558 | g_free(cmd_src); |
559 | ||
7195a871 | 560 | *to = qtest_init(cmd_dst); |
aaf89c8a | 561 | g_free(cmd_dst); |
660a9b68 YK |
562 | |
563 | /* | |
564 | * Remove shmem file immediately to avoid memory leak in test failed case. | |
565 | * It's valid becase QEMU has already opened this file | |
566 | */ | |
567 | if (use_shmem) { | |
568 | unlink(shmem_path); | |
569 | g_free(shmem_path); | |
570 | } | |
571 | ||
5fd4a9c9 | 572 | return 0; |
7195a871 JQ |
573 | } |
574 | ||
2c9bb297 | 575 | static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest) |
7195a871 JQ |
576 | { |
577 | unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d; | |
578 | ||
579 | qtest_quit(from); | |
580 | ||
2c9bb297 DDAG |
581 | if (test_dest) { |
582 | qtest_memread(to, start_address, &dest_byte_a, 1); | |
7195a871 | 583 | |
2c9bb297 DDAG |
584 | /* Destination still running, wait for a byte to change */ |
585 | do { | |
586 | qtest_memread(to, start_address, &dest_byte_b, 1); | |
587 | usleep(1000 * 10); | |
588 | } while (dest_byte_a == dest_byte_b); | |
589 | ||
590 | qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}"); | |
7195a871 | 591 | |
2c9bb297 DDAG |
592 | /* With it stopped, check nothing changes */ |
593 | qtest_memread(to, start_address, &dest_byte_c, 1); | |
594 | usleep(1000 * 200); | |
595 | qtest_memread(to, start_address, &dest_byte_d, 1); | |
596 | g_assert_cmpint(dest_byte_c, ==, dest_byte_d); | |
7195a871 | 597 | |
2c9bb297 DDAG |
598 | check_guests_ram(to); |
599 | } | |
7195a871 JQ |
600 | |
601 | qtest_quit(to); | |
602 | ||
603 | cleanup("bootsect"); | |
604 | cleanup("migsocket"); | |
605 | cleanup("src_serial"); | |
606 | cleanup("dest_serial"); | |
607 | } | |
608 | ||
4c27486d JQ |
609 | static void deprecated_set_downtime(QTestState *who, const double value) |
610 | { | |
611 | QDict *rsp; | |
4c27486d | 612 | |
015715f5 MA |
613 | rsp = qtest_qmp(who, |
614 | "{ 'execute': 'migrate_set_downtime'," | |
615 | " 'arguments': { 'value': %f } }", value); | |
4c27486d | 616 | g_assert(qdict_haskey(rsp, "return")); |
cb3e7f08 | 617 | qobject_unref(rsp); |
c44a56d8 | 618 | migrate_check_parameter(who, "downtime-limit", value * 1000); |
4c27486d JQ |
619 | } |
620 | ||
c44a56d8 | 621 | static void deprecated_set_speed(QTestState *who, long long value) |
4c27486d JQ |
622 | { |
623 | QDict *rsp; | |
4c27486d | 624 | |
c44a56d8 MA |
625 | rsp = qtest_qmp(who, "{ 'execute': 'migrate_set_speed'," |
626 | "'arguments': { 'value': %lld } }", value); | |
4c27486d | 627 | g_assert(qdict_haskey(rsp, "return")); |
cb3e7f08 | 628 | qobject_unref(rsp); |
4c27486d JQ |
629 | migrate_check_parameter(who, "max-bandwidth", value); |
630 | } | |
631 | ||
cdf84229 JQ |
632 | static void deprecated_set_cache_size(QTestState *who, long long value) |
633 | { | |
634 | QDict *rsp; | |
635 | ||
636 | rsp = qtest_qmp(who, "{ 'execute': 'migrate-set-cache-size'," | |
637 | "'arguments': { 'value': %lld } }", value); | |
638 | g_assert(qdict_haskey(rsp, "return")); | |
639 | qobject_unref(rsp); | |
640 | migrate_check_parameter(who, "xbzrle-cache-size", value); | |
641 | } | |
642 | ||
4c27486d JQ |
643 | static void test_deprecated(void) |
644 | { | |
645 | QTestState *from; | |
646 | ||
c02b3781 | 647 | from = qtest_start("-machine none"); |
4c27486d JQ |
648 | |
649 | deprecated_set_downtime(from, 0.12345); | |
c44a56d8 | 650 | deprecated_set_speed(from, 12345); |
cdf84229 | 651 | deprecated_set_cache_size(from, 4096); |
4c27486d JQ |
652 | |
653 | qtest_quit(from); | |
654 | } | |
655 | ||
d131662a | 656 | static int migrate_postcopy_prepare(QTestState **from_ptr, |
3e81f73c PX |
657 | QTestState **to_ptr, |
658 | bool hide_error) | |
7195a871 JQ |
659 | { |
660 | char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); | |
863e27a8 | 661 | QTestState *from, *to; |
7195a871 | 662 | |
660a9b68 | 663 | if (test_migrate_start(&from, &to, uri, hide_error, false)) { |
d131662a | 664 | return -1; |
5fd4a9c9 | 665 | } |
ea0c6d62 | 666 | |
c44a56d8 MA |
667 | migrate_set_capability(from, "postcopy-ram", true); |
668 | migrate_set_capability(to, "postcopy-ram", true); | |
669 | migrate_set_capability(to, "postcopy-blocktime", true); | |
ea0c6d62 DDAG |
670 | |
671 | /* We want to pick a speed slow enough that the test completes | |
672 | * quickly, but that it doesn't complete precopy even on a slow | |
673 | * machine, so also set the downtime. | |
674 | */ | |
c44a56d8 MA |
675 | migrate_set_parameter(from, "max-bandwidth", 100000000); |
676 | migrate_set_parameter(from, "downtime-limit", 1); | |
ea0c6d62 DDAG |
677 | |
678 | /* Wait for the first serial output from the source */ | |
679 | wait_for_serial("src_serial"); | |
680 | ||
b5bbd3f3 | 681 | migrate(from, uri, "{}"); |
d131662a | 682 | g_free(uri); |
ea0c6d62 | 683 | |
863e27a8 | 684 | wait_for_migration_pass(from); |
ea0c6d62 | 685 | |
d131662a PX |
686 | *from_ptr = from; |
687 | *to_ptr = to; | |
ea0c6d62 | 688 | |
d131662a PX |
689 | return 0; |
690 | } | |
ea0c6d62 | 691 | |
d131662a PX |
692 | static void migrate_postcopy_complete(QTestState *from, QTestState *to) |
693 | { | |
694 | wait_for_migration_complete(from); | |
ea0c6d62 | 695 | |
d131662a | 696 | /* Make sure we get at least one "B" on destination */ |
ea0c6d62 | 697 | wait_for_serial("dest_serial"); |
ea0c6d62 | 698 | |
346f3dab AP |
699 | if (uffd_feature_thread_id) { |
700 | read_blocktime(to); | |
701 | } | |
ea0c6d62 | 702 | |
2c9bb297 DDAG |
703 | test_migrate_end(from, to, true); |
704 | } | |
705 | ||
d131662a PX |
706 | static void test_postcopy(void) |
707 | { | |
708 | QTestState *from, *to; | |
709 | ||
3e81f73c | 710 | if (migrate_postcopy_prepare(&from, &to, false)) { |
d131662a PX |
711 | return; |
712 | } | |
713 | migrate_postcopy_start(from, to); | |
714 | migrate_postcopy_complete(from, to); | |
715 | } | |
716 | ||
d5f49640 PX |
717 | static void test_postcopy_recovery(void) |
718 | { | |
719 | QTestState *from, *to; | |
720 | char *uri; | |
721 | ||
3e81f73c | 722 | if (migrate_postcopy_prepare(&from, &to, true)) { |
d5f49640 PX |
723 | return; |
724 | } | |
725 | ||
726 | /* Turn postcopy speed down, 4K/s is slow enough on any machines */ | |
c44a56d8 | 727 | migrate_set_parameter(from, "max-postcopy-bandwidth", 4096); |
d5f49640 PX |
728 | |
729 | /* Now we start the postcopy */ | |
730 | migrate_postcopy_start(from, to); | |
731 | ||
732 | /* | |
733 | * Wait until postcopy is really started; we can only run the | |
734 | * migrate-pause command during a postcopy | |
735 | */ | |
736 | wait_for_migration_status(from, "postcopy-active"); | |
737 | ||
738 | /* | |
739 | * Manually stop the postcopy migration. This emulates a network | |
740 | * failure with the migration socket | |
741 | */ | |
742 | migrate_pause(from); | |
743 | ||
744 | /* | |
745 | * Wait for destination side to reach postcopy-paused state. The | |
746 | * migrate-recover command can only succeed if destination machine | |
747 | * is in the paused state | |
748 | */ | |
749 | wait_for_migration_status(to, "postcopy-paused"); | |
750 | ||
751 | /* | |
752 | * Create a new socket to emulate a new channel that is different | |
753 | * from the broken migration channel; tell the destination to | |
754 | * listen to the new port | |
755 | */ | |
756 | uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs); | |
757 | migrate_recover(to, uri); | |
758 | ||
759 | /* | |
760 | * Try to rebuild the migration channel using the resume flag and | |
761 | * the newly created channel | |
762 | */ | |
763 | wait_for_migration_status(from, "postcopy-paused"); | |
b5bbd3f3 | 764 | migrate(from, uri, "{'resume': true}"); |
d5f49640 PX |
765 | g_free(uri); |
766 | ||
767 | /* Restore the postcopy bandwidth to unlimited */ | |
c44a56d8 | 768 | migrate_set_parameter(from, "max-postcopy-bandwidth", 0); |
d5f49640 PX |
769 | |
770 | migrate_postcopy_complete(from, to); | |
771 | } | |
772 | ||
2c9bb297 DDAG |
773 | static void test_baddest(void) |
774 | { | |
775 | QTestState *from, *to; | |
e1454165 | 776 | QDict *rsp_return; |
2f7074c6 | 777 | char *status; |
2c9bb297 DDAG |
778 | bool failed; |
779 | ||
660a9b68 | 780 | if (test_migrate_start(&from, &to, "tcp:0:0", true, false)) { |
5fd4a9c9 DDAG |
781 | return; |
782 | } | |
b5bbd3f3 | 783 | migrate(from, "tcp:0:0", "{}"); |
2c9bb297 | 784 | do { |
2f7074c6 | 785 | status = migrate_query_status(from); |
2c9bb297 DDAG |
786 | g_assert(!strcmp(status, "setup") || !(strcmp(status, "failed"))); |
787 | failed = !strcmp(status, "failed"); | |
2f7074c6 | 788 | g_free(status); |
2c9bb297 DDAG |
789 | } while (!failed); |
790 | ||
791 | /* Is the machine currently running? */ | |
e1454165 | 792 | rsp_return = wait_command(from, "{ 'execute': 'query-status' }"); |
2c9bb297 DDAG |
793 | g_assert(qdict_haskey(rsp_return, "running")); |
794 | g_assert(qdict_get_bool(rsp_return, "running")); | |
e1454165 | 795 | qobject_unref(rsp_return); |
2c9bb297 DDAG |
796 | |
797 | test_migrate_end(from, to, false); | |
ea0c6d62 DDAG |
798 | } |
799 | ||
2884100c JQ |
800 | static void test_precopy_unix(void) |
801 | { | |
802 | char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); | |
803 | QTestState *from, *to; | |
804 | ||
660a9b68 | 805 | if (test_migrate_start(&from, &to, uri, false, false)) { |
5fd4a9c9 DDAG |
806 | return; |
807 | } | |
2884100c JQ |
808 | |
809 | /* We want to pick a speed slow enough that the test completes | |
810 | * quickly, but that it doesn't complete precopy even on a slow | |
811 | * machine, so also set the downtime. | |
812 | */ | |
813 | /* 1 ms should make it not converge*/ | |
c44a56d8 | 814 | migrate_set_parameter(from, "downtime-limit", 1); |
2884100c | 815 | /* 1GB/s */ |
c44a56d8 | 816 | migrate_set_parameter(from, "max-bandwidth", 1000000000); |
2884100c JQ |
817 | |
818 | /* Wait for the first serial output from the source */ | |
819 | wait_for_serial("src_serial"); | |
820 | ||
b5bbd3f3 | 821 | migrate(from, uri, "{}"); |
2884100c JQ |
822 | |
823 | wait_for_migration_pass(from); | |
824 | ||
825 | /* 300 ms should converge */ | |
c44a56d8 | 826 | migrate_set_parameter(from, "downtime-limit", 300); |
2884100c JQ |
827 | |
828 | if (!got_stop) { | |
829 | qtest_qmp_eventwait(from, "STOP"); | |
830 | } | |
831 | ||
832 | qtest_qmp_eventwait(to, "RESUME"); | |
833 | ||
834 | wait_for_serial("dest_serial"); | |
835 | wait_for_migration_complete(from); | |
836 | ||
837 | test_migrate_end(from, to, true); | |
838 | g_free(uri); | |
839 | } | |
840 | ||
660a9b68 YK |
841 | #if 0 |
842 | /* Currently upset on aarch64 TCG */ | |
843 | static void test_ignore_shared(void) | |
844 | { | |
845 | char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); | |
846 | QTestState *from, *to; | |
847 | ||
848 | if (test_migrate_start(&from, &to, uri, false, true)) { | |
849 | return; | |
850 | } | |
851 | ||
852 | migrate_set_capability(from, "x-ignore-shared", true); | |
853 | migrate_set_capability(to, "x-ignore-shared", true); | |
854 | ||
855 | /* Wait for the first serial output from the source */ | |
856 | wait_for_serial("src_serial"); | |
857 | ||
858 | migrate(from, uri, "{}"); | |
859 | ||
860 | wait_for_migration_pass(from); | |
861 | ||
862 | if (!got_stop) { | |
863 | qtest_qmp_eventwait(from, "STOP"); | |
864 | } | |
865 | ||
866 | qtest_qmp_eventwait(to, "RESUME"); | |
867 | ||
868 | wait_for_serial("dest_serial"); | |
869 | wait_for_migration_complete(from); | |
870 | ||
871 | /* Check whether shared RAM has been really skipped */ | |
872 | g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024); | |
873 | ||
874 | test_migrate_end(from, to, true); | |
875 | g_free(uri); | |
876 | } | |
877 | #endif | |
878 | ||
cdf84229 JQ |
879 | static void test_xbzrle(const char *uri) |
880 | { | |
881 | QTestState *from, *to; | |
882 | ||
883 | if (test_migrate_start(&from, &to, uri, false, false)) { | |
884 | return; | |
885 | } | |
886 | ||
887 | /* | |
888 | * We want to pick a speed slow enough that the test completes | |
889 | * quickly, but that it doesn't complete precopy even on a slow | |
890 | * machine, so also set the downtime. | |
891 | */ | |
892 | /* 1 ms should make it not converge*/ | |
893 | migrate_set_parameter(from, "downtime-limit", 1); | |
894 | /* 1GB/s */ | |
895 | migrate_set_parameter(from, "max-bandwidth", 1000000000); | |
896 | ||
897 | migrate_set_parameter(from, "xbzrle-cache-size", 33554432); | |
898 | ||
899 | migrate_set_capability(from, "xbzrle", "true"); | |
900 | migrate_set_capability(to, "xbzrle", "true"); | |
901 | /* Wait for the first serial output from the source */ | |
902 | wait_for_serial("src_serial"); | |
903 | ||
904 | migrate(from, uri, "{}"); | |
905 | ||
906 | wait_for_migration_pass(from); | |
907 | ||
908 | /* 300ms should converge */ | |
909 | migrate_set_parameter(from, "downtime-limit", 300); | |
910 | ||
911 | if (!got_stop) { | |
912 | qtest_qmp_eventwait(from, "STOP"); | |
913 | } | |
914 | qtest_qmp_eventwait(to, "RESUME"); | |
915 | ||
916 | wait_for_serial("dest_serial"); | |
917 | wait_for_migration_complete(from); | |
918 | ||
919 | test_migrate_end(from, to, true); | |
920 | } | |
921 | ||
922 | static void test_xbzrle_unix(void) | |
923 | { | |
924 | char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); | |
925 | ||
926 | test_xbzrle(uri); | |
927 | g_free(uri); | |
928 | } | |
929 | ||
ea0c6d62 DDAG |
930 | int main(int argc, char **argv) |
931 | { | |
2656bfd9 | 932 | char template[] = "/tmp/migration-test-XXXXXX"; |
ea0c6d62 DDAG |
933 | int ret; |
934 | ||
935 | g_test_init(&argc, &argv, NULL); | |
936 | ||
937 | if (!ufd_version_check()) { | |
4848cb3d | 938 | return g_test_run(); |
ea0c6d62 DDAG |
939 | } |
940 | ||
880b169a TH |
941 | /* |
942 | * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG | |
943 | * is touchy due to race conditions on dirty bits (especially on PPC for | |
944 | * some reason) | |
945 | */ | |
946 | if (g_str_equal(qtest_get_arch(), "ppc64") && | |
947 | access("/sys/module/kvm_hv", F_OK)) { | |
948 | g_test_message("Skipping test: kvm_hv not available"); | |
4848cb3d | 949 | return g_test_run(); |
880b169a TH |
950 | } |
951 | ||
d254b392 TH |
952 | /* |
953 | * Similar to ppc64, s390x seems to be touchy with TCG, so disable it | |
954 | * there until the problems are resolved | |
955 | */ | |
956 | if (g_str_equal(qtest_get_arch(), "s390x")) { | |
957 | #if defined(HOST_S390X) | |
958 | if (access("/dev/kvm", R_OK | W_OK)) { | |
959 | g_test_message("Skipping test: kvm not available"); | |
4848cb3d | 960 | return g_test_run(); |
d254b392 TH |
961 | } |
962 | #else | |
963 | g_test_message("Skipping test: Need s390x host to work properly"); | |
4848cb3d | 964 | return g_test_run(); |
d254b392 TH |
965 | #endif |
966 | } | |
967 | ||
ea0c6d62 DDAG |
968 | tmpfs = mkdtemp(template); |
969 | if (!tmpfs) { | |
970 | g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno)); | |
971 | } | |
972 | g_assert(tmpfs); | |
973 | ||
974 | module_call_init(MODULE_INIT_QOM); | |
975 | ||
2884100c | 976 | qtest_add_func("/migration/postcopy/unix", test_postcopy); |
d5f49640 | 977 | qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery); |
4c27486d | 978 | qtest_add_func("/migration/deprecated", test_deprecated); |
2c9bb297 | 979 | qtest_add_func("/migration/bad_dest", test_baddest); |
2884100c | 980 | qtest_add_func("/migration/precopy/unix", test_precopy_unix); |
660a9b68 | 981 | /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */ |
cdf84229 | 982 | qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix); |
ea0c6d62 DDAG |
983 | |
984 | ret = g_test_run(); | |
985 | ||
986 | g_assert_cmpint(ret, ==, 0); | |
987 | ||
988 | ret = rmdir(tmpfs); | |
989 | if (ret != 0) { | |
990 | g_test_message("unable to rmdir: path (%s): %s\n", | |
991 | tmpfs, strerror(errno)); | |
992 | } | |
993 | ||
994 | return ret; | |
995 | } |