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