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, ...) \
34 /* Migration speed throttling */
35 static int64_t max_throttle = (32 << 20);
37 static FdMigrationState *current_migration;
39 static NotifierList migration_state_notifiers =
40 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
42 int qemu_start_incoming_migration(const char *uri)
47 if (strstart(uri, "tcp:", &p))
48 ret = tcp_start_incoming_migration(p);
50 else if (strstart(uri, "exec:", &p))
51 ret = exec_start_incoming_migration(p);
52 else if (strstart(uri, "unix:", &p))
53 ret = unix_start_incoming_migration(p);
54 else if (strstart(uri, "fd:", &p))
55 ret = fd_start_incoming_migration(p);
58 fprintf(stderr, "unknown migration protocol: %s\n", uri);
59 ret = -EPROTONOSUPPORT;
64 void process_incoming_migration(QEMUFile *f)
66 if (qemu_loadvm_state(f) < 0) {
67 fprintf(stderr, "load of migration failed\n");
71 DPRINTF("successfully loaded vm state\n");
76 runstate_set(RUN_STATE_PRELAUNCH);
80 int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
82 FdMigrationState *s = NULL;
84 int detach = qdict_get_try_bool(qdict, "detach", 0);
85 int blk = qdict_get_try_bool(qdict, "blk", 0);
86 int inc = qdict_get_try_bool(qdict, "inc", 0);
87 const char *uri = qdict_get_str(qdict, "uri");
89 if (current_migration &&
90 current_migration->mig_state.get_status(current_migration) ==
92 monitor_printf(mon, "migration already in progress\n");
96 if (qemu_savevm_state_blocked(mon)) {
100 if (strstart(uri, "tcp:", &p)) {
101 s = tcp_start_outgoing_migration(mon, p, max_throttle, detach,
104 } else if (strstart(uri, "exec:", &p)) {
105 s = exec_start_outgoing_migration(mon, p, max_throttle, detach,
107 } else if (strstart(uri, "unix:", &p)) {
108 s = unix_start_outgoing_migration(mon, p, max_throttle, detach,
110 } else if (strstart(uri, "fd:", &p)) {
111 s = fd_start_outgoing_migration(mon, p, max_throttle, detach,
115 monitor_printf(mon, "unknown migration protocol: %s\n", uri);
120 monitor_printf(mon, "migration failed\n");
124 if (current_migration) {
125 current_migration->mig_state.release(current_migration);
128 current_migration = s;
129 notifier_list_notify(&migration_state_notifiers, NULL);
133 int do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data)
135 FdMigrationState *s = current_migration;
137 if (s && s->mig_state.get_status(s) == MIG_STATE_ACTIVE) {
138 s->mig_state.cancel(s);
143 int do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data)
148 d = qdict_get_int(qdict, "value");
154 s = current_migration;
156 qemu_file_set_rate_limit(s->file, max_throttle);
162 /* amount of nanoseconds we are willing to wait for migration to be down.
163 * the choice of nanoseconds is because it is the maximum resolution that
164 * get_clock() can achieve. It is an internal measure. All user-visible
165 * units must be in seconds */
166 static uint64_t max_downtime = 30000000;
168 uint64_t migrate_max_downtime(void)
173 int do_migrate_set_downtime(Monitor *mon, const QDict *qdict,
178 d = qdict_get_double(qdict, "value") * 1e9;
179 d = MAX(0, MIN(UINT64_MAX, d));
180 max_downtime = (uint64_t)d;
185 static void migrate_print_status(Monitor *mon, const char *name,
186 const QDict *status_dict)
190 qdict = qobject_to_qdict(qdict_get(status_dict, name));
192 monitor_printf(mon, "transferred %s: %" PRIu64 " kbytes\n", name,
193 qdict_get_int(qdict, "transferred") >> 10);
194 monitor_printf(mon, "remaining %s: %" PRIu64 " kbytes\n", name,
195 qdict_get_int(qdict, "remaining") >> 10);
196 monitor_printf(mon, "total %s: %" PRIu64 " kbytes\n", name,
197 qdict_get_int(qdict, "total") >> 10);
200 void do_info_migrate_print(Monitor *mon, const QObject *data)
204 qdict = qobject_to_qdict(data);
206 monitor_printf(mon, "Migration status: %s\n",
207 qdict_get_str(qdict, "status"));
209 if (qdict_haskey(qdict, "ram")) {
210 migrate_print_status(mon, "ram", qdict);
213 if (qdict_haskey(qdict, "disk")) {
214 migrate_print_status(mon, "disk", qdict);
218 static void migrate_put_status(QDict *qdict, const char *name,
219 uint64_t trans, uint64_t rem, uint64_t total)
223 obj = qobject_from_jsonf("{ 'transferred': %" PRId64 ", "
224 "'remaining': %" PRId64 ", "
225 "'total': %" PRId64 " }", trans, rem, total);
226 qdict_put_obj(qdict, name, obj);
229 void do_info_migrate(Monitor *mon, QObject **ret_data)
233 if (current_migration) {
234 MigrationState *s = ¤t_migration->mig_state;
236 switch (s->get_status(current_migration)) {
237 case MIG_STATE_ACTIVE:
239 qdict_put(qdict, "status", qstring_from_str("active"));
241 migrate_put_status(qdict, "ram", ram_bytes_transferred(),
242 ram_bytes_remaining(), ram_bytes_total());
244 if (blk_mig_active()) {
245 migrate_put_status(qdict, "disk", blk_mig_bytes_transferred(),
246 blk_mig_bytes_remaining(),
247 blk_mig_bytes_total());
250 *ret_data = QOBJECT(qdict);
252 case MIG_STATE_COMPLETED:
253 *ret_data = qobject_from_jsonf("{ 'status': 'completed' }");
255 case MIG_STATE_ERROR:
256 *ret_data = qobject_from_jsonf("{ 'status': 'failed' }");
258 case MIG_STATE_CANCELLED:
259 *ret_data = qobject_from_jsonf("{ 'status': 'cancelled' }");
265 /* shared migration helpers */
267 void migrate_fd_monitor_suspend(FdMigrationState *s, Monitor *mon)
270 if (monitor_suspend(mon) == 0) {
271 DPRINTF("suspending monitor\n");
273 monitor_printf(mon, "terminal does not allow synchronous "
274 "migration, continuing detached\n");
278 void migrate_fd_error(FdMigrationState *s)
280 DPRINTF("setting error state\n");
281 s->state = MIG_STATE_ERROR;
282 notifier_list_notify(&migration_state_notifiers, NULL);
283 migrate_fd_cleanup(s);
286 int migrate_fd_cleanup(FdMigrationState *s)
290 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
293 DPRINTF("closing file\n");
294 if (qemu_fclose(s->file) != 0) {
300 monitor_resume(s->mon);
312 void migrate_fd_put_notify(void *opaque)
314 FdMigrationState *s = opaque;
316 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
317 qemu_file_put_notify(s->file);
318 if (qemu_file_get_error(s->file)) {
323 ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size)
325 FdMigrationState *s = opaque;
328 if (s->state != MIG_STATE_ACTIVE) {
333 ret = s->write(s, data, size);
334 } while (ret == -1 && ((s->get_error(s)) == EINTR));
337 ret = -(s->get_error(s));
339 if (ret == -EAGAIN) {
340 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
346 void migrate_fd_connect(FdMigrationState *s)
350 s->file = qemu_fopen_ops_buffered(s,
352 migrate_fd_put_buffer,
353 migrate_fd_put_ready,
354 migrate_fd_wait_for_unfreeze,
357 DPRINTF("beginning savevm\n");
358 ret = qemu_savevm_state_begin(s->mon, s->file, s->mig_state.blk,
359 s->mig_state.shared);
361 DPRINTF("failed, %d\n", ret);
366 migrate_fd_put_ready(s);
369 void migrate_fd_put_ready(void *opaque)
371 FdMigrationState *s = opaque;
374 if (s->state != MIG_STATE_ACTIVE) {
375 DPRINTF("put_ready returning because of non-active state\n");
379 DPRINTF("iterate\n");
380 ret = qemu_savevm_state_iterate(s->mon, s->file);
383 } else if (ret == 1) {
384 int old_vm_running = runstate_is_running();
386 DPRINTF("done iterating\n");
387 vm_stop(RUN_STATE_FINISH_MIGRATE);
389 if ((qemu_savevm_state_complete(s->mon, s->file)) < 0) {
390 if (old_vm_running) {
393 s->state = MIG_STATE_ERROR;
395 if (migrate_fd_cleanup(s) < 0) {
396 if (old_vm_running) {
399 s->state = MIG_STATE_ERROR;
401 if (s->state == MIG_STATE_ACTIVE) {
402 s->state = MIG_STATE_COMPLETED;
403 runstate_set(RUN_STATE_POSTMIGRATE);
405 notifier_list_notify(&migration_state_notifiers, NULL);
409 int migrate_fd_get_status(FdMigrationState *s)
414 void migrate_fd_cancel(FdMigrationState *s)
416 if (s->state != MIG_STATE_ACTIVE)
419 DPRINTF("cancelling migration\n");
421 s->state = MIG_STATE_CANCELLED;
422 notifier_list_notify(&migration_state_notifiers, NULL);
423 qemu_savevm_state_cancel(s->mon, s->file);
425 migrate_fd_cleanup(s);
428 void migrate_fd_release(FdMigrationState *s)
431 DPRINTF("releasing state\n");
433 if (s->state == MIG_STATE_ACTIVE) {
434 s->state = MIG_STATE_CANCELLED;
435 notifier_list_notify(&migration_state_notifiers, NULL);
436 migrate_fd_cleanup(s);
441 void migrate_fd_wait_for_unfreeze(void *opaque)
443 FdMigrationState *s = opaque;
446 DPRINTF("wait for unfreeze\n");
447 if (s->state != MIG_STATE_ACTIVE)
454 FD_SET(s->fd, &wfds);
456 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
457 } while (ret == -1 && (s->get_error(s)) == EINTR);
460 qemu_file_set_error(s->file, -s->get_error(s));
464 int migrate_fd_close(void *opaque)
466 FdMigrationState *s = opaque;
469 monitor_resume(s->mon);
471 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
475 void add_migration_state_change_notifier(Notifier *notify)
477 notifier_list_add(&migration_state_notifiers, notify);
480 void remove_migration_state_change_notifier(Notifier *notify)
482 notifier_list_remove(&migration_state_notifiers, notify);
485 int get_migration_state(void)
487 if (current_migration) {
488 return migrate_fd_get_status(current_migration);
490 return MIG_STATE_ERROR;