xref: /qemu/tests/qtest/migration/precopy-tests.c (revision cd196679f413c7905143e5eb9b7d63b7a7eea158)
1 /*
2  * QTest testcase for precopy 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 #include "chardev/char.h"
15 #include "crypto/tlscredspsk.h"
16 #include "libqtest.h"
17 #include "migration/bootfile.h"
18 #include "migration/framework.h"
19 #include "migration/migration-qmp.h"
20 #include "migration/migration-util.h"
21 #include "ppc-util.h"
22 #include "qapi/qmp/qlist.h"
23 #include "qemu/module.h"
24 #include "qemu/option.h"
25 #include "qemu/range.h"
26 #include "qemu/sockets.h"
27 
28 
29 /*
30  * Dirtylimit stop working if dirty page rate error
31  * value less than DIRTYLIMIT_TOLERANCE_RANGE
32  */
33 #define DIRTYLIMIT_TOLERANCE_RANGE  25  /* MB/s */
34 
35 static char *tmpfs;
36 
37 static void test_precopy_unix_plain(void)
38 {
39     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
40     MigrateCommon args = {
41         .listen_uri = uri,
42         .connect_uri = uri,
43         /*
44          * The simplest use case of precopy, covering smoke tests of
45          * get-dirty-log dirty tracking.
46          */
47         .live = true,
48     };
49 
50     test_precopy_common(&args);
51 }
52 
53 static void test_precopy_unix_suspend_live(void)
54 {
55     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
56     MigrateCommon args = {
57         .listen_uri = uri,
58         .connect_uri = uri,
59         /*
60          * despite being live, the test is fast because the src
61          * suspends immediately.
62          */
63         .live = true,
64         .start.suspend_me = true,
65     };
66 
67     test_precopy_common(&args);
68 }
69 
70 static void test_precopy_unix_suspend_notlive(void)
71 {
72     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
73     MigrateCommon args = {
74         .listen_uri = uri,
75         .connect_uri = uri,
76         .start.suspend_me = true,
77     };
78 
79     test_precopy_common(&args);
80 }
81 
82 static void test_precopy_unix_dirty_ring(void)
83 {
84     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
85     MigrateCommon args = {
86         .start = {
87             .use_dirty_ring = true,
88         },
89         .listen_uri = uri,
90         .connect_uri = uri,
91         /*
92          * Besides the precopy/unix basic test, cover dirty ring interface
93          * rather than get-dirty-log.
94          */
95         .live = true,
96     };
97 
98     test_precopy_common(&args);
99 }
100 
101 static void test_precopy_tcp_plain(void)
102 {
103     MigrateCommon args = {
104         .listen_uri = "tcp:127.0.0.1:0",
105     };
106 
107     test_precopy_common(&args);
108 }
109 
110 static void *migrate_hook_start_switchover_ack(QTestState *from, QTestState *to)
111 {
112 
113     migrate_set_capability(from, "return-path", true);
114     migrate_set_capability(to, "return-path", true);
115 
116     migrate_set_capability(from, "switchover-ack", true);
117     migrate_set_capability(to, "switchover-ack", true);
118 
119     return NULL;
120 }
121 
122 static void test_precopy_tcp_switchover_ack(void)
123 {
124     MigrateCommon args = {
125         .listen_uri = "tcp:127.0.0.1:0",
126         .start_hook = migrate_hook_start_switchover_ack,
127         /*
128          * Source VM must be running in order to consider the switchover ACK
129          * when deciding to do switchover or not.
130          */
131         .live = true,
132     };
133 
134     test_precopy_common(&args);
135 }
136 
137 #ifndef _WIN32
138 static void *migrate_hook_start_fd(QTestState *from,
139                                    QTestState *to)
140 {
141     int ret;
142     int pair[2];
143 
144     /* Create two connected sockets for migration */
145     ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
146     g_assert_cmpint(ret, ==, 0);
147 
148     /* Send the 1st socket to the target */
149     qtest_qmp_fds_assert_success(to, &pair[0], 1,
150                                  "{ 'execute': 'getfd',"
151                                  "  'arguments': { 'fdname': 'fd-mig' }}");
152     close(pair[0]);
153 
154     /* Start incoming migration from the 1st socket */
155     migrate_incoming_qmp(to, "fd:fd-mig", "{}");
156 
157     /* Send the 2nd socket to the target */
158     qtest_qmp_fds_assert_success(from, &pair[1], 1,
159                                  "{ 'execute': 'getfd',"
160                                  "  'arguments': { 'fdname': 'fd-mig' }}");
161     close(pair[1]);
162 
163     return NULL;
164 }
165 
166 static void migrate_hook_end_fd(QTestState *from,
167                                 QTestState *to,
168                                 void *opaque)
169 {
170     QDict *rsp;
171     const char *error_desc;
172 
173     /* Test closing fds */
174     /*
175      * We assume, that QEMU removes named fd from its list,
176      * so this should fail.
177      */
178     rsp = qtest_qmp(from,
179                     "{ 'execute': 'closefd',"
180                     "  'arguments': { 'fdname': 'fd-mig' }}");
181     g_assert_true(qdict_haskey(rsp, "error"));
182     error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
183     g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
184     qobject_unref(rsp);
185 
186     rsp = qtest_qmp(to,
187                     "{ 'execute': 'closefd',"
188                     "  'arguments': { 'fdname': 'fd-mig' }}");
189     g_assert_true(qdict_haskey(rsp, "error"));
190     error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
191     g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
192     qobject_unref(rsp);
193 }
194 
195 static void test_precopy_fd_socket(void)
196 {
197     MigrateCommon args = {
198         .listen_uri = "defer",
199         .connect_uri = "fd:fd-mig",
200         .start_hook = migrate_hook_start_fd,
201         .end_hook = migrate_hook_end_fd,
202     };
203     test_precopy_common(&args);
204 }
205 
206 static void *migrate_hook_start_precopy_fd_file(QTestState *from,
207                                                 QTestState *to)
208 {
209     g_autofree char *file = g_strdup_printf("%s/%s", tmpfs, FILE_TEST_FILENAME);
210     int src_flags = O_CREAT | O_RDWR;
211     int dst_flags = O_CREAT | O_RDWR;
212     int fds[2];
213 
214     fds[0] = open(file, src_flags, 0660);
215     assert(fds[0] != -1);
216 
217     fds[1] = open(file, dst_flags, 0660);
218     assert(fds[1] != -1);
219 
220 
221     qtest_qmp_fds_assert_success(to, &fds[0], 1,
222                                  "{ 'execute': 'getfd',"
223                                  "  'arguments': { 'fdname': 'fd-mig' }}");
224 
225     qtest_qmp_fds_assert_success(from, &fds[1], 1,
226                                  "{ 'execute': 'getfd',"
227                                  "  'arguments': { 'fdname': 'fd-mig' }}");
228 
229     close(fds[0]);
230     close(fds[1]);
231 
232     return NULL;
233 }
234 
235 static void test_precopy_fd_file(void)
236 {
237     MigrateCommon args = {
238         .listen_uri = "defer",
239         .connect_uri = "fd:fd-mig",
240         .start_hook = migrate_hook_start_precopy_fd_file,
241         .end_hook = migrate_hook_end_fd,
242     };
243     test_file_common(&args, true);
244 }
245 #endif /* _WIN32 */
246 
247 /*
248  * The way auto_converge works, we need to do too many passes to
249  * run this test.  Auto_converge logic is only run once every
250  * three iterations, so:
251  *
252  * - 3 iterations without auto_converge enabled
253  * - 3 iterations with pct = 5
254  * - 3 iterations with pct = 30
255  * - 3 iterations with pct = 55
256  * - 3 iterations with pct = 80
257  * - 3 iterations with pct = 95 (max(95, 80 + 25))
258  *
259  * To make things even worse, we need to run the initial stage at
260  * 3MB/s so we enter autoconverge even when host is (over)loaded.
261  */
262 static void test_auto_converge(void)
263 {
264     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
265     MigrateStart args = {};
266     QTestState *from, *to;
267     int64_t percentage;
268 
269     /*
270      * We want the test to be stable and as fast as possible.
271      * E.g., with 1Gb/s bandwidth migration may pass without throttling,
272      * so we need to decrease a bandwidth.
273      */
274     const int64_t init_pct = 5, inc_pct = 25, max_pct = 95;
275     uint64_t prev_dirty_sync_cnt, dirty_sync_cnt;
276     int max_try_count, hit = 0;
277 
278     if (migrate_start(&from, &to, uri, &args)) {
279         return;
280     }
281 
282     migrate_set_capability(from, "auto-converge", true);
283     migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct);
284     migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct);
285     migrate_set_parameter_int(from, "max-cpu-throttle", max_pct);
286 
287     /*
288      * Set the initial parameters so that the migration could not converge
289      * without throttling.
290      */
291     migrate_ensure_non_converge(from);
292 
293     /* To check remaining size after precopy */
294     migrate_set_capability(from, "pause-before-switchover", true);
295 
296     /* Wait for the first serial output from the source */
297     wait_for_serial("src_serial");
298 
299     migrate_qmp(from, to, uri, NULL, "{}");
300 
301     /* Wait for throttling begins */
302     percentage = 0;
303     do {
304         percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
305         if (percentage != 0) {
306             break;
307         }
308         usleep(20);
309         g_assert_false(get_src()->stop_seen);
310     } while (true);
311     /* The first percentage of throttling should be at least init_pct */
312     g_assert_cmpint(percentage, >=, init_pct);
313 
314     /*
315      * End the loop when the dirty sync count greater than 1.
316      */
317     while ((dirty_sync_cnt = get_migration_pass(from)) < 2) {
318         usleep(1000 * 1000);
319     }
320 
321     prev_dirty_sync_cnt = dirty_sync_cnt;
322 
323     /*
324      * The RAMBlock dirty sync count must changes in 5 seconds, here we set
325      * the timeout to 10 seconds to ensure it changes.
326      *
327      * Note that migrate_ensure_non_converge set the max-bandwidth to 3MB/s,
328      * while the qtest mem is >= 100MB, one iteration takes at least 33s (100/3)
329      * to complete; this ensures that the RAMBlock dirty sync occurs.
330      */
331     max_try_count = 10;
332     while (--max_try_count) {
333         dirty_sync_cnt = get_migration_pass(from);
334         if (dirty_sync_cnt != prev_dirty_sync_cnt) {
335             hit = 1;
336             break;
337         }
338         prev_dirty_sync_cnt = dirty_sync_cnt;
339         sleep(1);
340     }
341     g_assert_cmpint(hit, ==, 1);
342 
343     /* Now, when we tested that throttling works, let it converge */
344     migrate_ensure_converge(from);
345 
346     /*
347      * Wait for pre-switchover status to check last throttle percentage
348      * and remaining. These values will be zeroed later
349      */
350     wait_for_migration_status(from, "pre-switchover", NULL);
351 
352     /* The final percentage of throttling shouldn't be greater than max_pct */
353     percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
354     g_assert_cmpint(percentage, <=, max_pct);
355     migrate_continue(from, "pre-switchover");
356 
357     qtest_qmp_eventwait(to, "RESUME");
358 
359     wait_for_serial("dest_serial");
360     wait_for_migration_complete(from);
361 
362     migrate_end(from, to, true);
363 }
364 
365 static void *
366 migrate_hook_start_precopy_tcp_multifd(QTestState *from,
367                                        QTestState *to)
368 {
369     return migrate_hook_start_precopy_tcp_multifd_common(from, to, "none");
370 }
371 
372 static void *
373 migrate_hook_start_precopy_tcp_multifd_zero_page_legacy(QTestState *from,
374                                                         QTestState *to)
375 {
376     migrate_hook_start_precopy_tcp_multifd_common(from, to, "none");
377     migrate_set_parameter_str(from, "zero-page-detection", "legacy");
378     return NULL;
379 }
380 
381 static void *
382 migrate_hook_start_precopy_tcp_multifd_no_zero_page(QTestState *from,
383                                                     QTestState *to)
384 {
385     migrate_hook_start_precopy_tcp_multifd_common(from, to, "none");
386     migrate_set_parameter_str(from, "zero-page-detection", "none");
387     return NULL;
388 }
389 
390 static void test_multifd_tcp_uri_none(void)
391 {
392     MigrateCommon args = {
393         .listen_uri = "defer",
394         .start_hook = migrate_hook_start_precopy_tcp_multifd,
395         /*
396          * Multifd is more complicated than most of the features, it
397          * directly takes guest page buffers when sending, make sure
398          * everything will work alright even if guest page is changing.
399          */
400         .live = true,
401     };
402     test_precopy_common(&args);
403 }
404 
405 static void test_multifd_tcp_zero_page_legacy(void)
406 {
407     MigrateCommon args = {
408         .listen_uri = "defer",
409         .start_hook = migrate_hook_start_precopy_tcp_multifd_zero_page_legacy,
410         /*
411          * Multifd is more complicated than most of the features, it
412          * directly takes guest page buffers when sending, make sure
413          * everything will work alright even if guest page is changing.
414          */
415         .live = true,
416     };
417     test_precopy_common(&args);
418 }
419 
420 static void test_multifd_tcp_no_zero_page(void)
421 {
422     MigrateCommon args = {
423         .listen_uri = "defer",
424         .start_hook = migrate_hook_start_precopy_tcp_multifd_no_zero_page,
425         /*
426          * Multifd is more complicated than most of the features, it
427          * directly takes guest page buffers when sending, make sure
428          * everything will work alright even if guest page is changing.
429          */
430         .live = true,
431     };
432     test_precopy_common(&args);
433 }
434 
435 static void test_multifd_tcp_channels_none(void)
436 {
437     MigrateCommon args = {
438         .listen_uri = "defer",
439         .start_hook = migrate_hook_start_precopy_tcp_multifd,
440         .live = true,
441         .connect_channels = ("[ { 'channel-type': 'main',"
442                              "    'addr': { 'transport': 'socket',"
443                              "              'type': 'inet',"
444                              "              'host': '127.0.0.1',"
445                              "              'port': '0' } } ]"),
446     };
447     test_precopy_common(&args);
448 }
449 
450 /*
451  * This test does:
452  *  source               target
453  *                       migrate_incoming
454  *     migrate
455  *     migrate_cancel
456  *                       launch another target
457  *     migrate
458  *
459  *  And see that it works
460  */
461 static void test_multifd_tcp_cancel(void)
462 {
463     MigrateStart args = {
464         .hide_stderr = true,
465     };
466     QTestState *from, *to, *to2;
467 
468     if (migrate_start(&from, &to, "defer", &args)) {
469         return;
470     }
471 
472     migrate_ensure_non_converge(from);
473     migrate_prepare_for_dirty_mem(from);
474 
475     migrate_set_parameter_int(from, "multifd-channels", 16);
476     migrate_set_parameter_int(to, "multifd-channels", 16);
477 
478     migrate_set_capability(from, "multifd", true);
479     migrate_set_capability(to, "multifd", true);
480 
481     /* Start incoming migration from the 1st socket */
482     migrate_incoming_qmp(to, "tcp:127.0.0.1:0", "{}");
483 
484     /* Wait for the first serial output from the source */
485     wait_for_serial("src_serial");
486 
487     migrate_qmp(from, to, NULL, NULL, "{}");
488 
489     migrate_wait_for_dirty_mem(from, to);
490 
491     migrate_cancel(from);
492 
493     /* Make sure QEMU process "to" exited */
494     qtest_set_expected_status(to, EXIT_FAILURE);
495     qtest_wait_qemu(to);
496     qtest_quit(to);
497 
498     /*
499      * Ensure the source QEMU finishes its cancellation process before we
500      * proceed with the setup of the next migration. The migrate_start()
501      * function and others might want to interact with the source in a way that
502      * is not possible while the migration is not canceled properly. For
503      * example, setting migration capabilities when the migration is still
504      * running leads to an error.
505      */
506     wait_for_migration_status(from, "cancelled", NULL);
507 
508     args = (MigrateStart){
509         .only_target = true,
510     };
511 
512     if (migrate_start(&from, &to2, "defer", &args)) {
513         return;
514     }
515 
516     migrate_set_parameter_int(to2, "multifd-channels", 16);
517 
518     migrate_set_capability(to2, "multifd", true);
519 
520     /* Start incoming migration from the 1st socket */
521     migrate_incoming_qmp(to2, "tcp:127.0.0.1:0", "{}");
522 
523     migrate_ensure_non_converge(from);
524 
525     migrate_qmp(from, to2, NULL, NULL, "{}");
526 
527     migrate_wait_for_dirty_mem(from, to2);
528 
529     migrate_ensure_converge(from);
530 
531     wait_for_stop(from, get_src());
532     qtest_qmp_eventwait(to2, "RESUME");
533 
534     wait_for_serial("dest_serial");
535     wait_for_migration_complete(from);
536     migrate_end(from, to2, true);
537 }
538 
539 static void calc_dirty_rate(QTestState *who, uint64_t calc_time)
540 {
541     qtest_qmp_assert_success(who,
542                              "{ 'execute': 'calc-dirty-rate',"
543                              "'arguments': { "
544                              "'calc-time': %" PRIu64 ","
545                              "'mode': 'dirty-ring' }}",
546                              calc_time);
547 }
548 
549 static QDict *query_dirty_rate(QTestState *who)
550 {
551     return qtest_qmp_assert_success_ref(who,
552                                         "{ 'execute': 'query-dirty-rate' }");
553 }
554 
555 static void dirtylimit_set_all(QTestState *who, uint64_t dirtyrate)
556 {
557     qtest_qmp_assert_success(who,
558                              "{ 'execute': 'set-vcpu-dirty-limit',"
559                              "'arguments': { "
560                              "'dirty-rate': %" PRIu64 " } }",
561                              dirtyrate);
562 }
563 
564 static void cancel_vcpu_dirty_limit(QTestState *who)
565 {
566     qtest_qmp_assert_success(who,
567                              "{ 'execute': 'cancel-vcpu-dirty-limit' }");
568 }
569 
570 static QDict *query_vcpu_dirty_limit(QTestState *who)
571 {
572     QDict *rsp;
573 
574     rsp = qtest_qmp(who, "{ 'execute': 'query-vcpu-dirty-limit' }");
575     g_assert(!qdict_haskey(rsp, "error"));
576     g_assert(qdict_haskey(rsp, "return"));
577 
578     return rsp;
579 }
580 
581 static bool calc_dirtyrate_ready(QTestState *who)
582 {
583     QDict *rsp_return;
584     const char *status;
585     bool ready;
586 
587     rsp_return = query_dirty_rate(who);
588     g_assert(rsp_return);
589 
590     status = qdict_get_str(rsp_return, "status");
591     g_assert(status);
592     ready = g_strcmp0(status, "measuring");
593     qobject_unref(rsp_return);
594 
595     return ready;
596 }
597 
598 static void wait_for_calc_dirtyrate_complete(QTestState *who,
599                                              int64_t time_s)
600 {
601     int max_try_count = 10000;
602     usleep(time_s * 1000000);
603 
604     while (!calc_dirtyrate_ready(who) && max_try_count--) {
605         usleep(1000);
606     }
607 
608     /*
609      * Set the timeout with 10 s(max_try_count * 1000us),
610      * if dirtyrate measurement not complete, fail test.
611      */
612     g_assert_cmpint(max_try_count, !=, 0);
613 }
614 
615 static int64_t get_dirty_rate(QTestState *who)
616 {
617     QDict *rsp_return;
618     const char *status;
619     QList *rates;
620     const QListEntry *entry;
621     QDict *rate;
622     int64_t dirtyrate;
623 
624     rsp_return = query_dirty_rate(who);
625     g_assert(rsp_return);
626 
627     status = qdict_get_str(rsp_return, "status");
628     g_assert(status);
629     g_assert_cmpstr(status, ==, "measured");
630 
631     rates = qdict_get_qlist(rsp_return, "vcpu-dirty-rate");
632     g_assert(rates && !qlist_empty(rates));
633 
634     entry = qlist_first(rates);
635     g_assert(entry);
636 
637     rate = qobject_to(QDict, qlist_entry_obj(entry));
638     g_assert(rate);
639 
640     dirtyrate = qdict_get_try_int(rate, "dirty-rate", -1);
641 
642     qobject_unref(rsp_return);
643     return dirtyrate;
644 }
645 
646 static int64_t get_limit_rate(QTestState *who)
647 {
648     QDict *rsp_return;
649     QList *rates;
650     const QListEntry *entry;
651     QDict *rate;
652     int64_t dirtyrate;
653 
654     rsp_return = query_vcpu_dirty_limit(who);
655     g_assert(rsp_return);
656 
657     rates = qdict_get_qlist(rsp_return, "return");
658     g_assert(rates && !qlist_empty(rates));
659 
660     entry = qlist_first(rates);
661     g_assert(entry);
662 
663     rate = qobject_to(QDict, qlist_entry_obj(entry));
664     g_assert(rate);
665 
666     dirtyrate = qdict_get_try_int(rate, "limit-rate", -1);
667 
668     qobject_unref(rsp_return);
669     return dirtyrate;
670 }
671 
672 static QTestState *dirtylimit_start_vm(void)
673 {
674     QTestState *vm = NULL;
675     g_autofree gchar *cmd = NULL;
676     const char *bootpath;
677 
678     bootpath = bootfile_create(qtest_get_arch(), tmpfs, false);
679     cmd = g_strdup_printf("-accel kvm,dirty-ring-size=4096 "
680                           "-name dirtylimit-test,debug-threads=on "
681                           "-m 150M -smp 1 "
682                           "-serial file:%s/vm_serial "
683                           "-drive file=%s,format=raw ",
684                           tmpfs, bootpath);
685 
686     vm = qtest_init(cmd);
687     return vm;
688 }
689 
690 static void dirtylimit_stop_vm(QTestState *vm)
691 {
692     g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, "vm_serial");
693 
694     qtest_quit(vm);
695     unlink(path);
696 }
697 
698 static void test_vcpu_dirty_limit(void)
699 {
700     QTestState *vm;
701     int64_t origin_rate;
702     int64_t quota_rate;
703     int64_t rate ;
704     int max_try_count = 20;
705     int hit = 0;
706 
707     /* Start vm for vcpu dirtylimit test */
708     vm = dirtylimit_start_vm();
709 
710     /* Wait for the first serial output from the vm*/
711     wait_for_serial("vm_serial");
712 
713     /* Do dirtyrate measurement with calc time equals 1s */
714     calc_dirty_rate(vm, 1);
715 
716     /* Sleep calc time and wait for calc dirtyrate complete */
717     wait_for_calc_dirtyrate_complete(vm, 1);
718 
719     /* Query original dirty page rate */
720     origin_rate = get_dirty_rate(vm);
721 
722     /* VM booted from bootsect should dirty memory steadily */
723     assert(origin_rate != 0);
724 
725     /* Setup quota dirty page rate at half of origin */
726     quota_rate = origin_rate / 2;
727 
728     /* Set dirtylimit */
729     dirtylimit_set_all(vm, quota_rate);
730 
731     /*
732      * Check if set-vcpu-dirty-limit and query-vcpu-dirty-limit
733      * works literally
734      */
735     g_assert_cmpint(quota_rate, ==, get_limit_rate(vm));
736 
737     /* Sleep a bit to check if it take effect */
738     usleep(2000000);
739 
740     /*
741      * Check if dirtylimit take effect realistically, set the
742      * timeout with 20 s(max_try_count * 1s), if dirtylimit
743      * doesn't take effect, fail test.
744      */
745     while (--max_try_count) {
746         calc_dirty_rate(vm, 1);
747         wait_for_calc_dirtyrate_complete(vm, 1);
748         rate = get_dirty_rate(vm);
749 
750         /*
751          * Assume hitting if current rate is less
752          * than quota rate (within accepting error)
753          */
754         if (rate < (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) {
755             hit = 1;
756             break;
757         }
758     }
759 
760     g_assert_cmpint(hit, ==, 1);
761 
762     hit = 0;
763     max_try_count = 20;
764 
765     /* Check if dirtylimit cancellation take effect */
766     cancel_vcpu_dirty_limit(vm);
767     while (--max_try_count) {
768         calc_dirty_rate(vm, 1);
769         wait_for_calc_dirtyrate_complete(vm, 1);
770         rate = get_dirty_rate(vm);
771 
772         /*
773          * Assume dirtylimit be canceled if current rate is
774          * greater than quota rate (within accepting error)
775          */
776         if (rate > (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) {
777             hit = 1;
778             break;
779         }
780     }
781 
782     g_assert_cmpint(hit, ==, 1);
783     dirtylimit_stop_vm(vm);
784 }
785 
786 static void migrate_dirty_limit_wait_showup(QTestState *from,
787                                             const int64_t period,
788                                             const int64_t value)
789 {
790     /* Enable dirty limit capability */
791     migrate_set_capability(from, "dirty-limit", true);
792 
793     /* Set dirty limit parameters */
794     migrate_set_parameter_int(from, "x-vcpu-dirty-limit-period", period);
795     migrate_set_parameter_int(from, "vcpu-dirty-limit", value);
796 
797     /* Make sure migrate can't converge */
798     migrate_ensure_non_converge(from);
799 
800     /* To check limit rate after precopy */
801     migrate_set_capability(from, "pause-before-switchover", true);
802 
803     /* Wait for the serial output from the source */
804     wait_for_serial("src_serial");
805 }
806 
807 /*
808  * This test does:
809  *  source                          destination
810  *  start vm
811  *                                  start incoming vm
812  *  migrate
813  *  wait dirty limit to begin
814  *  cancel migrate
815  *  cancellation check
816  *                                  restart incoming vm
817  *  migrate
818  *  wait dirty limit to begin
819  *  wait pre-switchover event
820  *  convergence condition check
821  *
822  * And see if dirty limit migration works correctly.
823  * This test case involves many passes, so it runs in slow mode only.
824  */
825 static void test_dirty_limit(void)
826 {
827     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
828     QTestState *from, *to;
829     int64_t remaining;
830     uint64_t throttle_us_per_full;
831     /*
832      * We want the test to be stable and as fast as possible.
833      * E.g., with 1Gb/s bandwidth migration may pass without dirty limit,
834      * so we need to decrease a bandwidth.
835      */
836     const int64_t dirtylimit_period = 1000, dirtylimit_value = 50;
837     const int64_t max_bandwidth = 400000000; /* ~400Mb/s */
838     const int64_t downtime_limit = 250; /* 250ms */
839     /*
840      * We migrate through unix-socket (> 500Mb/s).
841      * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
842      * So, we can predict expected_threshold
843      */
844     const int64_t expected_threshold = max_bandwidth * downtime_limit / 1000;
845     int max_try_count = 10;
846     MigrateCommon args = {
847         .start = {
848             .hide_stderr = true,
849             .use_dirty_ring = true,
850         },
851         .listen_uri = uri,
852         .connect_uri = uri,
853     };
854 
855     /* Start src, dst vm */
856     if (migrate_start(&from, &to, args.listen_uri, &args.start)) {
857         return;
858     }
859 
860     /* Prepare for dirty limit migration and wait src vm show up */
861     migrate_dirty_limit_wait_showup(from, dirtylimit_period, dirtylimit_value);
862 
863     /* Start migrate */
864     migrate_qmp(from, to, args.connect_uri, NULL, "{}");
865 
866     /* Wait for dirty limit throttle begin */
867     throttle_us_per_full = 0;
868     while (throttle_us_per_full == 0) {
869         throttle_us_per_full =
870             read_migrate_property_int(from,
871                                       "dirty-limit-throttle-time-per-round");
872         usleep(100);
873         g_assert_false(get_src()->stop_seen);
874     }
875 
876     /* Now cancel migrate and wait for dirty limit throttle switch off */
877     migrate_cancel(from);
878     wait_for_migration_status(from, "cancelled", NULL);
879 
880     /* destination always fails after cancel */
881     migration_event_wait(to, "failed");
882     qtest_set_expected_status(to, EXIT_FAILURE);
883     qtest_quit(to);
884 
885     /* Check if dirty limit throttle switched off, set timeout 1ms */
886     do {
887         throttle_us_per_full =
888             read_migrate_property_int(from,
889                                       "dirty-limit-throttle-time-per-round");
890         usleep(100);
891         g_assert_false(get_src()->stop_seen);
892     } while (throttle_us_per_full != 0 && --max_try_count);
893 
894     /* Assert dirty limit is not in service */
895     g_assert_cmpint(throttle_us_per_full, ==, 0);
896 
897     args = (MigrateCommon) {
898         .start = {
899             .only_target = true,
900             .use_dirty_ring = true,
901         },
902         .listen_uri = uri,
903         .connect_uri = uri,
904     };
905 
906     /* Restart dst vm, src vm already show up so we needn't wait anymore */
907     if (migrate_start(&from, &to, args.listen_uri, &args.start)) {
908         return;
909     }
910 
911     /* Start migrate */
912     migrate_qmp(from, to, args.connect_uri, NULL, "{}");
913 
914     /* Wait for dirty limit throttle begin */
915     throttle_us_per_full = 0;
916     while (throttle_us_per_full == 0) {
917         throttle_us_per_full =
918             read_migrate_property_int(from,
919                                       "dirty-limit-throttle-time-per-round");
920         usleep(100);
921         g_assert_false(get_src()->stop_seen);
922     }
923 
924     /*
925      * The dirty limit rate should equals the return value of
926      * query-vcpu-dirty-limit if dirty limit cap set
927      */
928     g_assert_cmpint(dirtylimit_value, ==, get_limit_rate(from));
929 
930     /* Now, we have tested if dirty limit works, let it converge */
931     migrate_set_parameter_int(from, "downtime-limit", downtime_limit);
932     migrate_set_parameter_int(from, "max-bandwidth", max_bandwidth);
933 
934     /*
935      * Wait for pre-switchover status to check if migration
936      * satisfy the convergence condition
937      */
938     wait_for_migration_status(from, "pre-switchover", NULL);
939 
940     remaining = read_ram_property_int(from, "remaining");
941     g_assert_cmpint(remaining, <,
942                     (expected_threshold + expected_threshold / 100));
943 
944     migrate_continue(from, "pre-switchover");
945 
946     qtest_qmp_eventwait(to, "RESUME");
947 
948     wait_for_serial("dest_serial");
949     wait_for_migration_complete(from);
950 
951     migrate_end(from, to, true);
952 }
953 
954 void migration_test_add_precopy(MigrationTestEnv *env)
955 {
956     tmpfs = env->tmpfs;
957 
958     if (env->is_x86) {
959         migration_test_add("/migration/precopy/unix/suspend/live",
960                            test_precopy_unix_suspend_live);
961         migration_test_add("/migration/precopy/unix/suspend/notlive",
962                            test_precopy_unix_suspend_notlive);
963     }
964 
965     migration_test_add("/migration/precopy/unix/plain",
966                        test_precopy_unix_plain);
967 
968     migration_test_add("/migration/precopy/tcp/plain", test_precopy_tcp_plain);
969 
970     migration_test_add("/migration/precopy/tcp/plain/switchover-ack",
971                        test_precopy_tcp_switchover_ack);
972 
973 #ifndef _WIN32
974     migration_test_add("/migration/precopy/fd/tcp",
975                        test_precopy_fd_socket);
976     migration_test_add("/migration/precopy/fd/file",
977                        test_precopy_fd_file);
978 #endif
979 
980     /*
981      * See explanation why this test is slow on function definition
982      */
983     if (g_test_slow()) {
984         migration_test_add("/migration/auto_converge",
985                            test_auto_converge);
986         if (g_str_equal(env->arch, "x86_64") &&
987             env->has_kvm && env->has_dirty_ring) {
988             migration_test_add("/dirty_limit",
989                                test_dirty_limit);
990         }
991     }
992     migration_test_add("/migration/multifd/tcp/uri/plain/none",
993                        test_multifd_tcp_uri_none);
994     migration_test_add("/migration/multifd/tcp/channels/plain/none",
995                        test_multifd_tcp_channels_none);
996     migration_test_add("/migration/multifd/tcp/plain/zero-page/legacy",
997                        test_multifd_tcp_zero_page_legacy);
998     migration_test_add("/migration/multifd/tcp/plain/zero-page/none",
999                        test_multifd_tcp_no_zero_page);
1000     migration_test_add("/migration/multifd/tcp/plain/cancel",
1001                        test_multifd_tcp_cancel);
1002     if (g_str_equal(env->arch, "x86_64")
1003         && env->has_kvm && env->has_dirty_ring) {
1004 
1005         migration_test_add("/migration/dirty_ring",
1006                            test_precopy_unix_dirty_ring);
1007         if (qtest_has_machine("pc") && g_test_slow()) {
1008             migration_test_add("/migration/vcpu_dirty_limit",
1009                                test_vcpu_dirty_limit);
1010         }
1011     }
1012 }
1013