live migration: Allow cleanup after cancellation or error
[qemu/mdroth.git] / migration.c
blobf8a15fb48594f7300131e35f0aba06ac6400e307
1 /*
2 * QEMU live migration
4 * Copyright IBM, Corp. 2008
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
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"
16 #include "monitor.h"
17 #include "buffered_file.h"
18 #include "sysemu.h"
19 #include "block.h"
20 #include "qemu_socket.h"
22 //#define DEBUG_MIGRATION
24 #ifdef DEBUG_MIGRATION
25 #define dprintf(fmt, ...) \
26 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
27 #else
28 #define dprintf(fmt, ...) \
29 do { } while (0)
30 #endif
32 /* Migration speed throttling */
33 static uint32_t max_throttle = (32 << 20);
35 static MigrationState *current_migration;
37 void qemu_start_incoming_migration(const char *uri)
39 const char *p;
41 if (strstart(uri, "tcp:", &p))
42 tcp_start_incoming_migration(p);
43 #if !defined(WIN32)
44 else if (strstart(uri, "exec:", &p))
45 exec_start_incoming_migration(p);
46 else if (strstart(uri, "unix:", &p))
47 unix_start_incoming_migration(p);
48 else if (strstart(uri, "fd:", &p))
49 fd_start_incoming_migration(p);
50 #endif
51 else
52 fprintf(stderr, "unknown migration protocol: %s\n", uri);
55 void do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
57 MigrationState *s = NULL;
58 const char *p;
59 int detach = qdict_get_int(qdict, "detach");
60 const char *uri = qdict_get_str(qdict, "uri");
62 if (current_migration &&
63 current_migration->get_status(current_migration) == MIG_STATE_ACTIVE) {
64 monitor_printf(mon, "migration already in progress\n");
65 return;
68 if (strstart(uri, "tcp:", &p))
69 s = tcp_start_outgoing_migration(p, max_throttle, detach,
70 (int)qdict_get_int(qdict, "blk"),
71 (int)qdict_get_int(qdict, "inc"));
72 #if !defined(WIN32)
73 else if (strstart(uri, "exec:", &p))
74 s = exec_start_outgoing_migration(p, max_throttle, detach,
75 (int)qdict_get_int(qdict, "blk"),
76 (int)qdict_get_int(qdict, "inc"));
77 else if (strstart(uri, "unix:", &p))
78 s = unix_start_outgoing_migration(p, max_throttle, detach,
79 (int)qdict_get_int(qdict, "blk"),
80 (int)qdict_get_int(qdict, "inc"));
81 else if (strstart(uri, "fd:", &p))
82 s = fd_start_outgoing_migration(mon, p, max_throttle, detach,
83 (int)qdict_get_int(qdict, "blk"),
84 (int)qdict_get_int(qdict, "inc"));
85 #endif
86 else
87 monitor_printf(mon, "unknown migration protocol: %s\n", uri);
89 if (s == NULL)
90 monitor_printf(mon, "migration failed\n");
91 else {
92 if (current_migration)
93 current_migration->release(current_migration);
95 current_migration = s;
99 void do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data)
101 MigrationState *s = current_migration;
103 if (s)
104 s->cancel(s);
107 void do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data)
109 double d;
110 char *ptr;
111 FdMigrationState *s;
112 const char *value = qdict_get_str(qdict, "value");
114 d = strtod(value, &ptr);
115 switch (*ptr) {
116 case 'G': case 'g':
117 d *= 1024;
118 case 'M': case 'm':
119 d *= 1024;
120 case 'K': case 'k':
121 d *= 1024;
122 default:
123 break;
126 max_throttle = (uint32_t)d;
128 s = migrate_to_fms(current_migration);
129 if (s && s->file) {
130 qemu_file_set_rate_limit(s->file, max_throttle);
134 /* amount of nanoseconds we are willing to wait for migration to be down.
135 * the choice of nanoseconds is because it is the maximum resolution that
136 * get_clock() can achieve. It is an internal measure. All user-visible
137 * units must be in seconds */
138 static uint64_t max_downtime = 30000000;
140 uint64_t migrate_max_downtime(void)
142 return max_downtime;
145 void do_migrate_set_downtime(Monitor *mon, const QDict *qdict)
147 char *ptr;
148 double d;
149 const char *value = qdict_get_str(qdict, "value");
151 d = strtod(value, &ptr);
152 if (!strcmp(ptr,"ms")) {
153 d *= 1000000;
154 } else if (!strcmp(ptr,"us")) {
155 d *= 1000;
156 } else if (!strcmp(ptr,"ns")) {
157 } else {
158 /* all else considered to be seconds */
159 d *= 1000000000;
162 max_downtime = (uint64_t)d;
165 void do_info_migrate(Monitor *mon)
167 MigrationState *s = current_migration;
169 if (s) {
170 monitor_printf(mon, "Migration status: ");
171 switch (s->get_status(s)) {
172 case MIG_STATE_ACTIVE:
173 monitor_printf(mon, "active\n");
174 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n", ram_bytes_transferred() >> 10);
175 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n", ram_bytes_remaining() >> 10);
176 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n", ram_bytes_total() >> 10);
177 break;
178 case MIG_STATE_COMPLETED:
179 monitor_printf(mon, "completed\n");
180 break;
181 case MIG_STATE_ERROR:
182 monitor_printf(mon, "failed\n");
183 break;
184 case MIG_STATE_CANCELLED:
185 monitor_printf(mon, "cancelled\n");
186 break;
191 /* shared migration helpers */
193 void migrate_fd_monitor_suspend(FdMigrationState *s)
195 s->mon_resume = cur_mon;
196 if (monitor_suspend(cur_mon) == 0)
197 dprintf("suspending monitor\n");
198 else
199 monitor_printf(cur_mon, "terminal does not allow synchronous "
200 "migration, continuing detached\n");
203 void migrate_fd_error(FdMigrationState *s)
205 dprintf("setting error state\n");
206 s->state = MIG_STATE_ERROR;
207 migrate_fd_cleanup(s);
210 void migrate_fd_cleanup(FdMigrationState *s)
212 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
214 if (s->file) {
215 dprintf("closing file\n");
216 qemu_fclose(s->file);
217 s->file = NULL;
220 if (s->fd != -1)
221 close(s->fd);
223 /* Don't resume monitor until we've flushed all of the buffers */
224 if (s->mon_resume)
225 monitor_resume(s->mon_resume);
227 s->fd = -1;
230 void migrate_fd_put_notify(void *opaque)
232 FdMigrationState *s = opaque;
234 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
235 qemu_file_put_notify(s->file);
238 ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size)
240 FdMigrationState *s = opaque;
241 ssize_t ret;
243 do {
244 ret = s->write(s, data, size);
245 } while (ret == -1 && ((s->get_error(s)) == EINTR));
247 if (ret == -1)
248 ret = -(s->get_error(s));
250 if (ret == -EAGAIN)
251 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
253 return ret;
256 void migrate_fd_connect(FdMigrationState *s)
258 int ret;
260 s->file = qemu_fopen_ops_buffered(s,
261 s->bandwidth_limit,
262 migrate_fd_put_buffer,
263 migrate_fd_put_ready,
264 migrate_fd_wait_for_unfreeze,
265 migrate_fd_close);
267 dprintf("beginning savevm\n");
268 ret = qemu_savevm_state_begin(s->file, s->mig_state.blk,
269 s->mig_state.shared);
270 if (ret < 0) {
271 dprintf("failed, %d\n", ret);
272 migrate_fd_error(s);
273 return;
276 migrate_fd_put_ready(s);
279 void migrate_fd_put_ready(void *opaque)
281 FdMigrationState *s = opaque;
283 if (s->state != MIG_STATE_ACTIVE) {
284 dprintf("put_ready returning because of non-active state\n");
285 return;
288 dprintf("iterate\n");
289 if (qemu_savevm_state_iterate(s->file) == 1) {
290 int state;
291 int old_vm_running = vm_running;
293 dprintf("done iterating\n");
294 vm_stop(0);
296 qemu_aio_flush();
297 bdrv_flush_all();
298 if ((qemu_savevm_state_complete(s->file)) < 0) {
299 if (old_vm_running) {
300 vm_start();
302 state = MIG_STATE_ERROR;
303 } else {
304 state = MIG_STATE_COMPLETED;
306 migrate_fd_cleanup(s);
307 s->state = state;
311 int migrate_fd_get_status(MigrationState *mig_state)
313 FdMigrationState *s = migrate_to_fms(mig_state);
314 return s->state;
317 void migrate_fd_cancel(MigrationState *mig_state)
319 FdMigrationState *s = migrate_to_fms(mig_state);
321 if (s->state != MIG_STATE_ACTIVE)
322 return;
324 dprintf("cancelling migration\n");
326 s->state = MIG_STATE_CANCELLED;
327 qemu_savevm_state_cancel(s->file);
329 migrate_fd_cleanup(s);
332 void migrate_fd_release(MigrationState *mig_state)
334 FdMigrationState *s = migrate_to_fms(mig_state);
336 dprintf("releasing state\n");
338 if (s->state == MIG_STATE_ACTIVE) {
339 s->state = MIG_STATE_CANCELLED;
340 migrate_fd_cleanup(s);
342 free(s);
345 void migrate_fd_wait_for_unfreeze(void *opaque)
347 FdMigrationState *s = opaque;
348 int ret;
350 dprintf("wait for unfreeze\n");
351 if (s->state != MIG_STATE_ACTIVE)
352 return;
354 do {
355 fd_set wfds;
357 FD_ZERO(&wfds);
358 FD_SET(s->fd, &wfds);
360 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
361 } while (ret == -1 && (s->get_error(s)) == EINTR);
364 int migrate_fd_close(void *opaque)
366 FdMigrationState *s = opaque;
368 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
369 return s->close(s);