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 "qapi-types.h"
23 #include "exec/cpu-common.h"
24 #include "qemu/coroutine_int.h"
26 #define QEMU_VM_FILE_MAGIC 0x5145564d
27 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
28 #define QEMU_VM_FILE_VERSION 0x00000003
30 #define QEMU_VM_EOF 0x00
31 #define QEMU_VM_SECTION_START 0x01
32 #define QEMU_VM_SECTION_PART 0x02
33 #define QEMU_VM_SECTION_END 0x03
34 #define QEMU_VM_SECTION_FULL 0x04
35 #define QEMU_VM_SUBSECTION 0x05
36 #define QEMU_VM_VMDESCRIPTION 0x06
37 #define QEMU_VM_CONFIGURATION 0x07
38 #define QEMU_VM_COMMAND 0x08
39 #define QEMU_VM_SECTION_FOOTER 0x7e
41 struct MigrationParams
{
46 /* Messages sent on the return path from destination to source */
47 enum mig_rp_message_type
{
48 MIG_RP_MSG_INVALID
= 0, /* Must be 0 */
49 MIG_RP_MSG_SHUT
, /* sibling will not send any more RP messages */
50 MIG_RP_MSG_PONG
, /* Response to a PING; data (seq: be32 ) */
52 MIG_RP_MSG_REQ_PAGES_ID
, /* data (start: be64, len: be32, id: string) */
53 MIG_RP_MSG_REQ_PAGES
, /* data (start: be64, len: be32) */
58 typedef QLIST_HEAD(, LoadStateEntry
) LoadStateEntry_Head
;
60 /* State for the incoming migration */
61 struct MigrationIncomingState
{
62 QEMUFile
*from_src_file
;
65 * Free at the start of the main state load, set as the main thread finishes
68 QemuEvent main_thread_load_event
;
70 size_t largest_page_size
;
71 bool have_fault_thread
;
72 QemuThread fault_thread
;
73 QemuSemaphore fault_thread_sem
;
75 bool have_listen_thread
;
76 QemuThread listen_thread
;
77 QemuSemaphore listen_thread_sem
;
79 /* For the kernel to send us notifications */
81 /* To tell the fault_thread to quit */
82 int userfault_quit_fd
;
83 QEMUFile
*to_src_file
;
84 QemuMutex rp_mutex
; /* We send replies from multiple threads */
85 void *postcopy_tmp_page
;
86 void *postcopy_tmp_zero_page
;
92 bool have_colo_incoming_thread
;
93 QemuThread colo_incoming_thread
;
94 /* The coroutine we should enter (back) after failover */
95 Coroutine
*migration_incoming_co
;
96 QemuSemaphore colo_incoming_sem
;
99 LoadStateEntry_Head loadvm_handlers
;
102 MigrationIncomingState
*migration_incoming_get_current(void);
103 void migration_incoming_state_destroy(void);
105 struct MigrationState
111 QEMUFile
*to_dst_file
;
113 /* New style params from 'migrate-set-parameters' */
114 MigrationParameters parameters
;
117 /* Old style params from 'migrate' command */
118 MigrationParams params
;
120 /* State related to return path */
122 QEMUFile
*from_dst_file
;
123 QemuThread rp_thread
;
130 int64_t expected_downtime
;
131 bool enabled_capabilities
[MIGRATION_CAPABILITY__MAX
];
132 int64_t xbzrle_cache_size
;
135 /* Flag set once the migration has been asked to enter postcopy */
137 /* Flag set after postcopy has sent the device state */
138 bool postcopy_after_devices
;
140 /* Flag set once the migration thread is running (and needs joining) */
141 bool migration_thread_running
;
143 /* Flag set once the migration thread called bdrv_inactivate_all */
146 /* The semaphore is used to notify COLO thread that failover is finished */
147 QemuSemaphore colo_exit_sem
;
149 /* The semaphore is used to notify COLO thread to do checkpoint */
150 QemuSemaphore colo_checkpoint_sem
;
151 int64_t colo_checkpoint_time
;
152 QEMUTimer
*colo_delay_timer
;
154 /* The last error that occurred */
156 /* Do we have to clean up -b/-i from old migrate parameters */
157 /* This feature is deprecated and will be removed */
158 bool must_remove_block_options
;
161 void migrate_set_state(int *state
, int old_state
, int new_state
);
163 void migration_fd_process_incoming(QEMUFile
*f
);
165 void qemu_start_incoming_migration(const char *uri
, Error
**errp
);
167 void migration_channel_process_incoming(MigrationState
*s
,
170 void migration_tls_channel_process_incoming(MigrationState
*s
,
174 void migration_channel_connect(MigrationState
*s
,
176 const char *hostname
);
178 void migration_tls_channel_connect(MigrationState
*s
,
180 const char *hostname
,
183 uint64_t migrate_max_downtime(void);
185 void exec_start_incoming_migration(const char *host_port
, Error
**errp
);
187 void exec_start_outgoing_migration(MigrationState
*s
, const char *host_port
, Error
**errp
);
189 void tcp_start_incoming_migration(const char *host_port
, Error
**errp
);
191 void tcp_start_outgoing_migration(MigrationState
*s
, const char *host_port
, Error
**errp
);
193 void unix_start_incoming_migration(const char *path
, Error
**errp
);
195 void unix_start_outgoing_migration(MigrationState
*s
, const char *path
, Error
**errp
);
197 void fd_start_incoming_migration(const char *path
, Error
**errp
);
199 void fd_start_outgoing_migration(MigrationState
*s
, const char *fdname
, Error
**errp
);
201 void rdma_start_outgoing_migration(void *opaque
, const char *host_port
, Error
**errp
);
203 void rdma_start_incoming_migration(const char *host_port
, Error
**errp
);
205 void migrate_fd_error(MigrationState
*s
, const Error
*error
);
207 void migrate_fd_connect(MigrationState
*s
);
209 void add_migration_state_change_notifier(Notifier
*notify
);
210 void remove_migration_state_change_notifier(Notifier
*notify
);
211 MigrationState
*migrate_init(const MigrationParams
*params
);
212 bool migration_is_blocked(Error
**errp
);
213 bool migration_in_setup(MigrationState
*);
214 bool migration_is_idle(void);
215 bool migration_has_finished(MigrationState
*);
216 bool migration_has_failed(MigrationState
*);
217 /* True if outgoing migration has entered postcopy phase */
218 bool migration_in_postcopy(void);
219 /* ...and after the device transmission */
220 bool migration_in_postcopy_after_devices(MigrationState
*);
221 MigrationState
*migrate_get_current(void);
223 void migrate_compress_threads_create(void);
224 void migrate_compress_threads_join(void);
225 void migrate_decompress_threads_create(void);
226 void migrate_decompress_threads_join(void);
227 uint64_t ram_bytes_remaining(void);
228 uint64_t ram_bytes_transferred(void);
229 uint64_t ram_bytes_total(void);
230 uint64_t ram_dirty_sync_count(void);
231 uint64_t ram_dirty_pages_rate(void);
232 uint64_t ram_postcopy_requests(void);
233 void free_xbzrle_decoded_buf(void);
235 void acct_update_position(QEMUFile
*f
, size_t size
, bool zero
);
237 uint64_t dup_mig_pages_transferred(void);
238 uint64_t norm_mig_pages_transferred(void);
239 uint64_t xbzrle_mig_bytes_transferred(void);
240 uint64_t xbzrle_mig_pages_transferred(void);
241 uint64_t xbzrle_mig_pages_overflow(void);
242 uint64_t xbzrle_mig_pages_cache_miss(void);
243 double xbzrle_mig_cache_miss_rate(void);
245 void ram_handle_compressed(void *host
, uint8_t ch
, uint64_t size
);
246 void ram_debug_dump_bitmap(unsigned long *todump
, bool expected
,
247 unsigned long pages
);
248 /* For outgoing discard bitmap */
249 int ram_postcopy_send_discard_bitmap(MigrationState
*ms
);
250 /* For incoming postcopy discard */
251 int ram_discard_range(const char *block_name
, uint64_t start
, size_t length
);
252 int ram_postcopy_incoming_init(MigrationIncomingState
*mis
);
253 void ram_postcopy_migrated_memory_release(MigrationState
*ms
);
255 bool migrate_release_ram(void);
256 bool migrate_postcopy_ram(void);
257 bool migrate_zero_blocks(void);
259 bool migrate_auto_converge(void);
261 int xbzrle_encode_buffer(uint8_t *old_buf
, uint8_t *new_buf
, int slen
,
262 uint8_t *dst
, int dlen
);
263 int xbzrle_decode_buffer(uint8_t *src
, int slen
, uint8_t *dst
, int dlen
);
265 int migrate_use_xbzrle(void);
266 int64_t migrate_xbzrle_cache_size(void);
267 bool migrate_colo_enabled(void);
269 int64_t xbzrle_cache_resize(int64_t new_size
);
271 bool migrate_use_block(void);
272 bool migrate_use_block_incremental(void);
274 bool migrate_use_compression(void);
275 int migrate_compress_level(void);
276 int migrate_compress_threads(void);
277 int migrate_decompress_threads(void);
278 bool migrate_use_events(void);
280 /* Sending on the return path - generic and then for each message type */
281 void migrate_send_rp_message(MigrationIncomingState
*mis
,
282 enum mig_rp_message_type message_type
,
283 uint16_t len
, void *data
);
284 void migrate_send_rp_shut(MigrationIncomingState
*mis
,
286 void migrate_send_rp_pong(MigrationIncomingState
*mis
,
288 void migrate_send_rp_req_pages(MigrationIncomingState
*mis
, const char* rbname
,
289 ram_addr_t start
, size_t len
);
291 void ram_control_before_iterate(QEMUFile
*f
, uint64_t flags
);
292 void ram_control_after_iterate(QEMUFile
*f
, uint64_t flags
);
293 void ram_control_load_hook(QEMUFile
*f
, uint64_t flags
, void *data
);
295 /* Whenever this is found in the data stream, the flags
296 * will be passed to ram_control_load_hook in the incoming-migration
297 * side. This lets before_ram_iterate/after_ram_iterate add
298 * transport-specific sections to the RAM migration data.
300 #define RAM_SAVE_FLAG_HOOK 0x80
302 #define RAM_SAVE_CONTROL_NOT_SUPP -1000
303 #define RAM_SAVE_CONTROL_DELAYED -2000
305 size_t ram_control_save_page(QEMUFile
*f
, ram_addr_t block_offset
,
306 ram_addr_t offset
, size_t size
,
307 uint64_t *bytes_sent
);
309 void ram_mig_init(void);
310 void savevm_skip_section_footers(void);
311 void register_global_state(void);
312 void global_state_set_optional(void);
313 void savevm_skip_configuration(void);
314 int global_state_store(void);
315 void global_state_store_running(void);
317 void migration_page_queue_free(void);
318 int ram_save_queue_pages(const char *rbname
, ram_addr_t start
, ram_addr_t len
);
319 uint64_t ram_pagesize_summary(void);