]> git.proxmox.com Git - mirror_qemu.git/blame - migration.c
migration: Refactor and simplify error checking in migrate_fd_put_ready
[mirror_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 */
3d002df3 35static int64_t max_throttle = (32 << 20);
5bb7910a 36
22f00a44 37static MigrationState *current_migration;
5bb7910a 38
99a0db9b
GH
39static NotifierList migration_state_notifiers =
40 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
41
8ca5e801 42int qemu_start_incoming_migration(const char *uri)
5bb7910a 43{
34c9dd8e 44 const char *p;
8ca5e801 45 int ret;
34c9dd8e
AL
46
47 if (strstart(uri, "tcp:", &p))
8ca5e801 48 ret = tcp_start_incoming_migration(p);
065e2813
AL
49#if !defined(WIN32)
50 else if (strstart(uri, "exec:", &p))
8ca5e801 51 ret = exec_start_incoming_migration(p);
4951f65b 52 else if (strstart(uri, "unix:", &p))
8ca5e801 53 ret = unix_start_incoming_migration(p);
5ac1fad3 54 else if (strstart(uri, "fd:", &p))
8ca5e801 55 ret = fd_start_incoming_migration(p);
065e2813 56#endif
8ca5e801 57 else {
34c9dd8e 58 fprintf(stderr, "unknown migration protocol: %s\n", uri);
8ca5e801
JQ
59 ret = -EPROTONOSUPPORT;
60 }
61 return ret;
5bb7910a
AL
62}
63
511c0231
JQ
64void process_incoming_migration(QEMUFile *f)
65{
66 if (qemu_loadvm_state(f) < 0) {
67 fprintf(stderr, "load of migration failed\n");
68 exit(0);
69 }
70 qemu_announce_self();
71 DPRINTF("successfully loaded vm state\n");
72
f5bbfba1 73 if (autostart) {
511c0231 74 vm_start();
f5bbfba1 75 } else {
0461d5a6 76 runstate_set(RUN_STATE_PRELAUNCH);
f5bbfba1 77 }
511c0231
JQ
78}
79
07af4452
JQ
80static MigrationState *migrate_new(Monitor *mon, int64_t bandwidth_limit,
81 int detach, int blk, int inc);
82
b5d17adb 83int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
5bb7910a 84{
22f00a44 85 MigrationState *s = NULL;
34c9dd8e 86 const char *p;
eb159d13
LC
87 int detach = qdict_get_try_bool(qdict, "detach", 0);
88 int blk = qdict_get_try_bool(qdict, "blk", 0);
89 int inc = qdict_get_try_bool(qdict, "inc", 0);
f18c16de 90 const char *uri = qdict_get_str(qdict, "uri");
07af4452 91 int ret;
1302425d
JK
92
93 if (current_migration &&
3f77fc55 94 current_migration->get_status(current_migration) == MIG_STATE_ACTIVE) {
1302425d 95 monitor_printf(mon, "migration already in progress\n");
b5d17adb 96 return -1;
1302425d
JK
97 }
98
dc912121
AW
99 if (qemu_savevm_state_blocked(mon)) {
100 return -1;
101 }
102
07af4452
JQ
103 s = migrate_new(mon, max_throttle, detach, blk, inc);
104
b5d17adb 105 if (strstart(uri, "tcp:", &p)) {
07af4452 106 ret = tcp_start_outgoing_migration(s, p);
065e2813 107#if !defined(WIN32)
b5d17adb 108 } else if (strstart(uri, "exec:", &p)) {
07af4452 109 ret = exec_start_outgoing_migration(s, p);
b5d17adb 110 } else if (strstart(uri, "unix:", &p)) {
07af4452 111 ret = unix_start_outgoing_migration(s, p);
b5d17adb 112 } else if (strstart(uri, "fd:", &p)) {
07af4452 113 ret = fd_start_outgoing_migration(s, p);
065e2813 114#endif
b5d17adb 115 } else {
376253ec 116 monitor_printf(mon, "unknown migration protocol: %s\n", uri);
07af4452
JQ
117 ret = -EINVAL;
118 goto free_migrate_state;
b5d17adb 119 }
34c9dd8e 120
07af4452 121 if (ret < 0) {
376253ec 122 monitor_printf(mon, "migration failed\n");
07af4452 123 goto free_migrate_state;
b5d17adb 124 }
34c9dd8e 125
b5d17adb 126 if (current_migration) {
3f77fc55 127 current_migration->release(current_migration);
34c9dd8e 128 }
b5d17adb 129
dc7acc61 130 current_migration = s;
9e8dd451 131 notifier_list_notify(&migration_state_notifiers, NULL);
b5d17adb 132 return 0;
07af4452
JQ
133free_migrate_state:
134 g_free(s);
135 return -1;
5bb7910a
AL
136}
137
ef4b7eee 138int do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data)
5bb7910a 139{
22f00a44 140 MigrationState *s = current_migration;
5bb7910a 141
3f77fc55
JQ
142 if (s && s->get_status(s) == MIG_STATE_ACTIVE) {
143 s->cancel(s);
e6494061 144 }
ef4b7eee 145 return 0;
5bb7910a
AL
146}
147
ef4b7eee 148int do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data)
5bb7910a 149{
ed3d4a80 150 int64_t d;
22f00a44 151 MigrationState *s;
5bb7910a 152
ed3d4a80 153 d = qdict_get_int(qdict, "value");
3d002df3
MT
154 if (d < 0) {
155 d = 0;
156 }
5667c493 157 max_throttle = d;
daa91de2 158
dc7acc61 159 s = current_migration;
5d39c799 160 if (s && s->file) {
daa91de2
GC
161 qemu_file_set_rate_limit(s->file, max_throttle);
162 }
ef4b7eee
LC
163
164 return 0;
5bb7910a
AL
165}
166
a0a3fd60
GC
167/* amount of nanoseconds we are willing to wait for migration to be down.
168 * the choice of nanoseconds is because it is the maximum resolution that
169 * get_clock() can achieve. It is an internal measure. All user-visible
170 * units must be in seconds */
171static uint64_t max_downtime = 30000000;
172
173uint64_t migrate_max_downtime(void)
174{
175 return max_downtime;
176}
177
ef4b7eee
LC
178int do_migrate_set_downtime(Monitor *mon, const QDict *qdict,
179 QObject **ret_data)
2ea42952 180{
2ea42952 181 double d;
2ea42952 182
b0fbf7d3
MA
183 d = qdict_get_double(qdict, "value") * 1e9;
184 d = MAX(0, MIN(UINT64_MAX, d));
2ea42952 185 max_downtime = (uint64_t)d;
ef4b7eee
LC
186
187 return 0;
2ea42952
GC
188}
189
c86a6683
LC
190static void migrate_print_status(Monitor *mon, const char *name,
191 const QDict *status_dict)
5bb7910a 192{
c86a6683
LC
193 QDict *qdict;
194
195 qdict = qobject_to_qdict(qdict_get(status_dict, name));
196
197 monitor_printf(mon, "transferred %s: %" PRIu64 " kbytes\n", name,
198 qdict_get_int(qdict, "transferred") >> 10);
199 monitor_printf(mon, "remaining %s: %" PRIu64 " kbytes\n", name,
200 qdict_get_int(qdict, "remaining") >> 10);
201 monitor_printf(mon, "total %s: %" PRIu64 " kbytes\n", name,
202 qdict_get_int(qdict, "total") >> 10);
203}
204
205void do_info_migrate_print(Monitor *mon, const QObject *data)
206{
207 QDict *qdict;
208
209 qdict = qobject_to_qdict(data);
210
211 monitor_printf(mon, "Migration status: %s\n",
212 qdict_get_str(qdict, "status"));
213
214 if (qdict_haskey(qdict, "ram")) {
215 migrate_print_status(mon, "ram", qdict);
216 }
217
218 if (qdict_haskey(qdict, "disk")) {
219 migrate_print_status(mon, "disk", qdict);
220 }
221}
222
223static void migrate_put_status(QDict *qdict, const char *name,
224 uint64_t trans, uint64_t rem, uint64_t total)
225{
226 QObject *obj;
227
228 obj = qobject_from_jsonf("{ 'transferred': %" PRId64 ", "
229 "'remaining': %" PRId64 ", "
230 "'total': %" PRId64 " }", trans, rem, total);
c86a6683
LC
231 qdict_put_obj(qdict, name, obj);
232}
233
c86a6683
LC
234void do_info_migrate(Monitor *mon, QObject **ret_data)
235{
236 QDict *qdict;
376253ec 237
dc7acc61 238 if (current_migration) {
22f00a44 239 MigrationState *s = current_migration;
dc7acc61
JQ
240
241 switch (s->get_status(current_migration)) {
d5934dde
JQ
242 case MIG_STATE_SETUP:
243 /* no migration has happened ever */
244 break;
ff8d81d8 245 case MIG_STATE_ACTIVE:
c86a6683
LC
246 qdict = qdict_new();
247 qdict_put(qdict, "status", qstring_from_str("active"));
248
249 migrate_put_status(qdict, "ram", ram_bytes_transferred(),
250 ram_bytes_remaining(), ram_bytes_total());
251
25f23643 252 if (blk_mig_active()) {
c86a6683
LC
253 migrate_put_status(qdict, "disk", blk_mig_bytes_transferred(),
254 blk_mig_bytes_remaining(),
255 blk_mig_bytes_total());
25f23643 256 }
c86a6683
LC
257
258 *ret_data = QOBJECT(qdict);
ff8d81d8
AL
259 break;
260 case MIG_STATE_COMPLETED:
c86a6683 261 *ret_data = qobject_from_jsonf("{ 'status': 'completed' }");
ff8d81d8
AL
262 break;
263 case MIG_STATE_ERROR:
c86a6683 264 *ret_data = qobject_from_jsonf("{ 'status': 'failed' }");
ff8d81d8
AL
265 break;
266 case MIG_STATE_CANCELLED:
c86a6683 267 *ret_data = qobject_from_jsonf("{ 'status': 'cancelled' }");
ff8d81d8
AL
268 break;
269 }
5bb7910a
AL
270 }
271}
272
065e2813
AL
273/* shared migration helpers */
274
0edda1c4 275static void migrate_fd_monitor_suspend(MigrationState *s, Monitor *mon)
731b0364 276{
f327aa0c
JK
277 s->mon = mon;
278 if (monitor_suspend(mon) == 0) {
d0f2c4c6 279 DPRINTF("suspending monitor\n");
f327aa0c
JK
280 } else {
281 monitor_printf(mon, "terminal does not allow synchronous "
cde76ee1 282 "migration, continuing detached\n");
f327aa0c 283 }
731b0364
AL
284}
285
8b6b99b3 286static int migrate_fd_cleanup(MigrationState *s)
065e2813 287{
41ef56e6
AL
288 int ret = 0;
289
065e2813
AL
290 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
291
292 if (s->file) {
d0f2c4c6 293 DPRINTF("closing file\n");
41ef56e6
AL
294 if (qemu_fclose(s->file) != 0) {
295 ret = -1;
296 }
5d39c799 297 s->file = NULL;
84ec6552
JK
298 } else {
299 if (s->mon) {
300 monitor_resume(s->mon);
301 }
065e2813
AL
302 }
303
84ec6552 304 if (s->fd != -1) {
065e2813 305 close(s->fd);
84ec6552 306 s->fd = -1;
f327aa0c 307 }
065e2813 308
41ef56e6 309 return ret;
065e2813
AL
310}
311
8b6b99b3
JQ
312void migrate_fd_error(MigrationState *s)
313{
314 DPRINTF("setting error state\n");
315 s->state = MIG_STATE_ERROR;
316 notifier_list_notify(&migration_state_notifiers, NULL);
317 migrate_fd_cleanup(s);
318}
319
320static void migrate_fd_put_notify(void *opaque)
065e2813 321{
22f00a44 322 MigrationState *s = opaque;
065e2813
AL
323
324 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
325 qemu_file_put_notify(s->file);
624b9cc2 326 if (qemu_file_get_error(s->file)) {
2350e13c
YT
327 migrate_fd_error(s);
328 }
065e2813
AL
329}
330
8b6b99b3
JQ
331static ssize_t migrate_fd_put_buffer(void *opaque, const void *data,
332 size_t size)
065e2813 333{
22f00a44 334 MigrationState *s = opaque;
065e2813
AL
335 ssize_t ret;
336
fdbecb5d
JQ
337 if (s->state != MIG_STATE_ACTIVE) {
338 return -EIO;
339 }
340
065e2813
AL
341 do {
342 ret = s->write(s, data, size);
95b134ea 343 } while (ret == -1 && ((s->get_error(s)) == EINTR));
065e2813
AL
344
345 if (ret == -1)
346 ret = -(s->get_error(s));
347
e447b1a6 348 if (ret == -EAGAIN) {
065e2813 349 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
e447b1a6 350 }
065e2813
AL
351
352 return ret;
353}
354
8b6b99b3 355static void migrate_fd_put_ready(void *opaque)
065e2813 356{
22f00a44 357 MigrationState *s = opaque;
39346385 358 int ret;
065e2813
AL
359
360 if (s->state != MIG_STATE_ACTIVE) {
d0f2c4c6 361 DPRINTF("put_ready returning because of non-active state\n");
065e2813
AL
362 return;
363 }
364
d0f2c4c6 365 DPRINTF("iterate\n");
39346385
JQ
366 ret = qemu_savevm_state_iterate(s->mon, s->file);
367 if (ret < 0) {
368 migrate_fd_error(s);
369 } else if (ret == 1) {
1354869c 370 int old_vm_running = runstate_is_running();
eeb34af9 371
d0f2c4c6 372 DPRINTF("done iterating\n");
0461d5a6 373 vm_stop(RUN_STATE_FINISH_MIGRATE);
065e2813 374
67afff79
JQ
375 if (qemu_savevm_state_complete(s->mon, s->file) < 0) {
376 migrate_fd_error(s);
377 } else {
378 if (migrate_fd_cleanup(s) < 0) {
379 migrate_fd_error(s);
380 } else {
381 s->state = MIG_STATE_COMPLETED;
382 runstate_set(RUN_STATE_POSTMIGRATE);
383 notifier_list_notify(&migration_state_notifiers, NULL);
eeb34af9 384 }
b161d123 385 }
67afff79 386 if (s->get_status(s) != MIG_STATE_COMPLETED) {
41ef56e6
AL
387 if (old_vm_running) {
388 vm_start();
389 }
41ef56e6 390 }
065e2813
AL
391 }
392}
393
0edda1c4 394static int migrate_fd_get_status(MigrationState *s)
065e2813 395{
065e2813
AL
396 return s->state;
397}
398
0edda1c4 399static void migrate_fd_cancel(MigrationState *s)
065e2813 400{
065e2813
AL
401 if (s->state != MIG_STATE_ACTIVE)
402 return;
403
d0f2c4c6 404 DPRINTF("cancelling migration\n");
065e2813
AL
405
406 s->state = MIG_STATE_CANCELLED;
9e8dd451 407 notifier_list_notify(&migration_state_notifiers, NULL);
f327aa0c 408 qemu_savevm_state_cancel(s->mon, s->file);
065e2813
AL
409
410 migrate_fd_cleanup(s);
411}
412
0edda1c4 413static void migrate_fd_release(MigrationState *s)
065e2813 414{
065e2813 415
d0f2c4c6 416 DPRINTF("releasing state\n");
065e2813
AL
417
418 if (s->state == MIG_STATE_ACTIVE) {
419 s->state = MIG_STATE_CANCELLED;
9e8dd451 420 notifier_list_notify(&migration_state_notifiers, NULL);
065e2813
AL
421 migrate_fd_cleanup(s);
422 }
7267c094 423 g_free(s);
065e2813
AL
424}
425
8b6b99b3 426static void migrate_fd_wait_for_unfreeze(void *opaque)
065e2813 427{
22f00a44 428 MigrationState *s = opaque;
065e2813
AL
429 int ret;
430
d0f2c4c6 431 DPRINTF("wait for unfreeze\n");
065e2813
AL
432 if (s->state != MIG_STATE_ACTIVE)
433 return;
434
435 do {
436 fd_set wfds;
437
438 FD_ZERO(&wfds);
439 FD_SET(s->fd, &wfds);
440
441 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
442 } while (ret == -1 && (s->get_error(s)) == EINTR);
af509450
JQ
443
444 if (ret == -1) {
dcd1d224 445 qemu_file_set_error(s->file, -s->get_error(s));
af509450 446 }
065e2813
AL
447}
448
8b6b99b3 449static int migrate_fd_close(void *opaque)
065e2813 450{
22f00a44 451 MigrationState *s = opaque;
e19252d3 452
84ec6552
JK
453 if (s->mon) {
454 monitor_resume(s->mon);
455 }
e19252d3 456 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
065e2813
AL
457 return s->close(s);
458}
99a0db9b
GH
459
460void add_migration_state_change_notifier(Notifier *notify)
461{
462 notifier_list_add(&migration_state_notifiers, notify);
463}
464
465void remove_migration_state_change_notifier(Notifier *notify)
466{
467 notifier_list_remove(&migration_state_notifiers, notify);
468}
469
470int get_migration_state(void)
471{
472 if (current_migration) {
473 return migrate_fd_get_status(current_migration);
474 } else {
475 return MIG_STATE_ERROR;
476 }
477}
0edda1c4 478
8b6b99b3
JQ
479void migrate_fd_connect(MigrationState *s)
480{
481 int ret;
482
d5934dde 483 s->state = MIG_STATE_ACTIVE;
8b6b99b3
JQ
484 s->file = qemu_fopen_ops_buffered(s,
485 s->bandwidth_limit,
486 migrate_fd_put_buffer,
487 migrate_fd_put_ready,
488 migrate_fd_wait_for_unfreeze,
489 migrate_fd_close);
490
491 DPRINTF("beginning savevm\n");
492 ret = qemu_savevm_state_begin(s->mon, s->file, s->blk, s->shared);
493 if (ret < 0) {
494 DPRINTF("failed, %d\n", ret);
495 migrate_fd_error(s);
496 return;
497 }
498 migrate_fd_put_ready(s);
499}
500
07af4452
JQ
501static MigrationState *migrate_new(Monitor *mon, int64_t bandwidth_limit,
502 int detach, int blk, int inc)
0edda1c4
JQ
503{
504 MigrationState *s = g_malloc0(sizeof(*s));
505
506 s->cancel = migrate_fd_cancel;
507 s->get_status = migrate_fd_get_status;
508 s->release = migrate_fd_release;
509 s->blk = blk;
510 s->shared = inc;
511 s->mon = NULL;
512 s->bandwidth_limit = bandwidth_limit;
d5934dde 513 s->state = MIG_STATE_SETUP;
0edda1c4
JQ
514
515 if (!detach) {
516 migrate_fd_monitor_suspend(s, mon);
517 }
518
519 return s;
520}