]> git.proxmox.com Git - mirror_qemu.git/blame - migration.c
Replace the VMSTOP macros with a proper state type
[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
AL
36
37static MigrationState *current_migration;
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
8e84865e
AS
73 incoming_expected = false;
74
511c0231
JQ
75 if (autostart)
76 vm_start();
77}
78
b5d17adb 79int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
5bb7910a 80{
34c9dd8e
AL
81 MigrationState *s = NULL;
82 const char *p;
eb159d13
LC
83 int detach = qdict_get_try_bool(qdict, "detach", 0);
84 int blk = qdict_get_try_bool(qdict, "blk", 0);
85 int inc = qdict_get_try_bool(qdict, "inc", 0);
f18c16de 86 const char *uri = qdict_get_str(qdict, "uri");
1302425d
JK
87
88 if (current_migration &&
89 current_migration->get_status(current_migration) == MIG_STATE_ACTIVE) {
90 monitor_printf(mon, "migration already in progress\n");
b5d17adb 91 return -1;
1302425d
JK
92 }
93
dc912121
AW
94 if (qemu_savevm_state_blocked(mon)) {
95 return -1;
96 }
97
b5d17adb 98 if (strstart(uri, "tcp:", &p)) {
f327aa0c 99 s = tcp_start_outgoing_migration(mon, p, max_throttle, detach,
eb159d13 100 blk, inc);
065e2813 101#if !defined(WIN32)
b5d17adb 102 } else if (strstart(uri, "exec:", &p)) {
f327aa0c 103 s = exec_start_outgoing_migration(mon, p, max_throttle, detach,
eb159d13 104 blk, inc);
b5d17adb 105 } else if (strstart(uri, "unix:", &p)) {
f327aa0c 106 s = unix_start_outgoing_migration(mon, p, max_throttle, detach,
eb159d13 107 blk, inc);
b5d17adb 108 } else if (strstart(uri, "fd:", &p)) {
c163b5ca 109 s = fd_start_outgoing_migration(mon, p, max_throttle, detach,
eb159d13 110 blk, inc);
065e2813 111#endif
b5d17adb 112 } else {
376253ec 113 monitor_printf(mon, "unknown migration protocol: %s\n", uri);
b5d17adb
LC
114 return -1;
115 }
34c9dd8e 116
b5d17adb 117 if (s == NULL) {
376253ec 118 monitor_printf(mon, "migration failed\n");
b5d17adb
LC
119 return -1;
120 }
34c9dd8e 121
b5d17adb
LC
122 if (current_migration) {
123 current_migration->release(current_migration);
34c9dd8e 124 }
b5d17adb
LC
125
126 current_migration = s;
9e8dd451 127 notifier_list_notify(&migration_state_notifiers, NULL);
b5d17adb 128 return 0;
5bb7910a
AL
129}
130
ef4b7eee 131int do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data)
5bb7910a
AL
132{
133 MigrationState *s = current_migration;
134
135 if (s)
ff8d81d8 136 s->cancel(s);
ef4b7eee
LC
137
138 return 0;
5bb7910a
AL
139}
140
ef4b7eee 141int do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data)
5bb7910a 142{
ed3d4a80 143 int64_t d;
daa91de2 144 FdMigrationState *s;
5bb7910a 145
ed3d4a80 146 d = qdict_get_int(qdict, "value");
3d002df3
MT
147 if (d < 0) {
148 d = 0;
149 }
5667c493 150 max_throttle = d;
daa91de2 151
5d39c799
JK
152 s = migrate_to_fms(current_migration);
153 if (s && s->file) {
daa91de2
GC
154 qemu_file_set_rate_limit(s->file, max_throttle);
155 }
ef4b7eee
LC
156
157 return 0;
5bb7910a
AL
158}
159
a0a3fd60
GC
160/* amount of nanoseconds we are willing to wait for migration to be down.
161 * the choice of nanoseconds is because it is the maximum resolution that
162 * get_clock() can achieve. It is an internal measure. All user-visible
163 * units must be in seconds */
164static uint64_t max_downtime = 30000000;
165
166uint64_t migrate_max_downtime(void)
167{
168 return max_downtime;
169}
170
ef4b7eee
LC
171int do_migrate_set_downtime(Monitor *mon, const QDict *qdict,
172 QObject **ret_data)
2ea42952 173{
2ea42952 174 double d;
2ea42952 175
b0fbf7d3
MA
176 d = qdict_get_double(qdict, "value") * 1e9;
177 d = MAX(0, MIN(UINT64_MAX, d));
2ea42952 178 max_downtime = (uint64_t)d;
ef4b7eee
LC
179
180 return 0;
2ea42952
GC
181}
182
c86a6683
LC
183static void migrate_print_status(Monitor *mon, const char *name,
184 const QDict *status_dict)
5bb7910a 185{
c86a6683
LC
186 QDict *qdict;
187
188 qdict = qobject_to_qdict(qdict_get(status_dict, name));
189
190 monitor_printf(mon, "transferred %s: %" PRIu64 " kbytes\n", name,
191 qdict_get_int(qdict, "transferred") >> 10);
192 monitor_printf(mon, "remaining %s: %" PRIu64 " kbytes\n", name,
193 qdict_get_int(qdict, "remaining") >> 10);
194 monitor_printf(mon, "total %s: %" PRIu64 " kbytes\n", name,
195 qdict_get_int(qdict, "total") >> 10);
196}
197
198void do_info_migrate_print(Monitor *mon, const QObject *data)
199{
200 QDict *qdict;
201
202 qdict = qobject_to_qdict(data);
203
204 monitor_printf(mon, "Migration status: %s\n",
205 qdict_get_str(qdict, "status"));
206
207 if (qdict_haskey(qdict, "ram")) {
208 migrate_print_status(mon, "ram", qdict);
209 }
210
211 if (qdict_haskey(qdict, "disk")) {
212 migrate_print_status(mon, "disk", qdict);
213 }
214}
215
216static void migrate_put_status(QDict *qdict, const char *name,
217 uint64_t trans, uint64_t rem, uint64_t total)
218{
219 QObject *obj;
220
221 obj = qobject_from_jsonf("{ 'transferred': %" PRId64 ", "
222 "'remaining': %" PRId64 ", "
223 "'total': %" PRId64 " }", trans, rem, total);
c86a6683
LC
224 qdict_put_obj(qdict, name, obj);
225}
226
c86a6683
LC
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 278 s->state = MIG_STATE_ERROR;
9e8dd451 279 notifier_list_notify(&migration_state_notifiers, NULL);
065e2813
AL
280 migrate_fd_cleanup(s);
281}
282
41ef56e6 283int migrate_fd_cleanup(FdMigrationState *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
309void migrate_fd_put_notify(void *opaque)
310{
311 FdMigrationState *s = opaque;
312
313 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
314 qemu_file_put_notify(s->file);
315}
316
317ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size)
318{
319 FdMigrationState *s = opaque;
320 ssize_t ret;
321
322 do {
323 ret = s->write(s, data, size);
95b134ea 324 } while (ret == -1 && ((s->get_error(s)) == EINTR));
065e2813
AL
325
326 if (ret == -1)
327 ret = -(s->get_error(s));
328
e447b1a6 329 if (ret == -EAGAIN) {
065e2813 330 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
e447b1a6 331 } else if (ret < 0) {
e447b1a6 332 s->state = MIG_STATE_ERROR;
9e8dd451 333 notifier_list_notify(&migration_state_notifiers, NULL);
e447b1a6 334 }
065e2813
AL
335
336 return ret;
337}
338
339void migrate_fd_connect(FdMigrationState *s)
340{
341 int ret;
342
343 s->file = qemu_fopen_ops_buffered(s,
344 s->bandwidth_limit,
345 migrate_fd_put_buffer,
346 migrate_fd_put_ready,
347 migrate_fd_wait_for_unfreeze,
348 migrate_fd_close);
349
d0f2c4c6 350 DPRINTF("beginning savevm\n");
f327aa0c 351 ret = qemu_savevm_state_begin(s->mon, s->file, s->mig_state.blk,
c163b5ca 352 s->mig_state.shared);
065e2813 353 if (ret < 0) {
d0f2c4c6 354 DPRINTF("failed, %d\n", ret);
065e2813
AL
355 migrate_fd_error(s);
356 return;
357 }
c163b5ca 358
065e2813
AL
359 migrate_fd_put_ready(s);
360}
361
362void migrate_fd_put_ready(void *opaque)
363{
364 FdMigrationState *s = opaque;
365
366 if (s->state != MIG_STATE_ACTIVE) {
d0f2c4c6 367 DPRINTF("put_ready returning because of non-active state\n");
065e2813
AL
368 return;
369 }
370
d0f2c4c6 371 DPRINTF("iterate\n");
f327aa0c 372 if (qemu_savevm_state_iterate(s->mon, s->file) == 1) {
b161d123 373 int state;
eeb34af9
AL
374 int old_vm_running = vm_running;
375
d0f2c4c6 376 DPRINTF("done iterating\n");
1dfb4dd9 377 vm_stop(RSTATE_PRE_MIGRATE);
065e2813 378
f327aa0c 379 if ((qemu_savevm_state_complete(s->mon, s->file)) < 0) {
eeb34af9
AL
380 if (old_vm_running) {
381 vm_start();
382 }
b161d123
AL
383 state = MIG_STATE_ERROR;
384 } else {
385 state = MIG_STATE_COMPLETED;
386 }
41ef56e6
AL
387 if (migrate_fd_cleanup(s) < 0) {
388 if (old_vm_running) {
389 vm_start();
390 }
391 state = MIG_STATE_ERROR;
392 }
b161d123 393 s->state = state;
9e8dd451 394 notifier_list_notify(&migration_state_notifiers, NULL);
065e2813
AL
395 }
396}
397
398int migrate_fd_get_status(MigrationState *mig_state)
399{
400 FdMigrationState *s = migrate_to_fms(mig_state);
401 return s->state;
402}
403
404void migrate_fd_cancel(MigrationState *mig_state)
405{
406 FdMigrationState *s = migrate_to_fms(mig_state);
407
408 if (s->state != MIG_STATE_ACTIVE)
409 return;
410
d0f2c4c6 411 DPRINTF("cancelling migration\n");
065e2813
AL
412
413 s->state = MIG_STATE_CANCELLED;
9e8dd451 414 notifier_list_notify(&migration_state_notifiers, NULL);
f327aa0c 415 qemu_savevm_state_cancel(s->mon, s->file);
065e2813
AL
416
417 migrate_fd_cleanup(s);
418}
419
420void migrate_fd_release(MigrationState *mig_state)
421{
422 FdMigrationState *s = migrate_to_fms(mig_state);
423
d0f2c4c6 424 DPRINTF("releasing state\n");
065e2813
AL
425
426 if (s->state == MIG_STATE_ACTIVE) {
427 s->state = MIG_STATE_CANCELLED;
9e8dd451 428 notifier_list_notify(&migration_state_notifiers, NULL);
065e2813
AL
429 migrate_fd_cleanup(s);
430 }
7267c094 431 g_free(s);
065e2813
AL
432}
433
434void migrate_fd_wait_for_unfreeze(void *opaque)
435{
436 FdMigrationState *s = opaque;
437 int ret;
438
d0f2c4c6 439 DPRINTF("wait for unfreeze\n");
065e2813
AL
440 if (s->state != MIG_STATE_ACTIVE)
441 return;
442
443 do {
444 fd_set wfds;
445
446 FD_ZERO(&wfds);
447 FD_SET(s->fd, &wfds);
448
449 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
450 } while (ret == -1 && (s->get_error(s)) == EINTR);
451}
452
453int migrate_fd_close(void *opaque)
454{
455 FdMigrationState *s = opaque;
e19252d3 456
84ec6552
JK
457 if (s->mon) {
458 monitor_resume(s->mon);
459 }
e19252d3 460 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
065e2813
AL
461 return s->close(s);
462}
99a0db9b
GH
463
464void add_migration_state_change_notifier(Notifier *notify)
465{
466 notifier_list_add(&migration_state_notifiers, notify);
467}
468
469void remove_migration_state_change_notifier(Notifier *notify)
470{
471 notifier_list_remove(&migration_state_notifiers, notify);
472}
473
474int get_migration_state(void)
475{
476 if (current_migration) {
477 return migrate_fd_get_status(current_migration);
478 } else {
479 return MIG_STATE_ERROR;
480 }
481}