]> git.proxmox.com Git - mirror_qemu.git/blame - migration.c
migration: move migrate_new to do_migrate
[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)) {
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
317static void migrate_fd_put_notify(void *opaque)
065e2813 318{
22f00a44 319 MigrationState *s = opaque;
065e2813
AL
320
321 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
322 qemu_file_put_notify(s->file);
624b9cc2 323 if (qemu_file_get_error(s->file)) {
2350e13c
YT
324 migrate_fd_error(s);
325 }
065e2813
AL
326}
327
8b6b99b3
JQ
328static ssize_t migrate_fd_put_buffer(void *opaque, const void *data,
329 size_t size)
065e2813 330{
22f00a44 331 MigrationState *s = opaque;
065e2813
AL
332 ssize_t ret;
333
fdbecb5d
JQ
334 if (s->state != MIG_STATE_ACTIVE) {
335 return -EIO;
336 }
337
065e2813
AL
338 do {
339 ret = s->write(s, data, size);
95b134ea 340 } while (ret == -1 && ((s->get_error(s)) == EINTR));
065e2813
AL
341
342 if (ret == -1)
343 ret = -(s->get_error(s));
344
e447b1a6 345 if (ret == -EAGAIN) {
065e2813 346 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
e447b1a6 347 }
065e2813
AL
348
349 return ret;
350}
351
8b6b99b3 352static void migrate_fd_put_ready(void *opaque)
065e2813 353{
22f00a44 354 MigrationState *s = opaque;
39346385 355 int ret;
065e2813
AL
356
357 if (s->state != MIG_STATE_ACTIVE) {
d0f2c4c6 358 DPRINTF("put_ready returning because of non-active state\n");
065e2813
AL
359 return;
360 }
361
d0f2c4c6 362 DPRINTF("iterate\n");
39346385
JQ
363 ret = qemu_savevm_state_iterate(s->mon, s->file);
364 if (ret < 0) {
365 migrate_fd_error(s);
366 } else if (ret == 1) {
1354869c 367 int old_vm_running = runstate_is_running();
eeb34af9 368
d0f2c4c6 369 DPRINTF("done iterating\n");
0461d5a6 370 vm_stop(RUN_STATE_FINISH_MIGRATE);
065e2813 371
f327aa0c 372 if ((qemu_savevm_state_complete(s->mon, s->file)) < 0) {
eeb34af9
AL
373 if (old_vm_running) {
374 vm_start();
375 }
c0b2ba46 376 s->state = MIG_STATE_ERROR;
b161d123 377 }
41ef56e6
AL
378 if (migrate_fd_cleanup(s) < 0) {
379 if (old_vm_running) {
380 vm_start();
381 }
c0b2ba46 382 s->state = MIG_STATE_ERROR;
41ef56e6 383 }
c0b2ba46
JQ
384 if (s->state == MIG_STATE_ACTIVE) {
385 s->state = MIG_STATE_COMPLETED;
0461d5a6 386 runstate_set(RUN_STATE_POSTMIGRATE);
f5bbfba1 387 }
9e8dd451 388 notifier_list_notify(&migration_state_notifiers, NULL);
065e2813
AL
389 }
390}
391
0edda1c4 392static int migrate_fd_get_status(MigrationState *s)
065e2813 393{
065e2813
AL
394 return s->state;
395}
396
0edda1c4 397static void migrate_fd_cancel(MigrationState *s)
065e2813 398{
065e2813
AL
399 if (s->state != MIG_STATE_ACTIVE)
400 return;
401
d0f2c4c6 402 DPRINTF("cancelling migration\n");
065e2813
AL
403
404 s->state = MIG_STATE_CANCELLED;
9e8dd451 405 notifier_list_notify(&migration_state_notifiers, NULL);
f327aa0c 406 qemu_savevm_state_cancel(s->mon, s->file);
065e2813
AL
407
408 migrate_fd_cleanup(s);
409}
410
0edda1c4 411static void migrate_fd_release(MigrationState *s)
065e2813 412{
065e2813 413
d0f2c4c6 414 DPRINTF("releasing state\n");
065e2813
AL
415
416 if (s->state == MIG_STATE_ACTIVE) {
417 s->state = MIG_STATE_CANCELLED;
9e8dd451 418 notifier_list_notify(&migration_state_notifiers, NULL);
065e2813
AL
419 migrate_fd_cleanup(s);
420 }
7267c094 421 g_free(s);
065e2813
AL
422}
423
8b6b99b3 424static void migrate_fd_wait_for_unfreeze(void *opaque)
065e2813 425{
22f00a44 426 MigrationState *s = opaque;
065e2813
AL
427 int ret;
428
d0f2c4c6 429 DPRINTF("wait for unfreeze\n");
065e2813
AL
430 if (s->state != MIG_STATE_ACTIVE)
431 return;
432
433 do {
434 fd_set wfds;
435
436 FD_ZERO(&wfds);
437 FD_SET(s->fd, &wfds);
438
439 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
440 } while (ret == -1 && (s->get_error(s)) == EINTR);
af509450
JQ
441
442 if (ret == -1) {
dcd1d224 443 qemu_file_set_error(s->file, -s->get_error(s));
af509450 444 }
065e2813
AL
445}
446
8b6b99b3 447static int migrate_fd_close(void *opaque)
065e2813 448{
22f00a44 449 MigrationState *s = opaque;
e19252d3 450
84ec6552
JK
451 if (s->mon) {
452 monitor_resume(s->mon);
453 }
e19252d3 454 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
065e2813
AL
455 return s->close(s);
456}
99a0db9b
GH
457
458void add_migration_state_change_notifier(Notifier *notify)
459{
460 notifier_list_add(&migration_state_notifiers, notify);
461}
462
463void remove_migration_state_change_notifier(Notifier *notify)
464{
465 notifier_list_remove(&migration_state_notifiers, notify);
466}
467
468int get_migration_state(void)
469{
470 if (current_migration) {
471 return migrate_fd_get_status(current_migration);
472 } else {
473 return MIG_STATE_ERROR;
474 }
475}
0edda1c4 476
8b6b99b3
JQ
477void migrate_fd_connect(MigrationState *s)
478{
479 int ret;
480
481 s->file = qemu_fopen_ops_buffered(s,
482 s->bandwidth_limit,
483 migrate_fd_put_buffer,
484 migrate_fd_put_ready,
485 migrate_fd_wait_for_unfreeze,
486 migrate_fd_close);
487
488 DPRINTF("beginning savevm\n");
489 ret = qemu_savevm_state_begin(s->mon, s->file, s->blk, s->shared);
490 if (ret < 0) {
491 DPRINTF("failed, %d\n", ret);
492 migrate_fd_error(s);
493 return;
494 }
495 migrate_fd_put_ready(s);
496}
497
07af4452
JQ
498static MigrationState *migrate_new(Monitor *mon, int64_t bandwidth_limit,
499 int detach, int blk, int inc)
0edda1c4
JQ
500{
501 MigrationState *s = g_malloc0(sizeof(*s));
502
503 s->cancel = migrate_fd_cancel;
504 s->get_status = migrate_fd_get_status;
505 s->release = migrate_fd_release;
506 s->blk = blk;
507 s->shared = inc;
508 s->mon = NULL;
509 s->bandwidth_limit = bandwidth_limit;
510 s->state = MIG_STATE_ACTIVE;
511
512 if (!detach) {
513 migrate_fd_monitor_suspend(s, mon);
514 }
515
516 return s;
517}