blob: 4e658c397e542fc0a546ec3f864a9668396a11b1 [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"
39#include "qapi/qapi-visit-sockets.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010040#include "qapi/qapi-commands-migration.h"
41#include "qapi/qapi-events-migration.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010042#include "qapi/qmp/qerror.h"
Markus Armbruster15280c32018-02-01 12:18:36 +010043#include "qapi/qmp/qnull.h"
Paolo Bonziniab28bd22015-07-09 08:55:38 +020044#include "qemu/rcu.h"
Juan Quintela2c9e6fe2017-04-21 14:31:22 +020045#include "block.h"
Juan Quintelabe07b0a2017-04-20 13:12:24 +020046#include "postcopy-ram.h"
Juan Quintela766bd172012-07-23 05:45:29 +020047#include "qemu/thread.h"
Kazuya Saitoc09e5bb2013-02-22 17:36:19 +010048#include "trace.h"
Juan Quintela51180422017-04-24 20:50:19 +020049#include "exec/target_page.h"
Daniel P. Berrange61b67d42016-04-27 11:05:01 +010050#include "io/channel-buffer.h"
zhanghailiang35a6ed42016-10-27 14:42:52 +080051#include "migration/colo.h"
Peter Xu4ffdb332017-06-27 12:10:18 +080052#include "hw/boards.h"
Markus Armbrustera27bd6c2019-08-12 07:23:51 +020053#include "hw/qdev-properties.h"
Peter Xu9d18af92017-06-27 12:10:19 +080054#include "monitor/monitor.h"
Dr. David Alan Gilbert50510ea2019-02-27 13:24:05 +000055#include "net/announce.h"
Jens Freimannc7e0acd2019-10-29 12:49:02 +010056#include "qemu/queue.h"
Juan Quintelad32ca5a2020-01-22 16:16:07 +010057#include "multifd.h"
aliguori065e2812008-11-11 16:46:33 +000058
Jason J. Hernedc325622015-09-08 13:12:37 -040059#define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000060
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010061/* Amount of time to allocate to each "chunk" of bandwidth-throttled
62 * data. */
63#define BUFFER_DELAY 100
64#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
65
Ashijeet Acharya2ff30252016-09-15 21:50:28 +053066/* Time in milliseconds we are allowed to stop the source,
67 * for sending the last part */
68#define DEFAULT_MIGRATE_SET_DOWNTIME 300
69
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -030070/* Maximum migrate downtime set to 2000 seconds */
71#define MAX_MIGRATE_DOWNTIME_SECONDS 2000
72#define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
73
Liang Li8706d2d2015-03-23 16:32:17 +080074/* Default compression thread count */
75#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080076/* Default decompression thread count, usually decompression is at
77 * least 4 times as fast as compression.*/
78#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080079/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
80#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
Jason J. Herne1626fee2015-09-08 13:12:34 -040081/* Define default autoconverge cpu throttle migration parameters */
Keqian Zhudc14a472020-02-24 10:31:42 +080082#define DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD 50
Jason J. Herned85a31d2016-04-21 14:07:18 -040083#define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
84#define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
Li Qiang4cbc9c72018-08-01 06:00:20 -070085#define DEFAULT_MIGRATE_MAX_CPU_THROTTLE 99
Liang Li8706d2d2015-03-23 16:32:17 +080086
Orit Wasserman17ad9b32012-08-06 21:42:53 +030087/* Migration XBZRLE default cache size */
Juan Quintela73af8dd2017-10-05 21:30:10 +020088#define DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE (64 * 1024 * 1024)
Orit Wasserman17ad9b32012-08-06 21:42:53 +030089
Zhang Chen131b2152018-09-03 12:38:45 +080090/* The delay time (in ms) between two COLO checkpoints */
91#define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY (200 * 100)
Juan Quintela4075fb12016-01-15 08:56:17 +010092#define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2
Juan Quintela96eef042019-01-16 10:35:55 +010093#define DEFAULT_MIGRATE_MULTIFD_COMPRESSION MULTIFD_COMPRESSION_NONE
Juan Quintela9004db42020-01-23 17:08:52 +010094/* 0: means nocompress, 1: best speed, ... 9: best compress ratio */
95#define DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL 1
Juan Quintela6a9ad152020-01-23 17:41:36 +010096/* 0: means nocompress, 1: best speed, ... 20: best compress ratio */
97#define DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL 1
zhanghailiang68b53592016-10-27 14:43:01 +080098
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +010099/* Background transfer rate for postcopy, 0 means unlimited, note
100 * that page requests can still exceed this limit.
101 */
102#define DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH 0
103
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +0000104/*
105 * Parameters for self_announce_delay giving a stream of RARP/ARP
106 * packets after migration.
107 */
108#define DEFAULT_MIGRATE_ANNOUNCE_INITIAL 50
109#define DEFAULT_MIGRATE_ANNOUNCE_MAX 550
110#define DEFAULT_MIGRATE_ANNOUNCE_ROUNDS 5
111#define DEFAULT_MIGRATE_ANNOUNCE_STEP 100
112
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100113static NotifierList migration_state_notifiers =
114 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
115
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000116static bool deferred_incoming;
117
Juan Quintelada6f1792017-04-24 17:37:14 +0200118/* Messages sent on the return path from destination to source */
119enum mig_rp_message_type {
120 MIG_RP_MSG_INVALID = 0, /* Must be 0 */
121 MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */
122 MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */
123
124 MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */
125 MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */
Peter Xua335deb2018-05-02 18:47:28 +0800126 MIG_RP_MSG_RECV_BITMAP, /* send recved_bitmap back to source */
Peter Xu13955b82018-05-02 18:47:30 +0800127 MIG_RP_MSG_RESUME_ACK, /* tell source that we are ready to resume */
Juan Quintelada6f1792017-04-24 17:37:14 +0200128
129 MIG_RP_MSG_MAX
130};
131
Juan Quintela17549e82011-10-05 13:50:43 +0200132/* When we add fault tolerance, we could have several
133 migrations at once. For now we don't need to add
134 dynamic creation of migration */
135
Peter Xue5cb7e72017-06-27 12:10:13 +0800136static MigrationState *current_migration;
Peter Xue1b1b1b2018-05-02 18:47:35 +0800137static MigrationIncomingState *current_incoming;
Peter Xue5cb7e72017-06-27 12:10:13 +0800138
Peter Xu8b0b29d2017-07-18 11:39:06 +0800139static bool migration_object_check(MigrationState *ms, Error **errp);
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +0100140static int migration_maybe_pause(MigrationState *s,
141 int *current_active_state,
142 int new_state);
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +0000143static void migrate_fd_cancel(MigrationState *s);
Peter Xu8b0b29d2017-07-18 11:39:06 +0800144
Peter Xue5cb7e72017-06-27 12:10:13 +0800145void migration_object_init(void)
146{
Peter Xu4ffdb332017-06-27 12:10:18 +0800147 MachineState *ms = MACHINE(qdev_get_machine());
Peter Xu8b0b29d2017-07-18 11:39:06 +0800148 Error *err = NULL;
Peter Xu4ffdb332017-06-27 12:10:18 +0800149
Peter Xue5cb7e72017-06-27 12:10:13 +0800150 /* This can only be called once. */
151 assert(!current_migration);
152 current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION));
Peter Xu4ffdb332017-06-27 12:10:18 +0800153
Peter Xue1b1b1b2018-05-02 18:47:35 +0800154 /*
155 * Init the migrate incoming object as well no matter whether
156 * we'll use it or not.
157 */
158 assert(!current_incoming);
159 current_incoming = g_new0(MigrationIncomingState, 1);
160 current_incoming->state = MIGRATION_STATUS_NONE;
161 current_incoming->postcopy_remote_fds =
162 g_array_new(FALSE, TRUE, sizeof(struct PostCopyFD));
163 qemu_mutex_init(&current_incoming->rp_mutex);
164 qemu_event_init(&current_incoming->main_thread_load_event, false);
165 qemu_sem_init(&current_incoming->postcopy_pause_sem_dst, 0);
166 qemu_sem_init(&current_incoming->postcopy_pause_sem_fault, 0);
167
168 init_dirty_bitmap_incoming_migration();
169
Peter Xu8b0b29d2017-07-18 11:39:06 +0800170 if (!migration_object_check(current_migration, &err)) {
171 error_report_err(err);
172 exit(1);
173 }
174
Peter Xu4ffdb332017-06-27 12:10:18 +0800175 /*
176 * We cannot really do this in migration_instance_init() since at
177 * that time global properties are not yet applied, then this
178 * value will be definitely replaced by something else.
179 */
180 if (ms->enforce_config_section) {
181 current_migration->send_configuration = true;
182 }
Peter Xue5cb7e72017-06-27 12:10:13 +0800183}
184
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +0000185void migration_shutdown(void)
Vladimir Sementsov-Ogievskiy1f895602017-12-28 12:16:16 +0300186{
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +0000187 /*
188 * Cancel the current migration - that will (eventually)
189 * stop the migration using this structure
190 */
191 migrate_fd_cancel(current_migration);
Vladimir Sementsov-Ogievskiy1f895602017-12-28 12:16:16 +0300192 object_unref(OBJECT(current_migration));
193}
194
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100195/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +0200196MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +0200197{
Peter Xue5cb7e72017-06-27 12:10:13 +0800198 /* This can only be called after the object created. */
199 assert(current_migration);
200 return current_migration;
Juan Quintela17549e82011-10-05 13:50:43 +0200201}
202
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100203MigrationIncomingState *migration_incoming_get_current(void)
204{
Peter Xue1b1b1b2018-05-02 18:47:35 +0800205 assert(current_incoming);
206 return current_incoming;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100207}
208
209void migration_incoming_state_destroy(void)
210{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100211 struct MigrationIncomingState *mis = migration_incoming_get_current();
212
Peter Xu34826552017-05-19 14:43:29 +0800213 if (mis->to_src_file) {
Peter Xu660819b2017-05-19 14:43:30 +0800214 /* Tell source that we are done */
215 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
Peter Xu34826552017-05-19 14:43:29 +0800216 qemu_fclose(mis->to_src_file);
217 mis->to_src_file = NULL;
218 }
219
Peter Xu660819b2017-05-19 14:43:30 +0800220 if (mis->from_src_file) {
221 qemu_fclose(mis->from_src_file);
222 mis->from_src_file = NULL;
223 }
Dr. David Alan Gilbert00fa4fc2018-03-12 17:21:04 +0000224 if (mis->postcopy_remote_fds) {
225 g_array_free(mis->postcopy_remote_fds, TRUE);
226 mis->postcopy_remote_fds = NULL;
227 }
Peter Xu660819b2017-05-19 14:43:30 +0800228
Dr. David Alan Gilbert5089e182017-08-25 15:19:39 +0100229 qemu_event_reset(&mis->main_thread_load_event);
Juan Quintela9aca82b2019-02-27 11:51:27 +0100230
231 if (mis->socket_address_list) {
232 qapi_free_SocketAddressList(mis->socket_address_list);
233 mis->socket_address_list = NULL;
234 }
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100235}
236
Juan Quintelab05dc722015-07-07 14:44:05 +0200237static void migrate_generate_event(int new_state)
238{
239 if (migrate_use_events()) {
Peter Xu3ab72382018-08-15 21:37:37 +0800240 qapi_event_send_migration(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200241 }
242}
243
Dr. David Alan Gilbert0f073f42018-04-16 18:09:30 +0100244static bool migrate_late_block_activate(void)
245{
246 MigrationState *s;
247
248 s = migrate_get_current();
249
250 return s->enabled_capabilities[
251 MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE];
252}
253
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000254/*
255 * Called on -incoming with a defer: uri.
256 * The migration can be started later after any parameters have been
257 * changed.
258 */
259static void deferred_incoming_migration(Error **errp)
260{
261 if (deferred_incoming) {
262 error_setg(errp, "Incoming migration already deferred");
263 }
264 deferred_incoming = true;
265}
266
Juan Quintelada6f1792017-04-24 17:37:14 +0200267/*
268 * Send a message on the return channel back to the source
269 * of the migration.
270 */
Peter Xud6208e32018-02-08 18:31:12 +0800271static int migrate_send_rp_message(MigrationIncomingState *mis,
272 enum mig_rp_message_type message_type,
273 uint16_t len, void *data)
Juan Quintelada6f1792017-04-24 17:37:14 +0200274{
Peter Xud6208e32018-02-08 18:31:12 +0800275 int ret = 0;
276
Juan Quintelada6f1792017-04-24 17:37:14 +0200277 trace_migrate_send_rp_message((int)message_type, len);
278 qemu_mutex_lock(&mis->rp_mutex);
Peter Xud6208e32018-02-08 18:31:12 +0800279
280 /*
281 * It's possible that the file handle got lost due to network
282 * failures.
283 */
284 if (!mis->to_src_file) {
285 ret = -EIO;
286 goto error;
287 }
288
Juan Quintelada6f1792017-04-24 17:37:14 +0200289 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
290 qemu_put_be16(mis->to_src_file, len);
291 qemu_put_buffer(mis->to_src_file, data, len);
292 qemu_fflush(mis->to_src_file);
Peter Xud6208e32018-02-08 18:31:12 +0800293
294 /* It's possible that qemu file got error during sending */
295 ret = qemu_file_get_error(mis->to_src_file);
296
297error:
Juan Quintelada6f1792017-04-24 17:37:14 +0200298 qemu_mutex_unlock(&mis->rp_mutex);
Peter Xud6208e32018-02-08 18:31:12 +0800299 return ret;
Juan Quintelada6f1792017-04-24 17:37:14 +0200300}
301
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000302/* Request a range of pages from the source VM at the given
303 * start address.
304 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
305 * as the last request (a name must have been given previously)
306 * Start: Address offset within the RB
307 * Len: Length in bytes required - must be a multiple of pagesize
308 */
Peter Xud6208e32018-02-08 18:31:12 +0800309int migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
310 ram_addr_t start, size_t len)
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000311{
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100312 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000313 size_t msglen = 12; /* start + len */
Peter Xud6208e32018-02-08 18:31:12 +0800314 enum mig_rp_message_type msg_type;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000315
316 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
317 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
318
319 if (rbname) {
320 int rbname_len = strlen(rbname);
321 assert(rbname_len < 256);
322
323 bufc[msglen++] = rbname_len;
324 memcpy(bufc + msglen, rbname, rbname_len);
325 msglen += rbname_len;
Peter Xud6208e32018-02-08 18:31:12 +0800326 msg_type = MIG_RP_MSG_REQ_PAGES_ID;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000327 } else {
Peter Xud6208e32018-02-08 18:31:12 +0800328 msg_type = MIG_RP_MSG_REQ_PAGES;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000329 }
Peter Xud6208e32018-02-08 18:31:12 +0800330
331 return migrate_send_rp_message(mis, msg_type, msglen, bufc);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000332}
333
Zhang Chenaad555c2018-09-03 12:38:47 +0800334static bool migration_colo_enabled;
335bool migration_incoming_colo_enabled(void)
336{
337 return migration_colo_enabled;
338}
339
340void migration_incoming_disable_colo(void)
341{
David Hildenbrand18b1d3c2020-06-26 09:22:36 +0200342 ram_block_discard_disable(false);
Zhang Chenaad555c2018-09-03 12:38:47 +0800343 migration_colo_enabled = false;
344}
345
David Hildenbrand18b1d3c2020-06-26 09:22:36 +0200346int migration_incoming_enable_colo(void)
Zhang Chenaad555c2018-09-03 12:38:47 +0800347{
David Hildenbrand18b1d3c2020-06-26 09:22:36 +0200348 if (ram_block_discard_disable(true)) {
349 error_report("COLO: cannot disable RAM discard");
350 return -EBUSY;
351 }
Zhang Chenaad555c2018-09-03 12:38:47 +0800352 migration_colo_enabled = true;
David Hildenbrand18b1d3c2020-06-26 09:22:36 +0200353 return 0;
Zhang Chenaad555c2018-09-03 12:38:47 +0800354}
355
Juan Quintela9aca82b2019-02-27 11:51:27 +0100356void migrate_add_address(SocketAddress *address)
357{
358 MigrationIncomingState *mis = migration_incoming_get_current();
359 SocketAddressList *addrs;
360
361 addrs = g_new0(SocketAddressList, 1);
362 addrs->next = mis->socket_address_list;
363 mis->socket_address_list = addrs;
364 addrs->value = QAPI_CLONE(SocketAddress, address);
365}
366
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200367void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000368{
aliguori34c9dd82008-10-13 03:14:31 +0000369 const char *p;
370
Peter Xu3ab72382018-08-15 21:37:37 +0800371 qapi_event_send_migration(MIGRATION_STATUS_SETUP);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000372 if (!strcmp(uri, "defer")) {
373 deferred_incoming_migration(errp);
374 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200375 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400376#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000377 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400378 rdma_start_incoming_migration(p, errp);
379#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000380 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200381 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000382 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200383 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000384 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200385 fd_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000386 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100387 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200388 }
aliguori5bb79102008-10-13 03:12:02 +0000389}
390
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300391static void process_incoming_migration_bh(void *opaque)
392{
393 Error *local_err = NULL;
394 MigrationIncomingState *mis = opaque;
395
Dr. David Alan Gilbert0f073f42018-04-16 18:09:30 +0100396 /* If capability late_block_activate is set:
397 * Only fire up the block code now if we're going to restart the
398 * VM, else 'cont' will do it.
399 * This causes file locking to happen; so we don't want it to happen
400 * unless we really are starting the VM.
401 */
402 if (!migrate_late_block_activate() ||
403 (autostart && (!global_state_received() ||
404 global_state_get_runstate() == RUN_STATE_RUNNING))) {
405 /* Make sure all file formats flush their mutable metadata.
406 * If we get an error here, just don't restart the VM yet. */
407 bdrv_invalidate_cache_all(&local_err);
408 if (local_err) {
409 error_report_err(local_err);
410 local_err = NULL;
411 autostart = false;
412 }
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200413 }
414
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300415 /*
416 * This must happen after all error conditions are dealt with and
417 * we're sure the VM is going to be running on this host.
418 */
Dr. David Alan Gilbert76595052019-02-27 13:24:08 +0000419 qemu_announce_self(&mis->announce_timer, migrate_announce_params());
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300420
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100421 if (multifd_load_cleanup(&local_err) != 0) {
422 error_report_err(local_err);
423 autostart = false;
424 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300425 /* If global state section was not received or we are in running
426 state, we need to obey autostart. Any other state is set with
427 runstate_set. */
428
Vladimir Sementsov-Ogievskiyb35ebdf2018-03-13 15:34:01 -0400429 dirty_bitmap_mig_before_vm_start();
430
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300431 if (!global_state_received() ||
432 global_state_get_runstate() == RUN_STATE_RUNNING) {
433 if (autostart) {
434 vm_start();
435 } else {
436 runstate_set(RUN_STATE_PAUSED);
437 }
Zhang Chendb009722019-03-03 22:50:17 +0800438 } else if (migration_incoming_colo_enabled()) {
439 migration_incoming_disable_colo();
440 vm_start();
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300441 } else {
442 runstate_set(global_state_get_runstate());
443 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300444 /*
445 * This must happen after any state changes since as soon as an external
446 * observer sees this event they might start to prod at the VM assuming
447 * it's ready to use.
448 */
449 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
450 MIGRATION_STATUS_COMPLETED);
451 qemu_bh_delete(mis->bh);
452 migration_incoming_state_destroy();
453}
454
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200455static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200456{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100457 MigrationIncomingState *mis = migration_incoming_get_current();
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000458 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200459 int ret;
Zhang Chen8e48ac92018-09-03 12:38:46 +0800460 Error *local_err = NULL;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200461
Juan Quintela4f0fae72017-07-24 12:42:02 +0200462 assert(mis->from_src_file);
Lidong Chen92370982018-08-06 21:29:35 +0800463 mis->migration_incoming_co = qemu_coroutine_self();
Dr. David Alan Gilbert67f11b52017-02-24 18:28:34 +0000464 mis->largest_page_size = qemu_ram_pagesize_largest();
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000465 postcopy_state_set(POSTCOPY_INCOMING_NONE);
zhanghailiang93d7af62015-12-16 11:47:34 +0000466 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
467 MIGRATION_STATUS_ACTIVE);
Juan Quintela4f0fae72017-07-24 12:42:02 +0200468 ret = qemu_loadvm_state(mis->from_src_file);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100469
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000470 ps = postcopy_state_get();
471 trace_process_incoming_migration_co_end(ret, ps);
472 if (ps != POSTCOPY_INCOMING_NONE) {
473 if (ps == POSTCOPY_INCOMING_ADVISE) {
474 /*
475 * Where a migration had postcopy enabled (and thus went to advise)
476 * but managed to complete within the precopy period, we can use
477 * the normal exit.
478 */
479 postcopy_ram_incoming_cleanup(mis);
480 } else if (ret >= 0) {
481 /*
482 * Postcopy was started, cleanup should happen at the end of the
483 * postcopy thread.
484 */
485 trace_process_incoming_migration_co_postcopy_end_main();
486 return;
487 }
488 /* Else if something went wrong then just fall out of the normal exit */
489 }
490
zhanghailiang25d0c162016-10-27 14:42:55 +0800491 /* we get COLO info, and know if we are in COLO mode */
Zhang Chenaad555c2018-09-03 12:38:47 +0800492 if (!ret && migration_incoming_colo_enabled()) {
Zhang Chen8e48ac92018-09-03 12:38:46 +0800493 /* Make sure all file formats flush their mutable metadata */
494 bdrv_invalidate_cache_all(&local_err);
495 if (local_err) {
Zhang Chen8e48ac92018-09-03 12:38:46 +0800496 error_report_err(local_err);
Fei Li6d99c2d2019-01-13 22:08:49 +0800497 goto fail;
Zhang Chen8e48ac92018-09-03 12:38:46 +0800498 }
499
zhanghailiang25d0c162016-10-27 14:42:55 +0800500 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
501 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
502 mis->have_colo_incoming_thread = true;
503 qemu_coroutine_yield();
504
505 /* Wait checkpoint incoming thread exit before free resource */
506 qemu_thread_join(&mis->colo_incoming_thread);
Zhang Chen13af18f2018-09-03 12:38:48 +0800507 /* We hold the global iothread lock, so it is safe here */
508 colo_release_ram_cache();
zhanghailiang25d0c162016-10-27 14:42:55 +0800509 }
510
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200511 if (ret < 0) {
Peter Lievendb80fac2014-06-10 11:29:16 +0200512 error_report("load of migration failed: %s", strerror(-ret));
Fei Li6d99c2d2019-01-13 22:08:49 +0800513 goto fail;
Juan Quintela511c0232010-06-09 14:10:55 +0200514 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300515 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
516 qemu_bh_schedule(mis->bh);
Lidong Chen92370982018-08-06 21:29:35 +0800517 mis->migration_incoming_co = NULL;
Fei Li6d99c2d2019-01-13 22:08:49 +0800518 return;
519fail:
520 local_err = NULL;
521 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
522 MIGRATION_STATUS_FAILED);
523 qemu_fclose(mis->from_src_file);
524 if (multifd_load_cleanup(&local_err) != 0) {
525 error_report_err(local_err);
526 }
527 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200528}
529
Juan Quintelab673eab2019-06-12 11:44:19 +0200530/**
531 * @migration_incoming_setup: Setup incoming migration
532 *
533 * Returns 0 for no error or 1 for error
534 *
535 * @f: file for main migration channel
536 * @errp: where to put errors
537 */
538static int migration_incoming_setup(QEMUFile *f, Error **errp)
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200539{
Juan Quintela4f0fae72017-07-24 12:42:02 +0200540 MigrationIncomingState *mis = migration_incoming_get_current();
Juan Quintelab673eab2019-06-12 11:44:19 +0200541 Error *local_err = NULL;
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200542
Juan Quintelab673eab2019-06-12 11:44:19 +0200543 if (multifd_load_setup(&local_err) != 0) {
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100544 /* We haven't been able to create multifd threads
545 nothing better to do */
Juan Quintelab673eab2019-06-12 11:44:19 +0200546 error_report_err(local_err);
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100547 exit(EXIT_FAILURE);
548 }
549
Juan Quintela4f0fae72017-07-24 12:42:02 +0200550 if (!mis->from_src_file) {
551 mis->from_src_file = f;
552 }
Daniel P. Berrange06ad5132016-04-27 11:04:56 +0100553 qemu_file_set_blocking(f, false);
Juan Quintelab673eab2019-06-12 11:44:19 +0200554 return 0;
Juan Quintelae595a012017-07-17 12:30:25 +0200555}
556
Juan Quintela36c2f8b2018-03-07 08:40:52 +0100557void migration_incoming_process(void)
Juan Quintelae595a012017-07-17 12:30:25 +0200558{
559 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL);
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200560 qemu_coroutine_enter(co);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200561}
562
Peter Xu884835f2018-06-27 21:22:45 +0800563/* Returns true if recovered from a paused migration, otherwise false */
564static bool postcopy_try_recover(QEMUFile *f)
Juan Quintelae595a012017-07-17 12:30:25 +0200565{
Peter Xud96c9e82018-05-02 18:47:26 +0800566 MigrationIncomingState *mis = migration_incoming_get_current();
567
568 if (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
569 /* Resumed from a paused postcopy migration */
570
571 mis->from_src_file = f;
572 /* Postcopy has standalone thread to do vm load */
573 qemu_file_set_blocking(f, true);
574
575 /* Re-configure the return path */
576 mis->to_src_file = qemu_file_get_return_path(f);
577
578 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
579 MIGRATION_STATUS_POSTCOPY_RECOVER);
580
581 /*
582 * Here, we only wake up the main loading thread (while the
583 * fault thread will still be waiting), so that we can receive
584 * commands from source now, and answer it if needed. The
585 * fault thread will be woken up afterwards until we are sure
586 * that source is ready to reply to page requests.
587 */
588 qemu_sem_post(&mis->postcopy_pause_sem_dst);
Peter Xu884835f2018-06-27 21:22:45 +0800589 return true;
Peter Xud96c9e82018-05-02 18:47:26 +0800590 }
Peter Xu884835f2018-06-27 21:22:45 +0800591
592 return false;
593}
594
Juan Quintelab673eab2019-06-12 11:44:19 +0200595void migration_fd_process_incoming(QEMUFile *f, Error **errp)
Peter Xu884835f2018-06-27 21:22:45 +0800596{
Juan Quintelab673eab2019-06-12 11:44:19 +0200597 Error *local_err = NULL;
598
Peter Xu884835f2018-06-27 21:22:45 +0800599 if (postcopy_try_recover(f)) {
600 return;
601 }
602
Juan Quintelab673eab2019-06-12 11:44:19 +0200603 if (migration_incoming_setup(f, &local_err)) {
604 if (local_err) {
605 error_propagate(errp, local_err);
606 }
607 return;
608 }
Peter Xu884835f2018-06-27 21:22:45 +0800609 migration_incoming_process();
Juan Quintelae595a012017-07-17 12:30:25 +0200610}
611
Fei Li49ed0d22019-01-13 22:08:46 +0800612void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp)
Juan Quintela4f0fae72017-07-24 12:42:02 +0200613{
614 MigrationIncomingState *mis = migration_incoming_get_current();
Juan Quintelab673eab2019-06-12 11:44:19 +0200615 Error *local_err = NULL;
Peter Xua429e7f2018-06-27 21:22:46 +0800616 bool start_migration;
Juan Quintela4f0fae72017-07-24 12:42:02 +0200617
618 if (!mis->from_src_file) {
Peter Xua429e7f2018-06-27 21:22:46 +0800619 /* The first connection (multifd may have multiple) */
Juan Quintela4f0fae72017-07-24 12:42:02 +0200620 QEMUFile *f = qemu_fopen_channel_input(ioc);
Peter Xua429e7f2018-06-27 21:22:46 +0800621
622 /* If it's a recovery, we're done */
Peter Xu884835f2018-06-27 21:22:45 +0800623 if (postcopy_try_recover(f)) {
624 return;
625 }
Peter Xua429e7f2018-06-27 21:22:46 +0800626
Juan Quintelab673eab2019-06-12 11:44:19 +0200627 if (migration_incoming_setup(f, &local_err)) {
628 if (local_err) {
629 error_propagate(errp, local_err);
630 }
631 return;
632 }
Peter Xua429e7f2018-06-27 21:22:46 +0800633
634 /*
635 * Common migration only needs one channel, so we can start
636 * right now. Multifd needs more than one channel, we wait.
637 */
638 start_migration = !migrate_use_multifd();
639 } else {
640 /* Multiple connections */
641 assert(migrate_use_multifd());
Fei Li49ed0d22019-01-13 22:08:46 +0800642 start_migration = multifd_recv_new_channel(ioc, &local_err);
643 if (local_err) {
644 error_propagate(errp, local_err);
645 return;
646 }
Juan Quintela4f0fae72017-07-24 12:42:02 +0200647 }
Peter Xu81e62052018-06-27 21:22:44 +0800648
Peter Xua429e7f2018-06-27 21:22:46 +0800649 if (start_migration) {
Peter Xu81e62052018-06-27 21:22:44 +0800650 migration_incoming_process();
651 }
Juan Quintela4f0fae72017-07-24 12:42:02 +0200652}
653
Juan Quintela428d8902017-07-24 13:06:25 +0200654/**
655 * @migration_has_all_channels: We have received all channels that we need
656 *
657 * Returns true when we have got connections to all the channels that
658 * we need for migration.
659 */
660bool migration_has_all_channels(void)
661{
Daniel P. Berrangéca273df2018-06-19 17:35:52 +0100662 MigrationIncomingState *mis = migration_incoming_get_current();
Juan Quintela62c1e0c2018-02-19 18:59:02 +0100663 bool all_channels;
664
665 all_channels = multifd_recv_all_channels_created();
666
Daniel P. Berrangéca273df2018-06-19 17:35:52 +0100667 return all_channels && mis->from_src_file != NULL;
Juan Quintela428d8902017-07-24 13:06:25 +0200668}
669
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000670/*
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000671 * Send a 'SHUT' message on the return channel with the given value
672 * to indicate that we've finished with the RP. Non-0 value indicates
673 * error.
674 */
675void migrate_send_rp_shut(MigrationIncomingState *mis,
676 uint32_t value)
677{
678 uint32_t buf;
679
680 buf = cpu_to_be32(value);
681 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
682}
683
684/*
685 * Send a 'PONG' message on the return channel with the given value
686 * (normally in response to a 'PING')
687 */
688void migrate_send_rp_pong(MigrationIncomingState *mis,
689 uint32_t value)
690{
691 uint32_t buf;
692
693 buf = cpu_to_be32(value);
694 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
695}
696
Peter Xua335deb2018-05-02 18:47:28 +0800697void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
698 char *block_name)
699{
700 char buf[512];
701 int len;
702 int64_t res;
703
704 /*
705 * First, we send the header part. It contains only the len of
706 * idstr, and the idstr itself.
707 */
708 len = strlen(block_name);
709 buf[0] = len;
710 memcpy(buf + 1, block_name, len);
711
712 if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
713 error_report("%s: MSG_RP_RECV_BITMAP only used for recovery",
714 __func__);
715 return;
716 }
717
718 migrate_send_rp_message(mis, MIG_RP_MSG_RECV_BITMAP, len + 1, buf);
719
720 /*
721 * Next, we dump the received bitmap to the stream.
722 *
723 * TODO: currently we are safe since we are the only one that is
724 * using the to_src_file handle (fault thread is still paused),
725 * and it's ok even not taking the mutex. However the best way is
726 * to take the lock before sending the message header, and release
727 * the lock after sending the bitmap.
728 */
729 qemu_mutex_lock(&mis->rp_mutex);
730 res = ramblock_recv_bitmap_send(mis->to_src_file, block_name);
731 qemu_mutex_unlock(&mis->rp_mutex);
732
733 trace_migrate_send_rp_recv_bitmap(block_name, res);
734}
735
Peter Xu13955b82018-05-02 18:47:30 +0800736void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value)
737{
738 uint32_t buf;
739
740 buf = cpu_to_be32(value);
741 migrate_send_rp_message(mis, MIG_RP_MSG_RESUME_ACK, sizeof(buf), &buf);
742}
743
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300744MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
745{
746 MigrationCapabilityStatusList *head = NULL;
747 MigrationCapabilityStatusList *caps;
748 MigrationState *s = migrate_get_current();
749 int i;
750
Michael Tokarev387eede2013-10-05 13:18:28 +0400751 caps = NULL; /* silence compiler warning */
Eric Blake7fb1cf12015-11-18 01:52:57 -0700752 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Dr. David Alan Gilberted1701c2017-05-15 15:05:29 +0100753#ifndef CONFIG_LIVE_BLOCK_MIGRATION
754 if (i == MIGRATION_CAPABILITY_BLOCK) {
755 continue;
756 }
757#endif
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300758 if (head == NULL) {
759 head = g_malloc0(sizeof(*caps));
760 caps = head;
761 } else {
762 caps->next = g_malloc0(sizeof(*caps));
763 caps = caps->next;
764 }
765 caps->value =
766 g_malloc(sizeof(*caps->value));
767 caps->value->capability = i;
768 caps->value->state = s->enabled_capabilities[i];
769 }
770
771 return head;
772}
773
Liang Li85de8322015-03-23 16:32:28 +0800774MigrationParameters *qmp_query_migrate_parameters(Error **errp)
775{
776 MigrationParameters *params;
777 MigrationState *s = migrate_get_current();
778
Markus Armbrustere87fae42017-07-18 12:57:38 +0200779 /* TODO use QAPI_CLONE() instead of duplicating it inline */
Liang Li85de8322015-03-23 16:32:28 +0800780 params = g_malloc0(sizeof(*params));
Eric Blakede63ab62016-09-08 22:14:15 -0500781 params->has_compress_level = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100782 params->compress_level = s->parameters.compress_level;
Eric Blakede63ab62016-09-08 22:14:15 -0500783 params->has_compress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100784 params->compress_threads = s->parameters.compress_threads;
Xiao Guangrong1d588722018-08-21 16:10:20 +0800785 params->has_compress_wait_thread = true;
786 params->compress_wait_thread = s->parameters.compress_wait_thread;
Eric Blakede63ab62016-09-08 22:14:15 -0500787 params->has_decompress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100788 params->decompress_threads = s->parameters.decompress_threads;
Keqian Zhudc14a472020-02-24 10:31:42 +0800789 params->has_throttle_trigger_threshold = true;
790 params->throttle_trigger_threshold = s->parameters.throttle_trigger_threshold;
Eric Blakede63ab62016-09-08 22:14:15 -0500791 params->has_cpu_throttle_initial = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100792 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
Eric Blakede63ab62016-09-08 22:14:15 -0500793 params->has_cpu_throttle_increment = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100794 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
Keqian Zhucbbf8182020-04-13 18:15:08 +0800795 params->has_cpu_throttle_tailslow = true;
796 params->cpu_throttle_tailslow = s->parameters.cpu_throttle_tailslow;
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200797 params->has_tls_creds = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100798 params->tls_creds = g_strdup(s->parameters.tls_creds);
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200799 params->has_tls_hostname = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100800 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
Daniel P. Berranged2f1d292019-02-27 14:53:24 +0000801 params->has_tls_authz = true;
Mao Zhongyi7cd75cb2020-03-25 09:49:30 +0800802 params->tls_authz = g_strdup(s->parameters.tls_authz ?
803 s->parameters.tls_authz : "");
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530804 params->has_max_bandwidth = true;
805 params->max_bandwidth = s->parameters.max_bandwidth;
806 params->has_downtime_limit = true;
807 params->downtime_limit = s->parameters.downtime_limit;
zhanghailiangfe39a4d2016-11-02 15:42:09 +0800808 params->has_x_checkpoint_delay = true;
zhanghailiang68b53592016-10-27 14:43:01 +0800809 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
Juan Quintela2833c592017-04-05 18:32:37 +0200810 params->has_block_incremental = true;
811 params->block_incremental = s->parameters.block_incremental;
Juan Quintelacbfd6c92019-02-06 13:54:06 +0100812 params->has_multifd_channels = true;
813 params->multifd_channels = s->parameters.multifd_channels;
Juan Quintela96eef042019-01-16 10:35:55 +0100814 params->has_multifd_compression = true;
815 params->multifd_compression = s->parameters.multifd_compression;
Juan Quintela9004db42020-01-23 17:08:52 +0100816 params->has_multifd_zlib_level = true;
817 params->multifd_zlib_level = s->parameters.multifd_zlib_level;
Juan Quintela6a9ad152020-01-23 17:41:36 +0100818 params->has_multifd_zstd_level = true;
819 params->multifd_zstd_level = s->parameters.multifd_zstd_level;
Juan Quintela73af8dd2017-10-05 21:30:10 +0200820 params->has_xbzrle_cache_size = true;
821 params->xbzrle_cache_size = s->parameters.xbzrle_cache_size;
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +0100822 params->has_max_postcopy_bandwidth = true;
823 params->max_postcopy_bandwidth = s->parameters.max_postcopy_bandwidth;
Li Qiang4cbc9c72018-08-01 06:00:20 -0700824 params->has_max_cpu_throttle = true;
825 params->max_cpu_throttle = s->parameters.max_cpu_throttle;
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +0000826 params->has_announce_initial = true;
827 params->announce_initial = s->parameters.announce_initial;
828 params->has_announce_max = true;
829 params->announce_max = s->parameters.announce_max;
830 params->has_announce_rounds = true;
831 params->announce_rounds = s->parameters.announce_rounds;
832 params->has_announce_step = true;
833 params->announce_step = s->parameters.announce_step;
Liang Li85de8322015-03-23 16:32:28 +0800834
835 return params;
836}
837
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +0000838AnnounceParameters *migrate_announce_params(void)
839{
840 static AnnounceParameters ap;
841
842 MigrationState *s = migrate_get_current();
843
844 ap.initial = s->parameters.announce_initial;
845 ap.max = s->parameters.announce_max;
846 ap.rounds = s->parameters.announce_rounds;
847 ap.step = s->parameters.announce_step;
848
849 return &ap;
850}
851
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000852/*
853 * Return true if we're already in the middle of a migration
854 * (i.e. any of the active or setup states)
855 */
Jia Lina3d63da12018-10-26 16:36:20 +0800856bool migration_is_setup_or_active(int state)
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000857{
858 switch (state) {
859 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000860 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Peter Xua688d2c2018-05-02 18:47:18 +0800861 case MIGRATION_STATUS_POSTCOPY_PAUSED:
Peter Xu135b87b2018-05-02 18:47:25 +0800862 case MIGRATION_STATUS_POSTCOPY_RECOVER:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000863 case MIGRATION_STATUS_SETUP:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +0100864 case MIGRATION_STATUS_PRE_SWITCHOVER:
865 case MIGRATION_STATUS_DEVICE:
Jens Freimannc7e0acd2019-10-29 12:49:02 +0100866 case MIGRATION_STATUS_WAIT_UNPLUG:
zhanghailiang19dd4082020-02-24 14:54:12 +0800867 case MIGRATION_STATUS_COLO:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000868 return true;
869
870 default:
871 return false;
872
873 }
874}
875
Juan Quintela392d87e2020-01-21 15:39:23 +0100876bool migration_is_running(int state)
877{
878 switch (state) {
879 case MIGRATION_STATUS_ACTIVE:
880 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
881 case MIGRATION_STATUS_POSTCOPY_PAUSED:
882 case MIGRATION_STATUS_POSTCOPY_RECOVER:
883 case MIGRATION_STATUS_SETUP:
884 case MIGRATION_STATUS_PRE_SWITCHOVER:
885 case MIGRATION_STATUS_DEVICE:
886 case MIGRATION_STATUS_WAIT_UNPLUG:
887 case MIGRATION_STATUS_CANCELLING:
Juan Quintela392d87e2020-01-21 15:39:23 +0100888 return true;
889
890 default:
891 return false;
892
893 }
894}
895
Wei Yang640dfb12019-07-16 08:54:11 +0800896static void populate_time_info(MigrationInfo *info, MigrationState *s)
897{
898 info->has_status = true;
899 info->has_setup_time = true;
900 info->setup_time = s->setup_time;
901 if (s->state == MIGRATION_STATUS_COMPLETED) {
902 info->has_total_time = true;
903 info->total_time = s->total_time;
904 info->has_downtime = true;
905 info->downtime = s->downtime;
906 } else {
907 info->has_total_time = true;
908 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
909 s->start_time;
910 info->has_expected_downtime = true;
911 info->expected_downtime = s->expected_downtime;
912 }
913}
914
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100915static void populate_ram_info(MigrationInfo *info, MigrationState *s)
916{
917 info->has_ram = true;
918 info->ram = g_malloc0(sizeof(*info->ram));
Juan Quintela93604472017-06-06 19:49:03 +0200919 info->ram->transferred = ram_counters.transferred;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100920 info->ram->total = ram_bytes_total();
Juan Quintela93604472017-06-06 19:49:03 +0200921 info->ram->duplicate = ram_counters.duplicate;
Juan Quintelabedf53c2017-03-13 20:35:54 +0100922 /* legacy value. It is not used anymore */
923 info->ram->skipped = 0;
Juan Quintela93604472017-06-06 19:49:03 +0200924 info->ram->normal = ram_counters.normal;
925 info->ram->normal_bytes = ram_counters.normal *
Juan Quintela20afaed2017-03-21 09:09:14 +0100926 qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100927 info->ram->mbps = s->mbps;
Juan Quintela93604472017-06-06 19:49:03 +0200928 info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
929 info->ram->postcopy_requests = ram_counters.postcopy_requests;
Chao Fan030ce1f2017-03-21 10:22:43 +0800930 info->ram->page_size = qemu_target_page_size();
Juan Quintelaa61c45b2018-06-26 15:20:11 +0200931 info->ram->multifd_bytes = ram_counters.multifd_bytes;
Xiao Guangrongaecbfe92019-01-11 14:37:30 +0800932 info->ram->pages_per_second = s->pages_per_second;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100933
Juan Quintela114f5ae2017-05-04 10:09:21 +0200934 if (migrate_use_xbzrle()) {
935 info->has_xbzrle_cache = true;
936 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
937 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
Juan Quintela93604472017-06-06 19:49:03 +0200938 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
939 info->xbzrle_cache->pages = xbzrle_counters.pages;
940 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
941 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
Wei Wange460a4b2020-04-30 08:59:35 +0800942 info->xbzrle_cache->encoding_rate = xbzrle_counters.encoding_rate;
Juan Quintela93604472017-06-06 19:49:03 +0200943 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
Juan Quintela114f5ae2017-05-04 10:09:21 +0200944 }
945
Xiao Guangrong76e03002018-09-06 15:01:00 +0800946 if (migrate_use_compression()) {
947 info->has_compression = true;
948 info->compression = g_malloc0(sizeof(*info->compression));
949 info->compression->pages = compression_counters.pages;
950 info->compression->busy = compression_counters.busy;
951 info->compression->busy_rate = compression_counters.busy_rate;
952 info->compression->compressed_size =
953 compression_counters.compressed_size;
954 info->compression->compression_rate =
955 compression_counters.compression_rate;
956 }
957
Juan Quintela338182c2017-05-03 13:16:38 +0200958 if (cpu_throttle_active()) {
959 info->has_cpu_throttle_percentage = true;
960 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
961 }
962
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100963 if (s->state != MIGRATION_STATUS_COMPLETED) {
964 info->ram->remaining = ram_bytes_remaining();
Juan Quintela93604472017-06-06 19:49:03 +0200965 info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100966 }
967}
968
Juan Quintela930ac042017-05-04 10:21:46 +0200969static void populate_disk_info(MigrationInfo *info)
970{
971 if (blk_mig_active()) {
972 info->has_disk = true;
973 info->disk = g_malloc0(sizeof(*info->disk));
974 info->disk->transferred = blk_mig_bytes_transferred();
975 info->disk->remaining = blk_mig_bytes_remaining();
976 info->disk->total = blk_mig_bytes_total();
977 }
978}
979
Alexey Perevalov65ace062018-03-22 21:17:27 +0300980static void fill_source_migration_info(MigrationInfo *info)
aliguori5bb79102008-10-13 03:12:02 +0000981{
Juan Quintela17549e82011-10-05 13:50:43 +0200982 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000983
Juan Quintela17549e82011-10-05 13:50:43 +0200984 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800985 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200986 /* no migration has happened ever */
Alexey Perevalov65ace062018-03-22 21:17:27 +0300987 /* do not overwrite destination migration status */
988 return;
Juan Quintela17549e82011-10-05 13:50:43 +0200989 break;
zhanghailiang31194732015-03-13 16:08:38 +0800990 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400991 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400992 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400993 break;
zhanghailiang31194732015-03-13 16:08:38 +0800994 case MIGRATION_STATUS_ACTIVE:
995 case MIGRATION_STATUS_CANCELLING:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000996 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +0100997 case MIGRATION_STATUS_PRE_SWITCHOVER:
998 case MIGRATION_STATUS_DEVICE:
Peter Xua688d2c2018-05-02 18:47:18 +0800999 case MIGRATION_STATUS_POSTCOPY_PAUSED:
Peter Xu135b87b2018-05-02 18:47:25 +08001000 case MIGRATION_STATUS_POSTCOPY_RECOVER:
Wei Yang640dfb12019-07-16 08:54:11 +08001001 /* TODO add some postcopy stats */
1002 populate_time_info(info, s);
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +01001003 populate_ram_info(info, s);
Juan Quintela930ac042017-05-04 10:21:46 +02001004 populate_disk_info(info);
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001005 break;
zhanghailiang0b827d52016-10-27 14:42:54 +08001006 case MIGRATION_STATUS_COLO:
1007 info->has_status = true;
1008 /* TODO: display COLO specific information (checkpoint info etc.) */
1009 break;
zhanghailiang31194732015-03-13 16:08:38 +08001010 case MIGRATION_STATUS_COMPLETED:
Wei Yang640dfb12019-07-16 08:54:11 +08001011 populate_time_info(info, s);
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +01001012 populate_ram_info(info, s);
Juan Quintela17549e82011-10-05 13:50:43 +02001013 break;
zhanghailiang31194732015-03-13 16:08:38 +08001014 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -03001015 info->has_status = true;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001016 if (s->error) {
1017 info->has_error_desc = true;
1018 info->error_desc = g_strdup(error_get_pretty(s->error));
1019 }
Juan Quintela17549e82011-10-05 13:50:43 +02001020 break;
zhanghailiang31194732015-03-13 16:08:38 +08001021 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -03001022 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +02001023 break;
Jens Freimannc7e0acd2019-10-29 12:49:02 +01001024 case MIGRATION_STATUS_WAIT_UNPLUG:
1025 info->has_status = true;
1026 break;
aliguori5bb79102008-10-13 03:12:02 +00001027 }
zhanghailiangcde63fb2015-03-13 16:08:41 +08001028 info->status = s->state;
aliguori5bb79102008-10-13 03:12:02 +00001029}
1030
Peter Xu4a842142017-07-18 11:39:08 +08001031/**
1032 * @migration_caps_check - check capability validity
1033 *
1034 * @cap_list: old capability list, array of bool
1035 * @params: new capabilities to be applied soon
1036 * @errp: set *errp if the check failed, with reason
1037 *
1038 * Returns true if check passed, otherwise false.
1039 */
1040static bool migrate_caps_check(bool *cap_list,
1041 MigrationCapabilityStatusList *params,
1042 Error **errp)
Orit Wasserman00458432012-08-06 21:42:48 +03001043{
Orit Wasserman00458432012-08-06 21:42:48 +03001044 MigrationCapabilityStatusList *cap;
Peter Xu4a842142017-07-18 11:39:08 +08001045 bool old_postcopy_cap;
Alexey Perevalovd7651f12017-09-19 19:47:56 +03001046 MigrationIncomingState *mis = migration_incoming_get_current();
Orit Wasserman00458432012-08-06 21:42:48 +03001047
Peter Xu4a842142017-07-18 11:39:08 +08001048 old_postcopy_cap = cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Orit Wasserman00458432012-08-06 21:42:48 +03001049
1050 for (cap = params; cap; cap = cap->next) {
Peter Xu4a842142017-07-18 11:39:08 +08001051 cap_list[cap->value->capability] = cap->value->state;
Orit Wasserman00458432012-08-06 21:42:48 +03001052 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001053
Peter Xu4a842142017-07-18 11:39:08 +08001054#ifndef CONFIG_LIVE_BLOCK_MIGRATION
1055 if (cap_list[MIGRATION_CAPABILITY_BLOCK]) {
1056 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
1057 "block migration");
1058 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
1059 return false;
1060 }
1061#endif
1062
Zhang Chen7e934f52018-11-15 03:09:12 +08001063#ifndef CONFIG_REPLICATION
1064 if (cap_list[MIGRATION_CAPABILITY_X_COLO]) {
1065 error_setg(errp, "QEMU compiled without replication module"
1066 " can't enable COLO");
1067 error_append_hint(errp, "Please enable replication before COLO.\n");
1068 return false;
1069 }
1070#endif
1071
Peter Xu4a842142017-07-18 11:39:08 +08001072 if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +01001073 /* This check is reasonably expensive, so only when it's being
1074 * set the first time, also it's only the destination that needs
1075 * special support.
1076 */
1077 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
Alexey Perevalovd7651f12017-09-19 19:47:56 +03001078 !postcopy_ram_supported_by_host(mis)) {
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +01001079 /* postcopy_ram_supported_by_host will have emitted a more
1080 * detailed message
1081 */
Peter Xu4a842142017-07-18 11:39:08 +08001082 error_setg(errp, "Postcopy is not supported");
1083 return false;
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +01001084 }
Yury Kotov18269062019-02-15 20:45:45 +03001085
1086 if (cap_list[MIGRATION_CAPABILITY_X_IGNORE_SHARED]) {
1087 error_setg(errp, "Postcopy is not compatible with ignore-shared");
1088 return false;
1089 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001090 }
Peter Xu4a842142017-07-18 11:39:08 +08001091
1092 return true;
1093}
1094
Alexey Perevalov65ace062018-03-22 21:17:27 +03001095static void fill_destination_migration_info(MigrationInfo *info)
1096{
1097 MigrationIncomingState *mis = migration_incoming_get_current();
1098
Juan Quintela9aca82b2019-02-27 11:51:27 +01001099 if (mis->socket_address_list) {
1100 info->has_socket_address = true;
1101 info->socket_address =
1102 QAPI_CLONE(SocketAddressList, mis->socket_address_list);
1103 }
1104
Alexey Perevalov65ace062018-03-22 21:17:27 +03001105 switch (mis->state) {
1106 case MIGRATION_STATUS_NONE:
1107 return;
1108 break;
1109 case MIGRATION_STATUS_SETUP:
1110 case MIGRATION_STATUS_CANCELLING:
1111 case MIGRATION_STATUS_CANCELLED:
1112 case MIGRATION_STATUS_ACTIVE:
1113 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Peter Xu3c9928d2018-07-10 17:18:56 +08001114 case MIGRATION_STATUS_POSTCOPY_PAUSED:
1115 case MIGRATION_STATUS_POSTCOPY_RECOVER:
Alexey Perevalov65ace062018-03-22 21:17:27 +03001116 case MIGRATION_STATUS_FAILED:
1117 case MIGRATION_STATUS_COLO:
1118 info->has_status = true;
1119 break;
1120 case MIGRATION_STATUS_COMPLETED:
1121 info->has_status = true;
1122 fill_destination_postcopy_migration_info(info);
1123 break;
1124 }
1125 info->status = mis->state;
1126}
1127
1128MigrationInfo *qmp_query_migrate(Error **errp)
1129{
1130 MigrationInfo *info = g_malloc0(sizeof(*info));
1131
1132 fill_destination_migration_info(info);
1133 fill_source_migration_info(info);
1134
1135 return info;
1136}
1137
Peter Xu4a842142017-07-18 11:39:08 +08001138void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
1139 Error **errp)
1140{
1141 MigrationState *s = migrate_get_current();
1142 MigrationCapabilityStatusList *cap;
Peter Xudd0ee302018-03-05 17:49:38 +08001143 bool cap_list[MIGRATION_CAPABILITY__MAX];
Peter Xu4a842142017-07-18 11:39:08 +08001144
Juan Quintela392d87e2020-01-21 15:39:23 +01001145 if (migration_is_running(s->state)) {
Peter Xu4a842142017-07-18 11:39:08 +08001146 error_setg(errp, QERR_MIGRATION_ACTIVE);
1147 return;
1148 }
1149
Peter Xudd0ee302018-03-05 17:49:38 +08001150 memcpy(cap_list, s->enabled_capabilities, sizeof(cap_list));
1151 if (!migrate_caps_check(cap_list, params, errp)) {
Peter Xu4a842142017-07-18 11:39:08 +08001152 return;
1153 }
1154
1155 for (cap = params; cap; cap = cap->next) {
1156 s->enabled_capabilities[cap->value->capability] = cap->value->state;
1157 }
Orit Wasserman00458432012-08-06 21:42:48 +03001158}
1159
Peter Xu16d063b2017-07-18 11:39:04 +08001160/*
1161 * Check whether the parameters are valid. Error will be put into errp
1162 * (if provided). Return true if valid, otherwise false.
1163 */
1164static bool migrate_params_check(MigrationParameters *params, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +08001165{
Eric Blake7f375e02016-09-08 22:14:16 -05001166 if (params->has_compress_level &&
Juan Quintela741d4082017-12-01 13:08:38 +01001167 (params->compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001168 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
1169 "is invalid, it should be in the range of 0 to 9");
Peter Xu16d063b2017-07-18 11:39:04 +08001170 return false;
Liang Li85de8322015-03-23 16:32:28 +08001171 }
Peter Xu16d063b2017-07-18 11:39:04 +08001172
Juan Quintela741d4082017-12-01 13:08:38 +01001173 if (params->has_compress_threads && (params->compress_threads < 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001174 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1175 "compress_threads",
1176 "is invalid, it should be in the range of 1 to 255");
Peter Xu16d063b2017-07-18 11:39:04 +08001177 return false;
Liang Li85de8322015-03-23 16:32:28 +08001178 }
Peter Xu16d063b2017-07-18 11:39:04 +08001179
Juan Quintela741d4082017-12-01 13:08:38 +01001180 if (params->has_decompress_threads && (params->decompress_threads < 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001181 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1182 "decompress_threads",
1183 "is invalid, it should be in the range of 1 to 255");
Peter Xu16d063b2017-07-18 11:39:04 +08001184 return false;
Liang Li85de8322015-03-23 16:32:28 +08001185 }
Peter Xu16d063b2017-07-18 11:39:04 +08001186
Keqian Zhudc14a472020-02-24 10:31:42 +08001187 if (params->has_throttle_trigger_threshold &&
1188 (params->throttle_trigger_threshold < 1 ||
1189 params->throttle_trigger_threshold > 100)) {
1190 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1191 "throttle_trigger_threshold",
1192 "an integer in the range of 1 to 100");
1193 return false;
1194 }
1195
Eric Blake7f375e02016-09-08 22:14:16 -05001196 if (params->has_cpu_throttle_initial &&
1197 (params->cpu_throttle_initial < 1 ||
1198 params->cpu_throttle_initial > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -04001199 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -04001200 "cpu_throttle_initial",
Jason J. Herne1626fee2015-09-08 13:12:34 -04001201 "an integer in the range of 1 to 99");
Peter Xu16d063b2017-07-18 11:39:04 +08001202 return false;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001203 }
Peter Xu16d063b2017-07-18 11:39:04 +08001204
Eric Blake7f375e02016-09-08 22:14:16 -05001205 if (params->has_cpu_throttle_increment &&
1206 (params->cpu_throttle_increment < 1 ||
1207 params->cpu_throttle_increment > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -04001208 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -04001209 "cpu_throttle_increment",
Jason J. Herne1626fee2015-09-08 13:12:34 -04001210 "an integer in the range of 1 to 99");
Peter Xu16d063b2017-07-18 11:39:04 +08001211 return false;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001212 }
Peter Xu16d063b2017-07-18 11:39:04 +08001213
Juan Quintela741d4082017-12-01 13:08:38 +01001214 if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) {
Mao Zhongyi2ee30cf2020-03-31 16:22:05 +08001215 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1216 "max_bandwidth",
1217 "an integer in the range of 0 to "stringify(SIZE_MAX)
1218 " bytes/second");
Peter Xu16d063b2017-07-18 11:39:04 +08001219 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301220 }
Peter Xu16d063b2017-07-18 11:39:04 +08001221
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301222 if (params->has_downtime_limit &&
Juan Quintela741d4082017-12-01 13:08:38 +01001223 (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
Mao Zhongyi2ee30cf2020-03-31 16:22:05 +08001224 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1225 "downtime_limit",
1226 "an integer in the range of 0 to "
Mao Zhongyi7ac55292020-03-31 16:22:07 +08001227 stringify(MAX_MIGRATE_DOWNTIME)" ms");
Peter Xu16d063b2017-07-18 11:39:04 +08001228 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301229 }
Peter Xu16d063b2017-07-18 11:39:04 +08001230
Juan Quintela741d4082017-12-01 13:08:38 +01001231 /* x_checkpoint_delay is now always positive */
1232
Juan Quintelacbfd6c92019-02-06 13:54:06 +01001233 if (params->has_multifd_channels && (params->multifd_channels < 1)) {
Juan Quintela4075fb12016-01-15 08:56:17 +01001234 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1235 "multifd_channels",
1236 "is invalid, it should be in the range of 1 to 255");
1237 return false;
1238 }
Peter Xu16d063b2017-07-18 11:39:04 +08001239
Juan Quintela9004db42020-01-23 17:08:52 +01001240 if (params->has_multifd_zlib_level &&
1241 (params->multifd_zlib_level > 9)) {
1242 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zlib_level",
1243 "is invalid, it should be in the range of 0 to 9");
1244 return false;
1245 }
1246
Juan Quintela6a9ad152020-01-23 17:41:36 +01001247 if (params->has_multifd_zstd_level &&
1248 (params->multifd_zstd_level > 20)) {
1249 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zstd_level",
1250 "is invalid, it should be in the range of 0 to 20");
1251 return false;
1252 }
1253
Juan Quintela73af8dd2017-10-05 21:30:10 +02001254 if (params->has_xbzrle_cache_size &&
1255 (params->xbzrle_cache_size < qemu_target_page_size() ||
1256 !is_power_of_2(params->xbzrle_cache_size))) {
1257 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1258 "xbzrle_cache_size",
1259 "is invalid, it should be bigger than target page size"
Mao Zhongyi06b1c6f2020-03-20 22:32:16 +08001260 " and a power of 2");
Juan Quintela73af8dd2017-10-05 21:30:10 +02001261 return false;
1262 }
1263
Li Qiang4cbc9c72018-08-01 06:00:20 -07001264 if (params->has_max_cpu_throttle &&
1265 (params->max_cpu_throttle < params->cpu_throttle_initial ||
1266 params->max_cpu_throttle > 99)) {
1267 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1268 "max_cpu_throttle",
1269 "an integer in the range of cpu_throttle_initial to 99");
1270 return false;
1271 }
1272
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00001273 if (params->has_announce_initial &&
1274 params->announce_initial > 100000) {
1275 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1276 "announce_initial",
1277 "is invalid, it must be less than 100000 ms");
1278 return false;
1279 }
1280 if (params->has_announce_max &&
1281 params->announce_max > 100000) {
1282 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1283 "announce_max",
1284 "is invalid, it must be less than 100000 ms");
1285 return false;
1286 }
1287 if (params->has_announce_rounds &&
1288 params->announce_rounds > 1000) {
1289 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1290 "announce_rounds",
1291 "is invalid, it must be in the range of 0 to 1000");
1292 return false;
1293 }
1294 if (params->has_announce_step &&
1295 (params->announce_step < 1 ||
1296 params->announce_step > 10000)) {
1297 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1298 "announce_step",
1299 "is invalid, it must be in the range of 1 to 10000 ms");
1300 return false;
1301 }
Peter Xu16d063b2017-07-18 11:39:04 +08001302 return true;
1303}
1304
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001305static void migrate_params_test_apply(MigrateSetParameters *params,
1306 MigrationParameters *dest)
1307{
1308 *dest = migrate_get_current()->parameters;
1309
1310 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1311
1312 if (params->has_compress_level) {
1313 dest->compress_level = params->compress_level;
1314 }
1315
1316 if (params->has_compress_threads) {
1317 dest->compress_threads = params->compress_threads;
1318 }
1319
Xiao Guangrong1d588722018-08-21 16:10:20 +08001320 if (params->has_compress_wait_thread) {
1321 dest->compress_wait_thread = params->compress_wait_thread;
1322 }
1323
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001324 if (params->has_decompress_threads) {
1325 dest->decompress_threads = params->decompress_threads;
1326 }
1327
Keqian Zhudc14a472020-02-24 10:31:42 +08001328 if (params->has_throttle_trigger_threshold) {
1329 dest->throttle_trigger_threshold = params->throttle_trigger_threshold;
1330 }
1331
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001332 if (params->has_cpu_throttle_initial) {
1333 dest->cpu_throttle_initial = params->cpu_throttle_initial;
1334 }
1335
1336 if (params->has_cpu_throttle_increment) {
1337 dest->cpu_throttle_increment = params->cpu_throttle_increment;
1338 }
1339
Keqian Zhucbbf8182020-04-13 18:15:08 +08001340 if (params->has_cpu_throttle_tailslow) {
1341 dest->cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1342 }
1343
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001344 if (params->has_tls_creds) {
Markus Armbruster01fa5592017-07-18 14:42:04 +02001345 assert(params->tls_creds->type == QTYPE_QSTRING);
1346 dest->tls_creds = g_strdup(params->tls_creds->u.s);
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001347 }
1348
1349 if (params->has_tls_hostname) {
Markus Armbruster01fa5592017-07-18 14:42:04 +02001350 assert(params->tls_hostname->type == QTYPE_QSTRING);
1351 dest->tls_hostname = g_strdup(params->tls_hostname->u.s);
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001352 }
1353
1354 if (params->has_max_bandwidth) {
1355 dest->max_bandwidth = params->max_bandwidth;
1356 }
1357
1358 if (params->has_downtime_limit) {
1359 dest->downtime_limit = params->downtime_limit;
1360 }
1361
1362 if (params->has_x_checkpoint_delay) {
1363 dest->x_checkpoint_delay = params->x_checkpoint_delay;
1364 }
1365
1366 if (params->has_block_incremental) {
1367 dest->block_incremental = params->block_incremental;
1368 }
Juan Quintelacbfd6c92019-02-06 13:54:06 +01001369 if (params->has_multifd_channels) {
1370 dest->multifd_channels = params->multifd_channels;
Juan Quintela5e7577a2017-10-09 18:07:56 +02001371 }
Juan Quintela96eef042019-01-16 10:35:55 +01001372 if (params->has_multifd_compression) {
1373 dest->multifd_compression = params->multifd_compression;
1374 }
Juan Quintela73af8dd2017-10-05 21:30:10 +02001375 if (params->has_xbzrle_cache_size) {
1376 dest->xbzrle_cache_size = params->xbzrle_cache_size;
1377 }
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01001378 if (params->has_max_postcopy_bandwidth) {
1379 dest->max_postcopy_bandwidth = params->max_postcopy_bandwidth;
1380 }
Li Qiang4cbc9c72018-08-01 06:00:20 -07001381 if (params->has_max_cpu_throttle) {
1382 dest->max_cpu_throttle = params->max_cpu_throttle;
1383 }
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00001384 if (params->has_announce_initial) {
1385 dest->announce_initial = params->announce_initial;
1386 }
1387 if (params->has_announce_max) {
1388 dest->announce_max = params->announce_max;
1389 }
1390 if (params->has_announce_rounds) {
1391 dest->announce_rounds = params->announce_rounds;
1392 }
1393 if (params->has_announce_step) {
1394 dest->announce_step = params->announce_step;
1395 }
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001396}
1397
Juan Quintela73af8dd2017-10-05 21:30:10 +02001398static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
Peter Xu16d063b2017-07-18 11:39:04 +08001399{
1400 MigrationState *s = migrate_get_current();
1401
Markus Armbrustere87fae42017-07-18 12:57:38 +02001402 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1403
Eric Blake7f375e02016-09-08 22:14:16 -05001404 if (params->has_compress_level) {
1405 s->parameters.compress_level = params->compress_level;
Liang Li85de8322015-03-23 16:32:28 +08001406 }
Peter Xu476c72a2017-07-18 11:39:05 +08001407
Eric Blake7f375e02016-09-08 22:14:16 -05001408 if (params->has_compress_threads) {
1409 s->parameters.compress_threads = params->compress_threads;
Liang Li85de8322015-03-23 16:32:28 +08001410 }
Peter Xu476c72a2017-07-18 11:39:05 +08001411
Xiao Guangrong1d588722018-08-21 16:10:20 +08001412 if (params->has_compress_wait_thread) {
1413 s->parameters.compress_wait_thread = params->compress_wait_thread;
1414 }
1415
Eric Blake7f375e02016-09-08 22:14:16 -05001416 if (params->has_decompress_threads) {
1417 s->parameters.decompress_threads = params->decompress_threads;
Liang Li85de8322015-03-23 16:32:28 +08001418 }
Peter Xu476c72a2017-07-18 11:39:05 +08001419
Keqian Zhudc14a472020-02-24 10:31:42 +08001420 if (params->has_throttle_trigger_threshold) {
1421 s->parameters.throttle_trigger_threshold = params->throttle_trigger_threshold;
1422 }
1423
Eric Blake7f375e02016-09-08 22:14:16 -05001424 if (params->has_cpu_throttle_initial) {
1425 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001426 }
Peter Xu476c72a2017-07-18 11:39:05 +08001427
Eric Blake7f375e02016-09-08 22:14:16 -05001428 if (params->has_cpu_throttle_increment) {
1429 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001430 }
Peter Xu476c72a2017-07-18 11:39:05 +08001431
Keqian Zhucbbf8182020-04-13 18:15:08 +08001432 if (params->has_cpu_throttle_tailslow) {
1433 s->parameters.cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1434 }
1435
Eric Blake7f375e02016-09-08 22:14:16 -05001436 if (params->has_tls_creds) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001437 g_free(s->parameters.tls_creds);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001438 assert(params->tls_creds->type == QTYPE_QSTRING);
1439 s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001440 }
Peter Xu476c72a2017-07-18 11:39:05 +08001441
Eric Blake7f375e02016-09-08 22:14:16 -05001442 if (params->has_tls_hostname) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001443 g_free(s->parameters.tls_hostname);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001444 assert(params->tls_hostname->type == QTYPE_QSTRING);
1445 s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001446 }
Peter Xu476c72a2017-07-18 11:39:05 +08001447
Daniel P. Berranged2f1d292019-02-27 14:53:24 +00001448 if (params->has_tls_authz) {
1449 g_free(s->parameters.tls_authz);
1450 assert(params->tls_authz->type == QTYPE_QSTRING);
1451 s->parameters.tls_authz = g_strdup(params->tls_authz->u.s);
1452 }
1453
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301454 if (params->has_max_bandwidth) {
1455 s->parameters.max_bandwidth = params->max_bandwidth;
Dr. David Alan Gilbertc38c1c12019-03-08 10:12:10 +00001456 if (s->to_dst_file && !migration_in_postcopy()) {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301457 qemu_file_set_rate_limit(s->to_dst_file,
1458 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
1459 }
1460 }
Peter Xu476c72a2017-07-18 11:39:05 +08001461
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301462 if (params->has_downtime_limit) {
1463 s->parameters.downtime_limit = params->downtime_limit;
1464 }
zhanghailiang68b53592016-10-27 14:43:01 +08001465
1466 if (params->has_x_checkpoint_delay) {
1467 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
zhanghailiang479125d2017-01-17 20:57:42 +08001468 if (migration_in_colo_state()) {
1469 colo_checkpoint_notify(s);
1470 }
zhanghailiang68b53592016-10-27 14:43:01 +08001471 }
Peter Xu476c72a2017-07-18 11:39:05 +08001472
Juan Quintela2833c592017-04-05 18:32:37 +02001473 if (params->has_block_incremental) {
1474 s->parameters.block_incremental = params->block_incremental;
1475 }
Juan Quintelacbfd6c92019-02-06 13:54:06 +01001476 if (params->has_multifd_channels) {
1477 s->parameters.multifd_channels = params->multifd_channels;
Juan Quintela4075fb12016-01-15 08:56:17 +01001478 }
Juan Quintela96eef042019-01-16 10:35:55 +01001479 if (params->has_multifd_compression) {
1480 s->parameters.multifd_compression = params->multifd_compression;
1481 }
Juan Quintela73af8dd2017-10-05 21:30:10 +02001482 if (params->has_xbzrle_cache_size) {
1483 s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
1484 xbzrle_cache_resize(params->xbzrle_cache_size, errp);
1485 }
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01001486 if (params->has_max_postcopy_bandwidth) {
1487 s->parameters.max_postcopy_bandwidth = params->max_postcopy_bandwidth;
Dr. David Alan Gilbertc38c1c12019-03-08 10:12:10 +00001488 if (s->to_dst_file && migration_in_postcopy()) {
1489 qemu_file_set_rate_limit(s->to_dst_file,
1490 s->parameters.max_postcopy_bandwidth / XFER_LIMIT_RATIO);
1491 }
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01001492 }
Li Qiang4cbc9c72018-08-01 06:00:20 -07001493 if (params->has_max_cpu_throttle) {
1494 s->parameters.max_cpu_throttle = params->max_cpu_throttle;
1495 }
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00001496 if (params->has_announce_initial) {
1497 s->parameters.announce_initial = params->announce_initial;
1498 }
1499 if (params->has_announce_max) {
1500 s->parameters.announce_max = params->announce_max;
1501 }
1502 if (params->has_announce_rounds) {
1503 s->parameters.announce_rounds = params->announce_rounds;
1504 }
1505 if (params->has_announce_step) {
1506 s->parameters.announce_step = params->announce_step;
1507 }
Liang Li85de8322015-03-23 16:32:28 +08001508}
1509
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001510void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
Peter Xu476c72a2017-07-18 11:39:05 +08001511{
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001512 MigrationParameters tmp;
1513
Markus Armbruster01fa5592017-07-18 14:42:04 +02001514 /* TODO Rewrite "" to null instead */
1515 if (params->has_tls_creds
1516 && params->tls_creds->type == QTYPE_QNULL) {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001517 qobject_unref(params->tls_creds->u.n);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001518 params->tls_creds->type = QTYPE_QSTRING;
1519 params->tls_creds->u.s = strdup("");
1520 }
1521 /* TODO Rewrite "" to null instead */
1522 if (params->has_tls_hostname
1523 && params->tls_hostname->type == QTYPE_QNULL) {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001524 qobject_unref(params->tls_hostname->u.n);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001525 params->tls_hostname->type = QTYPE_QSTRING;
1526 params->tls_hostname->u.s = strdup("");
1527 }
1528
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001529 migrate_params_test_apply(params, &tmp);
1530
1531 if (!migrate_params_check(&tmp, errp)) {
Peter Xu476c72a2017-07-18 11:39:05 +08001532 /* Invalid parameter */
1533 return;
1534 }
1535
Juan Quintela73af8dd2017-10-05 21:30:10 +02001536 migrate_params_apply(params, errp);
Peter Xu476c72a2017-07-18 11:39:05 +08001537}
1538
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001539
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001540void qmp_migrate_start_postcopy(Error **errp)
1541{
1542 MigrationState *s = migrate_get_current();
1543
Vladimir Sementsov-Ogievskiy16b0fd32018-03-13 15:34:01 -04001544 if (!migrate_postcopy()) {
Dr. David Alan Gilberta54d3402015-11-12 11:34:44 +00001545 error_setg(errp, "Enable postcopy with migrate_set_capability before"
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001546 " the start of migration");
1547 return;
1548 }
1549
1550 if (s->state == MIGRATION_STATUS_NONE) {
1551 error_setg(errp, "Postcopy must be started after migration has been"
1552 " started");
1553 return;
1554 }
1555 /*
1556 * we don't error if migration has finished since that would be racy
1557 * with issuing this command.
1558 */
1559 atomic_set(&s->start_postcopy, true);
1560}
1561
aliguori065e2812008-11-11 16:46:33 +00001562/* shared migration helpers */
1563
zhanghailiang48781e52015-12-16 11:47:33 +00001564void migrate_set_state(int *state, int old_state, int new_state)
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +00001565{
Peter Xua31fede2017-08-30 16:32:01 +08001566 assert(new_state < MIGRATION_STATUS__MAX);
zhanghailiang48781e52015-12-16 11:47:33 +00001567 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
Peter Xua31fede2017-08-30 16:32:01 +08001568 trace_migrate_set_state(MigrationStatus_str(new_state));
Juan Quintelab05dc722015-07-07 14:44:05 +02001569 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +00001570 }
1571}
1572
Peter Xu4e4a3d32017-07-18 11:39:09 +08001573static MigrationCapabilityStatusList *migrate_cap_add(
1574 MigrationCapabilityStatusList *list,
1575 MigrationCapability index,
1576 bool state)
Juan Quintela2833c592017-04-05 18:32:37 +02001577{
1578 MigrationCapabilityStatusList *cap;
1579
1580 cap = g_new0(MigrationCapabilityStatusList, 1);
1581 cap->value = g_new0(MigrationCapabilityStatus, 1);
Peter Xu4e4a3d32017-07-18 11:39:09 +08001582 cap->value->capability = index;
1583 cap->value->state = state;
1584 cap->next = list;
1585
1586 return cap;
1587}
1588
1589void migrate_set_block_enabled(bool value, Error **errp)
1590{
1591 MigrationCapabilityStatusList *cap;
1592
1593 cap = migrate_cap_add(NULL, MIGRATION_CAPABILITY_BLOCK, value);
Juan Quintela2833c592017-04-05 18:32:37 +02001594 qmp_migrate_set_capabilities(cap, errp);
1595 qapi_free_MigrationCapabilityStatusList(cap);
1596}
1597
1598static void migrate_set_block_incremental(MigrationState *s, bool value)
1599{
1600 s->parameters.block_incremental = value;
1601}
1602
1603static void block_cleanup_parameters(MigrationState *s)
1604{
1605 if (s->must_remove_block_options) {
1606 /* setting to false can never fail */
1607 migrate_set_block_enabled(false, &error_abort);
1608 migrate_set_block_incremental(s, false);
1609 s->must_remove_block_options = false;
1610 }
1611}
1612
Yury Kotovfd392cf2019-04-08 14:33:43 +03001613static void migrate_fd_cleanup(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +00001614{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001615 qemu_bh_delete(s->cleanup_bh);
1616 s->cleanup_bh = NULL;
1617
Peter Xu0ceccd82018-01-03 20:20:06 +08001618 qemu_savevm_state_cleanup();
1619
zhanghailiang89a02a92016-01-15 11:37:42 +08001620 if (s->to_dst_file) {
Peter Xu62df0662018-05-02 18:47:38 +08001621 QEMUFile *tmp;
Juan Quintelaf986c3d2016-01-14 16:52:55 +01001622
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001623 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001624 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001625 if (s->migration_thread_running) {
1626 qemu_thread_join(&s->thread);
1627 s->migration_thread_running = false;
1628 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001629 qemu_mutex_lock_iothread();
1630
Fei Li1398b2e2019-01-13 22:08:47 +08001631 multifd_save_cleanup();
Peter Xu62df0662018-05-02 18:47:38 +08001632 qemu_mutex_lock(&s->qemu_file_lock);
1633 tmp = s->to_dst_file;
zhanghailiang89a02a92016-01-15 11:37:42 +08001634 s->to_dst_file = NULL;
Peter Xu62df0662018-05-02 18:47:38 +08001635 qemu_mutex_unlock(&s->qemu_file_lock);
1636 /*
1637 * Close the file handle without the lock to make sure the
1638 * critical section won't block for long.
1639 */
1640 qemu_fclose(tmp);
aliguori065e2812008-11-11 16:46:33 +00001641 }
1642
Wei Yang8f8d5282019-07-17 08:53:41 +08001643 assert(!migration_is_active(s));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001644
Liang Li94f5a432015-11-02 15:37:00 +08001645 if (s->state == MIGRATION_STATUS_CANCELLING) {
zhanghailiang48781e52015-12-16 11:47:33 +00001646 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
Liang Li94f5a432015-11-02 15:37:00 +08001647 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001648 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001649
Juan Quintela87db1a72017-09-05 12:50:22 +02001650 if (s->error) {
1651 /* It is used on info migrate. We can't free it */
1652 error_report_err(error_copy(s->error));
1653 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001654 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +02001655 block_cleanup_parameters(s);
aliguori065e2812008-11-11 16:46:33 +00001656}
1657
Yury Kotovfd392cf2019-04-08 14:33:43 +03001658static void migrate_fd_cleanup_schedule(MigrationState *s)
1659{
1660 /*
1661 * Ref the state for bh, because it may be called when
1662 * there're already no other refs
1663 */
1664 object_ref(OBJECT(s));
1665 qemu_bh_schedule(s->cleanup_bh);
1666}
1667
1668static void migrate_fd_cleanup_bh(void *opaque)
1669{
1670 MigrationState *s = opaque;
1671 migrate_fd_cleanup(s);
1672 object_unref(OBJECT(s));
1673}
1674
Juan Quintela87db1a72017-09-05 12:50:22 +02001675void migrate_set_error(MigrationState *s, const Error *error)
1676{
Daniel Brodsky6e8a3552020-04-03 21:21:08 -07001677 QEMU_LOCK_GUARD(&s->error_mutex);
Juan Quintela87db1a72017-09-05 12:50:22 +02001678 if (!s->error) {
1679 s->error = error_copy(error);
1680 }
Juan Quintela87db1a72017-09-05 12:50:22 +02001681}
1682
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001683void migrate_fd_error(MigrationState *s, const Error *error)
aliguori065e2812008-11-11 16:46:33 +00001684{
Peter Maydell25174052016-10-21 18:41:45 +01001685 trace_migrate_fd_error(error_get_pretty(error));
zhanghailiang89a02a92016-01-15 11:37:42 +08001686 assert(s->to_dst_file == NULL);
zhanghailiang48781e52015-12-16 11:47:33 +00001687 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1688 MIGRATION_STATUS_FAILED);
Juan Quintela87db1a72017-09-05 12:50:22 +02001689 migrate_set_error(s, error);
Juan Quintela458cf282011-02-22 23:32:54 +01001690}
1691
Juan Quintela0edda1c2010-05-11 16:28:39 +02001692static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +00001693{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001694 int old_state ;
zhanghailiang89a02a92016-01-15 11:37:42 +08001695 QEMUFile *f = migrate_get_current()->to_dst_file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001696 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +00001697
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001698 if (s->rp_state.from_dst_file) {
1699 /* shutdown the rp socket, so causing the rp thread to shutdown */
1700 qemu_file_shutdown(s->rp_state.from_dst_file);
1701 }
1702
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001703 do {
1704 old_state = s->state;
Juan Quintela392d87e2020-01-21 15:39:23 +01001705 if (!migration_is_running(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001706 break;
1707 }
Dr. David Alan Gilberta7b36b42017-10-20 10:05:55 +01001708 /* If the migration is paused, kick it out of the pause */
1709 if (old_state == MIGRATION_STATUS_PRE_SWITCHOVER) {
1710 qemu_sem_post(&s->pause_sem);
1711 }
zhanghailiang48781e52015-12-16 11:47:33 +00001712 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
zhanghailiang31194732015-03-13 16:08:38 +08001713 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001714
1715 /*
1716 * If we're unlucky the migration code might be stuck somewhere in a
1717 * send/write while the network has failed and is waiting to timeout;
1718 * if we've got shutdown(2) available then we can force it to quit.
1719 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1720 * called in a bh, so there is no race against this cancel.
1721 */
zhanghailiang31194732015-03-13 16:08:38 +08001722 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001723 qemu_file_shutdown(f);
1724 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08001725 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
1726 Error *local_err = NULL;
1727
1728 bdrv_invalidate_cache_all(&local_err);
1729 if (local_err) {
1730 error_report_err(local_err);
1731 } else {
1732 s->block_inactive = false;
1733 }
1734 }
aliguori065e2812008-11-11 16:46:33 +00001735}
1736
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001737void add_migration_state_change_notifier(Notifier *notify)
1738{
1739 notifier_list_add(&migration_state_notifiers, notify);
1740}
1741
1742void remove_migration_state_change_notifier(Notifier *notify)
1743{
Paolo Bonzini31552522012-01-13 17:34:01 +01001744 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001745}
1746
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +02001747bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001748{
zhanghailiang31194732015-03-13 16:08:38 +08001749 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001750}
1751
Juan Quintela70736932011-02-23 00:43:59 +01001752bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001753{
zhanghailiang31194732015-03-13 16:08:38 +08001754 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001755}
Juan Quintela0edda1c2010-05-11 16:28:39 +02001756
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001757bool migration_has_failed(MigrationState *s)
1758{
zhanghailiang31194732015-03-13 16:08:38 +08001759 return (s->state == MIGRATION_STATUS_CANCELLED ||
1760 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001761}
1762
Juan Quintela57273092017-03-20 22:25:28 +01001763bool migration_in_postcopy(void)
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001764{
Juan Quintela57273092017-03-20 22:25:28 +01001765 MigrationState *s = migrate_get_current();
1766
Dr. David Alan Gilbert3748fef2019-09-23 18:49:42 +01001767 switch (s->state) {
1768 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1769 case MIGRATION_STATUS_POSTCOPY_PAUSED:
1770 case MIGRATION_STATUS_POSTCOPY_RECOVER:
1771 return true;
1772 default:
1773 return false;
1774 }
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001775}
1776
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001777bool migration_in_postcopy_after_devices(MigrationState *s)
1778{
Juan Quintela57273092017-03-20 22:25:28 +01001779 return migration_in_postcopy() && s->postcopy_after_devices;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001780}
1781
David Hildenbrand06df2e62020-06-26 09:22:33 +02001782bool migration_in_incoming_postcopy(void)
1783{
1784 PostcopyState ps = postcopy_state_get();
1785
1786 return ps >= POSTCOPY_INCOMING_DISCARD && ps < POSTCOPY_INCOMING_END;
1787}
1788
Juan Quintelafab35002017-03-22 17:36:57 +01001789bool migration_is_idle(void)
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301790{
Markus Armbrusterdaff7f02019-04-01 11:08:25 +02001791 MigrationState *s = current_migration;
1792
1793 if (!s) {
1794 return true;
1795 }
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301796
1797 switch (s->state) {
1798 case MIGRATION_STATUS_NONE:
1799 case MIGRATION_STATUS_CANCELLED:
1800 case MIGRATION_STATUS_COMPLETED:
1801 case MIGRATION_STATUS_FAILED:
1802 return true;
1803 case MIGRATION_STATUS_SETUP:
1804 case MIGRATION_STATUS_CANCELLING:
1805 case MIGRATION_STATUS_ACTIVE:
1806 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1807 case MIGRATION_STATUS_COLO:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +01001808 case MIGRATION_STATUS_PRE_SWITCHOVER:
1809 case MIGRATION_STATUS_DEVICE:
Jens Freimannc7e0acd2019-10-29 12:49:02 +01001810 case MIGRATION_STATUS_WAIT_UNPLUG:
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301811 return false;
1812 case MIGRATION_STATUS__MAX:
1813 g_assert_not_reached();
1814 }
1815
1816 return false;
1817}
1818
Wei Yang8f8d5282019-07-17 08:53:41 +08001819bool migration_is_active(MigrationState *s)
1820{
1821 return (s->state == MIGRATION_STATUS_ACTIVE ||
1822 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1823}
1824
Peter Xu3e0c8052018-02-08 18:31:15 +08001825void migrate_init(MigrationState *s)
Juan Quintela0edda1c2010-05-11 16:28:39 +02001826{
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001827 /*
1828 * Reinitialise all migration state, except
1829 * parameters/capabilities that the user set, and
1830 * locks.
1831 */
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001832 s->cleanup_bh = 0;
zhanghailiang89a02a92016-01-15 11:37:42 +08001833 s->to_dst_file = NULL;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001834 s->state = MIGRATION_STATUS_NONE;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001835 s->rp_state.from_dst_file = NULL;
1836 s->rp_state.error = false;
1837 s->mbps = 0.0;
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08001838 s->pages_per_second = 0.0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001839 s->downtime = 0;
1840 s->expected_downtime = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001841 s->setup_time = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001842 s->start_postcopy = false;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001843 s->postcopy_after_devices = false;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001844 s->migration_thread_running = false;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001845 error_free(s->error);
1846 s->error = NULL;
Juan Quintela1299c632011-11-09 21:29:01 +01001847
zhanghailiang48781e52015-12-16 11:47:33 +00001848 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +02001849
Peter Xu4af246a2018-01-03 20:20:08 +08001850 s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1851 s->total_time = 0;
Peter Xu7287cbd2018-01-03 20:20:09 +08001852 s->vm_was_running = false;
Peter Xub15df1a2018-01-03 20:20:13 +08001853 s->iteration_initial_bytes = 0;
1854 s->threshold_size = 0;
Juan Quintela0edda1c2010-05-11 16:28:39 +02001855}
Juan Quintelacab30142011-02-22 23:54:21 +01001856
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001857static GSList *migration_blockers;
1858
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301859int migrate_add_blocker(Error *reason, Error **errp)
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001860{
Markus Armbruster811f8652019-04-01 11:08:24 +02001861 if (only_migratable) {
Markus Armbruster4b576642018-10-17 10:26:25 +02001862 error_propagate_prepend(errp, error_copy(reason),
1863 "disallowing migration blocker "
Greg Kurzb6eca812019-05-16 12:58:05 +02001864 "(--only-migratable) for: ");
Ashijeet Acharyab67b8c32017-01-16 17:01:54 +05301865 return -EACCES;
1866 }
1867
Juan Quintelafab35002017-03-22 17:36:57 +01001868 if (migration_is_idle()) {
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301869 migration_blockers = g_slist_prepend(migration_blockers, reason);
1870 return 0;
1871 }
1872
Markus Armbruster4b576642018-10-17 10:26:25 +02001873 error_propagate_prepend(errp, error_copy(reason),
1874 "disallowing migration blocker "
1875 "(migration in progress) for: ");
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301876 return -EBUSY;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001877}
1878
1879void migrate_del_blocker(Error *reason)
1880{
1881 migration_blockers = g_slist_remove(migration_blockers, reason);
1882}
1883
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001884void qmp_migrate_incoming(const char *uri, Error **errp)
1885{
1886 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001887 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001888
1889 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001890 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001891 return;
1892 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001893 if (!once) {
1894 error_setg(errp, "The incoming migration has already been started");
Yury Kotov603d5a42019-11-13 20:53:25 +03001895 return;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001896 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001897
1898 qemu_start_incoming_migration(uri, &local_err);
1899
1900 if (local_err) {
1901 error_propagate(errp, local_err);
1902 return;
1903 }
1904
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001905 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001906}
1907
Peter Xu02affd42018-05-02 18:47:36 +08001908void qmp_migrate_recover(const char *uri, Error **errp)
1909{
1910 MigrationIncomingState *mis = migration_incoming_get_current();
1911
1912 if (mis->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
1913 error_setg(errp, "Migrate recover can only be run "
1914 "when postcopy is paused.");
1915 return;
1916 }
1917
1918 if (atomic_cmpxchg(&mis->postcopy_recover_triggered,
1919 false, true) == true) {
1920 error_setg(errp, "Migrate recovery is triggered already");
1921 return;
1922 }
1923
1924 /*
1925 * Note that this call will never start a real migration; it will
1926 * only re-setup the migration stream and poke existing migration
1927 * to continue using that newly established channel.
1928 */
1929 qemu_start_incoming_migration(uri, errp);
1930}
1931
Peter Xubfbf89c2018-05-02 18:47:39 +08001932void qmp_migrate_pause(Error **errp)
1933{
1934 MigrationState *ms = migrate_get_current();
1935 MigrationIncomingState *mis = migration_incoming_get_current();
1936 int ret;
1937
1938 if (ms->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1939 /* Source side, during postcopy */
1940 qemu_mutex_lock(&ms->qemu_file_lock);
1941 ret = qemu_file_shutdown(ms->to_dst_file);
1942 qemu_mutex_unlock(&ms->qemu_file_lock);
1943 if (ret) {
1944 error_setg(errp, "Failed to pause source migration");
1945 }
1946 return;
1947 }
1948
1949 if (mis->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1950 ret = qemu_file_shutdown(mis->from_src_file);
1951 if (ret) {
1952 error_setg(errp, "Failed to pause destination migration");
1953 }
1954 return;
1955 }
1956
1957 error_setg(errp, "migrate-pause is currently only supported "
1958 "during postcopy-active state");
1959}
1960
Greg Kurz24f39022016-05-04 21:44:19 +02001961bool migration_is_blocked(Error **errp)
1962{
1963 if (qemu_savevm_state_blocked(errp)) {
1964 return true;
1965 }
1966
1967 if (migration_blockers) {
Eduardo Habkost250561e2017-06-08 10:39:05 -03001968 error_propagate(errp, error_copy(migration_blockers->data));
Greg Kurz24f39022016-05-04 21:44:19 +02001969 return true;
1970 }
1971
1972 return false;
1973}
1974
Peter Xud3e35b82018-05-02 18:47:24 +08001975/* Returns true if continue to migrate, or false if error detected */
1976static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc,
1977 bool resume, Error **errp)
1978{
1979 Error *local_err = NULL;
1980
1981 if (resume) {
1982 if (s->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
1983 error_setg(errp, "Cannot resume if there is no "
1984 "paused migration");
1985 return false;
1986 }
Peter Xu97ca2112018-07-23 20:33:03 +08001987
1988 /*
1989 * Postcopy recovery won't work well with release-ram
1990 * capability since release-ram will drop the page buffer as
1991 * long as the page is put into the send buffer. So if there
1992 * is a network failure happened, any page buffers that have
1993 * not yet reached the destination VM but have already been
1994 * sent from the source VM will be lost forever. Let's refuse
1995 * the client from resuming such a postcopy migration.
1996 * Luckily release-ram was designed to only be used when src
1997 * and destination VMs are on the same host, so it should be
1998 * fine.
1999 */
2000 if (migrate_release_ram()) {
2001 error_setg(errp, "Postcopy recovery cannot work "
2002 "when release-ram capability is set");
2003 return false;
2004 }
2005
Peter Xud3e35b82018-05-02 18:47:24 +08002006 /* This is a resume, skip init status */
2007 return true;
2008 }
2009
Juan Quintela392d87e2020-01-21 15:39:23 +01002010 if (migration_is_running(s->state)) {
Peter Xud3e35b82018-05-02 18:47:24 +08002011 error_setg(errp, QERR_MIGRATION_ACTIVE);
2012 return false;
2013 }
2014
2015 if (runstate_check(RUN_STATE_INMIGRATE)) {
2016 error_setg(errp, "Guest is waiting for an incoming migration");
2017 return false;
2018 }
2019
2020 if (migration_is_blocked(errp)) {
2021 return false;
2022 }
2023
2024 if (blk || blk_inc) {
2025 if (migrate_use_block() || migrate_use_block_incremental()) {
2026 error_setg(errp, "Command options are incompatible with "
2027 "current migration capabilities");
2028 return false;
2029 }
2030 migrate_set_block_enabled(true, &local_err);
2031 if (local_err) {
2032 error_propagate(errp, local_err);
2033 return false;
2034 }
2035 s->must_remove_block_options = true;
2036 }
2037
2038 if (blk_inc) {
2039 migrate_set_block_incremental(s, true);
2040 }
2041
2042 migrate_init(s);
Ivan Ren87f3bd82019-08-02 18:18:41 +08002043 /*
2044 * set ram_counters memory to zero for a
2045 * new migration
2046 */
2047 memset(&ram_counters, 0, sizeof(ram_counters));
Peter Xud3e35b82018-05-02 18:47:24 +08002048
2049 return true;
2050}
2051
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002052void qmp_migrate(const char *uri, bool has_blk, bool blk,
2053 bool has_inc, bool inc, bool has_detach, bool detach,
Peter Xu7a4da282018-05-02 18:47:23 +08002054 bool has_resume, bool resume, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002055{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +02002056 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +02002057 MigrationState *s = migrate_get_current();
Juan Quintelacab30142011-02-22 23:54:21 +01002058 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +01002059
Peter Xud3e35b82018-05-02 18:47:24 +08002060 if (!migrate_prepare(s, has_blk && blk, has_inc && inc,
2061 has_resume && resume, errp)) {
2062 /* Error detected, put into errp */
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002063 return;
Juan Quintelacab30142011-02-22 23:54:21 +01002064 }
Juan Quintelacab30142011-02-22 23:54:21 +01002065
2066 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002067 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04002068#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08002069 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04002070 rdma_start_outgoing_migration(s, p, &local_err);
2071#endif
Juan Quintelacab30142011-02-22 23:54:21 +01002072 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002073 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01002074 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002075 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01002076 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002077 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01002078 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002079 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
2080 "a valid migration protocol");
zhanghailiang48781e52015-12-16 11:47:33 +00002081 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2082 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09576e72018-03-16 20:21:14 +00002083 block_cleanup_parameters(s);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002084 return;
Juan Quintelacab30142011-02-22 23:54:21 +01002085 }
2086
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002087 if (local_err) {
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01002088 migrate_fd_error(s, local_err);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002089 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002090 return;
Juan Quintela1299c632011-11-09 21:29:01 +01002091 }
Juan Quintelacab30142011-02-22 23:54:21 +01002092}
2093
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02002094void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002095{
Juan Quintela17549e82011-10-05 13:50:43 +02002096 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01002097}
2098
Dr. David Alan Gilbert89cfc022017-10-20 10:05:53 +01002099void qmp_migrate_continue(MigrationStatus state, Error **errp)
2100{
2101 MigrationState *s = migrate_get_current();
2102 if (s->state != state) {
2103 error_setg(errp, "Migration not in expected state: %s",
2104 MigrationStatus_str(s->state));
2105 return;
2106 }
2107 qemu_sem_post(&s->pause_sem);
2108}
2109
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002110void qmp_migrate_set_cache_size(int64_t value, Error **errp)
2111{
Juan Quintela73af8dd2017-10-05 21:30:10 +02002112 MigrateSetParameters p = {
2113 .has_xbzrle_cache_size = true,
2114 .xbzrle_cache_size = value,
2115 };
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002116
Juan Quintela73af8dd2017-10-05 21:30:10 +02002117 qmp_migrate_set_parameters(&p, errp);
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002118}
2119
2120int64_t qmp_query_migrate_cache_size(Error **errp)
2121{
2122 return migrate_xbzrle_cache_size();
2123}
2124
Luiz Capitulino3dc85382011-11-28 11:59:37 -02002125void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002126{
Markus Armbruster1bda8b32017-07-18 13:42:11 +02002127 MigrateSetParameters p = {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302128 .has_max_bandwidth = true,
2129 .max_bandwidth = value,
2130 };
Juan Quintelacab30142011-02-22 23:54:21 +01002131
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302132 qmp_migrate_set_parameters(&p, errp);
Juan Quintelacab30142011-02-22 23:54:21 +01002133}
2134
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02002135void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002136{
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03002137 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
Mao Zhongyi2ee30cf2020-03-31 16:22:05 +08002138 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
2139 "downtime_limit",
2140 "an integer in the range of 0 to "
2141 stringify(MAX_MIGRATE_DOWNTIME_SECONDS)" seconds");
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03002142 return;
2143 }
2144
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302145 value *= 1000; /* Convert to milliseconds */
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302146
Markus Armbruster1bda8b32017-07-18 13:42:11 +02002147 MigrateSetParameters p = {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302148 .has_downtime_limit = true,
Fangrui Song2667c982019-11-22 09:00:38 +01002149 .downtime_limit = (int64_t)value,
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302150 };
2151
2152 qmp_migrate_set_parameters(&p, errp);
aliguori5bb79102008-10-13 03:12:02 +00002153}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03002154
Pavel Butsykin53f09a12017-02-03 18:23:20 +03002155bool migrate_release_ram(void)
2156{
2157 MigrationState *s;
2158
2159 s = migrate_get_current();
2160
2161 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
2162}
2163
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00002164bool migrate_postcopy_ram(void)
2165{
2166 MigrationState *s;
2167
2168 s = migrate_get_current();
2169
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +00002170 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00002171}
2172
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002173bool migrate_postcopy(void)
2174{
Vladimir Sementsov-Ogievskiydd6bb912018-03-13 15:34:00 -04002175 return migrate_postcopy_ram() || migrate_dirty_bitmaps();
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002176}
2177
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06002178bool migrate_auto_converge(void)
2179{
2180 MigrationState *s;
2181
2182 s = migrate_get_current();
2183
2184 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
2185}
2186
Peter Lieven323004a2013-07-18 09:48:50 +02002187bool migrate_zero_blocks(void)
2188{
2189 MigrationState *s;
2190
2191 s = migrate_get_current();
2192
2193 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
2194}
2195
Alexey Perevalovf22f9282018-03-22 21:17:22 +03002196bool migrate_postcopy_blocktime(void)
2197{
2198 MigrationState *s;
2199
2200 s = migrate_get_current();
2201
2202 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME];
2203}
2204
Liang Li8706d2d2015-03-23 16:32:17 +08002205bool migrate_use_compression(void)
2206{
Liang Lidde4e692015-03-23 16:32:26 +08002207 MigrationState *s;
2208
2209 s = migrate_get_current();
2210
2211 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08002212}
2213
2214int migrate_compress_level(void)
2215{
2216 MigrationState *s;
2217
2218 s = migrate_get_current();
2219
Daniel P. Berrange2594f562016-04-27 11:05:14 +01002220 return s->parameters.compress_level;
Liang Li8706d2d2015-03-23 16:32:17 +08002221}
2222
2223int migrate_compress_threads(void)
2224{
2225 MigrationState *s;
2226
2227 s = migrate_get_current();
2228
Daniel P. Berrange2594f562016-04-27 11:05:14 +01002229 return s->parameters.compress_threads;
Liang Li8706d2d2015-03-23 16:32:17 +08002230}
2231
Xiao Guangrong1d588722018-08-21 16:10:20 +08002232int migrate_compress_wait_thread(void)
2233{
2234 MigrationState *s;
2235
2236 s = migrate_get_current();
2237
2238 return s->parameters.compress_wait_thread;
2239}
2240
Liang Li3fcb38c2015-03-23 16:32:18 +08002241int migrate_decompress_threads(void)
2242{
2243 MigrationState *s;
2244
2245 s = migrate_get_current();
2246
Daniel P. Berrange2594f562016-04-27 11:05:14 +01002247 return s->parameters.decompress_threads;
Liang Li3fcb38c2015-03-23 16:32:18 +08002248}
2249
Vladimir Sementsov-Ogievskiy55efc8c2018-03-13 15:34:00 -04002250bool migrate_dirty_bitmaps(void)
2251{
2252 MigrationState *s;
2253
2254 s = migrate_get_current();
2255
2256 return s->enabled_capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS];
2257}
2258
Yury Kotov18269062019-02-15 20:45:45 +03002259bool migrate_ignore_shared(void)
2260{
2261 MigrationState *s;
2262
2263 s = migrate_get_current();
2264
2265 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_IGNORE_SHARED];
2266}
2267
Yury Kotovb9d68df2019-09-03 19:22:44 +03002268bool migrate_validate_uuid(void)
2269{
2270 MigrationState *s;
2271
2272 s = migrate_get_current();
2273
2274 return s->enabled_capabilities[MIGRATION_CAPABILITY_VALIDATE_UUID];
2275}
2276
Juan Quintelab05dc722015-07-07 14:44:05 +02002277bool migrate_use_events(void)
2278{
2279 MigrationState *s;
2280
2281 s = migrate_get_current();
2282
2283 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
2284}
2285
Juan Quintela30126bb2016-01-14 12:23:00 +01002286bool migrate_use_multifd(void)
2287{
2288 MigrationState *s;
2289
2290 s = migrate_get_current();
2291
Juan Quintelacbfd6c92019-02-06 13:54:06 +01002292 return s->enabled_capabilities[MIGRATION_CAPABILITY_MULTIFD];
Juan Quintela30126bb2016-01-14 12:23:00 +01002293}
2294
Dr. David Alan Gilbert93fbd032017-10-20 10:05:50 +01002295bool migrate_pause_before_switchover(void)
2296{
2297 MigrationState *s;
2298
2299 s = migrate_get_current();
2300
2301 return s->enabled_capabilities[
2302 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER];
2303}
2304
Juan Quintela4075fb12016-01-15 08:56:17 +01002305int migrate_multifd_channels(void)
2306{
2307 MigrationState *s;
2308
2309 s = migrate_get_current();
2310
Juan Quintelacbfd6c92019-02-06 13:54:06 +01002311 return s->parameters.multifd_channels;
Juan Quintela4075fb12016-01-15 08:56:17 +01002312}
2313
Juan Quintelaab7cbb02019-05-15 13:37:46 +02002314MultiFDCompression migrate_multifd_compression(void)
2315{
2316 MigrationState *s;
2317
2318 s = migrate_get_current();
2319
2320 return s->parameters.multifd_compression;
2321}
2322
Juan Quintela9004db42020-01-23 17:08:52 +01002323int migrate_multifd_zlib_level(void)
2324{
2325 MigrationState *s;
2326
2327 s = migrate_get_current();
2328
2329 return s->parameters.multifd_zlib_level;
2330}
2331
Juan Quintela6a9ad152020-01-23 17:41:36 +01002332int migrate_multifd_zstd_level(void)
2333{
2334 MigrationState *s;
2335
2336 s = migrate_get_current();
2337
2338 return s->parameters.multifd_zstd_level;
2339}
2340
Orit Wasserman17ad9b32012-08-06 21:42:53 +03002341int migrate_use_xbzrle(void)
2342{
2343 MigrationState *s;
2344
2345 s = migrate_get_current();
2346
2347 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
2348}
2349
2350int64_t migrate_xbzrle_cache_size(void)
2351{
2352 MigrationState *s;
2353
2354 s = migrate_get_current();
2355
Juan Quintela73af8dd2017-10-05 21:30:10 +02002356 return s->parameters.xbzrle_cache_size;
Orit Wasserman17ad9b32012-08-06 21:42:53 +03002357}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002358
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01002359static int64_t migrate_max_postcopy_bandwidth(void)
2360{
2361 MigrationState *s;
2362
2363 s = migrate_get_current();
2364
2365 return s->parameters.max_postcopy_bandwidth;
2366}
2367
Juan Quintela2833c592017-04-05 18:32:37 +02002368bool migrate_use_block(void)
2369{
2370 MigrationState *s;
2371
2372 s = migrate_get_current();
2373
2374 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
2375}
2376
Peter Xuc788ada2017-06-26 18:28:55 +08002377bool migrate_use_return_path(void)
2378{
2379 MigrationState *s;
2380
2381 s = migrate_get_current();
2382
2383 return s->enabled_capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
2384}
2385
Juan Quintela2833c592017-04-05 18:32:37 +02002386bool migrate_use_block_incremental(void)
2387{
2388 MigrationState *s;
2389
2390 s = migrate_get_current();
2391
2392 return s->parameters.block_incremental;
2393}
2394
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002395/* migration thread support */
2396/*
2397 * Something bad happened to the RP stream, mark an error
2398 * The caller shall print or trace something to indicate why
2399 */
2400static void mark_source_rp_bad(MigrationState *s)
2401{
2402 s->rp_state.error = true;
2403}
2404
2405static struct rp_cmd_args {
2406 ssize_t len; /* -1 = variable */
2407 const char *name;
2408} rp_cmd_args[] = {
2409 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
2410 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
2411 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002412 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
2413 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Peter Xua335deb2018-05-02 18:47:28 +08002414 [MIG_RP_MSG_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" },
Peter Xu13955b82018-05-02 18:47:30 +08002415 [MIG_RP_MSG_RESUME_ACK] = { .len = 4, .name = "RESUME_ACK" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002416 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
2417};
2418
2419/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002420 * Process a request for pages received on the return path,
2421 * We're allowed to send more than requested (e.g. to round to our page size)
2422 * and we don't need to send pages that have already been sent.
2423 */
2424static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
2425 ram_addr_t start, size_t len)
2426{
Wei Yang038adc22019-10-13 10:11:45 +08002427 long our_host_ps = qemu_real_host_page_size;
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00002428
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002429 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00002430
2431 /*
2432 * Since we currently insist on matching page sizes, just sanity check
2433 * we're being asked for whole host pages.
2434 */
2435 if (start & (our_host_ps-1) ||
2436 (len & (our_host_ps-1))) {
2437 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
2438 " len: %zd", __func__, start, len);
2439 mark_source_rp_bad(ms);
2440 return;
2441 }
2442
Juan Quintela96506892017-03-14 18:41:03 +01002443 if (ram_save_queue_pages(rbname, start, len)) {
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00002444 mark_source_rp_bad(ms);
2445 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002446}
2447
Peter Xu14b17422018-05-02 18:47:21 +08002448/* Return true to retry, false to quit */
2449static bool postcopy_pause_return_path_thread(MigrationState *s)
2450{
2451 trace_postcopy_pause_return_path();
2452
2453 qemu_sem_wait(&s->postcopy_pause_rp_sem);
2454
2455 trace_postcopy_pause_return_path_continued();
2456
2457 return true;
2458}
2459
Peter Xua335deb2018-05-02 18:47:28 +08002460static int migrate_handle_rp_recv_bitmap(MigrationState *s, char *block_name)
2461{
2462 RAMBlock *block = qemu_ram_block_by_name(block_name);
2463
2464 if (!block) {
2465 error_report("%s: invalid block name '%s'", __func__, block_name);
2466 return -EINVAL;
2467 }
2468
2469 /* Fetch the received bitmap and refresh the dirty bitmap */
2470 return ram_dirty_bitmap_reload(s, block);
2471}
2472
Peter Xu13955b82018-05-02 18:47:30 +08002473static int migrate_handle_rp_resume_ack(MigrationState *s, uint32_t value)
2474{
2475 trace_source_return_path_thread_resume_ack(value);
2476
2477 if (value != MIGRATION_RESUME_ACK_VALUE) {
2478 error_report("%s: illegal resume_ack value %"PRIu32,
2479 __func__, value);
2480 return -1;
2481 }
2482
2483 /* Now both sides are active. */
2484 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
2485 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2486
Peter Xu94190692018-05-02 18:47:34 +08002487 /* Notify send thread that time to continue send pages */
2488 qemu_sem_post(&s->rp_state.rp_sem);
Peter Xu13955b82018-05-02 18:47:30 +08002489
2490 return 0;
2491}
2492
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002493/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002494 * Handles messages sent on the return path towards the source VM
2495 *
2496 */
2497static void *source_return_path_thread(void *opaque)
2498{
2499 MigrationState *ms = opaque;
2500 QEMUFile *rp = ms->rp_state.from_dst_file;
2501 uint16_t header_len, header_type;
Peter Xu568b01c2016-03-09 14:12:12 +08002502 uint8_t buf[512];
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002503 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002504 ram_addr_t start = 0; /* =0 to silence warning */
2505 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002506 int res;
2507
2508 trace_source_return_path_thread_entry();
Lidong Chen74637e62018-08-06 21:29:29 +08002509 rcu_register_thread();
Peter Xu14b17422018-05-02 18:47:21 +08002510
2511retry:
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002512 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
2513 migration_is_setup_or_active(ms->state)) {
2514 trace_source_return_path_thread_loop_top();
2515 header_type = qemu_get_be16(rp);
2516 header_len = qemu_get_be16(rp);
2517
Peter Xu7a9ddfb2018-02-08 18:31:05 +08002518 if (qemu_file_get_error(rp)) {
2519 mark_source_rp_bad(ms);
2520 goto out;
2521 }
2522
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002523 if (header_type >= MIG_RP_MSG_MAX ||
2524 header_type == MIG_RP_MSG_INVALID) {
2525 error_report("RP: Received invalid message 0x%04x length 0x%04x",
Mao Zhongyied8b2822020-03-28 01:19:08 +08002526 header_type, header_len);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002527 mark_source_rp_bad(ms);
2528 goto out;
2529 }
2530
2531 if ((rp_cmd_args[header_type].len != -1 &&
2532 header_len != rp_cmd_args[header_type].len) ||
Peter Xu568b01c2016-03-09 14:12:12 +08002533 header_len > sizeof(buf)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002534 error_report("RP: Received '%s' message (0x%04x) with"
Mao Zhongyied8b2822020-03-28 01:19:08 +08002535 "incorrect length %d expecting %zu",
2536 rp_cmd_args[header_type].name, header_type, header_len,
2537 (size_t)rp_cmd_args[header_type].len);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002538 mark_source_rp_bad(ms);
2539 goto out;
2540 }
2541
2542 /* We know we've got a valid header by this point */
2543 res = qemu_get_buffer(rp, buf, header_len);
2544 if (res != header_len) {
2545 error_report("RP: Failed reading data for message 0x%04x"
2546 " read %d expected %d",
2547 header_type, res, header_len);
2548 mark_source_rp_bad(ms);
2549 goto out;
2550 }
2551
2552 /* OK, we have the message and the data */
2553 switch (header_type) {
2554 case MIG_RP_MSG_SHUT:
Peter Maydell4d885132016-06-10 17:09:22 +01002555 sibling_error = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002556 trace_source_return_path_thread_shut(sibling_error);
2557 if (sibling_error) {
2558 error_report("RP: Sibling indicated error %d", sibling_error);
2559 mark_source_rp_bad(ms);
2560 }
2561 /*
2562 * We'll let the main thread deal with closing the RP
2563 * we could do a shutdown(2) on it, but we're the only user
2564 * anyway, so there's nothing gained.
2565 */
2566 goto out;
2567
2568 case MIG_RP_MSG_PONG:
Peter Maydell4d885132016-06-10 17:09:22 +01002569 tmp32 = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002570 trace_source_return_path_thread_pong(tmp32);
2571 break;
2572
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002573 case MIG_RP_MSG_REQ_PAGES:
Peter Maydell4d885132016-06-10 17:09:22 +01002574 start = ldq_be_p(buf);
2575 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002576 migrate_handle_rp_req_pages(ms, NULL, start, len);
2577 break;
2578
2579 case MIG_RP_MSG_REQ_PAGES_ID:
2580 expected_len = 12 + 1; /* header + termination */
2581
2582 if (header_len >= expected_len) {
Peter Maydell4d885132016-06-10 17:09:22 +01002583 start = ldq_be_p(buf);
2584 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002585 /* Now we expect an idstr */
2586 tmp32 = buf[12]; /* Length of the following idstr */
2587 buf[13 + tmp32] = '\0';
2588 expected_len += tmp32;
2589 }
2590 if (header_len != expected_len) {
2591 error_report("RP: Req_Page_id with length %d expecting %zd",
Mao Zhongyied8b2822020-03-28 01:19:08 +08002592 header_len, expected_len);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002593 mark_source_rp_bad(ms);
2594 goto out;
2595 }
2596 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
2597 break;
2598
Peter Xua335deb2018-05-02 18:47:28 +08002599 case MIG_RP_MSG_RECV_BITMAP:
2600 if (header_len < 1) {
2601 error_report("%s: missing block name", __func__);
2602 mark_source_rp_bad(ms);
2603 goto out;
2604 }
2605 /* Format: len (1B) + idstr (<255B). This ends the idstr. */
2606 buf[buf[0] + 1] = '\0';
2607 if (migrate_handle_rp_recv_bitmap(ms, (char *)(buf + 1))) {
2608 mark_source_rp_bad(ms);
2609 goto out;
2610 }
2611 break;
2612
Peter Xu13955b82018-05-02 18:47:30 +08002613 case MIG_RP_MSG_RESUME_ACK:
2614 tmp32 = ldl_be_p(buf);
2615 if (migrate_handle_rp_resume_ack(ms, tmp32)) {
2616 mark_source_rp_bad(ms);
2617 goto out;
2618 }
2619 break;
2620
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002621 default:
2622 break;
2623 }
2624 }
Peter Xu14b17422018-05-02 18:47:21 +08002625
2626out:
2627 res = qemu_file_get_error(rp);
2628 if (res) {
Dr. David Alan Gilbertfb14a422019-10-07 11:35:07 +01002629 if (res == -EIO && migration_in_postcopy()) {
Peter Xu14b17422018-05-02 18:47:21 +08002630 /*
2631 * Maybe there is something we can do: it looks like a
2632 * network down issue, and we pause for a recovery.
2633 */
2634 if (postcopy_pause_return_path_thread(ms)) {
2635 /* Reload rp, reset the rest */
Marc-André Lureau0284a2a2018-09-25 13:22:45 +04002636 if (rp != ms->rp_state.from_dst_file) {
2637 qemu_fclose(rp);
2638 rp = ms->rp_state.from_dst_file;
2639 }
Peter Xu14b17422018-05-02 18:47:21 +08002640 ms->rp_state.error = false;
2641 goto retry;
2642 }
2643 }
2644
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002645 trace_source_return_path_thread_bad_end();
2646 mark_source_rp_bad(ms);
2647 }
2648
2649 trace_source_return_path_thread_end();
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002650 ms->rp_state.from_dst_file = NULL;
2651 qemu_fclose(rp);
Lidong Chen74637e62018-08-06 21:29:29 +08002652 rcu_unregister_thread();
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002653 return NULL;
2654}
2655
Peter Xud3e35b82018-05-02 18:47:24 +08002656static int open_return_path_on_source(MigrationState *ms,
2657 bool create_thread)
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002658{
2659
zhanghailiang89a02a92016-01-15 11:37:42 +08002660 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002661 if (!ms->rp_state.from_dst_file) {
2662 return -1;
2663 }
2664
2665 trace_open_return_path_on_source();
Peter Xud3e35b82018-05-02 18:47:24 +08002666
2667 if (!create_thread) {
2668 /* We're done */
2669 return 0;
2670 }
2671
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002672 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
2673 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
2674
2675 trace_open_return_path_on_source_continue();
2676
2677 return 0;
2678}
2679
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002680/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
2681static int await_return_path_close_on_source(MigrationState *ms)
2682{
2683 /*
2684 * If this is a normal exit then the destination will send a SHUT and the
2685 * rp_thread will exit, however if there's an error we need to cause
2686 * it to exit.
2687 */
zhanghailiang89a02a92016-01-15 11:37:42 +08002688 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002689 /*
2690 * shutdown(2), if we have it, will cause it to unblock if it's stuck
2691 * waiting for the destination.
2692 */
2693 qemu_file_shutdown(ms->rp_state.from_dst_file);
2694 mark_source_rp_bad(ms);
2695 }
2696 trace_await_return_path_close_on_source_joining();
2697 qemu_thread_join(&ms->rp_state.rp_thread);
2698 trace_await_return_path_close_on_source_close();
2699 return ms->rp_state.error;
2700}
2701
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002702/*
2703 * Switch from normal iteration to postcopy
2704 * Returns non-0 on error
2705 */
Peter Xu7287cbd2018-01-03 20:20:09 +08002706static int postcopy_start(MigrationState *ms)
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002707{
2708 int ret;
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002709 QIOChannelBuffer *bioc;
2710 QEMUFile *fb;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002711 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01002712 int64_t bandwidth = migrate_max_postcopy_bandwidth();
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002713 bool restart_block = false;
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002714 int cur_state = MIGRATION_STATUS_ACTIVE;
2715 if (!migrate_pause_before_switchover()) {
2716 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
2717 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2718 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002719
2720 trace_postcopy_start();
2721 qemu_mutex_lock_iothread();
2722 trace_postcopy_start_set_run();
2723
Daniel Henrique Barbozafb064112018-12-05 17:47:01 -02002724 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002725 global_state_store();
2726 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01002727 if (ret < 0) {
2728 goto fail;
2729 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002730
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002731 ret = migration_maybe_pause(ms, &cur_state,
2732 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2733 if (ret < 0) {
2734 goto fail;
2735 }
2736
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01002737 ret = bdrv_inactivate_all();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002738 if (ret < 0) {
2739 goto fail;
2740 }
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002741 restart_block = true;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002742
2743 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00002744 * Cause any non-postcopiable, but iterative devices to
2745 * send out their final data.
2746 */
Fam Zhenga1fbe752017-06-17 00:06:58 +08002747 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00002748
2749 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002750 * in Finish migrate and with the io-lock held everything should
2751 * be quiet, but we've potentially still got dirty pages and we
2752 * need to tell the destination to throw any pages it's already received
2753 * that are dirty
2754 */
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002755 if (migrate_postcopy_ram()) {
2756 if (ram_postcopy_send_discard_bitmap(ms)) {
2757 error_report("postcopy send discard bitmap failed");
2758 goto fail;
2759 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002760 }
2761
2762 /*
2763 * send rest of state - note things that are doing postcopy
2764 * will notice we're in POSTCOPY_ACTIVE and not actually
2765 * wrap their state up here
2766 */
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01002767 /* 0 max-postcopy-bandwidth means unlimited */
2768 if (!bandwidth) {
2769 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
2770 } else {
2771 qemu_file_set_rate_limit(ms->to_dst_file, bandwidth / XFER_LIMIT_RATIO);
2772 }
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002773 if (migrate_postcopy_ram()) {
2774 /* Ping just for debugging, helps line traces up */
2775 qemu_savevm_send_ping(ms->to_dst_file, 2);
2776 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002777
2778 /*
2779 * While loading the device state we may trigger page transfer
2780 * requests and the fd must be free to process those, and thus
2781 * the destination must read the whole device state off the fd before
2782 * it starts processing it. Unfortunately the ad-hoc migration format
2783 * doesn't allow the destination to know the size to read without fully
2784 * parsing it through each devices load-state code (especially the open
2785 * coded devices that use get/put).
2786 * So we wrap the device state up in a package with a length at the start;
2787 * to do this we use a qemu_buf to hold the whole of the device state.
2788 */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002789 bioc = qio_channel_buffer_new(4096);
Daniel P. Berrange6f01f132016-09-30 11:57:14 +01002790 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002791 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
2792 object_unref(OBJECT(bioc));
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002793
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00002794 /*
2795 * Make sure the receiver can get incoming pages before we send the rest
2796 * of the state
2797 */
2798 qemu_savevm_send_postcopy_listen(fb);
2799
Fam Zhenga1fbe752017-06-17 00:06:58 +08002800 qemu_savevm_state_complete_precopy(fb, false, false);
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002801 if (migrate_postcopy_ram()) {
2802 qemu_savevm_send_ping(fb, 3);
2803 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002804
2805 qemu_savevm_send_postcopy_run(fb);
2806
2807 /* <><> end of stuff going into the package */
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002808
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002809 /* Last point of recovery; as soon as we send the package the destination
2810 * can open devices and potentially start running.
2811 * Lets just check again we've not got any errors.
2812 */
2813 ret = qemu_file_get_error(ms->to_dst_file);
2814 if (ret) {
2815 error_report("postcopy_start: Migration stream errored (pre package)");
2816 goto fail_closefb;
2817 }
2818
2819 restart_block = false;
2820
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002821 /* Now send that blob */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002822 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002823 goto fail_closefb;
2824 }
2825 qemu_fclose(fb);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00002826
2827 /* Send a notify to give a chance for anything that needs to happen
2828 * at the transition to postcopy and after the device state; in particular
2829 * spice needs to trigger a transition now
2830 */
2831 ms->postcopy_after_devices = true;
2832 notifier_list_notify(&migration_state_notifiers, ms);
2833
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002834 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
2835
2836 qemu_mutex_unlock_iothread();
2837
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002838 if (migrate_postcopy_ram()) {
2839 /*
2840 * Although this ping is just for debug, it could potentially be
2841 * used for getting a better measurement of downtime at the source.
2842 */
2843 qemu_savevm_send_ping(ms->to_dst_file, 4);
2844 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002845
Pavel Butsykinced1c612017-02-03 18:23:21 +03002846 if (migrate_release_ram()) {
2847 ram_postcopy_migrated_memory_release(ms);
2848 }
2849
zhanghailiang89a02a92016-01-15 11:37:42 +08002850 ret = qemu_file_get_error(ms->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002851 if (ret) {
2852 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00002853 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002854 MIGRATION_STATUS_FAILED);
2855 }
2856
2857 return ret;
2858
2859fail_closefb:
2860 qemu_fclose(fb);
2861fail:
zhanghailiang48781e52015-12-16 11:47:33 +00002862 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002863 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002864 if (restart_block) {
2865 /* A failure happened early enough that we know the destination hasn't
2866 * accessed block devices, so we're safe to recover.
2867 */
2868 Error *local_err = NULL;
2869
2870 bdrv_invalidate_cache_all(&local_err);
2871 if (local_err) {
2872 error_report_err(local_err);
2873 }
2874 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002875 qemu_mutex_unlock_iothread();
2876 return -1;
2877}
2878
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002879/**
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002880 * migration_maybe_pause: Pause if required to by
2881 * migrate_pause_before_switchover called with the iothread locked
2882 * Returns: 0 on success
2883 */
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002884static int migration_maybe_pause(MigrationState *s,
2885 int *current_active_state,
2886 int new_state)
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002887{
2888 if (!migrate_pause_before_switchover()) {
2889 return 0;
2890 }
2891
2892 /* Since leaving this state is not atomic with posting the semaphore
2893 * it's possible that someone could have issued multiple migrate_continue
2894 * and the semaphore is incorrectly positive at this point;
2895 * the docs say it's undefined to reinit a semaphore that's already
2896 * init'd, so use timedwait to eat up any existing posts.
2897 */
2898 while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) {
2899 /* This block intentionally left blank */
2900 }
2901
Zhimin Feng89583382020-01-14 17:43:09 +08002902 /*
2903 * If the migration is cancelled when it is in the completion phase,
2904 * the migration state is set to MIGRATION_STATUS_CANCELLING.
2905 * So we don't need to wait a semaphore, otherwise we would always
2906 * wait for the 'pause_sem' semaphore.
2907 */
2908 if (s->state != MIGRATION_STATUS_CANCELLING) {
2909 qemu_mutex_unlock_iothread();
2910 migrate_set_state(&s->state, *current_active_state,
2911 MIGRATION_STATUS_PRE_SWITCHOVER);
2912 qemu_sem_wait(&s->pause_sem);
2913 migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER,
2914 new_state);
2915 *current_active_state = new_state;
2916 qemu_mutex_lock_iothread();
2917 }
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002918
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002919 return s->state == new_state ? 0 : -EINVAL;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002920}
2921
2922/**
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002923 * migration_completion: Used by migration_thread when there's not much left.
2924 * The caller 'breaks' the loop when this returns.
2925 *
2926 * @s: Current migration state
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002927 */
Peter Xu2ad87302018-01-03 20:20:14 +08002928static void migration_completion(MigrationState *s)
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002929{
2930 int ret;
Peter Xu2ad87302018-01-03 20:20:14 +08002931 int current_active_state = s->state;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002932
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002933 if (s->state == MIGRATION_STATUS_ACTIVE) {
2934 qemu_mutex_lock_iothread();
Peter Xu64909f92018-01-03 20:20:10 +08002935 s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Daniel Henrique Barbozafb064112018-12-05 17:47:01 -02002936 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
Peter Xu7287cbd2018-01-03 20:20:09 +08002937 s->vm_was_running = runstate_is_running();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002938 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002939
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002940 if (!ret) {
Fam Zhenga1fbe752017-06-17 00:06:58 +08002941 bool inactivate = !migrate_colo_enabled();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002942 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02002943 if (ret >= 0) {
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002944 ret = migration_maybe_pause(s, &current_active_state,
2945 MIGRATION_STATUS_DEVICE);
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002946 }
2947 if (ret >= 0) {
Kevin Wolff07fa4c2017-05-22 17:10:38 +02002948 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
Fam Zhenga1fbe752017-06-17 00:06:58 +08002949 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
2950 inactivate);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02002951 }
Fam Zhenga1fbe752017-06-17 00:06:58 +08002952 if (inactivate && ret >= 0) {
2953 s->block_inactive = true;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002954 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002955 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002956 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002957
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002958 if (ret < 0) {
2959 goto fail;
2960 }
2961 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2962 trace_migration_completion_postcopy_end();
2963
zhanghailiang89a02a92016-01-15 11:37:42 +08002964 qemu_savevm_state_complete_postcopy(s->to_dst_file);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002965 trace_migration_completion_postcopy_end_after_complete();
2966 }
2967
2968 /*
2969 * If rp was opened we must clean up the thread before
2970 * cleaning everything else up (since if there are no failures
2971 * it will wait for the destination to send it's status in
2972 * a SHUT command).
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002973 */
Peter Xu0425dc92017-05-31 18:35:34 +08002974 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002975 int rp_error;
Peter Xu0425dc92017-05-31 18:35:34 +08002976 trace_migration_return_path_end_before();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002977 rp_error = await_return_path_close_on_source(s);
Peter Xu0425dc92017-05-31 18:35:34 +08002978 trace_migration_return_path_end_after(rp_error);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002979 if (rp_error) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02002980 goto fail_invalidate;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002981 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002982 }
2983
zhanghailiang89a02a92016-01-15 11:37:42 +08002984 if (qemu_file_get_error(s->to_dst_file)) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002985 trace_migration_completion_file_err();
Greg Kurzfe904ea2016-05-18 15:44:36 +02002986 goto fail_invalidate;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002987 }
2988
zhanghailiang0b827d52016-10-27 14:42:54 +08002989 if (!migrate_colo_enabled()) {
2990 migrate_set_state(&s->state, current_active_state,
2991 MIGRATION_STATUS_COMPLETED);
2992 }
2993
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002994 return;
2995
Greg Kurzfe904ea2016-05-18 15:44:36 +02002996fail_invalidate:
2997 /* If not doing postcopy, vm_start() will be called: let's regain
2998 * control on images.
2999 */
Dr. David Alan Gilbert6039dd52018-02-05 09:13:37 +00003000 if (s->state == MIGRATION_STATUS_ACTIVE ||
3001 s->state == MIGRATION_STATUS_DEVICE) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02003002 Error *local_err = NULL;
3003
zhanghailiang1d2acc32017-01-24 15:59:52 +08003004 qemu_mutex_lock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02003005 bdrv_invalidate_cache_all(&local_err);
3006 if (local_err) {
3007 error_report_err(local_err);
zhanghailiang1d2acc32017-01-24 15:59:52 +08003008 } else {
3009 s->block_inactive = false;
Greg Kurzfe904ea2016-05-18 15:44:36 +02003010 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08003011 qemu_mutex_unlock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02003012 }
3013
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003014fail:
zhanghailiang48781e52015-12-16 11:47:33 +00003015 migrate_set_state(&s->state, current_active_state,
3016 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003017}
3018
zhanghailiang35a6ed42016-10-27 14:42:52 +08003019bool migrate_colo_enabled(void)
3020{
3021 MigrationState *s = migrate_get_current();
3022 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
3023}
3024
Peter Xub23c2ad2018-05-02 18:47:19 +08003025typedef enum MigThrError {
3026 /* No error detected */
3027 MIG_THR_ERR_NONE = 0,
3028 /* Detected error, but resumed successfully */
3029 MIG_THR_ERR_RECOVERED = 1,
3030 /* Detected fatal error, need to exit */
3031 MIG_THR_ERR_FATAL = 2,
3032} MigThrError;
3033
Peter Xu94190692018-05-02 18:47:34 +08003034static int postcopy_resume_handshake(MigrationState *s)
3035{
3036 qemu_savevm_send_postcopy_resume(s->to_dst_file);
3037
3038 while (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
3039 qemu_sem_wait(&s->rp_state.rp_sem);
3040 }
3041
3042 if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
3043 return 0;
3044 }
3045
3046 return -1;
3047}
3048
Peter Xu135b87b2018-05-02 18:47:25 +08003049/* Return zero if success, or <0 for error */
3050static int postcopy_do_resume(MigrationState *s)
3051{
Peter Xud1b8ead2018-05-02 18:47:31 +08003052 int ret;
3053
3054 /*
3055 * Call all the resume_prepare() hooks, so that modules can be
3056 * ready for the migration resume.
3057 */
3058 ret = qemu_savevm_state_resume_prepare(s);
3059 if (ret) {
3060 error_report("%s: resume_prepare() failure detected: %d",
3061 __func__, ret);
3062 return ret;
3063 }
3064
3065 /*
Peter Xu94190692018-05-02 18:47:34 +08003066 * Last handshake with destination on the resume (destination will
3067 * switch to postcopy-active afterwards)
Peter Xud1b8ead2018-05-02 18:47:31 +08003068 */
Peter Xu94190692018-05-02 18:47:34 +08003069 ret = postcopy_resume_handshake(s);
3070 if (ret) {
3071 error_report("%s: handshake failed: %d", __func__, ret);
3072 return ret;
3073 }
Peter Xud1b8ead2018-05-02 18:47:31 +08003074
Peter Xu135b87b2018-05-02 18:47:25 +08003075 return 0;
3076}
3077
Peter Xub23c2ad2018-05-02 18:47:19 +08003078/*
3079 * We don't return until we are in a safe state to continue current
3080 * postcopy migration. Returns MIG_THR_ERR_RECOVERED if recovered, or
3081 * MIG_THR_ERR_FATAL if unrecovery failure happened.
3082 */
3083static MigThrError postcopy_pause(MigrationState *s)
3084{
3085 assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
Peter Xub23c2ad2018-05-02 18:47:19 +08003086
Peter Xu135b87b2018-05-02 18:47:25 +08003087 while (true) {
Peter Xu62df0662018-05-02 18:47:38 +08003088 QEMUFile *file;
3089
Peter Xu135b87b2018-05-02 18:47:25 +08003090 migrate_set_state(&s->state, s->state,
3091 MIGRATION_STATUS_POSTCOPY_PAUSED);
Peter Xub23c2ad2018-05-02 18:47:19 +08003092
Peter Xu135b87b2018-05-02 18:47:25 +08003093 /* Current channel is possibly broken. Release it. */
3094 assert(s->to_dst_file);
Peter Xu62df0662018-05-02 18:47:38 +08003095 qemu_mutex_lock(&s->qemu_file_lock);
3096 file = s->to_dst_file;
Peter Xu135b87b2018-05-02 18:47:25 +08003097 s->to_dst_file = NULL;
Peter Xu62df0662018-05-02 18:47:38 +08003098 qemu_mutex_unlock(&s->qemu_file_lock);
3099
3100 qemu_file_shutdown(file);
3101 qemu_fclose(file);
Peter Xub23c2ad2018-05-02 18:47:19 +08003102
Peter Xu135b87b2018-05-02 18:47:25 +08003103 error_report("Detected IO failure for postcopy. "
3104 "Migration paused.");
3105
3106 /*
3107 * We wait until things fixed up. Then someone will setup the
3108 * status back for us.
3109 */
3110 while (s->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
3111 qemu_sem_wait(&s->postcopy_pause_sem);
3112 }
3113
3114 if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
3115 /* Woken up by a recover procedure. Give it a shot */
3116
3117 /*
3118 * Firstly, let's wake up the return path now, with a new
3119 * return path channel.
3120 */
3121 qemu_sem_post(&s->postcopy_pause_rp_sem);
3122
3123 /* Do the resume logic */
3124 if (postcopy_do_resume(s) == 0) {
3125 /* Let's continue! */
3126 trace_postcopy_pause_continued();
3127 return MIG_THR_ERR_RECOVERED;
3128 } else {
3129 /*
3130 * Something wrong happened during the recovery, let's
3131 * pause again. Pause is always better than throwing
3132 * data away.
3133 */
3134 continue;
3135 }
3136 } else {
3137 /* This is not right... Time to quit. */
3138 return MIG_THR_ERR_FATAL;
3139 }
Peter Xub23c2ad2018-05-02 18:47:19 +08003140 }
Peter Xub23c2ad2018-05-02 18:47:19 +08003141}
3142
3143static MigThrError migration_detect_error(MigrationState *s)
3144{
3145 int ret;
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003146 int state = s->state;
Yury Kotov3d661c82019-04-22 13:34:20 +03003147 Error *local_error = NULL;
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003148
3149 if (state == MIGRATION_STATUS_CANCELLING ||
3150 state == MIGRATION_STATUS_CANCELLED) {
3151 /* End the migration, but don't set the state to failed */
3152 return MIG_THR_ERR_FATAL;
3153 }
Peter Xub23c2ad2018-05-02 18:47:19 +08003154
3155 /* Try to detect any file errors */
Yury Kotov3d661c82019-04-22 13:34:20 +03003156 ret = qemu_file_get_error_obj(s->to_dst_file, &local_error);
Peter Xub23c2ad2018-05-02 18:47:19 +08003157 if (!ret) {
3158 /* Everything is fine */
Yury Kotov3d661c82019-04-22 13:34:20 +03003159 assert(!local_error);
Peter Xub23c2ad2018-05-02 18:47:19 +08003160 return MIG_THR_ERR_NONE;
3161 }
3162
Yury Kotov3d661c82019-04-22 13:34:20 +03003163 if (local_error) {
3164 migrate_set_error(s, local_error);
3165 error_free(local_error);
3166 }
3167
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003168 if (state == MIGRATION_STATUS_POSTCOPY_ACTIVE && ret == -EIO) {
Peter Xub23c2ad2018-05-02 18:47:19 +08003169 /*
3170 * For postcopy, we allow the network to be down for a
3171 * while. After that, it can be continued by a
3172 * recovery phase.
3173 */
3174 return postcopy_pause(s);
3175 } else {
3176 /*
3177 * For precopy (or postcopy with error outside IO), we fail
3178 * with no time.
3179 */
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003180 migrate_set_state(&s->state, state, MIGRATION_STATUS_FAILED);
Peter Xub23c2ad2018-05-02 18:47:19 +08003181 trace_migration_thread_file_err();
3182
3183 /* Time to stop the migration, now. */
3184 return MIG_THR_ERR_FATAL;
3185 }
3186}
3187
Wei Yang268dcd42019-09-12 10:49:57 +08003188/* How many bytes have we transferred since the beginning of the migration */
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003189static uint64_t migration_total_bytes(MigrationState *s)
3190{
Juan Quintelab9ee2f72016-01-15 11:40:13 +01003191 return qemu_ftell(s->to_dst_file) + ram_counters.multifd_bytes;
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003192}
3193
Peter Xucf011f02018-01-03 20:20:11 +08003194static void migration_calculate_complete(MigrationState *s)
3195{
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003196 uint64_t bytes = migration_total_bytes(s);
Peter Xucf011f02018-01-03 20:20:11 +08003197 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela6cde6fb2018-06-26 15:26:35 +02003198 int64_t transfer_time;
Peter Xucf011f02018-01-03 20:20:11 +08003199
3200 s->total_time = end_time - s->start_time;
3201 if (!s->downtime) {
3202 /*
3203 * It's still not set, so we are precopy migration. For
3204 * postcopy, downtime is calculated during postcopy_start().
3205 */
3206 s->downtime = end_time - s->downtime_start;
3207 }
3208
Juan Quintela6cde6fb2018-06-26 15:26:35 +02003209 transfer_time = s->total_time - s->setup_time;
3210 if (transfer_time) {
3211 s->mbps = ((double) bytes * 8.0) / transfer_time / 1000;
Peter Xucf011f02018-01-03 20:20:11 +08003212 }
3213}
3214
Ivan Ren87f3bd82019-08-02 18:18:41 +08003215static void update_iteration_initial_status(MigrationState *s)
3216{
3217 /*
3218 * Update these three fields at the same time to avoid mismatch info lead
3219 * wrong speed calculation.
3220 */
3221 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3222 s->iteration_initial_bytes = migration_total_bytes(s);
3223 s->iteration_initial_pages = ram_get_total_transferred_pages();
3224}
3225
Peter Xub15df1a2018-01-03 20:20:13 +08003226static void migration_update_counters(MigrationState *s,
3227 int64_t current_time)
3228{
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08003229 uint64_t transferred, transferred_pages, time_spent;
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003230 uint64_t current_bytes; /* bytes transferred since the beginning */
Peter Xub15df1a2018-01-03 20:20:13 +08003231 double bandwidth;
3232
3233 if (current_time < s->iteration_start_time + BUFFER_DELAY) {
3234 return;
3235 }
3236
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003237 current_bytes = migration_total_bytes(s);
3238 transferred = current_bytes - s->iteration_initial_bytes;
Peter Xub15df1a2018-01-03 20:20:13 +08003239 time_spent = current_time - s->iteration_start_time;
3240 bandwidth = (double)transferred / time_spent;
Wei Wang0781c1e2018-01-22 19:36:39 +08003241 s->threshold_size = bandwidth * s->parameters.downtime_limit;
Peter Xub15df1a2018-01-03 20:20:13 +08003242
3243 s->mbps = (((double) transferred * 8.0) /
3244 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
3245
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08003246 transferred_pages = ram_get_total_transferred_pages() -
3247 s->iteration_initial_pages;
3248 s->pages_per_second = (double) transferred_pages /
3249 (((double) time_spent / 1000.0));
3250
Peter Xub15df1a2018-01-03 20:20:13 +08003251 /*
3252 * if we haven't sent anything, we don't want to
3253 * recalculate. 10000 is a small enough number for our purposes
3254 */
3255 if (ram_counters.dirty_pages_rate && transferred > 10000) {
Balamuruhan S650af892018-06-12 14:20:09 +05303256 s->expected_downtime = ram_counters.remaining / bandwidth;
Peter Xub15df1a2018-01-03 20:20:13 +08003257 }
3258
3259 qemu_file_reset_rate_limit(s->to_dst_file);
3260
Ivan Ren87f3bd82019-08-02 18:18:41 +08003261 update_iteration_initial_status(s);
Peter Xub15df1a2018-01-03 20:20:13 +08003262
3263 trace_migrate_transferred(transferred, time_spent,
Wei Wang0781c1e2018-01-22 19:36:39 +08003264 bandwidth, s->threshold_size);
Peter Xub15df1a2018-01-03 20:20:13 +08003265}
3266
Peter Xu2ad87302018-01-03 20:20:14 +08003267/* Migration thread iteration status */
3268typedef enum {
3269 MIG_ITERATE_RESUME, /* Resume current iteration */
3270 MIG_ITERATE_SKIP, /* Skip current iteration */
3271 MIG_ITERATE_BREAK, /* Break the loop */
3272} MigIterateState;
3273
3274/*
3275 * Return true if continue to the next iteration directly, false
3276 * otherwise.
3277 */
3278static MigIterateState migration_iteration_run(MigrationState *s)
3279{
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04003280 uint64_t pending_size, pend_pre, pend_compat, pend_post;
Peter Xu2ad87302018-01-03 20:20:14 +08003281 bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE;
3282
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04003283 qemu_savevm_state_pending(s->to_dst_file, s->threshold_size, &pend_pre,
3284 &pend_compat, &pend_post);
3285 pending_size = pend_pre + pend_compat + pend_post;
Peter Xu2ad87302018-01-03 20:20:14 +08003286
3287 trace_migrate_pending(pending_size, s->threshold_size,
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04003288 pend_pre, pend_compat, pend_post);
Peter Xu2ad87302018-01-03 20:20:14 +08003289
3290 if (pending_size && pending_size >= s->threshold_size) {
3291 /* Still a significant amount to transfer */
Wei Yang52aec702019-07-18 16:37:47 +08003292 if (!in_postcopy && pend_pre <= s->threshold_size &&
Peter Xu2ad87302018-01-03 20:20:14 +08003293 atomic_read(&s->start_postcopy)) {
3294 if (postcopy_start(s)) {
3295 error_report("%s: postcopy failed to start", __func__);
3296 }
3297 return MIG_ITERATE_SKIP;
3298 }
3299 /* Just another iteration step */
Wei Yang17d93512019-10-06 06:05:16 +08003300 qemu_savevm_state_iterate(s->to_dst_file, in_postcopy);
Peter Xu2ad87302018-01-03 20:20:14 +08003301 } else {
3302 trace_migration_thread_low_pending(pending_size);
3303 migration_completion(s);
3304 return MIG_ITERATE_BREAK;
3305 }
3306
3307 return MIG_ITERATE_RESUME;
3308}
3309
Peter Xu199aa6d2018-01-03 20:20:15 +08003310static void migration_iteration_finish(MigrationState *s)
3311{
3312 /* If we enabled cpu throttling for auto-converge, turn it off. */
3313 cpu_throttle_stop();
3314
3315 qemu_mutex_lock_iothread();
3316 switch (s->state) {
3317 case MIGRATION_STATUS_COMPLETED:
3318 migration_calculate_complete(s);
3319 runstate_set(RUN_STATE_POSTMIGRATE);
3320 break;
3321
3322 case MIGRATION_STATUS_ACTIVE:
3323 /*
3324 * We should really assert here, but since it's during
3325 * migration, let's try to reduce the usage of assertions.
3326 */
3327 if (!migrate_colo_enabled()) {
3328 error_report("%s: critical error: calling COLO code without "
3329 "COLO enabled", __func__);
3330 }
3331 migrate_start_colo_process(s);
3332 /*
3333 * Fixme: we will run VM in COLO no matter its old running state.
3334 * After exited COLO, we will keep running.
3335 */
3336 s->vm_was_running = true;
3337 /* Fallthrough */
3338 case MIGRATION_STATUS_FAILED:
3339 case MIGRATION_STATUS_CANCELLED:
Dr. David Alan Gilbert57225e52018-07-19 10:22:57 +01003340 case MIGRATION_STATUS_CANCELLING:
Peter Xu199aa6d2018-01-03 20:20:15 +08003341 if (s->vm_was_running) {
3342 vm_start();
3343 } else {
3344 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
3345 runstate_set(RUN_STATE_POSTMIGRATE);
3346 }
3347 }
3348 break;
3349
3350 default:
3351 /* Should not reach here, but if so, forgive the VM. */
3352 error_report("%s: Unknown ending state %d", __func__, s->state);
3353 break;
3354 }
Yury Kotovfd392cf2019-04-08 14:33:43 +03003355 migrate_fd_cleanup_schedule(s);
Peter Xu199aa6d2018-01-03 20:20:15 +08003356 qemu_mutex_unlock_iothread();
3357}
3358
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003359void migration_make_urgent_request(void)
3360{
3361 qemu_sem_post(&migrate_get_current()->rate_limit_sem);
3362}
3363
3364void migration_consume_urgent_request(void)
3365{
3366 qemu_sem_wait(&migrate_get_current()->rate_limit_sem);
3367}
3368
Dr. David Alan Gilbert97e1e062019-12-05 10:29:18 +00003369/* Returns true if the rate limiting was broken by an urgent request */
3370bool migration_rate_limit(void)
3371{
3372 int64_t now = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3373 MigrationState *s = migrate_get_current();
3374
3375 bool urgent = false;
3376 migration_update_counters(s, now);
3377 if (qemu_file_rate_limit(s->to_dst_file)) {
Lukas Straub77386122020-05-20 22:42:32 +02003378
3379 if (qemu_file_get_error(s->to_dst_file)) {
3380 return false;
3381 }
Dr. David Alan Gilbert97e1e062019-12-05 10:29:18 +00003382 /*
3383 * Wait for a delay to do rate limiting OR
3384 * something urgent to post the semaphore.
3385 */
3386 int ms = s->iteration_start_time + BUFFER_DELAY - now;
3387 trace_migration_rate_limit_pre(ms);
3388 if (qemu_sem_timedwait(&s->rate_limit_sem, ms) == 0) {
3389 /*
3390 * We were woken by one or more urgent things but
3391 * the timedwait will have consumed one of them.
3392 * The service routine for the urgent wake will dec
3393 * the semaphore itself for each item it consumes,
3394 * so add this one we just eat back.
3395 */
3396 qemu_sem_post(&s->rate_limit_sem);
3397 urgent = true;
3398 }
3399 trace_migration_rate_limit_post(urgent);
3400 }
3401 return urgent;
3402}
3403
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00003404/*
3405 * Master migration thread on the source VM.
3406 * It drives the migration and pumps the data down the outgoing channel.
3407 */
Juan Quintela5f496a12013-02-22 17:36:30 +01003408static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003409{
Juan Quintela9848a402012-12-19 09:55:50 +01003410 MigrationState *s = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +01003411 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Peter Xub23c2ad2018-05-02 18:47:19 +08003412 MigThrError thr_error;
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003413 bool urgent = false;
Juan Quintela76f59332012-10-03 20:16:24 +02003414
Paolo Bonziniab28bd22015-07-09 08:55:38 +02003415 rcu_register_thread();
3416
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +00003417 object_ref(OBJECT(s));
Ivan Ren87f3bd82019-08-02 18:18:41 +08003418 update_iteration_initial_status(s);
Peter Xub15df1a2018-01-03 20:20:13 +08003419
zhanghailiang89a02a92016-01-15 11:37:42 +08003420 qemu_savevm_state_header(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003421
Peter Xu62a02652017-06-14 15:55:58 +08003422 /*
3423 * If we opened the return path, we need to make sure dst has it
3424 * opened as well.
3425 */
3426 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003427 /* Now tell the dest that it should open its end so it can reply */
zhanghailiang89a02a92016-01-15 11:37:42 +08003428 qemu_savevm_send_open_return_path(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003429
3430 /* And do a ping that will make stuff easier to debug */
zhanghailiang89a02a92016-01-15 11:37:42 +08003431 qemu_savevm_send_ping(s->to_dst_file, 1);
Peter Xu0425dc92017-05-31 18:35:34 +08003432 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003433
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03003434 if (migrate_postcopy()) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003435 /*
3436 * Tell the destination that we *might* want to do postcopy later;
3437 * if the other end can't do postcopy it should fail now, nice and
3438 * early.
3439 */
zhanghailiang89a02a92016-01-15 11:37:42 +08003440 qemu_savevm_send_postcopy_advise(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003441 }
3442
Zhang Chenaad555c2018-09-03 12:38:47 +08003443 if (migrate_colo_enabled()) {
3444 /* Notify migration destination that we enable COLO */
3445 qemu_savevm_send_colo_enable(s->to_dst_file);
3446 }
3447
Juan Quintela9907e842017-06-28 11:52:24 +02003448 qemu_savevm_state_setup(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003449
Keqian Zhud05de9e2020-02-04 13:08:41 +08003450 if (qemu_savevm_state_guest_unplug_pending()) {
Jens Freimannc7e0acd2019-10-29 12:49:02 +01003451 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3452 MIGRATION_STATUS_WAIT_UNPLUG);
3453
3454 while (s->state == MIGRATION_STATUS_WAIT_UNPLUG &&
3455 qemu_savevm_state_guest_unplug_pending()) {
3456 qemu_sem_timedwait(&s->wait_unplug_sem, 250);
3457 }
3458
3459 migrate_set_state(&s->state, MIGRATION_STATUS_WAIT_UNPLUG,
3460 MIGRATION_STATUS_ACTIVE);
3461 }
3462
Alex Blighbc72ad62013-08-21 16:03:08 +01003463 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang48781e52015-12-16 11:47:33 +00003464 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3465 MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04003466
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00003467 trace_migration_thread_setup_complete();
3468
Wei Yang8f8d5282019-07-17 08:53:41 +08003469 while (migration_is_active(s)) {
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003470 if (urgent || !qemu_file_rate_limit(s->to_dst_file)) {
Peter Xu2ad87302018-01-03 20:20:14 +08003471 MigIterateState iter_state = migration_iteration_run(s);
3472 if (iter_state == MIG_ITERATE_SKIP) {
3473 continue;
3474 } else if (iter_state == MIG_ITERATE_BREAK) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003475 break;
Juan Quintelac369f402012-10-03 20:33:34 +02003476 }
3477 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01003478
Peter Xub23c2ad2018-05-02 18:47:19 +08003479 /*
3480 * Try to detect any kind of failures, and see whether we
3481 * should stop the migration now.
3482 */
3483 thr_error = migration_detect_error(s);
3484 if (thr_error == MIG_THR_ERR_FATAL) {
3485 /* Stop migration */
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01003486 break;
Peter Xub23c2ad2018-05-02 18:47:19 +08003487 } else if (thr_error == MIG_THR_ERR_RECOVERED) {
3488 /*
3489 * Just recovered from a e.g. network failure, reset all
3490 * the local variables. This is important to avoid
3491 * breaking transferred_bytes and bandwidth calculation
3492 */
Ivan Ren87f3bd82019-08-02 18:18:41 +08003493 update_iteration_initial_status(s);
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01003494 }
Peter Xub15df1a2018-01-03 20:20:13 +08003495
Dr. David Alan Gilbert97e1e062019-12-05 10:29:18 +00003496 urgent = migration_rate_limit();
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01003497 }
3498
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003499 trace_migration_thread_after_loop();
Peter Xu199aa6d2018-01-03 20:20:15 +08003500 migration_iteration_finish(s);
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +00003501 object_unref(OBJECT(s));
Paolo Bonziniab28bd22015-07-09 08:55:38 +02003502 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003503 return NULL;
3504}
3505
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00003506void migrate_fd_connect(MigrationState *s, Error *error_in)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003507{
Juan Quintela00f4b572019-06-12 11:33:27 +02003508 Error *local_err = NULL;
Peter Xud3e35b82018-05-02 18:47:24 +08003509 int64_t rate_limit;
3510 bool resume = s->state == MIGRATION_STATUS_POSTCOPY_PAUSED;
3511
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05303512 s->expected_downtime = s->parameters.downtime_limit;
Marc-André Lureau9cbc3642020-03-25 19:47:21 +01003513 if (resume) {
3514 assert(s->cleanup_bh);
3515 } else {
3516 assert(!s->cleanup_bh);
3517 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup_bh, s);
3518 }
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00003519 if (error_in) {
3520 migrate_fd_error(s, error_in);
3521 migrate_fd_cleanup(s);
3522 return;
3523 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003524
Peter Xud3e35b82018-05-02 18:47:24 +08003525 if (resume) {
3526 /* This is a resumed migration */
Peter Xu8504dde2019-09-06 21:01:03 +08003527 rate_limit = s->parameters.max_postcopy_bandwidth /
3528 XFER_LIMIT_RATIO;
Peter Xud3e35b82018-05-02 18:47:24 +08003529 } else {
3530 /* This is a fresh new migration */
3531 rate_limit = s->parameters.max_bandwidth / XFER_LIMIT_RATIO;
Paolo Bonzini442773c2013-02-22 17:36:44 +01003532
Peter Xud3e35b82018-05-02 18:47:24 +08003533 /* Notify before starting migration thread */
3534 notifier_list_notify(&migration_state_notifiers, s);
3535 }
3536
3537 qemu_file_set_rate_limit(s->to_dst_file, rate_limit);
3538 qemu_file_set_blocking(s->to_dst_file, true);
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02003539
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003540 /*
Peter Xuc788ada2017-06-26 18:28:55 +08003541 * Open the return path. For postcopy, it is used exclusively. For
3542 * precopy, only if user specified "return-path" capability would
3543 * QEMU uses the return path.
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003544 */
Peter Xuc788ada2017-06-26 18:28:55 +08003545 if (migrate_postcopy_ram() || migrate_use_return_path()) {
Peter Xud3e35b82018-05-02 18:47:24 +08003546 if (open_return_path_on_source(s, !resume)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003547 error_report("Unable to open return-path for postcopy");
Peter Xud3e35b82018-05-02 18:47:24 +08003548 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003549 migrate_fd_cleanup(s);
3550 return;
3551 }
3552 }
3553
Peter Xud3e35b82018-05-02 18:47:24 +08003554 if (resume) {
Peter Xu135b87b2018-05-02 18:47:25 +08003555 /* Wakeup the main migration thread to do the recovery */
3556 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
3557 MIGRATION_STATUS_POSTCOPY_RECOVER);
3558 qemu_sem_post(&s->postcopy_pause_sem);
Peter Xud3e35b82018-05-02 18:47:24 +08003559 return;
3560 }
3561
Juan Quintela00f4b572019-06-12 11:33:27 +02003562 if (multifd_save_setup(&local_err) != 0) {
3563 error_report_err(local_err);
Juan Quintelaf986c3d2016-01-14 16:52:55 +01003564 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3565 MIGRATION_STATUS_FAILED);
3566 migrate_fd_cleanup(s);
3567 return;
3568 }
Pankaj Gupta009fad72017-01-23 19:12:56 +05303569 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01003570 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003571 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003572}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00003573
Peter Xu9d18af92017-06-27 12:10:19 +08003574void migration_global_dump(Monitor *mon)
3575{
3576 MigrationState *ms = migrate_get_current();
3577
Juan Quintela6f0f6422017-10-26 11:49:57 +02003578 monitor_printf(mon, "globals:\n");
3579 monitor_printf(mon, "store-global-state: %s\n",
3580 ms->store_global_state ? "on" : "off");
3581 monitor_printf(mon, "only-migratable: %s\n",
Markus Armbruster811f8652019-04-01 11:08:24 +02003582 only_migratable ? "on" : "off");
Juan Quintela6f0f6422017-10-26 11:49:57 +02003583 monitor_printf(mon, "send-configuration: %s\n",
3584 ms->send_configuration ? "on" : "off");
3585 monitor_printf(mon, "send-section-footer: %s\n",
3586 ms->send_section_footer ? "on" : "off");
Xiao Guangrongf5482222018-05-03 16:06:11 +08003587 monitor_printf(mon, "decompress-error-check: %s\n",
3588 ms->decompress_error_check ? "on" : "off");
Peter Xu002cad62019-06-03 14:50:56 +08003589 monitor_printf(mon, "clear-bitmap-shift: %u\n",
3590 ms->clear_bitmap_shift);
Peter Xu9d18af92017-06-27 12:10:19 +08003591}
3592
Peter Xu20814752017-07-18 11:39:03 +08003593#define DEFINE_PROP_MIG_CAP(name, x) \
3594 DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
3595
Peter Xu52722982017-06-27 12:10:14 +08003596static Property migration_properties[] = {
3597 DEFINE_PROP_BOOL("store-global-state", MigrationState,
3598 store_global_state, true),
Peter Xu71dd4c12017-06-27 12:10:16 +08003599 DEFINE_PROP_BOOL("send-configuration", MigrationState,
3600 send_configuration, true),
Peter Xu15c38502017-06-27 12:10:17 +08003601 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
3602 send_section_footer, true),
Xiao Guangrongf5482222018-05-03 16:06:11 +08003603 DEFINE_PROP_BOOL("decompress-error-check", MigrationState,
3604 decompress_error_check, true),
Peter Xu002cad62019-06-03 14:50:56 +08003605 DEFINE_PROP_UINT8("x-clear-bitmap-shift", MigrationState,
3606 clear_bitmap_shift, CLEAR_BITMAP_SHIFT_DEFAULT),
Peter Xu89632fa2017-07-18 11:39:02 +08003607
3608 /* Migration parameters */
Juan Quintela741d4082017-12-01 13:08:38 +01003609 DEFINE_PROP_UINT8("x-compress-level", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003610 parameters.compress_level,
3611 DEFAULT_MIGRATE_COMPRESS_LEVEL),
Juan Quintela741d4082017-12-01 13:08:38 +01003612 DEFINE_PROP_UINT8("x-compress-threads", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003613 parameters.compress_threads,
3614 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
Xiao Guangrong1d588722018-08-21 16:10:20 +08003615 DEFINE_PROP_BOOL("x-compress-wait-thread", MigrationState,
3616 parameters.compress_wait_thread, true),
Juan Quintela741d4082017-12-01 13:08:38 +01003617 DEFINE_PROP_UINT8("x-decompress-threads", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003618 parameters.decompress_threads,
3619 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
Keqian Zhudc14a472020-02-24 10:31:42 +08003620 DEFINE_PROP_UINT8("x-throttle-trigger-threshold", MigrationState,
3621 parameters.throttle_trigger_threshold,
3622 DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD),
Juan Quintela741d4082017-12-01 13:08:38 +01003623 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003624 parameters.cpu_throttle_initial,
3625 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
Juan Quintela741d4082017-12-01 13:08:38 +01003626 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003627 parameters.cpu_throttle_increment,
3628 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
Keqian Zhucbbf8182020-04-13 18:15:08 +08003629 DEFINE_PROP_BOOL("x-cpu-throttle-tailslow", MigrationState,
3630 parameters.cpu_throttle_tailslow, false),
Juan Quintela741d4082017-12-01 13:08:38 +01003631 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003632 parameters.max_bandwidth, MAX_THROTTLE),
Juan Quintela741d4082017-12-01 13:08:38 +01003633 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003634 parameters.downtime_limit,
3635 DEFAULT_MIGRATE_SET_DOWNTIME),
Juan Quintela741d4082017-12-01 13:08:38 +01003636 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003637 parameters.x_checkpoint_delay,
3638 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
Juan Quintelacbfd6c92019-02-06 13:54:06 +01003639 DEFINE_PROP_UINT8("multifd-channels", MigrationState,
3640 parameters.multifd_channels,
Juan Quintela4075fb12016-01-15 08:56:17 +01003641 DEFAULT_MIGRATE_MULTIFD_CHANNELS),
Juan Quintela96eef042019-01-16 10:35:55 +01003642 DEFINE_PROP_MULTIFD_COMPRESSION("multifd-compression", MigrationState,
3643 parameters.multifd_compression,
3644 DEFAULT_MIGRATE_MULTIFD_COMPRESSION),
Juan Quintela9004db42020-01-23 17:08:52 +01003645 DEFINE_PROP_UINT8("multifd-zlib-level", MigrationState,
3646 parameters.multifd_zlib_level,
3647 DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL),
Juan Quintela6a9ad152020-01-23 17:41:36 +01003648 DEFINE_PROP_UINT8("multifd-zstd-level", MigrationState,
3649 parameters.multifd_zstd_level,
3650 DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL),
Juan Quintela73af8dd2017-10-05 21:30:10 +02003651 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
3652 parameters.xbzrle_cache_size,
3653 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01003654 DEFINE_PROP_SIZE("max-postcopy-bandwidth", MigrationState,
3655 parameters.max_postcopy_bandwidth,
3656 DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH),
Li Qiang4cbc9c72018-08-01 06:00:20 -07003657 DEFINE_PROP_UINT8("max-cpu-throttle", MigrationState,
3658 parameters.max_cpu_throttle,
3659 DEFAULT_MIGRATE_MAX_CPU_THROTTLE),
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00003660 DEFINE_PROP_SIZE("announce-initial", MigrationState,
3661 parameters.announce_initial,
3662 DEFAULT_MIGRATE_ANNOUNCE_INITIAL),
3663 DEFINE_PROP_SIZE("announce-max", MigrationState,
3664 parameters.announce_max,
3665 DEFAULT_MIGRATE_ANNOUNCE_MAX),
3666 DEFINE_PROP_SIZE("announce-rounds", MigrationState,
3667 parameters.announce_rounds,
3668 DEFAULT_MIGRATE_ANNOUNCE_ROUNDS),
3669 DEFINE_PROP_SIZE("announce-step", MigrationState,
3670 parameters.announce_step,
3671 DEFAULT_MIGRATE_ANNOUNCE_STEP),
Peter Xu20814752017-07-18 11:39:03 +08003672
3673 /* Migration capabilities */
3674 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
3675 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
3676 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
3677 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
3678 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
3679 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
3680 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
3681 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
3682 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
3683 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
3684 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
Juan Quintelacbfd6c92019-02-06 13:54:06 +01003685 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_MULTIFD),
Peter Xu20814752017-07-18 11:39:03 +08003686
Peter Xu52722982017-06-27 12:10:14 +08003687 DEFINE_PROP_END_OF_LIST(),
3688};
3689
Peter Xue5cb7e72017-06-27 12:10:13 +08003690static void migration_class_init(ObjectClass *klass, void *data)
3691{
3692 DeviceClass *dc = DEVICE_CLASS(klass);
3693
3694 dc->user_creatable = false;
Marc-André Lureau4f67d302020-01-10 19:30:32 +04003695 device_class_set_props(dc, migration_properties);
Peter Xue5cb7e72017-06-27 12:10:13 +08003696}
3697
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003698static void migration_instance_finalize(Object *obj)
3699{
3700 MigrationState *ms = MIGRATION_OBJ(obj);
3701 MigrationParameters *params = &ms->parameters;
3702
Juan Quintela87db1a72017-09-05 12:50:22 +02003703 qemu_mutex_destroy(&ms->error_mutex);
Peter Xu62df0662018-05-02 18:47:38 +08003704 qemu_mutex_destroy(&ms->qemu_file_lock);
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003705 g_free(params->tls_hostname);
3706 g_free(params->tls_creds);
Jens Freimannc7e0acd2019-10-29 12:49:02 +01003707 qemu_sem_destroy(&ms->wait_unplug_sem);
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003708 qemu_sem_destroy(&ms->rate_limit_sem);
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01003709 qemu_sem_destroy(&ms->pause_sem);
Peter Xub23c2ad2018-05-02 18:47:19 +08003710 qemu_sem_destroy(&ms->postcopy_pause_sem);
Peter Xu14b17422018-05-02 18:47:21 +08003711 qemu_sem_destroy(&ms->postcopy_pause_rp_sem);
Peter Xuedd090c2018-05-02 18:47:32 +08003712 qemu_sem_destroy(&ms->rp_state.rp_sem);
Marc-André Lureauab105cc2018-03-06 18:09:59 +01003713 error_free(ms->error);
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003714}
3715
Peter Xue5cb7e72017-06-27 12:10:13 +08003716static void migration_instance_init(Object *obj)
3717{
3718 MigrationState *ms = MIGRATION_OBJ(obj);
Peter Xu8b0b29d2017-07-18 11:39:06 +08003719 MigrationParameters *params = &ms->parameters;
Peter Xue5cb7e72017-06-27 12:10:13 +08003720
3721 ms->state = MIGRATION_STATUS_NONE;
Peter Xue5cb7e72017-06-27 12:10:13 +08003722 ms->mbps = -1;
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08003723 ms->pages_per_second = -1;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01003724 qemu_sem_init(&ms->pause_sem, 0);
Juan Quintela87db1a72017-09-05 12:50:22 +02003725 qemu_mutex_init(&ms->error_mutex);
Peter Xu8b0b29d2017-07-18 11:39:06 +08003726
3727 params->tls_hostname = g_strdup("");
3728 params->tls_creds = g_strdup("");
3729
3730 /* Set has_* up only for parameter checks */
3731 params->has_compress_level = true;
3732 params->has_compress_threads = true;
3733 params->has_decompress_threads = true;
Keqian Zhudc14a472020-02-24 10:31:42 +08003734 params->has_throttle_trigger_threshold = true;
Peter Xu8b0b29d2017-07-18 11:39:06 +08003735 params->has_cpu_throttle_initial = true;
3736 params->has_cpu_throttle_increment = true;
Keqian Zhucbbf8182020-04-13 18:15:08 +08003737 params->has_cpu_throttle_tailslow = true;
Peter Xu8b0b29d2017-07-18 11:39:06 +08003738 params->has_max_bandwidth = true;
3739 params->has_downtime_limit = true;
3740 params->has_x_checkpoint_delay = true;
3741 params->has_block_incremental = true;
Juan Quintelacbfd6c92019-02-06 13:54:06 +01003742 params->has_multifd_channels = true;
Juan Quintela96eef042019-01-16 10:35:55 +01003743 params->has_multifd_compression = true;
Juan Quintela9004db42020-01-23 17:08:52 +01003744 params->has_multifd_zlib_level = true;
Juan Quintela6a9ad152020-01-23 17:41:36 +01003745 params->has_multifd_zstd_level = true;
Juan Quintela73af8dd2017-10-05 21:30:10 +02003746 params->has_xbzrle_cache_size = true;
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01003747 params->has_max_postcopy_bandwidth = true;
Li Qiang4cbc9c72018-08-01 06:00:20 -07003748 params->has_max_cpu_throttle = true;
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00003749 params->has_announce_initial = true;
3750 params->has_announce_max = true;
3751 params->has_announce_rounds = true;
3752 params->has_announce_step = true;
Peter Xub23c2ad2018-05-02 18:47:19 +08003753
3754 qemu_sem_init(&ms->postcopy_pause_sem, 0);
Peter Xu14b17422018-05-02 18:47:21 +08003755 qemu_sem_init(&ms->postcopy_pause_rp_sem, 0);
Peter Xuedd090c2018-05-02 18:47:32 +08003756 qemu_sem_init(&ms->rp_state.rp_sem, 0);
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003757 qemu_sem_init(&ms->rate_limit_sem, 0);
Jens Freimannc7e0acd2019-10-29 12:49:02 +01003758 qemu_sem_init(&ms->wait_unplug_sem, 0);
Peter Xu62df0662018-05-02 18:47:38 +08003759 qemu_mutex_init(&ms->qemu_file_lock);
Peter Xu8b0b29d2017-07-18 11:39:06 +08003760}
3761
3762/*
3763 * Return true if check pass, false otherwise. Error will be put
3764 * inside errp if provided.
3765 */
3766static bool migration_object_check(MigrationState *ms, Error **errp)
3767{
Peter Xu6b19a7d2017-07-18 11:39:10 +08003768 MigrationCapabilityStatusList *head = NULL;
3769 /* Assuming all off */
3770 bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret;
3771 int i;
3772
Peter Xu8b0b29d2017-07-18 11:39:06 +08003773 if (!migrate_params_check(&ms->parameters, errp)) {
3774 return false;
3775 }
3776
Peter Xu6b19a7d2017-07-18 11:39:10 +08003777 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
3778 if (ms->enabled_capabilities[i]) {
3779 head = migrate_cap_add(head, i, true);
3780 }
3781 }
3782
3783 ret = migrate_caps_check(cap_list, head, errp);
3784
3785 /* It works with head == NULL */
3786 qapi_free_MigrationCapabilityStatusList(head);
3787
3788 return ret;
Peter Xue5cb7e72017-06-27 12:10:13 +08003789}
3790
3791static const TypeInfo migration_type = {
3792 .name = TYPE_MIGRATION,
Peter Xu01f6e142017-06-28 15:15:44 +08003793 /*
Peter Xuc8d3ff32017-07-05 16:21:23 +08003794 * NOTE: TYPE_MIGRATION is not really a device, as the object is
Markus Armbruster2194abd2020-06-10 07:32:19 +02003795 * not created using qdev_new(), it is not attached to the qdev
Peter Xuc8d3ff32017-07-05 16:21:23 +08003796 * device tree, and it is never realized.
3797 *
3798 * TODO: Make this TYPE_OBJECT once QOM provides something like
3799 * TYPE_DEVICE's "-global" properties.
Peter Xu01f6e142017-06-28 15:15:44 +08003800 */
Peter Xue5cb7e72017-06-27 12:10:13 +08003801 .parent = TYPE_DEVICE,
3802 .class_init = migration_class_init,
3803 .class_size = sizeof(MigrationClass),
3804 .instance_size = sizeof(MigrationState),
3805 .instance_init = migration_instance_init,
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003806 .instance_finalize = migration_instance_finalize,
Peter Xue5cb7e72017-06-27 12:10:13 +08003807};
3808
3809static void register_migration_types(void)
3810{
3811 type_register_static(&migration_type);
3812}
3813
3814type_init(register_migration_types);