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 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 void qemu_start_incoming_migration(const char *uri
, Error
**errp
)
71 if (strstart(uri
, "tcp:", &p
))
72 tcp_start_incoming_migration(p
, errp
);
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
);
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");
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();
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)
117 MigrationCapabilityStatusList
*qmp_query_migrate_capabilities(Error
**errp
)
119 MigrationCapabilityStatusList
*head
= NULL
;
120 MigrationCapabilityStatusList
*caps
;
121 MigrationState
*s
= migrate_get_current();
124 for (i
= 0; i
< MIGRATION_CAPABILITY_MAX
; i
++) {
126 head
= g_malloc0(sizeof(*caps
));
129 caps
->next
= g_malloc0(sizeof(*caps
));
133 g_malloc(sizeof(*caps
->value
));
134 caps
->value
->capability
= i
;
135 caps
->value
->state
= s
->enabled_capabilities
[i
];
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();
160 case MIG_STATE_SETUP
:
161 /* no migration has happened ever */
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
)
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
);
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();
211 case MIG_STATE_ERROR
:
212 info
->has_status
= true;
213 info
->status
= g_strdup("failed");
215 case MIG_STATE_CANCELLED
:
216 info
->has_status
= true;
217 info
->status
= g_strdup("cancelled");
224 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList
*params
,
227 MigrationState
*s
= migrate_get_current();
228 MigrationCapabilityStatusList
*cap
;
230 if (s
->state
== MIG_STATE_ACTIVE
) {
231 error_set(errp
, QERR_MIGRATION_ACTIVE
);
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
)
247 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
251 DPRINTF("closing file\n");
252 ret
= qemu_fclose(s
->file
);
264 void migrate_fd_error(MigrationState
*s
)
266 DPRINTF("setting error state\n");
267 s
->state
= MIG_STATE_ERROR
;
268 notifier_list_notify(&migration_state_notifiers
, s
);
269 migrate_fd_cleanup(s
);
272 static void migrate_fd_completed(MigrationState
*s
)
274 DPRINTF("setting completed state\n");
275 if (migrate_fd_cleanup(s
) < 0) {
276 s
->state
= MIG_STATE_ERROR
;
278 s
->state
= MIG_STATE_COMPLETED
;
279 runstate_set(RUN_STATE_POSTMIGRATE
);
281 notifier_list_notify(&migration_state_notifiers
, s
);
284 static void migrate_fd_put_notify(void *opaque
)
286 MigrationState
*s
= opaque
;
289 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
290 ret
= qemu_file_put_notify(s
->file
);
296 ssize_t
migrate_fd_put_buffer(MigrationState
*s
, const void *data
,
301 if (s
->state
!= MIG_STATE_ACTIVE
) {
306 ret
= s
->write(s
, data
, size
);
307 } while (ret
== -1 && ((s
->get_error(s
)) == EINTR
));
310 ret
= -(s
->get_error(s
));
312 if (ret
== -EAGAIN
) {
313 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, migrate_fd_put_notify
, s
);
319 void migrate_fd_put_ready(MigrationState
*s
)
323 if (s
->state
!= MIG_STATE_ACTIVE
) {
324 DPRINTF("put_ready returning because of non-active state\n");
328 DPRINTF("iterate\n");
329 ret
= qemu_savevm_state_iterate(s
->file
);
332 } else if (ret
== 1) {
333 int old_vm_running
= runstate_is_running();
334 int64_t start_time
, end_time
;
336 DPRINTF("done iterating\n");
337 start_time
= qemu_get_clock_ms(rt_clock
);
338 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
339 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE
);
341 if (qemu_savevm_state_complete(s
->file
) < 0) {
344 migrate_fd_completed(s
);
346 end_time
= qemu_get_clock_ms(rt_clock
);
347 s
->total_time
= end_time
- s
->total_time
;
348 s
->downtime
= end_time
- start_time
;
349 if (s
->state
!= MIG_STATE_COMPLETED
) {
350 if (old_vm_running
) {
357 static void migrate_fd_cancel(MigrationState
*s
)
359 if (s
->state
!= MIG_STATE_ACTIVE
)
362 DPRINTF("cancelling migration\n");
364 s
->state
= MIG_STATE_CANCELLED
;
365 notifier_list_notify(&migration_state_notifiers
, s
);
366 qemu_savevm_state_cancel(s
->file
);
368 migrate_fd_cleanup(s
);
371 int migrate_fd_wait_for_unfreeze(MigrationState
*s
)
375 DPRINTF("wait for unfreeze\n");
376 if (s
->state
!= MIG_STATE_ACTIVE
)
383 FD_SET(s
->fd
, &wfds
);
385 ret
= select(s
->fd
+ 1, NULL
, &wfds
, NULL
, NULL
);
386 } while (ret
== -1 && (s
->get_error(s
)) == EINTR
);
389 return -s
->get_error(s
);
394 int migrate_fd_close(MigrationState
*s
)
396 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
400 void add_migration_state_change_notifier(Notifier
*notify
)
402 notifier_list_add(&migration_state_notifiers
, notify
);
405 void remove_migration_state_change_notifier(Notifier
*notify
)
407 notifier_remove(notify
);
410 bool migration_is_active(MigrationState
*s
)
412 return s
->state
== MIG_STATE_ACTIVE
;
415 bool migration_has_finished(MigrationState
*s
)
417 return s
->state
== MIG_STATE_COMPLETED
;
420 bool migration_has_failed(MigrationState
*s
)
422 return (s
->state
== MIG_STATE_CANCELLED
||
423 s
->state
== MIG_STATE_ERROR
);
426 void migrate_fd_connect(MigrationState
*s
)
430 s
->state
= MIG_STATE_ACTIVE
;
431 s
->file
= qemu_fopen_ops_buffered(s
);
433 DPRINTF("beginning savevm\n");
434 ret
= qemu_savevm_state_begin(s
->file
, &s
->params
);
436 DPRINTF("failed, %d\n", ret
);
440 migrate_fd_put_ready(s
);
443 static MigrationState
*migrate_init(const MigrationParams
*params
)
445 MigrationState
*s
= migrate_get_current();
446 int64_t bandwidth_limit
= s
->bandwidth_limit
;
447 bool enabled_capabilities
[MIGRATION_CAPABILITY_MAX
];
448 int64_t xbzrle_cache_size
= s
->xbzrle_cache_size
;
450 memcpy(enabled_capabilities
, s
->enabled_capabilities
,
451 sizeof(enabled_capabilities
));
453 memset(s
, 0, sizeof(*s
));
454 s
->bandwidth_limit
= bandwidth_limit
;
456 memcpy(s
->enabled_capabilities
, enabled_capabilities
,
457 sizeof(enabled_capabilities
));
458 s
->xbzrle_cache_size
= xbzrle_cache_size
;
460 s
->bandwidth_limit
= bandwidth_limit
;
461 s
->state
= MIG_STATE_SETUP
;
462 s
->total_time
= qemu_get_clock_ms(rt_clock
);
467 static GSList
*migration_blockers
;
469 void migrate_add_blocker(Error
*reason
)
471 migration_blockers
= g_slist_prepend(migration_blockers
, reason
);
474 void migrate_del_blocker(Error
*reason
)
476 migration_blockers
= g_slist_remove(migration_blockers
, reason
);
479 void qmp_migrate(const char *uri
, bool has_blk
, bool blk
,
480 bool has_inc
, bool inc
, bool has_detach
, bool detach
,
483 Error
*local_err
= NULL
;
484 MigrationState
*s
= migrate_get_current();
485 MigrationParams params
;
491 if (s
->state
== MIG_STATE_ACTIVE
) {
492 error_set(errp
, QERR_MIGRATION_ACTIVE
);
496 if (qemu_savevm_state_blocked(errp
)) {
500 if (migration_blockers
) {
501 *errp
= error_copy(migration_blockers
->data
);
505 s
= migrate_init(¶ms
);
507 if (strstart(uri
, "tcp:", &p
)) {
508 tcp_start_outgoing_migration(s
, p
, &local_err
);
510 } else if (strstart(uri
, "exec:", &p
)) {
511 exec_start_outgoing_migration(s
, p
, &local_err
);
512 } else if (strstart(uri
, "unix:", &p
)) {
513 unix_start_outgoing_migration(s
, p
, &local_err
);
514 } else if (strstart(uri
, "fd:", &p
)) {
515 fd_start_outgoing_migration(s
, p
, &local_err
);
518 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "uri", "a valid migration protocol");
524 error_propagate(errp
, local_err
);
528 notifier_list_notify(&migration_state_notifiers
, s
);
531 void qmp_migrate_cancel(Error
**errp
)
533 migrate_fd_cancel(migrate_get_current());
536 void qmp_migrate_set_cache_size(int64_t value
, Error
**errp
)
538 MigrationState
*s
= migrate_get_current();
540 /* Check for truncation */
541 if (value
!= (size_t)value
) {
542 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "cache size",
543 "exceeding address space");
547 s
->xbzrle_cache_size
= xbzrle_cache_resize(value
);
550 int64_t qmp_query_migrate_cache_size(Error
**errp
)
552 return migrate_xbzrle_cache_size();
555 void qmp_migrate_set_speed(int64_t value
, Error
**errp
)
563 s
= migrate_get_current();
564 s
->bandwidth_limit
= value
;
565 qemu_file_set_rate_limit(s
->file
, s
->bandwidth_limit
);
568 void qmp_migrate_set_downtime(double value
, Error
**errp
)
571 value
= MAX(0, MIN(UINT64_MAX
, value
));
572 max_downtime
= (uint64_t)value
;
575 int migrate_use_xbzrle(void)
579 s
= migrate_get_current();
581 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_XBZRLE
];
584 int64_t migrate_xbzrle_cache_size(void)
588 s
= migrate_get_current();
590 return s
->xbzrle_cache_size
;