]>
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 | * | |
12 | */ | |
13 | ||
14 | #ifndef QEMU_MIGRATION_H | |
15 | #define QEMU_MIGRATION_H | |
16 | ||
7b1b5d19 | 17 | #include "qapi/qmp/qdict.h" |
376253ec | 18 | #include "qemu-common.h" |
9848a404 | 19 | #include "qemu/thread.h" |
1de7afc9 | 20 | #include "qemu/notify.h" |
7b1b5d19 | 21 | #include "qapi/error.h" |
caf71f86 | 22 | #include "migration/vmstate.h" |
bbf6da32 | 23 | #include "qapi-types.h" |
43487c67 | 24 | #include "exec/cpu-common.h" |
376253ec | 25 | |
b5503338 EH |
26 | #define QEMU_VM_FILE_MAGIC 0x5145564d |
27 | #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002 | |
28 | #define QEMU_VM_FILE_VERSION 0x00000003 | |
29 | ||
30 | #define QEMU_VM_EOF 0x00 | |
31 | #define QEMU_VM_SECTION_START 0x01 | |
32 | #define QEMU_VM_SECTION_PART 0x02 | |
33 | #define QEMU_VM_SECTION_END 0x03 | |
34 | #define QEMU_VM_SECTION_FULL 0x04 | |
35 | #define QEMU_VM_SUBSECTION 0x05 | |
8118f095 | 36 | #define QEMU_VM_VMDESCRIPTION 0x06 |
61964c23 | 37 | #define QEMU_VM_CONFIGURATION 0x07 |
c76ca188 | 38 | #define QEMU_VM_COMMAND 0x08 |
f68945d4 | 39 | #define QEMU_VM_SECTION_FOOTER 0x7e |
b5503338 | 40 | |
6607ae23 IY |
41 | struct MigrationParams { |
42 | bool blk; | |
43 | bool shared; | |
44 | }; | |
45 | ||
6decec93 DDAG |
46 | /* Messages sent on the return path from destination to source */ |
47 | enum mig_rp_message_type { | |
48 | MIG_RP_MSG_INVALID = 0, /* Must be 0 */ | |
49 | MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */ | |
50 | MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */ | |
51 | ||
1e2d90eb DDAG |
52 | MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */ |
53 | MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */ | |
54 | ||
6decec93 DDAG |
55 | MIG_RP_MSG_MAX |
56 | }; | |
57 | ||
1a8f46f8 | 58 | typedef QLIST_HEAD(, LoadStateEntry) LoadStateEntry_Head; |
093e3c42 DDAG |
59 | |
60 | /* The current postcopy state is read/set by postcopy_state_get/set | |
61 | * which update it atomically. | |
62 | * The state is updated as postcopy messages are received, and | |
63 | * in general only one thread should be writing to the state at any one | |
64 | * time, initially the main thread and then the listen thread; | |
65 | * Corner cases are where either thread finishes early and/or errors. | |
66 | * The state is checked as messages are received to ensure that | |
67 | * the source is sending us messages in the correct order. | |
68 | * The state is also used by the RAM reception code to know if it | |
69 | * has to place pages atomically, and the cleanup code at the end of | |
70 | * the main thread to know if it has to delay cleanup until the end | |
71 | * of postcopy. | |
72 | */ | |
73 | typedef enum { | |
74 | POSTCOPY_INCOMING_NONE = 0, /* Initial state - no postcopy */ | |
75 | POSTCOPY_INCOMING_ADVISE, | |
76 | POSTCOPY_INCOMING_DISCARD, | |
77 | POSTCOPY_INCOMING_LISTENING, | |
78 | POSTCOPY_INCOMING_RUNNING, | |
79 | POSTCOPY_INCOMING_END | |
80 | } PostcopyState; | |
81 | ||
bca7856a DDAG |
82 | /* State for the incoming migration */ |
83 | struct MigrationIncomingState { | |
42e2aa56 | 84 | QEMUFile *from_src_file; |
1a8f46f8 | 85 | |
7b89bf27 DDAG |
86 | /* |
87 | * Free at the start of the main state load, set as the main thread finishes | |
88 | * loading state. | |
89 | */ | |
90 | QemuEvent main_thread_load_event; | |
91 | ||
c4faeed2 | 92 | bool have_fault_thread; |
f0a227ad DDAG |
93 | QemuThread fault_thread; |
94 | QemuSemaphore fault_thread_sem; | |
95 | ||
c76201ab DDAG |
96 | bool have_listen_thread; |
97 | QemuThread listen_thread; | |
98 | QemuSemaphore listen_thread_sem; | |
99 | ||
1caddf8a DDAG |
100 | /* For the kernel to send us notifications */ |
101 | int userfault_fd; | |
c4faeed2 DDAG |
102 | /* To tell the fault_thread to quit */ |
103 | int userfault_quit_fd; | |
2e37701e | 104 | QEMUFile *to_src_file; |
6decec93 | 105 | QemuMutex rp_mutex; /* We send replies from multiple threads */ |
696ed9a9 | 106 | void *postcopy_tmp_page; |
2e37701e | 107 | |
93d7af6f | 108 | int state; |
1a8f46f8 DDAG |
109 | /* See savevm.c */ |
110 | LoadStateEntry_Head loadvm_handlers; | |
bca7856a DDAG |
111 | }; |
112 | ||
113 | MigrationIncomingState *migration_incoming_get_current(void); | |
114 | MigrationIncomingState *migration_incoming_state_new(QEMUFile *f); | |
115 | void migration_incoming_state_destroy(void); | |
116 | ||
6c595cde DDAG |
117 | /* |
118 | * An outstanding page request, on the source, having been received | |
119 | * and queued | |
120 | */ | |
121 | struct MigrationSrcPageRequest { | |
122 | RAMBlock *rb; | |
123 | hwaddr offset; | |
124 | hwaddr len; | |
125 | ||
126 | QSIMPLEQ_ENTRY(MigrationSrcPageRequest) next_req; | |
127 | }; | |
128 | ||
22f00a44 | 129 | struct MigrationState |
065e2813 | 130 | { |
065e2813 | 131 | int64_t bandwidth_limit; |
9848a404 JQ |
132 | size_t bytes_xfer; |
133 | size_t xfer_limit; | |
9848a404 | 134 | QemuThread thread; |
bb1fadc4 | 135 | QEMUBH *cleanup_bh; |
89a02a9f | 136 | QEMUFile *to_dst_file; |
7fb1cf16 | 137 | int parameters[MIGRATION_PARAMETER__MAX]; |
f8bbc128 | 138 | |
f8bbc128 | 139 | int state; |
6607ae23 | 140 | MigrationParams params; |
70b20477 DDAG |
141 | |
142 | /* State related to return path */ | |
143 | struct { | |
144 | QEMUFile *from_dst_file; | |
145 | QemuThread rp_thread; | |
146 | bool error; | |
147 | } rp_state; | |
148 | ||
7e114f8c | 149 | double mbps; |
d5f8a570 | 150 | int64_t total_time; |
9c5a9fcf | 151 | int64_t downtime; |
2c52ddf1 | 152 | int64_t expected_downtime; |
8d017193 | 153 | int64_t dirty_pages_rate; |
90f8ae72 | 154 | int64_t dirty_bytes_rate; |
7fb1cf16 | 155 | bool enabled_capabilities[MIGRATION_CAPABILITY__MAX]; |
17ad9b35 | 156 | int64_t xbzrle_cache_size; |
ed4fbd10 | 157 | int64_t setup_time; |
58570ed8 | 158 | int64_t dirty_sync_count; |
4886a1bc DDAG |
159 | |
160 | /* Flag set once the migration has been asked to enter postcopy */ | |
161 | bool start_postcopy; | |
1d34e4bf DDAG |
162 | |
163 | /* Flag set once the migration thread is running (and needs joining) */ | |
164 | bool migration_thread_running; | |
6c595cde DDAG |
165 | |
166 | /* Queue of outstanding page requests from the destination */ | |
167 | QemuMutex src_page_req_mutex; | |
168 | QSIMPLEQ_HEAD(src_page_requests, MigrationSrcPageRequest) src_page_requests; | |
169 | /* The RAMBlock used in the last src_page_request */ | |
170 | RAMBlock *last_req_rb; | |
065e2813 AL |
171 | }; |
172 | ||
48781e5b HZ |
173 | void migrate_set_state(int *state, int old_state, int new_state); |
174 | ||
511c0231 JQ |
175 | void process_incoming_migration(QEMUFile *f); |
176 | ||
43eaae28 | 177 | void qemu_start_incoming_migration(const char *uri, Error **errp); |
5bb7910a | 178 | |
a0a3fd60 GC |
179 | uint64_t migrate_max_downtime(void); |
180 | ||
43eaae28 | 181 | void exec_start_incoming_migration(const char *host_port, Error **errp); |
065e2813 | 182 | |
f37afb5a | 183 | void exec_start_outgoing_migration(MigrationState *s, const char *host_port, Error **errp); |
065e2813 | 184 | |
43eaae28 | 185 | void tcp_start_incoming_migration(const char *host_port, Error **errp); |
34c9dd8e | 186 | |
f37afb5a | 187 | void tcp_start_outgoing_migration(MigrationState *s, const char *host_port, Error **errp); |
34c9dd8e | 188 | |
43eaae28 | 189 | void unix_start_incoming_migration(const char *path, Error **errp); |
4951f65b | 190 | |
f37afb5a | 191 | void unix_start_outgoing_migration(MigrationState *s, const char *path, Error **errp); |
4951f65b | 192 | |
43eaae28 | 193 | void fd_start_incoming_migration(const char *path, Error **errp); |
5ac1fad3 | 194 | |
f37afb5a | 195 | void fd_start_outgoing_migration(MigrationState *s, const char *fdname, Error **errp); |
5ac1fad3 | 196 | |
2da776db MH |
197 | void rdma_start_outgoing_migration(void *opaque, const char *host_port, Error **errp); |
198 | ||
199 | void rdma_start_incoming_migration(const char *host_port, Error **errp); | |
200 | ||
22f00a44 | 201 | void migrate_fd_error(MigrationState *s); |
065e2813 | 202 | |
22f00a44 | 203 | void migrate_fd_connect(MigrationState *s); |
065e2813 | 204 | |
11c76741 | 205 | int migrate_fd_close(MigrationState *s); |
c87b015b | 206 | |
99a0db9b GH |
207 | void add_migration_state_change_notifier(Notifier *notify); |
208 | void remove_migration_state_change_notifier(Notifier *notify); | |
aefeb18b | 209 | MigrationState *migrate_init(const MigrationParams *params); |
02edd2e7 | 210 | bool migration_in_setup(MigrationState *); |
7073693b | 211 | bool migration_has_finished(MigrationState *); |
afe2df69 | 212 | bool migration_has_failed(MigrationState *); |
9ec055ae DDAG |
213 | /* True if outgoing migration has entered postcopy phase */ |
214 | bool migration_in_postcopy(MigrationState *); | |
859bc756 | 215 | MigrationState *migrate_get_current(void); |
99a0db9b | 216 | |
8706d2d5 LL |
217 | void migrate_compress_threads_create(void); |
218 | void migrate_compress_threads_join(void); | |
3fcb38c2 LL |
219 | void migrate_decompress_threads_create(void); |
220 | void migrate_decompress_threads_join(void); | |
adc56dda BS |
221 | uint64_t ram_bytes_remaining(void); |
222 | uint64_t ram_bytes_transferred(void); | |
223 | uint64_t ram_bytes_total(void); | |
905f26f2 | 224 | void free_xbzrle_decoded_buf(void); |
adc56dda | 225 | |
2b0ce079 MH |
226 | void acct_update_position(QEMUFile *f, size_t size, bool zero); |
227 | ||
004d4c10 OW |
228 | uint64_t dup_mig_bytes_transferred(void); |
229 | uint64_t dup_mig_pages_transferred(void); | |
f1c72795 PL |
230 | uint64_t skipped_mig_bytes_transferred(void); |
231 | uint64_t skipped_mig_pages_transferred(void); | |
004d4c10 OW |
232 | uint64_t norm_mig_bytes_transferred(void); |
233 | uint64_t norm_mig_pages_transferred(void); | |
f36d55af OW |
234 | uint64_t xbzrle_mig_bytes_transferred(void); |
235 | uint64_t xbzrle_mig_pages_transferred(void); | |
236 | uint64_t xbzrle_mig_pages_overflow(void); | |
237 | uint64_t xbzrle_mig_pages_cache_miss(void); | |
8bc39233 | 238 | double xbzrle_mig_cache_miss_rate(void); |
004d4c10 | 239 | |
44c3b58c | 240 | void ram_handle_compressed(void *host, uint8_t ch, uint64_t size); |
4f2e4252 | 241 | void ram_debug_dump_bitmap(unsigned long *todump, bool expected); |
e0b266f0 DDAG |
242 | /* For outgoing discard bitmap */ |
243 | int ram_postcopy_send_discard_bitmap(MigrationState *ms); | |
244 | /* For incoming postcopy discard */ | |
245 | int ram_discard_range(MigrationIncomingState *mis, const char *block_name, | |
246 | uint64_t start, size_t length); | |
1caddf8a | 247 | int ram_postcopy_incoming_init(MigrationIncomingState *mis); |
44c3b58c | 248 | |
fa2756b7 AL |
249 | /** |
250 | * @migrate_add_blocker - prevent migration from proceeding | |
251 | * | |
252 | * @reason - an error to be returned whenever migration is attempted | |
253 | */ | |
254 | void migrate_add_blocker(Error *reason); | |
255 | ||
256 | /** | |
257 | * @migrate_del_blocker - remove a blocking error from migration | |
258 | * | |
259 | * @reason - the error blocking migration | |
260 | */ | |
261 | void migrate_del_blocker(Error *reason); | |
262 | ||
53dd370c | 263 | bool migrate_postcopy_ram(void); |
323004a3 | 264 | bool migrate_zero_blocks(void); |
60d9222c | 265 | |
bde1e2ec CV |
266 | bool migrate_auto_converge(void); |
267 | ||
302dfbeb OW |
268 | int xbzrle_encode_buffer(uint8_t *old_buf, uint8_t *new_buf, int slen, |
269 | uint8_t *dst, int dlen); | |
270 | int xbzrle_decode_buffer(uint8_t *src, int slen, uint8_t *dst, int dlen); | |
271 | ||
17ad9b35 OW |
272 | int migrate_use_xbzrle(void); |
273 | int64_t migrate_xbzrle_cache_size(void); | |
274 | ||
9e1ba4cc | 275 | int64_t xbzrle_cache_resize(int64_t new_size); |
43487c67 | 276 | |
8706d2d5 LL |
277 | bool migrate_use_compression(void); |
278 | int migrate_compress_level(void); | |
279 | int migrate_compress_threads(void); | |
3fcb38c2 | 280 | int migrate_decompress_threads(void); |
b05dc723 | 281 | bool migrate_use_events(void); |
8706d2d5 | 282 | |
6decec93 DDAG |
283 | /* Sending on the return path - generic and then for each message type */ |
284 | void migrate_send_rp_message(MigrationIncomingState *mis, | |
285 | enum mig_rp_message_type message_type, | |
286 | uint16_t len, void *data); | |
287 | void migrate_send_rp_shut(MigrationIncomingState *mis, | |
288 | uint32_t value); | |
289 | void migrate_send_rp_pong(MigrationIncomingState *mis, | |
290 | uint32_t value); | |
1e2d90eb DDAG |
291 | void migrate_send_rp_req_pages(MigrationIncomingState *mis, const char* rbname, |
292 | ram_addr_t start, size_t len); | |
6decec93 | 293 | |
43487c67 MH |
294 | void ram_control_before_iterate(QEMUFile *f, uint64_t flags); |
295 | void ram_control_after_iterate(QEMUFile *f, uint64_t flags); | |
632e3a5c | 296 | void ram_control_load_hook(QEMUFile *f, uint64_t flags, void *data); |
43487c67 MH |
297 | |
298 | /* Whenever this is found in the data stream, the flags | |
299 | * will be passed to ram_control_load_hook in the incoming-migration | |
300 | * side. This lets before_ram_iterate/after_ram_iterate add | |
301 | * transport-specific sections to the RAM migration data. | |
302 | */ | |
303 | #define RAM_SAVE_FLAG_HOOK 0x80 | |
304 | ||
305 | #define RAM_SAVE_CONTROL_NOT_SUPP -1000 | |
306 | #define RAM_SAVE_CONTROL_DELAYED -2000 | |
307 | ||
308 | size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset, | |
309 | ram_addr_t offset, size_t size, | |
6e1dea46 | 310 | uint64_t *bytes_sent); |
43487c67 | 311 | |
56e93d26 | 312 | void ram_mig_init(void); |
37fb569c | 313 | void savevm_skip_section_footers(void); |
df4b1024 | 314 | void register_global_state(void); |
13d16814 | 315 | void global_state_set_optional(void); |
61964c23 | 316 | void savevm_skip_configuration(void); |
560d027b | 317 | int global_state_store(void); |
c69adea4 | 318 | void global_state_store_running(void); |
093e3c42 | 319 | |
6c595cde DDAG |
320 | void flush_page_queue(MigrationState *ms); |
321 | int ram_save_queue_pages(MigrationState *ms, const char *rbname, | |
322 | ram_addr_t start, ram_addr_t len); | |
323 | ||
093e3c42 DDAG |
324 | PostcopyState postcopy_state_get(void); |
325 | /* Set the state and return the old state */ | |
326 | PostcopyState postcopy_state_set(PostcopyState new_state); | |
5bb7910a | 327 | #endif |