4 * Copyright IBM, Corp. 2008
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #ifndef QEMU_MIGRATION_H
15 #define QEMU_MIGRATION_H
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"
28 struct PostcopyBlocktimeContext;
30 #define MIGRATION_RESUME_ACK_VALUE (1)
33 * 1<<6=64 pages -> 256K chunk when page size is 4K. This gives us
34 * the benefit that all the chunks are 64 pages aligned then the
35 * bitmaps are always aligned to LONG.
37 #define CLEAR_BITMAP_SHIFT_MIN 6
39 * 1<<18=256K pages -> 1G chunk when page size is 4K. This is the
40 * default value to use if no one specified.
42 #define CLEAR_BITMAP_SHIFT_DEFAULT 18
44 * 1<<31=2G pages -> 8T chunk when page size is 4K. This should be
45 * big enough and make sure we won't overflow easily.
47 #define CLEAR_BITMAP_SHIFT_MAX 31
49 /* This is an abstraction of a "temp huge page" for postcopy's purpose */
52 * This points to a temporary huge page as a buffer for UFFDIO_COPY. It's
53 * mmap()ed and needs to be freed when cleanup.
57 * This points to the host page we're going to install for this temp page.
58 * It tells us after we've received the whole page, where we should put it.
61 /* Number of small pages copied (in size of TARGET_PAGE_SIZE) */
62 unsigned int target_pages;
63 /* Whether this page contains all zeros */
67 /* State for the incoming migration */
68 struct MigrationIncomingState {
69 QEMUFile *from_src_file;
70 /* Previously received RAM's RAMBlock pointer */
71 RAMBlock *last_recv_block[RAM_CHANNEL_MAX];
72 /* A hook to allow cleanup at the end of incoming migration */
74 void (*transport_cleanup)(void *data);
76 * Used to sync thread creations. Note that we can't create threads in
77 * parallel with this sem.
79 QemuSemaphore thread_sync_sem;
81 * Free at the start of the main state load, set as the main thread finishes
84 QemuEvent main_thread_load_event;
86 /* For network announces */
87 AnnounceTimer announce_timer;
89 size_t largest_page_size;
90 bool have_fault_thread;
91 QemuThread fault_thread;
92 /* Set this when we want the fault thread to quit */
93 bool fault_thread_quit;
95 bool have_listen_thread;
96 QemuThread listen_thread;
98 /* For the kernel to send us notifications */
100 /* To notify the fault_thread to wake, e.g., when need to quit */
101 int userfault_event_fd;
102 QEMUFile *to_src_file;
103 QemuMutex rp_mutex; /* We send replies from multiple threads */
104 /* RAMBlock of last request sent to source */
107 * Number of postcopy channels including the default precopy channel, so
108 * vanilla postcopy will only contain one channel which contain both
109 * precopy and postcopy streams.
111 * This is calculated when the src requests to enable postcopy but before
112 * it starts. Its value can depend on e.g. whether postcopy preemption is
115 unsigned int postcopy_channels;
116 /* QEMUFile for postcopy only; it'll be handled by a separate thread */
117 QEMUFile *postcopy_qemufile_dst;
118 /* Postcopy priority thread is used to receive postcopy requested pages */
119 QemuThread postcopy_prio_thread;
120 bool postcopy_prio_thread_created;
122 * Used to sync between the ram load main thread and the fast ram load
123 * thread. It protects postcopy_qemufile_dst, which is the postcopy
126 * The ram fast load thread will take it mostly for the whole lifecycle
127 * because it needs to continuously read data from the channel, and
128 * it'll only release this mutex if postcopy is interrupted, so that
129 * the ram load main thread will take this mutex over and properly
130 * release the broken channel.
132 QemuMutex postcopy_prio_thread_mutex;
134 * An array of temp host huge pages to be used, one for each postcopy
137 PostcopyTmpPage *postcopy_tmp_pages;
138 /* This is shared for all postcopy channels */
139 void *postcopy_tmp_zero_page;
140 /* PostCopyFD's for external userfaultfds & handlers of shared memory */
141 GArray *postcopy_remote_fds;
147 bool have_colo_incoming_thread;
148 QemuThread colo_incoming_thread;
149 /* The coroutine we should enter (back) after failover */
150 Coroutine *migration_incoming_co;
151 QemuSemaphore colo_incoming_sem;
154 * PostcopyBlocktimeContext to keep information for postcopy
155 * live migration, to calculate vCPU block time
157 struct PostcopyBlocktimeContext *blocktime_ctx;
159 /* notify PAUSED postcopy incoming migrations to try to continue */
160 QemuSemaphore postcopy_pause_sem_dst;
161 QemuSemaphore postcopy_pause_sem_fault;
163 * This semaphore is used to allow the ram fast load thread (only when
164 * postcopy preempt is enabled) fall into sleep when there's network
165 * interruption detected. When the recovery is done, the main load
166 * thread will kick the fast ram load thread using this semaphore.
168 QemuSemaphore postcopy_pause_sem_fast_load;
170 /* List of listening socket addresses */
171 SocketAddressList *socket_address_list;
173 /* A tree of pages that we requested to the source VM */
174 GTree *page_requested;
175 /* For debugging purpose only, but would be nice to keep */
176 int page_requested_count;
178 * The mutex helps to maintain the requested pages that we sent to the
179 * source, IOW, to guarantee coherent between the page_requests tree and
180 * the per-ramblock receivedmap. Note! This does not guarantee consistency
181 * of the real page copy procedures (using UFFDIO_[ZERO]COPY). E.g., even
182 * if one bit in receivedmap is cleared, UFFDIO_COPY could have happened
183 * for that page already. This is intended so that the mutex won't
184 * serialize and blocked by slow operations like UFFDIO_* ioctls. However
185 * this should be enough to make sure the page_requested tree always
186 * contains valid information.
188 QemuMutex page_request_mutex;
191 MigrationIncomingState *migration_incoming_get_current(void);
192 void migration_incoming_state_destroy(void);
193 void migration_incoming_transport_cleanup(MigrationIncomingState *mis);
195 * Functions to work with blocktime context
197 void fill_destination_postcopy_migration_info(MigrationInfo *info);
199 #define TYPE_MIGRATION "migration"
201 typedef struct MigrationClass MigrationClass;
202 DECLARE_OBJ_CHECKERS(MigrationState, MigrationClass,
203 MIGRATION_OBJ, TYPE_MIGRATION)
205 struct MigrationClass {
207 DeviceClass parent_class;
210 struct MigrationState {
212 DeviceState parent_obj;
218 /* Protected by qemu_file_lock */
219 QEMUFile *to_dst_file;
220 /* Postcopy specific transfer channel */
221 QEMUFile *postcopy_qemufile_src;
222 QIOChannelBuffer *bioc;
224 * Protects to_dst_file/from_dst_file pointers. We need to make sure we
225 * won't yield or hang during the critical section, since this lock will be
226 * used in OOB command handler.
228 QemuMutex qemu_file_lock;
231 * Used to allow urgent requests to override rate limiting.
233 QemuSemaphore rate_limit_sem;
235 /* pages already send at the beginning of current iteration */
236 uint64_t iteration_initial_pages;
238 /* pages transferred per second */
239 double pages_per_second;
241 /* bytes already send at the beginning of current iteration */
242 uint64_t iteration_initial_bytes;
243 /* time at the start of current iteration */
244 int64_t iteration_start_time;
246 * The final stage happens when the remaining data is smaller than
247 * this threshold; it's calculated from the requested downtime and
250 int64_t threshold_size;
252 /* params from 'migrate-set-parameters' */
253 MigrationParameters parameters;
257 /* State related to return path */
259 /* Protected by qemu_file_lock */
260 QEMUFile *from_dst_file;
261 QemuThread rp_thread;
264 * We can also check non-zero of rp_thread, but there's no "official"
265 * way to do this, so this bool makes it slightly more elegant.
266 * Checking from_dst_file for this is racy because from_dst_file will
267 * be cleared in the rp_thread!
269 bool rp_thread_created;
270 QemuSemaphore rp_sem;
274 /* Timestamp when recent migration starts (ms) */
276 /* Total time used by latest migration (ms) */
278 /* Timestamp when VM is down (ms) to migrate the last stuff */
279 int64_t downtime_start;
281 int64_t expected_downtime;
282 bool enabled_capabilities[MIGRATION_CAPABILITY__MAX];
285 * Whether guest was running when we enter the completion stage.
286 * If migration is interrupted by any reason, we need to continue
287 * running the guest on source.
291 /* Flag set once the migration has been asked to enter postcopy */
293 /* Flag set after postcopy has sent the device state */
294 bool postcopy_after_devices;
296 /* Flag set once the migration thread is running (and needs joining) */
297 bool migration_thread_running;
299 /* Flag set once the migration thread called bdrv_inactivate_all */
302 /* Migration is waiting for guest to unplug device */
303 QemuSemaphore wait_unplug_sem;
305 /* Migration is paused due to pause-before-switchover */
306 QemuSemaphore pause_sem;
308 /* The semaphore is used to notify COLO thread that failover is finished */
309 QemuSemaphore colo_exit_sem;
311 /* The event is used to notify COLO thread to do checkpoint */
312 QemuEvent colo_checkpoint_event;
313 int64_t colo_checkpoint_time;
314 QEMUTimer *colo_delay_timer;
316 /* The first error that has occurred.
317 We used the mutex to be able to return the 1st error message */
319 /* mutex to protect errp */
320 QemuMutex error_mutex;
322 /* Do we have to clean up -b/-i from old migrate parameters */
323 /* This feature is deprecated and will be removed */
324 bool must_remove_block_options;
327 * Global switch on whether we need to store the global state
330 bool store_global_state;
332 /* Whether we send QEMU_VM_CONFIGURATION during migration */
333 bool send_configuration;
334 /* Whether we send section footer during migration */
335 bool send_section_footer;
337 /* Needed by postcopy-pause state */
338 QemuSemaphore postcopy_pause_sem;
339 QemuSemaphore postcopy_pause_rp_sem;
341 * Whether we abort the migration if decompression errors are
342 * detected at the destination. It is left at false for qemu
343 * older than 3.0, since only newer qemu sends streams that
344 * do not trigger spurious decompression errors.
346 bool decompress_error_check;
349 * This decides the size of guest memory chunk that will be used
350 * to track dirty bitmap clearing. The size of memory chunk will
351 * be GUEST_PAGE_SIZE << N. Say, N=0 means we will clear dirty
352 * bitmap for each page to send (1<<0=1); N=10 means we will clear
353 * dirty bitmap only once for 1<<10=1K continuous guest pages
354 * (which is in 4M chunk).
356 uint8_t clear_bitmap_shift;
359 * This save hostname when out-going migration starts
364 void migrate_set_state(int *state, int old_state, int new_state);
366 void migration_fd_process_incoming(QEMUFile *f, Error **errp);
367 void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp);
368 void migration_incoming_process(void);
370 bool migration_has_all_channels(void);
372 uint64_t migrate_max_downtime(void);
374 void migrate_set_error(MigrationState *s, const Error *error);
375 void migrate_fd_error(MigrationState *s, const Error *error);
377 void migrate_fd_connect(MigrationState *s, Error *error_in);
379 bool migration_is_setup_or_active(int state);
380 bool migration_is_running(int state);
382 void migrate_init(MigrationState *s);
383 bool migration_is_blocked(Error **errp);
384 /* True if outgoing migration has entered postcopy phase */
385 bool migration_in_postcopy(void);
386 MigrationState *migrate_get_current(void);
388 bool migrate_postcopy(void);
390 bool migrate_release_ram(void);
391 bool migrate_postcopy_ram(void);
392 bool migrate_zero_blocks(void);
393 bool migrate_dirty_bitmaps(void);
394 bool migrate_ignore_shared(void);
395 bool migrate_validate_uuid(void);
397 bool migrate_auto_converge(void);
398 bool migrate_use_multifd(void);
399 bool migrate_pause_before_switchover(void);
400 int migrate_multifd_channels(void);
401 MultiFDCompression migrate_multifd_compression(void);
402 int migrate_multifd_zlib_level(void);
403 int migrate_multifd_zstd_level(void);
406 bool migrate_use_zero_copy_send(void);
408 #define migrate_use_zero_copy_send() (false)
410 int migrate_use_tls(void);
411 int migrate_use_xbzrle(void);
412 uint64_t migrate_xbzrle_cache_size(void);
413 bool migrate_colo_enabled(void);
415 bool migrate_use_block(void);
416 bool migrate_use_block_incremental(void);
417 int migrate_max_cpu_throttle(void);
418 bool migrate_use_return_path(void);
420 uint64_t ram_get_total_transferred_pages(void);
422 bool migrate_use_compression(void);
423 int migrate_compress_level(void);
424 int migrate_compress_threads(void);
425 int migrate_compress_wait_thread(void);
426 int migrate_decompress_threads(void);
427 bool migrate_use_events(void);
428 bool migrate_postcopy_blocktime(void);
429 bool migrate_background_snapshot(void);
430 bool migrate_postcopy_preempt(void);
432 /* Sending on the return path - generic and then for each message type */
433 void migrate_send_rp_shut(MigrationIncomingState *mis,
435 void migrate_send_rp_pong(MigrationIncomingState *mis,
437 int migrate_send_rp_req_pages(MigrationIncomingState *mis, RAMBlock *rb,
438 ram_addr_t start, uint64_t haddr);
439 int migrate_send_rp_message_req_pages(MigrationIncomingState *mis,
440 RAMBlock *rb, ram_addr_t start);
441 void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
443 void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value);
445 void dirty_bitmap_mig_before_vm_start(void);
446 void dirty_bitmap_mig_cancel_outgoing(void);
447 void dirty_bitmap_mig_cancel_incoming(void);
448 bool check_dirty_bitmap_mig_alias_map(const BitmapMigrationNodeAliasList *bbm,
451 void migrate_add_address(SocketAddress *address);
453 int foreach_not_ignored_block(RAMBlockIterFunc func, void *opaque);
455 #define qemu_ram_foreach_block \
456 #warning "Use foreach_not_ignored_block in migration code"
458 void migration_make_urgent_request(void);
459 void migration_consume_urgent_request(void);
460 bool migration_rate_limit(void);
461 void migration_cancel(const Error *error);
463 void populate_vfio_info(MigrationInfo *info);
464 void postcopy_temp_page_reset(PostcopyTmpPage *tmp_page);
466 bool migrate_multi_channels_is_allowed(void);
467 void migrate_protocol_allow_multi_channels(bool allow);