blob: 3f4191a9c746901cc0e6c48cd525018e87ad39c8 [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
Juan Quintelada6f1792017-04-24 17:37:14 +0200121/* Messages sent on the return path from destination to source */
122enum mig_rp_message_type {
123 MIG_RP_MSG_INVALID = 0, /* Must be 0 */
124 MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */
125 MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */
126
127 MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */
128 MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */
Peter Xua335deb2018-05-02 18:47:28 +0800129 MIG_RP_MSG_RECV_BITMAP, /* send recved_bitmap back to source */
Peter Xu13955b82018-05-02 18:47:30 +0800130 MIG_RP_MSG_RESUME_ACK, /* tell source that we are ready to resume */
Juan Quintelada6f1792017-04-24 17:37:14 +0200131
132 MIG_RP_MSG_MAX
133};
134
Juan Quintela17549e82011-10-05 13:50:43 +0200135/* When we add fault tolerance, we could have several
136 migrations at once. For now we don't need to add
137 dynamic creation of migration */
138
Peter Xue5cb7e72017-06-27 12:10:13 +0800139static MigrationState *current_migration;
Peter Xue1b1b1b2018-05-02 18:47:35 +0800140static MigrationIncomingState *current_incoming;
Peter Xue5cb7e72017-06-27 12:10:13 +0800141
Peter Xu8b0b29d2017-07-18 11:39:06 +0800142static bool migration_object_check(MigrationState *ms, Error **errp);
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +0100143static int migration_maybe_pause(MigrationState *s,
144 int *current_active_state,
145 int new_state);
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +0000146static void migrate_fd_cancel(MigrationState *s);
Peter Xu8b0b29d2017-07-18 11:39:06 +0800147
Peter Xu8f8bfff2020-10-21 17:27:18 -0400148static gint page_request_addr_cmp(gconstpointer ap, gconstpointer bp)
149{
150 uintptr_t a = (uintptr_t) ap, b = (uintptr_t) bp;
151
152 return (a > b) - (a < b);
153}
154
Peter Xue5cb7e72017-06-27 12:10:13 +0800155void migration_object_init(void)
156{
Peter Xu8b0b29d2017-07-18 11:39:06 +0800157 Error *err = NULL;
Peter Xu4ffdb332017-06-27 12:10:18 +0800158
Peter Xue5cb7e72017-06-27 12:10:13 +0800159 /* This can only be called once. */
160 assert(!current_migration);
161 current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION));
Peter Xu4ffdb332017-06-27 12:10:18 +0800162
Peter Xue1b1b1b2018-05-02 18:47:35 +0800163 /*
164 * Init the migrate incoming object as well no matter whether
165 * we'll use it or not.
166 */
167 assert(!current_incoming);
168 current_incoming = g_new0(MigrationIncomingState, 1);
169 current_incoming->state = MIGRATION_STATUS_NONE;
170 current_incoming->postcopy_remote_fds =
171 g_array_new(FALSE, TRUE, sizeof(struct PostCopyFD));
172 qemu_mutex_init(&current_incoming->rp_mutex);
173 qemu_event_init(&current_incoming->main_thread_load_event, false);
174 qemu_sem_init(&current_incoming->postcopy_pause_sem_dst, 0);
175 qemu_sem_init(&current_incoming->postcopy_pause_sem_fault, 0);
Peter Xu8f8bfff2020-10-21 17:27:18 -0400176 qemu_mutex_init(&current_incoming->page_request_mutex);
177 current_incoming->page_requested = g_tree_new(page_request_addr_cmp);
Peter Xue1b1b1b2018-05-02 18:47:35 +0800178
Peter Xu8b0b29d2017-07-18 11:39:06 +0800179 if (!migration_object_check(current_migration, &err)) {
180 error_report_err(err);
181 exit(1);
182 }
Paolo Bonzinie0d17df2020-10-21 05:25:58 -0400183
184 blk_mig_init();
185 ram_mig_init();
186 dirty_bitmap_mig_init();
Peter Xue5cb7e72017-06-27 12:10:13 +0800187}
188
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +0000189void migration_shutdown(void)
Vladimir Sementsov-Ogievskiy1f895602017-12-28 12:16:16 +0300190{
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +0000191 /*
192 * Cancel the current migration - that will (eventually)
193 * stop the migration using this structure
194 */
195 migrate_fd_cancel(current_migration);
Vladimir Sementsov-Ogievskiy1f895602017-12-28 12:16:16 +0300196 object_unref(OBJECT(current_migration));
Vladimir Sementsov-Ogievskiy1499ab02020-07-27 22:42:31 +0300197
198 /*
199 * Cancel outgoing migration of dirty bitmaps. It should
200 * at least unref used block nodes.
201 */
202 dirty_bitmap_mig_cancel_outgoing();
203
204 /*
205 * Cancel incoming migration of dirty bitmaps. Dirty bitmaps
206 * are non-critical data, and their loss never considered as
207 * something serious.
208 */
209 dirty_bitmap_mig_cancel_incoming();
Vladimir Sementsov-Ogievskiy1f895602017-12-28 12:16:16 +0300210}
211
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100212/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +0200213MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +0200214{
Peter Xue5cb7e72017-06-27 12:10:13 +0800215 /* This can only be called after the object created. */
216 assert(current_migration);
217 return current_migration;
Juan Quintela17549e82011-10-05 13:50:43 +0200218}
219
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100220MigrationIncomingState *migration_incoming_get_current(void)
221{
Peter Xue1b1b1b2018-05-02 18:47:35 +0800222 assert(current_incoming);
223 return current_incoming;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100224}
225
226void migration_incoming_state_destroy(void)
227{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100228 struct MigrationIncomingState *mis = migration_incoming_get_current();
229
Peter Xu34826552017-05-19 14:43:29 +0800230 if (mis->to_src_file) {
Peter Xu660819b2017-05-19 14:43:30 +0800231 /* Tell source that we are done */
232 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
Peter Xu34826552017-05-19 14:43:29 +0800233 qemu_fclose(mis->to_src_file);
234 mis->to_src_file = NULL;
235 }
236
Peter Xu660819b2017-05-19 14:43:30 +0800237 if (mis->from_src_file) {
238 qemu_fclose(mis->from_src_file);
239 mis->from_src_file = NULL;
240 }
Dr. David Alan Gilbert00fa4fc2018-03-12 17:21:04 +0000241 if (mis->postcopy_remote_fds) {
242 g_array_free(mis->postcopy_remote_fds, TRUE);
243 mis->postcopy_remote_fds = NULL;
244 }
Peter Xu660819b2017-05-19 14:43:30 +0800245
Dr. David Alan Gilbert1783c002020-09-28 15:48:30 +0100246 qemu_event_reset(&mis->main_thread_load_event);
247
Peter Xu8f8bfff2020-10-21 17:27:18 -0400248 if (mis->page_requested) {
249 g_tree_destroy(mis->page_requested);
250 mis->page_requested = NULL;
251 }
252
Juan Quintela9aca82b2019-02-27 11:51:27 +0100253 if (mis->socket_address_list) {
254 qapi_free_SocketAddressList(mis->socket_address_list);
255 mis->socket_address_list = NULL;
256 }
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100257}
258
Juan Quintelab05dc722015-07-07 14:44:05 +0200259static void migrate_generate_event(int new_state)
260{
261 if (migrate_use_events()) {
Peter Xu3ab72382018-08-15 21:37:37 +0800262 qapi_event_send_migration(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200263 }
264}
265
Dr. David Alan Gilbert0f073f42018-04-16 18:09:30 +0100266static bool migrate_late_block_activate(void)
267{
268 MigrationState *s;
269
270 s = migrate_get_current();
271
272 return s->enabled_capabilities[
273 MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE];
274}
275
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000276/*
Juan Quintelada6f1792017-04-24 17:37:14 +0200277 * Send a message on the return channel back to the source
278 * of the migration.
279 */
Peter Xud6208e32018-02-08 18:31:12 +0800280static int migrate_send_rp_message(MigrationIncomingState *mis,
281 enum mig_rp_message_type message_type,
282 uint16_t len, void *data)
Juan Quintelada6f1792017-04-24 17:37:14 +0200283{
Peter Xud6208e32018-02-08 18:31:12 +0800284 int ret = 0;
285
Juan Quintelada6f1792017-04-24 17:37:14 +0200286 trace_migrate_send_rp_message((int)message_type, len);
287 qemu_mutex_lock(&mis->rp_mutex);
Peter Xud6208e32018-02-08 18:31:12 +0800288
289 /*
290 * It's possible that the file handle got lost due to network
291 * failures.
292 */
293 if (!mis->to_src_file) {
294 ret = -EIO;
295 goto error;
296 }
297
Juan Quintelada6f1792017-04-24 17:37:14 +0200298 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
299 qemu_put_be16(mis->to_src_file, len);
300 qemu_put_buffer(mis->to_src_file, data, len);
301 qemu_fflush(mis->to_src_file);
Peter Xud6208e32018-02-08 18:31:12 +0800302
303 /* It's possible that qemu file got error during sending */
304 ret = qemu_file_get_error(mis->to_src_file);
305
306error:
Juan Quintelada6f1792017-04-24 17:37:14 +0200307 qemu_mutex_unlock(&mis->rp_mutex);
Peter Xud6208e32018-02-08 18:31:12 +0800308 return ret;
Juan Quintelada6f1792017-04-24 17:37:14 +0200309}
310
Peter Xu2e2bce12020-09-08 16:30:18 -0400311/* Request one page from the source VM at the given start address.
312 * rb: the RAMBlock to request the page in
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000313 * Start: Address offset within the RB
314 * Len: Length in bytes required - must be a multiple of pagesize
315 */
Peter Xu7a267fc2020-10-21 17:27:17 -0400316int migrate_send_rp_message_req_pages(MigrationIncomingState *mis,
317 RAMBlock *rb, ram_addr_t start)
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000318{
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100319 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000320 size_t msglen = 12; /* start + len */
Peter Xu2e2bce12020-09-08 16:30:18 -0400321 size_t len = qemu_ram_pagesize(rb);
Peter Xud6208e32018-02-08 18:31:12 +0800322 enum mig_rp_message_type msg_type;
Peter Xu2e2bce12020-09-08 16:30:18 -0400323 const char *rbname;
324 int rbname_len;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000325
326 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
327 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
328
Peter Xu2e2bce12020-09-08 16:30:18 -0400329 /*
330 * We maintain the last ramblock that we requested for page. Note that we
331 * don't need locking because this function will only be called within the
332 * postcopy ram fault thread.
333 */
334 if (rb != mis->last_rb) {
335 mis->last_rb = rb;
336
337 rbname = qemu_ram_get_idstr(rb);
338 rbname_len = strlen(rbname);
339
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000340 assert(rbname_len < 256);
341
342 bufc[msglen++] = rbname_len;
343 memcpy(bufc + msglen, rbname, rbname_len);
344 msglen += rbname_len;
Peter Xud6208e32018-02-08 18:31:12 +0800345 msg_type = MIG_RP_MSG_REQ_PAGES_ID;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000346 } else {
Peter Xud6208e32018-02-08 18:31:12 +0800347 msg_type = MIG_RP_MSG_REQ_PAGES;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000348 }
Peter Xud6208e32018-02-08 18:31:12 +0800349
350 return migrate_send_rp_message(mis, msg_type, msglen, bufc);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000351}
352
Peter Xu7a267fc2020-10-21 17:27:17 -0400353int migrate_send_rp_req_pages(MigrationIncomingState *mis,
Peter Xu8f8bfff2020-10-21 17:27:18 -0400354 RAMBlock *rb, ram_addr_t start, uint64_t haddr)
Peter Xu7a267fc2020-10-21 17:27:17 -0400355{
Peter Xu8f8bfff2020-10-21 17:27:18 -0400356 void *aligned = (void *)(uintptr_t)(haddr & (-qemu_ram_pagesize(rb)));
Chen Quna2429282020-11-11 22:22:03 +0800357 bool received = false;
Peter Xu8f8bfff2020-10-21 17:27:18 -0400358
359 WITH_QEMU_LOCK_GUARD(&mis->page_request_mutex) {
360 received = ramblock_recv_bitmap_test_byte_offset(rb, start);
361 if (!received && !g_tree_lookup(mis->page_requested, aligned)) {
362 /*
363 * The page has not been received, and it's not yet in the page
364 * request list. Queue it. Set the value of element to 1, so that
365 * things like g_tree_lookup() will return TRUE (1) when found.
366 */
367 g_tree_insert(mis->page_requested, aligned, (gpointer)1);
368 mis->page_requested_count++;
369 trace_postcopy_page_req_add(aligned, mis->page_requested_count);
370 }
371 }
372
373 /*
374 * If the page is there, skip sending the message. We don't even need the
375 * lock because as long as the page arrived, it'll be there forever.
376 */
377 if (received) {
378 return 0;
379 }
380
Peter Xu7a267fc2020-10-21 17:27:17 -0400381 return migrate_send_rp_message_req_pages(mis, rb, start);
382}
383
Zhang Chenaad555c2018-09-03 12:38:47 +0800384static bool migration_colo_enabled;
385bool migration_incoming_colo_enabled(void)
386{
387 return migration_colo_enabled;
388}
389
390void migration_incoming_disable_colo(void)
391{
David Hildenbrand18b1d3c2020-06-26 09:22:36 +0200392 ram_block_discard_disable(false);
Zhang Chenaad555c2018-09-03 12:38:47 +0800393 migration_colo_enabled = false;
394}
395
David Hildenbrand18b1d3c2020-06-26 09:22:36 +0200396int migration_incoming_enable_colo(void)
Zhang Chenaad555c2018-09-03 12:38:47 +0800397{
David Hildenbrand18b1d3c2020-06-26 09:22:36 +0200398 if (ram_block_discard_disable(true)) {
399 error_report("COLO: cannot disable RAM discard");
400 return -EBUSY;
401 }
Zhang Chenaad555c2018-09-03 12:38:47 +0800402 migration_colo_enabled = true;
David Hildenbrand18b1d3c2020-06-26 09:22:36 +0200403 return 0;
Zhang Chenaad555c2018-09-03 12:38:47 +0800404}
405
Juan Quintela9aca82b2019-02-27 11:51:27 +0100406void migrate_add_address(SocketAddress *address)
407{
408 MigrationIncomingState *mis = migration_incoming_get_current();
Juan Quintela9aca82b2019-02-27 11:51:27 +0100409
Eric Blake54aa3de2020-11-12 19:13:37 -0600410 QAPI_LIST_PREPEND(mis->socket_address_list,
411 QAPI_CLONE(SocketAddress, address));
Juan Quintela9aca82b2019-02-27 11:51:27 +0100412}
413
Paolo Bonzinie69d50d2020-10-27 04:22:57 -0400414static void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000415{
Longpeng(Mike)d658f652020-08-06 15:40:29 +0800416 const char *p = NULL;
aliguori34c9dd82008-10-13 03:14:31 +0000417
Peter Xu3ab72382018-08-15 21:37:37 +0800418 qapi_event_send_migration(MIGRATION_STATUS_SETUP);
Paolo Bonzinie69d50d2020-10-27 04:22:57 -0400419 if (strstart(uri, "tcp:", &p) ||
420 strstart(uri, "unix:", NULL) ||
421 strstart(uri, "vsock:", NULL)) {
Longpeng(Mike)d658f652020-08-06 15:40:29 +0800422 socket_start_incoming_migration(p ? p : uri, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400423#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000424 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400425 rdma_start_incoming_migration(p, errp);
426#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000427 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200428 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000429 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200430 fd_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000431 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100432 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200433 }
aliguori5bb79102008-10-13 03:12:02 +0000434}
435
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300436static void process_incoming_migration_bh(void *opaque)
437{
438 Error *local_err = NULL;
439 MigrationIncomingState *mis = opaque;
440
Dr. David Alan Gilbert0f073f42018-04-16 18:09:30 +0100441 /* If capability late_block_activate is set:
442 * Only fire up the block code now if we're going to restart the
443 * VM, else 'cont' will do it.
444 * This causes file locking to happen; so we don't want it to happen
445 * unless we really are starting the VM.
446 */
447 if (!migrate_late_block_activate() ||
448 (autostart && (!global_state_received() ||
449 global_state_get_runstate() == RUN_STATE_RUNNING))) {
450 /* Make sure all file formats flush their mutable metadata.
451 * If we get an error here, just don't restart the VM yet. */
452 bdrv_invalidate_cache_all(&local_err);
453 if (local_err) {
454 error_report_err(local_err);
455 local_err = NULL;
456 autostart = false;
457 }
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200458 }
459
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300460 /*
461 * This must happen after all error conditions are dealt with and
462 * we're sure the VM is going to be running on this host.
463 */
Dr. David Alan Gilbert76595052019-02-27 13:24:08 +0000464 qemu_announce_self(&mis->announce_timer, migrate_announce_params());
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300465
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100466 if (multifd_load_cleanup(&local_err) != 0) {
467 error_report_err(local_err);
468 autostart = false;
469 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300470 /* If global state section was not received or we are in running
471 state, we need to obey autostart. Any other state is set with
472 runstate_set. */
473
Vladimir Sementsov-Ogievskiyb35ebdf2018-03-13 15:34:01 -0400474 dirty_bitmap_mig_before_vm_start();
475
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300476 if (!global_state_received() ||
477 global_state_get_runstate() == RUN_STATE_RUNNING) {
478 if (autostart) {
479 vm_start();
480 } else {
481 runstate_set(RUN_STATE_PAUSED);
482 }
Zhang Chendb009722019-03-03 22:50:17 +0800483 } else if (migration_incoming_colo_enabled()) {
484 migration_incoming_disable_colo();
485 vm_start();
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300486 } else {
487 runstate_set(global_state_get_runstate());
488 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300489 /*
490 * This must happen after any state changes since as soon as an external
491 * observer sees this event they might start to prod at the VM assuming
492 * it's ready to use.
493 */
494 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
495 MIGRATION_STATUS_COMPLETED);
496 qemu_bh_delete(mis->bh);
497 migration_incoming_state_destroy();
498}
499
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200500static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200501{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100502 MigrationIncomingState *mis = migration_incoming_get_current();
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000503 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200504 int ret;
Zhang Chen8e48ac92018-09-03 12:38:46 +0800505 Error *local_err = NULL;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200506
Juan Quintela4f0fae72017-07-24 12:42:02 +0200507 assert(mis->from_src_file);
Lidong Chen92370982018-08-06 21:29:35 +0800508 mis->migration_incoming_co = qemu_coroutine_self();
Dr. David Alan Gilbert67f11b52017-02-24 18:28:34 +0000509 mis->largest_page_size = qemu_ram_pagesize_largest();
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000510 postcopy_state_set(POSTCOPY_INCOMING_NONE);
zhanghailiang93d7af62015-12-16 11:47:34 +0000511 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
512 MIGRATION_STATUS_ACTIVE);
Juan Quintela4f0fae72017-07-24 12:42:02 +0200513 ret = qemu_loadvm_state(mis->from_src_file);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100514
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000515 ps = postcopy_state_get();
516 trace_process_incoming_migration_co_end(ret, ps);
517 if (ps != POSTCOPY_INCOMING_NONE) {
518 if (ps == POSTCOPY_INCOMING_ADVISE) {
519 /*
520 * Where a migration had postcopy enabled (and thus went to advise)
521 * but managed to complete within the precopy period, we can use
522 * the normal exit.
523 */
524 postcopy_ram_incoming_cleanup(mis);
525 } else if (ret >= 0) {
526 /*
527 * Postcopy was started, cleanup should happen at the end of the
528 * postcopy thread.
529 */
530 trace_process_incoming_migration_co_postcopy_end_main();
531 return;
532 }
533 /* Else if something went wrong then just fall out of the normal exit */
534 }
535
zhanghailiang25d0c162016-10-27 14:42:55 +0800536 /* we get COLO info, and know if we are in COLO mode */
Zhang Chenaad555c2018-09-03 12:38:47 +0800537 if (!ret && migration_incoming_colo_enabled()) {
Zhang Chen8e48ac92018-09-03 12:38:46 +0800538 /* Make sure all file formats flush their mutable metadata */
539 bdrv_invalidate_cache_all(&local_err);
540 if (local_err) {
Zhang Chen8e48ac92018-09-03 12:38:46 +0800541 error_report_err(local_err);
Fei Li6d99c2d2019-01-13 22:08:49 +0800542 goto fail;
Zhang Chen8e48ac92018-09-03 12:38:46 +0800543 }
544
zhanghailiang25d0c162016-10-27 14:42:55 +0800545 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
546 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
547 mis->have_colo_incoming_thread = true;
548 qemu_coroutine_yield();
549
550 /* Wait checkpoint incoming thread exit before free resource */
551 qemu_thread_join(&mis->colo_incoming_thread);
Zhang Chen13af18f2018-09-03 12:38:48 +0800552 /* We hold the global iothread lock, so it is safe here */
553 colo_release_ram_cache();
zhanghailiang25d0c162016-10-27 14:42:55 +0800554 }
555
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200556 if (ret < 0) {
Peter Lievendb80fac2014-06-10 11:29:16 +0200557 error_report("load of migration failed: %s", strerror(-ret));
Fei Li6d99c2d2019-01-13 22:08:49 +0800558 goto fail;
Juan Quintela511c0232010-06-09 14:10:55 +0200559 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300560 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
561 qemu_bh_schedule(mis->bh);
Lidong Chen92370982018-08-06 21:29:35 +0800562 mis->migration_incoming_co = NULL;
Fei Li6d99c2d2019-01-13 22:08:49 +0800563 return;
564fail:
565 local_err = NULL;
566 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
567 MIGRATION_STATUS_FAILED);
568 qemu_fclose(mis->from_src_file);
569 if (multifd_load_cleanup(&local_err) != 0) {
570 error_report_err(local_err);
571 }
572 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200573}
574
Juan Quintelab673eab2019-06-12 11:44:19 +0200575/**
576 * @migration_incoming_setup: Setup incoming migration
577 *
578 * Returns 0 for no error or 1 for error
579 *
580 * @f: file for main migration channel
581 * @errp: where to put errors
582 */
583static int migration_incoming_setup(QEMUFile *f, Error **errp)
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200584{
Juan Quintela4f0fae72017-07-24 12:42:02 +0200585 MigrationIncomingState *mis = migration_incoming_get_current();
Juan Quintelab673eab2019-06-12 11:44:19 +0200586 Error *local_err = NULL;
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200587
Juan Quintelab673eab2019-06-12 11:44:19 +0200588 if (multifd_load_setup(&local_err) != 0) {
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100589 /* We haven't been able to create multifd threads
590 nothing better to do */
Juan Quintelab673eab2019-06-12 11:44:19 +0200591 error_report_err(local_err);
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100592 exit(EXIT_FAILURE);
593 }
594
Juan Quintela4f0fae72017-07-24 12:42:02 +0200595 if (!mis->from_src_file) {
596 mis->from_src_file = f;
597 }
Daniel P. Berrange06ad5132016-04-27 11:04:56 +0100598 qemu_file_set_blocking(f, false);
Juan Quintelab673eab2019-06-12 11:44:19 +0200599 return 0;
Juan Quintelae595a012017-07-17 12:30:25 +0200600}
601
Juan Quintela36c2f8b2018-03-07 08:40:52 +0100602void migration_incoming_process(void)
Juan Quintelae595a012017-07-17 12:30:25 +0200603{
604 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL);
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200605 qemu_coroutine_enter(co);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200606}
607
Peter Xu884835f2018-06-27 21:22:45 +0800608/* Returns true if recovered from a paused migration, otherwise false */
609static bool postcopy_try_recover(QEMUFile *f)
Juan Quintelae595a012017-07-17 12:30:25 +0200610{
Peter Xud96c9e82018-05-02 18:47:26 +0800611 MigrationIncomingState *mis = migration_incoming_get_current();
612
613 if (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
614 /* Resumed from a paused postcopy migration */
615
616 mis->from_src_file = f;
617 /* Postcopy has standalone thread to do vm load */
618 qemu_file_set_blocking(f, true);
619
620 /* Re-configure the return path */
621 mis->to_src_file = qemu_file_get_return_path(f);
622
623 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
624 MIGRATION_STATUS_POSTCOPY_RECOVER);
625
626 /*
627 * Here, we only wake up the main loading thread (while the
628 * fault thread will still be waiting), so that we can receive
629 * commands from source now, and answer it if needed. The
630 * fault thread will be woken up afterwards until we are sure
631 * that source is ready to reply to page requests.
632 */
633 qemu_sem_post(&mis->postcopy_pause_sem_dst);
Peter Xu884835f2018-06-27 21:22:45 +0800634 return true;
Peter Xud96c9e82018-05-02 18:47:26 +0800635 }
Peter Xu884835f2018-06-27 21:22:45 +0800636
637 return false;
638}
639
Juan Quintelab673eab2019-06-12 11:44:19 +0200640void migration_fd_process_incoming(QEMUFile *f, Error **errp)
Peter Xu884835f2018-06-27 21:22:45 +0800641{
Juan Quintelab673eab2019-06-12 11:44:19 +0200642 Error *local_err = NULL;
643
Peter Xu884835f2018-06-27 21:22:45 +0800644 if (postcopy_try_recover(f)) {
645 return;
646 }
647
Juan Quintelab673eab2019-06-12 11:44:19 +0200648 if (migration_incoming_setup(f, &local_err)) {
Markus Armbruster2155cea2020-07-22 10:40:47 +0200649 error_propagate(errp, local_err);
Juan Quintelab673eab2019-06-12 11:44:19 +0200650 return;
651 }
Peter Xu884835f2018-06-27 21:22:45 +0800652 migration_incoming_process();
Juan Quintelae595a012017-07-17 12:30:25 +0200653}
654
Fei Li49ed0d22019-01-13 22:08:46 +0800655void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp)
Juan Quintela4f0fae72017-07-24 12:42:02 +0200656{
657 MigrationIncomingState *mis = migration_incoming_get_current();
Juan Quintelab673eab2019-06-12 11:44:19 +0200658 Error *local_err = NULL;
Peter Xua429e7f2018-06-27 21:22:46 +0800659 bool start_migration;
Juan Quintela4f0fae72017-07-24 12:42:02 +0200660
661 if (!mis->from_src_file) {
Peter Xua429e7f2018-06-27 21:22:46 +0800662 /* The first connection (multifd may have multiple) */
Juan Quintela4f0fae72017-07-24 12:42:02 +0200663 QEMUFile *f = qemu_fopen_channel_input(ioc);
Peter Xua429e7f2018-06-27 21:22:46 +0800664
665 /* If it's a recovery, we're done */
Peter Xu884835f2018-06-27 21:22:45 +0800666 if (postcopy_try_recover(f)) {
667 return;
668 }
Peter Xua429e7f2018-06-27 21:22:46 +0800669
Juan Quintelab673eab2019-06-12 11:44:19 +0200670 if (migration_incoming_setup(f, &local_err)) {
Markus Armbruster2155cea2020-07-22 10:40:47 +0200671 error_propagate(errp, local_err);
Juan Quintelab673eab2019-06-12 11:44:19 +0200672 return;
673 }
Peter Xua429e7f2018-06-27 21:22:46 +0800674
675 /*
676 * Common migration only needs one channel, so we can start
677 * right now. Multifd needs more than one channel, we wait.
678 */
679 start_migration = !migrate_use_multifd();
680 } else {
681 /* Multiple connections */
682 assert(migrate_use_multifd());
Fei Li49ed0d22019-01-13 22:08:46 +0800683 start_migration = multifd_recv_new_channel(ioc, &local_err);
684 if (local_err) {
685 error_propagate(errp, local_err);
686 return;
687 }
Juan Quintela4f0fae72017-07-24 12:42:02 +0200688 }
Peter Xu81e62052018-06-27 21:22:44 +0800689
Peter Xua429e7f2018-06-27 21:22:46 +0800690 if (start_migration) {
Peter Xu81e62052018-06-27 21:22:44 +0800691 migration_incoming_process();
692 }
Juan Quintela4f0fae72017-07-24 12:42:02 +0200693}
694
Juan Quintela428d8902017-07-24 13:06:25 +0200695/**
696 * @migration_has_all_channels: We have received all channels that we need
697 *
698 * Returns true when we have got connections to all the channels that
699 * we need for migration.
700 */
701bool migration_has_all_channels(void)
702{
Daniel P. Berrangéca273df2018-06-19 17:35:52 +0100703 MigrationIncomingState *mis = migration_incoming_get_current();
Juan Quintela62c1e0c2018-02-19 18:59:02 +0100704 bool all_channels;
705
706 all_channels = multifd_recv_all_channels_created();
707
Daniel P. Berrangéca273df2018-06-19 17:35:52 +0100708 return all_channels && mis->from_src_file != NULL;
Juan Quintela428d8902017-07-24 13:06:25 +0200709}
710
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000711/*
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000712 * Send a 'SHUT' message on the return channel with the given value
713 * to indicate that we've finished with the RP. Non-0 value indicates
714 * error.
715 */
716void migrate_send_rp_shut(MigrationIncomingState *mis,
717 uint32_t value)
718{
719 uint32_t buf;
720
721 buf = cpu_to_be32(value);
722 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
723}
724
725/*
726 * Send a 'PONG' message on the return channel with the given value
727 * (normally in response to a 'PING')
728 */
729void migrate_send_rp_pong(MigrationIncomingState *mis,
730 uint32_t value)
731{
732 uint32_t buf;
733
734 buf = cpu_to_be32(value);
735 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
736}
737
Peter Xua335deb2018-05-02 18:47:28 +0800738void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
739 char *block_name)
740{
741 char buf[512];
742 int len;
743 int64_t res;
744
745 /*
746 * First, we send the header part. It contains only the len of
747 * idstr, and the idstr itself.
748 */
749 len = strlen(block_name);
750 buf[0] = len;
751 memcpy(buf + 1, block_name, len);
752
753 if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
754 error_report("%s: MSG_RP_RECV_BITMAP only used for recovery",
755 __func__);
756 return;
757 }
758
759 migrate_send_rp_message(mis, MIG_RP_MSG_RECV_BITMAP, len + 1, buf);
760
761 /*
762 * Next, we dump the received bitmap to the stream.
763 *
764 * TODO: currently we are safe since we are the only one that is
765 * using the to_src_file handle (fault thread is still paused),
766 * and it's ok even not taking the mutex. However the best way is
767 * to take the lock before sending the message header, and release
768 * the lock after sending the bitmap.
769 */
770 qemu_mutex_lock(&mis->rp_mutex);
771 res = ramblock_recv_bitmap_send(mis->to_src_file, block_name);
772 qemu_mutex_unlock(&mis->rp_mutex);
773
774 trace_migrate_send_rp_recv_bitmap(block_name, res);
775}
776
Peter Xu13955b82018-05-02 18:47:30 +0800777void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value)
778{
779 uint32_t buf;
780
781 buf = cpu_to_be32(value);
782 migrate_send_rp_message(mis, MIG_RP_MSG_RESUME_ACK, sizeof(buf), &buf);
783}
784
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300785MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
786{
787 MigrationCapabilityStatusList *head = NULL;
788 MigrationCapabilityStatusList *caps;
789 MigrationState *s = migrate_get_current();
790 int i;
791
Michael Tokarev387eede2013-10-05 13:18:28 +0400792 caps = NULL; /* silence compiler warning */
Eric Blake7fb1cf12015-11-18 01:52:57 -0700793 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Dr. David Alan Gilberted1701c2017-05-15 15:05:29 +0100794#ifndef CONFIG_LIVE_BLOCK_MIGRATION
795 if (i == MIGRATION_CAPABILITY_BLOCK) {
796 continue;
797 }
798#endif
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300799 if (head == NULL) {
800 head = g_malloc0(sizeof(*caps));
801 caps = head;
802 } else {
803 caps->next = g_malloc0(sizeof(*caps));
804 caps = caps->next;
805 }
806 caps->value =
807 g_malloc(sizeof(*caps->value));
808 caps->value->capability = i;
809 caps->value->state = s->enabled_capabilities[i];
810 }
811
812 return head;
813}
814
Liang Li85de8322015-03-23 16:32:28 +0800815MigrationParameters *qmp_query_migrate_parameters(Error **errp)
816{
817 MigrationParameters *params;
818 MigrationState *s = migrate_get_current();
819
Markus Armbrustere87fae42017-07-18 12:57:38 +0200820 /* TODO use QAPI_CLONE() instead of duplicating it inline */
Liang Li85de8322015-03-23 16:32:28 +0800821 params = g_malloc0(sizeof(*params));
Eric Blakede63ab62016-09-08 22:14:15 -0500822 params->has_compress_level = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100823 params->compress_level = s->parameters.compress_level;
Eric Blakede63ab62016-09-08 22:14:15 -0500824 params->has_compress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100825 params->compress_threads = s->parameters.compress_threads;
Xiao Guangrong1d588722018-08-21 16:10:20 +0800826 params->has_compress_wait_thread = true;
827 params->compress_wait_thread = s->parameters.compress_wait_thread;
Eric Blakede63ab62016-09-08 22:14:15 -0500828 params->has_decompress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100829 params->decompress_threads = s->parameters.decompress_threads;
Keqian Zhudc14a472020-02-24 10:31:42 +0800830 params->has_throttle_trigger_threshold = true;
831 params->throttle_trigger_threshold = s->parameters.throttle_trigger_threshold;
Eric Blakede63ab62016-09-08 22:14:15 -0500832 params->has_cpu_throttle_initial = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100833 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
Eric Blakede63ab62016-09-08 22:14:15 -0500834 params->has_cpu_throttle_increment = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100835 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
Keqian Zhucbbf8182020-04-13 18:15:08 +0800836 params->has_cpu_throttle_tailslow = true;
837 params->cpu_throttle_tailslow = s->parameters.cpu_throttle_tailslow;
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200838 params->has_tls_creds = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100839 params->tls_creds = g_strdup(s->parameters.tls_creds);
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200840 params->has_tls_hostname = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100841 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
Daniel P. Berranged2f1d292019-02-27 14:53:24 +0000842 params->has_tls_authz = true;
Mao Zhongyi7cd75cb2020-03-25 09:49:30 +0800843 params->tls_authz = g_strdup(s->parameters.tls_authz ?
844 s->parameters.tls_authz : "");
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530845 params->has_max_bandwidth = true;
846 params->max_bandwidth = s->parameters.max_bandwidth;
847 params->has_downtime_limit = true;
848 params->downtime_limit = s->parameters.downtime_limit;
zhanghailiangfe39a4d2016-11-02 15:42:09 +0800849 params->has_x_checkpoint_delay = true;
zhanghailiang68b53592016-10-27 14:43:01 +0800850 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
Juan Quintela2833c592017-04-05 18:32:37 +0200851 params->has_block_incremental = true;
852 params->block_incremental = s->parameters.block_incremental;
Juan Quintelacbfd6c92019-02-06 13:54:06 +0100853 params->has_multifd_channels = true;
854 params->multifd_channels = s->parameters.multifd_channels;
Juan Quintela96eef042019-01-16 10:35:55 +0100855 params->has_multifd_compression = true;
856 params->multifd_compression = s->parameters.multifd_compression;
Juan Quintela9004db42020-01-23 17:08:52 +0100857 params->has_multifd_zlib_level = true;
858 params->multifd_zlib_level = s->parameters.multifd_zlib_level;
Juan Quintela6a9ad152020-01-23 17:41:36 +0100859 params->has_multifd_zstd_level = true;
860 params->multifd_zstd_level = s->parameters.multifd_zstd_level;
Juan Quintela73af8dd2017-10-05 21:30:10 +0200861 params->has_xbzrle_cache_size = true;
862 params->xbzrle_cache_size = s->parameters.xbzrle_cache_size;
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +0100863 params->has_max_postcopy_bandwidth = true;
864 params->max_postcopy_bandwidth = s->parameters.max_postcopy_bandwidth;
Li Qiang4cbc9c72018-08-01 06:00:20 -0700865 params->has_max_cpu_throttle = true;
866 params->max_cpu_throttle = s->parameters.max_cpu_throttle;
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +0000867 params->has_announce_initial = true;
868 params->announce_initial = s->parameters.announce_initial;
869 params->has_announce_max = true;
870 params->announce_max = s->parameters.announce_max;
871 params->has_announce_rounds = true;
872 params->announce_rounds = s->parameters.announce_rounds;
873 params->has_announce_step = true;
874 params->announce_step = s->parameters.announce_step;
Liang Li85de8322015-03-23 16:32:28 +0800875
Max Reitz31e4c352020-08-20 17:07:23 +0200876 if (s->parameters.has_block_bitmap_mapping) {
877 params->has_block_bitmap_mapping = true;
878 params->block_bitmap_mapping =
879 QAPI_CLONE(BitmapMigrationNodeAliasList,
880 s->parameters.block_bitmap_mapping);
881 }
882
Liang Li85de8322015-03-23 16:32:28 +0800883 return params;
884}
885
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +0000886AnnounceParameters *migrate_announce_params(void)
887{
888 static AnnounceParameters ap;
889
890 MigrationState *s = migrate_get_current();
891
892 ap.initial = s->parameters.announce_initial;
893 ap.max = s->parameters.announce_max;
894 ap.rounds = s->parameters.announce_rounds;
895 ap.step = s->parameters.announce_step;
896
897 return &ap;
898}
899
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000900/*
901 * Return true if we're already in the middle of a migration
902 * (i.e. any of the active or setup states)
903 */
Jia Lina3d63da12018-10-26 16:36:20 +0800904bool migration_is_setup_or_active(int state)
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000905{
906 switch (state) {
907 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000908 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Peter Xua688d2c2018-05-02 18:47:18 +0800909 case MIGRATION_STATUS_POSTCOPY_PAUSED:
Peter Xu135b87b2018-05-02 18:47:25 +0800910 case MIGRATION_STATUS_POSTCOPY_RECOVER:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000911 case MIGRATION_STATUS_SETUP:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +0100912 case MIGRATION_STATUS_PRE_SWITCHOVER:
913 case MIGRATION_STATUS_DEVICE:
Jens Freimannc7e0acd2019-10-29 12:49:02 +0100914 case MIGRATION_STATUS_WAIT_UNPLUG:
zhanghailiang19dd4082020-02-24 14:54:12 +0800915 case MIGRATION_STATUS_COLO:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000916 return true;
917
918 default:
919 return false;
920
921 }
922}
923
Juan Quintela392d87e2020-01-21 15:39:23 +0100924bool migration_is_running(int state)
925{
926 switch (state) {
927 case MIGRATION_STATUS_ACTIVE:
928 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
929 case MIGRATION_STATUS_POSTCOPY_PAUSED:
930 case MIGRATION_STATUS_POSTCOPY_RECOVER:
931 case MIGRATION_STATUS_SETUP:
932 case MIGRATION_STATUS_PRE_SWITCHOVER:
933 case MIGRATION_STATUS_DEVICE:
934 case MIGRATION_STATUS_WAIT_UNPLUG:
935 case MIGRATION_STATUS_CANCELLING:
Juan Quintela392d87e2020-01-21 15:39:23 +0100936 return true;
937
938 default:
939 return false;
940
941 }
942}
943
Wei Yang640dfb12019-07-16 08:54:11 +0800944static void populate_time_info(MigrationInfo *info, MigrationState *s)
945{
946 info->has_status = true;
947 info->has_setup_time = true;
948 info->setup_time = s->setup_time;
949 if (s->state == MIGRATION_STATUS_COMPLETED) {
950 info->has_total_time = true;
951 info->total_time = s->total_time;
952 info->has_downtime = true;
953 info->downtime = s->downtime;
954 } else {
955 info->has_total_time = true;
956 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
957 s->start_time;
958 info->has_expected_downtime = true;
959 info->expected_downtime = s->expected_downtime;
960 }
961}
962
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100963static void populate_ram_info(MigrationInfo *info, MigrationState *s)
964{
965 info->has_ram = true;
966 info->ram = g_malloc0(sizeof(*info->ram));
Juan Quintela93604472017-06-06 19:49:03 +0200967 info->ram->transferred = ram_counters.transferred;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100968 info->ram->total = ram_bytes_total();
Juan Quintela93604472017-06-06 19:49:03 +0200969 info->ram->duplicate = ram_counters.duplicate;
Juan Quintelabedf53c2017-03-13 20:35:54 +0100970 /* legacy value. It is not used anymore */
971 info->ram->skipped = 0;
Juan Quintela93604472017-06-06 19:49:03 +0200972 info->ram->normal = ram_counters.normal;
973 info->ram->normal_bytes = ram_counters.normal *
Juan Quintela20afaed2017-03-21 09:09:14 +0100974 qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100975 info->ram->mbps = s->mbps;
Juan Quintela93604472017-06-06 19:49:03 +0200976 info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
977 info->ram->postcopy_requests = ram_counters.postcopy_requests;
Chao Fan030ce1f2017-03-21 10:22:43 +0800978 info->ram->page_size = qemu_target_page_size();
Juan Quintelaa61c45b2018-06-26 15:20:11 +0200979 info->ram->multifd_bytes = ram_counters.multifd_bytes;
Xiao Guangrongaecbfe92019-01-11 14:37:30 +0800980 info->ram->pages_per_second = s->pages_per_second;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100981
Juan Quintela114f5ae2017-05-04 10:09:21 +0200982 if (migrate_use_xbzrle()) {
983 info->has_xbzrle_cache = true;
984 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
985 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
Juan Quintela93604472017-06-06 19:49:03 +0200986 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
987 info->xbzrle_cache->pages = xbzrle_counters.pages;
988 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
989 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
Wei Wange460a4b2020-04-30 08:59:35 +0800990 info->xbzrle_cache->encoding_rate = xbzrle_counters.encoding_rate;
Juan Quintela93604472017-06-06 19:49:03 +0200991 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
Juan Quintela114f5ae2017-05-04 10:09:21 +0200992 }
993
Xiao Guangrong76e03002018-09-06 15:01:00 +0800994 if (migrate_use_compression()) {
995 info->has_compression = true;
996 info->compression = g_malloc0(sizeof(*info->compression));
997 info->compression->pages = compression_counters.pages;
998 info->compression->busy = compression_counters.busy;
999 info->compression->busy_rate = compression_counters.busy_rate;
1000 info->compression->compressed_size =
1001 compression_counters.compressed_size;
1002 info->compression->compression_rate =
1003 compression_counters.compression_rate;
1004 }
1005
Juan Quintela338182c2017-05-03 13:16:38 +02001006 if (cpu_throttle_active()) {
1007 info->has_cpu_throttle_percentage = true;
1008 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
1009 }
1010
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +01001011 if (s->state != MIGRATION_STATUS_COMPLETED) {
1012 info->ram->remaining = ram_bytes_remaining();
Juan Quintela93604472017-06-06 19:49:03 +02001013 info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +01001014 }
1015}
1016
Juan Quintela930ac042017-05-04 10:21:46 +02001017static void populate_disk_info(MigrationInfo *info)
1018{
1019 if (blk_mig_active()) {
1020 info->has_disk = true;
1021 info->disk = g_malloc0(sizeof(*info->disk));
1022 info->disk->transferred = blk_mig_bytes_transferred();
1023 info->disk->remaining = blk_mig_bytes_remaining();
1024 info->disk->total = blk_mig_bytes_total();
1025 }
1026}
1027
Kirti Wankhede37105862020-10-26 15:06:27 +05301028static void populate_vfio_info(MigrationInfo *info)
1029{
1030#ifdef CONFIG_VFIO
1031 if (vfio_mig_active()) {
1032 info->has_vfio = true;
1033 info->vfio = g_malloc0(sizeof(*info->vfio));
1034 info->vfio->transferred = vfio_mig_bytes_transferred();
1035 }
1036#endif
1037}
1038
Alexey Perevalov65ace062018-03-22 21:17:27 +03001039static void fill_source_migration_info(MigrationInfo *info)
aliguori5bb79102008-10-13 03:12:02 +00001040{
Juan Quintela17549e82011-10-05 13:50:43 +02001041 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +00001042
Juan Quintela17549e82011-10-05 13:50:43 +02001043 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +08001044 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +02001045 /* no migration has happened ever */
Alexey Perevalov65ace062018-03-22 21:17:27 +03001046 /* do not overwrite destination migration status */
1047 return;
zhanghailiang31194732015-03-13 16:08:38 +08001048 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001049 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -04001050 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001051 break;
zhanghailiang31194732015-03-13 16:08:38 +08001052 case MIGRATION_STATUS_ACTIVE:
1053 case MIGRATION_STATUS_CANCELLING:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001054 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +01001055 case MIGRATION_STATUS_PRE_SWITCHOVER:
1056 case MIGRATION_STATUS_DEVICE:
Peter Xua688d2c2018-05-02 18:47:18 +08001057 case MIGRATION_STATUS_POSTCOPY_PAUSED:
Peter Xu135b87b2018-05-02 18:47:25 +08001058 case MIGRATION_STATUS_POSTCOPY_RECOVER:
Wei Yang640dfb12019-07-16 08:54:11 +08001059 /* TODO add some postcopy stats */
1060 populate_time_info(info, s);
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +01001061 populate_ram_info(info, s);
Juan Quintela930ac042017-05-04 10:21:46 +02001062 populate_disk_info(info);
Kirti Wankhede37105862020-10-26 15:06:27 +05301063 populate_vfio_info(info);
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001064 break;
zhanghailiang0b827d52016-10-27 14:42:54 +08001065 case MIGRATION_STATUS_COLO:
1066 info->has_status = true;
1067 /* TODO: display COLO specific information (checkpoint info etc.) */
1068 break;
zhanghailiang31194732015-03-13 16:08:38 +08001069 case MIGRATION_STATUS_COMPLETED:
Wei Yang640dfb12019-07-16 08:54:11 +08001070 populate_time_info(info, s);
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +01001071 populate_ram_info(info, s);
Kirti Wankhede37105862020-10-26 15:06:27 +05301072 populate_vfio_info(info);
Juan Quintela17549e82011-10-05 13:50:43 +02001073 break;
zhanghailiang31194732015-03-13 16:08:38 +08001074 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -03001075 info->has_status = true;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001076 if (s->error) {
1077 info->has_error_desc = true;
1078 info->error_desc = g_strdup(error_get_pretty(s->error));
1079 }
Juan Quintela17549e82011-10-05 13:50:43 +02001080 break;
zhanghailiang31194732015-03-13 16:08:38 +08001081 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -03001082 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +02001083 break;
Jens Freimannc7e0acd2019-10-29 12:49:02 +01001084 case MIGRATION_STATUS_WAIT_UNPLUG:
1085 info->has_status = true;
1086 break;
aliguori5bb79102008-10-13 03:12:02 +00001087 }
zhanghailiangcde63fb2015-03-13 16:08:41 +08001088 info->status = s->state;
aliguori5bb79102008-10-13 03:12:02 +00001089}
1090
Peter Xu4a842142017-07-18 11:39:08 +08001091/**
1092 * @migration_caps_check - check capability validity
1093 *
1094 * @cap_list: old capability list, array of bool
1095 * @params: new capabilities to be applied soon
1096 * @errp: set *errp if the check failed, with reason
1097 *
1098 * Returns true if check passed, otherwise false.
1099 */
1100static bool migrate_caps_check(bool *cap_list,
1101 MigrationCapabilityStatusList *params,
1102 Error **errp)
Orit Wasserman00458432012-08-06 21:42:48 +03001103{
Orit Wasserman00458432012-08-06 21:42:48 +03001104 MigrationCapabilityStatusList *cap;
Peter Xu4a842142017-07-18 11:39:08 +08001105 bool old_postcopy_cap;
Alexey Perevalovd7651f12017-09-19 19:47:56 +03001106 MigrationIncomingState *mis = migration_incoming_get_current();
Orit Wasserman00458432012-08-06 21:42:48 +03001107
Peter Xu4a842142017-07-18 11:39:08 +08001108 old_postcopy_cap = cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Orit Wasserman00458432012-08-06 21:42:48 +03001109
1110 for (cap = params; cap; cap = cap->next) {
Peter Xu4a842142017-07-18 11:39:08 +08001111 cap_list[cap->value->capability] = cap->value->state;
Orit Wasserman00458432012-08-06 21:42:48 +03001112 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001113
Peter Xu4a842142017-07-18 11:39:08 +08001114#ifndef CONFIG_LIVE_BLOCK_MIGRATION
1115 if (cap_list[MIGRATION_CAPABILITY_BLOCK]) {
1116 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
1117 "block migration");
1118 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
1119 return false;
1120 }
1121#endif
1122
Zhang Chen7e934f52018-11-15 03:09:12 +08001123#ifndef CONFIG_REPLICATION
1124 if (cap_list[MIGRATION_CAPABILITY_X_COLO]) {
1125 error_setg(errp, "QEMU compiled without replication module"
1126 " can't enable COLO");
1127 error_append_hint(errp, "Please enable replication before COLO.\n");
1128 return false;
1129 }
1130#endif
1131
Peter Xu4a842142017-07-18 11:39:08 +08001132 if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +01001133 /* This check is reasonably expensive, so only when it's being
1134 * set the first time, also it's only the destination that needs
1135 * special support.
1136 */
1137 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
Alexey Perevalovd7651f12017-09-19 19:47:56 +03001138 !postcopy_ram_supported_by_host(mis)) {
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +01001139 /* postcopy_ram_supported_by_host will have emitted a more
1140 * detailed message
1141 */
Peter Xu4a842142017-07-18 11:39:08 +08001142 error_setg(errp, "Postcopy is not supported");
1143 return false;
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +01001144 }
Yury Kotov18269062019-02-15 20:45:45 +03001145
1146 if (cap_list[MIGRATION_CAPABILITY_X_IGNORE_SHARED]) {
1147 error_setg(errp, "Postcopy is not compatible with ignore-shared");
1148 return false;
1149 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001150 }
Peter Xu4a842142017-07-18 11:39:08 +08001151
1152 return true;
1153}
1154
Alexey Perevalov65ace062018-03-22 21:17:27 +03001155static void fill_destination_migration_info(MigrationInfo *info)
1156{
1157 MigrationIncomingState *mis = migration_incoming_get_current();
1158
Juan Quintela9aca82b2019-02-27 11:51:27 +01001159 if (mis->socket_address_list) {
1160 info->has_socket_address = true;
1161 info->socket_address =
1162 QAPI_CLONE(SocketAddressList, mis->socket_address_list);
1163 }
1164
Alexey Perevalov65ace062018-03-22 21:17:27 +03001165 switch (mis->state) {
1166 case MIGRATION_STATUS_NONE:
1167 return;
Alexey Perevalov65ace062018-03-22 21:17:27 +03001168 case MIGRATION_STATUS_SETUP:
1169 case MIGRATION_STATUS_CANCELLING:
1170 case MIGRATION_STATUS_CANCELLED:
1171 case MIGRATION_STATUS_ACTIVE:
1172 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Peter Xu3c9928d2018-07-10 17:18:56 +08001173 case MIGRATION_STATUS_POSTCOPY_PAUSED:
1174 case MIGRATION_STATUS_POSTCOPY_RECOVER:
Alexey Perevalov65ace062018-03-22 21:17:27 +03001175 case MIGRATION_STATUS_FAILED:
1176 case MIGRATION_STATUS_COLO:
1177 info->has_status = true;
1178 break;
1179 case MIGRATION_STATUS_COMPLETED:
1180 info->has_status = true;
1181 fill_destination_postcopy_migration_info(info);
1182 break;
1183 }
1184 info->status = mis->state;
1185}
1186
1187MigrationInfo *qmp_query_migrate(Error **errp)
1188{
1189 MigrationInfo *info = g_malloc0(sizeof(*info));
1190
1191 fill_destination_migration_info(info);
1192 fill_source_migration_info(info);
1193
1194 return info;
1195}
1196
Peter Xu4a842142017-07-18 11:39:08 +08001197void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
1198 Error **errp)
1199{
1200 MigrationState *s = migrate_get_current();
1201 MigrationCapabilityStatusList *cap;
Peter Xudd0ee302018-03-05 17:49:38 +08001202 bool cap_list[MIGRATION_CAPABILITY__MAX];
Peter Xu4a842142017-07-18 11:39:08 +08001203
Juan Quintela392d87e2020-01-21 15:39:23 +01001204 if (migration_is_running(s->state)) {
Peter Xu4a842142017-07-18 11:39:08 +08001205 error_setg(errp, QERR_MIGRATION_ACTIVE);
1206 return;
1207 }
1208
Peter Xudd0ee302018-03-05 17:49:38 +08001209 memcpy(cap_list, s->enabled_capabilities, sizeof(cap_list));
1210 if (!migrate_caps_check(cap_list, params, errp)) {
Peter Xu4a842142017-07-18 11:39:08 +08001211 return;
1212 }
1213
1214 for (cap = params; cap; cap = cap->next) {
1215 s->enabled_capabilities[cap->value->capability] = cap->value->state;
1216 }
Orit Wasserman00458432012-08-06 21:42:48 +03001217}
1218
Peter Xu16d063b2017-07-18 11:39:04 +08001219/*
1220 * Check whether the parameters are valid. Error will be put into errp
1221 * (if provided). Return true if valid, otherwise false.
1222 */
1223static bool migrate_params_check(MigrationParameters *params, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +08001224{
Eric Blake7f375e02016-09-08 22:14:16 -05001225 if (params->has_compress_level &&
Juan Quintela741d4082017-12-01 13:08:38 +01001226 (params->compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001227 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
1228 "is invalid, it should be in the range of 0 to 9");
Peter Xu16d063b2017-07-18 11:39:04 +08001229 return false;
Liang Li85de8322015-03-23 16:32:28 +08001230 }
Peter Xu16d063b2017-07-18 11:39:04 +08001231
Juan Quintela741d4082017-12-01 13:08:38 +01001232 if (params->has_compress_threads && (params->compress_threads < 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001233 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1234 "compress_threads",
1235 "is invalid, it should be in the range of 1 to 255");
Peter Xu16d063b2017-07-18 11:39:04 +08001236 return false;
Liang Li85de8322015-03-23 16:32:28 +08001237 }
Peter Xu16d063b2017-07-18 11:39:04 +08001238
Juan Quintela741d4082017-12-01 13:08:38 +01001239 if (params->has_decompress_threads && (params->decompress_threads < 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001240 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1241 "decompress_threads",
1242 "is invalid, it should be in the range of 1 to 255");
Peter Xu16d063b2017-07-18 11:39:04 +08001243 return false;
Liang Li85de8322015-03-23 16:32:28 +08001244 }
Peter Xu16d063b2017-07-18 11:39:04 +08001245
Keqian Zhudc14a472020-02-24 10:31:42 +08001246 if (params->has_throttle_trigger_threshold &&
1247 (params->throttle_trigger_threshold < 1 ||
1248 params->throttle_trigger_threshold > 100)) {
1249 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1250 "throttle_trigger_threshold",
1251 "an integer in the range of 1 to 100");
1252 return false;
1253 }
1254
Eric Blake7f375e02016-09-08 22:14:16 -05001255 if (params->has_cpu_throttle_initial &&
1256 (params->cpu_throttle_initial < 1 ||
1257 params->cpu_throttle_initial > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -04001258 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -04001259 "cpu_throttle_initial",
Jason J. Herne1626fee2015-09-08 13:12:34 -04001260 "an integer in the range of 1 to 99");
Peter Xu16d063b2017-07-18 11:39:04 +08001261 return false;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001262 }
Peter Xu16d063b2017-07-18 11:39:04 +08001263
Eric Blake7f375e02016-09-08 22:14:16 -05001264 if (params->has_cpu_throttle_increment &&
1265 (params->cpu_throttle_increment < 1 ||
1266 params->cpu_throttle_increment > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -04001267 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -04001268 "cpu_throttle_increment",
Jason J. Herne1626fee2015-09-08 13:12:34 -04001269 "an integer in the range of 1 to 99");
Peter Xu16d063b2017-07-18 11:39:04 +08001270 return false;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001271 }
Peter Xu16d063b2017-07-18 11:39:04 +08001272
Juan Quintela741d4082017-12-01 13:08:38 +01001273 if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) {
Mao Zhongyi2ee30cf2020-03-31 16:22:05 +08001274 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1275 "max_bandwidth",
1276 "an integer in the range of 0 to "stringify(SIZE_MAX)
1277 " bytes/second");
Peter Xu16d063b2017-07-18 11:39:04 +08001278 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301279 }
Peter Xu16d063b2017-07-18 11:39:04 +08001280
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301281 if (params->has_downtime_limit &&
Juan Quintela741d4082017-12-01 13:08:38 +01001282 (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
Mao Zhongyi2ee30cf2020-03-31 16:22:05 +08001283 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1284 "downtime_limit",
1285 "an integer in the range of 0 to "
Mao Zhongyi7ac55292020-03-31 16:22:07 +08001286 stringify(MAX_MIGRATE_DOWNTIME)" ms");
Peter Xu16d063b2017-07-18 11:39:04 +08001287 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301288 }
Peter Xu16d063b2017-07-18 11:39:04 +08001289
Juan Quintela741d4082017-12-01 13:08:38 +01001290 /* x_checkpoint_delay is now always positive */
1291
Juan Quintelacbfd6c92019-02-06 13:54:06 +01001292 if (params->has_multifd_channels && (params->multifd_channels < 1)) {
Juan Quintela4075fb12016-01-15 08:56:17 +01001293 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1294 "multifd_channels",
1295 "is invalid, it should be in the range of 1 to 255");
1296 return false;
1297 }
Peter Xu16d063b2017-07-18 11:39:04 +08001298
Juan Quintela9004db42020-01-23 17:08:52 +01001299 if (params->has_multifd_zlib_level &&
1300 (params->multifd_zlib_level > 9)) {
1301 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zlib_level",
1302 "is invalid, it should be in the range of 0 to 9");
1303 return false;
1304 }
1305
Juan Quintela6a9ad152020-01-23 17:41:36 +01001306 if (params->has_multifd_zstd_level &&
1307 (params->multifd_zstd_level > 20)) {
1308 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zstd_level",
1309 "is invalid, it should be in the range of 0 to 20");
1310 return false;
1311 }
1312
Juan Quintela73af8dd2017-10-05 21:30:10 +02001313 if (params->has_xbzrle_cache_size &&
1314 (params->xbzrle_cache_size < qemu_target_page_size() ||
1315 !is_power_of_2(params->xbzrle_cache_size))) {
1316 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1317 "xbzrle_cache_size",
1318 "is invalid, it should be bigger than target page size"
Mao Zhongyi06b1c6f2020-03-20 22:32:16 +08001319 " and a power of 2");
Juan Quintela73af8dd2017-10-05 21:30:10 +02001320 return false;
1321 }
1322
Li Qiang4cbc9c72018-08-01 06:00:20 -07001323 if (params->has_max_cpu_throttle &&
1324 (params->max_cpu_throttle < params->cpu_throttle_initial ||
1325 params->max_cpu_throttle > 99)) {
1326 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1327 "max_cpu_throttle",
1328 "an integer in the range of cpu_throttle_initial to 99");
1329 return false;
1330 }
1331
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00001332 if (params->has_announce_initial &&
1333 params->announce_initial > 100000) {
1334 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1335 "announce_initial",
1336 "is invalid, it must be less than 100000 ms");
1337 return false;
1338 }
1339 if (params->has_announce_max &&
1340 params->announce_max > 100000) {
1341 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1342 "announce_max",
1343 "is invalid, it must be less than 100000 ms");
1344 return false;
1345 }
1346 if (params->has_announce_rounds &&
1347 params->announce_rounds > 1000) {
1348 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1349 "announce_rounds",
1350 "is invalid, it must be in the range of 0 to 1000");
1351 return false;
1352 }
1353 if (params->has_announce_step &&
1354 (params->announce_step < 1 ||
1355 params->announce_step > 10000)) {
1356 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1357 "announce_step",
1358 "is invalid, it must be in the range of 1 to 10000 ms");
1359 return false;
1360 }
Max Reitz31e4c352020-08-20 17:07:23 +02001361
1362 if (params->has_block_bitmap_mapping &&
1363 !check_dirty_bitmap_mig_alias_map(params->block_bitmap_mapping, errp)) {
1364 error_prepend(errp, "Invalid mapping given for block-bitmap-mapping: ");
1365 return false;
1366 }
1367
Peter Xu16d063b2017-07-18 11:39:04 +08001368 return true;
1369}
1370
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001371static void migrate_params_test_apply(MigrateSetParameters *params,
1372 MigrationParameters *dest)
1373{
1374 *dest = migrate_get_current()->parameters;
1375
1376 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1377
1378 if (params->has_compress_level) {
1379 dest->compress_level = params->compress_level;
1380 }
1381
1382 if (params->has_compress_threads) {
1383 dest->compress_threads = params->compress_threads;
1384 }
1385
Xiao Guangrong1d588722018-08-21 16:10:20 +08001386 if (params->has_compress_wait_thread) {
1387 dest->compress_wait_thread = params->compress_wait_thread;
1388 }
1389
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001390 if (params->has_decompress_threads) {
1391 dest->decompress_threads = params->decompress_threads;
1392 }
1393
Keqian Zhudc14a472020-02-24 10:31:42 +08001394 if (params->has_throttle_trigger_threshold) {
1395 dest->throttle_trigger_threshold = params->throttle_trigger_threshold;
1396 }
1397
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001398 if (params->has_cpu_throttle_initial) {
1399 dest->cpu_throttle_initial = params->cpu_throttle_initial;
1400 }
1401
1402 if (params->has_cpu_throttle_increment) {
1403 dest->cpu_throttle_increment = params->cpu_throttle_increment;
1404 }
1405
Keqian Zhucbbf8182020-04-13 18:15:08 +08001406 if (params->has_cpu_throttle_tailslow) {
1407 dest->cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1408 }
1409
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001410 if (params->has_tls_creds) {
Markus Armbruster01fa5592017-07-18 14:42:04 +02001411 assert(params->tls_creds->type == QTYPE_QSTRING);
Zheng Chuan9728ebf2020-07-09 16:28:25 +08001412 dest->tls_creds = params->tls_creds->u.s;
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001413 }
1414
1415 if (params->has_tls_hostname) {
Markus Armbruster01fa5592017-07-18 14:42:04 +02001416 assert(params->tls_hostname->type == QTYPE_QSTRING);
Zheng Chuan9728ebf2020-07-09 16:28:25 +08001417 dest->tls_hostname = params->tls_hostname->u.s;
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001418 }
1419
1420 if (params->has_max_bandwidth) {
1421 dest->max_bandwidth = params->max_bandwidth;
1422 }
1423
1424 if (params->has_downtime_limit) {
1425 dest->downtime_limit = params->downtime_limit;
1426 }
1427
1428 if (params->has_x_checkpoint_delay) {
1429 dest->x_checkpoint_delay = params->x_checkpoint_delay;
1430 }
1431
1432 if (params->has_block_incremental) {
1433 dest->block_incremental = params->block_incremental;
1434 }
Juan Quintelacbfd6c92019-02-06 13:54:06 +01001435 if (params->has_multifd_channels) {
1436 dest->multifd_channels = params->multifd_channels;
Juan Quintela5e7577a2017-10-09 18:07:56 +02001437 }
Juan Quintela96eef042019-01-16 10:35:55 +01001438 if (params->has_multifd_compression) {
1439 dest->multifd_compression = params->multifd_compression;
1440 }
Juan Quintela73af8dd2017-10-05 21:30:10 +02001441 if (params->has_xbzrle_cache_size) {
1442 dest->xbzrle_cache_size = params->xbzrle_cache_size;
1443 }
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01001444 if (params->has_max_postcopy_bandwidth) {
1445 dest->max_postcopy_bandwidth = params->max_postcopy_bandwidth;
1446 }
Li Qiang4cbc9c72018-08-01 06:00:20 -07001447 if (params->has_max_cpu_throttle) {
1448 dest->max_cpu_throttle = params->max_cpu_throttle;
1449 }
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00001450 if (params->has_announce_initial) {
1451 dest->announce_initial = params->announce_initial;
1452 }
1453 if (params->has_announce_max) {
1454 dest->announce_max = params->announce_max;
1455 }
1456 if (params->has_announce_rounds) {
1457 dest->announce_rounds = params->announce_rounds;
1458 }
1459 if (params->has_announce_step) {
1460 dest->announce_step = params->announce_step;
1461 }
Max Reitz31e4c352020-08-20 17:07:23 +02001462
1463 if (params->has_block_bitmap_mapping) {
1464 dest->has_block_bitmap_mapping = true;
1465 dest->block_bitmap_mapping = params->block_bitmap_mapping;
1466 }
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001467}
1468
Juan Quintela73af8dd2017-10-05 21:30:10 +02001469static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
Peter Xu16d063b2017-07-18 11:39:04 +08001470{
1471 MigrationState *s = migrate_get_current();
1472
Markus Armbrustere87fae42017-07-18 12:57:38 +02001473 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1474
Eric Blake7f375e02016-09-08 22:14:16 -05001475 if (params->has_compress_level) {
1476 s->parameters.compress_level = params->compress_level;
Liang Li85de8322015-03-23 16:32:28 +08001477 }
Peter Xu476c72a2017-07-18 11:39:05 +08001478
Eric Blake7f375e02016-09-08 22:14:16 -05001479 if (params->has_compress_threads) {
1480 s->parameters.compress_threads = params->compress_threads;
Liang Li85de8322015-03-23 16:32:28 +08001481 }
Peter Xu476c72a2017-07-18 11:39:05 +08001482
Xiao Guangrong1d588722018-08-21 16:10:20 +08001483 if (params->has_compress_wait_thread) {
1484 s->parameters.compress_wait_thread = params->compress_wait_thread;
1485 }
1486
Eric Blake7f375e02016-09-08 22:14:16 -05001487 if (params->has_decompress_threads) {
1488 s->parameters.decompress_threads = params->decompress_threads;
Liang Li85de8322015-03-23 16:32:28 +08001489 }
Peter Xu476c72a2017-07-18 11:39:05 +08001490
Keqian Zhudc14a472020-02-24 10:31:42 +08001491 if (params->has_throttle_trigger_threshold) {
1492 s->parameters.throttle_trigger_threshold = params->throttle_trigger_threshold;
1493 }
1494
Eric Blake7f375e02016-09-08 22:14:16 -05001495 if (params->has_cpu_throttle_initial) {
1496 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001497 }
Peter Xu476c72a2017-07-18 11:39:05 +08001498
Eric Blake7f375e02016-09-08 22:14:16 -05001499 if (params->has_cpu_throttle_increment) {
1500 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001501 }
Peter Xu476c72a2017-07-18 11:39:05 +08001502
Keqian Zhucbbf8182020-04-13 18:15:08 +08001503 if (params->has_cpu_throttle_tailslow) {
1504 s->parameters.cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1505 }
1506
Eric Blake7f375e02016-09-08 22:14:16 -05001507 if (params->has_tls_creds) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001508 g_free(s->parameters.tls_creds);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001509 assert(params->tls_creds->type == QTYPE_QSTRING);
1510 s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001511 }
Peter Xu476c72a2017-07-18 11:39:05 +08001512
Eric Blake7f375e02016-09-08 22:14:16 -05001513 if (params->has_tls_hostname) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001514 g_free(s->parameters.tls_hostname);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001515 assert(params->tls_hostname->type == QTYPE_QSTRING);
1516 s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001517 }
Peter Xu476c72a2017-07-18 11:39:05 +08001518
Daniel P. Berranged2f1d292019-02-27 14:53:24 +00001519 if (params->has_tls_authz) {
1520 g_free(s->parameters.tls_authz);
1521 assert(params->tls_authz->type == QTYPE_QSTRING);
1522 s->parameters.tls_authz = g_strdup(params->tls_authz->u.s);
1523 }
1524
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301525 if (params->has_max_bandwidth) {
1526 s->parameters.max_bandwidth = params->max_bandwidth;
Dr. David Alan Gilbertc38c1c12019-03-08 10:12:10 +00001527 if (s->to_dst_file && !migration_in_postcopy()) {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301528 qemu_file_set_rate_limit(s->to_dst_file,
1529 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
1530 }
1531 }
Peter Xu476c72a2017-07-18 11:39:05 +08001532
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301533 if (params->has_downtime_limit) {
1534 s->parameters.downtime_limit = params->downtime_limit;
1535 }
zhanghailiang68b53592016-10-27 14:43:01 +08001536
1537 if (params->has_x_checkpoint_delay) {
1538 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
zhanghailiang479125d2017-01-17 20:57:42 +08001539 if (migration_in_colo_state()) {
1540 colo_checkpoint_notify(s);
1541 }
zhanghailiang68b53592016-10-27 14:43:01 +08001542 }
Peter Xu476c72a2017-07-18 11:39:05 +08001543
Juan Quintela2833c592017-04-05 18:32:37 +02001544 if (params->has_block_incremental) {
1545 s->parameters.block_incremental = params->block_incremental;
1546 }
Juan Quintelacbfd6c92019-02-06 13:54:06 +01001547 if (params->has_multifd_channels) {
1548 s->parameters.multifd_channels = params->multifd_channels;
Juan Quintela4075fb12016-01-15 08:56:17 +01001549 }
Juan Quintela96eef042019-01-16 10:35:55 +01001550 if (params->has_multifd_compression) {
1551 s->parameters.multifd_compression = params->multifd_compression;
1552 }
Juan Quintela73af8dd2017-10-05 21:30:10 +02001553 if (params->has_xbzrle_cache_size) {
1554 s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
1555 xbzrle_cache_resize(params->xbzrle_cache_size, errp);
1556 }
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01001557 if (params->has_max_postcopy_bandwidth) {
1558 s->parameters.max_postcopy_bandwidth = params->max_postcopy_bandwidth;
Dr. David Alan Gilbertc38c1c12019-03-08 10:12:10 +00001559 if (s->to_dst_file && migration_in_postcopy()) {
1560 qemu_file_set_rate_limit(s->to_dst_file,
1561 s->parameters.max_postcopy_bandwidth / XFER_LIMIT_RATIO);
1562 }
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01001563 }
Li Qiang4cbc9c72018-08-01 06:00:20 -07001564 if (params->has_max_cpu_throttle) {
1565 s->parameters.max_cpu_throttle = params->max_cpu_throttle;
1566 }
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00001567 if (params->has_announce_initial) {
1568 s->parameters.announce_initial = params->announce_initial;
1569 }
1570 if (params->has_announce_max) {
1571 s->parameters.announce_max = params->announce_max;
1572 }
1573 if (params->has_announce_rounds) {
1574 s->parameters.announce_rounds = params->announce_rounds;
1575 }
1576 if (params->has_announce_step) {
1577 s->parameters.announce_step = params->announce_step;
1578 }
Max Reitz31e4c352020-08-20 17:07:23 +02001579
1580 if (params->has_block_bitmap_mapping) {
1581 qapi_free_BitmapMigrationNodeAliasList(
1582 s->parameters.block_bitmap_mapping);
1583
1584 s->parameters.has_block_bitmap_mapping = true;
1585 s->parameters.block_bitmap_mapping =
1586 QAPI_CLONE(BitmapMigrationNodeAliasList,
1587 params->block_bitmap_mapping);
1588 }
Liang Li85de8322015-03-23 16:32:28 +08001589}
1590
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001591void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
Peter Xu476c72a2017-07-18 11:39:05 +08001592{
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001593 MigrationParameters tmp;
1594
Markus Armbruster01fa5592017-07-18 14:42:04 +02001595 /* TODO Rewrite "" to null instead */
1596 if (params->has_tls_creds
1597 && params->tls_creds->type == QTYPE_QNULL) {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001598 qobject_unref(params->tls_creds->u.n);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001599 params->tls_creds->type = QTYPE_QSTRING;
1600 params->tls_creds->u.s = strdup("");
1601 }
1602 /* TODO Rewrite "" to null instead */
1603 if (params->has_tls_hostname
1604 && params->tls_hostname->type == QTYPE_QNULL) {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001605 qobject_unref(params->tls_hostname->u.n);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001606 params->tls_hostname->type = QTYPE_QSTRING;
1607 params->tls_hostname->u.s = strdup("");
1608 }
1609
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001610 migrate_params_test_apply(params, &tmp);
1611
1612 if (!migrate_params_check(&tmp, errp)) {
Peter Xu476c72a2017-07-18 11:39:05 +08001613 /* Invalid parameter */
1614 return;
1615 }
1616
Juan Quintela73af8dd2017-10-05 21:30:10 +02001617 migrate_params_apply(params, errp);
Peter Xu476c72a2017-07-18 11:39:05 +08001618}
1619
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001620
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001621void qmp_migrate_start_postcopy(Error **errp)
1622{
1623 MigrationState *s = migrate_get_current();
1624
Vladimir Sementsov-Ogievskiy16b0fd32018-03-13 15:34:01 -04001625 if (!migrate_postcopy()) {
Dr. David Alan Gilberta54d3402015-11-12 11:34:44 +00001626 error_setg(errp, "Enable postcopy with migrate_set_capability before"
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001627 " the start of migration");
1628 return;
1629 }
1630
1631 if (s->state == MIGRATION_STATUS_NONE) {
1632 error_setg(errp, "Postcopy must be started after migration has been"
1633 " started");
1634 return;
1635 }
1636 /*
1637 * we don't error if migration has finished since that would be racy
1638 * with issuing this command.
1639 */
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001640 qatomic_set(&s->start_postcopy, true);
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001641}
1642
aliguori065e2812008-11-11 16:46:33 +00001643/* shared migration helpers */
1644
zhanghailiang48781e52015-12-16 11:47:33 +00001645void migrate_set_state(int *state, int old_state, int new_state)
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +00001646{
Peter Xua31fede2017-08-30 16:32:01 +08001647 assert(new_state < MIGRATION_STATUS__MAX);
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001648 if (qatomic_cmpxchg(state, old_state, new_state) == old_state) {
Peter Xua31fede2017-08-30 16:32:01 +08001649 trace_migrate_set_state(MigrationStatus_str(new_state));
Juan Quintelab05dc722015-07-07 14:44:05 +02001650 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +00001651 }
1652}
1653
Eric Blakeeaedde52020-11-12 19:13:36 -06001654static MigrationCapabilityStatus *migrate_cap_add(MigrationCapability index,
1655 bool state)
Juan Quintela2833c592017-04-05 18:32:37 +02001656{
Eric Blakeeaedde52020-11-12 19:13:36 -06001657 MigrationCapabilityStatus *cap;
Juan Quintela2833c592017-04-05 18:32:37 +02001658
Eric Blakeeaedde52020-11-12 19:13:36 -06001659 cap = g_new0(MigrationCapabilityStatus, 1);
1660 cap->capability = index;
1661 cap->state = state;
Peter Xu4e4a3d32017-07-18 11:39:09 +08001662
1663 return cap;
1664}
1665
1666void migrate_set_block_enabled(bool value, Error **errp)
1667{
Eric Blakeeaedde52020-11-12 19:13:36 -06001668 MigrationCapabilityStatusList *cap = NULL;
Peter Xu4e4a3d32017-07-18 11:39:09 +08001669
Eric Blakeeaedde52020-11-12 19:13:36 -06001670 QAPI_LIST_PREPEND(cap, migrate_cap_add(MIGRATION_CAPABILITY_BLOCK, value));
Juan Quintela2833c592017-04-05 18:32:37 +02001671 qmp_migrate_set_capabilities(cap, errp);
1672 qapi_free_MigrationCapabilityStatusList(cap);
1673}
1674
1675static void migrate_set_block_incremental(MigrationState *s, bool value)
1676{
1677 s->parameters.block_incremental = value;
1678}
1679
1680static void block_cleanup_parameters(MigrationState *s)
1681{
1682 if (s->must_remove_block_options) {
1683 /* setting to false can never fail */
1684 migrate_set_block_enabled(false, &error_abort);
1685 migrate_set_block_incremental(s, false);
1686 s->must_remove_block_options = false;
1687 }
1688}
1689
Yury Kotovfd392cf2019-04-08 14:33:43 +03001690static void migrate_fd_cleanup(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +00001691{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001692 qemu_bh_delete(s->cleanup_bh);
1693 s->cleanup_bh = NULL;
1694
Peter Xu0ceccd82018-01-03 20:20:06 +08001695 qemu_savevm_state_cleanup();
1696
zhanghailiang89a02a92016-01-15 11:37:42 +08001697 if (s->to_dst_file) {
Peter Xu62df0662018-05-02 18:47:38 +08001698 QEMUFile *tmp;
Juan Quintelaf986c3d2016-01-14 16:52:55 +01001699
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001700 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001701 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001702 if (s->migration_thread_running) {
1703 qemu_thread_join(&s->thread);
1704 s->migration_thread_running = false;
1705 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001706 qemu_mutex_lock_iothread();
1707
Fei Li1398b2e2019-01-13 22:08:47 +08001708 multifd_save_cleanup();
Peter Xu62df0662018-05-02 18:47:38 +08001709 qemu_mutex_lock(&s->qemu_file_lock);
1710 tmp = s->to_dst_file;
zhanghailiang89a02a92016-01-15 11:37:42 +08001711 s->to_dst_file = NULL;
Peter Xu62df0662018-05-02 18:47:38 +08001712 qemu_mutex_unlock(&s->qemu_file_lock);
1713 /*
1714 * Close the file handle without the lock to make sure the
1715 * critical section won't block for long.
1716 */
1717 qemu_fclose(tmp);
aliguori065e2812008-11-11 16:46:33 +00001718 }
1719
Wei Yang8f8d5282019-07-17 08:53:41 +08001720 assert(!migration_is_active(s));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001721
Liang Li94f5a432015-11-02 15:37:00 +08001722 if (s->state == MIGRATION_STATUS_CANCELLING) {
zhanghailiang48781e52015-12-16 11:47:33 +00001723 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
Liang Li94f5a432015-11-02 15:37:00 +08001724 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001725 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001726
Juan Quintela87db1a72017-09-05 12:50:22 +02001727 if (s->error) {
1728 /* It is used on info migrate. We can't free it */
1729 error_report_err(error_copy(s->error));
1730 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001731 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +02001732 block_cleanup_parameters(s);
aliguori065e2812008-11-11 16:46:33 +00001733}
1734
Yury Kotovfd392cf2019-04-08 14:33:43 +03001735static void migrate_fd_cleanup_schedule(MigrationState *s)
1736{
1737 /*
1738 * Ref the state for bh, because it may be called when
1739 * there're already no other refs
1740 */
1741 object_ref(OBJECT(s));
1742 qemu_bh_schedule(s->cleanup_bh);
1743}
1744
1745static void migrate_fd_cleanup_bh(void *opaque)
1746{
1747 MigrationState *s = opaque;
1748 migrate_fd_cleanup(s);
1749 object_unref(OBJECT(s));
1750}
1751
Juan Quintela87db1a72017-09-05 12:50:22 +02001752void migrate_set_error(MigrationState *s, const Error *error)
1753{
Daniel Brodsky6e8a3552020-04-03 21:21:08 -07001754 QEMU_LOCK_GUARD(&s->error_mutex);
Juan Quintela87db1a72017-09-05 12:50:22 +02001755 if (!s->error) {
1756 s->error = error_copy(error);
1757 }
Juan Quintela87db1a72017-09-05 12:50:22 +02001758}
1759
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001760void migrate_fd_error(MigrationState *s, const Error *error)
aliguori065e2812008-11-11 16:46:33 +00001761{
Peter Maydell25174052016-10-21 18:41:45 +01001762 trace_migrate_fd_error(error_get_pretty(error));
zhanghailiang89a02a92016-01-15 11:37:42 +08001763 assert(s->to_dst_file == NULL);
zhanghailiang48781e52015-12-16 11:47:33 +00001764 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1765 MIGRATION_STATUS_FAILED);
Juan Quintela87db1a72017-09-05 12:50:22 +02001766 migrate_set_error(s, error);
Juan Quintela458cf282011-02-22 23:32:54 +01001767}
1768
Juan Quintela0edda1c2010-05-11 16:28:39 +02001769static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +00001770{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001771 int old_state ;
zhanghailiang89a02a92016-01-15 11:37:42 +08001772 QEMUFile *f = migrate_get_current()->to_dst_file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001773 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +00001774
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001775 if (s->rp_state.from_dst_file) {
1776 /* shutdown the rp socket, so causing the rp thread to shutdown */
1777 qemu_file_shutdown(s->rp_state.from_dst_file);
1778 }
1779
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001780 do {
1781 old_state = s->state;
Juan Quintela392d87e2020-01-21 15:39:23 +01001782 if (!migration_is_running(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001783 break;
1784 }
Dr. David Alan Gilberta7b36b42017-10-20 10:05:55 +01001785 /* If the migration is paused, kick it out of the pause */
1786 if (old_state == MIGRATION_STATUS_PRE_SWITCHOVER) {
1787 qemu_sem_post(&s->pause_sem);
1788 }
zhanghailiang48781e52015-12-16 11:47:33 +00001789 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
zhanghailiang31194732015-03-13 16:08:38 +08001790 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001791
1792 /*
1793 * If we're unlucky the migration code might be stuck somewhere in a
1794 * send/write while the network has failed and is waiting to timeout;
1795 * if we've got shutdown(2) available then we can force it to quit.
1796 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1797 * called in a bh, so there is no race against this cancel.
1798 */
zhanghailiang31194732015-03-13 16:08:38 +08001799 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001800 qemu_file_shutdown(f);
1801 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08001802 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
1803 Error *local_err = NULL;
1804
1805 bdrv_invalidate_cache_all(&local_err);
1806 if (local_err) {
1807 error_report_err(local_err);
1808 } else {
1809 s->block_inactive = false;
1810 }
1811 }
aliguori065e2812008-11-11 16:46:33 +00001812}
1813
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001814void add_migration_state_change_notifier(Notifier *notify)
1815{
1816 notifier_list_add(&migration_state_notifiers, notify);
1817}
1818
1819void remove_migration_state_change_notifier(Notifier *notify)
1820{
Paolo Bonzini31552522012-01-13 17:34:01 +01001821 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001822}
1823
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +02001824bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001825{
zhanghailiang31194732015-03-13 16:08:38 +08001826 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001827}
1828
Juan Quintela70736932011-02-23 00:43:59 +01001829bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001830{
zhanghailiang31194732015-03-13 16:08:38 +08001831 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001832}
Juan Quintela0edda1c2010-05-11 16:28:39 +02001833
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001834bool migration_has_failed(MigrationState *s)
1835{
zhanghailiang31194732015-03-13 16:08:38 +08001836 return (s->state == MIGRATION_STATUS_CANCELLED ||
1837 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001838}
1839
Juan Quintela57273092017-03-20 22:25:28 +01001840bool migration_in_postcopy(void)
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001841{
Juan Quintela57273092017-03-20 22:25:28 +01001842 MigrationState *s = migrate_get_current();
1843
Dr. David Alan Gilbert3748fef2019-09-23 18:49:42 +01001844 switch (s->state) {
1845 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1846 case MIGRATION_STATUS_POSTCOPY_PAUSED:
1847 case MIGRATION_STATUS_POSTCOPY_RECOVER:
1848 return true;
1849 default:
1850 return false;
1851 }
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001852}
1853
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001854bool migration_in_postcopy_after_devices(MigrationState *s)
1855{
Juan Quintela57273092017-03-20 22:25:28 +01001856 return migration_in_postcopy() && s->postcopy_after_devices;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001857}
1858
David Hildenbrand06df2e62020-06-26 09:22:33 +02001859bool migration_in_incoming_postcopy(void)
1860{
1861 PostcopyState ps = postcopy_state_get();
1862
1863 return ps >= POSTCOPY_INCOMING_DISCARD && ps < POSTCOPY_INCOMING_END;
1864}
1865
Juan Quintelafab35002017-03-22 17:36:57 +01001866bool migration_is_idle(void)
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301867{
Markus Armbrusterdaff7f02019-04-01 11:08:25 +02001868 MigrationState *s = current_migration;
1869
1870 if (!s) {
1871 return true;
1872 }
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301873
1874 switch (s->state) {
1875 case MIGRATION_STATUS_NONE:
1876 case MIGRATION_STATUS_CANCELLED:
1877 case MIGRATION_STATUS_COMPLETED:
1878 case MIGRATION_STATUS_FAILED:
1879 return true;
1880 case MIGRATION_STATUS_SETUP:
1881 case MIGRATION_STATUS_CANCELLING:
1882 case MIGRATION_STATUS_ACTIVE:
1883 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1884 case MIGRATION_STATUS_COLO:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +01001885 case MIGRATION_STATUS_PRE_SWITCHOVER:
1886 case MIGRATION_STATUS_DEVICE:
Jens Freimannc7e0acd2019-10-29 12:49:02 +01001887 case MIGRATION_STATUS_WAIT_UNPLUG:
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301888 return false;
1889 case MIGRATION_STATUS__MAX:
1890 g_assert_not_reached();
1891 }
1892
1893 return false;
1894}
1895
Wei Yang8f8d5282019-07-17 08:53:41 +08001896bool migration_is_active(MigrationState *s)
1897{
1898 return (s->state == MIGRATION_STATUS_ACTIVE ||
1899 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1900}
1901
Peter Xu3e0c8052018-02-08 18:31:15 +08001902void migrate_init(MigrationState *s)
Juan Quintela0edda1c2010-05-11 16:28:39 +02001903{
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001904 /*
1905 * Reinitialise all migration state, except
1906 * parameters/capabilities that the user set, and
1907 * locks.
1908 */
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001909 s->cleanup_bh = 0;
zhanghailiang89a02a92016-01-15 11:37:42 +08001910 s->to_dst_file = NULL;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001911 s->state = MIGRATION_STATUS_NONE;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001912 s->rp_state.from_dst_file = NULL;
1913 s->rp_state.error = false;
1914 s->mbps = 0.0;
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08001915 s->pages_per_second = 0.0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001916 s->downtime = 0;
1917 s->expected_downtime = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001918 s->setup_time = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001919 s->start_postcopy = false;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001920 s->postcopy_after_devices = false;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001921 s->migration_thread_running = false;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001922 error_free(s->error);
1923 s->error = NULL;
Chuan Zhengd8053e72020-09-15 11:03:57 +08001924 s->hostname = NULL;
Juan Quintela1299c632011-11-09 21:29:01 +01001925
zhanghailiang48781e52015-12-16 11:47:33 +00001926 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +02001927
Peter Xu4af246a2018-01-03 20:20:08 +08001928 s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1929 s->total_time = 0;
Peter Xu7287cbd2018-01-03 20:20:09 +08001930 s->vm_was_running = false;
Peter Xub15df1a2018-01-03 20:20:13 +08001931 s->iteration_initial_bytes = 0;
1932 s->threshold_size = 0;
Juan Quintela0edda1c2010-05-11 16:28:39 +02001933}
Juan Quintelacab30142011-02-22 23:54:21 +01001934
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001935static GSList *migration_blockers;
1936
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301937int migrate_add_blocker(Error *reason, Error **errp)
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001938{
Markus Armbruster811f8652019-04-01 11:08:24 +02001939 if (only_migratable) {
Markus Armbruster4b576642018-10-17 10:26:25 +02001940 error_propagate_prepend(errp, error_copy(reason),
1941 "disallowing migration blocker "
Greg Kurzb6eca812019-05-16 12:58:05 +02001942 "(--only-migratable) for: ");
Ashijeet Acharyab67b8c32017-01-16 17:01:54 +05301943 return -EACCES;
1944 }
1945
Juan Quintelafab35002017-03-22 17:36:57 +01001946 if (migration_is_idle()) {
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301947 migration_blockers = g_slist_prepend(migration_blockers, reason);
1948 return 0;
1949 }
1950
Markus Armbruster4b576642018-10-17 10:26:25 +02001951 error_propagate_prepend(errp, error_copy(reason),
1952 "disallowing migration blocker "
1953 "(migration in progress) for: ");
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301954 return -EBUSY;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001955}
1956
1957void migrate_del_blocker(Error *reason)
1958{
1959 migration_blockers = g_slist_remove(migration_blockers, reason);
1960}
1961
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001962void qmp_migrate_incoming(const char *uri, Error **errp)
1963{
1964 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001965 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001966
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001967 if (!once) {
1968 error_setg(errp, "The incoming migration has already been started");
Yury Kotov603d5a42019-11-13 20:53:25 +03001969 return;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001970 }
Paolo Bonzinie69d50d2020-10-27 04:22:57 -04001971 if (!runstate_check(RUN_STATE_INMIGRATE)) {
1972 error_setg(errp, "'-incoming' was not specified on the command line");
1973 return;
1974 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001975
1976 qemu_start_incoming_migration(uri, &local_err);
1977
1978 if (local_err) {
1979 error_propagate(errp, local_err);
1980 return;
1981 }
1982
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001983 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001984}
1985
Peter Xu02affd42018-05-02 18:47:36 +08001986void qmp_migrate_recover(const char *uri, Error **errp)
1987{
1988 MigrationIncomingState *mis = migration_incoming_get_current();
1989
1990 if (mis->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
1991 error_setg(errp, "Migrate recover can only be run "
1992 "when postcopy is paused.");
1993 return;
1994 }
1995
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001996 if (qatomic_cmpxchg(&mis->postcopy_recover_triggered,
Peter Xu02affd42018-05-02 18:47:36 +08001997 false, true) == true) {
1998 error_setg(errp, "Migrate recovery is triggered already");
1999 return;
2000 }
2001
2002 /*
2003 * Note that this call will never start a real migration; it will
2004 * only re-setup the migration stream and poke existing migration
2005 * to continue using that newly established channel.
2006 */
2007 qemu_start_incoming_migration(uri, errp);
2008}
2009
Peter Xubfbf89c2018-05-02 18:47:39 +08002010void qmp_migrate_pause(Error **errp)
2011{
2012 MigrationState *ms = migrate_get_current();
2013 MigrationIncomingState *mis = migration_incoming_get_current();
2014 int ret;
2015
2016 if (ms->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2017 /* Source side, during postcopy */
2018 qemu_mutex_lock(&ms->qemu_file_lock);
2019 ret = qemu_file_shutdown(ms->to_dst_file);
2020 qemu_mutex_unlock(&ms->qemu_file_lock);
2021 if (ret) {
2022 error_setg(errp, "Failed to pause source migration");
2023 }
2024 return;
2025 }
2026
2027 if (mis->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2028 ret = qemu_file_shutdown(mis->from_src_file);
2029 if (ret) {
2030 error_setg(errp, "Failed to pause destination migration");
2031 }
2032 return;
2033 }
2034
2035 error_setg(errp, "migrate-pause is currently only supported "
2036 "during postcopy-active state");
2037}
2038
Greg Kurz24f39022016-05-04 21:44:19 +02002039bool migration_is_blocked(Error **errp)
2040{
2041 if (qemu_savevm_state_blocked(errp)) {
2042 return true;
2043 }
2044
2045 if (migration_blockers) {
Eduardo Habkost250561e2017-06-08 10:39:05 -03002046 error_propagate(errp, error_copy(migration_blockers->data));
Greg Kurz24f39022016-05-04 21:44:19 +02002047 return true;
2048 }
2049
2050 return false;
2051}
2052
Peter Xud3e35b82018-05-02 18:47:24 +08002053/* Returns true if continue to migrate, or false if error detected */
2054static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc,
2055 bool resume, Error **errp)
2056{
2057 Error *local_err = NULL;
2058
2059 if (resume) {
2060 if (s->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
2061 error_setg(errp, "Cannot resume if there is no "
2062 "paused migration");
2063 return false;
2064 }
Peter Xu97ca2112018-07-23 20:33:03 +08002065
2066 /*
2067 * Postcopy recovery won't work well with release-ram
2068 * capability since release-ram will drop the page buffer as
2069 * long as the page is put into the send buffer. So if there
2070 * is a network failure happened, any page buffers that have
2071 * not yet reached the destination VM but have already been
2072 * sent from the source VM will be lost forever. Let's refuse
2073 * the client from resuming such a postcopy migration.
2074 * Luckily release-ram was designed to only be used when src
2075 * and destination VMs are on the same host, so it should be
2076 * fine.
2077 */
2078 if (migrate_release_ram()) {
2079 error_setg(errp, "Postcopy recovery cannot work "
2080 "when release-ram capability is set");
2081 return false;
2082 }
2083
Peter Xud3e35b82018-05-02 18:47:24 +08002084 /* This is a resume, skip init status */
2085 return true;
2086 }
2087
Juan Quintela392d87e2020-01-21 15:39:23 +01002088 if (migration_is_running(s->state)) {
Peter Xud3e35b82018-05-02 18:47:24 +08002089 error_setg(errp, QERR_MIGRATION_ACTIVE);
2090 return false;
2091 }
2092
2093 if (runstate_check(RUN_STATE_INMIGRATE)) {
2094 error_setg(errp, "Guest is waiting for an incoming migration");
2095 return false;
2096 }
2097
2098 if (migration_is_blocked(errp)) {
2099 return false;
2100 }
2101
2102 if (blk || blk_inc) {
2103 if (migrate_use_block() || migrate_use_block_incremental()) {
2104 error_setg(errp, "Command options are incompatible with "
2105 "current migration capabilities");
2106 return false;
2107 }
2108 migrate_set_block_enabled(true, &local_err);
2109 if (local_err) {
2110 error_propagate(errp, local_err);
2111 return false;
2112 }
2113 s->must_remove_block_options = true;
2114 }
2115
2116 if (blk_inc) {
2117 migrate_set_block_incremental(s, true);
2118 }
2119
2120 migrate_init(s);
Ivan Ren87f3bd82019-08-02 18:18:41 +08002121 /*
2122 * set ram_counters memory to zero for a
2123 * new migration
2124 */
2125 memset(&ram_counters, 0, sizeof(ram_counters));
Peter Xud3e35b82018-05-02 18:47:24 +08002126
2127 return true;
2128}
2129
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002130void qmp_migrate(const char *uri, bool has_blk, bool blk,
2131 bool has_inc, bool inc, bool has_detach, bool detach,
Peter Xu7a4da282018-05-02 18:47:23 +08002132 bool has_resume, bool resume, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002133{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +02002134 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +02002135 MigrationState *s = migrate_get_current();
Longpeng(Mike)d658f652020-08-06 15:40:29 +08002136 const char *p = NULL;
Juan Quintelacab30142011-02-22 23:54:21 +01002137
Peter Xud3e35b82018-05-02 18:47:24 +08002138 if (!migrate_prepare(s, has_blk && blk, has_inc && inc,
2139 has_resume && resume, errp)) {
2140 /* Error detected, put into errp */
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002141 return;
Juan Quintelacab30142011-02-22 23:54:21 +01002142 }
Juan Quintelacab30142011-02-22 23:54:21 +01002143
Longpeng(Mike)d658f652020-08-06 15:40:29 +08002144 if (strstart(uri, "tcp:", &p) ||
Longpeng(Mike)9ba3b2b2020-08-06 15:40:30 +08002145 strstart(uri, "unix:", NULL) ||
2146 strstart(uri, "vsock:", NULL)) {
Longpeng(Mike)d658f652020-08-06 15:40:29 +08002147 socket_start_outgoing_migration(s, p ? p : uri, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04002148#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08002149 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04002150 rdma_start_outgoing_migration(s, p, &local_err);
2151#endif
Juan Quintelacab30142011-02-22 23:54:21 +01002152 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002153 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01002154 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002155 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01002156 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002157 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
2158 "a valid migration protocol");
zhanghailiang48781e52015-12-16 11:47:33 +00002159 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2160 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09576e72018-03-16 20:21:14 +00002161 block_cleanup_parameters(s);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002162 return;
Juan Quintelacab30142011-02-22 23:54:21 +01002163 }
2164
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002165 if (local_err) {
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01002166 migrate_fd_error(s, local_err);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002167 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002168 return;
Juan Quintela1299c632011-11-09 21:29:01 +01002169 }
Juan Quintelacab30142011-02-22 23:54:21 +01002170}
2171
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02002172void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002173{
Juan Quintela17549e82011-10-05 13:50:43 +02002174 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01002175}
2176
Dr. David Alan Gilbert89cfc022017-10-20 10:05:53 +01002177void qmp_migrate_continue(MigrationStatus state, Error **errp)
2178{
2179 MigrationState *s = migrate_get_current();
2180 if (s->state != state) {
2181 error_setg(errp, "Migration not in expected state: %s",
2182 MigrationStatus_str(s->state));
2183 return;
2184 }
2185 qemu_sem_post(&s->pause_sem);
2186}
2187
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002188void qmp_migrate_set_cache_size(int64_t value, Error **errp)
2189{
Juan Quintela73af8dd2017-10-05 21:30:10 +02002190 MigrateSetParameters p = {
2191 .has_xbzrle_cache_size = true,
2192 .xbzrle_cache_size = value,
2193 };
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002194
Juan Quintela73af8dd2017-10-05 21:30:10 +02002195 qmp_migrate_set_parameters(&p, errp);
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002196}
2197
2198int64_t qmp_query_migrate_cache_size(Error **errp)
2199{
2200 return migrate_xbzrle_cache_size();
2201}
2202
Luiz Capitulino3dc85382011-11-28 11:59:37 -02002203void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002204{
Markus Armbruster1bda8b32017-07-18 13:42:11 +02002205 MigrateSetParameters p = {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302206 .has_max_bandwidth = true,
2207 .max_bandwidth = value,
2208 };
Juan Quintelacab30142011-02-22 23:54:21 +01002209
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302210 qmp_migrate_set_parameters(&p, errp);
Juan Quintelacab30142011-02-22 23:54:21 +01002211}
2212
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02002213void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002214{
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03002215 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
Mao Zhongyi2ee30cf2020-03-31 16:22:05 +08002216 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
2217 "downtime_limit",
2218 "an integer in the range of 0 to "
2219 stringify(MAX_MIGRATE_DOWNTIME_SECONDS)" seconds");
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03002220 return;
2221 }
2222
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302223 value *= 1000; /* Convert to milliseconds */
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302224
Markus Armbruster1bda8b32017-07-18 13:42:11 +02002225 MigrateSetParameters p = {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302226 .has_downtime_limit = true,
Fangrui Song2667c982019-11-22 09:00:38 +01002227 .downtime_limit = (int64_t)value,
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302228 };
2229
2230 qmp_migrate_set_parameters(&p, errp);
aliguori5bb79102008-10-13 03:12:02 +00002231}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03002232
Pavel Butsykin53f09a12017-02-03 18:23:20 +03002233bool migrate_release_ram(void)
2234{
2235 MigrationState *s;
2236
2237 s = migrate_get_current();
2238
2239 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
2240}
2241
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00002242bool migrate_postcopy_ram(void)
2243{
2244 MigrationState *s;
2245
2246 s = migrate_get_current();
2247
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +00002248 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00002249}
2250
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002251bool migrate_postcopy(void)
2252{
Vladimir Sementsov-Ogievskiydd6bb912018-03-13 15:34:00 -04002253 return migrate_postcopy_ram() || migrate_dirty_bitmaps();
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002254}
2255
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06002256bool migrate_auto_converge(void)
2257{
2258 MigrationState *s;
2259
2260 s = migrate_get_current();
2261
2262 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
2263}
2264
Peter Lieven323004a2013-07-18 09:48:50 +02002265bool migrate_zero_blocks(void)
2266{
2267 MigrationState *s;
2268
2269 s = migrate_get_current();
2270
2271 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
2272}
2273
Alexey Perevalovf22f9282018-03-22 21:17:22 +03002274bool migrate_postcopy_blocktime(void)
2275{
2276 MigrationState *s;
2277
2278 s = migrate_get_current();
2279
2280 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME];
2281}
2282
Liang Li8706d2d2015-03-23 16:32:17 +08002283bool migrate_use_compression(void)
2284{
Liang Lidde4e692015-03-23 16:32:26 +08002285 MigrationState *s;
2286
2287 s = migrate_get_current();
2288
2289 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08002290}
2291
2292int migrate_compress_level(void)
2293{
2294 MigrationState *s;
2295
2296 s = migrate_get_current();
2297
Daniel P. Berrange2594f562016-04-27 11:05:14 +01002298 return s->parameters.compress_level;
Liang Li8706d2d2015-03-23 16:32:17 +08002299}
2300
2301int migrate_compress_threads(void)
2302{
2303 MigrationState *s;
2304
2305 s = migrate_get_current();
2306
Daniel P. Berrange2594f562016-04-27 11:05:14 +01002307 return s->parameters.compress_threads;
Liang Li8706d2d2015-03-23 16:32:17 +08002308}
2309
Xiao Guangrong1d588722018-08-21 16:10:20 +08002310int migrate_compress_wait_thread(void)
2311{
2312 MigrationState *s;
2313
2314 s = migrate_get_current();
2315
2316 return s->parameters.compress_wait_thread;
2317}
2318
Liang Li3fcb38c2015-03-23 16:32:18 +08002319int migrate_decompress_threads(void)
2320{
2321 MigrationState *s;
2322
2323 s = migrate_get_current();
2324
Daniel P. Berrange2594f562016-04-27 11:05:14 +01002325 return s->parameters.decompress_threads;
Liang Li3fcb38c2015-03-23 16:32:18 +08002326}
2327
Vladimir Sementsov-Ogievskiy55efc8c2018-03-13 15:34:00 -04002328bool migrate_dirty_bitmaps(void)
2329{
2330 MigrationState *s;
2331
2332 s = migrate_get_current();
2333
2334 return s->enabled_capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS];
2335}
2336
Yury Kotov18269062019-02-15 20:45:45 +03002337bool migrate_ignore_shared(void)
2338{
2339 MigrationState *s;
2340
2341 s = migrate_get_current();
2342
2343 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_IGNORE_SHARED];
2344}
2345
Yury Kotovb9d68df2019-09-03 19:22:44 +03002346bool migrate_validate_uuid(void)
2347{
2348 MigrationState *s;
2349
2350 s = migrate_get_current();
2351
2352 return s->enabled_capabilities[MIGRATION_CAPABILITY_VALIDATE_UUID];
2353}
2354
Juan Quintelab05dc722015-07-07 14:44:05 +02002355bool migrate_use_events(void)
2356{
2357 MigrationState *s;
2358
2359 s = migrate_get_current();
2360
2361 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
2362}
2363
Juan Quintela30126bb2016-01-14 12:23:00 +01002364bool migrate_use_multifd(void)
2365{
2366 MigrationState *s;
2367
2368 s = migrate_get_current();
2369
Juan Quintelacbfd6c92019-02-06 13:54:06 +01002370 return s->enabled_capabilities[MIGRATION_CAPABILITY_MULTIFD];
Juan Quintela30126bb2016-01-14 12:23:00 +01002371}
2372
Dr. David Alan Gilbert93fbd032017-10-20 10:05:50 +01002373bool migrate_pause_before_switchover(void)
2374{
2375 MigrationState *s;
2376
2377 s = migrate_get_current();
2378
2379 return s->enabled_capabilities[
2380 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER];
2381}
2382
Juan Quintela4075fb12016-01-15 08:56:17 +01002383int migrate_multifd_channels(void)
2384{
2385 MigrationState *s;
2386
2387 s = migrate_get_current();
2388
Juan Quintelacbfd6c92019-02-06 13:54:06 +01002389 return s->parameters.multifd_channels;
Juan Quintela4075fb12016-01-15 08:56:17 +01002390}
2391
Juan Quintelaab7cbb02019-05-15 13:37:46 +02002392MultiFDCompression migrate_multifd_compression(void)
2393{
2394 MigrationState *s;
2395
2396 s = migrate_get_current();
2397
2398 return s->parameters.multifd_compression;
2399}
2400
Juan Quintela9004db42020-01-23 17:08:52 +01002401int migrate_multifd_zlib_level(void)
2402{
2403 MigrationState *s;
2404
2405 s = migrate_get_current();
2406
2407 return s->parameters.multifd_zlib_level;
2408}
2409
Juan Quintela6a9ad152020-01-23 17:41:36 +01002410int migrate_multifd_zstd_level(void)
2411{
2412 MigrationState *s;
2413
2414 s = migrate_get_current();
2415
2416 return s->parameters.multifd_zstd_level;
2417}
2418
Orit Wasserman17ad9b32012-08-06 21:42:53 +03002419int migrate_use_xbzrle(void)
2420{
2421 MigrationState *s;
2422
2423 s = migrate_get_current();
2424
2425 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
2426}
2427
2428int64_t migrate_xbzrle_cache_size(void)
2429{
2430 MigrationState *s;
2431
2432 s = migrate_get_current();
2433
Juan Quintela73af8dd2017-10-05 21:30:10 +02002434 return s->parameters.xbzrle_cache_size;
Orit Wasserman17ad9b32012-08-06 21:42:53 +03002435}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002436
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01002437static int64_t migrate_max_postcopy_bandwidth(void)
2438{
2439 MigrationState *s;
2440
2441 s = migrate_get_current();
2442
2443 return s->parameters.max_postcopy_bandwidth;
2444}
2445
Juan Quintela2833c592017-04-05 18:32:37 +02002446bool migrate_use_block(void)
2447{
2448 MigrationState *s;
2449
2450 s = migrate_get_current();
2451
2452 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
2453}
2454
Peter Xuc788ada2017-06-26 18:28:55 +08002455bool migrate_use_return_path(void)
2456{
2457 MigrationState *s;
2458
2459 s = migrate_get_current();
2460
2461 return s->enabled_capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
2462}
2463
Juan Quintela2833c592017-04-05 18:32:37 +02002464bool migrate_use_block_incremental(void)
2465{
2466 MigrationState *s;
2467
2468 s = migrate_get_current();
2469
2470 return s->parameters.block_incremental;
2471}
2472
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002473/* migration thread support */
2474/*
2475 * Something bad happened to the RP stream, mark an error
2476 * The caller shall print or trace something to indicate why
2477 */
2478static void mark_source_rp_bad(MigrationState *s)
2479{
2480 s->rp_state.error = true;
2481}
2482
2483static struct rp_cmd_args {
2484 ssize_t len; /* -1 = variable */
2485 const char *name;
2486} rp_cmd_args[] = {
2487 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
2488 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
2489 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002490 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
2491 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Peter Xua335deb2018-05-02 18:47:28 +08002492 [MIG_RP_MSG_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" },
Peter Xu13955b82018-05-02 18:47:30 +08002493 [MIG_RP_MSG_RESUME_ACK] = { .len = 4, .name = "RESUME_ACK" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002494 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
2495};
2496
2497/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002498 * Process a request for pages received on the return path,
2499 * We're allowed to send more than requested (e.g. to round to our page size)
2500 * and we don't need to send pages that have already been sent.
2501 */
2502static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
2503 ram_addr_t start, size_t len)
2504{
Wei Yang038adc22019-10-13 10:11:45 +08002505 long our_host_ps = qemu_real_host_page_size;
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00002506
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002507 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00002508
2509 /*
2510 * Since we currently insist on matching page sizes, just sanity check
2511 * we're being asked for whole host pages.
2512 */
Bihong Yu395cb452020-10-20 11:10:43 +08002513 if (start & (our_host_ps - 1) ||
2514 (len & (our_host_ps - 1))) {
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00002515 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
2516 " len: %zd", __func__, start, len);
2517 mark_source_rp_bad(ms);
2518 return;
2519 }
2520
Juan Quintela96506892017-03-14 18:41:03 +01002521 if (ram_save_queue_pages(rbname, start, len)) {
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00002522 mark_source_rp_bad(ms);
2523 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002524}
2525
Peter Xu14b17422018-05-02 18:47:21 +08002526/* Return true to retry, false to quit */
2527static bool postcopy_pause_return_path_thread(MigrationState *s)
2528{
2529 trace_postcopy_pause_return_path();
2530
2531 qemu_sem_wait(&s->postcopy_pause_rp_sem);
2532
2533 trace_postcopy_pause_return_path_continued();
2534
2535 return true;
2536}
2537
Peter Xua335deb2018-05-02 18:47:28 +08002538static int migrate_handle_rp_recv_bitmap(MigrationState *s, char *block_name)
2539{
2540 RAMBlock *block = qemu_ram_block_by_name(block_name);
2541
2542 if (!block) {
2543 error_report("%s: invalid block name '%s'", __func__, block_name);
2544 return -EINVAL;
2545 }
2546
2547 /* Fetch the received bitmap and refresh the dirty bitmap */
2548 return ram_dirty_bitmap_reload(s, block);
2549}
2550
Peter Xu13955b82018-05-02 18:47:30 +08002551static int migrate_handle_rp_resume_ack(MigrationState *s, uint32_t value)
2552{
2553 trace_source_return_path_thread_resume_ack(value);
2554
2555 if (value != MIGRATION_RESUME_ACK_VALUE) {
2556 error_report("%s: illegal resume_ack value %"PRIu32,
2557 __func__, value);
2558 return -1;
2559 }
2560
2561 /* Now both sides are active. */
2562 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
2563 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2564
Peter Xu94190692018-05-02 18:47:34 +08002565 /* Notify send thread that time to continue send pages */
2566 qemu_sem_post(&s->rp_state.rp_sem);
Peter Xu13955b82018-05-02 18:47:30 +08002567
2568 return 0;
2569}
2570
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002571/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002572 * Handles messages sent on the return path towards the source VM
2573 *
2574 */
2575static void *source_return_path_thread(void *opaque)
2576{
2577 MigrationState *ms = opaque;
2578 QEMUFile *rp = ms->rp_state.from_dst_file;
2579 uint16_t header_len, header_type;
Peter Xu568b01c2016-03-09 14:12:12 +08002580 uint8_t buf[512];
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002581 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002582 ram_addr_t start = 0; /* =0 to silence warning */
2583 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002584 int res;
2585
2586 trace_source_return_path_thread_entry();
Lidong Chen74637e62018-08-06 21:29:29 +08002587 rcu_register_thread();
Peter Xu14b17422018-05-02 18:47:21 +08002588
2589retry:
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002590 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
2591 migration_is_setup_or_active(ms->state)) {
2592 trace_source_return_path_thread_loop_top();
2593 header_type = qemu_get_be16(rp);
2594 header_len = qemu_get_be16(rp);
2595
Peter Xu7a9ddfb2018-02-08 18:31:05 +08002596 if (qemu_file_get_error(rp)) {
2597 mark_source_rp_bad(ms);
2598 goto out;
2599 }
2600
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002601 if (header_type >= MIG_RP_MSG_MAX ||
2602 header_type == MIG_RP_MSG_INVALID) {
2603 error_report("RP: Received invalid message 0x%04x length 0x%04x",
Mao Zhongyied8b2822020-03-28 01:19:08 +08002604 header_type, header_len);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002605 mark_source_rp_bad(ms);
2606 goto out;
2607 }
2608
2609 if ((rp_cmd_args[header_type].len != -1 &&
2610 header_len != rp_cmd_args[header_type].len) ||
Peter Xu568b01c2016-03-09 14:12:12 +08002611 header_len > sizeof(buf)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002612 error_report("RP: Received '%s' message (0x%04x) with"
Mao Zhongyied8b2822020-03-28 01:19:08 +08002613 "incorrect length %d expecting %zu",
2614 rp_cmd_args[header_type].name, header_type, header_len,
2615 (size_t)rp_cmd_args[header_type].len);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002616 mark_source_rp_bad(ms);
2617 goto out;
2618 }
2619
2620 /* We know we've got a valid header by this point */
2621 res = qemu_get_buffer(rp, buf, header_len);
2622 if (res != header_len) {
2623 error_report("RP: Failed reading data for message 0x%04x"
2624 " read %d expected %d",
2625 header_type, res, header_len);
2626 mark_source_rp_bad(ms);
2627 goto out;
2628 }
2629
2630 /* OK, we have the message and the data */
2631 switch (header_type) {
2632 case MIG_RP_MSG_SHUT:
Peter Maydell4d885132016-06-10 17:09:22 +01002633 sibling_error = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002634 trace_source_return_path_thread_shut(sibling_error);
2635 if (sibling_error) {
2636 error_report("RP: Sibling indicated error %d", sibling_error);
2637 mark_source_rp_bad(ms);
2638 }
2639 /*
2640 * We'll let the main thread deal with closing the RP
2641 * we could do a shutdown(2) on it, but we're the only user
2642 * anyway, so there's nothing gained.
2643 */
2644 goto out;
2645
2646 case MIG_RP_MSG_PONG:
Peter Maydell4d885132016-06-10 17:09:22 +01002647 tmp32 = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002648 trace_source_return_path_thread_pong(tmp32);
2649 break;
2650
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002651 case MIG_RP_MSG_REQ_PAGES:
Peter Maydell4d885132016-06-10 17:09:22 +01002652 start = ldq_be_p(buf);
2653 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002654 migrate_handle_rp_req_pages(ms, NULL, start, len);
2655 break;
2656
2657 case MIG_RP_MSG_REQ_PAGES_ID:
2658 expected_len = 12 + 1; /* header + termination */
2659
2660 if (header_len >= expected_len) {
Peter Maydell4d885132016-06-10 17:09:22 +01002661 start = ldq_be_p(buf);
2662 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002663 /* Now we expect an idstr */
2664 tmp32 = buf[12]; /* Length of the following idstr */
2665 buf[13 + tmp32] = '\0';
2666 expected_len += tmp32;
2667 }
2668 if (header_len != expected_len) {
2669 error_report("RP: Req_Page_id with length %d expecting %zd",
Mao Zhongyied8b2822020-03-28 01:19:08 +08002670 header_len, expected_len);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002671 mark_source_rp_bad(ms);
2672 goto out;
2673 }
2674 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
2675 break;
2676
Peter Xua335deb2018-05-02 18:47:28 +08002677 case MIG_RP_MSG_RECV_BITMAP:
2678 if (header_len < 1) {
2679 error_report("%s: missing block name", __func__);
2680 mark_source_rp_bad(ms);
2681 goto out;
2682 }
2683 /* Format: len (1B) + idstr (<255B). This ends the idstr. */
2684 buf[buf[0] + 1] = '\0';
2685 if (migrate_handle_rp_recv_bitmap(ms, (char *)(buf + 1))) {
2686 mark_source_rp_bad(ms);
2687 goto out;
2688 }
2689 break;
2690
Peter Xu13955b82018-05-02 18:47:30 +08002691 case MIG_RP_MSG_RESUME_ACK:
2692 tmp32 = ldl_be_p(buf);
2693 if (migrate_handle_rp_resume_ack(ms, tmp32)) {
2694 mark_source_rp_bad(ms);
2695 goto out;
2696 }
2697 break;
2698
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002699 default:
2700 break;
2701 }
2702 }
Peter Xu14b17422018-05-02 18:47:21 +08002703
2704out:
2705 res = qemu_file_get_error(rp);
2706 if (res) {
Dr. David Alan Gilbertfb14a422019-10-07 11:35:07 +01002707 if (res == -EIO && migration_in_postcopy()) {
Peter Xu14b17422018-05-02 18:47:21 +08002708 /*
2709 * Maybe there is something we can do: it looks like a
2710 * network down issue, and we pause for a recovery.
2711 */
2712 if (postcopy_pause_return_path_thread(ms)) {
2713 /* Reload rp, reset the rest */
Marc-André Lureau0284a2a2018-09-25 13:22:45 +04002714 if (rp != ms->rp_state.from_dst_file) {
2715 qemu_fclose(rp);
2716 rp = ms->rp_state.from_dst_file;
2717 }
Peter Xu14b17422018-05-02 18:47:21 +08002718 ms->rp_state.error = false;
2719 goto retry;
2720 }
2721 }
2722
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002723 trace_source_return_path_thread_bad_end();
2724 mark_source_rp_bad(ms);
2725 }
2726
2727 trace_source_return_path_thread_end();
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002728 ms->rp_state.from_dst_file = NULL;
2729 qemu_fclose(rp);
Lidong Chen74637e62018-08-06 21:29:29 +08002730 rcu_unregister_thread();
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002731 return NULL;
2732}
2733
Peter Xud3e35b82018-05-02 18:47:24 +08002734static int open_return_path_on_source(MigrationState *ms,
2735 bool create_thread)
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002736{
2737
zhanghailiang89a02a92016-01-15 11:37:42 +08002738 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002739 if (!ms->rp_state.from_dst_file) {
2740 return -1;
2741 }
2742
2743 trace_open_return_path_on_source();
Peter Xud3e35b82018-05-02 18:47:24 +08002744
2745 if (!create_thread) {
2746 /* We're done */
2747 return 0;
2748 }
2749
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002750 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
2751 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
2752
2753 trace_open_return_path_on_source_continue();
2754
2755 return 0;
2756}
2757
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002758/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
2759static int await_return_path_close_on_source(MigrationState *ms)
2760{
2761 /*
2762 * If this is a normal exit then the destination will send a SHUT and the
2763 * rp_thread will exit, however if there's an error we need to cause
2764 * it to exit.
2765 */
zhanghailiang89a02a92016-01-15 11:37:42 +08002766 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002767 /*
2768 * shutdown(2), if we have it, will cause it to unblock if it's stuck
2769 * waiting for the destination.
2770 */
2771 qemu_file_shutdown(ms->rp_state.from_dst_file);
2772 mark_source_rp_bad(ms);
2773 }
2774 trace_await_return_path_close_on_source_joining();
2775 qemu_thread_join(&ms->rp_state.rp_thread);
2776 trace_await_return_path_close_on_source_close();
2777 return ms->rp_state.error;
2778}
2779
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002780/*
2781 * Switch from normal iteration to postcopy
2782 * Returns non-0 on error
2783 */
Peter Xu7287cbd2018-01-03 20:20:09 +08002784static int postcopy_start(MigrationState *ms)
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002785{
2786 int ret;
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002787 QIOChannelBuffer *bioc;
2788 QEMUFile *fb;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002789 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01002790 int64_t bandwidth = migrate_max_postcopy_bandwidth();
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002791 bool restart_block = false;
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002792 int cur_state = MIGRATION_STATUS_ACTIVE;
2793 if (!migrate_pause_before_switchover()) {
2794 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
2795 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2796 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002797
2798 trace_postcopy_start();
2799 qemu_mutex_lock_iothread();
2800 trace_postcopy_start_set_run();
2801
Daniel Henrique Barbozafb064112018-12-05 17:47:01 -02002802 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002803 global_state_store();
2804 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01002805 if (ret < 0) {
2806 goto fail;
2807 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002808
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002809 ret = migration_maybe_pause(ms, &cur_state,
2810 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2811 if (ret < 0) {
2812 goto fail;
2813 }
2814
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01002815 ret = bdrv_inactivate_all();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002816 if (ret < 0) {
2817 goto fail;
2818 }
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002819 restart_block = true;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002820
2821 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00002822 * Cause any non-postcopiable, but iterative devices to
2823 * send out their final data.
2824 */
Fam Zhenga1fbe752017-06-17 00:06:58 +08002825 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00002826
2827 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002828 * in Finish migrate and with the io-lock held everything should
2829 * be quiet, but we've potentially still got dirty pages and we
2830 * need to tell the destination to throw any pages it's already received
2831 * that are dirty
2832 */
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002833 if (migrate_postcopy_ram()) {
2834 if (ram_postcopy_send_discard_bitmap(ms)) {
2835 error_report("postcopy send discard bitmap failed");
2836 goto fail;
2837 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002838 }
2839
2840 /*
2841 * send rest of state - note things that are doing postcopy
2842 * will notice we're in POSTCOPY_ACTIVE and not actually
2843 * wrap their state up here
2844 */
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01002845 /* 0 max-postcopy-bandwidth means unlimited */
2846 if (!bandwidth) {
2847 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
2848 } else {
2849 qemu_file_set_rate_limit(ms->to_dst_file, bandwidth / XFER_LIMIT_RATIO);
2850 }
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002851 if (migrate_postcopy_ram()) {
2852 /* Ping just for debugging, helps line traces up */
2853 qemu_savevm_send_ping(ms->to_dst_file, 2);
2854 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002855
2856 /*
2857 * While loading the device state we may trigger page transfer
2858 * requests and the fd must be free to process those, and thus
2859 * the destination must read the whole device state off the fd before
2860 * it starts processing it. Unfortunately the ad-hoc migration format
2861 * doesn't allow the destination to know the size to read without fully
2862 * parsing it through each devices load-state code (especially the open
2863 * coded devices that use get/put).
2864 * So we wrap the device state up in a package with a length at the start;
2865 * to do this we use a qemu_buf to hold the whole of the device state.
2866 */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002867 bioc = qio_channel_buffer_new(4096);
Daniel P. Berrange6f01f132016-09-30 11:57:14 +01002868 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002869 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
2870 object_unref(OBJECT(bioc));
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002871
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00002872 /*
2873 * Make sure the receiver can get incoming pages before we send the rest
2874 * of the state
2875 */
2876 qemu_savevm_send_postcopy_listen(fb);
2877
Fam Zhenga1fbe752017-06-17 00:06:58 +08002878 qemu_savevm_state_complete_precopy(fb, false, false);
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002879 if (migrate_postcopy_ram()) {
2880 qemu_savevm_send_ping(fb, 3);
2881 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002882
2883 qemu_savevm_send_postcopy_run(fb);
2884
2885 /* <><> end of stuff going into the package */
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002886
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002887 /* Last point of recovery; as soon as we send the package the destination
2888 * can open devices and potentially start running.
2889 * Lets just check again we've not got any errors.
2890 */
2891 ret = qemu_file_get_error(ms->to_dst_file);
2892 if (ret) {
2893 error_report("postcopy_start: Migration stream errored (pre package)");
2894 goto fail_closefb;
2895 }
2896
2897 restart_block = false;
2898
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002899 /* Now send that blob */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002900 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002901 goto fail_closefb;
2902 }
2903 qemu_fclose(fb);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00002904
2905 /* Send a notify to give a chance for anything that needs to happen
2906 * at the transition to postcopy and after the device state; in particular
2907 * spice needs to trigger a transition now
2908 */
2909 ms->postcopy_after_devices = true;
2910 notifier_list_notify(&migration_state_notifiers, ms);
2911
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002912 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
2913
2914 qemu_mutex_unlock_iothread();
2915
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002916 if (migrate_postcopy_ram()) {
2917 /*
2918 * Although this ping is just for debug, it could potentially be
2919 * used for getting a better measurement of downtime at the source.
2920 */
2921 qemu_savevm_send_ping(ms->to_dst_file, 4);
2922 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002923
Pavel Butsykinced1c612017-02-03 18:23:21 +03002924 if (migrate_release_ram()) {
2925 ram_postcopy_migrated_memory_release(ms);
2926 }
2927
zhanghailiang89a02a92016-01-15 11:37:42 +08002928 ret = qemu_file_get_error(ms->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002929 if (ret) {
2930 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00002931 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002932 MIGRATION_STATUS_FAILED);
2933 }
2934
2935 return ret;
2936
2937fail_closefb:
2938 qemu_fclose(fb);
2939fail:
zhanghailiang48781e52015-12-16 11:47:33 +00002940 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002941 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002942 if (restart_block) {
2943 /* A failure happened early enough that we know the destination hasn't
2944 * accessed block devices, so we're safe to recover.
2945 */
2946 Error *local_err = NULL;
2947
2948 bdrv_invalidate_cache_all(&local_err);
2949 if (local_err) {
2950 error_report_err(local_err);
2951 }
2952 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002953 qemu_mutex_unlock_iothread();
2954 return -1;
2955}
2956
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002957/**
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002958 * migration_maybe_pause: Pause if required to by
2959 * migrate_pause_before_switchover called with the iothread locked
2960 * Returns: 0 on success
2961 */
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002962static int migration_maybe_pause(MigrationState *s,
2963 int *current_active_state,
2964 int new_state)
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002965{
2966 if (!migrate_pause_before_switchover()) {
2967 return 0;
2968 }
2969
2970 /* Since leaving this state is not atomic with posting the semaphore
2971 * it's possible that someone could have issued multiple migrate_continue
2972 * and the semaphore is incorrectly positive at this point;
2973 * the docs say it's undefined to reinit a semaphore that's already
2974 * init'd, so use timedwait to eat up any existing posts.
2975 */
2976 while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) {
2977 /* This block intentionally left blank */
2978 }
2979
Zhimin Feng89583382020-01-14 17:43:09 +08002980 /*
2981 * If the migration is cancelled when it is in the completion phase,
2982 * the migration state is set to MIGRATION_STATUS_CANCELLING.
2983 * So we don't need to wait a semaphore, otherwise we would always
2984 * wait for the 'pause_sem' semaphore.
2985 */
2986 if (s->state != MIGRATION_STATUS_CANCELLING) {
2987 qemu_mutex_unlock_iothread();
2988 migrate_set_state(&s->state, *current_active_state,
2989 MIGRATION_STATUS_PRE_SWITCHOVER);
2990 qemu_sem_wait(&s->pause_sem);
2991 migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER,
2992 new_state);
2993 *current_active_state = new_state;
2994 qemu_mutex_lock_iothread();
2995 }
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002996
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002997 return s->state == new_state ? 0 : -EINVAL;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002998}
2999
3000/**
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003001 * migration_completion: Used by migration_thread when there's not much left.
3002 * The caller 'breaks' the loop when this returns.
3003 *
3004 * @s: Current migration state
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003005 */
Peter Xu2ad87302018-01-03 20:20:14 +08003006static void migration_completion(MigrationState *s)
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003007{
3008 int ret;
Peter Xu2ad87302018-01-03 20:20:14 +08003009 int current_active_state = s->state;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003010
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003011 if (s->state == MIGRATION_STATUS_ACTIVE) {
3012 qemu_mutex_lock_iothread();
Peter Xu64909f92018-01-03 20:20:10 +08003013 s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Daniel Henrique Barbozafb064112018-12-05 17:47:01 -02003014 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
Peter Xu7287cbd2018-01-03 20:20:09 +08003015 s->vm_was_running = runstate_is_running();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003016 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003017
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003018 if (!ret) {
Fam Zhenga1fbe752017-06-17 00:06:58 +08003019 bool inactivate = !migrate_colo_enabled();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003020 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02003021 if (ret >= 0) {
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01003022 ret = migration_maybe_pause(s, &current_active_state,
3023 MIGRATION_STATUS_DEVICE);
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01003024 }
3025 if (ret >= 0) {
Kevin Wolff07fa4c2017-05-22 17:10:38 +02003026 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
Fam Zhenga1fbe752017-06-17 00:06:58 +08003027 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
3028 inactivate);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02003029 }
Fam Zhenga1fbe752017-06-17 00:06:58 +08003030 if (inactivate && ret >= 0) {
3031 s->block_inactive = true;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003032 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003033 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003034 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003035
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003036 if (ret < 0) {
3037 goto fail;
3038 }
3039 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
3040 trace_migration_completion_postcopy_end();
3041
zhanghailiang89a02a92016-01-15 11:37:42 +08003042 qemu_savevm_state_complete_postcopy(s->to_dst_file);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003043 trace_migration_completion_postcopy_end_after_complete();
Longpeng(Mike)6ba11212020-11-05 17:17:26 +08003044 } else if (s->state == MIGRATION_STATUS_CANCELLING) {
3045 goto fail;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003046 }
3047
3048 /*
3049 * If rp was opened we must clean up the thread before
3050 * cleaning everything else up (since if there are no failures
3051 * it will wait for the destination to send it's status in
3052 * a SHUT command).
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003053 */
Peter Xu0425dc92017-05-31 18:35:34 +08003054 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003055 int rp_error;
Peter Xu0425dc92017-05-31 18:35:34 +08003056 trace_migration_return_path_end_before();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003057 rp_error = await_return_path_close_on_source(s);
Peter Xu0425dc92017-05-31 18:35:34 +08003058 trace_migration_return_path_end_after(rp_error);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003059 if (rp_error) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02003060 goto fail_invalidate;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003061 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003062 }
3063
zhanghailiang89a02a92016-01-15 11:37:42 +08003064 if (qemu_file_get_error(s->to_dst_file)) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003065 trace_migration_completion_file_err();
Greg Kurzfe904ea2016-05-18 15:44:36 +02003066 goto fail_invalidate;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003067 }
3068
zhanghailiang0b827d52016-10-27 14:42:54 +08003069 if (!migrate_colo_enabled()) {
3070 migrate_set_state(&s->state, current_active_state,
3071 MIGRATION_STATUS_COMPLETED);
3072 }
3073
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003074 return;
3075
Greg Kurzfe904ea2016-05-18 15:44:36 +02003076fail_invalidate:
3077 /* If not doing postcopy, vm_start() will be called: let's regain
3078 * control on images.
3079 */
Dr. David Alan Gilbert6039dd52018-02-05 09:13:37 +00003080 if (s->state == MIGRATION_STATUS_ACTIVE ||
3081 s->state == MIGRATION_STATUS_DEVICE) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02003082 Error *local_err = NULL;
3083
zhanghailiang1d2acc32017-01-24 15:59:52 +08003084 qemu_mutex_lock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02003085 bdrv_invalidate_cache_all(&local_err);
3086 if (local_err) {
3087 error_report_err(local_err);
zhanghailiang1d2acc32017-01-24 15:59:52 +08003088 } else {
3089 s->block_inactive = false;
Greg Kurzfe904ea2016-05-18 15:44:36 +02003090 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08003091 qemu_mutex_unlock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02003092 }
3093
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003094fail:
zhanghailiang48781e52015-12-16 11:47:33 +00003095 migrate_set_state(&s->state, current_active_state,
3096 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003097}
3098
zhanghailiang35a6ed42016-10-27 14:42:52 +08003099bool migrate_colo_enabled(void)
3100{
3101 MigrationState *s = migrate_get_current();
3102 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
3103}
3104
Peter Xub23c2ad2018-05-02 18:47:19 +08003105typedef enum MigThrError {
3106 /* No error detected */
3107 MIG_THR_ERR_NONE = 0,
3108 /* Detected error, but resumed successfully */
3109 MIG_THR_ERR_RECOVERED = 1,
3110 /* Detected fatal error, need to exit */
3111 MIG_THR_ERR_FATAL = 2,
3112} MigThrError;
3113
Peter Xu94190692018-05-02 18:47:34 +08003114static int postcopy_resume_handshake(MigrationState *s)
3115{
3116 qemu_savevm_send_postcopy_resume(s->to_dst_file);
3117
3118 while (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
3119 qemu_sem_wait(&s->rp_state.rp_sem);
3120 }
3121
3122 if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
3123 return 0;
3124 }
3125
3126 return -1;
3127}
3128
Peter Xu135b87b2018-05-02 18:47:25 +08003129/* Return zero if success, or <0 for error */
3130static int postcopy_do_resume(MigrationState *s)
3131{
Peter Xud1b8ead2018-05-02 18:47:31 +08003132 int ret;
3133
3134 /*
3135 * Call all the resume_prepare() hooks, so that modules can be
3136 * ready for the migration resume.
3137 */
3138 ret = qemu_savevm_state_resume_prepare(s);
3139 if (ret) {
3140 error_report("%s: resume_prepare() failure detected: %d",
3141 __func__, ret);
3142 return ret;
3143 }
3144
3145 /*
Peter Xu94190692018-05-02 18:47:34 +08003146 * Last handshake with destination on the resume (destination will
3147 * switch to postcopy-active afterwards)
Peter Xud1b8ead2018-05-02 18:47:31 +08003148 */
Peter Xu94190692018-05-02 18:47:34 +08003149 ret = postcopy_resume_handshake(s);
3150 if (ret) {
3151 error_report("%s: handshake failed: %d", __func__, ret);
3152 return ret;
3153 }
Peter Xud1b8ead2018-05-02 18:47:31 +08003154
Peter Xu135b87b2018-05-02 18:47:25 +08003155 return 0;
3156}
3157
Peter Xub23c2ad2018-05-02 18:47:19 +08003158/*
3159 * We don't return until we are in a safe state to continue current
3160 * postcopy migration. Returns MIG_THR_ERR_RECOVERED if recovered, or
3161 * MIG_THR_ERR_FATAL if unrecovery failure happened.
3162 */
3163static MigThrError postcopy_pause(MigrationState *s)
3164{
3165 assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
Peter Xub23c2ad2018-05-02 18:47:19 +08003166
Peter Xu135b87b2018-05-02 18:47:25 +08003167 while (true) {
Peter Xu62df0662018-05-02 18:47:38 +08003168 QEMUFile *file;
3169
Peter Xu135b87b2018-05-02 18:47:25 +08003170 /* Current channel is possibly broken. Release it. */
3171 assert(s->to_dst_file);
Peter Xu62df0662018-05-02 18:47:38 +08003172 qemu_mutex_lock(&s->qemu_file_lock);
3173 file = s->to_dst_file;
Peter Xu135b87b2018-05-02 18:47:25 +08003174 s->to_dst_file = NULL;
Peter Xu62df0662018-05-02 18:47:38 +08003175 qemu_mutex_unlock(&s->qemu_file_lock);
3176
3177 qemu_file_shutdown(file);
3178 qemu_fclose(file);
Peter Xub23c2ad2018-05-02 18:47:19 +08003179
Peter Xud246ea52020-10-21 17:27:20 -04003180 migrate_set_state(&s->state, s->state,
3181 MIGRATION_STATUS_POSTCOPY_PAUSED);
3182
Peter Xu135b87b2018-05-02 18:47:25 +08003183 error_report("Detected IO failure for postcopy. "
3184 "Migration paused.");
3185
3186 /*
3187 * We wait until things fixed up. Then someone will setup the
3188 * status back for us.
3189 */
3190 while (s->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
3191 qemu_sem_wait(&s->postcopy_pause_sem);
3192 }
3193
3194 if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
3195 /* Woken up by a recover procedure. Give it a shot */
3196
3197 /*
3198 * Firstly, let's wake up the return path now, with a new
3199 * return path channel.
3200 */
3201 qemu_sem_post(&s->postcopy_pause_rp_sem);
3202
3203 /* Do the resume logic */
3204 if (postcopy_do_resume(s) == 0) {
3205 /* Let's continue! */
3206 trace_postcopy_pause_continued();
3207 return MIG_THR_ERR_RECOVERED;
3208 } else {
3209 /*
3210 * Something wrong happened during the recovery, let's
3211 * pause again. Pause is always better than throwing
3212 * data away.
3213 */
3214 continue;
3215 }
3216 } else {
3217 /* This is not right... Time to quit. */
3218 return MIG_THR_ERR_FATAL;
3219 }
Peter Xub23c2ad2018-05-02 18:47:19 +08003220 }
Peter Xub23c2ad2018-05-02 18:47:19 +08003221}
3222
3223static MigThrError migration_detect_error(MigrationState *s)
3224{
3225 int ret;
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003226 int state = s->state;
Yury Kotov3d661c82019-04-22 13:34:20 +03003227 Error *local_error = NULL;
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003228
3229 if (state == MIGRATION_STATUS_CANCELLING ||
3230 state == MIGRATION_STATUS_CANCELLED) {
3231 /* End the migration, but don't set the state to failed */
3232 return MIG_THR_ERR_FATAL;
3233 }
Peter Xub23c2ad2018-05-02 18:47:19 +08003234
3235 /* Try to detect any file errors */
Yury Kotov3d661c82019-04-22 13:34:20 +03003236 ret = qemu_file_get_error_obj(s->to_dst_file, &local_error);
Peter Xub23c2ad2018-05-02 18:47:19 +08003237 if (!ret) {
3238 /* Everything is fine */
Yury Kotov3d661c82019-04-22 13:34:20 +03003239 assert(!local_error);
Peter Xub23c2ad2018-05-02 18:47:19 +08003240 return MIG_THR_ERR_NONE;
3241 }
3242
Yury Kotov3d661c82019-04-22 13:34:20 +03003243 if (local_error) {
3244 migrate_set_error(s, local_error);
3245 error_free(local_error);
3246 }
3247
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003248 if (state == MIGRATION_STATUS_POSTCOPY_ACTIVE && ret == -EIO) {
Peter Xub23c2ad2018-05-02 18:47:19 +08003249 /*
3250 * For postcopy, we allow the network to be down for a
3251 * while. After that, it can be continued by a
3252 * recovery phase.
3253 */
3254 return postcopy_pause(s);
3255 } else {
3256 /*
3257 * For precopy (or postcopy with error outside IO), we fail
3258 * with no time.
3259 */
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003260 migrate_set_state(&s->state, state, MIGRATION_STATUS_FAILED);
Peter Xub23c2ad2018-05-02 18:47:19 +08003261 trace_migration_thread_file_err();
3262
3263 /* Time to stop the migration, now. */
3264 return MIG_THR_ERR_FATAL;
3265 }
3266}
3267
Wei Yang268dcd42019-09-12 10:49:57 +08003268/* How many bytes have we transferred since the beginning of the migration */
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003269static uint64_t migration_total_bytes(MigrationState *s)
3270{
Juan Quintelab9ee2f72016-01-15 11:40:13 +01003271 return qemu_ftell(s->to_dst_file) + ram_counters.multifd_bytes;
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003272}
3273
Peter Xucf011f02018-01-03 20:20:11 +08003274static void migration_calculate_complete(MigrationState *s)
3275{
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003276 uint64_t bytes = migration_total_bytes(s);
Peter Xucf011f02018-01-03 20:20:11 +08003277 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela6cde6fb2018-06-26 15:26:35 +02003278 int64_t transfer_time;
Peter Xucf011f02018-01-03 20:20:11 +08003279
3280 s->total_time = end_time - s->start_time;
3281 if (!s->downtime) {
3282 /*
3283 * It's still not set, so we are precopy migration. For
3284 * postcopy, downtime is calculated during postcopy_start().
3285 */
3286 s->downtime = end_time - s->downtime_start;
3287 }
3288
Juan Quintela6cde6fb2018-06-26 15:26:35 +02003289 transfer_time = s->total_time - s->setup_time;
3290 if (transfer_time) {
3291 s->mbps = ((double) bytes * 8.0) / transfer_time / 1000;
Peter Xucf011f02018-01-03 20:20:11 +08003292 }
3293}
3294
Ivan Ren87f3bd82019-08-02 18:18:41 +08003295static void update_iteration_initial_status(MigrationState *s)
3296{
3297 /*
3298 * Update these three fields at the same time to avoid mismatch info lead
3299 * wrong speed calculation.
3300 */
3301 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3302 s->iteration_initial_bytes = migration_total_bytes(s);
3303 s->iteration_initial_pages = ram_get_total_transferred_pages();
3304}
3305
Peter Xub15df1a2018-01-03 20:20:13 +08003306static void migration_update_counters(MigrationState *s,
3307 int64_t current_time)
3308{
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08003309 uint64_t transferred, transferred_pages, time_spent;
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003310 uint64_t current_bytes; /* bytes transferred since the beginning */
Peter Xub15df1a2018-01-03 20:20:13 +08003311 double bandwidth;
3312
3313 if (current_time < s->iteration_start_time + BUFFER_DELAY) {
3314 return;
3315 }
3316
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003317 current_bytes = migration_total_bytes(s);
3318 transferred = current_bytes - s->iteration_initial_bytes;
Peter Xub15df1a2018-01-03 20:20:13 +08003319 time_spent = current_time - s->iteration_start_time;
3320 bandwidth = (double)transferred / time_spent;
Wei Wang0781c1e2018-01-22 19:36:39 +08003321 s->threshold_size = bandwidth * s->parameters.downtime_limit;
Peter Xub15df1a2018-01-03 20:20:13 +08003322
3323 s->mbps = (((double) transferred * 8.0) /
3324 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
3325
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08003326 transferred_pages = ram_get_total_transferred_pages() -
3327 s->iteration_initial_pages;
3328 s->pages_per_second = (double) transferred_pages /
3329 (((double) time_spent / 1000.0));
3330
Peter Xub15df1a2018-01-03 20:20:13 +08003331 /*
3332 * if we haven't sent anything, we don't want to
3333 * recalculate. 10000 is a small enough number for our purposes
3334 */
3335 if (ram_counters.dirty_pages_rate && transferred > 10000) {
Balamuruhan S650af892018-06-12 14:20:09 +05303336 s->expected_downtime = ram_counters.remaining / bandwidth;
Peter Xub15df1a2018-01-03 20:20:13 +08003337 }
3338
3339 qemu_file_reset_rate_limit(s->to_dst_file);
3340
Ivan Ren87f3bd82019-08-02 18:18:41 +08003341 update_iteration_initial_status(s);
Peter Xub15df1a2018-01-03 20:20:13 +08003342
3343 trace_migrate_transferred(transferred, time_spent,
Wei Wang0781c1e2018-01-22 19:36:39 +08003344 bandwidth, s->threshold_size);
Peter Xub15df1a2018-01-03 20:20:13 +08003345}
3346
Peter Xu2ad87302018-01-03 20:20:14 +08003347/* Migration thread iteration status */
3348typedef enum {
3349 MIG_ITERATE_RESUME, /* Resume current iteration */
3350 MIG_ITERATE_SKIP, /* Skip current iteration */
3351 MIG_ITERATE_BREAK, /* Break the loop */
3352} MigIterateState;
3353
3354/*
3355 * Return true if continue to the next iteration directly, false
3356 * otherwise.
3357 */
3358static MigIterateState migration_iteration_run(MigrationState *s)
3359{
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04003360 uint64_t pending_size, pend_pre, pend_compat, pend_post;
Peter Xu2ad87302018-01-03 20:20:14 +08003361 bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE;
3362
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04003363 qemu_savevm_state_pending(s->to_dst_file, s->threshold_size, &pend_pre,
3364 &pend_compat, &pend_post);
3365 pending_size = pend_pre + pend_compat + pend_post;
Peter Xu2ad87302018-01-03 20:20:14 +08003366
3367 trace_migrate_pending(pending_size, s->threshold_size,
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04003368 pend_pre, pend_compat, pend_post);
Peter Xu2ad87302018-01-03 20:20:14 +08003369
3370 if (pending_size && pending_size >= s->threshold_size) {
3371 /* Still a significant amount to transfer */
Wei Yang52aec702019-07-18 16:37:47 +08003372 if (!in_postcopy && pend_pre <= s->threshold_size &&
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01003373 qatomic_read(&s->start_postcopy)) {
Peter Xu2ad87302018-01-03 20:20:14 +08003374 if (postcopy_start(s)) {
3375 error_report("%s: postcopy failed to start", __func__);
3376 }
3377 return MIG_ITERATE_SKIP;
3378 }
3379 /* Just another iteration step */
Wei Yang17d93512019-10-06 06:05:16 +08003380 qemu_savevm_state_iterate(s->to_dst_file, in_postcopy);
Peter Xu2ad87302018-01-03 20:20:14 +08003381 } else {
3382 trace_migration_thread_low_pending(pending_size);
3383 migration_completion(s);
3384 return MIG_ITERATE_BREAK;
3385 }
3386
3387 return MIG_ITERATE_RESUME;
3388}
3389
Peter Xu199aa6d2018-01-03 20:20:15 +08003390static void migration_iteration_finish(MigrationState *s)
3391{
3392 /* If we enabled cpu throttling for auto-converge, turn it off. */
3393 cpu_throttle_stop();
3394
3395 qemu_mutex_lock_iothread();
3396 switch (s->state) {
3397 case MIGRATION_STATUS_COMPLETED:
3398 migration_calculate_complete(s);
3399 runstate_set(RUN_STATE_POSTMIGRATE);
3400 break;
3401
3402 case MIGRATION_STATUS_ACTIVE:
3403 /*
3404 * We should really assert here, but since it's during
3405 * migration, let's try to reduce the usage of assertions.
3406 */
3407 if (!migrate_colo_enabled()) {
3408 error_report("%s: critical error: calling COLO code without "
3409 "COLO enabled", __func__);
3410 }
3411 migrate_start_colo_process(s);
3412 /*
3413 * Fixme: we will run VM in COLO no matter its old running state.
3414 * After exited COLO, we will keep running.
3415 */
3416 s->vm_was_running = true;
3417 /* Fallthrough */
3418 case MIGRATION_STATUS_FAILED:
3419 case MIGRATION_STATUS_CANCELLED:
Dr. David Alan Gilbert57225e52018-07-19 10:22:57 +01003420 case MIGRATION_STATUS_CANCELLING:
Peter Xu199aa6d2018-01-03 20:20:15 +08003421 if (s->vm_was_running) {
3422 vm_start();
3423 } else {
3424 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
3425 runstate_set(RUN_STATE_POSTMIGRATE);
3426 }
3427 }
3428 break;
3429
3430 default:
3431 /* Should not reach here, but if so, forgive the VM. */
3432 error_report("%s: Unknown ending state %d", __func__, s->state);
3433 break;
3434 }
Yury Kotovfd392cf2019-04-08 14:33:43 +03003435 migrate_fd_cleanup_schedule(s);
Peter Xu199aa6d2018-01-03 20:20:15 +08003436 qemu_mutex_unlock_iothread();
3437}
3438
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003439void migration_make_urgent_request(void)
3440{
3441 qemu_sem_post(&migrate_get_current()->rate_limit_sem);
3442}
3443
3444void migration_consume_urgent_request(void)
3445{
3446 qemu_sem_wait(&migrate_get_current()->rate_limit_sem);
3447}
3448
Dr. David Alan Gilbert97e1e062019-12-05 10:29:18 +00003449/* Returns true if the rate limiting was broken by an urgent request */
3450bool migration_rate_limit(void)
3451{
3452 int64_t now = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3453 MigrationState *s = migrate_get_current();
3454
3455 bool urgent = false;
3456 migration_update_counters(s, now);
3457 if (qemu_file_rate_limit(s->to_dst_file)) {
Lukas Straub77386122020-05-20 22:42:32 +02003458
3459 if (qemu_file_get_error(s->to_dst_file)) {
3460 return false;
3461 }
Dr. David Alan Gilbert97e1e062019-12-05 10:29:18 +00003462 /*
3463 * Wait for a delay to do rate limiting OR
3464 * something urgent to post the semaphore.
3465 */
3466 int ms = s->iteration_start_time + BUFFER_DELAY - now;
3467 trace_migration_rate_limit_pre(ms);
3468 if (qemu_sem_timedwait(&s->rate_limit_sem, ms) == 0) {
3469 /*
3470 * We were woken by one or more urgent things but
3471 * the timedwait will have consumed one of them.
3472 * The service routine for the urgent wake will dec
3473 * the semaphore itself for each item it consumes,
3474 * so add this one we just eat back.
3475 */
3476 qemu_sem_post(&s->rate_limit_sem);
3477 urgent = true;
3478 }
3479 trace_migration_rate_limit_post(urgent);
3480 }
3481 return urgent;
3482}
3483
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00003484/*
3485 * Master migration thread on the source VM.
3486 * It drives the migration and pumps the data down the outgoing channel.
3487 */
Juan Quintela5f496a12013-02-22 17:36:30 +01003488static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003489{
Juan Quintela9848a402012-12-19 09:55:50 +01003490 MigrationState *s = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +01003491 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Peter Xub23c2ad2018-05-02 18:47:19 +08003492 MigThrError thr_error;
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003493 bool urgent = false;
Juan Quintela76f59332012-10-03 20:16:24 +02003494
Paolo Bonziniab28bd22015-07-09 08:55:38 +02003495 rcu_register_thread();
3496
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +00003497 object_ref(OBJECT(s));
Ivan Ren87f3bd82019-08-02 18:18:41 +08003498 update_iteration_initial_status(s);
Peter Xub15df1a2018-01-03 20:20:13 +08003499
zhanghailiang89a02a92016-01-15 11:37:42 +08003500 qemu_savevm_state_header(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003501
Peter Xu62a02652017-06-14 15:55:58 +08003502 /*
3503 * If we opened the return path, we need to make sure dst has it
3504 * opened as well.
3505 */
3506 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003507 /* Now tell the dest that it should open its end so it can reply */
zhanghailiang89a02a92016-01-15 11:37:42 +08003508 qemu_savevm_send_open_return_path(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003509
3510 /* And do a ping that will make stuff easier to debug */
zhanghailiang89a02a92016-01-15 11:37:42 +08003511 qemu_savevm_send_ping(s->to_dst_file, 1);
Peter Xu0425dc92017-05-31 18:35:34 +08003512 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003513
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03003514 if (migrate_postcopy()) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003515 /*
3516 * Tell the destination that we *might* want to do postcopy later;
3517 * if the other end can't do postcopy it should fail now, nice and
3518 * early.
3519 */
zhanghailiang89a02a92016-01-15 11:37:42 +08003520 qemu_savevm_send_postcopy_advise(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003521 }
3522
Zhang Chenaad555c2018-09-03 12:38:47 +08003523 if (migrate_colo_enabled()) {
3524 /* Notify migration destination that we enable COLO */
3525 qemu_savevm_send_colo_enable(s->to_dst_file);
3526 }
3527
Juan Quintela9907e842017-06-28 11:52:24 +02003528 qemu_savevm_state_setup(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003529
Keqian Zhud05de9e2020-02-04 13:08:41 +08003530 if (qemu_savevm_state_guest_unplug_pending()) {
Jens Freimannc7e0acd2019-10-29 12:49:02 +01003531 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3532 MIGRATION_STATUS_WAIT_UNPLUG);
3533
3534 while (s->state == MIGRATION_STATUS_WAIT_UNPLUG &&
3535 qemu_savevm_state_guest_unplug_pending()) {
3536 qemu_sem_timedwait(&s->wait_unplug_sem, 250);
3537 }
3538
3539 migrate_set_state(&s->state, MIGRATION_STATUS_WAIT_UNPLUG,
3540 MIGRATION_STATUS_ACTIVE);
3541 }
3542
Alex Blighbc72ad62013-08-21 16:03:08 +01003543 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang48781e52015-12-16 11:47:33 +00003544 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3545 MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04003546
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00003547 trace_migration_thread_setup_complete();
3548
Wei Yang8f8d5282019-07-17 08:53:41 +08003549 while (migration_is_active(s)) {
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003550 if (urgent || !qemu_file_rate_limit(s->to_dst_file)) {
Peter Xu2ad87302018-01-03 20:20:14 +08003551 MigIterateState iter_state = migration_iteration_run(s);
3552 if (iter_state == MIG_ITERATE_SKIP) {
3553 continue;
3554 } else if (iter_state == MIG_ITERATE_BREAK) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003555 break;
Juan Quintelac369f402012-10-03 20:33:34 +02003556 }
3557 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01003558
Peter Xub23c2ad2018-05-02 18:47:19 +08003559 /*
3560 * Try to detect any kind of failures, and see whether we
3561 * should stop the migration now.
3562 */
3563 thr_error = migration_detect_error(s);
3564 if (thr_error == MIG_THR_ERR_FATAL) {
3565 /* Stop migration */
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01003566 break;
Peter Xub23c2ad2018-05-02 18:47:19 +08003567 } else if (thr_error == MIG_THR_ERR_RECOVERED) {
3568 /*
3569 * Just recovered from a e.g. network failure, reset all
3570 * the local variables. This is important to avoid
3571 * breaking transferred_bytes and bandwidth calculation
3572 */
Ivan Ren87f3bd82019-08-02 18:18:41 +08003573 update_iteration_initial_status(s);
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01003574 }
Peter Xub15df1a2018-01-03 20:20:13 +08003575
Dr. David Alan Gilbert97e1e062019-12-05 10:29:18 +00003576 urgent = migration_rate_limit();
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01003577 }
3578
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003579 trace_migration_thread_after_loop();
Peter Xu199aa6d2018-01-03 20:20:15 +08003580 migration_iteration_finish(s);
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +00003581 object_unref(OBJECT(s));
Paolo Bonziniab28bd22015-07-09 08:55:38 +02003582 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003583 return NULL;
3584}
3585
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00003586void migrate_fd_connect(MigrationState *s, Error *error_in)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003587{
Juan Quintela00f4b572019-06-12 11:33:27 +02003588 Error *local_err = NULL;
Peter Xud3e35b82018-05-02 18:47:24 +08003589 int64_t rate_limit;
3590 bool resume = s->state == MIGRATION_STATUS_POSTCOPY_PAUSED;
3591
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05303592 s->expected_downtime = s->parameters.downtime_limit;
Marc-André Lureau9cbc3642020-03-25 19:47:21 +01003593 if (resume) {
3594 assert(s->cleanup_bh);
3595 } else {
3596 assert(!s->cleanup_bh);
3597 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup_bh, s);
3598 }
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00003599 if (error_in) {
3600 migrate_fd_error(s, error_in);
3601 migrate_fd_cleanup(s);
3602 return;
3603 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003604
Peter Xud3e35b82018-05-02 18:47:24 +08003605 if (resume) {
3606 /* This is a resumed migration */
Peter Xu8504dde2019-09-06 21:01:03 +08003607 rate_limit = s->parameters.max_postcopy_bandwidth /
3608 XFER_LIMIT_RATIO;
Peter Xud3e35b82018-05-02 18:47:24 +08003609 } else {
3610 /* This is a fresh new migration */
3611 rate_limit = s->parameters.max_bandwidth / XFER_LIMIT_RATIO;
Paolo Bonzini442773c2013-02-22 17:36:44 +01003612
Peter Xud3e35b82018-05-02 18:47:24 +08003613 /* Notify before starting migration thread */
3614 notifier_list_notify(&migration_state_notifiers, s);
3615 }
3616
3617 qemu_file_set_rate_limit(s->to_dst_file, rate_limit);
3618 qemu_file_set_blocking(s->to_dst_file, true);
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02003619
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003620 /*
Peter Xuc788ada2017-06-26 18:28:55 +08003621 * Open the return path. For postcopy, it is used exclusively. For
3622 * precopy, only if user specified "return-path" capability would
3623 * QEMU uses the return path.
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003624 */
Peter Xuc788ada2017-06-26 18:28:55 +08003625 if (migrate_postcopy_ram() || migrate_use_return_path()) {
Peter Xud3e35b82018-05-02 18:47:24 +08003626 if (open_return_path_on_source(s, !resume)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003627 error_report("Unable to open return-path for postcopy");
Peter Xud3e35b82018-05-02 18:47:24 +08003628 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003629 migrate_fd_cleanup(s);
3630 return;
3631 }
3632 }
3633
Peter Xud3e35b82018-05-02 18:47:24 +08003634 if (resume) {
Peter Xu135b87b2018-05-02 18:47:25 +08003635 /* Wakeup the main migration thread to do the recovery */
3636 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
3637 MIGRATION_STATUS_POSTCOPY_RECOVER);
3638 qemu_sem_post(&s->postcopy_pause_sem);
Peter Xud3e35b82018-05-02 18:47:24 +08003639 return;
3640 }
3641
Juan Quintela00f4b572019-06-12 11:33:27 +02003642 if (multifd_save_setup(&local_err) != 0) {
3643 error_report_err(local_err);
Juan Quintelaf986c3d2016-01-14 16:52:55 +01003644 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3645 MIGRATION_STATUS_FAILED);
3646 migrate_fd_cleanup(s);
3647 return;
3648 }
Pankaj Gupta009fad72017-01-23 19:12:56 +05303649 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01003650 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003651 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003652}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00003653
Peter Xu9d18af92017-06-27 12:10:19 +08003654void migration_global_dump(Monitor *mon)
3655{
3656 MigrationState *ms = migrate_get_current();
3657
Juan Quintela6f0f6422017-10-26 11:49:57 +02003658 monitor_printf(mon, "globals:\n");
3659 monitor_printf(mon, "store-global-state: %s\n",
3660 ms->store_global_state ? "on" : "off");
3661 monitor_printf(mon, "only-migratable: %s\n",
Markus Armbruster811f8652019-04-01 11:08:24 +02003662 only_migratable ? "on" : "off");
Juan Quintela6f0f6422017-10-26 11:49:57 +02003663 monitor_printf(mon, "send-configuration: %s\n",
3664 ms->send_configuration ? "on" : "off");
3665 monitor_printf(mon, "send-section-footer: %s\n",
3666 ms->send_section_footer ? "on" : "off");
Xiao Guangrongf5482222018-05-03 16:06:11 +08003667 monitor_printf(mon, "decompress-error-check: %s\n",
3668 ms->decompress_error_check ? "on" : "off");
Peter Xu002cad62019-06-03 14:50:56 +08003669 monitor_printf(mon, "clear-bitmap-shift: %u\n",
3670 ms->clear_bitmap_shift);
Peter Xu9d18af92017-06-27 12:10:19 +08003671}
3672
Peter Xu20814752017-07-18 11:39:03 +08003673#define DEFINE_PROP_MIG_CAP(name, x) \
3674 DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
3675
Peter Xu52722982017-06-27 12:10:14 +08003676static Property migration_properties[] = {
3677 DEFINE_PROP_BOOL("store-global-state", MigrationState,
3678 store_global_state, true),
Peter Xu71dd4c12017-06-27 12:10:16 +08003679 DEFINE_PROP_BOOL("send-configuration", MigrationState,
3680 send_configuration, true),
Peter Xu15c38502017-06-27 12:10:17 +08003681 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
3682 send_section_footer, true),
Xiao Guangrongf5482222018-05-03 16:06:11 +08003683 DEFINE_PROP_BOOL("decompress-error-check", MigrationState,
3684 decompress_error_check, true),
Peter Xu002cad62019-06-03 14:50:56 +08003685 DEFINE_PROP_UINT8("x-clear-bitmap-shift", MigrationState,
3686 clear_bitmap_shift, CLEAR_BITMAP_SHIFT_DEFAULT),
Peter Xu89632fa2017-07-18 11:39:02 +08003687
3688 /* Migration parameters */
Juan Quintela741d4082017-12-01 13:08:38 +01003689 DEFINE_PROP_UINT8("x-compress-level", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003690 parameters.compress_level,
3691 DEFAULT_MIGRATE_COMPRESS_LEVEL),
Juan Quintela741d4082017-12-01 13:08:38 +01003692 DEFINE_PROP_UINT8("x-compress-threads", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003693 parameters.compress_threads,
3694 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
Xiao Guangrong1d588722018-08-21 16:10:20 +08003695 DEFINE_PROP_BOOL("x-compress-wait-thread", MigrationState,
3696 parameters.compress_wait_thread, true),
Juan Quintela741d4082017-12-01 13:08:38 +01003697 DEFINE_PROP_UINT8("x-decompress-threads", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003698 parameters.decompress_threads,
3699 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
Keqian Zhudc14a472020-02-24 10:31:42 +08003700 DEFINE_PROP_UINT8("x-throttle-trigger-threshold", MigrationState,
3701 parameters.throttle_trigger_threshold,
3702 DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD),
Juan Quintela741d4082017-12-01 13:08:38 +01003703 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003704 parameters.cpu_throttle_initial,
3705 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
Juan Quintela741d4082017-12-01 13:08:38 +01003706 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003707 parameters.cpu_throttle_increment,
3708 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
Keqian Zhucbbf8182020-04-13 18:15:08 +08003709 DEFINE_PROP_BOOL("x-cpu-throttle-tailslow", MigrationState,
3710 parameters.cpu_throttle_tailslow, false),
Juan Quintela741d4082017-12-01 13:08:38 +01003711 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003712 parameters.max_bandwidth, MAX_THROTTLE),
Juan Quintela741d4082017-12-01 13:08:38 +01003713 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003714 parameters.downtime_limit,
3715 DEFAULT_MIGRATE_SET_DOWNTIME),
Juan Quintela741d4082017-12-01 13:08:38 +01003716 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003717 parameters.x_checkpoint_delay,
3718 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
Juan Quintelacbfd6c92019-02-06 13:54:06 +01003719 DEFINE_PROP_UINT8("multifd-channels", MigrationState,
3720 parameters.multifd_channels,
Juan Quintela4075fb12016-01-15 08:56:17 +01003721 DEFAULT_MIGRATE_MULTIFD_CHANNELS),
Juan Quintela96eef042019-01-16 10:35:55 +01003722 DEFINE_PROP_MULTIFD_COMPRESSION("multifd-compression", MigrationState,
3723 parameters.multifd_compression,
3724 DEFAULT_MIGRATE_MULTIFD_COMPRESSION),
Juan Quintela9004db42020-01-23 17:08:52 +01003725 DEFINE_PROP_UINT8("multifd-zlib-level", MigrationState,
3726 parameters.multifd_zlib_level,
3727 DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL),
Juan Quintela6a9ad152020-01-23 17:41:36 +01003728 DEFINE_PROP_UINT8("multifd-zstd-level", MigrationState,
3729 parameters.multifd_zstd_level,
3730 DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL),
Juan Quintela73af8dd2017-10-05 21:30:10 +02003731 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
3732 parameters.xbzrle_cache_size,
3733 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01003734 DEFINE_PROP_SIZE("max-postcopy-bandwidth", MigrationState,
3735 parameters.max_postcopy_bandwidth,
3736 DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH),
Li Qiang4cbc9c72018-08-01 06:00:20 -07003737 DEFINE_PROP_UINT8("max-cpu-throttle", MigrationState,
3738 parameters.max_cpu_throttle,
3739 DEFAULT_MIGRATE_MAX_CPU_THROTTLE),
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00003740 DEFINE_PROP_SIZE("announce-initial", MigrationState,
3741 parameters.announce_initial,
3742 DEFAULT_MIGRATE_ANNOUNCE_INITIAL),
3743 DEFINE_PROP_SIZE("announce-max", MigrationState,
3744 parameters.announce_max,
3745 DEFAULT_MIGRATE_ANNOUNCE_MAX),
3746 DEFINE_PROP_SIZE("announce-rounds", MigrationState,
3747 parameters.announce_rounds,
3748 DEFAULT_MIGRATE_ANNOUNCE_ROUNDS),
3749 DEFINE_PROP_SIZE("announce-step", MigrationState,
3750 parameters.announce_step,
3751 DEFAULT_MIGRATE_ANNOUNCE_STEP),
Peter Xu20814752017-07-18 11:39:03 +08003752
3753 /* Migration capabilities */
3754 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
3755 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
3756 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
3757 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
3758 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
3759 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
3760 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
3761 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
3762 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
3763 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
3764 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
Juan Quintelacbfd6c92019-02-06 13:54:06 +01003765 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_MULTIFD),
Peter Xu20814752017-07-18 11:39:03 +08003766
Peter Xu52722982017-06-27 12:10:14 +08003767 DEFINE_PROP_END_OF_LIST(),
3768};
3769
Peter Xue5cb7e72017-06-27 12:10:13 +08003770static void migration_class_init(ObjectClass *klass, void *data)
3771{
3772 DeviceClass *dc = DEVICE_CLASS(klass);
3773
3774 dc->user_creatable = false;
Marc-André Lureau4f67d302020-01-10 19:30:32 +04003775 device_class_set_props(dc, migration_properties);
Peter Xue5cb7e72017-06-27 12:10:13 +08003776}
3777
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003778static void migration_instance_finalize(Object *obj)
3779{
3780 MigrationState *ms = MIGRATION_OBJ(obj);
3781 MigrationParameters *params = &ms->parameters;
3782
Juan Quintela87db1a72017-09-05 12:50:22 +02003783 qemu_mutex_destroy(&ms->error_mutex);
Peter Xu62df0662018-05-02 18:47:38 +08003784 qemu_mutex_destroy(&ms->qemu_file_lock);
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003785 g_free(params->tls_hostname);
3786 g_free(params->tls_creds);
Jens Freimannc7e0acd2019-10-29 12:49:02 +01003787 qemu_sem_destroy(&ms->wait_unplug_sem);
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003788 qemu_sem_destroy(&ms->rate_limit_sem);
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01003789 qemu_sem_destroy(&ms->pause_sem);
Peter Xub23c2ad2018-05-02 18:47:19 +08003790 qemu_sem_destroy(&ms->postcopy_pause_sem);
Peter Xu14b17422018-05-02 18:47:21 +08003791 qemu_sem_destroy(&ms->postcopy_pause_rp_sem);
Peter Xuedd090c2018-05-02 18:47:32 +08003792 qemu_sem_destroy(&ms->rp_state.rp_sem);
Marc-André Lureauab105cc2018-03-06 18:09:59 +01003793 error_free(ms->error);
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003794}
3795
Peter Xue5cb7e72017-06-27 12:10:13 +08003796static void migration_instance_init(Object *obj)
3797{
3798 MigrationState *ms = MIGRATION_OBJ(obj);
Peter Xu8b0b29d2017-07-18 11:39:06 +08003799 MigrationParameters *params = &ms->parameters;
Peter Xue5cb7e72017-06-27 12:10:13 +08003800
3801 ms->state = MIGRATION_STATUS_NONE;
Peter Xue5cb7e72017-06-27 12:10:13 +08003802 ms->mbps = -1;
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08003803 ms->pages_per_second = -1;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01003804 qemu_sem_init(&ms->pause_sem, 0);
Juan Quintela87db1a72017-09-05 12:50:22 +02003805 qemu_mutex_init(&ms->error_mutex);
Peter Xu8b0b29d2017-07-18 11:39:06 +08003806
3807 params->tls_hostname = g_strdup("");
3808 params->tls_creds = g_strdup("");
3809
3810 /* Set has_* up only for parameter checks */
3811 params->has_compress_level = true;
3812 params->has_compress_threads = true;
3813 params->has_decompress_threads = true;
Keqian Zhudc14a472020-02-24 10:31:42 +08003814 params->has_throttle_trigger_threshold = true;
Peter Xu8b0b29d2017-07-18 11:39:06 +08003815 params->has_cpu_throttle_initial = true;
3816 params->has_cpu_throttle_increment = true;
Keqian Zhucbbf8182020-04-13 18:15:08 +08003817 params->has_cpu_throttle_tailslow = true;
Peter Xu8b0b29d2017-07-18 11:39:06 +08003818 params->has_max_bandwidth = true;
3819 params->has_downtime_limit = true;
3820 params->has_x_checkpoint_delay = true;
3821 params->has_block_incremental = true;
Juan Quintelacbfd6c92019-02-06 13:54:06 +01003822 params->has_multifd_channels = true;
Juan Quintela96eef042019-01-16 10:35:55 +01003823 params->has_multifd_compression = true;
Juan Quintela9004db42020-01-23 17:08:52 +01003824 params->has_multifd_zlib_level = true;
Juan Quintela6a9ad152020-01-23 17:41:36 +01003825 params->has_multifd_zstd_level = true;
Juan Quintela73af8dd2017-10-05 21:30:10 +02003826 params->has_xbzrle_cache_size = true;
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01003827 params->has_max_postcopy_bandwidth = true;
Li Qiang4cbc9c72018-08-01 06:00:20 -07003828 params->has_max_cpu_throttle = true;
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00003829 params->has_announce_initial = true;
3830 params->has_announce_max = true;
3831 params->has_announce_rounds = true;
3832 params->has_announce_step = true;
Peter Xub23c2ad2018-05-02 18:47:19 +08003833
3834 qemu_sem_init(&ms->postcopy_pause_sem, 0);
Peter Xu14b17422018-05-02 18:47:21 +08003835 qemu_sem_init(&ms->postcopy_pause_rp_sem, 0);
Peter Xuedd090c2018-05-02 18:47:32 +08003836 qemu_sem_init(&ms->rp_state.rp_sem, 0);
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003837 qemu_sem_init(&ms->rate_limit_sem, 0);
Jens Freimannc7e0acd2019-10-29 12:49:02 +01003838 qemu_sem_init(&ms->wait_unplug_sem, 0);
Peter Xu62df0662018-05-02 18:47:38 +08003839 qemu_mutex_init(&ms->qemu_file_lock);
Peter Xu8b0b29d2017-07-18 11:39:06 +08003840}
3841
3842/*
3843 * Return true if check pass, false otherwise. Error will be put
3844 * inside errp if provided.
3845 */
3846static bool migration_object_check(MigrationState *ms, Error **errp)
3847{
Peter Xu6b19a7d2017-07-18 11:39:10 +08003848 MigrationCapabilityStatusList *head = NULL;
3849 /* Assuming all off */
3850 bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret;
3851 int i;
3852
Peter Xu8b0b29d2017-07-18 11:39:06 +08003853 if (!migrate_params_check(&ms->parameters, errp)) {
3854 return false;
3855 }
3856
Peter Xu6b19a7d2017-07-18 11:39:10 +08003857 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
3858 if (ms->enabled_capabilities[i]) {
Eric Blakeeaedde52020-11-12 19:13:36 -06003859 QAPI_LIST_PREPEND(head, migrate_cap_add(i, true));
Peter Xu6b19a7d2017-07-18 11:39:10 +08003860 }
3861 }
3862
3863 ret = migrate_caps_check(cap_list, head, errp);
3864
3865 /* It works with head == NULL */
3866 qapi_free_MigrationCapabilityStatusList(head);
3867
3868 return ret;
Peter Xue5cb7e72017-06-27 12:10:13 +08003869}
3870
3871static const TypeInfo migration_type = {
3872 .name = TYPE_MIGRATION,
Peter Xu01f6e142017-06-28 15:15:44 +08003873 /*
Peter Xuc8d3ff32017-07-05 16:21:23 +08003874 * NOTE: TYPE_MIGRATION is not really a device, as the object is
Markus Armbruster2194abd2020-06-10 07:32:19 +02003875 * not created using qdev_new(), it is not attached to the qdev
Peter Xuc8d3ff32017-07-05 16:21:23 +08003876 * device tree, and it is never realized.
3877 *
3878 * TODO: Make this TYPE_OBJECT once QOM provides something like
3879 * TYPE_DEVICE's "-global" properties.
Peter Xu01f6e142017-06-28 15:15:44 +08003880 */
Peter Xue5cb7e72017-06-27 12:10:13 +08003881 .parent = TYPE_DEVICE,
3882 .class_init = migration_class_init,
3883 .class_size = sizeof(MigrationClass),
3884 .instance_size = sizeof(MigrationState),
3885 .instance_init = migration_instance_init,
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003886 .instance_finalize = migration_instance_finalize,
Peter Xue5cb7e72017-06-27 12:10:13 +08003887};
3888
3889static void register_migration_types(void)
3890{
3891 type_register_static(&migration_type);
3892}
3893
3894type_init(register_migration_types);