]>
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 | ||
1393a485 | 16 | #include "qemu/osdep.h" |
f348b6d1 | 17 | #include "qemu/cutils.h" |
d49b6836 | 18 | #include "qemu/error-report.h" |
795c40b8 | 19 | #include "migration/blocker.h" |
f4dbe1bf | 20 | #include "exec.h" |
7fcac4a2 | 21 | #include "fd.h" |
61e8b148 | 22 | #include "socket.h" |
e1a3ecee | 23 | #include "rdma.h" |
7b1e1a22 | 24 | #include "ram.h" |
84a899de | 25 | #include "migration/global_state.h" |
c4b63b7c | 26 | #include "migration/misc.h" |
6666c96a | 27 | #include "migration.h" |
20a519a0 | 28 | #include "savevm.h" |
40014d81 | 29 | #include "qemu-file-channel.h" |
08a0aee1 | 30 | #include "qemu-file.h" |
987772d9 | 31 | #include "migration/vmstate.h" |
737e150e | 32 | #include "block/block.h" |
e688df6b | 33 | #include "qapi/error.h" |
9af23989 MA |
34 | #include "qapi/qapi-commands-migration.h" |
35 | #include "qapi/qapi-events-migration.h" | |
cc7a8ea7 | 36 | #include "qapi/qmp/qerror.h" |
15280c36 | 37 | #include "qapi/qmp/qnull.h" |
ab28bd23 | 38 | #include "qemu/rcu.h" |
2c9e6fec | 39 | #include "block.h" |
be07b0ac | 40 | #include "postcopy-ram.h" |
766bd176 | 41 | #include "qemu/thread.h" |
c09e5bb1 | 42 | #include "trace.h" |
51180423 | 43 | #include "exec/target_page.h" |
61b67d47 | 44 | #include "io/channel-buffer.h" |
35a6ed4f | 45 | #include "migration/colo.h" |
4ffdb337 | 46 | #include "hw/boards.h" |
9d18af93 | 47 | #include "monitor/monitor.h" |
065e2813 | 48 | |
dc325627 | 49 | #define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */ |
5bb7910a | 50 | |
5b4e1eb7 JQ |
51 | /* Amount of time to allocate to each "chunk" of bandwidth-throttled |
52 | * data. */ | |
53 | #define BUFFER_DELAY 100 | |
54 | #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY) | |
55 | ||
2ff30257 AA |
56 | /* Time in milliseconds we are allowed to stop the source, |
57 | * for sending the last part */ | |
58 | #define DEFAULT_MIGRATE_SET_DOWNTIME 300 | |
59 | ||
87c9cc1c DHB |
60 | /* Maximum migrate downtime set to 2000 seconds */ |
61 | #define MAX_MIGRATE_DOWNTIME_SECONDS 2000 | |
62 | #define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000) | |
63 | ||
8706d2d5 LL |
64 | /* Default compression thread count */ |
65 | #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8 | |
3fcb38c2 LL |
66 | /* Default decompression thread count, usually decompression is at |
67 | * least 4 times as fast as compression.*/ | |
68 | #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2 | |
8706d2d5 LL |
69 | /*0: means nocompress, 1: best speed, ... 9: best compress ratio */ |
70 | #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1 | |
1626fee3 | 71 | /* Define default autoconverge cpu throttle migration parameters */ |
d85a31d1 JH |
72 | #define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20 |
73 | #define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10 | |
4cbc9c7f | 74 | #define DEFAULT_MIGRATE_MAX_CPU_THROTTLE 99 |
8706d2d5 | 75 | |
17ad9b35 | 76 | /* Migration XBZRLE default cache size */ |
73af8dd8 | 77 | #define DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE (64 * 1024 * 1024) |
17ad9b35 | 78 | |
68b53591 HZ |
79 | /* The delay time (in ms) between two COLO checkpoints |
80 | * Note: Please change this default value to 10000 when we support hybrid mode. | |
81 | */ | |
82 | #define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY 200 | |
4075fb1c | 83 | #define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2 |
0fb86605 | 84 | #define DEFAULT_MIGRATE_MULTIFD_PAGE_COUNT 16 |
68b53591 | 85 | |
7e555c6c DDAG |
86 | /* Background transfer rate for postcopy, 0 means unlimited, note |
87 | * that page requests can still exceed this limit. | |
88 | */ | |
89 | #define DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH 0 | |
90 | ||
99a0db9b GH |
91 | static NotifierList migration_state_notifiers = |
92 | NOTIFIER_LIST_INITIALIZER(migration_state_notifiers); | |
93 | ||
adde220a DDAG |
94 | static bool deferred_incoming; |
95 | ||
da6f1790 JQ |
96 | /* Messages sent on the return path from destination to source */ |
97 | enum mig_rp_message_type { | |
98 | MIG_RP_MSG_INVALID = 0, /* Must be 0 */ | |
99 | MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */ | |
100 | MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */ | |
101 | ||
102 | MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */ | |
103 | MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */ | |
a335debb | 104 | MIG_RP_MSG_RECV_BITMAP, /* send recved_bitmap back to source */ |
13955b89 | 105 | MIG_RP_MSG_RESUME_ACK, /* tell source that we are ready to resume */ |
da6f1790 JQ |
106 | |
107 | MIG_RP_MSG_MAX | |
108 | }; | |
109 | ||
17549e84 JQ |
110 | /* When we add fault tolerance, we could have several |
111 | migrations at once. For now we don't need to add | |
112 | dynamic creation of migration */ | |
113 | ||
e5cb7e76 | 114 | static MigrationState *current_migration; |
e1b1b1bc | 115 | static MigrationIncomingState *current_incoming; |
e5cb7e76 | 116 | |
8b0b29dc | 117 | static bool migration_object_check(MigrationState *ms, Error **errp); |
0331c8ca DDAG |
118 | static int migration_maybe_pause(MigrationState *s, |
119 | int *current_active_state, | |
120 | int new_state); | |
8b0b29dc | 121 | |
e5cb7e76 PX |
122 | void migration_object_init(void) |
123 | { | |
4ffdb337 | 124 | MachineState *ms = MACHINE(qdev_get_machine()); |
8b0b29dc | 125 | Error *err = NULL; |
4ffdb337 | 126 | |
e5cb7e76 PX |
127 | /* This can only be called once. */ |
128 | assert(!current_migration); | |
129 | current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION)); | |
4ffdb337 | 130 | |
e1b1b1bc PX |
131 | /* |
132 | * Init the migrate incoming object as well no matter whether | |
133 | * we'll use it or not. | |
134 | */ | |
135 | assert(!current_incoming); | |
136 | current_incoming = g_new0(MigrationIncomingState, 1); | |
137 | current_incoming->state = MIGRATION_STATUS_NONE; | |
138 | current_incoming->postcopy_remote_fds = | |
139 | g_array_new(FALSE, TRUE, sizeof(struct PostCopyFD)); | |
140 | qemu_mutex_init(¤t_incoming->rp_mutex); | |
141 | qemu_event_init(¤t_incoming->main_thread_load_event, false); | |
142 | qemu_sem_init(¤t_incoming->postcopy_pause_sem_dst, 0); | |
143 | qemu_sem_init(¤t_incoming->postcopy_pause_sem_fault, 0); | |
144 | ||
145 | init_dirty_bitmap_incoming_migration(); | |
146 | ||
8b0b29dc PX |
147 | if (!migration_object_check(current_migration, &err)) { |
148 | error_report_err(err); | |
149 | exit(1); | |
150 | } | |
151 | ||
4ffdb337 PX |
152 | /* |
153 | * We cannot really do this in migration_instance_init() since at | |
154 | * that time global properties are not yet applied, then this | |
155 | * value will be definitely replaced by something else. | |
156 | */ | |
157 | if (ms->enforce_config_section) { | |
158 | current_migration->send_configuration = true; | |
159 | } | |
e5cb7e76 PX |
160 | } |
161 | ||
1f895604 VSO |
162 | void migration_object_finalize(void) |
163 | { | |
164 | object_unref(OBJECT(current_migration)); | |
165 | } | |
166 | ||
bca7856a | 167 | /* For outgoing */ |
859bc756 | 168 | MigrationState *migrate_get_current(void) |
17549e84 | 169 | { |
e5cb7e76 PX |
170 | /* This can only be called after the object created. */ |
171 | assert(current_migration); | |
172 | return current_migration; | |
17549e84 JQ |
173 | } |
174 | ||
bca7856a DDAG |
175 | MigrationIncomingState *migration_incoming_get_current(void) |
176 | { | |
e1b1b1bc PX |
177 | assert(current_incoming); |
178 | return current_incoming; | |
bca7856a DDAG |
179 | } |
180 | ||
181 | void migration_incoming_state_destroy(void) | |
182 | { | |
b4b076da JQ |
183 | struct MigrationIncomingState *mis = migration_incoming_get_current(); |
184 | ||
3482655b | 185 | if (mis->to_src_file) { |
660819b1 PX |
186 | /* Tell source that we are done */ |
187 | migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0); | |
3482655b PX |
188 | qemu_fclose(mis->to_src_file); |
189 | mis->to_src_file = NULL; | |
190 | } | |
191 | ||
660819b1 PX |
192 | if (mis->from_src_file) { |
193 | qemu_fclose(mis->from_src_file); | |
194 | mis->from_src_file = NULL; | |
195 | } | |
00fa4fc8 DDAG |
196 | if (mis->postcopy_remote_fds) { |
197 | g_array_free(mis->postcopy_remote_fds, TRUE); | |
198 | mis->postcopy_remote_fds = NULL; | |
199 | } | |
660819b1 | 200 | |
5089e186 | 201 | qemu_event_reset(&mis->main_thread_load_event); |
bca7856a DDAG |
202 | } |
203 | ||
b05dc723 JQ |
204 | static void migrate_generate_event(int new_state) |
205 | { | |
206 | if (migrate_use_events()) { | |
3ab72385 | 207 | qapi_event_send_migration(new_state); |
b05dc723 JQ |
208 | } |
209 | } | |
210 | ||
0f073f44 DDAG |
211 | static bool migrate_late_block_activate(void) |
212 | { | |
213 | MigrationState *s; | |
214 | ||
215 | s = migrate_get_current(); | |
216 | ||
217 | return s->enabled_capabilities[ | |
218 | MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE]; | |
219 | } | |
220 | ||
adde220a DDAG |
221 | /* |
222 | * Called on -incoming with a defer: uri. | |
223 | * The migration can be started later after any parameters have been | |
224 | * changed. | |
225 | */ | |
226 | static void deferred_incoming_migration(Error **errp) | |
227 | { | |
228 | if (deferred_incoming) { | |
229 | error_setg(errp, "Incoming migration already deferred"); | |
230 | } | |
231 | deferred_incoming = true; | |
232 | } | |
233 | ||
da6f1790 JQ |
234 | /* |
235 | * Send a message on the return channel back to the source | |
236 | * of the migration. | |
237 | */ | |
d6208e35 PX |
238 | static int migrate_send_rp_message(MigrationIncomingState *mis, |
239 | enum mig_rp_message_type message_type, | |
240 | uint16_t len, void *data) | |
da6f1790 | 241 | { |
d6208e35 PX |
242 | int ret = 0; |
243 | ||
da6f1790 JQ |
244 | trace_migrate_send_rp_message((int)message_type, len); |
245 | qemu_mutex_lock(&mis->rp_mutex); | |
d6208e35 PX |
246 | |
247 | /* | |
248 | * It's possible that the file handle got lost due to network | |
249 | * failures. | |
250 | */ | |
251 | if (!mis->to_src_file) { | |
252 | ret = -EIO; | |
253 | goto error; | |
254 | } | |
255 | ||
da6f1790 JQ |
256 | qemu_put_be16(mis->to_src_file, (unsigned int)message_type); |
257 | qemu_put_be16(mis->to_src_file, len); | |
258 | qemu_put_buffer(mis->to_src_file, data, len); | |
259 | qemu_fflush(mis->to_src_file); | |
d6208e35 PX |
260 | |
261 | /* It's possible that qemu file got error during sending */ | |
262 | ret = qemu_file_get_error(mis->to_src_file); | |
263 | ||
264 | error: | |
da6f1790 | 265 | qemu_mutex_unlock(&mis->rp_mutex); |
d6208e35 | 266 | return ret; |
da6f1790 JQ |
267 | } |
268 | ||
1e2d90eb DDAG |
269 | /* Request a range of pages from the source VM at the given |
270 | * start address. | |
271 | * rbname: Name of the RAMBlock to request the page in, if NULL it's the same | |
272 | * as the last request (a name must have been given previously) | |
273 | * Start: Address offset within the RB | |
274 | * Len: Length in bytes required - must be a multiple of pagesize | |
275 | */ | |
d6208e35 PX |
276 | int migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname, |
277 | ram_addr_t start, size_t len) | |
1e2d90eb | 278 | { |
cb8d4c8f | 279 | uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */ |
1e2d90eb | 280 | size_t msglen = 12; /* start + len */ |
d6208e35 | 281 | enum mig_rp_message_type msg_type; |
1e2d90eb DDAG |
282 | |
283 | *(uint64_t *)bufc = cpu_to_be64((uint64_t)start); | |
284 | *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len); | |
285 | ||
286 | if (rbname) { | |
287 | int rbname_len = strlen(rbname); | |
288 | assert(rbname_len < 256); | |
289 | ||
290 | bufc[msglen++] = rbname_len; | |
291 | memcpy(bufc + msglen, rbname, rbname_len); | |
292 | msglen += rbname_len; | |
d6208e35 | 293 | msg_type = MIG_RP_MSG_REQ_PAGES_ID; |
1e2d90eb | 294 | } else { |
d6208e35 | 295 | msg_type = MIG_RP_MSG_REQ_PAGES; |
1e2d90eb | 296 | } |
d6208e35 PX |
297 | |
298 | return migrate_send_rp_message(mis, msg_type, msglen, bufc); | |
1e2d90eb DDAG |
299 | } |
300 | ||
43eaae28 | 301 | void qemu_start_incoming_migration(const char *uri, Error **errp) |
5bb7910a | 302 | { |
34c9dd8e AL |
303 | const char *p; |
304 | ||
3ab72385 | 305 | qapi_event_send_migration(MIGRATION_STATUS_SETUP); |
adde220a DDAG |
306 | if (!strcmp(uri, "defer")) { |
307 | deferred_incoming_migration(errp); | |
308 | } else if (strstart(uri, "tcp:", &p)) { | |
43eaae28 | 309 | tcp_start_incoming_migration(p, errp); |
2da776db | 310 | #ifdef CONFIG_RDMA |
adde220a | 311 | } else if (strstart(uri, "rdma:", &p)) { |
2da776db MH |
312 | rdma_start_incoming_migration(p, errp); |
313 | #endif | |
adde220a | 314 | } else if (strstart(uri, "exec:", &p)) { |
43eaae28 | 315 | exec_start_incoming_migration(p, errp); |
adde220a | 316 | } else if (strstart(uri, "unix:", &p)) { |
43eaae28 | 317 | unix_start_incoming_migration(p, errp); |
adde220a | 318 | } else if (strstart(uri, "fd:", &p)) { |
43eaae28 | 319 | fd_start_incoming_migration(p, errp); |
adde220a | 320 | } else { |
312fd5f2 | 321 | error_setg(errp, "unknown migration protocol: %s", uri); |
8ca5e801 | 322 | } |
5bb7910a AL |
323 | } |
324 | ||
0aa6aefc DL |
325 | static void process_incoming_migration_bh(void *opaque) |
326 | { | |
327 | Error *local_err = NULL; | |
328 | MigrationIncomingState *mis = opaque; | |
329 | ||
0f073f44 DDAG |
330 | /* If capability late_block_activate is set: |
331 | * Only fire up the block code now if we're going to restart the | |
332 | * VM, else 'cont' will do it. | |
333 | * This causes file locking to happen; so we don't want it to happen | |
334 | * unless we really are starting the VM. | |
335 | */ | |
336 | if (!migrate_late_block_activate() || | |
337 | (autostart && (!global_state_received() || | |
338 | global_state_get_runstate() == RUN_STATE_RUNNING))) { | |
339 | /* Make sure all file formats flush their mutable metadata. | |
340 | * If we get an error here, just don't restart the VM yet. */ | |
341 | bdrv_invalidate_cache_all(&local_err); | |
342 | if (local_err) { | |
343 | error_report_err(local_err); | |
344 | local_err = NULL; | |
345 | autostart = false; | |
346 | } | |
d35ff5e6 KW |
347 | } |
348 | ||
0aa6aefc DL |
349 | /* |
350 | * This must happen after all error conditions are dealt with and | |
351 | * we're sure the VM is going to be running on this host. | |
352 | */ | |
353 | qemu_announce_self(); | |
354 | ||
f986c3d2 JQ |
355 | if (multifd_load_cleanup(&local_err) != 0) { |
356 | error_report_err(local_err); | |
357 | autostart = false; | |
358 | } | |
0aa6aefc DL |
359 | /* If global state section was not received or we are in running |
360 | state, we need to obey autostart. Any other state is set with | |
361 | runstate_set. */ | |
362 | ||
b35ebdf0 VSO |
363 | dirty_bitmap_mig_before_vm_start(); |
364 | ||
0aa6aefc DL |
365 | if (!global_state_received() || |
366 | global_state_get_runstate() == RUN_STATE_RUNNING) { | |
367 | if (autostart) { | |
368 | vm_start(); | |
369 | } else { | |
370 | runstate_set(RUN_STATE_PAUSED); | |
371 | } | |
372 | } else { | |
373 | runstate_set(global_state_get_runstate()); | |
374 | } | |
0aa6aefc DL |
375 | /* |
376 | * This must happen after any state changes since as soon as an external | |
377 | * observer sees this event they might start to prod at the VM assuming | |
378 | * it's ready to use. | |
379 | */ | |
380 | migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE, | |
381 | MIGRATION_STATUS_COMPLETED); | |
382 | qemu_bh_delete(mis->bh); | |
383 | migration_incoming_state_destroy(); | |
384 | } | |
385 | ||
82a4da79 | 386 | static void process_incoming_migration_co(void *opaque) |
511c0231 | 387 | { |
b4b076da | 388 | MigrationIncomingState *mis = migration_incoming_get_current(); |
e9bef235 | 389 | PostcopyState ps; |
1c12e1f5 PB |
390 | int ret; |
391 | ||
4f0fae7f | 392 | assert(mis->from_src_file); |
92370989 | 393 | mis->migration_incoming_co = qemu_coroutine_self(); |
67f11b5c | 394 | mis->largest_page_size = qemu_ram_pagesize_largest(); |
093e3c42 | 395 | postcopy_state_set(POSTCOPY_INCOMING_NONE); |
93d7af6f HZ |
396 | migrate_set_state(&mis->state, MIGRATION_STATUS_NONE, |
397 | MIGRATION_STATUS_ACTIVE); | |
4f0fae7f | 398 | ret = qemu_loadvm_state(mis->from_src_file); |
bca7856a | 399 | |
e9bef235 DDAG |
400 | ps = postcopy_state_get(); |
401 | trace_process_incoming_migration_co_end(ret, ps); | |
402 | if (ps != POSTCOPY_INCOMING_NONE) { | |
403 | if (ps == POSTCOPY_INCOMING_ADVISE) { | |
404 | /* | |
405 | * Where a migration had postcopy enabled (and thus went to advise) | |
406 | * but managed to complete within the precopy period, we can use | |
407 | * the normal exit. | |
408 | */ | |
409 | postcopy_ram_incoming_cleanup(mis); | |
410 | } else if (ret >= 0) { | |
411 | /* | |
412 | * Postcopy was started, cleanup should happen at the end of the | |
413 | * postcopy thread. | |
414 | */ | |
415 | trace_process_incoming_migration_co_postcopy_end_main(); | |
416 | return; | |
417 | } | |
418 | /* Else if something went wrong then just fall out of the normal exit */ | |
419 | } | |
420 | ||
25d0c16f HZ |
421 | /* we get COLO info, and know if we are in COLO mode */ |
422 | if (!ret && migration_incoming_enable_colo()) { | |
25d0c16f HZ |
423 | qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming", |
424 | colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE); | |
425 | mis->have_colo_incoming_thread = true; | |
426 | qemu_coroutine_yield(); | |
427 | ||
428 | /* Wait checkpoint incoming thread exit before free resource */ | |
429 | qemu_thread_join(&mis->colo_incoming_thread); | |
430 | } | |
431 | ||
1c12e1f5 | 432 | if (ret < 0) { |
f986c3d2 JQ |
433 | Error *local_err = NULL; |
434 | ||
93d7af6f HZ |
435 | migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE, |
436 | MIGRATION_STATUS_FAILED); | |
db80face | 437 | error_report("load of migration failed: %s", strerror(-ret)); |
3a0f2cea | 438 | qemu_fclose(mis->from_src_file); |
f986c3d2 JQ |
439 | if (multifd_load_cleanup(&local_err) != 0) { |
440 | error_report_err(local_err); | |
441 | } | |
4aead692 | 442 | exit(EXIT_FAILURE); |
511c0231 | 443 | } |
0aa6aefc DL |
444 | mis->bh = qemu_bh_new(process_incoming_migration_bh, mis); |
445 | qemu_bh_schedule(mis->bh); | |
92370989 | 446 | mis->migration_incoming_co = NULL; |
511c0231 JQ |
447 | } |
448 | ||
e595a01a | 449 | static void migration_incoming_setup(QEMUFile *f) |
82a4da79 | 450 | { |
4f0fae7f | 451 | MigrationIncomingState *mis = migration_incoming_get_current(); |
82a4da79 | 452 | |
f986c3d2 JQ |
453 | if (multifd_load_setup() != 0) { |
454 | /* We haven't been able to create multifd threads | |
455 | nothing better to do */ | |
456 | exit(EXIT_FAILURE); | |
457 | } | |
458 | ||
4f0fae7f JQ |
459 | if (!mis->from_src_file) { |
460 | mis->from_src_file = f; | |
461 | } | |
06ad5135 | 462 | qemu_file_set_blocking(f, false); |
e595a01a JQ |
463 | } |
464 | ||
36c2f8be | 465 | void migration_incoming_process(void) |
e595a01a JQ |
466 | { |
467 | Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL); | |
0b8b8753 | 468 | qemu_coroutine_enter(co); |
82a4da79 PB |
469 | } |
470 | ||
884835fa PX |
471 | /* Returns true if recovered from a paused migration, otherwise false */ |
472 | static bool postcopy_try_recover(QEMUFile *f) | |
e595a01a | 473 | { |
d96c9e8d PX |
474 | MigrationIncomingState *mis = migration_incoming_get_current(); |
475 | ||
476 | if (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) { | |
477 | /* Resumed from a paused postcopy migration */ | |
478 | ||
479 | mis->from_src_file = f; | |
480 | /* Postcopy has standalone thread to do vm load */ | |
481 | qemu_file_set_blocking(f, true); | |
482 | ||
483 | /* Re-configure the return path */ | |
484 | mis->to_src_file = qemu_file_get_return_path(f); | |
485 | ||
486 | migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_PAUSED, | |
487 | MIGRATION_STATUS_POSTCOPY_RECOVER); | |
488 | ||
489 | /* | |
490 | * Here, we only wake up the main loading thread (while the | |
491 | * fault thread will still be waiting), so that we can receive | |
492 | * commands from source now, and answer it if needed. The | |
493 | * fault thread will be woken up afterwards until we are sure | |
494 | * that source is ready to reply to page requests. | |
495 | */ | |
496 | qemu_sem_post(&mis->postcopy_pause_sem_dst); | |
884835fa PX |
497 | return true; |
498 | } | |
499 | ||
500 | return false; | |
501 | } | |
502 | ||
503 | void migration_fd_process_incoming(QEMUFile *f) | |
504 | { | |
505 | if (postcopy_try_recover(f)) { | |
506 | return; | |
d96c9e8d | 507 | } |
884835fa PX |
508 | |
509 | migration_incoming_setup(f); | |
510 | migration_incoming_process(); | |
e595a01a JQ |
511 | } |
512 | ||
4f0fae7f JQ |
513 | void migration_ioc_process_incoming(QIOChannel *ioc) |
514 | { | |
515 | MigrationIncomingState *mis = migration_incoming_get_current(); | |
a429e7f4 | 516 | bool start_migration; |
4f0fae7f JQ |
517 | |
518 | if (!mis->from_src_file) { | |
a429e7f4 | 519 | /* The first connection (multifd may have multiple) */ |
4f0fae7f | 520 | QEMUFile *f = qemu_fopen_channel_input(ioc); |
a429e7f4 PX |
521 | |
522 | /* If it's a recovery, we're done */ | |
884835fa PX |
523 | if (postcopy_try_recover(f)) { |
524 | return; | |
525 | } | |
a429e7f4 | 526 | |
36c2f8be | 527 | migration_incoming_setup(f); |
a429e7f4 PX |
528 | |
529 | /* | |
530 | * Common migration only needs one channel, so we can start | |
531 | * right now. Multifd needs more than one channel, we wait. | |
532 | */ | |
533 | start_migration = !migrate_use_multifd(); | |
534 | } else { | |
535 | /* Multiple connections */ | |
536 | assert(migrate_use_multifd()); | |
537 | start_migration = multifd_recv_new_channel(ioc); | |
4f0fae7f | 538 | } |
81e62053 | 539 | |
a429e7f4 | 540 | if (start_migration) { |
81e62053 PX |
541 | migration_incoming_process(); |
542 | } | |
4f0fae7f JQ |
543 | } |
544 | ||
428d8908 JQ |
545 | /** |
546 | * @migration_has_all_channels: We have received all channels that we need | |
547 | * | |
548 | * Returns true when we have got connections to all the channels that | |
549 | * we need for migration. | |
550 | */ | |
551 | bool migration_has_all_channels(void) | |
552 | { | |
ca273df3 | 553 | MigrationIncomingState *mis = migration_incoming_get_current(); |
62c1e0ca JQ |
554 | bool all_channels; |
555 | ||
556 | all_channels = multifd_recv_all_channels_created(); | |
557 | ||
ca273df3 | 558 | return all_channels && mis->from_src_file != NULL; |
428d8908 JQ |
559 | } |
560 | ||
6decec93 DDAG |
561 | /* |
562 | * Send a 'SHUT' message on the return channel with the given value | |
563 | * to indicate that we've finished with the RP. Non-0 value indicates | |
564 | * error. | |
565 | */ | |
566 | void migrate_send_rp_shut(MigrationIncomingState *mis, | |
567 | uint32_t value) | |
568 | { | |
569 | uint32_t buf; | |
570 | ||
571 | buf = cpu_to_be32(value); | |
572 | migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf); | |
573 | } | |
574 | ||
575 | /* | |
576 | * Send a 'PONG' message on the return channel with the given value | |
577 | * (normally in response to a 'PING') | |
578 | */ | |
579 | void migrate_send_rp_pong(MigrationIncomingState *mis, | |
580 | uint32_t value) | |
581 | { | |
582 | uint32_t buf; | |
583 | ||
584 | buf = cpu_to_be32(value); | |
585 | migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf); | |
586 | } | |
587 | ||
a335debb PX |
588 | void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis, |
589 | char *block_name) | |
590 | { | |
591 | char buf[512]; | |
592 | int len; | |
593 | int64_t res; | |
594 | ||
595 | /* | |
596 | * First, we send the header part. It contains only the len of | |
597 | * idstr, and the idstr itself. | |
598 | */ | |
599 | len = strlen(block_name); | |
600 | buf[0] = len; | |
601 | memcpy(buf + 1, block_name, len); | |
602 | ||
603 | if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) { | |
604 | error_report("%s: MSG_RP_RECV_BITMAP only used for recovery", | |
605 | __func__); | |
606 | return; | |
607 | } | |
608 | ||
609 | migrate_send_rp_message(mis, MIG_RP_MSG_RECV_BITMAP, len + 1, buf); | |
610 | ||
611 | /* | |
612 | * Next, we dump the received bitmap to the stream. | |
613 | * | |
614 | * TODO: currently we are safe since we are the only one that is | |
615 | * using the to_src_file handle (fault thread is still paused), | |
616 | * and it's ok even not taking the mutex. However the best way is | |
617 | * to take the lock before sending the message header, and release | |
618 | * the lock after sending the bitmap. | |
619 | */ | |
620 | qemu_mutex_lock(&mis->rp_mutex); | |
621 | res = ramblock_recv_bitmap_send(mis->to_src_file, block_name); | |
622 | qemu_mutex_unlock(&mis->rp_mutex); | |
623 | ||
624 | trace_migrate_send_rp_recv_bitmap(block_name, res); | |
625 | } | |
626 | ||
13955b89 PX |
627 | void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value) |
628 | { | |
629 | uint32_t buf; | |
630 | ||
631 | buf = cpu_to_be32(value); | |
632 | migrate_send_rp_message(mis, MIG_RP_MSG_RESUME_ACK, sizeof(buf), &buf); | |
633 | } | |
634 | ||
bbf6da32 OW |
635 | MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp) |
636 | { | |
637 | MigrationCapabilityStatusList *head = NULL; | |
638 | MigrationCapabilityStatusList *caps; | |
639 | MigrationState *s = migrate_get_current(); | |
640 | int i; | |
641 | ||
387eedeb | 642 | caps = NULL; /* silence compiler warning */ |
7fb1cf16 | 643 | for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) { |
ed1701c6 DDAG |
644 | #ifndef CONFIG_LIVE_BLOCK_MIGRATION |
645 | if (i == MIGRATION_CAPABILITY_BLOCK) { | |
646 | continue; | |
647 | } | |
648 | #endif | |
bbf6da32 OW |
649 | if (head == NULL) { |
650 | head = g_malloc0(sizeof(*caps)); | |
651 | caps = head; | |
652 | } else { | |
653 | caps->next = g_malloc0(sizeof(*caps)); | |
654 | caps = caps->next; | |
655 | } | |
656 | caps->value = | |
657 | g_malloc(sizeof(*caps->value)); | |
658 | caps->value->capability = i; | |
659 | caps->value->state = s->enabled_capabilities[i]; | |
660 | } | |
661 | ||
662 | return head; | |
663 | } | |
664 | ||
85de8323 LL |
665 | MigrationParameters *qmp_query_migrate_parameters(Error **errp) |
666 | { | |
667 | MigrationParameters *params; | |
668 | MigrationState *s = migrate_get_current(); | |
669 | ||
e87fae4c | 670 | /* TODO use QAPI_CLONE() instead of duplicating it inline */ |
85de8323 | 671 | params = g_malloc0(sizeof(*params)); |
de63ab61 | 672 | params->has_compress_level = true; |
2594f56d | 673 | params->compress_level = s->parameters.compress_level; |
de63ab61 | 674 | params->has_compress_threads = true; |
2594f56d | 675 | params->compress_threads = s->parameters.compress_threads; |
1d58872a XG |
676 | params->has_compress_wait_thread = true; |
677 | params->compress_wait_thread = s->parameters.compress_wait_thread; | |
de63ab61 | 678 | params->has_decompress_threads = true; |
2594f56d | 679 | params->decompress_threads = s->parameters.decompress_threads; |
de63ab61 | 680 | params->has_cpu_throttle_initial = true; |
2594f56d | 681 | params->cpu_throttle_initial = s->parameters.cpu_throttle_initial; |
de63ab61 | 682 | params->has_cpu_throttle_increment = true; |
2594f56d | 683 | params->cpu_throttle_increment = s->parameters.cpu_throttle_increment; |
8cc99dcd | 684 | params->has_tls_creds = true; |
69ef1f36 | 685 | params->tls_creds = g_strdup(s->parameters.tls_creds); |
8cc99dcd | 686 | params->has_tls_hostname = true; |
69ef1f36 | 687 | params->tls_hostname = g_strdup(s->parameters.tls_hostname); |
2ff30257 AA |
688 | params->has_max_bandwidth = true; |
689 | params->max_bandwidth = s->parameters.max_bandwidth; | |
690 | params->has_downtime_limit = true; | |
691 | params->downtime_limit = s->parameters.downtime_limit; | |
fe39a4d4 | 692 | params->has_x_checkpoint_delay = true; |
68b53591 | 693 | params->x_checkpoint_delay = s->parameters.x_checkpoint_delay; |
2833c59b JQ |
694 | params->has_block_incremental = true; |
695 | params->block_incremental = s->parameters.block_incremental; | |
4075fb1c JQ |
696 | params->has_x_multifd_channels = true; |
697 | params->x_multifd_channels = s->parameters.x_multifd_channels; | |
0fb86605 JQ |
698 | params->has_x_multifd_page_count = true; |
699 | params->x_multifd_page_count = s->parameters.x_multifd_page_count; | |
73af8dd8 JQ |
700 | params->has_xbzrle_cache_size = true; |
701 | params->xbzrle_cache_size = s->parameters.xbzrle_cache_size; | |
7e555c6c DDAG |
702 | params->has_max_postcopy_bandwidth = true; |
703 | params->max_postcopy_bandwidth = s->parameters.max_postcopy_bandwidth; | |
4cbc9c7f LQ |
704 | params->has_max_cpu_throttle = true; |
705 | params->max_cpu_throttle = s->parameters.max_cpu_throttle; | |
85de8323 LL |
706 | |
707 | return params; | |
708 | } | |
709 | ||
f6844b99 DDAG |
710 | /* |
711 | * Return true if we're already in the middle of a migration | |
712 | * (i.e. any of the active or setup states) | |
713 | */ | |
714 | static bool migration_is_setup_or_active(int state) | |
715 | { | |
716 | switch (state) { | |
717 | case MIGRATION_STATUS_ACTIVE: | |
9ec055ae | 718 | case MIGRATION_STATUS_POSTCOPY_ACTIVE: |
a688d2c1 | 719 | case MIGRATION_STATUS_POSTCOPY_PAUSED: |
135b87b4 | 720 | case MIGRATION_STATUS_POSTCOPY_RECOVER: |
f6844b99 | 721 | case MIGRATION_STATUS_SETUP: |
31e06077 DDAG |
722 | case MIGRATION_STATUS_PRE_SWITCHOVER: |
723 | case MIGRATION_STATUS_DEVICE: | |
f6844b99 DDAG |
724 | return true; |
725 | ||
726 | default: | |
727 | return false; | |
728 | ||
729 | } | |
730 | } | |
731 | ||
a22463a5 DDAG |
732 | static void populate_ram_info(MigrationInfo *info, MigrationState *s) |
733 | { | |
734 | info->has_ram = true; | |
735 | info->ram = g_malloc0(sizeof(*info->ram)); | |
9360447d | 736 | info->ram->transferred = ram_counters.transferred; |
a22463a5 | 737 | info->ram->total = ram_bytes_total(); |
9360447d | 738 | info->ram->duplicate = ram_counters.duplicate; |
bedf53c1 JQ |
739 | /* legacy value. It is not used anymore */ |
740 | info->ram->skipped = 0; | |
9360447d JQ |
741 | info->ram->normal = ram_counters.normal; |
742 | info->ram->normal_bytes = ram_counters.normal * | |
20afaed9 | 743 | qemu_target_page_size(); |
a22463a5 | 744 | info->ram->mbps = s->mbps; |
9360447d JQ |
745 | info->ram->dirty_sync_count = ram_counters.dirty_sync_count; |
746 | info->ram->postcopy_requests = ram_counters.postcopy_requests; | |
030ce1f8 | 747 | info->ram->page_size = qemu_target_page_size(); |
a61c45bd | 748 | info->ram->multifd_bytes = ram_counters.multifd_bytes; |
a22463a5 | 749 | |
114f5aee JQ |
750 | if (migrate_use_xbzrle()) { |
751 | info->has_xbzrle_cache = true; | |
752 | info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache)); | |
753 | info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size(); | |
9360447d JQ |
754 | info->xbzrle_cache->bytes = xbzrle_counters.bytes; |
755 | info->xbzrle_cache->pages = xbzrle_counters.pages; | |
756 | info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss; | |
757 | info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate; | |
758 | info->xbzrle_cache->overflow = xbzrle_counters.overflow; | |
114f5aee JQ |
759 | } |
760 | ||
76e03000 XG |
761 | if (migrate_use_compression()) { |
762 | info->has_compression = true; | |
763 | info->compression = g_malloc0(sizeof(*info->compression)); | |
764 | info->compression->pages = compression_counters.pages; | |
765 | info->compression->busy = compression_counters.busy; | |
766 | info->compression->busy_rate = compression_counters.busy_rate; | |
767 | info->compression->compressed_size = | |
768 | compression_counters.compressed_size; | |
769 | info->compression->compression_rate = | |
770 | compression_counters.compression_rate; | |
771 | } | |
772 | ||
338182c8 JQ |
773 | if (cpu_throttle_active()) { |
774 | info->has_cpu_throttle_percentage = true; | |
775 | info->cpu_throttle_percentage = cpu_throttle_get_percentage(); | |
776 | } | |
777 | ||
a22463a5 DDAG |
778 | if (s->state != MIGRATION_STATUS_COMPLETED) { |
779 | info->ram->remaining = ram_bytes_remaining(); | |
9360447d | 780 | info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate; |
a22463a5 DDAG |
781 | } |
782 | } | |
783 | ||
930ac04c JQ |
784 | static void populate_disk_info(MigrationInfo *info) |
785 | { | |
786 | if (blk_mig_active()) { | |
787 | info->has_disk = true; | |
788 | info->disk = g_malloc0(sizeof(*info->disk)); | |
789 | info->disk->transferred = blk_mig_bytes_transferred(); | |
790 | info->disk->remaining = blk_mig_bytes_remaining(); | |
791 | info->disk->total = blk_mig_bytes_total(); | |
792 | } | |
793 | } | |
794 | ||
65ace060 | 795 | static void fill_source_migration_info(MigrationInfo *info) |
5bb7910a | 796 | { |
17549e84 JQ |
797 | MigrationState *s = migrate_get_current(); |
798 | ||
799 | switch (s->state) { | |
31194731 | 800 | case MIGRATION_STATUS_NONE: |
17549e84 | 801 | /* no migration has happened ever */ |
65ace060 AP |
802 | /* do not overwrite destination migration status */ |
803 | return; | |
17549e84 | 804 | break; |
31194731 | 805 | case MIGRATION_STATUS_SETUP: |
29ae8a41 | 806 | info->has_status = true; |
ed4fbd10 | 807 | info->has_total_time = false; |
29ae8a41 | 808 | break; |
31194731 HZ |
809 | case MIGRATION_STATUS_ACTIVE: |
810 | case MIGRATION_STATUS_CANCELLING: | |
9ec055ae | 811 | case MIGRATION_STATUS_POSTCOPY_ACTIVE: |
31e06077 DDAG |
812 | case MIGRATION_STATUS_PRE_SWITCHOVER: |
813 | case MIGRATION_STATUS_DEVICE: | |
a688d2c1 | 814 | case MIGRATION_STATUS_POSTCOPY_PAUSED: |
135b87b4 | 815 | case MIGRATION_STATUS_POSTCOPY_RECOVER: |
c8f9f4f4 | 816 | /* TODO add some postcopy stats */ |
9ec055ae DDAG |
817 | info->has_status = true; |
818 | info->has_total_time = true; | |
819 | info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) | |
4af246a3 | 820 | - s->start_time; |
9ec055ae DDAG |
821 | info->has_expected_downtime = true; |
822 | info->expected_downtime = s->expected_downtime; | |
823 | info->has_setup_time = true; | |
824 | info->setup_time = s->setup_time; | |
825 | ||
a22463a5 | 826 | populate_ram_info(info, s); |
930ac04c | 827 | populate_disk_info(info); |
17549e84 | 828 | break; |
0b827d5e HZ |
829 | case MIGRATION_STATUS_COLO: |
830 | info->has_status = true; | |
831 | /* TODO: display COLO specific information (checkpoint info etc.) */ | |
832 | break; | |
31194731 | 833 | case MIGRATION_STATUS_COMPLETED: |
791e7c82 | 834 | info->has_status = true; |
00c14997 | 835 | info->has_total_time = true; |
7aa939af | 836 | info->total_time = s->total_time; |
9c5a9fcf JQ |
837 | info->has_downtime = true; |
838 | info->downtime = s->downtime; | |
ed4fbd10 MH |
839 | info->has_setup_time = true; |
840 | info->setup_time = s->setup_time; | |
d5f8a570 | 841 | |
a22463a5 | 842 | populate_ram_info(info, s); |
17549e84 | 843 | break; |
31194731 | 844 | case MIGRATION_STATUS_FAILED: |
791e7c82 | 845 | info->has_status = true; |
d59ce6f3 DB |
846 | if (s->error) { |
847 | info->has_error_desc = true; | |
848 | info->error_desc = g_strdup(error_get_pretty(s->error)); | |
849 | } | |
17549e84 | 850 | break; |
31194731 | 851 | case MIGRATION_STATUS_CANCELLED: |
791e7c82 | 852 | info->has_status = true; |
17549e84 | 853 | break; |
5bb7910a | 854 | } |
cde63fbe | 855 | info->status = s->state; |
5bb7910a AL |
856 | } |
857 | ||
4a84214e PX |
858 | /** |
859 | * @migration_caps_check - check capability validity | |
860 | * | |
861 | * @cap_list: old capability list, array of bool | |
862 | * @params: new capabilities to be applied soon | |
863 | * @errp: set *errp if the check failed, with reason | |
864 | * | |
865 | * Returns true if check passed, otherwise false. | |
866 | */ | |
867 | static bool migrate_caps_check(bool *cap_list, | |
868 | MigrationCapabilityStatusList *params, | |
869 | Error **errp) | |
00458433 | 870 | { |
00458433 | 871 | MigrationCapabilityStatusList *cap; |
4a84214e | 872 | bool old_postcopy_cap; |
d7651f15 | 873 | MigrationIncomingState *mis = migration_incoming_get_current(); |
00458433 | 874 | |
4a84214e | 875 | old_postcopy_cap = cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]; |
00458433 OW |
876 | |
877 | for (cap = params; cap; cap = cap->next) { | |
4a84214e PX |
878 | cap_list[cap->value->capability] = cap->value->state; |
879 | } | |
880 | ||
ed1701c6 | 881 | #ifndef CONFIG_LIVE_BLOCK_MIGRATION |
4a84214e PX |
882 | if (cap_list[MIGRATION_CAPABILITY_BLOCK]) { |
883 | error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) " | |
884 | "block migration"); | |
885 | error_append_hint(errp, "Use drive_mirror+NBD instead.\n"); | |
886 | return false; | |
00458433 | 887 | } |
4a84214e | 888 | #endif |
53dd370c | 889 | |
4a84214e PX |
890 | if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) { |
891 | if (cap_list[MIGRATION_CAPABILITY_COMPRESS]) { | |
53dd370c DDAG |
892 | /* The decompression threads asynchronously write into RAM |
893 | * rather than use the atomic copies needed to avoid | |
894 | * userfaulting. It should be possible to fix the decompression | |
895 | * threads for compatibility in future. | |
896 | */ | |
4a84214e PX |
897 | error_setg(errp, "Postcopy is not currently compatible " |
898 | "with compression"); | |
899 | return false; | |
53dd370c | 900 | } |
4a84214e | 901 | |
096631bd DDAG |
902 | /* This check is reasonably expensive, so only when it's being |
903 | * set the first time, also it's only the destination that needs | |
904 | * special support. | |
905 | */ | |
906 | if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) && | |
d7651f15 | 907 | !postcopy_ram_supported_by_host(mis)) { |
096631bd DDAG |
908 | /* postcopy_ram_supported_by_host will have emitted a more |
909 | * detailed message | |
910 | */ | |
4a84214e PX |
911 | error_setg(errp, "Postcopy is not supported"); |
912 | return false; | |
096631bd | 913 | } |
53dd370c | 914 | } |
4a84214e PX |
915 | |
916 | return true; | |
917 | } | |
918 | ||
65ace060 AP |
919 | static void fill_destination_migration_info(MigrationInfo *info) |
920 | { | |
921 | MigrationIncomingState *mis = migration_incoming_get_current(); | |
922 | ||
923 | switch (mis->state) { | |
924 | case MIGRATION_STATUS_NONE: | |
925 | return; | |
926 | break; | |
927 | case MIGRATION_STATUS_SETUP: | |
928 | case MIGRATION_STATUS_CANCELLING: | |
929 | case MIGRATION_STATUS_CANCELLED: | |
930 | case MIGRATION_STATUS_ACTIVE: | |
931 | case MIGRATION_STATUS_POSTCOPY_ACTIVE: | |
3c9928d9 PX |
932 | case MIGRATION_STATUS_POSTCOPY_PAUSED: |
933 | case MIGRATION_STATUS_POSTCOPY_RECOVER: | |
65ace060 AP |
934 | case MIGRATION_STATUS_FAILED: |
935 | case MIGRATION_STATUS_COLO: | |
936 | info->has_status = true; | |
937 | break; | |
938 | case MIGRATION_STATUS_COMPLETED: | |
939 | info->has_status = true; | |
940 | fill_destination_postcopy_migration_info(info); | |
941 | break; | |
942 | } | |
943 | info->status = mis->state; | |
944 | } | |
945 | ||
946 | MigrationInfo *qmp_query_migrate(Error **errp) | |
947 | { | |
948 | MigrationInfo *info = g_malloc0(sizeof(*info)); | |
949 | ||
950 | fill_destination_migration_info(info); | |
951 | fill_source_migration_info(info); | |
952 | ||
953 | return info; | |
954 | } | |
955 | ||
4a84214e PX |
956 | void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params, |
957 | Error **errp) | |
958 | { | |
959 | MigrationState *s = migrate_get_current(); | |
960 | MigrationCapabilityStatusList *cap; | |
dd0ee30c | 961 | bool cap_list[MIGRATION_CAPABILITY__MAX]; |
4a84214e PX |
962 | |
963 | if (migration_is_setup_or_active(s->state)) { | |
964 | error_setg(errp, QERR_MIGRATION_ACTIVE); | |
965 | return; | |
966 | } | |
967 | ||
dd0ee30c PX |
968 | memcpy(cap_list, s->enabled_capabilities, sizeof(cap_list)); |
969 | if (!migrate_caps_check(cap_list, params, errp)) { | |
4a84214e PX |
970 | return; |
971 | } | |
972 | ||
973 | for (cap = params; cap; cap = cap->next) { | |
974 | s->enabled_capabilities[cap->value->capability] = cap->value->state; | |
975 | } | |
00458433 OW |
976 | } |
977 | ||
16d063bc PX |
978 | /* |
979 | * Check whether the parameters are valid. Error will be put into errp | |
980 | * (if provided). Return true if valid, otherwise false. | |
981 | */ | |
982 | static bool migrate_params_check(MigrationParameters *params, Error **errp) | |
85de8323 | 983 | { |
7f375e04 | 984 | if (params->has_compress_level && |
741d4086 | 985 | (params->compress_level > 9)) { |
c6bd8c70 MA |
986 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level", |
987 | "is invalid, it should be in the range of 0 to 9"); | |
16d063bc | 988 | return false; |
85de8323 | 989 | } |
16d063bc | 990 | |
741d4086 | 991 | if (params->has_compress_threads && (params->compress_threads < 1)) { |
c6bd8c70 MA |
992 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
993 | "compress_threads", | |
994 | "is invalid, it should be in the range of 1 to 255"); | |
16d063bc | 995 | return false; |
85de8323 | 996 | } |
16d063bc | 997 | |
741d4086 | 998 | if (params->has_decompress_threads && (params->decompress_threads < 1)) { |
c6bd8c70 MA |
999 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
1000 | "decompress_threads", | |
1001 | "is invalid, it should be in the range of 1 to 255"); | |
16d063bc | 1002 | return false; |
85de8323 | 1003 | } |
16d063bc | 1004 | |
7f375e04 EB |
1005 | if (params->has_cpu_throttle_initial && |
1006 | (params->cpu_throttle_initial < 1 || | |
1007 | params->cpu_throttle_initial > 99)) { | |
1626fee3 | 1008 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
d85a31d1 | 1009 | "cpu_throttle_initial", |
1626fee3 | 1010 | "an integer in the range of 1 to 99"); |
16d063bc | 1011 | return false; |
1626fee3 | 1012 | } |
16d063bc | 1013 | |
7f375e04 EB |
1014 | if (params->has_cpu_throttle_increment && |
1015 | (params->cpu_throttle_increment < 1 || | |
1016 | params->cpu_throttle_increment > 99)) { | |
1626fee3 | 1017 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
d85a31d1 | 1018 | "cpu_throttle_increment", |
1626fee3 | 1019 | "an integer in the range of 1 to 99"); |
16d063bc | 1020 | return false; |
1626fee3 | 1021 | } |
16d063bc | 1022 | |
741d4086 | 1023 | if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) { |
2ff30257 AA |
1024 | error_setg(errp, "Parameter 'max_bandwidth' expects an integer in the" |
1025 | " range of 0 to %zu bytes/second", SIZE_MAX); | |
16d063bc | 1026 | return false; |
2ff30257 | 1027 | } |
16d063bc | 1028 | |
2ff30257 | 1029 | if (params->has_downtime_limit && |
741d4086 | 1030 | (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) { |
87c9cc1c DHB |
1031 | error_setg(errp, "Parameter 'downtime_limit' expects an integer in " |
1032 | "the range of 0 to %d milliseconds", | |
1033 | MAX_MIGRATE_DOWNTIME); | |
16d063bc | 1034 | return false; |
2ff30257 | 1035 | } |
16d063bc | 1036 | |
741d4086 JQ |
1037 | /* x_checkpoint_delay is now always positive */ |
1038 | ||
1039 | if (params->has_x_multifd_channels && (params->x_multifd_channels < 1)) { | |
4075fb1c JQ |
1040 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
1041 | "multifd_channels", | |
1042 | "is invalid, it should be in the range of 1 to 255"); | |
1043 | return false; | |
1044 | } | |
0fb86605 | 1045 | if (params->has_x_multifd_page_count && |
741d4086 JQ |
1046 | (params->x_multifd_page_count < 1 || |
1047 | params->x_multifd_page_count > 10000)) { | |
0fb86605 JQ |
1048 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
1049 | "multifd_page_count", | |
1050 | "is invalid, it should be in the range of 1 to 10000"); | |
1051 | return false; | |
1052 | } | |
16d063bc | 1053 | |
73af8dd8 JQ |
1054 | if (params->has_xbzrle_cache_size && |
1055 | (params->xbzrle_cache_size < qemu_target_page_size() || | |
1056 | !is_power_of_2(params->xbzrle_cache_size))) { | |
1057 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, | |
1058 | "xbzrle_cache_size", | |
1059 | "is invalid, it should be bigger than target page size" | |
1060 | " and a power of two"); | |
1061 | return false; | |
1062 | } | |
1063 | ||
4cbc9c7f LQ |
1064 | if (params->has_max_cpu_throttle && |
1065 | (params->max_cpu_throttle < params->cpu_throttle_initial || | |
1066 | params->max_cpu_throttle > 99)) { | |
1067 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, | |
1068 | "max_cpu_throttle", | |
1069 | "an integer in the range of cpu_throttle_initial to 99"); | |
1070 | return false; | |
1071 | } | |
1072 | ||
16d063bc PX |
1073 | return true; |
1074 | } | |
1075 | ||
1bda8b3c MA |
1076 | static void migrate_params_test_apply(MigrateSetParameters *params, |
1077 | MigrationParameters *dest) | |
1078 | { | |
1079 | *dest = migrate_get_current()->parameters; | |
1080 | ||
1081 | /* TODO use QAPI_CLONE() instead of duplicating it inline */ | |
1082 | ||
1083 | if (params->has_compress_level) { | |
1084 | dest->compress_level = params->compress_level; | |
1085 | } | |
1086 | ||
1087 | if (params->has_compress_threads) { | |
1088 | dest->compress_threads = params->compress_threads; | |
1089 | } | |
1090 | ||
1d58872a XG |
1091 | if (params->has_compress_wait_thread) { |
1092 | dest->compress_wait_thread = params->compress_wait_thread; | |
1093 | } | |
1094 | ||
1bda8b3c MA |
1095 | if (params->has_decompress_threads) { |
1096 | dest->decompress_threads = params->decompress_threads; | |
1097 | } | |
1098 | ||
1099 | if (params->has_cpu_throttle_initial) { | |
1100 | dest->cpu_throttle_initial = params->cpu_throttle_initial; | |
1101 | } | |
1102 | ||
1103 | if (params->has_cpu_throttle_increment) { | |
1104 | dest->cpu_throttle_increment = params->cpu_throttle_increment; | |
1105 | } | |
1106 | ||
1107 | if (params->has_tls_creds) { | |
01fa5598 MA |
1108 | assert(params->tls_creds->type == QTYPE_QSTRING); |
1109 | dest->tls_creds = g_strdup(params->tls_creds->u.s); | |
1bda8b3c MA |
1110 | } |
1111 | ||
1112 | if (params->has_tls_hostname) { | |
01fa5598 MA |
1113 | assert(params->tls_hostname->type == QTYPE_QSTRING); |
1114 | dest->tls_hostname = g_strdup(params->tls_hostname->u.s); | |
1bda8b3c MA |
1115 | } |
1116 | ||
1117 | if (params->has_max_bandwidth) { | |
1118 | dest->max_bandwidth = params->max_bandwidth; | |
1119 | } | |
1120 | ||
1121 | if (params->has_downtime_limit) { | |
1122 | dest->downtime_limit = params->downtime_limit; | |
1123 | } | |
1124 | ||
1125 | if (params->has_x_checkpoint_delay) { | |
1126 | dest->x_checkpoint_delay = params->x_checkpoint_delay; | |
1127 | } | |
1128 | ||
1129 | if (params->has_block_incremental) { | |
1130 | dest->block_incremental = params->block_incremental; | |
1131 | } | |
5e7577a1 JQ |
1132 | if (params->has_x_multifd_channels) { |
1133 | dest->x_multifd_channels = params->x_multifd_channels; | |
1134 | } | |
1135 | if (params->has_x_multifd_page_count) { | |
1136 | dest->x_multifd_page_count = params->x_multifd_page_count; | |
1137 | } | |
73af8dd8 JQ |
1138 | if (params->has_xbzrle_cache_size) { |
1139 | dest->xbzrle_cache_size = params->xbzrle_cache_size; | |
1140 | } | |
7e555c6c DDAG |
1141 | if (params->has_max_postcopy_bandwidth) { |
1142 | dest->max_postcopy_bandwidth = params->max_postcopy_bandwidth; | |
1143 | } | |
4cbc9c7f LQ |
1144 | if (params->has_max_cpu_throttle) { |
1145 | dest->max_cpu_throttle = params->max_cpu_throttle; | |
1146 | } | |
1bda8b3c MA |
1147 | } |
1148 | ||
73af8dd8 | 1149 | static void migrate_params_apply(MigrateSetParameters *params, Error **errp) |
16d063bc PX |
1150 | { |
1151 | MigrationState *s = migrate_get_current(); | |
1152 | ||
e87fae4c MA |
1153 | /* TODO use QAPI_CLONE() instead of duplicating it inline */ |
1154 | ||
7f375e04 EB |
1155 | if (params->has_compress_level) { |
1156 | s->parameters.compress_level = params->compress_level; | |
85de8323 | 1157 | } |
476c72aa | 1158 | |
7f375e04 EB |
1159 | if (params->has_compress_threads) { |
1160 | s->parameters.compress_threads = params->compress_threads; | |
85de8323 | 1161 | } |
476c72aa | 1162 | |
1d58872a XG |
1163 | if (params->has_compress_wait_thread) { |
1164 | s->parameters.compress_wait_thread = params->compress_wait_thread; | |
1165 | } | |
1166 | ||
7f375e04 EB |
1167 | if (params->has_decompress_threads) { |
1168 | s->parameters.decompress_threads = params->decompress_threads; | |
85de8323 | 1169 | } |
476c72aa | 1170 | |
7f375e04 EB |
1171 | if (params->has_cpu_throttle_initial) { |
1172 | s->parameters.cpu_throttle_initial = params->cpu_throttle_initial; | |
1626fee3 | 1173 | } |
476c72aa | 1174 | |
7f375e04 EB |
1175 | if (params->has_cpu_throttle_increment) { |
1176 | s->parameters.cpu_throttle_increment = params->cpu_throttle_increment; | |
1626fee3 | 1177 | } |
476c72aa | 1178 | |
7f375e04 | 1179 | if (params->has_tls_creds) { |
69ef1f36 | 1180 | g_free(s->parameters.tls_creds); |
01fa5598 MA |
1181 | assert(params->tls_creds->type == QTYPE_QSTRING); |
1182 | s->parameters.tls_creds = g_strdup(params->tls_creds->u.s); | |
69ef1f36 | 1183 | } |
476c72aa | 1184 | |
7f375e04 | 1185 | if (params->has_tls_hostname) { |
69ef1f36 | 1186 | g_free(s->parameters.tls_hostname); |
01fa5598 MA |
1187 | assert(params->tls_hostname->type == QTYPE_QSTRING); |
1188 | s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s); | |
69ef1f36 | 1189 | } |
476c72aa | 1190 | |
2ff30257 AA |
1191 | if (params->has_max_bandwidth) { |
1192 | s->parameters.max_bandwidth = params->max_bandwidth; | |
1193 | if (s->to_dst_file) { | |
1194 | qemu_file_set_rate_limit(s->to_dst_file, | |
1195 | s->parameters.max_bandwidth / XFER_LIMIT_RATIO); | |
1196 | } | |
1197 | } | |
476c72aa | 1198 | |
2ff30257 AA |
1199 | if (params->has_downtime_limit) { |
1200 | s->parameters.downtime_limit = params->downtime_limit; | |
1201 | } | |
68b53591 HZ |
1202 | |
1203 | if (params->has_x_checkpoint_delay) { | |
1204 | s->parameters.x_checkpoint_delay = params->x_checkpoint_delay; | |
479125d5 HZ |
1205 | if (migration_in_colo_state()) { |
1206 | colo_checkpoint_notify(s); | |
1207 | } | |
68b53591 | 1208 | } |
476c72aa | 1209 | |
2833c59b JQ |
1210 | if (params->has_block_incremental) { |
1211 | s->parameters.block_incremental = params->block_incremental; | |
1212 | } | |
4075fb1c JQ |
1213 | if (params->has_x_multifd_channels) { |
1214 | s->parameters.x_multifd_channels = params->x_multifd_channels; | |
1215 | } | |
0fb86605 JQ |
1216 | if (params->has_x_multifd_page_count) { |
1217 | s->parameters.x_multifd_page_count = params->x_multifd_page_count; | |
1218 | } | |
73af8dd8 JQ |
1219 | if (params->has_xbzrle_cache_size) { |
1220 | s->parameters.xbzrle_cache_size = params->xbzrle_cache_size; | |
1221 | xbzrle_cache_resize(params->xbzrle_cache_size, errp); | |
1222 | } | |
7e555c6c DDAG |
1223 | if (params->has_max_postcopy_bandwidth) { |
1224 | s->parameters.max_postcopy_bandwidth = params->max_postcopy_bandwidth; | |
1225 | } | |
4cbc9c7f LQ |
1226 | if (params->has_max_cpu_throttle) { |
1227 | s->parameters.max_cpu_throttle = params->max_cpu_throttle; | |
1228 | } | |
85de8323 LL |
1229 | } |
1230 | ||
1bda8b3c | 1231 | void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp) |
476c72aa | 1232 | { |
1bda8b3c MA |
1233 | MigrationParameters tmp; |
1234 | ||
01fa5598 MA |
1235 | /* TODO Rewrite "" to null instead */ |
1236 | if (params->has_tls_creds | |
1237 | && params->tls_creds->type == QTYPE_QNULL) { | |
cb3e7f08 | 1238 | qobject_unref(params->tls_creds->u.n); |
01fa5598 MA |
1239 | params->tls_creds->type = QTYPE_QSTRING; |
1240 | params->tls_creds->u.s = strdup(""); | |
1241 | } | |
1242 | /* TODO Rewrite "" to null instead */ | |
1243 | if (params->has_tls_hostname | |
1244 | && params->tls_hostname->type == QTYPE_QNULL) { | |
cb3e7f08 | 1245 | qobject_unref(params->tls_hostname->u.n); |
01fa5598 MA |
1246 | params->tls_hostname->type = QTYPE_QSTRING; |
1247 | params->tls_hostname->u.s = strdup(""); | |
1248 | } | |
1249 | ||
1bda8b3c MA |
1250 | migrate_params_test_apply(params, &tmp); |
1251 | ||
1252 | if (!migrate_params_check(&tmp, errp)) { | |
476c72aa PX |
1253 | /* Invalid parameter */ |
1254 | return; | |
1255 | } | |
1256 | ||
73af8dd8 | 1257 | migrate_params_apply(params, errp); |
476c72aa PX |
1258 | } |
1259 | ||
2594f56d | 1260 | |
4886a1bc DDAG |
1261 | void qmp_migrate_start_postcopy(Error **errp) |
1262 | { | |
1263 | MigrationState *s = migrate_get_current(); | |
1264 | ||
16b0fd32 | 1265 | if (!migrate_postcopy()) { |
a54d340b | 1266 | error_setg(errp, "Enable postcopy with migrate_set_capability before" |
4886a1bc DDAG |
1267 | " the start of migration"); |
1268 | return; | |
1269 | } | |
1270 | ||
1271 | if (s->state == MIGRATION_STATUS_NONE) { | |
1272 | error_setg(errp, "Postcopy must be started after migration has been" | |
1273 | " started"); | |
1274 | return; | |
1275 | } | |
1276 | /* | |
1277 | * we don't error if migration has finished since that would be racy | |
1278 | * with issuing this command. | |
1279 | */ | |
1280 | atomic_set(&s->start_postcopy, true); | |
1281 | } | |
1282 | ||
065e2813 AL |
1283 | /* shared migration helpers */ |
1284 | ||
48781e5b | 1285 | void migrate_set_state(int *state, int old_state, int new_state) |
51cf4c1a | 1286 | { |
a31fedee | 1287 | assert(new_state < MIGRATION_STATUS__MAX); |
48781e5b | 1288 | if (atomic_cmpxchg(state, old_state, new_state) == old_state) { |
a31fedee | 1289 | trace_migrate_set_state(MigrationStatus_str(new_state)); |
b05dc723 | 1290 | migrate_generate_event(new_state); |
51cf4c1a Z |
1291 | } |
1292 | } | |
1293 | ||
4e4a3d3a PX |
1294 | static MigrationCapabilityStatusList *migrate_cap_add( |
1295 | MigrationCapabilityStatusList *list, | |
1296 | MigrationCapability index, | |
1297 | bool state) | |
2833c59b JQ |
1298 | { |
1299 | MigrationCapabilityStatusList *cap; | |
1300 | ||
1301 | cap = g_new0(MigrationCapabilityStatusList, 1); | |
1302 | cap->value = g_new0(MigrationCapabilityStatus, 1); | |
4e4a3d3a PX |
1303 | cap->value->capability = index; |
1304 | cap->value->state = state; | |
1305 | cap->next = list; | |
1306 | ||
1307 | return cap; | |
1308 | } | |
1309 | ||
1310 | void migrate_set_block_enabled(bool value, Error **errp) | |
1311 | { | |
1312 | MigrationCapabilityStatusList *cap; | |
1313 | ||
1314 | cap = migrate_cap_add(NULL, MIGRATION_CAPABILITY_BLOCK, value); | |
2833c59b JQ |
1315 | qmp_migrate_set_capabilities(cap, errp); |
1316 | qapi_free_MigrationCapabilityStatusList(cap); | |
1317 | } | |
1318 | ||
1319 | static void migrate_set_block_incremental(MigrationState *s, bool value) | |
1320 | { | |
1321 | s->parameters.block_incremental = value; | |
1322 | } | |
1323 | ||
1324 | static void block_cleanup_parameters(MigrationState *s) | |
1325 | { | |
1326 | if (s->must_remove_block_options) { | |
1327 | /* setting to false can never fail */ | |
1328 | migrate_set_block_enabled(false, &error_abort); | |
1329 | migrate_set_block_incremental(s, false); | |
1330 | s->must_remove_block_options = false; | |
1331 | } | |
1332 | } | |
1333 | ||
bb1fadc4 | 1334 | static void migrate_fd_cleanup(void *opaque) |
065e2813 | 1335 | { |
bb1fadc4 PB |
1336 | MigrationState *s = opaque; |
1337 | ||
1338 | qemu_bh_delete(s->cleanup_bh); | |
1339 | s->cleanup_bh = NULL; | |
1340 | ||
0ceccd85 PX |
1341 | qemu_savevm_state_cleanup(); |
1342 | ||
89a02a9f | 1343 | if (s->to_dst_file) { |
f986c3d2 | 1344 | Error *local_err = NULL; |
62df066f | 1345 | QEMUFile *tmp; |
f986c3d2 | 1346 | |
9013dca5 | 1347 | trace_migrate_fd_cleanup(); |
404a7c05 | 1348 | qemu_mutex_unlock_iothread(); |
1d34e4bf DDAG |
1349 | if (s->migration_thread_running) { |
1350 | qemu_thread_join(&s->thread); | |
1351 | s->migration_thread_running = false; | |
1352 | } | |
404a7c05 PB |
1353 | qemu_mutex_lock_iothread(); |
1354 | ||
f986c3d2 JQ |
1355 | if (multifd_save_cleanup(&local_err) != 0) { |
1356 | error_report_err(local_err); | |
1357 | } | |
62df066f PX |
1358 | qemu_mutex_lock(&s->qemu_file_lock); |
1359 | tmp = s->to_dst_file; | |
89a02a9f | 1360 | s->to_dst_file = NULL; |
62df066f PX |
1361 | qemu_mutex_unlock(&s->qemu_file_lock); |
1362 | /* | |
1363 | * Close the file handle without the lock to make sure the | |
1364 | * critical section won't block for long. | |
1365 | */ | |
1366 | qemu_fclose(tmp); | |
065e2813 AL |
1367 | } |
1368 | ||
9ec055ae DDAG |
1369 | assert((s->state != MIGRATION_STATUS_ACTIVE) && |
1370 | (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE)); | |
7a2c1721 | 1371 | |
94f5a437 | 1372 | if (s->state == MIGRATION_STATUS_CANCELLING) { |
48781e5b | 1373 | migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING, |
94f5a437 | 1374 | MIGRATION_STATUS_CANCELLED); |
7a2c1721 | 1375 | } |
a3fa1d78 | 1376 | |
87db1a7d JQ |
1377 | if (s->error) { |
1378 | /* It is used on info migrate. We can't free it */ | |
1379 | error_report_err(error_copy(s->error)); | |
1380 | } | |
a3fa1d78 | 1381 | notifier_list_notify(&migration_state_notifiers, s); |
2833c59b | 1382 | block_cleanup_parameters(s); |
065e2813 AL |
1383 | } |
1384 | ||
87db1a7d JQ |
1385 | void migrate_set_error(MigrationState *s, const Error *error) |
1386 | { | |
1387 | qemu_mutex_lock(&s->error_mutex); | |
1388 | if (!s->error) { | |
1389 | s->error = error_copy(error); | |
1390 | } | |
1391 | qemu_mutex_unlock(&s->error_mutex); | |
1392 | } | |
1393 | ||
d59ce6f3 | 1394 | void migrate_fd_error(MigrationState *s, const Error *error) |
065e2813 | 1395 | { |
25174055 | 1396 | trace_migrate_fd_error(error_get_pretty(error)); |
89a02a9f | 1397 | assert(s->to_dst_file == NULL); |
48781e5b HZ |
1398 | migrate_set_state(&s->state, MIGRATION_STATUS_SETUP, |
1399 | MIGRATION_STATUS_FAILED); | |
87db1a7d | 1400 | migrate_set_error(s, error); |
458cf28e JQ |
1401 | } |
1402 | ||
0edda1c4 | 1403 | static void migrate_fd_cancel(MigrationState *s) |
065e2813 | 1404 | { |
6f2b811a | 1405 | int old_state ; |
89a02a9f | 1406 | QEMUFile *f = migrate_get_current()->to_dst_file; |
9013dca5 | 1407 | trace_migrate_fd_cancel(); |
065e2813 | 1408 | |
70b20477 DDAG |
1409 | if (s->rp_state.from_dst_file) { |
1410 | /* shutdown the rp socket, so causing the rp thread to shutdown */ | |
1411 | qemu_file_shutdown(s->rp_state.from_dst_file); | |
1412 | } | |
1413 | ||
6f2b811a Z |
1414 | do { |
1415 | old_state = s->state; | |
f6844b99 | 1416 | if (!migration_is_setup_or_active(old_state)) { |
6f2b811a Z |
1417 | break; |
1418 | } | |
a7b36b48 DDAG |
1419 | /* If the migration is paused, kick it out of the pause */ |
1420 | if (old_state == MIGRATION_STATUS_PRE_SWITCHOVER) { | |
1421 | qemu_sem_post(&s->pause_sem); | |
1422 | } | |
48781e5b | 1423 | migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING); |
31194731 | 1424 | } while (s->state != MIGRATION_STATUS_CANCELLING); |
a26ba26e DDAG |
1425 | |
1426 | /* | |
1427 | * If we're unlucky the migration code might be stuck somewhere in a | |
1428 | * send/write while the network has failed and is waiting to timeout; | |
1429 | * if we've got shutdown(2) available then we can force it to quit. | |
1430 | * The outgoing qemu file gets closed in migrate_fd_cleanup that is | |
1431 | * called in a bh, so there is no race against this cancel. | |
1432 | */ | |
31194731 | 1433 | if (s->state == MIGRATION_STATUS_CANCELLING && f) { |
a26ba26e DDAG |
1434 | qemu_file_shutdown(f); |
1435 | } | |
1d2acc31 HZ |
1436 | if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) { |
1437 | Error *local_err = NULL; | |
1438 | ||
1439 | bdrv_invalidate_cache_all(&local_err); | |
1440 | if (local_err) { | |
1441 | error_report_err(local_err); | |
1442 | } else { | |
1443 | s->block_inactive = false; | |
1444 | } | |
1445 | } | |
065e2813 AL |
1446 | } |
1447 | ||
99a0db9b GH |
1448 | void add_migration_state_change_notifier(Notifier *notify) |
1449 | { | |
1450 | notifier_list_add(&migration_state_notifiers, notify); | |
1451 | } | |
1452 | ||
1453 | void remove_migration_state_change_notifier(Notifier *notify) | |
1454 | { | |
31552529 | 1455 | notifier_remove(notify); |
99a0db9b GH |
1456 | } |
1457 | ||
02edd2e7 | 1458 | bool migration_in_setup(MigrationState *s) |
afe2df69 | 1459 | { |
31194731 | 1460 | return s->state == MIGRATION_STATUS_SETUP; |
afe2df69 GH |
1461 | } |
1462 | ||
7073693b | 1463 | bool migration_has_finished(MigrationState *s) |
99a0db9b | 1464 | { |
31194731 | 1465 | return s->state == MIGRATION_STATUS_COMPLETED; |
99a0db9b | 1466 | } |
0edda1c4 | 1467 | |
afe2df69 GH |
1468 | bool migration_has_failed(MigrationState *s) |
1469 | { | |
31194731 HZ |
1470 | return (s->state == MIGRATION_STATUS_CANCELLED || |
1471 | s->state == MIGRATION_STATUS_FAILED); | |
afe2df69 GH |
1472 | } |
1473 | ||
5727309d | 1474 | bool migration_in_postcopy(void) |
9ec055ae | 1475 | { |
5727309d JQ |
1476 | MigrationState *s = migrate_get_current(); |
1477 | ||
9ec055ae DDAG |
1478 | return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE); |
1479 | } | |
1480 | ||
b82fc321 DDAG |
1481 | bool migration_in_postcopy_after_devices(MigrationState *s) |
1482 | { | |
5727309d | 1483 | return migration_in_postcopy() && s->postcopy_after_devices; |
b82fc321 DDAG |
1484 | } |
1485 | ||
fab35005 | 1486 | bool migration_is_idle(void) |
fe44dc91 | 1487 | { |
fab35005 | 1488 | MigrationState *s = migrate_get_current(); |
fe44dc91 AA |
1489 | |
1490 | switch (s->state) { | |
1491 | case MIGRATION_STATUS_NONE: | |
1492 | case MIGRATION_STATUS_CANCELLED: | |
1493 | case MIGRATION_STATUS_COMPLETED: | |
1494 | case MIGRATION_STATUS_FAILED: | |
1495 | return true; | |
1496 | case MIGRATION_STATUS_SETUP: | |
1497 | case MIGRATION_STATUS_CANCELLING: | |
1498 | case MIGRATION_STATUS_ACTIVE: | |
1499 | case MIGRATION_STATUS_POSTCOPY_ACTIVE: | |
1500 | case MIGRATION_STATUS_COLO: | |
31e06077 DDAG |
1501 | case MIGRATION_STATUS_PRE_SWITCHOVER: |
1502 | case MIGRATION_STATUS_DEVICE: | |
fe44dc91 AA |
1503 | return false; |
1504 | case MIGRATION_STATUS__MAX: | |
1505 | g_assert_not_reached(); | |
1506 | } | |
1507 | ||
1508 | return false; | |
1509 | } | |
1510 | ||
3e0c8050 | 1511 | void migrate_init(MigrationState *s) |
0edda1c4 | 1512 | { |
389775d1 DDAG |
1513 | /* |
1514 | * Reinitialise all migration state, except | |
1515 | * parameters/capabilities that the user set, and | |
1516 | * locks. | |
1517 | */ | |
1518 | s->bytes_xfer = 0; | |
1519 | s->xfer_limit = 0; | |
1520 | s->cleanup_bh = 0; | |
89a02a9f | 1521 | s->to_dst_file = NULL; |
389775d1 | 1522 | s->state = MIGRATION_STATUS_NONE; |
389775d1 DDAG |
1523 | s->rp_state.from_dst_file = NULL; |
1524 | s->rp_state.error = false; | |
1525 | s->mbps = 0.0; | |
1526 | s->downtime = 0; | |
1527 | s->expected_downtime = 0; | |
389775d1 | 1528 | s->setup_time = 0; |
389775d1 | 1529 | s->start_postcopy = false; |
b82fc321 | 1530 | s->postcopy_after_devices = false; |
389775d1 | 1531 | s->migration_thread_running = false; |
d59ce6f3 DB |
1532 | error_free(s->error); |
1533 | s->error = NULL; | |
389775d1 | 1534 | |
48781e5b | 1535 | migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP); |
0edda1c4 | 1536 | |
4af246a3 PX |
1537 | s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
1538 | s->total_time = 0; | |
7287cbd4 | 1539 | s->vm_was_running = false; |
b15df1ae PX |
1540 | s->iteration_initial_bytes = 0; |
1541 | s->threshold_size = 0; | |
0edda1c4 | 1542 | } |
cab30143 | 1543 | |
fa2756b7 AL |
1544 | static GSList *migration_blockers; |
1545 | ||
fe44dc91 | 1546 | int migrate_add_blocker(Error *reason, Error **errp) |
fa2756b7 | 1547 | { |
3df663e5 | 1548 | if (migrate_get_current()->only_migratable) { |
b67b8c3a AA |
1549 | error_propagate(errp, error_copy(reason)); |
1550 | error_prepend(errp, "disallowing migration blocker " | |
1551 | "(--only_migratable) for: "); | |
1552 | return -EACCES; | |
1553 | } | |
1554 | ||
fab35005 | 1555 | if (migration_is_idle()) { |
fe44dc91 AA |
1556 | migration_blockers = g_slist_prepend(migration_blockers, reason); |
1557 | return 0; | |
1558 | } | |
1559 | ||
1560 | error_propagate(errp, error_copy(reason)); | |
1561 | error_prepend(errp, "disallowing migration blocker (migration in " | |
1562 | "progress) for: "); | |
1563 | return -EBUSY; | |
fa2756b7 AL |
1564 | } |
1565 | ||
1566 | void migrate_del_blocker(Error *reason) | |
1567 | { | |
1568 | migration_blockers = g_slist_remove(migration_blockers, reason); | |
1569 | } | |
1570 | ||
bf1ae1f4 DDAG |
1571 | void qmp_migrate_incoming(const char *uri, Error **errp) |
1572 | { | |
1573 | Error *local_err = NULL; | |
4debb5f5 | 1574 | static bool once = true; |
bf1ae1f4 DDAG |
1575 | |
1576 | if (!deferred_incoming) { | |
4debb5f5 | 1577 | error_setg(errp, "For use with '-incoming defer'"); |
bf1ae1f4 DDAG |
1578 | return; |
1579 | } | |
4debb5f5 DDAG |
1580 | if (!once) { |
1581 | error_setg(errp, "The incoming migration has already been started"); | |
1582 | } | |
bf1ae1f4 DDAG |
1583 | |
1584 | qemu_start_incoming_migration(uri, &local_err); | |
1585 | ||
1586 | if (local_err) { | |
1587 | error_propagate(errp, local_err); | |
1588 | return; | |
1589 | } | |
1590 | ||
4debb5f5 | 1591 | once = false; |
bf1ae1f4 DDAG |
1592 | } |
1593 | ||
02affd41 PX |
1594 | void qmp_migrate_recover(const char *uri, Error **errp) |
1595 | { | |
1596 | MigrationIncomingState *mis = migration_incoming_get_current(); | |
1597 | ||
1598 | if (mis->state != MIGRATION_STATUS_POSTCOPY_PAUSED) { | |
1599 | error_setg(errp, "Migrate recover can only be run " | |
1600 | "when postcopy is paused."); | |
1601 | return; | |
1602 | } | |
1603 | ||
1604 | if (atomic_cmpxchg(&mis->postcopy_recover_triggered, | |
1605 | false, true) == true) { | |
1606 | error_setg(errp, "Migrate recovery is triggered already"); | |
1607 | return; | |
1608 | } | |
1609 | ||
1610 | /* | |
1611 | * Note that this call will never start a real migration; it will | |
1612 | * only re-setup the migration stream and poke existing migration | |
1613 | * to continue using that newly established channel. | |
1614 | */ | |
1615 | qemu_start_incoming_migration(uri, errp); | |
1616 | } | |
1617 | ||
bfbf89c2 PX |
1618 | void qmp_migrate_pause(Error **errp) |
1619 | { | |
1620 | MigrationState *ms = migrate_get_current(); | |
1621 | MigrationIncomingState *mis = migration_incoming_get_current(); | |
1622 | int ret; | |
1623 | ||
1624 | if (ms->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) { | |
1625 | /* Source side, during postcopy */ | |
1626 | qemu_mutex_lock(&ms->qemu_file_lock); | |
1627 | ret = qemu_file_shutdown(ms->to_dst_file); | |
1628 | qemu_mutex_unlock(&ms->qemu_file_lock); | |
1629 | if (ret) { | |
1630 | error_setg(errp, "Failed to pause source migration"); | |
1631 | } | |
1632 | return; | |
1633 | } | |
1634 | ||
1635 | if (mis->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) { | |
1636 | ret = qemu_file_shutdown(mis->from_src_file); | |
1637 | if (ret) { | |
1638 | error_setg(errp, "Failed to pause destination migration"); | |
1639 | } | |
1640 | return; | |
1641 | } | |
1642 | ||
1643 | error_setg(errp, "migrate-pause is currently only supported " | |
1644 | "during postcopy-active state"); | |
1645 | } | |
1646 | ||
24f3902b GK |
1647 | bool migration_is_blocked(Error **errp) |
1648 | { | |
1649 | if (qemu_savevm_state_blocked(errp)) { | |
1650 | return true; | |
1651 | } | |
1652 | ||
1653 | if (migration_blockers) { | |
250561e1 | 1654 | error_propagate(errp, error_copy(migration_blockers->data)); |
24f3902b GK |
1655 | return true; |
1656 | } | |
1657 | ||
1658 | return false; | |
1659 | } | |
1660 | ||
d3e35b8f PX |
1661 | /* Returns true if continue to migrate, or false if error detected */ |
1662 | static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc, | |
1663 | bool resume, Error **errp) | |
cab30143 | 1664 | { |
be7059cd | 1665 | Error *local_err = NULL; |
d3e35b8f PX |
1666 | |
1667 | if (resume) { | |
1668 | if (s->state != MIGRATION_STATUS_POSTCOPY_PAUSED) { | |
1669 | error_setg(errp, "Cannot resume if there is no " | |
1670 | "paused migration"); | |
1671 | return false; | |
1672 | } | |
97ca211c PX |
1673 | |
1674 | /* | |
1675 | * Postcopy recovery won't work well with release-ram | |
1676 | * capability since release-ram will drop the page buffer as | |
1677 | * long as the page is put into the send buffer. So if there | |
1678 | * is a network failure happened, any page buffers that have | |
1679 | * not yet reached the destination VM but have already been | |
1680 | * sent from the source VM will be lost forever. Let's refuse | |
1681 | * the client from resuming such a postcopy migration. | |
1682 | * Luckily release-ram was designed to only be used when src | |
1683 | * and destination VMs are on the same host, so it should be | |
1684 | * fine. | |
1685 | */ | |
1686 | if (migrate_release_ram()) { | |
1687 | error_setg(errp, "Postcopy recovery cannot work " | |
1688 | "when release-ram capability is set"); | |
1689 | return false; | |
1690 | } | |
1691 | ||
d3e35b8f PX |
1692 | /* This is a resume, skip init status */ |
1693 | return true; | |
1694 | } | |
cab30143 | 1695 | |
f6844b99 | 1696 | if (migration_is_setup_or_active(s->state) || |
0b827d5e HZ |
1697 | s->state == MIGRATION_STATUS_CANCELLING || |
1698 | s->state == MIGRATION_STATUS_COLO) { | |
c6bd8c70 | 1699 | error_setg(errp, QERR_MIGRATION_ACTIVE); |
d3e35b8f | 1700 | return false; |
cab30143 | 1701 | } |
d3e35b8f | 1702 | |
ca99993a DDAG |
1703 | if (runstate_check(RUN_STATE_INMIGRATE)) { |
1704 | error_setg(errp, "Guest is waiting for an incoming migration"); | |
d3e35b8f | 1705 | return false; |
ca99993a DDAG |
1706 | } |
1707 | ||
24f3902b | 1708 | if (migration_is_blocked(errp)) { |
d3e35b8f | 1709 | return false; |
fa2756b7 AL |
1710 | } |
1711 | ||
d3e35b8f | 1712 | if (blk || blk_inc) { |
2833c59b JQ |
1713 | if (migrate_use_block() || migrate_use_block_incremental()) { |
1714 | error_setg(errp, "Command options are incompatible with " | |
1715 | "current migration capabilities"); | |
d3e35b8f | 1716 | return false; |
2833c59b JQ |
1717 | } |
1718 | migrate_set_block_enabled(true, &local_err); | |
1719 | if (local_err) { | |
1720 | error_propagate(errp, local_err); | |
d3e35b8f | 1721 | return false; |
2833c59b JQ |
1722 | } |
1723 | s->must_remove_block_options = true; | |
1724 | } | |
1725 | ||
d3e35b8f | 1726 | if (blk_inc) { |
2833c59b JQ |
1727 | migrate_set_block_incremental(s, true); |
1728 | } | |
1729 | ||
3e0c8050 | 1730 | migrate_init(s); |
cab30143 | 1731 | |
d3e35b8f PX |
1732 | return true; |
1733 | } | |
1734 | ||
1735 | void qmp_migrate(const char *uri, bool has_blk, bool blk, | |
1736 | bool has_inc, bool inc, bool has_detach, bool detach, | |
1737 | bool has_resume, bool resume, Error **errp) | |
1738 | { | |
1739 | Error *local_err = NULL; | |
1740 | MigrationState *s = migrate_get_current(); | |
1741 | const char *p; | |
1742 | ||
1743 | if (!migrate_prepare(s, has_blk && blk, has_inc && inc, | |
1744 | has_resume && resume, errp)) { | |
1745 | /* Error detected, put into errp */ | |
1746 | return; | |
1747 | } | |
1748 | ||
cab30143 | 1749 | if (strstart(uri, "tcp:", &p)) { |
f37afb5a | 1750 | tcp_start_outgoing_migration(s, p, &local_err); |
2da776db | 1751 | #ifdef CONFIG_RDMA |
41310c68 | 1752 | } else if (strstart(uri, "rdma:", &p)) { |
2da776db MH |
1753 | rdma_start_outgoing_migration(s, p, &local_err); |
1754 | #endif | |
cab30143 | 1755 | } else if (strstart(uri, "exec:", &p)) { |
f37afb5a | 1756 | exec_start_outgoing_migration(s, p, &local_err); |
cab30143 | 1757 | } else if (strstart(uri, "unix:", &p)) { |
f37afb5a | 1758 | unix_start_outgoing_migration(s, p, &local_err); |
cab30143 | 1759 | } else if (strstart(uri, "fd:", &p)) { |
f37afb5a | 1760 | fd_start_outgoing_migration(s, p, &local_err); |
99a0db9b | 1761 | } else { |
c6bd8c70 MA |
1762 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri", |
1763 | "a valid migration protocol"); | |
48781e5b HZ |
1764 | migrate_set_state(&s->state, MIGRATION_STATUS_SETUP, |
1765 | MIGRATION_STATUS_FAILED); | |
09576e74 | 1766 | block_cleanup_parameters(s); |
e1c37d0e | 1767 | return; |
cab30143 JQ |
1768 | } |
1769 | ||
f37afb5a | 1770 | if (local_err) { |
d59ce6f3 | 1771 | migrate_fd_error(s, local_err); |
f37afb5a | 1772 | error_propagate(errp, local_err); |
e1c37d0e | 1773 | return; |
1299c631 | 1774 | } |
cab30143 JQ |
1775 | } |
1776 | ||
6cdedb07 | 1777 | void qmp_migrate_cancel(Error **errp) |
cab30143 | 1778 | { |
17549e84 | 1779 | migrate_fd_cancel(migrate_get_current()); |
cab30143 JQ |
1780 | } |
1781 | ||
89cfc02c DDAG |
1782 | void qmp_migrate_continue(MigrationStatus state, Error **errp) |
1783 | { | |
1784 | MigrationState *s = migrate_get_current(); | |
1785 | if (s->state != state) { | |
1786 | error_setg(errp, "Migration not in expected state: %s", | |
1787 | MigrationStatus_str(s->state)); | |
1788 | return; | |
1789 | } | |
1790 | qemu_sem_post(&s->pause_sem); | |
1791 | } | |
1792 | ||
9e1ba4cc OW |
1793 | void qmp_migrate_set_cache_size(int64_t value, Error **errp) |
1794 | { | |
73af8dd8 JQ |
1795 | MigrateSetParameters p = { |
1796 | .has_xbzrle_cache_size = true, | |
1797 | .xbzrle_cache_size = value, | |
1798 | }; | |
c91e681a | 1799 | |
73af8dd8 | 1800 | qmp_migrate_set_parameters(&p, errp); |
9e1ba4cc OW |
1801 | } |
1802 | ||
1803 | int64_t qmp_query_migrate_cache_size(Error **errp) | |
1804 | { | |
1805 | return migrate_xbzrle_cache_size(); | |
1806 | } | |
1807 | ||
3dc85383 | 1808 | void qmp_migrate_set_speed(int64_t value, Error **errp) |
cab30143 | 1809 | { |
1bda8b3c | 1810 | MigrateSetParameters p = { |
2ff30257 AA |
1811 | .has_max_bandwidth = true, |
1812 | .max_bandwidth = value, | |
1813 | }; | |
cab30143 | 1814 | |
2ff30257 | 1815 | qmp_migrate_set_parameters(&p, errp); |
cab30143 JQ |
1816 | } |
1817 | ||
4f0a993b | 1818 | void qmp_migrate_set_downtime(double value, Error **errp) |
cab30143 | 1819 | { |
87c9cc1c DHB |
1820 | if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) { |
1821 | error_setg(errp, "Parameter 'downtime_limit' expects an integer in " | |
1822 | "the range of 0 to %d seconds", | |
1823 | MAX_MIGRATE_DOWNTIME_SECONDS); | |
1824 | return; | |
1825 | } | |
1826 | ||
2ff30257 AA |
1827 | value *= 1000; /* Convert to milliseconds */ |
1828 | value = MAX(0, MIN(INT64_MAX, value)); | |
1829 | ||
1bda8b3c | 1830 | MigrateSetParameters p = { |
2ff30257 AA |
1831 | .has_downtime_limit = true, |
1832 | .downtime_limit = value, | |
1833 | }; | |
1834 | ||
1835 | qmp_migrate_set_parameters(&p, errp); | |
99a0db9b | 1836 | } |
17ad9b35 | 1837 | |
53f09a10 PB |
1838 | bool migrate_release_ram(void) |
1839 | { | |
1840 | MigrationState *s; | |
1841 | ||
1842 | s = migrate_get_current(); | |
1843 | ||
1844 | return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM]; | |
1845 | } | |
1846 | ||
53dd370c DDAG |
1847 | bool migrate_postcopy_ram(void) |
1848 | { | |
1849 | MigrationState *s; | |
1850 | ||
1851 | s = migrate_get_current(); | |
1852 | ||
32c3db5b | 1853 | return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM]; |
53dd370c DDAG |
1854 | } |
1855 | ||
58110f0a VSO |
1856 | bool migrate_postcopy(void) |
1857 | { | |
dd6bb914 | 1858 | return migrate_postcopy_ram() || migrate_dirty_bitmaps(); |
58110f0a VSO |
1859 | } |
1860 | ||
bde1e2ec CV |
1861 | bool migrate_auto_converge(void) |
1862 | { | |
1863 | MigrationState *s; | |
1864 | ||
1865 | s = migrate_get_current(); | |
1866 | ||
1867 | return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE]; | |
1868 | } | |
1869 | ||
323004a3 PL |
1870 | bool migrate_zero_blocks(void) |
1871 | { | |
1872 | MigrationState *s; | |
1873 | ||
1874 | s = migrate_get_current(); | |
1875 | ||
1876 | return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS]; | |
1877 | } | |
1878 | ||
f22f928e AP |
1879 | bool migrate_postcopy_blocktime(void) |
1880 | { | |
1881 | MigrationState *s; | |
1882 | ||
1883 | s = migrate_get_current(); | |
1884 | ||
1885 | return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME]; | |
1886 | } | |
1887 | ||
8706d2d5 LL |
1888 | bool migrate_use_compression(void) |
1889 | { | |
dde4e694 LL |
1890 | MigrationState *s; |
1891 | ||
1892 | s = migrate_get_current(); | |
1893 | ||
1894 | return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS]; | |
8706d2d5 LL |
1895 | } |
1896 | ||
1897 | int migrate_compress_level(void) | |
1898 | { | |
1899 | MigrationState *s; | |
1900 | ||
1901 | s = migrate_get_current(); | |
1902 | ||
2594f56d | 1903 | return s->parameters.compress_level; |
8706d2d5 LL |
1904 | } |
1905 | ||
1906 | int migrate_compress_threads(void) | |
1907 | { | |
1908 | MigrationState *s; | |
1909 | ||
1910 | s = migrate_get_current(); | |
1911 | ||
2594f56d | 1912 | return s->parameters.compress_threads; |
8706d2d5 LL |
1913 | } |
1914 | ||
1d58872a XG |
1915 | int migrate_compress_wait_thread(void) |
1916 | { | |
1917 | MigrationState *s; | |
1918 | ||
1919 | s = migrate_get_current(); | |
1920 | ||
1921 | return s->parameters.compress_wait_thread; | |
1922 | } | |
1923 | ||
3fcb38c2 LL |
1924 | int migrate_decompress_threads(void) |
1925 | { | |
1926 | MigrationState *s; | |
1927 | ||
1928 | s = migrate_get_current(); | |
1929 | ||
2594f56d | 1930 | return s->parameters.decompress_threads; |
3fcb38c2 LL |
1931 | } |
1932 | ||
55efc8c2 VSO |
1933 | bool migrate_dirty_bitmaps(void) |
1934 | { | |
1935 | MigrationState *s; | |
1936 | ||
1937 | s = migrate_get_current(); | |
1938 | ||
1939 | return s->enabled_capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS]; | |
1940 | } | |
1941 | ||
b05dc723 JQ |
1942 | bool migrate_use_events(void) |
1943 | { | |
1944 | MigrationState *s; | |
1945 | ||
1946 | s = migrate_get_current(); | |
1947 | ||
1948 | return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS]; | |
1949 | } | |
1950 | ||
30126bbf JQ |
1951 | bool migrate_use_multifd(void) |
1952 | { | |
1953 | MigrationState *s; | |
1954 | ||
1955 | s = migrate_get_current(); | |
1956 | ||
1957 | return s->enabled_capabilities[MIGRATION_CAPABILITY_X_MULTIFD]; | |
1958 | } | |
1959 | ||
93fbd031 DDAG |
1960 | bool migrate_pause_before_switchover(void) |
1961 | { | |
1962 | MigrationState *s; | |
1963 | ||
1964 | s = migrate_get_current(); | |
1965 | ||
1966 | return s->enabled_capabilities[ | |
1967 | MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER]; | |
1968 | } | |
1969 | ||
4075fb1c JQ |
1970 | int migrate_multifd_channels(void) |
1971 | { | |
1972 | MigrationState *s; | |
1973 | ||
1974 | s = migrate_get_current(); | |
1975 | ||
1976 | return s->parameters.x_multifd_channels; | |
1977 | } | |
1978 | ||
0fb86605 JQ |
1979 | int migrate_multifd_page_count(void) |
1980 | { | |
1981 | MigrationState *s; | |
1982 | ||
1983 | s = migrate_get_current(); | |
1984 | ||
1985 | return s->parameters.x_multifd_page_count; | |
1986 | } | |
1987 | ||
17ad9b35 OW |
1988 | int migrate_use_xbzrle(void) |
1989 | { | |
1990 | MigrationState *s; | |
1991 | ||
1992 | s = migrate_get_current(); | |
1993 | ||
1994 | return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE]; | |
1995 | } | |
1996 | ||
1997 | int64_t migrate_xbzrle_cache_size(void) | |
1998 | { | |
1999 | MigrationState *s; | |
2000 | ||
2001 | s = migrate_get_current(); | |
2002 | ||
73af8dd8 | 2003 | return s->parameters.xbzrle_cache_size; |
17ad9b35 | 2004 | } |
0d82d0e8 | 2005 | |
7e555c6c DDAG |
2006 | static int64_t migrate_max_postcopy_bandwidth(void) |
2007 | { | |
2008 | MigrationState *s; | |
2009 | ||
2010 | s = migrate_get_current(); | |
2011 | ||
2012 | return s->parameters.max_postcopy_bandwidth; | |
2013 | } | |
2014 | ||
2833c59b JQ |
2015 | bool migrate_use_block(void) |
2016 | { | |
2017 | MigrationState *s; | |
2018 | ||
2019 | s = migrate_get_current(); | |
2020 | ||
2021 | return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK]; | |
2022 | } | |
2023 | ||
c788ada8 PX |
2024 | bool migrate_use_return_path(void) |
2025 | { | |
2026 | MigrationState *s; | |
2027 | ||
2028 | s = migrate_get_current(); | |
2029 | ||
2030 | return s->enabled_capabilities[MIGRATION_CAPABILITY_RETURN_PATH]; | |
2031 | } | |
2032 | ||
2833c59b JQ |
2033 | bool migrate_use_block_incremental(void) |
2034 | { | |
2035 | MigrationState *s; | |
2036 | ||
2037 | s = migrate_get_current(); | |
2038 | ||
2039 | return s->parameters.block_incremental; | |
2040 | } | |
2041 | ||
70b20477 DDAG |
2042 | /* migration thread support */ |
2043 | /* | |
2044 | * Something bad happened to the RP stream, mark an error | |
2045 | * The caller shall print or trace something to indicate why | |
2046 | */ | |
2047 | static void mark_source_rp_bad(MigrationState *s) | |
2048 | { | |
2049 | s->rp_state.error = true; | |
2050 | } | |
2051 | ||
2052 | static struct rp_cmd_args { | |
2053 | ssize_t len; /* -1 = variable */ | |
2054 | const char *name; | |
2055 | } rp_cmd_args[] = { | |
2056 | [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" }, | |
2057 | [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" }, | |
2058 | [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" }, | |
1e2d90eb DDAG |
2059 | [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" }, |
2060 | [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" }, | |
a335debb | 2061 | [MIG_RP_MSG_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" }, |
13955b89 | 2062 | [MIG_RP_MSG_RESUME_ACK] = { .len = 4, .name = "RESUME_ACK" }, |
70b20477 DDAG |
2063 | [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" }, |
2064 | }; | |
2065 | ||
1e2d90eb DDAG |
2066 | /* |
2067 | * Process a request for pages received on the return path, | |
2068 | * We're allowed to send more than requested (e.g. to round to our page size) | |
2069 | * and we don't need to send pages that have already been sent. | |
2070 | */ | |
2071 | static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname, | |
2072 | ram_addr_t start, size_t len) | |
2073 | { | |
6c595cde DDAG |
2074 | long our_host_ps = getpagesize(); |
2075 | ||
1e2d90eb | 2076 | trace_migrate_handle_rp_req_pages(rbname, start, len); |
6c595cde DDAG |
2077 | |
2078 | /* | |
2079 | * Since we currently insist on matching page sizes, just sanity check | |
2080 | * we're being asked for whole host pages. | |
2081 | */ | |
2082 | if (start & (our_host_ps-1) || | |
2083 | (len & (our_host_ps-1))) { | |
2084 | error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT | |
2085 | " len: %zd", __func__, start, len); | |
2086 | mark_source_rp_bad(ms); | |
2087 | return; | |
2088 | } | |
2089 | ||
96506894 | 2090 | if (ram_save_queue_pages(rbname, start, len)) { |
6c595cde DDAG |
2091 | mark_source_rp_bad(ms); |
2092 | } | |
1e2d90eb DDAG |
2093 | } |
2094 | ||
14b1742e PX |
2095 | /* Return true to retry, false to quit */ |
2096 | static bool postcopy_pause_return_path_thread(MigrationState *s) | |
2097 | { | |
2098 | trace_postcopy_pause_return_path(); | |
2099 | ||
2100 | qemu_sem_wait(&s->postcopy_pause_rp_sem); | |
2101 | ||
2102 | trace_postcopy_pause_return_path_continued(); | |
2103 | ||
2104 | return true; | |
2105 | } | |
2106 | ||
a335debb PX |
2107 | static int migrate_handle_rp_recv_bitmap(MigrationState *s, char *block_name) |
2108 | { | |
2109 | RAMBlock *block = qemu_ram_block_by_name(block_name); | |
2110 | ||
2111 | if (!block) { | |
2112 | error_report("%s: invalid block name '%s'", __func__, block_name); | |
2113 | return -EINVAL; | |
2114 | } | |
2115 | ||
2116 | /* Fetch the received bitmap and refresh the dirty bitmap */ | |
2117 | return ram_dirty_bitmap_reload(s, block); | |
2118 | } | |
2119 | ||
13955b89 PX |
2120 | static int migrate_handle_rp_resume_ack(MigrationState *s, uint32_t value) |
2121 | { | |
2122 | trace_source_return_path_thread_resume_ack(value); | |
2123 | ||
2124 | if (value != MIGRATION_RESUME_ACK_VALUE) { | |
2125 | error_report("%s: illegal resume_ack value %"PRIu32, | |
2126 | __func__, value); | |
2127 | return -1; | |
2128 | } | |
2129 | ||
2130 | /* Now both sides are active. */ | |
2131 | migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER, | |
2132 | MIGRATION_STATUS_POSTCOPY_ACTIVE); | |
2133 | ||
94190696 PX |
2134 | /* Notify send thread that time to continue send pages */ |
2135 | qemu_sem_post(&s->rp_state.rp_sem); | |
13955b89 PX |
2136 | |
2137 | return 0; | |
2138 | } | |
2139 | ||
70b20477 DDAG |
2140 | /* |
2141 | * Handles messages sent on the return path towards the source VM | |
2142 | * | |
2143 | */ | |
2144 | static void *source_return_path_thread(void *opaque) | |
2145 | { | |
2146 | MigrationState *ms = opaque; | |
2147 | QEMUFile *rp = ms->rp_state.from_dst_file; | |
2148 | uint16_t header_len, header_type; | |
568b01ca | 2149 | uint8_t buf[512]; |
70b20477 | 2150 | uint32_t tmp32, sibling_error; |
1e2d90eb DDAG |
2151 | ram_addr_t start = 0; /* =0 to silence warning */ |
2152 | size_t len = 0, expected_len; | |
70b20477 DDAG |
2153 | int res; |
2154 | ||
2155 | trace_source_return_path_thread_entry(); | |
74637e6f | 2156 | rcu_register_thread(); |
14b1742e PX |
2157 | |
2158 | retry: | |
70b20477 DDAG |
2159 | while (!ms->rp_state.error && !qemu_file_get_error(rp) && |
2160 | migration_is_setup_or_active(ms->state)) { | |
2161 | trace_source_return_path_thread_loop_top(); | |
2162 | header_type = qemu_get_be16(rp); | |
2163 | header_len = qemu_get_be16(rp); | |
2164 | ||
7a9ddfbf PX |
2165 | if (qemu_file_get_error(rp)) { |
2166 | mark_source_rp_bad(ms); | |
2167 | goto out; | |
2168 | } | |
2169 | ||
70b20477 DDAG |
2170 | if (header_type >= MIG_RP_MSG_MAX || |
2171 | header_type == MIG_RP_MSG_INVALID) { | |
2172 | error_report("RP: Received invalid message 0x%04x length 0x%04x", | |
2173 | header_type, header_len); | |
2174 | mark_source_rp_bad(ms); | |
2175 | goto out; | |
2176 | } | |
2177 | ||
2178 | if ((rp_cmd_args[header_type].len != -1 && | |
2179 | header_len != rp_cmd_args[header_type].len) || | |
568b01ca | 2180 | header_len > sizeof(buf)) { |
70b20477 DDAG |
2181 | error_report("RP: Received '%s' message (0x%04x) with" |
2182 | "incorrect length %d expecting %zu", | |
2183 | rp_cmd_args[header_type].name, header_type, header_len, | |
2184 | (size_t)rp_cmd_args[header_type].len); | |
2185 | mark_source_rp_bad(ms); | |
2186 | goto out; | |
2187 | } | |
2188 | ||
2189 | /* We know we've got a valid header by this point */ | |
2190 | res = qemu_get_buffer(rp, buf, header_len); | |
2191 | if (res != header_len) { | |
2192 | error_report("RP: Failed reading data for message 0x%04x" | |
2193 | " read %d expected %d", | |
2194 | header_type, res, header_len); | |
2195 | mark_source_rp_bad(ms); | |
2196 | goto out; | |
2197 | } | |
2198 | ||
2199 | /* OK, we have the message and the data */ | |
2200 | switch (header_type) { | |
2201 | case MIG_RP_MSG_SHUT: | |
4d885131 | 2202 | sibling_error = ldl_be_p(buf); |
70b20477 DDAG |
2203 | trace_source_return_path_thread_shut(sibling_error); |
2204 | if (sibling_error) { | |
2205 | error_report("RP: Sibling indicated error %d", sibling_error); | |
2206 | mark_source_rp_bad(ms); | |
2207 | } | |
2208 | /* | |
2209 | * We'll let the main thread deal with closing the RP | |
2210 | * we could do a shutdown(2) on it, but we're the only user | |
2211 | * anyway, so there's nothing gained. | |
2212 | */ | |
2213 | goto out; | |
2214 | ||
2215 | case MIG_RP_MSG_PONG: | |
4d885131 | 2216 | tmp32 = ldl_be_p(buf); |
70b20477 DDAG |
2217 | trace_source_return_path_thread_pong(tmp32); |
2218 | break; | |
2219 | ||
1e2d90eb | 2220 | case MIG_RP_MSG_REQ_PAGES: |
4d885131 PM |
2221 | start = ldq_be_p(buf); |
2222 | len = ldl_be_p(buf + 8); | |
1e2d90eb DDAG |
2223 | migrate_handle_rp_req_pages(ms, NULL, start, len); |
2224 | break; | |
2225 | ||
2226 | case MIG_RP_MSG_REQ_PAGES_ID: | |
2227 | expected_len = 12 + 1; /* header + termination */ | |
2228 | ||
2229 | if (header_len >= expected_len) { | |
4d885131 PM |
2230 | start = ldq_be_p(buf); |
2231 | len = ldl_be_p(buf + 8); | |
1e2d90eb DDAG |
2232 | /* Now we expect an idstr */ |
2233 | tmp32 = buf[12]; /* Length of the following idstr */ | |
2234 | buf[13 + tmp32] = '\0'; | |
2235 | expected_len += tmp32; | |
2236 | } | |
2237 | if (header_len != expected_len) { | |
2238 | error_report("RP: Req_Page_id with length %d expecting %zd", | |
2239 | header_len, expected_len); | |
2240 | mark_source_rp_bad(ms); | |
2241 | goto out; | |
2242 | } | |
2243 | migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len); | |
2244 | break; | |
2245 | ||
a335debb PX |
2246 | case MIG_RP_MSG_RECV_BITMAP: |
2247 | if (header_len < 1) { | |
2248 | error_report("%s: missing block name", __func__); | |
2249 | mark_source_rp_bad(ms); | |
2250 | goto out; | |
2251 | } | |
2252 | /* Format: len (1B) + idstr (<255B). This ends the idstr. */ | |
2253 | buf[buf[0] + 1] = '\0'; | |
2254 | if (migrate_handle_rp_recv_bitmap(ms, (char *)(buf + 1))) { | |
2255 | mark_source_rp_bad(ms); | |
2256 | goto out; | |
2257 | } | |
2258 | break; | |
2259 | ||
13955b89 PX |
2260 | case MIG_RP_MSG_RESUME_ACK: |
2261 | tmp32 = ldl_be_p(buf); | |
2262 | if (migrate_handle_rp_resume_ack(ms, tmp32)) { | |
2263 | mark_source_rp_bad(ms); | |
2264 | goto out; | |
2265 | } | |
2266 | break; | |
2267 | ||
70b20477 DDAG |
2268 | default: |
2269 | break; | |
2270 | } | |
2271 | } | |
14b1742e PX |
2272 | |
2273 | out: | |
2274 | res = qemu_file_get_error(rp); | |
2275 | if (res) { | |
2276 | if (res == -EIO) { | |
2277 | /* | |
2278 | * Maybe there is something we can do: it looks like a | |
2279 | * network down issue, and we pause for a recovery. | |
2280 | */ | |
2281 | if (postcopy_pause_return_path_thread(ms)) { | |
2282 | /* Reload rp, reset the rest */ | |
2283 | rp = ms->rp_state.from_dst_file; | |
2284 | ms->rp_state.error = false; | |
2285 | goto retry; | |
2286 | } | |
2287 | } | |
2288 | ||
70b20477 DDAG |
2289 | trace_source_return_path_thread_bad_end(); |
2290 | mark_source_rp_bad(ms); | |
2291 | } | |
2292 | ||
2293 | trace_source_return_path_thread_end(); | |
70b20477 DDAG |
2294 | ms->rp_state.from_dst_file = NULL; |
2295 | qemu_fclose(rp); | |
74637e6f | 2296 | rcu_unregister_thread(); |
70b20477 DDAG |
2297 | return NULL; |
2298 | } | |
2299 | ||
d3e35b8f PX |
2300 | static int open_return_path_on_source(MigrationState *ms, |
2301 | bool create_thread) | |
70b20477 DDAG |
2302 | { |
2303 | ||
89a02a9f | 2304 | ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file); |
70b20477 DDAG |
2305 | if (!ms->rp_state.from_dst_file) { |
2306 | return -1; | |
2307 | } | |
2308 | ||
2309 | trace_open_return_path_on_source(); | |
d3e35b8f PX |
2310 | |
2311 | if (!create_thread) { | |
2312 | /* We're done */ | |
2313 | return 0; | |
2314 | } | |
2315 | ||
70b20477 DDAG |
2316 | qemu_thread_create(&ms->rp_state.rp_thread, "return path", |
2317 | source_return_path_thread, ms, QEMU_THREAD_JOINABLE); | |
2318 | ||
2319 | trace_open_return_path_on_source_continue(); | |
2320 | ||
2321 | return 0; | |
2322 | } | |
2323 | ||
70b20477 DDAG |
2324 | /* Returns 0 if the RP was ok, otherwise there was an error on the RP */ |
2325 | static int await_return_path_close_on_source(MigrationState *ms) | |
2326 | { | |
2327 | /* | |
2328 | * If this is a normal exit then the destination will send a SHUT and the | |
2329 | * rp_thread will exit, however if there's an error we need to cause | |
2330 | * it to exit. | |
2331 | */ | |
89a02a9f | 2332 | if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) { |
70b20477 DDAG |
2333 | /* |
2334 | * shutdown(2), if we have it, will cause it to unblock if it's stuck | |
2335 | * waiting for the destination. | |
2336 | */ | |
2337 | qemu_file_shutdown(ms->rp_state.from_dst_file); | |
2338 | mark_source_rp_bad(ms); | |
2339 | } | |
2340 | trace_await_return_path_close_on_source_joining(); | |
2341 | qemu_thread_join(&ms->rp_state.rp_thread); | |
2342 | trace_await_return_path_close_on_source_close(); | |
2343 | return ms->rp_state.error; | |
2344 | } | |
2345 | ||
1d34e4bf DDAG |
2346 | /* |
2347 | * Switch from normal iteration to postcopy | |
2348 | * Returns non-0 on error | |
2349 | */ | |
7287cbd4 | 2350 | static int postcopy_start(MigrationState *ms) |
1d34e4bf DDAG |
2351 | { |
2352 | int ret; | |
61b67d47 DB |
2353 | QIOChannelBuffer *bioc; |
2354 | QEMUFile *fb; | |
1d34e4bf | 2355 | int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
7e555c6c | 2356 | int64_t bandwidth = migrate_max_postcopy_bandwidth(); |
ef8d6488 | 2357 | bool restart_block = false; |
0331c8ca DDAG |
2358 | int cur_state = MIGRATION_STATUS_ACTIVE; |
2359 | if (!migrate_pause_before_switchover()) { | |
2360 | migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE, | |
2361 | MIGRATION_STATUS_POSTCOPY_ACTIVE); | |
2362 | } | |
1d34e4bf DDAG |
2363 | |
2364 | trace_postcopy_start(); | |
2365 | qemu_mutex_lock_iothread(); | |
2366 | trace_postcopy_start_set_run(); | |
2367 | ||
2368 | qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER); | |
1d34e4bf DDAG |
2369 | global_state_store(); |
2370 | ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE); | |
76b1c7fe KW |
2371 | if (ret < 0) { |
2372 | goto fail; | |
2373 | } | |
1d34e4bf | 2374 | |
0331c8ca DDAG |
2375 | ret = migration_maybe_pause(ms, &cur_state, |
2376 | MIGRATION_STATUS_POSTCOPY_ACTIVE); | |
2377 | if (ret < 0) { | |
2378 | goto fail; | |
2379 | } | |
2380 | ||
76b1c7fe | 2381 | ret = bdrv_inactivate_all(); |
1d34e4bf DDAG |
2382 | if (ret < 0) { |
2383 | goto fail; | |
2384 | } | |
ef8d6488 | 2385 | restart_block = true; |
1d34e4bf | 2386 | |
1c0d249d DDAG |
2387 | /* |
2388 | * Cause any non-postcopiable, but iterative devices to | |
2389 | * send out their final data. | |
2390 | */ | |
a1fbe750 | 2391 | qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false); |
1c0d249d | 2392 | |
1d34e4bf DDAG |
2393 | /* |
2394 | * in Finish migrate and with the io-lock held everything should | |
2395 | * be quiet, but we've potentially still got dirty pages and we | |
2396 | * need to tell the destination to throw any pages it's already received | |
2397 | * that are dirty | |
2398 | */ | |
58110f0a VSO |
2399 | if (migrate_postcopy_ram()) { |
2400 | if (ram_postcopy_send_discard_bitmap(ms)) { | |
2401 | error_report("postcopy send discard bitmap failed"); | |
2402 | goto fail; | |
2403 | } | |
1d34e4bf DDAG |
2404 | } |
2405 | ||
2406 | /* | |
2407 | * send rest of state - note things that are doing postcopy | |
2408 | * will notice we're in POSTCOPY_ACTIVE and not actually | |
2409 | * wrap their state up here | |
2410 | */ | |
7e555c6c DDAG |
2411 | /* 0 max-postcopy-bandwidth means unlimited */ |
2412 | if (!bandwidth) { | |
2413 | qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX); | |
2414 | } else { | |
2415 | qemu_file_set_rate_limit(ms->to_dst_file, bandwidth / XFER_LIMIT_RATIO); | |
2416 | } | |
58110f0a VSO |
2417 | if (migrate_postcopy_ram()) { |
2418 | /* Ping just for debugging, helps line traces up */ | |
2419 | qemu_savevm_send_ping(ms->to_dst_file, 2); | |
2420 | } | |
1d34e4bf DDAG |
2421 | |
2422 | /* | |
2423 | * While loading the device state we may trigger page transfer | |
2424 | * requests and the fd must be free to process those, and thus | |
2425 | * the destination must read the whole device state off the fd before | |
2426 | * it starts processing it. Unfortunately the ad-hoc migration format | |
2427 | * doesn't allow the destination to know the size to read without fully | |
2428 | * parsing it through each devices load-state code (especially the open | |
2429 | * coded devices that use get/put). | |
2430 | * So we wrap the device state up in a package with a length at the start; | |
2431 | * to do this we use a qemu_buf to hold the whole of the device state. | |
2432 | */ | |
61b67d47 | 2433 | bioc = qio_channel_buffer_new(4096); |
6f01f136 | 2434 | qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer"); |
61b67d47 DB |
2435 | fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc)); |
2436 | object_unref(OBJECT(bioc)); | |
1d34e4bf | 2437 | |
c76201ab DDAG |
2438 | /* |
2439 | * Make sure the receiver can get incoming pages before we send the rest | |
2440 | * of the state | |
2441 | */ | |
2442 | qemu_savevm_send_postcopy_listen(fb); | |
2443 | ||
a1fbe750 | 2444 | qemu_savevm_state_complete_precopy(fb, false, false); |
58110f0a VSO |
2445 | if (migrate_postcopy_ram()) { |
2446 | qemu_savevm_send_ping(fb, 3); | |
2447 | } | |
1d34e4bf DDAG |
2448 | |
2449 | qemu_savevm_send_postcopy_run(fb); | |
2450 | ||
2451 | /* <><> end of stuff going into the package */ | |
1d34e4bf | 2452 | |
ef8d6488 DDAG |
2453 | /* Last point of recovery; as soon as we send the package the destination |
2454 | * can open devices and potentially start running. | |
2455 | * Lets just check again we've not got any errors. | |
2456 | */ | |
2457 | ret = qemu_file_get_error(ms->to_dst_file); | |
2458 | if (ret) { | |
2459 | error_report("postcopy_start: Migration stream errored (pre package)"); | |
2460 | goto fail_closefb; | |
2461 | } | |
2462 | ||
2463 | restart_block = false; | |
2464 | ||
1d34e4bf | 2465 | /* Now send that blob */ |
61b67d47 | 2466 | if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) { |
1d34e4bf DDAG |
2467 | goto fail_closefb; |
2468 | } | |
2469 | qemu_fclose(fb); | |
b82fc321 DDAG |
2470 | |
2471 | /* Send a notify to give a chance for anything that needs to happen | |
2472 | * at the transition to postcopy and after the device state; in particular | |
2473 | * spice needs to trigger a transition now | |
2474 | */ | |
2475 | ms->postcopy_after_devices = true; | |
2476 | notifier_list_notify(&migration_state_notifiers, ms); | |
2477 | ||
1d34e4bf DDAG |
2478 | ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop; |
2479 | ||
2480 | qemu_mutex_unlock_iothread(); | |
2481 | ||
58110f0a VSO |
2482 | if (migrate_postcopy_ram()) { |
2483 | /* | |
2484 | * Although this ping is just for debug, it could potentially be | |
2485 | * used for getting a better measurement of downtime at the source. | |
2486 | */ | |
2487 | qemu_savevm_send_ping(ms->to_dst_file, 4); | |
2488 | } | |
1d34e4bf | 2489 | |
ced1c616 PB |
2490 | if (migrate_release_ram()) { |
2491 | ram_postcopy_migrated_memory_release(ms); | |
2492 | } | |
2493 | ||
89a02a9f | 2494 | ret = qemu_file_get_error(ms->to_dst_file); |
1d34e4bf DDAG |
2495 | if (ret) { |
2496 | error_report("postcopy_start: Migration stream errored"); | |
48781e5b | 2497 | migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE, |
1d34e4bf DDAG |
2498 | MIGRATION_STATUS_FAILED); |
2499 | } | |
2500 | ||
2501 | return ret; | |
2502 | ||
2503 | fail_closefb: | |
2504 | qemu_fclose(fb); | |
2505 | fail: | |
48781e5b | 2506 | migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE, |
1d34e4bf | 2507 | MIGRATION_STATUS_FAILED); |
ef8d6488 DDAG |
2508 | if (restart_block) { |
2509 | /* A failure happened early enough that we know the destination hasn't | |
2510 | * accessed block devices, so we're safe to recover. | |
2511 | */ | |
2512 | Error *local_err = NULL; | |
2513 | ||
2514 | bdrv_invalidate_cache_all(&local_err); | |
2515 | if (local_err) { | |
2516 | error_report_err(local_err); | |
2517 | } | |
2518 | } | |
1d34e4bf DDAG |
2519 | qemu_mutex_unlock_iothread(); |
2520 | return -1; | |
2521 | } | |
2522 | ||
e91d8951 DDAG |
2523 | /** |
2524 | * migration_maybe_pause: Pause if required to by | |
2525 | * migrate_pause_before_switchover called with the iothread locked | |
2526 | * Returns: 0 on success | |
2527 | */ | |
0331c8ca DDAG |
2528 | static int migration_maybe_pause(MigrationState *s, |
2529 | int *current_active_state, | |
2530 | int new_state) | |
e91d8951 DDAG |
2531 | { |
2532 | if (!migrate_pause_before_switchover()) { | |
2533 | return 0; | |
2534 | } | |
2535 | ||
2536 | /* Since leaving this state is not atomic with posting the semaphore | |
2537 | * it's possible that someone could have issued multiple migrate_continue | |
2538 | * and the semaphore is incorrectly positive at this point; | |
2539 | * the docs say it's undefined to reinit a semaphore that's already | |
2540 | * init'd, so use timedwait to eat up any existing posts. | |
2541 | */ | |
2542 | while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) { | |
2543 | /* This block intentionally left blank */ | |
2544 | } | |
2545 | ||
2546 | qemu_mutex_unlock_iothread(); | |
2547 | migrate_set_state(&s->state, *current_active_state, | |
2548 | MIGRATION_STATUS_PRE_SWITCHOVER); | |
2549 | qemu_sem_wait(&s->pause_sem); | |
2550 | migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER, | |
0331c8ca DDAG |
2551 | new_state); |
2552 | *current_active_state = new_state; | |
e91d8951 DDAG |
2553 | qemu_mutex_lock_iothread(); |
2554 | ||
0331c8ca | 2555 | return s->state == new_state ? 0 : -EINVAL; |
e91d8951 DDAG |
2556 | } |
2557 | ||
09f6c85e DDAG |
2558 | /** |
2559 | * migration_completion: Used by migration_thread when there's not much left. | |
2560 | * The caller 'breaks' the loop when this returns. | |
2561 | * | |
2562 | * @s: Current migration state | |
09f6c85e | 2563 | */ |
2ad87305 | 2564 | static void migration_completion(MigrationState *s) |
09f6c85e DDAG |
2565 | { |
2566 | int ret; | |
2ad87305 | 2567 | int current_active_state = s->state; |
09f6c85e | 2568 | |
b10ac0c4 DDAG |
2569 | if (s->state == MIGRATION_STATUS_ACTIVE) { |
2570 | qemu_mutex_lock_iothread(); | |
64909f97 | 2571 | s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
b10ac0c4 | 2572 | qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER); |
7287cbd4 | 2573 | s->vm_was_running = runstate_is_running(); |
b10ac0c4 DDAG |
2574 | ret = global_state_store(); |
2575 | ||
2576 | if (!ret) { | |
a1fbe750 | 2577 | bool inactivate = !migrate_colo_enabled(); |
b10ac0c4 | 2578 | ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE); |
e91d8951 | 2579 | if (ret >= 0) { |
0331c8ca DDAG |
2580 | ret = migration_maybe_pause(s, ¤t_active_state, |
2581 | MIGRATION_STATUS_DEVICE); | |
e91d8951 | 2582 | } |
f07fa4cb KW |
2583 | if (ret >= 0) { |
2584 | qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX); | |
a1fbe750 FZ |
2585 | ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false, |
2586 | inactivate); | |
f07fa4cb | 2587 | } |
a1fbe750 FZ |
2588 | if (inactivate && ret >= 0) { |
2589 | s->block_inactive = true; | |
b10ac0c4 DDAG |
2590 | } |
2591 | } | |
2592 | qemu_mutex_unlock_iothread(); | |
09f6c85e | 2593 | |
b10ac0c4 DDAG |
2594 | if (ret < 0) { |
2595 | goto fail; | |
09f6c85e | 2596 | } |
b10ac0c4 DDAG |
2597 | } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) { |
2598 | trace_migration_completion_postcopy_end(); | |
2599 | ||
89a02a9f | 2600 | qemu_savevm_state_complete_postcopy(s->to_dst_file); |
b10ac0c4 | 2601 | trace_migration_completion_postcopy_end_after_complete(); |
09f6c85e | 2602 | } |
09f6c85e | 2603 | |
b10ac0c4 DDAG |
2604 | /* |
2605 | * If rp was opened we must clean up the thread before | |
2606 | * cleaning everything else up (since if there are no failures | |
2607 | * it will wait for the destination to send it's status in | |
2608 | * a SHUT command). | |
b10ac0c4 | 2609 | */ |
0425dc97 | 2610 | if (s->rp_state.from_dst_file) { |
b10ac0c4 | 2611 | int rp_error; |
0425dc97 | 2612 | trace_migration_return_path_end_before(); |
b10ac0c4 | 2613 | rp_error = await_return_path_close_on_source(s); |
0425dc97 | 2614 | trace_migration_return_path_end_after(rp_error); |
b10ac0c4 | 2615 | if (rp_error) { |
fe904ea8 | 2616 | goto fail_invalidate; |
b10ac0c4 | 2617 | } |
09f6c85e DDAG |
2618 | } |
2619 | ||
89a02a9f | 2620 | if (qemu_file_get_error(s->to_dst_file)) { |
09f6c85e | 2621 | trace_migration_completion_file_err(); |
fe904ea8 | 2622 | goto fail_invalidate; |
09f6c85e DDAG |
2623 | } |
2624 | ||
0b827d5e HZ |
2625 | if (!migrate_colo_enabled()) { |
2626 | migrate_set_state(&s->state, current_active_state, | |
2627 | MIGRATION_STATUS_COMPLETED); | |
2628 | } | |
2629 | ||
09f6c85e DDAG |
2630 | return; |
2631 | ||
fe904ea8 GK |
2632 | fail_invalidate: |
2633 | /* If not doing postcopy, vm_start() will be called: let's regain | |
2634 | * control on images. | |
2635 | */ | |
6039dd5b DDAG |
2636 | if (s->state == MIGRATION_STATUS_ACTIVE || |
2637 | s->state == MIGRATION_STATUS_DEVICE) { | |
fe904ea8 GK |
2638 | Error *local_err = NULL; |
2639 | ||
1d2acc31 | 2640 | qemu_mutex_lock_iothread(); |
fe904ea8 GK |
2641 | bdrv_invalidate_cache_all(&local_err); |
2642 | if (local_err) { | |
2643 | error_report_err(local_err); | |
1d2acc31 HZ |
2644 | } else { |
2645 | s->block_inactive = false; | |
fe904ea8 | 2646 | } |
1d2acc31 | 2647 | qemu_mutex_unlock_iothread(); |
fe904ea8 GK |
2648 | } |
2649 | ||
09f6c85e | 2650 | fail: |
48781e5b HZ |
2651 | migrate_set_state(&s->state, current_active_state, |
2652 | MIGRATION_STATUS_FAILED); | |
09f6c85e DDAG |
2653 | } |
2654 | ||
35a6ed4f HZ |
2655 | bool migrate_colo_enabled(void) |
2656 | { | |
2657 | MigrationState *s = migrate_get_current(); | |
2658 | return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO]; | |
2659 | } | |
2660 | ||
b23c2ade PX |
2661 | typedef enum MigThrError { |
2662 | /* No error detected */ | |
2663 | MIG_THR_ERR_NONE = 0, | |
2664 | /* Detected error, but resumed successfully */ | |
2665 | MIG_THR_ERR_RECOVERED = 1, | |
2666 | /* Detected fatal error, need to exit */ | |
2667 | MIG_THR_ERR_FATAL = 2, | |
2668 | } MigThrError; | |
2669 | ||
94190696 PX |
2670 | static int postcopy_resume_handshake(MigrationState *s) |
2671 | { | |
2672 | qemu_savevm_send_postcopy_resume(s->to_dst_file); | |
2673 | ||
2674 | while (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) { | |
2675 | qemu_sem_wait(&s->rp_state.rp_sem); | |
2676 | } | |
2677 | ||
2678 | if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) { | |
2679 | return 0; | |
2680 | } | |
2681 | ||
2682 | return -1; | |
2683 | } | |
2684 | ||
135b87b4 PX |
2685 | /* Return zero if success, or <0 for error */ |
2686 | static int postcopy_do_resume(MigrationState *s) | |
2687 | { | |
d1b8eadb PX |
2688 | int ret; |
2689 | ||
2690 | /* | |
2691 | * Call all the resume_prepare() hooks, so that modules can be | |
2692 | * ready for the migration resume. | |
2693 | */ | |
2694 | ret = qemu_savevm_state_resume_prepare(s); | |
2695 | if (ret) { | |
2696 | error_report("%s: resume_prepare() failure detected: %d", | |
2697 | __func__, ret); | |
2698 | return ret; | |
2699 | } | |
2700 | ||
2701 | /* | |
94190696 PX |
2702 | * Last handshake with destination on the resume (destination will |
2703 | * switch to postcopy-active afterwards) | |
d1b8eadb | 2704 | */ |
94190696 PX |
2705 | ret = postcopy_resume_handshake(s); |
2706 | if (ret) { | |
2707 | error_report("%s: handshake failed: %d", __func__, ret); | |
2708 | return ret; | |
2709 | } | |
d1b8eadb | 2710 | |
135b87b4 PX |
2711 | return 0; |
2712 | } | |
2713 | ||
b23c2ade PX |
2714 | /* |
2715 | * We don't return until we are in a safe state to continue current | |
2716 | * postcopy migration. Returns MIG_THR_ERR_RECOVERED if recovered, or | |
2717 | * MIG_THR_ERR_FATAL if unrecovery failure happened. | |
2718 | */ | |
2719 | static MigThrError postcopy_pause(MigrationState *s) | |
2720 | { | |
2721 | assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE); | |
b23c2ade | 2722 | |
135b87b4 | 2723 | while (true) { |
62df066f PX |
2724 | QEMUFile *file; |
2725 | ||
135b87b4 PX |
2726 | migrate_set_state(&s->state, s->state, |
2727 | MIGRATION_STATUS_POSTCOPY_PAUSED); | |
b23c2ade | 2728 | |
135b87b4 PX |
2729 | /* Current channel is possibly broken. Release it. */ |
2730 | assert(s->to_dst_file); | |
62df066f PX |
2731 | qemu_mutex_lock(&s->qemu_file_lock); |
2732 | file = s->to_dst_file; | |
135b87b4 | 2733 | s->to_dst_file = NULL; |
62df066f PX |
2734 | qemu_mutex_unlock(&s->qemu_file_lock); |
2735 | ||
2736 | qemu_file_shutdown(file); | |
2737 | qemu_fclose(file); | |
b23c2ade | 2738 | |
135b87b4 PX |
2739 | error_report("Detected IO failure for postcopy. " |
2740 | "Migration paused."); | |
b23c2ade | 2741 | |
135b87b4 PX |
2742 | /* |
2743 | * We wait until things fixed up. Then someone will setup the | |
2744 | * status back for us. | |
2745 | */ | |
2746 | while (s->state == MIGRATION_STATUS_POSTCOPY_PAUSED) { | |
2747 | qemu_sem_wait(&s->postcopy_pause_sem); | |
2748 | } | |
2749 | ||
2750 | if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) { | |
2751 | /* Woken up by a recover procedure. Give it a shot */ | |
b23c2ade | 2752 | |
135b87b4 PX |
2753 | /* |
2754 | * Firstly, let's wake up the return path now, with a new | |
2755 | * return path channel. | |
2756 | */ | |
2757 | qemu_sem_post(&s->postcopy_pause_rp_sem); | |
2758 | ||
2759 | /* Do the resume logic */ | |
2760 | if (postcopy_do_resume(s) == 0) { | |
2761 | /* Let's continue! */ | |
2762 | trace_postcopy_pause_continued(); | |
2763 | return MIG_THR_ERR_RECOVERED; | |
2764 | } else { | |
2765 | /* | |
2766 | * Something wrong happened during the recovery, let's | |
2767 | * pause again. Pause is always better than throwing | |
2768 | * data away. | |
2769 | */ | |
2770 | continue; | |
2771 | } | |
2772 | } else { | |
2773 | /* This is not right... Time to quit. */ | |
2774 | return MIG_THR_ERR_FATAL; | |
2775 | } | |
2776 | } | |
b23c2ade PX |
2777 | } |
2778 | ||
2779 | static MigThrError migration_detect_error(MigrationState *s) | |
2780 | { | |
2781 | int ret; | |
2782 | ||
2783 | /* Try to detect any file errors */ | |
2784 | ret = qemu_file_get_error(s->to_dst_file); | |
2785 | ||
2786 | if (!ret) { | |
2787 | /* Everything is fine */ | |
2788 | return MIG_THR_ERR_NONE; | |
2789 | } | |
2790 | ||
2791 | if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE && ret == -EIO) { | |
2792 | /* | |
2793 | * For postcopy, we allow the network to be down for a | |
2794 | * while. After that, it can be continued by a | |
2795 | * recovery phase. | |
2796 | */ | |
2797 | return postcopy_pause(s); | |
2798 | } else { | |
2799 | /* | |
2800 | * For precopy (or postcopy with error outside IO), we fail | |
2801 | * with no time. | |
2802 | */ | |
2803 | migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED); | |
2804 | trace_migration_thread_file_err(); | |
2805 | ||
2806 | /* Time to stop the migration, now. */ | |
2807 | return MIG_THR_ERR_FATAL; | |
2808 | } | |
2809 | } | |
2810 | ||
0c8f0efd JQ |
2811 | /* How many bytes have we transferred since the beggining of the migration */ |
2812 | static uint64_t migration_total_bytes(MigrationState *s) | |
2813 | { | |
b9ee2f7d | 2814 | return qemu_ftell(s->to_dst_file) + ram_counters.multifd_bytes; |
0c8f0efd JQ |
2815 | } |
2816 | ||
cf011f08 PX |
2817 | static void migration_calculate_complete(MigrationState *s) |
2818 | { | |
0c8f0efd | 2819 | uint64_t bytes = migration_total_bytes(s); |
cf011f08 | 2820 | int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
6cde6fbe | 2821 | int64_t transfer_time; |
cf011f08 PX |
2822 | |
2823 | s->total_time = end_time - s->start_time; | |
2824 | if (!s->downtime) { | |
2825 | /* | |
2826 | * It's still not set, so we are precopy migration. For | |
2827 | * postcopy, downtime is calculated during postcopy_start(). | |
2828 | */ | |
2829 | s->downtime = end_time - s->downtime_start; | |
2830 | } | |
2831 | ||
6cde6fbe JQ |
2832 | transfer_time = s->total_time - s->setup_time; |
2833 | if (transfer_time) { | |
2834 | s->mbps = ((double) bytes * 8.0) / transfer_time / 1000; | |
cf011f08 PX |
2835 | } |
2836 | } | |
2837 | ||
b15df1ae PX |
2838 | static void migration_update_counters(MigrationState *s, |
2839 | int64_t current_time) | |
2840 | { | |
2841 | uint64_t transferred, time_spent; | |
0c8f0efd | 2842 | uint64_t current_bytes; /* bytes transferred since the beginning */ |
b15df1ae PX |
2843 | double bandwidth; |
2844 | ||
2845 | if (current_time < s->iteration_start_time + BUFFER_DELAY) { | |
2846 | return; | |
2847 | } | |
2848 | ||
0c8f0efd JQ |
2849 | current_bytes = migration_total_bytes(s); |
2850 | transferred = current_bytes - s->iteration_initial_bytes; | |
b15df1ae PX |
2851 | time_spent = current_time - s->iteration_start_time; |
2852 | bandwidth = (double)transferred / time_spent; | |
0781c1ed | 2853 | s->threshold_size = bandwidth * s->parameters.downtime_limit; |
b15df1ae PX |
2854 | |
2855 | s->mbps = (((double) transferred * 8.0) / | |
2856 | ((double) time_spent / 1000.0)) / 1000.0 / 1000.0; | |
2857 | ||
2858 | /* | |
2859 | * if we haven't sent anything, we don't want to | |
2860 | * recalculate. 10000 is a small enough number for our purposes | |
2861 | */ | |
2862 | if (ram_counters.dirty_pages_rate && transferred > 10000) { | |
650af890 | 2863 | s->expected_downtime = ram_counters.remaining / bandwidth; |
b15df1ae PX |
2864 | } |
2865 | ||
2866 | qemu_file_reset_rate_limit(s->to_dst_file); | |
2867 | ||
2868 | s->iteration_start_time = current_time; | |
0c8f0efd | 2869 | s->iteration_initial_bytes = current_bytes; |
b15df1ae PX |
2870 | |
2871 | trace_migrate_transferred(transferred, time_spent, | |
0781c1ed | 2872 | bandwidth, s->threshold_size); |
b15df1ae PX |
2873 | } |
2874 | ||
2ad87305 PX |
2875 | /* Migration thread iteration status */ |
2876 | typedef enum { | |
2877 | MIG_ITERATE_RESUME, /* Resume current iteration */ | |
2878 | MIG_ITERATE_SKIP, /* Skip current iteration */ | |
2879 | MIG_ITERATE_BREAK, /* Break the loop */ | |
2880 | } MigIterateState; | |
2881 | ||
2882 | /* | |
2883 | * Return true if continue to the next iteration directly, false | |
2884 | * otherwise. | |
2885 | */ | |
2886 | static MigIterateState migration_iteration_run(MigrationState *s) | |
2887 | { | |
47995026 | 2888 | uint64_t pending_size, pend_pre, pend_compat, pend_post; |
2ad87305 PX |
2889 | bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE; |
2890 | ||
47995026 VSO |
2891 | qemu_savevm_state_pending(s->to_dst_file, s->threshold_size, &pend_pre, |
2892 | &pend_compat, &pend_post); | |
2893 | pending_size = pend_pre + pend_compat + pend_post; | |
2ad87305 PX |
2894 | |
2895 | trace_migrate_pending(pending_size, s->threshold_size, | |
47995026 | 2896 | pend_pre, pend_compat, pend_post); |
2ad87305 PX |
2897 | |
2898 | if (pending_size && pending_size >= s->threshold_size) { | |
2899 | /* Still a significant amount to transfer */ | |
2900 | if (migrate_postcopy() && !in_postcopy && | |
47995026 | 2901 | pend_pre <= s->threshold_size && |
2ad87305 PX |
2902 | atomic_read(&s->start_postcopy)) { |
2903 | if (postcopy_start(s)) { | |
2904 | error_report("%s: postcopy failed to start", __func__); | |
2905 | } | |
2906 | return MIG_ITERATE_SKIP; | |
2907 | } | |
2908 | /* Just another iteration step */ | |
2909 | qemu_savevm_state_iterate(s->to_dst_file, | |
2910 | s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE); | |
2911 | } else { | |
2912 | trace_migration_thread_low_pending(pending_size); | |
2913 | migration_completion(s); | |
2914 | return MIG_ITERATE_BREAK; | |
2915 | } | |
2916 | ||
2917 | return MIG_ITERATE_RESUME; | |
2918 | } | |
2919 | ||
199aa6d4 PX |
2920 | static void migration_iteration_finish(MigrationState *s) |
2921 | { | |
2922 | /* If we enabled cpu throttling for auto-converge, turn it off. */ | |
2923 | cpu_throttle_stop(); | |
2924 | ||
2925 | qemu_mutex_lock_iothread(); | |
2926 | switch (s->state) { | |
2927 | case MIGRATION_STATUS_COMPLETED: | |
2928 | migration_calculate_complete(s); | |
2929 | runstate_set(RUN_STATE_POSTMIGRATE); | |
2930 | break; | |
2931 | ||
2932 | case MIGRATION_STATUS_ACTIVE: | |
2933 | /* | |
2934 | * We should really assert here, but since it's during | |
2935 | * migration, let's try to reduce the usage of assertions. | |
2936 | */ | |
2937 | if (!migrate_colo_enabled()) { | |
2938 | error_report("%s: critical error: calling COLO code without " | |
2939 | "COLO enabled", __func__); | |
2940 | } | |
2941 | migrate_start_colo_process(s); | |
2942 | /* | |
2943 | * Fixme: we will run VM in COLO no matter its old running state. | |
2944 | * After exited COLO, we will keep running. | |
2945 | */ | |
2946 | s->vm_was_running = true; | |
2947 | /* Fallthrough */ | |
2948 | case MIGRATION_STATUS_FAILED: | |
2949 | case MIGRATION_STATUS_CANCELLED: | |
57225e5f | 2950 | case MIGRATION_STATUS_CANCELLING: |
199aa6d4 PX |
2951 | if (s->vm_was_running) { |
2952 | vm_start(); | |
2953 | } else { | |
2954 | if (runstate_check(RUN_STATE_FINISH_MIGRATE)) { | |
2955 | runstate_set(RUN_STATE_POSTMIGRATE); | |
2956 | } | |
2957 | } | |
2958 | break; | |
2959 | ||
2960 | default: | |
2961 | /* Should not reach here, but if so, forgive the VM. */ | |
2962 | error_report("%s: Unknown ending state %d", __func__, s->state); | |
2963 | break; | |
2964 | } | |
2965 | qemu_bh_schedule(s->cleanup_bh); | |
2966 | qemu_mutex_unlock_iothread(); | |
2967 | } | |
2968 | ||
ad767bed DDAG |
2969 | void migration_make_urgent_request(void) |
2970 | { | |
2971 | qemu_sem_post(&migrate_get_current()->rate_limit_sem); | |
2972 | } | |
2973 | ||
2974 | void migration_consume_urgent_request(void) | |
2975 | { | |
2976 | qemu_sem_wait(&migrate_get_current()->rate_limit_sem); | |
2977 | } | |
2978 | ||
70b20477 DDAG |
2979 | /* |
2980 | * Master migration thread on the source VM. | |
2981 | * It drives the migration and pumps the data down the outgoing channel. | |
2982 | */ | |
5f496a1b | 2983 | static void *migration_thread(void *opaque) |
0d82d0e8 | 2984 | { |
9848a404 | 2985 | MigrationState *s = opaque; |
bc72ad67 | 2986 | int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST); |
b23c2ade | 2987 | MigThrError thr_error; |
ad767bed | 2988 | bool urgent = false; |
76f5933a | 2989 | |
ab28bd23 PB |
2990 | rcu_register_thread(); |
2991 | ||
b15df1ae PX |
2992 | s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
2993 | ||
89a02a9f | 2994 | qemu_savevm_state_header(s->to_dst_file); |
1d34e4bf | 2995 | |
62a02658 PX |
2996 | /* |
2997 | * If we opened the return path, we need to make sure dst has it | |
2998 | * opened as well. | |
2999 | */ | |
3000 | if (s->rp_state.from_dst_file) { | |
1d34e4bf | 3001 | /* Now tell the dest that it should open its end so it can reply */ |
89a02a9f | 3002 | qemu_savevm_send_open_return_path(s->to_dst_file); |
1d34e4bf DDAG |
3003 | |
3004 | /* And do a ping that will make stuff easier to debug */ | |
89a02a9f | 3005 | qemu_savevm_send_ping(s->to_dst_file, 1); |
0425dc97 | 3006 | } |
1d34e4bf | 3007 | |
58110f0a | 3008 | if (migrate_postcopy()) { |
1d34e4bf DDAG |
3009 | /* |
3010 | * Tell the destination that we *might* want to do postcopy later; | |
3011 | * if the other end can't do postcopy it should fail now, nice and | |
3012 | * early. | |
3013 | */ | |
89a02a9f | 3014 | qemu_savevm_send_postcopy_advise(s->to_dst_file); |
1d34e4bf DDAG |
3015 | } |
3016 | ||
9907e842 | 3017 | qemu_savevm_state_setup(s->to_dst_file); |
0d82d0e8 | 3018 | |
bc72ad67 | 3019 | s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start; |
48781e5b HZ |
3020 | migrate_set_state(&s->state, MIGRATION_STATUS_SETUP, |
3021 | MIGRATION_STATUS_ACTIVE); | |
29ae8a41 | 3022 | |
9ec055ae DDAG |
3023 | trace_migration_thread_setup_complete(); |
3024 | ||
3025 | while (s->state == MIGRATION_STATUS_ACTIVE || | |
3026 | s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) { | |
a3e879cd | 3027 | int64_t current_time; |
0d82d0e8 | 3028 | |
ad767bed | 3029 | if (urgent || !qemu_file_rate_limit(s->to_dst_file)) { |
2ad87305 PX |
3030 | MigIterateState iter_state = migration_iteration_run(s); |
3031 | if (iter_state == MIG_ITERATE_SKIP) { | |
3032 | continue; | |
3033 | } else if (iter_state == MIG_ITERATE_BREAK) { | |
09f6c85e | 3034 | break; |
c369f40d JQ |
3035 | } |
3036 | } | |
f4410a5d | 3037 | |
b23c2ade PX |
3038 | /* |
3039 | * Try to detect any kind of failures, and see whether we | |
3040 | * should stop the migration now. | |
3041 | */ | |
3042 | thr_error = migration_detect_error(s); | |
3043 | if (thr_error == MIG_THR_ERR_FATAL) { | |
3044 | /* Stop migration */ | |
fd45ee2c | 3045 | break; |
b23c2ade PX |
3046 | } else if (thr_error == MIG_THR_ERR_RECOVERED) { |
3047 | /* | |
3048 | * Just recovered from a e.g. network failure, reset all | |
3049 | * the local variables. This is important to avoid | |
3050 | * breaking transferred_bytes and bandwidth calculation | |
3051 | */ | |
3052 | s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); | |
3053 | s->iteration_initial_bytes = 0; | |
fd45ee2c | 3054 | } |
b15df1ae | 3055 | |
bc72ad67 | 3056 | current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
0d82d0e8 | 3057 | |
b15df1ae PX |
3058 | migration_update_counters(s, current_time); |
3059 | ||
ad767bed | 3060 | urgent = false; |
89a02a9f | 3061 | if (qemu_file_rate_limit(s->to_dst_file)) { |
ad767bed DDAG |
3062 | /* Wait for a delay to do rate limiting OR |
3063 | * something urgent to post the semaphore. | |
3064 | */ | |
3065 | int ms = s->iteration_start_time + BUFFER_DELAY - current_time; | |
3066 | trace_migration_thread_ratelimit_pre(ms); | |
3067 | if (qemu_sem_timedwait(&s->rate_limit_sem, ms) == 0) { | |
3068 | /* We were worken by one or more urgent things but | |
3069 | * the timedwait will have consumed one of them. | |
3070 | * The service routine for the urgent wake will dec | |
3071 | * the semaphore itself for each item it consumes, | |
3072 | * so add this one we just eat back. | |
3073 | */ | |
3074 | qemu_sem_post(&s->rate_limit_sem); | |
3075 | urgent = true; | |
3076 | } | |
3077 | trace_migration_thread_ratelimit_post(urgent); | |
0d82d0e8 | 3078 | } |
a3fa1d78 PB |
3079 | } |
3080 | ||
1d34e4bf | 3081 | trace_migration_thread_after_loop(); |
199aa6d4 | 3082 | migration_iteration_finish(s); |
ab28bd23 | 3083 | rcu_unregister_thread(); |
0d82d0e8 JQ |
3084 | return NULL; |
3085 | } | |
3086 | ||
cce8040b | 3087 | void migrate_fd_connect(MigrationState *s, Error *error_in) |
0d82d0e8 | 3088 | { |
d3e35b8f PX |
3089 | int64_t rate_limit; |
3090 | bool resume = s->state == MIGRATION_STATUS_POSTCOPY_PAUSED; | |
3091 | ||
2ff30257 | 3092 | s->expected_downtime = s->parameters.downtime_limit; |
bb1fadc4 | 3093 | s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s); |
cce8040b DDAG |
3094 | if (error_in) { |
3095 | migrate_fd_error(s, error_in); | |
3096 | migrate_fd_cleanup(s); | |
3097 | return; | |
3098 | } | |
0d82d0e8 | 3099 | |
d3e35b8f PX |
3100 | if (resume) { |
3101 | /* This is a resumed migration */ | |
3102 | rate_limit = INT64_MAX; | |
3103 | } else { | |
3104 | /* This is a fresh new migration */ | |
3105 | rate_limit = s->parameters.max_bandwidth / XFER_LIMIT_RATIO; | |
442773ce | 3106 | |
d3e35b8f PX |
3107 | /* Notify before starting migration thread */ |
3108 | notifier_list_notify(&migration_state_notifiers, s); | |
3109 | } | |
3110 | ||
3111 | qemu_file_set_rate_limit(s->to_dst_file, rate_limit); | |
3112 | qemu_file_set_blocking(s->to_dst_file, true); | |
9287ac27 | 3113 | |
1d34e4bf | 3114 | /* |
c788ada8 PX |
3115 | * Open the return path. For postcopy, it is used exclusively. For |
3116 | * precopy, only if user specified "return-path" capability would | |
3117 | * QEMU uses the return path. | |
1d34e4bf | 3118 | */ |
c788ada8 | 3119 | if (migrate_postcopy_ram() || migrate_use_return_path()) { |
d3e35b8f | 3120 | if (open_return_path_on_source(s, !resume)) { |
1d34e4bf | 3121 | error_report("Unable to open return-path for postcopy"); |
d3e35b8f | 3122 | migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED); |
1d34e4bf DDAG |
3123 | migrate_fd_cleanup(s); |
3124 | return; | |
3125 | } | |
3126 | } | |
3127 | ||
d3e35b8f | 3128 | if (resume) { |
135b87b4 PX |
3129 | /* Wakeup the main migration thread to do the recovery */ |
3130 | migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_PAUSED, | |
3131 | MIGRATION_STATUS_POSTCOPY_RECOVER); | |
3132 | qemu_sem_post(&s->postcopy_pause_sem); | |
d3e35b8f PX |
3133 | return; |
3134 | } | |
3135 | ||
f986c3d2 JQ |
3136 | if (multifd_save_setup() != 0) { |
3137 | migrate_set_state(&s->state, MIGRATION_STATUS_SETUP, | |
3138 | MIGRATION_STATUS_FAILED); | |
3139 | migrate_fd_cleanup(s); | |
3140 | return; | |
3141 | } | |
009fad7f | 3142 | qemu_thread_create(&s->thread, "live_migration", migration_thread, s, |
bb1fadc4 | 3143 | QEMU_THREAD_JOINABLE); |
1d34e4bf | 3144 | s->migration_thread_running = true; |
0d82d0e8 | 3145 | } |
093e3c42 | 3146 | |
9d18af93 PX |
3147 | void migration_global_dump(Monitor *mon) |
3148 | { | |
3149 | MigrationState *ms = migrate_get_current(); | |
3150 | ||
6f0f6428 JQ |
3151 | monitor_printf(mon, "globals:\n"); |
3152 | monitor_printf(mon, "store-global-state: %s\n", | |
3153 | ms->store_global_state ? "on" : "off"); | |
3154 | monitor_printf(mon, "only-migratable: %s\n", | |
3155 | ms->only_migratable ? "on" : "off"); | |
3156 | monitor_printf(mon, "send-configuration: %s\n", | |
3157 | ms->send_configuration ? "on" : "off"); | |
3158 | monitor_printf(mon, "send-section-footer: %s\n", | |
3159 | ms->send_section_footer ? "on" : "off"); | |
f548222c XG |
3160 | monitor_printf(mon, "decompress-error-check: %s\n", |
3161 | ms->decompress_error_check ? "on" : "off"); | |
9d18af93 PX |
3162 | } |
3163 | ||
20814758 PX |
3164 | #define DEFINE_PROP_MIG_CAP(name, x) \ |
3165 | DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false) | |
3166 | ||
5272298c PX |
3167 | static Property migration_properties[] = { |
3168 | DEFINE_PROP_BOOL("store-global-state", MigrationState, | |
3169 | store_global_state, true), | |
3df663e5 | 3170 | DEFINE_PROP_BOOL("only-migratable", MigrationState, only_migratable, false), |
71dd4c1a PX |
3171 | DEFINE_PROP_BOOL("send-configuration", MigrationState, |
3172 | send_configuration, true), | |
15c38503 PX |
3173 | DEFINE_PROP_BOOL("send-section-footer", MigrationState, |
3174 | send_section_footer, true), | |
f548222c XG |
3175 | DEFINE_PROP_BOOL("decompress-error-check", MigrationState, |
3176 | decompress_error_check, true), | |
89632faf PX |
3177 | |
3178 | /* Migration parameters */ | |
741d4086 | 3179 | DEFINE_PROP_UINT8("x-compress-level", MigrationState, |
89632faf PX |
3180 | parameters.compress_level, |
3181 | DEFAULT_MIGRATE_COMPRESS_LEVEL), | |
741d4086 | 3182 | DEFINE_PROP_UINT8("x-compress-threads", MigrationState, |
89632faf PX |
3183 | parameters.compress_threads, |
3184 | DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT), | |
1d58872a XG |
3185 | DEFINE_PROP_BOOL("x-compress-wait-thread", MigrationState, |
3186 | parameters.compress_wait_thread, true), | |
741d4086 | 3187 | DEFINE_PROP_UINT8("x-decompress-threads", MigrationState, |
89632faf PX |
3188 | parameters.decompress_threads, |
3189 | DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT), | |
741d4086 | 3190 | DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState, |
89632faf PX |
3191 | parameters.cpu_throttle_initial, |
3192 | DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL), | |
741d4086 | 3193 | DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState, |
89632faf PX |
3194 | parameters.cpu_throttle_increment, |
3195 | DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT), | |
741d4086 | 3196 | DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState, |
89632faf | 3197 | parameters.max_bandwidth, MAX_THROTTLE), |
741d4086 | 3198 | DEFINE_PROP_UINT64("x-downtime-limit", MigrationState, |
89632faf PX |
3199 | parameters.downtime_limit, |
3200 | DEFAULT_MIGRATE_SET_DOWNTIME), | |
741d4086 | 3201 | DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState, |
89632faf PX |
3202 | parameters.x_checkpoint_delay, |
3203 | DEFAULT_MIGRATE_X_CHECKPOINT_DELAY), | |
741d4086 | 3204 | DEFINE_PROP_UINT8("x-multifd-channels", MigrationState, |
4075fb1c JQ |
3205 | parameters.x_multifd_channels, |
3206 | DEFAULT_MIGRATE_MULTIFD_CHANNELS), | |
741d4086 | 3207 | DEFINE_PROP_UINT32("x-multifd-page-count", MigrationState, |
0fb86605 JQ |
3208 | parameters.x_multifd_page_count, |
3209 | DEFAULT_MIGRATE_MULTIFD_PAGE_COUNT), | |
73af8dd8 JQ |
3210 | DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState, |
3211 | parameters.xbzrle_cache_size, | |
3212 | DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE), | |
7e555c6c DDAG |
3213 | DEFINE_PROP_SIZE("max-postcopy-bandwidth", MigrationState, |
3214 | parameters.max_postcopy_bandwidth, | |
3215 | DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH), | |
4cbc9c7f LQ |
3216 | DEFINE_PROP_UINT8("max-cpu-throttle", MigrationState, |
3217 | parameters.max_cpu_throttle, | |
3218 | DEFAULT_MIGRATE_MAX_CPU_THROTTLE), | |
20814758 PX |
3219 | |
3220 | /* Migration capabilities */ | |
3221 | DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE), | |
3222 | DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL), | |
3223 | DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE), | |
3224 | DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS), | |
3225 | DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS), | |
3226 | DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS), | |
3227 | DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM), | |
3228 | DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO), | |
3229 | DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM), | |
3230 | DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK), | |
3231 | DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH), | |
30126bbf | 3232 | DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_X_MULTIFD), |
20814758 | 3233 | |
5272298c PX |
3234 | DEFINE_PROP_END_OF_LIST(), |
3235 | }; | |
3236 | ||
e5cb7e76 PX |
3237 | static void migration_class_init(ObjectClass *klass, void *data) |
3238 | { | |
3239 | DeviceClass *dc = DEVICE_CLASS(klass); | |
3240 | ||
3241 | dc->user_creatable = false; | |
5272298c | 3242 | dc->props = migration_properties; |
e5cb7e76 PX |
3243 | } |
3244 | ||
b91bf5e4 MAL |
3245 | static void migration_instance_finalize(Object *obj) |
3246 | { | |
3247 | MigrationState *ms = MIGRATION_OBJ(obj); | |
3248 | MigrationParameters *params = &ms->parameters; | |
3249 | ||
87db1a7d | 3250 | qemu_mutex_destroy(&ms->error_mutex); |
62df066f | 3251 | qemu_mutex_destroy(&ms->qemu_file_lock); |
b91bf5e4 MAL |
3252 | g_free(params->tls_hostname); |
3253 | g_free(params->tls_creds); | |
ad767bed | 3254 | qemu_sem_destroy(&ms->rate_limit_sem); |
e91d8951 | 3255 | qemu_sem_destroy(&ms->pause_sem); |
b23c2ade | 3256 | qemu_sem_destroy(&ms->postcopy_pause_sem); |
14b1742e | 3257 | qemu_sem_destroy(&ms->postcopy_pause_rp_sem); |
edd090c7 | 3258 | qemu_sem_destroy(&ms->rp_state.rp_sem); |
ab105cc1 | 3259 | error_free(ms->error); |
b91bf5e4 MAL |
3260 | } |
3261 | ||
e5cb7e76 PX |
3262 | static void migration_instance_init(Object *obj) |
3263 | { | |
3264 | MigrationState *ms = MIGRATION_OBJ(obj); | |
8b0b29dc | 3265 | MigrationParameters *params = &ms->parameters; |
e5cb7e76 PX |
3266 | |
3267 | ms->state = MIGRATION_STATUS_NONE; | |
e5cb7e76 | 3268 | ms->mbps = -1; |
e91d8951 | 3269 | qemu_sem_init(&ms->pause_sem, 0); |
87db1a7d | 3270 | qemu_mutex_init(&ms->error_mutex); |
8b0b29dc PX |
3271 | |
3272 | params->tls_hostname = g_strdup(""); | |
3273 | params->tls_creds = g_strdup(""); | |
3274 | ||
3275 | /* Set has_* up only for parameter checks */ | |
3276 | params->has_compress_level = true; | |
3277 | params->has_compress_threads = true; | |
3278 | params->has_decompress_threads = true; | |
3279 | params->has_cpu_throttle_initial = true; | |
3280 | params->has_cpu_throttle_increment = true; | |
3281 | params->has_max_bandwidth = true; | |
3282 | params->has_downtime_limit = true; | |
3283 | params->has_x_checkpoint_delay = true; | |
3284 | params->has_block_incremental = true; | |
4075fb1c | 3285 | params->has_x_multifd_channels = true; |
0fb86605 | 3286 | params->has_x_multifd_page_count = true; |
73af8dd8 | 3287 | params->has_xbzrle_cache_size = true; |
7e555c6c | 3288 | params->has_max_postcopy_bandwidth = true; |
4cbc9c7f | 3289 | params->has_max_cpu_throttle = true; |
b23c2ade PX |
3290 | |
3291 | qemu_sem_init(&ms->postcopy_pause_sem, 0); | |
14b1742e | 3292 | qemu_sem_init(&ms->postcopy_pause_rp_sem, 0); |
edd090c7 | 3293 | qemu_sem_init(&ms->rp_state.rp_sem, 0); |
ad767bed | 3294 | qemu_sem_init(&ms->rate_limit_sem, 0); |
62df066f | 3295 | qemu_mutex_init(&ms->qemu_file_lock); |
8b0b29dc PX |
3296 | } |
3297 | ||
3298 | /* | |
3299 | * Return true if check pass, false otherwise. Error will be put | |
3300 | * inside errp if provided. | |
3301 | */ | |
3302 | static bool migration_object_check(MigrationState *ms, Error **errp) | |
3303 | { | |
6b19a7d9 PX |
3304 | MigrationCapabilityStatusList *head = NULL; |
3305 | /* Assuming all off */ | |
3306 | bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret; | |
3307 | int i; | |
3308 | ||
8b0b29dc PX |
3309 | if (!migrate_params_check(&ms->parameters, errp)) { |
3310 | return false; | |
3311 | } | |
3312 | ||
6b19a7d9 PX |
3313 | for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) { |
3314 | if (ms->enabled_capabilities[i]) { | |
3315 | head = migrate_cap_add(head, i, true); | |
3316 | } | |
3317 | } | |
3318 | ||
3319 | ret = migrate_caps_check(cap_list, head, errp); | |
3320 | ||
3321 | /* It works with head == NULL */ | |
3322 | qapi_free_MigrationCapabilityStatusList(head); | |
3323 | ||
3324 | return ret; | |
e5cb7e76 PX |
3325 | } |
3326 | ||
3327 | static const TypeInfo migration_type = { | |
3328 | .name = TYPE_MIGRATION, | |
01f6e14c | 3329 | /* |
c8d3ff38 PX |
3330 | * NOTE: TYPE_MIGRATION is not really a device, as the object is |
3331 | * not created using qdev_create(), it is not attached to the qdev | |
3332 | * device tree, and it is never realized. | |
3333 | * | |
3334 | * TODO: Make this TYPE_OBJECT once QOM provides something like | |
3335 | * TYPE_DEVICE's "-global" properties. | |
01f6e14c | 3336 | */ |
e5cb7e76 PX |
3337 | .parent = TYPE_DEVICE, |
3338 | .class_init = migration_class_init, | |
3339 | .class_size = sizeof(MigrationClass), | |
3340 | .instance_size = sizeof(MigrationState), | |
3341 | .instance_init = migration_instance_init, | |
b91bf5e4 | 3342 | .instance_finalize = migration_instance_finalize, |
e5cb7e76 PX |
3343 | }; |
3344 | ||
3345 | static void register_migration_types(void) | |
3346 | { | |
3347 | type_register_static(&migration_type); | |
3348 | } | |
3349 | ||
3350 | type_init(register_migration_types); |