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, ...) \
48 #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
50 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
52 #define BUFFER_DELAY 100
53 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
55 /* Migration XBZRLE default cache size */
56 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
58 static NotifierList migration_state_notifiers
=
59 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers
);
61 /* When we add fault tolerance, we could have several
62 migrations at once. For now we don't need to add
63 dynamic creation of migration */
65 MigrationState
*migrate_get_current(void)
67 static MigrationState current_migration
= {
68 .state
= MIG_STATE_NONE
,
69 .bandwidth_limit
= MAX_THROTTLE
,
70 .xbzrle_cache_size
= DEFAULT_MIGRATE_CACHE_SIZE
,
74 return ¤t_migration
;
77 void qemu_start_incoming_migration(const char *uri
, Error
**errp
)
81 if (strstart(uri
, "tcp:", &p
))
82 tcp_start_incoming_migration(p
, errp
);
84 else if (strstart(uri
, "x-rdma:", &p
))
85 rdma_start_incoming_migration(p
, errp
);
88 else if (strstart(uri
, "exec:", &p
))
89 exec_start_incoming_migration(p
, errp
);
90 else if (strstart(uri
, "unix:", &p
))
91 unix_start_incoming_migration(p
, errp
);
92 else if (strstart(uri
, "fd:", &p
))
93 fd_start_incoming_migration(p
, errp
);
96 error_setg(errp
, "unknown migration protocol: %s", uri
);
100 static void process_incoming_migration_co(void *opaque
)
102 QEMUFile
*f
= opaque
;
105 ret
= qemu_loadvm_state(f
);
108 fprintf(stderr
, "load of migration failed\n");
111 qemu_announce_self();
112 DPRINTF("successfully loaded vm state\n");
114 bdrv_clear_incoming_migration_all();
115 /* Make sure all file formats flush their mutable metadata */
116 bdrv_invalidate_cache_all();
121 runstate_set(RUN_STATE_PAUSED
);
125 void process_incoming_migration(QEMUFile
*f
)
127 Coroutine
*co
= qemu_coroutine_create(process_incoming_migration_co
);
128 int fd
= qemu_get_fd(f
);
131 qemu_set_nonblock(fd
);
132 qemu_coroutine_enter(co
, f
);
135 /* amount of nanoseconds we are willing to wait for migration to be down.
136 * the choice of nanoseconds is because it is the maximum resolution that
137 * get_clock() can achieve. It is an internal measure. All user-visible
138 * units must be in seconds */
139 static uint64_t max_downtime
= 30000000;
141 uint64_t migrate_max_downtime(void)
146 MigrationCapabilityStatusList
*qmp_query_migrate_capabilities(Error
**errp
)
148 MigrationCapabilityStatusList
*head
= NULL
;
149 MigrationCapabilityStatusList
*caps
;
150 MigrationState
*s
= migrate_get_current();
153 caps
= NULL
; /* silence compiler warning */
154 for (i
= 0; i
< MIGRATION_CAPABILITY_MAX
; i
++) {
156 head
= g_malloc0(sizeof(*caps
));
159 caps
->next
= g_malloc0(sizeof(*caps
));
163 g_malloc(sizeof(*caps
->value
));
164 caps
->value
->capability
= i
;
165 caps
->value
->state
= s
->enabled_capabilities
[i
];
171 static void get_xbzrle_cache_stats(MigrationInfo
*info
)
173 if (migrate_use_xbzrle()) {
174 info
->has_xbzrle_cache
= true;
175 info
->xbzrle_cache
= g_malloc0(sizeof(*info
->xbzrle_cache
));
176 info
->xbzrle_cache
->cache_size
= migrate_xbzrle_cache_size();
177 info
->xbzrle_cache
->bytes
= xbzrle_mig_bytes_transferred();
178 info
->xbzrle_cache
->pages
= xbzrle_mig_pages_transferred();
179 info
->xbzrle_cache
->cache_miss
= xbzrle_mig_pages_cache_miss();
180 info
->xbzrle_cache
->overflow
= xbzrle_mig_pages_overflow();
184 MigrationInfo
*qmp_query_migrate(Error
**errp
)
186 MigrationInfo
*info
= g_malloc0(sizeof(*info
));
187 MigrationState
*s
= migrate_get_current();
191 /* no migration has happened ever */
193 case MIG_STATE_SETUP
:
194 info
->has_status
= true;
195 info
->status
= g_strdup("setup");
196 info
->has_total_time
= false;
198 case MIG_STATE_ACTIVE
:
199 info
->has_status
= true;
200 info
->status
= g_strdup("active");
201 info
->has_total_time
= true;
202 info
->total_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
)
204 info
->has_expected_downtime
= true;
205 info
->expected_downtime
= s
->expected_downtime
;
206 info
->has_setup_time
= true;
207 info
->setup_time
= s
->setup_time
;
209 info
->has_ram
= true;
210 info
->ram
= g_malloc0(sizeof(*info
->ram
));
211 info
->ram
->transferred
= ram_bytes_transferred();
212 info
->ram
->remaining
= ram_bytes_remaining();
213 info
->ram
->total
= ram_bytes_total();
214 info
->ram
->duplicate
= dup_mig_pages_transferred();
215 info
->ram
->skipped
= skipped_mig_pages_transferred();
216 info
->ram
->normal
= norm_mig_pages_transferred();
217 info
->ram
->normal_bytes
= norm_mig_bytes_transferred();
218 info
->ram
->dirty_pages_rate
= s
->dirty_pages_rate
;
219 info
->ram
->mbps
= s
->mbps
;
221 if (blk_mig_active()) {
222 info
->has_disk
= true;
223 info
->disk
= g_malloc0(sizeof(*info
->disk
));
224 info
->disk
->transferred
= blk_mig_bytes_transferred();
225 info
->disk
->remaining
= blk_mig_bytes_remaining();
226 info
->disk
->total
= blk_mig_bytes_total();
229 get_xbzrle_cache_stats(info
);
231 case MIG_STATE_COMPLETED
:
232 get_xbzrle_cache_stats(info
);
234 info
->has_status
= true;
235 info
->status
= g_strdup("completed");
236 info
->has_total_time
= true;
237 info
->total_time
= s
->total_time
;
238 info
->has_downtime
= true;
239 info
->downtime
= s
->downtime
;
240 info
->has_setup_time
= true;
241 info
->setup_time
= s
->setup_time
;
243 info
->has_ram
= true;
244 info
->ram
= g_malloc0(sizeof(*info
->ram
));
245 info
->ram
->transferred
= ram_bytes_transferred();
246 info
->ram
->remaining
= 0;
247 info
->ram
->total
= ram_bytes_total();
248 info
->ram
->duplicate
= dup_mig_pages_transferred();
249 info
->ram
->skipped
= skipped_mig_pages_transferred();
250 info
->ram
->normal
= norm_mig_pages_transferred();
251 info
->ram
->normal_bytes
= norm_mig_bytes_transferred();
252 info
->ram
->mbps
= s
->mbps
;
254 case MIG_STATE_ERROR
:
255 info
->has_status
= true;
256 info
->status
= g_strdup("failed");
258 case MIG_STATE_CANCELLED
:
259 info
->has_status
= true;
260 info
->status
= g_strdup("cancelled");
267 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList
*params
,
270 MigrationState
*s
= migrate_get_current();
271 MigrationCapabilityStatusList
*cap
;
273 if (s
->state
== MIG_STATE_ACTIVE
|| s
->state
== MIG_STATE_SETUP
) {
274 error_set(errp
, QERR_MIGRATION_ACTIVE
);
278 for (cap
= params
; cap
; cap
= cap
->next
) {
279 s
->enabled_capabilities
[cap
->value
->capability
] = cap
->value
->state
;
283 /* shared migration helpers */
285 static void migrate_fd_cleanup(void *opaque
)
287 MigrationState
*s
= opaque
;
289 qemu_bh_delete(s
->cleanup_bh
);
290 s
->cleanup_bh
= NULL
;
293 DPRINTF("closing file\n");
294 qemu_mutex_unlock_iothread();
295 qemu_thread_join(&s
->thread
);
296 qemu_mutex_lock_iothread();
298 qemu_fclose(s
->file
);
302 assert(s
->state
!= MIG_STATE_ACTIVE
);
304 if (s
->state
!= MIG_STATE_COMPLETED
) {
305 qemu_savevm_state_cancel();
308 notifier_list_notify(&migration_state_notifiers
, s
);
311 static void migrate_set_state(MigrationState
*s
, int old_state
, int new_state
)
313 if (atomic_cmpxchg(&s
->state
, old_state
, new_state
) == new_state
) {
314 trace_migrate_set_state(new_state
);
318 void migrate_fd_error(MigrationState
*s
)
320 DPRINTF("setting error state\n");
321 assert(s
->file
== NULL
);
322 s
->state
= MIG_STATE_ERROR
;
323 trace_migrate_set_state(MIG_STATE_ERROR
);
324 notifier_list_notify(&migration_state_notifiers
, s
);
327 static void migrate_fd_cancel(MigrationState
*s
)
329 DPRINTF("cancelling migration\n");
331 migrate_set_state(s
, s
->state
, MIG_STATE_CANCELLED
);
334 void add_migration_state_change_notifier(Notifier
*notify
)
336 notifier_list_add(&migration_state_notifiers
, notify
);
339 void remove_migration_state_change_notifier(Notifier
*notify
)
341 notifier_remove(notify
);
344 bool migration_in_setup(MigrationState
*s
)
346 return s
->state
== MIG_STATE_SETUP
;
349 bool migration_has_finished(MigrationState
*s
)
351 return s
->state
== MIG_STATE_COMPLETED
;
354 bool migration_has_failed(MigrationState
*s
)
356 return (s
->state
== MIG_STATE_CANCELLED
||
357 s
->state
== MIG_STATE_ERROR
);
360 static MigrationState
*migrate_init(const MigrationParams
*params
)
362 MigrationState
*s
= migrate_get_current();
363 int64_t bandwidth_limit
= s
->bandwidth_limit
;
364 bool enabled_capabilities
[MIGRATION_CAPABILITY_MAX
];
365 int64_t xbzrle_cache_size
= s
->xbzrle_cache_size
;
367 memcpy(enabled_capabilities
, s
->enabled_capabilities
,
368 sizeof(enabled_capabilities
));
370 memset(s
, 0, sizeof(*s
));
372 memcpy(s
->enabled_capabilities
, enabled_capabilities
,
373 sizeof(enabled_capabilities
));
374 s
->xbzrle_cache_size
= xbzrle_cache_size
;
376 s
->bandwidth_limit
= bandwidth_limit
;
377 s
->state
= MIG_STATE_SETUP
;
378 trace_migrate_set_state(MIG_STATE_SETUP
);
380 s
->total_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
384 static GSList
*migration_blockers
;
386 void migrate_add_blocker(Error
*reason
)
388 migration_blockers
= g_slist_prepend(migration_blockers
, reason
);
391 void migrate_del_blocker(Error
*reason
)
393 migration_blockers
= g_slist_remove(migration_blockers
, reason
);
396 void qmp_migrate(const char *uri
, bool has_blk
, bool blk
,
397 bool has_inc
, bool inc
, bool has_detach
, bool detach
,
400 Error
*local_err
= NULL
;
401 MigrationState
*s
= migrate_get_current();
402 MigrationParams params
;
405 params
.blk
= has_blk
&& blk
;
406 params
.shared
= has_inc
&& inc
;
408 if (s
->state
== MIG_STATE_ACTIVE
|| s
->state
== MIG_STATE_SETUP
) {
409 error_set(errp
, QERR_MIGRATION_ACTIVE
);
413 if (qemu_savevm_state_blocked(errp
)) {
417 if (migration_blockers
) {
418 *errp
= error_copy(migration_blockers
->data
);
422 s
= migrate_init(¶ms
);
424 if (strstart(uri
, "tcp:", &p
)) {
425 tcp_start_outgoing_migration(s
, p
, &local_err
);
427 } else if (strstart(uri
, "x-rdma:", &p
)) {
428 rdma_start_outgoing_migration(s
, p
, &local_err
);
431 } else if (strstart(uri
, "exec:", &p
)) {
432 exec_start_outgoing_migration(s
, p
, &local_err
);
433 } else if (strstart(uri
, "unix:", &p
)) {
434 unix_start_outgoing_migration(s
, p
, &local_err
);
435 } else if (strstart(uri
, "fd:", &p
)) {
436 fd_start_outgoing_migration(s
, p
, &local_err
);
439 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "uri", "a valid migration protocol");
445 error_propagate(errp
, local_err
);
450 void qmp_migrate_cancel(Error
**errp
)
452 migrate_fd_cancel(migrate_get_current());
455 void qmp_migrate_set_cache_size(int64_t value
, Error
**errp
)
457 MigrationState
*s
= migrate_get_current();
459 /* Check for truncation */
460 if (value
!= (size_t)value
) {
461 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "cache size",
462 "exceeding address space");
466 s
->xbzrle_cache_size
= xbzrle_cache_resize(value
);
469 int64_t qmp_query_migrate_cache_size(Error
**errp
)
471 return migrate_xbzrle_cache_size();
474 void qmp_migrate_set_speed(int64_t value
, Error
**errp
)
481 if (value
> SIZE_MAX
) {
485 s
= migrate_get_current();
486 s
->bandwidth_limit
= value
;
488 qemu_file_set_rate_limit(s
->file
, s
->bandwidth_limit
/ XFER_LIMIT_RATIO
);
492 void qmp_migrate_set_downtime(double value
, Error
**errp
)
495 value
= MAX(0, MIN(UINT64_MAX
, value
));
496 max_downtime
= (uint64_t)value
;
499 bool migrate_rdma_pin_all(void)
503 s
= migrate_get_current();
505 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_X_RDMA_PIN_ALL
];
508 bool migrate_auto_converge(void)
512 s
= migrate_get_current();
514 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_AUTO_CONVERGE
];
517 bool migrate_zero_blocks(void)
521 s
= migrate_get_current();
523 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_ZERO_BLOCKS
];
526 int migrate_use_xbzrle(void)
530 s
= migrate_get_current();
532 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_XBZRLE
];
535 int64_t migrate_xbzrle_cache_size(void)
539 s
= migrate_get_current();
541 return s
->xbzrle_cache_size
;
544 /* migration thread support */
546 static void *migration_thread(void *opaque
)
548 MigrationState
*s
= opaque
;
549 int64_t initial_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
550 int64_t setup_start
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
551 int64_t initial_bytes
= 0;
552 int64_t max_size
= 0;
553 int64_t start_time
= initial_time
;
554 bool old_vm_running
= false;
556 DPRINTF("beginning savevm\n");
557 qemu_savevm_state_begin(s
->file
, &s
->params
);
559 s
->setup_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
) - setup_start
;
560 migrate_set_state(s
, MIG_STATE_SETUP
, MIG_STATE_ACTIVE
);
562 DPRINTF("setup complete\n");
564 while (s
->state
== MIG_STATE_ACTIVE
) {
565 int64_t current_time
;
566 uint64_t pending_size
;
568 if (!qemu_file_rate_limit(s
->file
)) {
569 DPRINTF("iterate\n");
570 pending_size
= qemu_savevm_state_pending(s
->file
, max_size
);
571 DPRINTF("pending size %" PRIu64
" max %" PRIu64
"\n",
572 pending_size
, max_size
);
573 if (pending_size
&& pending_size
>= max_size
) {
574 qemu_savevm_state_iterate(s
->file
);
578 DPRINTF("done iterating\n");
579 qemu_mutex_lock_iothread();
580 start_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
581 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
582 old_vm_running
= runstate_is_running();
584 ret
= vm_stop_force_state(RUN_STATE_FINISH_MIGRATE
);
586 qemu_file_set_rate_limit(s
->file
, INT_MAX
);
587 qemu_savevm_state_complete(s
->file
);
589 qemu_mutex_unlock_iothread();
592 migrate_set_state(s
, MIG_STATE_ACTIVE
, MIG_STATE_ERROR
);
596 if (!qemu_file_get_error(s
->file
)) {
597 migrate_set_state(s
, MIG_STATE_ACTIVE
, MIG_STATE_COMPLETED
);
603 if (qemu_file_get_error(s
->file
)) {
604 migrate_set_state(s
, MIG_STATE_ACTIVE
, MIG_STATE_ERROR
);
607 current_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
608 if (current_time
>= initial_time
+ BUFFER_DELAY
) {
609 uint64_t transferred_bytes
= qemu_ftell(s
->file
) - initial_bytes
;
610 uint64_t time_spent
= current_time
- initial_time
;
611 double bandwidth
= transferred_bytes
/ time_spent
;
612 max_size
= bandwidth
* migrate_max_downtime() / 1000000;
614 s
->mbps
= time_spent
? (((double) transferred_bytes
* 8.0) /
615 ((double) time_spent
/ 1000.0)) / 1000.0 / 1000.0 : -1;
617 DPRINTF("transferred %" PRIu64
" time_spent %" PRIu64
618 " bandwidth %g max_size %" PRId64
"\n",
619 transferred_bytes
, time_spent
, bandwidth
, max_size
);
620 /* if we haven't sent anything, we don't want to recalculate
621 10000 is a small enough number for our purposes */
622 if (s
->dirty_bytes_rate
&& transferred_bytes
> 10000) {
623 s
->expected_downtime
= s
->dirty_bytes_rate
/ bandwidth
;
626 qemu_file_reset_rate_limit(s
->file
);
627 initial_time
= current_time
;
628 initial_bytes
= qemu_ftell(s
->file
);
630 if (qemu_file_rate_limit(s
->file
)) {
631 /* usleep expects microseconds */
632 g_usleep((initial_time
+ BUFFER_DELAY
- current_time
)*1000);
636 qemu_mutex_lock_iothread();
637 if (s
->state
== MIG_STATE_COMPLETED
) {
638 int64_t end_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
639 s
->total_time
= end_time
- s
->total_time
;
640 s
->downtime
= end_time
- start_time
;
641 runstate_set(RUN_STATE_POSTMIGRATE
);
643 if (old_vm_running
) {
647 qemu_bh_schedule(s
->cleanup_bh
);
648 qemu_mutex_unlock_iothread();
653 void migrate_fd_connect(MigrationState
*s
)
655 s
->state
= MIG_STATE_SETUP
;
656 trace_migrate_set_state(MIG_STATE_SETUP
);
658 /* This is a best 1st approximation. ns to ms */
659 s
->expected_downtime
= max_downtime
/1000000;
660 s
->cleanup_bh
= qemu_bh_new(migrate_fd_cleanup
, s
);
662 qemu_file_set_rate_limit(s
->file
,
663 s
->bandwidth_limit
/ XFER_LIMIT_RATIO
);
665 /* Notify before starting migration thread */
666 notifier_list_notify(&migration_state_notifiers
, s
);
668 qemu_thread_create(&s
->thread
, migration_thread
, s
,
669 QEMU_THREAD_JOINABLE
);