blob: 7c5e20b3f6049f25f0c0ac8aa0e0506d96c4cd2a [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) */
Peter Xua335deb2018-05-02 18:47:28 +080098 MIG_RP_MSG_RECV_BITMAP, /* send recved_bitmap back to source */
Juan Quintelada6f1792017-04-24 17:37:14 +020099
100 MIG_RP_MSG_MAX
101};
102
Juan Quintela17549e82011-10-05 13:50:43 +0200103/* When we add fault tolerance, we could have several
104 migrations at once. For now we don't need to add
105 dynamic creation of migration */
106
Peter Xue5cb7e72017-06-27 12:10:13 +0800107static MigrationState *current_migration;
108
Peter Xu8b0b29d2017-07-18 11:39:06 +0800109static bool migration_object_check(MigrationState *ms, Error **errp);
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +0100110static int migration_maybe_pause(MigrationState *s,
111 int *current_active_state,
112 int new_state);
Peter Xu8b0b29d2017-07-18 11:39:06 +0800113
Peter Xue5cb7e72017-06-27 12:10:13 +0800114void migration_object_init(void)
115{
Peter Xu4ffdb332017-06-27 12:10:18 +0800116 MachineState *ms = MACHINE(qdev_get_machine());
Peter Xu8b0b29d2017-07-18 11:39:06 +0800117 Error *err = NULL;
Peter Xu4ffdb332017-06-27 12:10:18 +0800118
Peter Xue5cb7e72017-06-27 12:10:13 +0800119 /* This can only be called once. */
120 assert(!current_migration);
121 current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION));
Peter Xu4ffdb332017-06-27 12:10:18 +0800122
Peter Xu8b0b29d2017-07-18 11:39:06 +0800123 if (!migration_object_check(current_migration, &err)) {
124 error_report_err(err);
125 exit(1);
126 }
127
Peter Xu4ffdb332017-06-27 12:10:18 +0800128 /*
129 * We cannot really do this in migration_instance_init() since at
130 * that time global properties are not yet applied, then this
131 * value will be definitely replaced by something else.
132 */
133 if (ms->enforce_config_section) {
134 current_migration->send_configuration = true;
135 }
Peter Xue5cb7e72017-06-27 12:10:13 +0800136}
137
Vladimir Sementsov-Ogievskiy1f895602017-12-28 12:16:16 +0300138void migration_object_finalize(void)
139{
140 object_unref(OBJECT(current_migration));
141}
142
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100143/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +0200144MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +0200145{
Peter Xue5cb7e72017-06-27 12:10:13 +0800146 /* This can only be called after the object created. */
147 assert(current_migration);
148 return current_migration;
Juan Quintela17549e82011-10-05 13:50:43 +0200149}
150
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100151MigrationIncomingState *migration_incoming_get_current(void)
152{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100153 static bool once;
154 static MigrationIncomingState mis_current;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100155
Juan Quintelab4b076d2017-01-23 22:32:06 +0100156 if (!once) {
157 mis_current.state = MIGRATION_STATUS_NONE;
158 memset(&mis_current, 0, sizeof(MigrationIncomingState));
Dr. David Alan Gilbert00fa4fc2018-03-12 17:21:04 +0000159 mis_current.postcopy_remote_fds = g_array_new(FALSE, TRUE,
160 sizeof(struct PostCopyFD));
Juan Quintelab4b076d2017-01-23 22:32:06 +0100161 qemu_mutex_init(&mis_current.rp_mutex);
162 qemu_event_init(&mis_current.main_thread_load_event, false);
Peter Xub411b842018-05-02 18:47:20 +0800163 qemu_sem_init(&mis_current.postcopy_pause_sem_dst, 0);
Peter Xu3a7804c2018-05-02 18:47:22 +0800164 qemu_sem_init(&mis_current.postcopy_pause_sem_fault, 0);
Vladimir Sementsov-Ogievskiyb35ebdf2018-03-13 15:34:01 -0400165
166 init_dirty_bitmap_incoming_migration();
167
Juan Quintelab4b076d2017-01-23 22:32:06 +0100168 once = true;
169 }
170 return &mis_current;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100171}
172
173void migration_incoming_state_destroy(void)
174{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100175 struct MigrationIncomingState *mis = migration_incoming_get_current();
176
Peter Xu34826552017-05-19 14:43:29 +0800177 if (mis->to_src_file) {
Peter Xu660819b2017-05-19 14:43:30 +0800178 /* Tell source that we are done */
179 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
Peter Xu34826552017-05-19 14:43:29 +0800180 qemu_fclose(mis->to_src_file);
181 mis->to_src_file = NULL;
182 }
183
Peter Xu660819b2017-05-19 14:43:30 +0800184 if (mis->from_src_file) {
185 qemu_fclose(mis->from_src_file);
186 mis->from_src_file = NULL;
187 }
Dr. David Alan Gilbert00fa4fc2018-03-12 17:21:04 +0000188 if (mis->postcopy_remote_fds) {
189 g_array_free(mis->postcopy_remote_fds, TRUE);
190 mis->postcopy_remote_fds = NULL;
191 }
Peter Xu660819b2017-05-19 14:43:30 +0800192
Dr. David Alan Gilbert5089e182017-08-25 15:19:39 +0100193 qemu_event_reset(&mis->main_thread_load_event);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100194}
195
Juan Quintelab05dc722015-07-07 14:44:05 +0200196static void migrate_generate_event(int new_state)
197{
198 if (migrate_use_events()) {
199 qapi_event_send_migration(new_state, &error_abort);
Juan Quintelab05dc722015-07-07 14:44:05 +0200200 }
201}
202
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000203/*
204 * Called on -incoming with a defer: uri.
205 * The migration can be started later after any parameters have been
206 * changed.
207 */
208static void deferred_incoming_migration(Error **errp)
209{
210 if (deferred_incoming) {
211 error_setg(errp, "Incoming migration already deferred");
212 }
213 deferred_incoming = true;
214}
215
Juan Quintelada6f1792017-04-24 17:37:14 +0200216/*
217 * Send a message on the return channel back to the source
218 * of the migration.
219 */
Peter Xud6208e32018-02-08 18:31:12 +0800220static int migrate_send_rp_message(MigrationIncomingState *mis,
221 enum mig_rp_message_type message_type,
222 uint16_t len, void *data)
Juan Quintelada6f1792017-04-24 17:37:14 +0200223{
Peter Xud6208e32018-02-08 18:31:12 +0800224 int ret = 0;
225
Juan Quintelada6f1792017-04-24 17:37:14 +0200226 trace_migrate_send_rp_message((int)message_type, len);
227 qemu_mutex_lock(&mis->rp_mutex);
Peter Xud6208e32018-02-08 18:31:12 +0800228
229 /*
230 * It's possible that the file handle got lost due to network
231 * failures.
232 */
233 if (!mis->to_src_file) {
234 ret = -EIO;
235 goto error;
236 }
237
Juan Quintelada6f1792017-04-24 17:37:14 +0200238 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
239 qemu_put_be16(mis->to_src_file, len);
240 qemu_put_buffer(mis->to_src_file, data, len);
241 qemu_fflush(mis->to_src_file);
Peter Xud6208e32018-02-08 18:31:12 +0800242
243 /* It's possible that qemu file got error during sending */
244 ret = qemu_file_get_error(mis->to_src_file);
245
246error:
Juan Quintelada6f1792017-04-24 17:37:14 +0200247 qemu_mutex_unlock(&mis->rp_mutex);
Peter Xud6208e32018-02-08 18:31:12 +0800248 return ret;
Juan Quintelada6f1792017-04-24 17:37:14 +0200249}
250
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000251/* Request a range of pages from the source VM at the given
252 * start address.
253 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
254 * as the last request (a name must have been given previously)
255 * Start: Address offset within the RB
256 * Len: Length in bytes required - must be a multiple of pagesize
257 */
Peter Xud6208e32018-02-08 18:31:12 +0800258int migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
259 ram_addr_t start, size_t len)
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000260{
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100261 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000262 size_t msglen = 12; /* start + len */
Peter Xud6208e32018-02-08 18:31:12 +0800263 enum mig_rp_message_type msg_type;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000264
265 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
266 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
267
268 if (rbname) {
269 int rbname_len = strlen(rbname);
270 assert(rbname_len < 256);
271
272 bufc[msglen++] = rbname_len;
273 memcpy(bufc + msglen, rbname, rbname_len);
274 msglen += rbname_len;
Peter Xud6208e32018-02-08 18:31:12 +0800275 msg_type = MIG_RP_MSG_REQ_PAGES_ID;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000276 } else {
Peter Xud6208e32018-02-08 18:31:12 +0800277 msg_type = MIG_RP_MSG_REQ_PAGES;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000278 }
Peter Xud6208e32018-02-08 18:31:12 +0800279
280 return migrate_send_rp_message(mis, msg_type, msglen, bufc);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000281}
282
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200283void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000284{
aliguori34c9dd82008-10-13 03:14:31 +0000285 const char *p;
286
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200287 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000288 if (!strcmp(uri, "defer")) {
289 deferred_incoming_migration(errp);
290 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200291 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400292#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000293 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400294 rdma_start_incoming_migration(p, errp);
295#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000296 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200297 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000298 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200299 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000300 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200301 fd_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000302 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100303 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200304 }
aliguori5bb79102008-10-13 03:12:02 +0000305}
306
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300307static void process_incoming_migration_bh(void *opaque)
308{
309 Error *local_err = NULL;
310 MigrationIncomingState *mis = opaque;
311
Dr. David Alan Gilberta18a73d2018-04-10 15:28:42 +0100312 /* Make sure all file formats flush their mutable metadata.
313 * If we get an error here, just don't restart the VM yet. */
314 bdrv_invalidate_cache_all(&local_err);
315 if (local_err) {
316 error_report_err(local_err);
317 local_err = NULL;
318 autostart = false;
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200319 }
320
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300321 /*
322 * This must happen after all error conditions are dealt with and
323 * we're sure the VM is going to be running on this host.
324 */
325 qemu_announce_self();
326
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100327 if (multifd_load_cleanup(&local_err) != 0) {
328 error_report_err(local_err);
329 autostart = false;
330 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300331 /* If global state section was not received or we are in running
332 state, we need to obey autostart. Any other state is set with
333 runstate_set. */
334
Vladimir Sementsov-Ogievskiyb35ebdf2018-03-13 15:34:01 -0400335 dirty_bitmap_mig_before_vm_start();
336
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300337 if (!global_state_received() ||
338 global_state_get_runstate() == RUN_STATE_RUNNING) {
339 if (autostart) {
340 vm_start();
341 } else {
342 runstate_set(RUN_STATE_PAUSED);
343 }
344 } else {
345 runstate_set(global_state_get_runstate());
346 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300347 /*
348 * This must happen after any state changes since as soon as an external
349 * observer sees this event they might start to prod at the VM assuming
350 * it's ready to use.
351 */
352 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
353 MIGRATION_STATUS_COMPLETED);
354 qemu_bh_delete(mis->bh);
355 migration_incoming_state_destroy();
356}
357
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200358static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200359{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100360 MigrationIncomingState *mis = migration_incoming_get_current();
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000361 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200362 int ret;
363
Juan Quintela4f0fae72017-07-24 12:42:02 +0200364 assert(mis->from_src_file);
Dr. David Alan Gilbert67f11b52017-02-24 18:28:34 +0000365 mis->largest_page_size = qemu_ram_pagesize_largest();
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000366 postcopy_state_set(POSTCOPY_INCOMING_NONE);
zhanghailiang93d7af62015-12-16 11:47:34 +0000367 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
368 MIGRATION_STATUS_ACTIVE);
Juan Quintela4f0fae72017-07-24 12:42:02 +0200369 ret = qemu_loadvm_state(mis->from_src_file);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100370
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000371 ps = postcopy_state_get();
372 trace_process_incoming_migration_co_end(ret, ps);
373 if (ps != POSTCOPY_INCOMING_NONE) {
374 if (ps == POSTCOPY_INCOMING_ADVISE) {
375 /*
376 * Where a migration had postcopy enabled (and thus went to advise)
377 * but managed to complete within the precopy period, we can use
378 * the normal exit.
379 */
380 postcopy_ram_incoming_cleanup(mis);
381 } else if (ret >= 0) {
382 /*
383 * Postcopy was started, cleanup should happen at the end of the
384 * postcopy thread.
385 */
386 trace_process_incoming_migration_co_postcopy_end_main();
387 return;
388 }
389 /* Else if something went wrong then just fall out of the normal exit */
390 }
391
zhanghailiang25d0c162016-10-27 14:42:55 +0800392 /* we get COLO info, and know if we are in COLO mode */
393 if (!ret && migration_incoming_enable_colo()) {
394 mis->migration_incoming_co = qemu_coroutine_self();
395 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
396 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
397 mis->have_colo_incoming_thread = true;
398 qemu_coroutine_yield();
399
400 /* Wait checkpoint incoming thread exit before free resource */
401 qemu_thread_join(&mis->colo_incoming_thread);
402 }
403
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200404 if (ret < 0) {
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100405 Error *local_err = NULL;
406
zhanghailiang93d7af62015-12-16 11:47:34 +0000407 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
408 MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200409 error_report("load of migration failed: %s", strerror(-ret));
Dr. David Alan Gilbert3a0f2ce2017-07-17 12:09:32 +0100410 qemu_fclose(mis->from_src_file);
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100411 if (multifd_load_cleanup(&local_err) != 0) {
412 error_report_err(local_err);
413 }
Eric Blake4aead692013-04-16 15:50:41 -0600414 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200415 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300416 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
417 qemu_bh_schedule(mis->bh);
Juan Quintela511c0232010-06-09 14:10:55 +0200418}
419
Juan Quintelae595a012017-07-17 12:30:25 +0200420static void migration_incoming_setup(QEMUFile *f)
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200421{
Juan Quintela4f0fae72017-07-24 12:42:02 +0200422 MigrationIncomingState *mis = migration_incoming_get_current();
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200423
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100424 if (multifd_load_setup() != 0) {
425 /* We haven't been able to create multifd threads
426 nothing better to do */
427 exit(EXIT_FAILURE);
428 }
429
Juan Quintela4f0fae72017-07-24 12:42:02 +0200430 if (!mis->from_src_file) {
431 mis->from_src_file = f;
432 }
Daniel P. Berrange06ad5132016-04-27 11:04:56 +0100433 qemu_file_set_blocking(f, false);
Juan Quintelae595a012017-07-17 12:30:25 +0200434}
435
Juan Quintela36c2f8b2018-03-07 08:40:52 +0100436void migration_incoming_process(void)
Juan Quintelae595a012017-07-17 12:30:25 +0200437{
438 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL);
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200439 qemu_coroutine_enter(co);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200440}
441
Juan Quintelae595a012017-07-17 12:30:25 +0200442void migration_fd_process_incoming(QEMUFile *f)
443{
Peter Xud96c9e82018-05-02 18:47:26 +0800444 MigrationIncomingState *mis = migration_incoming_get_current();
445
446 if (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
447 /* Resumed from a paused postcopy migration */
448
449 mis->from_src_file = f;
450 /* Postcopy has standalone thread to do vm load */
451 qemu_file_set_blocking(f, true);
452
453 /* Re-configure the return path */
454 mis->to_src_file = qemu_file_get_return_path(f);
455
456 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
457 MIGRATION_STATUS_POSTCOPY_RECOVER);
458
459 /*
460 * Here, we only wake up the main loading thread (while the
461 * fault thread will still be waiting), so that we can receive
462 * commands from source now, and answer it if needed. The
463 * fault thread will be woken up afterwards until we are sure
464 * that source is ready to reply to page requests.
465 */
466 qemu_sem_post(&mis->postcopy_pause_sem_dst);
467 } else {
468 /* New incoming migration */
469 migration_incoming_setup(f);
470 migration_incoming_process();
471 }
Juan Quintelae595a012017-07-17 12:30:25 +0200472}
473
Juan Quintela4f0fae72017-07-24 12:42:02 +0200474void migration_ioc_process_incoming(QIOChannel *ioc)
475{
476 MigrationIncomingState *mis = migration_incoming_get_current();
477
478 if (!mis->from_src_file) {
479 QEMUFile *f = qemu_fopen_channel_input(ioc);
Juan Quintela36c2f8b2018-03-07 08:40:52 +0100480 migration_incoming_setup(f);
Juan Quintela71bb07d2018-02-19 19:01:03 +0100481 return;
Juan Quintela4f0fae72017-07-24 12:42:02 +0200482 }
Juan Quintela71bb07d2018-02-19 19:01:03 +0100483 multifd_recv_new_channel(ioc);
Juan Quintela4f0fae72017-07-24 12:42:02 +0200484}
485
Juan Quintela428d8902017-07-24 13:06:25 +0200486/**
487 * @migration_has_all_channels: We have received all channels that we need
488 *
489 * Returns true when we have got connections to all the channels that
490 * we need for migration.
491 */
492bool migration_has_all_channels(void)
493{
Juan Quintela62c1e0c2018-02-19 18:59:02 +0100494 bool all_channels;
495
496 all_channels = multifd_recv_all_channels_created();
497
498 return all_channels;
Juan Quintela428d8902017-07-24 13:06:25 +0200499}
500
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000501/*
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000502 * Send a 'SHUT' message on the return channel with the given value
503 * to indicate that we've finished with the RP. Non-0 value indicates
504 * error.
505 */
506void migrate_send_rp_shut(MigrationIncomingState *mis,
507 uint32_t value)
508{
509 uint32_t buf;
510
511 buf = cpu_to_be32(value);
512 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
513}
514
515/*
516 * Send a 'PONG' message on the return channel with the given value
517 * (normally in response to a 'PING')
518 */
519void migrate_send_rp_pong(MigrationIncomingState *mis,
520 uint32_t value)
521{
522 uint32_t buf;
523
524 buf = cpu_to_be32(value);
525 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
526}
527
Peter Xua335deb2018-05-02 18:47:28 +0800528void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
529 char *block_name)
530{
531 char buf[512];
532 int len;
533 int64_t res;
534
535 /*
536 * First, we send the header part. It contains only the len of
537 * idstr, and the idstr itself.
538 */
539 len = strlen(block_name);
540 buf[0] = len;
541 memcpy(buf + 1, block_name, len);
542
543 if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
544 error_report("%s: MSG_RP_RECV_BITMAP only used for recovery",
545 __func__);
546 return;
547 }
548
549 migrate_send_rp_message(mis, MIG_RP_MSG_RECV_BITMAP, len + 1, buf);
550
551 /*
552 * Next, we dump the received bitmap to the stream.
553 *
554 * TODO: currently we are safe since we are the only one that is
555 * using the to_src_file handle (fault thread is still paused),
556 * and it's ok even not taking the mutex. However the best way is
557 * to take the lock before sending the message header, and release
558 * the lock after sending the bitmap.
559 */
560 qemu_mutex_lock(&mis->rp_mutex);
561 res = ramblock_recv_bitmap_send(mis->to_src_file, block_name);
562 qemu_mutex_unlock(&mis->rp_mutex);
563
564 trace_migrate_send_rp_recv_bitmap(block_name, res);
565}
566
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300567MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
568{
569 MigrationCapabilityStatusList *head = NULL;
570 MigrationCapabilityStatusList *caps;
571 MigrationState *s = migrate_get_current();
572 int i;
573
Michael Tokarev387eede2013-10-05 13:18:28 +0400574 caps = NULL; /* silence compiler warning */
Eric Blake7fb1cf12015-11-18 01:52:57 -0700575 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Dr. David Alan Gilberted1701c2017-05-15 15:05:29 +0100576#ifndef CONFIG_LIVE_BLOCK_MIGRATION
577 if (i == MIGRATION_CAPABILITY_BLOCK) {
578 continue;
579 }
580#endif
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300581 if (head == NULL) {
582 head = g_malloc0(sizeof(*caps));
583 caps = head;
584 } else {
585 caps->next = g_malloc0(sizeof(*caps));
586 caps = caps->next;
587 }
588 caps->value =
589 g_malloc(sizeof(*caps->value));
590 caps->value->capability = i;
591 caps->value->state = s->enabled_capabilities[i];
592 }
593
594 return head;
595}
596
Liang Li85de8322015-03-23 16:32:28 +0800597MigrationParameters *qmp_query_migrate_parameters(Error **errp)
598{
599 MigrationParameters *params;
600 MigrationState *s = migrate_get_current();
601
Markus Armbrustere87fae42017-07-18 12:57:38 +0200602 /* TODO use QAPI_CLONE() instead of duplicating it inline */
Liang Li85de8322015-03-23 16:32:28 +0800603 params = g_malloc0(sizeof(*params));
Eric Blakede63ab62016-09-08 22:14:15 -0500604 params->has_compress_level = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100605 params->compress_level = s->parameters.compress_level;
Eric Blakede63ab62016-09-08 22:14:15 -0500606 params->has_compress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100607 params->compress_threads = s->parameters.compress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500608 params->has_decompress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100609 params->decompress_threads = s->parameters.decompress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500610 params->has_cpu_throttle_initial = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100611 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
Eric Blakede63ab62016-09-08 22:14:15 -0500612 params->has_cpu_throttle_increment = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100613 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200614 params->has_tls_creds = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100615 params->tls_creds = g_strdup(s->parameters.tls_creds);
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200616 params->has_tls_hostname = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100617 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530618 params->has_max_bandwidth = true;
619 params->max_bandwidth = s->parameters.max_bandwidth;
620 params->has_downtime_limit = true;
621 params->downtime_limit = s->parameters.downtime_limit;
zhanghailiangfe39a4d2016-11-02 15:42:09 +0800622 params->has_x_checkpoint_delay = true;
zhanghailiang68b53592016-10-27 14:43:01 +0800623 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
Juan Quintela2833c592017-04-05 18:32:37 +0200624 params->has_block_incremental = true;
625 params->block_incremental = s->parameters.block_incremental;
Juan Quintela4075fb12016-01-15 08:56:17 +0100626 params->has_x_multifd_channels = true;
627 params->x_multifd_channels = s->parameters.x_multifd_channels;
Juan Quintela0fb86602017-04-27 10:48:25 +0200628 params->has_x_multifd_page_count = true;
629 params->x_multifd_page_count = s->parameters.x_multifd_page_count;
Juan Quintela73af8dd2017-10-05 21:30:10 +0200630 params->has_xbzrle_cache_size = true;
631 params->xbzrle_cache_size = s->parameters.xbzrle_cache_size;
Liang Li85de8322015-03-23 16:32:28 +0800632
633 return params;
634}
635
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000636/*
637 * Return true if we're already in the middle of a migration
638 * (i.e. any of the active or setup states)
639 */
640static bool migration_is_setup_or_active(int state)
641{
642 switch (state) {
643 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000644 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Peter Xua688d2c2018-05-02 18:47:18 +0800645 case MIGRATION_STATUS_POSTCOPY_PAUSED:
Peter Xu135b87b2018-05-02 18:47:25 +0800646 case MIGRATION_STATUS_POSTCOPY_RECOVER:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000647 case MIGRATION_STATUS_SETUP:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +0100648 case MIGRATION_STATUS_PRE_SWITCHOVER:
649 case MIGRATION_STATUS_DEVICE:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000650 return true;
651
652 default:
653 return false;
654
655 }
656}
657
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100658static void populate_ram_info(MigrationInfo *info, MigrationState *s)
659{
660 info->has_ram = true;
661 info->ram = g_malloc0(sizeof(*info->ram));
Juan Quintela93604472017-06-06 19:49:03 +0200662 info->ram->transferred = ram_counters.transferred;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100663 info->ram->total = ram_bytes_total();
Juan Quintela93604472017-06-06 19:49:03 +0200664 info->ram->duplicate = ram_counters.duplicate;
Juan Quintelabedf53c2017-03-13 20:35:54 +0100665 /* legacy value. It is not used anymore */
666 info->ram->skipped = 0;
Juan Quintela93604472017-06-06 19:49:03 +0200667 info->ram->normal = ram_counters.normal;
668 info->ram->normal_bytes = ram_counters.normal *
Juan Quintela20afaed2017-03-21 09:09:14 +0100669 qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100670 info->ram->mbps = s->mbps;
Juan Quintela93604472017-06-06 19:49:03 +0200671 info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
672 info->ram->postcopy_requests = ram_counters.postcopy_requests;
Chao Fan030ce1f2017-03-21 10:22:43 +0800673 info->ram->page_size = qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100674
Juan Quintela114f5ae2017-05-04 10:09:21 +0200675 if (migrate_use_xbzrle()) {
676 info->has_xbzrle_cache = true;
677 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
678 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
Juan Quintela93604472017-06-06 19:49:03 +0200679 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
680 info->xbzrle_cache->pages = xbzrle_counters.pages;
681 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
682 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
683 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
Juan Quintela114f5ae2017-05-04 10:09:21 +0200684 }
685
Juan Quintela338182c2017-05-03 13:16:38 +0200686 if (cpu_throttle_active()) {
687 info->has_cpu_throttle_percentage = true;
688 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
689 }
690
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100691 if (s->state != MIGRATION_STATUS_COMPLETED) {
692 info->ram->remaining = ram_bytes_remaining();
Juan Quintela93604472017-06-06 19:49:03 +0200693 info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100694 }
695}
696
Juan Quintela930ac042017-05-04 10:21:46 +0200697static void populate_disk_info(MigrationInfo *info)
698{
699 if (blk_mig_active()) {
700 info->has_disk = true;
701 info->disk = g_malloc0(sizeof(*info->disk));
702 info->disk->transferred = blk_mig_bytes_transferred();
703 info->disk->remaining = blk_mig_bytes_remaining();
704 info->disk->total = blk_mig_bytes_total();
705 }
706}
707
Alexey Perevalov65ace062018-03-22 21:17:27 +0300708static void fill_source_migration_info(MigrationInfo *info)
aliguori5bb79102008-10-13 03:12:02 +0000709{
Juan Quintela17549e82011-10-05 13:50:43 +0200710 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000711
Juan Quintela17549e82011-10-05 13:50:43 +0200712 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800713 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200714 /* no migration has happened ever */
Alexey Perevalov65ace062018-03-22 21:17:27 +0300715 /* do not overwrite destination migration status */
716 return;
Juan Quintela17549e82011-10-05 13:50:43 +0200717 break;
zhanghailiang31194732015-03-13 16:08:38 +0800718 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400719 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400720 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400721 break;
zhanghailiang31194732015-03-13 16:08:38 +0800722 case MIGRATION_STATUS_ACTIVE:
723 case MIGRATION_STATUS_CANCELLING:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000724 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +0100725 case MIGRATION_STATUS_PRE_SWITCHOVER:
726 case MIGRATION_STATUS_DEVICE:
Peter Xua688d2c2018-05-02 18:47:18 +0800727 case MIGRATION_STATUS_POSTCOPY_PAUSED:
Peter Xu135b87b2018-05-02 18:47:25 +0800728 case MIGRATION_STATUS_POSTCOPY_RECOVER:
Juan Quintelac8f9f4f2017-06-06 19:21:29 +0200729 /* TODO add some postcopy stats */
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000730 info->has_status = true;
731 info->has_total_time = true;
732 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Peter Xu4af246a2018-01-03 20:20:08 +0800733 - s->start_time;
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000734 info->has_expected_downtime = true;
735 info->expected_downtime = s->expected_downtime;
736 info->has_setup_time = true;
737 info->setup_time = s->setup_time;
738
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100739 populate_ram_info(info, s);
Juan Quintela930ac042017-05-04 10:21:46 +0200740 populate_disk_info(info);
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000741 break;
zhanghailiang0b827d52016-10-27 14:42:54 +0800742 case MIGRATION_STATUS_COLO:
743 info->has_status = true;
744 /* TODO: display COLO specific information (checkpoint info etc.) */
745 break;
zhanghailiang31194732015-03-13 16:08:38 +0800746 case MIGRATION_STATUS_COMPLETED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300747 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900748 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200749 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200750 info->has_downtime = true;
751 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400752 info->has_setup_time = true;
753 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200754
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100755 populate_ram_info(info, s);
Juan Quintela17549e82011-10-05 13:50:43 +0200756 break;
zhanghailiang31194732015-03-13 16:08:38 +0800757 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300758 info->has_status = true;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100759 if (s->error) {
760 info->has_error_desc = true;
761 info->error_desc = g_strdup(error_get_pretty(s->error));
762 }
Juan Quintela17549e82011-10-05 13:50:43 +0200763 break;
zhanghailiang31194732015-03-13 16:08:38 +0800764 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300765 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200766 break;
aliguori5bb79102008-10-13 03:12:02 +0000767 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800768 info->status = s->state;
aliguori5bb79102008-10-13 03:12:02 +0000769}
770
Peter Xu4a842142017-07-18 11:39:08 +0800771/**
772 * @migration_caps_check - check capability validity
773 *
774 * @cap_list: old capability list, array of bool
775 * @params: new capabilities to be applied soon
776 * @errp: set *errp if the check failed, with reason
777 *
778 * Returns true if check passed, otherwise false.
779 */
780static bool migrate_caps_check(bool *cap_list,
781 MigrationCapabilityStatusList *params,
782 Error **errp)
Orit Wasserman00458432012-08-06 21:42:48 +0300783{
Orit Wasserman00458432012-08-06 21:42:48 +0300784 MigrationCapabilityStatusList *cap;
Peter Xu4a842142017-07-18 11:39:08 +0800785 bool old_postcopy_cap;
Alexey Perevalovd7651f12017-09-19 19:47:56 +0300786 MigrationIncomingState *mis = migration_incoming_get_current();
Orit Wasserman00458432012-08-06 21:42:48 +0300787
Peter Xu4a842142017-07-18 11:39:08 +0800788 old_postcopy_cap = cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Orit Wasserman00458432012-08-06 21:42:48 +0300789
790 for (cap = params; cap; cap = cap->next) {
Peter Xu4a842142017-07-18 11:39:08 +0800791 cap_list[cap->value->capability] = cap->value->state;
Orit Wasserman00458432012-08-06 21:42:48 +0300792 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000793
Peter Xu4a842142017-07-18 11:39:08 +0800794#ifndef CONFIG_LIVE_BLOCK_MIGRATION
795 if (cap_list[MIGRATION_CAPABILITY_BLOCK]) {
796 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
797 "block migration");
798 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
799 return false;
800 }
801#endif
802
803 if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
804 if (cap_list[MIGRATION_CAPABILITY_COMPRESS]) {
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000805 /* The decompression threads asynchronously write into RAM
806 * rather than use the atomic copies needed to avoid
807 * userfaulting. It should be possible to fix the decompression
808 * threads for compatibility in future.
809 */
Peter Xu4a842142017-07-18 11:39:08 +0800810 error_setg(errp, "Postcopy is not currently compatible "
811 "with compression");
812 return false;
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000813 }
Peter Xu4a842142017-07-18 11:39:08 +0800814
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100815 /* This check is reasonably expensive, so only when it's being
816 * set the first time, also it's only the destination that needs
817 * special support.
818 */
819 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
Alexey Perevalovd7651f12017-09-19 19:47:56 +0300820 !postcopy_ram_supported_by_host(mis)) {
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100821 /* postcopy_ram_supported_by_host will have emitted a more
822 * detailed message
823 */
Peter Xu4a842142017-07-18 11:39:08 +0800824 error_setg(errp, "Postcopy is not supported");
825 return false;
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100826 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000827 }
Peter Xu4a842142017-07-18 11:39:08 +0800828
829 return true;
830}
831
Alexey Perevalov65ace062018-03-22 21:17:27 +0300832static void fill_destination_migration_info(MigrationInfo *info)
833{
834 MigrationIncomingState *mis = migration_incoming_get_current();
835
836 switch (mis->state) {
837 case MIGRATION_STATUS_NONE:
838 return;
839 break;
840 case MIGRATION_STATUS_SETUP:
841 case MIGRATION_STATUS_CANCELLING:
842 case MIGRATION_STATUS_CANCELLED:
843 case MIGRATION_STATUS_ACTIVE:
844 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
845 case MIGRATION_STATUS_FAILED:
846 case MIGRATION_STATUS_COLO:
847 info->has_status = true;
848 break;
849 case MIGRATION_STATUS_COMPLETED:
850 info->has_status = true;
851 fill_destination_postcopy_migration_info(info);
852 break;
853 }
854 info->status = mis->state;
855}
856
857MigrationInfo *qmp_query_migrate(Error **errp)
858{
859 MigrationInfo *info = g_malloc0(sizeof(*info));
860
861 fill_destination_migration_info(info);
862 fill_source_migration_info(info);
863
864 return info;
865}
866
Peter Xu4a842142017-07-18 11:39:08 +0800867void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
868 Error **errp)
869{
870 MigrationState *s = migrate_get_current();
871 MigrationCapabilityStatusList *cap;
Peter Xudd0ee302018-03-05 17:49:38 +0800872 bool cap_list[MIGRATION_CAPABILITY__MAX];
Peter Xu4a842142017-07-18 11:39:08 +0800873
874 if (migration_is_setup_or_active(s->state)) {
875 error_setg(errp, QERR_MIGRATION_ACTIVE);
876 return;
877 }
878
Peter Xudd0ee302018-03-05 17:49:38 +0800879 memcpy(cap_list, s->enabled_capabilities, sizeof(cap_list));
880 if (!migrate_caps_check(cap_list, params, errp)) {
Peter Xu4a842142017-07-18 11:39:08 +0800881 return;
882 }
883
884 for (cap = params; cap; cap = cap->next) {
885 s->enabled_capabilities[cap->value->capability] = cap->value->state;
886 }
Orit Wasserman00458432012-08-06 21:42:48 +0300887}
888
Peter Xu16d063b2017-07-18 11:39:04 +0800889/*
890 * Check whether the parameters are valid. Error will be put into errp
891 * (if provided). Return true if valid, otherwise false.
892 */
893static bool migrate_params_check(MigrationParameters *params, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +0800894{
Eric Blake7f375e02016-09-08 22:14:16 -0500895 if (params->has_compress_level &&
Juan Quintela741d4082017-12-01 13:08:38 +0100896 (params->compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100897 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
898 "is invalid, it should be in the range of 0 to 9");
Peter Xu16d063b2017-07-18 11:39:04 +0800899 return false;
Liang Li85de8322015-03-23 16:32:28 +0800900 }
Peter Xu16d063b2017-07-18 11:39:04 +0800901
Juan Quintela741d4082017-12-01 13:08:38 +0100902 if (params->has_compress_threads && (params->compress_threads < 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100903 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
904 "compress_threads",
905 "is invalid, it should be in the range of 1 to 255");
Peter Xu16d063b2017-07-18 11:39:04 +0800906 return false;
Liang Li85de8322015-03-23 16:32:28 +0800907 }
Peter Xu16d063b2017-07-18 11:39:04 +0800908
Juan Quintela741d4082017-12-01 13:08:38 +0100909 if (params->has_decompress_threads && (params->decompress_threads < 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100910 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
911 "decompress_threads",
912 "is invalid, it should be in the range of 1 to 255");
Peter Xu16d063b2017-07-18 11:39:04 +0800913 return false;
Liang Li85de8322015-03-23 16:32:28 +0800914 }
Peter Xu16d063b2017-07-18 11:39:04 +0800915
Eric Blake7f375e02016-09-08 22:14:16 -0500916 if (params->has_cpu_throttle_initial &&
917 (params->cpu_throttle_initial < 1 ||
918 params->cpu_throttle_initial > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400919 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400920 "cpu_throttle_initial",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400921 "an integer in the range of 1 to 99");
Peter Xu16d063b2017-07-18 11:39:04 +0800922 return false;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400923 }
Peter Xu16d063b2017-07-18 11:39:04 +0800924
Eric Blake7f375e02016-09-08 22:14:16 -0500925 if (params->has_cpu_throttle_increment &&
926 (params->cpu_throttle_increment < 1 ||
927 params->cpu_throttle_increment > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400928 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400929 "cpu_throttle_increment",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400930 "an integer in the range of 1 to 99");
Peter Xu16d063b2017-07-18 11:39:04 +0800931 return false;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400932 }
Peter Xu16d063b2017-07-18 11:39:04 +0800933
Juan Quintela741d4082017-12-01 13:08:38 +0100934 if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530935 error_setg(errp, "Parameter 'max_bandwidth' expects an integer in the"
936 " range of 0 to %zu bytes/second", SIZE_MAX);
Peter Xu16d063b2017-07-18 11:39:04 +0800937 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530938 }
Peter Xu16d063b2017-07-18 11:39:04 +0800939
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530940 if (params->has_downtime_limit &&
Juan Quintela741d4082017-12-01 13:08:38 +0100941 (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -0300942 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
943 "the range of 0 to %d milliseconds",
944 MAX_MIGRATE_DOWNTIME);
Peter Xu16d063b2017-07-18 11:39:04 +0800945 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530946 }
Peter Xu16d063b2017-07-18 11:39:04 +0800947
Juan Quintela741d4082017-12-01 13:08:38 +0100948 /* x_checkpoint_delay is now always positive */
949
950 if (params->has_x_multifd_channels && (params->x_multifd_channels < 1)) {
Juan Quintela4075fb12016-01-15 08:56:17 +0100951 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
952 "multifd_channels",
953 "is invalid, it should be in the range of 1 to 255");
954 return false;
955 }
Juan Quintela0fb86602017-04-27 10:48:25 +0200956 if (params->has_x_multifd_page_count &&
Juan Quintela741d4082017-12-01 13:08:38 +0100957 (params->x_multifd_page_count < 1 ||
958 params->x_multifd_page_count > 10000)) {
Juan Quintela0fb86602017-04-27 10:48:25 +0200959 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
960 "multifd_page_count",
961 "is invalid, it should be in the range of 1 to 10000");
962 return false;
963 }
Peter Xu16d063b2017-07-18 11:39:04 +0800964
Juan Quintela73af8dd2017-10-05 21:30:10 +0200965 if (params->has_xbzrle_cache_size &&
966 (params->xbzrle_cache_size < qemu_target_page_size() ||
967 !is_power_of_2(params->xbzrle_cache_size))) {
968 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
969 "xbzrle_cache_size",
970 "is invalid, it should be bigger than target page size"
971 " and a power of two");
972 return false;
973 }
974
Peter Xu16d063b2017-07-18 11:39:04 +0800975 return true;
976}
977
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200978static void migrate_params_test_apply(MigrateSetParameters *params,
979 MigrationParameters *dest)
980{
981 *dest = migrate_get_current()->parameters;
982
983 /* TODO use QAPI_CLONE() instead of duplicating it inline */
984
985 if (params->has_compress_level) {
986 dest->compress_level = params->compress_level;
987 }
988
989 if (params->has_compress_threads) {
990 dest->compress_threads = params->compress_threads;
991 }
992
993 if (params->has_decompress_threads) {
994 dest->decompress_threads = params->decompress_threads;
995 }
996
997 if (params->has_cpu_throttle_initial) {
998 dest->cpu_throttle_initial = params->cpu_throttle_initial;
999 }
1000
1001 if (params->has_cpu_throttle_increment) {
1002 dest->cpu_throttle_increment = params->cpu_throttle_increment;
1003 }
1004
1005 if (params->has_tls_creds) {
Markus Armbruster01fa5592017-07-18 14:42:04 +02001006 assert(params->tls_creds->type == QTYPE_QSTRING);
1007 dest->tls_creds = g_strdup(params->tls_creds->u.s);
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001008 }
1009
1010 if (params->has_tls_hostname) {
Markus Armbruster01fa5592017-07-18 14:42:04 +02001011 assert(params->tls_hostname->type == QTYPE_QSTRING);
1012 dest->tls_hostname = g_strdup(params->tls_hostname->u.s);
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001013 }
1014
1015 if (params->has_max_bandwidth) {
1016 dest->max_bandwidth = params->max_bandwidth;
1017 }
1018
1019 if (params->has_downtime_limit) {
1020 dest->downtime_limit = params->downtime_limit;
1021 }
1022
1023 if (params->has_x_checkpoint_delay) {
1024 dest->x_checkpoint_delay = params->x_checkpoint_delay;
1025 }
1026
1027 if (params->has_block_incremental) {
1028 dest->block_incremental = params->block_incremental;
1029 }
Juan Quintela5e7577a2017-10-09 18:07:56 +02001030 if (params->has_x_multifd_channels) {
1031 dest->x_multifd_channels = params->x_multifd_channels;
1032 }
1033 if (params->has_x_multifd_page_count) {
1034 dest->x_multifd_page_count = params->x_multifd_page_count;
1035 }
Juan Quintela73af8dd2017-10-05 21:30:10 +02001036 if (params->has_xbzrle_cache_size) {
1037 dest->xbzrle_cache_size = params->xbzrle_cache_size;
1038 }
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001039}
1040
Juan Quintela73af8dd2017-10-05 21:30:10 +02001041static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
Peter Xu16d063b2017-07-18 11:39:04 +08001042{
1043 MigrationState *s = migrate_get_current();
1044
Markus Armbrustere87fae42017-07-18 12:57:38 +02001045 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1046
Eric Blake7f375e02016-09-08 22:14:16 -05001047 if (params->has_compress_level) {
1048 s->parameters.compress_level = params->compress_level;
Liang Li85de8322015-03-23 16:32:28 +08001049 }
Peter Xu476c72a2017-07-18 11:39:05 +08001050
Eric Blake7f375e02016-09-08 22:14:16 -05001051 if (params->has_compress_threads) {
1052 s->parameters.compress_threads = params->compress_threads;
Liang Li85de8322015-03-23 16:32:28 +08001053 }
Peter Xu476c72a2017-07-18 11:39:05 +08001054
Eric Blake7f375e02016-09-08 22:14:16 -05001055 if (params->has_decompress_threads) {
1056 s->parameters.decompress_threads = params->decompress_threads;
Liang Li85de8322015-03-23 16:32:28 +08001057 }
Peter Xu476c72a2017-07-18 11:39:05 +08001058
Eric Blake7f375e02016-09-08 22:14:16 -05001059 if (params->has_cpu_throttle_initial) {
1060 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001061 }
Peter Xu476c72a2017-07-18 11:39:05 +08001062
Eric Blake7f375e02016-09-08 22:14:16 -05001063 if (params->has_cpu_throttle_increment) {
1064 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001065 }
Peter Xu476c72a2017-07-18 11:39:05 +08001066
Eric Blake7f375e02016-09-08 22:14:16 -05001067 if (params->has_tls_creds) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001068 g_free(s->parameters.tls_creds);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001069 assert(params->tls_creds->type == QTYPE_QSTRING);
1070 s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001071 }
Peter Xu476c72a2017-07-18 11:39:05 +08001072
Eric Blake7f375e02016-09-08 22:14:16 -05001073 if (params->has_tls_hostname) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001074 g_free(s->parameters.tls_hostname);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001075 assert(params->tls_hostname->type == QTYPE_QSTRING);
1076 s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001077 }
Peter Xu476c72a2017-07-18 11:39:05 +08001078
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301079 if (params->has_max_bandwidth) {
1080 s->parameters.max_bandwidth = params->max_bandwidth;
1081 if (s->to_dst_file) {
1082 qemu_file_set_rate_limit(s->to_dst_file,
1083 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
1084 }
1085 }
Peter Xu476c72a2017-07-18 11:39:05 +08001086
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301087 if (params->has_downtime_limit) {
1088 s->parameters.downtime_limit = params->downtime_limit;
1089 }
zhanghailiang68b53592016-10-27 14:43:01 +08001090
1091 if (params->has_x_checkpoint_delay) {
1092 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
zhanghailiang479125d2017-01-17 20:57:42 +08001093 if (migration_in_colo_state()) {
1094 colo_checkpoint_notify(s);
1095 }
zhanghailiang68b53592016-10-27 14:43:01 +08001096 }
Peter Xu476c72a2017-07-18 11:39:05 +08001097
Juan Quintela2833c592017-04-05 18:32:37 +02001098 if (params->has_block_incremental) {
1099 s->parameters.block_incremental = params->block_incremental;
1100 }
Juan Quintela4075fb12016-01-15 08:56:17 +01001101 if (params->has_x_multifd_channels) {
1102 s->parameters.x_multifd_channels = params->x_multifd_channels;
1103 }
Juan Quintela0fb86602017-04-27 10:48:25 +02001104 if (params->has_x_multifd_page_count) {
1105 s->parameters.x_multifd_page_count = params->x_multifd_page_count;
1106 }
Juan Quintela73af8dd2017-10-05 21:30:10 +02001107 if (params->has_xbzrle_cache_size) {
1108 s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
1109 xbzrle_cache_resize(params->xbzrle_cache_size, errp);
1110 }
Liang Li85de8322015-03-23 16:32:28 +08001111}
1112
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001113void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
Peter Xu476c72a2017-07-18 11:39:05 +08001114{
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001115 MigrationParameters tmp;
1116
Markus Armbruster01fa5592017-07-18 14:42:04 +02001117 /* TODO Rewrite "" to null instead */
1118 if (params->has_tls_creds
1119 && params->tls_creds->type == QTYPE_QNULL) {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001120 qobject_unref(params->tls_creds->u.n);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001121 params->tls_creds->type = QTYPE_QSTRING;
1122 params->tls_creds->u.s = strdup("");
1123 }
1124 /* TODO Rewrite "" to null instead */
1125 if (params->has_tls_hostname
1126 && params->tls_hostname->type == QTYPE_QNULL) {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001127 qobject_unref(params->tls_hostname->u.n);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001128 params->tls_hostname->type = QTYPE_QSTRING;
1129 params->tls_hostname->u.s = strdup("");
1130 }
1131
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001132 migrate_params_test_apply(params, &tmp);
1133
1134 if (!migrate_params_check(&tmp, errp)) {
Peter Xu476c72a2017-07-18 11:39:05 +08001135 /* Invalid parameter */
1136 return;
1137 }
1138
Juan Quintela73af8dd2017-10-05 21:30:10 +02001139 migrate_params_apply(params, errp);
Peter Xu476c72a2017-07-18 11:39:05 +08001140}
1141
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001142
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001143void qmp_migrate_start_postcopy(Error **errp)
1144{
1145 MigrationState *s = migrate_get_current();
1146
Vladimir Sementsov-Ogievskiy16b0fd32018-03-13 15:34:01 -04001147 if (!migrate_postcopy()) {
Dr. David Alan Gilberta54d3402015-11-12 11:34:44 +00001148 error_setg(errp, "Enable postcopy with migrate_set_capability before"
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001149 " the start of migration");
1150 return;
1151 }
1152
1153 if (s->state == MIGRATION_STATUS_NONE) {
1154 error_setg(errp, "Postcopy must be started after migration has been"
1155 " started");
1156 return;
1157 }
1158 /*
1159 * we don't error if migration has finished since that would be racy
1160 * with issuing this command.
1161 */
1162 atomic_set(&s->start_postcopy, true);
1163}
1164
aliguori065e2812008-11-11 16:46:33 +00001165/* shared migration helpers */
1166
zhanghailiang48781e52015-12-16 11:47:33 +00001167void migrate_set_state(int *state, int old_state, int new_state)
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +00001168{
Peter Xua31fede2017-08-30 16:32:01 +08001169 assert(new_state < MIGRATION_STATUS__MAX);
zhanghailiang48781e52015-12-16 11:47:33 +00001170 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
Peter Xua31fede2017-08-30 16:32:01 +08001171 trace_migrate_set_state(MigrationStatus_str(new_state));
Juan Quintelab05dc722015-07-07 14:44:05 +02001172 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +00001173 }
1174}
1175
Peter Xu4e4a3d32017-07-18 11:39:09 +08001176static MigrationCapabilityStatusList *migrate_cap_add(
1177 MigrationCapabilityStatusList *list,
1178 MigrationCapability index,
1179 bool state)
Juan Quintela2833c592017-04-05 18:32:37 +02001180{
1181 MigrationCapabilityStatusList *cap;
1182
1183 cap = g_new0(MigrationCapabilityStatusList, 1);
1184 cap->value = g_new0(MigrationCapabilityStatus, 1);
Peter Xu4e4a3d32017-07-18 11:39:09 +08001185 cap->value->capability = index;
1186 cap->value->state = state;
1187 cap->next = list;
1188
1189 return cap;
1190}
1191
1192void migrate_set_block_enabled(bool value, Error **errp)
1193{
1194 MigrationCapabilityStatusList *cap;
1195
1196 cap = migrate_cap_add(NULL, MIGRATION_CAPABILITY_BLOCK, value);
Juan Quintela2833c592017-04-05 18:32:37 +02001197 qmp_migrate_set_capabilities(cap, errp);
1198 qapi_free_MigrationCapabilityStatusList(cap);
1199}
1200
1201static void migrate_set_block_incremental(MigrationState *s, bool value)
1202{
1203 s->parameters.block_incremental = value;
1204}
1205
1206static void block_cleanup_parameters(MigrationState *s)
1207{
1208 if (s->must_remove_block_options) {
1209 /* setting to false can never fail */
1210 migrate_set_block_enabled(false, &error_abort);
1211 migrate_set_block_incremental(s, false);
1212 s->must_remove_block_options = false;
1213 }
1214}
1215
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001216static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +00001217{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001218 MigrationState *s = opaque;
1219
1220 qemu_bh_delete(s->cleanup_bh);
1221 s->cleanup_bh = NULL;
1222
Peter Xu0ceccd82018-01-03 20:20:06 +08001223 qemu_savevm_state_cleanup();
1224
zhanghailiang89a02a92016-01-15 11:37:42 +08001225 if (s->to_dst_file) {
Juan Quintelaf986c3d2016-01-14 16:52:55 +01001226 Error *local_err = NULL;
1227
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001228 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001229 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001230 if (s->migration_thread_running) {
1231 qemu_thread_join(&s->thread);
1232 s->migration_thread_running = false;
1233 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001234 qemu_mutex_lock_iothread();
1235
Juan Quintelaf986c3d2016-01-14 16:52:55 +01001236 if (multifd_save_cleanup(&local_err) != 0) {
1237 error_report_err(local_err);
1238 }
zhanghailiang89a02a92016-01-15 11:37:42 +08001239 qemu_fclose(s->to_dst_file);
1240 s->to_dst_file = NULL;
aliguori065e2812008-11-11 16:46:33 +00001241 }
1242
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001243 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
1244 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001245
Liang Li94f5a432015-11-02 15:37:00 +08001246 if (s->state == MIGRATION_STATUS_CANCELLING) {
zhanghailiang48781e52015-12-16 11:47:33 +00001247 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
Liang Li94f5a432015-11-02 15:37:00 +08001248 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001249 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001250
Juan Quintela87db1a72017-09-05 12:50:22 +02001251 if (s->error) {
1252 /* It is used on info migrate. We can't free it */
1253 error_report_err(error_copy(s->error));
1254 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001255 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +02001256 block_cleanup_parameters(s);
aliguori065e2812008-11-11 16:46:33 +00001257}
1258
Juan Quintela87db1a72017-09-05 12:50:22 +02001259void migrate_set_error(MigrationState *s, const Error *error)
1260{
1261 qemu_mutex_lock(&s->error_mutex);
1262 if (!s->error) {
1263 s->error = error_copy(error);
1264 }
1265 qemu_mutex_unlock(&s->error_mutex);
1266}
1267
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001268void migrate_fd_error(MigrationState *s, const Error *error)
aliguori065e2812008-11-11 16:46:33 +00001269{
Peter Maydell25174052016-10-21 18:41:45 +01001270 trace_migrate_fd_error(error_get_pretty(error));
zhanghailiang89a02a92016-01-15 11:37:42 +08001271 assert(s->to_dst_file == NULL);
zhanghailiang48781e52015-12-16 11:47:33 +00001272 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1273 MIGRATION_STATUS_FAILED);
Juan Quintela87db1a72017-09-05 12:50:22 +02001274 migrate_set_error(s, error);
Juan Quintela458cf282011-02-22 23:32:54 +01001275}
1276
Juan Quintela0edda1c2010-05-11 16:28:39 +02001277static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +00001278{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001279 int old_state ;
zhanghailiang89a02a92016-01-15 11:37:42 +08001280 QEMUFile *f = migrate_get_current()->to_dst_file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001281 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +00001282
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001283 if (s->rp_state.from_dst_file) {
1284 /* shutdown the rp socket, so causing the rp thread to shutdown */
1285 qemu_file_shutdown(s->rp_state.from_dst_file);
1286 }
1287
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001288 do {
1289 old_state = s->state;
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +00001290 if (!migration_is_setup_or_active(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001291 break;
1292 }
Dr. David Alan Gilberta7b36b42017-10-20 10:05:55 +01001293 /* If the migration is paused, kick it out of the pause */
1294 if (old_state == MIGRATION_STATUS_PRE_SWITCHOVER) {
1295 qemu_sem_post(&s->pause_sem);
1296 }
zhanghailiang48781e52015-12-16 11:47:33 +00001297 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
zhanghailiang31194732015-03-13 16:08:38 +08001298 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001299
1300 /*
1301 * If we're unlucky the migration code might be stuck somewhere in a
1302 * send/write while the network has failed and is waiting to timeout;
1303 * if we've got shutdown(2) available then we can force it to quit.
1304 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1305 * called in a bh, so there is no race against this cancel.
1306 */
zhanghailiang31194732015-03-13 16:08:38 +08001307 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001308 qemu_file_shutdown(f);
1309 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08001310 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
1311 Error *local_err = NULL;
1312
1313 bdrv_invalidate_cache_all(&local_err);
1314 if (local_err) {
1315 error_report_err(local_err);
1316 } else {
1317 s->block_inactive = false;
1318 }
1319 }
aliguori065e2812008-11-11 16:46:33 +00001320}
1321
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001322void add_migration_state_change_notifier(Notifier *notify)
1323{
1324 notifier_list_add(&migration_state_notifiers, notify);
1325}
1326
1327void remove_migration_state_change_notifier(Notifier *notify)
1328{
Paolo Bonzini31552522012-01-13 17:34:01 +01001329 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001330}
1331
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +02001332bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001333{
zhanghailiang31194732015-03-13 16:08:38 +08001334 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001335}
1336
Juan Quintela70736932011-02-23 00:43:59 +01001337bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001338{
zhanghailiang31194732015-03-13 16:08:38 +08001339 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001340}
Juan Quintela0edda1c2010-05-11 16:28:39 +02001341
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001342bool migration_has_failed(MigrationState *s)
1343{
zhanghailiang31194732015-03-13 16:08:38 +08001344 return (s->state == MIGRATION_STATUS_CANCELLED ||
1345 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001346}
1347
Juan Quintela57273092017-03-20 22:25:28 +01001348bool migration_in_postcopy(void)
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001349{
Juan Quintela57273092017-03-20 22:25:28 +01001350 MigrationState *s = migrate_get_current();
1351
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001352 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1353}
1354
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001355bool migration_in_postcopy_after_devices(MigrationState *s)
1356{
Juan Quintela57273092017-03-20 22:25:28 +01001357 return migration_in_postcopy() && s->postcopy_after_devices;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001358}
1359
Juan Quintelafab35002017-03-22 17:36:57 +01001360bool migration_is_idle(void)
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301361{
Juan Quintelafab35002017-03-22 17:36:57 +01001362 MigrationState *s = migrate_get_current();
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301363
1364 switch (s->state) {
1365 case MIGRATION_STATUS_NONE:
1366 case MIGRATION_STATUS_CANCELLED:
1367 case MIGRATION_STATUS_COMPLETED:
1368 case MIGRATION_STATUS_FAILED:
1369 return true;
1370 case MIGRATION_STATUS_SETUP:
1371 case MIGRATION_STATUS_CANCELLING:
1372 case MIGRATION_STATUS_ACTIVE:
1373 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1374 case MIGRATION_STATUS_COLO:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +01001375 case MIGRATION_STATUS_PRE_SWITCHOVER:
1376 case MIGRATION_STATUS_DEVICE:
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301377 return false;
1378 case MIGRATION_STATUS__MAX:
1379 g_assert_not_reached();
1380 }
1381
1382 return false;
1383}
1384
Peter Xu3e0c8052018-02-08 18:31:15 +08001385void migrate_init(MigrationState *s)
Juan Quintela0edda1c2010-05-11 16:28:39 +02001386{
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001387 /*
1388 * Reinitialise all migration state, except
1389 * parameters/capabilities that the user set, and
1390 * locks.
1391 */
1392 s->bytes_xfer = 0;
1393 s->xfer_limit = 0;
1394 s->cleanup_bh = 0;
zhanghailiang89a02a92016-01-15 11:37:42 +08001395 s->to_dst_file = NULL;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001396 s->state = MIGRATION_STATUS_NONE;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001397 s->rp_state.from_dst_file = NULL;
1398 s->rp_state.error = false;
1399 s->mbps = 0.0;
1400 s->downtime = 0;
1401 s->expected_downtime = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001402 s->setup_time = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001403 s->start_postcopy = false;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001404 s->postcopy_after_devices = false;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001405 s->migration_thread_running = false;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001406 error_free(s->error);
1407 s->error = NULL;
Juan Quintela1299c632011-11-09 21:29:01 +01001408
zhanghailiang48781e52015-12-16 11:47:33 +00001409 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +02001410
Peter Xu4af246a2018-01-03 20:20:08 +08001411 s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1412 s->total_time = 0;
Peter Xu7287cbd2018-01-03 20:20:09 +08001413 s->vm_was_running = false;
Peter Xub15df1a2018-01-03 20:20:13 +08001414 s->iteration_initial_bytes = 0;
1415 s->threshold_size = 0;
Juan Quintela0edda1c2010-05-11 16:28:39 +02001416}
Juan Quintelacab30142011-02-22 23:54:21 +01001417
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001418static GSList *migration_blockers;
1419
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301420int migrate_add_blocker(Error *reason, Error **errp)
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001421{
Peter Xu3df663e2017-06-27 12:10:15 +08001422 if (migrate_get_current()->only_migratable) {
Ashijeet Acharyab67b8c32017-01-16 17:01:54 +05301423 error_propagate(errp, error_copy(reason));
1424 error_prepend(errp, "disallowing migration blocker "
1425 "(--only_migratable) for: ");
1426 return -EACCES;
1427 }
1428
Juan Quintelafab35002017-03-22 17:36:57 +01001429 if (migration_is_idle()) {
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301430 migration_blockers = g_slist_prepend(migration_blockers, reason);
1431 return 0;
1432 }
1433
1434 error_propagate(errp, error_copy(reason));
1435 error_prepend(errp, "disallowing migration blocker (migration in "
1436 "progress) for: ");
1437 return -EBUSY;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001438}
1439
1440void migrate_del_blocker(Error *reason)
1441{
1442 migration_blockers = g_slist_remove(migration_blockers, reason);
1443}
1444
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001445void qmp_migrate_incoming(const char *uri, Error **errp)
1446{
1447 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001448 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001449
1450 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001451 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001452 return;
1453 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001454 if (!once) {
1455 error_setg(errp, "The incoming migration has already been started");
1456 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001457
1458 qemu_start_incoming_migration(uri, &local_err);
1459
1460 if (local_err) {
1461 error_propagate(errp, local_err);
1462 return;
1463 }
1464
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001465 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001466}
1467
Greg Kurz24f39022016-05-04 21:44:19 +02001468bool migration_is_blocked(Error **errp)
1469{
1470 if (qemu_savevm_state_blocked(errp)) {
1471 return true;
1472 }
1473
1474 if (migration_blockers) {
Eduardo Habkost250561e2017-06-08 10:39:05 -03001475 error_propagate(errp, error_copy(migration_blockers->data));
Greg Kurz24f39022016-05-04 21:44:19 +02001476 return true;
1477 }
1478
1479 return false;
1480}
1481
Peter Xud3e35b82018-05-02 18:47:24 +08001482/* Returns true if continue to migrate, or false if error detected */
1483static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc,
1484 bool resume, Error **errp)
1485{
1486 Error *local_err = NULL;
1487
1488 if (resume) {
1489 if (s->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
1490 error_setg(errp, "Cannot resume if there is no "
1491 "paused migration");
1492 return false;
1493 }
1494 /* This is a resume, skip init status */
1495 return true;
1496 }
1497
1498 if (migration_is_setup_or_active(s->state) ||
1499 s->state == MIGRATION_STATUS_CANCELLING ||
1500 s->state == MIGRATION_STATUS_COLO) {
1501 error_setg(errp, QERR_MIGRATION_ACTIVE);
1502 return false;
1503 }
1504
1505 if (runstate_check(RUN_STATE_INMIGRATE)) {
1506 error_setg(errp, "Guest is waiting for an incoming migration");
1507 return false;
1508 }
1509
1510 if (migration_is_blocked(errp)) {
1511 return false;
1512 }
1513
1514 if (blk || blk_inc) {
1515 if (migrate_use_block() || migrate_use_block_incremental()) {
1516 error_setg(errp, "Command options are incompatible with "
1517 "current migration capabilities");
1518 return false;
1519 }
1520 migrate_set_block_enabled(true, &local_err);
1521 if (local_err) {
1522 error_propagate(errp, local_err);
1523 return false;
1524 }
1525 s->must_remove_block_options = true;
1526 }
1527
1528 if (blk_inc) {
1529 migrate_set_block_incremental(s, true);
1530 }
1531
1532 migrate_init(s);
1533
1534 return true;
1535}
1536
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001537void qmp_migrate(const char *uri, bool has_blk, bool blk,
1538 bool has_inc, bool inc, bool has_detach, bool detach,
Peter Xu7a4da282018-05-02 18:47:23 +08001539 bool has_resume, bool resume, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001540{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +02001541 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +02001542 MigrationState *s = migrate_get_current();
Juan Quintelacab30142011-02-22 23:54:21 +01001543 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +01001544
Peter Xud3e35b82018-05-02 18:47:24 +08001545 if (!migrate_prepare(s, has_blk && blk, has_inc && inc,
1546 has_resume && resume, errp)) {
1547 /* Error detected, put into errp */
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001548 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001549 }
Juan Quintelacab30142011-02-22 23:54:21 +01001550
1551 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001552 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04001553#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08001554 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04001555 rdma_start_outgoing_migration(s, p, &local_err);
1556#endif
Juan Quintelacab30142011-02-22 23:54:21 +01001557 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001558 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001559 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001560 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001561 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001562 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001563 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001564 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1565 "a valid migration protocol");
zhanghailiang48781e52015-12-16 11:47:33 +00001566 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1567 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09576e72018-03-16 20:21:14 +00001568 block_cleanup_parameters(s);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001569 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001570 }
1571
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001572 if (local_err) {
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001573 migrate_fd_error(s, local_err);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001574 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001575 return;
Juan Quintela1299c632011-11-09 21:29:01 +01001576 }
Juan Quintelacab30142011-02-22 23:54:21 +01001577}
1578
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02001579void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001580{
Juan Quintela17549e82011-10-05 13:50:43 +02001581 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01001582}
1583
Dr. David Alan Gilbert89cfc022017-10-20 10:05:53 +01001584void qmp_migrate_continue(MigrationStatus state, Error **errp)
1585{
1586 MigrationState *s = migrate_get_current();
1587 if (s->state != state) {
1588 error_setg(errp, "Migration not in expected state: %s",
1589 MigrationStatus_str(s->state));
1590 return;
1591 }
1592 qemu_sem_post(&s->pause_sem);
1593}
1594
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001595void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1596{
Juan Quintela73af8dd2017-10-05 21:30:10 +02001597 MigrateSetParameters p = {
1598 .has_xbzrle_cache_size = true,
1599 .xbzrle_cache_size = value,
1600 };
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001601
Juan Quintela73af8dd2017-10-05 21:30:10 +02001602 qmp_migrate_set_parameters(&p, errp);
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001603}
1604
1605int64_t qmp_query_migrate_cache_size(Error **errp)
1606{
1607 return migrate_xbzrle_cache_size();
1608}
1609
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001610void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001611{
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001612 MigrateSetParameters p = {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301613 .has_max_bandwidth = true,
1614 .max_bandwidth = value,
1615 };
Juan Quintelacab30142011-02-22 23:54:21 +01001616
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301617 qmp_migrate_set_parameters(&p, errp);
Juan Quintelacab30142011-02-22 23:54:21 +01001618}
1619
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001620void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001621{
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03001622 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
1623 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
1624 "the range of 0 to %d seconds",
1625 MAX_MIGRATE_DOWNTIME_SECONDS);
1626 return;
1627 }
1628
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301629 value *= 1000; /* Convert to milliseconds */
1630 value = MAX(0, MIN(INT64_MAX, value));
1631
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001632 MigrateSetParameters p = {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301633 .has_downtime_limit = true,
1634 .downtime_limit = value,
1635 };
1636
1637 qmp_migrate_set_parameters(&p, errp);
aliguori5bb79102008-10-13 03:12:02 +00001638}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001639
Pavel Butsykin53f09a12017-02-03 18:23:20 +03001640bool migrate_release_ram(void)
1641{
1642 MigrationState *s;
1643
1644 s = migrate_get_current();
1645
1646 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
1647}
1648
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001649bool migrate_postcopy_ram(void)
1650{
1651 MigrationState *s;
1652
1653 s = migrate_get_current();
1654
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +00001655 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001656}
1657
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03001658bool migrate_postcopy(void)
1659{
Vladimir Sementsov-Ogievskiydd6bb912018-03-13 15:34:00 -04001660 return migrate_postcopy_ram() || migrate_dirty_bitmaps();
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03001661}
1662
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06001663bool migrate_auto_converge(void)
1664{
1665 MigrationState *s;
1666
1667 s = migrate_get_current();
1668
1669 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1670}
1671
Peter Lieven323004a2013-07-18 09:48:50 +02001672bool migrate_zero_blocks(void)
1673{
1674 MigrationState *s;
1675
1676 s = migrate_get_current();
1677
1678 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1679}
1680
Alexey Perevalovf22f9282018-03-22 21:17:22 +03001681bool migrate_postcopy_blocktime(void)
1682{
1683 MigrationState *s;
1684
1685 s = migrate_get_current();
1686
1687 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME];
1688}
1689
Liang Li8706d2d2015-03-23 16:32:17 +08001690bool migrate_use_compression(void)
1691{
Liang Lidde4e692015-03-23 16:32:26 +08001692 MigrationState *s;
1693
1694 s = migrate_get_current();
1695
1696 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08001697}
1698
1699int migrate_compress_level(void)
1700{
1701 MigrationState *s;
1702
1703 s = migrate_get_current();
1704
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001705 return s->parameters.compress_level;
Liang Li8706d2d2015-03-23 16:32:17 +08001706}
1707
1708int migrate_compress_threads(void)
1709{
1710 MigrationState *s;
1711
1712 s = migrate_get_current();
1713
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001714 return s->parameters.compress_threads;
Liang Li8706d2d2015-03-23 16:32:17 +08001715}
1716
Liang Li3fcb38c2015-03-23 16:32:18 +08001717int migrate_decompress_threads(void)
1718{
1719 MigrationState *s;
1720
1721 s = migrate_get_current();
1722
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001723 return s->parameters.decompress_threads;
Liang Li3fcb38c2015-03-23 16:32:18 +08001724}
1725
Vladimir Sementsov-Ogievskiy55efc8c2018-03-13 15:34:00 -04001726bool migrate_dirty_bitmaps(void)
1727{
1728 MigrationState *s;
1729
1730 s = migrate_get_current();
1731
1732 return s->enabled_capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS];
1733}
1734
Juan Quintelab05dc722015-07-07 14:44:05 +02001735bool migrate_use_events(void)
1736{
1737 MigrationState *s;
1738
1739 s = migrate_get_current();
1740
1741 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1742}
1743
Juan Quintela30126bb2016-01-14 12:23:00 +01001744bool migrate_use_multifd(void)
1745{
1746 MigrationState *s;
1747
1748 s = migrate_get_current();
1749
1750 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_MULTIFD];
1751}
1752
Dr. David Alan Gilbert93fbd032017-10-20 10:05:50 +01001753bool migrate_pause_before_switchover(void)
1754{
1755 MigrationState *s;
1756
1757 s = migrate_get_current();
1758
1759 return s->enabled_capabilities[
1760 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER];
1761}
1762
Juan Quintela4075fb12016-01-15 08:56:17 +01001763int migrate_multifd_channels(void)
1764{
1765 MigrationState *s;
1766
1767 s = migrate_get_current();
1768
1769 return s->parameters.x_multifd_channels;
1770}
1771
Juan Quintela0fb86602017-04-27 10:48:25 +02001772int migrate_multifd_page_count(void)
1773{
1774 MigrationState *s;
1775
1776 s = migrate_get_current();
1777
1778 return s->parameters.x_multifd_page_count;
1779}
1780
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001781int migrate_use_xbzrle(void)
1782{
1783 MigrationState *s;
1784
1785 s = migrate_get_current();
1786
1787 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1788}
1789
1790int64_t migrate_xbzrle_cache_size(void)
1791{
1792 MigrationState *s;
1793
1794 s = migrate_get_current();
1795
Juan Quintela73af8dd2017-10-05 21:30:10 +02001796 return s->parameters.xbzrle_cache_size;
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001797}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001798
Juan Quintela2833c592017-04-05 18:32:37 +02001799bool migrate_use_block(void)
1800{
1801 MigrationState *s;
1802
1803 s = migrate_get_current();
1804
1805 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
1806}
1807
Peter Xuc788ada2017-06-26 18:28:55 +08001808bool migrate_use_return_path(void)
1809{
1810 MigrationState *s;
1811
1812 s = migrate_get_current();
1813
1814 return s->enabled_capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
1815}
1816
Juan Quintela2833c592017-04-05 18:32:37 +02001817bool migrate_use_block_incremental(void)
1818{
1819 MigrationState *s;
1820
1821 s = migrate_get_current();
1822
1823 return s->parameters.block_incremental;
1824}
1825
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001826/* migration thread support */
1827/*
1828 * Something bad happened to the RP stream, mark an error
1829 * The caller shall print or trace something to indicate why
1830 */
1831static void mark_source_rp_bad(MigrationState *s)
1832{
1833 s->rp_state.error = true;
1834}
1835
1836static struct rp_cmd_args {
1837 ssize_t len; /* -1 = variable */
1838 const char *name;
1839} rp_cmd_args[] = {
1840 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1841 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1842 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001843 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1844 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Peter Xua335deb2018-05-02 18:47:28 +08001845 [MIG_RP_MSG_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001846 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1847};
1848
1849/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001850 * Process a request for pages received on the return path,
1851 * We're allowed to send more than requested (e.g. to round to our page size)
1852 * and we don't need to send pages that have already been sent.
1853 */
1854static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1855 ram_addr_t start, size_t len)
1856{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001857 long our_host_ps = getpagesize();
1858
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001859 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001860
1861 /*
1862 * Since we currently insist on matching page sizes, just sanity check
1863 * we're being asked for whole host pages.
1864 */
1865 if (start & (our_host_ps-1) ||
1866 (len & (our_host_ps-1))) {
1867 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1868 " len: %zd", __func__, start, len);
1869 mark_source_rp_bad(ms);
1870 return;
1871 }
1872
Juan Quintela96506892017-03-14 18:41:03 +01001873 if (ram_save_queue_pages(rbname, start, len)) {
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001874 mark_source_rp_bad(ms);
1875 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001876}
1877
Peter Xu14b17422018-05-02 18:47:21 +08001878/* Return true to retry, false to quit */
1879static bool postcopy_pause_return_path_thread(MigrationState *s)
1880{
1881 trace_postcopy_pause_return_path();
1882
1883 qemu_sem_wait(&s->postcopy_pause_rp_sem);
1884
1885 trace_postcopy_pause_return_path_continued();
1886
1887 return true;
1888}
1889
Peter Xua335deb2018-05-02 18:47:28 +08001890static int migrate_handle_rp_recv_bitmap(MigrationState *s, char *block_name)
1891{
1892 RAMBlock *block = qemu_ram_block_by_name(block_name);
1893
1894 if (!block) {
1895 error_report("%s: invalid block name '%s'", __func__, block_name);
1896 return -EINVAL;
1897 }
1898
1899 /* Fetch the received bitmap and refresh the dirty bitmap */
1900 return ram_dirty_bitmap_reload(s, block);
1901}
1902
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001903/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001904 * Handles messages sent on the return path towards the source VM
1905 *
1906 */
1907static void *source_return_path_thread(void *opaque)
1908{
1909 MigrationState *ms = opaque;
1910 QEMUFile *rp = ms->rp_state.from_dst_file;
1911 uint16_t header_len, header_type;
Peter Xu568b01c2016-03-09 14:12:12 +08001912 uint8_t buf[512];
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001913 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001914 ram_addr_t start = 0; /* =0 to silence warning */
1915 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001916 int res;
1917
1918 trace_source_return_path_thread_entry();
Peter Xu14b17422018-05-02 18:47:21 +08001919
1920retry:
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001921 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1922 migration_is_setup_or_active(ms->state)) {
1923 trace_source_return_path_thread_loop_top();
1924 header_type = qemu_get_be16(rp);
1925 header_len = qemu_get_be16(rp);
1926
Peter Xu7a9ddfb2018-02-08 18:31:05 +08001927 if (qemu_file_get_error(rp)) {
1928 mark_source_rp_bad(ms);
1929 goto out;
1930 }
1931
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001932 if (header_type >= MIG_RP_MSG_MAX ||
1933 header_type == MIG_RP_MSG_INVALID) {
1934 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1935 header_type, header_len);
1936 mark_source_rp_bad(ms);
1937 goto out;
1938 }
1939
1940 if ((rp_cmd_args[header_type].len != -1 &&
1941 header_len != rp_cmd_args[header_type].len) ||
Peter Xu568b01c2016-03-09 14:12:12 +08001942 header_len > sizeof(buf)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001943 error_report("RP: Received '%s' message (0x%04x) with"
1944 "incorrect length %d expecting %zu",
1945 rp_cmd_args[header_type].name, header_type, header_len,
1946 (size_t)rp_cmd_args[header_type].len);
1947 mark_source_rp_bad(ms);
1948 goto out;
1949 }
1950
1951 /* We know we've got a valid header by this point */
1952 res = qemu_get_buffer(rp, buf, header_len);
1953 if (res != header_len) {
1954 error_report("RP: Failed reading data for message 0x%04x"
1955 " read %d expected %d",
1956 header_type, res, header_len);
1957 mark_source_rp_bad(ms);
1958 goto out;
1959 }
1960
1961 /* OK, we have the message and the data */
1962 switch (header_type) {
1963 case MIG_RP_MSG_SHUT:
Peter Maydell4d885132016-06-10 17:09:22 +01001964 sibling_error = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001965 trace_source_return_path_thread_shut(sibling_error);
1966 if (sibling_error) {
1967 error_report("RP: Sibling indicated error %d", sibling_error);
1968 mark_source_rp_bad(ms);
1969 }
1970 /*
1971 * We'll let the main thread deal with closing the RP
1972 * we could do a shutdown(2) on it, but we're the only user
1973 * anyway, so there's nothing gained.
1974 */
1975 goto out;
1976
1977 case MIG_RP_MSG_PONG:
Peter Maydell4d885132016-06-10 17:09:22 +01001978 tmp32 = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001979 trace_source_return_path_thread_pong(tmp32);
1980 break;
1981
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001982 case MIG_RP_MSG_REQ_PAGES:
Peter Maydell4d885132016-06-10 17:09:22 +01001983 start = ldq_be_p(buf);
1984 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001985 migrate_handle_rp_req_pages(ms, NULL, start, len);
1986 break;
1987
1988 case MIG_RP_MSG_REQ_PAGES_ID:
1989 expected_len = 12 + 1; /* header + termination */
1990
1991 if (header_len >= expected_len) {
Peter Maydell4d885132016-06-10 17:09:22 +01001992 start = ldq_be_p(buf);
1993 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001994 /* Now we expect an idstr */
1995 tmp32 = buf[12]; /* Length of the following idstr */
1996 buf[13 + tmp32] = '\0';
1997 expected_len += tmp32;
1998 }
1999 if (header_len != expected_len) {
2000 error_report("RP: Req_Page_id with length %d expecting %zd",
2001 header_len, expected_len);
2002 mark_source_rp_bad(ms);
2003 goto out;
2004 }
2005 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
2006 break;
2007
Peter Xua335deb2018-05-02 18:47:28 +08002008 case MIG_RP_MSG_RECV_BITMAP:
2009 if (header_len < 1) {
2010 error_report("%s: missing block name", __func__);
2011 mark_source_rp_bad(ms);
2012 goto out;
2013 }
2014 /* Format: len (1B) + idstr (<255B). This ends the idstr. */
2015 buf[buf[0] + 1] = '\0';
2016 if (migrate_handle_rp_recv_bitmap(ms, (char *)(buf + 1))) {
2017 mark_source_rp_bad(ms);
2018 goto out;
2019 }
2020 break;
2021
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002022 default:
2023 break;
2024 }
2025 }
Peter Xu14b17422018-05-02 18:47:21 +08002026
2027out:
2028 res = qemu_file_get_error(rp);
2029 if (res) {
2030 if (res == -EIO) {
2031 /*
2032 * Maybe there is something we can do: it looks like a
2033 * network down issue, and we pause for a recovery.
2034 */
2035 if (postcopy_pause_return_path_thread(ms)) {
2036 /* Reload rp, reset the rest */
2037 rp = ms->rp_state.from_dst_file;
2038 ms->rp_state.error = false;
2039 goto retry;
2040 }
2041 }
2042
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002043 trace_source_return_path_thread_bad_end();
2044 mark_source_rp_bad(ms);
2045 }
2046
2047 trace_source_return_path_thread_end();
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002048 ms->rp_state.from_dst_file = NULL;
2049 qemu_fclose(rp);
2050 return NULL;
2051}
2052
Peter Xud3e35b82018-05-02 18:47:24 +08002053static int open_return_path_on_source(MigrationState *ms,
2054 bool create_thread)
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002055{
2056
zhanghailiang89a02a92016-01-15 11:37:42 +08002057 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002058 if (!ms->rp_state.from_dst_file) {
2059 return -1;
2060 }
2061
2062 trace_open_return_path_on_source();
Peter Xud3e35b82018-05-02 18:47:24 +08002063
2064 if (!create_thread) {
2065 /* We're done */
2066 return 0;
2067 }
2068
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002069 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
2070 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
2071
2072 trace_open_return_path_on_source_continue();
2073
2074 return 0;
2075}
2076
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002077/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
2078static int await_return_path_close_on_source(MigrationState *ms)
2079{
2080 /*
2081 * If this is a normal exit then the destination will send a SHUT and the
2082 * rp_thread will exit, however if there's an error we need to cause
2083 * it to exit.
2084 */
zhanghailiang89a02a92016-01-15 11:37:42 +08002085 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002086 /*
2087 * shutdown(2), if we have it, will cause it to unblock if it's stuck
2088 * waiting for the destination.
2089 */
2090 qemu_file_shutdown(ms->rp_state.from_dst_file);
2091 mark_source_rp_bad(ms);
2092 }
2093 trace_await_return_path_close_on_source_joining();
2094 qemu_thread_join(&ms->rp_state.rp_thread);
2095 trace_await_return_path_close_on_source_close();
2096 return ms->rp_state.error;
2097}
2098
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002099/*
2100 * Switch from normal iteration to postcopy
2101 * Returns non-0 on error
2102 */
Peter Xu7287cbd2018-01-03 20:20:09 +08002103static int postcopy_start(MigrationState *ms)
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002104{
2105 int ret;
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002106 QIOChannelBuffer *bioc;
2107 QEMUFile *fb;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002108 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002109 bool restart_block = false;
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002110 int cur_state = MIGRATION_STATUS_ACTIVE;
2111 if (!migrate_pause_before_switchover()) {
2112 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
2113 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2114 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002115
2116 trace_postcopy_start();
2117 qemu_mutex_lock_iothread();
2118 trace_postcopy_start_set_run();
2119
2120 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002121 global_state_store();
2122 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01002123 if (ret < 0) {
2124 goto fail;
2125 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002126
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002127 ret = migration_maybe_pause(ms, &cur_state,
2128 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2129 if (ret < 0) {
2130 goto fail;
2131 }
2132
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01002133 ret = bdrv_inactivate_all();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002134 if (ret < 0) {
2135 goto fail;
2136 }
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002137 restart_block = true;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002138
2139 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00002140 * Cause any non-postcopiable, but iterative devices to
2141 * send out their final data.
2142 */
Fam Zhenga1fbe752017-06-17 00:06:58 +08002143 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00002144
2145 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002146 * in Finish migrate and with the io-lock held everything should
2147 * be quiet, but we've potentially still got dirty pages and we
2148 * need to tell the destination to throw any pages it's already received
2149 * that are dirty
2150 */
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002151 if (migrate_postcopy_ram()) {
2152 if (ram_postcopy_send_discard_bitmap(ms)) {
2153 error_report("postcopy send discard bitmap failed");
2154 goto fail;
2155 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002156 }
2157
2158 /*
2159 * send rest of state - note things that are doing postcopy
2160 * will notice we're in POSTCOPY_ACTIVE and not actually
2161 * wrap their state up here
2162 */
zhanghailiang89a02a92016-01-15 11:37:42 +08002163 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002164 if (migrate_postcopy_ram()) {
2165 /* Ping just for debugging, helps line traces up */
2166 qemu_savevm_send_ping(ms->to_dst_file, 2);
2167 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002168
2169 /*
2170 * While loading the device state we may trigger page transfer
2171 * requests and the fd must be free to process those, and thus
2172 * the destination must read the whole device state off the fd before
2173 * it starts processing it. Unfortunately the ad-hoc migration format
2174 * doesn't allow the destination to know the size to read without fully
2175 * parsing it through each devices load-state code (especially the open
2176 * coded devices that use get/put).
2177 * So we wrap the device state up in a package with a length at the start;
2178 * to do this we use a qemu_buf to hold the whole of the device state.
2179 */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002180 bioc = qio_channel_buffer_new(4096);
Daniel P. Berrange6f01f132016-09-30 11:57:14 +01002181 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002182 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
2183 object_unref(OBJECT(bioc));
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002184
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00002185 /*
2186 * Make sure the receiver can get incoming pages before we send the rest
2187 * of the state
2188 */
2189 qemu_savevm_send_postcopy_listen(fb);
2190
Fam Zhenga1fbe752017-06-17 00:06:58 +08002191 qemu_savevm_state_complete_precopy(fb, false, false);
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002192 if (migrate_postcopy_ram()) {
2193 qemu_savevm_send_ping(fb, 3);
2194 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002195
2196 qemu_savevm_send_postcopy_run(fb);
2197
2198 /* <><> end of stuff going into the package */
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002199
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002200 /* Last point of recovery; as soon as we send the package the destination
2201 * can open devices and potentially start running.
2202 * Lets just check again we've not got any errors.
2203 */
2204 ret = qemu_file_get_error(ms->to_dst_file);
2205 if (ret) {
2206 error_report("postcopy_start: Migration stream errored (pre package)");
2207 goto fail_closefb;
2208 }
2209
2210 restart_block = false;
2211
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002212 /* Now send that blob */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002213 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002214 goto fail_closefb;
2215 }
2216 qemu_fclose(fb);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00002217
2218 /* Send a notify to give a chance for anything that needs to happen
2219 * at the transition to postcopy and after the device state; in particular
2220 * spice needs to trigger a transition now
2221 */
2222 ms->postcopy_after_devices = true;
2223 notifier_list_notify(&migration_state_notifiers, ms);
2224
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002225 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
2226
2227 qemu_mutex_unlock_iothread();
2228
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002229 if (migrate_postcopy_ram()) {
2230 /*
2231 * Although this ping is just for debug, it could potentially be
2232 * used for getting a better measurement of downtime at the source.
2233 */
2234 qemu_savevm_send_ping(ms->to_dst_file, 4);
2235 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002236
Pavel Butsykinced1c612017-02-03 18:23:21 +03002237 if (migrate_release_ram()) {
2238 ram_postcopy_migrated_memory_release(ms);
2239 }
2240
zhanghailiang89a02a92016-01-15 11:37:42 +08002241 ret = qemu_file_get_error(ms->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002242 if (ret) {
2243 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00002244 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002245 MIGRATION_STATUS_FAILED);
2246 }
2247
2248 return ret;
2249
2250fail_closefb:
2251 qemu_fclose(fb);
2252fail:
zhanghailiang48781e52015-12-16 11:47:33 +00002253 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002254 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002255 if (restart_block) {
2256 /* A failure happened early enough that we know the destination hasn't
2257 * accessed block devices, so we're safe to recover.
2258 */
2259 Error *local_err = NULL;
2260
2261 bdrv_invalidate_cache_all(&local_err);
2262 if (local_err) {
2263 error_report_err(local_err);
2264 }
2265 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002266 qemu_mutex_unlock_iothread();
2267 return -1;
2268}
2269
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002270/**
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002271 * migration_maybe_pause: Pause if required to by
2272 * migrate_pause_before_switchover called with the iothread locked
2273 * Returns: 0 on success
2274 */
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002275static int migration_maybe_pause(MigrationState *s,
2276 int *current_active_state,
2277 int new_state)
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002278{
2279 if (!migrate_pause_before_switchover()) {
2280 return 0;
2281 }
2282
2283 /* Since leaving this state is not atomic with posting the semaphore
2284 * it's possible that someone could have issued multiple migrate_continue
2285 * and the semaphore is incorrectly positive at this point;
2286 * the docs say it's undefined to reinit a semaphore that's already
2287 * init'd, so use timedwait to eat up any existing posts.
2288 */
2289 while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) {
2290 /* This block intentionally left blank */
2291 }
2292
2293 qemu_mutex_unlock_iothread();
2294 migrate_set_state(&s->state, *current_active_state,
2295 MIGRATION_STATUS_PRE_SWITCHOVER);
2296 qemu_sem_wait(&s->pause_sem);
2297 migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER,
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002298 new_state);
2299 *current_active_state = new_state;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002300 qemu_mutex_lock_iothread();
2301
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002302 return s->state == new_state ? 0 : -EINVAL;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002303}
2304
2305/**
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002306 * migration_completion: Used by migration_thread when there's not much left.
2307 * The caller 'breaks' the loop when this returns.
2308 *
2309 * @s: Current migration state
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002310 */
Peter Xu2ad87302018-01-03 20:20:14 +08002311static void migration_completion(MigrationState *s)
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002312{
2313 int ret;
Peter Xu2ad87302018-01-03 20:20:14 +08002314 int current_active_state = s->state;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002315
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002316 if (s->state == MIGRATION_STATUS_ACTIVE) {
2317 qemu_mutex_lock_iothread();
Peter Xu64909f92018-01-03 20:20:10 +08002318 s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002319 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
Peter Xu7287cbd2018-01-03 20:20:09 +08002320 s->vm_was_running = runstate_is_running();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002321 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002322
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002323 if (!ret) {
Fam Zhenga1fbe752017-06-17 00:06:58 +08002324 bool inactivate = !migrate_colo_enabled();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002325 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02002326 if (ret >= 0) {
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002327 ret = migration_maybe_pause(s, &current_active_state,
2328 MIGRATION_STATUS_DEVICE);
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002329 }
2330 if (ret >= 0) {
Kevin Wolff07fa4c2017-05-22 17:10:38 +02002331 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
Fam Zhenga1fbe752017-06-17 00:06:58 +08002332 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
2333 inactivate);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02002334 }
Fam Zhenga1fbe752017-06-17 00:06:58 +08002335 if (inactivate && ret >= 0) {
2336 s->block_inactive = true;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002337 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002338 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002339 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002340
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002341 if (ret < 0) {
2342 goto fail;
2343 }
2344 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2345 trace_migration_completion_postcopy_end();
2346
zhanghailiang89a02a92016-01-15 11:37:42 +08002347 qemu_savevm_state_complete_postcopy(s->to_dst_file);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002348 trace_migration_completion_postcopy_end_after_complete();
2349 }
2350
2351 /*
2352 * If rp was opened we must clean up the thread before
2353 * cleaning everything else up (since if there are no failures
2354 * it will wait for the destination to send it's status in
2355 * a SHUT command).
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002356 */
Peter Xu0425dc92017-05-31 18:35:34 +08002357 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002358 int rp_error;
Peter Xu0425dc92017-05-31 18:35:34 +08002359 trace_migration_return_path_end_before();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002360 rp_error = await_return_path_close_on_source(s);
Peter Xu0425dc92017-05-31 18:35:34 +08002361 trace_migration_return_path_end_after(rp_error);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002362 if (rp_error) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02002363 goto fail_invalidate;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002364 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002365 }
2366
zhanghailiang89a02a92016-01-15 11:37:42 +08002367 if (qemu_file_get_error(s->to_dst_file)) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002368 trace_migration_completion_file_err();
Greg Kurzfe904ea2016-05-18 15:44:36 +02002369 goto fail_invalidate;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002370 }
2371
zhanghailiang0b827d52016-10-27 14:42:54 +08002372 if (!migrate_colo_enabled()) {
2373 migrate_set_state(&s->state, current_active_state,
2374 MIGRATION_STATUS_COMPLETED);
2375 }
2376
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002377 return;
2378
Greg Kurzfe904ea2016-05-18 15:44:36 +02002379fail_invalidate:
2380 /* If not doing postcopy, vm_start() will be called: let's regain
2381 * control on images.
2382 */
Dr. David Alan Gilbert6039dd52018-02-05 09:13:37 +00002383 if (s->state == MIGRATION_STATUS_ACTIVE ||
2384 s->state == MIGRATION_STATUS_DEVICE) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02002385 Error *local_err = NULL;
2386
zhanghailiang1d2acc32017-01-24 15:59:52 +08002387 qemu_mutex_lock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02002388 bdrv_invalidate_cache_all(&local_err);
2389 if (local_err) {
2390 error_report_err(local_err);
zhanghailiang1d2acc32017-01-24 15:59:52 +08002391 } else {
2392 s->block_inactive = false;
Greg Kurzfe904ea2016-05-18 15:44:36 +02002393 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08002394 qemu_mutex_unlock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02002395 }
2396
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002397fail:
zhanghailiang48781e52015-12-16 11:47:33 +00002398 migrate_set_state(&s->state, current_active_state,
2399 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002400}
2401
zhanghailiang35a6ed42016-10-27 14:42:52 +08002402bool migrate_colo_enabled(void)
2403{
2404 MigrationState *s = migrate_get_current();
2405 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
2406}
2407
Peter Xub23c2ad2018-05-02 18:47:19 +08002408typedef enum MigThrError {
2409 /* No error detected */
2410 MIG_THR_ERR_NONE = 0,
2411 /* Detected error, but resumed successfully */
2412 MIG_THR_ERR_RECOVERED = 1,
2413 /* Detected fatal error, need to exit */
2414 MIG_THR_ERR_FATAL = 2,
2415} MigThrError;
2416
Peter Xu135b87b2018-05-02 18:47:25 +08002417/* Return zero if success, or <0 for error */
2418static int postcopy_do_resume(MigrationState *s)
2419{
2420 /* TODO: do the resume logic */
2421 return 0;
2422}
2423
Peter Xub23c2ad2018-05-02 18:47:19 +08002424/*
2425 * We don't return until we are in a safe state to continue current
2426 * postcopy migration. Returns MIG_THR_ERR_RECOVERED if recovered, or
2427 * MIG_THR_ERR_FATAL if unrecovery failure happened.
2428 */
2429static MigThrError postcopy_pause(MigrationState *s)
2430{
2431 assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
Peter Xub23c2ad2018-05-02 18:47:19 +08002432
Peter Xu135b87b2018-05-02 18:47:25 +08002433 while (true) {
2434 migrate_set_state(&s->state, s->state,
2435 MIGRATION_STATUS_POSTCOPY_PAUSED);
Peter Xub23c2ad2018-05-02 18:47:19 +08002436
Peter Xu135b87b2018-05-02 18:47:25 +08002437 /* Current channel is possibly broken. Release it. */
2438 assert(s->to_dst_file);
2439 qemu_file_shutdown(s->to_dst_file);
2440 qemu_fclose(s->to_dst_file);
2441 s->to_dst_file = NULL;
Peter Xub23c2ad2018-05-02 18:47:19 +08002442
Peter Xu135b87b2018-05-02 18:47:25 +08002443 error_report("Detected IO failure for postcopy. "
2444 "Migration paused.");
2445
2446 /*
2447 * We wait until things fixed up. Then someone will setup the
2448 * status back for us.
2449 */
2450 while (s->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
2451 qemu_sem_wait(&s->postcopy_pause_sem);
2452 }
2453
2454 if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
2455 /* Woken up by a recover procedure. Give it a shot */
2456
2457 /*
2458 * Firstly, let's wake up the return path now, with a new
2459 * return path channel.
2460 */
2461 qemu_sem_post(&s->postcopy_pause_rp_sem);
2462
2463 /* Do the resume logic */
2464 if (postcopy_do_resume(s) == 0) {
2465 /* Let's continue! */
2466 trace_postcopy_pause_continued();
2467 return MIG_THR_ERR_RECOVERED;
2468 } else {
2469 /*
2470 * Something wrong happened during the recovery, let's
2471 * pause again. Pause is always better than throwing
2472 * data away.
2473 */
2474 continue;
2475 }
2476 } else {
2477 /* This is not right... Time to quit. */
2478 return MIG_THR_ERR_FATAL;
2479 }
Peter Xub23c2ad2018-05-02 18:47:19 +08002480 }
Peter Xub23c2ad2018-05-02 18:47:19 +08002481}
2482
2483static MigThrError migration_detect_error(MigrationState *s)
2484{
2485 int ret;
2486
2487 /* Try to detect any file errors */
2488 ret = qemu_file_get_error(s->to_dst_file);
2489
2490 if (!ret) {
2491 /* Everything is fine */
2492 return MIG_THR_ERR_NONE;
2493 }
2494
2495 if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE && ret == -EIO) {
2496 /*
2497 * For postcopy, we allow the network to be down for a
2498 * while. After that, it can be continued by a
2499 * recovery phase.
2500 */
2501 return postcopy_pause(s);
2502 } else {
2503 /*
2504 * For precopy (or postcopy with error outside IO), we fail
2505 * with no time.
2506 */
2507 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
2508 trace_migration_thread_file_err();
2509
2510 /* Time to stop the migration, now. */
2511 return MIG_THR_ERR_FATAL;
2512 }
2513}
2514
Peter Xucf011f02018-01-03 20:20:11 +08002515static void migration_calculate_complete(MigrationState *s)
2516{
2517 uint64_t bytes = qemu_ftell(s->to_dst_file);
2518 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2519
2520 s->total_time = end_time - s->start_time;
2521 if (!s->downtime) {
2522 /*
2523 * It's still not set, so we are precopy migration. For
2524 * postcopy, downtime is calculated during postcopy_start().
2525 */
2526 s->downtime = end_time - s->downtime_start;
2527 }
2528
2529 if (s->total_time) {
2530 s->mbps = ((double) bytes * 8.0) / s->total_time / 1000;
2531 }
2532}
2533
Peter Xub15df1a2018-01-03 20:20:13 +08002534static void migration_update_counters(MigrationState *s,
2535 int64_t current_time)
2536{
2537 uint64_t transferred, time_spent;
Peter Xub15df1a2018-01-03 20:20:13 +08002538 double bandwidth;
2539
2540 if (current_time < s->iteration_start_time + BUFFER_DELAY) {
2541 return;
2542 }
2543
2544 transferred = qemu_ftell(s->to_dst_file) - s->iteration_initial_bytes;
2545 time_spent = current_time - s->iteration_start_time;
2546 bandwidth = (double)transferred / time_spent;
Wei Wang0781c1e2018-01-22 19:36:39 +08002547 s->threshold_size = bandwidth * s->parameters.downtime_limit;
Peter Xub15df1a2018-01-03 20:20:13 +08002548
2549 s->mbps = (((double) transferred * 8.0) /
2550 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
2551
2552 /*
2553 * if we haven't sent anything, we don't want to
2554 * recalculate. 10000 is a small enough number for our purposes
2555 */
2556 if (ram_counters.dirty_pages_rate && transferred > 10000) {
2557 s->expected_downtime = ram_counters.dirty_pages_rate *
2558 qemu_target_page_size() / bandwidth;
2559 }
2560
2561 qemu_file_reset_rate_limit(s->to_dst_file);
2562
2563 s->iteration_start_time = current_time;
2564 s->iteration_initial_bytes = qemu_ftell(s->to_dst_file);
2565
2566 trace_migrate_transferred(transferred, time_spent,
Wei Wang0781c1e2018-01-22 19:36:39 +08002567 bandwidth, s->threshold_size);
Peter Xub15df1a2018-01-03 20:20:13 +08002568}
2569
Peter Xu2ad87302018-01-03 20:20:14 +08002570/* Migration thread iteration status */
2571typedef enum {
2572 MIG_ITERATE_RESUME, /* Resume current iteration */
2573 MIG_ITERATE_SKIP, /* Skip current iteration */
2574 MIG_ITERATE_BREAK, /* Break the loop */
2575} MigIterateState;
2576
2577/*
2578 * Return true if continue to the next iteration directly, false
2579 * otherwise.
2580 */
2581static MigIterateState migration_iteration_run(MigrationState *s)
2582{
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04002583 uint64_t pending_size, pend_pre, pend_compat, pend_post;
Peter Xu2ad87302018-01-03 20:20:14 +08002584 bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE;
2585
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04002586 qemu_savevm_state_pending(s->to_dst_file, s->threshold_size, &pend_pre,
2587 &pend_compat, &pend_post);
2588 pending_size = pend_pre + pend_compat + pend_post;
Peter Xu2ad87302018-01-03 20:20:14 +08002589
2590 trace_migrate_pending(pending_size, s->threshold_size,
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04002591 pend_pre, pend_compat, pend_post);
Peter Xu2ad87302018-01-03 20:20:14 +08002592
2593 if (pending_size && pending_size >= s->threshold_size) {
2594 /* Still a significant amount to transfer */
2595 if (migrate_postcopy() && !in_postcopy &&
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04002596 pend_pre <= s->threshold_size &&
Peter Xu2ad87302018-01-03 20:20:14 +08002597 atomic_read(&s->start_postcopy)) {
2598 if (postcopy_start(s)) {
2599 error_report("%s: postcopy failed to start", __func__);
2600 }
2601 return MIG_ITERATE_SKIP;
2602 }
2603 /* Just another iteration step */
2604 qemu_savevm_state_iterate(s->to_dst_file,
2605 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
2606 } else {
2607 trace_migration_thread_low_pending(pending_size);
2608 migration_completion(s);
2609 return MIG_ITERATE_BREAK;
2610 }
2611
2612 return MIG_ITERATE_RESUME;
2613}
2614
Peter Xu199aa6d2018-01-03 20:20:15 +08002615static void migration_iteration_finish(MigrationState *s)
2616{
2617 /* If we enabled cpu throttling for auto-converge, turn it off. */
2618 cpu_throttle_stop();
2619
2620 qemu_mutex_lock_iothread();
2621 switch (s->state) {
2622 case MIGRATION_STATUS_COMPLETED:
2623 migration_calculate_complete(s);
2624 runstate_set(RUN_STATE_POSTMIGRATE);
2625 break;
2626
2627 case MIGRATION_STATUS_ACTIVE:
2628 /*
2629 * We should really assert here, but since it's during
2630 * migration, let's try to reduce the usage of assertions.
2631 */
2632 if (!migrate_colo_enabled()) {
2633 error_report("%s: critical error: calling COLO code without "
2634 "COLO enabled", __func__);
2635 }
2636 migrate_start_colo_process(s);
2637 /*
2638 * Fixme: we will run VM in COLO no matter its old running state.
2639 * After exited COLO, we will keep running.
2640 */
2641 s->vm_was_running = true;
2642 /* Fallthrough */
2643 case MIGRATION_STATUS_FAILED:
2644 case MIGRATION_STATUS_CANCELLED:
2645 if (s->vm_was_running) {
2646 vm_start();
2647 } else {
2648 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
2649 runstate_set(RUN_STATE_POSTMIGRATE);
2650 }
2651 }
2652 break;
2653
2654 default:
2655 /* Should not reach here, but if so, forgive the VM. */
2656 error_report("%s: Unknown ending state %d", __func__, s->state);
2657 break;
2658 }
2659 qemu_bh_schedule(s->cleanup_bh);
2660 qemu_mutex_unlock_iothread();
2661}
2662
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002663/*
2664 * Master migration thread on the source VM.
2665 * It drives the migration and pumps the data down the outgoing channel.
2666 */
Juan Quintela5f496a12013-02-22 17:36:30 +01002667static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002668{
Juan Quintela9848a402012-12-19 09:55:50 +01002669 MigrationState *s = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +01002670 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Peter Xub23c2ad2018-05-02 18:47:19 +08002671 MigThrError thr_error;
Juan Quintela76f59332012-10-03 20:16:24 +02002672
Paolo Bonziniab28bd22015-07-09 08:55:38 +02002673 rcu_register_thread();
2674
Peter Xub15df1a2018-01-03 20:20:13 +08002675 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2676
zhanghailiang89a02a92016-01-15 11:37:42 +08002677 qemu_savevm_state_header(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002678
Peter Xu62a02652017-06-14 15:55:58 +08002679 /*
2680 * If we opened the return path, we need to make sure dst has it
2681 * opened as well.
2682 */
2683 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002684 /* Now tell the dest that it should open its end so it can reply */
zhanghailiang89a02a92016-01-15 11:37:42 +08002685 qemu_savevm_send_open_return_path(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002686
2687 /* And do a ping that will make stuff easier to debug */
zhanghailiang89a02a92016-01-15 11:37:42 +08002688 qemu_savevm_send_ping(s->to_dst_file, 1);
Peter Xu0425dc92017-05-31 18:35:34 +08002689 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002690
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002691 if (migrate_postcopy()) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002692 /*
2693 * Tell the destination that we *might* want to do postcopy later;
2694 * if the other end can't do postcopy it should fail now, nice and
2695 * early.
2696 */
zhanghailiang89a02a92016-01-15 11:37:42 +08002697 qemu_savevm_send_postcopy_advise(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002698 }
2699
Juan Quintela9907e842017-06-28 11:52:24 +02002700 qemu_savevm_state_setup(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002701
Alex Blighbc72ad62013-08-21 16:03:08 +01002702 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang48781e52015-12-16 11:47:33 +00002703 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2704 MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04002705
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00002706 trace_migration_thread_setup_complete();
2707
2708 while (s->state == MIGRATION_STATUS_ACTIVE ||
2709 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +01002710 int64_t current_time;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002711
zhanghailiang89a02a92016-01-15 11:37:42 +08002712 if (!qemu_file_rate_limit(s->to_dst_file)) {
Peter Xu2ad87302018-01-03 20:20:14 +08002713 MigIterateState iter_state = migration_iteration_run(s);
2714 if (iter_state == MIG_ITERATE_SKIP) {
2715 continue;
2716 } else if (iter_state == MIG_ITERATE_BREAK) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002717 break;
Juan Quintelac369f402012-10-03 20:33:34 +02002718 }
2719 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01002720
Peter Xub23c2ad2018-05-02 18:47:19 +08002721 /*
2722 * Try to detect any kind of failures, and see whether we
2723 * should stop the migration now.
2724 */
2725 thr_error = migration_detect_error(s);
2726 if (thr_error == MIG_THR_ERR_FATAL) {
2727 /* Stop migration */
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01002728 break;
Peter Xub23c2ad2018-05-02 18:47:19 +08002729 } else if (thr_error == MIG_THR_ERR_RECOVERED) {
2730 /*
2731 * Just recovered from a e.g. network failure, reset all
2732 * the local variables. This is important to avoid
2733 * breaking transferred_bytes and bandwidth calculation
2734 */
2735 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2736 s->iteration_initial_bytes = 0;
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01002737 }
Peter Xub15df1a2018-01-03 20:20:13 +08002738
Alex Blighbc72ad62013-08-21 16:03:08 +01002739 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002740
Peter Xub15df1a2018-01-03 20:20:13 +08002741 migration_update_counters(s, current_time);
Michael R. Hines7e114f82013-06-25 21:35:30 -04002742
zhanghailiang89a02a92016-01-15 11:37:42 +08002743 if (qemu_file_rate_limit(s->to_dst_file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002744 /* usleep expects microseconds */
Peter Xub15df1a2018-01-03 20:20:13 +08002745 g_usleep((s->iteration_start_time + BUFFER_DELAY -
2746 current_time) * 1000);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002747 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002748 }
2749
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002750 trace_migration_thread_after_loop();
Peter Xu199aa6d2018-01-03 20:20:15 +08002751 migration_iteration_finish(s);
Paolo Bonziniab28bd22015-07-09 08:55:38 +02002752 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002753 return NULL;
2754}
2755
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00002756void migrate_fd_connect(MigrationState *s, Error *error_in)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002757{
Peter Xud3e35b82018-05-02 18:47:24 +08002758 int64_t rate_limit;
2759 bool resume = s->state == MIGRATION_STATUS_POSTCOPY_PAUSED;
2760
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302761 s->expected_downtime = s->parameters.downtime_limit;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01002762 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00002763 if (error_in) {
2764 migrate_fd_error(s, error_in);
2765 migrate_fd_cleanup(s);
2766 return;
2767 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002768
Peter Xud3e35b82018-05-02 18:47:24 +08002769 if (resume) {
2770 /* This is a resumed migration */
2771 rate_limit = INT64_MAX;
2772 } else {
2773 /* This is a fresh new migration */
2774 rate_limit = s->parameters.max_bandwidth / XFER_LIMIT_RATIO;
2775 s->expected_downtime = s->parameters.downtime_limit;
2776 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Paolo Bonzini442773c2013-02-22 17:36:44 +01002777
Peter Xud3e35b82018-05-02 18:47:24 +08002778 /* Notify before starting migration thread */
2779 notifier_list_notify(&migration_state_notifiers, s);
2780 }
2781
2782 qemu_file_set_rate_limit(s->to_dst_file, rate_limit);
2783 qemu_file_set_blocking(s->to_dst_file, true);
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02002784
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002785 /*
Peter Xuc788ada2017-06-26 18:28:55 +08002786 * Open the return path. For postcopy, it is used exclusively. For
2787 * precopy, only if user specified "return-path" capability would
2788 * QEMU uses the return path.
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002789 */
Peter Xuc788ada2017-06-26 18:28:55 +08002790 if (migrate_postcopy_ram() || migrate_use_return_path()) {
Peter Xud3e35b82018-05-02 18:47:24 +08002791 if (open_return_path_on_source(s, !resume)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002792 error_report("Unable to open return-path for postcopy");
Peter Xud3e35b82018-05-02 18:47:24 +08002793 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002794 migrate_fd_cleanup(s);
2795 return;
2796 }
2797 }
2798
Peter Xud3e35b82018-05-02 18:47:24 +08002799 if (resume) {
Peter Xu135b87b2018-05-02 18:47:25 +08002800 /* Wakeup the main migration thread to do the recovery */
2801 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
2802 MIGRATION_STATUS_POSTCOPY_RECOVER);
2803 qemu_sem_post(&s->postcopy_pause_sem);
Peter Xud3e35b82018-05-02 18:47:24 +08002804 return;
2805 }
2806
Juan Quintelaf986c3d2016-01-14 16:52:55 +01002807 if (multifd_save_setup() != 0) {
2808 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2809 MIGRATION_STATUS_FAILED);
2810 migrate_fd_cleanup(s);
2811 return;
2812 }
Pankaj Gupta009fad72017-01-23 19:12:56 +05302813 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01002814 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002815 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002816}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00002817
Peter Xu9d18af92017-06-27 12:10:19 +08002818void migration_global_dump(Monitor *mon)
2819{
2820 MigrationState *ms = migrate_get_current();
2821
Juan Quintela6f0f6422017-10-26 11:49:57 +02002822 monitor_printf(mon, "globals:\n");
2823 monitor_printf(mon, "store-global-state: %s\n",
2824 ms->store_global_state ? "on" : "off");
2825 monitor_printf(mon, "only-migratable: %s\n",
2826 ms->only_migratable ? "on" : "off");
2827 monitor_printf(mon, "send-configuration: %s\n",
2828 ms->send_configuration ? "on" : "off");
2829 monitor_printf(mon, "send-section-footer: %s\n",
2830 ms->send_section_footer ? "on" : "off");
Peter Xu9d18af92017-06-27 12:10:19 +08002831}
2832
Peter Xu20814752017-07-18 11:39:03 +08002833#define DEFINE_PROP_MIG_CAP(name, x) \
2834 DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
2835
Peter Xu52722982017-06-27 12:10:14 +08002836static Property migration_properties[] = {
2837 DEFINE_PROP_BOOL("store-global-state", MigrationState,
2838 store_global_state, true),
Peter Xu3df663e2017-06-27 12:10:15 +08002839 DEFINE_PROP_BOOL("only-migratable", MigrationState, only_migratable, false),
Peter Xu71dd4c12017-06-27 12:10:16 +08002840 DEFINE_PROP_BOOL("send-configuration", MigrationState,
2841 send_configuration, true),
Peter Xu15c38502017-06-27 12:10:17 +08002842 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
2843 send_section_footer, true),
Peter Xu89632fa2017-07-18 11:39:02 +08002844
2845 /* Migration parameters */
Juan Quintela741d4082017-12-01 13:08:38 +01002846 DEFINE_PROP_UINT8("x-compress-level", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002847 parameters.compress_level,
2848 DEFAULT_MIGRATE_COMPRESS_LEVEL),
Juan Quintela741d4082017-12-01 13:08:38 +01002849 DEFINE_PROP_UINT8("x-compress-threads", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002850 parameters.compress_threads,
2851 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
Juan Quintela741d4082017-12-01 13:08:38 +01002852 DEFINE_PROP_UINT8("x-decompress-threads", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002853 parameters.decompress_threads,
2854 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
Juan Quintela741d4082017-12-01 13:08:38 +01002855 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002856 parameters.cpu_throttle_initial,
2857 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
Juan Quintela741d4082017-12-01 13:08:38 +01002858 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002859 parameters.cpu_throttle_increment,
2860 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
Juan Quintela741d4082017-12-01 13:08:38 +01002861 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002862 parameters.max_bandwidth, MAX_THROTTLE),
Juan Quintela741d4082017-12-01 13:08:38 +01002863 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002864 parameters.downtime_limit,
2865 DEFAULT_MIGRATE_SET_DOWNTIME),
Juan Quintela741d4082017-12-01 13:08:38 +01002866 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002867 parameters.x_checkpoint_delay,
2868 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
Juan Quintela741d4082017-12-01 13:08:38 +01002869 DEFINE_PROP_UINT8("x-multifd-channels", MigrationState,
Juan Quintela4075fb12016-01-15 08:56:17 +01002870 parameters.x_multifd_channels,
2871 DEFAULT_MIGRATE_MULTIFD_CHANNELS),
Juan Quintela741d4082017-12-01 13:08:38 +01002872 DEFINE_PROP_UINT32("x-multifd-page-count", MigrationState,
Juan Quintela0fb86602017-04-27 10:48:25 +02002873 parameters.x_multifd_page_count,
2874 DEFAULT_MIGRATE_MULTIFD_PAGE_COUNT),
Juan Quintela73af8dd2017-10-05 21:30:10 +02002875 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
2876 parameters.xbzrle_cache_size,
2877 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
Peter Xu20814752017-07-18 11:39:03 +08002878
2879 /* Migration capabilities */
2880 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
2881 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
2882 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
2883 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
2884 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
2885 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
2886 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
2887 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
2888 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
2889 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
2890 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
Juan Quintela30126bb2016-01-14 12:23:00 +01002891 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_X_MULTIFD),
Peter Xu20814752017-07-18 11:39:03 +08002892
Peter Xu52722982017-06-27 12:10:14 +08002893 DEFINE_PROP_END_OF_LIST(),
2894};
2895
Peter Xue5cb7e72017-06-27 12:10:13 +08002896static void migration_class_init(ObjectClass *klass, void *data)
2897{
2898 DeviceClass *dc = DEVICE_CLASS(klass);
2899
2900 dc->user_creatable = false;
Peter Xu52722982017-06-27 12:10:14 +08002901 dc->props = migration_properties;
Peter Xue5cb7e72017-06-27 12:10:13 +08002902}
2903
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01002904static void migration_instance_finalize(Object *obj)
2905{
2906 MigrationState *ms = MIGRATION_OBJ(obj);
2907 MigrationParameters *params = &ms->parameters;
2908
Juan Quintela87db1a72017-09-05 12:50:22 +02002909 qemu_mutex_destroy(&ms->error_mutex);
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01002910 g_free(params->tls_hostname);
2911 g_free(params->tls_creds);
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002912 qemu_sem_destroy(&ms->pause_sem);
Peter Xub23c2ad2018-05-02 18:47:19 +08002913 qemu_sem_destroy(&ms->postcopy_pause_sem);
Peter Xu14b17422018-05-02 18:47:21 +08002914 qemu_sem_destroy(&ms->postcopy_pause_rp_sem);
Marc-André Lureauab105cc2018-03-06 18:09:59 +01002915 error_free(ms->error);
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01002916}
2917
Peter Xue5cb7e72017-06-27 12:10:13 +08002918static void migration_instance_init(Object *obj)
2919{
2920 MigrationState *ms = MIGRATION_OBJ(obj);
Peter Xu8b0b29d2017-07-18 11:39:06 +08002921 MigrationParameters *params = &ms->parameters;
Peter Xue5cb7e72017-06-27 12:10:13 +08002922
2923 ms->state = MIGRATION_STATUS_NONE;
Peter Xue5cb7e72017-06-27 12:10:13 +08002924 ms->mbps = -1;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002925 qemu_sem_init(&ms->pause_sem, 0);
Juan Quintela87db1a72017-09-05 12:50:22 +02002926 qemu_mutex_init(&ms->error_mutex);
Peter Xu8b0b29d2017-07-18 11:39:06 +08002927
2928 params->tls_hostname = g_strdup("");
2929 params->tls_creds = g_strdup("");
2930
2931 /* Set has_* up only for parameter checks */
2932 params->has_compress_level = true;
2933 params->has_compress_threads = true;
2934 params->has_decompress_threads = true;
2935 params->has_cpu_throttle_initial = true;
2936 params->has_cpu_throttle_increment = true;
2937 params->has_max_bandwidth = true;
2938 params->has_downtime_limit = true;
2939 params->has_x_checkpoint_delay = true;
2940 params->has_block_incremental = true;
Juan Quintela4075fb12016-01-15 08:56:17 +01002941 params->has_x_multifd_channels = true;
Juan Quintela0fb86602017-04-27 10:48:25 +02002942 params->has_x_multifd_page_count = true;
Juan Quintela73af8dd2017-10-05 21:30:10 +02002943 params->has_xbzrle_cache_size = true;
Peter Xub23c2ad2018-05-02 18:47:19 +08002944
2945 qemu_sem_init(&ms->postcopy_pause_sem, 0);
Peter Xu14b17422018-05-02 18:47:21 +08002946 qemu_sem_init(&ms->postcopy_pause_rp_sem, 0);
Peter Xu8b0b29d2017-07-18 11:39:06 +08002947}
2948
2949/*
2950 * Return true if check pass, false otherwise. Error will be put
2951 * inside errp if provided.
2952 */
2953static bool migration_object_check(MigrationState *ms, Error **errp)
2954{
Peter Xu6b19a7d2017-07-18 11:39:10 +08002955 MigrationCapabilityStatusList *head = NULL;
2956 /* Assuming all off */
2957 bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret;
2958 int i;
2959
Peter Xu8b0b29d2017-07-18 11:39:06 +08002960 if (!migrate_params_check(&ms->parameters, errp)) {
2961 return false;
2962 }
2963
Peter Xu6b19a7d2017-07-18 11:39:10 +08002964 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
2965 if (ms->enabled_capabilities[i]) {
2966 head = migrate_cap_add(head, i, true);
2967 }
2968 }
2969
2970 ret = migrate_caps_check(cap_list, head, errp);
2971
2972 /* It works with head == NULL */
2973 qapi_free_MigrationCapabilityStatusList(head);
2974
2975 return ret;
Peter Xue5cb7e72017-06-27 12:10:13 +08002976}
2977
2978static const TypeInfo migration_type = {
2979 .name = TYPE_MIGRATION,
Peter Xu01f6e142017-06-28 15:15:44 +08002980 /*
Peter Xuc8d3ff32017-07-05 16:21:23 +08002981 * NOTE: TYPE_MIGRATION is not really a device, as the object is
2982 * not created using qdev_create(), it is not attached to the qdev
2983 * device tree, and it is never realized.
2984 *
2985 * TODO: Make this TYPE_OBJECT once QOM provides something like
2986 * TYPE_DEVICE's "-global" properties.
Peter Xu01f6e142017-06-28 15:15:44 +08002987 */
Peter Xue5cb7e72017-06-27 12:10:13 +08002988 .parent = TYPE_DEVICE,
2989 .class_init = migration_class_init,
2990 .class_size = sizeof(MigrationClass),
2991 .instance_size = sizeof(MigrationState),
2992 .instance_init = migration_instance_init,
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01002993 .instance_finalize = migration_instance_finalize,
Peter Xue5cb7e72017-06-27 12:10:13 +08002994};
2995
2996static void register_migration_types(void)
2997{
2998 type_register_static(&migration_type);
2999}
3000
3001type_init(register_migration_types);