block migration: Report progress also via info migration
[qemu/aliguori-queue.git] / migration.c
blobd6a3e2615ed0fb113594fe51a9c21fdf20c85f22
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"
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)
28 #else
29 #define dprintf(fmt, ...) \
30 do { } while (0)
31 #endif
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)
40 const char *p;
42 if (strstart(uri, "tcp:", &p))
43 tcp_start_incoming_migration(p);
44 #if !defined(WIN32)
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);
51 #endif
52 else
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;
59 const char *p;
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");
66 return;
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"));
73 #if !defined(WIN32)
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"));
86 #endif
87 else
88 monitor_printf(mon, "unknown migration protocol: %s\n", uri);
90 if (s == NULL)
91 monitor_printf(mon, "migration failed\n");
92 else {
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;
104 if (s)
105 s->cancel(s);
108 void do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data)
110 double d;
111 char *ptr;
112 FdMigrationState *s;
113 const char *value = qdict_get_str(qdict, "value");
115 d = strtod(value, &ptr);
116 switch (*ptr) {
117 case 'G': case 'g':
118 d *= 1024;
119 case 'M': case 'm':
120 d *= 1024;
121 case 'K': case 'k':
122 d *= 1024;
123 default:
124 break;
127 max_throttle = (uint32_t)d;
129 s = migrate_to_fms(current_migration);
130 if (s && s->file) {
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)
143 return max_downtime;
146 void do_migrate_set_downtime(Monitor *mon, const QDict *qdict)
148 char *ptr;
149 double d;
150 const char *value = qdict_get_str(qdict, "value");
152 d = strtod(value, &ptr);
153 if (!strcmp(ptr,"ms")) {
154 d *= 1000000;
155 } else if (!strcmp(ptr,"us")) {
156 d *= 1000;
157 } else if (!strcmp(ptr,"ns")) {
158 } else {
159 /* all else considered to be seconds */
160 d *= 1000000000;
163 max_downtime = (uint64_t)d;
166 void do_info_migrate(Monitor *mon)
168 MigrationState *s = current_migration;
170 if (s) {
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);
186 break;
187 case MIG_STATE_COMPLETED:
188 monitor_printf(mon, "completed\n");
189 break;
190 case MIG_STATE_ERROR:
191 monitor_printf(mon, "failed\n");
192 break;
193 case MIG_STATE_CANCELLED:
194 monitor_printf(mon, "cancelled\n");
195 break;
200 /* shared migration helpers */
202 void migrate_fd_monitor_suspend(FdMigrationState *s, Monitor *mon)
204 s->mon = mon;
205 if (monitor_suspend(mon) == 0) {
206 dprintf("suspending monitor\n");
207 } else {
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);
224 if (s->file) {
225 dprintf("closing file\n");
226 qemu_fclose(s->file);
227 s->file = NULL;
230 if (s->fd != -1)
231 close(s->fd);
233 /* Don't resume monitor until we've flushed all of the buffers */
234 if (s->mon) {
235 monitor_resume(s->mon);
238 s->fd = -1;
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;
252 ssize_t ret;
254 do {
255 ret = s->write(s, data, size);
256 } while (ret == -1 && ((s->get_error(s)) == EINTR));
258 if (ret == -1)
259 ret = -(s->get_error(s));
261 if (ret == -EAGAIN)
262 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
264 return ret;
267 void migrate_fd_connect(FdMigrationState *s)
269 int ret;
271 s->file = qemu_fopen_ops_buffered(s,
272 s->bandwidth_limit,
273 migrate_fd_put_buffer,
274 migrate_fd_put_ready,
275 migrate_fd_wait_for_unfreeze,
276 migrate_fd_close);
278 dprintf("beginning savevm\n");
279 ret = qemu_savevm_state_begin(s->mon, s->file, s->mig_state.blk,
280 s->mig_state.shared);
281 if (ret < 0) {
282 dprintf("failed, %d\n", ret);
283 migrate_fd_error(s);
284 return;
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");
296 return;
299 dprintf("iterate\n");
300 if (qemu_savevm_state_iterate(s->mon, s->file) == 1) {
301 int state;
302 int old_vm_running = vm_running;
304 dprintf("done iterating\n");
305 vm_stop(0);
307 qemu_aio_flush();
308 bdrv_flush_all();
309 if ((qemu_savevm_state_complete(s->mon, s->file)) < 0) {
310 if (old_vm_running) {
311 vm_start();
313 state = MIG_STATE_ERROR;
314 } else {
315 state = MIG_STATE_COMPLETED;
317 migrate_fd_cleanup(s);
318 s->state = state;
322 int migrate_fd_get_status(MigrationState *mig_state)
324 FdMigrationState *s = migrate_to_fms(mig_state);
325 return s->state;
328 void migrate_fd_cancel(MigrationState *mig_state)
330 FdMigrationState *s = migrate_to_fms(mig_state);
332 if (s->state != MIG_STATE_ACTIVE)
333 return;
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);
353 free(s);
356 void migrate_fd_wait_for_unfreeze(void *opaque)
358 FdMigrationState *s = opaque;
359 int ret;
361 dprintf("wait for unfreeze\n");
362 if (s->state != MIG_STATE_ACTIVE)
363 return;
365 do {
366 fd_set wfds;
368 FD_ZERO(&wfds);
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);
380 return s->close(s);