]> git.proxmox.com Git - mirror_qemu.git/blame - migration/migration.h
Merge tag 'pull-request-2023-12-20' of https://gitlab.com/thuth/qemu into staging
[mirror_qemu.git] / migration / migration.h
CommitLineData
5bb7910a
AL
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
d4842052 17#include "exec/cpu-common.h"
a27bd6c7 18#include "hw/qdev-core.h"
9af23989 19#include "qapi/qapi-types-migration.h"
e3bf5e68 20#include "qapi/qmp/json-writer.h"
9848a404 21#include "qemu/thread.h"
25d0c16f 22#include "qemu/coroutine_int.h"
4f0fae7f 23#include "io/channel.h"
8518278a 24#include "io/channel-buffer.h"
7659505c 25#include "net/announce.h"
db1015e9 26#include "qom/object.h"
36f62f11 27#include "postcopy-ram.h"
f4584076 28#include "sysemu/runstate.h"
376253ec 29
2a4c42f1
AP
30struct PostcopyBlocktimeContext;
31
13955b89
PX
32#define MIGRATION_RESUME_ACK_VALUE (1)
33
002cad6b
PX
34/*
35 * 1<<6=64 pages -> 256K chunk when page size is 4K. This gives us
36 * the benefit that all the chunks are 64 pages aligned then the
37 * bitmaps are always aligned to LONG.
38 */
39#define CLEAR_BITMAP_SHIFT_MIN 6
40/*
41 * 1<<18=256K pages -> 1G chunk when page size is 4K. This is the
42 * default value to use if no one specified.
43 */
44#define CLEAR_BITMAP_SHIFT_DEFAULT 18
45/*
46 * 1<<31=2G pages -> 8T chunk when page size is 4K. This should be
47 * big enough and make sure we won't overflow easily.
48 */
49#define CLEAR_BITMAP_SHIFT_MAX 31
50
77dadc3f
PX
51/* This is an abstraction of a "temp huge page" for postcopy's purpose */
52typedef struct {
53 /*
54 * This points to a temporary huge page as a buffer for UFFDIO_COPY. It's
55 * mmap()ed and needs to be freed when cleanup.
56 */
57 void *tmp_huge_page;
58 /*
59 * This points to the host page we're going to install for this temp page.
60 * It tells us after we've received the whole page, where we should put it.
61 */
62 void *host_addr;
63 /* Number of small pages copied (in size of TARGET_PAGE_SIZE) */
64 unsigned int target_pages;
65 /* Whether this page contains all zeros */
66 bool all_zero;
67} PostcopyTmpPage;
68
6621883f
PX
69typedef enum {
70 PREEMPT_THREAD_NONE = 0,
71 PREEMPT_THREAD_CREATED,
72 PREEMPT_THREAD_QUIT,
73} PreemptThreadStatus;
74
bca7856a
DDAG
75/* State for the incoming migration */
76struct MigrationIncomingState {
42e2aa56 77 QEMUFile *from_src_file;
755e8d7c 78 /* Previously received RAM's RAMBlock pointer */
c01b16ed 79 RAMBlock *last_recv_block[RAM_CHANNEL_MAX];
1df6ddb4
DDAG
80 /* A hook to allow cleanup at the end of incoming migration */
81 void *transport_data;
82 void (*transport_cleanup)(void *data);
095c12a4
PX
83 /*
84 * Used to sync thread creations. Note that we can't create threads in
85 * parallel with this sem.
86 */
87 QemuSemaphore thread_sync_sem;
7b89bf27
DDAG
88 /*
89 * Free at the start of the main state load, set as the main thread finishes
90 * loading state.
91 */
92 QemuEvent main_thread_load_event;
93
7659505c
DDAG
94 /* For network announces */
95 AnnounceTimer announce_timer;
96
67f11b5c 97 size_t largest_page_size;
c4faeed2 98 bool have_fault_thread;
f0a227ad 99 QemuThread fault_thread;
64f615fe
PX
100 /* Set this when we want the fault thread to quit */
101 bool fault_thread_quit;
f0a227ad 102
c76201ab
DDAG
103 bool have_listen_thread;
104 QemuThread listen_thread;
c76201ab 105
1caddf8a
DDAG
106 /* For the kernel to send us notifications */
107 int userfault_fd;
64f615fe
PX
108 /* To notify the fault_thread to wake, e.g., when need to quit */
109 int userfault_event_fd;
2e37701e 110 QEMUFile *to_src_file;
6decec93 111 QemuMutex rp_mutex; /* We send replies from multiple threads */
096bf4c8
DDAG
112 /* RAMBlock of last request sent to source */
113 RAMBlock *last_rb;
77dadc3f
PX
114 /*
115 * Number of postcopy channels including the default precopy channel, so
116 * vanilla postcopy will only contain one channel which contain both
117 * precopy and postcopy streams.
118 *
119 * This is calculated when the src requests to enable postcopy but before
120 * it starts. Its value can depend on e.g. whether postcopy preemption is
121 * enabled.
122 */
123 unsigned int postcopy_channels;
36f62f11
PX
124 /* QEMUFile for postcopy only; it'll be handled by a separate thread */
125 QEMUFile *postcopy_qemufile_dst;
5655aab0
PX
126 /*
127 * When postcopy_qemufile_dst is properly setup, this sem is posted.
128 * One can wait on this semaphore to wait until the preempt channel is
129 * properly setup.
130 */
131 QemuSemaphore postcopy_qemufile_dst_done;
36f62f11
PX
132 /* Postcopy priority thread is used to receive postcopy requested pages */
133 QemuThread postcopy_prio_thread;
6621883f
PX
134 /*
135 * Always set by the main vm load thread only, but can be read by the
136 * postcopy preempt thread. "volatile" makes sure all reads will be
d8b71d96 137 * up-to-date across cores.
6621883f
PX
138 */
139 volatile PreemptThreadStatus preempt_thread_status;
60bb3c58
PX
140 /*
141 * Used to sync between the ram load main thread and the fast ram load
142 * thread. It protects postcopy_qemufile_dst, which is the postcopy
143 * fast channel.
144 *
145 * The ram fast load thread will take it mostly for the whole lifecycle
146 * because it needs to continuously read data from the channel, and
147 * it'll only release this mutex if postcopy is interrupted, so that
148 * the ram load main thread will take this mutex over and properly
149 * release the broken channel.
150 */
151 QemuMutex postcopy_prio_thread_mutex;
77dadc3f
PX
152 /*
153 * An array of temp host huge pages to be used, one for each postcopy
154 * channel.
155 */
156 PostcopyTmpPage *postcopy_tmp_pages;
157 /* This is shared for all postcopy channels */
41d84210 158 void *postcopy_tmp_zero_page;
00fa4fc8
DDAG
159 /* PostCopyFD's for external userfaultfds & handlers of shared memory */
160 GArray *postcopy_remote_fds;
2e37701e 161
0aa6aefc
DL
162 QEMUBH *bh;
163
93d7af6f 164 int state;
25d0c16f 165
dd42ce24
VSO
166 /*
167 * The incoming migration coroutine, non-NULL during qemu_loadvm_state().
168 * Used to wake the migration incoming coroutine from rdma code. How much is
169 * it safe - it's a question.
170 */
171 Coroutine *loadvm_co;
172
25d0c16f 173 /* The coroutine we should enter (back) after failover */
dd42ce24 174 Coroutine *colo_incoming_co;
c937b9a6 175 QemuSemaphore colo_incoming_sem;
2a4c42f1
AP
176
177 /*
178 * PostcopyBlocktimeContext to keep information for postcopy
179 * live migration, to calculate vCPU block time
180 * */
181 struct PostcopyBlocktimeContext *blocktime_ctx;
b411b844
PX
182
183 /* notify PAUSED postcopy incoming migrations to try to continue */
184 QemuSemaphore postcopy_pause_sem_dst;
3a7804c3 185 QemuSemaphore postcopy_pause_sem_fault;
60bb3c58
PX
186 /*
187 * This semaphore is used to allow the ram fast load thread (only when
188 * postcopy preempt is enabled) fall into sleep when there's network
189 * interruption detected. When the recovery is done, the main load
190 * thread will kick the fast ram load thread using this semaphore.
191 */
192 QemuSemaphore postcopy_pause_sem_fast_load;
9aca82ba
JQ
193
194 /* List of listening socket addresses */
195 SocketAddressList *socket_address_list;
8f8bfffc
PX
196
197 /* A tree of pages that we requested to the source VM */
198 GTree *page_requested;
cf02f29e
PX
199 /*
200 * For postcopy only, count the number of requested page faults that
201 * still haven't been resolved.
202 */
8f8bfffc
PX
203 int page_requested_count;
204 /*
205 * The mutex helps to maintain the requested pages that we sent to the
206 * source, IOW, to guarantee coherent between the page_requests tree and
207 * the per-ramblock receivedmap. Note! This does not guarantee consistency
208 * of the real page copy procedures (using UFFDIO_[ZERO]COPY). E.g., even
209 * if one bit in receivedmap is cleared, UFFDIO_COPY could have happened
210 * for that page already. This is intended so that the mutex won't
211 * serialize and blocked by slow operations like UFFDIO_* ioctls. However
212 * this should be enough to make sure the page_requested tree always
213 * contains valid information.
214 */
215 QemuMutex page_request_mutex;
cf02f29e
PX
216 /*
217 * If postcopy preempt is enabled, there is a chance that the main
218 * thread finished loading its data before the preempt channel has
219 * finished loading the urgent pages. If that happens, the two threads
220 * will use this condvar to synchronize, so the main thread will always
221 * wait until all pages received.
222 */
223 QemuCond page_request_cond;
1b4adb10
AH
224
225 /*
226 * Number of devices that have yet to approve switchover. When this reaches
227 * zero an ACK that it's OK to do switchover is sent to the source. No lock
228 * is needed as this field is updated serially.
229 */
230 unsigned int switchover_ack_pending_num;
bca7856a
DDAG
231};
232
233MigrationIncomingState *migration_incoming_get_current(void);
bca7856a 234void migration_incoming_state_destroy(void);
e031149c 235void migration_incoming_transport_cleanup(MigrationIncomingState *mis);
65ace060
AP
236/*
237 * Functions to work with blocktime context
238 */
239void fill_destination_postcopy_migration_info(MigrationInfo *info);
bca7856a 240
e5cb7e76
PX
241#define TYPE_MIGRATION "migration"
242
db1015e9 243typedef struct MigrationClass MigrationClass;
8110fa1d
EH
244DECLARE_OBJ_CHECKERS(MigrationState, MigrationClass,
245 MIGRATION_OBJ, TYPE_MIGRATION)
e5cb7e76 246
db1015e9 247struct MigrationClass {
e5cb7e76
PX
248 /*< private >*/
249 DeviceClass parent_class;
db1015e9 250};
e5cb7e76 251
f16aee44 252struct MigrationState {
e5cb7e76
PX
253 /*< private >*/
254 DeviceState parent_obj;
255
256 /*< public >*/
9848a404 257 QemuThread thread;
8518278a 258 QEMUBH *vm_start_bh;
bb1fadc4 259 QEMUBH *cleanup_bh;
43044ac0 260 /* Protected by qemu_file_lock */
89a02a9f 261 QEMUFile *to_dst_file;
36f62f11
PX
262 /* Postcopy specific transfer channel */
263 QEMUFile *postcopy_qemufile_src;
d0edb8a1
PX
264 /*
265 * It is posted when the preempt channel is established. Note: this is
266 * used for both the start or recover of a postcopy migration. We'll
267 * post to this sem every time a new preempt channel is created in the
268 * main thread, and we keep post() and wait() in pair.
269 */
270 QemuSemaphore postcopy_qemufile_src_sem;
8518278a 271 QIOChannelBuffer *bioc;
62df066f 272 /*
43044ac0
PX
273 * Protects to_dst_file/from_dst_file pointers. We need to make sure we
274 * won't yield or hang during the critical section, since this lock will be
275 * used in OOB command handler.
62df066f
PX
276 */
277 QemuMutex qemu_file_lock;
2594f56d 278
ad767bed
DDAG
279 /*
280 * Used to allow urgent requests to override rate limiting.
281 */
282 QemuSemaphore rate_limit_sem;
283
aecbfe9c
XG
284 /* pages already send at the beginning of current iteration */
285 uint64_t iteration_initial_pages;
286
287 /* pages transferred per second */
288 double pages_per_second;
289
290 /* bytes already send at the beginning of current iteration */
b15df1ae
PX
291 uint64_t iteration_initial_bytes;
292 /* time at the start of current iteration */
293 int64_t iteration_start_time;
294 /*
295 * The final stage happens when the remaining data is smaller than
296 * this threshold; it's calculated from the requested downtime and
8b239597 297 * measured bandwidth, or avail-switchover-bandwidth if specified.
b15df1ae
PX
298 */
299 int64_t threshold_size;
300
a0762d9e 301 /* params from 'migrate-set-parameters' */
2594f56d 302 MigrationParameters parameters;
f8bbc128 303
f8bbc128 304 int state;
70b20477
DDAG
305
306 /* State related to return path */
307 struct {
43044ac0 308 /* Protected by qemu_file_lock */
70b20477
DDAG
309 QEMUFile *from_dst_file;
310 QemuThread rp_thread;
53021ea1
PX
311 /*
312 * We can also check non-zero of rp_thread, but there's no "official"
313 * way to do this, so this bool makes it slightly more elegant.
314 * Checking from_dst_file for this is racy because from_dst_file will
315 * be cleared in the rp_thread!
316 */
317 bool rp_thread_created;
5e79a4bf
PX
318 /*
319 * Used to synchronize between migration main thread and return
320 * path thread. The migration thread can wait() on this sem, while
321 * other threads (e.g., return path thread) can kick it using a
322 * post().
323 */
edd090c7 324 QemuSemaphore rp_sem;
b28fb582
PX
325 /*
326 * We post to this when we got one PONG from dest. So far it's an
327 * easy way to know the main channel has successfully established
328 * on dest QEMU.
329 */
330 QemuSemaphore rp_pong_acks;
70b20477
DDAG
331 } rp_state;
332
7e114f8c 333 double mbps;
4af246a3
PX
334 /* Timestamp when recent migration starts (ms) */
335 int64_t start_time;
336 /* Total time used by latest migration (ms) */
d5f8a570 337 int64_t total_time;
64909f97
PX
338 /* Timestamp when VM is down (ms) to migrate the last stuff */
339 int64_t downtime_start;
9c5a9fcf 340 int64_t downtime;
2c52ddf1 341 int64_t expected_downtime;
0cec2056 342 bool capabilities[MIGRATION_CAPABILITY__MAX];
ed4fbd10 343 int64_t setup_time;
f4584076 344
7287cbd4 345 /*
f4584076 346 * State before stopping the vm by vm_stop_force_state().
7287cbd4 347 * If migration is interrupted by any reason, we need to continue
f4584076
VSO
348 * running the guest on source if it was running or restore its stopped
349 * state.
7287cbd4 350 */
f4584076 351 RunState vm_old_state;
4886a1bc
DDAG
352
353 /* Flag set once the migration has been asked to enter postcopy */
354 bool start_postcopy;
b82fc321
DDAG
355 /* Flag set after postcopy has sent the device state */
356 bool postcopy_after_devices;
1d34e4bf
DDAG
357
358 /* Flag set once the migration thread is running (and needs joining) */
359 bool migration_thread_running;
6c595cde 360
1d2acc31
HZ
361 /* Flag set once the migration thread called bdrv_inactivate_all */
362 bool block_inactive;
363
c7e0acd5
JF
364 /* Migration is waiting for guest to unplug device */
365 QemuSemaphore wait_unplug_sem;
366
e91d8951
DDAG
367 /* Migration is paused due to pause-before-switchover */
368 QemuSemaphore pause_sem;
369
c937b9a6
HZ
370 /* The semaphore is used to notify COLO thread that failover is finished */
371 QemuSemaphore colo_exit_sem;
d59ce6f3 372
bb70b66e
LS
373 /* The event is used to notify COLO thread to do checkpoint */
374 QemuEvent colo_checkpoint_event;
479125d5
HZ
375 int64_t colo_checkpoint_time;
376 QEMUTimer *colo_delay_timer;
377
87db1a7d
JQ
378 /* The first error that has occurred.
379 We used the mutex to be able to return the 1st error message */
d59ce6f3 380 Error *error;
87db1a7d
JQ
381 /* mutex to protect errp */
382 QemuMutex error_mutex;
383
2833c59b
JQ
384 /* Do we have to clean up -b/-i from old migrate parameters */
385 /* This feature is deprecated and will be removed */
386 bool must_remove_block_options;
5272298c
PX
387
388 /*
389 * Global switch on whether we need to store the global state
390 * during migration.
391 */
392 bool store_global_state;
3df663e5 393
71dd4c1a
PX
394 /* Whether we send QEMU_VM_CONFIGURATION during migration */
395 bool send_configuration;
15c38503
PX
396 /* Whether we send section footer during migration */
397 bool send_section_footer;
b23c2ade
PX
398
399 /* Needed by postcopy-pause state */
400 QemuSemaphore postcopy_pause_sem;
f548222c
XG
401 /*
402 * Whether we abort the migration if decompression errors are
403 * detected at the destination. It is left at false for qemu
404 * older than 3.0, since only newer qemu sends streams that
405 * do not trigger spurious decompression errors.
406 */
407 bool decompress_error_check;
6621883f
PX
408 /*
409 * This variable only affects behavior when postcopy preempt mode is
410 * enabled.
411 *
412 * When set:
413 *
414 * - postcopy preempt src QEMU instance will generate an EOS message at
415 * the end of migration to shut the preempt channel on dest side.
416 *
06064a67
PX
417 * - postcopy preempt channel will be created at the setup phase on src
418 QEMU.
419 *
6621883f
PX
420 * When clear:
421 *
422 * - postcopy preempt src QEMU instance will _not_ generate an EOS
423 * message at the end of migration, the dest qemu will shutdown the
424 * channel itself.
425 *
06064a67 426 * - postcopy preempt channel will be created at the switching phase
d8b71d96 427 * from precopy -> postcopy (to avoid race condition of misordered
06064a67
PX
428 * creation of channels).
429 *
6621883f
PX
430 * NOTE: See message-id <ZBoShWArKDPpX/D7@work-vm> on qemu-devel
431 * mailing list for more information on the possible race. Everyone
432 * should probably just keep this value untouched after set by the
433 * machine type (or the default).
434 */
435 bool preempt_pre_7_2;
002cad6b 436
77c259a4
JQ
437 /*
438 * flush every channel after each section sent.
439 *
440 * This assures that we can't mix pages from one iteration through
441 * ram pages with pages for the following iteration. We really
442 * only need to do this flush after we have go through all the
443 * dirty pages. For historical reasons, we do that after each
444 * section. This is suboptimal (we flush too many times).
294e5a40 445 * Default value is false. (since 8.1)
77c259a4
JQ
446 */
447 bool multifd_flush_after_each_section;
002cad6b
PX
448 /*
449 * This decides the size of guest memory chunk that will be used
450 * to track dirty bitmap clearing. The size of memory chunk will
451 * be GUEST_PAGE_SIZE << N. Say, N=0 means we will clear dirty
452 * bitmap for each page to send (1<<0=1); N=10 means we will clear
453 * dirty bitmap only once for 1<<10=1K continuous guest pages
454 * (which is in 4M chunk).
455 */
456 uint8_t clear_bitmap_shift;
d8053e73
CZ
457
458 /*
459 * This save hostname when out-going migration starts
460 */
461 char *hostname;
e3bf5e68
DH
462
463 /* QEMU_VM_VMDESCRIPTION content filled for all non-iterable devices. */
464 JSONWriter *vmdesc;
1b4adb10
AH
465
466 /*
467 * Indicates whether an ACK from the destination that it's OK to do
468 * switchover has been received.
469 */
470 bool switchover_acked;
27fd25b0
JQ
471 /* Is this a rdma migration */
472 bool rdma_migration;
065e2813
AL
473};
474
48781e5b
HZ
475void migrate_set_state(int *state, int old_state, int new_state);
476
b673eab4 477void migration_fd_process_incoming(QEMUFile *f, Error **errp);
49ed0d24 478void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp);
36c2f8be 479void migration_incoming_process(void);
511c0231 480
428d8908
JQ
481bool migration_has_all_channels(void);
482
a0a3fd60
GC
483uint64_t migrate_max_downtime(void);
484
87db1a7d 485void migrate_set_error(MigrationState *s, const Error *error);
2b2f6f41 486bool migrate_has_error(MigrationState *s);
065e2813 487
cce8040b 488void migrate_fd_connect(MigrationState *s, Error *error_in);
065e2813 489
3d63da16 490bool migration_is_setup_or_active(int state);
392d87e2 491bool migration_is_running(int state);
3d63da16 492
08fc4cb5 493int migrate_init(MigrationState *s, Error **errp);
24f3902b 494bool migration_is_blocked(Error **errp);
9ec055ae 495/* True if outgoing migration has entered postcopy phase */
5727309d 496bool migration_in_postcopy(void);
f8c543e8 497bool migration_postcopy_is_alive(int state);
859bc756 498MigrationState *migrate_get_current(void);
99a0db9b 499
aecbfe9c
XG
500uint64_t ram_get_total_transferred_pages(void);
501
6decec93 502/* Sending on the return path - generic and then for each message type */
6decec93
DDAG
503void migrate_send_rp_shut(MigrationIncomingState *mis,
504 uint32_t value);
505void migrate_send_rp_pong(MigrationIncomingState *mis,
506 uint32_t value);
2e2bce16 507int migrate_send_rp_req_pages(MigrationIncomingState *mis, RAMBlock *rb,
8f8bfffc 508 ram_addr_t start, uint64_t haddr);
7a267fc4
PX
509int migrate_send_rp_message_req_pages(MigrationIncomingState *mis,
510 RAMBlock *rb, ram_addr_t start);
a335debb
PX
511void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
512 char *block_name);
13955b89 513void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value);
1b4adb10 514int migrate_send_rp_switchover_ack(MigrationIncomingState *mis);
6decec93 515
b35ebdf0 516void dirty_bitmap_mig_before_vm_start(void);
1499ab09
VSO
517void dirty_bitmap_mig_cancel_outgoing(void);
518void dirty_bitmap_mig_cancel_incoming(void);
31e4c354
HR
519bool check_dirty_bitmap_mig_alias_map(const BitmapMigrationNodeAliasList *bbm,
520 Error **errp);
521
9aca82ba 522void migrate_add_address(SocketAddress *address);
967f2de5
HG
523bool migrate_uri_parse(const char *uri, MigrationChannel **channel,
524 Error **errp);
fbd162e6
YK
525int foreach_not_ignored_block(RAMBlockIterFunc func, void *opaque);
526
343f632c 527#define qemu_ram_foreach_block \
fbd162e6 528 #warning "Use foreach_not_ignored_block in migration code"
343f632c 529
ad767bed
DDAG
530void migration_make_urgent_request(void);
531void migration_consume_urgent_request(void);
97e1e067 532bool migration_rate_limit(void);
458fecca 533void migration_cancel(const Error *error);
ad767bed 534
38c482b4
AH
535void migration_populate_vfio_info(MigrationInfo *info);
536void migration_reset_vfio_bytes_transferred(void);
77dadc3f 537void postcopy_temp_page_reset(PostcopyTmpPage *tmp_page);
43bd0bf3 538
f8c543e8
PX
539/*
540 * Migration thread waiting for return path thread. Return non-zero if an
541 * error is detected.
542 */
543int migration_rp_wait(MigrationState *s);
5e79a4bf
PX
544/*
545 * Kick the migration thread waiting for return path messages. NOTE: the
546 * name can be slightly confusing (when read as "kick the rp thread"), just
547 * to remember the target is always the migration thread.
548 */
549void migration_rp_kick(MigrationState *s);
550
93bdf888
PX
551int migration_stop_vm(RunState state);
552
5bb7910a 553#endif