blob: 64a7b3373546086d0d7046e74f55234c92750a36 [file] [log] [blame]
aliguori5bb79102008-10-13 03:12:02 +00001/*
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
aliguori376253e2009-03-05 23:01:23 +000017#include "qemu-common.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010018#include "qapi/qapi-types-migration.h"
Juan Quintela9848a402012-12-19 09:55:50 +010019#include "qemu/thread.h"
Michael R. Hines43487c62013-06-25 21:35:35 -040020#include "exec/cpu-common.h"
zhanghailiang25d0c162016-10-27 14:42:55 +080021#include "qemu/coroutine_int.h"
Peter Xue5cb7e72017-06-27 12:10:13 +080022#include "hw/qdev.h"
Juan Quintela4f0fae72017-07-24 12:42:02 +020023#include "io/channel.h"
aliguori376253e2009-03-05 23:01:23 +000024
Alexey Perevalov2a4c42f2018-03-22 21:17:23 +030025struct PostcopyBlocktimeContext;
26
Peter Xu13955b82018-05-02 18:47:30 +080027#define MIGRATION_RESUME_ACK_VALUE (1)
28
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010029/* State for the incoming migration */
30struct MigrationIncomingState {
Dr. David Alan Gilbert42e2aa52015-11-05 18:10:34 +000031 QEMUFile *from_src_file;
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +010032
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +000033 /*
34 * Free at the start of the main state load, set as the main thread finishes
35 * loading state.
36 */
37 QemuEvent main_thread_load_event;
38
Dr. David Alan Gilbert67f11b52017-02-24 18:28:34 +000039 size_t largest_page_size;
Dr. David Alan Gilbertc4faeed2015-11-05 18:11:17 +000040 bool have_fault_thread;
Dr. David Alan Gilbertf0a227a2015-11-05 18:11:04 +000041 QemuThread fault_thread;
42 QemuSemaphore fault_thread_sem;
Peter Xu64f615f2018-02-08 18:31:06 +080043 /* Set this when we want the fault thread to quit */
44 bool fault_thread_quit;
Dr. David Alan Gilbertf0a227a2015-11-05 18:11:04 +000045
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +000046 bool have_listen_thread;
47 QemuThread listen_thread;
48 QemuSemaphore listen_thread_sem;
49
Dr. David Alan Gilbert1caddf82015-11-05 18:11:03 +000050 /* For the kernel to send us notifications */
51 int userfault_fd;
Peter Xu64f615f2018-02-08 18:31:06 +080052 /* To notify the fault_thread to wake, e.g., when need to quit */
53 int userfault_event_fd;
Dr. David Alan Gilbert2e377012015-11-05 18:10:46 +000054 QEMUFile *to_src_file;
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +000055 QemuMutex rp_mutex; /* We send replies from multiple threads */
Dr. David Alan Gilbert096bf4c2018-03-12 17:21:12 +000056 /* RAMBlock of last request sent to source */
57 RAMBlock *last_rb;
Dr. David Alan Gilbert696ed9a2015-11-05 18:11:10 +000058 void *postcopy_tmp_page;
Dr. David Alan Gilbert41d84212017-02-24 18:28:36 +000059 void *postcopy_tmp_zero_page;
Dr. David Alan Gilbert00fa4fc2018-03-12 17:21:04 +000060 /* PostCopyFD's for external userfaultfds & handlers of shared memory */
61 GArray *postcopy_remote_fds;
Dr. David Alan Gilbert2e377012015-11-05 18:10:46 +000062
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +030063 QEMUBH *bh;
64
zhanghailiang93d7af62015-12-16 11:47:34 +000065 int state;
zhanghailiang25d0c162016-10-27 14:42:55 +080066
67 bool have_colo_incoming_thread;
68 QemuThread colo_incoming_thread;
69 /* The coroutine we should enter (back) after failover */
70 Coroutine *migration_incoming_co;
zhanghailiangc937b9a2017-01-17 20:57:43 +080071 QemuSemaphore colo_incoming_sem;
Alexey Perevalov2a4c42f2018-03-22 21:17:23 +030072
73 /*
74 * PostcopyBlocktimeContext to keep information for postcopy
75 * live migration, to calculate vCPU block time
76 * */
77 struct PostcopyBlocktimeContext *blocktime_ctx;
Peter Xub411b842018-05-02 18:47:20 +080078
79 /* notify PAUSED postcopy incoming migrations to try to continue */
Peter Xu02affd42018-05-02 18:47:36 +080080 bool postcopy_recover_triggered;
Peter Xub411b842018-05-02 18:47:20 +080081 QemuSemaphore postcopy_pause_sem_dst;
Peter Xu3a7804c2018-05-02 18:47:22 +080082 QemuSemaphore postcopy_pause_sem_fault;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010083};
84
85MigrationIncomingState *migration_incoming_get_current(void);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010086void migration_incoming_state_destroy(void);
Alexey Perevalov65ace062018-03-22 21:17:27 +030087/*
88 * Functions to work with blocktime context
89 */
90void fill_destination_postcopy_migration_info(MigrationInfo *info);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010091
Peter Xue5cb7e72017-06-27 12:10:13 +080092#define TYPE_MIGRATION "migration"
93
94#define MIGRATION_CLASS(klass) \
95 OBJECT_CLASS_CHECK(MigrationClass, (klass), TYPE_MIGRATION)
96#define MIGRATION_OBJ(obj) \
97 OBJECT_CHECK(MigrationState, (obj), TYPE_MIGRATION)
98#define MIGRATION_GET_CLASS(obj) \
99 OBJECT_GET_CLASS(MigrationClass, (obj), TYPE_MIGRATION)
100
101typedef struct MigrationClass {
102 /*< private >*/
103 DeviceClass parent_class;
104} MigrationClass;
105
Juan Quintela22f00a42010-05-11 15:56:35 +0200106struct MigrationState
aliguori065e2812008-11-11 16:46:33 +0000107{
Peter Xue5cb7e72017-06-27 12:10:13 +0800108 /*< private >*/
109 DeviceState parent_obj;
110
111 /*< public >*/
Juan Quintela9848a402012-12-19 09:55:50 +0100112 size_t bytes_xfer;
113 size_t xfer_limit;
Juan Quintela9848a402012-12-19 09:55:50 +0100114 QemuThread thread;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100115 QEMUBH *cleanup_bh;
zhanghailiang89a02a92016-01-15 11:37:42 +0800116 QEMUFile *to_dst_file;
Peter Xu62df0662018-05-02 18:47:38 +0800117 /*
118 * Protects to_dst_file pointer. We need to make sure we won't
119 * yield or hang during the critical section, since this lock will
120 * be used in OOB command handler.
121 */
122 QemuMutex qemu_file_lock;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100123
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +0100124 /*
125 * Used to allow urgent requests to override rate limiting.
126 */
127 QemuSemaphore rate_limit_sem;
128
Peter Xub15df1a2018-01-03 20:20:13 +0800129 /* bytes already send at the beggining of current interation */
130 uint64_t iteration_initial_bytes;
131 /* time at the start of current iteration */
132 int64_t iteration_start_time;
133 /*
134 * The final stage happens when the remaining data is smaller than
135 * this threshold; it's calculated from the requested downtime and
136 * measured bandwidth
137 */
138 int64_t threshold_size;
139
Juan Quintelaa0762d92017-04-05 21:00:09 +0200140 /* params from 'migrate-set-parameters' */
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100141 MigrationParameters parameters;
Paolo Bonzinif8bbc122013-02-22 17:36:41 +0100142
Paolo Bonzinif8bbc122013-02-22 17:36:41 +0100143 int state;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +0000144
145 /* State related to return path */
146 struct {
147 QEMUFile *from_dst_file;
148 QemuThread rp_thread;
149 bool error;
Peter Xuedd090c2018-05-02 18:47:32 +0800150 QemuSemaphore rp_sem;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +0000151 } rp_state;
152
Michael R. Hines7e114f82013-06-25 21:35:30 -0400153 double mbps;
Peter Xu4af246a2018-01-03 20:20:08 +0800154 /* Timestamp when recent migration starts (ms) */
155 int64_t start_time;
156 /* Total time used by latest migration (ms) */
Juan Quintelad5f8a572012-05-21 22:01:07 +0200157 int64_t total_time;
Peter Xu64909f92018-01-03 20:20:10 +0800158 /* Timestamp when VM is down (ms) to migrate the last stuff */
159 int64_t downtime_start;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200160 int64_t downtime;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200161 int64_t expected_downtime;
Eric Blake7fb1cf12015-11-18 01:52:57 -0700162 bool enabled_capabilities[MIGRATION_CAPABILITY__MAX];
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400163 int64_t setup_time;
Peter Xu7287cbd2018-01-03 20:20:09 +0800164 /*
165 * Whether guest was running when we enter the completion stage.
166 * If migration is interrupted by any reason, we need to continue
167 * running the guest on source.
168 */
169 bool vm_was_running;
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000170
171 /* Flag set once the migration has been asked to enter postcopy */
172 bool start_postcopy;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +0000173 /* Flag set after postcopy has sent the device state */
174 bool postcopy_after_devices;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +0000175
176 /* Flag set once the migration thread is running (and needs joining) */
177 bool migration_thread_running;
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +0000178
zhanghailiang1d2acc32017-01-24 15:59:52 +0800179 /* Flag set once the migration thread called bdrv_inactivate_all */
180 bool block_inactive;
181
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +0100182 /* Migration is paused due to pause-before-switchover */
183 QemuSemaphore pause_sem;
184
zhanghailiangc937b9a2017-01-17 20:57:43 +0800185 /* The semaphore is used to notify COLO thread that failover is finished */
186 QemuSemaphore colo_exit_sem;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100187
zhanghailiang479125d2017-01-17 20:57:42 +0800188 /* The semaphore is used to notify COLO thread to do checkpoint */
189 QemuSemaphore colo_checkpoint_sem;
190 int64_t colo_checkpoint_time;
191 QEMUTimer *colo_delay_timer;
192
Juan Quintela87db1a72017-09-05 12:50:22 +0200193 /* The first error that has occurred.
194 We used the mutex to be able to return the 1st error message */
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100195 Error *error;
Juan Quintela87db1a72017-09-05 12:50:22 +0200196 /* mutex to protect errp */
197 QemuMutex error_mutex;
198
Juan Quintela2833c592017-04-05 18:32:37 +0200199 /* Do we have to clean up -b/-i from old migrate parameters */
200 /* This feature is deprecated and will be removed */
201 bool must_remove_block_options;
Peter Xu52722982017-06-27 12:10:14 +0800202
203 /*
204 * Global switch on whether we need to store the global state
205 * during migration.
206 */
207 bool store_global_state;
Peter Xu3df663e2017-06-27 12:10:15 +0800208
209 /* Whether the VM is only allowing for migratable devices */
210 bool only_migratable;
Peter Xu71dd4c12017-06-27 12:10:16 +0800211
212 /* Whether we send QEMU_VM_CONFIGURATION during migration */
213 bool send_configuration;
Peter Xu15c38502017-06-27 12:10:17 +0800214 /* Whether we send section footer during migration */
215 bool send_section_footer;
Peter Xub23c2ad2018-05-02 18:47:19 +0800216
217 /* Needed by postcopy-pause state */
218 QemuSemaphore postcopy_pause_sem;
Peter Xu14b17422018-05-02 18:47:21 +0800219 QemuSemaphore postcopy_pause_rp_sem;
Xiao Guangrongf5482222018-05-03 16:06:11 +0800220 /*
221 * Whether we abort the migration if decompression errors are
222 * detected at the destination. It is left at false for qemu
223 * older than 3.0, since only newer qemu sends streams that
224 * do not trigger spurious decompression errors.
225 */
226 bool decompress_error_check;
aliguori065e2812008-11-11 16:46:33 +0000227};
228
zhanghailiang48781e52015-12-16 11:47:33 +0000229void migrate_set_state(int *state, int old_state, int new_state);
230
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100231void migration_fd_process_incoming(QEMUFile *f);
Juan Quintela4f0fae72017-07-24 12:42:02 +0200232void migration_ioc_process_incoming(QIOChannel *ioc);
Juan Quintela36c2f8b2018-03-07 08:40:52 +0100233void migration_incoming_process(void);
Juan Quintela511c0232010-06-09 14:10:55 +0200234
Juan Quintela428d8902017-07-24 13:06:25 +0200235bool migration_has_all_channels(void);
236
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400237uint64_t migrate_max_downtime(void);
238
Juan Quintela87db1a72017-09-05 12:50:22 +0200239void migrate_set_error(MigrationState *s, const Error *error);
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100240void migrate_fd_error(MigrationState *s, const Error *error);
aliguori065e2812008-11-11 16:46:33 +0000241
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +0000242void migrate_fd_connect(MigrationState *s, Error *error_in);
aliguori065e2812008-11-11 16:46:33 +0000243
Peter Xu3e0c8052018-02-08 18:31:15 +0800244void migrate_init(MigrationState *s);
Greg Kurz24f39022016-05-04 21:44:19 +0200245bool migration_is_blocked(Error **errp);
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000246/* True if outgoing migration has entered postcopy phase */
Juan Quintela57273092017-03-20 22:25:28 +0100247bool migration_in_postcopy(void);
Juan Quintela859bc752012-08-13 09:42:49 +0200248MigrationState *migrate_get_current(void);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100249
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +0300250bool migrate_postcopy(void);
251
Pavel Butsykin53f09a12017-02-03 18:23:20 +0300252bool migrate_release_ram(void);
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000253bool migrate_postcopy_ram(void);
Peter Lieven323004a2013-07-18 09:48:50 +0200254bool migrate_zero_blocks(void);
Vladimir Sementsov-Ogievskiy55efc8c2018-03-13 15:34:00 -0400255bool migrate_dirty_bitmaps(void);
Michael R. Hines60d92222013-06-25 21:35:36 -0400256
Chegu Vinodbde1e2e2013-06-24 03:49:42 -0600257bool migrate_auto_converge(void);
Juan Quintela30126bb2016-01-14 12:23:00 +0100258bool migrate_use_multifd(void);
Dr. David Alan Gilbert93fbd032017-10-20 10:05:50 +0100259bool migrate_pause_before_switchover(void);
Juan Quintela4075fb12016-01-15 08:56:17 +0100260int migrate_multifd_channels(void);
Juan Quintela0fb86602017-04-27 10:48:25 +0200261int migrate_multifd_page_count(void);
Chegu Vinodbde1e2e2013-06-24 03:49:42 -0600262
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300263int migrate_use_xbzrle(void);
264int64_t migrate_xbzrle_cache_size(void);
zhanghailiang35a6ed42016-10-27 14:42:52 +0800265bool migrate_colo_enabled(void);
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300266
Juan Quintela2833c592017-04-05 18:32:37 +0200267bool migrate_use_block(void);
268bool migrate_use_block_incremental(void);
Peter Xuc788ada2017-06-26 18:28:55 +0800269bool migrate_use_return_path(void);
Juan Quintela2833c592017-04-05 18:32:37 +0200270
Liang Li8706d2d2015-03-23 16:32:17 +0800271bool migrate_use_compression(void);
272int migrate_compress_level(void);
273int migrate_compress_threads(void);
Liang Li3fcb38c2015-03-23 16:32:18 +0800274int migrate_decompress_threads(void);
Juan Quintelab05dc722015-07-07 14:44:05 +0200275bool migrate_use_events(void);
Alexey Perevalovf22f9282018-03-22 21:17:22 +0300276bool migrate_postcopy_blocktime(void);
Liang Li8706d2d2015-03-23 16:32:17 +0800277
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000278/* Sending on the return path - generic and then for each message type */
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000279void migrate_send_rp_shut(MigrationIncomingState *mis,
280 uint32_t value);
281void migrate_send_rp_pong(MigrationIncomingState *mis,
282 uint32_t value);
Peter Xud6208e32018-02-08 18:31:12 +0800283int migrate_send_rp_req_pages(MigrationIncomingState *mis, const char* rbname,
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000284 ram_addr_t start, size_t len);
Peter Xua335deb2018-05-02 18:47:28 +0800285void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
286 char *block_name);
Peter Xu13955b82018-05-02 18:47:30 +0800287void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value);
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000288
Vladimir Sementsov-Ogievskiyb35ebdf2018-03-13 15:34:01 -0400289void dirty_bitmap_mig_before_vm_start(void);
290void init_dirty_bitmap_incoming_migration(void);
291
Dr. David Alan Gilbert343f6322018-06-05 17:25:45 +0100292#define qemu_ram_foreach_block \
293 #warning "Use qemu_ram_foreach_block_migratable in migration code"
294
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +0100295void migration_make_urgent_request(void);
296void migration_consume_urgent_request(void);
297
aliguori5bb79102008-10-13 03:12:02 +0000298#endif