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.h"
19 #include "buffered_file.h"
22 #include "qemu_socket.h"
23 #include "block-migration.h"
24 #include "qmp-commands.h"
26 //#define DEBUG_MIGRATION
28 #ifdef DEBUG_MIGRATION
29 #define DPRINTF(fmt, ...) \
30 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
32 #define DPRINTF(fmt, ...) \
44 #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
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 /* When we add fault tolerance, we could have several
53 migrations at once. For now we don't need to add
54 dynamic creation of migration */
56 MigrationState
*migrate_get_current(void)
58 static MigrationState current_migration
= {
59 .state
= MIG_STATE_SETUP
,
60 .bandwidth_limit
= MAX_THROTTLE
,
61 .xbzrle_cache_size
= DEFAULT_MIGRATE_CACHE_SIZE
,
64 return ¤t_migration
;
67 void qemu_start_incoming_migration(const char *uri
, Error
**errp
)
71 if (strstart(uri
, "tcp:", &p
))
72 tcp_start_incoming_migration(p
, errp
);
74 else if (strstart(uri
, "exec:", &p
))
75 exec_start_incoming_migration(p
, errp
);
76 else if (strstart(uri
, "unix:", &p
))
77 unix_start_incoming_migration(p
, errp
);
78 else if (strstart(uri
, "fd:", &p
))
79 fd_start_incoming_migration(p
, errp
);
82 error_setg(errp
, "unknown migration protocol: %s\n", uri
);
86 static void process_incoming_migration_co(void *opaque
)
91 ret
= qemu_loadvm_state(f
);
92 qemu_set_fd_handler(qemu_get_fd(f
), NULL
, NULL
, NULL
);
95 fprintf(stderr
, "load of migration failed\n");
99 DPRINTF("successfully loaded vm state\n");
101 bdrv_clear_incoming_migration_all();
102 /* Make sure all file formats flush their mutable metadata */
103 bdrv_invalidate_cache_all();
108 runstate_set(RUN_STATE_PAUSED
);
112 static void enter_migration_coroutine(void *opaque
)
114 Coroutine
*co
= opaque
;
115 qemu_coroutine_enter(co
, NULL
);
118 void process_incoming_migration(QEMUFile
*f
)
120 Coroutine
*co
= qemu_coroutine_create(process_incoming_migration_co
);
121 int fd
= qemu_get_fd(f
);
124 socket_set_nonblock(fd
);
125 qemu_set_fd_handler(fd
, enter_migration_coroutine
, NULL
, co
);
126 qemu_coroutine_enter(co
, f
);
129 /* amount of nanoseconds we are willing to wait for migration to be down.
130 * the choice of nanoseconds is because it is the maximum resolution that
131 * get_clock() can achieve. It is an internal measure. All user-visible
132 * units must be in seconds */
133 static uint64_t max_downtime
= 30000000;
135 uint64_t migrate_max_downtime(void)
140 MigrationCapabilityStatusList
*qmp_query_migrate_capabilities(Error
**errp
)
142 MigrationCapabilityStatusList
*head
= NULL
;
143 MigrationCapabilityStatusList
*caps
;
144 MigrationState
*s
= migrate_get_current();
147 for (i
= 0; i
< MIGRATION_CAPABILITY_MAX
; i
++) {
149 head
= g_malloc0(sizeof(*caps
));
152 caps
->next
= g_malloc0(sizeof(*caps
));
156 g_malloc(sizeof(*caps
->value
));
157 caps
->value
->capability
= i
;
158 caps
->value
->state
= s
->enabled_capabilities
[i
];
164 static void get_xbzrle_cache_stats(MigrationInfo
*info
)
166 if (migrate_use_xbzrle()) {
167 info
->has_xbzrle_cache
= true;
168 info
->xbzrle_cache
= g_malloc0(sizeof(*info
->xbzrle_cache
));
169 info
->xbzrle_cache
->cache_size
= migrate_xbzrle_cache_size();
170 info
->xbzrle_cache
->bytes
= xbzrle_mig_bytes_transferred();
171 info
->xbzrle_cache
->pages
= xbzrle_mig_pages_transferred();
172 info
->xbzrle_cache
->cache_miss
= xbzrle_mig_pages_cache_miss();
173 info
->xbzrle_cache
->overflow
= xbzrle_mig_pages_overflow();
177 MigrationInfo
*qmp_query_migrate(Error
**errp
)
179 MigrationInfo
*info
= g_malloc0(sizeof(*info
));
180 MigrationState
*s
= migrate_get_current();
183 case MIG_STATE_SETUP
:
184 /* no migration has happened ever */
186 case MIG_STATE_ACTIVE
:
187 info
->has_status
= true;
188 info
->status
= g_strdup("active");
189 info
->has_total_time
= true;
190 info
->total_time
= qemu_get_clock_ms(rt_clock
)
192 info
->has_expected_downtime
= true;
193 info
->expected_downtime
= s
->expected_downtime
;
195 info
->has_ram
= true;
196 info
->ram
= g_malloc0(sizeof(*info
->ram
));
197 info
->ram
->transferred
= ram_bytes_transferred();
198 info
->ram
->remaining
= ram_bytes_remaining();
199 info
->ram
->total
= ram_bytes_total();
200 info
->ram
->duplicate
= dup_mig_pages_transferred();
201 info
->ram
->normal
= norm_mig_pages_transferred();
202 info
->ram
->normal_bytes
= norm_mig_bytes_transferred();
203 info
->ram
->dirty_pages_rate
= s
->dirty_pages_rate
;
206 if (blk_mig_active()) {
207 info
->has_disk
= true;
208 info
->disk
= g_malloc0(sizeof(*info
->disk
));
209 info
->disk
->transferred
= blk_mig_bytes_transferred();
210 info
->disk
->remaining
= blk_mig_bytes_remaining();
211 info
->disk
->total
= blk_mig_bytes_total();
214 get_xbzrle_cache_stats(info
);
216 case MIG_STATE_COMPLETED
:
217 get_xbzrle_cache_stats(info
);
219 info
->has_status
= true;
220 info
->status
= g_strdup("completed");
221 info
->total_time
= s
->total_time
;
222 info
->has_downtime
= true;
223 info
->downtime
= s
->downtime
;
225 info
->has_ram
= true;
226 info
->ram
= g_malloc0(sizeof(*info
->ram
));
227 info
->ram
->transferred
= ram_bytes_transferred();
228 info
->ram
->remaining
= 0;
229 info
->ram
->total
= ram_bytes_total();
230 info
->ram
->duplicate
= dup_mig_pages_transferred();
231 info
->ram
->normal
= norm_mig_pages_transferred();
232 info
->ram
->normal_bytes
= norm_mig_bytes_transferred();
234 case MIG_STATE_ERROR
:
235 info
->has_status
= true;
236 info
->status
= g_strdup("failed");
238 case MIG_STATE_CANCELLED
:
239 info
->has_status
= true;
240 info
->status
= g_strdup("cancelled");
247 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList
*params
,
250 MigrationState
*s
= migrate_get_current();
251 MigrationCapabilityStatusList
*cap
;
253 if (s
->state
== MIG_STATE_ACTIVE
) {
254 error_set(errp
, QERR_MIGRATION_ACTIVE
);
258 for (cap
= params
; cap
; cap
= cap
->next
) {
259 s
->enabled_capabilities
[cap
->value
->capability
] = cap
->value
->state
;
263 /* shared migration helpers */
265 static int migrate_fd_cleanup(MigrationState
*s
)
270 DPRINTF("closing file\n");
271 ret
= qemu_fclose(s
->file
);
279 void migrate_fd_error(MigrationState
*s
)
281 DPRINTF("setting error state\n");
282 s
->state
= MIG_STATE_ERROR
;
283 notifier_list_notify(&migration_state_notifiers
, s
);
284 migrate_fd_cleanup(s
);
287 static void migrate_fd_completed(MigrationState
*s
)
289 DPRINTF("setting completed state\n");
290 if (migrate_fd_cleanup(s
) < 0) {
291 s
->state
= MIG_STATE_ERROR
;
293 s
->state
= MIG_STATE_COMPLETED
;
294 runstate_set(RUN_STATE_POSTMIGRATE
);
296 notifier_list_notify(&migration_state_notifiers
, s
);
299 static void migrate_fd_put_notify(void *opaque
)
301 MigrationState
*s
= opaque
;
304 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
305 ret
= qemu_file_put_notify(s
->file
);
311 ssize_t
migrate_fd_put_buffer(MigrationState
*s
, const void *data
,
316 if (s
->state
!= MIG_STATE_ACTIVE
) {
321 ret
= s
->write(s
, data
, size
);
322 } while (ret
== -1 && ((s
->get_error(s
)) == EINTR
));
325 ret
= -(s
->get_error(s
));
327 if (ret
== -EAGAIN
) {
328 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, migrate_fd_put_notify
, s
);
334 void migrate_fd_put_ready(MigrationState
*s
)
338 if (s
->state
!= MIG_STATE_ACTIVE
) {
339 DPRINTF("put_ready returning because of non-active state\n");
343 DPRINTF("iterate\n");
344 ret
= qemu_savevm_state_iterate(s
->file
);
347 } else if (ret
== 1) {
348 int old_vm_running
= runstate_is_running();
349 int64_t start_time
, end_time
;
351 DPRINTF("done iterating\n");
352 start_time
= qemu_get_clock_ms(rt_clock
);
353 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
354 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE
);
356 if (qemu_savevm_state_complete(s
->file
) < 0) {
359 migrate_fd_completed(s
);
361 end_time
= qemu_get_clock_ms(rt_clock
);
362 s
->total_time
= end_time
- s
->total_time
;
363 s
->downtime
= end_time
- start_time
;
364 if (s
->state
!= MIG_STATE_COMPLETED
) {
365 if (old_vm_running
) {
372 static void migrate_fd_cancel(MigrationState
*s
)
374 if (s
->state
!= MIG_STATE_ACTIVE
)
377 DPRINTF("cancelling migration\n");
379 s
->state
= MIG_STATE_CANCELLED
;
380 notifier_list_notify(&migration_state_notifiers
, s
);
381 qemu_savevm_state_cancel(s
->file
);
383 migrate_fd_cleanup(s
);
386 int migrate_fd_wait_for_unfreeze(MigrationState
*s
)
390 DPRINTF("wait for unfreeze\n");
391 if (s
->state
!= MIG_STATE_ACTIVE
)
398 FD_SET(s
->fd
, &wfds
);
400 ret
= select(s
->fd
+ 1, NULL
, &wfds
, NULL
, NULL
);
401 } while (ret
== -1 && (s
->get_error(s
)) == EINTR
);
404 return -s
->get_error(s
);
409 int migrate_fd_close(MigrationState
*s
)
413 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
420 void add_migration_state_change_notifier(Notifier
*notify
)
422 notifier_list_add(&migration_state_notifiers
, notify
);
425 void remove_migration_state_change_notifier(Notifier
*notify
)
427 notifier_remove(notify
);
430 bool migration_is_active(MigrationState
*s
)
432 return s
->state
== MIG_STATE_ACTIVE
;
435 bool migration_has_finished(MigrationState
*s
)
437 return s
->state
== MIG_STATE_COMPLETED
;
440 bool migration_has_failed(MigrationState
*s
)
442 return (s
->state
== MIG_STATE_CANCELLED
||
443 s
->state
== MIG_STATE_ERROR
);
446 void migrate_fd_connect(MigrationState
*s
)
450 s
->state
= MIG_STATE_ACTIVE
;
451 s
->file
= qemu_fopen_ops_buffered(s
);
453 DPRINTF("beginning savevm\n");
454 ret
= qemu_savevm_state_begin(s
->file
, &s
->params
);
456 DPRINTF("failed, %d\n", ret
);
460 migrate_fd_put_ready(s
);
463 static MigrationState
*migrate_init(const MigrationParams
*params
)
465 MigrationState
*s
= migrate_get_current();
466 int64_t bandwidth_limit
= s
->bandwidth_limit
;
467 bool enabled_capabilities
[MIGRATION_CAPABILITY_MAX
];
468 int64_t xbzrle_cache_size
= s
->xbzrle_cache_size
;
470 memcpy(enabled_capabilities
, s
->enabled_capabilities
,
471 sizeof(enabled_capabilities
));
473 memset(s
, 0, sizeof(*s
));
474 s
->bandwidth_limit
= bandwidth_limit
;
476 memcpy(s
->enabled_capabilities
, enabled_capabilities
,
477 sizeof(enabled_capabilities
));
478 s
->xbzrle_cache_size
= xbzrle_cache_size
;
480 s
->bandwidth_limit
= bandwidth_limit
;
481 s
->state
= MIG_STATE_SETUP
;
482 s
->total_time
= qemu_get_clock_ms(rt_clock
);
487 static GSList
*migration_blockers
;
489 void migrate_add_blocker(Error
*reason
)
491 migration_blockers
= g_slist_prepend(migration_blockers
, reason
);
494 void migrate_del_blocker(Error
*reason
)
496 migration_blockers
= g_slist_remove(migration_blockers
, reason
);
499 void qmp_migrate(const char *uri
, bool has_blk
, bool blk
,
500 bool has_inc
, bool inc
, bool has_detach
, bool detach
,
503 Error
*local_err
= NULL
;
504 MigrationState
*s
= migrate_get_current();
505 MigrationParams params
;
511 if (s
->state
== MIG_STATE_ACTIVE
) {
512 error_set(errp
, QERR_MIGRATION_ACTIVE
);
516 if (qemu_savevm_state_blocked(errp
)) {
520 if (migration_blockers
) {
521 *errp
= error_copy(migration_blockers
->data
);
525 s
= migrate_init(¶ms
);
527 if (strstart(uri
, "tcp:", &p
)) {
528 tcp_start_outgoing_migration(s
, p
, &local_err
);
530 } else if (strstart(uri
, "exec:", &p
)) {
531 exec_start_outgoing_migration(s
, p
, &local_err
);
532 } else if (strstart(uri
, "unix:", &p
)) {
533 unix_start_outgoing_migration(s
, p
, &local_err
);
534 } else if (strstart(uri
, "fd:", &p
)) {
535 fd_start_outgoing_migration(s
, p
, &local_err
);
538 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "uri", "a valid migration protocol");
544 error_propagate(errp
, local_err
);
548 notifier_list_notify(&migration_state_notifiers
, s
);
551 void qmp_migrate_cancel(Error
**errp
)
553 migrate_fd_cancel(migrate_get_current());
556 void qmp_migrate_set_cache_size(int64_t value
, Error
**errp
)
558 MigrationState
*s
= migrate_get_current();
560 /* Check for truncation */
561 if (value
!= (size_t)value
) {
562 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "cache size",
563 "exceeding address space");
567 s
->xbzrle_cache_size
= xbzrle_cache_resize(value
);
570 int64_t qmp_query_migrate_cache_size(Error
**errp
)
572 return migrate_xbzrle_cache_size();
575 void qmp_migrate_set_speed(int64_t value
, Error
**errp
)
583 s
= migrate_get_current();
584 s
->bandwidth_limit
= value
;
585 qemu_file_set_rate_limit(s
->file
, s
->bandwidth_limit
);
588 void qmp_migrate_set_downtime(double value
, Error
**errp
)
591 value
= MAX(0, MIN(UINT64_MAX
, value
));
592 max_downtime
= (uint64_t)value
;
595 int migrate_use_xbzrle(void)
599 s
= migrate_get_current();
601 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_XBZRLE
];
604 int64_t migrate_xbzrle_cache_size(void)
608 s
= migrate_get_current();
610 return s
->xbzrle_cache_size
;