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
)
330 DPRINTF("cancelling migration\n");
333 old_state
= s
->state
;
334 if (old_state
!= MIG_STATE_SETUP
&& old_state
!= MIG_STATE_ACTIVE
) {
337 migrate_set_state(s
, old_state
, MIG_STATE_CANCELLED
);
338 } while (s
->state
!= MIG_STATE_CANCELLED
);
341 void add_migration_state_change_notifier(Notifier
*notify
)
343 notifier_list_add(&migration_state_notifiers
, notify
);
346 void remove_migration_state_change_notifier(Notifier
*notify
)
348 notifier_remove(notify
);
351 bool migration_in_setup(MigrationState
*s
)
353 return s
->state
== MIG_STATE_SETUP
;
356 bool migration_has_finished(MigrationState
*s
)
358 return s
->state
== MIG_STATE_COMPLETED
;
361 bool migration_has_failed(MigrationState
*s
)
363 return (s
->state
== MIG_STATE_CANCELLED
||
364 s
->state
== MIG_STATE_ERROR
);
367 static MigrationState
*migrate_init(const MigrationParams
*params
)
369 MigrationState
*s
= migrate_get_current();
370 int64_t bandwidth_limit
= s
->bandwidth_limit
;
371 bool enabled_capabilities
[MIGRATION_CAPABILITY_MAX
];
372 int64_t xbzrle_cache_size
= s
->xbzrle_cache_size
;
374 memcpy(enabled_capabilities
, s
->enabled_capabilities
,
375 sizeof(enabled_capabilities
));
377 memset(s
, 0, sizeof(*s
));
379 memcpy(s
->enabled_capabilities
, enabled_capabilities
,
380 sizeof(enabled_capabilities
));
381 s
->xbzrle_cache_size
= xbzrle_cache_size
;
383 s
->bandwidth_limit
= bandwidth_limit
;
384 s
->state
= MIG_STATE_SETUP
;
385 trace_migrate_set_state(MIG_STATE_SETUP
);
387 s
->total_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
391 static GSList
*migration_blockers
;
393 void migrate_add_blocker(Error
*reason
)
395 migration_blockers
= g_slist_prepend(migration_blockers
, reason
);
398 void migrate_del_blocker(Error
*reason
)
400 migration_blockers
= g_slist_remove(migration_blockers
, reason
);
403 void qmp_migrate(const char *uri
, bool has_blk
, bool blk
,
404 bool has_inc
, bool inc
, bool has_detach
, bool detach
,
407 Error
*local_err
= NULL
;
408 MigrationState
*s
= migrate_get_current();
409 MigrationParams params
;
412 params
.blk
= has_blk
&& blk
;
413 params
.shared
= has_inc
&& inc
;
415 if (s
->state
== MIG_STATE_ACTIVE
|| s
->state
== MIG_STATE_SETUP
) {
416 error_set(errp
, QERR_MIGRATION_ACTIVE
);
420 if (qemu_savevm_state_blocked(errp
)) {
424 if (migration_blockers
) {
425 *errp
= error_copy(migration_blockers
->data
);
429 s
= migrate_init(¶ms
);
431 if (strstart(uri
, "tcp:", &p
)) {
432 tcp_start_outgoing_migration(s
, p
, &local_err
);
434 } else if (strstart(uri
, "x-rdma:", &p
)) {
435 rdma_start_outgoing_migration(s
, p
, &local_err
);
438 } else if (strstart(uri
, "exec:", &p
)) {
439 exec_start_outgoing_migration(s
, p
, &local_err
);
440 } else if (strstart(uri
, "unix:", &p
)) {
441 unix_start_outgoing_migration(s
, p
, &local_err
);
442 } else if (strstart(uri
, "fd:", &p
)) {
443 fd_start_outgoing_migration(s
, p
, &local_err
);
446 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "uri", "a valid migration protocol");
452 error_propagate(errp
, local_err
);
457 void qmp_migrate_cancel(Error
**errp
)
459 migrate_fd_cancel(migrate_get_current());
462 void qmp_migrate_set_cache_size(int64_t value
, Error
**errp
)
464 MigrationState
*s
= migrate_get_current();
466 /* Check for truncation */
467 if (value
!= (size_t)value
) {
468 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "cache size",
469 "exceeding address space");
473 s
->xbzrle_cache_size
= xbzrle_cache_resize(value
);
476 int64_t qmp_query_migrate_cache_size(Error
**errp
)
478 return migrate_xbzrle_cache_size();
481 void qmp_migrate_set_speed(int64_t value
, Error
**errp
)
488 if (value
> SIZE_MAX
) {
492 s
= migrate_get_current();
493 s
->bandwidth_limit
= value
;
495 qemu_file_set_rate_limit(s
->file
, s
->bandwidth_limit
/ XFER_LIMIT_RATIO
);
499 void qmp_migrate_set_downtime(double value
, Error
**errp
)
502 value
= MAX(0, MIN(UINT64_MAX
, value
));
503 max_downtime
= (uint64_t)value
;
506 bool migrate_rdma_pin_all(void)
510 s
= migrate_get_current();
512 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_X_RDMA_PIN_ALL
];
515 bool migrate_auto_converge(void)
519 s
= migrate_get_current();
521 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_AUTO_CONVERGE
];
524 bool migrate_zero_blocks(void)
528 s
= migrate_get_current();
530 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_ZERO_BLOCKS
];
533 int migrate_use_xbzrle(void)
537 s
= migrate_get_current();
539 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_XBZRLE
];
542 int64_t migrate_xbzrle_cache_size(void)
546 s
= migrate_get_current();
548 return s
->xbzrle_cache_size
;
551 /* migration thread support */
553 static void *migration_thread(void *opaque
)
555 MigrationState
*s
= opaque
;
556 int64_t initial_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
557 int64_t setup_start
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
558 int64_t initial_bytes
= 0;
559 int64_t max_size
= 0;
560 int64_t start_time
= initial_time
;
561 bool old_vm_running
= false;
563 DPRINTF("beginning savevm\n");
564 qemu_savevm_state_begin(s
->file
, &s
->params
);
566 s
->setup_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
) - setup_start
;
567 migrate_set_state(s
, MIG_STATE_SETUP
, MIG_STATE_ACTIVE
);
569 DPRINTF("setup complete\n");
571 while (s
->state
== MIG_STATE_ACTIVE
) {
572 int64_t current_time
;
573 uint64_t pending_size
;
575 if (!qemu_file_rate_limit(s
->file
)) {
576 DPRINTF("iterate\n");
577 pending_size
= qemu_savevm_state_pending(s
->file
, max_size
);
578 DPRINTF("pending size %" PRIu64
" max %" PRIu64
"\n",
579 pending_size
, max_size
);
580 if (pending_size
&& pending_size
>= max_size
) {
581 qemu_savevm_state_iterate(s
->file
);
585 DPRINTF("done iterating\n");
586 qemu_mutex_lock_iothread();
587 start_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
588 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
589 old_vm_running
= runstate_is_running();
591 ret
= vm_stop_force_state(RUN_STATE_FINISH_MIGRATE
);
593 qemu_file_set_rate_limit(s
->file
, INT_MAX
);
594 qemu_savevm_state_complete(s
->file
);
596 qemu_mutex_unlock_iothread();
599 migrate_set_state(s
, MIG_STATE_ACTIVE
, MIG_STATE_ERROR
);
603 if (!qemu_file_get_error(s
->file
)) {
604 migrate_set_state(s
, MIG_STATE_ACTIVE
, MIG_STATE_COMPLETED
);
610 if (qemu_file_get_error(s
->file
)) {
611 migrate_set_state(s
, MIG_STATE_ACTIVE
, MIG_STATE_ERROR
);
614 current_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
615 if (current_time
>= initial_time
+ BUFFER_DELAY
) {
616 uint64_t transferred_bytes
= qemu_ftell(s
->file
) - initial_bytes
;
617 uint64_t time_spent
= current_time
- initial_time
;
618 double bandwidth
= transferred_bytes
/ time_spent
;
619 max_size
= bandwidth
* migrate_max_downtime() / 1000000;
621 s
->mbps
= time_spent
? (((double) transferred_bytes
* 8.0) /
622 ((double) time_spent
/ 1000.0)) / 1000.0 / 1000.0 : -1;
624 DPRINTF("transferred %" PRIu64
" time_spent %" PRIu64
625 " bandwidth %g max_size %" PRId64
"\n",
626 transferred_bytes
, time_spent
, bandwidth
, max_size
);
627 /* if we haven't sent anything, we don't want to recalculate
628 10000 is a small enough number for our purposes */
629 if (s
->dirty_bytes_rate
&& transferred_bytes
> 10000) {
630 s
->expected_downtime
= s
->dirty_bytes_rate
/ bandwidth
;
633 qemu_file_reset_rate_limit(s
->file
);
634 initial_time
= current_time
;
635 initial_bytes
= qemu_ftell(s
->file
);
637 if (qemu_file_rate_limit(s
->file
)) {
638 /* usleep expects microseconds */
639 g_usleep((initial_time
+ BUFFER_DELAY
- current_time
)*1000);
643 qemu_mutex_lock_iothread();
644 if (s
->state
== MIG_STATE_COMPLETED
) {
645 int64_t end_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
646 s
->total_time
= end_time
- s
->total_time
;
647 s
->downtime
= end_time
- start_time
;
648 runstate_set(RUN_STATE_POSTMIGRATE
);
650 if (old_vm_running
) {
654 qemu_bh_schedule(s
->cleanup_bh
);
655 qemu_mutex_unlock_iothread();
660 void migrate_fd_connect(MigrationState
*s
)
662 s
->state
= MIG_STATE_SETUP
;
663 trace_migrate_set_state(MIG_STATE_SETUP
);
665 /* This is a best 1st approximation. ns to ms */
666 s
->expected_downtime
= max_downtime
/1000000;
667 s
->cleanup_bh
= qemu_bh_new(migrate_fd_cleanup
, s
);
669 qemu_file_set_rate_limit(s
->file
,
670 s
->bandwidth_limit
/ XFER_LIMIT_RATIO
);
672 /* Notify before starting migration thread */
673 notifier_list_notify(&migration_state_notifiers
, s
);
675 qemu_thread_create(&s
->thread
, migration_thread
, s
,
676 QEMU_THREAD_JOINABLE
);