blob: 838ca79f577c89b1885c5a4850cbae509e0de797 [file] [log] [blame]
aliguori5bb79102008-10-13 03:12:02 +00001/*
2 * QEMU live migration
3 *
4 * Copyright IBM, Corp. 2008
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010012 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
aliguori5bb79102008-10-13 03:12:02 +000014 */
15
Peter Maydell1393a482016-01-26 18:16:54 +000016#include "qemu/osdep.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020017#include "qemu/cutils.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010018#include "qemu/error-report.h"
Markus Armbrusterdb725812019-08-12 07:23:50 +020019#include "qemu/main-loop.h"
Juan Quintela795c40b2017-04-06 12:00:28 +020020#include "migration/blocker.h"
Juan Quintelaf4dbe1b2017-04-05 15:54:10 +020021#include "exec.h"
Juan Quintela7fcac4a2017-04-05 15:58:29 +020022#include "fd.h"
Juan Quintela61e8b142017-04-05 17:40:11 +020023#include "socket.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020024#include "sysemu/runstate.h"
Markus Armbruster46517dd2019-08-12 07:23:57 +020025#include "sysemu/sysemu.h"
Claudio Fontanab0c3cf92020-06-29 11:35:03 +020026#include "sysemu/cpu-throttle.h"
Juan Quintelae1a3ece2017-04-17 20:32:36 +020027#include "rdma.h"
Juan Quintela7b1e1a22017-04-17 20:26:27 +020028#include "ram.h"
Juan Quintela84a899d2017-04-24 18:53:30 +020029#include "migration/global_state.h"
Juan Quintelac4b63b72017-04-24 19:02:44 +020030#include "migration/misc.h"
Juan Quintela6666c962017-04-24 20:07:27 +020031#include "migration.h"
Juan Quintela20a519a2017-04-20 14:48:46 +020032#include "savevm.h"
Juan Quintela40014d82017-04-17 19:34:36 +020033#include "qemu-file-channel.h"
Juan Quintela08a0aee2017-04-20 18:52:18 +020034#include "qemu-file.h"
Juan Quintela987772d2017-04-17 19:02:59 +020035#include "migration/vmstate.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010036#include "block/block.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010037#include "qapi/error.h"
Juan Quintela9aca82b2019-02-27 11:51:27 +010038#include "qapi/clone-visitor.h"
Max Reitz31e4c352020-08-20 17:07:23 +020039#include "qapi/qapi-visit-migration.h"
Juan Quintela9aca82b2019-02-27 11:51:27 +010040#include "qapi/qapi-visit-sockets.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010041#include "qapi/qapi-commands-migration.h"
42#include "qapi/qapi-events-migration.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010043#include "qapi/qmp/qerror.h"
Markus Armbruster15280c32018-02-01 12:18:36 +010044#include "qapi/qmp/qnull.h"
Paolo Bonziniab28bd22015-07-09 08:55:38 +020045#include "qemu/rcu.h"
Juan Quintela2c9e6fe2017-04-21 14:31:22 +020046#include "block.h"
Juan Quintelabe07b0a2017-04-20 13:12:24 +020047#include "postcopy-ram.h"
Juan Quintela766bd172012-07-23 05:45:29 +020048#include "qemu/thread.h"
Kazuya Saitoc09e5bb2013-02-22 17:36:19 +010049#include "trace.h"
Juan Quintela51180422017-04-24 20:50:19 +020050#include "exec/target_page.h"
Daniel P. Berrange61b67d42016-04-27 11:05:01 +010051#include "io/channel-buffer.h"
zhanghailiang35a6ed42016-10-27 14:42:52 +080052#include "migration/colo.h"
Peter Xu4ffdb332017-06-27 12:10:18 +080053#include "hw/boards.h"
Markus Armbrustera27bd6c2019-08-12 07:23:51 +020054#include "hw/qdev-properties.h"
Peter Xu9d18af92017-06-27 12:10:19 +080055#include "monitor/monitor.h"
Dr. David Alan Gilbert50510ea2019-02-27 13:24:05 +000056#include "net/announce.h"
Jens Freimannc7e0acd2019-10-29 12:49:02 +010057#include "qemu/queue.h"
Juan Quintelad32ca5a2020-01-22 16:16:07 +010058#include "multifd.h"
aliguori065e2812008-11-11 16:46:33 +000059
Laurent Vivier7590a2a2020-09-21 16:49:57 +020060#define MAX_THROTTLE (128 << 20) /* Migration transfer speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000061
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010062/* Amount of time to allocate to each "chunk" of bandwidth-throttled
63 * data. */
64#define BUFFER_DELAY 100
65#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
66
Ashijeet Acharya2ff30252016-09-15 21:50:28 +053067/* Time in milliseconds we are allowed to stop the source,
68 * for sending the last part */
69#define DEFAULT_MIGRATE_SET_DOWNTIME 300
70
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -030071/* Maximum migrate downtime set to 2000 seconds */
72#define MAX_MIGRATE_DOWNTIME_SECONDS 2000
73#define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
74
Liang Li8706d2d2015-03-23 16:32:17 +080075/* Default compression thread count */
76#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080077/* Default decompression thread count, usually decompression is at
78 * least 4 times as fast as compression.*/
79#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080080/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
81#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
Jason J. Herne1626fee2015-09-08 13:12:34 -040082/* Define default autoconverge cpu throttle migration parameters */
Keqian Zhudc14a472020-02-24 10:31:42 +080083#define DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD 50
Jason J. Herned85a31d2016-04-21 14:07:18 -040084#define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
85#define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
Li Qiang4cbc9c72018-08-01 06:00:20 -070086#define DEFAULT_MIGRATE_MAX_CPU_THROTTLE 99
Liang Li8706d2d2015-03-23 16:32:17 +080087
Orit Wasserman17ad9b32012-08-06 21:42:53 +030088/* Migration XBZRLE default cache size */
Juan Quintela73af8dd2017-10-05 21:30:10 +020089#define DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE (64 * 1024 * 1024)
Orit Wasserman17ad9b32012-08-06 21:42:53 +030090
Zhang Chen131b2152018-09-03 12:38:45 +080091/* The delay time (in ms) between two COLO checkpoints */
92#define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY (200 * 100)
Juan Quintela4075fb12016-01-15 08:56:17 +010093#define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2
Juan Quintela96eef042019-01-16 10:35:55 +010094#define DEFAULT_MIGRATE_MULTIFD_COMPRESSION MULTIFD_COMPRESSION_NONE
Juan Quintela9004db42020-01-23 17:08:52 +010095/* 0: means nocompress, 1: best speed, ... 9: best compress ratio */
96#define DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL 1
Juan Quintela6a9ad152020-01-23 17:41:36 +010097/* 0: means nocompress, 1: best speed, ... 20: best compress ratio */
98#define DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL 1
zhanghailiang68b53592016-10-27 14:43:01 +080099
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +0100100/* Background transfer rate for postcopy, 0 means unlimited, note
101 * that page requests can still exceed this limit.
102 */
103#define DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH 0
104
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +0000105/*
106 * Parameters for self_announce_delay giving a stream of RARP/ARP
107 * packets after migration.
108 */
109#define DEFAULT_MIGRATE_ANNOUNCE_INITIAL 50
110#define DEFAULT_MIGRATE_ANNOUNCE_MAX 550
111#define DEFAULT_MIGRATE_ANNOUNCE_ROUNDS 5
112#define DEFAULT_MIGRATE_ANNOUNCE_STEP 100
113
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100114static NotifierList migration_state_notifiers =
115 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
116
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000117static bool deferred_incoming;
118
Juan Quintelada6f1792017-04-24 17:37:14 +0200119/* Messages sent on the return path from destination to source */
120enum mig_rp_message_type {
121 MIG_RP_MSG_INVALID = 0, /* Must be 0 */
122 MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */
123 MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */
124
125 MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */
126 MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */
Peter Xua335deb2018-05-02 18:47:28 +0800127 MIG_RP_MSG_RECV_BITMAP, /* send recved_bitmap back to source */
Peter Xu13955b82018-05-02 18:47:30 +0800128 MIG_RP_MSG_RESUME_ACK, /* tell source that we are ready to resume */
Juan Quintelada6f1792017-04-24 17:37:14 +0200129
130 MIG_RP_MSG_MAX
131};
132
Juan Quintela17549e82011-10-05 13:50:43 +0200133/* When we add fault tolerance, we could have several
134 migrations at once. For now we don't need to add
135 dynamic creation of migration */
136
Peter Xue5cb7e72017-06-27 12:10:13 +0800137static MigrationState *current_migration;
Peter Xue1b1b1b2018-05-02 18:47:35 +0800138static MigrationIncomingState *current_incoming;
Peter Xue5cb7e72017-06-27 12:10:13 +0800139
Peter Xu8b0b29d2017-07-18 11:39:06 +0800140static bool migration_object_check(MigrationState *ms, Error **errp);
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +0100141static int migration_maybe_pause(MigrationState *s,
142 int *current_active_state,
143 int new_state);
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +0000144static void migrate_fd_cancel(MigrationState *s);
Peter Xu8b0b29d2017-07-18 11:39:06 +0800145
Peter 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
Juan Quintela9aca82b2019-02-27 11:51:27 +0100241 if (mis->socket_address_list) {
242 qapi_free_SocketAddressList(mis->socket_address_list);
243 mis->socket_address_list = NULL;
244 }
Peter Xuc02039a2020-09-08 16:30:17 -0400245
246 qemu_event_destroy(&mis->main_thread_load_event);
247 qemu_sem_destroy(&mis->postcopy_pause_sem_dst);
248 qemu_sem_destroy(&mis->postcopy_pause_sem_fault);
249 qemu_mutex_destroy(&mis->rp_mutex);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100250}
251
Juan Quintelab05dc722015-07-07 14:44:05 +0200252static void migrate_generate_event(int new_state)
253{
254 if (migrate_use_events()) {
Peter Xu3ab72382018-08-15 21:37:37 +0800255 qapi_event_send_migration(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200256 }
257}
258
Dr. David Alan Gilbert0f073f42018-04-16 18:09:30 +0100259static bool migrate_late_block_activate(void)
260{
261 MigrationState *s;
262
263 s = migrate_get_current();
264
265 return s->enabled_capabilities[
266 MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE];
267}
268
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000269/*
270 * Called on -incoming with a defer: uri.
271 * The migration can be started later after any parameters have been
272 * changed.
273 */
274static void deferred_incoming_migration(Error **errp)
275{
276 if (deferred_incoming) {
277 error_setg(errp, "Incoming migration already deferred");
278 }
279 deferred_incoming = true;
280}
281
Juan Quintelada6f1792017-04-24 17:37:14 +0200282/*
283 * Send a message on the return channel back to the source
284 * of the migration.
285 */
Peter Xud6208e32018-02-08 18:31:12 +0800286static int migrate_send_rp_message(MigrationIncomingState *mis,
287 enum mig_rp_message_type message_type,
288 uint16_t len, void *data)
Juan Quintelada6f1792017-04-24 17:37:14 +0200289{
Peter Xud6208e32018-02-08 18:31:12 +0800290 int ret = 0;
291
Juan Quintelada6f1792017-04-24 17:37:14 +0200292 trace_migrate_send_rp_message((int)message_type, len);
293 qemu_mutex_lock(&mis->rp_mutex);
Peter Xud6208e32018-02-08 18:31:12 +0800294
295 /*
296 * It's possible that the file handle got lost due to network
297 * failures.
298 */
299 if (!mis->to_src_file) {
300 ret = -EIO;
301 goto error;
302 }
303
Juan Quintelada6f1792017-04-24 17:37:14 +0200304 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
305 qemu_put_be16(mis->to_src_file, len);
306 qemu_put_buffer(mis->to_src_file, data, len);
307 qemu_fflush(mis->to_src_file);
Peter Xud6208e32018-02-08 18:31:12 +0800308
309 /* It's possible that qemu file got error during sending */
310 ret = qemu_file_get_error(mis->to_src_file);
311
312error:
Juan Quintelada6f1792017-04-24 17:37:14 +0200313 qemu_mutex_unlock(&mis->rp_mutex);
Peter Xud6208e32018-02-08 18:31:12 +0800314 return ret;
Juan Quintelada6f1792017-04-24 17:37:14 +0200315}
316
Peter Xu2e2bce12020-09-08 16:30:18 -0400317/* Request one page from the source VM at the given start address.
318 * rb: the RAMBlock to request the page in
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000319 * Start: Address offset within the RB
320 * Len: Length in bytes required - must be a multiple of pagesize
321 */
Peter Xu2e2bce12020-09-08 16:30:18 -0400322int migrate_send_rp_req_pages(MigrationIncomingState *mis, RAMBlock *rb,
323 ram_addr_t start)
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000324{
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100325 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000326 size_t msglen = 12; /* start + len */
Peter Xu2e2bce12020-09-08 16:30:18 -0400327 size_t len = qemu_ram_pagesize(rb);
Peter Xud6208e32018-02-08 18:31:12 +0800328 enum mig_rp_message_type msg_type;
Peter Xu2e2bce12020-09-08 16:30:18 -0400329 const char *rbname;
330 int rbname_len;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000331
332 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
333 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
334
Peter Xu2e2bce12020-09-08 16:30:18 -0400335 /*
336 * We maintain the last ramblock that we requested for page. Note that we
337 * don't need locking because this function will only be called within the
338 * postcopy ram fault thread.
339 */
340 if (rb != mis->last_rb) {
341 mis->last_rb = rb;
342
343 rbname = qemu_ram_get_idstr(rb);
344 rbname_len = strlen(rbname);
345
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000346 assert(rbname_len < 256);
347
348 bufc[msglen++] = rbname_len;
349 memcpy(bufc + msglen, rbname, rbname_len);
350 msglen += rbname_len;
Peter Xud6208e32018-02-08 18:31:12 +0800351 msg_type = MIG_RP_MSG_REQ_PAGES_ID;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000352 } else {
Peter Xud6208e32018-02-08 18:31:12 +0800353 msg_type = MIG_RP_MSG_REQ_PAGES;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000354 }
Peter Xud6208e32018-02-08 18:31:12 +0800355
356 return migrate_send_rp_message(mis, msg_type, msglen, bufc);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000357}
358
Zhang Chenaad555c2018-09-03 12:38:47 +0800359static bool migration_colo_enabled;
360bool migration_incoming_colo_enabled(void)
361{
362 return migration_colo_enabled;
363}
364
365void migration_incoming_disable_colo(void)
366{
David Hildenbrand18b1d3c2020-06-26 09:22:36 +0200367 ram_block_discard_disable(false);
Zhang Chenaad555c2018-09-03 12:38:47 +0800368 migration_colo_enabled = false;
369}
370
David Hildenbrand18b1d3c2020-06-26 09:22:36 +0200371int migration_incoming_enable_colo(void)
Zhang Chenaad555c2018-09-03 12:38:47 +0800372{
David Hildenbrand18b1d3c2020-06-26 09:22:36 +0200373 if (ram_block_discard_disable(true)) {
374 error_report("COLO: cannot disable RAM discard");
375 return -EBUSY;
376 }
Zhang Chenaad555c2018-09-03 12:38:47 +0800377 migration_colo_enabled = true;
David Hildenbrand18b1d3c2020-06-26 09:22:36 +0200378 return 0;
Zhang Chenaad555c2018-09-03 12:38:47 +0800379}
380
Juan Quintela9aca82b2019-02-27 11:51:27 +0100381void migrate_add_address(SocketAddress *address)
382{
383 MigrationIncomingState *mis = migration_incoming_get_current();
384 SocketAddressList *addrs;
385
386 addrs = g_new0(SocketAddressList, 1);
387 addrs->next = mis->socket_address_list;
388 mis->socket_address_list = addrs;
389 addrs->value = QAPI_CLONE(SocketAddress, address);
390}
391
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200392void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000393{
Longpeng(Mike)d658f652020-08-06 15:40:29 +0800394 const char *p = NULL;
aliguori34c9dd82008-10-13 03:14:31 +0000395
Peter Xu3ab72382018-08-15 21:37:37 +0800396 qapi_event_send_migration(MIGRATION_STATUS_SETUP);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000397 if (!strcmp(uri, "defer")) {
398 deferred_incoming_migration(errp);
Longpeng(Mike)d658f652020-08-06 15:40:29 +0800399 } else if (strstart(uri, "tcp:", &p) ||
Longpeng(Mike)9ba3b2b2020-08-06 15:40:30 +0800400 strstart(uri, "unix:", NULL) ||
401 strstart(uri, "vsock:", NULL)) {
Longpeng(Mike)d658f652020-08-06 15:40:29 +0800402 socket_start_incoming_migration(p ? p : uri, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400403#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000404 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400405 rdma_start_incoming_migration(p, errp);
406#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000407 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200408 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000409 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200410 fd_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000411 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100412 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200413 }
aliguori5bb79102008-10-13 03:12:02 +0000414}
415
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300416static void process_incoming_migration_bh(void *opaque)
417{
418 Error *local_err = NULL;
419 MigrationIncomingState *mis = opaque;
420
Dr. David Alan Gilbert0f073f42018-04-16 18:09:30 +0100421 /* If capability late_block_activate is set:
422 * Only fire up the block code now if we're going to restart the
423 * VM, else 'cont' will do it.
424 * This causes file locking to happen; so we don't want it to happen
425 * unless we really are starting the VM.
426 */
427 if (!migrate_late_block_activate() ||
428 (autostart && (!global_state_received() ||
429 global_state_get_runstate() == RUN_STATE_RUNNING))) {
430 /* Make sure all file formats flush their mutable metadata.
431 * If we get an error here, just don't restart the VM yet. */
432 bdrv_invalidate_cache_all(&local_err);
433 if (local_err) {
434 error_report_err(local_err);
435 local_err = NULL;
436 autostart = false;
437 }
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200438 }
439
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300440 /*
441 * This must happen after all error conditions are dealt with and
442 * we're sure the VM is going to be running on this host.
443 */
Dr. David Alan Gilbert76595052019-02-27 13:24:08 +0000444 qemu_announce_self(&mis->announce_timer, migrate_announce_params());
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300445
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100446 if (multifd_load_cleanup(&local_err) != 0) {
447 error_report_err(local_err);
448 autostart = false;
449 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300450 /* If global state section was not received or we are in running
451 state, we need to obey autostart. Any other state is set with
452 runstate_set. */
453
Vladimir Sementsov-Ogievskiyb35ebdf2018-03-13 15:34:01 -0400454 dirty_bitmap_mig_before_vm_start();
455
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300456 if (!global_state_received() ||
457 global_state_get_runstate() == RUN_STATE_RUNNING) {
458 if (autostart) {
459 vm_start();
460 } else {
461 runstate_set(RUN_STATE_PAUSED);
462 }
Zhang Chendb009722019-03-03 22:50:17 +0800463 } else if (migration_incoming_colo_enabled()) {
464 migration_incoming_disable_colo();
465 vm_start();
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300466 } else {
467 runstate_set(global_state_get_runstate());
468 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300469 /*
470 * This must happen after any state changes since as soon as an external
471 * observer sees this event they might start to prod at the VM assuming
472 * it's ready to use.
473 */
474 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
475 MIGRATION_STATUS_COMPLETED);
476 qemu_bh_delete(mis->bh);
477 migration_incoming_state_destroy();
478}
479
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200480static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200481{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100482 MigrationIncomingState *mis = migration_incoming_get_current();
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000483 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200484 int ret;
Zhang Chen8e48ac92018-09-03 12:38:46 +0800485 Error *local_err = NULL;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200486
Juan Quintela4f0fae72017-07-24 12:42:02 +0200487 assert(mis->from_src_file);
Lidong Chen92370982018-08-06 21:29:35 +0800488 mis->migration_incoming_co = qemu_coroutine_self();
Dr. David Alan Gilbert67f11b52017-02-24 18:28:34 +0000489 mis->largest_page_size = qemu_ram_pagesize_largest();
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000490 postcopy_state_set(POSTCOPY_INCOMING_NONE);
zhanghailiang93d7af62015-12-16 11:47:34 +0000491 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
492 MIGRATION_STATUS_ACTIVE);
Juan Quintela4f0fae72017-07-24 12:42:02 +0200493 ret = qemu_loadvm_state(mis->from_src_file);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100494
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000495 ps = postcopy_state_get();
496 trace_process_incoming_migration_co_end(ret, ps);
497 if (ps != POSTCOPY_INCOMING_NONE) {
498 if (ps == POSTCOPY_INCOMING_ADVISE) {
499 /*
500 * Where a migration had postcopy enabled (and thus went to advise)
501 * but managed to complete within the precopy period, we can use
502 * the normal exit.
503 */
504 postcopy_ram_incoming_cleanup(mis);
505 } else if (ret >= 0) {
506 /*
507 * Postcopy was started, cleanup should happen at the end of the
508 * postcopy thread.
509 */
510 trace_process_incoming_migration_co_postcopy_end_main();
511 return;
512 }
513 /* Else if something went wrong then just fall out of the normal exit */
514 }
515
zhanghailiang25d0c162016-10-27 14:42:55 +0800516 /* we get COLO info, and know if we are in COLO mode */
Zhang Chenaad555c2018-09-03 12:38:47 +0800517 if (!ret && migration_incoming_colo_enabled()) {
Zhang Chen8e48ac92018-09-03 12:38:46 +0800518 /* Make sure all file formats flush their mutable metadata */
519 bdrv_invalidate_cache_all(&local_err);
520 if (local_err) {
Zhang Chen8e48ac92018-09-03 12:38:46 +0800521 error_report_err(local_err);
Fei Li6d99c2d2019-01-13 22:08:49 +0800522 goto fail;
Zhang Chen8e48ac92018-09-03 12:38:46 +0800523 }
524
zhanghailiang25d0c162016-10-27 14:42:55 +0800525 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
526 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
527 mis->have_colo_incoming_thread = true;
528 qemu_coroutine_yield();
529
530 /* Wait checkpoint incoming thread exit before free resource */
531 qemu_thread_join(&mis->colo_incoming_thread);
Zhang Chen13af18f2018-09-03 12:38:48 +0800532 /* We hold the global iothread lock, so it is safe here */
533 colo_release_ram_cache();
zhanghailiang25d0c162016-10-27 14:42:55 +0800534 }
535
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200536 if (ret < 0) {
Peter Lievendb80fac2014-06-10 11:29:16 +0200537 error_report("load of migration failed: %s", strerror(-ret));
Fei Li6d99c2d2019-01-13 22:08:49 +0800538 goto fail;
Juan Quintela511c0232010-06-09 14:10:55 +0200539 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300540 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
541 qemu_bh_schedule(mis->bh);
Lidong Chen92370982018-08-06 21:29:35 +0800542 mis->migration_incoming_co = NULL;
Fei Li6d99c2d2019-01-13 22:08:49 +0800543 return;
544fail:
545 local_err = NULL;
546 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
547 MIGRATION_STATUS_FAILED);
548 qemu_fclose(mis->from_src_file);
549 if (multifd_load_cleanup(&local_err) != 0) {
550 error_report_err(local_err);
551 }
552 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200553}
554
Juan Quintelab673eab2019-06-12 11:44:19 +0200555/**
556 * @migration_incoming_setup: Setup incoming migration
557 *
558 * Returns 0 for no error or 1 for error
559 *
560 * @f: file for main migration channel
561 * @errp: where to put errors
562 */
563static int migration_incoming_setup(QEMUFile *f, Error **errp)
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200564{
Juan Quintela4f0fae72017-07-24 12:42:02 +0200565 MigrationIncomingState *mis = migration_incoming_get_current();
Juan Quintelab673eab2019-06-12 11:44:19 +0200566 Error *local_err = NULL;
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200567
Juan Quintelab673eab2019-06-12 11:44:19 +0200568 if (multifd_load_setup(&local_err) != 0) {
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100569 /* We haven't been able to create multifd threads
570 nothing better to do */
Juan Quintelab673eab2019-06-12 11:44:19 +0200571 error_report_err(local_err);
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100572 exit(EXIT_FAILURE);
573 }
574
Juan Quintela4f0fae72017-07-24 12:42:02 +0200575 if (!mis->from_src_file) {
576 mis->from_src_file = f;
577 }
Daniel P. Berrange06ad5132016-04-27 11:04:56 +0100578 qemu_file_set_blocking(f, false);
Juan Quintelab673eab2019-06-12 11:44:19 +0200579 return 0;
Juan Quintelae595a012017-07-17 12:30:25 +0200580}
581
Juan Quintela36c2f8b2018-03-07 08:40:52 +0100582void migration_incoming_process(void)
Juan Quintelae595a012017-07-17 12:30:25 +0200583{
584 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL);
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200585 qemu_coroutine_enter(co);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200586}
587
Peter Xu884835f2018-06-27 21:22:45 +0800588/* Returns true if recovered from a paused migration, otherwise false */
589static bool postcopy_try_recover(QEMUFile *f)
Juan Quintelae595a012017-07-17 12:30:25 +0200590{
Peter Xud96c9e82018-05-02 18:47:26 +0800591 MigrationIncomingState *mis = migration_incoming_get_current();
592
593 if (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
594 /* Resumed from a paused postcopy migration */
595
596 mis->from_src_file = f;
597 /* Postcopy has standalone thread to do vm load */
598 qemu_file_set_blocking(f, true);
599
600 /* Re-configure the return path */
601 mis->to_src_file = qemu_file_get_return_path(f);
602
603 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
604 MIGRATION_STATUS_POSTCOPY_RECOVER);
605
606 /*
607 * Here, we only wake up the main loading thread (while the
608 * fault thread will still be waiting), so that we can receive
609 * commands from source now, and answer it if needed. The
610 * fault thread will be woken up afterwards until we are sure
611 * that source is ready to reply to page requests.
612 */
613 qemu_sem_post(&mis->postcopy_pause_sem_dst);
Peter Xu884835f2018-06-27 21:22:45 +0800614 return true;
Peter Xud96c9e82018-05-02 18:47:26 +0800615 }
Peter Xu884835f2018-06-27 21:22:45 +0800616
617 return false;
618}
619
Juan Quintelab673eab2019-06-12 11:44:19 +0200620void migration_fd_process_incoming(QEMUFile *f, Error **errp)
Peter Xu884835f2018-06-27 21:22:45 +0800621{
Juan Quintelab673eab2019-06-12 11:44:19 +0200622 Error *local_err = NULL;
623
Peter Xu884835f2018-06-27 21:22:45 +0800624 if (postcopy_try_recover(f)) {
625 return;
626 }
627
Juan Quintelab673eab2019-06-12 11:44:19 +0200628 if (migration_incoming_setup(f, &local_err)) {
629 if (local_err) {
630 error_propagate(errp, local_err);
631 }
632 return;
633 }
Peter Xu884835f2018-06-27 21:22:45 +0800634 migration_incoming_process();
Juan Quintelae595a012017-07-17 12:30:25 +0200635}
636
Fei Li49ed0d22019-01-13 22:08:46 +0800637void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp)
Juan Quintela4f0fae72017-07-24 12:42:02 +0200638{
639 MigrationIncomingState *mis = migration_incoming_get_current();
Juan Quintelab673eab2019-06-12 11:44:19 +0200640 Error *local_err = NULL;
Peter Xua429e7f2018-06-27 21:22:46 +0800641 bool start_migration;
Juan Quintela4f0fae72017-07-24 12:42:02 +0200642
643 if (!mis->from_src_file) {
Peter Xua429e7f2018-06-27 21:22:46 +0800644 /* The first connection (multifd may have multiple) */
Juan Quintela4f0fae72017-07-24 12:42:02 +0200645 QEMUFile *f = qemu_fopen_channel_input(ioc);
Peter Xua429e7f2018-06-27 21:22:46 +0800646
647 /* If it's a recovery, we're done */
Peter Xu884835f2018-06-27 21:22:45 +0800648 if (postcopy_try_recover(f)) {
649 return;
650 }
Peter Xua429e7f2018-06-27 21:22:46 +0800651
Juan Quintelab673eab2019-06-12 11:44:19 +0200652 if (migration_incoming_setup(f, &local_err)) {
653 if (local_err) {
654 error_propagate(errp, local_err);
655 }
656 return;
657 }
Peter Xua429e7f2018-06-27 21:22:46 +0800658
659 /*
660 * Common migration only needs one channel, so we can start
661 * right now. Multifd needs more than one channel, we wait.
662 */
663 start_migration = !migrate_use_multifd();
664 } else {
665 /* Multiple connections */
666 assert(migrate_use_multifd());
Fei Li49ed0d22019-01-13 22:08:46 +0800667 start_migration = multifd_recv_new_channel(ioc, &local_err);
668 if (local_err) {
669 error_propagate(errp, local_err);
670 return;
671 }
Juan Quintela4f0fae72017-07-24 12:42:02 +0200672 }
Peter Xu81e62052018-06-27 21:22:44 +0800673
Peter Xua429e7f2018-06-27 21:22:46 +0800674 if (start_migration) {
Peter Xu81e62052018-06-27 21:22:44 +0800675 migration_incoming_process();
676 }
Juan Quintela4f0fae72017-07-24 12:42:02 +0200677}
678
Juan Quintela428d8902017-07-24 13:06:25 +0200679/**
680 * @migration_has_all_channels: We have received all channels that we need
681 *
682 * Returns true when we have got connections to all the channels that
683 * we need for migration.
684 */
685bool migration_has_all_channels(void)
686{
Daniel P. Berrangéca273df2018-06-19 17:35:52 +0100687 MigrationIncomingState *mis = migration_incoming_get_current();
Juan Quintela62c1e0c2018-02-19 18:59:02 +0100688 bool all_channels;
689
690 all_channels = multifd_recv_all_channels_created();
691
Daniel P. Berrangéca273df2018-06-19 17:35:52 +0100692 return all_channels && mis->from_src_file != NULL;
Juan Quintela428d8902017-07-24 13:06:25 +0200693}
694
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000695/*
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000696 * Send a 'SHUT' message on the return channel with the given value
697 * to indicate that we've finished with the RP. Non-0 value indicates
698 * error.
699 */
700void migrate_send_rp_shut(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_SHUT, sizeof(buf), &buf);
707}
708
709/*
710 * Send a 'PONG' message on the return channel with the given value
711 * (normally in response to a 'PING')
712 */
713void migrate_send_rp_pong(MigrationIncomingState *mis,
714 uint32_t value)
715{
716 uint32_t buf;
717
718 buf = cpu_to_be32(value);
719 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
720}
721
Peter Xua335deb2018-05-02 18:47:28 +0800722void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
723 char *block_name)
724{
725 char buf[512];
726 int len;
727 int64_t res;
728
729 /*
730 * First, we send the header part. It contains only the len of
731 * idstr, and the idstr itself.
732 */
733 len = strlen(block_name);
734 buf[0] = len;
735 memcpy(buf + 1, block_name, len);
736
737 if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
738 error_report("%s: MSG_RP_RECV_BITMAP only used for recovery",
739 __func__);
740 return;
741 }
742
743 migrate_send_rp_message(mis, MIG_RP_MSG_RECV_BITMAP, len + 1, buf);
744
745 /*
746 * Next, we dump the received bitmap to the stream.
747 *
748 * TODO: currently we are safe since we are the only one that is
749 * using the to_src_file handle (fault thread is still paused),
750 * and it's ok even not taking the mutex. However the best way is
751 * to take the lock before sending the message header, and release
752 * the lock after sending the bitmap.
753 */
754 qemu_mutex_lock(&mis->rp_mutex);
755 res = ramblock_recv_bitmap_send(mis->to_src_file, block_name);
756 qemu_mutex_unlock(&mis->rp_mutex);
757
758 trace_migrate_send_rp_recv_bitmap(block_name, res);
759}
760
Peter Xu13955b82018-05-02 18:47:30 +0800761void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value)
762{
763 uint32_t buf;
764
765 buf = cpu_to_be32(value);
766 migrate_send_rp_message(mis, MIG_RP_MSG_RESUME_ACK, sizeof(buf), &buf);
767}
768
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300769MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
770{
771 MigrationCapabilityStatusList *head = NULL;
772 MigrationCapabilityStatusList *caps;
773 MigrationState *s = migrate_get_current();
774 int i;
775
Michael Tokarev387eede2013-10-05 13:18:28 +0400776 caps = NULL; /* silence compiler warning */
Eric Blake7fb1cf12015-11-18 01:52:57 -0700777 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Dr. David Alan Gilberted1701c2017-05-15 15:05:29 +0100778#ifndef CONFIG_LIVE_BLOCK_MIGRATION
779 if (i == MIGRATION_CAPABILITY_BLOCK) {
780 continue;
781 }
782#endif
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300783 if (head == NULL) {
784 head = g_malloc0(sizeof(*caps));
785 caps = head;
786 } else {
787 caps->next = g_malloc0(sizeof(*caps));
788 caps = caps->next;
789 }
790 caps->value =
791 g_malloc(sizeof(*caps->value));
792 caps->value->capability = i;
793 caps->value->state = s->enabled_capabilities[i];
794 }
795
796 return head;
797}
798
Liang Li85de8322015-03-23 16:32:28 +0800799MigrationParameters *qmp_query_migrate_parameters(Error **errp)
800{
801 MigrationParameters *params;
802 MigrationState *s = migrate_get_current();
803
Markus Armbrustere87fae42017-07-18 12:57:38 +0200804 /* TODO use QAPI_CLONE() instead of duplicating it inline */
Liang Li85de8322015-03-23 16:32:28 +0800805 params = g_malloc0(sizeof(*params));
Eric Blakede63ab62016-09-08 22:14:15 -0500806 params->has_compress_level = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100807 params->compress_level = s->parameters.compress_level;
Eric Blakede63ab62016-09-08 22:14:15 -0500808 params->has_compress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100809 params->compress_threads = s->parameters.compress_threads;
Xiao Guangrong1d588722018-08-21 16:10:20 +0800810 params->has_compress_wait_thread = true;
811 params->compress_wait_thread = s->parameters.compress_wait_thread;
Eric Blakede63ab62016-09-08 22:14:15 -0500812 params->has_decompress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100813 params->decompress_threads = s->parameters.decompress_threads;
Keqian Zhudc14a472020-02-24 10:31:42 +0800814 params->has_throttle_trigger_threshold = true;
815 params->throttle_trigger_threshold = s->parameters.throttle_trigger_threshold;
Eric Blakede63ab62016-09-08 22:14:15 -0500816 params->has_cpu_throttle_initial = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100817 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
Eric Blakede63ab62016-09-08 22:14:15 -0500818 params->has_cpu_throttle_increment = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100819 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
Keqian Zhucbbf8182020-04-13 18:15:08 +0800820 params->has_cpu_throttle_tailslow = true;
821 params->cpu_throttle_tailslow = s->parameters.cpu_throttle_tailslow;
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200822 params->has_tls_creds = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100823 params->tls_creds = g_strdup(s->parameters.tls_creds);
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200824 params->has_tls_hostname = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100825 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
Daniel P. Berranged2f1d292019-02-27 14:53:24 +0000826 params->has_tls_authz = true;
Mao Zhongyi7cd75cb2020-03-25 09:49:30 +0800827 params->tls_authz = g_strdup(s->parameters.tls_authz ?
828 s->parameters.tls_authz : "");
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530829 params->has_max_bandwidth = true;
830 params->max_bandwidth = s->parameters.max_bandwidth;
831 params->has_downtime_limit = true;
832 params->downtime_limit = s->parameters.downtime_limit;
zhanghailiangfe39a4d2016-11-02 15:42:09 +0800833 params->has_x_checkpoint_delay = true;
zhanghailiang68b53592016-10-27 14:43:01 +0800834 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
Juan Quintela2833c592017-04-05 18:32:37 +0200835 params->has_block_incremental = true;
836 params->block_incremental = s->parameters.block_incremental;
Juan Quintelacbfd6c92019-02-06 13:54:06 +0100837 params->has_multifd_channels = true;
838 params->multifd_channels = s->parameters.multifd_channels;
Juan Quintela96eef042019-01-16 10:35:55 +0100839 params->has_multifd_compression = true;
840 params->multifd_compression = s->parameters.multifd_compression;
Juan Quintela9004db42020-01-23 17:08:52 +0100841 params->has_multifd_zlib_level = true;
842 params->multifd_zlib_level = s->parameters.multifd_zlib_level;
Juan Quintela6a9ad152020-01-23 17:41:36 +0100843 params->has_multifd_zstd_level = true;
844 params->multifd_zstd_level = s->parameters.multifd_zstd_level;
Juan Quintela73af8dd2017-10-05 21:30:10 +0200845 params->has_xbzrle_cache_size = true;
846 params->xbzrle_cache_size = s->parameters.xbzrle_cache_size;
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +0100847 params->has_max_postcopy_bandwidth = true;
848 params->max_postcopy_bandwidth = s->parameters.max_postcopy_bandwidth;
Li Qiang4cbc9c72018-08-01 06:00:20 -0700849 params->has_max_cpu_throttle = true;
850 params->max_cpu_throttle = s->parameters.max_cpu_throttle;
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +0000851 params->has_announce_initial = true;
852 params->announce_initial = s->parameters.announce_initial;
853 params->has_announce_max = true;
854 params->announce_max = s->parameters.announce_max;
855 params->has_announce_rounds = true;
856 params->announce_rounds = s->parameters.announce_rounds;
857 params->has_announce_step = true;
858 params->announce_step = s->parameters.announce_step;
Liang Li85de8322015-03-23 16:32:28 +0800859
Max Reitz31e4c352020-08-20 17:07:23 +0200860 if (s->parameters.has_block_bitmap_mapping) {
861 params->has_block_bitmap_mapping = true;
862 params->block_bitmap_mapping =
863 QAPI_CLONE(BitmapMigrationNodeAliasList,
864 s->parameters.block_bitmap_mapping);
865 }
866
Liang Li85de8322015-03-23 16:32:28 +0800867 return params;
868}
869
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +0000870AnnounceParameters *migrate_announce_params(void)
871{
872 static AnnounceParameters ap;
873
874 MigrationState *s = migrate_get_current();
875
876 ap.initial = s->parameters.announce_initial;
877 ap.max = s->parameters.announce_max;
878 ap.rounds = s->parameters.announce_rounds;
879 ap.step = s->parameters.announce_step;
880
881 return &ap;
882}
883
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000884/*
885 * Return true if we're already in the middle of a migration
886 * (i.e. any of the active or setup states)
887 */
Jia Lina3d63da12018-10-26 16:36:20 +0800888bool migration_is_setup_or_active(int state)
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000889{
890 switch (state) {
891 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000892 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Peter Xua688d2c2018-05-02 18:47:18 +0800893 case MIGRATION_STATUS_POSTCOPY_PAUSED:
Peter Xu135b87b2018-05-02 18:47:25 +0800894 case MIGRATION_STATUS_POSTCOPY_RECOVER:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000895 case MIGRATION_STATUS_SETUP:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +0100896 case MIGRATION_STATUS_PRE_SWITCHOVER:
897 case MIGRATION_STATUS_DEVICE:
Jens Freimannc7e0acd2019-10-29 12:49:02 +0100898 case MIGRATION_STATUS_WAIT_UNPLUG:
zhanghailiang19dd4082020-02-24 14:54:12 +0800899 case MIGRATION_STATUS_COLO:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000900 return true;
901
902 default:
903 return false;
904
905 }
906}
907
Juan Quintela392d87e2020-01-21 15:39:23 +0100908bool migration_is_running(int state)
909{
910 switch (state) {
911 case MIGRATION_STATUS_ACTIVE:
912 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
913 case MIGRATION_STATUS_POSTCOPY_PAUSED:
914 case MIGRATION_STATUS_POSTCOPY_RECOVER:
915 case MIGRATION_STATUS_SETUP:
916 case MIGRATION_STATUS_PRE_SWITCHOVER:
917 case MIGRATION_STATUS_DEVICE:
918 case MIGRATION_STATUS_WAIT_UNPLUG:
919 case MIGRATION_STATUS_CANCELLING:
Juan Quintela392d87e2020-01-21 15:39:23 +0100920 return true;
921
922 default:
923 return false;
924
925 }
926}
927
Wei Yang640dfb12019-07-16 08:54:11 +0800928static void populate_time_info(MigrationInfo *info, MigrationState *s)
929{
930 info->has_status = true;
931 info->has_setup_time = true;
932 info->setup_time = s->setup_time;
933 if (s->state == MIGRATION_STATUS_COMPLETED) {
934 info->has_total_time = true;
935 info->total_time = s->total_time;
936 info->has_downtime = true;
937 info->downtime = s->downtime;
938 } else {
939 info->has_total_time = true;
940 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
941 s->start_time;
942 info->has_expected_downtime = true;
943 info->expected_downtime = s->expected_downtime;
944 }
945}
946
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100947static void populate_ram_info(MigrationInfo *info, MigrationState *s)
948{
949 info->has_ram = true;
950 info->ram = g_malloc0(sizeof(*info->ram));
Juan Quintela93604472017-06-06 19:49:03 +0200951 info->ram->transferred = ram_counters.transferred;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100952 info->ram->total = ram_bytes_total();
Juan Quintela93604472017-06-06 19:49:03 +0200953 info->ram->duplicate = ram_counters.duplicate;
Juan Quintelabedf53c2017-03-13 20:35:54 +0100954 /* legacy value. It is not used anymore */
955 info->ram->skipped = 0;
Juan Quintela93604472017-06-06 19:49:03 +0200956 info->ram->normal = ram_counters.normal;
957 info->ram->normal_bytes = ram_counters.normal *
Juan Quintela20afaed2017-03-21 09:09:14 +0100958 qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100959 info->ram->mbps = s->mbps;
Juan Quintela93604472017-06-06 19:49:03 +0200960 info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
961 info->ram->postcopy_requests = ram_counters.postcopy_requests;
Chao Fan030ce1f2017-03-21 10:22:43 +0800962 info->ram->page_size = qemu_target_page_size();
Juan Quintelaa61c45b2018-06-26 15:20:11 +0200963 info->ram->multifd_bytes = ram_counters.multifd_bytes;
Xiao Guangrongaecbfe92019-01-11 14:37:30 +0800964 info->ram->pages_per_second = s->pages_per_second;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100965
Juan Quintela114f5ae2017-05-04 10:09:21 +0200966 if (migrate_use_xbzrle()) {
967 info->has_xbzrle_cache = true;
968 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
969 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
Juan Quintela93604472017-06-06 19:49:03 +0200970 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
971 info->xbzrle_cache->pages = xbzrle_counters.pages;
972 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
973 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
Wei Wange460a4b2020-04-30 08:59:35 +0800974 info->xbzrle_cache->encoding_rate = xbzrle_counters.encoding_rate;
Juan Quintela93604472017-06-06 19:49:03 +0200975 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
Juan Quintela114f5ae2017-05-04 10:09:21 +0200976 }
977
Xiao Guangrong76e03002018-09-06 15:01:00 +0800978 if (migrate_use_compression()) {
979 info->has_compression = true;
980 info->compression = g_malloc0(sizeof(*info->compression));
981 info->compression->pages = compression_counters.pages;
982 info->compression->busy = compression_counters.busy;
983 info->compression->busy_rate = compression_counters.busy_rate;
984 info->compression->compressed_size =
985 compression_counters.compressed_size;
986 info->compression->compression_rate =
987 compression_counters.compression_rate;
988 }
989
Juan Quintela338182c2017-05-03 13:16:38 +0200990 if (cpu_throttle_active()) {
991 info->has_cpu_throttle_percentage = true;
992 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
993 }
994
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100995 if (s->state != MIGRATION_STATUS_COMPLETED) {
996 info->ram->remaining = ram_bytes_remaining();
Juan Quintela93604472017-06-06 19:49:03 +0200997 info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100998 }
999}
1000
Juan Quintela930ac042017-05-04 10:21:46 +02001001static void populate_disk_info(MigrationInfo *info)
1002{
1003 if (blk_mig_active()) {
1004 info->has_disk = true;
1005 info->disk = g_malloc0(sizeof(*info->disk));
1006 info->disk->transferred = blk_mig_bytes_transferred();
1007 info->disk->remaining = blk_mig_bytes_remaining();
1008 info->disk->total = blk_mig_bytes_total();
1009 }
1010}
1011
Alexey Perevalov65ace062018-03-22 21:17:27 +03001012static void fill_source_migration_info(MigrationInfo *info)
aliguori5bb79102008-10-13 03:12:02 +00001013{
Juan Quintela17549e82011-10-05 13:50:43 +02001014 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +00001015
Juan Quintela17549e82011-10-05 13:50:43 +02001016 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +08001017 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +02001018 /* no migration has happened ever */
Alexey Perevalov65ace062018-03-22 21:17:27 +03001019 /* do not overwrite destination migration status */
1020 return;
zhanghailiang31194732015-03-13 16:08:38 +08001021 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001022 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -04001023 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001024 break;
zhanghailiang31194732015-03-13 16:08:38 +08001025 case MIGRATION_STATUS_ACTIVE:
1026 case MIGRATION_STATUS_CANCELLING:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001027 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +01001028 case MIGRATION_STATUS_PRE_SWITCHOVER:
1029 case MIGRATION_STATUS_DEVICE:
Peter Xua688d2c2018-05-02 18:47:18 +08001030 case MIGRATION_STATUS_POSTCOPY_PAUSED:
Peter Xu135b87b2018-05-02 18:47:25 +08001031 case MIGRATION_STATUS_POSTCOPY_RECOVER:
Wei Yang640dfb12019-07-16 08:54:11 +08001032 /* TODO add some postcopy stats */
1033 populate_time_info(info, s);
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +01001034 populate_ram_info(info, s);
Juan Quintela930ac042017-05-04 10:21:46 +02001035 populate_disk_info(info);
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001036 break;
zhanghailiang0b827d52016-10-27 14:42:54 +08001037 case MIGRATION_STATUS_COLO:
1038 info->has_status = true;
1039 /* TODO: display COLO specific information (checkpoint info etc.) */
1040 break;
zhanghailiang31194732015-03-13 16:08:38 +08001041 case MIGRATION_STATUS_COMPLETED:
Wei Yang640dfb12019-07-16 08:54:11 +08001042 populate_time_info(info, s);
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +01001043 populate_ram_info(info, s);
Juan Quintela17549e82011-10-05 13:50:43 +02001044 break;
zhanghailiang31194732015-03-13 16:08:38 +08001045 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -03001046 info->has_status = true;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001047 if (s->error) {
1048 info->has_error_desc = true;
1049 info->error_desc = g_strdup(error_get_pretty(s->error));
1050 }
Juan Quintela17549e82011-10-05 13:50:43 +02001051 break;
zhanghailiang31194732015-03-13 16:08:38 +08001052 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -03001053 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +02001054 break;
Jens Freimannc7e0acd2019-10-29 12:49:02 +01001055 case MIGRATION_STATUS_WAIT_UNPLUG:
1056 info->has_status = true;
1057 break;
aliguori5bb79102008-10-13 03:12:02 +00001058 }
zhanghailiangcde63fb2015-03-13 16:08:41 +08001059 info->status = s->state;
aliguori5bb79102008-10-13 03:12:02 +00001060}
1061
Peter Xu4a842142017-07-18 11:39:08 +08001062/**
1063 * @migration_caps_check - check capability validity
1064 *
1065 * @cap_list: old capability list, array of bool
1066 * @params: new capabilities to be applied soon
1067 * @errp: set *errp if the check failed, with reason
1068 *
1069 * Returns true if check passed, otherwise false.
1070 */
1071static bool migrate_caps_check(bool *cap_list,
1072 MigrationCapabilityStatusList *params,
1073 Error **errp)
Orit Wasserman00458432012-08-06 21:42:48 +03001074{
Orit Wasserman00458432012-08-06 21:42:48 +03001075 MigrationCapabilityStatusList *cap;
Peter Xu4a842142017-07-18 11:39:08 +08001076 bool old_postcopy_cap;
Alexey Perevalovd7651f12017-09-19 19:47:56 +03001077 MigrationIncomingState *mis = migration_incoming_get_current();
Orit Wasserman00458432012-08-06 21:42:48 +03001078
Peter Xu4a842142017-07-18 11:39:08 +08001079 old_postcopy_cap = cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Orit Wasserman00458432012-08-06 21:42:48 +03001080
1081 for (cap = params; cap; cap = cap->next) {
Peter Xu4a842142017-07-18 11:39:08 +08001082 cap_list[cap->value->capability] = cap->value->state;
Orit Wasserman00458432012-08-06 21:42:48 +03001083 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001084
Peter Xu4a842142017-07-18 11:39:08 +08001085#ifndef CONFIG_LIVE_BLOCK_MIGRATION
1086 if (cap_list[MIGRATION_CAPABILITY_BLOCK]) {
1087 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
1088 "block migration");
1089 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
1090 return false;
1091 }
1092#endif
1093
Zhang Chen7e934f52018-11-15 03:09:12 +08001094#ifndef CONFIG_REPLICATION
1095 if (cap_list[MIGRATION_CAPABILITY_X_COLO]) {
1096 error_setg(errp, "QEMU compiled without replication module"
1097 " can't enable COLO");
1098 error_append_hint(errp, "Please enable replication before COLO.\n");
1099 return false;
1100 }
1101#endif
1102
Peter Xu4a842142017-07-18 11:39:08 +08001103 if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +01001104 /* This check is reasonably expensive, so only when it's being
1105 * set the first time, also it's only the destination that needs
1106 * special support.
1107 */
1108 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
Alexey Perevalovd7651f12017-09-19 19:47:56 +03001109 !postcopy_ram_supported_by_host(mis)) {
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +01001110 /* postcopy_ram_supported_by_host will have emitted a more
1111 * detailed message
1112 */
Peter Xu4a842142017-07-18 11:39:08 +08001113 error_setg(errp, "Postcopy is not supported");
1114 return false;
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +01001115 }
Yury Kotov18269062019-02-15 20:45:45 +03001116
1117 if (cap_list[MIGRATION_CAPABILITY_X_IGNORE_SHARED]) {
1118 error_setg(errp, "Postcopy is not compatible with ignore-shared");
1119 return false;
1120 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001121 }
Peter Xu4a842142017-07-18 11:39:08 +08001122
1123 return true;
1124}
1125
Alexey Perevalov65ace062018-03-22 21:17:27 +03001126static void fill_destination_migration_info(MigrationInfo *info)
1127{
1128 MigrationIncomingState *mis = migration_incoming_get_current();
1129
Juan Quintela9aca82b2019-02-27 11:51:27 +01001130 if (mis->socket_address_list) {
1131 info->has_socket_address = true;
1132 info->socket_address =
1133 QAPI_CLONE(SocketAddressList, mis->socket_address_list);
1134 }
1135
Alexey Perevalov65ace062018-03-22 21:17:27 +03001136 switch (mis->state) {
1137 case MIGRATION_STATUS_NONE:
1138 return;
Alexey Perevalov65ace062018-03-22 21:17:27 +03001139 case MIGRATION_STATUS_SETUP:
1140 case MIGRATION_STATUS_CANCELLING:
1141 case MIGRATION_STATUS_CANCELLED:
1142 case MIGRATION_STATUS_ACTIVE:
1143 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Peter Xu3c9928d2018-07-10 17:18:56 +08001144 case MIGRATION_STATUS_POSTCOPY_PAUSED:
1145 case MIGRATION_STATUS_POSTCOPY_RECOVER:
Alexey Perevalov65ace062018-03-22 21:17:27 +03001146 case MIGRATION_STATUS_FAILED:
1147 case MIGRATION_STATUS_COLO:
1148 info->has_status = true;
1149 break;
1150 case MIGRATION_STATUS_COMPLETED:
1151 info->has_status = true;
1152 fill_destination_postcopy_migration_info(info);
1153 break;
1154 }
1155 info->status = mis->state;
1156}
1157
1158MigrationInfo *qmp_query_migrate(Error **errp)
1159{
1160 MigrationInfo *info = g_malloc0(sizeof(*info));
1161
1162 fill_destination_migration_info(info);
1163 fill_source_migration_info(info);
1164
1165 return info;
1166}
1167
Peter Xu4a842142017-07-18 11:39:08 +08001168void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
1169 Error **errp)
1170{
1171 MigrationState *s = migrate_get_current();
1172 MigrationCapabilityStatusList *cap;
Peter Xudd0ee302018-03-05 17:49:38 +08001173 bool cap_list[MIGRATION_CAPABILITY__MAX];
Peter Xu4a842142017-07-18 11:39:08 +08001174
Juan Quintela392d87e2020-01-21 15:39:23 +01001175 if (migration_is_running(s->state)) {
Peter Xu4a842142017-07-18 11:39:08 +08001176 error_setg(errp, QERR_MIGRATION_ACTIVE);
1177 return;
1178 }
1179
Peter Xudd0ee302018-03-05 17:49:38 +08001180 memcpy(cap_list, s->enabled_capabilities, sizeof(cap_list));
1181 if (!migrate_caps_check(cap_list, params, errp)) {
Peter Xu4a842142017-07-18 11:39:08 +08001182 return;
1183 }
1184
1185 for (cap = params; cap; cap = cap->next) {
1186 s->enabled_capabilities[cap->value->capability] = cap->value->state;
1187 }
Orit Wasserman00458432012-08-06 21:42:48 +03001188}
1189
Peter Xu16d063b2017-07-18 11:39:04 +08001190/*
1191 * Check whether the parameters are valid. Error will be put into errp
1192 * (if provided). Return true if valid, otherwise false.
1193 */
1194static bool migrate_params_check(MigrationParameters *params, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +08001195{
Eric Blake7f375e02016-09-08 22:14:16 -05001196 if (params->has_compress_level &&
Juan Quintela741d4082017-12-01 13:08:38 +01001197 (params->compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001198 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
1199 "is invalid, it should be in the range of 0 to 9");
Peter Xu16d063b2017-07-18 11:39:04 +08001200 return false;
Liang Li85de8322015-03-23 16:32:28 +08001201 }
Peter Xu16d063b2017-07-18 11:39:04 +08001202
Juan Quintela741d4082017-12-01 13:08:38 +01001203 if (params->has_compress_threads && (params->compress_threads < 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001204 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1205 "compress_threads",
1206 "is invalid, it should be in the range of 1 to 255");
Peter Xu16d063b2017-07-18 11:39:04 +08001207 return false;
Liang Li85de8322015-03-23 16:32:28 +08001208 }
Peter Xu16d063b2017-07-18 11:39:04 +08001209
Juan Quintela741d4082017-12-01 13:08:38 +01001210 if (params->has_decompress_threads && (params->decompress_threads < 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001211 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1212 "decompress_threads",
1213 "is invalid, it should be in the range of 1 to 255");
Peter Xu16d063b2017-07-18 11:39:04 +08001214 return false;
Liang Li85de8322015-03-23 16:32:28 +08001215 }
Peter Xu16d063b2017-07-18 11:39:04 +08001216
Keqian Zhudc14a472020-02-24 10:31:42 +08001217 if (params->has_throttle_trigger_threshold &&
1218 (params->throttle_trigger_threshold < 1 ||
1219 params->throttle_trigger_threshold > 100)) {
1220 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1221 "throttle_trigger_threshold",
1222 "an integer in the range of 1 to 100");
1223 return false;
1224 }
1225
Eric Blake7f375e02016-09-08 22:14:16 -05001226 if (params->has_cpu_throttle_initial &&
1227 (params->cpu_throttle_initial < 1 ||
1228 params->cpu_throttle_initial > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -04001229 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -04001230 "cpu_throttle_initial",
Jason J. Herne1626fee2015-09-08 13:12:34 -04001231 "an integer in the range of 1 to 99");
Peter Xu16d063b2017-07-18 11:39:04 +08001232 return false;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001233 }
Peter Xu16d063b2017-07-18 11:39:04 +08001234
Eric Blake7f375e02016-09-08 22:14:16 -05001235 if (params->has_cpu_throttle_increment &&
1236 (params->cpu_throttle_increment < 1 ||
1237 params->cpu_throttle_increment > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -04001238 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -04001239 "cpu_throttle_increment",
Jason J. Herne1626fee2015-09-08 13:12:34 -04001240 "an integer in the range of 1 to 99");
Peter Xu16d063b2017-07-18 11:39:04 +08001241 return false;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001242 }
Peter Xu16d063b2017-07-18 11:39:04 +08001243
Juan Quintela741d4082017-12-01 13:08:38 +01001244 if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) {
Mao Zhongyi2ee30cf2020-03-31 16:22:05 +08001245 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1246 "max_bandwidth",
1247 "an integer in the range of 0 to "stringify(SIZE_MAX)
1248 " bytes/second");
Peter Xu16d063b2017-07-18 11:39:04 +08001249 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301250 }
Peter Xu16d063b2017-07-18 11:39:04 +08001251
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301252 if (params->has_downtime_limit &&
Juan Quintela741d4082017-12-01 13:08:38 +01001253 (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
Mao Zhongyi2ee30cf2020-03-31 16:22:05 +08001254 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1255 "downtime_limit",
1256 "an integer in the range of 0 to "
Mao Zhongyi7ac55292020-03-31 16:22:07 +08001257 stringify(MAX_MIGRATE_DOWNTIME)" ms");
Peter Xu16d063b2017-07-18 11:39:04 +08001258 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301259 }
Peter Xu16d063b2017-07-18 11:39:04 +08001260
Juan Quintela741d4082017-12-01 13:08:38 +01001261 /* x_checkpoint_delay is now always positive */
1262
Juan Quintelacbfd6c92019-02-06 13:54:06 +01001263 if (params->has_multifd_channels && (params->multifd_channels < 1)) {
Juan Quintela4075fb12016-01-15 08:56:17 +01001264 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1265 "multifd_channels",
1266 "is invalid, it should be in the range of 1 to 255");
1267 return false;
1268 }
Peter Xu16d063b2017-07-18 11:39:04 +08001269
Juan Quintela9004db42020-01-23 17:08:52 +01001270 if (params->has_multifd_zlib_level &&
1271 (params->multifd_zlib_level > 9)) {
1272 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zlib_level",
1273 "is invalid, it should be in the range of 0 to 9");
1274 return false;
1275 }
1276
Juan Quintela6a9ad152020-01-23 17:41:36 +01001277 if (params->has_multifd_zstd_level &&
1278 (params->multifd_zstd_level > 20)) {
1279 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zstd_level",
1280 "is invalid, it should be in the range of 0 to 20");
1281 return false;
1282 }
1283
Juan Quintela73af8dd2017-10-05 21:30:10 +02001284 if (params->has_xbzrle_cache_size &&
1285 (params->xbzrle_cache_size < qemu_target_page_size() ||
1286 !is_power_of_2(params->xbzrle_cache_size))) {
1287 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1288 "xbzrle_cache_size",
1289 "is invalid, it should be bigger than target page size"
Mao Zhongyi06b1c6f2020-03-20 22:32:16 +08001290 " and a power of 2");
Juan Quintela73af8dd2017-10-05 21:30:10 +02001291 return false;
1292 }
1293
Li Qiang4cbc9c72018-08-01 06:00:20 -07001294 if (params->has_max_cpu_throttle &&
1295 (params->max_cpu_throttle < params->cpu_throttle_initial ||
1296 params->max_cpu_throttle > 99)) {
1297 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1298 "max_cpu_throttle",
1299 "an integer in the range of cpu_throttle_initial to 99");
1300 return false;
1301 }
1302
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00001303 if (params->has_announce_initial &&
1304 params->announce_initial > 100000) {
1305 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1306 "announce_initial",
1307 "is invalid, it must be less than 100000 ms");
1308 return false;
1309 }
1310 if (params->has_announce_max &&
1311 params->announce_max > 100000) {
1312 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1313 "announce_max",
1314 "is invalid, it must be less than 100000 ms");
1315 return false;
1316 }
1317 if (params->has_announce_rounds &&
1318 params->announce_rounds > 1000) {
1319 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1320 "announce_rounds",
1321 "is invalid, it must be in the range of 0 to 1000");
1322 return false;
1323 }
1324 if (params->has_announce_step &&
1325 (params->announce_step < 1 ||
1326 params->announce_step > 10000)) {
1327 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1328 "announce_step",
1329 "is invalid, it must be in the range of 1 to 10000 ms");
1330 return false;
1331 }
Max Reitz31e4c352020-08-20 17:07:23 +02001332
1333 if (params->has_block_bitmap_mapping &&
1334 !check_dirty_bitmap_mig_alias_map(params->block_bitmap_mapping, errp)) {
1335 error_prepend(errp, "Invalid mapping given for block-bitmap-mapping: ");
1336 return false;
1337 }
1338
Peter Xu16d063b2017-07-18 11:39:04 +08001339 return true;
1340}
1341
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001342static void migrate_params_test_apply(MigrateSetParameters *params,
1343 MigrationParameters *dest)
1344{
1345 *dest = migrate_get_current()->parameters;
1346
1347 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1348
1349 if (params->has_compress_level) {
1350 dest->compress_level = params->compress_level;
1351 }
1352
1353 if (params->has_compress_threads) {
1354 dest->compress_threads = params->compress_threads;
1355 }
1356
Xiao Guangrong1d588722018-08-21 16:10:20 +08001357 if (params->has_compress_wait_thread) {
1358 dest->compress_wait_thread = params->compress_wait_thread;
1359 }
1360
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001361 if (params->has_decompress_threads) {
1362 dest->decompress_threads = params->decompress_threads;
1363 }
1364
Keqian Zhudc14a472020-02-24 10:31:42 +08001365 if (params->has_throttle_trigger_threshold) {
1366 dest->throttle_trigger_threshold = params->throttle_trigger_threshold;
1367 }
1368
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001369 if (params->has_cpu_throttle_initial) {
1370 dest->cpu_throttle_initial = params->cpu_throttle_initial;
1371 }
1372
1373 if (params->has_cpu_throttle_increment) {
1374 dest->cpu_throttle_increment = params->cpu_throttle_increment;
1375 }
1376
Keqian Zhucbbf8182020-04-13 18:15:08 +08001377 if (params->has_cpu_throttle_tailslow) {
1378 dest->cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1379 }
1380
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001381 if (params->has_tls_creds) {
Markus Armbruster01fa5592017-07-18 14:42:04 +02001382 assert(params->tls_creds->type == QTYPE_QSTRING);
Zheng Chuan9728ebf2020-07-09 16:28:25 +08001383 dest->tls_creds = params->tls_creds->u.s;
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001384 }
1385
1386 if (params->has_tls_hostname) {
Markus Armbruster01fa5592017-07-18 14:42:04 +02001387 assert(params->tls_hostname->type == QTYPE_QSTRING);
Zheng Chuan9728ebf2020-07-09 16:28:25 +08001388 dest->tls_hostname = params->tls_hostname->u.s;
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001389 }
1390
1391 if (params->has_max_bandwidth) {
1392 dest->max_bandwidth = params->max_bandwidth;
1393 }
1394
1395 if (params->has_downtime_limit) {
1396 dest->downtime_limit = params->downtime_limit;
1397 }
1398
1399 if (params->has_x_checkpoint_delay) {
1400 dest->x_checkpoint_delay = params->x_checkpoint_delay;
1401 }
1402
1403 if (params->has_block_incremental) {
1404 dest->block_incremental = params->block_incremental;
1405 }
Juan Quintelacbfd6c92019-02-06 13:54:06 +01001406 if (params->has_multifd_channels) {
1407 dest->multifd_channels = params->multifd_channels;
Juan Quintela5e7577a2017-10-09 18:07:56 +02001408 }
Juan Quintela96eef042019-01-16 10:35:55 +01001409 if (params->has_multifd_compression) {
1410 dest->multifd_compression = params->multifd_compression;
1411 }
Juan Quintela73af8dd2017-10-05 21:30:10 +02001412 if (params->has_xbzrle_cache_size) {
1413 dest->xbzrle_cache_size = params->xbzrle_cache_size;
1414 }
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01001415 if (params->has_max_postcopy_bandwidth) {
1416 dest->max_postcopy_bandwidth = params->max_postcopy_bandwidth;
1417 }
Li Qiang4cbc9c72018-08-01 06:00:20 -07001418 if (params->has_max_cpu_throttle) {
1419 dest->max_cpu_throttle = params->max_cpu_throttle;
1420 }
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00001421 if (params->has_announce_initial) {
1422 dest->announce_initial = params->announce_initial;
1423 }
1424 if (params->has_announce_max) {
1425 dest->announce_max = params->announce_max;
1426 }
1427 if (params->has_announce_rounds) {
1428 dest->announce_rounds = params->announce_rounds;
1429 }
1430 if (params->has_announce_step) {
1431 dest->announce_step = params->announce_step;
1432 }
Max Reitz31e4c352020-08-20 17:07:23 +02001433
1434 if (params->has_block_bitmap_mapping) {
1435 dest->has_block_bitmap_mapping = true;
1436 dest->block_bitmap_mapping = params->block_bitmap_mapping;
1437 }
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001438}
1439
Juan Quintela73af8dd2017-10-05 21:30:10 +02001440static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
Peter Xu16d063b2017-07-18 11:39:04 +08001441{
1442 MigrationState *s = migrate_get_current();
1443
Markus Armbrustere87fae42017-07-18 12:57:38 +02001444 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1445
Eric Blake7f375e02016-09-08 22:14:16 -05001446 if (params->has_compress_level) {
1447 s->parameters.compress_level = params->compress_level;
Liang Li85de8322015-03-23 16:32:28 +08001448 }
Peter Xu476c72a2017-07-18 11:39:05 +08001449
Eric Blake7f375e02016-09-08 22:14:16 -05001450 if (params->has_compress_threads) {
1451 s->parameters.compress_threads = params->compress_threads;
Liang Li85de8322015-03-23 16:32:28 +08001452 }
Peter Xu476c72a2017-07-18 11:39:05 +08001453
Xiao Guangrong1d588722018-08-21 16:10:20 +08001454 if (params->has_compress_wait_thread) {
1455 s->parameters.compress_wait_thread = params->compress_wait_thread;
1456 }
1457
Eric Blake7f375e02016-09-08 22:14:16 -05001458 if (params->has_decompress_threads) {
1459 s->parameters.decompress_threads = params->decompress_threads;
Liang Li85de8322015-03-23 16:32:28 +08001460 }
Peter Xu476c72a2017-07-18 11:39:05 +08001461
Keqian Zhudc14a472020-02-24 10:31:42 +08001462 if (params->has_throttle_trigger_threshold) {
1463 s->parameters.throttle_trigger_threshold = params->throttle_trigger_threshold;
1464 }
1465
Eric Blake7f375e02016-09-08 22:14:16 -05001466 if (params->has_cpu_throttle_initial) {
1467 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001468 }
Peter Xu476c72a2017-07-18 11:39:05 +08001469
Eric Blake7f375e02016-09-08 22:14:16 -05001470 if (params->has_cpu_throttle_increment) {
1471 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001472 }
Peter Xu476c72a2017-07-18 11:39:05 +08001473
Keqian Zhucbbf8182020-04-13 18:15:08 +08001474 if (params->has_cpu_throttle_tailslow) {
1475 s->parameters.cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1476 }
1477
Eric Blake7f375e02016-09-08 22:14:16 -05001478 if (params->has_tls_creds) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001479 g_free(s->parameters.tls_creds);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001480 assert(params->tls_creds->type == QTYPE_QSTRING);
1481 s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001482 }
Peter Xu476c72a2017-07-18 11:39:05 +08001483
Eric Blake7f375e02016-09-08 22:14:16 -05001484 if (params->has_tls_hostname) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001485 g_free(s->parameters.tls_hostname);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001486 assert(params->tls_hostname->type == QTYPE_QSTRING);
1487 s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001488 }
Peter Xu476c72a2017-07-18 11:39:05 +08001489
Daniel P. Berranged2f1d292019-02-27 14:53:24 +00001490 if (params->has_tls_authz) {
1491 g_free(s->parameters.tls_authz);
1492 assert(params->tls_authz->type == QTYPE_QSTRING);
1493 s->parameters.tls_authz = g_strdup(params->tls_authz->u.s);
1494 }
1495
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301496 if (params->has_max_bandwidth) {
1497 s->parameters.max_bandwidth = params->max_bandwidth;
Dr. David Alan Gilbertc38c1c12019-03-08 10:12:10 +00001498 if (s->to_dst_file && !migration_in_postcopy()) {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301499 qemu_file_set_rate_limit(s->to_dst_file,
1500 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
1501 }
1502 }
Peter Xu476c72a2017-07-18 11:39:05 +08001503
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301504 if (params->has_downtime_limit) {
1505 s->parameters.downtime_limit = params->downtime_limit;
1506 }
zhanghailiang68b53592016-10-27 14:43:01 +08001507
1508 if (params->has_x_checkpoint_delay) {
1509 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
zhanghailiang479125d2017-01-17 20:57:42 +08001510 if (migration_in_colo_state()) {
1511 colo_checkpoint_notify(s);
1512 }
zhanghailiang68b53592016-10-27 14:43:01 +08001513 }
Peter Xu476c72a2017-07-18 11:39:05 +08001514
Juan Quintela2833c592017-04-05 18:32:37 +02001515 if (params->has_block_incremental) {
1516 s->parameters.block_incremental = params->block_incremental;
1517 }
Juan Quintelacbfd6c92019-02-06 13:54:06 +01001518 if (params->has_multifd_channels) {
1519 s->parameters.multifd_channels = params->multifd_channels;
Juan Quintela4075fb12016-01-15 08:56:17 +01001520 }
Juan Quintela96eef042019-01-16 10:35:55 +01001521 if (params->has_multifd_compression) {
1522 s->parameters.multifd_compression = params->multifd_compression;
1523 }
Juan Quintela73af8dd2017-10-05 21:30:10 +02001524 if (params->has_xbzrle_cache_size) {
1525 s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
1526 xbzrle_cache_resize(params->xbzrle_cache_size, errp);
1527 }
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01001528 if (params->has_max_postcopy_bandwidth) {
1529 s->parameters.max_postcopy_bandwidth = params->max_postcopy_bandwidth;
Dr. David Alan Gilbertc38c1c12019-03-08 10:12:10 +00001530 if (s->to_dst_file && migration_in_postcopy()) {
1531 qemu_file_set_rate_limit(s->to_dst_file,
1532 s->parameters.max_postcopy_bandwidth / XFER_LIMIT_RATIO);
1533 }
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01001534 }
Li Qiang4cbc9c72018-08-01 06:00:20 -07001535 if (params->has_max_cpu_throttle) {
1536 s->parameters.max_cpu_throttle = params->max_cpu_throttle;
1537 }
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00001538 if (params->has_announce_initial) {
1539 s->parameters.announce_initial = params->announce_initial;
1540 }
1541 if (params->has_announce_max) {
1542 s->parameters.announce_max = params->announce_max;
1543 }
1544 if (params->has_announce_rounds) {
1545 s->parameters.announce_rounds = params->announce_rounds;
1546 }
1547 if (params->has_announce_step) {
1548 s->parameters.announce_step = params->announce_step;
1549 }
Max Reitz31e4c352020-08-20 17:07:23 +02001550
1551 if (params->has_block_bitmap_mapping) {
1552 qapi_free_BitmapMigrationNodeAliasList(
1553 s->parameters.block_bitmap_mapping);
1554
1555 s->parameters.has_block_bitmap_mapping = true;
1556 s->parameters.block_bitmap_mapping =
1557 QAPI_CLONE(BitmapMigrationNodeAliasList,
1558 params->block_bitmap_mapping);
1559 }
Liang Li85de8322015-03-23 16:32:28 +08001560}
1561
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001562void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
Peter Xu476c72a2017-07-18 11:39:05 +08001563{
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001564 MigrationParameters tmp;
1565
Markus Armbruster01fa5592017-07-18 14:42:04 +02001566 /* TODO Rewrite "" to null instead */
1567 if (params->has_tls_creds
1568 && params->tls_creds->type == QTYPE_QNULL) {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001569 qobject_unref(params->tls_creds->u.n);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001570 params->tls_creds->type = QTYPE_QSTRING;
1571 params->tls_creds->u.s = strdup("");
1572 }
1573 /* TODO Rewrite "" to null instead */
1574 if (params->has_tls_hostname
1575 && params->tls_hostname->type == QTYPE_QNULL) {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001576 qobject_unref(params->tls_hostname->u.n);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001577 params->tls_hostname->type = QTYPE_QSTRING;
1578 params->tls_hostname->u.s = strdup("");
1579 }
1580
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001581 migrate_params_test_apply(params, &tmp);
1582
1583 if (!migrate_params_check(&tmp, errp)) {
Peter Xu476c72a2017-07-18 11:39:05 +08001584 /* Invalid parameter */
1585 return;
1586 }
1587
Juan Quintela73af8dd2017-10-05 21:30:10 +02001588 migrate_params_apply(params, errp);
Peter Xu476c72a2017-07-18 11:39:05 +08001589}
1590
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001591
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001592void qmp_migrate_start_postcopy(Error **errp)
1593{
1594 MigrationState *s = migrate_get_current();
1595
Vladimir Sementsov-Ogievskiy16b0fd32018-03-13 15:34:01 -04001596 if (!migrate_postcopy()) {
Dr. David Alan Gilberta54d3402015-11-12 11:34:44 +00001597 error_setg(errp, "Enable postcopy with migrate_set_capability before"
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001598 " the start of migration");
1599 return;
1600 }
1601
1602 if (s->state == MIGRATION_STATUS_NONE) {
1603 error_setg(errp, "Postcopy must be started after migration has been"
1604 " started");
1605 return;
1606 }
1607 /*
1608 * we don't error if migration has finished since that would be racy
1609 * with issuing this command.
1610 */
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001611 qatomic_set(&s->start_postcopy, true);
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001612}
1613
aliguori065e2812008-11-11 16:46:33 +00001614/* shared migration helpers */
1615
zhanghailiang48781e52015-12-16 11:47:33 +00001616void migrate_set_state(int *state, int old_state, int new_state)
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +00001617{
Peter Xua31fede2017-08-30 16:32:01 +08001618 assert(new_state < MIGRATION_STATUS__MAX);
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001619 if (qatomic_cmpxchg(state, old_state, new_state) == old_state) {
Peter Xua31fede2017-08-30 16:32:01 +08001620 trace_migrate_set_state(MigrationStatus_str(new_state));
Juan Quintelab05dc722015-07-07 14:44:05 +02001621 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +00001622 }
1623}
1624
Peter Xu4e4a3d32017-07-18 11:39:09 +08001625static MigrationCapabilityStatusList *migrate_cap_add(
1626 MigrationCapabilityStatusList *list,
1627 MigrationCapability index,
1628 bool state)
Juan Quintela2833c592017-04-05 18:32:37 +02001629{
1630 MigrationCapabilityStatusList *cap;
1631
1632 cap = g_new0(MigrationCapabilityStatusList, 1);
1633 cap->value = g_new0(MigrationCapabilityStatus, 1);
Peter Xu4e4a3d32017-07-18 11:39:09 +08001634 cap->value->capability = index;
1635 cap->value->state = state;
1636 cap->next = list;
1637
1638 return cap;
1639}
1640
1641void migrate_set_block_enabled(bool value, Error **errp)
1642{
1643 MigrationCapabilityStatusList *cap;
1644
1645 cap = migrate_cap_add(NULL, MIGRATION_CAPABILITY_BLOCK, value);
Juan Quintela2833c592017-04-05 18:32:37 +02001646 qmp_migrate_set_capabilities(cap, errp);
1647 qapi_free_MigrationCapabilityStatusList(cap);
1648}
1649
1650static void migrate_set_block_incremental(MigrationState *s, bool value)
1651{
1652 s->parameters.block_incremental = value;
1653}
1654
1655static void block_cleanup_parameters(MigrationState *s)
1656{
1657 if (s->must_remove_block_options) {
1658 /* setting to false can never fail */
1659 migrate_set_block_enabled(false, &error_abort);
1660 migrate_set_block_incremental(s, false);
1661 s->must_remove_block_options = false;
1662 }
1663}
1664
Yury Kotovfd392cf2019-04-08 14:33:43 +03001665static void migrate_fd_cleanup(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +00001666{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001667 qemu_bh_delete(s->cleanup_bh);
1668 s->cleanup_bh = NULL;
1669
Peter Xu0ceccd82018-01-03 20:20:06 +08001670 qemu_savevm_state_cleanup();
1671
zhanghailiang89a02a92016-01-15 11:37:42 +08001672 if (s->to_dst_file) {
Peter Xu62df0662018-05-02 18:47:38 +08001673 QEMUFile *tmp;
Juan Quintelaf986c3d2016-01-14 16:52:55 +01001674
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001675 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001676 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001677 if (s->migration_thread_running) {
1678 qemu_thread_join(&s->thread);
1679 s->migration_thread_running = false;
1680 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001681 qemu_mutex_lock_iothread();
1682
Fei Li1398b2e2019-01-13 22:08:47 +08001683 multifd_save_cleanup();
Peter Xu62df0662018-05-02 18:47:38 +08001684 qemu_mutex_lock(&s->qemu_file_lock);
1685 tmp = s->to_dst_file;
zhanghailiang89a02a92016-01-15 11:37:42 +08001686 s->to_dst_file = NULL;
Peter Xu62df0662018-05-02 18:47:38 +08001687 qemu_mutex_unlock(&s->qemu_file_lock);
1688 /*
1689 * Close the file handle without the lock to make sure the
1690 * critical section won't block for long.
1691 */
1692 qemu_fclose(tmp);
aliguori065e2812008-11-11 16:46:33 +00001693 }
1694
Wei Yang8f8d5282019-07-17 08:53:41 +08001695 assert(!migration_is_active(s));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001696
Liang Li94f5a432015-11-02 15:37:00 +08001697 if (s->state == MIGRATION_STATUS_CANCELLING) {
zhanghailiang48781e52015-12-16 11:47:33 +00001698 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
Liang Li94f5a432015-11-02 15:37:00 +08001699 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001700 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001701
Juan Quintela87db1a72017-09-05 12:50:22 +02001702 if (s->error) {
1703 /* It is used on info migrate. We can't free it */
1704 error_report_err(error_copy(s->error));
1705 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001706 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +02001707 block_cleanup_parameters(s);
aliguori065e2812008-11-11 16:46:33 +00001708}
1709
Yury Kotovfd392cf2019-04-08 14:33:43 +03001710static void migrate_fd_cleanup_schedule(MigrationState *s)
1711{
1712 /*
1713 * Ref the state for bh, because it may be called when
1714 * there're already no other refs
1715 */
1716 object_ref(OBJECT(s));
1717 qemu_bh_schedule(s->cleanup_bh);
1718}
1719
1720static void migrate_fd_cleanup_bh(void *opaque)
1721{
1722 MigrationState *s = opaque;
1723 migrate_fd_cleanup(s);
1724 object_unref(OBJECT(s));
1725}
1726
Juan Quintela87db1a72017-09-05 12:50:22 +02001727void migrate_set_error(MigrationState *s, const Error *error)
1728{
Daniel Brodsky6e8a3552020-04-03 21:21:08 -07001729 QEMU_LOCK_GUARD(&s->error_mutex);
Juan Quintela87db1a72017-09-05 12:50:22 +02001730 if (!s->error) {
1731 s->error = error_copy(error);
1732 }
Juan Quintela87db1a72017-09-05 12:50:22 +02001733}
1734
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001735void migrate_fd_error(MigrationState *s, const Error *error)
aliguori065e2812008-11-11 16:46:33 +00001736{
Peter Maydell25174052016-10-21 18:41:45 +01001737 trace_migrate_fd_error(error_get_pretty(error));
zhanghailiang89a02a92016-01-15 11:37:42 +08001738 assert(s->to_dst_file == NULL);
zhanghailiang48781e52015-12-16 11:47:33 +00001739 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1740 MIGRATION_STATUS_FAILED);
Juan Quintela87db1a72017-09-05 12:50:22 +02001741 migrate_set_error(s, error);
Juan Quintela458cf282011-02-22 23:32:54 +01001742}
1743
Juan Quintela0edda1c2010-05-11 16:28:39 +02001744static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +00001745{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001746 int old_state ;
zhanghailiang89a02a92016-01-15 11:37:42 +08001747 QEMUFile *f = migrate_get_current()->to_dst_file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001748 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +00001749
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001750 if (s->rp_state.from_dst_file) {
1751 /* shutdown the rp socket, so causing the rp thread to shutdown */
1752 qemu_file_shutdown(s->rp_state.from_dst_file);
1753 }
1754
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001755 do {
1756 old_state = s->state;
Juan Quintela392d87e2020-01-21 15:39:23 +01001757 if (!migration_is_running(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001758 break;
1759 }
Dr. David Alan Gilberta7b36b42017-10-20 10:05:55 +01001760 /* If the migration is paused, kick it out of the pause */
1761 if (old_state == MIGRATION_STATUS_PRE_SWITCHOVER) {
1762 qemu_sem_post(&s->pause_sem);
1763 }
zhanghailiang48781e52015-12-16 11:47:33 +00001764 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
zhanghailiang31194732015-03-13 16:08:38 +08001765 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001766
1767 /*
1768 * If we're unlucky the migration code might be stuck somewhere in a
1769 * send/write while the network has failed and is waiting to timeout;
1770 * if we've got shutdown(2) available then we can force it to quit.
1771 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1772 * called in a bh, so there is no race against this cancel.
1773 */
zhanghailiang31194732015-03-13 16:08:38 +08001774 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001775 qemu_file_shutdown(f);
1776 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08001777 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
1778 Error *local_err = NULL;
1779
1780 bdrv_invalidate_cache_all(&local_err);
1781 if (local_err) {
1782 error_report_err(local_err);
1783 } else {
1784 s->block_inactive = false;
1785 }
1786 }
aliguori065e2812008-11-11 16:46:33 +00001787}
1788
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001789void add_migration_state_change_notifier(Notifier *notify)
1790{
1791 notifier_list_add(&migration_state_notifiers, notify);
1792}
1793
1794void remove_migration_state_change_notifier(Notifier *notify)
1795{
Paolo Bonzini31552522012-01-13 17:34:01 +01001796 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001797}
1798
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +02001799bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001800{
zhanghailiang31194732015-03-13 16:08:38 +08001801 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001802}
1803
Juan Quintela70736932011-02-23 00:43:59 +01001804bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001805{
zhanghailiang31194732015-03-13 16:08:38 +08001806 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001807}
Juan Quintela0edda1c2010-05-11 16:28:39 +02001808
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001809bool migration_has_failed(MigrationState *s)
1810{
zhanghailiang31194732015-03-13 16:08:38 +08001811 return (s->state == MIGRATION_STATUS_CANCELLED ||
1812 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001813}
1814
Juan Quintela57273092017-03-20 22:25:28 +01001815bool migration_in_postcopy(void)
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001816{
Juan Quintela57273092017-03-20 22:25:28 +01001817 MigrationState *s = migrate_get_current();
1818
Dr. David Alan Gilbert3748fef2019-09-23 18:49:42 +01001819 switch (s->state) {
1820 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1821 case MIGRATION_STATUS_POSTCOPY_PAUSED:
1822 case MIGRATION_STATUS_POSTCOPY_RECOVER:
1823 return true;
1824 default:
1825 return false;
1826 }
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001827}
1828
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001829bool migration_in_postcopy_after_devices(MigrationState *s)
1830{
Juan Quintela57273092017-03-20 22:25:28 +01001831 return migration_in_postcopy() && s->postcopy_after_devices;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001832}
1833
David Hildenbrand06df2e62020-06-26 09:22:33 +02001834bool migration_in_incoming_postcopy(void)
1835{
1836 PostcopyState ps = postcopy_state_get();
1837
1838 return ps >= POSTCOPY_INCOMING_DISCARD && ps < POSTCOPY_INCOMING_END;
1839}
1840
Juan Quintelafab35002017-03-22 17:36:57 +01001841bool migration_is_idle(void)
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301842{
Markus Armbrusterdaff7f02019-04-01 11:08:25 +02001843 MigrationState *s = current_migration;
1844
1845 if (!s) {
1846 return true;
1847 }
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301848
1849 switch (s->state) {
1850 case MIGRATION_STATUS_NONE:
1851 case MIGRATION_STATUS_CANCELLED:
1852 case MIGRATION_STATUS_COMPLETED:
1853 case MIGRATION_STATUS_FAILED:
1854 return true;
1855 case MIGRATION_STATUS_SETUP:
1856 case MIGRATION_STATUS_CANCELLING:
1857 case MIGRATION_STATUS_ACTIVE:
1858 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1859 case MIGRATION_STATUS_COLO:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +01001860 case MIGRATION_STATUS_PRE_SWITCHOVER:
1861 case MIGRATION_STATUS_DEVICE:
Jens Freimannc7e0acd2019-10-29 12:49:02 +01001862 case MIGRATION_STATUS_WAIT_UNPLUG:
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301863 return false;
1864 case MIGRATION_STATUS__MAX:
1865 g_assert_not_reached();
1866 }
1867
1868 return false;
1869}
1870
Wei Yang8f8d5282019-07-17 08:53:41 +08001871bool migration_is_active(MigrationState *s)
1872{
1873 return (s->state == MIGRATION_STATUS_ACTIVE ||
1874 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1875}
1876
Peter Xu3e0c8052018-02-08 18:31:15 +08001877void migrate_init(MigrationState *s)
Juan Quintela0edda1c2010-05-11 16:28:39 +02001878{
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001879 /*
1880 * Reinitialise all migration state, except
1881 * parameters/capabilities that the user set, and
1882 * locks.
1883 */
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001884 s->cleanup_bh = 0;
zhanghailiang89a02a92016-01-15 11:37:42 +08001885 s->to_dst_file = NULL;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001886 s->state = MIGRATION_STATUS_NONE;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001887 s->rp_state.from_dst_file = NULL;
1888 s->rp_state.error = false;
1889 s->mbps = 0.0;
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08001890 s->pages_per_second = 0.0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001891 s->downtime = 0;
1892 s->expected_downtime = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001893 s->setup_time = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001894 s->start_postcopy = false;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001895 s->postcopy_after_devices = false;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001896 s->migration_thread_running = false;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001897 error_free(s->error);
1898 s->error = NULL;
Chuan Zhengd8053e72020-09-15 11:03:57 +08001899 s->hostname = NULL;
Juan Quintela1299c632011-11-09 21:29:01 +01001900
zhanghailiang48781e52015-12-16 11:47:33 +00001901 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +02001902
Peter Xu4af246a2018-01-03 20:20:08 +08001903 s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1904 s->total_time = 0;
Peter Xu7287cbd2018-01-03 20:20:09 +08001905 s->vm_was_running = false;
Peter Xub15df1a2018-01-03 20:20:13 +08001906 s->iteration_initial_bytes = 0;
1907 s->threshold_size = 0;
Juan Quintela0edda1c2010-05-11 16:28:39 +02001908}
Juan Quintelacab30142011-02-22 23:54:21 +01001909
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001910static GSList *migration_blockers;
1911
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301912int migrate_add_blocker(Error *reason, Error **errp)
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001913{
Markus Armbruster811f8652019-04-01 11:08:24 +02001914 if (only_migratable) {
Markus Armbruster4b576642018-10-17 10:26:25 +02001915 error_propagate_prepend(errp, error_copy(reason),
1916 "disallowing migration blocker "
Greg Kurzb6eca812019-05-16 12:58:05 +02001917 "(--only-migratable) for: ");
Ashijeet Acharyab67b8c32017-01-16 17:01:54 +05301918 return -EACCES;
1919 }
1920
Juan Quintelafab35002017-03-22 17:36:57 +01001921 if (migration_is_idle()) {
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301922 migration_blockers = g_slist_prepend(migration_blockers, reason);
1923 return 0;
1924 }
1925
Markus Armbruster4b576642018-10-17 10:26:25 +02001926 error_propagate_prepend(errp, error_copy(reason),
1927 "disallowing migration blocker "
1928 "(migration in progress) for: ");
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301929 return -EBUSY;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001930}
1931
1932void migrate_del_blocker(Error *reason)
1933{
1934 migration_blockers = g_slist_remove(migration_blockers, reason);
1935}
1936
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001937void qmp_migrate_incoming(const char *uri, Error **errp)
1938{
1939 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001940 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001941
1942 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001943 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001944 return;
1945 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001946 if (!once) {
1947 error_setg(errp, "The incoming migration has already been started");
Yury Kotov603d5a42019-11-13 20:53:25 +03001948 return;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001949 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001950
1951 qemu_start_incoming_migration(uri, &local_err);
1952
1953 if (local_err) {
1954 error_propagate(errp, local_err);
1955 return;
1956 }
1957
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001958 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001959}
1960
Peter Xu02affd42018-05-02 18:47:36 +08001961void qmp_migrate_recover(const char *uri, Error **errp)
1962{
1963 MigrationIncomingState *mis = migration_incoming_get_current();
1964
1965 if (mis->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
1966 error_setg(errp, "Migrate recover can only be run "
1967 "when postcopy is paused.");
1968 return;
1969 }
1970
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001971 if (qatomic_cmpxchg(&mis->postcopy_recover_triggered,
Peter Xu02affd42018-05-02 18:47:36 +08001972 false, true) == true) {
1973 error_setg(errp, "Migrate recovery is triggered already");
1974 return;
1975 }
1976
1977 /*
1978 * Note that this call will never start a real migration; it will
1979 * only re-setup the migration stream and poke existing migration
1980 * to continue using that newly established channel.
1981 */
1982 qemu_start_incoming_migration(uri, errp);
1983}
1984
Peter Xubfbf89c2018-05-02 18:47:39 +08001985void qmp_migrate_pause(Error **errp)
1986{
1987 MigrationState *ms = migrate_get_current();
1988 MigrationIncomingState *mis = migration_incoming_get_current();
1989 int ret;
1990
1991 if (ms->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1992 /* Source side, during postcopy */
1993 qemu_mutex_lock(&ms->qemu_file_lock);
1994 ret = qemu_file_shutdown(ms->to_dst_file);
1995 qemu_mutex_unlock(&ms->qemu_file_lock);
1996 if (ret) {
1997 error_setg(errp, "Failed to pause source migration");
1998 }
1999 return;
2000 }
2001
2002 if (mis->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2003 ret = qemu_file_shutdown(mis->from_src_file);
2004 if (ret) {
2005 error_setg(errp, "Failed to pause destination migration");
2006 }
2007 return;
2008 }
2009
2010 error_setg(errp, "migrate-pause is currently only supported "
2011 "during postcopy-active state");
2012}
2013
Greg Kurz24f39022016-05-04 21:44:19 +02002014bool migration_is_blocked(Error **errp)
2015{
2016 if (qemu_savevm_state_blocked(errp)) {
2017 return true;
2018 }
2019
2020 if (migration_blockers) {
Eduardo Habkost250561e2017-06-08 10:39:05 -03002021 error_propagate(errp, error_copy(migration_blockers->data));
Greg Kurz24f39022016-05-04 21:44:19 +02002022 return true;
2023 }
2024
2025 return false;
2026}
2027
Peter Xud3e35b82018-05-02 18:47:24 +08002028/* Returns true if continue to migrate, or false if error detected */
2029static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc,
2030 bool resume, Error **errp)
2031{
2032 Error *local_err = NULL;
2033
2034 if (resume) {
2035 if (s->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
2036 error_setg(errp, "Cannot resume if there is no "
2037 "paused migration");
2038 return false;
2039 }
Peter Xu97ca2112018-07-23 20:33:03 +08002040
2041 /*
2042 * Postcopy recovery won't work well with release-ram
2043 * capability since release-ram will drop the page buffer as
2044 * long as the page is put into the send buffer. So if there
2045 * is a network failure happened, any page buffers that have
2046 * not yet reached the destination VM but have already been
2047 * sent from the source VM will be lost forever. Let's refuse
2048 * the client from resuming such a postcopy migration.
2049 * Luckily release-ram was designed to only be used when src
2050 * and destination VMs are on the same host, so it should be
2051 * fine.
2052 */
2053 if (migrate_release_ram()) {
2054 error_setg(errp, "Postcopy recovery cannot work "
2055 "when release-ram capability is set");
2056 return false;
2057 }
2058
Peter Xud3e35b82018-05-02 18:47:24 +08002059 /* This is a resume, skip init status */
2060 return true;
2061 }
2062
Juan Quintela392d87e2020-01-21 15:39:23 +01002063 if (migration_is_running(s->state)) {
Peter Xud3e35b82018-05-02 18:47:24 +08002064 error_setg(errp, QERR_MIGRATION_ACTIVE);
2065 return false;
2066 }
2067
2068 if (runstate_check(RUN_STATE_INMIGRATE)) {
2069 error_setg(errp, "Guest is waiting for an incoming migration");
2070 return false;
2071 }
2072
2073 if (migration_is_blocked(errp)) {
2074 return false;
2075 }
2076
2077 if (blk || blk_inc) {
2078 if (migrate_use_block() || migrate_use_block_incremental()) {
2079 error_setg(errp, "Command options are incompatible with "
2080 "current migration capabilities");
2081 return false;
2082 }
2083 migrate_set_block_enabled(true, &local_err);
2084 if (local_err) {
2085 error_propagate(errp, local_err);
2086 return false;
2087 }
2088 s->must_remove_block_options = true;
2089 }
2090
2091 if (blk_inc) {
2092 migrate_set_block_incremental(s, true);
2093 }
2094
2095 migrate_init(s);
Ivan Ren87f3bd82019-08-02 18:18:41 +08002096 /*
2097 * set ram_counters memory to zero for a
2098 * new migration
2099 */
2100 memset(&ram_counters, 0, sizeof(ram_counters));
Peter Xud3e35b82018-05-02 18:47:24 +08002101
2102 return true;
2103}
2104
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002105void qmp_migrate(const char *uri, bool has_blk, bool blk,
2106 bool has_inc, bool inc, bool has_detach, bool detach,
Peter Xu7a4da282018-05-02 18:47:23 +08002107 bool has_resume, bool resume, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002108{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +02002109 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +02002110 MigrationState *s = migrate_get_current();
Longpeng(Mike)d658f652020-08-06 15:40:29 +08002111 const char *p = NULL;
Juan Quintelacab30142011-02-22 23:54:21 +01002112
Peter Xud3e35b82018-05-02 18:47:24 +08002113 if (!migrate_prepare(s, has_blk && blk, has_inc && inc,
2114 has_resume && resume, errp)) {
2115 /* Error detected, put into errp */
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002116 return;
Juan Quintelacab30142011-02-22 23:54:21 +01002117 }
Juan Quintelacab30142011-02-22 23:54:21 +01002118
Longpeng(Mike)d658f652020-08-06 15:40:29 +08002119 if (strstart(uri, "tcp:", &p) ||
Longpeng(Mike)9ba3b2b2020-08-06 15:40:30 +08002120 strstart(uri, "unix:", NULL) ||
2121 strstart(uri, "vsock:", NULL)) {
Longpeng(Mike)d658f652020-08-06 15:40:29 +08002122 socket_start_outgoing_migration(s, p ? p : uri, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04002123#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08002124 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04002125 rdma_start_outgoing_migration(s, p, &local_err);
2126#endif
Juan Quintelacab30142011-02-22 23:54:21 +01002127 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002128 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01002129 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002130 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01002131 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002132 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
2133 "a valid migration protocol");
zhanghailiang48781e52015-12-16 11:47:33 +00002134 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2135 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09576e72018-03-16 20:21:14 +00002136 block_cleanup_parameters(s);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002137 return;
Juan Quintelacab30142011-02-22 23:54:21 +01002138 }
2139
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002140 if (local_err) {
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01002141 migrate_fd_error(s, local_err);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002142 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002143 return;
Juan Quintela1299c632011-11-09 21:29:01 +01002144 }
Juan Quintelacab30142011-02-22 23:54:21 +01002145}
2146
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02002147void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002148{
Juan Quintela17549e82011-10-05 13:50:43 +02002149 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01002150}
2151
Dr. David Alan Gilbert89cfc022017-10-20 10:05:53 +01002152void qmp_migrate_continue(MigrationStatus state, Error **errp)
2153{
2154 MigrationState *s = migrate_get_current();
2155 if (s->state != state) {
2156 error_setg(errp, "Migration not in expected state: %s",
2157 MigrationStatus_str(s->state));
2158 return;
2159 }
2160 qemu_sem_post(&s->pause_sem);
2161}
2162
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002163void qmp_migrate_set_cache_size(int64_t value, Error **errp)
2164{
Juan Quintela73af8dd2017-10-05 21:30:10 +02002165 MigrateSetParameters p = {
2166 .has_xbzrle_cache_size = true,
2167 .xbzrle_cache_size = value,
2168 };
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002169
Juan Quintela73af8dd2017-10-05 21:30:10 +02002170 qmp_migrate_set_parameters(&p, errp);
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002171}
2172
2173int64_t qmp_query_migrate_cache_size(Error **errp)
2174{
2175 return migrate_xbzrle_cache_size();
2176}
2177
Luiz Capitulino3dc85382011-11-28 11:59:37 -02002178void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002179{
Markus Armbruster1bda8b32017-07-18 13:42:11 +02002180 MigrateSetParameters p = {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302181 .has_max_bandwidth = true,
2182 .max_bandwidth = value,
2183 };
Juan Quintelacab30142011-02-22 23:54:21 +01002184
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302185 qmp_migrate_set_parameters(&p, errp);
Juan Quintelacab30142011-02-22 23:54:21 +01002186}
2187
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02002188void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002189{
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03002190 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
Mao Zhongyi2ee30cf2020-03-31 16:22:05 +08002191 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
2192 "downtime_limit",
2193 "an integer in the range of 0 to "
2194 stringify(MAX_MIGRATE_DOWNTIME_SECONDS)" seconds");
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03002195 return;
2196 }
2197
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302198 value *= 1000; /* Convert to milliseconds */
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302199
Markus Armbruster1bda8b32017-07-18 13:42:11 +02002200 MigrateSetParameters p = {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302201 .has_downtime_limit = true,
Fangrui Song2667c982019-11-22 09:00:38 +01002202 .downtime_limit = (int64_t)value,
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302203 };
2204
2205 qmp_migrate_set_parameters(&p, errp);
aliguori5bb79102008-10-13 03:12:02 +00002206}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03002207
Pavel Butsykin53f09a12017-02-03 18:23:20 +03002208bool migrate_release_ram(void)
2209{
2210 MigrationState *s;
2211
2212 s = migrate_get_current();
2213
2214 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
2215}
2216
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00002217bool migrate_postcopy_ram(void)
2218{
2219 MigrationState *s;
2220
2221 s = migrate_get_current();
2222
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +00002223 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00002224}
2225
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002226bool migrate_postcopy(void)
2227{
Vladimir Sementsov-Ogievskiydd6bb912018-03-13 15:34:00 -04002228 return migrate_postcopy_ram() || migrate_dirty_bitmaps();
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002229}
2230
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06002231bool migrate_auto_converge(void)
2232{
2233 MigrationState *s;
2234
2235 s = migrate_get_current();
2236
2237 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
2238}
2239
Peter Lieven323004a2013-07-18 09:48:50 +02002240bool migrate_zero_blocks(void)
2241{
2242 MigrationState *s;
2243
2244 s = migrate_get_current();
2245
2246 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
2247}
2248
Alexey Perevalovf22f9282018-03-22 21:17:22 +03002249bool migrate_postcopy_blocktime(void)
2250{
2251 MigrationState *s;
2252
2253 s = migrate_get_current();
2254
2255 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME];
2256}
2257
Liang Li8706d2d2015-03-23 16:32:17 +08002258bool migrate_use_compression(void)
2259{
Liang Lidde4e692015-03-23 16:32:26 +08002260 MigrationState *s;
2261
2262 s = migrate_get_current();
2263
2264 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08002265}
2266
2267int migrate_compress_level(void)
2268{
2269 MigrationState *s;
2270
2271 s = migrate_get_current();
2272
Daniel P. Berrange2594f562016-04-27 11:05:14 +01002273 return s->parameters.compress_level;
Liang Li8706d2d2015-03-23 16:32:17 +08002274}
2275
2276int migrate_compress_threads(void)
2277{
2278 MigrationState *s;
2279
2280 s = migrate_get_current();
2281
Daniel P. Berrange2594f562016-04-27 11:05:14 +01002282 return s->parameters.compress_threads;
Liang Li8706d2d2015-03-23 16:32:17 +08002283}
2284
Xiao Guangrong1d588722018-08-21 16:10:20 +08002285int migrate_compress_wait_thread(void)
2286{
2287 MigrationState *s;
2288
2289 s = migrate_get_current();
2290
2291 return s->parameters.compress_wait_thread;
2292}
2293
Liang Li3fcb38c2015-03-23 16:32:18 +08002294int migrate_decompress_threads(void)
2295{
2296 MigrationState *s;
2297
2298 s = migrate_get_current();
2299
Daniel P. Berrange2594f562016-04-27 11:05:14 +01002300 return s->parameters.decompress_threads;
Liang Li3fcb38c2015-03-23 16:32:18 +08002301}
2302
Vladimir Sementsov-Ogievskiy55efc8c2018-03-13 15:34:00 -04002303bool migrate_dirty_bitmaps(void)
2304{
2305 MigrationState *s;
2306
2307 s = migrate_get_current();
2308
2309 return s->enabled_capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS];
2310}
2311
Yury Kotov18269062019-02-15 20:45:45 +03002312bool migrate_ignore_shared(void)
2313{
2314 MigrationState *s;
2315
2316 s = migrate_get_current();
2317
2318 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_IGNORE_SHARED];
2319}
2320
Yury Kotovb9d68df2019-09-03 19:22:44 +03002321bool migrate_validate_uuid(void)
2322{
2323 MigrationState *s;
2324
2325 s = migrate_get_current();
2326
2327 return s->enabled_capabilities[MIGRATION_CAPABILITY_VALIDATE_UUID];
2328}
2329
Juan Quintelab05dc722015-07-07 14:44:05 +02002330bool migrate_use_events(void)
2331{
2332 MigrationState *s;
2333
2334 s = migrate_get_current();
2335
2336 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
2337}
2338
Juan Quintela30126bb2016-01-14 12:23:00 +01002339bool migrate_use_multifd(void)
2340{
2341 MigrationState *s;
2342
2343 s = migrate_get_current();
2344
Juan Quintelacbfd6c92019-02-06 13:54:06 +01002345 return s->enabled_capabilities[MIGRATION_CAPABILITY_MULTIFD];
Juan Quintela30126bb2016-01-14 12:23:00 +01002346}
2347
Dr. David Alan Gilbert93fbd032017-10-20 10:05:50 +01002348bool migrate_pause_before_switchover(void)
2349{
2350 MigrationState *s;
2351
2352 s = migrate_get_current();
2353
2354 return s->enabled_capabilities[
2355 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER];
2356}
2357
Juan Quintela4075fb12016-01-15 08:56:17 +01002358int migrate_multifd_channels(void)
2359{
2360 MigrationState *s;
2361
2362 s = migrate_get_current();
2363
Juan Quintelacbfd6c92019-02-06 13:54:06 +01002364 return s->parameters.multifd_channels;
Juan Quintela4075fb12016-01-15 08:56:17 +01002365}
2366
Juan Quintelaab7cbb02019-05-15 13:37:46 +02002367MultiFDCompression migrate_multifd_compression(void)
2368{
2369 MigrationState *s;
2370
2371 s = migrate_get_current();
2372
2373 return s->parameters.multifd_compression;
2374}
2375
Juan Quintela9004db42020-01-23 17:08:52 +01002376int migrate_multifd_zlib_level(void)
2377{
2378 MigrationState *s;
2379
2380 s = migrate_get_current();
2381
2382 return s->parameters.multifd_zlib_level;
2383}
2384
Juan Quintela6a9ad152020-01-23 17:41:36 +01002385int migrate_multifd_zstd_level(void)
2386{
2387 MigrationState *s;
2388
2389 s = migrate_get_current();
2390
2391 return s->parameters.multifd_zstd_level;
2392}
2393
Orit Wasserman17ad9b32012-08-06 21:42:53 +03002394int migrate_use_xbzrle(void)
2395{
2396 MigrationState *s;
2397
2398 s = migrate_get_current();
2399
2400 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
2401}
2402
2403int64_t migrate_xbzrle_cache_size(void)
2404{
2405 MigrationState *s;
2406
2407 s = migrate_get_current();
2408
Juan Quintela73af8dd2017-10-05 21:30:10 +02002409 return s->parameters.xbzrle_cache_size;
Orit Wasserman17ad9b32012-08-06 21:42:53 +03002410}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002411
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01002412static int64_t migrate_max_postcopy_bandwidth(void)
2413{
2414 MigrationState *s;
2415
2416 s = migrate_get_current();
2417
2418 return s->parameters.max_postcopy_bandwidth;
2419}
2420
Juan Quintela2833c592017-04-05 18:32:37 +02002421bool migrate_use_block(void)
2422{
2423 MigrationState *s;
2424
2425 s = migrate_get_current();
2426
2427 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
2428}
2429
Peter Xuc788ada2017-06-26 18:28:55 +08002430bool migrate_use_return_path(void)
2431{
2432 MigrationState *s;
2433
2434 s = migrate_get_current();
2435
2436 return s->enabled_capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
2437}
2438
Juan Quintela2833c592017-04-05 18:32:37 +02002439bool migrate_use_block_incremental(void)
2440{
2441 MigrationState *s;
2442
2443 s = migrate_get_current();
2444
2445 return s->parameters.block_incremental;
2446}
2447
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002448/* migration thread support */
2449/*
2450 * Something bad happened to the RP stream, mark an error
2451 * The caller shall print or trace something to indicate why
2452 */
2453static void mark_source_rp_bad(MigrationState *s)
2454{
2455 s->rp_state.error = true;
2456}
2457
2458static struct rp_cmd_args {
2459 ssize_t len; /* -1 = variable */
2460 const char *name;
2461} rp_cmd_args[] = {
2462 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
2463 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
2464 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002465 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
2466 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Peter Xua335deb2018-05-02 18:47:28 +08002467 [MIG_RP_MSG_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" },
Peter Xu13955b82018-05-02 18:47:30 +08002468 [MIG_RP_MSG_RESUME_ACK] = { .len = 4, .name = "RESUME_ACK" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002469 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
2470};
2471
2472/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002473 * Process a request for pages received on the return path,
2474 * We're allowed to send more than requested (e.g. to round to our page size)
2475 * and we don't need to send pages that have already been sent.
2476 */
2477static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
2478 ram_addr_t start, size_t len)
2479{
Wei Yang038adc22019-10-13 10:11:45 +08002480 long our_host_ps = qemu_real_host_page_size;
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00002481
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002482 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00002483
2484 /*
2485 * Since we currently insist on matching page sizes, just sanity check
2486 * we're being asked for whole host pages.
2487 */
2488 if (start & (our_host_ps-1) ||
2489 (len & (our_host_ps-1))) {
2490 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
2491 " len: %zd", __func__, start, len);
2492 mark_source_rp_bad(ms);
2493 return;
2494 }
2495
Juan Quintela96506892017-03-14 18:41:03 +01002496 if (ram_save_queue_pages(rbname, start, len)) {
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00002497 mark_source_rp_bad(ms);
2498 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002499}
2500
Peter Xu14b17422018-05-02 18:47:21 +08002501/* Return true to retry, false to quit */
2502static bool postcopy_pause_return_path_thread(MigrationState *s)
2503{
2504 trace_postcopy_pause_return_path();
2505
2506 qemu_sem_wait(&s->postcopy_pause_rp_sem);
2507
2508 trace_postcopy_pause_return_path_continued();
2509
2510 return true;
2511}
2512
Peter Xua335deb2018-05-02 18:47:28 +08002513static int migrate_handle_rp_recv_bitmap(MigrationState *s, char *block_name)
2514{
2515 RAMBlock *block = qemu_ram_block_by_name(block_name);
2516
2517 if (!block) {
2518 error_report("%s: invalid block name '%s'", __func__, block_name);
2519 return -EINVAL;
2520 }
2521
2522 /* Fetch the received bitmap and refresh the dirty bitmap */
2523 return ram_dirty_bitmap_reload(s, block);
2524}
2525
Peter Xu13955b82018-05-02 18:47:30 +08002526static int migrate_handle_rp_resume_ack(MigrationState *s, uint32_t value)
2527{
2528 trace_source_return_path_thread_resume_ack(value);
2529
2530 if (value != MIGRATION_RESUME_ACK_VALUE) {
2531 error_report("%s: illegal resume_ack value %"PRIu32,
2532 __func__, value);
2533 return -1;
2534 }
2535
2536 /* Now both sides are active. */
2537 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
2538 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2539
Peter Xu94190692018-05-02 18:47:34 +08002540 /* Notify send thread that time to continue send pages */
2541 qemu_sem_post(&s->rp_state.rp_sem);
Peter Xu13955b82018-05-02 18:47:30 +08002542
2543 return 0;
2544}
2545
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002546/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002547 * Handles messages sent on the return path towards the source VM
2548 *
2549 */
2550static void *source_return_path_thread(void *opaque)
2551{
2552 MigrationState *ms = opaque;
2553 QEMUFile *rp = ms->rp_state.from_dst_file;
2554 uint16_t header_len, header_type;
Peter Xu568b01c2016-03-09 14:12:12 +08002555 uint8_t buf[512];
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002556 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002557 ram_addr_t start = 0; /* =0 to silence warning */
2558 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002559 int res;
2560
2561 trace_source_return_path_thread_entry();
Lidong Chen74637e62018-08-06 21:29:29 +08002562 rcu_register_thread();
Peter Xu14b17422018-05-02 18:47:21 +08002563
2564retry:
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002565 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
2566 migration_is_setup_or_active(ms->state)) {
2567 trace_source_return_path_thread_loop_top();
2568 header_type = qemu_get_be16(rp);
2569 header_len = qemu_get_be16(rp);
2570
Peter Xu7a9ddfb2018-02-08 18:31:05 +08002571 if (qemu_file_get_error(rp)) {
2572 mark_source_rp_bad(ms);
2573 goto out;
2574 }
2575
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002576 if (header_type >= MIG_RP_MSG_MAX ||
2577 header_type == MIG_RP_MSG_INVALID) {
2578 error_report("RP: Received invalid message 0x%04x length 0x%04x",
Mao Zhongyied8b2822020-03-28 01:19:08 +08002579 header_type, header_len);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002580 mark_source_rp_bad(ms);
2581 goto out;
2582 }
2583
2584 if ((rp_cmd_args[header_type].len != -1 &&
2585 header_len != rp_cmd_args[header_type].len) ||
Peter Xu568b01c2016-03-09 14:12:12 +08002586 header_len > sizeof(buf)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002587 error_report("RP: Received '%s' message (0x%04x) with"
Mao Zhongyied8b2822020-03-28 01:19:08 +08002588 "incorrect length %d expecting %zu",
2589 rp_cmd_args[header_type].name, header_type, header_len,
2590 (size_t)rp_cmd_args[header_type].len);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002591 mark_source_rp_bad(ms);
2592 goto out;
2593 }
2594
2595 /* We know we've got a valid header by this point */
2596 res = qemu_get_buffer(rp, buf, header_len);
2597 if (res != header_len) {
2598 error_report("RP: Failed reading data for message 0x%04x"
2599 " read %d expected %d",
2600 header_type, res, header_len);
2601 mark_source_rp_bad(ms);
2602 goto out;
2603 }
2604
2605 /* OK, we have the message and the data */
2606 switch (header_type) {
2607 case MIG_RP_MSG_SHUT:
Peter Maydell4d885132016-06-10 17:09:22 +01002608 sibling_error = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002609 trace_source_return_path_thread_shut(sibling_error);
2610 if (sibling_error) {
2611 error_report("RP: Sibling indicated error %d", sibling_error);
2612 mark_source_rp_bad(ms);
2613 }
2614 /*
2615 * We'll let the main thread deal with closing the RP
2616 * we could do a shutdown(2) on it, but we're the only user
2617 * anyway, so there's nothing gained.
2618 */
2619 goto out;
2620
2621 case MIG_RP_MSG_PONG:
Peter Maydell4d885132016-06-10 17:09:22 +01002622 tmp32 = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002623 trace_source_return_path_thread_pong(tmp32);
2624 break;
2625
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002626 case MIG_RP_MSG_REQ_PAGES:
Peter Maydell4d885132016-06-10 17:09:22 +01002627 start = ldq_be_p(buf);
2628 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002629 migrate_handle_rp_req_pages(ms, NULL, start, len);
2630 break;
2631
2632 case MIG_RP_MSG_REQ_PAGES_ID:
2633 expected_len = 12 + 1; /* header + termination */
2634
2635 if (header_len >= expected_len) {
Peter Maydell4d885132016-06-10 17:09:22 +01002636 start = ldq_be_p(buf);
2637 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002638 /* Now we expect an idstr */
2639 tmp32 = buf[12]; /* Length of the following idstr */
2640 buf[13 + tmp32] = '\0';
2641 expected_len += tmp32;
2642 }
2643 if (header_len != expected_len) {
2644 error_report("RP: Req_Page_id with length %d expecting %zd",
Mao Zhongyied8b2822020-03-28 01:19:08 +08002645 header_len, expected_len);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002646 mark_source_rp_bad(ms);
2647 goto out;
2648 }
2649 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
2650 break;
2651
Peter Xua335deb2018-05-02 18:47:28 +08002652 case MIG_RP_MSG_RECV_BITMAP:
2653 if (header_len < 1) {
2654 error_report("%s: missing block name", __func__);
2655 mark_source_rp_bad(ms);
2656 goto out;
2657 }
2658 /* Format: len (1B) + idstr (<255B). This ends the idstr. */
2659 buf[buf[0] + 1] = '\0';
2660 if (migrate_handle_rp_recv_bitmap(ms, (char *)(buf + 1))) {
2661 mark_source_rp_bad(ms);
2662 goto out;
2663 }
2664 break;
2665
Peter Xu13955b82018-05-02 18:47:30 +08002666 case MIG_RP_MSG_RESUME_ACK:
2667 tmp32 = ldl_be_p(buf);
2668 if (migrate_handle_rp_resume_ack(ms, tmp32)) {
2669 mark_source_rp_bad(ms);
2670 goto out;
2671 }
2672 break;
2673
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002674 default:
2675 break;
2676 }
2677 }
Peter Xu14b17422018-05-02 18:47:21 +08002678
2679out:
2680 res = qemu_file_get_error(rp);
2681 if (res) {
Dr. David Alan Gilbertfb14a422019-10-07 11:35:07 +01002682 if (res == -EIO && migration_in_postcopy()) {
Peter Xu14b17422018-05-02 18:47:21 +08002683 /*
2684 * Maybe there is something we can do: it looks like a
2685 * network down issue, and we pause for a recovery.
2686 */
2687 if (postcopy_pause_return_path_thread(ms)) {
2688 /* Reload rp, reset the rest */
Marc-André Lureau0284a2a2018-09-25 13:22:45 +04002689 if (rp != ms->rp_state.from_dst_file) {
2690 qemu_fclose(rp);
2691 rp = ms->rp_state.from_dst_file;
2692 }
Peter Xu14b17422018-05-02 18:47:21 +08002693 ms->rp_state.error = false;
2694 goto retry;
2695 }
2696 }
2697
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002698 trace_source_return_path_thread_bad_end();
2699 mark_source_rp_bad(ms);
2700 }
2701
2702 trace_source_return_path_thread_end();
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002703 ms->rp_state.from_dst_file = NULL;
2704 qemu_fclose(rp);
Lidong Chen74637e62018-08-06 21:29:29 +08002705 rcu_unregister_thread();
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002706 return NULL;
2707}
2708
Peter Xud3e35b82018-05-02 18:47:24 +08002709static int open_return_path_on_source(MigrationState *ms,
2710 bool create_thread)
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002711{
2712
zhanghailiang89a02a92016-01-15 11:37:42 +08002713 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002714 if (!ms->rp_state.from_dst_file) {
2715 return -1;
2716 }
2717
2718 trace_open_return_path_on_source();
Peter Xud3e35b82018-05-02 18:47:24 +08002719
2720 if (!create_thread) {
2721 /* We're done */
2722 return 0;
2723 }
2724
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002725 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
2726 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
2727
2728 trace_open_return_path_on_source_continue();
2729
2730 return 0;
2731}
2732
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002733/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
2734static int await_return_path_close_on_source(MigrationState *ms)
2735{
2736 /*
2737 * If this is a normal exit then the destination will send a SHUT and the
2738 * rp_thread will exit, however if there's an error we need to cause
2739 * it to exit.
2740 */
zhanghailiang89a02a92016-01-15 11:37:42 +08002741 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002742 /*
2743 * shutdown(2), if we have it, will cause it to unblock if it's stuck
2744 * waiting for the destination.
2745 */
2746 qemu_file_shutdown(ms->rp_state.from_dst_file);
2747 mark_source_rp_bad(ms);
2748 }
2749 trace_await_return_path_close_on_source_joining();
2750 qemu_thread_join(&ms->rp_state.rp_thread);
2751 trace_await_return_path_close_on_source_close();
2752 return ms->rp_state.error;
2753}
2754
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002755/*
2756 * Switch from normal iteration to postcopy
2757 * Returns non-0 on error
2758 */
Peter Xu7287cbd2018-01-03 20:20:09 +08002759static int postcopy_start(MigrationState *ms)
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002760{
2761 int ret;
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002762 QIOChannelBuffer *bioc;
2763 QEMUFile *fb;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002764 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01002765 int64_t bandwidth = migrate_max_postcopy_bandwidth();
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002766 bool restart_block = false;
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002767 int cur_state = MIGRATION_STATUS_ACTIVE;
2768 if (!migrate_pause_before_switchover()) {
2769 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
2770 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2771 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002772
2773 trace_postcopy_start();
2774 qemu_mutex_lock_iothread();
2775 trace_postcopy_start_set_run();
2776
Daniel Henrique Barbozafb064112018-12-05 17:47:01 -02002777 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002778 global_state_store();
2779 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01002780 if (ret < 0) {
2781 goto fail;
2782 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002783
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002784 ret = migration_maybe_pause(ms, &cur_state,
2785 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2786 if (ret < 0) {
2787 goto fail;
2788 }
2789
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01002790 ret = bdrv_inactivate_all();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002791 if (ret < 0) {
2792 goto fail;
2793 }
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002794 restart_block = true;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002795
2796 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00002797 * Cause any non-postcopiable, but iterative devices to
2798 * send out their final data.
2799 */
Fam Zhenga1fbe752017-06-17 00:06:58 +08002800 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00002801
2802 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002803 * in Finish migrate and with the io-lock held everything should
2804 * be quiet, but we've potentially still got dirty pages and we
2805 * need to tell the destination to throw any pages it's already received
2806 * that are dirty
2807 */
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002808 if (migrate_postcopy_ram()) {
2809 if (ram_postcopy_send_discard_bitmap(ms)) {
2810 error_report("postcopy send discard bitmap failed");
2811 goto fail;
2812 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002813 }
2814
2815 /*
2816 * send rest of state - note things that are doing postcopy
2817 * will notice we're in POSTCOPY_ACTIVE and not actually
2818 * wrap their state up here
2819 */
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01002820 /* 0 max-postcopy-bandwidth means unlimited */
2821 if (!bandwidth) {
2822 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
2823 } else {
2824 qemu_file_set_rate_limit(ms->to_dst_file, bandwidth / XFER_LIMIT_RATIO);
2825 }
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002826 if (migrate_postcopy_ram()) {
2827 /* Ping just for debugging, helps line traces up */
2828 qemu_savevm_send_ping(ms->to_dst_file, 2);
2829 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002830
2831 /*
2832 * While loading the device state we may trigger page transfer
2833 * requests and the fd must be free to process those, and thus
2834 * the destination must read the whole device state off the fd before
2835 * it starts processing it. Unfortunately the ad-hoc migration format
2836 * doesn't allow the destination to know the size to read without fully
2837 * parsing it through each devices load-state code (especially the open
2838 * coded devices that use get/put).
2839 * So we wrap the device state up in a package with a length at the start;
2840 * to do this we use a qemu_buf to hold the whole of the device state.
2841 */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002842 bioc = qio_channel_buffer_new(4096);
Daniel P. Berrange6f01f132016-09-30 11:57:14 +01002843 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002844 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
2845 object_unref(OBJECT(bioc));
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002846
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00002847 /*
2848 * Make sure the receiver can get incoming pages before we send the rest
2849 * of the state
2850 */
2851 qemu_savevm_send_postcopy_listen(fb);
2852
Fam Zhenga1fbe752017-06-17 00:06:58 +08002853 qemu_savevm_state_complete_precopy(fb, false, false);
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002854 if (migrate_postcopy_ram()) {
2855 qemu_savevm_send_ping(fb, 3);
2856 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002857
2858 qemu_savevm_send_postcopy_run(fb);
2859
2860 /* <><> end of stuff going into the package */
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002861
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002862 /* Last point of recovery; as soon as we send the package the destination
2863 * can open devices and potentially start running.
2864 * Lets just check again we've not got any errors.
2865 */
2866 ret = qemu_file_get_error(ms->to_dst_file);
2867 if (ret) {
2868 error_report("postcopy_start: Migration stream errored (pre package)");
2869 goto fail_closefb;
2870 }
2871
2872 restart_block = false;
2873
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002874 /* Now send that blob */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002875 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002876 goto fail_closefb;
2877 }
2878 qemu_fclose(fb);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00002879
2880 /* Send a notify to give a chance for anything that needs to happen
2881 * at the transition to postcopy and after the device state; in particular
2882 * spice needs to trigger a transition now
2883 */
2884 ms->postcopy_after_devices = true;
2885 notifier_list_notify(&migration_state_notifiers, ms);
2886
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002887 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
2888
2889 qemu_mutex_unlock_iothread();
2890
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002891 if (migrate_postcopy_ram()) {
2892 /*
2893 * Although this ping is just for debug, it could potentially be
2894 * used for getting a better measurement of downtime at the source.
2895 */
2896 qemu_savevm_send_ping(ms->to_dst_file, 4);
2897 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002898
Pavel Butsykinced1c612017-02-03 18:23:21 +03002899 if (migrate_release_ram()) {
2900 ram_postcopy_migrated_memory_release(ms);
2901 }
2902
zhanghailiang89a02a92016-01-15 11:37:42 +08002903 ret = qemu_file_get_error(ms->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002904 if (ret) {
2905 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00002906 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002907 MIGRATION_STATUS_FAILED);
2908 }
2909
2910 return ret;
2911
2912fail_closefb:
2913 qemu_fclose(fb);
2914fail:
zhanghailiang48781e52015-12-16 11:47:33 +00002915 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002916 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002917 if (restart_block) {
2918 /* A failure happened early enough that we know the destination hasn't
2919 * accessed block devices, so we're safe to recover.
2920 */
2921 Error *local_err = NULL;
2922
2923 bdrv_invalidate_cache_all(&local_err);
2924 if (local_err) {
2925 error_report_err(local_err);
2926 }
2927 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002928 qemu_mutex_unlock_iothread();
2929 return -1;
2930}
2931
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002932/**
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002933 * migration_maybe_pause: Pause if required to by
2934 * migrate_pause_before_switchover called with the iothread locked
2935 * Returns: 0 on success
2936 */
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002937static int migration_maybe_pause(MigrationState *s,
2938 int *current_active_state,
2939 int new_state)
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002940{
2941 if (!migrate_pause_before_switchover()) {
2942 return 0;
2943 }
2944
2945 /* Since leaving this state is not atomic with posting the semaphore
2946 * it's possible that someone could have issued multiple migrate_continue
2947 * and the semaphore is incorrectly positive at this point;
2948 * the docs say it's undefined to reinit a semaphore that's already
2949 * init'd, so use timedwait to eat up any existing posts.
2950 */
2951 while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) {
2952 /* This block intentionally left blank */
2953 }
2954
Zhimin Feng89583382020-01-14 17:43:09 +08002955 /*
2956 * If the migration is cancelled when it is in the completion phase,
2957 * the migration state is set to MIGRATION_STATUS_CANCELLING.
2958 * So we don't need to wait a semaphore, otherwise we would always
2959 * wait for the 'pause_sem' semaphore.
2960 */
2961 if (s->state != MIGRATION_STATUS_CANCELLING) {
2962 qemu_mutex_unlock_iothread();
2963 migrate_set_state(&s->state, *current_active_state,
2964 MIGRATION_STATUS_PRE_SWITCHOVER);
2965 qemu_sem_wait(&s->pause_sem);
2966 migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER,
2967 new_state);
2968 *current_active_state = new_state;
2969 qemu_mutex_lock_iothread();
2970 }
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002971
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002972 return s->state == new_state ? 0 : -EINVAL;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002973}
2974
2975/**
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002976 * migration_completion: Used by migration_thread when there's not much left.
2977 * The caller 'breaks' the loop when this returns.
2978 *
2979 * @s: Current migration state
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002980 */
Peter Xu2ad87302018-01-03 20:20:14 +08002981static void migration_completion(MigrationState *s)
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002982{
2983 int ret;
Peter Xu2ad87302018-01-03 20:20:14 +08002984 int current_active_state = s->state;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002985
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002986 if (s->state == MIGRATION_STATUS_ACTIVE) {
2987 qemu_mutex_lock_iothread();
Peter Xu64909f92018-01-03 20:20:10 +08002988 s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Daniel Henrique Barbozafb064112018-12-05 17:47:01 -02002989 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
Peter Xu7287cbd2018-01-03 20:20:09 +08002990 s->vm_was_running = runstate_is_running();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002991 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002992
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002993 if (!ret) {
Fam Zhenga1fbe752017-06-17 00:06:58 +08002994 bool inactivate = !migrate_colo_enabled();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002995 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02002996 if (ret >= 0) {
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002997 ret = migration_maybe_pause(s, &current_active_state,
2998 MIGRATION_STATUS_DEVICE);
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002999 }
3000 if (ret >= 0) {
Kevin Wolff07fa4c2017-05-22 17:10:38 +02003001 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
Fam Zhenga1fbe752017-06-17 00:06:58 +08003002 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
3003 inactivate);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02003004 }
Fam Zhenga1fbe752017-06-17 00:06:58 +08003005 if (inactivate && ret >= 0) {
3006 s->block_inactive = true;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003007 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003008 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003009 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003010
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003011 if (ret < 0) {
3012 goto fail;
3013 }
3014 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
3015 trace_migration_completion_postcopy_end();
3016
zhanghailiang89a02a92016-01-15 11:37:42 +08003017 qemu_savevm_state_complete_postcopy(s->to_dst_file);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003018 trace_migration_completion_postcopy_end_after_complete();
3019 }
3020
3021 /*
3022 * If rp was opened we must clean up the thread before
3023 * cleaning everything else up (since if there are no failures
3024 * it will wait for the destination to send it's status in
3025 * a SHUT command).
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003026 */
Peter Xu0425dc92017-05-31 18:35:34 +08003027 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003028 int rp_error;
Peter Xu0425dc92017-05-31 18:35:34 +08003029 trace_migration_return_path_end_before();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003030 rp_error = await_return_path_close_on_source(s);
Peter Xu0425dc92017-05-31 18:35:34 +08003031 trace_migration_return_path_end_after(rp_error);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003032 if (rp_error) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02003033 goto fail_invalidate;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00003034 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003035 }
3036
zhanghailiang89a02a92016-01-15 11:37:42 +08003037 if (qemu_file_get_error(s->to_dst_file)) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003038 trace_migration_completion_file_err();
Greg Kurzfe904ea2016-05-18 15:44:36 +02003039 goto fail_invalidate;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003040 }
3041
zhanghailiang0b827d52016-10-27 14:42:54 +08003042 if (!migrate_colo_enabled()) {
3043 migrate_set_state(&s->state, current_active_state,
3044 MIGRATION_STATUS_COMPLETED);
3045 }
3046
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003047 return;
3048
Greg Kurzfe904ea2016-05-18 15:44:36 +02003049fail_invalidate:
3050 /* If not doing postcopy, vm_start() will be called: let's regain
3051 * control on images.
3052 */
Dr. David Alan Gilbert6039dd52018-02-05 09:13:37 +00003053 if (s->state == MIGRATION_STATUS_ACTIVE ||
3054 s->state == MIGRATION_STATUS_DEVICE) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02003055 Error *local_err = NULL;
3056
zhanghailiang1d2acc32017-01-24 15:59:52 +08003057 qemu_mutex_lock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02003058 bdrv_invalidate_cache_all(&local_err);
3059 if (local_err) {
3060 error_report_err(local_err);
zhanghailiang1d2acc32017-01-24 15:59:52 +08003061 } else {
3062 s->block_inactive = false;
Greg Kurzfe904ea2016-05-18 15:44:36 +02003063 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08003064 qemu_mutex_unlock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02003065 }
3066
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003067fail:
zhanghailiang48781e52015-12-16 11:47:33 +00003068 migrate_set_state(&s->state, current_active_state,
3069 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003070}
3071
zhanghailiang35a6ed42016-10-27 14:42:52 +08003072bool migrate_colo_enabled(void)
3073{
3074 MigrationState *s = migrate_get_current();
3075 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
3076}
3077
Peter Xub23c2ad2018-05-02 18:47:19 +08003078typedef enum MigThrError {
3079 /* No error detected */
3080 MIG_THR_ERR_NONE = 0,
3081 /* Detected error, but resumed successfully */
3082 MIG_THR_ERR_RECOVERED = 1,
3083 /* Detected fatal error, need to exit */
3084 MIG_THR_ERR_FATAL = 2,
3085} MigThrError;
3086
Peter Xu94190692018-05-02 18:47:34 +08003087static int postcopy_resume_handshake(MigrationState *s)
3088{
3089 qemu_savevm_send_postcopy_resume(s->to_dst_file);
3090
3091 while (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
3092 qemu_sem_wait(&s->rp_state.rp_sem);
3093 }
3094
3095 if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
3096 return 0;
3097 }
3098
3099 return -1;
3100}
3101
Peter Xu135b87b2018-05-02 18:47:25 +08003102/* Return zero if success, or <0 for error */
3103static int postcopy_do_resume(MigrationState *s)
3104{
Peter Xud1b8ead2018-05-02 18:47:31 +08003105 int ret;
3106
3107 /*
3108 * Call all the resume_prepare() hooks, so that modules can be
3109 * ready for the migration resume.
3110 */
3111 ret = qemu_savevm_state_resume_prepare(s);
3112 if (ret) {
3113 error_report("%s: resume_prepare() failure detected: %d",
3114 __func__, ret);
3115 return ret;
3116 }
3117
3118 /*
Peter Xu94190692018-05-02 18:47:34 +08003119 * Last handshake with destination on the resume (destination will
3120 * switch to postcopy-active afterwards)
Peter Xud1b8ead2018-05-02 18:47:31 +08003121 */
Peter Xu94190692018-05-02 18:47:34 +08003122 ret = postcopy_resume_handshake(s);
3123 if (ret) {
3124 error_report("%s: handshake failed: %d", __func__, ret);
3125 return ret;
3126 }
Peter Xud1b8ead2018-05-02 18:47:31 +08003127
Peter Xu135b87b2018-05-02 18:47:25 +08003128 return 0;
3129}
3130
Peter Xub23c2ad2018-05-02 18:47:19 +08003131/*
3132 * We don't return until we are in a safe state to continue current
3133 * postcopy migration. Returns MIG_THR_ERR_RECOVERED if recovered, or
3134 * MIG_THR_ERR_FATAL if unrecovery failure happened.
3135 */
3136static MigThrError postcopy_pause(MigrationState *s)
3137{
3138 assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
Peter Xub23c2ad2018-05-02 18:47:19 +08003139
Peter Xu135b87b2018-05-02 18:47:25 +08003140 while (true) {
Peter Xu62df0662018-05-02 18:47:38 +08003141 QEMUFile *file;
3142
Peter Xu135b87b2018-05-02 18:47:25 +08003143 migrate_set_state(&s->state, s->state,
3144 MIGRATION_STATUS_POSTCOPY_PAUSED);
Peter Xub23c2ad2018-05-02 18:47:19 +08003145
Peter Xu135b87b2018-05-02 18:47:25 +08003146 /* Current channel is possibly broken. Release it. */
3147 assert(s->to_dst_file);
Peter Xu62df0662018-05-02 18:47:38 +08003148 qemu_mutex_lock(&s->qemu_file_lock);
3149 file = s->to_dst_file;
Peter Xu135b87b2018-05-02 18:47:25 +08003150 s->to_dst_file = NULL;
Peter Xu62df0662018-05-02 18:47:38 +08003151 qemu_mutex_unlock(&s->qemu_file_lock);
3152
3153 qemu_file_shutdown(file);
3154 qemu_fclose(file);
Peter Xub23c2ad2018-05-02 18:47:19 +08003155
Peter Xu135b87b2018-05-02 18:47:25 +08003156 error_report("Detected IO failure for postcopy. "
3157 "Migration paused.");
3158
3159 /*
3160 * We wait until things fixed up. Then someone will setup the
3161 * status back for us.
3162 */
3163 while (s->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
3164 qemu_sem_wait(&s->postcopy_pause_sem);
3165 }
3166
3167 if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
3168 /* Woken up by a recover procedure. Give it a shot */
3169
3170 /*
3171 * Firstly, let's wake up the return path now, with a new
3172 * return path channel.
3173 */
3174 qemu_sem_post(&s->postcopy_pause_rp_sem);
3175
3176 /* Do the resume logic */
3177 if (postcopy_do_resume(s) == 0) {
3178 /* Let's continue! */
3179 trace_postcopy_pause_continued();
3180 return MIG_THR_ERR_RECOVERED;
3181 } else {
3182 /*
3183 * Something wrong happened during the recovery, let's
3184 * pause again. Pause is always better than throwing
3185 * data away.
3186 */
3187 continue;
3188 }
3189 } else {
3190 /* This is not right... Time to quit. */
3191 return MIG_THR_ERR_FATAL;
3192 }
Peter Xub23c2ad2018-05-02 18:47:19 +08003193 }
Peter Xub23c2ad2018-05-02 18:47:19 +08003194}
3195
3196static MigThrError migration_detect_error(MigrationState *s)
3197{
3198 int ret;
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003199 int state = s->state;
Yury Kotov3d661c82019-04-22 13:34:20 +03003200 Error *local_error = NULL;
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003201
3202 if (state == MIGRATION_STATUS_CANCELLING ||
3203 state == MIGRATION_STATUS_CANCELLED) {
3204 /* End the migration, but don't set the state to failed */
3205 return MIG_THR_ERR_FATAL;
3206 }
Peter Xub23c2ad2018-05-02 18:47:19 +08003207
3208 /* Try to detect any file errors */
Yury Kotov3d661c82019-04-22 13:34:20 +03003209 ret = qemu_file_get_error_obj(s->to_dst_file, &local_error);
Peter Xub23c2ad2018-05-02 18:47:19 +08003210 if (!ret) {
3211 /* Everything is fine */
Yury Kotov3d661c82019-04-22 13:34:20 +03003212 assert(!local_error);
Peter Xub23c2ad2018-05-02 18:47:19 +08003213 return MIG_THR_ERR_NONE;
3214 }
3215
Yury Kotov3d661c82019-04-22 13:34:20 +03003216 if (local_error) {
3217 migrate_set_error(s, local_error);
3218 error_free(local_error);
3219 }
3220
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003221 if (state == MIGRATION_STATUS_POSTCOPY_ACTIVE && ret == -EIO) {
Peter Xub23c2ad2018-05-02 18:47:19 +08003222 /*
3223 * For postcopy, we allow the network to be down for a
3224 * while. After that, it can be continued by a
3225 * recovery phase.
3226 */
3227 return postcopy_pause(s);
3228 } else {
3229 /*
3230 * For precopy (or postcopy with error outside IO), we fail
3231 * with no time.
3232 */
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003233 migrate_set_state(&s->state, state, MIGRATION_STATUS_FAILED);
Peter Xub23c2ad2018-05-02 18:47:19 +08003234 trace_migration_thread_file_err();
3235
3236 /* Time to stop the migration, now. */
3237 return MIG_THR_ERR_FATAL;
3238 }
3239}
3240
Wei Yang268dcd42019-09-12 10:49:57 +08003241/* How many bytes have we transferred since the beginning of the migration */
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003242static uint64_t migration_total_bytes(MigrationState *s)
3243{
Juan Quintelab9ee2f72016-01-15 11:40:13 +01003244 return qemu_ftell(s->to_dst_file) + ram_counters.multifd_bytes;
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003245}
3246
Peter Xucf011f02018-01-03 20:20:11 +08003247static void migration_calculate_complete(MigrationState *s)
3248{
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003249 uint64_t bytes = migration_total_bytes(s);
Peter Xucf011f02018-01-03 20:20:11 +08003250 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela6cde6fb2018-06-26 15:26:35 +02003251 int64_t transfer_time;
Peter Xucf011f02018-01-03 20:20:11 +08003252
3253 s->total_time = end_time - s->start_time;
3254 if (!s->downtime) {
3255 /*
3256 * It's still not set, so we are precopy migration. For
3257 * postcopy, downtime is calculated during postcopy_start().
3258 */
3259 s->downtime = end_time - s->downtime_start;
3260 }
3261
Juan Quintela6cde6fb2018-06-26 15:26:35 +02003262 transfer_time = s->total_time - s->setup_time;
3263 if (transfer_time) {
3264 s->mbps = ((double) bytes * 8.0) / transfer_time / 1000;
Peter Xucf011f02018-01-03 20:20:11 +08003265 }
3266}
3267
Ivan Ren87f3bd82019-08-02 18:18:41 +08003268static void update_iteration_initial_status(MigrationState *s)
3269{
3270 /*
3271 * Update these three fields at the same time to avoid mismatch info lead
3272 * wrong speed calculation.
3273 */
3274 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3275 s->iteration_initial_bytes = migration_total_bytes(s);
3276 s->iteration_initial_pages = ram_get_total_transferred_pages();
3277}
3278
Peter Xub15df1a2018-01-03 20:20:13 +08003279static void migration_update_counters(MigrationState *s,
3280 int64_t current_time)
3281{
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08003282 uint64_t transferred, transferred_pages, time_spent;
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003283 uint64_t current_bytes; /* bytes transferred since the beginning */
Peter Xub15df1a2018-01-03 20:20:13 +08003284 double bandwidth;
3285
3286 if (current_time < s->iteration_start_time + BUFFER_DELAY) {
3287 return;
3288 }
3289
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003290 current_bytes = migration_total_bytes(s);
3291 transferred = current_bytes - s->iteration_initial_bytes;
Peter Xub15df1a2018-01-03 20:20:13 +08003292 time_spent = current_time - s->iteration_start_time;
3293 bandwidth = (double)transferred / time_spent;
Wei Wang0781c1e2018-01-22 19:36:39 +08003294 s->threshold_size = bandwidth * s->parameters.downtime_limit;
Peter Xub15df1a2018-01-03 20:20:13 +08003295
3296 s->mbps = (((double) transferred * 8.0) /
3297 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
3298
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08003299 transferred_pages = ram_get_total_transferred_pages() -
3300 s->iteration_initial_pages;
3301 s->pages_per_second = (double) transferred_pages /
3302 (((double) time_spent / 1000.0));
3303
Peter Xub15df1a2018-01-03 20:20:13 +08003304 /*
3305 * if we haven't sent anything, we don't want to
3306 * recalculate. 10000 is a small enough number for our purposes
3307 */
3308 if (ram_counters.dirty_pages_rate && transferred > 10000) {
Balamuruhan S650af892018-06-12 14:20:09 +05303309 s->expected_downtime = ram_counters.remaining / bandwidth;
Peter Xub15df1a2018-01-03 20:20:13 +08003310 }
3311
3312 qemu_file_reset_rate_limit(s->to_dst_file);
3313
Ivan Ren87f3bd82019-08-02 18:18:41 +08003314 update_iteration_initial_status(s);
Peter Xub15df1a2018-01-03 20:20:13 +08003315
3316 trace_migrate_transferred(transferred, time_spent,
Wei Wang0781c1e2018-01-22 19:36:39 +08003317 bandwidth, s->threshold_size);
Peter Xub15df1a2018-01-03 20:20:13 +08003318}
3319
Peter Xu2ad87302018-01-03 20:20:14 +08003320/* Migration thread iteration status */
3321typedef enum {
3322 MIG_ITERATE_RESUME, /* Resume current iteration */
3323 MIG_ITERATE_SKIP, /* Skip current iteration */
3324 MIG_ITERATE_BREAK, /* Break the loop */
3325} MigIterateState;
3326
3327/*
3328 * Return true if continue to the next iteration directly, false
3329 * otherwise.
3330 */
3331static MigIterateState migration_iteration_run(MigrationState *s)
3332{
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04003333 uint64_t pending_size, pend_pre, pend_compat, pend_post;
Peter Xu2ad87302018-01-03 20:20:14 +08003334 bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE;
3335
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04003336 qemu_savevm_state_pending(s->to_dst_file, s->threshold_size, &pend_pre,
3337 &pend_compat, &pend_post);
3338 pending_size = pend_pre + pend_compat + pend_post;
Peter Xu2ad87302018-01-03 20:20:14 +08003339
3340 trace_migrate_pending(pending_size, s->threshold_size,
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04003341 pend_pre, pend_compat, pend_post);
Peter Xu2ad87302018-01-03 20:20:14 +08003342
3343 if (pending_size && pending_size >= s->threshold_size) {
3344 /* Still a significant amount to transfer */
Wei Yang52aec702019-07-18 16:37:47 +08003345 if (!in_postcopy && pend_pre <= s->threshold_size &&
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01003346 qatomic_read(&s->start_postcopy)) {
Peter Xu2ad87302018-01-03 20:20:14 +08003347 if (postcopy_start(s)) {
3348 error_report("%s: postcopy failed to start", __func__);
3349 }
3350 return MIG_ITERATE_SKIP;
3351 }
3352 /* Just another iteration step */
Wei Yang17d93512019-10-06 06:05:16 +08003353 qemu_savevm_state_iterate(s->to_dst_file, in_postcopy);
Peter Xu2ad87302018-01-03 20:20:14 +08003354 } else {
3355 trace_migration_thread_low_pending(pending_size);
3356 migration_completion(s);
3357 return MIG_ITERATE_BREAK;
3358 }
3359
3360 return MIG_ITERATE_RESUME;
3361}
3362
Peter Xu199aa6d2018-01-03 20:20:15 +08003363static void migration_iteration_finish(MigrationState *s)
3364{
3365 /* If we enabled cpu throttling for auto-converge, turn it off. */
3366 cpu_throttle_stop();
3367
3368 qemu_mutex_lock_iothread();
3369 switch (s->state) {
3370 case MIGRATION_STATUS_COMPLETED:
3371 migration_calculate_complete(s);
3372 runstate_set(RUN_STATE_POSTMIGRATE);
3373 break;
3374
3375 case MIGRATION_STATUS_ACTIVE:
3376 /*
3377 * We should really assert here, but since it's during
3378 * migration, let's try to reduce the usage of assertions.
3379 */
3380 if (!migrate_colo_enabled()) {
3381 error_report("%s: critical error: calling COLO code without "
3382 "COLO enabled", __func__);
3383 }
3384 migrate_start_colo_process(s);
3385 /*
3386 * Fixme: we will run VM in COLO no matter its old running state.
3387 * After exited COLO, we will keep running.
3388 */
3389 s->vm_was_running = true;
3390 /* Fallthrough */
3391 case MIGRATION_STATUS_FAILED:
3392 case MIGRATION_STATUS_CANCELLED:
Dr. David Alan Gilbert57225e52018-07-19 10:22:57 +01003393 case MIGRATION_STATUS_CANCELLING:
Peter Xu199aa6d2018-01-03 20:20:15 +08003394 if (s->vm_was_running) {
3395 vm_start();
3396 } else {
3397 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
3398 runstate_set(RUN_STATE_POSTMIGRATE);
3399 }
3400 }
3401 break;
3402
3403 default:
3404 /* Should not reach here, but if so, forgive the VM. */
3405 error_report("%s: Unknown ending state %d", __func__, s->state);
3406 break;
3407 }
Yury Kotovfd392cf2019-04-08 14:33:43 +03003408 migrate_fd_cleanup_schedule(s);
Peter Xu199aa6d2018-01-03 20:20:15 +08003409 qemu_mutex_unlock_iothread();
3410}
3411
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003412void migration_make_urgent_request(void)
3413{
3414 qemu_sem_post(&migrate_get_current()->rate_limit_sem);
3415}
3416
3417void migration_consume_urgent_request(void)
3418{
3419 qemu_sem_wait(&migrate_get_current()->rate_limit_sem);
3420}
3421
Dr. David Alan Gilbert97e1e062019-12-05 10:29:18 +00003422/* Returns true if the rate limiting was broken by an urgent request */
3423bool migration_rate_limit(void)
3424{
3425 int64_t now = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3426 MigrationState *s = migrate_get_current();
3427
3428 bool urgent = false;
3429 migration_update_counters(s, now);
3430 if (qemu_file_rate_limit(s->to_dst_file)) {
Lukas Straub77386122020-05-20 22:42:32 +02003431
3432 if (qemu_file_get_error(s->to_dst_file)) {
3433 return false;
3434 }
Dr. David Alan Gilbert97e1e062019-12-05 10:29:18 +00003435 /*
3436 * Wait for a delay to do rate limiting OR
3437 * something urgent to post the semaphore.
3438 */
3439 int ms = s->iteration_start_time + BUFFER_DELAY - now;
3440 trace_migration_rate_limit_pre(ms);
3441 if (qemu_sem_timedwait(&s->rate_limit_sem, ms) == 0) {
3442 /*
3443 * We were woken by one or more urgent things but
3444 * the timedwait will have consumed one of them.
3445 * The service routine for the urgent wake will dec
3446 * the semaphore itself for each item it consumes,
3447 * so add this one we just eat back.
3448 */
3449 qemu_sem_post(&s->rate_limit_sem);
3450 urgent = true;
3451 }
3452 trace_migration_rate_limit_post(urgent);
3453 }
3454 return urgent;
3455}
3456
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00003457/*
3458 * Master migration thread on the source VM.
3459 * It drives the migration and pumps the data down the outgoing channel.
3460 */
Juan Quintela5f496a12013-02-22 17:36:30 +01003461static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003462{
Juan Quintela9848a402012-12-19 09:55:50 +01003463 MigrationState *s = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +01003464 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Peter Xub23c2ad2018-05-02 18:47:19 +08003465 MigThrError thr_error;
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003466 bool urgent = false;
Juan Quintela76f59332012-10-03 20:16:24 +02003467
Paolo Bonziniab28bd22015-07-09 08:55:38 +02003468 rcu_register_thread();
3469
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +00003470 object_ref(OBJECT(s));
Ivan Ren87f3bd82019-08-02 18:18:41 +08003471 update_iteration_initial_status(s);
Peter Xub15df1a2018-01-03 20:20:13 +08003472
zhanghailiang89a02a92016-01-15 11:37:42 +08003473 qemu_savevm_state_header(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003474
Peter Xu62a02652017-06-14 15:55:58 +08003475 /*
3476 * If we opened the return path, we need to make sure dst has it
3477 * opened as well.
3478 */
3479 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003480 /* Now tell the dest that it should open its end so it can reply */
zhanghailiang89a02a92016-01-15 11:37:42 +08003481 qemu_savevm_send_open_return_path(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003482
3483 /* And do a ping that will make stuff easier to debug */
zhanghailiang89a02a92016-01-15 11:37:42 +08003484 qemu_savevm_send_ping(s->to_dst_file, 1);
Peter Xu0425dc92017-05-31 18:35:34 +08003485 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003486
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03003487 if (migrate_postcopy()) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003488 /*
3489 * Tell the destination that we *might* want to do postcopy later;
3490 * if the other end can't do postcopy it should fail now, nice and
3491 * early.
3492 */
zhanghailiang89a02a92016-01-15 11:37:42 +08003493 qemu_savevm_send_postcopy_advise(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003494 }
3495
Zhang Chenaad555c2018-09-03 12:38:47 +08003496 if (migrate_colo_enabled()) {
3497 /* Notify migration destination that we enable COLO */
3498 qemu_savevm_send_colo_enable(s->to_dst_file);
3499 }
3500
Juan Quintela9907e842017-06-28 11:52:24 +02003501 qemu_savevm_state_setup(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003502
Keqian Zhud05de9e2020-02-04 13:08:41 +08003503 if (qemu_savevm_state_guest_unplug_pending()) {
Jens Freimannc7e0acd2019-10-29 12:49:02 +01003504 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3505 MIGRATION_STATUS_WAIT_UNPLUG);
3506
3507 while (s->state == MIGRATION_STATUS_WAIT_UNPLUG &&
3508 qemu_savevm_state_guest_unplug_pending()) {
3509 qemu_sem_timedwait(&s->wait_unplug_sem, 250);
3510 }
3511
3512 migrate_set_state(&s->state, MIGRATION_STATUS_WAIT_UNPLUG,
3513 MIGRATION_STATUS_ACTIVE);
3514 }
3515
Alex Blighbc72ad62013-08-21 16:03:08 +01003516 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang48781e52015-12-16 11:47:33 +00003517 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3518 MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04003519
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00003520 trace_migration_thread_setup_complete();
3521
Wei Yang8f8d5282019-07-17 08:53:41 +08003522 while (migration_is_active(s)) {
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003523 if (urgent || !qemu_file_rate_limit(s->to_dst_file)) {
Peter Xu2ad87302018-01-03 20:20:14 +08003524 MigIterateState iter_state = migration_iteration_run(s);
3525 if (iter_state == MIG_ITERATE_SKIP) {
3526 continue;
3527 } else if (iter_state == MIG_ITERATE_BREAK) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003528 break;
Juan Quintelac369f402012-10-03 20:33:34 +02003529 }
3530 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01003531
Peter Xub23c2ad2018-05-02 18:47:19 +08003532 /*
3533 * Try to detect any kind of failures, and see whether we
3534 * should stop the migration now.
3535 */
3536 thr_error = migration_detect_error(s);
3537 if (thr_error == MIG_THR_ERR_FATAL) {
3538 /* Stop migration */
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01003539 break;
Peter Xub23c2ad2018-05-02 18:47:19 +08003540 } else if (thr_error == MIG_THR_ERR_RECOVERED) {
3541 /*
3542 * Just recovered from a e.g. network failure, reset all
3543 * the local variables. This is important to avoid
3544 * breaking transferred_bytes and bandwidth calculation
3545 */
Ivan Ren87f3bd82019-08-02 18:18:41 +08003546 update_iteration_initial_status(s);
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01003547 }
Peter Xub15df1a2018-01-03 20:20:13 +08003548
Dr. David Alan Gilbert97e1e062019-12-05 10:29:18 +00003549 urgent = migration_rate_limit();
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01003550 }
3551
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003552 trace_migration_thread_after_loop();
Peter Xu199aa6d2018-01-03 20:20:15 +08003553 migration_iteration_finish(s);
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +00003554 object_unref(OBJECT(s));
Paolo Bonziniab28bd22015-07-09 08:55:38 +02003555 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003556 return NULL;
3557}
3558
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00003559void migrate_fd_connect(MigrationState *s, Error *error_in)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003560{
Juan Quintela00f4b572019-06-12 11:33:27 +02003561 Error *local_err = NULL;
Peter Xud3e35b82018-05-02 18:47:24 +08003562 int64_t rate_limit;
3563 bool resume = s->state == MIGRATION_STATUS_POSTCOPY_PAUSED;
3564
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05303565 s->expected_downtime = s->parameters.downtime_limit;
Marc-André Lureau9cbc3642020-03-25 19:47:21 +01003566 if (resume) {
3567 assert(s->cleanup_bh);
3568 } else {
3569 assert(!s->cleanup_bh);
3570 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup_bh, s);
3571 }
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00003572 if (error_in) {
3573 migrate_fd_error(s, error_in);
3574 migrate_fd_cleanup(s);
3575 return;
3576 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003577
Peter Xud3e35b82018-05-02 18:47:24 +08003578 if (resume) {
3579 /* This is a resumed migration */
Peter Xu8504dde2019-09-06 21:01:03 +08003580 rate_limit = s->parameters.max_postcopy_bandwidth /
3581 XFER_LIMIT_RATIO;
Peter Xud3e35b82018-05-02 18:47:24 +08003582 } else {
3583 /* This is a fresh new migration */
3584 rate_limit = s->parameters.max_bandwidth / XFER_LIMIT_RATIO;
Paolo Bonzini442773c2013-02-22 17:36:44 +01003585
Peter Xud3e35b82018-05-02 18:47:24 +08003586 /* Notify before starting migration thread */
3587 notifier_list_notify(&migration_state_notifiers, s);
3588 }
3589
3590 qemu_file_set_rate_limit(s->to_dst_file, rate_limit);
3591 qemu_file_set_blocking(s->to_dst_file, true);
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02003592
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003593 /*
Peter Xuc788ada2017-06-26 18:28:55 +08003594 * Open the return path. For postcopy, it is used exclusively. For
3595 * precopy, only if user specified "return-path" capability would
3596 * QEMU uses the return path.
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003597 */
Peter Xuc788ada2017-06-26 18:28:55 +08003598 if (migrate_postcopy_ram() || migrate_use_return_path()) {
Peter Xud3e35b82018-05-02 18:47:24 +08003599 if (open_return_path_on_source(s, !resume)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003600 error_report("Unable to open return-path for postcopy");
Peter Xud3e35b82018-05-02 18:47:24 +08003601 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003602 migrate_fd_cleanup(s);
3603 return;
3604 }
3605 }
3606
Peter Xud3e35b82018-05-02 18:47:24 +08003607 if (resume) {
Peter Xu135b87b2018-05-02 18:47:25 +08003608 /* Wakeup the main migration thread to do the recovery */
3609 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
3610 MIGRATION_STATUS_POSTCOPY_RECOVER);
3611 qemu_sem_post(&s->postcopy_pause_sem);
Peter Xud3e35b82018-05-02 18:47:24 +08003612 return;
3613 }
3614
Juan Quintela00f4b572019-06-12 11:33:27 +02003615 if (multifd_save_setup(&local_err) != 0) {
3616 error_report_err(local_err);
Juan Quintelaf986c3d2016-01-14 16:52:55 +01003617 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3618 MIGRATION_STATUS_FAILED);
3619 migrate_fd_cleanup(s);
3620 return;
3621 }
Pankaj Gupta009fad72017-01-23 19:12:56 +05303622 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01003623 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003624 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003625}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00003626
Peter Xu9d18af92017-06-27 12:10:19 +08003627void migration_global_dump(Monitor *mon)
3628{
3629 MigrationState *ms = migrate_get_current();
3630
Juan Quintela6f0f6422017-10-26 11:49:57 +02003631 monitor_printf(mon, "globals:\n");
3632 monitor_printf(mon, "store-global-state: %s\n",
3633 ms->store_global_state ? "on" : "off");
3634 monitor_printf(mon, "only-migratable: %s\n",
Markus Armbruster811f8652019-04-01 11:08:24 +02003635 only_migratable ? "on" : "off");
Juan Quintela6f0f6422017-10-26 11:49:57 +02003636 monitor_printf(mon, "send-configuration: %s\n",
3637 ms->send_configuration ? "on" : "off");
3638 monitor_printf(mon, "send-section-footer: %s\n",
3639 ms->send_section_footer ? "on" : "off");
Xiao Guangrongf5482222018-05-03 16:06:11 +08003640 monitor_printf(mon, "decompress-error-check: %s\n",
3641 ms->decompress_error_check ? "on" : "off");
Peter Xu002cad62019-06-03 14:50:56 +08003642 monitor_printf(mon, "clear-bitmap-shift: %u\n",
3643 ms->clear_bitmap_shift);
Peter Xu9d18af92017-06-27 12:10:19 +08003644}
3645
Peter Xu20814752017-07-18 11:39:03 +08003646#define DEFINE_PROP_MIG_CAP(name, x) \
3647 DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
3648
Peter Xu52722982017-06-27 12:10:14 +08003649static Property migration_properties[] = {
3650 DEFINE_PROP_BOOL("store-global-state", MigrationState,
3651 store_global_state, true),
Peter Xu71dd4c12017-06-27 12:10:16 +08003652 DEFINE_PROP_BOOL("send-configuration", MigrationState,
3653 send_configuration, true),
Peter Xu15c38502017-06-27 12:10:17 +08003654 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
3655 send_section_footer, true),
Xiao Guangrongf5482222018-05-03 16:06:11 +08003656 DEFINE_PROP_BOOL("decompress-error-check", MigrationState,
3657 decompress_error_check, true),
Peter Xu002cad62019-06-03 14:50:56 +08003658 DEFINE_PROP_UINT8("x-clear-bitmap-shift", MigrationState,
3659 clear_bitmap_shift, CLEAR_BITMAP_SHIFT_DEFAULT),
Peter Xu89632fa2017-07-18 11:39:02 +08003660
3661 /* Migration parameters */
Juan Quintela741d4082017-12-01 13:08:38 +01003662 DEFINE_PROP_UINT8("x-compress-level", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003663 parameters.compress_level,
3664 DEFAULT_MIGRATE_COMPRESS_LEVEL),
Juan Quintela741d4082017-12-01 13:08:38 +01003665 DEFINE_PROP_UINT8("x-compress-threads", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003666 parameters.compress_threads,
3667 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
Xiao Guangrong1d588722018-08-21 16:10:20 +08003668 DEFINE_PROP_BOOL("x-compress-wait-thread", MigrationState,
3669 parameters.compress_wait_thread, true),
Juan Quintela741d4082017-12-01 13:08:38 +01003670 DEFINE_PROP_UINT8("x-decompress-threads", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003671 parameters.decompress_threads,
3672 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
Keqian Zhudc14a472020-02-24 10:31:42 +08003673 DEFINE_PROP_UINT8("x-throttle-trigger-threshold", MigrationState,
3674 parameters.throttle_trigger_threshold,
3675 DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD),
Juan Quintela741d4082017-12-01 13:08:38 +01003676 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003677 parameters.cpu_throttle_initial,
3678 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
Juan Quintela741d4082017-12-01 13:08:38 +01003679 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003680 parameters.cpu_throttle_increment,
3681 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
Keqian Zhucbbf8182020-04-13 18:15:08 +08003682 DEFINE_PROP_BOOL("x-cpu-throttle-tailslow", MigrationState,
3683 parameters.cpu_throttle_tailslow, false),
Juan Quintela741d4082017-12-01 13:08:38 +01003684 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003685 parameters.max_bandwidth, MAX_THROTTLE),
Juan Quintela741d4082017-12-01 13:08:38 +01003686 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003687 parameters.downtime_limit,
3688 DEFAULT_MIGRATE_SET_DOWNTIME),
Juan Quintela741d4082017-12-01 13:08:38 +01003689 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003690 parameters.x_checkpoint_delay,
3691 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
Juan Quintelacbfd6c92019-02-06 13:54:06 +01003692 DEFINE_PROP_UINT8("multifd-channels", MigrationState,
3693 parameters.multifd_channels,
Juan Quintela4075fb12016-01-15 08:56:17 +01003694 DEFAULT_MIGRATE_MULTIFD_CHANNELS),
Juan Quintela96eef042019-01-16 10:35:55 +01003695 DEFINE_PROP_MULTIFD_COMPRESSION("multifd-compression", MigrationState,
3696 parameters.multifd_compression,
3697 DEFAULT_MIGRATE_MULTIFD_COMPRESSION),
Juan Quintela9004db42020-01-23 17:08:52 +01003698 DEFINE_PROP_UINT8("multifd-zlib-level", MigrationState,
3699 parameters.multifd_zlib_level,
3700 DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL),
Juan Quintela6a9ad152020-01-23 17:41:36 +01003701 DEFINE_PROP_UINT8("multifd-zstd-level", MigrationState,
3702 parameters.multifd_zstd_level,
3703 DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL),
Juan Quintela73af8dd2017-10-05 21:30:10 +02003704 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
3705 parameters.xbzrle_cache_size,
3706 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01003707 DEFINE_PROP_SIZE("max-postcopy-bandwidth", MigrationState,
3708 parameters.max_postcopy_bandwidth,
3709 DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH),
Li Qiang4cbc9c72018-08-01 06:00:20 -07003710 DEFINE_PROP_UINT8("max-cpu-throttle", MigrationState,
3711 parameters.max_cpu_throttle,
3712 DEFAULT_MIGRATE_MAX_CPU_THROTTLE),
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00003713 DEFINE_PROP_SIZE("announce-initial", MigrationState,
3714 parameters.announce_initial,
3715 DEFAULT_MIGRATE_ANNOUNCE_INITIAL),
3716 DEFINE_PROP_SIZE("announce-max", MigrationState,
3717 parameters.announce_max,
3718 DEFAULT_MIGRATE_ANNOUNCE_MAX),
3719 DEFINE_PROP_SIZE("announce-rounds", MigrationState,
3720 parameters.announce_rounds,
3721 DEFAULT_MIGRATE_ANNOUNCE_ROUNDS),
3722 DEFINE_PROP_SIZE("announce-step", MigrationState,
3723 parameters.announce_step,
3724 DEFAULT_MIGRATE_ANNOUNCE_STEP),
Peter Xu20814752017-07-18 11:39:03 +08003725
3726 /* Migration capabilities */
3727 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
3728 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
3729 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
3730 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
3731 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
3732 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
3733 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
3734 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
3735 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
3736 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
3737 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
Juan Quintelacbfd6c92019-02-06 13:54:06 +01003738 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_MULTIFD),
Peter Xu20814752017-07-18 11:39:03 +08003739
Peter Xu52722982017-06-27 12:10:14 +08003740 DEFINE_PROP_END_OF_LIST(),
3741};
3742
Peter Xue5cb7e72017-06-27 12:10:13 +08003743static void migration_class_init(ObjectClass *klass, void *data)
3744{
3745 DeviceClass *dc = DEVICE_CLASS(klass);
3746
3747 dc->user_creatable = false;
Marc-André Lureau4f67d302020-01-10 19:30:32 +04003748 device_class_set_props(dc, migration_properties);
Peter Xue5cb7e72017-06-27 12:10:13 +08003749}
3750
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003751static void migration_instance_finalize(Object *obj)
3752{
3753 MigrationState *ms = MIGRATION_OBJ(obj);
3754 MigrationParameters *params = &ms->parameters;
3755
Juan Quintela87db1a72017-09-05 12:50:22 +02003756 qemu_mutex_destroy(&ms->error_mutex);
Peter Xu62df0662018-05-02 18:47:38 +08003757 qemu_mutex_destroy(&ms->qemu_file_lock);
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003758 g_free(params->tls_hostname);
3759 g_free(params->tls_creds);
Jens Freimannc7e0acd2019-10-29 12:49:02 +01003760 qemu_sem_destroy(&ms->wait_unplug_sem);
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003761 qemu_sem_destroy(&ms->rate_limit_sem);
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01003762 qemu_sem_destroy(&ms->pause_sem);
Peter Xub23c2ad2018-05-02 18:47:19 +08003763 qemu_sem_destroy(&ms->postcopy_pause_sem);
Peter Xu14b17422018-05-02 18:47:21 +08003764 qemu_sem_destroy(&ms->postcopy_pause_rp_sem);
Peter Xuedd090c2018-05-02 18:47:32 +08003765 qemu_sem_destroy(&ms->rp_state.rp_sem);
Marc-André Lureauab105cc2018-03-06 18:09:59 +01003766 error_free(ms->error);
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003767}
3768
Peter Xue5cb7e72017-06-27 12:10:13 +08003769static void migration_instance_init(Object *obj)
3770{
3771 MigrationState *ms = MIGRATION_OBJ(obj);
Peter Xu8b0b29d2017-07-18 11:39:06 +08003772 MigrationParameters *params = &ms->parameters;
Peter Xue5cb7e72017-06-27 12:10:13 +08003773
3774 ms->state = MIGRATION_STATUS_NONE;
Peter Xue5cb7e72017-06-27 12:10:13 +08003775 ms->mbps = -1;
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08003776 ms->pages_per_second = -1;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01003777 qemu_sem_init(&ms->pause_sem, 0);
Juan Quintela87db1a72017-09-05 12:50:22 +02003778 qemu_mutex_init(&ms->error_mutex);
Peter Xu8b0b29d2017-07-18 11:39:06 +08003779
3780 params->tls_hostname = g_strdup("");
3781 params->tls_creds = g_strdup("");
3782
3783 /* Set has_* up only for parameter checks */
3784 params->has_compress_level = true;
3785 params->has_compress_threads = true;
3786 params->has_decompress_threads = true;
Keqian Zhudc14a472020-02-24 10:31:42 +08003787 params->has_throttle_trigger_threshold = true;
Peter Xu8b0b29d2017-07-18 11:39:06 +08003788 params->has_cpu_throttle_initial = true;
3789 params->has_cpu_throttle_increment = true;
Keqian Zhucbbf8182020-04-13 18:15:08 +08003790 params->has_cpu_throttle_tailslow = true;
Peter Xu8b0b29d2017-07-18 11:39:06 +08003791 params->has_max_bandwidth = true;
3792 params->has_downtime_limit = true;
3793 params->has_x_checkpoint_delay = true;
3794 params->has_block_incremental = true;
Juan Quintelacbfd6c92019-02-06 13:54:06 +01003795 params->has_multifd_channels = true;
Juan Quintela96eef042019-01-16 10:35:55 +01003796 params->has_multifd_compression = true;
Juan Quintela9004db42020-01-23 17:08:52 +01003797 params->has_multifd_zlib_level = true;
Juan Quintela6a9ad152020-01-23 17:41:36 +01003798 params->has_multifd_zstd_level = true;
Juan Quintela73af8dd2017-10-05 21:30:10 +02003799 params->has_xbzrle_cache_size = true;
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01003800 params->has_max_postcopy_bandwidth = true;
Li Qiang4cbc9c72018-08-01 06:00:20 -07003801 params->has_max_cpu_throttle = true;
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00003802 params->has_announce_initial = true;
3803 params->has_announce_max = true;
3804 params->has_announce_rounds = true;
3805 params->has_announce_step = true;
Peter Xub23c2ad2018-05-02 18:47:19 +08003806
3807 qemu_sem_init(&ms->postcopy_pause_sem, 0);
Peter Xu14b17422018-05-02 18:47:21 +08003808 qemu_sem_init(&ms->postcopy_pause_rp_sem, 0);
Peter Xuedd090c2018-05-02 18:47:32 +08003809 qemu_sem_init(&ms->rp_state.rp_sem, 0);
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003810 qemu_sem_init(&ms->rate_limit_sem, 0);
Jens Freimannc7e0acd2019-10-29 12:49:02 +01003811 qemu_sem_init(&ms->wait_unplug_sem, 0);
Peter Xu62df0662018-05-02 18:47:38 +08003812 qemu_mutex_init(&ms->qemu_file_lock);
Peter Xu8b0b29d2017-07-18 11:39:06 +08003813}
3814
3815/*
3816 * Return true if check pass, false otherwise. Error will be put
3817 * inside errp if provided.
3818 */
3819static bool migration_object_check(MigrationState *ms, Error **errp)
3820{
Peter Xu6b19a7d2017-07-18 11:39:10 +08003821 MigrationCapabilityStatusList *head = NULL;
3822 /* Assuming all off */
3823 bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret;
3824 int i;
3825
Peter Xu8b0b29d2017-07-18 11:39:06 +08003826 if (!migrate_params_check(&ms->parameters, errp)) {
3827 return false;
3828 }
3829
Peter Xu6b19a7d2017-07-18 11:39:10 +08003830 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
3831 if (ms->enabled_capabilities[i]) {
3832 head = migrate_cap_add(head, i, true);
3833 }
3834 }
3835
3836 ret = migrate_caps_check(cap_list, head, errp);
3837
3838 /* It works with head == NULL */
3839 qapi_free_MigrationCapabilityStatusList(head);
3840
3841 return ret;
Peter Xue5cb7e72017-06-27 12:10:13 +08003842}
3843
3844static const TypeInfo migration_type = {
3845 .name = TYPE_MIGRATION,
Peter Xu01f6e142017-06-28 15:15:44 +08003846 /*
Peter Xuc8d3ff32017-07-05 16:21:23 +08003847 * NOTE: TYPE_MIGRATION is not really a device, as the object is
Markus Armbruster2194abd2020-06-10 07:32:19 +02003848 * not created using qdev_new(), it is not attached to the qdev
Peter Xuc8d3ff32017-07-05 16:21:23 +08003849 * device tree, and it is never realized.
3850 *
3851 * TODO: Make this TYPE_OBJECT once QOM provides something like
3852 * TYPE_DEVICE's "-global" properties.
Peter Xu01f6e142017-06-28 15:15:44 +08003853 */
Peter Xue5cb7e72017-06-27 12:10:13 +08003854 .parent = TYPE_DEVICE,
3855 .class_init = migration_class_init,
3856 .class_size = sizeof(MigrationClass),
3857 .instance_size = sizeof(MigrationState),
3858 .instance_init = migration_instance_init,
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003859 .instance_finalize = migration_instance_finalize,
Peter Xue5cb7e72017-06-27 12:10:13 +08003860};
3861
3862static void register_migration_types(void)
3863{
3864 type_register_static(&migration_type);
3865}
3866
3867type_init(register_migration_types);