]> git.proxmox.com Git - qemu.git/blame - migration.c
Error: Introduce error_copy()
[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 *
6b620ca3
PB
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
5bb7910a
AL
14 */
15
16#include "qemu-common.h"
17#include "migration.h"
376253ec 18#include "monitor.h"
065e2813
AL
19#include "buffered_file.h"
20#include "sysemu.h"
21#include "block.h"
22#include "qemu_socket.h"
25f23643 23#include "block-migration.h"
791e7c82 24#include "qmp-commands.h"
065e2813
AL
25
26//#define DEBUG_MIGRATION
27
28#ifdef DEBUG_MIGRATION
d0f2c4c6 29#define DPRINTF(fmt, ...) \
065e2813
AL
30 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
31#else
d0f2c4c6 32#define DPRINTF(fmt, ...) \
065e2813
AL
33 do { } while (0)
34#endif
5bb7910a 35
7dc688ed
JQ
36enum {
37 MIG_STATE_ERROR,
38 MIG_STATE_SETUP,
39 MIG_STATE_CANCELLED,
40 MIG_STATE_ACTIVE,
41 MIG_STATE_COMPLETED,
42};
5bb7910a 43
d0ae46c1 44#define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
5bb7910a 45
99a0db9b
GH
46static NotifierList migration_state_notifiers =
47 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
48
17549e84
JQ
49/* When we add fault tolerance, we could have several
50 migrations at once. For now we don't need to add
51 dynamic creation of migration */
52
53static MigrationState *migrate_get_current(void)
54{
55 static MigrationState current_migration = {
56 .state = MIG_STATE_SETUP,
d0ae46c1 57 .bandwidth_limit = MAX_THROTTLE,
17549e84
JQ
58 };
59
60 return &current_migration;
61}
62
8ca5e801 63int qemu_start_incoming_migration(const char *uri)
5bb7910a 64{
34c9dd8e 65 const char *p;
8ca5e801 66 int ret;
34c9dd8e
AL
67
68 if (strstart(uri, "tcp:", &p))
8ca5e801 69 ret = tcp_start_incoming_migration(p);
065e2813
AL
70#if !defined(WIN32)
71 else if (strstart(uri, "exec:", &p))
8ca5e801 72 ret = exec_start_incoming_migration(p);
4951f65b 73 else if (strstart(uri, "unix:", &p))
8ca5e801 74 ret = unix_start_incoming_migration(p);
5ac1fad3 75 else if (strstart(uri, "fd:", &p))
8ca5e801 76 ret = fd_start_incoming_migration(p);
065e2813 77#endif
8ca5e801 78 else {
34c9dd8e 79 fprintf(stderr, "unknown migration protocol: %s\n", uri);
8ca5e801
JQ
80 ret = -EPROTONOSUPPORT;
81 }
82 return ret;
5bb7910a
AL
83}
84
511c0231
JQ
85void process_incoming_migration(QEMUFile *f)
86{
87 if (qemu_loadvm_state(f) < 0) {
88 fprintf(stderr, "load of migration failed\n");
89 exit(0);
90 }
91 qemu_announce_self();
92 DPRINTF("successfully loaded vm state\n");
93
0f15423c
AL
94 /* Make sure all file formats flush their mutable metadata */
95 bdrv_invalidate_cache_all();
96
f5bbfba1 97 if (autostart) {
511c0231 98 vm_start();
f5bbfba1 99 } else {
0461d5a6 100 runstate_set(RUN_STATE_PRELAUNCH);
f5bbfba1 101 }
511c0231
JQ
102}
103
a0a3fd60
GC
104/* amount of nanoseconds we are willing to wait for migration to be down.
105 * the choice of nanoseconds is because it is the maximum resolution that
106 * get_clock() can achieve. It is an internal measure. All user-visible
107 * units must be in seconds */
108static uint64_t max_downtime = 30000000;
109
110uint64_t migrate_max_downtime(void)
111{
112 return max_downtime;
113}
114
791e7c82 115MigrationInfo *qmp_query_migrate(Error **errp)
5bb7910a 116{
791e7c82 117 MigrationInfo *info = g_malloc0(sizeof(*info));
17549e84
JQ
118 MigrationState *s = migrate_get_current();
119
120 switch (s->state) {
121 case MIG_STATE_SETUP:
122 /* no migration has happened ever */
123 break;
124 case MIG_STATE_ACTIVE:
791e7c82
LC
125 info->has_status = true;
126 info->status = g_strdup("active");
17549e84 127
791e7c82
LC
128 info->has_ram = true;
129 info->ram = g_malloc0(sizeof(*info->ram));
130 info->ram->transferred = ram_bytes_transferred();
131 info->ram->remaining = ram_bytes_remaining();
132 info->ram->total = ram_bytes_total();
17549e84
JQ
133
134 if (blk_mig_active()) {
791e7c82
LC
135 info->has_disk = true;
136 info->disk = g_malloc0(sizeof(*info->disk));
137 info->disk->transferred = blk_mig_bytes_transferred();
138 info->disk->remaining = blk_mig_bytes_remaining();
139 info->disk->total = blk_mig_bytes_total();
ff8d81d8 140 }
17549e84
JQ
141 break;
142 case MIG_STATE_COMPLETED:
791e7c82
LC
143 info->has_status = true;
144 info->status = g_strdup("completed");
17549e84
JQ
145 break;
146 case MIG_STATE_ERROR:
791e7c82
LC
147 info->has_status = true;
148 info->status = g_strdup("failed");
17549e84
JQ
149 break;
150 case MIG_STATE_CANCELLED:
791e7c82
LC
151 info->has_status = true;
152 info->status = g_strdup("cancelled");
17549e84 153 break;
5bb7910a 154 }
791e7c82
LC
155
156 return info;
5bb7910a
AL
157}
158
065e2813
AL
159/* shared migration helpers */
160
0edda1c4 161static void migrate_fd_monitor_suspend(MigrationState *s, Monitor *mon)
731b0364 162{
f327aa0c 163 if (monitor_suspend(mon) == 0) {
d0f2c4c6 164 DPRINTF("suspending monitor\n");
f327aa0c
JK
165 } else {
166 monitor_printf(mon, "terminal does not allow synchronous "
cde76ee1 167 "migration, continuing detached\n");
f327aa0c 168 }
731b0364
AL
169}
170
8b6b99b3 171static int migrate_fd_cleanup(MigrationState *s)
065e2813 172{
41ef56e6
AL
173 int ret = 0;
174
065e2813
AL
175 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
176
177 if (s->file) {
d0f2c4c6 178 DPRINTF("closing file\n");
a6d34a94 179 ret = qemu_fclose(s->file);
5d39c799 180 s->file = NULL;
84ec6552
JK
181 } else {
182 if (s->mon) {
183 monitor_resume(s->mon);
184 }
065e2813
AL
185 }
186
84ec6552 187 if (s->fd != -1) {
065e2813 188 close(s->fd);
84ec6552 189 s->fd = -1;
f327aa0c 190 }
065e2813 191
41ef56e6 192 return ret;
065e2813
AL
193}
194
8b6b99b3 195void migrate_fd_error(MigrationState *s)
065e2813 196{
8b6b99b3
JQ
197 DPRINTF("setting error state\n");
198 s->state = MIG_STATE_ERROR;
e0eb7390 199 notifier_list_notify(&migration_state_notifiers, s);
8b6b99b3
JQ
200 migrate_fd_cleanup(s);
201}
202
458cf28e
JQ
203static void migrate_fd_completed(MigrationState *s)
204{
205 DPRINTF("setting completed state\n");
206 if (migrate_fd_cleanup(s) < 0) {
207 s->state = MIG_STATE_ERROR;
208 } else {
209 s->state = MIG_STATE_COMPLETED;
210 runstate_set(RUN_STATE_POSTMIGRATE);
211 }
e0eb7390 212 notifier_list_notify(&migration_state_notifiers, s);
458cf28e
JQ
213}
214
8b6b99b3 215static void migrate_fd_put_notify(void *opaque)
065e2813 216{
22f00a44 217 MigrationState *s = opaque;
065e2813
AL
218
219 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
220 qemu_file_put_notify(s->file);
1fdc11c3 221 if (s->file && qemu_file_get_error(s->file)) {
2350e13c
YT
222 migrate_fd_error(s);
223 }
065e2813
AL
224}
225
8b6b99b3
JQ
226static ssize_t migrate_fd_put_buffer(void *opaque, const void *data,
227 size_t size)
065e2813 228{
22f00a44 229 MigrationState *s = opaque;
065e2813
AL
230 ssize_t ret;
231
fdbecb5d
JQ
232 if (s->state != MIG_STATE_ACTIVE) {
233 return -EIO;
234 }
235
065e2813
AL
236 do {
237 ret = s->write(s, data, size);
95b134ea 238 } while (ret == -1 && ((s->get_error(s)) == EINTR));
065e2813
AL
239
240 if (ret == -1)
241 ret = -(s->get_error(s));
242
e447b1a6 243 if (ret == -EAGAIN) {
065e2813 244 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
e447b1a6 245 }
065e2813
AL
246
247 return ret;
248}
249
8b6b99b3 250static void migrate_fd_put_ready(void *opaque)
065e2813 251{
22f00a44 252 MigrationState *s = opaque;
065e2813
AL
253 int ret;
254
065e2813 255 if (s->state != MIG_STATE_ACTIVE) {
d0f2c4c6 256 DPRINTF("put_ready returning because of non-active state\n");
065e2813
AL
257 return;
258 }
259
d0f2c4c6 260 DPRINTF("iterate\n");
39346385
JQ
261 ret = qemu_savevm_state_iterate(s->mon, s->file);
262 if (ret < 0) {
263 migrate_fd_error(s);
264 } else if (ret == 1) {
1354869c 265 int old_vm_running = runstate_is_running();
eeb34af9 266
d0f2c4c6 267 DPRINTF("done iterating\n");
8a9236f1 268 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
065e2813 269
67afff79
JQ
270 if (qemu_savevm_state_complete(s->mon, s->file) < 0) {
271 migrate_fd_error(s);
b161d123 272 } else {
458cf28e 273 migrate_fd_completed(s);
b161d123 274 }
48a2f4d6 275 if (s->state != MIG_STATE_COMPLETED) {
41ef56e6
AL
276 if (old_vm_running) {
277 vm_start();
278 }
f5bbfba1 279 }
065e2813
AL
280 }
281}
282
0edda1c4 283static void migrate_fd_cancel(MigrationState *s)
065e2813 284{
065e2813
AL
285 if (s->state != MIG_STATE_ACTIVE)
286 return;
287
d0f2c4c6 288 DPRINTF("cancelling migration\n");
065e2813
AL
289
290 s->state = MIG_STATE_CANCELLED;
e0eb7390 291 notifier_list_notify(&migration_state_notifiers, s);
f327aa0c 292 qemu_savevm_state_cancel(s->mon, s->file);
065e2813
AL
293
294 migrate_fd_cleanup(s);
295}
296
8b6b99b3 297static void migrate_fd_wait_for_unfreeze(void *opaque)
065e2813 298{
22f00a44 299 MigrationState *s = opaque;
065e2813
AL
300 int ret;
301
d0f2c4c6 302 DPRINTF("wait for unfreeze\n");
065e2813
AL
303 if (s->state != MIG_STATE_ACTIVE)
304 return;
305
306 do {
307 fd_set wfds;
308
309 FD_ZERO(&wfds);
310 FD_SET(s->fd, &wfds);
311
312 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
313 } while (ret == -1 && (s->get_error(s)) == EINTR);
af509450
JQ
314
315 if (ret == -1) {
dcd1d224 316 qemu_file_set_error(s->file, -s->get_error(s));
af509450 317 }
065e2813
AL
318}
319
8b6b99b3 320static int migrate_fd_close(void *opaque)
065e2813 321{
22f00a44 322 MigrationState *s = opaque;
e19252d3 323
84ec6552
JK
324 if (s->mon) {
325 monitor_resume(s->mon);
326 }
e19252d3 327 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
065e2813
AL
328 return s->close(s);
329}
99a0db9b
GH
330
331void add_migration_state_change_notifier(Notifier *notify)
332{
333 notifier_list_add(&migration_state_notifiers, notify);
334}
335
336void remove_migration_state_change_notifier(Notifier *notify)
337{
31552529 338 notifier_remove(notify);
99a0db9b
GH
339}
340
afe2df69
GH
341bool migration_is_active(MigrationState *s)
342{
343 return s->state == MIG_STATE_ACTIVE;
344}
345
7073693b 346bool migration_has_finished(MigrationState *s)
99a0db9b 347{
7073693b 348 return s->state == MIG_STATE_COMPLETED;
99a0db9b 349}
0edda1c4 350
afe2df69
GH
351bool migration_has_failed(MigrationState *s)
352{
353 return (s->state == MIG_STATE_CANCELLED ||
354 s->state == MIG_STATE_ERROR);
355}
356
8b6b99b3 357void migrate_fd_connect(MigrationState *s)
99a0db9b 358{
8b6b99b3
JQ
359 int ret;
360
d5934dde 361 s->state = MIG_STATE_ACTIVE;
8b6b99b3
JQ
362 s->file = qemu_fopen_ops_buffered(s,
363 s->bandwidth_limit,
364 migrate_fd_put_buffer,
365 migrate_fd_put_ready,
366 migrate_fd_wait_for_unfreeze,
367 migrate_fd_close);
368
369 DPRINTF("beginning savevm\n");
370 ret = qemu_savevm_state_begin(s->mon, s->file, s->blk, s->shared);
371 if (ret < 0) {
372 DPRINTF("failed, %d\n", ret);
373 migrate_fd_error(s);
374 return;
375 }
376 migrate_fd_put_ready(s);
377}
378
d0ae46c1 379static MigrationState *migrate_init(Monitor *mon, int detach, int blk, int inc)
0edda1c4 380{
17549e84 381 MigrationState *s = migrate_get_current();
d0ae46c1 382 int64_t bandwidth_limit = s->bandwidth_limit;
0edda1c4 383
17549e84 384 memset(s, 0, sizeof(*s));
d0ae46c1 385 s->bandwidth_limit = bandwidth_limit;
0edda1c4
JQ
386 s->blk = blk;
387 s->shared = inc;
1299c631
JQ
388
389 /* s->mon is used for two things:
390 - pass fd in fd migration
391 - suspend/resume monitor for not detached migration
392 */
393 s->mon = mon;
0edda1c4 394 s->bandwidth_limit = bandwidth_limit;
d5934dde 395 s->state = MIG_STATE_SETUP;
0edda1c4
JQ
396
397 if (!detach) {
398 migrate_fd_monitor_suspend(s, mon);
399 }
400
401 return s;
402}
cab30143 403
fa2756b7
AL
404static GSList *migration_blockers;
405
406void migrate_add_blocker(Error *reason)
407{
408 migration_blockers = g_slist_prepend(migration_blockers, reason);
409}
410
411void migrate_del_blocker(Error *reason)
412{
413 migration_blockers = g_slist_remove(migration_blockers, reason);
414}
415
cab30143
JQ
416int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
417{
17549e84 418 MigrationState *s = migrate_get_current();
cab30143
JQ
419 const char *p;
420 int detach = qdict_get_try_bool(qdict, "detach", 0);
421 int blk = qdict_get_try_bool(qdict, "blk", 0);
422 int inc = qdict_get_try_bool(qdict, "inc", 0);
423 const char *uri = qdict_get_str(qdict, "uri");
424 int ret;
425
17549e84 426 if (s->state == MIG_STATE_ACTIVE) {
cab30143
JQ
427 monitor_printf(mon, "migration already in progress\n");
428 return -1;
429 }
430
431 if (qemu_savevm_state_blocked(mon)) {
432 return -1;
433 }
434
fa2756b7
AL
435 if (migration_blockers) {
436 Error *err = migration_blockers->data;
437 qerror_report_err(err);
438 return -1;
439 }
440
d0ae46c1 441 s = migrate_init(mon, detach, blk, inc);
cab30143
JQ
442
443 if (strstart(uri, "tcp:", &p)) {
444 ret = tcp_start_outgoing_migration(s, p);
445#if !defined(WIN32)
446 } else if (strstart(uri, "exec:", &p)) {
447 ret = exec_start_outgoing_migration(s, p);
448 } else if (strstart(uri, "unix:", &p)) {
449 ret = unix_start_outgoing_migration(s, p);
450 } else if (strstart(uri, "fd:", &p)) {
451 ret = fd_start_outgoing_migration(s, p);
452#endif
99a0db9b 453 } else {
cab30143
JQ
454 monitor_printf(mon, "unknown migration protocol: %s\n", uri);
455 ret = -EINVAL;
cab30143
JQ
456 }
457
458 if (ret < 0) {
17549e84
JQ
459 monitor_printf(mon, "migration failed: %s\n", strerror(-ret));
460 return ret;
cab30143
JQ
461 }
462
1299c631
JQ
463 if (detach) {
464 s->mon = NULL;
465 }
466
e0eb7390 467 notifier_list_notify(&migration_state_notifiers, s);
cab30143 468 return 0;
cab30143
JQ
469}
470
6cdedb07 471void qmp_migrate_cancel(Error **errp)
cab30143 472{
17549e84 473 migrate_fd_cancel(migrate_get_current());
cab30143
JQ
474}
475
3dc85383 476void qmp_migrate_set_speed(int64_t value, Error **errp)
cab30143 477{
cab30143
JQ
478 MigrationState *s;
479
3dc85383
LC
480 if (value < 0) {
481 value = 0;
99a0db9b 482 }
cab30143 483
17549e84 484 s = migrate_get_current();
3dc85383 485 s->bandwidth_limit = value;
d0ae46c1 486 qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
cab30143
JQ
487}
488
4f0a993b 489void qmp_migrate_set_downtime(double value, Error **errp)
cab30143 490{
4f0a993b
LC
491 value *= 1e9;
492 value = MAX(0, MIN(UINT64_MAX, value));
493 max_downtime = (uint64_t)value;
99a0db9b 494}