linux-user: fcntl fixes for LTP
[qemu.git] / migration.c
blob34e2bc14c0d97fb6b326a1c455dd1a6d26e1f937
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 #endif
49 else
50 fprintf(stderr, "unknown migration protocol: %s\n", uri);
53 void do_migrate(Monitor *mon, int detach, const char *uri)
55 MigrationState *s = NULL;
56 const char *p;
58 if (strstart(uri, "tcp:", &p))
59 s = tcp_start_outgoing_migration(p, max_throttle, detach);
60 #if !defined(WIN32)
61 else if (strstart(uri, "exec:", &p))
62 s = exec_start_outgoing_migration(p, max_throttle, detach);
63 else if (strstart(uri, "unix:", &p))
64 s = unix_start_outgoing_migration(p, max_throttle, detach);
65 #endif
66 else
67 monitor_printf(mon, "unknown migration protocol: %s\n", uri);
69 if (s == NULL)
70 monitor_printf(mon, "migration failed\n");
71 else {
72 if (current_migration)
73 current_migration->release(current_migration);
75 current_migration = s;
79 void do_migrate_cancel(Monitor *mon)
81 MigrationState *s = current_migration;
83 if (s)
84 s->cancel(s);
87 void do_migrate_set_speed(Monitor *mon, const char *value)
89 double d;
90 char *ptr;
91 FdMigrationState *s;
93 d = strtod(value, &ptr);
94 switch (*ptr) {
95 case 'G': case 'g':
96 d *= 1024;
97 case 'M': case 'm':
98 d *= 1024;
99 case 'K': case 'k':
100 d *= 1024;
101 default:
102 break;
105 max_throttle = (uint32_t)d;
106 s = migrate_to_fms(current_migration);
108 if (s) {
109 qemu_file_set_rate_limit(s->file, max_throttle);
114 /* amount of nanoseconds we are willing to wait for migration to be down.
115 * the choice of nanoseconds is because it is the maximum resolution that
116 * get_clock() can achieve. It is an internal measure. All user-visible
117 * units must be in seconds */
118 static uint64_t max_downtime = 30000000;
120 uint64_t migrate_max_downtime(void)
122 return max_downtime;
125 void do_migrate_set_downtime(Monitor *mon, const char *value)
127 char *ptr;
128 double d;
130 d = strtod(value, &ptr);
131 if (!strcmp(ptr,"ms")) {
132 d *= 1000000;
133 } else if (!strcmp(ptr,"us")) {
134 d *= 1000;
135 } else if (!strcmp(ptr,"ns")) {
136 } else {
137 /* all else considered to be seconds */
138 d *= 1000000000;
141 max_downtime = (uint64_t)d;
144 void do_info_migrate(Monitor *mon)
146 MigrationState *s = current_migration;
148 if (s) {
149 monitor_printf(mon, "Migration status: ");
150 switch (s->get_status(s)) {
151 case MIG_STATE_ACTIVE:
152 monitor_printf(mon, "active\n");
153 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n", ram_bytes_transferred() >> 10);
154 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n", ram_bytes_remaining() >> 10);
155 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n", ram_bytes_total() >> 10);
156 break;
157 case MIG_STATE_COMPLETED:
158 monitor_printf(mon, "completed\n");
159 break;
160 case MIG_STATE_ERROR:
161 monitor_printf(mon, "failed\n");
162 break;
163 case MIG_STATE_CANCELLED:
164 monitor_printf(mon, "cancelled\n");
165 break;
170 /* shared migration helpers */
172 void migrate_fd_monitor_suspend(FdMigrationState *s)
174 s->mon_resume = cur_mon;
175 if (monitor_suspend(cur_mon) == 0)
176 dprintf("suspending monitor\n");
177 else
178 monitor_printf(cur_mon, "terminal does not allow synchronous "
179 "migration, continuing detached\n");
182 void migrate_fd_error(FdMigrationState *s)
184 dprintf("setting error state\n");
185 s->state = MIG_STATE_ERROR;
186 migrate_fd_cleanup(s);
189 void migrate_fd_cleanup(FdMigrationState *s)
191 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
193 if (s->file) {
194 dprintf("closing file\n");
195 qemu_fclose(s->file);
198 if (s->fd != -1)
199 close(s->fd);
201 /* Don't resume monitor until we've flushed all of the buffers */
202 if (s->mon_resume)
203 monitor_resume(s->mon_resume);
205 s->fd = -1;
208 void migrate_fd_put_notify(void *opaque)
210 FdMigrationState *s = opaque;
212 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
213 qemu_file_put_notify(s->file);
216 ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size)
218 FdMigrationState *s = opaque;
219 ssize_t ret;
221 do {
222 ret = s->write(s, data, size);
223 } while (ret == -1 && ((s->get_error(s)) == EINTR));
225 if (ret == -1)
226 ret = -(s->get_error(s));
228 if (ret == -EAGAIN)
229 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
231 return ret;
234 void migrate_fd_connect(FdMigrationState *s)
236 int ret;
238 s->file = qemu_fopen_ops_buffered(s,
239 s->bandwidth_limit,
240 migrate_fd_put_buffer,
241 migrate_fd_put_ready,
242 migrate_fd_wait_for_unfreeze,
243 migrate_fd_close);
245 dprintf("beginning savevm\n");
246 ret = qemu_savevm_state_begin(s->file);
247 if (ret < 0) {
248 dprintf("failed, %d\n", ret);
249 migrate_fd_error(s);
250 return;
253 migrate_fd_put_ready(s);
256 void migrate_fd_put_ready(void *opaque)
258 FdMigrationState *s = opaque;
260 if (s->state != MIG_STATE_ACTIVE) {
261 dprintf("put_ready returning because of non-active state\n");
262 return;
265 dprintf("iterate\n");
266 if (qemu_savevm_state_iterate(s->file) == 1) {
267 int state;
268 int old_vm_running = vm_running;
270 dprintf("done iterating\n");
271 vm_stop(0);
273 qemu_aio_flush();
274 bdrv_flush_all();
275 if ((qemu_savevm_state_complete(s->file)) < 0) {
276 if (old_vm_running) {
277 vm_start();
279 state = MIG_STATE_ERROR;
280 } else {
281 state = MIG_STATE_COMPLETED;
283 migrate_fd_cleanup(s);
284 s->state = state;
288 int migrate_fd_get_status(MigrationState *mig_state)
290 FdMigrationState *s = migrate_to_fms(mig_state);
291 return s->state;
294 void migrate_fd_cancel(MigrationState *mig_state)
296 FdMigrationState *s = migrate_to_fms(mig_state);
298 if (s->state != MIG_STATE_ACTIVE)
299 return;
301 dprintf("cancelling migration\n");
303 s->state = MIG_STATE_CANCELLED;
305 migrate_fd_cleanup(s);
308 void migrate_fd_release(MigrationState *mig_state)
310 FdMigrationState *s = migrate_to_fms(mig_state);
312 dprintf("releasing state\n");
314 if (s->state == MIG_STATE_ACTIVE) {
315 s->state = MIG_STATE_CANCELLED;
316 migrate_fd_cleanup(s);
318 free(s);
321 void migrate_fd_wait_for_unfreeze(void *opaque)
323 FdMigrationState *s = opaque;
324 int ret;
326 dprintf("wait for unfreeze\n");
327 if (s->state != MIG_STATE_ACTIVE)
328 return;
330 do {
331 fd_set wfds;
333 FD_ZERO(&wfds);
334 FD_SET(s->fd, &wfds);
336 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
337 } while (ret == -1 && (s->get_error(s)) == EINTR);
340 int migrate_fd_close(void *opaque)
342 FdMigrationState *s = opaque;
344 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
345 return s->close(s);