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 void migrate_fd_monitor_suspend(MigrationState
*s
, Monitor
*mon
)
163 if (monitor_suspend(mon
) == 0) {
164 DPRINTF("suspending monitor\n");
166 monitor_printf(mon
, "terminal does not allow synchronous "
167 "migration, continuing detached\n");
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
);
183 monitor_resume(s
->mon
);
195 void migrate_fd_error(MigrationState
*s
)
197 DPRINTF("setting error state\n");
198 s
->state
= MIG_STATE_ERROR
;
199 notifier_list_notify(&migration_state_notifiers
, s
);
200 migrate_fd_cleanup(s
);
203 static void migrate_fd_completed(MigrationState
*s
)
205 DPRINTF("setting completed state\n");
206 if (migrate_fd_cleanup(s
) < 0) {
207 s
->state
= MIG_STATE_ERROR
;
209 s
->state
= MIG_STATE_COMPLETED
;
210 runstate_set(RUN_STATE_POSTMIGRATE
);
212 notifier_list_notify(&migration_state_notifiers
, s
);
215 static void migrate_fd_put_notify(void *opaque
)
217 MigrationState
*s
= opaque
;
219 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
220 qemu_file_put_notify(s
->file
);
221 if (s
->file
&& qemu_file_get_error(s
->file
)) {
226 static ssize_t
migrate_fd_put_buffer(void *opaque
, const void *data
,
229 MigrationState
*s
= opaque
;
232 if (s
->state
!= MIG_STATE_ACTIVE
) {
237 ret
= s
->write(s
, data
, size
);
238 } while (ret
== -1 && ((s
->get_error(s
)) == EINTR
));
241 ret
= -(s
->get_error(s
));
243 if (ret
== -EAGAIN
) {
244 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, migrate_fd_put_notify
, s
);
250 static void migrate_fd_put_ready(void *opaque
)
252 MigrationState
*s
= opaque
;
255 if (s
->state
!= MIG_STATE_ACTIVE
) {
256 DPRINTF("put_ready returning because of non-active state\n");
260 DPRINTF("iterate\n");
261 ret
= qemu_savevm_state_iterate(s
->mon
, s
->file
);
264 } else if (ret
== 1) {
265 int old_vm_running
= runstate_is_running();
267 DPRINTF("done iterating\n");
268 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE
);
270 if (qemu_savevm_state_complete(s
->mon
, s
->file
) < 0) {
273 migrate_fd_completed(s
);
275 if (s
->state
!= MIG_STATE_COMPLETED
) {
276 if (old_vm_running
) {
283 static void migrate_fd_cancel(MigrationState
*s
)
285 if (s
->state
!= MIG_STATE_ACTIVE
)
288 DPRINTF("cancelling migration\n");
290 s
->state
= MIG_STATE_CANCELLED
;
291 notifier_list_notify(&migration_state_notifiers
, s
);
292 qemu_savevm_state_cancel(s
->mon
, s
->file
);
294 migrate_fd_cleanup(s
);
297 static void migrate_fd_wait_for_unfreeze(void *opaque
)
299 MigrationState
*s
= opaque
;
302 DPRINTF("wait for unfreeze\n");
303 if (s
->state
!= MIG_STATE_ACTIVE
)
310 FD_SET(s
->fd
, &wfds
);
312 ret
= select(s
->fd
+ 1, NULL
, &wfds
, NULL
, NULL
);
313 } while (ret
== -1 && (s
->get_error(s
)) == EINTR
);
316 qemu_file_set_error(s
->file
, -s
->get_error(s
));
320 static int migrate_fd_close(void *opaque
)
322 MigrationState
*s
= opaque
;
325 monitor_resume(s
->mon
);
327 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
331 void add_migration_state_change_notifier(Notifier
*notify
)
333 notifier_list_add(&migration_state_notifiers
, notify
);
336 void remove_migration_state_change_notifier(Notifier
*notify
)
338 notifier_remove(notify
);
341 bool migration_is_active(MigrationState
*s
)
343 return s
->state
== MIG_STATE_ACTIVE
;
346 bool migration_has_finished(MigrationState
*s
)
348 return s
->state
== MIG_STATE_COMPLETED
;
351 bool migration_has_failed(MigrationState
*s
)
353 return (s
->state
== MIG_STATE_CANCELLED
||
354 s
->state
== MIG_STATE_ERROR
);
357 void migrate_fd_connect(MigrationState
*s
)
361 s
->state
= MIG_STATE_ACTIVE
;
362 s
->file
= qemu_fopen_ops_buffered(s
,
364 migrate_fd_put_buffer
,
365 migrate_fd_put_ready
,
366 migrate_fd_wait_for_unfreeze
,
369 DPRINTF("beginning savevm\n");
370 ret
= qemu_savevm_state_begin(s
->mon
, s
->file
, s
->blk
, s
->shared
);
372 DPRINTF("failed, %d\n", ret
);
376 migrate_fd_put_ready(s
);
379 static MigrationState
*migrate_init(Monitor
*mon
, int detach
, int blk
, int inc
)
381 MigrationState
*s
= migrate_get_current();
382 int64_t bandwidth_limit
= s
->bandwidth_limit
;
384 memset(s
, 0, sizeof(*s
));
385 s
->bandwidth_limit
= bandwidth_limit
;
389 /* s->mon is used for two things:
390 - pass fd in fd migration
391 - suspend/resume monitor for not detached migration
394 s
->bandwidth_limit
= bandwidth_limit
;
395 s
->state
= MIG_STATE_SETUP
;
398 migrate_fd_monitor_suspend(s
, mon
);
404 static GSList
*migration_blockers
;
406 void migrate_add_blocker(Error
*reason
)
408 migration_blockers
= g_slist_prepend(migration_blockers
, reason
);
411 void migrate_del_blocker(Error
*reason
)
413 migration_blockers
= g_slist_remove(migration_blockers
, reason
);
416 int do_migrate(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
418 MigrationState
*s
= migrate_get_current();
420 int detach
= qdict_get_try_bool(qdict
, "detach", 0);
421 int blk
= qdict_get_try_bool(qdict
, "blk", 0);
422 int inc
= qdict_get_try_bool(qdict
, "inc", 0);
423 const char *uri
= qdict_get_str(qdict
, "uri");
426 if (s
->state
== MIG_STATE_ACTIVE
) {
427 monitor_printf(mon
, "migration already in progress\n");
431 if (qemu_savevm_state_blocked(mon
)) {
435 if (migration_blockers
) {
436 Error
*err
= migration_blockers
->data
;
437 qerror_report_err(err
);
441 s
= migrate_init(mon
, detach
, blk
, inc
);
443 if (strstart(uri
, "tcp:", &p
)) {
444 ret
= tcp_start_outgoing_migration(s
, p
);
446 } else if (strstart(uri
, "exec:", &p
)) {
447 ret
= exec_start_outgoing_migration(s
, p
);
448 } else if (strstart(uri
, "unix:", &p
)) {
449 ret
= unix_start_outgoing_migration(s
, p
);
450 } else if (strstart(uri
, "fd:", &p
)) {
451 ret
= fd_start_outgoing_migration(s
, p
);
454 monitor_printf(mon
, "unknown migration protocol: %s\n", uri
);
459 monitor_printf(mon
, "migration failed: %s\n", strerror(-ret
));
467 notifier_list_notify(&migration_state_notifiers
, s
);
471 void qmp_migrate_cancel(Error
**errp
)
473 migrate_fd_cancel(migrate_get_current());
476 void qmp_migrate_set_speed(int64_t value
, Error
**errp
)
484 s
= migrate_get_current();
485 s
->bandwidth_limit
= value
;
486 qemu_file_set_rate_limit(s
->file
, s
->bandwidth_limit
);
489 void qmp_migrate_set_downtime(double value
, Error
**errp
)
492 value
= MAX(0, MIN(UINT64_MAX
, value
));
493 max_downtime
= (uint64_t)value
;