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"
29 //#define DEBUG_MIGRATION
31 #ifdef DEBUG_MIGRATION
32 #define DPRINTF(fmt, ...) \
33 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
35 #define DPRINTF(fmt, ...) \
49 #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
51 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
53 #define BUFFER_DELAY 100
54 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
56 /* Migration XBZRLE default cache size */
57 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
59 static NotifierList migration_state_notifiers
=
60 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers
);
62 /* When we add fault tolerance, we could have several
63 migrations at once. For now we don't need to add
64 dynamic creation of migration */
66 MigrationState
*migrate_get_current(void)
68 static MigrationState current_migration
= {
69 .state
= MIG_STATE_NONE
,
70 .bandwidth_limit
= MAX_THROTTLE
,
71 .xbzrle_cache_size
= DEFAULT_MIGRATE_CACHE_SIZE
,
75 return ¤t_migration
;
78 void qemu_start_incoming_migration(const char *uri
, Error
**errp
)
82 if (strstart(uri
, "tcp:", &p
))
83 tcp_start_incoming_migration(p
, errp
);
85 else if (strstart(uri
, "rdma:", &p
))
86 rdma_start_incoming_migration(p
, errp
);
89 else if (strstart(uri
, "exec:", &p
))
90 exec_start_incoming_migration(p
, errp
);
91 else if (strstart(uri
, "unix:", &p
))
92 unix_start_incoming_migration(p
, errp
);
93 else if (strstart(uri
, "fd:", &p
))
94 fd_start_incoming_migration(p
, errp
);
97 error_setg(errp
, "unknown migration protocol: %s", uri
);
101 static void process_incoming_migration_co(void *opaque
)
103 QEMUFile
*f
= opaque
;
104 Error
*local_err
= NULL
;
107 ret
= qemu_loadvm_state(f
);
109 free_xbzrle_decoded_buf();
111 fprintf(stderr
, "load of migration failed\n");
114 qemu_announce_self();
115 DPRINTF("successfully loaded vm state\n");
117 bdrv_clear_incoming_migration_all();
118 /* Make sure all file formats flush their mutable metadata */
119 bdrv_invalidate_cache_all(&local_err
);
121 qerror_report_err(local_err
);
122 error_free(local_err
);
129 runstate_set(RUN_STATE_PAUSED
);
133 void process_incoming_migration(QEMUFile
*f
)
135 Coroutine
*co
= qemu_coroutine_create(process_incoming_migration_co
);
136 int fd
= qemu_get_fd(f
);
139 qemu_set_nonblock(fd
);
140 qemu_coroutine_enter(co
, f
);
143 /* amount of nanoseconds we are willing to wait for migration to be down.
144 * the choice of nanoseconds is because it is the maximum resolution that
145 * get_clock() can achieve. It is an internal measure. All user-visible
146 * units must be in seconds */
147 static uint64_t max_downtime
= 30000000;
149 uint64_t migrate_max_downtime(void)
154 MigrationCapabilityStatusList
*qmp_query_migrate_capabilities(Error
**errp
)
156 MigrationCapabilityStatusList
*head
= NULL
;
157 MigrationCapabilityStatusList
*caps
;
158 MigrationState
*s
= migrate_get_current();
161 caps
= NULL
; /* silence compiler warning */
162 for (i
= 0; i
< MIGRATION_CAPABILITY_MAX
; i
++) {
164 head
= g_malloc0(sizeof(*caps
));
167 caps
->next
= g_malloc0(sizeof(*caps
));
171 g_malloc(sizeof(*caps
->value
));
172 caps
->value
->capability
= i
;
173 caps
->value
->state
= s
->enabled_capabilities
[i
];
179 static void get_xbzrle_cache_stats(MigrationInfo
*info
)
181 if (migrate_use_xbzrle()) {
182 info
->has_xbzrle_cache
= true;
183 info
->xbzrle_cache
= g_malloc0(sizeof(*info
->xbzrle_cache
));
184 info
->xbzrle_cache
->cache_size
= migrate_xbzrle_cache_size();
185 info
->xbzrle_cache
->bytes
= xbzrle_mig_bytes_transferred();
186 info
->xbzrle_cache
->pages
= xbzrle_mig_pages_transferred();
187 info
->xbzrle_cache
->cache_miss
= xbzrle_mig_pages_cache_miss();
188 info
->xbzrle_cache
->overflow
= xbzrle_mig_pages_overflow();
192 MigrationInfo
*qmp_query_migrate(Error
**errp
)
194 MigrationInfo
*info
= g_malloc0(sizeof(*info
));
195 MigrationState
*s
= migrate_get_current();
199 /* no migration has happened ever */
201 case MIG_STATE_SETUP
:
202 info
->has_status
= true;
203 info
->status
= g_strdup("setup");
204 info
->has_total_time
= false;
206 case MIG_STATE_ACTIVE
:
207 case MIG_STATE_CANCELLING
:
208 info
->has_status
= true;
209 info
->status
= g_strdup("active");
210 info
->has_total_time
= true;
211 info
->total_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
)
213 info
->has_expected_downtime
= true;
214 info
->expected_downtime
= s
->expected_downtime
;
215 info
->has_setup_time
= true;
216 info
->setup_time
= s
->setup_time
;
218 info
->has_ram
= true;
219 info
->ram
= g_malloc0(sizeof(*info
->ram
));
220 info
->ram
->transferred
= ram_bytes_transferred();
221 info
->ram
->remaining
= ram_bytes_remaining();
222 info
->ram
->total
= ram_bytes_total();
223 info
->ram
->duplicate
= dup_mig_pages_transferred();
224 info
->ram
->skipped
= skipped_mig_pages_transferred();
225 info
->ram
->normal
= norm_mig_pages_transferred();
226 info
->ram
->normal_bytes
= norm_mig_bytes_transferred();
227 info
->ram
->dirty_pages_rate
= s
->dirty_pages_rate
;
228 info
->ram
->mbps
= s
->mbps
;
230 if (blk_mig_active()) {
231 info
->has_disk
= true;
232 info
->disk
= g_malloc0(sizeof(*info
->disk
));
233 info
->disk
->transferred
= blk_mig_bytes_transferred();
234 info
->disk
->remaining
= blk_mig_bytes_remaining();
235 info
->disk
->total
= blk_mig_bytes_total();
238 get_xbzrle_cache_stats(info
);
240 case MIG_STATE_COMPLETED
:
241 get_xbzrle_cache_stats(info
);
243 info
->has_status
= true;
244 info
->status
= g_strdup("completed");
245 info
->has_total_time
= true;
246 info
->total_time
= s
->total_time
;
247 info
->has_downtime
= true;
248 info
->downtime
= s
->downtime
;
249 info
->has_setup_time
= true;
250 info
->setup_time
= s
->setup_time
;
252 info
->has_ram
= true;
253 info
->ram
= g_malloc0(sizeof(*info
->ram
));
254 info
->ram
->transferred
= ram_bytes_transferred();
255 info
->ram
->remaining
= 0;
256 info
->ram
->total
= ram_bytes_total();
257 info
->ram
->duplicate
= dup_mig_pages_transferred();
258 info
->ram
->skipped
= skipped_mig_pages_transferred();
259 info
->ram
->normal
= norm_mig_pages_transferred();
260 info
->ram
->normal_bytes
= norm_mig_bytes_transferred();
261 info
->ram
->mbps
= s
->mbps
;
263 case MIG_STATE_ERROR
:
264 info
->has_status
= true;
265 info
->status
= g_strdup("failed");
267 case MIG_STATE_CANCELLED
:
268 info
->has_status
= true;
269 info
->status
= g_strdup("cancelled");
276 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList
*params
,
279 MigrationState
*s
= migrate_get_current();
280 MigrationCapabilityStatusList
*cap
;
282 if (s
->state
== MIG_STATE_ACTIVE
|| s
->state
== MIG_STATE_SETUP
) {
283 error_set(errp
, QERR_MIGRATION_ACTIVE
);
287 for (cap
= params
; cap
; cap
= cap
->next
) {
288 s
->enabled_capabilities
[cap
->value
->capability
] = cap
->value
->state
;
292 /* shared migration helpers */
294 static void migrate_set_state(MigrationState
*s
, int old_state
, int new_state
)
296 if (atomic_cmpxchg(&s
->state
, old_state
, new_state
) == new_state
) {
297 trace_migrate_set_state(new_state
);
301 static void migrate_fd_cleanup(void *opaque
)
303 MigrationState
*s
= opaque
;
305 qemu_bh_delete(s
->cleanup_bh
);
306 s
->cleanup_bh
= NULL
;
309 DPRINTF("closing file\n");
310 qemu_mutex_unlock_iothread();
311 qemu_thread_join(&s
->thread
);
312 qemu_mutex_lock_iothread();
314 qemu_fclose(s
->file
);
318 assert(s
->state
!= MIG_STATE_ACTIVE
);
320 if (s
->state
!= MIG_STATE_COMPLETED
) {
321 qemu_savevm_state_cancel();
322 if (s
->state
== MIG_STATE_CANCELLING
) {
323 migrate_set_state(s
, MIG_STATE_CANCELLING
, MIG_STATE_CANCELLED
);
327 notifier_list_notify(&migration_state_notifiers
, s
);
330 void migrate_fd_error(MigrationState
*s
)
332 DPRINTF("setting error state\n");
333 assert(s
->file
== NULL
);
334 s
->state
= MIG_STATE_ERROR
;
335 trace_migrate_set_state(MIG_STATE_ERROR
);
336 notifier_list_notify(&migration_state_notifiers
, s
);
339 static void migrate_fd_cancel(MigrationState
*s
)
342 DPRINTF("cancelling migration\n");
345 old_state
= s
->state
;
346 if (old_state
!= MIG_STATE_SETUP
&& old_state
!= MIG_STATE_ACTIVE
) {
349 migrate_set_state(s
, old_state
, MIG_STATE_CANCELLING
);
350 } while (s
->state
!= MIG_STATE_CANCELLING
);
353 void add_migration_state_change_notifier(Notifier
*notify
)
355 notifier_list_add(&migration_state_notifiers
, notify
);
358 void remove_migration_state_change_notifier(Notifier
*notify
)
360 notifier_remove(notify
);
363 bool migration_in_setup(MigrationState
*s
)
365 return s
->state
== MIG_STATE_SETUP
;
368 bool migration_has_finished(MigrationState
*s
)
370 return s
->state
== MIG_STATE_COMPLETED
;
373 bool migration_has_failed(MigrationState
*s
)
375 return (s
->state
== MIG_STATE_CANCELLED
||
376 s
->state
== MIG_STATE_ERROR
);
379 static MigrationState
*migrate_init(const MigrationParams
*params
)
381 MigrationState
*s
= migrate_get_current();
382 int64_t bandwidth_limit
= s
->bandwidth_limit
;
383 bool enabled_capabilities
[MIGRATION_CAPABILITY_MAX
];
384 int64_t xbzrle_cache_size
= s
->xbzrle_cache_size
;
386 memcpy(enabled_capabilities
, s
->enabled_capabilities
,
387 sizeof(enabled_capabilities
));
389 memset(s
, 0, sizeof(*s
));
391 memcpy(s
->enabled_capabilities
, enabled_capabilities
,
392 sizeof(enabled_capabilities
));
393 s
->xbzrle_cache_size
= xbzrle_cache_size
;
395 s
->bandwidth_limit
= bandwidth_limit
;
396 s
->state
= MIG_STATE_SETUP
;
397 trace_migrate_set_state(MIG_STATE_SETUP
);
399 s
->total_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
403 static GSList
*migration_blockers
;
405 void migrate_add_blocker(Error
*reason
)
407 migration_blockers
= g_slist_prepend(migration_blockers
, reason
);
410 void migrate_del_blocker(Error
*reason
)
412 migration_blockers
= g_slist_remove(migration_blockers
, reason
);
415 void qmp_migrate(const char *uri
, bool has_blk
, bool blk
,
416 bool has_inc
, bool inc
, bool has_detach
, bool detach
,
419 Error
*local_err
= NULL
;
420 MigrationState
*s
= migrate_get_current();
421 MigrationParams params
;
424 params
.blk
= has_blk
&& blk
;
425 params
.shared
= has_inc
&& inc
;
427 if (s
->state
== MIG_STATE_ACTIVE
|| s
->state
== MIG_STATE_SETUP
||
428 s
->state
== MIG_STATE_CANCELLING
) {
429 error_set(errp
, QERR_MIGRATION_ACTIVE
);
433 if (qemu_savevm_state_blocked(errp
)) {
437 if (migration_blockers
) {
438 *errp
= error_copy(migration_blockers
->data
);
442 s
= migrate_init(¶ms
);
444 if (strstart(uri
, "tcp:", &p
)) {
445 tcp_start_outgoing_migration(s
, p
, &local_err
);
447 } else if (strstart(uri
, "rdma:", &p
)) {
448 rdma_start_outgoing_migration(s
, p
, &local_err
);
451 } else if (strstart(uri
, "exec:", &p
)) {
452 exec_start_outgoing_migration(s
, p
, &local_err
);
453 } else if (strstart(uri
, "unix:", &p
)) {
454 unix_start_outgoing_migration(s
, p
, &local_err
);
455 } else if (strstart(uri
, "fd:", &p
)) {
456 fd_start_outgoing_migration(s
, p
, &local_err
);
459 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "uri", "a valid migration protocol");
460 s
->state
= MIG_STATE_ERROR
;
466 error_propagate(errp
, local_err
);
471 void qmp_migrate_cancel(Error
**errp
)
473 migrate_fd_cancel(migrate_get_current());
476 void qmp_migrate_set_cache_size(int64_t value
, Error
**errp
)
478 MigrationState
*s
= migrate_get_current();
481 /* Check for truncation */
482 if (value
!= (size_t)value
) {
483 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "cache size",
484 "exceeding address space");
488 /* Cache should not be larger than guest ram size */
489 if (value
> ram_bytes_total()) {
490 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "cache size",
491 "exceeds guest ram size ");
495 new_size
= xbzrle_cache_resize(value
);
497 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "cache size",
498 "is smaller than page size");
502 s
->xbzrle_cache_size
= new_size
;
505 int64_t qmp_query_migrate_cache_size(Error
**errp
)
507 return migrate_xbzrle_cache_size();
510 void qmp_migrate_set_speed(int64_t value
, Error
**errp
)
517 if (value
> SIZE_MAX
) {
521 s
= migrate_get_current();
522 s
->bandwidth_limit
= value
;
524 qemu_file_set_rate_limit(s
->file
, s
->bandwidth_limit
/ XFER_LIMIT_RATIO
);
528 void qmp_migrate_set_downtime(double value
, Error
**errp
)
531 value
= MAX(0, MIN(UINT64_MAX
, value
));
532 max_downtime
= (uint64_t)value
;
535 bool migrate_rdma_pin_all(void)
539 s
= migrate_get_current();
541 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_RDMA_PIN_ALL
];
544 bool migrate_auto_converge(void)
548 s
= migrate_get_current();
550 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_AUTO_CONVERGE
];
553 bool migrate_zero_blocks(void)
557 s
= migrate_get_current();
559 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_ZERO_BLOCKS
];
562 int migrate_use_xbzrle(void)
566 s
= migrate_get_current();
568 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_XBZRLE
];
571 int64_t migrate_xbzrle_cache_size(void)
575 s
= migrate_get_current();
577 return s
->xbzrle_cache_size
;
580 /* migration thread support */
582 static void *migration_thread(void *opaque
)
584 MigrationState
*s
= opaque
;
585 int64_t initial_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
586 int64_t setup_start
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
587 int64_t initial_bytes
= 0;
588 int64_t max_size
= 0;
589 int64_t start_time
= initial_time
;
590 bool old_vm_running
= false;
592 DPRINTF("beginning savevm\n");
593 qemu_savevm_state_begin(s
->file
, &s
->params
);
595 s
->setup_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
) - setup_start
;
596 migrate_set_state(s
, MIG_STATE_SETUP
, MIG_STATE_ACTIVE
);
598 DPRINTF("setup complete\n");
600 while (s
->state
== MIG_STATE_ACTIVE
) {
601 int64_t current_time
;
602 uint64_t pending_size
;
604 if (!qemu_file_rate_limit(s
->file
)) {
605 DPRINTF("iterate\n");
606 pending_size
= qemu_savevm_state_pending(s
->file
, max_size
);
607 DPRINTF("pending size %" PRIu64
" max %" PRIu64
"\n",
608 pending_size
, max_size
);
609 if (pending_size
&& pending_size
>= max_size
) {
610 qemu_savevm_state_iterate(s
->file
);
614 DPRINTF("done iterating\n");
615 qemu_mutex_lock_iothread();
616 start_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
617 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
618 old_vm_running
= runstate_is_running();
620 ret
= vm_stop_force_state(RUN_STATE_FINISH_MIGRATE
);
622 qemu_file_set_rate_limit(s
->file
, INT64_MAX
);
623 qemu_savevm_state_complete(s
->file
);
625 qemu_mutex_unlock_iothread();
628 migrate_set_state(s
, MIG_STATE_ACTIVE
, MIG_STATE_ERROR
);
632 if (!qemu_file_get_error(s
->file
)) {
633 migrate_set_state(s
, MIG_STATE_ACTIVE
, MIG_STATE_COMPLETED
);
639 if (qemu_file_get_error(s
->file
)) {
640 migrate_set_state(s
, MIG_STATE_ACTIVE
, MIG_STATE_ERROR
);
643 current_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
644 if (current_time
>= initial_time
+ BUFFER_DELAY
) {
645 uint64_t transferred_bytes
= qemu_ftell(s
->file
) - initial_bytes
;
646 uint64_t time_spent
= current_time
- initial_time
;
647 double bandwidth
= transferred_bytes
/ time_spent
;
648 max_size
= bandwidth
* migrate_max_downtime() / 1000000;
650 s
->mbps
= time_spent
? (((double) transferred_bytes
* 8.0) /
651 ((double) time_spent
/ 1000.0)) / 1000.0 / 1000.0 : -1;
653 DPRINTF("transferred %" PRIu64
" time_spent %" PRIu64
654 " bandwidth %g max_size %" PRId64
"\n",
655 transferred_bytes
, time_spent
, bandwidth
, max_size
);
656 /* if we haven't sent anything, we don't want to recalculate
657 10000 is a small enough number for our purposes */
658 if (s
->dirty_bytes_rate
&& transferred_bytes
> 10000) {
659 s
->expected_downtime
= s
->dirty_bytes_rate
/ bandwidth
;
662 qemu_file_reset_rate_limit(s
->file
);
663 initial_time
= current_time
;
664 initial_bytes
= qemu_ftell(s
->file
);
666 if (qemu_file_rate_limit(s
->file
)) {
667 /* usleep expects microseconds */
668 g_usleep((initial_time
+ BUFFER_DELAY
- current_time
)*1000);
672 qemu_mutex_lock_iothread();
673 if (s
->state
== MIG_STATE_COMPLETED
) {
674 int64_t end_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
675 s
->total_time
= end_time
- s
->total_time
;
676 s
->downtime
= end_time
- start_time
;
677 runstate_set(RUN_STATE_POSTMIGRATE
);
679 if (old_vm_running
) {
683 qemu_bh_schedule(s
->cleanup_bh
);
684 qemu_mutex_unlock_iothread();
689 void migrate_fd_connect(MigrationState
*s
)
691 s
->state
= MIG_STATE_SETUP
;
692 trace_migrate_set_state(MIG_STATE_SETUP
);
694 /* This is a best 1st approximation. ns to ms */
695 s
->expected_downtime
= max_downtime
/1000000;
696 s
->cleanup_bh
= qemu_bh_new(migrate_fd_cleanup
, s
);
698 qemu_file_set_rate_limit(s
->file
,
699 s
->bandwidth_limit
/ XFER_LIMIT_RATIO
);
701 /* Notify before starting migration thread */
702 notifier_list_notify(&migration_state_notifiers
, s
);
704 qemu_thread_create(&s
->thread
, "migration", migration_thread
, s
,
705 QEMU_THREAD_JOINABLE
);