]> git.proxmox.com Git - mirror_qemu.git/blame - migration.c
Add migration capabilities
[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 *
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
d5c5dacc 63int qemu_start_incoming_migration(const char *uri, Error **errp)
5bb7910a 64{
34c9dd8e 65 const char *p;
8ca5e801 66 int ret;
34c9dd8e
AL
67
68 if (strstart(uri, "tcp:", &p))
d5c5dacc 69 ret = tcp_start_incoming_migration(p, errp);
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
901862cb 94 bdrv_clear_incoming_migration_all();
0f15423c
AL
95 /* Make sure all file formats flush their mutable metadata */
96 bdrv_invalidate_cache_all();
97
f5bbfba1 98 if (autostart) {
511c0231 99 vm_start();
f5bbfba1 100 } else {
0461d5a6 101 runstate_set(RUN_STATE_PRELAUNCH);
f5bbfba1 102 }
511c0231
JQ
103}
104
a0a3fd60
GC
105/* amount of nanoseconds we are willing to wait for migration to be down.
106 * the choice of nanoseconds is because it is the maximum resolution that
107 * get_clock() can achieve. It is an internal measure. All user-visible
108 * units must be in seconds */
109static uint64_t max_downtime = 30000000;
110
111uint64_t migrate_max_downtime(void)
112{
113 return max_downtime;
114}
115
bbf6da32
OW
116MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
117{
118 MigrationCapabilityStatusList *head = NULL;
119 MigrationCapabilityStatusList *caps;
120 MigrationState *s = migrate_get_current();
121 int i;
122
123 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
124 if (head == NULL) {
125 head = g_malloc0(sizeof(*caps));
126 caps = head;
127 } else {
128 caps->next = g_malloc0(sizeof(*caps));
129 caps = caps->next;
130 }
131 caps->value =
132 g_malloc(sizeof(*caps->value));
133 caps->value->capability = i;
134 caps->value->state = s->enabled_capabilities[i];
135 }
136
137 return head;
138}
139
791e7c82 140MigrationInfo *qmp_query_migrate(Error **errp)
5bb7910a 141{
791e7c82 142 MigrationInfo *info = g_malloc0(sizeof(*info));
17549e84
JQ
143 MigrationState *s = migrate_get_current();
144
145 switch (s->state) {
146 case MIG_STATE_SETUP:
147 /* no migration has happened ever */
148 break;
149 case MIG_STATE_ACTIVE:
791e7c82
LC
150 info->has_status = true;
151 info->status = g_strdup("active");
17549e84 152
791e7c82
LC
153 info->has_ram = true;
154 info->ram = g_malloc0(sizeof(*info->ram));
155 info->ram->transferred = ram_bytes_transferred();
156 info->ram->remaining = ram_bytes_remaining();
157 info->ram->total = ram_bytes_total();
d5f8a570
JQ
158 info->ram->total_time = qemu_get_clock_ms(rt_clock)
159 - s->total_time;
17549e84
JQ
160
161 if (blk_mig_active()) {
791e7c82
LC
162 info->has_disk = true;
163 info->disk = g_malloc0(sizeof(*info->disk));
164 info->disk->transferred = blk_mig_bytes_transferred();
165 info->disk->remaining = blk_mig_bytes_remaining();
166 info->disk->total = blk_mig_bytes_total();
ff8d81d8 167 }
17549e84
JQ
168 break;
169 case MIG_STATE_COMPLETED:
791e7c82
LC
170 info->has_status = true;
171 info->status = g_strdup("completed");
d5f8a570
JQ
172
173 info->has_ram = true;
174 info->ram = g_malloc0(sizeof(*info->ram));
175 info->ram->transferred = ram_bytes_transferred();
176 info->ram->remaining = 0;
177 info->ram->total = ram_bytes_total();
178 info->ram->total_time = s->total_time;
17549e84
JQ
179 break;
180 case MIG_STATE_ERROR:
791e7c82
LC
181 info->has_status = true;
182 info->status = g_strdup("failed");
17549e84
JQ
183 break;
184 case MIG_STATE_CANCELLED:
791e7c82
LC
185 info->has_status = true;
186 info->status = g_strdup("cancelled");
17549e84 187 break;
5bb7910a 188 }
791e7c82
LC
189
190 return info;
5bb7910a
AL
191}
192
065e2813
AL
193/* shared migration helpers */
194
8b6b99b3 195static int migrate_fd_cleanup(MigrationState *s)
065e2813 196{
41ef56e6
AL
197 int ret = 0;
198
065e2813
AL
199 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
200
201 if (s->file) {
d0f2c4c6 202 DPRINTF("closing file\n");
a6d34a94 203 ret = qemu_fclose(s->file);
5d39c799 204 s->file = NULL;
065e2813
AL
205 }
206
84ec6552 207 if (s->fd != -1) {
065e2813 208 close(s->fd);
84ec6552 209 s->fd = -1;
f327aa0c 210 }
065e2813 211
41ef56e6 212 return ret;
065e2813
AL
213}
214
8b6b99b3 215void migrate_fd_error(MigrationState *s)
065e2813 216{
8b6b99b3
JQ
217 DPRINTF("setting error state\n");
218 s->state = MIG_STATE_ERROR;
e0eb7390 219 notifier_list_notify(&migration_state_notifiers, s);
8b6b99b3
JQ
220 migrate_fd_cleanup(s);
221}
222
458cf28e
JQ
223static void migrate_fd_completed(MigrationState *s)
224{
225 DPRINTF("setting completed state\n");
226 if (migrate_fd_cleanup(s) < 0) {
227 s->state = MIG_STATE_ERROR;
228 } else {
229 s->state = MIG_STATE_COMPLETED;
230 runstate_set(RUN_STATE_POSTMIGRATE);
231 }
e0eb7390 232 notifier_list_notify(&migration_state_notifiers, s);
458cf28e
JQ
233}
234
8b6b99b3 235static void migrate_fd_put_notify(void *opaque)
065e2813 236{
22f00a44 237 MigrationState *s = opaque;
065e2813
AL
238
239 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
240 qemu_file_put_notify(s->file);
1fdc11c3 241 if (s->file && qemu_file_get_error(s->file)) {
2350e13c
YT
242 migrate_fd_error(s);
243 }
065e2813
AL
244}
245
8b6b99b3
JQ
246static ssize_t migrate_fd_put_buffer(void *opaque, const void *data,
247 size_t size)
065e2813 248{
22f00a44 249 MigrationState *s = opaque;
065e2813
AL
250 ssize_t ret;
251
fdbecb5d
JQ
252 if (s->state != MIG_STATE_ACTIVE) {
253 return -EIO;
254 }
255
065e2813
AL
256 do {
257 ret = s->write(s, data, size);
95b134ea 258 } while (ret == -1 && ((s->get_error(s)) == EINTR));
065e2813
AL
259
260 if (ret == -1)
261 ret = -(s->get_error(s));
262
e447b1a6 263 if (ret == -EAGAIN) {
065e2813 264 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
e447b1a6 265 }
065e2813
AL
266
267 return ret;
268}
269
8b6b99b3 270static void migrate_fd_put_ready(void *opaque)
065e2813 271{
22f00a44 272 MigrationState *s = opaque;
065e2813
AL
273 int ret;
274
065e2813 275 if (s->state != MIG_STATE_ACTIVE) {
d0f2c4c6 276 DPRINTF("put_ready returning because of non-active state\n");
065e2813
AL
277 return;
278 }
279
d0f2c4c6 280 DPRINTF("iterate\n");
539de124 281 ret = qemu_savevm_state_iterate(s->file);
39346385
JQ
282 if (ret < 0) {
283 migrate_fd_error(s);
284 } else if (ret == 1) {
1354869c 285 int old_vm_running = runstate_is_running();
eeb34af9 286
d0f2c4c6 287 DPRINTF("done iterating\n");
7b5d3aa2 288 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
8a9236f1 289 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
065e2813 290
539de124 291 if (qemu_savevm_state_complete(s->file) < 0) {
67afff79 292 migrate_fd_error(s);
b161d123 293 } else {
458cf28e 294 migrate_fd_completed(s);
b161d123 295 }
d5f8a570 296 s->total_time = qemu_get_clock_ms(rt_clock) - s->total_time;
48a2f4d6 297 if (s->state != MIG_STATE_COMPLETED) {
41ef56e6
AL
298 if (old_vm_running) {
299 vm_start();
300 }
f5bbfba1 301 }
065e2813
AL
302 }
303}
304
0edda1c4 305static void migrate_fd_cancel(MigrationState *s)
065e2813 306{
065e2813
AL
307 if (s->state != MIG_STATE_ACTIVE)
308 return;
309
d0f2c4c6 310 DPRINTF("cancelling migration\n");
065e2813
AL
311
312 s->state = MIG_STATE_CANCELLED;
e0eb7390 313 notifier_list_notify(&migration_state_notifiers, s);
539de124 314 qemu_savevm_state_cancel(s->file);
065e2813
AL
315
316 migrate_fd_cleanup(s);
317}
318
8b6b99b3 319static void migrate_fd_wait_for_unfreeze(void *opaque)
065e2813 320{
22f00a44 321 MigrationState *s = opaque;
065e2813
AL
322 int ret;
323
d0f2c4c6 324 DPRINTF("wait for unfreeze\n");
065e2813
AL
325 if (s->state != MIG_STATE_ACTIVE)
326 return;
327
328 do {
329 fd_set wfds;
330
331 FD_ZERO(&wfds);
332 FD_SET(s->fd, &wfds);
333
334 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
335 } while (ret == -1 && (s->get_error(s)) == EINTR);
af509450
JQ
336
337 if (ret == -1) {
dcd1d224 338 qemu_file_set_error(s->file, -s->get_error(s));
af509450 339 }
065e2813
AL
340}
341
8b6b99b3 342static int migrate_fd_close(void *opaque)
065e2813 343{
22f00a44 344 MigrationState *s = opaque;
e19252d3
UL
345
346 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
065e2813
AL
347 return s->close(s);
348}
99a0db9b
GH
349
350void add_migration_state_change_notifier(Notifier *notify)
351{
352 notifier_list_add(&migration_state_notifiers, notify);
353}
354
355void remove_migration_state_change_notifier(Notifier *notify)
356{
31552529 357 notifier_remove(notify);
99a0db9b
GH
358}
359
afe2df69
GH
360bool migration_is_active(MigrationState *s)
361{
362 return s->state == MIG_STATE_ACTIVE;
363}
364
7073693b 365bool migration_has_finished(MigrationState *s)
99a0db9b 366{
7073693b 367 return s->state == MIG_STATE_COMPLETED;
99a0db9b 368}
0edda1c4 369
afe2df69
GH
370bool migration_has_failed(MigrationState *s)
371{
372 return (s->state == MIG_STATE_CANCELLED ||
373 s->state == MIG_STATE_ERROR);
374}
375
8b6b99b3 376void migrate_fd_connect(MigrationState *s)
99a0db9b 377{
8b6b99b3
JQ
378 int ret;
379
d5934dde 380 s->state = MIG_STATE_ACTIVE;
8b6b99b3
JQ
381 s->file = qemu_fopen_ops_buffered(s,
382 s->bandwidth_limit,
383 migrate_fd_put_buffer,
384 migrate_fd_put_ready,
385 migrate_fd_wait_for_unfreeze,
386 migrate_fd_close);
387
388 DPRINTF("beginning savevm\n");
6607ae23 389 ret = qemu_savevm_state_begin(s->file, &s->params);
8b6b99b3
JQ
390 if (ret < 0) {
391 DPRINTF("failed, %d\n", ret);
392 migrate_fd_error(s);
393 return;
394 }
395 migrate_fd_put_ready(s);
396}
397
6607ae23 398static MigrationState *migrate_init(const MigrationParams *params)
0edda1c4 399{
17549e84 400 MigrationState *s = migrate_get_current();
d0ae46c1 401 int64_t bandwidth_limit = s->bandwidth_limit;
bbf6da32
OW
402 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
403
404 memcpy(enabled_capabilities, s->enabled_capabilities,
405 sizeof(enabled_capabilities));
0edda1c4 406
17549e84 407 memset(s, 0, sizeof(*s));
d0ae46c1 408 s->bandwidth_limit = bandwidth_limit;
6607ae23 409 s->params = *params;
bbf6da32
OW
410 memcpy(s->enabled_capabilities, enabled_capabilities,
411 sizeof(enabled_capabilities));
1299c631 412
0edda1c4 413 s->bandwidth_limit = bandwidth_limit;
d5934dde 414 s->state = MIG_STATE_SETUP;
d5f8a570 415 s->total_time = qemu_get_clock_ms(rt_clock);
0edda1c4 416
0edda1c4
JQ
417 return s;
418}
cab30143 419
fa2756b7
AL
420static GSList *migration_blockers;
421
422void migrate_add_blocker(Error *reason)
423{
424 migration_blockers = g_slist_prepend(migration_blockers, reason);
425}
426
427void migrate_del_blocker(Error *reason)
428{
429 migration_blockers = g_slist_remove(migration_blockers, reason);
430}
431
e1c37d0e
LC
432void qmp_migrate(const char *uri, bool has_blk, bool blk,
433 bool has_inc, bool inc, bool has_detach, bool detach,
434 Error **errp)
cab30143 435{
17549e84 436 MigrationState *s = migrate_get_current();
6607ae23 437 MigrationParams params;
cab30143 438 const char *p;
cab30143
JQ
439 int ret;
440
6607ae23
IY
441 params.blk = blk;
442 params.shared = inc;
443
17549e84 444 if (s->state == MIG_STATE_ACTIVE) {
e1c37d0e
LC
445 error_set(errp, QERR_MIGRATION_ACTIVE);
446 return;
cab30143
JQ
447 }
448
e1c37d0e
LC
449 if (qemu_savevm_state_blocked(errp)) {
450 return;
cab30143
JQ
451 }
452
fa2756b7 453 if (migration_blockers) {
e1c37d0e
LC
454 *errp = error_copy(migration_blockers->data);
455 return;
fa2756b7
AL
456 }
457
6607ae23 458 s = migrate_init(&params);
cab30143
JQ
459
460 if (strstart(uri, "tcp:", &p)) {
d5c5dacc 461 ret = tcp_start_outgoing_migration(s, p, errp);
cab30143
JQ
462#if !defined(WIN32)
463 } else if (strstart(uri, "exec:", &p)) {
464 ret = exec_start_outgoing_migration(s, p);
465 } else if (strstart(uri, "unix:", &p)) {
466 ret = unix_start_outgoing_migration(s, p);
467 } else if (strstart(uri, "fd:", &p)) {
468 ret = fd_start_outgoing_migration(s, p);
469#endif
99a0db9b 470 } else {
e1c37d0e
LC
471 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
472 return;
cab30143
JQ
473 }
474
475 if (ret < 0) {
d5c5dacc
AK
476 if (!error_is_set(errp)) {
477 DPRINTF("migration failed: %s\n", strerror(-ret));
478 /* FIXME: we should return meaningful errors */
479 error_set(errp, QERR_UNDEFINED_ERROR);
480 }
e1c37d0e 481 return;
1299c631
JQ
482 }
483
e0eb7390 484 notifier_list_notify(&migration_state_notifiers, s);
cab30143
JQ
485}
486
6cdedb07 487void qmp_migrate_cancel(Error **errp)
cab30143 488{
17549e84 489 migrate_fd_cancel(migrate_get_current());
cab30143
JQ
490}
491
3dc85383 492void qmp_migrate_set_speed(int64_t value, Error **errp)
cab30143 493{
cab30143
JQ
494 MigrationState *s;
495
3dc85383
LC
496 if (value < 0) {
497 value = 0;
99a0db9b 498 }
cab30143 499
17549e84 500 s = migrate_get_current();
3dc85383 501 s->bandwidth_limit = value;
d0ae46c1 502 qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
cab30143
JQ
503}
504
4f0a993b 505void qmp_migrate_set_downtime(double value, Error **errp)
cab30143 506{
4f0a993b
LC
507 value *= 1e9;
508 value = MAX(0, MIN(UINT64_MAX, value));
509 max_downtime = (uint64_t)value;
99a0db9b 510}