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 "qapi/qmp/qdict.h"
18 #include "qemu-common.h"
19 #include "qemu/thread.h"
20 #include "qemu/notify.h"
21 #include "migration/vmstate.h"
22 #include "io/channel.h"
23 #include "qapi-types.h"
24 #include "exec/cpu-common.h"
25 #include "qemu/coroutine_int.h"
27 #define QEMU_VM_FILE_MAGIC 0x5145564d
28 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
29 #define QEMU_VM_FILE_VERSION 0x00000003
31 #define QEMU_VM_EOF 0x00
32 #define QEMU_VM_SECTION_START 0x01
33 #define QEMU_VM_SECTION_PART 0x02
34 #define QEMU_VM_SECTION_END 0x03
35 #define QEMU_VM_SECTION_FULL 0x04
36 #define QEMU_VM_SUBSECTION 0x05
37 #define QEMU_VM_VMDESCRIPTION 0x06
38 #define QEMU_VM_CONFIGURATION 0x07
39 #define QEMU_VM_COMMAND 0x08
40 #define QEMU_VM_SECTION_FOOTER 0x7e
42 /* Messages sent on the return path from destination to source */
43 enum mig_rp_message_type
{
44 MIG_RP_MSG_INVALID
= 0, /* Must be 0 */
45 MIG_RP_MSG_SHUT
, /* sibling will not send any more RP messages */
46 MIG_RP_MSG_PONG
, /* Response to a PING; data (seq: be32 ) */
48 MIG_RP_MSG_REQ_PAGES_ID
, /* data (start: be64, len: be32, id: string) */
49 MIG_RP_MSG_REQ_PAGES
, /* data (start: be64, len: be32) */
54 typedef QLIST_HEAD(, LoadStateEntry
) LoadStateEntry_Head
;
56 /* State for the incoming migration */
57 struct MigrationIncomingState
{
58 QEMUFile
*from_src_file
;
61 * Free at the start of the main state load, set as the main thread finishes
64 QemuEvent main_thread_load_event
;
66 size_t largest_page_size
;
67 bool have_fault_thread
;
68 QemuThread fault_thread
;
69 QemuSemaphore fault_thread_sem
;
71 bool have_listen_thread
;
72 QemuThread listen_thread
;
73 QemuSemaphore listen_thread_sem
;
75 /* For the kernel to send us notifications */
77 /* To tell the fault_thread to quit */
78 int userfault_quit_fd
;
79 QEMUFile
*to_src_file
;
80 QemuMutex rp_mutex
; /* We send replies from multiple threads */
81 void *postcopy_tmp_page
;
82 void *postcopy_tmp_zero_page
;
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 LoadStateEntry_Head loadvm_handlers
;
98 MigrationIncomingState
*migration_incoming_get_current(void);
99 void migration_incoming_state_destroy(void);
101 struct MigrationState
107 QEMUFile
*to_dst_file
;
109 /* params from 'migrate-set-parameters' */
110 MigrationParameters parameters
;
114 /* State related to return path */
116 QEMUFile
*from_dst_file
;
117 QemuThread rp_thread
;
124 int64_t expected_downtime
;
125 bool enabled_capabilities
[MIGRATION_CAPABILITY__MAX
];
126 int64_t xbzrle_cache_size
;
129 /* Flag set once the migration has been asked to enter postcopy */
131 /* Flag set after postcopy has sent the device state */
132 bool postcopy_after_devices
;
134 /* Flag set once the migration thread is running (and needs joining) */
135 bool migration_thread_running
;
137 /* Flag set once the migration thread called bdrv_inactivate_all */
140 /* The semaphore is used to notify COLO thread that failover is finished */
141 QemuSemaphore colo_exit_sem
;
143 /* The semaphore is used to notify COLO thread to do checkpoint */
144 QemuSemaphore colo_checkpoint_sem
;
145 int64_t colo_checkpoint_time
;
146 QEMUTimer
*colo_delay_timer
;
148 /* The last error that occurred */
150 /* Do we have to clean up -b/-i from old migrate parameters */
151 /* This feature is deprecated and will be removed */
152 bool must_remove_block_options
;
155 void migrate_set_state(int *state
, int old_state
, int new_state
);
157 void migration_fd_process_incoming(QEMUFile
*f
);
159 void qemu_start_incoming_migration(const char *uri
, Error
**errp
);
161 void migration_tls_channel_process_incoming(MigrationState
*s
,
165 void migration_tls_channel_connect(MigrationState
*s
,
167 const char *hostname
,
170 uint64_t migrate_max_downtime(void);
172 void exec_start_incoming_migration(const char *host_port
, Error
**errp
);
174 void exec_start_outgoing_migration(MigrationState
*s
, const char *host_port
, Error
**errp
);
176 void tcp_start_incoming_migration(const char *host_port
, Error
**errp
);
178 void tcp_start_outgoing_migration(MigrationState
*s
, const char *host_port
, Error
**errp
);
180 void unix_start_incoming_migration(const char *path
, Error
**errp
);
182 void unix_start_outgoing_migration(MigrationState
*s
, const char *path
, Error
**errp
);
184 void fd_start_incoming_migration(const char *path
, Error
**errp
);
186 void fd_start_outgoing_migration(MigrationState
*s
, const char *fdname
, Error
**errp
);
188 void rdma_start_outgoing_migration(void *opaque
, const char *host_port
, Error
**errp
);
190 void rdma_start_incoming_migration(const char *host_port
, Error
**errp
);
192 void migrate_fd_error(MigrationState
*s
, const Error
*error
);
194 void migrate_fd_connect(MigrationState
*s
);
196 void add_migration_state_change_notifier(Notifier
*notify
);
197 void remove_migration_state_change_notifier(Notifier
*notify
);
198 MigrationState
*migrate_init(void);
199 bool migration_is_blocked(Error
**errp
);
200 bool migration_in_setup(MigrationState
*);
201 bool migration_is_idle(void);
202 bool migration_has_finished(MigrationState
*);
203 bool migration_has_failed(MigrationState
*);
204 /* True if outgoing migration has entered postcopy phase */
205 bool migration_in_postcopy(void);
206 /* ...and after the device transmission */
207 bool migration_in_postcopy_after_devices(MigrationState
*);
208 MigrationState
*migrate_get_current(void);
210 void migrate_compress_threads_create(void);
211 void migrate_compress_threads_join(void);
212 void migrate_decompress_threads_create(void);
213 void migrate_decompress_threads_join(void);
214 uint64_t ram_bytes_remaining(void);
215 uint64_t ram_bytes_transferred(void);
216 uint64_t ram_bytes_total(void);
217 uint64_t ram_dirty_sync_count(void);
218 uint64_t ram_dirty_pages_rate(void);
219 uint64_t ram_postcopy_requests(void);
220 void free_xbzrle_decoded_buf(void);
222 void acct_update_position(QEMUFile
*f
, size_t size
, bool zero
);
224 uint64_t dup_mig_pages_transferred(void);
225 uint64_t norm_mig_pages_transferred(void);
226 uint64_t xbzrle_mig_bytes_transferred(void);
227 uint64_t xbzrle_mig_pages_transferred(void);
228 uint64_t xbzrle_mig_pages_overflow(void);
229 uint64_t xbzrle_mig_pages_cache_miss(void);
230 double xbzrle_mig_cache_miss_rate(void);
232 void ram_handle_compressed(void *host
, uint8_t ch
, uint64_t size
);
233 void ram_debug_dump_bitmap(unsigned long *todump
, bool expected
,
234 unsigned long pages
);
235 /* For outgoing discard bitmap */
236 int ram_postcopy_send_discard_bitmap(MigrationState
*ms
);
237 /* For incoming postcopy discard */
238 int ram_discard_range(const char *block_name
, uint64_t start
, size_t length
);
239 int ram_postcopy_incoming_init(MigrationIncomingState
*mis
);
240 void ram_postcopy_migrated_memory_release(MigrationState
*ms
);
242 bool migrate_release_ram(void);
243 bool migrate_postcopy_ram(void);
244 bool migrate_zero_blocks(void);
246 bool migrate_auto_converge(void);
248 int migrate_use_xbzrle(void);
249 int64_t migrate_xbzrle_cache_size(void);
250 bool migrate_colo_enabled(void);
252 int64_t xbzrle_cache_resize(int64_t new_size
);
254 bool migrate_use_block(void);
255 bool migrate_use_block_incremental(void);
257 bool migrate_use_compression(void);
258 int migrate_compress_level(void);
259 int migrate_compress_threads(void);
260 int migrate_decompress_threads(void);
261 bool migrate_use_events(void);
263 /* Sending on the return path - generic and then for each message type */
264 void migrate_send_rp_message(MigrationIncomingState
*mis
,
265 enum mig_rp_message_type message_type
,
266 uint16_t len
, void *data
);
267 void migrate_send_rp_shut(MigrationIncomingState
*mis
,
269 void migrate_send_rp_pong(MigrationIncomingState
*mis
,
271 void migrate_send_rp_req_pages(MigrationIncomingState
*mis
, const char* rbname
,
272 ram_addr_t start
, size_t len
);
274 void ram_control_before_iterate(QEMUFile
*f
, uint64_t flags
);
275 void ram_control_after_iterate(QEMUFile
*f
, uint64_t flags
);
276 void ram_control_load_hook(QEMUFile
*f
, uint64_t flags
, void *data
);
278 /* Whenever this is found in the data stream, the flags
279 * will be passed to ram_control_load_hook in the incoming-migration
280 * side. This lets before_ram_iterate/after_ram_iterate add
281 * transport-specific sections to the RAM migration data.
283 #define RAM_SAVE_FLAG_HOOK 0x80
285 #define RAM_SAVE_CONTROL_NOT_SUPP -1000
286 #define RAM_SAVE_CONTROL_DELAYED -2000
288 size_t ram_control_save_page(QEMUFile
*f
, ram_addr_t block_offset
,
289 ram_addr_t offset
, size_t size
,
290 uint64_t *bytes_sent
);
292 void ram_mig_init(void);
293 void savevm_skip_section_footers(void);
294 void register_global_state(void);
295 void global_state_set_optional(void);
296 void savevm_skip_configuration(void);
297 int global_state_store(void);
298 void global_state_store_running(void);
300 void migration_page_queue_free(void);
301 int ram_save_queue_pages(const char *rbname
, ram_addr_t start
, ram_addr_t len
);
302 uint64_t ram_pagesize_summary(void);