]>
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 | ||
863e27a8 | 218 | static uint64_t get_migration_pass(QTestState *who) |
ea0c6d62 | 219 | { |
2f7074c6 | 220 | QDict *rsp_return, *rsp_ram; |
ea0c6d62 DDAG |
221 | uint64_t result; |
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"); | |
229 | result = qdict_get_try_int(rsp_ram, "dirty-sync-count", 0); | |
ea0c6d62 | 230 | } |
2f7074c6 | 231 | qobject_unref(rsp_return); |
ea0c6d62 DDAG |
232 | return result; |
233 | } | |
234 | ||
346f3dab AP |
235 | static void read_blocktime(QTestState *who) |
236 | { | |
2f7074c6 | 237 | QDict *rsp_return; |
346f3dab | 238 | |
2f7074c6 | 239 | rsp_return = migrate_query(who); |
346f3dab | 240 | g_assert(qdict_haskey(rsp_return, "postcopy-blocktime")); |
2f7074c6 | 241 | qobject_unref(rsp_return); |
346f3dab AP |
242 | } |
243 | ||
2f6d3138 PX |
244 | static void wait_for_migration_status(QTestState *who, |
245 | const char *goal) | |
ea0c6d62 | 246 | { |
6a7724e9 | 247 | while (true) { |
6a7724e9 | 248 | bool completed; |
2f7074c6 | 249 | char *status; |
ea0c6d62 | 250 | |
2f7074c6 | 251 | status = migrate_query_status(who); |
2f6d3138 | 252 | completed = strcmp(status, goal) == 0; |
ea0c6d62 | 253 | g_assert_cmpstr(status, !=, "failed"); |
2f7074c6 | 254 | g_free(status); |
6a7724e9 JQ |
255 | if (completed) { |
256 | return; | |
257 | } | |
258 | usleep(1000); | |
259 | } | |
ea0c6d62 DDAG |
260 | } |
261 | ||
2f6d3138 PX |
262 | static void wait_for_migration_complete(QTestState *who) |
263 | { | |
264 | wait_for_migration_status(who, "completed"); | |
265 | } | |
266 | ||
863e27a8 | 267 | static void wait_for_migration_pass(QTestState *who) |
ea0c6d62 | 268 | { |
863e27a8 | 269 | uint64_t initial_pass = get_migration_pass(who); |
ea0c6d62 DDAG |
270 | uint64_t pass; |
271 | ||
272 | /* Wait for the 1st sync */ | |
6a7724e9 JQ |
273 | while (!got_stop && !initial_pass) { |
274 | usleep(1000); | |
863e27a8 | 275 | initial_pass = get_migration_pass(who); |
6a7724e9 | 276 | } |
ea0c6d62 DDAG |
277 | |
278 | do { | |
6a7724e9 | 279 | usleep(1000); |
863e27a8 | 280 | pass = get_migration_pass(who); |
ea0c6d62 DDAG |
281 | } while (pass == initial_pass && !got_stop); |
282 | } | |
283 | ||
7195a871 | 284 | static void check_guests_ram(QTestState *who) |
ea0c6d62 DDAG |
285 | { |
286 | /* Our ASM test will have been incrementing one byte from each page from | |
e51e711b WH |
287 | * start_address to < end_address in order. This gives us a constraint |
288 | * that any page's byte should be equal or less than the previous pages | |
289 | * byte (mod 256); and they should all be equal except for one transition | |
290 | * at the point where we meet the incrementer. (We're running this with | |
291 | * the guest stopped). | |
ea0c6d62 DDAG |
292 | */ |
293 | unsigned address; | |
294 | uint8_t first_byte; | |
295 | uint8_t last_byte; | |
296 | bool hit_edge = false; | |
297 | bool bad = false; | |
298 | ||
7195a871 | 299 | qtest_memread(who, start_address, &first_byte, 1); |
ea0c6d62 DDAG |
300 | last_byte = first_byte; |
301 | ||
e51e711b WH |
302 | for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address; |
303 | address += TEST_MEM_PAGE_SIZE) | |
ea0c6d62 DDAG |
304 | { |
305 | uint8_t b; | |
7195a871 | 306 | qtest_memread(who, address, &b, 1); |
ea0c6d62 DDAG |
307 | if (b != last_byte) { |
308 | if (((b + 1) % 256) == last_byte && !hit_edge) { | |
309 | /* This is OK, the guest stopped at the point of | |
310 | * incrementing the previous page but didn't get | |
311 | * to us yet. | |
312 | */ | |
313 | hit_edge = true; | |
829db8b4 | 314 | last_byte = b; |
ea0c6d62 DDAG |
315 | } else { |
316 | fprintf(stderr, "Memory content inconsistency at %x" | |
317 | " first_byte = %x last_byte = %x current = %x" | |
318 | " hit_edge = %x\n", | |
319 | address, first_byte, last_byte, b, hit_edge); | |
320 | bad = true; | |
321 | } | |
322 | } | |
ea0c6d62 DDAG |
323 | } |
324 | g_assert_false(bad); | |
325 | } | |
326 | ||
327 | static void cleanup(const char *filename) | |
328 | { | |
329 | char *path = g_strdup_printf("%s/%s", tmpfs, filename); | |
330 | ||
331 | unlink(path); | |
e2dd21e5 | 332 | g_free(path); |
ea0c6d62 DDAG |
333 | } |
334 | ||
56b4a42a | 335 | static void migrate_check_parameter(QTestState *who, const char *parameter, |
c44a56d8 | 336 | long long value) |
56b4a42a | 337 | { |
e1454165 | 338 | QDict *rsp_return; |
56b4a42a | 339 | |
e1454165 MA |
340 | rsp_return = wait_command(who, |
341 | "{ 'execute': 'query-migrate-parameters' }"); | |
c44a56d8 | 342 | g_assert_cmpint(qdict_get_int(rsp_return, parameter), ==, value); |
e1454165 | 343 | qobject_unref(rsp_return); |
56b4a42a JQ |
344 | } |
345 | ||
1f90d797 | 346 | static void migrate_set_parameter(QTestState *who, const char *parameter, |
c44a56d8 | 347 | long long value) |
d62fbe60 JQ |
348 | { |
349 | QDict *rsp; | |
d62fbe60 | 350 | |
c44a56d8 MA |
351 | rsp = qtest_qmp(who, |
352 | "{ 'execute': 'migrate-set-parameters'," | |
353 | "'arguments': { %s: %lld } }", | |
354 | parameter, value); | |
d62fbe60 | 355 | g_assert(qdict_haskey(rsp, "return")); |
cb3e7f08 | 356 | qobject_unref(rsp); |
1f90d797 | 357 | migrate_check_parameter(who, parameter, value); |
d62fbe60 JQ |
358 | } |
359 | ||
d5f49640 PX |
360 | static void migrate_pause(QTestState *who) |
361 | { | |
362 | QDict *rsp; | |
363 | ||
364 | rsp = wait_command(who, "{ 'execute': 'migrate-pause' }"); | |
d5f49640 PX |
365 | qobject_unref(rsp); |
366 | } | |
367 | ||
368 | static void migrate_recover(QTestState *who, const char *uri) | |
369 | { | |
370 | QDict *rsp; | |
d5f49640 | 371 | |
b7281c69 MA |
372 | rsp = wait_command(who, |
373 | "{ 'execute': 'migrate-recover', " | |
374 | " 'id': 'recover-cmd', " | |
375 | " 'arguments': { 'uri': %s } }", | |
376 | uri); | |
d5f49640 PX |
377 | qobject_unref(rsp); |
378 | } | |
379 | ||
d62fbe60 | 380 | static void migrate_set_capability(QTestState *who, const char *capability, |
c44a56d8 | 381 | bool value) |
d62fbe60 JQ |
382 | { |
383 | QDict *rsp; | |
c44a56d8 MA |
384 | |
385 | rsp = qtest_qmp(who, | |
386 | "{ 'execute': 'migrate-set-capabilities'," | |
387 | "'arguments': { " | |
388 | "'capabilities': [ { " | |
389 | "'capability': %s, 'state': %i } ] } }", | |
390 | capability, value); | |
d62fbe60 | 391 | g_assert(qdict_haskey(rsp, "return")); |
cb3e7f08 | 392 | qobject_unref(rsp); |
d62fbe60 JQ |
393 | } |
394 | ||
b5bbd3f3 MA |
395 | /* |
396 | * Send QMP command "migrate". | |
397 | * Arguments are built from @fmt... (formatted like | |
398 | * qobject_from_jsonf_nofail()) with "uri": @uri spliced in. | |
399 | */ | |
400 | GCC_FMT_ATTR(3, 4) | |
401 | static void migrate(QTestState *who, const char *uri, const char *fmt, ...) | |
d62fbe60 | 402 | { |
b5bbd3f3 MA |
403 | va_list ap; |
404 | QDict *args, *rsp; | |
d62fbe60 | 405 | |
b5bbd3f3 MA |
406 | va_start(ap, fmt); |
407 | args = qdict_from_vjsonf_nofail(fmt, ap); | |
408 | va_end(ap); | |
409 | ||
410 | g_assert(!qdict_haskey(args, "uri")); | |
411 | qdict_put_str(args, "uri", uri); | |
412 | ||
413 | rsp = qmp("{ 'execute': 'migrate', 'arguments': %p}", args); | |
d62fbe60 | 414 | g_assert(qdict_haskey(rsp, "return")); |
cb3e7f08 | 415 | qobject_unref(rsp); |
d62fbe60 JQ |
416 | } |
417 | ||
d131662a | 418 | static void migrate_postcopy_start(QTestState *from, QTestState *to) |
eb665d7d JQ |
419 | { |
420 | QDict *rsp; | |
421 | ||
d131662a | 422 | rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }"); |
cb3e7f08 | 423 | qobject_unref(rsp); |
d131662a PX |
424 | |
425 | if (!got_stop) { | |
426 | qtest_qmp_eventwait(from, "STOP"); | |
427 | } | |
428 | ||
429 | qtest_qmp_eventwait(to, "RESUME"); | |
eb665d7d JQ |
430 | } |
431 | ||
5fd4a9c9 | 432 | static int test_migrate_start(QTestState **from, QTestState **to, |
f96d6651 | 433 | const char *uri, bool hide_stderr) |
ea0c6d62 | 434 | { |
d62fbe60 | 435 | gchar *cmd_src, *cmd_dst; |
ea0c6d62 | 436 | char *bootpath = g_strdup_printf("%s/bootsect", tmpfs); |
aaf89c8a | 437 | const char *arch = qtest_get_arch(); |
63b2d935 | 438 | const char *accel = "kvm:tcg"; |
ea0c6d62 DDAG |
439 | |
440 | got_stop = false; | |
ea0c6d62 | 441 | |
aaf89c8a | 442 | if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { |
c02b3781 | 443 | init_bootfile(bootpath, x86_bootsect); |
63b2d935 | 444 | cmd_src = g_strdup_printf("-machine accel=%s -m 150M" |
31a6bb74 | 445 | " -name source,debug-threads=on" |
aaf89c8a | 446 | " -serial file:%s/src_serial" |
447 | " -drive file=%s,format=raw", | |
63b2d935 JQ |
448 | accel, tmpfs, bootpath); |
449 | cmd_dst = g_strdup_printf("-machine accel=%s -m 150M" | |
31a6bb74 | 450 | " -name target,debug-threads=on" |
aaf89c8a | 451 | " -serial file:%s/dest_serial" |
452 | " -drive file=%s,format=raw" | |
453 | " -incoming %s", | |
63b2d935 | 454 | accel, tmpfs, bootpath, uri); |
e51e711b WH |
455 | start_address = X86_TEST_MEM_START; |
456 | end_address = X86_TEST_MEM_END; | |
5571dc82 TH |
457 | } else if (g_str_equal(arch, "s390x")) { |
458 | init_bootfile_s390x(bootpath); | |
459 | cmd_src = g_strdup_printf("-machine accel=%s -m 128M" | |
460 | " -name source,debug-threads=on" | |
461 | " -serial file:%s/src_serial -bios %s", | |
462 | accel, tmpfs, bootpath); | |
463 | cmd_dst = g_strdup_printf("-machine accel=%s -m 128M" | |
464 | " -name target,debug-threads=on" | |
465 | " -serial file:%s/dest_serial -bios %s" | |
466 | " -incoming %s", | |
467 | accel, tmpfs, bootpath, uri); | |
468 | start_address = S390_TEST_MEM_START; | |
469 | end_address = S390_TEST_MEM_END; | |
aaf89c8a | 470 | } else if (strcmp(arch, "ppc64") == 0) { |
fc71e3e5 | 471 | cmd_src = g_strdup_printf("-machine accel=%s -m 256M -nodefaults" |
31a6bb74 | 472 | " -name source,debug-threads=on" |
aaf89c8a | 473 | " -serial file:%s/src_serial" |
fc71e3e5 TH |
474 | " -prom-env 'use-nvramrc?=true' -prom-env " |
475 | "'nvramrc=hex .\" _\" begin %x %x " | |
cdf33815 JQ |
476 | "do i c@ 1 + i c! 1000 +loop .\" B\" 0 " |
477 | "until'", accel, tmpfs, end_address, | |
478 | start_address); | |
171da9d5 | 479 | cmd_dst = g_strdup_printf("-machine accel=%s -m 256M" |
31a6bb74 | 480 | " -name target,debug-threads=on" |
aaf89c8a | 481 | " -serial file:%s/dest_serial" |
482 | " -incoming %s", | |
171da9d5 | 483 | accel, tmpfs, uri); |
e51e711b WH |
484 | |
485 | start_address = PPC_TEST_MEM_START; | |
486 | end_address = PPC_TEST_MEM_END; | |
c02b3781 WH |
487 | } else if (strcmp(arch, "aarch64") == 0) { |
488 | init_bootfile(bootpath, aarch64_kernel); | |
489 | cmd_src = g_strdup_printf("-machine virt,accel=%s,gic-version=max " | |
490 | "-name vmsource,debug-threads=on -cpu max " | |
491 | "-m 150M -serial file:%s/src_serial " | |
492 | "-kernel %s ", | |
493 | accel, tmpfs, bootpath); | |
494 | cmd_dst = g_strdup_printf("-machine virt,accel=%s,gic-version=max " | |
495 | "-name vmdest,debug-threads=on -cpu max " | |
496 | "-m 150M -serial file:%s/dest_serial " | |
497 | "-kernel %s " | |
498 | "-incoming %s ", | |
499 | accel, tmpfs, bootpath, uri); | |
500 | ||
501 | start_address = ARM_TEST_MEM_START; | |
502 | end_address = ARM_TEST_MEM_END; | |
503 | ||
504 | g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE); | |
aaf89c8a | 505 | } else { |
506 | g_assert_not_reached(); | |
507 | } | |
508 | ||
e2dd21e5 MAL |
509 | g_free(bootpath); |
510 | ||
f96d6651 DDAG |
511 | if (hide_stderr) { |
512 | gchar *tmp; | |
513 | tmp = g_strdup_printf("%s 2>/dev/null", cmd_src); | |
514 | g_free(cmd_src); | |
515 | cmd_src = tmp; | |
516 | ||
517 | tmp = g_strdup_printf("%s 2>/dev/null", cmd_dst); | |
518 | g_free(cmd_dst); | |
519 | cmd_dst = tmp; | |
520 | } | |
521 | ||
7195a871 | 522 | *from = qtest_start(cmd_src); |
aaf89c8a | 523 | g_free(cmd_src); |
524 | ||
7195a871 | 525 | *to = qtest_init(cmd_dst); |
aaf89c8a | 526 | g_free(cmd_dst); |
5fd4a9c9 | 527 | return 0; |
7195a871 JQ |
528 | } |
529 | ||
2c9bb297 | 530 | static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest) |
7195a871 JQ |
531 | { |
532 | unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d; | |
533 | ||
534 | qtest_quit(from); | |
535 | ||
2c9bb297 DDAG |
536 | if (test_dest) { |
537 | qtest_memread(to, start_address, &dest_byte_a, 1); | |
7195a871 | 538 | |
2c9bb297 DDAG |
539 | /* Destination still running, wait for a byte to change */ |
540 | do { | |
541 | qtest_memread(to, start_address, &dest_byte_b, 1); | |
542 | usleep(1000 * 10); | |
543 | } while (dest_byte_a == dest_byte_b); | |
544 | ||
545 | qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}"); | |
7195a871 | 546 | |
2c9bb297 DDAG |
547 | /* With it stopped, check nothing changes */ |
548 | qtest_memread(to, start_address, &dest_byte_c, 1); | |
549 | usleep(1000 * 200); | |
550 | qtest_memread(to, start_address, &dest_byte_d, 1); | |
551 | g_assert_cmpint(dest_byte_c, ==, dest_byte_d); | |
7195a871 | 552 | |
2c9bb297 DDAG |
553 | check_guests_ram(to); |
554 | } | |
7195a871 JQ |
555 | |
556 | qtest_quit(to); | |
557 | ||
558 | cleanup("bootsect"); | |
559 | cleanup("migsocket"); | |
560 | cleanup("src_serial"); | |
561 | cleanup("dest_serial"); | |
562 | } | |
563 | ||
4c27486d JQ |
564 | static void deprecated_set_downtime(QTestState *who, const double value) |
565 | { | |
566 | QDict *rsp; | |
4c27486d | 567 | |
015715f5 MA |
568 | rsp = qtest_qmp(who, |
569 | "{ 'execute': 'migrate_set_downtime'," | |
570 | " 'arguments': { 'value': %f } }", value); | |
4c27486d | 571 | g_assert(qdict_haskey(rsp, "return")); |
cb3e7f08 | 572 | qobject_unref(rsp); |
c44a56d8 | 573 | migrate_check_parameter(who, "downtime-limit", value * 1000); |
4c27486d JQ |
574 | } |
575 | ||
c44a56d8 | 576 | static void deprecated_set_speed(QTestState *who, long long value) |
4c27486d JQ |
577 | { |
578 | QDict *rsp; | |
4c27486d | 579 | |
c44a56d8 MA |
580 | rsp = qtest_qmp(who, "{ 'execute': 'migrate_set_speed'," |
581 | "'arguments': { 'value': %lld } }", value); | |
4c27486d | 582 | g_assert(qdict_haskey(rsp, "return")); |
cb3e7f08 | 583 | qobject_unref(rsp); |
4c27486d JQ |
584 | migrate_check_parameter(who, "max-bandwidth", value); |
585 | } | |
586 | ||
587 | static void test_deprecated(void) | |
588 | { | |
589 | QTestState *from; | |
590 | ||
c02b3781 | 591 | from = qtest_start("-machine none"); |
4c27486d JQ |
592 | |
593 | deprecated_set_downtime(from, 0.12345); | |
c44a56d8 | 594 | deprecated_set_speed(from, 12345); |
4c27486d JQ |
595 | |
596 | qtest_quit(from); | |
597 | } | |
598 | ||
d131662a | 599 | static int migrate_postcopy_prepare(QTestState **from_ptr, |
3e81f73c PX |
600 | QTestState **to_ptr, |
601 | bool hide_error) | |
7195a871 JQ |
602 | { |
603 | char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); | |
863e27a8 | 604 | QTestState *from, *to; |
7195a871 | 605 | |
3e81f73c | 606 | if (test_migrate_start(&from, &to, uri, hide_error)) { |
d131662a | 607 | return -1; |
5fd4a9c9 | 608 | } |
ea0c6d62 | 609 | |
c44a56d8 MA |
610 | migrate_set_capability(from, "postcopy-ram", true); |
611 | migrate_set_capability(to, "postcopy-ram", true); | |
612 | migrate_set_capability(to, "postcopy-blocktime", true); | |
ea0c6d62 DDAG |
613 | |
614 | /* We want to pick a speed slow enough that the test completes | |
615 | * quickly, but that it doesn't complete precopy even on a slow | |
616 | * machine, so also set the downtime. | |
617 | */ | |
c44a56d8 MA |
618 | migrate_set_parameter(from, "max-bandwidth", 100000000); |
619 | migrate_set_parameter(from, "downtime-limit", 1); | |
ea0c6d62 DDAG |
620 | |
621 | /* Wait for the first serial output from the source */ | |
622 | wait_for_serial("src_serial"); | |
623 | ||
b5bbd3f3 | 624 | migrate(from, uri, "{}"); |
d131662a | 625 | g_free(uri); |
ea0c6d62 | 626 | |
863e27a8 | 627 | wait_for_migration_pass(from); |
ea0c6d62 | 628 | |
d131662a PX |
629 | *from_ptr = from; |
630 | *to_ptr = to; | |
ea0c6d62 | 631 | |
d131662a PX |
632 | return 0; |
633 | } | |
ea0c6d62 | 634 | |
d131662a PX |
635 | static void migrate_postcopy_complete(QTestState *from, QTestState *to) |
636 | { | |
637 | wait_for_migration_complete(from); | |
ea0c6d62 | 638 | |
d131662a | 639 | /* Make sure we get at least one "B" on destination */ |
ea0c6d62 | 640 | wait_for_serial("dest_serial"); |
ea0c6d62 | 641 | |
346f3dab AP |
642 | if (uffd_feature_thread_id) { |
643 | read_blocktime(to); | |
644 | } | |
ea0c6d62 | 645 | |
2c9bb297 DDAG |
646 | test_migrate_end(from, to, true); |
647 | } | |
648 | ||
d131662a PX |
649 | static void test_postcopy(void) |
650 | { | |
651 | QTestState *from, *to; | |
652 | ||
3e81f73c | 653 | if (migrate_postcopy_prepare(&from, &to, false)) { |
d131662a PX |
654 | return; |
655 | } | |
656 | migrate_postcopy_start(from, to); | |
657 | migrate_postcopy_complete(from, to); | |
658 | } | |
659 | ||
d5f49640 PX |
660 | static void test_postcopy_recovery(void) |
661 | { | |
662 | QTestState *from, *to; | |
663 | char *uri; | |
664 | ||
3e81f73c | 665 | if (migrate_postcopy_prepare(&from, &to, true)) { |
d5f49640 PX |
666 | return; |
667 | } | |
668 | ||
669 | /* Turn postcopy speed down, 4K/s is slow enough on any machines */ | |
c44a56d8 | 670 | migrate_set_parameter(from, "max-postcopy-bandwidth", 4096); |
d5f49640 PX |
671 | |
672 | /* Now we start the postcopy */ | |
673 | migrate_postcopy_start(from, to); | |
674 | ||
675 | /* | |
676 | * Wait until postcopy is really started; we can only run the | |
677 | * migrate-pause command during a postcopy | |
678 | */ | |
679 | wait_for_migration_status(from, "postcopy-active"); | |
680 | ||
681 | /* | |
682 | * Manually stop the postcopy migration. This emulates a network | |
683 | * failure with the migration socket | |
684 | */ | |
685 | migrate_pause(from); | |
686 | ||
687 | /* | |
688 | * Wait for destination side to reach postcopy-paused state. The | |
689 | * migrate-recover command can only succeed if destination machine | |
690 | * is in the paused state | |
691 | */ | |
692 | wait_for_migration_status(to, "postcopy-paused"); | |
693 | ||
694 | /* | |
695 | * Create a new socket to emulate a new channel that is different | |
696 | * from the broken migration channel; tell the destination to | |
697 | * listen to the new port | |
698 | */ | |
699 | uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs); | |
700 | migrate_recover(to, uri); | |
701 | ||
702 | /* | |
703 | * Try to rebuild the migration channel using the resume flag and | |
704 | * the newly created channel | |
705 | */ | |
706 | wait_for_migration_status(from, "postcopy-paused"); | |
b5bbd3f3 | 707 | migrate(from, uri, "{'resume': true}"); |
d5f49640 PX |
708 | g_free(uri); |
709 | ||
710 | /* Restore the postcopy bandwidth to unlimited */ | |
c44a56d8 | 711 | migrate_set_parameter(from, "max-postcopy-bandwidth", 0); |
d5f49640 PX |
712 | |
713 | migrate_postcopy_complete(from, to); | |
714 | } | |
715 | ||
2c9bb297 DDAG |
716 | static void test_baddest(void) |
717 | { | |
718 | QTestState *from, *to; | |
e1454165 | 719 | QDict *rsp_return; |
2f7074c6 | 720 | char *status; |
2c9bb297 DDAG |
721 | bool failed; |
722 | ||
5fd4a9c9 DDAG |
723 | if (test_migrate_start(&from, &to, "tcp:0:0", true)) { |
724 | return; | |
725 | } | |
b5bbd3f3 | 726 | migrate(from, "tcp:0:0", "{}"); |
2c9bb297 | 727 | do { |
2f7074c6 | 728 | status = migrate_query_status(from); |
2c9bb297 DDAG |
729 | g_assert(!strcmp(status, "setup") || !(strcmp(status, "failed"))); |
730 | failed = !strcmp(status, "failed"); | |
2f7074c6 | 731 | g_free(status); |
2c9bb297 DDAG |
732 | } while (!failed); |
733 | ||
734 | /* Is the machine currently running? */ | |
e1454165 | 735 | rsp_return = wait_command(from, "{ 'execute': 'query-status' }"); |
2c9bb297 DDAG |
736 | g_assert(qdict_haskey(rsp_return, "running")); |
737 | g_assert(qdict_get_bool(rsp_return, "running")); | |
e1454165 | 738 | qobject_unref(rsp_return); |
2c9bb297 DDAG |
739 | |
740 | test_migrate_end(from, to, false); | |
ea0c6d62 DDAG |
741 | } |
742 | ||
2884100c JQ |
743 | static void test_precopy_unix(void) |
744 | { | |
745 | char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); | |
746 | QTestState *from, *to; | |
747 | ||
5fd4a9c9 DDAG |
748 | if (test_migrate_start(&from, &to, uri, false)) { |
749 | return; | |
750 | } | |
2884100c JQ |
751 | |
752 | /* We want to pick a speed slow enough that the test completes | |
753 | * quickly, but that it doesn't complete precopy even on a slow | |
754 | * machine, so also set the downtime. | |
755 | */ | |
756 | /* 1 ms should make it not converge*/ | |
c44a56d8 | 757 | migrate_set_parameter(from, "downtime-limit", 1); |
2884100c | 758 | /* 1GB/s */ |
c44a56d8 | 759 | migrate_set_parameter(from, "max-bandwidth", 1000000000); |
2884100c JQ |
760 | |
761 | /* Wait for the first serial output from the source */ | |
762 | wait_for_serial("src_serial"); | |
763 | ||
b5bbd3f3 | 764 | migrate(from, uri, "{}"); |
2884100c JQ |
765 | |
766 | wait_for_migration_pass(from); | |
767 | ||
768 | /* 300 ms should converge */ | |
c44a56d8 | 769 | migrate_set_parameter(from, "downtime-limit", 300); |
2884100c JQ |
770 | |
771 | if (!got_stop) { | |
772 | qtest_qmp_eventwait(from, "STOP"); | |
773 | } | |
774 | ||
775 | qtest_qmp_eventwait(to, "RESUME"); | |
776 | ||
777 | wait_for_serial("dest_serial"); | |
778 | wait_for_migration_complete(from); | |
779 | ||
780 | test_migrate_end(from, to, true); | |
781 | g_free(uri); | |
782 | } | |
783 | ||
ea0c6d62 DDAG |
784 | int main(int argc, char **argv) |
785 | { | |
2656bfd9 | 786 | char template[] = "/tmp/migration-test-XXXXXX"; |
ea0c6d62 DDAG |
787 | int ret; |
788 | ||
789 | g_test_init(&argc, &argv, NULL); | |
790 | ||
791 | if (!ufd_version_check()) { | |
792 | return 0; | |
793 | } | |
794 | ||
880b169a TH |
795 | /* |
796 | * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG | |
797 | * is touchy due to race conditions on dirty bits (especially on PPC for | |
798 | * some reason) | |
799 | */ | |
800 | if (g_str_equal(qtest_get_arch(), "ppc64") && | |
801 | access("/sys/module/kvm_hv", F_OK)) { | |
802 | g_test_message("Skipping test: kvm_hv not available"); | |
803 | return 0; | |
804 | } | |
805 | ||
d254b392 TH |
806 | /* |
807 | * Similar to ppc64, s390x seems to be touchy with TCG, so disable it | |
808 | * there until the problems are resolved | |
809 | */ | |
810 | if (g_str_equal(qtest_get_arch(), "s390x")) { | |
811 | #if defined(HOST_S390X) | |
812 | if (access("/dev/kvm", R_OK | W_OK)) { | |
813 | g_test_message("Skipping test: kvm not available"); | |
814 | return 0; | |
815 | } | |
816 | #else | |
817 | g_test_message("Skipping test: Need s390x host to work properly"); | |
818 | return 0; | |
819 | #endif | |
820 | } | |
821 | ||
ea0c6d62 DDAG |
822 | tmpfs = mkdtemp(template); |
823 | if (!tmpfs) { | |
824 | g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno)); | |
825 | } | |
826 | g_assert(tmpfs); | |
827 | ||
828 | module_call_init(MODULE_INIT_QOM); | |
829 | ||
2884100c | 830 | qtest_add_func("/migration/postcopy/unix", test_postcopy); |
d5f49640 | 831 | qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery); |
4c27486d | 832 | qtest_add_func("/migration/deprecated", test_deprecated); |
2c9bb297 | 833 | qtest_add_func("/migration/bad_dest", test_baddest); |
2884100c | 834 | qtest_add_func("/migration/precopy/unix", test_precopy_unix); |
ea0c6d62 DDAG |
835 | |
836 | ret = g_test_run(); | |
837 | ||
838 | g_assert_cmpint(ret, ==, 0); | |
839 | ||
840 | ret = rmdir(tmpfs); | |
841 | if (ret != 0) { | |
842 | g_test_message("unable to rmdir: path (%s): %s\n", | |
843 | tmpfs, strerror(errno)); | |
844 | } | |
845 | ||
846 | return ret; | |
847 | } |