4 * Copyright IBM, Corp. 2008
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 static 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 MigrationInfo *qmp_query_migrate(Error **errp)
146 MigrationInfo *info = g_malloc0(sizeof(*info));
147 MigrationState *s = migrate_get_current();
150 case MIG_STATE_SETUP:
151 /* no migration has happened ever */
153 case MIG_STATE_ACTIVE:
154 info->has_status = true;
155 info->status = g_strdup("active");
157 info->has_ram = true;
158 info->ram = g_malloc0(sizeof(*info->ram));
159 info->ram->transferred = ram_bytes_transferred();
160 info->ram->remaining = ram_bytes_remaining();
161 info->ram->total = ram_bytes_total();
162 info->ram->total_time = qemu_get_clock_ms(rt_clock)
165 if (blk_mig_active()) {
166 info->has_disk = true;
167 info->disk = g_malloc0(sizeof(*info->disk));
168 info->disk->transferred = blk_mig_bytes_transferred();
169 info->disk->remaining = blk_mig_bytes_remaining();
170 info->disk->total = blk_mig_bytes_total();
173 case MIG_STATE_COMPLETED:
174 info->has_status = true;
175 info->status = g_strdup("completed");
177 info->has_ram = true;
178 info->ram = g_malloc0(sizeof(*info->ram));
179 info->ram->transferred = ram_bytes_transferred();
180 info->ram->remaining = 0;
181 info->ram->total = ram_bytes_total();
182 info->ram->total_time = s->total_time;
184 case MIG_STATE_ERROR:
185 info->has_status = true;
186 info->status = g_strdup("failed");
188 case MIG_STATE_CANCELLED:
189 info->has_status = true;
190 info->status = g_strdup("cancelled");
197 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
200 MigrationState *s = migrate_get_current();
201 MigrationCapabilityStatusList *cap;
203 if (s->state == MIG_STATE_ACTIVE) {
204 error_set(errp, QERR_MIGRATION_ACTIVE);
208 for (cap = params; cap; cap = cap->next) {
209 s->enabled_capabilities[cap->value->capability] = cap->value->state;
213 /* shared migration helpers */
215 static int migrate_fd_cleanup(MigrationState *s)
219 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
222 DPRINTF("closing file\n");
223 ret = qemu_fclose(s->file);
235 void migrate_fd_error(MigrationState *s)
237 DPRINTF("setting error state\n");
238 s->state = MIG_STATE_ERROR;
239 notifier_list_notify(&migration_state_notifiers, s);
240 migrate_fd_cleanup(s);
243 static void migrate_fd_completed(MigrationState *s)
245 DPRINTF("setting completed state\n");
246 if (migrate_fd_cleanup(s) < 0) {
247 s->state = MIG_STATE_ERROR;
249 s->state = MIG_STATE_COMPLETED;
250 runstate_set(RUN_STATE_POSTMIGRATE);
252 notifier_list_notify(&migration_state_notifiers, s);
255 static void migrate_fd_put_notify(void *opaque)
257 MigrationState *s = opaque;
259 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
260 qemu_file_put_notify(s->file);
261 if (s->file && qemu_file_get_error(s->file)) {
266 static ssize_t migrate_fd_put_buffer(void *opaque, const void *data,
269 MigrationState *s = opaque;
272 if (s->state != MIG_STATE_ACTIVE) {
277 ret = s->write(s, data, size);
278 } while (ret == -1 && ((s->get_error(s)) == EINTR));
281 ret = -(s->get_error(s));
283 if (ret == -EAGAIN) {
284 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
290 static void migrate_fd_put_ready(void *opaque)
292 MigrationState *s = opaque;
295 if (s->state != MIG_STATE_ACTIVE) {
296 DPRINTF("put_ready returning because of non-active state\n");
300 DPRINTF("iterate\n");
301 ret = qemu_savevm_state_iterate(s->file);
304 } else if (ret == 1) {
305 int old_vm_running = runstate_is_running();
307 DPRINTF("done iterating\n");
308 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
309 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
311 if (qemu_savevm_state_complete(s->file) < 0) {
314 migrate_fd_completed(s);
316 s->total_time = qemu_get_clock_ms(rt_clock) - s->total_time;
317 if (s->state != MIG_STATE_COMPLETED) {
318 if (old_vm_running) {
325 static void migrate_fd_cancel(MigrationState *s)
327 if (s->state != MIG_STATE_ACTIVE)
330 DPRINTF("cancelling migration\n");
332 s->state = MIG_STATE_CANCELLED;
333 notifier_list_notify(&migration_state_notifiers, s);
334 qemu_savevm_state_cancel(s->file);
336 migrate_fd_cleanup(s);
339 static void migrate_fd_wait_for_unfreeze(void *opaque)
341 MigrationState *s = opaque;
344 DPRINTF("wait for unfreeze\n");
345 if (s->state != MIG_STATE_ACTIVE)
352 FD_SET(s->fd, &wfds);
354 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
355 } while (ret == -1 && (s->get_error(s)) == EINTR);
358 qemu_file_set_error(s->file, -s->get_error(s));
362 static int migrate_fd_close(void *opaque)
364 MigrationState *s = opaque;
366 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
370 void add_migration_state_change_notifier(Notifier *notify)
372 notifier_list_add(&migration_state_notifiers, notify);
375 void remove_migration_state_change_notifier(Notifier *notify)
377 notifier_remove(notify);
380 bool migration_is_active(MigrationState *s)
382 return s->state == MIG_STATE_ACTIVE;
385 bool migration_has_finished(MigrationState *s)
387 return s->state == MIG_STATE_COMPLETED;
390 bool migration_has_failed(MigrationState *s)
392 return (s->state == MIG_STATE_CANCELLED ||
393 s->state == MIG_STATE_ERROR);
396 void migrate_fd_connect(MigrationState *s)
400 s->state = MIG_STATE_ACTIVE;
401 s->file = qemu_fopen_ops_buffered(s,
403 migrate_fd_put_buffer,
404 migrate_fd_put_ready,
405 migrate_fd_wait_for_unfreeze,
408 DPRINTF("beginning savevm\n");
409 ret = qemu_savevm_state_begin(s->file, &s->params);
411 DPRINTF("failed, %d\n", ret);
415 migrate_fd_put_ready(s);
418 static MigrationState *migrate_init(const MigrationParams *params)
420 MigrationState *s = migrate_get_current();
421 int64_t bandwidth_limit = s->bandwidth_limit;
422 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
423 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
425 memcpy(enabled_capabilities, s->enabled_capabilities,
426 sizeof(enabled_capabilities));
428 memset(s, 0, sizeof(*s));
429 s->bandwidth_limit = bandwidth_limit;
431 memcpy(s->enabled_capabilities, enabled_capabilities,
432 sizeof(enabled_capabilities));
433 s->xbzrle_cache_size = xbzrle_cache_size;
435 s->bandwidth_limit = bandwidth_limit;
436 s->state = MIG_STATE_SETUP;
437 s->total_time = qemu_get_clock_ms(rt_clock);
442 static GSList *migration_blockers;
444 void migrate_add_blocker(Error *reason)
446 migration_blockers = g_slist_prepend(migration_blockers, reason);
449 void migrate_del_blocker(Error *reason)
451 migration_blockers = g_slist_remove(migration_blockers, reason);
454 void qmp_migrate(const char *uri, bool has_blk, bool blk,
455 bool has_inc, bool inc, bool has_detach, bool detach,
458 MigrationState *s = migrate_get_current();
459 MigrationParams params;
466 if (s->state == MIG_STATE_ACTIVE) {
467 error_set(errp, QERR_MIGRATION_ACTIVE);
471 if (qemu_savevm_state_blocked(errp)) {
475 if (migration_blockers) {
476 *errp = error_copy(migration_blockers->data);
480 s = migrate_init(¶ms);
482 if (strstart(uri, "tcp:", &p)) {
483 ret = tcp_start_outgoing_migration(s, p, errp);
485 } else if (strstart(uri, "exec:", &p)) {
486 ret = exec_start_outgoing_migration(s, p);
487 } else if (strstart(uri, "unix:", &p)) {
488 ret = unix_start_outgoing_migration(s, p);
489 } else if (strstart(uri, "fd:", &p)) {
490 ret = fd_start_outgoing_migration(s, p);
493 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
498 if (!error_is_set(errp)) {
499 DPRINTF("migration failed: %s\n", strerror(-ret));
500 /* FIXME: we should return meaningful errors */
501 error_set(errp, QERR_UNDEFINED_ERROR);
506 notifier_list_notify(&migration_state_notifiers, s);
509 void qmp_migrate_cancel(Error **errp)
511 migrate_fd_cancel(migrate_get_current());
514 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
516 MigrationState *s = migrate_get_current();
518 /* Check for truncation */
519 if (value != (size_t)value) {
520 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
521 "exceeding address space");
525 s->xbzrle_cache_size = xbzrle_cache_resize(value);
528 int64_t qmp_query_migrate_cache_size(Error **errp)
530 return migrate_xbzrle_cache_size();
533 void qmp_migrate_set_speed(int64_t value, Error **errp)
541 s = migrate_get_current();
542 s->bandwidth_limit = value;
543 qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
546 void qmp_migrate_set_downtime(double value, Error **errp)
549 value = MAX(0, MIN(UINT64_MAX, value));
550 max_downtime = (uint64_t)value;
553 int migrate_use_xbzrle(void)
557 s = migrate_get_current();
559 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
562 int64_t migrate_xbzrle_cache_size(void)
566 s = migrate_get_current();
568 return s->xbzrle_cache_size;