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