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 static NotifierList migration_state_notifiers
=
47 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers
);
49 /* When we add fault tolerance, we could have several
50 migrations at once. For now we don't need to add
51 dynamic creation of migration */
53 static MigrationState
*migrate_get_current(void)
55 static MigrationState current_migration
= {
56 .state
= MIG_STATE_SETUP
,
57 .bandwidth_limit
= MAX_THROTTLE
,
60 return ¤t_migration
;
63 int qemu_start_incoming_migration(const char *uri
, Error
**errp
)
68 if (strstart(uri
, "tcp:", &p
))
69 ret
= tcp_start_incoming_migration(p
, errp
);
71 else if (strstart(uri
, "exec:", &p
))
72 ret
= exec_start_incoming_migration(p
);
73 else if (strstart(uri
, "unix:", &p
))
74 ret
= unix_start_incoming_migration(p
);
75 else if (strstart(uri
, "fd:", &p
))
76 ret
= fd_start_incoming_migration(p
);
79 fprintf(stderr
, "unknown migration protocol: %s\n", uri
);
80 ret
= -EPROTONOSUPPORT
;
85 void process_incoming_migration(QEMUFile
*f
)
87 if (qemu_loadvm_state(f
) < 0) {
88 fprintf(stderr
, "load of migration failed\n");
92 DPRINTF("successfully loaded vm state\n");
94 bdrv_clear_incoming_migration_all();
95 /* Make sure all file formats flush their mutable metadata */
96 bdrv_invalidate_cache_all();
101 runstate_set(RUN_STATE_PRELAUNCH
);
105 /* amount of nanoseconds we are willing to wait for migration to be down.
106 * the choice of nanoseconds is because it is the maximum resolution that
107 * get_clock() can achieve. It is an internal measure. All user-visible
108 * units must be in seconds */
109 static uint64_t max_downtime
= 30000000;
111 uint64_t migrate_max_downtime(void)
116 MigrationInfo
*qmp_query_migrate(Error
**errp
)
118 MigrationInfo
*info
= g_malloc0(sizeof(*info
));
119 MigrationState
*s
= migrate_get_current();
122 case MIG_STATE_SETUP
:
123 /* no migration has happened ever */
125 case MIG_STATE_ACTIVE
:
126 info
->has_status
= true;
127 info
->status
= g_strdup("active");
129 info
->has_ram
= true;
130 info
->ram
= g_malloc0(sizeof(*info
->ram
));
131 info
->ram
->transferred
= ram_bytes_transferred();
132 info
->ram
->remaining
= ram_bytes_remaining();
133 info
->ram
->total
= ram_bytes_total();
135 if (blk_mig_active()) {
136 info
->has_disk
= true;
137 info
->disk
= g_malloc0(sizeof(*info
->disk
));
138 info
->disk
->transferred
= blk_mig_bytes_transferred();
139 info
->disk
->remaining
= blk_mig_bytes_remaining();
140 info
->disk
->total
= blk_mig_bytes_total();
143 case MIG_STATE_COMPLETED
:
144 info
->has_status
= true;
145 info
->status
= g_strdup("completed");
147 case MIG_STATE_ERROR
:
148 info
->has_status
= true;
149 info
->status
= g_strdup("failed");
151 case MIG_STATE_CANCELLED
:
152 info
->has_status
= true;
153 info
->status
= g_strdup("cancelled");
160 /* shared migration helpers */
162 static int migrate_fd_cleanup(MigrationState
*s
)
166 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
169 DPRINTF("closing file\n");
170 ret
= qemu_fclose(s
->file
);
182 void migrate_fd_error(MigrationState
*s
)
184 DPRINTF("setting error state\n");
185 s
->state
= MIG_STATE_ERROR
;
186 notifier_list_notify(&migration_state_notifiers
, s
);
187 migrate_fd_cleanup(s
);
190 static void migrate_fd_completed(MigrationState
*s
)
192 DPRINTF("setting completed state\n");
193 if (migrate_fd_cleanup(s
) < 0) {
194 s
->state
= MIG_STATE_ERROR
;
196 s
->state
= MIG_STATE_COMPLETED
;
197 runstate_set(RUN_STATE_POSTMIGRATE
);
199 notifier_list_notify(&migration_state_notifiers
, s
);
202 static void migrate_fd_put_notify(void *opaque
)
204 MigrationState
*s
= opaque
;
206 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
207 qemu_file_put_notify(s
->file
);
208 if (s
->file
&& qemu_file_get_error(s
->file
)) {
213 static ssize_t
migrate_fd_put_buffer(void *opaque
, const void *data
,
216 MigrationState
*s
= opaque
;
219 if (s
->state
!= MIG_STATE_ACTIVE
) {
224 ret
= s
->write(s
, data
, size
);
225 } while (ret
== -1 && ((s
->get_error(s
)) == EINTR
));
228 ret
= -(s
->get_error(s
));
230 if (ret
== -EAGAIN
) {
231 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, migrate_fd_put_notify
, s
);
237 static void migrate_fd_put_ready(void *opaque
)
239 MigrationState
*s
= opaque
;
242 if (s
->state
!= MIG_STATE_ACTIVE
) {
243 DPRINTF("put_ready returning because of non-active state\n");
247 DPRINTF("iterate\n");
248 ret
= qemu_savevm_state_iterate(s
->file
);
251 } else if (ret
== 1) {
252 int old_vm_running
= runstate_is_running();
254 DPRINTF("done iterating\n");
255 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
256 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE
);
258 if (qemu_savevm_state_complete(s
->file
) < 0) {
261 migrate_fd_completed(s
);
263 if (s
->state
!= MIG_STATE_COMPLETED
) {
264 if (old_vm_running
) {
271 static void migrate_fd_cancel(MigrationState
*s
)
273 if (s
->state
!= MIG_STATE_ACTIVE
)
276 DPRINTF("cancelling migration\n");
278 s
->state
= MIG_STATE_CANCELLED
;
279 notifier_list_notify(&migration_state_notifiers
, s
);
280 qemu_savevm_state_cancel(s
->file
);
282 migrate_fd_cleanup(s
);
285 static void migrate_fd_wait_for_unfreeze(void *opaque
)
287 MigrationState
*s
= opaque
;
290 DPRINTF("wait for unfreeze\n");
291 if (s
->state
!= MIG_STATE_ACTIVE
)
298 FD_SET(s
->fd
, &wfds
);
300 ret
= select(s
->fd
+ 1, NULL
, &wfds
, NULL
, NULL
);
301 } while (ret
== -1 && (s
->get_error(s
)) == EINTR
);
304 qemu_file_set_error(s
->file
, -s
->get_error(s
));
308 static int migrate_fd_close(void *opaque
)
310 MigrationState
*s
= opaque
;
312 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
316 void add_migration_state_change_notifier(Notifier
*notify
)
318 notifier_list_add(&migration_state_notifiers
, notify
);
321 void remove_migration_state_change_notifier(Notifier
*notify
)
323 notifier_remove(notify
);
326 bool migration_is_active(MigrationState
*s
)
328 return s
->state
== MIG_STATE_ACTIVE
;
331 bool migration_has_finished(MigrationState
*s
)
333 return s
->state
== MIG_STATE_COMPLETED
;
336 bool migration_has_failed(MigrationState
*s
)
338 return (s
->state
== MIG_STATE_CANCELLED
||
339 s
->state
== MIG_STATE_ERROR
);
342 void migrate_fd_connect(MigrationState
*s
)
346 s
->state
= MIG_STATE_ACTIVE
;
347 s
->file
= qemu_fopen_ops_buffered(s
,
349 migrate_fd_put_buffer
,
350 migrate_fd_put_ready
,
351 migrate_fd_wait_for_unfreeze
,
354 DPRINTF("beginning savevm\n");
355 ret
= qemu_savevm_state_begin(s
->file
, s
->blk
, s
->shared
);
357 DPRINTF("failed, %d\n", ret
);
361 migrate_fd_put_ready(s
);
364 static MigrationState
*migrate_init(int blk
, int inc
)
366 MigrationState
*s
= migrate_get_current();
367 int64_t bandwidth_limit
= s
->bandwidth_limit
;
369 memset(s
, 0, sizeof(*s
));
370 s
->bandwidth_limit
= bandwidth_limit
;
374 s
->bandwidth_limit
= bandwidth_limit
;
375 s
->state
= MIG_STATE_SETUP
;
380 static GSList
*migration_blockers
;
382 void migrate_add_blocker(Error
*reason
)
384 migration_blockers
= g_slist_prepend(migration_blockers
, reason
);
387 void migrate_del_blocker(Error
*reason
)
389 migration_blockers
= g_slist_remove(migration_blockers
, reason
);
392 void qmp_migrate(const char *uri
, bool has_blk
, bool blk
,
393 bool has_inc
, bool inc
, bool has_detach
, bool detach
,
396 MigrationState
*s
= migrate_get_current();
400 if (s
->state
== MIG_STATE_ACTIVE
) {
401 error_set(errp
, QERR_MIGRATION_ACTIVE
);
405 if (qemu_savevm_state_blocked(errp
)) {
409 if (migration_blockers
) {
410 *errp
= error_copy(migration_blockers
->data
);
414 s
= migrate_init(blk
, inc
);
416 if (strstart(uri
, "tcp:", &p
)) {
417 ret
= tcp_start_outgoing_migration(s
, p
, errp
);
419 } else if (strstart(uri
, "exec:", &p
)) {
420 ret
= exec_start_outgoing_migration(s
, p
);
421 } else if (strstart(uri
, "unix:", &p
)) {
422 ret
= unix_start_outgoing_migration(s
, p
);
423 } else if (strstart(uri
, "fd:", &p
)) {
424 ret
= fd_start_outgoing_migration(s
, p
);
427 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "uri", "a valid migration protocol");
432 if (!error_is_set(errp
)) {
433 DPRINTF("migration failed: %s\n", strerror(-ret
));
434 /* FIXME: we should return meaningful errors */
435 error_set(errp
, QERR_UNDEFINED_ERROR
);
440 notifier_list_notify(&migration_state_notifiers
, s
);
443 void qmp_migrate_cancel(Error
**errp
)
445 migrate_fd_cancel(migrate_get_current());
448 void qmp_migrate_set_speed(int64_t value
, Error
**errp
)
456 s
= migrate_get_current();
457 s
->bandwidth_limit
= value
;
458 qemu_file_set_rate_limit(s
->file
, s
->bandwidth_limit
);
461 void qmp_migrate_set_downtime(double value
, Error
**errp
)
464 value
= MAX(0, MIN(UINT64_MAX
, value
));
465 max_downtime
= (uint64_t)value
;