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.
14 #include "qemu-common.h"
15 #include "migration.h"
17 #include "buffered_file.h"
20 #include "qemu_socket.h"
21 #include "block-migration.h"
22 #include "qemu-objects.h"
24 //#define DEBUG_MIGRATION
26 #ifdef DEBUG_MIGRATION
27 #define DPRINTF(fmt, ...) \
28 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
30 #define DPRINTF(fmt, ...) \
42 #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
44 static NotifierList migration_state_notifiers =
45 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
47 /* When we add fault tolerance, we could have several
48 migrations at once. For now we don't need to add
49 dynamic creation of migration */
51 static MigrationState *migrate_get_current(void)
53 static MigrationState current_migration = {
54 .state = MIG_STATE_SETUP,
55 .bandwidth_limit = MAX_THROTTLE,
58 return ¤t_migration;
61 int qemu_start_incoming_migration(const char *uri)
66 if (strstart(uri, "tcp:", &p))
67 ret = tcp_start_incoming_migration(p);
69 else if (strstart(uri, "exec:", &p))
70 ret = exec_start_incoming_migration(p);
71 else if (strstart(uri, "unix:", &p))
72 ret = unix_start_incoming_migration(p);
73 else if (strstart(uri, "fd:", &p))
74 ret = fd_start_incoming_migration(p);
77 fprintf(stderr, "unknown migration protocol: %s\n", uri);
78 ret = -EPROTONOSUPPORT;
83 void process_incoming_migration(QEMUFile *f)
85 if (qemu_loadvm_state(f) < 0) {
86 fprintf(stderr, "load of migration failed\n");
90 DPRINTF("successfully loaded vm state\n");
95 runstate_set(RUN_STATE_PRELAUNCH);
99 /* amount of nanoseconds we are willing to wait for migration to be down.
100 * the choice of nanoseconds is because it is the maximum resolution that
101 * get_clock() can achieve. It is an internal measure. All user-visible
102 * units must be in seconds */
103 static uint64_t max_downtime = 30000000;
105 uint64_t migrate_max_downtime(void)
110 static void migrate_print_status(Monitor *mon, const char *name,
111 const QDict *status_dict)
115 qdict = qobject_to_qdict(qdict_get(status_dict, name));
117 monitor_printf(mon, "transferred %s: %" PRIu64 " kbytes\n", name,
118 qdict_get_int(qdict, "transferred") >> 10);
119 monitor_printf(mon, "remaining %s: %" PRIu64 " kbytes\n", name,
120 qdict_get_int(qdict, "remaining") >> 10);
121 monitor_printf(mon, "total %s: %" PRIu64 " kbytes\n", name,
122 qdict_get_int(qdict, "total") >> 10);
125 void do_info_migrate_print(Monitor *mon, const QObject *data)
129 qdict = qobject_to_qdict(data);
131 monitor_printf(mon, "Migration status: %s\n",
132 qdict_get_str(qdict, "status"));
134 if (qdict_haskey(qdict, "ram")) {
135 migrate_print_status(mon, "ram", qdict);
138 if (qdict_haskey(qdict, "disk")) {
139 migrate_print_status(mon, "disk", qdict);
143 static void migrate_put_status(QDict *qdict, const char *name,
144 uint64_t trans, uint64_t rem, uint64_t total)
148 obj = qobject_from_jsonf("{ 'transferred': %" PRId64 ", "
149 "'remaining': %" PRId64 ", "
150 "'total': %" PRId64 " }", trans, rem, total);
151 qdict_put_obj(qdict, name, obj);
154 void do_info_migrate(Monitor *mon, QObject **ret_data)
157 MigrationState *s = migrate_get_current();
160 case MIG_STATE_SETUP:
161 /* no migration has happened ever */
163 case MIG_STATE_ACTIVE:
165 qdict_put(qdict, "status", qstring_from_str("active"));
167 migrate_put_status(qdict, "ram", ram_bytes_transferred(),
168 ram_bytes_remaining(), ram_bytes_total());
170 if (blk_mig_active()) {
171 migrate_put_status(qdict, "disk", blk_mig_bytes_transferred(),
172 blk_mig_bytes_remaining(),
173 blk_mig_bytes_total());
176 *ret_data = QOBJECT(qdict);
178 case MIG_STATE_COMPLETED:
179 *ret_data = qobject_from_jsonf("{ 'status': 'completed' }");
181 case MIG_STATE_ERROR:
182 *ret_data = qobject_from_jsonf("{ 'status': 'failed' }");
184 case MIG_STATE_CANCELLED:
185 *ret_data = qobject_from_jsonf("{ 'status': 'cancelled' }");
190 /* shared migration helpers */
192 static void migrate_fd_monitor_suspend(MigrationState *s, Monitor *mon)
195 if (monitor_suspend(mon) == 0) {
196 DPRINTF("suspending monitor\n");
198 monitor_printf(mon, "terminal does not allow synchronous "
199 "migration, continuing detached\n");
203 static int migrate_fd_cleanup(MigrationState *s)
207 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
210 DPRINTF("closing file\n");
211 if (qemu_fclose(s->file) != 0) {
217 monitor_resume(s->mon);
229 void migrate_fd_error(MigrationState *s)
231 DPRINTF("setting error state\n");
232 s->state = MIG_STATE_ERROR;
233 notifier_list_notify(&migration_state_notifiers, s);
234 migrate_fd_cleanup(s);
237 static void migrate_fd_completed(MigrationState *s)
239 DPRINTF("setting completed state\n");
240 if (migrate_fd_cleanup(s) < 0) {
241 s->state = MIG_STATE_ERROR;
243 s->state = MIG_STATE_COMPLETED;
244 runstate_set(RUN_STATE_POSTMIGRATE);
246 notifier_list_notify(&migration_state_notifiers, s);
249 static void migrate_fd_put_notify(void *opaque)
251 MigrationState *s = opaque;
253 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
254 qemu_file_put_notify(s->file);
255 if (qemu_file_get_error(s->file)) {
260 static ssize_t migrate_fd_put_buffer(void *opaque, const void *data,
263 MigrationState *s = opaque;
266 if (s->state != MIG_STATE_ACTIVE) {
271 ret = s->write(s, data, size);
272 } while (ret == -1 && ((s->get_error(s)) == EINTR));
275 ret = -(s->get_error(s));
277 if (ret == -EAGAIN) {
278 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
284 static void migrate_fd_put_ready(void *opaque)
286 MigrationState *s = opaque;
289 if (s->state != MIG_STATE_ACTIVE) {
290 DPRINTF("put_ready returning because of non-active state\n");
294 DPRINTF("iterate\n");
295 ret = qemu_savevm_state_iterate(s->mon, s->file);
298 } else if (ret == 1) {
299 int old_vm_running = runstate_is_running();
301 DPRINTF("done iterating\n");
302 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
304 if (qemu_savevm_state_complete(s->mon, s->file) < 0) {
307 migrate_fd_completed(s);
309 if (s->state != MIG_STATE_COMPLETED) {
310 if (old_vm_running) {
317 static void migrate_fd_cancel(MigrationState *s)
319 if (s->state != MIG_STATE_ACTIVE)
322 DPRINTF("cancelling migration\n");
324 s->state = MIG_STATE_CANCELLED;
325 notifier_list_notify(&migration_state_notifiers, s);
326 qemu_savevm_state_cancel(s->mon, s->file);
328 migrate_fd_cleanup(s);
331 static void migrate_fd_wait_for_unfreeze(void *opaque)
333 MigrationState *s = opaque;
336 DPRINTF("wait for unfreeze\n");
337 if (s->state != MIG_STATE_ACTIVE)
344 FD_SET(s->fd, &wfds);
346 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
347 } while (ret == -1 && (s->get_error(s)) == EINTR);
350 qemu_file_set_error(s->file, -s->get_error(s));
354 static int migrate_fd_close(void *opaque)
356 MigrationState *s = opaque;
359 monitor_resume(s->mon);
361 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
365 void add_migration_state_change_notifier(Notifier *notify)
367 notifier_list_add(&migration_state_notifiers, notify);
370 void remove_migration_state_change_notifier(Notifier *notify)
372 notifier_list_remove(&migration_state_notifiers, notify);
375 bool migration_has_finished(MigrationState *s)
377 return s->state == MIG_STATE_COMPLETED;
380 void migrate_fd_connect(MigrationState *s)
384 s->state = MIG_STATE_ACTIVE;
385 s->file = qemu_fopen_ops_buffered(s,
387 migrate_fd_put_buffer,
388 migrate_fd_put_ready,
389 migrate_fd_wait_for_unfreeze,
392 DPRINTF("beginning savevm\n");
393 ret = qemu_savevm_state_begin(s->mon, s->file, s->blk, s->shared);
395 DPRINTF("failed, %d\n", ret);
399 migrate_fd_put_ready(s);
402 static MigrationState *migrate_init(Monitor *mon, int detach, int blk, int inc)
404 MigrationState *s = migrate_get_current();
405 int64_t bandwidth_limit = s->bandwidth_limit;
407 memset(s, 0, sizeof(*s));
408 s->bandwidth_limit = bandwidth_limit;
412 s->bandwidth_limit = bandwidth_limit;
413 s->state = MIG_STATE_SETUP;
416 migrate_fd_monitor_suspend(s, mon);
422 int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
424 MigrationState *s = migrate_get_current();
426 int detach = qdict_get_try_bool(qdict, "detach", 0);
427 int blk = qdict_get_try_bool(qdict, "blk", 0);
428 int inc = qdict_get_try_bool(qdict, "inc", 0);
429 const char *uri = qdict_get_str(qdict, "uri");
432 if (s->state == MIG_STATE_ACTIVE) {
433 monitor_printf(mon, "migration already in progress\n");
437 if (qemu_savevm_state_blocked(mon)) {
441 s = migrate_init(mon, detach, blk, inc);
443 if (strstart(uri, "tcp:", &p)) {
444 ret = tcp_start_outgoing_migration(s, p);
446 } else if (strstart(uri, "exec:", &p)) {
447 ret = exec_start_outgoing_migration(s, p);
448 } else if (strstart(uri, "unix:", &p)) {
449 ret = unix_start_outgoing_migration(s, p);
450 } else if (strstart(uri, "fd:", &p)) {
451 ret = fd_start_outgoing_migration(s, p);
454 monitor_printf(mon, "unknown migration protocol: %s\n", uri);
459 monitor_printf(mon, "migration failed: %s\n", strerror(-ret));
463 notifier_list_notify(&migration_state_notifiers, s);
467 int do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data)
469 migrate_fd_cancel(migrate_get_current());
473 int do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data)
478 d = qdict_get_int(qdict, "value");
483 s = migrate_get_current();
484 s->bandwidth_limit = d;
485 qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
490 int do_migrate_set_downtime(Monitor *mon, const QDict *qdict,
495 d = qdict_get_double(qdict, "value") * 1e9;
496 d = MAX(0, MIN(UINT64_MAX, d));
497 max_downtime = (uint64_t)d;