xref: /qemu/migration/migration.h (revision 93e0932b)
1 /*
2  * QEMU live migration
3  *
4  * Copyright IBM, Corp. 2008
5  *
6  * Authors:
7  *  Anthony Liguori   <aliguori@us.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  *
12  */
13 
14 #ifndef QEMU_MIGRATION_H
15 #define QEMU_MIGRATION_H
16 
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"
28 
29 struct PostcopyBlocktimeContext;
30 
31 #define  MIGRATION_RESUME_ACK_VALUE  (1)
32 
33 /*
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.
37  */
38 #define CLEAR_BITMAP_SHIFT_MIN             6
39 /*
40  * 1<<18=256K pages -> 1G chunk when page size is 4K.  This is the
41  * default value to use if no one specified.
42  */
43 #define CLEAR_BITMAP_SHIFT_DEFAULT        18
44 /*
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.
47  */
48 #define CLEAR_BITMAP_SHIFT_MAX            31
49 
50 /* This is an abstraction of a "temp huge page" for postcopy's purpose */
51 typedef struct {
52     /*
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.
55      */
56     void *tmp_huge_page;
57     /*
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.
60      */
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;
67 
68 /* State for the incoming migration */
69 struct MigrationIncomingState {
70     QEMUFile *from_src_file;
71     /* Previously received RAM's RAMBlock pointer */
72     RAMBlock *last_recv_block[RAM_CHANNEL_MAX];
73     /* A hook to allow cleanup at the end of incoming migration */
74     void *transport_data;
75     void (*transport_cleanup)(void *data);
76     /*
77      * Used to sync thread creations.  Note that we can't create threads in
78      * parallel with this sem.
79      */
80     QemuSemaphore  thread_sync_sem;
81     /*
82      * Free at the start of the main state load, set as the main thread finishes
83      * loading state.
84      */
85     QemuEvent main_thread_load_event;
86 
87     /* For network announces */
88     AnnounceTimer  announce_timer;
89 
90     size_t         largest_page_size;
91     bool           have_fault_thread;
92     QemuThread     fault_thread;
93     /* Set this when we want the fault thread to quit */
94     bool           fault_thread_quit;
95 
96     bool           have_listen_thread;
97     QemuThread     listen_thread;
98 
99     /* For the kernel to send us notifications */
100     int       userfault_fd;
101     /* To notify the fault_thread to wake, e.g., when need to quit */
102     int       userfault_event_fd;
103     QEMUFile *to_src_file;
104     QemuMutex rp_mutex;    /* We send replies from multiple threads */
105     /* RAMBlock of last request sent to source */
106     RAMBlock *last_rb;
107     /*
108      * Number of postcopy channels including the default precopy channel, so
109      * vanilla postcopy will only contain one channel which contain both
110      * precopy and postcopy streams.
111      *
112      * This is calculated when the src requests to enable postcopy but before
113      * it starts.  Its value can depend on e.g. whether postcopy preemption is
114      * enabled.
115      */
116     unsigned int postcopy_channels;
117     /* QEMUFile for postcopy only; it'll be handled by a separate thread */
118     QEMUFile *postcopy_qemufile_dst;
119     /* Postcopy priority thread is used to receive postcopy requested pages */
120     QemuThread postcopy_prio_thread;
121     bool postcopy_prio_thread_created;
122     /*
123      * Used to sync between the ram load main thread and the fast ram load
124      * thread.  It protects postcopy_qemufile_dst, which is the postcopy
125      * fast channel.
126      *
127      * The ram fast load thread will take it mostly for the whole lifecycle
128      * because it needs to continuously read data from the channel, and
129      * it'll only release this mutex if postcopy is interrupted, so that
130      * the ram load main thread will take this mutex over and properly
131      * release the broken channel.
132      */
133     QemuMutex postcopy_prio_thread_mutex;
134     /*
135      * An array of temp host huge pages to be used, one for each postcopy
136      * channel.
137      */
138     PostcopyTmpPage *postcopy_tmp_pages;
139     /* This is shared for all postcopy channels */
140     void     *postcopy_tmp_zero_page;
141     /* PostCopyFD's for external userfaultfds & handlers of shared memory */
142     GArray   *postcopy_remote_fds;
143 
144     QEMUBH *bh;
145 
146     int state;
147 
148     bool have_colo_incoming_thread;
149     QemuThread colo_incoming_thread;
150     /* The coroutine we should enter (back) after failover */
151     Coroutine *migration_incoming_co;
152     QemuSemaphore colo_incoming_sem;
153 
154     /*
155      * PostcopyBlocktimeContext to keep information for postcopy
156      * live migration, to calculate vCPU block time
157      * */
158     struct PostcopyBlocktimeContext *blocktime_ctx;
159 
160     /* notify PAUSED postcopy incoming migrations to try to continue */
161     QemuSemaphore postcopy_pause_sem_dst;
162     QemuSemaphore postcopy_pause_sem_fault;
163     /*
164      * This semaphore is used to allow the ram fast load thread (only when
165      * postcopy preempt is enabled) fall into sleep when there's network
166      * interruption detected.  When the recovery is done, the main load
167      * thread will kick the fast ram load thread using this semaphore.
168      */
169     QemuSemaphore postcopy_pause_sem_fast_load;
170 
171     /* List of listening socket addresses  */
172     SocketAddressList *socket_address_list;
173 
174     /* A tree of pages that we requested to the source VM */
175     GTree *page_requested;
176     /* For debugging purpose only, but would be nice to keep */
177     int page_requested_count;
178     /*
179      * The mutex helps to maintain the requested pages that we sent to the
180      * source, IOW, to guarantee coherent between the page_requests tree and
181      * the per-ramblock receivedmap.  Note! This does not guarantee consistency
182      * of the real page copy procedures (using UFFDIO_[ZERO]COPY).  E.g., even
183      * if one bit in receivedmap is cleared, UFFDIO_COPY could have happened
184      * for that page already.  This is intended so that the mutex won't
185      * serialize and blocked by slow operations like UFFDIO_* ioctls.  However
186      * this should be enough to make sure the page_requested tree always
187      * contains valid information.
188      */
189     QemuMutex page_request_mutex;
190 };
191 
192 MigrationIncomingState *migration_incoming_get_current(void);
193 void migration_incoming_state_destroy(void);
194 void migration_incoming_transport_cleanup(MigrationIncomingState *mis);
195 /*
196  * Functions to work with blocktime context
197  */
198 void fill_destination_postcopy_migration_info(MigrationInfo *info);
199 
200 #define TYPE_MIGRATION "migration"
201 
202 typedef struct MigrationClass MigrationClass;
203 DECLARE_OBJ_CHECKERS(MigrationState, MigrationClass,
204                      MIGRATION_OBJ, TYPE_MIGRATION)
205 
206 struct MigrationClass {
207     /*< private >*/
208     DeviceClass parent_class;
209 };
210 
211 struct MigrationState {
212     /*< private >*/
213     DeviceState parent_obj;
214 
215     /*< public >*/
216     QemuThread thread;
217     QEMUBH *vm_start_bh;
218     QEMUBH *cleanup_bh;
219     /* Protected by qemu_file_lock */
220     QEMUFile *to_dst_file;
221     /* Postcopy specific transfer channel */
222     QEMUFile *postcopy_qemufile_src;
223     /*
224      * It is posted when the preempt channel is established.  Note: this is
225      * used for both the start or recover of a postcopy migration.  We'll
226      * post to this sem every time a new preempt channel is created in the
227      * main thread, and we keep post() and wait() in pair.
228      */
229     QemuSemaphore postcopy_qemufile_src_sem;
230     QIOChannelBuffer *bioc;
231     /*
232      * Protects to_dst_file/from_dst_file pointers.  We need to make sure we
233      * won't yield or hang during the critical section, since this lock will be
234      * used in OOB command handler.
235      */
236     QemuMutex qemu_file_lock;
237 
238     /*
239      * Used to allow urgent requests to override rate limiting.
240      */
241     QemuSemaphore rate_limit_sem;
242 
243     /* pages already send at the beginning of current iteration */
244     uint64_t iteration_initial_pages;
245 
246     /* pages transferred per second */
247     double pages_per_second;
248 
249     /* bytes already send at the beginning of current iteration */
250     uint64_t iteration_initial_bytes;
251     /* time at the start of current iteration */
252     int64_t iteration_start_time;
253     /*
254      * The final stage happens when the remaining data is smaller than
255      * this threshold; it's calculated from the requested downtime and
256      * measured bandwidth
257      */
258     int64_t threshold_size;
259 
260     /* params from 'migrate-set-parameters' */
261     MigrationParameters parameters;
262 
263     int state;
264 
265     /* State related to return path */
266     struct {
267         /* Protected by qemu_file_lock */
268         QEMUFile     *from_dst_file;
269         QemuThread    rp_thread;
270         bool          error;
271         /*
272          * We can also check non-zero of rp_thread, but there's no "official"
273          * way to do this, so this bool makes it slightly more elegant.
274          * Checking from_dst_file for this is racy because from_dst_file will
275          * be cleared in the rp_thread!
276          */
277         bool          rp_thread_created;
278         QemuSemaphore rp_sem;
279     } rp_state;
280 
281     double mbps;
282     /* Timestamp when recent migration starts (ms) */
283     int64_t start_time;
284     /* Total time used by latest migration (ms) */
285     int64_t total_time;
286     /* Timestamp when VM is down (ms) to migrate the last stuff */
287     int64_t downtime_start;
288     int64_t downtime;
289     int64_t expected_downtime;
290     bool enabled_capabilities[MIGRATION_CAPABILITY__MAX];
291     int64_t setup_time;
292     /*
293      * Whether guest was running when we enter the completion stage.
294      * If migration is interrupted by any reason, we need to continue
295      * running the guest on source.
296      */
297     bool vm_was_running;
298 
299     /* Flag set once the migration has been asked to enter postcopy */
300     bool start_postcopy;
301     /* Flag set after postcopy has sent the device state */
302     bool postcopy_after_devices;
303 
304     /* Flag set once the migration thread is running (and needs joining) */
305     bool migration_thread_running;
306 
307     /* Flag set once the migration thread called bdrv_inactivate_all */
308     bool block_inactive;
309 
310     /* Migration is waiting for guest to unplug device */
311     QemuSemaphore wait_unplug_sem;
312 
313     /* Migration is paused due to pause-before-switchover */
314     QemuSemaphore pause_sem;
315 
316     /* The semaphore is used to notify COLO thread that failover is finished */
317     QemuSemaphore colo_exit_sem;
318 
319     /* The event is used to notify COLO thread to do checkpoint */
320     QemuEvent colo_checkpoint_event;
321     int64_t colo_checkpoint_time;
322     QEMUTimer *colo_delay_timer;
323 
324     /* The first error that has occurred.
325        We used the mutex to be able to return the 1st error message */
326     Error *error;
327     /* mutex to protect errp */
328     QemuMutex error_mutex;
329 
330     /* Do we have to clean up -b/-i from old migrate parameters */
331     /* This feature is deprecated and will be removed */
332     bool must_remove_block_options;
333 
334     /*
335      * Global switch on whether we need to store the global state
336      * during migration.
337      */
338     bool store_global_state;
339 
340     /* Whether we send QEMU_VM_CONFIGURATION during migration */
341     bool send_configuration;
342     /* Whether we send section footer during migration */
343     bool send_section_footer;
344 
345     /* Needed by postcopy-pause state */
346     QemuSemaphore postcopy_pause_sem;
347     QemuSemaphore postcopy_pause_rp_sem;
348     /*
349      * Whether we abort the migration if decompression errors are
350      * detected at the destination. It is left at false for qemu
351      * older than 3.0, since only newer qemu sends streams that
352      * do not trigger spurious decompression errors.
353      */
354     bool decompress_error_check;
355 
356     /*
357      * This decides the size of guest memory chunk that will be used
358      * to track dirty bitmap clearing.  The size of memory chunk will
359      * be GUEST_PAGE_SIZE << N.  Say, N=0 means we will clear dirty
360      * bitmap for each page to send (1<<0=1); N=10 means we will clear
361      * dirty bitmap only once for 1<<10=1K continuous guest pages
362      * (which is in 4M chunk).
363      */
364     uint8_t clear_bitmap_shift;
365 
366     /*
367      * This save hostname when out-going migration starts
368      */
369     char *hostname;
370 
371     /* QEMU_VM_VMDESCRIPTION content filled for all non-iterable devices. */
372     JSONWriter *vmdesc;
373 };
374 
375 void migrate_set_state(int *state, int old_state, int new_state);
376 
377 void migration_fd_process_incoming(QEMUFile *f, Error **errp);
378 void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp);
379 void migration_incoming_process(void);
380 
381 bool  migration_has_all_channels(void);
382 
383 uint64_t migrate_max_downtime(void);
384 
385 void migrate_set_error(MigrationState *s, const Error *error);
386 void migrate_fd_error(MigrationState *s, const Error *error);
387 
388 void migrate_fd_connect(MigrationState *s, Error *error_in);
389 
390 bool migration_is_setup_or_active(int state);
391 bool migration_is_running(int state);
392 
393 void migrate_init(MigrationState *s);
394 bool migration_is_blocked(Error **errp);
395 /* True if outgoing migration has entered postcopy phase */
396 bool migration_in_postcopy(void);
397 MigrationState *migrate_get_current(void);
398 
399 bool migrate_postcopy(void);
400 
401 bool migrate_release_ram(void);
402 bool migrate_postcopy_ram(void);
403 bool migrate_zero_blocks(void);
404 bool migrate_dirty_bitmaps(void);
405 bool migrate_ignore_shared(void);
406 bool migrate_validate_uuid(void);
407 
408 bool migrate_auto_converge(void);
409 bool migrate_use_multifd(void);
410 bool migrate_pause_before_switchover(void);
411 int migrate_multifd_channels(void);
412 MultiFDCompression migrate_multifd_compression(void);
413 int migrate_multifd_zlib_level(void);
414 int migrate_multifd_zstd_level(void);
415 
416 #ifdef CONFIG_LINUX
417 bool migrate_use_zero_copy_send(void);
418 #else
419 #define migrate_use_zero_copy_send() (false)
420 #endif
421 int migrate_use_tls(void);
422 int migrate_use_xbzrle(void);
423 uint64_t migrate_xbzrle_cache_size(void);
424 bool migrate_colo_enabled(void);
425 
426 bool migrate_use_block(void);
427 bool migrate_use_block_incremental(void);
428 int migrate_max_cpu_throttle(void);
429 bool migrate_use_return_path(void);
430 
431 uint64_t ram_get_total_transferred_pages(void);
432 
433 bool migrate_use_compression(void);
434 int migrate_compress_level(void);
435 int migrate_compress_threads(void);
436 int migrate_compress_wait_thread(void);
437 int migrate_decompress_threads(void);
438 bool migrate_use_events(void);
439 bool migrate_postcopy_blocktime(void);
440 bool migrate_background_snapshot(void);
441 bool migrate_postcopy_preempt(void);
442 
443 /* Sending on the return path - generic and then for each message type */
444 void migrate_send_rp_shut(MigrationIncomingState *mis,
445                           uint32_t value);
446 void migrate_send_rp_pong(MigrationIncomingState *mis,
447                           uint32_t value);
448 int migrate_send_rp_req_pages(MigrationIncomingState *mis, RAMBlock *rb,
449                               ram_addr_t start, uint64_t haddr);
450 int migrate_send_rp_message_req_pages(MigrationIncomingState *mis,
451                                       RAMBlock *rb, ram_addr_t start);
452 void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
453                                  char *block_name);
454 void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value);
455 
456 void dirty_bitmap_mig_before_vm_start(void);
457 void dirty_bitmap_mig_cancel_outgoing(void);
458 void dirty_bitmap_mig_cancel_incoming(void);
459 bool check_dirty_bitmap_mig_alias_map(const BitmapMigrationNodeAliasList *bbm,
460                                       Error **errp);
461 
462 void migrate_add_address(SocketAddress *address);
463 
464 int foreach_not_ignored_block(RAMBlockIterFunc func, void *opaque);
465 
466 #define qemu_ram_foreach_block \
467   #warning "Use foreach_not_ignored_block in migration code"
468 
469 void migration_make_urgent_request(void);
470 void migration_consume_urgent_request(void);
471 bool migration_rate_limit(void);
472 void migration_cancel(const Error *error);
473 
474 void populate_vfio_info(MigrationInfo *info);
475 void postcopy_temp_page_reset(PostcopyTmpPage *tmp_page);
476 
477 bool migrate_multi_channels_is_allowed(void);
478 void migrate_protocol_allow_multi_channels(bool allow);
479 
480 #endif
481