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 int qemu_start_incoming_migration(const char *uri
, Error
**errp
)
72 if (strstart(uri
, "tcp:", &p
))
73 ret
= tcp_start_incoming_migration(p
, errp
);
75 else if (strstart(uri
, "exec:", &p
))
76 ret
= exec_start_incoming_migration(p
);
77 else if (strstart(uri
, "unix:", &p
))
78 ret
= unix_start_incoming_migration(p
);
79 else if (strstart(uri
, "fd:", &p
))
80 ret
= fd_start_incoming_migration(p
);
83 fprintf(stderr
, "unknown migration protocol: %s\n", uri
);
84 ret
= -EPROTONOSUPPORT
;
89 void process_incoming_migration(QEMUFile
*f
)
91 if (qemu_loadvm_state(f
) < 0) {
92 fprintf(stderr
, "load of migration failed\n");
96 DPRINTF("successfully loaded vm state\n");
98 bdrv_clear_incoming_migration_all();
99 /* Make sure all file formats flush their mutable metadata */
100 bdrv_invalidate_cache_all();
105 runstate_set(RUN_STATE_PRELAUNCH
);
109 /* amount of nanoseconds we are willing to wait for migration to be down.
110 * the choice of nanoseconds is because it is the maximum resolution that
111 * get_clock() can achieve. It is an internal measure. All user-visible
112 * units must be in seconds */
113 static uint64_t max_downtime
= 30000000;
115 uint64_t migrate_max_downtime(void)
120 MigrationCapabilityStatusList
*qmp_query_migrate_capabilities(Error
**errp
)
122 MigrationCapabilityStatusList
*head
= NULL
;
123 MigrationCapabilityStatusList
*caps
;
124 MigrationState
*s
= migrate_get_current();
127 for (i
= 0; i
< MIGRATION_CAPABILITY_MAX
; i
++) {
129 head
= g_malloc0(sizeof(*caps
));
132 caps
->next
= g_malloc0(sizeof(*caps
));
136 g_malloc(sizeof(*caps
->value
));
137 caps
->value
->capability
= i
;
138 caps
->value
->state
= s
->enabled_capabilities
[i
];
144 static void get_xbzrle_cache_stats(MigrationInfo
*info
)
146 if (migrate_use_xbzrle()) {
147 info
->has_xbzrle_cache
= true;
148 info
->xbzrle_cache
= g_malloc0(sizeof(*info
->xbzrle_cache
));
149 info
->xbzrle_cache
->cache_size
= migrate_xbzrle_cache_size();
150 info
->xbzrle_cache
->bytes
= xbzrle_mig_bytes_transferred();
151 info
->xbzrle_cache
->pages
= xbzrle_mig_pages_transferred();
152 info
->xbzrle_cache
->cache_miss
= xbzrle_mig_pages_cache_miss();
153 info
->xbzrle_cache
->overflow
= xbzrle_mig_pages_overflow();
157 MigrationInfo
*qmp_query_migrate(Error
**errp
)
159 MigrationInfo
*info
= g_malloc0(sizeof(*info
));
160 MigrationState
*s
= migrate_get_current();
163 case MIG_STATE_SETUP
:
164 /* no migration has happened ever */
166 case MIG_STATE_ACTIVE
:
167 info
->has_status
= true;
168 info
->status
= g_strdup("active");
169 info
->has_total_time
= true;
170 info
->total_time
= qemu_get_clock_ms(rt_clock
)
172 info
->has_expected_downtime
= true;
173 info
->expected_downtime
= s
->expected_downtime
;
175 info
->has_ram
= true;
176 info
->ram
= g_malloc0(sizeof(*info
->ram
));
177 info
->ram
->transferred
= ram_bytes_transferred();
178 info
->ram
->remaining
= ram_bytes_remaining();
179 info
->ram
->total
= ram_bytes_total();
180 info
->ram
->duplicate
= dup_mig_pages_transferred();
181 info
->ram
->normal
= norm_mig_pages_transferred();
182 info
->ram
->normal_bytes
= norm_mig_bytes_transferred();
184 if (blk_mig_active()) {
185 info
->has_disk
= true;
186 info
->disk
= g_malloc0(sizeof(*info
->disk
));
187 info
->disk
->transferred
= blk_mig_bytes_transferred();
188 info
->disk
->remaining
= blk_mig_bytes_remaining();
189 info
->disk
->total
= blk_mig_bytes_total();
192 get_xbzrle_cache_stats(info
);
194 case MIG_STATE_COMPLETED
:
195 get_xbzrle_cache_stats(info
);
197 info
->has_status
= true;
198 info
->status
= g_strdup("completed");
199 info
->total_time
= s
->total_time
;
200 info
->has_downtime
= true;
201 info
->downtime
= s
->downtime
;
203 info
->has_ram
= true;
204 info
->ram
= g_malloc0(sizeof(*info
->ram
));
205 info
->ram
->transferred
= ram_bytes_transferred();
206 info
->ram
->remaining
= 0;
207 info
->ram
->total
= ram_bytes_total();
208 info
->ram
->duplicate
= dup_mig_pages_transferred();
209 info
->ram
->normal
= norm_mig_pages_transferred();
210 info
->ram
->normal_bytes
= norm_mig_bytes_transferred();
212 case MIG_STATE_ERROR
:
213 info
->has_status
= true;
214 info
->status
= g_strdup("failed");
216 case MIG_STATE_CANCELLED
:
217 info
->has_status
= true;
218 info
->status
= g_strdup("cancelled");
225 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList
*params
,
228 MigrationState
*s
= migrate_get_current();
229 MigrationCapabilityStatusList
*cap
;
231 if (s
->state
== MIG_STATE_ACTIVE
) {
232 error_set(errp
, QERR_MIGRATION_ACTIVE
);
236 for (cap
= params
; cap
; cap
= cap
->next
) {
237 s
->enabled_capabilities
[cap
->value
->capability
] = cap
->value
->state
;
241 /* shared migration helpers */
243 static int migrate_fd_cleanup(MigrationState
*s
)
248 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
252 DPRINTF("closing file\n");
253 ret
= qemu_fclose(s
->file
);
265 void migrate_fd_error(MigrationState
*s
)
267 DPRINTF("setting error state\n");
268 s
->state
= MIG_STATE_ERROR
;
269 notifier_list_notify(&migration_state_notifiers
, s
);
270 migrate_fd_cleanup(s
);
273 static void migrate_fd_completed(MigrationState
*s
)
275 DPRINTF("setting completed state\n");
276 if (migrate_fd_cleanup(s
) < 0) {
277 s
->state
= MIG_STATE_ERROR
;
279 s
->state
= MIG_STATE_COMPLETED
;
280 runstate_set(RUN_STATE_POSTMIGRATE
);
282 notifier_list_notify(&migration_state_notifiers
, s
);
285 static void migrate_fd_put_notify(void *opaque
)
287 MigrationState
*s
= opaque
;
289 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
290 qemu_file_put_notify(s
->file
);
291 if (s
->file
&& qemu_file_get_error(s
->file
)) {
296 static ssize_t
migrate_fd_put_buffer(void *opaque
, const void *data
,
299 MigrationState
*s
= opaque
;
302 if (s
->state
!= MIG_STATE_ACTIVE
) {
307 ret
= s
->write(s
, data
, size
);
308 } while (ret
== -1 && ((s
->get_error(s
)) == EINTR
));
311 ret
= -(s
->get_error(s
));
313 if (ret
== -EAGAIN
) {
314 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, migrate_fd_put_notify
, s
);
320 static void migrate_fd_put_ready(void *opaque
)
322 MigrationState
*s
= opaque
;
325 if (s
->state
!= MIG_STATE_ACTIVE
) {
326 DPRINTF("put_ready returning because of non-active state\n");
330 DPRINTF("iterate\n");
331 ret
= qemu_savevm_state_iterate(s
->file
);
334 } else if (ret
== 1) {
335 int old_vm_running
= runstate_is_running();
336 int64_t start_time
, end_time
;
338 DPRINTF("done iterating\n");
339 start_time
= qemu_get_clock_ms(rt_clock
);
340 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
341 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE
);
343 if (qemu_savevm_state_complete(s
->file
) < 0) {
346 migrate_fd_completed(s
);
348 end_time
= qemu_get_clock_ms(rt_clock
);
349 s
->total_time
= end_time
- s
->total_time
;
350 s
->downtime
= end_time
- start_time
;
351 if (s
->state
!= MIG_STATE_COMPLETED
) {
352 if (old_vm_running
) {
359 static void migrate_fd_cancel(MigrationState
*s
)
361 if (s
->state
!= MIG_STATE_ACTIVE
)
364 DPRINTF("cancelling migration\n");
366 s
->state
= MIG_STATE_CANCELLED
;
367 notifier_list_notify(&migration_state_notifiers
, s
);
368 qemu_savevm_state_cancel(s
->file
);
370 migrate_fd_cleanup(s
);
373 static void migrate_fd_wait_for_unfreeze(void *opaque
)
375 MigrationState
*s
= opaque
;
378 DPRINTF("wait for unfreeze\n");
379 if (s
->state
!= MIG_STATE_ACTIVE
)
386 FD_SET(s
->fd
, &wfds
);
388 ret
= select(s
->fd
+ 1, NULL
, &wfds
, NULL
, NULL
);
389 } while (ret
== -1 && (s
->get_error(s
)) == EINTR
);
392 qemu_file_set_error(s
->file
, -s
->get_error(s
));
396 static int migrate_fd_close(void *opaque
)
398 MigrationState
*s
= opaque
;
400 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
404 void add_migration_state_change_notifier(Notifier
*notify
)
406 notifier_list_add(&migration_state_notifiers
, notify
);
409 void remove_migration_state_change_notifier(Notifier
*notify
)
411 notifier_remove(notify
);
414 bool migration_is_active(MigrationState
*s
)
416 return s
->state
== MIG_STATE_ACTIVE
;
419 bool migration_has_finished(MigrationState
*s
)
421 return s
->state
== MIG_STATE_COMPLETED
;
424 bool migration_has_failed(MigrationState
*s
)
426 return (s
->state
== MIG_STATE_CANCELLED
||
427 s
->state
== MIG_STATE_ERROR
);
430 void migrate_fd_connect(MigrationState
*s
)
434 s
->state
= MIG_STATE_ACTIVE
;
435 s
->file
= qemu_fopen_ops_buffered(s
,
437 migrate_fd_put_buffer
,
438 migrate_fd_put_ready
,
439 migrate_fd_wait_for_unfreeze
,
442 DPRINTF("beginning savevm\n");
443 ret
= qemu_savevm_state_begin(s
->file
, &s
->params
);
445 DPRINTF("failed, %d\n", ret
);
449 migrate_fd_put_ready(s
);
452 static MigrationState
*migrate_init(const MigrationParams
*params
)
454 MigrationState
*s
= migrate_get_current();
455 int64_t bandwidth_limit
= s
->bandwidth_limit
;
456 bool enabled_capabilities
[MIGRATION_CAPABILITY_MAX
];
457 int64_t xbzrle_cache_size
= s
->xbzrle_cache_size
;
459 memcpy(enabled_capabilities
, s
->enabled_capabilities
,
460 sizeof(enabled_capabilities
));
462 memset(s
, 0, sizeof(*s
));
463 s
->bandwidth_limit
= bandwidth_limit
;
465 memcpy(s
->enabled_capabilities
, enabled_capabilities
,
466 sizeof(enabled_capabilities
));
467 s
->xbzrle_cache_size
= xbzrle_cache_size
;
469 s
->bandwidth_limit
= bandwidth_limit
;
470 s
->state
= MIG_STATE_SETUP
;
471 s
->total_time
= qemu_get_clock_ms(rt_clock
);
476 static GSList
*migration_blockers
;
478 void migrate_add_blocker(Error
*reason
)
480 migration_blockers
= g_slist_prepend(migration_blockers
, reason
);
483 void migrate_del_blocker(Error
*reason
)
485 migration_blockers
= g_slist_remove(migration_blockers
, reason
);
488 void qmp_migrate(const char *uri
, bool has_blk
, bool blk
,
489 bool has_inc
, bool inc
, bool has_detach
, bool detach
,
492 MigrationState
*s
= migrate_get_current();
493 MigrationParams params
;
500 if (s
->state
== MIG_STATE_ACTIVE
) {
501 error_set(errp
, QERR_MIGRATION_ACTIVE
);
505 if (qemu_savevm_state_blocked(errp
)) {
509 if (migration_blockers
) {
510 *errp
= error_copy(migration_blockers
->data
);
514 s
= migrate_init(¶ms
);
516 if (strstart(uri
, "tcp:", &p
)) {
517 ret
= tcp_start_outgoing_migration(s
, p
, errp
);
519 } else if (strstart(uri
, "exec:", &p
)) {
520 ret
= exec_start_outgoing_migration(s
, p
);
521 } else if (strstart(uri
, "unix:", &p
)) {
522 ret
= unix_start_outgoing_migration(s
, p
);
523 } else if (strstart(uri
, "fd:", &p
)) {
524 ret
= fd_start_outgoing_migration(s
, p
);
527 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "uri", "a valid migration protocol");
532 if (!error_is_set(errp
)) {
533 DPRINTF("migration failed: %s\n", strerror(-ret
));
534 /* FIXME: we should return meaningful errors */
535 error_set(errp
, QERR_UNDEFINED_ERROR
);
540 notifier_list_notify(&migration_state_notifiers
, s
);
543 void qmp_migrate_cancel(Error
**errp
)
545 migrate_fd_cancel(migrate_get_current());
548 void qmp_migrate_set_cache_size(int64_t value
, Error
**errp
)
550 MigrationState
*s
= migrate_get_current();
552 /* Check for truncation */
553 if (value
!= (size_t)value
) {
554 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "cache size",
555 "exceeding address space");
559 s
->xbzrle_cache_size
= xbzrle_cache_resize(value
);
562 int64_t qmp_query_migrate_cache_size(Error
**errp
)
564 return migrate_xbzrle_cache_size();
567 void qmp_migrate_set_speed(int64_t value
, Error
**errp
)
575 s
= migrate_get_current();
576 s
->bandwidth_limit
= value
;
577 qemu_file_set_rate_limit(s
->file
, s
->bandwidth_limit
);
580 void qmp_migrate_set_downtime(double value
, Error
**errp
)
583 value
= MAX(0, MIN(UINT64_MAX
, value
));
584 max_downtime
= (uint64_t)value
;
587 int migrate_use_xbzrle(void)
591 s
= migrate_get_current();
593 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_XBZRLE
];
596 int64_t migrate_xbzrle_cache_size(void)
600 s
= migrate_get_current();
602 return s
->xbzrle_cache_size
;