blob: 4f2c6d22d14b97e7d8e707823ab10632b543b311 [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 */
Peter Xu13955b82018-05-02 18:47:30 +080099 MIG_RP_MSG_RESUME_ACK, /* tell source that we are ready to resume */
Juan Quintelada6f1792017-04-24 17:37:14 +0200100
101 MIG_RP_MSG_MAX
102};
103
Juan Quintela17549e82011-10-05 13:50:43 +0200104/* When we add fault tolerance, we could have several
105 migrations at once. For now we don't need to add
106 dynamic creation of migration */
107
Peter Xue5cb7e72017-06-27 12:10:13 +0800108static MigrationState *current_migration;
109
Peter Xu8b0b29d2017-07-18 11:39:06 +0800110static bool migration_object_check(MigrationState *ms, Error **errp);
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +0100111static int migration_maybe_pause(MigrationState *s,
112 int *current_active_state,
113 int new_state);
Peter Xu8b0b29d2017-07-18 11:39:06 +0800114
Peter Xue5cb7e72017-06-27 12:10:13 +0800115void migration_object_init(void)
116{
Peter Xu4ffdb332017-06-27 12:10:18 +0800117 MachineState *ms = MACHINE(qdev_get_machine());
Peter Xu8b0b29d2017-07-18 11:39:06 +0800118 Error *err = NULL;
Peter Xu4ffdb332017-06-27 12:10:18 +0800119
Peter Xue5cb7e72017-06-27 12:10:13 +0800120 /* This can only be called once. */
121 assert(!current_migration);
122 current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION));
Peter Xu4ffdb332017-06-27 12:10:18 +0800123
Peter Xu8b0b29d2017-07-18 11:39:06 +0800124 if (!migration_object_check(current_migration, &err)) {
125 error_report_err(err);
126 exit(1);
127 }
128
Peter Xu4ffdb332017-06-27 12:10:18 +0800129 /*
130 * We cannot really do this in migration_instance_init() since at
131 * that time global properties are not yet applied, then this
132 * value will be definitely replaced by something else.
133 */
134 if (ms->enforce_config_section) {
135 current_migration->send_configuration = true;
136 }
Peter Xue5cb7e72017-06-27 12:10:13 +0800137}
138
Vladimir Sementsov-Ogievskiy1f895602017-12-28 12:16:16 +0300139void migration_object_finalize(void)
140{
141 object_unref(OBJECT(current_migration));
142}
143
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100144/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +0200145MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +0200146{
Peter Xue5cb7e72017-06-27 12:10:13 +0800147 /* This can only be called after the object created. */
148 assert(current_migration);
149 return current_migration;
Juan Quintela17549e82011-10-05 13:50:43 +0200150}
151
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100152MigrationIncomingState *migration_incoming_get_current(void)
153{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100154 static bool once;
155 static MigrationIncomingState mis_current;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100156
Juan Quintelab4b076d2017-01-23 22:32:06 +0100157 if (!once) {
158 mis_current.state = MIGRATION_STATUS_NONE;
159 memset(&mis_current, 0, sizeof(MigrationIncomingState));
Dr. David Alan Gilbert00fa4fc2018-03-12 17:21:04 +0000160 mis_current.postcopy_remote_fds = g_array_new(FALSE, TRUE,
161 sizeof(struct PostCopyFD));
Juan Quintelab4b076d2017-01-23 22:32:06 +0100162 qemu_mutex_init(&mis_current.rp_mutex);
163 qemu_event_init(&mis_current.main_thread_load_event, false);
Peter Xub411b842018-05-02 18:47:20 +0800164 qemu_sem_init(&mis_current.postcopy_pause_sem_dst, 0);
Peter Xu3a7804c2018-05-02 18:47:22 +0800165 qemu_sem_init(&mis_current.postcopy_pause_sem_fault, 0);
Vladimir Sementsov-Ogievskiyb35ebdf2018-03-13 15:34:01 -0400166
167 init_dirty_bitmap_incoming_migration();
168
Juan Quintelab4b076d2017-01-23 22:32:06 +0100169 once = true;
170 }
171 return &mis_current;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100172}
173
174void migration_incoming_state_destroy(void)
175{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100176 struct MigrationIncomingState *mis = migration_incoming_get_current();
177
Peter Xu34826552017-05-19 14:43:29 +0800178 if (mis->to_src_file) {
Peter Xu660819b2017-05-19 14:43:30 +0800179 /* Tell source that we are done */
180 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
Peter Xu34826552017-05-19 14:43:29 +0800181 qemu_fclose(mis->to_src_file);
182 mis->to_src_file = NULL;
183 }
184
Peter Xu660819b2017-05-19 14:43:30 +0800185 if (mis->from_src_file) {
186 qemu_fclose(mis->from_src_file);
187 mis->from_src_file = NULL;
188 }
Dr. David Alan Gilbert00fa4fc2018-03-12 17:21:04 +0000189 if (mis->postcopy_remote_fds) {
190 g_array_free(mis->postcopy_remote_fds, TRUE);
191 mis->postcopy_remote_fds = NULL;
192 }
Peter Xu660819b2017-05-19 14:43:30 +0800193
Dr. David Alan Gilbert5089e182017-08-25 15:19:39 +0100194 qemu_event_reset(&mis->main_thread_load_event);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100195}
196
Juan Quintelab05dc722015-07-07 14:44:05 +0200197static void migrate_generate_event(int new_state)
198{
199 if (migrate_use_events()) {
200 qapi_event_send_migration(new_state, &error_abort);
Juan Quintelab05dc722015-07-07 14:44:05 +0200201 }
202}
203
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000204/*
205 * Called on -incoming with a defer: uri.
206 * The migration can be started later after any parameters have been
207 * changed.
208 */
209static void deferred_incoming_migration(Error **errp)
210{
211 if (deferred_incoming) {
212 error_setg(errp, "Incoming migration already deferred");
213 }
214 deferred_incoming = true;
215}
216
Juan Quintelada6f1792017-04-24 17:37:14 +0200217/*
218 * Send a message on the return channel back to the source
219 * of the migration.
220 */
Peter Xud6208e32018-02-08 18:31:12 +0800221static int migrate_send_rp_message(MigrationIncomingState *mis,
222 enum mig_rp_message_type message_type,
223 uint16_t len, void *data)
Juan Quintelada6f1792017-04-24 17:37:14 +0200224{
Peter Xud6208e32018-02-08 18:31:12 +0800225 int ret = 0;
226
Juan Quintelada6f1792017-04-24 17:37:14 +0200227 trace_migrate_send_rp_message((int)message_type, len);
228 qemu_mutex_lock(&mis->rp_mutex);
Peter Xud6208e32018-02-08 18:31:12 +0800229
230 /*
231 * It's possible that the file handle got lost due to network
232 * failures.
233 */
234 if (!mis->to_src_file) {
235 ret = -EIO;
236 goto error;
237 }
238
Juan Quintelada6f1792017-04-24 17:37:14 +0200239 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
240 qemu_put_be16(mis->to_src_file, len);
241 qemu_put_buffer(mis->to_src_file, data, len);
242 qemu_fflush(mis->to_src_file);
Peter Xud6208e32018-02-08 18:31:12 +0800243
244 /* It's possible that qemu file got error during sending */
245 ret = qemu_file_get_error(mis->to_src_file);
246
247error:
Juan Quintelada6f1792017-04-24 17:37:14 +0200248 qemu_mutex_unlock(&mis->rp_mutex);
Peter Xud6208e32018-02-08 18:31:12 +0800249 return ret;
Juan Quintelada6f1792017-04-24 17:37:14 +0200250}
251
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000252/* Request a range of pages from the source VM at the given
253 * start address.
254 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
255 * as the last request (a name must have been given previously)
256 * Start: Address offset within the RB
257 * Len: Length in bytes required - must be a multiple of pagesize
258 */
Peter Xud6208e32018-02-08 18:31:12 +0800259int migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
260 ram_addr_t start, size_t len)
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000261{
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100262 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000263 size_t msglen = 12; /* start + len */
Peter Xud6208e32018-02-08 18:31:12 +0800264 enum mig_rp_message_type msg_type;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000265
266 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
267 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
268
269 if (rbname) {
270 int rbname_len = strlen(rbname);
271 assert(rbname_len < 256);
272
273 bufc[msglen++] = rbname_len;
274 memcpy(bufc + msglen, rbname, rbname_len);
275 msglen += rbname_len;
Peter Xud6208e32018-02-08 18:31:12 +0800276 msg_type = MIG_RP_MSG_REQ_PAGES_ID;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000277 } else {
Peter Xud6208e32018-02-08 18:31:12 +0800278 msg_type = MIG_RP_MSG_REQ_PAGES;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000279 }
Peter Xud6208e32018-02-08 18:31:12 +0800280
281 return migrate_send_rp_message(mis, msg_type, msglen, bufc);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000282}
283
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200284void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000285{
aliguori34c9dd82008-10-13 03:14:31 +0000286 const char *p;
287
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200288 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000289 if (!strcmp(uri, "defer")) {
290 deferred_incoming_migration(errp);
291 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200292 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400293#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000294 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400295 rdma_start_incoming_migration(p, errp);
296#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000297 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200298 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000299 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200300 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000301 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200302 fd_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000303 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100304 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200305 }
aliguori5bb79102008-10-13 03:12:02 +0000306}
307
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300308static void process_incoming_migration_bh(void *opaque)
309{
310 Error *local_err = NULL;
311 MigrationIncomingState *mis = opaque;
312
Dr. David Alan Gilberta18a73d2018-04-10 15:28:42 +0100313 /* Make sure all file formats flush their mutable metadata.
314 * If we get an error here, just don't restart the VM yet. */
315 bdrv_invalidate_cache_all(&local_err);
316 if (local_err) {
317 error_report_err(local_err);
318 local_err = NULL;
319 autostart = false;
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200320 }
321
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300322 /*
323 * This must happen after all error conditions are dealt with and
324 * we're sure the VM is going to be running on this host.
325 */
326 qemu_announce_self();
327
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100328 if (multifd_load_cleanup(&local_err) != 0) {
329 error_report_err(local_err);
330 autostart = false;
331 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300332 /* If global state section was not received or we are in running
333 state, we need to obey autostart. Any other state is set with
334 runstate_set. */
335
Vladimir Sementsov-Ogievskiyb35ebdf2018-03-13 15:34:01 -0400336 dirty_bitmap_mig_before_vm_start();
337
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300338 if (!global_state_received() ||
339 global_state_get_runstate() == RUN_STATE_RUNNING) {
340 if (autostart) {
341 vm_start();
342 } else {
343 runstate_set(RUN_STATE_PAUSED);
344 }
345 } else {
346 runstate_set(global_state_get_runstate());
347 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300348 /*
349 * This must happen after any state changes since as soon as an external
350 * observer sees this event they might start to prod at the VM assuming
351 * it's ready to use.
352 */
353 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
354 MIGRATION_STATUS_COMPLETED);
355 qemu_bh_delete(mis->bh);
356 migration_incoming_state_destroy();
357}
358
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200359static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200360{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100361 MigrationIncomingState *mis = migration_incoming_get_current();
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000362 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200363 int ret;
364
Juan Quintela4f0fae72017-07-24 12:42:02 +0200365 assert(mis->from_src_file);
Dr. David Alan Gilbert67f11b52017-02-24 18:28:34 +0000366 mis->largest_page_size = qemu_ram_pagesize_largest();
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000367 postcopy_state_set(POSTCOPY_INCOMING_NONE);
zhanghailiang93d7af62015-12-16 11:47:34 +0000368 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
369 MIGRATION_STATUS_ACTIVE);
Juan Quintela4f0fae72017-07-24 12:42:02 +0200370 ret = qemu_loadvm_state(mis->from_src_file);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100371
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000372 ps = postcopy_state_get();
373 trace_process_incoming_migration_co_end(ret, ps);
374 if (ps != POSTCOPY_INCOMING_NONE) {
375 if (ps == POSTCOPY_INCOMING_ADVISE) {
376 /*
377 * Where a migration had postcopy enabled (and thus went to advise)
378 * but managed to complete within the precopy period, we can use
379 * the normal exit.
380 */
381 postcopy_ram_incoming_cleanup(mis);
382 } else if (ret >= 0) {
383 /*
384 * Postcopy was started, cleanup should happen at the end of the
385 * postcopy thread.
386 */
387 trace_process_incoming_migration_co_postcopy_end_main();
388 return;
389 }
390 /* Else if something went wrong then just fall out of the normal exit */
391 }
392
zhanghailiang25d0c162016-10-27 14:42:55 +0800393 /* we get COLO info, and know if we are in COLO mode */
394 if (!ret && migration_incoming_enable_colo()) {
395 mis->migration_incoming_co = qemu_coroutine_self();
396 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
397 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
398 mis->have_colo_incoming_thread = true;
399 qemu_coroutine_yield();
400
401 /* Wait checkpoint incoming thread exit before free resource */
402 qemu_thread_join(&mis->colo_incoming_thread);
403 }
404
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200405 if (ret < 0) {
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100406 Error *local_err = NULL;
407
zhanghailiang93d7af62015-12-16 11:47:34 +0000408 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
409 MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200410 error_report("load of migration failed: %s", strerror(-ret));
Dr. David Alan Gilbert3a0f2ce2017-07-17 12:09:32 +0100411 qemu_fclose(mis->from_src_file);
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100412 if (multifd_load_cleanup(&local_err) != 0) {
413 error_report_err(local_err);
414 }
Eric Blake4aead692013-04-16 15:50:41 -0600415 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200416 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300417 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
418 qemu_bh_schedule(mis->bh);
Juan Quintela511c0232010-06-09 14:10:55 +0200419}
420
Juan Quintelae595a012017-07-17 12:30:25 +0200421static void migration_incoming_setup(QEMUFile *f)
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200422{
Juan Quintela4f0fae72017-07-24 12:42:02 +0200423 MigrationIncomingState *mis = migration_incoming_get_current();
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200424
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100425 if (multifd_load_setup() != 0) {
426 /* We haven't been able to create multifd threads
427 nothing better to do */
428 exit(EXIT_FAILURE);
429 }
430
Juan Quintela4f0fae72017-07-24 12:42:02 +0200431 if (!mis->from_src_file) {
432 mis->from_src_file = f;
433 }
Daniel P. Berrange06ad5132016-04-27 11:04:56 +0100434 qemu_file_set_blocking(f, false);
Juan Quintelae595a012017-07-17 12:30:25 +0200435}
436
Juan Quintela36c2f8b2018-03-07 08:40:52 +0100437void migration_incoming_process(void)
Juan Quintelae595a012017-07-17 12:30:25 +0200438{
439 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL);
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200440 qemu_coroutine_enter(co);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200441}
442
Juan Quintelae595a012017-07-17 12:30:25 +0200443void migration_fd_process_incoming(QEMUFile *f)
444{
Peter Xud96c9e82018-05-02 18:47:26 +0800445 MigrationIncomingState *mis = migration_incoming_get_current();
446
447 if (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
448 /* Resumed from a paused postcopy migration */
449
450 mis->from_src_file = f;
451 /* Postcopy has standalone thread to do vm load */
452 qemu_file_set_blocking(f, true);
453
454 /* Re-configure the return path */
455 mis->to_src_file = qemu_file_get_return_path(f);
456
457 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
458 MIGRATION_STATUS_POSTCOPY_RECOVER);
459
460 /*
461 * Here, we only wake up the main loading thread (while the
462 * fault thread will still be waiting), so that we can receive
463 * commands from source now, and answer it if needed. The
464 * fault thread will be woken up afterwards until we are sure
465 * that source is ready to reply to page requests.
466 */
467 qemu_sem_post(&mis->postcopy_pause_sem_dst);
468 } else {
469 /* New incoming migration */
470 migration_incoming_setup(f);
471 migration_incoming_process();
472 }
Juan Quintelae595a012017-07-17 12:30:25 +0200473}
474
Juan Quintela4f0fae72017-07-24 12:42:02 +0200475void migration_ioc_process_incoming(QIOChannel *ioc)
476{
477 MigrationIncomingState *mis = migration_incoming_get_current();
478
479 if (!mis->from_src_file) {
480 QEMUFile *f = qemu_fopen_channel_input(ioc);
Juan Quintela36c2f8b2018-03-07 08:40:52 +0100481 migration_incoming_setup(f);
Juan Quintela71bb07d2018-02-19 19:01:03 +0100482 return;
Juan Quintela4f0fae72017-07-24 12:42:02 +0200483 }
Juan Quintela71bb07d2018-02-19 19:01:03 +0100484 multifd_recv_new_channel(ioc);
Juan Quintela4f0fae72017-07-24 12:42:02 +0200485}
486
Juan Quintela428d8902017-07-24 13:06:25 +0200487/**
488 * @migration_has_all_channels: We have received all channels that we need
489 *
490 * Returns true when we have got connections to all the channels that
491 * we need for migration.
492 */
493bool migration_has_all_channels(void)
494{
Juan Quintela62c1e0c2018-02-19 18:59:02 +0100495 bool all_channels;
496
497 all_channels = multifd_recv_all_channels_created();
498
499 return all_channels;
Juan Quintela428d8902017-07-24 13:06:25 +0200500}
501
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000502/*
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000503 * Send a 'SHUT' message on the return channel with the given value
504 * to indicate that we've finished with the RP. Non-0 value indicates
505 * error.
506 */
507void migrate_send_rp_shut(MigrationIncomingState *mis,
508 uint32_t value)
509{
510 uint32_t buf;
511
512 buf = cpu_to_be32(value);
513 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
514}
515
516/*
517 * Send a 'PONG' message on the return channel with the given value
518 * (normally in response to a 'PING')
519 */
520void migrate_send_rp_pong(MigrationIncomingState *mis,
521 uint32_t value)
522{
523 uint32_t buf;
524
525 buf = cpu_to_be32(value);
526 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
527}
528
Peter Xua335deb2018-05-02 18:47:28 +0800529void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
530 char *block_name)
531{
532 char buf[512];
533 int len;
534 int64_t res;
535
536 /*
537 * First, we send the header part. It contains only the len of
538 * idstr, and the idstr itself.
539 */
540 len = strlen(block_name);
541 buf[0] = len;
542 memcpy(buf + 1, block_name, len);
543
544 if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
545 error_report("%s: MSG_RP_RECV_BITMAP only used for recovery",
546 __func__);
547 return;
548 }
549
550 migrate_send_rp_message(mis, MIG_RP_MSG_RECV_BITMAP, len + 1, buf);
551
552 /*
553 * Next, we dump the received bitmap to the stream.
554 *
555 * TODO: currently we are safe since we are the only one that is
556 * using the to_src_file handle (fault thread is still paused),
557 * and it's ok even not taking the mutex. However the best way is
558 * to take the lock before sending the message header, and release
559 * the lock after sending the bitmap.
560 */
561 qemu_mutex_lock(&mis->rp_mutex);
562 res = ramblock_recv_bitmap_send(mis->to_src_file, block_name);
563 qemu_mutex_unlock(&mis->rp_mutex);
564
565 trace_migrate_send_rp_recv_bitmap(block_name, res);
566}
567
Peter Xu13955b82018-05-02 18:47:30 +0800568void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value)
569{
570 uint32_t buf;
571
572 buf = cpu_to_be32(value);
573 migrate_send_rp_message(mis, MIG_RP_MSG_RESUME_ACK, sizeof(buf), &buf);
574}
575
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300576MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
577{
578 MigrationCapabilityStatusList *head = NULL;
579 MigrationCapabilityStatusList *caps;
580 MigrationState *s = migrate_get_current();
581 int i;
582
Michael Tokarev387eede2013-10-05 13:18:28 +0400583 caps = NULL; /* silence compiler warning */
Eric Blake7fb1cf12015-11-18 01:52:57 -0700584 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Dr. David Alan Gilberted1701c2017-05-15 15:05:29 +0100585#ifndef CONFIG_LIVE_BLOCK_MIGRATION
586 if (i == MIGRATION_CAPABILITY_BLOCK) {
587 continue;
588 }
589#endif
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300590 if (head == NULL) {
591 head = g_malloc0(sizeof(*caps));
592 caps = head;
593 } else {
594 caps->next = g_malloc0(sizeof(*caps));
595 caps = caps->next;
596 }
597 caps->value =
598 g_malloc(sizeof(*caps->value));
599 caps->value->capability = i;
600 caps->value->state = s->enabled_capabilities[i];
601 }
602
603 return head;
604}
605
Liang Li85de8322015-03-23 16:32:28 +0800606MigrationParameters *qmp_query_migrate_parameters(Error **errp)
607{
608 MigrationParameters *params;
609 MigrationState *s = migrate_get_current();
610
Markus Armbrustere87fae42017-07-18 12:57:38 +0200611 /* TODO use QAPI_CLONE() instead of duplicating it inline */
Liang Li85de8322015-03-23 16:32:28 +0800612 params = g_malloc0(sizeof(*params));
Eric Blakede63ab62016-09-08 22:14:15 -0500613 params->has_compress_level = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100614 params->compress_level = s->parameters.compress_level;
Eric Blakede63ab62016-09-08 22:14:15 -0500615 params->has_compress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100616 params->compress_threads = s->parameters.compress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500617 params->has_decompress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100618 params->decompress_threads = s->parameters.decompress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500619 params->has_cpu_throttle_initial = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100620 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
Eric Blakede63ab62016-09-08 22:14:15 -0500621 params->has_cpu_throttle_increment = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100622 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200623 params->has_tls_creds = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100624 params->tls_creds = g_strdup(s->parameters.tls_creds);
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200625 params->has_tls_hostname = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100626 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530627 params->has_max_bandwidth = true;
628 params->max_bandwidth = s->parameters.max_bandwidth;
629 params->has_downtime_limit = true;
630 params->downtime_limit = s->parameters.downtime_limit;
zhanghailiangfe39a4d2016-11-02 15:42:09 +0800631 params->has_x_checkpoint_delay = true;
zhanghailiang68b53592016-10-27 14:43:01 +0800632 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
Juan Quintela2833c592017-04-05 18:32:37 +0200633 params->has_block_incremental = true;
634 params->block_incremental = s->parameters.block_incremental;
Juan Quintela4075fb12016-01-15 08:56:17 +0100635 params->has_x_multifd_channels = true;
636 params->x_multifd_channels = s->parameters.x_multifd_channels;
Juan Quintela0fb86602017-04-27 10:48:25 +0200637 params->has_x_multifd_page_count = true;
638 params->x_multifd_page_count = s->parameters.x_multifd_page_count;
Juan Quintela73af8dd2017-10-05 21:30:10 +0200639 params->has_xbzrle_cache_size = true;
640 params->xbzrle_cache_size = s->parameters.xbzrle_cache_size;
Liang Li85de8322015-03-23 16:32:28 +0800641
642 return params;
643}
644
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000645/*
646 * Return true if we're already in the middle of a migration
647 * (i.e. any of the active or setup states)
648 */
649static bool migration_is_setup_or_active(int state)
650{
651 switch (state) {
652 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000653 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Peter Xua688d2c2018-05-02 18:47:18 +0800654 case MIGRATION_STATUS_POSTCOPY_PAUSED:
Peter Xu135b87b2018-05-02 18:47:25 +0800655 case MIGRATION_STATUS_POSTCOPY_RECOVER:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000656 case MIGRATION_STATUS_SETUP:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +0100657 case MIGRATION_STATUS_PRE_SWITCHOVER:
658 case MIGRATION_STATUS_DEVICE:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000659 return true;
660
661 default:
662 return false;
663
664 }
665}
666
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100667static void populate_ram_info(MigrationInfo *info, MigrationState *s)
668{
669 info->has_ram = true;
670 info->ram = g_malloc0(sizeof(*info->ram));
Juan Quintela93604472017-06-06 19:49:03 +0200671 info->ram->transferred = ram_counters.transferred;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100672 info->ram->total = ram_bytes_total();
Juan Quintela93604472017-06-06 19:49:03 +0200673 info->ram->duplicate = ram_counters.duplicate;
Juan Quintelabedf53c2017-03-13 20:35:54 +0100674 /* legacy value. It is not used anymore */
675 info->ram->skipped = 0;
Juan Quintela93604472017-06-06 19:49:03 +0200676 info->ram->normal = ram_counters.normal;
677 info->ram->normal_bytes = ram_counters.normal *
Juan Quintela20afaed2017-03-21 09:09:14 +0100678 qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100679 info->ram->mbps = s->mbps;
Juan Quintela93604472017-06-06 19:49:03 +0200680 info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
681 info->ram->postcopy_requests = ram_counters.postcopy_requests;
Chao Fan030ce1f2017-03-21 10:22:43 +0800682 info->ram->page_size = qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100683
Juan Quintela114f5ae2017-05-04 10:09:21 +0200684 if (migrate_use_xbzrle()) {
685 info->has_xbzrle_cache = true;
686 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
687 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
Juan Quintela93604472017-06-06 19:49:03 +0200688 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
689 info->xbzrle_cache->pages = xbzrle_counters.pages;
690 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
691 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
692 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
Juan Quintela114f5ae2017-05-04 10:09:21 +0200693 }
694
Juan Quintela338182c2017-05-03 13:16:38 +0200695 if (cpu_throttle_active()) {
696 info->has_cpu_throttle_percentage = true;
697 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
698 }
699
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100700 if (s->state != MIGRATION_STATUS_COMPLETED) {
701 info->ram->remaining = ram_bytes_remaining();
Juan Quintela93604472017-06-06 19:49:03 +0200702 info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100703 }
704}
705
Juan Quintela930ac042017-05-04 10:21:46 +0200706static void populate_disk_info(MigrationInfo *info)
707{
708 if (blk_mig_active()) {
709 info->has_disk = true;
710 info->disk = g_malloc0(sizeof(*info->disk));
711 info->disk->transferred = blk_mig_bytes_transferred();
712 info->disk->remaining = blk_mig_bytes_remaining();
713 info->disk->total = blk_mig_bytes_total();
714 }
715}
716
Alexey Perevalov65ace062018-03-22 21:17:27 +0300717static void fill_source_migration_info(MigrationInfo *info)
aliguori5bb79102008-10-13 03:12:02 +0000718{
Juan Quintela17549e82011-10-05 13:50:43 +0200719 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000720
Juan Quintela17549e82011-10-05 13:50:43 +0200721 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800722 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200723 /* no migration has happened ever */
Alexey Perevalov65ace062018-03-22 21:17:27 +0300724 /* do not overwrite destination migration status */
725 return;
Juan Quintela17549e82011-10-05 13:50:43 +0200726 break;
zhanghailiang31194732015-03-13 16:08:38 +0800727 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400728 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400729 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400730 break;
zhanghailiang31194732015-03-13 16:08:38 +0800731 case MIGRATION_STATUS_ACTIVE:
732 case MIGRATION_STATUS_CANCELLING:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000733 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +0100734 case MIGRATION_STATUS_PRE_SWITCHOVER:
735 case MIGRATION_STATUS_DEVICE:
Peter Xua688d2c2018-05-02 18:47:18 +0800736 case MIGRATION_STATUS_POSTCOPY_PAUSED:
Peter Xu135b87b2018-05-02 18:47:25 +0800737 case MIGRATION_STATUS_POSTCOPY_RECOVER:
Juan Quintelac8f9f4f2017-06-06 19:21:29 +0200738 /* TODO add some postcopy stats */
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000739 info->has_status = true;
740 info->has_total_time = true;
741 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Peter Xu4af246a2018-01-03 20:20:08 +0800742 - s->start_time;
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000743 info->has_expected_downtime = true;
744 info->expected_downtime = s->expected_downtime;
745 info->has_setup_time = true;
746 info->setup_time = s->setup_time;
747
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100748 populate_ram_info(info, s);
Juan Quintela930ac042017-05-04 10:21:46 +0200749 populate_disk_info(info);
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000750 break;
zhanghailiang0b827d52016-10-27 14:42:54 +0800751 case MIGRATION_STATUS_COLO:
752 info->has_status = true;
753 /* TODO: display COLO specific information (checkpoint info etc.) */
754 break;
zhanghailiang31194732015-03-13 16:08:38 +0800755 case MIGRATION_STATUS_COMPLETED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300756 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900757 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200758 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200759 info->has_downtime = true;
760 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400761 info->has_setup_time = true;
762 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200763
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100764 populate_ram_info(info, s);
Juan Quintela17549e82011-10-05 13:50:43 +0200765 break;
zhanghailiang31194732015-03-13 16:08:38 +0800766 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300767 info->has_status = true;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100768 if (s->error) {
769 info->has_error_desc = true;
770 info->error_desc = g_strdup(error_get_pretty(s->error));
771 }
Juan Quintela17549e82011-10-05 13:50:43 +0200772 break;
zhanghailiang31194732015-03-13 16:08:38 +0800773 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300774 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200775 break;
aliguori5bb79102008-10-13 03:12:02 +0000776 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800777 info->status = s->state;
aliguori5bb79102008-10-13 03:12:02 +0000778}
779
Peter Xu4a842142017-07-18 11:39:08 +0800780/**
781 * @migration_caps_check - check capability validity
782 *
783 * @cap_list: old capability list, array of bool
784 * @params: new capabilities to be applied soon
785 * @errp: set *errp if the check failed, with reason
786 *
787 * Returns true if check passed, otherwise false.
788 */
789static bool migrate_caps_check(bool *cap_list,
790 MigrationCapabilityStatusList *params,
791 Error **errp)
Orit Wasserman00458432012-08-06 21:42:48 +0300792{
Orit Wasserman00458432012-08-06 21:42:48 +0300793 MigrationCapabilityStatusList *cap;
Peter Xu4a842142017-07-18 11:39:08 +0800794 bool old_postcopy_cap;
Alexey Perevalovd7651f12017-09-19 19:47:56 +0300795 MigrationIncomingState *mis = migration_incoming_get_current();
Orit Wasserman00458432012-08-06 21:42:48 +0300796
Peter Xu4a842142017-07-18 11:39:08 +0800797 old_postcopy_cap = cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Orit Wasserman00458432012-08-06 21:42:48 +0300798
799 for (cap = params; cap; cap = cap->next) {
Peter Xu4a842142017-07-18 11:39:08 +0800800 cap_list[cap->value->capability] = cap->value->state;
Orit Wasserman00458432012-08-06 21:42:48 +0300801 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000802
Peter Xu4a842142017-07-18 11:39:08 +0800803#ifndef CONFIG_LIVE_BLOCK_MIGRATION
804 if (cap_list[MIGRATION_CAPABILITY_BLOCK]) {
805 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
806 "block migration");
807 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
808 return false;
809 }
810#endif
811
812 if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
813 if (cap_list[MIGRATION_CAPABILITY_COMPRESS]) {
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000814 /* The decompression threads asynchronously write into RAM
815 * rather than use the atomic copies needed to avoid
816 * userfaulting. It should be possible to fix the decompression
817 * threads for compatibility in future.
818 */
Peter Xu4a842142017-07-18 11:39:08 +0800819 error_setg(errp, "Postcopy is not currently compatible "
820 "with compression");
821 return false;
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000822 }
Peter Xu4a842142017-07-18 11:39:08 +0800823
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100824 /* This check is reasonably expensive, so only when it's being
825 * set the first time, also it's only the destination that needs
826 * special support.
827 */
828 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
Alexey Perevalovd7651f12017-09-19 19:47:56 +0300829 !postcopy_ram_supported_by_host(mis)) {
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100830 /* postcopy_ram_supported_by_host will have emitted a more
831 * detailed message
832 */
Peter Xu4a842142017-07-18 11:39:08 +0800833 error_setg(errp, "Postcopy is not supported");
834 return false;
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100835 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000836 }
Peter Xu4a842142017-07-18 11:39:08 +0800837
838 return true;
839}
840
Alexey Perevalov65ace062018-03-22 21:17:27 +0300841static void fill_destination_migration_info(MigrationInfo *info)
842{
843 MigrationIncomingState *mis = migration_incoming_get_current();
844
845 switch (mis->state) {
846 case MIGRATION_STATUS_NONE:
847 return;
848 break;
849 case MIGRATION_STATUS_SETUP:
850 case MIGRATION_STATUS_CANCELLING:
851 case MIGRATION_STATUS_CANCELLED:
852 case MIGRATION_STATUS_ACTIVE:
853 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
854 case MIGRATION_STATUS_FAILED:
855 case MIGRATION_STATUS_COLO:
856 info->has_status = true;
857 break;
858 case MIGRATION_STATUS_COMPLETED:
859 info->has_status = true;
860 fill_destination_postcopy_migration_info(info);
861 break;
862 }
863 info->status = mis->state;
864}
865
866MigrationInfo *qmp_query_migrate(Error **errp)
867{
868 MigrationInfo *info = g_malloc0(sizeof(*info));
869
870 fill_destination_migration_info(info);
871 fill_source_migration_info(info);
872
873 return info;
874}
875
Peter Xu4a842142017-07-18 11:39:08 +0800876void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
877 Error **errp)
878{
879 MigrationState *s = migrate_get_current();
880 MigrationCapabilityStatusList *cap;
Peter Xudd0ee302018-03-05 17:49:38 +0800881 bool cap_list[MIGRATION_CAPABILITY__MAX];
Peter Xu4a842142017-07-18 11:39:08 +0800882
883 if (migration_is_setup_or_active(s->state)) {
884 error_setg(errp, QERR_MIGRATION_ACTIVE);
885 return;
886 }
887
Peter Xudd0ee302018-03-05 17:49:38 +0800888 memcpy(cap_list, s->enabled_capabilities, sizeof(cap_list));
889 if (!migrate_caps_check(cap_list, params, errp)) {
Peter Xu4a842142017-07-18 11:39:08 +0800890 return;
891 }
892
893 for (cap = params; cap; cap = cap->next) {
894 s->enabled_capabilities[cap->value->capability] = cap->value->state;
895 }
Orit Wasserman00458432012-08-06 21:42:48 +0300896}
897
Peter Xu16d063b2017-07-18 11:39:04 +0800898/*
899 * Check whether the parameters are valid. Error will be put into errp
900 * (if provided). Return true if valid, otherwise false.
901 */
902static bool migrate_params_check(MigrationParameters *params, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +0800903{
Eric Blake7f375e02016-09-08 22:14:16 -0500904 if (params->has_compress_level &&
Juan Quintela741d4082017-12-01 13:08:38 +0100905 (params->compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100906 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
907 "is invalid, it should be in the range of 0 to 9");
Peter Xu16d063b2017-07-18 11:39:04 +0800908 return false;
Liang Li85de8322015-03-23 16:32:28 +0800909 }
Peter Xu16d063b2017-07-18 11:39:04 +0800910
Juan Quintela741d4082017-12-01 13:08:38 +0100911 if (params->has_compress_threads && (params->compress_threads < 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100912 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
913 "compress_threads",
914 "is invalid, it should be in the range of 1 to 255");
Peter Xu16d063b2017-07-18 11:39:04 +0800915 return false;
Liang Li85de8322015-03-23 16:32:28 +0800916 }
Peter Xu16d063b2017-07-18 11:39:04 +0800917
Juan Quintela741d4082017-12-01 13:08:38 +0100918 if (params->has_decompress_threads && (params->decompress_threads < 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100919 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
920 "decompress_threads",
921 "is invalid, it should be in the range of 1 to 255");
Peter Xu16d063b2017-07-18 11:39:04 +0800922 return false;
Liang Li85de8322015-03-23 16:32:28 +0800923 }
Peter Xu16d063b2017-07-18 11:39:04 +0800924
Eric Blake7f375e02016-09-08 22:14:16 -0500925 if (params->has_cpu_throttle_initial &&
926 (params->cpu_throttle_initial < 1 ||
927 params->cpu_throttle_initial > 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_initial",
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
Eric Blake7f375e02016-09-08 22:14:16 -0500934 if (params->has_cpu_throttle_increment &&
935 (params->cpu_throttle_increment < 1 ||
936 params->cpu_throttle_increment > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400937 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400938 "cpu_throttle_increment",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400939 "an integer in the range of 1 to 99");
Peter Xu16d063b2017-07-18 11:39:04 +0800940 return false;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400941 }
Peter Xu16d063b2017-07-18 11:39:04 +0800942
Juan Quintela741d4082017-12-01 13:08:38 +0100943 if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530944 error_setg(errp, "Parameter 'max_bandwidth' expects an integer in the"
945 " range of 0 to %zu bytes/second", SIZE_MAX);
Peter Xu16d063b2017-07-18 11:39:04 +0800946 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530947 }
Peter Xu16d063b2017-07-18 11:39:04 +0800948
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530949 if (params->has_downtime_limit &&
Juan Quintela741d4082017-12-01 13:08:38 +0100950 (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -0300951 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
952 "the range of 0 to %d milliseconds",
953 MAX_MIGRATE_DOWNTIME);
Peter Xu16d063b2017-07-18 11:39:04 +0800954 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530955 }
Peter Xu16d063b2017-07-18 11:39:04 +0800956
Juan Quintela741d4082017-12-01 13:08:38 +0100957 /* x_checkpoint_delay is now always positive */
958
959 if (params->has_x_multifd_channels && (params->x_multifd_channels < 1)) {
Juan Quintela4075fb12016-01-15 08:56:17 +0100960 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
961 "multifd_channels",
962 "is invalid, it should be in the range of 1 to 255");
963 return false;
964 }
Juan Quintela0fb86602017-04-27 10:48:25 +0200965 if (params->has_x_multifd_page_count &&
Juan Quintela741d4082017-12-01 13:08:38 +0100966 (params->x_multifd_page_count < 1 ||
967 params->x_multifd_page_count > 10000)) {
Juan Quintela0fb86602017-04-27 10:48:25 +0200968 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
969 "multifd_page_count",
970 "is invalid, it should be in the range of 1 to 10000");
971 return false;
972 }
Peter Xu16d063b2017-07-18 11:39:04 +0800973
Juan Quintela73af8dd2017-10-05 21:30:10 +0200974 if (params->has_xbzrle_cache_size &&
975 (params->xbzrle_cache_size < qemu_target_page_size() ||
976 !is_power_of_2(params->xbzrle_cache_size))) {
977 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
978 "xbzrle_cache_size",
979 "is invalid, it should be bigger than target page size"
980 " and a power of two");
981 return false;
982 }
983
Peter Xu16d063b2017-07-18 11:39:04 +0800984 return true;
985}
986
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200987static void migrate_params_test_apply(MigrateSetParameters *params,
988 MigrationParameters *dest)
989{
990 *dest = migrate_get_current()->parameters;
991
992 /* TODO use QAPI_CLONE() instead of duplicating it inline */
993
994 if (params->has_compress_level) {
995 dest->compress_level = params->compress_level;
996 }
997
998 if (params->has_compress_threads) {
999 dest->compress_threads = params->compress_threads;
1000 }
1001
1002 if (params->has_decompress_threads) {
1003 dest->decompress_threads = params->decompress_threads;
1004 }
1005
1006 if (params->has_cpu_throttle_initial) {
1007 dest->cpu_throttle_initial = params->cpu_throttle_initial;
1008 }
1009
1010 if (params->has_cpu_throttle_increment) {
1011 dest->cpu_throttle_increment = params->cpu_throttle_increment;
1012 }
1013
1014 if (params->has_tls_creds) {
Markus Armbruster01fa5592017-07-18 14:42:04 +02001015 assert(params->tls_creds->type == QTYPE_QSTRING);
1016 dest->tls_creds = g_strdup(params->tls_creds->u.s);
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001017 }
1018
1019 if (params->has_tls_hostname) {
Markus Armbruster01fa5592017-07-18 14:42:04 +02001020 assert(params->tls_hostname->type == QTYPE_QSTRING);
1021 dest->tls_hostname = g_strdup(params->tls_hostname->u.s);
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001022 }
1023
1024 if (params->has_max_bandwidth) {
1025 dest->max_bandwidth = params->max_bandwidth;
1026 }
1027
1028 if (params->has_downtime_limit) {
1029 dest->downtime_limit = params->downtime_limit;
1030 }
1031
1032 if (params->has_x_checkpoint_delay) {
1033 dest->x_checkpoint_delay = params->x_checkpoint_delay;
1034 }
1035
1036 if (params->has_block_incremental) {
1037 dest->block_incremental = params->block_incremental;
1038 }
Juan Quintela5e7577a2017-10-09 18:07:56 +02001039 if (params->has_x_multifd_channels) {
1040 dest->x_multifd_channels = params->x_multifd_channels;
1041 }
1042 if (params->has_x_multifd_page_count) {
1043 dest->x_multifd_page_count = params->x_multifd_page_count;
1044 }
Juan Quintela73af8dd2017-10-05 21:30:10 +02001045 if (params->has_xbzrle_cache_size) {
1046 dest->xbzrle_cache_size = params->xbzrle_cache_size;
1047 }
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001048}
1049
Juan Quintela73af8dd2017-10-05 21:30:10 +02001050static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
Peter Xu16d063b2017-07-18 11:39:04 +08001051{
1052 MigrationState *s = migrate_get_current();
1053
Markus Armbrustere87fae42017-07-18 12:57:38 +02001054 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1055
Eric Blake7f375e02016-09-08 22:14:16 -05001056 if (params->has_compress_level) {
1057 s->parameters.compress_level = params->compress_level;
Liang Li85de8322015-03-23 16:32:28 +08001058 }
Peter Xu476c72a2017-07-18 11:39:05 +08001059
Eric Blake7f375e02016-09-08 22:14:16 -05001060 if (params->has_compress_threads) {
1061 s->parameters.compress_threads = params->compress_threads;
Liang Li85de8322015-03-23 16:32:28 +08001062 }
Peter Xu476c72a2017-07-18 11:39:05 +08001063
Eric Blake7f375e02016-09-08 22:14:16 -05001064 if (params->has_decompress_threads) {
1065 s->parameters.decompress_threads = params->decompress_threads;
Liang Li85de8322015-03-23 16:32:28 +08001066 }
Peter Xu476c72a2017-07-18 11:39:05 +08001067
Eric Blake7f375e02016-09-08 22:14:16 -05001068 if (params->has_cpu_throttle_initial) {
1069 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001070 }
Peter Xu476c72a2017-07-18 11:39:05 +08001071
Eric Blake7f375e02016-09-08 22:14:16 -05001072 if (params->has_cpu_throttle_increment) {
1073 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
Jason J. Herne1626fee2015-09-08 13:12:34 -04001074 }
Peter Xu476c72a2017-07-18 11:39:05 +08001075
Eric Blake7f375e02016-09-08 22:14:16 -05001076 if (params->has_tls_creds) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001077 g_free(s->parameters.tls_creds);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001078 assert(params->tls_creds->type == QTYPE_QSTRING);
1079 s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001080 }
Peter Xu476c72a2017-07-18 11:39:05 +08001081
Eric Blake7f375e02016-09-08 22:14:16 -05001082 if (params->has_tls_hostname) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001083 g_free(s->parameters.tls_hostname);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001084 assert(params->tls_hostname->type == QTYPE_QSTRING);
1085 s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +01001086 }
Peter Xu476c72a2017-07-18 11:39:05 +08001087
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301088 if (params->has_max_bandwidth) {
1089 s->parameters.max_bandwidth = params->max_bandwidth;
1090 if (s->to_dst_file) {
1091 qemu_file_set_rate_limit(s->to_dst_file,
1092 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
1093 }
1094 }
Peter Xu476c72a2017-07-18 11:39:05 +08001095
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301096 if (params->has_downtime_limit) {
1097 s->parameters.downtime_limit = params->downtime_limit;
1098 }
zhanghailiang68b53592016-10-27 14:43:01 +08001099
1100 if (params->has_x_checkpoint_delay) {
1101 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
zhanghailiang479125d2017-01-17 20:57:42 +08001102 if (migration_in_colo_state()) {
1103 colo_checkpoint_notify(s);
1104 }
zhanghailiang68b53592016-10-27 14:43:01 +08001105 }
Peter Xu476c72a2017-07-18 11:39:05 +08001106
Juan Quintela2833c592017-04-05 18:32:37 +02001107 if (params->has_block_incremental) {
1108 s->parameters.block_incremental = params->block_incremental;
1109 }
Juan Quintela4075fb12016-01-15 08:56:17 +01001110 if (params->has_x_multifd_channels) {
1111 s->parameters.x_multifd_channels = params->x_multifd_channels;
1112 }
Juan Quintela0fb86602017-04-27 10:48:25 +02001113 if (params->has_x_multifd_page_count) {
1114 s->parameters.x_multifd_page_count = params->x_multifd_page_count;
1115 }
Juan Quintela73af8dd2017-10-05 21:30:10 +02001116 if (params->has_xbzrle_cache_size) {
1117 s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
1118 xbzrle_cache_resize(params->xbzrle_cache_size, errp);
1119 }
Liang Li85de8322015-03-23 16:32:28 +08001120}
1121
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001122void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
Peter Xu476c72a2017-07-18 11:39:05 +08001123{
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001124 MigrationParameters tmp;
1125
Markus Armbruster01fa5592017-07-18 14:42:04 +02001126 /* TODO Rewrite "" to null instead */
1127 if (params->has_tls_creds
1128 && params->tls_creds->type == QTYPE_QNULL) {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001129 qobject_unref(params->tls_creds->u.n);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001130 params->tls_creds->type = QTYPE_QSTRING;
1131 params->tls_creds->u.s = strdup("");
1132 }
1133 /* TODO Rewrite "" to null instead */
1134 if (params->has_tls_hostname
1135 && params->tls_hostname->type == QTYPE_QNULL) {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001136 qobject_unref(params->tls_hostname->u.n);
Markus Armbruster01fa5592017-07-18 14:42:04 +02001137 params->tls_hostname->type = QTYPE_QSTRING;
1138 params->tls_hostname->u.s = strdup("");
1139 }
1140
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001141 migrate_params_test_apply(params, &tmp);
1142
1143 if (!migrate_params_check(&tmp, errp)) {
Peter Xu476c72a2017-07-18 11:39:05 +08001144 /* Invalid parameter */
1145 return;
1146 }
1147
Juan Quintela73af8dd2017-10-05 21:30:10 +02001148 migrate_params_apply(params, errp);
Peter Xu476c72a2017-07-18 11:39:05 +08001149}
1150
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001151
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001152void qmp_migrate_start_postcopy(Error **errp)
1153{
1154 MigrationState *s = migrate_get_current();
1155
Vladimir Sementsov-Ogievskiy16b0fd32018-03-13 15:34:01 -04001156 if (!migrate_postcopy()) {
Dr. David Alan Gilberta54d3402015-11-12 11:34:44 +00001157 error_setg(errp, "Enable postcopy with migrate_set_capability before"
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001158 " the start of migration");
1159 return;
1160 }
1161
1162 if (s->state == MIGRATION_STATUS_NONE) {
1163 error_setg(errp, "Postcopy must be started after migration has been"
1164 " started");
1165 return;
1166 }
1167 /*
1168 * we don't error if migration has finished since that would be racy
1169 * with issuing this command.
1170 */
1171 atomic_set(&s->start_postcopy, true);
1172}
1173
aliguori065e2812008-11-11 16:46:33 +00001174/* shared migration helpers */
1175
zhanghailiang48781e52015-12-16 11:47:33 +00001176void migrate_set_state(int *state, int old_state, int new_state)
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +00001177{
Peter Xua31fede2017-08-30 16:32:01 +08001178 assert(new_state < MIGRATION_STATUS__MAX);
zhanghailiang48781e52015-12-16 11:47:33 +00001179 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
Peter Xua31fede2017-08-30 16:32:01 +08001180 trace_migrate_set_state(MigrationStatus_str(new_state));
Juan Quintelab05dc722015-07-07 14:44:05 +02001181 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +00001182 }
1183}
1184
Peter Xu4e4a3d32017-07-18 11:39:09 +08001185static MigrationCapabilityStatusList *migrate_cap_add(
1186 MigrationCapabilityStatusList *list,
1187 MigrationCapability index,
1188 bool state)
Juan Quintela2833c592017-04-05 18:32:37 +02001189{
1190 MigrationCapabilityStatusList *cap;
1191
1192 cap = g_new0(MigrationCapabilityStatusList, 1);
1193 cap->value = g_new0(MigrationCapabilityStatus, 1);
Peter Xu4e4a3d32017-07-18 11:39:09 +08001194 cap->value->capability = index;
1195 cap->value->state = state;
1196 cap->next = list;
1197
1198 return cap;
1199}
1200
1201void migrate_set_block_enabled(bool value, Error **errp)
1202{
1203 MigrationCapabilityStatusList *cap;
1204
1205 cap = migrate_cap_add(NULL, MIGRATION_CAPABILITY_BLOCK, value);
Juan Quintela2833c592017-04-05 18:32:37 +02001206 qmp_migrate_set_capabilities(cap, errp);
1207 qapi_free_MigrationCapabilityStatusList(cap);
1208}
1209
1210static void migrate_set_block_incremental(MigrationState *s, bool value)
1211{
1212 s->parameters.block_incremental = value;
1213}
1214
1215static void block_cleanup_parameters(MigrationState *s)
1216{
1217 if (s->must_remove_block_options) {
1218 /* setting to false can never fail */
1219 migrate_set_block_enabled(false, &error_abort);
1220 migrate_set_block_incremental(s, false);
1221 s->must_remove_block_options = false;
1222 }
1223}
1224
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001225static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +00001226{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001227 MigrationState *s = opaque;
1228
1229 qemu_bh_delete(s->cleanup_bh);
1230 s->cleanup_bh = NULL;
1231
Peter Xu0ceccd82018-01-03 20:20:06 +08001232 qemu_savevm_state_cleanup();
1233
zhanghailiang89a02a92016-01-15 11:37:42 +08001234 if (s->to_dst_file) {
Juan Quintelaf986c3d2016-01-14 16:52:55 +01001235 Error *local_err = NULL;
1236
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001237 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001238 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001239 if (s->migration_thread_running) {
1240 qemu_thread_join(&s->thread);
1241 s->migration_thread_running = false;
1242 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001243 qemu_mutex_lock_iothread();
1244
Juan Quintelaf986c3d2016-01-14 16:52:55 +01001245 if (multifd_save_cleanup(&local_err) != 0) {
1246 error_report_err(local_err);
1247 }
zhanghailiang89a02a92016-01-15 11:37:42 +08001248 qemu_fclose(s->to_dst_file);
1249 s->to_dst_file = NULL;
aliguori065e2812008-11-11 16:46:33 +00001250 }
1251
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001252 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
1253 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001254
Liang Li94f5a432015-11-02 15:37:00 +08001255 if (s->state == MIGRATION_STATUS_CANCELLING) {
zhanghailiang48781e52015-12-16 11:47:33 +00001256 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
Liang Li94f5a432015-11-02 15:37:00 +08001257 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001258 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001259
Juan Quintela87db1a72017-09-05 12:50:22 +02001260 if (s->error) {
1261 /* It is used on info migrate. We can't free it */
1262 error_report_err(error_copy(s->error));
1263 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001264 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +02001265 block_cleanup_parameters(s);
aliguori065e2812008-11-11 16:46:33 +00001266}
1267
Juan Quintela87db1a72017-09-05 12:50:22 +02001268void migrate_set_error(MigrationState *s, const Error *error)
1269{
1270 qemu_mutex_lock(&s->error_mutex);
1271 if (!s->error) {
1272 s->error = error_copy(error);
1273 }
1274 qemu_mutex_unlock(&s->error_mutex);
1275}
1276
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001277void migrate_fd_error(MigrationState *s, const Error *error)
aliguori065e2812008-11-11 16:46:33 +00001278{
Peter Maydell25174052016-10-21 18:41:45 +01001279 trace_migrate_fd_error(error_get_pretty(error));
zhanghailiang89a02a92016-01-15 11:37:42 +08001280 assert(s->to_dst_file == NULL);
zhanghailiang48781e52015-12-16 11:47:33 +00001281 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1282 MIGRATION_STATUS_FAILED);
Juan Quintela87db1a72017-09-05 12:50:22 +02001283 migrate_set_error(s, error);
Juan Quintela458cf282011-02-22 23:32:54 +01001284}
1285
Juan Quintela0edda1c2010-05-11 16:28:39 +02001286static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +00001287{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001288 int old_state ;
zhanghailiang89a02a92016-01-15 11:37:42 +08001289 QEMUFile *f = migrate_get_current()->to_dst_file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001290 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +00001291
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001292 if (s->rp_state.from_dst_file) {
1293 /* shutdown the rp socket, so causing the rp thread to shutdown */
1294 qemu_file_shutdown(s->rp_state.from_dst_file);
1295 }
1296
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001297 do {
1298 old_state = s->state;
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +00001299 if (!migration_is_setup_or_active(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001300 break;
1301 }
Dr. David Alan Gilberta7b36b42017-10-20 10:05:55 +01001302 /* If the migration is paused, kick it out of the pause */
1303 if (old_state == MIGRATION_STATUS_PRE_SWITCHOVER) {
1304 qemu_sem_post(&s->pause_sem);
1305 }
zhanghailiang48781e52015-12-16 11:47:33 +00001306 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
zhanghailiang31194732015-03-13 16:08:38 +08001307 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001308
1309 /*
1310 * If we're unlucky the migration code might be stuck somewhere in a
1311 * send/write while the network has failed and is waiting to timeout;
1312 * if we've got shutdown(2) available then we can force it to quit.
1313 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1314 * called in a bh, so there is no race against this cancel.
1315 */
zhanghailiang31194732015-03-13 16:08:38 +08001316 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001317 qemu_file_shutdown(f);
1318 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08001319 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
1320 Error *local_err = NULL;
1321
1322 bdrv_invalidate_cache_all(&local_err);
1323 if (local_err) {
1324 error_report_err(local_err);
1325 } else {
1326 s->block_inactive = false;
1327 }
1328 }
aliguori065e2812008-11-11 16:46:33 +00001329}
1330
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001331void add_migration_state_change_notifier(Notifier *notify)
1332{
1333 notifier_list_add(&migration_state_notifiers, notify);
1334}
1335
1336void remove_migration_state_change_notifier(Notifier *notify)
1337{
Paolo Bonzini31552522012-01-13 17:34:01 +01001338 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001339}
1340
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +02001341bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001342{
zhanghailiang31194732015-03-13 16:08:38 +08001343 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001344}
1345
Juan Quintela70736932011-02-23 00:43:59 +01001346bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001347{
zhanghailiang31194732015-03-13 16:08:38 +08001348 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001349}
Juan Quintela0edda1c2010-05-11 16:28:39 +02001350
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001351bool migration_has_failed(MigrationState *s)
1352{
zhanghailiang31194732015-03-13 16:08:38 +08001353 return (s->state == MIGRATION_STATUS_CANCELLED ||
1354 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001355}
1356
Juan Quintela57273092017-03-20 22:25:28 +01001357bool migration_in_postcopy(void)
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001358{
Juan Quintela57273092017-03-20 22:25:28 +01001359 MigrationState *s = migrate_get_current();
1360
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001361 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1362}
1363
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001364bool migration_in_postcopy_after_devices(MigrationState *s)
1365{
Juan Quintela57273092017-03-20 22:25:28 +01001366 return migration_in_postcopy() && s->postcopy_after_devices;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001367}
1368
Juan Quintelafab35002017-03-22 17:36:57 +01001369bool migration_is_idle(void)
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301370{
Juan Quintelafab35002017-03-22 17:36:57 +01001371 MigrationState *s = migrate_get_current();
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301372
1373 switch (s->state) {
1374 case MIGRATION_STATUS_NONE:
1375 case MIGRATION_STATUS_CANCELLED:
1376 case MIGRATION_STATUS_COMPLETED:
1377 case MIGRATION_STATUS_FAILED:
1378 return true;
1379 case MIGRATION_STATUS_SETUP:
1380 case MIGRATION_STATUS_CANCELLING:
1381 case MIGRATION_STATUS_ACTIVE:
1382 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1383 case MIGRATION_STATUS_COLO:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +01001384 case MIGRATION_STATUS_PRE_SWITCHOVER:
1385 case MIGRATION_STATUS_DEVICE:
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301386 return false;
1387 case MIGRATION_STATUS__MAX:
1388 g_assert_not_reached();
1389 }
1390
1391 return false;
1392}
1393
Peter Xu3e0c8052018-02-08 18:31:15 +08001394void migrate_init(MigrationState *s)
Juan Quintela0edda1c2010-05-11 16:28:39 +02001395{
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001396 /*
1397 * Reinitialise all migration state, except
1398 * parameters/capabilities that the user set, and
1399 * locks.
1400 */
1401 s->bytes_xfer = 0;
1402 s->xfer_limit = 0;
1403 s->cleanup_bh = 0;
zhanghailiang89a02a92016-01-15 11:37:42 +08001404 s->to_dst_file = NULL;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001405 s->state = MIGRATION_STATUS_NONE;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001406 s->rp_state.from_dst_file = NULL;
1407 s->rp_state.error = false;
1408 s->mbps = 0.0;
1409 s->downtime = 0;
1410 s->expected_downtime = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001411 s->setup_time = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001412 s->start_postcopy = false;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001413 s->postcopy_after_devices = false;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001414 s->migration_thread_running = false;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001415 error_free(s->error);
1416 s->error = NULL;
Juan Quintela1299c632011-11-09 21:29:01 +01001417
zhanghailiang48781e52015-12-16 11:47:33 +00001418 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +02001419
Peter Xu4af246a2018-01-03 20:20:08 +08001420 s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1421 s->total_time = 0;
Peter Xu7287cbd2018-01-03 20:20:09 +08001422 s->vm_was_running = false;
Peter Xub15df1a2018-01-03 20:20:13 +08001423 s->iteration_initial_bytes = 0;
1424 s->threshold_size = 0;
Juan Quintela0edda1c2010-05-11 16:28:39 +02001425}
Juan Quintelacab30142011-02-22 23:54:21 +01001426
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001427static GSList *migration_blockers;
1428
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301429int migrate_add_blocker(Error *reason, Error **errp)
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001430{
Peter Xu3df663e2017-06-27 12:10:15 +08001431 if (migrate_get_current()->only_migratable) {
Ashijeet Acharyab67b8c32017-01-16 17:01:54 +05301432 error_propagate(errp, error_copy(reason));
1433 error_prepend(errp, "disallowing migration blocker "
1434 "(--only_migratable) for: ");
1435 return -EACCES;
1436 }
1437
Juan Quintelafab35002017-03-22 17:36:57 +01001438 if (migration_is_idle()) {
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301439 migration_blockers = g_slist_prepend(migration_blockers, reason);
1440 return 0;
1441 }
1442
1443 error_propagate(errp, error_copy(reason));
1444 error_prepend(errp, "disallowing migration blocker (migration in "
1445 "progress) for: ");
1446 return -EBUSY;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001447}
1448
1449void migrate_del_blocker(Error *reason)
1450{
1451 migration_blockers = g_slist_remove(migration_blockers, reason);
1452}
1453
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001454void qmp_migrate_incoming(const char *uri, Error **errp)
1455{
1456 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001457 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001458
1459 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001460 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001461 return;
1462 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001463 if (!once) {
1464 error_setg(errp, "The incoming migration has already been started");
1465 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001466
1467 qemu_start_incoming_migration(uri, &local_err);
1468
1469 if (local_err) {
1470 error_propagate(errp, local_err);
1471 return;
1472 }
1473
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001474 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001475}
1476
Greg Kurz24f39022016-05-04 21:44:19 +02001477bool migration_is_blocked(Error **errp)
1478{
1479 if (qemu_savevm_state_blocked(errp)) {
1480 return true;
1481 }
1482
1483 if (migration_blockers) {
Eduardo Habkost250561e2017-06-08 10:39:05 -03001484 error_propagate(errp, error_copy(migration_blockers->data));
Greg Kurz24f39022016-05-04 21:44:19 +02001485 return true;
1486 }
1487
1488 return false;
1489}
1490
Peter Xud3e35b82018-05-02 18:47:24 +08001491/* Returns true if continue to migrate, or false if error detected */
1492static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc,
1493 bool resume, Error **errp)
1494{
1495 Error *local_err = NULL;
1496
1497 if (resume) {
1498 if (s->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
1499 error_setg(errp, "Cannot resume if there is no "
1500 "paused migration");
1501 return false;
1502 }
1503 /* This is a resume, skip init status */
1504 return true;
1505 }
1506
1507 if (migration_is_setup_or_active(s->state) ||
1508 s->state == MIGRATION_STATUS_CANCELLING ||
1509 s->state == MIGRATION_STATUS_COLO) {
1510 error_setg(errp, QERR_MIGRATION_ACTIVE);
1511 return false;
1512 }
1513
1514 if (runstate_check(RUN_STATE_INMIGRATE)) {
1515 error_setg(errp, "Guest is waiting for an incoming migration");
1516 return false;
1517 }
1518
1519 if (migration_is_blocked(errp)) {
1520 return false;
1521 }
1522
1523 if (blk || blk_inc) {
1524 if (migrate_use_block() || migrate_use_block_incremental()) {
1525 error_setg(errp, "Command options are incompatible with "
1526 "current migration capabilities");
1527 return false;
1528 }
1529 migrate_set_block_enabled(true, &local_err);
1530 if (local_err) {
1531 error_propagate(errp, local_err);
1532 return false;
1533 }
1534 s->must_remove_block_options = true;
1535 }
1536
1537 if (blk_inc) {
1538 migrate_set_block_incremental(s, true);
1539 }
1540
1541 migrate_init(s);
1542
1543 return true;
1544}
1545
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001546void qmp_migrate(const char *uri, bool has_blk, bool blk,
1547 bool has_inc, bool inc, bool has_detach, bool detach,
Peter Xu7a4da282018-05-02 18:47:23 +08001548 bool has_resume, bool resume, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001549{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +02001550 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +02001551 MigrationState *s = migrate_get_current();
Juan Quintelacab30142011-02-22 23:54:21 +01001552 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +01001553
Peter Xud3e35b82018-05-02 18:47:24 +08001554 if (!migrate_prepare(s, has_blk && blk, has_inc && inc,
1555 has_resume && resume, errp)) {
1556 /* Error detected, put into errp */
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001557 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001558 }
Juan Quintelacab30142011-02-22 23:54:21 +01001559
1560 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001561 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04001562#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08001563 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04001564 rdma_start_outgoing_migration(s, p, &local_err);
1565#endif
Juan Quintelacab30142011-02-22 23:54:21 +01001566 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001567 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001568 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001569 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001570 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001571 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001572 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001573 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1574 "a valid migration protocol");
zhanghailiang48781e52015-12-16 11:47:33 +00001575 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1576 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09576e72018-03-16 20:21:14 +00001577 block_cleanup_parameters(s);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001578 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001579 }
1580
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001581 if (local_err) {
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001582 migrate_fd_error(s, local_err);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001583 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001584 return;
Juan Quintela1299c632011-11-09 21:29:01 +01001585 }
Juan Quintelacab30142011-02-22 23:54:21 +01001586}
1587
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02001588void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001589{
Juan Quintela17549e82011-10-05 13:50:43 +02001590 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01001591}
1592
Dr. David Alan Gilbert89cfc022017-10-20 10:05:53 +01001593void qmp_migrate_continue(MigrationStatus state, Error **errp)
1594{
1595 MigrationState *s = migrate_get_current();
1596 if (s->state != state) {
1597 error_setg(errp, "Migration not in expected state: %s",
1598 MigrationStatus_str(s->state));
1599 return;
1600 }
1601 qemu_sem_post(&s->pause_sem);
1602}
1603
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001604void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1605{
Juan Quintela73af8dd2017-10-05 21:30:10 +02001606 MigrateSetParameters p = {
1607 .has_xbzrle_cache_size = true,
1608 .xbzrle_cache_size = value,
1609 };
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001610
Juan Quintela73af8dd2017-10-05 21:30:10 +02001611 qmp_migrate_set_parameters(&p, errp);
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001612}
1613
1614int64_t qmp_query_migrate_cache_size(Error **errp)
1615{
1616 return migrate_xbzrle_cache_size();
1617}
1618
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001619void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001620{
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001621 MigrateSetParameters p = {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301622 .has_max_bandwidth = true,
1623 .max_bandwidth = value,
1624 };
Juan Quintelacab30142011-02-22 23:54:21 +01001625
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301626 qmp_migrate_set_parameters(&p, errp);
Juan Quintelacab30142011-02-22 23:54:21 +01001627}
1628
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001629void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001630{
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03001631 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
1632 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
1633 "the range of 0 to %d seconds",
1634 MAX_MIGRATE_DOWNTIME_SECONDS);
1635 return;
1636 }
1637
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301638 value *= 1000; /* Convert to milliseconds */
1639 value = MAX(0, MIN(INT64_MAX, value));
1640
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001641 MigrateSetParameters p = {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301642 .has_downtime_limit = true,
1643 .downtime_limit = value,
1644 };
1645
1646 qmp_migrate_set_parameters(&p, errp);
aliguori5bb79102008-10-13 03:12:02 +00001647}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001648
Pavel Butsykin53f09a12017-02-03 18:23:20 +03001649bool migrate_release_ram(void)
1650{
1651 MigrationState *s;
1652
1653 s = migrate_get_current();
1654
1655 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
1656}
1657
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001658bool migrate_postcopy_ram(void)
1659{
1660 MigrationState *s;
1661
1662 s = migrate_get_current();
1663
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +00001664 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001665}
1666
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03001667bool migrate_postcopy(void)
1668{
Vladimir Sementsov-Ogievskiydd6bb912018-03-13 15:34:00 -04001669 return migrate_postcopy_ram() || migrate_dirty_bitmaps();
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03001670}
1671
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06001672bool migrate_auto_converge(void)
1673{
1674 MigrationState *s;
1675
1676 s = migrate_get_current();
1677
1678 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1679}
1680
Peter Lieven323004a2013-07-18 09:48:50 +02001681bool migrate_zero_blocks(void)
1682{
1683 MigrationState *s;
1684
1685 s = migrate_get_current();
1686
1687 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1688}
1689
Alexey Perevalovf22f9282018-03-22 21:17:22 +03001690bool migrate_postcopy_blocktime(void)
1691{
1692 MigrationState *s;
1693
1694 s = migrate_get_current();
1695
1696 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME];
1697}
1698
Liang Li8706d2d2015-03-23 16:32:17 +08001699bool migrate_use_compression(void)
1700{
Liang Lidde4e692015-03-23 16:32:26 +08001701 MigrationState *s;
1702
1703 s = migrate_get_current();
1704
1705 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08001706}
1707
1708int migrate_compress_level(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_level;
Liang Li8706d2d2015-03-23 16:32:17 +08001715}
1716
1717int migrate_compress_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.compress_threads;
Liang Li8706d2d2015-03-23 16:32:17 +08001724}
1725
Liang Li3fcb38c2015-03-23 16:32:18 +08001726int migrate_decompress_threads(void)
1727{
1728 MigrationState *s;
1729
1730 s = migrate_get_current();
1731
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001732 return s->parameters.decompress_threads;
Liang Li3fcb38c2015-03-23 16:32:18 +08001733}
1734
Vladimir Sementsov-Ogievskiy55efc8c2018-03-13 15:34:00 -04001735bool migrate_dirty_bitmaps(void)
1736{
1737 MigrationState *s;
1738
1739 s = migrate_get_current();
1740
1741 return s->enabled_capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS];
1742}
1743
Juan Quintelab05dc722015-07-07 14:44:05 +02001744bool migrate_use_events(void)
1745{
1746 MigrationState *s;
1747
1748 s = migrate_get_current();
1749
1750 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1751}
1752
Juan Quintela30126bb2016-01-14 12:23:00 +01001753bool migrate_use_multifd(void)
1754{
1755 MigrationState *s;
1756
1757 s = migrate_get_current();
1758
1759 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_MULTIFD];
1760}
1761
Dr. David Alan Gilbert93fbd032017-10-20 10:05:50 +01001762bool migrate_pause_before_switchover(void)
1763{
1764 MigrationState *s;
1765
1766 s = migrate_get_current();
1767
1768 return s->enabled_capabilities[
1769 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER];
1770}
1771
Juan Quintela4075fb12016-01-15 08:56:17 +01001772int migrate_multifd_channels(void)
1773{
1774 MigrationState *s;
1775
1776 s = migrate_get_current();
1777
1778 return s->parameters.x_multifd_channels;
1779}
1780
Juan Quintela0fb86602017-04-27 10:48:25 +02001781int migrate_multifd_page_count(void)
1782{
1783 MigrationState *s;
1784
1785 s = migrate_get_current();
1786
1787 return s->parameters.x_multifd_page_count;
1788}
1789
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001790int migrate_use_xbzrle(void)
1791{
1792 MigrationState *s;
1793
1794 s = migrate_get_current();
1795
1796 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1797}
1798
1799int64_t migrate_xbzrle_cache_size(void)
1800{
1801 MigrationState *s;
1802
1803 s = migrate_get_current();
1804
Juan Quintela73af8dd2017-10-05 21:30:10 +02001805 return s->parameters.xbzrle_cache_size;
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001806}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001807
Juan Quintela2833c592017-04-05 18:32:37 +02001808bool migrate_use_block(void)
1809{
1810 MigrationState *s;
1811
1812 s = migrate_get_current();
1813
1814 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
1815}
1816
Peter Xuc788ada2017-06-26 18:28:55 +08001817bool migrate_use_return_path(void)
1818{
1819 MigrationState *s;
1820
1821 s = migrate_get_current();
1822
1823 return s->enabled_capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
1824}
1825
Juan Quintela2833c592017-04-05 18:32:37 +02001826bool migrate_use_block_incremental(void)
1827{
1828 MigrationState *s;
1829
1830 s = migrate_get_current();
1831
1832 return s->parameters.block_incremental;
1833}
1834
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001835/* migration thread support */
1836/*
1837 * Something bad happened to the RP stream, mark an error
1838 * The caller shall print or trace something to indicate why
1839 */
1840static void mark_source_rp_bad(MigrationState *s)
1841{
1842 s->rp_state.error = true;
1843}
1844
1845static struct rp_cmd_args {
1846 ssize_t len; /* -1 = variable */
1847 const char *name;
1848} rp_cmd_args[] = {
1849 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1850 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1851 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001852 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1853 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Peter Xua335deb2018-05-02 18:47:28 +08001854 [MIG_RP_MSG_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" },
Peter Xu13955b82018-05-02 18:47:30 +08001855 [MIG_RP_MSG_RESUME_ACK] = { .len = 4, .name = "RESUME_ACK" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001856 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1857};
1858
1859/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001860 * Process a request for pages received on the return path,
1861 * We're allowed to send more than requested (e.g. to round to our page size)
1862 * and we don't need to send pages that have already been sent.
1863 */
1864static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1865 ram_addr_t start, size_t len)
1866{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001867 long our_host_ps = getpagesize();
1868
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001869 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001870
1871 /*
1872 * Since we currently insist on matching page sizes, just sanity check
1873 * we're being asked for whole host pages.
1874 */
1875 if (start & (our_host_ps-1) ||
1876 (len & (our_host_ps-1))) {
1877 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1878 " len: %zd", __func__, start, len);
1879 mark_source_rp_bad(ms);
1880 return;
1881 }
1882
Juan Quintela96506892017-03-14 18:41:03 +01001883 if (ram_save_queue_pages(rbname, start, len)) {
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001884 mark_source_rp_bad(ms);
1885 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001886}
1887
Peter Xu14b17422018-05-02 18:47:21 +08001888/* Return true to retry, false to quit */
1889static bool postcopy_pause_return_path_thread(MigrationState *s)
1890{
1891 trace_postcopy_pause_return_path();
1892
1893 qemu_sem_wait(&s->postcopy_pause_rp_sem);
1894
1895 trace_postcopy_pause_return_path_continued();
1896
1897 return true;
1898}
1899
Peter Xua335deb2018-05-02 18:47:28 +08001900static int migrate_handle_rp_recv_bitmap(MigrationState *s, char *block_name)
1901{
1902 RAMBlock *block = qemu_ram_block_by_name(block_name);
1903
1904 if (!block) {
1905 error_report("%s: invalid block name '%s'", __func__, block_name);
1906 return -EINVAL;
1907 }
1908
1909 /* Fetch the received bitmap and refresh the dirty bitmap */
1910 return ram_dirty_bitmap_reload(s, block);
1911}
1912
Peter Xu13955b82018-05-02 18:47:30 +08001913static int migrate_handle_rp_resume_ack(MigrationState *s, uint32_t value)
1914{
1915 trace_source_return_path_thread_resume_ack(value);
1916
1917 if (value != MIGRATION_RESUME_ACK_VALUE) {
1918 error_report("%s: illegal resume_ack value %"PRIu32,
1919 __func__, value);
1920 return -1;
1921 }
1922
1923 /* Now both sides are active. */
1924 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
1925 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1926
1927 /* TODO: notify send thread that time to continue send pages */
1928
1929 return 0;
1930}
1931
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001932/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001933 * Handles messages sent on the return path towards the source VM
1934 *
1935 */
1936static void *source_return_path_thread(void *opaque)
1937{
1938 MigrationState *ms = opaque;
1939 QEMUFile *rp = ms->rp_state.from_dst_file;
1940 uint16_t header_len, header_type;
Peter Xu568b01c2016-03-09 14:12:12 +08001941 uint8_t buf[512];
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001942 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001943 ram_addr_t start = 0; /* =0 to silence warning */
1944 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001945 int res;
1946
1947 trace_source_return_path_thread_entry();
Peter Xu14b17422018-05-02 18:47:21 +08001948
1949retry:
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001950 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1951 migration_is_setup_or_active(ms->state)) {
1952 trace_source_return_path_thread_loop_top();
1953 header_type = qemu_get_be16(rp);
1954 header_len = qemu_get_be16(rp);
1955
Peter Xu7a9ddfb2018-02-08 18:31:05 +08001956 if (qemu_file_get_error(rp)) {
1957 mark_source_rp_bad(ms);
1958 goto out;
1959 }
1960
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001961 if (header_type >= MIG_RP_MSG_MAX ||
1962 header_type == MIG_RP_MSG_INVALID) {
1963 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1964 header_type, header_len);
1965 mark_source_rp_bad(ms);
1966 goto out;
1967 }
1968
1969 if ((rp_cmd_args[header_type].len != -1 &&
1970 header_len != rp_cmd_args[header_type].len) ||
Peter Xu568b01c2016-03-09 14:12:12 +08001971 header_len > sizeof(buf)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001972 error_report("RP: Received '%s' message (0x%04x) with"
1973 "incorrect length %d expecting %zu",
1974 rp_cmd_args[header_type].name, header_type, header_len,
1975 (size_t)rp_cmd_args[header_type].len);
1976 mark_source_rp_bad(ms);
1977 goto out;
1978 }
1979
1980 /* We know we've got a valid header by this point */
1981 res = qemu_get_buffer(rp, buf, header_len);
1982 if (res != header_len) {
1983 error_report("RP: Failed reading data for message 0x%04x"
1984 " read %d expected %d",
1985 header_type, res, header_len);
1986 mark_source_rp_bad(ms);
1987 goto out;
1988 }
1989
1990 /* OK, we have the message and the data */
1991 switch (header_type) {
1992 case MIG_RP_MSG_SHUT:
Peter Maydell4d885132016-06-10 17:09:22 +01001993 sibling_error = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001994 trace_source_return_path_thread_shut(sibling_error);
1995 if (sibling_error) {
1996 error_report("RP: Sibling indicated error %d", sibling_error);
1997 mark_source_rp_bad(ms);
1998 }
1999 /*
2000 * We'll let the main thread deal with closing the RP
2001 * we could do a shutdown(2) on it, but we're the only user
2002 * anyway, so there's nothing gained.
2003 */
2004 goto out;
2005
2006 case MIG_RP_MSG_PONG:
Peter Maydell4d885132016-06-10 17:09:22 +01002007 tmp32 = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002008 trace_source_return_path_thread_pong(tmp32);
2009 break;
2010
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002011 case MIG_RP_MSG_REQ_PAGES:
Peter Maydell4d885132016-06-10 17:09:22 +01002012 start = ldq_be_p(buf);
2013 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002014 migrate_handle_rp_req_pages(ms, NULL, start, len);
2015 break;
2016
2017 case MIG_RP_MSG_REQ_PAGES_ID:
2018 expected_len = 12 + 1; /* header + termination */
2019
2020 if (header_len >= expected_len) {
Peter Maydell4d885132016-06-10 17:09:22 +01002021 start = ldq_be_p(buf);
2022 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00002023 /* Now we expect an idstr */
2024 tmp32 = buf[12]; /* Length of the following idstr */
2025 buf[13 + tmp32] = '\0';
2026 expected_len += tmp32;
2027 }
2028 if (header_len != expected_len) {
2029 error_report("RP: Req_Page_id with length %d expecting %zd",
2030 header_len, expected_len);
2031 mark_source_rp_bad(ms);
2032 goto out;
2033 }
2034 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
2035 break;
2036
Peter Xua335deb2018-05-02 18:47:28 +08002037 case MIG_RP_MSG_RECV_BITMAP:
2038 if (header_len < 1) {
2039 error_report("%s: missing block name", __func__);
2040 mark_source_rp_bad(ms);
2041 goto out;
2042 }
2043 /* Format: len (1B) + idstr (<255B). This ends the idstr. */
2044 buf[buf[0] + 1] = '\0';
2045 if (migrate_handle_rp_recv_bitmap(ms, (char *)(buf + 1))) {
2046 mark_source_rp_bad(ms);
2047 goto out;
2048 }
2049 break;
2050
Peter Xu13955b82018-05-02 18:47:30 +08002051 case MIG_RP_MSG_RESUME_ACK:
2052 tmp32 = ldl_be_p(buf);
2053 if (migrate_handle_rp_resume_ack(ms, tmp32)) {
2054 mark_source_rp_bad(ms);
2055 goto out;
2056 }
2057 break;
2058
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002059 default:
2060 break;
2061 }
2062 }
Peter Xu14b17422018-05-02 18:47:21 +08002063
2064out:
2065 res = qemu_file_get_error(rp);
2066 if (res) {
2067 if (res == -EIO) {
2068 /*
2069 * Maybe there is something we can do: it looks like a
2070 * network down issue, and we pause for a recovery.
2071 */
2072 if (postcopy_pause_return_path_thread(ms)) {
2073 /* Reload rp, reset the rest */
2074 rp = ms->rp_state.from_dst_file;
2075 ms->rp_state.error = false;
2076 goto retry;
2077 }
2078 }
2079
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002080 trace_source_return_path_thread_bad_end();
2081 mark_source_rp_bad(ms);
2082 }
2083
2084 trace_source_return_path_thread_end();
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002085 ms->rp_state.from_dst_file = NULL;
2086 qemu_fclose(rp);
2087 return NULL;
2088}
2089
Peter Xud3e35b82018-05-02 18:47:24 +08002090static int open_return_path_on_source(MigrationState *ms,
2091 bool create_thread)
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002092{
2093
zhanghailiang89a02a92016-01-15 11:37:42 +08002094 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002095 if (!ms->rp_state.from_dst_file) {
2096 return -1;
2097 }
2098
2099 trace_open_return_path_on_source();
Peter Xud3e35b82018-05-02 18:47:24 +08002100
2101 if (!create_thread) {
2102 /* We're done */
2103 return 0;
2104 }
2105
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002106 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
2107 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
2108
2109 trace_open_return_path_on_source_continue();
2110
2111 return 0;
2112}
2113
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002114/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
2115static int await_return_path_close_on_source(MigrationState *ms)
2116{
2117 /*
2118 * If this is a normal exit then the destination will send a SHUT and the
2119 * rp_thread will exit, however if there's an error we need to cause
2120 * it to exit.
2121 */
zhanghailiang89a02a92016-01-15 11:37:42 +08002122 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002123 /*
2124 * shutdown(2), if we have it, will cause it to unblock if it's stuck
2125 * waiting for the destination.
2126 */
2127 qemu_file_shutdown(ms->rp_state.from_dst_file);
2128 mark_source_rp_bad(ms);
2129 }
2130 trace_await_return_path_close_on_source_joining();
2131 qemu_thread_join(&ms->rp_state.rp_thread);
2132 trace_await_return_path_close_on_source_close();
2133 return ms->rp_state.error;
2134}
2135
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002136/*
2137 * Switch from normal iteration to postcopy
2138 * Returns non-0 on error
2139 */
Peter Xu7287cbd2018-01-03 20:20:09 +08002140static int postcopy_start(MigrationState *ms)
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002141{
2142 int ret;
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002143 QIOChannelBuffer *bioc;
2144 QEMUFile *fb;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002145 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002146 bool restart_block = false;
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002147 int cur_state = MIGRATION_STATUS_ACTIVE;
2148 if (!migrate_pause_before_switchover()) {
2149 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
2150 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2151 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002152
2153 trace_postcopy_start();
2154 qemu_mutex_lock_iothread();
2155 trace_postcopy_start_set_run();
2156
2157 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002158 global_state_store();
2159 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01002160 if (ret < 0) {
2161 goto fail;
2162 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002163
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002164 ret = migration_maybe_pause(ms, &cur_state,
2165 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2166 if (ret < 0) {
2167 goto fail;
2168 }
2169
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01002170 ret = bdrv_inactivate_all();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002171 if (ret < 0) {
2172 goto fail;
2173 }
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002174 restart_block = true;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002175
2176 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00002177 * Cause any non-postcopiable, but iterative devices to
2178 * send out their final data.
2179 */
Fam Zhenga1fbe752017-06-17 00:06:58 +08002180 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00002181
2182 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002183 * in Finish migrate and with the io-lock held everything should
2184 * be quiet, but we've potentially still got dirty pages and we
2185 * need to tell the destination to throw any pages it's already received
2186 * that are dirty
2187 */
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002188 if (migrate_postcopy_ram()) {
2189 if (ram_postcopy_send_discard_bitmap(ms)) {
2190 error_report("postcopy send discard bitmap failed");
2191 goto fail;
2192 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002193 }
2194
2195 /*
2196 * send rest of state - note things that are doing postcopy
2197 * will notice we're in POSTCOPY_ACTIVE and not actually
2198 * wrap their state up here
2199 */
zhanghailiang89a02a92016-01-15 11:37:42 +08002200 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002201 if (migrate_postcopy_ram()) {
2202 /* Ping just for debugging, helps line traces up */
2203 qemu_savevm_send_ping(ms->to_dst_file, 2);
2204 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002205
2206 /*
2207 * While loading the device state we may trigger page transfer
2208 * requests and the fd must be free to process those, and thus
2209 * the destination must read the whole device state off the fd before
2210 * it starts processing it. Unfortunately the ad-hoc migration format
2211 * doesn't allow the destination to know the size to read without fully
2212 * parsing it through each devices load-state code (especially the open
2213 * coded devices that use get/put).
2214 * So we wrap the device state up in a package with a length at the start;
2215 * to do this we use a qemu_buf to hold the whole of the device state.
2216 */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002217 bioc = qio_channel_buffer_new(4096);
Daniel P. Berrange6f01f132016-09-30 11:57:14 +01002218 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002219 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
2220 object_unref(OBJECT(bioc));
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002221
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00002222 /*
2223 * Make sure the receiver can get incoming pages before we send the rest
2224 * of the state
2225 */
2226 qemu_savevm_send_postcopy_listen(fb);
2227
Fam Zhenga1fbe752017-06-17 00:06:58 +08002228 qemu_savevm_state_complete_precopy(fb, false, false);
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002229 if (migrate_postcopy_ram()) {
2230 qemu_savevm_send_ping(fb, 3);
2231 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002232
2233 qemu_savevm_send_postcopy_run(fb);
2234
2235 /* <><> end of stuff going into the package */
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002236
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002237 /* Last point of recovery; as soon as we send the package the destination
2238 * can open devices and potentially start running.
2239 * Lets just check again we've not got any errors.
2240 */
2241 ret = qemu_file_get_error(ms->to_dst_file);
2242 if (ret) {
2243 error_report("postcopy_start: Migration stream errored (pre package)");
2244 goto fail_closefb;
2245 }
2246
2247 restart_block = false;
2248
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002249 /* Now send that blob */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002250 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002251 goto fail_closefb;
2252 }
2253 qemu_fclose(fb);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00002254
2255 /* Send a notify to give a chance for anything that needs to happen
2256 * at the transition to postcopy and after the device state; in particular
2257 * spice needs to trigger a transition now
2258 */
2259 ms->postcopy_after_devices = true;
2260 notifier_list_notify(&migration_state_notifiers, ms);
2261
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002262 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
2263
2264 qemu_mutex_unlock_iothread();
2265
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002266 if (migrate_postcopy_ram()) {
2267 /*
2268 * Although this ping is just for debug, it could potentially be
2269 * used for getting a better measurement of downtime at the source.
2270 */
2271 qemu_savevm_send_ping(ms->to_dst_file, 4);
2272 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002273
Pavel Butsykinced1c612017-02-03 18:23:21 +03002274 if (migrate_release_ram()) {
2275 ram_postcopy_migrated_memory_release(ms);
2276 }
2277
zhanghailiang89a02a92016-01-15 11:37:42 +08002278 ret = qemu_file_get_error(ms->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002279 if (ret) {
2280 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00002281 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002282 MIGRATION_STATUS_FAILED);
2283 }
2284
2285 return ret;
2286
2287fail_closefb:
2288 qemu_fclose(fb);
2289fail:
zhanghailiang48781e52015-12-16 11:47:33 +00002290 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002291 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00002292 if (restart_block) {
2293 /* A failure happened early enough that we know the destination hasn't
2294 * accessed block devices, so we're safe to recover.
2295 */
2296 Error *local_err = NULL;
2297
2298 bdrv_invalidate_cache_all(&local_err);
2299 if (local_err) {
2300 error_report_err(local_err);
2301 }
2302 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002303 qemu_mutex_unlock_iothread();
2304 return -1;
2305}
2306
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002307/**
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002308 * migration_maybe_pause: Pause if required to by
2309 * migrate_pause_before_switchover called with the iothread locked
2310 * Returns: 0 on success
2311 */
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002312static int migration_maybe_pause(MigrationState *s,
2313 int *current_active_state,
2314 int new_state)
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002315{
2316 if (!migrate_pause_before_switchover()) {
2317 return 0;
2318 }
2319
2320 /* Since leaving this state is not atomic with posting the semaphore
2321 * it's possible that someone could have issued multiple migrate_continue
2322 * and the semaphore is incorrectly positive at this point;
2323 * the docs say it's undefined to reinit a semaphore that's already
2324 * init'd, so use timedwait to eat up any existing posts.
2325 */
2326 while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) {
2327 /* This block intentionally left blank */
2328 }
2329
2330 qemu_mutex_unlock_iothread();
2331 migrate_set_state(&s->state, *current_active_state,
2332 MIGRATION_STATUS_PRE_SWITCHOVER);
2333 qemu_sem_wait(&s->pause_sem);
2334 migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER,
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002335 new_state);
2336 *current_active_state = new_state;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002337 qemu_mutex_lock_iothread();
2338
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002339 return s->state == new_state ? 0 : -EINVAL;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002340}
2341
2342/**
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002343 * migration_completion: Used by migration_thread when there's not much left.
2344 * The caller 'breaks' the loop when this returns.
2345 *
2346 * @s: Current migration state
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002347 */
Peter Xu2ad87302018-01-03 20:20:14 +08002348static void migration_completion(MigrationState *s)
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002349{
2350 int ret;
Peter Xu2ad87302018-01-03 20:20:14 +08002351 int current_active_state = s->state;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002352
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002353 if (s->state == MIGRATION_STATUS_ACTIVE) {
2354 qemu_mutex_lock_iothread();
Peter Xu64909f92018-01-03 20:20:10 +08002355 s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002356 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
Peter Xu7287cbd2018-01-03 20:20:09 +08002357 s->vm_was_running = runstate_is_running();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002358 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002359
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002360 if (!ret) {
Fam Zhenga1fbe752017-06-17 00:06:58 +08002361 bool inactivate = !migrate_colo_enabled();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002362 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02002363 if (ret >= 0) {
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002364 ret = migration_maybe_pause(s, &current_active_state,
2365 MIGRATION_STATUS_DEVICE);
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002366 }
2367 if (ret >= 0) {
Kevin Wolff07fa4c2017-05-22 17:10:38 +02002368 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
Fam Zhenga1fbe752017-06-17 00:06:58 +08002369 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
2370 inactivate);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02002371 }
Fam Zhenga1fbe752017-06-17 00:06:58 +08002372 if (inactivate && ret >= 0) {
2373 s->block_inactive = true;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002374 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002375 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002376 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002377
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002378 if (ret < 0) {
2379 goto fail;
2380 }
2381 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2382 trace_migration_completion_postcopy_end();
2383
zhanghailiang89a02a92016-01-15 11:37:42 +08002384 qemu_savevm_state_complete_postcopy(s->to_dst_file);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002385 trace_migration_completion_postcopy_end_after_complete();
2386 }
2387
2388 /*
2389 * If rp was opened we must clean up the thread before
2390 * cleaning everything else up (since if there are no failures
2391 * it will wait for the destination to send it's status in
2392 * a SHUT command).
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002393 */
Peter Xu0425dc92017-05-31 18:35:34 +08002394 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002395 int rp_error;
Peter Xu0425dc92017-05-31 18:35:34 +08002396 trace_migration_return_path_end_before();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002397 rp_error = await_return_path_close_on_source(s);
Peter Xu0425dc92017-05-31 18:35:34 +08002398 trace_migration_return_path_end_after(rp_error);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002399 if (rp_error) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02002400 goto fail_invalidate;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002401 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002402 }
2403
zhanghailiang89a02a92016-01-15 11:37:42 +08002404 if (qemu_file_get_error(s->to_dst_file)) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002405 trace_migration_completion_file_err();
Greg Kurzfe904ea2016-05-18 15:44:36 +02002406 goto fail_invalidate;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002407 }
2408
zhanghailiang0b827d52016-10-27 14:42:54 +08002409 if (!migrate_colo_enabled()) {
2410 migrate_set_state(&s->state, current_active_state,
2411 MIGRATION_STATUS_COMPLETED);
2412 }
2413
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002414 return;
2415
Greg Kurzfe904ea2016-05-18 15:44:36 +02002416fail_invalidate:
2417 /* If not doing postcopy, vm_start() will be called: let's regain
2418 * control on images.
2419 */
Dr. David Alan Gilbert6039dd52018-02-05 09:13:37 +00002420 if (s->state == MIGRATION_STATUS_ACTIVE ||
2421 s->state == MIGRATION_STATUS_DEVICE) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02002422 Error *local_err = NULL;
2423
zhanghailiang1d2acc32017-01-24 15:59:52 +08002424 qemu_mutex_lock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02002425 bdrv_invalidate_cache_all(&local_err);
2426 if (local_err) {
2427 error_report_err(local_err);
zhanghailiang1d2acc32017-01-24 15:59:52 +08002428 } else {
2429 s->block_inactive = false;
Greg Kurzfe904ea2016-05-18 15:44:36 +02002430 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08002431 qemu_mutex_unlock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02002432 }
2433
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002434fail:
zhanghailiang48781e52015-12-16 11:47:33 +00002435 migrate_set_state(&s->state, current_active_state,
2436 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002437}
2438
zhanghailiang35a6ed42016-10-27 14:42:52 +08002439bool migrate_colo_enabled(void)
2440{
2441 MigrationState *s = migrate_get_current();
2442 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
2443}
2444
Peter Xub23c2ad2018-05-02 18:47:19 +08002445typedef enum MigThrError {
2446 /* No error detected */
2447 MIG_THR_ERR_NONE = 0,
2448 /* Detected error, but resumed successfully */
2449 MIG_THR_ERR_RECOVERED = 1,
2450 /* Detected fatal error, need to exit */
2451 MIG_THR_ERR_FATAL = 2,
2452} MigThrError;
2453
Peter Xu135b87b2018-05-02 18:47:25 +08002454/* Return zero if success, or <0 for error */
2455static int postcopy_do_resume(MigrationState *s)
2456{
2457 /* TODO: do the resume logic */
2458 return 0;
2459}
2460
Peter Xub23c2ad2018-05-02 18:47:19 +08002461/*
2462 * We don't return until we are in a safe state to continue current
2463 * postcopy migration. Returns MIG_THR_ERR_RECOVERED if recovered, or
2464 * MIG_THR_ERR_FATAL if unrecovery failure happened.
2465 */
2466static MigThrError postcopy_pause(MigrationState *s)
2467{
2468 assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
Peter Xub23c2ad2018-05-02 18:47:19 +08002469
Peter Xu135b87b2018-05-02 18:47:25 +08002470 while (true) {
2471 migrate_set_state(&s->state, s->state,
2472 MIGRATION_STATUS_POSTCOPY_PAUSED);
Peter Xub23c2ad2018-05-02 18:47:19 +08002473
Peter Xu135b87b2018-05-02 18:47:25 +08002474 /* Current channel is possibly broken. Release it. */
2475 assert(s->to_dst_file);
2476 qemu_file_shutdown(s->to_dst_file);
2477 qemu_fclose(s->to_dst_file);
2478 s->to_dst_file = NULL;
Peter Xub23c2ad2018-05-02 18:47:19 +08002479
Peter Xu135b87b2018-05-02 18:47:25 +08002480 error_report("Detected IO failure for postcopy. "
2481 "Migration paused.");
2482
2483 /*
2484 * We wait until things fixed up. Then someone will setup the
2485 * status back for us.
2486 */
2487 while (s->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
2488 qemu_sem_wait(&s->postcopy_pause_sem);
2489 }
2490
2491 if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
2492 /* Woken up by a recover procedure. Give it a shot */
2493
2494 /*
2495 * Firstly, let's wake up the return path now, with a new
2496 * return path channel.
2497 */
2498 qemu_sem_post(&s->postcopy_pause_rp_sem);
2499
2500 /* Do the resume logic */
2501 if (postcopy_do_resume(s) == 0) {
2502 /* Let's continue! */
2503 trace_postcopy_pause_continued();
2504 return MIG_THR_ERR_RECOVERED;
2505 } else {
2506 /*
2507 * Something wrong happened during the recovery, let's
2508 * pause again. Pause is always better than throwing
2509 * data away.
2510 */
2511 continue;
2512 }
2513 } else {
2514 /* This is not right... Time to quit. */
2515 return MIG_THR_ERR_FATAL;
2516 }
Peter Xub23c2ad2018-05-02 18:47:19 +08002517 }
Peter Xub23c2ad2018-05-02 18:47:19 +08002518}
2519
2520static MigThrError migration_detect_error(MigrationState *s)
2521{
2522 int ret;
2523
2524 /* Try to detect any file errors */
2525 ret = qemu_file_get_error(s->to_dst_file);
2526
2527 if (!ret) {
2528 /* Everything is fine */
2529 return MIG_THR_ERR_NONE;
2530 }
2531
2532 if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE && ret == -EIO) {
2533 /*
2534 * For postcopy, we allow the network to be down for a
2535 * while. After that, it can be continued by a
2536 * recovery phase.
2537 */
2538 return postcopy_pause(s);
2539 } else {
2540 /*
2541 * For precopy (or postcopy with error outside IO), we fail
2542 * with no time.
2543 */
2544 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
2545 trace_migration_thread_file_err();
2546
2547 /* Time to stop the migration, now. */
2548 return MIG_THR_ERR_FATAL;
2549 }
2550}
2551
Peter Xucf011f02018-01-03 20:20:11 +08002552static void migration_calculate_complete(MigrationState *s)
2553{
2554 uint64_t bytes = qemu_ftell(s->to_dst_file);
2555 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2556
2557 s->total_time = end_time - s->start_time;
2558 if (!s->downtime) {
2559 /*
2560 * It's still not set, so we are precopy migration. For
2561 * postcopy, downtime is calculated during postcopy_start().
2562 */
2563 s->downtime = end_time - s->downtime_start;
2564 }
2565
2566 if (s->total_time) {
2567 s->mbps = ((double) bytes * 8.0) / s->total_time / 1000;
2568 }
2569}
2570
Peter Xub15df1a2018-01-03 20:20:13 +08002571static void migration_update_counters(MigrationState *s,
2572 int64_t current_time)
2573{
2574 uint64_t transferred, time_spent;
Peter Xub15df1a2018-01-03 20:20:13 +08002575 double bandwidth;
2576
2577 if (current_time < s->iteration_start_time + BUFFER_DELAY) {
2578 return;
2579 }
2580
2581 transferred = qemu_ftell(s->to_dst_file) - s->iteration_initial_bytes;
2582 time_spent = current_time - s->iteration_start_time;
2583 bandwidth = (double)transferred / time_spent;
Wei Wang0781c1e2018-01-22 19:36:39 +08002584 s->threshold_size = bandwidth * s->parameters.downtime_limit;
Peter Xub15df1a2018-01-03 20:20:13 +08002585
2586 s->mbps = (((double) transferred * 8.0) /
2587 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
2588
2589 /*
2590 * if we haven't sent anything, we don't want to
2591 * recalculate. 10000 is a small enough number for our purposes
2592 */
2593 if (ram_counters.dirty_pages_rate && transferred > 10000) {
2594 s->expected_downtime = ram_counters.dirty_pages_rate *
2595 qemu_target_page_size() / bandwidth;
2596 }
2597
2598 qemu_file_reset_rate_limit(s->to_dst_file);
2599
2600 s->iteration_start_time = current_time;
2601 s->iteration_initial_bytes = qemu_ftell(s->to_dst_file);
2602
2603 trace_migrate_transferred(transferred, time_spent,
Wei Wang0781c1e2018-01-22 19:36:39 +08002604 bandwidth, s->threshold_size);
Peter Xub15df1a2018-01-03 20:20:13 +08002605}
2606
Peter Xu2ad87302018-01-03 20:20:14 +08002607/* Migration thread iteration status */
2608typedef enum {
2609 MIG_ITERATE_RESUME, /* Resume current iteration */
2610 MIG_ITERATE_SKIP, /* Skip current iteration */
2611 MIG_ITERATE_BREAK, /* Break the loop */
2612} MigIterateState;
2613
2614/*
2615 * Return true if continue to the next iteration directly, false
2616 * otherwise.
2617 */
2618static MigIterateState migration_iteration_run(MigrationState *s)
2619{
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04002620 uint64_t pending_size, pend_pre, pend_compat, pend_post;
Peter Xu2ad87302018-01-03 20:20:14 +08002621 bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE;
2622
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04002623 qemu_savevm_state_pending(s->to_dst_file, s->threshold_size, &pend_pre,
2624 &pend_compat, &pend_post);
2625 pending_size = pend_pre + pend_compat + pend_post;
Peter Xu2ad87302018-01-03 20:20:14 +08002626
2627 trace_migrate_pending(pending_size, s->threshold_size,
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04002628 pend_pre, pend_compat, pend_post);
Peter Xu2ad87302018-01-03 20:20:14 +08002629
2630 if (pending_size && pending_size >= s->threshold_size) {
2631 /* Still a significant amount to transfer */
2632 if (migrate_postcopy() && !in_postcopy &&
Vladimir Sementsov-Ogievskiy47995022018-03-13 15:34:00 -04002633 pend_pre <= s->threshold_size &&
Peter Xu2ad87302018-01-03 20:20:14 +08002634 atomic_read(&s->start_postcopy)) {
2635 if (postcopy_start(s)) {
2636 error_report("%s: postcopy failed to start", __func__);
2637 }
2638 return MIG_ITERATE_SKIP;
2639 }
2640 /* Just another iteration step */
2641 qemu_savevm_state_iterate(s->to_dst_file,
2642 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
2643 } else {
2644 trace_migration_thread_low_pending(pending_size);
2645 migration_completion(s);
2646 return MIG_ITERATE_BREAK;
2647 }
2648
2649 return MIG_ITERATE_RESUME;
2650}
2651
Peter Xu199aa6d2018-01-03 20:20:15 +08002652static void migration_iteration_finish(MigrationState *s)
2653{
2654 /* If we enabled cpu throttling for auto-converge, turn it off. */
2655 cpu_throttle_stop();
2656
2657 qemu_mutex_lock_iothread();
2658 switch (s->state) {
2659 case MIGRATION_STATUS_COMPLETED:
2660 migration_calculate_complete(s);
2661 runstate_set(RUN_STATE_POSTMIGRATE);
2662 break;
2663
2664 case MIGRATION_STATUS_ACTIVE:
2665 /*
2666 * We should really assert here, but since it's during
2667 * migration, let's try to reduce the usage of assertions.
2668 */
2669 if (!migrate_colo_enabled()) {
2670 error_report("%s: critical error: calling COLO code without "
2671 "COLO enabled", __func__);
2672 }
2673 migrate_start_colo_process(s);
2674 /*
2675 * Fixme: we will run VM in COLO no matter its old running state.
2676 * After exited COLO, we will keep running.
2677 */
2678 s->vm_was_running = true;
2679 /* Fallthrough */
2680 case MIGRATION_STATUS_FAILED:
2681 case MIGRATION_STATUS_CANCELLED:
2682 if (s->vm_was_running) {
2683 vm_start();
2684 } else {
2685 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
2686 runstate_set(RUN_STATE_POSTMIGRATE);
2687 }
2688 }
2689 break;
2690
2691 default:
2692 /* Should not reach here, but if so, forgive the VM. */
2693 error_report("%s: Unknown ending state %d", __func__, s->state);
2694 break;
2695 }
2696 qemu_bh_schedule(s->cleanup_bh);
2697 qemu_mutex_unlock_iothread();
2698}
2699
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002700/*
2701 * Master migration thread on the source VM.
2702 * It drives the migration and pumps the data down the outgoing channel.
2703 */
Juan Quintela5f496a12013-02-22 17:36:30 +01002704static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002705{
Juan Quintela9848a402012-12-19 09:55:50 +01002706 MigrationState *s = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +01002707 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Peter Xub23c2ad2018-05-02 18:47:19 +08002708 MigThrError thr_error;
Juan Quintela76f59332012-10-03 20:16:24 +02002709
Paolo Bonziniab28bd22015-07-09 08:55:38 +02002710 rcu_register_thread();
2711
Peter Xub15df1a2018-01-03 20:20:13 +08002712 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2713
zhanghailiang89a02a92016-01-15 11:37:42 +08002714 qemu_savevm_state_header(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002715
Peter Xu62a02652017-06-14 15:55:58 +08002716 /*
2717 * If we opened the return path, we need to make sure dst has it
2718 * opened as well.
2719 */
2720 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002721 /* Now tell the dest that it should open its end so it can reply */
zhanghailiang89a02a92016-01-15 11:37:42 +08002722 qemu_savevm_send_open_return_path(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002723
2724 /* And do a ping that will make stuff easier to debug */
zhanghailiang89a02a92016-01-15 11:37:42 +08002725 qemu_savevm_send_ping(s->to_dst_file, 1);
Peter Xu0425dc92017-05-31 18:35:34 +08002726 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002727
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002728 if (migrate_postcopy()) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002729 /*
2730 * Tell the destination that we *might* want to do postcopy later;
2731 * if the other end can't do postcopy it should fail now, nice and
2732 * early.
2733 */
zhanghailiang89a02a92016-01-15 11:37:42 +08002734 qemu_savevm_send_postcopy_advise(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002735 }
2736
Juan Quintela9907e842017-06-28 11:52:24 +02002737 qemu_savevm_state_setup(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002738
Alex Blighbc72ad62013-08-21 16:03:08 +01002739 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang48781e52015-12-16 11:47:33 +00002740 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2741 MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04002742
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00002743 trace_migration_thread_setup_complete();
2744
2745 while (s->state == MIGRATION_STATUS_ACTIVE ||
2746 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +01002747 int64_t current_time;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002748
zhanghailiang89a02a92016-01-15 11:37:42 +08002749 if (!qemu_file_rate_limit(s->to_dst_file)) {
Peter Xu2ad87302018-01-03 20:20:14 +08002750 MigIterateState iter_state = migration_iteration_run(s);
2751 if (iter_state == MIG_ITERATE_SKIP) {
2752 continue;
2753 } else if (iter_state == MIG_ITERATE_BREAK) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002754 break;
Juan Quintelac369f402012-10-03 20:33:34 +02002755 }
2756 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01002757
Peter Xub23c2ad2018-05-02 18:47:19 +08002758 /*
2759 * Try to detect any kind of failures, and see whether we
2760 * should stop the migration now.
2761 */
2762 thr_error = migration_detect_error(s);
2763 if (thr_error == MIG_THR_ERR_FATAL) {
2764 /* Stop migration */
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01002765 break;
Peter Xub23c2ad2018-05-02 18:47:19 +08002766 } else if (thr_error == MIG_THR_ERR_RECOVERED) {
2767 /*
2768 * Just recovered from a e.g. network failure, reset all
2769 * the local variables. This is important to avoid
2770 * breaking transferred_bytes and bandwidth calculation
2771 */
2772 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2773 s->iteration_initial_bytes = 0;
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01002774 }
Peter Xub15df1a2018-01-03 20:20:13 +08002775
Alex Blighbc72ad62013-08-21 16:03:08 +01002776 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002777
Peter Xub15df1a2018-01-03 20:20:13 +08002778 migration_update_counters(s, current_time);
Michael R. Hines7e114f82013-06-25 21:35:30 -04002779
zhanghailiang89a02a92016-01-15 11:37:42 +08002780 if (qemu_file_rate_limit(s->to_dst_file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002781 /* usleep expects microseconds */
Peter Xub15df1a2018-01-03 20:20:13 +08002782 g_usleep((s->iteration_start_time + BUFFER_DELAY -
2783 current_time) * 1000);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002784 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002785 }
2786
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002787 trace_migration_thread_after_loop();
Peter Xu199aa6d2018-01-03 20:20:15 +08002788 migration_iteration_finish(s);
Paolo Bonziniab28bd22015-07-09 08:55:38 +02002789 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002790 return NULL;
2791}
2792
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00002793void migrate_fd_connect(MigrationState *s, Error *error_in)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002794{
Peter Xud3e35b82018-05-02 18:47:24 +08002795 int64_t rate_limit;
2796 bool resume = s->state == MIGRATION_STATUS_POSTCOPY_PAUSED;
2797
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302798 s->expected_downtime = s->parameters.downtime_limit;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01002799 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00002800 if (error_in) {
2801 migrate_fd_error(s, error_in);
2802 migrate_fd_cleanup(s);
2803 return;
2804 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002805
Peter Xud3e35b82018-05-02 18:47:24 +08002806 if (resume) {
2807 /* This is a resumed migration */
2808 rate_limit = INT64_MAX;
2809 } else {
2810 /* This is a fresh new migration */
2811 rate_limit = s->parameters.max_bandwidth / XFER_LIMIT_RATIO;
2812 s->expected_downtime = s->parameters.downtime_limit;
2813 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Paolo Bonzini442773c2013-02-22 17:36:44 +01002814
Peter Xud3e35b82018-05-02 18:47:24 +08002815 /* Notify before starting migration thread */
2816 notifier_list_notify(&migration_state_notifiers, s);
2817 }
2818
2819 qemu_file_set_rate_limit(s->to_dst_file, rate_limit);
2820 qemu_file_set_blocking(s->to_dst_file, true);
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02002821
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002822 /*
Peter Xuc788ada2017-06-26 18:28:55 +08002823 * Open the return path. For postcopy, it is used exclusively. For
2824 * precopy, only if user specified "return-path" capability would
2825 * QEMU uses the return path.
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002826 */
Peter Xuc788ada2017-06-26 18:28:55 +08002827 if (migrate_postcopy_ram() || migrate_use_return_path()) {
Peter Xud3e35b82018-05-02 18:47:24 +08002828 if (open_return_path_on_source(s, !resume)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002829 error_report("Unable to open return-path for postcopy");
Peter Xud3e35b82018-05-02 18:47:24 +08002830 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002831 migrate_fd_cleanup(s);
2832 return;
2833 }
2834 }
2835
Peter Xud3e35b82018-05-02 18:47:24 +08002836 if (resume) {
Peter Xu135b87b2018-05-02 18:47:25 +08002837 /* Wakeup the main migration thread to do the recovery */
2838 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
2839 MIGRATION_STATUS_POSTCOPY_RECOVER);
2840 qemu_sem_post(&s->postcopy_pause_sem);
Peter Xud3e35b82018-05-02 18:47:24 +08002841 return;
2842 }
2843
Juan Quintelaf986c3d2016-01-14 16:52:55 +01002844 if (multifd_save_setup() != 0) {
2845 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2846 MIGRATION_STATUS_FAILED);
2847 migrate_fd_cleanup(s);
2848 return;
2849 }
Pankaj Gupta009fad72017-01-23 19:12:56 +05302850 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01002851 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002852 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002853}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00002854
Peter Xu9d18af92017-06-27 12:10:19 +08002855void migration_global_dump(Monitor *mon)
2856{
2857 MigrationState *ms = migrate_get_current();
2858
Juan Quintela6f0f6422017-10-26 11:49:57 +02002859 monitor_printf(mon, "globals:\n");
2860 monitor_printf(mon, "store-global-state: %s\n",
2861 ms->store_global_state ? "on" : "off");
2862 monitor_printf(mon, "only-migratable: %s\n",
2863 ms->only_migratable ? "on" : "off");
2864 monitor_printf(mon, "send-configuration: %s\n",
2865 ms->send_configuration ? "on" : "off");
2866 monitor_printf(mon, "send-section-footer: %s\n",
2867 ms->send_section_footer ? "on" : "off");
Peter Xu9d18af92017-06-27 12:10:19 +08002868}
2869
Peter Xu20814752017-07-18 11:39:03 +08002870#define DEFINE_PROP_MIG_CAP(name, x) \
2871 DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
2872
Peter Xu52722982017-06-27 12:10:14 +08002873static Property migration_properties[] = {
2874 DEFINE_PROP_BOOL("store-global-state", MigrationState,
2875 store_global_state, true),
Peter Xu3df663e2017-06-27 12:10:15 +08002876 DEFINE_PROP_BOOL("only-migratable", MigrationState, only_migratable, false),
Peter Xu71dd4c12017-06-27 12:10:16 +08002877 DEFINE_PROP_BOOL("send-configuration", MigrationState,
2878 send_configuration, true),
Peter Xu15c38502017-06-27 12:10:17 +08002879 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
2880 send_section_footer, true),
Peter Xu89632fa2017-07-18 11:39:02 +08002881
2882 /* Migration parameters */
Juan Quintela741d4082017-12-01 13:08:38 +01002883 DEFINE_PROP_UINT8("x-compress-level", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002884 parameters.compress_level,
2885 DEFAULT_MIGRATE_COMPRESS_LEVEL),
Juan Quintela741d4082017-12-01 13:08:38 +01002886 DEFINE_PROP_UINT8("x-compress-threads", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002887 parameters.compress_threads,
2888 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
Juan Quintela741d4082017-12-01 13:08:38 +01002889 DEFINE_PROP_UINT8("x-decompress-threads", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002890 parameters.decompress_threads,
2891 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
Juan Quintela741d4082017-12-01 13:08:38 +01002892 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002893 parameters.cpu_throttle_initial,
2894 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
Juan Quintela741d4082017-12-01 13:08:38 +01002895 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002896 parameters.cpu_throttle_increment,
2897 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
Juan Quintela741d4082017-12-01 13:08:38 +01002898 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002899 parameters.max_bandwidth, MAX_THROTTLE),
Juan Quintela741d4082017-12-01 13:08:38 +01002900 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002901 parameters.downtime_limit,
2902 DEFAULT_MIGRATE_SET_DOWNTIME),
Juan Quintela741d4082017-12-01 13:08:38 +01002903 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002904 parameters.x_checkpoint_delay,
2905 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
Juan Quintela741d4082017-12-01 13:08:38 +01002906 DEFINE_PROP_UINT8("x-multifd-channels", MigrationState,
Juan Quintela4075fb12016-01-15 08:56:17 +01002907 parameters.x_multifd_channels,
2908 DEFAULT_MIGRATE_MULTIFD_CHANNELS),
Juan Quintela741d4082017-12-01 13:08:38 +01002909 DEFINE_PROP_UINT32("x-multifd-page-count", MigrationState,
Juan Quintela0fb86602017-04-27 10:48:25 +02002910 parameters.x_multifd_page_count,
2911 DEFAULT_MIGRATE_MULTIFD_PAGE_COUNT),
Juan Quintela73af8dd2017-10-05 21:30:10 +02002912 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
2913 parameters.xbzrle_cache_size,
2914 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
Peter Xu20814752017-07-18 11:39:03 +08002915
2916 /* Migration capabilities */
2917 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
2918 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
2919 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
2920 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
2921 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
2922 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
2923 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
2924 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
2925 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
2926 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
2927 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
Juan Quintela30126bb2016-01-14 12:23:00 +01002928 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_X_MULTIFD),
Peter Xu20814752017-07-18 11:39:03 +08002929
Peter Xu52722982017-06-27 12:10:14 +08002930 DEFINE_PROP_END_OF_LIST(),
2931};
2932
Peter Xue5cb7e72017-06-27 12:10:13 +08002933static void migration_class_init(ObjectClass *klass, void *data)
2934{
2935 DeviceClass *dc = DEVICE_CLASS(klass);
2936
2937 dc->user_creatable = false;
Peter Xu52722982017-06-27 12:10:14 +08002938 dc->props = migration_properties;
Peter Xue5cb7e72017-06-27 12:10:13 +08002939}
2940
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01002941static void migration_instance_finalize(Object *obj)
2942{
2943 MigrationState *ms = MIGRATION_OBJ(obj);
2944 MigrationParameters *params = &ms->parameters;
2945
Juan Quintela87db1a72017-09-05 12:50:22 +02002946 qemu_mutex_destroy(&ms->error_mutex);
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01002947 g_free(params->tls_hostname);
2948 g_free(params->tls_creds);
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002949 qemu_sem_destroy(&ms->pause_sem);
Peter Xub23c2ad2018-05-02 18:47:19 +08002950 qemu_sem_destroy(&ms->postcopy_pause_sem);
Peter Xu14b17422018-05-02 18:47:21 +08002951 qemu_sem_destroy(&ms->postcopy_pause_rp_sem);
Marc-André Lureauab105cc2018-03-06 18:09:59 +01002952 error_free(ms->error);
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01002953}
2954
Peter Xue5cb7e72017-06-27 12:10:13 +08002955static void migration_instance_init(Object *obj)
2956{
2957 MigrationState *ms = MIGRATION_OBJ(obj);
Peter Xu8b0b29d2017-07-18 11:39:06 +08002958 MigrationParameters *params = &ms->parameters;
Peter Xue5cb7e72017-06-27 12:10:13 +08002959
2960 ms->state = MIGRATION_STATUS_NONE;
Peter Xue5cb7e72017-06-27 12:10:13 +08002961 ms->mbps = -1;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002962 qemu_sem_init(&ms->pause_sem, 0);
Juan Quintela87db1a72017-09-05 12:50:22 +02002963 qemu_mutex_init(&ms->error_mutex);
Peter Xu8b0b29d2017-07-18 11:39:06 +08002964
2965 params->tls_hostname = g_strdup("");
2966 params->tls_creds = g_strdup("");
2967
2968 /* Set has_* up only for parameter checks */
2969 params->has_compress_level = true;
2970 params->has_compress_threads = true;
2971 params->has_decompress_threads = true;
2972 params->has_cpu_throttle_initial = true;
2973 params->has_cpu_throttle_increment = true;
2974 params->has_max_bandwidth = true;
2975 params->has_downtime_limit = true;
2976 params->has_x_checkpoint_delay = true;
2977 params->has_block_incremental = true;
Juan Quintela4075fb12016-01-15 08:56:17 +01002978 params->has_x_multifd_channels = true;
Juan Quintela0fb86602017-04-27 10:48:25 +02002979 params->has_x_multifd_page_count = true;
Juan Quintela73af8dd2017-10-05 21:30:10 +02002980 params->has_xbzrle_cache_size = true;
Peter Xub23c2ad2018-05-02 18:47:19 +08002981
2982 qemu_sem_init(&ms->postcopy_pause_sem, 0);
Peter Xu14b17422018-05-02 18:47:21 +08002983 qemu_sem_init(&ms->postcopy_pause_rp_sem, 0);
Peter Xu8b0b29d2017-07-18 11:39:06 +08002984}
2985
2986/*
2987 * Return true if check pass, false otherwise. Error will be put
2988 * inside errp if provided.
2989 */
2990static bool migration_object_check(MigrationState *ms, Error **errp)
2991{
Peter Xu6b19a7d2017-07-18 11:39:10 +08002992 MigrationCapabilityStatusList *head = NULL;
2993 /* Assuming all off */
2994 bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret;
2995 int i;
2996
Peter Xu8b0b29d2017-07-18 11:39:06 +08002997 if (!migrate_params_check(&ms->parameters, errp)) {
2998 return false;
2999 }
3000
Peter Xu6b19a7d2017-07-18 11:39:10 +08003001 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
3002 if (ms->enabled_capabilities[i]) {
3003 head = migrate_cap_add(head, i, true);
3004 }
3005 }
3006
3007 ret = migrate_caps_check(cap_list, head, errp);
3008
3009 /* It works with head == NULL */
3010 qapi_free_MigrationCapabilityStatusList(head);
3011
3012 return ret;
Peter Xue5cb7e72017-06-27 12:10:13 +08003013}
3014
3015static const TypeInfo migration_type = {
3016 .name = TYPE_MIGRATION,
Peter Xu01f6e142017-06-28 15:15:44 +08003017 /*
Peter Xuc8d3ff32017-07-05 16:21:23 +08003018 * NOTE: TYPE_MIGRATION is not really a device, as the object is
3019 * not created using qdev_create(), it is not attached to the qdev
3020 * device tree, and it is never realized.
3021 *
3022 * TODO: Make this TYPE_OBJECT once QOM provides something like
3023 * TYPE_DEVICE's "-global" properties.
Peter Xu01f6e142017-06-28 15:15:44 +08003024 */
Peter Xue5cb7e72017-06-27 12:10:13 +08003025 .parent = TYPE_DEVICE,
3026 .class_init = migration_class_init,
3027 .class_size = sizeof(MigrationClass),
3028 .instance_size = sizeof(MigrationState),
3029 .instance_init = migration_instance_init,
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01003030 .instance_finalize = migration_instance_finalize,
Peter Xue5cb7e72017-06-27 12:10:13 +08003031};
3032
3033static void register_migration_types(void)
3034{
3035 type_register_static(&migration_type);
3036}
3037
3038type_init(register_migration_types);