xref: /openbmc/qemu/migration/migration.h (revision b2e64cb929c178629f53c7eb50649ea84c745d71)
16666c96aSJuan Quintela /*
26666c96aSJuan Quintela  * QEMU live migration
36666c96aSJuan Quintela  *
46666c96aSJuan Quintela  * Copyright IBM, Corp. 2008
56666c96aSJuan Quintela  *
66666c96aSJuan Quintela  * Authors:
76666c96aSJuan Quintela  *  Anthony Liguori   <aliguori@us.ibm.com>
86666c96aSJuan Quintela  *
96666c96aSJuan Quintela  * This work is licensed under the terms of the GNU GPL, version 2.  See
106666c96aSJuan Quintela  * the COPYING file in the top-level directory.
116666c96aSJuan Quintela  *
126666c96aSJuan Quintela  */
136666c96aSJuan Quintela 
146666c96aSJuan Quintela #ifndef QEMU_MIGRATION_H
156666c96aSJuan Quintela #define QEMU_MIGRATION_H
166666c96aSJuan Quintela 
17d4842052SMarkus Armbruster #include "exec/cpu-common.h"
18a27bd6c7SMarkus Armbruster #include "hw/qdev-core.h"
199af23989SMarkus Armbruster #include "qapi/qapi-types-migration.h"
20e3bf5e68SDavid Hildenbrand #include "qapi/qmp/json-writer.h"
216666c96aSJuan Quintela #include "qemu/thread.h"
229608723aSPaolo Bonzini #include "qemu/coroutine.h"
234f0fae7fSJuan Quintela #include "io/channel.h"
248518278aSAndrey Gruzdev #include "io/channel-buffer.h"
257659505cSDr. David Alan Gilbert #include "net/announce.h"
26db1015e9SEduardo Habkost #include "qom/object.h"
2736f62f11SPeter Xu #include "postcopy-ram.h"
28f4584076SVladimir Sementsov-Ogievskiy #include "sysemu/runstate.h"
29c9539d9bSSteve Sistare #include "migration/misc.h"
306666c96aSJuan Quintela 
31e620b1e4SPeter Xu #define  MIGRATION_THREAD_SNAPSHOT          "mig/snapshot"
32e620b1e4SPeter Xu #define  MIGRATION_THREAD_DIRTY_RATE        "mig/dirtyrate"
33e620b1e4SPeter Xu 
34e620b1e4SPeter Xu #define  MIGRATION_THREAD_SRC_MAIN          "mig/src/main"
35e620b1e4SPeter Xu #define  MIGRATION_THREAD_SRC_MULTIFD       "mig/src/send_%d"
36e620b1e4SPeter Xu #define  MIGRATION_THREAD_SRC_RETURN        "mig/src/return"
37e620b1e4SPeter Xu #define  MIGRATION_THREAD_SRC_TLS           "mig/src/tls"
38e620b1e4SPeter Xu 
39e620b1e4SPeter Xu #define  MIGRATION_THREAD_DST_COLO          "mig/dst/colo"
40*3b83e663SPrasad Pandit #define  MIGRATION_THREAD_DST_MULTIFD       "mig/dst/recv_%d"
41e620b1e4SPeter Xu #define  MIGRATION_THREAD_DST_FAULT         "mig/dst/fault"
42e620b1e4SPeter Xu #define  MIGRATION_THREAD_DST_LISTEN        "mig/dst/listen"
43e620b1e4SPeter Xu #define  MIGRATION_THREAD_DST_PREEMPT       "mig/dst/preempt"
44e620b1e4SPeter Xu 
452a4c42f1SAlexey Perevalov struct PostcopyBlocktimeContext;
462a4c42f1SAlexey Perevalov 
4713955b89SPeter Xu #define  MIGRATION_RESUME_ACK_VALUE  (1)
4813955b89SPeter Xu 
49002cad6bSPeter Xu /*
50002cad6bSPeter Xu  * 1<<6=64 pages -> 256K chunk when page size is 4K.  This gives us
51002cad6bSPeter Xu  * the benefit that all the chunks are 64 pages aligned then the
52002cad6bSPeter Xu  * bitmaps are always aligned to LONG.
53002cad6bSPeter Xu  */
54002cad6bSPeter Xu #define CLEAR_BITMAP_SHIFT_MIN             6
55002cad6bSPeter Xu /*
56002cad6bSPeter Xu  * 1<<18=256K pages -> 1G chunk when page size is 4K.  This is the
57002cad6bSPeter Xu  * default value to use if no one specified.
58002cad6bSPeter Xu  */
59002cad6bSPeter Xu #define CLEAR_BITMAP_SHIFT_DEFAULT        18
60002cad6bSPeter Xu /*
61002cad6bSPeter Xu  * 1<<31=2G pages -> 8T chunk when page size is 4K.  This should be
62002cad6bSPeter Xu  * big enough and make sure we won't overflow easily.
63002cad6bSPeter Xu  */
64002cad6bSPeter Xu #define CLEAR_BITMAP_SHIFT_MAX            31
65002cad6bSPeter Xu 
6677dadc3fSPeter Xu /* This is an abstraction of a "temp huge page" for postcopy's purpose */
6777dadc3fSPeter Xu typedef struct {
6877dadc3fSPeter Xu     /*
6977dadc3fSPeter Xu      * This points to a temporary huge page as a buffer for UFFDIO_COPY.  It's
7077dadc3fSPeter Xu      * mmap()ed and needs to be freed when cleanup.
7177dadc3fSPeter Xu      */
7277dadc3fSPeter Xu     void *tmp_huge_page;
7377dadc3fSPeter Xu     /*
7477dadc3fSPeter Xu      * This points to the host page we're going to install for this temp page.
7577dadc3fSPeter Xu      * It tells us after we've received the whole page, where we should put it.
7677dadc3fSPeter Xu      */
7777dadc3fSPeter Xu     void *host_addr;
7877dadc3fSPeter Xu     /* Number of small pages copied (in size of TARGET_PAGE_SIZE) */
7977dadc3fSPeter Xu     unsigned int target_pages;
8077dadc3fSPeter Xu     /* Whether this page contains all zeros */
8177dadc3fSPeter Xu     bool all_zero;
8277dadc3fSPeter Xu } PostcopyTmpPage;
8377dadc3fSPeter Xu 
846621883fSPeter Xu typedef enum {
856621883fSPeter Xu     PREEMPT_THREAD_NONE = 0,
866621883fSPeter Xu     PREEMPT_THREAD_CREATED,
876621883fSPeter Xu     PREEMPT_THREAD_QUIT,
886621883fSPeter Xu } PreemptThreadStatus;
896621883fSPeter Xu 
906666c96aSJuan Quintela /* State for the incoming migration */
916666c96aSJuan Quintela struct MigrationIncomingState {
926666c96aSJuan Quintela     QEMUFile *from_src_file;
93755e8d7cSPeter Xu     /* Previously received RAM's RAMBlock pointer */
94c01b16edSPeter Xu     RAMBlock *last_recv_block[RAM_CHANNEL_MAX];
951df6ddb4SDr. David Alan Gilbert     /* A hook to allow cleanup at the end of incoming migration */
961df6ddb4SDr. David Alan Gilbert     void *transport_data;
971df6ddb4SDr. David Alan Gilbert     void (*transport_cleanup)(void *data);
98095c12a4SPeter Xu     /*
99095c12a4SPeter Xu      * Used to sync thread creations.  Note that we can't create threads in
100095c12a4SPeter Xu      * parallel with this sem.
101095c12a4SPeter Xu      */
102095c12a4SPeter Xu     QemuSemaphore  thread_sync_sem;
1036666c96aSJuan Quintela     /*
1046666c96aSJuan Quintela      * Free at the start of the main state load, set as the main thread finishes
1056666c96aSJuan Quintela      * loading state.
1066666c96aSJuan Quintela      */
1076666c96aSJuan Quintela     QemuEvent main_thread_load_event;
1086666c96aSJuan Quintela 
1097659505cSDr. David Alan Gilbert     /* For network announces */
1107659505cSDr. David Alan Gilbert     AnnounceTimer  announce_timer;
1117659505cSDr. David Alan Gilbert 
1126666c96aSJuan Quintela     size_t         largest_page_size;
1136666c96aSJuan Quintela     bool           have_fault_thread;
1146666c96aSJuan Quintela     QemuThread     fault_thread;
11564f615feSPeter Xu     /* Set this when we want the fault thread to quit */
11664f615feSPeter Xu     bool           fault_thread_quit;
1176666c96aSJuan Quintela 
1186666c96aSJuan Quintela     bool           have_listen_thread;
1196666c96aSJuan Quintela     QemuThread     listen_thread;
1206666c96aSJuan Quintela 
1216666c96aSJuan Quintela     /* For the kernel to send us notifications */
1226666c96aSJuan Quintela     int       userfault_fd;
12364f615feSPeter Xu     /* To notify the fault_thread to wake, e.g., when need to quit */
12464f615feSPeter Xu     int       userfault_event_fd;
1256666c96aSJuan Quintela     QEMUFile *to_src_file;
1266666c96aSJuan Quintela     QemuMutex rp_mutex;    /* We send replies from multiple threads */
127096bf4c8SDr. David Alan Gilbert     /* RAMBlock of last request sent to source */
128096bf4c8SDr. David Alan Gilbert     RAMBlock *last_rb;
12977dadc3fSPeter Xu     /*
13077dadc3fSPeter Xu      * Number of postcopy channels including the default precopy channel, so
13177dadc3fSPeter Xu      * vanilla postcopy will only contain one channel which contain both
13277dadc3fSPeter Xu      * precopy and postcopy streams.
13377dadc3fSPeter Xu      *
13477dadc3fSPeter Xu      * This is calculated when the src requests to enable postcopy but before
13577dadc3fSPeter Xu      * it starts.  Its value can depend on e.g. whether postcopy preemption is
13677dadc3fSPeter Xu      * enabled.
13777dadc3fSPeter Xu      */
13877dadc3fSPeter Xu     unsigned int postcopy_channels;
13936f62f11SPeter Xu     /* QEMUFile for postcopy only; it'll be handled by a separate thread */
14036f62f11SPeter Xu     QEMUFile *postcopy_qemufile_dst;
1415655aab0SPeter Xu     /*
1425655aab0SPeter Xu      * When postcopy_qemufile_dst is properly setup, this sem is posted.
1435655aab0SPeter Xu      * One can wait on this semaphore to wait until the preempt channel is
1445655aab0SPeter Xu      * properly setup.
1455655aab0SPeter Xu      */
1465655aab0SPeter Xu     QemuSemaphore postcopy_qemufile_dst_done;
14736f62f11SPeter Xu     /* Postcopy priority thread is used to receive postcopy requested pages */
14836f62f11SPeter Xu     QemuThread postcopy_prio_thread;
1496621883fSPeter Xu     /*
1506621883fSPeter Xu      * Always set by the main vm load thread only, but can be read by the
1516621883fSPeter Xu      * postcopy preempt thread.  "volatile" makes sure all reads will be
152d8b71d96SMichael Tokarev      * up-to-date across cores.
1536621883fSPeter Xu      */
1546621883fSPeter Xu     volatile PreemptThreadStatus preempt_thread_status;
15577dadc3fSPeter Xu     /*
15660bb3c58SPeter Xu      * Used to sync between the ram load main thread and the fast ram load
15760bb3c58SPeter Xu      * thread.  It protects postcopy_qemufile_dst, which is the postcopy
15860bb3c58SPeter Xu      * fast channel.
15960bb3c58SPeter Xu      *
16060bb3c58SPeter Xu      * The ram fast load thread will take it mostly for the whole lifecycle
16160bb3c58SPeter Xu      * because it needs to continuously read data from the channel, and
16260bb3c58SPeter Xu      * it'll only release this mutex if postcopy is interrupted, so that
16360bb3c58SPeter Xu      * the ram load main thread will take this mutex over and properly
16460bb3c58SPeter Xu      * release the broken channel.
16560bb3c58SPeter Xu      */
16660bb3c58SPeter Xu     QemuMutex postcopy_prio_thread_mutex;
16760bb3c58SPeter Xu     /*
16877dadc3fSPeter Xu      * An array of temp host huge pages to be used, one for each postcopy
16977dadc3fSPeter Xu      * channel.
17077dadc3fSPeter Xu      */
17177dadc3fSPeter Xu     PostcopyTmpPage *postcopy_tmp_pages;
17277dadc3fSPeter Xu     /* This is shared for all postcopy channels */
1736666c96aSJuan Quintela     void     *postcopy_tmp_zero_page;
17400fa4fc8SDr. David Alan Gilbert     /* PostCopyFD's for external userfaultfds & handlers of shared memory */
17500fa4fc8SDr. David Alan Gilbert     GArray   *postcopy_remote_fds;
1766666c96aSJuan Quintela 
177a5c24e13SPeter Xu     MigrationStatus state;
1786666c96aSJuan Quintela 
179dd42ce24SVladimir Sementsov-Ogievskiy     /*
180dd42ce24SVladimir Sementsov-Ogievskiy      * The incoming migration coroutine, non-NULL during qemu_loadvm_state().
181dd42ce24SVladimir Sementsov-Ogievskiy      * Used to wake the migration incoming coroutine from rdma code. How much is
182dd42ce24SVladimir Sementsov-Ogievskiy      * it safe - it's a question.
183dd42ce24SVladimir Sementsov-Ogievskiy      */
184dd42ce24SVladimir Sementsov-Ogievskiy     Coroutine *loadvm_co;
185dd42ce24SVladimir Sementsov-Ogievskiy 
1866666c96aSJuan Quintela     /* The coroutine we should enter (back) after failover */
187dd42ce24SVladimir Sementsov-Ogievskiy     Coroutine *colo_incoming_co;
1886666c96aSJuan Quintela     QemuSemaphore colo_incoming_sem;
1892a4c42f1SAlexey Perevalov 
1902a4c42f1SAlexey Perevalov     /*
1912a4c42f1SAlexey Perevalov      * PostcopyBlocktimeContext to keep information for postcopy
1922a4c42f1SAlexey Perevalov      * live migration, to calculate vCPU block time
1932a4c42f1SAlexey Perevalov      * */
1942a4c42f1SAlexey Perevalov     struct PostcopyBlocktimeContext *blocktime_ctx;
195b411b844SPeter Xu 
196b411b844SPeter Xu     /* notify PAUSED postcopy incoming migrations to try to continue */
197b411b844SPeter Xu     QemuSemaphore postcopy_pause_sem_dst;
1983a7804c3SPeter Xu     QemuSemaphore postcopy_pause_sem_fault;
19960bb3c58SPeter Xu     /*
20060bb3c58SPeter Xu      * This semaphore is used to allow the ram fast load thread (only when
20160bb3c58SPeter Xu      * postcopy preempt is enabled) fall into sleep when there's network
20260bb3c58SPeter Xu      * interruption detected.  When the recovery is done, the main load
20360bb3c58SPeter Xu      * thread will kick the fast ram load thread using this semaphore.
20460bb3c58SPeter Xu      */
20560bb3c58SPeter Xu     QemuSemaphore postcopy_pause_sem_fast_load;
2069aca82baSJuan Quintela 
2079aca82baSJuan Quintela     /* List of listening socket addresses  */
2089aca82baSJuan Quintela     SocketAddressList *socket_address_list;
2098f8bfffcSPeter Xu 
2108f8bfffcSPeter Xu     /* A tree of pages that we requested to the source VM */
2118f8bfffcSPeter Xu     GTree *page_requested;
212cf02f29eSPeter Xu     /*
213cf02f29eSPeter Xu      * For postcopy only, count the number of requested page faults that
214cf02f29eSPeter Xu      * still haven't been resolved.
215cf02f29eSPeter Xu      */
2168f8bfffcSPeter Xu     int page_requested_count;
2178f8bfffcSPeter Xu     /*
2188f8bfffcSPeter Xu      * The mutex helps to maintain the requested pages that we sent to the
2198f8bfffcSPeter Xu      * source, IOW, to guarantee coherent between the page_requests tree and
2208f8bfffcSPeter Xu      * the per-ramblock receivedmap.  Note! This does not guarantee consistency
2218f8bfffcSPeter Xu      * of the real page copy procedures (using UFFDIO_[ZERO]COPY).  E.g., even
2228f8bfffcSPeter Xu      * if one bit in receivedmap is cleared, UFFDIO_COPY could have happened
2238f8bfffcSPeter Xu      * for that page already.  This is intended so that the mutex won't
2248f8bfffcSPeter Xu      * serialize and blocked by slow operations like UFFDIO_* ioctls.  However
2258f8bfffcSPeter Xu      * this should be enough to make sure the page_requested tree always
2268f8bfffcSPeter Xu      * contains valid information.
2278f8bfffcSPeter Xu      */
2288f8bfffcSPeter Xu     QemuMutex page_request_mutex;
229cf02f29eSPeter Xu     /*
230cf02f29eSPeter Xu      * If postcopy preempt is enabled, there is a chance that the main
231cf02f29eSPeter Xu      * thread finished loading its data before the preempt channel has
232cf02f29eSPeter Xu      * finished loading the urgent pages.  If that happens, the two threads
233cf02f29eSPeter Xu      * will use this condvar to synchronize, so the main thread will always
234cf02f29eSPeter Xu      * wait until all pages received.
235cf02f29eSPeter Xu      */
236cf02f29eSPeter Xu     QemuCond page_request_cond;
2371b4adb10SAvihai Horon 
2381b4adb10SAvihai Horon     /*
2391b4adb10SAvihai Horon      * Number of devices that have yet to approve switchover. When this reaches
2401b4adb10SAvihai Horon      * zero an ACK that it's OK to do switchover is sent to the source. No lock
2411b4adb10SAvihai Horon      * is needed as this field is updated serially.
2421b4adb10SAvihai Horon      */
2431b4adb10SAvihai Horon     unsigned int switchover_ack_pending_num;
244dbea1c89SVladimir Sementsov-Ogievskiy 
245dbea1c89SVladimir Sementsov-Ogievskiy     /* Do exit on incoming migration failure */
246dbea1c89SVladimir Sementsov-Ogievskiy     bool exit_on_error;
2476666c96aSJuan Quintela };
2486666c96aSJuan Quintela 
2496666c96aSJuan Quintela MigrationIncomingState *migration_incoming_get_current(void);
2506666c96aSJuan Quintela void migration_incoming_state_destroy(void);
251e031149cSPeter Xu void migration_incoming_transport_cleanup(MigrationIncomingState *mis);
25265ace060SAlexey Perevalov /*
25365ace060SAlexey Perevalov  * Functions to work with blocktime context
25465ace060SAlexey Perevalov  */
25565ace060SAlexey Perevalov void fill_destination_postcopy_migration_info(MigrationInfo *info);
2566666c96aSJuan Quintela 
257e5cb7e76SPeter Xu #define TYPE_MIGRATION "migration"
258e5cb7e76SPeter Xu 
259db1015e9SEduardo Habkost typedef struct MigrationClass MigrationClass;
2608110fa1dSEduardo Habkost DECLARE_OBJ_CHECKERS(MigrationState, MigrationClass,
2618110fa1dSEduardo Habkost                      MIGRATION_OBJ, TYPE_MIGRATION)
262e5cb7e76SPeter Xu 
263db1015e9SEduardo Habkost struct MigrationClass {
264e5cb7e76SPeter Xu     /*< private >*/
265e5cb7e76SPeter Xu     DeviceClass parent_class;
266db1015e9SEduardo Habkost };
267e5cb7e76SPeter Xu 
268f16aee44SBihong Yu struct MigrationState {
269e5cb7e76SPeter Xu     /*< private >*/
270e5cb7e76SPeter Xu     DeviceState parent_obj;
271e5cb7e76SPeter Xu 
272e5cb7e76SPeter Xu     /*< public >*/
2736666c96aSJuan Quintela     QemuThread thread;
27443044ac0SPeter Xu     /* Protected by qemu_file_lock */
2756666c96aSJuan Quintela     QEMUFile *to_dst_file;
27636f62f11SPeter Xu     /* Postcopy specific transfer channel */
27736f62f11SPeter Xu     QEMUFile *postcopy_qemufile_src;
278d0edb8a1SPeter Xu     /*
279d0edb8a1SPeter Xu      * It is posted when the preempt channel is established.  Note: this is
280d0edb8a1SPeter Xu      * used for both the start or recover of a postcopy migration.  We'll
281d0edb8a1SPeter Xu      * post to this sem every time a new preempt channel is created in the
282d0edb8a1SPeter Xu      * main thread, and we keep post() and wait() in pair.
283d0edb8a1SPeter Xu      */
284d0edb8a1SPeter Xu     QemuSemaphore postcopy_qemufile_src_sem;
2858518278aSAndrey Gruzdev     QIOChannelBuffer *bioc;
28662df066fSPeter Xu     /*
28743044ac0SPeter Xu      * Protects to_dst_file/from_dst_file pointers.  We need to make sure we
28843044ac0SPeter Xu      * won't yield or hang during the critical section, since this lock will be
28943044ac0SPeter Xu      * used in OOB command handler.
29062df066fSPeter Xu      */
29162df066fSPeter Xu     QemuMutex qemu_file_lock;
2926666c96aSJuan Quintela 
293ad767bedSDr. David Alan Gilbert     /*
294ad767bedSDr. David Alan Gilbert      * Used to allow urgent requests to override rate limiting.
295ad767bedSDr. David Alan Gilbert      */
296ad767bedSDr. David Alan Gilbert     QemuSemaphore rate_limit_sem;
297ad767bedSDr. David Alan Gilbert 
298aecbfe9cSXiao Guangrong     /* pages already send at the beginning of current iteration */
299aecbfe9cSXiao Guangrong     uint64_t iteration_initial_pages;
300aecbfe9cSXiao Guangrong 
301aecbfe9cSXiao Guangrong     /* pages transferred per second */
302aecbfe9cSXiao Guangrong     double pages_per_second;
303aecbfe9cSXiao Guangrong 
304aecbfe9cSXiao Guangrong     /* bytes already send at the beginning of current iteration */
305b15df1aeSPeter Xu     uint64_t iteration_initial_bytes;
306b15df1aeSPeter Xu     /* time at the start of current iteration */
307b15df1aeSPeter Xu     int64_t iteration_start_time;
308b15df1aeSPeter Xu     /*
309b15df1aeSPeter Xu      * The final stage happens when the remaining data is smaller than
310b15df1aeSPeter Xu      * this threshold; it's calculated from the requested downtime and
3118b239597SPeter Xu      * measured bandwidth, or avail-switchover-bandwidth if specified.
312b15df1aeSPeter Xu      */
313a8629e0cSPeter Xu     uint64_t threshold_size;
314b15df1aeSPeter Xu 
3156666c96aSJuan Quintela     /* params from 'migrate-set-parameters' */
3166666c96aSJuan Quintela     MigrationParameters parameters;
3176666c96aSJuan Quintela 
318a5c24e13SPeter Xu     MigrationStatus state;
3196666c96aSJuan Quintela 
3206666c96aSJuan Quintela     /* State related to return path */
3216666c96aSJuan Quintela     struct {
32243044ac0SPeter Xu         /* Protected by qemu_file_lock */
3236666c96aSJuan Quintela         QEMUFile     *from_dst_file;
3246666c96aSJuan Quintela         QemuThread    rp_thread;
32553021ea1SPeter Xu         /*
32653021ea1SPeter Xu          * We can also check non-zero of rp_thread, but there's no "official"
32753021ea1SPeter Xu          * way to do this, so this bool makes it slightly more elegant.
32853021ea1SPeter Xu          * Checking from_dst_file for this is racy because from_dst_file will
32953021ea1SPeter Xu          * be cleared in the rp_thread!
33053021ea1SPeter Xu          */
33153021ea1SPeter Xu         bool          rp_thread_created;
3325e79a4bfSPeter Xu         /*
3335e79a4bfSPeter Xu          * Used to synchronize between migration main thread and return
3345e79a4bfSPeter Xu          * path thread.  The migration thread can wait() on this sem, while
3355e79a4bfSPeter Xu          * other threads (e.g., return path thread) can kick it using a
3365e79a4bfSPeter Xu          * post().
3375e79a4bfSPeter Xu          */
338edd090c7SPeter Xu         QemuSemaphore rp_sem;
339b28fb582SPeter Xu         /*
340b28fb582SPeter Xu          * We post to this when we got one PONG from dest. So far it's an
341b28fb582SPeter Xu          * easy way to know the main channel has successfully established
342b28fb582SPeter Xu          * on dest QEMU.
343b28fb582SPeter Xu          */
344b28fb582SPeter Xu         QemuSemaphore rp_pong_acks;
3456666c96aSJuan Quintela     } rp_state;
3466666c96aSJuan Quintela 
3476666c96aSJuan Quintela     double mbps;
3484af246a3SPeter Xu     /* Timestamp when recent migration starts (ms) */
3494af246a3SPeter Xu     int64_t start_time;
3504af246a3SPeter Xu     /* Total time used by latest migration (ms) */
3516666c96aSJuan Quintela     int64_t total_time;
35264909f97SPeter Xu     /* Timestamp when VM is down (ms) to migrate the last stuff */
35364909f97SPeter Xu     int64_t downtime_start;
3546666c96aSJuan Quintela     int64_t downtime;
3556666c96aSJuan Quintela     int64_t expected_downtime;
3560cec2056SJuan Quintela     bool capabilities[MIGRATION_CAPABILITY__MAX];
3576666c96aSJuan Quintela     int64_t setup_time;
358f4584076SVladimir Sementsov-Ogievskiy 
3597287cbd4SPeter Xu     /*
360f4584076SVladimir Sementsov-Ogievskiy      * State before stopping the vm by vm_stop_force_state().
3617287cbd4SPeter Xu      * If migration is interrupted by any reason, we need to continue
362f4584076SVladimir Sementsov-Ogievskiy      * running the guest on source if it was running or restore its stopped
363f4584076SVladimir Sementsov-Ogievskiy      * state.
3647287cbd4SPeter Xu      */
365f4584076SVladimir Sementsov-Ogievskiy     RunState vm_old_state;
3666666c96aSJuan Quintela 
3676666c96aSJuan Quintela     /* Flag set once the migration has been asked to enter postcopy */
3686666c96aSJuan Quintela     bool start_postcopy;
3696666c96aSJuan Quintela 
3706666c96aSJuan Quintela     /* Flag set once the migration thread is running (and needs joining) */
3716666c96aSJuan Quintela     bool migration_thread_running;
3726666c96aSJuan Quintela 
3736666c96aSJuan Quintela     /* Flag set once the migration thread called bdrv_inactivate_all */
3746666c96aSJuan Quintela     bool block_inactive;
3756666c96aSJuan Quintela 
376c7e0acd5SJens Freimann     /* Migration is waiting for guest to unplug device */
377c7e0acd5SJens Freimann     QemuSemaphore wait_unplug_sem;
378c7e0acd5SJens Freimann 
379e91d8951SDr. David Alan Gilbert     /* Migration is paused due to pause-before-switchover */
380e91d8951SDr. David Alan Gilbert     QemuSemaphore pause_sem;
381e91d8951SDr. David Alan Gilbert 
3826666c96aSJuan Quintela     /* The semaphore is used to notify COLO thread that failover is finished */
3836666c96aSJuan Quintela     QemuSemaphore colo_exit_sem;
3846666c96aSJuan Quintela 
385bb70b66eSLukas Straub     /* The event is used to notify COLO thread to do checkpoint */
386bb70b66eSLukas Straub     QemuEvent colo_checkpoint_event;
3876666c96aSJuan Quintela     int64_t colo_checkpoint_time;
3886666c96aSJuan Quintela     QEMUTimer *colo_delay_timer;
3896666c96aSJuan Quintela 
39087db1a7dSJuan Quintela     /* The first error that has occurred.
39187db1a7dSJuan Quintela        We used the mutex to be able to return the 1st error message */
3926666c96aSJuan Quintela     Error *error;
39387db1a7dSJuan Quintela     /* mutex to protect errp */
39487db1a7dSJuan Quintela     QemuMutex error_mutex;
39587db1a7dSJuan Quintela 
3965272298cSPeter Xu     /*
3975272298cSPeter Xu      * Global switch on whether we need to store the global state
3985272298cSPeter Xu      * during migration.
3995272298cSPeter Xu      */
4005272298cSPeter Xu     bool store_global_state;
4013df663e5SPeter Xu 
40271dd4c1aSPeter Xu     /* Whether we send QEMU_VM_CONFIGURATION during migration */
40371dd4c1aSPeter Xu     bool send_configuration;
40415c38503SPeter Xu     /* Whether we send section footer during migration */
40515c38503SPeter Xu     bool send_section_footer;
406b23c2adeSPeter Xu 
407b23c2adeSPeter Xu     /* Needed by postcopy-pause state */
408b23c2adeSPeter Xu     QemuSemaphore postcopy_pause_sem;
409f548222cSXiao Guangrong     /*
4106621883fSPeter Xu      * This variable only affects behavior when postcopy preempt mode is
4116621883fSPeter Xu      * enabled.
4126621883fSPeter Xu      *
4136621883fSPeter Xu      * When set:
4146621883fSPeter Xu      *
4156621883fSPeter Xu      * - postcopy preempt src QEMU instance will generate an EOS message at
4166621883fSPeter Xu      *   the end of migration to shut the preempt channel on dest side.
4176621883fSPeter Xu      *
41806064a67SPeter Xu      * - postcopy preempt channel will be created at the setup phase on src
41906064a67SPeter Xu          QEMU.
42006064a67SPeter Xu      *
4216621883fSPeter Xu      * When clear:
4226621883fSPeter Xu      *
4236621883fSPeter Xu      * - postcopy preempt src QEMU instance will _not_ generate an EOS
4246621883fSPeter Xu      *   message at the end of migration, the dest qemu will shutdown the
4256621883fSPeter Xu      *   channel itself.
4266621883fSPeter Xu      *
42706064a67SPeter Xu      * - postcopy preempt channel will be created at the switching phase
428d8b71d96SMichael Tokarev      *   from precopy -> postcopy (to avoid race condition of misordered
42906064a67SPeter Xu      *   creation of channels).
43006064a67SPeter Xu      *
4316621883fSPeter Xu      * NOTE: See message-id <ZBoShWArKDPpX/D7@work-vm> on qemu-devel
4326621883fSPeter Xu      * mailing list for more information on the possible race.  Everyone
4336621883fSPeter Xu      * should probably just keep this value untouched after set by the
4346621883fSPeter Xu      * machine type (or the default).
4356621883fSPeter Xu      */
4366621883fSPeter Xu     bool preempt_pre_7_2;
437002cad6bSPeter Xu 
438002cad6bSPeter Xu     /*
43977c259a4SJuan Quintela      * flush every channel after each section sent.
44077c259a4SJuan Quintela      *
44177c259a4SJuan Quintela      * This assures that we can't mix pages from one iteration through
44277c259a4SJuan Quintela      * ram pages with pages for the following iteration.  We really
44377c259a4SJuan Quintela      * only need to do this flush after we have go through all the
44477c259a4SJuan Quintela      * dirty pages.  For historical reasons, we do that after each
44577c259a4SJuan Quintela      * section.  This is suboptimal (we flush too many times).
446294e5a40SJuan Quintela      * Default value is false. (since 8.1)
44777c259a4SJuan Quintela      */
44877c259a4SJuan Quintela     bool multifd_flush_after_each_section;
44977c259a4SJuan Quintela     /*
450002cad6bSPeter Xu      * This decides the size of guest memory chunk that will be used
451002cad6bSPeter Xu      * to track dirty bitmap clearing.  The size of memory chunk will
452002cad6bSPeter Xu      * be GUEST_PAGE_SIZE << N.  Say, N=0 means we will clear dirty
453002cad6bSPeter Xu      * bitmap for each page to send (1<<0=1); N=10 means we will clear
454002cad6bSPeter Xu      * dirty bitmap only once for 1<<10=1K continuous guest pages
455002cad6bSPeter Xu      * (which is in 4M chunk).
456002cad6bSPeter Xu      */
457002cad6bSPeter Xu     uint8_t clear_bitmap_shift;
458d8053e73SChuan Zheng 
459d8053e73SChuan Zheng     /*
460d8053e73SChuan Zheng      * This save hostname when out-going migration starts
461d8053e73SChuan Zheng      */
462d8053e73SChuan Zheng     char *hostname;
463e3bf5e68SDavid Hildenbrand 
464e3bf5e68SDavid Hildenbrand     /* QEMU_VM_VMDESCRIPTION content filled for all non-iterable devices. */
465e3bf5e68SDavid Hildenbrand     JSONWriter *vmdesc;
4661b4adb10SAvihai Horon 
4671b4adb10SAvihai Horon     /*
4681b4adb10SAvihai Horon      * Indicates whether an ACK from the destination that it's OK to do
4691b4adb10SAvihai Horon      * switchover has been received.
4701b4adb10SAvihai Horon      */
4711b4adb10SAvihai Horon     bool switchover_acked;
47227fd25b0SJuan Quintela     /* Is this a rdma migration */
47327fd25b0SJuan Quintela     bool rdma_migration;
4746666c96aSJuan Quintela };
4756666c96aSJuan Quintela 
476a5c24e13SPeter Xu void migrate_set_state(MigrationStatus *state, MigrationStatus old_state,
477a5c24e13SPeter Xu                        MigrationStatus new_state);
4786666c96aSJuan Quintela 
479b0cf3bfcSAvihai Horon void migration_fd_process_incoming(QEMUFile *f);
48049ed0d24SFei Li void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp);
48136c2f8beSJuan Quintela void migration_incoming_process(void);
4826666c96aSJuan Quintela 
483428d8908SJuan Quintela bool  migration_has_all_channels(void);
484428d8908SJuan Quintela 
48587db1a7dSJuan Quintela void migrate_set_error(MigrationState *s, const Error *error);
4862b2f6f41SPeter Xu bool migrate_has_error(MigrationState *s);
4876666c96aSJuan Quintela 
488cce8040bSDr. David Alan Gilbert void migrate_fd_connect(MigrationState *s, Error *error_in);
4896666c96aSJuan Quintela 
490c9539d9bSSteve Sistare int migration_call_notifiers(MigrationState *s, MigrationEventType type,
491c9539d9bSSteve Sistare                              Error **errp);
492c9539d9bSSteve Sistare 
49308fc4cb5SAvihai Horon int migrate_init(MigrationState *s, Error **errp);
4946666c96aSJuan Quintela bool migration_is_blocked(Error **errp);
4956666c96aSJuan Quintela /* True if outgoing migration has entered postcopy phase */
4966666c96aSJuan Quintela bool migration_in_postcopy(void);
497a5c24e13SPeter Xu bool migration_postcopy_is_alive(MigrationStatus state);
4986666c96aSJuan Quintela MigrationState *migrate_get_current(void);
499c9539d9bSSteve Sistare bool migration_has_failed(MigrationState *);
500c9539d9bSSteve Sistare bool migrate_mode_is_cpr(MigrationState *);
5016666c96aSJuan Quintela 
502aecbfe9cSXiao Guangrong uint64_t ram_get_total_transferred_pages(void);
503aecbfe9cSXiao Guangrong 
5046666c96aSJuan Quintela /* Sending on the return path - generic and then for each message type */
5056666c96aSJuan Quintela void migrate_send_rp_shut(MigrationIncomingState *mis,
5066666c96aSJuan Quintela                           uint32_t value);
5076666c96aSJuan Quintela void migrate_send_rp_pong(MigrationIncomingState *mis,
5086666c96aSJuan Quintela                           uint32_t value);
5092e2bce16SPeter Xu int migrate_send_rp_req_pages(MigrationIncomingState *mis, RAMBlock *rb,
5108f8bfffcSPeter Xu                               ram_addr_t start, uint64_t haddr);
5117a267fc4SPeter Xu int migrate_send_rp_message_req_pages(MigrationIncomingState *mis,
5127a267fc4SPeter Xu                                       RAMBlock *rb, ram_addr_t start);
513a335debbSPeter Xu void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
514a335debbSPeter Xu                                  char *block_name);
51513955b89SPeter Xu void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value);
5161b4adb10SAvihai Horon int migrate_send_rp_switchover_ack(MigrationIncomingState *mis);
5176666c96aSJuan Quintela 
518b35ebdf0SVladimir Sementsov-Ogievskiy void dirty_bitmap_mig_before_vm_start(void);
5191499ab09SVladimir Sementsov-Ogievskiy void dirty_bitmap_mig_cancel_outgoing(void);
5201499ab09SVladimir Sementsov-Ogievskiy void dirty_bitmap_mig_cancel_incoming(void);
52131e4c354SMax Reitz bool check_dirty_bitmap_mig_alias_map(const BitmapMigrationNodeAliasList *bbm,
52231e4c354SMax Reitz                                       Error **errp);
52331e4c354SMax Reitz 
5249aca82baSJuan Quintela void migrate_add_address(SocketAddress *address);
525967f2de5SHet Gala bool migrate_uri_parse(const char *uri, MigrationChannel **channel,
526967f2de5SHet Gala                        Error **errp);
527fbd162e6SYury Kotov int foreach_not_ignored_block(RAMBlockIterFunc func, void *opaque);
528fbd162e6SYury Kotov 
529343f632cSDr. David Alan Gilbert #define qemu_ram_foreach_block \
530fbd162e6SYury Kotov   #warning "Use foreach_not_ignored_block in migration code"
531343f632cSDr. David Alan Gilbert 
532ad767bedSDr. David Alan Gilbert void migration_make_urgent_request(void);
533ad767bedSDr. David Alan Gilbert void migration_consume_urgent_request(void);
53497e1e067SDr. David Alan Gilbert bool migration_rate_limit(void);
53544d0d456SFabiano Rosas void migration_bh_schedule(QEMUBHFunc *cb, void *opaque);
536458feccaSLaurent Vivier void migration_cancel(const Error *error);
537ad767bedSDr. David Alan Gilbert 
53838c482b4SAvihai Horon void migration_populate_vfio_info(MigrationInfo *info);
53938c482b4SAvihai Horon void migration_reset_vfio_bytes_transferred(void);
54077dadc3fSPeter Xu void postcopy_temp_page_reset(PostcopyTmpPage *tmp_page);
54143bd0bf3SThomas Huth 
542f8c543e8SPeter Xu /*
543f8c543e8SPeter Xu  * Migration thread waiting for return path thread.  Return non-zero if an
544f8c543e8SPeter Xu  * error is detected.
545f8c543e8SPeter Xu  */
546f8c543e8SPeter Xu int migration_rp_wait(MigrationState *s);
5475e79a4bfSPeter Xu /*
5485e79a4bfSPeter Xu  * Kick the migration thread waiting for return path messages.  NOTE: the
5495e79a4bfSPeter Xu  * name can be slightly confusing (when read as "kick the rp thread"), just
5505e79a4bfSPeter Xu  * to remember the target is always the migration thread.
5515e79a4bfSPeter Xu  */
5525e79a4bfSPeter Xu void migration_rp_kick(MigrationState *s);
5535e79a4bfSPeter Xu 
55452ac968aSHyman Huang void migration_bitmap_sync_precopy(bool last_stage);
555a4ddab35SPeter Xu 
556a4ddab35SPeter Xu /* migration/block-dirty-bitmap.c */
557a4ddab35SPeter Xu void dirty_bitmap_mig_init(void);
558a4ddab35SPeter Xu 
5596666c96aSJuan Quintela #endif
560