1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include "qemu/osdep.h"
17#include "qemu/cutils.h"
18#include "qemu/error-report.h"
19#include "qemu/main-loop.h"
20#include "migration/blocker.h"
21#include "exec.h"
22#include "fd.h"
23#include "socket.h"
24#include "sysemu/runstate.h"
25#include "sysemu/sysemu.h"
26#include "sysemu/cpu-throttle.h"
27#include "rdma.h"
28#include "ram.h"
29#include "ram-compress.h"
30#include "migration/global_state.h"
31#include "migration/misc.h"
32#include "migration.h"
33#include "migration-stats.h"
34#include "savevm.h"
35#include "qemu-file.h"
36#include "channel.h"
37#include "migration/vmstate.h"
38#include "block/block.h"
39#include "qapi/error.h"
40#include "qapi/clone-visitor.h"
41#include "qapi/qapi-visit-migration.h"
42#include "qapi/qapi-visit-sockets.h"
43#include "qapi/qapi-commands-migration.h"
44#include "qapi/qapi-events-migration.h"
45#include "qapi/qmp/qerror.h"
46#include "qapi/qmp/qnull.h"
47#include "qemu/rcu.h"
48#include "block.h"
49#include "postcopy-ram.h"
50#include "qemu/thread.h"
51#include "trace.h"
52#include "exec/target_page.h"
53#include "io/channel-buffer.h"
54#include "io/channel-tls.h"
55#include "migration/colo.h"
56#include "hw/boards.h"
57#include "monitor/monitor.h"
58#include "net/announce.h"
59#include "qemu/queue.h"
60#include "multifd.h"
61#include "threadinfo.h"
62#include "qemu/yank.h"
63#include "sysemu/cpus.h"
64#include "yank_functions.h"
65#include "sysemu/qtest.h"
66#include "options.h"
67#include "sysemu/dirtylimit.h"
68
69static NotifierList migration_state_notifiers =
70 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
71
72
73enum mig_rp_message_type {
74 MIG_RP_MSG_INVALID = 0,
75 MIG_RP_MSG_SHUT,
76 MIG_RP_MSG_PONG,
77
78 MIG_RP_MSG_REQ_PAGES_ID,
79 MIG_RP_MSG_REQ_PAGES,
80 MIG_RP_MSG_RECV_BITMAP,
81 MIG_RP_MSG_RESUME_ACK,
82 MIG_RP_MSG_SWITCHOVER_ACK,
83
84 MIG_RP_MSG_MAX
85};
86
87
88
89
90
91static MigrationState *current_migration;
92static MigrationIncomingState *current_incoming;
93
94static GSList *migration_blockers;
95
96static bool migration_object_check(MigrationState *ms, Error **errp);
97static int migration_maybe_pause(MigrationState *s,
98 int *current_active_state,
99 int new_state);
100static void migrate_fd_cancel(MigrationState *s);
101
102static bool migration_needs_multiple_sockets(void)
103{
104 return migrate_multifd() || migrate_postcopy_preempt();
105}
106
107static bool uri_supports_multi_channels(const char *uri)
108{
109 return strstart(uri, "tcp:", NULL) || strstart(uri, "unix:", NULL) ||
110 strstart(uri, "vsock:", NULL);
111}
112
113static bool
114migration_channels_and_uri_compatible(const char *uri, Error **errp)
115{
116 if (migration_needs_multiple_sockets() &&
117 !uri_supports_multi_channels(uri)) {
118 error_setg(errp, "Migration requires multi-channel URIs (e.g. tcp)");
119 return false;
120 }
121
122 return true;
123}
124
125static gint page_request_addr_cmp(gconstpointer ap, gconstpointer bp)
126{
127 uintptr_t a = (uintptr_t) ap, b = (uintptr_t) bp;
128
129 return (a > b) - (a < b);
130}
131
132void migration_object_init(void)
133{
134
135 assert(!current_migration);
136 current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION));
137
138
139
140
141
142 assert(!current_incoming);
143 current_incoming = g_new0(MigrationIncomingState, 1);
144 current_incoming->state = MIGRATION_STATUS_NONE;
145 current_incoming->postcopy_remote_fds =
146 g_array_new(FALSE, TRUE, sizeof(struct PostCopyFD));
147 qemu_mutex_init(¤t_incoming->rp_mutex);
148 qemu_mutex_init(¤t_incoming->postcopy_prio_thread_mutex);
149 qemu_event_init(¤t_incoming->main_thread_load_event, false);
150 qemu_sem_init(¤t_incoming->postcopy_pause_sem_dst, 0);
151 qemu_sem_init(¤t_incoming->postcopy_pause_sem_fault, 0);
152 qemu_sem_init(¤t_incoming->postcopy_pause_sem_fast_load, 0);
153 qemu_sem_init(¤t_incoming->postcopy_qemufile_dst_done, 0);
154
155 qemu_mutex_init(¤t_incoming->page_request_mutex);
156 current_incoming->page_requested = g_tree_new(page_request_addr_cmp);
157
158 migration_object_check(current_migration, &error_fatal);
159
160 blk_mig_init();
161 ram_mig_init();
162 dirty_bitmap_mig_init();
163}
164
165void migration_cancel(const Error *error)
166{
167 if (error) {
168 migrate_set_error(current_migration, error);
169 }
170 if (migrate_dirty_limit()) {
171 qmp_cancel_vcpu_dirty_limit(false, -1, NULL);
172 }
173 migrate_fd_cancel(current_migration);
174}
175
176void migration_shutdown(void)
177{
178
179
180
181
182
183 colo_shutdown();
184
185
186
187
188 migration_cancel(NULL);
189 object_unref(OBJECT(current_migration));
190
191
192
193
194
195 dirty_bitmap_mig_cancel_outgoing();
196
197
198
199
200
201
202 dirty_bitmap_mig_cancel_incoming();
203}
204
205
206MigrationState *migrate_get_current(void)
207{
208
209 assert(current_migration);
210 return current_migration;
211}
212
213MigrationIncomingState *migration_incoming_get_current(void)
214{
215 assert(current_incoming);
216 return current_incoming;
217}
218
219void migration_incoming_transport_cleanup(MigrationIncomingState *mis)
220{
221 if (mis->socket_address_list) {
222 qapi_free_SocketAddressList(mis->socket_address_list);
223 mis->socket_address_list = NULL;
224 }
225
226 if (mis->transport_cleanup) {
227 mis->transport_cleanup(mis->transport_data);
228 mis->transport_data = mis->transport_cleanup = NULL;
229 }
230}
231
232void migration_incoming_state_destroy(void)
233{
234 struct MigrationIncomingState *mis = migration_incoming_get_current();
235
236 multifd_load_cleanup();
237 compress_threads_load_cleanup();
238
239 if (mis->to_src_file) {
240
241 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
242 qemu_fclose(mis->to_src_file);
243 mis->to_src_file = NULL;
244 }
245
246 if (mis->from_src_file) {
247 migration_ioc_unregister_yank_from_file(mis->from_src_file);
248 qemu_fclose(mis->from_src_file);
249 mis->from_src_file = NULL;
250 }
251 if (mis->postcopy_remote_fds) {
252 g_array_free(mis->postcopy_remote_fds, TRUE);
253 mis->postcopy_remote_fds = NULL;
254 }
255
256 migration_incoming_transport_cleanup(mis);
257 qemu_event_reset(&mis->main_thread_load_event);
258
259 if (mis->page_requested) {
260 g_tree_destroy(mis->page_requested);
261 mis->page_requested = NULL;
262 }
263
264 if (mis->postcopy_qemufile_dst) {
265 migration_ioc_unregister_yank_from_file(mis->postcopy_qemufile_dst);
266 qemu_fclose(mis->postcopy_qemufile_dst);
267 mis->postcopy_qemufile_dst = NULL;
268 }
269
270 yank_unregister_instance(MIGRATION_YANK_INSTANCE);
271}
272
273static void migrate_generate_event(int new_state)
274{
275 if (migrate_events()) {
276 qapi_event_send_migration(new_state);
277 }
278}
279
280
281
282
283
284static int migrate_send_rp_message(MigrationIncomingState *mis,
285 enum mig_rp_message_type message_type,
286 uint16_t len, void *data)
287{
288 int ret = 0;
289
290 trace_migrate_send_rp_message((int)message_type, len);
291 QEMU_LOCK_GUARD(&mis->rp_mutex);
292
293
294
295
296
297 if (!mis->to_src_file) {
298 ret = -EIO;
299 return ret;
300 }
301
302 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
303 qemu_put_be16(mis->to_src_file, len);
304 qemu_put_buffer(mis->to_src_file, data, len);
305 qemu_fflush(mis->to_src_file);
306
307
308 ret = qemu_file_get_error(mis->to_src_file);
309
310 return ret;
311}
312
313
314
315
316
317
318int migrate_send_rp_message_req_pages(MigrationIncomingState *mis,
319 RAMBlock *rb, ram_addr_t start)
320{
321 uint8_t bufc[12 + 1 + 255];
322 size_t msglen = 12;
323 size_t len = qemu_ram_pagesize(rb);
324 enum mig_rp_message_type msg_type;
325 const char *rbname;
326 int rbname_len;
327
328 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
329 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
330
331
332
333
334
335
336 if (rb != mis->last_rb) {
337 mis->last_rb = rb;
338
339 rbname = qemu_ram_get_idstr(rb);
340 rbname_len = strlen(rbname);
341
342 assert(rbname_len < 256);
343
344 bufc[msglen++] = rbname_len;
345 memcpy(bufc + msglen, rbname, rbname_len);
346 msglen += rbname_len;
347 msg_type = MIG_RP_MSG_REQ_PAGES_ID;
348 } else {
349 msg_type = MIG_RP_MSG_REQ_PAGES;
350 }
351
352 return migrate_send_rp_message(mis, msg_type, msglen, bufc);
353}
354
355int migrate_send_rp_req_pages(MigrationIncomingState *mis,
356 RAMBlock *rb, ram_addr_t start, uint64_t haddr)
357{
358 void *aligned = (void *)(uintptr_t)ROUND_DOWN(haddr, qemu_ram_pagesize(rb));
359 bool received = false;
360
361 WITH_QEMU_LOCK_GUARD(&mis->page_request_mutex) {
362 received = ramblock_recv_bitmap_test_byte_offset(rb, start);
363 if (!received && !g_tree_lookup(mis->page_requested, aligned)) {
364
365
366
367
368
369 g_tree_insert(mis->page_requested, aligned, (gpointer)1);
370 mis->page_requested_count++;
371 trace_postcopy_page_req_add(aligned, mis->page_requested_count);
372 }
373 }
374
375
376
377
378
379 if (received) {
380 return 0;
381 }
382
383 return migrate_send_rp_message_req_pages(mis, rb, start);
384}
385
386static bool migration_colo_enabled;
387bool migration_incoming_colo_enabled(void)
388{
389 return migration_colo_enabled;
390}
391
392void migration_incoming_disable_colo(void)
393{
394 ram_block_discard_disable(false);
395 migration_colo_enabled = false;
396}
397
398int migration_incoming_enable_colo(void)
399{
400#ifndef CONFIG_REPLICATION
401 error_report("ENABLE_COLO command come in migration stream, but COLO "
402 "module is not built in");
403 return -ENOTSUP;
404#endif
405
406 if (!migrate_colo()) {
407 error_report("ENABLE_COLO command come in migration stream, but c-colo "
408 "capability is not set");
409 return -EINVAL;
410 }
411
412 if (ram_block_discard_disable(true)) {
413 error_report("COLO: cannot disable RAM discard");
414 return -EBUSY;
415 }
416 migration_colo_enabled = true;
417 return 0;
418}
419
420void migrate_add_address(SocketAddress *address)
421{
422 MigrationIncomingState *mis = migration_incoming_get_current();
423
424 QAPI_LIST_PREPEND(mis->socket_address_list,
425 QAPI_CLONE(SocketAddress, address));
426}
427
428static void qemu_start_incoming_migration(const char *uri, Error **errp)
429{
430 const char *p = NULL;
431
432
433 if (!migration_channels_and_uri_compatible(uri, errp)) {
434 return;
435 }
436
437 qapi_event_send_migration(MIGRATION_STATUS_SETUP);
438 if (strstart(uri, "tcp:", &p) ||
439 strstart(uri, "unix:", NULL) ||
440 strstart(uri, "vsock:", NULL)) {
441 socket_start_incoming_migration(p ? p : uri, errp);
442#ifdef CONFIG_RDMA
443 } else if (strstart(uri, "rdma:", &p)) {
444 rdma_start_incoming_migration(p, errp);
445#endif
446 } else if (strstart(uri, "exec:", &p)) {
447 exec_start_incoming_migration(p, errp);
448 } else if (strstart(uri, "fd:", &p)) {
449 fd_start_incoming_migration(p, errp);
450 } else {
451 error_setg(errp, "unknown migration protocol: %s", uri);
452 }
453}
454
455static void process_incoming_migration_bh(void *opaque)
456{
457 Error *local_err = NULL;
458 MigrationIncomingState *mis = opaque;
459
460
461
462
463
464
465
466 if (!migrate_late_block_activate() ||
467 (autostart && (!global_state_received() ||
468 global_state_get_runstate() == RUN_STATE_RUNNING))) {
469
470
471 bdrv_activate_all(&local_err);
472 if (local_err) {
473 error_report_err(local_err);
474 local_err = NULL;
475 autostart = false;
476 }
477 }
478
479
480
481
482
483 qemu_announce_self(&mis->announce_timer, migrate_announce_params());
484
485 multifd_load_shutdown();
486
487 dirty_bitmap_mig_before_vm_start();
488
489 if (!global_state_received() ||
490 global_state_get_runstate() == RUN_STATE_RUNNING) {
491 if (autostart) {
492 vm_start();
493 } else {
494 runstate_set(RUN_STATE_PAUSED);
495 }
496 } else if (migration_incoming_colo_enabled()) {
497 migration_incoming_disable_colo();
498 vm_start();
499 } else {
500 runstate_set(global_state_get_runstate());
501 }
502
503
504
505
506
507 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
508 MIGRATION_STATUS_COMPLETED);
509 qemu_bh_delete(mis->bh);
510 migration_incoming_state_destroy();
511}
512
513static void coroutine_fn
514process_incoming_migration_co(void *opaque)
515{
516 MigrationIncomingState *mis = migration_incoming_get_current();
517 PostcopyState ps;
518 int ret;
519
520 assert(mis->from_src_file);
521
522 if (compress_threads_load_setup(mis->from_src_file)) {
523 error_report("Failed to setup decompress threads");
524 goto fail;
525 }
526
527 mis->largest_page_size = qemu_ram_pagesize_largest();
528 postcopy_state_set(POSTCOPY_INCOMING_NONE);
529 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
530 MIGRATION_STATUS_ACTIVE);
531
532 mis->loadvm_co = qemu_coroutine_self();
533 ret = qemu_loadvm_state(mis->from_src_file);
534 mis->loadvm_co = NULL;
535
536 ps = postcopy_state_get();
537 trace_process_incoming_migration_co_end(ret, ps);
538 if (ps != POSTCOPY_INCOMING_NONE) {
539 if (ps == POSTCOPY_INCOMING_ADVISE) {
540
541
542
543
544
545 postcopy_ram_incoming_cleanup(mis);
546 } else if (ret >= 0) {
547
548
549
550
551 trace_process_incoming_migration_co_postcopy_end_main();
552 return;
553 }
554
555 }
556
557 if (ret < 0) {
558 error_report("load of migration failed: %s", strerror(-ret));
559 goto fail;
560 }
561
562 if (colo_incoming_co() < 0) {
563 goto fail;
564 }
565
566 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
567 qemu_bh_schedule(mis->bh);
568 return;
569fail:
570 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
571 MIGRATION_STATUS_FAILED);
572 qemu_fclose(mis->from_src_file);
573
574 multifd_load_cleanup();
575 compress_threads_load_cleanup();
576
577 exit(EXIT_FAILURE);
578}
579
580
581
582
583
584
585
586
587static bool migration_incoming_setup(QEMUFile *f, Error **errp)
588{
589 MigrationIncomingState *mis = migration_incoming_get_current();
590
591 if (!mis->from_src_file) {
592 mis->from_src_file = f;
593 }
594 qemu_file_set_blocking(f, false);
595 return true;
596}
597
598void migration_incoming_process(void)
599{
600 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL);
601 qemu_coroutine_enter(co);
602}
603
604
605static bool postcopy_try_recover(void)
606{
607 MigrationIncomingState *mis = migration_incoming_get_current();
608
609 if (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
610
611
612
613 assert(mis->from_src_file);
614
615 qemu_file_set_blocking(mis->from_src_file, true);
616
617
618 mis->to_src_file = qemu_file_get_return_path(mis->from_src_file);
619
620 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
621 MIGRATION_STATUS_POSTCOPY_RECOVER);
622
623
624
625
626
627
628
629
630 qemu_sem_post(&mis->postcopy_pause_sem_dst);
631 return true;
632 }
633
634 return false;
635}
636
637void migration_fd_process_incoming(QEMUFile *f, Error **errp)
638{
639 if (!migration_incoming_setup(f, errp)) {
640 return;
641 }
642 if (postcopy_try_recover()) {
643 return;
644 }
645 migration_incoming_process();
646}
647
648
649
650
651
652static bool migration_should_start_incoming(bool main_channel)
653{
654
655 if (migrate_multifd()) {
656 return migration_has_all_channels();
657 }
658
659
660 if (migrate_postcopy_preempt()) {
661 return main_channel;
662 }
663
664
665
666
667
668
669 assert(main_channel);
670 return true;
671}
672
673void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp)
674{
675 MigrationIncomingState *mis = migration_incoming_get_current();
676 Error *local_err = NULL;
677 QEMUFile *f;
678 bool default_channel = true;
679 uint32_t channel_magic = 0;
680 int ret = 0;
681
682 if (migrate_multifd() && !migrate_postcopy_ram() &&
683 qio_channel_has_feature(ioc, QIO_CHANNEL_FEATURE_READ_MSG_PEEK)) {
684
685
686
687
688
689
690
691
692
693
694 ret = migration_channel_read_peek(ioc, (void *)&channel_magic,
695 sizeof(channel_magic), &local_err);
696
697 if (ret != 0) {
698 error_propagate(errp, local_err);
699 return;
700 }
701
702 default_channel = (channel_magic == cpu_to_be32(QEMU_VM_FILE_MAGIC));
703 } else {
704 default_channel = !mis->from_src_file;
705 }
706
707 if (multifd_load_setup(errp) != 0) {
708 error_setg(errp, "Failed to setup multifd channels");
709 return;
710 }
711
712 if (default_channel) {
713 f = qemu_file_new_input(ioc);
714
715 if (!migration_incoming_setup(f, errp)) {
716 return;
717 }
718 } else {
719
720 assert(migration_needs_multiple_sockets());
721 if (migrate_multifd()) {
722 multifd_recv_new_channel(ioc, &local_err);
723 } else {
724 assert(migrate_postcopy_preempt());
725 f = qemu_file_new_input(ioc);
726 postcopy_preempt_new_channel(mis, f);
727 }
728 if (local_err) {
729 error_propagate(errp, local_err);
730 return;
731 }
732 }
733
734 if (migration_should_start_incoming(default_channel)) {
735
736 if (postcopy_try_recover()) {
737 return;
738 }
739 migration_incoming_process();
740 }
741}
742
743
744
745
746
747
748
749bool migration_has_all_channels(void)
750{
751 MigrationIncomingState *mis = migration_incoming_get_current();
752
753 if (!mis->from_src_file) {
754 return false;
755 }
756
757 if (migrate_multifd()) {
758 return multifd_recv_all_channels_created();
759 }
760
761 if (migrate_postcopy_preempt()) {
762 return mis->postcopy_qemufile_dst != NULL;
763 }
764
765 return true;
766}
767
768int migrate_send_rp_switchover_ack(MigrationIncomingState *mis)
769{
770 return migrate_send_rp_message(mis, MIG_RP_MSG_SWITCHOVER_ACK, 0, NULL);
771}
772
773
774
775
776
777
778void migrate_send_rp_shut(MigrationIncomingState *mis,
779 uint32_t value)
780{
781 uint32_t buf;
782
783 buf = cpu_to_be32(value);
784 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
785}
786
787
788
789
790
791void migrate_send_rp_pong(MigrationIncomingState *mis,
792 uint32_t value)
793{
794 uint32_t buf;
795
796 buf = cpu_to_be32(value);
797 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
798}
799
800void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
801 char *block_name)
802{
803 char buf[512];
804 int len;
805 int64_t res;
806
807
808
809
810
811 len = strlen(block_name);
812 buf[0] = len;
813 memcpy(buf + 1, block_name, len);
814
815 if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
816 error_report("%s: MSG_RP_RECV_BITMAP only used for recovery",
817 __func__);
818 return;
819 }
820
821 migrate_send_rp_message(mis, MIG_RP_MSG_RECV_BITMAP, len + 1, buf);
822
823
824
825
826
827
828
829
830
831
832 qemu_mutex_lock(&mis->rp_mutex);
833 res = ramblock_recv_bitmap_send(mis->to_src_file, block_name);
834 qemu_mutex_unlock(&mis->rp_mutex);
835
836 trace_migrate_send_rp_recv_bitmap(block_name, res);
837}
838
839void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value)
840{
841 uint32_t buf;
842
843 buf = cpu_to_be32(value);
844 migrate_send_rp_message(mis, MIG_RP_MSG_RESUME_ACK, sizeof(buf), &buf);
845}
846
847
848
849
850
851bool migration_is_setup_or_active(int state)
852{
853 switch (state) {
854 case MIGRATION_STATUS_ACTIVE:
855 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
856 case MIGRATION_STATUS_POSTCOPY_PAUSED:
857 case MIGRATION_STATUS_POSTCOPY_RECOVER:
858 case MIGRATION_STATUS_SETUP:
859 case MIGRATION_STATUS_PRE_SWITCHOVER:
860 case MIGRATION_STATUS_DEVICE:
861 case MIGRATION_STATUS_WAIT_UNPLUG:
862 case MIGRATION_STATUS_COLO:
863 return true;
864
865 default:
866 return false;
867
868 }
869}
870
871bool migration_is_running(int state)
872{
873 switch (state) {
874 case MIGRATION_STATUS_ACTIVE:
875 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
876 case MIGRATION_STATUS_POSTCOPY_PAUSED:
877 case MIGRATION_STATUS_POSTCOPY_RECOVER:
878 case MIGRATION_STATUS_SETUP:
879 case MIGRATION_STATUS_PRE_SWITCHOVER:
880 case MIGRATION_STATUS_DEVICE:
881 case MIGRATION_STATUS_WAIT_UNPLUG:
882 case MIGRATION_STATUS_CANCELLING:
883 return true;
884
885 default:
886 return false;
887
888 }
889}
890
891static bool migrate_show_downtime(MigrationState *s)
892{
893 return (s->state == MIGRATION_STATUS_COMPLETED) || migration_in_postcopy();
894}
895
896static void populate_time_info(MigrationInfo *info, MigrationState *s)
897{
898 info->has_status = true;
899 info->has_setup_time = true;
900 info->setup_time = s->setup_time;
901
902 if (s->state == MIGRATION_STATUS_COMPLETED) {
903 info->has_total_time = true;
904 info->total_time = s->total_time;
905 } else {
906 info->has_total_time = true;
907 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
908 s->start_time;
909 }
910
911 if (migrate_show_downtime(s)) {
912 info->has_downtime = true;
913 info->downtime = s->downtime;
914 } else {
915 info->has_expected_downtime = true;
916 info->expected_downtime = s->expected_downtime;
917 }
918}
919
920static void populate_ram_info(MigrationInfo *info, MigrationState *s)
921{
922 size_t page_size = qemu_target_page_size();
923
924 info->ram = g_malloc0(sizeof(*info->ram));
925 info->ram->transferred = stat64_get(&mig_stats.transferred);
926 info->ram->total = ram_bytes_total();
927 info->ram->duplicate = stat64_get(&mig_stats.zero_pages);
928
929 info->ram->skipped = 0;
930 info->ram->normal = stat64_get(&mig_stats.normal_pages);
931 info->ram->normal_bytes = info->ram->normal * page_size;
932 info->ram->mbps = s->mbps;
933 info->ram->dirty_sync_count =
934 stat64_get(&mig_stats.dirty_sync_count);
935 info->ram->dirty_sync_missed_zero_copy =
936 stat64_get(&mig_stats.dirty_sync_missed_zero_copy);
937 info->ram->postcopy_requests =
938 stat64_get(&mig_stats.postcopy_requests);
939 info->ram->page_size = page_size;
940 info->ram->multifd_bytes = stat64_get(&mig_stats.multifd_bytes);
941 info->ram->pages_per_second = s->pages_per_second;
942 info->ram->precopy_bytes = stat64_get(&mig_stats.precopy_bytes);
943 info->ram->downtime_bytes = stat64_get(&mig_stats.downtime_bytes);
944 info->ram->postcopy_bytes = stat64_get(&mig_stats.postcopy_bytes);
945
946 if (migrate_xbzrle()) {
947 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
948 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
949 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
950 info->xbzrle_cache->pages = xbzrle_counters.pages;
951 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
952 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
953 info->xbzrle_cache->encoding_rate = xbzrle_counters.encoding_rate;
954 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
955 }
956
957 if (migrate_compress()) {
958 info->compression = g_malloc0(sizeof(*info->compression));
959 info->compression->pages = compression_counters.pages;
960 info->compression->busy = compression_counters.busy;
961 info->compression->busy_rate = compression_counters.busy_rate;
962 info->compression->compressed_size =
963 compression_counters.compressed_size;
964 info->compression->compression_rate =
965 compression_counters.compression_rate;
966 }
967
968 if (cpu_throttle_active()) {
969 info->has_cpu_throttle_percentage = true;
970 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
971 }
972
973 if (s->state != MIGRATION_STATUS_COMPLETED) {
974 info->ram->remaining = ram_bytes_remaining();
975 info->ram->dirty_pages_rate =
976 stat64_get(&mig_stats.dirty_pages_rate);
977 }
978
979 if (migrate_dirty_limit() && dirtylimit_in_service()) {
980 info->has_dirty_limit_throttle_time_per_round = true;
981 info->dirty_limit_throttle_time_per_round =
982 dirtylimit_throttle_time_per_round();
983
984 info->has_dirty_limit_ring_full_time = true;
985 info->dirty_limit_ring_full_time = dirtylimit_ring_full_time();
986 }
987}
988
989static void populate_disk_info(MigrationInfo *info)
990{
991 if (blk_mig_active()) {
992 info->disk = g_malloc0(sizeof(*info->disk));
993 info->disk->transferred = blk_mig_bytes_transferred();
994 info->disk->remaining = blk_mig_bytes_remaining();
995 info->disk->total = blk_mig_bytes_total();
996 }
997}
998
999static void fill_source_migration_info(MigrationInfo *info)
1000{
1001 MigrationState *s = migrate_get_current();
1002 int state = qatomic_read(&s->state);
1003 GSList *cur_blocker = migration_blockers;
1004
1005 info->blocked_reasons = NULL;
1006
1007
1008
1009
1010
1011
1012
1013 qemu_savevm_non_migratable_list(&info->blocked_reasons);
1014
1015 while (cur_blocker) {
1016 QAPI_LIST_PREPEND(info->blocked_reasons,
1017 g_strdup(error_get_pretty(cur_blocker->data)));
1018 cur_blocker = g_slist_next(cur_blocker);
1019 }
1020 info->has_blocked_reasons = info->blocked_reasons != NULL;
1021
1022 switch (state) {
1023 case MIGRATION_STATUS_NONE:
1024
1025
1026 return;
1027 case MIGRATION_STATUS_SETUP:
1028 info->has_status = true;
1029 info->has_total_time = false;
1030 break;
1031 case MIGRATION_STATUS_ACTIVE:
1032 case MIGRATION_STATUS_CANCELLING:
1033 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1034 case MIGRATION_STATUS_PRE_SWITCHOVER:
1035 case MIGRATION_STATUS_DEVICE:
1036 case MIGRATION_STATUS_POSTCOPY_PAUSED:
1037 case MIGRATION_STATUS_POSTCOPY_RECOVER:
1038
1039 populate_time_info(info, s);
1040 populate_ram_info(info, s);
1041 populate_disk_info(info);
1042 populate_vfio_info(info);
1043 break;
1044 case MIGRATION_STATUS_COLO:
1045 info->has_status = true;
1046
1047 break;
1048 case MIGRATION_STATUS_COMPLETED:
1049 populate_time_info(info, s);
1050 populate_ram_info(info, s);
1051 populate_vfio_info(info);
1052 break;
1053 case MIGRATION_STATUS_FAILED:
1054 info->has_status = true;
1055 if (s->error) {
1056 info->error_desc = g_strdup(error_get_pretty(s->error));
1057 }
1058 break;
1059 case MIGRATION_STATUS_CANCELLED:
1060 info->has_status = true;
1061 break;
1062 case MIGRATION_STATUS_WAIT_UNPLUG:
1063 info->has_status = true;
1064 break;
1065 }
1066 info->status = state;
1067}
1068
1069static void fill_destination_migration_info(MigrationInfo *info)
1070{
1071 MigrationIncomingState *mis = migration_incoming_get_current();
1072
1073 if (mis->socket_address_list) {
1074 info->has_socket_address = true;
1075 info->socket_address =
1076 QAPI_CLONE(SocketAddressList, mis->socket_address_list);
1077 }
1078
1079 switch (mis->state) {
1080 case MIGRATION_STATUS_NONE:
1081 return;
1082 case MIGRATION_STATUS_SETUP:
1083 case MIGRATION_STATUS_CANCELLING:
1084 case MIGRATION_STATUS_CANCELLED:
1085 case MIGRATION_STATUS_ACTIVE:
1086 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1087 case MIGRATION_STATUS_POSTCOPY_PAUSED:
1088 case MIGRATION_STATUS_POSTCOPY_RECOVER:
1089 case MIGRATION_STATUS_FAILED:
1090 case MIGRATION_STATUS_COLO:
1091 info->has_status = true;
1092 break;
1093 case MIGRATION_STATUS_COMPLETED:
1094 info->has_status = true;
1095 fill_destination_postcopy_migration_info(info);
1096 break;
1097 }
1098 info->status = mis->state;
1099}
1100
1101MigrationInfo *qmp_query_migrate(Error **errp)
1102{
1103 MigrationInfo *info = g_malloc0(sizeof(*info));
1104
1105 fill_destination_migration_info(info);
1106 fill_source_migration_info(info);
1107
1108 return info;
1109}
1110
1111void qmp_migrate_start_postcopy(Error **errp)
1112{
1113 MigrationState *s = migrate_get_current();
1114
1115 if (!migrate_postcopy()) {
1116 error_setg(errp, "Enable postcopy with migrate_set_capability before"
1117 " the start of migration");
1118 return;
1119 }
1120
1121 if (s->state == MIGRATION_STATUS_NONE) {
1122 error_setg(errp, "Postcopy must be started after migration has been"
1123 " started");
1124 return;
1125 }
1126
1127
1128
1129
1130 qatomic_set(&s->start_postcopy, true);
1131}
1132
1133
1134
1135void migrate_set_state(int *state, int old_state, int new_state)
1136{
1137 assert(new_state < MIGRATION_STATUS__MAX);
1138 if (qatomic_cmpxchg(state, old_state, new_state) == old_state) {
1139 trace_migrate_set_state(MigrationStatus_str(new_state));
1140 migrate_generate_event(new_state);
1141 }
1142}
1143
1144static void migrate_fd_cleanup(MigrationState *s)
1145{
1146 qemu_bh_delete(s->cleanup_bh);
1147 s->cleanup_bh = NULL;
1148
1149 g_free(s->hostname);
1150 s->hostname = NULL;
1151 json_writer_free(s->vmdesc);
1152 s->vmdesc = NULL;
1153
1154 qemu_savevm_state_cleanup();
1155
1156 if (s->to_dst_file) {
1157 QEMUFile *tmp;
1158
1159 trace_migrate_fd_cleanup();
1160 qemu_mutex_unlock_iothread();
1161 if (s->migration_thread_running) {
1162 qemu_thread_join(&s->thread);
1163 s->migration_thread_running = false;
1164 }
1165 qemu_mutex_lock_iothread();
1166
1167 multifd_save_cleanup();
1168 qemu_mutex_lock(&s->qemu_file_lock);
1169 tmp = s->to_dst_file;
1170 s->to_dst_file = NULL;
1171 qemu_mutex_unlock(&s->qemu_file_lock);
1172
1173
1174
1175
1176 migration_ioc_unregister_yank_from_file(tmp);
1177 qemu_fclose(tmp);
1178 }
1179
1180 if (s->postcopy_qemufile_src) {
1181 migration_ioc_unregister_yank_from_file(s->postcopy_qemufile_src);
1182 qemu_fclose(s->postcopy_qemufile_src);
1183 s->postcopy_qemufile_src = NULL;
1184 }
1185
1186 assert(!migration_is_active(s));
1187
1188 if (s->state == MIGRATION_STATUS_CANCELLING) {
1189 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
1190 MIGRATION_STATUS_CANCELLED);
1191 }
1192
1193 if (s->error) {
1194
1195 error_report_err(error_copy(s->error));
1196 }
1197 notifier_list_notify(&migration_state_notifiers, s);
1198 block_cleanup_parameters();
1199 yank_unregister_instance(MIGRATION_YANK_INSTANCE);
1200}
1201
1202static void migrate_fd_cleanup_schedule(MigrationState *s)
1203{
1204
1205
1206
1207
1208 object_ref(OBJECT(s));
1209 qemu_bh_schedule(s->cleanup_bh);
1210}
1211
1212static void migrate_fd_cleanup_bh(void *opaque)
1213{
1214 MigrationState *s = opaque;
1215 migrate_fd_cleanup(s);
1216 object_unref(OBJECT(s));
1217}
1218
1219void migrate_set_error(MigrationState *s, const Error *error)
1220{
1221 QEMU_LOCK_GUARD(&s->error_mutex);
1222 if (!s->error) {
1223 s->error = error_copy(error);
1224 }
1225}
1226
1227static void migrate_error_free(MigrationState *s)
1228{
1229 QEMU_LOCK_GUARD(&s->error_mutex);
1230 if (s->error) {
1231 error_free(s->error);
1232 s->error = NULL;
1233 }
1234}
1235
1236static void migrate_fd_error(MigrationState *s, const Error *error)
1237{
1238 trace_migrate_fd_error(error_get_pretty(error));
1239 assert(s->to_dst_file == NULL);
1240 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1241 MIGRATION_STATUS_FAILED);
1242 migrate_set_error(s, error);
1243}
1244
1245static void migrate_fd_cancel(MigrationState *s)
1246{
1247 int old_state ;
1248 QEMUFile *f = migrate_get_current()->to_dst_file;
1249 trace_migrate_fd_cancel();
1250
1251 WITH_QEMU_LOCK_GUARD(&s->qemu_file_lock) {
1252 if (s->rp_state.from_dst_file) {
1253
1254 qemu_file_shutdown(s->rp_state.from_dst_file);
1255 }
1256 }
1257
1258 do {
1259 old_state = s->state;
1260 if (!migration_is_running(old_state)) {
1261 break;
1262 }
1263
1264 if (old_state == MIGRATION_STATUS_PRE_SWITCHOVER) {
1265 qemu_sem_post(&s->pause_sem);
1266 }
1267 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
1268 } while (s->state != MIGRATION_STATUS_CANCELLING);
1269
1270
1271
1272
1273
1274
1275
1276
1277 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
1278 qemu_file_shutdown(f);
1279 }
1280 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
1281 Error *local_err = NULL;
1282
1283 bdrv_activate_all(&local_err);
1284 if (local_err) {
1285 error_report_err(local_err);
1286 } else {
1287 s->block_inactive = false;
1288 }
1289 }
1290}
1291
1292void add_migration_state_change_notifier(Notifier *notify)
1293{
1294 notifier_list_add(&migration_state_notifiers, notify);
1295}
1296
1297void remove_migration_state_change_notifier(Notifier *notify)
1298{
1299 notifier_remove(notify);
1300}
1301
1302bool migration_in_setup(MigrationState *s)
1303{
1304 return s->state == MIGRATION_STATUS_SETUP;
1305}
1306
1307bool migration_has_finished(MigrationState *s)
1308{
1309 return s->state == MIGRATION_STATUS_COMPLETED;
1310}
1311
1312bool migration_has_failed(MigrationState *s)
1313{
1314 return (s->state == MIGRATION_STATUS_CANCELLED ||
1315 s->state == MIGRATION_STATUS_FAILED);
1316}
1317
1318bool migration_in_postcopy(void)
1319{
1320 MigrationState *s = migrate_get_current();
1321
1322 switch (s->state) {
1323 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1324 case MIGRATION_STATUS_POSTCOPY_PAUSED:
1325 case MIGRATION_STATUS_POSTCOPY_RECOVER:
1326 return true;
1327 default:
1328 return false;
1329 }
1330}
1331
1332bool migration_in_postcopy_after_devices(MigrationState *s)
1333{
1334 return migration_in_postcopy() && s->postcopy_after_devices;
1335}
1336
1337bool migration_in_incoming_postcopy(void)
1338{
1339 PostcopyState ps = postcopy_state_get();
1340
1341 return ps >= POSTCOPY_INCOMING_DISCARD && ps < POSTCOPY_INCOMING_END;
1342}
1343
1344bool migration_incoming_postcopy_advised(void)
1345{
1346 PostcopyState ps = postcopy_state_get();
1347
1348 return ps >= POSTCOPY_INCOMING_ADVISE && ps < POSTCOPY_INCOMING_END;
1349}
1350
1351bool migration_in_bg_snapshot(void)
1352{
1353 MigrationState *s = migrate_get_current();
1354
1355 return migrate_background_snapshot() &&
1356 migration_is_setup_or_active(s->state);
1357}
1358
1359bool migration_is_idle(void)
1360{
1361 MigrationState *s = current_migration;
1362
1363 if (!s) {
1364 return true;
1365 }
1366
1367 switch (s->state) {
1368 case MIGRATION_STATUS_NONE:
1369 case MIGRATION_STATUS_CANCELLED:
1370 case MIGRATION_STATUS_COMPLETED:
1371 case MIGRATION_STATUS_FAILED:
1372 return true;
1373 case MIGRATION_STATUS_SETUP:
1374 case MIGRATION_STATUS_CANCELLING:
1375 case MIGRATION_STATUS_ACTIVE:
1376 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1377 case MIGRATION_STATUS_COLO:
1378 case MIGRATION_STATUS_PRE_SWITCHOVER:
1379 case MIGRATION_STATUS_DEVICE:
1380 case MIGRATION_STATUS_WAIT_UNPLUG:
1381 return false;
1382 case MIGRATION_STATUS__MAX:
1383 g_assert_not_reached();
1384 }
1385
1386 return false;
1387}
1388
1389bool migration_is_active(MigrationState *s)
1390{
1391 return (s->state == MIGRATION_STATUS_ACTIVE ||
1392 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1393}
1394
1395void migrate_init(MigrationState *s)
1396{
1397
1398
1399
1400
1401
1402 s->cleanup_bh = 0;
1403 s->vm_start_bh = 0;
1404 s->to_dst_file = NULL;
1405 s->state = MIGRATION_STATUS_NONE;
1406 s->rp_state.from_dst_file = NULL;
1407 s->rp_state.error = false;
1408 s->mbps = 0.0;
1409 s->pages_per_second = 0.0;
1410 s->downtime = 0;
1411 s->expected_downtime = 0;
1412 s->setup_time = 0;
1413 s->start_postcopy = false;
1414 s->postcopy_after_devices = false;
1415 s->migration_thread_running = false;
1416 error_free(s->error);
1417 s->error = NULL;
1418 s->hostname = NULL;
1419
1420 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
1421
1422 s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1423 s->total_time = 0;
1424 s->vm_old_state = -1;
1425 s->iteration_initial_bytes = 0;
1426 s->threshold_size = 0;
1427 s->switchover_acked = false;
1428}
1429
1430int migrate_add_blocker_internal(Error *reason, Error **errp)
1431{
1432
1433 if (runstate_check(RUN_STATE_SAVE_VM) || !migration_is_idle()) {
1434 error_propagate_prepend(errp, error_copy(reason),
1435 "disallowing migration blocker "
1436 "(migration/snapshot in progress) for: ");
1437 return -EBUSY;
1438 }
1439
1440 migration_blockers = g_slist_prepend(migration_blockers, reason);
1441 return 0;
1442}
1443
1444int migrate_add_blocker(Error *reason, Error **errp)
1445{
1446 if (only_migratable) {
1447 error_propagate_prepend(errp, error_copy(reason),
1448 "disallowing migration blocker "
1449 "(--only-migratable) for: ");
1450 return -EACCES;
1451 }
1452
1453 return migrate_add_blocker_internal(reason, errp);
1454}
1455
1456void migrate_del_blocker(Error *reason)
1457{
1458 migration_blockers = g_slist_remove(migration_blockers, reason);
1459}
1460
1461void qmp_migrate_incoming(const char *uri, Error **errp)
1462{
1463 Error *local_err = NULL;
1464 static bool once = true;
1465
1466 if (!once) {
1467 error_setg(errp, "The incoming migration has already been started");
1468 return;
1469 }
1470 if (!runstate_check(RUN_STATE_INMIGRATE)) {
1471 error_setg(errp, "'-incoming' was not specified on the command line");
1472 return;
1473 }
1474
1475 if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) {
1476 return;
1477 }
1478
1479 qemu_start_incoming_migration(uri, &local_err);
1480
1481 if (local_err) {
1482 yank_unregister_instance(MIGRATION_YANK_INSTANCE);
1483 error_propagate(errp, local_err);
1484 return;
1485 }
1486
1487 once = false;
1488}
1489
1490void qmp_migrate_recover(const char *uri, Error **errp)
1491{
1492 MigrationIncomingState *mis = migration_incoming_get_current();
1493
1494
1495
1496
1497
1498
1499 assert(errp);
1500
1501 if (mis->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
1502 error_setg(errp, "Migrate recover can only be run "
1503 "when postcopy is paused.");
1504 return;
1505 }
1506
1507
1508 migration_incoming_transport_cleanup(mis);
1509
1510
1511
1512
1513
1514
1515 qemu_start_incoming_migration(uri, errp);
1516}
1517
1518void qmp_migrate_pause(Error **errp)
1519{
1520 MigrationState *ms = migrate_get_current();
1521 MigrationIncomingState *mis = migration_incoming_get_current();
1522 int ret;
1523
1524 if (ms->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1525
1526 qemu_mutex_lock(&ms->qemu_file_lock);
1527 ret = qemu_file_shutdown(ms->to_dst_file);
1528 qemu_mutex_unlock(&ms->qemu_file_lock);
1529 if (ret) {
1530 error_setg(errp, "Failed to pause source migration");
1531 }
1532 return;
1533 }
1534
1535 if (mis->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1536 ret = qemu_file_shutdown(mis->from_src_file);
1537 if (ret) {
1538 error_setg(errp, "Failed to pause destination migration");
1539 }
1540 return;
1541 }
1542
1543 error_setg(errp, "migrate-pause is currently only supported "
1544 "during postcopy-active state");
1545}
1546
1547bool migration_is_blocked(Error **errp)
1548{
1549 if (qemu_savevm_state_blocked(errp)) {
1550 return true;
1551 }
1552
1553 if (migration_blockers) {
1554 error_propagate(errp, error_copy(migration_blockers->data));
1555 return true;
1556 }
1557
1558 return false;
1559}
1560
1561
1562static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc,
1563 bool resume, Error **errp)
1564{
1565 Error *local_err = NULL;
1566
1567 if (resume) {
1568 if (s->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
1569 error_setg(errp, "Cannot resume if there is no "
1570 "paused migration");
1571 return false;
1572 }
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586 if (migrate_release_ram()) {
1587 error_setg(errp, "Postcopy recovery cannot work "
1588 "when release-ram capability is set");
1589 return false;
1590 }
1591
1592
1593 return true;
1594 }
1595
1596 if (migration_is_running(s->state)) {
1597 error_setg(errp, QERR_MIGRATION_ACTIVE);
1598 return false;
1599 }
1600
1601 if (runstate_check(RUN_STATE_INMIGRATE)) {
1602 error_setg(errp, "Guest is waiting for an incoming migration");
1603 return false;
1604 }
1605
1606 if (runstate_check(RUN_STATE_POSTMIGRATE)) {
1607 error_setg(errp, "Can't migrate the vm that was paused due to "
1608 "previous migration");
1609 return false;
1610 }
1611
1612 if (migration_is_blocked(errp)) {
1613 return false;
1614 }
1615
1616 if (blk || blk_inc) {
1617 if (migrate_colo()) {
1618 error_setg(errp, "No disk migration is required in COLO mode");
1619 return false;
1620 }
1621 if (migrate_block() || migrate_block_incremental()) {
1622 error_setg(errp, "Command options are incompatible with "
1623 "current migration capabilities");
1624 return false;
1625 }
1626 if (!migrate_cap_set(MIGRATION_CAPABILITY_BLOCK, true, &local_err)) {
1627 error_propagate(errp, local_err);
1628 return false;
1629 }
1630 s->must_remove_block_options = true;
1631 }
1632
1633 if (blk_inc) {
1634 migrate_set_block_incremental(true);
1635 }
1636
1637 migrate_init(s);
1638
1639
1640
1641
1642 memset(&mig_stats, 0, sizeof(mig_stats));
1643 memset(&compression_counters, 0, sizeof(compression_counters));
1644 reset_vfio_bytes_transferred();
1645
1646 return true;
1647}
1648
1649void qmp_migrate(const char *uri, bool has_blk, bool blk,
1650 bool has_inc, bool inc, bool has_detach, bool detach,
1651 bool has_resume, bool resume, Error **errp)
1652{
1653 bool resume_requested;
1654 Error *local_err = NULL;
1655 MigrationState *s = migrate_get_current();
1656 const char *p = NULL;
1657
1658
1659 if (!migration_channels_and_uri_compatible(uri, errp)) {
1660 return;
1661 }
1662
1663 resume_requested = has_resume && resume;
1664 if (!migrate_prepare(s, has_blk && blk, has_inc && inc,
1665 resume_requested, errp)) {
1666
1667 return;
1668 }
1669
1670 if (!resume_requested) {
1671 if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) {
1672 return;
1673 }
1674 }
1675
1676 if (strstart(uri, "tcp:", &p) ||
1677 strstart(uri, "unix:", NULL) ||
1678 strstart(uri, "vsock:", NULL)) {
1679 socket_start_outgoing_migration(s, p ? p : uri, &local_err);
1680#ifdef CONFIG_RDMA
1681 } else if (strstart(uri, "rdma:", &p)) {
1682 rdma_start_outgoing_migration(s, p, &local_err);
1683#endif
1684 } else if (strstart(uri, "exec:", &p)) {
1685 exec_start_outgoing_migration(s, p, &local_err);
1686 } else if (strstart(uri, "fd:", &p)) {
1687 fd_start_outgoing_migration(s, p, &local_err);
1688 } else {
1689 if (!resume_requested) {
1690 yank_unregister_instance(MIGRATION_YANK_INSTANCE);
1691 }
1692 error_setg(&local_err, QERR_INVALID_PARAMETER_VALUE, "uri",
1693 "a valid migration protocol");
1694 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1695 MIGRATION_STATUS_FAILED);
1696 block_cleanup_parameters();
1697 return;
1698 }
1699
1700 if (local_err) {
1701 if (!resume_requested) {
1702 yank_unregister_instance(MIGRATION_YANK_INSTANCE);
1703 }
1704 migrate_fd_error(s, local_err);
1705 error_propagate(errp, local_err);
1706 return;
1707 }
1708}
1709
1710void qmp_migrate_cancel(Error **errp)
1711{
1712 migration_cancel(NULL);
1713}
1714
1715void qmp_migrate_continue(MigrationStatus state, Error **errp)
1716{
1717 MigrationState *s = migrate_get_current();
1718 if (s->state != state) {
1719 error_setg(errp, "Migration not in expected state: %s",
1720 MigrationStatus_str(s->state));
1721 return;
1722 }
1723 qemu_sem_post(&s->pause_sem);
1724}
1725
1726
1727
1728
1729
1730
1731static void mark_source_rp_bad(MigrationState *s)
1732{
1733 s->rp_state.error = true;
1734}
1735
1736static struct rp_cmd_args {
1737 ssize_t len;
1738 const char *name;
1739} rp_cmd_args[] = {
1740 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1741 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1742 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
1743 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1744 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
1745 [MIG_RP_MSG_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" },
1746 [MIG_RP_MSG_RESUME_ACK] = { .len = 4, .name = "RESUME_ACK" },
1747 [MIG_RP_MSG_SWITCHOVER_ACK] = { .len = 0, .name = "SWITCHOVER_ACK" },
1748 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1749};
1750
1751
1752
1753
1754
1755
1756static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1757 ram_addr_t start, size_t len)
1758{
1759 long our_host_ps = qemu_real_host_page_size();
1760
1761 trace_migrate_handle_rp_req_pages(rbname, start, len);
1762
1763
1764
1765
1766
1767 if (!QEMU_IS_ALIGNED(start, our_host_ps) ||
1768 !QEMU_IS_ALIGNED(len, our_host_ps)) {
1769 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1770 " len: %zd", __func__, start, len);
1771 mark_source_rp_bad(ms);
1772 return;
1773 }
1774
1775 if (ram_save_queue_pages(rbname, start, len)) {
1776 mark_source_rp_bad(ms);
1777 }
1778}
1779
1780
1781static bool postcopy_pause_return_path_thread(MigrationState *s)
1782{
1783 trace_postcopy_pause_return_path();
1784
1785 qemu_sem_wait(&s->postcopy_pause_rp_sem);
1786
1787 trace_postcopy_pause_return_path_continued();
1788
1789 return true;
1790}
1791
1792static int migrate_handle_rp_recv_bitmap(MigrationState *s, char *block_name)
1793{
1794 RAMBlock *block = qemu_ram_block_by_name(block_name);
1795
1796 if (!block) {
1797 error_report("%s: invalid block name '%s'", __func__, block_name);
1798 return -EINVAL;
1799 }
1800
1801
1802 return ram_dirty_bitmap_reload(s, block);
1803}
1804
1805static int migrate_handle_rp_resume_ack(MigrationState *s, uint32_t value)
1806{
1807 trace_source_return_path_thread_resume_ack(value);
1808
1809 if (value != MIGRATION_RESUME_ACK_VALUE) {
1810 error_report("%s: illegal resume_ack value %"PRIu32,
1811 __func__, value);
1812 return -1;
1813 }
1814
1815
1816 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
1817 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1818
1819
1820 qemu_sem_post(&s->rp_state.rp_sem);
1821
1822 return 0;
1823}
1824
1825
1826
1827
1828
1829static void migration_release_dst_files(MigrationState *ms)
1830{
1831 QEMUFile *file;
1832
1833 WITH_QEMU_LOCK_GUARD(&ms->qemu_file_lock) {
1834
1835
1836
1837
1838 file = ms->rp_state.from_dst_file;
1839 ms->rp_state.from_dst_file = NULL;
1840 }
1841
1842
1843
1844
1845
1846
1847 if (ms->postcopy_qemufile_src) {
1848 migration_ioc_unregister_yank_from_file(ms->postcopy_qemufile_src);
1849 qemu_file_shutdown(ms->postcopy_qemufile_src);
1850 qemu_fclose(ms->postcopy_qemufile_src);
1851 ms->postcopy_qemufile_src = NULL;
1852 }
1853
1854 qemu_fclose(file);
1855}
1856
1857
1858
1859
1860
1861static void *source_return_path_thread(void *opaque)
1862{
1863 MigrationState *ms = opaque;
1864 QEMUFile *rp = ms->rp_state.from_dst_file;
1865 uint16_t header_len, header_type;
1866 uint8_t buf[512];
1867 uint32_t tmp32, sibling_error;
1868 ram_addr_t start = 0;
1869 size_t len = 0, expected_len;
1870 int res;
1871
1872 trace_source_return_path_thread_entry();
1873 rcu_register_thread();
1874
1875retry:
1876 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1877 migration_is_setup_or_active(ms->state)) {
1878 trace_source_return_path_thread_loop_top();
1879 header_type = qemu_get_be16(rp);
1880 header_len = qemu_get_be16(rp);
1881
1882 if (qemu_file_get_error(rp)) {
1883 mark_source_rp_bad(ms);
1884 goto out;
1885 }
1886
1887 if (header_type >= MIG_RP_MSG_MAX ||
1888 header_type == MIG_RP_MSG_INVALID) {
1889 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1890 header_type, header_len);
1891 mark_source_rp_bad(ms);
1892 goto out;
1893 }
1894
1895 if ((rp_cmd_args[header_type].len != -1 &&
1896 header_len != rp_cmd_args[header_type].len) ||
1897 header_len > sizeof(buf)) {
1898 error_report("RP: Received '%s' message (0x%04x) with"
1899 "incorrect length %d expecting %zu",
1900 rp_cmd_args[header_type].name, header_type, header_len,
1901 (size_t)rp_cmd_args[header_type].len);
1902 mark_source_rp_bad(ms);
1903 goto out;
1904 }
1905
1906
1907 res = qemu_get_buffer(rp, buf, header_len);
1908 if (res != header_len) {
1909 error_report("RP: Failed reading data for message 0x%04x"
1910 " read %d expected %d",
1911 header_type, res, header_len);
1912 mark_source_rp_bad(ms);
1913 goto out;
1914 }
1915
1916
1917 switch (header_type) {
1918 case MIG_RP_MSG_SHUT:
1919 sibling_error = ldl_be_p(buf);
1920 trace_source_return_path_thread_shut(sibling_error);
1921 if (sibling_error) {
1922 error_report("RP: Sibling indicated error %d", sibling_error);
1923 mark_source_rp_bad(ms);
1924 }
1925
1926
1927
1928
1929
1930 goto out;
1931
1932 case MIG_RP_MSG_PONG:
1933 tmp32 = ldl_be_p(buf);
1934 trace_source_return_path_thread_pong(tmp32);
1935 qemu_sem_post(&ms->rp_state.rp_pong_acks);
1936 break;
1937
1938 case MIG_RP_MSG_REQ_PAGES:
1939 start = ldq_be_p(buf);
1940 len = ldl_be_p(buf + 8);
1941 migrate_handle_rp_req_pages(ms, NULL, start, len);
1942 break;
1943
1944 case MIG_RP_MSG_REQ_PAGES_ID:
1945 expected_len = 12 + 1;
1946
1947 if (header_len >= expected_len) {
1948 start = ldq_be_p(buf);
1949 len = ldl_be_p(buf + 8);
1950
1951 tmp32 = buf[12];
1952 buf[13 + tmp32] = '\0';
1953 expected_len += tmp32;
1954 }
1955 if (header_len != expected_len) {
1956 error_report("RP: Req_Page_id with length %d expecting %zd",
1957 header_len, expected_len);
1958 mark_source_rp_bad(ms);
1959 goto out;
1960 }
1961 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
1962 break;
1963
1964 case MIG_RP_MSG_RECV_BITMAP:
1965 if (header_len < 1) {
1966 error_report("%s: missing block name", __func__);
1967 mark_source_rp_bad(ms);
1968 goto out;
1969 }
1970
1971 buf[buf[0] + 1] = '\0';
1972 if (migrate_handle_rp_recv_bitmap(ms, (char *)(buf + 1))) {
1973 mark_source_rp_bad(ms);
1974 goto out;
1975 }
1976 break;
1977
1978 case MIG_RP_MSG_RESUME_ACK:
1979 tmp32 = ldl_be_p(buf);
1980 if (migrate_handle_rp_resume_ack(ms, tmp32)) {
1981 mark_source_rp_bad(ms);
1982 goto out;
1983 }
1984 break;
1985
1986 case MIG_RP_MSG_SWITCHOVER_ACK:
1987 ms->switchover_acked = true;
1988 trace_source_return_path_thread_switchover_acked();
1989 break;
1990
1991 default:
1992 break;
1993 }
1994 }
1995
1996out:
1997 res = qemu_file_get_error(rp);
1998 if (res) {
1999 if (res && migration_in_postcopy()) {
2000
2001
2002
2003
2004 migration_release_dst_files(ms);
2005 rp = NULL;
2006 if (postcopy_pause_return_path_thread(ms)) {
2007
2008
2009
2010
2011 rp = ms->rp_state.from_dst_file;
2012 ms->rp_state.error = false;
2013 goto retry;
2014 }
2015 }
2016
2017 trace_source_return_path_thread_bad_end();
2018 mark_source_rp_bad(ms);
2019 }
2020
2021 trace_source_return_path_thread_end();
2022 migration_release_dst_files(ms);
2023 rcu_unregister_thread();
2024 return NULL;
2025}
2026
2027static int open_return_path_on_source(MigrationState *ms,
2028 bool create_thread)
2029{
2030 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
2031 if (!ms->rp_state.from_dst_file) {
2032 return -1;
2033 }
2034
2035 trace_open_return_path_on_source();
2036
2037 if (!create_thread) {
2038
2039 return 0;
2040 }
2041
2042 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
2043 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
2044 ms->rp_state.rp_thread_created = true;
2045
2046 trace_open_return_path_on_source_continue();
2047
2048 return 0;
2049}
2050
2051
2052static int await_return_path_close_on_source(MigrationState *ms)
2053{
2054
2055
2056
2057
2058
2059 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
2060
2061
2062
2063
2064 qemu_file_shutdown(ms->rp_state.from_dst_file);
2065 mark_source_rp_bad(ms);
2066 }
2067 trace_await_return_path_close_on_source_joining();
2068 qemu_thread_join(&ms->rp_state.rp_thread);
2069 ms->rp_state.rp_thread_created = false;
2070 trace_await_return_path_close_on_source_close();
2071 return ms->rp_state.error;
2072}
2073
2074static inline void
2075migration_wait_main_channel(MigrationState *ms)
2076{
2077
2078 qemu_sem_wait(&ms->rp_state.rp_pong_acks);
2079}
2080
2081
2082
2083
2084
2085static int postcopy_start(MigrationState *ms, Error **errp)
2086{
2087 int ret;
2088 QIOChannelBuffer *bioc;
2089 QEMUFile *fb;
2090 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2091 uint64_t bandwidth = migrate_max_postcopy_bandwidth();
2092 bool restart_block = false;
2093 int cur_state = MIGRATION_STATUS_ACTIVE;
2094
2095 if (migrate_postcopy_preempt()) {
2096 migration_wait_main_channel(ms);
2097 if (postcopy_preempt_establish_channel(ms)) {
2098 migrate_set_state(&ms->state, ms->state, MIGRATION_STATUS_FAILED);
2099 return -1;
2100 }
2101 }
2102
2103 if (!migrate_pause_before_switchover()) {
2104 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
2105 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2106 }
2107
2108 trace_postcopy_start();
2109 qemu_mutex_lock_iothread();
2110 trace_postcopy_start_set_run();
2111
2112 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
2113 global_state_store();
2114 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
2115 if (ret < 0) {
2116 goto fail;
2117 }
2118
2119 ret = migration_maybe_pause(ms, &cur_state,
2120 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2121 if (ret < 0) {
2122 goto fail;
2123 }
2124
2125 ret = bdrv_inactivate_all();
2126 if (ret < 0) {
2127 goto fail;
2128 }
2129 restart_block = true;
2130
2131
2132
2133
2134
2135 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
2136
2137
2138
2139
2140
2141
2142
2143 if (migrate_postcopy_ram()) {
2144 ram_postcopy_send_discard_bitmap(ms);
2145 }
2146
2147
2148
2149
2150
2151
2152 migration_rate_set(bandwidth);
2153 if (migrate_postcopy_ram()) {
2154
2155 qemu_savevm_send_ping(ms->to_dst_file, 2);
2156 }
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169 bioc = qio_channel_buffer_new(4096);
2170 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
2171 fb = qemu_file_new_output(QIO_CHANNEL(bioc));
2172 object_unref(OBJECT(bioc));
2173
2174
2175
2176
2177
2178 qemu_savevm_send_postcopy_listen(fb);
2179
2180 qemu_savevm_state_complete_precopy(fb, false, false);
2181 if (migrate_postcopy_ram()) {
2182 qemu_savevm_send_ping(fb, 3);
2183 }
2184
2185 qemu_savevm_send_postcopy_run(fb);
2186
2187
2188
2189
2190
2191
2192
2193 ret = qemu_file_get_error(ms->to_dst_file);
2194 if (ret) {
2195 error_setg(errp, "postcopy_start: Migration stream errored (pre package)");
2196 goto fail_closefb;
2197 }
2198
2199 restart_block = false;
2200
2201
2202 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
2203 goto fail_closefb;
2204 }
2205 qemu_fclose(fb);
2206
2207
2208
2209
2210
2211 ms->postcopy_after_devices = true;
2212 notifier_list_notify(&migration_state_notifiers, ms);
2213
2214 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
2215
2216 qemu_mutex_unlock_iothread();
2217
2218 if (migrate_postcopy_ram()) {
2219
2220
2221
2222
2223 qemu_savevm_send_ping(ms->to_dst_file, 4);
2224 }
2225
2226 if (migrate_release_ram()) {
2227 ram_postcopy_migrated_memory_release(ms);
2228 }
2229
2230 ret = qemu_file_get_error(ms->to_dst_file);
2231 if (ret) {
2232 error_setg(errp, "postcopy_start: Migration stream errored");
2233 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
2234 MIGRATION_STATUS_FAILED);
2235 }
2236
2237 trace_postcopy_preempt_enabled(migrate_postcopy_preempt());
2238
2239 return ret;
2240
2241fail_closefb:
2242 qemu_fclose(fb);
2243fail:
2244 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
2245 MIGRATION_STATUS_FAILED);
2246 if (restart_block) {
2247
2248
2249
2250 Error *local_err = NULL;
2251
2252 bdrv_activate_all(&local_err);
2253 if (local_err) {
2254 error_report_err(local_err);
2255 }
2256 }
2257 qemu_mutex_unlock_iothread();
2258 return -1;
2259}
2260
2261
2262
2263
2264
2265
2266static int migration_maybe_pause(MigrationState *s,
2267 int *current_active_state,
2268 int new_state)
2269{
2270 if (!migrate_pause_before_switchover()) {
2271 return 0;
2272 }
2273
2274
2275
2276
2277
2278
2279
2280 while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) {
2281
2282 }
2283
2284
2285
2286
2287
2288
2289
2290 if (s->state != MIGRATION_STATUS_CANCELLING) {
2291 qemu_mutex_unlock_iothread();
2292 migrate_set_state(&s->state, *current_active_state,
2293 MIGRATION_STATUS_PRE_SWITCHOVER);
2294 qemu_sem_wait(&s->pause_sem);
2295 migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER,
2296 new_state);
2297 *current_active_state = new_state;
2298 qemu_mutex_lock_iothread();
2299 }
2300
2301 return s->state == new_state ? 0 : -EINVAL;
2302}
2303
2304
2305
2306
2307
2308
2309
2310static void migration_completion(MigrationState *s)
2311{
2312 int ret;
2313 int current_active_state = s->state;
2314
2315 if (s->state == MIGRATION_STATUS_ACTIVE) {
2316 qemu_mutex_lock_iothread();
2317 s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2318 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
2319
2320 s->vm_old_state = runstate_get();
2321 global_state_store();
2322
2323 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
2324 trace_migration_completion_vm_stop(ret);
2325 if (ret >= 0) {
2326 ret = migration_maybe_pause(s, ¤t_active_state,
2327 MIGRATION_STATUS_DEVICE);
2328 }
2329 if (ret >= 0) {
2330
2331
2332
2333
2334
2335 s->block_inactive = !migrate_colo();
2336 migration_rate_set(RATE_LIMIT_DISABLED);
2337 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
2338 s->block_inactive);
2339 }
2340
2341 qemu_mutex_unlock_iothread();
2342
2343 if (ret < 0) {
2344 goto fail;
2345 }
2346 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2347 trace_migration_completion_postcopy_end();
2348
2349 qemu_mutex_lock_iothread();
2350 qemu_savevm_state_complete_postcopy(s->to_dst_file);
2351 qemu_mutex_unlock_iothread();
2352
2353
2354
2355
2356
2357
2358 if (migrate_postcopy_preempt() && s->preempt_pre_7_2) {
2359 postcopy_preempt_shutdown_file(s);
2360 }
2361
2362 trace_migration_completion_postcopy_end_after_complete();
2363 } else {
2364 goto fail;
2365 }
2366
2367
2368
2369
2370
2371
2372
2373 if (s->rp_state.rp_thread_created) {
2374 int rp_error;
2375 trace_migration_return_path_end_before();
2376 rp_error = await_return_path_close_on_source(s);
2377 trace_migration_return_path_end_after(rp_error);
2378 if (rp_error) {
2379 goto fail;
2380 }
2381 }
2382
2383 if (qemu_file_get_error(s->to_dst_file)) {
2384 trace_migration_completion_file_err();
2385 goto fail;
2386 }
2387
2388 if (migrate_colo() && s->state == MIGRATION_STATUS_ACTIVE) {
2389
2390 migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE,
2391 MIGRATION_STATUS_COLO);
2392 } else {
2393 migrate_set_state(&s->state, current_active_state,
2394 MIGRATION_STATUS_COMPLETED);
2395 }
2396
2397 return;
2398
2399fail:
2400 if (s->block_inactive && (s->state == MIGRATION_STATUS_ACTIVE ||
2401 s->state == MIGRATION_STATUS_DEVICE)) {
2402
2403
2404
2405
2406 Error *local_err = NULL;
2407
2408 qemu_mutex_lock_iothread();
2409 bdrv_activate_all(&local_err);
2410 if (local_err) {
2411 error_report_err(local_err);
2412 } else {
2413 s->block_inactive = false;
2414 }
2415 qemu_mutex_unlock_iothread();
2416 }
2417
2418 migrate_set_state(&s->state, current_active_state,
2419 MIGRATION_STATUS_FAILED);
2420}
2421
2422
2423
2424
2425
2426
2427
2428static void bg_migration_completion(MigrationState *s)
2429{
2430 int current_active_state = s->state;
2431
2432 if (s->state == MIGRATION_STATUS_ACTIVE) {
2433
2434
2435
2436
2437
2438
2439 qemu_put_buffer(s->to_dst_file, s->bioc->data, s->bioc->usage);
2440 qemu_fflush(s->to_dst_file);
2441 } else if (s->state == MIGRATION_STATUS_CANCELLING) {
2442 goto fail;
2443 }
2444
2445 if (qemu_file_get_error(s->to_dst_file)) {
2446 trace_migration_completion_file_err();
2447 goto fail;
2448 }
2449
2450 migrate_set_state(&s->state, current_active_state,
2451 MIGRATION_STATUS_COMPLETED);
2452 return;
2453
2454fail:
2455 migrate_set_state(&s->state, current_active_state,
2456 MIGRATION_STATUS_FAILED);
2457}
2458
2459typedef enum MigThrError {
2460
2461 MIG_THR_ERR_NONE = 0,
2462
2463 MIG_THR_ERR_RECOVERED = 1,
2464
2465 MIG_THR_ERR_FATAL = 2,
2466} MigThrError;
2467
2468static int postcopy_resume_handshake(MigrationState *s)
2469{
2470 qemu_savevm_send_postcopy_resume(s->to_dst_file);
2471
2472 while (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
2473 qemu_sem_wait(&s->rp_state.rp_sem);
2474 }
2475
2476 if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2477 return 0;
2478 }
2479
2480 return -1;
2481}
2482
2483
2484static int postcopy_do_resume(MigrationState *s)
2485{
2486 int ret;
2487
2488
2489
2490
2491
2492 ret = qemu_savevm_state_resume_prepare(s);
2493 if (ret) {
2494 error_report("%s: resume_prepare() failure detected: %d",
2495 __func__, ret);
2496 return ret;
2497 }
2498
2499
2500
2501
2502
2503
2504
2505
2506 ret = postcopy_preempt_establish_channel(s);
2507 if (ret) {
2508 error_report("%s: postcopy_preempt_establish_channel(): %d",
2509 __func__, ret);
2510 return ret;
2511 }
2512
2513
2514
2515
2516
2517 ret = postcopy_resume_handshake(s);
2518 if (ret) {
2519 error_report("%s: handshake failed: %d", __func__, ret);
2520 return ret;
2521 }
2522
2523 return 0;
2524}
2525
2526
2527
2528
2529
2530
2531static MigThrError postcopy_pause(MigrationState *s)
2532{
2533 assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
2534
2535 while (true) {
2536 QEMUFile *file;
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547 assert(s->to_dst_file);
2548 migration_ioc_unregister_yank_from_file(s->to_dst_file);
2549 qemu_mutex_lock(&s->qemu_file_lock);
2550 file = s->to_dst_file;
2551 s->to_dst_file = NULL;
2552 qemu_mutex_unlock(&s->qemu_file_lock);
2553
2554 qemu_file_shutdown(file);
2555 qemu_fclose(file);
2556
2557 migrate_set_state(&s->state, s->state,
2558 MIGRATION_STATUS_POSTCOPY_PAUSED);
2559
2560 error_report("Detected IO failure for postcopy. "
2561 "Migration paused.");
2562
2563
2564
2565
2566
2567 while (s->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
2568 qemu_sem_wait(&s->postcopy_pause_sem);
2569 }
2570
2571 if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
2572
2573
2574
2575
2576
2577
2578 qemu_sem_post(&s->postcopy_pause_rp_sem);
2579
2580
2581 if (postcopy_do_resume(s) == 0) {
2582
2583 trace_postcopy_pause_continued();
2584 return MIG_THR_ERR_RECOVERED;
2585 } else {
2586
2587
2588
2589
2590
2591 continue;
2592 }
2593 } else {
2594
2595 return MIG_THR_ERR_FATAL;
2596 }
2597 }
2598}
2599
2600static MigThrError migration_detect_error(MigrationState *s)
2601{
2602 int ret;
2603 int state = s->state;
2604 Error *local_error = NULL;
2605
2606 if (state == MIGRATION_STATUS_CANCELLING ||
2607 state == MIGRATION_STATUS_CANCELLED) {
2608
2609 return MIG_THR_ERR_FATAL;
2610 }
2611
2612
2613
2614
2615
2616 ret = qemu_file_get_error_obj_any(s->to_dst_file,
2617 s->postcopy_qemufile_src,
2618 &local_error);
2619 if (!ret) {
2620
2621 assert(!local_error);
2622 return MIG_THR_ERR_NONE;
2623 }
2624
2625 if (local_error) {
2626 migrate_set_error(s, local_error);
2627 error_free(local_error);
2628 }
2629
2630 if (state == MIGRATION_STATUS_POSTCOPY_ACTIVE && ret) {
2631
2632
2633
2634
2635
2636 return postcopy_pause(s);
2637 } else {
2638
2639
2640
2641
2642 migrate_set_state(&s->state, state, MIGRATION_STATUS_FAILED);
2643 trace_migration_thread_file_err();
2644
2645
2646 return MIG_THR_ERR_FATAL;
2647 }
2648}
2649
2650static void migration_calculate_complete(MigrationState *s)
2651{
2652 uint64_t bytes = migration_transferred_bytes(s->to_dst_file);
2653 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2654 int64_t transfer_time;
2655
2656 s->total_time = end_time - s->start_time;
2657 if (!s->downtime) {
2658
2659
2660
2661
2662 s->downtime = end_time - s->downtime_start;
2663 }
2664
2665 transfer_time = s->total_time - s->setup_time;
2666 if (transfer_time) {
2667 s->mbps = ((double) bytes * 8.0) / transfer_time / 1000;
2668 }
2669}
2670
2671static void update_iteration_initial_status(MigrationState *s)
2672{
2673
2674
2675
2676
2677 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2678 s->iteration_initial_bytes = migration_transferred_bytes(s->to_dst_file);
2679 s->iteration_initial_pages = ram_get_total_transferred_pages();
2680}
2681
2682static void migration_update_counters(MigrationState *s,
2683 int64_t current_time)
2684{
2685 uint64_t transferred, transferred_pages, time_spent;
2686 uint64_t current_bytes;
2687 double bandwidth;
2688
2689 if (current_time < s->iteration_start_time + BUFFER_DELAY) {
2690 return;
2691 }
2692
2693 current_bytes = migration_transferred_bytes(s->to_dst_file);
2694 transferred = current_bytes - s->iteration_initial_bytes;
2695 time_spent = current_time - s->iteration_start_time;
2696 bandwidth = (double)transferred / time_spent;
2697 s->threshold_size = bandwidth * migrate_downtime_limit();
2698
2699 s->mbps = (((double) transferred * 8.0) /
2700 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
2701
2702 transferred_pages = ram_get_total_transferred_pages() -
2703 s->iteration_initial_pages;
2704 s->pages_per_second = (double) transferred_pages /
2705 (((double) time_spent / 1000.0));
2706
2707
2708
2709
2710
2711 if (stat64_get(&mig_stats.dirty_pages_rate) &&
2712 transferred > 10000) {
2713 s->expected_downtime =
2714 stat64_get(&mig_stats.dirty_bytes_last_sync) / bandwidth;
2715 }
2716
2717 migration_rate_reset(s->to_dst_file);
2718
2719 update_iteration_initial_status(s);
2720
2721 trace_migrate_transferred(transferred, time_spent,
2722 bandwidth, s->threshold_size);
2723}
2724
2725static bool migration_can_switchover(MigrationState *s)
2726{
2727 if (!migrate_switchover_ack()) {
2728 return true;
2729 }
2730
2731
2732 if (!runstate_is_running()) {
2733 return true;
2734 }
2735
2736 return s->switchover_acked;
2737}
2738
2739
2740typedef enum {
2741 MIG_ITERATE_RESUME,
2742 MIG_ITERATE_SKIP,
2743 MIG_ITERATE_BREAK,
2744} MigIterateState;
2745
2746
2747
2748
2749
2750static MigIterateState migration_iteration_run(MigrationState *s)
2751{
2752 uint64_t must_precopy, can_postcopy;
2753 Error *local_err = NULL;
2754 bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE;
2755 bool can_switchover = migration_can_switchover(s);
2756
2757 qemu_savevm_state_pending_estimate(&must_precopy, &can_postcopy);
2758 uint64_t pending_size = must_precopy + can_postcopy;
2759
2760 trace_migrate_pending_estimate(pending_size, must_precopy, can_postcopy);
2761
2762 if (must_precopy <= s->threshold_size) {
2763 qemu_savevm_state_pending_exact(&must_precopy, &can_postcopy);
2764 pending_size = must_precopy + can_postcopy;
2765 trace_migrate_pending_exact(pending_size, must_precopy, can_postcopy);
2766 }
2767
2768 if ((!pending_size || pending_size < s->threshold_size) && can_switchover) {
2769 trace_migration_thread_low_pending(pending_size);
2770 migration_completion(s);
2771 return MIG_ITERATE_BREAK;
2772 }
2773
2774
2775 if (!in_postcopy && must_precopy <= s->threshold_size && can_switchover &&
2776 qatomic_read(&s->start_postcopy)) {
2777 if (postcopy_start(s, &local_err)) {
2778 migrate_set_error(s, local_err);
2779 error_report_err(local_err);
2780 }
2781 return MIG_ITERATE_SKIP;
2782 }
2783
2784
2785 qemu_savevm_state_iterate(s->to_dst_file, in_postcopy);
2786 return MIG_ITERATE_RESUME;
2787}
2788
2789static void migration_iteration_finish(MigrationState *s)
2790{
2791
2792 cpu_throttle_stop();
2793
2794 qemu_mutex_lock_iothread();
2795 switch (s->state) {
2796 case MIGRATION_STATUS_COMPLETED:
2797 migration_calculate_complete(s);
2798 runstate_set(RUN_STATE_POSTMIGRATE);
2799 break;
2800 case MIGRATION_STATUS_COLO:
2801 assert(migrate_colo());
2802 migrate_start_colo_process(s);
2803 s->vm_old_state = RUN_STATE_RUNNING;
2804
2805 case MIGRATION_STATUS_FAILED:
2806 case MIGRATION_STATUS_CANCELLED:
2807 case MIGRATION_STATUS_CANCELLING:
2808 if (s->vm_old_state == RUN_STATE_RUNNING) {
2809 if (!runstate_check(RUN_STATE_SHUTDOWN)) {
2810 vm_start();
2811 }
2812 } else {
2813 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
2814 runstate_set(s->vm_old_state);
2815 }
2816 }
2817 break;
2818
2819 default:
2820
2821 error_report("%s: Unknown ending state %d", __func__, s->state);
2822 break;
2823 }
2824 migrate_fd_cleanup_schedule(s);
2825 qemu_mutex_unlock_iothread();
2826}
2827
2828static void bg_migration_iteration_finish(MigrationState *s)
2829{
2830
2831
2832
2833
2834
2835 ram_write_tracking_stop();
2836
2837 qemu_mutex_lock_iothread();
2838 switch (s->state) {
2839 case MIGRATION_STATUS_COMPLETED:
2840 migration_calculate_complete(s);
2841 break;
2842
2843 case MIGRATION_STATUS_ACTIVE:
2844 case MIGRATION_STATUS_FAILED:
2845 case MIGRATION_STATUS_CANCELLED:
2846 case MIGRATION_STATUS_CANCELLING:
2847 break;
2848
2849 default:
2850
2851 error_report("%s: Unknown ending state %d", __func__, s->state);
2852 break;
2853 }
2854
2855 migrate_fd_cleanup_schedule(s);
2856 qemu_mutex_unlock_iothread();
2857}
2858
2859
2860
2861
2862
2863static MigIterateState bg_migration_iteration_run(MigrationState *s)
2864{
2865 int res;
2866
2867 res = qemu_savevm_state_iterate(s->to_dst_file, false);
2868 if (res > 0) {
2869 bg_migration_completion(s);
2870 return MIG_ITERATE_BREAK;
2871 }
2872
2873 return MIG_ITERATE_RESUME;
2874}
2875
2876void migration_make_urgent_request(void)
2877{
2878 qemu_sem_post(&migrate_get_current()->rate_limit_sem);
2879}
2880
2881void migration_consume_urgent_request(void)
2882{
2883 qemu_sem_wait(&migrate_get_current()->rate_limit_sem);
2884}
2885
2886
2887bool migration_rate_limit(void)
2888{
2889 int64_t now = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2890 MigrationState *s = migrate_get_current();
2891
2892 bool urgent = false;
2893 migration_update_counters(s, now);
2894 if (migration_rate_exceeded(s->to_dst_file)) {
2895
2896 if (qemu_file_get_error(s->to_dst_file)) {
2897 return false;
2898 }
2899
2900
2901
2902
2903 int ms = s->iteration_start_time + BUFFER_DELAY - now;
2904 trace_migration_rate_limit_pre(ms);
2905 if (qemu_sem_timedwait(&s->rate_limit_sem, ms) == 0) {
2906
2907
2908
2909
2910
2911
2912
2913 qemu_sem_post(&s->rate_limit_sem);
2914 urgent = true;
2915 }
2916 trace_migration_rate_limit_post(urgent);
2917 }
2918 return urgent;
2919}
2920
2921
2922
2923
2924
2925
2926static void qemu_savevm_wait_unplug(MigrationState *s, int old_state,
2927 int new_state)
2928{
2929 if (qemu_savevm_state_guest_unplug_pending()) {
2930 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_WAIT_UNPLUG);
2931
2932 while (s->state == MIGRATION_STATUS_WAIT_UNPLUG &&
2933 qemu_savevm_state_guest_unplug_pending()) {
2934 qemu_sem_timedwait(&s->wait_unplug_sem, 250);
2935 }
2936 if (s->state != MIGRATION_STATUS_WAIT_UNPLUG) {
2937 int timeout = 120;
2938
2939
2940
2941
2942
2943 while (timeout-- && qemu_savevm_state_guest_unplug_pending()) {
2944 qemu_sem_timedwait(&s->wait_unplug_sem, 250);
2945 }
2946 if (qemu_savevm_state_guest_unplug_pending() &&
2947 !qtest_enabled()) {
2948 warn_report("migration: partially unplugged device on "
2949 "failure");
2950 }
2951 }
2952
2953 migrate_set_state(&s->state, MIGRATION_STATUS_WAIT_UNPLUG, new_state);
2954 } else {
2955 migrate_set_state(&s->state, old_state, new_state);
2956 }
2957}
2958
2959
2960
2961
2962
2963static void *migration_thread(void *opaque)
2964{
2965 MigrationState *s = opaque;
2966 MigrationThread *thread = NULL;
2967 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
2968 MigThrError thr_error;
2969 bool urgent = false;
2970
2971 thread = migration_threads_add("live_migration", qemu_get_thread_id());
2972
2973 rcu_register_thread();
2974
2975 object_ref(OBJECT(s));
2976 update_iteration_initial_status(s);
2977
2978 qemu_savevm_state_header(s->to_dst_file);
2979
2980
2981
2982
2983
2984 if (s->rp_state.rp_thread_created) {
2985
2986 qemu_savevm_send_open_return_path(s->to_dst_file);
2987
2988
2989 qemu_savevm_send_ping(s->to_dst_file, 1);
2990 }
2991
2992 if (migrate_postcopy()) {
2993
2994
2995
2996
2997
2998 qemu_savevm_send_postcopy_advise(s->to_dst_file);
2999 }
3000
3001 if (migrate_colo()) {
3002
3003 qemu_savevm_send_colo_enable(s->to_dst_file);
3004 }
3005
3006 qemu_savevm_state_setup(s->to_dst_file);
3007
3008 qemu_savevm_wait_unplug(s, MIGRATION_STATUS_SETUP,
3009 MIGRATION_STATUS_ACTIVE);
3010
3011 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
3012
3013 trace_migration_thread_setup_complete();
3014
3015 while (migration_is_active(s)) {
3016 if (urgent || !migration_rate_exceeded(s->to_dst_file)) {
3017 MigIterateState iter_state = migration_iteration_run(s);
3018 if (iter_state == MIG_ITERATE_SKIP) {
3019 continue;
3020 } else if (iter_state == MIG_ITERATE_BREAK) {
3021 break;
3022 }
3023 }
3024
3025
3026
3027
3028
3029 thr_error = migration_detect_error(s);
3030 if (thr_error == MIG_THR_ERR_FATAL) {
3031
3032 break;
3033 } else if (thr_error == MIG_THR_ERR_RECOVERED) {
3034
3035
3036
3037
3038
3039 update_iteration_initial_status(s);
3040 }
3041
3042 urgent = migration_rate_limit();
3043 }
3044
3045 trace_migration_thread_after_loop();
3046 migration_iteration_finish(s);
3047 object_unref(OBJECT(s));
3048 rcu_unregister_thread();
3049 migration_threads_remove(thread);
3050 return NULL;
3051}
3052
3053static void bg_migration_vm_start_bh(void *opaque)
3054{
3055 MigrationState *s = opaque;
3056
3057 qemu_bh_delete(s->vm_start_bh);
3058 s->vm_start_bh = NULL;
3059
3060 vm_start();
3061 s->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - s->downtime_start;
3062}
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079static void *bg_migration_thread(void *opaque)
3080{
3081 MigrationState *s = opaque;
3082 int64_t setup_start;
3083 MigThrError thr_error;
3084 QEMUFile *fb;
3085 bool early_fail = true;
3086
3087 rcu_register_thread();
3088 object_ref(OBJECT(s));
3089
3090 migration_rate_set(RATE_LIMIT_DISABLED);
3091
3092 setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102 s->bioc = qio_channel_buffer_new(512 * 1024);
3103 qio_channel_set_name(QIO_CHANNEL(s->bioc), "vmstate-buffer");
3104 fb = qemu_file_new_output(QIO_CHANNEL(s->bioc));
3105 object_unref(OBJECT(s->bioc));
3106
3107 update_iteration_initial_status(s);
3108
3109
3110
3111
3112
3113#ifdef __linux__
3114 ram_write_tracking_prepare();
3115#endif
3116
3117 qemu_savevm_state_header(s->to_dst_file);
3118 qemu_savevm_state_setup(s->to_dst_file);
3119
3120 qemu_savevm_wait_unplug(s, MIGRATION_STATUS_SETUP,
3121 MIGRATION_STATUS_ACTIVE);
3122
3123 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
3124
3125 trace_migration_thread_setup_complete();
3126 s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3127
3128 qemu_mutex_lock_iothread();
3129
3130
3131
3132
3133
3134 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
3135 s->vm_old_state = runstate_get();
3136
3137 global_state_store();
3138
3139 if (vm_stop_force_state(RUN_STATE_PAUSED)) {
3140 goto fail;
3141 }
3142
3143
3144
3145
3146 cpu_synchronize_all_states();
3147 if (qemu_savevm_state_complete_precopy_non_iterable(fb, false, false)) {
3148 goto fail;
3149 }
3150
3151
3152
3153
3154 qemu_fflush(fb);
3155
3156
3157 if (ram_write_tracking_start()) {
3158 goto fail;
3159 }
3160 early_fail = false;
3161
3162
3163
3164
3165
3166
3167
3168 s->vm_start_bh = qemu_bh_new(bg_migration_vm_start_bh, s);
3169 qemu_bh_schedule(s->vm_start_bh);
3170
3171 qemu_mutex_unlock_iothread();
3172
3173 while (migration_is_active(s)) {
3174 MigIterateState iter_state = bg_migration_iteration_run(s);
3175 if (iter_state == MIG_ITERATE_SKIP) {
3176 continue;
3177 } else if (iter_state == MIG_ITERATE_BREAK) {
3178 break;
3179 }
3180
3181
3182
3183
3184
3185 thr_error = migration_detect_error(s);
3186 if (thr_error == MIG_THR_ERR_FATAL) {
3187
3188 break;
3189 }
3190
3191 migration_update_counters(s, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
3192 }
3193
3194 trace_migration_thread_after_loop();
3195
3196fail:
3197 if (early_fail) {
3198 migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE,
3199 MIGRATION_STATUS_FAILED);
3200 qemu_mutex_unlock_iothread();
3201 }
3202
3203 bg_migration_iteration_finish(s);
3204
3205 qemu_fclose(fb);
3206 object_unref(OBJECT(s));
3207 rcu_unregister_thread();
3208
3209 return NULL;
3210}
3211
3212void migrate_fd_connect(MigrationState *s, Error *error_in)
3213{
3214 Error *local_err = NULL;
3215 uint64_t rate_limit;
3216 bool resume = s->state == MIGRATION_STATUS_POSTCOPY_PAUSED;
3217
3218
3219
3220
3221
3222
3223 migrate_error_free(s);
3224
3225 s->expected_downtime = migrate_downtime_limit();
3226 if (resume) {
3227 assert(s->cleanup_bh);
3228 } else {
3229 assert(!s->cleanup_bh);
3230 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup_bh, s);
3231 }
3232 if (error_in) {
3233 migrate_fd_error(s, error_in);
3234 if (resume) {
3235
3236
3237
3238
3239
3240
3241
3242 error_report_err(error_copy(s->error));
3243 } else {
3244 migrate_fd_cleanup(s);
3245 }
3246 return;
3247 }
3248
3249 if (resume) {
3250
3251 rate_limit = migrate_max_postcopy_bandwidth();
3252 } else {
3253
3254 rate_limit = migrate_max_bandwidth();
3255
3256
3257 notifier_list_notify(&migration_state_notifiers, s);
3258 }
3259
3260 migration_rate_set(rate_limit);
3261 qemu_file_set_blocking(s->to_dst_file, true);
3262
3263
3264
3265
3266
3267
3268 if (migrate_postcopy_ram() || migrate_return_path()) {
3269 if (open_return_path_on_source(s, !resume)) {
3270 error_setg(&local_err, "Unable to open return-path for postcopy");
3271 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
3272 migrate_set_error(s, local_err);
3273 error_report_err(local_err);
3274 migrate_fd_cleanup(s);
3275 return;
3276 }
3277 }
3278
3279
3280
3281
3282
3283
3284 if (migrate_postcopy_preempt() && s->preempt_pre_7_2) {
3285 postcopy_preempt_setup(s);
3286 }
3287
3288 if (resume) {
3289
3290 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
3291 MIGRATION_STATUS_POSTCOPY_RECOVER);
3292 qemu_sem_post(&s->postcopy_pause_sem);
3293 return;
3294 }
3295
3296 if (multifd_save_setup(&local_err) != 0) {
3297 migrate_set_error(s, local_err);
3298 error_report_err(local_err);
3299 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3300 MIGRATION_STATUS_FAILED);
3301 migrate_fd_cleanup(s);
3302 return;
3303 }
3304
3305 if (migrate_background_snapshot()) {
3306 qemu_thread_create(&s->thread, "bg_snapshot",
3307 bg_migration_thread, s, QEMU_THREAD_JOINABLE);
3308 } else {
3309 qemu_thread_create(&s->thread, "live_migration",
3310 migration_thread, s, QEMU_THREAD_JOINABLE);
3311 }
3312 s->migration_thread_running = true;
3313}
3314
3315static void migration_class_init(ObjectClass *klass, void *data)
3316{
3317 DeviceClass *dc = DEVICE_CLASS(klass);
3318
3319 dc->user_creatable = false;
3320 device_class_set_props(dc, migration_properties);
3321}
3322
3323static void migration_instance_finalize(Object *obj)
3324{
3325 MigrationState *ms = MIGRATION_OBJ(obj);
3326
3327 qemu_mutex_destroy(&ms->error_mutex);
3328 qemu_mutex_destroy(&ms->qemu_file_lock);
3329 qemu_sem_destroy(&ms->wait_unplug_sem);
3330 qemu_sem_destroy(&ms->rate_limit_sem);
3331 qemu_sem_destroy(&ms->pause_sem);
3332 qemu_sem_destroy(&ms->postcopy_pause_sem);
3333 qemu_sem_destroy(&ms->postcopy_pause_rp_sem);
3334 qemu_sem_destroy(&ms->rp_state.rp_sem);
3335 qemu_sem_destroy(&ms->rp_state.rp_pong_acks);
3336 qemu_sem_destroy(&ms->postcopy_qemufile_src_sem);
3337 error_free(ms->error);
3338}
3339
3340static void migration_instance_init(Object *obj)
3341{
3342 MigrationState *ms = MIGRATION_OBJ(obj);
3343
3344 ms->state = MIGRATION_STATUS_NONE;
3345 ms->mbps = -1;
3346 ms->pages_per_second = -1;
3347 qemu_sem_init(&ms->pause_sem, 0);
3348 qemu_mutex_init(&ms->error_mutex);
3349
3350 migrate_params_init(&ms->parameters);
3351
3352 qemu_sem_init(&ms->postcopy_pause_sem, 0);
3353 qemu_sem_init(&ms->postcopy_pause_rp_sem, 0);
3354 qemu_sem_init(&ms->rp_state.rp_sem, 0);
3355 qemu_sem_init(&ms->rp_state.rp_pong_acks, 0);
3356 qemu_sem_init(&ms->rate_limit_sem, 0);
3357 qemu_sem_init(&ms->wait_unplug_sem, 0);
3358 qemu_sem_init(&ms->postcopy_qemufile_src_sem, 0);
3359 qemu_mutex_init(&ms->qemu_file_lock);
3360}
3361
3362
3363
3364
3365
3366static bool migration_object_check(MigrationState *ms, Error **errp)
3367{
3368
3369 bool old_caps[MIGRATION_CAPABILITY__MAX] = { 0 };
3370
3371 if (!migrate_params_check(&ms->parameters, errp)) {
3372 return false;
3373 }
3374
3375 return migrate_caps_check(old_caps, ms->capabilities, errp);
3376}
3377
3378static const TypeInfo migration_type = {
3379 .name = TYPE_MIGRATION,
3380
3381
3382
3383
3384
3385
3386
3387
3388 .parent = TYPE_DEVICE,
3389 .class_init = migration_class_init,
3390 .class_size = sizeof(MigrationClass),
3391 .instance_size = sizeof(MigrationState),
3392 .instance_init = migration_instance_init,
3393 .instance_finalize = migration_instance_finalize,
3394};
3395
3396static void register_migration_types(void)
3397{
3398 type_register_static(&migration_type);
3399}
3400
3401type_init(register_migration_types);
3402