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