sclp: Fix uninitialized var in handle_write_event_buf().
[qemu.git] / migration.c
blob73ce170ddf4ee742eaba092074617a3523c90cc5
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 "migration.h"
18 #include "monitor.h"
19 #include "buffered_file.h"
20 #include "sysemu.h"
21 #include "block.h"
22 #include "qemu_socket.h"
23 #include "block-migration.h"
24 #include "qmp-commands.h"
26 //#define DEBUG_MIGRATION
28 #ifdef DEBUG_MIGRATION
29 #define DPRINTF(fmt, ...) \
30 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
31 #else
32 #define DPRINTF(fmt, ...) \
33 do { } while (0)
34 #endif
36 enum {
37 MIG_STATE_ERROR,
38 MIG_STATE_SETUP,
39 MIG_STATE_CANCELLED,
40 MIG_STATE_ACTIVE,
41 MIG_STATE_COMPLETED,
44 #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
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 /* When we add fault tolerance, we could have several
53 migrations at once. For now we don't need to add
54 dynamic creation of migration */
56 MigrationState *migrate_get_current(void)
58 static MigrationState current_migration = {
59 .state = MIG_STATE_SETUP,
60 .bandwidth_limit = MAX_THROTTLE,
61 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
64 return &current_migration;
67 void qemu_start_incoming_migration(const char *uri, Error **errp)
69 const char *p;
71 if (strstart(uri, "tcp:", &p))
72 tcp_start_incoming_migration(p, errp);
73 #if !defined(WIN32)
74 else if (strstart(uri, "exec:", &p))
75 exec_start_incoming_migration(p, errp);
76 else if (strstart(uri, "unix:", &p))
77 unix_start_incoming_migration(p, errp);
78 else if (strstart(uri, "fd:", &p))
79 fd_start_incoming_migration(p, errp);
80 #endif
81 else {
82 error_setg(errp, "unknown migration protocol: %s\n", uri);
86 static void process_incoming_migration_co(void *opaque)
88 QEMUFile *f = opaque;
89 int ret;
91 ret = qemu_loadvm_state(f);
92 qemu_set_fd_handler(qemu_get_fd(f), NULL, NULL, NULL);
93 qemu_fclose(f);
94 if (ret < 0) {
95 fprintf(stderr, "load of migration failed\n");
96 exit(0);
98 qemu_announce_self();
99 DPRINTF("successfully loaded vm state\n");
101 bdrv_clear_incoming_migration_all();
102 /* Make sure all file formats flush their mutable metadata */
103 bdrv_invalidate_cache_all();
105 if (autostart) {
106 vm_start();
107 } else {
108 runstate_set(RUN_STATE_PAUSED);
112 static void enter_migration_coroutine(void *opaque)
114 Coroutine *co = opaque;
115 qemu_coroutine_enter(co, NULL);
118 void process_incoming_migration(QEMUFile *f)
120 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
121 int fd = qemu_get_fd(f);
123 assert(fd != -1);
124 socket_set_nonblock(fd);
125 qemu_set_fd_handler(fd, enter_migration_coroutine, NULL, co);
126 qemu_coroutine_enter(co, f);
129 /* amount of nanoseconds we are willing to wait for migration to be down.
130 * the choice of nanoseconds is because it is the maximum resolution that
131 * get_clock() can achieve. It is an internal measure. All user-visible
132 * units must be in seconds */
133 static uint64_t max_downtime = 30000000;
135 uint64_t migrate_max_downtime(void)
137 return max_downtime;
140 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
142 MigrationCapabilityStatusList *head = NULL;
143 MigrationCapabilityStatusList *caps;
144 MigrationState *s = migrate_get_current();
145 int i;
147 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
148 if (head == NULL) {
149 head = g_malloc0(sizeof(*caps));
150 caps = head;
151 } else {
152 caps->next = g_malloc0(sizeof(*caps));
153 caps = caps->next;
155 caps->value =
156 g_malloc(sizeof(*caps->value));
157 caps->value->capability = i;
158 caps->value->state = s->enabled_capabilities[i];
161 return head;
164 static void get_xbzrle_cache_stats(MigrationInfo *info)
166 if (migrate_use_xbzrle()) {
167 info->has_xbzrle_cache = true;
168 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
169 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
170 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
171 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
172 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
173 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
177 MigrationInfo *qmp_query_migrate(Error **errp)
179 MigrationInfo *info = g_malloc0(sizeof(*info));
180 MigrationState *s = migrate_get_current();
182 switch (s->state) {
183 case MIG_STATE_SETUP:
184 /* no migration has happened ever */
185 break;
186 case MIG_STATE_ACTIVE:
187 info->has_status = true;
188 info->status = g_strdup("active");
189 info->has_total_time = true;
190 info->total_time = qemu_get_clock_ms(rt_clock)
191 - s->total_time;
192 info->has_expected_downtime = true;
193 info->expected_downtime = s->expected_downtime;
195 info->has_ram = true;
196 info->ram = g_malloc0(sizeof(*info->ram));
197 info->ram->transferred = ram_bytes_transferred();
198 info->ram->remaining = ram_bytes_remaining();
199 info->ram->total = ram_bytes_total();
200 info->ram->duplicate = dup_mig_pages_transferred();
201 info->ram->normal = norm_mig_pages_transferred();
202 info->ram->normal_bytes = norm_mig_bytes_transferred();
203 info->ram->dirty_pages_rate = s->dirty_pages_rate;
206 if (blk_mig_active()) {
207 info->has_disk = true;
208 info->disk = g_malloc0(sizeof(*info->disk));
209 info->disk->transferred = blk_mig_bytes_transferred();
210 info->disk->remaining = blk_mig_bytes_remaining();
211 info->disk->total = blk_mig_bytes_total();
214 get_xbzrle_cache_stats(info);
215 break;
216 case MIG_STATE_COMPLETED:
217 get_xbzrle_cache_stats(info);
219 info->has_status = true;
220 info->status = g_strdup("completed");
221 info->total_time = s->total_time;
222 info->has_downtime = true;
223 info->downtime = s->downtime;
225 info->has_ram = true;
226 info->ram = g_malloc0(sizeof(*info->ram));
227 info->ram->transferred = ram_bytes_transferred();
228 info->ram->remaining = 0;
229 info->ram->total = ram_bytes_total();
230 info->ram->duplicate = dup_mig_pages_transferred();
231 info->ram->normal = norm_mig_pages_transferred();
232 info->ram->normal_bytes = norm_mig_bytes_transferred();
233 break;
234 case MIG_STATE_ERROR:
235 info->has_status = true;
236 info->status = g_strdup("failed");
237 break;
238 case MIG_STATE_CANCELLED:
239 info->has_status = true;
240 info->status = g_strdup("cancelled");
241 break;
244 return info;
247 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
248 Error **errp)
250 MigrationState *s = migrate_get_current();
251 MigrationCapabilityStatusList *cap;
253 if (s->state == MIG_STATE_ACTIVE) {
254 error_set(errp, QERR_MIGRATION_ACTIVE);
255 return;
258 for (cap = params; cap; cap = cap->next) {
259 s->enabled_capabilities[cap->value->capability] = cap->value->state;
263 /* shared migration helpers */
265 static int migrate_fd_cleanup(MigrationState *s)
267 int ret = 0;
269 if (s->file) {
270 DPRINTF("closing file\n");
271 ret = qemu_fclose(s->file);
272 s->file = NULL;
275 migrate_fd_close(s);
276 return ret;
279 void migrate_fd_error(MigrationState *s)
281 DPRINTF("setting error state\n");
282 s->state = MIG_STATE_ERROR;
283 notifier_list_notify(&migration_state_notifiers, s);
284 migrate_fd_cleanup(s);
287 static void migrate_fd_completed(MigrationState *s)
289 DPRINTF("setting completed state\n");
290 if (migrate_fd_cleanup(s) < 0) {
291 s->state = MIG_STATE_ERROR;
292 } else {
293 s->state = MIG_STATE_COMPLETED;
294 runstate_set(RUN_STATE_POSTMIGRATE);
296 notifier_list_notify(&migration_state_notifiers, s);
299 static void migrate_fd_put_notify(void *opaque)
301 MigrationState *s = opaque;
302 int ret;
304 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
305 ret = qemu_file_put_notify(s->file);
306 if (ret) {
307 migrate_fd_error(s);
311 ssize_t migrate_fd_put_buffer(MigrationState *s, const void *data,
312 size_t size)
314 ssize_t ret;
316 if (s->state != MIG_STATE_ACTIVE) {
317 return -EIO;
320 do {
321 ret = s->write(s, data, size);
322 } while (ret == -1 && ((s->get_error(s)) == EINTR));
324 if (ret == -1)
325 ret = -(s->get_error(s));
327 if (ret == -EAGAIN) {
328 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
331 return ret;
334 void migrate_fd_put_ready(MigrationState *s)
336 int ret;
338 if (s->state != MIG_STATE_ACTIVE) {
339 DPRINTF("put_ready returning because of non-active state\n");
340 return;
343 DPRINTF("iterate\n");
344 ret = qemu_savevm_state_iterate(s->file);
345 if (ret < 0) {
346 migrate_fd_error(s);
347 } else if (ret == 1) {
348 int old_vm_running = runstate_is_running();
349 int64_t start_time, end_time;
351 DPRINTF("done iterating\n");
352 start_time = qemu_get_clock_ms(rt_clock);
353 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
354 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
356 if (qemu_savevm_state_complete(s->file) < 0) {
357 migrate_fd_error(s);
358 } else {
359 migrate_fd_completed(s);
361 end_time = qemu_get_clock_ms(rt_clock);
362 s->total_time = end_time - s->total_time;
363 s->downtime = end_time - start_time;
364 if (s->state != MIG_STATE_COMPLETED) {
365 if (old_vm_running) {
366 vm_start();
372 static void migrate_fd_cancel(MigrationState *s)
374 if (s->state != MIG_STATE_ACTIVE)
375 return;
377 DPRINTF("cancelling migration\n");
379 s->state = MIG_STATE_CANCELLED;
380 notifier_list_notify(&migration_state_notifiers, s);
381 qemu_savevm_state_cancel(s->file);
383 migrate_fd_cleanup(s);
386 int migrate_fd_wait_for_unfreeze(MigrationState *s)
388 int ret;
390 DPRINTF("wait for unfreeze\n");
391 if (s->state != MIG_STATE_ACTIVE)
392 return -EINVAL;
394 do {
395 fd_set wfds;
397 FD_ZERO(&wfds);
398 FD_SET(s->fd, &wfds);
400 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
401 } while (ret == -1 && (s->get_error(s)) == EINTR);
403 if (ret == -1) {
404 return -s->get_error(s);
406 return 0;
409 int migrate_fd_close(MigrationState *s)
411 int rc = 0;
412 if (s->fd != -1) {
413 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
414 rc = s->close(s);
415 s->fd = -1;
417 return rc;
420 void add_migration_state_change_notifier(Notifier *notify)
422 notifier_list_add(&migration_state_notifiers, notify);
425 void remove_migration_state_change_notifier(Notifier *notify)
427 notifier_remove(notify);
430 bool migration_is_active(MigrationState *s)
432 return s->state == MIG_STATE_ACTIVE;
435 bool migration_has_finished(MigrationState *s)
437 return s->state == MIG_STATE_COMPLETED;
440 bool migration_has_failed(MigrationState *s)
442 return (s->state == MIG_STATE_CANCELLED ||
443 s->state == MIG_STATE_ERROR);
446 void migrate_fd_connect(MigrationState *s)
448 int ret;
450 s->state = MIG_STATE_ACTIVE;
451 s->file = qemu_fopen_ops_buffered(s);
453 DPRINTF("beginning savevm\n");
454 ret = qemu_savevm_state_begin(s->file, &s->params);
455 if (ret < 0) {
456 DPRINTF("failed, %d\n", ret);
457 migrate_fd_error(s);
458 return;
460 migrate_fd_put_ready(s);
463 static MigrationState *migrate_init(const MigrationParams *params)
465 MigrationState *s = migrate_get_current();
466 int64_t bandwidth_limit = s->bandwidth_limit;
467 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
468 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
470 memcpy(enabled_capabilities, s->enabled_capabilities,
471 sizeof(enabled_capabilities));
473 memset(s, 0, sizeof(*s));
474 s->bandwidth_limit = bandwidth_limit;
475 s->params = *params;
476 memcpy(s->enabled_capabilities, enabled_capabilities,
477 sizeof(enabled_capabilities));
478 s->xbzrle_cache_size = xbzrle_cache_size;
480 s->bandwidth_limit = bandwidth_limit;
481 s->state = MIG_STATE_SETUP;
482 s->total_time = qemu_get_clock_ms(rt_clock);
484 return s;
487 static GSList *migration_blockers;
489 void migrate_add_blocker(Error *reason)
491 migration_blockers = g_slist_prepend(migration_blockers, reason);
494 void migrate_del_blocker(Error *reason)
496 migration_blockers = g_slist_remove(migration_blockers, reason);
499 void qmp_migrate(const char *uri, bool has_blk, bool blk,
500 bool has_inc, bool inc, bool has_detach, bool detach,
501 Error **errp)
503 Error *local_err = NULL;
504 MigrationState *s = migrate_get_current();
505 MigrationParams params;
506 const char *p;
508 params.blk = blk;
509 params.shared = inc;
511 if (s->state == MIG_STATE_ACTIVE) {
512 error_set(errp, QERR_MIGRATION_ACTIVE);
513 return;
516 if (qemu_savevm_state_blocked(errp)) {
517 return;
520 if (migration_blockers) {
521 *errp = error_copy(migration_blockers->data);
522 return;
525 s = migrate_init(&params);
527 if (strstart(uri, "tcp:", &p)) {
528 tcp_start_outgoing_migration(s, p, &local_err);
529 #if !defined(WIN32)
530 } else if (strstart(uri, "exec:", &p)) {
531 exec_start_outgoing_migration(s, p, &local_err);
532 } else if (strstart(uri, "unix:", &p)) {
533 unix_start_outgoing_migration(s, p, &local_err);
534 } else if (strstart(uri, "fd:", &p)) {
535 fd_start_outgoing_migration(s, p, &local_err);
536 #endif
537 } else {
538 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
539 return;
542 if (local_err) {
543 migrate_fd_error(s);
544 error_propagate(errp, local_err);
545 return;
548 notifier_list_notify(&migration_state_notifiers, s);
551 void qmp_migrate_cancel(Error **errp)
553 migrate_fd_cancel(migrate_get_current());
556 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
558 MigrationState *s = migrate_get_current();
560 /* Check for truncation */
561 if (value != (size_t)value) {
562 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
563 "exceeding address space");
564 return;
567 s->xbzrle_cache_size = xbzrle_cache_resize(value);
570 int64_t qmp_query_migrate_cache_size(Error **errp)
572 return migrate_xbzrle_cache_size();
575 void qmp_migrate_set_speed(int64_t value, Error **errp)
577 MigrationState *s;
579 if (value < 0) {
580 value = 0;
583 s = migrate_get_current();
584 s->bandwidth_limit = value;
585 qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
588 void qmp_migrate_set_downtime(double value, Error **errp)
590 value *= 1e9;
591 value = MAX(0, MIN(UINT64_MAX, value));
592 max_downtime = (uint64_t)value;
595 int migrate_use_xbzrle(void)
597 MigrationState *s;
599 s = migrate_get_current();
601 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
604 int64_t migrate_xbzrle_cache_size(void)
606 MigrationState *s;
608 s = migrate_get_current();
610 return s->xbzrle_cache_size;