]>
Commit | Line | Data |
---|---|---|
5bb7910a AL |
1 | /* |
2 | * QEMU live migration | |
3 | * | |
4 | * Copyright IBM, Corp. 2008 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <[email protected]> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
10 | * the COPYING file in the top-level directory. | |
11 | * | |
6b620ca3 PB |
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. | |
5bb7910a AL |
14 | */ |
15 | ||
16 | #include "qemu-common.h" | |
17 | #include "migration.h" | |
376253ec | 18 | #include "monitor.h" |
065e2813 AL |
19 | #include "buffered_file.h" |
20 | #include "sysemu.h" | |
21 | #include "block.h" | |
22 | #include "qemu_socket.h" | |
25f23643 | 23 | #include "block-migration.h" |
791e7c82 | 24 | #include "qmp-commands.h" |
065e2813 AL |
25 | |
26 | //#define DEBUG_MIGRATION | |
27 | ||
28 | #ifdef DEBUG_MIGRATION | |
d0f2c4c6 | 29 | #define DPRINTF(fmt, ...) \ |
065e2813 AL |
30 | do { printf("migration: " fmt, ## __VA_ARGS__); } while (0) |
31 | #else | |
d0f2c4c6 | 32 | #define DPRINTF(fmt, ...) \ |
065e2813 AL |
33 | do { } while (0) |
34 | #endif | |
5bb7910a | 35 | |
7dc688ed JQ |
36 | enum { |
37 | MIG_STATE_ERROR, | |
38 | MIG_STATE_SETUP, | |
39 | MIG_STATE_CANCELLED, | |
40 | MIG_STATE_ACTIVE, | |
41 | MIG_STATE_COMPLETED, | |
42 | }; | |
5bb7910a | 43 | |
d0ae46c1 | 44 | #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */ |
5bb7910a | 45 | |
99a0db9b GH |
46 | static NotifierList migration_state_notifiers = |
47 | NOTIFIER_LIST_INITIALIZER(migration_state_notifiers); | |
48 | ||
17549e84 JQ |
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 */ | |
52 | ||
53 | static MigrationState *migrate_get_current(void) | |
54 | { | |
55 | static MigrationState current_migration = { | |
56 | .state = MIG_STATE_SETUP, | |
d0ae46c1 | 57 | .bandwidth_limit = MAX_THROTTLE, |
17549e84 JQ |
58 | }; |
59 | ||
60 | return ¤t_migration; | |
61 | } | |
62 | ||
d5c5dacc | 63 | int qemu_start_incoming_migration(const char *uri, Error **errp) |
5bb7910a | 64 | { |
34c9dd8e | 65 | const char *p; |
8ca5e801 | 66 | int ret; |
34c9dd8e AL |
67 | |
68 | if (strstart(uri, "tcp:", &p)) | |
d5c5dacc | 69 | ret = tcp_start_incoming_migration(p, errp); |
065e2813 AL |
70 | #if !defined(WIN32) |
71 | else if (strstart(uri, "exec:", &p)) | |
8ca5e801 | 72 | ret = exec_start_incoming_migration(p); |
4951f65b | 73 | else if (strstart(uri, "unix:", &p)) |
8ca5e801 | 74 | ret = unix_start_incoming_migration(p); |
5ac1fad3 | 75 | else if (strstart(uri, "fd:", &p)) |
8ca5e801 | 76 | ret = fd_start_incoming_migration(p); |
065e2813 | 77 | #endif |
8ca5e801 | 78 | else { |
34c9dd8e | 79 | fprintf(stderr, "unknown migration protocol: %s\n", uri); |
8ca5e801 JQ |
80 | ret = -EPROTONOSUPPORT; |
81 | } | |
82 | return ret; | |
5bb7910a AL |
83 | } |
84 | ||
511c0231 JQ |
85 | void process_incoming_migration(QEMUFile *f) |
86 | { | |
87 | if (qemu_loadvm_state(f) < 0) { | |
88 | fprintf(stderr, "load of migration failed\n"); | |
89 | exit(0); | |
90 | } | |
91 | qemu_announce_self(); | |
92 | DPRINTF("successfully loaded vm state\n"); | |
93 | ||
901862cb | 94 | bdrv_clear_incoming_migration_all(); |
0f15423c AL |
95 | /* Make sure all file formats flush their mutable metadata */ |
96 | bdrv_invalidate_cache_all(); | |
97 | ||
f5bbfba1 | 98 | if (autostart) { |
511c0231 | 99 | vm_start(); |
f5bbfba1 | 100 | } else { |
0461d5a6 | 101 | runstate_set(RUN_STATE_PRELAUNCH); |
f5bbfba1 | 102 | } |
511c0231 JQ |
103 | } |
104 | ||
a0a3fd60 GC |
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; | |
110 | ||
111 | uint64_t migrate_max_downtime(void) | |
112 | { | |
113 | return max_downtime; | |
114 | } | |
115 | ||
791e7c82 | 116 | MigrationInfo *qmp_query_migrate(Error **errp) |
5bb7910a | 117 | { |
791e7c82 | 118 | MigrationInfo *info = g_malloc0(sizeof(*info)); |
17549e84 JQ |
119 | MigrationState *s = migrate_get_current(); |
120 | ||
121 | switch (s->state) { | |
122 | case MIG_STATE_SETUP: | |
123 | /* no migration has happened ever */ | |
124 | break; | |
125 | case MIG_STATE_ACTIVE: | |
791e7c82 LC |
126 | info->has_status = true; |
127 | info->status = g_strdup("active"); | |
17549e84 | 128 | |
791e7c82 LC |
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(); | |
d5f8a570 JQ |
134 | info->ram->total_time = qemu_get_clock_ms(rt_clock) |
135 | - s->total_time; | |
17549e84 JQ |
136 | |
137 | if (blk_mig_active()) { | |
791e7c82 LC |
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(); | |
ff8d81d8 | 143 | } |
17549e84 JQ |
144 | break; |
145 | case MIG_STATE_COMPLETED: | |
791e7c82 LC |
146 | info->has_status = true; |
147 | info->status = g_strdup("completed"); | |
d5f8a570 JQ |
148 | |
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; | |
17549e84 JQ |
155 | break; |
156 | case MIG_STATE_ERROR: | |
791e7c82 LC |
157 | info->has_status = true; |
158 | info->status = g_strdup("failed"); | |
17549e84 JQ |
159 | break; |
160 | case MIG_STATE_CANCELLED: | |
791e7c82 LC |
161 | info->has_status = true; |
162 | info->status = g_strdup("cancelled"); | |
17549e84 | 163 | break; |
5bb7910a | 164 | } |
791e7c82 LC |
165 | |
166 | return info; | |
5bb7910a AL |
167 | } |
168 | ||
065e2813 AL |
169 | /* shared migration helpers */ |
170 | ||
8b6b99b3 | 171 | static int migrate_fd_cleanup(MigrationState *s) |
065e2813 | 172 | { |
41ef56e6 AL |
173 | int ret = 0; |
174 | ||
065e2813 AL |
175 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
176 | ||
177 | if (s->file) { | |
d0f2c4c6 | 178 | DPRINTF("closing file\n"); |
a6d34a94 | 179 | ret = qemu_fclose(s->file); |
5d39c799 | 180 | s->file = NULL; |
065e2813 AL |
181 | } |
182 | ||
84ec6552 | 183 | if (s->fd != -1) { |
065e2813 | 184 | close(s->fd); |
84ec6552 | 185 | s->fd = -1; |
f327aa0c | 186 | } |
065e2813 | 187 | |
41ef56e6 | 188 | return ret; |
065e2813 AL |
189 | } |
190 | ||
8b6b99b3 | 191 | void migrate_fd_error(MigrationState *s) |
065e2813 | 192 | { |
8b6b99b3 JQ |
193 | DPRINTF("setting error state\n"); |
194 | s->state = MIG_STATE_ERROR; | |
e0eb7390 | 195 | notifier_list_notify(&migration_state_notifiers, s); |
8b6b99b3 JQ |
196 | migrate_fd_cleanup(s); |
197 | } | |
198 | ||
458cf28e JQ |
199 | static void migrate_fd_completed(MigrationState *s) |
200 | { | |
201 | DPRINTF("setting completed state\n"); | |
202 | if (migrate_fd_cleanup(s) < 0) { | |
203 | s->state = MIG_STATE_ERROR; | |
204 | } else { | |
205 | s->state = MIG_STATE_COMPLETED; | |
206 | runstate_set(RUN_STATE_POSTMIGRATE); | |
207 | } | |
e0eb7390 | 208 | notifier_list_notify(&migration_state_notifiers, s); |
458cf28e JQ |
209 | } |
210 | ||
8b6b99b3 | 211 | static void migrate_fd_put_notify(void *opaque) |
065e2813 | 212 | { |
22f00a44 | 213 | MigrationState *s = opaque; |
065e2813 AL |
214 | |
215 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); | |
216 | qemu_file_put_notify(s->file); | |
1fdc11c3 | 217 | if (s->file && qemu_file_get_error(s->file)) { |
2350e13c YT |
218 | migrate_fd_error(s); |
219 | } | |
065e2813 AL |
220 | } |
221 | ||
8b6b99b3 JQ |
222 | static ssize_t migrate_fd_put_buffer(void *opaque, const void *data, |
223 | size_t size) | |
065e2813 | 224 | { |
22f00a44 | 225 | MigrationState *s = opaque; |
065e2813 AL |
226 | ssize_t ret; |
227 | ||
fdbecb5d JQ |
228 | if (s->state != MIG_STATE_ACTIVE) { |
229 | return -EIO; | |
230 | } | |
231 | ||
065e2813 AL |
232 | do { |
233 | ret = s->write(s, data, size); | |
95b134ea | 234 | } while (ret == -1 && ((s->get_error(s)) == EINTR)); |
065e2813 AL |
235 | |
236 | if (ret == -1) | |
237 | ret = -(s->get_error(s)); | |
238 | ||
e447b1a6 | 239 | if (ret == -EAGAIN) { |
065e2813 | 240 | qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s); |
e447b1a6 | 241 | } |
065e2813 AL |
242 | |
243 | return ret; | |
244 | } | |
245 | ||
8b6b99b3 | 246 | static void migrate_fd_put_ready(void *opaque) |
065e2813 | 247 | { |
22f00a44 | 248 | MigrationState *s = opaque; |
065e2813 AL |
249 | int ret; |
250 | ||
065e2813 | 251 | if (s->state != MIG_STATE_ACTIVE) { |
d0f2c4c6 | 252 | DPRINTF("put_ready returning because of non-active state\n"); |
065e2813 AL |
253 | return; |
254 | } | |
255 | ||
d0f2c4c6 | 256 | DPRINTF("iterate\n"); |
539de124 | 257 | ret = qemu_savevm_state_iterate(s->file); |
39346385 JQ |
258 | if (ret < 0) { |
259 | migrate_fd_error(s); | |
260 | } else if (ret == 1) { | |
1354869c | 261 | int old_vm_running = runstate_is_running(); |
eeb34af9 | 262 | |
d0f2c4c6 | 263 | DPRINTF("done iterating\n"); |
7b5d3aa2 | 264 | qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER); |
8a9236f1 | 265 | vm_stop_force_state(RUN_STATE_FINISH_MIGRATE); |
065e2813 | 266 | |
539de124 | 267 | if (qemu_savevm_state_complete(s->file) < 0) { |
67afff79 | 268 | migrate_fd_error(s); |
b161d123 | 269 | } else { |
458cf28e | 270 | migrate_fd_completed(s); |
b161d123 | 271 | } |
d5f8a570 | 272 | s->total_time = qemu_get_clock_ms(rt_clock) - s->total_time; |
48a2f4d6 | 273 | if (s->state != MIG_STATE_COMPLETED) { |
41ef56e6 AL |
274 | if (old_vm_running) { |
275 | vm_start(); | |
276 | } | |
f5bbfba1 | 277 | } |
065e2813 AL |
278 | } |
279 | } | |
280 | ||
0edda1c4 | 281 | static void migrate_fd_cancel(MigrationState *s) |
065e2813 | 282 | { |
065e2813 AL |
283 | if (s->state != MIG_STATE_ACTIVE) |
284 | return; | |
285 | ||
d0f2c4c6 | 286 | DPRINTF("cancelling migration\n"); |
065e2813 AL |
287 | |
288 | s->state = MIG_STATE_CANCELLED; | |
e0eb7390 | 289 | notifier_list_notify(&migration_state_notifiers, s); |
539de124 | 290 | qemu_savevm_state_cancel(s->file); |
065e2813 AL |
291 | |
292 | migrate_fd_cleanup(s); | |
293 | } | |
294 | ||
8b6b99b3 | 295 | static void migrate_fd_wait_for_unfreeze(void *opaque) |
065e2813 | 296 | { |
22f00a44 | 297 | MigrationState *s = opaque; |
065e2813 AL |
298 | int ret; |
299 | ||
d0f2c4c6 | 300 | DPRINTF("wait for unfreeze\n"); |
065e2813 AL |
301 | if (s->state != MIG_STATE_ACTIVE) |
302 | return; | |
303 | ||
304 | do { | |
305 | fd_set wfds; | |
306 | ||
307 | FD_ZERO(&wfds); | |
308 | FD_SET(s->fd, &wfds); | |
309 | ||
310 | ret = select(s->fd + 1, NULL, &wfds, NULL, NULL); | |
311 | } while (ret == -1 && (s->get_error(s)) == EINTR); | |
af509450 JQ |
312 | |
313 | if (ret == -1) { | |
dcd1d224 | 314 | qemu_file_set_error(s->file, -s->get_error(s)); |
af509450 | 315 | } |
065e2813 AL |
316 | } |
317 | ||
8b6b99b3 | 318 | static int migrate_fd_close(void *opaque) |
065e2813 | 319 | { |
22f00a44 | 320 | MigrationState *s = opaque; |
e19252d3 UL |
321 | |
322 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); | |
065e2813 AL |
323 | return s->close(s); |
324 | } | |
99a0db9b GH |
325 | |
326 | void add_migration_state_change_notifier(Notifier *notify) | |
327 | { | |
328 | notifier_list_add(&migration_state_notifiers, notify); | |
329 | } | |
330 | ||
331 | void remove_migration_state_change_notifier(Notifier *notify) | |
332 | { | |
31552529 | 333 | notifier_remove(notify); |
99a0db9b GH |
334 | } |
335 | ||
afe2df69 GH |
336 | bool migration_is_active(MigrationState *s) |
337 | { | |
338 | return s->state == MIG_STATE_ACTIVE; | |
339 | } | |
340 | ||
7073693b | 341 | bool migration_has_finished(MigrationState *s) |
99a0db9b | 342 | { |
7073693b | 343 | return s->state == MIG_STATE_COMPLETED; |
99a0db9b | 344 | } |
0edda1c4 | 345 | |
afe2df69 GH |
346 | bool migration_has_failed(MigrationState *s) |
347 | { | |
348 | return (s->state == MIG_STATE_CANCELLED || | |
349 | s->state == MIG_STATE_ERROR); | |
350 | } | |
351 | ||
8b6b99b3 | 352 | void migrate_fd_connect(MigrationState *s) |
99a0db9b | 353 | { |
8b6b99b3 JQ |
354 | int ret; |
355 | ||
d5934dde | 356 | s->state = MIG_STATE_ACTIVE; |
8b6b99b3 JQ |
357 | s->file = qemu_fopen_ops_buffered(s, |
358 | s->bandwidth_limit, | |
359 | migrate_fd_put_buffer, | |
360 | migrate_fd_put_ready, | |
361 | migrate_fd_wait_for_unfreeze, | |
362 | migrate_fd_close); | |
363 | ||
364 | DPRINTF("beginning savevm\n"); | |
6607ae23 | 365 | ret = qemu_savevm_state_begin(s->file, &s->params); |
8b6b99b3 JQ |
366 | if (ret < 0) { |
367 | DPRINTF("failed, %d\n", ret); | |
368 | migrate_fd_error(s); | |
369 | return; | |
370 | } | |
371 | migrate_fd_put_ready(s); | |
372 | } | |
373 | ||
6607ae23 | 374 | static MigrationState *migrate_init(const MigrationParams *params) |
0edda1c4 | 375 | { |
17549e84 | 376 | MigrationState *s = migrate_get_current(); |
d0ae46c1 | 377 | int64_t bandwidth_limit = s->bandwidth_limit; |
0edda1c4 | 378 | |
17549e84 | 379 | memset(s, 0, sizeof(*s)); |
d0ae46c1 | 380 | s->bandwidth_limit = bandwidth_limit; |
6607ae23 | 381 | s->params = *params; |
1299c631 | 382 | |
0edda1c4 | 383 | s->bandwidth_limit = bandwidth_limit; |
d5934dde | 384 | s->state = MIG_STATE_SETUP; |
d5f8a570 | 385 | s->total_time = qemu_get_clock_ms(rt_clock); |
0edda1c4 | 386 | |
0edda1c4 JQ |
387 | return s; |
388 | } | |
cab30143 | 389 | |
fa2756b7 AL |
390 | static GSList *migration_blockers; |
391 | ||
392 | void migrate_add_blocker(Error *reason) | |
393 | { | |
394 | migration_blockers = g_slist_prepend(migration_blockers, reason); | |
395 | } | |
396 | ||
397 | void migrate_del_blocker(Error *reason) | |
398 | { | |
399 | migration_blockers = g_slist_remove(migration_blockers, reason); | |
400 | } | |
401 | ||
e1c37d0e LC |
402 | void qmp_migrate(const char *uri, bool has_blk, bool blk, |
403 | bool has_inc, bool inc, bool has_detach, bool detach, | |
404 | Error **errp) | |
cab30143 | 405 | { |
17549e84 | 406 | MigrationState *s = migrate_get_current(); |
6607ae23 | 407 | MigrationParams params; |
cab30143 | 408 | const char *p; |
cab30143 JQ |
409 | int ret; |
410 | ||
6607ae23 IY |
411 | params.blk = blk; |
412 | params.shared = inc; | |
413 | ||
17549e84 | 414 | if (s->state == MIG_STATE_ACTIVE) { |
e1c37d0e LC |
415 | error_set(errp, QERR_MIGRATION_ACTIVE); |
416 | return; | |
cab30143 JQ |
417 | } |
418 | ||
e1c37d0e LC |
419 | if (qemu_savevm_state_blocked(errp)) { |
420 | return; | |
cab30143 JQ |
421 | } |
422 | ||
fa2756b7 | 423 | if (migration_blockers) { |
e1c37d0e LC |
424 | *errp = error_copy(migration_blockers->data); |
425 | return; | |
fa2756b7 AL |
426 | } |
427 | ||
6607ae23 | 428 | s = migrate_init(¶ms); |
cab30143 JQ |
429 | |
430 | if (strstart(uri, "tcp:", &p)) { | |
d5c5dacc | 431 | ret = tcp_start_outgoing_migration(s, p, errp); |
cab30143 JQ |
432 | #if !defined(WIN32) |
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); | |
439 | #endif | |
99a0db9b | 440 | } else { |
e1c37d0e LC |
441 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol"); |
442 | return; | |
cab30143 JQ |
443 | } |
444 | ||
445 | if (ret < 0) { | |
d5c5dacc AK |
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); | |
450 | } | |
e1c37d0e | 451 | return; |
1299c631 JQ |
452 | } |
453 | ||
e0eb7390 | 454 | notifier_list_notify(&migration_state_notifiers, s); |
cab30143 JQ |
455 | } |
456 | ||
6cdedb07 | 457 | void qmp_migrate_cancel(Error **errp) |
cab30143 | 458 | { |
17549e84 | 459 | migrate_fd_cancel(migrate_get_current()); |
cab30143 JQ |
460 | } |
461 | ||
3dc85383 | 462 | void qmp_migrate_set_speed(int64_t value, Error **errp) |
cab30143 | 463 | { |
cab30143 JQ |
464 | MigrationState *s; |
465 | ||
3dc85383 LC |
466 | if (value < 0) { |
467 | value = 0; | |
99a0db9b | 468 | } |
cab30143 | 469 | |
17549e84 | 470 | s = migrate_get_current(); |
3dc85383 | 471 | s->bandwidth_limit = value; |
d0ae46c1 | 472 | qemu_file_set_rate_limit(s->file, s->bandwidth_limit); |
cab30143 JQ |
473 | } |
474 | ||
4f0a993b | 475 | void qmp_migrate_set_downtime(double value, Error **errp) |
cab30143 | 476 | { |
4f0a993b LC |
477 | value *= 1e9; |
478 | value = MAX(0, MIN(UINT64_MAX, value)); | |
479 | max_downtime = (uint64_t)value; | |
99a0db9b | 480 | } |