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