xref: /qemu/migration/migration.h (revision 513823e7521a09ed7ad1e32e6454bac3b2cbf52d)
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 "qobject/json-writer.h"
21 #include "qemu/thread.h"
22 #include "qemu/coroutine.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 #include "system/runstate.h"
29 #include "migration/misc.h"
30 
31 #define  MIGRATION_THREAD_SNAPSHOT          "mig/snapshot"
32 #define  MIGRATION_THREAD_DIRTY_RATE        "mig/dirtyrate"
33 
34 #define  MIGRATION_THREAD_SRC_MAIN          "mig/src/main"
35 #define  MIGRATION_THREAD_SRC_MULTIFD       "mig/src/send_%d"
36 #define  MIGRATION_THREAD_SRC_RETURN        "mig/src/return"
37 #define  MIGRATION_THREAD_SRC_TLS           "mig/src/tls"
38 
39 #define  MIGRATION_THREAD_DST_COLO          "mig/dst/colo"
40 #define  MIGRATION_THREAD_DST_MULTIFD       "mig/dst/recv_%d"
41 #define  MIGRATION_THREAD_DST_FAULT         "mig/dst/fault"
42 #define  MIGRATION_THREAD_DST_LISTEN        "mig/dst/listen"
43 #define  MIGRATION_THREAD_DST_PREEMPT       "mig/dst/preempt"
44 
45 struct PostcopyBlocktimeContext;
46 
47 #define  MIGRATION_RESUME_ACK_VALUE  (1)
48 
49 /*
50  * 1<<6=64 pages -> 256K chunk when page size is 4K.  This gives us
51  * the benefit that all the chunks are 64 pages aligned then the
52  * bitmaps are always aligned to LONG.
53  */
54 #define CLEAR_BITMAP_SHIFT_MIN             6
55 /*
56  * 1<<18=256K pages -> 1G chunk when page size is 4K.  This is the
57  * default value to use if no one specified.
58  */
59 #define CLEAR_BITMAP_SHIFT_DEFAULT        18
60 /*
61  * 1<<31=2G pages -> 8T chunk when page size is 4K.  This should be
62  * big enough and make sure we won't overflow easily.
63  */
64 #define CLEAR_BITMAP_SHIFT_MAX            31
65 
66 /* This is an abstraction of a "temp huge page" for postcopy's purpose */
67 typedef struct {
68     /*
69      * This points to a temporary huge page as a buffer for UFFDIO_COPY.  It's
70      * mmap()ed and needs to be freed when cleanup.
71      */
72     void *tmp_huge_page;
73     /*
74      * This points to the host page we're going to install for this temp page.
75      * It tells us after we've received the whole page, where we should put it.
76      */
77     void *host_addr;
78     /* Number of small pages copied (in size of TARGET_PAGE_SIZE) */
79     unsigned int target_pages;
80     /* Whether this page contains all zeros */
81     bool all_zero;
82 } PostcopyTmpPage;
83 
84 typedef enum {
85     PREEMPT_THREAD_NONE = 0,
86     PREEMPT_THREAD_CREATED,
87     PREEMPT_THREAD_QUIT,
88 } PreemptThreadStatus;
89 
90 /* State for the incoming migration */
91 struct MigrationIncomingState {
92     QEMUFile *from_src_file;
93     /* Previously received RAM's RAMBlock pointer */
94     RAMBlock *last_recv_block[RAM_CHANNEL_MAX];
95     /* A hook to allow cleanup at the end of incoming migration */
96     void *transport_data;
97     void (*transport_cleanup)(void *data);
98     /*
99      * Used to sync thread creations.  Note that we can't create threads in
100      * parallel with this sem.
101      */
102     QemuSemaphore  thread_sync_sem;
103     /*
104      * Free at the start of the main state load, set as the main thread finishes
105      * loading state.
106      */
107     QemuEvent main_thread_load_event;
108 
109     /* For network announces */
110     AnnounceTimer  announce_timer;
111 
112     size_t         largest_page_size;
113     bool           have_fault_thread;
114     QemuThread     fault_thread;
115     /* Set this when we want the fault thread to quit */
116     bool           fault_thread_quit;
117 
118     bool           have_listen_thread;
119     QemuThread     listen_thread;
120 
121     /* For the kernel to send us notifications */
122     int       userfault_fd;
123     /* To notify the fault_thread to wake, e.g., when need to quit */
124     int       userfault_event_fd;
125     QEMUFile *to_src_file;
126     QemuMutex rp_mutex;    /* We send replies from multiple threads */
127     /* RAMBlock of last request sent to source */
128     RAMBlock *last_rb;
129     /*
130      * Number of postcopy channels including the default precopy channel, so
131      * vanilla postcopy will only contain one channel which contain both
132      * precopy and postcopy streams.
133      *
134      * This is calculated when the src requests to enable postcopy but before
135      * it starts.  Its value can depend on e.g. whether postcopy preemption is
136      * enabled.
137      */
138     unsigned int postcopy_channels;
139     /* QEMUFile for postcopy only; it'll be handled by a separate thread */
140     QEMUFile *postcopy_qemufile_dst;
141     /*
142      * When postcopy_qemufile_dst is properly setup, this sem is posted.
143      * One can wait on this semaphore to wait until the preempt channel is
144      * properly setup.
145      */
146     QemuSemaphore postcopy_qemufile_dst_done;
147     /* Postcopy priority thread is used to receive postcopy requested pages */
148     QemuThread postcopy_prio_thread;
149     /*
150      * Always set by the main vm load thread only, but can be read by the
151      * postcopy preempt thread.  "volatile" makes sure all reads will be
152      * up-to-date across cores.
153      */
154     volatile PreemptThreadStatus preempt_thread_status;
155     /*
156      * Used to sync between the ram load main thread and the fast ram load
157      * thread.  It protects postcopy_qemufile_dst, which is the postcopy
158      * fast channel.
159      *
160      * The ram fast load thread will take it mostly for the whole lifecycle
161      * because it needs to continuously read data from the channel, and
162      * it'll only release this mutex if postcopy is interrupted, so that
163      * the ram load main thread will take this mutex over and properly
164      * release the broken channel.
165      */
166     QemuMutex postcopy_prio_thread_mutex;
167     /*
168      * An array of temp host huge pages to be used, one for each postcopy
169      * channel.
170      */
171     PostcopyTmpPage *postcopy_tmp_pages;
172     /* This is shared for all postcopy channels */
173     void     *postcopy_tmp_zero_page;
174     /* PostCopyFD's for external userfaultfds & handlers of shared memory */
175     GArray   *postcopy_remote_fds;
176 
177     MigrationStatus state;
178 
179     /*
180      * The incoming migration coroutine, non-NULL during qemu_loadvm_state().
181      * Used to wake the migration incoming coroutine from rdma code. How much is
182      * it safe - it's a question.
183      */
184     Coroutine *loadvm_co;
185 
186     /* The coroutine we should enter (back) after failover */
187     Coroutine *colo_incoming_co;
188     QemuSemaphore colo_incoming_sem;
189 
190     /*
191      * PostcopyBlocktimeContext to keep information for postcopy
192      * live migration, to calculate vCPU block time
193      * */
194     struct PostcopyBlocktimeContext *blocktime_ctx;
195 
196     /* notify PAUSED postcopy incoming migrations to try to continue */
197     QemuSemaphore postcopy_pause_sem_dst;
198     QemuSemaphore postcopy_pause_sem_fault;
199     /*
200      * This semaphore is used to allow the ram fast load thread (only when
201      * postcopy preempt is enabled) fall into sleep when there's network
202      * interruption detected.  When the recovery is done, the main load
203      * thread will kick the fast ram load thread using this semaphore.
204      */
205     QemuSemaphore postcopy_pause_sem_fast_load;
206 
207     /* List of listening socket addresses  */
208     SocketAddressList *socket_address_list;
209 
210     /* A tree of pages that we requested to the source VM */
211     GTree *page_requested;
212     /*
213      * For postcopy only, count the number of requested page faults that
214      * still haven't been resolved.
215      */
216     int page_requested_count;
217     /*
218      * The mutex helps to maintain the requested pages that we sent to the
219      * source, IOW, to guarantee coherent between the page_requests tree and
220      * the per-ramblock receivedmap.  Note! This does not guarantee consistency
221      * of the real page copy procedures (using UFFDIO_[ZERO]COPY).  E.g., even
222      * if one bit in receivedmap is cleared, UFFDIO_COPY could have happened
223      * for that page already.  This is intended so that the mutex won't
224      * serialize and blocked by slow operations like UFFDIO_* ioctls.  However
225      * this should be enough to make sure the page_requested tree always
226      * contains valid information.
227      */
228     QemuMutex page_request_mutex;
229     /*
230      * If postcopy preempt is enabled, there is a chance that the main
231      * thread finished loading its data before the preempt channel has
232      * finished loading the urgent pages.  If that happens, the two threads
233      * will use this condvar to synchronize, so the main thread will always
234      * wait until all pages received.
235      */
236     QemuCond page_request_cond;
237 
238     /*
239      * Number of devices that have yet to approve switchover. When this reaches
240      * zero an ACK that it's OK to do switchover is sent to the source. No lock
241      * is needed as this field is updated serially.
242      */
243     unsigned int switchover_ack_pending_num;
244 
245     /* Do exit on incoming migration failure */
246     bool exit_on_error;
247 };
248 
249 MigrationIncomingState *migration_incoming_get_current(void);
250 void migration_incoming_state_destroy(void);
251 void migration_incoming_transport_cleanup(MigrationIncomingState *mis);
252 /*
253  * Functions to work with blocktime context
254  */
255 void fill_destination_postcopy_migration_info(MigrationInfo *info);
256 
257 #define TYPE_MIGRATION "migration"
258 
259 typedef struct MigrationClass MigrationClass;
260 DECLARE_OBJ_CHECKERS(MigrationState, MigrationClass,
261                      MIGRATION_OBJ, TYPE_MIGRATION)
262 
263 struct MigrationClass {
264     /*< private >*/
265     DeviceClass parent_class;
266 };
267 
268 struct MigrationState {
269     /*< private >*/
270     DeviceState parent_obj;
271 
272     /*< public >*/
273     QemuThread thread;
274     /* Protected by qemu_file_lock */
275     QEMUFile *to_dst_file;
276     /* Postcopy specific transfer channel */
277     QEMUFile *postcopy_qemufile_src;
278     /*
279      * It is posted when the preempt channel is established.  Note: this is
280      * used for both the start or recover of a postcopy migration.  We'll
281      * post to this sem every time a new preempt channel is created in the
282      * main thread, and we keep post() and wait() in pair.
283      */
284     QemuSemaphore postcopy_qemufile_src_sem;
285     QIOChannelBuffer *bioc;
286     /*
287      * Protects to_dst_file/from_dst_file pointers.  We need to make sure we
288      * won't yield or hang during the critical section, since this lock will be
289      * used in OOB command handler.
290      */
291     QemuMutex qemu_file_lock;
292 
293     /*
294      * Used to allow urgent requests to override rate limiting.
295      */
296     QemuSemaphore rate_limit_sem;
297 
298     /* pages already send at the beginning of current iteration */
299     uint64_t iteration_initial_pages;
300 
301     /* pages transferred per second */
302     double pages_per_second;
303 
304     /* bytes already send at the beginning of current iteration */
305     uint64_t iteration_initial_bytes;
306     /* time at the start of current iteration */
307     int64_t iteration_start_time;
308     /*
309      * The final stage happens when the remaining data is smaller than
310      * this threshold; it's calculated from the requested downtime and
311      * measured bandwidth, or avail-switchover-bandwidth if specified.
312      */
313     uint64_t threshold_size;
314 
315     /* params from 'migrate-set-parameters' */
316     MigrationParameters parameters;
317 
318     MigrationStatus state;
319 
320     /* State related to return path */
321     struct {
322         /* Protected by qemu_file_lock */
323         QEMUFile     *from_dst_file;
324         QemuThread    rp_thread;
325         /*
326          * We can also check non-zero of rp_thread, but there's no "official"
327          * way to do this, so this bool makes it slightly more elegant.
328          * Checking from_dst_file for this is racy because from_dst_file will
329          * be cleared in the rp_thread!
330          */
331         bool          rp_thread_created;
332         /*
333          * Used to synchronize between migration main thread and return
334          * path thread.  The migration thread can wait() on this sem, while
335          * other threads (e.g., return path thread) can kick it using a
336          * post().
337          */
338         QemuSemaphore rp_sem;
339         /*
340          * We post to this when we got one PONG from dest. So far it's an
341          * easy way to know the main channel has successfully established
342          * on dest QEMU.
343          */
344         QemuSemaphore rp_pong_acks;
345     } rp_state;
346 
347     double mbps;
348     /* Timestamp when recent migration starts (ms) */
349     int64_t start_time;
350     /* Total time used by latest migration (ms) */
351     int64_t total_time;
352     /* Timestamp when VM is down (ms) to migrate the last stuff */
353     int64_t downtime_start;
354     int64_t downtime;
355     int64_t expected_downtime;
356     bool capabilities[MIGRATION_CAPABILITY__MAX];
357     int64_t setup_time;
358 
359     /*
360      * State before stopping the vm by vm_stop_force_state().
361      * If migration is interrupted by any reason, we need to continue
362      * running the guest on source if it was running or restore its stopped
363      * state.
364      */
365     RunState vm_old_state;
366 
367     /* Flag set once the migration has been asked to enter postcopy */
368     bool start_postcopy;
369 
370     /* Flag set once the migration thread is running (and needs joining) */
371     bool migration_thread_running;
372 
373     /* Migration is waiting for guest to unplug device */
374     QemuSemaphore wait_unplug_sem;
375 
376     /* Migration is paused due to pause-before-switchover */
377     QemuSemaphore pause_sem;
378 
379     /* The semaphore is used to notify COLO thread that failover is finished */
380     QemuSemaphore colo_exit_sem;
381 
382     /* The event is used to notify COLO thread to do checkpoint */
383     QemuEvent colo_checkpoint_event;
384     int64_t colo_checkpoint_time;
385     QEMUTimer *colo_delay_timer;
386 
387     /* The first error that has occurred.
388        We used the mutex to be able to return the 1st error message */
389     Error *error;
390     /* mutex to protect errp */
391     QemuMutex error_mutex;
392 
393     /*
394      * Global switch on whether we need to store the global state
395      * during migration.
396      */
397     bool store_global_state;
398 
399     /* Whether we send QEMU_VM_CONFIGURATION during migration */
400     bool send_configuration;
401     /* Whether we send section footer during migration */
402     bool send_section_footer;
403 
404     /* Needed by postcopy-pause state */
405     QemuSemaphore postcopy_pause_sem;
406     /*
407      * This variable only affects behavior when postcopy preempt mode is
408      * enabled.
409      *
410      * When set:
411      *
412      * - postcopy preempt src QEMU instance will generate an EOS message at
413      *   the end of migration to shut the preempt channel on dest side.
414      *
415      * - postcopy preempt channel will be created at the setup phase on src
416          QEMU.
417      *
418      * When clear:
419      *
420      * - postcopy preempt src QEMU instance will _not_ generate an EOS
421      *   message at the end of migration, the dest qemu will shutdown the
422      *   channel itself.
423      *
424      * - postcopy preempt channel will be created at the switching phase
425      *   from precopy -> postcopy (to avoid race condition of misordered
426      *   creation of channels).
427      *
428      * NOTE: See message-id <ZBoShWArKDPpX/D7@work-vm> on qemu-devel
429      * mailing list for more information on the possible race.  Everyone
430      * should probably just keep this value untouched after set by the
431      * machine type (or the default).
432      */
433     bool preempt_pre_7_2;
434 
435     /*
436      * flush every channel after each section sent.
437      *
438      * This assures that we can't mix pages from one iteration through
439      * ram pages with pages for the following iteration.  We really
440      * only need to do this flush after we have go through all the
441      * dirty pages.  For historical reasons, we do that after each
442      * section.  This is suboptimal (we flush too many times).
443      * Default value is false. (since 8.1)
444      */
445     bool multifd_flush_after_each_section;
446 
447     /*
448      * This variable only makes sense when set on the machine that is
449      * the destination of a multifd migration with TLS enabled. It
450      * affects the behavior of the last send->recv iteration with
451      * regards to termination of the TLS session.
452      *
453      * When set:
454      *
455      * - the destination QEMU instance can expect to never get a
456      *   GNUTLS_E_PREMATURE_TERMINATION error. Manifested as the error
457      *   message: "The TLS connection was non-properly terminated".
458      *
459      * When clear:
460      *
461      * - the destination QEMU instance can expect to see a
462      *   GNUTLS_E_PREMATURE_TERMINATION error in any multifd channel
463      *   whenever the last recv() call of that channel happens after
464      *   the source QEMU instance has already issued shutdown() on the
465      *   channel.
466      *
467      *   Commit 637280aeb2 (since 9.1) introduced a side effect that
468      *   causes the destination instance to not be affected by the
469      *   premature termination, while commit 1d457daf86 (since 10.0)
470      *   causes the premature termination condition to be once again
471      *   reachable.
472      *
473      * NOTE: Regardless of the state of this option, a premature
474      * termination of the TLS connection might happen due to error at
475      * any moment prior to the last send->recv iteration.
476      */
477     bool multifd_clean_tls_termination;
478 
479     /*
480      * This decides the size of guest memory chunk that will be used
481      * to track dirty bitmap clearing.  The size of memory chunk will
482      * be GUEST_PAGE_SIZE << N.  Say, N=0 means we will clear dirty
483      * bitmap for each page to send (1<<0=1); N=10 means we will clear
484      * dirty bitmap only once for 1<<10=1K continuous guest pages
485      * (which is in 4M chunk).
486      */
487     uint8_t clear_bitmap_shift;
488 
489     /*
490      * This save hostname when out-going migration starts
491      */
492     char *hostname;
493 
494     /* QEMU_VM_VMDESCRIPTION content filled for all non-iterable devices. */
495     JSONWriter *vmdesc;
496 
497     /*
498      * Indicates whether an ACK from the destination that it's OK to do
499      * switchover has been received.
500      */
501     bool switchover_acked;
502     /* Is this a rdma migration */
503     bool rdma_migration;
504 
505     GSource *hup_source;
506 };
507 
508 void migrate_set_state(MigrationStatus *state, MigrationStatus old_state,
509                        MigrationStatus new_state);
510 
511 void migration_fd_process_incoming(QEMUFile *f);
512 void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp);
513 void migration_incoming_process(void);
514 
515 bool  migration_has_all_channels(void);
516 
517 void migrate_set_error(MigrationState *s, const Error *error);
518 bool migrate_has_error(MigrationState *s);
519 
520 void migration_connect(MigrationState *s, Error *error_in);
521 
522 int migration_call_notifiers(MigrationState *s, MigrationEventType type,
523                              Error **errp);
524 
525 int migrate_init(MigrationState *s, Error **errp);
526 bool migration_is_blocked(Error **errp);
527 /* True if outgoing migration has entered postcopy phase */
528 bool migration_in_postcopy(void);
529 bool migration_postcopy_is_alive(MigrationStatus state);
530 MigrationState *migrate_get_current(void);
531 bool migration_has_failed(MigrationState *);
532 bool migrate_mode_is_cpr(MigrationState *);
533 
534 uint64_t ram_get_total_transferred_pages(void);
535 
536 /* Sending on the return path - generic and then for each message type */
537 void migrate_send_rp_shut(MigrationIncomingState *mis,
538                           uint32_t value);
539 void migrate_send_rp_pong(MigrationIncomingState *mis,
540                           uint32_t value);
541 int migrate_send_rp_req_pages(MigrationIncomingState *mis, RAMBlock *rb,
542                               ram_addr_t start, uint64_t haddr);
543 int migrate_send_rp_message_req_pages(MigrationIncomingState *mis,
544                                       RAMBlock *rb, ram_addr_t start);
545 void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
546                                  char *block_name);
547 void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value);
548 int migrate_send_rp_switchover_ack(MigrationIncomingState *mis);
549 
550 void dirty_bitmap_mig_before_vm_start(void);
551 void dirty_bitmap_mig_cancel_outgoing(void);
552 void dirty_bitmap_mig_cancel_incoming(void);
553 bool check_dirty_bitmap_mig_alias_map(const BitmapMigrationNodeAliasList *bbm,
554                                       Error **errp);
555 
556 void migrate_add_address(SocketAddress *address);
557 int foreach_not_ignored_block(RAMBlockIterFunc func, void *opaque);
558 
559 #define qemu_ram_foreach_block \
560   #warning "Use foreach_not_ignored_block in migration code"
561 
562 void migration_make_urgent_request(void);
563 void migration_consume_urgent_request(void);
564 bool migration_rate_limit(void);
565 void migration_bh_schedule(QEMUBHFunc *cb, void *opaque);
566 void migration_cancel(void);
567 
568 void migration_populate_vfio_info(MigrationInfo *info);
569 void migration_reset_vfio_bytes_transferred(void);
570 void postcopy_temp_page_reset(PostcopyTmpPage *tmp_page);
571 
572 /*
573  * Migration thread waiting for return path thread.  Return non-zero if an
574  * error is detected.
575  */
576 int migration_rp_wait(MigrationState *s);
577 /*
578  * Kick the migration thread waiting for return path messages.  NOTE: the
579  * name can be slightly confusing (when read as "kick the rp thread"), just
580  * to remember the target is always the migration thread.
581  */
582 void migration_rp_kick(MigrationState *s);
583 
584 void migration_bitmap_sync_precopy(bool last_stage);
585 
586 /* migration/block-dirty-bitmap.c */
587 void dirty_bitmap_mig_init(void);
588 bool should_send_vmdesc(void);
589 
590 #endif
591