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"
23 //#define DEBUG_MIGRATION
25 #ifdef DEBUG_MIGRATION
26 #define dprintf(fmt, ...) \
27 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
29 #define dprintf(fmt, ...) \
33 /* Migration speed throttling */
34 static uint32_t max_throttle = (32 << 20);
36 static MigrationState *current_migration;
38 void qemu_start_incoming_migration(const char *uri)
42 if (strstart(uri, "tcp:", &p))
43 tcp_start_incoming_migration(p);
45 else if (strstart(uri, "exec:", &p))
46 exec_start_incoming_migration(p);
47 else if (strstart(uri, "unix:", &p))
48 unix_start_incoming_migration(p);
49 else if (strstart(uri, "fd:", &p))
50 fd_start_incoming_migration(p);
53 fprintf(stderr, "unknown migration protocol: %s\n", uri);
56 void do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
58 MigrationState *s = NULL;
60 int detach = qdict_get_int(qdict, "detach");
61 const char *uri = qdict_get_str(qdict, "uri");
63 if (current_migration &&
64 current_migration->get_status(current_migration) == MIG_STATE_ACTIVE) {
65 monitor_printf(mon, "migration already in progress\n");
69 if (strstart(uri, "tcp:", &p))
70 s = tcp_start_outgoing_migration(mon, p, max_throttle, detach,
71 (int)qdict_get_int(qdict, "blk"),
72 (int)qdict_get_int(qdict, "inc"));
74 else if (strstart(uri, "exec:", &p))
75 s = exec_start_outgoing_migration(mon, p, max_throttle, detach,
76 (int)qdict_get_int(qdict, "blk"),
77 (int)qdict_get_int(qdict, "inc"));
78 else if (strstart(uri, "unix:", &p))
79 s = unix_start_outgoing_migration(mon, p, max_throttle, detach,
80 (int)qdict_get_int(qdict, "blk"),
81 (int)qdict_get_int(qdict, "inc"));
82 else if (strstart(uri, "fd:", &p))
83 s = fd_start_outgoing_migration(mon, p, max_throttle, detach,
84 (int)qdict_get_int(qdict, "blk"),
85 (int)qdict_get_int(qdict, "inc"));
88 monitor_printf(mon, "unknown migration protocol: %s\n", uri);
91 monitor_printf(mon, "migration failed\n");
93 if (current_migration)
94 current_migration->release(current_migration);
96 current_migration = s;
100 void do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data)
102 MigrationState *s = current_migration;
108 void do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data)
113 const char *value = qdict_get_str(qdict, "value");
115 d = strtod(value, &ptr);
127 max_throttle = (uint32_t)d;
129 s = migrate_to_fms(current_migration);
131 qemu_file_set_rate_limit(s->file, max_throttle);
135 /* amount of nanoseconds we are willing to wait for migration to be down.
136 * the choice of nanoseconds is because it is the maximum resolution that
137 * get_clock() can achieve. It is an internal measure. All user-visible
138 * units must be in seconds */
139 static uint64_t max_downtime = 30000000;
141 uint64_t migrate_max_downtime(void)
146 void do_migrate_set_downtime(Monitor *mon, const QDict *qdict)
150 const char *value = qdict_get_str(qdict, "value");
152 d = strtod(value, &ptr);
153 if (!strcmp(ptr,"ms")) {
155 } else if (!strcmp(ptr,"us")) {
157 } else if (!strcmp(ptr,"ns")) {
159 /* all else considered to be seconds */
163 max_downtime = (uint64_t)d;
166 void do_info_migrate(Monitor *mon)
168 MigrationState *s = current_migration;
171 monitor_printf(mon, "Migration status: ");
172 switch (s->get_status(s)) {
173 case MIG_STATE_ACTIVE:
174 monitor_printf(mon, "active\n");
175 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n", ram_bytes_transferred() >> 10);
176 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n", ram_bytes_remaining() >> 10);
177 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n", ram_bytes_total() >> 10);
178 if (blk_mig_active()) {
179 monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
180 blk_mig_bytes_transferred() >> 10);
181 monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
182 blk_mig_bytes_remaining() >> 10);
183 monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
184 blk_mig_bytes_total() >> 10);
187 case MIG_STATE_COMPLETED:
188 monitor_printf(mon, "completed\n");
190 case MIG_STATE_ERROR:
191 monitor_printf(mon, "failed\n");
193 case MIG_STATE_CANCELLED:
194 monitor_printf(mon, "cancelled\n");
200 /* shared migration helpers */
202 void migrate_fd_monitor_suspend(FdMigrationState *s, Monitor *mon)
205 if (monitor_suspend(mon) == 0) {
206 dprintf("suspending monitor\n");
208 monitor_printf(mon, "terminal does not allow synchronous "
209 "migration, continuing detached\n");
213 void migrate_fd_error(FdMigrationState *s)
215 dprintf("setting error state\n");
216 s->state = MIG_STATE_ERROR;
217 migrate_fd_cleanup(s);
220 void migrate_fd_cleanup(FdMigrationState *s)
222 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
225 dprintf("closing file\n");
226 qemu_fclose(s->file);
233 /* Don't resume monitor until we've flushed all of the buffers */
235 monitor_resume(s->mon);
241 void migrate_fd_put_notify(void *opaque)
243 FdMigrationState *s = opaque;
245 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
246 qemu_file_put_notify(s->file);
249 ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size)
251 FdMigrationState *s = opaque;
255 ret = s->write(s, data, size);
256 } while (ret == -1 && ((s->get_error(s)) == EINTR));
259 ret = -(s->get_error(s));
262 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
267 void migrate_fd_connect(FdMigrationState *s)
271 s->file = qemu_fopen_ops_buffered(s,
273 migrate_fd_put_buffer,
274 migrate_fd_put_ready,
275 migrate_fd_wait_for_unfreeze,
278 dprintf("beginning savevm\n");
279 ret = qemu_savevm_state_begin(s->mon, s->file, s->mig_state.blk,
280 s->mig_state.shared);
282 dprintf("failed, %d\n", ret);
287 migrate_fd_put_ready(s);
290 void migrate_fd_put_ready(void *opaque)
292 FdMigrationState *s = opaque;
294 if (s->state != MIG_STATE_ACTIVE) {
295 dprintf("put_ready returning because of non-active state\n");
299 dprintf("iterate\n");
300 if (qemu_savevm_state_iterate(s->mon, s->file) == 1) {
302 int old_vm_running = vm_running;
304 dprintf("done iterating\n");
309 if ((qemu_savevm_state_complete(s->mon, s->file)) < 0) {
310 if (old_vm_running) {
313 state = MIG_STATE_ERROR;
315 state = MIG_STATE_COMPLETED;
317 migrate_fd_cleanup(s);
322 int migrate_fd_get_status(MigrationState *mig_state)
324 FdMigrationState *s = migrate_to_fms(mig_state);
328 void migrate_fd_cancel(MigrationState *mig_state)
330 FdMigrationState *s = migrate_to_fms(mig_state);
332 if (s->state != MIG_STATE_ACTIVE)
335 dprintf("cancelling migration\n");
337 s->state = MIG_STATE_CANCELLED;
338 qemu_savevm_state_cancel(s->mon, s->file);
340 migrate_fd_cleanup(s);
343 void migrate_fd_release(MigrationState *mig_state)
345 FdMigrationState *s = migrate_to_fms(mig_state);
347 dprintf("releasing state\n");
349 if (s->state == MIG_STATE_ACTIVE) {
350 s->state = MIG_STATE_CANCELLED;
351 migrate_fd_cleanup(s);
356 void migrate_fd_wait_for_unfreeze(void *opaque)
358 FdMigrationState *s = opaque;
361 dprintf("wait for unfreeze\n");
362 if (s->state != MIG_STATE_ACTIVE)
369 FD_SET(s->fd, &wfds);
371 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
372 } while (ret == -1 && (s->get_error(s)) == EINTR);
375 int migrate_fd_close(void *opaque)
377 FdMigrationState *s = opaque;
379 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);