migrate_incoming: Cleanup/clarify error messages
[qemu.git] / migration / migration.c
blob1e44d9bb83d7171ade1cade5ce27f58ead0cbf9e
1 /*
2 * QEMU live migration
4 * Copyright IBM, Corp. 2008
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
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.
16 #include "qemu-common.h"
17 #include "qemu/main-loop.h"
18 #include "migration/migration.h"
19 #include "monitor/monitor.h"
20 #include "migration/qemu-file.h"
21 #include "sysemu/sysemu.h"
22 #include "block/block.h"
23 #include "qemu/sockets.h"
24 #include "migration/block.h"
25 #include "qemu/thread.h"
26 #include "qmp-commands.h"
27 #include "trace.h"
29 enum {
30 MIG_STATE_ERROR = -1,
31 MIG_STATE_NONE,
32 MIG_STATE_SETUP,
33 MIG_STATE_CANCELLING,
34 MIG_STATE_CANCELLED,
35 MIG_STATE_ACTIVE,
36 MIG_STATE_COMPLETED,
39 #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
41 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
42 * data. */
43 #define BUFFER_DELAY 100
44 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
46 /* Migration XBZRLE default cache size */
47 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
49 static NotifierList migration_state_notifiers =
50 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
52 static bool deferred_incoming;
54 /* When we add fault tolerance, we could have several
55 migrations at once. For now we don't need to add
56 dynamic creation of migration */
58 MigrationState *migrate_get_current(void)
60 static MigrationState current_migration = {
61 .state = MIG_STATE_NONE,
62 .bandwidth_limit = MAX_THROTTLE,
63 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
64 .mbps = -1,
67 return &current_migration;
71 * Called on -incoming with a defer: uri.
72 * The migration can be started later after any parameters have been
73 * changed.
75 static void deferred_incoming_migration(Error **errp)
77 if (deferred_incoming) {
78 error_setg(errp, "Incoming migration already deferred");
80 deferred_incoming = true;
83 void qemu_start_incoming_migration(const char *uri, Error **errp)
85 const char *p;
87 if (!strcmp(uri, "defer")) {
88 deferred_incoming_migration(errp);
89 } else if (strstart(uri, "tcp:", &p)) {
90 tcp_start_incoming_migration(p, errp);
91 #ifdef CONFIG_RDMA
92 } else if (strstart(uri, "rdma:", &p)) {
93 rdma_start_incoming_migration(p, errp);
94 #endif
95 #if !defined(WIN32)
96 } else if (strstart(uri, "exec:", &p)) {
97 exec_start_incoming_migration(p, errp);
98 } else if (strstart(uri, "unix:", &p)) {
99 unix_start_incoming_migration(p, errp);
100 } else if (strstart(uri, "fd:", &p)) {
101 fd_start_incoming_migration(p, errp);
102 #endif
103 } else {
104 error_setg(errp, "unknown migration protocol: %s", uri);
108 static void process_incoming_migration_co(void *opaque)
110 QEMUFile *f = opaque;
111 Error *local_err = NULL;
112 int ret;
114 ret = qemu_loadvm_state(f);
115 qemu_fclose(f);
116 free_xbzrle_decoded_buf();
117 if (ret < 0) {
118 error_report("load of migration failed: %s", strerror(-ret));
119 exit(EXIT_FAILURE);
121 qemu_announce_self();
123 /* Make sure all file formats flush their mutable metadata */
124 bdrv_invalidate_cache_all(&local_err);
125 if (local_err) {
126 error_report_err(local_err);
127 exit(EXIT_FAILURE);
130 if (autostart) {
131 vm_start();
132 } else {
133 runstate_set(RUN_STATE_PAUSED);
137 void process_incoming_migration(QEMUFile *f)
139 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
140 int fd = qemu_get_fd(f);
142 assert(fd != -1);
143 qemu_set_nonblock(fd);
144 qemu_coroutine_enter(co, f);
147 /* amount of nanoseconds we are willing to wait for migration to be down.
148 * the choice of nanoseconds is because it is the maximum resolution that
149 * get_clock() can achieve. It is an internal measure. All user-visible
150 * units must be in seconds */
151 static uint64_t max_downtime = 300000000;
153 uint64_t migrate_max_downtime(void)
155 return max_downtime;
158 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
160 MigrationCapabilityStatusList *head = NULL;
161 MigrationCapabilityStatusList *caps;
162 MigrationState *s = migrate_get_current();
163 int i;
165 caps = NULL; /* silence compiler warning */
166 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
167 if (head == NULL) {
168 head = g_malloc0(sizeof(*caps));
169 caps = head;
170 } else {
171 caps->next = g_malloc0(sizeof(*caps));
172 caps = caps->next;
174 caps->value =
175 g_malloc(sizeof(*caps->value));
176 caps->value->capability = i;
177 caps->value->state = s->enabled_capabilities[i];
180 return head;
183 static void get_xbzrle_cache_stats(MigrationInfo *info)
185 if (migrate_use_xbzrle()) {
186 info->has_xbzrle_cache = true;
187 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
188 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
189 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
190 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
191 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
192 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
193 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
197 MigrationInfo *qmp_query_migrate(Error **errp)
199 MigrationInfo *info = g_malloc0(sizeof(*info));
200 MigrationState *s = migrate_get_current();
202 switch (s->state) {
203 case MIG_STATE_NONE:
204 /* no migration has happened ever */
205 break;
206 case MIG_STATE_SETUP:
207 info->has_status = true;
208 info->status = g_strdup("setup");
209 info->has_total_time = false;
210 break;
211 case MIG_STATE_ACTIVE:
212 case MIG_STATE_CANCELLING:
213 info->has_status = true;
214 info->status = g_strdup("active");
215 info->has_total_time = true;
216 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
217 - s->total_time;
218 info->has_expected_downtime = true;
219 info->expected_downtime = s->expected_downtime;
220 info->has_setup_time = true;
221 info->setup_time = s->setup_time;
223 info->has_ram = true;
224 info->ram = g_malloc0(sizeof(*info->ram));
225 info->ram->transferred = ram_bytes_transferred();
226 info->ram->remaining = ram_bytes_remaining();
227 info->ram->total = ram_bytes_total();
228 info->ram->duplicate = dup_mig_pages_transferred();
229 info->ram->skipped = skipped_mig_pages_transferred();
230 info->ram->normal = norm_mig_pages_transferred();
231 info->ram->normal_bytes = norm_mig_bytes_transferred();
232 info->ram->dirty_pages_rate = s->dirty_pages_rate;
233 info->ram->mbps = s->mbps;
234 info->ram->dirty_sync_count = s->dirty_sync_count;
236 if (blk_mig_active()) {
237 info->has_disk = true;
238 info->disk = g_malloc0(sizeof(*info->disk));
239 info->disk->transferred = blk_mig_bytes_transferred();
240 info->disk->remaining = blk_mig_bytes_remaining();
241 info->disk->total = blk_mig_bytes_total();
244 get_xbzrle_cache_stats(info);
245 break;
246 case MIG_STATE_COMPLETED:
247 get_xbzrle_cache_stats(info);
249 info->has_status = true;
250 info->status = g_strdup("completed");
251 info->has_total_time = true;
252 info->total_time = s->total_time;
253 info->has_downtime = true;
254 info->downtime = s->downtime;
255 info->has_setup_time = true;
256 info->setup_time = s->setup_time;
258 info->has_ram = true;
259 info->ram = g_malloc0(sizeof(*info->ram));
260 info->ram->transferred = ram_bytes_transferred();
261 info->ram->remaining = 0;
262 info->ram->total = ram_bytes_total();
263 info->ram->duplicate = dup_mig_pages_transferred();
264 info->ram->skipped = skipped_mig_pages_transferred();
265 info->ram->normal = norm_mig_pages_transferred();
266 info->ram->normal_bytes = norm_mig_bytes_transferred();
267 info->ram->mbps = s->mbps;
268 info->ram->dirty_sync_count = s->dirty_sync_count;
269 break;
270 case MIG_STATE_ERROR:
271 info->has_status = true;
272 info->status = g_strdup("failed");
273 break;
274 case MIG_STATE_CANCELLED:
275 info->has_status = true;
276 info->status = g_strdup("cancelled");
277 break;
280 return info;
283 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
284 Error **errp)
286 MigrationState *s = migrate_get_current();
287 MigrationCapabilityStatusList *cap;
289 if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP) {
290 error_set(errp, QERR_MIGRATION_ACTIVE);
291 return;
294 for (cap = params; cap; cap = cap->next) {
295 s->enabled_capabilities[cap->value->capability] = cap->value->state;
299 /* shared migration helpers */
301 static void migrate_set_state(MigrationState *s, int old_state, int new_state)
303 if (atomic_cmpxchg(&s->state, old_state, new_state) == new_state) {
304 trace_migrate_set_state(new_state);
308 static void migrate_fd_cleanup(void *opaque)
310 MigrationState *s = opaque;
312 qemu_bh_delete(s->cleanup_bh);
313 s->cleanup_bh = NULL;
315 if (s->file) {
316 trace_migrate_fd_cleanup();
317 qemu_mutex_unlock_iothread();
318 qemu_thread_join(&s->thread);
319 qemu_mutex_lock_iothread();
321 qemu_fclose(s->file);
322 s->file = NULL;
325 assert(s->state != MIG_STATE_ACTIVE);
327 if (s->state != MIG_STATE_COMPLETED) {
328 qemu_savevm_state_cancel();
329 if (s->state == MIG_STATE_CANCELLING) {
330 migrate_set_state(s, MIG_STATE_CANCELLING, MIG_STATE_CANCELLED);
334 notifier_list_notify(&migration_state_notifiers, s);
337 void migrate_fd_error(MigrationState *s)
339 trace_migrate_fd_error();
340 assert(s->file == NULL);
341 s->state = MIG_STATE_ERROR;
342 trace_migrate_set_state(MIG_STATE_ERROR);
343 notifier_list_notify(&migration_state_notifiers, s);
346 static void migrate_fd_cancel(MigrationState *s)
348 int old_state ;
349 QEMUFile *f = migrate_get_current()->file;
350 trace_migrate_fd_cancel();
352 do {
353 old_state = s->state;
354 if (old_state != MIG_STATE_SETUP && old_state != MIG_STATE_ACTIVE) {
355 break;
357 migrate_set_state(s, old_state, MIG_STATE_CANCELLING);
358 } while (s->state != MIG_STATE_CANCELLING);
361 * If we're unlucky the migration code might be stuck somewhere in a
362 * send/write while the network has failed and is waiting to timeout;
363 * if we've got shutdown(2) available then we can force it to quit.
364 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
365 * called in a bh, so there is no race against this cancel.
367 if (s->state == MIG_STATE_CANCELLING && f) {
368 qemu_file_shutdown(f);
372 void add_migration_state_change_notifier(Notifier *notify)
374 notifier_list_add(&migration_state_notifiers, notify);
377 void remove_migration_state_change_notifier(Notifier *notify)
379 notifier_remove(notify);
382 bool migration_in_setup(MigrationState *s)
384 return s->state == MIG_STATE_SETUP;
387 bool migration_has_finished(MigrationState *s)
389 return s->state == MIG_STATE_COMPLETED;
392 bool migration_has_failed(MigrationState *s)
394 return (s->state == MIG_STATE_CANCELLED ||
395 s->state == MIG_STATE_ERROR);
398 static MigrationState *migrate_init(const MigrationParams *params)
400 MigrationState *s = migrate_get_current();
401 int64_t bandwidth_limit = s->bandwidth_limit;
402 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
403 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
405 memcpy(enabled_capabilities, s->enabled_capabilities,
406 sizeof(enabled_capabilities));
408 memset(s, 0, sizeof(*s));
409 s->params = *params;
410 memcpy(s->enabled_capabilities, enabled_capabilities,
411 sizeof(enabled_capabilities));
412 s->xbzrle_cache_size = xbzrle_cache_size;
414 s->bandwidth_limit = bandwidth_limit;
415 s->state = MIG_STATE_SETUP;
416 trace_migrate_set_state(MIG_STATE_SETUP);
418 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
419 return s;
422 static GSList *migration_blockers;
424 void migrate_add_blocker(Error *reason)
426 migration_blockers = g_slist_prepend(migration_blockers, reason);
429 void migrate_del_blocker(Error *reason)
431 migration_blockers = g_slist_remove(migration_blockers, reason);
434 void qmp_migrate_incoming(const char *uri, Error **errp)
436 Error *local_err = NULL;
437 static bool once = true;
439 if (!deferred_incoming) {
440 error_setg(errp, "For use with '-incoming defer'");
441 return;
443 if (!once) {
444 error_setg(errp, "The incoming migration has already been started");
447 qemu_start_incoming_migration(uri, &local_err);
449 if (local_err) {
450 error_propagate(errp, local_err);
451 return;
454 once = false;
457 void qmp_migrate(const char *uri, bool has_blk, bool blk,
458 bool has_inc, bool inc, bool has_detach, bool detach,
459 Error **errp)
461 Error *local_err = NULL;
462 MigrationState *s = migrate_get_current();
463 MigrationParams params;
464 const char *p;
466 params.blk = has_blk && blk;
467 params.shared = has_inc && inc;
469 if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP ||
470 s->state == MIG_STATE_CANCELLING) {
471 error_set(errp, QERR_MIGRATION_ACTIVE);
472 return;
475 if (runstate_check(RUN_STATE_INMIGRATE)) {
476 error_setg(errp, "Guest is waiting for an incoming migration");
477 return;
480 if (qemu_savevm_state_blocked(errp)) {
481 return;
484 if (migration_blockers) {
485 *errp = error_copy(migration_blockers->data);
486 return;
489 s = migrate_init(&params);
491 if (strstart(uri, "tcp:", &p)) {
492 tcp_start_outgoing_migration(s, p, &local_err);
493 #ifdef CONFIG_RDMA
494 } else if (strstart(uri, "rdma:", &p)) {
495 rdma_start_outgoing_migration(s, p, &local_err);
496 #endif
497 #if !defined(WIN32)
498 } else if (strstart(uri, "exec:", &p)) {
499 exec_start_outgoing_migration(s, p, &local_err);
500 } else if (strstart(uri, "unix:", &p)) {
501 unix_start_outgoing_migration(s, p, &local_err);
502 } else if (strstart(uri, "fd:", &p)) {
503 fd_start_outgoing_migration(s, p, &local_err);
504 #endif
505 } else {
506 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
507 s->state = MIG_STATE_ERROR;
508 return;
511 if (local_err) {
512 migrate_fd_error(s);
513 error_propagate(errp, local_err);
514 return;
518 void qmp_migrate_cancel(Error **errp)
520 migrate_fd_cancel(migrate_get_current());
523 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
525 MigrationState *s = migrate_get_current();
526 int64_t new_size;
528 /* Check for truncation */
529 if (value != (size_t)value) {
530 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
531 "exceeding address space");
532 return;
535 /* Cache should not be larger than guest ram size */
536 if (value > ram_bytes_total()) {
537 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
538 "exceeds guest ram size ");
539 return;
542 new_size = xbzrle_cache_resize(value);
543 if (new_size < 0) {
544 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
545 "is smaller than page size");
546 return;
549 s->xbzrle_cache_size = new_size;
552 int64_t qmp_query_migrate_cache_size(Error **errp)
554 return migrate_xbzrle_cache_size();
557 void qmp_migrate_set_speed(int64_t value, Error **errp)
559 MigrationState *s;
561 if (value < 0) {
562 value = 0;
564 if (value > SIZE_MAX) {
565 value = SIZE_MAX;
568 s = migrate_get_current();
569 s->bandwidth_limit = value;
570 if (s->file) {
571 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
575 void qmp_migrate_set_downtime(double value, Error **errp)
577 value *= 1e9;
578 value = MAX(0, MIN(UINT64_MAX, value));
579 max_downtime = (uint64_t)value;
582 bool migrate_rdma_pin_all(void)
584 MigrationState *s;
586 s = migrate_get_current();
588 return s->enabled_capabilities[MIGRATION_CAPABILITY_RDMA_PIN_ALL];
591 bool migrate_auto_converge(void)
593 MigrationState *s;
595 s = migrate_get_current();
597 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
600 bool migrate_zero_blocks(void)
602 MigrationState *s;
604 s = migrate_get_current();
606 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
609 int migrate_use_xbzrle(void)
611 MigrationState *s;
613 s = migrate_get_current();
615 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
618 int64_t migrate_xbzrle_cache_size(void)
620 MigrationState *s;
622 s = migrate_get_current();
624 return s->xbzrle_cache_size;
627 /* migration thread support */
629 static void *migration_thread(void *opaque)
631 MigrationState *s = opaque;
632 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
633 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
634 int64_t initial_bytes = 0;
635 int64_t max_size = 0;
636 int64_t start_time = initial_time;
637 bool old_vm_running = false;
639 qemu_savevm_state_begin(s->file, &s->params);
641 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
642 migrate_set_state(s, MIG_STATE_SETUP, MIG_STATE_ACTIVE);
644 while (s->state == MIG_STATE_ACTIVE) {
645 int64_t current_time;
646 uint64_t pending_size;
648 if (!qemu_file_rate_limit(s->file)) {
649 pending_size = qemu_savevm_state_pending(s->file, max_size);
650 trace_migrate_pending(pending_size, max_size);
651 if (pending_size && pending_size >= max_size) {
652 qemu_savevm_state_iterate(s->file);
653 } else {
654 int ret;
656 qemu_mutex_lock_iothread();
657 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
658 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
659 old_vm_running = runstate_is_running();
661 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
662 if (ret >= 0) {
663 qemu_file_set_rate_limit(s->file, INT64_MAX);
664 qemu_savevm_state_complete(s->file);
666 qemu_mutex_unlock_iothread();
668 if (ret < 0) {
669 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR);
670 break;
673 if (!qemu_file_get_error(s->file)) {
674 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_COMPLETED);
675 break;
680 if (qemu_file_get_error(s->file)) {
681 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR);
682 break;
684 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
685 if (current_time >= initial_time + BUFFER_DELAY) {
686 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
687 uint64_t time_spent = current_time - initial_time;
688 double bandwidth = transferred_bytes / time_spent;
689 max_size = bandwidth * migrate_max_downtime() / 1000000;
691 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
692 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
694 trace_migrate_transferred(transferred_bytes, time_spent,
695 bandwidth, max_size);
696 /* if we haven't sent anything, we don't want to recalculate
697 10000 is a small enough number for our purposes */
698 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
699 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
702 qemu_file_reset_rate_limit(s->file);
703 initial_time = current_time;
704 initial_bytes = qemu_ftell(s->file);
706 if (qemu_file_rate_limit(s->file)) {
707 /* usleep expects microseconds */
708 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
712 qemu_mutex_lock_iothread();
713 if (s->state == MIG_STATE_COMPLETED) {
714 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
715 uint64_t transferred_bytes = qemu_ftell(s->file);
716 s->total_time = end_time - s->total_time;
717 s->downtime = end_time - start_time;
718 if (s->total_time) {
719 s->mbps = (((double) transferred_bytes * 8.0) /
720 ((double) s->total_time)) / 1000;
722 runstate_set(RUN_STATE_POSTMIGRATE);
723 } else {
724 if (old_vm_running) {
725 vm_start();
728 qemu_bh_schedule(s->cleanup_bh);
729 qemu_mutex_unlock_iothread();
731 return NULL;
734 void migrate_fd_connect(MigrationState *s)
736 s->state = MIG_STATE_SETUP;
737 trace_migrate_set_state(MIG_STATE_SETUP);
739 /* This is a best 1st approximation. ns to ms */
740 s->expected_downtime = max_downtime/1000000;
741 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
743 qemu_file_set_rate_limit(s->file,
744 s->bandwidth_limit / XFER_LIMIT_RATIO);
746 /* Notify before starting migration thread */
747 notifier_list_notify(&migration_state_notifiers, s);
749 qemu_thread_create(&s->thread, "migration", migration_thread, s,
750 QEMU_THREAD_JOINABLE);