xref: /qemu/migration/migration.h (revision db1015e92e04835c9eb50c29625fe566d1202dbd)
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 "qemu/thread.h"
21 #include "qemu/coroutine_int.h"
22 #include "io/channel.h"
23 #include "net/announce.h"
24 #include "qom/object.h"
25 
26 struct PostcopyBlocktimeContext;
27 
28 #define  MIGRATION_RESUME_ACK_VALUE  (1)
29 
30 /*
31  * 1<<6=64 pages -> 256K chunk when page size is 4K.  This gives us
32  * the benefit that all the chunks are 64 pages aligned then the
33  * bitmaps are always aligned to LONG.
34  */
35 #define CLEAR_BITMAP_SHIFT_MIN             6
36 /*
37  * 1<<18=256K pages -> 1G chunk when page size is 4K.  This is the
38  * default value to use if no one specified.
39  */
40 #define CLEAR_BITMAP_SHIFT_DEFAULT        18
41 /*
42  * 1<<31=2G pages -> 8T chunk when page size is 4K.  This should be
43  * big enough and make sure we won't overflow easily.
44  */
45 #define CLEAR_BITMAP_SHIFT_MAX            31
46 
47 /* State for the incoming migration */
48 struct MigrationIncomingState {
49     QEMUFile *from_src_file;
50 
51     /*
52      * Free at the start of the main state load, set as the main thread finishes
53      * loading state.
54      */
55     QemuEvent main_thread_load_event;
56 
57     /* For network announces */
58     AnnounceTimer  announce_timer;
59 
60     size_t         largest_page_size;
61     bool           have_fault_thread;
62     QemuThread     fault_thread;
63     QemuSemaphore  fault_thread_sem;
64     /* Set this when we want the fault thread to quit */
65     bool           fault_thread_quit;
66 
67     bool           have_listen_thread;
68     QemuThread     listen_thread;
69     QemuSemaphore  listen_thread_sem;
70 
71     /* For the kernel to send us notifications */
72     int       userfault_fd;
73     /* To notify the fault_thread to wake, e.g., when need to quit */
74     int       userfault_event_fd;
75     QEMUFile *to_src_file;
76     QemuMutex rp_mutex;    /* We send replies from multiple threads */
77     /* RAMBlock of last request sent to source */
78     RAMBlock *last_rb;
79     void     *postcopy_tmp_page;
80     void     *postcopy_tmp_zero_page;
81     /* PostCopyFD's for external userfaultfds & handlers of shared memory */
82     GArray   *postcopy_remote_fds;
83 
84     QEMUBH *bh;
85 
86     int state;
87 
88     bool have_colo_incoming_thread;
89     QemuThread colo_incoming_thread;
90     /* The coroutine we should enter (back) after failover */
91     Coroutine *migration_incoming_co;
92     QemuSemaphore colo_incoming_sem;
93 
94     /*
95      * PostcopyBlocktimeContext to keep information for postcopy
96      * live migration, to calculate vCPU block time
97      * */
98     struct PostcopyBlocktimeContext *blocktime_ctx;
99 
100     /* notify PAUSED postcopy incoming migrations to try to continue */
101     bool postcopy_recover_triggered;
102     QemuSemaphore postcopy_pause_sem_dst;
103     QemuSemaphore postcopy_pause_sem_fault;
104 
105     /* List of listening socket addresses  */
106     SocketAddressList *socket_address_list;
107 };
108 
109 MigrationIncomingState *migration_incoming_get_current(void);
110 void migration_incoming_state_destroy(void);
111 /*
112  * Functions to work with blocktime context
113  */
114 void fill_destination_postcopy_migration_info(MigrationInfo *info);
115 
116 #define TYPE_MIGRATION "migration"
117 
118 typedef struct MigrationClass MigrationClass;
119 #define MIGRATION_OBJ_CLASS(klass) \
120     OBJECT_CLASS_CHECK(MigrationClass, (klass), TYPE_MIGRATION)
121 #define MIGRATION_OBJ(obj) \
122     OBJECT_CHECK(MigrationState, (obj), TYPE_MIGRATION)
123 #define MIGRATION_OBJ_GET_CLASS(obj) \
124     OBJECT_GET_CLASS(MigrationClass, (obj), TYPE_MIGRATION)
125 
126 struct MigrationClass {
127     /*< private >*/
128     DeviceClass parent_class;
129 };
130 
131 struct MigrationState
132 {
133     /*< private >*/
134     DeviceState parent_obj;
135 
136     /*< public >*/
137     QemuThread thread;
138     QEMUBH *cleanup_bh;
139     QEMUFile *to_dst_file;
140     /*
141      * Protects to_dst_file pointer.  We need to make sure we won't
142      * yield or hang during the critical section, since this lock will
143      * be used in OOB command handler.
144      */
145     QemuMutex qemu_file_lock;
146 
147     /*
148      * Used to allow urgent requests to override rate limiting.
149      */
150     QemuSemaphore rate_limit_sem;
151 
152     /* pages already send at the beginning of current iteration */
153     uint64_t iteration_initial_pages;
154 
155     /* pages transferred per second */
156     double pages_per_second;
157 
158     /* bytes already send at the beginning of current iteration */
159     uint64_t iteration_initial_bytes;
160     /* time at the start of current iteration */
161     int64_t iteration_start_time;
162     /*
163      * The final stage happens when the remaining data is smaller than
164      * this threshold; it's calculated from the requested downtime and
165      * measured bandwidth
166      */
167     int64_t threshold_size;
168 
169     /* params from 'migrate-set-parameters' */
170     MigrationParameters parameters;
171 
172     int state;
173 
174     /* State related to return path */
175     struct {
176         QEMUFile     *from_dst_file;
177         QemuThread    rp_thread;
178         bool          error;
179         QemuSemaphore rp_sem;
180     } rp_state;
181 
182     double mbps;
183     /* Timestamp when recent migration starts (ms) */
184     int64_t start_time;
185     /* Total time used by latest migration (ms) */
186     int64_t total_time;
187     /* Timestamp when VM is down (ms) to migrate the last stuff */
188     int64_t downtime_start;
189     int64_t downtime;
190     int64_t expected_downtime;
191     bool enabled_capabilities[MIGRATION_CAPABILITY__MAX];
192     int64_t setup_time;
193     /*
194      * Whether guest was running when we enter the completion stage.
195      * If migration is interrupted by any reason, we need to continue
196      * running the guest on source.
197      */
198     bool vm_was_running;
199 
200     /* Flag set once the migration has been asked to enter postcopy */
201     bool start_postcopy;
202     /* Flag set after postcopy has sent the device state */
203     bool postcopy_after_devices;
204 
205     /* Flag set once the migration thread is running (and needs joining) */
206     bool migration_thread_running;
207 
208     /* Flag set once the migration thread called bdrv_inactivate_all */
209     bool block_inactive;
210 
211     /* Migration is waiting for guest to unplug device */
212     QemuSemaphore wait_unplug_sem;
213 
214     /* Migration is paused due to pause-before-switchover */
215     QemuSemaphore pause_sem;
216 
217     /* The semaphore is used to notify COLO thread that failover is finished */
218     QemuSemaphore colo_exit_sem;
219 
220     /* The event is used to notify COLO thread to do checkpoint */
221     QemuEvent colo_checkpoint_event;
222     int64_t colo_checkpoint_time;
223     QEMUTimer *colo_delay_timer;
224 
225     /* The first error that has occurred.
226        We used the mutex to be able to return the 1st error message */
227     Error *error;
228     /* mutex to protect errp */
229     QemuMutex error_mutex;
230 
231     /* Do we have to clean up -b/-i from old migrate parameters */
232     /* This feature is deprecated and will be removed */
233     bool must_remove_block_options;
234 
235     /*
236      * Global switch on whether we need to store the global state
237      * during migration.
238      */
239     bool store_global_state;
240 
241     /* Whether we send QEMU_VM_CONFIGURATION during migration */
242     bool send_configuration;
243     /* Whether we send section footer during migration */
244     bool send_section_footer;
245 
246     /* Needed by postcopy-pause state */
247     QemuSemaphore postcopy_pause_sem;
248     QemuSemaphore postcopy_pause_rp_sem;
249     /*
250      * Whether we abort the migration if decompression errors are
251      * detected at the destination. It is left at false for qemu
252      * older than 3.0, since only newer qemu sends streams that
253      * do not trigger spurious decompression errors.
254      */
255     bool decompress_error_check;
256 
257     /*
258      * This decides the size of guest memory chunk that will be used
259      * to track dirty bitmap clearing.  The size of memory chunk will
260      * be GUEST_PAGE_SIZE << N.  Say, N=0 means we will clear dirty
261      * bitmap for each page to send (1<<0=1); N=10 means we will clear
262      * dirty bitmap only once for 1<<10=1K continuous guest pages
263      * (which is in 4M chunk).
264      */
265     uint8_t clear_bitmap_shift;
266 };
267 
268 void migrate_set_state(int *state, int old_state, int new_state);
269 
270 void migration_fd_process_incoming(QEMUFile *f, Error **errp);
271 void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp);
272 void migration_incoming_process(void);
273 
274 bool  migration_has_all_channels(void);
275 
276 uint64_t migrate_max_downtime(void);
277 
278 void migrate_set_error(MigrationState *s, const Error *error);
279 void migrate_fd_error(MigrationState *s, const Error *error);
280 
281 void migrate_fd_connect(MigrationState *s, Error *error_in);
282 
283 bool migration_is_setup_or_active(int state);
284 bool migration_is_running(int state);
285 
286 void migrate_init(MigrationState *s);
287 bool migration_is_blocked(Error **errp);
288 /* True if outgoing migration has entered postcopy phase */
289 bool migration_in_postcopy(void);
290 MigrationState *migrate_get_current(void);
291 
292 bool migrate_postcopy(void);
293 
294 bool migrate_release_ram(void);
295 bool migrate_postcopy_ram(void);
296 bool migrate_zero_blocks(void);
297 bool migrate_dirty_bitmaps(void);
298 bool migrate_ignore_shared(void);
299 bool migrate_validate_uuid(void);
300 
301 bool migrate_auto_converge(void);
302 bool migrate_use_multifd(void);
303 bool migrate_pause_before_switchover(void);
304 int migrate_multifd_channels(void);
305 MultiFDCompression migrate_multifd_compression(void);
306 int migrate_multifd_zlib_level(void);
307 int migrate_multifd_zstd_level(void);
308 
309 int migrate_use_xbzrle(void);
310 int64_t migrate_xbzrle_cache_size(void);
311 bool migrate_colo_enabled(void);
312 
313 bool migrate_use_block(void);
314 bool migrate_use_block_incremental(void);
315 int migrate_max_cpu_throttle(void);
316 bool migrate_use_return_path(void);
317 
318 uint64_t ram_get_total_transferred_pages(void);
319 
320 bool migrate_use_compression(void);
321 int migrate_compress_level(void);
322 int migrate_compress_threads(void);
323 int migrate_compress_wait_thread(void);
324 int migrate_decompress_threads(void);
325 bool migrate_use_events(void);
326 bool migrate_postcopy_blocktime(void);
327 
328 /* Sending on the return path - generic and then for each message type */
329 void migrate_send_rp_shut(MigrationIncomingState *mis,
330                           uint32_t value);
331 void migrate_send_rp_pong(MigrationIncomingState *mis,
332                           uint32_t value);
333 int migrate_send_rp_req_pages(MigrationIncomingState *mis, const char* rbname,
334                               ram_addr_t start, size_t len);
335 void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
336                                  char *block_name);
337 void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value);
338 
339 void dirty_bitmap_mig_before_vm_start(void);
340 void dirty_bitmap_mig_cancel_outgoing(void);
341 void dirty_bitmap_mig_cancel_incoming(void);
342 bool check_dirty_bitmap_mig_alias_map(const BitmapMigrationNodeAliasList *bbm,
343                                       Error **errp);
344 
345 void migrate_add_address(SocketAddress *address);
346 
347 int foreach_not_ignored_block(RAMBlockIterFunc func, void *opaque);
348 
349 #define qemu_ram_foreach_block \
350   #warning "Use foreach_not_ignored_block in migration code"
351 
352 void migration_make_urgent_request(void);
353 void migration_consume_urgent_request(void);
354 bool migration_rate_limit(void);
355 
356 #endif
357