4 * Copyright IBM, Corp. 2008
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"
19 #include "buffered_file.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)
32 #define DPRINTF(fmt, ...) \
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 ¤t_migration
;
67 int qemu_start_incoming_migration(const char *uri
, Error
**errp
)
72 if (strstart(uri
, "tcp:", &p
))
73 ret
= tcp_start_incoming_migration(p
, errp
);
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
);
83 fprintf(stderr
, "unknown migration protocol: %s\n", uri
);
84 ret
= -EPROTONOSUPPORT
;
89 void process_incoming_migration(QEMUFile
*f
)
91 if (qemu_loadvm_state(f
) < 0) {
92 fprintf(stderr
, "load of migration failed\n");
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();
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)
120 MigrationCapabilityStatusList
*qmp_query_migrate_capabilities(Error
**errp
)
122 MigrationCapabilityStatusList
*head
= NULL
;
123 MigrationCapabilityStatusList
*caps
;
124 MigrationState
*s
= migrate_get_current();
127 for (i
= 0; i
< MIGRATION_CAPABILITY_MAX
; i
++) {
129 head
= g_malloc0(sizeof(*caps
));
132 caps
->next
= g_malloc0(sizeof(*caps
));
136 g_malloc(sizeof(*caps
->value
));
137 caps
->value
->capability
= i
;
138 caps
->value
->state
= s
->enabled_capabilities
[i
];
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();
163 case MIG_STATE_SETUP
:
164 /* no migration has happened ever */
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
)
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
);
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();
208 case MIG_STATE_ERROR
:
209 info
->has_status
= true;
210 info
->status
= g_strdup("failed");
212 case MIG_STATE_CANCELLED
:
213 info
->has_status
= true;
214 info
->status
= g_strdup("cancelled");
221 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList
*params
,
224 MigrationState
*s
= migrate_get_current();
225 MigrationCapabilityStatusList
*cap
;
227 if (s
->state
== MIG_STATE_ACTIVE
) {
228 error_set(errp
, QERR_MIGRATION_ACTIVE
);
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
)
244 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
248 DPRINTF("closing file\n");
249 ret
= qemu_fclose(s
->file
);
261 void migrate_fd_error(MigrationState
*s
)
263 DPRINTF("setting error state\n");
264 s
->state
= MIG_STATE_ERROR
;
265 notifier_list_notify(&migration_state_notifiers
, s
);
266 migrate_fd_cleanup(s
);
269 static void migrate_fd_completed(MigrationState
*s
)
271 DPRINTF("setting completed state\n");
272 if (migrate_fd_cleanup(s
) < 0) {
273 s
->state
= MIG_STATE_ERROR
;
275 s
->state
= MIG_STATE_COMPLETED
;
276 runstate_set(RUN_STATE_POSTMIGRATE
);
278 notifier_list_notify(&migration_state_notifiers
, s
);
281 static void migrate_fd_put_notify(void *opaque
)
283 MigrationState
*s
= opaque
;
285 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
286 qemu_file_put_notify(s
->file
);
287 if (s
->file
&& qemu_file_get_error(s
->file
)) {
292 static ssize_t
migrate_fd_put_buffer(void *opaque
, const void *data
,
295 MigrationState
*s
= opaque
;
298 if (s
->state
!= MIG_STATE_ACTIVE
) {
303 ret
= s
->write(s
, data
, size
);
304 } while (ret
== -1 && ((s
->get_error(s
)) == EINTR
));
307 ret
= -(s
->get_error(s
));
309 if (ret
== -EAGAIN
) {
310 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, migrate_fd_put_notify
, s
);
316 static void migrate_fd_put_ready(void *opaque
)
318 MigrationState
*s
= opaque
;
321 if (s
->state
!= MIG_STATE_ACTIVE
) {
322 DPRINTF("put_ready returning because of non-active state\n");
326 DPRINTF("iterate\n");
327 ret
= qemu_savevm_state_iterate(s
->file
);
330 } else if (ret
== 1) {
331 int old_vm_running
= runstate_is_running();
333 DPRINTF("done iterating\n");
334 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
335 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE
);
337 if (qemu_savevm_state_complete(s
->file
) < 0) {
340 migrate_fd_completed(s
);
342 s
->total_time
= qemu_get_clock_ms(rt_clock
) - s
->total_time
;
343 if (s
->state
!= MIG_STATE_COMPLETED
) {
344 if (old_vm_running
) {
351 static void migrate_fd_cancel(MigrationState
*s
)
353 if (s
->state
!= MIG_STATE_ACTIVE
)
356 DPRINTF("cancelling migration\n");
358 s
->state
= MIG_STATE_CANCELLED
;
359 notifier_list_notify(&migration_state_notifiers
, s
);
360 qemu_savevm_state_cancel(s
->file
);
362 migrate_fd_cleanup(s
);
365 static void migrate_fd_wait_for_unfreeze(void *opaque
)
367 MigrationState
*s
= opaque
;
370 DPRINTF("wait for unfreeze\n");
371 if (s
->state
!= MIG_STATE_ACTIVE
)
378 FD_SET(s
->fd
, &wfds
);
380 ret
= select(s
->fd
+ 1, NULL
, &wfds
, NULL
, NULL
);
381 } while (ret
== -1 && (s
->get_error(s
)) == EINTR
);
384 qemu_file_set_error(s
->file
, -s
->get_error(s
));
388 static int migrate_fd_close(void *opaque
)
390 MigrationState
*s
= opaque
;
392 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
396 void add_migration_state_change_notifier(Notifier
*notify
)
398 notifier_list_add(&migration_state_notifiers
, notify
);
401 void remove_migration_state_change_notifier(Notifier
*notify
)
403 notifier_remove(notify
);
406 bool migration_is_active(MigrationState
*s
)
408 return s
->state
== MIG_STATE_ACTIVE
;
411 bool migration_has_finished(MigrationState
*s
)
413 return s
->state
== MIG_STATE_COMPLETED
;
416 bool migration_has_failed(MigrationState
*s
)
418 return (s
->state
== MIG_STATE_CANCELLED
||
419 s
->state
== MIG_STATE_ERROR
);
422 void migrate_fd_connect(MigrationState
*s
)
426 s
->state
= MIG_STATE_ACTIVE
;
427 s
->file
= qemu_fopen_ops_buffered(s
,
429 migrate_fd_put_buffer
,
430 migrate_fd_put_ready
,
431 migrate_fd_wait_for_unfreeze
,
434 DPRINTF("beginning savevm\n");
435 ret
= qemu_savevm_state_begin(s
->file
, &s
->params
);
437 DPRINTF("failed, %d\n", ret
);
441 migrate_fd_put_ready(s
);
444 static MigrationState
*migrate_init(const MigrationParams
*params
)
446 MigrationState
*s
= migrate_get_current();
447 int64_t bandwidth_limit
= s
->bandwidth_limit
;
448 bool enabled_capabilities
[MIGRATION_CAPABILITY_MAX
];
449 int64_t xbzrle_cache_size
= s
->xbzrle_cache_size
;
451 memcpy(enabled_capabilities
, s
->enabled_capabilities
,
452 sizeof(enabled_capabilities
));
454 memset(s
, 0, sizeof(*s
));
455 s
->bandwidth_limit
= bandwidth_limit
;
457 memcpy(s
->enabled_capabilities
, enabled_capabilities
,
458 sizeof(enabled_capabilities
));
459 s
->xbzrle_cache_size
= xbzrle_cache_size
;
461 s
->bandwidth_limit
= bandwidth_limit
;
462 s
->state
= MIG_STATE_SETUP
;
463 s
->total_time
= qemu_get_clock_ms(rt_clock
);
468 static GSList
*migration_blockers
;
470 void migrate_add_blocker(Error
*reason
)
472 migration_blockers
= g_slist_prepend(migration_blockers
, reason
);
475 void migrate_del_blocker(Error
*reason
)
477 migration_blockers
= g_slist_remove(migration_blockers
, reason
);
480 void qmp_migrate(const char *uri
, bool has_blk
, bool blk
,
481 bool has_inc
, bool inc
, bool has_detach
, bool detach
,
484 MigrationState
*s
= migrate_get_current();
485 MigrationParams params
;
492 if (s
->state
== MIG_STATE_ACTIVE
) {
493 error_set(errp
, QERR_MIGRATION_ACTIVE
);
497 if (qemu_savevm_state_blocked(errp
)) {
501 if (migration_blockers
) {
502 *errp
= error_copy(migration_blockers
->data
);
506 s
= migrate_init(¶ms
);
508 if (strstart(uri
, "tcp:", &p
)) {
509 ret
= tcp_start_outgoing_migration(s
, p
, errp
);
511 } else if (strstart(uri
, "exec:", &p
)) {
512 ret
= exec_start_outgoing_migration(s
, p
);
513 } else if (strstart(uri
, "unix:", &p
)) {
514 ret
= unix_start_outgoing_migration(s
, p
);
515 } else if (strstart(uri
, "fd:", &p
)) {
516 ret
= fd_start_outgoing_migration(s
, p
);
519 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "uri", "a valid migration protocol");
524 if (!error_is_set(errp
)) {
525 DPRINTF("migration failed: %s\n", strerror(-ret
));
526 /* FIXME: we should return meaningful errors */
527 error_set(errp
, QERR_UNDEFINED_ERROR
);
532 notifier_list_notify(&migration_state_notifiers
, s
);
535 void qmp_migrate_cancel(Error
**errp
)
537 migrate_fd_cancel(migrate_get_current());
540 void qmp_migrate_set_cache_size(int64_t value
, Error
**errp
)
542 MigrationState
*s
= migrate_get_current();
544 /* Check for truncation */
545 if (value
!= (size_t)value
) {
546 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "cache size",
547 "exceeding address space");
551 s
->xbzrle_cache_size
= xbzrle_cache_resize(value
);
554 int64_t qmp_query_migrate_cache_size(Error
**errp
)
556 return migrate_xbzrle_cache_size();
559 void qmp_migrate_set_speed(int64_t value
, Error
**errp
)
567 s
= migrate_get_current();
568 s
->bandwidth_limit
= value
;
569 qemu_file_set_rate_limit(s
->file
, s
->bandwidth_limit
);
572 void qmp_migrate_set_downtime(double value
, Error
**errp
)
575 value
= MAX(0, MIN(UINT64_MAX
, value
));
576 max_downtime
= (uint64_t)value
;
579 int migrate_use_xbzrle(void)
583 s
= migrate_get_current();
585 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_XBZRLE
];
588 int64_t migrate_xbzrle_cache_size(void)
592 s
= migrate_get_current();
594 return s
->xbzrle_cache_size
;