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();
134 info
->ram
->total_time
= qemu_get_clock_ms(rt_clock
)
137 if (blk_mig_active()) {
138 info
->has_disk
= true;
139 info
->disk
= g_malloc0(sizeof(*info
->disk
));
140 info
->disk
->transferred
= blk_mig_bytes_transferred();
141 info
->disk
->remaining
= blk_mig_bytes_remaining();
142 info
->disk
->total
= blk_mig_bytes_total();
145 case MIG_STATE_COMPLETED
:
146 info
->has_status
= true;
147 info
->status
= g_strdup("completed");
149 info
->has_ram
= true;
150 info
->ram
= g_malloc0(sizeof(*info
->ram
));
151 info
->ram
->transferred
= ram_bytes_transferred();
152 info
->ram
->remaining
= 0;
153 info
->ram
->total
= ram_bytes_total();
154 info
->ram
->total_time
= s
->total_time
;
156 case MIG_STATE_ERROR
:
157 info
->has_status
= true;
158 info
->status
= g_strdup("failed");
160 case MIG_STATE_CANCELLED
:
161 info
->has_status
= true;
162 info
->status
= g_strdup("cancelled");
169 /* shared migration helpers */
171 static int migrate_fd_cleanup(MigrationState
*s
)
175 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
178 DPRINTF("closing file\n");
179 ret
= qemu_fclose(s
->file
);
191 void migrate_fd_error(MigrationState
*s
)
193 DPRINTF("setting error state\n");
194 s
->state
= MIG_STATE_ERROR
;
195 notifier_list_notify(&migration_state_notifiers
, s
);
196 migrate_fd_cleanup(s
);
199 static void migrate_fd_completed(MigrationState
*s
)
201 DPRINTF("setting completed state\n");
202 if (migrate_fd_cleanup(s
) < 0) {
203 s
->state
= MIG_STATE_ERROR
;
205 s
->state
= MIG_STATE_COMPLETED
;
206 runstate_set(RUN_STATE_POSTMIGRATE
);
208 notifier_list_notify(&migration_state_notifiers
, s
);
211 static void migrate_fd_put_notify(void *opaque
)
213 MigrationState
*s
= opaque
;
215 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
216 qemu_file_put_notify(s
->file
);
217 if (s
->file
&& qemu_file_get_error(s
->file
)) {
222 static ssize_t
migrate_fd_put_buffer(void *opaque
, const void *data
,
225 MigrationState
*s
= opaque
;
228 if (s
->state
!= MIG_STATE_ACTIVE
) {
233 ret
= s
->write(s
, data
, size
);
234 } while (ret
== -1 && ((s
->get_error(s
)) == EINTR
));
237 ret
= -(s
->get_error(s
));
239 if (ret
== -EAGAIN
) {
240 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, migrate_fd_put_notify
, s
);
246 static void migrate_fd_put_ready(void *opaque
)
248 MigrationState
*s
= opaque
;
251 if (s
->state
!= MIG_STATE_ACTIVE
) {
252 DPRINTF("put_ready returning because of non-active state\n");
256 DPRINTF("iterate\n");
257 ret
= qemu_savevm_state_iterate(s
->file
);
260 } else if (ret
== 1) {
261 int old_vm_running
= runstate_is_running();
263 DPRINTF("done iterating\n");
264 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
265 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE
);
267 if (qemu_savevm_state_complete(s
->file
) < 0) {
270 migrate_fd_completed(s
);
272 s
->total_time
= qemu_get_clock_ms(rt_clock
) - s
->total_time
;
273 if (s
->state
!= MIG_STATE_COMPLETED
) {
274 if (old_vm_running
) {
281 static void migrate_fd_cancel(MigrationState
*s
)
283 if (s
->state
!= MIG_STATE_ACTIVE
)
286 DPRINTF("cancelling migration\n");
288 s
->state
= MIG_STATE_CANCELLED
;
289 notifier_list_notify(&migration_state_notifiers
, s
);
290 qemu_savevm_state_cancel(s
->file
);
292 migrate_fd_cleanup(s
);
295 static void migrate_fd_wait_for_unfreeze(void *opaque
)
297 MigrationState
*s
= opaque
;
300 DPRINTF("wait for unfreeze\n");
301 if (s
->state
!= MIG_STATE_ACTIVE
)
308 FD_SET(s
->fd
, &wfds
);
310 ret
= select(s
->fd
+ 1, NULL
, &wfds
, NULL
, NULL
);
311 } while (ret
== -1 && (s
->get_error(s
)) == EINTR
);
314 qemu_file_set_error(s
->file
, -s
->get_error(s
));
318 static int migrate_fd_close(void *opaque
)
320 MigrationState
*s
= opaque
;
322 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
326 void add_migration_state_change_notifier(Notifier
*notify
)
328 notifier_list_add(&migration_state_notifiers
, notify
);
331 void remove_migration_state_change_notifier(Notifier
*notify
)
333 notifier_remove(notify
);
336 bool migration_is_active(MigrationState
*s
)
338 return s
->state
== MIG_STATE_ACTIVE
;
341 bool migration_has_finished(MigrationState
*s
)
343 return s
->state
== MIG_STATE_COMPLETED
;
346 bool migration_has_failed(MigrationState
*s
)
348 return (s
->state
== MIG_STATE_CANCELLED
||
349 s
->state
== MIG_STATE_ERROR
);
352 void migrate_fd_connect(MigrationState
*s
)
356 s
->state
= MIG_STATE_ACTIVE
;
357 s
->file
= qemu_fopen_ops_buffered(s
,
359 migrate_fd_put_buffer
,
360 migrate_fd_put_ready
,
361 migrate_fd_wait_for_unfreeze
,
364 DPRINTF("beginning savevm\n");
365 ret
= qemu_savevm_state_begin(s
->file
, &s
->params
);
367 DPRINTF("failed, %d\n", ret
);
371 migrate_fd_put_ready(s
);
374 static MigrationState
*migrate_init(const MigrationParams
*params
)
376 MigrationState
*s
= migrate_get_current();
377 int64_t bandwidth_limit
= s
->bandwidth_limit
;
379 memset(s
, 0, sizeof(*s
));
380 s
->bandwidth_limit
= bandwidth_limit
;
383 s
->bandwidth_limit
= bandwidth_limit
;
384 s
->state
= MIG_STATE_SETUP
;
385 s
->total_time
= qemu_get_clock_ms(rt_clock
);
390 static GSList
*migration_blockers
;
392 void migrate_add_blocker(Error
*reason
)
394 migration_blockers
= g_slist_prepend(migration_blockers
, reason
);
397 void migrate_del_blocker(Error
*reason
)
399 migration_blockers
= g_slist_remove(migration_blockers
, reason
);
402 void qmp_migrate(const char *uri
, bool has_blk
, bool blk
,
403 bool has_inc
, bool inc
, bool has_detach
, bool detach
,
406 MigrationState
*s
= migrate_get_current();
407 MigrationParams params
;
414 if (s
->state
== MIG_STATE_ACTIVE
) {
415 error_set(errp
, QERR_MIGRATION_ACTIVE
);
419 if (qemu_savevm_state_blocked(errp
)) {
423 if (migration_blockers
) {
424 *errp
= error_copy(migration_blockers
->data
);
428 s
= migrate_init(¶ms
);
430 if (strstart(uri
, "tcp:", &p
)) {
431 ret
= tcp_start_outgoing_migration(s
, p
, errp
);
433 } else if (strstart(uri
, "exec:", &p
)) {
434 ret
= exec_start_outgoing_migration(s
, p
);
435 } else if (strstart(uri
, "unix:", &p
)) {
436 ret
= unix_start_outgoing_migration(s
, p
);
437 } else if (strstart(uri
, "fd:", &p
)) {
438 ret
= fd_start_outgoing_migration(s
, p
);
441 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "uri", "a valid migration protocol");
446 if (!error_is_set(errp
)) {
447 DPRINTF("migration failed: %s\n", strerror(-ret
));
448 /* FIXME: we should return meaningful errors */
449 error_set(errp
, QERR_UNDEFINED_ERROR
);
454 notifier_list_notify(&migration_state_notifiers
, s
);
457 void qmp_migrate_cancel(Error
**errp
)
459 migrate_fd_cancel(migrate_get_current());
462 void qmp_migrate_set_speed(int64_t value
, Error
**errp
)
470 s
= migrate_get_current();
471 s
->bandwidth_limit
= value
;
472 qemu_file_set_rate_limit(s
->file
, s
->bandwidth_limit
);
475 void qmp_migrate_set_downtime(double value
, Error
**errp
)
478 value
= MAX(0, MIN(UINT64_MAX
, value
));
479 max_downtime
= (uint64_t)value
;