4 * Copyright IBM, Corp. 2008
7 * Anthony Liguori <aliguori@us.ibm.com>
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 "net/announce.h"
24 #include "qom/object.h"
26 struct PostcopyBlocktimeContext
;
28 #define MIGRATION_RESUME_ACK_VALUE (1)
31 * 1<<6=64 pages -> 256K chunk when page size is 4K. This gives us
32 * the benefit that all the chunks are 64 pages aligned then the
33 * bitmaps are always aligned to LONG.
35 #define CLEAR_BITMAP_SHIFT_MIN 6
37 * 1<<18=256K pages -> 1G chunk when page size is 4K. This is the
38 * default value to use if no one specified.
40 #define CLEAR_BITMAP_SHIFT_DEFAULT 18
42 * 1<<31=2G pages -> 8T chunk when page size is 4K. This should be
43 * big enough and make sure we won't overflow easily.
45 #define CLEAR_BITMAP_SHIFT_MAX 31
47 /* State for the incoming migration */
48 struct MigrationIncomingState
{
49 QEMUFile
*from_src_file
;
52 * Free at the start of the main state load, set as the main thread finishes
55 QemuEvent main_thread_load_event
;
57 /* For network announces */
58 AnnounceTimer announce_timer
;
60 size_t largest_page_size
;
61 bool have_fault_thread
;
62 QemuThread fault_thread
;
63 QemuSemaphore fault_thread_sem
;
64 /* Set this when we want the fault thread to quit */
65 bool fault_thread_quit
;
67 bool have_listen_thread
;
68 QemuThread listen_thread
;
69 QemuSemaphore listen_thread_sem
;
71 /* For the kernel to send us notifications */
73 /* To notify the fault_thread to wake, e.g., when need to quit */
74 int userfault_event_fd
;
75 QEMUFile
*to_src_file
;
76 QemuMutex rp_mutex
; /* We send replies from multiple threads */
77 /* RAMBlock of last request sent to source */
79 void *postcopy_tmp_page
;
80 void *postcopy_tmp_zero_page
;
81 /* PostCopyFD's for external userfaultfds & handlers of shared memory */
82 GArray
*postcopy_remote_fds
;
88 bool have_colo_incoming_thread
;
89 QemuThread colo_incoming_thread
;
90 /* The coroutine we should enter (back) after failover */
91 Coroutine
*migration_incoming_co
;
92 QemuSemaphore colo_incoming_sem
;
95 * PostcopyBlocktimeContext to keep information for postcopy
96 * live migration, to calculate vCPU block time
98 struct PostcopyBlocktimeContext
*blocktime_ctx
;
100 /* notify PAUSED postcopy incoming migrations to try to continue */
101 bool postcopy_recover_triggered
;
102 QemuSemaphore postcopy_pause_sem_dst
;
103 QemuSemaphore postcopy_pause_sem_fault
;
105 /* List of listening socket addresses */
106 SocketAddressList
*socket_address_list
;
109 MigrationIncomingState
*migration_incoming_get_current(void);
110 void migration_incoming_state_destroy(void);
112 * Functions to work with blocktime context
114 void fill_destination_postcopy_migration_info(MigrationInfo
*info
);
116 #define TYPE_MIGRATION "migration"
118 typedef struct MigrationClass MigrationClass
;
119 DECLARE_OBJ_CHECKERS(MigrationState
, MigrationClass
,
120 MIGRATION_OBJ
, TYPE_MIGRATION
)
122 struct MigrationClass
{
124 DeviceClass parent_class
;
127 struct MigrationState
130 DeviceState parent_obj
;
135 QEMUFile
*to_dst_file
;
137 * Protects to_dst_file pointer. We need to make sure we won't
138 * yield or hang during the critical section, since this lock will
139 * be used in OOB command handler.
141 QemuMutex qemu_file_lock
;
144 * Used to allow urgent requests to override rate limiting.
146 QemuSemaphore rate_limit_sem
;
148 /* pages already send at the beginning of current iteration */
149 uint64_t iteration_initial_pages
;
151 /* pages transferred per second */
152 double pages_per_second
;
154 /* bytes already send at the beginning of current iteration */
155 uint64_t iteration_initial_bytes
;
156 /* time at the start of current iteration */
157 int64_t iteration_start_time
;
159 * The final stage happens when the remaining data is smaller than
160 * this threshold; it's calculated from the requested downtime and
163 int64_t threshold_size
;
165 /* params from 'migrate-set-parameters' */
166 MigrationParameters parameters
;
170 /* State related to return path */
172 QEMUFile
*from_dst_file
;
173 QemuThread rp_thread
;
175 QemuSemaphore rp_sem
;
179 /* Timestamp when recent migration starts (ms) */
181 /* Total time used by latest migration (ms) */
183 /* Timestamp when VM is down (ms) to migrate the last stuff */
184 int64_t downtime_start
;
186 int64_t expected_downtime
;
187 bool enabled_capabilities
[MIGRATION_CAPABILITY__MAX
];
190 * Whether guest was running when we enter the completion stage.
191 * If migration is interrupted by any reason, we need to continue
192 * running the guest on source.
196 /* Flag set once the migration has been asked to enter postcopy */
198 /* Flag set after postcopy has sent the device state */
199 bool postcopy_after_devices
;
201 /* Flag set once the migration thread is running (and needs joining) */
202 bool migration_thread_running
;
204 /* Flag set once the migration thread called bdrv_inactivate_all */
207 /* Migration is waiting for guest to unplug device */
208 QemuSemaphore wait_unplug_sem
;
210 /* Migration is paused due to pause-before-switchover */
211 QemuSemaphore pause_sem
;
213 /* The semaphore is used to notify COLO thread that failover is finished */
214 QemuSemaphore colo_exit_sem
;
216 /* The event is used to notify COLO thread to do checkpoint */
217 QemuEvent colo_checkpoint_event
;
218 int64_t colo_checkpoint_time
;
219 QEMUTimer
*colo_delay_timer
;
221 /* The first error that has occurred.
222 We used the mutex to be able to return the 1st error message */
224 /* mutex to protect errp */
225 QemuMutex error_mutex
;
227 /* Do we have to clean up -b/-i from old migrate parameters */
228 /* This feature is deprecated and will be removed */
229 bool must_remove_block_options
;
232 * Global switch on whether we need to store the global state
235 bool store_global_state
;
237 /* Whether we send QEMU_VM_CONFIGURATION during migration */
238 bool send_configuration
;
239 /* Whether we send section footer during migration */
240 bool send_section_footer
;
242 /* Needed by postcopy-pause state */
243 QemuSemaphore postcopy_pause_sem
;
244 QemuSemaphore postcopy_pause_rp_sem
;
246 * Whether we abort the migration if decompression errors are
247 * detected at the destination. It is left at false for qemu
248 * older than 3.0, since only newer qemu sends streams that
249 * do not trigger spurious decompression errors.
251 bool decompress_error_check
;
254 * This decides the size of guest memory chunk that will be used
255 * to track dirty bitmap clearing. The size of memory chunk will
256 * be GUEST_PAGE_SIZE << N. Say, N=0 means we will clear dirty
257 * bitmap for each page to send (1<<0=1); N=10 means we will clear
258 * dirty bitmap only once for 1<<10=1K continuous guest pages
259 * (which is in 4M chunk).
261 uint8_t clear_bitmap_shift
;
264 void migrate_set_state(int *state
, int old_state
, int new_state
);
266 void migration_fd_process_incoming(QEMUFile
*f
, Error
**errp
);
267 void migration_ioc_process_incoming(QIOChannel
*ioc
, Error
**errp
);
268 void migration_incoming_process(void);
270 bool migration_has_all_channels(void);
272 uint64_t migrate_max_downtime(void);
274 void migrate_set_error(MigrationState
*s
, const Error
*error
);
275 void migrate_fd_error(MigrationState
*s
, const Error
*error
);
277 void migrate_fd_connect(MigrationState
*s
, Error
*error_in
);
279 bool migration_is_setup_or_active(int state
);
280 bool migration_is_running(int state
);
282 void migrate_init(MigrationState
*s
);
283 bool migration_is_blocked(Error
**errp
);
284 /* True if outgoing migration has entered postcopy phase */
285 bool migration_in_postcopy(void);
286 MigrationState
*migrate_get_current(void);
288 bool migrate_postcopy(void);
290 bool migrate_release_ram(void);
291 bool migrate_postcopy_ram(void);
292 bool migrate_zero_blocks(void);
293 bool migrate_dirty_bitmaps(void);
294 bool migrate_ignore_shared(void);
295 bool migrate_validate_uuid(void);
297 bool migrate_auto_converge(void);
298 bool migrate_use_multifd(void);
299 bool migrate_pause_before_switchover(void);
300 int migrate_multifd_channels(void);
301 MultiFDCompression
migrate_multifd_compression(void);
302 int migrate_multifd_zlib_level(void);
303 int migrate_multifd_zstd_level(void);
305 int migrate_use_xbzrle(void);
306 int64_t migrate_xbzrle_cache_size(void);
307 bool migrate_colo_enabled(void);
309 bool migrate_use_block(void);
310 bool migrate_use_block_incremental(void);
311 int migrate_max_cpu_throttle(void);
312 bool migrate_use_return_path(void);
314 uint64_t ram_get_total_transferred_pages(void);
316 bool migrate_use_compression(void);
317 int migrate_compress_level(void);
318 int migrate_compress_threads(void);
319 int migrate_compress_wait_thread(void);
320 int migrate_decompress_threads(void);
321 bool migrate_use_events(void);
322 bool migrate_postcopy_blocktime(void);
324 /* Sending on the return path - generic and then for each message type */
325 void migrate_send_rp_shut(MigrationIncomingState
*mis
,
327 void migrate_send_rp_pong(MigrationIncomingState
*mis
,
329 int migrate_send_rp_req_pages(MigrationIncomingState
*mis
, RAMBlock
*rb
,
331 void migrate_send_rp_recv_bitmap(MigrationIncomingState
*mis
,
333 void migrate_send_rp_resume_ack(MigrationIncomingState
*mis
, uint32_t value
);
335 void dirty_bitmap_mig_before_vm_start(void);
336 void dirty_bitmap_mig_cancel_outgoing(void);
337 void dirty_bitmap_mig_cancel_incoming(void);
338 bool check_dirty_bitmap_mig_alias_map(const BitmapMigrationNodeAliasList
*bbm
,
341 void migrate_add_address(SocketAddress
*address
);
343 int foreach_not_ignored_block(RAMBlockIterFunc func
, void *opaque
);
345 #define qemu_ram_foreach_block \
346 #warning "Use foreach_not_ignored_block in migration code"
348 void migration_make_urgent_request(void);
349 void migration_consume_urgent_request(void);
350 bool migration_rate_limit(void);