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