blob: 041b8451a6303a251e3d14bdd6bd1cedf3313429 [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 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010012 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
aliguori5bb79102008-10-13 03:12:02 +000014 */
15
Peter Maydell1393a482016-01-26 18:16:54 +000016#include "qemu/osdep.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020017#include "qemu/cutils.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010018#include "qemu/error-report.h"
Markus Armbrusterdb725812019-08-12 07:23:50 +020019#include "qemu/main-loop.h"
Juan Quintela795c40b2017-04-06 12:00:28 +020020#include "migration/blocker.h"
Juan Quintelaf4dbe1b2017-04-05 15:54:10 +020021#include "exec.h"
Juan Quintela7fcac4a2017-04-05 15:58:29 +020022#include "fd.h"
Juan Quintela61e8b142017-04-05 17:40:11 +020023#include "socket.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020024#include "sysemu/runstate.h"
Markus Armbruster46517dd2019-08-12 07:23:57 +020025#include "sysemu/sysemu.h"
Claudio Fontanab0c3cf92020-06-29 11:35:03 +020026#include "sysemu/cpu-throttle.h"
Juan Quintelae1a3ece2017-04-17 20:32:36 +020027#include "rdma.h"
Juan Quintela7b1e1a22017-04-17 20:26:27 +020028#include "ram.h"
Juan Quintela84a899d2017-04-24 18:53:30 +020029#include "migration/global_state.h"
Juan Quintelac4b63b72017-04-24 19:02:44 +020030#include "migration/misc.h"
Juan Quintela6666c962017-04-24 20:07:27 +020031#include "migration.h"
Juan Quintela20a519a2017-04-20 14:48:46 +020032#include "savevm.h"
Juan Quintela40014d82017-04-17 19:34:36 +020033#include "qemu-file-channel.h"
Juan Quintela08a0aee2017-04-20 18:52:18 +020034#include "qemu-file.h"
Juan Quintela987772d2017-04-17 19:02:59 +020035#include "migration/vmstate.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010036#include "block/block.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010037#include "qapi/error.h"
Juan Quintela9aca82b2019-02-27 11:51:27 +010038#include "qapi/clone-visitor.h"
Max Reitz31e4c352020-08-20 17:07:23 +020039#include "qapi/qapi-visit-migration.h"
Juan Quintela9aca82b2019-02-27 11:51:27 +010040#include "qapi/qapi-visit-sockets.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010041#include "qapi/qapi-commands-migration.h"
42#include "qapi/qapi-events-migration.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010043#include "qapi/qmp/qerror.h"
Markus Armbruster15280c32018-02-01 12:18:36 +010044#include "qapi/qmp/qnull.h"
Paolo Bonziniab28bd22015-07-09 08:55:38 +020045#include "qemu/rcu.h"
Juan Quintela2c9e6fe2017-04-21 14:31:22 +020046#include "block.h"
Juan Quintelabe07b0a2017-04-20 13:12:24 +020047#include "postcopy-ram.h"
Juan Quintela766bd172012-07-23 05:45:29 +020048#include "qemu/thread.h"
Kazuya Saitoc09e5bb2013-02-22 17:36:19 +010049#include "trace.h"
Juan Quintela51180422017-04-24 20:50:19 +020050#include "exec/target_page.h"
Daniel P. Berrange61b67d42016-04-27 11:05:01 +010051#include "io/channel-buffer.h"
zhanghailiang35a6ed42016-10-27 14:42:52 +080052#include "migration/colo.h"
Peter Xu4ffdb332017-06-27 12:10:18 +080053#include "hw/boards.h"
Markus Armbrustera27bd6c2019-08-12 07:23:51 +020054#include "hw/qdev-properties.h"
Eduardo Habkostce35e222020-12-11 17:05:12 -050055#include "hw/qdev-properties-system.h"
Peter Xu9d18af92017-06-27 12:10:19 +080056#include "monitor/monitor.h"
Dr. David Alan Gilbert50510ea2019-02-27 13:24:05 +000057#include "net/announce.h"
Jens Freimannc7e0acd2019-10-29 12:49:02 +010058#include "qemu/queue.h"
Juan Quintelad32ca5a2020-01-22 16:16:07 +010059#include "multifd.h"
Lukas Straubb5eea992020-12-28 16:08:52 +010060#include "qemu/yank.h"
Andrey Gruzdev6e8c25b2021-01-29 13:14:03 +030061#include "sysemu/cpus.h"
Peter Xu39675ff2021-07-22 13:58:41 -040062#include "yank_functions.h"
aliguori065e2812008-11-11 16:46:33 +000063
Laurent Vivier7590a2a2020-09-21 16:49:57 +020064#define MAX_THROTTLE (128 << 20) /* Migration transfer speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000065
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010066/* Amount of time to allocate to each "chunk" of bandwidth-throttled
67 * data. */
68#define BUFFER_DELAY 100
69#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
70
Ashijeet Acharya2ff30252016-09-15 21:50:28 +053071/* Time in milliseconds we are allowed to stop the source,
72 * for sending the last part */
73#define DEFAULT_MIGRATE_SET_DOWNTIME 300
74
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -030075/* Maximum migrate downtime set to 2000 seconds */
76#define MAX_MIGRATE_DOWNTIME_SECONDS 2000
77#define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
78
Liang Li8706d2d2015-03-23 16:32:17 +080079/* Default compression thread count */
80#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080081/* Default decompression thread count, usually decompression is at
82 * least 4 times as fast as compression.*/
83#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080084/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
85#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
Jason J. Herne1626fee2015-09-08 13:12:34 -040086/* Define default autoconverge cpu throttle migration parameters */
Keqian Zhudc14a472020-02-24 10:31:42 +080087#define DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD 50
Jason J. Herned85a31d2016-04-21 14:07:18 -040088#define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
89#define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
Li Qiang4cbc9c72018-08-01 06:00:20 -070090#define DEFAULT_MIGRATE_MAX_CPU_THROTTLE 99
Liang Li8706d2d2015-03-23 16:32:17 +080091
Orit Wasserman17ad9b32012-08-06 21:42:53 +030092/* Migration XBZRLE default cache size */
Juan Quintela73af8dd2017-10-05 21:30:10 +020093#define DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE (64 * 1024 * 1024)
Orit Wasserman17ad9b32012-08-06 21:42:53 +030094
Zhang Chen131b2152018-09-03 12:38:45 +080095/* The delay time (in ms) between two COLO checkpoints */
96#define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY (200 * 100)
Juan Quintela4075fb12016-01-15 08:56:17 +010097#define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2
Juan Quintela96eef042019-01-16 10:35:55 +010098#define DEFAULT_MIGRATE_MULTIFD_COMPRESSION MULTIFD_COMPRESSION_NONE
Juan Quintela9004db42020-01-23 17:08:52 +010099/* 0: means nocompress, 1: best speed, ... 9: best compress ratio */
100#define DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL 1
Juan Quintela6a9ad152020-01-23 17:41:36 +0100101/* 0: means nocompress, 1: best speed, ... 20: best compress ratio */
102#define DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL 1
zhanghailiang68b53592016-10-27 14:43:01 +0800103
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +0100104/* Background transfer rate for postcopy, 0 means unlimited, note
105 * that page requests can still exceed this limit.
106 */
107#define DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH 0
108
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +0000109/*
110 * Parameters for self_announce_delay giving a stream of RARP/ARP
111 * packets after migration.
112 */
113#define DEFAULT_MIGRATE_ANNOUNCE_INITIAL 50
114#define DEFAULT_MIGRATE_ANNOUNCE_MAX 550
115#define DEFAULT_MIGRATE_ANNOUNCE_ROUNDS 5
116#define DEFAULT_MIGRATE_ANNOUNCE_STEP 100
117
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100118static NotifierList migration_state_notifiers =
119 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
120
Juan Quintelada6f1792017-04-24 17:37:14 +0200121/* Messages sent on the return path from destination to source */
122enum mig_rp_message_type {
123 MIG_RP_MSG_INVALID = 0, /* Must be 0 */
124 MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */
125 MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */
126
127 MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */
128 MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */
Peter Xua335deb2018-05-02 18:47:28 +0800129 MIG_RP_MSG_RECV_BITMAP, /* send recved_bitmap back to source */
Peter Xu13955b82018-05-02 18:47:30 +0800130 MIG_RP_MSG_RESUME_ACK, /* tell source that we are ready to resume */
Juan Quintelada6f1792017-04-24 17:37:14 +0200131
132 MIG_RP_MSG_MAX
133};
134
Andrey Gruzdev6e8c25b2021-01-29 13:14:03 +0300135/* Migration capabilities set */
136struct MigrateCapsSet {
137 int size; /* Capability set size */
138 MigrationCapability caps[]; /* Variadic array of capabilities */
139};
140typedef struct MigrateCapsSet MigrateCapsSet;
141
142/* Define and initialize MigrateCapsSet */
143#define INITIALIZE_MIGRATE_CAPS_SET(_name, ...) \
144 MigrateCapsSet _name = { \
145 .size = sizeof((int []) { __VA_ARGS__ }) / sizeof(int), \
146 .caps = { __VA_ARGS__ } \
147 }
148
149/* Background-snapshot compatibility check list */
150static const
151INITIALIZE_MIGRATE_CAPS_SET(check_caps_background_snapshot,
152 MIGRATION_CAPABILITY_POSTCOPY_RAM,
153 MIGRATION_CAPABILITY_DIRTY_BITMAPS,
154 MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME,
155 MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE,
156 MIGRATION_CAPABILITY_RETURN_PATH,
157 MIGRATION_CAPABILITY_MULTIFD,
158 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER,
159 MIGRATION_CAPABILITY_AUTO_CONVERGE,
160 MIGRATION_CAPABILITY_RELEASE_RAM,
161 MIGRATION_CAPABILITY_RDMA_PIN_ALL,
162 MIGRATION_CAPABILITY_COMPRESS,
163 MIGRATION_CAPABILITY_XBZRLE,
164 MIGRATION_CAPABILITY_X_COLO,
165 MIGRATION_CAPABILITY_VALIDATE_UUID);
166
Juan Quintela17549e82011-10-05 13:50:43 +0200167/* When we add fault tolerance, we could have several
168 migrations at once. For now we don't need to add
169 dynamic creation of migration */
170
Peter Xue5cb7e72017-06-27 12:10:13 +0800171static MigrationState *current_migration;
Peter Xue1b1b1b2018-05-02 18:47:35 +0800172static MigrationIncomingState *current_incoming;
Peter Xue5cb7e72017-06-27 12:10:13 +0800173
Dr. David Alan Gilbert3af85542021-02-02 13:55:21 +0000174static GSList *migration_blockers;
175
Peter Xu8b0b29d2017-07-18 11:39:06 +0800176static bool migration_object_check(MigrationState *ms, Error **errp);
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +0100177static int migration_maybe_pause(MigrationState *s,
178 int *current_active_state,
179 int new_state);
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +0000180static void migrate_fd_cancel(MigrationState *s);
Peter Xu8b0b29d2017-07-18 11:39:06 +0800181
Peter Xu8f8bfff2020-10-21 17:27:18 -0400182static gint page_request_addr_cmp(gconstpointer ap, gconstpointer bp)
183{
184 uintptr_t a = (uintptr_t) ap, b = (uintptr_t) bp;
185
186 return (a > b) - (a < b);
187}
188
Peter Xue5cb7e72017-06-27 12:10:13 +0800189void migration_object_init(void)
190{
Peter Xu8b0b29d2017-07-18 11:39:06 +0800191 Error *err = NULL;
Peter Xu4ffdb332017-06-27 12:10:18 +0800192
Peter Xue5cb7e72017-06-27 12:10:13 +0800193 /* This can only be called once. */
194 assert(!current_migration);
195 current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION));
Peter Xu4ffdb332017-06-27 12:10:18 +0800196
Peter Xue1b1b1b2018-05-02 18:47:35 +0800197 /*
198 * Init the migrate incoming object as well no matter whether
199 * we'll use it or not.
200 */
201 assert(!current_incoming);
202 current_incoming = g_new0(MigrationIncomingState, 1);
203 current_incoming->state = MIGRATION_STATUS_NONE;
204 current_incoming->postcopy_remote_fds =
205 g_array_new(FALSE, TRUE, sizeof(struct PostCopyFD));
206 qemu_mutex_init(&current_incoming->rp_mutex);
207 qemu_event_init(&current_incoming->main_thread_load_event, false);
208 qemu_sem_init(&current_incoming->postcopy_pause_sem_dst, 0);
209 qemu_sem_init(&current_incoming->postcopy_pause_sem_fault, 0);
Peter Xu8f8bfff2020-10-21 17:27:18 -0400210 qemu_mutex_init(&current_incoming->page_request_mutex);
211 current_incoming->page_requested = g_tree_new(page_request_addr_cmp);
Peter Xue1b1b1b2018-05-02 18:47:35 +0800212
Peter Xu8b0b29d2017-07-18 11:39:06 +0800213 if (!migration_object_check(current_migration, &err)) {
214 error_report_err(err);
215 exit(1);
216 }
Paolo Bonzinie0d17df2020-10-21 05:25:58 -0400217
218 blk_mig_init();
219 ram_mig_init();
220 dirty_bitmap_mig_init();
Peter Xue5cb7e72017-06-27 12:10:13 +0800221}
222
David Hildenbrandc7c0e722021-04-29 13:27:02 +0200223void migration_cancel(void)
224{
225 migrate_fd_cancel(current_migration);
226}
227
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +0000228void migration_shutdown(void)
Vladimir Sementsov-Ogievskiy1f895602017-12-28 12:16:16 +0300229{
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +0000230 /*
231 * Cancel the current migration - that will (eventually)
232 * stop the migration using this structure
233 */
David Hildenbrandc7c0e722021-04-29 13:27:02 +0200234 migration_cancel();
Vladimir Sementsov-Ogievskiy1f895602017-12-28 12:16:16 +0300235 object_unref(OBJECT(current_migration));
Vladimir Sementsov-Ogievskiy1499ab02020-07-27 22:42:31 +0300236
237 /*
238 * Cancel outgoing migration of dirty bitmaps. It should
239 * at least unref used block nodes.
240 */
241 dirty_bitmap_mig_cancel_outgoing();
242
243 /*
244 * Cancel incoming migration of dirty bitmaps. Dirty bitmaps
245 * are non-critical data, and their loss never considered as
246 * something serious.
247 */
248 dirty_bitmap_mig_cancel_incoming();
Vladimir Sementsov-Ogievskiy1f895602017-12-28 12:16:16 +0300249}
250
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100251/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +0200252MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +0200253{
Peter Xue5cb7e72017-06-27 12:10:13 +0800254 /* This can only be called after the object created. */
255 assert(current_migration);
256 return current_migration;
Juan Quintela17549e82011-10-05 13:50:43 +0200257}
258
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100259MigrationIncomingState *migration_incoming_get_current(void)
260{
Peter Xue1b1b1b2018-05-02 18:47:35 +0800261 assert(current_incoming);
262 return current_incoming;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100263}
264
265void migration_incoming_state_destroy(void)
266{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100267 struct MigrationIncomingState *mis = migration_incoming_get_current();
268
Peter Xu34826552017-05-19 14:43:29 +0800269 if (mis->to_src_file) {
Peter Xu660819b2017-05-19 14:43:30 +0800270 /* Tell source that we are done */
271 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
Peter Xu34826552017-05-19 14:43:29 +0800272 qemu_fclose(mis->to_src_file);
273 mis->to_src_file = NULL;
274 }
275
Peter Xu660819b2017-05-19 14:43:30 +0800276 if (mis->from_src_file) {
Peter Xu39675ff2021-07-22 13:58:41 -0400277 migration_ioc_unregister_yank_from_file(mis->from_src_file);
Peter Xu660819b2017-05-19 14:43:30 +0800278 qemu_fclose(mis->from_src_file);
279 mis->from_src_file = NULL;
280 }
Dr. David Alan Gilbert00fa4fc2018-03-12 17:21:04 +0000281 if (mis->postcopy_remote_fds) {
282 g_array_free(mis->postcopy_remote_fds, TRUE);
283 mis->postcopy_remote_fds = NULL;
284 }
Dr. David Alan Gilbert1df6ddb2021-04-21 12:28:32 +0100285 if (mis->transport_cleanup) {
286 mis->transport_cleanup(mis->transport_data);
287 }
Peter Xu660819b2017-05-19 14:43:30 +0800288
Dr. David Alan Gilbert1783c002020-09-28 15:48:30 +0100289 qemu_event_reset(&mis->main_thread_load_event);
290
Peter Xu8f8bfff2020-10-21 17:27:18 -0400291 if (mis->page_requested) {
292 g_tree_destroy(mis->page_requested);
293 mis->page_requested = NULL;
294 }
295
Juan Quintela9aca82b2019-02-27 11:51:27 +0100296 if (mis->socket_address_list) {
297 qapi_free_SocketAddressList(mis->socket_address_list);
298 mis->socket_address_list = NULL;
299 }
Lukas Straubb5eea992020-12-28 16:08:52 +0100300
301 yank_unregister_instance(MIGRATION_YANK_INSTANCE);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100302}
303
Juan Quintelab05dc722015-07-07 14:44:05 +0200304static void migrate_generate_event(int new_state)
305{
306 if (migrate_use_events()) {
Peter Xu3ab72382018-08-15 21:37:37 +0800307 qapi_event_send_migration(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200308 }
309}
310
Dr. David Alan Gilbert0f073f42018-04-16 18:09:30 +0100311static bool migrate_late_block_activate(void)
312{
313 MigrationState *s;
314
315 s = migrate_get_current();
316
317 return s->enabled_capabilities[
318 MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE];
319}
320
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000321/*
Juan Quintelada6f1792017-04-24 17:37:14 +0200322 * Send a message on the return channel back to the source
323 * of the migration.
324 */
Peter Xud6208e32018-02-08 18:31:12 +0800325static int migrate_send_rp_message(MigrationIncomingState *mis,
326 enum mig_rp_message_type message_type,
327 uint16_t len, void *data)
Juan Quintelada6f1792017-04-24 17:37:14 +0200328{
Peter Xud6208e32018-02-08 18:31:12 +0800329 int ret = 0;
330
Juan Quintelada6f1792017-04-24 17:37:14 +0200331 trace_migrate_send_rp_message((int)message_type, len);
Mahmoud Mandour37396952021-03-11 05:15:35 +0200332 QEMU_LOCK_GUARD(&mis->rp_mutex);
Peter Xud6208e32018-02-08 18:31:12 +0800333
334 /*
335 * It's possible that the file handle got lost due to network
336 * failures.
337 */
338 if (!mis->to_src_file) {
339 ret = -EIO;
Mahmoud Mandour37396952021-03-11 05:15:35 +0200340 return ret;
Peter Xud6208e32018-02-08 18:31:12 +0800341 }
342
Juan Quintelada6f1792017-04-24 17:37:14 +0200343 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
344 qemu_put_be16(mis->to_src_file, len);
345 qemu_put_buffer(mis->to_src_file, data, len);
346 qemu_fflush(mis->to_src_file);
Peter Xud6208e32018-02-08 18:31:12 +0800347
348 /* It's possible that qemu file got error during sending */
349 ret = qemu_file_get_error(mis->to_src_file);
350
Peter Xud6208e32018-02-08 18:31:12 +0800351 return ret;
Juan Quintelada6f1792017-04-24 17:37:14 +0200352}
353
Peter Xu2e2bce12020-09-08 16:30:18 -0400354/* Request one page from the source VM at the given start address.
355 * rb: the RAMBlock to request the page in
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000356 * Start: Address offset within the RB
357 * Len: Length in bytes required - must be a multiple of pagesize
358 */
Peter Xu7a267fc2020-10-21 17:27:17 -0400359int migrate_send_rp_message_req_pages(MigrationIncomingState *mis,
360 RAMBlock *rb, ram_addr_t start)
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000361{
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100362 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000363 size_t msglen = 12; /* start + len */
Peter Xu2e2bce12020-09-08 16:30:18 -0400364 size_t len = qemu_ram_pagesize(rb);
Peter Xud6208e32018-02-08 18:31:12 +0800365 enum mig_rp_message_type msg_type;
Peter Xu2e2bce12020-09-08 16:30:18 -0400366 const char *rbname;
367 int rbname_len;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000368
369 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
370 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
371
Peter Xu2e2bce12020-09-08 16:30:18 -0400372 /*
373 * We maintain the last ramblock that we requested for page. Note that we
374 * don't need locking because this function will only be called within the
375 * postcopy ram fault thread.
376 */
377 if (rb != mis->last_rb) {
378 mis->last_rb = rb;
379
380 rbname = qemu_ram_get_idstr(rb);
381 rbname_len = strlen(rbname);
382
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000383 assert(rbname_len < 256);
384
385 bufc[msglen++] = rbname_len;
386 memcpy(bufc + msglen, rbname, rbname_len);
387 msglen += rbname_len;
Peter Xud6208e32018-02-08 18:31:12 +0800388 msg_type = MIG_RP_MSG_REQ_PAGES_ID;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000389 } else {
Peter Xud6208e32018-02-08 18:31:12 +0800390 msg_type = MIG_RP_MSG_REQ_PAGES;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000391 }
Peter Xud6208e32018-02-08 18:31:12 +0800392
393 return migrate_send_rp_message(mis, msg_type, msglen, bufc);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000394}
395
Peter Xu7a267fc2020-10-21 17:27:17 -0400396int migrate_send_rp_req_pages(MigrationIncomingState *mis,
Peter Xu8f8bfff2020-10-21 17:27:18 -0400397 RAMBlock *rb, ram_addr_t start, uint64_t haddr)
Peter Xu7a267fc2020-10-21 17:27:17 -0400398{
Peter Xu8f8bfff2020-10-21 17:27:18 -0400399 void *aligned = (void *)(uintptr_t)(haddr & (-qemu_ram_pagesize(rb)));
Chen Quna2429282020-11-11 22:22:03 +0800400 bool received = false;
Peter Xu8f8bfff2020-10-21 17:27:18 -0400401
402 WITH_QEMU_LOCK_GUARD(&mis->page_request_mutex) {
403 received = ramblock_recv_bitmap_test_byte_offset(rb, start);
404 if (!received && !g_tree_lookup(mis->page_requested, aligned)) {
405 /*
406 * The page has not been received, and it's not yet in the page
407 * request list. Queue it. Set the value of element to 1, so that
408 * things like g_tree_lookup() will return TRUE (1) when found.
409 */
410 g_tree_insert(mis->page_requested, aligned, (gpointer)1);
411 mis->page_requested_count++;
412 trace_postcopy_page_req_add(aligned, mis->page_requested_count);
413 }
414 }
415
416 /*
417 * If the page is there, skip sending the message. We don't even need the
418 * lock because as long as the page arrived, it'll be there forever.
419 */
420 if (received) {
421 return 0;
422 }
423
Peter Xu7a267fc2020-10-21 17:27:17 -0400424 return migrate_send_rp_message_req_pages(mis, rb, start);
425}
426
Zhang Chenaad555c2018-09-03 12:38:47 +0800427static bool migration_colo_enabled;
428bool migration_incoming_colo_enabled(void)
429{
430 return migration_colo_enabled;
431}
432
433void migration_incoming_disable_colo(void)
434{
David Hildenbrand18b1d3c2020-06-26 09:22:36 +0200435 ram_block_discard_disable(false);
Zhang Chenaad555c2018-09-03 12:38:47 +0800436 migration_colo_enabled = false;
437}
438
David Hildenbrand18b1d3c2020-06-26 09:22:36 +0200439int migration_incoming_enable_colo(void)
Zhang Chenaad555c2018-09-03 12:38:47 +0800440{
David Hildenbrand18b1d3c2020-06-26 09:22:36 +0200441 if (ram_block_discard_disable(true)) {
442 error_report("COLO: cannot disable RAM discard");
443 return -EBUSY;
444 }
Zhang Chenaad555c2018-09-03 12:38:47 +0800445 migration_colo_enabled = true;
David Hildenbrand18b1d3c2020-06-26 09:22:36 +0200446 return 0;
Zhang Chenaad555c2018-09-03 12:38:47 +0800447}
448
Juan Quintela9aca82b2019-02-27 11:51:27 +0100449void migrate_add_address(SocketAddress *address)
450{
451 MigrationIncomingState *mis = migration_incoming_get_current();
Juan Quintela9aca82b2019-02-27 11:51:27 +0100452
Eric Blake54aa3de2020-11-12 19:13:37 -0600453 QAPI_LIST_PREPEND(mis->socket_address_list,
454 QAPI_CLONE(SocketAddress, address));
Juan Quintela9aca82b2019-02-27 11:51:27 +0100455}
456
Paolo Bonzinie69d50d2020-10-27 04:22:57 -0400457static void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000458{
Longpeng(Mike)d658f652020-08-06 15:40:29 +0800459 const char *p = NULL;
aliguori34c9dd82008-10-13 03:14:31 +0000460
Peter Xu3ab72382018-08-15 21:37:37 +0800461 qapi_event_send_migration(MIGRATION_STATUS_SETUP);
Paolo Bonzinie69d50d2020-10-27 04:22:57 -0400462 if (strstart(uri, "tcp:", &p) ||
463 strstart(uri, "unix:", NULL) ||
464 strstart(uri, "vsock:", NULL)) {
Longpeng(Mike)d658f652020-08-06 15:40:29 +0800465 socket_start_incoming_migration(p ? p : uri, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400466#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000467 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400468 rdma_start_incoming_migration(p, errp);
469#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000470 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200471 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000472 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200473 fd_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000474 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100475 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200476 }
aliguori5bb79102008-10-13 03:12:02 +0000477}
478
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300479static void process_incoming_migration_bh(void *opaque)
480{
481 Error *local_err = NULL;
482 MigrationIncomingState *mis = opaque;
483
Dr. David Alan Gilbert0f073f42018-04-16 18:09:30 +0100484 /* If capability late_block_activate is set:
485 * Only fire up the block code now if we're going to restart the
486 * VM, else 'cont' will do it.
487 * This causes file locking to happen; so we don't want it to happen
488 * unless we really are starting the VM.
489 */
490 if (!migrate_late_block_activate() ||
491 (autostart && (!global_state_received() ||
492 global_state_get_runstate() == RUN_STATE_RUNNING))) {
493 /* Make sure all file formats flush their mutable metadata.
494 * If we get an error here, just don't restart the VM yet. */
495 bdrv_invalidate_cache_all(&local_err);
496 if (local_err) {
497 error_report_err(local_err);
498 local_err = NULL;
499 autostart = false;
500 }
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200501 }
502
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300503 /*
504 * This must happen after all error conditions are dealt with and
505 * we're sure the VM is going to be running on this host.
506 */
Dr. David Alan Gilbert76595052019-02-27 13:24:08 +0000507 qemu_announce_self(&mis->announce_timer, migrate_announce_params());
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300508
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100509 if (multifd_load_cleanup(&local_err) != 0) {
510 error_report_err(local_err);
511 autostart = false;
512 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300513 /* If global state section was not received or we are in running
514 state, we need to obey autostart. Any other state is set with
515 runstate_set. */
516
Vladimir Sementsov-Ogievskiyb35ebdf2018-03-13 15:34:01 -0400517 dirty_bitmap_mig_before_vm_start();
518
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300519 if (!global_state_received() ||
520 global_state_get_runstate() == RUN_STATE_RUNNING) {
521 if (autostart) {
522 vm_start();
523 } else {
524 runstate_set(RUN_STATE_PAUSED);
525 }
Zhang Chendb009722019-03-03 22:50:17 +0800526 } else if (migration_incoming_colo_enabled()) {
527 migration_incoming_disable_colo();
528 vm_start();
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300529 } else {
530 runstate_set(global_state_get_runstate());
531 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300532 /*
533 * This must happen after any state changes since as soon as an external
534 * observer sees this event they might start to prod at the VM assuming
535 * it's ready to use.
536 */
537 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
538 MIGRATION_STATUS_COMPLETED);
539 qemu_bh_delete(mis->bh);
540 migration_incoming_state_destroy();
541}
542
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200543static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200544{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100545 MigrationIncomingState *mis = migration_incoming_get_current();
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000546 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200547 int ret;
Zhang Chen8e48ac92018-09-03 12:38:46 +0800548 Error *local_err = NULL;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200549
Juan Quintela4f0fae72017-07-24 12:42:02 +0200550 assert(mis->from_src_file);
Lidong Chen92370982018-08-06 21:29:35 +0800551 mis->migration_incoming_co = qemu_coroutine_self();
Dr. David Alan Gilbert67f11b52017-02-24 18:28:34 +0000552 mis->largest_page_size = qemu_ram_pagesize_largest();
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000553 postcopy_state_set(POSTCOPY_INCOMING_NONE);
zhanghailiang93d7af62015-12-16 11:47:34 +0000554 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
555 MIGRATION_STATUS_ACTIVE);
Juan Quintela4f0fae72017-07-24 12:42:02 +0200556 ret = qemu_loadvm_state(mis->from_src_file);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100557
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000558 ps = postcopy_state_get();
559 trace_process_incoming_migration_co_end(ret, ps);
560 if (ps != POSTCOPY_INCOMING_NONE) {
561 if (ps == POSTCOPY_INCOMING_ADVISE) {
562 /*
563 * Where a migration had postcopy enabled (and thus went to advise)
564 * but managed to complete within the precopy period, we can use
565 * the normal exit.
566 */
567 postcopy_ram_incoming_cleanup(mis);
568 } else if (ret >= 0) {
569 /*
570 * Postcopy was started, cleanup should happen at the end of the
571 * postcopy thread.
572 */
573 trace_process_incoming_migration_co_postcopy_end_main();
574 return;
575 }
576 /* Else if something went wrong then just fall out of the normal exit */
577 }
578
zhanghailiang25d0c162016-10-27 14:42:55 +0800579 /* we get COLO info, and know if we are in COLO mode */
Zhang Chenaad555c2018-09-03 12:38:47 +0800580 if (!ret && migration_incoming_colo_enabled()) {
Zhang Chen8e48ac92018-09-03 12:38:46 +0800581 /* Make sure all file formats flush their mutable metadata */
582 bdrv_invalidate_cache_all(&local_err);
583 if (local_err) {
Zhang Chen8e48ac92018-09-03 12:38:46 +0800584 error_report_err(local_err);
Fei Li6d99c2d2019-01-13 22:08:49 +0800585 goto fail;
Zhang Chen8e48ac92018-09-03 12:38:46 +0800586 }
587
zhanghailiang25d0c162016-10-27 14:42:55 +0800588 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
589 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
590 mis->have_colo_incoming_thread = true;
591 qemu_coroutine_yield();
592
593 /* Wait checkpoint incoming thread exit before free resource */
594 qemu_thread_join(&mis->colo_incoming_thread);
Zhang Chen13af18f2018-09-03 12:38:48 +0800595 /* We hold the global iothread lock, so it is safe here */
596 colo_release_ram_cache();
zhanghailiang25d0c162016-10-27 14:42:55 +0800597 }
598
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200599 if (ret < 0) {
Peter Lievendb80fac2014-06-10 11:29:16 +0200600 error_report("load of migration failed: %s", strerror(-ret));
Fei Li6d99c2d2019-01-13 22:08:49 +0800601 goto fail;
Juan Quintela511c0232010-06-09 14:10:55 +0200602 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300603 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
604 qemu_bh_schedule(mis->bh);
Lidong Chen92370982018-08-06 21:29:35 +0800605 mis->migration_incoming_co = NULL;
Fei Li6d99c2d2019-01-13 22:08:49 +0800606 return;
607fail:
608 local_err = NULL;
609 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
610 MIGRATION_STATUS_FAILED);
611 qemu_fclose(mis->from_src_file);
612 if (multifd_load_cleanup(&local_err) != 0) {
613 error_report_err(local_err);
614 }
615 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200616}
617
Juan Quintelab673eab2019-06-12 11:44:19 +0200618/**
619 * @migration_incoming_setup: Setup incoming migration
620 *
621 * Returns 0 for no error or 1 for error
622 *
623 * @f: file for main migration channel
624 * @errp: where to put errors
625 */
626static int migration_incoming_setup(QEMUFile *f, Error **errp)
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200627{
Juan Quintela4f0fae72017-07-24 12:42:02 +0200628 MigrationIncomingState *mis = migration_incoming_get_current();
Juan Quintelab673eab2019-06-12 11:44:19 +0200629 Error *local_err = NULL;
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200630
Juan Quintelab673eab2019-06-12 11:44:19 +0200631 if (multifd_load_setup(&local_err) != 0) {
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100632 /* We haven't been able to create multifd threads
633 nothing better to do */
Juan Quintelab673eab2019-06-12 11:44:19 +0200634 error_report_err(local_err);
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100635 exit(EXIT_FAILURE);
636 }
637
Juan Quintela4f0fae72017-07-24 12:42:02 +0200638 if (!mis->from_src_file) {
639 mis->from_src_file = f;
640 }
Daniel P. Berrange06ad5132016-04-27 11:04:56 +0100641 qemu_file_set_blocking(f, false);
Juan Quintelab673eab2019-06-12 11:44:19 +0200642 return 0;
Juan Quintelae595a012017-07-17 12:30:25 +0200643}
644
Juan Quintela36c2f8b2018-03-07 08:40:52 +0100645void migration_incoming_process(void)
Juan Quintelae595a012017-07-17 12:30:25 +0200646{
647 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL);
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200648 qemu_coroutine_enter(co);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200649}
650
Peter Xu884835f2018-06-27 21:22:45 +0800651/* Returns true if recovered from a paused migration, otherwise false */
652static bool postcopy_try_recover(QEMUFile *f)
Juan Quintelae595a012017-07-17 12:30:25 +0200653{
Peter Xud96c9e82018-05-02 18:47:26 +0800654 MigrationIncomingState *mis = migration_incoming_get_current();
655
656 if (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
657 /* Resumed from a paused postcopy migration */
658
659 mis->from_src_file = f;
660 /* Postcopy has standalone thread to do vm load */
661 qemu_file_set_blocking(f, true);
662
663 /* Re-configure the return path */
664 mis->to_src_file = qemu_file_get_return_path(f);
665
666 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
667 MIGRATION_STATUS_POSTCOPY_RECOVER);
668
669 /*
670 * Here, we only wake up the main loading thread (while the
671 * fault thread will still be waiting), so that we can receive
672 * commands from source now, and answer it if needed. The
673 * fault thread will be woken up afterwards until we are sure
674 * that source is ready to reply to page requests.
675 */
676 qemu_sem_post(&mis->postcopy_pause_sem_dst);
Peter Xu884835f2018-06-27 21:22:45 +0800677 return true;
Peter Xud96c9e82018-05-02 18:47:26 +0800678 }
Peter Xu884835f2018-06-27 21:22:45 +0800679
680 return false;
681}
682
Juan Quintelab673eab2019-06-12 11:44:19 +0200683void migration_fd_process_incoming(QEMUFile *f, Error **errp)
Peter Xu884835f2018-06-27 21:22:45 +0800684{
Juan Quintelab673eab2019-06-12 11:44:19 +0200685 Error *local_err = NULL;
686
Peter Xu884835f2018-06-27 21:22:45 +0800687 if (postcopy_try_recover(f)) {
688 return;
689 }
690
Juan Quintelab673eab2019-06-12 11:44:19 +0200691 if (migration_incoming_setup(f, &local_err)) {
Markus Armbruster2155cea2020-07-22 10:40:47 +0200692 error_propagate(errp, local_err);
Juan Quintelab673eab2019-06-12 11:44:19 +0200693 return;
694 }
Peter Xu884835f2018-06-27 21:22:45 +0800695 migration_incoming_process();
Juan Quintelae595a012017-07-17 12:30:25 +0200696}
697
Fei Li49ed0d22019-01-13 22:08:46 +0800698void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp)
Juan Quintela4f0fae72017-07-24 12:42:02 +0200699{
700 MigrationIncomingState *mis = migration_incoming_get_current();
Juan Quintelab673eab2019-06-12 11:44:19 +0200701 Error *local_err = NULL;
Peter Xua429e7f2018-06-27 21:22:46 +0800702 bool start_migration;
Juan Quintela4f0fae72017-07-24 12:42:02 +0200703
704 if (!mis->from_src_file) {
Peter Xua429e7f2018-06-27 21:22:46 +0800705 /* The first connection (multifd may have multiple) */
Juan Quintela4f0fae72017-07-24 12:42:02 +0200706 QEMUFile *f = qemu_fopen_channel_input(ioc);
Peter Xua429e7f2018-06-27 21:22:46 +0800707
708 /* If it's a recovery, we're done */
Peter Xu884835f2018-06-27 21:22:45 +0800709 if (postcopy_try_recover(f)) {
710 return;
711 }
Peter Xua429e7f2018-06-27 21:22:46 +0800712
Juan Quintelab673eab2019-06-12 11:44:19 +0200713 if (migration_incoming_setup(f, &local_err)) {
Markus Armbruster2155cea2020-07-22 10:40:47 +0200714 error_propagate(errp, local_err);
Juan Quintelab673eab2019-06-12 11:44:19 +0200715 return;
716 }
Peter Xua429e7f2018-06-27 21:22:46 +0800717
718 /*
719 * Common migration only needs one channel, so we can start
720 * right now. Multifd needs more than one channel, we wait.
721 */
722 start_migration = !migrate_use_multifd();
723 } else {
724 /* Multiple connections */
725 assert(migrate_use_multifd());
Fei Li49ed0d22019-01-13 22:08:46 +0800726 start_migration = multifd_recv_new_channel(ioc, &local_err);
727 if (local_err) {
728 error_propagate(errp, local_err);
729 return;
730 }
Juan Quintela4f0fae72017-07-24 12:42:02 +0200731 }
Peter Xu81e62052018-06-27 21:22:44 +0800732
Peter Xua429e7f2018-06-27 21:22:46 +0800733 if (start_migration) {
Peter Xu81e62052018-06-27 21:22:44 +0800734 migration_incoming_process();
735 }
Juan Quintela4f0fae72017-07-24 12:42:02 +0200736}
737
Juan Quintela428d8902017-07-24 13:06:25 +0200738/**
739 * @migration_has_all_channels: We have received all channels that we need
740 *
741 * Returns true when we have got connections to all the channels that
742 * we need for migration.
743 */
744bool migration_has_all_channels(void)
745{
Daniel P. Berrangéca273df2018-06-19 17:35:52 +0100746 MigrationIncomingState *mis = migration_incoming_get_current();
Juan Quintela62c1e0c2018-02-19 18:59:02 +0100747 bool all_channels;
748
749 all_channels = multifd_recv_all_channels_created();
750
Daniel P. Berrangéca273df2018-06-19 17:35:52 +0100751 return all_channels && mis->from_src_file != NULL;
Juan Quintela428d8902017-07-24 13:06:25 +0200752}
753
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000754/*
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000755 * Send a 'SHUT' message on the return channel with the given value
756 * to indicate that we've finished with the RP. Non-0 value indicates
757 * error.
758 */
759void migrate_send_rp_shut(MigrationIncomingState *mis,
760 uint32_t value)
761{
762 uint32_t buf;
763
764 buf = cpu_to_be32(value);
765 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
766}
767
768/*
769 * Send a 'PONG' message on the return channel with the given value
770 * (normally in response to a 'PING')
771 */
772void migrate_send_rp_pong(MigrationIncomingState *mis,
773 uint32_t value)
774{
775 uint32_t buf;
776
777 buf = cpu_to_be32(value);
778 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
779}
780
Peter Xua335deb2018-05-02 18:47:28 +0800781void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
782 char *block_name)
783{
784 char buf[512];
785 int len;
786 int64_t res;
787
788 /*
789 * First, we send the header part. It contains only the len of
790 * idstr, and the idstr itself.
791 */
792 len = strlen(block_name);
793 buf[0] = len;
794 memcpy(buf + 1, block_name, len);
795
796 if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
797 error_report("%s: MSG_RP_RECV_BITMAP only used for recovery",
798 __func__);
799 return;
800 }
801
802 migrate_send_rp_message(mis, MIG_RP_MSG_RECV_BITMAP, len + 1, buf);
803
804 /*
805 * Next, we dump the received bitmap to the stream.
806 *
807 * TODO: currently we are safe since we are the only one that is
808 * using the to_src_file handle (fault thread is still paused),
809 * and it's ok even not taking the mutex. However the best way is
810 * to take the lock before sending the message header, and release
811 * the lock after sending the bitmap.
812 */
813 qemu_mutex_lock(&mis->rp_mutex);
814 res = ramblock_recv_bitmap_send(mis->to_src_file, block_name);
815 qemu_mutex_unlock(&mis->rp_mutex);
816
817 trace_migrate_send_rp_recv_bitmap(block_name, res);
818}
819
Peter Xu13955b82018-05-02 18:47:30 +0800820void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value)
821{
822 uint32_t buf;
823
824 buf = cpu_to_be32(value);
825 migrate_send_rp_message(mis, MIG_RP_MSG_RESUME_ACK, sizeof(buf), &buf);
826}
827
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300828MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
829{
Eric Blake95b3a8c2021-01-13 16:10:13 -0600830 MigrationCapabilityStatusList *head = NULL, **tail = &head;
831 MigrationCapabilityStatus *caps;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300832 MigrationState *s = migrate_get_current();
833 int i;
834
Eric Blake7fb1cf12015-11-18 01:52:57 -0700835 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Dr. David Alan Gilberted1701c2017-05-15 15:05:29 +0100836#ifndef CONFIG_LIVE_BLOCK_MIGRATION
837 if (i == MIGRATION_CAPABILITY_BLOCK) {
838 continue;
839 }
840#endif
Eric Blake95b3a8c2021-01-13 16:10:13 -0600841 caps = g_malloc0(sizeof(*caps));
842 caps->capability = i;
843 caps->state = s->enabled_capabilities[i];
844 QAPI_LIST_APPEND(tail, caps);
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300845 }
846
847 return head;
848}
849
Liang Li85de8322015-03-23 16:32:28 +0800850MigrationParameters *qmp_query_migrate_parameters(Error **errp)
851{
852 MigrationParameters *params;
853 MigrationState *s = migrate_get_current();
854
Markus Armbrustere87fae42017-07-18 12:57:38 +0200855 /* TODO use QAPI_CLONE() instead of duplicating it inline */
Liang Li85de8322015-03-23 16:32:28 +0800856 params = g_malloc0(sizeof(*params));
Eric Blakede63ab62016-09-08 22:14:15 -0500857 params->has_compress_level = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100858 params->compress_level = s->parameters.compress_level;
Eric Blakede63ab62016-09-08 22:14:15 -0500859 params->has_compress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100860 params->compress_threads = s->parameters.compress_threads;
Xiao Guangrong1d588722018-08-21 16:10:20 +0800861 params->has_compress_wait_thread = true;
862 params->compress_wait_thread = s->parameters.compress_wait_thread;
Eric Blakede63ab62016-09-08 22:14:15 -0500863 params->has_decompress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100864 params->decompress_threads = s->parameters.decompress_threads;
Keqian Zhudc14a472020-02-24 10:31:42 +0800865 params->has_throttle_trigger_threshold = true;
866 params->throttle_trigger_threshold = s->parameters.throttle_trigger_threshold;
Eric Blakede63ab62016-09-08 22:14:15 -0500867 params->has_cpu_throttle_initial = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100868 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
Eric Blakede63ab62016-09-08 22:14:15 -0500869 params->has_cpu_throttle_increment = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100870 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
Keqian Zhucbbf8182020-04-13 18:15:08 +0800871 params->has_cpu_throttle_tailslow = true;
872 params->cpu_throttle_tailslow = s->parameters.cpu_throttle_tailslow;
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200873 params->has_tls_creds = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100874 params->tls_creds = g_strdup(s->parameters.tls_creds);
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200875 params->has_tls_hostname = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100876 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
Daniel P. Berranged2f1d292019-02-27 14:53:24 +0000877 params->has_tls_authz = true;
Mao Zhongyi7cd75cb2020-03-25 09:49:30 +0800878 params->tls_authz = g_strdup(s->parameters.tls_authz ?
879 s->parameters.tls_authz : "");
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530880 params->has_max_bandwidth = true;
881 params->max_bandwidth = s->parameters.max_bandwidth;
882 params->has_downtime_limit = true;
883 params->downtime_limit = s->parameters.downtime_limit;
zhanghailiangfe39a4d2016-11-02 15:42:09 +0800884 params->has_x_checkpoint_delay = true;
zhanghailiang68b53592016-10-27 14:43:01 +0800885 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
Juan Quintela2833c592017-04-05 18:32:37 +0200886 params->has_block_incremental = true;
887 params->block_incremental = s->parameters.block_incremental;
Juan Quintelacbfd6c92019-02-06 13:54:06 +0100888 params->has_multifd_channels = true;
889 params->multifd_channels = s->parameters.multifd_channels;
Juan Quintela96eef042019-01-16 10:35:55 +0100890 params->has_multifd_compression = true;
891 params->multifd_compression = s->parameters.multifd_compression;
Juan Quintela9004db42020-01-23 17:08:52 +0100892 params->has_multifd_zlib_level = true;
893 params->multifd_zlib_level = s->parameters.multifd_zlib_level;
Juan Quintela6a9ad152020-01-23 17:41:36 +0100894 params->has_multifd_zstd_level = true;
895 params->multifd_zstd_level = s->parameters.multifd_zstd_level;
Juan Quintela73af8dd2017-10-05 21:30:10 +0200896 params->has_xbzrle_cache_size = true;
897 params->xbzrle_cache_size = s->parameters.xbzrle_cache_size;
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +0100898 params->has_max_postcopy_bandwidth = true;
899 params->max_postcopy_bandwidth = s->parameters.max_postcopy_bandwidth;
Li Qiang4cbc9c72018-08-01 06:00:20 -0700900 params->has_max_cpu_throttle = true;
901 params->max_cpu_throttle = s->parameters.max_cpu_throttle;
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +0000902 params->has_announce_initial = true;
903 params->announce_initial = s->parameters.announce_initial;
904 params->has_announce_max = true;
905 params->announce_max = s->parameters.announce_max;
906 params->has_announce_rounds = true;
907 params->announce_rounds = s->parameters.announce_rounds;
908 params->has_announce_step = true;
909 params->announce_step = s->parameters.announce_step;
Liang Li85de8322015-03-23 16:32:28 +0800910
Max Reitz31e4c352020-08-20 17:07:23 +0200911 if (s->parameters.has_block_bitmap_mapping) {
912 params->has_block_bitmap_mapping = true;
913 params->block_bitmap_mapping =
914 QAPI_CLONE(BitmapMigrationNodeAliasList,
915 s->parameters.block_bitmap_mapping);
916 }
917
Liang Li85de8322015-03-23 16:32:28 +0800918 return params;
919}
920
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +0000921AnnounceParameters *migrate_announce_params(void)
922{
923 static AnnounceParameters ap;
924
925 MigrationState *s = migrate_get_current();
926
927 ap.initial = s->parameters.announce_initial;
928 ap.max = s->parameters.announce_max;
929 ap.rounds = s->parameters.announce_rounds;
930 ap.step = s->parameters.announce_step;
931
932 return &ap;
933}
934
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000935/*
936 * Return true if we're already in the middle of a migration
937 * (i.e. any of the active or setup states)
938 */
Jia Lina3d63da12018-10-26 16:36:20 +0800939bool migration_is_setup_or_active(int state)
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000940{
941 switch (state) {
942 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000943 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Peter Xua688d2c2018-05-02 18:47:18 +0800944 case MIGRATION_STATUS_POSTCOPY_PAUSED:
Peter Xu135b87b2018-05-02 18:47:25 +0800945 case MIGRATION_STATUS_POSTCOPY_RECOVER:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000946 case MIGRATION_STATUS_SETUP:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +0100947 case MIGRATION_STATUS_PRE_SWITCHOVER:
948 case MIGRATION_STATUS_DEVICE:
Jens Freimannc7e0acd2019-10-29 12:49:02 +0100949 case MIGRATION_STATUS_WAIT_UNPLUG:
zhanghailiang19dd4082020-02-24 14:54:12 +0800950 case MIGRATION_STATUS_COLO:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000951 return true;
952
953 default:
954 return false;
955
956 }
957}
958
Juan Quintela392d87e2020-01-21 15:39:23 +0100959bool migration_is_running(int state)
960{
961 switch (state) {
962 case MIGRATION_STATUS_ACTIVE:
963 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
964 case MIGRATION_STATUS_POSTCOPY_PAUSED:
965 case MIGRATION_STATUS_POSTCOPY_RECOVER:
966 case MIGRATION_STATUS_SETUP:
967 case MIGRATION_STATUS_PRE_SWITCHOVER:
968 case MIGRATION_STATUS_DEVICE:
969 case MIGRATION_STATUS_WAIT_UNPLUG:
970 case MIGRATION_STATUS_CANCELLING:
Juan Quintela392d87e2020-01-21 15:39:23 +0100971 return true;
972
973 default:
974 return false;
975
976 }
977}
978
Wei Yang640dfb12019-07-16 08:54:11 +0800979static void populate_time_info(MigrationInfo *info, MigrationState *s)
980{
981 info->has_status = true;
982 info->has_setup_time = true;
983 info->setup_time = s->setup_time;
984 if (s->state == MIGRATION_STATUS_COMPLETED) {
985 info->has_total_time = true;
986 info->total_time = s->total_time;
987 info->has_downtime = true;
988 info->downtime = s->downtime;
989 } else {
990 info->has_total_time = true;
991 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
992 s->start_time;
993 info->has_expected_downtime = true;
994 info->expected_downtime = s->expected_downtime;
995 }
996}
997
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100998static void populate_ram_info(MigrationInfo *info, MigrationState *s)
999{
1000 info->has_ram = true;
1001 info->ram = g_malloc0(sizeof(*info->ram));
Juan Quintela93604472017-06-06 19:49:03 +02001002 info->ram->transferred = ram_counters.transferred;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +01001003 info->ram->total = ram_bytes_total();
Juan Quintela93604472017-06-06 19:49:03 +02001004 info->ram->duplicate = ram_counters.duplicate;
Juan Quintelabedf53c2017-03-13 20:35:54 +01001005 /* legacy value. It is not used anymore */
1006 info->ram->skipped = 0;
Juan Quintela93604472017-06-06 19:49:03 +02001007 info->ram->normal = ram_counters.normal;
1008 info->ram->normal_bytes = ram_counters.normal *
Juan Quintela20afaed2017-03-21 09:09:14 +01001009 qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +01001010 info->ram->mbps = s->mbps;
Juan Quintela93604472017-06-06 19:49:03 +02001011 info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
1012 info->ram->postcopy_requests = ram_counters.postcopy_requests;
Chao Fan030ce1f2017-03-21 10:22:43 +08001013 info->ram->page_size = qemu_target_page_size();
Juan Quintelaa61c45b2018-06-26 15:20:11 +02001014 info->ram->multifd_bytes = ram_counters.multifd_bytes;
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08001015 info->ram->pages_per_second = s->pages_per_second;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +01001016
Juan Quintela114f5ae2017-05-04 10:09:21 +02001017 if (migrate_use_xbzrle()) {
1018 info->has_xbzrle_cache = true;
1019 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
1020 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
Juan Quintela93604472017-06-06 19:49:03 +02001021 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
1022 info->xbzrle_cache->pages = xbzrle_counters.pages;
1023 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
1024 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
Wei Wange460a4b2020-04-30 08:59:35 +08001025 info->xbzrle_cache->encoding_rate = xbzrle_counters.encoding_rate;
Juan Quintela93604472017-06-06 19:49:03 +02001026 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
Juan Quintela114f5ae2017-05-04 10:09:21 +02001027 }
1028
Xiao Guangrong76e03002018-09-06 15:01:00 +08001029 if (migrate_use_compression()) {
1030 info->has_compression = true;
1031 info->compression = g_malloc0(sizeof(*info->compression));
1032 info->compression->pages = compression_counters.pages;
1033 info->compression->busy = compression_counters.busy;
1034 info->compression->busy_rate = compression_counters.busy_rate;
1035 info->compression->compressed_size =
1036 compression_counters.compressed_size;
1037 info->compression->compression_rate =
1038 compression_counters.compression_rate;
1039 }
1040
Juan Quintela338182c2017-05-03 13:16:38 +02001041 if (cpu_throttle_active()) {
1042 info->has_cpu_throttle_percentage = true;
1043 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
1044 }
1045
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +01001046 if (s->state != MIGRATION_STATUS_COMPLETED) {
1047 info->ram->remaining = ram_bytes_remaining();
Juan Quintela93604472017-06-06 19:49:03 +02001048 info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +01001049 }
1050}
1051
Juan Quintela930ac042017-05-04 10:21:46 +02001052static void populate_disk_info(MigrationInfo *info)
1053{
1054 if (blk_mig_active()) {
1055 info->has_disk = true;
1056 info->disk = g_malloc0(sizeof(*info->disk));
1057 info->disk->transferred = blk_mig_bytes_transferred();
1058 info->disk->remaining = blk_mig_bytes_remaining();
1059 info->disk->total = blk_mig_bytes_total();
1060 }
1061}
1062
Alexey Perevalov65ace062018-03-22 21:17:27 +03001063static void fill_source_migration_info(MigrationInfo *info)
aliguori5bb79102008-10-13 03:12:02 +00001064{
Juan Quintela17549e82011-10-05 13:50:43 +02001065 MigrationState *s = migrate_get_current();
Markus Armbruster372043f2021-04-29 16:04:24 +02001066 GSList *cur_blocker = migration_blockers;
aliguori376253e2009-03-05 23:01:23 +00001067
Dr. David Alan Gilbert3af85542021-02-02 13:55:21 +00001068 info->blocked_reasons = NULL;
Dr. David Alan Gilbert3af85542021-02-02 13:55:21 +00001069
Markus Armbruster372043f2021-04-29 16:04:24 +02001070 /*
1071 * There are two types of reasons a migration might be blocked;
1072 * a) devices marked in VMState as non-migratable, and
1073 * b) Explicit migration blockers
1074 * We need to add both of them here.
1075 */
1076 qemu_savevm_non_migratable_list(&info->blocked_reasons);
Dr. David Alan Gilbert3af85542021-02-02 13:55:21 +00001077
Markus Armbruster372043f2021-04-29 16:04:24 +02001078 while (cur_blocker) {
1079 QAPI_LIST_PREPEND(info->blocked_reasons,
1080 g_strdup(error_get_pretty(cur_blocker->data)));
1081 cur_blocker = g_slist_next(cur_blocker);
Dr. David Alan Gilbert3af85542021-02-02 13:55:21 +00001082 }
Markus Armbruster372043f2021-04-29 16:04:24 +02001083 info->has_blocked_reasons = info->blocked_reasons != NULL;
Dr. David Alan Gilbert3af85542021-02-02 13:55:21 +00001084
Juan Quintela17549e82011-10-05 13:50:43 +02001085 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +08001086 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +02001087 /* no migration has happened ever */
Alexey Perevalov65ace062018-03-22 21:17:27 +03001088 /* do not overwrite destination migration status */
1089 return;
zhanghailiang31194732015-03-13 16:08:38 +08001090 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001091 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -04001092 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001093 break;
zhanghailiang31194732015-03-13 16:08:38 +08001094 case MIGRATION_STATUS_ACTIVE:
1095 case MIGRATION_STATUS_CANCELLING:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001096 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +01001097 case MIGRATION_STATUS_PRE_SWITCHOVER:
1098 case MIGRATION_STATUS_DEVICE:
Peter Xua688d2c2018-05-02 18:47:18 +08001099 case MIGRATION_STATUS_POSTCOPY_PAUSED:
Peter Xu135b87b2018-05-02 18:47:25 +08001100 case MIGRATION_STATUS_POSTCOPY_RECOVER:
Wei Yang640dfb12019-07-16 08:54:11 +08001101 /* TODO add some postcopy stats */
1102 populate_time_info(info, s);
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +01001103 populate_ram_info(info, s);
Juan Quintela930ac042017-05-04 10:21:46 +02001104 populate_disk_info(info);
Kirti Wankhede37105862020-10-26 15:06:27 +05301105 populate_vfio_info(info);
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001106 break;
zhanghailiang0b827d52016-10-27 14:42:54 +08001107 case MIGRATION_STATUS_COLO:
1108 info->has_status = true;
1109 /* TODO: display COLO specific information (checkpoint info etc.) */
1110 break;
zhanghailiang31194732015-03-13 16:08:38 +08001111 case MIGRATION_STATUS_COMPLETED:
Wei Yang640dfb12019-07-16 08:54:11 +08001112 populate_time_info(info, s);
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +01001113 populate_ram_info(info, s);
Kirti Wankhede37105862020-10-26 15:06:27 +05301114 populate_vfio_info(info);
Juan Quintela17549e82011-10-05 13:50:43 +02001115 break;
zhanghailiang31194732015-03-13 16:08:38 +08001116 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -03001117 info->has_status = true;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001118 if (s->error) {
1119 info->has_error_desc = true;
1120 info->error_desc = g_strdup(error_get_pretty(s->error));
1121 }
Juan Quintela17549e82011-10-05 13:50:43 +02001122 break;
zhanghailiang31194732015-03-13 16:08:38 +08001123 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -03001124 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +02001125 break;
Jens Freimannc7e0acd2019-10-29 12:49:02 +01001126 case MIGRATION_STATUS_WAIT_UNPLUG:
1127 info->has_status = true;
1128 break;
aliguori5bb79102008-10-13 03:12:02 +00001129 }
zhanghailiangcde63fb2015-03-13 16:08:41 +08001130 info->status = s->state;
aliguori5bb79102008-10-13 03:12:02 +00001131}
1132
Andrey Gruzdev6e8c25b2021-01-29 13:14:03 +03001133typedef enum WriteTrackingSupport {
1134 WT_SUPPORT_UNKNOWN = 0,
1135 WT_SUPPORT_ABSENT,
1136 WT_SUPPORT_AVAILABLE,
1137 WT_SUPPORT_COMPATIBLE
1138} WriteTrackingSupport;
1139
1140static
1141WriteTrackingSupport migrate_query_write_tracking(void)
1142{
1143 /* Check if kernel supports required UFFD features */
1144 if (!ram_write_tracking_available()) {
1145 return WT_SUPPORT_ABSENT;
1146 }
1147 /*
1148 * Check if current memory configuration is
1149 * compatible with required UFFD features.
1150 */
1151 if (!ram_write_tracking_compatible()) {
1152 return WT_SUPPORT_AVAILABLE;
1153 }
1154
1155 return WT_SUPPORT_COMPATIBLE;
1156}
1157
Peter Xu4a842142017-07-18 11:39:08 +08001158/**
1159 * @migration_caps_check - check capability validity
1160 *
1161 * @cap_list: old capability list, array of bool
1162 * @params: new capabilities to be applied soon
1163 * @errp: set *errp if the check failed, with reason
1164 *
1165 * Returns true if check passed, otherwise false.
1166 */
1167static bool migrate_caps_check(bool *cap_list,
1168 MigrationCapabilityStatusList *params,
1169 Error **errp)
Orit Wasserman00458432012-08-06 21:42:48 +03001170{
Orit Wasserman00458432012-08-06 21:42:48 +03001171 MigrationCapabilityStatusList *cap;
Peter Xu4a842142017-07-18 11:39:08 +08001172 bool old_postcopy_cap;
Alexey Perevalovd7651f12017-09-19 19:47:56 +03001173 MigrationIncomingState *mis = migration_incoming_get_current();
Orit Wasserman00458432012-08-06 21:42:48 +03001174
Peter Xu4a842142017-07-18 11:39:08 +08001175 old_postcopy_cap = cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Orit Wasserman00458432012-08-06 21:42:48 +03001176
1177 for (cap = params; cap; cap = cap->next) {
Peter Xu4a842142017-07-18 11:39:08 +08001178 cap_list[cap->value->capability] = cap->value->state;
Orit Wasserman00458432012-08-06 21:42:48 +03001179 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001180
Peter Xu4a842142017-07-18 11:39:08 +08001181#ifndef CONFIG_LIVE_BLOCK_MIGRATION
1182 if (cap_list[MIGRATION_CAPABILITY_BLOCK]) {
1183 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
1184 "block migration");
1185 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
1186 return false;
1187 }
1188#endif
1189
Zhang Chen7e934f52018-11-15 03:09:12 +08001190#ifndef CONFIG_REPLICATION
1191 if (cap_list[MIGRATION_CAPABILITY_X_COLO]) {
1192 error_setg(errp, "QEMU compiled without replication module"
1193 " can't enable COLO");
1194 error_append_hint(errp, "Please enable replication before COLO.\n");
1195 return false;
1196 }
1197#endif
1198
Peter Xu4a842142017-07-18 11:39:08 +08001199 if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +01001200 /* This check is reasonably expensive, so only when it's being
1201 * set the first time, also it's only the destination that needs
1202 * special support.
1203 */
1204 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
Alexey Perevalovd7651f12017-09-19 19:47:56 +03001205 !postcopy_ram_supported_by_host(mis)) {
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +01001206 /* postcopy_ram_supported_by_host will have emitted a more
1207 * detailed message
1208 */
Peter Xu4a842142017-07-18 11:39:08 +08001209 error_setg(errp, "Postcopy is not supported");
1210 return false;
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +01001211 }
Yury Kotov18269062019-02-15 20:45:45 +03001212
1213 if (cap_list[MIGRATION_CAPABILITY_X_IGNORE_SHARED]) {
1214 error_setg(errp, "Postcopy is not compatible with ignore-shared");
1215 return false;
1216 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001217 }
Peter Xu4a842142017-07-18 11:39:08 +08001218
Andrey Gruzdev6e8c25b2021-01-29 13:14:03 +03001219 if (cap_list[MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT]) {
1220 WriteTrackingSupport wt_support;
1221 int idx;
1222 /*
1223 * Check if 'background-snapshot' capability is supported by
1224 * host kernel and compatible with guest memory configuration.
1225 */
1226 wt_support = migrate_query_write_tracking();
1227 if (wt_support < WT_SUPPORT_AVAILABLE) {
1228 error_setg(errp, "Background-snapshot is not supported by host kernel");
1229 return false;
1230 }
1231 if (wt_support < WT_SUPPORT_COMPATIBLE) {
1232 error_setg(errp, "Background-snapshot is not compatible "
1233 "with guest memory configuration");
1234 return false;
1235 }
1236
1237 /*
1238 * Check if there are any migration capabilities
1239 * incompatible with 'background-snapshot'.
1240 */
1241 for (idx = 0; idx < check_caps_background_snapshot.size; idx++) {
1242 int incomp_cap = check_caps_background_snapshot.caps[idx];
1243 if (cap_list[incomp_cap]) {
1244 error_setg(errp,
1245 "Background-snapshot is not compatible with %s",
1246 MigrationCapability_str(incomp_cap));
1247 return false;
1248 }
1249 }
1250 }
1251
Peter Xu4a842142017-07-18 11:39:08 +08001252 return true;
1253}
1254
Alexey Perevalov65ace062018-03-22 21:17:27 +03001255static void fill_destination_migration_info(MigrationInfo *info)
1256{
1257 MigrationIncomingState *mis = migration_incoming_get_current();
1258
Juan Quintela9aca82b2019-02-27 11:51:27 +01001259 if (mis->socket_address_list) {
1260 info->has_socket_address = true;
1261 info->socket_address =
1262 QAPI_CLONE(SocketAddressList, mis->socket_address_list);
1263 }
1264
Alexey Perevalov65ace062018-03-22 21:17:27 +03001265 switch (mis->state) {
1266 case MIGRATION_STATUS_NONE:
1267 return;
Alexey Perevalov65ace062018-03-22 21:17:27 +03001268 case MIGRATION_STATUS_SETUP:
1269 case MIGRATION_STATUS_CANCELLING:
1270 case MIGRATION_STATUS_CANCELLED:
1271 case MIGRATION_STATUS_ACTIVE:
1272 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Peter Xu3c9928d2018-07-10 17:18:56 +08001273 case MIGRATION_STATUS_POSTCOPY_PAUSED:
1274 case MIGRATION_STATUS_POSTCOPY_RECOVER:
Alexey Perevalov65ace062018-03-22 21:17:27 +03001275 case MIGRATION_STATUS_FAILED:
1276 case MIGRATION_STATUS_COLO:
1277 info->has_status = true;
1278 break;
1279 case MIGRATION_STATUS_COMPLETED:
1280 info->has_status = true;
1281 fill_destination_postcopy_migration_info(info);
1282 break;
1283 }
1284 info->status = mis->state;
1285}
1286
1287MigrationInfo *qmp_query_migrate(Error **errp)
1288{
1289 MigrationInfo *info = g_malloc0(sizeof(*info));
1290
1291 fill_destination_migration_info(info);
1292 fill_source_migration_info(info);
1293
1294 return info;
1295}
1296
Peter Xu4a842142017-07-18 11:39:08 +08001297void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
1298 Error **errp)
1299{
1300 MigrationState *s = migrate_get_current();
1301 MigrationCapabilityStatusList *cap;
Peter Xudd0ee302018-03-05 17:49:38 +08001302 bool cap_list[MIGRATION_CAPABILITY__MAX];
Peter Xu4a842142017-07-18 11:39:08 +08001303
Juan Quintela392d87e2020-01-21 15:39:23 +01001304 if (migration_is_running(s->state)) {
Peter Xu4a842142017-07-18 11:39:08 +08001305 error_setg(errp, QERR_MIGRATION_ACTIVE);
1306 return;
1307 }
1308
Peter Xudd0ee302018-03-05 17:49:38 +08001309 memcpy(cap_list, s->enabled_capabilities, sizeof(cap_list));
1310 if (!migrate_caps_check(cap_list, params, errp)) {
Peter Xu4a842142017-07-18 11:39:08 +08001311 return;
1312 }
1313
1314 for (cap = params; cap; cap = cap->next) {
1315 s->enabled_capabilities[cap->value->capability] = cap->value->state;
1316 }
Orit Wasserman00458432012-08-06 21:42:48 +03001317}
1318
Peter Xu16d063b2017-07-18 11:39:04 +08001319/*
1320 * Check whether the parameters are valid. Error will be put into errp
1321 * (if provided). Return true if valid, otherwise false.
1322 */
1323static bool migrate_params_check(MigrationParameters *params, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +08001324{
Eric Blake7f375e02016-09-08 22:14:16 -05001325 if (params->has_compress_level &&
Juan Quintela741d4082017-12-01 13:08:38 +01001326 (params->compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001327 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
Markus Armbruster54270c42021-02-02 15:17:34 +01001328 "a value between 0 and 9");
Peter Xu16d063b2017-07-18 11:39:04 +08001329 return false;
Liang Li85de8322015-03-23 16:32:28 +08001330 }
Peter Xu16d063b2017-07-18 11:39:04 +08001331
Juan Quintela741d4082017-12-01 13:08:38 +01001332 if (params->has_compress_threads && (params->compress_threads < 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001333 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1334 "compress_threads",
Markus Armbruster54270c42021-02-02 15:17:34 +01001335 "a value between 1 and 255");
Peter Xu16d063b2017-07-18 11:39:04 +08001336 return false;
Liang Li85de8322015-03-23 16:32:28 +08001337 }
Peter Xu16d063b2017-07-18 11:39:04 +08001338
Juan Quintela741d4082017-12-01 13:08:38 +01001339 if (params->has_decompress_threads && (params->decompress_threads < 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001340 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1341 "decompress_threads",
Markus Armbruster54270c42021-02-02 15:17:34 +01001342 "a value between 1 and 255");
Peter Xu16d063b2017-07-18 11:39:04 +08001343 return false;
Liang Li85de8322015-03-23 16:32:28 +08001344 }
Peter Xu16d063b2017-07-18 11:39:04 +08001345
Keqian Zhudc14a472020-02-24 10:31:42 +08001346 if (params->has_throttle_trigger_threshold &&
1347 (params->throttle_trigger_threshold < 1 ||
1348 params->throttle_trigger_threshold > 100)) {
1349 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1350 "throttle_trigger_threshold",
1351 "an integer in the range of 1 to 100");
1352 return false;
1353 }
1354
Eric Blake7f375e02016-09-08 22:14:16 -05001355 if (params->has_cpu_throttle_initial &&
1356 (params->cpu_throttle_initial < 1 ||
1357 params->cpu_throttle_initial > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -04001358 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -04001359 "cpu_throttle_initial",
Jason J. Herne1626fee2015-09-08 13:12:34 -04001360 "an integer in the range of 1 to 99");
Peter Xu16d063b2017-07-18 11:39:04 +08001361 return false;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001362 }
Peter Xu16d063b2017-07-18 11:39:04 +08001363
Eric Blake7f375e02016-09-08 22:14:16 -05001364 if (params->has_cpu_throttle_increment &&
1365 (params->cpu_throttle_increment < 1 ||
1366 params->cpu_throttle_increment > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -04001367 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -04001368 "cpu_throttle_increment",
Jason J. Herne1626fee2015-09-08 13:12:34 -04001369 "an integer in the range of 1 to 99");
Peter Xu16d063b2017-07-18 11:39:04 +08001370 return false;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001371 }
Peter Xu16d063b2017-07-18 11:39:04 +08001372
Juan Quintela741d4082017-12-01 13:08:38 +01001373 if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) {
Mao Zhongyi2ee30cf2020-03-31 16:22:05 +08001374 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1375 "max_bandwidth",
1376 "an integer in the range of 0 to "stringify(SIZE_MAX)
1377 " bytes/second");
Peter Xu16d063b2017-07-18 11:39:04 +08001378 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301379 }
Peter Xu16d063b2017-07-18 11:39:04 +08001380
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301381 if (params->has_downtime_limit &&
Juan Quintela741d4082017-12-01 13:08:38 +01001382 (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
Mao Zhongyi2ee30cf2020-03-31 16:22:05 +08001383 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1384 "downtime_limit",
1385 "an integer in the range of 0 to "
Mao Zhongyi7ac55292020-03-31 16:22:07 +08001386 stringify(MAX_MIGRATE_DOWNTIME)" ms");
Peter Xu16d063b2017-07-18 11:39:04 +08001387 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301388 }
Peter Xu16d063b2017-07-18 11:39:04 +08001389
Juan Quintela741d4082017-12-01 13:08:38 +01001390 /* x_checkpoint_delay is now always positive */
1391
Juan Quintelacbfd6c92019-02-06 13:54:06 +01001392 if (params->has_multifd_channels && (params->multifd_channels < 1)) {
Juan Quintela4075fb12016-01-15 08:56:17 +01001393 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1394 "multifd_channels",
Markus Armbruster54270c42021-02-02 15:17:34 +01001395 "a value between 1 and 255");
Juan Quintela4075fb12016-01-15 08:56:17 +01001396 return false;
1397 }
Peter Xu16d063b2017-07-18 11:39:04 +08001398
Juan Quintela9004db42020-01-23 17:08:52 +01001399 if (params->has_multifd_zlib_level &&
1400 (params->multifd_zlib_level > 9)) {
1401 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zlib_level",
Markus Armbruster54270c42021-02-02 15:17:34 +01001402 "a value between 0 and 9");
Juan Quintela9004db42020-01-23 17:08:52 +01001403 return false;
1404 }
1405
Juan Quintela6a9ad152020-01-23 17:41:36 +01001406 if (params->has_multifd_zstd_level &&
1407 (params->multifd_zstd_level > 20)) {
1408 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zstd_level",
Markus Armbruster54270c42021-02-02 15:17:34 +01001409 "a value between 0 and 20");
Juan Quintela6a9ad152020-01-23 17:41:36 +01001410 return false;
1411 }
1412
Juan Quintela73af8dd2017-10-05 21:30:10 +02001413 if (params->has_xbzrle_cache_size &&
1414 (params->xbzrle_cache_size < qemu_target_page_size() ||
1415 !is_power_of_2(params->xbzrle_cache_size))) {
1416 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1417 "xbzrle_cache_size",
Markus Armbruster54270c42021-02-02 15:17:34 +01001418 "a power of two no less than the target page size");
Juan Quintela73af8dd2017-10-05 21:30:10 +02001419 return false;
1420 }
1421
Li Qiang4cbc9c72018-08-01 06:00:20 -07001422 if (params->has_max_cpu_throttle &&
1423 (params->max_cpu_throttle < params->cpu_throttle_initial ||
1424 params->max_cpu_throttle > 99)) {
1425 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1426 "max_cpu_throttle",
1427 "an integer in the range of cpu_throttle_initial to 99");
1428 return false;
1429 }
1430
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00001431 if (params->has_announce_initial &&
1432 params->announce_initial > 100000) {
1433 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1434 "announce_initial",
Markus Armbruster54270c42021-02-02 15:17:34 +01001435 "a value between 0 and 100000");
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00001436 return false;
1437 }
1438 if (params->has_announce_max &&
1439 params->announce_max > 100000) {
1440 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1441 "announce_max",
Markus Armbruster54270c42021-02-02 15:17:34 +01001442 "a value between 0 and 100000");
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00001443 return false;
1444 }
1445 if (params->has_announce_rounds &&
1446 params->announce_rounds > 1000) {
1447 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1448 "announce_rounds",
Markus Armbruster54270c42021-02-02 15:17:34 +01001449 "a value between 0 and 1000");
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00001450 return false;
1451 }
1452 if (params->has_announce_step &&
1453 (params->announce_step < 1 ||
1454 params->announce_step > 10000)) {
1455 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1456 "announce_step",
Markus Armbruster54270c42021-02-02 15:17:34 +01001457 "a value between 0 and 10000");
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00001458 return false;
1459 }
Max Reitz31e4c352020-08-20 17:07:23 +02001460
1461 if (params->has_block_bitmap_mapping &&
1462 !check_dirty_bitmap_mig_alias_map(params->block_bitmap_mapping, errp)) {
1463 error_prepend(errp, "Invalid mapping given for block-bitmap-mapping: ");
1464 return false;
1465 }
1466
Peter Xu16d063b2017-07-18 11:39:04 +08001467 return true;
1468}
1469
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001470static void migrate_params_test_apply(MigrateSetParameters *params,
1471 MigrationParameters *dest)
1472{
1473 *dest = migrate_get_current()->parameters;
1474
1475 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1476
1477 if (params->has_compress_level) {
1478 dest->compress_level = params->compress_level;
1479 }
1480
1481 if (params->has_compress_threads) {
1482 dest->compress_threads = params->compress_threads;
1483 }
1484
Xiao Guangrong1d588722018-08-21 16:10:20 +08001485 if (params->has_compress_wait_thread) {
1486 dest->compress_wait_thread = params->compress_wait_thread;
1487 }
1488
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001489 if (params->has_decompress_threads) {
1490 dest->decompress_threads = params->decompress_threads;
1491 }
1492
Keqian Zhudc14a472020-02-24 10:31:42 +08001493 if (params->has_throttle_trigger_threshold) {
1494 dest->throttle_trigger_threshold = params->throttle_trigger_threshold;
1495 }
1496
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001497 if (params->has_cpu_throttle_initial) {
1498 dest->cpu_throttle_initial = params->cpu_throttle_initial;
1499 }
1500
1501 if (params->has_cpu_throttle_increment) {
1502 dest->cpu_throttle_increment = params->cpu_throttle_increment;
1503 }
1504
Keqian Zhucbbf8182020-04-13 18:15:08 +08001505 if (params->has_cpu_throttle_tailslow) {
1506 dest->cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1507 }
1508
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001509 if (params->has_tls_creds) {
Markus Armbruster01fa5592017-07-18 14:42:04 +02001510 assert(params->tls_creds->type == QTYPE_QSTRING);
Zheng Chuan9728ebf2020-07-09 16:28:25 +08001511 dest->tls_creds = params->tls_creds->u.s;
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001512 }
1513
1514 if (params->has_tls_hostname) {
Markus Armbruster01fa5592017-07-18 14:42:04 +02001515 assert(params->tls_hostname->type == QTYPE_QSTRING);
Zheng Chuan9728ebf2020-07-09 16:28:25 +08001516 dest->tls_hostname = params->tls_hostname->u.s;
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001517 }
1518
1519 if (params->has_max_bandwidth) {
1520 dest->max_bandwidth = params->max_bandwidth;
1521 }
1522
1523 if (params->has_downtime_limit) {
1524 dest->downtime_limit = params->downtime_limit;
1525 }
1526
1527 if (params->has_x_checkpoint_delay) {
1528 dest->x_checkpoint_delay = params->x_checkpoint_delay;
1529 }
1530
1531 if (params->has_block_incremental) {
1532 dest->block_incremental = params->block_incremental;
1533 }
Juan Quintelacbfd6c92019-02-06 13:54:06 +01001534 if (params->has_multifd_channels) {
1535 dest->multifd_channels = params->multifd_channels;
Juan Quintela5e7577a2017-10-09 18:07:56 +02001536 }
Juan Quintela96eef042019-01-16 10:35:55 +01001537 if (params->has_multifd_compression) {
1538 dest->multifd_compression = params->multifd_compression;
1539 }
Juan Quintela73af8dd2017-10-05 21:30:10 +02001540 if (params->has_xbzrle_cache_size) {
1541 dest->xbzrle_cache_size = params->xbzrle_cache_size;
1542 }
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01001543 if (params->has_max_postcopy_bandwidth) {
1544 dest->max_postcopy_bandwidth = params->max_postcopy_bandwidth;
1545 }
Li Qiang4cbc9c72018-08-01 06:00:20 -07001546 if (params->has_max_cpu_throttle) {
1547 dest->max_cpu_throttle = params->max_cpu_throttle;
1548 }
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00001549 if (params->has_announce_initial) {
1550 dest->announce_initial = params->announce_initial;
1551 }
1552 if (params->has_announce_max) {
1553 dest->announce_max = params->announce_max;
1554 }
1555 if (params->has_announce_rounds) {
1556 dest->announce_rounds = params->announce_rounds;
1557 }
1558 if (params->has_announce_step) {
1559 dest->announce_step = params->announce_step;
1560 }
Max Reitz31e4c352020-08-20 17:07:23 +02001561
1562 if (params->has_block_bitmap_mapping) {
1563 dest->has_block_bitmap_mapping = true;
1564 dest->block_bitmap_mapping = params->block_bitmap_mapping;
1565 }
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001566}
1567
Juan Quintela73af8dd2017-10-05 21:30:10 +02001568static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
Peter Xu16d063b2017-07-18 11:39:04 +08001569{
1570 MigrationState *s = migrate_get_current();
1571
Markus Armbrustere87fae42017-07-18 12:57:38 +02001572 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1573
Eric Blake7f375e02016-09-08 22:14:16 -05001574 if (params->has_compress_level) {
1575 s->parameters.compress_level = params->compress_level;
Liang Li85de8322015-03-23 16:32:28 +08001576 }
Peter Xu476c72a2017-07-18 11:39:05 +08001577
Eric Blake7f375e02016-09-08 22:14:16 -05001578 if (params->has_compress_threads) {
1579 s->parameters.compress_threads = params->compress_threads;
Liang Li85de8322015-03-23 16:32:28 +08001580 }
Peter Xu476c72a2017-07-18 11:39:05 +08001581
Xiao Guangrong1d588722018-08-21 16:10:20 +08001582 if (params->has_compress_wait_thread) {
1583 s->parameters.compress_wait_thread = params->compress_wait_thread;
1584 }
1585
Eric Blake7f375e02016-09-08 22:14:16 -05001586 if (params->has_decompress_threads) {
1587 s->parameters.decompress_threads = params->decompress_threads;
Liang Li85de8322015-03-23 16:32:28 +08001588 }
Peter Xu476c72a2017-07-18 11:39:05 +08001589
Keqian Zhudc14a472020-02-24 10:31:42 +08001590 if (params->has_throttle_trigger_threshold) {
1591 s->parameters.throttle_trigger_threshold = params->throttle_trigger_threshold;
1592 }
1593
Eric Blake7f375e02016-09-08 22:14:16 -05001594 if (params->has_cpu_throttle_initial) {
1595 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001596 }
Peter Xu476c72a2017-07-18 11:39:05 +08001597
Eric Blake7f375e02016-09-08 22:14:16 -05001598 if (params->has_cpu_throttle_increment) {
1599 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001600 }
Peter Xu476c72a2017-07-18 11:39:05 +08001601
Keqian Zhucbbf8182020-04-13 18:15:08 +08001602 if (params->has_cpu_throttle_tailslow) {
1603 s->parameters.cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1604 }
1605
Eric Blake7f375e02016-09-08 22:14:16 -05001606 if (params->has_tls_creds) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001607 g_free(s->parameters.tls_creds);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001608 assert(params->tls_creds->type == QTYPE_QSTRING);
1609 s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001610 }
Peter Xu476c72a2017-07-18 11:39:05 +08001611
Eric Blake7f375e02016-09-08 22:14:16 -05001612 if (params->has_tls_hostname) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001613 g_free(s->parameters.tls_hostname);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001614 assert(params->tls_hostname->type == QTYPE_QSTRING);
1615 s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001616 }
Peter Xu476c72a2017-07-18 11:39:05 +08001617
Daniel P. Berranged2f1d292019-02-27 14:53:24 +00001618 if (params->has_tls_authz) {
1619 g_free(s->parameters.tls_authz);
1620 assert(params->tls_authz->type == QTYPE_QSTRING);
1621 s->parameters.tls_authz = g_strdup(params->tls_authz->u.s);
1622 }
1623
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301624 if (params->has_max_bandwidth) {
1625 s->parameters.max_bandwidth = params->max_bandwidth;
Dr. David Alan Gilbertc38c1c12019-03-08 10:12:10 +00001626 if (s->to_dst_file && !migration_in_postcopy()) {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301627 qemu_file_set_rate_limit(s->to_dst_file,
1628 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
1629 }
1630 }
Peter Xu476c72a2017-07-18 11:39:05 +08001631
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301632 if (params->has_downtime_limit) {
1633 s->parameters.downtime_limit = params->downtime_limit;
1634 }
zhanghailiang68b53592016-10-27 14:43:01 +08001635
1636 if (params->has_x_checkpoint_delay) {
1637 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
zhanghailiang479125d2017-01-17 20:57:42 +08001638 if (migration_in_colo_state()) {
1639 colo_checkpoint_notify(s);
1640 }
zhanghailiang68b53592016-10-27 14:43:01 +08001641 }
Peter Xu476c72a2017-07-18 11:39:05 +08001642
Juan Quintela2833c592017-04-05 18:32:37 +02001643 if (params->has_block_incremental) {
1644 s->parameters.block_incremental = params->block_incremental;
1645 }
Juan Quintelacbfd6c92019-02-06 13:54:06 +01001646 if (params->has_multifd_channels) {
1647 s->parameters.multifd_channels = params->multifd_channels;
Juan Quintela4075fb12016-01-15 08:56:17 +01001648 }
Juan Quintela96eef042019-01-16 10:35:55 +01001649 if (params->has_multifd_compression) {
1650 s->parameters.multifd_compression = params->multifd_compression;
1651 }
Juan Quintela73af8dd2017-10-05 21:30:10 +02001652 if (params->has_xbzrle_cache_size) {
1653 s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
1654 xbzrle_cache_resize(params->xbzrle_cache_size, errp);
1655 }
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01001656 if (params->has_max_postcopy_bandwidth) {
1657 s->parameters.max_postcopy_bandwidth = params->max_postcopy_bandwidth;
Dr. David Alan Gilbertc38c1c12019-03-08 10:12:10 +00001658 if (s->to_dst_file && migration_in_postcopy()) {
1659 qemu_file_set_rate_limit(s->to_dst_file,
1660 s->parameters.max_postcopy_bandwidth / XFER_LIMIT_RATIO);
1661 }
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01001662 }
Li Qiang4cbc9c72018-08-01 06:00:20 -07001663 if (params->has_max_cpu_throttle) {
1664 s->parameters.max_cpu_throttle = params->max_cpu_throttle;
1665 }
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00001666 if (params->has_announce_initial) {
1667 s->parameters.announce_initial = params->announce_initial;
1668 }
1669 if (params->has_announce_max) {
1670 s->parameters.announce_max = params->announce_max;
1671 }
1672 if (params->has_announce_rounds) {
1673 s->parameters.announce_rounds = params->announce_rounds;
1674 }
1675 if (params->has_announce_step) {
1676 s->parameters.announce_step = params->announce_step;
1677 }
Max Reitz31e4c352020-08-20 17:07:23 +02001678
1679 if (params->has_block_bitmap_mapping) {
1680 qapi_free_BitmapMigrationNodeAliasList(
1681 s->parameters.block_bitmap_mapping);
1682
1683 s->parameters.has_block_bitmap_mapping = true;
1684 s->parameters.block_bitmap_mapping =
1685 QAPI_CLONE(BitmapMigrationNodeAliasList,
1686 params->block_bitmap_mapping);
1687 }
Liang Li85de8322015-03-23 16:32:28 +08001688}
1689
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001690void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
Peter Xu476c72a2017-07-18 11:39:05 +08001691{
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001692 MigrationParameters tmp;
1693
Markus Armbruster01fa5592017-07-18 14:42:04 +02001694 /* TODO Rewrite "" to null instead */
1695 if (params->has_tls_creds
1696 && params->tls_creds->type == QTYPE_QNULL) {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001697 qobject_unref(params->tls_creds->u.n);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001698 params->tls_creds->type = QTYPE_QSTRING;
1699 params->tls_creds->u.s = strdup("");
1700 }
1701 /* TODO Rewrite "" to null instead */
1702 if (params->has_tls_hostname
1703 && params->tls_hostname->type == QTYPE_QNULL) {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001704 qobject_unref(params->tls_hostname->u.n);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001705 params->tls_hostname->type = QTYPE_QSTRING;
1706 params->tls_hostname->u.s = strdup("");
1707 }
1708
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001709 migrate_params_test_apply(params, &tmp);
1710
1711 if (!migrate_params_check(&tmp, errp)) {
Peter Xu476c72a2017-07-18 11:39:05 +08001712 /* Invalid parameter */
1713 return;
1714 }
1715
Juan Quintela73af8dd2017-10-05 21:30:10 +02001716 migrate_params_apply(params, errp);
Peter Xu476c72a2017-07-18 11:39:05 +08001717}
1718
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001719
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001720void qmp_migrate_start_postcopy(Error **errp)
1721{
1722 MigrationState *s = migrate_get_current();
1723
Vladimir Sementsov-Ogievskiy16b0fd32018-03-13 15:34:01 -04001724 if (!migrate_postcopy()) {
Dr. David Alan Gilberta54d3402015-11-12 11:34:44 +00001725 error_setg(errp, "Enable postcopy with migrate_set_capability before"
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001726 " the start of migration");
1727 return;
1728 }
1729
1730 if (s->state == MIGRATION_STATUS_NONE) {
1731 error_setg(errp, "Postcopy must be started after migration has been"
1732 " started");
1733 return;
1734 }
1735 /*
1736 * we don't error if migration has finished since that would be racy
1737 * with issuing this command.
1738 */
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001739 qatomic_set(&s->start_postcopy, true);
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001740}
1741
aliguori065e2812008-11-11 16:46:33 +00001742/* shared migration helpers */
1743
zhanghailiang48781e52015-12-16 11:47:33 +00001744void migrate_set_state(int *state, int old_state, int new_state)
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +00001745{
Peter Xua31fede2017-08-30 16:32:01 +08001746 assert(new_state < MIGRATION_STATUS__MAX);
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001747 if (qatomic_cmpxchg(state, old_state, new_state) == old_state) {
Peter Xua31fede2017-08-30 16:32:01 +08001748 trace_migrate_set_state(MigrationStatus_str(new_state));
Juan Quintelab05dc722015-07-07 14:44:05 +02001749 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +00001750 }
1751}
1752
Eric Blakeeaedde52020-11-12 19:13:36 -06001753static MigrationCapabilityStatus *migrate_cap_add(MigrationCapability index,
1754 bool state)
Juan Quintela2833c592017-04-05 18:32:37 +02001755{
Eric Blakeeaedde52020-11-12 19:13:36 -06001756 MigrationCapabilityStatus *cap;
Juan Quintela2833c592017-04-05 18:32:37 +02001757
Eric Blakeeaedde52020-11-12 19:13:36 -06001758 cap = g_new0(MigrationCapabilityStatus, 1);
1759 cap->capability = index;
1760 cap->state = state;
Peter Xu4e4a3d32017-07-18 11:39:09 +08001761
1762 return cap;
1763}
1764
1765void migrate_set_block_enabled(bool value, Error **errp)
1766{
Eric Blakeeaedde52020-11-12 19:13:36 -06001767 MigrationCapabilityStatusList *cap = NULL;
Peter Xu4e4a3d32017-07-18 11:39:09 +08001768
Eric Blakeeaedde52020-11-12 19:13:36 -06001769 QAPI_LIST_PREPEND(cap, migrate_cap_add(MIGRATION_CAPABILITY_BLOCK, value));
Juan Quintela2833c592017-04-05 18:32:37 +02001770 qmp_migrate_set_capabilities(cap, errp);
1771 qapi_free_MigrationCapabilityStatusList(cap);
1772}
1773
1774static void migrate_set_block_incremental(MigrationState *s, bool value)
1775{
1776 s->parameters.block_incremental = value;
1777}
1778
1779static void block_cleanup_parameters(MigrationState *s)
1780{
1781 if (s->must_remove_block_options) {
1782 /* setting to false can never fail */
1783 migrate_set_block_enabled(false, &error_abort);
1784 migrate_set_block_incremental(s, false);
1785 s->must_remove_block_options = false;
1786 }
1787}
1788
Yury Kotovfd392cf2019-04-08 14:33:43 +03001789static void migrate_fd_cleanup(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +00001790{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001791 qemu_bh_delete(s->cleanup_bh);
1792 s->cleanup_bh = NULL;
1793
Peter Xu0ceccd82018-01-03 20:20:06 +08001794 qemu_savevm_state_cleanup();
1795
zhanghailiang89a02a92016-01-15 11:37:42 +08001796 if (s->to_dst_file) {
Peter Xu62df0662018-05-02 18:47:38 +08001797 QEMUFile *tmp;
Juan Quintelaf986c3d2016-01-14 16:52:55 +01001798
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001799 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001800 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001801 if (s->migration_thread_running) {
1802 qemu_thread_join(&s->thread);
1803 s->migration_thread_running = false;
1804 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001805 qemu_mutex_lock_iothread();
1806
Fei Li1398b2e2019-01-13 22:08:47 +08001807 multifd_save_cleanup();
Peter Xu62df0662018-05-02 18:47:38 +08001808 qemu_mutex_lock(&s->qemu_file_lock);
1809 tmp = s->to_dst_file;
zhanghailiang89a02a92016-01-15 11:37:42 +08001810 s->to_dst_file = NULL;
Peter Xu62df0662018-05-02 18:47:38 +08001811 qemu_mutex_unlock(&s->qemu_file_lock);
1812 /*
1813 * Close the file handle without the lock to make sure the
1814 * critical section won't block for long.
1815 */
Peter Xu39675ff2021-07-22 13:58:41 -04001816 migration_ioc_unregister_yank_from_file(tmp);
Peter Xu62df0662018-05-02 18:47:38 +08001817 qemu_fclose(tmp);
aliguori065e2812008-11-11 16:46:33 +00001818 }
1819
Wei Yang8f8d5282019-07-17 08:53:41 +08001820 assert(!migration_is_active(s));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001821
Liang Li94f5a432015-11-02 15:37:00 +08001822 if (s->state == MIGRATION_STATUS_CANCELLING) {
zhanghailiang48781e52015-12-16 11:47:33 +00001823 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
Liang Li94f5a432015-11-02 15:37:00 +08001824 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001825 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001826
Juan Quintela87db1a72017-09-05 12:50:22 +02001827 if (s->error) {
1828 /* It is used on info migrate. We can't free it */
1829 error_report_err(error_copy(s->error));
1830 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001831 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +02001832 block_cleanup_parameters(s);
Lukas Straubb5eea992020-12-28 16:08:52 +01001833 yank_unregister_instance(MIGRATION_YANK_INSTANCE);
aliguori065e2812008-11-11 16:46:33 +00001834}
1835
Yury Kotovfd392cf2019-04-08 14:33:43 +03001836static void migrate_fd_cleanup_schedule(MigrationState *s)
1837{
1838 /*
1839 * Ref the state for bh, because it may be called when
1840 * there're already no other refs
1841 */
1842 object_ref(OBJECT(s));
1843 qemu_bh_schedule(s->cleanup_bh);
1844}
1845
1846static void migrate_fd_cleanup_bh(void *opaque)
1847{
1848 MigrationState *s = opaque;
1849 migrate_fd_cleanup(s);
1850 object_unref(OBJECT(s));
1851}
1852
Juan Quintela87db1a72017-09-05 12:50:22 +02001853void migrate_set_error(MigrationState *s, const Error *error)
1854{
Daniel Brodsky6e8a3552020-04-03 21:21:08 -07001855 QEMU_LOCK_GUARD(&s->error_mutex);
Juan Quintela87db1a72017-09-05 12:50:22 +02001856 if (!s->error) {
1857 s->error = error_copy(error);
1858 }
Juan Quintela87db1a72017-09-05 12:50:22 +02001859}
1860
Peter Xuca7bd082021-07-08 15:06:53 -04001861static void migrate_error_free(MigrationState *s)
1862{
1863 QEMU_LOCK_GUARD(&s->error_mutex);
1864 if (s->error) {
1865 error_free(s->error);
1866 s->error = NULL;
1867 }
1868}
1869
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001870void migrate_fd_error(MigrationState *s, const Error *error)
aliguori065e2812008-11-11 16:46:33 +00001871{
Peter Maydell25174052016-10-21 18:41:45 +01001872 trace_migrate_fd_error(error_get_pretty(error));
zhanghailiang89a02a92016-01-15 11:37:42 +08001873 assert(s->to_dst_file == NULL);
zhanghailiang48781e52015-12-16 11:47:33 +00001874 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1875 MIGRATION_STATUS_FAILED);
Juan Quintela87db1a72017-09-05 12:50:22 +02001876 migrate_set_error(s, error);
Juan Quintela458cf282011-02-22 23:32:54 +01001877}
1878
Juan Quintela0edda1c2010-05-11 16:28:39 +02001879static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +00001880{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001881 int old_state ;
zhanghailiang89a02a92016-01-15 11:37:42 +08001882 QEMUFile *f = migrate_get_current()->to_dst_file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001883 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +00001884
Peter Xu43044ac2021-07-22 13:58:38 -04001885 WITH_QEMU_LOCK_GUARD(&s->qemu_file_lock) {
1886 if (s->rp_state.from_dst_file) {
1887 /* shutdown the rp socket, so causing the rp thread to shutdown */
1888 qemu_file_shutdown(s->rp_state.from_dst_file);
1889 }
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001890 }
1891
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001892 do {
1893 old_state = s->state;
Juan Quintela392d87e2020-01-21 15:39:23 +01001894 if (!migration_is_running(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001895 break;
1896 }
Dr. David Alan Gilberta7b36b42017-10-20 10:05:55 +01001897 /* If the migration is paused, kick it out of the pause */
1898 if (old_state == MIGRATION_STATUS_PRE_SWITCHOVER) {
1899 qemu_sem_post(&s->pause_sem);
1900 }
zhanghailiang48781e52015-12-16 11:47:33 +00001901 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
zhanghailiang31194732015-03-13 16:08:38 +08001902 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001903
1904 /*
1905 * If we're unlucky the migration code might be stuck somewhere in a
1906 * send/write while the network has failed and is waiting to timeout;
1907 * if we've got shutdown(2) available then we can force it to quit.
1908 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1909 * called in a bh, so there is no race against this cancel.
1910 */
zhanghailiang31194732015-03-13 16:08:38 +08001911 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001912 qemu_file_shutdown(f);
1913 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08001914 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
1915 Error *local_err = NULL;
1916
1917 bdrv_invalidate_cache_all(&local_err);
1918 if (local_err) {
1919 error_report_err(local_err);
1920 } else {
1921 s->block_inactive = false;
1922 }
1923 }
aliguori065e2812008-11-11 16:46:33 +00001924}
1925
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001926void add_migration_state_change_notifier(Notifier *notify)
1927{
1928 notifier_list_add(&migration_state_notifiers, notify);
1929}
1930
1931void remove_migration_state_change_notifier(Notifier *notify)
1932{
Paolo Bonzini31552522012-01-13 17:34:01 +01001933 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001934}
1935
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +02001936bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001937{
zhanghailiang31194732015-03-13 16:08:38 +08001938 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001939}
1940
Juan Quintela70736932011-02-23 00:43:59 +01001941bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001942{
zhanghailiang31194732015-03-13 16:08:38 +08001943 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001944}
Juan Quintela0edda1c2010-05-11 16:28:39 +02001945
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001946bool migration_has_failed(MigrationState *s)
1947{
zhanghailiang31194732015-03-13 16:08:38 +08001948 return (s->state == MIGRATION_STATUS_CANCELLED ||
1949 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001950}
1951
Juan Quintela57273092017-03-20 22:25:28 +01001952bool migration_in_postcopy(void)
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001953{
Juan Quintela57273092017-03-20 22:25:28 +01001954 MigrationState *s = migrate_get_current();
1955
Dr. David Alan Gilbert3748fef2019-09-23 18:49:42 +01001956 switch (s->state) {
1957 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1958 case MIGRATION_STATUS_POSTCOPY_PAUSED:
1959 case MIGRATION_STATUS_POSTCOPY_RECOVER:
1960 return true;
1961 default:
1962 return false;
1963 }
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001964}
1965
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001966bool migration_in_postcopy_after_devices(MigrationState *s)
1967{
Juan Quintela57273092017-03-20 22:25:28 +01001968 return migration_in_postcopy() && s->postcopy_after_devices;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001969}
1970
David Hildenbrand06df2e62020-06-26 09:22:33 +02001971bool migration_in_incoming_postcopy(void)
1972{
1973 PostcopyState ps = postcopy_state_get();
1974
1975 return ps >= POSTCOPY_INCOMING_DISCARD && ps < POSTCOPY_INCOMING_END;
1976}
1977
Andrey Gruzdev1a8e44a2021-04-01 12:22:24 +03001978bool migration_in_bg_snapshot(void)
1979{
1980 MigrationState *s = migrate_get_current();
1981
1982 return migrate_background_snapshot() &&
1983 migration_is_setup_or_active(s->state);
1984}
1985
Juan Quintelafab35002017-03-22 17:36:57 +01001986bool migration_is_idle(void)
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301987{
Markus Armbrusterdaff7f02019-04-01 11:08:25 +02001988 MigrationState *s = current_migration;
1989
1990 if (!s) {
1991 return true;
1992 }
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301993
1994 switch (s->state) {
1995 case MIGRATION_STATUS_NONE:
1996 case MIGRATION_STATUS_CANCELLED:
1997 case MIGRATION_STATUS_COMPLETED:
1998 case MIGRATION_STATUS_FAILED:
1999 return true;
2000 case MIGRATION_STATUS_SETUP:
2001 case MIGRATION_STATUS_CANCELLING:
2002 case MIGRATION_STATUS_ACTIVE:
2003 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
2004 case MIGRATION_STATUS_COLO:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +01002005 case MIGRATION_STATUS_PRE_SWITCHOVER:
2006 case MIGRATION_STATUS_DEVICE:
Jens Freimannc7e0acd2019-10-29 12:49:02 +01002007 case MIGRATION_STATUS_WAIT_UNPLUG:
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05302008 return false;
2009 case MIGRATION_STATUS__MAX:
2010 g_assert_not_reached();
2011 }
2012
2013 return false;
2014}
2015
Wei Yang8f8d5282019-07-17 08:53:41 +08002016bool migration_is_active(MigrationState *s)
2017{
2018 return (s->state == MIGRATION_STATUS_ACTIVE ||
2019 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
2020}
2021
Peter Xu3e0c8052018-02-08 18:31:15 +08002022void migrate_init(MigrationState *s)
Juan Quintela0edda1c2010-05-11 16:28:39 +02002023{
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00002024 /*
2025 * Reinitialise all migration state, except
2026 * parameters/capabilities that the user set, and
2027 * locks.
2028 */
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00002029 s->cleanup_bh = 0;
Andrey Gruzdev85182782021-01-29 13:14:06 +03002030 s->vm_start_bh = 0;
zhanghailiang89a02a92016-01-15 11:37:42 +08002031 s->to_dst_file = NULL;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00002032 s->state = MIGRATION_STATUS_NONE;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00002033 s->rp_state.from_dst_file = NULL;
2034 s->rp_state.error = false;
2035 s->mbps = 0.0;
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08002036 s->pages_per_second = 0.0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00002037 s->downtime = 0;
2038 s->expected_downtime = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00002039 s->setup_time = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00002040 s->start_postcopy = false;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00002041 s->postcopy_after_devices = false;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00002042 s->migration_thread_running = false;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01002043 error_free(s->error);
2044 s->error = NULL;
Chuan Zhengd8053e72020-09-15 11:03:57 +08002045 s->hostname = NULL;
Juan Quintela1299c632011-11-09 21:29:01 +01002046
zhanghailiang48781e52015-12-16 11:47:33 +00002047 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +02002048
Peter Xu4af246a2018-01-03 20:20:08 +08002049 s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2050 s->total_time = 0;
Peter Xu7287cbd2018-01-03 20:20:09 +08002051 s->vm_was_running = false;
Peter Xub15df1a2018-01-03 20:20:13 +08002052 s->iteration_initial_bytes = 0;
2053 s->threshold_size = 0;
Juan Quintela0edda1c2010-05-11 16:28:39 +02002054}
Juan Quintelacab30142011-02-22 23:54:21 +01002055
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05302056int migrate_add_blocker(Error *reason, Error **errp)
Anthony Liguorifa2756b2011-11-14 15:09:43 -06002057{
Markus Armbruster811f8652019-04-01 11:08:24 +02002058 if (only_migratable) {
Markus Armbruster4b576642018-10-17 10:26:25 +02002059 error_propagate_prepend(errp, error_copy(reason),
2060 "disallowing migration blocker "
Greg Kurzb6eca812019-05-16 12:58:05 +02002061 "(--only-migratable) for: ");
Ashijeet Acharyab67b8c32017-01-16 17:01:54 +05302062 return -EACCES;
2063 }
2064
Juan Quintelafab35002017-03-22 17:36:57 +01002065 if (migration_is_idle()) {
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05302066 migration_blockers = g_slist_prepend(migration_blockers, reason);
2067 return 0;
2068 }
2069
Markus Armbruster4b576642018-10-17 10:26:25 +02002070 error_propagate_prepend(errp, error_copy(reason),
2071 "disallowing migration blocker "
2072 "(migration in progress) for: ");
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05302073 return -EBUSY;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06002074}
2075
2076void migrate_del_blocker(Error *reason)
2077{
2078 migration_blockers = g_slist_remove(migration_blockers, reason);
2079}
2080
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00002081void qmp_migrate_incoming(const char *uri, Error **errp)
2082{
2083 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00002084 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00002085
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00002086 if (!once) {
2087 error_setg(errp, "The incoming migration has already been started");
Yury Kotov603d5a42019-11-13 20:53:25 +03002088 return;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00002089 }
Paolo Bonzinie69d50d2020-10-27 04:22:57 -04002090 if (!runstate_check(RUN_STATE_INMIGRATE)) {
2091 error_setg(errp, "'-incoming' was not specified on the command line");
2092 return;
2093 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00002094
Peter Xucc48c582021-06-29 14:13:55 -04002095 if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) {
2096 return;
2097 }
2098
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00002099 qemu_start_incoming_migration(uri, &local_err);
2100
2101 if (local_err) {
Peter Xucc48c582021-06-29 14:13:55 -04002102 yank_unregister_instance(MIGRATION_YANK_INSTANCE);
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00002103 error_propagate(errp, local_err);
2104 return;
2105 }
2106
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00002107 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00002108}
2109
Peter Xu02affd42018-05-02 18:47:36 +08002110void qmp_migrate_recover(const char *uri, Error **errp)
2111{
2112 MigrationIncomingState *mis = migration_incoming_get_current();
2113
Peter Xub7f9afd2021-06-29 14:13:56 -04002114 /*
2115 * Don't even bother to use ERRP_GUARD() as it _must_ always be set by
2116 * callers (no one should ignore a recover failure); if there is, it's a
2117 * programming error.
2118 */
2119 assert(errp);
2120
Peter Xu02affd42018-05-02 18:47:36 +08002121 if (mis->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
2122 error_setg(errp, "Migrate recover can only be run "
2123 "when postcopy is paused.");
2124 return;
2125 }
2126
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01002127 if (qatomic_cmpxchg(&mis->postcopy_recover_triggered,
Peter Xu02affd42018-05-02 18:47:36 +08002128 false, true) == true) {
2129 error_setg(errp, "Migrate recovery is triggered already");
2130 return;
2131 }
2132
2133 /*
2134 * Note that this call will never start a real migration; it will
2135 * only re-setup the migration stream and poke existing migration
2136 * to continue using that newly established channel.
2137 */
2138 qemu_start_incoming_migration(uri, errp);
Peter Xub7f9afd2021-06-29 14:13:56 -04002139
2140 /* Safe to dereference with the assert above */
2141 if (*errp) {
2142 /* Reset the flag so user could still retry */
2143 qatomic_set(&mis->postcopy_recover_triggered, false);
2144 }
Peter Xu02affd42018-05-02 18:47:36 +08002145}
2146
Peter Xubfbf89c2018-05-02 18:47:39 +08002147void qmp_migrate_pause(Error **errp)
2148{
2149 MigrationState *ms = migrate_get_current();
2150 MigrationIncomingState *mis = migration_incoming_get_current();
2151 int ret;
2152
2153 if (ms->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2154 /* Source side, during postcopy */
2155 qemu_mutex_lock(&ms->qemu_file_lock);
2156 ret = qemu_file_shutdown(ms->to_dst_file);
2157 qemu_mutex_unlock(&ms->qemu_file_lock);
2158 if (ret) {
2159 error_setg(errp, "Failed to pause source migration");
2160 }
2161 return;
2162 }
2163
2164 if (mis->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2165 ret = qemu_file_shutdown(mis->from_src_file);
2166 if (ret) {
2167 error_setg(errp, "Failed to pause destination migration");
2168 }
2169 return;
2170 }
2171
2172 error_setg(errp, "migrate-pause is currently only supported "
2173 "during postcopy-active state");
2174}
2175
Greg Kurz24f39022016-05-04 21:44:19 +02002176bool migration_is_blocked(Error **errp)
2177{
2178 if (qemu_savevm_state_blocked(errp)) {
2179 return true;
2180 }
2181
2182 if (migration_blockers) {
Eduardo Habkost250561e2017-06-08 10:39:05 -03002183 error_propagate(errp, error_copy(migration_blockers->data));
Greg Kurz24f39022016-05-04 21:44:19 +02002184 return true;
2185 }
2186
2187 return false;
2188}
2189
Peter Xud3e35b82018-05-02 18:47:24 +08002190/* Returns true if continue to migrate, or false if error detected */
2191static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc,
2192 bool resume, Error **errp)
2193{
2194 Error *local_err = NULL;
2195
2196 if (resume) {
2197 if (s->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
2198 error_setg(errp, "Cannot resume if there is no "
2199 "paused migration");
2200 return false;
2201 }
Peter Xu97ca2112018-07-23 20:33:03 +08002202
2203 /*
2204 * Postcopy recovery won't work well with release-ram
2205 * capability since release-ram will drop the page buffer as
2206 * long as the page is put into the send buffer. So if there
2207 * is a network failure happened, any page buffers that have
2208 * not yet reached the destination VM but have already been
2209 * sent from the source VM will be lost forever. Let's refuse
2210 * the client from resuming such a postcopy migration.
2211 * Luckily release-ram was designed to only be used when src
2212 * and destination VMs are on the same host, so it should be
2213 * fine.
2214 */
2215 if (migrate_release_ram()) {
2216 error_setg(errp, "Postcopy recovery cannot work "
2217 "when release-ram capability is set");
2218 return false;
2219 }
2220
Peter Xud3e35b82018-05-02 18:47:24 +08002221 /* This is a resume, skip init status */
2222 return true;
2223 }
2224
Juan Quintela392d87e2020-01-21 15:39:23 +01002225 if (migration_is_running(s->state)) {
Peter Xud3e35b82018-05-02 18:47:24 +08002226 error_setg(errp, QERR_MIGRATION_ACTIVE);
2227 return false;
2228 }
2229
2230 if (runstate_check(RUN_STATE_INMIGRATE)) {
2231 error_setg(errp, "Guest is waiting for an incoming migration");
2232 return false;
2233 }
2234
Tuguoyi36d0fe62020-12-08 01:46:25 +00002235 if (runstate_check(RUN_STATE_POSTMIGRATE)) {
2236 error_setg(errp, "Can't migrate the vm that was paused due to "
2237 "previous migration");
2238 return false;
2239 }
2240
Peter Xud3e35b82018-05-02 18:47:24 +08002241 if (migration_is_blocked(errp)) {
2242 return false;
2243 }
2244
2245 if (blk || blk_inc) {
Rao, Lei3ba02442021-06-08 16:23:28 +08002246 if (migrate_colo_enabled()) {
2247 error_setg(errp, "No disk migration is required in COLO mode");
2248 return false;
2249 }
Peter Xud3e35b82018-05-02 18:47:24 +08002250 if (migrate_use_block() || migrate_use_block_incremental()) {
2251 error_setg(errp, "Command options are incompatible with "
2252 "current migration capabilities");
2253 return false;
2254 }
2255 migrate_set_block_enabled(true, &local_err);
2256 if (local_err) {
2257 error_propagate(errp, local_err);
2258 return false;
2259 }
2260 s->must_remove_block_options = true;
2261 }
2262
2263 if (blk_inc) {
2264 migrate_set_block_incremental(s, true);
2265 }
2266
2267 migrate_init(s);
Ivan Ren87f3bd82019-08-02 18:18:41 +08002268 /*
2269 * set ram_counters memory to zero for a
2270 * new migration
2271 */
2272 memset(&ram_counters, 0, sizeof(ram_counters));
Peter Xud3e35b82018-05-02 18:47:24 +08002273
2274 return true;
2275}
2276
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002277void qmp_migrate(const char *uri, bool has_blk, bool blk,
2278 bool has_inc, bool inc, bool has_detach, bool detach,
Peter Xu7a4da282018-05-02 18:47:23 +08002279 bool has_resume, bool resume, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002280{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +02002281 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +02002282 MigrationState *s = migrate_get_current();
Longpeng(Mike)d658f652020-08-06 15:40:29 +08002283 const char *p = NULL;
Juan Quintelacab30142011-02-22 23:54:21 +01002284
Peter Xud3e35b82018-05-02 18:47:24 +08002285 if (!migrate_prepare(s, has_blk && blk, has_inc && inc,
2286 has_resume && resume, errp)) {
2287 /* Error detected, put into errp */
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002288 return;
Juan Quintelacab30142011-02-22 23:54:21 +01002289 }
Juan Quintelacab30142011-02-22 23:54:21 +01002290
Lukas Straubb5eea992020-12-28 16:08:52 +01002291 if (!(has_resume && resume)) {
2292 if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) {
2293 return;
2294 }
2295 }
2296
Longpeng(Mike)d658f652020-08-06 15:40:29 +08002297 if (strstart(uri, "tcp:", &p) ||
Longpeng(Mike)9ba3b2b2020-08-06 15:40:30 +08002298 strstart(uri, "unix:", NULL) ||
2299 strstart(uri, "vsock:", NULL)) {
Longpeng(Mike)d658f652020-08-06 15:40:29 +08002300 socket_start_outgoing_migration(s, p ? p : uri, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04002301#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08002302 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04002303 rdma_start_outgoing_migration(s, p, &local_err);
2304#endif
Juan Quintelacab30142011-02-22 23:54:21 +01002305 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002306 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01002307 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002308 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01002309 } else {
Lukas Straubb5eea992020-12-28 16:08:52 +01002310 if (!(has_resume && resume)) {
2311 yank_unregister_instance(MIGRATION_YANK_INSTANCE);
2312 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002313 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
2314 "a valid migration protocol");
zhanghailiang48781e52015-12-16 11:47:33 +00002315 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2316 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09576e72018-03-16 20:21:14 +00002317 block_cleanup_parameters(s);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002318 return;
Juan Quintelacab30142011-02-22 23:54:21 +01002319 }
2320
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002321 if (local_err) {
Lukas Straubb5eea992020-12-28 16:08:52 +01002322 if (!(has_resume && resume)) {
2323 yank_unregister_instance(MIGRATION_YANK_INSTANCE);
2324 }
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01002325 migrate_fd_error(s, local_err);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002326 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002327 return;
Juan Quintela1299c632011-11-09 21:29:01 +01002328 }
Juan Quintelacab30142011-02-22 23:54:21 +01002329}
2330
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02002331void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002332{
David Hildenbrandc7c0e722021-04-29 13:27:02 +02002333 migration_cancel();
Juan Quintelacab30142011-02-22 23:54:21 +01002334}
2335
Dr. David Alan Gilbert89cfc022017-10-20 10:05:53 +01002336void qmp_migrate_continue(MigrationStatus state, Error **errp)
2337{
2338 MigrationState *s = migrate_get_current();
2339 if (s->state != state) {
2340 error_setg(errp, "Migration not in expected state: %s",
2341 MigrationStatus_str(s->state));
2342 return;
2343 }
2344 qemu_sem_post(&s->pause_sem);
2345}
2346
Pavel Butsykin53f09a12017-02-03 18:23:20 +03002347bool migrate_release_ram(void)
2348{
2349 MigrationState *s;
2350
2351 s = migrate_get_current();
2352
2353 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
2354}
2355
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00002356bool migrate_postcopy_ram(void)
2357{
2358 MigrationState *s;
2359
2360 s = migrate_get_current();
2361
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +00002362 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00002363}
2364
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002365bool migrate_postcopy(void)
2366{
Vladimir Sementsov-Ogievskiydd6bb912018-03-13 15:34:00 -04002367 return migrate_postcopy_ram() || migrate_dirty_bitmaps();
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002368}
2369
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06002370bool migrate_auto_converge(void)
2371{
2372 MigrationState *s;
2373
2374 s = migrate_get_current();
2375
2376 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
2377}
2378
Peter Lieven323004a2013-07-18 09:48:50 +02002379bool migrate_zero_blocks(void)
2380{
2381 MigrationState *s;
2382
2383 s = migrate_get_current();
2384
2385 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
2386}
2387
Alexey Perevalovf22f9282018-03-22 21:17:22 +03002388bool migrate_postcopy_blocktime(void)
2389{
2390 MigrationState *s;
2391
2392 s = migrate_get_current();
2393
2394 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME];
2395}
2396
Liang Li8706d2d2015-03-23 16:32:17 +08002397bool migrate_use_compression(void)
2398{
Liang Lidde4e692015-03-23 16:32:26 +08002399 MigrationState *s;
2400
2401 s = migrate_get_current();
2402
2403 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08002404}
2405
2406int migrate_compress_level(void)
2407{
2408 MigrationState *s;
2409
2410 s = migrate_get_current();
2411
Daniel P. Berrange2594f562016-04-27 11:05:14 +01002412 return s->parameters.compress_level;
Liang Li8706d2d2015-03-23 16:32:17 +08002413}
2414
2415int migrate_compress_threads(void)
2416{
2417 MigrationState *s;
2418
2419 s = migrate_get_current();
2420
Daniel P. Berrange2594f562016-04-27 11:05:14 +01002421 return s->parameters.compress_threads;
Liang Li8706d2d2015-03-23 16:32:17 +08002422}
2423
Xiao Guangrong1d588722018-08-21 16:10:20 +08002424int migrate_compress_wait_thread(void)
2425{
2426 MigrationState *s;
2427
2428 s = migrate_get_current();
2429
2430 return s->parameters.compress_wait_thread;
2431}
2432
Liang Li3fcb38c2015-03-23 16:32:18 +08002433int migrate_decompress_threads(void)
2434{
2435 MigrationState *s;
2436
2437 s = migrate_get_current();
2438
Daniel P. Berrange2594f562016-04-27 11:05:14 +01002439 return s->parameters.decompress_threads;
Liang Li3fcb38c2015-03-23 16:32:18 +08002440}
2441
Vladimir Sementsov-Ogievskiy55efc8c2018-03-13 15:34:00 -04002442bool migrate_dirty_bitmaps(void)
2443{
2444 MigrationState *s;
2445
2446 s = migrate_get_current();
2447
2448 return s->enabled_capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS];
2449}
2450
Yury Kotov18269062019-02-15 20:45:45 +03002451bool migrate_ignore_shared(void)
2452{
2453 MigrationState *s;
2454
2455 s = migrate_get_current();
2456
2457 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_IGNORE_SHARED];
2458}
2459
Yury Kotovb9d68df2019-09-03 19:22:44 +03002460bool migrate_validate_uuid(void)
2461{
2462 MigrationState *s;
2463
2464 s = migrate_get_current();
2465
2466 return s->enabled_capabilities[MIGRATION_CAPABILITY_VALIDATE_UUID];
2467}
2468
Juan Quintelab05dc722015-07-07 14:44:05 +02002469bool migrate_use_events(void)
2470{
2471 MigrationState *s;
2472
2473 s = migrate_get_current();
2474
2475 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
2476}
2477
Juan Quintela30126bb2016-01-14 12:23:00 +01002478bool migrate_use_multifd(void)
2479{
2480 MigrationState *s;
2481
2482 s = migrate_get_current();
2483
Juan Quintelacbfd6c92019-02-06 13:54:06 +01002484 return s->enabled_capabilities[MIGRATION_CAPABILITY_MULTIFD];
Juan Quintela30126bb2016-01-14 12:23:00 +01002485}
2486
Dr. David Alan Gilbert93fbd032017-10-20 10:05:50 +01002487bool migrate_pause_before_switchover(void)
2488{
2489 MigrationState *s;
2490
2491 s = migrate_get_current();
2492
2493 return s->enabled_capabilities[
2494 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER];
2495}
2496
Juan Quintela4075fb12016-01-15 08:56:17 +01002497int migrate_multifd_channels(void)
2498{
2499 MigrationState *s;
2500
2501 s = migrate_get_current();
2502
Juan Quintelacbfd6c92019-02-06 13:54:06 +01002503 return s->parameters.multifd_channels;
Juan Quintela4075fb12016-01-15 08:56:17 +01002504}
2505
Juan Quintelaab7cbb02019-05-15 13:37:46 +02002506MultiFDCompression migrate_multifd_compression(void)
2507{
2508 MigrationState *s;
2509
2510 s = migrate_get_current();
2511
2512 return s->parameters.multifd_compression;
2513}
2514
Juan Quintela9004db42020-01-23 17:08:52 +01002515int migrate_multifd_zlib_level(void)
2516{
2517 MigrationState *s;
2518
2519 s = migrate_get_current();
2520
2521 return s->parameters.multifd_zlib_level;
2522}
2523
Juan Quintela6a9ad152020-01-23 17:41:36 +01002524int migrate_multifd_zstd_level(void)
2525{
2526 MigrationState *s;
2527
2528 s = migrate_get_current();
2529
2530 return s->parameters.multifd_zstd_level;
2531}
2532
Orit Wasserman17ad9b32012-08-06 21:42:53 +03002533int migrate_use_xbzrle(void)
2534{
2535 MigrationState *s;
2536
2537 s = migrate_get_current();
2538
2539 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
2540}
2541
Markus Armbruster8b9407a2021-02-02 15:17:32 +01002542uint64_t migrate_xbzrle_cache_size(void)
Orit Wasserman17ad9b32012-08-06 21:42:53 +03002543{
2544 MigrationState *s;
2545
2546 s = migrate_get_current();
2547
Juan Quintela73af8dd2017-10-05 21:30:10 +02002548 return s->parameters.xbzrle_cache_size;
Orit Wasserman17ad9b32012-08-06 21:42:53 +03002549}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002550
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01002551static int64_t migrate_max_postcopy_bandwidth(void)
2552{
2553 MigrationState *s;
2554
2555 s = migrate_get_current();
2556
2557 return s->parameters.max_postcopy_bandwidth;
2558}
2559
Juan Quintela2833c592017-04-05 18:32:37 +02002560bool migrate_use_block(void)
2561{
2562 MigrationState *s;
2563
2564 s = migrate_get_current();
2565
2566 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
2567}
2568
Peter Xuc788ada2017-06-26 18:28:55 +08002569bool migrate_use_return_path(void)
2570{
2571 MigrationState *s;
2572
2573 s = migrate_get_current();
2574
2575 return s->enabled_capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
2576}
2577
Juan Quintela2833c592017-04-05 18:32:37 +02002578bool migrate_use_block_incremental(void)
2579{
2580 MigrationState *s;
2581
2582 s = migrate_get_current();
2583
2584 return s->parameters.block_incremental;
2585}
2586
Andrey Gruzdev6e8c25b2021-01-29 13:14:03 +03002587bool migrate_background_snapshot(void)
2588{
2589 MigrationState *s;
2590
2591 s = migrate_get_current();
2592
2593 return s->enabled_capabilities[MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT];
2594}
2595
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002596/* migration thread support */
2597/*
2598 * Something bad happened to the RP stream, mark an error
2599 * The caller shall print or trace something to indicate why
2600 */
2601static void mark_source_rp_bad(MigrationState *s)
2602{
2603 s->rp_state.error = true;
2604}
2605
2606static struct rp_cmd_args {
2607 ssize_t len; /* -1 = variable */
2608 const char *name;
2609} rp_cmd_args[] = {
2610 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
2611 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
2612 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002613 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
2614 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Peter Xua335deb2018-05-02 18:47:28 +08002615 [MIG_RP_MSG_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" },
Peter Xu13955b82018-05-02 18:47:30 +08002616 [MIG_RP_MSG_RESUME_ACK] = { .len = 4, .name = "RESUME_ACK" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002617 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
2618};
2619
2620/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002621 * Process a request for pages received on the return path,
2622 * We're allowed to send more than requested (e.g. to round to our page size)
2623 * and we don't need to send pages that have already been sent.
2624 */
2625static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
2626 ram_addr_t start, size_t len)
2627{
Wei Yang038adc22019-10-13 10:11:45 +08002628 long our_host_ps = qemu_real_host_page_size;
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00002629
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002630 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00002631
2632 /*
2633 * Since we currently insist on matching page sizes, just sanity check
2634 * we're being asked for whole host pages.
2635 */
Bihong Yu395cb452020-10-20 11:10:43 +08002636 if (start & (our_host_ps - 1) ||
2637 (len & (our_host_ps - 1))) {
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00002638 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
2639 " len: %zd", __func__, start, len);
2640 mark_source_rp_bad(ms);
2641 return;
2642 }
2643
Juan Quintela96506892017-03-14 18:41:03 +01002644 if (ram_save_queue_pages(rbname, start, len)) {
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00002645 mark_source_rp_bad(ms);
2646 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002647}
2648
Peter Xu14b17422018-05-02 18:47:21 +08002649/* Return true to retry, false to quit */
2650static bool postcopy_pause_return_path_thread(MigrationState *s)
2651{
2652 trace_postcopy_pause_return_path();
2653
2654 qemu_sem_wait(&s->postcopy_pause_rp_sem);
2655
2656 trace_postcopy_pause_return_path_continued();
2657
2658 return true;
2659}
2660
Peter Xua335deb2018-05-02 18:47:28 +08002661static int migrate_handle_rp_recv_bitmap(MigrationState *s, char *block_name)
2662{
2663 RAMBlock *block = qemu_ram_block_by_name(block_name);
2664
2665 if (!block) {
2666 error_report("%s: invalid block name '%s'", __func__, block_name);
2667 return -EINVAL;
2668 }
2669
2670 /* Fetch the received bitmap and refresh the dirty bitmap */
2671 return ram_dirty_bitmap_reload(s, block);
2672}
2673
Peter Xu13955b82018-05-02 18:47:30 +08002674static int migrate_handle_rp_resume_ack(MigrationState *s, uint32_t value)
2675{
2676 trace_source_return_path_thread_resume_ack(value);
2677
2678 if (value != MIGRATION_RESUME_ACK_VALUE) {
2679 error_report("%s: illegal resume_ack value %"PRIu32,
2680 __func__, value);
2681 return -1;
2682 }
2683
2684 /* Now both sides are active. */
2685 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
2686 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2687
Peter Xu94190692018-05-02 18:47:34 +08002688 /* Notify send thread that time to continue send pages */
2689 qemu_sem_post(&s->rp_state.rp_sem);
Peter Xu13955b82018-05-02 18:47:30 +08002690
2691 return 0;
2692}
2693
Peter Xu43044ac2021-07-22 13:58:38 -04002694/* Release ms->rp_state.from_dst_file in a safe way */
2695static void migration_release_from_dst_file(MigrationState *ms)
2696{
2697 QEMUFile *file;
2698
2699 WITH_QEMU_LOCK_GUARD(&ms->qemu_file_lock) {
2700 /*
2701 * Reset the from_dst_file pointer first before releasing it, as we
2702 * can't block within lock section
2703 */
2704 file = ms->rp_state.from_dst_file;
2705 ms->rp_state.from_dst_file = NULL;
2706 }
2707
2708 qemu_fclose(file);
2709}
2710
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002711/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002712 * Handles messages sent on the return path towards the source VM
2713 *
2714 */
2715static void *source_return_path_thread(void *opaque)
2716{
2717 MigrationState *ms = opaque;
2718 QEMUFile *rp = ms->rp_state.from_dst_file;
2719 uint16_t header_len, header_type;
Peter Xu568b01c2016-03-09 14:12:12 +08002720 uint8_t buf[512];
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002721 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002722 ram_addr_t start = 0; /* =0 to silence warning */
2723 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002724 int res;
2725
2726 trace_source_return_path_thread_entry();
Lidong Chen74637e62018-08-06 21:29:29 +08002727 rcu_register_thread();
Peter Xu14b17422018-05-02 18:47:21 +08002728
2729retry:
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002730 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
2731 migration_is_setup_or_active(ms->state)) {
2732 trace_source_return_path_thread_loop_top();
2733 header_type = qemu_get_be16(rp);
2734 header_len = qemu_get_be16(rp);
2735
Peter Xu7a9ddfb2018-02-08 18:31:05 +08002736 if (qemu_file_get_error(rp)) {
2737 mark_source_rp_bad(ms);
2738 goto out;
2739 }
2740
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002741 if (header_type >= MIG_RP_MSG_MAX ||
2742 header_type == MIG_RP_MSG_INVALID) {
2743 error_report("RP: Received invalid message 0x%04x length 0x%04x",
Mao Zhongyied8b2822020-03-28 01:19:08 +08002744 header_type, header_len);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002745 mark_source_rp_bad(ms);
2746 goto out;
2747 }
2748
2749 if ((rp_cmd_args[header_type].len != -1 &&
2750 header_len != rp_cmd_args[header_type].len) ||
Peter Xu568b01c2016-03-09 14:12:12 +08002751 header_len > sizeof(buf)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002752 error_report("RP: Received '%s' message (0x%04x) with"
Mao Zhongyied8b2822020-03-28 01:19:08 +08002753 "incorrect length %d expecting %zu",
2754 rp_cmd_args[header_type].name, header_type, header_len,
2755 (size_t)rp_cmd_args[header_type].len);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002756 mark_source_rp_bad(ms);
2757 goto out;
2758 }
2759
2760 /* We know we've got a valid header by this point */
2761 res = qemu_get_buffer(rp, buf, header_len);
2762 if (res != header_len) {
2763 error_report("RP: Failed reading data for message 0x%04x"
2764 " read %d expected %d",
2765 header_type, res, header_len);
2766 mark_source_rp_bad(ms);
2767 goto out;
2768 }
2769
2770 /* OK, we have the message and the data */
2771 switch (header_type) {
2772 case MIG_RP_MSG_SHUT:
Peter Maydell4d885132016-06-10 17:09:22 +01002773 sibling_error = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002774 trace_source_return_path_thread_shut(sibling_error);
2775 if (sibling_error) {
2776 error_report("RP: Sibling indicated error %d", sibling_error);
2777 mark_source_rp_bad(ms);
2778 }
2779 /*
2780 * We'll let the main thread deal with closing the RP
2781 * we could do a shutdown(2) on it, but we're the only user
2782 * anyway, so there's nothing gained.
2783 */
2784 goto out;
2785
2786 case MIG_RP_MSG_PONG:
Peter Maydell4d885132016-06-10 17:09:22 +01002787 tmp32 = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002788 trace_source_return_path_thread_pong(tmp32);
2789 break;
2790
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002791 case MIG_RP_MSG_REQ_PAGES:
Peter Maydell4d885132016-06-10 17:09:22 +01002792 start = ldq_be_p(buf);
2793 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002794 migrate_handle_rp_req_pages(ms, NULL, start, len);
2795 break;
2796
2797 case MIG_RP_MSG_REQ_PAGES_ID:
2798 expected_len = 12 + 1; /* header + termination */
2799
2800 if (header_len >= expected_len) {
Peter Maydell4d885132016-06-10 17:09:22 +01002801 start = ldq_be_p(buf);
2802 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002803 /* Now we expect an idstr */
2804 tmp32 = buf[12]; /* Length of the following idstr */
2805 buf[13 + tmp32] = '\0';
2806 expected_len += tmp32;
2807 }
2808 if (header_len != expected_len) {
2809 error_report("RP: Req_Page_id with length %d expecting %zd",
Mao Zhongyied8b2822020-03-28 01:19:08 +08002810 header_len, expected_len);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002811 mark_source_rp_bad(ms);
2812 goto out;
2813 }
2814 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
2815 break;
2816
Peter Xua335deb2018-05-02 18:47:28 +08002817 case MIG_RP_MSG_RECV_BITMAP:
2818 if (header_len < 1) {
2819 error_report("%s: missing block name", __func__);
2820 mark_source_rp_bad(ms);
2821 goto out;
2822 }
2823 /* Format: len (1B) + idstr (<255B). This ends the idstr. */
2824 buf[buf[0] + 1] = '\0';
2825 if (migrate_handle_rp_recv_bitmap(ms, (char *)(buf + 1))) {
2826 mark_source_rp_bad(ms);
2827 goto out;
2828 }
2829 break;
2830
Peter Xu13955b82018-05-02 18:47:30 +08002831 case MIG_RP_MSG_RESUME_ACK:
2832 tmp32 = ldl_be_p(buf);
2833 if (migrate_handle_rp_resume_ack(ms, tmp32)) {
2834 mark_source_rp_bad(ms);
2835 goto out;
2836 }
2837 break;
2838
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002839 default:
2840 break;
2841 }
2842 }
Peter Xu14b17422018-05-02 18:47:21 +08002843
2844out:
2845 res = qemu_file_get_error(rp);
2846 if (res) {
Dr. David Alan Gilbertfb14a422019-10-07 11:35:07 +01002847 if (res == -EIO && migration_in_postcopy()) {
Peter Xu14b17422018-05-02 18:47:21 +08002848 /*
2849 * Maybe there is something we can do: it looks like a
2850 * network down issue, and we pause for a recovery.
2851 */
Peter Xu43044ac2021-07-22 13:58:38 -04002852 migration_release_from_dst_file(ms);
Peter Xu2e3e3da2021-07-08 15:06:51 -04002853 rp = NULL;
Peter Xu14b17422018-05-02 18:47:21 +08002854 if (postcopy_pause_return_path_thread(ms)) {
Peter Xu43044ac2021-07-22 13:58:38 -04002855 /*
2856 * Reload rp, reset the rest. Referencing it is safe since
2857 * it's reset only by us above, or when migration completes
2858 */
Peter Xu2e3e3da2021-07-08 15:06:51 -04002859 rp = ms->rp_state.from_dst_file;
Peter Xu14b17422018-05-02 18:47:21 +08002860 ms->rp_state.error = false;
2861 goto retry;
2862 }
2863 }
2864
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002865 trace_source_return_path_thread_bad_end();
2866 mark_source_rp_bad(ms);
2867 }
2868
2869 trace_source_return_path_thread_end();
Peter Xu43044ac2021-07-22 13:58:38 -04002870 migration_release_from_dst_file(ms);
Lidong Chen74637e62018-08-06 21:29:29 +08002871 rcu_unregister_thread();
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002872 return NULL;
2873}
2874
Peter Xud3e35b82018-05-02 18:47:24 +08002875static int open_return_path_on_source(MigrationState *ms,
2876 bool create_thread)
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002877{
zhanghailiang89a02a92016-01-15 11:37:42 +08002878 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002879 if (!ms->rp_state.from_dst_file) {
2880 return -1;
2881 }
2882
2883 trace_open_return_path_on_source();
Peter Xud3e35b82018-05-02 18:47:24 +08002884
2885 if (!create_thread) {
2886 /* We're done */
2887 return 0;
2888 }
2889
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002890 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
2891 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
Peter Xu53021ea2021-07-22 13:58:37 -04002892 ms->rp_state.rp_thread_created = true;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002893
2894 trace_open_return_path_on_source_continue();
2895
2896 return 0;
2897}
2898
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002899/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
2900static int await_return_path_close_on_source(MigrationState *ms)
2901{
2902 /*
2903 * If this is a normal exit then the destination will send a SHUT and the
2904 * rp_thread will exit, however if there's an error we need to cause
2905 * it to exit.
2906 */
zhanghailiang89a02a92016-01-15 11:37:42 +08002907 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002908 /*
2909 * shutdown(2), if we have it, will cause it to unblock if it's stuck
2910 * waiting for the destination.
2911 */
2912 qemu_file_shutdown(ms->rp_state.from_dst_file);
2913 mark_source_rp_bad(ms);
2914 }
2915 trace_await_return_path_close_on_source_joining();
2916 qemu_thread_join(&ms->rp_state.rp_thread);
Peter Xu53021ea2021-07-22 13:58:37 -04002917 ms->rp_state.rp_thread_created = false;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002918 trace_await_return_path_close_on_source_close();
2919 return ms->rp_state.error;
2920}
2921
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002922/*
2923 * Switch from normal iteration to postcopy
2924 * Returns non-0 on error
2925 */
Peter Xu7287cbd2018-01-03 20:20:09 +08002926static int postcopy_start(MigrationState *ms)
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002927{
2928 int ret;
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002929 QIOChannelBuffer *bioc;
2930 QEMUFile *fb;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002931 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01002932 int64_t bandwidth = migrate_max_postcopy_bandwidth();
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002933 bool restart_block = false;
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002934 int cur_state = MIGRATION_STATUS_ACTIVE;
2935 if (!migrate_pause_before_switchover()) {
2936 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
2937 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2938 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002939
2940 trace_postcopy_start();
2941 qemu_mutex_lock_iothread();
2942 trace_postcopy_start_set_run();
2943
Daniel Henrique Barbozafb064112018-12-05 17:47:01 -02002944 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002945 global_state_store();
2946 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01002947 if (ret < 0) {
2948 goto fail;
2949 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002950
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002951 ret = migration_maybe_pause(ms, &cur_state,
2952 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2953 if (ret < 0) {
2954 goto fail;
2955 }
2956
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01002957 ret = bdrv_inactivate_all();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002958 if (ret < 0) {
2959 goto fail;
2960 }
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002961 restart_block = true;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002962
2963 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00002964 * Cause any non-postcopiable, but iterative devices to
2965 * send out their final data.
2966 */
Fam Zhenga1fbe752017-06-17 00:06:58 +08002967 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00002968
2969 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002970 * in Finish migrate and with the io-lock held everything should
2971 * be quiet, but we've potentially still got dirty pages and we
2972 * need to tell the destination to throw any pages it's already received
2973 * that are dirty
2974 */
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002975 if (migrate_postcopy_ram()) {
2976 if (ram_postcopy_send_discard_bitmap(ms)) {
2977 error_report("postcopy send discard bitmap failed");
2978 goto fail;
2979 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002980 }
2981
2982 /*
2983 * send rest of state - note things that are doing postcopy
2984 * will notice we're in POSTCOPY_ACTIVE and not actually
2985 * wrap their state up here
2986 */
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01002987 /* 0 max-postcopy-bandwidth means unlimited */
2988 if (!bandwidth) {
2989 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
2990 } else {
2991 qemu_file_set_rate_limit(ms->to_dst_file, bandwidth / XFER_LIMIT_RATIO);
2992 }
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002993 if (migrate_postcopy_ram()) {
2994 /* Ping just for debugging, helps line traces up */
2995 qemu_savevm_send_ping(ms->to_dst_file, 2);
2996 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002997
2998 /*
2999 * While loading the device state we may trigger page transfer
3000 * requests and the fd must be free to process those, and thus
3001 * the destination must read the whole device state off the fd before
3002 * it starts processing it. Unfortunately the ad-hoc migration format
3003 * doesn't allow the destination to know the size to read without fully
3004 * parsing it through each devices load-state code (especially the open
3005 * coded devices that use get/put).
3006 * So we wrap the device state up in a package with a length at the start;
3007 * to do this we use a qemu_buf to hold the whole of the device state.
3008 */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01003009 bioc = qio_channel_buffer_new(4096);
Daniel P. Berrange6f01f132016-09-30 11:57:14 +01003010 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01003011 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
3012 object_unref(OBJECT(bioc));
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003013
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00003014 /*
3015 * Make sure the receiver can get incoming pages before we send the rest
3016 * of the state
3017 */
3018 qemu_savevm_send_postcopy_listen(fb);
3019
Fam Zhenga1fbe752017-06-17 00:06:58 +08003020 qemu_savevm_state_complete_precopy(fb, false, false);
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03003021 if (migrate_postcopy_ram()) {
3022 qemu_savevm_send_ping(fb, 3);
3023 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003024
3025 qemu_savevm_send_postcopy_run(fb);
3026
3027 /* <><> end of stuff going into the package */
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003028
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00003029 /* Last point of recovery; as soon as we send the package the destination
3030 * can open devices and potentially start running.
3031 * Lets just check again we've not got any errors.
3032 */
3033 ret = qemu_file_get_error(ms->to_dst_file);
3034 if (ret) {
3035 error_report("postcopy_start: Migration stream errored (pre package)");
3036 goto fail_closefb;
3037 }
3038
3039 restart_block = false;
3040
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003041 /* Now send that blob */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01003042 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003043 goto fail_closefb;
3044 }
3045 qemu_fclose(fb);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00003046
3047 /* Send a notify to give a chance for anything that needs to happen
3048 * at the transition to postcopy and after the device state; in particular
3049 * spice needs to trigger a transition now
3050 */
3051 ms->postcopy_after_devices = true;
3052 notifier_list_notify(&migration_state_notifiers, ms);
3053
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003054 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
3055
3056 qemu_mutex_unlock_iothread();
3057
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03003058 if (migrate_postcopy_ram()) {
3059 /*
3060 * Although this ping is just for debug, it could potentially be
3061 * used for getting a better measurement of downtime at the source.
3062 */
3063 qemu_savevm_send_ping(ms->to_dst_file, 4);
3064 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003065
Pavel Butsykinced1c612017-02-03 18:23:21 +03003066 if (migrate_release_ram()) {
3067 ram_postcopy_migrated_memory_release(ms);
3068 }
3069
zhanghailiang89a02a92016-01-15 11:37:42 +08003070 ret = qemu_file_get_error(ms->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003071 if (ret) {
3072 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00003073 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003074 MIGRATION_STATUS_FAILED);
3075 }
3076
3077 return ret;
3078
3079fail_closefb:
3080 qemu_fclose(fb);
3081fail:
zhanghailiang48781e52015-12-16 11:47:33 +00003082 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003083 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00003084 if (restart_block) {
3085 /* A failure happened early enough that we know the destination hasn't
3086 * accessed block devices, so we're safe to recover.
3087 */
3088 Error *local_err = NULL;
3089
3090 bdrv_invalidate_cache_all(&local_err);
3091 if (local_err) {
3092 error_report_err(local_err);
3093 }
3094 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003095 qemu_mutex_unlock_iothread();
3096 return -1;
3097}
3098
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003099/**
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01003100 * migration_maybe_pause: Pause if required to by
3101 * migrate_pause_before_switchover called with the iothread locked
3102 * Returns: 0 on success
3103 */
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01003104static int migration_maybe_pause(MigrationState *s,
3105 int *current_active_state,
3106 int new_state)
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01003107{
3108 if (!migrate_pause_before_switchover()) {
3109 return 0;
3110 }
3111
3112 /* Since leaving this state is not atomic with posting the semaphore
3113 * it's possible that someone could have issued multiple migrate_continue
3114 * and the semaphore is incorrectly positive at this point;
3115 * the docs say it's undefined to reinit a semaphore that's already
3116 * init'd, so use timedwait to eat up any existing posts.
3117 */
3118 while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) {
3119 /* This block intentionally left blank */
3120 }
3121
Zhimin Feng89583382020-01-14 17:43:09 +08003122 /*
3123 * If the migration is cancelled when it is in the completion phase,
3124 * the migration state is set to MIGRATION_STATUS_CANCELLING.
3125 * So we don't need to wait a semaphore, otherwise we would always
3126 * wait for the 'pause_sem' semaphore.
3127 */
3128 if (s->state != MIGRATION_STATUS_CANCELLING) {
3129 qemu_mutex_unlock_iothread();
3130 migrate_set_state(&s->state, *current_active_state,
3131 MIGRATION_STATUS_PRE_SWITCHOVER);
3132 qemu_sem_wait(&s->pause_sem);
3133 migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER,
3134 new_state);
3135 *current_active_state = new_state;
3136 qemu_mutex_lock_iothread();
3137 }
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01003138
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01003139 return s->state == new_state ? 0 : -EINVAL;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01003140}
3141
3142/**
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003143 * migration_completion: Used by migration_thread when there's not much left.
3144 * The caller 'breaks' the loop when this returns.
3145 *
3146 * @s: Current migration state
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003147 */
Peter Xu2ad87302018-01-03 20:20:14 +08003148static void migration_completion(MigrationState *s)
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003149{
3150 int ret;
Peter Xu2ad87302018-01-03 20:20:14 +08003151 int current_active_state = s->state;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003152
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003153 if (s->state == MIGRATION_STATUS_ACTIVE) {
3154 qemu_mutex_lock_iothread();
Peter Xu64909f92018-01-03 20:20:10 +08003155 s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Daniel Henrique Barbozafb064112018-12-05 17:47:01 -02003156 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
Peter Xu7287cbd2018-01-03 20:20:09 +08003157 s->vm_was_running = runstate_is_running();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003158 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003159
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003160 if (!ret) {
Fam Zhenga1fbe752017-06-17 00:06:58 +08003161 bool inactivate = !migrate_colo_enabled();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003162 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Daniel P. Berrangé626ff652021-04-15 14:33:51 +01003163 trace_migration_completion_vm_stop(ret);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02003164 if (ret >= 0) {
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01003165 ret = migration_maybe_pause(s, &current_active_state,
3166 MIGRATION_STATUS_DEVICE);
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01003167 }
3168 if (ret >= 0) {
Kevin Wolff07fa4c2017-05-22 17:10:38 +02003169 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
Fam Zhenga1fbe752017-06-17 00:06:58 +08003170 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
3171 inactivate);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02003172 }
Fam Zhenga1fbe752017-06-17 00:06:58 +08003173 if (inactivate && ret >= 0) {
3174 s->block_inactive = true;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003175 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003176 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003177 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003178
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003179 if (ret < 0) {
3180 goto fail;
3181 }
3182 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
3183 trace_migration_completion_postcopy_end();
3184
zhanghailiang89a02a92016-01-15 11:37:42 +08003185 qemu_savevm_state_complete_postcopy(s->to_dst_file);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003186 trace_migration_completion_postcopy_end_after_complete();
Longpeng(Mike)6ba11212020-11-05 17:17:26 +08003187 } else if (s->state == MIGRATION_STATUS_CANCELLING) {
3188 goto fail;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003189 }
3190
3191 /*
3192 * If rp was opened we must clean up the thread before
3193 * cleaning everything else up (since if there are no failures
3194 * it will wait for the destination to send it's status in
3195 * a SHUT command).
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003196 */
Peter Xu53021ea2021-07-22 13:58:37 -04003197 if (s->rp_state.rp_thread_created) {
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003198 int rp_error;
Peter Xu0425dc92017-05-31 18:35:34 +08003199 trace_migration_return_path_end_before();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003200 rp_error = await_return_path_close_on_source(s);
Peter Xu0425dc92017-05-31 18:35:34 +08003201 trace_migration_return_path_end_after(rp_error);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003202 if (rp_error) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02003203 goto fail_invalidate;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003204 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003205 }
3206
zhanghailiang89a02a92016-01-15 11:37:42 +08003207 if (qemu_file_get_error(s->to_dst_file)) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003208 trace_migration_completion_file_err();
Greg Kurzfe904ea2016-05-18 15:44:36 +02003209 goto fail_invalidate;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003210 }
3211
zhanghailiang0b827d52016-10-27 14:42:54 +08003212 if (!migrate_colo_enabled()) {
3213 migrate_set_state(&s->state, current_active_state,
3214 MIGRATION_STATUS_COMPLETED);
3215 }
3216
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003217 return;
3218
Greg Kurzfe904ea2016-05-18 15:44:36 +02003219fail_invalidate:
3220 /* If not doing postcopy, vm_start() will be called: let's regain
3221 * control on images.
3222 */
Dr. David Alan Gilbert6039dd52018-02-05 09:13:37 +00003223 if (s->state == MIGRATION_STATUS_ACTIVE ||
3224 s->state == MIGRATION_STATUS_DEVICE) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02003225 Error *local_err = NULL;
3226
zhanghailiang1d2acc32017-01-24 15:59:52 +08003227 qemu_mutex_lock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02003228 bdrv_invalidate_cache_all(&local_err);
3229 if (local_err) {
3230 error_report_err(local_err);
zhanghailiang1d2acc32017-01-24 15:59:52 +08003231 } else {
3232 s->block_inactive = false;
Greg Kurzfe904ea2016-05-18 15:44:36 +02003233 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08003234 qemu_mutex_unlock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02003235 }
3236
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003237fail:
zhanghailiang48781e52015-12-16 11:47:33 +00003238 migrate_set_state(&s->state, current_active_state,
3239 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003240}
3241
Andrey Gruzdev85182782021-01-29 13:14:06 +03003242/**
3243 * bg_migration_completion: Used by bg_migration_thread when after all the
3244 * RAM has been saved. The caller 'breaks' the loop when this returns.
3245 *
3246 * @s: Current migration state
3247 */
3248static void bg_migration_completion(MigrationState *s)
3249{
3250 int current_active_state = s->state;
3251
3252 /*
3253 * Stop tracking RAM writes - un-protect memory, un-register UFFD
3254 * memory ranges, flush kernel wait queues and wake up threads
3255 * waiting for write fault to be resolved.
3256 */
3257 ram_write_tracking_stop();
3258
3259 if (s->state == MIGRATION_STATUS_ACTIVE) {
3260 /*
3261 * By this moment we have RAM content saved into the migration stream.
3262 * The next step is to flush the non-RAM content (device state)
3263 * right after the ram content. The device state has been stored into
3264 * the temporary buffer before RAM saving started.
3265 */
3266 qemu_put_buffer(s->to_dst_file, s->bioc->data, s->bioc->usage);
3267 qemu_fflush(s->to_dst_file);
3268 } else if (s->state == MIGRATION_STATUS_CANCELLING) {
3269 goto fail;
3270 }
3271
3272 if (qemu_file_get_error(s->to_dst_file)) {
3273 trace_migration_completion_file_err();
3274 goto fail;
3275 }
3276
3277 migrate_set_state(&s->state, current_active_state,
3278 MIGRATION_STATUS_COMPLETED);
3279 return;
3280
3281fail:
3282 migrate_set_state(&s->state, current_active_state,
3283 MIGRATION_STATUS_FAILED);
3284}
3285
zhanghailiang35a6ed42016-10-27 14:42:52 +08003286bool migrate_colo_enabled(void)
3287{
3288 MigrationState *s = migrate_get_current();
3289 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
3290}
3291
Peter Xub23c2ad2018-05-02 18:47:19 +08003292typedef enum MigThrError {
3293 /* No error detected */
3294 MIG_THR_ERR_NONE = 0,
3295 /* Detected error, but resumed successfully */
3296 MIG_THR_ERR_RECOVERED = 1,
3297 /* Detected fatal error, need to exit */
3298 MIG_THR_ERR_FATAL = 2,
3299} MigThrError;
3300
Peter Xu94190692018-05-02 18:47:34 +08003301static int postcopy_resume_handshake(MigrationState *s)
3302{
3303 qemu_savevm_send_postcopy_resume(s->to_dst_file);
3304
3305 while (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
3306 qemu_sem_wait(&s->rp_state.rp_sem);
3307 }
3308
3309 if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
3310 return 0;
3311 }
3312
3313 return -1;
3314}
3315
Peter Xu135b87b2018-05-02 18:47:25 +08003316/* Return zero if success, or <0 for error */
3317static int postcopy_do_resume(MigrationState *s)
3318{
Peter Xud1b8ead2018-05-02 18:47:31 +08003319 int ret;
3320
3321 /*
3322 * Call all the resume_prepare() hooks, so that modules can be
3323 * ready for the migration resume.
3324 */
3325 ret = qemu_savevm_state_resume_prepare(s);
3326 if (ret) {
3327 error_report("%s: resume_prepare() failure detected: %d",
3328 __func__, ret);
3329 return ret;
3330 }
3331
3332 /*
Peter Xu94190692018-05-02 18:47:34 +08003333 * Last handshake with destination on the resume (destination will
3334 * switch to postcopy-active afterwards)
Peter Xud1b8ead2018-05-02 18:47:31 +08003335 */
Peter Xu94190692018-05-02 18:47:34 +08003336 ret = postcopy_resume_handshake(s);
3337 if (ret) {
3338 error_report("%s: handshake failed: %d", __func__, ret);
3339 return ret;
3340 }
Peter Xud1b8ead2018-05-02 18:47:31 +08003341
Peter Xu135b87b2018-05-02 18:47:25 +08003342 return 0;
3343}
3344
Peter Xub23c2ad2018-05-02 18:47:19 +08003345/*
3346 * We don't return until we are in a safe state to continue current
3347 * postcopy migration. Returns MIG_THR_ERR_RECOVERED if recovered, or
3348 * MIG_THR_ERR_FATAL if unrecovery failure happened.
3349 */
3350static MigThrError postcopy_pause(MigrationState *s)
3351{
3352 assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
Peter Xub23c2ad2018-05-02 18:47:19 +08003353
Peter Xu135b87b2018-05-02 18:47:25 +08003354 while (true) {
Peter Xu62df0662018-05-02 18:47:38 +08003355 QEMUFile *file;
3356
Peter Xu39675ff2021-07-22 13:58:41 -04003357 /*
3358 * Current channel is possibly broken. Release it. Note that this is
3359 * guaranteed even without lock because to_dst_file should only be
3360 * modified by the migration thread. That also guarantees that the
3361 * unregister of yank is safe too without the lock. It should be safe
3362 * even to be within the qemu_file_lock, but we didn't do that to avoid
3363 * taking more mutex (yank_lock) within qemu_file_lock. TL;DR: we make
3364 * the qemu_file_lock critical section as small as possible.
3365 */
Peter Xu135b87b2018-05-02 18:47:25 +08003366 assert(s->to_dst_file);
Peter Xu39675ff2021-07-22 13:58:41 -04003367 migration_ioc_unregister_yank_from_file(s->to_dst_file);
Peter Xu62df0662018-05-02 18:47:38 +08003368 qemu_mutex_lock(&s->qemu_file_lock);
3369 file = s->to_dst_file;
Peter Xu135b87b2018-05-02 18:47:25 +08003370 s->to_dst_file = NULL;
Peter Xu62df0662018-05-02 18:47:38 +08003371 qemu_mutex_unlock(&s->qemu_file_lock);
3372
3373 qemu_file_shutdown(file);
3374 qemu_fclose(file);
Peter Xub23c2ad2018-05-02 18:47:19 +08003375
Peter Xud246ea52020-10-21 17:27:20 -04003376 migrate_set_state(&s->state, s->state,
3377 MIGRATION_STATUS_POSTCOPY_PAUSED);
3378
Peter Xu135b87b2018-05-02 18:47:25 +08003379 error_report("Detected IO failure for postcopy. "
3380 "Migration paused.");
3381
3382 /*
3383 * We wait until things fixed up. Then someone will setup the
3384 * status back for us.
3385 */
3386 while (s->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
3387 qemu_sem_wait(&s->postcopy_pause_sem);
3388 }
3389
3390 if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
3391 /* Woken up by a recover procedure. Give it a shot */
3392
3393 /*
3394 * Firstly, let's wake up the return path now, with a new
3395 * return path channel.
3396 */
3397 qemu_sem_post(&s->postcopy_pause_rp_sem);
3398
3399 /* Do the resume logic */
3400 if (postcopy_do_resume(s) == 0) {
3401 /* Let's continue! */
3402 trace_postcopy_pause_continued();
3403 return MIG_THR_ERR_RECOVERED;
3404 } else {
3405 /*
3406 * Something wrong happened during the recovery, let's
3407 * pause again. Pause is always better than throwing
3408 * data away.
3409 */
3410 continue;
3411 }
3412 } else {
3413 /* This is not right... Time to quit. */
3414 return MIG_THR_ERR_FATAL;
3415 }
Peter Xub23c2ad2018-05-02 18:47:19 +08003416 }
Peter Xub23c2ad2018-05-02 18:47:19 +08003417}
3418
3419static MigThrError migration_detect_error(MigrationState *s)
3420{
3421 int ret;
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003422 int state = s->state;
Yury Kotov3d661c82019-04-22 13:34:20 +03003423 Error *local_error = NULL;
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003424
3425 if (state == MIGRATION_STATUS_CANCELLING ||
3426 state == MIGRATION_STATUS_CANCELLED) {
3427 /* End the migration, but don't set the state to failed */
3428 return MIG_THR_ERR_FATAL;
3429 }
Peter Xub23c2ad2018-05-02 18:47:19 +08003430
3431 /* Try to detect any file errors */
Yury Kotov3d661c82019-04-22 13:34:20 +03003432 ret = qemu_file_get_error_obj(s->to_dst_file, &local_error);
Peter Xub23c2ad2018-05-02 18:47:19 +08003433 if (!ret) {
3434 /* Everything is fine */
Yury Kotov3d661c82019-04-22 13:34:20 +03003435 assert(!local_error);
Peter Xub23c2ad2018-05-02 18:47:19 +08003436 return MIG_THR_ERR_NONE;
3437 }
3438
Yury Kotov3d661c82019-04-22 13:34:20 +03003439 if (local_error) {
3440 migrate_set_error(s, local_error);
3441 error_free(local_error);
3442 }
3443
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003444 if (state == MIGRATION_STATUS_POSTCOPY_ACTIVE && ret == -EIO) {
Peter Xub23c2ad2018-05-02 18:47:19 +08003445 /*
3446 * For postcopy, we allow the network to be down for a
3447 * while. After that, it can be continued by a
3448 * recovery phase.
3449 */
3450 return postcopy_pause(s);
3451 } else {
3452 /*
3453 * For precopy (or postcopy with error outside IO), we fail
3454 * with no time.
3455 */
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003456 migrate_set_state(&s->state, state, MIGRATION_STATUS_FAILED);
Peter Xub23c2ad2018-05-02 18:47:19 +08003457 trace_migration_thread_file_err();
3458
3459 /* Time to stop the migration, now. */
3460 return MIG_THR_ERR_FATAL;
3461 }
3462}
3463
Wei Yang268dcd42019-09-12 10:49:57 +08003464/* How many bytes have we transferred since the beginning of the migration */
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003465static uint64_t migration_total_bytes(MigrationState *s)
3466{
Juan Quintelab9ee2f72016-01-15 11:40:13 +01003467 return qemu_ftell(s->to_dst_file) + ram_counters.multifd_bytes;
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003468}
3469
Peter Xucf011f02018-01-03 20:20:11 +08003470static void migration_calculate_complete(MigrationState *s)
3471{
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003472 uint64_t bytes = migration_total_bytes(s);
Peter Xucf011f02018-01-03 20:20:11 +08003473 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela6cde6fb2018-06-26 15:26:35 +02003474 int64_t transfer_time;
Peter Xucf011f02018-01-03 20:20:11 +08003475
3476 s->total_time = end_time - s->start_time;
3477 if (!s->downtime) {
3478 /*
3479 * It's still not set, so we are precopy migration. For
3480 * postcopy, downtime is calculated during postcopy_start().
3481 */
3482 s->downtime = end_time - s->downtime_start;
3483 }
3484
Juan Quintela6cde6fb2018-06-26 15:26:35 +02003485 transfer_time = s->total_time - s->setup_time;
3486 if (transfer_time) {
3487 s->mbps = ((double) bytes * 8.0) / transfer_time / 1000;
Peter Xucf011f02018-01-03 20:20:11 +08003488 }
3489}
3490
Ivan Ren87f3bd82019-08-02 18:18:41 +08003491static void update_iteration_initial_status(MigrationState *s)
3492{
3493 /*
3494 * Update these three fields at the same time to avoid mismatch info lead
3495 * wrong speed calculation.
3496 */
3497 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3498 s->iteration_initial_bytes = migration_total_bytes(s);
3499 s->iteration_initial_pages = ram_get_total_transferred_pages();
3500}
3501
Peter Xub15df1a2018-01-03 20:20:13 +08003502static void migration_update_counters(MigrationState *s,
3503 int64_t current_time)
3504{
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08003505 uint64_t transferred, transferred_pages, time_spent;
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003506 uint64_t current_bytes; /* bytes transferred since the beginning */
Peter Xub15df1a2018-01-03 20:20:13 +08003507 double bandwidth;
3508
3509 if (current_time < s->iteration_start_time + BUFFER_DELAY) {
3510 return;
3511 }
3512
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003513 current_bytes = migration_total_bytes(s);
3514 transferred = current_bytes - s->iteration_initial_bytes;
Peter Xub15df1a2018-01-03 20:20:13 +08003515 time_spent = current_time - s->iteration_start_time;
3516 bandwidth = (double)transferred / time_spent;
Wei Wang0781c1e2018-01-22 19:36:39 +08003517 s->threshold_size = bandwidth * s->parameters.downtime_limit;
Peter Xub15df1a2018-01-03 20:20:13 +08003518
3519 s->mbps = (((double) transferred * 8.0) /
3520 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
3521
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08003522 transferred_pages = ram_get_total_transferred_pages() -
3523 s->iteration_initial_pages;
3524 s->pages_per_second = (double) transferred_pages /
3525 (((double) time_spent / 1000.0));
3526
Peter Xub15df1a2018-01-03 20:20:13 +08003527 /*
3528 * if we haven't sent anything, we don't want to
3529 * recalculate. 10000 is a small enough number for our purposes
3530 */
3531 if (ram_counters.dirty_pages_rate && transferred > 10000) {
Balamuruhan S650af892018-06-12 14:20:09 +05303532 s->expected_downtime = ram_counters.remaining / bandwidth;
Peter Xub15df1a2018-01-03 20:20:13 +08003533 }
3534
3535 qemu_file_reset_rate_limit(s->to_dst_file);
3536
Ivan Ren87f3bd82019-08-02 18:18:41 +08003537 update_iteration_initial_status(s);
Peter Xub15df1a2018-01-03 20:20:13 +08003538
3539 trace_migrate_transferred(transferred, time_spent,
Wei Wang0781c1e2018-01-22 19:36:39 +08003540 bandwidth, s->threshold_size);
Peter Xub15df1a2018-01-03 20:20:13 +08003541}
3542
Peter Xu2ad87302018-01-03 20:20:14 +08003543/* Migration thread iteration status */
3544typedef enum {
3545 MIG_ITERATE_RESUME, /* Resume current iteration */
3546 MIG_ITERATE_SKIP, /* Skip current iteration */
3547 MIG_ITERATE_BREAK, /* Break the loop */
3548} MigIterateState;
3549
3550/*
3551 * Return true if continue to the next iteration directly, false
3552 * otherwise.
3553 */
3554static MigIterateState migration_iteration_run(MigrationState *s)
3555{
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04003556 uint64_t pending_size, pend_pre, pend_compat, pend_post;
Peter Xu2ad87302018-01-03 20:20:14 +08003557 bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE;
3558
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04003559 qemu_savevm_state_pending(s->to_dst_file, s->threshold_size, &pend_pre,
3560 &pend_compat, &pend_post);
3561 pending_size = pend_pre + pend_compat + pend_post;
Peter Xu2ad87302018-01-03 20:20:14 +08003562
3563 trace_migrate_pending(pending_size, s->threshold_size,
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04003564 pend_pre, pend_compat, pend_post);
Peter Xu2ad87302018-01-03 20:20:14 +08003565
3566 if (pending_size && pending_size >= s->threshold_size) {
3567 /* Still a significant amount to transfer */
Wei Yang52aec702019-07-18 16:37:47 +08003568 if (!in_postcopy && pend_pre <= s->threshold_size &&
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01003569 qatomic_read(&s->start_postcopy)) {
Peter Xu2ad87302018-01-03 20:20:14 +08003570 if (postcopy_start(s)) {
3571 error_report("%s: postcopy failed to start", __func__);
3572 }
3573 return MIG_ITERATE_SKIP;
3574 }
3575 /* Just another iteration step */
Wei Yang17d93512019-10-06 06:05:16 +08003576 qemu_savevm_state_iterate(s->to_dst_file, in_postcopy);
Peter Xu2ad87302018-01-03 20:20:14 +08003577 } else {
3578 trace_migration_thread_low_pending(pending_size);
3579 migration_completion(s);
3580 return MIG_ITERATE_BREAK;
3581 }
3582
3583 return MIG_ITERATE_RESUME;
3584}
3585
Peter Xu199aa6d2018-01-03 20:20:15 +08003586static void migration_iteration_finish(MigrationState *s)
3587{
3588 /* If we enabled cpu throttling for auto-converge, turn it off. */
3589 cpu_throttle_stop();
3590
3591 qemu_mutex_lock_iothread();
3592 switch (s->state) {
3593 case MIGRATION_STATUS_COMPLETED:
3594 migration_calculate_complete(s);
3595 runstate_set(RUN_STATE_POSTMIGRATE);
3596 break;
3597
3598 case MIGRATION_STATUS_ACTIVE:
3599 /*
3600 * We should really assert here, but since it's during
3601 * migration, let's try to reduce the usage of assertions.
3602 */
3603 if (!migrate_colo_enabled()) {
3604 error_report("%s: critical error: calling COLO code without "
3605 "COLO enabled", __func__);
3606 }
3607 migrate_start_colo_process(s);
3608 /*
3609 * Fixme: we will run VM in COLO no matter its old running state.
3610 * After exited COLO, we will keep running.
3611 */
3612 s->vm_was_running = true;
3613 /* Fallthrough */
3614 case MIGRATION_STATUS_FAILED:
3615 case MIGRATION_STATUS_CANCELLED:
Dr. David Alan Gilbert57225e52018-07-19 10:22:57 +01003616 case MIGRATION_STATUS_CANCELLING:
Peter Xu199aa6d2018-01-03 20:20:15 +08003617 if (s->vm_was_running) {
3618 vm_start();
3619 } else {
3620 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
3621 runstate_set(RUN_STATE_POSTMIGRATE);
3622 }
3623 }
3624 break;
3625
3626 default:
3627 /* Should not reach here, but if so, forgive the VM. */
3628 error_report("%s: Unknown ending state %d", __func__, s->state);
3629 break;
3630 }
Yury Kotovfd392cf2019-04-08 14:33:43 +03003631 migrate_fd_cleanup_schedule(s);
Peter Xu199aa6d2018-01-03 20:20:15 +08003632 qemu_mutex_unlock_iothread();
3633}
3634
Andrey Gruzdev85182782021-01-29 13:14:06 +03003635static void bg_migration_iteration_finish(MigrationState *s)
3636{
3637 qemu_mutex_lock_iothread();
3638 switch (s->state) {
3639 case MIGRATION_STATUS_COMPLETED:
3640 migration_calculate_complete(s);
3641 break;
3642
3643 case MIGRATION_STATUS_ACTIVE:
3644 case MIGRATION_STATUS_FAILED:
3645 case MIGRATION_STATUS_CANCELLED:
3646 case MIGRATION_STATUS_CANCELLING:
3647 break;
3648
3649 default:
3650 /* Should not reach here, but if so, forgive the VM. */
3651 error_report("%s: Unknown ending state %d", __func__, s->state);
3652 break;
3653 }
3654
3655 migrate_fd_cleanup_schedule(s);
3656 qemu_mutex_unlock_iothread();
3657}
3658
3659/*
3660 * Return true if continue to the next iteration directly, false
3661 * otherwise.
3662 */
3663static MigIterateState bg_migration_iteration_run(MigrationState *s)
3664{
3665 int res;
3666
3667 res = qemu_savevm_state_iterate(s->to_dst_file, false);
3668 if (res > 0) {
3669 bg_migration_completion(s);
3670 return MIG_ITERATE_BREAK;
3671 }
3672
3673 return MIG_ITERATE_RESUME;
3674}
3675
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003676void migration_make_urgent_request(void)
3677{
3678 qemu_sem_post(&migrate_get_current()->rate_limit_sem);
3679}
3680
3681void migration_consume_urgent_request(void)
3682{
3683 qemu_sem_wait(&migrate_get_current()->rate_limit_sem);
3684}
3685
Dr. David Alan Gilbert97e1e062019-12-05 10:29:18 +00003686/* Returns true if the rate limiting was broken by an urgent request */
3687bool migration_rate_limit(void)
3688{
3689 int64_t now = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3690 MigrationState *s = migrate_get_current();
3691
3692 bool urgent = false;
3693 migration_update_counters(s, now);
3694 if (qemu_file_rate_limit(s->to_dst_file)) {
Lukas Straub77386122020-05-20 22:42:32 +02003695
3696 if (qemu_file_get_error(s->to_dst_file)) {
3697 return false;
3698 }
Dr. David Alan Gilbert97e1e062019-12-05 10:29:18 +00003699 /*
3700 * Wait for a delay to do rate limiting OR
3701 * something urgent to post the semaphore.
3702 */
3703 int ms = s->iteration_start_time + BUFFER_DELAY - now;
3704 trace_migration_rate_limit_pre(ms);
3705 if (qemu_sem_timedwait(&s->rate_limit_sem, ms) == 0) {
3706 /*
3707 * We were woken by one or more urgent things but
3708 * the timedwait will have consumed one of them.
3709 * The service routine for the urgent wake will dec
3710 * the semaphore itself for each item it consumes,
3711 * so add this one we just eat back.
3712 */
3713 qemu_sem_post(&s->rate_limit_sem);
3714 urgent = true;
3715 }
3716 trace_migration_rate_limit_post(urgent);
3717 }
3718 return urgent;
3719}
3720
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00003721/*
Laurent Vivierfde93d92021-06-29 17:50:06 +02003722 * if failover devices are present, wait they are completely
3723 * unplugged
3724 */
3725
3726static void qemu_savevm_wait_unplug(MigrationState *s, int old_state,
3727 int new_state)
3728{
3729 if (qemu_savevm_state_guest_unplug_pending()) {
3730 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_WAIT_UNPLUG);
3731
3732 while (s->state == MIGRATION_STATUS_WAIT_UNPLUG &&
3733 qemu_savevm_state_guest_unplug_pending()) {
3734 qemu_sem_timedwait(&s->wait_unplug_sem, 250);
3735 }
Laurent Vivier944bc522021-06-29 17:50:07 +02003736 if (s->state != MIGRATION_STATUS_WAIT_UNPLUG) {
3737 int timeout = 120; /* 30 seconds */
3738 /*
3739 * migration has been canceled
3740 * but as we have started an unplug we must wait the end
3741 * to be able to plug back the card
3742 */
3743 while (timeout-- && qemu_savevm_state_guest_unplug_pending()) {
3744 qemu_sem_timedwait(&s->wait_unplug_sem, 250);
3745 }
Laurent Viviera51dcef2021-07-01 15:14:58 +02003746 if (qemu_savevm_state_guest_unplug_pending()) {
3747 warn_report("migration: partially unplugged device on "
3748 "failure");
3749 }
Laurent Vivier944bc522021-06-29 17:50:07 +02003750 }
Laurent Vivierfde93d92021-06-29 17:50:06 +02003751
3752 migrate_set_state(&s->state, MIGRATION_STATUS_WAIT_UNPLUG, new_state);
3753 } else {
3754 migrate_set_state(&s->state, old_state, new_state);
3755 }
3756}
3757
3758/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00003759 * Master migration thread on the source VM.
3760 * It drives the migration and pumps the data down the outgoing channel.
3761 */
Juan Quintela5f496a12013-02-22 17:36:30 +01003762static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003763{
Juan Quintela9848a402012-12-19 09:55:50 +01003764 MigrationState *s = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +01003765 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Peter Xub23c2ad2018-05-02 18:47:19 +08003766 MigThrError thr_error;
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003767 bool urgent = false;
Juan Quintela76f59332012-10-03 20:16:24 +02003768
Paolo Bonziniab28bd22015-07-09 08:55:38 +02003769 rcu_register_thread();
3770
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +00003771 object_ref(OBJECT(s));
Ivan Ren87f3bd82019-08-02 18:18:41 +08003772 update_iteration_initial_status(s);
Peter Xub15df1a2018-01-03 20:20:13 +08003773
zhanghailiang89a02a92016-01-15 11:37:42 +08003774 qemu_savevm_state_header(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003775
Peter Xu62a02652017-06-14 15:55:58 +08003776 /*
3777 * If we opened the return path, we need to make sure dst has it
3778 * opened as well.
3779 */
Peter Xu43044ac2021-07-22 13:58:38 -04003780 if (s->rp_state.rp_thread_created) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003781 /* Now tell the dest that it should open its end so it can reply */
zhanghailiang89a02a92016-01-15 11:37:42 +08003782 qemu_savevm_send_open_return_path(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003783
3784 /* And do a ping that will make stuff easier to debug */
zhanghailiang89a02a92016-01-15 11:37:42 +08003785 qemu_savevm_send_ping(s->to_dst_file, 1);
Peter Xu0425dc92017-05-31 18:35:34 +08003786 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003787
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03003788 if (migrate_postcopy()) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003789 /*
3790 * Tell the destination that we *might* want to do postcopy later;
3791 * if the other end can't do postcopy it should fail now, nice and
3792 * early.
3793 */
zhanghailiang89a02a92016-01-15 11:37:42 +08003794 qemu_savevm_send_postcopy_advise(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003795 }
3796
Zhang Chenaad555c2018-09-03 12:38:47 +08003797 if (migrate_colo_enabled()) {
3798 /* Notify migration destination that we enable COLO */
3799 qemu_savevm_send_colo_enable(s->to_dst_file);
3800 }
3801
Juan Quintela9907e842017-06-28 11:52:24 +02003802 qemu_savevm_state_setup(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003803
Laurent Vivierfde93d92021-06-29 17:50:06 +02003804 qemu_savevm_wait_unplug(s, MIGRATION_STATUS_SETUP,
3805 MIGRATION_STATUS_ACTIVE);
Jens Freimannc7e0acd2019-10-29 12:49:02 +01003806
Alex Blighbc72ad62013-08-21 16:03:08 +01003807 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
Michael R. Hines29ae8a42013-07-22 10:01:57 -04003808
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00003809 trace_migration_thread_setup_complete();
3810
Wei Yang8f8d5282019-07-17 08:53:41 +08003811 while (migration_is_active(s)) {
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003812 if (urgent || !qemu_file_rate_limit(s->to_dst_file)) {
Peter Xu2ad87302018-01-03 20:20:14 +08003813 MigIterateState iter_state = migration_iteration_run(s);
3814 if (iter_state == MIG_ITERATE_SKIP) {
3815 continue;
3816 } else if (iter_state == MIG_ITERATE_BREAK) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003817 break;
Juan Quintelac369f402012-10-03 20:33:34 +02003818 }
3819 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01003820
Peter Xub23c2ad2018-05-02 18:47:19 +08003821 /*
3822 * Try to detect any kind of failures, and see whether we
3823 * should stop the migration now.
3824 */
3825 thr_error = migration_detect_error(s);
3826 if (thr_error == MIG_THR_ERR_FATAL) {
3827 /* Stop migration */
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01003828 break;
Peter Xub23c2ad2018-05-02 18:47:19 +08003829 } else if (thr_error == MIG_THR_ERR_RECOVERED) {
3830 /*
3831 * Just recovered from a e.g. network failure, reset all
3832 * the local variables. This is important to avoid
3833 * breaking transferred_bytes and bandwidth calculation
3834 */
Ivan Ren87f3bd82019-08-02 18:18:41 +08003835 update_iteration_initial_status(s);
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01003836 }
Peter Xub15df1a2018-01-03 20:20:13 +08003837
Dr. David Alan Gilbert97e1e062019-12-05 10:29:18 +00003838 urgent = migration_rate_limit();
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01003839 }
3840
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003841 trace_migration_thread_after_loop();
Peter Xu199aa6d2018-01-03 20:20:15 +08003842 migration_iteration_finish(s);
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +00003843 object_unref(OBJECT(s));
Paolo Bonziniab28bd22015-07-09 08:55:38 +02003844 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003845 return NULL;
3846}
3847
Andrey Gruzdev85182782021-01-29 13:14:06 +03003848static void bg_migration_vm_start_bh(void *opaque)
3849{
3850 MigrationState *s = opaque;
3851
3852 qemu_bh_delete(s->vm_start_bh);
3853 s->vm_start_bh = NULL;
3854
3855 vm_start();
3856 s->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - s->downtime_start;
3857}
3858
3859/**
3860 * Background snapshot thread, based on live migration code.
3861 * This is an alternative implementation of live migration mechanism
3862 * introduced specifically to support background snapshots.
3863 *
3864 * It takes advantage of userfault_fd write protection mechanism introduced
3865 * in v5.7 kernel. Compared to existing dirty page logging migration much
3866 * lesser stream traffic is produced resulting in smaller snapshot images,
3867 * simply cause of no page duplicates can get into the stream.
3868 *
3869 * Another key point is that generated vmstate stream reflects machine state
3870 * 'frozen' at the beginning of snapshot creation compared to dirty page logging
3871 * mechanism, which effectively results in that saved snapshot is the state of VM
3872 * at the end of the process.
3873 */
3874static void *bg_migration_thread(void *opaque)
3875{
3876 MigrationState *s = opaque;
3877 int64_t setup_start;
3878 MigThrError thr_error;
3879 QEMUFile *fb;
3880 bool early_fail = true;
3881
3882 rcu_register_thread();
3883 object_ref(OBJECT(s));
3884
3885 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
3886
3887 setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
3888 /*
3889 * We want to save vmstate for the moment when migration has been
3890 * initiated but also we want to save RAM content while VM is running.
3891 * The RAM content should appear first in the vmstate. So, we first
3892 * stash the non-RAM part of the vmstate to the temporary buffer,
3893 * then write RAM part of the vmstate to the migration stream
3894 * with vCPUs running and, finally, write stashed non-RAM part of
3895 * the vmstate from the buffer to the migration stream.
3896 */
Andrey Gruzdevecb23ef2021-04-01 12:22:23 +03003897 s->bioc = qio_channel_buffer_new(512 * 1024);
Andrey Gruzdev85182782021-01-29 13:14:06 +03003898 qio_channel_set_name(QIO_CHANNEL(s->bioc), "vmstate-buffer");
3899 fb = qemu_fopen_channel_output(QIO_CHANNEL(s->bioc));
3900 object_unref(OBJECT(s->bioc));
3901
3902 update_iteration_initial_status(s);
3903
Andrey Gruzdeveeccb992021-04-01 12:22:25 +03003904 /*
3905 * Prepare for tracking memory writes with UFFD-WP - populate
3906 * RAM pages before protecting.
3907 */
3908#ifdef __linux__
3909 ram_write_tracking_prepare();
3910#endif
3911
Andrey Gruzdev85182782021-01-29 13:14:06 +03003912 qemu_savevm_state_header(s->to_dst_file);
3913 qemu_savevm_state_setup(s->to_dst_file);
3914
Laurent Vivierfde93d92021-06-29 17:50:06 +02003915 qemu_savevm_wait_unplug(s, MIGRATION_STATUS_SETUP,
3916 MIGRATION_STATUS_ACTIVE);
Andrey Gruzdev85182782021-01-29 13:14:06 +03003917
Andrey Gruzdev85182782021-01-29 13:14:06 +03003918 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
3919
3920 trace_migration_thread_setup_complete();
3921 s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3922
3923 qemu_mutex_lock_iothread();
3924
3925 /*
3926 * If VM is currently in suspended state, then, to make a valid runstate
3927 * transition in vm_stop_force_state() we need to wakeup it up.
3928 */
3929 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
3930 s->vm_was_running = runstate_is_running();
3931
3932 if (global_state_store()) {
3933 goto fail;
3934 }
3935 /* Forcibly stop VM before saving state of vCPUs and devices */
3936 if (vm_stop_force_state(RUN_STATE_PAUSED)) {
3937 goto fail;
3938 }
3939 /*
3940 * Put vCPUs in sync with shadow context structures, then
3941 * save their state to channel-buffer along with devices.
3942 */
3943 cpu_synchronize_all_states();
3944 if (qemu_savevm_state_complete_precopy_non_iterable(fb, false, false)) {
3945 goto fail;
3946 }
Andrey Gruzdevecb23ef2021-04-01 12:22:23 +03003947 /*
3948 * Since we are going to get non-iterable state data directly
3949 * from s->bioc->data, explicit flush is needed here.
3950 */
3951 qemu_fflush(fb);
3952
Andrey Gruzdev85182782021-01-29 13:14:06 +03003953 /* Now initialize UFFD context and start tracking RAM writes */
3954 if (ram_write_tracking_start()) {
3955 goto fail;
3956 }
3957 early_fail = false;
3958
3959 /*
3960 * Start VM from BH handler to avoid write-fault lock here.
3961 * UFFD-WP protection for the whole RAM is already enabled so
3962 * calling VM state change notifiers from vm_start() would initiate
3963 * writes to virtio VQs memory which is in write-protected region.
3964 */
3965 s->vm_start_bh = qemu_bh_new(bg_migration_vm_start_bh, s);
3966 qemu_bh_schedule(s->vm_start_bh);
3967
3968 qemu_mutex_unlock_iothread();
3969
3970 while (migration_is_active(s)) {
3971 MigIterateState iter_state = bg_migration_iteration_run(s);
3972 if (iter_state == MIG_ITERATE_SKIP) {
3973 continue;
3974 } else if (iter_state == MIG_ITERATE_BREAK) {
3975 break;
3976 }
3977
3978 /*
3979 * Try to detect any kind of failures, and see whether we
3980 * should stop the migration now.
3981 */
3982 thr_error = migration_detect_error(s);
3983 if (thr_error == MIG_THR_ERR_FATAL) {
3984 /* Stop migration */
3985 break;
3986 }
3987
3988 migration_update_counters(s, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
3989 }
3990
3991 trace_migration_thread_after_loop();
3992
3993fail:
3994 if (early_fail) {
3995 migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE,
3996 MIGRATION_STATUS_FAILED);
3997 qemu_mutex_unlock_iothread();
3998 }
3999
4000 bg_migration_iteration_finish(s);
4001
4002 qemu_fclose(fb);
4003 object_unref(OBJECT(s));
4004 rcu_unregister_thread();
4005
4006 return NULL;
4007}
4008
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00004009void migrate_fd_connect(MigrationState *s, Error *error_in)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02004010{
Juan Quintela00f4b572019-06-12 11:33:27 +02004011 Error *local_err = NULL;
Peter Xud3e35b82018-05-02 18:47:24 +08004012 int64_t rate_limit;
4013 bool resume = s->state == MIGRATION_STATUS_POSTCOPY_PAUSED;
4014
Peter Xuca7bd082021-07-08 15:06:53 -04004015 /*
4016 * If there's a previous error, free it and prepare for another one.
4017 * Meanwhile if migration completes successfully, there won't have an error
4018 * dumped when calling migrate_fd_cleanup().
4019 */
4020 migrate_error_free(s);
4021
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05304022 s->expected_downtime = s->parameters.downtime_limit;
Marc-André Lureau9cbc3642020-03-25 19:47:21 +01004023 if (resume) {
4024 assert(s->cleanup_bh);
4025 } else {
4026 assert(!s->cleanup_bh);
4027 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup_bh, s);
4028 }
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00004029 if (error_in) {
4030 migrate_fd_error(s, error_in);
Peter Xuca30f242021-07-08 15:06:52 -04004031 if (resume) {
4032 /*
4033 * Don't do cleanup for resume if channel is invalid, but only dump
4034 * the error. We wait for another channel connect from the user.
4035 * The error_report still gives HMP user a hint on what failed.
4036 * It's normally done in migrate_fd_cleanup(), but call it here
4037 * explicitly.
4038 */
4039 error_report_err(error_copy(s->error));
4040 } else {
4041 migrate_fd_cleanup(s);
4042 }
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00004043 return;
4044 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02004045
Peter Xud3e35b82018-05-02 18:47:24 +08004046 if (resume) {
4047 /* This is a resumed migration */
Peter Xu8504dde2019-09-06 21:01:03 +08004048 rate_limit = s->parameters.max_postcopy_bandwidth /
4049 XFER_LIMIT_RATIO;
Peter Xud3e35b82018-05-02 18:47:24 +08004050 } else {
4051 /* This is a fresh new migration */
4052 rate_limit = s->parameters.max_bandwidth / XFER_LIMIT_RATIO;
Paolo Bonzini442773c2013-02-22 17:36:44 +01004053
Peter Xud3e35b82018-05-02 18:47:24 +08004054 /* Notify before starting migration thread */
4055 notifier_list_notify(&migration_state_notifiers, s);
4056 }
4057
4058 qemu_file_set_rate_limit(s->to_dst_file, rate_limit);
4059 qemu_file_set_blocking(s->to_dst_file, true);
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02004060
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00004061 /*
Peter Xuc788ada2017-06-26 18:28:55 +08004062 * Open the return path. For postcopy, it is used exclusively. For
4063 * precopy, only if user specified "return-path" capability would
4064 * QEMU uses the return path.
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00004065 */
Peter Xuc788ada2017-06-26 18:28:55 +08004066 if (migrate_postcopy_ram() || migrate_use_return_path()) {
Peter Xud3e35b82018-05-02 18:47:24 +08004067 if (open_return_path_on_source(s, !resume)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00004068 error_report("Unable to open return-path for postcopy");
Peter Xud3e35b82018-05-02 18:47:24 +08004069 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00004070 migrate_fd_cleanup(s);
4071 return;
4072 }
4073 }
4074
Peter Xud3e35b82018-05-02 18:47:24 +08004075 if (resume) {
Peter Xu135b87b2018-05-02 18:47:25 +08004076 /* Wakeup the main migration thread to do the recovery */
4077 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
4078 MIGRATION_STATUS_POSTCOPY_RECOVER);
4079 qemu_sem_post(&s->postcopy_pause_sem);
Peter Xud3e35b82018-05-02 18:47:24 +08004080 return;
4081 }
4082
Juan Quintela00f4b572019-06-12 11:33:27 +02004083 if (multifd_save_setup(&local_err) != 0) {
4084 error_report_err(local_err);
Juan Quintelaf986c3d2016-01-14 16:52:55 +01004085 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
4086 MIGRATION_STATUS_FAILED);
4087 migrate_fd_cleanup(s);
4088 return;
4089 }
Andrey Gruzdev85182782021-01-29 13:14:06 +03004090
4091 if (migrate_background_snapshot()) {
4092 qemu_thread_create(&s->thread, "bg_snapshot",
4093 bg_migration_thread, s, QEMU_THREAD_JOINABLE);
4094 } else {
4095 qemu_thread_create(&s->thread, "live_migration",
4096 migration_thread, s, QEMU_THREAD_JOINABLE);
4097 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00004098 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02004099}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00004100
Peter Xu9d18af92017-06-27 12:10:19 +08004101void migration_global_dump(Monitor *mon)
4102{
4103 MigrationState *ms = migrate_get_current();
4104
Juan Quintela6f0f6422017-10-26 11:49:57 +02004105 monitor_printf(mon, "globals:\n");
4106 monitor_printf(mon, "store-global-state: %s\n",
4107 ms->store_global_state ? "on" : "off");
4108 monitor_printf(mon, "only-migratable: %s\n",
Markus Armbruster811f8652019-04-01 11:08:24 +02004109 only_migratable ? "on" : "off");
Juan Quintela6f0f6422017-10-26 11:49:57 +02004110 monitor_printf(mon, "send-configuration: %s\n",
4111 ms->send_configuration ? "on" : "off");
4112 monitor_printf(mon, "send-section-footer: %s\n",
4113 ms->send_section_footer ? "on" : "off");
Xiao Guangrongf5482222018-05-03 16:06:11 +08004114 monitor_printf(mon, "decompress-error-check: %s\n",
4115 ms->decompress_error_check ? "on" : "off");
Peter Xu002cad62019-06-03 14:50:56 +08004116 monitor_printf(mon, "clear-bitmap-shift: %u\n",
4117 ms->clear_bitmap_shift);
Peter Xu9d18af92017-06-27 12:10:19 +08004118}
4119
Peter Xu20814752017-07-18 11:39:03 +08004120#define DEFINE_PROP_MIG_CAP(name, x) \
4121 DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
4122
Peter Xu52722982017-06-27 12:10:14 +08004123static Property migration_properties[] = {
4124 DEFINE_PROP_BOOL("store-global-state", MigrationState,
4125 store_global_state, true),
Peter Xu71dd4c12017-06-27 12:10:16 +08004126 DEFINE_PROP_BOOL("send-configuration", MigrationState,
4127 send_configuration, true),
Peter Xu15c38502017-06-27 12:10:17 +08004128 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
4129 send_section_footer, true),
Xiao Guangrongf5482222018-05-03 16:06:11 +08004130 DEFINE_PROP_BOOL("decompress-error-check", MigrationState,
4131 decompress_error_check, true),
Peter Xu002cad62019-06-03 14:50:56 +08004132 DEFINE_PROP_UINT8("x-clear-bitmap-shift", MigrationState,
4133 clear_bitmap_shift, CLEAR_BITMAP_SHIFT_DEFAULT),
Peter Xu89632fa2017-07-18 11:39:02 +08004134
4135 /* Migration parameters */
Juan Quintela741d4082017-12-01 13:08:38 +01004136 DEFINE_PROP_UINT8("x-compress-level", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08004137 parameters.compress_level,
4138 DEFAULT_MIGRATE_COMPRESS_LEVEL),
Juan Quintela741d4082017-12-01 13:08:38 +01004139 DEFINE_PROP_UINT8("x-compress-threads", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08004140 parameters.compress_threads,
4141 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
Xiao Guangrong1d588722018-08-21 16:10:20 +08004142 DEFINE_PROP_BOOL("x-compress-wait-thread", MigrationState,
4143 parameters.compress_wait_thread, true),
Juan Quintela741d4082017-12-01 13:08:38 +01004144 DEFINE_PROP_UINT8("x-decompress-threads", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08004145 parameters.decompress_threads,
4146 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
Keqian Zhudc14a472020-02-24 10:31:42 +08004147 DEFINE_PROP_UINT8("x-throttle-trigger-threshold", MigrationState,
4148 parameters.throttle_trigger_threshold,
4149 DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD),
Juan Quintela741d4082017-12-01 13:08:38 +01004150 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08004151 parameters.cpu_throttle_initial,
4152 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
Juan Quintela741d4082017-12-01 13:08:38 +01004153 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08004154 parameters.cpu_throttle_increment,
4155 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
Keqian Zhucbbf8182020-04-13 18:15:08 +08004156 DEFINE_PROP_BOOL("x-cpu-throttle-tailslow", MigrationState,
4157 parameters.cpu_throttle_tailslow, false),
Juan Quintela741d4082017-12-01 13:08:38 +01004158 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08004159 parameters.max_bandwidth, MAX_THROTTLE),
Juan Quintela741d4082017-12-01 13:08:38 +01004160 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08004161 parameters.downtime_limit,
4162 DEFAULT_MIGRATE_SET_DOWNTIME),
Juan Quintela741d4082017-12-01 13:08:38 +01004163 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08004164 parameters.x_checkpoint_delay,
4165 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
Juan Quintelacbfd6c92019-02-06 13:54:06 +01004166 DEFINE_PROP_UINT8("multifd-channels", MigrationState,
4167 parameters.multifd_channels,
Juan Quintela4075fb12016-01-15 08:56:17 +01004168 DEFAULT_MIGRATE_MULTIFD_CHANNELS),
Juan Quintela96eef042019-01-16 10:35:55 +01004169 DEFINE_PROP_MULTIFD_COMPRESSION("multifd-compression", MigrationState,
4170 parameters.multifd_compression,
4171 DEFAULT_MIGRATE_MULTIFD_COMPRESSION),
Juan Quintela9004db42020-01-23 17:08:52 +01004172 DEFINE_PROP_UINT8("multifd-zlib-level", MigrationState,
4173 parameters.multifd_zlib_level,
4174 DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL),
Juan Quintela6a9ad152020-01-23 17:41:36 +01004175 DEFINE_PROP_UINT8("multifd-zstd-level", MigrationState,
4176 parameters.multifd_zstd_level,
4177 DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL),
Juan Quintela73af8dd2017-10-05 21:30:10 +02004178 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
4179 parameters.xbzrle_cache_size,
4180 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01004181 DEFINE_PROP_SIZE("max-postcopy-bandwidth", MigrationState,
4182 parameters.max_postcopy_bandwidth,
4183 DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH),
Li Qiang4cbc9c72018-08-01 06:00:20 -07004184 DEFINE_PROP_UINT8("max-cpu-throttle", MigrationState,
4185 parameters.max_cpu_throttle,
4186 DEFAULT_MIGRATE_MAX_CPU_THROTTLE),
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00004187 DEFINE_PROP_SIZE("announce-initial", MigrationState,
4188 parameters.announce_initial,
4189 DEFAULT_MIGRATE_ANNOUNCE_INITIAL),
4190 DEFINE_PROP_SIZE("announce-max", MigrationState,
4191 parameters.announce_max,
4192 DEFAULT_MIGRATE_ANNOUNCE_MAX),
4193 DEFINE_PROP_SIZE("announce-rounds", MigrationState,
4194 parameters.announce_rounds,
4195 DEFAULT_MIGRATE_ANNOUNCE_ROUNDS),
4196 DEFINE_PROP_SIZE("announce-step", MigrationState,
4197 parameters.announce_step,
4198 DEFAULT_MIGRATE_ANNOUNCE_STEP),
Peter Xu20814752017-07-18 11:39:03 +08004199
4200 /* Migration capabilities */
4201 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
4202 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
4203 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
4204 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
4205 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
4206 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
4207 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
4208 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
4209 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
4210 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
4211 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
Juan Quintelacbfd6c92019-02-06 13:54:06 +01004212 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_MULTIFD),
Andrey Gruzdev6e8c25b2021-01-29 13:14:03 +03004213 DEFINE_PROP_MIG_CAP("x-background-snapshot",
4214 MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT),
Peter Xu20814752017-07-18 11:39:03 +08004215
Peter Xu52722982017-06-27 12:10:14 +08004216 DEFINE_PROP_END_OF_LIST(),
4217};
4218
Peter Xue5cb7e72017-06-27 12:10:13 +08004219static void migration_class_init(ObjectClass *klass, void *data)
4220{
4221 DeviceClass *dc = DEVICE_CLASS(klass);
4222
4223 dc->user_creatable = false;
Marc-André Lureau4f67d302020-01-10 19:30:32 +04004224 device_class_set_props(dc, migration_properties);
Peter Xue5cb7e72017-06-27 12:10:13 +08004225}
4226
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01004227static void migration_instance_finalize(Object *obj)
4228{
4229 MigrationState *ms = MIGRATION_OBJ(obj);
4230 MigrationParameters *params = &ms->parameters;
4231
Juan Quintela87db1a72017-09-05 12:50:22 +02004232 qemu_mutex_destroy(&ms->error_mutex);
Peter Xu62df0662018-05-02 18:47:38 +08004233 qemu_mutex_destroy(&ms->qemu_file_lock);
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01004234 g_free(params->tls_hostname);
4235 g_free(params->tls_creds);
Jens Freimannc7e0acd2019-10-29 12:49:02 +01004236 qemu_sem_destroy(&ms->wait_unplug_sem);
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01004237 qemu_sem_destroy(&ms->rate_limit_sem);
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01004238 qemu_sem_destroy(&ms->pause_sem);
Peter Xub23c2ad2018-05-02 18:47:19 +08004239 qemu_sem_destroy(&ms->postcopy_pause_sem);
Peter Xu14b17422018-05-02 18:47:21 +08004240 qemu_sem_destroy(&ms->postcopy_pause_rp_sem);
Peter Xuedd090c2018-05-02 18:47:32 +08004241 qemu_sem_destroy(&ms->rp_state.rp_sem);
Marc-André Lureauab105cc2018-03-06 18:09:59 +01004242 error_free(ms->error);
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01004243}
4244
Peter Xue5cb7e72017-06-27 12:10:13 +08004245static void migration_instance_init(Object *obj)
4246{
4247 MigrationState *ms = MIGRATION_OBJ(obj);
Peter Xu8b0b29d2017-07-18 11:39:06 +08004248 MigrationParameters *params = &ms->parameters;
Peter Xue5cb7e72017-06-27 12:10:13 +08004249
4250 ms->state = MIGRATION_STATUS_NONE;
Peter Xue5cb7e72017-06-27 12:10:13 +08004251 ms->mbps = -1;
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08004252 ms->pages_per_second = -1;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01004253 qemu_sem_init(&ms->pause_sem, 0);
Juan Quintela87db1a72017-09-05 12:50:22 +02004254 qemu_mutex_init(&ms->error_mutex);
Peter Xu8b0b29d2017-07-18 11:39:06 +08004255
4256 params->tls_hostname = g_strdup("");
4257 params->tls_creds = g_strdup("");
4258
4259 /* Set has_* up only for parameter checks */
4260 params->has_compress_level = true;
4261 params->has_compress_threads = true;
4262 params->has_decompress_threads = true;
Keqian Zhudc14a472020-02-24 10:31:42 +08004263 params->has_throttle_trigger_threshold = true;
Peter Xu8b0b29d2017-07-18 11:39:06 +08004264 params->has_cpu_throttle_initial = true;
4265 params->has_cpu_throttle_increment = true;
Keqian Zhucbbf8182020-04-13 18:15:08 +08004266 params->has_cpu_throttle_tailslow = true;
Peter Xu8b0b29d2017-07-18 11:39:06 +08004267 params->has_max_bandwidth = true;
4268 params->has_downtime_limit = true;
4269 params->has_x_checkpoint_delay = true;
4270 params->has_block_incremental = true;
Juan Quintelacbfd6c92019-02-06 13:54:06 +01004271 params->has_multifd_channels = true;
Juan Quintela96eef042019-01-16 10:35:55 +01004272 params->has_multifd_compression = true;
Juan Quintela9004db42020-01-23 17:08:52 +01004273 params->has_multifd_zlib_level = true;
Juan Quintela6a9ad152020-01-23 17:41:36 +01004274 params->has_multifd_zstd_level = true;
Juan Quintela73af8dd2017-10-05 21:30:10 +02004275 params->has_xbzrle_cache_size = true;
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01004276 params->has_max_postcopy_bandwidth = true;
Li Qiang4cbc9c72018-08-01 06:00:20 -07004277 params->has_max_cpu_throttle = true;
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00004278 params->has_announce_initial = true;
4279 params->has_announce_max = true;
4280 params->has_announce_rounds = true;
4281 params->has_announce_step = true;
Peter Xub23c2ad2018-05-02 18:47:19 +08004282
4283 qemu_sem_init(&ms->postcopy_pause_sem, 0);
Peter Xu14b17422018-05-02 18:47:21 +08004284 qemu_sem_init(&ms->postcopy_pause_rp_sem, 0);
Peter Xuedd090c2018-05-02 18:47:32 +08004285 qemu_sem_init(&ms->rp_state.rp_sem, 0);
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01004286 qemu_sem_init(&ms->rate_limit_sem, 0);
Jens Freimannc7e0acd2019-10-29 12:49:02 +01004287 qemu_sem_init(&ms->wait_unplug_sem, 0);
Peter Xu62df0662018-05-02 18:47:38 +08004288 qemu_mutex_init(&ms->qemu_file_lock);
Peter Xu8b0b29d2017-07-18 11:39:06 +08004289}
4290
4291/*
4292 * Return true if check pass, false otherwise. Error will be put
4293 * inside errp if provided.
4294 */
4295static bool migration_object_check(MigrationState *ms, Error **errp)
4296{
Peter Xu6b19a7d2017-07-18 11:39:10 +08004297 MigrationCapabilityStatusList *head = NULL;
4298 /* Assuming all off */
4299 bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret;
4300 int i;
4301
Peter Xu8b0b29d2017-07-18 11:39:06 +08004302 if (!migrate_params_check(&ms->parameters, errp)) {
4303 return false;
4304 }
4305
Peter Xu6b19a7d2017-07-18 11:39:10 +08004306 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
4307 if (ms->enabled_capabilities[i]) {
Eric Blakeeaedde52020-11-12 19:13:36 -06004308 QAPI_LIST_PREPEND(head, migrate_cap_add(i, true));
Peter Xu6b19a7d2017-07-18 11:39:10 +08004309 }
4310 }
4311
4312 ret = migrate_caps_check(cap_list, head, errp);
4313
4314 /* It works with head == NULL */
4315 qapi_free_MigrationCapabilityStatusList(head);
4316
4317 return ret;
Peter Xue5cb7e72017-06-27 12:10:13 +08004318}
4319
4320static const TypeInfo migration_type = {
4321 .name = TYPE_MIGRATION,
Peter Xu01f6e142017-06-28 15:15:44 +08004322 /*
Peter Xuc8d3ff32017-07-05 16:21:23 +08004323 * NOTE: TYPE_MIGRATION is not really a device, as the object is
Markus Armbruster2194abd2020-06-10 07:32:19 +02004324 * not created using qdev_new(), it is not attached to the qdev
Peter Xuc8d3ff32017-07-05 16:21:23 +08004325 * device tree, and it is never realized.
4326 *
4327 * TODO: Make this TYPE_OBJECT once QOM provides something like
4328 * TYPE_DEVICE's "-global" properties.
Peter Xu01f6e142017-06-28 15:15:44 +08004329 */
Peter Xue5cb7e72017-06-27 12:10:13 +08004330 .parent = TYPE_DEVICE,
4331 .class_init = migration_class_init,
4332 .class_size = sizeof(MigrationClass),
4333 .instance_size = sizeof(MigrationState),
4334 .instance_init = migration_instance_init,
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01004335 .instance_finalize = migration_instance_finalize,
Peter Xue5cb7e72017-06-27 12:10:13 +08004336};
4337
4338static void register_migration_types(void)
4339{
4340 type_register_static(&migration_type);
4341}
4342
4343type_init(register_migration_types);