blob: 4b26110d57a15daac4aee0993e39dc20fc05ec43 [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"
Juan Quintelae1a3ece2017-04-17 20:32:36 +020026#include "rdma.h"
Juan Quintela7b1e1a22017-04-17 20:26:27 +020027#include "ram.h"
Juan Quintela84a899d2017-04-24 18:53:30 +020028#include "migration/global_state.h"
Juan Quintelac4b63b72017-04-24 19:02:44 +020029#include "migration/misc.h"
Juan Quintela6666c962017-04-24 20:07:27 +020030#include "migration.h"
Juan Quintela20a519a2017-04-20 14:48:46 +020031#include "savevm.h"
Juan Quintela40014d82017-04-17 19:34:36 +020032#include "qemu-file-channel.h"
Juan Quintela08a0aee2017-04-20 18:52:18 +020033#include "qemu-file.h"
Juan Quintela987772d2017-04-17 19:02:59 +020034#include "migration/vmstate.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010035#include "block/block.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010036#include "qapi/error.h"
Juan Quintela9aca82b2019-02-27 11:51:27 +010037#include "qapi/clone-visitor.h"
38#include "qapi/qapi-visit-sockets.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010039#include "qapi/qapi-commands-migration.h"
40#include "qapi/qapi-events-migration.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010041#include "qapi/qmp/qerror.h"
Markus Armbruster15280c32018-02-01 12:18:36 +010042#include "qapi/qmp/qnull.h"
Paolo Bonziniab28bd22015-07-09 08:55:38 +020043#include "qemu/rcu.h"
Juan Quintela2c9e6fe2017-04-21 14:31:22 +020044#include "block.h"
Juan Quintelabe07b0a2017-04-20 13:12:24 +020045#include "postcopy-ram.h"
Juan Quintela766bd172012-07-23 05:45:29 +020046#include "qemu/thread.h"
Kazuya Saitoc09e5bb2013-02-22 17:36:19 +010047#include "trace.h"
Juan Quintela51180422017-04-24 20:50:19 +020048#include "exec/target_page.h"
Daniel P. Berrange61b67d42016-04-27 11:05:01 +010049#include "io/channel-buffer.h"
zhanghailiang35a6ed42016-10-27 14:42:52 +080050#include "migration/colo.h"
Peter Xu4ffdb332017-06-27 12:10:18 +080051#include "hw/boards.h"
Markus Armbrustera27bd6c2019-08-12 07:23:51 +020052#include "hw/qdev-properties.h"
Peter Xu9d18af92017-06-27 12:10:19 +080053#include "monitor/monitor.h"
Dr. David Alan Gilbert50510ea2019-02-27 13:24:05 +000054#include "net/announce.h"
Jens Freimannc7e0acd2019-10-29 12:49:02 +010055#include "qemu/queue.h"
Juan Quintelad32ca5a2020-01-22 16:16:07 +010056#include "multifd.h"
aliguori065e2812008-11-11 16:46:33 +000057
Jason J. Hernedc325622015-09-08 13:12:37 -040058#define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000059
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010060/* Amount of time to allocate to each "chunk" of bandwidth-throttled
61 * data. */
62#define BUFFER_DELAY 100
63#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
64
Ashijeet Acharya2ff30252016-09-15 21:50:28 +053065/* Time in milliseconds we are allowed to stop the source,
66 * for sending the last part */
67#define DEFAULT_MIGRATE_SET_DOWNTIME 300
68
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -030069/* Maximum migrate downtime set to 2000 seconds */
70#define MAX_MIGRATE_DOWNTIME_SECONDS 2000
71#define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
72
Liang Li8706d2d2015-03-23 16:32:17 +080073/* Default compression thread count */
74#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080075/* Default decompression thread count, usually decompression is at
76 * least 4 times as fast as compression.*/
77#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080078/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
79#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
Jason J. Herne1626fee2015-09-08 13:12:34 -040080/* Define default autoconverge cpu throttle migration parameters */
Keqian Zhudc14a472020-02-24 10:31:42 +080081#define DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD 50
Jason J. Herned85a31d2016-04-21 14:07:18 -040082#define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
83#define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
Li Qiang4cbc9c72018-08-01 06:00:20 -070084#define DEFAULT_MIGRATE_MAX_CPU_THROTTLE 99
Liang Li8706d2d2015-03-23 16:32:17 +080085
Orit Wasserman17ad9b32012-08-06 21:42:53 +030086/* Migration XBZRLE default cache size */
Juan Quintela73af8dd2017-10-05 21:30:10 +020087#define DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE (64 * 1024 * 1024)
Orit Wasserman17ad9b32012-08-06 21:42:53 +030088
Zhang Chen131b2152018-09-03 12:38:45 +080089/* The delay time (in ms) between two COLO checkpoints */
90#define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY (200 * 100)
Juan Quintela4075fb12016-01-15 08:56:17 +010091#define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2
Juan Quintela96eef042019-01-16 10:35:55 +010092#define DEFAULT_MIGRATE_MULTIFD_COMPRESSION MULTIFD_COMPRESSION_NONE
Juan Quintela9004db42020-01-23 17:08:52 +010093/* 0: means nocompress, 1: best speed, ... 9: best compress ratio */
94#define DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL 1
Juan Quintela6a9ad152020-01-23 17:41:36 +010095/* 0: means nocompress, 1: best speed, ... 20: best compress ratio */
96#define DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL 1
zhanghailiang68b53592016-10-27 14:43:01 +080097
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +010098/* Background transfer rate for postcopy, 0 means unlimited, note
99 * that page requests can still exceed this limit.
100 */
101#define DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH 0
102
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +0000103/*
104 * Parameters for self_announce_delay giving a stream of RARP/ARP
105 * packets after migration.
106 */
107#define DEFAULT_MIGRATE_ANNOUNCE_INITIAL 50
108#define DEFAULT_MIGRATE_ANNOUNCE_MAX 550
109#define DEFAULT_MIGRATE_ANNOUNCE_ROUNDS 5
110#define DEFAULT_MIGRATE_ANNOUNCE_STEP 100
111
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100112static NotifierList migration_state_notifiers =
113 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
114
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000115static bool deferred_incoming;
116
Juan Quintelada6f1792017-04-24 17:37:14 +0200117/* Messages sent on the return path from destination to source */
118enum mig_rp_message_type {
119 MIG_RP_MSG_INVALID = 0, /* Must be 0 */
120 MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */
121 MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */
122
123 MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */
124 MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */
Peter Xua335deb2018-05-02 18:47:28 +0800125 MIG_RP_MSG_RECV_BITMAP, /* send recved_bitmap back to source */
Peter Xu13955b82018-05-02 18:47:30 +0800126 MIG_RP_MSG_RESUME_ACK, /* tell source that we are ready to resume */
Juan Quintelada6f1792017-04-24 17:37:14 +0200127
128 MIG_RP_MSG_MAX
129};
130
Juan Quintela17549e82011-10-05 13:50:43 +0200131/* When we add fault tolerance, we could have several
132 migrations at once. For now we don't need to add
133 dynamic creation of migration */
134
Peter Xue5cb7e72017-06-27 12:10:13 +0800135static MigrationState *current_migration;
Peter Xue1b1b1b2018-05-02 18:47:35 +0800136static MigrationIncomingState *current_incoming;
Peter Xue5cb7e72017-06-27 12:10:13 +0800137
Peter Xu8b0b29d2017-07-18 11:39:06 +0800138static bool migration_object_check(MigrationState *ms, Error **errp);
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +0100139static int migration_maybe_pause(MigrationState *s,
140 int *current_active_state,
141 int new_state);
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +0000142static void migrate_fd_cancel(MigrationState *s);
Peter Xu8b0b29d2017-07-18 11:39:06 +0800143
Peter Xue5cb7e72017-06-27 12:10:13 +0800144void migration_object_init(void)
145{
Peter Xu4ffdb332017-06-27 12:10:18 +0800146 MachineState *ms = MACHINE(qdev_get_machine());
Peter Xu8b0b29d2017-07-18 11:39:06 +0800147 Error *err = NULL;
Peter Xu4ffdb332017-06-27 12:10:18 +0800148
Peter Xue5cb7e72017-06-27 12:10:13 +0800149 /* This can only be called once. */
150 assert(!current_migration);
151 current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION));
Peter Xu4ffdb332017-06-27 12:10:18 +0800152
Peter Xue1b1b1b2018-05-02 18:47:35 +0800153 /*
154 * Init the migrate incoming object as well no matter whether
155 * we'll use it or not.
156 */
157 assert(!current_incoming);
158 current_incoming = g_new0(MigrationIncomingState, 1);
159 current_incoming->state = MIGRATION_STATUS_NONE;
160 current_incoming->postcopy_remote_fds =
161 g_array_new(FALSE, TRUE, sizeof(struct PostCopyFD));
162 qemu_mutex_init(&current_incoming->rp_mutex);
163 qemu_event_init(&current_incoming->main_thread_load_event, false);
164 qemu_sem_init(&current_incoming->postcopy_pause_sem_dst, 0);
165 qemu_sem_init(&current_incoming->postcopy_pause_sem_fault, 0);
166
167 init_dirty_bitmap_incoming_migration();
168
Peter Xu8b0b29d2017-07-18 11:39:06 +0800169 if (!migration_object_check(current_migration, &err)) {
170 error_report_err(err);
171 exit(1);
172 }
173
Peter Xu4ffdb332017-06-27 12:10:18 +0800174 /*
175 * We cannot really do this in migration_instance_init() since at
176 * that time global properties are not yet applied, then this
177 * value will be definitely replaced by something else.
178 */
179 if (ms->enforce_config_section) {
180 current_migration->send_configuration = true;
181 }
Peter Xue5cb7e72017-06-27 12:10:13 +0800182}
183
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +0000184void migration_shutdown(void)
Vladimir Sementsov-Ogievskiy1f895602017-12-28 12:16:16 +0300185{
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +0000186 /*
187 * Cancel the current migration - that will (eventually)
188 * stop the migration using this structure
189 */
190 migrate_fd_cancel(current_migration);
Vladimir Sementsov-Ogievskiy1f895602017-12-28 12:16:16 +0300191 object_unref(OBJECT(current_migration));
192}
193
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100194/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +0200195MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +0200196{
Peter Xue5cb7e72017-06-27 12:10:13 +0800197 /* This can only be called after the object created. */
198 assert(current_migration);
199 return current_migration;
Juan Quintela17549e82011-10-05 13:50:43 +0200200}
201
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100202MigrationIncomingState *migration_incoming_get_current(void)
203{
Peter Xue1b1b1b2018-05-02 18:47:35 +0800204 assert(current_incoming);
205 return current_incoming;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100206}
207
208void migration_incoming_state_destroy(void)
209{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100210 struct MigrationIncomingState *mis = migration_incoming_get_current();
211
Peter Xu34826552017-05-19 14:43:29 +0800212 if (mis->to_src_file) {
Peter Xu660819b2017-05-19 14:43:30 +0800213 /* Tell source that we are done */
214 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
Peter Xu34826552017-05-19 14:43:29 +0800215 qemu_fclose(mis->to_src_file);
216 mis->to_src_file = NULL;
217 }
218
Peter Xu660819b2017-05-19 14:43:30 +0800219 if (mis->from_src_file) {
220 qemu_fclose(mis->from_src_file);
221 mis->from_src_file = NULL;
222 }
Dr. David Alan Gilbert00fa4fc2018-03-12 17:21:04 +0000223 if (mis->postcopy_remote_fds) {
224 g_array_free(mis->postcopy_remote_fds, TRUE);
225 mis->postcopy_remote_fds = NULL;
226 }
Peter Xu660819b2017-05-19 14:43:30 +0800227
Dr. David Alan Gilbert5089e182017-08-25 15:19:39 +0100228 qemu_event_reset(&mis->main_thread_load_event);
Juan Quintela9aca82b2019-02-27 11:51:27 +0100229
230 if (mis->socket_address_list) {
231 qapi_free_SocketAddressList(mis->socket_address_list);
232 mis->socket_address_list = NULL;
233 }
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100234}
235
Juan Quintelab05dc722015-07-07 14:44:05 +0200236static void migrate_generate_event(int new_state)
237{
238 if (migrate_use_events()) {
Peter Xu3ab72382018-08-15 21:37:37 +0800239 qapi_event_send_migration(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200240 }
241}
242
Dr. David Alan Gilbert0f073f42018-04-16 18:09:30 +0100243static bool migrate_late_block_activate(void)
244{
245 MigrationState *s;
246
247 s = migrate_get_current();
248
249 return s->enabled_capabilities[
250 MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE];
251}
252
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000253/*
254 * Called on -incoming with a defer: uri.
255 * The migration can be started later after any parameters have been
256 * changed.
257 */
258static void deferred_incoming_migration(Error **errp)
259{
260 if (deferred_incoming) {
261 error_setg(errp, "Incoming migration already deferred");
262 }
263 deferred_incoming = true;
264}
265
Juan Quintelada6f1792017-04-24 17:37:14 +0200266/*
267 * Send a message on the return channel back to the source
268 * of the migration.
269 */
Peter Xud6208e32018-02-08 18:31:12 +0800270static int migrate_send_rp_message(MigrationIncomingState *mis,
271 enum mig_rp_message_type message_type,
272 uint16_t len, void *data)
Juan Quintelada6f1792017-04-24 17:37:14 +0200273{
Peter Xud6208e32018-02-08 18:31:12 +0800274 int ret = 0;
275
Juan Quintelada6f1792017-04-24 17:37:14 +0200276 trace_migrate_send_rp_message((int)message_type, len);
277 qemu_mutex_lock(&mis->rp_mutex);
Peter Xud6208e32018-02-08 18:31:12 +0800278
279 /*
280 * It's possible that the file handle got lost due to network
281 * failures.
282 */
283 if (!mis->to_src_file) {
284 ret = -EIO;
285 goto error;
286 }
287
Juan Quintelada6f1792017-04-24 17:37:14 +0200288 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
289 qemu_put_be16(mis->to_src_file, len);
290 qemu_put_buffer(mis->to_src_file, data, len);
291 qemu_fflush(mis->to_src_file);
Peter Xud6208e32018-02-08 18:31:12 +0800292
293 /* It's possible that qemu file got error during sending */
294 ret = qemu_file_get_error(mis->to_src_file);
295
296error:
Juan Quintelada6f1792017-04-24 17:37:14 +0200297 qemu_mutex_unlock(&mis->rp_mutex);
Peter Xud6208e32018-02-08 18:31:12 +0800298 return ret;
Juan Quintelada6f1792017-04-24 17:37:14 +0200299}
300
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000301/* Request a range of pages from the source VM at the given
302 * start address.
303 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
304 * as the last request (a name must have been given previously)
305 * Start: Address offset within the RB
306 * Len: Length in bytes required - must be a multiple of pagesize
307 */
Peter Xud6208e32018-02-08 18:31:12 +0800308int migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
309 ram_addr_t start, size_t len)
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000310{
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100311 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000312 size_t msglen = 12; /* start + len */
Peter Xud6208e32018-02-08 18:31:12 +0800313 enum mig_rp_message_type msg_type;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000314
315 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
316 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
317
318 if (rbname) {
319 int rbname_len = strlen(rbname);
320 assert(rbname_len < 256);
321
322 bufc[msglen++] = rbname_len;
323 memcpy(bufc + msglen, rbname, rbname_len);
324 msglen += rbname_len;
Peter Xud6208e32018-02-08 18:31:12 +0800325 msg_type = MIG_RP_MSG_REQ_PAGES_ID;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000326 } else {
Peter Xud6208e32018-02-08 18:31:12 +0800327 msg_type = MIG_RP_MSG_REQ_PAGES;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000328 }
Peter Xud6208e32018-02-08 18:31:12 +0800329
330 return migrate_send_rp_message(mis, msg_type, msglen, bufc);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000331}
332
Zhang Chenaad555c2018-09-03 12:38:47 +0800333static bool migration_colo_enabled;
334bool migration_incoming_colo_enabled(void)
335{
336 return migration_colo_enabled;
337}
338
339void migration_incoming_disable_colo(void)
340{
341 migration_colo_enabled = false;
342}
343
344void migration_incoming_enable_colo(void)
345{
346 migration_colo_enabled = true;
347}
348
Juan Quintela9aca82b2019-02-27 11:51:27 +0100349void migrate_add_address(SocketAddress *address)
350{
351 MigrationIncomingState *mis = migration_incoming_get_current();
352 SocketAddressList *addrs;
353
354 addrs = g_new0(SocketAddressList, 1);
355 addrs->next = mis->socket_address_list;
356 mis->socket_address_list = addrs;
357 addrs->value = QAPI_CLONE(SocketAddress, address);
358}
359
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200360void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000361{
aliguori34c9dd82008-10-13 03:14:31 +0000362 const char *p;
363
Peter Xu3ab72382018-08-15 21:37:37 +0800364 qapi_event_send_migration(MIGRATION_STATUS_SETUP);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000365 if (!strcmp(uri, "defer")) {
366 deferred_incoming_migration(errp);
367 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200368 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400369#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000370 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400371 rdma_start_incoming_migration(p, errp);
372#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000373 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200374 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000375 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200376 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000377 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200378 fd_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000379 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100380 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200381 }
aliguori5bb79102008-10-13 03:12:02 +0000382}
383
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300384static void process_incoming_migration_bh(void *opaque)
385{
386 Error *local_err = NULL;
387 MigrationIncomingState *mis = opaque;
388
Dr. David Alan Gilbert0f073f42018-04-16 18:09:30 +0100389 /* If capability late_block_activate is set:
390 * Only fire up the block code now if we're going to restart the
391 * VM, else 'cont' will do it.
392 * This causes file locking to happen; so we don't want it to happen
393 * unless we really are starting the VM.
394 */
395 if (!migrate_late_block_activate() ||
396 (autostart && (!global_state_received() ||
397 global_state_get_runstate() == RUN_STATE_RUNNING))) {
398 /* Make sure all file formats flush their mutable metadata.
399 * If we get an error here, just don't restart the VM yet. */
400 bdrv_invalidate_cache_all(&local_err);
401 if (local_err) {
402 error_report_err(local_err);
403 local_err = NULL;
404 autostart = false;
405 }
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200406 }
407
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300408 /*
409 * This must happen after all error conditions are dealt with and
410 * we're sure the VM is going to be running on this host.
411 */
Dr. David Alan Gilbert76595052019-02-27 13:24:08 +0000412 qemu_announce_self(&mis->announce_timer, migrate_announce_params());
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300413
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100414 if (multifd_load_cleanup(&local_err) != 0) {
415 error_report_err(local_err);
416 autostart = false;
417 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300418 /* If global state section was not received or we are in running
419 state, we need to obey autostart. Any other state is set with
420 runstate_set. */
421
Vladimir Sementsov-Ogievskiyb35ebdf2018-03-13 15:34:01 -0400422 dirty_bitmap_mig_before_vm_start();
423
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300424 if (!global_state_received() ||
425 global_state_get_runstate() == RUN_STATE_RUNNING) {
426 if (autostart) {
427 vm_start();
428 } else {
429 runstate_set(RUN_STATE_PAUSED);
430 }
Zhang Chendb009722019-03-03 22:50:17 +0800431 } else if (migration_incoming_colo_enabled()) {
432 migration_incoming_disable_colo();
433 vm_start();
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300434 } else {
435 runstate_set(global_state_get_runstate());
436 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300437 /*
438 * This must happen after any state changes since as soon as an external
439 * observer sees this event they might start to prod at the VM assuming
440 * it's ready to use.
441 */
442 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
443 MIGRATION_STATUS_COMPLETED);
444 qemu_bh_delete(mis->bh);
445 migration_incoming_state_destroy();
446}
447
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200448static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200449{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100450 MigrationIncomingState *mis = migration_incoming_get_current();
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000451 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200452 int ret;
Zhang Chen8e48ac92018-09-03 12:38:46 +0800453 Error *local_err = NULL;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200454
Juan Quintela4f0fae72017-07-24 12:42:02 +0200455 assert(mis->from_src_file);
Lidong Chen92370982018-08-06 21:29:35 +0800456 mis->migration_incoming_co = qemu_coroutine_self();
Dr. David Alan Gilbert67f11b52017-02-24 18:28:34 +0000457 mis->largest_page_size = qemu_ram_pagesize_largest();
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000458 postcopy_state_set(POSTCOPY_INCOMING_NONE);
zhanghailiang93d7af62015-12-16 11:47:34 +0000459 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
460 MIGRATION_STATUS_ACTIVE);
Juan Quintela4f0fae72017-07-24 12:42:02 +0200461 ret = qemu_loadvm_state(mis->from_src_file);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100462
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000463 ps = postcopy_state_get();
464 trace_process_incoming_migration_co_end(ret, ps);
465 if (ps != POSTCOPY_INCOMING_NONE) {
466 if (ps == POSTCOPY_INCOMING_ADVISE) {
467 /*
468 * Where a migration had postcopy enabled (and thus went to advise)
469 * but managed to complete within the precopy period, we can use
470 * the normal exit.
471 */
472 postcopy_ram_incoming_cleanup(mis);
473 } else if (ret >= 0) {
474 /*
475 * Postcopy was started, cleanup should happen at the end of the
476 * postcopy thread.
477 */
478 trace_process_incoming_migration_co_postcopy_end_main();
479 return;
480 }
481 /* Else if something went wrong then just fall out of the normal exit */
482 }
483
zhanghailiang25d0c162016-10-27 14:42:55 +0800484 /* we get COLO info, and know if we are in COLO mode */
Zhang Chenaad555c2018-09-03 12:38:47 +0800485 if (!ret && migration_incoming_colo_enabled()) {
Zhang Chen8e48ac92018-09-03 12:38:46 +0800486 /* Make sure all file formats flush their mutable metadata */
487 bdrv_invalidate_cache_all(&local_err);
488 if (local_err) {
Zhang Chen8e48ac92018-09-03 12:38:46 +0800489 error_report_err(local_err);
Fei Li6d99c2d2019-01-13 22:08:49 +0800490 goto fail;
Zhang Chen8e48ac92018-09-03 12:38:46 +0800491 }
492
zhanghailiang25d0c162016-10-27 14:42:55 +0800493 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
494 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
495 mis->have_colo_incoming_thread = true;
496 qemu_coroutine_yield();
497
498 /* Wait checkpoint incoming thread exit before free resource */
499 qemu_thread_join(&mis->colo_incoming_thread);
Zhang Chen13af18f2018-09-03 12:38:48 +0800500 /* We hold the global iothread lock, so it is safe here */
501 colo_release_ram_cache();
zhanghailiang25d0c162016-10-27 14:42:55 +0800502 }
503
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200504 if (ret < 0) {
Peter Lievendb80fac2014-06-10 11:29:16 +0200505 error_report("load of migration failed: %s", strerror(-ret));
Fei Li6d99c2d2019-01-13 22:08:49 +0800506 goto fail;
Juan Quintela511c0232010-06-09 14:10:55 +0200507 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300508 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
509 qemu_bh_schedule(mis->bh);
Lidong Chen92370982018-08-06 21:29:35 +0800510 mis->migration_incoming_co = NULL;
Fei Li6d99c2d2019-01-13 22:08:49 +0800511 return;
512fail:
513 local_err = NULL;
514 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
515 MIGRATION_STATUS_FAILED);
516 qemu_fclose(mis->from_src_file);
517 if (multifd_load_cleanup(&local_err) != 0) {
518 error_report_err(local_err);
519 }
520 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200521}
522
Juan Quintelab673eab2019-06-12 11:44:19 +0200523/**
524 * @migration_incoming_setup: Setup incoming migration
525 *
526 * Returns 0 for no error or 1 for error
527 *
528 * @f: file for main migration channel
529 * @errp: where to put errors
530 */
531static int migration_incoming_setup(QEMUFile *f, Error **errp)
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200532{
Juan Quintela4f0fae72017-07-24 12:42:02 +0200533 MigrationIncomingState *mis = migration_incoming_get_current();
Juan Quintelab673eab2019-06-12 11:44:19 +0200534 Error *local_err = NULL;
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200535
Juan Quintelab673eab2019-06-12 11:44:19 +0200536 if (multifd_load_setup(&local_err) != 0) {
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100537 /* We haven't been able to create multifd threads
538 nothing better to do */
Juan Quintelab673eab2019-06-12 11:44:19 +0200539 error_report_err(local_err);
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100540 exit(EXIT_FAILURE);
541 }
542
Juan Quintela4f0fae72017-07-24 12:42:02 +0200543 if (!mis->from_src_file) {
544 mis->from_src_file = f;
545 }
Daniel P. Berrange06ad5132016-04-27 11:04:56 +0100546 qemu_file_set_blocking(f, false);
Juan Quintelab673eab2019-06-12 11:44:19 +0200547 return 0;
Juan Quintelae595a012017-07-17 12:30:25 +0200548}
549
Juan Quintela36c2f8b2018-03-07 08:40:52 +0100550void migration_incoming_process(void)
Juan Quintelae595a012017-07-17 12:30:25 +0200551{
552 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL);
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200553 qemu_coroutine_enter(co);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200554}
555
Peter Xu884835f2018-06-27 21:22:45 +0800556/* Returns true if recovered from a paused migration, otherwise false */
557static bool postcopy_try_recover(QEMUFile *f)
Juan Quintelae595a012017-07-17 12:30:25 +0200558{
Peter Xud96c9e82018-05-02 18:47:26 +0800559 MigrationIncomingState *mis = migration_incoming_get_current();
560
561 if (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
562 /* Resumed from a paused postcopy migration */
563
564 mis->from_src_file = f;
565 /* Postcopy has standalone thread to do vm load */
566 qemu_file_set_blocking(f, true);
567
568 /* Re-configure the return path */
569 mis->to_src_file = qemu_file_get_return_path(f);
570
571 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
572 MIGRATION_STATUS_POSTCOPY_RECOVER);
573
574 /*
575 * Here, we only wake up the main loading thread (while the
576 * fault thread will still be waiting), so that we can receive
577 * commands from source now, and answer it if needed. The
578 * fault thread will be woken up afterwards until we are sure
579 * that source is ready to reply to page requests.
580 */
581 qemu_sem_post(&mis->postcopy_pause_sem_dst);
Peter Xu884835f2018-06-27 21:22:45 +0800582 return true;
Peter Xud96c9e82018-05-02 18:47:26 +0800583 }
Peter Xu884835f2018-06-27 21:22:45 +0800584
585 return false;
586}
587
Juan Quintelab673eab2019-06-12 11:44:19 +0200588void migration_fd_process_incoming(QEMUFile *f, Error **errp)
Peter Xu884835f2018-06-27 21:22:45 +0800589{
Juan Quintelab673eab2019-06-12 11:44:19 +0200590 Error *local_err = NULL;
591
Peter Xu884835f2018-06-27 21:22:45 +0800592 if (postcopy_try_recover(f)) {
593 return;
594 }
595
Juan Quintelab673eab2019-06-12 11:44:19 +0200596 if (migration_incoming_setup(f, &local_err)) {
597 if (local_err) {
598 error_propagate(errp, local_err);
599 }
600 return;
601 }
Peter Xu884835f2018-06-27 21:22:45 +0800602 migration_incoming_process();
Juan Quintelae595a012017-07-17 12:30:25 +0200603}
604
Fei Li49ed0d22019-01-13 22:08:46 +0800605void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp)
Juan Quintela4f0fae72017-07-24 12:42:02 +0200606{
607 MigrationIncomingState *mis = migration_incoming_get_current();
Juan Quintelab673eab2019-06-12 11:44:19 +0200608 Error *local_err = NULL;
Peter Xua429e7f2018-06-27 21:22:46 +0800609 bool start_migration;
Juan Quintela4f0fae72017-07-24 12:42:02 +0200610
611 if (!mis->from_src_file) {
Peter Xua429e7f2018-06-27 21:22:46 +0800612 /* The first connection (multifd may have multiple) */
Juan Quintela4f0fae72017-07-24 12:42:02 +0200613 QEMUFile *f = qemu_fopen_channel_input(ioc);
Peter Xua429e7f2018-06-27 21:22:46 +0800614
615 /* If it's a recovery, we're done */
Peter Xu884835f2018-06-27 21:22:45 +0800616 if (postcopy_try_recover(f)) {
617 return;
618 }
Peter Xua429e7f2018-06-27 21:22:46 +0800619
Juan Quintelab673eab2019-06-12 11:44:19 +0200620 if (migration_incoming_setup(f, &local_err)) {
621 if (local_err) {
622 error_propagate(errp, local_err);
623 }
624 return;
625 }
Peter Xua429e7f2018-06-27 21:22:46 +0800626
627 /*
628 * Common migration only needs one channel, so we can start
629 * right now. Multifd needs more than one channel, we wait.
630 */
631 start_migration = !migrate_use_multifd();
632 } else {
633 /* Multiple connections */
634 assert(migrate_use_multifd());
Fei Li49ed0d22019-01-13 22:08:46 +0800635 start_migration = multifd_recv_new_channel(ioc, &local_err);
636 if (local_err) {
637 error_propagate(errp, local_err);
638 return;
639 }
Juan Quintela4f0fae72017-07-24 12:42:02 +0200640 }
Peter Xu81e62052018-06-27 21:22:44 +0800641
Peter Xua429e7f2018-06-27 21:22:46 +0800642 if (start_migration) {
Peter Xu81e62052018-06-27 21:22:44 +0800643 migration_incoming_process();
644 }
Juan Quintela4f0fae72017-07-24 12:42:02 +0200645}
646
Juan Quintela428d8902017-07-24 13:06:25 +0200647/**
648 * @migration_has_all_channels: We have received all channels that we need
649 *
650 * Returns true when we have got connections to all the channels that
651 * we need for migration.
652 */
653bool migration_has_all_channels(void)
654{
Daniel P. Berrangéca273df2018-06-19 17:35:52 +0100655 MigrationIncomingState *mis = migration_incoming_get_current();
Juan Quintela62c1e0c2018-02-19 18:59:02 +0100656 bool all_channels;
657
658 all_channels = multifd_recv_all_channels_created();
659
Daniel P. Berrangéca273df2018-06-19 17:35:52 +0100660 return all_channels && mis->from_src_file != NULL;
Juan Quintela428d8902017-07-24 13:06:25 +0200661}
662
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000663/*
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000664 * Send a 'SHUT' message on the return channel with the given value
665 * to indicate that we've finished with the RP. Non-0 value indicates
666 * error.
667 */
668void migrate_send_rp_shut(MigrationIncomingState *mis,
669 uint32_t value)
670{
671 uint32_t buf;
672
673 buf = cpu_to_be32(value);
674 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
675}
676
677/*
678 * Send a 'PONG' message on the return channel with the given value
679 * (normally in response to a 'PING')
680 */
681void migrate_send_rp_pong(MigrationIncomingState *mis,
682 uint32_t value)
683{
684 uint32_t buf;
685
686 buf = cpu_to_be32(value);
687 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
688}
689
Peter Xua335deb2018-05-02 18:47:28 +0800690void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
691 char *block_name)
692{
693 char buf[512];
694 int len;
695 int64_t res;
696
697 /*
698 * First, we send the header part. It contains only the len of
699 * idstr, and the idstr itself.
700 */
701 len = strlen(block_name);
702 buf[0] = len;
703 memcpy(buf + 1, block_name, len);
704
705 if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
706 error_report("%s: MSG_RP_RECV_BITMAP only used for recovery",
707 __func__);
708 return;
709 }
710
711 migrate_send_rp_message(mis, MIG_RP_MSG_RECV_BITMAP, len + 1, buf);
712
713 /*
714 * Next, we dump the received bitmap to the stream.
715 *
716 * TODO: currently we are safe since we are the only one that is
717 * using the to_src_file handle (fault thread is still paused),
718 * and it's ok even not taking the mutex. However the best way is
719 * to take the lock before sending the message header, and release
720 * the lock after sending the bitmap.
721 */
722 qemu_mutex_lock(&mis->rp_mutex);
723 res = ramblock_recv_bitmap_send(mis->to_src_file, block_name);
724 qemu_mutex_unlock(&mis->rp_mutex);
725
726 trace_migrate_send_rp_recv_bitmap(block_name, res);
727}
728
Peter Xu13955b82018-05-02 18:47:30 +0800729void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value)
730{
731 uint32_t buf;
732
733 buf = cpu_to_be32(value);
734 migrate_send_rp_message(mis, MIG_RP_MSG_RESUME_ACK, sizeof(buf), &buf);
735}
736
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300737MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
738{
739 MigrationCapabilityStatusList *head = NULL;
740 MigrationCapabilityStatusList *caps;
741 MigrationState *s = migrate_get_current();
742 int i;
743
Michael Tokarev387eede2013-10-05 13:18:28 +0400744 caps = NULL; /* silence compiler warning */
Eric Blake7fb1cf12015-11-18 01:52:57 -0700745 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Dr. David Alan Gilberted1701c2017-05-15 15:05:29 +0100746#ifndef CONFIG_LIVE_BLOCK_MIGRATION
747 if (i == MIGRATION_CAPABILITY_BLOCK) {
748 continue;
749 }
750#endif
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300751 if (head == NULL) {
752 head = g_malloc0(sizeof(*caps));
753 caps = head;
754 } else {
755 caps->next = g_malloc0(sizeof(*caps));
756 caps = caps->next;
757 }
758 caps->value =
759 g_malloc(sizeof(*caps->value));
760 caps->value->capability = i;
761 caps->value->state = s->enabled_capabilities[i];
762 }
763
764 return head;
765}
766
Liang Li85de8322015-03-23 16:32:28 +0800767MigrationParameters *qmp_query_migrate_parameters(Error **errp)
768{
769 MigrationParameters *params;
770 MigrationState *s = migrate_get_current();
771
Markus Armbrustere87fae42017-07-18 12:57:38 +0200772 /* TODO use QAPI_CLONE() instead of duplicating it inline */
Liang Li85de8322015-03-23 16:32:28 +0800773 params = g_malloc0(sizeof(*params));
Eric Blakede63ab62016-09-08 22:14:15 -0500774 params->has_compress_level = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100775 params->compress_level = s->parameters.compress_level;
Eric Blakede63ab62016-09-08 22:14:15 -0500776 params->has_compress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100777 params->compress_threads = s->parameters.compress_threads;
Xiao Guangrong1d588722018-08-21 16:10:20 +0800778 params->has_compress_wait_thread = true;
779 params->compress_wait_thread = s->parameters.compress_wait_thread;
Eric Blakede63ab62016-09-08 22:14:15 -0500780 params->has_decompress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100781 params->decompress_threads = s->parameters.decompress_threads;
Keqian Zhudc14a472020-02-24 10:31:42 +0800782 params->has_throttle_trigger_threshold = true;
783 params->throttle_trigger_threshold = s->parameters.throttle_trigger_threshold;
Eric Blakede63ab62016-09-08 22:14:15 -0500784 params->has_cpu_throttle_initial = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100785 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
Eric Blakede63ab62016-09-08 22:14:15 -0500786 params->has_cpu_throttle_increment = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100787 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200788 params->has_tls_creds = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100789 params->tls_creds = g_strdup(s->parameters.tls_creds);
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200790 params->has_tls_hostname = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100791 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
Daniel P. Berranged2f1d292019-02-27 14:53:24 +0000792 params->has_tls_authz = true;
793 params->tls_authz = g_strdup(s->parameters.tls_authz);
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530794 params->has_max_bandwidth = true;
795 params->max_bandwidth = s->parameters.max_bandwidth;
796 params->has_downtime_limit = true;
797 params->downtime_limit = s->parameters.downtime_limit;
zhanghailiangfe39a4d2016-11-02 15:42:09 +0800798 params->has_x_checkpoint_delay = true;
zhanghailiang68b53592016-10-27 14:43:01 +0800799 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
Juan Quintela2833c592017-04-05 18:32:37 +0200800 params->has_block_incremental = true;
801 params->block_incremental = s->parameters.block_incremental;
Juan Quintelacbfd6c92019-02-06 13:54:06 +0100802 params->has_multifd_channels = true;
803 params->multifd_channels = s->parameters.multifd_channels;
Juan Quintela96eef042019-01-16 10:35:55 +0100804 params->has_multifd_compression = true;
805 params->multifd_compression = s->parameters.multifd_compression;
Juan Quintela9004db42020-01-23 17:08:52 +0100806 params->has_multifd_zlib_level = true;
807 params->multifd_zlib_level = s->parameters.multifd_zlib_level;
Juan Quintela6a9ad152020-01-23 17:41:36 +0100808 params->has_multifd_zstd_level = true;
809 params->multifd_zstd_level = s->parameters.multifd_zstd_level;
Juan Quintela73af8dd2017-10-05 21:30:10 +0200810 params->has_xbzrle_cache_size = true;
811 params->xbzrle_cache_size = s->parameters.xbzrle_cache_size;
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +0100812 params->has_max_postcopy_bandwidth = true;
813 params->max_postcopy_bandwidth = s->parameters.max_postcopy_bandwidth;
Li Qiang4cbc9c72018-08-01 06:00:20 -0700814 params->has_max_cpu_throttle = true;
815 params->max_cpu_throttle = s->parameters.max_cpu_throttle;
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +0000816 params->has_announce_initial = true;
817 params->announce_initial = s->parameters.announce_initial;
818 params->has_announce_max = true;
819 params->announce_max = s->parameters.announce_max;
820 params->has_announce_rounds = true;
821 params->announce_rounds = s->parameters.announce_rounds;
822 params->has_announce_step = true;
823 params->announce_step = s->parameters.announce_step;
Liang Li85de8322015-03-23 16:32:28 +0800824
825 return params;
826}
827
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +0000828AnnounceParameters *migrate_announce_params(void)
829{
830 static AnnounceParameters ap;
831
832 MigrationState *s = migrate_get_current();
833
834 ap.initial = s->parameters.announce_initial;
835 ap.max = s->parameters.announce_max;
836 ap.rounds = s->parameters.announce_rounds;
837 ap.step = s->parameters.announce_step;
838
839 return &ap;
840}
841
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000842/*
843 * Return true if we're already in the middle of a migration
844 * (i.e. any of the active or setup states)
845 */
Jia Lina3d63da12018-10-26 16:36:20 +0800846bool migration_is_setup_or_active(int state)
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000847{
848 switch (state) {
849 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000850 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Peter Xua688d2c2018-05-02 18:47:18 +0800851 case MIGRATION_STATUS_POSTCOPY_PAUSED:
Peter Xu135b87b2018-05-02 18:47:25 +0800852 case MIGRATION_STATUS_POSTCOPY_RECOVER:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000853 case MIGRATION_STATUS_SETUP:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +0100854 case MIGRATION_STATUS_PRE_SWITCHOVER:
855 case MIGRATION_STATUS_DEVICE:
Jens Freimannc7e0acd2019-10-29 12:49:02 +0100856 case MIGRATION_STATUS_WAIT_UNPLUG:
zhanghailiang19dd4082020-02-24 14:54:12 +0800857 case MIGRATION_STATUS_COLO:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000858 return true;
859
860 default:
861 return false;
862
863 }
864}
865
Juan Quintela392d87e2020-01-21 15:39:23 +0100866bool migration_is_running(int state)
867{
868 switch (state) {
869 case MIGRATION_STATUS_ACTIVE:
870 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
871 case MIGRATION_STATUS_POSTCOPY_PAUSED:
872 case MIGRATION_STATUS_POSTCOPY_RECOVER:
873 case MIGRATION_STATUS_SETUP:
874 case MIGRATION_STATUS_PRE_SWITCHOVER:
875 case MIGRATION_STATUS_DEVICE:
876 case MIGRATION_STATUS_WAIT_UNPLUG:
877 case MIGRATION_STATUS_CANCELLING:
Juan Quintela392d87e2020-01-21 15:39:23 +0100878 return true;
879
880 default:
881 return false;
882
883 }
884}
885
Wei Yang640dfb12019-07-16 08:54:11 +0800886static void populate_time_info(MigrationInfo *info, MigrationState *s)
887{
888 info->has_status = true;
889 info->has_setup_time = true;
890 info->setup_time = s->setup_time;
891 if (s->state == MIGRATION_STATUS_COMPLETED) {
892 info->has_total_time = true;
893 info->total_time = s->total_time;
894 info->has_downtime = true;
895 info->downtime = s->downtime;
896 } else {
897 info->has_total_time = true;
898 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
899 s->start_time;
900 info->has_expected_downtime = true;
901 info->expected_downtime = s->expected_downtime;
902 }
903}
904
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100905static void populate_ram_info(MigrationInfo *info, MigrationState *s)
906{
907 info->has_ram = true;
908 info->ram = g_malloc0(sizeof(*info->ram));
Juan Quintela93604472017-06-06 19:49:03 +0200909 info->ram->transferred = ram_counters.transferred;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100910 info->ram->total = ram_bytes_total();
Juan Quintela93604472017-06-06 19:49:03 +0200911 info->ram->duplicate = ram_counters.duplicate;
Juan Quintelabedf53c2017-03-13 20:35:54 +0100912 /* legacy value. It is not used anymore */
913 info->ram->skipped = 0;
Juan Quintela93604472017-06-06 19:49:03 +0200914 info->ram->normal = ram_counters.normal;
915 info->ram->normal_bytes = ram_counters.normal *
Juan Quintela20afaed2017-03-21 09:09:14 +0100916 qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100917 info->ram->mbps = s->mbps;
Juan Quintela93604472017-06-06 19:49:03 +0200918 info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
919 info->ram->postcopy_requests = ram_counters.postcopy_requests;
Chao Fan030ce1f2017-03-21 10:22:43 +0800920 info->ram->page_size = qemu_target_page_size();
Juan Quintelaa61c45b2018-06-26 15:20:11 +0200921 info->ram->multifd_bytes = ram_counters.multifd_bytes;
Xiao Guangrongaecbfe92019-01-11 14:37:30 +0800922 info->ram->pages_per_second = s->pages_per_second;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100923
Juan Quintela114f5ae2017-05-04 10:09:21 +0200924 if (migrate_use_xbzrle()) {
925 info->has_xbzrle_cache = true;
926 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
927 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
Juan Quintela93604472017-06-06 19:49:03 +0200928 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
929 info->xbzrle_cache->pages = xbzrle_counters.pages;
930 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
931 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
932 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
Juan Quintela114f5ae2017-05-04 10:09:21 +0200933 }
934
Xiao Guangrong76e03002018-09-06 15:01:00 +0800935 if (migrate_use_compression()) {
936 info->has_compression = true;
937 info->compression = g_malloc0(sizeof(*info->compression));
938 info->compression->pages = compression_counters.pages;
939 info->compression->busy = compression_counters.busy;
940 info->compression->busy_rate = compression_counters.busy_rate;
941 info->compression->compressed_size =
942 compression_counters.compressed_size;
943 info->compression->compression_rate =
944 compression_counters.compression_rate;
945 }
946
Juan Quintela338182c2017-05-03 13:16:38 +0200947 if (cpu_throttle_active()) {
948 info->has_cpu_throttle_percentage = true;
949 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
950 }
951
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100952 if (s->state != MIGRATION_STATUS_COMPLETED) {
953 info->ram->remaining = ram_bytes_remaining();
Juan Quintela93604472017-06-06 19:49:03 +0200954 info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100955 }
956}
957
Juan Quintela930ac042017-05-04 10:21:46 +0200958static void populate_disk_info(MigrationInfo *info)
959{
960 if (blk_mig_active()) {
961 info->has_disk = true;
962 info->disk = g_malloc0(sizeof(*info->disk));
963 info->disk->transferred = blk_mig_bytes_transferred();
964 info->disk->remaining = blk_mig_bytes_remaining();
965 info->disk->total = blk_mig_bytes_total();
966 }
967}
968
Alexey Perevalov65ace062018-03-22 21:17:27 +0300969static void fill_source_migration_info(MigrationInfo *info)
aliguori5bb79102008-10-13 03:12:02 +0000970{
Juan Quintela17549e82011-10-05 13:50:43 +0200971 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000972
Juan Quintela17549e82011-10-05 13:50:43 +0200973 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800974 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200975 /* no migration has happened ever */
Alexey Perevalov65ace062018-03-22 21:17:27 +0300976 /* do not overwrite destination migration status */
977 return;
Juan Quintela17549e82011-10-05 13:50:43 +0200978 break;
zhanghailiang31194732015-03-13 16:08:38 +0800979 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400980 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400981 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400982 break;
zhanghailiang31194732015-03-13 16:08:38 +0800983 case MIGRATION_STATUS_ACTIVE:
984 case MIGRATION_STATUS_CANCELLING:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000985 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +0100986 case MIGRATION_STATUS_PRE_SWITCHOVER:
987 case MIGRATION_STATUS_DEVICE:
Peter Xua688d2c2018-05-02 18:47:18 +0800988 case MIGRATION_STATUS_POSTCOPY_PAUSED:
Peter Xu135b87b2018-05-02 18:47:25 +0800989 case MIGRATION_STATUS_POSTCOPY_RECOVER:
Wei Yang640dfb12019-07-16 08:54:11 +0800990 /* TODO add some postcopy stats */
991 populate_time_info(info, s);
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100992 populate_ram_info(info, s);
Juan Quintela930ac042017-05-04 10:21:46 +0200993 populate_disk_info(info);
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000994 break;
zhanghailiang0b827d52016-10-27 14:42:54 +0800995 case MIGRATION_STATUS_COLO:
996 info->has_status = true;
997 /* TODO: display COLO specific information (checkpoint info etc.) */
998 break;
zhanghailiang31194732015-03-13 16:08:38 +0800999 case MIGRATION_STATUS_COMPLETED:
Wei Yang640dfb12019-07-16 08:54:11 +08001000 populate_time_info(info, s);
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +01001001 populate_ram_info(info, s);
Juan Quintela17549e82011-10-05 13:50:43 +02001002 break;
zhanghailiang31194732015-03-13 16:08:38 +08001003 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -03001004 info->has_status = true;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001005 if (s->error) {
1006 info->has_error_desc = true;
1007 info->error_desc = g_strdup(error_get_pretty(s->error));
1008 }
Juan Quintela17549e82011-10-05 13:50:43 +02001009 break;
zhanghailiang31194732015-03-13 16:08:38 +08001010 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -03001011 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +02001012 break;
Jens Freimannc7e0acd2019-10-29 12:49:02 +01001013 case MIGRATION_STATUS_WAIT_UNPLUG:
1014 info->has_status = true;
1015 break;
aliguori5bb79102008-10-13 03:12:02 +00001016 }
zhanghailiangcde63fb2015-03-13 16:08:41 +08001017 info->status = s->state;
aliguori5bb79102008-10-13 03:12:02 +00001018}
1019
Peter Xu4a842142017-07-18 11:39:08 +08001020/**
1021 * @migration_caps_check - check capability validity
1022 *
1023 * @cap_list: old capability list, array of bool
1024 * @params: new capabilities to be applied soon
1025 * @errp: set *errp if the check failed, with reason
1026 *
1027 * Returns true if check passed, otherwise false.
1028 */
1029static bool migrate_caps_check(bool *cap_list,
1030 MigrationCapabilityStatusList *params,
1031 Error **errp)
Orit Wasserman00458432012-08-06 21:42:48 +03001032{
Orit Wasserman00458432012-08-06 21:42:48 +03001033 MigrationCapabilityStatusList *cap;
Peter Xu4a842142017-07-18 11:39:08 +08001034 bool old_postcopy_cap;
Alexey Perevalovd7651f12017-09-19 19:47:56 +03001035 MigrationIncomingState *mis = migration_incoming_get_current();
Orit Wasserman00458432012-08-06 21:42:48 +03001036
Peter Xu4a842142017-07-18 11:39:08 +08001037 old_postcopy_cap = cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Orit Wasserman00458432012-08-06 21:42:48 +03001038
1039 for (cap = params; cap; cap = cap->next) {
Peter Xu4a842142017-07-18 11:39:08 +08001040 cap_list[cap->value->capability] = cap->value->state;
Orit Wasserman00458432012-08-06 21:42:48 +03001041 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001042
Peter Xu4a842142017-07-18 11:39:08 +08001043#ifndef CONFIG_LIVE_BLOCK_MIGRATION
1044 if (cap_list[MIGRATION_CAPABILITY_BLOCK]) {
1045 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
1046 "block migration");
1047 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
1048 return false;
1049 }
1050#endif
1051
Zhang Chen7e934f52018-11-15 03:09:12 +08001052#ifndef CONFIG_REPLICATION
1053 if (cap_list[MIGRATION_CAPABILITY_X_COLO]) {
1054 error_setg(errp, "QEMU compiled without replication module"
1055 " can't enable COLO");
1056 error_append_hint(errp, "Please enable replication before COLO.\n");
1057 return false;
1058 }
1059#endif
1060
Peter Xu4a842142017-07-18 11:39:08 +08001061 if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +01001062 /* This check is reasonably expensive, so only when it's being
1063 * set the first time, also it's only the destination that needs
1064 * special support.
1065 */
1066 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
Alexey Perevalovd7651f12017-09-19 19:47:56 +03001067 !postcopy_ram_supported_by_host(mis)) {
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +01001068 /* postcopy_ram_supported_by_host will have emitted a more
1069 * detailed message
1070 */
Peter Xu4a842142017-07-18 11:39:08 +08001071 error_setg(errp, "Postcopy is not supported");
1072 return false;
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +01001073 }
Yury Kotov18269062019-02-15 20:45:45 +03001074
1075 if (cap_list[MIGRATION_CAPABILITY_X_IGNORE_SHARED]) {
1076 error_setg(errp, "Postcopy is not compatible with ignore-shared");
1077 return false;
1078 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001079 }
Peter Xu4a842142017-07-18 11:39:08 +08001080
1081 return true;
1082}
1083
Alexey Perevalov65ace062018-03-22 21:17:27 +03001084static void fill_destination_migration_info(MigrationInfo *info)
1085{
1086 MigrationIncomingState *mis = migration_incoming_get_current();
1087
Juan Quintela9aca82b2019-02-27 11:51:27 +01001088 if (mis->socket_address_list) {
1089 info->has_socket_address = true;
1090 info->socket_address =
1091 QAPI_CLONE(SocketAddressList, mis->socket_address_list);
1092 }
1093
Alexey Perevalov65ace062018-03-22 21:17:27 +03001094 switch (mis->state) {
1095 case MIGRATION_STATUS_NONE:
1096 return;
1097 break;
1098 case MIGRATION_STATUS_SETUP:
1099 case MIGRATION_STATUS_CANCELLING:
1100 case MIGRATION_STATUS_CANCELLED:
1101 case MIGRATION_STATUS_ACTIVE:
1102 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Peter Xu3c9928d2018-07-10 17:18:56 +08001103 case MIGRATION_STATUS_POSTCOPY_PAUSED:
1104 case MIGRATION_STATUS_POSTCOPY_RECOVER:
Alexey Perevalov65ace062018-03-22 21:17:27 +03001105 case MIGRATION_STATUS_FAILED:
1106 case MIGRATION_STATUS_COLO:
1107 info->has_status = true;
1108 break;
1109 case MIGRATION_STATUS_COMPLETED:
1110 info->has_status = true;
1111 fill_destination_postcopy_migration_info(info);
1112 break;
1113 }
1114 info->status = mis->state;
1115}
1116
1117MigrationInfo *qmp_query_migrate(Error **errp)
1118{
1119 MigrationInfo *info = g_malloc0(sizeof(*info));
1120
1121 fill_destination_migration_info(info);
1122 fill_source_migration_info(info);
1123
1124 return info;
1125}
1126
Peter Xu4a842142017-07-18 11:39:08 +08001127void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
1128 Error **errp)
1129{
1130 MigrationState *s = migrate_get_current();
1131 MigrationCapabilityStatusList *cap;
Peter Xudd0ee302018-03-05 17:49:38 +08001132 bool cap_list[MIGRATION_CAPABILITY__MAX];
Peter Xu4a842142017-07-18 11:39:08 +08001133
Juan Quintela392d87e2020-01-21 15:39:23 +01001134 if (migration_is_running(s->state)) {
Peter Xu4a842142017-07-18 11:39:08 +08001135 error_setg(errp, QERR_MIGRATION_ACTIVE);
1136 return;
1137 }
1138
Peter Xudd0ee302018-03-05 17:49:38 +08001139 memcpy(cap_list, s->enabled_capabilities, sizeof(cap_list));
1140 if (!migrate_caps_check(cap_list, params, errp)) {
Peter Xu4a842142017-07-18 11:39:08 +08001141 return;
1142 }
1143
1144 for (cap = params; cap; cap = cap->next) {
1145 s->enabled_capabilities[cap->value->capability] = cap->value->state;
1146 }
Orit Wasserman00458432012-08-06 21:42:48 +03001147}
1148
Peter Xu16d063b2017-07-18 11:39:04 +08001149/*
1150 * Check whether the parameters are valid. Error will be put into errp
1151 * (if provided). Return true if valid, otherwise false.
1152 */
1153static bool migrate_params_check(MigrationParameters *params, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +08001154{
Eric Blake7f375e02016-09-08 22:14:16 -05001155 if (params->has_compress_level &&
Juan Quintela741d4082017-12-01 13:08:38 +01001156 (params->compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001157 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
1158 "is invalid, it should be in the range of 0 to 9");
Peter Xu16d063b2017-07-18 11:39:04 +08001159 return false;
Liang Li85de8322015-03-23 16:32:28 +08001160 }
Peter Xu16d063b2017-07-18 11:39:04 +08001161
Juan Quintela741d4082017-12-01 13:08:38 +01001162 if (params->has_compress_threads && (params->compress_threads < 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001163 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1164 "compress_threads",
1165 "is invalid, it should be in the range of 1 to 255");
Peter Xu16d063b2017-07-18 11:39:04 +08001166 return false;
Liang Li85de8322015-03-23 16:32:28 +08001167 }
Peter Xu16d063b2017-07-18 11:39:04 +08001168
Juan Quintela741d4082017-12-01 13:08:38 +01001169 if (params->has_decompress_threads && (params->decompress_threads < 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001170 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1171 "decompress_threads",
1172 "is invalid, it should be in the range of 1 to 255");
Peter Xu16d063b2017-07-18 11:39:04 +08001173 return false;
Liang Li85de8322015-03-23 16:32:28 +08001174 }
Peter Xu16d063b2017-07-18 11:39:04 +08001175
Keqian Zhudc14a472020-02-24 10:31:42 +08001176 if (params->has_throttle_trigger_threshold &&
1177 (params->throttle_trigger_threshold < 1 ||
1178 params->throttle_trigger_threshold > 100)) {
1179 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1180 "throttle_trigger_threshold",
1181 "an integer in the range of 1 to 100");
1182 return false;
1183 }
1184
Eric Blake7f375e02016-09-08 22:14:16 -05001185 if (params->has_cpu_throttle_initial &&
1186 (params->cpu_throttle_initial < 1 ||
1187 params->cpu_throttle_initial > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -04001188 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -04001189 "cpu_throttle_initial",
Jason J. Herne1626fee2015-09-08 13:12:34 -04001190 "an integer in the range of 1 to 99");
Peter Xu16d063b2017-07-18 11:39:04 +08001191 return false;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001192 }
Peter Xu16d063b2017-07-18 11:39:04 +08001193
Eric Blake7f375e02016-09-08 22:14:16 -05001194 if (params->has_cpu_throttle_increment &&
1195 (params->cpu_throttle_increment < 1 ||
1196 params->cpu_throttle_increment > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -04001197 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -04001198 "cpu_throttle_increment",
Jason J. Herne1626fee2015-09-08 13:12:34 -04001199 "an integer in the range of 1 to 99");
Peter Xu16d063b2017-07-18 11:39:04 +08001200 return false;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001201 }
Peter Xu16d063b2017-07-18 11:39:04 +08001202
Juan Quintela741d4082017-12-01 13:08:38 +01001203 if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301204 error_setg(errp, "Parameter 'max_bandwidth' expects an integer in the"
1205 " range of 0 to %zu bytes/second", SIZE_MAX);
Peter Xu16d063b2017-07-18 11:39:04 +08001206 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301207 }
Peter Xu16d063b2017-07-18 11:39:04 +08001208
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301209 if (params->has_downtime_limit &&
Juan Quintela741d4082017-12-01 13:08:38 +01001210 (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03001211 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
1212 "the range of 0 to %d milliseconds",
1213 MAX_MIGRATE_DOWNTIME);
Peter Xu16d063b2017-07-18 11:39:04 +08001214 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301215 }
Peter Xu16d063b2017-07-18 11:39:04 +08001216
Juan Quintela741d4082017-12-01 13:08:38 +01001217 /* x_checkpoint_delay is now always positive */
1218
Juan Quintelacbfd6c92019-02-06 13:54:06 +01001219 if (params->has_multifd_channels && (params->multifd_channels < 1)) {
Juan Quintela4075fb12016-01-15 08:56:17 +01001220 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1221 "multifd_channels",
1222 "is invalid, it should be in the range of 1 to 255");
1223 return false;
1224 }
Peter Xu16d063b2017-07-18 11:39:04 +08001225
Juan Quintela9004db42020-01-23 17:08:52 +01001226 if (params->has_multifd_zlib_level &&
1227 (params->multifd_zlib_level > 9)) {
1228 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zlib_level",
1229 "is invalid, it should be in the range of 0 to 9");
1230 return false;
1231 }
1232
Juan Quintela6a9ad152020-01-23 17:41:36 +01001233 if (params->has_multifd_zstd_level &&
1234 (params->multifd_zstd_level > 20)) {
1235 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zstd_level",
1236 "is invalid, it should be in the range of 0 to 20");
1237 return false;
1238 }
1239
Juan Quintela73af8dd2017-10-05 21:30:10 +02001240 if (params->has_xbzrle_cache_size &&
1241 (params->xbzrle_cache_size < qemu_target_page_size() ||
1242 !is_power_of_2(params->xbzrle_cache_size))) {
1243 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1244 "xbzrle_cache_size",
1245 "is invalid, it should be bigger than target page size"
Mao Zhongyi06b1c6f2020-03-20 22:32:16 +08001246 " and a power of 2");
Juan Quintela73af8dd2017-10-05 21:30:10 +02001247 return false;
1248 }
1249
Li Qiang4cbc9c72018-08-01 06:00:20 -07001250 if (params->has_max_cpu_throttle &&
1251 (params->max_cpu_throttle < params->cpu_throttle_initial ||
1252 params->max_cpu_throttle > 99)) {
1253 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1254 "max_cpu_throttle",
1255 "an integer in the range of cpu_throttle_initial to 99");
1256 return false;
1257 }
1258
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00001259 if (params->has_announce_initial &&
1260 params->announce_initial > 100000) {
1261 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1262 "announce_initial",
1263 "is invalid, it must be less than 100000 ms");
1264 return false;
1265 }
1266 if (params->has_announce_max &&
1267 params->announce_max > 100000) {
1268 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1269 "announce_max",
1270 "is invalid, it must be less than 100000 ms");
1271 return false;
1272 }
1273 if (params->has_announce_rounds &&
1274 params->announce_rounds > 1000) {
1275 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1276 "announce_rounds",
1277 "is invalid, it must be in the range of 0 to 1000");
1278 return false;
1279 }
1280 if (params->has_announce_step &&
1281 (params->announce_step < 1 ||
1282 params->announce_step > 10000)) {
1283 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1284 "announce_step",
1285 "is invalid, it must be in the range of 1 to 10000 ms");
1286 return false;
1287 }
Peter Xu16d063b2017-07-18 11:39:04 +08001288 return true;
1289}
1290
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001291static void migrate_params_test_apply(MigrateSetParameters *params,
1292 MigrationParameters *dest)
1293{
1294 *dest = migrate_get_current()->parameters;
1295
1296 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1297
1298 if (params->has_compress_level) {
1299 dest->compress_level = params->compress_level;
1300 }
1301
1302 if (params->has_compress_threads) {
1303 dest->compress_threads = params->compress_threads;
1304 }
1305
Xiao Guangrong1d588722018-08-21 16:10:20 +08001306 if (params->has_compress_wait_thread) {
1307 dest->compress_wait_thread = params->compress_wait_thread;
1308 }
1309
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001310 if (params->has_decompress_threads) {
1311 dest->decompress_threads = params->decompress_threads;
1312 }
1313
Keqian Zhudc14a472020-02-24 10:31:42 +08001314 if (params->has_throttle_trigger_threshold) {
1315 dest->throttle_trigger_threshold = params->throttle_trigger_threshold;
1316 }
1317
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001318 if (params->has_cpu_throttle_initial) {
1319 dest->cpu_throttle_initial = params->cpu_throttle_initial;
1320 }
1321
1322 if (params->has_cpu_throttle_increment) {
1323 dest->cpu_throttle_increment = params->cpu_throttle_increment;
1324 }
1325
1326 if (params->has_tls_creds) {
Markus Armbruster01fa5592017-07-18 14:42:04 +02001327 assert(params->tls_creds->type == QTYPE_QSTRING);
1328 dest->tls_creds = g_strdup(params->tls_creds->u.s);
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001329 }
1330
1331 if (params->has_tls_hostname) {
Markus Armbruster01fa5592017-07-18 14:42:04 +02001332 assert(params->tls_hostname->type == QTYPE_QSTRING);
1333 dest->tls_hostname = g_strdup(params->tls_hostname->u.s);
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001334 }
1335
1336 if (params->has_max_bandwidth) {
1337 dest->max_bandwidth = params->max_bandwidth;
1338 }
1339
1340 if (params->has_downtime_limit) {
1341 dest->downtime_limit = params->downtime_limit;
1342 }
1343
1344 if (params->has_x_checkpoint_delay) {
1345 dest->x_checkpoint_delay = params->x_checkpoint_delay;
1346 }
1347
1348 if (params->has_block_incremental) {
1349 dest->block_incremental = params->block_incremental;
1350 }
Juan Quintelacbfd6c92019-02-06 13:54:06 +01001351 if (params->has_multifd_channels) {
1352 dest->multifd_channels = params->multifd_channels;
Juan Quintela5e7577a2017-10-09 18:07:56 +02001353 }
Juan Quintela96eef042019-01-16 10:35:55 +01001354 if (params->has_multifd_compression) {
1355 dest->multifd_compression = params->multifd_compression;
1356 }
Juan Quintela73af8dd2017-10-05 21:30:10 +02001357 if (params->has_xbzrle_cache_size) {
1358 dest->xbzrle_cache_size = params->xbzrle_cache_size;
1359 }
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01001360 if (params->has_max_postcopy_bandwidth) {
1361 dest->max_postcopy_bandwidth = params->max_postcopy_bandwidth;
1362 }
Li Qiang4cbc9c72018-08-01 06:00:20 -07001363 if (params->has_max_cpu_throttle) {
1364 dest->max_cpu_throttle = params->max_cpu_throttle;
1365 }
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00001366 if (params->has_announce_initial) {
1367 dest->announce_initial = params->announce_initial;
1368 }
1369 if (params->has_announce_max) {
1370 dest->announce_max = params->announce_max;
1371 }
1372 if (params->has_announce_rounds) {
1373 dest->announce_rounds = params->announce_rounds;
1374 }
1375 if (params->has_announce_step) {
1376 dest->announce_step = params->announce_step;
1377 }
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001378}
1379
Juan Quintela73af8dd2017-10-05 21:30:10 +02001380static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
Peter Xu16d063b2017-07-18 11:39:04 +08001381{
1382 MigrationState *s = migrate_get_current();
1383
Markus Armbrustere87fae42017-07-18 12:57:38 +02001384 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1385
Eric Blake7f375e02016-09-08 22:14:16 -05001386 if (params->has_compress_level) {
1387 s->parameters.compress_level = params->compress_level;
Liang Li85de8322015-03-23 16:32:28 +08001388 }
Peter Xu476c72a2017-07-18 11:39:05 +08001389
Eric Blake7f375e02016-09-08 22:14:16 -05001390 if (params->has_compress_threads) {
1391 s->parameters.compress_threads = params->compress_threads;
Liang Li85de8322015-03-23 16:32:28 +08001392 }
Peter Xu476c72a2017-07-18 11:39:05 +08001393
Xiao Guangrong1d588722018-08-21 16:10:20 +08001394 if (params->has_compress_wait_thread) {
1395 s->parameters.compress_wait_thread = params->compress_wait_thread;
1396 }
1397
Eric Blake7f375e02016-09-08 22:14:16 -05001398 if (params->has_decompress_threads) {
1399 s->parameters.decompress_threads = params->decompress_threads;
Liang Li85de8322015-03-23 16:32:28 +08001400 }
Peter Xu476c72a2017-07-18 11:39:05 +08001401
Keqian Zhudc14a472020-02-24 10:31:42 +08001402 if (params->has_throttle_trigger_threshold) {
1403 s->parameters.throttle_trigger_threshold = params->throttle_trigger_threshold;
1404 }
1405
Eric Blake7f375e02016-09-08 22:14:16 -05001406 if (params->has_cpu_throttle_initial) {
1407 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001408 }
Peter Xu476c72a2017-07-18 11:39:05 +08001409
Eric Blake7f375e02016-09-08 22:14:16 -05001410 if (params->has_cpu_throttle_increment) {
1411 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001412 }
Peter Xu476c72a2017-07-18 11:39:05 +08001413
Eric Blake7f375e02016-09-08 22:14:16 -05001414 if (params->has_tls_creds) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001415 g_free(s->parameters.tls_creds);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001416 assert(params->tls_creds->type == QTYPE_QSTRING);
1417 s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001418 }
Peter Xu476c72a2017-07-18 11:39:05 +08001419
Eric Blake7f375e02016-09-08 22:14:16 -05001420 if (params->has_tls_hostname) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001421 g_free(s->parameters.tls_hostname);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001422 assert(params->tls_hostname->type == QTYPE_QSTRING);
1423 s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001424 }
Peter Xu476c72a2017-07-18 11:39:05 +08001425
Daniel P. Berranged2f1d292019-02-27 14:53:24 +00001426 if (params->has_tls_authz) {
1427 g_free(s->parameters.tls_authz);
1428 assert(params->tls_authz->type == QTYPE_QSTRING);
1429 s->parameters.tls_authz = g_strdup(params->tls_authz->u.s);
1430 }
1431
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301432 if (params->has_max_bandwidth) {
1433 s->parameters.max_bandwidth = params->max_bandwidth;
Dr. David Alan Gilbertc38c1c12019-03-08 10:12:10 +00001434 if (s->to_dst_file && !migration_in_postcopy()) {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301435 qemu_file_set_rate_limit(s->to_dst_file,
1436 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
1437 }
1438 }
Peter Xu476c72a2017-07-18 11:39:05 +08001439
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301440 if (params->has_downtime_limit) {
1441 s->parameters.downtime_limit = params->downtime_limit;
1442 }
zhanghailiang68b53592016-10-27 14:43:01 +08001443
1444 if (params->has_x_checkpoint_delay) {
1445 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
zhanghailiang479125d2017-01-17 20:57:42 +08001446 if (migration_in_colo_state()) {
1447 colo_checkpoint_notify(s);
1448 }
zhanghailiang68b53592016-10-27 14:43:01 +08001449 }
Peter Xu476c72a2017-07-18 11:39:05 +08001450
Juan Quintela2833c592017-04-05 18:32:37 +02001451 if (params->has_block_incremental) {
1452 s->parameters.block_incremental = params->block_incremental;
1453 }
Juan Quintelacbfd6c92019-02-06 13:54:06 +01001454 if (params->has_multifd_channels) {
1455 s->parameters.multifd_channels = params->multifd_channels;
Juan Quintela4075fb12016-01-15 08:56:17 +01001456 }
Juan Quintela96eef042019-01-16 10:35:55 +01001457 if (params->has_multifd_compression) {
1458 s->parameters.multifd_compression = params->multifd_compression;
1459 }
Juan Quintela73af8dd2017-10-05 21:30:10 +02001460 if (params->has_xbzrle_cache_size) {
1461 s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
1462 xbzrle_cache_resize(params->xbzrle_cache_size, errp);
1463 }
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01001464 if (params->has_max_postcopy_bandwidth) {
1465 s->parameters.max_postcopy_bandwidth = params->max_postcopy_bandwidth;
Dr. David Alan Gilbertc38c1c12019-03-08 10:12:10 +00001466 if (s->to_dst_file && migration_in_postcopy()) {
1467 qemu_file_set_rate_limit(s->to_dst_file,
1468 s->parameters.max_postcopy_bandwidth / XFER_LIMIT_RATIO);
1469 }
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01001470 }
Li Qiang4cbc9c72018-08-01 06:00:20 -07001471 if (params->has_max_cpu_throttle) {
1472 s->parameters.max_cpu_throttle = params->max_cpu_throttle;
1473 }
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00001474 if (params->has_announce_initial) {
1475 s->parameters.announce_initial = params->announce_initial;
1476 }
1477 if (params->has_announce_max) {
1478 s->parameters.announce_max = params->announce_max;
1479 }
1480 if (params->has_announce_rounds) {
1481 s->parameters.announce_rounds = params->announce_rounds;
1482 }
1483 if (params->has_announce_step) {
1484 s->parameters.announce_step = params->announce_step;
1485 }
Liang Li85de8322015-03-23 16:32:28 +08001486}
1487
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001488void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
Peter Xu476c72a2017-07-18 11:39:05 +08001489{
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001490 MigrationParameters tmp;
1491
Markus Armbruster01fa5592017-07-18 14:42:04 +02001492 /* TODO Rewrite "" to null instead */
1493 if (params->has_tls_creds
1494 && params->tls_creds->type == QTYPE_QNULL) {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001495 qobject_unref(params->tls_creds->u.n);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001496 params->tls_creds->type = QTYPE_QSTRING;
1497 params->tls_creds->u.s = strdup("");
1498 }
1499 /* TODO Rewrite "" to null instead */
1500 if (params->has_tls_hostname
1501 && params->tls_hostname->type == QTYPE_QNULL) {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001502 qobject_unref(params->tls_hostname->u.n);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001503 params->tls_hostname->type = QTYPE_QSTRING;
1504 params->tls_hostname->u.s = strdup("");
1505 }
1506
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001507 migrate_params_test_apply(params, &tmp);
1508
1509 if (!migrate_params_check(&tmp, errp)) {
Peter Xu476c72a2017-07-18 11:39:05 +08001510 /* Invalid parameter */
1511 return;
1512 }
1513
Juan Quintela73af8dd2017-10-05 21:30:10 +02001514 migrate_params_apply(params, errp);
Peter Xu476c72a2017-07-18 11:39:05 +08001515}
1516
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001517
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001518void qmp_migrate_start_postcopy(Error **errp)
1519{
1520 MigrationState *s = migrate_get_current();
1521
Vladimir Sementsov-Ogievskiy16b0fd32018-03-13 15:34:01 -04001522 if (!migrate_postcopy()) {
Dr. David Alan Gilberta54d3402015-11-12 11:34:44 +00001523 error_setg(errp, "Enable postcopy with migrate_set_capability before"
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001524 " the start of migration");
1525 return;
1526 }
1527
1528 if (s->state == MIGRATION_STATUS_NONE) {
1529 error_setg(errp, "Postcopy must be started after migration has been"
1530 " started");
1531 return;
1532 }
1533 /*
1534 * we don't error if migration has finished since that would be racy
1535 * with issuing this command.
1536 */
1537 atomic_set(&s->start_postcopy, true);
1538}
1539
aliguori065e2812008-11-11 16:46:33 +00001540/* shared migration helpers */
1541
zhanghailiang48781e52015-12-16 11:47:33 +00001542void migrate_set_state(int *state, int old_state, int new_state)
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +00001543{
Peter Xua31fede2017-08-30 16:32:01 +08001544 assert(new_state < MIGRATION_STATUS__MAX);
zhanghailiang48781e52015-12-16 11:47:33 +00001545 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
Peter Xua31fede2017-08-30 16:32:01 +08001546 trace_migrate_set_state(MigrationStatus_str(new_state));
Juan Quintelab05dc722015-07-07 14:44:05 +02001547 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +00001548 }
1549}
1550
Peter Xu4e4a3d32017-07-18 11:39:09 +08001551static MigrationCapabilityStatusList *migrate_cap_add(
1552 MigrationCapabilityStatusList *list,
1553 MigrationCapability index,
1554 bool state)
Juan Quintela2833c592017-04-05 18:32:37 +02001555{
1556 MigrationCapabilityStatusList *cap;
1557
1558 cap = g_new0(MigrationCapabilityStatusList, 1);
1559 cap->value = g_new0(MigrationCapabilityStatus, 1);
Peter Xu4e4a3d32017-07-18 11:39:09 +08001560 cap->value->capability = index;
1561 cap->value->state = state;
1562 cap->next = list;
1563
1564 return cap;
1565}
1566
1567void migrate_set_block_enabled(bool value, Error **errp)
1568{
1569 MigrationCapabilityStatusList *cap;
1570
1571 cap = migrate_cap_add(NULL, MIGRATION_CAPABILITY_BLOCK, value);
Juan Quintela2833c592017-04-05 18:32:37 +02001572 qmp_migrate_set_capabilities(cap, errp);
1573 qapi_free_MigrationCapabilityStatusList(cap);
1574}
1575
1576static void migrate_set_block_incremental(MigrationState *s, bool value)
1577{
1578 s->parameters.block_incremental = value;
1579}
1580
1581static void block_cleanup_parameters(MigrationState *s)
1582{
1583 if (s->must_remove_block_options) {
1584 /* setting to false can never fail */
1585 migrate_set_block_enabled(false, &error_abort);
1586 migrate_set_block_incremental(s, false);
1587 s->must_remove_block_options = false;
1588 }
1589}
1590
Yury Kotovfd392cf2019-04-08 14:33:43 +03001591static void migrate_fd_cleanup(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +00001592{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001593 qemu_bh_delete(s->cleanup_bh);
1594 s->cleanup_bh = NULL;
1595
Peter Xu0ceccd82018-01-03 20:20:06 +08001596 qemu_savevm_state_cleanup();
1597
zhanghailiang89a02a92016-01-15 11:37:42 +08001598 if (s->to_dst_file) {
Peter Xu62df0662018-05-02 18:47:38 +08001599 QEMUFile *tmp;
Juan Quintelaf986c3d2016-01-14 16:52:55 +01001600
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001601 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001602 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001603 if (s->migration_thread_running) {
1604 qemu_thread_join(&s->thread);
1605 s->migration_thread_running = false;
1606 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001607 qemu_mutex_lock_iothread();
1608
Fei Li1398b2e2019-01-13 22:08:47 +08001609 multifd_save_cleanup();
Peter Xu62df0662018-05-02 18:47:38 +08001610 qemu_mutex_lock(&s->qemu_file_lock);
1611 tmp = s->to_dst_file;
zhanghailiang89a02a92016-01-15 11:37:42 +08001612 s->to_dst_file = NULL;
Peter Xu62df0662018-05-02 18:47:38 +08001613 qemu_mutex_unlock(&s->qemu_file_lock);
1614 /*
1615 * Close the file handle without the lock to make sure the
1616 * critical section won't block for long.
1617 */
1618 qemu_fclose(tmp);
aliguori065e2812008-11-11 16:46:33 +00001619 }
1620
Wei Yang8f8d5282019-07-17 08:53:41 +08001621 assert(!migration_is_active(s));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001622
Liang Li94f5a432015-11-02 15:37:00 +08001623 if (s->state == MIGRATION_STATUS_CANCELLING) {
zhanghailiang48781e52015-12-16 11:47:33 +00001624 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
Liang Li94f5a432015-11-02 15:37:00 +08001625 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001626 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001627
Juan Quintela87db1a72017-09-05 12:50:22 +02001628 if (s->error) {
1629 /* It is used on info migrate. We can't free it */
1630 error_report_err(error_copy(s->error));
1631 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001632 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +02001633 block_cleanup_parameters(s);
aliguori065e2812008-11-11 16:46:33 +00001634}
1635
Yury Kotovfd392cf2019-04-08 14:33:43 +03001636static void migrate_fd_cleanup_schedule(MigrationState *s)
1637{
1638 /*
1639 * Ref the state for bh, because it may be called when
1640 * there're already no other refs
1641 */
1642 object_ref(OBJECT(s));
1643 qemu_bh_schedule(s->cleanup_bh);
1644}
1645
1646static void migrate_fd_cleanup_bh(void *opaque)
1647{
1648 MigrationState *s = opaque;
1649 migrate_fd_cleanup(s);
1650 object_unref(OBJECT(s));
1651}
1652
Juan Quintela87db1a72017-09-05 12:50:22 +02001653void migrate_set_error(MigrationState *s, const Error *error)
1654{
1655 qemu_mutex_lock(&s->error_mutex);
1656 if (!s->error) {
1657 s->error = error_copy(error);
1658 }
1659 qemu_mutex_unlock(&s->error_mutex);
1660}
1661
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001662void migrate_fd_error(MigrationState *s, const Error *error)
aliguori065e2812008-11-11 16:46:33 +00001663{
Peter Maydell25174052016-10-21 18:41:45 +01001664 trace_migrate_fd_error(error_get_pretty(error));
zhanghailiang89a02a92016-01-15 11:37:42 +08001665 assert(s->to_dst_file == NULL);
zhanghailiang48781e52015-12-16 11:47:33 +00001666 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1667 MIGRATION_STATUS_FAILED);
Juan Quintela87db1a72017-09-05 12:50:22 +02001668 migrate_set_error(s, error);
Juan Quintela458cf282011-02-22 23:32:54 +01001669}
1670
Juan Quintela0edda1c2010-05-11 16:28:39 +02001671static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +00001672{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001673 int old_state ;
zhanghailiang89a02a92016-01-15 11:37:42 +08001674 QEMUFile *f = migrate_get_current()->to_dst_file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001675 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +00001676
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001677 if (s->rp_state.from_dst_file) {
1678 /* shutdown the rp socket, so causing the rp thread to shutdown */
1679 qemu_file_shutdown(s->rp_state.from_dst_file);
1680 }
1681
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001682 do {
1683 old_state = s->state;
Juan Quintela392d87e2020-01-21 15:39:23 +01001684 if (!migration_is_running(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001685 break;
1686 }
Dr. David Alan Gilberta7b36b42017-10-20 10:05:55 +01001687 /* If the migration is paused, kick it out of the pause */
1688 if (old_state == MIGRATION_STATUS_PRE_SWITCHOVER) {
1689 qemu_sem_post(&s->pause_sem);
1690 }
zhanghailiang48781e52015-12-16 11:47:33 +00001691 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
zhanghailiang31194732015-03-13 16:08:38 +08001692 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001693
1694 /*
1695 * If we're unlucky the migration code might be stuck somewhere in a
1696 * send/write while the network has failed and is waiting to timeout;
1697 * if we've got shutdown(2) available then we can force it to quit.
1698 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1699 * called in a bh, so there is no race against this cancel.
1700 */
zhanghailiang31194732015-03-13 16:08:38 +08001701 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001702 qemu_file_shutdown(f);
1703 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08001704 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
1705 Error *local_err = NULL;
1706
1707 bdrv_invalidate_cache_all(&local_err);
1708 if (local_err) {
1709 error_report_err(local_err);
1710 } else {
1711 s->block_inactive = false;
1712 }
1713 }
aliguori065e2812008-11-11 16:46:33 +00001714}
1715
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001716void add_migration_state_change_notifier(Notifier *notify)
1717{
1718 notifier_list_add(&migration_state_notifiers, notify);
1719}
1720
1721void remove_migration_state_change_notifier(Notifier *notify)
1722{
Paolo Bonzini31552522012-01-13 17:34:01 +01001723 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001724}
1725
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +02001726bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001727{
zhanghailiang31194732015-03-13 16:08:38 +08001728 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001729}
1730
Juan Quintela70736932011-02-23 00:43:59 +01001731bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001732{
zhanghailiang31194732015-03-13 16:08:38 +08001733 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001734}
Juan Quintela0edda1c2010-05-11 16:28:39 +02001735
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001736bool migration_has_failed(MigrationState *s)
1737{
zhanghailiang31194732015-03-13 16:08:38 +08001738 return (s->state == MIGRATION_STATUS_CANCELLED ||
1739 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001740}
1741
Juan Quintela57273092017-03-20 22:25:28 +01001742bool migration_in_postcopy(void)
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001743{
Juan Quintela57273092017-03-20 22:25:28 +01001744 MigrationState *s = migrate_get_current();
1745
Dr. David Alan Gilbert3748fef2019-09-23 18:49:42 +01001746 switch (s->state) {
1747 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1748 case MIGRATION_STATUS_POSTCOPY_PAUSED:
1749 case MIGRATION_STATUS_POSTCOPY_RECOVER:
1750 return true;
1751 default:
1752 return false;
1753 }
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001754}
1755
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001756bool migration_in_postcopy_after_devices(MigrationState *s)
1757{
Juan Quintela57273092017-03-20 22:25:28 +01001758 return migration_in_postcopy() && s->postcopy_after_devices;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001759}
1760
Juan Quintelafab35002017-03-22 17:36:57 +01001761bool migration_is_idle(void)
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301762{
Markus Armbrusterdaff7f02019-04-01 11:08:25 +02001763 MigrationState *s = current_migration;
1764
1765 if (!s) {
1766 return true;
1767 }
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301768
1769 switch (s->state) {
1770 case MIGRATION_STATUS_NONE:
1771 case MIGRATION_STATUS_CANCELLED:
1772 case MIGRATION_STATUS_COMPLETED:
1773 case MIGRATION_STATUS_FAILED:
1774 return true;
1775 case MIGRATION_STATUS_SETUP:
1776 case MIGRATION_STATUS_CANCELLING:
1777 case MIGRATION_STATUS_ACTIVE:
1778 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1779 case MIGRATION_STATUS_COLO:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +01001780 case MIGRATION_STATUS_PRE_SWITCHOVER:
1781 case MIGRATION_STATUS_DEVICE:
Jens Freimannc7e0acd2019-10-29 12:49:02 +01001782 case MIGRATION_STATUS_WAIT_UNPLUG:
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301783 return false;
1784 case MIGRATION_STATUS__MAX:
1785 g_assert_not_reached();
1786 }
1787
1788 return false;
1789}
1790
Wei Yang8f8d5282019-07-17 08:53:41 +08001791bool migration_is_active(MigrationState *s)
1792{
1793 return (s->state == MIGRATION_STATUS_ACTIVE ||
1794 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1795}
1796
Peter Xu3e0c8052018-02-08 18:31:15 +08001797void migrate_init(MigrationState *s)
Juan Quintela0edda1c2010-05-11 16:28:39 +02001798{
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001799 /*
1800 * Reinitialise all migration state, except
1801 * parameters/capabilities that the user set, and
1802 * locks.
1803 */
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001804 s->cleanup_bh = 0;
zhanghailiang89a02a92016-01-15 11:37:42 +08001805 s->to_dst_file = NULL;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001806 s->state = MIGRATION_STATUS_NONE;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001807 s->rp_state.from_dst_file = NULL;
1808 s->rp_state.error = false;
1809 s->mbps = 0.0;
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08001810 s->pages_per_second = 0.0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001811 s->downtime = 0;
1812 s->expected_downtime = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001813 s->setup_time = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001814 s->start_postcopy = false;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001815 s->postcopy_after_devices = false;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001816 s->migration_thread_running = false;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001817 error_free(s->error);
1818 s->error = NULL;
Juan Quintela1299c632011-11-09 21:29:01 +01001819
zhanghailiang48781e52015-12-16 11:47:33 +00001820 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +02001821
Peter Xu4af246a2018-01-03 20:20:08 +08001822 s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1823 s->total_time = 0;
Peter Xu7287cbd2018-01-03 20:20:09 +08001824 s->vm_was_running = false;
Peter Xub15df1a2018-01-03 20:20:13 +08001825 s->iteration_initial_bytes = 0;
1826 s->threshold_size = 0;
Juan Quintela0edda1c2010-05-11 16:28:39 +02001827}
Juan Quintelacab30142011-02-22 23:54:21 +01001828
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001829static GSList *migration_blockers;
1830
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301831int migrate_add_blocker(Error *reason, Error **errp)
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001832{
Markus Armbruster811f8652019-04-01 11:08:24 +02001833 if (only_migratable) {
Markus Armbruster4b576642018-10-17 10:26:25 +02001834 error_propagate_prepend(errp, error_copy(reason),
1835 "disallowing migration blocker "
Greg Kurzb6eca812019-05-16 12:58:05 +02001836 "(--only-migratable) for: ");
Ashijeet Acharyab67b8c32017-01-16 17:01:54 +05301837 return -EACCES;
1838 }
1839
Juan Quintelafab35002017-03-22 17:36:57 +01001840 if (migration_is_idle()) {
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301841 migration_blockers = g_slist_prepend(migration_blockers, reason);
1842 return 0;
1843 }
1844
Markus Armbruster4b576642018-10-17 10:26:25 +02001845 error_propagate_prepend(errp, error_copy(reason),
1846 "disallowing migration blocker "
1847 "(migration in progress) for: ");
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301848 return -EBUSY;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001849}
1850
1851void migrate_del_blocker(Error *reason)
1852{
1853 migration_blockers = g_slist_remove(migration_blockers, reason);
1854}
1855
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001856void qmp_migrate_incoming(const char *uri, Error **errp)
1857{
1858 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001859 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001860
1861 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001862 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001863 return;
1864 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001865 if (!once) {
1866 error_setg(errp, "The incoming migration has already been started");
Yury Kotov603d5a42019-11-13 20:53:25 +03001867 return;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001868 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001869
1870 qemu_start_incoming_migration(uri, &local_err);
1871
1872 if (local_err) {
1873 error_propagate(errp, local_err);
1874 return;
1875 }
1876
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001877 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001878}
1879
Peter Xu02affd42018-05-02 18:47:36 +08001880void qmp_migrate_recover(const char *uri, Error **errp)
1881{
1882 MigrationIncomingState *mis = migration_incoming_get_current();
1883
1884 if (mis->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
1885 error_setg(errp, "Migrate recover can only be run "
1886 "when postcopy is paused.");
1887 return;
1888 }
1889
1890 if (atomic_cmpxchg(&mis->postcopy_recover_triggered,
1891 false, true) == true) {
1892 error_setg(errp, "Migrate recovery is triggered already");
1893 return;
1894 }
1895
1896 /*
1897 * Note that this call will never start a real migration; it will
1898 * only re-setup the migration stream and poke existing migration
1899 * to continue using that newly established channel.
1900 */
1901 qemu_start_incoming_migration(uri, errp);
1902}
1903
Peter Xubfbf89c2018-05-02 18:47:39 +08001904void qmp_migrate_pause(Error **errp)
1905{
1906 MigrationState *ms = migrate_get_current();
1907 MigrationIncomingState *mis = migration_incoming_get_current();
1908 int ret;
1909
1910 if (ms->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1911 /* Source side, during postcopy */
1912 qemu_mutex_lock(&ms->qemu_file_lock);
1913 ret = qemu_file_shutdown(ms->to_dst_file);
1914 qemu_mutex_unlock(&ms->qemu_file_lock);
1915 if (ret) {
1916 error_setg(errp, "Failed to pause source migration");
1917 }
1918 return;
1919 }
1920
1921 if (mis->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1922 ret = qemu_file_shutdown(mis->from_src_file);
1923 if (ret) {
1924 error_setg(errp, "Failed to pause destination migration");
1925 }
1926 return;
1927 }
1928
1929 error_setg(errp, "migrate-pause is currently only supported "
1930 "during postcopy-active state");
1931}
1932
Greg Kurz24f39022016-05-04 21:44:19 +02001933bool migration_is_blocked(Error **errp)
1934{
1935 if (qemu_savevm_state_blocked(errp)) {
1936 return true;
1937 }
1938
1939 if (migration_blockers) {
Eduardo Habkost250561e2017-06-08 10:39:05 -03001940 error_propagate(errp, error_copy(migration_blockers->data));
Greg Kurz24f39022016-05-04 21:44:19 +02001941 return true;
1942 }
1943
1944 return false;
1945}
1946
Peter Xud3e35b82018-05-02 18:47:24 +08001947/* Returns true if continue to migrate, or false if error detected */
1948static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc,
1949 bool resume, Error **errp)
1950{
1951 Error *local_err = NULL;
1952
1953 if (resume) {
1954 if (s->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
1955 error_setg(errp, "Cannot resume if there is no "
1956 "paused migration");
1957 return false;
1958 }
Peter Xu97ca2112018-07-23 20:33:03 +08001959
1960 /*
1961 * Postcopy recovery won't work well with release-ram
1962 * capability since release-ram will drop the page buffer as
1963 * long as the page is put into the send buffer. So if there
1964 * is a network failure happened, any page buffers that have
1965 * not yet reached the destination VM but have already been
1966 * sent from the source VM will be lost forever. Let's refuse
1967 * the client from resuming such a postcopy migration.
1968 * Luckily release-ram was designed to only be used when src
1969 * and destination VMs are on the same host, so it should be
1970 * fine.
1971 */
1972 if (migrate_release_ram()) {
1973 error_setg(errp, "Postcopy recovery cannot work "
1974 "when release-ram capability is set");
1975 return false;
1976 }
1977
Peter Xud3e35b82018-05-02 18:47:24 +08001978 /* This is a resume, skip init status */
1979 return true;
1980 }
1981
Juan Quintela392d87e2020-01-21 15:39:23 +01001982 if (migration_is_running(s->state)) {
Peter Xud3e35b82018-05-02 18:47:24 +08001983 error_setg(errp, QERR_MIGRATION_ACTIVE);
1984 return false;
1985 }
1986
1987 if (runstate_check(RUN_STATE_INMIGRATE)) {
1988 error_setg(errp, "Guest is waiting for an incoming migration");
1989 return false;
1990 }
1991
1992 if (migration_is_blocked(errp)) {
1993 return false;
1994 }
1995
1996 if (blk || blk_inc) {
1997 if (migrate_use_block() || migrate_use_block_incremental()) {
1998 error_setg(errp, "Command options are incompatible with "
1999 "current migration capabilities");
2000 return false;
2001 }
2002 migrate_set_block_enabled(true, &local_err);
2003 if (local_err) {
2004 error_propagate(errp, local_err);
2005 return false;
2006 }
2007 s->must_remove_block_options = true;
2008 }
2009
2010 if (blk_inc) {
2011 migrate_set_block_incremental(s, true);
2012 }
2013
2014 migrate_init(s);
Ivan Ren87f3bd82019-08-02 18:18:41 +08002015 /*
2016 * set ram_counters memory to zero for a
2017 * new migration
2018 */
2019 memset(&ram_counters, 0, sizeof(ram_counters));
Peter Xud3e35b82018-05-02 18:47:24 +08002020
2021 return true;
2022}
2023
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002024void qmp_migrate(const char *uri, bool has_blk, bool blk,
2025 bool has_inc, bool inc, bool has_detach, bool detach,
Peter Xu7a4da282018-05-02 18:47:23 +08002026 bool has_resume, bool resume, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002027{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +02002028 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +02002029 MigrationState *s = migrate_get_current();
Juan Quintelacab30142011-02-22 23:54:21 +01002030 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +01002031
Peter Xud3e35b82018-05-02 18:47:24 +08002032 if (!migrate_prepare(s, has_blk && blk, has_inc && inc,
2033 has_resume && resume, errp)) {
2034 /* Error detected, put into errp */
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002035 return;
Juan Quintelacab30142011-02-22 23:54:21 +01002036 }
Juan Quintelacab30142011-02-22 23:54:21 +01002037
2038 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002039 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04002040#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08002041 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04002042 rdma_start_outgoing_migration(s, p, &local_err);
2043#endif
Juan Quintelacab30142011-02-22 23:54:21 +01002044 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002045 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01002046 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002047 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01002048 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002049 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01002050 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002051 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
2052 "a valid migration protocol");
zhanghailiang48781e52015-12-16 11:47:33 +00002053 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2054 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09576e72018-03-16 20:21:14 +00002055 block_cleanup_parameters(s);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002056 return;
Juan Quintelacab30142011-02-22 23:54:21 +01002057 }
2058
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002059 if (local_err) {
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01002060 migrate_fd_error(s, local_err);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02002061 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002062 return;
Juan Quintela1299c632011-11-09 21:29:01 +01002063 }
Juan Quintelacab30142011-02-22 23:54:21 +01002064}
2065
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02002066void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002067{
Juan Quintela17549e82011-10-05 13:50:43 +02002068 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01002069}
2070
Dr. David Alan Gilbert89cfc022017-10-20 10:05:53 +01002071void qmp_migrate_continue(MigrationStatus state, Error **errp)
2072{
2073 MigrationState *s = migrate_get_current();
2074 if (s->state != state) {
2075 error_setg(errp, "Migration not in expected state: %s",
2076 MigrationStatus_str(s->state));
2077 return;
2078 }
2079 qemu_sem_post(&s->pause_sem);
2080}
2081
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002082void qmp_migrate_set_cache_size(int64_t value, Error **errp)
2083{
Juan Quintela73af8dd2017-10-05 21:30:10 +02002084 MigrateSetParameters p = {
2085 .has_xbzrle_cache_size = true,
2086 .xbzrle_cache_size = value,
2087 };
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002088
Juan Quintela73af8dd2017-10-05 21:30:10 +02002089 qmp_migrate_set_parameters(&p, errp);
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002090}
2091
2092int64_t qmp_query_migrate_cache_size(Error **errp)
2093{
2094 return migrate_xbzrle_cache_size();
2095}
2096
Luiz Capitulino3dc85382011-11-28 11:59:37 -02002097void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002098{
Markus Armbruster1bda8b32017-07-18 13:42:11 +02002099 MigrateSetParameters p = {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302100 .has_max_bandwidth = true,
2101 .max_bandwidth = value,
2102 };
Juan Quintelacab30142011-02-22 23:54:21 +01002103
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302104 qmp_migrate_set_parameters(&p, errp);
Juan Quintelacab30142011-02-22 23:54:21 +01002105}
2106
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02002107void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01002108{
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03002109 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
2110 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
2111 "the range of 0 to %d seconds",
2112 MAX_MIGRATE_DOWNTIME_SECONDS);
2113 return;
2114 }
2115
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302116 value *= 1000; /* Convert to milliseconds */
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302117
Markus Armbruster1bda8b32017-07-18 13:42:11 +02002118 MigrateSetParameters p = {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302119 .has_downtime_limit = true,
Fangrui Song2667c982019-11-22 09:00:38 +01002120 .downtime_limit = (int64_t)value,
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302121 };
2122
2123 qmp_migrate_set_parameters(&p, errp);
aliguori5bb79102008-10-13 03:12:02 +00002124}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03002125
Pavel Butsykin53f09a12017-02-03 18:23:20 +03002126bool migrate_release_ram(void)
2127{
2128 MigrationState *s;
2129
2130 s = migrate_get_current();
2131
2132 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
2133}
2134
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00002135bool migrate_postcopy_ram(void)
2136{
2137 MigrationState *s;
2138
2139 s = migrate_get_current();
2140
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +00002141 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00002142}
2143
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002144bool migrate_postcopy(void)
2145{
Vladimir Sementsov-Ogievskiydd6bb912018-03-13 15:34:00 -04002146 return migrate_postcopy_ram() || migrate_dirty_bitmaps();
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002147}
2148
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06002149bool migrate_auto_converge(void)
2150{
2151 MigrationState *s;
2152
2153 s = migrate_get_current();
2154
2155 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
2156}
2157
Peter Lieven323004a2013-07-18 09:48:50 +02002158bool migrate_zero_blocks(void)
2159{
2160 MigrationState *s;
2161
2162 s = migrate_get_current();
2163
2164 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
2165}
2166
Alexey Perevalovf22f9282018-03-22 21:17:22 +03002167bool migrate_postcopy_blocktime(void)
2168{
2169 MigrationState *s;
2170
2171 s = migrate_get_current();
2172
2173 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME];
2174}
2175
Liang Li8706d2d2015-03-23 16:32:17 +08002176bool migrate_use_compression(void)
2177{
Liang Lidde4e692015-03-23 16:32:26 +08002178 MigrationState *s;
2179
2180 s = migrate_get_current();
2181
2182 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08002183}
2184
2185int migrate_compress_level(void)
2186{
2187 MigrationState *s;
2188
2189 s = migrate_get_current();
2190
Daniel P. Berrange2594f562016-04-27 11:05:14 +01002191 return s->parameters.compress_level;
Liang Li8706d2d2015-03-23 16:32:17 +08002192}
2193
2194int migrate_compress_threads(void)
2195{
2196 MigrationState *s;
2197
2198 s = migrate_get_current();
2199
Daniel P. Berrange2594f562016-04-27 11:05:14 +01002200 return s->parameters.compress_threads;
Liang Li8706d2d2015-03-23 16:32:17 +08002201}
2202
Xiao Guangrong1d588722018-08-21 16:10:20 +08002203int migrate_compress_wait_thread(void)
2204{
2205 MigrationState *s;
2206
2207 s = migrate_get_current();
2208
2209 return s->parameters.compress_wait_thread;
2210}
2211
Liang Li3fcb38c2015-03-23 16:32:18 +08002212int migrate_decompress_threads(void)
2213{
2214 MigrationState *s;
2215
2216 s = migrate_get_current();
2217
Daniel P. Berrange2594f562016-04-27 11:05:14 +01002218 return s->parameters.decompress_threads;
Liang Li3fcb38c2015-03-23 16:32:18 +08002219}
2220
Vladimir Sementsov-Ogievskiy55efc8c2018-03-13 15:34:00 -04002221bool migrate_dirty_bitmaps(void)
2222{
2223 MigrationState *s;
2224
2225 s = migrate_get_current();
2226
2227 return s->enabled_capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS];
2228}
2229
Yury Kotov18269062019-02-15 20:45:45 +03002230bool migrate_ignore_shared(void)
2231{
2232 MigrationState *s;
2233
2234 s = migrate_get_current();
2235
2236 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_IGNORE_SHARED];
2237}
2238
Yury Kotovb9d68df2019-09-03 19:22:44 +03002239bool migrate_validate_uuid(void)
2240{
2241 MigrationState *s;
2242
2243 s = migrate_get_current();
2244
2245 return s->enabled_capabilities[MIGRATION_CAPABILITY_VALIDATE_UUID];
2246}
2247
Juan Quintelab05dc722015-07-07 14:44:05 +02002248bool migrate_use_events(void)
2249{
2250 MigrationState *s;
2251
2252 s = migrate_get_current();
2253
2254 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
2255}
2256
Juan Quintela30126bb2016-01-14 12:23:00 +01002257bool migrate_use_multifd(void)
2258{
2259 MigrationState *s;
2260
2261 s = migrate_get_current();
2262
Juan Quintelacbfd6c92019-02-06 13:54:06 +01002263 return s->enabled_capabilities[MIGRATION_CAPABILITY_MULTIFD];
Juan Quintela30126bb2016-01-14 12:23:00 +01002264}
2265
Dr. David Alan Gilbert93fbd032017-10-20 10:05:50 +01002266bool migrate_pause_before_switchover(void)
2267{
2268 MigrationState *s;
2269
2270 s = migrate_get_current();
2271
2272 return s->enabled_capabilities[
2273 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER];
2274}
2275
Juan Quintela4075fb12016-01-15 08:56:17 +01002276int migrate_multifd_channels(void)
2277{
2278 MigrationState *s;
2279
2280 s = migrate_get_current();
2281
Juan Quintelacbfd6c92019-02-06 13:54:06 +01002282 return s->parameters.multifd_channels;
Juan Quintela4075fb12016-01-15 08:56:17 +01002283}
2284
Juan Quintelaab7cbb02019-05-15 13:37:46 +02002285MultiFDCompression migrate_multifd_compression(void)
2286{
2287 MigrationState *s;
2288
2289 s = migrate_get_current();
2290
2291 return s->parameters.multifd_compression;
2292}
2293
Juan Quintela9004db42020-01-23 17:08:52 +01002294int migrate_multifd_zlib_level(void)
2295{
2296 MigrationState *s;
2297
2298 s = migrate_get_current();
2299
2300 return s->parameters.multifd_zlib_level;
2301}
2302
Juan Quintela6a9ad152020-01-23 17:41:36 +01002303int migrate_multifd_zstd_level(void)
2304{
2305 MigrationState *s;
2306
2307 s = migrate_get_current();
2308
2309 return s->parameters.multifd_zstd_level;
2310}
2311
Orit Wasserman17ad9b32012-08-06 21:42:53 +03002312int migrate_use_xbzrle(void)
2313{
2314 MigrationState *s;
2315
2316 s = migrate_get_current();
2317
2318 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
2319}
2320
2321int64_t migrate_xbzrle_cache_size(void)
2322{
2323 MigrationState *s;
2324
2325 s = migrate_get_current();
2326
Juan Quintela73af8dd2017-10-05 21:30:10 +02002327 return s->parameters.xbzrle_cache_size;
Orit Wasserman17ad9b32012-08-06 21:42:53 +03002328}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002329
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01002330static int64_t migrate_max_postcopy_bandwidth(void)
2331{
2332 MigrationState *s;
2333
2334 s = migrate_get_current();
2335
2336 return s->parameters.max_postcopy_bandwidth;
2337}
2338
Juan Quintela2833c592017-04-05 18:32:37 +02002339bool migrate_use_block(void)
2340{
2341 MigrationState *s;
2342
2343 s = migrate_get_current();
2344
2345 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
2346}
2347
Peter Xuc788ada2017-06-26 18:28:55 +08002348bool migrate_use_return_path(void)
2349{
2350 MigrationState *s;
2351
2352 s = migrate_get_current();
2353
2354 return s->enabled_capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
2355}
2356
Juan Quintela2833c592017-04-05 18:32:37 +02002357bool migrate_use_block_incremental(void)
2358{
2359 MigrationState *s;
2360
2361 s = migrate_get_current();
2362
2363 return s->parameters.block_incremental;
2364}
2365
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002366/* migration thread support */
2367/*
2368 * Something bad happened to the RP stream, mark an error
2369 * The caller shall print or trace something to indicate why
2370 */
2371static void mark_source_rp_bad(MigrationState *s)
2372{
2373 s->rp_state.error = true;
2374}
2375
2376static struct rp_cmd_args {
2377 ssize_t len; /* -1 = variable */
2378 const char *name;
2379} rp_cmd_args[] = {
2380 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
2381 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
2382 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002383 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
2384 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Peter Xua335deb2018-05-02 18:47:28 +08002385 [MIG_RP_MSG_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" },
Peter Xu13955b82018-05-02 18:47:30 +08002386 [MIG_RP_MSG_RESUME_ACK] = { .len = 4, .name = "RESUME_ACK" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002387 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
2388};
2389
2390/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002391 * Process a request for pages received on the return path,
2392 * We're allowed to send more than requested (e.g. to round to our page size)
2393 * and we don't need to send pages that have already been sent.
2394 */
2395static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
2396 ram_addr_t start, size_t len)
2397{
Wei Yang038adc22019-10-13 10:11:45 +08002398 long our_host_ps = qemu_real_host_page_size;
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00002399
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002400 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00002401
2402 /*
2403 * Since we currently insist on matching page sizes, just sanity check
2404 * we're being asked for whole host pages.
2405 */
2406 if (start & (our_host_ps-1) ||
2407 (len & (our_host_ps-1))) {
2408 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
2409 " len: %zd", __func__, start, len);
2410 mark_source_rp_bad(ms);
2411 return;
2412 }
2413
Juan Quintela96506892017-03-14 18:41:03 +01002414 if (ram_save_queue_pages(rbname, start, len)) {
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00002415 mark_source_rp_bad(ms);
2416 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002417}
2418
Peter Xu14b17422018-05-02 18:47:21 +08002419/* Return true to retry, false to quit */
2420static bool postcopy_pause_return_path_thread(MigrationState *s)
2421{
2422 trace_postcopy_pause_return_path();
2423
2424 qemu_sem_wait(&s->postcopy_pause_rp_sem);
2425
2426 trace_postcopy_pause_return_path_continued();
2427
2428 return true;
2429}
2430
Peter Xua335deb2018-05-02 18:47:28 +08002431static int migrate_handle_rp_recv_bitmap(MigrationState *s, char *block_name)
2432{
2433 RAMBlock *block = qemu_ram_block_by_name(block_name);
2434
2435 if (!block) {
2436 error_report("%s: invalid block name '%s'", __func__, block_name);
2437 return -EINVAL;
2438 }
2439
2440 /* Fetch the received bitmap and refresh the dirty bitmap */
2441 return ram_dirty_bitmap_reload(s, block);
2442}
2443
Peter Xu13955b82018-05-02 18:47:30 +08002444static int migrate_handle_rp_resume_ack(MigrationState *s, uint32_t value)
2445{
2446 trace_source_return_path_thread_resume_ack(value);
2447
2448 if (value != MIGRATION_RESUME_ACK_VALUE) {
2449 error_report("%s: illegal resume_ack value %"PRIu32,
2450 __func__, value);
2451 return -1;
2452 }
2453
2454 /* Now both sides are active. */
2455 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
2456 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2457
Peter Xu94190692018-05-02 18:47:34 +08002458 /* Notify send thread that time to continue send pages */
2459 qemu_sem_post(&s->rp_state.rp_sem);
Peter Xu13955b82018-05-02 18:47:30 +08002460
2461 return 0;
2462}
2463
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002464/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002465 * Handles messages sent on the return path towards the source VM
2466 *
2467 */
2468static void *source_return_path_thread(void *opaque)
2469{
2470 MigrationState *ms = opaque;
2471 QEMUFile *rp = ms->rp_state.from_dst_file;
2472 uint16_t header_len, header_type;
Peter Xu568b01c2016-03-09 14:12:12 +08002473 uint8_t buf[512];
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002474 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002475 ram_addr_t start = 0; /* =0 to silence warning */
2476 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002477 int res;
2478
2479 trace_source_return_path_thread_entry();
Lidong Chen74637e62018-08-06 21:29:29 +08002480 rcu_register_thread();
Peter Xu14b17422018-05-02 18:47:21 +08002481
2482retry:
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002483 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
2484 migration_is_setup_or_active(ms->state)) {
2485 trace_source_return_path_thread_loop_top();
2486 header_type = qemu_get_be16(rp);
2487 header_len = qemu_get_be16(rp);
2488
Peter Xu7a9ddfb2018-02-08 18:31:05 +08002489 if (qemu_file_get_error(rp)) {
2490 mark_source_rp_bad(ms);
2491 goto out;
2492 }
2493
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002494 if (header_type >= MIG_RP_MSG_MAX ||
2495 header_type == MIG_RP_MSG_INVALID) {
2496 error_report("RP: Received invalid message 0x%04x length 0x%04x",
2497 header_type, header_len);
2498 mark_source_rp_bad(ms);
2499 goto out;
2500 }
2501
2502 if ((rp_cmd_args[header_type].len != -1 &&
2503 header_len != rp_cmd_args[header_type].len) ||
Peter Xu568b01c2016-03-09 14:12:12 +08002504 header_len > sizeof(buf)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002505 error_report("RP: Received '%s' message (0x%04x) with"
2506 "incorrect length %d expecting %zu",
2507 rp_cmd_args[header_type].name, header_type, header_len,
2508 (size_t)rp_cmd_args[header_type].len);
2509 mark_source_rp_bad(ms);
2510 goto out;
2511 }
2512
2513 /* We know we've got a valid header by this point */
2514 res = qemu_get_buffer(rp, buf, header_len);
2515 if (res != header_len) {
2516 error_report("RP: Failed reading data for message 0x%04x"
2517 " read %d expected %d",
2518 header_type, res, header_len);
2519 mark_source_rp_bad(ms);
2520 goto out;
2521 }
2522
2523 /* OK, we have the message and the data */
2524 switch (header_type) {
2525 case MIG_RP_MSG_SHUT:
Peter Maydell4d885132016-06-10 17:09:22 +01002526 sibling_error = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002527 trace_source_return_path_thread_shut(sibling_error);
2528 if (sibling_error) {
2529 error_report("RP: Sibling indicated error %d", sibling_error);
2530 mark_source_rp_bad(ms);
2531 }
2532 /*
2533 * We'll let the main thread deal with closing the RP
2534 * we could do a shutdown(2) on it, but we're the only user
2535 * anyway, so there's nothing gained.
2536 */
2537 goto out;
2538
2539 case MIG_RP_MSG_PONG:
Peter Maydell4d885132016-06-10 17:09:22 +01002540 tmp32 = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002541 trace_source_return_path_thread_pong(tmp32);
2542 break;
2543
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002544 case MIG_RP_MSG_REQ_PAGES:
Peter Maydell4d885132016-06-10 17:09:22 +01002545 start = ldq_be_p(buf);
2546 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002547 migrate_handle_rp_req_pages(ms, NULL, start, len);
2548 break;
2549
2550 case MIG_RP_MSG_REQ_PAGES_ID:
2551 expected_len = 12 + 1; /* header + termination */
2552
2553 if (header_len >= expected_len) {
Peter Maydell4d885132016-06-10 17:09:22 +01002554 start = ldq_be_p(buf);
2555 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002556 /* Now we expect an idstr */
2557 tmp32 = buf[12]; /* Length of the following idstr */
2558 buf[13 + tmp32] = '\0';
2559 expected_len += tmp32;
2560 }
2561 if (header_len != expected_len) {
2562 error_report("RP: Req_Page_id with length %d expecting %zd",
2563 header_len, expected_len);
2564 mark_source_rp_bad(ms);
2565 goto out;
2566 }
2567 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
2568 break;
2569
Peter Xua335deb2018-05-02 18:47:28 +08002570 case MIG_RP_MSG_RECV_BITMAP:
2571 if (header_len < 1) {
2572 error_report("%s: missing block name", __func__);
2573 mark_source_rp_bad(ms);
2574 goto out;
2575 }
2576 /* Format: len (1B) + idstr (<255B). This ends the idstr. */
2577 buf[buf[0] + 1] = '\0';
2578 if (migrate_handle_rp_recv_bitmap(ms, (char *)(buf + 1))) {
2579 mark_source_rp_bad(ms);
2580 goto out;
2581 }
2582 break;
2583
Peter Xu13955b82018-05-02 18:47:30 +08002584 case MIG_RP_MSG_RESUME_ACK:
2585 tmp32 = ldl_be_p(buf);
2586 if (migrate_handle_rp_resume_ack(ms, tmp32)) {
2587 mark_source_rp_bad(ms);
2588 goto out;
2589 }
2590 break;
2591
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002592 default:
2593 break;
2594 }
2595 }
Peter Xu14b17422018-05-02 18:47:21 +08002596
2597out:
2598 res = qemu_file_get_error(rp);
2599 if (res) {
Dr. David Alan Gilbertfb14a422019-10-07 11:35:07 +01002600 if (res == -EIO && migration_in_postcopy()) {
Peter Xu14b17422018-05-02 18:47:21 +08002601 /*
2602 * Maybe there is something we can do: it looks like a
2603 * network down issue, and we pause for a recovery.
2604 */
2605 if (postcopy_pause_return_path_thread(ms)) {
2606 /* Reload rp, reset the rest */
Marc-André Lureau0284a2a2018-09-25 13:22:45 +04002607 if (rp != ms->rp_state.from_dst_file) {
2608 qemu_fclose(rp);
2609 rp = ms->rp_state.from_dst_file;
2610 }
Peter Xu14b17422018-05-02 18:47:21 +08002611 ms->rp_state.error = false;
2612 goto retry;
2613 }
2614 }
2615
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002616 trace_source_return_path_thread_bad_end();
2617 mark_source_rp_bad(ms);
2618 }
2619
2620 trace_source_return_path_thread_end();
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002621 ms->rp_state.from_dst_file = NULL;
2622 qemu_fclose(rp);
Lidong Chen74637e62018-08-06 21:29:29 +08002623 rcu_unregister_thread();
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002624 return NULL;
2625}
2626
Peter Xud3e35b82018-05-02 18:47:24 +08002627static int open_return_path_on_source(MigrationState *ms,
2628 bool create_thread)
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002629{
2630
zhanghailiang89a02a92016-01-15 11:37:42 +08002631 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002632 if (!ms->rp_state.from_dst_file) {
2633 return -1;
2634 }
2635
2636 trace_open_return_path_on_source();
Peter Xud3e35b82018-05-02 18:47:24 +08002637
2638 if (!create_thread) {
2639 /* We're done */
2640 return 0;
2641 }
2642
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002643 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
2644 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
2645
2646 trace_open_return_path_on_source_continue();
2647
2648 return 0;
2649}
2650
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002651/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
2652static int await_return_path_close_on_source(MigrationState *ms)
2653{
2654 /*
2655 * If this is a normal exit then the destination will send a SHUT and the
2656 * rp_thread will exit, however if there's an error we need to cause
2657 * it to exit.
2658 */
zhanghailiang89a02a92016-01-15 11:37:42 +08002659 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002660 /*
2661 * shutdown(2), if we have it, will cause it to unblock if it's stuck
2662 * waiting for the destination.
2663 */
2664 qemu_file_shutdown(ms->rp_state.from_dst_file);
2665 mark_source_rp_bad(ms);
2666 }
2667 trace_await_return_path_close_on_source_joining();
2668 qemu_thread_join(&ms->rp_state.rp_thread);
2669 trace_await_return_path_close_on_source_close();
2670 return ms->rp_state.error;
2671}
2672
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002673/*
2674 * Switch from normal iteration to postcopy
2675 * Returns non-0 on error
2676 */
Peter Xu7287cbd2018-01-03 20:20:09 +08002677static int postcopy_start(MigrationState *ms)
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002678{
2679 int ret;
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002680 QIOChannelBuffer *bioc;
2681 QEMUFile *fb;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002682 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01002683 int64_t bandwidth = migrate_max_postcopy_bandwidth();
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002684 bool restart_block = false;
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002685 int cur_state = MIGRATION_STATUS_ACTIVE;
2686 if (!migrate_pause_before_switchover()) {
2687 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
2688 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2689 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002690
2691 trace_postcopy_start();
2692 qemu_mutex_lock_iothread();
2693 trace_postcopy_start_set_run();
2694
Daniel Henrique Barbozafb064112018-12-05 17:47:01 -02002695 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002696 global_state_store();
2697 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01002698 if (ret < 0) {
2699 goto fail;
2700 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002701
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002702 ret = migration_maybe_pause(ms, &cur_state,
2703 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2704 if (ret < 0) {
2705 goto fail;
2706 }
2707
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01002708 ret = bdrv_inactivate_all();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002709 if (ret < 0) {
2710 goto fail;
2711 }
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002712 restart_block = true;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002713
2714 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00002715 * Cause any non-postcopiable, but iterative devices to
2716 * send out their final data.
2717 */
Fam Zhenga1fbe752017-06-17 00:06:58 +08002718 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00002719
2720 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002721 * in Finish migrate and with the io-lock held everything should
2722 * be quiet, but we've potentially still got dirty pages and we
2723 * need to tell the destination to throw any pages it's already received
2724 * that are dirty
2725 */
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002726 if (migrate_postcopy_ram()) {
2727 if (ram_postcopy_send_discard_bitmap(ms)) {
2728 error_report("postcopy send discard bitmap failed");
2729 goto fail;
2730 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002731 }
2732
2733 /*
2734 * send rest of state - note things that are doing postcopy
2735 * will notice we're in POSTCOPY_ACTIVE and not actually
2736 * wrap their state up here
2737 */
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01002738 /* 0 max-postcopy-bandwidth means unlimited */
2739 if (!bandwidth) {
2740 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
2741 } else {
2742 qemu_file_set_rate_limit(ms->to_dst_file, bandwidth / XFER_LIMIT_RATIO);
2743 }
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002744 if (migrate_postcopy_ram()) {
2745 /* Ping just for debugging, helps line traces up */
2746 qemu_savevm_send_ping(ms->to_dst_file, 2);
2747 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002748
2749 /*
2750 * While loading the device state we may trigger page transfer
2751 * requests and the fd must be free to process those, and thus
2752 * the destination must read the whole device state off the fd before
2753 * it starts processing it. Unfortunately the ad-hoc migration format
2754 * doesn't allow the destination to know the size to read without fully
2755 * parsing it through each devices load-state code (especially the open
2756 * coded devices that use get/put).
2757 * So we wrap the device state up in a package with a length at the start;
2758 * to do this we use a qemu_buf to hold the whole of the device state.
2759 */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002760 bioc = qio_channel_buffer_new(4096);
Daniel P. Berrange6f01f132016-09-30 11:57:14 +01002761 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002762 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
2763 object_unref(OBJECT(bioc));
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002764
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00002765 /*
2766 * Make sure the receiver can get incoming pages before we send the rest
2767 * of the state
2768 */
2769 qemu_savevm_send_postcopy_listen(fb);
2770
Fam Zhenga1fbe752017-06-17 00:06:58 +08002771 qemu_savevm_state_complete_precopy(fb, false, false);
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002772 if (migrate_postcopy_ram()) {
2773 qemu_savevm_send_ping(fb, 3);
2774 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002775
2776 qemu_savevm_send_postcopy_run(fb);
2777
2778 /* <><> end of stuff going into the package */
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002779
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002780 /* Last point of recovery; as soon as we send the package the destination
2781 * can open devices and potentially start running.
2782 * Lets just check again we've not got any errors.
2783 */
2784 ret = qemu_file_get_error(ms->to_dst_file);
2785 if (ret) {
2786 error_report("postcopy_start: Migration stream errored (pre package)");
2787 goto fail_closefb;
2788 }
2789
2790 restart_block = false;
2791
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002792 /* Now send that blob */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002793 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002794 goto fail_closefb;
2795 }
2796 qemu_fclose(fb);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00002797
2798 /* Send a notify to give a chance for anything that needs to happen
2799 * at the transition to postcopy and after the device state; in particular
2800 * spice needs to trigger a transition now
2801 */
2802 ms->postcopy_after_devices = true;
2803 notifier_list_notify(&migration_state_notifiers, ms);
2804
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002805 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
2806
2807 qemu_mutex_unlock_iothread();
2808
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002809 if (migrate_postcopy_ram()) {
2810 /*
2811 * Although this ping is just for debug, it could potentially be
2812 * used for getting a better measurement of downtime at the source.
2813 */
2814 qemu_savevm_send_ping(ms->to_dst_file, 4);
2815 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002816
Pavel Butsykinced1c612017-02-03 18:23:21 +03002817 if (migrate_release_ram()) {
2818 ram_postcopy_migrated_memory_release(ms);
2819 }
2820
zhanghailiang89a02a92016-01-15 11:37:42 +08002821 ret = qemu_file_get_error(ms->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002822 if (ret) {
2823 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00002824 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002825 MIGRATION_STATUS_FAILED);
2826 }
2827
2828 return ret;
2829
2830fail_closefb:
2831 qemu_fclose(fb);
2832fail:
zhanghailiang48781e52015-12-16 11:47:33 +00002833 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002834 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002835 if (restart_block) {
2836 /* A failure happened early enough that we know the destination hasn't
2837 * accessed block devices, so we're safe to recover.
2838 */
2839 Error *local_err = NULL;
2840
2841 bdrv_invalidate_cache_all(&local_err);
2842 if (local_err) {
2843 error_report_err(local_err);
2844 }
2845 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002846 qemu_mutex_unlock_iothread();
2847 return -1;
2848}
2849
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002850/**
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002851 * migration_maybe_pause: Pause if required to by
2852 * migrate_pause_before_switchover called with the iothread locked
2853 * Returns: 0 on success
2854 */
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002855static int migration_maybe_pause(MigrationState *s,
2856 int *current_active_state,
2857 int new_state)
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002858{
2859 if (!migrate_pause_before_switchover()) {
2860 return 0;
2861 }
2862
2863 /* Since leaving this state is not atomic with posting the semaphore
2864 * it's possible that someone could have issued multiple migrate_continue
2865 * and the semaphore is incorrectly positive at this point;
2866 * the docs say it's undefined to reinit a semaphore that's already
2867 * init'd, so use timedwait to eat up any existing posts.
2868 */
2869 while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) {
2870 /* This block intentionally left blank */
2871 }
2872
Zhimin Feng89583382020-01-14 17:43:09 +08002873 /*
2874 * If the migration is cancelled when it is in the completion phase,
2875 * the migration state is set to MIGRATION_STATUS_CANCELLING.
2876 * So we don't need to wait a semaphore, otherwise we would always
2877 * wait for the 'pause_sem' semaphore.
2878 */
2879 if (s->state != MIGRATION_STATUS_CANCELLING) {
2880 qemu_mutex_unlock_iothread();
2881 migrate_set_state(&s->state, *current_active_state,
2882 MIGRATION_STATUS_PRE_SWITCHOVER);
2883 qemu_sem_wait(&s->pause_sem);
2884 migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER,
2885 new_state);
2886 *current_active_state = new_state;
2887 qemu_mutex_lock_iothread();
2888 }
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002889
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002890 return s->state == new_state ? 0 : -EINVAL;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002891}
2892
2893/**
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002894 * migration_completion: Used by migration_thread when there's not much left.
2895 * The caller 'breaks' the loop when this returns.
2896 *
2897 * @s: Current migration state
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002898 */
Peter Xu2ad87302018-01-03 20:20:14 +08002899static void migration_completion(MigrationState *s)
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002900{
2901 int ret;
Peter Xu2ad87302018-01-03 20:20:14 +08002902 int current_active_state = s->state;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002903
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002904 if (s->state == MIGRATION_STATUS_ACTIVE) {
2905 qemu_mutex_lock_iothread();
Peter Xu64909f92018-01-03 20:20:10 +08002906 s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Daniel Henrique Barbozafb064112018-12-05 17:47:01 -02002907 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
Peter Xu7287cbd2018-01-03 20:20:09 +08002908 s->vm_was_running = runstate_is_running();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002909 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002910
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002911 if (!ret) {
Fam Zhenga1fbe752017-06-17 00:06:58 +08002912 bool inactivate = !migrate_colo_enabled();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002913 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02002914 if (ret >= 0) {
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002915 ret = migration_maybe_pause(s, &current_active_state,
2916 MIGRATION_STATUS_DEVICE);
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002917 }
2918 if (ret >= 0) {
Kevin Wolff07fa4c2017-05-22 17:10:38 +02002919 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
Fam Zhenga1fbe752017-06-17 00:06:58 +08002920 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
2921 inactivate);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02002922 }
Fam Zhenga1fbe752017-06-17 00:06:58 +08002923 if (inactivate && ret >= 0) {
2924 s->block_inactive = true;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002925 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002926 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002927 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002928
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002929 if (ret < 0) {
2930 goto fail;
2931 }
2932 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2933 trace_migration_completion_postcopy_end();
2934
zhanghailiang89a02a92016-01-15 11:37:42 +08002935 qemu_savevm_state_complete_postcopy(s->to_dst_file);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002936 trace_migration_completion_postcopy_end_after_complete();
2937 }
2938
2939 /*
2940 * If rp was opened we must clean up the thread before
2941 * cleaning everything else up (since if there are no failures
2942 * it will wait for the destination to send it's status in
2943 * a SHUT command).
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002944 */
Peter Xu0425dc92017-05-31 18:35:34 +08002945 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002946 int rp_error;
Peter Xu0425dc92017-05-31 18:35:34 +08002947 trace_migration_return_path_end_before();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002948 rp_error = await_return_path_close_on_source(s);
Peter Xu0425dc92017-05-31 18:35:34 +08002949 trace_migration_return_path_end_after(rp_error);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002950 if (rp_error) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02002951 goto fail_invalidate;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002952 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002953 }
2954
zhanghailiang89a02a92016-01-15 11:37:42 +08002955 if (qemu_file_get_error(s->to_dst_file)) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002956 trace_migration_completion_file_err();
Greg Kurzfe904ea2016-05-18 15:44:36 +02002957 goto fail_invalidate;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002958 }
2959
zhanghailiang0b827d52016-10-27 14:42:54 +08002960 if (!migrate_colo_enabled()) {
2961 migrate_set_state(&s->state, current_active_state,
2962 MIGRATION_STATUS_COMPLETED);
2963 }
2964
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002965 return;
2966
Greg Kurzfe904ea2016-05-18 15:44:36 +02002967fail_invalidate:
2968 /* If not doing postcopy, vm_start() will be called: let's regain
2969 * control on images.
2970 */
Dr. David Alan Gilbert6039dd52018-02-05 09:13:37 +00002971 if (s->state == MIGRATION_STATUS_ACTIVE ||
2972 s->state == MIGRATION_STATUS_DEVICE) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02002973 Error *local_err = NULL;
2974
zhanghailiang1d2acc32017-01-24 15:59:52 +08002975 qemu_mutex_lock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02002976 bdrv_invalidate_cache_all(&local_err);
2977 if (local_err) {
2978 error_report_err(local_err);
zhanghailiang1d2acc32017-01-24 15:59:52 +08002979 } else {
2980 s->block_inactive = false;
Greg Kurzfe904ea2016-05-18 15:44:36 +02002981 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08002982 qemu_mutex_unlock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02002983 }
2984
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002985fail:
zhanghailiang48781e52015-12-16 11:47:33 +00002986 migrate_set_state(&s->state, current_active_state,
2987 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002988}
2989
zhanghailiang35a6ed42016-10-27 14:42:52 +08002990bool migrate_colo_enabled(void)
2991{
2992 MigrationState *s = migrate_get_current();
2993 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
2994}
2995
Peter Xub23c2ad2018-05-02 18:47:19 +08002996typedef enum MigThrError {
2997 /* No error detected */
2998 MIG_THR_ERR_NONE = 0,
2999 /* Detected error, but resumed successfully */
3000 MIG_THR_ERR_RECOVERED = 1,
3001 /* Detected fatal error, need to exit */
3002 MIG_THR_ERR_FATAL = 2,
3003} MigThrError;
3004
Peter Xu94190692018-05-02 18:47:34 +08003005static int postcopy_resume_handshake(MigrationState *s)
3006{
3007 qemu_savevm_send_postcopy_resume(s->to_dst_file);
3008
3009 while (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
3010 qemu_sem_wait(&s->rp_state.rp_sem);
3011 }
3012
3013 if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
3014 return 0;
3015 }
3016
3017 return -1;
3018}
3019
Peter Xu135b87b2018-05-02 18:47:25 +08003020/* Return zero if success, or <0 for error */
3021static int postcopy_do_resume(MigrationState *s)
3022{
Peter Xud1b8ead2018-05-02 18:47:31 +08003023 int ret;
3024
3025 /*
3026 * Call all the resume_prepare() hooks, so that modules can be
3027 * ready for the migration resume.
3028 */
3029 ret = qemu_savevm_state_resume_prepare(s);
3030 if (ret) {
3031 error_report("%s: resume_prepare() failure detected: %d",
3032 __func__, ret);
3033 return ret;
3034 }
3035
3036 /*
Peter Xu94190692018-05-02 18:47:34 +08003037 * Last handshake with destination on the resume (destination will
3038 * switch to postcopy-active afterwards)
Peter Xud1b8ead2018-05-02 18:47:31 +08003039 */
Peter Xu94190692018-05-02 18:47:34 +08003040 ret = postcopy_resume_handshake(s);
3041 if (ret) {
3042 error_report("%s: handshake failed: %d", __func__, ret);
3043 return ret;
3044 }
Peter Xud1b8ead2018-05-02 18:47:31 +08003045
Peter Xu135b87b2018-05-02 18:47:25 +08003046 return 0;
3047}
3048
Peter Xub23c2ad2018-05-02 18:47:19 +08003049/*
3050 * We don't return until we are in a safe state to continue current
3051 * postcopy migration. Returns MIG_THR_ERR_RECOVERED if recovered, or
3052 * MIG_THR_ERR_FATAL if unrecovery failure happened.
3053 */
3054static MigThrError postcopy_pause(MigrationState *s)
3055{
3056 assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
Peter Xub23c2ad2018-05-02 18:47:19 +08003057
Peter Xu135b87b2018-05-02 18:47:25 +08003058 while (true) {
Peter Xu62df0662018-05-02 18:47:38 +08003059 QEMUFile *file;
3060
Peter Xu135b87b2018-05-02 18:47:25 +08003061 migrate_set_state(&s->state, s->state,
3062 MIGRATION_STATUS_POSTCOPY_PAUSED);
Peter Xub23c2ad2018-05-02 18:47:19 +08003063
Peter Xu135b87b2018-05-02 18:47:25 +08003064 /* Current channel is possibly broken. Release it. */
3065 assert(s->to_dst_file);
Peter Xu62df0662018-05-02 18:47:38 +08003066 qemu_mutex_lock(&s->qemu_file_lock);
3067 file = s->to_dst_file;
Peter Xu135b87b2018-05-02 18:47:25 +08003068 s->to_dst_file = NULL;
Peter Xu62df0662018-05-02 18:47:38 +08003069 qemu_mutex_unlock(&s->qemu_file_lock);
3070
3071 qemu_file_shutdown(file);
3072 qemu_fclose(file);
Peter Xub23c2ad2018-05-02 18:47:19 +08003073
Peter Xu135b87b2018-05-02 18:47:25 +08003074 error_report("Detected IO failure for postcopy. "
3075 "Migration paused.");
3076
3077 /*
3078 * We wait until things fixed up. Then someone will setup the
3079 * status back for us.
3080 */
3081 while (s->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
3082 qemu_sem_wait(&s->postcopy_pause_sem);
3083 }
3084
3085 if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
3086 /* Woken up by a recover procedure. Give it a shot */
3087
3088 /*
3089 * Firstly, let's wake up the return path now, with a new
3090 * return path channel.
3091 */
3092 qemu_sem_post(&s->postcopy_pause_rp_sem);
3093
3094 /* Do the resume logic */
3095 if (postcopy_do_resume(s) == 0) {
3096 /* Let's continue! */
3097 trace_postcopy_pause_continued();
3098 return MIG_THR_ERR_RECOVERED;
3099 } else {
3100 /*
3101 * Something wrong happened during the recovery, let's
3102 * pause again. Pause is always better than throwing
3103 * data away.
3104 */
3105 continue;
3106 }
3107 } else {
3108 /* This is not right... Time to quit. */
3109 return MIG_THR_ERR_FATAL;
3110 }
Peter Xub23c2ad2018-05-02 18:47:19 +08003111 }
Peter Xub23c2ad2018-05-02 18:47:19 +08003112}
3113
3114static MigThrError migration_detect_error(MigrationState *s)
3115{
3116 int ret;
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003117 int state = s->state;
Yury Kotov3d661c82019-04-22 13:34:20 +03003118 Error *local_error = NULL;
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003119
3120 if (state == MIGRATION_STATUS_CANCELLING ||
3121 state == MIGRATION_STATUS_CANCELLED) {
3122 /* End the migration, but don't set the state to failed */
3123 return MIG_THR_ERR_FATAL;
3124 }
Peter Xub23c2ad2018-05-02 18:47:19 +08003125
3126 /* Try to detect any file errors */
Yury Kotov3d661c82019-04-22 13:34:20 +03003127 ret = qemu_file_get_error_obj(s->to_dst_file, &local_error);
Peter Xub23c2ad2018-05-02 18:47:19 +08003128 if (!ret) {
3129 /* Everything is fine */
Yury Kotov3d661c82019-04-22 13:34:20 +03003130 assert(!local_error);
Peter Xub23c2ad2018-05-02 18:47:19 +08003131 return MIG_THR_ERR_NONE;
3132 }
3133
Yury Kotov3d661c82019-04-22 13:34:20 +03003134 if (local_error) {
3135 migrate_set_error(s, local_error);
3136 error_free(local_error);
3137 }
3138
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003139 if (state == MIGRATION_STATUS_POSTCOPY_ACTIVE && ret == -EIO) {
Peter Xub23c2ad2018-05-02 18:47:19 +08003140 /*
3141 * For postcopy, we allow the network to be down for a
3142 * while. After that, it can be continued by a
3143 * recovery phase.
3144 */
3145 return postcopy_pause(s);
3146 } else {
3147 /*
3148 * For precopy (or postcopy with error outside IO), we fail
3149 * with no time.
3150 */
Dr. David Alan Gilbertc3c5eae2019-02-19 19:59:28 +00003151 migrate_set_state(&s->state, state, MIGRATION_STATUS_FAILED);
Peter Xub23c2ad2018-05-02 18:47:19 +08003152 trace_migration_thread_file_err();
3153
3154 /* Time to stop the migration, now. */
3155 return MIG_THR_ERR_FATAL;
3156 }
3157}
3158
Wei Yang268dcd42019-09-12 10:49:57 +08003159/* How many bytes have we transferred since the beginning of the migration */
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003160static uint64_t migration_total_bytes(MigrationState *s)
3161{
Juan Quintelab9ee2f72016-01-15 11:40:13 +01003162 return qemu_ftell(s->to_dst_file) + ram_counters.multifd_bytes;
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003163}
3164
Peter Xucf011f02018-01-03 20:20:11 +08003165static void migration_calculate_complete(MigrationState *s)
3166{
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003167 uint64_t bytes = migration_total_bytes(s);
Peter Xucf011f02018-01-03 20:20:11 +08003168 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela6cde6fb2018-06-26 15:26:35 +02003169 int64_t transfer_time;
Peter Xucf011f02018-01-03 20:20:11 +08003170
3171 s->total_time = end_time - s->start_time;
3172 if (!s->downtime) {
3173 /*
3174 * It's still not set, so we are precopy migration. For
3175 * postcopy, downtime is calculated during postcopy_start().
3176 */
3177 s->downtime = end_time - s->downtime_start;
3178 }
3179
Juan Quintela6cde6fb2018-06-26 15:26:35 +02003180 transfer_time = s->total_time - s->setup_time;
3181 if (transfer_time) {
3182 s->mbps = ((double) bytes * 8.0) / transfer_time / 1000;
Peter Xucf011f02018-01-03 20:20:11 +08003183 }
3184}
3185
Ivan Ren87f3bd82019-08-02 18:18:41 +08003186static void update_iteration_initial_status(MigrationState *s)
3187{
3188 /*
3189 * Update these three fields at the same time to avoid mismatch info lead
3190 * wrong speed calculation.
3191 */
3192 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3193 s->iteration_initial_bytes = migration_total_bytes(s);
3194 s->iteration_initial_pages = ram_get_total_transferred_pages();
3195}
3196
Peter Xub15df1a2018-01-03 20:20:13 +08003197static void migration_update_counters(MigrationState *s,
3198 int64_t current_time)
3199{
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08003200 uint64_t transferred, transferred_pages, time_spent;
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003201 uint64_t current_bytes; /* bytes transferred since the beginning */
Peter Xub15df1a2018-01-03 20:20:13 +08003202 double bandwidth;
3203
3204 if (current_time < s->iteration_start_time + BUFFER_DELAY) {
3205 return;
3206 }
3207
Juan Quintela0c8f0ef2018-06-26 15:38:00 +02003208 current_bytes = migration_total_bytes(s);
3209 transferred = current_bytes - s->iteration_initial_bytes;
Peter Xub15df1a2018-01-03 20:20:13 +08003210 time_spent = current_time - s->iteration_start_time;
3211 bandwidth = (double)transferred / time_spent;
Wei Wang0781c1e2018-01-22 19:36:39 +08003212 s->threshold_size = bandwidth * s->parameters.downtime_limit;
Peter Xub15df1a2018-01-03 20:20:13 +08003213
3214 s->mbps = (((double) transferred * 8.0) /
3215 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
3216
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08003217 transferred_pages = ram_get_total_transferred_pages() -
3218 s->iteration_initial_pages;
3219 s->pages_per_second = (double) transferred_pages /
3220 (((double) time_spent / 1000.0));
3221
Peter Xub15df1a2018-01-03 20:20:13 +08003222 /*
3223 * if we haven't sent anything, we don't want to
3224 * recalculate. 10000 is a small enough number for our purposes
3225 */
3226 if (ram_counters.dirty_pages_rate && transferred > 10000) {
Balamuruhan S650af892018-06-12 14:20:09 +05303227 s->expected_downtime = ram_counters.remaining / bandwidth;
Peter Xub15df1a2018-01-03 20:20:13 +08003228 }
3229
3230 qemu_file_reset_rate_limit(s->to_dst_file);
3231
Ivan Ren87f3bd82019-08-02 18:18:41 +08003232 update_iteration_initial_status(s);
Peter Xub15df1a2018-01-03 20:20:13 +08003233
3234 trace_migrate_transferred(transferred, time_spent,
Wei Wang0781c1e2018-01-22 19:36:39 +08003235 bandwidth, s->threshold_size);
Peter Xub15df1a2018-01-03 20:20:13 +08003236}
3237
Peter Xu2ad87302018-01-03 20:20:14 +08003238/* Migration thread iteration status */
3239typedef enum {
3240 MIG_ITERATE_RESUME, /* Resume current iteration */
3241 MIG_ITERATE_SKIP, /* Skip current iteration */
3242 MIG_ITERATE_BREAK, /* Break the loop */
3243} MigIterateState;
3244
3245/*
3246 * Return true if continue to the next iteration directly, false
3247 * otherwise.
3248 */
3249static MigIterateState migration_iteration_run(MigrationState *s)
3250{
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04003251 uint64_t pending_size, pend_pre, pend_compat, pend_post;
Peter Xu2ad87302018-01-03 20:20:14 +08003252 bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE;
3253
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04003254 qemu_savevm_state_pending(s->to_dst_file, s->threshold_size, &pend_pre,
3255 &pend_compat, &pend_post);
3256 pending_size = pend_pre + pend_compat + pend_post;
Peter Xu2ad87302018-01-03 20:20:14 +08003257
3258 trace_migrate_pending(pending_size, s->threshold_size,
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04003259 pend_pre, pend_compat, pend_post);
Peter Xu2ad87302018-01-03 20:20:14 +08003260
3261 if (pending_size && pending_size >= s->threshold_size) {
3262 /* Still a significant amount to transfer */
Wei Yang52aec702019-07-18 16:37:47 +08003263 if (!in_postcopy && pend_pre <= s->threshold_size &&
Peter Xu2ad87302018-01-03 20:20:14 +08003264 atomic_read(&s->start_postcopy)) {
3265 if (postcopy_start(s)) {
3266 error_report("%s: postcopy failed to start", __func__);
3267 }
3268 return MIG_ITERATE_SKIP;
3269 }
3270 /* Just another iteration step */
Wei Yang17d93512019-10-06 06:05:16 +08003271 qemu_savevm_state_iterate(s->to_dst_file, in_postcopy);
Peter Xu2ad87302018-01-03 20:20:14 +08003272 } else {
3273 trace_migration_thread_low_pending(pending_size);
3274 migration_completion(s);
3275 return MIG_ITERATE_BREAK;
3276 }
3277
3278 return MIG_ITERATE_RESUME;
3279}
3280
Peter Xu199aa6d2018-01-03 20:20:15 +08003281static void migration_iteration_finish(MigrationState *s)
3282{
3283 /* If we enabled cpu throttling for auto-converge, turn it off. */
3284 cpu_throttle_stop();
3285
3286 qemu_mutex_lock_iothread();
3287 switch (s->state) {
3288 case MIGRATION_STATUS_COMPLETED:
3289 migration_calculate_complete(s);
3290 runstate_set(RUN_STATE_POSTMIGRATE);
3291 break;
3292
3293 case MIGRATION_STATUS_ACTIVE:
3294 /*
3295 * We should really assert here, but since it's during
3296 * migration, let's try to reduce the usage of assertions.
3297 */
3298 if (!migrate_colo_enabled()) {
3299 error_report("%s: critical error: calling COLO code without "
3300 "COLO enabled", __func__);
3301 }
3302 migrate_start_colo_process(s);
3303 /*
3304 * Fixme: we will run VM in COLO no matter its old running state.
3305 * After exited COLO, we will keep running.
3306 */
3307 s->vm_was_running = true;
3308 /* Fallthrough */
3309 case MIGRATION_STATUS_FAILED:
3310 case MIGRATION_STATUS_CANCELLED:
Dr. David Alan Gilbert57225e52018-07-19 10:22:57 +01003311 case MIGRATION_STATUS_CANCELLING:
Peter Xu199aa6d2018-01-03 20:20:15 +08003312 if (s->vm_was_running) {
3313 vm_start();
3314 } else {
3315 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
3316 runstate_set(RUN_STATE_POSTMIGRATE);
3317 }
3318 }
3319 break;
3320
3321 default:
3322 /* Should not reach here, but if so, forgive the VM. */
3323 error_report("%s: Unknown ending state %d", __func__, s->state);
3324 break;
3325 }
Yury Kotovfd392cf2019-04-08 14:33:43 +03003326 migrate_fd_cleanup_schedule(s);
Peter Xu199aa6d2018-01-03 20:20:15 +08003327 qemu_mutex_unlock_iothread();
3328}
3329
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003330void migration_make_urgent_request(void)
3331{
3332 qemu_sem_post(&migrate_get_current()->rate_limit_sem);
3333}
3334
3335void migration_consume_urgent_request(void)
3336{
3337 qemu_sem_wait(&migrate_get_current()->rate_limit_sem);
3338}
3339
Dr. David Alan Gilbert97e1e062019-12-05 10:29:18 +00003340/* Returns true if the rate limiting was broken by an urgent request */
3341bool migration_rate_limit(void)
3342{
3343 int64_t now = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3344 MigrationState *s = migrate_get_current();
3345
3346 bool urgent = false;
3347 migration_update_counters(s, now);
3348 if (qemu_file_rate_limit(s->to_dst_file)) {
3349 /*
3350 * Wait for a delay to do rate limiting OR
3351 * something urgent to post the semaphore.
3352 */
3353 int ms = s->iteration_start_time + BUFFER_DELAY - now;
3354 trace_migration_rate_limit_pre(ms);
3355 if (qemu_sem_timedwait(&s->rate_limit_sem, ms) == 0) {
3356 /*
3357 * We were woken by one or more urgent things but
3358 * the timedwait will have consumed one of them.
3359 * The service routine for the urgent wake will dec
3360 * the semaphore itself for each item it consumes,
3361 * so add this one we just eat back.
3362 */
3363 qemu_sem_post(&s->rate_limit_sem);
3364 urgent = true;
3365 }
3366 trace_migration_rate_limit_post(urgent);
3367 }
3368 return urgent;
3369}
3370
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00003371/*
3372 * Master migration thread on the source VM.
3373 * It drives the migration and pumps the data down the outgoing channel.
3374 */
Juan Quintela5f496a12013-02-22 17:36:30 +01003375static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003376{
Juan Quintela9848a402012-12-19 09:55:50 +01003377 MigrationState *s = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +01003378 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Peter Xub23c2ad2018-05-02 18:47:19 +08003379 MigThrError thr_error;
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003380 bool urgent = false;
Juan Quintela76f59332012-10-03 20:16:24 +02003381
Paolo Bonziniab28bd22015-07-09 08:55:38 +02003382 rcu_register_thread();
3383
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +00003384 object_ref(OBJECT(s));
Ivan Ren87f3bd82019-08-02 18:18:41 +08003385 update_iteration_initial_status(s);
Peter Xub15df1a2018-01-03 20:20:13 +08003386
zhanghailiang89a02a92016-01-15 11:37:42 +08003387 qemu_savevm_state_header(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003388
Peter Xu62a02652017-06-14 15:55:58 +08003389 /*
3390 * If we opened the return path, we need to make sure dst has it
3391 * opened as well.
3392 */
3393 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003394 /* Now tell the dest that it should open its end so it can reply */
zhanghailiang89a02a92016-01-15 11:37:42 +08003395 qemu_savevm_send_open_return_path(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003396
3397 /* And do a ping that will make stuff easier to debug */
zhanghailiang89a02a92016-01-15 11:37:42 +08003398 qemu_savevm_send_ping(s->to_dst_file, 1);
Peter Xu0425dc92017-05-31 18:35:34 +08003399 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003400
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03003401 if (migrate_postcopy()) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003402 /*
3403 * Tell the destination that we *might* want to do postcopy later;
3404 * if the other end can't do postcopy it should fail now, nice and
3405 * early.
3406 */
zhanghailiang89a02a92016-01-15 11:37:42 +08003407 qemu_savevm_send_postcopy_advise(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003408 }
3409
Zhang Chenaad555c2018-09-03 12:38:47 +08003410 if (migrate_colo_enabled()) {
3411 /* Notify migration destination that we enable COLO */
3412 qemu_savevm_send_colo_enable(s->to_dst_file);
3413 }
3414
Juan Quintela9907e842017-06-28 11:52:24 +02003415 qemu_savevm_state_setup(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003416
Keqian Zhud05de9e2020-02-04 13:08:41 +08003417 if (qemu_savevm_state_guest_unplug_pending()) {
Jens Freimannc7e0acd2019-10-29 12:49:02 +01003418 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3419 MIGRATION_STATUS_WAIT_UNPLUG);
3420
3421 while (s->state == MIGRATION_STATUS_WAIT_UNPLUG &&
3422 qemu_savevm_state_guest_unplug_pending()) {
3423 qemu_sem_timedwait(&s->wait_unplug_sem, 250);
3424 }
3425
3426 migrate_set_state(&s->state, MIGRATION_STATUS_WAIT_UNPLUG,
3427 MIGRATION_STATUS_ACTIVE);
3428 }
3429
Alex Blighbc72ad62013-08-21 16:03:08 +01003430 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang48781e52015-12-16 11:47:33 +00003431 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3432 MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04003433
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00003434 trace_migration_thread_setup_complete();
3435
Wei Yang8f8d5282019-07-17 08:53:41 +08003436 while (migration_is_active(s)) {
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003437 if (urgent || !qemu_file_rate_limit(s->to_dst_file)) {
Peter Xu2ad87302018-01-03 20:20:14 +08003438 MigIterateState iter_state = migration_iteration_run(s);
3439 if (iter_state == MIG_ITERATE_SKIP) {
3440 continue;
3441 } else if (iter_state == MIG_ITERATE_BREAK) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01003442 break;
Juan Quintelac369f402012-10-03 20:33:34 +02003443 }
3444 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01003445
Peter Xub23c2ad2018-05-02 18:47:19 +08003446 /*
3447 * Try to detect any kind of failures, and see whether we
3448 * should stop the migration now.
3449 */
3450 thr_error = migration_detect_error(s);
3451 if (thr_error == MIG_THR_ERR_FATAL) {
3452 /* Stop migration */
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01003453 break;
Peter Xub23c2ad2018-05-02 18:47:19 +08003454 } else if (thr_error == MIG_THR_ERR_RECOVERED) {
3455 /*
3456 * Just recovered from a e.g. network failure, reset all
3457 * the local variables. This is important to avoid
3458 * breaking transferred_bytes and bandwidth calculation
3459 */
Ivan Ren87f3bd82019-08-02 18:18:41 +08003460 update_iteration_initial_status(s);
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01003461 }
Peter Xub15df1a2018-01-03 20:20:13 +08003462
Dr. David Alan Gilbert97e1e062019-12-05 10:29:18 +00003463 urgent = migration_rate_limit();
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01003464 }
3465
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003466 trace_migration_thread_after_loop();
Peter Xu199aa6d2018-01-03 20:20:15 +08003467 migration_iteration_finish(s);
Dr. David Alan Gilbert892ae712019-02-27 16:49:00 +00003468 object_unref(OBJECT(s));
Paolo Bonziniab28bd22015-07-09 08:55:38 +02003469 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003470 return NULL;
3471}
3472
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00003473void migrate_fd_connect(MigrationState *s, Error *error_in)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003474{
Juan Quintela00f4b572019-06-12 11:33:27 +02003475 Error *local_err = NULL;
Peter Xud3e35b82018-05-02 18:47:24 +08003476 int64_t rate_limit;
3477 bool resume = s->state == MIGRATION_STATUS_POSTCOPY_PAUSED;
3478
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05303479 s->expected_downtime = s->parameters.downtime_limit;
Yury Kotovfd392cf2019-04-08 14:33:43 +03003480 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup_bh, s);
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00003481 if (error_in) {
3482 migrate_fd_error(s, error_in);
3483 migrate_fd_cleanup(s);
3484 return;
3485 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003486
Peter Xud3e35b82018-05-02 18:47:24 +08003487 if (resume) {
3488 /* This is a resumed migration */
Peter Xu8504dde2019-09-06 21:01:03 +08003489 rate_limit = s->parameters.max_postcopy_bandwidth /
3490 XFER_LIMIT_RATIO;
Peter Xud3e35b82018-05-02 18:47:24 +08003491 } else {
3492 /* This is a fresh new migration */
3493 rate_limit = s->parameters.max_bandwidth / XFER_LIMIT_RATIO;
Paolo Bonzini442773c2013-02-22 17:36:44 +01003494
Peter Xud3e35b82018-05-02 18:47:24 +08003495 /* Notify before starting migration thread */
3496 notifier_list_notify(&migration_state_notifiers, s);
3497 }
3498
3499 qemu_file_set_rate_limit(s->to_dst_file, rate_limit);
3500 qemu_file_set_blocking(s->to_dst_file, true);
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02003501
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003502 /*
Peter Xuc788ada2017-06-26 18:28:55 +08003503 * Open the return path. For postcopy, it is used exclusively. For
3504 * precopy, only if user specified "return-path" capability would
3505 * QEMU uses the return path.
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003506 */
Peter Xuc788ada2017-06-26 18:28:55 +08003507 if (migrate_postcopy_ram() || migrate_use_return_path()) {
Peter Xud3e35b82018-05-02 18:47:24 +08003508 if (open_return_path_on_source(s, !resume)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003509 error_report("Unable to open return-path for postcopy");
Peter Xud3e35b82018-05-02 18:47:24 +08003510 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003511 migrate_fd_cleanup(s);
3512 return;
3513 }
3514 }
3515
Peter Xud3e35b82018-05-02 18:47:24 +08003516 if (resume) {
Peter Xu135b87b2018-05-02 18:47:25 +08003517 /* Wakeup the main migration thread to do the recovery */
3518 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
3519 MIGRATION_STATUS_POSTCOPY_RECOVER);
3520 qemu_sem_post(&s->postcopy_pause_sem);
Peter Xud3e35b82018-05-02 18:47:24 +08003521 return;
3522 }
3523
Juan Quintela00f4b572019-06-12 11:33:27 +02003524 if (multifd_save_setup(&local_err) != 0) {
3525 error_report_err(local_err);
Juan Quintelaf986c3d2016-01-14 16:52:55 +01003526 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3527 MIGRATION_STATUS_FAILED);
3528 migrate_fd_cleanup(s);
3529 return;
3530 }
Pankaj Gupta009fad72017-01-23 19:12:56 +05303531 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01003532 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00003533 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02003534}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00003535
Peter Xu9d18af92017-06-27 12:10:19 +08003536void migration_global_dump(Monitor *mon)
3537{
3538 MigrationState *ms = migrate_get_current();
3539
Juan Quintela6f0f6422017-10-26 11:49:57 +02003540 monitor_printf(mon, "globals:\n");
3541 monitor_printf(mon, "store-global-state: %s\n",
3542 ms->store_global_state ? "on" : "off");
3543 monitor_printf(mon, "only-migratable: %s\n",
Markus Armbruster811f8652019-04-01 11:08:24 +02003544 only_migratable ? "on" : "off");
Juan Quintela6f0f6422017-10-26 11:49:57 +02003545 monitor_printf(mon, "send-configuration: %s\n",
3546 ms->send_configuration ? "on" : "off");
3547 monitor_printf(mon, "send-section-footer: %s\n",
3548 ms->send_section_footer ? "on" : "off");
Xiao Guangrongf5482222018-05-03 16:06:11 +08003549 monitor_printf(mon, "decompress-error-check: %s\n",
3550 ms->decompress_error_check ? "on" : "off");
Peter Xu002cad62019-06-03 14:50:56 +08003551 monitor_printf(mon, "clear-bitmap-shift: %u\n",
3552 ms->clear_bitmap_shift);
Peter Xu9d18af92017-06-27 12:10:19 +08003553}
3554
Peter Xu20814752017-07-18 11:39:03 +08003555#define DEFINE_PROP_MIG_CAP(name, x) \
3556 DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
3557
Peter Xu52722982017-06-27 12:10:14 +08003558static Property migration_properties[] = {
3559 DEFINE_PROP_BOOL("store-global-state", MigrationState,
3560 store_global_state, true),
Peter Xu71dd4c12017-06-27 12:10:16 +08003561 DEFINE_PROP_BOOL("send-configuration", MigrationState,
3562 send_configuration, true),
Peter Xu15c38502017-06-27 12:10:17 +08003563 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
3564 send_section_footer, true),
Xiao Guangrongf5482222018-05-03 16:06:11 +08003565 DEFINE_PROP_BOOL("decompress-error-check", MigrationState,
3566 decompress_error_check, true),
Peter Xu002cad62019-06-03 14:50:56 +08003567 DEFINE_PROP_UINT8("x-clear-bitmap-shift", MigrationState,
3568 clear_bitmap_shift, CLEAR_BITMAP_SHIFT_DEFAULT),
Peter Xu89632fa2017-07-18 11:39:02 +08003569
3570 /* Migration parameters */
Juan Quintela741d4082017-12-01 13:08:38 +01003571 DEFINE_PROP_UINT8("x-compress-level", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003572 parameters.compress_level,
3573 DEFAULT_MIGRATE_COMPRESS_LEVEL),
Juan Quintela741d4082017-12-01 13:08:38 +01003574 DEFINE_PROP_UINT8("x-compress-threads", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003575 parameters.compress_threads,
3576 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
Xiao Guangrong1d588722018-08-21 16:10:20 +08003577 DEFINE_PROP_BOOL("x-compress-wait-thread", MigrationState,
3578 parameters.compress_wait_thread, true),
Juan Quintela741d4082017-12-01 13:08:38 +01003579 DEFINE_PROP_UINT8("x-decompress-threads", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003580 parameters.decompress_threads,
3581 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
Keqian Zhudc14a472020-02-24 10:31:42 +08003582 DEFINE_PROP_UINT8("x-throttle-trigger-threshold", MigrationState,
3583 parameters.throttle_trigger_threshold,
3584 DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD),
Juan Quintela741d4082017-12-01 13:08:38 +01003585 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003586 parameters.cpu_throttle_initial,
3587 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
Juan Quintela741d4082017-12-01 13:08:38 +01003588 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003589 parameters.cpu_throttle_increment,
3590 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
Juan Quintela741d4082017-12-01 13:08:38 +01003591 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003592 parameters.max_bandwidth, MAX_THROTTLE),
Juan Quintela741d4082017-12-01 13:08:38 +01003593 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003594 parameters.downtime_limit,
3595 DEFAULT_MIGRATE_SET_DOWNTIME),
Juan Quintela741d4082017-12-01 13:08:38 +01003596 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08003597 parameters.x_checkpoint_delay,
3598 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
Juan Quintelacbfd6c92019-02-06 13:54:06 +01003599 DEFINE_PROP_UINT8("multifd-channels", MigrationState,
3600 parameters.multifd_channels,
Juan Quintela4075fb12016-01-15 08:56:17 +01003601 DEFAULT_MIGRATE_MULTIFD_CHANNELS),
Juan Quintela96eef042019-01-16 10:35:55 +01003602 DEFINE_PROP_MULTIFD_COMPRESSION("multifd-compression", MigrationState,
3603 parameters.multifd_compression,
3604 DEFAULT_MIGRATE_MULTIFD_COMPRESSION),
Juan Quintela9004db42020-01-23 17:08:52 +01003605 DEFINE_PROP_UINT8("multifd-zlib-level", MigrationState,
3606 parameters.multifd_zlib_level,
3607 DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL),
Juan Quintela6a9ad152020-01-23 17:41:36 +01003608 DEFINE_PROP_UINT8("multifd-zstd-level", MigrationState,
3609 parameters.multifd_zstd_level,
3610 DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL),
Juan Quintela73af8dd2017-10-05 21:30:10 +02003611 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
3612 parameters.xbzrle_cache_size,
3613 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01003614 DEFINE_PROP_SIZE("max-postcopy-bandwidth", MigrationState,
3615 parameters.max_postcopy_bandwidth,
3616 DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH),
Li Qiang4cbc9c72018-08-01 06:00:20 -07003617 DEFINE_PROP_UINT8("max-cpu-throttle", MigrationState,
3618 parameters.max_cpu_throttle,
3619 DEFAULT_MIGRATE_MAX_CPU_THROTTLE),
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00003620 DEFINE_PROP_SIZE("announce-initial", MigrationState,
3621 parameters.announce_initial,
3622 DEFAULT_MIGRATE_ANNOUNCE_INITIAL),
3623 DEFINE_PROP_SIZE("announce-max", MigrationState,
3624 parameters.announce_max,
3625 DEFAULT_MIGRATE_ANNOUNCE_MAX),
3626 DEFINE_PROP_SIZE("announce-rounds", MigrationState,
3627 parameters.announce_rounds,
3628 DEFAULT_MIGRATE_ANNOUNCE_ROUNDS),
3629 DEFINE_PROP_SIZE("announce-step", MigrationState,
3630 parameters.announce_step,
3631 DEFAULT_MIGRATE_ANNOUNCE_STEP),
Peter Xu20814752017-07-18 11:39:03 +08003632
3633 /* Migration capabilities */
3634 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
3635 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
3636 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
3637 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
3638 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
3639 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
3640 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
3641 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
3642 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
3643 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
3644 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
Juan Quintelacbfd6c92019-02-06 13:54:06 +01003645 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_MULTIFD),
Peter Xu20814752017-07-18 11:39:03 +08003646
Peter Xu52722982017-06-27 12:10:14 +08003647 DEFINE_PROP_END_OF_LIST(),
3648};
3649
Peter Xue5cb7e72017-06-27 12:10:13 +08003650static void migration_class_init(ObjectClass *klass, void *data)
3651{
3652 DeviceClass *dc = DEVICE_CLASS(klass);
3653
3654 dc->user_creatable = false;
Marc-André Lureau4f67d302020-01-10 19:30:32 +04003655 device_class_set_props(dc, migration_properties);
Peter Xue5cb7e72017-06-27 12:10:13 +08003656}
3657
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003658static void migration_instance_finalize(Object *obj)
3659{
3660 MigrationState *ms = MIGRATION_OBJ(obj);
3661 MigrationParameters *params = &ms->parameters;
3662
Juan Quintela87db1a72017-09-05 12:50:22 +02003663 qemu_mutex_destroy(&ms->error_mutex);
Peter Xu62df0662018-05-02 18:47:38 +08003664 qemu_mutex_destroy(&ms->qemu_file_lock);
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003665 g_free(params->tls_hostname);
3666 g_free(params->tls_creds);
Jens Freimannc7e0acd2019-10-29 12:49:02 +01003667 qemu_sem_destroy(&ms->wait_unplug_sem);
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003668 qemu_sem_destroy(&ms->rate_limit_sem);
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01003669 qemu_sem_destroy(&ms->pause_sem);
Peter Xub23c2ad2018-05-02 18:47:19 +08003670 qemu_sem_destroy(&ms->postcopy_pause_sem);
Peter Xu14b17422018-05-02 18:47:21 +08003671 qemu_sem_destroy(&ms->postcopy_pause_rp_sem);
Peter Xuedd090c2018-05-02 18:47:32 +08003672 qemu_sem_destroy(&ms->rp_state.rp_sem);
Marc-André Lureauab105cc2018-03-06 18:09:59 +01003673 error_free(ms->error);
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003674}
3675
Peter Xue5cb7e72017-06-27 12:10:13 +08003676static void migration_instance_init(Object *obj)
3677{
3678 MigrationState *ms = MIGRATION_OBJ(obj);
Peter Xu8b0b29d2017-07-18 11:39:06 +08003679 MigrationParameters *params = &ms->parameters;
Peter Xue5cb7e72017-06-27 12:10:13 +08003680
3681 ms->state = MIGRATION_STATUS_NONE;
Peter Xue5cb7e72017-06-27 12:10:13 +08003682 ms->mbps = -1;
Xiao Guangrongaecbfe92019-01-11 14:37:30 +08003683 ms->pages_per_second = -1;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01003684 qemu_sem_init(&ms->pause_sem, 0);
Juan Quintela87db1a72017-09-05 12:50:22 +02003685 qemu_mutex_init(&ms->error_mutex);
Peter Xu8b0b29d2017-07-18 11:39:06 +08003686
3687 params->tls_hostname = g_strdup("");
3688 params->tls_creds = g_strdup("");
3689
3690 /* Set has_* up only for parameter checks */
3691 params->has_compress_level = true;
3692 params->has_compress_threads = true;
3693 params->has_decompress_threads = true;
Keqian Zhudc14a472020-02-24 10:31:42 +08003694 params->has_throttle_trigger_threshold = true;
Peter Xu8b0b29d2017-07-18 11:39:06 +08003695 params->has_cpu_throttle_initial = true;
3696 params->has_cpu_throttle_increment = true;
3697 params->has_max_bandwidth = true;
3698 params->has_downtime_limit = true;
3699 params->has_x_checkpoint_delay = true;
3700 params->has_block_incremental = true;
Juan Quintelacbfd6c92019-02-06 13:54:06 +01003701 params->has_multifd_channels = true;
Juan Quintela96eef042019-01-16 10:35:55 +01003702 params->has_multifd_compression = true;
Juan Quintela9004db42020-01-23 17:08:52 +01003703 params->has_multifd_zlib_level = true;
Juan Quintela6a9ad152020-01-23 17:41:36 +01003704 params->has_multifd_zstd_level = true;
Juan Quintela73af8dd2017-10-05 21:30:10 +02003705 params->has_xbzrle_cache_size = true;
Dr. David Alan Gilbert7e555c62018-06-13 11:26:40 +01003706 params->has_max_postcopy_bandwidth = true;
Li Qiang4cbc9c72018-08-01 06:00:20 -07003707 params->has_max_cpu_throttle = true;
Dr. David Alan Gilbertee3d96b2019-02-27 13:24:06 +00003708 params->has_announce_initial = true;
3709 params->has_announce_max = true;
3710 params->has_announce_rounds = true;
3711 params->has_announce_step = true;
Peter Xub23c2ad2018-05-02 18:47:19 +08003712
3713 qemu_sem_init(&ms->postcopy_pause_sem, 0);
Peter Xu14b17422018-05-02 18:47:21 +08003714 qemu_sem_init(&ms->postcopy_pause_rp_sem, 0);
Peter Xuedd090c2018-05-02 18:47:32 +08003715 qemu_sem_init(&ms->rp_state.rp_sem, 0);
Dr. David Alan Gilbertad767be2018-06-13 11:26:41 +01003716 qemu_sem_init(&ms->rate_limit_sem, 0);
Jens Freimannc7e0acd2019-10-29 12:49:02 +01003717 qemu_sem_init(&ms->wait_unplug_sem, 0);
Peter Xu62df0662018-05-02 18:47:38 +08003718 qemu_mutex_init(&ms->qemu_file_lock);
Peter Xu8b0b29d2017-07-18 11:39:06 +08003719}
3720
3721/*
3722 * Return true if check pass, false otherwise. Error will be put
3723 * inside errp if provided.
3724 */
3725static bool migration_object_check(MigrationState *ms, Error **errp)
3726{
Peter Xu6b19a7d2017-07-18 11:39:10 +08003727 MigrationCapabilityStatusList *head = NULL;
3728 /* Assuming all off */
3729 bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret;
3730 int i;
3731
Peter Xu8b0b29d2017-07-18 11:39:06 +08003732 if (!migrate_params_check(&ms->parameters, errp)) {
3733 return false;
3734 }
3735
Peter Xu6b19a7d2017-07-18 11:39:10 +08003736 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
3737 if (ms->enabled_capabilities[i]) {
3738 head = migrate_cap_add(head, i, true);
3739 }
3740 }
3741
3742 ret = migrate_caps_check(cap_list, head, errp);
3743
3744 /* It works with head == NULL */
3745 qapi_free_MigrationCapabilityStatusList(head);
3746
3747 return ret;
Peter Xue5cb7e72017-06-27 12:10:13 +08003748}
3749
3750static const TypeInfo migration_type = {
3751 .name = TYPE_MIGRATION,
Peter Xu01f6e142017-06-28 15:15:44 +08003752 /*
Peter Xuc8d3ff32017-07-05 16:21:23 +08003753 * NOTE: TYPE_MIGRATION is not really a device, as the object is
3754 * not created using qdev_create(), it is not attached to the qdev
3755 * device tree, and it is never realized.
3756 *
3757 * TODO: Make this TYPE_OBJECT once QOM provides something like
3758 * TYPE_DEVICE's "-global" properties.
Peter Xu01f6e142017-06-28 15:15:44 +08003759 */
Peter Xue5cb7e72017-06-27 12:10:13 +08003760 .parent = TYPE_DEVICE,
3761 .class_init = migration_class_init,
3762 .class_size = sizeof(MigrationClass),
3763 .instance_size = sizeof(MigrationState),
3764 .instance_init = migration_instance_init,
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003765 .instance_finalize = migration_instance_finalize,
Peter Xue5cb7e72017-06-27 12:10:13 +08003766};
3767
3768static void register_migration_types(void)
3769{
3770 type_register_static(&migration_type);
3771}
3772
3773type_init(register_migration_types);