blob: f9c58d2511e777155aa0ff8ba0a898146f205688 [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
Peter Xu14b17422018-05-02 18:47:21 +08001782/* Return true to retry, false to quit */
1783static bool postcopy_pause_return_path_thread(MigrationState *s)
1784{
1785 trace_postcopy_pause_return_path();
1786
1787 qemu_sem_wait(&s->postcopy_pause_rp_sem);
1788
1789 trace_postcopy_pause_return_path_continued();
1790
1791 return true;
1792}
1793
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001794/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001795 * Handles messages sent on the return path towards the source VM
1796 *
1797 */
1798static void *source_return_path_thread(void *opaque)
1799{
1800 MigrationState *ms = opaque;
1801 QEMUFile *rp = ms->rp_state.from_dst_file;
1802 uint16_t header_len, header_type;
Peter Xu568b01c2016-03-09 14:12:12 +08001803 uint8_t buf[512];
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001804 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001805 ram_addr_t start = 0; /* =0 to silence warning */
1806 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001807 int res;
1808
1809 trace_source_return_path_thread_entry();
Peter Xu14b17422018-05-02 18:47:21 +08001810
1811retry:
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001812 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1813 migration_is_setup_or_active(ms->state)) {
1814 trace_source_return_path_thread_loop_top();
1815 header_type = qemu_get_be16(rp);
1816 header_len = qemu_get_be16(rp);
1817
Peter Xu7a9ddfb2018-02-08 18:31:05 +08001818 if (qemu_file_get_error(rp)) {
1819 mark_source_rp_bad(ms);
1820 goto out;
1821 }
1822
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001823 if (header_type >= MIG_RP_MSG_MAX ||
1824 header_type == MIG_RP_MSG_INVALID) {
1825 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1826 header_type, header_len);
1827 mark_source_rp_bad(ms);
1828 goto out;
1829 }
1830
1831 if ((rp_cmd_args[header_type].len != -1 &&
1832 header_len != rp_cmd_args[header_type].len) ||
Peter Xu568b01c2016-03-09 14:12:12 +08001833 header_len > sizeof(buf)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001834 error_report("RP: Received '%s' message (0x%04x) with"
1835 "incorrect length %d expecting %zu",
1836 rp_cmd_args[header_type].name, header_type, header_len,
1837 (size_t)rp_cmd_args[header_type].len);
1838 mark_source_rp_bad(ms);
1839 goto out;
1840 }
1841
1842 /* We know we've got a valid header by this point */
1843 res = qemu_get_buffer(rp, buf, header_len);
1844 if (res != header_len) {
1845 error_report("RP: Failed reading data for message 0x%04x"
1846 " read %d expected %d",
1847 header_type, res, header_len);
1848 mark_source_rp_bad(ms);
1849 goto out;
1850 }
1851
1852 /* OK, we have the message and the data */
1853 switch (header_type) {
1854 case MIG_RP_MSG_SHUT:
Peter Maydell4d885132016-06-10 17:09:22 +01001855 sibling_error = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001856 trace_source_return_path_thread_shut(sibling_error);
1857 if (sibling_error) {
1858 error_report("RP: Sibling indicated error %d", sibling_error);
1859 mark_source_rp_bad(ms);
1860 }
1861 /*
1862 * We'll let the main thread deal with closing the RP
1863 * we could do a shutdown(2) on it, but we're the only user
1864 * anyway, so there's nothing gained.
1865 */
1866 goto out;
1867
1868 case MIG_RP_MSG_PONG:
Peter Maydell4d885132016-06-10 17:09:22 +01001869 tmp32 = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001870 trace_source_return_path_thread_pong(tmp32);
1871 break;
1872
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001873 case MIG_RP_MSG_REQ_PAGES:
Peter Maydell4d885132016-06-10 17:09:22 +01001874 start = ldq_be_p(buf);
1875 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001876 migrate_handle_rp_req_pages(ms, NULL, start, len);
1877 break;
1878
1879 case MIG_RP_MSG_REQ_PAGES_ID:
1880 expected_len = 12 + 1; /* header + termination */
1881
1882 if (header_len >= expected_len) {
Peter Maydell4d885132016-06-10 17:09:22 +01001883 start = ldq_be_p(buf);
1884 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001885 /* Now we expect an idstr */
1886 tmp32 = buf[12]; /* Length of the following idstr */
1887 buf[13 + tmp32] = '\0';
1888 expected_len += tmp32;
1889 }
1890 if (header_len != expected_len) {
1891 error_report("RP: Req_Page_id with length %d expecting %zd",
1892 header_len, expected_len);
1893 mark_source_rp_bad(ms);
1894 goto out;
1895 }
1896 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
1897 break;
1898
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001899 default:
1900 break;
1901 }
1902 }
Peter Xu14b17422018-05-02 18:47:21 +08001903
1904out:
1905 res = qemu_file_get_error(rp);
1906 if (res) {
1907 if (res == -EIO) {
1908 /*
1909 * Maybe there is something we can do: it looks like a
1910 * network down issue, and we pause for a recovery.
1911 */
1912 if (postcopy_pause_return_path_thread(ms)) {
1913 /* Reload rp, reset the rest */
1914 rp = ms->rp_state.from_dst_file;
1915 ms->rp_state.error = false;
1916 goto retry;
1917 }
1918 }
1919
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001920 trace_source_return_path_thread_bad_end();
1921 mark_source_rp_bad(ms);
1922 }
1923
1924 trace_source_return_path_thread_end();
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001925 ms->rp_state.from_dst_file = NULL;
1926 qemu_fclose(rp);
1927 return NULL;
1928}
1929
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001930static int open_return_path_on_source(MigrationState *ms)
1931{
1932
zhanghailiang89a02a92016-01-15 11:37:42 +08001933 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001934 if (!ms->rp_state.from_dst_file) {
1935 return -1;
1936 }
1937
1938 trace_open_return_path_on_source();
1939 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1940 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1941
1942 trace_open_return_path_on_source_continue();
1943
1944 return 0;
1945}
1946
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001947/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1948static int await_return_path_close_on_source(MigrationState *ms)
1949{
1950 /*
1951 * If this is a normal exit then the destination will send a SHUT and the
1952 * rp_thread will exit, however if there's an error we need to cause
1953 * it to exit.
1954 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001955 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001956 /*
1957 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1958 * waiting for the destination.
1959 */
1960 qemu_file_shutdown(ms->rp_state.from_dst_file);
1961 mark_source_rp_bad(ms);
1962 }
1963 trace_await_return_path_close_on_source_joining();
1964 qemu_thread_join(&ms->rp_state.rp_thread);
1965 trace_await_return_path_close_on_source_close();
1966 return ms->rp_state.error;
1967}
1968
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001969/*
1970 * Switch from normal iteration to postcopy
1971 * Returns non-0 on error
1972 */
Peter Xu7287cbd2018-01-03 20:20:09 +08001973static int postcopy_start(MigrationState *ms)
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001974{
1975 int ret;
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001976 QIOChannelBuffer *bioc;
1977 QEMUFile *fb;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001978 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001979 bool restart_block = false;
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01001980 int cur_state = MIGRATION_STATUS_ACTIVE;
1981 if (!migrate_pause_before_switchover()) {
1982 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
1983 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1984 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001985
1986 trace_postcopy_start();
1987 qemu_mutex_lock_iothread();
1988 trace_postcopy_start_set_run();
1989
1990 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001991 global_state_store();
1992 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001993 if (ret < 0) {
1994 goto fail;
1995 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001996
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01001997 ret = migration_maybe_pause(ms, &cur_state,
1998 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1999 if (ret < 0) {
2000 goto fail;
2001 }
2002
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01002003 ret = bdrv_inactivate_all();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002004 if (ret < 0) {
2005 goto fail;
2006 }
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002007 restart_block = true;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002008
2009 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00002010 * Cause any non-postcopiable, but iterative devices to
2011 * send out their final data.
2012 */
Fam Zhenga1fbe752017-06-17 00:06:58 +08002013 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00002014
2015 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002016 * in Finish migrate and with the io-lock held everything should
2017 * be quiet, but we've potentially still got dirty pages and we
2018 * need to tell the destination to throw any pages it's already received
2019 * that are dirty
2020 */
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002021 if (migrate_postcopy_ram()) {
2022 if (ram_postcopy_send_discard_bitmap(ms)) {
2023 error_report("postcopy send discard bitmap failed");
2024 goto fail;
2025 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002026 }
2027
2028 /*
2029 * send rest of state - note things that are doing postcopy
2030 * will notice we're in POSTCOPY_ACTIVE and not actually
2031 * wrap their state up here
2032 */
zhanghailiang89a02a92016-01-15 11:37:42 +08002033 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002034 if (migrate_postcopy_ram()) {
2035 /* Ping just for debugging, helps line traces up */
2036 qemu_savevm_send_ping(ms->to_dst_file, 2);
2037 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002038
2039 /*
2040 * While loading the device state we may trigger page transfer
2041 * requests and the fd must be free to process those, and thus
2042 * the destination must read the whole device state off the fd before
2043 * it starts processing it. Unfortunately the ad-hoc migration format
2044 * doesn't allow the destination to know the size to read without fully
2045 * parsing it through each devices load-state code (especially the open
2046 * coded devices that use get/put).
2047 * So we wrap the device state up in a package with a length at the start;
2048 * to do this we use a qemu_buf to hold the whole of the device state.
2049 */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002050 bioc = qio_channel_buffer_new(4096);
Daniel P. Berrange6f01f132016-09-30 11:57:14 +01002051 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002052 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
2053 object_unref(OBJECT(bioc));
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002054
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00002055 /*
2056 * Make sure the receiver can get incoming pages before we send the rest
2057 * of the state
2058 */
2059 qemu_savevm_send_postcopy_listen(fb);
2060
Fam Zhenga1fbe752017-06-17 00:06:58 +08002061 qemu_savevm_state_complete_precopy(fb, false, false);
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002062 if (migrate_postcopy_ram()) {
2063 qemu_savevm_send_ping(fb, 3);
2064 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002065
2066 qemu_savevm_send_postcopy_run(fb);
2067
2068 /* <><> end of stuff going into the package */
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002069
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002070 /* Last point of recovery; as soon as we send the package the destination
2071 * can open devices and potentially start running.
2072 * Lets just check again we've not got any errors.
2073 */
2074 ret = qemu_file_get_error(ms->to_dst_file);
2075 if (ret) {
2076 error_report("postcopy_start: Migration stream errored (pre package)");
2077 goto fail_closefb;
2078 }
2079
2080 restart_block = false;
2081
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002082 /* Now send that blob */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002083 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002084 goto fail_closefb;
2085 }
2086 qemu_fclose(fb);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00002087
2088 /* Send a notify to give a chance for anything that needs to happen
2089 * at the transition to postcopy and after the device state; in particular
2090 * spice needs to trigger a transition now
2091 */
2092 ms->postcopy_after_devices = true;
2093 notifier_list_notify(&migration_state_notifiers, ms);
2094
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002095 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
2096
2097 qemu_mutex_unlock_iothread();
2098
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002099 if (migrate_postcopy_ram()) {
2100 /*
2101 * Although this ping is just for debug, it could potentially be
2102 * used for getting a better measurement of downtime at the source.
2103 */
2104 qemu_savevm_send_ping(ms->to_dst_file, 4);
2105 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002106
Pavel Butsykinced1c612017-02-03 18:23:21 +03002107 if (migrate_release_ram()) {
2108 ram_postcopy_migrated_memory_release(ms);
2109 }
2110
zhanghailiang89a02a92016-01-15 11:37:42 +08002111 ret = qemu_file_get_error(ms->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002112 if (ret) {
2113 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00002114 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002115 MIGRATION_STATUS_FAILED);
2116 }
2117
2118 return ret;
2119
2120fail_closefb:
2121 qemu_fclose(fb);
2122fail:
zhanghailiang48781e52015-12-16 11:47:33 +00002123 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002124 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002125 if (restart_block) {
2126 /* A failure happened early enough that we know the destination hasn't
2127 * accessed block devices, so we're safe to recover.
2128 */
2129 Error *local_err = NULL;
2130
2131 bdrv_invalidate_cache_all(&local_err);
2132 if (local_err) {
2133 error_report_err(local_err);
2134 }
2135 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002136 qemu_mutex_unlock_iothread();
2137 return -1;
2138}
2139
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002140/**
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002141 * migration_maybe_pause: Pause if required to by
2142 * migrate_pause_before_switchover called with the iothread locked
2143 * Returns: 0 on success
2144 */
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002145static int migration_maybe_pause(MigrationState *s,
2146 int *current_active_state,
2147 int new_state)
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002148{
2149 if (!migrate_pause_before_switchover()) {
2150 return 0;
2151 }
2152
2153 /* Since leaving this state is not atomic with posting the semaphore
2154 * it's possible that someone could have issued multiple migrate_continue
2155 * and the semaphore is incorrectly positive at this point;
2156 * the docs say it's undefined to reinit a semaphore that's already
2157 * init'd, so use timedwait to eat up any existing posts.
2158 */
2159 while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) {
2160 /* This block intentionally left blank */
2161 }
2162
2163 qemu_mutex_unlock_iothread();
2164 migrate_set_state(&s->state, *current_active_state,
2165 MIGRATION_STATUS_PRE_SWITCHOVER);
2166 qemu_sem_wait(&s->pause_sem);
2167 migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER,
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002168 new_state);
2169 *current_active_state = new_state;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002170 qemu_mutex_lock_iothread();
2171
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002172 return s->state == new_state ? 0 : -EINVAL;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002173}
2174
2175/**
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002176 * migration_completion: Used by migration_thread when there's not much left.
2177 * The caller 'breaks' the loop when this returns.
2178 *
2179 * @s: Current migration state
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002180 */
Peter Xu2ad87302018-01-03 20:20:14 +08002181static void migration_completion(MigrationState *s)
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002182{
2183 int ret;
Peter Xu2ad87302018-01-03 20:20:14 +08002184 int current_active_state = s->state;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002185
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002186 if (s->state == MIGRATION_STATUS_ACTIVE) {
2187 qemu_mutex_lock_iothread();
Peter Xu64909f92018-01-03 20:20:10 +08002188 s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002189 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
Peter Xu7287cbd2018-01-03 20:20:09 +08002190 s->vm_was_running = runstate_is_running();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002191 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002192
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002193 if (!ret) {
Fam Zhenga1fbe752017-06-17 00:06:58 +08002194 bool inactivate = !migrate_colo_enabled();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002195 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02002196 if (ret >= 0) {
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002197 ret = migration_maybe_pause(s, &current_active_state,
2198 MIGRATION_STATUS_DEVICE);
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002199 }
2200 if (ret >= 0) {
Kevin Wolff07fa4c2017-05-22 17:10:38 +02002201 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
Fam Zhenga1fbe752017-06-17 00:06:58 +08002202 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
2203 inactivate);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02002204 }
Fam Zhenga1fbe752017-06-17 00:06:58 +08002205 if (inactivate && ret >= 0) {
2206 s->block_inactive = true;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002207 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002208 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002209 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002210
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002211 if (ret < 0) {
2212 goto fail;
2213 }
2214 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2215 trace_migration_completion_postcopy_end();
2216
zhanghailiang89a02a92016-01-15 11:37:42 +08002217 qemu_savevm_state_complete_postcopy(s->to_dst_file);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002218 trace_migration_completion_postcopy_end_after_complete();
2219 }
2220
2221 /*
2222 * If rp was opened we must clean up the thread before
2223 * cleaning everything else up (since if there are no failures
2224 * it will wait for the destination to send it's status in
2225 * a SHUT command).
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002226 */
Peter Xu0425dc92017-05-31 18:35:34 +08002227 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002228 int rp_error;
Peter Xu0425dc92017-05-31 18:35:34 +08002229 trace_migration_return_path_end_before();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002230 rp_error = await_return_path_close_on_source(s);
Peter Xu0425dc92017-05-31 18:35:34 +08002231 trace_migration_return_path_end_after(rp_error);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002232 if (rp_error) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02002233 goto fail_invalidate;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002234 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002235 }
2236
zhanghailiang89a02a92016-01-15 11:37:42 +08002237 if (qemu_file_get_error(s->to_dst_file)) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002238 trace_migration_completion_file_err();
Greg Kurzfe904ea2016-05-18 15:44:36 +02002239 goto fail_invalidate;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002240 }
2241
zhanghailiang0b827d52016-10-27 14:42:54 +08002242 if (!migrate_colo_enabled()) {
2243 migrate_set_state(&s->state, current_active_state,
2244 MIGRATION_STATUS_COMPLETED);
2245 }
2246
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002247 return;
2248
Greg Kurzfe904ea2016-05-18 15:44:36 +02002249fail_invalidate:
2250 /* If not doing postcopy, vm_start() will be called: let's regain
2251 * control on images.
2252 */
Dr. David Alan Gilbert6039dd52018-02-05 09:13:37 +00002253 if (s->state == MIGRATION_STATUS_ACTIVE ||
2254 s->state == MIGRATION_STATUS_DEVICE) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02002255 Error *local_err = NULL;
2256
zhanghailiang1d2acc32017-01-24 15:59:52 +08002257 qemu_mutex_lock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02002258 bdrv_invalidate_cache_all(&local_err);
2259 if (local_err) {
2260 error_report_err(local_err);
zhanghailiang1d2acc32017-01-24 15:59:52 +08002261 } else {
2262 s->block_inactive = false;
Greg Kurzfe904ea2016-05-18 15:44:36 +02002263 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08002264 qemu_mutex_unlock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02002265 }
2266
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002267fail:
zhanghailiang48781e52015-12-16 11:47:33 +00002268 migrate_set_state(&s->state, current_active_state,
2269 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002270}
2271
zhanghailiang35a6ed42016-10-27 14:42:52 +08002272bool migrate_colo_enabled(void)
2273{
2274 MigrationState *s = migrate_get_current();
2275 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
2276}
2277
Peter Xub23c2ad2018-05-02 18:47:19 +08002278typedef enum MigThrError {
2279 /* No error detected */
2280 MIG_THR_ERR_NONE = 0,
2281 /* Detected error, but resumed successfully */
2282 MIG_THR_ERR_RECOVERED = 1,
2283 /* Detected fatal error, need to exit */
2284 MIG_THR_ERR_FATAL = 2,
2285} MigThrError;
2286
2287/*
2288 * We don't return until we are in a safe state to continue current
2289 * postcopy migration. Returns MIG_THR_ERR_RECOVERED if recovered, or
2290 * MIG_THR_ERR_FATAL if unrecovery failure happened.
2291 */
2292static MigThrError postcopy_pause(MigrationState *s)
2293{
2294 assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
2295 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
2296 MIGRATION_STATUS_POSTCOPY_PAUSED);
2297
2298 /* Current channel is possibly broken. Release it. */
2299 assert(s->to_dst_file);
2300 qemu_file_shutdown(s->to_dst_file);
2301 qemu_fclose(s->to_dst_file);
2302 s->to_dst_file = NULL;
2303
2304 error_report("Detected IO failure for postcopy. "
2305 "Migration paused.");
2306
2307 /*
2308 * We wait until things fixed up. Then someone will setup the
2309 * status back for us.
2310 */
2311 while (s->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
2312 qemu_sem_wait(&s->postcopy_pause_sem);
2313 }
2314
2315 trace_postcopy_pause_continued();
2316
2317 return MIG_THR_ERR_RECOVERED;
2318}
2319
2320static MigThrError migration_detect_error(MigrationState *s)
2321{
2322 int ret;
2323
2324 /* Try to detect any file errors */
2325 ret = qemu_file_get_error(s->to_dst_file);
2326
2327 if (!ret) {
2328 /* Everything is fine */
2329 return MIG_THR_ERR_NONE;
2330 }
2331
2332 if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE && ret == -EIO) {
2333 /*
2334 * For postcopy, we allow the network to be down for a
2335 * while. After that, it can be continued by a
2336 * recovery phase.
2337 */
2338 return postcopy_pause(s);
2339 } else {
2340 /*
2341 * For precopy (or postcopy with error outside IO), we fail
2342 * with no time.
2343 */
2344 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
2345 trace_migration_thread_file_err();
2346
2347 /* Time to stop the migration, now. */
2348 return MIG_THR_ERR_FATAL;
2349 }
2350}
2351
Peter Xucf011f02018-01-03 20:20:11 +08002352static void migration_calculate_complete(MigrationState *s)
2353{
2354 uint64_t bytes = qemu_ftell(s->to_dst_file);
2355 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2356
2357 s->total_time = end_time - s->start_time;
2358 if (!s->downtime) {
2359 /*
2360 * It's still not set, so we are precopy migration. For
2361 * postcopy, downtime is calculated during postcopy_start().
2362 */
2363 s->downtime = end_time - s->downtime_start;
2364 }
2365
2366 if (s->total_time) {
2367 s->mbps = ((double) bytes * 8.0) / s->total_time / 1000;
2368 }
2369}
2370
Peter Xub15df1a2018-01-03 20:20:13 +08002371static void migration_update_counters(MigrationState *s,
2372 int64_t current_time)
2373{
2374 uint64_t transferred, time_spent;
Peter Xub15df1a2018-01-03 20:20:13 +08002375 double bandwidth;
2376
2377 if (current_time < s->iteration_start_time + BUFFER_DELAY) {
2378 return;
2379 }
2380
2381 transferred = qemu_ftell(s->to_dst_file) - s->iteration_initial_bytes;
2382 time_spent = current_time - s->iteration_start_time;
2383 bandwidth = (double)transferred / time_spent;
Wei Wang0781c1e2018-01-22 19:36:39 +08002384 s->threshold_size = bandwidth * s->parameters.downtime_limit;
Peter Xub15df1a2018-01-03 20:20:13 +08002385
2386 s->mbps = (((double) transferred * 8.0) /
2387 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
2388
2389 /*
2390 * if we haven't sent anything, we don't want to
2391 * recalculate. 10000 is a small enough number for our purposes
2392 */
2393 if (ram_counters.dirty_pages_rate && transferred > 10000) {
2394 s->expected_downtime = ram_counters.dirty_pages_rate *
2395 qemu_target_page_size() / bandwidth;
2396 }
2397
2398 qemu_file_reset_rate_limit(s->to_dst_file);
2399
2400 s->iteration_start_time = current_time;
2401 s->iteration_initial_bytes = qemu_ftell(s->to_dst_file);
2402
2403 trace_migrate_transferred(transferred, time_spent,
Wei Wang0781c1e2018-01-22 19:36:39 +08002404 bandwidth, s->threshold_size);
Peter Xub15df1a2018-01-03 20:20:13 +08002405}
2406
Peter Xu2ad87302018-01-03 20:20:14 +08002407/* Migration thread iteration status */
2408typedef enum {
2409 MIG_ITERATE_RESUME, /* Resume current iteration */
2410 MIG_ITERATE_SKIP, /* Skip current iteration */
2411 MIG_ITERATE_BREAK, /* Break the loop */
2412} MigIterateState;
2413
2414/*
2415 * Return true if continue to the next iteration directly, false
2416 * otherwise.
2417 */
2418static MigIterateState migration_iteration_run(MigrationState *s)
2419{
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04002420 uint64_t pending_size, pend_pre, pend_compat, pend_post;
Peter Xu2ad87302018-01-03 20:20:14 +08002421 bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE;
2422
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04002423 qemu_savevm_state_pending(s->to_dst_file, s->threshold_size, &pend_pre,
2424 &pend_compat, &pend_post);
2425 pending_size = pend_pre + pend_compat + pend_post;
Peter Xu2ad87302018-01-03 20:20:14 +08002426
2427 trace_migrate_pending(pending_size, s->threshold_size,
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04002428 pend_pre, pend_compat, pend_post);
Peter Xu2ad87302018-01-03 20:20:14 +08002429
2430 if (pending_size && pending_size >= s->threshold_size) {
2431 /* Still a significant amount to transfer */
2432 if (migrate_postcopy() && !in_postcopy &&
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04002433 pend_pre <= s->threshold_size &&
Peter Xu2ad87302018-01-03 20:20:14 +08002434 atomic_read(&s->start_postcopy)) {
2435 if (postcopy_start(s)) {
2436 error_report("%s: postcopy failed to start", __func__);
2437 }
2438 return MIG_ITERATE_SKIP;
2439 }
2440 /* Just another iteration step */
2441 qemu_savevm_state_iterate(s->to_dst_file,
2442 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
2443 } else {
2444 trace_migration_thread_low_pending(pending_size);
2445 migration_completion(s);
2446 return MIG_ITERATE_BREAK;
2447 }
2448
2449 return MIG_ITERATE_RESUME;
2450}
2451
Peter Xu199aa6d2018-01-03 20:20:15 +08002452static void migration_iteration_finish(MigrationState *s)
2453{
2454 /* If we enabled cpu throttling for auto-converge, turn it off. */
2455 cpu_throttle_stop();
2456
2457 qemu_mutex_lock_iothread();
2458 switch (s->state) {
2459 case MIGRATION_STATUS_COMPLETED:
2460 migration_calculate_complete(s);
2461 runstate_set(RUN_STATE_POSTMIGRATE);
2462 break;
2463
2464 case MIGRATION_STATUS_ACTIVE:
2465 /*
2466 * We should really assert here, but since it's during
2467 * migration, let's try to reduce the usage of assertions.
2468 */
2469 if (!migrate_colo_enabled()) {
2470 error_report("%s: critical error: calling COLO code without "
2471 "COLO enabled", __func__);
2472 }
2473 migrate_start_colo_process(s);
2474 /*
2475 * Fixme: we will run VM in COLO no matter its old running state.
2476 * After exited COLO, we will keep running.
2477 */
2478 s->vm_was_running = true;
2479 /* Fallthrough */
2480 case MIGRATION_STATUS_FAILED:
2481 case MIGRATION_STATUS_CANCELLED:
2482 if (s->vm_was_running) {
2483 vm_start();
2484 } else {
2485 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
2486 runstate_set(RUN_STATE_POSTMIGRATE);
2487 }
2488 }
2489 break;
2490
2491 default:
2492 /* Should not reach here, but if so, forgive the VM. */
2493 error_report("%s: Unknown ending state %d", __func__, s->state);
2494 break;
2495 }
2496 qemu_bh_schedule(s->cleanup_bh);
2497 qemu_mutex_unlock_iothread();
2498}
2499
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002500/*
2501 * Master migration thread on the source VM.
2502 * It drives the migration and pumps the data down the outgoing channel.
2503 */
Juan Quintela5f496a12013-02-22 17:36:30 +01002504static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002505{
Juan Quintela9848a402012-12-19 09:55:50 +01002506 MigrationState *s = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +01002507 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Peter Xub23c2ad2018-05-02 18:47:19 +08002508 MigThrError thr_error;
Juan Quintela76f59332012-10-03 20:16:24 +02002509
Paolo Bonziniab28bd22015-07-09 08:55:38 +02002510 rcu_register_thread();
2511
Peter Xub15df1a2018-01-03 20:20:13 +08002512 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2513
zhanghailiang89a02a92016-01-15 11:37:42 +08002514 qemu_savevm_state_header(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002515
Peter Xu62a02652017-06-14 15:55:58 +08002516 /*
2517 * If we opened the return path, we need to make sure dst has it
2518 * opened as well.
2519 */
2520 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002521 /* Now tell the dest that it should open its end so it can reply */
zhanghailiang89a02a92016-01-15 11:37:42 +08002522 qemu_savevm_send_open_return_path(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002523
2524 /* And do a ping that will make stuff easier to debug */
zhanghailiang89a02a92016-01-15 11:37:42 +08002525 qemu_savevm_send_ping(s->to_dst_file, 1);
Peter Xu0425dc92017-05-31 18:35:34 +08002526 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002527
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002528 if (migrate_postcopy()) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002529 /*
2530 * Tell the destination that we *might* want to do postcopy later;
2531 * if the other end can't do postcopy it should fail now, nice and
2532 * early.
2533 */
zhanghailiang89a02a92016-01-15 11:37:42 +08002534 qemu_savevm_send_postcopy_advise(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002535 }
2536
Juan Quintela9907e842017-06-28 11:52:24 +02002537 qemu_savevm_state_setup(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002538
Alex Blighbc72ad62013-08-21 16:03:08 +01002539 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang48781e52015-12-16 11:47:33 +00002540 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2541 MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04002542
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00002543 trace_migration_thread_setup_complete();
2544
2545 while (s->state == MIGRATION_STATUS_ACTIVE ||
2546 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +01002547 int64_t current_time;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002548
zhanghailiang89a02a92016-01-15 11:37:42 +08002549 if (!qemu_file_rate_limit(s->to_dst_file)) {
Peter Xu2ad87302018-01-03 20:20:14 +08002550 MigIterateState iter_state = migration_iteration_run(s);
2551 if (iter_state == MIG_ITERATE_SKIP) {
2552 continue;
2553 } else if (iter_state == MIG_ITERATE_BREAK) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002554 break;
Juan Quintelac369f402012-10-03 20:33:34 +02002555 }
2556 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01002557
Peter Xub23c2ad2018-05-02 18:47:19 +08002558 /*
2559 * Try to detect any kind of failures, and see whether we
2560 * should stop the migration now.
2561 */
2562 thr_error = migration_detect_error(s);
2563 if (thr_error == MIG_THR_ERR_FATAL) {
2564 /* Stop migration */
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01002565 break;
Peter Xub23c2ad2018-05-02 18:47:19 +08002566 } else if (thr_error == MIG_THR_ERR_RECOVERED) {
2567 /*
2568 * Just recovered from a e.g. network failure, reset all
2569 * the local variables. This is important to avoid
2570 * breaking transferred_bytes and bandwidth calculation
2571 */
2572 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2573 s->iteration_initial_bytes = 0;
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01002574 }
Peter Xub15df1a2018-01-03 20:20:13 +08002575
Alex Blighbc72ad62013-08-21 16:03:08 +01002576 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002577
Peter Xub15df1a2018-01-03 20:20:13 +08002578 migration_update_counters(s, current_time);
Michael R. Hines7e114f82013-06-25 21:35:30 -04002579
zhanghailiang89a02a92016-01-15 11:37:42 +08002580 if (qemu_file_rate_limit(s->to_dst_file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002581 /* usleep expects microseconds */
Peter Xub15df1a2018-01-03 20:20:13 +08002582 g_usleep((s->iteration_start_time + BUFFER_DELAY -
2583 current_time) * 1000);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002584 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002585 }
2586
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002587 trace_migration_thread_after_loop();
Peter Xu199aa6d2018-01-03 20:20:15 +08002588 migration_iteration_finish(s);
Paolo Bonziniab28bd22015-07-09 08:55:38 +02002589 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002590 return NULL;
2591}
2592
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00002593void migrate_fd_connect(MigrationState *s, Error *error_in)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002594{
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302595 s->expected_downtime = s->parameters.downtime_limit;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01002596 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00002597 if (error_in) {
2598 migrate_fd_error(s, error_in);
2599 migrate_fd_cleanup(s);
2600 return;
2601 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002602
Daniel P. Berrange9e4d2b92016-04-27 11:04:57 +01002603 qemu_file_set_blocking(s->to_dst_file, true);
zhanghailiang89a02a92016-01-15 11:37:42 +08002604 qemu_file_set_rate_limit(s->to_dst_file,
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302605 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
Paolo Bonzini442773c2013-02-22 17:36:44 +01002606
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02002607 /* Notify before starting migration thread */
2608 notifier_list_notify(&migration_state_notifiers, s);
2609
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002610 /*
Peter Xuc788ada2017-06-26 18:28:55 +08002611 * Open the return path. For postcopy, it is used exclusively. For
2612 * precopy, only if user specified "return-path" capability would
2613 * QEMU uses the return path.
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002614 */
Peter Xuc788ada2017-06-26 18:28:55 +08002615 if (migrate_postcopy_ram() || migrate_use_return_path()) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002616 if (open_return_path_on_source(s)) {
2617 error_report("Unable to open return-path for postcopy");
zhanghailiang48781e52015-12-16 11:47:33 +00002618 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002619 MIGRATION_STATUS_FAILED);
2620 migrate_fd_cleanup(s);
2621 return;
2622 }
2623 }
2624
Juan Quintelaf986c3d2016-01-14 16:52:55 +01002625 if (multifd_save_setup() != 0) {
2626 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2627 MIGRATION_STATUS_FAILED);
2628 migrate_fd_cleanup(s);
2629 return;
2630 }
Pankaj Gupta009fad72017-01-23 19:12:56 +05302631 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01002632 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002633 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002634}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00002635
Peter Xu9d18af92017-06-27 12:10:19 +08002636void migration_global_dump(Monitor *mon)
2637{
2638 MigrationState *ms = migrate_get_current();
2639
Juan Quintela6f0f6422017-10-26 11:49:57 +02002640 monitor_printf(mon, "globals:\n");
2641 monitor_printf(mon, "store-global-state: %s\n",
2642 ms->store_global_state ? "on" : "off");
2643 monitor_printf(mon, "only-migratable: %s\n",
2644 ms->only_migratable ? "on" : "off");
2645 monitor_printf(mon, "send-configuration: %s\n",
2646 ms->send_configuration ? "on" : "off");
2647 monitor_printf(mon, "send-section-footer: %s\n",
2648 ms->send_section_footer ? "on" : "off");
Peter Xu9d18af92017-06-27 12:10:19 +08002649}
2650
Peter Xu20814752017-07-18 11:39:03 +08002651#define DEFINE_PROP_MIG_CAP(name, x) \
2652 DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
2653
Peter Xu52722982017-06-27 12:10:14 +08002654static Property migration_properties[] = {
2655 DEFINE_PROP_BOOL("store-global-state", MigrationState,
2656 store_global_state, true),
Peter Xu3df663e2017-06-27 12:10:15 +08002657 DEFINE_PROP_BOOL("only-migratable", MigrationState, only_migratable, false),
Peter Xu71dd4c12017-06-27 12:10:16 +08002658 DEFINE_PROP_BOOL("send-configuration", MigrationState,
2659 send_configuration, true),
Peter Xu15c38502017-06-27 12:10:17 +08002660 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
2661 send_section_footer, true),
Peter Xu89632fa2017-07-18 11:39:02 +08002662
2663 /* Migration parameters */
Juan Quintela741d4082017-12-01 13:08:38 +01002664 DEFINE_PROP_UINT8("x-compress-level", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002665 parameters.compress_level,
2666 DEFAULT_MIGRATE_COMPRESS_LEVEL),
Juan Quintela741d4082017-12-01 13:08:38 +01002667 DEFINE_PROP_UINT8("x-compress-threads", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002668 parameters.compress_threads,
2669 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
Juan Quintela741d4082017-12-01 13:08:38 +01002670 DEFINE_PROP_UINT8("x-decompress-threads", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002671 parameters.decompress_threads,
2672 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
Juan Quintela741d4082017-12-01 13:08:38 +01002673 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002674 parameters.cpu_throttle_initial,
2675 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
Juan Quintela741d4082017-12-01 13:08:38 +01002676 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002677 parameters.cpu_throttle_increment,
2678 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
Juan Quintela741d4082017-12-01 13:08:38 +01002679 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002680 parameters.max_bandwidth, MAX_THROTTLE),
Juan Quintela741d4082017-12-01 13:08:38 +01002681 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002682 parameters.downtime_limit,
2683 DEFAULT_MIGRATE_SET_DOWNTIME),
Juan Quintela741d4082017-12-01 13:08:38 +01002684 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002685 parameters.x_checkpoint_delay,
2686 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
Juan Quintela741d4082017-12-01 13:08:38 +01002687 DEFINE_PROP_UINT8("x-multifd-channels", MigrationState,
Juan Quintela4075fb12016-01-15 08:56:17 +01002688 parameters.x_multifd_channels,
2689 DEFAULT_MIGRATE_MULTIFD_CHANNELS),
Juan Quintela741d4082017-12-01 13:08:38 +01002690 DEFINE_PROP_UINT32("x-multifd-page-count", MigrationState,
Juan Quintela0fb86602017-04-27 10:48:25 +02002691 parameters.x_multifd_page_count,
2692 DEFAULT_MIGRATE_MULTIFD_PAGE_COUNT),
Juan Quintela73af8dd2017-10-05 21:30:10 +02002693 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
2694 parameters.xbzrle_cache_size,
2695 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
Peter Xu20814752017-07-18 11:39:03 +08002696
2697 /* Migration capabilities */
2698 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
2699 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
2700 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
2701 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
2702 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
2703 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
2704 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
2705 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
2706 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
2707 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
2708 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
Juan Quintela30126bb2016-01-14 12:23:00 +01002709 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_X_MULTIFD),
Peter Xu20814752017-07-18 11:39:03 +08002710
Peter Xu52722982017-06-27 12:10:14 +08002711 DEFINE_PROP_END_OF_LIST(),
2712};
2713
Peter Xue5cb7e72017-06-27 12:10:13 +08002714static void migration_class_init(ObjectClass *klass, void *data)
2715{
2716 DeviceClass *dc = DEVICE_CLASS(klass);
2717
2718 dc->user_creatable = false;
Peter Xu52722982017-06-27 12:10:14 +08002719 dc->props = migration_properties;
Peter Xue5cb7e72017-06-27 12:10:13 +08002720}
2721
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01002722static void migration_instance_finalize(Object *obj)
2723{
2724 MigrationState *ms = MIGRATION_OBJ(obj);
2725 MigrationParameters *params = &ms->parameters;
2726
Juan Quintela87db1a72017-09-05 12:50:22 +02002727 qemu_mutex_destroy(&ms->error_mutex);
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01002728 g_free(params->tls_hostname);
2729 g_free(params->tls_creds);
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002730 qemu_sem_destroy(&ms->pause_sem);
Peter Xub23c2ad2018-05-02 18:47:19 +08002731 qemu_sem_destroy(&ms->postcopy_pause_sem);
Peter Xu14b17422018-05-02 18:47:21 +08002732 qemu_sem_destroy(&ms->postcopy_pause_rp_sem);
Marc-André Lureauab105cc2018-03-06 18:09:59 +01002733 error_free(ms->error);
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01002734}
2735
Peter Xue5cb7e72017-06-27 12:10:13 +08002736static void migration_instance_init(Object *obj)
2737{
2738 MigrationState *ms = MIGRATION_OBJ(obj);
Peter Xu8b0b29d2017-07-18 11:39:06 +08002739 MigrationParameters *params = &ms->parameters;
Peter Xue5cb7e72017-06-27 12:10:13 +08002740
2741 ms->state = MIGRATION_STATUS_NONE;
Peter Xue5cb7e72017-06-27 12:10:13 +08002742 ms->mbps = -1;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002743 qemu_sem_init(&ms->pause_sem, 0);
Juan Quintela87db1a72017-09-05 12:50:22 +02002744 qemu_mutex_init(&ms->error_mutex);
Peter Xu8b0b29d2017-07-18 11:39:06 +08002745
2746 params->tls_hostname = g_strdup("");
2747 params->tls_creds = g_strdup("");
2748
2749 /* Set has_* up only for parameter checks */
2750 params->has_compress_level = true;
2751 params->has_compress_threads = true;
2752 params->has_decompress_threads = true;
2753 params->has_cpu_throttle_initial = true;
2754 params->has_cpu_throttle_increment = true;
2755 params->has_max_bandwidth = true;
2756 params->has_downtime_limit = true;
2757 params->has_x_checkpoint_delay = true;
2758 params->has_block_incremental = true;
Juan Quintela4075fb12016-01-15 08:56:17 +01002759 params->has_x_multifd_channels = true;
Juan Quintela0fb86602017-04-27 10:48:25 +02002760 params->has_x_multifd_page_count = true;
Juan Quintela73af8dd2017-10-05 21:30:10 +02002761 params->has_xbzrle_cache_size = true;
Peter Xub23c2ad2018-05-02 18:47:19 +08002762
2763 qemu_sem_init(&ms->postcopy_pause_sem, 0);
Peter Xu14b17422018-05-02 18:47:21 +08002764 qemu_sem_init(&ms->postcopy_pause_rp_sem, 0);
Peter Xu8b0b29d2017-07-18 11:39:06 +08002765}
2766
2767/*
2768 * Return true if check pass, false otherwise. Error will be put
2769 * inside errp if provided.
2770 */
2771static bool migration_object_check(MigrationState *ms, Error **errp)
2772{
Peter Xu6b19a7d2017-07-18 11:39:10 +08002773 MigrationCapabilityStatusList *head = NULL;
2774 /* Assuming all off */
2775 bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret;
2776 int i;
2777
Peter Xu8b0b29d2017-07-18 11:39:06 +08002778 if (!migrate_params_check(&ms->parameters, errp)) {
2779 return false;
2780 }
2781
Peter Xu6b19a7d2017-07-18 11:39:10 +08002782 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
2783 if (ms->enabled_capabilities[i]) {
2784 head = migrate_cap_add(head, i, true);
2785 }
2786 }
2787
2788 ret = migrate_caps_check(cap_list, head, errp);
2789
2790 /* It works with head == NULL */
2791 qapi_free_MigrationCapabilityStatusList(head);
2792
2793 return ret;
Peter Xue5cb7e72017-06-27 12:10:13 +08002794}
2795
2796static const TypeInfo migration_type = {
2797 .name = TYPE_MIGRATION,
Peter Xu01f6e142017-06-28 15:15:44 +08002798 /*
Peter Xuc8d3ff32017-07-05 16:21:23 +08002799 * NOTE: TYPE_MIGRATION is not really a device, as the object is
2800 * not created using qdev_create(), it is not attached to the qdev
2801 * device tree, and it is never realized.
2802 *
2803 * TODO: Make this TYPE_OBJECT once QOM provides something like
2804 * TYPE_DEVICE's "-global" properties.
Peter Xu01f6e142017-06-28 15:15:44 +08002805 */
Peter Xue5cb7e72017-06-27 12:10:13 +08002806 .parent = TYPE_DEVICE,
2807 .class_init = migration_class_init,
2808 .class_size = sizeof(MigrationClass),
2809 .instance_size = sizeof(MigrationState),
2810 .instance_init = migration_instance_init,
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01002811 .instance_finalize = migration_instance_finalize,
Peter Xue5cb7e72017-06-27 12:10:13 +08002812};
2813
2814static void register_migration_types(void)
2815{
2816 type_register_static(&migration_type);
2817}
2818
2819type_init(register_migration_types);