migration: use migrate_fd_close in migrate_fd_cleanup
[qemu/ar7.git] / migration.c
bloba63596f9a0ba656291f9e4d9a2c1c2b0141c8cc6
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 void process_incoming_migration(QEMUFile *f)
88 if (qemu_loadvm_state(f) < 0) {
89 fprintf(stderr, "load of migration failed\n");
90 exit(0);
92 qemu_announce_self();
93 DPRINTF("successfully loaded vm state\n");
95 bdrv_clear_incoming_migration_all();
96 /* Make sure all file formats flush their mutable metadata */
97 bdrv_invalidate_cache_all();
99 if (autostart) {
100 vm_start();
101 } else {
102 runstate_set(RUN_STATE_PAUSED);
106 /* amount of nanoseconds we are willing to wait for migration to be down.
107 * the choice of nanoseconds is because it is the maximum resolution that
108 * get_clock() can achieve. It is an internal measure. All user-visible
109 * units must be in seconds */
110 static uint64_t max_downtime = 30000000;
112 uint64_t migrate_max_downtime(void)
114 return max_downtime;
117 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
119 MigrationCapabilityStatusList *head = NULL;
120 MigrationCapabilityStatusList *caps;
121 MigrationState *s = migrate_get_current();
122 int i;
124 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
125 if (head == NULL) {
126 head = g_malloc0(sizeof(*caps));
127 caps = head;
128 } else {
129 caps->next = g_malloc0(sizeof(*caps));
130 caps = caps->next;
132 caps->value =
133 g_malloc(sizeof(*caps->value));
134 caps->value->capability = i;
135 caps->value->state = s->enabled_capabilities[i];
138 return head;
141 static void get_xbzrle_cache_stats(MigrationInfo *info)
143 if (migrate_use_xbzrle()) {
144 info->has_xbzrle_cache = true;
145 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
146 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
147 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
148 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
149 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
150 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
154 MigrationInfo *qmp_query_migrate(Error **errp)
156 MigrationInfo *info = g_malloc0(sizeof(*info));
157 MigrationState *s = migrate_get_current();
159 switch (s->state) {
160 case MIG_STATE_SETUP:
161 /* no migration has happened ever */
162 break;
163 case MIG_STATE_ACTIVE:
164 info->has_status = true;
165 info->status = g_strdup("active");
166 info->has_total_time = true;
167 info->total_time = qemu_get_clock_ms(rt_clock)
168 - s->total_time;
169 info->has_expected_downtime = true;
170 info->expected_downtime = s->expected_downtime;
172 info->has_ram = true;
173 info->ram = g_malloc0(sizeof(*info->ram));
174 info->ram->transferred = ram_bytes_transferred();
175 info->ram->remaining = ram_bytes_remaining();
176 info->ram->total = ram_bytes_total();
177 info->ram->duplicate = dup_mig_pages_transferred();
178 info->ram->normal = norm_mig_pages_transferred();
179 info->ram->normal_bytes = norm_mig_bytes_transferred();
180 info->ram->dirty_pages_rate = s->dirty_pages_rate;
183 if (blk_mig_active()) {
184 info->has_disk = true;
185 info->disk = g_malloc0(sizeof(*info->disk));
186 info->disk->transferred = blk_mig_bytes_transferred();
187 info->disk->remaining = blk_mig_bytes_remaining();
188 info->disk->total = blk_mig_bytes_total();
191 get_xbzrle_cache_stats(info);
192 break;
193 case MIG_STATE_COMPLETED:
194 get_xbzrle_cache_stats(info);
196 info->has_status = true;
197 info->status = g_strdup("completed");
198 info->total_time = s->total_time;
199 info->has_downtime = true;
200 info->downtime = s->downtime;
202 info->has_ram = true;
203 info->ram = g_malloc0(sizeof(*info->ram));
204 info->ram->transferred = ram_bytes_transferred();
205 info->ram->remaining = 0;
206 info->ram->total = ram_bytes_total();
207 info->ram->duplicate = dup_mig_pages_transferred();
208 info->ram->normal = norm_mig_pages_transferred();
209 info->ram->normal_bytes = norm_mig_bytes_transferred();
210 break;
211 case MIG_STATE_ERROR:
212 info->has_status = true;
213 info->status = g_strdup("failed");
214 break;
215 case MIG_STATE_CANCELLED:
216 info->has_status = true;
217 info->status = g_strdup("cancelled");
218 break;
221 return info;
224 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
225 Error **errp)
227 MigrationState *s = migrate_get_current();
228 MigrationCapabilityStatusList *cap;
230 if (s->state == MIG_STATE_ACTIVE) {
231 error_set(errp, QERR_MIGRATION_ACTIVE);
232 return;
235 for (cap = params; cap; cap = cap->next) {
236 s->enabled_capabilities[cap->value->capability] = cap->value->state;
240 /* shared migration helpers */
242 static int migrate_fd_cleanup(MigrationState *s)
244 int ret = 0;
246 if (s->file) {
247 DPRINTF("closing file\n");
248 ret = qemu_fclose(s->file);
249 s->file = NULL;
252 migrate_fd_close(s);
253 return ret;
256 void migrate_fd_error(MigrationState *s)
258 DPRINTF("setting error state\n");
259 s->state = MIG_STATE_ERROR;
260 notifier_list_notify(&migration_state_notifiers, s);
261 migrate_fd_cleanup(s);
264 static void migrate_fd_completed(MigrationState *s)
266 DPRINTF("setting completed state\n");
267 if (migrate_fd_cleanup(s) < 0) {
268 s->state = MIG_STATE_ERROR;
269 } else {
270 s->state = MIG_STATE_COMPLETED;
271 runstate_set(RUN_STATE_POSTMIGRATE);
273 notifier_list_notify(&migration_state_notifiers, s);
276 static void migrate_fd_put_notify(void *opaque)
278 MigrationState *s = opaque;
279 int ret;
281 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
282 ret = qemu_file_put_notify(s->file);
283 if (ret) {
284 migrate_fd_error(s);
288 ssize_t migrate_fd_put_buffer(MigrationState *s, const void *data,
289 size_t size)
291 ssize_t ret;
293 if (s->state != MIG_STATE_ACTIVE) {
294 return -EIO;
297 do {
298 ret = s->write(s, data, size);
299 } while (ret == -1 && ((s->get_error(s)) == EINTR));
301 if (ret == -1)
302 ret = -(s->get_error(s));
304 if (ret == -EAGAIN) {
305 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
308 return ret;
311 void migrate_fd_put_ready(MigrationState *s)
313 int ret;
315 if (s->state != MIG_STATE_ACTIVE) {
316 DPRINTF("put_ready returning because of non-active state\n");
317 return;
320 DPRINTF("iterate\n");
321 ret = qemu_savevm_state_iterate(s->file);
322 if (ret < 0) {
323 migrate_fd_error(s);
324 } else if (ret == 1) {
325 int old_vm_running = runstate_is_running();
326 int64_t start_time, end_time;
328 DPRINTF("done iterating\n");
329 start_time = qemu_get_clock_ms(rt_clock);
330 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
331 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
333 if (qemu_savevm_state_complete(s->file) < 0) {
334 migrate_fd_error(s);
335 } else {
336 migrate_fd_completed(s);
338 end_time = qemu_get_clock_ms(rt_clock);
339 s->total_time = end_time - s->total_time;
340 s->downtime = end_time - start_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 int migrate_fd_wait_for_unfreeze(MigrationState *s)
365 int ret;
367 DPRINTF("wait for unfreeze\n");
368 if (s->state != MIG_STATE_ACTIVE)
369 return -EINVAL;
371 do {
372 fd_set wfds;
374 FD_ZERO(&wfds);
375 FD_SET(s->fd, &wfds);
377 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
378 } while (ret == -1 && (s->get_error(s)) == EINTR);
380 if (ret == -1) {
381 return -s->get_error(s);
383 return 0;
386 int migrate_fd_close(MigrationState *s)
388 int rc = 0;
389 if (s->fd != -1) {
390 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
391 rc = s->close(s);
392 s->fd = -1;
394 return rc;
397 void add_migration_state_change_notifier(Notifier *notify)
399 notifier_list_add(&migration_state_notifiers, notify);
402 void remove_migration_state_change_notifier(Notifier *notify)
404 notifier_remove(notify);
407 bool migration_is_active(MigrationState *s)
409 return s->state == MIG_STATE_ACTIVE;
412 bool migration_has_finished(MigrationState *s)
414 return s->state == MIG_STATE_COMPLETED;
417 bool migration_has_failed(MigrationState *s)
419 return (s->state == MIG_STATE_CANCELLED ||
420 s->state == MIG_STATE_ERROR);
423 void migrate_fd_connect(MigrationState *s)
425 int ret;
427 s->state = MIG_STATE_ACTIVE;
428 s->file = qemu_fopen_ops_buffered(s);
430 DPRINTF("beginning savevm\n");
431 ret = qemu_savevm_state_begin(s->file, &s->params);
432 if (ret < 0) {
433 DPRINTF("failed, %d\n", ret);
434 migrate_fd_error(s);
435 return;
437 migrate_fd_put_ready(s);
440 static MigrationState *migrate_init(const MigrationParams *params)
442 MigrationState *s = migrate_get_current();
443 int64_t bandwidth_limit = s->bandwidth_limit;
444 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
445 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
447 memcpy(enabled_capabilities, s->enabled_capabilities,
448 sizeof(enabled_capabilities));
450 memset(s, 0, sizeof(*s));
451 s->bandwidth_limit = bandwidth_limit;
452 s->params = *params;
453 memcpy(s->enabled_capabilities, enabled_capabilities,
454 sizeof(enabled_capabilities));
455 s->xbzrle_cache_size = xbzrle_cache_size;
457 s->bandwidth_limit = bandwidth_limit;
458 s->state = MIG_STATE_SETUP;
459 s->total_time = qemu_get_clock_ms(rt_clock);
461 return s;
464 static GSList *migration_blockers;
466 void migrate_add_blocker(Error *reason)
468 migration_blockers = g_slist_prepend(migration_blockers, reason);
471 void migrate_del_blocker(Error *reason)
473 migration_blockers = g_slist_remove(migration_blockers, reason);
476 void qmp_migrate(const char *uri, bool has_blk, bool blk,
477 bool has_inc, bool inc, bool has_detach, bool detach,
478 Error **errp)
480 Error *local_err = NULL;
481 MigrationState *s = migrate_get_current();
482 MigrationParams params;
483 const char *p;
485 params.blk = blk;
486 params.shared = inc;
488 if (s->state == MIG_STATE_ACTIVE) {
489 error_set(errp, QERR_MIGRATION_ACTIVE);
490 return;
493 if (qemu_savevm_state_blocked(errp)) {
494 return;
497 if (migration_blockers) {
498 *errp = error_copy(migration_blockers->data);
499 return;
502 s = migrate_init(&params);
504 if (strstart(uri, "tcp:", &p)) {
505 tcp_start_outgoing_migration(s, p, &local_err);
506 #if !defined(WIN32)
507 } else if (strstart(uri, "exec:", &p)) {
508 exec_start_outgoing_migration(s, p, &local_err);
509 } else if (strstart(uri, "unix:", &p)) {
510 unix_start_outgoing_migration(s, p, &local_err);
511 } else if (strstart(uri, "fd:", &p)) {
512 fd_start_outgoing_migration(s, p, &local_err);
513 #endif
514 } else {
515 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
516 return;
519 if (local_err) {
520 migrate_fd_error(s);
521 error_propagate(errp, local_err);
522 return;
525 notifier_list_notify(&migration_state_notifiers, s);
528 void qmp_migrate_cancel(Error **errp)
530 migrate_fd_cancel(migrate_get_current());
533 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
535 MigrationState *s = migrate_get_current();
537 /* Check for truncation */
538 if (value != (size_t)value) {
539 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
540 "exceeding address space");
541 return;
544 s->xbzrle_cache_size = xbzrle_cache_resize(value);
547 int64_t qmp_query_migrate_cache_size(Error **errp)
549 return migrate_xbzrle_cache_size();
552 void qmp_migrate_set_speed(int64_t value, Error **errp)
554 MigrationState *s;
556 if (value < 0) {
557 value = 0;
560 s = migrate_get_current();
561 s->bandwidth_limit = value;
562 qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
565 void qmp_migrate_set_downtime(double value, Error **errp)
567 value *= 1e9;
568 value = MAX(0, MIN(UINT64_MAX, value));
569 max_downtime = (uint64_t)value;
572 int migrate_use_xbzrle(void)
574 MigrationState *s;
576 s = migrate_get_current();
578 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
581 int64_t migrate_xbzrle_cache_size(void)
583 MigrationState *s;
585 s = migrate_get_current();
587 return s->xbzrle_cache_size;