]> git.proxmox.com Git - mirror_qemu.git/blame - migration.c
savevm: New save live migration method: pending
[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"
caf71f86 17#include "migration/migration.h"
83c9089e 18#include "monitor/monitor.h"
065e2813 19#include "buffered_file.h"
9c17d615 20#include "sysemu/sysemu.h"
737e150e 21#include "block/block.h"
1de7afc9 22#include "qemu/sockets.h"
caf71f86 23#include "migration/block.h"
766bd176 24#include "qemu/thread.h"
791e7c82 25#include "qmp-commands.h"
065e2813
AL
26
27//#define DEBUG_MIGRATION
28
29#ifdef DEBUG_MIGRATION
d0f2c4c6 30#define DPRINTF(fmt, ...) \
065e2813
AL
31 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
32#else
d0f2c4c6 33#define DPRINTF(fmt, ...) \
065e2813
AL
34 do { } while (0)
35#endif
5bb7910a 36
7dc688ed
JQ
37enum {
38 MIG_STATE_ERROR,
39 MIG_STATE_SETUP,
40 MIG_STATE_CANCELLED,
41 MIG_STATE_ACTIVE,
42 MIG_STATE_COMPLETED,
43};
5bb7910a 44
d0ae46c1 45#define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
5bb7910a 46
17ad9b35
OW
47/* Migration XBZRLE default cache size */
48#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
49
99a0db9b
GH
50static NotifierList migration_state_notifiers =
51 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
52
17549e84
JQ
53/* When we add fault tolerance, we could have several
54 migrations at once. For now we don't need to add
55 dynamic creation of migration */
56
859bc756 57MigrationState *migrate_get_current(void)
17549e84
JQ
58{
59 static MigrationState current_migration = {
60 .state = MIG_STATE_SETUP,
d0ae46c1 61 .bandwidth_limit = MAX_THROTTLE,
17ad9b35 62 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
17549e84
JQ
63 };
64
65 return &current_migration;
66}
67
43eaae28 68void qemu_start_incoming_migration(const char *uri, Error **errp)
5bb7910a 69{
34c9dd8e
AL
70 const char *p;
71
72 if (strstart(uri, "tcp:", &p))
43eaae28 73 tcp_start_incoming_migration(p, errp);
065e2813
AL
74#if !defined(WIN32)
75 else if (strstart(uri, "exec:", &p))
43eaae28 76 exec_start_incoming_migration(p, errp);
4951f65b 77 else if (strstart(uri, "unix:", &p))
43eaae28 78 unix_start_incoming_migration(p, errp);
5ac1fad3 79 else if (strstart(uri, "fd:", &p))
43eaae28 80 fd_start_incoming_migration(p, errp);
065e2813 81#endif
8ca5e801 82 else {
43eaae28 83 error_setg(errp, "unknown migration protocol: %s\n", uri);
8ca5e801 84 }
5bb7910a
AL
85}
86
82a4da79 87static void process_incoming_migration_co(void *opaque)
511c0231 88{
82a4da79 89 QEMUFile *f = opaque;
1c12e1f5
PB
90 int ret;
91
92 ret = qemu_loadvm_state(f);
82a4da79 93 qemu_set_fd_handler(qemu_get_fd(f), NULL, NULL, NULL);
1c12e1f5
PB
94 qemu_fclose(f);
95 if (ret < 0) {
511c0231
JQ
96 fprintf(stderr, "load of migration failed\n");
97 exit(0);
98 }
99 qemu_announce_self();
100 DPRINTF("successfully loaded vm state\n");
101
901862cb 102 bdrv_clear_incoming_migration_all();
0f15423c
AL
103 /* Make sure all file formats flush their mutable metadata */
104 bdrv_invalidate_cache_all();
105
f5bbfba1 106 if (autostart) {
511c0231 107 vm_start();
f5bbfba1 108 } else {
29ed72f1 109 runstate_set(RUN_STATE_PAUSED);
f5bbfba1 110 }
511c0231
JQ
111}
112
82a4da79
PB
113static void enter_migration_coroutine(void *opaque)
114{
115 Coroutine *co = opaque;
116 qemu_coroutine_enter(co, NULL);
117}
118
119void process_incoming_migration(QEMUFile *f)
120{
121 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
122 int fd = qemu_get_fd(f);
123
124 assert(fd != -1);
125 socket_set_nonblock(fd);
126 qemu_set_fd_handler(fd, enter_migration_coroutine, NULL, co);
127 qemu_coroutine_enter(co, f);
128}
129
a0a3fd60
GC
130/* amount of nanoseconds we are willing to wait for migration to be down.
131 * the choice of nanoseconds is because it is the maximum resolution that
132 * get_clock() can achieve. It is an internal measure. All user-visible
133 * units must be in seconds */
134static uint64_t max_downtime = 30000000;
135
136uint64_t migrate_max_downtime(void)
137{
138 return max_downtime;
139}
140
bbf6da32
OW
141MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
142{
143 MigrationCapabilityStatusList *head = NULL;
144 MigrationCapabilityStatusList *caps;
145 MigrationState *s = migrate_get_current();
146 int i;
147
148 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
149 if (head == NULL) {
150 head = g_malloc0(sizeof(*caps));
151 caps = head;
152 } else {
153 caps->next = g_malloc0(sizeof(*caps));
154 caps = caps->next;
155 }
156 caps->value =
157 g_malloc(sizeof(*caps->value));
158 caps->value->capability = i;
159 caps->value->state = s->enabled_capabilities[i];
160 }
161
162 return head;
163}
164
f36d55af
OW
165static void get_xbzrle_cache_stats(MigrationInfo *info)
166{
167 if (migrate_use_xbzrle()) {
168 info->has_xbzrle_cache = true;
169 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
170 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
171 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
172 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
173 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
174 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
175 }
176}
177
791e7c82 178MigrationInfo *qmp_query_migrate(Error **errp)
5bb7910a 179{
791e7c82 180 MigrationInfo *info = g_malloc0(sizeof(*info));
17549e84
JQ
181 MigrationState *s = migrate_get_current();
182
183 switch (s->state) {
184 case MIG_STATE_SETUP:
185 /* no migration has happened ever */
186 break;
187 case MIG_STATE_ACTIVE:
791e7c82
LC
188 info->has_status = true;
189 info->status = g_strdup("active");
7aa939af
JQ
190 info->has_total_time = true;
191 info->total_time = qemu_get_clock_ms(rt_clock)
192 - s->total_time;
2c52ddf1
JQ
193 info->has_expected_downtime = true;
194 info->expected_downtime = s->expected_downtime;
17549e84 195
791e7c82
LC
196 info->has_ram = true;
197 info->ram = g_malloc0(sizeof(*info->ram));
198 info->ram->transferred = ram_bytes_transferred();
199 info->ram->remaining = ram_bytes_remaining();
200 info->ram->total = ram_bytes_total();
004d4c10
OW
201 info->ram->duplicate = dup_mig_pages_transferred();
202 info->ram->normal = norm_mig_pages_transferred();
203 info->ram->normal_bytes = norm_mig_bytes_transferred();
8d017193
JQ
204 info->ram->dirty_pages_rate = s->dirty_pages_rate;
205
17549e84
JQ
206
207 if (blk_mig_active()) {
791e7c82
LC
208 info->has_disk = true;
209 info->disk = g_malloc0(sizeof(*info->disk));
210 info->disk->transferred = blk_mig_bytes_transferred();
211 info->disk->remaining = blk_mig_bytes_remaining();
212 info->disk->total = blk_mig_bytes_total();
ff8d81d8 213 }
f36d55af
OW
214
215 get_xbzrle_cache_stats(info);
17549e84
JQ
216 break;
217 case MIG_STATE_COMPLETED:
f36d55af
OW
218 get_xbzrle_cache_stats(info);
219
791e7c82
LC
220 info->has_status = true;
221 info->status = g_strdup("completed");
7aa939af 222 info->total_time = s->total_time;
9c5a9fcf
JQ
223 info->has_downtime = true;
224 info->downtime = s->downtime;
d5f8a570
JQ
225
226 info->has_ram = true;
227 info->ram = g_malloc0(sizeof(*info->ram));
228 info->ram->transferred = ram_bytes_transferred();
229 info->ram->remaining = 0;
230 info->ram->total = ram_bytes_total();
004d4c10
OW
231 info->ram->duplicate = dup_mig_pages_transferred();
232 info->ram->normal = norm_mig_pages_transferred();
233 info->ram->normal_bytes = norm_mig_bytes_transferred();
17549e84
JQ
234 break;
235 case MIG_STATE_ERROR:
791e7c82
LC
236 info->has_status = true;
237 info->status = g_strdup("failed");
17549e84
JQ
238 break;
239 case MIG_STATE_CANCELLED:
791e7c82
LC
240 info->has_status = true;
241 info->status = g_strdup("cancelled");
17549e84 242 break;
5bb7910a 243 }
791e7c82
LC
244
245 return info;
5bb7910a
AL
246}
247
00458433
OW
248void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
249 Error **errp)
250{
251 MigrationState *s = migrate_get_current();
252 MigrationCapabilityStatusList *cap;
253
254 if (s->state == MIG_STATE_ACTIVE) {
255 error_set(errp, QERR_MIGRATION_ACTIVE);
256 return;
257 }
258
259 for (cap = params; cap; cap = cap->next) {
260 s->enabled_capabilities[cap->value->capability] = cap->value->state;
261 }
262}
263
065e2813
AL
264/* shared migration helpers */
265
8b6b99b3 266static int migrate_fd_cleanup(MigrationState *s)
065e2813 267{
41ef56e6
AL
268 int ret = 0;
269
065e2813 270 if (s->file) {
d0f2c4c6 271 DPRINTF("closing file\n");
a6d34a94 272 ret = qemu_fclose(s->file);
5d39c799 273 s->file = NULL;
065e2813
AL
274 }
275
24ea1e4b 276 assert(s->fd == -1);
41ef56e6 277 return ret;
065e2813
AL
278}
279
8b6b99b3 280void migrate_fd_error(MigrationState *s)
065e2813 281{
8b6b99b3
JQ
282 DPRINTF("setting error state\n");
283 s->state = MIG_STATE_ERROR;
e0eb7390 284 notifier_list_notify(&migration_state_notifiers, s);
8b6b99b3
JQ
285 migrate_fd_cleanup(s);
286}
287
458cf28e
JQ
288static void migrate_fd_completed(MigrationState *s)
289{
290 DPRINTF("setting completed state\n");
291 if (migrate_fd_cleanup(s) < 0) {
292 s->state = MIG_STATE_ERROR;
293 } else {
294 s->state = MIG_STATE_COMPLETED;
295 runstate_set(RUN_STATE_POSTMIGRATE);
296 }
e0eb7390 297 notifier_list_notify(&migration_state_notifiers, s);
458cf28e
JQ
298}
299
c87b015b
JQ
300ssize_t migrate_fd_put_buffer(MigrationState *s, const void *data,
301 size_t size)
065e2813 302{
065e2813
AL
303 ssize_t ret;
304
fdbecb5d
JQ
305 if (s->state != MIG_STATE_ACTIVE) {
306 return -EIO;
307 }
308
065e2813
AL
309 do {
310 ret = s->write(s, data, size);
95b134ea 311 } while (ret == -1 && ((s->get_error(s)) == EINTR));
065e2813
AL
312
313 if (ret == -1)
314 ret = -(s->get_error(s));
315
065e2813
AL
316 return ret;
317}
318
e4ed1541 319bool migrate_fd_put_ready(MigrationState *s, uint64_t max_size)
065e2813
AL
320{
321 int ret;
e4ed1541
JQ
322 uint64_t pending_size;
323 bool last_round = false;
065e2813 324
e7627482 325 qemu_mutex_lock_iothread();
065e2813 326 if (s->state != MIG_STATE_ACTIVE) {
d0f2c4c6 327 DPRINTF("put_ready returning because of non-active state\n");
e7627482 328 qemu_mutex_unlock_iothread();
e4ed1541 329 return false;
065e2813 330 }
766bd176
JQ
331 if (s->first_time) {
332 s->first_time = false;
333 DPRINTF("beginning savevm\n");
334 ret = qemu_savevm_state_begin(s->file, &s->params);
335 if (ret < 0) {
336 DPRINTF("failed, %d\n", ret);
337 migrate_fd_error(s);
e7627482 338 qemu_mutex_unlock_iothread();
e4ed1541 339 return false;
766bd176
JQ
340 }
341 }
065e2813 342
d0f2c4c6 343 DPRINTF("iterate\n");
e4ed1541
JQ
344 pending_size = qemu_savevm_state_pending(s->file, max_size);
345 DPRINTF("pending size %lu max %lu\n", pending_size, max_size);
346 if (pending_size >= max_size) {
347 ret = qemu_savevm_state_iterate(s->file);
348 if (ret < 0) {
349 migrate_fd_error(s);
350 }
351 } else {
1354869c 352 int old_vm_running = runstate_is_running();
9c5a9fcf 353 int64_t start_time, end_time;
eeb34af9 354
d0f2c4c6 355 DPRINTF("done iterating\n");
9c5a9fcf 356 start_time = qemu_get_clock_ms(rt_clock);
7b5d3aa2 357 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
766bd176
JQ
358 if (old_vm_running) {
359 vm_stop(RUN_STATE_FINISH_MIGRATE);
360 } else {
361 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
362 }
065e2813 363
539de124 364 if (qemu_savevm_state_complete(s->file) < 0) {
67afff79 365 migrate_fd_error(s);
b161d123 366 } else {
458cf28e 367 migrate_fd_completed(s);
b161d123 368 }
97d4d961
JQ
369 end_time = qemu_get_clock_ms(rt_clock);
370 s->total_time = end_time - s->total_time;
9c5a9fcf 371 s->downtime = end_time - start_time;
48a2f4d6 372 if (s->state != MIG_STATE_COMPLETED) {
41ef56e6
AL
373 if (old_vm_running) {
374 vm_start();
375 }
f5bbfba1 376 }
e4ed1541 377 last_round = true;
065e2813 378 }
e7627482
JQ
379 qemu_mutex_unlock_iothread();
380
e4ed1541 381 return last_round;
065e2813
AL
382}
383
0edda1c4 384static void migrate_fd_cancel(MigrationState *s)
065e2813 385{
065e2813
AL
386 if (s->state != MIG_STATE_ACTIVE)
387 return;
388
d0f2c4c6 389 DPRINTF("cancelling migration\n");
065e2813
AL
390
391 s->state = MIG_STATE_CANCELLED;
e0eb7390 392 notifier_list_notify(&migration_state_notifiers, s);
539de124 393 qemu_savevm_state_cancel(s->file);
065e2813
AL
394
395 migrate_fd_cleanup(s);
396}
397
11c76741 398int migrate_fd_close(MigrationState *s)
065e2813 399{
8dc592e6
PB
400 int rc = 0;
401 if (s->fd != -1) {
8dc592e6
PB
402 rc = s->close(s);
403 s->fd = -1;
404 }
405 return rc;
065e2813 406}
99a0db9b
GH
407
408void add_migration_state_change_notifier(Notifier *notify)
409{
410 notifier_list_add(&migration_state_notifiers, notify);
411}
412
413void remove_migration_state_change_notifier(Notifier *notify)
414{
31552529 415 notifier_remove(notify);
99a0db9b
GH
416}
417
afe2df69
GH
418bool migration_is_active(MigrationState *s)
419{
420 return s->state == MIG_STATE_ACTIVE;
421}
422
7073693b 423bool migration_has_finished(MigrationState *s)
99a0db9b 424{
7073693b 425 return s->state == MIG_STATE_COMPLETED;
99a0db9b 426}
0edda1c4 427
afe2df69
GH
428bool migration_has_failed(MigrationState *s)
429{
430 return (s->state == MIG_STATE_CANCELLED ||
431 s->state == MIG_STATE_ERROR);
432}
433
8b6b99b3 434void migrate_fd_connect(MigrationState *s)
99a0db9b 435{
d5934dde 436 s->state = MIG_STATE_ACTIVE;
766bd176 437 s->first_time = true;
edfa1af5 438 qemu_fopen_ops_buffered(s);
8b6b99b3
JQ
439}
440
6607ae23 441static MigrationState *migrate_init(const MigrationParams *params)
0edda1c4 442{
17549e84 443 MigrationState *s = migrate_get_current();
d0ae46c1 444 int64_t bandwidth_limit = s->bandwidth_limit;
bbf6da32 445 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
17ad9b35 446 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
bbf6da32
OW
447
448 memcpy(enabled_capabilities, s->enabled_capabilities,
449 sizeof(enabled_capabilities));
0edda1c4 450
17549e84 451 memset(s, 0, sizeof(*s));
d0ae46c1 452 s->bandwidth_limit = bandwidth_limit;
6607ae23 453 s->params = *params;
bbf6da32
OW
454 memcpy(s->enabled_capabilities, enabled_capabilities,
455 sizeof(enabled_capabilities));
17ad9b35 456 s->xbzrle_cache_size = xbzrle_cache_size;
1299c631 457
0edda1c4 458 s->bandwidth_limit = bandwidth_limit;
d5934dde 459 s->state = MIG_STATE_SETUP;
d5f8a570 460 s->total_time = qemu_get_clock_ms(rt_clock);
0edda1c4 461
0edda1c4
JQ
462 return s;
463}
cab30143 464
fa2756b7
AL
465static GSList *migration_blockers;
466
467void migrate_add_blocker(Error *reason)
468{
469 migration_blockers = g_slist_prepend(migration_blockers, reason);
470}
471
472void migrate_del_blocker(Error *reason)
473{
474 migration_blockers = g_slist_remove(migration_blockers, reason);
475}
476
e1c37d0e
LC
477void qmp_migrate(const char *uri, bool has_blk, bool blk,
478 bool has_inc, bool inc, bool has_detach, bool detach,
479 Error **errp)
cab30143 480{
be7059cd 481 Error *local_err = NULL;
17549e84 482 MigrationState *s = migrate_get_current();
6607ae23 483 MigrationParams params;
cab30143 484 const char *p;
cab30143 485
6607ae23
IY
486 params.blk = blk;
487 params.shared = inc;
488
17549e84 489 if (s->state == MIG_STATE_ACTIVE) {
e1c37d0e
LC
490 error_set(errp, QERR_MIGRATION_ACTIVE);
491 return;
cab30143
JQ
492 }
493
e1c37d0e
LC
494 if (qemu_savevm_state_blocked(errp)) {
495 return;
cab30143
JQ
496 }
497
fa2756b7 498 if (migration_blockers) {
e1c37d0e
LC
499 *errp = error_copy(migration_blockers->data);
500 return;
fa2756b7
AL
501 }
502
6607ae23 503 s = migrate_init(&params);
cab30143
JQ
504
505 if (strstart(uri, "tcp:", &p)) {
f37afb5a 506 tcp_start_outgoing_migration(s, p, &local_err);
cab30143
JQ
507#if !defined(WIN32)
508 } else if (strstart(uri, "exec:", &p)) {
f37afb5a 509 exec_start_outgoing_migration(s, p, &local_err);
cab30143 510 } else if (strstart(uri, "unix:", &p)) {
f37afb5a 511 unix_start_outgoing_migration(s, p, &local_err);
cab30143 512 } else if (strstart(uri, "fd:", &p)) {
f37afb5a 513 fd_start_outgoing_migration(s, p, &local_err);
cab30143 514#endif
99a0db9b 515 } else {
e1c37d0e
LC
516 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
517 return;
cab30143
JQ
518 }
519
f37afb5a 520 if (local_err) {
342ab8d1 521 migrate_fd_error(s);
f37afb5a 522 error_propagate(errp, local_err);
e1c37d0e 523 return;
1299c631
JQ
524 }
525
e0eb7390 526 notifier_list_notify(&migration_state_notifiers, s);
cab30143
JQ
527}
528
6cdedb07 529void qmp_migrate_cancel(Error **errp)
cab30143 530{
17549e84 531 migrate_fd_cancel(migrate_get_current());
cab30143
JQ
532}
533
9e1ba4cc
OW
534void qmp_migrate_set_cache_size(int64_t value, Error **errp)
535{
536 MigrationState *s = migrate_get_current();
537
538 /* Check for truncation */
539 if (value != (size_t)value) {
540 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
541 "exceeding address space");
542 return;
543 }
544
545 s->xbzrle_cache_size = xbzrle_cache_resize(value);
546}
547
548int64_t qmp_query_migrate_cache_size(Error **errp)
549{
550 return migrate_xbzrle_cache_size();
551}
552
3dc85383 553void qmp_migrate_set_speed(int64_t value, Error **errp)
cab30143 554{
cab30143
JQ
555 MigrationState *s;
556
3dc85383
LC
557 if (value < 0) {
558 value = 0;
99a0db9b 559 }
cab30143 560
17549e84 561 s = migrate_get_current();
3dc85383 562 s->bandwidth_limit = value;
d0ae46c1 563 qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
cab30143
JQ
564}
565
4f0a993b 566void qmp_migrate_set_downtime(double value, Error **errp)
cab30143 567{
4f0a993b
LC
568 value *= 1e9;
569 value = MAX(0, MIN(UINT64_MAX, value));
570 max_downtime = (uint64_t)value;
99a0db9b 571}
17ad9b35
OW
572
573int migrate_use_xbzrle(void)
574{
575 MigrationState *s;
576
577 s = migrate_get_current();
578
579 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
580}
581
582int64_t migrate_xbzrle_cache_size(void)
583{
584 MigrationState *s;
585
586 s = migrate_get_current();
587
588 return s->xbzrle_cache_size;
589}