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
)
68 if (strstart(uri
, "tcp:", &p
))
69 ret
= tcp_start_incoming_migration(p
);
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 /* Make sure all file formats flush their mutable metadata */
95 bdrv_invalidate_cache_all();
100 runstate_set(RUN_STATE_PRELAUNCH
);
104 /* amount of nanoseconds we are willing to wait for migration to be down.
105 * the choice of nanoseconds is because it is the maximum resolution that
106 * get_clock() can achieve. It is an internal measure. All user-visible
107 * units must be in seconds */
108 static uint64_t max_downtime
= 30000000;
110 uint64_t migrate_max_downtime(void)
115 MigrationInfo
*qmp_query_migrate(Error
**errp
)
117 MigrationInfo
*info
= g_malloc0(sizeof(*info
));
118 MigrationState
*s
= migrate_get_current();
121 case MIG_STATE_SETUP
:
122 /* no migration has happened ever */
124 case MIG_STATE_ACTIVE
:
125 info
->has_status
= true;
126 info
->status
= g_strdup("active");
128 info
->has_ram
= true;
129 info
->ram
= g_malloc0(sizeof(*info
->ram
));
130 info
->ram
->transferred
= ram_bytes_transferred();
131 info
->ram
->remaining
= ram_bytes_remaining();
132 info
->ram
->total
= ram_bytes_total();
134 if (blk_mig_active()) {
135 info
->has_disk
= true;
136 info
->disk
= g_malloc0(sizeof(*info
->disk
));
137 info
->disk
->transferred
= blk_mig_bytes_transferred();
138 info
->disk
->remaining
= blk_mig_bytes_remaining();
139 info
->disk
->total
= blk_mig_bytes_total();
142 case MIG_STATE_COMPLETED
:
143 info
->has_status
= true;
144 info
->status
= g_strdup("completed");
146 case MIG_STATE_ERROR
:
147 info
->has_status
= true;
148 info
->status
= g_strdup("failed");
150 case MIG_STATE_CANCELLED
:
151 info
->has_status
= true;
152 info
->status
= g_strdup("cancelled");
159 /* shared migration helpers */
161 static int migrate_fd_cleanup(MigrationState
*s
)
165 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
168 DPRINTF("closing file\n");
169 ret
= qemu_fclose(s
->file
);
181 void migrate_fd_error(MigrationState
*s
)
183 DPRINTF("setting error state\n");
184 s
->state
= MIG_STATE_ERROR
;
185 notifier_list_notify(&migration_state_notifiers
, s
);
186 migrate_fd_cleanup(s
);
189 static void migrate_fd_completed(MigrationState
*s
)
191 DPRINTF("setting completed state\n");
192 if (migrate_fd_cleanup(s
) < 0) {
193 s
->state
= MIG_STATE_ERROR
;
195 s
->state
= MIG_STATE_COMPLETED
;
196 runstate_set(RUN_STATE_POSTMIGRATE
);
198 notifier_list_notify(&migration_state_notifiers
, s
);
201 static void migrate_fd_put_notify(void *opaque
)
203 MigrationState
*s
= opaque
;
205 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
206 qemu_file_put_notify(s
->file
);
207 if (s
->file
&& qemu_file_get_error(s
->file
)) {
212 static ssize_t
migrate_fd_put_buffer(void *opaque
, const void *data
,
215 MigrationState
*s
= opaque
;
218 if (s
->state
!= MIG_STATE_ACTIVE
) {
223 ret
= s
->write(s
, data
, size
);
224 } while (ret
== -1 && ((s
->get_error(s
)) == EINTR
));
227 ret
= -(s
->get_error(s
));
229 if (ret
== -EAGAIN
) {
230 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, migrate_fd_put_notify
, s
);
236 static void migrate_fd_put_ready(void *opaque
)
238 MigrationState
*s
= opaque
;
241 if (s
->state
!= MIG_STATE_ACTIVE
) {
242 DPRINTF("put_ready returning because of non-active state\n");
246 DPRINTF("iterate\n");
247 ret
= qemu_savevm_state_iterate(s
->file
);
250 } else if (ret
== 1) {
251 int old_vm_running
= runstate_is_running();
253 DPRINTF("done iterating\n");
254 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE
);
256 if (qemu_savevm_state_complete(s
->file
) < 0) {
259 migrate_fd_completed(s
);
261 if (s
->state
!= MIG_STATE_COMPLETED
) {
262 if (old_vm_running
) {
269 static void migrate_fd_cancel(MigrationState
*s
)
271 if (s
->state
!= MIG_STATE_ACTIVE
)
274 DPRINTF("cancelling migration\n");
276 s
->state
= MIG_STATE_CANCELLED
;
277 notifier_list_notify(&migration_state_notifiers
, s
);
278 qemu_savevm_state_cancel(s
->file
);
280 migrate_fd_cleanup(s
);
283 static void migrate_fd_wait_for_unfreeze(void *opaque
)
285 MigrationState
*s
= opaque
;
288 DPRINTF("wait for unfreeze\n");
289 if (s
->state
!= MIG_STATE_ACTIVE
)
296 FD_SET(s
->fd
, &wfds
);
298 ret
= select(s
->fd
+ 1, NULL
, &wfds
, NULL
, NULL
);
299 } while (ret
== -1 && (s
->get_error(s
)) == EINTR
);
302 qemu_file_set_error(s
->file
, -s
->get_error(s
));
306 static int migrate_fd_close(void *opaque
)
308 MigrationState
*s
= opaque
;
310 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
314 void add_migration_state_change_notifier(Notifier
*notify
)
316 notifier_list_add(&migration_state_notifiers
, notify
);
319 void remove_migration_state_change_notifier(Notifier
*notify
)
321 notifier_remove(notify
);
324 bool migration_is_active(MigrationState
*s
)
326 return s
->state
== MIG_STATE_ACTIVE
;
329 bool migration_has_finished(MigrationState
*s
)
331 return s
->state
== MIG_STATE_COMPLETED
;
334 bool migration_has_failed(MigrationState
*s
)
336 return (s
->state
== MIG_STATE_CANCELLED
||
337 s
->state
== MIG_STATE_ERROR
);
340 void migrate_fd_connect(MigrationState
*s
)
344 s
->state
= MIG_STATE_ACTIVE
;
345 s
->file
= qemu_fopen_ops_buffered(s
,
347 migrate_fd_put_buffer
,
348 migrate_fd_put_ready
,
349 migrate_fd_wait_for_unfreeze
,
352 DPRINTF("beginning savevm\n");
353 ret
= qemu_savevm_state_begin(s
->file
, s
->blk
, s
->shared
);
355 DPRINTF("failed, %d\n", ret
);
359 migrate_fd_put_ready(s
);
362 static MigrationState
*migrate_init(int blk
, int inc
)
364 MigrationState
*s
= migrate_get_current();
365 int64_t bandwidth_limit
= s
->bandwidth_limit
;
367 memset(s
, 0, sizeof(*s
));
368 s
->bandwidth_limit
= bandwidth_limit
;
372 s
->bandwidth_limit
= bandwidth_limit
;
373 s
->state
= MIG_STATE_SETUP
;
378 static GSList
*migration_blockers
;
380 void migrate_add_blocker(Error
*reason
)
382 migration_blockers
= g_slist_prepend(migration_blockers
, reason
);
385 void migrate_del_blocker(Error
*reason
)
387 migration_blockers
= g_slist_remove(migration_blockers
, reason
);
390 void qmp_migrate(const char *uri
, bool has_blk
, bool blk
,
391 bool has_inc
, bool inc
, bool has_detach
, bool detach
,
394 MigrationState
*s
= migrate_get_current();
398 if (s
->state
== MIG_STATE_ACTIVE
) {
399 error_set(errp
, QERR_MIGRATION_ACTIVE
);
403 if (qemu_savevm_state_blocked(errp
)) {
407 if (migration_blockers
) {
408 *errp
= error_copy(migration_blockers
->data
);
412 s
= migrate_init(blk
, inc
);
414 if (strstart(uri
, "tcp:", &p
)) {
415 ret
= tcp_start_outgoing_migration(s
, p
);
417 } else if (strstart(uri
, "exec:", &p
)) {
418 ret
= exec_start_outgoing_migration(s
, p
);
419 } else if (strstart(uri
, "unix:", &p
)) {
420 ret
= unix_start_outgoing_migration(s
, p
);
421 } else if (strstart(uri
, "fd:", &p
)) {
422 ret
= fd_start_outgoing_migration(s
, p
);
425 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "uri", "a valid migration protocol");
430 DPRINTF("migration failed: %s\n", strerror(-ret
));
431 /* FIXME: we should return meaningful errors */
432 error_set(errp
, QERR_UNDEFINED_ERROR
);
436 notifier_list_notify(&migration_state_notifiers
, s
);
439 void qmp_migrate_cancel(Error
**errp
)
441 migrate_fd_cancel(migrate_get_current());
444 void qmp_migrate_set_speed(int64_t value
, Error
**errp
)
452 s
= migrate_get_current();
453 s
->bandwidth_limit
= value
;
454 qemu_file_set_rate_limit(s
->file
, s
->bandwidth_limit
);
457 void qmp_migrate_set_downtime(double value
, Error
**errp
)
460 value
= MAX(0, MIN(UINT64_MAX
, value
));
461 max_downtime
= (uint64_t)value
;