blob: 5421042d4a8869c160f2653b48a4483f91a775c4 [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
Laurent Vivier7590a2a2020-09-21 16:49:57 +020060#define MAX_THROTTLE (128 << 20) /* Migration transfer speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000061
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010062/* Amount of time to allocate to each "chunk" of bandwidth-throttled
63 * data. */
64#define BUFFER_DELAY 100
65#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
66
Ashijeet Acharya2ff30252016-09-15 21:50:28 +053067/* Time in milliseconds we are allowed to stop the source,
68 * for sending the last part */
69#define DEFAULT_MIGRATE_SET_DOWNTIME 300
70
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -030071/* Maximum migrate downtime set to 2000 seconds */
72#define MAX_MIGRATE_DOWNTIME_SECONDS 2000
73#define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
74
Liang Li8706d2d2015-03-23 16:32:17 +080075/* Default compression thread count */
76#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080077/* Default decompression thread count, usually decompression is at
78 * least 4 times as fast as compression.*/
79#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080080/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
81#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
Jason J. Herne1626fee2015-09-08 13:12:34 -040082/* Define default autoconverge cpu throttle migration parameters */
Keqian Zhudc14a472020-02-24 10:31:42 +080083#define DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD 50
Jason J. Herned85a31d2016-04-21 14:07:18 -040084#define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
85#define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
Li Qiang4cbc9c72018-08-01 06:00:20 -070086#define DEFAULT_MIGRATE_MAX_CPU_THROTTLE 99
Liang Li8706d2d2015-03-23 16:32:17 +080087
Orit Wasserman17ad9b32012-08-06 21:42:53 +030088/* Migration XBZRLE default cache size */
Juan Quintela73af8dd2017-10-05 21:30:10 +020089#define DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE (64 * 1024 * 1024)
Orit Wasserman17ad9b32012-08-06 21:42:53 +030090
Zhang Chen131b2152018-09-03 12:38:45 +080091/* The delay time (in ms) between two COLO checkpoints */
92#define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY (200 * 100)
Juan Quintela4075fb12016-01-15 08:56:17 +010093#define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2
Juan Quintela96eef042019-01-16 10:35:55 +010094#define DEFAULT_MIGRATE_MULTIFD_COMPRESSION MULTIFD_COMPRESSION_NONE
Juan Quintela9004db42020-01-23 17:08:52 +010095/* 0: means nocompress, 1: best speed, ... 9: best compress ratio */
96#define DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL 1
Juan Quintela6a9ad152020-01-23 17:41:36 +010097/* 0: means nocompress, 1: best speed, ... 20: best compress ratio */
98#define DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL 1
zhanghailiang68b53592016-10-27 14:43:01 +080099
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +0100100/* Background transfer rate for postcopy, 0 means unlimited, note
101 * that page requests can still exceed this limit.
102 */
103#define DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH 0
104
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +0000105/*
106 * Parameters for self_announce_delay giving a stream of RARP/ARP
107 * packets after migration.
108 */
109#define DEFAULT_MIGRATE_ANNOUNCE_INITIAL 50
110#define DEFAULT_MIGRATE_ANNOUNCE_MAX 550
111#define DEFAULT_MIGRATE_ANNOUNCE_ROUNDS 5
112#define DEFAULT_MIGRATE_ANNOUNCE_STEP 100
113
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100114static NotifierList migration_state_notifiers =
115 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
116
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000117static bool deferred_incoming;
118
Juan Quintelada6f1792017-04-24 17:37:14 +0200119/* Messages sent on the return path from destination to source */
120enum mig_rp_message_type {
121 MIG_RP_MSG_INVALID = 0, /* Must be 0 */
122 MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */
123 MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */
124
125 MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */
126 MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */
Peter Xua335deb2018-05-02 18:47:28 +0800127 MIG_RP_MSG_RECV_BITMAP, /* send recved_bitmap back to source */
Peter Xu13955b82018-05-02 18:47:30 +0800128 MIG_RP_MSG_RESUME_ACK, /* tell source that we are ready to resume */
Juan Quintelada6f1792017-04-24 17:37:14 +0200129
130 MIG_RP_MSG_MAX
131};
132
Juan Quintela17549e82011-10-05 13:50:43 +0200133/* When we add fault tolerance, we could have several
134 migrations at once. For now we don't need to add
135 dynamic creation of migration */
136
Peter Xue5cb7e72017-06-27 12:10:13 +0800137static MigrationState *current_migration;
Peter Xue1b1b1b2018-05-02 18:47:35 +0800138static MigrationIncomingState *current_incoming;
Peter Xue5cb7e72017-06-27 12:10:13 +0800139
Peter Xu8b0b29d2017-07-18 11:39:06 +0800140static bool migration_object_check(MigrationState *ms, Error **errp);
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +0100141static int migration_maybe_pause(MigrationState *s,
142 int *current_active_state,
143 int new_state);
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +0000144static void migrate_fd_cancel(MigrationState *s);
Peter Xu8b0b29d2017-07-18 11:39:06 +0800145
Peter Xu8f8bfff2020-10-21 17:27:18 -0400146static gint page_request_addr_cmp(gconstpointer ap, gconstpointer bp)
147{
148 uintptr_t a = (uintptr_t) ap, b = (uintptr_t) bp;
149
150 return (a > b) - (a < b);
151}
152
Peter Xue5cb7e72017-06-27 12:10:13 +0800153void migration_object_init(void)
154{
Peter Xu4ffdb332017-06-27 12:10:18 +0800155 MachineState *ms = MACHINE(qdev_get_machine());
Peter Xu8b0b29d2017-07-18 11:39:06 +0800156 Error *err = NULL;
Peter Xu4ffdb332017-06-27 12:10:18 +0800157
Peter Xue5cb7e72017-06-27 12:10:13 +0800158 /* This can only be called once. */
159 assert(!current_migration);
160 current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION));
Peter Xu4ffdb332017-06-27 12:10:18 +0800161
Peter Xue1b1b1b2018-05-02 18:47:35 +0800162 /*
163 * Init the migrate incoming object as well no matter whether
164 * we'll use it or not.
165 */
166 assert(!current_incoming);
167 current_incoming = g_new0(MigrationIncomingState, 1);
168 current_incoming->state = MIGRATION_STATUS_NONE;
169 current_incoming->postcopy_remote_fds =
170 g_array_new(FALSE, TRUE, sizeof(struct PostCopyFD));
171 qemu_mutex_init(&current_incoming->rp_mutex);
172 qemu_event_init(&current_incoming->main_thread_load_event, false);
173 qemu_sem_init(&current_incoming->postcopy_pause_sem_dst, 0);
174 qemu_sem_init(&current_incoming->postcopy_pause_sem_fault, 0);
Peter Xu8f8bfff2020-10-21 17:27:18 -0400175 qemu_mutex_init(&current_incoming->page_request_mutex);
176 current_incoming->page_requested = g_tree_new(page_request_addr_cmp);
Peter Xue1b1b1b2018-05-02 18:47:35 +0800177
Peter Xu8b0b29d2017-07-18 11:39:06 +0800178 if (!migration_object_check(current_migration, &err)) {
179 error_report_err(err);
180 exit(1);
181 }
182
Peter Xu4ffdb332017-06-27 12:10:18 +0800183 /*
184 * We cannot really do this in migration_instance_init() since at
185 * that time global properties are not yet applied, then this
186 * value will be definitely replaced by something else.
187 */
188 if (ms->enforce_config_section) {
189 current_migration->send_configuration = true;
190 }
Peter Xue5cb7e72017-06-27 12:10:13 +0800191}
192
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +0000193void migration_shutdown(void)
Vladimir Sementsov-Ogievskiy1f895602017-12-28 12:16:16 +0300194{
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +0000195 /*
196 * Cancel the current migration - that will (eventually)
197 * stop the migration using this structure
198 */
199 migrate_fd_cancel(current_migration);
Vladimir Sementsov-Ogievskiy1f895602017-12-28 12:16:16 +0300200 object_unref(OBJECT(current_migration));
Vladimir Sementsov-Ogievskiy1499ab02020-07-27 22:42:31 +0300201
202 /*
203 * Cancel outgoing migration of dirty bitmaps. It should
204 * at least unref used block nodes.
205 */
206 dirty_bitmap_mig_cancel_outgoing();
207
208 /*
209 * Cancel incoming migration of dirty bitmaps. Dirty bitmaps
210 * are non-critical data, and their loss never considered as
211 * something serious.
212 */
213 dirty_bitmap_mig_cancel_incoming();
Vladimir Sementsov-Ogievskiy1f895602017-12-28 12:16:16 +0300214}
215
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100216/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +0200217MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +0200218{
Peter Xue5cb7e72017-06-27 12:10:13 +0800219 /* This can only be called after the object created. */
220 assert(current_migration);
221 return current_migration;
Juan Quintela17549e82011-10-05 13:50:43 +0200222}
223
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100224MigrationIncomingState *migration_incoming_get_current(void)
225{
Peter Xue1b1b1b2018-05-02 18:47:35 +0800226 assert(current_incoming);
227 return current_incoming;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100228}
229
230void migration_incoming_state_destroy(void)
231{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100232 struct MigrationIncomingState *mis = migration_incoming_get_current();
233
Peter Xu34826552017-05-19 14:43:29 +0800234 if (mis->to_src_file) {
Peter Xu660819b2017-05-19 14:43:30 +0800235 /* Tell source that we are done */
236 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
Peter Xu34826552017-05-19 14:43:29 +0800237 qemu_fclose(mis->to_src_file);
238 mis->to_src_file = NULL;
239 }
240
Peter Xu660819b2017-05-19 14:43:30 +0800241 if (mis->from_src_file) {
242 qemu_fclose(mis->from_src_file);
243 mis->from_src_file = NULL;
244 }
Dr. David Alan Gilbert00fa4fc2018-03-12 17:21:04 +0000245 if (mis->postcopy_remote_fds) {
246 g_array_free(mis->postcopy_remote_fds, TRUE);
247 mis->postcopy_remote_fds = NULL;
248 }
Peter Xu660819b2017-05-19 14:43:30 +0800249
Dr. David Alan Gilbert1783c002020-09-28 15:48:30 +0100250 qemu_event_reset(&mis->main_thread_load_event);
251
Peter Xu8f8bfff2020-10-21 17:27:18 -0400252 if (mis->page_requested) {
253 g_tree_destroy(mis->page_requested);
254 mis->page_requested = NULL;
255 }
256
Juan Quintela9aca82b2019-02-27 11:51:27 +0100257 if (mis->socket_address_list) {
258 qapi_free_SocketAddressList(mis->socket_address_list);
259 mis->socket_address_list = NULL;
260 }
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100261}
262
Juan Quintelab05dc722015-07-07 14:44:05 +0200263static void migrate_generate_event(int new_state)
264{
265 if (migrate_use_events()) {
Peter Xu3ab72382018-08-15 21:37:37 +0800266 qapi_event_send_migration(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200267 }
268}
269
Dr. David Alan Gilbert0f073f42018-04-16 18:09:30 +0100270static bool migrate_late_block_activate(void)
271{
272 MigrationState *s;
273
274 s = migrate_get_current();
275
276 return s->enabled_capabilities[
277 MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE];
278}
279
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000280/*
281 * Called on -incoming with a defer: uri.
282 * The migration can be started later after any parameters have been
283 * changed.
284 */
285static void deferred_incoming_migration(Error **errp)
286{
287 if (deferred_incoming) {
288 error_setg(errp, "Incoming migration already deferred");
289 }
290 deferred_incoming = true;
291}
292
Juan Quintelada6f1792017-04-24 17:37:14 +0200293/*
294 * Send a message on the return channel back to the source
295 * of the migration.
296 */
Peter Xud6208e32018-02-08 18:31:12 +0800297static int migrate_send_rp_message(MigrationIncomingState *mis,
298 enum mig_rp_message_type message_type,
299 uint16_t len, void *data)
Juan Quintelada6f1792017-04-24 17:37:14 +0200300{
Peter Xud6208e32018-02-08 18:31:12 +0800301 int ret = 0;
302
Juan Quintelada6f1792017-04-24 17:37:14 +0200303 trace_migrate_send_rp_message((int)message_type, len);
304 qemu_mutex_lock(&mis->rp_mutex);
Peter Xud6208e32018-02-08 18:31:12 +0800305
306 /*
307 * It's possible that the file handle got lost due to network
308 * failures.
309 */
310 if (!mis->to_src_file) {
311 ret = -EIO;
312 goto error;
313 }
314
Juan Quintelada6f1792017-04-24 17:37:14 +0200315 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
316 qemu_put_be16(mis->to_src_file, len);
317 qemu_put_buffer(mis->to_src_file, data, len);
318 qemu_fflush(mis->to_src_file);
Peter Xud6208e32018-02-08 18:31:12 +0800319
320 /* It's possible that qemu file got error during sending */
321 ret = qemu_file_get_error(mis->to_src_file);
322
323error:
Juan Quintelada6f1792017-04-24 17:37:14 +0200324 qemu_mutex_unlock(&mis->rp_mutex);
Peter Xud6208e32018-02-08 18:31:12 +0800325 return ret;
Juan Quintelada6f1792017-04-24 17:37:14 +0200326}
327
Peter Xu2e2bce12020-09-08 16:30:18 -0400328/* Request one page from the source VM at the given start address.
329 * rb: the RAMBlock to request the page in
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000330 * Start: Address offset within the RB
331 * Len: Length in bytes required - must be a multiple of pagesize
332 */
Peter Xu7a267fc2020-10-21 17:27:17 -0400333int migrate_send_rp_message_req_pages(MigrationIncomingState *mis,
334 RAMBlock *rb, ram_addr_t start)
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000335{
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100336 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000337 size_t msglen = 12; /* start + len */
Peter Xu2e2bce12020-09-08 16:30:18 -0400338 size_t len = qemu_ram_pagesize(rb);
Peter Xud6208e32018-02-08 18:31:12 +0800339 enum mig_rp_message_type msg_type;
Peter Xu2e2bce12020-09-08 16:30:18 -0400340 const char *rbname;
341 int rbname_len;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000342
343 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
344 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
345
Peter Xu2e2bce12020-09-08 16:30:18 -0400346 /*
347 * We maintain the last ramblock that we requested for page. Note that we
348 * don't need locking because this function will only be called within the
349 * postcopy ram fault thread.
350 */
351 if (rb != mis->last_rb) {
352 mis->last_rb = rb;
353
354 rbname = qemu_ram_get_idstr(rb);
355 rbname_len = strlen(rbname);
356
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000357 assert(rbname_len < 256);
358
359 bufc[msglen++] = rbname_len;
360 memcpy(bufc + msglen, rbname, rbname_len);
361 msglen += rbname_len;
Peter Xud6208e32018-02-08 18:31:12 +0800362 msg_type = MIG_RP_MSG_REQ_PAGES_ID;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000363 } else {
Peter Xud6208e32018-02-08 18:31:12 +0800364 msg_type = MIG_RP_MSG_REQ_PAGES;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000365 }
Peter Xud6208e32018-02-08 18:31:12 +0800366
367 return migrate_send_rp_message(mis, msg_type, msglen, bufc);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000368}
369
Peter Xu7a267fc2020-10-21 17:27:17 -0400370int migrate_send_rp_req_pages(MigrationIncomingState *mis,
Peter Xu8f8bfff2020-10-21 17:27:18 -0400371 RAMBlock *rb, ram_addr_t start, uint64_t haddr)
Peter Xu7a267fc2020-10-21 17:27:17 -0400372{
Peter Xu8f8bfff2020-10-21 17:27:18 -0400373 void *aligned = (void *)(uintptr_t)(haddr & (-qemu_ram_pagesize(rb)));
374 bool received;
375
376 WITH_QEMU_LOCK_GUARD(&mis->page_request_mutex) {
377 received = ramblock_recv_bitmap_test_byte_offset(rb, start);
378 if (!received && !g_tree_lookup(mis->page_requested, aligned)) {
379 /*
380 * The page has not been received, and it's not yet in the page
381 * request list. Queue it. Set the value of element to 1, so that
382 * things like g_tree_lookup() will return TRUE (1) when found.
383 */
384 g_tree_insert(mis->page_requested, aligned, (gpointer)1);
385 mis->page_requested_count++;
386 trace_postcopy_page_req_add(aligned, mis->page_requested_count);
387 }
388 }
389
390 /*
391 * If the page is there, skip sending the message. We don't even need the
392 * lock because as long as the page arrived, it'll be there forever.
393 */
394 if (received) {
395 return 0;
396 }
397
Peter Xu7a267fc2020-10-21 17:27:17 -0400398 return migrate_send_rp_message_req_pages(mis, rb, start);
399}
400
Zhang Chenaad555c2018-09-03 12:38:47 +0800401static bool migration_colo_enabled;
402bool migration_incoming_colo_enabled(void)
403{
404 return migration_colo_enabled;
405}
406
407void migration_incoming_disable_colo(void)
408{
David Hildenbrand18b1d3c2020-06-26 09:22:36 +0200409 ram_block_discard_disable(false);
Zhang Chenaad555c2018-09-03 12:38:47 +0800410 migration_colo_enabled = false;
411}
412
David Hildenbrand18b1d3c2020-06-26 09:22:36 +0200413int migration_incoming_enable_colo(void)
Zhang Chenaad555c2018-09-03 12:38:47 +0800414{
David Hildenbrand18b1d3c2020-06-26 09:22:36 +0200415 if (ram_block_discard_disable(true)) {
416 error_report("COLO: cannot disable RAM discard");
417 return -EBUSY;
418 }
Zhang Chenaad555c2018-09-03 12:38:47 +0800419 migration_colo_enabled = true;
David Hildenbrand18b1d3c2020-06-26 09:22:36 +0200420 return 0;
Zhang Chenaad555c2018-09-03 12:38:47 +0800421}
422
Juan Quintela9aca82b2019-02-27 11:51:27 +0100423void migrate_add_address(SocketAddress *address)
424{
425 MigrationIncomingState *mis = migration_incoming_get_current();
426 SocketAddressList *addrs;
427
428 addrs = g_new0(SocketAddressList, 1);
429 addrs->next = mis->socket_address_list;
430 mis->socket_address_list = addrs;
431 addrs->value = QAPI_CLONE(SocketAddress, address);
432}
433
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200434void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000435{
Longpeng(Mike)d658f652020-08-06 15:40:29 +0800436 const char *p = NULL;
aliguori34c9dd82008-10-13 03:14:31 +0000437
Peter Xu3ab72382018-08-15 21:37:37 +0800438 qapi_event_send_migration(MIGRATION_STATUS_SETUP);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000439 if (!strcmp(uri, "defer")) {
440 deferred_incoming_migration(errp);
Longpeng(Mike)d658f652020-08-06 15:40:29 +0800441 } else if (strstart(uri, "tcp:", &p) ||
Longpeng(Mike)9ba3b2b2020-08-06 15:40:30 +0800442 strstart(uri, "unix:", NULL) ||
443 strstart(uri, "vsock:", NULL)) {
Longpeng(Mike)d658f652020-08-06 15:40:29 +0800444 socket_start_incoming_migration(p ? p : uri, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400445#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000446 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400447 rdma_start_incoming_migration(p, errp);
448#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000449 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200450 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000451 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200452 fd_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000453 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100454 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200455 }
aliguori5bb79102008-10-13 03:12:02 +0000456}
457
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300458static void process_incoming_migration_bh(void *opaque)
459{
460 Error *local_err = NULL;
461 MigrationIncomingState *mis = opaque;
462
Dr. David Alan Gilbert0f073f42018-04-16 18:09:30 +0100463 /* If capability late_block_activate is set:
464 * Only fire up the block code now if we're going to restart the
465 * VM, else 'cont' will do it.
466 * This causes file locking to happen; so we don't want it to happen
467 * unless we really are starting the VM.
468 */
469 if (!migrate_late_block_activate() ||
470 (autostart && (!global_state_received() ||
471 global_state_get_runstate() == RUN_STATE_RUNNING))) {
472 /* Make sure all file formats flush their mutable metadata.
473 * If we get an error here, just don't restart the VM yet. */
474 bdrv_invalidate_cache_all(&local_err);
475 if (local_err) {
476 error_report_err(local_err);
477 local_err = NULL;
478 autostart = false;
479 }
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200480 }
481
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300482 /*
483 * This must happen after all error conditions are dealt with and
484 * we're sure the VM is going to be running on this host.
485 */
Dr. David Alan Gilbert76595052019-02-27 13:24:08 +0000486 qemu_announce_self(&mis->announce_timer, migrate_announce_params());
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300487
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100488 if (multifd_load_cleanup(&local_err) != 0) {
489 error_report_err(local_err);
490 autostart = false;
491 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300492 /* If global state section was not received or we are in running
493 state, we need to obey autostart. Any other state is set with
494 runstate_set. */
495
Vladimir Sementsov-Ogievskiyb35ebdf2018-03-13 15:34:01 -0400496 dirty_bitmap_mig_before_vm_start();
497
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300498 if (!global_state_received() ||
499 global_state_get_runstate() == RUN_STATE_RUNNING) {
500 if (autostart) {
501 vm_start();
502 } else {
503 runstate_set(RUN_STATE_PAUSED);
504 }
Zhang Chendb009722019-03-03 22:50:17 +0800505 } else if (migration_incoming_colo_enabled()) {
506 migration_incoming_disable_colo();
507 vm_start();
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300508 } else {
509 runstate_set(global_state_get_runstate());
510 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300511 /*
512 * This must happen after any state changes since as soon as an external
513 * observer sees this event they might start to prod at the VM assuming
514 * it's ready to use.
515 */
516 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
517 MIGRATION_STATUS_COMPLETED);
518 qemu_bh_delete(mis->bh);
519 migration_incoming_state_destroy();
520}
521
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200522static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200523{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100524 MigrationIncomingState *mis = migration_incoming_get_current();
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000525 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200526 int ret;
Zhang Chen8e48ac92018-09-03 12:38:46 +0800527 Error *local_err = NULL;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200528
Juan Quintela4f0fae72017-07-24 12:42:02 +0200529 assert(mis->from_src_file);
Lidong Chen92370982018-08-06 21:29:35 +0800530 mis->migration_incoming_co = qemu_coroutine_self();
Dr. David Alan Gilbert67f11b52017-02-24 18:28:34 +0000531 mis->largest_page_size = qemu_ram_pagesize_largest();
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000532 postcopy_state_set(POSTCOPY_INCOMING_NONE);
zhanghailiang93d7af62015-12-16 11:47:34 +0000533 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
534 MIGRATION_STATUS_ACTIVE);
Juan Quintela4f0fae72017-07-24 12:42:02 +0200535 ret = qemu_loadvm_state(mis->from_src_file);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100536
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000537 ps = postcopy_state_get();
538 trace_process_incoming_migration_co_end(ret, ps);
539 if (ps != POSTCOPY_INCOMING_NONE) {
540 if (ps == POSTCOPY_INCOMING_ADVISE) {
541 /*
542 * Where a migration had postcopy enabled (and thus went to advise)
543 * but managed to complete within the precopy period, we can use
544 * the normal exit.
545 */
546 postcopy_ram_incoming_cleanup(mis);
547 } else if (ret >= 0) {
548 /*
549 * Postcopy was started, cleanup should happen at the end of the
550 * postcopy thread.
551 */
552 trace_process_incoming_migration_co_postcopy_end_main();
553 return;
554 }
555 /* Else if something went wrong then just fall out of the normal exit */
556 }
557
zhanghailiang25d0c162016-10-27 14:42:55 +0800558 /* we get COLO info, and know if we are in COLO mode */
Zhang Chenaad555c2018-09-03 12:38:47 +0800559 if (!ret && migration_incoming_colo_enabled()) {
Zhang Chen8e48ac92018-09-03 12:38:46 +0800560 /* Make sure all file formats flush their mutable metadata */
561 bdrv_invalidate_cache_all(&local_err);
562 if (local_err) {
Zhang Chen8e48ac92018-09-03 12:38:46 +0800563 error_report_err(local_err);
Fei Li6d99c2d2019-01-13 22:08:49 +0800564 goto fail;
Zhang Chen8e48ac92018-09-03 12:38:46 +0800565 }
566
zhanghailiang25d0c162016-10-27 14:42:55 +0800567 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
568 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
569 mis->have_colo_incoming_thread = true;
570 qemu_coroutine_yield();
571
572 /* Wait checkpoint incoming thread exit before free resource */
573 qemu_thread_join(&mis->colo_incoming_thread);
Zhang Chen13af18f2018-09-03 12:38:48 +0800574 /* We hold the global iothread lock, so it is safe here */
575 colo_release_ram_cache();
zhanghailiang25d0c162016-10-27 14:42:55 +0800576 }
577
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200578 if (ret < 0) {
Peter Lievendb80fac2014-06-10 11:29:16 +0200579 error_report("load of migration failed: %s", strerror(-ret));
Fei Li6d99c2d2019-01-13 22:08:49 +0800580 goto fail;
Juan Quintela511c0232010-06-09 14:10:55 +0200581 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300582 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
583 qemu_bh_schedule(mis->bh);
Lidong Chen92370982018-08-06 21:29:35 +0800584 mis->migration_incoming_co = NULL;
Fei Li6d99c2d2019-01-13 22:08:49 +0800585 return;
586fail:
587 local_err = NULL;
588 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
589 MIGRATION_STATUS_FAILED);
590 qemu_fclose(mis->from_src_file);
591 if (multifd_load_cleanup(&local_err) != 0) {
592 error_report_err(local_err);
593 }
594 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200595}
596
Juan Quintelab673eab2019-06-12 11:44:19 +0200597/**
598 * @migration_incoming_setup: Setup incoming migration
599 *
600 * Returns 0 for no error or 1 for error
601 *
602 * @f: file for main migration channel
603 * @errp: where to put errors
604 */
605static int migration_incoming_setup(QEMUFile *f, Error **errp)
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200606{
Juan Quintela4f0fae72017-07-24 12:42:02 +0200607 MigrationIncomingState *mis = migration_incoming_get_current();
Juan Quintelab673eab2019-06-12 11:44:19 +0200608 Error *local_err = NULL;
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200609
Juan Quintelab673eab2019-06-12 11:44:19 +0200610 if (multifd_load_setup(&local_err) != 0) {
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100611 /* We haven't been able to create multifd threads
612 nothing better to do */
Juan Quintelab673eab2019-06-12 11:44:19 +0200613 error_report_err(local_err);
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100614 exit(EXIT_FAILURE);
615 }
616
Juan Quintela4f0fae72017-07-24 12:42:02 +0200617 if (!mis->from_src_file) {
618 mis->from_src_file = f;
619 }
Daniel P. Berrange06ad5132016-04-27 11:04:56 +0100620 qemu_file_set_blocking(f, false);
Juan Quintelab673eab2019-06-12 11:44:19 +0200621 return 0;
Juan Quintelae595a012017-07-17 12:30:25 +0200622}
623
Juan Quintela36c2f8b2018-03-07 08:40:52 +0100624void migration_incoming_process(void)
Juan Quintelae595a012017-07-17 12:30:25 +0200625{
626 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL);
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200627 qemu_coroutine_enter(co);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200628}
629
Peter Xu884835f2018-06-27 21:22:45 +0800630/* Returns true if recovered from a paused migration, otherwise false */
631static bool postcopy_try_recover(QEMUFile *f)
Juan Quintelae595a012017-07-17 12:30:25 +0200632{
Peter Xud96c9e82018-05-02 18:47:26 +0800633 MigrationIncomingState *mis = migration_incoming_get_current();
634
635 if (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
636 /* Resumed from a paused postcopy migration */
637
638 mis->from_src_file = f;
639 /* Postcopy has standalone thread to do vm load */
640 qemu_file_set_blocking(f, true);
641
642 /* Re-configure the return path */
643 mis->to_src_file = qemu_file_get_return_path(f);
644
645 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
646 MIGRATION_STATUS_POSTCOPY_RECOVER);
647
648 /*
649 * Here, we only wake up the main loading thread (while the
650 * fault thread will still be waiting), so that we can receive
651 * commands from source now, and answer it if needed. The
652 * fault thread will be woken up afterwards until we are sure
653 * that source is ready to reply to page requests.
654 */
655 qemu_sem_post(&mis->postcopy_pause_sem_dst);
Peter Xu884835f2018-06-27 21:22:45 +0800656 return true;
Peter Xud96c9e82018-05-02 18:47:26 +0800657 }
Peter Xu884835f2018-06-27 21:22:45 +0800658
659 return false;
660}
661
Juan Quintelab673eab2019-06-12 11:44:19 +0200662void migration_fd_process_incoming(QEMUFile *f, Error **errp)
Peter Xu884835f2018-06-27 21:22:45 +0800663{
Juan Quintelab673eab2019-06-12 11:44:19 +0200664 Error *local_err = NULL;
665
Peter Xu884835f2018-06-27 21:22:45 +0800666 if (postcopy_try_recover(f)) {
667 return;
668 }
669
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 Xu884835f2018-06-27 21:22:45 +0800674 migration_incoming_process();
Juan Quintelae595a012017-07-17 12:30:25 +0200675}
676
Fei Li49ed0d22019-01-13 22:08:46 +0800677void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp)
Juan Quintela4f0fae72017-07-24 12:42:02 +0200678{
679 MigrationIncomingState *mis = migration_incoming_get_current();
Juan Quintelab673eab2019-06-12 11:44:19 +0200680 Error *local_err = NULL;
Peter Xua429e7f2018-06-27 21:22:46 +0800681 bool start_migration;
Juan Quintela4f0fae72017-07-24 12:42:02 +0200682
683 if (!mis->from_src_file) {
Peter Xua429e7f2018-06-27 21:22:46 +0800684 /* The first connection (multifd may have multiple) */
Juan Quintela4f0fae72017-07-24 12:42:02 +0200685 QEMUFile *f = qemu_fopen_channel_input(ioc);
Peter Xua429e7f2018-06-27 21:22:46 +0800686
687 /* If it's a recovery, we're done */
Peter Xu884835f2018-06-27 21:22:45 +0800688 if (postcopy_try_recover(f)) {
689 return;
690 }
Peter Xua429e7f2018-06-27 21:22:46 +0800691
Juan Quintelab673eab2019-06-12 11:44:19 +0200692 if (migration_incoming_setup(f, &local_err)) {
Markus Armbruster2155cea2020-07-22 10:40:47 +0200693 error_propagate(errp, local_err);
Juan Quintelab673eab2019-06-12 11:44:19 +0200694 return;
695 }
Peter Xua429e7f2018-06-27 21:22:46 +0800696
697 /*
698 * Common migration only needs one channel, so we can start
699 * right now. Multifd needs more than one channel, we wait.
700 */
701 start_migration = !migrate_use_multifd();
702 } else {
703 /* Multiple connections */
704 assert(migrate_use_multifd());
Fei Li49ed0d22019-01-13 22:08:46 +0800705 start_migration = multifd_recv_new_channel(ioc, &local_err);
706 if (local_err) {
707 error_propagate(errp, local_err);
708 return;
709 }
Juan Quintela4f0fae72017-07-24 12:42:02 +0200710 }
Peter Xu81e62052018-06-27 21:22:44 +0800711
Peter Xua429e7f2018-06-27 21:22:46 +0800712 if (start_migration) {
Peter Xu81e62052018-06-27 21:22:44 +0800713 migration_incoming_process();
714 }
Juan Quintela4f0fae72017-07-24 12:42:02 +0200715}
716
Juan Quintela428d8902017-07-24 13:06:25 +0200717/**
718 * @migration_has_all_channels: We have received all channels that we need
719 *
720 * Returns true when we have got connections to all the channels that
721 * we need for migration.
722 */
723bool migration_has_all_channels(void)
724{
Daniel P. Berrangéca273df2018-06-19 17:35:52 +0100725 MigrationIncomingState *mis = migration_incoming_get_current();
Juan Quintela62c1e0c2018-02-19 18:59:02 +0100726 bool all_channels;
727
728 all_channels = multifd_recv_all_channels_created();
729
Daniel P. Berrangéca273df2018-06-19 17:35:52 +0100730 return all_channels && mis->from_src_file != NULL;
Juan Quintela428d8902017-07-24 13:06:25 +0200731}
732
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000733/*
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000734 * Send a 'SHUT' message on the return channel with the given value
735 * to indicate that we've finished with the RP. Non-0 value indicates
736 * error.
737 */
738void migrate_send_rp_shut(MigrationIncomingState *mis,
739 uint32_t value)
740{
741 uint32_t buf;
742
743 buf = cpu_to_be32(value);
744 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
745}
746
747/*
748 * Send a 'PONG' message on the return channel with the given value
749 * (normally in response to a 'PING')
750 */
751void migrate_send_rp_pong(MigrationIncomingState *mis,
752 uint32_t value)
753{
754 uint32_t buf;
755
756 buf = cpu_to_be32(value);
757 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
758}
759
Peter Xua335deb2018-05-02 18:47:28 +0800760void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
761 char *block_name)
762{
763 char buf[512];
764 int len;
765 int64_t res;
766
767 /*
768 * First, we send the header part. It contains only the len of
769 * idstr, and the idstr itself.
770 */
771 len = strlen(block_name);
772 buf[0] = len;
773 memcpy(buf + 1, block_name, len);
774
775 if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
776 error_report("%s: MSG_RP_RECV_BITMAP only used for recovery",
777 __func__);
778 return;
779 }
780
781 migrate_send_rp_message(mis, MIG_RP_MSG_RECV_BITMAP, len + 1, buf);
782
783 /*
784 * Next, we dump the received bitmap to the stream.
785 *
786 * TODO: currently we are safe since we are the only one that is
787 * using the to_src_file handle (fault thread is still paused),
788 * and it's ok even not taking the mutex. However the best way is
789 * to take the lock before sending the message header, and release
790 * the lock after sending the bitmap.
791 */
792 qemu_mutex_lock(&mis->rp_mutex);
793 res = ramblock_recv_bitmap_send(mis->to_src_file, block_name);
794 qemu_mutex_unlock(&mis->rp_mutex);
795
796 trace_migrate_send_rp_recv_bitmap(block_name, res);
797}
798
Peter Xu13955b82018-05-02 18:47:30 +0800799void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value)
800{
801 uint32_t buf;
802
803 buf = cpu_to_be32(value);
804 migrate_send_rp_message(mis, MIG_RP_MSG_RESUME_ACK, sizeof(buf), &buf);
805}
806
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300807MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
808{
809 MigrationCapabilityStatusList *head = NULL;
810 MigrationCapabilityStatusList *caps;
811 MigrationState *s = migrate_get_current();
812 int i;
813
Michael Tokarev387eede2013-10-05 13:18:28 +0400814 caps = NULL; /* silence compiler warning */
Eric Blake7fb1cf12015-11-18 01:52:57 -0700815 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Dr. David Alan Gilberted1701c2017-05-15 15:05:29 +0100816#ifndef CONFIG_LIVE_BLOCK_MIGRATION
817 if (i == MIGRATION_CAPABILITY_BLOCK) {
818 continue;
819 }
820#endif
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300821 if (head == NULL) {
822 head = g_malloc0(sizeof(*caps));
823 caps = head;
824 } else {
825 caps->next = g_malloc0(sizeof(*caps));
826 caps = caps->next;
827 }
828 caps->value =
829 g_malloc(sizeof(*caps->value));
830 caps->value->capability = i;
831 caps->value->state = s->enabled_capabilities[i];
832 }
833
834 return head;
835}
836
Liang Li85de8322015-03-23 16:32:28 +0800837MigrationParameters *qmp_query_migrate_parameters(Error **errp)
838{
839 MigrationParameters *params;
840 MigrationState *s = migrate_get_current();
841
Markus Armbrustere87fae42017-07-18 12:57:38 +0200842 /* TODO use QAPI_CLONE() instead of duplicating it inline */
Liang Li85de8322015-03-23 16:32:28 +0800843 params = g_malloc0(sizeof(*params));
Eric Blakede63ab62016-09-08 22:14:15 -0500844 params->has_compress_level = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100845 params->compress_level = s->parameters.compress_level;
Eric Blakede63ab62016-09-08 22:14:15 -0500846 params->has_compress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100847 params->compress_threads = s->parameters.compress_threads;
Xiao Guangrong1d588722018-08-21 16:10:20 +0800848 params->has_compress_wait_thread = true;
849 params->compress_wait_thread = s->parameters.compress_wait_thread;
Eric Blakede63ab62016-09-08 22:14:15 -0500850 params->has_decompress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100851 params->decompress_threads = s->parameters.decompress_threads;
Keqian Zhudc14a472020-02-24 10:31:42 +0800852 params->has_throttle_trigger_threshold = true;
853 params->throttle_trigger_threshold = s->parameters.throttle_trigger_threshold;
Eric Blakede63ab62016-09-08 22:14:15 -0500854 params->has_cpu_throttle_initial = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100855 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
Eric Blakede63ab62016-09-08 22:14:15 -0500856 params->has_cpu_throttle_increment = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100857 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
Keqian Zhucbbf8182020-04-13 18:15:08 +0800858 params->has_cpu_throttle_tailslow = true;
859 params->cpu_throttle_tailslow = s->parameters.cpu_throttle_tailslow;
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200860 params->has_tls_creds = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100861 params->tls_creds = g_strdup(s->parameters.tls_creds);
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200862 params->has_tls_hostname = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100863 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
Daniel P. Berranged2f1d292019-02-27 14:53:24 +0000864 params->has_tls_authz = true;
Mao Zhongyi7cd75cb2020-03-25 09:49:30 +0800865 params->tls_authz = g_strdup(s->parameters.tls_authz ?
866 s->parameters.tls_authz : "");
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530867 params->has_max_bandwidth = true;
868 params->max_bandwidth = s->parameters.max_bandwidth;
869 params->has_downtime_limit = true;
870 params->downtime_limit = s->parameters.downtime_limit;
zhanghailiangfe39a4d2016-11-02 15:42:09 +0800871 params->has_x_checkpoint_delay = true;
zhanghailiang68b53592016-10-27 14:43:01 +0800872 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
Juan Quintela2833c592017-04-05 18:32:37 +0200873 params->has_block_incremental = true;
874 params->block_incremental = s->parameters.block_incremental;
Juan Quintelacbfd6c92019-02-06 13:54:06 +0100875 params->has_multifd_channels = true;
876 params->multifd_channels = s->parameters.multifd_channels;
Juan Quintela96eef042019-01-16 10:35:55 +0100877 params->has_multifd_compression = true;
878 params->multifd_compression = s->parameters.multifd_compression;
Juan Quintela9004db42020-01-23 17:08:52 +0100879 params->has_multifd_zlib_level = true;
880 params->multifd_zlib_level = s->parameters.multifd_zlib_level;
Juan Quintela6a9ad152020-01-23 17:41:36 +0100881 params->has_multifd_zstd_level = true;
882 params->multifd_zstd_level = s->parameters.multifd_zstd_level;
Juan Quintela73af8dd2017-10-05 21:30:10 +0200883 params->has_xbzrle_cache_size = true;
884 params->xbzrle_cache_size = s->parameters.xbzrle_cache_size;
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +0100885 params->has_max_postcopy_bandwidth = true;
886 params->max_postcopy_bandwidth = s->parameters.max_postcopy_bandwidth;
Li Qiang4cbc9c72018-08-01 06:00:20 -0700887 params->has_max_cpu_throttle = true;
888 params->max_cpu_throttle = s->parameters.max_cpu_throttle;
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +0000889 params->has_announce_initial = true;
890 params->announce_initial = s->parameters.announce_initial;
891 params->has_announce_max = true;
892 params->announce_max = s->parameters.announce_max;
893 params->has_announce_rounds = true;
894 params->announce_rounds = s->parameters.announce_rounds;
895 params->has_announce_step = true;
896 params->announce_step = s->parameters.announce_step;
Liang Li85de8322015-03-23 16:32:28 +0800897
Max Reitz31e4c352020-08-20 17:07:23 +0200898 if (s->parameters.has_block_bitmap_mapping) {
899 params->has_block_bitmap_mapping = true;
900 params->block_bitmap_mapping =
901 QAPI_CLONE(BitmapMigrationNodeAliasList,
902 s->parameters.block_bitmap_mapping);
903 }
904
Liang Li85de8322015-03-23 16:32:28 +0800905 return params;
906}
907
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +0000908AnnounceParameters *migrate_announce_params(void)
909{
910 static AnnounceParameters ap;
911
912 MigrationState *s = migrate_get_current();
913
914 ap.initial = s->parameters.announce_initial;
915 ap.max = s->parameters.announce_max;
916 ap.rounds = s->parameters.announce_rounds;
917 ap.step = s->parameters.announce_step;
918
919 return &ap;
920}
921
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000922/*
923 * Return true if we're already in the middle of a migration
924 * (i.e. any of the active or setup states)
925 */
Jia Lina3d63da12018-10-26 16:36:20 +0800926bool migration_is_setup_or_active(int state)
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000927{
928 switch (state) {
929 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000930 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Peter Xua688d2c2018-05-02 18:47:18 +0800931 case MIGRATION_STATUS_POSTCOPY_PAUSED:
Peter Xu135b87b2018-05-02 18:47:25 +0800932 case MIGRATION_STATUS_POSTCOPY_RECOVER:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000933 case MIGRATION_STATUS_SETUP:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +0100934 case MIGRATION_STATUS_PRE_SWITCHOVER:
935 case MIGRATION_STATUS_DEVICE:
Jens Freimannc7e0acd2019-10-29 12:49:02 +0100936 case MIGRATION_STATUS_WAIT_UNPLUG:
zhanghailiang19dd4082020-02-24 14:54:12 +0800937 case MIGRATION_STATUS_COLO:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000938 return true;
939
940 default:
941 return false;
942
943 }
944}
945
Juan Quintela392d87e2020-01-21 15:39:23 +0100946bool migration_is_running(int state)
947{
948 switch (state) {
949 case MIGRATION_STATUS_ACTIVE:
950 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
951 case MIGRATION_STATUS_POSTCOPY_PAUSED:
952 case MIGRATION_STATUS_POSTCOPY_RECOVER:
953 case MIGRATION_STATUS_SETUP:
954 case MIGRATION_STATUS_PRE_SWITCHOVER:
955 case MIGRATION_STATUS_DEVICE:
956 case MIGRATION_STATUS_WAIT_UNPLUG:
957 case MIGRATION_STATUS_CANCELLING:
Juan Quintela392d87e2020-01-21 15:39:23 +0100958 return true;
959
960 default:
961 return false;
962
963 }
964}
965
Wei Yang640dfb12019-07-16 08:54:11 +0800966static void populate_time_info(MigrationInfo *info, MigrationState *s)
967{
968 info->has_status = true;
969 info->has_setup_time = true;
970 info->setup_time = s->setup_time;
971 if (s->state == MIGRATION_STATUS_COMPLETED) {
972 info->has_total_time = true;
973 info->total_time = s->total_time;
974 info->has_downtime = true;
975 info->downtime = s->downtime;
976 } else {
977 info->has_total_time = true;
978 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
979 s->start_time;
980 info->has_expected_downtime = true;
981 info->expected_downtime = s->expected_downtime;
982 }
983}
984
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100985static void populate_ram_info(MigrationInfo *info, MigrationState *s)
986{
987 info->has_ram = true;
988 info->ram = g_malloc0(sizeof(*info->ram));
Juan Quintela93604472017-06-06 19:49:03 +0200989 info->ram->transferred = ram_counters.transferred;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100990 info->ram->total = ram_bytes_total();
Juan Quintela93604472017-06-06 19:49:03 +0200991 info->ram->duplicate = ram_counters.duplicate;
Juan Quintelabedf53c2017-03-13 20:35:54 +0100992 /* legacy value. It is not used anymore */
993 info->ram->skipped = 0;
Juan Quintela93604472017-06-06 19:49:03 +0200994 info->ram->normal = ram_counters.normal;
995 info->ram->normal_bytes = ram_counters.normal *
Juan Quintela20afaed2017-03-21 09:09:14 +0100996 qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100997 info->ram->mbps = s->mbps;
Juan Quintela93604472017-06-06 19:49:03 +0200998 info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
999 info->ram->postcopy_requests = ram_counters.postcopy_requests;
Chao Fan030ce1f2017-03-21 10:22:43 +08001000 info->ram->page_size = qemu_target_page_size();
Juan Quintelaa61c45b2018-06-26 15:20:11 +02001001 info->ram->multifd_bytes = ram_counters.multifd_bytes;
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08001002 info->ram->pages_per_second = s->pages_per_second;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +01001003
Juan Quintela114f5ae2017-05-04 10:09:21 +02001004 if (migrate_use_xbzrle()) {
1005 info->has_xbzrle_cache = true;
1006 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
1007 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
Juan Quintela93604472017-06-06 19:49:03 +02001008 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
1009 info->xbzrle_cache->pages = xbzrle_counters.pages;
1010 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
1011 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
Wei Wange460a4b2020-04-30 08:59:35 +08001012 info->xbzrle_cache->encoding_rate = xbzrle_counters.encoding_rate;
Juan Quintela93604472017-06-06 19:49:03 +02001013 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
Juan Quintela114f5ae2017-05-04 10:09:21 +02001014 }
1015
Xiao Guangrong76e03002018-09-06 15:01:00 +08001016 if (migrate_use_compression()) {
1017 info->has_compression = true;
1018 info->compression = g_malloc0(sizeof(*info->compression));
1019 info->compression->pages = compression_counters.pages;
1020 info->compression->busy = compression_counters.busy;
1021 info->compression->busy_rate = compression_counters.busy_rate;
1022 info->compression->compressed_size =
1023 compression_counters.compressed_size;
1024 info->compression->compression_rate =
1025 compression_counters.compression_rate;
1026 }
1027
Juan Quintela338182c2017-05-03 13:16:38 +02001028 if (cpu_throttle_active()) {
1029 info->has_cpu_throttle_percentage = true;
1030 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
1031 }
1032
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +01001033 if (s->state != MIGRATION_STATUS_COMPLETED) {
1034 info->ram->remaining = ram_bytes_remaining();
Juan Quintela93604472017-06-06 19:49:03 +02001035 info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +01001036 }
1037}
1038
Juan Quintela930ac042017-05-04 10:21:46 +02001039static void populate_disk_info(MigrationInfo *info)
1040{
1041 if (blk_mig_active()) {
1042 info->has_disk = true;
1043 info->disk = g_malloc0(sizeof(*info->disk));
1044 info->disk->transferred = blk_mig_bytes_transferred();
1045 info->disk->remaining = blk_mig_bytes_remaining();
1046 info->disk->total = blk_mig_bytes_total();
1047 }
1048}
1049
Alexey Perevalov65ace062018-03-22 21:17:27 +03001050static void fill_source_migration_info(MigrationInfo *info)
aliguori5bb79102008-10-13 03:12:02 +00001051{
Juan Quintela17549e82011-10-05 13:50:43 +02001052 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +00001053
Juan Quintela17549e82011-10-05 13:50:43 +02001054 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +08001055 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +02001056 /* no migration has happened ever */
Alexey Perevalov65ace062018-03-22 21:17:27 +03001057 /* do not overwrite destination migration status */
1058 return;
zhanghailiang31194732015-03-13 16:08:38 +08001059 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001060 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -04001061 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001062 break;
zhanghailiang31194732015-03-13 16:08:38 +08001063 case MIGRATION_STATUS_ACTIVE:
1064 case MIGRATION_STATUS_CANCELLING:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001065 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +01001066 case MIGRATION_STATUS_PRE_SWITCHOVER:
1067 case MIGRATION_STATUS_DEVICE:
Peter Xua688d2c2018-05-02 18:47:18 +08001068 case MIGRATION_STATUS_POSTCOPY_PAUSED:
Peter Xu135b87b2018-05-02 18:47:25 +08001069 case MIGRATION_STATUS_POSTCOPY_RECOVER:
Wei Yang640dfb12019-07-16 08:54:11 +08001070 /* TODO add some postcopy stats */
1071 populate_time_info(info, s);
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +01001072 populate_ram_info(info, s);
Juan Quintela930ac042017-05-04 10:21:46 +02001073 populate_disk_info(info);
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001074 break;
zhanghailiang0b827d52016-10-27 14:42:54 +08001075 case MIGRATION_STATUS_COLO:
1076 info->has_status = true;
1077 /* TODO: display COLO specific information (checkpoint info etc.) */
1078 break;
zhanghailiang31194732015-03-13 16:08:38 +08001079 case MIGRATION_STATUS_COMPLETED:
Wei Yang640dfb12019-07-16 08:54:11 +08001080 populate_time_info(info, s);
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +01001081 populate_ram_info(info, s);
Juan Quintela17549e82011-10-05 13:50:43 +02001082 break;
zhanghailiang31194732015-03-13 16:08:38 +08001083 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -03001084 info->has_status = true;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001085 if (s->error) {
1086 info->has_error_desc = true;
1087 info->error_desc = g_strdup(error_get_pretty(s->error));
1088 }
Juan Quintela17549e82011-10-05 13:50:43 +02001089 break;
zhanghailiang31194732015-03-13 16:08:38 +08001090 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -03001091 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +02001092 break;
Jens Freimannc7e0acd2019-10-29 12:49:02 +01001093 case MIGRATION_STATUS_WAIT_UNPLUG:
1094 info->has_status = true;
1095 break;
aliguori5bb79102008-10-13 03:12:02 +00001096 }
zhanghailiangcde63fb2015-03-13 16:08:41 +08001097 info->status = s->state;
aliguori5bb79102008-10-13 03:12:02 +00001098}
1099
Peter Xu4a842142017-07-18 11:39:08 +08001100/**
1101 * @migration_caps_check - check capability validity
1102 *
1103 * @cap_list: old capability list, array of bool
1104 * @params: new capabilities to be applied soon
1105 * @errp: set *errp if the check failed, with reason
1106 *
1107 * Returns true if check passed, otherwise false.
1108 */
1109static bool migrate_caps_check(bool *cap_list,
1110 MigrationCapabilityStatusList *params,
1111 Error **errp)
Orit Wasserman00458432012-08-06 21:42:48 +03001112{
Orit Wasserman00458432012-08-06 21:42:48 +03001113 MigrationCapabilityStatusList *cap;
Peter Xu4a842142017-07-18 11:39:08 +08001114 bool old_postcopy_cap;
Alexey Perevalovd7651f12017-09-19 19:47:56 +03001115 MigrationIncomingState *mis = migration_incoming_get_current();
Orit Wasserman00458432012-08-06 21:42:48 +03001116
Peter Xu4a842142017-07-18 11:39:08 +08001117 old_postcopy_cap = cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Orit Wasserman00458432012-08-06 21:42:48 +03001118
1119 for (cap = params; cap; cap = cap->next) {
Peter Xu4a842142017-07-18 11:39:08 +08001120 cap_list[cap->value->capability] = cap->value->state;
Orit Wasserman00458432012-08-06 21:42:48 +03001121 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001122
Peter Xu4a842142017-07-18 11:39:08 +08001123#ifndef CONFIG_LIVE_BLOCK_MIGRATION
1124 if (cap_list[MIGRATION_CAPABILITY_BLOCK]) {
1125 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
1126 "block migration");
1127 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
1128 return false;
1129 }
1130#endif
1131
Zhang Chen7e934f52018-11-15 03:09:12 +08001132#ifndef CONFIG_REPLICATION
1133 if (cap_list[MIGRATION_CAPABILITY_X_COLO]) {
1134 error_setg(errp, "QEMU compiled without replication module"
1135 " can't enable COLO");
1136 error_append_hint(errp, "Please enable replication before COLO.\n");
1137 return false;
1138 }
1139#endif
1140
Peter Xu4a842142017-07-18 11:39:08 +08001141 if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +01001142 /* This check is reasonably expensive, so only when it's being
1143 * set the first time, also it's only the destination that needs
1144 * special support.
1145 */
1146 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
Alexey Perevalovd7651f12017-09-19 19:47:56 +03001147 !postcopy_ram_supported_by_host(mis)) {
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +01001148 /* postcopy_ram_supported_by_host will have emitted a more
1149 * detailed message
1150 */
Peter Xu4a842142017-07-18 11:39:08 +08001151 error_setg(errp, "Postcopy is not supported");
1152 return false;
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +01001153 }
Yury Kotov18269062019-02-15 20:45:45 +03001154
1155 if (cap_list[MIGRATION_CAPABILITY_X_IGNORE_SHARED]) {
1156 error_setg(errp, "Postcopy is not compatible with ignore-shared");
1157 return false;
1158 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001159 }
Peter Xu4a842142017-07-18 11:39:08 +08001160
1161 return true;
1162}
1163
Alexey Perevalov65ace062018-03-22 21:17:27 +03001164static void fill_destination_migration_info(MigrationInfo *info)
1165{
1166 MigrationIncomingState *mis = migration_incoming_get_current();
1167
Juan Quintela9aca82b2019-02-27 11:51:27 +01001168 if (mis->socket_address_list) {
1169 info->has_socket_address = true;
1170 info->socket_address =
1171 QAPI_CLONE(SocketAddressList, mis->socket_address_list);
1172 }
1173
Alexey Perevalov65ace062018-03-22 21:17:27 +03001174 switch (mis->state) {
1175 case MIGRATION_STATUS_NONE:
1176 return;
Alexey Perevalov65ace062018-03-22 21:17:27 +03001177 case MIGRATION_STATUS_SETUP:
1178 case MIGRATION_STATUS_CANCELLING:
1179 case MIGRATION_STATUS_CANCELLED:
1180 case MIGRATION_STATUS_ACTIVE:
1181 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Peter Xu3c9928d2018-07-10 17:18:56 +08001182 case MIGRATION_STATUS_POSTCOPY_PAUSED:
1183 case MIGRATION_STATUS_POSTCOPY_RECOVER:
Alexey Perevalov65ace062018-03-22 21:17:27 +03001184 case MIGRATION_STATUS_FAILED:
1185 case MIGRATION_STATUS_COLO:
1186 info->has_status = true;
1187 break;
1188 case MIGRATION_STATUS_COMPLETED:
1189 info->has_status = true;
1190 fill_destination_postcopy_migration_info(info);
1191 break;
1192 }
1193 info->status = mis->state;
1194}
1195
1196MigrationInfo *qmp_query_migrate(Error **errp)
1197{
1198 MigrationInfo *info = g_malloc0(sizeof(*info));
1199
1200 fill_destination_migration_info(info);
1201 fill_source_migration_info(info);
1202
1203 return info;
1204}
1205
Peter Xu4a842142017-07-18 11:39:08 +08001206void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
1207 Error **errp)
1208{
1209 MigrationState *s = migrate_get_current();
1210 MigrationCapabilityStatusList *cap;
Peter Xudd0ee302018-03-05 17:49:38 +08001211 bool cap_list[MIGRATION_CAPABILITY__MAX];
Peter Xu4a842142017-07-18 11:39:08 +08001212
Juan Quintela392d87e2020-01-21 15:39:23 +01001213 if (migration_is_running(s->state)) {
Peter Xu4a842142017-07-18 11:39:08 +08001214 error_setg(errp, QERR_MIGRATION_ACTIVE);
1215 return;
1216 }
1217
Peter Xudd0ee302018-03-05 17:49:38 +08001218 memcpy(cap_list, s->enabled_capabilities, sizeof(cap_list));
1219 if (!migrate_caps_check(cap_list, params, errp)) {
Peter Xu4a842142017-07-18 11:39:08 +08001220 return;
1221 }
1222
1223 for (cap = params; cap; cap = cap->next) {
1224 s->enabled_capabilities[cap->value->capability] = cap->value->state;
1225 }
Orit Wasserman00458432012-08-06 21:42:48 +03001226}
1227
Peter Xu16d063b2017-07-18 11:39:04 +08001228/*
1229 * Check whether the parameters are valid. Error will be put into errp
1230 * (if provided). Return true if valid, otherwise false.
1231 */
1232static bool migrate_params_check(MigrationParameters *params, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +08001233{
Eric Blake7f375e02016-09-08 22:14:16 -05001234 if (params->has_compress_level &&
Juan Quintela741d4082017-12-01 13:08:38 +01001235 (params->compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001236 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
1237 "is invalid, it should be in the range of 0 to 9");
Peter Xu16d063b2017-07-18 11:39:04 +08001238 return false;
Liang Li85de8322015-03-23 16:32:28 +08001239 }
Peter Xu16d063b2017-07-18 11:39:04 +08001240
Juan Quintela741d4082017-12-01 13:08:38 +01001241 if (params->has_compress_threads && (params->compress_threads < 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001242 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1243 "compress_threads",
1244 "is invalid, it should be in the range of 1 to 255");
Peter Xu16d063b2017-07-18 11:39:04 +08001245 return false;
Liang Li85de8322015-03-23 16:32:28 +08001246 }
Peter Xu16d063b2017-07-18 11:39:04 +08001247
Juan Quintela741d4082017-12-01 13:08:38 +01001248 if (params->has_decompress_threads && (params->decompress_threads < 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001249 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1250 "decompress_threads",
1251 "is invalid, it should be in the range of 1 to 255");
Peter Xu16d063b2017-07-18 11:39:04 +08001252 return false;
Liang Li85de8322015-03-23 16:32:28 +08001253 }
Peter Xu16d063b2017-07-18 11:39:04 +08001254
Keqian Zhudc14a472020-02-24 10:31:42 +08001255 if (params->has_throttle_trigger_threshold &&
1256 (params->throttle_trigger_threshold < 1 ||
1257 params->throttle_trigger_threshold > 100)) {
1258 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1259 "throttle_trigger_threshold",
1260 "an integer in the range of 1 to 100");
1261 return false;
1262 }
1263
Eric Blake7f375e02016-09-08 22:14:16 -05001264 if (params->has_cpu_throttle_initial &&
1265 (params->cpu_throttle_initial < 1 ||
1266 params->cpu_throttle_initial > 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_initial",
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
Eric Blake7f375e02016-09-08 22:14:16 -05001273 if (params->has_cpu_throttle_increment &&
1274 (params->cpu_throttle_increment < 1 ||
1275 params->cpu_throttle_increment > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -04001276 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -04001277 "cpu_throttle_increment",
Jason J. Herne1626fee2015-09-08 13:12:34 -04001278 "an integer in the range of 1 to 99");
Peter Xu16d063b2017-07-18 11:39:04 +08001279 return false;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001280 }
Peter Xu16d063b2017-07-18 11:39:04 +08001281
Juan Quintela741d4082017-12-01 13:08:38 +01001282 if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) {
Mao Zhongyi2ee30cf2020-03-31 16:22:05 +08001283 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1284 "max_bandwidth",
1285 "an integer in the range of 0 to "stringify(SIZE_MAX)
1286 " bytes/second");
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
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301290 if (params->has_downtime_limit &&
Juan Quintela741d4082017-12-01 13:08:38 +01001291 (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
Mao Zhongyi2ee30cf2020-03-31 16:22:05 +08001292 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1293 "downtime_limit",
1294 "an integer in the range of 0 to "
Mao Zhongyi7ac55292020-03-31 16:22:07 +08001295 stringify(MAX_MIGRATE_DOWNTIME)" ms");
Peter Xu16d063b2017-07-18 11:39:04 +08001296 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301297 }
Peter Xu16d063b2017-07-18 11:39:04 +08001298
Juan Quintela741d4082017-12-01 13:08:38 +01001299 /* x_checkpoint_delay is now always positive */
1300
Juan Quintelacbfd6c92019-02-06 13:54:06 +01001301 if (params->has_multifd_channels && (params->multifd_channels < 1)) {
Juan Quintela4075fb12016-01-15 08:56:17 +01001302 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1303 "multifd_channels",
1304 "is invalid, it should be in the range of 1 to 255");
1305 return false;
1306 }
Peter Xu16d063b2017-07-18 11:39:04 +08001307
Juan Quintela9004db42020-01-23 17:08:52 +01001308 if (params->has_multifd_zlib_level &&
1309 (params->multifd_zlib_level > 9)) {
1310 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zlib_level",
1311 "is invalid, it should be in the range of 0 to 9");
1312 return false;
1313 }
1314
Juan Quintela6a9ad152020-01-23 17:41:36 +01001315 if (params->has_multifd_zstd_level &&
1316 (params->multifd_zstd_level > 20)) {
1317 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zstd_level",
1318 "is invalid, it should be in the range of 0 to 20");
1319 return false;
1320 }
1321
Juan Quintela73af8dd2017-10-05 21:30:10 +02001322 if (params->has_xbzrle_cache_size &&
1323 (params->xbzrle_cache_size < qemu_target_page_size() ||
1324 !is_power_of_2(params->xbzrle_cache_size))) {
1325 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1326 "xbzrle_cache_size",
1327 "is invalid, it should be bigger than target page size"
Mao Zhongyi06b1c6f2020-03-20 22:32:16 +08001328 " and a power of 2");
Juan Quintela73af8dd2017-10-05 21:30:10 +02001329 return false;
1330 }
1331
Li Qiang4cbc9c72018-08-01 06:00:20 -07001332 if (params->has_max_cpu_throttle &&
1333 (params->max_cpu_throttle < params->cpu_throttle_initial ||
1334 params->max_cpu_throttle > 99)) {
1335 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1336 "max_cpu_throttle",
1337 "an integer in the range of cpu_throttle_initial to 99");
1338 return false;
1339 }
1340
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00001341 if (params->has_announce_initial &&
1342 params->announce_initial > 100000) {
1343 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1344 "announce_initial",
1345 "is invalid, it must be less than 100000 ms");
1346 return false;
1347 }
1348 if (params->has_announce_max &&
1349 params->announce_max > 100000) {
1350 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1351 "announce_max",
1352 "is invalid, it must be less than 100000 ms");
1353 return false;
1354 }
1355 if (params->has_announce_rounds &&
1356 params->announce_rounds > 1000) {
1357 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1358 "announce_rounds",
1359 "is invalid, it must be in the range of 0 to 1000");
1360 return false;
1361 }
1362 if (params->has_announce_step &&
1363 (params->announce_step < 1 ||
1364 params->announce_step > 10000)) {
1365 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1366 "announce_step",
1367 "is invalid, it must be in the range of 1 to 10000 ms");
1368 return false;
1369 }
Max Reitz31e4c352020-08-20 17:07:23 +02001370
1371 if (params->has_block_bitmap_mapping &&
1372 !check_dirty_bitmap_mig_alias_map(params->block_bitmap_mapping, errp)) {
1373 error_prepend(errp, "Invalid mapping given for block-bitmap-mapping: ");
1374 return false;
1375 }
1376
Peter Xu16d063b2017-07-18 11:39:04 +08001377 return true;
1378}
1379
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001380static void migrate_params_test_apply(MigrateSetParameters *params,
1381 MigrationParameters *dest)
1382{
1383 *dest = migrate_get_current()->parameters;
1384
1385 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1386
1387 if (params->has_compress_level) {
1388 dest->compress_level = params->compress_level;
1389 }
1390
1391 if (params->has_compress_threads) {
1392 dest->compress_threads = params->compress_threads;
1393 }
1394
Xiao Guangrong1d588722018-08-21 16:10:20 +08001395 if (params->has_compress_wait_thread) {
1396 dest->compress_wait_thread = params->compress_wait_thread;
1397 }
1398
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001399 if (params->has_decompress_threads) {
1400 dest->decompress_threads = params->decompress_threads;
1401 }
1402
Keqian Zhudc14a472020-02-24 10:31:42 +08001403 if (params->has_throttle_trigger_threshold) {
1404 dest->throttle_trigger_threshold = params->throttle_trigger_threshold;
1405 }
1406
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001407 if (params->has_cpu_throttle_initial) {
1408 dest->cpu_throttle_initial = params->cpu_throttle_initial;
1409 }
1410
1411 if (params->has_cpu_throttle_increment) {
1412 dest->cpu_throttle_increment = params->cpu_throttle_increment;
1413 }
1414
Keqian Zhucbbf8182020-04-13 18:15:08 +08001415 if (params->has_cpu_throttle_tailslow) {
1416 dest->cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1417 }
1418
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001419 if (params->has_tls_creds) {
Markus Armbruster01fa5592017-07-18 14:42:04 +02001420 assert(params->tls_creds->type == QTYPE_QSTRING);
Zheng Chuan9728ebf2020-07-09 16:28:25 +08001421 dest->tls_creds = params->tls_creds->u.s;
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001422 }
1423
1424 if (params->has_tls_hostname) {
Markus Armbruster01fa5592017-07-18 14:42:04 +02001425 assert(params->tls_hostname->type == QTYPE_QSTRING);
Zheng Chuan9728ebf2020-07-09 16:28:25 +08001426 dest->tls_hostname = params->tls_hostname->u.s;
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001427 }
1428
1429 if (params->has_max_bandwidth) {
1430 dest->max_bandwidth = params->max_bandwidth;
1431 }
1432
1433 if (params->has_downtime_limit) {
1434 dest->downtime_limit = params->downtime_limit;
1435 }
1436
1437 if (params->has_x_checkpoint_delay) {
1438 dest->x_checkpoint_delay = params->x_checkpoint_delay;
1439 }
1440
1441 if (params->has_block_incremental) {
1442 dest->block_incremental = params->block_incremental;
1443 }
Juan Quintelacbfd6c92019-02-06 13:54:06 +01001444 if (params->has_multifd_channels) {
1445 dest->multifd_channels = params->multifd_channels;
Juan Quintela5e7577a2017-10-09 18:07:56 +02001446 }
Juan Quintela96eef042019-01-16 10:35:55 +01001447 if (params->has_multifd_compression) {
1448 dest->multifd_compression = params->multifd_compression;
1449 }
Juan Quintela73af8dd2017-10-05 21:30:10 +02001450 if (params->has_xbzrle_cache_size) {
1451 dest->xbzrle_cache_size = params->xbzrle_cache_size;
1452 }
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01001453 if (params->has_max_postcopy_bandwidth) {
1454 dest->max_postcopy_bandwidth = params->max_postcopy_bandwidth;
1455 }
Li Qiang4cbc9c72018-08-01 06:00:20 -07001456 if (params->has_max_cpu_throttle) {
1457 dest->max_cpu_throttle = params->max_cpu_throttle;
1458 }
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00001459 if (params->has_announce_initial) {
1460 dest->announce_initial = params->announce_initial;
1461 }
1462 if (params->has_announce_max) {
1463 dest->announce_max = params->announce_max;
1464 }
1465 if (params->has_announce_rounds) {
1466 dest->announce_rounds = params->announce_rounds;
1467 }
1468 if (params->has_announce_step) {
1469 dest->announce_step = params->announce_step;
1470 }
Max Reitz31e4c352020-08-20 17:07:23 +02001471
1472 if (params->has_block_bitmap_mapping) {
1473 dest->has_block_bitmap_mapping = true;
1474 dest->block_bitmap_mapping = params->block_bitmap_mapping;
1475 }
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001476}
1477
Juan Quintela73af8dd2017-10-05 21:30:10 +02001478static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
Peter Xu16d063b2017-07-18 11:39:04 +08001479{
1480 MigrationState *s = migrate_get_current();
1481
Markus Armbrustere87fae42017-07-18 12:57:38 +02001482 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1483
Eric Blake7f375e02016-09-08 22:14:16 -05001484 if (params->has_compress_level) {
1485 s->parameters.compress_level = params->compress_level;
Liang Li85de8322015-03-23 16:32:28 +08001486 }
Peter Xu476c72a2017-07-18 11:39:05 +08001487
Eric Blake7f375e02016-09-08 22:14:16 -05001488 if (params->has_compress_threads) {
1489 s->parameters.compress_threads = params->compress_threads;
Liang Li85de8322015-03-23 16:32:28 +08001490 }
Peter Xu476c72a2017-07-18 11:39:05 +08001491
Xiao Guangrong1d588722018-08-21 16:10:20 +08001492 if (params->has_compress_wait_thread) {
1493 s->parameters.compress_wait_thread = params->compress_wait_thread;
1494 }
1495
Eric Blake7f375e02016-09-08 22:14:16 -05001496 if (params->has_decompress_threads) {
1497 s->parameters.decompress_threads = params->decompress_threads;
Liang Li85de8322015-03-23 16:32:28 +08001498 }
Peter Xu476c72a2017-07-18 11:39:05 +08001499
Keqian Zhudc14a472020-02-24 10:31:42 +08001500 if (params->has_throttle_trigger_threshold) {
1501 s->parameters.throttle_trigger_threshold = params->throttle_trigger_threshold;
1502 }
1503
Eric Blake7f375e02016-09-08 22:14:16 -05001504 if (params->has_cpu_throttle_initial) {
1505 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001506 }
Peter Xu476c72a2017-07-18 11:39:05 +08001507
Eric Blake7f375e02016-09-08 22:14:16 -05001508 if (params->has_cpu_throttle_increment) {
1509 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001510 }
Peter Xu476c72a2017-07-18 11:39:05 +08001511
Keqian Zhucbbf8182020-04-13 18:15:08 +08001512 if (params->has_cpu_throttle_tailslow) {
1513 s->parameters.cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1514 }
1515
Eric Blake7f375e02016-09-08 22:14:16 -05001516 if (params->has_tls_creds) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001517 g_free(s->parameters.tls_creds);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001518 assert(params->tls_creds->type == QTYPE_QSTRING);
1519 s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001520 }
Peter Xu476c72a2017-07-18 11:39:05 +08001521
Eric Blake7f375e02016-09-08 22:14:16 -05001522 if (params->has_tls_hostname) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001523 g_free(s->parameters.tls_hostname);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001524 assert(params->tls_hostname->type == QTYPE_QSTRING);
1525 s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001526 }
Peter Xu476c72a2017-07-18 11:39:05 +08001527
Daniel P. Berranged2f1d292019-02-27 14:53:24 +00001528 if (params->has_tls_authz) {
1529 g_free(s->parameters.tls_authz);
1530 assert(params->tls_authz->type == QTYPE_QSTRING);
1531 s->parameters.tls_authz = g_strdup(params->tls_authz->u.s);
1532 }
1533
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301534 if (params->has_max_bandwidth) {
1535 s->parameters.max_bandwidth = params->max_bandwidth;
Dr. David Alan Gilbertc38c1c12019-03-08 10:12:10 +00001536 if (s->to_dst_file && !migration_in_postcopy()) {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301537 qemu_file_set_rate_limit(s->to_dst_file,
1538 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
1539 }
1540 }
Peter Xu476c72a2017-07-18 11:39:05 +08001541
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301542 if (params->has_downtime_limit) {
1543 s->parameters.downtime_limit = params->downtime_limit;
1544 }
zhanghailiang68b53592016-10-27 14:43:01 +08001545
1546 if (params->has_x_checkpoint_delay) {
1547 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
zhanghailiang479125d2017-01-17 20:57:42 +08001548 if (migration_in_colo_state()) {
1549 colo_checkpoint_notify(s);
1550 }
zhanghailiang68b53592016-10-27 14:43:01 +08001551 }
Peter Xu476c72a2017-07-18 11:39:05 +08001552
Juan Quintela2833c592017-04-05 18:32:37 +02001553 if (params->has_block_incremental) {
1554 s->parameters.block_incremental = params->block_incremental;
1555 }
Juan Quintelacbfd6c92019-02-06 13:54:06 +01001556 if (params->has_multifd_channels) {
1557 s->parameters.multifd_channels = params->multifd_channels;
Juan Quintela4075fb12016-01-15 08:56:17 +01001558 }
Juan Quintela96eef042019-01-16 10:35:55 +01001559 if (params->has_multifd_compression) {
1560 s->parameters.multifd_compression = params->multifd_compression;
1561 }
Juan Quintela73af8dd2017-10-05 21:30:10 +02001562 if (params->has_xbzrle_cache_size) {
1563 s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
1564 xbzrle_cache_resize(params->xbzrle_cache_size, errp);
1565 }
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01001566 if (params->has_max_postcopy_bandwidth) {
1567 s->parameters.max_postcopy_bandwidth = params->max_postcopy_bandwidth;
Dr. David Alan Gilbertc38c1c12019-03-08 10:12:10 +00001568 if (s->to_dst_file && migration_in_postcopy()) {
1569 qemu_file_set_rate_limit(s->to_dst_file,
1570 s->parameters.max_postcopy_bandwidth / XFER_LIMIT_RATIO);
1571 }
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01001572 }
Li Qiang4cbc9c72018-08-01 06:00:20 -07001573 if (params->has_max_cpu_throttle) {
1574 s->parameters.max_cpu_throttle = params->max_cpu_throttle;
1575 }
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00001576 if (params->has_announce_initial) {
1577 s->parameters.announce_initial = params->announce_initial;
1578 }
1579 if (params->has_announce_max) {
1580 s->parameters.announce_max = params->announce_max;
1581 }
1582 if (params->has_announce_rounds) {
1583 s->parameters.announce_rounds = params->announce_rounds;
1584 }
1585 if (params->has_announce_step) {
1586 s->parameters.announce_step = params->announce_step;
1587 }
Max Reitz31e4c352020-08-20 17:07:23 +02001588
1589 if (params->has_block_bitmap_mapping) {
1590 qapi_free_BitmapMigrationNodeAliasList(
1591 s->parameters.block_bitmap_mapping);
1592
1593 s->parameters.has_block_bitmap_mapping = true;
1594 s->parameters.block_bitmap_mapping =
1595 QAPI_CLONE(BitmapMigrationNodeAliasList,
1596 params->block_bitmap_mapping);
1597 }
Liang Li85de8322015-03-23 16:32:28 +08001598}
1599
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001600void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
Peter Xu476c72a2017-07-18 11:39:05 +08001601{
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001602 MigrationParameters tmp;
1603
Markus Armbruster01fa5592017-07-18 14:42:04 +02001604 /* TODO Rewrite "" to null instead */
1605 if (params->has_tls_creds
1606 && params->tls_creds->type == QTYPE_QNULL) {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001607 qobject_unref(params->tls_creds->u.n);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001608 params->tls_creds->type = QTYPE_QSTRING;
1609 params->tls_creds->u.s = strdup("");
1610 }
1611 /* TODO Rewrite "" to null instead */
1612 if (params->has_tls_hostname
1613 && params->tls_hostname->type == QTYPE_QNULL) {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001614 qobject_unref(params->tls_hostname->u.n);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001615 params->tls_hostname->type = QTYPE_QSTRING;
1616 params->tls_hostname->u.s = strdup("");
1617 }
1618
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001619 migrate_params_test_apply(params, &tmp);
1620
1621 if (!migrate_params_check(&tmp, errp)) {
Peter Xu476c72a2017-07-18 11:39:05 +08001622 /* Invalid parameter */
1623 return;
1624 }
1625
Juan Quintela73af8dd2017-10-05 21:30:10 +02001626 migrate_params_apply(params, errp);
Peter Xu476c72a2017-07-18 11:39:05 +08001627}
1628
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001629
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001630void qmp_migrate_start_postcopy(Error **errp)
1631{
1632 MigrationState *s = migrate_get_current();
1633
Vladimir Sementsov-Ogievskiy16b0fd32018-03-13 15:34:01 -04001634 if (!migrate_postcopy()) {
Dr. David Alan Gilberta54d3402015-11-12 11:34:44 +00001635 error_setg(errp, "Enable postcopy with migrate_set_capability before"
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001636 " the start of migration");
1637 return;
1638 }
1639
1640 if (s->state == MIGRATION_STATUS_NONE) {
1641 error_setg(errp, "Postcopy must be started after migration has been"
1642 " started");
1643 return;
1644 }
1645 /*
1646 * we don't error if migration has finished since that would be racy
1647 * with issuing this command.
1648 */
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001649 qatomic_set(&s->start_postcopy, true);
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001650}
1651
aliguori065e2812008-11-11 16:46:33 +00001652/* shared migration helpers */
1653
zhanghailiang48781e52015-12-16 11:47:33 +00001654void migrate_set_state(int *state, int old_state, int new_state)
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +00001655{
Peter Xua31fede2017-08-30 16:32:01 +08001656 assert(new_state < MIGRATION_STATUS__MAX);
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001657 if (qatomic_cmpxchg(state, old_state, new_state) == old_state) {
Peter Xua31fede2017-08-30 16:32:01 +08001658 trace_migrate_set_state(MigrationStatus_str(new_state));
Juan Quintelab05dc722015-07-07 14:44:05 +02001659 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +00001660 }
1661}
1662
Peter Xu4e4a3d32017-07-18 11:39:09 +08001663static MigrationCapabilityStatusList *migrate_cap_add(
1664 MigrationCapabilityStatusList *list,
1665 MigrationCapability index,
1666 bool state)
Juan Quintela2833c592017-04-05 18:32:37 +02001667{
1668 MigrationCapabilityStatusList *cap;
1669
1670 cap = g_new0(MigrationCapabilityStatusList, 1);
1671 cap->value = g_new0(MigrationCapabilityStatus, 1);
Peter Xu4e4a3d32017-07-18 11:39:09 +08001672 cap->value->capability = index;
1673 cap->value->state = state;
1674 cap->next = list;
1675
1676 return cap;
1677}
1678
1679void migrate_set_block_enabled(bool value, Error **errp)
1680{
1681 MigrationCapabilityStatusList *cap;
1682
1683 cap = migrate_cap_add(NULL, MIGRATION_CAPABILITY_BLOCK, value);
Juan Quintela2833c592017-04-05 18:32:37 +02001684 qmp_migrate_set_capabilities(cap, errp);
1685 qapi_free_MigrationCapabilityStatusList(cap);
1686}
1687
1688static void migrate_set_block_incremental(MigrationState *s, bool value)
1689{
1690 s->parameters.block_incremental = value;
1691}
1692
1693static void block_cleanup_parameters(MigrationState *s)
1694{
1695 if (s->must_remove_block_options) {
1696 /* setting to false can never fail */
1697 migrate_set_block_enabled(false, &error_abort);
1698 migrate_set_block_incremental(s, false);
1699 s->must_remove_block_options = false;
1700 }
1701}
1702
Yury Kotovfd392cf2019-04-08 14:33:43 +03001703static void migrate_fd_cleanup(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +00001704{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001705 qemu_bh_delete(s->cleanup_bh);
1706 s->cleanup_bh = NULL;
1707
Peter Xu0ceccd82018-01-03 20:20:06 +08001708 qemu_savevm_state_cleanup();
1709
zhanghailiang89a02a92016-01-15 11:37:42 +08001710 if (s->to_dst_file) {
Peter Xu62df0662018-05-02 18:47:38 +08001711 QEMUFile *tmp;
Juan Quintelaf986c3d2016-01-14 16:52:55 +01001712
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001713 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001714 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001715 if (s->migration_thread_running) {
1716 qemu_thread_join(&s->thread);
1717 s->migration_thread_running = false;
1718 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001719 qemu_mutex_lock_iothread();
1720
Fei Li1398b2e2019-01-13 22:08:47 +08001721 multifd_save_cleanup();
Peter Xu62df0662018-05-02 18:47:38 +08001722 qemu_mutex_lock(&s->qemu_file_lock);
1723 tmp = s->to_dst_file;
zhanghailiang89a02a92016-01-15 11:37:42 +08001724 s->to_dst_file = NULL;
Peter Xu62df0662018-05-02 18:47:38 +08001725 qemu_mutex_unlock(&s->qemu_file_lock);
1726 /*
1727 * Close the file handle without the lock to make sure the
1728 * critical section won't block for long.
1729 */
1730 qemu_fclose(tmp);
aliguori065e2812008-11-11 16:46:33 +00001731 }
1732
Wei Yang8f8d5282019-07-17 08:53:41 +08001733 assert(!migration_is_active(s));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001734
Liang Li94f5a432015-11-02 15:37:00 +08001735 if (s->state == MIGRATION_STATUS_CANCELLING) {
zhanghailiang48781e52015-12-16 11:47:33 +00001736 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
Liang Li94f5a432015-11-02 15:37:00 +08001737 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001738 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001739
Juan Quintela87db1a72017-09-05 12:50:22 +02001740 if (s->error) {
1741 /* It is used on info migrate. We can't free it */
1742 error_report_err(error_copy(s->error));
1743 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001744 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +02001745 block_cleanup_parameters(s);
aliguori065e2812008-11-11 16:46:33 +00001746}
1747
Yury Kotovfd392cf2019-04-08 14:33:43 +03001748static void migrate_fd_cleanup_schedule(MigrationState *s)
1749{
1750 /*
1751 * Ref the state for bh, because it may be called when
1752 * there're already no other refs
1753 */
1754 object_ref(OBJECT(s));
1755 qemu_bh_schedule(s->cleanup_bh);
1756}
1757
1758static void migrate_fd_cleanup_bh(void *opaque)
1759{
1760 MigrationState *s = opaque;
1761 migrate_fd_cleanup(s);
1762 object_unref(OBJECT(s));
1763}
1764
Juan Quintela87db1a72017-09-05 12:50:22 +02001765void migrate_set_error(MigrationState *s, const Error *error)
1766{
Daniel Brodsky6e8a3552020-04-03 21:21:08 -07001767 QEMU_LOCK_GUARD(&s->error_mutex);
Juan Quintela87db1a72017-09-05 12:50:22 +02001768 if (!s->error) {
1769 s->error = error_copy(error);
1770 }
Juan Quintela87db1a72017-09-05 12:50:22 +02001771}
1772
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001773void migrate_fd_error(MigrationState *s, const Error *error)
aliguori065e2812008-11-11 16:46:33 +00001774{
Peter Maydell25174052016-10-21 18:41:45 +01001775 trace_migrate_fd_error(error_get_pretty(error));
zhanghailiang89a02a92016-01-15 11:37:42 +08001776 assert(s->to_dst_file == NULL);
zhanghailiang48781e52015-12-16 11:47:33 +00001777 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1778 MIGRATION_STATUS_FAILED);
Juan Quintela87db1a72017-09-05 12:50:22 +02001779 migrate_set_error(s, error);
Juan Quintela458cf282011-02-22 23:32:54 +01001780}
1781
Juan Quintela0edda1c2010-05-11 16:28:39 +02001782static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +00001783{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001784 int old_state ;
zhanghailiang89a02a92016-01-15 11:37:42 +08001785 QEMUFile *f = migrate_get_current()->to_dst_file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001786 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +00001787
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001788 if (s->rp_state.from_dst_file) {
1789 /* shutdown the rp socket, so causing the rp thread to shutdown */
1790 qemu_file_shutdown(s->rp_state.from_dst_file);
1791 }
1792
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001793 do {
1794 old_state = s->state;
Juan Quintela392d87e2020-01-21 15:39:23 +01001795 if (!migration_is_running(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001796 break;
1797 }
Dr. David Alan Gilberta7b36b42017-10-20 10:05:55 +01001798 /* If the migration is paused, kick it out of the pause */
1799 if (old_state == MIGRATION_STATUS_PRE_SWITCHOVER) {
1800 qemu_sem_post(&s->pause_sem);
1801 }
zhanghailiang48781e52015-12-16 11:47:33 +00001802 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
zhanghailiang31194732015-03-13 16:08:38 +08001803 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001804
1805 /*
1806 * If we're unlucky the migration code might be stuck somewhere in a
1807 * send/write while the network has failed and is waiting to timeout;
1808 * if we've got shutdown(2) available then we can force it to quit.
1809 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1810 * called in a bh, so there is no race against this cancel.
1811 */
zhanghailiang31194732015-03-13 16:08:38 +08001812 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001813 qemu_file_shutdown(f);
1814 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08001815 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
1816 Error *local_err = NULL;
1817
1818 bdrv_invalidate_cache_all(&local_err);
1819 if (local_err) {
1820 error_report_err(local_err);
1821 } else {
1822 s->block_inactive = false;
1823 }
1824 }
aliguori065e2812008-11-11 16:46:33 +00001825}
1826
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001827void add_migration_state_change_notifier(Notifier *notify)
1828{
1829 notifier_list_add(&migration_state_notifiers, notify);
1830}
1831
1832void remove_migration_state_change_notifier(Notifier *notify)
1833{
Paolo Bonzini31552522012-01-13 17:34:01 +01001834 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001835}
1836
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +02001837bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001838{
zhanghailiang31194732015-03-13 16:08:38 +08001839 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001840}
1841
Juan Quintela70736932011-02-23 00:43:59 +01001842bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001843{
zhanghailiang31194732015-03-13 16:08:38 +08001844 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001845}
Juan Quintela0edda1c2010-05-11 16:28:39 +02001846
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001847bool migration_has_failed(MigrationState *s)
1848{
zhanghailiang31194732015-03-13 16:08:38 +08001849 return (s->state == MIGRATION_STATUS_CANCELLED ||
1850 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001851}
1852
Juan Quintela57273092017-03-20 22:25:28 +01001853bool migration_in_postcopy(void)
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001854{
Juan Quintela57273092017-03-20 22:25:28 +01001855 MigrationState *s = migrate_get_current();
1856
Dr. David Alan Gilbert3748fef2019-09-23 18:49:42 +01001857 switch (s->state) {
1858 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1859 case MIGRATION_STATUS_POSTCOPY_PAUSED:
1860 case MIGRATION_STATUS_POSTCOPY_RECOVER:
1861 return true;
1862 default:
1863 return false;
1864 }
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001865}
1866
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001867bool migration_in_postcopy_after_devices(MigrationState *s)
1868{
Juan Quintela57273092017-03-20 22:25:28 +01001869 return migration_in_postcopy() && s->postcopy_after_devices;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001870}
1871
David Hildenbrand06df2e62020-06-26 09:22:33 +02001872bool migration_in_incoming_postcopy(void)
1873{
1874 PostcopyState ps = postcopy_state_get();
1875
1876 return ps >= POSTCOPY_INCOMING_DISCARD && ps < POSTCOPY_INCOMING_END;
1877}
1878
Juan Quintelafab35002017-03-22 17:36:57 +01001879bool migration_is_idle(void)
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301880{
Markus Armbrusterdaff7f02019-04-01 11:08:25 +02001881 MigrationState *s = current_migration;
1882
1883 if (!s) {
1884 return true;
1885 }
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301886
1887 switch (s->state) {
1888 case MIGRATION_STATUS_NONE:
1889 case MIGRATION_STATUS_CANCELLED:
1890 case MIGRATION_STATUS_COMPLETED:
1891 case MIGRATION_STATUS_FAILED:
1892 return true;
1893 case MIGRATION_STATUS_SETUP:
1894 case MIGRATION_STATUS_CANCELLING:
1895 case MIGRATION_STATUS_ACTIVE:
1896 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1897 case MIGRATION_STATUS_COLO:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +01001898 case MIGRATION_STATUS_PRE_SWITCHOVER:
1899 case MIGRATION_STATUS_DEVICE:
Jens Freimannc7e0acd2019-10-29 12:49:02 +01001900 case MIGRATION_STATUS_WAIT_UNPLUG:
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301901 return false;
1902 case MIGRATION_STATUS__MAX:
1903 g_assert_not_reached();
1904 }
1905
1906 return false;
1907}
1908
Wei Yang8f8d5282019-07-17 08:53:41 +08001909bool migration_is_active(MigrationState *s)
1910{
1911 return (s->state == MIGRATION_STATUS_ACTIVE ||
1912 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1913}
1914
Peter Xu3e0c8052018-02-08 18:31:15 +08001915void migrate_init(MigrationState *s)
Juan Quintela0edda1c2010-05-11 16:28:39 +02001916{
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001917 /*
1918 * Reinitialise all migration state, except
1919 * parameters/capabilities that the user set, and
1920 * locks.
1921 */
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001922 s->cleanup_bh = 0;
zhanghailiang89a02a92016-01-15 11:37:42 +08001923 s->to_dst_file = NULL;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001924 s->state = MIGRATION_STATUS_NONE;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001925 s->rp_state.from_dst_file = NULL;
1926 s->rp_state.error = false;
1927 s->mbps = 0.0;
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08001928 s->pages_per_second = 0.0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001929 s->downtime = 0;
1930 s->expected_downtime = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001931 s->setup_time = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001932 s->start_postcopy = false;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001933 s->postcopy_after_devices = false;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001934 s->migration_thread_running = false;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001935 error_free(s->error);
1936 s->error = NULL;
Chuan Zhengd8053e72020-09-15 11:03:57 +08001937 s->hostname = NULL;
Juan Quintela1299c632011-11-09 21:29:01 +01001938
zhanghailiang48781e52015-12-16 11:47:33 +00001939 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +02001940
Peter Xu4af246a2018-01-03 20:20:08 +08001941 s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1942 s->total_time = 0;
Peter Xu7287cbd2018-01-03 20:20:09 +08001943 s->vm_was_running = false;
Peter Xub15df1a2018-01-03 20:20:13 +08001944 s->iteration_initial_bytes = 0;
1945 s->threshold_size = 0;
Juan Quintela0edda1c2010-05-11 16:28:39 +02001946}
Juan Quintelacab30142011-02-22 23:54:21 +01001947
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001948static GSList *migration_blockers;
1949
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301950int migrate_add_blocker(Error *reason, Error **errp)
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001951{
Markus Armbruster811f8652019-04-01 11:08:24 +02001952 if (only_migratable) {
Markus Armbruster4b576642018-10-17 10:26:25 +02001953 error_propagate_prepend(errp, error_copy(reason),
1954 "disallowing migration blocker "
Greg Kurzb6eca812019-05-16 12:58:05 +02001955 "(--only-migratable) for: ");
Ashijeet Acharyab67b8c32017-01-16 17:01:54 +05301956 return -EACCES;
1957 }
1958
Juan Quintelafab35002017-03-22 17:36:57 +01001959 if (migration_is_idle()) {
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301960 migration_blockers = g_slist_prepend(migration_blockers, reason);
1961 return 0;
1962 }
1963
Markus Armbruster4b576642018-10-17 10:26:25 +02001964 error_propagate_prepend(errp, error_copy(reason),
1965 "disallowing migration blocker "
1966 "(migration in progress) for: ");
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301967 return -EBUSY;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001968}
1969
1970void migrate_del_blocker(Error *reason)
1971{
1972 migration_blockers = g_slist_remove(migration_blockers, reason);
1973}
1974
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001975void qmp_migrate_incoming(const char *uri, Error **errp)
1976{
1977 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001978 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001979
1980 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001981 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001982 return;
1983 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001984 if (!once) {
1985 error_setg(errp, "The incoming migration has already been started");
Yury Kotov603d5a42019-11-13 20:53:25 +03001986 return;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001987 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001988
1989 qemu_start_incoming_migration(uri, &local_err);
1990
1991 if (local_err) {
1992 error_propagate(errp, local_err);
1993 return;
1994 }
1995
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001996 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001997}
1998
Peter Xu02affd42018-05-02 18:47:36 +08001999void qmp_migrate_recover(const char *uri, Error **errp)
2000{
2001 MigrationIncomingState *mis = migration_incoming_get_current();
2002
2003 if (mis->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
2004 error_setg(errp, "Migrate recover can only be run "
2005 "when postcopy is paused.");
2006 return;
2007 }
2008
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01002009 if (qatomic_cmpxchg(&mis->postcopy_recover_triggered,
Peter Xu02affd42018-05-02 18:47:36 +08002010 false, true) == true) {
2011 error_setg(errp, "Migrate recovery is triggered already");
2012 return;
2013 }
2014
2015 /*
2016 * Note that this call will never start a real migration; it will
2017 * only re-setup the migration stream and poke existing migration
2018 * to continue using that newly established channel.
2019 */
2020 qemu_start_incoming_migration(uri, errp);
2021}
2022
Peter Xubfbf89c2018-05-02 18:47:39 +08002023void qmp_migrate_pause(Error **errp)
2024{
2025 MigrationState *ms = migrate_get_current();
2026 MigrationIncomingState *mis = migration_incoming_get_current();
2027 int ret;
2028
2029 if (ms->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2030 /* Source side, during postcopy */
2031 qemu_mutex_lock(&ms->qemu_file_lock);
2032 ret = qemu_file_shutdown(ms->to_dst_file);
2033 qemu_mutex_unlock(&ms->qemu_file_lock);
2034 if (ret) {
2035 error_setg(errp, "Failed to pause source migration");
2036 }
2037 return;
2038 }
2039
2040 if (mis->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2041 ret = qemu_file_shutdown(mis->from_src_file);
2042 if (ret) {
2043 error_setg(errp, "Failed to pause destination migration");
2044 }
2045 return;
2046 }
2047
2048 error_setg(errp, "migrate-pause is currently only supported "
2049 "during postcopy-active state");
2050}
2051
Greg Kurz24f39022016-05-04 21:44:19 +02002052bool migration_is_blocked(Error **errp)
2053{
2054 if (qemu_savevm_state_blocked(errp)) {
2055 return true;
2056 }
2057
2058 if (migration_blockers) {
Eduardo Habkost250561e2017-06-08 10:39:05 -03002059 error_propagate(errp, error_copy(migration_blockers->data));
Greg Kurz24f39022016-05-04 21:44:19 +02002060 return true;
2061 }
2062
2063 return false;
2064}
2065
Peter Xud3e35b82018-05-02 18:47:24 +08002066/* Returns true if continue to migrate, or false if error detected */
2067static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc,
2068 bool resume, Error **errp)
2069{
2070 Error *local_err = NULL;
2071
2072 if (resume) {
2073 if (s->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
2074 error_setg(errp, "Cannot resume if there is no "
2075 "paused migration");
2076 return false;
2077 }
Peter Xu97ca2112018-07-23 20:33:03 +08002078
2079 /*
2080 * Postcopy recovery won't work well with release-ram
2081 * capability since release-ram will drop the page buffer as
2082 * long as the page is put into the send buffer. So if there
2083 * is a network failure happened, any page buffers that have
2084 * not yet reached the destination VM but have already been
2085 * sent from the source VM will be lost forever. Let's refuse
2086 * the client from resuming such a postcopy migration.
2087 * Luckily release-ram was designed to only be used when src
2088 * and destination VMs are on the same host, so it should be
2089 * fine.
2090 */
2091 if (migrate_release_ram()) {
2092 error_setg(errp, "Postcopy recovery cannot work "
2093 "when release-ram capability is set");
2094 return false;
2095 }
2096
Peter Xud3e35b82018-05-02 18:47:24 +08002097 /* This is a resume, skip init status */
2098 return true;
2099 }
2100
Juan Quintela392d87e2020-01-21 15:39:23 +01002101 if (migration_is_running(s->state)) {
Peter Xud3e35b82018-05-02 18:47:24 +08002102 error_setg(errp, QERR_MIGRATION_ACTIVE);
2103 return false;
2104 }
2105
2106 if (runstate_check(RUN_STATE_INMIGRATE)) {
2107 error_setg(errp, "Guest is waiting for an incoming migration");
2108 return false;
2109 }
2110
2111 if (migration_is_blocked(errp)) {
2112 return false;
2113 }
2114
2115 if (blk || blk_inc) {
2116 if (migrate_use_block() || migrate_use_block_incremental()) {
2117 error_setg(errp, "Command options are incompatible with "
2118 "current migration capabilities");
2119 return false;
2120 }
2121 migrate_set_block_enabled(true, &local_err);
2122 if (local_err) {
2123 error_propagate(errp, local_err);
2124 return false;
2125 }
2126 s->must_remove_block_options = true;
2127 }
2128
2129 if (blk_inc) {
2130 migrate_set_block_incremental(s, true);
2131 }
2132
2133 migrate_init(s);
Ivan Ren87f3bd82019-08-02 18:18:41 +08002134 /*
2135 * set ram_counters memory to zero for a
2136 * new migration
2137 */
2138 memset(&ram_counters, 0, sizeof(ram_counters));
Peter Xud3e35b82018-05-02 18:47:24 +08002139
2140 return true;
2141}
2142
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002143void qmp_migrate(const char *uri, bool has_blk, bool blk,
2144 bool has_inc, bool inc, bool has_detach, bool detach,
Peter Xu7a4da282018-05-02 18:47:23 +08002145 bool has_resume, bool resume, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002146{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +02002147 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +02002148 MigrationState *s = migrate_get_current();
Longpeng(Mike)d658f652020-08-06 15:40:29 +08002149 const char *p = NULL;
Juan Quintelacab30142011-02-22 23:54:21 +01002150
Peter Xud3e35b82018-05-02 18:47:24 +08002151 if (!migrate_prepare(s, has_blk && blk, has_inc && inc,
2152 has_resume && resume, errp)) {
2153 /* Error detected, put into errp */
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002154 return;
Juan Quintelacab30142011-02-22 23:54:21 +01002155 }
Juan Quintelacab30142011-02-22 23:54:21 +01002156
Longpeng(Mike)d658f652020-08-06 15:40:29 +08002157 if (strstart(uri, "tcp:", &p) ||
Longpeng(Mike)9ba3b2b2020-08-06 15:40:30 +08002158 strstart(uri, "unix:", NULL) ||
2159 strstart(uri, "vsock:", NULL)) {
Longpeng(Mike)d658f652020-08-06 15:40:29 +08002160 socket_start_outgoing_migration(s, p ? p : uri, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04002161#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08002162 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04002163 rdma_start_outgoing_migration(s, p, &local_err);
2164#endif
Juan Quintelacab30142011-02-22 23:54:21 +01002165 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002166 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01002167 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002168 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01002169 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002170 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
2171 "a valid migration protocol");
zhanghailiang48781e52015-12-16 11:47:33 +00002172 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2173 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09576e72018-03-16 20:21:14 +00002174 block_cleanup_parameters(s);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002175 return;
Juan Quintelacab30142011-02-22 23:54:21 +01002176 }
2177
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002178 if (local_err) {
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01002179 migrate_fd_error(s, local_err);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002180 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002181 return;
Juan Quintela1299c632011-11-09 21:29:01 +01002182 }
Juan Quintelacab30142011-02-22 23:54:21 +01002183}
2184
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02002185void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002186{
Juan Quintela17549e82011-10-05 13:50:43 +02002187 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01002188}
2189
Dr. David Alan Gilbert89cfc022017-10-20 10:05:53 +01002190void qmp_migrate_continue(MigrationStatus state, Error **errp)
2191{
2192 MigrationState *s = migrate_get_current();
2193 if (s->state != state) {
2194 error_setg(errp, "Migration not in expected state: %s",
2195 MigrationStatus_str(s->state));
2196 return;
2197 }
2198 qemu_sem_post(&s->pause_sem);
2199}
2200
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002201void qmp_migrate_set_cache_size(int64_t value, Error **errp)
2202{
Juan Quintela73af8dd2017-10-05 21:30:10 +02002203 MigrateSetParameters p = {
2204 .has_xbzrle_cache_size = true,
2205 .xbzrle_cache_size = value,
2206 };
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002207
Juan Quintela73af8dd2017-10-05 21:30:10 +02002208 qmp_migrate_set_parameters(&p, errp);
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002209}
2210
2211int64_t qmp_query_migrate_cache_size(Error **errp)
2212{
2213 return migrate_xbzrle_cache_size();
2214}
2215
Luiz Capitulino3dc85382011-11-28 11:59:37 -02002216void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002217{
Markus Armbruster1bda8b32017-07-18 13:42:11 +02002218 MigrateSetParameters p = {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302219 .has_max_bandwidth = true,
2220 .max_bandwidth = value,
2221 };
Juan Quintelacab30142011-02-22 23:54:21 +01002222
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302223 qmp_migrate_set_parameters(&p, errp);
Juan Quintelacab30142011-02-22 23:54:21 +01002224}
2225
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02002226void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002227{
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03002228 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
Mao Zhongyi2ee30cf2020-03-31 16:22:05 +08002229 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
2230 "downtime_limit",
2231 "an integer in the range of 0 to "
2232 stringify(MAX_MIGRATE_DOWNTIME_SECONDS)" seconds");
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03002233 return;
2234 }
2235
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302236 value *= 1000; /* Convert to milliseconds */
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302237
Markus Armbruster1bda8b32017-07-18 13:42:11 +02002238 MigrateSetParameters p = {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302239 .has_downtime_limit = true,
Fangrui Song2667c982019-11-22 09:00:38 +01002240 .downtime_limit = (int64_t)value,
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302241 };
2242
2243 qmp_migrate_set_parameters(&p, errp);
aliguori5bb79102008-10-13 03:12:02 +00002244}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03002245
Pavel Butsykin53f09a12017-02-03 18:23:20 +03002246bool migrate_release_ram(void)
2247{
2248 MigrationState *s;
2249
2250 s = migrate_get_current();
2251
2252 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
2253}
2254
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00002255bool migrate_postcopy_ram(void)
2256{
2257 MigrationState *s;
2258
2259 s = migrate_get_current();
2260
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +00002261 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00002262}
2263
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002264bool migrate_postcopy(void)
2265{
Vladimir Sementsov-Ogievskiydd6bb912018-03-13 15:34:00 -04002266 return migrate_postcopy_ram() || migrate_dirty_bitmaps();
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002267}
2268
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06002269bool migrate_auto_converge(void)
2270{
2271 MigrationState *s;
2272
2273 s = migrate_get_current();
2274
2275 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
2276}
2277
Peter Lieven323004a2013-07-18 09:48:50 +02002278bool migrate_zero_blocks(void)
2279{
2280 MigrationState *s;
2281
2282 s = migrate_get_current();
2283
2284 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
2285}
2286
Alexey Perevalovf22f9282018-03-22 21:17:22 +03002287bool migrate_postcopy_blocktime(void)
2288{
2289 MigrationState *s;
2290
2291 s = migrate_get_current();
2292
2293 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME];
2294}
2295
Liang Li8706d2d2015-03-23 16:32:17 +08002296bool migrate_use_compression(void)
2297{
Liang Lidde4e692015-03-23 16:32:26 +08002298 MigrationState *s;
2299
2300 s = migrate_get_current();
2301
2302 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08002303}
2304
2305int migrate_compress_level(void)
2306{
2307 MigrationState *s;
2308
2309 s = migrate_get_current();
2310
Daniel P. Berrange2594f562016-04-27 11:05:14 +01002311 return s->parameters.compress_level;
Liang Li8706d2d2015-03-23 16:32:17 +08002312}
2313
2314int migrate_compress_threads(void)
2315{
2316 MigrationState *s;
2317
2318 s = migrate_get_current();
2319
Daniel P. Berrange2594f562016-04-27 11:05:14 +01002320 return s->parameters.compress_threads;
Liang Li8706d2d2015-03-23 16:32:17 +08002321}
2322
Xiao Guangrong1d588722018-08-21 16:10:20 +08002323int migrate_compress_wait_thread(void)
2324{
2325 MigrationState *s;
2326
2327 s = migrate_get_current();
2328
2329 return s->parameters.compress_wait_thread;
2330}
2331
Liang Li3fcb38c2015-03-23 16:32:18 +08002332int migrate_decompress_threads(void)
2333{
2334 MigrationState *s;
2335
2336 s = migrate_get_current();
2337
Daniel P. Berrange2594f562016-04-27 11:05:14 +01002338 return s->parameters.decompress_threads;
Liang Li3fcb38c2015-03-23 16:32:18 +08002339}
2340
Vladimir Sementsov-Ogievskiy55efc8c2018-03-13 15:34:00 -04002341bool migrate_dirty_bitmaps(void)
2342{
2343 MigrationState *s;
2344
2345 s = migrate_get_current();
2346
2347 return s->enabled_capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS];
2348}
2349
Yury Kotov18269062019-02-15 20:45:45 +03002350bool migrate_ignore_shared(void)
2351{
2352 MigrationState *s;
2353
2354 s = migrate_get_current();
2355
2356 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_IGNORE_SHARED];
2357}
2358
Yury Kotovb9d68df2019-09-03 19:22:44 +03002359bool migrate_validate_uuid(void)
2360{
2361 MigrationState *s;
2362
2363 s = migrate_get_current();
2364
2365 return s->enabled_capabilities[MIGRATION_CAPABILITY_VALIDATE_UUID];
2366}
2367
Juan Quintelab05dc722015-07-07 14:44:05 +02002368bool migrate_use_events(void)
2369{
2370 MigrationState *s;
2371
2372 s = migrate_get_current();
2373
2374 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
2375}
2376
Juan Quintela30126bb2016-01-14 12:23:00 +01002377bool migrate_use_multifd(void)
2378{
2379 MigrationState *s;
2380
2381 s = migrate_get_current();
2382
Juan Quintelacbfd6c92019-02-06 13:54:06 +01002383 return s->enabled_capabilities[MIGRATION_CAPABILITY_MULTIFD];
Juan Quintela30126bb2016-01-14 12:23:00 +01002384}
2385
Dr. David Alan Gilbert93fbd032017-10-20 10:05:50 +01002386bool migrate_pause_before_switchover(void)
2387{
2388 MigrationState *s;
2389
2390 s = migrate_get_current();
2391
2392 return s->enabled_capabilities[
2393 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER];
2394}
2395
Juan Quintela4075fb12016-01-15 08:56:17 +01002396int migrate_multifd_channels(void)
2397{
2398 MigrationState *s;
2399
2400 s = migrate_get_current();
2401
Juan Quintelacbfd6c92019-02-06 13:54:06 +01002402 return s->parameters.multifd_channels;
Juan Quintela4075fb12016-01-15 08:56:17 +01002403}
2404
Juan Quintelaab7cbb02019-05-15 13:37:46 +02002405MultiFDCompression migrate_multifd_compression(void)
2406{
2407 MigrationState *s;
2408
2409 s = migrate_get_current();
2410
2411 return s->parameters.multifd_compression;
2412}
2413
Juan Quintela9004db42020-01-23 17:08:52 +01002414int migrate_multifd_zlib_level(void)
2415{
2416 MigrationState *s;
2417
2418 s = migrate_get_current();
2419
2420 return s->parameters.multifd_zlib_level;
2421}
2422
Juan Quintela6a9ad152020-01-23 17:41:36 +01002423int migrate_multifd_zstd_level(void)
2424{
2425 MigrationState *s;
2426
2427 s = migrate_get_current();
2428
2429 return s->parameters.multifd_zstd_level;
2430}
2431
Orit Wasserman17ad9b32012-08-06 21:42:53 +03002432int migrate_use_xbzrle(void)
2433{
2434 MigrationState *s;
2435
2436 s = migrate_get_current();
2437
2438 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
2439}
2440
2441int64_t migrate_xbzrle_cache_size(void)
2442{
2443 MigrationState *s;
2444
2445 s = migrate_get_current();
2446
Juan Quintela73af8dd2017-10-05 21:30:10 +02002447 return s->parameters.xbzrle_cache_size;
Orit Wasserman17ad9b32012-08-06 21:42:53 +03002448}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002449
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01002450static int64_t migrate_max_postcopy_bandwidth(void)
2451{
2452 MigrationState *s;
2453
2454 s = migrate_get_current();
2455
2456 return s->parameters.max_postcopy_bandwidth;
2457}
2458
Juan Quintela2833c592017-04-05 18:32:37 +02002459bool migrate_use_block(void)
2460{
2461 MigrationState *s;
2462
2463 s = migrate_get_current();
2464
2465 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
2466}
2467
Peter Xuc788ada2017-06-26 18:28:55 +08002468bool migrate_use_return_path(void)
2469{
2470 MigrationState *s;
2471
2472 s = migrate_get_current();
2473
2474 return s->enabled_capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
2475}
2476
Juan Quintela2833c592017-04-05 18:32:37 +02002477bool migrate_use_block_incremental(void)
2478{
2479 MigrationState *s;
2480
2481 s = migrate_get_current();
2482
2483 return s->parameters.block_incremental;
2484}
2485
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002486/* migration thread support */
2487/*
2488 * Something bad happened to the RP stream, mark an error
2489 * The caller shall print or trace something to indicate why
2490 */
2491static void mark_source_rp_bad(MigrationState *s)
2492{
2493 s->rp_state.error = true;
2494}
2495
2496static struct rp_cmd_args {
2497 ssize_t len; /* -1 = variable */
2498 const char *name;
2499} rp_cmd_args[] = {
2500 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
2501 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
2502 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002503 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
2504 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Peter Xua335deb2018-05-02 18:47:28 +08002505 [MIG_RP_MSG_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" },
Peter Xu13955b82018-05-02 18:47:30 +08002506 [MIG_RP_MSG_RESUME_ACK] = { .len = 4, .name = "RESUME_ACK" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002507 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
2508};
2509
2510/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002511 * Process a request for pages received on the return path,
2512 * We're allowed to send more than requested (e.g. to round to our page size)
2513 * and we don't need to send pages that have already been sent.
2514 */
2515static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
2516 ram_addr_t start, size_t len)
2517{
Wei Yang038adc22019-10-13 10:11:45 +08002518 long our_host_ps = qemu_real_host_page_size;
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00002519
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002520 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00002521
2522 /*
2523 * Since we currently insist on matching page sizes, just sanity check
2524 * we're being asked for whole host pages.
2525 */
Bihong Yu395cb452020-10-20 11:10:43 +08002526 if (start & (our_host_ps - 1) ||
2527 (len & (our_host_ps - 1))) {
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00002528 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
2529 " len: %zd", __func__, start, len);
2530 mark_source_rp_bad(ms);
2531 return;
2532 }
2533
Juan Quintela96506892017-03-14 18:41:03 +01002534 if (ram_save_queue_pages(rbname, start, len)) {
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00002535 mark_source_rp_bad(ms);
2536 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002537}
2538
Peter Xu14b17422018-05-02 18:47:21 +08002539/* Return true to retry, false to quit */
2540static bool postcopy_pause_return_path_thread(MigrationState *s)
2541{
2542 trace_postcopy_pause_return_path();
2543
2544 qemu_sem_wait(&s->postcopy_pause_rp_sem);
2545
2546 trace_postcopy_pause_return_path_continued();
2547
2548 return true;
2549}
2550
Peter Xua335deb2018-05-02 18:47:28 +08002551static int migrate_handle_rp_recv_bitmap(MigrationState *s, char *block_name)
2552{
2553 RAMBlock *block = qemu_ram_block_by_name(block_name);
2554
2555 if (!block) {
2556 error_report("%s: invalid block name '%s'", __func__, block_name);
2557 return -EINVAL;
2558 }
2559
2560 /* Fetch the received bitmap and refresh the dirty bitmap */
2561 return ram_dirty_bitmap_reload(s, block);
2562}
2563
Peter Xu13955b82018-05-02 18:47:30 +08002564static int migrate_handle_rp_resume_ack(MigrationState *s, uint32_t value)
2565{
2566 trace_source_return_path_thread_resume_ack(value);
2567
2568 if (value != MIGRATION_RESUME_ACK_VALUE) {
2569 error_report("%s: illegal resume_ack value %"PRIu32,
2570 __func__, value);
2571 return -1;
2572 }
2573
2574 /* Now both sides are active. */
2575 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
2576 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2577
Peter Xu94190692018-05-02 18:47:34 +08002578 /* Notify send thread that time to continue send pages */
2579 qemu_sem_post(&s->rp_state.rp_sem);
Peter Xu13955b82018-05-02 18:47:30 +08002580
2581 return 0;
2582}
2583
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002584/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002585 * Handles messages sent on the return path towards the source VM
2586 *
2587 */
2588static void *source_return_path_thread(void *opaque)
2589{
2590 MigrationState *ms = opaque;
2591 QEMUFile *rp = ms->rp_state.from_dst_file;
2592 uint16_t header_len, header_type;
Peter Xu568b01c2016-03-09 14:12:12 +08002593 uint8_t buf[512];
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002594 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002595 ram_addr_t start = 0; /* =0 to silence warning */
2596 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002597 int res;
2598
2599 trace_source_return_path_thread_entry();
Lidong Chen74637e62018-08-06 21:29:29 +08002600 rcu_register_thread();
Peter Xu14b17422018-05-02 18:47:21 +08002601
2602retry:
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002603 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
2604 migration_is_setup_or_active(ms->state)) {
2605 trace_source_return_path_thread_loop_top();
2606 header_type = qemu_get_be16(rp);
2607 header_len = qemu_get_be16(rp);
2608
Peter Xu7a9ddfb2018-02-08 18:31:05 +08002609 if (qemu_file_get_error(rp)) {
2610 mark_source_rp_bad(ms);
2611 goto out;
2612 }
2613
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002614 if (header_type >= MIG_RP_MSG_MAX ||
2615 header_type == MIG_RP_MSG_INVALID) {
2616 error_report("RP: Received invalid message 0x%04x length 0x%04x",
Mao Zhongyied8b2822020-03-28 01:19:08 +08002617 header_type, header_len);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002618 mark_source_rp_bad(ms);
2619 goto out;
2620 }
2621
2622 if ((rp_cmd_args[header_type].len != -1 &&
2623 header_len != rp_cmd_args[header_type].len) ||
Peter Xu568b01c2016-03-09 14:12:12 +08002624 header_len > sizeof(buf)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002625 error_report("RP: Received '%s' message (0x%04x) with"
Mao Zhongyied8b2822020-03-28 01:19:08 +08002626 "incorrect length %d expecting %zu",
2627 rp_cmd_args[header_type].name, header_type, header_len,
2628 (size_t)rp_cmd_args[header_type].len);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002629 mark_source_rp_bad(ms);
2630 goto out;
2631 }
2632
2633 /* We know we've got a valid header by this point */
2634 res = qemu_get_buffer(rp, buf, header_len);
2635 if (res != header_len) {
2636 error_report("RP: Failed reading data for message 0x%04x"
2637 " read %d expected %d",
2638 header_type, res, header_len);
2639 mark_source_rp_bad(ms);
2640 goto out;
2641 }
2642
2643 /* OK, we have the message and the data */
2644 switch (header_type) {
2645 case MIG_RP_MSG_SHUT:
Peter Maydell4d885132016-06-10 17:09:22 +01002646 sibling_error = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002647 trace_source_return_path_thread_shut(sibling_error);
2648 if (sibling_error) {
2649 error_report("RP: Sibling indicated error %d", sibling_error);
2650 mark_source_rp_bad(ms);
2651 }
2652 /*
2653 * We'll let the main thread deal with closing the RP
2654 * we could do a shutdown(2) on it, but we're the only user
2655 * anyway, so there's nothing gained.
2656 */
2657 goto out;
2658
2659 case MIG_RP_MSG_PONG:
Peter Maydell4d885132016-06-10 17:09:22 +01002660 tmp32 = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002661 trace_source_return_path_thread_pong(tmp32);
2662 break;
2663
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002664 case MIG_RP_MSG_REQ_PAGES:
Peter Maydell4d885132016-06-10 17:09:22 +01002665 start = ldq_be_p(buf);
2666 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002667 migrate_handle_rp_req_pages(ms, NULL, start, len);
2668 break;
2669
2670 case MIG_RP_MSG_REQ_PAGES_ID:
2671 expected_len = 12 + 1; /* header + termination */
2672
2673 if (header_len >= expected_len) {
Peter Maydell4d885132016-06-10 17:09:22 +01002674 start = ldq_be_p(buf);
2675 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002676 /* Now we expect an idstr */
2677 tmp32 = buf[12]; /* Length of the following idstr */
2678 buf[13 + tmp32] = '\0';
2679 expected_len += tmp32;
2680 }
2681 if (header_len != expected_len) {
2682 error_report("RP: Req_Page_id with length %d expecting %zd",
Mao Zhongyied8b2822020-03-28 01:19:08 +08002683 header_len, expected_len);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002684 mark_source_rp_bad(ms);
2685 goto out;
2686 }
2687 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
2688 break;
2689
Peter Xua335deb2018-05-02 18:47:28 +08002690 case MIG_RP_MSG_RECV_BITMAP:
2691 if (header_len < 1) {
2692 error_report("%s: missing block name", __func__);
2693 mark_source_rp_bad(ms);
2694 goto out;
2695 }
2696 /* Format: len (1B) + idstr (<255B). This ends the idstr. */
2697 buf[buf[0] + 1] = '\0';
2698 if (migrate_handle_rp_recv_bitmap(ms, (char *)(buf + 1))) {
2699 mark_source_rp_bad(ms);
2700 goto out;
2701 }
2702 break;
2703
Peter Xu13955b82018-05-02 18:47:30 +08002704 case MIG_RP_MSG_RESUME_ACK:
2705 tmp32 = ldl_be_p(buf);
2706 if (migrate_handle_rp_resume_ack(ms, tmp32)) {
2707 mark_source_rp_bad(ms);
2708 goto out;
2709 }
2710 break;
2711
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002712 default:
2713 break;
2714 }
2715 }
Peter Xu14b17422018-05-02 18:47:21 +08002716
2717out:
2718 res = qemu_file_get_error(rp);
2719 if (res) {
Dr. David Alan Gilbertfb14a422019-10-07 11:35:07 +01002720 if (res == -EIO && migration_in_postcopy()) {
Peter Xu14b17422018-05-02 18:47:21 +08002721 /*
2722 * Maybe there is something we can do: it looks like a
2723 * network down issue, and we pause for a recovery.
2724 */
2725 if (postcopy_pause_return_path_thread(ms)) {
2726 /* Reload rp, reset the rest */
Marc-André Lureau0284a2a2018-09-25 13:22:45 +04002727 if (rp != ms->rp_state.from_dst_file) {
2728 qemu_fclose(rp);
2729 rp = ms->rp_state.from_dst_file;
2730 }
Peter Xu14b17422018-05-02 18:47:21 +08002731 ms->rp_state.error = false;
2732 goto retry;
2733 }
2734 }
2735
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002736 trace_source_return_path_thread_bad_end();
2737 mark_source_rp_bad(ms);
2738 }
2739
2740 trace_source_return_path_thread_end();
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002741 ms->rp_state.from_dst_file = NULL;
2742 qemu_fclose(rp);
Lidong Chen74637e62018-08-06 21:29:29 +08002743 rcu_unregister_thread();
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002744 return NULL;
2745}
2746
Peter Xud3e35b82018-05-02 18:47:24 +08002747static int open_return_path_on_source(MigrationState *ms,
2748 bool create_thread)
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002749{
2750
zhanghailiang89a02a92016-01-15 11:37:42 +08002751 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002752 if (!ms->rp_state.from_dst_file) {
2753 return -1;
2754 }
2755
2756 trace_open_return_path_on_source();
Peter Xud3e35b82018-05-02 18:47:24 +08002757
2758 if (!create_thread) {
2759 /* We're done */
2760 return 0;
2761 }
2762
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002763 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
2764 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
2765
2766 trace_open_return_path_on_source_continue();
2767
2768 return 0;
2769}
2770
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002771/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
2772static int await_return_path_close_on_source(MigrationState *ms)
2773{
2774 /*
2775 * If this is a normal exit then the destination will send a SHUT and the
2776 * rp_thread will exit, however if there's an error we need to cause
2777 * it to exit.
2778 */
zhanghailiang89a02a92016-01-15 11:37:42 +08002779 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002780 /*
2781 * shutdown(2), if we have it, will cause it to unblock if it's stuck
2782 * waiting for the destination.
2783 */
2784 qemu_file_shutdown(ms->rp_state.from_dst_file);
2785 mark_source_rp_bad(ms);
2786 }
2787 trace_await_return_path_close_on_source_joining();
2788 qemu_thread_join(&ms->rp_state.rp_thread);
2789 trace_await_return_path_close_on_source_close();
2790 return ms->rp_state.error;
2791}
2792
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002793/*
2794 * Switch from normal iteration to postcopy
2795 * Returns non-0 on error
2796 */
Peter Xu7287cbd2018-01-03 20:20:09 +08002797static int postcopy_start(MigrationState *ms)
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002798{
2799 int ret;
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002800 QIOChannelBuffer *bioc;
2801 QEMUFile *fb;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002802 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01002803 int64_t bandwidth = migrate_max_postcopy_bandwidth();
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002804 bool restart_block = false;
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002805 int cur_state = MIGRATION_STATUS_ACTIVE;
2806 if (!migrate_pause_before_switchover()) {
2807 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
2808 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2809 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002810
2811 trace_postcopy_start();
2812 qemu_mutex_lock_iothread();
2813 trace_postcopy_start_set_run();
2814
Daniel Henrique Barbozafb064112018-12-05 17:47:01 -02002815 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002816 global_state_store();
2817 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01002818 if (ret < 0) {
2819 goto fail;
2820 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002821
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002822 ret = migration_maybe_pause(ms, &cur_state,
2823 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2824 if (ret < 0) {
2825 goto fail;
2826 }
2827
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01002828 ret = bdrv_inactivate_all();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002829 if (ret < 0) {
2830 goto fail;
2831 }
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002832 restart_block = true;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002833
2834 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00002835 * Cause any non-postcopiable, but iterative devices to
2836 * send out their final data.
2837 */
Fam Zhenga1fbe752017-06-17 00:06:58 +08002838 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00002839
2840 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002841 * in Finish migrate and with the io-lock held everything should
2842 * be quiet, but we've potentially still got dirty pages and we
2843 * need to tell the destination to throw any pages it's already received
2844 * that are dirty
2845 */
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002846 if (migrate_postcopy_ram()) {
2847 if (ram_postcopy_send_discard_bitmap(ms)) {
2848 error_report("postcopy send discard bitmap failed");
2849 goto fail;
2850 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002851 }
2852
2853 /*
2854 * send rest of state - note things that are doing postcopy
2855 * will notice we're in POSTCOPY_ACTIVE and not actually
2856 * wrap their state up here
2857 */
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01002858 /* 0 max-postcopy-bandwidth means unlimited */
2859 if (!bandwidth) {
2860 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
2861 } else {
2862 qemu_file_set_rate_limit(ms->to_dst_file, bandwidth / XFER_LIMIT_RATIO);
2863 }
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002864 if (migrate_postcopy_ram()) {
2865 /* Ping just for debugging, helps line traces up */
2866 qemu_savevm_send_ping(ms->to_dst_file, 2);
2867 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002868
2869 /*
2870 * While loading the device state we may trigger page transfer
2871 * requests and the fd must be free to process those, and thus
2872 * the destination must read the whole device state off the fd before
2873 * it starts processing it. Unfortunately the ad-hoc migration format
2874 * doesn't allow the destination to know the size to read without fully
2875 * parsing it through each devices load-state code (especially the open
2876 * coded devices that use get/put).
2877 * So we wrap the device state up in a package with a length at the start;
2878 * to do this we use a qemu_buf to hold the whole of the device state.
2879 */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002880 bioc = qio_channel_buffer_new(4096);
Daniel P. Berrange6f01f132016-09-30 11:57:14 +01002881 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002882 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
2883 object_unref(OBJECT(bioc));
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002884
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00002885 /*
2886 * Make sure the receiver can get incoming pages before we send the rest
2887 * of the state
2888 */
2889 qemu_savevm_send_postcopy_listen(fb);
2890
Fam Zhenga1fbe752017-06-17 00:06:58 +08002891 qemu_savevm_state_complete_precopy(fb, false, false);
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002892 if (migrate_postcopy_ram()) {
2893 qemu_savevm_send_ping(fb, 3);
2894 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002895
2896 qemu_savevm_send_postcopy_run(fb);
2897
2898 /* <><> end of stuff going into the package */
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002899
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002900 /* Last point of recovery; as soon as we send the package the destination
2901 * can open devices and potentially start running.
2902 * Lets just check again we've not got any errors.
2903 */
2904 ret = qemu_file_get_error(ms->to_dst_file);
2905 if (ret) {
2906 error_report("postcopy_start: Migration stream errored (pre package)");
2907 goto fail_closefb;
2908 }
2909
2910 restart_block = false;
2911
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002912 /* Now send that blob */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002913 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002914 goto fail_closefb;
2915 }
2916 qemu_fclose(fb);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00002917
2918 /* Send a notify to give a chance for anything that needs to happen
2919 * at the transition to postcopy and after the device state; in particular
2920 * spice needs to trigger a transition now
2921 */
2922 ms->postcopy_after_devices = true;
2923 notifier_list_notify(&migration_state_notifiers, ms);
2924
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002925 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
2926
2927 qemu_mutex_unlock_iothread();
2928
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002929 if (migrate_postcopy_ram()) {
2930 /*
2931 * Although this ping is just for debug, it could potentially be
2932 * used for getting a better measurement of downtime at the source.
2933 */
2934 qemu_savevm_send_ping(ms->to_dst_file, 4);
2935 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002936
Pavel Butsykinced1c612017-02-03 18:23:21 +03002937 if (migrate_release_ram()) {
2938 ram_postcopy_migrated_memory_release(ms);
2939 }
2940
zhanghailiang89a02a92016-01-15 11:37:42 +08002941 ret = qemu_file_get_error(ms->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002942 if (ret) {
2943 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00002944 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002945 MIGRATION_STATUS_FAILED);
2946 }
2947
2948 return ret;
2949
2950fail_closefb:
2951 qemu_fclose(fb);
2952fail:
zhanghailiang48781e52015-12-16 11:47:33 +00002953 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002954 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002955 if (restart_block) {
2956 /* A failure happened early enough that we know the destination hasn't
2957 * accessed block devices, so we're safe to recover.
2958 */
2959 Error *local_err = NULL;
2960
2961 bdrv_invalidate_cache_all(&local_err);
2962 if (local_err) {
2963 error_report_err(local_err);
2964 }
2965 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002966 qemu_mutex_unlock_iothread();
2967 return -1;
2968}
2969
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002970/**
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002971 * migration_maybe_pause: Pause if required to by
2972 * migrate_pause_before_switchover called with the iothread locked
2973 * Returns: 0 on success
2974 */
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002975static int migration_maybe_pause(MigrationState *s,
2976 int *current_active_state,
2977 int new_state)
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002978{
2979 if (!migrate_pause_before_switchover()) {
2980 return 0;
2981 }
2982
2983 /* Since leaving this state is not atomic with posting the semaphore
2984 * it's possible that someone could have issued multiple migrate_continue
2985 * and the semaphore is incorrectly positive at this point;
2986 * the docs say it's undefined to reinit a semaphore that's already
2987 * init'd, so use timedwait to eat up any existing posts.
2988 */
2989 while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) {
2990 /* This block intentionally left blank */
2991 }
2992
Zhimin Feng89583382020-01-14 17:43:09 +08002993 /*
2994 * If the migration is cancelled when it is in the completion phase,
2995 * the migration state is set to MIGRATION_STATUS_CANCELLING.
2996 * So we don't need to wait a semaphore, otherwise we would always
2997 * wait for the 'pause_sem' semaphore.
2998 */
2999 if (s->state != MIGRATION_STATUS_CANCELLING) {
3000 qemu_mutex_unlock_iothread();
3001 migrate_set_state(&s->state, *current_active_state,
3002 MIGRATION_STATUS_PRE_SWITCHOVER);
3003 qemu_sem_wait(&s->pause_sem);
3004 migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER,
3005 new_state);
3006 *current_active_state = new_state;
3007 qemu_mutex_lock_iothread();
3008 }
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01003009
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01003010 return s->state == new_state ? 0 : -EINVAL;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01003011}
3012
3013/**
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003014 * migration_completion: Used by migration_thread when there's not much left.
3015 * The caller 'breaks' the loop when this returns.
3016 *
3017 * @s: Current migration state
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003018 */
Peter Xu2ad87302018-01-03 20:20:14 +08003019static void migration_completion(MigrationState *s)
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003020{
3021 int ret;
Peter Xu2ad87302018-01-03 20:20:14 +08003022 int current_active_state = s->state;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003023
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003024 if (s->state == MIGRATION_STATUS_ACTIVE) {
3025 qemu_mutex_lock_iothread();
Peter Xu64909f92018-01-03 20:20:10 +08003026 s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Daniel Henrique Barbozafb064112018-12-05 17:47:01 -02003027 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
Peter Xu7287cbd2018-01-03 20:20:09 +08003028 s->vm_was_running = runstate_is_running();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003029 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003030
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003031 if (!ret) {
Fam Zhenga1fbe752017-06-17 00:06:58 +08003032 bool inactivate = !migrate_colo_enabled();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003033 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02003034 if (ret >= 0) {
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01003035 ret = migration_maybe_pause(s, &current_active_state,
3036 MIGRATION_STATUS_DEVICE);
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01003037 }
3038 if (ret >= 0) {
Kevin Wolff07fa4c2017-05-22 17:10:38 +02003039 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
Fam Zhenga1fbe752017-06-17 00:06:58 +08003040 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
3041 inactivate);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02003042 }
Fam Zhenga1fbe752017-06-17 00:06:58 +08003043 if (inactivate && ret >= 0) {
3044 s->block_inactive = true;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003045 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003046 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003047 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003048
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003049 if (ret < 0) {
3050 goto fail;
3051 }
3052 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
3053 trace_migration_completion_postcopy_end();
3054
zhanghailiang89a02a92016-01-15 11:37:42 +08003055 qemu_savevm_state_complete_postcopy(s->to_dst_file);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003056 trace_migration_completion_postcopy_end_after_complete();
3057 }
3058
3059 /*
3060 * If rp was opened we must clean up the thread before
3061 * cleaning everything else up (since if there are no failures
3062 * it will wait for the destination to send it's status in
3063 * a SHUT command).
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003064 */
Peter Xu0425dc92017-05-31 18:35:34 +08003065 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003066 int rp_error;
Peter Xu0425dc92017-05-31 18:35:34 +08003067 trace_migration_return_path_end_before();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003068 rp_error = await_return_path_close_on_source(s);
Peter Xu0425dc92017-05-31 18:35:34 +08003069 trace_migration_return_path_end_after(rp_error);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003070 if (rp_error) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02003071 goto fail_invalidate;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003072 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003073 }
3074
zhanghailiang89a02a92016-01-15 11:37:42 +08003075 if (qemu_file_get_error(s->to_dst_file)) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003076 trace_migration_completion_file_err();
Greg Kurzfe904ea2016-05-18 15:44:36 +02003077 goto fail_invalidate;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003078 }
3079
zhanghailiang0b827d52016-10-27 14:42:54 +08003080 if (!migrate_colo_enabled()) {
3081 migrate_set_state(&s->state, current_active_state,
3082 MIGRATION_STATUS_COMPLETED);
3083 }
3084
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003085 return;
3086
Greg Kurzfe904ea2016-05-18 15:44:36 +02003087fail_invalidate:
3088 /* If not doing postcopy, vm_start() will be called: let's regain
3089 * control on images.
3090 */
Dr. David Alan Gilbert6039dd52018-02-05 09:13:37 +00003091 if (s->state == MIGRATION_STATUS_ACTIVE ||
3092 s->state == MIGRATION_STATUS_DEVICE) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02003093 Error *local_err = NULL;
3094
zhanghailiang1d2acc32017-01-24 15:59:52 +08003095 qemu_mutex_lock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02003096 bdrv_invalidate_cache_all(&local_err);
3097 if (local_err) {
3098 error_report_err(local_err);
zhanghailiang1d2acc32017-01-24 15:59:52 +08003099 } else {
3100 s->block_inactive = false;
Greg Kurzfe904ea2016-05-18 15:44:36 +02003101 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08003102 qemu_mutex_unlock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02003103 }
3104
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003105fail:
zhanghailiang48781e52015-12-16 11:47:33 +00003106 migrate_set_state(&s->state, current_active_state,
3107 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003108}
3109
zhanghailiang35a6ed42016-10-27 14:42:52 +08003110bool migrate_colo_enabled(void)
3111{
3112 MigrationState *s = migrate_get_current();
3113 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
3114}
3115
Peter Xub23c2ad2018-05-02 18:47:19 +08003116typedef enum MigThrError {
3117 /* No error detected */
3118 MIG_THR_ERR_NONE = 0,
3119 /* Detected error, but resumed successfully */
3120 MIG_THR_ERR_RECOVERED = 1,
3121 /* Detected fatal error, need to exit */
3122 MIG_THR_ERR_FATAL = 2,
3123} MigThrError;
3124
Peter Xu94190692018-05-02 18:47:34 +08003125static int postcopy_resume_handshake(MigrationState *s)
3126{
3127 qemu_savevm_send_postcopy_resume(s->to_dst_file);
3128
3129 while (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
3130 qemu_sem_wait(&s->rp_state.rp_sem);
3131 }
3132
3133 if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
3134 return 0;
3135 }
3136
3137 return -1;
3138}
3139
Peter Xu135b87b2018-05-02 18:47:25 +08003140/* Return zero if success, or <0 for error */
3141static int postcopy_do_resume(MigrationState *s)
3142{
Peter Xud1b8ead2018-05-02 18:47:31 +08003143 int ret;
3144
3145 /*
3146 * Call all the resume_prepare() hooks, so that modules can be
3147 * ready for the migration resume.
3148 */
3149 ret = qemu_savevm_state_resume_prepare(s);
3150 if (ret) {
3151 error_report("%s: resume_prepare() failure detected: %d",
3152 __func__, ret);
3153 return ret;
3154 }
3155
3156 /*
Peter Xu94190692018-05-02 18:47:34 +08003157 * Last handshake with destination on the resume (destination will
3158 * switch to postcopy-active afterwards)
Peter Xud1b8ead2018-05-02 18:47:31 +08003159 */
Peter Xu94190692018-05-02 18:47:34 +08003160 ret = postcopy_resume_handshake(s);
3161 if (ret) {
3162 error_report("%s: handshake failed: %d", __func__, ret);
3163 return ret;
3164 }
Peter Xud1b8ead2018-05-02 18:47:31 +08003165
Peter Xu135b87b2018-05-02 18:47:25 +08003166 return 0;
3167}
3168
Peter Xub23c2ad2018-05-02 18:47:19 +08003169/*
3170 * We don't return until we are in a safe state to continue current
3171 * postcopy migration. Returns MIG_THR_ERR_RECOVERED if recovered, or
3172 * MIG_THR_ERR_FATAL if unrecovery failure happened.
3173 */
3174static MigThrError postcopy_pause(MigrationState *s)
3175{
3176 assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
Peter Xub23c2ad2018-05-02 18:47:19 +08003177
Peter Xu135b87b2018-05-02 18:47:25 +08003178 while (true) {
Peter Xu62df0662018-05-02 18:47:38 +08003179 QEMUFile *file;
3180
Peter Xu135b87b2018-05-02 18:47:25 +08003181 migrate_set_state(&s->state, s->state,
3182 MIGRATION_STATUS_POSTCOPY_PAUSED);
Peter Xub23c2ad2018-05-02 18:47:19 +08003183
Peter Xu135b87b2018-05-02 18:47:25 +08003184 /* Current channel is possibly broken. Release it. */
3185 assert(s->to_dst_file);
Peter Xu62df0662018-05-02 18:47:38 +08003186 qemu_mutex_lock(&s->qemu_file_lock);
3187 file = s->to_dst_file;
Peter Xu135b87b2018-05-02 18:47:25 +08003188 s->to_dst_file = NULL;
Peter Xu62df0662018-05-02 18:47:38 +08003189 qemu_mutex_unlock(&s->qemu_file_lock);
3190
3191 qemu_file_shutdown(file);
3192 qemu_fclose(file);
Peter Xub23c2ad2018-05-02 18:47:19 +08003193
Peter Xu135b87b2018-05-02 18:47:25 +08003194 error_report("Detected IO failure for postcopy. "
3195 "Migration paused.");
3196
3197 /*
3198 * We wait until things fixed up. Then someone will setup the
3199 * status back for us.
3200 */
3201 while (s->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
3202 qemu_sem_wait(&s->postcopy_pause_sem);
3203 }
3204
3205 if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
3206 /* Woken up by a recover procedure. Give it a shot */
3207
3208 /*
3209 * Firstly, let's wake up the return path now, with a new
3210 * return path channel.
3211 */
3212 qemu_sem_post(&s->postcopy_pause_rp_sem);
3213
3214 /* Do the resume logic */
3215 if (postcopy_do_resume(s) == 0) {
3216 /* Let's continue! */
3217 trace_postcopy_pause_continued();
3218 return MIG_THR_ERR_RECOVERED;
3219 } else {
3220 /*
3221 * Something wrong happened during the recovery, let's
3222 * pause again. Pause is always better than throwing
3223 * data away.
3224 */
3225 continue;
3226 }
3227 } else {
3228 /* This is not right... Time to quit. */
3229 return MIG_THR_ERR_FATAL;
3230 }
Peter Xub23c2ad2018-05-02 18:47:19 +08003231 }
Peter Xub23c2ad2018-05-02 18:47:19 +08003232}
3233
3234static MigThrError migration_detect_error(MigrationState *s)
3235{
3236 int ret;
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003237 int state = s->state;
Yury Kotov3d661c82019-04-22 13:34:20 +03003238 Error *local_error = NULL;
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003239
3240 if (state == MIGRATION_STATUS_CANCELLING ||
3241 state == MIGRATION_STATUS_CANCELLED) {
3242 /* End the migration, but don't set the state to failed */
3243 return MIG_THR_ERR_FATAL;
3244 }
Peter Xub23c2ad2018-05-02 18:47:19 +08003245
3246 /* Try to detect any file errors */
Yury Kotov3d661c82019-04-22 13:34:20 +03003247 ret = qemu_file_get_error_obj(s->to_dst_file, &local_error);
Peter Xub23c2ad2018-05-02 18:47:19 +08003248 if (!ret) {
3249 /* Everything is fine */
Yury Kotov3d661c82019-04-22 13:34:20 +03003250 assert(!local_error);
Peter Xub23c2ad2018-05-02 18:47:19 +08003251 return MIG_THR_ERR_NONE;
3252 }
3253
Yury Kotov3d661c82019-04-22 13:34:20 +03003254 if (local_error) {
3255 migrate_set_error(s, local_error);
3256 error_free(local_error);
3257 }
3258
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003259 if (state == MIGRATION_STATUS_POSTCOPY_ACTIVE && ret == -EIO) {
Peter Xub23c2ad2018-05-02 18:47:19 +08003260 /*
3261 * For postcopy, we allow the network to be down for a
3262 * while. After that, it can be continued by a
3263 * recovery phase.
3264 */
3265 return postcopy_pause(s);
3266 } else {
3267 /*
3268 * For precopy (or postcopy with error outside IO), we fail
3269 * with no time.
3270 */
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003271 migrate_set_state(&s->state, state, MIGRATION_STATUS_FAILED);
Peter Xub23c2ad2018-05-02 18:47:19 +08003272 trace_migration_thread_file_err();
3273
3274 /* Time to stop the migration, now. */
3275 return MIG_THR_ERR_FATAL;
3276 }
3277}
3278
Wei Yang268dcd42019-09-12 10:49:57 +08003279/* How many bytes have we transferred since the beginning of the migration */
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003280static uint64_t migration_total_bytes(MigrationState *s)
3281{
Juan Quintelab9ee2f72016-01-15 11:40:13 +01003282 return qemu_ftell(s->to_dst_file) + ram_counters.multifd_bytes;
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003283}
3284
Peter Xucf011f02018-01-03 20:20:11 +08003285static void migration_calculate_complete(MigrationState *s)
3286{
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003287 uint64_t bytes = migration_total_bytes(s);
Peter Xucf011f02018-01-03 20:20:11 +08003288 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela6cde6fb2018-06-26 15:26:35 +02003289 int64_t transfer_time;
Peter Xucf011f02018-01-03 20:20:11 +08003290
3291 s->total_time = end_time - s->start_time;
3292 if (!s->downtime) {
3293 /*
3294 * It's still not set, so we are precopy migration. For
3295 * postcopy, downtime is calculated during postcopy_start().
3296 */
3297 s->downtime = end_time - s->downtime_start;
3298 }
3299
Juan Quintela6cde6fb2018-06-26 15:26:35 +02003300 transfer_time = s->total_time - s->setup_time;
3301 if (transfer_time) {
3302 s->mbps = ((double) bytes * 8.0) / transfer_time / 1000;
Peter Xucf011f02018-01-03 20:20:11 +08003303 }
3304}
3305
Ivan Ren87f3bd82019-08-02 18:18:41 +08003306static void update_iteration_initial_status(MigrationState *s)
3307{
3308 /*
3309 * Update these three fields at the same time to avoid mismatch info lead
3310 * wrong speed calculation.
3311 */
3312 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3313 s->iteration_initial_bytes = migration_total_bytes(s);
3314 s->iteration_initial_pages = ram_get_total_transferred_pages();
3315}
3316
Peter Xub15df1a2018-01-03 20:20:13 +08003317static void migration_update_counters(MigrationState *s,
3318 int64_t current_time)
3319{
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08003320 uint64_t transferred, transferred_pages, time_spent;
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003321 uint64_t current_bytes; /* bytes transferred since the beginning */
Peter Xub15df1a2018-01-03 20:20:13 +08003322 double bandwidth;
3323
3324 if (current_time < s->iteration_start_time + BUFFER_DELAY) {
3325 return;
3326 }
3327
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003328 current_bytes = migration_total_bytes(s);
3329 transferred = current_bytes - s->iteration_initial_bytes;
Peter Xub15df1a2018-01-03 20:20:13 +08003330 time_spent = current_time - s->iteration_start_time;
3331 bandwidth = (double)transferred / time_spent;
Wei Wang0781c1e2018-01-22 19:36:39 +08003332 s->threshold_size = bandwidth * s->parameters.downtime_limit;
Peter Xub15df1a2018-01-03 20:20:13 +08003333
3334 s->mbps = (((double) transferred * 8.0) /
3335 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
3336
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08003337 transferred_pages = ram_get_total_transferred_pages() -
3338 s->iteration_initial_pages;
3339 s->pages_per_second = (double) transferred_pages /
3340 (((double) time_spent / 1000.0));
3341
Peter Xub15df1a2018-01-03 20:20:13 +08003342 /*
3343 * if we haven't sent anything, we don't want to
3344 * recalculate. 10000 is a small enough number for our purposes
3345 */
3346 if (ram_counters.dirty_pages_rate && transferred > 10000) {
Balamuruhan S650af892018-06-12 14:20:09 +05303347 s->expected_downtime = ram_counters.remaining / bandwidth;
Peter Xub15df1a2018-01-03 20:20:13 +08003348 }
3349
3350 qemu_file_reset_rate_limit(s->to_dst_file);
3351
Ivan Ren87f3bd82019-08-02 18:18:41 +08003352 update_iteration_initial_status(s);
Peter Xub15df1a2018-01-03 20:20:13 +08003353
3354 trace_migrate_transferred(transferred, time_spent,
Wei Wang0781c1e2018-01-22 19:36:39 +08003355 bandwidth, s->threshold_size);
Peter Xub15df1a2018-01-03 20:20:13 +08003356}
3357
Peter Xu2ad87302018-01-03 20:20:14 +08003358/* Migration thread iteration status */
3359typedef enum {
3360 MIG_ITERATE_RESUME, /* Resume current iteration */
3361 MIG_ITERATE_SKIP, /* Skip current iteration */
3362 MIG_ITERATE_BREAK, /* Break the loop */
3363} MigIterateState;
3364
3365/*
3366 * Return true if continue to the next iteration directly, false
3367 * otherwise.
3368 */
3369static MigIterateState migration_iteration_run(MigrationState *s)
3370{
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04003371 uint64_t pending_size, pend_pre, pend_compat, pend_post;
Peter Xu2ad87302018-01-03 20:20:14 +08003372 bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE;
3373
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04003374 qemu_savevm_state_pending(s->to_dst_file, s->threshold_size, &pend_pre,
3375 &pend_compat, &pend_post);
3376 pending_size = pend_pre + pend_compat + pend_post;
Peter Xu2ad87302018-01-03 20:20:14 +08003377
3378 trace_migrate_pending(pending_size, s->threshold_size,
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04003379 pend_pre, pend_compat, pend_post);
Peter Xu2ad87302018-01-03 20:20:14 +08003380
3381 if (pending_size && pending_size >= s->threshold_size) {
3382 /* Still a significant amount to transfer */
Wei Yang52aec702019-07-18 16:37:47 +08003383 if (!in_postcopy && pend_pre <= s->threshold_size &&
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01003384 qatomic_read(&s->start_postcopy)) {
Peter Xu2ad87302018-01-03 20:20:14 +08003385 if (postcopy_start(s)) {
3386 error_report("%s: postcopy failed to start", __func__);
3387 }
3388 return MIG_ITERATE_SKIP;
3389 }
3390 /* Just another iteration step */
Wei Yang17d93512019-10-06 06:05:16 +08003391 qemu_savevm_state_iterate(s->to_dst_file, in_postcopy);
Peter Xu2ad87302018-01-03 20:20:14 +08003392 } else {
3393 trace_migration_thread_low_pending(pending_size);
3394 migration_completion(s);
3395 return MIG_ITERATE_BREAK;
3396 }
3397
3398 return MIG_ITERATE_RESUME;
3399}
3400
Peter Xu199aa6d2018-01-03 20:20:15 +08003401static void migration_iteration_finish(MigrationState *s)
3402{
3403 /* If we enabled cpu throttling for auto-converge, turn it off. */
3404 cpu_throttle_stop();
3405
3406 qemu_mutex_lock_iothread();
3407 switch (s->state) {
3408 case MIGRATION_STATUS_COMPLETED:
3409 migration_calculate_complete(s);
3410 runstate_set(RUN_STATE_POSTMIGRATE);
3411 break;
3412
3413 case MIGRATION_STATUS_ACTIVE:
3414 /*
3415 * We should really assert here, but since it's during
3416 * migration, let's try to reduce the usage of assertions.
3417 */
3418 if (!migrate_colo_enabled()) {
3419 error_report("%s: critical error: calling COLO code without "
3420 "COLO enabled", __func__);
3421 }
3422 migrate_start_colo_process(s);
3423 /*
3424 * Fixme: we will run VM in COLO no matter its old running state.
3425 * After exited COLO, we will keep running.
3426 */
3427 s->vm_was_running = true;
3428 /* Fallthrough */
3429 case MIGRATION_STATUS_FAILED:
3430 case MIGRATION_STATUS_CANCELLED:
Dr. David Alan Gilbert57225e52018-07-19 10:22:57 +01003431 case MIGRATION_STATUS_CANCELLING:
Peter Xu199aa6d2018-01-03 20:20:15 +08003432 if (s->vm_was_running) {
3433 vm_start();
3434 } else {
3435 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
3436 runstate_set(RUN_STATE_POSTMIGRATE);
3437 }
3438 }
3439 break;
3440
3441 default:
3442 /* Should not reach here, but if so, forgive the VM. */
3443 error_report("%s: Unknown ending state %d", __func__, s->state);
3444 break;
3445 }
Yury Kotovfd392cf2019-04-08 14:33:43 +03003446 migrate_fd_cleanup_schedule(s);
Peter Xu199aa6d2018-01-03 20:20:15 +08003447 qemu_mutex_unlock_iothread();
3448}
3449
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003450void migration_make_urgent_request(void)
3451{
3452 qemu_sem_post(&migrate_get_current()->rate_limit_sem);
3453}
3454
3455void migration_consume_urgent_request(void)
3456{
3457 qemu_sem_wait(&migrate_get_current()->rate_limit_sem);
3458}
3459
Dr. David Alan Gilbert97e1e062019-12-05 10:29:18 +00003460/* Returns true if the rate limiting was broken by an urgent request */
3461bool migration_rate_limit(void)
3462{
3463 int64_t now = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3464 MigrationState *s = migrate_get_current();
3465
3466 bool urgent = false;
3467 migration_update_counters(s, now);
3468 if (qemu_file_rate_limit(s->to_dst_file)) {
Lukas Straub77386122020-05-20 22:42:32 +02003469
3470 if (qemu_file_get_error(s->to_dst_file)) {
3471 return false;
3472 }
Dr. David Alan Gilbert97e1e062019-12-05 10:29:18 +00003473 /*
3474 * Wait for a delay to do rate limiting OR
3475 * something urgent to post the semaphore.
3476 */
3477 int ms = s->iteration_start_time + BUFFER_DELAY - now;
3478 trace_migration_rate_limit_pre(ms);
3479 if (qemu_sem_timedwait(&s->rate_limit_sem, ms) == 0) {
3480 /*
3481 * We were woken by one or more urgent things but
3482 * the timedwait will have consumed one of them.
3483 * The service routine for the urgent wake will dec
3484 * the semaphore itself for each item it consumes,
3485 * so add this one we just eat back.
3486 */
3487 qemu_sem_post(&s->rate_limit_sem);
3488 urgent = true;
3489 }
3490 trace_migration_rate_limit_post(urgent);
3491 }
3492 return urgent;
3493}
3494
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00003495/*
3496 * Master migration thread on the source VM.
3497 * It drives the migration and pumps the data down the outgoing channel.
3498 */
Juan Quintela5f496a12013-02-22 17:36:30 +01003499static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003500{
Juan Quintela9848a402012-12-19 09:55:50 +01003501 MigrationState *s = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +01003502 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Peter Xub23c2ad2018-05-02 18:47:19 +08003503 MigThrError thr_error;
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003504 bool urgent = false;
Juan Quintela76f59332012-10-03 20:16:24 +02003505
Paolo Bonziniab28bd22015-07-09 08:55:38 +02003506 rcu_register_thread();
3507
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +00003508 object_ref(OBJECT(s));
Ivan Ren87f3bd82019-08-02 18:18:41 +08003509 update_iteration_initial_status(s);
Peter Xub15df1a2018-01-03 20:20:13 +08003510
zhanghailiang89a02a92016-01-15 11:37:42 +08003511 qemu_savevm_state_header(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003512
Peter Xu62a02652017-06-14 15:55:58 +08003513 /*
3514 * If we opened the return path, we need to make sure dst has it
3515 * opened as well.
3516 */
3517 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003518 /* Now tell the dest that it should open its end so it can reply */
zhanghailiang89a02a92016-01-15 11:37:42 +08003519 qemu_savevm_send_open_return_path(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003520
3521 /* And do a ping that will make stuff easier to debug */
zhanghailiang89a02a92016-01-15 11:37:42 +08003522 qemu_savevm_send_ping(s->to_dst_file, 1);
Peter Xu0425dc92017-05-31 18:35:34 +08003523 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003524
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03003525 if (migrate_postcopy()) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003526 /*
3527 * Tell the destination that we *might* want to do postcopy later;
3528 * if the other end can't do postcopy it should fail now, nice and
3529 * early.
3530 */
zhanghailiang89a02a92016-01-15 11:37:42 +08003531 qemu_savevm_send_postcopy_advise(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003532 }
3533
Zhang Chenaad555c2018-09-03 12:38:47 +08003534 if (migrate_colo_enabled()) {
3535 /* Notify migration destination that we enable COLO */
3536 qemu_savevm_send_colo_enable(s->to_dst_file);
3537 }
3538
Juan Quintela9907e842017-06-28 11:52:24 +02003539 qemu_savevm_state_setup(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003540
Keqian Zhud05de9e2020-02-04 13:08:41 +08003541 if (qemu_savevm_state_guest_unplug_pending()) {
Jens Freimannc7e0acd2019-10-29 12:49:02 +01003542 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3543 MIGRATION_STATUS_WAIT_UNPLUG);
3544
3545 while (s->state == MIGRATION_STATUS_WAIT_UNPLUG &&
3546 qemu_savevm_state_guest_unplug_pending()) {
3547 qemu_sem_timedwait(&s->wait_unplug_sem, 250);
3548 }
3549
3550 migrate_set_state(&s->state, MIGRATION_STATUS_WAIT_UNPLUG,
3551 MIGRATION_STATUS_ACTIVE);
3552 }
3553
Alex Blighbc72ad62013-08-21 16:03:08 +01003554 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang48781e52015-12-16 11:47:33 +00003555 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3556 MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04003557
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00003558 trace_migration_thread_setup_complete();
3559
Wei Yang8f8d5282019-07-17 08:53:41 +08003560 while (migration_is_active(s)) {
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003561 if (urgent || !qemu_file_rate_limit(s->to_dst_file)) {
Peter Xu2ad87302018-01-03 20:20:14 +08003562 MigIterateState iter_state = migration_iteration_run(s);
3563 if (iter_state == MIG_ITERATE_SKIP) {
3564 continue;
3565 } else if (iter_state == MIG_ITERATE_BREAK) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003566 break;
Juan Quintelac369f402012-10-03 20:33:34 +02003567 }
3568 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01003569
Peter Xub23c2ad2018-05-02 18:47:19 +08003570 /*
3571 * Try to detect any kind of failures, and see whether we
3572 * should stop the migration now.
3573 */
3574 thr_error = migration_detect_error(s);
3575 if (thr_error == MIG_THR_ERR_FATAL) {
3576 /* Stop migration */
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01003577 break;
Peter Xub23c2ad2018-05-02 18:47:19 +08003578 } else if (thr_error == MIG_THR_ERR_RECOVERED) {
3579 /*
3580 * Just recovered from a e.g. network failure, reset all
3581 * the local variables. This is important to avoid
3582 * breaking transferred_bytes and bandwidth calculation
3583 */
Ivan Ren87f3bd82019-08-02 18:18:41 +08003584 update_iteration_initial_status(s);
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01003585 }
Peter Xub15df1a2018-01-03 20:20:13 +08003586
Dr. David Alan Gilbert97e1e062019-12-05 10:29:18 +00003587 urgent = migration_rate_limit();
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01003588 }
3589
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003590 trace_migration_thread_after_loop();
Peter Xu199aa6d2018-01-03 20:20:15 +08003591 migration_iteration_finish(s);
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +00003592 object_unref(OBJECT(s));
Paolo Bonziniab28bd22015-07-09 08:55:38 +02003593 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003594 return NULL;
3595}
3596
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00003597void migrate_fd_connect(MigrationState *s, Error *error_in)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003598{
Juan Quintela00f4b572019-06-12 11:33:27 +02003599 Error *local_err = NULL;
Peter Xud3e35b82018-05-02 18:47:24 +08003600 int64_t rate_limit;
3601 bool resume = s->state == MIGRATION_STATUS_POSTCOPY_PAUSED;
3602
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05303603 s->expected_downtime = s->parameters.downtime_limit;
Marc-André Lureau9cbc3642020-03-25 19:47:21 +01003604 if (resume) {
3605 assert(s->cleanup_bh);
3606 } else {
3607 assert(!s->cleanup_bh);
3608 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup_bh, s);
3609 }
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00003610 if (error_in) {
3611 migrate_fd_error(s, error_in);
3612 migrate_fd_cleanup(s);
3613 return;
3614 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003615
Peter Xud3e35b82018-05-02 18:47:24 +08003616 if (resume) {
3617 /* This is a resumed migration */
Peter Xu8504dde2019-09-06 21:01:03 +08003618 rate_limit = s->parameters.max_postcopy_bandwidth /
3619 XFER_LIMIT_RATIO;
Peter Xud3e35b82018-05-02 18:47:24 +08003620 } else {
3621 /* This is a fresh new migration */
3622 rate_limit = s->parameters.max_bandwidth / XFER_LIMIT_RATIO;
Paolo Bonzini442773c2013-02-22 17:36:44 +01003623
Peter Xud3e35b82018-05-02 18:47:24 +08003624 /* Notify before starting migration thread */
3625 notifier_list_notify(&migration_state_notifiers, s);
3626 }
3627
3628 qemu_file_set_rate_limit(s->to_dst_file, rate_limit);
3629 qemu_file_set_blocking(s->to_dst_file, true);
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02003630
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003631 /*
Peter Xuc788ada2017-06-26 18:28:55 +08003632 * Open the return path. For postcopy, it is used exclusively. For
3633 * precopy, only if user specified "return-path" capability would
3634 * QEMU uses the return path.
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003635 */
Peter Xuc788ada2017-06-26 18:28:55 +08003636 if (migrate_postcopy_ram() || migrate_use_return_path()) {
Peter Xud3e35b82018-05-02 18:47:24 +08003637 if (open_return_path_on_source(s, !resume)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003638 error_report("Unable to open return-path for postcopy");
Peter Xud3e35b82018-05-02 18:47:24 +08003639 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003640 migrate_fd_cleanup(s);
3641 return;
3642 }
3643 }
3644
Peter Xud3e35b82018-05-02 18:47:24 +08003645 if (resume) {
Peter Xu135b87b2018-05-02 18:47:25 +08003646 /* Wakeup the main migration thread to do the recovery */
3647 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
3648 MIGRATION_STATUS_POSTCOPY_RECOVER);
3649 qemu_sem_post(&s->postcopy_pause_sem);
Peter Xud3e35b82018-05-02 18:47:24 +08003650 return;
3651 }
3652
Juan Quintela00f4b572019-06-12 11:33:27 +02003653 if (multifd_save_setup(&local_err) != 0) {
3654 error_report_err(local_err);
Juan Quintelaf986c3d2016-01-14 16:52:55 +01003655 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3656 MIGRATION_STATUS_FAILED);
3657 migrate_fd_cleanup(s);
3658 return;
3659 }
Pankaj Gupta009fad72017-01-23 19:12:56 +05303660 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01003661 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003662 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003663}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00003664
Peter Xu9d18af92017-06-27 12:10:19 +08003665void migration_global_dump(Monitor *mon)
3666{
3667 MigrationState *ms = migrate_get_current();
3668
Juan Quintela6f0f6422017-10-26 11:49:57 +02003669 monitor_printf(mon, "globals:\n");
3670 monitor_printf(mon, "store-global-state: %s\n",
3671 ms->store_global_state ? "on" : "off");
3672 monitor_printf(mon, "only-migratable: %s\n",
Markus Armbruster811f8652019-04-01 11:08:24 +02003673 only_migratable ? "on" : "off");
Juan Quintela6f0f6422017-10-26 11:49:57 +02003674 monitor_printf(mon, "send-configuration: %s\n",
3675 ms->send_configuration ? "on" : "off");
3676 monitor_printf(mon, "send-section-footer: %s\n",
3677 ms->send_section_footer ? "on" : "off");
Xiao Guangrongf5482222018-05-03 16:06:11 +08003678 monitor_printf(mon, "decompress-error-check: %s\n",
3679 ms->decompress_error_check ? "on" : "off");
Peter Xu002cad62019-06-03 14:50:56 +08003680 monitor_printf(mon, "clear-bitmap-shift: %u\n",
3681 ms->clear_bitmap_shift);
Peter Xu9d18af92017-06-27 12:10:19 +08003682}
3683
Peter Xu20814752017-07-18 11:39:03 +08003684#define DEFINE_PROP_MIG_CAP(name, x) \
3685 DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
3686
Peter Xu52722982017-06-27 12:10:14 +08003687static Property migration_properties[] = {
3688 DEFINE_PROP_BOOL("store-global-state", MigrationState,
3689 store_global_state, true),
Peter Xu71dd4c12017-06-27 12:10:16 +08003690 DEFINE_PROP_BOOL("send-configuration", MigrationState,
3691 send_configuration, true),
Peter Xu15c38502017-06-27 12:10:17 +08003692 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
3693 send_section_footer, true),
Xiao Guangrongf5482222018-05-03 16:06:11 +08003694 DEFINE_PROP_BOOL("decompress-error-check", MigrationState,
3695 decompress_error_check, true),
Peter Xu002cad62019-06-03 14:50:56 +08003696 DEFINE_PROP_UINT8("x-clear-bitmap-shift", MigrationState,
3697 clear_bitmap_shift, CLEAR_BITMAP_SHIFT_DEFAULT),
Peter Xu89632fa2017-07-18 11:39:02 +08003698
3699 /* Migration parameters */
Juan Quintela741d4082017-12-01 13:08:38 +01003700 DEFINE_PROP_UINT8("x-compress-level", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003701 parameters.compress_level,
3702 DEFAULT_MIGRATE_COMPRESS_LEVEL),
Juan Quintela741d4082017-12-01 13:08:38 +01003703 DEFINE_PROP_UINT8("x-compress-threads", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003704 parameters.compress_threads,
3705 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
Xiao Guangrong1d588722018-08-21 16:10:20 +08003706 DEFINE_PROP_BOOL("x-compress-wait-thread", MigrationState,
3707 parameters.compress_wait_thread, true),
Juan Quintela741d4082017-12-01 13:08:38 +01003708 DEFINE_PROP_UINT8("x-decompress-threads", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003709 parameters.decompress_threads,
3710 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
Keqian Zhudc14a472020-02-24 10:31:42 +08003711 DEFINE_PROP_UINT8("x-throttle-trigger-threshold", MigrationState,
3712 parameters.throttle_trigger_threshold,
3713 DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD),
Juan Quintela741d4082017-12-01 13:08:38 +01003714 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003715 parameters.cpu_throttle_initial,
3716 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
Juan Quintela741d4082017-12-01 13:08:38 +01003717 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003718 parameters.cpu_throttle_increment,
3719 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
Keqian Zhucbbf8182020-04-13 18:15:08 +08003720 DEFINE_PROP_BOOL("x-cpu-throttle-tailslow", MigrationState,
3721 parameters.cpu_throttle_tailslow, false),
Juan Quintela741d4082017-12-01 13:08:38 +01003722 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003723 parameters.max_bandwidth, MAX_THROTTLE),
Juan Quintela741d4082017-12-01 13:08:38 +01003724 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003725 parameters.downtime_limit,
3726 DEFAULT_MIGRATE_SET_DOWNTIME),
Juan Quintela741d4082017-12-01 13:08:38 +01003727 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003728 parameters.x_checkpoint_delay,
3729 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
Juan Quintelacbfd6c92019-02-06 13:54:06 +01003730 DEFINE_PROP_UINT8("multifd-channels", MigrationState,
3731 parameters.multifd_channels,
Juan Quintela4075fb12016-01-15 08:56:17 +01003732 DEFAULT_MIGRATE_MULTIFD_CHANNELS),
Juan Quintela96eef042019-01-16 10:35:55 +01003733 DEFINE_PROP_MULTIFD_COMPRESSION("multifd-compression", MigrationState,
3734 parameters.multifd_compression,
3735 DEFAULT_MIGRATE_MULTIFD_COMPRESSION),
Juan Quintela9004db42020-01-23 17:08:52 +01003736 DEFINE_PROP_UINT8("multifd-zlib-level", MigrationState,
3737 parameters.multifd_zlib_level,
3738 DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL),
Juan Quintela6a9ad152020-01-23 17:41:36 +01003739 DEFINE_PROP_UINT8("multifd-zstd-level", MigrationState,
3740 parameters.multifd_zstd_level,
3741 DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL),
Juan Quintela73af8dd2017-10-05 21:30:10 +02003742 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
3743 parameters.xbzrle_cache_size,
3744 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01003745 DEFINE_PROP_SIZE("max-postcopy-bandwidth", MigrationState,
3746 parameters.max_postcopy_bandwidth,
3747 DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH),
Li Qiang4cbc9c72018-08-01 06:00:20 -07003748 DEFINE_PROP_UINT8("max-cpu-throttle", MigrationState,
3749 parameters.max_cpu_throttle,
3750 DEFAULT_MIGRATE_MAX_CPU_THROTTLE),
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00003751 DEFINE_PROP_SIZE("announce-initial", MigrationState,
3752 parameters.announce_initial,
3753 DEFAULT_MIGRATE_ANNOUNCE_INITIAL),
3754 DEFINE_PROP_SIZE("announce-max", MigrationState,
3755 parameters.announce_max,
3756 DEFAULT_MIGRATE_ANNOUNCE_MAX),
3757 DEFINE_PROP_SIZE("announce-rounds", MigrationState,
3758 parameters.announce_rounds,
3759 DEFAULT_MIGRATE_ANNOUNCE_ROUNDS),
3760 DEFINE_PROP_SIZE("announce-step", MigrationState,
3761 parameters.announce_step,
3762 DEFAULT_MIGRATE_ANNOUNCE_STEP),
Peter Xu20814752017-07-18 11:39:03 +08003763
3764 /* Migration capabilities */
3765 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
3766 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
3767 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
3768 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
3769 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
3770 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
3771 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
3772 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
3773 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
3774 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
3775 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
Juan Quintelacbfd6c92019-02-06 13:54:06 +01003776 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_MULTIFD),
Peter Xu20814752017-07-18 11:39:03 +08003777
Peter Xu52722982017-06-27 12:10:14 +08003778 DEFINE_PROP_END_OF_LIST(),
3779};
3780
Peter Xue5cb7e72017-06-27 12:10:13 +08003781static void migration_class_init(ObjectClass *klass, void *data)
3782{
3783 DeviceClass *dc = DEVICE_CLASS(klass);
3784
3785 dc->user_creatable = false;
Marc-André Lureau4f67d302020-01-10 19:30:32 +04003786 device_class_set_props(dc, migration_properties);
Peter Xue5cb7e72017-06-27 12:10:13 +08003787}
3788
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003789static void migration_instance_finalize(Object *obj)
3790{
3791 MigrationState *ms = MIGRATION_OBJ(obj);
3792 MigrationParameters *params = &ms->parameters;
3793
Juan Quintela87db1a72017-09-05 12:50:22 +02003794 qemu_mutex_destroy(&ms->error_mutex);
Peter Xu62df0662018-05-02 18:47:38 +08003795 qemu_mutex_destroy(&ms->qemu_file_lock);
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003796 g_free(params->tls_hostname);
3797 g_free(params->tls_creds);
Jens Freimannc7e0acd2019-10-29 12:49:02 +01003798 qemu_sem_destroy(&ms->wait_unplug_sem);
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003799 qemu_sem_destroy(&ms->rate_limit_sem);
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01003800 qemu_sem_destroy(&ms->pause_sem);
Peter Xub23c2ad2018-05-02 18:47:19 +08003801 qemu_sem_destroy(&ms->postcopy_pause_sem);
Peter Xu14b17422018-05-02 18:47:21 +08003802 qemu_sem_destroy(&ms->postcopy_pause_rp_sem);
Peter Xuedd090c2018-05-02 18:47:32 +08003803 qemu_sem_destroy(&ms->rp_state.rp_sem);
Marc-André Lureauab105cc2018-03-06 18:09:59 +01003804 error_free(ms->error);
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003805}
3806
Peter Xue5cb7e72017-06-27 12:10:13 +08003807static void migration_instance_init(Object *obj)
3808{
3809 MigrationState *ms = MIGRATION_OBJ(obj);
Peter Xu8b0b29d2017-07-18 11:39:06 +08003810 MigrationParameters *params = &ms->parameters;
Peter Xue5cb7e72017-06-27 12:10:13 +08003811
3812 ms->state = MIGRATION_STATUS_NONE;
Peter Xue5cb7e72017-06-27 12:10:13 +08003813 ms->mbps = -1;
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08003814 ms->pages_per_second = -1;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01003815 qemu_sem_init(&ms->pause_sem, 0);
Juan Quintela87db1a72017-09-05 12:50:22 +02003816 qemu_mutex_init(&ms->error_mutex);
Peter Xu8b0b29d2017-07-18 11:39:06 +08003817
3818 params->tls_hostname = g_strdup("");
3819 params->tls_creds = g_strdup("");
3820
3821 /* Set has_* up only for parameter checks */
3822 params->has_compress_level = true;
3823 params->has_compress_threads = true;
3824 params->has_decompress_threads = true;
Keqian Zhudc14a472020-02-24 10:31:42 +08003825 params->has_throttle_trigger_threshold = true;
Peter Xu8b0b29d2017-07-18 11:39:06 +08003826 params->has_cpu_throttle_initial = true;
3827 params->has_cpu_throttle_increment = true;
Keqian Zhucbbf8182020-04-13 18:15:08 +08003828 params->has_cpu_throttle_tailslow = true;
Peter Xu8b0b29d2017-07-18 11:39:06 +08003829 params->has_max_bandwidth = true;
3830 params->has_downtime_limit = true;
3831 params->has_x_checkpoint_delay = true;
3832 params->has_block_incremental = true;
Juan Quintelacbfd6c92019-02-06 13:54:06 +01003833 params->has_multifd_channels = true;
Juan Quintela96eef042019-01-16 10:35:55 +01003834 params->has_multifd_compression = true;
Juan Quintela9004db42020-01-23 17:08:52 +01003835 params->has_multifd_zlib_level = true;
Juan Quintela6a9ad152020-01-23 17:41:36 +01003836 params->has_multifd_zstd_level = true;
Juan Quintela73af8dd2017-10-05 21:30:10 +02003837 params->has_xbzrle_cache_size = true;
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01003838 params->has_max_postcopy_bandwidth = true;
Li Qiang4cbc9c72018-08-01 06:00:20 -07003839 params->has_max_cpu_throttle = true;
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00003840 params->has_announce_initial = true;
3841 params->has_announce_max = true;
3842 params->has_announce_rounds = true;
3843 params->has_announce_step = true;
Peter Xub23c2ad2018-05-02 18:47:19 +08003844
3845 qemu_sem_init(&ms->postcopy_pause_sem, 0);
Peter Xu14b17422018-05-02 18:47:21 +08003846 qemu_sem_init(&ms->postcopy_pause_rp_sem, 0);
Peter Xuedd090c2018-05-02 18:47:32 +08003847 qemu_sem_init(&ms->rp_state.rp_sem, 0);
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003848 qemu_sem_init(&ms->rate_limit_sem, 0);
Jens Freimannc7e0acd2019-10-29 12:49:02 +01003849 qemu_sem_init(&ms->wait_unplug_sem, 0);
Peter Xu62df0662018-05-02 18:47:38 +08003850 qemu_mutex_init(&ms->qemu_file_lock);
Peter Xu8b0b29d2017-07-18 11:39:06 +08003851}
3852
3853/*
3854 * Return true if check pass, false otherwise. Error will be put
3855 * inside errp if provided.
3856 */
3857static bool migration_object_check(MigrationState *ms, Error **errp)
3858{
Peter Xu6b19a7d2017-07-18 11:39:10 +08003859 MigrationCapabilityStatusList *head = NULL;
3860 /* Assuming all off */
3861 bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret;
3862 int i;
3863
Peter Xu8b0b29d2017-07-18 11:39:06 +08003864 if (!migrate_params_check(&ms->parameters, errp)) {
3865 return false;
3866 }
3867
Peter Xu6b19a7d2017-07-18 11:39:10 +08003868 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
3869 if (ms->enabled_capabilities[i]) {
3870 head = migrate_cap_add(head, i, true);
3871 }
3872 }
3873
3874 ret = migrate_caps_check(cap_list, head, errp);
3875
3876 /* It works with head == NULL */
3877 qapi_free_MigrationCapabilityStatusList(head);
3878
3879 return ret;
Peter Xue5cb7e72017-06-27 12:10:13 +08003880}
3881
3882static const TypeInfo migration_type = {
3883 .name = TYPE_MIGRATION,
Peter Xu01f6e142017-06-28 15:15:44 +08003884 /*
Peter Xuc8d3ff32017-07-05 16:21:23 +08003885 * NOTE: TYPE_MIGRATION is not really a device, as the object is
Markus Armbruster2194abd2020-06-10 07:32:19 +02003886 * not created using qdev_new(), it is not attached to the qdev
Peter Xuc8d3ff32017-07-05 16:21:23 +08003887 * device tree, and it is never realized.
3888 *
3889 * TODO: Make this TYPE_OBJECT once QOM provides something like
3890 * TYPE_DEVICE's "-global" properties.
Peter Xu01f6e142017-06-28 15:15:44 +08003891 */
Peter Xue5cb7e72017-06-27 12:10:13 +08003892 .parent = TYPE_DEVICE,
3893 .class_init = migration_class_init,
3894 .class_size = sizeof(MigrationClass),
3895 .instance_size = sizeof(MigrationState),
3896 .instance_init = migration_instance_init,
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003897 .instance_finalize = migration_instance_finalize,
Peter Xue5cb7e72017-06-27 12:10:13 +08003898};
3899
3900static void register_migration_types(void)
3901{
3902 type_register_static(&migration_type);
3903}
3904
3905type_init(register_migration_types);