]> git.proxmox.com Git - qemu.git/blame - migration.c
migration: make writes blocking
[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
2c9adcb8 319void migrate_fd_put_ready(MigrationState *s)
065e2813
AL
320{
321 int ret;
322
065e2813 323 if (s->state != MIG_STATE_ACTIVE) {
d0f2c4c6 324 DPRINTF("put_ready returning because of non-active state\n");
065e2813
AL
325 return;
326 }
766bd176
JQ
327 if (s->first_time) {
328 s->first_time = false;
329 DPRINTF("beginning savevm\n");
330 ret = qemu_savevm_state_begin(s->file, &s->params);
331 if (ret < 0) {
332 DPRINTF("failed, %d\n", ret);
333 migrate_fd_error(s);
334 return;
335 }
336 }
065e2813 337
d0f2c4c6 338 DPRINTF("iterate\n");
539de124 339 ret = qemu_savevm_state_iterate(s->file);
39346385
JQ
340 if (ret < 0) {
341 migrate_fd_error(s);
342 } else if (ret == 1) {
1354869c 343 int old_vm_running = runstate_is_running();
9c5a9fcf 344 int64_t start_time, end_time;
eeb34af9 345
d0f2c4c6 346 DPRINTF("done iterating\n");
9c5a9fcf 347 start_time = qemu_get_clock_ms(rt_clock);
7b5d3aa2 348 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
766bd176
JQ
349 if (old_vm_running) {
350 vm_stop(RUN_STATE_FINISH_MIGRATE);
351 } else {
352 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
353 }
065e2813 354
539de124 355 if (qemu_savevm_state_complete(s->file) < 0) {
67afff79 356 migrate_fd_error(s);
b161d123 357 } else {
458cf28e 358 migrate_fd_completed(s);
b161d123 359 }
97d4d961
JQ
360 end_time = qemu_get_clock_ms(rt_clock);
361 s->total_time = end_time - s->total_time;
9c5a9fcf 362 s->downtime = end_time - start_time;
48a2f4d6 363 if (s->state != MIG_STATE_COMPLETED) {
41ef56e6
AL
364 if (old_vm_running) {
365 vm_start();
366 }
f5bbfba1 367 }
065e2813
AL
368 }
369}
370
0edda1c4 371static void migrate_fd_cancel(MigrationState *s)
065e2813 372{
065e2813
AL
373 if (s->state != MIG_STATE_ACTIVE)
374 return;
375
d0f2c4c6 376 DPRINTF("cancelling migration\n");
065e2813
AL
377
378 s->state = MIG_STATE_CANCELLED;
e0eb7390 379 notifier_list_notify(&migration_state_notifiers, s);
539de124 380 qemu_savevm_state_cancel(s->file);
065e2813
AL
381
382 migrate_fd_cleanup(s);
383}
384
9499743f 385int migrate_fd_wait_for_unfreeze(MigrationState *s)
065e2813 386{
065e2813
AL
387 int ret;
388
d0f2c4c6 389 DPRINTF("wait for unfreeze\n");
065e2813 390 if (s->state != MIG_STATE_ACTIVE)
9499743f 391 return -EINVAL;
065e2813
AL
392
393 do {
394 fd_set wfds;
395
396 FD_ZERO(&wfds);
397 FD_SET(s->fd, &wfds);
398
399 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
400 } while (ret == -1 && (s->get_error(s)) == EINTR);
af509450
JQ
401
402 if (ret == -1) {
9499743f 403 return -s->get_error(s);
af509450 404 }
9499743f 405 return 0;
065e2813
AL
406}
407
11c76741 408int migrate_fd_close(MigrationState *s)
065e2813 409{
8dc592e6
PB
410 int rc = 0;
411 if (s->fd != -1) {
8dc592e6
PB
412 rc = s->close(s);
413 s->fd = -1;
414 }
415 return rc;
065e2813 416}
99a0db9b
GH
417
418void add_migration_state_change_notifier(Notifier *notify)
419{
420 notifier_list_add(&migration_state_notifiers, notify);
421}
422
423void remove_migration_state_change_notifier(Notifier *notify)
424{
31552529 425 notifier_remove(notify);
99a0db9b
GH
426}
427
afe2df69
GH
428bool migration_is_active(MigrationState *s)
429{
430 return s->state == MIG_STATE_ACTIVE;
431}
432
7073693b 433bool migration_has_finished(MigrationState *s)
99a0db9b 434{
7073693b 435 return s->state == MIG_STATE_COMPLETED;
99a0db9b 436}
0edda1c4 437
afe2df69
GH
438bool migration_has_failed(MigrationState *s)
439{
440 return (s->state == MIG_STATE_CANCELLED ||
441 s->state == MIG_STATE_ERROR);
442}
443
8b6b99b3 444void migrate_fd_connect(MigrationState *s)
99a0db9b 445{
d5934dde 446 s->state = MIG_STATE_ACTIVE;
766bd176 447 s->first_time = true;
edfa1af5 448 qemu_fopen_ops_buffered(s);
8b6b99b3
JQ
449}
450
6607ae23 451static MigrationState *migrate_init(const MigrationParams *params)
0edda1c4 452{
17549e84 453 MigrationState *s = migrate_get_current();
d0ae46c1 454 int64_t bandwidth_limit = s->bandwidth_limit;
bbf6da32 455 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
17ad9b35 456 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
bbf6da32
OW
457
458 memcpy(enabled_capabilities, s->enabled_capabilities,
459 sizeof(enabled_capabilities));
0edda1c4 460
17549e84 461 memset(s, 0, sizeof(*s));
d0ae46c1 462 s->bandwidth_limit = bandwidth_limit;
6607ae23 463 s->params = *params;
bbf6da32
OW
464 memcpy(s->enabled_capabilities, enabled_capabilities,
465 sizeof(enabled_capabilities));
17ad9b35 466 s->xbzrle_cache_size = xbzrle_cache_size;
1299c631 467
0edda1c4 468 s->bandwidth_limit = bandwidth_limit;
d5934dde 469 s->state = MIG_STATE_SETUP;
d5f8a570 470 s->total_time = qemu_get_clock_ms(rt_clock);
0edda1c4 471
0edda1c4
JQ
472 return s;
473}
cab30143 474
fa2756b7
AL
475static GSList *migration_blockers;
476
477void migrate_add_blocker(Error *reason)
478{
479 migration_blockers = g_slist_prepend(migration_blockers, reason);
480}
481
482void migrate_del_blocker(Error *reason)
483{
484 migration_blockers = g_slist_remove(migration_blockers, reason);
485}
486
e1c37d0e
LC
487void qmp_migrate(const char *uri, bool has_blk, bool blk,
488 bool has_inc, bool inc, bool has_detach, bool detach,
489 Error **errp)
cab30143 490{
be7059cd 491 Error *local_err = NULL;
17549e84 492 MigrationState *s = migrate_get_current();
6607ae23 493 MigrationParams params;
cab30143 494 const char *p;
cab30143 495
6607ae23
IY
496 params.blk = blk;
497 params.shared = inc;
498
17549e84 499 if (s->state == MIG_STATE_ACTIVE) {
e1c37d0e
LC
500 error_set(errp, QERR_MIGRATION_ACTIVE);
501 return;
cab30143
JQ
502 }
503
e1c37d0e
LC
504 if (qemu_savevm_state_blocked(errp)) {
505 return;
cab30143
JQ
506 }
507
fa2756b7 508 if (migration_blockers) {
e1c37d0e
LC
509 *errp = error_copy(migration_blockers->data);
510 return;
fa2756b7
AL
511 }
512
6607ae23 513 s = migrate_init(&params);
cab30143
JQ
514
515 if (strstart(uri, "tcp:", &p)) {
f37afb5a 516 tcp_start_outgoing_migration(s, p, &local_err);
cab30143
JQ
517#if !defined(WIN32)
518 } else if (strstart(uri, "exec:", &p)) {
f37afb5a 519 exec_start_outgoing_migration(s, p, &local_err);
cab30143 520 } else if (strstart(uri, "unix:", &p)) {
f37afb5a 521 unix_start_outgoing_migration(s, p, &local_err);
cab30143 522 } else if (strstart(uri, "fd:", &p)) {
f37afb5a 523 fd_start_outgoing_migration(s, p, &local_err);
cab30143 524#endif
99a0db9b 525 } else {
e1c37d0e
LC
526 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
527 return;
cab30143
JQ
528 }
529
f37afb5a 530 if (local_err) {
342ab8d1 531 migrate_fd_error(s);
f37afb5a 532 error_propagate(errp, local_err);
e1c37d0e 533 return;
1299c631
JQ
534 }
535
e0eb7390 536 notifier_list_notify(&migration_state_notifiers, s);
cab30143
JQ
537}
538
6cdedb07 539void qmp_migrate_cancel(Error **errp)
cab30143 540{
17549e84 541 migrate_fd_cancel(migrate_get_current());
cab30143
JQ
542}
543
9e1ba4cc
OW
544void qmp_migrate_set_cache_size(int64_t value, Error **errp)
545{
546 MigrationState *s = migrate_get_current();
547
548 /* Check for truncation */
549 if (value != (size_t)value) {
550 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
551 "exceeding address space");
552 return;
553 }
554
555 s->xbzrle_cache_size = xbzrle_cache_resize(value);
556}
557
558int64_t qmp_query_migrate_cache_size(Error **errp)
559{
560 return migrate_xbzrle_cache_size();
561}
562
3dc85383 563void qmp_migrate_set_speed(int64_t value, Error **errp)
cab30143 564{
cab30143
JQ
565 MigrationState *s;
566
3dc85383
LC
567 if (value < 0) {
568 value = 0;
99a0db9b 569 }
cab30143 570
17549e84 571 s = migrate_get_current();
3dc85383 572 s->bandwidth_limit = value;
d0ae46c1 573 qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
cab30143
JQ
574}
575
4f0a993b 576void qmp_migrate_set_downtime(double value, Error **errp)
cab30143 577{
4f0a993b
LC
578 value *= 1e9;
579 value = MAX(0, MIN(UINT64_MAX, value));
580 max_downtime = (uint64_t)value;
99a0db9b 581}
17ad9b35
OW
582
583int migrate_use_xbzrle(void)
584{
585 MigrationState *s;
586
587 s = migrate_get_current();
588
589 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
590}
591
592int64_t migrate_xbzrle_cache_size(void)
593{
594 MigrationState *s;
595
596 s = migrate_get_current();
597
598 return s->xbzrle_cache_size;
599}