4 * Copyright IBM, Corp. 2008
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
16 #include "qemu-common.h"
17 #include "qemu/main-loop.h"
18 #include "migration/migration.h"
19 #include "monitor/monitor.h"
20 #include "migration/qemu-file.h"
21 #include "sysemu/sysemu.h"
22 #include "block/block.h"
23 #include "qemu/sockets.h"
24 #include "migration/block.h"
25 #include "qemu/thread.h"
26 #include "qmp-commands.h"
39 #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
41 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
43 #define BUFFER_DELAY 100
44 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
46 /* Migration XBZRLE default cache size */
47 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
49 static NotifierList migration_state_notifiers
=
50 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers
);
52 static bool deferred_incoming
;
54 /* When we add fault tolerance, we could have several
55 migrations at once. For now we don't need to add
56 dynamic creation of migration */
58 MigrationState
*migrate_get_current(void)
60 static MigrationState current_migration
= {
61 .state
= MIG_STATE_NONE
,
62 .bandwidth_limit
= MAX_THROTTLE
,
63 .xbzrle_cache_size
= DEFAULT_MIGRATE_CACHE_SIZE
,
67 return ¤t_migration
;
71 * Called on -incoming with a defer: uri.
72 * The migration can be started later after any parameters have been
75 static void deferred_incoming_migration(Error
**errp
)
77 if (deferred_incoming
) {
78 error_setg(errp
, "Incoming migration already deferred");
80 deferred_incoming
= true;
83 void qemu_start_incoming_migration(const char *uri
, Error
**errp
)
87 if (!strcmp(uri
, "defer")) {
88 deferred_incoming_migration(errp
);
89 } else if (strstart(uri
, "tcp:", &p
)) {
90 tcp_start_incoming_migration(p
, errp
);
92 } else if (strstart(uri
, "rdma:", &p
)) {
93 rdma_start_incoming_migration(p
, errp
);
96 } else if (strstart(uri
, "exec:", &p
)) {
97 exec_start_incoming_migration(p
, errp
);
98 } else if (strstart(uri
, "unix:", &p
)) {
99 unix_start_incoming_migration(p
, errp
);
100 } else if (strstart(uri
, "fd:", &p
)) {
101 fd_start_incoming_migration(p
, errp
);
104 error_setg(errp
, "unknown migration protocol: %s", uri
);
108 static void process_incoming_migration_co(void *opaque
)
110 QEMUFile
*f
= opaque
;
111 Error
*local_err
= NULL
;
114 ret
= qemu_loadvm_state(f
);
116 free_xbzrle_decoded_buf();
118 error_report("load of migration failed: %s", strerror(-ret
));
121 qemu_announce_self();
123 /* Make sure all file formats flush their mutable metadata */
124 bdrv_invalidate_cache_all(&local_err
);
126 qerror_report_err(local_err
);
127 error_free(local_err
);
134 runstate_set(RUN_STATE_PAUSED
);
138 void process_incoming_migration(QEMUFile
*f
)
140 Coroutine
*co
= qemu_coroutine_create(process_incoming_migration_co
);
141 int fd
= qemu_get_fd(f
);
144 qemu_set_nonblock(fd
);
145 qemu_coroutine_enter(co
, f
);
148 /* amount of nanoseconds we are willing to wait for migration to be down.
149 * the choice of nanoseconds is because it is the maximum resolution that
150 * get_clock() can achieve. It is an internal measure. All user-visible
151 * units must be in seconds */
152 static uint64_t max_downtime
= 300000000;
154 uint64_t migrate_max_downtime(void)
159 MigrationCapabilityStatusList
*qmp_query_migrate_capabilities(Error
**errp
)
161 MigrationCapabilityStatusList
*head
= NULL
;
162 MigrationCapabilityStatusList
*caps
;
163 MigrationState
*s
= migrate_get_current();
166 caps
= NULL
; /* silence compiler warning */
167 for (i
= 0; i
< MIGRATION_CAPABILITY_MAX
; i
++) {
169 head
= g_malloc0(sizeof(*caps
));
172 caps
->next
= g_malloc0(sizeof(*caps
));
176 g_malloc(sizeof(*caps
->value
));
177 caps
->value
->capability
= i
;
178 caps
->value
->state
= s
->enabled_capabilities
[i
];
184 static void get_xbzrle_cache_stats(MigrationInfo
*info
)
186 if (migrate_use_xbzrle()) {
187 info
->has_xbzrle_cache
= true;
188 info
->xbzrle_cache
= g_malloc0(sizeof(*info
->xbzrle_cache
));
189 info
->xbzrle_cache
->cache_size
= migrate_xbzrle_cache_size();
190 info
->xbzrle_cache
->bytes
= xbzrle_mig_bytes_transferred();
191 info
->xbzrle_cache
->pages
= xbzrle_mig_pages_transferred();
192 info
->xbzrle_cache
->cache_miss
= xbzrle_mig_pages_cache_miss();
193 info
->xbzrle_cache
->cache_miss_rate
= xbzrle_mig_cache_miss_rate();
194 info
->xbzrle_cache
->overflow
= xbzrle_mig_pages_overflow();
198 MigrationInfo
*qmp_query_migrate(Error
**errp
)
200 MigrationInfo
*info
= g_malloc0(sizeof(*info
));
201 MigrationState
*s
= migrate_get_current();
205 /* no migration has happened ever */
207 case MIG_STATE_SETUP
:
208 info
->has_status
= true;
209 info
->status
= g_strdup("setup");
210 info
->has_total_time
= false;
212 case MIG_STATE_ACTIVE
:
213 case MIG_STATE_CANCELLING
:
214 info
->has_status
= true;
215 info
->status
= g_strdup("active");
216 info
->has_total_time
= true;
217 info
->total_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
)
219 info
->has_expected_downtime
= true;
220 info
->expected_downtime
= s
->expected_downtime
;
221 info
->has_setup_time
= true;
222 info
->setup_time
= s
->setup_time
;
224 info
->has_ram
= true;
225 info
->ram
= g_malloc0(sizeof(*info
->ram
));
226 info
->ram
->transferred
= ram_bytes_transferred();
227 info
->ram
->remaining
= ram_bytes_remaining();
228 info
->ram
->total
= ram_bytes_total();
229 info
->ram
->duplicate
= dup_mig_pages_transferred();
230 info
->ram
->skipped
= skipped_mig_pages_transferred();
231 info
->ram
->normal
= norm_mig_pages_transferred();
232 info
->ram
->normal_bytes
= norm_mig_bytes_transferred();
233 info
->ram
->dirty_pages_rate
= s
->dirty_pages_rate
;
234 info
->ram
->mbps
= s
->mbps
;
235 info
->ram
->dirty_sync_count
= s
->dirty_sync_count
;
237 if (blk_mig_active()) {
238 info
->has_disk
= true;
239 info
->disk
= g_malloc0(sizeof(*info
->disk
));
240 info
->disk
->transferred
= blk_mig_bytes_transferred();
241 info
->disk
->remaining
= blk_mig_bytes_remaining();
242 info
->disk
->total
= blk_mig_bytes_total();
245 get_xbzrle_cache_stats(info
);
247 case MIG_STATE_COMPLETED
:
248 get_xbzrle_cache_stats(info
);
250 info
->has_status
= true;
251 info
->status
= g_strdup("completed");
252 info
->has_total_time
= true;
253 info
->total_time
= s
->total_time
;
254 info
->has_downtime
= true;
255 info
->downtime
= s
->downtime
;
256 info
->has_setup_time
= true;
257 info
->setup_time
= s
->setup_time
;
259 info
->has_ram
= true;
260 info
->ram
= g_malloc0(sizeof(*info
->ram
));
261 info
->ram
->transferred
= ram_bytes_transferred();
262 info
->ram
->remaining
= 0;
263 info
->ram
->total
= ram_bytes_total();
264 info
->ram
->duplicate
= dup_mig_pages_transferred();
265 info
->ram
->skipped
= skipped_mig_pages_transferred();
266 info
->ram
->normal
= norm_mig_pages_transferred();
267 info
->ram
->normal_bytes
= norm_mig_bytes_transferred();
268 info
->ram
->mbps
= s
->mbps
;
269 info
->ram
->dirty_sync_count
= s
->dirty_sync_count
;
271 case MIG_STATE_ERROR
:
272 info
->has_status
= true;
273 info
->status
= g_strdup("failed");
275 case MIG_STATE_CANCELLED
:
276 info
->has_status
= true;
277 info
->status
= g_strdup("cancelled");
284 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList
*params
,
287 MigrationState
*s
= migrate_get_current();
288 MigrationCapabilityStatusList
*cap
;
290 if (s
->state
== MIG_STATE_ACTIVE
|| s
->state
== MIG_STATE_SETUP
) {
291 error_set(errp
, QERR_MIGRATION_ACTIVE
);
295 for (cap
= params
; cap
; cap
= cap
->next
) {
296 s
->enabled_capabilities
[cap
->value
->capability
] = cap
->value
->state
;
300 /* shared migration helpers */
302 static void migrate_set_state(MigrationState
*s
, int old_state
, int new_state
)
304 if (atomic_cmpxchg(&s
->state
, old_state
, new_state
) == new_state
) {
305 trace_migrate_set_state(new_state
);
309 static void migrate_fd_cleanup(void *opaque
)
311 MigrationState
*s
= opaque
;
313 qemu_bh_delete(s
->cleanup_bh
);
314 s
->cleanup_bh
= NULL
;
317 trace_migrate_fd_cleanup();
318 qemu_mutex_unlock_iothread();
319 qemu_thread_join(&s
->thread
);
320 qemu_mutex_lock_iothread();
322 qemu_fclose(s
->file
);
326 assert(s
->state
!= MIG_STATE_ACTIVE
);
328 if (s
->state
!= MIG_STATE_COMPLETED
) {
329 qemu_savevm_state_cancel();
330 if (s
->state
== MIG_STATE_CANCELLING
) {
331 migrate_set_state(s
, MIG_STATE_CANCELLING
, MIG_STATE_CANCELLED
);
335 notifier_list_notify(&migration_state_notifiers
, s
);
338 void migrate_fd_error(MigrationState
*s
)
340 trace_migrate_fd_error();
341 assert(s
->file
== NULL
);
342 s
->state
= MIG_STATE_ERROR
;
343 trace_migrate_set_state(MIG_STATE_ERROR
);
344 notifier_list_notify(&migration_state_notifiers
, s
);
347 static void migrate_fd_cancel(MigrationState
*s
)
350 QEMUFile
*f
= migrate_get_current()->file
;
351 trace_migrate_fd_cancel();
354 old_state
= s
->state
;
355 if (old_state
!= MIG_STATE_SETUP
&& old_state
!= MIG_STATE_ACTIVE
) {
358 migrate_set_state(s
, old_state
, MIG_STATE_CANCELLING
);
359 } while (s
->state
!= MIG_STATE_CANCELLING
);
362 * If we're unlucky the migration code might be stuck somewhere in a
363 * send/write while the network has failed and is waiting to timeout;
364 * if we've got shutdown(2) available then we can force it to quit.
365 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
366 * called in a bh, so there is no race against this cancel.
368 if (s
->state
== MIG_STATE_CANCELLING
&& f
) {
369 qemu_file_shutdown(f
);
373 void add_migration_state_change_notifier(Notifier
*notify
)
375 notifier_list_add(&migration_state_notifiers
, notify
);
378 void remove_migration_state_change_notifier(Notifier
*notify
)
380 notifier_remove(notify
);
383 bool migration_in_setup(MigrationState
*s
)
385 return s
->state
== MIG_STATE_SETUP
;
388 bool migration_has_finished(MigrationState
*s
)
390 return s
->state
== MIG_STATE_COMPLETED
;
393 bool migration_has_failed(MigrationState
*s
)
395 return (s
->state
== MIG_STATE_CANCELLED
||
396 s
->state
== MIG_STATE_ERROR
);
399 static MigrationState
*migrate_init(const MigrationParams
*params
)
401 MigrationState
*s
= migrate_get_current();
402 int64_t bandwidth_limit
= s
->bandwidth_limit
;
403 bool enabled_capabilities
[MIGRATION_CAPABILITY_MAX
];
404 int64_t xbzrle_cache_size
= s
->xbzrle_cache_size
;
406 memcpy(enabled_capabilities
, s
->enabled_capabilities
,
407 sizeof(enabled_capabilities
));
409 memset(s
, 0, sizeof(*s
));
411 memcpy(s
->enabled_capabilities
, enabled_capabilities
,
412 sizeof(enabled_capabilities
));
413 s
->xbzrle_cache_size
= xbzrle_cache_size
;
415 s
->bandwidth_limit
= bandwidth_limit
;
416 s
->state
= MIG_STATE_SETUP
;
417 trace_migrate_set_state(MIG_STATE_SETUP
);
419 s
->total_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
423 static GSList
*migration_blockers
;
425 void migrate_add_blocker(Error
*reason
)
427 migration_blockers
= g_slist_prepend(migration_blockers
, reason
);
430 void migrate_del_blocker(Error
*reason
)
432 migration_blockers
= g_slist_remove(migration_blockers
, reason
);
435 void qmp_migrate_incoming(const char *uri
, Error
**errp
)
437 Error
*local_err
= NULL
;
439 if (!deferred_incoming
) {
440 error_setg(errp
, "'-incoming defer' is required for migrate_incoming");
444 qemu_start_incoming_migration(uri
, &local_err
);
447 error_propagate(errp
, local_err
);
451 deferred_incoming
= false;
454 void qmp_migrate(const char *uri
, bool has_blk
, bool blk
,
455 bool has_inc
, bool inc
, bool has_detach
, bool detach
,
458 Error
*local_err
= NULL
;
459 MigrationState
*s
= migrate_get_current();
460 MigrationParams params
;
463 params
.blk
= has_blk
&& blk
;
464 params
.shared
= has_inc
&& inc
;
466 if (s
->state
== MIG_STATE_ACTIVE
|| s
->state
== MIG_STATE_SETUP
||
467 s
->state
== MIG_STATE_CANCELLING
) {
468 error_set(errp
, QERR_MIGRATION_ACTIVE
);
472 if (runstate_check(RUN_STATE_INMIGRATE
)) {
473 error_setg(errp
, "Guest is waiting for an incoming migration");
477 if (qemu_savevm_state_blocked(errp
)) {
481 if (migration_blockers
) {
482 *errp
= error_copy(migration_blockers
->data
);
486 s
= migrate_init(¶ms
);
488 if (strstart(uri
, "tcp:", &p
)) {
489 tcp_start_outgoing_migration(s
, p
, &local_err
);
491 } else if (strstart(uri
, "rdma:", &p
)) {
492 rdma_start_outgoing_migration(s
, p
, &local_err
);
495 } else if (strstart(uri
, "exec:", &p
)) {
496 exec_start_outgoing_migration(s
, p
, &local_err
);
497 } else if (strstart(uri
, "unix:", &p
)) {
498 unix_start_outgoing_migration(s
, p
, &local_err
);
499 } else if (strstart(uri
, "fd:", &p
)) {
500 fd_start_outgoing_migration(s
, p
, &local_err
);
503 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "uri", "a valid migration protocol");
504 s
->state
= MIG_STATE_ERROR
;
510 error_propagate(errp
, local_err
);
515 void qmp_migrate_cancel(Error
**errp
)
517 migrate_fd_cancel(migrate_get_current());
520 void qmp_migrate_set_cache_size(int64_t value
, Error
**errp
)
522 MigrationState
*s
= migrate_get_current();
525 /* Check for truncation */
526 if (value
!= (size_t)value
) {
527 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "cache size",
528 "exceeding address space");
532 /* Cache should not be larger than guest ram size */
533 if (value
> ram_bytes_total()) {
534 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "cache size",
535 "exceeds guest ram size ");
539 new_size
= xbzrle_cache_resize(value
);
541 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "cache size",
542 "is smaller than page size");
546 s
->xbzrle_cache_size
= new_size
;
549 int64_t qmp_query_migrate_cache_size(Error
**errp
)
551 return migrate_xbzrle_cache_size();
554 void qmp_migrate_set_speed(int64_t value
, Error
**errp
)
561 if (value
> SIZE_MAX
) {
565 s
= migrate_get_current();
566 s
->bandwidth_limit
= value
;
568 qemu_file_set_rate_limit(s
->file
, s
->bandwidth_limit
/ XFER_LIMIT_RATIO
);
572 void qmp_migrate_set_downtime(double value
, Error
**errp
)
575 value
= MAX(0, MIN(UINT64_MAX
, value
));
576 max_downtime
= (uint64_t)value
;
579 bool migrate_rdma_pin_all(void)
583 s
= migrate_get_current();
585 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_RDMA_PIN_ALL
];
588 bool migrate_auto_converge(void)
592 s
= migrate_get_current();
594 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_AUTO_CONVERGE
];
597 bool migrate_zero_blocks(void)
601 s
= migrate_get_current();
603 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_ZERO_BLOCKS
];
606 int migrate_use_xbzrle(void)
610 s
= migrate_get_current();
612 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_XBZRLE
];
615 int64_t migrate_xbzrle_cache_size(void)
619 s
= migrate_get_current();
621 return s
->xbzrle_cache_size
;
624 /* migration thread support */
626 static void *migration_thread(void *opaque
)
628 MigrationState
*s
= opaque
;
629 int64_t initial_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
630 int64_t setup_start
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
631 int64_t initial_bytes
= 0;
632 int64_t max_size
= 0;
633 int64_t start_time
= initial_time
;
634 bool old_vm_running
= false;
636 qemu_savevm_state_begin(s
->file
, &s
->params
);
638 s
->setup_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
) - setup_start
;
639 migrate_set_state(s
, MIG_STATE_SETUP
, MIG_STATE_ACTIVE
);
641 while (s
->state
== MIG_STATE_ACTIVE
) {
642 int64_t current_time
;
643 uint64_t pending_size
;
645 if (!qemu_file_rate_limit(s
->file
)) {
646 pending_size
= qemu_savevm_state_pending(s
->file
, max_size
);
647 trace_migrate_pending(pending_size
, max_size
);
648 if (pending_size
&& pending_size
>= max_size
) {
649 qemu_savevm_state_iterate(s
->file
);
653 qemu_mutex_lock_iothread();
654 start_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
655 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
656 old_vm_running
= runstate_is_running();
658 ret
= vm_stop_force_state(RUN_STATE_FINISH_MIGRATE
);
660 qemu_file_set_rate_limit(s
->file
, INT64_MAX
);
661 qemu_savevm_state_complete(s
->file
);
663 qemu_mutex_unlock_iothread();
666 migrate_set_state(s
, MIG_STATE_ACTIVE
, MIG_STATE_ERROR
);
670 if (!qemu_file_get_error(s
->file
)) {
671 migrate_set_state(s
, MIG_STATE_ACTIVE
, MIG_STATE_COMPLETED
);
677 if (qemu_file_get_error(s
->file
)) {
678 migrate_set_state(s
, MIG_STATE_ACTIVE
, MIG_STATE_ERROR
);
681 current_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
682 if (current_time
>= initial_time
+ BUFFER_DELAY
) {
683 uint64_t transferred_bytes
= qemu_ftell(s
->file
) - initial_bytes
;
684 uint64_t time_spent
= current_time
- initial_time
;
685 double bandwidth
= transferred_bytes
/ time_spent
;
686 max_size
= bandwidth
* migrate_max_downtime() / 1000000;
688 s
->mbps
= time_spent
? (((double) transferred_bytes
* 8.0) /
689 ((double) time_spent
/ 1000.0)) / 1000.0 / 1000.0 : -1;
691 trace_migrate_transferred(transferred_bytes
, time_spent
,
692 bandwidth
, max_size
);
693 /* if we haven't sent anything, we don't want to recalculate
694 10000 is a small enough number for our purposes */
695 if (s
->dirty_bytes_rate
&& transferred_bytes
> 10000) {
696 s
->expected_downtime
= s
->dirty_bytes_rate
/ bandwidth
;
699 qemu_file_reset_rate_limit(s
->file
);
700 initial_time
= current_time
;
701 initial_bytes
= qemu_ftell(s
->file
);
703 if (qemu_file_rate_limit(s
->file
)) {
704 /* usleep expects microseconds */
705 g_usleep((initial_time
+ BUFFER_DELAY
- current_time
)*1000);
709 qemu_mutex_lock_iothread();
710 if (s
->state
== MIG_STATE_COMPLETED
) {
711 int64_t end_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
712 uint64_t transferred_bytes
= qemu_ftell(s
->file
);
713 s
->total_time
= end_time
- s
->total_time
;
714 s
->downtime
= end_time
- start_time
;
716 s
->mbps
= (((double) transferred_bytes
* 8.0) /
717 ((double) s
->total_time
)) / 1000;
719 runstate_set(RUN_STATE_POSTMIGRATE
);
721 if (old_vm_running
) {
725 qemu_bh_schedule(s
->cleanup_bh
);
726 qemu_mutex_unlock_iothread();
731 void migrate_fd_connect(MigrationState
*s
)
733 s
->state
= MIG_STATE_SETUP
;
734 trace_migrate_set_state(MIG_STATE_SETUP
);
736 /* This is a best 1st approximation. ns to ms */
737 s
->expected_downtime
= max_downtime
/1000000;
738 s
->cleanup_bh
= qemu_bh_new(migrate_fd_cleanup
, s
);
740 qemu_file_set_rate_limit(s
->file
,
741 s
->bandwidth_limit
/ XFER_LIMIT_RATIO
);
743 /* Notify before starting migration thread */
744 notifier_list_notify(&migration_state_notifiers
, s
);
746 qemu_thread_create(&s
->thread
, "migration", migration_thread
, s
,
747 QEMU_THREAD_JOINABLE
);