]> git.proxmox.com Git - mirror_qemu.git/blob - tests/qtest/migration-test.c
tests/qtest: switch to using event callbacks for STOP event
[mirror_qemu.git] / tests / qtest / migration-test.c
1 /*
2 * QTest testcase for migration
3 *
4 * Copyright (c) 2016-2018 Red Hat, Inc. and/or its affiliates
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"
14
15 #include "libqtest.h"
16 #include "qapi/error.h"
17 #include "qapi/qmp/qdict.h"
18 #include "qemu/module.h"
19 #include "qemu/option.h"
20 #include "qemu/range.h"
21 #include "qemu/sockets.h"
22 #include "chardev/char.h"
23 #include "qapi/qapi-visit-sockets.h"
24 #include "qapi/qobject-input-visitor.h"
25 #include "qapi/qobject-output-visitor.h"
26 #include "crypto/tlscredspsk.h"
27 #include "qapi/qmp/qlist.h"
28
29 #include "migration-helpers.h"
30 #include "tests/migration/migration-test.h"
31 #ifdef CONFIG_GNUTLS
32 # include "tests/unit/crypto-tls-psk-helpers.h"
33 # ifdef CONFIG_TASN1
34 # include "tests/unit/crypto-tls-x509-helpers.h"
35 # endif /* CONFIG_TASN1 */
36 #endif /* CONFIG_GNUTLS */
37
38 /* For dirty ring test; so far only x86_64 is supported */
39 #if defined(__linux__) && defined(HOST_X86_64)
40 #include "linux/kvm.h"
41 #endif
42
43 unsigned start_address;
44 unsigned end_address;
45 static bool uffd_feature_thread_id;
46 static bool got_stop;
47
48 /*
49 * Dirtylimit stop working if dirty page rate error
50 * value less than DIRTYLIMIT_TOLERANCE_RANGE
51 */
52 #define DIRTYLIMIT_TOLERANCE_RANGE 25 /* MB/s */
53
54 #if defined(__linux__)
55 #include <sys/syscall.h>
56 #include <sys/vfs.h>
57 #endif
58
59 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
60 #include <sys/eventfd.h>
61 #include <sys/ioctl.h>
62 #include "qemu/userfaultfd.h"
63
64 static bool ufd_version_check(void)
65 {
66 struct uffdio_api api_struct;
67 uint64_t ioctl_mask;
68
69 int ufd = uffd_open(O_CLOEXEC);
70
71 if (ufd == -1) {
72 g_test_message("Skipping test: userfaultfd not available");
73 return false;
74 }
75
76 api_struct.api = UFFD_API;
77 api_struct.features = 0;
78 if (ioctl(ufd, UFFDIO_API, &api_struct)) {
79 g_test_message("Skipping test: UFFDIO_API failed");
80 return false;
81 }
82 uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
83
84 ioctl_mask = (__u64)1 << _UFFDIO_REGISTER |
85 (__u64)1 << _UFFDIO_UNREGISTER;
86 if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
87 g_test_message("Skipping test: Missing userfault feature");
88 return false;
89 }
90
91 return true;
92 }
93
94 #else
95 static bool ufd_version_check(void)
96 {
97 g_test_message("Skipping test: Userfault not available (builtdtime)");
98 return false;
99 }
100
101 #endif
102
103 static char *tmpfs;
104
105 /* The boot file modifies memory area in [start_address, end_address)
106 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
107 */
108 #include "tests/migration/i386/a-b-bootblock.h"
109 #include "tests/migration/aarch64/a-b-kernel.h"
110 #include "tests/migration/s390x/a-b-bios.h"
111
112 static void init_bootfile(const char *bootpath, void *content, size_t len)
113 {
114 FILE *bootfile = fopen(bootpath, "wb");
115
116 g_assert_cmpint(fwrite(content, len, 1, bootfile), ==, 1);
117 fclose(bootfile);
118 }
119
120 /*
121 * Wait for some output in the serial output file,
122 * we get an 'A' followed by an endless string of 'B's
123 * but on the destination we won't have the A.
124 */
125 static void wait_for_serial(const char *side)
126 {
127 g_autofree char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
128 FILE *serialfile = fopen(serialpath, "r");
129 const char *arch = qtest_get_arch();
130 int started = (strcmp(side, "src_serial") == 0 &&
131 strcmp(arch, "ppc64") == 0) ? 0 : 1;
132
133 do {
134 int readvalue = fgetc(serialfile);
135
136 if (!started) {
137 /* SLOF prints its banner before starting test,
138 * to ignore it, mark the start of the test with '_',
139 * ignore all characters until this marker
140 */
141 switch (readvalue) {
142 case '_':
143 started = 1;
144 break;
145 case EOF:
146 fseek(serialfile, 0, SEEK_SET);
147 usleep(1000);
148 break;
149 }
150 continue;
151 }
152 switch (readvalue) {
153 case 'A':
154 /* Fine */
155 break;
156
157 case 'B':
158 /* It's alive! */
159 fclose(serialfile);
160 return;
161
162 case EOF:
163 started = (strcmp(side, "src_serial") == 0 &&
164 strcmp(arch, "ppc64") == 0) ? 0 : 1;
165 fseek(serialfile, 0, SEEK_SET);
166 usleep(1000);
167 break;
168
169 default:
170 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
171 g_assert_not_reached();
172 }
173 } while (true);
174 }
175
176 /*
177 * It's tricky to use qemu's migration event capability with qtest,
178 * events suddenly appearing confuse the qmp()/hmp() responses.
179 */
180
181 static int64_t read_ram_property_int(QTestState *who, const char *property)
182 {
183 QDict *rsp_return, *rsp_ram;
184 int64_t result;
185
186 rsp_return = migrate_query_not_failed(who);
187 if (!qdict_haskey(rsp_return, "ram")) {
188 /* Still in setup */
189 result = 0;
190 } else {
191 rsp_ram = qdict_get_qdict(rsp_return, "ram");
192 result = qdict_get_try_int(rsp_ram, property, 0);
193 }
194 qobject_unref(rsp_return);
195 return result;
196 }
197
198 static int64_t read_migrate_property_int(QTestState *who, const char *property)
199 {
200 QDict *rsp_return;
201 int64_t result;
202
203 rsp_return = migrate_query_not_failed(who);
204 result = qdict_get_try_int(rsp_return, property, 0);
205 qobject_unref(rsp_return);
206 return result;
207 }
208
209 static uint64_t get_migration_pass(QTestState *who)
210 {
211 return read_ram_property_int(who, "dirty-sync-count");
212 }
213
214 static void read_blocktime(QTestState *who)
215 {
216 QDict *rsp_return;
217
218 rsp_return = migrate_query_not_failed(who);
219 g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
220 qobject_unref(rsp_return);
221 }
222
223 static void wait_for_migration_pass(QTestState *who)
224 {
225 uint64_t initial_pass = get_migration_pass(who);
226 uint64_t pass;
227
228 /* Wait for the 1st sync */
229 while (!got_stop && !initial_pass) {
230 usleep(1000);
231 initial_pass = get_migration_pass(who);
232 }
233
234 do {
235 usleep(1000);
236 pass = get_migration_pass(who);
237 } while (pass == initial_pass && !got_stop);
238 }
239
240 static void check_guests_ram(QTestState *who)
241 {
242 /* Our ASM test will have been incrementing one byte from each page from
243 * start_address to < end_address in order. This gives us a constraint
244 * that any page's byte should be equal or less than the previous pages
245 * byte (mod 256); and they should all be equal except for one transition
246 * at the point where we meet the incrementer. (We're running this with
247 * the guest stopped).
248 */
249 unsigned address;
250 uint8_t first_byte;
251 uint8_t last_byte;
252 bool hit_edge = false;
253 int bad = 0;
254
255 qtest_memread(who, start_address, &first_byte, 1);
256 last_byte = first_byte;
257
258 for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
259 address += TEST_MEM_PAGE_SIZE)
260 {
261 uint8_t b;
262 qtest_memread(who, address, &b, 1);
263 if (b != last_byte) {
264 if (((b + 1) % 256) == last_byte && !hit_edge) {
265 /* This is OK, the guest stopped at the point of
266 * incrementing the previous page but didn't get
267 * to us yet.
268 */
269 hit_edge = true;
270 last_byte = b;
271 } else {
272 bad++;
273 if (bad <= 10) {
274 fprintf(stderr, "Memory content inconsistency at %x"
275 " first_byte = %x last_byte = %x current = %x"
276 " hit_edge = %x\n",
277 address, first_byte, last_byte, b, hit_edge);
278 }
279 }
280 }
281 }
282 if (bad >= 10) {
283 fprintf(stderr, "and in another %d pages", bad - 10);
284 }
285 g_assert(bad == 0);
286 }
287
288 static void cleanup(const char *filename)
289 {
290 g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, filename);
291
292 unlink(path);
293 }
294
295 static char *SocketAddress_to_str(SocketAddress *addr)
296 {
297 switch (addr->type) {
298 case SOCKET_ADDRESS_TYPE_INET:
299 return g_strdup_printf("tcp:%s:%s",
300 addr->u.inet.host,
301 addr->u.inet.port);
302 case SOCKET_ADDRESS_TYPE_UNIX:
303 return g_strdup_printf("unix:%s",
304 addr->u.q_unix.path);
305 case SOCKET_ADDRESS_TYPE_FD:
306 return g_strdup_printf("fd:%s", addr->u.fd.str);
307 case SOCKET_ADDRESS_TYPE_VSOCK:
308 return g_strdup_printf("tcp:%s:%s",
309 addr->u.vsock.cid,
310 addr->u.vsock.port);
311 default:
312 return g_strdup("unknown address type");
313 }
314 }
315
316 static char *migrate_get_socket_address(QTestState *who, const char *parameter)
317 {
318 QDict *rsp;
319 char *result;
320 SocketAddressList *addrs;
321 Visitor *iv = NULL;
322 QObject *object;
323
324 rsp = migrate_query(who);
325 object = qdict_get(rsp, parameter);
326
327 iv = qobject_input_visitor_new(object);
328 visit_type_SocketAddressList(iv, NULL, &addrs, &error_abort);
329 visit_free(iv);
330
331 /* we are only using a single address */
332 result = SocketAddress_to_str(addrs->value);
333
334 qapi_free_SocketAddressList(addrs);
335 qobject_unref(rsp);
336 return result;
337 }
338
339 static long long migrate_get_parameter_int(QTestState *who,
340 const char *parameter)
341 {
342 QDict *rsp;
343 long long result;
344
345 rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
346 result = qdict_get_int(rsp, parameter);
347 qobject_unref(rsp);
348 return result;
349 }
350
351 static void migrate_check_parameter_int(QTestState *who, const char *parameter,
352 long long value)
353 {
354 long long result;
355
356 result = migrate_get_parameter_int(who, parameter);
357 g_assert_cmpint(result, ==, value);
358 }
359
360 static void migrate_set_parameter_int(QTestState *who, const char *parameter,
361 long long value)
362 {
363 qtest_qmp_assert_success(who,
364 "{ 'execute': 'migrate-set-parameters',"
365 "'arguments': { %s: %lld } }",
366 parameter, value);
367 migrate_check_parameter_int(who, parameter, value);
368 }
369
370 static char *migrate_get_parameter_str(QTestState *who,
371 const char *parameter)
372 {
373 QDict *rsp;
374 char *result;
375
376 rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
377 result = g_strdup(qdict_get_str(rsp, parameter));
378 qobject_unref(rsp);
379 return result;
380 }
381
382 static void migrate_check_parameter_str(QTestState *who, const char *parameter,
383 const char *value)
384 {
385 g_autofree char *result = migrate_get_parameter_str(who, parameter);
386 g_assert_cmpstr(result, ==, value);
387 }
388
389 static void migrate_set_parameter_str(QTestState *who, const char *parameter,
390 const char *value)
391 {
392 qtest_qmp_assert_success(who,
393 "{ 'execute': 'migrate-set-parameters',"
394 "'arguments': { %s: %s } }",
395 parameter, value);
396 migrate_check_parameter_str(who, parameter, value);
397 }
398
399 static long long migrate_get_parameter_bool(QTestState *who,
400 const char *parameter)
401 {
402 QDict *rsp;
403 int result;
404
405 rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
406 result = qdict_get_bool(rsp, parameter);
407 qobject_unref(rsp);
408 return !!result;
409 }
410
411 static void migrate_check_parameter_bool(QTestState *who, const char *parameter,
412 int value)
413 {
414 int result;
415
416 result = migrate_get_parameter_bool(who, parameter);
417 g_assert_cmpint(result, ==, value);
418 }
419
420 static void migrate_set_parameter_bool(QTestState *who, const char *parameter,
421 int value)
422 {
423 qtest_qmp_assert_success(who,
424 "{ 'execute': 'migrate-set-parameters',"
425 "'arguments': { %s: %i } }",
426 parameter, value);
427 migrate_check_parameter_bool(who, parameter, value);
428 }
429
430 static void migrate_ensure_non_converge(QTestState *who)
431 {
432 /* Can't converge with 1ms downtime + 3 mbs bandwidth limit */
433 migrate_set_parameter_int(who, "max-bandwidth", 3 * 1000 * 1000);
434 migrate_set_parameter_int(who, "downtime-limit", 1);
435 }
436
437 static void migrate_ensure_converge(QTestState *who)
438 {
439 /* Should converge with 30s downtime + 1 gbs bandwidth limit */
440 migrate_set_parameter_int(who, "max-bandwidth", 1 * 1000 * 1000 * 1000);
441 migrate_set_parameter_int(who, "downtime-limit", 30 * 1000);
442 }
443
444 static void migrate_pause(QTestState *who)
445 {
446 QDict *rsp;
447
448 rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
449 qobject_unref(rsp);
450 }
451
452 static void migrate_continue(QTestState *who, const char *state)
453 {
454 QDict *rsp;
455
456 rsp = wait_command(who,
457 "{ 'execute': 'migrate-continue',"
458 " 'arguments': { 'state': %s } }",
459 state);
460 qobject_unref(rsp);
461 }
462
463 static void migrate_recover(QTestState *who, const char *uri)
464 {
465 QDict *rsp;
466
467 rsp = wait_command(who,
468 "{ 'execute': 'migrate-recover', "
469 " 'id': 'recover-cmd', "
470 " 'arguments': { 'uri': %s } }",
471 uri);
472 qobject_unref(rsp);
473 }
474
475 static void migrate_cancel(QTestState *who)
476 {
477 QDict *rsp;
478
479 rsp = wait_command(who, "{ 'execute': 'migrate_cancel' }");
480 qobject_unref(rsp);
481 }
482
483 static void migrate_set_capability(QTestState *who, const char *capability,
484 bool value)
485 {
486 qtest_qmp_assert_success(who,
487 "{ 'execute': 'migrate-set-capabilities',"
488 "'arguments': { "
489 "'capabilities': [ { "
490 "'capability': %s, 'state': %i } ] } }",
491 capability, value);
492 }
493
494 static void migrate_postcopy_start(QTestState *from, QTestState *to)
495 {
496 QDict *rsp;
497
498 rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
499 qobject_unref(rsp);
500
501 if (!got_stop) {
502 qtest_qmp_eventwait(from, "STOP");
503 }
504
505 qtest_qmp_eventwait(to, "RESUME");
506 }
507
508 typedef struct {
509 /*
510 * QTEST_LOG=1 may override this. When QTEST_LOG=1, we always dump errors
511 * unconditionally, because it means the user would like to be verbose.
512 */
513 bool hide_stderr;
514 bool use_shmem;
515 /* only launch the target process */
516 bool only_target;
517 /* Use dirty ring if true; dirty logging otherwise */
518 bool use_dirty_ring;
519 const char *opts_source;
520 const char *opts_target;
521 } MigrateStart;
522
523 /*
524 * A hook that runs after the src and dst QEMUs have been
525 * created, but before the migration is started. This can
526 * be used to set migration parameters and capabilities.
527 *
528 * Returns: NULL, or a pointer to opaque state to be
529 * later passed to the TestMigrateFinishHook
530 */
531 typedef void * (*TestMigrateStartHook)(QTestState *from,
532 QTestState *to);
533
534 /*
535 * A hook that runs after the migration has finished,
536 * regardless of whether it succeeded or failed, but
537 * before QEMU has terminated (unless it self-terminated
538 * due to migration error)
539 *
540 * @opaque is a pointer to state previously returned
541 * by the TestMigrateStartHook if any, or NULL.
542 */
543 typedef void (*TestMigrateFinishHook)(QTestState *from,
544 QTestState *to,
545 void *opaque);
546
547 typedef struct {
548 /* Optional: fine tune start parameters */
549 MigrateStart start;
550
551 /* Required: the URI for the dst QEMU to listen on */
552 const char *listen_uri;
553
554 /*
555 * Optional: the URI for the src QEMU to connect to
556 * If NULL, then it will query the dst QEMU for its actual
557 * listening address and use that as the connect address.
558 * This allows for dynamically picking a free TCP port.
559 */
560 const char *connect_uri;
561
562 /* Optional: callback to run at start to set migration parameters */
563 TestMigrateStartHook start_hook;
564 /* Optional: callback to run at finish to cleanup */
565 TestMigrateFinishHook finish_hook;
566
567 /*
568 * Optional: normally we expect the migration process to complete.
569 *
570 * There can be a variety of reasons and stages in which failure
571 * can happen during tests.
572 *
573 * If a failure is expected to happen at time of establishing
574 * the connection, then MIG_TEST_FAIL will indicate that the dst
575 * QEMU is expected to stay running and accept future migration
576 * connections.
577 *
578 * If a failure is expected to happen while processing the
579 * migration stream, then MIG_TEST_FAIL_DEST_QUIT_ERR will indicate
580 * that the dst QEMU is expected to quit with non-zero exit status
581 */
582 enum {
583 /* This test should succeed, the default */
584 MIG_TEST_SUCCEED = 0,
585 /* This test should fail, dest qemu should keep alive */
586 MIG_TEST_FAIL,
587 /* This test should fail, dest qemu should fail with abnormal status */
588 MIG_TEST_FAIL_DEST_QUIT_ERR,
589 } result;
590
591 /* Optional: set number of migration passes to wait for */
592 unsigned int iterations;
593
594 /* Postcopy specific fields */
595 void *postcopy_data;
596 bool postcopy_preempt;
597 } MigrateCommon;
598
599 static int test_migrate_start(QTestState **from, QTestState **to,
600 const char *uri, MigrateStart *args)
601 {
602 g_autofree gchar *arch_source = NULL;
603 g_autofree gchar *arch_target = NULL;
604 g_autofree gchar *cmd_source = NULL;
605 g_autofree gchar *cmd_target = NULL;
606 const gchar *ignore_stderr;
607 g_autofree char *bootpath = NULL;
608 g_autofree char *shmem_opts = NULL;
609 g_autofree char *shmem_path = NULL;
610 const char *arch = qtest_get_arch();
611 const char *machine_opts = NULL;
612 const char *memory_size;
613
614 if (args->use_shmem) {
615 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
616 g_test_skip("/dev/shm is not supported");
617 return -1;
618 }
619 }
620
621 got_stop = false;
622 bootpath = g_strdup_printf("%s/bootsect", tmpfs);
623 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
624 /* the assembled x86 boot sector should be exactly one sector large */
625 assert(sizeof(x86_bootsect) == 512);
626 init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect));
627 memory_size = "150M";
628 arch_source = g_strdup_printf("-drive file=%s,format=raw", bootpath);
629 arch_target = g_strdup(arch_source);
630 start_address = X86_TEST_MEM_START;
631 end_address = X86_TEST_MEM_END;
632 } else if (g_str_equal(arch, "s390x")) {
633 init_bootfile(bootpath, s390x_elf, sizeof(s390x_elf));
634 memory_size = "128M";
635 arch_source = g_strdup_printf("-bios %s", bootpath);
636 arch_target = g_strdup(arch_source);
637 start_address = S390_TEST_MEM_START;
638 end_address = S390_TEST_MEM_END;
639 } else if (strcmp(arch, "ppc64") == 0) {
640 machine_opts = "vsmt=8";
641 memory_size = "256M";
642 start_address = PPC_TEST_MEM_START;
643 end_address = PPC_TEST_MEM_END;
644 arch_source = g_strdup_printf("-nodefaults "
645 "-prom-env 'use-nvramrc?=true' -prom-env "
646 "'nvramrc=hex .\" _\" begin %x %x "
647 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
648 "until'", end_address, start_address);
649 arch_target = g_strdup("");
650 } else if (strcmp(arch, "aarch64") == 0) {
651 init_bootfile(bootpath, aarch64_kernel, sizeof(aarch64_kernel));
652 machine_opts = "virt,gic-version=max";
653 memory_size = "150M";
654 arch_source = g_strdup_printf("-cpu max "
655 "-kernel %s",
656 bootpath);
657 arch_target = g_strdup(arch_source);
658 start_address = ARM_TEST_MEM_START;
659 end_address = ARM_TEST_MEM_END;
660
661 g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
662 } else {
663 g_assert_not_reached();
664 }
665
666 if (!getenv("QTEST_LOG") && args->hide_stderr) {
667 #ifndef _WIN32
668 ignore_stderr = "2>/dev/null";
669 #else
670 /*
671 * On Windows the QEMU executable is created via CreateProcess() and
672 * IO redirection does not work, so don't bother adding IO redirection
673 * to the command line.
674 */
675 ignore_stderr = "";
676 #endif
677 } else {
678 ignore_stderr = "";
679 }
680
681 if (args->use_shmem) {
682 shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
683 shmem_opts = g_strdup_printf(
684 "-object memory-backend-file,id=mem0,size=%s"
685 ",mem-path=%s,share=on -numa node,memdev=mem0",
686 memory_size, shmem_path);
687 } else {
688 shmem_path = NULL;
689 shmem_opts = g_strdup("");
690 }
691
692 cmd_source = g_strdup_printf("-accel kvm%s -accel tcg%s%s "
693 "-name source,debug-threads=on "
694 "-m %s "
695 "-serial file:%s/src_serial "
696 "%s %s %s %s",
697 args->use_dirty_ring ?
698 ",dirty-ring-size=4096" : "",
699 machine_opts ? " -machine " : "",
700 machine_opts ? machine_opts : "",
701 memory_size, tmpfs,
702 arch_source, shmem_opts,
703 args->opts_source ? args->opts_source : "",
704 ignore_stderr);
705 if (!args->only_target) {
706 *from = qtest_init(cmd_source);
707 qtest_qmp_set_event_callback(*from,
708 migrate_watch_for_stop,
709 &got_stop);
710 }
711
712 cmd_target = g_strdup_printf("-accel kvm%s -accel tcg%s%s "
713 "-name target,debug-threads=on "
714 "-m %s "
715 "-serial file:%s/dest_serial "
716 "-incoming %s "
717 "%s %s %s %s",
718 args->use_dirty_ring ?
719 ",dirty-ring-size=4096" : "",
720 machine_opts ? " -machine " : "",
721 machine_opts ? machine_opts : "",
722 memory_size, tmpfs, uri,
723 arch_target, shmem_opts,
724 args->opts_target ? args->opts_target : "",
725 ignore_stderr);
726 *to = qtest_init(cmd_target);
727
728 /*
729 * Remove shmem file immediately to avoid memory leak in test failed case.
730 * It's valid becase QEMU has already opened this file
731 */
732 if (args->use_shmem) {
733 unlink(shmem_path);
734 }
735
736 return 0;
737 }
738
739 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
740 {
741 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
742
743 qtest_quit(from);
744
745 if (test_dest) {
746 qtest_memread(to, start_address, &dest_byte_a, 1);
747
748 /* Destination still running, wait for a byte to change */
749 do {
750 qtest_memread(to, start_address, &dest_byte_b, 1);
751 usleep(1000 * 10);
752 } while (dest_byte_a == dest_byte_b);
753
754 qtest_qmp_assert_success(to, "{ 'execute' : 'stop'}");
755
756 /* With it stopped, check nothing changes */
757 qtest_memread(to, start_address, &dest_byte_c, 1);
758 usleep(1000 * 200);
759 qtest_memread(to, start_address, &dest_byte_d, 1);
760 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
761
762 check_guests_ram(to);
763 }
764
765 qtest_quit(to);
766
767 cleanup("bootsect");
768 cleanup("migsocket");
769 cleanup("src_serial");
770 cleanup("dest_serial");
771 }
772
773 #ifdef CONFIG_GNUTLS
774 struct TestMigrateTLSPSKData {
775 char *workdir;
776 char *workdiralt;
777 char *pskfile;
778 char *pskfilealt;
779 };
780
781 static void *
782 test_migrate_tls_psk_start_common(QTestState *from,
783 QTestState *to,
784 bool mismatch)
785 {
786 struct TestMigrateTLSPSKData *data =
787 g_new0(struct TestMigrateTLSPSKData, 1);
788 QDict *rsp;
789
790 data->workdir = g_strdup_printf("%s/tlscredspsk0", tmpfs);
791 data->pskfile = g_strdup_printf("%s/%s", data->workdir,
792 QCRYPTO_TLS_CREDS_PSKFILE);
793 g_mkdir_with_parents(data->workdir, 0700);
794 test_tls_psk_init(data->pskfile);
795
796 if (mismatch) {
797 data->workdiralt = g_strdup_printf("%s/tlscredspskalt0", tmpfs);
798 data->pskfilealt = g_strdup_printf("%s/%s", data->workdiralt,
799 QCRYPTO_TLS_CREDS_PSKFILE);
800 g_mkdir_with_parents(data->workdiralt, 0700);
801 test_tls_psk_init_alt(data->pskfilealt);
802 }
803
804 rsp = wait_command(from,
805 "{ 'execute': 'object-add',"
806 " 'arguments': { 'qom-type': 'tls-creds-psk',"
807 " 'id': 'tlscredspsk0',"
808 " 'endpoint': 'client',"
809 " 'dir': %s,"
810 " 'username': 'qemu'} }",
811 data->workdir);
812 qobject_unref(rsp);
813
814 rsp = wait_command(to,
815 "{ 'execute': 'object-add',"
816 " 'arguments': { 'qom-type': 'tls-creds-psk',"
817 " 'id': 'tlscredspsk0',"
818 " 'endpoint': 'server',"
819 " 'dir': %s } }",
820 mismatch ? data->workdiralt : data->workdir);
821 qobject_unref(rsp);
822
823 migrate_set_parameter_str(from, "tls-creds", "tlscredspsk0");
824 migrate_set_parameter_str(to, "tls-creds", "tlscredspsk0");
825
826 return data;
827 }
828
829 static void *
830 test_migrate_tls_psk_start_match(QTestState *from,
831 QTestState *to)
832 {
833 return test_migrate_tls_psk_start_common(from, to, false);
834 }
835
836 static void *
837 test_migrate_tls_psk_start_mismatch(QTestState *from,
838 QTestState *to)
839 {
840 return test_migrate_tls_psk_start_common(from, to, true);
841 }
842
843 static void
844 test_migrate_tls_psk_finish(QTestState *from,
845 QTestState *to,
846 void *opaque)
847 {
848 struct TestMigrateTLSPSKData *data = opaque;
849
850 test_tls_psk_cleanup(data->pskfile);
851 if (data->pskfilealt) {
852 test_tls_psk_cleanup(data->pskfilealt);
853 }
854 rmdir(data->workdir);
855 if (data->workdiralt) {
856 rmdir(data->workdiralt);
857 }
858
859 g_free(data->workdiralt);
860 g_free(data->pskfilealt);
861 g_free(data->workdir);
862 g_free(data->pskfile);
863 g_free(data);
864 }
865
866 #ifdef CONFIG_TASN1
867 typedef struct {
868 char *workdir;
869 char *keyfile;
870 char *cacert;
871 char *servercert;
872 char *serverkey;
873 char *clientcert;
874 char *clientkey;
875 } TestMigrateTLSX509Data;
876
877 typedef struct {
878 bool verifyclient;
879 bool clientcert;
880 bool hostileclient;
881 bool authzclient;
882 const char *certhostname;
883 const char *certipaddr;
884 } TestMigrateTLSX509;
885
886 static void *
887 test_migrate_tls_x509_start_common(QTestState *from,
888 QTestState *to,
889 TestMigrateTLSX509 *args)
890 {
891 TestMigrateTLSX509Data *data = g_new0(TestMigrateTLSX509Data, 1);
892 QDict *rsp;
893
894 data->workdir = g_strdup_printf("%s/tlscredsx5090", tmpfs);
895 data->keyfile = g_strdup_printf("%s/key.pem", data->workdir);
896
897 data->cacert = g_strdup_printf("%s/ca-cert.pem", data->workdir);
898 data->serverkey = g_strdup_printf("%s/server-key.pem", data->workdir);
899 data->servercert = g_strdup_printf("%s/server-cert.pem", data->workdir);
900 if (args->clientcert) {
901 data->clientkey = g_strdup_printf("%s/client-key.pem", data->workdir);
902 data->clientcert = g_strdup_printf("%s/client-cert.pem", data->workdir);
903 }
904
905 g_mkdir_with_parents(data->workdir, 0700);
906
907 test_tls_init(data->keyfile);
908 #ifndef _WIN32
909 g_assert(link(data->keyfile, data->serverkey) == 0);
910 #else
911 g_assert(CreateHardLink(data->serverkey, data->keyfile, NULL) != 0);
912 #endif
913 if (args->clientcert) {
914 #ifndef _WIN32
915 g_assert(link(data->keyfile, data->clientkey) == 0);
916 #else
917 g_assert(CreateHardLink(data->clientkey, data->keyfile, NULL) != 0);
918 #endif
919 }
920
921 TLS_ROOT_REQ_SIMPLE(cacertreq, data->cacert);
922 if (args->clientcert) {
923 TLS_CERT_REQ_SIMPLE_CLIENT(servercertreq, cacertreq,
924 args->hostileclient ?
925 QCRYPTO_TLS_TEST_CLIENT_HOSTILE_NAME :
926 QCRYPTO_TLS_TEST_CLIENT_NAME,
927 data->clientcert);
928 }
929
930 TLS_CERT_REQ_SIMPLE_SERVER(clientcertreq, cacertreq,
931 data->servercert,
932 args->certhostname,
933 args->certipaddr);
934
935 rsp = wait_command(from,
936 "{ 'execute': 'object-add',"
937 " 'arguments': { 'qom-type': 'tls-creds-x509',"
938 " 'id': 'tlscredsx509client0',"
939 " 'endpoint': 'client',"
940 " 'dir': %s,"
941 " 'sanity-check': true,"
942 " 'verify-peer': true} }",
943 data->workdir);
944 qobject_unref(rsp);
945 migrate_set_parameter_str(from, "tls-creds", "tlscredsx509client0");
946 if (args->certhostname) {
947 migrate_set_parameter_str(from, "tls-hostname", args->certhostname);
948 }
949
950 rsp = wait_command(to,
951 "{ 'execute': 'object-add',"
952 " 'arguments': { 'qom-type': 'tls-creds-x509',"
953 " 'id': 'tlscredsx509server0',"
954 " 'endpoint': 'server',"
955 " 'dir': %s,"
956 " 'sanity-check': true,"
957 " 'verify-peer': %i} }",
958 data->workdir, args->verifyclient);
959 qobject_unref(rsp);
960 migrate_set_parameter_str(to, "tls-creds", "tlscredsx509server0");
961
962 if (args->authzclient) {
963 rsp = wait_command(to,
964 "{ 'execute': 'object-add',"
965 " 'arguments': { 'qom-type': 'authz-simple',"
966 " 'id': 'tlsauthz0',"
967 " 'identity': %s} }",
968 "CN=" QCRYPTO_TLS_TEST_CLIENT_NAME);
969 migrate_set_parameter_str(to, "tls-authz", "tlsauthz0");
970 }
971
972 return data;
973 }
974
975 /*
976 * The normal case: match server's cert hostname against
977 * whatever host we were telling QEMU to connect to (if any)
978 */
979 static void *
980 test_migrate_tls_x509_start_default_host(QTestState *from,
981 QTestState *to)
982 {
983 TestMigrateTLSX509 args = {
984 .verifyclient = true,
985 .clientcert = true,
986 .certipaddr = "127.0.0.1"
987 };
988 return test_migrate_tls_x509_start_common(from, to, &args);
989 }
990
991 /*
992 * The unusual case: the server's cert is different from
993 * the address we're telling QEMU to connect to (if any),
994 * so we must give QEMU an explicit hostname to validate
995 */
996 static void *
997 test_migrate_tls_x509_start_override_host(QTestState *from,
998 QTestState *to)
999 {
1000 TestMigrateTLSX509 args = {
1001 .verifyclient = true,
1002 .clientcert = true,
1003 .certhostname = "qemu.org",
1004 };
1005 return test_migrate_tls_x509_start_common(from, to, &args);
1006 }
1007
1008 /*
1009 * The unusual case: the server's cert is different from
1010 * the address we're telling QEMU to connect to, and so we
1011 * expect the client to reject the server
1012 */
1013 static void *
1014 test_migrate_tls_x509_start_mismatch_host(QTestState *from,
1015 QTestState *to)
1016 {
1017 TestMigrateTLSX509 args = {
1018 .verifyclient = true,
1019 .clientcert = true,
1020 .certipaddr = "10.0.0.1",
1021 };
1022 return test_migrate_tls_x509_start_common(from, to, &args);
1023 }
1024
1025 static void *
1026 test_migrate_tls_x509_start_friendly_client(QTestState *from,
1027 QTestState *to)
1028 {
1029 TestMigrateTLSX509 args = {
1030 .verifyclient = true,
1031 .clientcert = true,
1032 .authzclient = true,
1033 .certipaddr = "127.0.0.1",
1034 };
1035 return test_migrate_tls_x509_start_common(from, to, &args);
1036 }
1037
1038 static void *
1039 test_migrate_tls_x509_start_hostile_client(QTestState *from,
1040 QTestState *to)
1041 {
1042 TestMigrateTLSX509 args = {
1043 .verifyclient = true,
1044 .clientcert = true,
1045 .hostileclient = true,
1046 .authzclient = true,
1047 .certipaddr = "127.0.0.1",
1048 };
1049 return test_migrate_tls_x509_start_common(from, to, &args);
1050 }
1051
1052 /*
1053 * The case with no client certificate presented,
1054 * and no server verification
1055 */
1056 static void *
1057 test_migrate_tls_x509_start_allow_anon_client(QTestState *from,
1058 QTestState *to)
1059 {
1060 TestMigrateTLSX509 args = {
1061 .certipaddr = "127.0.0.1",
1062 };
1063 return test_migrate_tls_x509_start_common(from, to, &args);
1064 }
1065
1066 /*
1067 * The case with no client certificate presented,
1068 * and server verification rejecting
1069 */
1070 static void *
1071 test_migrate_tls_x509_start_reject_anon_client(QTestState *from,
1072 QTestState *to)
1073 {
1074 TestMigrateTLSX509 args = {
1075 .verifyclient = true,
1076 .certipaddr = "127.0.0.1",
1077 };
1078 return test_migrate_tls_x509_start_common(from, to, &args);
1079 }
1080
1081 static void
1082 test_migrate_tls_x509_finish(QTestState *from,
1083 QTestState *to,
1084 void *opaque)
1085 {
1086 TestMigrateTLSX509Data *data = opaque;
1087
1088 test_tls_cleanup(data->keyfile);
1089 g_free(data->keyfile);
1090
1091 unlink(data->cacert);
1092 g_free(data->cacert);
1093 unlink(data->servercert);
1094 g_free(data->servercert);
1095 unlink(data->serverkey);
1096 g_free(data->serverkey);
1097
1098 if (data->clientcert) {
1099 unlink(data->clientcert);
1100 g_free(data->clientcert);
1101 }
1102 if (data->clientkey) {
1103 unlink(data->clientkey);
1104 g_free(data->clientkey);
1105 }
1106
1107 rmdir(data->workdir);
1108 g_free(data->workdir);
1109
1110 g_free(data);
1111 }
1112 #endif /* CONFIG_TASN1 */
1113 #endif /* CONFIG_GNUTLS */
1114
1115 static void *
1116 test_migrate_compress_start(QTestState *from,
1117 QTestState *to)
1118 {
1119 migrate_set_parameter_int(from, "compress-level", 1);
1120 migrate_set_parameter_int(from, "compress-threads", 4);
1121 migrate_set_parameter_bool(from, "compress-wait-thread", true);
1122 migrate_set_parameter_int(to, "decompress-threads", 4);
1123
1124 migrate_set_capability(from, "compress", true);
1125 migrate_set_capability(to, "compress", true);
1126
1127 return NULL;
1128 }
1129
1130 static void *
1131 test_migrate_compress_nowait_start(QTestState *from,
1132 QTestState *to)
1133 {
1134 migrate_set_parameter_int(from, "compress-level", 9);
1135 migrate_set_parameter_int(from, "compress-threads", 1);
1136 migrate_set_parameter_bool(from, "compress-wait-thread", false);
1137 migrate_set_parameter_int(to, "decompress-threads", 1);
1138
1139 migrate_set_capability(from, "compress", true);
1140 migrate_set_capability(to, "compress", true);
1141
1142 return NULL;
1143 }
1144
1145 static int migrate_postcopy_prepare(QTestState **from_ptr,
1146 QTestState **to_ptr,
1147 MigrateCommon *args)
1148 {
1149 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1150 QTestState *from, *to;
1151
1152 if (test_migrate_start(&from, &to, uri, &args->start)) {
1153 return -1;
1154 }
1155
1156 if (args->start_hook) {
1157 args->postcopy_data = args->start_hook(from, to);
1158 }
1159
1160 migrate_set_capability(from, "postcopy-ram", true);
1161 migrate_set_capability(to, "postcopy-ram", true);
1162 migrate_set_capability(to, "postcopy-blocktime", true);
1163
1164 if (args->postcopy_preempt) {
1165 migrate_set_capability(from, "postcopy-preempt", true);
1166 migrate_set_capability(to, "postcopy-preempt", true);
1167 }
1168
1169 migrate_ensure_non_converge(from);
1170
1171 /* Wait for the first serial output from the source */
1172 wait_for_serial("src_serial");
1173
1174 migrate_qmp(from, uri, "{}");
1175
1176 wait_for_migration_pass(from);
1177
1178 *from_ptr = from;
1179 *to_ptr = to;
1180
1181 return 0;
1182 }
1183
1184 static void migrate_postcopy_complete(QTestState *from, QTestState *to,
1185 MigrateCommon *args)
1186 {
1187 wait_for_migration_complete(from);
1188
1189 /* Make sure we get at least one "B" on destination */
1190 wait_for_serial("dest_serial");
1191
1192 if (uffd_feature_thread_id) {
1193 read_blocktime(to);
1194 }
1195
1196 if (args->finish_hook) {
1197 args->finish_hook(from, to, args->postcopy_data);
1198 args->postcopy_data = NULL;
1199 }
1200
1201 test_migrate_end(from, to, true);
1202 }
1203
1204 static void test_postcopy_common(MigrateCommon *args)
1205 {
1206 QTestState *from, *to;
1207
1208 if (migrate_postcopy_prepare(&from, &to, args)) {
1209 return;
1210 }
1211 migrate_postcopy_start(from, to);
1212 migrate_postcopy_complete(from, to, args);
1213 }
1214
1215 static void test_postcopy(void)
1216 {
1217 MigrateCommon args = { };
1218
1219 test_postcopy_common(&args);
1220 }
1221
1222 static void test_postcopy_compress(void)
1223 {
1224 MigrateCommon args = {
1225 .start_hook = test_migrate_compress_start
1226 };
1227
1228 test_postcopy_common(&args);
1229 }
1230
1231 static void test_postcopy_preempt(void)
1232 {
1233 MigrateCommon args = {
1234 .postcopy_preempt = true,
1235 };
1236
1237 test_postcopy_common(&args);
1238 }
1239
1240 #ifdef CONFIG_GNUTLS
1241 static void test_postcopy_tls_psk(void)
1242 {
1243 MigrateCommon args = {
1244 .start_hook = test_migrate_tls_psk_start_match,
1245 .finish_hook = test_migrate_tls_psk_finish,
1246 };
1247
1248 test_postcopy_common(&args);
1249 }
1250
1251 static void test_postcopy_preempt_tls_psk(void)
1252 {
1253 MigrateCommon args = {
1254 .postcopy_preempt = true,
1255 .start_hook = test_migrate_tls_psk_start_match,
1256 .finish_hook = test_migrate_tls_psk_finish,
1257 };
1258
1259 test_postcopy_common(&args);
1260 }
1261 #endif
1262
1263 static void test_postcopy_recovery_common(MigrateCommon *args)
1264 {
1265 QTestState *from, *to;
1266 g_autofree char *uri = NULL;
1267
1268 /* Always hide errors for postcopy recover tests since they're expected */
1269 args->start.hide_stderr = true;
1270
1271 if (migrate_postcopy_prepare(&from, &to, args)) {
1272 return;
1273 }
1274
1275 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
1276 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
1277
1278 /* Now we start the postcopy */
1279 migrate_postcopy_start(from, to);
1280
1281 /*
1282 * Wait until postcopy is really started; we can only run the
1283 * migrate-pause command during a postcopy
1284 */
1285 wait_for_migration_status(from, "postcopy-active", NULL);
1286
1287 /*
1288 * Manually stop the postcopy migration. This emulates a network
1289 * failure with the migration socket
1290 */
1291 migrate_pause(from);
1292
1293 /*
1294 * Wait for destination side to reach postcopy-paused state. The
1295 * migrate-recover command can only succeed if destination machine
1296 * is in the paused state
1297 */
1298 wait_for_migration_status(to, "postcopy-paused",
1299 (const char * []) { "failed", "active",
1300 "completed", NULL });
1301
1302 /*
1303 * Create a new socket to emulate a new channel that is different
1304 * from the broken migration channel; tell the destination to
1305 * listen to the new port
1306 */
1307 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
1308 migrate_recover(to, uri);
1309
1310 /*
1311 * Try to rebuild the migration channel using the resume flag and
1312 * the newly created channel
1313 */
1314 wait_for_migration_status(from, "postcopy-paused",
1315 (const char * []) { "failed", "active",
1316 "completed", NULL });
1317 migrate_qmp(from, uri, "{'resume': true}");
1318
1319 /* Restore the postcopy bandwidth to unlimited */
1320 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
1321
1322 migrate_postcopy_complete(from, to, args);
1323 }
1324
1325 static void test_postcopy_recovery(void)
1326 {
1327 MigrateCommon args = { };
1328
1329 test_postcopy_recovery_common(&args);
1330 }
1331
1332 static void test_postcopy_recovery_compress(void)
1333 {
1334 MigrateCommon args = {
1335 .start_hook = test_migrate_compress_start
1336 };
1337
1338 test_postcopy_recovery_common(&args);
1339 }
1340
1341 #ifdef CONFIG_GNUTLS
1342 static void test_postcopy_recovery_tls_psk(void)
1343 {
1344 MigrateCommon args = {
1345 .start_hook = test_migrate_tls_psk_start_match,
1346 .finish_hook = test_migrate_tls_psk_finish,
1347 };
1348
1349 test_postcopy_recovery_common(&args);
1350 }
1351 #endif
1352
1353 static void test_postcopy_preempt_recovery(void)
1354 {
1355 MigrateCommon args = {
1356 .postcopy_preempt = true,
1357 };
1358
1359 test_postcopy_recovery_common(&args);
1360 }
1361
1362 #ifdef CONFIG_GNUTLS
1363 /* This contains preempt+recovery+tls test altogether */
1364 static void test_postcopy_preempt_all(void)
1365 {
1366 MigrateCommon args = {
1367 .postcopy_preempt = true,
1368 .start_hook = test_migrate_tls_psk_start_match,
1369 .finish_hook = test_migrate_tls_psk_finish,
1370 };
1371
1372 test_postcopy_recovery_common(&args);
1373 }
1374
1375 #endif
1376
1377 static void test_baddest(void)
1378 {
1379 MigrateStart args = {
1380 .hide_stderr = true
1381 };
1382 QTestState *from, *to;
1383
1384 if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", &args)) {
1385 return;
1386 }
1387 migrate_qmp(from, "tcp:127.0.0.1:0", "{}");
1388 wait_for_migration_fail(from, false);
1389 test_migrate_end(from, to, false);
1390 }
1391
1392 static void test_precopy_common(MigrateCommon *args)
1393 {
1394 QTestState *from, *to;
1395 void *data_hook = NULL;
1396
1397 if (test_migrate_start(&from, &to, args->listen_uri, &args->start)) {
1398 return;
1399 }
1400
1401 migrate_ensure_non_converge(from);
1402
1403 if (args->start_hook) {
1404 data_hook = args->start_hook(from, to);
1405 }
1406
1407 /* Wait for the first serial output from the source */
1408 if (args->result == MIG_TEST_SUCCEED) {
1409 wait_for_serial("src_serial");
1410 }
1411
1412 if (!args->connect_uri) {
1413 g_autofree char *local_connect_uri =
1414 migrate_get_socket_address(to, "socket-address");
1415 migrate_qmp(from, local_connect_uri, "{}");
1416 } else {
1417 migrate_qmp(from, args->connect_uri, "{}");
1418 }
1419
1420
1421 if (args->result != MIG_TEST_SUCCEED) {
1422 bool allow_active = args->result == MIG_TEST_FAIL;
1423 wait_for_migration_fail(from, allow_active);
1424
1425 if (args->result == MIG_TEST_FAIL_DEST_QUIT_ERR) {
1426 qtest_set_expected_status(to, EXIT_FAILURE);
1427 }
1428 } else {
1429 if (args->iterations) {
1430 while (args->iterations--) {
1431 wait_for_migration_pass(from);
1432 }
1433 } else {
1434 wait_for_migration_pass(from);
1435 }
1436
1437 migrate_ensure_converge(from);
1438
1439 /* We do this first, as it has a timeout to stop us
1440 * hanging forever if migration didn't converge */
1441 wait_for_migration_complete(from);
1442
1443 if (!got_stop) {
1444 qtest_qmp_eventwait(from, "STOP");
1445 }
1446
1447 qtest_qmp_eventwait(to, "RESUME");
1448
1449 wait_for_serial("dest_serial");
1450 }
1451
1452 if (args->finish_hook) {
1453 args->finish_hook(from, to, data_hook);
1454 }
1455
1456 test_migrate_end(from, to, args->result == MIG_TEST_SUCCEED);
1457 }
1458
1459 static void test_precopy_unix_plain(void)
1460 {
1461 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1462 MigrateCommon args = {
1463 .listen_uri = uri,
1464 .connect_uri = uri,
1465 };
1466
1467 test_precopy_common(&args);
1468 }
1469
1470
1471 static void test_precopy_unix_dirty_ring(void)
1472 {
1473 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1474 MigrateCommon args = {
1475 .start = {
1476 .use_dirty_ring = true,
1477 },
1478 .listen_uri = uri,
1479 .connect_uri = uri,
1480 };
1481
1482 test_precopy_common(&args);
1483 }
1484
1485 #ifdef CONFIG_GNUTLS
1486 static void test_precopy_unix_tls_psk(void)
1487 {
1488 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1489 MigrateCommon args = {
1490 .connect_uri = uri,
1491 .listen_uri = uri,
1492 .start_hook = test_migrate_tls_psk_start_match,
1493 .finish_hook = test_migrate_tls_psk_finish,
1494 };
1495
1496 test_precopy_common(&args);
1497 }
1498
1499 #ifdef CONFIG_TASN1
1500 static void test_precopy_unix_tls_x509_default_host(void)
1501 {
1502 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1503 MigrateCommon args = {
1504 .start = {
1505 .hide_stderr = true,
1506 },
1507 .connect_uri = uri,
1508 .listen_uri = uri,
1509 .start_hook = test_migrate_tls_x509_start_default_host,
1510 .finish_hook = test_migrate_tls_x509_finish,
1511 .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
1512 };
1513
1514 test_precopy_common(&args);
1515 }
1516
1517 static void test_precopy_unix_tls_x509_override_host(void)
1518 {
1519 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1520 MigrateCommon args = {
1521 .connect_uri = uri,
1522 .listen_uri = uri,
1523 .start_hook = test_migrate_tls_x509_start_override_host,
1524 .finish_hook = test_migrate_tls_x509_finish,
1525 };
1526
1527 test_precopy_common(&args);
1528 }
1529 #endif /* CONFIG_TASN1 */
1530 #endif /* CONFIG_GNUTLS */
1531
1532 #if 0
1533 /* Currently upset on aarch64 TCG */
1534 static void test_ignore_shared(void)
1535 {
1536 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1537 QTestState *from, *to;
1538
1539 if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) {
1540 return;
1541 }
1542
1543 migrate_set_capability(from, "x-ignore-shared", true);
1544 migrate_set_capability(to, "x-ignore-shared", true);
1545
1546 /* Wait for the first serial output from the source */
1547 wait_for_serial("src_serial");
1548
1549 migrate_qmp(from, uri, "{}");
1550
1551 wait_for_migration_pass(from);
1552
1553 if (!got_stop) {
1554 qtest_qmp_eventwait(from, "STOP");
1555 }
1556
1557 qtest_qmp_eventwait(to, "RESUME");
1558
1559 wait_for_serial("dest_serial");
1560 wait_for_migration_complete(from);
1561
1562 /* Check whether shared RAM has been really skipped */
1563 g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
1564
1565 test_migrate_end(from, to, true);
1566 }
1567 #endif
1568
1569 static void *
1570 test_migrate_xbzrle_start(QTestState *from,
1571 QTestState *to)
1572 {
1573 migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
1574
1575 migrate_set_capability(from, "xbzrle", true);
1576 migrate_set_capability(to, "xbzrle", true);
1577
1578 return NULL;
1579 }
1580
1581 static void test_precopy_unix_xbzrle(void)
1582 {
1583 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1584 MigrateCommon args = {
1585 .connect_uri = uri,
1586 .listen_uri = uri,
1587
1588 .start_hook = test_migrate_xbzrle_start,
1589
1590 .iterations = 2,
1591 };
1592
1593 test_precopy_common(&args);
1594 }
1595
1596 static void test_precopy_unix_compress(void)
1597 {
1598 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1599 MigrateCommon args = {
1600 .connect_uri = uri,
1601 .listen_uri = uri,
1602 .start_hook = test_migrate_compress_start,
1603 /*
1604 * Test that no invalid thread state is left over from
1605 * the previous iteration.
1606 */
1607 .iterations = 2,
1608 };
1609
1610 test_precopy_common(&args);
1611 }
1612
1613 static void test_precopy_unix_compress_nowait(void)
1614 {
1615 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1616 MigrateCommon args = {
1617 .connect_uri = uri,
1618 .listen_uri = uri,
1619 .start_hook = test_migrate_compress_nowait_start,
1620 /*
1621 * Test that no invalid thread state is left over from
1622 * the previous iteration.
1623 */
1624 .iterations = 2,
1625 };
1626
1627 test_precopy_common(&args);
1628 }
1629
1630 static void test_precopy_tcp_plain(void)
1631 {
1632 MigrateCommon args = {
1633 .listen_uri = "tcp:127.0.0.1:0",
1634 };
1635
1636 test_precopy_common(&args);
1637 }
1638
1639 #ifdef CONFIG_GNUTLS
1640 static void test_precopy_tcp_tls_psk_match(void)
1641 {
1642 MigrateCommon args = {
1643 .listen_uri = "tcp:127.0.0.1:0",
1644 .start_hook = test_migrate_tls_psk_start_match,
1645 .finish_hook = test_migrate_tls_psk_finish,
1646 };
1647
1648 test_precopy_common(&args);
1649 }
1650
1651 static void test_precopy_tcp_tls_psk_mismatch(void)
1652 {
1653 MigrateCommon args = {
1654 .start = {
1655 .hide_stderr = true,
1656 },
1657 .listen_uri = "tcp:127.0.0.1:0",
1658 .start_hook = test_migrate_tls_psk_start_mismatch,
1659 .finish_hook = test_migrate_tls_psk_finish,
1660 .result = MIG_TEST_FAIL,
1661 };
1662
1663 test_precopy_common(&args);
1664 }
1665
1666 #ifdef CONFIG_TASN1
1667 static void test_precopy_tcp_tls_x509_default_host(void)
1668 {
1669 MigrateCommon args = {
1670 .listen_uri = "tcp:127.0.0.1:0",
1671 .start_hook = test_migrate_tls_x509_start_default_host,
1672 .finish_hook = test_migrate_tls_x509_finish,
1673 };
1674
1675 test_precopy_common(&args);
1676 }
1677
1678 static void test_precopy_tcp_tls_x509_override_host(void)
1679 {
1680 MigrateCommon args = {
1681 .listen_uri = "tcp:127.0.0.1:0",
1682 .start_hook = test_migrate_tls_x509_start_override_host,
1683 .finish_hook = test_migrate_tls_x509_finish,
1684 };
1685
1686 test_precopy_common(&args);
1687 }
1688
1689 static void test_precopy_tcp_tls_x509_mismatch_host(void)
1690 {
1691 MigrateCommon args = {
1692 .start = {
1693 .hide_stderr = true,
1694 },
1695 .listen_uri = "tcp:127.0.0.1:0",
1696 .start_hook = test_migrate_tls_x509_start_mismatch_host,
1697 .finish_hook = test_migrate_tls_x509_finish,
1698 .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
1699 };
1700
1701 test_precopy_common(&args);
1702 }
1703
1704 static void test_precopy_tcp_tls_x509_friendly_client(void)
1705 {
1706 MigrateCommon args = {
1707 .listen_uri = "tcp:127.0.0.1:0",
1708 .start_hook = test_migrate_tls_x509_start_friendly_client,
1709 .finish_hook = test_migrate_tls_x509_finish,
1710 };
1711
1712 test_precopy_common(&args);
1713 }
1714
1715 static void test_precopy_tcp_tls_x509_hostile_client(void)
1716 {
1717 MigrateCommon args = {
1718 .start = {
1719 .hide_stderr = true,
1720 },
1721 .listen_uri = "tcp:127.0.0.1:0",
1722 .start_hook = test_migrate_tls_x509_start_hostile_client,
1723 .finish_hook = test_migrate_tls_x509_finish,
1724 .result = MIG_TEST_FAIL,
1725 };
1726
1727 test_precopy_common(&args);
1728 }
1729
1730 static void test_precopy_tcp_tls_x509_allow_anon_client(void)
1731 {
1732 MigrateCommon args = {
1733 .listen_uri = "tcp:127.0.0.1:0",
1734 .start_hook = test_migrate_tls_x509_start_allow_anon_client,
1735 .finish_hook = test_migrate_tls_x509_finish,
1736 };
1737
1738 test_precopy_common(&args);
1739 }
1740
1741 static void test_precopy_tcp_tls_x509_reject_anon_client(void)
1742 {
1743 MigrateCommon args = {
1744 .start = {
1745 .hide_stderr = true,
1746 },
1747 .listen_uri = "tcp:127.0.0.1:0",
1748 .start_hook = test_migrate_tls_x509_start_reject_anon_client,
1749 .finish_hook = test_migrate_tls_x509_finish,
1750 .result = MIG_TEST_FAIL,
1751 };
1752
1753 test_precopy_common(&args);
1754 }
1755 #endif /* CONFIG_TASN1 */
1756 #endif /* CONFIG_GNUTLS */
1757
1758 #ifndef _WIN32
1759 static void *test_migrate_fd_start_hook(QTestState *from,
1760 QTestState *to)
1761 {
1762 QDict *rsp;
1763 int ret;
1764 int pair[2];
1765
1766 /* Create two connected sockets for migration */
1767 ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
1768 g_assert_cmpint(ret, ==, 0);
1769
1770 /* Send the 1st socket to the target */
1771 rsp = wait_command_fd(to, pair[0],
1772 "{ 'execute': 'getfd',"
1773 " 'arguments': { 'fdname': 'fd-mig' }}");
1774 qobject_unref(rsp);
1775 close(pair[0]);
1776
1777 /* Start incoming migration from the 1st socket */
1778 rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1779 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1780 qobject_unref(rsp);
1781
1782 /* Send the 2nd socket to the target */
1783 rsp = wait_command_fd(from, pair[1],
1784 "{ 'execute': 'getfd',"
1785 " 'arguments': { 'fdname': 'fd-mig' }}");
1786 qobject_unref(rsp);
1787 close(pair[1]);
1788
1789 return NULL;
1790 }
1791
1792 static void test_migrate_fd_finish_hook(QTestState *from,
1793 QTestState *to,
1794 void *opaque)
1795 {
1796 QDict *rsp;
1797 const char *error_desc;
1798
1799 /* Test closing fds */
1800 /* We assume, that QEMU removes named fd from its list,
1801 * so this should fail */
1802 rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
1803 " 'arguments': { 'fdname': 'fd-mig' }}");
1804 g_assert_true(qdict_haskey(rsp, "error"));
1805 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1806 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1807 qobject_unref(rsp);
1808
1809 rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
1810 " 'arguments': { 'fdname': 'fd-mig' }}");
1811 g_assert_true(qdict_haskey(rsp, "error"));
1812 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1813 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1814 qobject_unref(rsp);
1815 }
1816
1817 static void test_migrate_fd_proto(void)
1818 {
1819 MigrateCommon args = {
1820 .listen_uri = "defer",
1821 .connect_uri = "fd:fd-mig",
1822 .start_hook = test_migrate_fd_start_hook,
1823 .finish_hook = test_migrate_fd_finish_hook
1824 };
1825 test_precopy_common(&args);
1826 }
1827 #endif /* _WIN32 */
1828
1829 static void do_test_validate_uuid(MigrateStart *args, bool should_fail)
1830 {
1831 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1832 QTestState *from, *to;
1833
1834 if (test_migrate_start(&from, &to, uri, args)) {
1835 return;
1836 }
1837
1838 /*
1839 * UUID validation is at the begin of migration. So, the main process of
1840 * migration is not interesting for us here. Thus, set huge downtime for
1841 * very fast migration.
1842 */
1843 migrate_set_parameter_int(from, "downtime-limit", 1000000);
1844 migrate_set_capability(from, "validate-uuid", true);
1845
1846 /* Wait for the first serial output from the source */
1847 wait_for_serial("src_serial");
1848
1849 migrate_qmp(from, uri, "{}");
1850
1851 if (should_fail) {
1852 qtest_set_expected_status(to, EXIT_FAILURE);
1853 wait_for_migration_fail(from, true);
1854 } else {
1855 wait_for_migration_complete(from);
1856 }
1857
1858 test_migrate_end(from, to, false);
1859 }
1860
1861 static void test_validate_uuid(void)
1862 {
1863 MigrateStart args = {
1864 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
1865 .opts_target = "-uuid 11111111-1111-1111-1111-111111111111",
1866 };
1867
1868 do_test_validate_uuid(&args, false);
1869 }
1870
1871 static void test_validate_uuid_error(void)
1872 {
1873 MigrateStart args = {
1874 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
1875 .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
1876 .hide_stderr = true,
1877 };
1878
1879 do_test_validate_uuid(&args, true);
1880 }
1881
1882 static void test_validate_uuid_src_not_set(void)
1883 {
1884 MigrateStart args = {
1885 .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
1886 .hide_stderr = true,
1887 };
1888
1889 do_test_validate_uuid(&args, false);
1890 }
1891
1892 static void test_validate_uuid_dst_not_set(void)
1893 {
1894 MigrateStart args = {
1895 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
1896 .hide_stderr = true,
1897 };
1898
1899 do_test_validate_uuid(&args, false);
1900 }
1901
1902 /*
1903 * The way auto_converge works, we need to do too many passes to
1904 * run this test. Auto_converge logic is only run once every
1905 * three iterations, so:
1906 *
1907 * - 3 iterations without auto_converge enabled
1908 * - 3 iterations with pct = 5
1909 * - 3 iterations with pct = 30
1910 * - 3 iterations with pct = 55
1911 * - 3 iterations with pct = 80
1912 * - 3 iterations with pct = 95 (max(95, 80 + 25))
1913 *
1914 * To make things even worse, we need to run the initial stage at
1915 * 3MB/s so we enter autoconverge even when host is (over)loaded.
1916 */
1917 static void test_migrate_auto_converge(void)
1918 {
1919 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1920 MigrateStart args = {};
1921 QTestState *from, *to;
1922 int64_t percentage;
1923
1924 /*
1925 * We want the test to be stable and as fast as possible.
1926 * E.g., with 1Gb/s bandwith migration may pass without throttling,
1927 * so we need to decrease a bandwidth.
1928 */
1929 const int64_t init_pct = 5, inc_pct = 25, max_pct = 95;
1930
1931 if (test_migrate_start(&from, &to, uri, &args)) {
1932 return;
1933 }
1934
1935 migrate_set_capability(from, "auto-converge", true);
1936 migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct);
1937 migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct);
1938 migrate_set_parameter_int(from, "max-cpu-throttle", max_pct);
1939
1940 /*
1941 * Set the initial parameters so that the migration could not converge
1942 * without throttling.
1943 */
1944 migrate_ensure_non_converge(from);
1945
1946 /* To check remaining size after precopy */
1947 migrate_set_capability(from, "pause-before-switchover", true);
1948
1949 /* Wait for the first serial output from the source */
1950 wait_for_serial("src_serial");
1951
1952 migrate_qmp(from, uri, "{}");
1953
1954 /* Wait for throttling begins */
1955 percentage = 0;
1956 do {
1957 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1958 if (percentage != 0) {
1959 break;
1960 }
1961 usleep(20);
1962 g_assert_false(got_stop);
1963 } while (true);
1964 /* The first percentage of throttling should be at least init_pct */
1965 g_assert_cmpint(percentage, >=, init_pct);
1966 /* Now, when we tested that throttling works, let it converge */
1967 migrate_ensure_converge(from);
1968
1969 /*
1970 * Wait for pre-switchover status to check last throttle percentage
1971 * and remaining. These values will be zeroed later
1972 */
1973 wait_for_migration_status(from, "pre-switchover", NULL);
1974
1975 /* The final percentage of throttling shouldn't be greater than max_pct */
1976 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1977 g_assert_cmpint(percentage, <=, max_pct);
1978 migrate_continue(from, "pre-switchover");
1979
1980 qtest_qmp_eventwait(to, "RESUME");
1981
1982 wait_for_serial("dest_serial");
1983 wait_for_migration_complete(from);
1984
1985 test_migrate_end(from, to, true);
1986 }
1987
1988 static void *
1989 test_migrate_precopy_tcp_multifd_start_common(QTestState *from,
1990 QTestState *to,
1991 const char *method)
1992 {
1993 QDict *rsp;
1994
1995 migrate_set_parameter_int(from, "multifd-channels", 16);
1996 migrate_set_parameter_int(to, "multifd-channels", 16);
1997
1998 migrate_set_parameter_str(from, "multifd-compression", method);
1999 migrate_set_parameter_str(to, "multifd-compression", method);
2000
2001 migrate_set_capability(from, "multifd", true);
2002 migrate_set_capability(to, "multifd", true);
2003
2004 /* Start incoming migration from the 1st socket */
2005 rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
2006 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2007 qobject_unref(rsp);
2008
2009 return NULL;
2010 }
2011
2012 static void *
2013 test_migrate_precopy_tcp_multifd_start(QTestState *from,
2014 QTestState *to)
2015 {
2016 return test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2017 }
2018
2019 static void *
2020 test_migrate_precopy_tcp_multifd_zlib_start(QTestState *from,
2021 QTestState *to)
2022 {
2023 return test_migrate_precopy_tcp_multifd_start_common(from, to, "zlib");
2024 }
2025
2026 #ifdef CONFIG_ZSTD
2027 static void *
2028 test_migrate_precopy_tcp_multifd_zstd_start(QTestState *from,
2029 QTestState *to)
2030 {
2031 return test_migrate_precopy_tcp_multifd_start_common(from, to, "zstd");
2032 }
2033 #endif /* CONFIG_ZSTD */
2034
2035 static void test_multifd_tcp_none(void)
2036 {
2037 MigrateCommon args = {
2038 .listen_uri = "defer",
2039 .start_hook = test_migrate_precopy_tcp_multifd_start,
2040 };
2041 test_precopy_common(&args);
2042 }
2043
2044 static void test_multifd_tcp_zlib(void)
2045 {
2046 MigrateCommon args = {
2047 .listen_uri = "defer",
2048 .start_hook = test_migrate_precopy_tcp_multifd_zlib_start,
2049 };
2050 test_precopy_common(&args);
2051 }
2052
2053 #ifdef CONFIG_ZSTD
2054 static void test_multifd_tcp_zstd(void)
2055 {
2056 MigrateCommon args = {
2057 .listen_uri = "defer",
2058 .start_hook = test_migrate_precopy_tcp_multifd_zstd_start,
2059 };
2060 test_precopy_common(&args);
2061 }
2062 #endif
2063
2064 #ifdef CONFIG_GNUTLS
2065 static void *
2066 test_migrate_multifd_tcp_tls_psk_start_match(QTestState *from,
2067 QTestState *to)
2068 {
2069 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2070 return test_migrate_tls_psk_start_match(from, to);
2071 }
2072
2073 static void *
2074 test_migrate_multifd_tcp_tls_psk_start_mismatch(QTestState *from,
2075 QTestState *to)
2076 {
2077 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2078 return test_migrate_tls_psk_start_mismatch(from, to);
2079 }
2080
2081 #ifdef CONFIG_TASN1
2082 static void *
2083 test_migrate_multifd_tls_x509_start_default_host(QTestState *from,
2084 QTestState *to)
2085 {
2086 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2087 return test_migrate_tls_x509_start_default_host(from, to);
2088 }
2089
2090 static void *
2091 test_migrate_multifd_tls_x509_start_override_host(QTestState *from,
2092 QTestState *to)
2093 {
2094 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2095 return test_migrate_tls_x509_start_override_host(from, to);
2096 }
2097
2098 static void *
2099 test_migrate_multifd_tls_x509_start_mismatch_host(QTestState *from,
2100 QTestState *to)
2101 {
2102 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2103 return test_migrate_tls_x509_start_mismatch_host(from, to);
2104 }
2105
2106 static void *
2107 test_migrate_multifd_tls_x509_start_allow_anon_client(QTestState *from,
2108 QTestState *to)
2109 {
2110 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2111 return test_migrate_tls_x509_start_allow_anon_client(from, to);
2112 }
2113
2114 static void *
2115 test_migrate_multifd_tls_x509_start_reject_anon_client(QTestState *from,
2116 QTestState *to)
2117 {
2118 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2119 return test_migrate_tls_x509_start_reject_anon_client(from, to);
2120 }
2121 #endif /* CONFIG_TASN1 */
2122
2123 static void test_multifd_tcp_tls_psk_match(void)
2124 {
2125 MigrateCommon args = {
2126 .listen_uri = "defer",
2127 .start_hook = test_migrate_multifd_tcp_tls_psk_start_match,
2128 .finish_hook = test_migrate_tls_psk_finish,
2129 };
2130 test_precopy_common(&args);
2131 }
2132
2133 static void test_multifd_tcp_tls_psk_mismatch(void)
2134 {
2135 MigrateCommon args = {
2136 .start = {
2137 .hide_stderr = true,
2138 },
2139 .listen_uri = "defer",
2140 .start_hook = test_migrate_multifd_tcp_tls_psk_start_mismatch,
2141 .finish_hook = test_migrate_tls_psk_finish,
2142 .result = MIG_TEST_FAIL,
2143 };
2144 test_precopy_common(&args);
2145 }
2146
2147 #ifdef CONFIG_TASN1
2148 static void test_multifd_tcp_tls_x509_default_host(void)
2149 {
2150 MigrateCommon args = {
2151 .listen_uri = "defer",
2152 .start_hook = test_migrate_multifd_tls_x509_start_default_host,
2153 .finish_hook = test_migrate_tls_x509_finish,
2154 };
2155 test_precopy_common(&args);
2156 }
2157
2158 static void test_multifd_tcp_tls_x509_override_host(void)
2159 {
2160 MigrateCommon args = {
2161 .listen_uri = "defer",
2162 .start_hook = test_migrate_multifd_tls_x509_start_override_host,
2163 .finish_hook = test_migrate_tls_x509_finish,
2164 };
2165 test_precopy_common(&args);
2166 }
2167
2168 static void test_multifd_tcp_tls_x509_mismatch_host(void)
2169 {
2170 /*
2171 * This has different behaviour to the non-multifd case.
2172 *
2173 * In non-multifd case when client aborts due to mismatched
2174 * cert host, the server has already started trying to load
2175 * migration state, and so it exits with I/O failure.
2176 *
2177 * In multifd case when client aborts due to mismatched
2178 * cert host, the server is still waiting for the other
2179 * multifd connections to arrive so hasn't started trying
2180 * to load migration state, and thus just aborts the migration
2181 * without exiting.
2182 */
2183 MigrateCommon args = {
2184 .start = {
2185 .hide_stderr = true,
2186 },
2187 .listen_uri = "defer",
2188 .start_hook = test_migrate_multifd_tls_x509_start_mismatch_host,
2189 .finish_hook = test_migrate_tls_x509_finish,
2190 .result = MIG_TEST_FAIL,
2191 };
2192 test_precopy_common(&args);
2193 }
2194
2195 static void test_multifd_tcp_tls_x509_allow_anon_client(void)
2196 {
2197 MigrateCommon args = {
2198 .listen_uri = "defer",
2199 .start_hook = test_migrate_multifd_tls_x509_start_allow_anon_client,
2200 .finish_hook = test_migrate_tls_x509_finish,
2201 };
2202 test_precopy_common(&args);
2203 }
2204
2205 static void test_multifd_tcp_tls_x509_reject_anon_client(void)
2206 {
2207 MigrateCommon args = {
2208 .start = {
2209 .hide_stderr = true,
2210 },
2211 .listen_uri = "defer",
2212 .start_hook = test_migrate_multifd_tls_x509_start_reject_anon_client,
2213 .finish_hook = test_migrate_tls_x509_finish,
2214 .result = MIG_TEST_FAIL,
2215 };
2216 test_precopy_common(&args);
2217 }
2218 #endif /* CONFIG_TASN1 */
2219 #endif /* CONFIG_GNUTLS */
2220
2221 /*
2222 * This test does:
2223 * source target
2224 * migrate_incoming
2225 * migrate
2226 * migrate_cancel
2227 * launch another target
2228 * migrate
2229 *
2230 * And see that it works
2231 */
2232 static void test_multifd_tcp_cancel(void)
2233 {
2234 MigrateStart args = {
2235 .hide_stderr = true,
2236 };
2237 QTestState *from, *to, *to2;
2238 QDict *rsp;
2239 g_autofree char *uri = NULL;
2240
2241 if (test_migrate_start(&from, &to, "defer", &args)) {
2242 return;
2243 }
2244
2245 migrate_ensure_non_converge(from);
2246
2247 migrate_set_parameter_int(from, "multifd-channels", 16);
2248 migrate_set_parameter_int(to, "multifd-channels", 16);
2249
2250 migrate_set_capability(from, "multifd", true);
2251 migrate_set_capability(to, "multifd", true);
2252
2253 /* Start incoming migration from the 1st socket */
2254 rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
2255 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2256 qobject_unref(rsp);
2257
2258 /* Wait for the first serial output from the source */
2259 wait_for_serial("src_serial");
2260
2261 uri = migrate_get_socket_address(to, "socket-address");
2262
2263 migrate_qmp(from, uri, "{}");
2264
2265 wait_for_migration_pass(from);
2266
2267 migrate_cancel(from);
2268
2269 /* Make sure QEMU process "to" exited */
2270 qtest_set_expected_status(to, EXIT_FAILURE);
2271 qtest_wait_qemu(to);
2272
2273 args = (MigrateStart){
2274 .only_target = true,
2275 };
2276
2277 if (test_migrate_start(&from, &to2, "defer", &args)) {
2278 return;
2279 }
2280
2281 migrate_set_parameter_int(to2, "multifd-channels", 16);
2282
2283 migrate_set_capability(to2, "multifd", true);
2284
2285 /* Start incoming migration from the 1st socket */
2286 rsp = wait_command(to2, "{ 'execute': 'migrate-incoming',"
2287 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2288 qobject_unref(rsp);
2289
2290 g_free(uri);
2291 uri = migrate_get_socket_address(to2, "socket-address");
2292
2293 wait_for_migration_status(from, "cancelled", NULL);
2294
2295 migrate_ensure_converge(from);
2296
2297 migrate_qmp(from, uri, "{}");
2298
2299 wait_for_migration_pass(from);
2300
2301 if (!got_stop) {
2302 qtest_qmp_eventwait(from, "STOP");
2303 }
2304 qtest_qmp_eventwait(to2, "RESUME");
2305
2306 wait_for_serial("dest_serial");
2307 wait_for_migration_complete(from);
2308 test_migrate_end(from, to2, true);
2309 }
2310
2311 static void calc_dirty_rate(QTestState *who, uint64_t calc_time)
2312 {
2313 qtest_qmp_assert_success(who,
2314 "{ 'execute': 'calc-dirty-rate',"
2315 "'arguments': { "
2316 "'calc-time': %" PRIu64 ","
2317 "'mode': 'dirty-ring' }}",
2318 calc_time);
2319 }
2320
2321 static QDict *query_dirty_rate(QTestState *who)
2322 {
2323 return qtest_qmp_assert_success_ref(who,
2324 "{ 'execute': 'query-dirty-rate' }");
2325 }
2326
2327 static void dirtylimit_set_all(QTestState *who, uint64_t dirtyrate)
2328 {
2329 qtest_qmp_assert_success(who,
2330 "{ 'execute': 'set-vcpu-dirty-limit',"
2331 "'arguments': { "
2332 "'dirty-rate': %" PRIu64 " } }",
2333 dirtyrate);
2334 }
2335
2336 static void cancel_vcpu_dirty_limit(QTestState *who)
2337 {
2338 qtest_qmp_assert_success(who,
2339 "{ 'execute': 'cancel-vcpu-dirty-limit' }");
2340 }
2341
2342 static QDict *query_vcpu_dirty_limit(QTestState *who)
2343 {
2344 QDict *rsp;
2345
2346 rsp = qtest_qmp(who, "{ 'execute': 'query-vcpu-dirty-limit' }");
2347 g_assert(!qdict_haskey(rsp, "error"));
2348 g_assert(qdict_haskey(rsp, "return"));
2349
2350 return rsp;
2351 }
2352
2353 static bool calc_dirtyrate_ready(QTestState *who)
2354 {
2355 QDict *rsp_return;
2356 gchar *status;
2357
2358 rsp_return = query_dirty_rate(who);
2359 g_assert(rsp_return);
2360
2361 status = g_strdup(qdict_get_str(rsp_return, "status"));
2362 g_assert(status);
2363
2364 return g_strcmp0(status, "measuring");
2365 }
2366
2367 static void wait_for_calc_dirtyrate_complete(QTestState *who,
2368 int64_t time_s)
2369 {
2370 int max_try_count = 10000;
2371 usleep(time_s * 1000000);
2372
2373 while (!calc_dirtyrate_ready(who) && max_try_count--) {
2374 usleep(1000);
2375 }
2376
2377 /*
2378 * Set the timeout with 10 s(max_try_count * 1000us),
2379 * if dirtyrate measurement not complete, fail test.
2380 */
2381 g_assert_cmpint(max_try_count, !=, 0);
2382 }
2383
2384 static int64_t get_dirty_rate(QTestState *who)
2385 {
2386 QDict *rsp_return;
2387 gchar *status;
2388 QList *rates;
2389 const QListEntry *entry;
2390 QDict *rate;
2391 int64_t dirtyrate;
2392
2393 rsp_return = query_dirty_rate(who);
2394 g_assert(rsp_return);
2395
2396 status = g_strdup(qdict_get_str(rsp_return, "status"));
2397 g_assert(status);
2398 g_assert_cmpstr(status, ==, "measured");
2399
2400 rates = qdict_get_qlist(rsp_return, "vcpu-dirty-rate");
2401 g_assert(rates && !qlist_empty(rates));
2402
2403 entry = qlist_first(rates);
2404 g_assert(entry);
2405
2406 rate = qobject_to(QDict, qlist_entry_obj(entry));
2407 g_assert(rate);
2408
2409 dirtyrate = qdict_get_try_int(rate, "dirty-rate", -1);
2410
2411 qobject_unref(rsp_return);
2412 return dirtyrate;
2413 }
2414
2415 static int64_t get_limit_rate(QTestState *who)
2416 {
2417 QDict *rsp_return;
2418 QList *rates;
2419 const QListEntry *entry;
2420 QDict *rate;
2421 int64_t dirtyrate;
2422
2423 rsp_return = query_vcpu_dirty_limit(who);
2424 g_assert(rsp_return);
2425
2426 rates = qdict_get_qlist(rsp_return, "return");
2427 g_assert(rates && !qlist_empty(rates));
2428
2429 entry = qlist_first(rates);
2430 g_assert(entry);
2431
2432 rate = qobject_to(QDict, qlist_entry_obj(entry));
2433 g_assert(rate);
2434
2435 dirtyrate = qdict_get_try_int(rate, "limit-rate", -1);
2436
2437 qobject_unref(rsp_return);
2438 return dirtyrate;
2439 }
2440
2441 static QTestState *dirtylimit_start_vm(void)
2442 {
2443 QTestState *vm = NULL;
2444 g_autofree gchar *cmd = NULL;
2445 const char *arch = qtest_get_arch();
2446 g_autofree char *bootpath = NULL;
2447
2448 assert((strcmp(arch, "x86_64") == 0));
2449 bootpath = g_strdup_printf("%s/bootsect", tmpfs);
2450 assert(sizeof(x86_bootsect) == 512);
2451 init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect));
2452
2453 cmd = g_strdup_printf("-accel kvm,dirty-ring-size=4096 "
2454 "-name dirtylimit-test,debug-threads=on "
2455 "-m 150M -smp 1 "
2456 "-serial file:%s/vm_serial "
2457 "-drive file=%s,format=raw ",
2458 tmpfs, bootpath);
2459
2460 vm = qtest_init(cmd);
2461 return vm;
2462 }
2463
2464 static void dirtylimit_stop_vm(QTestState *vm)
2465 {
2466 qtest_quit(vm);
2467 cleanup("bootsect");
2468 cleanup("vm_serial");
2469 }
2470
2471 static void test_vcpu_dirty_limit(void)
2472 {
2473 QTestState *vm;
2474 int64_t origin_rate;
2475 int64_t quota_rate;
2476 int64_t rate ;
2477 int max_try_count = 20;
2478 int hit = 0;
2479
2480 /* Start vm for vcpu dirtylimit test */
2481 vm = dirtylimit_start_vm();
2482
2483 /* Wait for the first serial output from the vm*/
2484 wait_for_serial("vm_serial");
2485
2486 /* Do dirtyrate measurement with calc time equals 1s */
2487 calc_dirty_rate(vm, 1);
2488
2489 /* Sleep calc time and wait for calc dirtyrate complete */
2490 wait_for_calc_dirtyrate_complete(vm, 1);
2491
2492 /* Query original dirty page rate */
2493 origin_rate = get_dirty_rate(vm);
2494
2495 /* VM booted from bootsect should dirty memory steadily */
2496 assert(origin_rate != 0);
2497
2498 /* Setup quota dirty page rate at half of origin */
2499 quota_rate = origin_rate / 2;
2500
2501 /* Set dirtylimit */
2502 dirtylimit_set_all(vm, quota_rate);
2503
2504 /*
2505 * Check if set-vcpu-dirty-limit and query-vcpu-dirty-limit
2506 * works literally
2507 */
2508 g_assert_cmpint(quota_rate, ==, get_limit_rate(vm));
2509
2510 /* Sleep a bit to check if it take effect */
2511 usleep(2000000);
2512
2513 /*
2514 * Check if dirtylimit take effect realistically, set the
2515 * timeout with 20 s(max_try_count * 1s), if dirtylimit
2516 * doesn't take effect, fail test.
2517 */
2518 while (--max_try_count) {
2519 calc_dirty_rate(vm, 1);
2520 wait_for_calc_dirtyrate_complete(vm, 1);
2521 rate = get_dirty_rate(vm);
2522
2523 /*
2524 * Assume hitting if current rate is less
2525 * than quota rate (within accepting error)
2526 */
2527 if (rate < (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) {
2528 hit = 1;
2529 break;
2530 }
2531 }
2532
2533 g_assert_cmpint(hit, ==, 1);
2534
2535 hit = 0;
2536 max_try_count = 20;
2537
2538 /* Check if dirtylimit cancellation take effect */
2539 cancel_vcpu_dirty_limit(vm);
2540 while (--max_try_count) {
2541 calc_dirty_rate(vm, 1);
2542 wait_for_calc_dirtyrate_complete(vm, 1);
2543 rate = get_dirty_rate(vm);
2544
2545 /*
2546 * Assume dirtylimit be canceled if current rate is
2547 * greater than quota rate (within accepting error)
2548 */
2549 if (rate > (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) {
2550 hit = 1;
2551 break;
2552 }
2553 }
2554
2555 g_assert_cmpint(hit, ==, 1);
2556 dirtylimit_stop_vm(vm);
2557 }
2558
2559 static bool kvm_dirty_ring_supported(void)
2560 {
2561 #if defined(__linux__) && defined(HOST_X86_64)
2562 int ret, kvm_fd = open("/dev/kvm", O_RDONLY);
2563
2564 if (kvm_fd < 0) {
2565 return false;
2566 }
2567
2568 ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, KVM_CAP_DIRTY_LOG_RING);
2569 close(kvm_fd);
2570
2571 /* We test with 4096 slots */
2572 if (ret < 4096) {
2573 return false;
2574 }
2575
2576 return true;
2577 #else
2578 return false;
2579 #endif
2580 }
2581
2582 int main(int argc, char **argv)
2583 {
2584 bool has_kvm, has_tcg;
2585 bool has_uffd;
2586 const char *arch;
2587 g_autoptr(GError) err = NULL;
2588 int ret;
2589
2590 g_test_init(&argc, &argv, NULL);
2591
2592 has_kvm = qtest_has_accel("kvm");
2593 has_tcg = qtest_has_accel("tcg");
2594
2595 if (!has_tcg && !has_kvm) {
2596 g_test_skip("No KVM or TCG accelerator available");
2597 return 0;
2598 }
2599
2600 has_uffd = ufd_version_check();
2601 arch = qtest_get_arch();
2602
2603 /*
2604 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
2605 * is touchy due to race conditions on dirty bits (especially on PPC for
2606 * some reason)
2607 */
2608 if (g_str_equal(arch, "ppc64") &&
2609 (!has_kvm || access("/sys/module/kvm_hv", F_OK))) {
2610 g_test_message("Skipping test: kvm_hv not available");
2611 return g_test_run();
2612 }
2613
2614 /*
2615 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
2616 * there until the problems are resolved
2617 */
2618 if (g_str_equal(arch, "s390x") && !has_kvm) {
2619 g_test_message("Skipping test: s390x host with KVM is required");
2620 return g_test_run();
2621 }
2622
2623 tmpfs = g_dir_make_tmp("migration-test-XXXXXX", &err);
2624 if (!tmpfs) {
2625 g_test_message("Can't create temporary directory in %s: %s",
2626 g_get_tmp_dir(), err->message);
2627 }
2628 g_assert(tmpfs);
2629
2630 module_call_init(MODULE_INIT_QOM);
2631
2632 if (has_uffd) {
2633 qtest_add_func("/migration/postcopy/plain", test_postcopy);
2634 qtest_add_func("/migration/postcopy/recovery/plain",
2635 test_postcopy_recovery);
2636 qtest_add_func("/migration/postcopy/preempt/plain", test_postcopy_preempt);
2637 qtest_add_func("/migration/postcopy/preempt/recovery/plain",
2638 test_postcopy_preempt_recovery);
2639 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2640 qtest_add_func("/migration/postcopy/compress/plain",
2641 test_postcopy_compress);
2642 qtest_add_func("/migration/postcopy/recovery/compress/plain",
2643 test_postcopy_recovery_compress);
2644 }
2645 }
2646
2647 qtest_add_func("/migration/bad_dest", test_baddest);
2648 qtest_add_func("/migration/precopy/unix/plain", test_precopy_unix_plain);
2649 qtest_add_func("/migration/precopy/unix/xbzrle", test_precopy_unix_xbzrle);
2650 /*
2651 * Compression fails from time to time.
2652 * Put test here but don't enable it until everything is fixed.
2653 */
2654 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2655 qtest_add_func("/migration/precopy/unix/compress/wait",
2656 test_precopy_unix_compress);
2657 qtest_add_func("/migration/precopy/unix/compress/nowait",
2658 test_precopy_unix_compress_nowait);
2659 }
2660 #ifdef CONFIG_GNUTLS
2661 qtest_add_func("/migration/precopy/unix/tls/psk",
2662 test_precopy_unix_tls_psk);
2663
2664 if (has_uffd) {
2665 /*
2666 * NOTE: psk test is enough for postcopy, as other types of TLS
2667 * channels are tested under precopy. Here what we want to test is the
2668 * general postcopy path that has TLS channel enabled.
2669 */
2670 qtest_add_func("/migration/postcopy/tls/psk", test_postcopy_tls_psk);
2671 qtest_add_func("/migration/postcopy/recovery/tls/psk",
2672 test_postcopy_recovery_tls_psk);
2673 qtest_add_func("/migration/postcopy/preempt/tls/psk",
2674 test_postcopy_preempt_tls_psk);
2675 qtest_add_func("/migration/postcopy/preempt/recovery/tls/psk",
2676 test_postcopy_preempt_all);
2677 }
2678 #ifdef CONFIG_TASN1
2679 qtest_add_func("/migration/precopy/unix/tls/x509/default-host",
2680 test_precopy_unix_tls_x509_default_host);
2681 qtest_add_func("/migration/precopy/unix/tls/x509/override-host",
2682 test_precopy_unix_tls_x509_override_host);
2683 #endif /* CONFIG_TASN1 */
2684 #endif /* CONFIG_GNUTLS */
2685
2686 qtest_add_func("/migration/precopy/tcp/plain", test_precopy_tcp_plain);
2687 #ifdef CONFIG_GNUTLS
2688 qtest_add_func("/migration/precopy/tcp/tls/psk/match",
2689 test_precopy_tcp_tls_psk_match);
2690 qtest_add_func("/migration/precopy/tcp/tls/psk/mismatch",
2691 test_precopy_tcp_tls_psk_mismatch);
2692 #ifdef CONFIG_TASN1
2693 qtest_add_func("/migration/precopy/tcp/tls/x509/default-host",
2694 test_precopy_tcp_tls_x509_default_host);
2695 qtest_add_func("/migration/precopy/tcp/tls/x509/override-host",
2696 test_precopy_tcp_tls_x509_override_host);
2697 qtest_add_func("/migration/precopy/tcp/tls/x509/mismatch-host",
2698 test_precopy_tcp_tls_x509_mismatch_host);
2699 qtest_add_func("/migration/precopy/tcp/tls/x509/friendly-client",
2700 test_precopy_tcp_tls_x509_friendly_client);
2701 qtest_add_func("/migration/precopy/tcp/tls/x509/hostile-client",
2702 test_precopy_tcp_tls_x509_hostile_client);
2703 qtest_add_func("/migration/precopy/tcp/tls/x509/allow-anon-client",
2704 test_precopy_tcp_tls_x509_allow_anon_client);
2705 qtest_add_func("/migration/precopy/tcp/tls/x509/reject-anon-client",
2706 test_precopy_tcp_tls_x509_reject_anon_client);
2707 #endif /* CONFIG_TASN1 */
2708 #endif /* CONFIG_GNUTLS */
2709
2710 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
2711 #ifndef _WIN32
2712 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto);
2713 #endif
2714 qtest_add_func("/migration/validate_uuid", test_validate_uuid);
2715 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error);
2716 qtest_add_func("/migration/validate_uuid_src_not_set",
2717 test_validate_uuid_src_not_set);
2718 qtest_add_func("/migration/validate_uuid_dst_not_set",
2719 test_validate_uuid_dst_not_set);
2720 /*
2721 * See explanation why this test is slow on function definition
2722 */
2723 if (g_test_slow()) {
2724 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge);
2725 }
2726 qtest_add_func("/migration/multifd/tcp/plain/none",
2727 test_multifd_tcp_none);
2728 /*
2729 * This test is flaky and sometimes fails in CI and otherwise:
2730 * don't run unless user opts in via environment variable.
2731 */
2732 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2733 qtest_add_func("/migration/multifd/tcp/plain/cancel",
2734 test_multifd_tcp_cancel);
2735 }
2736 qtest_add_func("/migration/multifd/tcp/plain/zlib",
2737 test_multifd_tcp_zlib);
2738 #ifdef CONFIG_ZSTD
2739 qtest_add_func("/migration/multifd/tcp/plain/zstd",
2740 test_multifd_tcp_zstd);
2741 #endif
2742 #ifdef CONFIG_GNUTLS
2743 qtest_add_func("/migration/multifd/tcp/tls/psk/match",
2744 test_multifd_tcp_tls_psk_match);
2745 qtest_add_func("/migration/multifd/tcp/tls/psk/mismatch",
2746 test_multifd_tcp_tls_psk_mismatch);
2747 #ifdef CONFIG_TASN1
2748 qtest_add_func("/migration/multifd/tcp/tls/x509/default-host",
2749 test_multifd_tcp_tls_x509_default_host);
2750 qtest_add_func("/migration/multifd/tcp/tls/x509/override-host",
2751 test_multifd_tcp_tls_x509_override_host);
2752 qtest_add_func("/migration/multifd/tcp/tls/x509/mismatch-host",
2753 test_multifd_tcp_tls_x509_mismatch_host);
2754 qtest_add_func("/migration/multifd/tcp/tls/x509/allow-anon-client",
2755 test_multifd_tcp_tls_x509_allow_anon_client);
2756 qtest_add_func("/migration/multifd/tcp/tls/x509/reject-anon-client",
2757 test_multifd_tcp_tls_x509_reject_anon_client);
2758 #endif /* CONFIG_TASN1 */
2759 #endif /* CONFIG_GNUTLS */
2760
2761 if (g_str_equal(arch, "x86_64") && has_kvm && kvm_dirty_ring_supported()) {
2762 qtest_add_func("/migration/dirty_ring",
2763 test_precopy_unix_dirty_ring);
2764 qtest_add_func("/migration/vcpu_dirty_limit",
2765 test_vcpu_dirty_limit);
2766 }
2767
2768 ret = g_test_run();
2769
2770 g_assert_cmpint(ret, ==, 0);
2771
2772 ret = rmdir(tmpfs);
2773 if (ret != 0) {
2774 g_test_message("unable to rmdir: path (%s): %s",
2775 tmpfs, strerror(errno));
2776 }
2777 g_free(tmpfs);
2778
2779 return ret;
2780 }