]> git.proxmox.com Git - qemu.git/blame - migration.c
QError: Don't abort on multiple faults
[qemu.git] / migration.c
CommitLineData
5bb7910a
AL
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"
376253ec 16#include "monitor.h"
065e2813
AL
17#include "buffered_file.h"
18#include "sysemu.h"
19#include "block.h"
20#include "qemu_socket.h"
25f23643 21#include "block-migration.h"
c86a6683 22#include "qemu-objects.h"
065e2813
AL
23
24//#define DEBUG_MIGRATION
25
26#ifdef DEBUG_MIGRATION
d0f2c4c6 27#define DPRINTF(fmt, ...) \
065e2813
AL
28 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
29#else
d0f2c4c6 30#define DPRINTF(fmt, ...) \
065e2813
AL
31 do { } while (0)
32#endif
5bb7910a
AL
33
34/* Migration speed throttling */
35static uint32_t max_throttle = (32 << 20);
36
37static MigrationState *current_migration;
38
39void qemu_start_incoming_migration(const char *uri)
40{
34c9dd8e
AL
41 const char *p;
42
43 if (strstart(uri, "tcp:", &p))
44 tcp_start_incoming_migration(p);
065e2813
AL
45#if !defined(WIN32)
46 else if (strstart(uri, "exec:", &p))
47 exec_start_incoming_migration(p);
4951f65b
CL
48 else if (strstart(uri, "unix:", &p))
49 unix_start_incoming_migration(p);
5ac1fad3
PB
50 else if (strstart(uri, "fd:", &p))
51 fd_start_incoming_migration(p);
065e2813 52#endif
34c9dd8e
AL
53 else
54 fprintf(stderr, "unknown migration protocol: %s\n", uri);
5bb7910a
AL
55}
56
5f79da00 57void do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
5bb7910a 58{
34c9dd8e
AL
59 MigrationState *s = NULL;
60 const char *p;
f18c16de
LC
61 int detach = qdict_get_int(qdict, "detach");
62 const char *uri = qdict_get_str(qdict, "uri");
1302425d
JK
63
64 if (current_migration &&
65 current_migration->get_status(current_migration) == MIG_STATE_ACTIVE) {
66 monitor_printf(mon, "migration already in progress\n");
67 return;
68 }
69
34c9dd8e 70 if (strstart(uri, "tcp:", &p))
f327aa0c 71 s = tcp_start_outgoing_migration(mon, p, max_throttle, detach,
c163b5ca 72 (int)qdict_get_int(qdict, "blk"),
73 (int)qdict_get_int(qdict, "inc"));
065e2813
AL
74#if !defined(WIN32)
75 else if (strstart(uri, "exec:", &p))
f327aa0c 76 s = exec_start_outgoing_migration(mon, p, max_throttle, detach,
c163b5ca 77 (int)qdict_get_int(qdict, "blk"),
78 (int)qdict_get_int(qdict, "inc"));
4951f65b 79 else if (strstart(uri, "unix:", &p))
f327aa0c 80 s = unix_start_outgoing_migration(mon, p, max_throttle, detach,
c163b5ca 81 (int)qdict_get_int(qdict, "blk"),
82 (int)qdict_get_int(qdict, "inc"));
5ac1fad3 83 else if (strstart(uri, "fd:", &p))
c163b5ca 84 s = fd_start_outgoing_migration(mon, p, max_throttle, detach,
85 (int)qdict_get_int(qdict, "blk"),
86 (int)qdict_get_int(qdict, "inc"));
065e2813 87#endif
34c9dd8e 88 else
376253ec 89 monitor_printf(mon, "unknown migration protocol: %s\n", uri);
34c9dd8e
AL
90
91 if (s == NULL)
376253ec 92 monitor_printf(mon, "migration failed\n");
34c9dd8e 93 else {
ff8d81d8
AL
94 if (current_migration)
95 current_migration->release(current_migration);
34c9dd8e 96
ff8d81d8 97 current_migration = s;
34c9dd8e 98 }
5bb7910a
AL
99}
100
911d2963 101void do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data)
5bb7910a
AL
102{
103 MigrationState *s = current_migration;
104
105 if (s)
ff8d81d8 106 s->cancel(s);
5bb7910a
AL
107}
108
5fd9083c 109void do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data)
5bb7910a
AL
110{
111 double d;
daa91de2 112 FdMigrationState *s;
5bb7910a 113
5667c493
MA
114 d = qdict_get_double(qdict, "value");
115 d = MAX(0, MIN(UINT32_MAX, d));
116 max_throttle = d;
daa91de2 117
5d39c799
JK
118 s = migrate_to_fms(current_migration);
119 if (s && s->file) {
daa91de2
GC
120 qemu_file_set_rate_limit(s->file, max_throttle);
121 }
5bb7910a
AL
122}
123
a0a3fd60
GC
124/* amount of nanoseconds we are willing to wait for migration to be down.
125 * the choice of nanoseconds is because it is the maximum resolution that
126 * get_clock() can achieve. It is an internal measure. All user-visible
127 * units must be in seconds */
128static uint64_t max_downtime = 30000000;
129
130uint64_t migrate_max_downtime(void)
131{
132 return max_downtime;
133}
134
c6027f56
MA
135void do_migrate_set_downtime(Monitor *mon, const QDict *qdict,
136 QObject **ret_data)
2ea42952 137{
2ea42952 138 double d;
2ea42952 139
b0fbf7d3
MA
140 d = qdict_get_double(qdict, "value") * 1e9;
141 d = MAX(0, MIN(UINT64_MAX, d));
2ea42952
GC
142 max_downtime = (uint64_t)d;
143}
144
c86a6683
LC
145static void migrate_print_status(Monitor *mon, const char *name,
146 const QDict *status_dict)
5bb7910a 147{
c86a6683
LC
148 QDict *qdict;
149
150 qdict = qobject_to_qdict(qdict_get(status_dict, name));
151
152 monitor_printf(mon, "transferred %s: %" PRIu64 " kbytes\n", name,
153 qdict_get_int(qdict, "transferred") >> 10);
154 monitor_printf(mon, "remaining %s: %" PRIu64 " kbytes\n", name,
155 qdict_get_int(qdict, "remaining") >> 10);
156 monitor_printf(mon, "total %s: %" PRIu64 " kbytes\n", name,
157 qdict_get_int(qdict, "total") >> 10);
158}
159
160void do_info_migrate_print(Monitor *mon, const QObject *data)
161{
162 QDict *qdict;
163
164 qdict = qobject_to_qdict(data);
165
166 monitor_printf(mon, "Migration status: %s\n",
167 qdict_get_str(qdict, "status"));
168
169 if (qdict_haskey(qdict, "ram")) {
170 migrate_print_status(mon, "ram", qdict);
171 }
172
173 if (qdict_haskey(qdict, "disk")) {
174 migrate_print_status(mon, "disk", qdict);
175 }
176}
177
178static void migrate_put_status(QDict *qdict, const char *name,
179 uint64_t trans, uint64_t rem, uint64_t total)
180{
181 QObject *obj;
182
183 obj = qobject_from_jsonf("{ 'transferred': %" PRId64 ", "
184 "'remaining': %" PRId64 ", "
185 "'total': %" PRId64 " }", trans, rem, total);
c86a6683
LC
186 qdict_put_obj(qdict, name, obj);
187}
188
189/**
190 * do_info_migrate(): Migration status
191 *
192 * Return a QDict. If migration is active there will be another
193 * QDict with RAM migration status and if block migration is active
194 * another one with block migration status.
195 *
196 * The main QDict contains the following:
197 *
198 * - "status": migration status
199 * - "ram": only present if "status" is "active", it is a QDict with the
200 * following RAM information (in bytes):
201 * - "transferred": amount transferred
202 * - "remaining": amount remaining
203 * - "total": total
204 * - "disk": only present if "status" is "active" and it is a block migration,
205 * it is a QDict with the following disk information (in bytes):
206 * - "transferred": amount transferred
207 * - "remaining": amount remaining
208 * - "total": total
209 *
210 * Examples:
211 *
212 * 1. Migration is "completed":
213 *
214 * { "status": "completed" }
215 *
216 * 2. Migration is "active" and it is not a block migration:
217 *
218 * { "status": "active",
219 * "ram": { "transferred": 123, "remaining": 123, "total": 246 } }
220 *
221 * 3. Migration is "active" and it is a block migration:
222 *
223 * { "status": "active",
224 * "ram": { "total": 1057024, "remaining": 1053304, "transferred": 3720 },
225 * "disk": { "total": 20971520, "remaining": 20880384, "transferred": 91136 }}
226 */
227void do_info_migrate(Monitor *mon, QObject **ret_data)
228{
229 QDict *qdict;
5bb7910a 230 MigrationState *s = current_migration;
376253ec 231
5bb7910a 232 if (s) {
ff8d81d8
AL
233 switch (s->get_status(s)) {
234 case MIG_STATE_ACTIVE:
c86a6683
LC
235 qdict = qdict_new();
236 qdict_put(qdict, "status", qstring_from_str("active"));
237
238 migrate_put_status(qdict, "ram", ram_bytes_transferred(),
239 ram_bytes_remaining(), ram_bytes_total());
240
25f23643 241 if (blk_mig_active()) {
c86a6683
LC
242 migrate_put_status(qdict, "disk", blk_mig_bytes_transferred(),
243 blk_mig_bytes_remaining(),
244 blk_mig_bytes_total());
25f23643 245 }
c86a6683
LC
246
247 *ret_data = QOBJECT(qdict);
ff8d81d8
AL
248 break;
249 case MIG_STATE_COMPLETED:
c86a6683 250 *ret_data = qobject_from_jsonf("{ 'status': 'completed' }");
ff8d81d8
AL
251 break;
252 case MIG_STATE_ERROR:
c86a6683 253 *ret_data = qobject_from_jsonf("{ 'status': 'failed' }");
ff8d81d8
AL
254 break;
255 case MIG_STATE_CANCELLED:
c86a6683 256 *ret_data = qobject_from_jsonf("{ 'status': 'cancelled' }");
ff8d81d8
AL
257 break;
258 }
5bb7910a
AL
259 }
260}
261
065e2813
AL
262/* shared migration helpers */
263
f327aa0c 264void migrate_fd_monitor_suspend(FdMigrationState *s, Monitor *mon)
731b0364 265{
f327aa0c
JK
266 s->mon = mon;
267 if (monitor_suspend(mon) == 0) {
d0f2c4c6 268 DPRINTF("suspending monitor\n");
f327aa0c
JK
269 } else {
270 monitor_printf(mon, "terminal does not allow synchronous "
cde76ee1 271 "migration, continuing detached\n");
f327aa0c 272 }
731b0364
AL
273}
274
065e2813
AL
275void migrate_fd_error(FdMigrationState *s)
276{
d0f2c4c6 277 DPRINTF("setting error state\n");
065e2813
AL
278 s->state = MIG_STATE_ERROR;
279 migrate_fd_cleanup(s);
280}
281
282void migrate_fd_cleanup(FdMigrationState *s)
283{
284 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
285
286 if (s->file) {
d0f2c4c6 287 DPRINTF("closing file\n");
065e2813 288 qemu_fclose(s->file);
5d39c799 289 s->file = NULL;
065e2813
AL
290 }
291
292 if (s->fd != -1)
293 close(s->fd);
294
295 /* Don't resume monitor until we've flushed all of the buffers */
f327aa0c
JK
296 if (s->mon) {
297 monitor_resume(s->mon);
298 }
065e2813
AL
299
300 s->fd = -1;
301}
302
303void migrate_fd_put_notify(void *opaque)
304{
305 FdMigrationState *s = opaque;
306
307 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
308 qemu_file_put_notify(s->file);
309}
310
311ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size)
312{
313 FdMigrationState *s = opaque;
314 ssize_t ret;
315
316 do {
317 ret = s->write(s, data, size);
95b134ea 318 } while (ret == -1 && ((s->get_error(s)) == EINTR));
065e2813
AL
319
320 if (ret == -1)
321 ret = -(s->get_error(s));
322
323 if (ret == -EAGAIN)
324 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
325
326 return ret;
327}
328
329void migrate_fd_connect(FdMigrationState *s)
330{
331 int ret;
332
333 s->file = qemu_fopen_ops_buffered(s,
334 s->bandwidth_limit,
335 migrate_fd_put_buffer,
336 migrate_fd_put_ready,
337 migrate_fd_wait_for_unfreeze,
338 migrate_fd_close);
339
d0f2c4c6 340 DPRINTF("beginning savevm\n");
f327aa0c 341 ret = qemu_savevm_state_begin(s->mon, s->file, s->mig_state.blk,
c163b5ca 342 s->mig_state.shared);
065e2813 343 if (ret < 0) {
d0f2c4c6 344 DPRINTF("failed, %d\n", ret);
065e2813
AL
345 migrate_fd_error(s);
346 return;
347 }
c163b5ca 348
065e2813
AL
349 migrate_fd_put_ready(s);
350}
351
352void migrate_fd_put_ready(void *opaque)
353{
354 FdMigrationState *s = opaque;
355
356 if (s->state != MIG_STATE_ACTIVE) {
d0f2c4c6 357 DPRINTF("put_ready returning because of non-active state\n");
065e2813
AL
358 return;
359 }
360
d0f2c4c6 361 DPRINTF("iterate\n");
f327aa0c 362 if (qemu_savevm_state_iterate(s->mon, s->file) == 1) {
b161d123 363 int state;
eeb34af9
AL
364 int old_vm_running = vm_running;
365
d0f2c4c6 366 DPRINTF("done iterating\n");
065e2813
AL
367 vm_stop(0);
368
0884657b 369 qemu_aio_flush();
065e2813 370 bdrv_flush_all();
f327aa0c 371 if ((qemu_savevm_state_complete(s->mon, s->file)) < 0) {
eeb34af9
AL
372 if (old_vm_running) {
373 vm_start();
374 }
b161d123
AL
375 state = MIG_STATE_ERROR;
376 } else {
377 state = MIG_STATE_COMPLETED;
378 }
065e2813 379 migrate_fd_cleanup(s);
b161d123 380 s->state = state;
065e2813
AL
381 }
382}
383
384int migrate_fd_get_status(MigrationState *mig_state)
385{
386 FdMigrationState *s = migrate_to_fms(mig_state);
387 return s->state;
388}
389
390void migrate_fd_cancel(MigrationState *mig_state)
391{
392 FdMigrationState *s = migrate_to_fms(mig_state);
393
394 if (s->state != MIG_STATE_ACTIVE)
395 return;
396
d0f2c4c6 397 DPRINTF("cancelling migration\n");
065e2813
AL
398
399 s->state = MIG_STATE_CANCELLED;
f327aa0c 400 qemu_savevm_state_cancel(s->mon, s->file);
065e2813
AL
401
402 migrate_fd_cleanup(s);
403}
404
405void migrate_fd_release(MigrationState *mig_state)
406{
407 FdMigrationState *s = migrate_to_fms(mig_state);
408
d0f2c4c6 409 DPRINTF("releasing state\n");
065e2813
AL
410
411 if (s->state == MIG_STATE_ACTIVE) {
412 s->state = MIG_STATE_CANCELLED;
413 migrate_fd_cleanup(s);
414 }
415 free(s);
416}
417
418void migrate_fd_wait_for_unfreeze(void *opaque)
419{
420 FdMigrationState *s = opaque;
421 int ret;
422
d0f2c4c6 423 DPRINTF("wait for unfreeze\n");
065e2813
AL
424 if (s->state != MIG_STATE_ACTIVE)
425 return;
426
427 do {
428 fd_set wfds;
429
430 FD_ZERO(&wfds);
431 FD_SET(s->fd, &wfds);
432
433 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
434 } while (ret == -1 && (s->get_error(s)) == EINTR);
435}
436
437int migrate_fd_close(void *opaque)
438{
439 FdMigrationState *s = opaque;
e19252d3
UL
440
441 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
065e2813
AL
442 return s->close(s);
443}