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