blob: d9e94f4080076eee5d57b227abc97798665ffa1c [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"
Peter Xu9d18af92017-06-27 12:10:19 +080055#include "monitor/monitor.h"
Dr. David Alan Gilbert50510ea2019-02-27 13:24:05 +000056#include "net/announce.h"
Jens Freimannc7e0acd2019-10-29 12:49:02 +010057#include "qemu/queue.h"
Juan Quintelad32ca5a2020-01-22 16:16:07 +010058#include "multifd.h"
aliguori065e2812008-11-11 16:46:33 +000059
Kirti Wankhede37105862020-10-26 15:06:27 +053060#ifdef CONFIG_VFIO
61#include "hw/vfio/vfio-common.h"
62#endif
63
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
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000121static bool deferred_incoming;
122
Juan Quintelada6f1792017-04-24 17:37:14 +0200123/* Messages sent on the return path from destination to source */
124enum mig_rp_message_type {
125 MIG_RP_MSG_INVALID = 0, /* Must be 0 */
126 MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */
127 MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */
128
129 MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */
130 MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */
Peter Xua335deb2018-05-02 18:47:28 +0800131 MIG_RP_MSG_RECV_BITMAP, /* send recved_bitmap back to source */
Peter Xu13955b82018-05-02 18:47:30 +0800132 MIG_RP_MSG_RESUME_ACK, /* tell source that we are ready to resume */
Juan Quintelada6f1792017-04-24 17:37:14 +0200133
134 MIG_RP_MSG_MAX
135};
136
Juan Quintela17549e82011-10-05 13:50:43 +0200137/* When we add fault tolerance, we could have several
138 migrations at once. For now we don't need to add
139 dynamic creation of migration */
140
Peter Xue5cb7e72017-06-27 12:10:13 +0800141static MigrationState *current_migration;
Peter Xue1b1b1b2018-05-02 18:47:35 +0800142static MigrationIncomingState *current_incoming;
Peter Xue5cb7e72017-06-27 12:10:13 +0800143
Peter Xu8b0b29d2017-07-18 11:39:06 +0800144static bool migration_object_check(MigrationState *ms, Error **errp);
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +0100145static int migration_maybe_pause(MigrationState *s,
146 int *current_active_state,
147 int new_state);
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +0000148static void migrate_fd_cancel(MigrationState *s);
Peter Xu8b0b29d2017-07-18 11:39:06 +0800149
Peter Xu8f8bfff2020-10-21 17:27:18 -0400150static gint page_request_addr_cmp(gconstpointer ap, gconstpointer bp)
151{
152 uintptr_t a = (uintptr_t) ap, b = (uintptr_t) bp;
153
154 return (a > b) - (a < b);
155}
156
Peter Xue5cb7e72017-06-27 12:10:13 +0800157void migration_object_init(void)
158{
Peter Xu8b0b29d2017-07-18 11:39:06 +0800159 Error *err = NULL;
Peter Xu4ffdb332017-06-27 12:10:18 +0800160
Peter Xue5cb7e72017-06-27 12:10:13 +0800161 /* This can only be called once. */
162 assert(!current_migration);
163 current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION));
Peter Xu4ffdb332017-06-27 12:10:18 +0800164
Peter Xue1b1b1b2018-05-02 18:47:35 +0800165 /*
166 * Init the migrate incoming object as well no matter whether
167 * we'll use it or not.
168 */
169 assert(!current_incoming);
170 current_incoming = g_new0(MigrationIncomingState, 1);
171 current_incoming->state = MIGRATION_STATUS_NONE;
172 current_incoming->postcopy_remote_fds =
173 g_array_new(FALSE, TRUE, sizeof(struct PostCopyFD));
174 qemu_mutex_init(&current_incoming->rp_mutex);
175 qemu_event_init(&current_incoming->main_thread_load_event, false);
176 qemu_sem_init(&current_incoming->postcopy_pause_sem_dst, 0);
177 qemu_sem_init(&current_incoming->postcopy_pause_sem_fault, 0);
Peter Xu8f8bfff2020-10-21 17:27:18 -0400178 qemu_mutex_init(&current_incoming->page_request_mutex);
179 current_incoming->page_requested = g_tree_new(page_request_addr_cmp);
Peter Xue1b1b1b2018-05-02 18:47:35 +0800180
Peter Xu8b0b29d2017-07-18 11:39:06 +0800181 if (!migration_object_check(current_migration, &err)) {
182 error_report_err(err);
183 exit(1);
184 }
Paolo Bonzinie0d17df2020-10-21 05:25:58 -0400185
186 blk_mig_init();
187 ram_mig_init();
188 dirty_bitmap_mig_init();
Peter Xue5cb7e72017-06-27 12:10:13 +0800189}
190
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +0000191void migration_shutdown(void)
Vladimir Sementsov-Ogievskiy1f895602017-12-28 12:16:16 +0300192{
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +0000193 /*
194 * Cancel the current migration - that will (eventually)
195 * stop the migration using this structure
196 */
197 migrate_fd_cancel(current_migration);
Vladimir Sementsov-Ogievskiy1f895602017-12-28 12:16:16 +0300198 object_unref(OBJECT(current_migration));
Vladimir Sementsov-Ogievskiy1499ab02020-07-27 22:42:31 +0300199
200 /*
201 * Cancel outgoing migration of dirty bitmaps. It should
202 * at least unref used block nodes.
203 */
204 dirty_bitmap_mig_cancel_outgoing();
205
206 /*
207 * Cancel incoming migration of dirty bitmaps. Dirty bitmaps
208 * are non-critical data, and their loss never considered as
209 * something serious.
210 */
211 dirty_bitmap_mig_cancel_incoming();
Vladimir Sementsov-Ogievskiy1f895602017-12-28 12:16:16 +0300212}
213
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100214/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +0200215MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +0200216{
Peter Xue5cb7e72017-06-27 12:10:13 +0800217 /* This can only be called after the object created. */
218 assert(current_migration);
219 return current_migration;
Juan Quintela17549e82011-10-05 13:50:43 +0200220}
221
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100222MigrationIncomingState *migration_incoming_get_current(void)
223{
Peter Xue1b1b1b2018-05-02 18:47:35 +0800224 assert(current_incoming);
225 return current_incoming;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100226}
227
228void migration_incoming_state_destroy(void)
229{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100230 struct MigrationIncomingState *mis = migration_incoming_get_current();
231
Peter Xu34826552017-05-19 14:43:29 +0800232 if (mis->to_src_file) {
Peter Xu660819b2017-05-19 14:43:30 +0800233 /* Tell source that we are done */
234 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
Peter Xu34826552017-05-19 14:43:29 +0800235 qemu_fclose(mis->to_src_file);
236 mis->to_src_file = NULL;
237 }
238
Peter Xu660819b2017-05-19 14:43:30 +0800239 if (mis->from_src_file) {
240 qemu_fclose(mis->from_src_file);
241 mis->from_src_file = NULL;
242 }
Dr. David Alan Gilbert00fa4fc2018-03-12 17:21:04 +0000243 if (mis->postcopy_remote_fds) {
244 g_array_free(mis->postcopy_remote_fds, TRUE);
245 mis->postcopy_remote_fds = NULL;
246 }
Peter Xu660819b2017-05-19 14:43:30 +0800247
Dr. David Alan Gilbert1783c002020-09-28 15:48:30 +0100248 qemu_event_reset(&mis->main_thread_load_event);
249
Peter Xu8f8bfff2020-10-21 17:27:18 -0400250 if (mis->page_requested) {
251 g_tree_destroy(mis->page_requested);
252 mis->page_requested = NULL;
253 }
254
Juan Quintela9aca82b2019-02-27 11:51:27 +0100255 if (mis->socket_address_list) {
256 qapi_free_SocketAddressList(mis->socket_address_list);
257 mis->socket_address_list = NULL;
258 }
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100259}
260
Juan Quintelab05dc722015-07-07 14:44:05 +0200261static void migrate_generate_event(int new_state)
262{
263 if (migrate_use_events()) {
Peter Xu3ab72382018-08-15 21:37:37 +0800264 qapi_event_send_migration(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200265 }
266}
267
Dr. David Alan Gilbert0f073f42018-04-16 18:09:30 +0100268static bool migrate_late_block_activate(void)
269{
270 MigrationState *s;
271
272 s = migrate_get_current();
273
274 return s->enabled_capabilities[
275 MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE];
276}
277
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000278/*
279 * Called on -incoming with a defer: uri.
280 * The migration can be started later after any parameters have been
281 * changed.
282 */
283static void deferred_incoming_migration(Error **errp)
284{
285 if (deferred_incoming) {
286 error_setg(errp, "Incoming migration already deferred");
287 }
288 deferred_incoming = true;
289}
290
Juan Quintelada6f1792017-04-24 17:37:14 +0200291/*
292 * Send a message on the return channel back to the source
293 * of the migration.
294 */
Peter Xud6208e32018-02-08 18:31:12 +0800295static int migrate_send_rp_message(MigrationIncomingState *mis,
296 enum mig_rp_message_type message_type,
297 uint16_t len, void *data)
Juan Quintelada6f1792017-04-24 17:37:14 +0200298{
Peter Xud6208e32018-02-08 18:31:12 +0800299 int ret = 0;
300
Juan Quintelada6f1792017-04-24 17:37:14 +0200301 trace_migrate_send_rp_message((int)message_type, len);
302 qemu_mutex_lock(&mis->rp_mutex);
Peter Xud6208e32018-02-08 18:31:12 +0800303
304 /*
305 * It's possible that the file handle got lost due to network
306 * failures.
307 */
308 if (!mis->to_src_file) {
309 ret = -EIO;
310 goto error;
311 }
312
Juan Quintelada6f1792017-04-24 17:37:14 +0200313 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
314 qemu_put_be16(mis->to_src_file, len);
315 qemu_put_buffer(mis->to_src_file, data, len);
316 qemu_fflush(mis->to_src_file);
Peter Xud6208e32018-02-08 18:31:12 +0800317
318 /* It's possible that qemu file got error during sending */
319 ret = qemu_file_get_error(mis->to_src_file);
320
321error:
Juan Quintelada6f1792017-04-24 17:37:14 +0200322 qemu_mutex_unlock(&mis->rp_mutex);
Peter Xud6208e32018-02-08 18:31:12 +0800323 return ret;
Juan Quintelada6f1792017-04-24 17:37:14 +0200324}
325
Peter Xu2e2bce12020-09-08 16:30:18 -0400326/* Request one page from the source VM at the given start address.
327 * rb: the RAMBlock to request the page in
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000328 * Start: Address offset within the RB
329 * Len: Length in bytes required - must be a multiple of pagesize
330 */
Peter Xu7a267fc2020-10-21 17:27:17 -0400331int migrate_send_rp_message_req_pages(MigrationIncomingState *mis,
332 RAMBlock *rb, ram_addr_t start)
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000333{
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100334 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000335 size_t msglen = 12; /* start + len */
Peter Xu2e2bce12020-09-08 16:30:18 -0400336 size_t len = qemu_ram_pagesize(rb);
Peter Xud6208e32018-02-08 18:31:12 +0800337 enum mig_rp_message_type msg_type;
Peter Xu2e2bce12020-09-08 16:30:18 -0400338 const char *rbname;
339 int rbname_len;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000340
341 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
342 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
343
Peter Xu2e2bce12020-09-08 16:30:18 -0400344 /*
345 * We maintain the last ramblock that we requested for page. Note that we
346 * don't need locking because this function will only be called within the
347 * postcopy ram fault thread.
348 */
349 if (rb != mis->last_rb) {
350 mis->last_rb = rb;
351
352 rbname = qemu_ram_get_idstr(rb);
353 rbname_len = strlen(rbname);
354
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000355 assert(rbname_len < 256);
356
357 bufc[msglen++] = rbname_len;
358 memcpy(bufc + msglen, rbname, rbname_len);
359 msglen += rbname_len;
Peter Xud6208e32018-02-08 18:31:12 +0800360 msg_type = MIG_RP_MSG_REQ_PAGES_ID;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000361 } else {
Peter Xud6208e32018-02-08 18:31:12 +0800362 msg_type = MIG_RP_MSG_REQ_PAGES;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000363 }
Peter Xud6208e32018-02-08 18:31:12 +0800364
365 return migrate_send_rp_message(mis, msg_type, msglen, bufc);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000366}
367
Peter Xu7a267fc2020-10-21 17:27:17 -0400368int migrate_send_rp_req_pages(MigrationIncomingState *mis,
Peter Xu8f8bfff2020-10-21 17:27:18 -0400369 RAMBlock *rb, ram_addr_t start, uint64_t haddr)
Peter Xu7a267fc2020-10-21 17:27:17 -0400370{
Peter Xu8f8bfff2020-10-21 17:27:18 -0400371 void *aligned = (void *)(uintptr_t)(haddr & (-qemu_ram_pagesize(rb)));
Chen Quna2429282020-11-11 22:22:03 +0800372 bool received = false;
Peter Xu8f8bfff2020-10-21 17:27:18 -0400373
374 WITH_QEMU_LOCK_GUARD(&mis->page_request_mutex) {
375 received = ramblock_recv_bitmap_test_byte_offset(rb, start);
376 if (!received && !g_tree_lookup(mis->page_requested, aligned)) {
377 /*
378 * The page has not been received, and it's not yet in the page
379 * request list. Queue it. Set the value of element to 1, so that
380 * things like g_tree_lookup() will return TRUE (1) when found.
381 */
382 g_tree_insert(mis->page_requested, aligned, (gpointer)1);
383 mis->page_requested_count++;
384 trace_postcopy_page_req_add(aligned, mis->page_requested_count);
385 }
386 }
387
388 /*
389 * If the page is there, skip sending the message. We don't even need the
390 * lock because as long as the page arrived, it'll be there forever.
391 */
392 if (received) {
393 return 0;
394 }
395
Peter Xu7a267fc2020-10-21 17:27:17 -0400396 return migrate_send_rp_message_req_pages(mis, rb, start);
397}
398
Zhang Chenaad555c2018-09-03 12:38:47 +0800399static bool migration_colo_enabled;
400bool migration_incoming_colo_enabled(void)
401{
402 return migration_colo_enabled;
403}
404
405void migration_incoming_disable_colo(void)
406{
David Hildenbrand18b1d3c2020-06-26 09:22:36 +0200407 ram_block_discard_disable(false);
Zhang Chenaad555c2018-09-03 12:38:47 +0800408 migration_colo_enabled = false;
409}
410
David Hildenbrand18b1d3c2020-06-26 09:22:36 +0200411int migration_incoming_enable_colo(void)
Zhang Chenaad555c2018-09-03 12:38:47 +0800412{
David Hildenbrand18b1d3c2020-06-26 09:22:36 +0200413 if (ram_block_discard_disable(true)) {
414 error_report("COLO: cannot disable RAM discard");
415 return -EBUSY;
416 }
Zhang Chenaad555c2018-09-03 12:38:47 +0800417 migration_colo_enabled = true;
David Hildenbrand18b1d3c2020-06-26 09:22:36 +0200418 return 0;
Zhang Chenaad555c2018-09-03 12:38:47 +0800419}
420
Juan Quintela9aca82b2019-02-27 11:51:27 +0100421void migrate_add_address(SocketAddress *address)
422{
423 MigrationIncomingState *mis = migration_incoming_get_current();
424 SocketAddressList *addrs;
425
426 addrs = g_new0(SocketAddressList, 1);
427 addrs->next = mis->socket_address_list;
428 mis->socket_address_list = addrs;
429 addrs->value = QAPI_CLONE(SocketAddress, address);
430}
431
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200432void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000433{
Longpeng(Mike)d658f652020-08-06 15:40:29 +0800434 const char *p = NULL;
aliguori34c9dd82008-10-13 03:14:31 +0000435
Peter Xu3ab72382018-08-15 21:37:37 +0800436 qapi_event_send_migration(MIGRATION_STATUS_SETUP);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000437 if (!strcmp(uri, "defer")) {
438 deferred_incoming_migration(errp);
Longpeng(Mike)d658f652020-08-06 15:40:29 +0800439 } else if (strstart(uri, "tcp:", &p) ||
Longpeng(Mike)9ba3b2b2020-08-06 15:40:30 +0800440 strstart(uri, "unix:", NULL) ||
441 strstart(uri, "vsock:", NULL)) {
Longpeng(Mike)d658f652020-08-06 15:40:29 +0800442 socket_start_incoming_migration(p ? p : uri, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400443#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000444 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400445 rdma_start_incoming_migration(p, errp);
446#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000447 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200448 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000449 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200450 fd_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000451 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100452 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200453 }
aliguori5bb79102008-10-13 03:12:02 +0000454}
455
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300456static void process_incoming_migration_bh(void *opaque)
457{
458 Error *local_err = NULL;
459 MigrationIncomingState *mis = opaque;
460
Dr. David Alan Gilbert0f073f42018-04-16 18:09:30 +0100461 /* If capability late_block_activate is set:
462 * Only fire up the block code now if we're going to restart the
463 * VM, else 'cont' will do it.
464 * This causes file locking to happen; so we don't want it to happen
465 * unless we really are starting the VM.
466 */
467 if (!migrate_late_block_activate() ||
468 (autostart && (!global_state_received() ||
469 global_state_get_runstate() == RUN_STATE_RUNNING))) {
470 /* Make sure all file formats flush their mutable metadata.
471 * If we get an error here, just don't restart the VM yet. */
472 bdrv_invalidate_cache_all(&local_err);
473 if (local_err) {
474 error_report_err(local_err);
475 local_err = NULL;
476 autostart = false;
477 }
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200478 }
479
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300480 /*
481 * This must happen after all error conditions are dealt with and
482 * we're sure the VM is going to be running on this host.
483 */
Dr. David Alan Gilbert76595052019-02-27 13:24:08 +0000484 qemu_announce_self(&mis->announce_timer, migrate_announce_params());
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300485
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100486 if (multifd_load_cleanup(&local_err) != 0) {
487 error_report_err(local_err);
488 autostart = false;
489 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300490 /* If global state section was not received or we are in running
491 state, we need to obey autostart. Any other state is set with
492 runstate_set. */
493
Vladimir Sementsov-Ogievskiyb35ebdf2018-03-13 15:34:01 -0400494 dirty_bitmap_mig_before_vm_start();
495
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300496 if (!global_state_received() ||
497 global_state_get_runstate() == RUN_STATE_RUNNING) {
498 if (autostart) {
499 vm_start();
500 } else {
501 runstate_set(RUN_STATE_PAUSED);
502 }
Zhang Chendb009722019-03-03 22:50:17 +0800503 } else if (migration_incoming_colo_enabled()) {
504 migration_incoming_disable_colo();
505 vm_start();
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300506 } else {
507 runstate_set(global_state_get_runstate());
508 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300509 /*
510 * This must happen after any state changes since as soon as an external
511 * observer sees this event they might start to prod at the VM assuming
512 * it's ready to use.
513 */
514 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
515 MIGRATION_STATUS_COMPLETED);
516 qemu_bh_delete(mis->bh);
517 migration_incoming_state_destroy();
518}
519
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200520static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200521{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100522 MigrationIncomingState *mis = migration_incoming_get_current();
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000523 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200524 int ret;
Zhang Chen8e48ac92018-09-03 12:38:46 +0800525 Error *local_err = NULL;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200526
Juan Quintela4f0fae72017-07-24 12:42:02 +0200527 assert(mis->from_src_file);
Lidong Chen92370982018-08-06 21:29:35 +0800528 mis->migration_incoming_co = qemu_coroutine_self();
Dr. David Alan Gilbert67f11b52017-02-24 18:28:34 +0000529 mis->largest_page_size = qemu_ram_pagesize_largest();
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000530 postcopy_state_set(POSTCOPY_INCOMING_NONE);
zhanghailiang93d7af62015-12-16 11:47:34 +0000531 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
532 MIGRATION_STATUS_ACTIVE);
Juan Quintela4f0fae72017-07-24 12:42:02 +0200533 ret = qemu_loadvm_state(mis->from_src_file);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100534
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000535 ps = postcopy_state_get();
536 trace_process_incoming_migration_co_end(ret, ps);
537 if (ps != POSTCOPY_INCOMING_NONE) {
538 if (ps == POSTCOPY_INCOMING_ADVISE) {
539 /*
540 * Where a migration had postcopy enabled (and thus went to advise)
541 * but managed to complete within the precopy period, we can use
542 * the normal exit.
543 */
544 postcopy_ram_incoming_cleanup(mis);
545 } else if (ret >= 0) {
546 /*
547 * Postcopy was started, cleanup should happen at the end of the
548 * postcopy thread.
549 */
550 trace_process_incoming_migration_co_postcopy_end_main();
551 return;
552 }
553 /* Else if something went wrong then just fall out of the normal exit */
554 }
555
zhanghailiang25d0c162016-10-27 14:42:55 +0800556 /* we get COLO info, and know if we are in COLO mode */
Zhang Chenaad555c2018-09-03 12:38:47 +0800557 if (!ret && migration_incoming_colo_enabled()) {
Zhang Chen8e48ac92018-09-03 12:38:46 +0800558 /* Make sure all file formats flush their mutable metadata */
559 bdrv_invalidate_cache_all(&local_err);
560 if (local_err) {
Zhang Chen8e48ac92018-09-03 12:38:46 +0800561 error_report_err(local_err);
Fei Li6d99c2d2019-01-13 22:08:49 +0800562 goto fail;
Zhang Chen8e48ac92018-09-03 12:38:46 +0800563 }
564
zhanghailiang25d0c162016-10-27 14:42:55 +0800565 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
566 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
567 mis->have_colo_incoming_thread = true;
568 qemu_coroutine_yield();
569
570 /* Wait checkpoint incoming thread exit before free resource */
571 qemu_thread_join(&mis->colo_incoming_thread);
Zhang Chen13af18f2018-09-03 12:38:48 +0800572 /* We hold the global iothread lock, so it is safe here */
573 colo_release_ram_cache();
zhanghailiang25d0c162016-10-27 14:42:55 +0800574 }
575
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200576 if (ret < 0) {
Peter Lievendb80fac2014-06-10 11:29:16 +0200577 error_report("load of migration failed: %s", strerror(-ret));
Fei Li6d99c2d2019-01-13 22:08:49 +0800578 goto fail;
Juan Quintela511c0232010-06-09 14:10:55 +0200579 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300580 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
581 qemu_bh_schedule(mis->bh);
Lidong Chen92370982018-08-06 21:29:35 +0800582 mis->migration_incoming_co = NULL;
Fei Li6d99c2d2019-01-13 22:08:49 +0800583 return;
584fail:
585 local_err = NULL;
586 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
587 MIGRATION_STATUS_FAILED);
588 qemu_fclose(mis->from_src_file);
589 if (multifd_load_cleanup(&local_err) != 0) {
590 error_report_err(local_err);
591 }
592 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200593}
594
Juan Quintelab673eab2019-06-12 11:44:19 +0200595/**
596 * @migration_incoming_setup: Setup incoming migration
597 *
598 * Returns 0 for no error or 1 for error
599 *
600 * @f: file for main migration channel
601 * @errp: where to put errors
602 */
603static int migration_incoming_setup(QEMUFile *f, Error **errp)
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200604{
Juan Quintela4f0fae72017-07-24 12:42:02 +0200605 MigrationIncomingState *mis = migration_incoming_get_current();
Juan Quintelab673eab2019-06-12 11:44:19 +0200606 Error *local_err = NULL;
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200607
Juan Quintelab673eab2019-06-12 11:44:19 +0200608 if (multifd_load_setup(&local_err) != 0) {
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100609 /* We haven't been able to create multifd threads
610 nothing better to do */
Juan Quintelab673eab2019-06-12 11:44:19 +0200611 error_report_err(local_err);
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100612 exit(EXIT_FAILURE);
613 }
614
Juan Quintela4f0fae72017-07-24 12:42:02 +0200615 if (!mis->from_src_file) {
616 mis->from_src_file = f;
617 }
Daniel P. Berrange06ad5132016-04-27 11:04:56 +0100618 qemu_file_set_blocking(f, false);
Juan Quintelab673eab2019-06-12 11:44:19 +0200619 return 0;
Juan Quintelae595a012017-07-17 12:30:25 +0200620}
621
Juan Quintela36c2f8b2018-03-07 08:40:52 +0100622void migration_incoming_process(void)
Juan Quintelae595a012017-07-17 12:30:25 +0200623{
624 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL);
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200625 qemu_coroutine_enter(co);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200626}
627
Peter Xu884835f2018-06-27 21:22:45 +0800628/* Returns true if recovered from a paused migration, otherwise false */
629static bool postcopy_try_recover(QEMUFile *f)
Juan Quintelae595a012017-07-17 12:30:25 +0200630{
Peter Xud96c9e82018-05-02 18:47:26 +0800631 MigrationIncomingState *mis = migration_incoming_get_current();
632
633 if (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
634 /* Resumed from a paused postcopy migration */
635
636 mis->from_src_file = f;
637 /* Postcopy has standalone thread to do vm load */
638 qemu_file_set_blocking(f, true);
639
640 /* Re-configure the return path */
641 mis->to_src_file = qemu_file_get_return_path(f);
642
643 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
644 MIGRATION_STATUS_POSTCOPY_RECOVER);
645
646 /*
647 * Here, we only wake up the main loading thread (while the
648 * fault thread will still be waiting), so that we can receive
649 * commands from source now, and answer it if needed. The
650 * fault thread will be woken up afterwards until we are sure
651 * that source is ready to reply to page requests.
652 */
653 qemu_sem_post(&mis->postcopy_pause_sem_dst);
Peter Xu884835f2018-06-27 21:22:45 +0800654 return true;
Peter Xud96c9e82018-05-02 18:47:26 +0800655 }
Peter Xu884835f2018-06-27 21:22:45 +0800656
657 return false;
658}
659
Juan Quintelab673eab2019-06-12 11:44:19 +0200660void migration_fd_process_incoming(QEMUFile *f, Error **errp)
Peter Xu884835f2018-06-27 21:22:45 +0800661{
Juan Quintelab673eab2019-06-12 11:44:19 +0200662 Error *local_err = NULL;
663
Peter Xu884835f2018-06-27 21:22:45 +0800664 if (postcopy_try_recover(f)) {
665 return;
666 }
667
Juan Quintelab673eab2019-06-12 11:44:19 +0200668 if (migration_incoming_setup(f, &local_err)) {
Markus Armbruster2155cea2020-07-22 10:40:47 +0200669 error_propagate(errp, local_err);
Juan Quintelab673eab2019-06-12 11:44:19 +0200670 return;
671 }
Peter Xu884835f2018-06-27 21:22:45 +0800672 migration_incoming_process();
Juan Quintelae595a012017-07-17 12:30:25 +0200673}
674
Fei Li49ed0d22019-01-13 22:08:46 +0800675void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp)
Juan Quintela4f0fae72017-07-24 12:42:02 +0200676{
677 MigrationIncomingState *mis = migration_incoming_get_current();
Juan Quintelab673eab2019-06-12 11:44:19 +0200678 Error *local_err = NULL;
Peter Xua429e7f2018-06-27 21:22:46 +0800679 bool start_migration;
Juan Quintela4f0fae72017-07-24 12:42:02 +0200680
681 if (!mis->from_src_file) {
Peter Xua429e7f2018-06-27 21:22:46 +0800682 /* The first connection (multifd may have multiple) */
Juan Quintela4f0fae72017-07-24 12:42:02 +0200683 QEMUFile *f = qemu_fopen_channel_input(ioc);
Peter Xua429e7f2018-06-27 21:22:46 +0800684
685 /* If it's a recovery, we're done */
Peter Xu884835f2018-06-27 21:22:45 +0800686 if (postcopy_try_recover(f)) {
687 return;
688 }
Peter Xua429e7f2018-06-27 21:22:46 +0800689
Juan Quintelab673eab2019-06-12 11:44:19 +0200690 if (migration_incoming_setup(f, &local_err)) {
Markus Armbruster2155cea2020-07-22 10:40:47 +0200691 error_propagate(errp, local_err);
Juan Quintelab673eab2019-06-12 11:44:19 +0200692 return;
693 }
Peter Xua429e7f2018-06-27 21:22:46 +0800694
695 /*
696 * Common migration only needs one channel, so we can start
697 * right now. Multifd needs more than one channel, we wait.
698 */
699 start_migration = !migrate_use_multifd();
700 } else {
701 /* Multiple connections */
702 assert(migrate_use_multifd());
Fei Li49ed0d22019-01-13 22:08:46 +0800703 start_migration = multifd_recv_new_channel(ioc, &local_err);
704 if (local_err) {
705 error_propagate(errp, local_err);
706 return;
707 }
Juan Quintela4f0fae72017-07-24 12:42:02 +0200708 }
Peter Xu81e62052018-06-27 21:22:44 +0800709
Peter Xua429e7f2018-06-27 21:22:46 +0800710 if (start_migration) {
Peter Xu81e62052018-06-27 21:22:44 +0800711 migration_incoming_process();
712 }
Juan Quintela4f0fae72017-07-24 12:42:02 +0200713}
714
Juan Quintela428d8902017-07-24 13:06:25 +0200715/**
716 * @migration_has_all_channels: We have received all channels that we need
717 *
718 * Returns true when we have got connections to all the channels that
719 * we need for migration.
720 */
721bool migration_has_all_channels(void)
722{
Daniel P. Berrangéca273df2018-06-19 17:35:52 +0100723 MigrationIncomingState *mis = migration_incoming_get_current();
Juan Quintela62c1e0c2018-02-19 18:59:02 +0100724 bool all_channels;
725
726 all_channels = multifd_recv_all_channels_created();
727
Daniel P. Berrangéca273df2018-06-19 17:35:52 +0100728 return all_channels && mis->from_src_file != NULL;
Juan Quintela428d8902017-07-24 13:06:25 +0200729}
730
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000731/*
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000732 * Send a 'SHUT' message on the return channel with the given value
733 * to indicate that we've finished with the RP. Non-0 value indicates
734 * error.
735 */
736void migrate_send_rp_shut(MigrationIncomingState *mis,
737 uint32_t value)
738{
739 uint32_t buf;
740
741 buf = cpu_to_be32(value);
742 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
743}
744
745/*
746 * Send a 'PONG' message on the return channel with the given value
747 * (normally in response to a 'PING')
748 */
749void migrate_send_rp_pong(MigrationIncomingState *mis,
750 uint32_t value)
751{
752 uint32_t buf;
753
754 buf = cpu_to_be32(value);
755 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
756}
757
Peter Xua335deb2018-05-02 18:47:28 +0800758void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
759 char *block_name)
760{
761 char buf[512];
762 int len;
763 int64_t res;
764
765 /*
766 * First, we send the header part. It contains only the len of
767 * idstr, and the idstr itself.
768 */
769 len = strlen(block_name);
770 buf[0] = len;
771 memcpy(buf + 1, block_name, len);
772
773 if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
774 error_report("%s: MSG_RP_RECV_BITMAP only used for recovery",
775 __func__);
776 return;
777 }
778
779 migrate_send_rp_message(mis, MIG_RP_MSG_RECV_BITMAP, len + 1, buf);
780
781 /*
782 * Next, we dump the received bitmap to the stream.
783 *
784 * TODO: currently we are safe since we are the only one that is
785 * using the to_src_file handle (fault thread is still paused),
786 * and it's ok even not taking the mutex. However the best way is
787 * to take the lock before sending the message header, and release
788 * the lock after sending the bitmap.
789 */
790 qemu_mutex_lock(&mis->rp_mutex);
791 res = ramblock_recv_bitmap_send(mis->to_src_file, block_name);
792 qemu_mutex_unlock(&mis->rp_mutex);
793
794 trace_migrate_send_rp_recv_bitmap(block_name, res);
795}
796
Peter Xu13955b82018-05-02 18:47:30 +0800797void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value)
798{
799 uint32_t buf;
800
801 buf = cpu_to_be32(value);
802 migrate_send_rp_message(mis, MIG_RP_MSG_RESUME_ACK, sizeof(buf), &buf);
803}
804
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300805MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
806{
807 MigrationCapabilityStatusList *head = NULL;
808 MigrationCapabilityStatusList *caps;
809 MigrationState *s = migrate_get_current();
810 int i;
811
Michael Tokarev387eede2013-10-05 13:18:28 +0400812 caps = NULL; /* silence compiler warning */
Eric Blake7fb1cf12015-11-18 01:52:57 -0700813 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Dr. David Alan Gilberted1701c2017-05-15 15:05:29 +0100814#ifndef CONFIG_LIVE_BLOCK_MIGRATION
815 if (i == MIGRATION_CAPABILITY_BLOCK) {
816 continue;
817 }
818#endif
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300819 if (head == NULL) {
820 head = g_malloc0(sizeof(*caps));
821 caps = head;
822 } else {
823 caps->next = g_malloc0(sizeof(*caps));
824 caps = caps->next;
825 }
826 caps->value =
827 g_malloc(sizeof(*caps->value));
828 caps->value->capability = i;
829 caps->value->state = s->enabled_capabilities[i];
830 }
831
832 return head;
833}
834
Liang Li85de8322015-03-23 16:32:28 +0800835MigrationParameters *qmp_query_migrate_parameters(Error **errp)
836{
837 MigrationParameters *params;
838 MigrationState *s = migrate_get_current();
839
Markus Armbrustere87fae42017-07-18 12:57:38 +0200840 /* TODO use QAPI_CLONE() instead of duplicating it inline */
Liang Li85de8322015-03-23 16:32:28 +0800841 params = g_malloc0(sizeof(*params));
Eric Blakede63ab62016-09-08 22:14:15 -0500842 params->has_compress_level = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100843 params->compress_level = s->parameters.compress_level;
Eric Blakede63ab62016-09-08 22:14:15 -0500844 params->has_compress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100845 params->compress_threads = s->parameters.compress_threads;
Xiao Guangrong1d588722018-08-21 16:10:20 +0800846 params->has_compress_wait_thread = true;
847 params->compress_wait_thread = s->parameters.compress_wait_thread;
Eric Blakede63ab62016-09-08 22:14:15 -0500848 params->has_decompress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100849 params->decompress_threads = s->parameters.decompress_threads;
Keqian Zhudc14a472020-02-24 10:31:42 +0800850 params->has_throttle_trigger_threshold = true;
851 params->throttle_trigger_threshold = s->parameters.throttle_trigger_threshold;
Eric Blakede63ab62016-09-08 22:14:15 -0500852 params->has_cpu_throttle_initial = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100853 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
Eric Blakede63ab62016-09-08 22:14:15 -0500854 params->has_cpu_throttle_increment = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100855 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
Keqian Zhucbbf8182020-04-13 18:15:08 +0800856 params->has_cpu_throttle_tailslow = true;
857 params->cpu_throttle_tailslow = s->parameters.cpu_throttle_tailslow;
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200858 params->has_tls_creds = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100859 params->tls_creds = g_strdup(s->parameters.tls_creds);
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200860 params->has_tls_hostname = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100861 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
Daniel P. Berranged2f1d292019-02-27 14:53:24 +0000862 params->has_tls_authz = true;
Mao Zhongyi7cd75cb2020-03-25 09:49:30 +0800863 params->tls_authz = g_strdup(s->parameters.tls_authz ?
864 s->parameters.tls_authz : "");
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530865 params->has_max_bandwidth = true;
866 params->max_bandwidth = s->parameters.max_bandwidth;
867 params->has_downtime_limit = true;
868 params->downtime_limit = s->parameters.downtime_limit;
zhanghailiangfe39a4d2016-11-02 15:42:09 +0800869 params->has_x_checkpoint_delay = true;
zhanghailiang68b53592016-10-27 14:43:01 +0800870 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
Juan Quintela2833c592017-04-05 18:32:37 +0200871 params->has_block_incremental = true;
872 params->block_incremental = s->parameters.block_incremental;
Juan Quintelacbfd6c92019-02-06 13:54:06 +0100873 params->has_multifd_channels = true;
874 params->multifd_channels = s->parameters.multifd_channels;
Juan Quintela96eef042019-01-16 10:35:55 +0100875 params->has_multifd_compression = true;
876 params->multifd_compression = s->parameters.multifd_compression;
Juan Quintela9004db42020-01-23 17:08:52 +0100877 params->has_multifd_zlib_level = true;
878 params->multifd_zlib_level = s->parameters.multifd_zlib_level;
Juan Quintela6a9ad152020-01-23 17:41:36 +0100879 params->has_multifd_zstd_level = true;
880 params->multifd_zstd_level = s->parameters.multifd_zstd_level;
Juan Quintela73af8dd2017-10-05 21:30:10 +0200881 params->has_xbzrle_cache_size = true;
882 params->xbzrle_cache_size = s->parameters.xbzrle_cache_size;
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +0100883 params->has_max_postcopy_bandwidth = true;
884 params->max_postcopy_bandwidth = s->parameters.max_postcopy_bandwidth;
Li Qiang4cbc9c72018-08-01 06:00:20 -0700885 params->has_max_cpu_throttle = true;
886 params->max_cpu_throttle = s->parameters.max_cpu_throttle;
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +0000887 params->has_announce_initial = true;
888 params->announce_initial = s->parameters.announce_initial;
889 params->has_announce_max = true;
890 params->announce_max = s->parameters.announce_max;
891 params->has_announce_rounds = true;
892 params->announce_rounds = s->parameters.announce_rounds;
893 params->has_announce_step = true;
894 params->announce_step = s->parameters.announce_step;
Liang Li85de8322015-03-23 16:32:28 +0800895
Max Reitz31e4c352020-08-20 17:07:23 +0200896 if (s->parameters.has_block_bitmap_mapping) {
897 params->has_block_bitmap_mapping = true;
898 params->block_bitmap_mapping =
899 QAPI_CLONE(BitmapMigrationNodeAliasList,
900 s->parameters.block_bitmap_mapping);
901 }
902
Liang Li85de8322015-03-23 16:32:28 +0800903 return params;
904}
905
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +0000906AnnounceParameters *migrate_announce_params(void)
907{
908 static AnnounceParameters ap;
909
910 MigrationState *s = migrate_get_current();
911
912 ap.initial = s->parameters.announce_initial;
913 ap.max = s->parameters.announce_max;
914 ap.rounds = s->parameters.announce_rounds;
915 ap.step = s->parameters.announce_step;
916
917 return &ap;
918}
919
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000920/*
921 * Return true if we're already in the middle of a migration
922 * (i.e. any of the active or setup states)
923 */
Jia Lina3d63da12018-10-26 16:36:20 +0800924bool migration_is_setup_or_active(int state)
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000925{
926 switch (state) {
927 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000928 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Peter Xua688d2c2018-05-02 18:47:18 +0800929 case MIGRATION_STATUS_POSTCOPY_PAUSED:
Peter Xu135b87b2018-05-02 18:47:25 +0800930 case MIGRATION_STATUS_POSTCOPY_RECOVER:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000931 case MIGRATION_STATUS_SETUP:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +0100932 case MIGRATION_STATUS_PRE_SWITCHOVER:
933 case MIGRATION_STATUS_DEVICE:
Jens Freimannc7e0acd2019-10-29 12:49:02 +0100934 case MIGRATION_STATUS_WAIT_UNPLUG:
zhanghailiang19dd4082020-02-24 14:54:12 +0800935 case MIGRATION_STATUS_COLO:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000936 return true;
937
938 default:
939 return false;
940
941 }
942}
943
Juan Quintela392d87e2020-01-21 15:39:23 +0100944bool migration_is_running(int state)
945{
946 switch (state) {
947 case MIGRATION_STATUS_ACTIVE:
948 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
949 case MIGRATION_STATUS_POSTCOPY_PAUSED:
950 case MIGRATION_STATUS_POSTCOPY_RECOVER:
951 case MIGRATION_STATUS_SETUP:
952 case MIGRATION_STATUS_PRE_SWITCHOVER:
953 case MIGRATION_STATUS_DEVICE:
954 case MIGRATION_STATUS_WAIT_UNPLUG:
955 case MIGRATION_STATUS_CANCELLING:
Juan Quintela392d87e2020-01-21 15:39:23 +0100956 return true;
957
958 default:
959 return false;
960
961 }
962}
963
Wei Yang640dfb12019-07-16 08:54:11 +0800964static void populate_time_info(MigrationInfo *info, MigrationState *s)
965{
966 info->has_status = true;
967 info->has_setup_time = true;
968 info->setup_time = s->setup_time;
969 if (s->state == MIGRATION_STATUS_COMPLETED) {
970 info->has_total_time = true;
971 info->total_time = s->total_time;
972 info->has_downtime = true;
973 info->downtime = s->downtime;
974 } else {
975 info->has_total_time = true;
976 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
977 s->start_time;
978 info->has_expected_downtime = true;
979 info->expected_downtime = s->expected_downtime;
980 }
981}
982
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100983static void populate_ram_info(MigrationInfo *info, MigrationState *s)
984{
985 info->has_ram = true;
986 info->ram = g_malloc0(sizeof(*info->ram));
Juan Quintela93604472017-06-06 19:49:03 +0200987 info->ram->transferred = ram_counters.transferred;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100988 info->ram->total = ram_bytes_total();
Juan Quintela93604472017-06-06 19:49:03 +0200989 info->ram->duplicate = ram_counters.duplicate;
Juan Quintelabedf53c2017-03-13 20:35:54 +0100990 /* legacy value. It is not used anymore */
991 info->ram->skipped = 0;
Juan Quintela93604472017-06-06 19:49:03 +0200992 info->ram->normal = ram_counters.normal;
993 info->ram->normal_bytes = ram_counters.normal *
Juan Quintela20afaed2017-03-21 09:09:14 +0100994 qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100995 info->ram->mbps = s->mbps;
Juan Quintela93604472017-06-06 19:49:03 +0200996 info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
997 info->ram->postcopy_requests = ram_counters.postcopy_requests;
Chao Fan030ce1f2017-03-21 10:22:43 +0800998 info->ram->page_size = qemu_target_page_size();
Juan Quintelaa61c45b2018-06-26 15:20:11 +0200999 info->ram->multifd_bytes = ram_counters.multifd_bytes;
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08001000 info->ram->pages_per_second = s->pages_per_second;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +01001001
Juan Quintela114f5ae2017-05-04 10:09:21 +02001002 if (migrate_use_xbzrle()) {
1003 info->has_xbzrle_cache = true;
1004 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
1005 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
Juan Quintela93604472017-06-06 19:49:03 +02001006 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
1007 info->xbzrle_cache->pages = xbzrle_counters.pages;
1008 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
1009 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
Wei Wange460a4b2020-04-30 08:59:35 +08001010 info->xbzrle_cache->encoding_rate = xbzrle_counters.encoding_rate;
Juan Quintela93604472017-06-06 19:49:03 +02001011 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
Juan Quintela114f5ae2017-05-04 10:09:21 +02001012 }
1013
Xiao Guangrong76e03002018-09-06 15:01:00 +08001014 if (migrate_use_compression()) {
1015 info->has_compression = true;
1016 info->compression = g_malloc0(sizeof(*info->compression));
1017 info->compression->pages = compression_counters.pages;
1018 info->compression->busy = compression_counters.busy;
1019 info->compression->busy_rate = compression_counters.busy_rate;
1020 info->compression->compressed_size =
1021 compression_counters.compressed_size;
1022 info->compression->compression_rate =
1023 compression_counters.compression_rate;
1024 }
1025
Juan Quintela338182c2017-05-03 13:16:38 +02001026 if (cpu_throttle_active()) {
1027 info->has_cpu_throttle_percentage = true;
1028 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
1029 }
1030
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +01001031 if (s->state != MIGRATION_STATUS_COMPLETED) {
1032 info->ram->remaining = ram_bytes_remaining();
Juan Quintela93604472017-06-06 19:49:03 +02001033 info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +01001034 }
1035}
1036
Juan Quintela930ac042017-05-04 10:21:46 +02001037static void populate_disk_info(MigrationInfo *info)
1038{
1039 if (blk_mig_active()) {
1040 info->has_disk = true;
1041 info->disk = g_malloc0(sizeof(*info->disk));
1042 info->disk->transferred = blk_mig_bytes_transferred();
1043 info->disk->remaining = blk_mig_bytes_remaining();
1044 info->disk->total = blk_mig_bytes_total();
1045 }
1046}
1047
Kirti Wankhede37105862020-10-26 15:06:27 +05301048static void populate_vfio_info(MigrationInfo *info)
1049{
1050#ifdef CONFIG_VFIO
1051 if (vfio_mig_active()) {
1052 info->has_vfio = true;
1053 info->vfio = g_malloc0(sizeof(*info->vfio));
1054 info->vfio->transferred = vfio_mig_bytes_transferred();
1055 }
1056#endif
1057}
1058
Alexey Perevalov65ace062018-03-22 21:17:27 +03001059static void fill_source_migration_info(MigrationInfo *info)
aliguori5bb79102008-10-13 03:12:02 +00001060{
Juan Quintela17549e82011-10-05 13:50:43 +02001061 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +00001062
Juan Quintela17549e82011-10-05 13:50:43 +02001063 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +08001064 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +02001065 /* no migration has happened ever */
Alexey Perevalov65ace062018-03-22 21:17:27 +03001066 /* do not overwrite destination migration status */
1067 return;
zhanghailiang31194732015-03-13 16:08:38 +08001068 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001069 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -04001070 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001071 break;
zhanghailiang31194732015-03-13 16:08:38 +08001072 case MIGRATION_STATUS_ACTIVE:
1073 case MIGRATION_STATUS_CANCELLING:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001074 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +01001075 case MIGRATION_STATUS_PRE_SWITCHOVER:
1076 case MIGRATION_STATUS_DEVICE:
Peter Xua688d2c2018-05-02 18:47:18 +08001077 case MIGRATION_STATUS_POSTCOPY_PAUSED:
Peter Xu135b87b2018-05-02 18:47:25 +08001078 case MIGRATION_STATUS_POSTCOPY_RECOVER:
Wei Yang640dfb12019-07-16 08:54:11 +08001079 /* TODO add some postcopy stats */
1080 populate_time_info(info, s);
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +01001081 populate_ram_info(info, s);
Juan Quintela930ac042017-05-04 10:21:46 +02001082 populate_disk_info(info);
Kirti Wankhede37105862020-10-26 15:06:27 +05301083 populate_vfio_info(info);
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001084 break;
zhanghailiang0b827d52016-10-27 14:42:54 +08001085 case MIGRATION_STATUS_COLO:
1086 info->has_status = true;
1087 /* TODO: display COLO specific information (checkpoint info etc.) */
1088 break;
zhanghailiang31194732015-03-13 16:08:38 +08001089 case MIGRATION_STATUS_COMPLETED:
Wei Yang640dfb12019-07-16 08:54:11 +08001090 populate_time_info(info, s);
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +01001091 populate_ram_info(info, s);
Kirti Wankhede37105862020-10-26 15:06:27 +05301092 populate_vfio_info(info);
Juan Quintela17549e82011-10-05 13:50:43 +02001093 break;
zhanghailiang31194732015-03-13 16:08:38 +08001094 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -03001095 info->has_status = true;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001096 if (s->error) {
1097 info->has_error_desc = true;
1098 info->error_desc = g_strdup(error_get_pretty(s->error));
1099 }
Juan Quintela17549e82011-10-05 13:50:43 +02001100 break;
zhanghailiang31194732015-03-13 16:08:38 +08001101 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -03001102 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +02001103 break;
Jens Freimannc7e0acd2019-10-29 12:49:02 +01001104 case MIGRATION_STATUS_WAIT_UNPLUG:
1105 info->has_status = true;
1106 break;
aliguori5bb79102008-10-13 03:12:02 +00001107 }
zhanghailiangcde63fb2015-03-13 16:08:41 +08001108 info->status = s->state;
aliguori5bb79102008-10-13 03:12:02 +00001109}
1110
Peter Xu4a842142017-07-18 11:39:08 +08001111/**
1112 * @migration_caps_check - check capability validity
1113 *
1114 * @cap_list: old capability list, array of bool
1115 * @params: new capabilities to be applied soon
1116 * @errp: set *errp if the check failed, with reason
1117 *
1118 * Returns true if check passed, otherwise false.
1119 */
1120static bool migrate_caps_check(bool *cap_list,
1121 MigrationCapabilityStatusList *params,
1122 Error **errp)
Orit Wasserman00458432012-08-06 21:42:48 +03001123{
Orit Wasserman00458432012-08-06 21:42:48 +03001124 MigrationCapabilityStatusList *cap;
Peter Xu4a842142017-07-18 11:39:08 +08001125 bool old_postcopy_cap;
Alexey Perevalovd7651f12017-09-19 19:47:56 +03001126 MigrationIncomingState *mis = migration_incoming_get_current();
Orit Wasserman00458432012-08-06 21:42:48 +03001127
Peter Xu4a842142017-07-18 11:39:08 +08001128 old_postcopy_cap = cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Orit Wasserman00458432012-08-06 21:42:48 +03001129
1130 for (cap = params; cap; cap = cap->next) {
Peter Xu4a842142017-07-18 11:39:08 +08001131 cap_list[cap->value->capability] = cap->value->state;
Orit Wasserman00458432012-08-06 21:42:48 +03001132 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001133
Peter Xu4a842142017-07-18 11:39:08 +08001134#ifndef CONFIG_LIVE_BLOCK_MIGRATION
1135 if (cap_list[MIGRATION_CAPABILITY_BLOCK]) {
1136 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
1137 "block migration");
1138 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
1139 return false;
1140 }
1141#endif
1142
Zhang Chen7e934f52018-11-15 03:09:12 +08001143#ifndef CONFIG_REPLICATION
1144 if (cap_list[MIGRATION_CAPABILITY_X_COLO]) {
1145 error_setg(errp, "QEMU compiled without replication module"
1146 " can't enable COLO");
1147 error_append_hint(errp, "Please enable replication before COLO.\n");
1148 return false;
1149 }
1150#endif
1151
Peter Xu4a842142017-07-18 11:39:08 +08001152 if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +01001153 /* This check is reasonably expensive, so only when it's being
1154 * set the first time, also it's only the destination that needs
1155 * special support.
1156 */
1157 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
Alexey Perevalovd7651f12017-09-19 19:47:56 +03001158 !postcopy_ram_supported_by_host(mis)) {
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +01001159 /* postcopy_ram_supported_by_host will have emitted a more
1160 * detailed message
1161 */
Peter Xu4a842142017-07-18 11:39:08 +08001162 error_setg(errp, "Postcopy is not supported");
1163 return false;
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +01001164 }
Yury Kotov18269062019-02-15 20:45:45 +03001165
1166 if (cap_list[MIGRATION_CAPABILITY_X_IGNORE_SHARED]) {
1167 error_setg(errp, "Postcopy is not compatible with ignore-shared");
1168 return false;
1169 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001170 }
Peter Xu4a842142017-07-18 11:39:08 +08001171
1172 return true;
1173}
1174
Alexey Perevalov65ace062018-03-22 21:17:27 +03001175static void fill_destination_migration_info(MigrationInfo *info)
1176{
1177 MigrationIncomingState *mis = migration_incoming_get_current();
1178
Juan Quintela9aca82b2019-02-27 11:51:27 +01001179 if (mis->socket_address_list) {
1180 info->has_socket_address = true;
1181 info->socket_address =
1182 QAPI_CLONE(SocketAddressList, mis->socket_address_list);
1183 }
1184
Alexey Perevalov65ace062018-03-22 21:17:27 +03001185 switch (mis->state) {
1186 case MIGRATION_STATUS_NONE:
1187 return;
Alexey Perevalov65ace062018-03-22 21:17:27 +03001188 case MIGRATION_STATUS_SETUP:
1189 case MIGRATION_STATUS_CANCELLING:
1190 case MIGRATION_STATUS_CANCELLED:
1191 case MIGRATION_STATUS_ACTIVE:
1192 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Peter Xu3c9928d2018-07-10 17:18:56 +08001193 case MIGRATION_STATUS_POSTCOPY_PAUSED:
1194 case MIGRATION_STATUS_POSTCOPY_RECOVER:
Alexey Perevalov65ace062018-03-22 21:17:27 +03001195 case MIGRATION_STATUS_FAILED:
1196 case MIGRATION_STATUS_COLO:
1197 info->has_status = true;
1198 break;
1199 case MIGRATION_STATUS_COMPLETED:
1200 info->has_status = true;
1201 fill_destination_postcopy_migration_info(info);
1202 break;
1203 }
1204 info->status = mis->state;
1205}
1206
1207MigrationInfo *qmp_query_migrate(Error **errp)
1208{
1209 MigrationInfo *info = g_malloc0(sizeof(*info));
1210
1211 fill_destination_migration_info(info);
1212 fill_source_migration_info(info);
1213
1214 return info;
1215}
1216
Peter Xu4a842142017-07-18 11:39:08 +08001217void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
1218 Error **errp)
1219{
1220 MigrationState *s = migrate_get_current();
1221 MigrationCapabilityStatusList *cap;
Peter Xudd0ee302018-03-05 17:49:38 +08001222 bool cap_list[MIGRATION_CAPABILITY__MAX];
Peter Xu4a842142017-07-18 11:39:08 +08001223
Juan Quintela392d87e2020-01-21 15:39:23 +01001224 if (migration_is_running(s->state)) {
Peter Xu4a842142017-07-18 11:39:08 +08001225 error_setg(errp, QERR_MIGRATION_ACTIVE);
1226 return;
1227 }
1228
Peter Xudd0ee302018-03-05 17:49:38 +08001229 memcpy(cap_list, s->enabled_capabilities, sizeof(cap_list));
1230 if (!migrate_caps_check(cap_list, params, errp)) {
Peter Xu4a842142017-07-18 11:39:08 +08001231 return;
1232 }
1233
1234 for (cap = params; cap; cap = cap->next) {
1235 s->enabled_capabilities[cap->value->capability] = cap->value->state;
1236 }
Orit Wasserman00458432012-08-06 21:42:48 +03001237}
1238
Peter Xu16d063b2017-07-18 11:39:04 +08001239/*
1240 * Check whether the parameters are valid. Error will be put into errp
1241 * (if provided). Return true if valid, otherwise false.
1242 */
1243static bool migrate_params_check(MigrationParameters *params, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +08001244{
Eric Blake7f375e02016-09-08 22:14:16 -05001245 if (params->has_compress_level &&
Juan Quintela741d4082017-12-01 13:08:38 +01001246 (params->compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001247 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
1248 "is invalid, it should be in the range of 0 to 9");
Peter Xu16d063b2017-07-18 11:39:04 +08001249 return false;
Liang Li85de8322015-03-23 16:32:28 +08001250 }
Peter Xu16d063b2017-07-18 11:39:04 +08001251
Juan Quintela741d4082017-12-01 13:08:38 +01001252 if (params->has_compress_threads && (params->compress_threads < 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001253 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1254 "compress_threads",
1255 "is invalid, it should be in the range of 1 to 255");
Peter Xu16d063b2017-07-18 11:39:04 +08001256 return false;
Liang Li85de8322015-03-23 16:32:28 +08001257 }
Peter Xu16d063b2017-07-18 11:39:04 +08001258
Juan Quintela741d4082017-12-01 13:08:38 +01001259 if (params->has_decompress_threads && (params->decompress_threads < 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001260 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1261 "decompress_threads",
1262 "is invalid, it should be in the range of 1 to 255");
Peter Xu16d063b2017-07-18 11:39:04 +08001263 return false;
Liang Li85de8322015-03-23 16:32:28 +08001264 }
Peter Xu16d063b2017-07-18 11:39:04 +08001265
Keqian Zhudc14a472020-02-24 10:31:42 +08001266 if (params->has_throttle_trigger_threshold &&
1267 (params->throttle_trigger_threshold < 1 ||
1268 params->throttle_trigger_threshold > 100)) {
1269 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1270 "throttle_trigger_threshold",
1271 "an integer in the range of 1 to 100");
1272 return false;
1273 }
1274
Eric Blake7f375e02016-09-08 22:14:16 -05001275 if (params->has_cpu_throttle_initial &&
1276 (params->cpu_throttle_initial < 1 ||
1277 params->cpu_throttle_initial > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -04001278 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -04001279 "cpu_throttle_initial",
Jason J. Herne1626fee2015-09-08 13:12:34 -04001280 "an integer in the range of 1 to 99");
Peter Xu16d063b2017-07-18 11:39:04 +08001281 return false;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001282 }
Peter Xu16d063b2017-07-18 11:39:04 +08001283
Eric Blake7f375e02016-09-08 22:14:16 -05001284 if (params->has_cpu_throttle_increment &&
1285 (params->cpu_throttle_increment < 1 ||
1286 params->cpu_throttle_increment > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -04001287 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -04001288 "cpu_throttle_increment",
Jason J. Herne1626fee2015-09-08 13:12:34 -04001289 "an integer in the range of 1 to 99");
Peter Xu16d063b2017-07-18 11:39:04 +08001290 return false;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001291 }
Peter Xu16d063b2017-07-18 11:39:04 +08001292
Juan Quintela741d4082017-12-01 13:08:38 +01001293 if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) {
Mao Zhongyi2ee30cf2020-03-31 16:22:05 +08001294 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1295 "max_bandwidth",
1296 "an integer in the range of 0 to "stringify(SIZE_MAX)
1297 " bytes/second");
Peter Xu16d063b2017-07-18 11:39:04 +08001298 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301299 }
Peter Xu16d063b2017-07-18 11:39:04 +08001300
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301301 if (params->has_downtime_limit &&
Juan Quintela741d4082017-12-01 13:08:38 +01001302 (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
Mao Zhongyi2ee30cf2020-03-31 16:22:05 +08001303 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1304 "downtime_limit",
1305 "an integer in the range of 0 to "
Mao Zhongyi7ac55292020-03-31 16:22:07 +08001306 stringify(MAX_MIGRATE_DOWNTIME)" ms");
Peter Xu16d063b2017-07-18 11:39:04 +08001307 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301308 }
Peter Xu16d063b2017-07-18 11:39:04 +08001309
Juan Quintela741d4082017-12-01 13:08:38 +01001310 /* x_checkpoint_delay is now always positive */
1311
Juan Quintelacbfd6c92019-02-06 13:54:06 +01001312 if (params->has_multifd_channels && (params->multifd_channels < 1)) {
Juan Quintela4075fb12016-01-15 08:56:17 +01001313 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1314 "multifd_channels",
1315 "is invalid, it should be in the range of 1 to 255");
1316 return false;
1317 }
Peter Xu16d063b2017-07-18 11:39:04 +08001318
Juan Quintela9004db42020-01-23 17:08:52 +01001319 if (params->has_multifd_zlib_level &&
1320 (params->multifd_zlib_level > 9)) {
1321 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zlib_level",
1322 "is invalid, it should be in the range of 0 to 9");
1323 return false;
1324 }
1325
Juan Quintela6a9ad152020-01-23 17:41:36 +01001326 if (params->has_multifd_zstd_level &&
1327 (params->multifd_zstd_level > 20)) {
1328 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zstd_level",
1329 "is invalid, it should be in the range of 0 to 20");
1330 return false;
1331 }
1332
Juan Quintela73af8dd2017-10-05 21:30:10 +02001333 if (params->has_xbzrle_cache_size &&
1334 (params->xbzrle_cache_size < qemu_target_page_size() ||
1335 !is_power_of_2(params->xbzrle_cache_size))) {
1336 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1337 "xbzrle_cache_size",
1338 "is invalid, it should be bigger than target page size"
Mao Zhongyi06b1c6f2020-03-20 22:32:16 +08001339 " and a power of 2");
Juan Quintela73af8dd2017-10-05 21:30:10 +02001340 return false;
1341 }
1342
Li Qiang4cbc9c72018-08-01 06:00:20 -07001343 if (params->has_max_cpu_throttle &&
1344 (params->max_cpu_throttle < params->cpu_throttle_initial ||
1345 params->max_cpu_throttle > 99)) {
1346 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1347 "max_cpu_throttle",
1348 "an integer in the range of cpu_throttle_initial to 99");
1349 return false;
1350 }
1351
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00001352 if (params->has_announce_initial &&
1353 params->announce_initial > 100000) {
1354 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1355 "announce_initial",
1356 "is invalid, it must be less than 100000 ms");
1357 return false;
1358 }
1359 if (params->has_announce_max &&
1360 params->announce_max > 100000) {
1361 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1362 "announce_max",
1363 "is invalid, it must be less than 100000 ms");
1364 return false;
1365 }
1366 if (params->has_announce_rounds &&
1367 params->announce_rounds > 1000) {
1368 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1369 "announce_rounds",
1370 "is invalid, it must be in the range of 0 to 1000");
1371 return false;
1372 }
1373 if (params->has_announce_step &&
1374 (params->announce_step < 1 ||
1375 params->announce_step > 10000)) {
1376 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1377 "announce_step",
1378 "is invalid, it must be in the range of 1 to 10000 ms");
1379 return false;
1380 }
Max Reitz31e4c352020-08-20 17:07:23 +02001381
1382 if (params->has_block_bitmap_mapping &&
1383 !check_dirty_bitmap_mig_alias_map(params->block_bitmap_mapping, errp)) {
1384 error_prepend(errp, "Invalid mapping given for block-bitmap-mapping: ");
1385 return false;
1386 }
1387
Peter Xu16d063b2017-07-18 11:39:04 +08001388 return true;
1389}
1390
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001391static void migrate_params_test_apply(MigrateSetParameters *params,
1392 MigrationParameters *dest)
1393{
1394 *dest = migrate_get_current()->parameters;
1395
1396 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1397
1398 if (params->has_compress_level) {
1399 dest->compress_level = params->compress_level;
1400 }
1401
1402 if (params->has_compress_threads) {
1403 dest->compress_threads = params->compress_threads;
1404 }
1405
Xiao Guangrong1d588722018-08-21 16:10:20 +08001406 if (params->has_compress_wait_thread) {
1407 dest->compress_wait_thread = params->compress_wait_thread;
1408 }
1409
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001410 if (params->has_decompress_threads) {
1411 dest->decompress_threads = params->decompress_threads;
1412 }
1413
Keqian Zhudc14a472020-02-24 10:31:42 +08001414 if (params->has_throttle_trigger_threshold) {
1415 dest->throttle_trigger_threshold = params->throttle_trigger_threshold;
1416 }
1417
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001418 if (params->has_cpu_throttle_initial) {
1419 dest->cpu_throttle_initial = params->cpu_throttle_initial;
1420 }
1421
1422 if (params->has_cpu_throttle_increment) {
1423 dest->cpu_throttle_increment = params->cpu_throttle_increment;
1424 }
1425
Keqian Zhucbbf8182020-04-13 18:15:08 +08001426 if (params->has_cpu_throttle_tailslow) {
1427 dest->cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1428 }
1429
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001430 if (params->has_tls_creds) {
Markus Armbruster01fa5592017-07-18 14:42:04 +02001431 assert(params->tls_creds->type == QTYPE_QSTRING);
Zheng Chuan9728ebf2020-07-09 16:28:25 +08001432 dest->tls_creds = params->tls_creds->u.s;
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001433 }
1434
1435 if (params->has_tls_hostname) {
Markus Armbruster01fa5592017-07-18 14:42:04 +02001436 assert(params->tls_hostname->type == QTYPE_QSTRING);
Zheng Chuan9728ebf2020-07-09 16:28:25 +08001437 dest->tls_hostname = params->tls_hostname->u.s;
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001438 }
1439
1440 if (params->has_max_bandwidth) {
1441 dest->max_bandwidth = params->max_bandwidth;
1442 }
1443
1444 if (params->has_downtime_limit) {
1445 dest->downtime_limit = params->downtime_limit;
1446 }
1447
1448 if (params->has_x_checkpoint_delay) {
1449 dest->x_checkpoint_delay = params->x_checkpoint_delay;
1450 }
1451
1452 if (params->has_block_incremental) {
1453 dest->block_incremental = params->block_incremental;
1454 }
Juan Quintelacbfd6c92019-02-06 13:54:06 +01001455 if (params->has_multifd_channels) {
1456 dest->multifd_channels = params->multifd_channels;
Juan Quintela5e7577a2017-10-09 18:07:56 +02001457 }
Juan Quintela96eef042019-01-16 10:35:55 +01001458 if (params->has_multifd_compression) {
1459 dest->multifd_compression = params->multifd_compression;
1460 }
Juan Quintela73af8dd2017-10-05 21:30:10 +02001461 if (params->has_xbzrle_cache_size) {
1462 dest->xbzrle_cache_size = params->xbzrle_cache_size;
1463 }
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01001464 if (params->has_max_postcopy_bandwidth) {
1465 dest->max_postcopy_bandwidth = params->max_postcopy_bandwidth;
1466 }
Li Qiang4cbc9c72018-08-01 06:00:20 -07001467 if (params->has_max_cpu_throttle) {
1468 dest->max_cpu_throttle = params->max_cpu_throttle;
1469 }
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00001470 if (params->has_announce_initial) {
1471 dest->announce_initial = params->announce_initial;
1472 }
1473 if (params->has_announce_max) {
1474 dest->announce_max = params->announce_max;
1475 }
1476 if (params->has_announce_rounds) {
1477 dest->announce_rounds = params->announce_rounds;
1478 }
1479 if (params->has_announce_step) {
1480 dest->announce_step = params->announce_step;
1481 }
Max Reitz31e4c352020-08-20 17:07:23 +02001482
1483 if (params->has_block_bitmap_mapping) {
1484 dest->has_block_bitmap_mapping = true;
1485 dest->block_bitmap_mapping = params->block_bitmap_mapping;
1486 }
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001487}
1488
Juan Quintela73af8dd2017-10-05 21:30:10 +02001489static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
Peter Xu16d063b2017-07-18 11:39:04 +08001490{
1491 MigrationState *s = migrate_get_current();
1492
Markus Armbrustere87fae42017-07-18 12:57:38 +02001493 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1494
Eric Blake7f375e02016-09-08 22:14:16 -05001495 if (params->has_compress_level) {
1496 s->parameters.compress_level = params->compress_level;
Liang Li85de8322015-03-23 16:32:28 +08001497 }
Peter Xu476c72a2017-07-18 11:39:05 +08001498
Eric Blake7f375e02016-09-08 22:14:16 -05001499 if (params->has_compress_threads) {
1500 s->parameters.compress_threads = params->compress_threads;
Liang Li85de8322015-03-23 16:32:28 +08001501 }
Peter Xu476c72a2017-07-18 11:39:05 +08001502
Xiao Guangrong1d588722018-08-21 16:10:20 +08001503 if (params->has_compress_wait_thread) {
1504 s->parameters.compress_wait_thread = params->compress_wait_thread;
1505 }
1506
Eric Blake7f375e02016-09-08 22:14:16 -05001507 if (params->has_decompress_threads) {
1508 s->parameters.decompress_threads = params->decompress_threads;
Liang Li85de8322015-03-23 16:32:28 +08001509 }
Peter Xu476c72a2017-07-18 11:39:05 +08001510
Keqian Zhudc14a472020-02-24 10:31:42 +08001511 if (params->has_throttle_trigger_threshold) {
1512 s->parameters.throttle_trigger_threshold = params->throttle_trigger_threshold;
1513 }
1514
Eric Blake7f375e02016-09-08 22:14:16 -05001515 if (params->has_cpu_throttle_initial) {
1516 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001517 }
Peter Xu476c72a2017-07-18 11:39:05 +08001518
Eric Blake7f375e02016-09-08 22:14:16 -05001519 if (params->has_cpu_throttle_increment) {
1520 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001521 }
Peter Xu476c72a2017-07-18 11:39:05 +08001522
Keqian Zhucbbf8182020-04-13 18:15:08 +08001523 if (params->has_cpu_throttle_tailslow) {
1524 s->parameters.cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1525 }
1526
Eric Blake7f375e02016-09-08 22:14:16 -05001527 if (params->has_tls_creds) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001528 g_free(s->parameters.tls_creds);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001529 assert(params->tls_creds->type == QTYPE_QSTRING);
1530 s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001531 }
Peter Xu476c72a2017-07-18 11:39:05 +08001532
Eric Blake7f375e02016-09-08 22:14:16 -05001533 if (params->has_tls_hostname) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001534 g_free(s->parameters.tls_hostname);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001535 assert(params->tls_hostname->type == QTYPE_QSTRING);
1536 s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001537 }
Peter Xu476c72a2017-07-18 11:39:05 +08001538
Daniel P. Berranged2f1d292019-02-27 14:53:24 +00001539 if (params->has_tls_authz) {
1540 g_free(s->parameters.tls_authz);
1541 assert(params->tls_authz->type == QTYPE_QSTRING);
1542 s->parameters.tls_authz = g_strdup(params->tls_authz->u.s);
1543 }
1544
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301545 if (params->has_max_bandwidth) {
1546 s->parameters.max_bandwidth = params->max_bandwidth;
Dr. David Alan Gilbertc38c1c12019-03-08 10:12:10 +00001547 if (s->to_dst_file && !migration_in_postcopy()) {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301548 qemu_file_set_rate_limit(s->to_dst_file,
1549 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
1550 }
1551 }
Peter Xu476c72a2017-07-18 11:39:05 +08001552
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301553 if (params->has_downtime_limit) {
1554 s->parameters.downtime_limit = params->downtime_limit;
1555 }
zhanghailiang68b53592016-10-27 14:43:01 +08001556
1557 if (params->has_x_checkpoint_delay) {
1558 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
zhanghailiang479125d2017-01-17 20:57:42 +08001559 if (migration_in_colo_state()) {
1560 colo_checkpoint_notify(s);
1561 }
zhanghailiang68b53592016-10-27 14:43:01 +08001562 }
Peter Xu476c72a2017-07-18 11:39:05 +08001563
Juan Quintela2833c592017-04-05 18:32:37 +02001564 if (params->has_block_incremental) {
1565 s->parameters.block_incremental = params->block_incremental;
1566 }
Juan Quintelacbfd6c92019-02-06 13:54:06 +01001567 if (params->has_multifd_channels) {
1568 s->parameters.multifd_channels = params->multifd_channels;
Juan Quintela4075fb12016-01-15 08:56:17 +01001569 }
Juan Quintela96eef042019-01-16 10:35:55 +01001570 if (params->has_multifd_compression) {
1571 s->parameters.multifd_compression = params->multifd_compression;
1572 }
Juan Quintela73af8dd2017-10-05 21:30:10 +02001573 if (params->has_xbzrle_cache_size) {
1574 s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
1575 xbzrle_cache_resize(params->xbzrle_cache_size, errp);
1576 }
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01001577 if (params->has_max_postcopy_bandwidth) {
1578 s->parameters.max_postcopy_bandwidth = params->max_postcopy_bandwidth;
Dr. David Alan Gilbertc38c1c12019-03-08 10:12:10 +00001579 if (s->to_dst_file && migration_in_postcopy()) {
1580 qemu_file_set_rate_limit(s->to_dst_file,
1581 s->parameters.max_postcopy_bandwidth / XFER_LIMIT_RATIO);
1582 }
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01001583 }
Li Qiang4cbc9c72018-08-01 06:00:20 -07001584 if (params->has_max_cpu_throttle) {
1585 s->parameters.max_cpu_throttle = params->max_cpu_throttle;
1586 }
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00001587 if (params->has_announce_initial) {
1588 s->parameters.announce_initial = params->announce_initial;
1589 }
1590 if (params->has_announce_max) {
1591 s->parameters.announce_max = params->announce_max;
1592 }
1593 if (params->has_announce_rounds) {
1594 s->parameters.announce_rounds = params->announce_rounds;
1595 }
1596 if (params->has_announce_step) {
1597 s->parameters.announce_step = params->announce_step;
1598 }
Max Reitz31e4c352020-08-20 17:07:23 +02001599
1600 if (params->has_block_bitmap_mapping) {
1601 qapi_free_BitmapMigrationNodeAliasList(
1602 s->parameters.block_bitmap_mapping);
1603
1604 s->parameters.has_block_bitmap_mapping = true;
1605 s->parameters.block_bitmap_mapping =
1606 QAPI_CLONE(BitmapMigrationNodeAliasList,
1607 params->block_bitmap_mapping);
1608 }
Liang Li85de8322015-03-23 16:32:28 +08001609}
1610
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001611void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
Peter Xu476c72a2017-07-18 11:39:05 +08001612{
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001613 MigrationParameters tmp;
1614
Markus Armbruster01fa5592017-07-18 14:42:04 +02001615 /* TODO Rewrite "" to null instead */
1616 if (params->has_tls_creds
1617 && params->tls_creds->type == QTYPE_QNULL) {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001618 qobject_unref(params->tls_creds->u.n);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001619 params->tls_creds->type = QTYPE_QSTRING;
1620 params->tls_creds->u.s = strdup("");
1621 }
1622 /* TODO Rewrite "" to null instead */
1623 if (params->has_tls_hostname
1624 && params->tls_hostname->type == QTYPE_QNULL) {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001625 qobject_unref(params->tls_hostname->u.n);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001626 params->tls_hostname->type = QTYPE_QSTRING;
1627 params->tls_hostname->u.s = strdup("");
1628 }
1629
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001630 migrate_params_test_apply(params, &tmp);
1631
1632 if (!migrate_params_check(&tmp, errp)) {
Peter Xu476c72a2017-07-18 11:39:05 +08001633 /* Invalid parameter */
1634 return;
1635 }
1636
Juan Quintela73af8dd2017-10-05 21:30:10 +02001637 migrate_params_apply(params, errp);
Peter Xu476c72a2017-07-18 11:39:05 +08001638}
1639
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001640
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001641void qmp_migrate_start_postcopy(Error **errp)
1642{
1643 MigrationState *s = migrate_get_current();
1644
Vladimir Sementsov-Ogievskiy16b0fd32018-03-13 15:34:01 -04001645 if (!migrate_postcopy()) {
Dr. David Alan Gilberta54d3402015-11-12 11:34:44 +00001646 error_setg(errp, "Enable postcopy with migrate_set_capability before"
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001647 " the start of migration");
1648 return;
1649 }
1650
1651 if (s->state == MIGRATION_STATUS_NONE) {
1652 error_setg(errp, "Postcopy must be started after migration has been"
1653 " started");
1654 return;
1655 }
1656 /*
1657 * we don't error if migration has finished since that would be racy
1658 * with issuing this command.
1659 */
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001660 qatomic_set(&s->start_postcopy, true);
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001661}
1662
aliguori065e2812008-11-11 16:46:33 +00001663/* shared migration helpers */
1664
zhanghailiang48781e52015-12-16 11:47:33 +00001665void migrate_set_state(int *state, int old_state, int new_state)
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +00001666{
Peter Xua31fede2017-08-30 16:32:01 +08001667 assert(new_state < MIGRATION_STATUS__MAX);
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001668 if (qatomic_cmpxchg(state, old_state, new_state) == old_state) {
Peter Xua31fede2017-08-30 16:32:01 +08001669 trace_migrate_set_state(MigrationStatus_str(new_state));
Juan Quintelab05dc722015-07-07 14:44:05 +02001670 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +00001671 }
1672}
1673
Peter Xu4e4a3d32017-07-18 11:39:09 +08001674static MigrationCapabilityStatusList *migrate_cap_add(
1675 MigrationCapabilityStatusList *list,
1676 MigrationCapability index,
1677 bool state)
Juan Quintela2833c592017-04-05 18:32:37 +02001678{
1679 MigrationCapabilityStatusList *cap;
1680
1681 cap = g_new0(MigrationCapabilityStatusList, 1);
1682 cap->value = g_new0(MigrationCapabilityStatus, 1);
Peter Xu4e4a3d32017-07-18 11:39:09 +08001683 cap->value->capability = index;
1684 cap->value->state = state;
1685 cap->next = list;
1686
1687 return cap;
1688}
1689
1690void migrate_set_block_enabled(bool value, Error **errp)
1691{
1692 MigrationCapabilityStatusList *cap;
1693
1694 cap = migrate_cap_add(NULL, MIGRATION_CAPABILITY_BLOCK, value);
Juan Quintela2833c592017-04-05 18:32:37 +02001695 qmp_migrate_set_capabilities(cap, errp);
1696 qapi_free_MigrationCapabilityStatusList(cap);
1697}
1698
1699static void migrate_set_block_incremental(MigrationState *s, bool value)
1700{
1701 s->parameters.block_incremental = value;
1702}
1703
1704static void block_cleanup_parameters(MigrationState *s)
1705{
1706 if (s->must_remove_block_options) {
1707 /* setting to false can never fail */
1708 migrate_set_block_enabled(false, &error_abort);
1709 migrate_set_block_incremental(s, false);
1710 s->must_remove_block_options = false;
1711 }
1712}
1713
Yury Kotovfd392cf2019-04-08 14:33:43 +03001714static void migrate_fd_cleanup(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +00001715{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001716 qemu_bh_delete(s->cleanup_bh);
1717 s->cleanup_bh = NULL;
1718
Peter Xu0ceccd82018-01-03 20:20:06 +08001719 qemu_savevm_state_cleanup();
1720
zhanghailiang89a02a92016-01-15 11:37:42 +08001721 if (s->to_dst_file) {
Peter Xu62df0662018-05-02 18:47:38 +08001722 QEMUFile *tmp;
Juan Quintelaf986c3d2016-01-14 16:52:55 +01001723
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001724 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001725 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001726 if (s->migration_thread_running) {
1727 qemu_thread_join(&s->thread);
1728 s->migration_thread_running = false;
1729 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001730 qemu_mutex_lock_iothread();
1731
Fei Li1398b2e2019-01-13 22:08:47 +08001732 multifd_save_cleanup();
Peter Xu62df0662018-05-02 18:47:38 +08001733 qemu_mutex_lock(&s->qemu_file_lock);
1734 tmp = s->to_dst_file;
zhanghailiang89a02a92016-01-15 11:37:42 +08001735 s->to_dst_file = NULL;
Peter Xu62df0662018-05-02 18:47:38 +08001736 qemu_mutex_unlock(&s->qemu_file_lock);
1737 /*
1738 * Close the file handle without the lock to make sure the
1739 * critical section won't block for long.
1740 */
1741 qemu_fclose(tmp);
aliguori065e2812008-11-11 16:46:33 +00001742 }
1743
Wei Yang8f8d5282019-07-17 08:53:41 +08001744 assert(!migration_is_active(s));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001745
Liang Li94f5a432015-11-02 15:37:00 +08001746 if (s->state == MIGRATION_STATUS_CANCELLING) {
zhanghailiang48781e52015-12-16 11:47:33 +00001747 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
Liang Li94f5a432015-11-02 15:37:00 +08001748 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001749 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001750
Juan Quintela87db1a72017-09-05 12:50:22 +02001751 if (s->error) {
1752 /* It is used on info migrate. We can't free it */
1753 error_report_err(error_copy(s->error));
1754 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001755 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +02001756 block_cleanup_parameters(s);
aliguori065e2812008-11-11 16:46:33 +00001757}
1758
Yury Kotovfd392cf2019-04-08 14:33:43 +03001759static void migrate_fd_cleanup_schedule(MigrationState *s)
1760{
1761 /*
1762 * Ref the state for bh, because it may be called when
1763 * there're already no other refs
1764 */
1765 object_ref(OBJECT(s));
1766 qemu_bh_schedule(s->cleanup_bh);
1767}
1768
1769static void migrate_fd_cleanup_bh(void *opaque)
1770{
1771 MigrationState *s = opaque;
1772 migrate_fd_cleanup(s);
1773 object_unref(OBJECT(s));
1774}
1775
Juan Quintela87db1a72017-09-05 12:50:22 +02001776void migrate_set_error(MigrationState *s, const Error *error)
1777{
Daniel Brodsky6e8a3552020-04-03 21:21:08 -07001778 QEMU_LOCK_GUARD(&s->error_mutex);
Juan Quintela87db1a72017-09-05 12:50:22 +02001779 if (!s->error) {
1780 s->error = error_copy(error);
1781 }
Juan Quintela87db1a72017-09-05 12:50:22 +02001782}
1783
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001784void migrate_fd_error(MigrationState *s, const Error *error)
aliguori065e2812008-11-11 16:46:33 +00001785{
Peter Maydell25174052016-10-21 18:41:45 +01001786 trace_migrate_fd_error(error_get_pretty(error));
zhanghailiang89a02a92016-01-15 11:37:42 +08001787 assert(s->to_dst_file == NULL);
zhanghailiang48781e52015-12-16 11:47:33 +00001788 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1789 MIGRATION_STATUS_FAILED);
Juan Quintela87db1a72017-09-05 12:50:22 +02001790 migrate_set_error(s, error);
Juan Quintela458cf282011-02-22 23:32:54 +01001791}
1792
Juan Quintela0edda1c2010-05-11 16:28:39 +02001793static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +00001794{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001795 int old_state ;
zhanghailiang89a02a92016-01-15 11:37:42 +08001796 QEMUFile *f = migrate_get_current()->to_dst_file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001797 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +00001798
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001799 if (s->rp_state.from_dst_file) {
1800 /* shutdown the rp socket, so causing the rp thread to shutdown */
1801 qemu_file_shutdown(s->rp_state.from_dst_file);
1802 }
1803
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001804 do {
1805 old_state = s->state;
Juan Quintela392d87e2020-01-21 15:39:23 +01001806 if (!migration_is_running(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001807 break;
1808 }
Dr. David Alan Gilberta7b36b42017-10-20 10:05:55 +01001809 /* If the migration is paused, kick it out of the pause */
1810 if (old_state == MIGRATION_STATUS_PRE_SWITCHOVER) {
1811 qemu_sem_post(&s->pause_sem);
1812 }
zhanghailiang48781e52015-12-16 11:47:33 +00001813 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
zhanghailiang31194732015-03-13 16:08:38 +08001814 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001815
1816 /*
1817 * If we're unlucky the migration code might be stuck somewhere in a
1818 * send/write while the network has failed and is waiting to timeout;
1819 * if we've got shutdown(2) available then we can force it to quit.
1820 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1821 * called in a bh, so there is no race against this cancel.
1822 */
zhanghailiang31194732015-03-13 16:08:38 +08001823 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001824 qemu_file_shutdown(f);
1825 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08001826 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
1827 Error *local_err = NULL;
1828
1829 bdrv_invalidate_cache_all(&local_err);
1830 if (local_err) {
1831 error_report_err(local_err);
1832 } else {
1833 s->block_inactive = false;
1834 }
1835 }
aliguori065e2812008-11-11 16:46:33 +00001836}
1837
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001838void add_migration_state_change_notifier(Notifier *notify)
1839{
1840 notifier_list_add(&migration_state_notifiers, notify);
1841}
1842
1843void remove_migration_state_change_notifier(Notifier *notify)
1844{
Paolo Bonzini31552522012-01-13 17:34:01 +01001845 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001846}
1847
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +02001848bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001849{
zhanghailiang31194732015-03-13 16:08:38 +08001850 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001851}
1852
Juan Quintela70736932011-02-23 00:43:59 +01001853bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001854{
zhanghailiang31194732015-03-13 16:08:38 +08001855 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001856}
Juan Quintela0edda1c2010-05-11 16:28:39 +02001857
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001858bool migration_has_failed(MigrationState *s)
1859{
zhanghailiang31194732015-03-13 16:08:38 +08001860 return (s->state == MIGRATION_STATUS_CANCELLED ||
1861 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001862}
1863
Juan Quintela57273092017-03-20 22:25:28 +01001864bool migration_in_postcopy(void)
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001865{
Juan Quintela57273092017-03-20 22:25:28 +01001866 MigrationState *s = migrate_get_current();
1867
Dr. David Alan Gilbert3748fef2019-09-23 18:49:42 +01001868 switch (s->state) {
1869 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1870 case MIGRATION_STATUS_POSTCOPY_PAUSED:
1871 case MIGRATION_STATUS_POSTCOPY_RECOVER:
1872 return true;
1873 default:
1874 return false;
1875 }
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001876}
1877
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001878bool migration_in_postcopy_after_devices(MigrationState *s)
1879{
Juan Quintela57273092017-03-20 22:25:28 +01001880 return migration_in_postcopy() && s->postcopy_after_devices;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001881}
1882
David Hildenbrand06df2e62020-06-26 09:22:33 +02001883bool migration_in_incoming_postcopy(void)
1884{
1885 PostcopyState ps = postcopy_state_get();
1886
1887 return ps >= POSTCOPY_INCOMING_DISCARD && ps < POSTCOPY_INCOMING_END;
1888}
1889
Juan Quintelafab35002017-03-22 17:36:57 +01001890bool migration_is_idle(void)
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301891{
Markus Armbrusterdaff7f02019-04-01 11:08:25 +02001892 MigrationState *s = current_migration;
1893
1894 if (!s) {
1895 return true;
1896 }
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301897
1898 switch (s->state) {
1899 case MIGRATION_STATUS_NONE:
1900 case MIGRATION_STATUS_CANCELLED:
1901 case MIGRATION_STATUS_COMPLETED:
1902 case MIGRATION_STATUS_FAILED:
1903 return true;
1904 case MIGRATION_STATUS_SETUP:
1905 case MIGRATION_STATUS_CANCELLING:
1906 case MIGRATION_STATUS_ACTIVE:
1907 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1908 case MIGRATION_STATUS_COLO:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +01001909 case MIGRATION_STATUS_PRE_SWITCHOVER:
1910 case MIGRATION_STATUS_DEVICE:
Jens Freimannc7e0acd2019-10-29 12:49:02 +01001911 case MIGRATION_STATUS_WAIT_UNPLUG:
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301912 return false;
1913 case MIGRATION_STATUS__MAX:
1914 g_assert_not_reached();
1915 }
1916
1917 return false;
1918}
1919
Wei Yang8f8d5282019-07-17 08:53:41 +08001920bool migration_is_active(MigrationState *s)
1921{
1922 return (s->state == MIGRATION_STATUS_ACTIVE ||
1923 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1924}
1925
Peter Xu3e0c8052018-02-08 18:31:15 +08001926void migrate_init(MigrationState *s)
Juan Quintela0edda1c2010-05-11 16:28:39 +02001927{
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001928 /*
1929 * Reinitialise all migration state, except
1930 * parameters/capabilities that the user set, and
1931 * locks.
1932 */
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001933 s->cleanup_bh = 0;
zhanghailiang89a02a92016-01-15 11:37:42 +08001934 s->to_dst_file = NULL;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001935 s->state = MIGRATION_STATUS_NONE;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001936 s->rp_state.from_dst_file = NULL;
1937 s->rp_state.error = false;
1938 s->mbps = 0.0;
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08001939 s->pages_per_second = 0.0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001940 s->downtime = 0;
1941 s->expected_downtime = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001942 s->setup_time = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001943 s->start_postcopy = false;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001944 s->postcopy_after_devices = false;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001945 s->migration_thread_running = false;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001946 error_free(s->error);
1947 s->error = NULL;
Chuan Zhengd8053e72020-09-15 11:03:57 +08001948 s->hostname = NULL;
Juan Quintela1299c632011-11-09 21:29:01 +01001949
zhanghailiang48781e52015-12-16 11:47:33 +00001950 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +02001951
Peter Xu4af246a2018-01-03 20:20:08 +08001952 s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1953 s->total_time = 0;
Peter Xu7287cbd2018-01-03 20:20:09 +08001954 s->vm_was_running = false;
Peter Xub15df1a2018-01-03 20:20:13 +08001955 s->iteration_initial_bytes = 0;
1956 s->threshold_size = 0;
Juan Quintela0edda1c2010-05-11 16:28:39 +02001957}
Juan Quintelacab30142011-02-22 23:54:21 +01001958
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001959static GSList *migration_blockers;
1960
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301961int migrate_add_blocker(Error *reason, Error **errp)
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001962{
Markus Armbruster811f8652019-04-01 11:08:24 +02001963 if (only_migratable) {
Markus Armbruster4b576642018-10-17 10:26:25 +02001964 error_propagate_prepend(errp, error_copy(reason),
1965 "disallowing migration blocker "
Greg Kurzb6eca812019-05-16 12:58:05 +02001966 "(--only-migratable) for: ");
Ashijeet Acharyab67b8c32017-01-16 17:01:54 +05301967 return -EACCES;
1968 }
1969
Juan Quintelafab35002017-03-22 17:36:57 +01001970 if (migration_is_idle()) {
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301971 migration_blockers = g_slist_prepend(migration_blockers, reason);
1972 return 0;
1973 }
1974
Markus Armbruster4b576642018-10-17 10:26:25 +02001975 error_propagate_prepend(errp, error_copy(reason),
1976 "disallowing migration blocker "
1977 "(migration in progress) for: ");
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301978 return -EBUSY;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001979}
1980
1981void migrate_del_blocker(Error *reason)
1982{
1983 migration_blockers = g_slist_remove(migration_blockers, reason);
1984}
1985
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001986void qmp_migrate_incoming(const char *uri, Error **errp)
1987{
1988 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001989 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001990
1991 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001992 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001993 return;
1994 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001995 if (!once) {
1996 error_setg(errp, "The incoming migration has already been started");
Yury Kotov603d5a42019-11-13 20:53:25 +03001997 return;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001998 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001999
2000 qemu_start_incoming_migration(uri, &local_err);
2001
2002 if (local_err) {
2003 error_propagate(errp, local_err);
2004 return;
2005 }
2006
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00002007 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00002008}
2009
Peter Xu02affd42018-05-02 18:47:36 +08002010void qmp_migrate_recover(const char *uri, Error **errp)
2011{
2012 MigrationIncomingState *mis = migration_incoming_get_current();
2013
2014 if (mis->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
2015 error_setg(errp, "Migrate recover can only be run "
2016 "when postcopy is paused.");
2017 return;
2018 }
2019
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01002020 if (qatomic_cmpxchg(&mis->postcopy_recover_triggered,
Peter Xu02affd42018-05-02 18:47:36 +08002021 false, true) == true) {
2022 error_setg(errp, "Migrate recovery is triggered already");
2023 return;
2024 }
2025
2026 /*
2027 * Note that this call will never start a real migration; it will
2028 * only re-setup the migration stream and poke existing migration
2029 * to continue using that newly established channel.
2030 */
2031 qemu_start_incoming_migration(uri, errp);
2032}
2033
Peter Xubfbf89c2018-05-02 18:47:39 +08002034void qmp_migrate_pause(Error **errp)
2035{
2036 MigrationState *ms = migrate_get_current();
2037 MigrationIncomingState *mis = migration_incoming_get_current();
2038 int ret;
2039
2040 if (ms->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2041 /* Source side, during postcopy */
2042 qemu_mutex_lock(&ms->qemu_file_lock);
2043 ret = qemu_file_shutdown(ms->to_dst_file);
2044 qemu_mutex_unlock(&ms->qemu_file_lock);
2045 if (ret) {
2046 error_setg(errp, "Failed to pause source migration");
2047 }
2048 return;
2049 }
2050
2051 if (mis->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2052 ret = qemu_file_shutdown(mis->from_src_file);
2053 if (ret) {
2054 error_setg(errp, "Failed to pause destination migration");
2055 }
2056 return;
2057 }
2058
2059 error_setg(errp, "migrate-pause is currently only supported "
2060 "during postcopy-active state");
2061}
2062
Greg Kurz24f39022016-05-04 21:44:19 +02002063bool migration_is_blocked(Error **errp)
2064{
2065 if (qemu_savevm_state_blocked(errp)) {
2066 return true;
2067 }
2068
2069 if (migration_blockers) {
Eduardo Habkost250561e2017-06-08 10:39:05 -03002070 error_propagate(errp, error_copy(migration_blockers->data));
Greg Kurz24f39022016-05-04 21:44:19 +02002071 return true;
2072 }
2073
2074 return false;
2075}
2076
Peter Xud3e35b82018-05-02 18:47:24 +08002077/* Returns true if continue to migrate, or false if error detected */
2078static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc,
2079 bool resume, Error **errp)
2080{
2081 Error *local_err = NULL;
2082
2083 if (resume) {
2084 if (s->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
2085 error_setg(errp, "Cannot resume if there is no "
2086 "paused migration");
2087 return false;
2088 }
Peter Xu97ca2112018-07-23 20:33:03 +08002089
2090 /*
2091 * Postcopy recovery won't work well with release-ram
2092 * capability since release-ram will drop the page buffer as
2093 * long as the page is put into the send buffer. So if there
2094 * is a network failure happened, any page buffers that have
2095 * not yet reached the destination VM but have already been
2096 * sent from the source VM will be lost forever. Let's refuse
2097 * the client from resuming such a postcopy migration.
2098 * Luckily release-ram was designed to only be used when src
2099 * and destination VMs are on the same host, so it should be
2100 * fine.
2101 */
2102 if (migrate_release_ram()) {
2103 error_setg(errp, "Postcopy recovery cannot work "
2104 "when release-ram capability is set");
2105 return false;
2106 }
2107
Peter Xud3e35b82018-05-02 18:47:24 +08002108 /* This is a resume, skip init status */
2109 return true;
2110 }
2111
Juan Quintela392d87e2020-01-21 15:39:23 +01002112 if (migration_is_running(s->state)) {
Peter Xud3e35b82018-05-02 18:47:24 +08002113 error_setg(errp, QERR_MIGRATION_ACTIVE);
2114 return false;
2115 }
2116
2117 if (runstate_check(RUN_STATE_INMIGRATE)) {
2118 error_setg(errp, "Guest is waiting for an incoming migration");
2119 return false;
2120 }
2121
2122 if (migration_is_blocked(errp)) {
2123 return false;
2124 }
2125
2126 if (blk || blk_inc) {
2127 if (migrate_use_block() || migrate_use_block_incremental()) {
2128 error_setg(errp, "Command options are incompatible with "
2129 "current migration capabilities");
2130 return false;
2131 }
2132 migrate_set_block_enabled(true, &local_err);
2133 if (local_err) {
2134 error_propagate(errp, local_err);
2135 return false;
2136 }
2137 s->must_remove_block_options = true;
2138 }
2139
2140 if (blk_inc) {
2141 migrate_set_block_incremental(s, true);
2142 }
2143
2144 migrate_init(s);
Ivan Ren87f3bd82019-08-02 18:18:41 +08002145 /*
2146 * set ram_counters memory to zero for a
2147 * new migration
2148 */
2149 memset(&ram_counters, 0, sizeof(ram_counters));
Peter Xud3e35b82018-05-02 18:47:24 +08002150
2151 return true;
2152}
2153
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002154void qmp_migrate(const char *uri, bool has_blk, bool blk,
2155 bool has_inc, bool inc, bool has_detach, bool detach,
Peter Xu7a4da282018-05-02 18:47:23 +08002156 bool has_resume, bool resume, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002157{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +02002158 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +02002159 MigrationState *s = migrate_get_current();
Longpeng(Mike)d658f652020-08-06 15:40:29 +08002160 const char *p = NULL;
Juan Quintelacab30142011-02-22 23:54:21 +01002161
Peter Xud3e35b82018-05-02 18:47:24 +08002162 if (!migrate_prepare(s, has_blk && blk, has_inc && inc,
2163 has_resume && resume, errp)) {
2164 /* Error detected, put into errp */
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002165 return;
Juan Quintelacab30142011-02-22 23:54:21 +01002166 }
Juan Quintelacab30142011-02-22 23:54:21 +01002167
Longpeng(Mike)d658f652020-08-06 15:40:29 +08002168 if (strstart(uri, "tcp:", &p) ||
Longpeng(Mike)9ba3b2b2020-08-06 15:40:30 +08002169 strstart(uri, "unix:", NULL) ||
2170 strstart(uri, "vsock:", NULL)) {
Longpeng(Mike)d658f652020-08-06 15:40:29 +08002171 socket_start_outgoing_migration(s, p ? p : uri, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04002172#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08002173 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04002174 rdma_start_outgoing_migration(s, p, &local_err);
2175#endif
Juan Quintelacab30142011-02-22 23:54:21 +01002176 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002177 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01002178 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002179 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01002180 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002181 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
2182 "a valid migration protocol");
zhanghailiang48781e52015-12-16 11:47:33 +00002183 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2184 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09576e72018-03-16 20:21:14 +00002185 block_cleanup_parameters(s);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002186 return;
Juan Quintelacab30142011-02-22 23:54:21 +01002187 }
2188
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002189 if (local_err) {
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01002190 migrate_fd_error(s, local_err);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002191 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002192 return;
Juan Quintela1299c632011-11-09 21:29:01 +01002193 }
Juan Quintelacab30142011-02-22 23:54:21 +01002194}
2195
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02002196void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002197{
Juan Quintela17549e82011-10-05 13:50:43 +02002198 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01002199}
2200
Dr. David Alan Gilbert89cfc022017-10-20 10:05:53 +01002201void qmp_migrate_continue(MigrationStatus state, Error **errp)
2202{
2203 MigrationState *s = migrate_get_current();
2204 if (s->state != state) {
2205 error_setg(errp, "Migration not in expected state: %s",
2206 MigrationStatus_str(s->state));
2207 return;
2208 }
2209 qemu_sem_post(&s->pause_sem);
2210}
2211
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002212void qmp_migrate_set_cache_size(int64_t value, Error **errp)
2213{
Juan Quintela73af8dd2017-10-05 21:30:10 +02002214 MigrateSetParameters p = {
2215 .has_xbzrle_cache_size = true,
2216 .xbzrle_cache_size = value,
2217 };
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002218
Juan Quintela73af8dd2017-10-05 21:30:10 +02002219 qmp_migrate_set_parameters(&p, errp);
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002220}
2221
2222int64_t qmp_query_migrate_cache_size(Error **errp)
2223{
2224 return migrate_xbzrle_cache_size();
2225}
2226
Luiz Capitulino3dc85382011-11-28 11:59:37 -02002227void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002228{
Markus Armbruster1bda8b32017-07-18 13:42:11 +02002229 MigrateSetParameters p = {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302230 .has_max_bandwidth = true,
2231 .max_bandwidth = value,
2232 };
Juan Quintelacab30142011-02-22 23:54:21 +01002233
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302234 qmp_migrate_set_parameters(&p, errp);
Juan Quintelacab30142011-02-22 23:54:21 +01002235}
2236
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02002237void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002238{
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03002239 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
Mao Zhongyi2ee30cf2020-03-31 16:22:05 +08002240 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
2241 "downtime_limit",
2242 "an integer in the range of 0 to "
2243 stringify(MAX_MIGRATE_DOWNTIME_SECONDS)" seconds");
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03002244 return;
2245 }
2246
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302247 value *= 1000; /* Convert to milliseconds */
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302248
Markus Armbruster1bda8b32017-07-18 13:42:11 +02002249 MigrateSetParameters p = {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302250 .has_downtime_limit = true,
Fangrui Song2667c982019-11-22 09:00:38 +01002251 .downtime_limit = (int64_t)value,
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302252 };
2253
2254 qmp_migrate_set_parameters(&p, errp);
aliguori5bb79102008-10-13 03:12:02 +00002255}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03002256
Pavel Butsykin53f09a12017-02-03 18:23:20 +03002257bool migrate_release_ram(void)
2258{
2259 MigrationState *s;
2260
2261 s = migrate_get_current();
2262
2263 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
2264}
2265
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00002266bool migrate_postcopy_ram(void)
2267{
2268 MigrationState *s;
2269
2270 s = migrate_get_current();
2271
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +00002272 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00002273}
2274
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002275bool migrate_postcopy(void)
2276{
Vladimir Sementsov-Ogievskiydd6bb912018-03-13 15:34:00 -04002277 return migrate_postcopy_ram() || migrate_dirty_bitmaps();
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002278}
2279
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06002280bool migrate_auto_converge(void)
2281{
2282 MigrationState *s;
2283
2284 s = migrate_get_current();
2285
2286 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
2287}
2288
Peter Lieven323004a2013-07-18 09:48:50 +02002289bool migrate_zero_blocks(void)
2290{
2291 MigrationState *s;
2292
2293 s = migrate_get_current();
2294
2295 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
2296}
2297
Alexey Perevalovf22f9282018-03-22 21:17:22 +03002298bool migrate_postcopy_blocktime(void)
2299{
2300 MigrationState *s;
2301
2302 s = migrate_get_current();
2303
2304 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME];
2305}
2306
Liang Li8706d2d2015-03-23 16:32:17 +08002307bool migrate_use_compression(void)
2308{
Liang Lidde4e692015-03-23 16:32:26 +08002309 MigrationState *s;
2310
2311 s = migrate_get_current();
2312
2313 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08002314}
2315
2316int migrate_compress_level(void)
2317{
2318 MigrationState *s;
2319
2320 s = migrate_get_current();
2321
Daniel P. Berrange2594f562016-04-27 11:05:14 +01002322 return s->parameters.compress_level;
Liang Li8706d2d2015-03-23 16:32:17 +08002323}
2324
2325int migrate_compress_threads(void)
2326{
2327 MigrationState *s;
2328
2329 s = migrate_get_current();
2330
Daniel P. Berrange2594f562016-04-27 11:05:14 +01002331 return s->parameters.compress_threads;
Liang Li8706d2d2015-03-23 16:32:17 +08002332}
2333
Xiao Guangrong1d588722018-08-21 16:10:20 +08002334int migrate_compress_wait_thread(void)
2335{
2336 MigrationState *s;
2337
2338 s = migrate_get_current();
2339
2340 return s->parameters.compress_wait_thread;
2341}
2342
Liang Li3fcb38c2015-03-23 16:32:18 +08002343int migrate_decompress_threads(void)
2344{
2345 MigrationState *s;
2346
2347 s = migrate_get_current();
2348
Daniel P. Berrange2594f562016-04-27 11:05:14 +01002349 return s->parameters.decompress_threads;
Liang Li3fcb38c2015-03-23 16:32:18 +08002350}
2351
Vladimir Sementsov-Ogievskiy55efc8c2018-03-13 15:34:00 -04002352bool migrate_dirty_bitmaps(void)
2353{
2354 MigrationState *s;
2355
2356 s = migrate_get_current();
2357
2358 return s->enabled_capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS];
2359}
2360
Yury Kotov18269062019-02-15 20:45:45 +03002361bool migrate_ignore_shared(void)
2362{
2363 MigrationState *s;
2364
2365 s = migrate_get_current();
2366
2367 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_IGNORE_SHARED];
2368}
2369
Yury Kotovb9d68df2019-09-03 19:22:44 +03002370bool migrate_validate_uuid(void)
2371{
2372 MigrationState *s;
2373
2374 s = migrate_get_current();
2375
2376 return s->enabled_capabilities[MIGRATION_CAPABILITY_VALIDATE_UUID];
2377}
2378
Juan Quintelab05dc722015-07-07 14:44:05 +02002379bool migrate_use_events(void)
2380{
2381 MigrationState *s;
2382
2383 s = migrate_get_current();
2384
2385 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
2386}
2387
Juan Quintela30126bb2016-01-14 12:23:00 +01002388bool migrate_use_multifd(void)
2389{
2390 MigrationState *s;
2391
2392 s = migrate_get_current();
2393
Juan Quintelacbfd6c92019-02-06 13:54:06 +01002394 return s->enabled_capabilities[MIGRATION_CAPABILITY_MULTIFD];
Juan Quintela30126bb2016-01-14 12:23:00 +01002395}
2396
Dr. David Alan Gilbert93fbd032017-10-20 10:05:50 +01002397bool migrate_pause_before_switchover(void)
2398{
2399 MigrationState *s;
2400
2401 s = migrate_get_current();
2402
2403 return s->enabled_capabilities[
2404 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER];
2405}
2406
Juan Quintela4075fb12016-01-15 08:56:17 +01002407int migrate_multifd_channels(void)
2408{
2409 MigrationState *s;
2410
2411 s = migrate_get_current();
2412
Juan Quintelacbfd6c92019-02-06 13:54:06 +01002413 return s->parameters.multifd_channels;
Juan Quintela4075fb12016-01-15 08:56:17 +01002414}
2415
Juan Quintelaab7cbb02019-05-15 13:37:46 +02002416MultiFDCompression migrate_multifd_compression(void)
2417{
2418 MigrationState *s;
2419
2420 s = migrate_get_current();
2421
2422 return s->parameters.multifd_compression;
2423}
2424
Juan Quintela9004db42020-01-23 17:08:52 +01002425int migrate_multifd_zlib_level(void)
2426{
2427 MigrationState *s;
2428
2429 s = migrate_get_current();
2430
2431 return s->parameters.multifd_zlib_level;
2432}
2433
Juan Quintela6a9ad152020-01-23 17:41:36 +01002434int migrate_multifd_zstd_level(void)
2435{
2436 MigrationState *s;
2437
2438 s = migrate_get_current();
2439
2440 return s->parameters.multifd_zstd_level;
2441}
2442
Orit Wasserman17ad9b32012-08-06 21:42:53 +03002443int migrate_use_xbzrle(void)
2444{
2445 MigrationState *s;
2446
2447 s = migrate_get_current();
2448
2449 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
2450}
2451
2452int64_t migrate_xbzrle_cache_size(void)
2453{
2454 MigrationState *s;
2455
2456 s = migrate_get_current();
2457
Juan Quintela73af8dd2017-10-05 21:30:10 +02002458 return s->parameters.xbzrle_cache_size;
Orit Wasserman17ad9b32012-08-06 21:42:53 +03002459}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002460
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01002461static int64_t migrate_max_postcopy_bandwidth(void)
2462{
2463 MigrationState *s;
2464
2465 s = migrate_get_current();
2466
2467 return s->parameters.max_postcopy_bandwidth;
2468}
2469
Juan Quintela2833c592017-04-05 18:32:37 +02002470bool migrate_use_block(void)
2471{
2472 MigrationState *s;
2473
2474 s = migrate_get_current();
2475
2476 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
2477}
2478
Peter Xuc788ada2017-06-26 18:28:55 +08002479bool migrate_use_return_path(void)
2480{
2481 MigrationState *s;
2482
2483 s = migrate_get_current();
2484
2485 return s->enabled_capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
2486}
2487
Juan Quintela2833c592017-04-05 18:32:37 +02002488bool migrate_use_block_incremental(void)
2489{
2490 MigrationState *s;
2491
2492 s = migrate_get_current();
2493
2494 return s->parameters.block_incremental;
2495}
2496
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002497/* migration thread support */
2498/*
2499 * Something bad happened to the RP stream, mark an error
2500 * The caller shall print or trace something to indicate why
2501 */
2502static void mark_source_rp_bad(MigrationState *s)
2503{
2504 s->rp_state.error = true;
2505}
2506
2507static struct rp_cmd_args {
2508 ssize_t len; /* -1 = variable */
2509 const char *name;
2510} rp_cmd_args[] = {
2511 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
2512 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
2513 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002514 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
2515 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Peter Xua335deb2018-05-02 18:47:28 +08002516 [MIG_RP_MSG_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" },
Peter Xu13955b82018-05-02 18:47:30 +08002517 [MIG_RP_MSG_RESUME_ACK] = { .len = 4, .name = "RESUME_ACK" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002518 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
2519};
2520
2521/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002522 * Process a request for pages received on the return path,
2523 * We're allowed to send more than requested (e.g. to round to our page size)
2524 * and we don't need to send pages that have already been sent.
2525 */
2526static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
2527 ram_addr_t start, size_t len)
2528{
Wei Yang038adc22019-10-13 10:11:45 +08002529 long our_host_ps = qemu_real_host_page_size;
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00002530
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002531 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00002532
2533 /*
2534 * Since we currently insist on matching page sizes, just sanity check
2535 * we're being asked for whole host pages.
2536 */
Bihong Yu395cb452020-10-20 11:10:43 +08002537 if (start & (our_host_ps - 1) ||
2538 (len & (our_host_ps - 1))) {
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00002539 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
2540 " len: %zd", __func__, start, len);
2541 mark_source_rp_bad(ms);
2542 return;
2543 }
2544
Juan Quintela96506892017-03-14 18:41:03 +01002545 if (ram_save_queue_pages(rbname, start, len)) {
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00002546 mark_source_rp_bad(ms);
2547 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002548}
2549
Peter Xu14b17422018-05-02 18:47:21 +08002550/* Return true to retry, false to quit */
2551static bool postcopy_pause_return_path_thread(MigrationState *s)
2552{
2553 trace_postcopy_pause_return_path();
2554
2555 qemu_sem_wait(&s->postcopy_pause_rp_sem);
2556
2557 trace_postcopy_pause_return_path_continued();
2558
2559 return true;
2560}
2561
Peter Xua335deb2018-05-02 18:47:28 +08002562static int migrate_handle_rp_recv_bitmap(MigrationState *s, char *block_name)
2563{
2564 RAMBlock *block = qemu_ram_block_by_name(block_name);
2565
2566 if (!block) {
2567 error_report("%s: invalid block name '%s'", __func__, block_name);
2568 return -EINVAL;
2569 }
2570
2571 /* Fetch the received bitmap and refresh the dirty bitmap */
2572 return ram_dirty_bitmap_reload(s, block);
2573}
2574
Peter Xu13955b82018-05-02 18:47:30 +08002575static int migrate_handle_rp_resume_ack(MigrationState *s, uint32_t value)
2576{
2577 trace_source_return_path_thread_resume_ack(value);
2578
2579 if (value != MIGRATION_RESUME_ACK_VALUE) {
2580 error_report("%s: illegal resume_ack value %"PRIu32,
2581 __func__, value);
2582 return -1;
2583 }
2584
2585 /* Now both sides are active. */
2586 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
2587 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2588
Peter Xu94190692018-05-02 18:47:34 +08002589 /* Notify send thread that time to continue send pages */
2590 qemu_sem_post(&s->rp_state.rp_sem);
Peter Xu13955b82018-05-02 18:47:30 +08002591
2592 return 0;
2593}
2594
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002595/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002596 * Handles messages sent on the return path towards the source VM
2597 *
2598 */
2599static void *source_return_path_thread(void *opaque)
2600{
2601 MigrationState *ms = opaque;
2602 QEMUFile *rp = ms->rp_state.from_dst_file;
2603 uint16_t header_len, header_type;
Peter Xu568b01c2016-03-09 14:12:12 +08002604 uint8_t buf[512];
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002605 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002606 ram_addr_t start = 0; /* =0 to silence warning */
2607 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002608 int res;
2609
2610 trace_source_return_path_thread_entry();
Lidong Chen74637e62018-08-06 21:29:29 +08002611 rcu_register_thread();
Peter Xu14b17422018-05-02 18:47:21 +08002612
2613retry:
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002614 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
2615 migration_is_setup_or_active(ms->state)) {
2616 trace_source_return_path_thread_loop_top();
2617 header_type = qemu_get_be16(rp);
2618 header_len = qemu_get_be16(rp);
2619
Peter Xu7a9ddfb2018-02-08 18:31:05 +08002620 if (qemu_file_get_error(rp)) {
2621 mark_source_rp_bad(ms);
2622 goto out;
2623 }
2624
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002625 if (header_type >= MIG_RP_MSG_MAX ||
2626 header_type == MIG_RP_MSG_INVALID) {
2627 error_report("RP: Received invalid message 0x%04x length 0x%04x",
Mao Zhongyied8b2822020-03-28 01:19:08 +08002628 header_type, header_len);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002629 mark_source_rp_bad(ms);
2630 goto out;
2631 }
2632
2633 if ((rp_cmd_args[header_type].len != -1 &&
2634 header_len != rp_cmd_args[header_type].len) ||
Peter Xu568b01c2016-03-09 14:12:12 +08002635 header_len > sizeof(buf)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002636 error_report("RP: Received '%s' message (0x%04x) with"
Mao Zhongyied8b2822020-03-28 01:19:08 +08002637 "incorrect length %d expecting %zu",
2638 rp_cmd_args[header_type].name, header_type, header_len,
2639 (size_t)rp_cmd_args[header_type].len);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002640 mark_source_rp_bad(ms);
2641 goto out;
2642 }
2643
2644 /* We know we've got a valid header by this point */
2645 res = qemu_get_buffer(rp, buf, header_len);
2646 if (res != header_len) {
2647 error_report("RP: Failed reading data for message 0x%04x"
2648 " read %d expected %d",
2649 header_type, res, header_len);
2650 mark_source_rp_bad(ms);
2651 goto out;
2652 }
2653
2654 /* OK, we have the message and the data */
2655 switch (header_type) {
2656 case MIG_RP_MSG_SHUT:
Peter Maydell4d885132016-06-10 17:09:22 +01002657 sibling_error = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002658 trace_source_return_path_thread_shut(sibling_error);
2659 if (sibling_error) {
2660 error_report("RP: Sibling indicated error %d", sibling_error);
2661 mark_source_rp_bad(ms);
2662 }
2663 /*
2664 * We'll let the main thread deal with closing the RP
2665 * we could do a shutdown(2) on it, but we're the only user
2666 * anyway, so there's nothing gained.
2667 */
2668 goto out;
2669
2670 case MIG_RP_MSG_PONG:
Peter Maydell4d885132016-06-10 17:09:22 +01002671 tmp32 = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002672 trace_source_return_path_thread_pong(tmp32);
2673 break;
2674
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002675 case MIG_RP_MSG_REQ_PAGES:
Peter Maydell4d885132016-06-10 17:09:22 +01002676 start = ldq_be_p(buf);
2677 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002678 migrate_handle_rp_req_pages(ms, NULL, start, len);
2679 break;
2680
2681 case MIG_RP_MSG_REQ_PAGES_ID:
2682 expected_len = 12 + 1; /* header + termination */
2683
2684 if (header_len >= expected_len) {
Peter Maydell4d885132016-06-10 17:09:22 +01002685 start = ldq_be_p(buf);
2686 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002687 /* Now we expect an idstr */
2688 tmp32 = buf[12]; /* Length of the following idstr */
2689 buf[13 + tmp32] = '\0';
2690 expected_len += tmp32;
2691 }
2692 if (header_len != expected_len) {
2693 error_report("RP: Req_Page_id with length %d expecting %zd",
Mao Zhongyied8b2822020-03-28 01:19:08 +08002694 header_len, expected_len);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002695 mark_source_rp_bad(ms);
2696 goto out;
2697 }
2698 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
2699 break;
2700
Peter Xua335deb2018-05-02 18:47:28 +08002701 case MIG_RP_MSG_RECV_BITMAP:
2702 if (header_len < 1) {
2703 error_report("%s: missing block name", __func__);
2704 mark_source_rp_bad(ms);
2705 goto out;
2706 }
2707 /* Format: len (1B) + idstr (<255B). This ends the idstr. */
2708 buf[buf[0] + 1] = '\0';
2709 if (migrate_handle_rp_recv_bitmap(ms, (char *)(buf + 1))) {
2710 mark_source_rp_bad(ms);
2711 goto out;
2712 }
2713 break;
2714
Peter Xu13955b82018-05-02 18:47:30 +08002715 case MIG_RP_MSG_RESUME_ACK:
2716 tmp32 = ldl_be_p(buf);
2717 if (migrate_handle_rp_resume_ack(ms, tmp32)) {
2718 mark_source_rp_bad(ms);
2719 goto out;
2720 }
2721 break;
2722
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002723 default:
2724 break;
2725 }
2726 }
Peter Xu14b17422018-05-02 18:47:21 +08002727
2728out:
2729 res = qemu_file_get_error(rp);
2730 if (res) {
Dr. David Alan Gilbertfb14a422019-10-07 11:35:07 +01002731 if (res == -EIO && migration_in_postcopy()) {
Peter Xu14b17422018-05-02 18:47:21 +08002732 /*
2733 * Maybe there is something we can do: it looks like a
2734 * network down issue, and we pause for a recovery.
2735 */
2736 if (postcopy_pause_return_path_thread(ms)) {
2737 /* Reload rp, reset the rest */
Marc-André Lureau0284a2a2018-09-25 13:22:45 +04002738 if (rp != ms->rp_state.from_dst_file) {
2739 qemu_fclose(rp);
2740 rp = ms->rp_state.from_dst_file;
2741 }
Peter Xu14b17422018-05-02 18:47:21 +08002742 ms->rp_state.error = false;
2743 goto retry;
2744 }
2745 }
2746
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002747 trace_source_return_path_thread_bad_end();
2748 mark_source_rp_bad(ms);
2749 }
2750
2751 trace_source_return_path_thread_end();
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002752 ms->rp_state.from_dst_file = NULL;
2753 qemu_fclose(rp);
Lidong Chen74637e62018-08-06 21:29:29 +08002754 rcu_unregister_thread();
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002755 return NULL;
2756}
2757
Peter Xud3e35b82018-05-02 18:47:24 +08002758static int open_return_path_on_source(MigrationState *ms,
2759 bool create_thread)
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002760{
2761
zhanghailiang89a02a92016-01-15 11:37:42 +08002762 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002763 if (!ms->rp_state.from_dst_file) {
2764 return -1;
2765 }
2766
2767 trace_open_return_path_on_source();
Peter Xud3e35b82018-05-02 18:47:24 +08002768
2769 if (!create_thread) {
2770 /* We're done */
2771 return 0;
2772 }
2773
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002774 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
2775 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
2776
2777 trace_open_return_path_on_source_continue();
2778
2779 return 0;
2780}
2781
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002782/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
2783static int await_return_path_close_on_source(MigrationState *ms)
2784{
2785 /*
2786 * If this is a normal exit then the destination will send a SHUT and the
2787 * rp_thread will exit, however if there's an error we need to cause
2788 * it to exit.
2789 */
zhanghailiang89a02a92016-01-15 11:37:42 +08002790 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002791 /*
2792 * shutdown(2), if we have it, will cause it to unblock if it's stuck
2793 * waiting for the destination.
2794 */
2795 qemu_file_shutdown(ms->rp_state.from_dst_file);
2796 mark_source_rp_bad(ms);
2797 }
2798 trace_await_return_path_close_on_source_joining();
2799 qemu_thread_join(&ms->rp_state.rp_thread);
2800 trace_await_return_path_close_on_source_close();
2801 return ms->rp_state.error;
2802}
2803
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002804/*
2805 * Switch from normal iteration to postcopy
2806 * Returns non-0 on error
2807 */
Peter Xu7287cbd2018-01-03 20:20:09 +08002808static int postcopy_start(MigrationState *ms)
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002809{
2810 int ret;
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002811 QIOChannelBuffer *bioc;
2812 QEMUFile *fb;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002813 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01002814 int64_t bandwidth = migrate_max_postcopy_bandwidth();
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002815 bool restart_block = false;
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002816 int cur_state = MIGRATION_STATUS_ACTIVE;
2817 if (!migrate_pause_before_switchover()) {
2818 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
2819 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2820 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002821
2822 trace_postcopy_start();
2823 qemu_mutex_lock_iothread();
2824 trace_postcopy_start_set_run();
2825
Daniel Henrique Barbozafb064112018-12-05 17:47:01 -02002826 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002827 global_state_store();
2828 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01002829 if (ret < 0) {
2830 goto fail;
2831 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002832
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002833 ret = migration_maybe_pause(ms, &cur_state,
2834 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2835 if (ret < 0) {
2836 goto fail;
2837 }
2838
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01002839 ret = bdrv_inactivate_all();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002840 if (ret < 0) {
2841 goto fail;
2842 }
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002843 restart_block = true;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002844
2845 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00002846 * Cause any non-postcopiable, but iterative devices to
2847 * send out their final data.
2848 */
Fam Zhenga1fbe752017-06-17 00:06:58 +08002849 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00002850
2851 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002852 * in Finish migrate and with the io-lock held everything should
2853 * be quiet, but we've potentially still got dirty pages and we
2854 * need to tell the destination to throw any pages it's already received
2855 * that are dirty
2856 */
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002857 if (migrate_postcopy_ram()) {
2858 if (ram_postcopy_send_discard_bitmap(ms)) {
2859 error_report("postcopy send discard bitmap failed");
2860 goto fail;
2861 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002862 }
2863
2864 /*
2865 * send rest of state - note things that are doing postcopy
2866 * will notice we're in POSTCOPY_ACTIVE and not actually
2867 * wrap their state up here
2868 */
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01002869 /* 0 max-postcopy-bandwidth means unlimited */
2870 if (!bandwidth) {
2871 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
2872 } else {
2873 qemu_file_set_rate_limit(ms->to_dst_file, bandwidth / XFER_LIMIT_RATIO);
2874 }
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002875 if (migrate_postcopy_ram()) {
2876 /* Ping just for debugging, helps line traces up */
2877 qemu_savevm_send_ping(ms->to_dst_file, 2);
2878 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002879
2880 /*
2881 * While loading the device state we may trigger page transfer
2882 * requests and the fd must be free to process those, and thus
2883 * the destination must read the whole device state off the fd before
2884 * it starts processing it. Unfortunately the ad-hoc migration format
2885 * doesn't allow the destination to know the size to read without fully
2886 * parsing it through each devices load-state code (especially the open
2887 * coded devices that use get/put).
2888 * So we wrap the device state up in a package with a length at the start;
2889 * to do this we use a qemu_buf to hold the whole of the device state.
2890 */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002891 bioc = qio_channel_buffer_new(4096);
Daniel P. Berrange6f01f132016-09-30 11:57:14 +01002892 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002893 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
2894 object_unref(OBJECT(bioc));
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002895
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00002896 /*
2897 * Make sure the receiver can get incoming pages before we send the rest
2898 * of the state
2899 */
2900 qemu_savevm_send_postcopy_listen(fb);
2901
Fam Zhenga1fbe752017-06-17 00:06:58 +08002902 qemu_savevm_state_complete_precopy(fb, false, false);
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002903 if (migrate_postcopy_ram()) {
2904 qemu_savevm_send_ping(fb, 3);
2905 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002906
2907 qemu_savevm_send_postcopy_run(fb);
2908
2909 /* <><> end of stuff going into the package */
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002910
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002911 /* Last point of recovery; as soon as we send the package the destination
2912 * can open devices and potentially start running.
2913 * Lets just check again we've not got any errors.
2914 */
2915 ret = qemu_file_get_error(ms->to_dst_file);
2916 if (ret) {
2917 error_report("postcopy_start: Migration stream errored (pre package)");
2918 goto fail_closefb;
2919 }
2920
2921 restart_block = false;
2922
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002923 /* Now send that blob */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002924 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002925 goto fail_closefb;
2926 }
2927 qemu_fclose(fb);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00002928
2929 /* Send a notify to give a chance for anything that needs to happen
2930 * at the transition to postcopy and after the device state; in particular
2931 * spice needs to trigger a transition now
2932 */
2933 ms->postcopy_after_devices = true;
2934 notifier_list_notify(&migration_state_notifiers, ms);
2935
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002936 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
2937
2938 qemu_mutex_unlock_iothread();
2939
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002940 if (migrate_postcopy_ram()) {
2941 /*
2942 * Although this ping is just for debug, it could potentially be
2943 * used for getting a better measurement of downtime at the source.
2944 */
2945 qemu_savevm_send_ping(ms->to_dst_file, 4);
2946 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002947
Pavel Butsykinced1c612017-02-03 18:23:21 +03002948 if (migrate_release_ram()) {
2949 ram_postcopy_migrated_memory_release(ms);
2950 }
2951
zhanghailiang89a02a92016-01-15 11:37:42 +08002952 ret = qemu_file_get_error(ms->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002953 if (ret) {
2954 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00002955 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002956 MIGRATION_STATUS_FAILED);
2957 }
2958
2959 return ret;
2960
2961fail_closefb:
2962 qemu_fclose(fb);
2963fail:
zhanghailiang48781e52015-12-16 11:47:33 +00002964 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002965 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002966 if (restart_block) {
2967 /* A failure happened early enough that we know the destination hasn't
2968 * accessed block devices, so we're safe to recover.
2969 */
2970 Error *local_err = NULL;
2971
2972 bdrv_invalidate_cache_all(&local_err);
2973 if (local_err) {
2974 error_report_err(local_err);
2975 }
2976 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002977 qemu_mutex_unlock_iothread();
2978 return -1;
2979}
2980
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002981/**
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002982 * migration_maybe_pause: Pause if required to by
2983 * migrate_pause_before_switchover called with the iothread locked
2984 * Returns: 0 on success
2985 */
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002986static int migration_maybe_pause(MigrationState *s,
2987 int *current_active_state,
2988 int new_state)
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002989{
2990 if (!migrate_pause_before_switchover()) {
2991 return 0;
2992 }
2993
2994 /* Since leaving this state is not atomic with posting the semaphore
2995 * it's possible that someone could have issued multiple migrate_continue
2996 * and the semaphore is incorrectly positive at this point;
2997 * the docs say it's undefined to reinit a semaphore that's already
2998 * init'd, so use timedwait to eat up any existing posts.
2999 */
3000 while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) {
3001 /* This block intentionally left blank */
3002 }
3003
Zhimin Feng89583382020-01-14 17:43:09 +08003004 /*
3005 * If the migration is cancelled when it is in the completion phase,
3006 * the migration state is set to MIGRATION_STATUS_CANCELLING.
3007 * So we don't need to wait a semaphore, otherwise we would always
3008 * wait for the 'pause_sem' semaphore.
3009 */
3010 if (s->state != MIGRATION_STATUS_CANCELLING) {
3011 qemu_mutex_unlock_iothread();
3012 migrate_set_state(&s->state, *current_active_state,
3013 MIGRATION_STATUS_PRE_SWITCHOVER);
3014 qemu_sem_wait(&s->pause_sem);
3015 migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER,
3016 new_state);
3017 *current_active_state = new_state;
3018 qemu_mutex_lock_iothread();
3019 }
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01003020
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01003021 return s->state == new_state ? 0 : -EINVAL;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01003022}
3023
3024/**
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003025 * migration_completion: Used by migration_thread when there's not much left.
3026 * The caller 'breaks' the loop when this returns.
3027 *
3028 * @s: Current migration state
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003029 */
Peter Xu2ad87302018-01-03 20:20:14 +08003030static void migration_completion(MigrationState *s)
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003031{
3032 int ret;
Peter Xu2ad87302018-01-03 20:20:14 +08003033 int current_active_state = s->state;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003034
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003035 if (s->state == MIGRATION_STATUS_ACTIVE) {
3036 qemu_mutex_lock_iothread();
Peter Xu64909f92018-01-03 20:20:10 +08003037 s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Daniel Henrique Barbozafb064112018-12-05 17:47:01 -02003038 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
Peter Xu7287cbd2018-01-03 20:20:09 +08003039 s->vm_was_running = runstate_is_running();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003040 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003041
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003042 if (!ret) {
Fam Zhenga1fbe752017-06-17 00:06:58 +08003043 bool inactivate = !migrate_colo_enabled();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003044 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02003045 if (ret >= 0) {
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01003046 ret = migration_maybe_pause(s, &current_active_state,
3047 MIGRATION_STATUS_DEVICE);
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01003048 }
3049 if (ret >= 0) {
Kevin Wolff07fa4c2017-05-22 17:10:38 +02003050 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
Fam Zhenga1fbe752017-06-17 00:06:58 +08003051 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
3052 inactivate);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02003053 }
Fam Zhenga1fbe752017-06-17 00:06:58 +08003054 if (inactivate && ret >= 0) {
3055 s->block_inactive = true;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003056 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003057 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003058 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003059
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003060 if (ret < 0) {
3061 goto fail;
3062 }
3063 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
3064 trace_migration_completion_postcopy_end();
3065
zhanghailiang89a02a92016-01-15 11:37:42 +08003066 qemu_savevm_state_complete_postcopy(s->to_dst_file);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003067 trace_migration_completion_postcopy_end_after_complete();
Longpeng(Mike)6ba11212020-11-05 17:17:26 +08003068 } else if (s->state == MIGRATION_STATUS_CANCELLING) {
3069 goto fail;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003070 }
3071
3072 /*
3073 * If rp was opened we must clean up the thread before
3074 * cleaning everything else up (since if there are no failures
3075 * it will wait for the destination to send it's status in
3076 * a SHUT command).
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003077 */
Peter Xu0425dc92017-05-31 18:35:34 +08003078 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003079 int rp_error;
Peter Xu0425dc92017-05-31 18:35:34 +08003080 trace_migration_return_path_end_before();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003081 rp_error = await_return_path_close_on_source(s);
Peter Xu0425dc92017-05-31 18:35:34 +08003082 trace_migration_return_path_end_after(rp_error);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003083 if (rp_error) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02003084 goto fail_invalidate;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003085 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003086 }
3087
zhanghailiang89a02a92016-01-15 11:37:42 +08003088 if (qemu_file_get_error(s->to_dst_file)) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003089 trace_migration_completion_file_err();
Greg Kurzfe904ea2016-05-18 15:44:36 +02003090 goto fail_invalidate;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003091 }
3092
zhanghailiang0b827d52016-10-27 14:42:54 +08003093 if (!migrate_colo_enabled()) {
3094 migrate_set_state(&s->state, current_active_state,
3095 MIGRATION_STATUS_COMPLETED);
3096 }
3097
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003098 return;
3099
Greg Kurzfe904ea2016-05-18 15:44:36 +02003100fail_invalidate:
3101 /* If not doing postcopy, vm_start() will be called: let's regain
3102 * control on images.
3103 */
Dr. David Alan Gilbert6039dd52018-02-05 09:13:37 +00003104 if (s->state == MIGRATION_STATUS_ACTIVE ||
3105 s->state == MIGRATION_STATUS_DEVICE) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02003106 Error *local_err = NULL;
3107
zhanghailiang1d2acc32017-01-24 15:59:52 +08003108 qemu_mutex_lock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02003109 bdrv_invalidate_cache_all(&local_err);
3110 if (local_err) {
3111 error_report_err(local_err);
zhanghailiang1d2acc32017-01-24 15:59:52 +08003112 } else {
3113 s->block_inactive = false;
Greg Kurzfe904ea2016-05-18 15:44:36 +02003114 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08003115 qemu_mutex_unlock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02003116 }
3117
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003118fail:
zhanghailiang48781e52015-12-16 11:47:33 +00003119 migrate_set_state(&s->state, current_active_state,
3120 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003121}
3122
zhanghailiang35a6ed42016-10-27 14:42:52 +08003123bool migrate_colo_enabled(void)
3124{
3125 MigrationState *s = migrate_get_current();
3126 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
3127}
3128
Peter Xub23c2ad2018-05-02 18:47:19 +08003129typedef enum MigThrError {
3130 /* No error detected */
3131 MIG_THR_ERR_NONE = 0,
3132 /* Detected error, but resumed successfully */
3133 MIG_THR_ERR_RECOVERED = 1,
3134 /* Detected fatal error, need to exit */
3135 MIG_THR_ERR_FATAL = 2,
3136} MigThrError;
3137
Peter Xu94190692018-05-02 18:47:34 +08003138static int postcopy_resume_handshake(MigrationState *s)
3139{
3140 qemu_savevm_send_postcopy_resume(s->to_dst_file);
3141
3142 while (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
3143 qemu_sem_wait(&s->rp_state.rp_sem);
3144 }
3145
3146 if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
3147 return 0;
3148 }
3149
3150 return -1;
3151}
3152
Peter Xu135b87b2018-05-02 18:47:25 +08003153/* Return zero if success, or <0 for error */
3154static int postcopy_do_resume(MigrationState *s)
3155{
Peter Xud1b8ead2018-05-02 18:47:31 +08003156 int ret;
3157
3158 /*
3159 * Call all the resume_prepare() hooks, so that modules can be
3160 * ready for the migration resume.
3161 */
3162 ret = qemu_savevm_state_resume_prepare(s);
3163 if (ret) {
3164 error_report("%s: resume_prepare() failure detected: %d",
3165 __func__, ret);
3166 return ret;
3167 }
3168
3169 /*
Peter Xu94190692018-05-02 18:47:34 +08003170 * Last handshake with destination on the resume (destination will
3171 * switch to postcopy-active afterwards)
Peter Xud1b8ead2018-05-02 18:47:31 +08003172 */
Peter Xu94190692018-05-02 18:47:34 +08003173 ret = postcopy_resume_handshake(s);
3174 if (ret) {
3175 error_report("%s: handshake failed: %d", __func__, ret);
3176 return ret;
3177 }
Peter Xud1b8ead2018-05-02 18:47:31 +08003178
Peter Xu135b87b2018-05-02 18:47:25 +08003179 return 0;
3180}
3181
Peter Xub23c2ad2018-05-02 18:47:19 +08003182/*
3183 * We don't return until we are in a safe state to continue current
3184 * postcopy migration. Returns MIG_THR_ERR_RECOVERED if recovered, or
3185 * MIG_THR_ERR_FATAL if unrecovery failure happened.
3186 */
3187static MigThrError postcopy_pause(MigrationState *s)
3188{
3189 assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
Peter Xub23c2ad2018-05-02 18:47:19 +08003190
Peter Xu135b87b2018-05-02 18:47:25 +08003191 while (true) {
Peter Xu62df0662018-05-02 18:47:38 +08003192 QEMUFile *file;
3193
Peter Xu135b87b2018-05-02 18:47:25 +08003194 /* Current channel is possibly broken. Release it. */
3195 assert(s->to_dst_file);
Peter Xu62df0662018-05-02 18:47:38 +08003196 qemu_mutex_lock(&s->qemu_file_lock);
3197 file = s->to_dst_file;
Peter Xu135b87b2018-05-02 18:47:25 +08003198 s->to_dst_file = NULL;
Peter Xu62df0662018-05-02 18:47:38 +08003199 qemu_mutex_unlock(&s->qemu_file_lock);
3200
3201 qemu_file_shutdown(file);
3202 qemu_fclose(file);
Peter Xub23c2ad2018-05-02 18:47:19 +08003203
Peter Xud246ea52020-10-21 17:27:20 -04003204 migrate_set_state(&s->state, s->state,
3205 MIGRATION_STATUS_POSTCOPY_PAUSED);
3206
Peter Xu135b87b2018-05-02 18:47:25 +08003207 error_report("Detected IO failure for postcopy. "
3208 "Migration paused.");
3209
3210 /*
3211 * We wait until things fixed up. Then someone will setup the
3212 * status back for us.
3213 */
3214 while (s->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
3215 qemu_sem_wait(&s->postcopy_pause_sem);
3216 }
3217
3218 if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
3219 /* Woken up by a recover procedure. Give it a shot */
3220
3221 /*
3222 * Firstly, let's wake up the return path now, with a new
3223 * return path channel.
3224 */
3225 qemu_sem_post(&s->postcopy_pause_rp_sem);
3226
3227 /* Do the resume logic */
3228 if (postcopy_do_resume(s) == 0) {
3229 /* Let's continue! */
3230 trace_postcopy_pause_continued();
3231 return MIG_THR_ERR_RECOVERED;
3232 } else {
3233 /*
3234 * Something wrong happened during the recovery, let's
3235 * pause again. Pause is always better than throwing
3236 * data away.
3237 */
3238 continue;
3239 }
3240 } else {
3241 /* This is not right... Time to quit. */
3242 return MIG_THR_ERR_FATAL;
3243 }
Peter Xub23c2ad2018-05-02 18:47:19 +08003244 }
Peter Xub23c2ad2018-05-02 18:47:19 +08003245}
3246
3247static MigThrError migration_detect_error(MigrationState *s)
3248{
3249 int ret;
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003250 int state = s->state;
Yury Kotov3d661c82019-04-22 13:34:20 +03003251 Error *local_error = NULL;
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003252
3253 if (state == MIGRATION_STATUS_CANCELLING ||
3254 state == MIGRATION_STATUS_CANCELLED) {
3255 /* End the migration, but don't set the state to failed */
3256 return MIG_THR_ERR_FATAL;
3257 }
Peter Xub23c2ad2018-05-02 18:47:19 +08003258
3259 /* Try to detect any file errors */
Yury Kotov3d661c82019-04-22 13:34:20 +03003260 ret = qemu_file_get_error_obj(s->to_dst_file, &local_error);
Peter Xub23c2ad2018-05-02 18:47:19 +08003261 if (!ret) {
3262 /* Everything is fine */
Yury Kotov3d661c82019-04-22 13:34:20 +03003263 assert(!local_error);
Peter Xub23c2ad2018-05-02 18:47:19 +08003264 return MIG_THR_ERR_NONE;
3265 }
3266
Yury Kotov3d661c82019-04-22 13:34:20 +03003267 if (local_error) {
3268 migrate_set_error(s, local_error);
3269 error_free(local_error);
3270 }
3271
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003272 if (state == MIGRATION_STATUS_POSTCOPY_ACTIVE && ret == -EIO) {
Peter Xub23c2ad2018-05-02 18:47:19 +08003273 /*
3274 * For postcopy, we allow the network to be down for a
3275 * while. After that, it can be continued by a
3276 * recovery phase.
3277 */
3278 return postcopy_pause(s);
3279 } else {
3280 /*
3281 * For precopy (or postcopy with error outside IO), we fail
3282 * with no time.
3283 */
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003284 migrate_set_state(&s->state, state, MIGRATION_STATUS_FAILED);
Peter Xub23c2ad2018-05-02 18:47:19 +08003285 trace_migration_thread_file_err();
3286
3287 /* Time to stop the migration, now. */
3288 return MIG_THR_ERR_FATAL;
3289 }
3290}
3291
Wei Yang268dcd42019-09-12 10:49:57 +08003292/* How many bytes have we transferred since the beginning of the migration */
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003293static uint64_t migration_total_bytes(MigrationState *s)
3294{
Juan Quintelab9ee2f72016-01-15 11:40:13 +01003295 return qemu_ftell(s->to_dst_file) + ram_counters.multifd_bytes;
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003296}
3297
Peter Xucf011f02018-01-03 20:20:11 +08003298static void migration_calculate_complete(MigrationState *s)
3299{
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003300 uint64_t bytes = migration_total_bytes(s);
Peter Xucf011f02018-01-03 20:20:11 +08003301 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela6cde6fb2018-06-26 15:26:35 +02003302 int64_t transfer_time;
Peter Xucf011f02018-01-03 20:20:11 +08003303
3304 s->total_time = end_time - s->start_time;
3305 if (!s->downtime) {
3306 /*
3307 * It's still not set, so we are precopy migration. For
3308 * postcopy, downtime is calculated during postcopy_start().
3309 */
3310 s->downtime = end_time - s->downtime_start;
3311 }
3312
Juan Quintela6cde6fb2018-06-26 15:26:35 +02003313 transfer_time = s->total_time - s->setup_time;
3314 if (transfer_time) {
3315 s->mbps = ((double) bytes * 8.0) / transfer_time / 1000;
Peter Xucf011f02018-01-03 20:20:11 +08003316 }
3317}
3318
Ivan Ren87f3bd82019-08-02 18:18:41 +08003319static void update_iteration_initial_status(MigrationState *s)
3320{
3321 /*
3322 * Update these three fields at the same time to avoid mismatch info lead
3323 * wrong speed calculation.
3324 */
3325 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3326 s->iteration_initial_bytes = migration_total_bytes(s);
3327 s->iteration_initial_pages = ram_get_total_transferred_pages();
3328}
3329
Peter Xub15df1a2018-01-03 20:20:13 +08003330static void migration_update_counters(MigrationState *s,
3331 int64_t current_time)
3332{
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08003333 uint64_t transferred, transferred_pages, time_spent;
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003334 uint64_t current_bytes; /* bytes transferred since the beginning */
Peter Xub15df1a2018-01-03 20:20:13 +08003335 double bandwidth;
3336
3337 if (current_time < s->iteration_start_time + BUFFER_DELAY) {
3338 return;
3339 }
3340
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003341 current_bytes = migration_total_bytes(s);
3342 transferred = current_bytes - s->iteration_initial_bytes;
Peter Xub15df1a2018-01-03 20:20:13 +08003343 time_spent = current_time - s->iteration_start_time;
3344 bandwidth = (double)transferred / time_spent;
Wei Wang0781c1e2018-01-22 19:36:39 +08003345 s->threshold_size = bandwidth * s->parameters.downtime_limit;
Peter Xub15df1a2018-01-03 20:20:13 +08003346
3347 s->mbps = (((double) transferred * 8.0) /
3348 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
3349
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08003350 transferred_pages = ram_get_total_transferred_pages() -
3351 s->iteration_initial_pages;
3352 s->pages_per_second = (double) transferred_pages /
3353 (((double) time_spent / 1000.0));
3354
Peter Xub15df1a2018-01-03 20:20:13 +08003355 /*
3356 * if we haven't sent anything, we don't want to
3357 * recalculate. 10000 is a small enough number for our purposes
3358 */
3359 if (ram_counters.dirty_pages_rate && transferred > 10000) {
Balamuruhan S650af892018-06-12 14:20:09 +05303360 s->expected_downtime = ram_counters.remaining / bandwidth;
Peter Xub15df1a2018-01-03 20:20:13 +08003361 }
3362
3363 qemu_file_reset_rate_limit(s->to_dst_file);
3364
Ivan Ren87f3bd82019-08-02 18:18:41 +08003365 update_iteration_initial_status(s);
Peter Xub15df1a2018-01-03 20:20:13 +08003366
3367 trace_migrate_transferred(transferred, time_spent,
Wei Wang0781c1e2018-01-22 19:36:39 +08003368 bandwidth, s->threshold_size);
Peter Xub15df1a2018-01-03 20:20:13 +08003369}
3370
Peter Xu2ad87302018-01-03 20:20:14 +08003371/* Migration thread iteration status */
3372typedef enum {
3373 MIG_ITERATE_RESUME, /* Resume current iteration */
3374 MIG_ITERATE_SKIP, /* Skip current iteration */
3375 MIG_ITERATE_BREAK, /* Break the loop */
3376} MigIterateState;
3377
3378/*
3379 * Return true if continue to the next iteration directly, false
3380 * otherwise.
3381 */
3382static MigIterateState migration_iteration_run(MigrationState *s)
3383{
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04003384 uint64_t pending_size, pend_pre, pend_compat, pend_post;
Peter Xu2ad87302018-01-03 20:20:14 +08003385 bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE;
3386
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04003387 qemu_savevm_state_pending(s->to_dst_file, s->threshold_size, &pend_pre,
3388 &pend_compat, &pend_post);
3389 pending_size = pend_pre + pend_compat + pend_post;
Peter Xu2ad87302018-01-03 20:20:14 +08003390
3391 trace_migrate_pending(pending_size, s->threshold_size,
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04003392 pend_pre, pend_compat, pend_post);
Peter Xu2ad87302018-01-03 20:20:14 +08003393
3394 if (pending_size && pending_size >= s->threshold_size) {
3395 /* Still a significant amount to transfer */
Wei Yang52aec702019-07-18 16:37:47 +08003396 if (!in_postcopy && pend_pre <= s->threshold_size &&
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01003397 qatomic_read(&s->start_postcopy)) {
Peter Xu2ad87302018-01-03 20:20:14 +08003398 if (postcopy_start(s)) {
3399 error_report("%s: postcopy failed to start", __func__);
3400 }
3401 return MIG_ITERATE_SKIP;
3402 }
3403 /* Just another iteration step */
Wei Yang17d93512019-10-06 06:05:16 +08003404 qemu_savevm_state_iterate(s->to_dst_file, in_postcopy);
Peter Xu2ad87302018-01-03 20:20:14 +08003405 } else {
3406 trace_migration_thread_low_pending(pending_size);
3407 migration_completion(s);
3408 return MIG_ITERATE_BREAK;
3409 }
3410
3411 return MIG_ITERATE_RESUME;
3412}
3413
Peter Xu199aa6d2018-01-03 20:20:15 +08003414static void migration_iteration_finish(MigrationState *s)
3415{
3416 /* If we enabled cpu throttling for auto-converge, turn it off. */
3417 cpu_throttle_stop();
3418
3419 qemu_mutex_lock_iothread();
3420 switch (s->state) {
3421 case MIGRATION_STATUS_COMPLETED:
3422 migration_calculate_complete(s);
3423 runstate_set(RUN_STATE_POSTMIGRATE);
3424 break;
3425
3426 case MIGRATION_STATUS_ACTIVE:
3427 /*
3428 * We should really assert here, but since it's during
3429 * migration, let's try to reduce the usage of assertions.
3430 */
3431 if (!migrate_colo_enabled()) {
3432 error_report("%s: critical error: calling COLO code without "
3433 "COLO enabled", __func__);
3434 }
3435 migrate_start_colo_process(s);
3436 /*
3437 * Fixme: we will run VM in COLO no matter its old running state.
3438 * After exited COLO, we will keep running.
3439 */
3440 s->vm_was_running = true;
3441 /* Fallthrough */
3442 case MIGRATION_STATUS_FAILED:
3443 case MIGRATION_STATUS_CANCELLED:
Dr. David Alan Gilbert57225e52018-07-19 10:22:57 +01003444 case MIGRATION_STATUS_CANCELLING:
Peter Xu199aa6d2018-01-03 20:20:15 +08003445 if (s->vm_was_running) {
3446 vm_start();
3447 } else {
3448 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
3449 runstate_set(RUN_STATE_POSTMIGRATE);
3450 }
3451 }
3452 break;
3453
3454 default:
3455 /* Should not reach here, but if so, forgive the VM. */
3456 error_report("%s: Unknown ending state %d", __func__, s->state);
3457 break;
3458 }
Yury Kotovfd392cf2019-04-08 14:33:43 +03003459 migrate_fd_cleanup_schedule(s);
Peter Xu199aa6d2018-01-03 20:20:15 +08003460 qemu_mutex_unlock_iothread();
3461}
3462
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003463void migration_make_urgent_request(void)
3464{
3465 qemu_sem_post(&migrate_get_current()->rate_limit_sem);
3466}
3467
3468void migration_consume_urgent_request(void)
3469{
3470 qemu_sem_wait(&migrate_get_current()->rate_limit_sem);
3471}
3472
Dr. David Alan Gilbert97e1e062019-12-05 10:29:18 +00003473/* Returns true if the rate limiting was broken by an urgent request */
3474bool migration_rate_limit(void)
3475{
3476 int64_t now = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3477 MigrationState *s = migrate_get_current();
3478
3479 bool urgent = false;
3480 migration_update_counters(s, now);
3481 if (qemu_file_rate_limit(s->to_dst_file)) {
Lukas Straub77386122020-05-20 22:42:32 +02003482
3483 if (qemu_file_get_error(s->to_dst_file)) {
3484 return false;
3485 }
Dr. David Alan Gilbert97e1e062019-12-05 10:29:18 +00003486 /*
3487 * Wait for a delay to do rate limiting OR
3488 * something urgent to post the semaphore.
3489 */
3490 int ms = s->iteration_start_time + BUFFER_DELAY - now;
3491 trace_migration_rate_limit_pre(ms);
3492 if (qemu_sem_timedwait(&s->rate_limit_sem, ms) == 0) {
3493 /*
3494 * We were woken by one or more urgent things but
3495 * the timedwait will have consumed one of them.
3496 * The service routine for the urgent wake will dec
3497 * the semaphore itself for each item it consumes,
3498 * so add this one we just eat back.
3499 */
3500 qemu_sem_post(&s->rate_limit_sem);
3501 urgent = true;
3502 }
3503 trace_migration_rate_limit_post(urgent);
3504 }
3505 return urgent;
3506}
3507
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00003508/*
3509 * Master migration thread on the source VM.
3510 * It drives the migration and pumps the data down the outgoing channel.
3511 */
Juan Quintela5f496a12013-02-22 17:36:30 +01003512static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003513{
Juan Quintela9848a402012-12-19 09:55:50 +01003514 MigrationState *s = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +01003515 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Peter Xub23c2ad2018-05-02 18:47:19 +08003516 MigThrError thr_error;
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003517 bool urgent = false;
Juan Quintela76f59332012-10-03 20:16:24 +02003518
Paolo Bonziniab28bd22015-07-09 08:55:38 +02003519 rcu_register_thread();
3520
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +00003521 object_ref(OBJECT(s));
Ivan Ren87f3bd82019-08-02 18:18:41 +08003522 update_iteration_initial_status(s);
Peter Xub15df1a2018-01-03 20:20:13 +08003523
zhanghailiang89a02a92016-01-15 11:37:42 +08003524 qemu_savevm_state_header(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003525
Peter Xu62a02652017-06-14 15:55:58 +08003526 /*
3527 * If we opened the return path, we need to make sure dst has it
3528 * opened as well.
3529 */
3530 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003531 /* Now tell the dest that it should open its end so it can reply */
zhanghailiang89a02a92016-01-15 11:37:42 +08003532 qemu_savevm_send_open_return_path(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003533
3534 /* And do a ping that will make stuff easier to debug */
zhanghailiang89a02a92016-01-15 11:37:42 +08003535 qemu_savevm_send_ping(s->to_dst_file, 1);
Peter Xu0425dc92017-05-31 18:35:34 +08003536 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003537
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03003538 if (migrate_postcopy()) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003539 /*
3540 * Tell the destination that we *might* want to do postcopy later;
3541 * if the other end can't do postcopy it should fail now, nice and
3542 * early.
3543 */
zhanghailiang89a02a92016-01-15 11:37:42 +08003544 qemu_savevm_send_postcopy_advise(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003545 }
3546
Zhang Chenaad555c2018-09-03 12:38:47 +08003547 if (migrate_colo_enabled()) {
3548 /* Notify migration destination that we enable COLO */
3549 qemu_savevm_send_colo_enable(s->to_dst_file);
3550 }
3551
Juan Quintela9907e842017-06-28 11:52:24 +02003552 qemu_savevm_state_setup(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003553
Keqian Zhud05de9e2020-02-04 13:08:41 +08003554 if (qemu_savevm_state_guest_unplug_pending()) {
Jens Freimannc7e0acd2019-10-29 12:49:02 +01003555 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3556 MIGRATION_STATUS_WAIT_UNPLUG);
3557
3558 while (s->state == MIGRATION_STATUS_WAIT_UNPLUG &&
3559 qemu_savevm_state_guest_unplug_pending()) {
3560 qemu_sem_timedwait(&s->wait_unplug_sem, 250);
3561 }
3562
3563 migrate_set_state(&s->state, MIGRATION_STATUS_WAIT_UNPLUG,
3564 MIGRATION_STATUS_ACTIVE);
3565 }
3566
Alex Blighbc72ad62013-08-21 16:03:08 +01003567 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang48781e52015-12-16 11:47:33 +00003568 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3569 MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04003570
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00003571 trace_migration_thread_setup_complete();
3572
Wei Yang8f8d5282019-07-17 08:53:41 +08003573 while (migration_is_active(s)) {
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003574 if (urgent || !qemu_file_rate_limit(s->to_dst_file)) {
Peter Xu2ad87302018-01-03 20:20:14 +08003575 MigIterateState iter_state = migration_iteration_run(s);
3576 if (iter_state == MIG_ITERATE_SKIP) {
3577 continue;
3578 } else if (iter_state == MIG_ITERATE_BREAK) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003579 break;
Juan Quintelac369f402012-10-03 20:33:34 +02003580 }
3581 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01003582
Peter Xub23c2ad2018-05-02 18:47:19 +08003583 /*
3584 * Try to detect any kind of failures, and see whether we
3585 * should stop the migration now.
3586 */
3587 thr_error = migration_detect_error(s);
3588 if (thr_error == MIG_THR_ERR_FATAL) {
3589 /* Stop migration */
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01003590 break;
Peter Xub23c2ad2018-05-02 18:47:19 +08003591 } else if (thr_error == MIG_THR_ERR_RECOVERED) {
3592 /*
3593 * Just recovered from a e.g. network failure, reset all
3594 * the local variables. This is important to avoid
3595 * breaking transferred_bytes and bandwidth calculation
3596 */
Ivan Ren87f3bd82019-08-02 18:18:41 +08003597 update_iteration_initial_status(s);
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01003598 }
Peter Xub15df1a2018-01-03 20:20:13 +08003599
Dr. David Alan Gilbert97e1e062019-12-05 10:29:18 +00003600 urgent = migration_rate_limit();
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01003601 }
3602
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003603 trace_migration_thread_after_loop();
Peter Xu199aa6d2018-01-03 20:20:15 +08003604 migration_iteration_finish(s);
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +00003605 object_unref(OBJECT(s));
Paolo Bonziniab28bd22015-07-09 08:55:38 +02003606 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003607 return NULL;
3608}
3609
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00003610void migrate_fd_connect(MigrationState *s, Error *error_in)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003611{
Juan Quintela00f4b572019-06-12 11:33:27 +02003612 Error *local_err = NULL;
Peter Xud3e35b82018-05-02 18:47:24 +08003613 int64_t rate_limit;
3614 bool resume = s->state == MIGRATION_STATUS_POSTCOPY_PAUSED;
3615
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05303616 s->expected_downtime = s->parameters.downtime_limit;
Marc-André Lureau9cbc3642020-03-25 19:47:21 +01003617 if (resume) {
3618 assert(s->cleanup_bh);
3619 } else {
3620 assert(!s->cleanup_bh);
3621 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup_bh, s);
3622 }
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00003623 if (error_in) {
3624 migrate_fd_error(s, error_in);
3625 migrate_fd_cleanup(s);
3626 return;
3627 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003628
Peter Xud3e35b82018-05-02 18:47:24 +08003629 if (resume) {
3630 /* This is a resumed migration */
Peter Xu8504dde2019-09-06 21:01:03 +08003631 rate_limit = s->parameters.max_postcopy_bandwidth /
3632 XFER_LIMIT_RATIO;
Peter Xud3e35b82018-05-02 18:47:24 +08003633 } else {
3634 /* This is a fresh new migration */
3635 rate_limit = s->parameters.max_bandwidth / XFER_LIMIT_RATIO;
Paolo Bonzini442773c2013-02-22 17:36:44 +01003636
Peter Xud3e35b82018-05-02 18:47:24 +08003637 /* Notify before starting migration thread */
3638 notifier_list_notify(&migration_state_notifiers, s);
3639 }
3640
3641 qemu_file_set_rate_limit(s->to_dst_file, rate_limit);
3642 qemu_file_set_blocking(s->to_dst_file, true);
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02003643
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003644 /*
Peter Xuc788ada2017-06-26 18:28:55 +08003645 * Open the return path. For postcopy, it is used exclusively. For
3646 * precopy, only if user specified "return-path" capability would
3647 * QEMU uses the return path.
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003648 */
Peter Xuc788ada2017-06-26 18:28:55 +08003649 if (migrate_postcopy_ram() || migrate_use_return_path()) {
Peter Xud3e35b82018-05-02 18:47:24 +08003650 if (open_return_path_on_source(s, !resume)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003651 error_report("Unable to open return-path for postcopy");
Peter Xud3e35b82018-05-02 18:47:24 +08003652 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003653 migrate_fd_cleanup(s);
3654 return;
3655 }
3656 }
3657
Peter Xud3e35b82018-05-02 18:47:24 +08003658 if (resume) {
Peter Xu135b87b2018-05-02 18:47:25 +08003659 /* Wakeup the main migration thread to do the recovery */
3660 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
3661 MIGRATION_STATUS_POSTCOPY_RECOVER);
3662 qemu_sem_post(&s->postcopy_pause_sem);
Peter Xud3e35b82018-05-02 18:47:24 +08003663 return;
3664 }
3665
Juan Quintela00f4b572019-06-12 11:33:27 +02003666 if (multifd_save_setup(&local_err) != 0) {
3667 error_report_err(local_err);
Juan Quintelaf986c3d2016-01-14 16:52:55 +01003668 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3669 MIGRATION_STATUS_FAILED);
3670 migrate_fd_cleanup(s);
3671 return;
3672 }
Pankaj Gupta009fad72017-01-23 19:12:56 +05303673 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01003674 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003675 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003676}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00003677
Peter Xu9d18af92017-06-27 12:10:19 +08003678void migration_global_dump(Monitor *mon)
3679{
3680 MigrationState *ms = migrate_get_current();
3681
Juan Quintela6f0f6422017-10-26 11:49:57 +02003682 monitor_printf(mon, "globals:\n");
3683 monitor_printf(mon, "store-global-state: %s\n",
3684 ms->store_global_state ? "on" : "off");
3685 monitor_printf(mon, "only-migratable: %s\n",
Markus Armbruster811f8652019-04-01 11:08:24 +02003686 only_migratable ? "on" : "off");
Juan Quintela6f0f6422017-10-26 11:49:57 +02003687 monitor_printf(mon, "send-configuration: %s\n",
3688 ms->send_configuration ? "on" : "off");
3689 monitor_printf(mon, "send-section-footer: %s\n",
3690 ms->send_section_footer ? "on" : "off");
Xiao Guangrongf5482222018-05-03 16:06:11 +08003691 monitor_printf(mon, "decompress-error-check: %s\n",
3692 ms->decompress_error_check ? "on" : "off");
Peter Xu002cad62019-06-03 14:50:56 +08003693 monitor_printf(mon, "clear-bitmap-shift: %u\n",
3694 ms->clear_bitmap_shift);
Peter Xu9d18af92017-06-27 12:10:19 +08003695}
3696
Peter Xu20814752017-07-18 11:39:03 +08003697#define DEFINE_PROP_MIG_CAP(name, x) \
3698 DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
3699
Peter Xu52722982017-06-27 12:10:14 +08003700static Property migration_properties[] = {
3701 DEFINE_PROP_BOOL("store-global-state", MigrationState,
3702 store_global_state, true),
Peter Xu71dd4c12017-06-27 12:10:16 +08003703 DEFINE_PROP_BOOL("send-configuration", MigrationState,
3704 send_configuration, true),
Peter Xu15c38502017-06-27 12:10:17 +08003705 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
3706 send_section_footer, true),
Xiao Guangrongf5482222018-05-03 16:06:11 +08003707 DEFINE_PROP_BOOL("decompress-error-check", MigrationState,
3708 decompress_error_check, true),
Peter Xu002cad62019-06-03 14:50:56 +08003709 DEFINE_PROP_UINT8("x-clear-bitmap-shift", MigrationState,
3710 clear_bitmap_shift, CLEAR_BITMAP_SHIFT_DEFAULT),
Peter Xu89632fa2017-07-18 11:39:02 +08003711
3712 /* Migration parameters */
Juan Quintela741d4082017-12-01 13:08:38 +01003713 DEFINE_PROP_UINT8("x-compress-level", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003714 parameters.compress_level,
3715 DEFAULT_MIGRATE_COMPRESS_LEVEL),
Juan Quintela741d4082017-12-01 13:08:38 +01003716 DEFINE_PROP_UINT8("x-compress-threads", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003717 parameters.compress_threads,
3718 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
Xiao Guangrong1d588722018-08-21 16:10:20 +08003719 DEFINE_PROP_BOOL("x-compress-wait-thread", MigrationState,
3720 parameters.compress_wait_thread, true),
Juan Quintela741d4082017-12-01 13:08:38 +01003721 DEFINE_PROP_UINT8("x-decompress-threads", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003722 parameters.decompress_threads,
3723 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
Keqian Zhudc14a472020-02-24 10:31:42 +08003724 DEFINE_PROP_UINT8("x-throttle-trigger-threshold", MigrationState,
3725 parameters.throttle_trigger_threshold,
3726 DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD),
Juan Quintela741d4082017-12-01 13:08:38 +01003727 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003728 parameters.cpu_throttle_initial,
3729 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
Juan Quintela741d4082017-12-01 13:08:38 +01003730 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003731 parameters.cpu_throttle_increment,
3732 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
Keqian Zhucbbf8182020-04-13 18:15:08 +08003733 DEFINE_PROP_BOOL("x-cpu-throttle-tailslow", MigrationState,
3734 parameters.cpu_throttle_tailslow, false),
Juan Quintela741d4082017-12-01 13:08:38 +01003735 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003736 parameters.max_bandwidth, MAX_THROTTLE),
Juan Quintela741d4082017-12-01 13:08:38 +01003737 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003738 parameters.downtime_limit,
3739 DEFAULT_MIGRATE_SET_DOWNTIME),
Juan Quintela741d4082017-12-01 13:08:38 +01003740 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003741 parameters.x_checkpoint_delay,
3742 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
Juan Quintelacbfd6c92019-02-06 13:54:06 +01003743 DEFINE_PROP_UINT8("multifd-channels", MigrationState,
3744 parameters.multifd_channels,
Juan Quintela4075fb12016-01-15 08:56:17 +01003745 DEFAULT_MIGRATE_MULTIFD_CHANNELS),
Juan Quintela96eef042019-01-16 10:35:55 +01003746 DEFINE_PROP_MULTIFD_COMPRESSION("multifd-compression", MigrationState,
3747 parameters.multifd_compression,
3748 DEFAULT_MIGRATE_MULTIFD_COMPRESSION),
Juan Quintela9004db42020-01-23 17:08:52 +01003749 DEFINE_PROP_UINT8("multifd-zlib-level", MigrationState,
3750 parameters.multifd_zlib_level,
3751 DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL),
Juan Quintela6a9ad152020-01-23 17:41:36 +01003752 DEFINE_PROP_UINT8("multifd-zstd-level", MigrationState,
3753 parameters.multifd_zstd_level,
3754 DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL),
Juan Quintela73af8dd2017-10-05 21:30:10 +02003755 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
3756 parameters.xbzrle_cache_size,
3757 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01003758 DEFINE_PROP_SIZE("max-postcopy-bandwidth", MigrationState,
3759 parameters.max_postcopy_bandwidth,
3760 DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH),
Li Qiang4cbc9c72018-08-01 06:00:20 -07003761 DEFINE_PROP_UINT8("max-cpu-throttle", MigrationState,
3762 parameters.max_cpu_throttle,
3763 DEFAULT_MIGRATE_MAX_CPU_THROTTLE),
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00003764 DEFINE_PROP_SIZE("announce-initial", MigrationState,
3765 parameters.announce_initial,
3766 DEFAULT_MIGRATE_ANNOUNCE_INITIAL),
3767 DEFINE_PROP_SIZE("announce-max", MigrationState,
3768 parameters.announce_max,
3769 DEFAULT_MIGRATE_ANNOUNCE_MAX),
3770 DEFINE_PROP_SIZE("announce-rounds", MigrationState,
3771 parameters.announce_rounds,
3772 DEFAULT_MIGRATE_ANNOUNCE_ROUNDS),
3773 DEFINE_PROP_SIZE("announce-step", MigrationState,
3774 parameters.announce_step,
3775 DEFAULT_MIGRATE_ANNOUNCE_STEP),
Peter Xu20814752017-07-18 11:39:03 +08003776
3777 /* Migration capabilities */
3778 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
3779 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
3780 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
3781 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
3782 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
3783 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
3784 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
3785 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
3786 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
3787 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
3788 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
Juan Quintelacbfd6c92019-02-06 13:54:06 +01003789 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_MULTIFD),
Peter Xu20814752017-07-18 11:39:03 +08003790
Peter Xu52722982017-06-27 12:10:14 +08003791 DEFINE_PROP_END_OF_LIST(),
3792};
3793
Peter Xue5cb7e72017-06-27 12:10:13 +08003794static void migration_class_init(ObjectClass *klass, void *data)
3795{
3796 DeviceClass *dc = DEVICE_CLASS(klass);
3797
3798 dc->user_creatable = false;
Marc-André Lureau4f67d302020-01-10 19:30:32 +04003799 device_class_set_props(dc, migration_properties);
Peter Xue5cb7e72017-06-27 12:10:13 +08003800}
3801
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003802static void migration_instance_finalize(Object *obj)
3803{
3804 MigrationState *ms = MIGRATION_OBJ(obj);
3805 MigrationParameters *params = &ms->parameters;
3806
Juan Quintela87db1a72017-09-05 12:50:22 +02003807 qemu_mutex_destroy(&ms->error_mutex);
Peter Xu62df0662018-05-02 18:47:38 +08003808 qemu_mutex_destroy(&ms->qemu_file_lock);
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003809 g_free(params->tls_hostname);
3810 g_free(params->tls_creds);
Jens Freimannc7e0acd2019-10-29 12:49:02 +01003811 qemu_sem_destroy(&ms->wait_unplug_sem);
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003812 qemu_sem_destroy(&ms->rate_limit_sem);
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01003813 qemu_sem_destroy(&ms->pause_sem);
Peter Xub23c2ad2018-05-02 18:47:19 +08003814 qemu_sem_destroy(&ms->postcopy_pause_sem);
Peter Xu14b17422018-05-02 18:47:21 +08003815 qemu_sem_destroy(&ms->postcopy_pause_rp_sem);
Peter Xuedd090c2018-05-02 18:47:32 +08003816 qemu_sem_destroy(&ms->rp_state.rp_sem);
Marc-André Lureauab105cc2018-03-06 18:09:59 +01003817 error_free(ms->error);
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003818}
3819
Peter Xue5cb7e72017-06-27 12:10:13 +08003820static void migration_instance_init(Object *obj)
3821{
3822 MigrationState *ms = MIGRATION_OBJ(obj);
Peter Xu8b0b29d2017-07-18 11:39:06 +08003823 MigrationParameters *params = &ms->parameters;
Peter Xue5cb7e72017-06-27 12:10:13 +08003824
3825 ms->state = MIGRATION_STATUS_NONE;
Peter Xue5cb7e72017-06-27 12:10:13 +08003826 ms->mbps = -1;
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08003827 ms->pages_per_second = -1;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01003828 qemu_sem_init(&ms->pause_sem, 0);
Juan Quintela87db1a72017-09-05 12:50:22 +02003829 qemu_mutex_init(&ms->error_mutex);
Peter Xu8b0b29d2017-07-18 11:39:06 +08003830
3831 params->tls_hostname = g_strdup("");
3832 params->tls_creds = g_strdup("");
3833
3834 /* Set has_* up only for parameter checks */
3835 params->has_compress_level = true;
3836 params->has_compress_threads = true;
3837 params->has_decompress_threads = true;
Keqian Zhudc14a472020-02-24 10:31:42 +08003838 params->has_throttle_trigger_threshold = true;
Peter Xu8b0b29d2017-07-18 11:39:06 +08003839 params->has_cpu_throttle_initial = true;
3840 params->has_cpu_throttle_increment = true;
Keqian Zhucbbf8182020-04-13 18:15:08 +08003841 params->has_cpu_throttle_tailslow = true;
Peter Xu8b0b29d2017-07-18 11:39:06 +08003842 params->has_max_bandwidth = true;
3843 params->has_downtime_limit = true;
3844 params->has_x_checkpoint_delay = true;
3845 params->has_block_incremental = true;
Juan Quintelacbfd6c92019-02-06 13:54:06 +01003846 params->has_multifd_channels = true;
Juan Quintela96eef042019-01-16 10:35:55 +01003847 params->has_multifd_compression = true;
Juan Quintela9004db42020-01-23 17:08:52 +01003848 params->has_multifd_zlib_level = true;
Juan Quintela6a9ad152020-01-23 17:41:36 +01003849 params->has_multifd_zstd_level = true;
Juan Quintela73af8dd2017-10-05 21:30:10 +02003850 params->has_xbzrle_cache_size = true;
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01003851 params->has_max_postcopy_bandwidth = true;
Li Qiang4cbc9c72018-08-01 06:00:20 -07003852 params->has_max_cpu_throttle = true;
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00003853 params->has_announce_initial = true;
3854 params->has_announce_max = true;
3855 params->has_announce_rounds = true;
3856 params->has_announce_step = true;
Peter Xub23c2ad2018-05-02 18:47:19 +08003857
3858 qemu_sem_init(&ms->postcopy_pause_sem, 0);
Peter Xu14b17422018-05-02 18:47:21 +08003859 qemu_sem_init(&ms->postcopy_pause_rp_sem, 0);
Peter Xuedd090c2018-05-02 18:47:32 +08003860 qemu_sem_init(&ms->rp_state.rp_sem, 0);
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003861 qemu_sem_init(&ms->rate_limit_sem, 0);
Jens Freimannc7e0acd2019-10-29 12:49:02 +01003862 qemu_sem_init(&ms->wait_unplug_sem, 0);
Peter Xu62df0662018-05-02 18:47:38 +08003863 qemu_mutex_init(&ms->qemu_file_lock);
Peter Xu8b0b29d2017-07-18 11:39:06 +08003864}
3865
3866/*
3867 * Return true if check pass, false otherwise. Error will be put
3868 * inside errp if provided.
3869 */
3870static bool migration_object_check(MigrationState *ms, Error **errp)
3871{
Peter Xu6b19a7d2017-07-18 11:39:10 +08003872 MigrationCapabilityStatusList *head = NULL;
3873 /* Assuming all off */
3874 bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret;
3875 int i;
3876
Peter Xu8b0b29d2017-07-18 11:39:06 +08003877 if (!migrate_params_check(&ms->parameters, errp)) {
3878 return false;
3879 }
3880
Peter Xu6b19a7d2017-07-18 11:39:10 +08003881 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
3882 if (ms->enabled_capabilities[i]) {
3883 head = migrate_cap_add(head, i, true);
3884 }
3885 }
3886
3887 ret = migrate_caps_check(cap_list, head, errp);
3888
3889 /* It works with head == NULL */
3890 qapi_free_MigrationCapabilityStatusList(head);
3891
3892 return ret;
Peter Xue5cb7e72017-06-27 12:10:13 +08003893}
3894
3895static const TypeInfo migration_type = {
3896 .name = TYPE_MIGRATION,
Peter Xu01f6e142017-06-28 15:15:44 +08003897 /*
Peter Xuc8d3ff32017-07-05 16:21:23 +08003898 * NOTE: TYPE_MIGRATION is not really a device, as the object is
Markus Armbruster2194abd2020-06-10 07:32:19 +02003899 * not created using qdev_new(), it is not attached to the qdev
Peter Xuc8d3ff32017-07-05 16:21:23 +08003900 * device tree, and it is never realized.
3901 *
3902 * TODO: Make this TYPE_OBJECT once QOM provides something like
3903 * TYPE_DEVICE's "-global" properties.
Peter Xu01f6e142017-06-28 15:15:44 +08003904 */
Peter Xue5cb7e72017-06-27 12:10:13 +08003905 .parent = TYPE_DEVICE,
3906 .class_init = migration_class_init,
3907 .class_size = sizeof(MigrationClass),
3908 .instance_size = sizeof(MigrationState),
3909 .instance_init = migration_instance_init,
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003910 .instance_finalize = migration_instance_finalize,
Peter Xue5cb7e72017-06-27 12:10:13 +08003911};
3912
3913static void register_migration_types(void)
3914{
3915 type_register_static(&migration_type);
3916}
3917
3918type_init(register_migration_types);