]>
Commit | Line | Data |
---|---|---|
5bb7910a AL |
1 | /* |
2 | * QEMU live migration | |
3 | * | |
4 | * Copyright IBM, Corp. 2008 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <[email protected]> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
10 | * the COPYING file in the top-level directory. | |
11 | * | |
12 | */ | |
13 | ||
14 | #ifndef QEMU_MIGRATION_H | |
15 | #define QEMU_MIGRATION_H | |
16 | ||
d4842052 | 17 | #include "exec/cpu-common.h" |
a27bd6c7 | 18 | #include "hw/qdev-core.h" |
9af23989 | 19 | #include "qapi/qapi-types-migration.h" |
9848a404 | 20 | #include "qemu/thread.h" |
25d0c16f | 21 | #include "qemu/coroutine_int.h" |
4f0fae7f | 22 | #include "io/channel.h" |
8518278a | 23 | #include "io/channel-buffer.h" |
7659505c | 24 | #include "net/announce.h" |
db1015e9 | 25 | #include "qom/object.h" |
376253ec | 26 | |
2a4c42f1 AP |
27 | struct PostcopyBlocktimeContext; |
28 | ||
13955b89 PX |
29 | #define MIGRATION_RESUME_ACK_VALUE (1) |
30 | ||
002cad6b PX |
31 | /* |
32 | * 1<<6=64 pages -> 256K chunk when page size is 4K. This gives us | |
33 | * the benefit that all the chunks are 64 pages aligned then the | |
34 | * bitmaps are always aligned to LONG. | |
35 | */ | |
36 | #define CLEAR_BITMAP_SHIFT_MIN 6 | |
37 | /* | |
38 | * 1<<18=256K pages -> 1G chunk when page size is 4K. This is the | |
39 | * default value to use if no one specified. | |
40 | */ | |
41 | #define CLEAR_BITMAP_SHIFT_DEFAULT 18 | |
42 | /* | |
43 | * 1<<31=2G pages -> 8T chunk when page size is 4K. This should be | |
44 | * big enough and make sure we won't overflow easily. | |
45 | */ | |
46 | #define CLEAR_BITMAP_SHIFT_MAX 31 | |
47 | ||
bca7856a DDAG |
48 | /* State for the incoming migration */ |
49 | struct MigrationIncomingState { | |
42e2aa56 | 50 | QEMUFile *from_src_file; |
1a8f46f8 | 51 | |
7b89bf27 DDAG |
52 | /* |
53 | * Free at the start of the main state load, set as the main thread finishes | |
54 | * loading state. | |
55 | */ | |
56 | QemuEvent main_thread_load_event; | |
57 | ||
7659505c DDAG |
58 | /* For network announces */ |
59 | AnnounceTimer announce_timer; | |
60 | ||
67f11b5c | 61 | size_t largest_page_size; |
c4faeed2 | 62 | bool have_fault_thread; |
f0a227ad DDAG |
63 | QemuThread fault_thread; |
64 | QemuSemaphore fault_thread_sem; | |
64f615fe PX |
65 | /* Set this when we want the fault thread to quit */ |
66 | bool fault_thread_quit; | |
f0a227ad | 67 | |
c76201ab DDAG |
68 | bool have_listen_thread; |
69 | QemuThread listen_thread; | |
70 | QemuSemaphore listen_thread_sem; | |
71 | ||
1caddf8a DDAG |
72 | /* For the kernel to send us notifications */ |
73 | int userfault_fd; | |
64f615fe PX |
74 | /* To notify the fault_thread to wake, e.g., when need to quit */ |
75 | int userfault_event_fd; | |
2e37701e | 76 | QEMUFile *to_src_file; |
6decec93 | 77 | QemuMutex rp_mutex; /* We send replies from multiple threads */ |
096bf4c8 DDAG |
78 | /* RAMBlock of last request sent to source */ |
79 | RAMBlock *last_rb; | |
696ed9a9 | 80 | void *postcopy_tmp_page; |
41d84210 | 81 | void *postcopy_tmp_zero_page; |
00fa4fc8 DDAG |
82 | /* PostCopyFD's for external userfaultfds & handlers of shared memory */ |
83 | GArray *postcopy_remote_fds; | |
2e37701e | 84 | |
0aa6aefc DL |
85 | QEMUBH *bh; |
86 | ||
93d7af6f | 87 | int state; |
25d0c16f HZ |
88 | |
89 | bool have_colo_incoming_thread; | |
90 | QemuThread colo_incoming_thread; | |
91 | /* The coroutine we should enter (back) after failover */ | |
92 | Coroutine *migration_incoming_co; | |
c937b9a6 | 93 | QemuSemaphore colo_incoming_sem; |
2a4c42f1 AP |
94 | |
95 | /* | |
96 | * PostcopyBlocktimeContext to keep information for postcopy | |
97 | * live migration, to calculate vCPU block time | |
98 | * */ | |
99 | struct PostcopyBlocktimeContext *blocktime_ctx; | |
b411b844 PX |
100 | |
101 | /* notify PAUSED postcopy incoming migrations to try to continue */ | |
02affd41 | 102 | bool postcopy_recover_triggered; |
b411b844 | 103 | QemuSemaphore postcopy_pause_sem_dst; |
3a7804c3 | 104 | QemuSemaphore postcopy_pause_sem_fault; |
9aca82ba JQ |
105 | |
106 | /* List of listening socket addresses */ | |
107 | SocketAddressList *socket_address_list; | |
8f8bfffc PX |
108 | |
109 | /* A tree of pages that we requested to the source VM */ | |
110 | GTree *page_requested; | |
111 | /* For debugging purpose only, but would be nice to keep */ | |
112 | int page_requested_count; | |
113 | /* | |
114 | * The mutex helps to maintain the requested pages that we sent to the | |
115 | * source, IOW, to guarantee coherent between the page_requests tree and | |
116 | * the per-ramblock receivedmap. Note! This does not guarantee consistency | |
117 | * of the real page copy procedures (using UFFDIO_[ZERO]COPY). E.g., even | |
118 | * if one bit in receivedmap is cleared, UFFDIO_COPY could have happened | |
119 | * for that page already. This is intended so that the mutex won't | |
120 | * serialize and blocked by slow operations like UFFDIO_* ioctls. However | |
121 | * this should be enough to make sure the page_requested tree always | |
122 | * contains valid information. | |
123 | */ | |
124 | QemuMutex page_request_mutex; | |
bca7856a DDAG |
125 | }; |
126 | ||
127 | MigrationIncomingState *migration_incoming_get_current(void); | |
bca7856a | 128 | void migration_incoming_state_destroy(void); |
65ace060 AP |
129 | /* |
130 | * Functions to work with blocktime context | |
131 | */ | |
132 | void fill_destination_postcopy_migration_info(MigrationInfo *info); | |
bca7856a | 133 | |
e5cb7e76 PX |
134 | #define TYPE_MIGRATION "migration" |
135 | ||
db1015e9 | 136 | typedef struct MigrationClass MigrationClass; |
8110fa1d EH |
137 | DECLARE_OBJ_CHECKERS(MigrationState, MigrationClass, |
138 | MIGRATION_OBJ, TYPE_MIGRATION) | |
e5cb7e76 | 139 | |
db1015e9 | 140 | struct MigrationClass { |
e5cb7e76 PX |
141 | /*< private >*/ |
142 | DeviceClass parent_class; | |
db1015e9 | 143 | }; |
e5cb7e76 | 144 | |
f16aee44 | 145 | struct MigrationState { |
e5cb7e76 PX |
146 | /*< private >*/ |
147 | DeviceState parent_obj; | |
148 | ||
149 | /*< public >*/ | |
9848a404 | 150 | QemuThread thread; |
8518278a | 151 | QEMUBH *vm_start_bh; |
bb1fadc4 | 152 | QEMUBH *cleanup_bh; |
89a02a9f | 153 | QEMUFile *to_dst_file; |
8518278a | 154 | QIOChannelBuffer *bioc; |
62df066f PX |
155 | /* |
156 | * Protects to_dst_file pointer. We need to make sure we won't | |
157 | * yield or hang during the critical section, since this lock will | |
158 | * be used in OOB command handler. | |
159 | */ | |
160 | QemuMutex qemu_file_lock; | |
2594f56d | 161 | |
ad767bed DDAG |
162 | /* |
163 | * Used to allow urgent requests to override rate limiting. | |
164 | */ | |
165 | QemuSemaphore rate_limit_sem; | |
166 | ||
aecbfe9c XG |
167 | /* pages already send at the beginning of current iteration */ |
168 | uint64_t iteration_initial_pages; | |
169 | ||
170 | /* pages transferred per second */ | |
171 | double pages_per_second; | |
172 | ||
173 | /* bytes already send at the beginning of current iteration */ | |
b15df1ae PX |
174 | uint64_t iteration_initial_bytes; |
175 | /* time at the start of current iteration */ | |
176 | int64_t iteration_start_time; | |
177 | /* | |
178 | * The final stage happens when the remaining data is smaller than | |
179 | * this threshold; it's calculated from the requested downtime and | |
180 | * measured bandwidth | |
181 | */ | |
182 | int64_t threshold_size; | |
183 | ||
a0762d9e | 184 | /* params from 'migrate-set-parameters' */ |
2594f56d | 185 | MigrationParameters parameters; |
f8bbc128 | 186 | |
f8bbc128 | 187 | int state; |
70b20477 DDAG |
188 | |
189 | /* State related to return path */ | |
190 | struct { | |
191 | QEMUFile *from_dst_file; | |
192 | QemuThread rp_thread; | |
193 | bool error; | |
edd090c7 | 194 | QemuSemaphore rp_sem; |
70b20477 DDAG |
195 | } rp_state; |
196 | ||
7e114f8c | 197 | double mbps; |
4af246a3 PX |
198 | /* Timestamp when recent migration starts (ms) */ |
199 | int64_t start_time; | |
200 | /* Total time used by latest migration (ms) */ | |
d5f8a570 | 201 | int64_t total_time; |
64909f97 PX |
202 | /* Timestamp when VM is down (ms) to migrate the last stuff */ |
203 | int64_t downtime_start; | |
9c5a9fcf | 204 | int64_t downtime; |
2c52ddf1 | 205 | int64_t expected_downtime; |
7fb1cf16 | 206 | bool enabled_capabilities[MIGRATION_CAPABILITY__MAX]; |
ed4fbd10 | 207 | int64_t setup_time; |
7287cbd4 PX |
208 | /* |
209 | * Whether guest was running when we enter the completion stage. | |
210 | * If migration is interrupted by any reason, we need to continue | |
211 | * running the guest on source. | |
212 | */ | |
213 | bool vm_was_running; | |
4886a1bc DDAG |
214 | |
215 | /* Flag set once the migration has been asked to enter postcopy */ | |
216 | bool start_postcopy; | |
b82fc321 DDAG |
217 | /* Flag set after postcopy has sent the device state */ |
218 | bool postcopy_after_devices; | |
1d34e4bf DDAG |
219 | |
220 | /* Flag set once the migration thread is running (and needs joining) */ | |
221 | bool migration_thread_running; | |
6c595cde | 222 | |
1d2acc31 HZ |
223 | /* Flag set once the migration thread called bdrv_inactivate_all */ |
224 | bool block_inactive; | |
225 | ||
c7e0acd5 JF |
226 | /* Migration is waiting for guest to unplug device */ |
227 | QemuSemaphore wait_unplug_sem; | |
228 | ||
e91d8951 DDAG |
229 | /* Migration is paused due to pause-before-switchover */ |
230 | QemuSemaphore pause_sem; | |
231 | ||
c937b9a6 HZ |
232 | /* The semaphore is used to notify COLO thread that failover is finished */ |
233 | QemuSemaphore colo_exit_sem; | |
d59ce6f3 | 234 | |
bb70b66e LS |
235 | /* The event is used to notify COLO thread to do checkpoint */ |
236 | QemuEvent colo_checkpoint_event; | |
479125d5 HZ |
237 | int64_t colo_checkpoint_time; |
238 | QEMUTimer *colo_delay_timer; | |
239 | ||
87db1a7d JQ |
240 | /* The first error that has occurred. |
241 | We used the mutex to be able to return the 1st error message */ | |
d59ce6f3 | 242 | Error *error; |
87db1a7d JQ |
243 | /* mutex to protect errp */ |
244 | QemuMutex error_mutex; | |
245 | ||
2833c59b JQ |
246 | /* Do we have to clean up -b/-i from old migrate parameters */ |
247 | /* This feature is deprecated and will be removed */ | |
248 | bool must_remove_block_options; | |
5272298c PX |
249 | |
250 | /* | |
251 | * Global switch on whether we need to store the global state | |
252 | * during migration. | |
253 | */ | |
254 | bool store_global_state; | |
3df663e5 | 255 | |
71dd4c1a PX |
256 | /* Whether we send QEMU_VM_CONFIGURATION during migration */ |
257 | bool send_configuration; | |
15c38503 PX |
258 | /* Whether we send section footer during migration */ |
259 | bool send_section_footer; | |
b23c2ade PX |
260 | |
261 | /* Needed by postcopy-pause state */ | |
262 | QemuSemaphore postcopy_pause_sem; | |
14b1742e | 263 | QemuSemaphore postcopy_pause_rp_sem; |
f548222c XG |
264 | /* |
265 | * Whether we abort the migration if decompression errors are | |
266 | * detected at the destination. It is left at false for qemu | |
267 | * older than 3.0, since only newer qemu sends streams that | |
268 | * do not trigger spurious decompression errors. | |
269 | */ | |
270 | bool decompress_error_check; | |
002cad6b PX |
271 | |
272 | /* | |
273 | * This decides the size of guest memory chunk that will be used | |
274 | * to track dirty bitmap clearing. The size of memory chunk will | |
275 | * be GUEST_PAGE_SIZE << N. Say, N=0 means we will clear dirty | |
276 | * bitmap for each page to send (1<<0=1); N=10 means we will clear | |
277 | * dirty bitmap only once for 1<<10=1K continuous guest pages | |
278 | * (which is in 4M chunk). | |
279 | */ | |
280 | uint8_t clear_bitmap_shift; | |
d8053e73 CZ |
281 | |
282 | /* | |
283 | * This save hostname when out-going migration starts | |
284 | */ | |
285 | char *hostname; | |
065e2813 AL |
286 | }; |
287 | ||
48781e5b HZ |
288 | void migrate_set_state(int *state, int old_state, int new_state); |
289 | ||
b673eab4 | 290 | void migration_fd_process_incoming(QEMUFile *f, Error **errp); |
49ed0d24 | 291 | void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp); |
36c2f8be | 292 | void migration_incoming_process(void); |
511c0231 | 293 | |
428d8908 JQ |
294 | bool migration_has_all_channels(void); |
295 | ||
a0a3fd60 GC |
296 | uint64_t migrate_max_downtime(void); |
297 | ||
87db1a7d | 298 | void migrate_set_error(MigrationState *s, const Error *error); |
d59ce6f3 | 299 | void migrate_fd_error(MigrationState *s, const Error *error); |
065e2813 | 300 | |
cce8040b | 301 | void migrate_fd_connect(MigrationState *s, Error *error_in); |
065e2813 | 302 | |
3d63da16 | 303 | bool migration_is_setup_or_active(int state); |
392d87e2 | 304 | bool migration_is_running(int state); |
3d63da16 | 305 | |
3e0c8050 | 306 | void migrate_init(MigrationState *s); |
24f3902b | 307 | bool migration_is_blocked(Error **errp); |
9ec055ae | 308 | /* True if outgoing migration has entered postcopy phase */ |
5727309d | 309 | bool migration_in_postcopy(void); |
859bc756 | 310 | MigrationState *migrate_get_current(void); |
99a0db9b | 311 | |
58110f0a VSO |
312 | bool migrate_postcopy(void); |
313 | ||
53f09a10 | 314 | bool migrate_release_ram(void); |
53dd370c | 315 | bool migrate_postcopy_ram(void); |
323004a3 | 316 | bool migrate_zero_blocks(void); |
55efc8c2 | 317 | bool migrate_dirty_bitmaps(void); |
18269069 | 318 | bool migrate_ignore_shared(void); |
b9d68df6 | 319 | bool migrate_validate_uuid(void); |
60d9222c | 320 | |
bde1e2ec | 321 | bool migrate_auto_converge(void); |
30126bbf | 322 | bool migrate_use_multifd(void); |
93fbd031 | 323 | bool migrate_pause_before_switchover(void); |
4075fb1c | 324 | int migrate_multifd_channels(void); |
ab7cbb0b | 325 | MultiFDCompression migrate_multifd_compression(void); |
9004db48 | 326 | int migrate_multifd_zlib_level(void); |
6a9ad154 | 327 | int migrate_multifd_zstd_level(void); |
bde1e2ec | 328 | |
17ad9b35 | 329 | int migrate_use_xbzrle(void); |
8b9407a0 | 330 | uint64_t migrate_xbzrle_cache_size(void); |
35a6ed4f | 331 | bool migrate_colo_enabled(void); |
17ad9b35 | 332 | |
2833c59b JQ |
333 | bool migrate_use_block(void); |
334 | bool migrate_use_block_incremental(void); | |
4cbc9c7f | 335 | int migrate_max_cpu_throttle(void); |
c788ada8 | 336 | bool migrate_use_return_path(void); |
2833c59b | 337 | |
aecbfe9c XG |
338 | uint64_t ram_get_total_transferred_pages(void); |
339 | ||
8706d2d5 LL |
340 | bool migrate_use_compression(void); |
341 | int migrate_compress_level(void); | |
342 | int migrate_compress_threads(void); | |
1d58872a | 343 | int migrate_compress_wait_thread(void); |
3fcb38c2 | 344 | int migrate_decompress_threads(void); |
b05dc723 | 345 | bool migrate_use_events(void); |
f22f928e | 346 | bool migrate_postcopy_blocktime(void); |
6e8c25b4 | 347 | bool migrate_background_snapshot(void); |
8706d2d5 | 348 | |
6decec93 | 349 | /* Sending on the return path - generic and then for each message type */ |
6decec93 DDAG |
350 | void migrate_send_rp_shut(MigrationIncomingState *mis, |
351 | uint32_t value); | |
352 | void migrate_send_rp_pong(MigrationIncomingState *mis, | |
353 | uint32_t value); | |
2e2bce16 | 354 | int migrate_send_rp_req_pages(MigrationIncomingState *mis, RAMBlock *rb, |
8f8bfffc | 355 | ram_addr_t start, uint64_t haddr); |
7a267fc4 PX |
356 | int migrate_send_rp_message_req_pages(MigrationIncomingState *mis, |
357 | RAMBlock *rb, ram_addr_t start); | |
a335debb PX |
358 | void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis, |
359 | char *block_name); | |
13955b89 | 360 | void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value); |
6decec93 | 361 | |
b35ebdf0 | 362 | void dirty_bitmap_mig_before_vm_start(void); |
1499ab09 VSO |
363 | void dirty_bitmap_mig_cancel_outgoing(void); |
364 | void dirty_bitmap_mig_cancel_incoming(void); | |
31e4c354 HR |
365 | bool check_dirty_bitmap_mig_alias_map(const BitmapMigrationNodeAliasList *bbm, |
366 | Error **errp); | |
367 | ||
9aca82ba | 368 | void migrate_add_address(SocketAddress *address); |
b35ebdf0 | 369 | |
fbd162e6 YK |
370 | int foreach_not_ignored_block(RAMBlockIterFunc func, void *opaque); |
371 | ||
343f632c | 372 | #define qemu_ram_foreach_block \ |
fbd162e6 | 373 | #warning "Use foreach_not_ignored_block in migration code" |
343f632c | 374 | |
ad767bed DDAG |
375 | void migration_make_urgent_request(void); |
376 | void migration_consume_urgent_request(void); | |
97e1e067 | 377 | bool migration_rate_limit(void); |
c7c0e724 | 378 | void migration_cancel(void); |
ad767bed | 379 | |
5bb7910a | 380 | #endif |