]> git.proxmox.com Git - qemu.git/blob - migration.c
block migration: Add support for restore progress reporting
[qemu.git] / migration.c
1 /*
2 * QEMU live migration
3 *
4 * Copyright IBM, Corp. 2008
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
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"
22
23 //#define DEBUG_MIGRATION
24
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
32
33 /* Migration speed throttling */
34 static uint32_t max_throttle = (32 << 20);
35
36 static MigrationState *current_migration;
37
38 void qemu_start_incoming_migration(const char *uri)
39 {
40 const char *p;
41
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);
54 }
55
56 void do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
57 {
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");
62
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;
67 }
68
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);
89
90 if (s == NULL)
91 monitor_printf(mon, "migration failed\n");
92 else {
93 if (current_migration)
94 current_migration->release(current_migration);
95
96 current_migration = s;
97 }
98 }
99
100 void do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data)
101 {
102 MigrationState *s = current_migration;
103
104 if (s)
105 s->cancel(s);
106 }
107
108 void do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data)
109 {
110 double d;
111 char *ptr;
112 FdMigrationState *s;
113 const char *value = qdict_get_str(qdict, "value");
114
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;
125 }
126
127 max_throttle = (uint32_t)d;
128
129 s = migrate_to_fms(current_migration);
130 if (s && s->file) {
131 qemu_file_set_rate_limit(s->file, max_throttle);
132 }
133 }
134
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;
140
141 uint64_t migrate_max_downtime(void)
142 {
143 return max_downtime;
144 }
145
146 void do_migrate_set_downtime(Monitor *mon, const QDict *qdict)
147 {
148 char *ptr;
149 double d;
150 const char *value = qdict_get_str(qdict, "value");
151
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;
161 }
162
163 max_downtime = (uint64_t)d;
164 }
165
166 void do_info_migrate(Monitor *mon)
167 {
168 MigrationState *s = current_migration;
169
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);
185 }
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;
196 }
197 }
198 }
199
200 /* shared migration helpers */
201
202 void migrate_fd_monitor_suspend(FdMigrationState *s, Monitor *mon)
203 {
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");
210 }
211 }
212
213 void migrate_fd_error(FdMigrationState *s)
214 {
215 dprintf("setting error state\n");
216 s->state = MIG_STATE_ERROR;
217 migrate_fd_cleanup(s);
218 }
219
220 void migrate_fd_cleanup(FdMigrationState *s)
221 {
222 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
223
224 if (s->file) {
225 dprintf("closing file\n");
226 qemu_fclose(s->file);
227 s->file = NULL;
228 }
229
230 if (s->fd != -1)
231 close(s->fd);
232
233 /* Don't resume monitor until we've flushed all of the buffers */
234 if (s->mon) {
235 monitor_resume(s->mon);
236 }
237
238 s->fd = -1;
239 }
240
241 void migrate_fd_put_notify(void *opaque)
242 {
243 FdMigrationState *s = opaque;
244
245 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
246 qemu_file_put_notify(s->file);
247 }
248
249 ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size)
250 {
251 FdMigrationState *s = opaque;
252 ssize_t ret;
253
254 do {
255 ret = s->write(s, data, size);
256 } while (ret == -1 && ((s->get_error(s)) == EINTR));
257
258 if (ret == -1)
259 ret = -(s->get_error(s));
260
261 if (ret == -EAGAIN)
262 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
263
264 return ret;
265 }
266
267 void migrate_fd_connect(FdMigrationState *s)
268 {
269 int ret;
270
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);
277
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;
285 }
286
287 migrate_fd_put_ready(s);
288 }
289
290 void migrate_fd_put_ready(void *opaque)
291 {
292 FdMigrationState *s = opaque;
293
294 if (s->state != MIG_STATE_ACTIVE) {
295 dprintf("put_ready returning because of non-active state\n");
296 return;
297 }
298
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;
303
304 dprintf("done iterating\n");
305 vm_stop(0);
306
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();
312 }
313 state = MIG_STATE_ERROR;
314 } else {
315 state = MIG_STATE_COMPLETED;
316 }
317 migrate_fd_cleanup(s);
318 s->state = state;
319 }
320 }
321
322 int migrate_fd_get_status(MigrationState *mig_state)
323 {
324 FdMigrationState *s = migrate_to_fms(mig_state);
325 return s->state;
326 }
327
328 void migrate_fd_cancel(MigrationState *mig_state)
329 {
330 FdMigrationState *s = migrate_to_fms(mig_state);
331
332 if (s->state != MIG_STATE_ACTIVE)
333 return;
334
335 dprintf("cancelling migration\n");
336
337 s->state = MIG_STATE_CANCELLED;
338 qemu_savevm_state_cancel(s->mon, s->file);
339
340 migrate_fd_cleanup(s);
341 }
342
343 void migrate_fd_release(MigrationState *mig_state)
344 {
345 FdMigrationState *s = migrate_to_fms(mig_state);
346
347 dprintf("releasing state\n");
348
349 if (s->state == MIG_STATE_ACTIVE) {
350 s->state = MIG_STATE_CANCELLED;
351 migrate_fd_cleanup(s);
352 }
353 free(s);
354 }
355
356 void migrate_fd_wait_for_unfreeze(void *opaque)
357 {
358 FdMigrationState *s = opaque;
359 int ret;
360
361 dprintf("wait for unfreeze\n");
362 if (s->state != MIG_STATE_ACTIVE)
363 return;
364
365 do {
366 fd_set wfds;
367
368 FD_ZERO(&wfds);
369 FD_SET(s->fd, &wfds);
370
371 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
372 } while (ret == -1 && (s->get_error(s)) == EINTR);
373 }
374
375 int migrate_fd_close(void *opaque)
376 {
377 FdMigrationState *s = opaque;
378
379 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
380 return s->close(s);
381 }