1
2
3
4
5
6
7
8
9
10
11
12
13
14#ifndef QEMU_MIGRATION_H
15#define QEMU_MIGRATION_H
16
17#include "exec/cpu-common.h"
18#include "hw/qdev-core.h"
19#include "qapi/qapi-types-migration.h"
20#include "qemu/thread.h"
21#include "qemu/coroutine_int.h"
22#include "io/channel.h"
23#include "io/channel-buffer.h"
24#include "net/announce.h"
25#include "qom/object.h"
26#include "postcopy-ram.h"
27
28struct PostcopyBlocktimeContext;
29
30#define MIGRATION_RESUME_ACK_VALUE (1)
31
32
33
34
35
36
37#define CLEAR_BITMAP_SHIFT_MIN 6
38
39
40
41
42#define CLEAR_BITMAP_SHIFT_DEFAULT 18
43
44
45
46
47#define CLEAR_BITMAP_SHIFT_MAX 31
48
49
50typedef struct {
51
52
53
54
55 void *tmp_huge_page;
56
57
58
59
60 void *host_addr;
61
62 unsigned int target_pages;
63
64 bool all_zero;
65} PostcopyTmpPage;
66
67
68struct MigrationIncomingState {
69 QEMUFile *from_src_file;
70
71 RAMBlock *last_recv_block[RAM_CHANNEL_MAX];
72
73 void *transport_data;
74 void (*transport_cleanup)(void *data);
75
76
77
78
79 QemuSemaphore thread_sync_sem;
80
81
82
83
84 QemuEvent main_thread_load_event;
85
86
87 AnnounceTimer announce_timer;
88
89 size_t largest_page_size;
90 bool have_fault_thread;
91 QemuThread fault_thread;
92
93 bool fault_thread_quit;
94
95 bool have_listen_thread;
96 QemuThread listen_thread;
97
98
99 int userfault_fd;
100
101 int userfault_event_fd;
102 QEMUFile *to_src_file;
103 QemuMutex rp_mutex;
104
105 RAMBlock *last_rb;
106
107
108
109
110
111
112
113
114
115 unsigned int postcopy_channels;
116
117 QEMUFile *postcopy_qemufile_dst;
118
119 QemuThread postcopy_prio_thread;
120 bool postcopy_prio_thread_created;
121
122
123
124
125
126
127
128
129
130
131
132 QemuMutex postcopy_prio_thread_mutex;
133
134
135
136
137 PostcopyTmpPage *postcopy_tmp_pages;
138
139 void *postcopy_tmp_zero_page;
140
141 GArray *postcopy_remote_fds;
142
143 QEMUBH *bh;
144
145 int state;
146
147 bool have_colo_incoming_thread;
148 QemuThread colo_incoming_thread;
149
150 Coroutine *migration_incoming_co;
151 QemuSemaphore colo_incoming_sem;
152
153
154
155
156
157 struct PostcopyBlocktimeContext *blocktime_ctx;
158
159
160 QemuSemaphore postcopy_pause_sem_dst;
161 QemuSemaphore postcopy_pause_sem_fault;
162
163
164
165
166
167
168 QemuSemaphore postcopy_pause_sem_fast_load;
169
170
171 SocketAddressList *socket_address_list;
172
173
174 GTree *page_requested;
175
176 int page_requested_count;
177
178
179
180
181
182
183
184
185
186
187
188 QemuMutex page_request_mutex;
189};
190
191MigrationIncomingState *migration_incoming_get_current(void);
192void migration_incoming_state_destroy(void);
193void migration_incoming_transport_cleanup(MigrationIncomingState *mis);
194
195
196
197void fill_destination_postcopy_migration_info(MigrationInfo *info);
198
199#define TYPE_MIGRATION "migration"
200
201typedef struct MigrationClass MigrationClass;
202DECLARE_OBJ_CHECKERS(MigrationState, MigrationClass,
203 MIGRATION_OBJ, TYPE_MIGRATION)
204
205struct MigrationClass {
206
207 DeviceClass parent_class;
208};
209
210struct MigrationState {
211
212 DeviceState parent_obj;
213
214
215 QemuThread thread;
216 QEMUBH *vm_start_bh;
217 QEMUBH *cleanup_bh;
218
219 QEMUFile *to_dst_file;
220
221 QEMUFile *postcopy_qemufile_src;
222
223
224
225
226
227
228 QemuSemaphore postcopy_qemufile_src_sem;
229 QIOChannelBuffer *bioc;
230
231
232
233
234
235 QemuMutex qemu_file_lock;
236
237
238
239
240 QemuSemaphore rate_limit_sem;
241
242
243 uint64_t iteration_initial_pages;
244
245
246 double pages_per_second;
247
248
249 uint64_t iteration_initial_bytes;
250
251 int64_t iteration_start_time;
252
253
254
255
256
257 int64_t threshold_size;
258
259
260 MigrationParameters parameters;
261
262 int state;
263
264
265 struct {
266
267 QEMUFile *from_dst_file;
268 QemuThread rp_thread;
269 bool error;
270
271
272
273
274
275
276 bool rp_thread_created;
277 QemuSemaphore rp_sem;
278 } rp_state;
279
280 double mbps;
281
282 int64_t start_time;
283
284 int64_t total_time;
285
286 int64_t downtime_start;
287 int64_t downtime;
288 int64_t expected_downtime;
289 bool enabled_capabilities[MIGRATION_CAPABILITY__MAX];
290 int64_t setup_time;
291
292
293
294
295
296 bool vm_was_running;
297
298
299 bool start_postcopy;
300
301 bool postcopy_after_devices;
302
303
304 bool migration_thread_running;
305
306
307 bool block_inactive;
308
309
310 QemuSemaphore wait_unplug_sem;
311
312
313 QemuSemaphore pause_sem;
314
315
316 QemuSemaphore colo_exit_sem;
317
318
319 QemuEvent colo_checkpoint_event;
320 int64_t colo_checkpoint_time;
321 QEMUTimer *colo_delay_timer;
322
323
324
325 Error *error;
326
327 QemuMutex error_mutex;
328
329
330
331 bool must_remove_block_options;
332
333
334
335
336
337 bool store_global_state;
338
339
340 bool send_configuration;
341
342 bool send_section_footer;
343
344
345
346
347
348
349 bool postcopy_preempt_break_huge;
350
351
352 QemuSemaphore postcopy_pause_sem;
353 QemuSemaphore postcopy_pause_rp_sem;
354
355
356
357
358
359
360 bool decompress_error_check;
361
362
363
364
365
366
367
368
369
370 uint8_t clear_bitmap_shift;
371
372
373
374
375 char *hostname;
376};
377
378void migrate_set_state(int *state, int old_state, int new_state);
379
380void migration_fd_process_incoming(QEMUFile *f, Error **errp);
381void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp);
382void migration_incoming_process(void);
383
384bool migration_has_all_channels(void);
385
386uint64_t migrate_max_downtime(void);
387
388void migrate_set_error(MigrationState *s, const Error *error);
389void migrate_fd_error(MigrationState *s, const Error *error);
390
391void migrate_fd_connect(MigrationState *s, Error *error_in);
392
393bool migration_is_setup_or_active(int state);
394bool migration_is_running(int state);
395
396void migrate_init(MigrationState *s);
397bool migration_is_blocked(Error **errp);
398
399bool migration_in_postcopy(void);
400MigrationState *migrate_get_current(void);
401
402bool migrate_postcopy(void);
403
404bool migrate_release_ram(void);
405bool migrate_postcopy_ram(void);
406bool migrate_zero_blocks(void);
407bool migrate_dirty_bitmaps(void);
408bool migrate_ignore_shared(void);
409bool migrate_validate_uuid(void);
410
411bool migrate_auto_converge(void);
412bool migrate_use_multifd(void);
413bool migrate_pause_before_switchover(void);
414int migrate_multifd_channels(void);
415MultiFDCompression migrate_multifd_compression(void);
416int migrate_multifd_zlib_level(void);
417int migrate_multifd_zstd_level(void);
418
419#ifdef CONFIG_LINUX
420bool migrate_use_zero_copy_send(void);
421#else
422#define migrate_use_zero_copy_send() (false)
423#endif
424int migrate_use_tls(void);
425int migrate_use_xbzrle(void);
426uint64_t migrate_xbzrle_cache_size(void);
427bool migrate_colo_enabled(void);
428
429bool migrate_use_block(void);
430bool migrate_use_block_incremental(void);
431int migrate_max_cpu_throttle(void);
432bool migrate_use_return_path(void);
433
434uint64_t ram_get_total_transferred_pages(void);
435
436bool migrate_use_compression(void);
437int migrate_compress_level(void);
438int migrate_compress_threads(void);
439int migrate_compress_wait_thread(void);
440int migrate_decompress_threads(void);
441bool migrate_use_events(void);
442bool migrate_postcopy_blocktime(void);
443bool migrate_background_snapshot(void);
444bool migrate_postcopy_preempt(void);
445
446
447void migrate_send_rp_shut(MigrationIncomingState *mis,
448 uint32_t value);
449void migrate_send_rp_pong(MigrationIncomingState *mis,
450 uint32_t value);
451int migrate_send_rp_req_pages(MigrationIncomingState *mis, RAMBlock *rb,
452 ram_addr_t start, uint64_t haddr);
453int migrate_send_rp_message_req_pages(MigrationIncomingState *mis,
454 RAMBlock *rb, ram_addr_t start);
455void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
456 char *block_name);
457void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value);
458
459void dirty_bitmap_mig_before_vm_start(void);
460void dirty_bitmap_mig_cancel_outgoing(void);
461void dirty_bitmap_mig_cancel_incoming(void);
462bool check_dirty_bitmap_mig_alias_map(const BitmapMigrationNodeAliasList *bbm,
463 Error **errp);
464
465void migrate_add_address(SocketAddress *address);
466
467int foreach_not_ignored_block(RAMBlockIterFunc func, void *opaque);
468
469#define qemu_ram_foreach_block \
470 #warning "Use foreach_not_ignored_block in migration code"
471
472void migration_make_urgent_request(void);
473void migration_consume_urgent_request(void);
474bool migration_rate_limit(void);
475void migration_cancel(const Error *error);
476
477void populate_vfio_info(MigrationInfo *info);
478void postcopy_temp_page_reset(PostcopyTmpPage *tmp_page);
479
480bool migrate_multi_channels_is_allowed(void);
481void migrate_protocol_allow_multi_channels(bool allow);
482
483#endif
484