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