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 MigrationState *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");
73 incoming_expected = false;
78 runstate_set(RSTATE_PRE_LAUNCH);
82 int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
84 MigrationState *s = NULL;
86 int detach = qdict_get_try_bool(qdict, "detach", 0);
87 int blk = qdict_get_try_bool(qdict, "blk", 0);
88 int inc = qdict_get_try_bool(qdict, "inc", 0);
89 const char *uri = qdict_get_str(qdict, "uri");
91 if (current_migration &&
92 current_migration->get_status(current_migration) == MIG_STATE_ACTIVE) {
93 monitor_printf(mon, "migration already in progress\n");
97 if (qemu_savevm_state_blocked(mon)) {
101 if (strstart(uri, "tcp:", &p)) {
102 s = tcp_start_outgoing_migration(mon, p, max_throttle, detach,
105 } else if (strstart(uri, "exec:", &p)) {
106 s = exec_start_outgoing_migration(mon, p, max_throttle, detach,
108 } else if (strstart(uri, "unix:", &p)) {
109 s = unix_start_outgoing_migration(mon, p, max_throttle, detach,
111 } else if (strstart(uri, "fd:", &p)) {
112 s = fd_start_outgoing_migration(mon, p, max_throttle, detach,
116 monitor_printf(mon, "unknown migration protocol: %s\n", uri);
121 monitor_printf(mon, "migration failed\n");
125 if (current_migration) {
126 current_migration->release(current_migration);
129 current_migration = s;
130 notifier_list_notify(&migration_state_notifiers, NULL);
134 int do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data)
136 MigrationState *s = current_migration;
144 int do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data)
149 d = qdict_get_int(qdict, "value");
155 s = migrate_to_fms(current_migration);
157 qemu_file_set_rate_limit(s->file, max_throttle);
163 /* amount of nanoseconds we are willing to wait for migration to be down.
164 * the choice of nanoseconds is because it is the maximum resolution that
165 * get_clock() can achieve. It is an internal measure. All user-visible
166 * units must be in seconds */
167 static uint64_t max_downtime = 30000000;
169 uint64_t migrate_max_downtime(void)
174 int do_migrate_set_downtime(Monitor *mon, const QDict *qdict,
179 d = qdict_get_double(qdict, "value") * 1e9;
180 d = MAX(0, MIN(UINT64_MAX, d));
181 max_downtime = (uint64_t)d;
186 static void migrate_print_status(Monitor *mon, const char *name,
187 const QDict *status_dict)
191 qdict = qobject_to_qdict(qdict_get(status_dict, name));
193 monitor_printf(mon, "transferred %s: %" PRIu64 " kbytes\n", name,
194 qdict_get_int(qdict, "transferred") >> 10);
195 monitor_printf(mon, "remaining %s: %" PRIu64 " kbytes\n", name,
196 qdict_get_int(qdict, "remaining") >> 10);
197 monitor_printf(mon, "total %s: %" PRIu64 " kbytes\n", name,
198 qdict_get_int(qdict, "total") >> 10);
201 void do_info_migrate_print(Monitor *mon, const QObject *data)
205 qdict = qobject_to_qdict(data);
207 monitor_printf(mon, "Migration status: %s\n",
208 qdict_get_str(qdict, "status"));
210 if (qdict_haskey(qdict, "ram")) {
211 migrate_print_status(mon, "ram", qdict);
214 if (qdict_haskey(qdict, "disk")) {
215 migrate_print_status(mon, "disk", qdict);
219 static void migrate_put_status(QDict *qdict, const char *name,
220 uint64_t trans, uint64_t rem, uint64_t total)
224 obj = qobject_from_jsonf("{ 'transferred': %" PRId64 ", "
225 "'remaining': %" PRId64 ", "
226 "'total': %" PRId64 " }", trans, rem, total);
227 qdict_put_obj(qdict, name, obj);
230 void do_info_migrate(Monitor *mon, QObject **ret_data)
233 MigrationState *s = current_migration;
236 switch (s->get_status(s)) {
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);
320 ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size)
322 FdMigrationState *s = opaque;
326 ret = s->write(s, data, size);
327 } while (ret == -1 && ((s->get_error(s)) == EINTR));
330 ret = -(s->get_error(s));
332 if (ret == -EAGAIN) {
333 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
334 } else if (ret < 0) {
335 s->state = MIG_STATE_ERROR;
336 notifier_list_notify(&migration_state_notifiers, NULL);
342 void migrate_fd_connect(FdMigrationState *s)
346 s->file = qemu_fopen_ops_buffered(s,
348 migrate_fd_put_buffer,
349 migrate_fd_put_ready,
350 migrate_fd_wait_for_unfreeze,
353 DPRINTF("beginning savevm\n");
354 ret = qemu_savevm_state_begin(s->mon, s->file, s->mig_state.blk,
355 s->mig_state.shared);
357 DPRINTF("failed, %d\n", ret);
362 migrate_fd_put_ready(s);
365 void migrate_fd_put_ready(void *opaque)
367 FdMigrationState *s = opaque;
369 if (s->state != MIG_STATE_ACTIVE) {
370 DPRINTF("put_ready returning because of non-active state\n");
374 DPRINTF("iterate\n");
375 if (qemu_savevm_state_iterate(s->mon, s->file) == 1) {
377 int old_vm_running = vm_running;
379 DPRINTF("done iterating\n");
380 vm_stop(RSTATE_PRE_MIGRATE);
382 if ((qemu_savevm_state_complete(s->mon, s->file)) < 0) {
383 if (old_vm_running) {
386 state = MIG_STATE_ERROR;
388 state = MIG_STATE_COMPLETED;
390 if (migrate_fd_cleanup(s) < 0) {
391 if (old_vm_running) {
394 state = MIG_STATE_ERROR;
396 if (state == MIG_STATE_COMPLETED) {
397 runstate_set(RSTATE_POST_MIGRATE);
400 notifier_list_notify(&migration_state_notifiers, NULL);
404 int migrate_fd_get_status(MigrationState *mig_state)
406 FdMigrationState *s = migrate_to_fms(mig_state);
410 void migrate_fd_cancel(MigrationState *mig_state)
412 FdMigrationState *s = migrate_to_fms(mig_state);
414 if (s->state != MIG_STATE_ACTIVE)
417 DPRINTF("cancelling migration\n");
419 s->state = MIG_STATE_CANCELLED;
420 notifier_list_notify(&migration_state_notifiers, NULL);
421 qemu_savevm_state_cancel(s->mon, s->file);
423 migrate_fd_cleanup(s);
426 void migrate_fd_release(MigrationState *mig_state)
428 FdMigrationState *s = migrate_to_fms(mig_state);
430 DPRINTF("releasing state\n");
432 if (s->state == MIG_STATE_ACTIVE) {
433 s->state = MIG_STATE_CANCELLED;
434 notifier_list_notify(&migration_state_notifiers, NULL);
435 migrate_fd_cleanup(s);
440 void migrate_fd_wait_for_unfreeze(void *opaque)
442 FdMigrationState *s = opaque;
445 DPRINTF("wait for unfreeze\n");
446 if (s->state != MIG_STATE_ACTIVE)
453 FD_SET(s->fd, &wfds);
455 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
456 } while (ret == -1 && (s->get_error(s)) == EINTR);
459 int migrate_fd_close(void *opaque)
461 FdMigrationState *s = opaque;
464 monitor_resume(s->mon);
466 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
470 void add_migration_state_change_notifier(Notifier *notify)
472 notifier_list_add(&migration_state_notifiers, notify);
475 void remove_migration_state_change_notifier(Notifier *notify)
477 notifier_list_remove(&migration_state_notifiers, notify);
480 int get_migration_state(void)
482 if (current_migration) {
483 return migrate_fd_get_status(current_migration);
485 return MIG_STATE_ERROR;