Remove unused CONFIG_TCG_PASS_AREG0 and dead code
[qemu/opensuse.git] / migration.c
blob1edeec5ddd151f6e44ffde76743cdb5a4d2aff0a
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 static 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 int qemu_start_incoming_migration(const char *uri, Error **errp)
69 const char *p;
70 int ret;
72 if (strstart(uri, "tcp:", &p))
73 ret = tcp_start_incoming_migration(p, errp);
74 #if !defined(WIN32)
75 else if (strstart(uri, "exec:", &p))
76 ret = exec_start_incoming_migration(p);
77 else if (strstart(uri, "unix:", &p))
78 ret = unix_start_incoming_migration(p);
79 else if (strstart(uri, "fd:", &p))
80 ret = fd_start_incoming_migration(p);
81 #endif
82 else {
83 fprintf(stderr, "unknown migration protocol: %s\n", uri);
84 ret = -EPROTONOSUPPORT;
86 return ret;
89 void process_incoming_migration(QEMUFile *f)
91 if (qemu_loadvm_state(f) < 0) {
92 fprintf(stderr, "load of migration failed\n");
93 exit(0);
95 qemu_announce_self();
96 DPRINTF("successfully loaded vm state\n");
98 bdrv_clear_incoming_migration_all();
99 /* Make sure all file formats flush their mutable metadata */
100 bdrv_invalidate_cache_all();
102 if (autostart) {
103 vm_start();
104 } else {
105 runstate_set(RUN_STATE_PRELAUNCH);
109 /* amount of nanoseconds we are willing to wait for migration to be down.
110 * the choice of nanoseconds is because it is the maximum resolution that
111 * get_clock() can achieve. It is an internal measure. All user-visible
112 * units must be in seconds */
113 static uint64_t max_downtime = 30000000;
115 uint64_t migrate_max_downtime(void)
117 return max_downtime;
120 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
122 MigrationCapabilityStatusList *head = NULL;
123 MigrationCapabilityStatusList *caps;
124 MigrationState *s = migrate_get_current();
125 int i;
127 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
128 if (head == NULL) {
129 head = g_malloc0(sizeof(*caps));
130 caps = head;
131 } else {
132 caps->next = g_malloc0(sizeof(*caps));
133 caps = caps->next;
135 caps->value =
136 g_malloc(sizeof(*caps->value));
137 caps->value->capability = i;
138 caps->value->state = s->enabled_capabilities[i];
141 return head;
144 static void get_xbzrle_cache_stats(MigrationInfo *info)
146 if (migrate_use_xbzrle()) {
147 info->has_xbzrle_cache = true;
148 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
149 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
150 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
151 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
152 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
153 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
157 MigrationInfo *qmp_query_migrate(Error **errp)
159 MigrationInfo *info = g_malloc0(sizeof(*info));
160 MigrationState *s = migrate_get_current();
162 switch (s->state) {
163 case MIG_STATE_SETUP:
164 /* no migration has happened ever */
165 break;
166 case MIG_STATE_ACTIVE:
167 info->has_status = true;
168 info->status = g_strdup("active");
169 info->has_total_time = true;
170 info->total_time = qemu_get_clock_ms(rt_clock)
171 - s->total_time;
173 info->has_ram = true;
174 info->ram = g_malloc0(sizeof(*info->ram));
175 info->ram->transferred = ram_bytes_transferred();
176 info->ram->remaining = ram_bytes_remaining();
177 info->ram->total = ram_bytes_total();
178 info->ram->duplicate = dup_mig_pages_transferred();
179 info->ram->normal = norm_mig_pages_transferred();
180 info->ram->normal_bytes = norm_mig_bytes_transferred();
182 if (blk_mig_active()) {
183 info->has_disk = true;
184 info->disk = g_malloc0(sizeof(*info->disk));
185 info->disk->transferred = blk_mig_bytes_transferred();
186 info->disk->remaining = blk_mig_bytes_remaining();
187 info->disk->total = blk_mig_bytes_total();
190 get_xbzrle_cache_stats(info);
191 break;
192 case MIG_STATE_COMPLETED:
193 get_xbzrle_cache_stats(info);
195 info->has_status = true;
196 info->status = g_strdup("completed");
197 info->total_time = s->total_time;
199 info->has_ram = true;
200 info->ram = g_malloc0(sizeof(*info->ram));
201 info->ram->transferred = ram_bytes_transferred();
202 info->ram->remaining = 0;
203 info->ram->total = ram_bytes_total();
204 info->ram->duplicate = dup_mig_pages_transferred();
205 info->ram->normal = norm_mig_pages_transferred();
206 info->ram->normal_bytes = norm_mig_bytes_transferred();
207 break;
208 case MIG_STATE_ERROR:
209 info->has_status = true;
210 info->status = g_strdup("failed");
211 break;
212 case MIG_STATE_CANCELLED:
213 info->has_status = true;
214 info->status = g_strdup("cancelled");
215 break;
218 return info;
221 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
222 Error **errp)
224 MigrationState *s = migrate_get_current();
225 MigrationCapabilityStatusList *cap;
227 if (s->state == MIG_STATE_ACTIVE) {
228 error_set(errp, QERR_MIGRATION_ACTIVE);
229 return;
232 for (cap = params; cap; cap = cap->next) {
233 s->enabled_capabilities[cap->value->capability] = cap->value->state;
237 /* shared migration helpers */
239 static int migrate_fd_cleanup(MigrationState *s)
241 int ret = 0;
243 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
245 if (s->file) {
246 DPRINTF("closing file\n");
247 ret = qemu_fclose(s->file);
248 s->file = NULL;
251 if (s->fd != -1) {
252 close(s->fd);
253 s->fd = -1;
256 return ret;
259 void migrate_fd_error(MigrationState *s)
261 DPRINTF("setting error state\n");
262 s->state = MIG_STATE_ERROR;
263 notifier_list_notify(&migration_state_notifiers, s);
264 migrate_fd_cleanup(s);
267 static void migrate_fd_completed(MigrationState *s)
269 DPRINTF("setting completed state\n");
270 if (migrate_fd_cleanup(s) < 0) {
271 s->state = MIG_STATE_ERROR;
272 } else {
273 s->state = MIG_STATE_COMPLETED;
274 runstate_set(RUN_STATE_POSTMIGRATE);
276 notifier_list_notify(&migration_state_notifiers, s);
279 static void migrate_fd_put_notify(void *opaque)
281 MigrationState *s = opaque;
283 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
284 qemu_file_put_notify(s->file);
285 if (s->file && qemu_file_get_error(s->file)) {
286 migrate_fd_error(s);
290 static ssize_t migrate_fd_put_buffer(void *opaque, const void *data,
291 size_t size)
293 MigrationState *s = opaque;
294 ssize_t ret;
296 if (s->state != MIG_STATE_ACTIVE) {
297 return -EIO;
300 do {
301 ret = s->write(s, data, size);
302 } while (ret == -1 && ((s->get_error(s)) == EINTR));
304 if (ret == -1)
305 ret = -(s->get_error(s));
307 if (ret == -EAGAIN) {
308 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
311 return ret;
314 static void migrate_fd_put_ready(void *opaque)
316 MigrationState *s = opaque;
317 int ret;
319 if (s->state != MIG_STATE_ACTIVE) {
320 DPRINTF("put_ready returning because of non-active state\n");
321 return;
324 DPRINTF("iterate\n");
325 ret = qemu_savevm_state_iterate(s->file);
326 if (ret < 0) {
327 migrate_fd_error(s);
328 } else if (ret == 1) {
329 int old_vm_running = runstate_is_running();
331 DPRINTF("done iterating\n");
332 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
333 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
335 if (qemu_savevm_state_complete(s->file) < 0) {
336 migrate_fd_error(s);
337 } else {
338 migrate_fd_completed(s);
340 s->total_time = qemu_get_clock_ms(rt_clock) - s->total_time;
341 if (s->state != MIG_STATE_COMPLETED) {
342 if (old_vm_running) {
343 vm_start();
349 static void migrate_fd_cancel(MigrationState *s)
351 if (s->state != MIG_STATE_ACTIVE)
352 return;
354 DPRINTF("cancelling migration\n");
356 s->state = MIG_STATE_CANCELLED;
357 notifier_list_notify(&migration_state_notifiers, s);
358 qemu_savevm_state_cancel(s->file);
360 migrate_fd_cleanup(s);
363 static void migrate_fd_wait_for_unfreeze(void *opaque)
365 MigrationState *s = opaque;
366 int ret;
368 DPRINTF("wait for unfreeze\n");
369 if (s->state != MIG_STATE_ACTIVE)
370 return;
372 do {
373 fd_set wfds;
375 FD_ZERO(&wfds);
376 FD_SET(s->fd, &wfds);
378 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
379 } while (ret == -1 && (s->get_error(s)) == EINTR);
381 if (ret == -1) {
382 qemu_file_set_error(s->file, -s->get_error(s));
386 static int migrate_fd_close(void *opaque)
388 MigrationState *s = opaque;
390 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
391 return s->close(s);
394 void add_migration_state_change_notifier(Notifier *notify)
396 notifier_list_add(&migration_state_notifiers, notify);
399 void remove_migration_state_change_notifier(Notifier *notify)
401 notifier_remove(notify);
404 bool migration_is_active(MigrationState *s)
406 return s->state == MIG_STATE_ACTIVE;
409 bool migration_has_finished(MigrationState *s)
411 return s->state == MIG_STATE_COMPLETED;
414 bool migration_has_failed(MigrationState *s)
416 return (s->state == MIG_STATE_CANCELLED ||
417 s->state == MIG_STATE_ERROR);
420 void migrate_fd_connect(MigrationState *s)
422 int ret;
424 s->state = MIG_STATE_ACTIVE;
425 s->file = qemu_fopen_ops_buffered(s,
426 s->bandwidth_limit,
427 migrate_fd_put_buffer,
428 migrate_fd_put_ready,
429 migrate_fd_wait_for_unfreeze,
430 migrate_fd_close);
432 DPRINTF("beginning savevm\n");
433 ret = qemu_savevm_state_begin(s->file, &s->params);
434 if (ret < 0) {
435 DPRINTF("failed, %d\n", ret);
436 migrate_fd_error(s);
437 return;
439 migrate_fd_put_ready(s);
442 static MigrationState *migrate_init(const MigrationParams *params)
444 MigrationState *s = migrate_get_current();
445 int64_t bandwidth_limit = s->bandwidth_limit;
446 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
447 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
449 memcpy(enabled_capabilities, s->enabled_capabilities,
450 sizeof(enabled_capabilities));
452 memset(s, 0, sizeof(*s));
453 s->bandwidth_limit = bandwidth_limit;
454 s->params = *params;
455 memcpy(s->enabled_capabilities, enabled_capabilities,
456 sizeof(enabled_capabilities));
457 s->xbzrle_cache_size = xbzrle_cache_size;
459 s->bandwidth_limit = bandwidth_limit;
460 s->state = MIG_STATE_SETUP;
461 s->total_time = qemu_get_clock_ms(rt_clock);
463 return s;
466 static GSList *migration_blockers;
468 void migrate_add_blocker(Error *reason)
470 migration_blockers = g_slist_prepend(migration_blockers, reason);
473 void migrate_del_blocker(Error *reason)
475 migration_blockers = g_slist_remove(migration_blockers, reason);
478 void qmp_migrate(const char *uri, bool has_blk, bool blk,
479 bool has_inc, bool inc, bool has_detach, bool detach,
480 Error **errp)
482 MigrationState *s = migrate_get_current();
483 MigrationParams params;
484 const char *p;
485 int ret;
487 params.blk = blk;
488 params.shared = inc;
490 if (s->state == MIG_STATE_ACTIVE) {
491 error_set(errp, QERR_MIGRATION_ACTIVE);
492 return;
495 if (qemu_savevm_state_blocked(errp)) {
496 return;
499 if (migration_blockers) {
500 *errp = error_copy(migration_blockers->data);
501 return;
504 s = migrate_init(&params);
506 if (strstart(uri, "tcp:", &p)) {
507 ret = tcp_start_outgoing_migration(s, p, errp);
508 #if !defined(WIN32)
509 } else if (strstart(uri, "exec:", &p)) {
510 ret = exec_start_outgoing_migration(s, p);
511 } else if (strstart(uri, "unix:", &p)) {
512 ret = unix_start_outgoing_migration(s, p);
513 } else if (strstart(uri, "fd:", &p)) {
514 ret = fd_start_outgoing_migration(s, p);
515 #endif
516 } else {
517 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
518 return;
521 if (ret < 0) {
522 if (!error_is_set(errp)) {
523 DPRINTF("migration failed: %s\n", strerror(-ret));
524 /* FIXME: we should return meaningful errors */
525 error_set(errp, QERR_UNDEFINED_ERROR);
527 return;
530 notifier_list_notify(&migration_state_notifiers, s);
533 void qmp_migrate_cancel(Error **errp)
535 migrate_fd_cancel(migrate_get_current());
538 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
540 MigrationState *s = migrate_get_current();
542 /* Check for truncation */
543 if (value != (size_t)value) {
544 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
545 "exceeding address space");
546 return;
549 s->xbzrle_cache_size = xbzrle_cache_resize(value);
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;
565 s = migrate_get_current();
566 s->bandwidth_limit = value;
567 qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
570 void qmp_migrate_set_downtime(double value, Error **errp)
572 value *= 1e9;
573 value = MAX(0, MIN(UINT64_MAX, value));
574 max_downtime = (uint64_t)value;
577 int migrate_use_xbzrle(void)
579 MigrationState *s;
581 s = migrate_get_current();
583 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
586 int64_t migrate_xbzrle_cache_size(void)
588 MigrationState *s;
590 s = migrate_get_current();
592 return s->xbzrle_cache_size;