blob: 4a7c02fd3c9ff7bc6cde08b10cd6f4929d83ec4d [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"
Juan Quintela795c40b2017-04-06 12:00:28 +020019#include "migration/blocker.h"
Juan Quintelaf4dbe1b2017-04-05 15:54:10 +020020#include "exec.h"
Juan Quintela7fcac4a2017-04-05 15:58:29 +020021#include "fd.h"
Juan Quintela61e8b142017-04-05 17:40:11 +020022#include "socket.h"
Juan Quintelae1a3ece2017-04-17 20:32:36 +020023#include "rdma.h"
Juan Quintela7b1e1a22017-04-17 20:26:27 +020024#include "ram.h"
Juan Quintela84a899d2017-04-24 18:53:30 +020025#include "migration/global_state.h"
Juan Quintelac4b63b72017-04-24 19:02:44 +020026#include "migration/misc.h"
Juan Quintela6666c962017-04-24 20:07:27 +020027#include "migration.h"
Juan Quintela20a519a2017-04-20 14:48:46 +020028#include "savevm.h"
Juan Quintela40014d82017-04-17 19:34:36 +020029#include "qemu-file-channel.h"
Juan Quintela08a0aee2017-04-20 18:52:18 +020030#include "qemu-file.h"
Juan Quintela987772d2017-04-17 19:02:59 +020031#include "migration/vmstate.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010032#include "block/block.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010033#include "qapi/error.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010034#include "qapi/qapi-commands-migration.h"
35#include "qapi/qapi-events-migration.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010036#include "qapi/qmp/qerror.h"
Markus Armbruster15280c32018-02-01 12:18:36 +010037#include "qapi/qmp/qnull.h"
Paolo Bonziniab28bd22015-07-09 08:55:38 +020038#include "qemu/rcu.h"
Juan Quintela2c9e6fe2017-04-21 14:31:22 +020039#include "block.h"
Juan Quintelabe07b0a2017-04-20 13:12:24 +020040#include "postcopy-ram.h"
Juan Quintela766bd172012-07-23 05:45:29 +020041#include "qemu/thread.h"
Kazuya Saitoc09e5bb2013-02-22 17:36:19 +010042#include "trace.h"
Juan Quintela51180422017-04-24 20:50:19 +020043#include "exec/target_page.h"
Daniel P. Berrange61b67d42016-04-27 11:05:01 +010044#include "io/channel-buffer.h"
zhanghailiang35a6ed42016-10-27 14:42:52 +080045#include "migration/colo.h"
Peter Xu4ffdb332017-06-27 12:10:18 +080046#include "hw/boards.h"
Peter Xu9d18af92017-06-27 12:10:19 +080047#include "monitor/monitor.h"
aliguori065e2812008-11-11 16:46:33 +000048
Jason J. Hernedc325622015-09-08 13:12:37 -040049#define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000050
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010051/* Amount of time to allocate to each "chunk" of bandwidth-throttled
52 * data. */
53#define BUFFER_DELAY 100
54#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
55
Ashijeet Acharya2ff30252016-09-15 21:50:28 +053056/* Time in milliseconds we are allowed to stop the source,
57 * for sending the last part */
58#define DEFAULT_MIGRATE_SET_DOWNTIME 300
59
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -030060/* Maximum migrate downtime set to 2000 seconds */
61#define MAX_MIGRATE_DOWNTIME_SECONDS 2000
62#define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
63
Liang Li8706d2d2015-03-23 16:32:17 +080064/* Default compression thread count */
65#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080066/* Default decompression thread count, usually decompression is at
67 * least 4 times as fast as compression.*/
68#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080069/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
70#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
Jason J. Herne1626fee2015-09-08 13:12:34 -040071/* Define default autoconverge cpu throttle migration parameters */
Jason J. Herned85a31d2016-04-21 14:07:18 -040072#define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
73#define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
Liang Li8706d2d2015-03-23 16:32:17 +080074
Orit Wasserman17ad9b32012-08-06 21:42:53 +030075/* Migration XBZRLE default cache size */
Juan Quintela73af8dd2017-10-05 21:30:10 +020076#define DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE (64 * 1024 * 1024)
Orit Wasserman17ad9b32012-08-06 21:42:53 +030077
zhanghailiang68b53592016-10-27 14:43:01 +080078/* The delay time (in ms) between two COLO checkpoints
79 * Note: Please change this default value to 10000 when we support hybrid mode.
80 */
81#define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY 200
Juan Quintela4075fb12016-01-15 08:56:17 +010082#define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2
Juan Quintela0fb86602017-04-27 10:48:25 +020083#define DEFAULT_MIGRATE_MULTIFD_PAGE_COUNT 16
zhanghailiang68b53592016-10-27 14:43:01 +080084
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010085static NotifierList migration_state_notifiers =
86 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
87
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000088static bool deferred_incoming;
89
Juan Quintelada6f1792017-04-24 17:37:14 +020090/* Messages sent on the return path from destination to source */
91enum mig_rp_message_type {
92 MIG_RP_MSG_INVALID = 0, /* Must be 0 */
93 MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */
94 MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */
95
96 MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */
97 MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */
98
99 MIG_RP_MSG_MAX
100};
101
Juan Quintela17549e82011-10-05 13:50:43 +0200102/* When we add fault tolerance, we could have several
103 migrations at once. For now we don't need to add
104 dynamic creation of migration */
105
Peter Xue5cb7e72017-06-27 12:10:13 +0800106static MigrationState *current_migration;
107
Peter Xu8b0b29d2017-07-18 11:39:06 +0800108static bool migration_object_check(MigrationState *ms, Error **errp);
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +0100109static int migration_maybe_pause(MigrationState *s,
110 int *current_active_state,
111 int new_state);
Peter Xu8b0b29d2017-07-18 11:39:06 +0800112
Peter Xue5cb7e72017-06-27 12:10:13 +0800113void migration_object_init(void)
114{
Peter Xu4ffdb332017-06-27 12:10:18 +0800115 MachineState *ms = MACHINE(qdev_get_machine());
Peter Xu8b0b29d2017-07-18 11:39:06 +0800116 Error *err = NULL;
Peter Xu4ffdb332017-06-27 12:10:18 +0800117
Peter Xue5cb7e72017-06-27 12:10:13 +0800118 /* This can only be called once. */
119 assert(!current_migration);
120 current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION));
Peter Xu4ffdb332017-06-27 12:10:18 +0800121
Peter Xu8b0b29d2017-07-18 11:39:06 +0800122 if (!migration_object_check(current_migration, &err)) {
123 error_report_err(err);
124 exit(1);
125 }
126
Peter Xu4ffdb332017-06-27 12:10:18 +0800127 /*
128 * We cannot really do this in migration_instance_init() since at
129 * that time global properties are not yet applied, then this
130 * value will be definitely replaced by something else.
131 */
132 if (ms->enforce_config_section) {
133 current_migration->send_configuration = true;
134 }
Peter Xue5cb7e72017-06-27 12:10:13 +0800135}
136
Vladimir Sementsov-Ogievskiy1f895602017-12-28 12:16:16 +0300137void migration_object_finalize(void)
138{
139 object_unref(OBJECT(current_migration));
140}
141
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100142/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +0200143MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +0200144{
Peter Xue5cb7e72017-06-27 12:10:13 +0800145 /* This can only be called after the object created. */
146 assert(current_migration);
147 return current_migration;
Juan Quintela17549e82011-10-05 13:50:43 +0200148}
149
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100150MigrationIncomingState *migration_incoming_get_current(void)
151{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100152 static bool once;
153 static MigrationIncomingState mis_current;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100154
Juan Quintelab4b076d2017-01-23 22:32:06 +0100155 if (!once) {
156 mis_current.state = MIGRATION_STATUS_NONE;
157 memset(&mis_current, 0, sizeof(MigrationIncomingState));
Dr. David Alan Gilbert00fa4fc2018-03-12 17:21:04 +0000158 mis_current.postcopy_remote_fds = g_array_new(FALSE, TRUE,
159 sizeof(struct PostCopyFD));
Juan Quintelab4b076d2017-01-23 22:32:06 +0100160 qemu_mutex_init(&mis_current.rp_mutex);
161 qemu_event_init(&mis_current.main_thread_load_event, false);
Peter Xub411b842018-05-02 18:47:20 +0800162 qemu_sem_init(&mis_current.postcopy_pause_sem_dst, 0);
Vladimir Sementsov-Ogievskiyb35ebdf2018-03-13 15:34:01 -0400163
164 init_dirty_bitmap_incoming_migration();
165
Juan Quintelab4b076d2017-01-23 22:32:06 +0100166 once = true;
167 }
168 return &mis_current;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100169}
170
171void migration_incoming_state_destroy(void)
172{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100173 struct MigrationIncomingState *mis = migration_incoming_get_current();
174
Peter Xu34826552017-05-19 14:43:29 +0800175 if (mis->to_src_file) {
Peter Xu660819b2017-05-19 14:43:30 +0800176 /* Tell source that we are done */
177 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
Peter Xu34826552017-05-19 14:43:29 +0800178 qemu_fclose(mis->to_src_file);
179 mis->to_src_file = NULL;
180 }
181
Peter Xu660819b2017-05-19 14:43:30 +0800182 if (mis->from_src_file) {
183 qemu_fclose(mis->from_src_file);
184 mis->from_src_file = NULL;
185 }
Dr. David Alan Gilbert00fa4fc2018-03-12 17:21:04 +0000186 if (mis->postcopy_remote_fds) {
187 g_array_free(mis->postcopy_remote_fds, TRUE);
188 mis->postcopy_remote_fds = NULL;
189 }
Peter Xu660819b2017-05-19 14:43:30 +0800190
Dr. David Alan Gilbert5089e182017-08-25 15:19:39 +0100191 qemu_event_reset(&mis->main_thread_load_event);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100192}
193
Juan Quintelab05dc722015-07-07 14:44:05 +0200194static void migrate_generate_event(int new_state)
195{
196 if (migrate_use_events()) {
197 qapi_event_send_migration(new_state, &error_abort);
Juan Quintelab05dc722015-07-07 14:44:05 +0200198 }
199}
200
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000201/*
202 * Called on -incoming with a defer: uri.
203 * The migration can be started later after any parameters have been
204 * changed.
205 */
206static void deferred_incoming_migration(Error **errp)
207{
208 if (deferred_incoming) {
209 error_setg(errp, "Incoming migration already deferred");
210 }
211 deferred_incoming = true;
212}
213
Juan Quintelada6f1792017-04-24 17:37:14 +0200214/*
215 * Send a message on the return channel back to the source
216 * of the migration.
217 */
Peter Xud6208e32018-02-08 18:31:12 +0800218static int migrate_send_rp_message(MigrationIncomingState *mis,
219 enum mig_rp_message_type message_type,
220 uint16_t len, void *data)
Juan Quintelada6f1792017-04-24 17:37:14 +0200221{
Peter Xud6208e32018-02-08 18:31:12 +0800222 int ret = 0;
223
Juan Quintelada6f1792017-04-24 17:37:14 +0200224 trace_migrate_send_rp_message((int)message_type, len);
225 qemu_mutex_lock(&mis->rp_mutex);
Peter Xud6208e32018-02-08 18:31:12 +0800226
227 /*
228 * It's possible that the file handle got lost due to network
229 * failures.
230 */
231 if (!mis->to_src_file) {
232 ret = -EIO;
233 goto error;
234 }
235
Juan Quintelada6f1792017-04-24 17:37:14 +0200236 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
237 qemu_put_be16(mis->to_src_file, len);
238 qemu_put_buffer(mis->to_src_file, data, len);
239 qemu_fflush(mis->to_src_file);
Peter Xud6208e32018-02-08 18:31:12 +0800240
241 /* It's possible that qemu file got error during sending */
242 ret = qemu_file_get_error(mis->to_src_file);
243
244error:
Juan Quintelada6f1792017-04-24 17:37:14 +0200245 qemu_mutex_unlock(&mis->rp_mutex);
Peter Xud6208e32018-02-08 18:31:12 +0800246 return ret;
Juan Quintelada6f1792017-04-24 17:37:14 +0200247}
248
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000249/* Request a range of pages from the source VM at the given
250 * start address.
251 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
252 * as the last request (a name must have been given previously)
253 * Start: Address offset within the RB
254 * Len: Length in bytes required - must be a multiple of pagesize
255 */
Peter Xud6208e32018-02-08 18:31:12 +0800256int migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
257 ram_addr_t start, size_t len)
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000258{
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100259 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000260 size_t msglen = 12; /* start + len */
Peter Xud6208e32018-02-08 18:31:12 +0800261 enum mig_rp_message_type msg_type;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000262
263 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
264 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
265
266 if (rbname) {
267 int rbname_len = strlen(rbname);
268 assert(rbname_len < 256);
269
270 bufc[msglen++] = rbname_len;
271 memcpy(bufc + msglen, rbname, rbname_len);
272 msglen += rbname_len;
Peter Xud6208e32018-02-08 18:31:12 +0800273 msg_type = MIG_RP_MSG_REQ_PAGES_ID;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000274 } else {
Peter Xud6208e32018-02-08 18:31:12 +0800275 msg_type = MIG_RP_MSG_REQ_PAGES;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000276 }
Peter Xud6208e32018-02-08 18:31:12 +0800277
278 return migrate_send_rp_message(mis, msg_type, msglen, bufc);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000279}
280
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200281void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000282{
aliguori34c9dd82008-10-13 03:14:31 +0000283 const char *p;
284
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200285 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000286 if (!strcmp(uri, "defer")) {
287 deferred_incoming_migration(errp);
288 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200289 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400290#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000291 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400292 rdma_start_incoming_migration(p, errp);
293#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000294 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200295 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000296 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200297 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000298 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200299 fd_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000300 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100301 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200302 }
aliguori5bb79102008-10-13 03:12:02 +0000303}
304
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300305static void process_incoming_migration_bh(void *opaque)
306{
307 Error *local_err = NULL;
308 MigrationIncomingState *mis = opaque;
309
Dr. David Alan Gilberta18a73d2018-04-10 15:28:42 +0100310 /* Make sure all file formats flush their mutable metadata.
311 * If we get an error here, just don't restart the VM yet. */
312 bdrv_invalidate_cache_all(&local_err);
313 if (local_err) {
314 error_report_err(local_err);
315 local_err = NULL;
316 autostart = false;
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200317 }
318
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300319 /*
320 * This must happen after all error conditions are dealt with and
321 * we're sure the VM is going to be running on this host.
322 */
323 qemu_announce_self();
324
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100325 if (multifd_load_cleanup(&local_err) != 0) {
326 error_report_err(local_err);
327 autostart = false;
328 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300329 /* If global state section was not received or we are in running
330 state, we need to obey autostart. Any other state is set with
331 runstate_set. */
332
Vladimir Sementsov-Ogievskiyb35ebdf2018-03-13 15:34:01 -0400333 dirty_bitmap_mig_before_vm_start();
334
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300335 if (!global_state_received() ||
336 global_state_get_runstate() == RUN_STATE_RUNNING) {
337 if (autostart) {
338 vm_start();
339 } else {
340 runstate_set(RUN_STATE_PAUSED);
341 }
342 } else {
343 runstate_set(global_state_get_runstate());
344 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300345 /*
346 * This must happen after any state changes since as soon as an external
347 * observer sees this event they might start to prod at the VM assuming
348 * it's ready to use.
349 */
350 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
351 MIGRATION_STATUS_COMPLETED);
352 qemu_bh_delete(mis->bh);
353 migration_incoming_state_destroy();
354}
355
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200356static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200357{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100358 MigrationIncomingState *mis = migration_incoming_get_current();
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000359 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200360 int ret;
361
Juan Quintela4f0fae72017-07-24 12:42:02 +0200362 assert(mis->from_src_file);
Dr. David Alan Gilbert67f11b52017-02-24 18:28:34 +0000363 mis->largest_page_size = qemu_ram_pagesize_largest();
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000364 postcopy_state_set(POSTCOPY_INCOMING_NONE);
zhanghailiang93d7af62015-12-16 11:47:34 +0000365 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
366 MIGRATION_STATUS_ACTIVE);
Juan Quintela4f0fae72017-07-24 12:42:02 +0200367 ret = qemu_loadvm_state(mis->from_src_file);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100368
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000369 ps = postcopy_state_get();
370 trace_process_incoming_migration_co_end(ret, ps);
371 if (ps != POSTCOPY_INCOMING_NONE) {
372 if (ps == POSTCOPY_INCOMING_ADVISE) {
373 /*
374 * Where a migration had postcopy enabled (and thus went to advise)
375 * but managed to complete within the precopy period, we can use
376 * the normal exit.
377 */
378 postcopy_ram_incoming_cleanup(mis);
379 } else if (ret >= 0) {
380 /*
381 * Postcopy was started, cleanup should happen at the end of the
382 * postcopy thread.
383 */
384 trace_process_incoming_migration_co_postcopy_end_main();
385 return;
386 }
387 /* Else if something went wrong then just fall out of the normal exit */
388 }
389
zhanghailiang25d0c162016-10-27 14:42:55 +0800390 /* we get COLO info, and know if we are in COLO mode */
391 if (!ret && migration_incoming_enable_colo()) {
392 mis->migration_incoming_co = qemu_coroutine_self();
393 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
394 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
395 mis->have_colo_incoming_thread = true;
396 qemu_coroutine_yield();
397
398 /* Wait checkpoint incoming thread exit before free resource */
399 qemu_thread_join(&mis->colo_incoming_thread);
400 }
401
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200402 if (ret < 0) {
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100403 Error *local_err = NULL;
404
zhanghailiang93d7af62015-12-16 11:47:34 +0000405 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
406 MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200407 error_report("load of migration failed: %s", strerror(-ret));
Dr. David Alan Gilbert3a0f2ce2017-07-17 12:09:32 +0100408 qemu_fclose(mis->from_src_file);
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100409 if (multifd_load_cleanup(&local_err) != 0) {
410 error_report_err(local_err);
411 }
Eric Blake4aead692013-04-16 15:50:41 -0600412 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200413 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300414 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
415 qemu_bh_schedule(mis->bh);
Juan Quintela511c0232010-06-09 14:10:55 +0200416}
417
Juan Quintelae595a012017-07-17 12:30:25 +0200418static void migration_incoming_setup(QEMUFile *f)
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200419{
Juan Quintela4f0fae72017-07-24 12:42:02 +0200420 MigrationIncomingState *mis = migration_incoming_get_current();
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200421
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100422 if (multifd_load_setup() != 0) {
423 /* We haven't been able to create multifd threads
424 nothing better to do */
425 exit(EXIT_FAILURE);
426 }
427
Juan Quintela4f0fae72017-07-24 12:42:02 +0200428 if (!mis->from_src_file) {
429 mis->from_src_file = f;
430 }
Daniel P. Berrange06ad5132016-04-27 11:04:56 +0100431 qemu_file_set_blocking(f, false);
Juan Quintelae595a012017-07-17 12:30:25 +0200432}
433
Juan Quintela36c2f8b2018-03-07 08:40:52 +0100434void migration_incoming_process(void)
Juan Quintelae595a012017-07-17 12:30:25 +0200435{
436 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL);
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200437 qemu_coroutine_enter(co);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200438}
439
Juan Quintelae595a012017-07-17 12:30:25 +0200440void migration_fd_process_incoming(QEMUFile *f)
441{
442 migration_incoming_setup(f);
443 migration_incoming_process();
444}
445
Juan Quintela4f0fae72017-07-24 12:42:02 +0200446void migration_ioc_process_incoming(QIOChannel *ioc)
447{
448 MigrationIncomingState *mis = migration_incoming_get_current();
449
450 if (!mis->from_src_file) {
451 QEMUFile *f = qemu_fopen_channel_input(ioc);
Juan Quintela36c2f8b2018-03-07 08:40:52 +0100452 migration_incoming_setup(f);
Juan Quintela71bb07d2018-02-19 19:01:03 +0100453 return;
Juan Quintela4f0fae72017-07-24 12:42:02 +0200454 }
Juan Quintela71bb07d2018-02-19 19:01:03 +0100455 multifd_recv_new_channel(ioc);
Juan Quintela4f0fae72017-07-24 12:42:02 +0200456}
457
Juan Quintela428d8902017-07-24 13:06:25 +0200458/**
459 * @migration_has_all_channels: We have received all channels that we need
460 *
461 * Returns true when we have got connections to all the channels that
462 * we need for migration.
463 */
464bool migration_has_all_channels(void)
465{
Juan Quintela62c1e0c2018-02-19 18:59:02 +0100466 bool all_channels;
467
468 all_channels = multifd_recv_all_channels_created();
469
470 return all_channels;
Juan Quintela428d8902017-07-24 13:06:25 +0200471}
472
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000473/*
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000474 * Send a 'SHUT' message on the return channel with the given value
475 * to indicate that we've finished with the RP. Non-0 value indicates
476 * error.
477 */
478void migrate_send_rp_shut(MigrationIncomingState *mis,
479 uint32_t value)
480{
481 uint32_t buf;
482
483 buf = cpu_to_be32(value);
484 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
485}
486
487/*
488 * Send a 'PONG' message on the return channel with the given value
489 * (normally in response to a 'PING')
490 */
491void migrate_send_rp_pong(MigrationIncomingState *mis,
492 uint32_t value)
493{
494 uint32_t buf;
495
496 buf = cpu_to_be32(value);
497 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
498}
499
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300500MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
501{
502 MigrationCapabilityStatusList *head = NULL;
503 MigrationCapabilityStatusList *caps;
504 MigrationState *s = migrate_get_current();
505 int i;
506
Michael Tokarev387eede2013-10-05 13:18:28 +0400507 caps = NULL; /* silence compiler warning */
Eric Blake7fb1cf12015-11-18 01:52:57 -0700508 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Dr. David Alan Gilberted1701c2017-05-15 15:05:29 +0100509#ifndef CONFIG_LIVE_BLOCK_MIGRATION
510 if (i == MIGRATION_CAPABILITY_BLOCK) {
511 continue;
512 }
513#endif
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300514 if (head == NULL) {
515 head = g_malloc0(sizeof(*caps));
516 caps = head;
517 } else {
518 caps->next = g_malloc0(sizeof(*caps));
519 caps = caps->next;
520 }
521 caps->value =
522 g_malloc(sizeof(*caps->value));
523 caps->value->capability = i;
524 caps->value->state = s->enabled_capabilities[i];
525 }
526
527 return head;
528}
529
Liang Li85de8322015-03-23 16:32:28 +0800530MigrationParameters *qmp_query_migrate_parameters(Error **errp)
531{
532 MigrationParameters *params;
533 MigrationState *s = migrate_get_current();
534
Markus Armbrustere87fae42017-07-18 12:57:38 +0200535 /* TODO use QAPI_CLONE() instead of duplicating it inline */
Liang Li85de8322015-03-23 16:32:28 +0800536 params = g_malloc0(sizeof(*params));
Eric Blakede63ab62016-09-08 22:14:15 -0500537 params->has_compress_level = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100538 params->compress_level = s->parameters.compress_level;
Eric Blakede63ab62016-09-08 22:14:15 -0500539 params->has_compress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100540 params->compress_threads = s->parameters.compress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500541 params->has_decompress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100542 params->decompress_threads = s->parameters.decompress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500543 params->has_cpu_throttle_initial = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100544 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
Eric Blakede63ab62016-09-08 22:14:15 -0500545 params->has_cpu_throttle_increment = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100546 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200547 params->has_tls_creds = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100548 params->tls_creds = g_strdup(s->parameters.tls_creds);
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200549 params->has_tls_hostname = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100550 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530551 params->has_max_bandwidth = true;
552 params->max_bandwidth = s->parameters.max_bandwidth;
553 params->has_downtime_limit = true;
554 params->downtime_limit = s->parameters.downtime_limit;
zhanghailiangfe39a4d2016-11-02 15:42:09 +0800555 params->has_x_checkpoint_delay = true;
zhanghailiang68b53592016-10-27 14:43:01 +0800556 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
Juan Quintela2833c592017-04-05 18:32:37 +0200557 params->has_block_incremental = true;
558 params->block_incremental = s->parameters.block_incremental;
Juan Quintela4075fb12016-01-15 08:56:17 +0100559 params->has_x_multifd_channels = true;
560 params->x_multifd_channels = s->parameters.x_multifd_channels;
Juan Quintela0fb86602017-04-27 10:48:25 +0200561 params->has_x_multifd_page_count = true;
562 params->x_multifd_page_count = s->parameters.x_multifd_page_count;
Juan Quintela73af8dd2017-10-05 21:30:10 +0200563 params->has_xbzrle_cache_size = true;
564 params->xbzrle_cache_size = s->parameters.xbzrle_cache_size;
Liang Li85de8322015-03-23 16:32:28 +0800565
566 return params;
567}
568
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000569/*
570 * Return true if we're already in the middle of a migration
571 * (i.e. any of the active or setup states)
572 */
573static bool migration_is_setup_or_active(int state)
574{
575 switch (state) {
576 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000577 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Peter Xua688d2c2018-05-02 18:47:18 +0800578 case MIGRATION_STATUS_POSTCOPY_PAUSED:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000579 case MIGRATION_STATUS_SETUP:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +0100580 case MIGRATION_STATUS_PRE_SWITCHOVER:
581 case MIGRATION_STATUS_DEVICE:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000582 return true;
583
584 default:
585 return false;
586
587 }
588}
589
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100590static void populate_ram_info(MigrationInfo *info, MigrationState *s)
591{
592 info->has_ram = true;
593 info->ram = g_malloc0(sizeof(*info->ram));
Juan Quintela93604472017-06-06 19:49:03 +0200594 info->ram->transferred = ram_counters.transferred;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100595 info->ram->total = ram_bytes_total();
Juan Quintela93604472017-06-06 19:49:03 +0200596 info->ram->duplicate = ram_counters.duplicate;
Juan Quintelabedf53c2017-03-13 20:35:54 +0100597 /* legacy value. It is not used anymore */
598 info->ram->skipped = 0;
Juan Quintela93604472017-06-06 19:49:03 +0200599 info->ram->normal = ram_counters.normal;
600 info->ram->normal_bytes = ram_counters.normal *
Juan Quintela20afaed2017-03-21 09:09:14 +0100601 qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100602 info->ram->mbps = s->mbps;
Juan Quintela93604472017-06-06 19:49:03 +0200603 info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
604 info->ram->postcopy_requests = ram_counters.postcopy_requests;
Chao Fan030ce1f2017-03-21 10:22:43 +0800605 info->ram->page_size = qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100606
Juan Quintela114f5ae2017-05-04 10:09:21 +0200607 if (migrate_use_xbzrle()) {
608 info->has_xbzrle_cache = true;
609 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
610 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
Juan Quintela93604472017-06-06 19:49:03 +0200611 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
612 info->xbzrle_cache->pages = xbzrle_counters.pages;
613 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
614 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
615 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
Juan Quintela114f5ae2017-05-04 10:09:21 +0200616 }
617
Juan Quintela338182c2017-05-03 13:16:38 +0200618 if (cpu_throttle_active()) {
619 info->has_cpu_throttle_percentage = true;
620 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
621 }
622
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100623 if (s->state != MIGRATION_STATUS_COMPLETED) {
624 info->ram->remaining = ram_bytes_remaining();
Juan Quintela93604472017-06-06 19:49:03 +0200625 info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100626 }
627}
628
Juan Quintela930ac042017-05-04 10:21:46 +0200629static void populate_disk_info(MigrationInfo *info)
630{
631 if (blk_mig_active()) {
632 info->has_disk = true;
633 info->disk = g_malloc0(sizeof(*info->disk));
634 info->disk->transferred = blk_mig_bytes_transferred();
635 info->disk->remaining = blk_mig_bytes_remaining();
636 info->disk->total = blk_mig_bytes_total();
637 }
638}
639
Alexey Perevalov65ace062018-03-22 21:17:27 +0300640static void fill_source_migration_info(MigrationInfo *info)
aliguori5bb79102008-10-13 03:12:02 +0000641{
Juan Quintela17549e82011-10-05 13:50:43 +0200642 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000643
Juan Quintela17549e82011-10-05 13:50:43 +0200644 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800645 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200646 /* no migration has happened ever */
Alexey Perevalov65ace062018-03-22 21:17:27 +0300647 /* do not overwrite destination migration status */
648 return;
Juan Quintela17549e82011-10-05 13:50:43 +0200649 break;
zhanghailiang31194732015-03-13 16:08:38 +0800650 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400651 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400652 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400653 break;
zhanghailiang31194732015-03-13 16:08:38 +0800654 case MIGRATION_STATUS_ACTIVE:
655 case MIGRATION_STATUS_CANCELLING:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000656 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +0100657 case MIGRATION_STATUS_PRE_SWITCHOVER:
658 case MIGRATION_STATUS_DEVICE:
Peter Xua688d2c2018-05-02 18:47:18 +0800659 case MIGRATION_STATUS_POSTCOPY_PAUSED:
Juan Quintelac8f9f4f2017-06-06 19:21:29 +0200660 /* TODO add some postcopy stats */
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000661 info->has_status = true;
662 info->has_total_time = true;
663 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Peter Xu4af246a2018-01-03 20:20:08 +0800664 - s->start_time;
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000665 info->has_expected_downtime = true;
666 info->expected_downtime = s->expected_downtime;
667 info->has_setup_time = true;
668 info->setup_time = s->setup_time;
669
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100670 populate_ram_info(info, s);
Juan Quintela930ac042017-05-04 10:21:46 +0200671 populate_disk_info(info);
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000672 break;
zhanghailiang0b827d52016-10-27 14:42:54 +0800673 case MIGRATION_STATUS_COLO:
674 info->has_status = true;
675 /* TODO: display COLO specific information (checkpoint info etc.) */
676 break;
zhanghailiang31194732015-03-13 16:08:38 +0800677 case MIGRATION_STATUS_COMPLETED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300678 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900679 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200680 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200681 info->has_downtime = true;
682 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400683 info->has_setup_time = true;
684 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200685
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100686 populate_ram_info(info, s);
Juan Quintela17549e82011-10-05 13:50:43 +0200687 break;
zhanghailiang31194732015-03-13 16:08:38 +0800688 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300689 info->has_status = true;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100690 if (s->error) {
691 info->has_error_desc = true;
692 info->error_desc = g_strdup(error_get_pretty(s->error));
693 }
Juan Quintela17549e82011-10-05 13:50:43 +0200694 break;
zhanghailiang31194732015-03-13 16:08:38 +0800695 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300696 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200697 break;
aliguori5bb79102008-10-13 03:12:02 +0000698 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800699 info->status = s->state;
aliguori5bb79102008-10-13 03:12:02 +0000700}
701
Peter Xu4a842142017-07-18 11:39:08 +0800702/**
703 * @migration_caps_check - check capability validity
704 *
705 * @cap_list: old capability list, array of bool
706 * @params: new capabilities to be applied soon
707 * @errp: set *errp if the check failed, with reason
708 *
709 * Returns true if check passed, otherwise false.
710 */
711static bool migrate_caps_check(bool *cap_list,
712 MigrationCapabilityStatusList *params,
713 Error **errp)
Orit Wasserman00458432012-08-06 21:42:48 +0300714{
Orit Wasserman00458432012-08-06 21:42:48 +0300715 MigrationCapabilityStatusList *cap;
Peter Xu4a842142017-07-18 11:39:08 +0800716 bool old_postcopy_cap;
Alexey Perevalovd7651f12017-09-19 19:47:56 +0300717 MigrationIncomingState *mis = migration_incoming_get_current();
Orit Wasserman00458432012-08-06 21:42:48 +0300718
Peter Xu4a842142017-07-18 11:39:08 +0800719 old_postcopy_cap = cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Orit Wasserman00458432012-08-06 21:42:48 +0300720
721 for (cap = params; cap; cap = cap->next) {
Peter Xu4a842142017-07-18 11:39:08 +0800722 cap_list[cap->value->capability] = cap->value->state;
Orit Wasserman00458432012-08-06 21:42:48 +0300723 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000724
Peter Xu4a842142017-07-18 11:39:08 +0800725#ifndef CONFIG_LIVE_BLOCK_MIGRATION
726 if (cap_list[MIGRATION_CAPABILITY_BLOCK]) {
727 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
728 "block migration");
729 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
730 return false;
731 }
732#endif
733
734 if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
735 if (cap_list[MIGRATION_CAPABILITY_COMPRESS]) {
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000736 /* The decompression threads asynchronously write into RAM
737 * rather than use the atomic copies needed to avoid
738 * userfaulting. It should be possible to fix the decompression
739 * threads for compatibility in future.
740 */
Peter Xu4a842142017-07-18 11:39:08 +0800741 error_setg(errp, "Postcopy is not currently compatible "
742 "with compression");
743 return false;
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000744 }
Peter Xu4a842142017-07-18 11:39:08 +0800745
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100746 /* This check is reasonably expensive, so only when it's being
747 * set the first time, also it's only the destination that needs
748 * special support.
749 */
750 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
Alexey Perevalovd7651f12017-09-19 19:47:56 +0300751 !postcopy_ram_supported_by_host(mis)) {
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100752 /* postcopy_ram_supported_by_host will have emitted a more
753 * detailed message
754 */
Peter Xu4a842142017-07-18 11:39:08 +0800755 error_setg(errp, "Postcopy is not supported");
756 return false;
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100757 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000758 }
Peter Xu4a842142017-07-18 11:39:08 +0800759
760 return true;
761}
762
Alexey Perevalov65ace062018-03-22 21:17:27 +0300763static void fill_destination_migration_info(MigrationInfo *info)
764{
765 MigrationIncomingState *mis = migration_incoming_get_current();
766
767 switch (mis->state) {
768 case MIGRATION_STATUS_NONE:
769 return;
770 break;
771 case MIGRATION_STATUS_SETUP:
772 case MIGRATION_STATUS_CANCELLING:
773 case MIGRATION_STATUS_CANCELLED:
774 case MIGRATION_STATUS_ACTIVE:
775 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
776 case MIGRATION_STATUS_FAILED:
777 case MIGRATION_STATUS_COLO:
778 info->has_status = true;
779 break;
780 case MIGRATION_STATUS_COMPLETED:
781 info->has_status = true;
782 fill_destination_postcopy_migration_info(info);
783 break;
784 }
785 info->status = mis->state;
786}
787
788MigrationInfo *qmp_query_migrate(Error **errp)
789{
790 MigrationInfo *info = g_malloc0(sizeof(*info));
791
792 fill_destination_migration_info(info);
793 fill_source_migration_info(info);
794
795 return info;
796}
797
Peter Xu4a842142017-07-18 11:39:08 +0800798void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
799 Error **errp)
800{
801 MigrationState *s = migrate_get_current();
802 MigrationCapabilityStatusList *cap;
Peter Xudd0ee302018-03-05 17:49:38 +0800803 bool cap_list[MIGRATION_CAPABILITY__MAX];
Peter Xu4a842142017-07-18 11:39:08 +0800804
805 if (migration_is_setup_or_active(s->state)) {
806 error_setg(errp, QERR_MIGRATION_ACTIVE);
807 return;
808 }
809
Peter Xudd0ee302018-03-05 17:49:38 +0800810 memcpy(cap_list, s->enabled_capabilities, sizeof(cap_list));
811 if (!migrate_caps_check(cap_list, params, errp)) {
Peter Xu4a842142017-07-18 11:39:08 +0800812 return;
813 }
814
815 for (cap = params; cap; cap = cap->next) {
816 s->enabled_capabilities[cap->value->capability] = cap->value->state;
817 }
Orit Wasserman00458432012-08-06 21:42:48 +0300818}
819
Peter Xu16d063b2017-07-18 11:39:04 +0800820/*
821 * Check whether the parameters are valid. Error will be put into errp
822 * (if provided). Return true if valid, otherwise false.
823 */
824static bool migrate_params_check(MigrationParameters *params, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +0800825{
Eric Blake7f375e02016-09-08 22:14:16 -0500826 if (params->has_compress_level &&
Juan Quintela741d4082017-12-01 13:08:38 +0100827 (params->compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100828 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
829 "is invalid, it should be in the range of 0 to 9");
Peter Xu16d063b2017-07-18 11:39:04 +0800830 return false;
Liang Li85de8322015-03-23 16:32:28 +0800831 }
Peter Xu16d063b2017-07-18 11:39:04 +0800832
Juan Quintela741d4082017-12-01 13:08:38 +0100833 if (params->has_compress_threads && (params->compress_threads < 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100834 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
835 "compress_threads",
836 "is invalid, it should be in the range of 1 to 255");
Peter Xu16d063b2017-07-18 11:39:04 +0800837 return false;
Liang Li85de8322015-03-23 16:32:28 +0800838 }
Peter Xu16d063b2017-07-18 11:39:04 +0800839
Juan Quintela741d4082017-12-01 13:08:38 +0100840 if (params->has_decompress_threads && (params->decompress_threads < 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100841 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
842 "decompress_threads",
843 "is invalid, it should be in the range of 1 to 255");
Peter Xu16d063b2017-07-18 11:39:04 +0800844 return false;
Liang Li85de8322015-03-23 16:32:28 +0800845 }
Peter Xu16d063b2017-07-18 11:39:04 +0800846
Eric Blake7f375e02016-09-08 22:14:16 -0500847 if (params->has_cpu_throttle_initial &&
848 (params->cpu_throttle_initial < 1 ||
849 params->cpu_throttle_initial > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400850 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400851 "cpu_throttle_initial",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400852 "an integer in the range of 1 to 99");
Peter Xu16d063b2017-07-18 11:39:04 +0800853 return false;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400854 }
Peter Xu16d063b2017-07-18 11:39:04 +0800855
Eric Blake7f375e02016-09-08 22:14:16 -0500856 if (params->has_cpu_throttle_increment &&
857 (params->cpu_throttle_increment < 1 ||
858 params->cpu_throttle_increment > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400859 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400860 "cpu_throttle_increment",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400861 "an integer in the range of 1 to 99");
Peter Xu16d063b2017-07-18 11:39:04 +0800862 return false;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400863 }
Peter Xu16d063b2017-07-18 11:39:04 +0800864
Juan Quintela741d4082017-12-01 13:08:38 +0100865 if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530866 error_setg(errp, "Parameter 'max_bandwidth' expects an integer in the"
867 " range of 0 to %zu bytes/second", SIZE_MAX);
Peter Xu16d063b2017-07-18 11:39:04 +0800868 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530869 }
Peter Xu16d063b2017-07-18 11:39:04 +0800870
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530871 if (params->has_downtime_limit &&
Juan Quintela741d4082017-12-01 13:08:38 +0100872 (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -0300873 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
874 "the range of 0 to %d milliseconds",
875 MAX_MIGRATE_DOWNTIME);
Peter Xu16d063b2017-07-18 11:39:04 +0800876 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530877 }
Peter Xu16d063b2017-07-18 11:39:04 +0800878
Juan Quintela741d4082017-12-01 13:08:38 +0100879 /* x_checkpoint_delay is now always positive */
880
881 if (params->has_x_multifd_channels && (params->x_multifd_channels < 1)) {
Juan Quintela4075fb12016-01-15 08:56:17 +0100882 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
883 "multifd_channels",
884 "is invalid, it should be in the range of 1 to 255");
885 return false;
886 }
Juan Quintela0fb86602017-04-27 10:48:25 +0200887 if (params->has_x_multifd_page_count &&
Juan Quintela741d4082017-12-01 13:08:38 +0100888 (params->x_multifd_page_count < 1 ||
889 params->x_multifd_page_count > 10000)) {
Juan Quintela0fb86602017-04-27 10:48:25 +0200890 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
891 "multifd_page_count",
892 "is invalid, it should be in the range of 1 to 10000");
893 return false;
894 }
Peter Xu16d063b2017-07-18 11:39:04 +0800895
Juan Quintela73af8dd2017-10-05 21:30:10 +0200896 if (params->has_xbzrle_cache_size &&
897 (params->xbzrle_cache_size < qemu_target_page_size() ||
898 !is_power_of_2(params->xbzrle_cache_size))) {
899 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
900 "xbzrle_cache_size",
901 "is invalid, it should be bigger than target page size"
902 " and a power of two");
903 return false;
904 }
905
Peter Xu16d063b2017-07-18 11:39:04 +0800906 return true;
907}
908
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200909static void migrate_params_test_apply(MigrateSetParameters *params,
910 MigrationParameters *dest)
911{
912 *dest = migrate_get_current()->parameters;
913
914 /* TODO use QAPI_CLONE() instead of duplicating it inline */
915
916 if (params->has_compress_level) {
917 dest->compress_level = params->compress_level;
918 }
919
920 if (params->has_compress_threads) {
921 dest->compress_threads = params->compress_threads;
922 }
923
924 if (params->has_decompress_threads) {
925 dest->decompress_threads = params->decompress_threads;
926 }
927
928 if (params->has_cpu_throttle_initial) {
929 dest->cpu_throttle_initial = params->cpu_throttle_initial;
930 }
931
932 if (params->has_cpu_throttle_increment) {
933 dest->cpu_throttle_increment = params->cpu_throttle_increment;
934 }
935
936 if (params->has_tls_creds) {
Markus Armbruster01fa5592017-07-18 14:42:04 +0200937 assert(params->tls_creds->type == QTYPE_QSTRING);
938 dest->tls_creds = g_strdup(params->tls_creds->u.s);
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200939 }
940
941 if (params->has_tls_hostname) {
Markus Armbruster01fa5592017-07-18 14:42:04 +0200942 assert(params->tls_hostname->type == QTYPE_QSTRING);
943 dest->tls_hostname = g_strdup(params->tls_hostname->u.s);
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200944 }
945
946 if (params->has_max_bandwidth) {
947 dest->max_bandwidth = params->max_bandwidth;
948 }
949
950 if (params->has_downtime_limit) {
951 dest->downtime_limit = params->downtime_limit;
952 }
953
954 if (params->has_x_checkpoint_delay) {
955 dest->x_checkpoint_delay = params->x_checkpoint_delay;
956 }
957
958 if (params->has_block_incremental) {
959 dest->block_incremental = params->block_incremental;
960 }
Juan Quintela5e7577a2017-10-09 18:07:56 +0200961 if (params->has_x_multifd_channels) {
962 dest->x_multifd_channels = params->x_multifd_channels;
963 }
964 if (params->has_x_multifd_page_count) {
965 dest->x_multifd_page_count = params->x_multifd_page_count;
966 }
Juan Quintela73af8dd2017-10-05 21:30:10 +0200967 if (params->has_xbzrle_cache_size) {
968 dest->xbzrle_cache_size = params->xbzrle_cache_size;
969 }
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200970}
971
Juan Quintela73af8dd2017-10-05 21:30:10 +0200972static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
Peter Xu16d063b2017-07-18 11:39:04 +0800973{
974 MigrationState *s = migrate_get_current();
975
Markus Armbrustere87fae42017-07-18 12:57:38 +0200976 /* TODO use QAPI_CLONE() instead of duplicating it inline */
977
Eric Blake7f375e02016-09-08 22:14:16 -0500978 if (params->has_compress_level) {
979 s->parameters.compress_level = params->compress_level;
Liang Li85de8322015-03-23 16:32:28 +0800980 }
Peter Xu476c72a2017-07-18 11:39:05 +0800981
Eric Blake7f375e02016-09-08 22:14:16 -0500982 if (params->has_compress_threads) {
983 s->parameters.compress_threads = params->compress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800984 }
Peter Xu476c72a2017-07-18 11:39:05 +0800985
Eric Blake7f375e02016-09-08 22:14:16 -0500986 if (params->has_decompress_threads) {
987 s->parameters.decompress_threads = params->decompress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800988 }
Peter Xu476c72a2017-07-18 11:39:05 +0800989
Eric Blake7f375e02016-09-08 22:14:16 -0500990 if (params->has_cpu_throttle_initial) {
991 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400992 }
Peter Xu476c72a2017-07-18 11:39:05 +0800993
Eric Blake7f375e02016-09-08 22:14:16 -0500994 if (params->has_cpu_throttle_increment) {
995 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400996 }
Peter Xu476c72a2017-07-18 11:39:05 +0800997
Eric Blake7f375e02016-09-08 22:14:16 -0500998 if (params->has_tls_creds) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100999 g_free(s->parameters.tls_creds);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001000 assert(params->tls_creds->type == QTYPE_QSTRING);
1001 s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001002 }
Peter Xu476c72a2017-07-18 11:39:05 +08001003
Eric Blake7f375e02016-09-08 22:14:16 -05001004 if (params->has_tls_hostname) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001005 g_free(s->parameters.tls_hostname);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001006 assert(params->tls_hostname->type == QTYPE_QSTRING);
1007 s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001008 }
Peter Xu476c72a2017-07-18 11:39:05 +08001009
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301010 if (params->has_max_bandwidth) {
1011 s->parameters.max_bandwidth = params->max_bandwidth;
1012 if (s->to_dst_file) {
1013 qemu_file_set_rate_limit(s->to_dst_file,
1014 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
1015 }
1016 }
Peter Xu476c72a2017-07-18 11:39:05 +08001017
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301018 if (params->has_downtime_limit) {
1019 s->parameters.downtime_limit = params->downtime_limit;
1020 }
zhanghailiang68b53592016-10-27 14:43:01 +08001021
1022 if (params->has_x_checkpoint_delay) {
1023 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
zhanghailiang479125d2017-01-17 20:57:42 +08001024 if (migration_in_colo_state()) {
1025 colo_checkpoint_notify(s);
1026 }
zhanghailiang68b53592016-10-27 14:43:01 +08001027 }
Peter Xu476c72a2017-07-18 11:39:05 +08001028
Juan Quintela2833c592017-04-05 18:32:37 +02001029 if (params->has_block_incremental) {
1030 s->parameters.block_incremental = params->block_incremental;
1031 }
Juan Quintela4075fb12016-01-15 08:56:17 +01001032 if (params->has_x_multifd_channels) {
1033 s->parameters.x_multifd_channels = params->x_multifd_channels;
1034 }
Juan Quintela0fb86602017-04-27 10:48:25 +02001035 if (params->has_x_multifd_page_count) {
1036 s->parameters.x_multifd_page_count = params->x_multifd_page_count;
1037 }
Juan Quintela73af8dd2017-10-05 21:30:10 +02001038 if (params->has_xbzrle_cache_size) {
1039 s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
1040 xbzrle_cache_resize(params->xbzrle_cache_size, errp);
1041 }
Liang Li85de8322015-03-23 16:32:28 +08001042}
1043
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001044void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
Peter Xu476c72a2017-07-18 11:39:05 +08001045{
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001046 MigrationParameters tmp;
1047
Markus Armbruster01fa5592017-07-18 14:42:04 +02001048 /* TODO Rewrite "" to null instead */
1049 if (params->has_tls_creds
1050 && params->tls_creds->type == QTYPE_QNULL) {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001051 qobject_unref(params->tls_creds->u.n);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001052 params->tls_creds->type = QTYPE_QSTRING;
1053 params->tls_creds->u.s = strdup("");
1054 }
1055 /* TODO Rewrite "" to null instead */
1056 if (params->has_tls_hostname
1057 && params->tls_hostname->type == QTYPE_QNULL) {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001058 qobject_unref(params->tls_hostname->u.n);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001059 params->tls_hostname->type = QTYPE_QSTRING;
1060 params->tls_hostname->u.s = strdup("");
1061 }
1062
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001063 migrate_params_test_apply(params, &tmp);
1064
1065 if (!migrate_params_check(&tmp, errp)) {
Peter Xu476c72a2017-07-18 11:39:05 +08001066 /* Invalid parameter */
1067 return;
1068 }
1069
Juan Quintela73af8dd2017-10-05 21:30:10 +02001070 migrate_params_apply(params, errp);
Peter Xu476c72a2017-07-18 11:39:05 +08001071}
1072
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001073
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001074void qmp_migrate_start_postcopy(Error **errp)
1075{
1076 MigrationState *s = migrate_get_current();
1077
Vladimir Sementsov-Ogievskiy16b0fd32018-03-13 15:34:01 -04001078 if (!migrate_postcopy()) {
Dr. David Alan Gilberta54d3402015-11-12 11:34:44 +00001079 error_setg(errp, "Enable postcopy with migrate_set_capability before"
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001080 " the start of migration");
1081 return;
1082 }
1083
1084 if (s->state == MIGRATION_STATUS_NONE) {
1085 error_setg(errp, "Postcopy must be started after migration has been"
1086 " started");
1087 return;
1088 }
1089 /*
1090 * we don't error if migration has finished since that would be racy
1091 * with issuing this command.
1092 */
1093 atomic_set(&s->start_postcopy, true);
1094}
1095
aliguori065e2812008-11-11 16:46:33 +00001096/* shared migration helpers */
1097
zhanghailiang48781e52015-12-16 11:47:33 +00001098void migrate_set_state(int *state, int old_state, int new_state)
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +00001099{
Peter Xua31fede2017-08-30 16:32:01 +08001100 assert(new_state < MIGRATION_STATUS__MAX);
zhanghailiang48781e52015-12-16 11:47:33 +00001101 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
Peter Xua31fede2017-08-30 16:32:01 +08001102 trace_migrate_set_state(MigrationStatus_str(new_state));
Juan Quintelab05dc722015-07-07 14:44:05 +02001103 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +00001104 }
1105}
1106
Peter Xu4e4a3d32017-07-18 11:39:09 +08001107static MigrationCapabilityStatusList *migrate_cap_add(
1108 MigrationCapabilityStatusList *list,
1109 MigrationCapability index,
1110 bool state)
Juan Quintela2833c592017-04-05 18:32:37 +02001111{
1112 MigrationCapabilityStatusList *cap;
1113
1114 cap = g_new0(MigrationCapabilityStatusList, 1);
1115 cap->value = g_new0(MigrationCapabilityStatus, 1);
Peter Xu4e4a3d32017-07-18 11:39:09 +08001116 cap->value->capability = index;
1117 cap->value->state = state;
1118 cap->next = list;
1119
1120 return cap;
1121}
1122
1123void migrate_set_block_enabled(bool value, Error **errp)
1124{
1125 MigrationCapabilityStatusList *cap;
1126
1127 cap = migrate_cap_add(NULL, MIGRATION_CAPABILITY_BLOCK, value);
Juan Quintela2833c592017-04-05 18:32:37 +02001128 qmp_migrate_set_capabilities(cap, errp);
1129 qapi_free_MigrationCapabilityStatusList(cap);
1130}
1131
1132static void migrate_set_block_incremental(MigrationState *s, bool value)
1133{
1134 s->parameters.block_incremental = value;
1135}
1136
1137static void block_cleanup_parameters(MigrationState *s)
1138{
1139 if (s->must_remove_block_options) {
1140 /* setting to false can never fail */
1141 migrate_set_block_enabled(false, &error_abort);
1142 migrate_set_block_incremental(s, false);
1143 s->must_remove_block_options = false;
1144 }
1145}
1146
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001147static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +00001148{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001149 MigrationState *s = opaque;
1150
1151 qemu_bh_delete(s->cleanup_bh);
1152 s->cleanup_bh = NULL;
1153
Peter Xu0ceccd82018-01-03 20:20:06 +08001154 qemu_savevm_state_cleanup();
1155
zhanghailiang89a02a92016-01-15 11:37:42 +08001156 if (s->to_dst_file) {
Juan Quintelaf986c3d2016-01-14 16:52:55 +01001157 Error *local_err = NULL;
1158
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001159 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001160 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001161 if (s->migration_thread_running) {
1162 qemu_thread_join(&s->thread);
1163 s->migration_thread_running = false;
1164 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001165 qemu_mutex_lock_iothread();
1166
Juan Quintelaf986c3d2016-01-14 16:52:55 +01001167 if (multifd_save_cleanup(&local_err) != 0) {
1168 error_report_err(local_err);
1169 }
zhanghailiang89a02a92016-01-15 11:37:42 +08001170 qemu_fclose(s->to_dst_file);
1171 s->to_dst_file = NULL;
aliguori065e2812008-11-11 16:46:33 +00001172 }
1173
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001174 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
1175 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001176
Liang Li94f5a432015-11-02 15:37:00 +08001177 if (s->state == MIGRATION_STATUS_CANCELLING) {
zhanghailiang48781e52015-12-16 11:47:33 +00001178 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
Liang Li94f5a432015-11-02 15:37:00 +08001179 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001180 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001181
Juan Quintela87db1a72017-09-05 12:50:22 +02001182 if (s->error) {
1183 /* It is used on info migrate. We can't free it */
1184 error_report_err(error_copy(s->error));
1185 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001186 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +02001187 block_cleanup_parameters(s);
aliguori065e2812008-11-11 16:46:33 +00001188}
1189
Juan Quintela87db1a72017-09-05 12:50:22 +02001190void migrate_set_error(MigrationState *s, const Error *error)
1191{
1192 qemu_mutex_lock(&s->error_mutex);
1193 if (!s->error) {
1194 s->error = error_copy(error);
1195 }
1196 qemu_mutex_unlock(&s->error_mutex);
1197}
1198
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001199void migrate_fd_error(MigrationState *s, const Error *error)
aliguori065e2812008-11-11 16:46:33 +00001200{
Peter Maydell25174052016-10-21 18:41:45 +01001201 trace_migrate_fd_error(error_get_pretty(error));
zhanghailiang89a02a92016-01-15 11:37:42 +08001202 assert(s->to_dst_file == NULL);
zhanghailiang48781e52015-12-16 11:47:33 +00001203 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1204 MIGRATION_STATUS_FAILED);
Juan Quintela87db1a72017-09-05 12:50:22 +02001205 migrate_set_error(s, error);
Juan Quintela458cf282011-02-22 23:32:54 +01001206}
1207
Juan Quintela0edda1c2010-05-11 16:28:39 +02001208static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +00001209{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001210 int old_state ;
zhanghailiang89a02a92016-01-15 11:37:42 +08001211 QEMUFile *f = migrate_get_current()->to_dst_file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001212 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +00001213
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001214 if (s->rp_state.from_dst_file) {
1215 /* shutdown the rp socket, so causing the rp thread to shutdown */
1216 qemu_file_shutdown(s->rp_state.from_dst_file);
1217 }
1218
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001219 do {
1220 old_state = s->state;
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +00001221 if (!migration_is_setup_or_active(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001222 break;
1223 }
Dr. David Alan Gilberta7b36b42017-10-20 10:05:55 +01001224 /* If the migration is paused, kick it out of the pause */
1225 if (old_state == MIGRATION_STATUS_PRE_SWITCHOVER) {
1226 qemu_sem_post(&s->pause_sem);
1227 }
zhanghailiang48781e52015-12-16 11:47:33 +00001228 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
zhanghailiang31194732015-03-13 16:08:38 +08001229 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001230
1231 /*
1232 * If we're unlucky the migration code might be stuck somewhere in a
1233 * send/write while the network has failed and is waiting to timeout;
1234 * if we've got shutdown(2) available then we can force it to quit.
1235 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1236 * called in a bh, so there is no race against this cancel.
1237 */
zhanghailiang31194732015-03-13 16:08:38 +08001238 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001239 qemu_file_shutdown(f);
1240 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08001241 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
1242 Error *local_err = NULL;
1243
1244 bdrv_invalidate_cache_all(&local_err);
1245 if (local_err) {
1246 error_report_err(local_err);
1247 } else {
1248 s->block_inactive = false;
1249 }
1250 }
aliguori065e2812008-11-11 16:46:33 +00001251}
1252
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001253void add_migration_state_change_notifier(Notifier *notify)
1254{
1255 notifier_list_add(&migration_state_notifiers, notify);
1256}
1257
1258void remove_migration_state_change_notifier(Notifier *notify)
1259{
Paolo Bonzini31552522012-01-13 17:34:01 +01001260 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001261}
1262
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +02001263bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001264{
zhanghailiang31194732015-03-13 16:08:38 +08001265 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001266}
1267
Juan Quintela70736932011-02-23 00:43:59 +01001268bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001269{
zhanghailiang31194732015-03-13 16:08:38 +08001270 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001271}
Juan Quintela0edda1c2010-05-11 16:28:39 +02001272
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001273bool migration_has_failed(MigrationState *s)
1274{
zhanghailiang31194732015-03-13 16:08:38 +08001275 return (s->state == MIGRATION_STATUS_CANCELLED ||
1276 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001277}
1278
Juan Quintela57273092017-03-20 22:25:28 +01001279bool migration_in_postcopy(void)
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001280{
Juan Quintela57273092017-03-20 22:25:28 +01001281 MigrationState *s = migrate_get_current();
1282
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001283 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1284}
1285
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001286bool migration_in_postcopy_after_devices(MigrationState *s)
1287{
Juan Quintela57273092017-03-20 22:25:28 +01001288 return migration_in_postcopy() && s->postcopy_after_devices;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001289}
1290
Juan Quintelafab35002017-03-22 17:36:57 +01001291bool migration_is_idle(void)
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301292{
Juan Quintelafab35002017-03-22 17:36:57 +01001293 MigrationState *s = migrate_get_current();
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301294
1295 switch (s->state) {
1296 case MIGRATION_STATUS_NONE:
1297 case MIGRATION_STATUS_CANCELLED:
1298 case MIGRATION_STATUS_COMPLETED:
1299 case MIGRATION_STATUS_FAILED:
1300 return true;
1301 case MIGRATION_STATUS_SETUP:
1302 case MIGRATION_STATUS_CANCELLING:
1303 case MIGRATION_STATUS_ACTIVE:
1304 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1305 case MIGRATION_STATUS_COLO:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +01001306 case MIGRATION_STATUS_PRE_SWITCHOVER:
1307 case MIGRATION_STATUS_DEVICE:
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301308 return false;
1309 case MIGRATION_STATUS__MAX:
1310 g_assert_not_reached();
1311 }
1312
1313 return false;
1314}
1315
Peter Xu3e0c8052018-02-08 18:31:15 +08001316void migrate_init(MigrationState *s)
Juan Quintela0edda1c2010-05-11 16:28:39 +02001317{
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001318 /*
1319 * Reinitialise all migration state, except
1320 * parameters/capabilities that the user set, and
1321 * locks.
1322 */
1323 s->bytes_xfer = 0;
1324 s->xfer_limit = 0;
1325 s->cleanup_bh = 0;
zhanghailiang89a02a92016-01-15 11:37:42 +08001326 s->to_dst_file = NULL;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001327 s->state = MIGRATION_STATUS_NONE;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001328 s->rp_state.from_dst_file = NULL;
1329 s->rp_state.error = false;
1330 s->mbps = 0.0;
1331 s->downtime = 0;
1332 s->expected_downtime = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001333 s->setup_time = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001334 s->start_postcopy = false;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001335 s->postcopy_after_devices = false;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001336 s->migration_thread_running = false;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001337 error_free(s->error);
1338 s->error = NULL;
Juan Quintela1299c632011-11-09 21:29:01 +01001339
zhanghailiang48781e52015-12-16 11:47:33 +00001340 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +02001341
Peter Xu4af246a2018-01-03 20:20:08 +08001342 s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1343 s->total_time = 0;
Peter Xu7287cbd2018-01-03 20:20:09 +08001344 s->vm_was_running = false;
Peter Xub15df1a2018-01-03 20:20:13 +08001345 s->iteration_initial_bytes = 0;
1346 s->threshold_size = 0;
Juan Quintela0edda1c2010-05-11 16:28:39 +02001347}
Juan Quintelacab30142011-02-22 23:54:21 +01001348
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001349static GSList *migration_blockers;
1350
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301351int migrate_add_blocker(Error *reason, Error **errp)
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001352{
Peter Xu3df663e2017-06-27 12:10:15 +08001353 if (migrate_get_current()->only_migratable) {
Ashijeet Acharyab67b8c32017-01-16 17:01:54 +05301354 error_propagate(errp, error_copy(reason));
1355 error_prepend(errp, "disallowing migration blocker "
1356 "(--only_migratable) for: ");
1357 return -EACCES;
1358 }
1359
Juan Quintelafab35002017-03-22 17:36:57 +01001360 if (migration_is_idle()) {
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301361 migration_blockers = g_slist_prepend(migration_blockers, reason);
1362 return 0;
1363 }
1364
1365 error_propagate(errp, error_copy(reason));
1366 error_prepend(errp, "disallowing migration blocker (migration in "
1367 "progress) for: ");
1368 return -EBUSY;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001369}
1370
1371void migrate_del_blocker(Error *reason)
1372{
1373 migration_blockers = g_slist_remove(migration_blockers, reason);
1374}
1375
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001376void qmp_migrate_incoming(const char *uri, Error **errp)
1377{
1378 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001379 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001380
1381 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001382 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001383 return;
1384 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001385 if (!once) {
1386 error_setg(errp, "The incoming migration has already been started");
1387 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001388
1389 qemu_start_incoming_migration(uri, &local_err);
1390
1391 if (local_err) {
1392 error_propagate(errp, local_err);
1393 return;
1394 }
1395
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001396 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001397}
1398
Greg Kurz24f39022016-05-04 21:44:19 +02001399bool migration_is_blocked(Error **errp)
1400{
1401 if (qemu_savevm_state_blocked(errp)) {
1402 return true;
1403 }
1404
1405 if (migration_blockers) {
Eduardo Habkost250561e2017-06-08 10:39:05 -03001406 error_propagate(errp, error_copy(migration_blockers->data));
Greg Kurz24f39022016-05-04 21:44:19 +02001407 return true;
1408 }
1409
1410 return false;
1411}
1412
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001413void qmp_migrate(const char *uri, bool has_blk, bool blk,
1414 bool has_inc, bool inc, bool has_detach, bool detach,
1415 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001416{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +02001417 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +02001418 MigrationState *s = migrate_get_current();
Juan Quintelacab30142011-02-22 23:54:21 +01001419 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +01001420
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +00001421 if (migration_is_setup_or_active(s->state) ||
zhanghailiang0b827d52016-10-27 14:42:54 +08001422 s->state == MIGRATION_STATUS_CANCELLING ||
1423 s->state == MIGRATION_STATUS_COLO) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001424 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001425 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001426 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +01001427 if (runstate_check(RUN_STATE_INMIGRATE)) {
1428 error_setg(errp, "Guest is waiting for an incoming migration");
1429 return;
1430 }
1431
Greg Kurz24f39022016-05-04 21:44:19 +02001432 if (migration_is_blocked(errp)) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001433 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001434 }
1435
Juan Quintela2833c592017-04-05 18:32:37 +02001436 if ((has_blk && blk) || (has_inc && inc)) {
1437 if (migrate_use_block() || migrate_use_block_incremental()) {
1438 error_setg(errp, "Command options are incompatible with "
1439 "current migration capabilities");
1440 return;
1441 }
1442 migrate_set_block_enabled(true, &local_err);
1443 if (local_err) {
1444 error_propagate(errp, local_err);
1445 return;
1446 }
1447 s->must_remove_block_options = true;
1448 }
1449
1450 if (has_inc && inc) {
1451 migrate_set_block_incremental(s, true);
1452 }
1453
Peter Xu3e0c8052018-02-08 18:31:15 +08001454 migrate_init(s);
Juan Quintelacab30142011-02-22 23:54:21 +01001455
1456 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001457 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04001458#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08001459 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04001460 rdma_start_outgoing_migration(s, p, &local_err);
1461#endif
Juan Quintelacab30142011-02-22 23:54:21 +01001462 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001463 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001464 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001465 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001466 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001467 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001468 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001469 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1470 "a valid migration protocol");
zhanghailiang48781e52015-12-16 11:47:33 +00001471 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1472 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09576e72018-03-16 20:21:14 +00001473 block_cleanup_parameters(s);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001474 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001475 }
1476
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001477 if (local_err) {
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001478 migrate_fd_error(s, local_err);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001479 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001480 return;
Juan Quintela1299c632011-11-09 21:29:01 +01001481 }
Juan Quintelacab30142011-02-22 23:54:21 +01001482}
1483
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02001484void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001485{
Juan Quintela17549e82011-10-05 13:50:43 +02001486 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01001487}
1488
Dr. David Alan Gilbert89cfc022017-10-20 10:05:53 +01001489void qmp_migrate_continue(MigrationStatus state, Error **errp)
1490{
1491 MigrationState *s = migrate_get_current();
1492 if (s->state != state) {
1493 error_setg(errp, "Migration not in expected state: %s",
1494 MigrationStatus_str(s->state));
1495 return;
1496 }
1497 qemu_sem_post(&s->pause_sem);
1498}
1499
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001500void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1501{
Juan Quintela73af8dd2017-10-05 21:30:10 +02001502 MigrateSetParameters p = {
1503 .has_xbzrle_cache_size = true,
1504 .xbzrle_cache_size = value,
1505 };
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001506
Juan Quintela73af8dd2017-10-05 21:30:10 +02001507 qmp_migrate_set_parameters(&p, errp);
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001508}
1509
1510int64_t qmp_query_migrate_cache_size(Error **errp)
1511{
1512 return migrate_xbzrle_cache_size();
1513}
1514
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001515void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001516{
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001517 MigrateSetParameters p = {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301518 .has_max_bandwidth = true,
1519 .max_bandwidth = value,
1520 };
Juan Quintelacab30142011-02-22 23:54:21 +01001521
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301522 qmp_migrate_set_parameters(&p, errp);
Juan Quintelacab30142011-02-22 23:54:21 +01001523}
1524
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001525void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001526{
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03001527 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
1528 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
1529 "the range of 0 to %d seconds",
1530 MAX_MIGRATE_DOWNTIME_SECONDS);
1531 return;
1532 }
1533
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301534 value *= 1000; /* Convert to milliseconds */
1535 value = MAX(0, MIN(INT64_MAX, value));
1536
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001537 MigrateSetParameters p = {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301538 .has_downtime_limit = true,
1539 .downtime_limit = value,
1540 };
1541
1542 qmp_migrate_set_parameters(&p, errp);
aliguori5bb79102008-10-13 03:12:02 +00001543}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001544
Pavel Butsykin53f09a12017-02-03 18:23:20 +03001545bool migrate_release_ram(void)
1546{
1547 MigrationState *s;
1548
1549 s = migrate_get_current();
1550
1551 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
1552}
1553
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001554bool migrate_postcopy_ram(void)
1555{
1556 MigrationState *s;
1557
1558 s = migrate_get_current();
1559
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +00001560 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001561}
1562
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03001563bool migrate_postcopy(void)
1564{
Vladimir Sementsov-Ogievskiydd6bb912018-03-13 15:34:00 -04001565 return migrate_postcopy_ram() || migrate_dirty_bitmaps();
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03001566}
1567
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06001568bool migrate_auto_converge(void)
1569{
1570 MigrationState *s;
1571
1572 s = migrate_get_current();
1573
1574 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1575}
1576
Peter Lieven323004a2013-07-18 09:48:50 +02001577bool migrate_zero_blocks(void)
1578{
1579 MigrationState *s;
1580
1581 s = migrate_get_current();
1582
1583 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1584}
1585
Alexey Perevalovf22f9282018-03-22 21:17:22 +03001586bool migrate_postcopy_blocktime(void)
1587{
1588 MigrationState *s;
1589
1590 s = migrate_get_current();
1591
1592 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME];
1593}
1594
Liang Li8706d2d2015-03-23 16:32:17 +08001595bool migrate_use_compression(void)
1596{
Liang Lidde4e692015-03-23 16:32:26 +08001597 MigrationState *s;
1598
1599 s = migrate_get_current();
1600
1601 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08001602}
1603
1604int migrate_compress_level(void)
1605{
1606 MigrationState *s;
1607
1608 s = migrate_get_current();
1609
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001610 return s->parameters.compress_level;
Liang Li8706d2d2015-03-23 16:32:17 +08001611}
1612
1613int migrate_compress_threads(void)
1614{
1615 MigrationState *s;
1616
1617 s = migrate_get_current();
1618
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001619 return s->parameters.compress_threads;
Liang Li8706d2d2015-03-23 16:32:17 +08001620}
1621
Liang Li3fcb38c2015-03-23 16:32:18 +08001622int migrate_decompress_threads(void)
1623{
1624 MigrationState *s;
1625
1626 s = migrate_get_current();
1627
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001628 return s->parameters.decompress_threads;
Liang Li3fcb38c2015-03-23 16:32:18 +08001629}
1630
Vladimir Sementsov-Ogievskiy55efc8c2018-03-13 15:34:00 -04001631bool migrate_dirty_bitmaps(void)
1632{
1633 MigrationState *s;
1634
1635 s = migrate_get_current();
1636
1637 return s->enabled_capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS];
1638}
1639
Juan Quintelab05dc722015-07-07 14:44:05 +02001640bool migrate_use_events(void)
1641{
1642 MigrationState *s;
1643
1644 s = migrate_get_current();
1645
1646 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1647}
1648
Juan Quintela30126bb2016-01-14 12:23:00 +01001649bool migrate_use_multifd(void)
1650{
1651 MigrationState *s;
1652
1653 s = migrate_get_current();
1654
1655 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_MULTIFD];
1656}
1657
Dr. David Alan Gilbert93fbd032017-10-20 10:05:50 +01001658bool migrate_pause_before_switchover(void)
1659{
1660 MigrationState *s;
1661
1662 s = migrate_get_current();
1663
1664 return s->enabled_capabilities[
1665 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER];
1666}
1667
Juan Quintela4075fb12016-01-15 08:56:17 +01001668int migrate_multifd_channels(void)
1669{
1670 MigrationState *s;
1671
1672 s = migrate_get_current();
1673
1674 return s->parameters.x_multifd_channels;
1675}
1676
Juan Quintela0fb86602017-04-27 10:48:25 +02001677int migrate_multifd_page_count(void)
1678{
1679 MigrationState *s;
1680
1681 s = migrate_get_current();
1682
1683 return s->parameters.x_multifd_page_count;
1684}
1685
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001686int migrate_use_xbzrle(void)
1687{
1688 MigrationState *s;
1689
1690 s = migrate_get_current();
1691
1692 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1693}
1694
1695int64_t migrate_xbzrle_cache_size(void)
1696{
1697 MigrationState *s;
1698
1699 s = migrate_get_current();
1700
Juan Quintela73af8dd2017-10-05 21:30:10 +02001701 return s->parameters.xbzrle_cache_size;
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001702}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001703
Juan Quintela2833c592017-04-05 18:32:37 +02001704bool migrate_use_block(void)
1705{
1706 MigrationState *s;
1707
1708 s = migrate_get_current();
1709
1710 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
1711}
1712
Peter Xuc788ada2017-06-26 18:28:55 +08001713bool migrate_use_return_path(void)
1714{
1715 MigrationState *s;
1716
1717 s = migrate_get_current();
1718
1719 return s->enabled_capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
1720}
1721
Juan Quintela2833c592017-04-05 18:32:37 +02001722bool migrate_use_block_incremental(void)
1723{
1724 MigrationState *s;
1725
1726 s = migrate_get_current();
1727
1728 return s->parameters.block_incremental;
1729}
1730
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001731/* migration thread support */
1732/*
1733 * Something bad happened to the RP stream, mark an error
1734 * The caller shall print or trace something to indicate why
1735 */
1736static void mark_source_rp_bad(MigrationState *s)
1737{
1738 s->rp_state.error = true;
1739}
1740
1741static struct rp_cmd_args {
1742 ssize_t len; /* -1 = variable */
1743 const char *name;
1744} rp_cmd_args[] = {
1745 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1746 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1747 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001748 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1749 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001750 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1751};
1752
1753/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001754 * Process a request for pages received on the return path,
1755 * We're allowed to send more than requested (e.g. to round to our page size)
1756 * and we don't need to send pages that have already been sent.
1757 */
1758static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1759 ram_addr_t start, size_t len)
1760{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001761 long our_host_ps = getpagesize();
1762
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001763 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001764
1765 /*
1766 * Since we currently insist on matching page sizes, just sanity check
1767 * we're being asked for whole host pages.
1768 */
1769 if (start & (our_host_ps-1) ||
1770 (len & (our_host_ps-1))) {
1771 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1772 " len: %zd", __func__, start, len);
1773 mark_source_rp_bad(ms);
1774 return;
1775 }
1776
Juan Quintela96506892017-03-14 18:41:03 +01001777 if (ram_save_queue_pages(rbname, start, len)) {
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001778 mark_source_rp_bad(ms);
1779 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001780}
1781
1782/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001783 * Handles messages sent on the return path towards the source VM
1784 *
1785 */
1786static void *source_return_path_thread(void *opaque)
1787{
1788 MigrationState *ms = opaque;
1789 QEMUFile *rp = ms->rp_state.from_dst_file;
1790 uint16_t header_len, header_type;
Peter Xu568b01c2016-03-09 14:12:12 +08001791 uint8_t buf[512];
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001792 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001793 ram_addr_t start = 0; /* =0 to silence warning */
1794 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001795 int res;
1796
1797 trace_source_return_path_thread_entry();
1798 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1799 migration_is_setup_or_active(ms->state)) {
1800 trace_source_return_path_thread_loop_top();
1801 header_type = qemu_get_be16(rp);
1802 header_len = qemu_get_be16(rp);
1803
Peter Xu7a9ddfb2018-02-08 18:31:05 +08001804 if (qemu_file_get_error(rp)) {
1805 mark_source_rp_bad(ms);
1806 goto out;
1807 }
1808
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001809 if (header_type >= MIG_RP_MSG_MAX ||
1810 header_type == MIG_RP_MSG_INVALID) {
1811 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1812 header_type, header_len);
1813 mark_source_rp_bad(ms);
1814 goto out;
1815 }
1816
1817 if ((rp_cmd_args[header_type].len != -1 &&
1818 header_len != rp_cmd_args[header_type].len) ||
Peter Xu568b01c2016-03-09 14:12:12 +08001819 header_len > sizeof(buf)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001820 error_report("RP: Received '%s' message (0x%04x) with"
1821 "incorrect length %d expecting %zu",
1822 rp_cmd_args[header_type].name, header_type, header_len,
1823 (size_t)rp_cmd_args[header_type].len);
1824 mark_source_rp_bad(ms);
1825 goto out;
1826 }
1827
1828 /* We know we've got a valid header by this point */
1829 res = qemu_get_buffer(rp, buf, header_len);
1830 if (res != header_len) {
1831 error_report("RP: Failed reading data for message 0x%04x"
1832 " read %d expected %d",
1833 header_type, res, header_len);
1834 mark_source_rp_bad(ms);
1835 goto out;
1836 }
1837
1838 /* OK, we have the message and the data */
1839 switch (header_type) {
1840 case MIG_RP_MSG_SHUT:
Peter Maydell4d885132016-06-10 17:09:22 +01001841 sibling_error = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001842 trace_source_return_path_thread_shut(sibling_error);
1843 if (sibling_error) {
1844 error_report("RP: Sibling indicated error %d", sibling_error);
1845 mark_source_rp_bad(ms);
1846 }
1847 /*
1848 * We'll let the main thread deal with closing the RP
1849 * we could do a shutdown(2) on it, but we're the only user
1850 * anyway, so there's nothing gained.
1851 */
1852 goto out;
1853
1854 case MIG_RP_MSG_PONG:
Peter Maydell4d885132016-06-10 17:09:22 +01001855 tmp32 = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001856 trace_source_return_path_thread_pong(tmp32);
1857 break;
1858
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001859 case MIG_RP_MSG_REQ_PAGES:
Peter Maydell4d885132016-06-10 17:09:22 +01001860 start = ldq_be_p(buf);
1861 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001862 migrate_handle_rp_req_pages(ms, NULL, start, len);
1863 break;
1864
1865 case MIG_RP_MSG_REQ_PAGES_ID:
1866 expected_len = 12 + 1; /* header + termination */
1867
1868 if (header_len >= expected_len) {
Peter Maydell4d885132016-06-10 17:09:22 +01001869 start = ldq_be_p(buf);
1870 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001871 /* Now we expect an idstr */
1872 tmp32 = buf[12]; /* Length of the following idstr */
1873 buf[13 + tmp32] = '\0';
1874 expected_len += tmp32;
1875 }
1876 if (header_len != expected_len) {
1877 error_report("RP: Req_Page_id with length %d expecting %zd",
1878 header_len, expected_len);
1879 mark_source_rp_bad(ms);
1880 goto out;
1881 }
1882 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
1883 break;
1884
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001885 default:
1886 break;
1887 }
1888 }
Dr. David Alan Gilbert5df54162015-11-18 11:48:41 +00001889 if (qemu_file_get_error(rp)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001890 trace_source_return_path_thread_bad_end();
1891 mark_source_rp_bad(ms);
1892 }
1893
1894 trace_source_return_path_thread_end();
1895out:
1896 ms->rp_state.from_dst_file = NULL;
1897 qemu_fclose(rp);
1898 return NULL;
1899}
1900
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001901static int open_return_path_on_source(MigrationState *ms)
1902{
1903
zhanghailiang89a02a92016-01-15 11:37:42 +08001904 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001905 if (!ms->rp_state.from_dst_file) {
1906 return -1;
1907 }
1908
1909 trace_open_return_path_on_source();
1910 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1911 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1912
1913 trace_open_return_path_on_source_continue();
1914
1915 return 0;
1916}
1917
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001918/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1919static int await_return_path_close_on_source(MigrationState *ms)
1920{
1921 /*
1922 * If this is a normal exit then the destination will send a SHUT and the
1923 * rp_thread will exit, however if there's an error we need to cause
1924 * it to exit.
1925 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001926 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001927 /*
1928 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1929 * waiting for the destination.
1930 */
1931 qemu_file_shutdown(ms->rp_state.from_dst_file);
1932 mark_source_rp_bad(ms);
1933 }
1934 trace_await_return_path_close_on_source_joining();
1935 qemu_thread_join(&ms->rp_state.rp_thread);
1936 trace_await_return_path_close_on_source_close();
1937 return ms->rp_state.error;
1938}
1939
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001940/*
1941 * Switch from normal iteration to postcopy
1942 * Returns non-0 on error
1943 */
Peter Xu7287cbd2018-01-03 20:20:09 +08001944static int postcopy_start(MigrationState *ms)
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001945{
1946 int ret;
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001947 QIOChannelBuffer *bioc;
1948 QEMUFile *fb;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001949 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001950 bool restart_block = false;
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01001951 int cur_state = MIGRATION_STATUS_ACTIVE;
1952 if (!migrate_pause_before_switchover()) {
1953 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
1954 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1955 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001956
1957 trace_postcopy_start();
1958 qemu_mutex_lock_iothread();
1959 trace_postcopy_start_set_run();
1960
1961 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001962 global_state_store();
1963 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001964 if (ret < 0) {
1965 goto fail;
1966 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001967
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01001968 ret = migration_maybe_pause(ms, &cur_state,
1969 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1970 if (ret < 0) {
1971 goto fail;
1972 }
1973
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001974 ret = bdrv_inactivate_all();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001975 if (ret < 0) {
1976 goto fail;
1977 }
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001978 restart_block = true;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001979
1980 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001981 * Cause any non-postcopiable, but iterative devices to
1982 * send out their final data.
1983 */
Fam Zhenga1fbe752017-06-17 00:06:58 +08001984 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001985
1986 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001987 * in Finish migrate and with the io-lock held everything should
1988 * be quiet, but we've potentially still got dirty pages and we
1989 * need to tell the destination to throw any pages it's already received
1990 * that are dirty
1991 */
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03001992 if (migrate_postcopy_ram()) {
1993 if (ram_postcopy_send_discard_bitmap(ms)) {
1994 error_report("postcopy send discard bitmap failed");
1995 goto fail;
1996 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001997 }
1998
1999 /*
2000 * send rest of state - note things that are doing postcopy
2001 * will notice we're in POSTCOPY_ACTIVE and not actually
2002 * wrap their state up here
2003 */
zhanghailiang89a02a92016-01-15 11:37:42 +08002004 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002005 if (migrate_postcopy_ram()) {
2006 /* Ping just for debugging, helps line traces up */
2007 qemu_savevm_send_ping(ms->to_dst_file, 2);
2008 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002009
2010 /*
2011 * While loading the device state we may trigger page transfer
2012 * requests and the fd must be free to process those, and thus
2013 * the destination must read the whole device state off the fd before
2014 * it starts processing it. Unfortunately the ad-hoc migration format
2015 * doesn't allow the destination to know the size to read without fully
2016 * parsing it through each devices load-state code (especially the open
2017 * coded devices that use get/put).
2018 * So we wrap the device state up in a package with a length at the start;
2019 * to do this we use a qemu_buf to hold the whole of the device state.
2020 */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002021 bioc = qio_channel_buffer_new(4096);
Daniel P. Berrange6f01f132016-09-30 11:57:14 +01002022 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002023 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
2024 object_unref(OBJECT(bioc));
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002025
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00002026 /*
2027 * Make sure the receiver can get incoming pages before we send the rest
2028 * of the state
2029 */
2030 qemu_savevm_send_postcopy_listen(fb);
2031
Fam Zhenga1fbe752017-06-17 00:06:58 +08002032 qemu_savevm_state_complete_precopy(fb, false, false);
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002033 if (migrate_postcopy_ram()) {
2034 qemu_savevm_send_ping(fb, 3);
2035 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002036
2037 qemu_savevm_send_postcopy_run(fb);
2038
2039 /* <><> end of stuff going into the package */
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002040
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002041 /* Last point of recovery; as soon as we send the package the destination
2042 * can open devices and potentially start running.
2043 * Lets just check again we've not got any errors.
2044 */
2045 ret = qemu_file_get_error(ms->to_dst_file);
2046 if (ret) {
2047 error_report("postcopy_start: Migration stream errored (pre package)");
2048 goto fail_closefb;
2049 }
2050
2051 restart_block = false;
2052
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002053 /* Now send that blob */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002054 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002055 goto fail_closefb;
2056 }
2057 qemu_fclose(fb);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00002058
2059 /* Send a notify to give a chance for anything that needs to happen
2060 * at the transition to postcopy and after the device state; in particular
2061 * spice needs to trigger a transition now
2062 */
2063 ms->postcopy_after_devices = true;
2064 notifier_list_notify(&migration_state_notifiers, ms);
2065
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002066 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
2067
2068 qemu_mutex_unlock_iothread();
2069
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002070 if (migrate_postcopy_ram()) {
2071 /*
2072 * Although this ping is just for debug, it could potentially be
2073 * used for getting a better measurement of downtime at the source.
2074 */
2075 qemu_savevm_send_ping(ms->to_dst_file, 4);
2076 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002077
Pavel Butsykinced1c612017-02-03 18:23:21 +03002078 if (migrate_release_ram()) {
2079 ram_postcopy_migrated_memory_release(ms);
2080 }
2081
zhanghailiang89a02a92016-01-15 11:37:42 +08002082 ret = qemu_file_get_error(ms->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002083 if (ret) {
2084 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00002085 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002086 MIGRATION_STATUS_FAILED);
2087 }
2088
2089 return ret;
2090
2091fail_closefb:
2092 qemu_fclose(fb);
2093fail:
zhanghailiang48781e52015-12-16 11:47:33 +00002094 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002095 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002096 if (restart_block) {
2097 /* A failure happened early enough that we know the destination hasn't
2098 * accessed block devices, so we're safe to recover.
2099 */
2100 Error *local_err = NULL;
2101
2102 bdrv_invalidate_cache_all(&local_err);
2103 if (local_err) {
2104 error_report_err(local_err);
2105 }
2106 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002107 qemu_mutex_unlock_iothread();
2108 return -1;
2109}
2110
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002111/**
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002112 * migration_maybe_pause: Pause if required to by
2113 * migrate_pause_before_switchover called with the iothread locked
2114 * Returns: 0 on success
2115 */
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002116static int migration_maybe_pause(MigrationState *s,
2117 int *current_active_state,
2118 int new_state)
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002119{
2120 if (!migrate_pause_before_switchover()) {
2121 return 0;
2122 }
2123
2124 /* Since leaving this state is not atomic with posting the semaphore
2125 * it's possible that someone could have issued multiple migrate_continue
2126 * and the semaphore is incorrectly positive at this point;
2127 * the docs say it's undefined to reinit a semaphore that's already
2128 * init'd, so use timedwait to eat up any existing posts.
2129 */
2130 while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) {
2131 /* This block intentionally left blank */
2132 }
2133
2134 qemu_mutex_unlock_iothread();
2135 migrate_set_state(&s->state, *current_active_state,
2136 MIGRATION_STATUS_PRE_SWITCHOVER);
2137 qemu_sem_wait(&s->pause_sem);
2138 migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER,
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002139 new_state);
2140 *current_active_state = new_state;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002141 qemu_mutex_lock_iothread();
2142
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002143 return s->state == new_state ? 0 : -EINVAL;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002144}
2145
2146/**
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002147 * migration_completion: Used by migration_thread when there's not much left.
2148 * The caller 'breaks' the loop when this returns.
2149 *
2150 * @s: Current migration state
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002151 */
Peter Xu2ad87302018-01-03 20:20:14 +08002152static void migration_completion(MigrationState *s)
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002153{
2154 int ret;
Peter Xu2ad87302018-01-03 20:20:14 +08002155 int current_active_state = s->state;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002156
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002157 if (s->state == MIGRATION_STATUS_ACTIVE) {
2158 qemu_mutex_lock_iothread();
Peter Xu64909f92018-01-03 20:20:10 +08002159 s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002160 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
Peter Xu7287cbd2018-01-03 20:20:09 +08002161 s->vm_was_running = runstate_is_running();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002162 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002163
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002164 if (!ret) {
Fam Zhenga1fbe752017-06-17 00:06:58 +08002165 bool inactivate = !migrate_colo_enabled();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002166 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02002167 if (ret >= 0) {
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002168 ret = migration_maybe_pause(s, &current_active_state,
2169 MIGRATION_STATUS_DEVICE);
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002170 }
2171 if (ret >= 0) {
Kevin Wolff07fa4c2017-05-22 17:10:38 +02002172 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
Fam Zhenga1fbe752017-06-17 00:06:58 +08002173 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
2174 inactivate);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02002175 }
Fam Zhenga1fbe752017-06-17 00:06:58 +08002176 if (inactivate && ret >= 0) {
2177 s->block_inactive = true;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002178 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002179 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002180 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002181
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002182 if (ret < 0) {
2183 goto fail;
2184 }
2185 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2186 trace_migration_completion_postcopy_end();
2187
zhanghailiang89a02a92016-01-15 11:37:42 +08002188 qemu_savevm_state_complete_postcopy(s->to_dst_file);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002189 trace_migration_completion_postcopy_end_after_complete();
2190 }
2191
2192 /*
2193 * If rp was opened we must clean up the thread before
2194 * cleaning everything else up (since if there are no failures
2195 * it will wait for the destination to send it's status in
2196 * a SHUT command).
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002197 */
Peter Xu0425dc92017-05-31 18:35:34 +08002198 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002199 int rp_error;
Peter Xu0425dc92017-05-31 18:35:34 +08002200 trace_migration_return_path_end_before();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002201 rp_error = await_return_path_close_on_source(s);
Peter Xu0425dc92017-05-31 18:35:34 +08002202 trace_migration_return_path_end_after(rp_error);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002203 if (rp_error) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02002204 goto fail_invalidate;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002205 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002206 }
2207
zhanghailiang89a02a92016-01-15 11:37:42 +08002208 if (qemu_file_get_error(s->to_dst_file)) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002209 trace_migration_completion_file_err();
Greg Kurzfe904ea2016-05-18 15:44:36 +02002210 goto fail_invalidate;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002211 }
2212
zhanghailiang0b827d52016-10-27 14:42:54 +08002213 if (!migrate_colo_enabled()) {
2214 migrate_set_state(&s->state, current_active_state,
2215 MIGRATION_STATUS_COMPLETED);
2216 }
2217
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002218 return;
2219
Greg Kurzfe904ea2016-05-18 15:44:36 +02002220fail_invalidate:
2221 /* If not doing postcopy, vm_start() will be called: let's regain
2222 * control on images.
2223 */
Dr. David Alan Gilbert6039dd52018-02-05 09:13:37 +00002224 if (s->state == MIGRATION_STATUS_ACTIVE ||
2225 s->state == MIGRATION_STATUS_DEVICE) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02002226 Error *local_err = NULL;
2227
zhanghailiang1d2acc32017-01-24 15:59:52 +08002228 qemu_mutex_lock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02002229 bdrv_invalidate_cache_all(&local_err);
2230 if (local_err) {
2231 error_report_err(local_err);
zhanghailiang1d2acc32017-01-24 15:59:52 +08002232 } else {
2233 s->block_inactive = false;
Greg Kurzfe904ea2016-05-18 15:44:36 +02002234 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08002235 qemu_mutex_unlock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02002236 }
2237
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002238fail:
zhanghailiang48781e52015-12-16 11:47:33 +00002239 migrate_set_state(&s->state, current_active_state,
2240 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002241}
2242
zhanghailiang35a6ed42016-10-27 14:42:52 +08002243bool migrate_colo_enabled(void)
2244{
2245 MigrationState *s = migrate_get_current();
2246 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
2247}
2248
Peter Xub23c2ad2018-05-02 18:47:19 +08002249typedef enum MigThrError {
2250 /* No error detected */
2251 MIG_THR_ERR_NONE = 0,
2252 /* Detected error, but resumed successfully */
2253 MIG_THR_ERR_RECOVERED = 1,
2254 /* Detected fatal error, need to exit */
2255 MIG_THR_ERR_FATAL = 2,
2256} MigThrError;
2257
2258/*
2259 * We don't return until we are in a safe state to continue current
2260 * postcopy migration. Returns MIG_THR_ERR_RECOVERED if recovered, or
2261 * MIG_THR_ERR_FATAL if unrecovery failure happened.
2262 */
2263static MigThrError postcopy_pause(MigrationState *s)
2264{
2265 assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
2266 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
2267 MIGRATION_STATUS_POSTCOPY_PAUSED);
2268
2269 /* Current channel is possibly broken. Release it. */
2270 assert(s->to_dst_file);
2271 qemu_file_shutdown(s->to_dst_file);
2272 qemu_fclose(s->to_dst_file);
2273 s->to_dst_file = NULL;
2274
2275 error_report("Detected IO failure for postcopy. "
2276 "Migration paused.");
2277
2278 /*
2279 * We wait until things fixed up. Then someone will setup the
2280 * status back for us.
2281 */
2282 while (s->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
2283 qemu_sem_wait(&s->postcopy_pause_sem);
2284 }
2285
2286 trace_postcopy_pause_continued();
2287
2288 return MIG_THR_ERR_RECOVERED;
2289}
2290
2291static MigThrError migration_detect_error(MigrationState *s)
2292{
2293 int ret;
2294
2295 /* Try to detect any file errors */
2296 ret = qemu_file_get_error(s->to_dst_file);
2297
2298 if (!ret) {
2299 /* Everything is fine */
2300 return MIG_THR_ERR_NONE;
2301 }
2302
2303 if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE && ret == -EIO) {
2304 /*
2305 * For postcopy, we allow the network to be down for a
2306 * while. After that, it can be continued by a
2307 * recovery phase.
2308 */
2309 return postcopy_pause(s);
2310 } else {
2311 /*
2312 * For precopy (or postcopy with error outside IO), we fail
2313 * with no time.
2314 */
2315 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
2316 trace_migration_thread_file_err();
2317
2318 /* Time to stop the migration, now. */
2319 return MIG_THR_ERR_FATAL;
2320 }
2321}
2322
Peter Xucf011f02018-01-03 20:20:11 +08002323static void migration_calculate_complete(MigrationState *s)
2324{
2325 uint64_t bytes = qemu_ftell(s->to_dst_file);
2326 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2327
2328 s->total_time = end_time - s->start_time;
2329 if (!s->downtime) {
2330 /*
2331 * It's still not set, so we are precopy migration. For
2332 * postcopy, downtime is calculated during postcopy_start().
2333 */
2334 s->downtime = end_time - s->downtime_start;
2335 }
2336
2337 if (s->total_time) {
2338 s->mbps = ((double) bytes * 8.0) / s->total_time / 1000;
2339 }
2340}
2341
Peter Xub15df1a2018-01-03 20:20:13 +08002342static void migration_update_counters(MigrationState *s,
2343 int64_t current_time)
2344{
2345 uint64_t transferred, time_spent;
Peter Xub15df1a2018-01-03 20:20:13 +08002346 double bandwidth;
2347
2348 if (current_time < s->iteration_start_time + BUFFER_DELAY) {
2349 return;
2350 }
2351
2352 transferred = qemu_ftell(s->to_dst_file) - s->iteration_initial_bytes;
2353 time_spent = current_time - s->iteration_start_time;
2354 bandwidth = (double)transferred / time_spent;
Wei Wang0781c1e2018-01-22 19:36:39 +08002355 s->threshold_size = bandwidth * s->parameters.downtime_limit;
Peter Xub15df1a2018-01-03 20:20:13 +08002356
2357 s->mbps = (((double) transferred * 8.0) /
2358 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
2359
2360 /*
2361 * if we haven't sent anything, we don't want to
2362 * recalculate. 10000 is a small enough number for our purposes
2363 */
2364 if (ram_counters.dirty_pages_rate && transferred > 10000) {
2365 s->expected_downtime = ram_counters.dirty_pages_rate *
2366 qemu_target_page_size() / bandwidth;
2367 }
2368
2369 qemu_file_reset_rate_limit(s->to_dst_file);
2370
2371 s->iteration_start_time = current_time;
2372 s->iteration_initial_bytes = qemu_ftell(s->to_dst_file);
2373
2374 trace_migrate_transferred(transferred, time_spent,
Wei Wang0781c1e2018-01-22 19:36:39 +08002375 bandwidth, s->threshold_size);
Peter Xub15df1a2018-01-03 20:20:13 +08002376}
2377
Peter Xu2ad87302018-01-03 20:20:14 +08002378/* Migration thread iteration status */
2379typedef enum {
2380 MIG_ITERATE_RESUME, /* Resume current iteration */
2381 MIG_ITERATE_SKIP, /* Skip current iteration */
2382 MIG_ITERATE_BREAK, /* Break the loop */
2383} MigIterateState;
2384
2385/*
2386 * Return true if continue to the next iteration directly, false
2387 * otherwise.
2388 */
2389static MigIterateState migration_iteration_run(MigrationState *s)
2390{
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04002391 uint64_t pending_size, pend_pre, pend_compat, pend_post;
Peter Xu2ad87302018-01-03 20:20:14 +08002392 bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE;
2393
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04002394 qemu_savevm_state_pending(s->to_dst_file, s->threshold_size, &pend_pre,
2395 &pend_compat, &pend_post);
2396 pending_size = pend_pre + pend_compat + pend_post;
Peter Xu2ad87302018-01-03 20:20:14 +08002397
2398 trace_migrate_pending(pending_size, s->threshold_size,
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04002399 pend_pre, pend_compat, pend_post);
Peter Xu2ad87302018-01-03 20:20:14 +08002400
2401 if (pending_size && pending_size >= s->threshold_size) {
2402 /* Still a significant amount to transfer */
2403 if (migrate_postcopy() && !in_postcopy &&
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04002404 pend_pre <= s->threshold_size &&
Peter Xu2ad87302018-01-03 20:20:14 +08002405 atomic_read(&s->start_postcopy)) {
2406 if (postcopy_start(s)) {
2407 error_report("%s: postcopy failed to start", __func__);
2408 }
2409 return MIG_ITERATE_SKIP;
2410 }
2411 /* Just another iteration step */
2412 qemu_savevm_state_iterate(s->to_dst_file,
2413 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
2414 } else {
2415 trace_migration_thread_low_pending(pending_size);
2416 migration_completion(s);
2417 return MIG_ITERATE_BREAK;
2418 }
2419
2420 return MIG_ITERATE_RESUME;
2421}
2422
Peter Xu199aa6d2018-01-03 20:20:15 +08002423static void migration_iteration_finish(MigrationState *s)
2424{
2425 /* If we enabled cpu throttling for auto-converge, turn it off. */
2426 cpu_throttle_stop();
2427
2428 qemu_mutex_lock_iothread();
2429 switch (s->state) {
2430 case MIGRATION_STATUS_COMPLETED:
2431 migration_calculate_complete(s);
2432 runstate_set(RUN_STATE_POSTMIGRATE);
2433 break;
2434
2435 case MIGRATION_STATUS_ACTIVE:
2436 /*
2437 * We should really assert here, but since it's during
2438 * migration, let's try to reduce the usage of assertions.
2439 */
2440 if (!migrate_colo_enabled()) {
2441 error_report("%s: critical error: calling COLO code without "
2442 "COLO enabled", __func__);
2443 }
2444 migrate_start_colo_process(s);
2445 /*
2446 * Fixme: we will run VM in COLO no matter its old running state.
2447 * After exited COLO, we will keep running.
2448 */
2449 s->vm_was_running = true;
2450 /* Fallthrough */
2451 case MIGRATION_STATUS_FAILED:
2452 case MIGRATION_STATUS_CANCELLED:
2453 if (s->vm_was_running) {
2454 vm_start();
2455 } else {
2456 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
2457 runstate_set(RUN_STATE_POSTMIGRATE);
2458 }
2459 }
2460 break;
2461
2462 default:
2463 /* Should not reach here, but if so, forgive the VM. */
2464 error_report("%s: Unknown ending state %d", __func__, s->state);
2465 break;
2466 }
2467 qemu_bh_schedule(s->cleanup_bh);
2468 qemu_mutex_unlock_iothread();
2469}
2470
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002471/*
2472 * Master migration thread on the source VM.
2473 * It drives the migration and pumps the data down the outgoing channel.
2474 */
Juan Quintela5f496a12013-02-22 17:36:30 +01002475static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002476{
Juan Quintela9848a402012-12-19 09:55:50 +01002477 MigrationState *s = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +01002478 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Peter Xub23c2ad2018-05-02 18:47:19 +08002479 MigThrError thr_error;
Juan Quintela76f59332012-10-03 20:16:24 +02002480
Paolo Bonziniab28bd22015-07-09 08:55:38 +02002481 rcu_register_thread();
2482
Peter Xub15df1a2018-01-03 20:20:13 +08002483 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2484
zhanghailiang89a02a92016-01-15 11:37:42 +08002485 qemu_savevm_state_header(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002486
Peter Xu62a02652017-06-14 15:55:58 +08002487 /*
2488 * If we opened the return path, we need to make sure dst has it
2489 * opened as well.
2490 */
2491 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002492 /* Now tell the dest that it should open its end so it can reply */
zhanghailiang89a02a92016-01-15 11:37:42 +08002493 qemu_savevm_send_open_return_path(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002494
2495 /* And do a ping that will make stuff easier to debug */
zhanghailiang89a02a92016-01-15 11:37:42 +08002496 qemu_savevm_send_ping(s->to_dst_file, 1);
Peter Xu0425dc92017-05-31 18:35:34 +08002497 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002498
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002499 if (migrate_postcopy()) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002500 /*
2501 * Tell the destination that we *might* want to do postcopy later;
2502 * if the other end can't do postcopy it should fail now, nice and
2503 * early.
2504 */
zhanghailiang89a02a92016-01-15 11:37:42 +08002505 qemu_savevm_send_postcopy_advise(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002506 }
2507
Juan Quintela9907e842017-06-28 11:52:24 +02002508 qemu_savevm_state_setup(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002509
Alex Blighbc72ad62013-08-21 16:03:08 +01002510 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang48781e52015-12-16 11:47:33 +00002511 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2512 MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04002513
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00002514 trace_migration_thread_setup_complete();
2515
2516 while (s->state == MIGRATION_STATUS_ACTIVE ||
2517 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +01002518 int64_t current_time;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002519
zhanghailiang89a02a92016-01-15 11:37:42 +08002520 if (!qemu_file_rate_limit(s->to_dst_file)) {
Peter Xu2ad87302018-01-03 20:20:14 +08002521 MigIterateState iter_state = migration_iteration_run(s);
2522 if (iter_state == MIG_ITERATE_SKIP) {
2523 continue;
2524 } else if (iter_state == MIG_ITERATE_BREAK) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002525 break;
Juan Quintelac369f402012-10-03 20:33:34 +02002526 }
2527 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01002528
Peter Xub23c2ad2018-05-02 18:47:19 +08002529 /*
2530 * Try to detect any kind of failures, and see whether we
2531 * should stop the migration now.
2532 */
2533 thr_error = migration_detect_error(s);
2534 if (thr_error == MIG_THR_ERR_FATAL) {
2535 /* Stop migration */
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01002536 break;
Peter Xub23c2ad2018-05-02 18:47:19 +08002537 } else if (thr_error == MIG_THR_ERR_RECOVERED) {
2538 /*
2539 * Just recovered from a e.g. network failure, reset all
2540 * the local variables. This is important to avoid
2541 * breaking transferred_bytes and bandwidth calculation
2542 */
2543 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2544 s->iteration_initial_bytes = 0;
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01002545 }
Peter Xub15df1a2018-01-03 20:20:13 +08002546
Alex Blighbc72ad62013-08-21 16:03:08 +01002547 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002548
Peter Xub15df1a2018-01-03 20:20:13 +08002549 migration_update_counters(s, current_time);
Michael R. Hines7e114f82013-06-25 21:35:30 -04002550
zhanghailiang89a02a92016-01-15 11:37:42 +08002551 if (qemu_file_rate_limit(s->to_dst_file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002552 /* usleep expects microseconds */
Peter Xub15df1a2018-01-03 20:20:13 +08002553 g_usleep((s->iteration_start_time + BUFFER_DELAY -
2554 current_time) * 1000);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002555 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002556 }
2557
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002558 trace_migration_thread_after_loop();
Peter Xu199aa6d2018-01-03 20:20:15 +08002559 migration_iteration_finish(s);
Paolo Bonziniab28bd22015-07-09 08:55:38 +02002560 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002561 return NULL;
2562}
2563
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00002564void migrate_fd_connect(MigrationState *s, Error *error_in)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002565{
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302566 s->expected_downtime = s->parameters.downtime_limit;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01002567 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00002568 if (error_in) {
2569 migrate_fd_error(s, error_in);
2570 migrate_fd_cleanup(s);
2571 return;
2572 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002573
Daniel P. Berrange9e4d2b92016-04-27 11:04:57 +01002574 qemu_file_set_blocking(s->to_dst_file, true);
zhanghailiang89a02a92016-01-15 11:37:42 +08002575 qemu_file_set_rate_limit(s->to_dst_file,
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302576 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
Paolo Bonzini442773c2013-02-22 17:36:44 +01002577
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02002578 /* Notify before starting migration thread */
2579 notifier_list_notify(&migration_state_notifiers, s);
2580
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002581 /*
Peter Xuc788ada2017-06-26 18:28:55 +08002582 * Open the return path. For postcopy, it is used exclusively. For
2583 * precopy, only if user specified "return-path" capability would
2584 * QEMU uses the return path.
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002585 */
Peter Xuc788ada2017-06-26 18:28:55 +08002586 if (migrate_postcopy_ram() || migrate_use_return_path()) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002587 if (open_return_path_on_source(s)) {
2588 error_report("Unable to open return-path for postcopy");
zhanghailiang48781e52015-12-16 11:47:33 +00002589 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002590 MIGRATION_STATUS_FAILED);
2591 migrate_fd_cleanup(s);
2592 return;
2593 }
2594 }
2595
Juan Quintelaf986c3d2016-01-14 16:52:55 +01002596 if (multifd_save_setup() != 0) {
2597 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2598 MIGRATION_STATUS_FAILED);
2599 migrate_fd_cleanup(s);
2600 return;
2601 }
Pankaj Gupta009fad72017-01-23 19:12:56 +05302602 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01002603 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002604 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002605}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00002606
Peter Xu9d18af92017-06-27 12:10:19 +08002607void migration_global_dump(Monitor *mon)
2608{
2609 MigrationState *ms = migrate_get_current();
2610
Juan Quintela6f0f6422017-10-26 11:49:57 +02002611 monitor_printf(mon, "globals:\n");
2612 monitor_printf(mon, "store-global-state: %s\n",
2613 ms->store_global_state ? "on" : "off");
2614 monitor_printf(mon, "only-migratable: %s\n",
2615 ms->only_migratable ? "on" : "off");
2616 monitor_printf(mon, "send-configuration: %s\n",
2617 ms->send_configuration ? "on" : "off");
2618 monitor_printf(mon, "send-section-footer: %s\n",
2619 ms->send_section_footer ? "on" : "off");
Peter Xu9d18af92017-06-27 12:10:19 +08002620}
2621
Peter Xu20814752017-07-18 11:39:03 +08002622#define DEFINE_PROP_MIG_CAP(name, x) \
2623 DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
2624
Peter Xu52722982017-06-27 12:10:14 +08002625static Property migration_properties[] = {
2626 DEFINE_PROP_BOOL("store-global-state", MigrationState,
2627 store_global_state, true),
Peter Xu3df663e2017-06-27 12:10:15 +08002628 DEFINE_PROP_BOOL("only-migratable", MigrationState, only_migratable, false),
Peter Xu71dd4c12017-06-27 12:10:16 +08002629 DEFINE_PROP_BOOL("send-configuration", MigrationState,
2630 send_configuration, true),
Peter Xu15c38502017-06-27 12:10:17 +08002631 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
2632 send_section_footer, true),
Peter Xu89632fa2017-07-18 11:39:02 +08002633
2634 /* Migration parameters */
Juan Quintela741d4082017-12-01 13:08:38 +01002635 DEFINE_PROP_UINT8("x-compress-level", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002636 parameters.compress_level,
2637 DEFAULT_MIGRATE_COMPRESS_LEVEL),
Juan Quintela741d4082017-12-01 13:08:38 +01002638 DEFINE_PROP_UINT8("x-compress-threads", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002639 parameters.compress_threads,
2640 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
Juan Quintela741d4082017-12-01 13:08:38 +01002641 DEFINE_PROP_UINT8("x-decompress-threads", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002642 parameters.decompress_threads,
2643 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
Juan Quintela741d4082017-12-01 13:08:38 +01002644 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002645 parameters.cpu_throttle_initial,
2646 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
Juan Quintela741d4082017-12-01 13:08:38 +01002647 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002648 parameters.cpu_throttle_increment,
2649 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
Juan Quintela741d4082017-12-01 13:08:38 +01002650 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002651 parameters.max_bandwidth, MAX_THROTTLE),
Juan Quintela741d4082017-12-01 13:08:38 +01002652 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002653 parameters.downtime_limit,
2654 DEFAULT_MIGRATE_SET_DOWNTIME),
Juan Quintela741d4082017-12-01 13:08:38 +01002655 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002656 parameters.x_checkpoint_delay,
2657 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
Juan Quintela741d4082017-12-01 13:08:38 +01002658 DEFINE_PROP_UINT8("x-multifd-channels", MigrationState,
Juan Quintela4075fb12016-01-15 08:56:17 +01002659 parameters.x_multifd_channels,
2660 DEFAULT_MIGRATE_MULTIFD_CHANNELS),
Juan Quintela741d4082017-12-01 13:08:38 +01002661 DEFINE_PROP_UINT32("x-multifd-page-count", MigrationState,
Juan Quintela0fb86602017-04-27 10:48:25 +02002662 parameters.x_multifd_page_count,
2663 DEFAULT_MIGRATE_MULTIFD_PAGE_COUNT),
Juan Quintela73af8dd2017-10-05 21:30:10 +02002664 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
2665 parameters.xbzrle_cache_size,
2666 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
Peter Xu20814752017-07-18 11:39:03 +08002667
2668 /* Migration capabilities */
2669 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
2670 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
2671 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
2672 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
2673 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
2674 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
2675 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
2676 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
2677 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
2678 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
2679 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
Juan Quintela30126bb2016-01-14 12:23:00 +01002680 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_X_MULTIFD),
Peter Xu20814752017-07-18 11:39:03 +08002681
Peter Xu52722982017-06-27 12:10:14 +08002682 DEFINE_PROP_END_OF_LIST(),
2683};
2684
Peter Xue5cb7e72017-06-27 12:10:13 +08002685static void migration_class_init(ObjectClass *klass, void *data)
2686{
2687 DeviceClass *dc = DEVICE_CLASS(klass);
2688
2689 dc->user_creatable = false;
Peter Xu52722982017-06-27 12:10:14 +08002690 dc->props = migration_properties;
Peter Xue5cb7e72017-06-27 12:10:13 +08002691}
2692
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01002693static void migration_instance_finalize(Object *obj)
2694{
2695 MigrationState *ms = MIGRATION_OBJ(obj);
2696 MigrationParameters *params = &ms->parameters;
2697
Juan Quintela87db1a72017-09-05 12:50:22 +02002698 qemu_mutex_destroy(&ms->error_mutex);
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01002699 g_free(params->tls_hostname);
2700 g_free(params->tls_creds);
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002701 qemu_sem_destroy(&ms->pause_sem);
Peter Xub23c2ad2018-05-02 18:47:19 +08002702 qemu_sem_destroy(&ms->postcopy_pause_sem);
Marc-André Lureauab105cc2018-03-06 18:09:59 +01002703 error_free(ms->error);
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01002704}
2705
Peter Xue5cb7e72017-06-27 12:10:13 +08002706static void migration_instance_init(Object *obj)
2707{
2708 MigrationState *ms = MIGRATION_OBJ(obj);
Peter Xu8b0b29d2017-07-18 11:39:06 +08002709 MigrationParameters *params = &ms->parameters;
Peter Xue5cb7e72017-06-27 12:10:13 +08002710
2711 ms->state = MIGRATION_STATUS_NONE;
Peter Xue5cb7e72017-06-27 12:10:13 +08002712 ms->mbps = -1;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002713 qemu_sem_init(&ms->pause_sem, 0);
Juan Quintela87db1a72017-09-05 12:50:22 +02002714 qemu_mutex_init(&ms->error_mutex);
Peter Xu8b0b29d2017-07-18 11:39:06 +08002715
2716 params->tls_hostname = g_strdup("");
2717 params->tls_creds = g_strdup("");
2718
2719 /* Set has_* up only for parameter checks */
2720 params->has_compress_level = true;
2721 params->has_compress_threads = true;
2722 params->has_decompress_threads = true;
2723 params->has_cpu_throttle_initial = true;
2724 params->has_cpu_throttle_increment = true;
2725 params->has_max_bandwidth = true;
2726 params->has_downtime_limit = true;
2727 params->has_x_checkpoint_delay = true;
2728 params->has_block_incremental = true;
Juan Quintela4075fb12016-01-15 08:56:17 +01002729 params->has_x_multifd_channels = true;
Juan Quintela0fb86602017-04-27 10:48:25 +02002730 params->has_x_multifd_page_count = true;
Juan Quintela73af8dd2017-10-05 21:30:10 +02002731 params->has_xbzrle_cache_size = true;
Peter Xub23c2ad2018-05-02 18:47:19 +08002732
2733 qemu_sem_init(&ms->postcopy_pause_sem, 0);
Peter Xu8b0b29d2017-07-18 11:39:06 +08002734}
2735
2736/*
2737 * Return true if check pass, false otherwise. Error will be put
2738 * inside errp if provided.
2739 */
2740static bool migration_object_check(MigrationState *ms, Error **errp)
2741{
Peter Xu6b19a7d2017-07-18 11:39:10 +08002742 MigrationCapabilityStatusList *head = NULL;
2743 /* Assuming all off */
2744 bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret;
2745 int i;
2746
Peter Xu8b0b29d2017-07-18 11:39:06 +08002747 if (!migrate_params_check(&ms->parameters, errp)) {
2748 return false;
2749 }
2750
Peter Xu6b19a7d2017-07-18 11:39:10 +08002751 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
2752 if (ms->enabled_capabilities[i]) {
2753 head = migrate_cap_add(head, i, true);
2754 }
2755 }
2756
2757 ret = migrate_caps_check(cap_list, head, errp);
2758
2759 /* It works with head == NULL */
2760 qapi_free_MigrationCapabilityStatusList(head);
2761
2762 return ret;
Peter Xue5cb7e72017-06-27 12:10:13 +08002763}
2764
2765static const TypeInfo migration_type = {
2766 .name = TYPE_MIGRATION,
Peter Xu01f6e142017-06-28 15:15:44 +08002767 /*
Peter Xuc8d3ff32017-07-05 16:21:23 +08002768 * NOTE: TYPE_MIGRATION is not really a device, as the object is
2769 * not created using qdev_create(), it is not attached to the qdev
2770 * device tree, and it is never realized.
2771 *
2772 * TODO: Make this TYPE_OBJECT once QOM provides something like
2773 * TYPE_DEVICE's "-global" properties.
Peter Xu01f6e142017-06-28 15:15:44 +08002774 */
Peter Xue5cb7e72017-06-27 12:10:13 +08002775 .parent = TYPE_DEVICE,
2776 .class_init = migration_class_init,
2777 .class_size = sizeof(MigrationClass),
2778 .instance_size = sizeof(MigrationState),
2779 .instance_init = migration_instance_init,
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01002780 .instance_finalize = migration_instance_finalize,
Peter Xue5cb7e72017-06-27 12:10:13 +08002781};
2782
2783static void register_migration_types(void)
2784{
2785 type_register_static(&migration_type);
2786}
2787
2788type_init(register_migration_types);