tcg: Remove TARGET_LONG_BITS, TCG_TYPE_TL
[qemu/ar7.git] / migration / migration.h
blob7721c7658bf0da3146d8c8b4f38f8d8836d5dd35
1 /*
2 * QEMU live migration
4 * Copyright IBM, Corp. 2008
6 * Authors:
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 "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"
29 struct PostcopyBlocktimeContext;
31 #define MIGRATION_RESUME_ACK_VALUE (1)
34 * 1<<6=64 pages -> 256K chunk when page size is 4K. This gives us
35 * the benefit that all the chunks are 64 pages aligned then the
36 * bitmaps are always aligned to LONG.
38 #define CLEAR_BITMAP_SHIFT_MIN 6
40 * 1<<18=256K pages -> 1G chunk when page size is 4K. This is the
41 * default value to use if no one specified.
43 #define CLEAR_BITMAP_SHIFT_DEFAULT 18
45 * 1<<31=2G pages -> 8T chunk when page size is 4K. This should be
46 * big enough and make sure we won't overflow easily.
48 #define CLEAR_BITMAP_SHIFT_MAX 31
50 /* This is an abstraction of a "temp huge page" for postcopy's purpose */
51 typedef struct {
53 * This points to a temporary huge page as a buffer for UFFDIO_COPY. It's
54 * mmap()ed and needs to be freed when cleanup.
56 void *tmp_huge_page;
58 * This points to the host page we're going to install for this temp page.
59 * It tells us after we've received the whole page, where we should put it.
61 void *host_addr;
62 /* Number of small pages copied (in size of TARGET_PAGE_SIZE) */
63 unsigned int target_pages;
64 /* Whether this page contains all zeros */
65 bool all_zero;
66 } PostcopyTmpPage;
68 typedef enum {
69 PREEMPT_THREAD_NONE = 0,
70 PREEMPT_THREAD_CREATED,
71 PREEMPT_THREAD_QUIT,
72 } PreemptThreadStatus;
74 /* State for the incoming migration */
75 struct MigrationIncomingState {
76 QEMUFile *from_src_file;
77 /* Previously received RAM's RAMBlock pointer */
78 RAMBlock *last_recv_block[RAM_CHANNEL_MAX];
79 /* A hook to allow cleanup at the end of incoming migration */
80 void *transport_data;
81 void (*transport_cleanup)(void *data);
83 * Used to sync thread creations. Note that we can't create threads in
84 * parallel with this sem.
86 QemuSemaphore thread_sync_sem;
88 * Free at the start of the main state load, set as the main thread finishes
89 * loading state.
91 QemuEvent main_thread_load_event;
93 /* For network announces */
94 AnnounceTimer announce_timer;
96 size_t largest_page_size;
97 bool have_fault_thread;
98 QemuThread fault_thread;
99 /* Set this when we want the fault thread to quit */
100 bool fault_thread_quit;
102 bool have_listen_thread;
103 QemuThread listen_thread;
105 /* For the kernel to send us notifications */
106 int userfault_fd;
107 /* To notify the fault_thread to wake, e.g., when need to quit */
108 int userfault_event_fd;
109 QEMUFile *to_src_file;
110 QemuMutex rp_mutex; /* We send replies from multiple threads */
111 /* RAMBlock of last request sent to source */
112 RAMBlock *last_rb;
114 * Number of postcopy channels including the default precopy channel, so
115 * vanilla postcopy will only contain one channel which contain both
116 * precopy and postcopy streams.
118 * This is calculated when the src requests to enable postcopy but before
119 * it starts. Its value can depend on e.g. whether postcopy preemption is
120 * enabled.
122 unsigned int postcopy_channels;
123 /* QEMUFile for postcopy only; it'll be handled by a separate thread */
124 QEMUFile *postcopy_qemufile_dst;
126 * When postcopy_qemufile_dst is properly setup, this sem is posted.
127 * One can wait on this semaphore to wait until the preempt channel is
128 * properly setup.
130 QemuSemaphore postcopy_qemufile_dst_done;
131 /* Postcopy priority thread is used to receive postcopy requested pages */
132 QemuThread postcopy_prio_thread;
134 * Always set by the main vm load thread only, but can be read by the
135 * postcopy preempt thread. "volatile" makes sure all reads will be
136 * uptodate across cores.
138 volatile PreemptThreadStatus preempt_thread_status;
140 * Used to sync between the ram load main thread and the fast ram load
141 * thread. It protects postcopy_qemufile_dst, which is the postcopy
142 * fast channel.
144 * The ram fast load thread will take it mostly for the whole lifecycle
145 * because it needs to continuously read data from the channel, and
146 * it'll only release this mutex if postcopy is interrupted, so that
147 * the ram load main thread will take this mutex over and properly
148 * release the broken channel.
150 QemuMutex postcopy_prio_thread_mutex;
152 * An array of temp host huge pages to be used, one for each postcopy
153 * channel.
155 PostcopyTmpPage *postcopy_tmp_pages;
156 /* This is shared for all postcopy channels */
157 void *postcopy_tmp_zero_page;
158 /* PostCopyFD's for external userfaultfds & handlers of shared memory */
159 GArray *postcopy_remote_fds;
161 QEMUBH *bh;
163 int state;
165 /* The coroutine we should enter (back) after failover */
166 Coroutine *migration_incoming_co;
167 QemuSemaphore colo_incoming_sem;
170 * PostcopyBlocktimeContext to keep information for postcopy
171 * live migration, to calculate vCPU block time
172 * */
173 struct PostcopyBlocktimeContext *blocktime_ctx;
175 /* notify PAUSED postcopy incoming migrations to try to continue */
176 QemuSemaphore postcopy_pause_sem_dst;
177 QemuSemaphore postcopy_pause_sem_fault;
179 * This semaphore is used to allow the ram fast load thread (only when
180 * postcopy preempt is enabled) fall into sleep when there's network
181 * interruption detected. When the recovery is done, the main load
182 * thread will kick the fast ram load thread using this semaphore.
184 QemuSemaphore postcopy_pause_sem_fast_load;
186 /* List of listening socket addresses */
187 SocketAddressList *socket_address_list;
189 /* A tree of pages that we requested to the source VM */
190 GTree *page_requested;
191 /* For debugging purpose only, but would be nice to keep */
192 int page_requested_count;
194 * The mutex helps to maintain the requested pages that we sent to the
195 * source, IOW, to guarantee coherent between the page_requests tree and
196 * the per-ramblock receivedmap. Note! This does not guarantee consistency
197 * of the real page copy procedures (using UFFDIO_[ZERO]COPY). E.g., even
198 * if one bit in receivedmap is cleared, UFFDIO_COPY could have happened
199 * for that page already. This is intended so that the mutex won't
200 * serialize and blocked by slow operations like UFFDIO_* ioctls. However
201 * this should be enough to make sure the page_requested tree always
202 * contains valid information.
204 QemuMutex page_request_mutex;
207 MigrationIncomingState *migration_incoming_get_current(void);
208 void migration_incoming_state_destroy(void);
209 void migration_incoming_transport_cleanup(MigrationIncomingState *mis);
211 * Functions to work with blocktime context
213 void fill_destination_postcopy_migration_info(MigrationInfo *info);
215 #define TYPE_MIGRATION "migration"
217 typedef struct MigrationClass MigrationClass;
218 DECLARE_OBJ_CHECKERS(MigrationState, MigrationClass,
219 MIGRATION_OBJ, TYPE_MIGRATION)
221 struct MigrationClass {
222 /*< private >*/
223 DeviceClass parent_class;
226 struct MigrationState {
227 /*< private >*/
228 DeviceState parent_obj;
230 /*< public >*/
231 QemuThread thread;
232 QEMUBH *vm_start_bh;
233 QEMUBH *cleanup_bh;
234 /* Protected by qemu_file_lock */
235 QEMUFile *to_dst_file;
236 /* Postcopy specific transfer channel */
237 QEMUFile *postcopy_qemufile_src;
239 * It is posted when the preempt channel is established. Note: this is
240 * used for both the start or recover of a postcopy migration. We'll
241 * post to this sem every time a new preempt channel is created in the
242 * main thread, and we keep post() and wait() in pair.
244 QemuSemaphore postcopy_qemufile_src_sem;
245 QIOChannelBuffer *bioc;
247 * Protects to_dst_file/from_dst_file pointers. We need to make sure we
248 * won't yield or hang during the critical section, since this lock will be
249 * used in OOB command handler.
251 QemuMutex qemu_file_lock;
254 * Used to allow urgent requests to override rate limiting.
256 QemuSemaphore rate_limit_sem;
258 /* pages already send at the beginning of current iteration */
259 uint64_t iteration_initial_pages;
261 /* pages transferred per second */
262 double pages_per_second;
264 /* bytes already send at the beginning of current iteration */
265 uint64_t iteration_initial_bytes;
266 /* time at the start of current iteration */
267 int64_t iteration_start_time;
269 * The final stage happens when the remaining data is smaller than
270 * this threshold; it's calculated from the requested downtime and
271 * measured bandwidth
273 int64_t threshold_size;
275 /* params from 'migrate-set-parameters' */
276 MigrationParameters parameters;
278 int state;
280 /* State related to return path */
281 struct {
282 /* Protected by qemu_file_lock */
283 QEMUFile *from_dst_file;
284 QemuThread rp_thread;
285 bool error;
287 * We can also check non-zero of rp_thread, but there's no "official"
288 * way to do this, so this bool makes it slightly more elegant.
289 * Checking from_dst_file for this is racy because from_dst_file will
290 * be cleared in the rp_thread!
292 bool rp_thread_created;
293 QemuSemaphore rp_sem;
295 * We post to this when we got one PONG from dest. So far it's an
296 * easy way to know the main channel has successfully established
297 * on dest QEMU.
299 QemuSemaphore rp_pong_acks;
300 } rp_state;
302 double mbps;
303 /* Timestamp when recent migration starts (ms) */
304 int64_t start_time;
305 /* Total time used by latest migration (ms) */
306 int64_t total_time;
307 /* Timestamp when VM is down (ms) to migrate the last stuff */
308 int64_t downtime_start;
309 int64_t downtime;
310 int64_t expected_downtime;
311 bool capabilities[MIGRATION_CAPABILITY__MAX];
312 int64_t setup_time;
314 * Whether guest was running when we enter the completion stage.
315 * If migration is interrupted by any reason, we need to continue
316 * running the guest on source.
318 bool vm_was_running;
320 /* Flag set once the migration has been asked to enter postcopy */
321 bool start_postcopy;
322 /* Flag set after postcopy has sent the device state */
323 bool postcopy_after_devices;
325 /* Flag set once the migration thread is running (and needs joining) */
326 bool migration_thread_running;
328 /* Flag set once the migration thread called bdrv_inactivate_all */
329 bool block_inactive;
331 /* Migration is waiting for guest to unplug device */
332 QemuSemaphore wait_unplug_sem;
334 /* Migration is paused due to pause-before-switchover */
335 QemuSemaphore pause_sem;
337 /* The semaphore is used to notify COLO thread that failover is finished */
338 QemuSemaphore colo_exit_sem;
340 /* The event is used to notify COLO thread to do checkpoint */
341 QemuEvent colo_checkpoint_event;
342 int64_t colo_checkpoint_time;
343 QEMUTimer *colo_delay_timer;
345 /* The first error that has occurred.
346 We used the mutex to be able to return the 1st error message */
347 Error *error;
348 /* mutex to protect errp */
349 QemuMutex error_mutex;
351 /* Do we have to clean up -b/-i from old migrate parameters */
352 /* This feature is deprecated and will be removed */
353 bool must_remove_block_options;
356 * Global switch on whether we need to store the global state
357 * during migration.
359 bool store_global_state;
361 /* Whether we send QEMU_VM_CONFIGURATION during migration */
362 bool send_configuration;
363 /* Whether we send section footer during migration */
364 bool send_section_footer;
366 /* Needed by postcopy-pause state */
367 QemuSemaphore postcopy_pause_sem;
368 QemuSemaphore postcopy_pause_rp_sem;
370 * Whether we abort the migration if decompression errors are
371 * detected at the destination. It is left at false for qemu
372 * older than 3.0, since only newer qemu sends streams that
373 * do not trigger spurious decompression errors.
375 bool decompress_error_check;
377 * This variable only affects behavior when postcopy preempt mode is
378 * enabled.
380 * When set:
382 * - postcopy preempt src QEMU instance will generate an EOS message at
383 * the end of migration to shut the preempt channel on dest side.
385 * - postcopy preempt channel will be created at the setup phase on src
386 QEMU.
388 * When clear:
390 * - postcopy preempt src QEMU instance will _not_ generate an EOS
391 * message at the end of migration, the dest qemu will shutdown the
392 * channel itself.
394 * - postcopy preempt channel will be created at the switching phase
395 * from precopy -> postcopy (to avoid race condtion of misordered
396 * creation of channels).
398 * NOTE: See message-id <ZBoShWArKDPpX/D7@work-vm> on qemu-devel
399 * mailing list for more information on the possible race. Everyone
400 * should probably just keep this value untouched after set by the
401 * machine type (or the default).
403 bool preempt_pre_7_2;
406 * flush every channel after each section sent.
408 * This assures that we can't mix pages from one iteration through
409 * ram pages with pages for the following iteration. We really
410 * only need to do this flush after we have go through all the
411 * dirty pages. For historical reasons, we do that after each
412 * section. This is suboptimal (we flush too many times).
413 * Default value is false. (since 8.1)
415 bool multifd_flush_after_each_section;
417 * This decides the size of guest memory chunk that will be used
418 * to track dirty bitmap clearing. The size of memory chunk will
419 * be GUEST_PAGE_SIZE << N. Say, N=0 means we will clear dirty
420 * bitmap for each page to send (1<<0=1); N=10 means we will clear
421 * dirty bitmap only once for 1<<10=1K continuous guest pages
422 * (which is in 4M chunk).
424 uint8_t clear_bitmap_shift;
427 * This save hostname when out-going migration starts
429 char *hostname;
431 /* QEMU_VM_VMDESCRIPTION content filled for all non-iterable devices. */
432 JSONWriter *vmdesc;
435 void migrate_set_state(int *state, int old_state, int new_state);
437 void migration_fd_process_incoming(QEMUFile *f, Error **errp);
438 void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp);
439 void migration_incoming_process(void);
441 bool migration_has_all_channels(void);
443 uint64_t migrate_max_downtime(void);
445 void migrate_set_error(MigrationState *s, const Error *error);
446 void migrate_fd_error(MigrationState *s, const Error *error);
448 void migrate_fd_connect(MigrationState *s, Error *error_in);
450 bool migration_is_setup_or_active(int state);
451 bool migration_is_running(int state);
453 void migrate_init(MigrationState *s);
454 bool migration_is_blocked(Error **errp);
455 /* True if outgoing migration has entered postcopy phase */
456 bool migration_in_postcopy(void);
457 MigrationState *migrate_get_current(void);
459 uint64_t ram_get_total_transferred_pages(void);
461 /* Sending on the return path - generic and then for each message type */
462 void migrate_send_rp_shut(MigrationIncomingState *mis,
463 uint32_t value);
464 void migrate_send_rp_pong(MigrationIncomingState *mis,
465 uint32_t value);
466 int migrate_send_rp_req_pages(MigrationIncomingState *mis, RAMBlock *rb,
467 ram_addr_t start, uint64_t haddr);
468 int migrate_send_rp_message_req_pages(MigrationIncomingState *mis,
469 RAMBlock *rb, ram_addr_t start);
470 void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
471 char *block_name);
472 void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value);
474 void dirty_bitmap_mig_before_vm_start(void);
475 void dirty_bitmap_mig_cancel_outgoing(void);
476 void dirty_bitmap_mig_cancel_incoming(void);
477 bool check_dirty_bitmap_mig_alias_map(const BitmapMigrationNodeAliasList *bbm,
478 Error **errp);
480 void migrate_add_address(SocketAddress *address);
482 int foreach_not_ignored_block(RAMBlockIterFunc func, void *opaque);
484 #define qemu_ram_foreach_block \
485 #warning "Use foreach_not_ignored_block in migration code"
487 void migration_make_urgent_request(void);
488 void migration_consume_urgent_request(void);
489 bool migration_rate_limit(void);
490 void migration_cancel(const Error *error);
492 void populate_vfio_info(MigrationInfo *info);
493 void postcopy_temp_page_reset(PostcopyTmpPage *tmp_page);
495 #endif