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 "migration/migration.h"
18 #include "monitor/monitor.h"
19 #include "migration/qemu-file.h"
20 #include "sysemu/sysemu.h"
21 #include "block/block.h"
22 #include "qemu/sockets.h"
23 #include "migration/block.h"
24 #include "qemu/thread.h"
25 #include "qmp-commands.h"
28 //#define DEBUG_MIGRATION
30 #ifdef DEBUG_MIGRATION
31 #define DPRINTF(fmt, ...) \
32 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
34 #define DPRINTF(fmt, ...) \
47 #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
49 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
51 #define BUFFER_DELAY 100
52 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
54 /* Migration XBZRLE default cache size */
55 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
57 static NotifierList migration_state_notifiers
=
58 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers
);
60 /* When we add fault tolerance, we could have several
61 migrations at once. For now we don't need to add
62 dynamic creation of migration */
64 MigrationState
*migrate_get_current(void)
66 static MigrationState current_migration
= {
67 .state
= MIG_STATE_NONE
,
68 .bandwidth_limit
= MAX_THROTTLE
,
69 .xbzrle_cache_size
= DEFAULT_MIGRATE_CACHE_SIZE
,
73 return ¤t_migration
;
76 void qemu_start_incoming_migration(const char *uri
, Error
**errp
)
80 if (strstart(uri
, "tcp:", &p
))
81 tcp_start_incoming_migration(p
, errp
);
83 else if (strstart(uri
, "x-rdma:", &p
))
84 rdma_start_incoming_migration(p
, errp
);
87 else if (strstart(uri
, "exec:", &p
))
88 exec_start_incoming_migration(p
, errp
);
89 else if (strstart(uri
, "unix:", &p
))
90 unix_start_incoming_migration(p
, errp
);
91 else if (strstart(uri
, "fd:", &p
))
92 fd_start_incoming_migration(p
, errp
);
95 error_setg(errp
, "unknown migration protocol: %s", uri
);
99 static void process_incoming_migration_co(void *opaque
)
101 QEMUFile
*f
= opaque
;
104 ret
= qemu_loadvm_state(f
);
107 fprintf(stderr
, "load of migration failed\n");
110 qemu_announce_self();
111 DPRINTF("successfully loaded vm state\n");
113 bdrv_clear_incoming_migration_all();
114 /* Make sure all file formats flush their mutable metadata */
115 bdrv_invalidate_cache_all();
120 runstate_set(RUN_STATE_PAUSED
);
124 void process_incoming_migration(QEMUFile
*f
)
126 Coroutine
*co
= qemu_coroutine_create(process_incoming_migration_co
);
127 int fd
= qemu_get_fd(f
);
130 qemu_set_nonblock(fd
);
131 qemu_coroutine_enter(co
, f
);
134 /* amount of nanoseconds we are willing to wait for migration to be down.
135 * the choice of nanoseconds is because it is the maximum resolution that
136 * get_clock() can achieve. It is an internal measure. All user-visible
137 * units must be in seconds */
138 static uint64_t max_downtime
= 30000000;
140 uint64_t migrate_max_downtime(void)
145 MigrationCapabilityStatusList
*qmp_query_migrate_capabilities(Error
**errp
)
147 MigrationCapabilityStatusList
*head
= NULL
;
148 MigrationCapabilityStatusList
*caps
;
149 MigrationState
*s
= migrate_get_current();
152 for (i
= 0; i
< MIGRATION_CAPABILITY_MAX
; i
++) {
154 head
= g_malloc0(sizeof(*caps
));
157 caps
->next
= g_malloc0(sizeof(*caps
));
161 g_malloc(sizeof(*caps
->value
));
162 caps
->value
->capability
= i
;
163 caps
->value
->state
= s
->enabled_capabilities
[i
];
169 static void get_xbzrle_cache_stats(MigrationInfo
*info
)
171 if (migrate_use_xbzrle()) {
172 info
->has_xbzrle_cache
= true;
173 info
->xbzrle_cache
= g_malloc0(sizeof(*info
->xbzrle_cache
));
174 info
->xbzrle_cache
->cache_size
= migrate_xbzrle_cache_size();
175 info
->xbzrle_cache
->bytes
= xbzrle_mig_bytes_transferred();
176 info
->xbzrle_cache
->pages
= xbzrle_mig_pages_transferred();
177 info
->xbzrle_cache
->cache_miss
= xbzrle_mig_pages_cache_miss();
178 info
->xbzrle_cache
->overflow
= xbzrle_mig_pages_overflow();
182 MigrationInfo
*qmp_query_migrate(Error
**errp
)
184 MigrationInfo
*info
= g_malloc0(sizeof(*info
));
185 MigrationState
*s
= migrate_get_current();
189 /* no migration has happened ever */
191 case MIG_STATE_SETUP
:
192 info
->has_status
= true;
193 info
->status
= g_strdup("setup");
194 info
->has_total_time
= false;
196 case MIG_STATE_ACTIVE
:
197 info
->has_status
= true;
198 info
->status
= g_strdup("active");
199 info
->has_total_time
= true;
200 info
->total_time
= qemu_get_clock_ms(rt_clock
)
202 info
->has_expected_downtime
= true;
203 info
->expected_downtime
= s
->expected_downtime
;
204 info
->has_setup_time
= true;
205 info
->setup_time
= s
->setup_time
;
207 info
->has_ram
= true;
208 info
->ram
= g_malloc0(sizeof(*info
->ram
));
209 info
->ram
->transferred
= ram_bytes_transferred();
210 info
->ram
->remaining
= ram_bytes_remaining();
211 info
->ram
->total
= ram_bytes_total();
212 info
->ram
->duplicate
= dup_mig_pages_transferred();
213 info
->ram
->skipped
= skipped_mig_pages_transferred();
214 info
->ram
->normal
= norm_mig_pages_transferred();
215 info
->ram
->normal_bytes
= norm_mig_bytes_transferred();
216 info
->ram
->dirty_pages_rate
= s
->dirty_pages_rate
;
217 info
->ram
->mbps
= s
->mbps
;
219 if (blk_mig_active()) {
220 info
->has_disk
= true;
221 info
->disk
= g_malloc0(sizeof(*info
->disk
));
222 info
->disk
->transferred
= blk_mig_bytes_transferred();
223 info
->disk
->remaining
= blk_mig_bytes_remaining();
224 info
->disk
->total
= blk_mig_bytes_total();
227 get_xbzrle_cache_stats(info
);
229 case MIG_STATE_COMPLETED
:
230 get_xbzrle_cache_stats(info
);
232 info
->has_status
= true;
233 info
->status
= g_strdup("completed");
234 info
->has_total_time
= true;
235 info
->total_time
= s
->total_time
;
236 info
->has_downtime
= true;
237 info
->downtime
= s
->downtime
;
238 info
->has_setup_time
= true;
239 info
->setup_time
= s
->setup_time
;
241 info
->has_ram
= true;
242 info
->ram
= g_malloc0(sizeof(*info
->ram
));
243 info
->ram
->transferred
= ram_bytes_transferred();
244 info
->ram
->remaining
= 0;
245 info
->ram
->total
= ram_bytes_total();
246 info
->ram
->duplicate
= dup_mig_pages_transferred();
247 info
->ram
->skipped
= skipped_mig_pages_transferred();
248 info
->ram
->normal
= norm_mig_pages_transferred();
249 info
->ram
->normal_bytes
= norm_mig_bytes_transferred();
250 info
->ram
->mbps
= s
->mbps
;
252 case MIG_STATE_ERROR
:
253 info
->has_status
= true;
254 info
->status
= g_strdup("failed");
256 case MIG_STATE_CANCELLED
:
257 info
->has_status
= true;
258 info
->status
= g_strdup("cancelled");
265 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList
*params
,
268 MigrationState
*s
= migrate_get_current();
269 MigrationCapabilityStatusList
*cap
;
271 if (s
->state
== MIG_STATE_ACTIVE
|| s
->state
== MIG_STATE_SETUP
) {
272 error_set(errp
, QERR_MIGRATION_ACTIVE
);
276 for (cap
= params
; cap
; cap
= cap
->next
) {
277 s
->enabled_capabilities
[cap
->value
->capability
] = cap
->value
->state
;
281 /* shared migration helpers */
283 static void migrate_fd_cleanup(void *opaque
)
285 MigrationState
*s
= opaque
;
287 qemu_bh_delete(s
->cleanup_bh
);
288 s
->cleanup_bh
= NULL
;
291 DPRINTF("closing file\n");
292 qemu_mutex_unlock_iothread();
293 qemu_thread_join(&s
->thread
);
294 qemu_mutex_lock_iothread();
296 qemu_fclose(s
->file
);
300 assert(s
->state
!= MIG_STATE_ACTIVE
);
302 if (s
->state
!= MIG_STATE_COMPLETED
) {
303 qemu_savevm_state_cancel();
306 notifier_list_notify(&migration_state_notifiers
, s
);
309 static void migrate_set_state(MigrationState
*s
, int old_state
, int new_state
)
311 if (atomic_cmpxchg(&s
->state
, old_state
, new_state
) == new_state
) {
312 trace_migrate_set_state(new_state
);
316 void migrate_fd_error(MigrationState
*s
)
318 DPRINTF("setting error state\n");
319 assert(s
->file
== NULL
);
320 s
->state
= MIG_STATE_ERROR
;
321 trace_migrate_set_state(MIG_STATE_ERROR
);
322 notifier_list_notify(&migration_state_notifiers
, s
);
325 static void migrate_fd_cancel(MigrationState
*s
)
327 DPRINTF("cancelling migration\n");
329 migrate_set_state(s
, s
->state
, MIG_STATE_CANCELLED
);
332 void add_migration_state_change_notifier(Notifier
*notify
)
334 notifier_list_add(&migration_state_notifiers
, notify
);
337 void remove_migration_state_change_notifier(Notifier
*notify
)
339 notifier_remove(notify
);
342 bool migration_in_setup(MigrationState
*s
)
344 return s
->state
== MIG_STATE_SETUP
;
347 bool migration_has_finished(MigrationState
*s
)
349 return s
->state
== MIG_STATE_COMPLETED
;
352 bool migration_has_failed(MigrationState
*s
)
354 return (s
->state
== MIG_STATE_CANCELLED
||
355 s
->state
== MIG_STATE_ERROR
);
358 static MigrationState
*migrate_init(const MigrationParams
*params
)
360 MigrationState
*s
= migrate_get_current();
361 int64_t bandwidth_limit
= s
->bandwidth_limit
;
362 bool enabled_capabilities
[MIGRATION_CAPABILITY_MAX
];
363 int64_t xbzrle_cache_size
= s
->xbzrle_cache_size
;
365 memcpy(enabled_capabilities
, s
->enabled_capabilities
,
366 sizeof(enabled_capabilities
));
368 memset(s
, 0, sizeof(*s
));
370 memcpy(s
->enabled_capabilities
, enabled_capabilities
,
371 sizeof(enabled_capabilities
));
372 s
->xbzrle_cache_size
= xbzrle_cache_size
;
374 s
->bandwidth_limit
= bandwidth_limit
;
375 s
->state
= MIG_STATE_SETUP
;
376 trace_migrate_set_state(MIG_STATE_SETUP
);
378 s
->total_time
= qemu_get_clock_ms(rt_clock
);
382 static GSList
*migration_blockers
;
384 void migrate_add_blocker(Error
*reason
)
386 migration_blockers
= g_slist_prepend(migration_blockers
, reason
);
389 void migrate_del_blocker(Error
*reason
)
391 migration_blockers
= g_slist_remove(migration_blockers
, reason
);
394 void qmp_migrate(const char *uri
, bool has_blk
, bool blk
,
395 bool has_inc
, bool inc
, bool has_detach
, bool detach
,
398 Error
*local_err
= NULL
;
399 MigrationState
*s
= migrate_get_current();
400 MigrationParams params
;
403 params
.blk
= has_blk
&& blk
;
404 params
.shared
= has_inc
&& inc
;
406 if (s
->state
== MIG_STATE_ACTIVE
|| s
->state
== MIG_STATE_SETUP
) {
407 error_set(errp
, QERR_MIGRATION_ACTIVE
);
411 if (qemu_savevm_state_blocked(errp
)) {
415 if (migration_blockers
) {
416 *errp
= error_copy(migration_blockers
->data
);
420 s
= migrate_init(¶ms
);
422 if (strstart(uri
, "tcp:", &p
)) {
423 tcp_start_outgoing_migration(s
, p
, &local_err
);
425 } else if (strstart(uri
, "x-rdma:", &p
)) {
426 rdma_start_outgoing_migration(s
, p
, &local_err
);
429 } else if (strstart(uri
, "exec:", &p
)) {
430 exec_start_outgoing_migration(s
, p
, &local_err
);
431 } else if (strstart(uri
, "unix:", &p
)) {
432 unix_start_outgoing_migration(s
, p
, &local_err
);
433 } else if (strstart(uri
, "fd:", &p
)) {
434 fd_start_outgoing_migration(s
, p
, &local_err
);
437 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "uri", "a valid migration protocol");
443 error_propagate(errp
, local_err
);
448 void qmp_migrate_cancel(Error
**errp
)
450 migrate_fd_cancel(migrate_get_current());
453 void qmp_migrate_set_cache_size(int64_t value
, Error
**errp
)
455 MigrationState
*s
= migrate_get_current();
457 /* Check for truncation */
458 if (value
!= (size_t)value
) {
459 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "cache size",
460 "exceeding address space");
464 s
->xbzrle_cache_size
= xbzrle_cache_resize(value
);
467 int64_t qmp_query_migrate_cache_size(Error
**errp
)
469 return migrate_xbzrle_cache_size();
472 void qmp_migrate_set_speed(int64_t value
, Error
**errp
)
479 if (value
> SIZE_MAX
) {
483 s
= migrate_get_current();
484 s
->bandwidth_limit
= value
;
486 qemu_file_set_rate_limit(s
->file
, s
->bandwidth_limit
/ XFER_LIMIT_RATIO
);
490 void qmp_migrate_set_downtime(double value
, Error
**errp
)
493 value
= MAX(0, MIN(UINT64_MAX
, value
));
494 max_downtime
= (uint64_t)value
;
497 bool migrate_rdma_pin_all(void)
501 s
= migrate_get_current();
503 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_X_RDMA_PIN_ALL
];
506 bool migrate_auto_converge(void)
510 s
= migrate_get_current();
512 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_AUTO_CONVERGE
];
515 bool migrate_zero_blocks(void)
519 s
= migrate_get_current();
521 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_ZERO_BLOCKS
];
524 int migrate_use_xbzrle(void)
528 s
= migrate_get_current();
530 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_XBZRLE
];
533 int64_t migrate_xbzrle_cache_size(void)
537 s
= migrate_get_current();
539 return s
->xbzrle_cache_size
;
542 /* migration thread support */
544 static void *migration_thread(void *opaque
)
546 MigrationState
*s
= opaque
;
547 int64_t initial_time
= qemu_get_clock_ms(rt_clock
);
548 int64_t setup_start
= qemu_get_clock_ms(host_clock
);
549 int64_t initial_bytes
= 0;
550 int64_t max_size
= 0;
551 int64_t start_time
= initial_time
;
552 bool old_vm_running
= false;
554 DPRINTF("beginning savevm\n");
555 qemu_savevm_state_begin(s
->file
, &s
->params
);
557 s
->setup_time
= qemu_get_clock_ms(host_clock
) - setup_start
;
558 migrate_set_state(s
, MIG_STATE_SETUP
, MIG_STATE_ACTIVE
);
560 DPRINTF("setup complete\n");
562 while (s
->state
== MIG_STATE_ACTIVE
) {
563 int64_t current_time
;
564 uint64_t pending_size
;
566 if (!qemu_file_rate_limit(s
->file
)) {
567 DPRINTF("iterate\n");
568 pending_size
= qemu_savevm_state_pending(s
->file
, max_size
);
569 DPRINTF("pending size %lu max %lu\n", pending_size
, max_size
);
570 if (pending_size
&& pending_size
>= max_size
) {
571 qemu_savevm_state_iterate(s
->file
);
575 DPRINTF("done iterating\n");
576 qemu_mutex_lock_iothread();
577 start_time
= qemu_get_clock_ms(rt_clock
);
578 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
579 old_vm_running
= runstate_is_running();
581 ret
= vm_stop_force_state(RUN_STATE_FINISH_MIGRATE
);
583 qemu_file_set_rate_limit(s
->file
, INT_MAX
);
584 qemu_savevm_state_complete(s
->file
);
586 qemu_mutex_unlock_iothread();
589 migrate_set_state(s
, MIG_STATE_ACTIVE
, MIG_STATE_ERROR
);
593 if (!qemu_file_get_error(s
->file
)) {
594 migrate_set_state(s
, MIG_STATE_ACTIVE
, MIG_STATE_COMPLETED
);
600 if (qemu_file_get_error(s
->file
)) {
601 migrate_set_state(s
, MIG_STATE_ACTIVE
, MIG_STATE_ERROR
);
604 current_time
= qemu_get_clock_ms(rt_clock
);
605 if (current_time
>= initial_time
+ BUFFER_DELAY
) {
606 uint64_t transferred_bytes
= qemu_ftell(s
->file
) - initial_bytes
;
607 uint64_t time_spent
= current_time
- initial_time
;
608 double bandwidth
= transferred_bytes
/ time_spent
;
609 max_size
= bandwidth
* migrate_max_downtime() / 1000000;
611 s
->mbps
= time_spent
? (((double) transferred_bytes
* 8.0) /
612 ((double) time_spent
/ 1000.0)) / 1000.0 / 1000.0 : -1;
614 DPRINTF("transferred %" PRIu64
" time_spent %" PRIu64
615 " bandwidth %g max_size %" PRId64
"\n",
616 transferred_bytes
, time_spent
, bandwidth
, max_size
);
617 /* if we haven't sent anything, we don't want to recalculate
618 10000 is a small enough number for our purposes */
619 if (s
->dirty_bytes_rate
&& transferred_bytes
> 10000) {
620 s
->expected_downtime
= s
->dirty_bytes_rate
/ bandwidth
;
623 qemu_file_reset_rate_limit(s
->file
);
624 initial_time
= current_time
;
625 initial_bytes
= qemu_ftell(s
->file
);
627 if (qemu_file_rate_limit(s
->file
)) {
628 /* usleep expects microseconds */
629 g_usleep((initial_time
+ BUFFER_DELAY
- current_time
)*1000);
633 qemu_mutex_lock_iothread();
634 if (s
->state
== MIG_STATE_COMPLETED
) {
635 int64_t end_time
= qemu_get_clock_ms(rt_clock
);
636 s
->total_time
= end_time
- s
->total_time
;
637 s
->downtime
= end_time
- start_time
;
638 runstate_set(RUN_STATE_POSTMIGRATE
);
640 if (old_vm_running
) {
644 qemu_bh_schedule(s
->cleanup_bh
);
645 qemu_mutex_unlock_iothread();
650 void migrate_fd_connect(MigrationState
*s
)
652 s
->state
= MIG_STATE_SETUP
;
653 trace_migrate_set_state(MIG_STATE_SETUP
);
655 /* This is a best 1st approximation. ns to ms */
656 s
->expected_downtime
= max_downtime
/1000000;
657 s
->cleanup_bh
= qemu_bh_new(migrate_fd_cleanup
, s
);
659 qemu_file_set_rate_limit(s
->file
,
660 s
->bandwidth_limit
/ XFER_LIMIT_RATIO
);
662 /* Notify before starting migration thread */
663 notifier_list_notify(&migration_state_notifiers
, s
);
665 qemu_thread_create(&s
->thread
, migration_thread
, s
,
666 QEMU_THREAD_JOINABLE
);