blob: dbd4afa1e895da32b3625d67aea4b44afe3b8bb3 [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
Jason J. Hernedc325622015-09-08 13:12:37 -040060#define MAX_THROTTLE (32 << 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 Xue5cb7e72017-06-27 12:10:13 +0800146void migration_object_init(void)
147{
Peter Xu4ffdb332017-06-27 12:10:18 +0800148 MachineState *ms = MACHINE(qdev_get_machine());
Peter Xu8b0b29d2017-07-18 11:39:06 +0800149 Error *err = NULL;
Peter Xu4ffdb332017-06-27 12:10:18 +0800150
Peter Xue5cb7e72017-06-27 12:10:13 +0800151 /* This can only be called once. */
152 assert(!current_migration);
153 current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION));
Peter Xu4ffdb332017-06-27 12:10:18 +0800154
Peter Xue1b1b1b2018-05-02 18:47:35 +0800155 /*
156 * Init the migrate incoming object as well no matter whether
157 * we'll use it or not.
158 */
159 assert(!current_incoming);
160 current_incoming = g_new0(MigrationIncomingState, 1);
161 current_incoming->state = MIGRATION_STATUS_NONE;
162 current_incoming->postcopy_remote_fds =
163 g_array_new(FALSE, TRUE, sizeof(struct PostCopyFD));
164 qemu_mutex_init(&current_incoming->rp_mutex);
165 qemu_event_init(&current_incoming->main_thread_load_event, false);
166 qemu_sem_init(&current_incoming->postcopy_pause_sem_dst, 0);
167 qemu_sem_init(&current_incoming->postcopy_pause_sem_fault, 0);
168
Peter Xu8b0b29d2017-07-18 11:39:06 +0800169 if (!migration_object_check(current_migration, &err)) {
170 error_report_err(err);
171 exit(1);
172 }
173
Peter Xu4ffdb332017-06-27 12:10:18 +0800174 /*
175 * We cannot really do this in migration_instance_init() since at
176 * that time global properties are not yet applied, then this
177 * value will be definitely replaced by something else.
178 */
179 if (ms->enforce_config_section) {
180 current_migration->send_configuration = true;
181 }
Peter Xue5cb7e72017-06-27 12:10:13 +0800182}
183
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +0000184void migration_shutdown(void)
Vladimir Sementsov-Ogievskiy1f895602017-12-28 12:16:16 +0300185{
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +0000186 /*
187 * Cancel the current migration - that will (eventually)
188 * stop the migration using this structure
189 */
190 migrate_fd_cancel(current_migration);
Vladimir Sementsov-Ogievskiy1f895602017-12-28 12:16:16 +0300191 object_unref(OBJECT(current_migration));
Vladimir Sementsov-Ogievskiy1499ab02020-07-27 22:42:31 +0300192
193 /*
194 * Cancel outgoing migration of dirty bitmaps. It should
195 * at least unref used block nodes.
196 */
197 dirty_bitmap_mig_cancel_outgoing();
198
199 /*
200 * Cancel incoming migration of dirty bitmaps. Dirty bitmaps
201 * are non-critical data, and their loss never considered as
202 * something serious.
203 */
204 dirty_bitmap_mig_cancel_incoming();
Vladimir Sementsov-Ogievskiy1f895602017-12-28 12:16:16 +0300205}
206
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100207/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +0200208MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +0200209{
Peter Xue5cb7e72017-06-27 12:10:13 +0800210 /* This can only be called after the object created. */
211 assert(current_migration);
212 return current_migration;
Juan Quintela17549e82011-10-05 13:50:43 +0200213}
214
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100215MigrationIncomingState *migration_incoming_get_current(void)
216{
Peter Xue1b1b1b2018-05-02 18:47:35 +0800217 assert(current_incoming);
218 return current_incoming;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100219}
220
221void migration_incoming_state_destroy(void)
222{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100223 struct MigrationIncomingState *mis = migration_incoming_get_current();
224
Peter Xu34826552017-05-19 14:43:29 +0800225 if (mis->to_src_file) {
Peter Xu660819b2017-05-19 14:43:30 +0800226 /* Tell source that we are done */
227 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
Peter Xu34826552017-05-19 14:43:29 +0800228 qemu_fclose(mis->to_src_file);
229 mis->to_src_file = NULL;
230 }
231
Peter Xu660819b2017-05-19 14:43:30 +0800232 if (mis->from_src_file) {
233 qemu_fclose(mis->from_src_file);
234 mis->from_src_file = NULL;
235 }
Dr. David Alan Gilbert00fa4fc2018-03-12 17:21:04 +0000236 if (mis->postcopy_remote_fds) {
237 g_array_free(mis->postcopy_remote_fds, TRUE);
238 mis->postcopy_remote_fds = NULL;
239 }
Peter Xu660819b2017-05-19 14:43:30 +0800240
Dr. David Alan Gilbert5089e182017-08-25 15:19:39 +0100241 qemu_event_reset(&mis->main_thread_load_event);
Juan Quintela9aca82b2019-02-27 11:51:27 +0100242
243 if (mis->socket_address_list) {
244 qapi_free_SocketAddressList(mis->socket_address_list);
245 mis->socket_address_list = NULL;
246 }
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100247}
248
Juan Quintelab05dc722015-07-07 14:44:05 +0200249static void migrate_generate_event(int new_state)
250{
251 if (migrate_use_events()) {
Peter Xu3ab72382018-08-15 21:37:37 +0800252 qapi_event_send_migration(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200253 }
254}
255
Dr. David Alan Gilbert0f073f42018-04-16 18:09:30 +0100256static bool migrate_late_block_activate(void)
257{
258 MigrationState *s;
259
260 s = migrate_get_current();
261
262 return s->enabled_capabilities[
263 MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE];
264}
265
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000266/*
267 * Called on -incoming with a defer: uri.
268 * The migration can be started later after any parameters have been
269 * changed.
270 */
271static void deferred_incoming_migration(Error **errp)
272{
273 if (deferred_incoming) {
274 error_setg(errp, "Incoming migration already deferred");
275 }
276 deferred_incoming = true;
277}
278
Juan Quintelada6f1792017-04-24 17:37:14 +0200279/*
280 * Send a message on the return channel back to the source
281 * of the migration.
282 */
Peter Xud6208e32018-02-08 18:31:12 +0800283static int migrate_send_rp_message(MigrationIncomingState *mis,
284 enum mig_rp_message_type message_type,
285 uint16_t len, void *data)
Juan Quintelada6f1792017-04-24 17:37:14 +0200286{
Peter Xud6208e32018-02-08 18:31:12 +0800287 int ret = 0;
288
Juan Quintelada6f1792017-04-24 17:37:14 +0200289 trace_migrate_send_rp_message((int)message_type, len);
290 qemu_mutex_lock(&mis->rp_mutex);
Peter Xud6208e32018-02-08 18:31:12 +0800291
292 /*
293 * It's possible that the file handle got lost due to network
294 * failures.
295 */
296 if (!mis->to_src_file) {
297 ret = -EIO;
298 goto error;
299 }
300
Juan Quintelada6f1792017-04-24 17:37:14 +0200301 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
302 qemu_put_be16(mis->to_src_file, len);
303 qemu_put_buffer(mis->to_src_file, data, len);
304 qemu_fflush(mis->to_src_file);
Peter Xud6208e32018-02-08 18:31:12 +0800305
306 /* It's possible that qemu file got error during sending */
307 ret = qemu_file_get_error(mis->to_src_file);
308
309error:
Juan Quintelada6f1792017-04-24 17:37:14 +0200310 qemu_mutex_unlock(&mis->rp_mutex);
Peter Xud6208e32018-02-08 18:31:12 +0800311 return ret;
Juan Quintelada6f1792017-04-24 17:37:14 +0200312}
313
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000314/* Request a range of pages from the source VM at the given
315 * start address.
316 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
317 * as the last request (a name must have been given previously)
318 * Start: Address offset within the RB
319 * Len: Length in bytes required - must be a multiple of pagesize
320 */
Peter Xud6208e32018-02-08 18:31:12 +0800321int migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
322 ram_addr_t start, size_t len)
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000323{
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100324 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000325 size_t msglen = 12; /* start + len */
Peter Xud6208e32018-02-08 18:31:12 +0800326 enum mig_rp_message_type msg_type;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000327
328 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
329 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
330
331 if (rbname) {
332 int rbname_len = strlen(rbname);
333 assert(rbname_len < 256);
334
335 bufc[msglen++] = rbname_len;
336 memcpy(bufc + msglen, rbname, rbname_len);
337 msglen += rbname_len;
Peter Xud6208e32018-02-08 18:31:12 +0800338 msg_type = MIG_RP_MSG_REQ_PAGES_ID;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000339 } else {
Peter Xud6208e32018-02-08 18:31:12 +0800340 msg_type = MIG_RP_MSG_REQ_PAGES;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000341 }
Peter Xud6208e32018-02-08 18:31:12 +0800342
343 return migrate_send_rp_message(mis, msg_type, msglen, bufc);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000344}
345
Zhang Chenaad555c2018-09-03 12:38:47 +0800346static bool migration_colo_enabled;
347bool migration_incoming_colo_enabled(void)
348{
349 return migration_colo_enabled;
350}
351
352void migration_incoming_disable_colo(void)
353{
David Hildenbrand18b1d3c2020-06-26 09:22:36 +0200354 ram_block_discard_disable(false);
Zhang Chenaad555c2018-09-03 12:38:47 +0800355 migration_colo_enabled = false;
356}
357
David Hildenbrand18b1d3c2020-06-26 09:22:36 +0200358int migration_incoming_enable_colo(void)
Zhang Chenaad555c2018-09-03 12:38:47 +0800359{
David Hildenbrand18b1d3c2020-06-26 09:22:36 +0200360 if (ram_block_discard_disable(true)) {
361 error_report("COLO: cannot disable RAM discard");
362 return -EBUSY;
363 }
Zhang Chenaad555c2018-09-03 12:38:47 +0800364 migration_colo_enabled = true;
David Hildenbrand18b1d3c2020-06-26 09:22:36 +0200365 return 0;
Zhang Chenaad555c2018-09-03 12:38:47 +0800366}
367
Juan Quintela9aca82b2019-02-27 11:51:27 +0100368void migrate_add_address(SocketAddress *address)
369{
370 MigrationIncomingState *mis = migration_incoming_get_current();
371 SocketAddressList *addrs;
372
373 addrs = g_new0(SocketAddressList, 1);
374 addrs->next = mis->socket_address_list;
375 mis->socket_address_list = addrs;
376 addrs->value = QAPI_CLONE(SocketAddress, address);
377}
378
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200379void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000380{
aliguori34c9dd82008-10-13 03:14:31 +0000381 const char *p;
382
Peter Xu3ab72382018-08-15 21:37:37 +0800383 qapi_event_send_migration(MIGRATION_STATUS_SETUP);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000384 if (!strcmp(uri, "defer")) {
385 deferred_incoming_migration(errp);
386 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200387 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400388#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000389 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400390 rdma_start_incoming_migration(p, errp);
391#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000392 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200393 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000394 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200395 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000396 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200397 fd_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000398 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100399 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200400 }
aliguori5bb79102008-10-13 03:12:02 +0000401}
402
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300403static void process_incoming_migration_bh(void *opaque)
404{
405 Error *local_err = NULL;
406 MigrationIncomingState *mis = opaque;
407
Dr. David Alan Gilbert0f073f42018-04-16 18:09:30 +0100408 /* If capability late_block_activate is set:
409 * Only fire up the block code now if we're going to restart the
410 * VM, else 'cont' will do it.
411 * This causes file locking to happen; so we don't want it to happen
412 * unless we really are starting the VM.
413 */
414 if (!migrate_late_block_activate() ||
415 (autostart && (!global_state_received() ||
416 global_state_get_runstate() == RUN_STATE_RUNNING))) {
417 /* Make sure all file formats flush their mutable metadata.
418 * If we get an error here, just don't restart the VM yet. */
419 bdrv_invalidate_cache_all(&local_err);
420 if (local_err) {
421 error_report_err(local_err);
422 local_err = NULL;
423 autostart = false;
424 }
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200425 }
426
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300427 /*
428 * This must happen after all error conditions are dealt with and
429 * we're sure the VM is going to be running on this host.
430 */
Dr. David Alan Gilbert76595052019-02-27 13:24:08 +0000431 qemu_announce_self(&mis->announce_timer, migrate_announce_params());
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300432
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100433 if (multifd_load_cleanup(&local_err) != 0) {
434 error_report_err(local_err);
435 autostart = false;
436 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300437 /* If global state section was not received or we are in running
438 state, we need to obey autostart. Any other state is set with
439 runstate_set. */
440
Vladimir Sementsov-Ogievskiyb35ebdf2018-03-13 15:34:01 -0400441 dirty_bitmap_mig_before_vm_start();
442
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300443 if (!global_state_received() ||
444 global_state_get_runstate() == RUN_STATE_RUNNING) {
445 if (autostart) {
446 vm_start();
447 } else {
448 runstate_set(RUN_STATE_PAUSED);
449 }
Zhang Chendb009722019-03-03 22:50:17 +0800450 } else if (migration_incoming_colo_enabled()) {
451 migration_incoming_disable_colo();
452 vm_start();
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300453 } else {
454 runstate_set(global_state_get_runstate());
455 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300456 /*
457 * This must happen after any state changes since as soon as an external
458 * observer sees this event they might start to prod at the VM assuming
459 * it's ready to use.
460 */
461 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
462 MIGRATION_STATUS_COMPLETED);
463 qemu_bh_delete(mis->bh);
464 migration_incoming_state_destroy();
465}
466
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200467static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200468{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100469 MigrationIncomingState *mis = migration_incoming_get_current();
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000470 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200471 int ret;
Zhang Chen8e48ac92018-09-03 12:38:46 +0800472 Error *local_err = NULL;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200473
Juan Quintela4f0fae72017-07-24 12:42:02 +0200474 assert(mis->from_src_file);
Lidong Chen92370982018-08-06 21:29:35 +0800475 mis->migration_incoming_co = qemu_coroutine_self();
Dr. David Alan Gilbert67f11b52017-02-24 18:28:34 +0000476 mis->largest_page_size = qemu_ram_pagesize_largest();
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000477 postcopy_state_set(POSTCOPY_INCOMING_NONE);
zhanghailiang93d7af62015-12-16 11:47:34 +0000478 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
479 MIGRATION_STATUS_ACTIVE);
Juan Quintela4f0fae72017-07-24 12:42:02 +0200480 ret = qemu_loadvm_state(mis->from_src_file);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100481
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000482 ps = postcopy_state_get();
483 trace_process_incoming_migration_co_end(ret, ps);
484 if (ps != POSTCOPY_INCOMING_NONE) {
485 if (ps == POSTCOPY_INCOMING_ADVISE) {
486 /*
487 * Where a migration had postcopy enabled (and thus went to advise)
488 * but managed to complete within the precopy period, we can use
489 * the normal exit.
490 */
491 postcopy_ram_incoming_cleanup(mis);
492 } else if (ret >= 0) {
493 /*
494 * Postcopy was started, cleanup should happen at the end of the
495 * postcopy thread.
496 */
497 trace_process_incoming_migration_co_postcopy_end_main();
498 return;
499 }
500 /* Else if something went wrong then just fall out of the normal exit */
501 }
502
zhanghailiang25d0c162016-10-27 14:42:55 +0800503 /* we get COLO info, and know if we are in COLO mode */
Zhang Chenaad555c2018-09-03 12:38:47 +0800504 if (!ret && migration_incoming_colo_enabled()) {
Zhang Chen8e48ac92018-09-03 12:38:46 +0800505 /* Make sure all file formats flush their mutable metadata */
506 bdrv_invalidate_cache_all(&local_err);
507 if (local_err) {
Zhang Chen8e48ac92018-09-03 12:38:46 +0800508 error_report_err(local_err);
Fei Li6d99c2d2019-01-13 22:08:49 +0800509 goto fail;
Zhang Chen8e48ac92018-09-03 12:38:46 +0800510 }
511
zhanghailiang25d0c162016-10-27 14:42:55 +0800512 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
513 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
514 mis->have_colo_incoming_thread = true;
515 qemu_coroutine_yield();
516
517 /* Wait checkpoint incoming thread exit before free resource */
518 qemu_thread_join(&mis->colo_incoming_thread);
Zhang Chen13af18f2018-09-03 12:38:48 +0800519 /* We hold the global iothread lock, so it is safe here */
520 colo_release_ram_cache();
zhanghailiang25d0c162016-10-27 14:42:55 +0800521 }
522
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200523 if (ret < 0) {
Peter Lievendb80fac2014-06-10 11:29:16 +0200524 error_report("load of migration failed: %s", strerror(-ret));
Fei Li6d99c2d2019-01-13 22:08:49 +0800525 goto fail;
Juan Quintela511c0232010-06-09 14:10:55 +0200526 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300527 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
528 qemu_bh_schedule(mis->bh);
Lidong Chen92370982018-08-06 21:29:35 +0800529 mis->migration_incoming_co = NULL;
Fei Li6d99c2d2019-01-13 22:08:49 +0800530 return;
531fail:
532 local_err = NULL;
533 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
534 MIGRATION_STATUS_FAILED);
535 qemu_fclose(mis->from_src_file);
536 if (multifd_load_cleanup(&local_err) != 0) {
537 error_report_err(local_err);
538 }
539 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200540}
541
Juan Quintelab673eab2019-06-12 11:44:19 +0200542/**
543 * @migration_incoming_setup: Setup incoming migration
544 *
545 * Returns 0 for no error or 1 for error
546 *
547 * @f: file for main migration channel
548 * @errp: where to put errors
549 */
550static int migration_incoming_setup(QEMUFile *f, Error **errp)
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200551{
Juan Quintela4f0fae72017-07-24 12:42:02 +0200552 MigrationIncomingState *mis = migration_incoming_get_current();
Juan Quintelab673eab2019-06-12 11:44:19 +0200553 Error *local_err = NULL;
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200554
Juan Quintelab673eab2019-06-12 11:44:19 +0200555 if (multifd_load_setup(&local_err) != 0) {
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100556 /* We haven't been able to create multifd threads
557 nothing better to do */
Juan Quintelab673eab2019-06-12 11:44:19 +0200558 error_report_err(local_err);
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100559 exit(EXIT_FAILURE);
560 }
561
Juan Quintela4f0fae72017-07-24 12:42:02 +0200562 if (!mis->from_src_file) {
563 mis->from_src_file = f;
564 }
Daniel P. Berrange06ad5132016-04-27 11:04:56 +0100565 qemu_file_set_blocking(f, false);
Juan Quintelab673eab2019-06-12 11:44:19 +0200566 return 0;
Juan Quintelae595a012017-07-17 12:30:25 +0200567}
568
Juan Quintela36c2f8b2018-03-07 08:40:52 +0100569void migration_incoming_process(void)
Juan Quintelae595a012017-07-17 12:30:25 +0200570{
571 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL);
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200572 qemu_coroutine_enter(co);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200573}
574
Peter Xu884835f2018-06-27 21:22:45 +0800575/* Returns true if recovered from a paused migration, otherwise false */
576static bool postcopy_try_recover(QEMUFile *f)
Juan Quintelae595a012017-07-17 12:30:25 +0200577{
Peter Xud96c9e82018-05-02 18:47:26 +0800578 MigrationIncomingState *mis = migration_incoming_get_current();
579
580 if (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
581 /* Resumed from a paused postcopy migration */
582
583 mis->from_src_file = f;
584 /* Postcopy has standalone thread to do vm load */
585 qemu_file_set_blocking(f, true);
586
587 /* Re-configure the return path */
588 mis->to_src_file = qemu_file_get_return_path(f);
589
590 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
591 MIGRATION_STATUS_POSTCOPY_RECOVER);
592
593 /*
594 * Here, we only wake up the main loading thread (while the
595 * fault thread will still be waiting), so that we can receive
596 * commands from source now, and answer it if needed. The
597 * fault thread will be woken up afterwards until we are sure
598 * that source is ready to reply to page requests.
599 */
600 qemu_sem_post(&mis->postcopy_pause_sem_dst);
Peter Xu884835f2018-06-27 21:22:45 +0800601 return true;
Peter Xud96c9e82018-05-02 18:47:26 +0800602 }
Peter Xu884835f2018-06-27 21:22:45 +0800603
604 return false;
605}
606
Juan Quintelab673eab2019-06-12 11:44:19 +0200607void migration_fd_process_incoming(QEMUFile *f, Error **errp)
Peter Xu884835f2018-06-27 21:22:45 +0800608{
Juan Quintelab673eab2019-06-12 11:44:19 +0200609 Error *local_err = NULL;
610
Peter Xu884835f2018-06-27 21:22:45 +0800611 if (postcopy_try_recover(f)) {
612 return;
613 }
614
Juan Quintelab673eab2019-06-12 11:44:19 +0200615 if (migration_incoming_setup(f, &local_err)) {
616 if (local_err) {
617 error_propagate(errp, local_err);
618 }
619 return;
620 }
Peter Xu884835f2018-06-27 21:22:45 +0800621 migration_incoming_process();
Juan Quintelae595a012017-07-17 12:30:25 +0200622}
623
Fei Li49ed0d22019-01-13 22:08:46 +0800624void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp)
Juan Quintela4f0fae72017-07-24 12:42:02 +0200625{
626 MigrationIncomingState *mis = migration_incoming_get_current();
Juan Quintelab673eab2019-06-12 11:44:19 +0200627 Error *local_err = NULL;
Peter Xua429e7f2018-06-27 21:22:46 +0800628 bool start_migration;
Juan Quintela4f0fae72017-07-24 12:42:02 +0200629
630 if (!mis->from_src_file) {
Peter Xua429e7f2018-06-27 21:22:46 +0800631 /* The first connection (multifd may have multiple) */
Juan Quintela4f0fae72017-07-24 12:42:02 +0200632 QEMUFile *f = qemu_fopen_channel_input(ioc);
Peter Xua429e7f2018-06-27 21:22:46 +0800633
634 /* If it's a recovery, we're done */
Peter Xu884835f2018-06-27 21:22:45 +0800635 if (postcopy_try_recover(f)) {
636 return;
637 }
Peter Xua429e7f2018-06-27 21:22:46 +0800638
Juan Quintelab673eab2019-06-12 11:44:19 +0200639 if (migration_incoming_setup(f, &local_err)) {
640 if (local_err) {
641 error_propagate(errp, local_err);
642 }
643 return;
644 }
Peter Xua429e7f2018-06-27 21:22:46 +0800645
646 /*
647 * Common migration only needs one channel, so we can start
648 * right now. Multifd needs more than one channel, we wait.
649 */
650 start_migration = !migrate_use_multifd();
651 } else {
652 /* Multiple connections */
653 assert(migrate_use_multifd());
Fei Li49ed0d22019-01-13 22:08:46 +0800654 start_migration = multifd_recv_new_channel(ioc, &local_err);
655 if (local_err) {
656 error_propagate(errp, local_err);
657 return;
658 }
Juan Quintela4f0fae72017-07-24 12:42:02 +0200659 }
Peter Xu81e62052018-06-27 21:22:44 +0800660
Peter Xua429e7f2018-06-27 21:22:46 +0800661 if (start_migration) {
Peter Xu81e62052018-06-27 21:22:44 +0800662 migration_incoming_process();
663 }
Juan Quintela4f0fae72017-07-24 12:42:02 +0200664}
665
Juan Quintela428d8902017-07-24 13:06:25 +0200666/**
667 * @migration_has_all_channels: We have received all channels that we need
668 *
669 * Returns true when we have got connections to all the channels that
670 * we need for migration.
671 */
672bool migration_has_all_channels(void)
673{
Daniel P. Berrangéca273df2018-06-19 17:35:52 +0100674 MigrationIncomingState *mis = migration_incoming_get_current();
Juan Quintela62c1e0c2018-02-19 18:59:02 +0100675 bool all_channels;
676
677 all_channels = multifd_recv_all_channels_created();
678
Daniel P. Berrangéca273df2018-06-19 17:35:52 +0100679 return all_channels && mis->from_src_file != NULL;
Juan Quintela428d8902017-07-24 13:06:25 +0200680}
681
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000682/*
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000683 * Send a 'SHUT' message on the return channel with the given value
684 * to indicate that we've finished with the RP. Non-0 value indicates
685 * error.
686 */
687void migrate_send_rp_shut(MigrationIncomingState *mis,
688 uint32_t value)
689{
690 uint32_t buf;
691
692 buf = cpu_to_be32(value);
693 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
694}
695
696/*
697 * Send a 'PONG' message on the return channel with the given value
698 * (normally in response to a 'PING')
699 */
700void migrate_send_rp_pong(MigrationIncomingState *mis,
701 uint32_t value)
702{
703 uint32_t buf;
704
705 buf = cpu_to_be32(value);
706 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
707}
708
Peter Xua335deb2018-05-02 18:47:28 +0800709void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
710 char *block_name)
711{
712 char buf[512];
713 int len;
714 int64_t res;
715
716 /*
717 * First, we send the header part. It contains only the len of
718 * idstr, and the idstr itself.
719 */
720 len = strlen(block_name);
721 buf[0] = len;
722 memcpy(buf + 1, block_name, len);
723
724 if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
725 error_report("%s: MSG_RP_RECV_BITMAP only used for recovery",
726 __func__);
727 return;
728 }
729
730 migrate_send_rp_message(mis, MIG_RP_MSG_RECV_BITMAP, len + 1, buf);
731
732 /*
733 * Next, we dump the received bitmap to the stream.
734 *
735 * TODO: currently we are safe since we are the only one that is
736 * using the to_src_file handle (fault thread is still paused),
737 * and it's ok even not taking the mutex. However the best way is
738 * to take the lock before sending the message header, and release
739 * the lock after sending the bitmap.
740 */
741 qemu_mutex_lock(&mis->rp_mutex);
742 res = ramblock_recv_bitmap_send(mis->to_src_file, block_name);
743 qemu_mutex_unlock(&mis->rp_mutex);
744
745 trace_migrate_send_rp_recv_bitmap(block_name, res);
746}
747
Peter Xu13955b82018-05-02 18:47:30 +0800748void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value)
749{
750 uint32_t buf;
751
752 buf = cpu_to_be32(value);
753 migrate_send_rp_message(mis, MIG_RP_MSG_RESUME_ACK, sizeof(buf), &buf);
754}
755
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300756MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
757{
758 MigrationCapabilityStatusList *head = NULL;
759 MigrationCapabilityStatusList *caps;
760 MigrationState *s = migrate_get_current();
761 int i;
762
Michael Tokarev387eede2013-10-05 13:18:28 +0400763 caps = NULL; /* silence compiler warning */
Eric Blake7fb1cf12015-11-18 01:52:57 -0700764 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Dr. David Alan Gilberted1701c2017-05-15 15:05:29 +0100765#ifndef CONFIG_LIVE_BLOCK_MIGRATION
766 if (i == MIGRATION_CAPABILITY_BLOCK) {
767 continue;
768 }
769#endif
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300770 if (head == NULL) {
771 head = g_malloc0(sizeof(*caps));
772 caps = head;
773 } else {
774 caps->next = g_malloc0(sizeof(*caps));
775 caps = caps->next;
776 }
777 caps->value =
778 g_malloc(sizeof(*caps->value));
779 caps->value->capability = i;
780 caps->value->state = s->enabled_capabilities[i];
781 }
782
783 return head;
784}
785
Liang Li85de8322015-03-23 16:32:28 +0800786MigrationParameters *qmp_query_migrate_parameters(Error **errp)
787{
788 MigrationParameters *params;
789 MigrationState *s = migrate_get_current();
790
Markus Armbrustere87fae42017-07-18 12:57:38 +0200791 /* TODO use QAPI_CLONE() instead of duplicating it inline */
Liang Li85de8322015-03-23 16:32:28 +0800792 params = g_malloc0(sizeof(*params));
Eric Blakede63ab62016-09-08 22:14:15 -0500793 params->has_compress_level = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100794 params->compress_level = s->parameters.compress_level;
Eric Blakede63ab62016-09-08 22:14:15 -0500795 params->has_compress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100796 params->compress_threads = s->parameters.compress_threads;
Xiao Guangrong1d588722018-08-21 16:10:20 +0800797 params->has_compress_wait_thread = true;
798 params->compress_wait_thread = s->parameters.compress_wait_thread;
Eric Blakede63ab62016-09-08 22:14:15 -0500799 params->has_decompress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100800 params->decompress_threads = s->parameters.decompress_threads;
Keqian Zhudc14a472020-02-24 10:31:42 +0800801 params->has_throttle_trigger_threshold = true;
802 params->throttle_trigger_threshold = s->parameters.throttle_trigger_threshold;
Eric Blakede63ab62016-09-08 22:14:15 -0500803 params->has_cpu_throttle_initial = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100804 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
Eric Blakede63ab62016-09-08 22:14:15 -0500805 params->has_cpu_throttle_increment = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100806 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
Keqian Zhucbbf8182020-04-13 18:15:08 +0800807 params->has_cpu_throttle_tailslow = true;
808 params->cpu_throttle_tailslow = s->parameters.cpu_throttle_tailslow;
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200809 params->has_tls_creds = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100810 params->tls_creds = g_strdup(s->parameters.tls_creds);
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200811 params->has_tls_hostname = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100812 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
Daniel P. Berranged2f1d292019-02-27 14:53:24 +0000813 params->has_tls_authz = true;
Mao Zhongyi7cd75cb2020-03-25 09:49:30 +0800814 params->tls_authz = g_strdup(s->parameters.tls_authz ?
815 s->parameters.tls_authz : "");
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530816 params->has_max_bandwidth = true;
817 params->max_bandwidth = s->parameters.max_bandwidth;
818 params->has_downtime_limit = true;
819 params->downtime_limit = s->parameters.downtime_limit;
zhanghailiangfe39a4d2016-11-02 15:42:09 +0800820 params->has_x_checkpoint_delay = true;
zhanghailiang68b53592016-10-27 14:43:01 +0800821 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
Juan Quintela2833c592017-04-05 18:32:37 +0200822 params->has_block_incremental = true;
823 params->block_incremental = s->parameters.block_incremental;
Juan Quintelacbfd6c92019-02-06 13:54:06 +0100824 params->has_multifd_channels = true;
825 params->multifd_channels = s->parameters.multifd_channels;
Juan Quintela96eef042019-01-16 10:35:55 +0100826 params->has_multifd_compression = true;
827 params->multifd_compression = s->parameters.multifd_compression;
Juan Quintela9004db42020-01-23 17:08:52 +0100828 params->has_multifd_zlib_level = true;
829 params->multifd_zlib_level = s->parameters.multifd_zlib_level;
Juan Quintela6a9ad152020-01-23 17:41:36 +0100830 params->has_multifd_zstd_level = true;
831 params->multifd_zstd_level = s->parameters.multifd_zstd_level;
Juan Quintela73af8dd2017-10-05 21:30:10 +0200832 params->has_xbzrle_cache_size = true;
833 params->xbzrle_cache_size = s->parameters.xbzrle_cache_size;
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +0100834 params->has_max_postcopy_bandwidth = true;
835 params->max_postcopy_bandwidth = s->parameters.max_postcopy_bandwidth;
Li Qiang4cbc9c72018-08-01 06:00:20 -0700836 params->has_max_cpu_throttle = true;
837 params->max_cpu_throttle = s->parameters.max_cpu_throttle;
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +0000838 params->has_announce_initial = true;
839 params->announce_initial = s->parameters.announce_initial;
840 params->has_announce_max = true;
841 params->announce_max = s->parameters.announce_max;
842 params->has_announce_rounds = true;
843 params->announce_rounds = s->parameters.announce_rounds;
844 params->has_announce_step = true;
845 params->announce_step = s->parameters.announce_step;
Liang Li85de8322015-03-23 16:32:28 +0800846
Max Reitz31e4c352020-08-20 17:07:23 +0200847 if (s->parameters.has_block_bitmap_mapping) {
848 params->has_block_bitmap_mapping = true;
849 params->block_bitmap_mapping =
850 QAPI_CLONE(BitmapMigrationNodeAliasList,
851 s->parameters.block_bitmap_mapping);
852 }
853
Liang Li85de8322015-03-23 16:32:28 +0800854 return params;
855}
856
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +0000857AnnounceParameters *migrate_announce_params(void)
858{
859 static AnnounceParameters ap;
860
861 MigrationState *s = migrate_get_current();
862
863 ap.initial = s->parameters.announce_initial;
864 ap.max = s->parameters.announce_max;
865 ap.rounds = s->parameters.announce_rounds;
866 ap.step = s->parameters.announce_step;
867
868 return &ap;
869}
870
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000871/*
872 * Return true if we're already in the middle of a migration
873 * (i.e. any of the active or setup states)
874 */
Jia Lina3d63da12018-10-26 16:36:20 +0800875bool migration_is_setup_or_active(int state)
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000876{
877 switch (state) {
878 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000879 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Peter Xua688d2c2018-05-02 18:47:18 +0800880 case MIGRATION_STATUS_POSTCOPY_PAUSED:
Peter Xu135b87b2018-05-02 18:47:25 +0800881 case MIGRATION_STATUS_POSTCOPY_RECOVER:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000882 case MIGRATION_STATUS_SETUP:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +0100883 case MIGRATION_STATUS_PRE_SWITCHOVER:
884 case MIGRATION_STATUS_DEVICE:
Jens Freimannc7e0acd2019-10-29 12:49:02 +0100885 case MIGRATION_STATUS_WAIT_UNPLUG:
zhanghailiang19dd4082020-02-24 14:54:12 +0800886 case MIGRATION_STATUS_COLO:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000887 return true;
888
889 default:
890 return false;
891
892 }
893}
894
Juan Quintela392d87e2020-01-21 15:39:23 +0100895bool migration_is_running(int state)
896{
897 switch (state) {
898 case MIGRATION_STATUS_ACTIVE:
899 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
900 case MIGRATION_STATUS_POSTCOPY_PAUSED:
901 case MIGRATION_STATUS_POSTCOPY_RECOVER:
902 case MIGRATION_STATUS_SETUP:
903 case MIGRATION_STATUS_PRE_SWITCHOVER:
904 case MIGRATION_STATUS_DEVICE:
905 case MIGRATION_STATUS_WAIT_UNPLUG:
906 case MIGRATION_STATUS_CANCELLING:
Juan Quintela392d87e2020-01-21 15:39:23 +0100907 return true;
908
909 default:
910 return false;
911
912 }
913}
914
Wei Yang640dfb12019-07-16 08:54:11 +0800915static void populate_time_info(MigrationInfo *info, MigrationState *s)
916{
917 info->has_status = true;
918 info->has_setup_time = true;
919 info->setup_time = s->setup_time;
920 if (s->state == MIGRATION_STATUS_COMPLETED) {
921 info->has_total_time = true;
922 info->total_time = s->total_time;
923 info->has_downtime = true;
924 info->downtime = s->downtime;
925 } else {
926 info->has_total_time = true;
927 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
928 s->start_time;
929 info->has_expected_downtime = true;
930 info->expected_downtime = s->expected_downtime;
931 }
932}
933
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100934static void populate_ram_info(MigrationInfo *info, MigrationState *s)
935{
936 info->has_ram = true;
937 info->ram = g_malloc0(sizeof(*info->ram));
Juan Quintela93604472017-06-06 19:49:03 +0200938 info->ram->transferred = ram_counters.transferred;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100939 info->ram->total = ram_bytes_total();
Juan Quintela93604472017-06-06 19:49:03 +0200940 info->ram->duplicate = ram_counters.duplicate;
Juan Quintelabedf53c2017-03-13 20:35:54 +0100941 /* legacy value. It is not used anymore */
942 info->ram->skipped = 0;
Juan Quintela93604472017-06-06 19:49:03 +0200943 info->ram->normal = ram_counters.normal;
944 info->ram->normal_bytes = ram_counters.normal *
Juan Quintela20afaed2017-03-21 09:09:14 +0100945 qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100946 info->ram->mbps = s->mbps;
Juan Quintela93604472017-06-06 19:49:03 +0200947 info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
948 info->ram->postcopy_requests = ram_counters.postcopy_requests;
Chao Fan030ce1f2017-03-21 10:22:43 +0800949 info->ram->page_size = qemu_target_page_size();
Juan Quintelaa61c45b2018-06-26 15:20:11 +0200950 info->ram->multifd_bytes = ram_counters.multifd_bytes;
Xiao Guangrongaecbfe92019-01-11 14:37:30 +0800951 info->ram->pages_per_second = s->pages_per_second;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100952
Juan Quintela114f5ae2017-05-04 10:09:21 +0200953 if (migrate_use_xbzrle()) {
954 info->has_xbzrle_cache = true;
955 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
956 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
Juan Quintela93604472017-06-06 19:49:03 +0200957 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
958 info->xbzrle_cache->pages = xbzrle_counters.pages;
959 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
960 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
Wei Wange460a4b2020-04-30 08:59:35 +0800961 info->xbzrle_cache->encoding_rate = xbzrle_counters.encoding_rate;
Juan Quintela93604472017-06-06 19:49:03 +0200962 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
Juan Quintela114f5ae2017-05-04 10:09:21 +0200963 }
964
Xiao Guangrong76e03002018-09-06 15:01:00 +0800965 if (migrate_use_compression()) {
966 info->has_compression = true;
967 info->compression = g_malloc0(sizeof(*info->compression));
968 info->compression->pages = compression_counters.pages;
969 info->compression->busy = compression_counters.busy;
970 info->compression->busy_rate = compression_counters.busy_rate;
971 info->compression->compressed_size =
972 compression_counters.compressed_size;
973 info->compression->compression_rate =
974 compression_counters.compression_rate;
975 }
976
Juan Quintela338182c2017-05-03 13:16:38 +0200977 if (cpu_throttle_active()) {
978 info->has_cpu_throttle_percentage = true;
979 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
980 }
981
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100982 if (s->state != MIGRATION_STATUS_COMPLETED) {
983 info->ram->remaining = ram_bytes_remaining();
Juan Quintela93604472017-06-06 19:49:03 +0200984 info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100985 }
986}
987
Juan Quintela930ac042017-05-04 10:21:46 +0200988static void populate_disk_info(MigrationInfo *info)
989{
990 if (blk_mig_active()) {
991 info->has_disk = true;
992 info->disk = g_malloc0(sizeof(*info->disk));
993 info->disk->transferred = blk_mig_bytes_transferred();
994 info->disk->remaining = blk_mig_bytes_remaining();
995 info->disk->total = blk_mig_bytes_total();
996 }
997}
998
Alexey Perevalov65ace062018-03-22 21:17:27 +0300999static void fill_source_migration_info(MigrationInfo *info)
aliguori5bb79102008-10-13 03:12:02 +00001000{
Juan Quintela17549e82011-10-05 13:50:43 +02001001 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +00001002
Juan Quintela17549e82011-10-05 13:50:43 +02001003 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +08001004 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +02001005 /* no migration has happened ever */
Alexey Perevalov65ace062018-03-22 21:17:27 +03001006 /* do not overwrite destination migration status */
1007 return;
zhanghailiang31194732015-03-13 16:08:38 +08001008 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001009 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -04001010 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001011 break;
zhanghailiang31194732015-03-13 16:08:38 +08001012 case MIGRATION_STATUS_ACTIVE:
1013 case MIGRATION_STATUS_CANCELLING:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001014 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +01001015 case MIGRATION_STATUS_PRE_SWITCHOVER:
1016 case MIGRATION_STATUS_DEVICE:
Peter Xua688d2c2018-05-02 18:47:18 +08001017 case MIGRATION_STATUS_POSTCOPY_PAUSED:
Peter Xu135b87b2018-05-02 18:47:25 +08001018 case MIGRATION_STATUS_POSTCOPY_RECOVER:
Wei Yang640dfb12019-07-16 08:54:11 +08001019 /* TODO add some postcopy stats */
1020 populate_time_info(info, s);
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +01001021 populate_ram_info(info, s);
Juan Quintela930ac042017-05-04 10:21:46 +02001022 populate_disk_info(info);
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001023 break;
zhanghailiang0b827d52016-10-27 14:42:54 +08001024 case MIGRATION_STATUS_COLO:
1025 info->has_status = true;
1026 /* TODO: display COLO specific information (checkpoint info etc.) */
1027 break;
zhanghailiang31194732015-03-13 16:08:38 +08001028 case MIGRATION_STATUS_COMPLETED:
Wei Yang640dfb12019-07-16 08:54:11 +08001029 populate_time_info(info, s);
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +01001030 populate_ram_info(info, s);
Juan Quintela17549e82011-10-05 13:50:43 +02001031 break;
zhanghailiang31194732015-03-13 16:08:38 +08001032 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -03001033 info->has_status = true;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001034 if (s->error) {
1035 info->has_error_desc = true;
1036 info->error_desc = g_strdup(error_get_pretty(s->error));
1037 }
Juan Quintela17549e82011-10-05 13:50:43 +02001038 break;
zhanghailiang31194732015-03-13 16:08:38 +08001039 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -03001040 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +02001041 break;
Jens Freimannc7e0acd2019-10-29 12:49:02 +01001042 case MIGRATION_STATUS_WAIT_UNPLUG:
1043 info->has_status = true;
1044 break;
aliguori5bb79102008-10-13 03:12:02 +00001045 }
zhanghailiangcde63fb2015-03-13 16:08:41 +08001046 info->status = s->state;
aliguori5bb79102008-10-13 03:12:02 +00001047}
1048
Peter Xu4a842142017-07-18 11:39:08 +08001049/**
1050 * @migration_caps_check - check capability validity
1051 *
1052 * @cap_list: old capability list, array of bool
1053 * @params: new capabilities to be applied soon
1054 * @errp: set *errp if the check failed, with reason
1055 *
1056 * Returns true if check passed, otherwise false.
1057 */
1058static bool migrate_caps_check(bool *cap_list,
1059 MigrationCapabilityStatusList *params,
1060 Error **errp)
Orit Wasserman00458432012-08-06 21:42:48 +03001061{
Orit Wasserman00458432012-08-06 21:42:48 +03001062 MigrationCapabilityStatusList *cap;
Peter Xu4a842142017-07-18 11:39:08 +08001063 bool old_postcopy_cap;
Alexey Perevalovd7651f12017-09-19 19:47:56 +03001064 MigrationIncomingState *mis = migration_incoming_get_current();
Orit Wasserman00458432012-08-06 21:42:48 +03001065
Peter Xu4a842142017-07-18 11:39:08 +08001066 old_postcopy_cap = cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Orit Wasserman00458432012-08-06 21:42:48 +03001067
1068 for (cap = params; cap; cap = cap->next) {
Peter Xu4a842142017-07-18 11:39:08 +08001069 cap_list[cap->value->capability] = cap->value->state;
Orit Wasserman00458432012-08-06 21:42:48 +03001070 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001071
Peter Xu4a842142017-07-18 11:39:08 +08001072#ifndef CONFIG_LIVE_BLOCK_MIGRATION
1073 if (cap_list[MIGRATION_CAPABILITY_BLOCK]) {
1074 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
1075 "block migration");
1076 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
1077 return false;
1078 }
1079#endif
1080
Zhang Chen7e934f52018-11-15 03:09:12 +08001081#ifndef CONFIG_REPLICATION
1082 if (cap_list[MIGRATION_CAPABILITY_X_COLO]) {
1083 error_setg(errp, "QEMU compiled without replication module"
1084 " can't enable COLO");
1085 error_append_hint(errp, "Please enable replication before COLO.\n");
1086 return false;
1087 }
1088#endif
1089
Peter Xu4a842142017-07-18 11:39:08 +08001090 if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +01001091 /* This check is reasonably expensive, so only when it's being
1092 * set the first time, also it's only the destination that needs
1093 * special support.
1094 */
1095 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
Alexey Perevalovd7651f12017-09-19 19:47:56 +03001096 !postcopy_ram_supported_by_host(mis)) {
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +01001097 /* postcopy_ram_supported_by_host will have emitted a more
1098 * detailed message
1099 */
Peter Xu4a842142017-07-18 11:39:08 +08001100 error_setg(errp, "Postcopy is not supported");
1101 return false;
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +01001102 }
Yury Kotov18269062019-02-15 20:45:45 +03001103
1104 if (cap_list[MIGRATION_CAPABILITY_X_IGNORE_SHARED]) {
1105 error_setg(errp, "Postcopy is not compatible with ignore-shared");
1106 return false;
1107 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001108 }
Peter Xu4a842142017-07-18 11:39:08 +08001109
1110 return true;
1111}
1112
Alexey Perevalov65ace062018-03-22 21:17:27 +03001113static void fill_destination_migration_info(MigrationInfo *info)
1114{
1115 MigrationIncomingState *mis = migration_incoming_get_current();
1116
Juan Quintela9aca82b2019-02-27 11:51:27 +01001117 if (mis->socket_address_list) {
1118 info->has_socket_address = true;
1119 info->socket_address =
1120 QAPI_CLONE(SocketAddressList, mis->socket_address_list);
1121 }
1122
Alexey Perevalov65ace062018-03-22 21:17:27 +03001123 switch (mis->state) {
1124 case MIGRATION_STATUS_NONE:
1125 return;
Alexey Perevalov65ace062018-03-22 21:17:27 +03001126 case MIGRATION_STATUS_SETUP:
1127 case MIGRATION_STATUS_CANCELLING:
1128 case MIGRATION_STATUS_CANCELLED:
1129 case MIGRATION_STATUS_ACTIVE:
1130 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Peter Xu3c9928d2018-07-10 17:18:56 +08001131 case MIGRATION_STATUS_POSTCOPY_PAUSED:
1132 case MIGRATION_STATUS_POSTCOPY_RECOVER:
Alexey Perevalov65ace062018-03-22 21:17:27 +03001133 case MIGRATION_STATUS_FAILED:
1134 case MIGRATION_STATUS_COLO:
1135 info->has_status = true;
1136 break;
1137 case MIGRATION_STATUS_COMPLETED:
1138 info->has_status = true;
1139 fill_destination_postcopy_migration_info(info);
1140 break;
1141 }
1142 info->status = mis->state;
1143}
1144
1145MigrationInfo *qmp_query_migrate(Error **errp)
1146{
1147 MigrationInfo *info = g_malloc0(sizeof(*info));
1148
1149 fill_destination_migration_info(info);
1150 fill_source_migration_info(info);
1151
1152 return info;
1153}
1154
Peter Xu4a842142017-07-18 11:39:08 +08001155void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
1156 Error **errp)
1157{
1158 MigrationState *s = migrate_get_current();
1159 MigrationCapabilityStatusList *cap;
Peter Xudd0ee302018-03-05 17:49:38 +08001160 bool cap_list[MIGRATION_CAPABILITY__MAX];
Peter Xu4a842142017-07-18 11:39:08 +08001161
Juan Quintela392d87e2020-01-21 15:39:23 +01001162 if (migration_is_running(s->state)) {
Peter Xu4a842142017-07-18 11:39:08 +08001163 error_setg(errp, QERR_MIGRATION_ACTIVE);
1164 return;
1165 }
1166
Peter Xudd0ee302018-03-05 17:49:38 +08001167 memcpy(cap_list, s->enabled_capabilities, sizeof(cap_list));
1168 if (!migrate_caps_check(cap_list, params, errp)) {
Peter Xu4a842142017-07-18 11:39:08 +08001169 return;
1170 }
1171
1172 for (cap = params; cap; cap = cap->next) {
1173 s->enabled_capabilities[cap->value->capability] = cap->value->state;
1174 }
Orit Wasserman00458432012-08-06 21:42:48 +03001175}
1176
Peter Xu16d063b2017-07-18 11:39:04 +08001177/*
1178 * Check whether the parameters are valid. Error will be put into errp
1179 * (if provided). Return true if valid, otherwise false.
1180 */
1181static bool migrate_params_check(MigrationParameters *params, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +08001182{
Eric Blake7f375e02016-09-08 22:14:16 -05001183 if (params->has_compress_level &&
Juan Quintela741d4082017-12-01 13:08:38 +01001184 (params->compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001185 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
1186 "is invalid, it should be in the range of 0 to 9");
Peter Xu16d063b2017-07-18 11:39:04 +08001187 return false;
Liang Li85de8322015-03-23 16:32:28 +08001188 }
Peter Xu16d063b2017-07-18 11:39:04 +08001189
Juan Quintela741d4082017-12-01 13:08:38 +01001190 if (params->has_compress_threads && (params->compress_threads < 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001191 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1192 "compress_threads",
1193 "is invalid, it should be in the range of 1 to 255");
Peter Xu16d063b2017-07-18 11:39:04 +08001194 return false;
Liang Li85de8322015-03-23 16:32:28 +08001195 }
Peter Xu16d063b2017-07-18 11:39:04 +08001196
Juan Quintela741d4082017-12-01 13:08:38 +01001197 if (params->has_decompress_threads && (params->decompress_threads < 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001198 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1199 "decompress_threads",
1200 "is invalid, it should be in the range of 1 to 255");
Peter Xu16d063b2017-07-18 11:39:04 +08001201 return false;
Liang Li85de8322015-03-23 16:32:28 +08001202 }
Peter Xu16d063b2017-07-18 11:39:04 +08001203
Keqian Zhudc14a472020-02-24 10:31:42 +08001204 if (params->has_throttle_trigger_threshold &&
1205 (params->throttle_trigger_threshold < 1 ||
1206 params->throttle_trigger_threshold > 100)) {
1207 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1208 "throttle_trigger_threshold",
1209 "an integer in the range of 1 to 100");
1210 return false;
1211 }
1212
Eric Blake7f375e02016-09-08 22:14:16 -05001213 if (params->has_cpu_throttle_initial &&
1214 (params->cpu_throttle_initial < 1 ||
1215 params->cpu_throttle_initial > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -04001216 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -04001217 "cpu_throttle_initial",
Jason J. Herne1626fee2015-09-08 13:12:34 -04001218 "an integer in the range of 1 to 99");
Peter Xu16d063b2017-07-18 11:39:04 +08001219 return false;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001220 }
Peter Xu16d063b2017-07-18 11:39:04 +08001221
Eric Blake7f375e02016-09-08 22:14:16 -05001222 if (params->has_cpu_throttle_increment &&
1223 (params->cpu_throttle_increment < 1 ||
1224 params->cpu_throttle_increment > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -04001225 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -04001226 "cpu_throttle_increment",
Jason J. Herne1626fee2015-09-08 13:12:34 -04001227 "an integer in the range of 1 to 99");
Peter Xu16d063b2017-07-18 11:39:04 +08001228 return false;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001229 }
Peter Xu16d063b2017-07-18 11:39:04 +08001230
Juan Quintela741d4082017-12-01 13:08:38 +01001231 if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) {
Mao Zhongyi2ee30cf2020-03-31 16:22:05 +08001232 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1233 "max_bandwidth",
1234 "an integer in the range of 0 to "stringify(SIZE_MAX)
1235 " bytes/second");
Peter Xu16d063b2017-07-18 11:39:04 +08001236 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301237 }
Peter Xu16d063b2017-07-18 11:39:04 +08001238
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301239 if (params->has_downtime_limit &&
Juan Quintela741d4082017-12-01 13:08:38 +01001240 (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
Mao Zhongyi2ee30cf2020-03-31 16:22:05 +08001241 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1242 "downtime_limit",
1243 "an integer in the range of 0 to "
Mao Zhongyi7ac55292020-03-31 16:22:07 +08001244 stringify(MAX_MIGRATE_DOWNTIME)" ms");
Peter Xu16d063b2017-07-18 11:39:04 +08001245 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301246 }
Peter Xu16d063b2017-07-18 11:39:04 +08001247
Juan Quintela741d4082017-12-01 13:08:38 +01001248 /* x_checkpoint_delay is now always positive */
1249
Juan Quintelacbfd6c92019-02-06 13:54:06 +01001250 if (params->has_multifd_channels && (params->multifd_channels < 1)) {
Juan Quintela4075fb12016-01-15 08:56:17 +01001251 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1252 "multifd_channels",
1253 "is invalid, it should be in the range of 1 to 255");
1254 return false;
1255 }
Peter Xu16d063b2017-07-18 11:39:04 +08001256
Juan Quintela9004db42020-01-23 17:08:52 +01001257 if (params->has_multifd_zlib_level &&
1258 (params->multifd_zlib_level > 9)) {
1259 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zlib_level",
1260 "is invalid, it should be in the range of 0 to 9");
1261 return false;
1262 }
1263
Juan Quintela6a9ad152020-01-23 17:41:36 +01001264 if (params->has_multifd_zstd_level &&
1265 (params->multifd_zstd_level > 20)) {
1266 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zstd_level",
1267 "is invalid, it should be in the range of 0 to 20");
1268 return false;
1269 }
1270
Juan Quintela73af8dd2017-10-05 21:30:10 +02001271 if (params->has_xbzrle_cache_size &&
1272 (params->xbzrle_cache_size < qemu_target_page_size() ||
1273 !is_power_of_2(params->xbzrle_cache_size))) {
1274 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1275 "xbzrle_cache_size",
1276 "is invalid, it should be bigger than target page size"
Mao Zhongyi06b1c6f2020-03-20 22:32:16 +08001277 " and a power of 2");
Juan Quintela73af8dd2017-10-05 21:30:10 +02001278 return false;
1279 }
1280
Li Qiang4cbc9c72018-08-01 06:00:20 -07001281 if (params->has_max_cpu_throttle &&
1282 (params->max_cpu_throttle < params->cpu_throttle_initial ||
1283 params->max_cpu_throttle > 99)) {
1284 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1285 "max_cpu_throttle",
1286 "an integer in the range of cpu_throttle_initial to 99");
1287 return false;
1288 }
1289
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00001290 if (params->has_announce_initial &&
1291 params->announce_initial > 100000) {
1292 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1293 "announce_initial",
1294 "is invalid, it must be less than 100000 ms");
1295 return false;
1296 }
1297 if (params->has_announce_max &&
1298 params->announce_max > 100000) {
1299 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1300 "announce_max",
1301 "is invalid, it must be less than 100000 ms");
1302 return false;
1303 }
1304 if (params->has_announce_rounds &&
1305 params->announce_rounds > 1000) {
1306 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1307 "announce_rounds",
1308 "is invalid, it must be in the range of 0 to 1000");
1309 return false;
1310 }
1311 if (params->has_announce_step &&
1312 (params->announce_step < 1 ||
1313 params->announce_step > 10000)) {
1314 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1315 "announce_step",
1316 "is invalid, it must be in the range of 1 to 10000 ms");
1317 return false;
1318 }
Max Reitz31e4c352020-08-20 17:07:23 +02001319
1320 if (params->has_block_bitmap_mapping &&
1321 !check_dirty_bitmap_mig_alias_map(params->block_bitmap_mapping, errp)) {
1322 error_prepend(errp, "Invalid mapping given for block-bitmap-mapping: ");
1323 return false;
1324 }
1325
Peter Xu16d063b2017-07-18 11:39:04 +08001326 return true;
1327}
1328
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001329static void migrate_params_test_apply(MigrateSetParameters *params,
1330 MigrationParameters *dest)
1331{
1332 *dest = migrate_get_current()->parameters;
1333
1334 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1335
1336 if (params->has_compress_level) {
1337 dest->compress_level = params->compress_level;
1338 }
1339
1340 if (params->has_compress_threads) {
1341 dest->compress_threads = params->compress_threads;
1342 }
1343
Xiao Guangrong1d588722018-08-21 16:10:20 +08001344 if (params->has_compress_wait_thread) {
1345 dest->compress_wait_thread = params->compress_wait_thread;
1346 }
1347
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001348 if (params->has_decompress_threads) {
1349 dest->decompress_threads = params->decompress_threads;
1350 }
1351
Keqian Zhudc14a472020-02-24 10:31:42 +08001352 if (params->has_throttle_trigger_threshold) {
1353 dest->throttle_trigger_threshold = params->throttle_trigger_threshold;
1354 }
1355
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001356 if (params->has_cpu_throttle_initial) {
1357 dest->cpu_throttle_initial = params->cpu_throttle_initial;
1358 }
1359
1360 if (params->has_cpu_throttle_increment) {
1361 dest->cpu_throttle_increment = params->cpu_throttle_increment;
1362 }
1363
Keqian Zhucbbf8182020-04-13 18:15:08 +08001364 if (params->has_cpu_throttle_tailslow) {
1365 dest->cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1366 }
1367
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001368 if (params->has_tls_creds) {
Markus Armbruster01fa5592017-07-18 14:42:04 +02001369 assert(params->tls_creds->type == QTYPE_QSTRING);
Zheng Chuan9728ebf2020-07-09 16:28:25 +08001370 dest->tls_creds = params->tls_creds->u.s;
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001371 }
1372
1373 if (params->has_tls_hostname) {
Markus Armbruster01fa5592017-07-18 14:42:04 +02001374 assert(params->tls_hostname->type == QTYPE_QSTRING);
Zheng Chuan9728ebf2020-07-09 16:28:25 +08001375 dest->tls_hostname = params->tls_hostname->u.s;
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001376 }
1377
1378 if (params->has_max_bandwidth) {
1379 dest->max_bandwidth = params->max_bandwidth;
1380 }
1381
1382 if (params->has_downtime_limit) {
1383 dest->downtime_limit = params->downtime_limit;
1384 }
1385
1386 if (params->has_x_checkpoint_delay) {
1387 dest->x_checkpoint_delay = params->x_checkpoint_delay;
1388 }
1389
1390 if (params->has_block_incremental) {
1391 dest->block_incremental = params->block_incremental;
1392 }
Juan Quintelacbfd6c92019-02-06 13:54:06 +01001393 if (params->has_multifd_channels) {
1394 dest->multifd_channels = params->multifd_channels;
Juan Quintela5e7577a2017-10-09 18:07:56 +02001395 }
Juan Quintela96eef042019-01-16 10:35:55 +01001396 if (params->has_multifd_compression) {
1397 dest->multifd_compression = params->multifd_compression;
1398 }
Juan Quintela73af8dd2017-10-05 21:30:10 +02001399 if (params->has_xbzrle_cache_size) {
1400 dest->xbzrle_cache_size = params->xbzrle_cache_size;
1401 }
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01001402 if (params->has_max_postcopy_bandwidth) {
1403 dest->max_postcopy_bandwidth = params->max_postcopy_bandwidth;
1404 }
Li Qiang4cbc9c72018-08-01 06:00:20 -07001405 if (params->has_max_cpu_throttle) {
1406 dest->max_cpu_throttle = params->max_cpu_throttle;
1407 }
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00001408 if (params->has_announce_initial) {
1409 dest->announce_initial = params->announce_initial;
1410 }
1411 if (params->has_announce_max) {
1412 dest->announce_max = params->announce_max;
1413 }
1414 if (params->has_announce_rounds) {
1415 dest->announce_rounds = params->announce_rounds;
1416 }
1417 if (params->has_announce_step) {
1418 dest->announce_step = params->announce_step;
1419 }
Max Reitz31e4c352020-08-20 17:07:23 +02001420
1421 if (params->has_block_bitmap_mapping) {
1422 dest->has_block_bitmap_mapping = true;
1423 dest->block_bitmap_mapping = params->block_bitmap_mapping;
1424 }
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001425}
1426
Juan Quintela73af8dd2017-10-05 21:30:10 +02001427static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
Peter Xu16d063b2017-07-18 11:39:04 +08001428{
1429 MigrationState *s = migrate_get_current();
1430
Markus Armbrustere87fae42017-07-18 12:57:38 +02001431 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1432
Eric Blake7f375e02016-09-08 22:14:16 -05001433 if (params->has_compress_level) {
1434 s->parameters.compress_level = params->compress_level;
Liang Li85de8322015-03-23 16:32:28 +08001435 }
Peter Xu476c72a2017-07-18 11:39:05 +08001436
Eric Blake7f375e02016-09-08 22:14:16 -05001437 if (params->has_compress_threads) {
1438 s->parameters.compress_threads = params->compress_threads;
Liang Li85de8322015-03-23 16:32:28 +08001439 }
Peter Xu476c72a2017-07-18 11:39:05 +08001440
Xiao Guangrong1d588722018-08-21 16:10:20 +08001441 if (params->has_compress_wait_thread) {
1442 s->parameters.compress_wait_thread = params->compress_wait_thread;
1443 }
1444
Eric Blake7f375e02016-09-08 22:14:16 -05001445 if (params->has_decompress_threads) {
1446 s->parameters.decompress_threads = params->decompress_threads;
Liang Li85de8322015-03-23 16:32:28 +08001447 }
Peter Xu476c72a2017-07-18 11:39:05 +08001448
Keqian Zhudc14a472020-02-24 10:31:42 +08001449 if (params->has_throttle_trigger_threshold) {
1450 s->parameters.throttle_trigger_threshold = params->throttle_trigger_threshold;
1451 }
1452
Eric Blake7f375e02016-09-08 22:14:16 -05001453 if (params->has_cpu_throttle_initial) {
1454 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001455 }
Peter Xu476c72a2017-07-18 11:39:05 +08001456
Eric Blake7f375e02016-09-08 22:14:16 -05001457 if (params->has_cpu_throttle_increment) {
1458 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001459 }
Peter Xu476c72a2017-07-18 11:39:05 +08001460
Keqian Zhucbbf8182020-04-13 18:15:08 +08001461 if (params->has_cpu_throttle_tailslow) {
1462 s->parameters.cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1463 }
1464
Eric Blake7f375e02016-09-08 22:14:16 -05001465 if (params->has_tls_creds) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001466 g_free(s->parameters.tls_creds);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001467 assert(params->tls_creds->type == QTYPE_QSTRING);
1468 s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001469 }
Peter Xu476c72a2017-07-18 11:39:05 +08001470
Eric Blake7f375e02016-09-08 22:14:16 -05001471 if (params->has_tls_hostname) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001472 g_free(s->parameters.tls_hostname);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001473 assert(params->tls_hostname->type == QTYPE_QSTRING);
1474 s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001475 }
Peter Xu476c72a2017-07-18 11:39:05 +08001476
Daniel P. Berranged2f1d292019-02-27 14:53:24 +00001477 if (params->has_tls_authz) {
1478 g_free(s->parameters.tls_authz);
1479 assert(params->tls_authz->type == QTYPE_QSTRING);
1480 s->parameters.tls_authz = g_strdup(params->tls_authz->u.s);
1481 }
1482
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301483 if (params->has_max_bandwidth) {
1484 s->parameters.max_bandwidth = params->max_bandwidth;
Dr. David Alan Gilbertc38c1c12019-03-08 10:12:10 +00001485 if (s->to_dst_file && !migration_in_postcopy()) {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301486 qemu_file_set_rate_limit(s->to_dst_file,
1487 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
1488 }
1489 }
Peter Xu476c72a2017-07-18 11:39:05 +08001490
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301491 if (params->has_downtime_limit) {
1492 s->parameters.downtime_limit = params->downtime_limit;
1493 }
zhanghailiang68b53592016-10-27 14:43:01 +08001494
1495 if (params->has_x_checkpoint_delay) {
1496 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
zhanghailiang479125d2017-01-17 20:57:42 +08001497 if (migration_in_colo_state()) {
1498 colo_checkpoint_notify(s);
1499 }
zhanghailiang68b53592016-10-27 14:43:01 +08001500 }
Peter Xu476c72a2017-07-18 11:39:05 +08001501
Juan Quintela2833c592017-04-05 18:32:37 +02001502 if (params->has_block_incremental) {
1503 s->parameters.block_incremental = params->block_incremental;
1504 }
Juan Quintelacbfd6c92019-02-06 13:54:06 +01001505 if (params->has_multifd_channels) {
1506 s->parameters.multifd_channels = params->multifd_channels;
Juan Quintela4075fb12016-01-15 08:56:17 +01001507 }
Juan Quintela96eef042019-01-16 10:35:55 +01001508 if (params->has_multifd_compression) {
1509 s->parameters.multifd_compression = params->multifd_compression;
1510 }
Juan Quintela73af8dd2017-10-05 21:30:10 +02001511 if (params->has_xbzrle_cache_size) {
1512 s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
1513 xbzrle_cache_resize(params->xbzrle_cache_size, errp);
1514 }
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01001515 if (params->has_max_postcopy_bandwidth) {
1516 s->parameters.max_postcopy_bandwidth = params->max_postcopy_bandwidth;
Dr. David Alan Gilbertc38c1c12019-03-08 10:12:10 +00001517 if (s->to_dst_file && migration_in_postcopy()) {
1518 qemu_file_set_rate_limit(s->to_dst_file,
1519 s->parameters.max_postcopy_bandwidth / XFER_LIMIT_RATIO);
1520 }
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01001521 }
Li Qiang4cbc9c72018-08-01 06:00:20 -07001522 if (params->has_max_cpu_throttle) {
1523 s->parameters.max_cpu_throttle = params->max_cpu_throttle;
1524 }
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00001525 if (params->has_announce_initial) {
1526 s->parameters.announce_initial = params->announce_initial;
1527 }
1528 if (params->has_announce_max) {
1529 s->parameters.announce_max = params->announce_max;
1530 }
1531 if (params->has_announce_rounds) {
1532 s->parameters.announce_rounds = params->announce_rounds;
1533 }
1534 if (params->has_announce_step) {
1535 s->parameters.announce_step = params->announce_step;
1536 }
Max Reitz31e4c352020-08-20 17:07:23 +02001537
1538 if (params->has_block_bitmap_mapping) {
1539 qapi_free_BitmapMigrationNodeAliasList(
1540 s->parameters.block_bitmap_mapping);
1541
1542 s->parameters.has_block_bitmap_mapping = true;
1543 s->parameters.block_bitmap_mapping =
1544 QAPI_CLONE(BitmapMigrationNodeAliasList,
1545 params->block_bitmap_mapping);
1546 }
Liang Li85de8322015-03-23 16:32:28 +08001547}
1548
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001549void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
Peter Xu476c72a2017-07-18 11:39:05 +08001550{
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001551 MigrationParameters tmp;
1552
Markus Armbruster01fa5592017-07-18 14:42:04 +02001553 /* TODO Rewrite "" to null instead */
1554 if (params->has_tls_creds
1555 && params->tls_creds->type == QTYPE_QNULL) {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001556 qobject_unref(params->tls_creds->u.n);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001557 params->tls_creds->type = QTYPE_QSTRING;
1558 params->tls_creds->u.s = strdup("");
1559 }
1560 /* TODO Rewrite "" to null instead */
1561 if (params->has_tls_hostname
1562 && params->tls_hostname->type == QTYPE_QNULL) {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001563 qobject_unref(params->tls_hostname->u.n);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001564 params->tls_hostname->type = QTYPE_QSTRING;
1565 params->tls_hostname->u.s = strdup("");
1566 }
1567
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001568 migrate_params_test_apply(params, &tmp);
1569
1570 if (!migrate_params_check(&tmp, errp)) {
Peter Xu476c72a2017-07-18 11:39:05 +08001571 /* Invalid parameter */
1572 return;
1573 }
1574
Juan Quintela73af8dd2017-10-05 21:30:10 +02001575 migrate_params_apply(params, errp);
Peter Xu476c72a2017-07-18 11:39:05 +08001576}
1577
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001578
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001579void qmp_migrate_start_postcopy(Error **errp)
1580{
1581 MigrationState *s = migrate_get_current();
1582
Vladimir Sementsov-Ogievskiy16b0fd32018-03-13 15:34:01 -04001583 if (!migrate_postcopy()) {
Dr. David Alan Gilberta54d3402015-11-12 11:34:44 +00001584 error_setg(errp, "Enable postcopy with migrate_set_capability before"
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001585 " the start of migration");
1586 return;
1587 }
1588
1589 if (s->state == MIGRATION_STATUS_NONE) {
1590 error_setg(errp, "Postcopy must be started after migration has been"
1591 " started");
1592 return;
1593 }
1594 /*
1595 * we don't error if migration has finished since that would be racy
1596 * with issuing this command.
1597 */
1598 atomic_set(&s->start_postcopy, true);
1599}
1600
aliguori065e2812008-11-11 16:46:33 +00001601/* shared migration helpers */
1602
zhanghailiang48781e52015-12-16 11:47:33 +00001603void migrate_set_state(int *state, int old_state, int new_state)
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +00001604{
Peter Xua31fede2017-08-30 16:32:01 +08001605 assert(new_state < MIGRATION_STATUS__MAX);
zhanghailiang48781e52015-12-16 11:47:33 +00001606 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
Peter Xua31fede2017-08-30 16:32:01 +08001607 trace_migrate_set_state(MigrationStatus_str(new_state));
Juan Quintelab05dc722015-07-07 14:44:05 +02001608 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +00001609 }
1610}
1611
Peter Xu4e4a3d32017-07-18 11:39:09 +08001612static MigrationCapabilityStatusList *migrate_cap_add(
1613 MigrationCapabilityStatusList *list,
1614 MigrationCapability index,
1615 bool state)
Juan Quintela2833c592017-04-05 18:32:37 +02001616{
1617 MigrationCapabilityStatusList *cap;
1618
1619 cap = g_new0(MigrationCapabilityStatusList, 1);
1620 cap->value = g_new0(MigrationCapabilityStatus, 1);
Peter Xu4e4a3d32017-07-18 11:39:09 +08001621 cap->value->capability = index;
1622 cap->value->state = state;
1623 cap->next = list;
1624
1625 return cap;
1626}
1627
1628void migrate_set_block_enabled(bool value, Error **errp)
1629{
1630 MigrationCapabilityStatusList *cap;
1631
1632 cap = migrate_cap_add(NULL, MIGRATION_CAPABILITY_BLOCK, value);
Juan Quintela2833c592017-04-05 18:32:37 +02001633 qmp_migrate_set_capabilities(cap, errp);
1634 qapi_free_MigrationCapabilityStatusList(cap);
1635}
1636
1637static void migrate_set_block_incremental(MigrationState *s, bool value)
1638{
1639 s->parameters.block_incremental = value;
1640}
1641
1642static void block_cleanup_parameters(MigrationState *s)
1643{
1644 if (s->must_remove_block_options) {
1645 /* setting to false can never fail */
1646 migrate_set_block_enabled(false, &error_abort);
1647 migrate_set_block_incremental(s, false);
1648 s->must_remove_block_options = false;
1649 }
1650}
1651
Yury Kotovfd392cf2019-04-08 14:33:43 +03001652static void migrate_fd_cleanup(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +00001653{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001654 qemu_bh_delete(s->cleanup_bh);
1655 s->cleanup_bh = NULL;
1656
Peter Xu0ceccd82018-01-03 20:20:06 +08001657 qemu_savevm_state_cleanup();
1658
zhanghailiang89a02a92016-01-15 11:37:42 +08001659 if (s->to_dst_file) {
Peter Xu62df0662018-05-02 18:47:38 +08001660 QEMUFile *tmp;
Juan Quintelaf986c3d2016-01-14 16:52:55 +01001661
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001662 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001663 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001664 if (s->migration_thread_running) {
1665 qemu_thread_join(&s->thread);
1666 s->migration_thread_running = false;
1667 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001668 qemu_mutex_lock_iothread();
1669
Fei Li1398b2e2019-01-13 22:08:47 +08001670 multifd_save_cleanup();
Peter Xu62df0662018-05-02 18:47:38 +08001671 qemu_mutex_lock(&s->qemu_file_lock);
1672 tmp = s->to_dst_file;
zhanghailiang89a02a92016-01-15 11:37:42 +08001673 s->to_dst_file = NULL;
Peter Xu62df0662018-05-02 18:47:38 +08001674 qemu_mutex_unlock(&s->qemu_file_lock);
1675 /*
1676 * Close the file handle without the lock to make sure the
1677 * critical section won't block for long.
1678 */
1679 qemu_fclose(tmp);
aliguori065e2812008-11-11 16:46:33 +00001680 }
1681
Wei Yang8f8d5282019-07-17 08:53:41 +08001682 assert(!migration_is_active(s));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001683
Liang Li94f5a432015-11-02 15:37:00 +08001684 if (s->state == MIGRATION_STATUS_CANCELLING) {
zhanghailiang48781e52015-12-16 11:47:33 +00001685 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
Liang Li94f5a432015-11-02 15:37:00 +08001686 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001687 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001688
Juan Quintela87db1a72017-09-05 12:50:22 +02001689 if (s->error) {
1690 /* It is used on info migrate. We can't free it */
1691 error_report_err(error_copy(s->error));
1692 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001693 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +02001694 block_cleanup_parameters(s);
aliguori065e2812008-11-11 16:46:33 +00001695}
1696
Yury Kotovfd392cf2019-04-08 14:33:43 +03001697static void migrate_fd_cleanup_schedule(MigrationState *s)
1698{
1699 /*
1700 * Ref the state for bh, because it may be called when
1701 * there're already no other refs
1702 */
1703 object_ref(OBJECT(s));
1704 qemu_bh_schedule(s->cleanup_bh);
1705}
1706
1707static void migrate_fd_cleanup_bh(void *opaque)
1708{
1709 MigrationState *s = opaque;
1710 migrate_fd_cleanup(s);
1711 object_unref(OBJECT(s));
1712}
1713
Juan Quintela87db1a72017-09-05 12:50:22 +02001714void migrate_set_error(MigrationState *s, const Error *error)
1715{
Daniel Brodsky6e8a3552020-04-03 21:21:08 -07001716 QEMU_LOCK_GUARD(&s->error_mutex);
Juan Quintela87db1a72017-09-05 12:50:22 +02001717 if (!s->error) {
1718 s->error = error_copy(error);
1719 }
Juan Quintela87db1a72017-09-05 12:50:22 +02001720}
1721
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001722void migrate_fd_error(MigrationState *s, const Error *error)
aliguori065e2812008-11-11 16:46:33 +00001723{
Peter Maydell25174052016-10-21 18:41:45 +01001724 trace_migrate_fd_error(error_get_pretty(error));
zhanghailiang89a02a92016-01-15 11:37:42 +08001725 assert(s->to_dst_file == NULL);
zhanghailiang48781e52015-12-16 11:47:33 +00001726 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1727 MIGRATION_STATUS_FAILED);
Juan Quintela87db1a72017-09-05 12:50:22 +02001728 migrate_set_error(s, error);
Juan Quintela458cf282011-02-22 23:32:54 +01001729}
1730
Juan Quintela0edda1c2010-05-11 16:28:39 +02001731static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +00001732{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001733 int old_state ;
zhanghailiang89a02a92016-01-15 11:37:42 +08001734 QEMUFile *f = migrate_get_current()->to_dst_file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001735 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +00001736
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001737 if (s->rp_state.from_dst_file) {
1738 /* shutdown the rp socket, so causing the rp thread to shutdown */
1739 qemu_file_shutdown(s->rp_state.from_dst_file);
1740 }
1741
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001742 do {
1743 old_state = s->state;
Juan Quintela392d87e2020-01-21 15:39:23 +01001744 if (!migration_is_running(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001745 break;
1746 }
Dr. David Alan Gilberta7b36b42017-10-20 10:05:55 +01001747 /* If the migration is paused, kick it out of the pause */
1748 if (old_state == MIGRATION_STATUS_PRE_SWITCHOVER) {
1749 qemu_sem_post(&s->pause_sem);
1750 }
zhanghailiang48781e52015-12-16 11:47:33 +00001751 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
zhanghailiang31194732015-03-13 16:08:38 +08001752 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001753
1754 /*
1755 * If we're unlucky the migration code might be stuck somewhere in a
1756 * send/write while the network has failed and is waiting to timeout;
1757 * if we've got shutdown(2) available then we can force it to quit.
1758 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1759 * called in a bh, so there is no race against this cancel.
1760 */
zhanghailiang31194732015-03-13 16:08:38 +08001761 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001762 qemu_file_shutdown(f);
1763 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08001764 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
1765 Error *local_err = NULL;
1766
1767 bdrv_invalidate_cache_all(&local_err);
1768 if (local_err) {
1769 error_report_err(local_err);
1770 } else {
1771 s->block_inactive = false;
1772 }
1773 }
aliguori065e2812008-11-11 16:46:33 +00001774}
1775
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001776void add_migration_state_change_notifier(Notifier *notify)
1777{
1778 notifier_list_add(&migration_state_notifiers, notify);
1779}
1780
1781void remove_migration_state_change_notifier(Notifier *notify)
1782{
Paolo Bonzini31552522012-01-13 17:34:01 +01001783 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001784}
1785
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +02001786bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001787{
zhanghailiang31194732015-03-13 16:08:38 +08001788 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001789}
1790
Juan Quintela70736932011-02-23 00:43:59 +01001791bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001792{
zhanghailiang31194732015-03-13 16:08:38 +08001793 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001794}
Juan Quintela0edda1c2010-05-11 16:28:39 +02001795
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001796bool migration_has_failed(MigrationState *s)
1797{
zhanghailiang31194732015-03-13 16:08:38 +08001798 return (s->state == MIGRATION_STATUS_CANCELLED ||
1799 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001800}
1801
Juan Quintela57273092017-03-20 22:25:28 +01001802bool migration_in_postcopy(void)
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001803{
Juan Quintela57273092017-03-20 22:25:28 +01001804 MigrationState *s = migrate_get_current();
1805
Dr. David Alan Gilbert3748fef2019-09-23 18:49:42 +01001806 switch (s->state) {
1807 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1808 case MIGRATION_STATUS_POSTCOPY_PAUSED:
1809 case MIGRATION_STATUS_POSTCOPY_RECOVER:
1810 return true;
1811 default:
1812 return false;
1813 }
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001814}
1815
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001816bool migration_in_postcopy_after_devices(MigrationState *s)
1817{
Juan Quintela57273092017-03-20 22:25:28 +01001818 return migration_in_postcopy() && s->postcopy_after_devices;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001819}
1820
David Hildenbrand06df2e62020-06-26 09:22:33 +02001821bool migration_in_incoming_postcopy(void)
1822{
1823 PostcopyState ps = postcopy_state_get();
1824
1825 return ps >= POSTCOPY_INCOMING_DISCARD && ps < POSTCOPY_INCOMING_END;
1826}
1827
Juan Quintelafab35002017-03-22 17:36:57 +01001828bool migration_is_idle(void)
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301829{
Markus Armbrusterdaff7f02019-04-01 11:08:25 +02001830 MigrationState *s = current_migration;
1831
1832 if (!s) {
1833 return true;
1834 }
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301835
1836 switch (s->state) {
1837 case MIGRATION_STATUS_NONE:
1838 case MIGRATION_STATUS_CANCELLED:
1839 case MIGRATION_STATUS_COMPLETED:
1840 case MIGRATION_STATUS_FAILED:
1841 return true;
1842 case MIGRATION_STATUS_SETUP:
1843 case MIGRATION_STATUS_CANCELLING:
1844 case MIGRATION_STATUS_ACTIVE:
1845 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1846 case MIGRATION_STATUS_COLO:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +01001847 case MIGRATION_STATUS_PRE_SWITCHOVER:
1848 case MIGRATION_STATUS_DEVICE:
Jens Freimannc7e0acd2019-10-29 12:49:02 +01001849 case MIGRATION_STATUS_WAIT_UNPLUG:
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301850 return false;
1851 case MIGRATION_STATUS__MAX:
1852 g_assert_not_reached();
1853 }
1854
1855 return false;
1856}
1857
Wei Yang8f8d5282019-07-17 08:53:41 +08001858bool migration_is_active(MigrationState *s)
1859{
1860 return (s->state == MIGRATION_STATUS_ACTIVE ||
1861 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1862}
1863
Peter Xu3e0c8052018-02-08 18:31:15 +08001864void migrate_init(MigrationState *s)
Juan Quintela0edda1c2010-05-11 16:28:39 +02001865{
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001866 /*
1867 * Reinitialise all migration state, except
1868 * parameters/capabilities that the user set, and
1869 * locks.
1870 */
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001871 s->cleanup_bh = 0;
zhanghailiang89a02a92016-01-15 11:37:42 +08001872 s->to_dst_file = NULL;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001873 s->state = MIGRATION_STATUS_NONE;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001874 s->rp_state.from_dst_file = NULL;
1875 s->rp_state.error = false;
1876 s->mbps = 0.0;
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08001877 s->pages_per_second = 0.0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001878 s->downtime = 0;
1879 s->expected_downtime = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001880 s->setup_time = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001881 s->start_postcopy = false;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001882 s->postcopy_after_devices = false;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001883 s->migration_thread_running = false;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001884 error_free(s->error);
1885 s->error = NULL;
Juan Quintela1299c632011-11-09 21:29:01 +01001886
zhanghailiang48781e52015-12-16 11:47:33 +00001887 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +02001888
Peter Xu4af246a2018-01-03 20:20:08 +08001889 s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1890 s->total_time = 0;
Peter Xu7287cbd2018-01-03 20:20:09 +08001891 s->vm_was_running = false;
Peter Xub15df1a2018-01-03 20:20:13 +08001892 s->iteration_initial_bytes = 0;
1893 s->threshold_size = 0;
Juan Quintela0edda1c2010-05-11 16:28:39 +02001894}
Juan Quintelacab30142011-02-22 23:54:21 +01001895
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001896static GSList *migration_blockers;
1897
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301898int migrate_add_blocker(Error *reason, Error **errp)
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001899{
Markus Armbruster811f8652019-04-01 11:08:24 +02001900 if (only_migratable) {
Markus Armbruster4b576642018-10-17 10:26:25 +02001901 error_propagate_prepend(errp, error_copy(reason),
1902 "disallowing migration blocker "
Greg Kurzb6eca812019-05-16 12:58:05 +02001903 "(--only-migratable) for: ");
Ashijeet Acharyab67b8c32017-01-16 17:01:54 +05301904 return -EACCES;
1905 }
1906
Juan Quintelafab35002017-03-22 17:36:57 +01001907 if (migration_is_idle()) {
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301908 migration_blockers = g_slist_prepend(migration_blockers, reason);
1909 return 0;
1910 }
1911
Markus Armbruster4b576642018-10-17 10:26:25 +02001912 error_propagate_prepend(errp, error_copy(reason),
1913 "disallowing migration blocker "
1914 "(migration in progress) for: ");
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301915 return -EBUSY;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001916}
1917
1918void migrate_del_blocker(Error *reason)
1919{
1920 migration_blockers = g_slist_remove(migration_blockers, reason);
1921}
1922
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001923void qmp_migrate_incoming(const char *uri, Error **errp)
1924{
1925 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001926 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001927
1928 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001929 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001930 return;
1931 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001932 if (!once) {
1933 error_setg(errp, "The incoming migration has already been started");
Yury Kotov603d5a42019-11-13 20:53:25 +03001934 return;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001935 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001936
1937 qemu_start_incoming_migration(uri, &local_err);
1938
1939 if (local_err) {
1940 error_propagate(errp, local_err);
1941 return;
1942 }
1943
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001944 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001945}
1946
Peter Xu02affd42018-05-02 18:47:36 +08001947void qmp_migrate_recover(const char *uri, Error **errp)
1948{
1949 MigrationIncomingState *mis = migration_incoming_get_current();
1950
1951 if (mis->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
1952 error_setg(errp, "Migrate recover can only be run "
1953 "when postcopy is paused.");
1954 return;
1955 }
1956
1957 if (atomic_cmpxchg(&mis->postcopy_recover_triggered,
1958 false, true) == true) {
1959 error_setg(errp, "Migrate recovery is triggered already");
1960 return;
1961 }
1962
1963 /*
1964 * Note that this call will never start a real migration; it will
1965 * only re-setup the migration stream and poke existing migration
1966 * to continue using that newly established channel.
1967 */
1968 qemu_start_incoming_migration(uri, errp);
1969}
1970
Peter Xubfbf89c2018-05-02 18:47:39 +08001971void qmp_migrate_pause(Error **errp)
1972{
1973 MigrationState *ms = migrate_get_current();
1974 MigrationIncomingState *mis = migration_incoming_get_current();
1975 int ret;
1976
1977 if (ms->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1978 /* Source side, during postcopy */
1979 qemu_mutex_lock(&ms->qemu_file_lock);
1980 ret = qemu_file_shutdown(ms->to_dst_file);
1981 qemu_mutex_unlock(&ms->qemu_file_lock);
1982 if (ret) {
1983 error_setg(errp, "Failed to pause source migration");
1984 }
1985 return;
1986 }
1987
1988 if (mis->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1989 ret = qemu_file_shutdown(mis->from_src_file);
1990 if (ret) {
1991 error_setg(errp, "Failed to pause destination migration");
1992 }
1993 return;
1994 }
1995
1996 error_setg(errp, "migrate-pause is currently only supported "
1997 "during postcopy-active state");
1998}
1999
Greg Kurz24f39022016-05-04 21:44:19 +02002000bool migration_is_blocked(Error **errp)
2001{
2002 if (qemu_savevm_state_blocked(errp)) {
2003 return true;
2004 }
2005
2006 if (migration_blockers) {
Eduardo Habkost250561e2017-06-08 10:39:05 -03002007 error_propagate(errp, error_copy(migration_blockers->data));
Greg Kurz24f39022016-05-04 21:44:19 +02002008 return true;
2009 }
2010
2011 return false;
2012}
2013
Peter Xud3e35b82018-05-02 18:47:24 +08002014/* Returns true if continue to migrate, or false if error detected */
2015static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc,
2016 bool resume, Error **errp)
2017{
2018 Error *local_err = NULL;
2019
2020 if (resume) {
2021 if (s->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
2022 error_setg(errp, "Cannot resume if there is no "
2023 "paused migration");
2024 return false;
2025 }
Peter Xu97ca2112018-07-23 20:33:03 +08002026
2027 /*
2028 * Postcopy recovery won't work well with release-ram
2029 * capability since release-ram will drop the page buffer as
2030 * long as the page is put into the send buffer. So if there
2031 * is a network failure happened, any page buffers that have
2032 * not yet reached the destination VM but have already been
2033 * sent from the source VM will be lost forever. Let's refuse
2034 * the client from resuming such a postcopy migration.
2035 * Luckily release-ram was designed to only be used when src
2036 * and destination VMs are on the same host, so it should be
2037 * fine.
2038 */
2039 if (migrate_release_ram()) {
2040 error_setg(errp, "Postcopy recovery cannot work "
2041 "when release-ram capability is set");
2042 return false;
2043 }
2044
Peter Xud3e35b82018-05-02 18:47:24 +08002045 /* This is a resume, skip init status */
2046 return true;
2047 }
2048
Juan Quintela392d87e2020-01-21 15:39:23 +01002049 if (migration_is_running(s->state)) {
Peter Xud3e35b82018-05-02 18:47:24 +08002050 error_setg(errp, QERR_MIGRATION_ACTIVE);
2051 return false;
2052 }
2053
2054 if (runstate_check(RUN_STATE_INMIGRATE)) {
2055 error_setg(errp, "Guest is waiting for an incoming migration");
2056 return false;
2057 }
2058
2059 if (migration_is_blocked(errp)) {
2060 return false;
2061 }
2062
2063 if (blk || blk_inc) {
2064 if (migrate_use_block() || migrate_use_block_incremental()) {
2065 error_setg(errp, "Command options are incompatible with "
2066 "current migration capabilities");
2067 return false;
2068 }
2069 migrate_set_block_enabled(true, &local_err);
2070 if (local_err) {
2071 error_propagate(errp, local_err);
2072 return false;
2073 }
2074 s->must_remove_block_options = true;
2075 }
2076
2077 if (blk_inc) {
2078 migrate_set_block_incremental(s, true);
2079 }
2080
2081 migrate_init(s);
Ivan Ren87f3bd82019-08-02 18:18:41 +08002082 /*
2083 * set ram_counters memory to zero for a
2084 * new migration
2085 */
2086 memset(&ram_counters, 0, sizeof(ram_counters));
Peter Xud3e35b82018-05-02 18:47:24 +08002087
2088 return true;
2089}
2090
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002091void qmp_migrate(const char *uri, bool has_blk, bool blk,
2092 bool has_inc, bool inc, bool has_detach, bool detach,
Peter Xu7a4da282018-05-02 18:47:23 +08002093 bool has_resume, bool resume, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002094{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +02002095 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +02002096 MigrationState *s = migrate_get_current();
Juan Quintelacab30142011-02-22 23:54:21 +01002097 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +01002098
Peter Xud3e35b82018-05-02 18:47:24 +08002099 if (!migrate_prepare(s, has_blk && blk, has_inc && inc,
2100 has_resume && resume, errp)) {
2101 /* Error detected, put into errp */
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002102 return;
Juan Quintelacab30142011-02-22 23:54:21 +01002103 }
Juan Quintelacab30142011-02-22 23:54:21 +01002104
2105 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002106 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04002107#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08002108 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04002109 rdma_start_outgoing_migration(s, p, &local_err);
2110#endif
Juan Quintelacab30142011-02-22 23:54:21 +01002111 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002112 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01002113 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002114 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01002115 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002116 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01002117 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002118 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
2119 "a valid migration protocol");
zhanghailiang48781e52015-12-16 11:47:33 +00002120 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2121 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09576e72018-03-16 20:21:14 +00002122 block_cleanup_parameters(s);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002123 return;
Juan Quintelacab30142011-02-22 23:54:21 +01002124 }
2125
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002126 if (local_err) {
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01002127 migrate_fd_error(s, local_err);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002128 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002129 return;
Juan Quintela1299c632011-11-09 21:29:01 +01002130 }
Juan Quintelacab30142011-02-22 23:54:21 +01002131}
2132
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02002133void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002134{
Juan Quintela17549e82011-10-05 13:50:43 +02002135 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01002136}
2137
Dr. David Alan Gilbert89cfc022017-10-20 10:05:53 +01002138void qmp_migrate_continue(MigrationStatus state, Error **errp)
2139{
2140 MigrationState *s = migrate_get_current();
2141 if (s->state != state) {
2142 error_setg(errp, "Migration not in expected state: %s",
2143 MigrationStatus_str(s->state));
2144 return;
2145 }
2146 qemu_sem_post(&s->pause_sem);
2147}
2148
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002149void qmp_migrate_set_cache_size(int64_t value, Error **errp)
2150{
Juan Quintela73af8dd2017-10-05 21:30:10 +02002151 MigrateSetParameters p = {
2152 .has_xbzrle_cache_size = true,
2153 .xbzrle_cache_size = value,
2154 };
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002155
Juan Quintela73af8dd2017-10-05 21:30:10 +02002156 qmp_migrate_set_parameters(&p, errp);
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002157}
2158
2159int64_t qmp_query_migrate_cache_size(Error **errp)
2160{
2161 return migrate_xbzrle_cache_size();
2162}
2163
Luiz Capitulino3dc85382011-11-28 11:59:37 -02002164void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002165{
Markus Armbruster1bda8b32017-07-18 13:42:11 +02002166 MigrateSetParameters p = {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302167 .has_max_bandwidth = true,
2168 .max_bandwidth = value,
2169 };
Juan Quintelacab30142011-02-22 23:54:21 +01002170
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302171 qmp_migrate_set_parameters(&p, errp);
Juan Quintelacab30142011-02-22 23:54:21 +01002172}
2173
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02002174void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002175{
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03002176 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
Mao Zhongyi2ee30cf2020-03-31 16:22:05 +08002177 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
2178 "downtime_limit",
2179 "an integer in the range of 0 to "
2180 stringify(MAX_MIGRATE_DOWNTIME_SECONDS)" seconds");
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03002181 return;
2182 }
2183
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302184 value *= 1000; /* Convert to milliseconds */
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302185
Markus Armbruster1bda8b32017-07-18 13:42:11 +02002186 MigrateSetParameters p = {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302187 .has_downtime_limit = true,
Fangrui Song2667c982019-11-22 09:00:38 +01002188 .downtime_limit = (int64_t)value,
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302189 };
2190
2191 qmp_migrate_set_parameters(&p, errp);
aliguori5bb79102008-10-13 03:12:02 +00002192}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03002193
Pavel Butsykin53f09a12017-02-03 18:23:20 +03002194bool migrate_release_ram(void)
2195{
2196 MigrationState *s;
2197
2198 s = migrate_get_current();
2199
2200 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
2201}
2202
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00002203bool migrate_postcopy_ram(void)
2204{
2205 MigrationState *s;
2206
2207 s = migrate_get_current();
2208
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +00002209 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00002210}
2211
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002212bool migrate_postcopy(void)
2213{
Vladimir Sementsov-Ogievskiydd6bb912018-03-13 15:34:00 -04002214 return migrate_postcopy_ram() || migrate_dirty_bitmaps();
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002215}
2216
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06002217bool migrate_auto_converge(void)
2218{
2219 MigrationState *s;
2220
2221 s = migrate_get_current();
2222
2223 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
2224}
2225
Peter Lieven323004a2013-07-18 09:48:50 +02002226bool migrate_zero_blocks(void)
2227{
2228 MigrationState *s;
2229
2230 s = migrate_get_current();
2231
2232 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
2233}
2234
Alexey Perevalovf22f9282018-03-22 21:17:22 +03002235bool migrate_postcopy_blocktime(void)
2236{
2237 MigrationState *s;
2238
2239 s = migrate_get_current();
2240
2241 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME];
2242}
2243
Liang Li8706d2d2015-03-23 16:32:17 +08002244bool migrate_use_compression(void)
2245{
Liang Lidde4e692015-03-23 16:32:26 +08002246 MigrationState *s;
2247
2248 s = migrate_get_current();
2249
2250 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08002251}
2252
2253int migrate_compress_level(void)
2254{
2255 MigrationState *s;
2256
2257 s = migrate_get_current();
2258
Daniel P. Berrange2594f562016-04-27 11:05:14 +01002259 return s->parameters.compress_level;
Liang Li8706d2d2015-03-23 16:32:17 +08002260}
2261
2262int migrate_compress_threads(void)
2263{
2264 MigrationState *s;
2265
2266 s = migrate_get_current();
2267
Daniel P. Berrange2594f562016-04-27 11:05:14 +01002268 return s->parameters.compress_threads;
Liang Li8706d2d2015-03-23 16:32:17 +08002269}
2270
Xiao Guangrong1d588722018-08-21 16:10:20 +08002271int migrate_compress_wait_thread(void)
2272{
2273 MigrationState *s;
2274
2275 s = migrate_get_current();
2276
2277 return s->parameters.compress_wait_thread;
2278}
2279
Liang Li3fcb38c2015-03-23 16:32:18 +08002280int migrate_decompress_threads(void)
2281{
2282 MigrationState *s;
2283
2284 s = migrate_get_current();
2285
Daniel P. Berrange2594f562016-04-27 11:05:14 +01002286 return s->parameters.decompress_threads;
Liang Li3fcb38c2015-03-23 16:32:18 +08002287}
2288
Vladimir Sementsov-Ogievskiy55efc8c2018-03-13 15:34:00 -04002289bool migrate_dirty_bitmaps(void)
2290{
2291 MigrationState *s;
2292
2293 s = migrate_get_current();
2294
2295 return s->enabled_capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS];
2296}
2297
Yury Kotov18269062019-02-15 20:45:45 +03002298bool migrate_ignore_shared(void)
2299{
2300 MigrationState *s;
2301
2302 s = migrate_get_current();
2303
2304 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_IGNORE_SHARED];
2305}
2306
Yury Kotovb9d68df2019-09-03 19:22:44 +03002307bool migrate_validate_uuid(void)
2308{
2309 MigrationState *s;
2310
2311 s = migrate_get_current();
2312
2313 return s->enabled_capabilities[MIGRATION_CAPABILITY_VALIDATE_UUID];
2314}
2315
Juan Quintelab05dc722015-07-07 14:44:05 +02002316bool migrate_use_events(void)
2317{
2318 MigrationState *s;
2319
2320 s = migrate_get_current();
2321
2322 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
2323}
2324
Juan Quintela30126bb2016-01-14 12:23:00 +01002325bool migrate_use_multifd(void)
2326{
2327 MigrationState *s;
2328
2329 s = migrate_get_current();
2330
Juan Quintelacbfd6c92019-02-06 13:54:06 +01002331 return s->enabled_capabilities[MIGRATION_CAPABILITY_MULTIFD];
Juan Quintela30126bb2016-01-14 12:23:00 +01002332}
2333
Dr. David Alan Gilbert93fbd032017-10-20 10:05:50 +01002334bool migrate_pause_before_switchover(void)
2335{
2336 MigrationState *s;
2337
2338 s = migrate_get_current();
2339
2340 return s->enabled_capabilities[
2341 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER];
2342}
2343
Juan Quintela4075fb12016-01-15 08:56:17 +01002344int migrate_multifd_channels(void)
2345{
2346 MigrationState *s;
2347
2348 s = migrate_get_current();
2349
Juan Quintelacbfd6c92019-02-06 13:54:06 +01002350 return s->parameters.multifd_channels;
Juan Quintela4075fb12016-01-15 08:56:17 +01002351}
2352
Juan Quintelaab7cbb02019-05-15 13:37:46 +02002353MultiFDCompression migrate_multifd_compression(void)
2354{
2355 MigrationState *s;
2356
2357 s = migrate_get_current();
2358
2359 return s->parameters.multifd_compression;
2360}
2361
Juan Quintela9004db42020-01-23 17:08:52 +01002362int migrate_multifd_zlib_level(void)
2363{
2364 MigrationState *s;
2365
2366 s = migrate_get_current();
2367
2368 return s->parameters.multifd_zlib_level;
2369}
2370
Juan Quintela6a9ad152020-01-23 17:41:36 +01002371int migrate_multifd_zstd_level(void)
2372{
2373 MigrationState *s;
2374
2375 s = migrate_get_current();
2376
2377 return s->parameters.multifd_zstd_level;
2378}
2379
Orit Wasserman17ad9b32012-08-06 21:42:53 +03002380int migrate_use_xbzrle(void)
2381{
2382 MigrationState *s;
2383
2384 s = migrate_get_current();
2385
2386 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
2387}
2388
2389int64_t migrate_xbzrle_cache_size(void)
2390{
2391 MigrationState *s;
2392
2393 s = migrate_get_current();
2394
Juan Quintela73af8dd2017-10-05 21:30:10 +02002395 return s->parameters.xbzrle_cache_size;
Orit Wasserman17ad9b32012-08-06 21:42:53 +03002396}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002397
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01002398static int64_t migrate_max_postcopy_bandwidth(void)
2399{
2400 MigrationState *s;
2401
2402 s = migrate_get_current();
2403
2404 return s->parameters.max_postcopy_bandwidth;
2405}
2406
Juan Quintela2833c592017-04-05 18:32:37 +02002407bool migrate_use_block(void)
2408{
2409 MigrationState *s;
2410
2411 s = migrate_get_current();
2412
2413 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
2414}
2415
Peter Xuc788ada2017-06-26 18:28:55 +08002416bool migrate_use_return_path(void)
2417{
2418 MigrationState *s;
2419
2420 s = migrate_get_current();
2421
2422 return s->enabled_capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
2423}
2424
Juan Quintela2833c592017-04-05 18:32:37 +02002425bool migrate_use_block_incremental(void)
2426{
2427 MigrationState *s;
2428
2429 s = migrate_get_current();
2430
2431 return s->parameters.block_incremental;
2432}
2433
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002434/* migration thread support */
2435/*
2436 * Something bad happened to the RP stream, mark an error
2437 * The caller shall print or trace something to indicate why
2438 */
2439static void mark_source_rp_bad(MigrationState *s)
2440{
2441 s->rp_state.error = true;
2442}
2443
2444static struct rp_cmd_args {
2445 ssize_t len; /* -1 = variable */
2446 const char *name;
2447} rp_cmd_args[] = {
2448 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
2449 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
2450 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002451 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
2452 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Peter Xua335deb2018-05-02 18:47:28 +08002453 [MIG_RP_MSG_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" },
Peter Xu13955b82018-05-02 18:47:30 +08002454 [MIG_RP_MSG_RESUME_ACK] = { .len = 4, .name = "RESUME_ACK" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002455 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
2456};
2457
2458/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002459 * Process a request for pages received on the return path,
2460 * We're allowed to send more than requested (e.g. to round to our page size)
2461 * and we don't need to send pages that have already been sent.
2462 */
2463static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
2464 ram_addr_t start, size_t len)
2465{
Wei Yang038adc22019-10-13 10:11:45 +08002466 long our_host_ps = qemu_real_host_page_size;
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00002467
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002468 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00002469
2470 /*
2471 * Since we currently insist on matching page sizes, just sanity check
2472 * we're being asked for whole host pages.
2473 */
2474 if (start & (our_host_ps-1) ||
2475 (len & (our_host_ps-1))) {
2476 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
2477 " len: %zd", __func__, start, len);
2478 mark_source_rp_bad(ms);
2479 return;
2480 }
2481
Juan Quintela96506892017-03-14 18:41:03 +01002482 if (ram_save_queue_pages(rbname, start, len)) {
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00002483 mark_source_rp_bad(ms);
2484 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002485}
2486
Peter Xu14b17422018-05-02 18:47:21 +08002487/* Return true to retry, false to quit */
2488static bool postcopy_pause_return_path_thread(MigrationState *s)
2489{
2490 trace_postcopy_pause_return_path();
2491
2492 qemu_sem_wait(&s->postcopy_pause_rp_sem);
2493
2494 trace_postcopy_pause_return_path_continued();
2495
2496 return true;
2497}
2498
Peter Xua335deb2018-05-02 18:47:28 +08002499static int migrate_handle_rp_recv_bitmap(MigrationState *s, char *block_name)
2500{
2501 RAMBlock *block = qemu_ram_block_by_name(block_name);
2502
2503 if (!block) {
2504 error_report("%s: invalid block name '%s'", __func__, block_name);
2505 return -EINVAL;
2506 }
2507
2508 /* Fetch the received bitmap and refresh the dirty bitmap */
2509 return ram_dirty_bitmap_reload(s, block);
2510}
2511
Peter Xu13955b82018-05-02 18:47:30 +08002512static int migrate_handle_rp_resume_ack(MigrationState *s, uint32_t value)
2513{
2514 trace_source_return_path_thread_resume_ack(value);
2515
2516 if (value != MIGRATION_RESUME_ACK_VALUE) {
2517 error_report("%s: illegal resume_ack value %"PRIu32,
2518 __func__, value);
2519 return -1;
2520 }
2521
2522 /* Now both sides are active. */
2523 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
2524 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2525
Peter Xu94190692018-05-02 18:47:34 +08002526 /* Notify send thread that time to continue send pages */
2527 qemu_sem_post(&s->rp_state.rp_sem);
Peter Xu13955b82018-05-02 18:47:30 +08002528
2529 return 0;
2530}
2531
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002532/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002533 * Handles messages sent on the return path towards the source VM
2534 *
2535 */
2536static void *source_return_path_thread(void *opaque)
2537{
2538 MigrationState *ms = opaque;
2539 QEMUFile *rp = ms->rp_state.from_dst_file;
2540 uint16_t header_len, header_type;
Peter Xu568b01c2016-03-09 14:12:12 +08002541 uint8_t buf[512];
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002542 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002543 ram_addr_t start = 0; /* =0 to silence warning */
2544 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002545 int res;
2546
2547 trace_source_return_path_thread_entry();
Lidong Chen74637e62018-08-06 21:29:29 +08002548 rcu_register_thread();
Peter Xu14b17422018-05-02 18:47:21 +08002549
2550retry:
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002551 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
2552 migration_is_setup_or_active(ms->state)) {
2553 trace_source_return_path_thread_loop_top();
2554 header_type = qemu_get_be16(rp);
2555 header_len = qemu_get_be16(rp);
2556
Peter Xu7a9ddfb2018-02-08 18:31:05 +08002557 if (qemu_file_get_error(rp)) {
2558 mark_source_rp_bad(ms);
2559 goto out;
2560 }
2561
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002562 if (header_type >= MIG_RP_MSG_MAX ||
2563 header_type == MIG_RP_MSG_INVALID) {
2564 error_report("RP: Received invalid message 0x%04x length 0x%04x",
Mao Zhongyied8b2822020-03-28 01:19:08 +08002565 header_type, header_len);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002566 mark_source_rp_bad(ms);
2567 goto out;
2568 }
2569
2570 if ((rp_cmd_args[header_type].len != -1 &&
2571 header_len != rp_cmd_args[header_type].len) ||
Peter Xu568b01c2016-03-09 14:12:12 +08002572 header_len > sizeof(buf)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002573 error_report("RP: Received '%s' message (0x%04x) with"
Mao Zhongyied8b2822020-03-28 01:19:08 +08002574 "incorrect length %d expecting %zu",
2575 rp_cmd_args[header_type].name, header_type, header_len,
2576 (size_t)rp_cmd_args[header_type].len);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002577 mark_source_rp_bad(ms);
2578 goto out;
2579 }
2580
2581 /* We know we've got a valid header by this point */
2582 res = qemu_get_buffer(rp, buf, header_len);
2583 if (res != header_len) {
2584 error_report("RP: Failed reading data for message 0x%04x"
2585 " read %d expected %d",
2586 header_type, res, header_len);
2587 mark_source_rp_bad(ms);
2588 goto out;
2589 }
2590
2591 /* OK, we have the message and the data */
2592 switch (header_type) {
2593 case MIG_RP_MSG_SHUT:
Peter Maydell4d885132016-06-10 17:09:22 +01002594 sibling_error = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002595 trace_source_return_path_thread_shut(sibling_error);
2596 if (sibling_error) {
2597 error_report("RP: Sibling indicated error %d", sibling_error);
2598 mark_source_rp_bad(ms);
2599 }
2600 /*
2601 * We'll let the main thread deal with closing the RP
2602 * we could do a shutdown(2) on it, but we're the only user
2603 * anyway, so there's nothing gained.
2604 */
2605 goto out;
2606
2607 case MIG_RP_MSG_PONG:
Peter Maydell4d885132016-06-10 17:09:22 +01002608 tmp32 = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002609 trace_source_return_path_thread_pong(tmp32);
2610 break;
2611
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002612 case MIG_RP_MSG_REQ_PAGES:
Peter Maydell4d885132016-06-10 17:09:22 +01002613 start = ldq_be_p(buf);
2614 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002615 migrate_handle_rp_req_pages(ms, NULL, start, len);
2616 break;
2617
2618 case MIG_RP_MSG_REQ_PAGES_ID:
2619 expected_len = 12 + 1; /* header + termination */
2620
2621 if (header_len >= expected_len) {
Peter Maydell4d885132016-06-10 17:09:22 +01002622 start = ldq_be_p(buf);
2623 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002624 /* Now we expect an idstr */
2625 tmp32 = buf[12]; /* Length of the following idstr */
2626 buf[13 + tmp32] = '\0';
2627 expected_len += tmp32;
2628 }
2629 if (header_len != expected_len) {
2630 error_report("RP: Req_Page_id with length %d expecting %zd",
Mao Zhongyied8b2822020-03-28 01:19:08 +08002631 header_len, expected_len);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002632 mark_source_rp_bad(ms);
2633 goto out;
2634 }
2635 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
2636 break;
2637
Peter Xua335deb2018-05-02 18:47:28 +08002638 case MIG_RP_MSG_RECV_BITMAP:
2639 if (header_len < 1) {
2640 error_report("%s: missing block name", __func__);
2641 mark_source_rp_bad(ms);
2642 goto out;
2643 }
2644 /* Format: len (1B) + idstr (<255B). This ends the idstr. */
2645 buf[buf[0] + 1] = '\0';
2646 if (migrate_handle_rp_recv_bitmap(ms, (char *)(buf + 1))) {
2647 mark_source_rp_bad(ms);
2648 goto out;
2649 }
2650 break;
2651
Peter Xu13955b82018-05-02 18:47:30 +08002652 case MIG_RP_MSG_RESUME_ACK:
2653 tmp32 = ldl_be_p(buf);
2654 if (migrate_handle_rp_resume_ack(ms, tmp32)) {
2655 mark_source_rp_bad(ms);
2656 goto out;
2657 }
2658 break;
2659
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002660 default:
2661 break;
2662 }
2663 }
Peter Xu14b17422018-05-02 18:47:21 +08002664
2665out:
2666 res = qemu_file_get_error(rp);
2667 if (res) {
Dr. David Alan Gilbertfb14a422019-10-07 11:35:07 +01002668 if (res == -EIO && migration_in_postcopy()) {
Peter Xu14b17422018-05-02 18:47:21 +08002669 /*
2670 * Maybe there is something we can do: it looks like a
2671 * network down issue, and we pause for a recovery.
2672 */
2673 if (postcopy_pause_return_path_thread(ms)) {
2674 /* Reload rp, reset the rest */
Marc-André Lureau0284a2a2018-09-25 13:22:45 +04002675 if (rp != ms->rp_state.from_dst_file) {
2676 qemu_fclose(rp);
2677 rp = ms->rp_state.from_dst_file;
2678 }
Peter Xu14b17422018-05-02 18:47:21 +08002679 ms->rp_state.error = false;
2680 goto retry;
2681 }
2682 }
2683
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002684 trace_source_return_path_thread_bad_end();
2685 mark_source_rp_bad(ms);
2686 }
2687
2688 trace_source_return_path_thread_end();
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002689 ms->rp_state.from_dst_file = NULL;
2690 qemu_fclose(rp);
Lidong Chen74637e62018-08-06 21:29:29 +08002691 rcu_unregister_thread();
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002692 return NULL;
2693}
2694
Peter Xud3e35b82018-05-02 18:47:24 +08002695static int open_return_path_on_source(MigrationState *ms,
2696 bool create_thread)
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002697{
2698
zhanghailiang89a02a92016-01-15 11:37:42 +08002699 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002700 if (!ms->rp_state.from_dst_file) {
2701 return -1;
2702 }
2703
2704 trace_open_return_path_on_source();
Peter Xud3e35b82018-05-02 18:47:24 +08002705
2706 if (!create_thread) {
2707 /* We're done */
2708 return 0;
2709 }
2710
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002711 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
2712 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
2713
2714 trace_open_return_path_on_source_continue();
2715
2716 return 0;
2717}
2718
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002719/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
2720static int await_return_path_close_on_source(MigrationState *ms)
2721{
2722 /*
2723 * If this is a normal exit then the destination will send a SHUT and the
2724 * rp_thread will exit, however if there's an error we need to cause
2725 * it to exit.
2726 */
zhanghailiang89a02a92016-01-15 11:37:42 +08002727 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002728 /*
2729 * shutdown(2), if we have it, will cause it to unblock if it's stuck
2730 * waiting for the destination.
2731 */
2732 qemu_file_shutdown(ms->rp_state.from_dst_file);
2733 mark_source_rp_bad(ms);
2734 }
2735 trace_await_return_path_close_on_source_joining();
2736 qemu_thread_join(&ms->rp_state.rp_thread);
2737 trace_await_return_path_close_on_source_close();
2738 return ms->rp_state.error;
2739}
2740
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002741/*
2742 * Switch from normal iteration to postcopy
2743 * Returns non-0 on error
2744 */
Peter Xu7287cbd2018-01-03 20:20:09 +08002745static int postcopy_start(MigrationState *ms)
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002746{
2747 int ret;
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002748 QIOChannelBuffer *bioc;
2749 QEMUFile *fb;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002750 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01002751 int64_t bandwidth = migrate_max_postcopy_bandwidth();
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002752 bool restart_block = false;
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002753 int cur_state = MIGRATION_STATUS_ACTIVE;
2754 if (!migrate_pause_before_switchover()) {
2755 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
2756 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2757 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002758
2759 trace_postcopy_start();
2760 qemu_mutex_lock_iothread();
2761 trace_postcopy_start_set_run();
2762
Daniel Henrique Barbozafb064112018-12-05 17:47:01 -02002763 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002764 global_state_store();
2765 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01002766 if (ret < 0) {
2767 goto fail;
2768 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002769
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002770 ret = migration_maybe_pause(ms, &cur_state,
2771 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2772 if (ret < 0) {
2773 goto fail;
2774 }
2775
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01002776 ret = bdrv_inactivate_all();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002777 if (ret < 0) {
2778 goto fail;
2779 }
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002780 restart_block = true;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002781
2782 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00002783 * Cause any non-postcopiable, but iterative devices to
2784 * send out their final data.
2785 */
Fam Zhenga1fbe752017-06-17 00:06:58 +08002786 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00002787
2788 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002789 * in Finish migrate and with the io-lock held everything should
2790 * be quiet, but we've potentially still got dirty pages and we
2791 * need to tell the destination to throw any pages it's already received
2792 * that are dirty
2793 */
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002794 if (migrate_postcopy_ram()) {
2795 if (ram_postcopy_send_discard_bitmap(ms)) {
2796 error_report("postcopy send discard bitmap failed");
2797 goto fail;
2798 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002799 }
2800
2801 /*
2802 * send rest of state - note things that are doing postcopy
2803 * will notice we're in POSTCOPY_ACTIVE and not actually
2804 * wrap their state up here
2805 */
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01002806 /* 0 max-postcopy-bandwidth means unlimited */
2807 if (!bandwidth) {
2808 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
2809 } else {
2810 qemu_file_set_rate_limit(ms->to_dst_file, bandwidth / XFER_LIMIT_RATIO);
2811 }
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002812 if (migrate_postcopy_ram()) {
2813 /* Ping just for debugging, helps line traces up */
2814 qemu_savevm_send_ping(ms->to_dst_file, 2);
2815 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002816
2817 /*
2818 * While loading the device state we may trigger page transfer
2819 * requests and the fd must be free to process those, and thus
2820 * the destination must read the whole device state off the fd before
2821 * it starts processing it. Unfortunately the ad-hoc migration format
2822 * doesn't allow the destination to know the size to read without fully
2823 * parsing it through each devices load-state code (especially the open
2824 * coded devices that use get/put).
2825 * So we wrap the device state up in a package with a length at the start;
2826 * to do this we use a qemu_buf to hold the whole of the device state.
2827 */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002828 bioc = qio_channel_buffer_new(4096);
Daniel P. Berrange6f01f132016-09-30 11:57:14 +01002829 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002830 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
2831 object_unref(OBJECT(bioc));
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002832
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00002833 /*
2834 * Make sure the receiver can get incoming pages before we send the rest
2835 * of the state
2836 */
2837 qemu_savevm_send_postcopy_listen(fb);
2838
Fam Zhenga1fbe752017-06-17 00:06:58 +08002839 qemu_savevm_state_complete_precopy(fb, false, false);
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002840 if (migrate_postcopy_ram()) {
2841 qemu_savevm_send_ping(fb, 3);
2842 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002843
2844 qemu_savevm_send_postcopy_run(fb);
2845
2846 /* <><> end of stuff going into the package */
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002847
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002848 /* Last point of recovery; as soon as we send the package the destination
2849 * can open devices and potentially start running.
2850 * Lets just check again we've not got any errors.
2851 */
2852 ret = qemu_file_get_error(ms->to_dst_file);
2853 if (ret) {
2854 error_report("postcopy_start: Migration stream errored (pre package)");
2855 goto fail_closefb;
2856 }
2857
2858 restart_block = false;
2859
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002860 /* Now send that blob */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002861 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002862 goto fail_closefb;
2863 }
2864 qemu_fclose(fb);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00002865
2866 /* Send a notify to give a chance for anything that needs to happen
2867 * at the transition to postcopy and after the device state; in particular
2868 * spice needs to trigger a transition now
2869 */
2870 ms->postcopy_after_devices = true;
2871 notifier_list_notify(&migration_state_notifiers, ms);
2872
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002873 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
2874
2875 qemu_mutex_unlock_iothread();
2876
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002877 if (migrate_postcopy_ram()) {
2878 /*
2879 * Although this ping is just for debug, it could potentially be
2880 * used for getting a better measurement of downtime at the source.
2881 */
2882 qemu_savevm_send_ping(ms->to_dst_file, 4);
2883 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002884
Pavel Butsykinced1c612017-02-03 18:23:21 +03002885 if (migrate_release_ram()) {
2886 ram_postcopy_migrated_memory_release(ms);
2887 }
2888
zhanghailiang89a02a92016-01-15 11:37:42 +08002889 ret = qemu_file_get_error(ms->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002890 if (ret) {
2891 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00002892 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002893 MIGRATION_STATUS_FAILED);
2894 }
2895
2896 return ret;
2897
2898fail_closefb:
2899 qemu_fclose(fb);
2900fail:
zhanghailiang48781e52015-12-16 11:47:33 +00002901 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002902 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002903 if (restart_block) {
2904 /* A failure happened early enough that we know the destination hasn't
2905 * accessed block devices, so we're safe to recover.
2906 */
2907 Error *local_err = NULL;
2908
2909 bdrv_invalidate_cache_all(&local_err);
2910 if (local_err) {
2911 error_report_err(local_err);
2912 }
2913 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002914 qemu_mutex_unlock_iothread();
2915 return -1;
2916}
2917
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002918/**
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002919 * migration_maybe_pause: Pause if required to by
2920 * migrate_pause_before_switchover called with the iothread locked
2921 * Returns: 0 on success
2922 */
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002923static int migration_maybe_pause(MigrationState *s,
2924 int *current_active_state,
2925 int new_state)
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002926{
2927 if (!migrate_pause_before_switchover()) {
2928 return 0;
2929 }
2930
2931 /* Since leaving this state is not atomic with posting the semaphore
2932 * it's possible that someone could have issued multiple migrate_continue
2933 * and the semaphore is incorrectly positive at this point;
2934 * the docs say it's undefined to reinit a semaphore that's already
2935 * init'd, so use timedwait to eat up any existing posts.
2936 */
2937 while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) {
2938 /* This block intentionally left blank */
2939 }
2940
Zhimin Feng89583382020-01-14 17:43:09 +08002941 /*
2942 * If the migration is cancelled when it is in the completion phase,
2943 * the migration state is set to MIGRATION_STATUS_CANCELLING.
2944 * So we don't need to wait a semaphore, otherwise we would always
2945 * wait for the 'pause_sem' semaphore.
2946 */
2947 if (s->state != MIGRATION_STATUS_CANCELLING) {
2948 qemu_mutex_unlock_iothread();
2949 migrate_set_state(&s->state, *current_active_state,
2950 MIGRATION_STATUS_PRE_SWITCHOVER);
2951 qemu_sem_wait(&s->pause_sem);
2952 migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER,
2953 new_state);
2954 *current_active_state = new_state;
2955 qemu_mutex_lock_iothread();
2956 }
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002957
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002958 return s->state == new_state ? 0 : -EINVAL;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002959}
2960
2961/**
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002962 * migration_completion: Used by migration_thread when there's not much left.
2963 * The caller 'breaks' the loop when this returns.
2964 *
2965 * @s: Current migration state
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002966 */
Peter Xu2ad87302018-01-03 20:20:14 +08002967static void migration_completion(MigrationState *s)
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002968{
2969 int ret;
Peter Xu2ad87302018-01-03 20:20:14 +08002970 int current_active_state = s->state;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002971
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002972 if (s->state == MIGRATION_STATUS_ACTIVE) {
2973 qemu_mutex_lock_iothread();
Peter Xu64909f92018-01-03 20:20:10 +08002974 s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Daniel Henrique Barbozafb064112018-12-05 17:47:01 -02002975 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
Peter Xu7287cbd2018-01-03 20:20:09 +08002976 s->vm_was_running = runstate_is_running();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002977 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002978
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002979 if (!ret) {
Fam Zhenga1fbe752017-06-17 00:06:58 +08002980 bool inactivate = !migrate_colo_enabled();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002981 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02002982 if (ret >= 0) {
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002983 ret = migration_maybe_pause(s, &current_active_state,
2984 MIGRATION_STATUS_DEVICE);
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002985 }
2986 if (ret >= 0) {
Kevin Wolff07fa4c2017-05-22 17:10:38 +02002987 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
Fam Zhenga1fbe752017-06-17 00:06:58 +08002988 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
2989 inactivate);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02002990 }
Fam Zhenga1fbe752017-06-17 00:06:58 +08002991 if (inactivate && ret >= 0) {
2992 s->block_inactive = true;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002993 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002994 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002995 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002996
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002997 if (ret < 0) {
2998 goto fail;
2999 }
3000 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
3001 trace_migration_completion_postcopy_end();
3002
zhanghailiang89a02a92016-01-15 11:37:42 +08003003 qemu_savevm_state_complete_postcopy(s->to_dst_file);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003004 trace_migration_completion_postcopy_end_after_complete();
3005 }
3006
3007 /*
3008 * If rp was opened we must clean up the thread before
3009 * cleaning everything else up (since if there are no failures
3010 * it will wait for the destination to send it's status in
3011 * a SHUT command).
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003012 */
Peter Xu0425dc92017-05-31 18:35:34 +08003013 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003014 int rp_error;
Peter Xu0425dc92017-05-31 18:35:34 +08003015 trace_migration_return_path_end_before();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003016 rp_error = await_return_path_close_on_source(s);
Peter Xu0425dc92017-05-31 18:35:34 +08003017 trace_migration_return_path_end_after(rp_error);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003018 if (rp_error) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02003019 goto fail_invalidate;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003020 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003021 }
3022
zhanghailiang89a02a92016-01-15 11:37:42 +08003023 if (qemu_file_get_error(s->to_dst_file)) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003024 trace_migration_completion_file_err();
Greg Kurzfe904ea2016-05-18 15:44:36 +02003025 goto fail_invalidate;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003026 }
3027
zhanghailiang0b827d52016-10-27 14:42:54 +08003028 if (!migrate_colo_enabled()) {
3029 migrate_set_state(&s->state, current_active_state,
3030 MIGRATION_STATUS_COMPLETED);
3031 }
3032
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003033 return;
3034
Greg Kurzfe904ea2016-05-18 15:44:36 +02003035fail_invalidate:
3036 /* If not doing postcopy, vm_start() will be called: let's regain
3037 * control on images.
3038 */
Dr. David Alan Gilbert6039dd52018-02-05 09:13:37 +00003039 if (s->state == MIGRATION_STATUS_ACTIVE ||
3040 s->state == MIGRATION_STATUS_DEVICE) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02003041 Error *local_err = NULL;
3042
zhanghailiang1d2acc32017-01-24 15:59:52 +08003043 qemu_mutex_lock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02003044 bdrv_invalidate_cache_all(&local_err);
3045 if (local_err) {
3046 error_report_err(local_err);
zhanghailiang1d2acc32017-01-24 15:59:52 +08003047 } else {
3048 s->block_inactive = false;
Greg Kurzfe904ea2016-05-18 15:44:36 +02003049 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08003050 qemu_mutex_unlock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02003051 }
3052
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003053fail:
zhanghailiang48781e52015-12-16 11:47:33 +00003054 migrate_set_state(&s->state, current_active_state,
3055 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003056}
3057
zhanghailiang35a6ed42016-10-27 14:42:52 +08003058bool migrate_colo_enabled(void)
3059{
3060 MigrationState *s = migrate_get_current();
3061 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
3062}
3063
Peter Xub23c2ad2018-05-02 18:47:19 +08003064typedef enum MigThrError {
3065 /* No error detected */
3066 MIG_THR_ERR_NONE = 0,
3067 /* Detected error, but resumed successfully */
3068 MIG_THR_ERR_RECOVERED = 1,
3069 /* Detected fatal error, need to exit */
3070 MIG_THR_ERR_FATAL = 2,
3071} MigThrError;
3072
Peter Xu94190692018-05-02 18:47:34 +08003073static int postcopy_resume_handshake(MigrationState *s)
3074{
3075 qemu_savevm_send_postcopy_resume(s->to_dst_file);
3076
3077 while (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
3078 qemu_sem_wait(&s->rp_state.rp_sem);
3079 }
3080
3081 if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
3082 return 0;
3083 }
3084
3085 return -1;
3086}
3087
Peter Xu135b87b2018-05-02 18:47:25 +08003088/* Return zero if success, or <0 for error */
3089static int postcopy_do_resume(MigrationState *s)
3090{
Peter Xud1b8ead2018-05-02 18:47:31 +08003091 int ret;
3092
3093 /*
3094 * Call all the resume_prepare() hooks, so that modules can be
3095 * ready for the migration resume.
3096 */
3097 ret = qemu_savevm_state_resume_prepare(s);
3098 if (ret) {
3099 error_report("%s: resume_prepare() failure detected: %d",
3100 __func__, ret);
3101 return ret;
3102 }
3103
3104 /*
Peter Xu94190692018-05-02 18:47:34 +08003105 * Last handshake with destination on the resume (destination will
3106 * switch to postcopy-active afterwards)
Peter Xud1b8ead2018-05-02 18:47:31 +08003107 */
Peter Xu94190692018-05-02 18:47:34 +08003108 ret = postcopy_resume_handshake(s);
3109 if (ret) {
3110 error_report("%s: handshake failed: %d", __func__, ret);
3111 return ret;
3112 }
Peter Xud1b8ead2018-05-02 18:47:31 +08003113
Peter Xu135b87b2018-05-02 18:47:25 +08003114 return 0;
3115}
3116
Peter Xub23c2ad2018-05-02 18:47:19 +08003117/*
3118 * We don't return until we are in a safe state to continue current
3119 * postcopy migration. Returns MIG_THR_ERR_RECOVERED if recovered, or
3120 * MIG_THR_ERR_FATAL if unrecovery failure happened.
3121 */
3122static MigThrError postcopy_pause(MigrationState *s)
3123{
3124 assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
Peter Xub23c2ad2018-05-02 18:47:19 +08003125
Peter Xu135b87b2018-05-02 18:47:25 +08003126 while (true) {
Peter Xu62df0662018-05-02 18:47:38 +08003127 QEMUFile *file;
3128
Peter Xu135b87b2018-05-02 18:47:25 +08003129 migrate_set_state(&s->state, s->state,
3130 MIGRATION_STATUS_POSTCOPY_PAUSED);
Peter Xub23c2ad2018-05-02 18:47:19 +08003131
Peter Xu135b87b2018-05-02 18:47:25 +08003132 /* Current channel is possibly broken. Release it. */
3133 assert(s->to_dst_file);
Peter Xu62df0662018-05-02 18:47:38 +08003134 qemu_mutex_lock(&s->qemu_file_lock);
3135 file = s->to_dst_file;
Peter Xu135b87b2018-05-02 18:47:25 +08003136 s->to_dst_file = NULL;
Peter Xu62df0662018-05-02 18:47:38 +08003137 qemu_mutex_unlock(&s->qemu_file_lock);
3138
3139 qemu_file_shutdown(file);
3140 qemu_fclose(file);
Peter Xub23c2ad2018-05-02 18:47:19 +08003141
Peter Xu135b87b2018-05-02 18:47:25 +08003142 error_report("Detected IO failure for postcopy. "
3143 "Migration paused.");
3144
3145 /*
3146 * We wait until things fixed up. Then someone will setup the
3147 * status back for us.
3148 */
3149 while (s->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
3150 qemu_sem_wait(&s->postcopy_pause_sem);
3151 }
3152
3153 if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
3154 /* Woken up by a recover procedure. Give it a shot */
3155
3156 /*
3157 * Firstly, let's wake up the return path now, with a new
3158 * return path channel.
3159 */
3160 qemu_sem_post(&s->postcopy_pause_rp_sem);
3161
3162 /* Do the resume logic */
3163 if (postcopy_do_resume(s) == 0) {
3164 /* Let's continue! */
3165 trace_postcopy_pause_continued();
3166 return MIG_THR_ERR_RECOVERED;
3167 } else {
3168 /*
3169 * Something wrong happened during the recovery, let's
3170 * pause again. Pause is always better than throwing
3171 * data away.
3172 */
3173 continue;
3174 }
3175 } else {
3176 /* This is not right... Time to quit. */
3177 return MIG_THR_ERR_FATAL;
3178 }
Peter Xub23c2ad2018-05-02 18:47:19 +08003179 }
Peter Xub23c2ad2018-05-02 18:47:19 +08003180}
3181
3182static MigThrError migration_detect_error(MigrationState *s)
3183{
3184 int ret;
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003185 int state = s->state;
Yury Kotov3d661c82019-04-22 13:34:20 +03003186 Error *local_error = NULL;
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003187
3188 if (state == MIGRATION_STATUS_CANCELLING ||
3189 state == MIGRATION_STATUS_CANCELLED) {
3190 /* End the migration, but don't set the state to failed */
3191 return MIG_THR_ERR_FATAL;
3192 }
Peter Xub23c2ad2018-05-02 18:47:19 +08003193
3194 /* Try to detect any file errors */
Yury Kotov3d661c82019-04-22 13:34:20 +03003195 ret = qemu_file_get_error_obj(s->to_dst_file, &local_error);
Peter Xub23c2ad2018-05-02 18:47:19 +08003196 if (!ret) {
3197 /* Everything is fine */
Yury Kotov3d661c82019-04-22 13:34:20 +03003198 assert(!local_error);
Peter Xub23c2ad2018-05-02 18:47:19 +08003199 return MIG_THR_ERR_NONE;
3200 }
3201
Yury Kotov3d661c82019-04-22 13:34:20 +03003202 if (local_error) {
3203 migrate_set_error(s, local_error);
3204 error_free(local_error);
3205 }
3206
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003207 if (state == MIGRATION_STATUS_POSTCOPY_ACTIVE && ret == -EIO) {
Peter Xub23c2ad2018-05-02 18:47:19 +08003208 /*
3209 * For postcopy, we allow the network to be down for a
3210 * while. After that, it can be continued by a
3211 * recovery phase.
3212 */
3213 return postcopy_pause(s);
3214 } else {
3215 /*
3216 * For precopy (or postcopy with error outside IO), we fail
3217 * with no time.
3218 */
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003219 migrate_set_state(&s->state, state, MIGRATION_STATUS_FAILED);
Peter Xub23c2ad2018-05-02 18:47:19 +08003220 trace_migration_thread_file_err();
3221
3222 /* Time to stop the migration, now. */
3223 return MIG_THR_ERR_FATAL;
3224 }
3225}
3226
Wei Yang268dcd42019-09-12 10:49:57 +08003227/* How many bytes have we transferred since the beginning of the migration */
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003228static uint64_t migration_total_bytes(MigrationState *s)
3229{
Juan Quintelab9ee2f72016-01-15 11:40:13 +01003230 return qemu_ftell(s->to_dst_file) + ram_counters.multifd_bytes;
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003231}
3232
Peter Xucf011f02018-01-03 20:20:11 +08003233static void migration_calculate_complete(MigrationState *s)
3234{
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003235 uint64_t bytes = migration_total_bytes(s);
Peter Xucf011f02018-01-03 20:20:11 +08003236 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela6cde6fb2018-06-26 15:26:35 +02003237 int64_t transfer_time;
Peter Xucf011f02018-01-03 20:20:11 +08003238
3239 s->total_time = end_time - s->start_time;
3240 if (!s->downtime) {
3241 /*
3242 * It's still not set, so we are precopy migration. For
3243 * postcopy, downtime is calculated during postcopy_start().
3244 */
3245 s->downtime = end_time - s->downtime_start;
3246 }
3247
Juan Quintela6cde6fb2018-06-26 15:26:35 +02003248 transfer_time = s->total_time - s->setup_time;
3249 if (transfer_time) {
3250 s->mbps = ((double) bytes * 8.0) / transfer_time / 1000;
Peter Xucf011f02018-01-03 20:20:11 +08003251 }
3252}
3253
Ivan Ren87f3bd82019-08-02 18:18:41 +08003254static void update_iteration_initial_status(MigrationState *s)
3255{
3256 /*
3257 * Update these three fields at the same time to avoid mismatch info lead
3258 * wrong speed calculation.
3259 */
3260 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3261 s->iteration_initial_bytes = migration_total_bytes(s);
3262 s->iteration_initial_pages = ram_get_total_transferred_pages();
3263}
3264
Peter Xub15df1a2018-01-03 20:20:13 +08003265static void migration_update_counters(MigrationState *s,
3266 int64_t current_time)
3267{
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08003268 uint64_t transferred, transferred_pages, time_spent;
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003269 uint64_t current_bytes; /* bytes transferred since the beginning */
Peter Xub15df1a2018-01-03 20:20:13 +08003270 double bandwidth;
3271
3272 if (current_time < s->iteration_start_time + BUFFER_DELAY) {
3273 return;
3274 }
3275
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003276 current_bytes = migration_total_bytes(s);
3277 transferred = current_bytes - s->iteration_initial_bytes;
Peter Xub15df1a2018-01-03 20:20:13 +08003278 time_spent = current_time - s->iteration_start_time;
3279 bandwidth = (double)transferred / time_spent;
Wei Wang0781c1e2018-01-22 19:36:39 +08003280 s->threshold_size = bandwidth * s->parameters.downtime_limit;
Peter Xub15df1a2018-01-03 20:20:13 +08003281
3282 s->mbps = (((double) transferred * 8.0) /
3283 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
3284
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08003285 transferred_pages = ram_get_total_transferred_pages() -
3286 s->iteration_initial_pages;
3287 s->pages_per_second = (double) transferred_pages /
3288 (((double) time_spent / 1000.0));
3289
Peter Xub15df1a2018-01-03 20:20:13 +08003290 /*
3291 * if we haven't sent anything, we don't want to
3292 * recalculate. 10000 is a small enough number for our purposes
3293 */
3294 if (ram_counters.dirty_pages_rate && transferred > 10000) {
Balamuruhan S650af892018-06-12 14:20:09 +05303295 s->expected_downtime = ram_counters.remaining / bandwidth;
Peter Xub15df1a2018-01-03 20:20:13 +08003296 }
3297
3298 qemu_file_reset_rate_limit(s->to_dst_file);
3299
Ivan Ren87f3bd82019-08-02 18:18:41 +08003300 update_iteration_initial_status(s);
Peter Xub15df1a2018-01-03 20:20:13 +08003301
3302 trace_migrate_transferred(transferred, time_spent,
Wei Wang0781c1e2018-01-22 19:36:39 +08003303 bandwidth, s->threshold_size);
Peter Xub15df1a2018-01-03 20:20:13 +08003304}
3305
Peter Xu2ad87302018-01-03 20:20:14 +08003306/* Migration thread iteration status */
3307typedef enum {
3308 MIG_ITERATE_RESUME, /* Resume current iteration */
3309 MIG_ITERATE_SKIP, /* Skip current iteration */
3310 MIG_ITERATE_BREAK, /* Break the loop */
3311} MigIterateState;
3312
3313/*
3314 * Return true if continue to the next iteration directly, false
3315 * otherwise.
3316 */
3317static MigIterateState migration_iteration_run(MigrationState *s)
3318{
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04003319 uint64_t pending_size, pend_pre, pend_compat, pend_post;
Peter Xu2ad87302018-01-03 20:20:14 +08003320 bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE;
3321
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04003322 qemu_savevm_state_pending(s->to_dst_file, s->threshold_size, &pend_pre,
3323 &pend_compat, &pend_post);
3324 pending_size = pend_pre + pend_compat + pend_post;
Peter Xu2ad87302018-01-03 20:20:14 +08003325
3326 trace_migrate_pending(pending_size, s->threshold_size,
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04003327 pend_pre, pend_compat, pend_post);
Peter Xu2ad87302018-01-03 20:20:14 +08003328
3329 if (pending_size && pending_size >= s->threshold_size) {
3330 /* Still a significant amount to transfer */
Wei Yang52aec702019-07-18 16:37:47 +08003331 if (!in_postcopy && pend_pre <= s->threshold_size &&
Peter Xu2ad87302018-01-03 20:20:14 +08003332 atomic_read(&s->start_postcopy)) {
3333 if (postcopy_start(s)) {
3334 error_report("%s: postcopy failed to start", __func__);
3335 }
3336 return MIG_ITERATE_SKIP;
3337 }
3338 /* Just another iteration step */
Wei Yang17d93512019-10-06 06:05:16 +08003339 qemu_savevm_state_iterate(s->to_dst_file, in_postcopy);
Peter Xu2ad87302018-01-03 20:20:14 +08003340 } else {
3341 trace_migration_thread_low_pending(pending_size);
3342 migration_completion(s);
3343 return MIG_ITERATE_BREAK;
3344 }
3345
3346 return MIG_ITERATE_RESUME;
3347}
3348
Peter Xu199aa6d2018-01-03 20:20:15 +08003349static void migration_iteration_finish(MigrationState *s)
3350{
3351 /* If we enabled cpu throttling for auto-converge, turn it off. */
3352 cpu_throttle_stop();
3353
3354 qemu_mutex_lock_iothread();
3355 switch (s->state) {
3356 case MIGRATION_STATUS_COMPLETED:
3357 migration_calculate_complete(s);
3358 runstate_set(RUN_STATE_POSTMIGRATE);
3359 break;
3360
3361 case MIGRATION_STATUS_ACTIVE:
3362 /*
3363 * We should really assert here, but since it's during
3364 * migration, let's try to reduce the usage of assertions.
3365 */
3366 if (!migrate_colo_enabled()) {
3367 error_report("%s: critical error: calling COLO code without "
3368 "COLO enabled", __func__);
3369 }
3370 migrate_start_colo_process(s);
3371 /*
3372 * Fixme: we will run VM in COLO no matter its old running state.
3373 * After exited COLO, we will keep running.
3374 */
3375 s->vm_was_running = true;
3376 /* Fallthrough */
3377 case MIGRATION_STATUS_FAILED:
3378 case MIGRATION_STATUS_CANCELLED:
Dr. David Alan Gilbert57225e52018-07-19 10:22:57 +01003379 case MIGRATION_STATUS_CANCELLING:
Peter Xu199aa6d2018-01-03 20:20:15 +08003380 if (s->vm_was_running) {
3381 vm_start();
3382 } else {
3383 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
3384 runstate_set(RUN_STATE_POSTMIGRATE);
3385 }
3386 }
3387 break;
3388
3389 default:
3390 /* Should not reach here, but if so, forgive the VM. */
3391 error_report("%s: Unknown ending state %d", __func__, s->state);
3392 break;
3393 }
Yury Kotovfd392cf2019-04-08 14:33:43 +03003394 migrate_fd_cleanup_schedule(s);
Peter Xu199aa6d2018-01-03 20:20:15 +08003395 qemu_mutex_unlock_iothread();
3396}
3397
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003398void migration_make_urgent_request(void)
3399{
3400 qemu_sem_post(&migrate_get_current()->rate_limit_sem);
3401}
3402
3403void migration_consume_urgent_request(void)
3404{
3405 qemu_sem_wait(&migrate_get_current()->rate_limit_sem);
3406}
3407
Dr. David Alan Gilbert97e1e062019-12-05 10:29:18 +00003408/* Returns true if the rate limiting was broken by an urgent request */
3409bool migration_rate_limit(void)
3410{
3411 int64_t now = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3412 MigrationState *s = migrate_get_current();
3413
3414 bool urgent = false;
3415 migration_update_counters(s, now);
3416 if (qemu_file_rate_limit(s->to_dst_file)) {
Lukas Straub77386122020-05-20 22:42:32 +02003417
3418 if (qemu_file_get_error(s->to_dst_file)) {
3419 return false;
3420 }
Dr. David Alan Gilbert97e1e062019-12-05 10:29:18 +00003421 /*
3422 * Wait for a delay to do rate limiting OR
3423 * something urgent to post the semaphore.
3424 */
3425 int ms = s->iteration_start_time + BUFFER_DELAY - now;
3426 trace_migration_rate_limit_pre(ms);
3427 if (qemu_sem_timedwait(&s->rate_limit_sem, ms) == 0) {
3428 /*
3429 * We were woken by one or more urgent things but
3430 * the timedwait will have consumed one of them.
3431 * The service routine for the urgent wake will dec
3432 * the semaphore itself for each item it consumes,
3433 * so add this one we just eat back.
3434 */
3435 qemu_sem_post(&s->rate_limit_sem);
3436 urgent = true;
3437 }
3438 trace_migration_rate_limit_post(urgent);
3439 }
3440 return urgent;
3441}
3442
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00003443/*
3444 * Master migration thread on the source VM.
3445 * It drives the migration and pumps the data down the outgoing channel.
3446 */
Juan Quintela5f496a12013-02-22 17:36:30 +01003447static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003448{
Juan Quintela9848a402012-12-19 09:55:50 +01003449 MigrationState *s = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +01003450 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Peter Xub23c2ad2018-05-02 18:47:19 +08003451 MigThrError thr_error;
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003452 bool urgent = false;
Juan Quintela76f59332012-10-03 20:16:24 +02003453
Paolo Bonziniab28bd22015-07-09 08:55:38 +02003454 rcu_register_thread();
3455
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +00003456 object_ref(OBJECT(s));
Ivan Ren87f3bd82019-08-02 18:18:41 +08003457 update_iteration_initial_status(s);
Peter Xub15df1a2018-01-03 20:20:13 +08003458
zhanghailiang89a02a92016-01-15 11:37:42 +08003459 qemu_savevm_state_header(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003460
Peter Xu62a02652017-06-14 15:55:58 +08003461 /*
3462 * If we opened the return path, we need to make sure dst has it
3463 * opened as well.
3464 */
3465 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003466 /* Now tell the dest that it should open its end so it can reply */
zhanghailiang89a02a92016-01-15 11:37:42 +08003467 qemu_savevm_send_open_return_path(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003468
3469 /* And do a ping that will make stuff easier to debug */
zhanghailiang89a02a92016-01-15 11:37:42 +08003470 qemu_savevm_send_ping(s->to_dst_file, 1);
Peter Xu0425dc92017-05-31 18:35:34 +08003471 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003472
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03003473 if (migrate_postcopy()) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003474 /*
3475 * Tell the destination that we *might* want to do postcopy later;
3476 * if the other end can't do postcopy it should fail now, nice and
3477 * early.
3478 */
zhanghailiang89a02a92016-01-15 11:37:42 +08003479 qemu_savevm_send_postcopy_advise(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003480 }
3481
Zhang Chenaad555c2018-09-03 12:38:47 +08003482 if (migrate_colo_enabled()) {
3483 /* Notify migration destination that we enable COLO */
3484 qemu_savevm_send_colo_enable(s->to_dst_file);
3485 }
3486
Juan Quintela9907e842017-06-28 11:52:24 +02003487 qemu_savevm_state_setup(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003488
Keqian Zhud05de9e2020-02-04 13:08:41 +08003489 if (qemu_savevm_state_guest_unplug_pending()) {
Jens Freimannc7e0acd2019-10-29 12:49:02 +01003490 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3491 MIGRATION_STATUS_WAIT_UNPLUG);
3492
3493 while (s->state == MIGRATION_STATUS_WAIT_UNPLUG &&
3494 qemu_savevm_state_guest_unplug_pending()) {
3495 qemu_sem_timedwait(&s->wait_unplug_sem, 250);
3496 }
3497
3498 migrate_set_state(&s->state, MIGRATION_STATUS_WAIT_UNPLUG,
3499 MIGRATION_STATUS_ACTIVE);
3500 }
3501
Alex Blighbc72ad62013-08-21 16:03:08 +01003502 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang48781e52015-12-16 11:47:33 +00003503 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3504 MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04003505
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00003506 trace_migration_thread_setup_complete();
3507
Wei Yang8f8d5282019-07-17 08:53:41 +08003508 while (migration_is_active(s)) {
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003509 if (urgent || !qemu_file_rate_limit(s->to_dst_file)) {
Peter Xu2ad87302018-01-03 20:20:14 +08003510 MigIterateState iter_state = migration_iteration_run(s);
3511 if (iter_state == MIG_ITERATE_SKIP) {
3512 continue;
3513 } else if (iter_state == MIG_ITERATE_BREAK) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003514 break;
Juan Quintelac369f402012-10-03 20:33:34 +02003515 }
3516 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01003517
Peter Xub23c2ad2018-05-02 18:47:19 +08003518 /*
3519 * Try to detect any kind of failures, and see whether we
3520 * should stop the migration now.
3521 */
3522 thr_error = migration_detect_error(s);
3523 if (thr_error == MIG_THR_ERR_FATAL) {
3524 /* Stop migration */
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01003525 break;
Peter Xub23c2ad2018-05-02 18:47:19 +08003526 } else if (thr_error == MIG_THR_ERR_RECOVERED) {
3527 /*
3528 * Just recovered from a e.g. network failure, reset all
3529 * the local variables. This is important to avoid
3530 * breaking transferred_bytes and bandwidth calculation
3531 */
Ivan Ren87f3bd82019-08-02 18:18:41 +08003532 update_iteration_initial_status(s);
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01003533 }
Peter Xub15df1a2018-01-03 20:20:13 +08003534
Dr. David Alan Gilbert97e1e062019-12-05 10:29:18 +00003535 urgent = migration_rate_limit();
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01003536 }
3537
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003538 trace_migration_thread_after_loop();
Peter Xu199aa6d2018-01-03 20:20:15 +08003539 migration_iteration_finish(s);
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +00003540 object_unref(OBJECT(s));
Paolo Bonziniab28bd22015-07-09 08:55:38 +02003541 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003542 return NULL;
3543}
3544
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00003545void migrate_fd_connect(MigrationState *s, Error *error_in)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003546{
Juan Quintela00f4b572019-06-12 11:33:27 +02003547 Error *local_err = NULL;
Peter Xud3e35b82018-05-02 18:47:24 +08003548 int64_t rate_limit;
3549 bool resume = s->state == MIGRATION_STATUS_POSTCOPY_PAUSED;
3550
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05303551 s->expected_downtime = s->parameters.downtime_limit;
Marc-André Lureau9cbc3642020-03-25 19:47:21 +01003552 if (resume) {
3553 assert(s->cleanup_bh);
3554 } else {
3555 assert(!s->cleanup_bh);
3556 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup_bh, s);
3557 }
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00003558 if (error_in) {
3559 migrate_fd_error(s, error_in);
3560 migrate_fd_cleanup(s);
3561 return;
3562 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003563
Peter Xud3e35b82018-05-02 18:47:24 +08003564 if (resume) {
3565 /* This is a resumed migration */
Peter Xu8504dde2019-09-06 21:01:03 +08003566 rate_limit = s->parameters.max_postcopy_bandwidth /
3567 XFER_LIMIT_RATIO;
Peter Xud3e35b82018-05-02 18:47:24 +08003568 } else {
3569 /* This is a fresh new migration */
3570 rate_limit = s->parameters.max_bandwidth / XFER_LIMIT_RATIO;
Paolo Bonzini442773c2013-02-22 17:36:44 +01003571
Peter Xud3e35b82018-05-02 18:47:24 +08003572 /* Notify before starting migration thread */
3573 notifier_list_notify(&migration_state_notifiers, s);
3574 }
3575
3576 qemu_file_set_rate_limit(s->to_dst_file, rate_limit);
3577 qemu_file_set_blocking(s->to_dst_file, true);
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02003578
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003579 /*
Peter Xuc788ada2017-06-26 18:28:55 +08003580 * Open the return path. For postcopy, it is used exclusively. For
3581 * precopy, only if user specified "return-path" capability would
3582 * QEMU uses the return path.
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003583 */
Peter Xuc788ada2017-06-26 18:28:55 +08003584 if (migrate_postcopy_ram() || migrate_use_return_path()) {
Peter Xud3e35b82018-05-02 18:47:24 +08003585 if (open_return_path_on_source(s, !resume)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003586 error_report("Unable to open return-path for postcopy");
Peter Xud3e35b82018-05-02 18:47:24 +08003587 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003588 migrate_fd_cleanup(s);
3589 return;
3590 }
3591 }
3592
Peter Xud3e35b82018-05-02 18:47:24 +08003593 if (resume) {
Peter Xu135b87b2018-05-02 18:47:25 +08003594 /* Wakeup the main migration thread to do the recovery */
3595 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
3596 MIGRATION_STATUS_POSTCOPY_RECOVER);
3597 qemu_sem_post(&s->postcopy_pause_sem);
Peter Xud3e35b82018-05-02 18:47:24 +08003598 return;
3599 }
3600
Juan Quintela00f4b572019-06-12 11:33:27 +02003601 if (multifd_save_setup(&local_err) != 0) {
3602 error_report_err(local_err);
Juan Quintelaf986c3d2016-01-14 16:52:55 +01003603 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3604 MIGRATION_STATUS_FAILED);
3605 migrate_fd_cleanup(s);
3606 return;
3607 }
Pankaj Gupta009fad72017-01-23 19:12:56 +05303608 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01003609 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003610 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003611}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00003612
Peter Xu9d18af92017-06-27 12:10:19 +08003613void migration_global_dump(Monitor *mon)
3614{
3615 MigrationState *ms = migrate_get_current();
3616
Juan Quintela6f0f6422017-10-26 11:49:57 +02003617 monitor_printf(mon, "globals:\n");
3618 monitor_printf(mon, "store-global-state: %s\n",
3619 ms->store_global_state ? "on" : "off");
3620 monitor_printf(mon, "only-migratable: %s\n",
Markus Armbruster811f8652019-04-01 11:08:24 +02003621 only_migratable ? "on" : "off");
Juan Quintela6f0f6422017-10-26 11:49:57 +02003622 monitor_printf(mon, "send-configuration: %s\n",
3623 ms->send_configuration ? "on" : "off");
3624 monitor_printf(mon, "send-section-footer: %s\n",
3625 ms->send_section_footer ? "on" : "off");
Xiao Guangrongf5482222018-05-03 16:06:11 +08003626 monitor_printf(mon, "decompress-error-check: %s\n",
3627 ms->decompress_error_check ? "on" : "off");
Peter Xu002cad62019-06-03 14:50:56 +08003628 monitor_printf(mon, "clear-bitmap-shift: %u\n",
3629 ms->clear_bitmap_shift);
Peter Xu9d18af92017-06-27 12:10:19 +08003630}
3631
Peter Xu20814752017-07-18 11:39:03 +08003632#define DEFINE_PROP_MIG_CAP(name, x) \
3633 DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
3634
Peter Xu52722982017-06-27 12:10:14 +08003635static Property migration_properties[] = {
3636 DEFINE_PROP_BOOL("store-global-state", MigrationState,
3637 store_global_state, true),
Peter Xu71dd4c12017-06-27 12:10:16 +08003638 DEFINE_PROP_BOOL("send-configuration", MigrationState,
3639 send_configuration, true),
Peter Xu15c38502017-06-27 12:10:17 +08003640 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
3641 send_section_footer, true),
Xiao Guangrongf5482222018-05-03 16:06:11 +08003642 DEFINE_PROP_BOOL("decompress-error-check", MigrationState,
3643 decompress_error_check, true),
Peter Xu002cad62019-06-03 14:50:56 +08003644 DEFINE_PROP_UINT8("x-clear-bitmap-shift", MigrationState,
3645 clear_bitmap_shift, CLEAR_BITMAP_SHIFT_DEFAULT),
Peter Xu89632fa2017-07-18 11:39:02 +08003646
3647 /* Migration parameters */
Juan Quintela741d4082017-12-01 13:08:38 +01003648 DEFINE_PROP_UINT8("x-compress-level", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003649 parameters.compress_level,
3650 DEFAULT_MIGRATE_COMPRESS_LEVEL),
Juan Quintela741d4082017-12-01 13:08:38 +01003651 DEFINE_PROP_UINT8("x-compress-threads", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003652 parameters.compress_threads,
3653 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
Xiao Guangrong1d588722018-08-21 16:10:20 +08003654 DEFINE_PROP_BOOL("x-compress-wait-thread", MigrationState,
3655 parameters.compress_wait_thread, true),
Juan Quintela741d4082017-12-01 13:08:38 +01003656 DEFINE_PROP_UINT8("x-decompress-threads", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003657 parameters.decompress_threads,
3658 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
Keqian Zhudc14a472020-02-24 10:31:42 +08003659 DEFINE_PROP_UINT8("x-throttle-trigger-threshold", MigrationState,
3660 parameters.throttle_trigger_threshold,
3661 DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD),
Juan Quintela741d4082017-12-01 13:08:38 +01003662 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003663 parameters.cpu_throttle_initial,
3664 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
Juan Quintela741d4082017-12-01 13:08:38 +01003665 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003666 parameters.cpu_throttle_increment,
3667 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
Keqian Zhucbbf8182020-04-13 18:15:08 +08003668 DEFINE_PROP_BOOL("x-cpu-throttle-tailslow", MigrationState,
3669 parameters.cpu_throttle_tailslow, false),
Juan Quintela741d4082017-12-01 13:08:38 +01003670 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003671 parameters.max_bandwidth, MAX_THROTTLE),
Juan Quintela741d4082017-12-01 13:08:38 +01003672 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003673 parameters.downtime_limit,
3674 DEFAULT_MIGRATE_SET_DOWNTIME),
Juan Quintela741d4082017-12-01 13:08:38 +01003675 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003676 parameters.x_checkpoint_delay,
3677 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
Juan Quintelacbfd6c92019-02-06 13:54:06 +01003678 DEFINE_PROP_UINT8("multifd-channels", MigrationState,
3679 parameters.multifd_channels,
Juan Quintela4075fb12016-01-15 08:56:17 +01003680 DEFAULT_MIGRATE_MULTIFD_CHANNELS),
Juan Quintela96eef042019-01-16 10:35:55 +01003681 DEFINE_PROP_MULTIFD_COMPRESSION("multifd-compression", MigrationState,
3682 parameters.multifd_compression,
3683 DEFAULT_MIGRATE_MULTIFD_COMPRESSION),
Juan Quintela9004db42020-01-23 17:08:52 +01003684 DEFINE_PROP_UINT8("multifd-zlib-level", MigrationState,
3685 parameters.multifd_zlib_level,
3686 DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL),
Juan Quintela6a9ad152020-01-23 17:41:36 +01003687 DEFINE_PROP_UINT8("multifd-zstd-level", MigrationState,
3688 parameters.multifd_zstd_level,
3689 DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL),
Juan Quintela73af8dd2017-10-05 21:30:10 +02003690 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
3691 parameters.xbzrle_cache_size,
3692 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01003693 DEFINE_PROP_SIZE("max-postcopy-bandwidth", MigrationState,
3694 parameters.max_postcopy_bandwidth,
3695 DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH),
Li Qiang4cbc9c72018-08-01 06:00:20 -07003696 DEFINE_PROP_UINT8("max-cpu-throttle", MigrationState,
3697 parameters.max_cpu_throttle,
3698 DEFAULT_MIGRATE_MAX_CPU_THROTTLE),
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00003699 DEFINE_PROP_SIZE("announce-initial", MigrationState,
3700 parameters.announce_initial,
3701 DEFAULT_MIGRATE_ANNOUNCE_INITIAL),
3702 DEFINE_PROP_SIZE("announce-max", MigrationState,
3703 parameters.announce_max,
3704 DEFAULT_MIGRATE_ANNOUNCE_MAX),
3705 DEFINE_PROP_SIZE("announce-rounds", MigrationState,
3706 parameters.announce_rounds,
3707 DEFAULT_MIGRATE_ANNOUNCE_ROUNDS),
3708 DEFINE_PROP_SIZE("announce-step", MigrationState,
3709 parameters.announce_step,
3710 DEFAULT_MIGRATE_ANNOUNCE_STEP),
Peter Xu20814752017-07-18 11:39:03 +08003711
3712 /* Migration capabilities */
3713 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
3714 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
3715 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
3716 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
3717 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
3718 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
3719 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
3720 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
3721 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
3722 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
3723 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
Juan Quintelacbfd6c92019-02-06 13:54:06 +01003724 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_MULTIFD),
Peter Xu20814752017-07-18 11:39:03 +08003725
Peter Xu52722982017-06-27 12:10:14 +08003726 DEFINE_PROP_END_OF_LIST(),
3727};
3728
Peter Xue5cb7e72017-06-27 12:10:13 +08003729static void migration_class_init(ObjectClass *klass, void *data)
3730{
3731 DeviceClass *dc = DEVICE_CLASS(klass);
3732
3733 dc->user_creatable = false;
Marc-André Lureau4f67d302020-01-10 19:30:32 +04003734 device_class_set_props(dc, migration_properties);
Peter Xue5cb7e72017-06-27 12:10:13 +08003735}
3736
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003737static void migration_instance_finalize(Object *obj)
3738{
3739 MigrationState *ms = MIGRATION_OBJ(obj);
3740 MigrationParameters *params = &ms->parameters;
3741
Juan Quintela87db1a72017-09-05 12:50:22 +02003742 qemu_mutex_destroy(&ms->error_mutex);
Peter Xu62df0662018-05-02 18:47:38 +08003743 qemu_mutex_destroy(&ms->qemu_file_lock);
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003744 g_free(params->tls_hostname);
3745 g_free(params->tls_creds);
Jens Freimannc7e0acd2019-10-29 12:49:02 +01003746 qemu_sem_destroy(&ms->wait_unplug_sem);
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003747 qemu_sem_destroy(&ms->rate_limit_sem);
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01003748 qemu_sem_destroy(&ms->pause_sem);
Peter Xub23c2ad2018-05-02 18:47:19 +08003749 qemu_sem_destroy(&ms->postcopy_pause_sem);
Peter Xu14b17422018-05-02 18:47:21 +08003750 qemu_sem_destroy(&ms->postcopy_pause_rp_sem);
Peter Xuedd090c2018-05-02 18:47:32 +08003751 qemu_sem_destroy(&ms->rp_state.rp_sem);
Marc-André Lureauab105cc2018-03-06 18:09:59 +01003752 error_free(ms->error);
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003753}
3754
Peter Xue5cb7e72017-06-27 12:10:13 +08003755static void migration_instance_init(Object *obj)
3756{
3757 MigrationState *ms = MIGRATION_OBJ(obj);
Peter Xu8b0b29d2017-07-18 11:39:06 +08003758 MigrationParameters *params = &ms->parameters;
Peter Xue5cb7e72017-06-27 12:10:13 +08003759
3760 ms->state = MIGRATION_STATUS_NONE;
Peter Xue5cb7e72017-06-27 12:10:13 +08003761 ms->mbps = -1;
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08003762 ms->pages_per_second = -1;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01003763 qemu_sem_init(&ms->pause_sem, 0);
Juan Quintela87db1a72017-09-05 12:50:22 +02003764 qemu_mutex_init(&ms->error_mutex);
Peter Xu8b0b29d2017-07-18 11:39:06 +08003765
3766 params->tls_hostname = g_strdup("");
3767 params->tls_creds = g_strdup("");
3768
3769 /* Set has_* up only for parameter checks */
3770 params->has_compress_level = true;
3771 params->has_compress_threads = true;
3772 params->has_decompress_threads = true;
Keqian Zhudc14a472020-02-24 10:31:42 +08003773 params->has_throttle_trigger_threshold = true;
Peter Xu8b0b29d2017-07-18 11:39:06 +08003774 params->has_cpu_throttle_initial = true;
3775 params->has_cpu_throttle_increment = true;
Keqian Zhucbbf8182020-04-13 18:15:08 +08003776 params->has_cpu_throttle_tailslow = true;
Peter Xu8b0b29d2017-07-18 11:39:06 +08003777 params->has_max_bandwidth = true;
3778 params->has_downtime_limit = true;
3779 params->has_x_checkpoint_delay = true;
3780 params->has_block_incremental = true;
Juan Quintelacbfd6c92019-02-06 13:54:06 +01003781 params->has_multifd_channels = true;
Juan Quintela96eef042019-01-16 10:35:55 +01003782 params->has_multifd_compression = true;
Juan Quintela9004db42020-01-23 17:08:52 +01003783 params->has_multifd_zlib_level = true;
Juan Quintela6a9ad152020-01-23 17:41:36 +01003784 params->has_multifd_zstd_level = true;
Juan Quintela73af8dd2017-10-05 21:30:10 +02003785 params->has_xbzrle_cache_size = true;
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01003786 params->has_max_postcopy_bandwidth = true;
Li Qiang4cbc9c72018-08-01 06:00:20 -07003787 params->has_max_cpu_throttle = true;
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00003788 params->has_announce_initial = true;
3789 params->has_announce_max = true;
3790 params->has_announce_rounds = true;
3791 params->has_announce_step = true;
Peter Xub23c2ad2018-05-02 18:47:19 +08003792
3793 qemu_sem_init(&ms->postcopy_pause_sem, 0);
Peter Xu14b17422018-05-02 18:47:21 +08003794 qemu_sem_init(&ms->postcopy_pause_rp_sem, 0);
Peter Xuedd090c2018-05-02 18:47:32 +08003795 qemu_sem_init(&ms->rp_state.rp_sem, 0);
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003796 qemu_sem_init(&ms->rate_limit_sem, 0);
Jens Freimannc7e0acd2019-10-29 12:49:02 +01003797 qemu_sem_init(&ms->wait_unplug_sem, 0);
Peter Xu62df0662018-05-02 18:47:38 +08003798 qemu_mutex_init(&ms->qemu_file_lock);
Peter Xu8b0b29d2017-07-18 11:39:06 +08003799}
3800
3801/*
3802 * Return true if check pass, false otherwise. Error will be put
3803 * inside errp if provided.
3804 */
3805static bool migration_object_check(MigrationState *ms, Error **errp)
3806{
Peter Xu6b19a7d2017-07-18 11:39:10 +08003807 MigrationCapabilityStatusList *head = NULL;
3808 /* Assuming all off */
3809 bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret;
3810 int i;
3811
Peter Xu8b0b29d2017-07-18 11:39:06 +08003812 if (!migrate_params_check(&ms->parameters, errp)) {
3813 return false;
3814 }
3815
Peter Xu6b19a7d2017-07-18 11:39:10 +08003816 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
3817 if (ms->enabled_capabilities[i]) {
3818 head = migrate_cap_add(head, i, true);
3819 }
3820 }
3821
3822 ret = migrate_caps_check(cap_list, head, errp);
3823
3824 /* It works with head == NULL */
3825 qapi_free_MigrationCapabilityStatusList(head);
3826
3827 return ret;
Peter Xue5cb7e72017-06-27 12:10:13 +08003828}
3829
3830static const TypeInfo migration_type = {
3831 .name = TYPE_MIGRATION,
Peter Xu01f6e142017-06-28 15:15:44 +08003832 /*
Peter Xuc8d3ff32017-07-05 16:21:23 +08003833 * NOTE: TYPE_MIGRATION is not really a device, as the object is
Markus Armbruster2194abd2020-06-10 07:32:19 +02003834 * not created using qdev_new(), it is not attached to the qdev
Peter Xuc8d3ff32017-07-05 16:21:23 +08003835 * device tree, and it is never realized.
3836 *
3837 * TODO: Make this TYPE_OBJECT once QOM provides something like
3838 * TYPE_DEVICE's "-global" properties.
Peter Xu01f6e142017-06-28 15:15:44 +08003839 */
Peter Xue5cb7e72017-06-27 12:10:13 +08003840 .parent = TYPE_DEVICE,
3841 .class_init = migration_class_init,
3842 .class_size = sizeof(MigrationClass),
3843 .instance_size = sizeof(MigrationState),
3844 .instance_init = migration_instance_init,
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003845 .instance_finalize = migration_instance_finalize,
Peter Xue5cb7e72017-06-27 12:10:13 +08003846};
3847
3848static void register_migration_types(void)
3849{
3850 type_register_static(&migration_type);
3851}
3852
3853type_init(register_migration_types);