]>
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 | ||
8ca5e801 | 63 | int qemu_start_incoming_migration(const char *uri) |
5bb7910a | 64 | { |
34c9dd8e | 65 | const char *p; |
8ca5e801 | 66 | int ret; |
34c9dd8e AL |
67 | |
68 | if (strstart(uri, "tcp:", &p)) | |
8ca5e801 | 69 | ret = tcp_start_incoming_migration(p); |
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 | ||
0f15423c AL |
94 | /* Make sure all file formats flush their mutable metadata */ |
95 | bdrv_invalidate_cache_all(); | |
96 | ||
f5bbfba1 | 97 | if (autostart) { |
511c0231 | 98 | vm_start(); |
f5bbfba1 | 99 | } else { |
0461d5a6 | 100 | runstate_set(RUN_STATE_PRELAUNCH); |
f5bbfba1 | 101 | } |
511c0231 JQ |
102 | } |
103 | ||
a0a3fd60 GC |
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; | |
109 | ||
110 | uint64_t migrate_max_downtime(void) | |
111 | { | |
112 | return max_downtime; | |
113 | } | |
114 | ||
791e7c82 | 115 | MigrationInfo *qmp_query_migrate(Error **errp) |
5bb7910a | 116 | { |
791e7c82 | 117 | MigrationInfo *info = g_malloc0(sizeof(*info)); |
17549e84 JQ |
118 | MigrationState *s = migrate_get_current(); |
119 | ||
120 | switch (s->state) { | |
121 | case MIG_STATE_SETUP: | |
122 | /* no migration has happened ever */ | |
123 | break; | |
124 | case MIG_STATE_ACTIVE: | |
791e7c82 LC |
125 | info->has_status = true; |
126 | info->status = g_strdup("active"); | |
17549e84 | 127 | |
791e7c82 LC |
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(); | |
17549e84 JQ |
133 | |
134 | if (blk_mig_active()) { | |
791e7c82 LC |
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(); | |
ff8d81d8 | 140 | } |
17549e84 JQ |
141 | break; |
142 | case MIG_STATE_COMPLETED: | |
791e7c82 LC |
143 | info->has_status = true; |
144 | info->status = g_strdup("completed"); | |
17549e84 JQ |
145 | break; |
146 | case MIG_STATE_ERROR: | |
791e7c82 LC |
147 | info->has_status = true; |
148 | info->status = g_strdup("failed"); | |
17549e84 JQ |
149 | break; |
150 | case MIG_STATE_CANCELLED: | |
791e7c82 LC |
151 | info->has_status = true; |
152 | info->status = g_strdup("cancelled"); | |
17549e84 | 153 | break; |
5bb7910a | 154 | } |
791e7c82 LC |
155 | |
156 | return info; | |
5bb7910a AL |
157 | } |
158 | ||
065e2813 AL |
159 | /* shared migration helpers */ |
160 | ||
8b6b99b3 | 161 | static int migrate_fd_cleanup(MigrationState *s) |
065e2813 | 162 | { |
41ef56e6 AL |
163 | int ret = 0; |
164 | ||
065e2813 AL |
165 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
166 | ||
167 | if (s->file) { | |
d0f2c4c6 | 168 | DPRINTF("closing file\n"); |
a6d34a94 | 169 | ret = qemu_fclose(s->file); |
5d39c799 | 170 | s->file = NULL; |
065e2813 AL |
171 | } |
172 | ||
84ec6552 | 173 | if (s->fd != -1) { |
065e2813 | 174 | close(s->fd); |
84ec6552 | 175 | s->fd = -1; |
f327aa0c | 176 | } |
065e2813 | 177 | |
41ef56e6 | 178 | return ret; |
065e2813 AL |
179 | } |
180 | ||
8b6b99b3 | 181 | void migrate_fd_error(MigrationState *s) |
065e2813 | 182 | { |
8b6b99b3 JQ |
183 | DPRINTF("setting error state\n"); |
184 | s->state = MIG_STATE_ERROR; | |
e0eb7390 | 185 | notifier_list_notify(&migration_state_notifiers, s); |
8b6b99b3 JQ |
186 | migrate_fd_cleanup(s); |
187 | } | |
188 | ||
458cf28e JQ |
189 | static void migrate_fd_completed(MigrationState *s) |
190 | { | |
191 | DPRINTF("setting completed state\n"); | |
192 | if (migrate_fd_cleanup(s) < 0) { | |
193 | s->state = MIG_STATE_ERROR; | |
194 | } else { | |
195 | s->state = MIG_STATE_COMPLETED; | |
196 | runstate_set(RUN_STATE_POSTMIGRATE); | |
197 | } | |
e0eb7390 | 198 | notifier_list_notify(&migration_state_notifiers, s); |
458cf28e JQ |
199 | } |
200 | ||
8b6b99b3 | 201 | static void migrate_fd_put_notify(void *opaque) |
065e2813 | 202 | { |
22f00a44 | 203 | MigrationState *s = opaque; |
065e2813 AL |
204 | |
205 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); | |
206 | qemu_file_put_notify(s->file); | |
1fdc11c3 | 207 | if (s->file && qemu_file_get_error(s->file)) { |
2350e13c YT |
208 | migrate_fd_error(s); |
209 | } | |
065e2813 AL |
210 | } |
211 | ||
8b6b99b3 JQ |
212 | static ssize_t migrate_fd_put_buffer(void *opaque, const void *data, |
213 | size_t size) | |
065e2813 | 214 | { |
22f00a44 | 215 | MigrationState *s = opaque; |
065e2813 AL |
216 | ssize_t ret; |
217 | ||
fdbecb5d JQ |
218 | if (s->state != MIG_STATE_ACTIVE) { |
219 | return -EIO; | |
220 | } | |
221 | ||
065e2813 AL |
222 | do { |
223 | ret = s->write(s, data, size); | |
95b134ea | 224 | } while (ret == -1 && ((s->get_error(s)) == EINTR)); |
065e2813 AL |
225 | |
226 | if (ret == -1) | |
227 | ret = -(s->get_error(s)); | |
228 | ||
e447b1a6 | 229 | if (ret == -EAGAIN) { |
065e2813 | 230 | qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s); |
e447b1a6 | 231 | } |
065e2813 AL |
232 | |
233 | return ret; | |
234 | } | |
235 | ||
8b6b99b3 | 236 | static void migrate_fd_put_ready(void *opaque) |
065e2813 | 237 | { |
22f00a44 | 238 | MigrationState *s = opaque; |
065e2813 AL |
239 | int ret; |
240 | ||
065e2813 | 241 | if (s->state != MIG_STATE_ACTIVE) { |
d0f2c4c6 | 242 | DPRINTF("put_ready returning because of non-active state\n"); |
065e2813 AL |
243 | return; |
244 | } | |
245 | ||
d0f2c4c6 | 246 | DPRINTF("iterate\n"); |
539de124 | 247 | ret = qemu_savevm_state_iterate(s->file); |
39346385 JQ |
248 | if (ret < 0) { |
249 | migrate_fd_error(s); | |
250 | } else if (ret == 1) { | |
1354869c | 251 | int old_vm_running = runstate_is_running(); |
eeb34af9 | 252 | |
d0f2c4c6 | 253 | DPRINTF("done iterating\n"); |
8a9236f1 | 254 | vm_stop_force_state(RUN_STATE_FINISH_MIGRATE); |
065e2813 | 255 | |
539de124 | 256 | if (qemu_savevm_state_complete(s->file) < 0) { |
67afff79 | 257 | migrate_fd_error(s); |
b161d123 | 258 | } else { |
458cf28e | 259 | migrate_fd_completed(s); |
b161d123 | 260 | } |
48a2f4d6 | 261 | if (s->state != MIG_STATE_COMPLETED) { |
41ef56e6 AL |
262 | if (old_vm_running) { |
263 | vm_start(); | |
264 | } | |
f5bbfba1 | 265 | } |
065e2813 AL |
266 | } |
267 | } | |
268 | ||
0edda1c4 | 269 | static void migrate_fd_cancel(MigrationState *s) |
065e2813 | 270 | { |
065e2813 AL |
271 | if (s->state != MIG_STATE_ACTIVE) |
272 | return; | |
273 | ||
d0f2c4c6 | 274 | DPRINTF("cancelling migration\n"); |
065e2813 AL |
275 | |
276 | s->state = MIG_STATE_CANCELLED; | |
e0eb7390 | 277 | notifier_list_notify(&migration_state_notifiers, s); |
539de124 | 278 | qemu_savevm_state_cancel(s->file); |
065e2813 AL |
279 | |
280 | migrate_fd_cleanup(s); | |
281 | } | |
282 | ||
8b6b99b3 | 283 | static void migrate_fd_wait_for_unfreeze(void *opaque) |
065e2813 | 284 | { |
22f00a44 | 285 | MigrationState *s = opaque; |
065e2813 AL |
286 | int ret; |
287 | ||
d0f2c4c6 | 288 | DPRINTF("wait for unfreeze\n"); |
065e2813 AL |
289 | if (s->state != MIG_STATE_ACTIVE) |
290 | return; | |
291 | ||
292 | do { | |
293 | fd_set wfds; | |
294 | ||
295 | FD_ZERO(&wfds); | |
296 | FD_SET(s->fd, &wfds); | |
297 | ||
298 | ret = select(s->fd + 1, NULL, &wfds, NULL, NULL); | |
299 | } while (ret == -1 && (s->get_error(s)) == EINTR); | |
af509450 JQ |
300 | |
301 | if (ret == -1) { | |
dcd1d224 | 302 | qemu_file_set_error(s->file, -s->get_error(s)); |
af509450 | 303 | } |
065e2813 AL |
304 | } |
305 | ||
8b6b99b3 | 306 | static int migrate_fd_close(void *opaque) |
065e2813 | 307 | { |
22f00a44 | 308 | MigrationState *s = opaque; |
e19252d3 UL |
309 | |
310 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); | |
065e2813 AL |
311 | return s->close(s); |
312 | } | |
99a0db9b GH |
313 | |
314 | void add_migration_state_change_notifier(Notifier *notify) | |
315 | { | |
316 | notifier_list_add(&migration_state_notifiers, notify); | |
317 | } | |
318 | ||
319 | void remove_migration_state_change_notifier(Notifier *notify) | |
320 | { | |
31552529 | 321 | notifier_remove(notify); |
99a0db9b GH |
322 | } |
323 | ||
afe2df69 GH |
324 | bool migration_is_active(MigrationState *s) |
325 | { | |
326 | return s->state == MIG_STATE_ACTIVE; | |
327 | } | |
328 | ||
7073693b | 329 | bool migration_has_finished(MigrationState *s) |
99a0db9b | 330 | { |
7073693b | 331 | return s->state == MIG_STATE_COMPLETED; |
99a0db9b | 332 | } |
0edda1c4 | 333 | |
afe2df69 GH |
334 | bool migration_has_failed(MigrationState *s) |
335 | { | |
336 | return (s->state == MIG_STATE_CANCELLED || | |
337 | s->state == MIG_STATE_ERROR); | |
338 | } | |
339 | ||
8b6b99b3 | 340 | void migrate_fd_connect(MigrationState *s) |
99a0db9b | 341 | { |
8b6b99b3 JQ |
342 | int ret; |
343 | ||
d5934dde | 344 | s->state = MIG_STATE_ACTIVE; |
8b6b99b3 JQ |
345 | s->file = qemu_fopen_ops_buffered(s, |
346 | s->bandwidth_limit, | |
347 | migrate_fd_put_buffer, | |
348 | migrate_fd_put_ready, | |
349 | migrate_fd_wait_for_unfreeze, | |
350 | migrate_fd_close); | |
351 | ||
352 | DPRINTF("beginning savevm\n"); | |
539de124 | 353 | ret = qemu_savevm_state_begin(s->file, s->blk, s->shared); |
8b6b99b3 JQ |
354 | if (ret < 0) { |
355 | DPRINTF("failed, %d\n", ret); | |
356 | migrate_fd_error(s); | |
357 | return; | |
358 | } | |
359 | migrate_fd_put_ready(s); | |
360 | } | |
361 | ||
e1c37d0e | 362 | static MigrationState *migrate_init(int blk, int inc) |
0edda1c4 | 363 | { |
17549e84 | 364 | MigrationState *s = migrate_get_current(); |
d0ae46c1 | 365 | int64_t bandwidth_limit = s->bandwidth_limit; |
0edda1c4 | 366 | |
17549e84 | 367 | memset(s, 0, sizeof(*s)); |
d0ae46c1 | 368 | s->bandwidth_limit = bandwidth_limit; |
0edda1c4 JQ |
369 | s->blk = blk; |
370 | s->shared = inc; | |
1299c631 | 371 | |
0edda1c4 | 372 | s->bandwidth_limit = bandwidth_limit; |
d5934dde | 373 | s->state = MIG_STATE_SETUP; |
0edda1c4 | 374 | |
0edda1c4 JQ |
375 | return s; |
376 | } | |
cab30143 | 377 | |
fa2756b7 AL |
378 | static GSList *migration_blockers; |
379 | ||
380 | void migrate_add_blocker(Error *reason) | |
381 | { | |
382 | migration_blockers = g_slist_prepend(migration_blockers, reason); | |
383 | } | |
384 | ||
385 | void migrate_del_blocker(Error *reason) | |
386 | { | |
387 | migration_blockers = g_slist_remove(migration_blockers, reason); | |
388 | } | |
389 | ||
e1c37d0e LC |
390 | void qmp_migrate(const char *uri, bool has_blk, bool blk, |
391 | bool has_inc, bool inc, bool has_detach, bool detach, | |
392 | Error **errp) | |
cab30143 | 393 | { |
17549e84 | 394 | MigrationState *s = migrate_get_current(); |
cab30143 | 395 | const char *p; |
cab30143 JQ |
396 | int ret; |
397 | ||
17549e84 | 398 | if (s->state == MIG_STATE_ACTIVE) { |
e1c37d0e LC |
399 | error_set(errp, QERR_MIGRATION_ACTIVE); |
400 | return; | |
cab30143 JQ |
401 | } |
402 | ||
e1c37d0e LC |
403 | if (qemu_savevm_state_blocked(errp)) { |
404 | return; | |
cab30143 JQ |
405 | } |
406 | ||
fa2756b7 | 407 | if (migration_blockers) { |
e1c37d0e LC |
408 | *errp = error_copy(migration_blockers->data); |
409 | return; | |
fa2756b7 AL |
410 | } |
411 | ||
e1c37d0e | 412 | s = migrate_init(blk, inc); |
cab30143 JQ |
413 | |
414 | if (strstart(uri, "tcp:", &p)) { | |
415 | ret = tcp_start_outgoing_migration(s, p); | |
416 | #if !defined(WIN32) | |
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); | |
423 | #endif | |
99a0db9b | 424 | } else { |
e1c37d0e LC |
425 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol"); |
426 | return; | |
cab30143 JQ |
427 | } |
428 | ||
429 | if (ret < 0) { | |
e1c37d0e LC |
430 | DPRINTF("migration failed: %s\n", strerror(-ret)); |
431 | /* FIXME: we should return meaningful errors */ | |
432 | error_set(errp, QERR_UNDEFINED_ERROR); | |
433 | return; | |
1299c631 JQ |
434 | } |
435 | ||
e0eb7390 | 436 | notifier_list_notify(&migration_state_notifiers, s); |
cab30143 JQ |
437 | } |
438 | ||
6cdedb07 | 439 | void qmp_migrate_cancel(Error **errp) |
cab30143 | 440 | { |
17549e84 | 441 | migrate_fd_cancel(migrate_get_current()); |
cab30143 JQ |
442 | } |
443 | ||
3dc85383 | 444 | void qmp_migrate_set_speed(int64_t value, Error **errp) |
cab30143 | 445 | { |
cab30143 JQ |
446 | MigrationState *s; |
447 | ||
3dc85383 LC |
448 | if (value < 0) { |
449 | value = 0; | |
99a0db9b | 450 | } |
cab30143 | 451 | |
17549e84 | 452 | s = migrate_get_current(); |
3dc85383 | 453 | s->bandwidth_limit = value; |
d0ae46c1 | 454 | qemu_file_set_rate_limit(s->file, s->bandwidth_limit); |
cab30143 JQ |
455 | } |
456 | ||
4f0a993b | 457 | void qmp_migrate_set_downtime(double value, Error **errp) |
cab30143 | 458 | { |
4f0a993b LC |
459 | value *= 1e9; |
460 | value = MAX(0, MIN(UINT64_MAX, value)); | |
461 | max_downtime = (uint64_t)value; | |
99a0db9b | 462 | } |