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