xref: /openbmc/qemu/migration/migration.h (revision a9ded601)
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 "qapi/qmp/qdict.h"
18 #include "qemu-common.h"
19 #include "qemu/thread.h"
20 #include "qemu/notify.h"
21 #include "qapi-types.h"
22 #include "exec/cpu-common.h"
23 #include "qemu/coroutine_int.h"
24 
25 /* State for the incoming migration */
26 struct MigrationIncomingState {
27     QEMUFile *from_src_file;
28 
29     /*
30      * Free at the start of the main state load, set as the main thread finishes
31      * loading state.
32      */
33     QemuEvent main_thread_load_event;
34 
35     size_t         largest_page_size;
36     bool           have_fault_thread;
37     QemuThread     fault_thread;
38     QemuSemaphore  fault_thread_sem;
39 
40     bool           have_listen_thread;
41     QemuThread     listen_thread;
42     QemuSemaphore  listen_thread_sem;
43 
44     /* For the kernel to send us notifications */
45     int       userfault_fd;
46     /* To tell the fault_thread to quit */
47     int       userfault_quit_fd;
48     QEMUFile *to_src_file;
49     QemuMutex rp_mutex;    /* We send replies from multiple threads */
50     void     *postcopy_tmp_page;
51     void     *postcopy_tmp_zero_page;
52 
53     QEMUBH *bh;
54 
55     int state;
56 
57     bool have_colo_incoming_thread;
58     QemuThread colo_incoming_thread;
59     /* The coroutine we should enter (back) after failover */
60     Coroutine *migration_incoming_co;
61     QemuSemaphore colo_incoming_sem;
62 };
63 
64 MigrationIncomingState *migration_incoming_get_current(void);
65 void migration_incoming_state_destroy(void);
66 
67 struct MigrationState
68 {
69     size_t bytes_xfer;
70     size_t xfer_limit;
71     QemuThread thread;
72     QEMUBH *cleanup_bh;
73     QEMUFile *to_dst_file;
74 
75     /* params from 'migrate-set-parameters' */
76     MigrationParameters parameters;
77 
78     int state;
79 
80     /* State related to return path */
81     struct {
82         QEMUFile     *from_dst_file;
83         QemuThread    rp_thread;
84         bool          error;
85     } rp_state;
86 
87     double mbps;
88     int64_t total_time;
89     int64_t downtime;
90     int64_t expected_downtime;
91     bool enabled_capabilities[MIGRATION_CAPABILITY__MAX];
92     int64_t xbzrle_cache_size;
93     int64_t setup_time;
94 
95     /* Flag set once the migration has been asked to enter postcopy */
96     bool start_postcopy;
97     /* Flag set after postcopy has sent the device state */
98     bool postcopy_after_devices;
99 
100     /* Flag set once the migration thread is running (and needs joining) */
101     bool migration_thread_running;
102 
103     /* Flag set once the migration thread called bdrv_inactivate_all */
104     bool block_inactive;
105 
106     /* The semaphore is used to notify COLO thread that failover is finished */
107     QemuSemaphore colo_exit_sem;
108 
109     /* The semaphore is used to notify COLO thread to do checkpoint */
110     QemuSemaphore colo_checkpoint_sem;
111     int64_t colo_checkpoint_time;
112     QEMUTimer *colo_delay_timer;
113 
114     /* The last error that occurred */
115     Error *error;
116     /* Do we have to clean up -b/-i from old migrate parameters */
117     /* This feature is deprecated and will be removed */
118     bool must_remove_block_options;
119 };
120 
121 void migrate_set_state(int *state, int old_state, int new_state);
122 
123 void migration_fd_process_incoming(QEMUFile *f);
124 
125 uint64_t migrate_max_downtime(void);
126 
127 void migrate_fd_error(MigrationState *s, const Error *error);
128 
129 void migrate_fd_connect(MigrationState *s);
130 
131 MigrationState *migrate_init(void);
132 bool migration_is_blocked(Error **errp);
133 /* True if outgoing migration has entered postcopy phase */
134 bool migration_in_postcopy(void);
135 MigrationState *migrate_get_current(void);
136 
137 bool migrate_release_ram(void);
138 bool migrate_postcopy_ram(void);
139 bool migrate_zero_blocks(void);
140 
141 bool migrate_auto_converge(void);
142 
143 int migrate_use_xbzrle(void);
144 int64_t migrate_xbzrle_cache_size(void);
145 bool migrate_colo_enabled(void);
146 
147 bool migrate_use_block(void);
148 bool migrate_use_block_incremental(void);
149 
150 bool migrate_use_compression(void);
151 int migrate_compress_level(void);
152 int migrate_compress_threads(void);
153 int migrate_decompress_threads(void);
154 bool migrate_use_events(void);
155 
156 /* Sending on the return path - generic and then for each message type */
157 void migrate_send_rp_shut(MigrationIncomingState *mis,
158                           uint32_t value);
159 void migrate_send_rp_pong(MigrationIncomingState *mis,
160                           uint32_t value);
161 void migrate_send_rp_req_pages(MigrationIncomingState *mis, const char* rbname,
162                               ram_addr_t start, size_t len);
163 
164 #endif
165