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