blob: 274160e9700da78dd73ecd91e6571ec8c5008528 [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 Armbrustercc7a8ea2015-03-17 17:22:46 +010034#include "qapi/qmp/qerror.h"
Paolo Bonziniab28bd22015-07-09 08:55:38 +020035#include "qemu/rcu.h"
Juan Quintela2c9e6fe2017-04-21 14:31:22 +020036#include "block.h"
Juan Quintelabe07b0a2017-04-20 13:12:24 +020037#include "postcopy-ram.h"
Juan Quintela766bd172012-07-23 05:45:29 +020038#include "qemu/thread.h"
Luiz Capitulino791e7c82011-09-13 17:37:16 -030039#include "qmp-commands.h"
Kazuya Saitoc09e5bb2013-02-22 17:36:19 +010040#include "trace.h"
Juan Quintela598cd2b2015-05-20 12:16:15 +020041#include "qapi-event.h"
Juan Quintela51180422017-04-24 20:50:19 +020042#include "exec/target_page.h"
Daniel P. Berrange61b67d42016-04-27 11:05:01 +010043#include "io/channel-buffer.h"
zhanghailiang35a6ed42016-10-27 14:42:52 +080044#include "migration/colo.h"
Peter Xu4ffdb332017-06-27 12:10:18 +080045#include "hw/boards.h"
Peter Xu9d18af92017-06-27 12:10:19 +080046#include "monitor/monitor.h"
aliguori065e2812008-11-11 16:46:33 +000047
Jason J. Hernedc325622015-09-08 13:12:37 -040048#define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000049
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010050/* Amount of time to allocate to each "chunk" of bandwidth-throttled
51 * data. */
52#define BUFFER_DELAY 100
53#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
54
Ashijeet Acharya2ff30252016-09-15 21:50:28 +053055/* Time in milliseconds we are allowed to stop the source,
56 * for sending the last part */
57#define DEFAULT_MIGRATE_SET_DOWNTIME 300
58
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -030059/* Maximum migrate downtime set to 2000 seconds */
60#define MAX_MIGRATE_DOWNTIME_SECONDS 2000
61#define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
62
Liang Li8706d2d2015-03-23 16:32:17 +080063/* Default compression thread count */
64#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080065/* Default decompression thread count, usually decompression is at
66 * least 4 times as fast as compression.*/
67#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080068/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
69#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
Jason J. Herne1626fee2015-09-08 13:12:34 -040070/* Define default autoconverge cpu throttle migration parameters */
Jason J. Herned85a31d2016-04-21 14:07:18 -040071#define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
72#define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
Liang Li8706d2d2015-03-23 16:32:17 +080073
Orit Wasserman17ad9b32012-08-06 21:42:53 +030074/* Migration XBZRLE default cache size */
Juan Quintela73af8dd2017-10-05 21:30:10 +020075#define DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE (64 * 1024 * 1024)
Orit Wasserman17ad9b32012-08-06 21:42:53 +030076
zhanghailiang68b53592016-10-27 14:43:01 +080077/* The delay time (in ms) between two COLO checkpoints
78 * Note: Please change this default value to 10000 when we support hybrid mode.
79 */
80#define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY 200
Juan Quintela4075fb12016-01-15 08:56:17 +010081#define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2
Juan Quintela0fb86602017-04-27 10:48:25 +020082#define DEFAULT_MIGRATE_MULTIFD_PAGE_COUNT 16
zhanghailiang68b53592016-10-27 14:43:01 +080083
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010084static NotifierList migration_state_notifiers =
85 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
86
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000087static bool deferred_incoming;
88
Juan Quintelada6f1792017-04-24 17:37:14 +020089/* Messages sent on the return path from destination to source */
90enum mig_rp_message_type {
91 MIG_RP_MSG_INVALID = 0, /* Must be 0 */
92 MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */
93 MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */
94
95 MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */
96 MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */
97
98 MIG_RP_MSG_MAX
99};
100
Juan Quintela17549e82011-10-05 13:50:43 +0200101/* When we add fault tolerance, we could have several
102 migrations at once. For now we don't need to add
103 dynamic creation of migration */
104
Peter Xue5cb7e72017-06-27 12:10:13 +0800105static MigrationState *current_migration;
106
Peter Xu8b0b29d2017-07-18 11:39:06 +0800107static bool migration_object_check(MigrationState *ms, Error **errp);
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +0100108static int migration_maybe_pause(MigrationState *s,
109 int *current_active_state,
110 int new_state);
Peter Xu8b0b29d2017-07-18 11:39:06 +0800111
Peter Xue5cb7e72017-06-27 12:10:13 +0800112void migration_object_init(void)
113{
Peter Xu4ffdb332017-06-27 12:10:18 +0800114 MachineState *ms = MACHINE(qdev_get_machine());
Peter Xu8b0b29d2017-07-18 11:39:06 +0800115 Error *err = NULL;
Peter Xu4ffdb332017-06-27 12:10:18 +0800116
Peter Xue5cb7e72017-06-27 12:10:13 +0800117 /* This can only be called once. */
118 assert(!current_migration);
119 current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION));
Peter Xu4ffdb332017-06-27 12:10:18 +0800120
Peter Xu8b0b29d2017-07-18 11:39:06 +0800121 if (!migration_object_check(current_migration, &err)) {
122 error_report_err(err);
123 exit(1);
124 }
125
Peter Xu4ffdb332017-06-27 12:10:18 +0800126 /*
127 * We cannot really do this in migration_instance_init() since at
128 * that time global properties are not yet applied, then this
129 * value will be definitely replaced by something else.
130 */
131 if (ms->enforce_config_section) {
132 current_migration->send_configuration = true;
133 }
Peter Xue5cb7e72017-06-27 12:10:13 +0800134}
135
Vladimir Sementsov-Ogievskiy1f895602017-12-28 12:16:16 +0300136void migration_object_finalize(void)
137{
138 object_unref(OBJECT(current_migration));
139}
140
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100141/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +0200142MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +0200143{
Peter Xue5cb7e72017-06-27 12:10:13 +0800144 /* This can only be called after the object created. */
145 assert(current_migration);
146 return current_migration;
Juan Quintela17549e82011-10-05 13:50:43 +0200147}
148
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100149MigrationIncomingState *migration_incoming_get_current(void)
150{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100151 static bool once;
152 static MigrationIncomingState mis_current;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100153
Juan Quintelab4b076d2017-01-23 22:32:06 +0100154 if (!once) {
155 mis_current.state = MIGRATION_STATUS_NONE;
156 memset(&mis_current, 0, sizeof(MigrationIncomingState));
Juan Quintelab4b076d2017-01-23 22:32:06 +0100157 qemu_mutex_init(&mis_current.rp_mutex);
158 qemu_event_init(&mis_current.main_thread_load_event, false);
159 once = true;
160 }
161 return &mis_current;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100162}
163
164void migration_incoming_state_destroy(void)
165{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100166 struct MigrationIncomingState *mis = migration_incoming_get_current();
167
Peter Xu34826552017-05-19 14:43:29 +0800168 if (mis->to_src_file) {
Peter Xu660819b2017-05-19 14:43:30 +0800169 /* Tell source that we are done */
170 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
Peter Xu34826552017-05-19 14:43:29 +0800171 qemu_fclose(mis->to_src_file);
172 mis->to_src_file = NULL;
173 }
174
Peter Xu660819b2017-05-19 14:43:30 +0800175 if (mis->from_src_file) {
176 qemu_fclose(mis->from_src_file);
177 mis->from_src_file = NULL;
178 }
179
Dr. David Alan Gilbert5089e182017-08-25 15:19:39 +0100180 qemu_event_reset(&mis->main_thread_load_event);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100181}
182
Juan Quintelab05dc722015-07-07 14:44:05 +0200183static void migrate_generate_event(int new_state)
184{
185 if (migrate_use_events()) {
186 qapi_event_send_migration(new_state, &error_abort);
Juan Quintelab05dc722015-07-07 14:44:05 +0200187 }
188}
189
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000190/*
191 * Called on -incoming with a defer: uri.
192 * The migration can be started later after any parameters have been
193 * changed.
194 */
195static void deferred_incoming_migration(Error **errp)
196{
197 if (deferred_incoming) {
198 error_setg(errp, "Incoming migration already deferred");
199 }
200 deferred_incoming = true;
201}
202
Juan Quintelada6f1792017-04-24 17:37:14 +0200203/*
204 * Send a message on the return channel back to the source
205 * of the migration.
206 */
207static void migrate_send_rp_message(MigrationIncomingState *mis,
208 enum mig_rp_message_type message_type,
209 uint16_t len, void *data)
210{
211 trace_migrate_send_rp_message((int)message_type, len);
212 qemu_mutex_lock(&mis->rp_mutex);
213 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
214 qemu_put_be16(mis->to_src_file, len);
215 qemu_put_buffer(mis->to_src_file, data, len);
216 qemu_fflush(mis->to_src_file);
217 qemu_mutex_unlock(&mis->rp_mutex);
218}
219
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000220/* Request a range of pages from the source VM at the given
221 * start address.
222 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
223 * as the last request (a name must have been given previously)
224 * Start: Address offset within the RB
225 * Len: Length in bytes required - must be a multiple of pagesize
226 */
227void migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
228 ram_addr_t start, size_t len)
229{
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100230 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000231 size_t msglen = 12; /* start + len */
232
233 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
234 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
235
236 if (rbname) {
237 int rbname_len = strlen(rbname);
238 assert(rbname_len < 256);
239
240 bufc[msglen++] = rbname_len;
241 memcpy(bufc + msglen, rbname, rbname_len);
242 msglen += rbname_len;
243 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES_ID, msglen, bufc);
244 } else {
245 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES, msglen, bufc);
246 }
247}
248
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200249void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000250{
aliguori34c9dd82008-10-13 03:14:31 +0000251 const char *p;
252
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200253 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000254 if (!strcmp(uri, "defer")) {
255 deferred_incoming_migration(errp);
256 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200257 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400258#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000259 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400260 rdma_start_incoming_migration(p, errp);
261#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000262 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200263 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000264 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200265 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000266 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200267 fd_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000268 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100269 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200270 }
aliguori5bb79102008-10-13 03:12:02 +0000271}
272
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300273static void process_incoming_migration_bh(void *opaque)
274{
275 Error *local_err = NULL;
276 MigrationIncomingState *mis = opaque;
277
Kevin Wolface21a52017-05-04 18:52:36 +0200278 /* Make sure all file formats flush their mutable metadata.
279 * If we get an error here, just don't restart the VM yet. */
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300280 bdrv_invalidate_cache_all(&local_err);
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200281 if (local_err) {
Kevin Wolface21a52017-05-04 18:52:36 +0200282 error_report_err(local_err);
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200283 local_err = NULL;
284 autostart = false;
285 }
286
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300287 /*
288 * This must happen after all error conditions are dealt with and
289 * we're sure the VM is going to be running on this host.
290 */
291 qemu_announce_self();
292
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100293 if (multifd_load_cleanup(&local_err) != 0) {
294 error_report_err(local_err);
295 autostart = false;
296 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300297 /* If global state section was not received or we are in running
298 state, we need to obey autostart. Any other state is set with
299 runstate_set. */
300
301 if (!global_state_received() ||
302 global_state_get_runstate() == RUN_STATE_RUNNING) {
303 if (autostart) {
304 vm_start();
305 } else {
306 runstate_set(RUN_STATE_PAUSED);
307 }
308 } else {
309 runstate_set(global_state_get_runstate());
310 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300311 /*
312 * This must happen after any state changes since as soon as an external
313 * observer sees this event they might start to prod at the VM assuming
314 * it's ready to use.
315 */
316 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
317 MIGRATION_STATUS_COMPLETED);
318 qemu_bh_delete(mis->bh);
319 migration_incoming_state_destroy();
320}
321
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200322static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200323{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100324 MigrationIncomingState *mis = migration_incoming_get_current();
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000325 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200326 int ret;
327
Juan Quintela4f0fae72017-07-24 12:42:02 +0200328 assert(mis->from_src_file);
Dr. David Alan Gilbert67f11b52017-02-24 18:28:34 +0000329 mis->largest_page_size = qemu_ram_pagesize_largest();
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000330 postcopy_state_set(POSTCOPY_INCOMING_NONE);
zhanghailiang93d7af62015-12-16 11:47:34 +0000331 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
332 MIGRATION_STATUS_ACTIVE);
Juan Quintela4f0fae72017-07-24 12:42:02 +0200333 ret = qemu_loadvm_state(mis->from_src_file);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100334
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000335 ps = postcopy_state_get();
336 trace_process_incoming_migration_co_end(ret, ps);
337 if (ps != POSTCOPY_INCOMING_NONE) {
338 if (ps == POSTCOPY_INCOMING_ADVISE) {
339 /*
340 * Where a migration had postcopy enabled (and thus went to advise)
341 * but managed to complete within the precopy period, we can use
342 * the normal exit.
343 */
344 postcopy_ram_incoming_cleanup(mis);
345 } else if (ret >= 0) {
346 /*
347 * Postcopy was started, cleanup should happen at the end of the
348 * postcopy thread.
349 */
350 trace_process_incoming_migration_co_postcopy_end_main();
351 return;
352 }
353 /* Else if something went wrong then just fall out of the normal exit */
354 }
355
zhanghailiang25d0c162016-10-27 14:42:55 +0800356 /* we get COLO info, and know if we are in COLO mode */
357 if (!ret && migration_incoming_enable_colo()) {
358 mis->migration_incoming_co = qemu_coroutine_self();
359 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
360 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
361 mis->have_colo_incoming_thread = true;
362 qemu_coroutine_yield();
363
364 /* Wait checkpoint incoming thread exit before free resource */
365 qemu_thread_join(&mis->colo_incoming_thread);
366 }
367
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200368 if (ret < 0) {
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100369 Error *local_err = NULL;
370
zhanghailiang93d7af62015-12-16 11:47:34 +0000371 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
372 MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200373 error_report("load of migration failed: %s", strerror(-ret));
Dr. David Alan Gilbert3a0f2ce2017-07-17 12:09:32 +0100374 qemu_fclose(mis->from_src_file);
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100375 if (multifd_load_cleanup(&local_err) != 0) {
376 error_report_err(local_err);
377 }
Eric Blake4aead692013-04-16 15:50:41 -0600378 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200379 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300380 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
381 qemu_bh_schedule(mis->bh);
Juan Quintela511c0232010-06-09 14:10:55 +0200382}
383
Juan Quintelae595a012017-07-17 12:30:25 +0200384static void migration_incoming_setup(QEMUFile *f)
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200385{
Juan Quintela4f0fae72017-07-24 12:42:02 +0200386 MigrationIncomingState *mis = migration_incoming_get_current();
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200387
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100388 if (multifd_load_setup() != 0) {
389 /* We haven't been able to create multifd threads
390 nothing better to do */
391 exit(EXIT_FAILURE);
392 }
393
Juan Quintela4f0fae72017-07-24 12:42:02 +0200394 if (!mis->from_src_file) {
395 mis->from_src_file = f;
396 }
Daniel P. Berrange06ad5132016-04-27 11:04:56 +0100397 qemu_file_set_blocking(f, false);
Juan Quintelae595a012017-07-17 12:30:25 +0200398}
399
400static void migration_incoming_process(void)
401{
402 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL);
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200403 qemu_coroutine_enter(co);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200404}
405
Juan Quintelae595a012017-07-17 12:30:25 +0200406void migration_fd_process_incoming(QEMUFile *f)
407{
408 migration_incoming_setup(f);
409 migration_incoming_process();
410}
411
Juan Quintela4f0fae72017-07-24 12:42:02 +0200412void migration_ioc_process_incoming(QIOChannel *ioc)
413{
414 MigrationIncomingState *mis = migration_incoming_get_current();
415
416 if (!mis->from_src_file) {
417 QEMUFile *f = qemu_fopen_channel_input(ioc);
418 migration_fd_process_incoming(f);
419 }
420 /* We still only have a single channel. Nothing to do here yet */
421}
422
Juan Quintela428d8902017-07-24 13:06:25 +0200423/**
424 * @migration_has_all_channels: We have received all channels that we need
425 *
426 * Returns true when we have got connections to all the channels that
427 * we need for migration.
428 */
429bool migration_has_all_channels(void)
430{
431 return true;
432}
433
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000434/*
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000435 * Send a 'SHUT' message on the return channel with the given value
436 * to indicate that we've finished with the RP. Non-0 value indicates
437 * error.
438 */
439void migrate_send_rp_shut(MigrationIncomingState *mis,
440 uint32_t value)
441{
442 uint32_t buf;
443
444 buf = cpu_to_be32(value);
445 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
446}
447
448/*
449 * Send a 'PONG' message on the return channel with the given value
450 * (normally in response to a 'PING')
451 */
452void migrate_send_rp_pong(MigrationIncomingState *mis,
453 uint32_t value)
454{
455 uint32_t buf;
456
457 buf = cpu_to_be32(value);
458 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
459}
460
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300461MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
462{
463 MigrationCapabilityStatusList *head = NULL;
464 MigrationCapabilityStatusList *caps;
465 MigrationState *s = migrate_get_current();
466 int i;
467
Michael Tokarev387eede2013-10-05 13:18:28 +0400468 caps = NULL; /* silence compiler warning */
Eric Blake7fb1cf12015-11-18 01:52:57 -0700469 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Dr. David Alan Gilberted1701c2017-05-15 15:05:29 +0100470#ifndef CONFIG_LIVE_BLOCK_MIGRATION
471 if (i == MIGRATION_CAPABILITY_BLOCK) {
472 continue;
473 }
474#endif
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300475 if (head == NULL) {
476 head = g_malloc0(sizeof(*caps));
477 caps = head;
478 } else {
479 caps->next = g_malloc0(sizeof(*caps));
480 caps = caps->next;
481 }
482 caps->value =
483 g_malloc(sizeof(*caps->value));
484 caps->value->capability = i;
485 caps->value->state = s->enabled_capabilities[i];
486 }
487
488 return head;
489}
490
Liang Li85de8322015-03-23 16:32:28 +0800491MigrationParameters *qmp_query_migrate_parameters(Error **errp)
492{
493 MigrationParameters *params;
494 MigrationState *s = migrate_get_current();
495
Markus Armbrustere87fae42017-07-18 12:57:38 +0200496 /* TODO use QAPI_CLONE() instead of duplicating it inline */
Liang Li85de8322015-03-23 16:32:28 +0800497 params = g_malloc0(sizeof(*params));
Eric Blakede63ab62016-09-08 22:14:15 -0500498 params->has_compress_level = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100499 params->compress_level = s->parameters.compress_level;
Eric Blakede63ab62016-09-08 22:14:15 -0500500 params->has_compress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100501 params->compress_threads = s->parameters.compress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500502 params->has_decompress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100503 params->decompress_threads = s->parameters.decompress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500504 params->has_cpu_throttle_initial = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100505 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
Eric Blakede63ab62016-09-08 22:14:15 -0500506 params->has_cpu_throttle_increment = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100507 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200508 params->has_tls_creds = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100509 params->tls_creds = g_strdup(s->parameters.tls_creds);
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200510 params->has_tls_hostname = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100511 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530512 params->has_max_bandwidth = true;
513 params->max_bandwidth = s->parameters.max_bandwidth;
514 params->has_downtime_limit = true;
515 params->downtime_limit = s->parameters.downtime_limit;
zhanghailiangfe39a4d2016-11-02 15:42:09 +0800516 params->has_x_checkpoint_delay = true;
zhanghailiang68b53592016-10-27 14:43:01 +0800517 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
Juan Quintela2833c592017-04-05 18:32:37 +0200518 params->has_block_incremental = true;
519 params->block_incremental = s->parameters.block_incremental;
Juan Quintela4075fb12016-01-15 08:56:17 +0100520 params->has_x_multifd_channels = true;
521 params->x_multifd_channels = s->parameters.x_multifd_channels;
Juan Quintela0fb86602017-04-27 10:48:25 +0200522 params->has_x_multifd_page_count = true;
523 params->x_multifd_page_count = s->parameters.x_multifd_page_count;
Juan Quintela73af8dd2017-10-05 21:30:10 +0200524 params->has_xbzrle_cache_size = true;
525 params->xbzrle_cache_size = s->parameters.xbzrle_cache_size;
Liang Li85de8322015-03-23 16:32:28 +0800526
527 return params;
528}
529
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000530/*
531 * Return true if we're already in the middle of a migration
532 * (i.e. any of the active or setup states)
533 */
534static bool migration_is_setup_or_active(int state)
535{
536 switch (state) {
537 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000538 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000539 case MIGRATION_STATUS_SETUP:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +0100540 case MIGRATION_STATUS_PRE_SWITCHOVER:
541 case MIGRATION_STATUS_DEVICE:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000542 return true;
543
544 default:
545 return false;
546
547 }
548}
549
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100550static void populate_ram_info(MigrationInfo *info, MigrationState *s)
551{
552 info->has_ram = true;
553 info->ram = g_malloc0(sizeof(*info->ram));
Juan Quintela93604472017-06-06 19:49:03 +0200554 info->ram->transferred = ram_counters.transferred;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100555 info->ram->total = ram_bytes_total();
Juan Quintela93604472017-06-06 19:49:03 +0200556 info->ram->duplicate = ram_counters.duplicate;
Juan Quintelabedf53c2017-03-13 20:35:54 +0100557 /* legacy value. It is not used anymore */
558 info->ram->skipped = 0;
Juan Quintela93604472017-06-06 19:49:03 +0200559 info->ram->normal = ram_counters.normal;
560 info->ram->normal_bytes = ram_counters.normal *
Juan Quintela20afaed2017-03-21 09:09:14 +0100561 qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100562 info->ram->mbps = s->mbps;
Juan Quintela93604472017-06-06 19:49:03 +0200563 info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
564 info->ram->postcopy_requests = ram_counters.postcopy_requests;
Chao Fan030ce1f2017-03-21 10:22:43 +0800565 info->ram->page_size = qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100566
Juan Quintela114f5ae2017-05-04 10:09:21 +0200567 if (migrate_use_xbzrle()) {
568 info->has_xbzrle_cache = true;
569 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
570 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
Juan Quintela93604472017-06-06 19:49:03 +0200571 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
572 info->xbzrle_cache->pages = xbzrle_counters.pages;
573 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
574 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
575 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
Juan Quintela114f5ae2017-05-04 10:09:21 +0200576 }
577
Juan Quintela338182c2017-05-03 13:16:38 +0200578 if (cpu_throttle_active()) {
579 info->has_cpu_throttle_percentage = true;
580 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
581 }
582
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100583 if (s->state != MIGRATION_STATUS_COMPLETED) {
584 info->ram->remaining = ram_bytes_remaining();
Juan Quintela93604472017-06-06 19:49:03 +0200585 info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100586 }
587}
588
Juan Quintela930ac042017-05-04 10:21:46 +0200589static void populate_disk_info(MigrationInfo *info)
590{
591 if (blk_mig_active()) {
592 info->has_disk = true;
593 info->disk = g_malloc0(sizeof(*info->disk));
594 info->disk->transferred = blk_mig_bytes_transferred();
595 info->disk->remaining = blk_mig_bytes_remaining();
596 info->disk->total = blk_mig_bytes_total();
597 }
598}
599
Peter Maydellee869812018-01-22 13:20:14 +0000600MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000601{
Peter Maydellee869812018-01-22 13:20:14 +0000602 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200603 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000604
Juan Quintela17549e82011-10-05 13:50:43 +0200605 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800606 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200607 /* no migration has happened ever */
608 break;
zhanghailiang31194732015-03-13 16:08:38 +0800609 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400610 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400611 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400612 break;
zhanghailiang31194732015-03-13 16:08:38 +0800613 case MIGRATION_STATUS_ACTIVE:
614 case MIGRATION_STATUS_CANCELLING:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000615 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +0100616 case MIGRATION_STATUS_PRE_SWITCHOVER:
617 case MIGRATION_STATUS_DEVICE:
Juan Quintelac8f9f4f2017-06-06 19:21:29 +0200618 /* TODO add some postcopy stats */
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000619 info->has_status = true;
620 info->has_total_time = true;
621 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Peter Xu4af246a2018-01-03 20:20:08 +0800622 - s->start_time;
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000623 info->has_expected_downtime = true;
624 info->expected_downtime = s->expected_downtime;
625 info->has_setup_time = true;
626 info->setup_time = s->setup_time;
627
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100628 populate_ram_info(info, s);
Juan Quintela930ac042017-05-04 10:21:46 +0200629 populate_disk_info(info);
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000630 break;
zhanghailiang0b827d52016-10-27 14:42:54 +0800631 case MIGRATION_STATUS_COLO:
632 info->has_status = true;
633 /* TODO: display COLO specific information (checkpoint info etc.) */
634 break;
zhanghailiang31194732015-03-13 16:08:38 +0800635 case MIGRATION_STATUS_COMPLETED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300636 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900637 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200638 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200639 info->has_downtime = true;
640 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400641 info->has_setup_time = true;
642 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200643
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100644 populate_ram_info(info, s);
Juan Quintela17549e82011-10-05 13:50:43 +0200645 break;
zhanghailiang31194732015-03-13 16:08:38 +0800646 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300647 info->has_status = true;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100648 if (s->error) {
649 info->has_error_desc = true;
650 info->error_desc = g_strdup(error_get_pretty(s->error));
651 }
Juan Quintela17549e82011-10-05 13:50:43 +0200652 break;
zhanghailiang31194732015-03-13 16:08:38 +0800653 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300654 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200655 break;
aliguori5bb79102008-10-13 03:12:02 +0000656 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800657 info->status = s->state;
Peter Maydellee869812018-01-22 13:20:14 +0000658
659 return info;
aliguori5bb79102008-10-13 03:12:02 +0000660}
661
Peter Xu4a842142017-07-18 11:39:08 +0800662/**
663 * @migration_caps_check - check capability validity
664 *
665 * @cap_list: old capability list, array of bool
666 * @params: new capabilities to be applied soon
667 * @errp: set *errp if the check failed, with reason
668 *
669 * Returns true if check passed, otherwise false.
670 */
671static bool migrate_caps_check(bool *cap_list,
672 MigrationCapabilityStatusList *params,
673 Error **errp)
Orit Wasserman00458432012-08-06 21:42:48 +0300674{
Orit Wasserman00458432012-08-06 21:42:48 +0300675 MigrationCapabilityStatusList *cap;
Peter Xu4a842142017-07-18 11:39:08 +0800676 bool old_postcopy_cap;
Alexey Perevalovd7651f12017-09-19 19:47:56 +0300677 MigrationIncomingState *mis = migration_incoming_get_current();
Orit Wasserman00458432012-08-06 21:42:48 +0300678
Peter Xu4a842142017-07-18 11:39:08 +0800679 old_postcopy_cap = cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Orit Wasserman00458432012-08-06 21:42:48 +0300680
681 for (cap = params; cap; cap = cap->next) {
Peter Xu4a842142017-07-18 11:39:08 +0800682 cap_list[cap->value->capability] = cap->value->state;
Orit Wasserman00458432012-08-06 21:42:48 +0300683 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000684
Peter Xu4a842142017-07-18 11:39:08 +0800685#ifndef CONFIG_LIVE_BLOCK_MIGRATION
686 if (cap_list[MIGRATION_CAPABILITY_BLOCK]) {
687 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
688 "block migration");
689 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
690 return false;
691 }
692#endif
693
694 if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
695 if (cap_list[MIGRATION_CAPABILITY_COMPRESS]) {
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000696 /* The decompression threads asynchronously write into RAM
697 * rather than use the atomic copies needed to avoid
698 * userfaulting. It should be possible to fix the decompression
699 * threads for compatibility in future.
700 */
Peter Xu4a842142017-07-18 11:39:08 +0800701 error_setg(errp, "Postcopy is not currently compatible "
702 "with compression");
703 return false;
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000704 }
Peter Xu4a842142017-07-18 11:39:08 +0800705
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100706 /* This check is reasonably expensive, so only when it's being
707 * set the first time, also it's only the destination that needs
708 * special support.
709 */
710 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
Alexey Perevalovd7651f12017-09-19 19:47:56 +0300711 !postcopy_ram_supported_by_host(mis)) {
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100712 /* postcopy_ram_supported_by_host will have emitted a more
713 * detailed message
714 */
Peter Xu4a842142017-07-18 11:39:08 +0800715 error_setg(errp, "Postcopy is not supported");
716 return false;
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100717 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000718 }
Peter Xu4a842142017-07-18 11:39:08 +0800719
720 return true;
721}
722
723void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
724 Error **errp)
725{
726 MigrationState *s = migrate_get_current();
727 MigrationCapabilityStatusList *cap;
728
729 if (migration_is_setup_or_active(s->state)) {
730 error_setg(errp, QERR_MIGRATION_ACTIVE);
731 return;
732 }
733
734 if (!migrate_caps_check(s->enabled_capabilities, params, errp)) {
735 return;
736 }
737
738 for (cap = params; cap; cap = cap->next) {
739 s->enabled_capabilities[cap->value->capability] = cap->value->state;
740 }
Orit Wasserman00458432012-08-06 21:42:48 +0300741}
742
Peter Xu16d063b2017-07-18 11:39:04 +0800743/*
744 * Check whether the parameters are valid. Error will be put into errp
745 * (if provided). Return true if valid, otherwise false.
746 */
747static bool migrate_params_check(MigrationParameters *params, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +0800748{
Eric Blake7f375e02016-09-08 22:14:16 -0500749 if (params->has_compress_level &&
Juan Quintela741d4082017-12-01 13:08:38 +0100750 (params->compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100751 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
752 "is invalid, it should be in the range of 0 to 9");
Peter Xu16d063b2017-07-18 11:39:04 +0800753 return false;
Liang Li85de8322015-03-23 16:32:28 +0800754 }
Peter Xu16d063b2017-07-18 11:39:04 +0800755
Juan Quintela741d4082017-12-01 13:08:38 +0100756 if (params->has_compress_threads && (params->compress_threads < 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100757 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
758 "compress_threads",
759 "is invalid, it should be in the range of 1 to 255");
Peter Xu16d063b2017-07-18 11:39:04 +0800760 return false;
Liang Li85de8322015-03-23 16:32:28 +0800761 }
Peter Xu16d063b2017-07-18 11:39:04 +0800762
Juan Quintela741d4082017-12-01 13:08:38 +0100763 if (params->has_decompress_threads && (params->decompress_threads < 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100764 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
765 "decompress_threads",
766 "is invalid, it should be in the range of 1 to 255");
Peter Xu16d063b2017-07-18 11:39:04 +0800767 return false;
Liang Li85de8322015-03-23 16:32:28 +0800768 }
Peter Xu16d063b2017-07-18 11:39:04 +0800769
Eric Blake7f375e02016-09-08 22:14:16 -0500770 if (params->has_cpu_throttle_initial &&
771 (params->cpu_throttle_initial < 1 ||
772 params->cpu_throttle_initial > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400773 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400774 "cpu_throttle_initial",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400775 "an integer in the range of 1 to 99");
Peter Xu16d063b2017-07-18 11:39:04 +0800776 return false;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400777 }
Peter Xu16d063b2017-07-18 11:39:04 +0800778
Eric Blake7f375e02016-09-08 22:14:16 -0500779 if (params->has_cpu_throttle_increment &&
780 (params->cpu_throttle_increment < 1 ||
781 params->cpu_throttle_increment > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400782 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400783 "cpu_throttle_increment",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400784 "an integer in the range of 1 to 99");
Peter Xu16d063b2017-07-18 11:39:04 +0800785 return false;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400786 }
Peter Xu16d063b2017-07-18 11:39:04 +0800787
Juan Quintela741d4082017-12-01 13:08:38 +0100788 if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530789 error_setg(errp, "Parameter 'max_bandwidth' expects an integer in the"
790 " range of 0 to %zu bytes/second", SIZE_MAX);
Peter Xu16d063b2017-07-18 11:39:04 +0800791 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530792 }
Peter Xu16d063b2017-07-18 11:39:04 +0800793
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530794 if (params->has_downtime_limit &&
Juan Quintela741d4082017-12-01 13:08:38 +0100795 (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -0300796 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
797 "the range of 0 to %d milliseconds",
798 MAX_MIGRATE_DOWNTIME);
Peter Xu16d063b2017-07-18 11:39:04 +0800799 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530800 }
Peter Xu16d063b2017-07-18 11:39:04 +0800801
Juan Quintela741d4082017-12-01 13:08:38 +0100802 /* x_checkpoint_delay is now always positive */
803
804 if (params->has_x_multifd_channels && (params->x_multifd_channels < 1)) {
Juan Quintela4075fb12016-01-15 08:56:17 +0100805 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
806 "multifd_channels",
807 "is invalid, it should be in the range of 1 to 255");
808 return false;
809 }
Juan Quintela0fb86602017-04-27 10:48:25 +0200810 if (params->has_x_multifd_page_count &&
Juan Quintela741d4082017-12-01 13:08:38 +0100811 (params->x_multifd_page_count < 1 ||
812 params->x_multifd_page_count > 10000)) {
Juan Quintela0fb86602017-04-27 10:48:25 +0200813 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
814 "multifd_page_count",
815 "is invalid, it should be in the range of 1 to 10000");
816 return false;
817 }
Peter Xu16d063b2017-07-18 11:39:04 +0800818
Juan Quintela73af8dd2017-10-05 21:30:10 +0200819 if (params->has_xbzrle_cache_size &&
820 (params->xbzrle_cache_size < qemu_target_page_size() ||
821 !is_power_of_2(params->xbzrle_cache_size))) {
822 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
823 "xbzrle_cache_size",
824 "is invalid, it should be bigger than target page size"
825 " and a power of two");
826 return false;
827 }
828
Peter Xu16d063b2017-07-18 11:39:04 +0800829 return true;
830}
831
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200832static void migrate_params_test_apply(MigrateSetParameters *params,
833 MigrationParameters *dest)
834{
835 *dest = migrate_get_current()->parameters;
836
837 /* TODO use QAPI_CLONE() instead of duplicating it inline */
838
839 if (params->has_compress_level) {
840 dest->compress_level = params->compress_level;
841 }
842
843 if (params->has_compress_threads) {
844 dest->compress_threads = params->compress_threads;
845 }
846
847 if (params->has_decompress_threads) {
848 dest->decompress_threads = params->decompress_threads;
849 }
850
851 if (params->has_cpu_throttle_initial) {
852 dest->cpu_throttle_initial = params->cpu_throttle_initial;
853 }
854
855 if (params->has_cpu_throttle_increment) {
856 dest->cpu_throttle_increment = params->cpu_throttle_increment;
857 }
858
859 if (params->has_tls_creds) {
Markus Armbruster01fa5592017-07-18 14:42:04 +0200860 assert(params->tls_creds->type == QTYPE_QSTRING);
861 dest->tls_creds = g_strdup(params->tls_creds->u.s);
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200862 }
863
864 if (params->has_tls_hostname) {
Markus Armbruster01fa5592017-07-18 14:42:04 +0200865 assert(params->tls_hostname->type == QTYPE_QSTRING);
866 dest->tls_hostname = g_strdup(params->tls_hostname->u.s);
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200867 }
868
869 if (params->has_max_bandwidth) {
870 dest->max_bandwidth = params->max_bandwidth;
871 }
872
873 if (params->has_downtime_limit) {
874 dest->downtime_limit = params->downtime_limit;
875 }
876
877 if (params->has_x_checkpoint_delay) {
878 dest->x_checkpoint_delay = params->x_checkpoint_delay;
879 }
880
881 if (params->has_block_incremental) {
882 dest->block_incremental = params->block_incremental;
883 }
Juan Quintela5e7577a2017-10-09 18:07:56 +0200884 if (params->has_x_multifd_channels) {
885 dest->x_multifd_channels = params->x_multifd_channels;
886 }
887 if (params->has_x_multifd_page_count) {
888 dest->x_multifd_page_count = params->x_multifd_page_count;
889 }
Juan Quintela73af8dd2017-10-05 21:30:10 +0200890 if (params->has_xbzrle_cache_size) {
891 dest->xbzrle_cache_size = params->xbzrle_cache_size;
892 }
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200893}
894
Juan Quintela73af8dd2017-10-05 21:30:10 +0200895static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
Peter Xu16d063b2017-07-18 11:39:04 +0800896{
897 MigrationState *s = migrate_get_current();
898
Markus Armbrustere87fae42017-07-18 12:57:38 +0200899 /* TODO use QAPI_CLONE() instead of duplicating it inline */
900
Eric Blake7f375e02016-09-08 22:14:16 -0500901 if (params->has_compress_level) {
902 s->parameters.compress_level = params->compress_level;
Liang Li85de8322015-03-23 16:32:28 +0800903 }
Peter Xu476c72a2017-07-18 11:39:05 +0800904
Eric Blake7f375e02016-09-08 22:14:16 -0500905 if (params->has_compress_threads) {
906 s->parameters.compress_threads = params->compress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800907 }
Peter Xu476c72a2017-07-18 11:39:05 +0800908
Eric Blake7f375e02016-09-08 22:14:16 -0500909 if (params->has_decompress_threads) {
910 s->parameters.decompress_threads = params->decompress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800911 }
Peter Xu476c72a2017-07-18 11:39:05 +0800912
Eric Blake7f375e02016-09-08 22:14:16 -0500913 if (params->has_cpu_throttle_initial) {
914 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400915 }
Peter Xu476c72a2017-07-18 11:39:05 +0800916
Eric Blake7f375e02016-09-08 22:14:16 -0500917 if (params->has_cpu_throttle_increment) {
918 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400919 }
Peter Xu476c72a2017-07-18 11:39:05 +0800920
Eric Blake7f375e02016-09-08 22:14:16 -0500921 if (params->has_tls_creds) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100922 g_free(s->parameters.tls_creds);
Markus Armbruster01fa5592017-07-18 14:42:04 +0200923 assert(params->tls_creds->type == QTYPE_QSTRING);
924 s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100925 }
Peter Xu476c72a2017-07-18 11:39:05 +0800926
Eric Blake7f375e02016-09-08 22:14:16 -0500927 if (params->has_tls_hostname) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100928 g_free(s->parameters.tls_hostname);
Markus Armbruster01fa5592017-07-18 14:42:04 +0200929 assert(params->tls_hostname->type == QTYPE_QSTRING);
930 s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100931 }
Peter Xu476c72a2017-07-18 11:39:05 +0800932
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530933 if (params->has_max_bandwidth) {
934 s->parameters.max_bandwidth = params->max_bandwidth;
935 if (s->to_dst_file) {
936 qemu_file_set_rate_limit(s->to_dst_file,
937 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
938 }
939 }
Peter Xu476c72a2017-07-18 11:39:05 +0800940
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530941 if (params->has_downtime_limit) {
942 s->parameters.downtime_limit = params->downtime_limit;
943 }
zhanghailiang68b53592016-10-27 14:43:01 +0800944
945 if (params->has_x_checkpoint_delay) {
946 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
zhanghailiang479125d2017-01-17 20:57:42 +0800947 if (migration_in_colo_state()) {
948 colo_checkpoint_notify(s);
949 }
zhanghailiang68b53592016-10-27 14:43:01 +0800950 }
Peter Xu476c72a2017-07-18 11:39:05 +0800951
Juan Quintela2833c592017-04-05 18:32:37 +0200952 if (params->has_block_incremental) {
953 s->parameters.block_incremental = params->block_incremental;
954 }
Juan Quintela4075fb12016-01-15 08:56:17 +0100955 if (params->has_x_multifd_channels) {
956 s->parameters.x_multifd_channels = params->x_multifd_channels;
957 }
Juan Quintela0fb86602017-04-27 10:48:25 +0200958 if (params->has_x_multifd_page_count) {
959 s->parameters.x_multifd_page_count = params->x_multifd_page_count;
960 }
Juan Quintela73af8dd2017-10-05 21:30:10 +0200961 if (params->has_xbzrle_cache_size) {
962 s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
963 xbzrle_cache_resize(params->xbzrle_cache_size, errp);
964 }
Liang Li85de8322015-03-23 16:32:28 +0800965}
966
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200967void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
Peter Xu476c72a2017-07-18 11:39:05 +0800968{
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200969 MigrationParameters tmp;
970
Markus Armbruster01fa5592017-07-18 14:42:04 +0200971 /* TODO Rewrite "" to null instead */
972 if (params->has_tls_creds
973 && params->tls_creds->type == QTYPE_QNULL) {
974 QDECREF(params->tls_creds->u.n);
975 params->tls_creds->type = QTYPE_QSTRING;
976 params->tls_creds->u.s = strdup("");
977 }
978 /* TODO Rewrite "" to null instead */
979 if (params->has_tls_hostname
980 && params->tls_hostname->type == QTYPE_QNULL) {
981 QDECREF(params->tls_hostname->u.n);
982 params->tls_hostname->type = QTYPE_QSTRING;
983 params->tls_hostname->u.s = strdup("");
984 }
985
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200986 migrate_params_test_apply(params, &tmp);
987
988 if (!migrate_params_check(&tmp, errp)) {
Peter Xu476c72a2017-07-18 11:39:05 +0800989 /* Invalid parameter */
990 return;
991 }
992
Juan Quintela73af8dd2017-10-05 21:30:10 +0200993 migrate_params_apply(params, errp);
Peter Xu476c72a2017-07-18 11:39:05 +0800994}
995
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100996
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000997void qmp_migrate_start_postcopy(Error **errp)
998{
999 MigrationState *s = migrate_get_current();
1000
1001 if (!migrate_postcopy_ram()) {
Dr. David Alan Gilberta54d3402015-11-12 11:34:44 +00001002 error_setg(errp, "Enable postcopy with migrate_set_capability before"
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +00001003 " the start of migration");
1004 return;
1005 }
1006
1007 if (s->state == MIGRATION_STATUS_NONE) {
1008 error_setg(errp, "Postcopy must be started after migration has been"
1009 " started");
1010 return;
1011 }
1012 /*
1013 * we don't error if migration has finished since that would be racy
1014 * with issuing this command.
1015 */
1016 atomic_set(&s->start_postcopy, true);
1017}
1018
aliguori065e2812008-11-11 16:46:33 +00001019/* shared migration helpers */
1020
zhanghailiang48781e52015-12-16 11:47:33 +00001021void migrate_set_state(int *state, int old_state, int new_state)
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +00001022{
Peter Xua31fede2017-08-30 16:32:01 +08001023 assert(new_state < MIGRATION_STATUS__MAX);
zhanghailiang48781e52015-12-16 11:47:33 +00001024 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
Peter Xua31fede2017-08-30 16:32:01 +08001025 trace_migrate_set_state(MigrationStatus_str(new_state));
Juan Quintelab05dc722015-07-07 14:44:05 +02001026 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +00001027 }
1028}
1029
Peter Xu4e4a3d32017-07-18 11:39:09 +08001030static MigrationCapabilityStatusList *migrate_cap_add(
1031 MigrationCapabilityStatusList *list,
1032 MigrationCapability index,
1033 bool state)
Juan Quintela2833c592017-04-05 18:32:37 +02001034{
1035 MigrationCapabilityStatusList *cap;
1036
1037 cap = g_new0(MigrationCapabilityStatusList, 1);
1038 cap->value = g_new0(MigrationCapabilityStatus, 1);
Peter Xu4e4a3d32017-07-18 11:39:09 +08001039 cap->value->capability = index;
1040 cap->value->state = state;
1041 cap->next = list;
1042
1043 return cap;
1044}
1045
1046void migrate_set_block_enabled(bool value, Error **errp)
1047{
1048 MigrationCapabilityStatusList *cap;
1049
1050 cap = migrate_cap_add(NULL, MIGRATION_CAPABILITY_BLOCK, value);
Juan Quintela2833c592017-04-05 18:32:37 +02001051 qmp_migrate_set_capabilities(cap, errp);
1052 qapi_free_MigrationCapabilityStatusList(cap);
1053}
1054
1055static void migrate_set_block_incremental(MigrationState *s, bool value)
1056{
1057 s->parameters.block_incremental = value;
1058}
1059
1060static void block_cleanup_parameters(MigrationState *s)
1061{
1062 if (s->must_remove_block_options) {
1063 /* setting to false can never fail */
1064 migrate_set_block_enabled(false, &error_abort);
1065 migrate_set_block_incremental(s, false);
1066 s->must_remove_block_options = false;
1067 }
1068}
1069
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001070static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +00001071{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001072 MigrationState *s = opaque;
1073
1074 qemu_bh_delete(s->cleanup_bh);
1075 s->cleanup_bh = NULL;
1076
Peter Xu0ceccd82018-01-03 20:20:06 +08001077 qemu_savevm_state_cleanup();
1078
zhanghailiang89a02a92016-01-15 11:37:42 +08001079 if (s->to_dst_file) {
Juan Quintelaf986c3d2016-01-14 16:52:55 +01001080 Error *local_err = NULL;
1081
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001082 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001083 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001084 if (s->migration_thread_running) {
1085 qemu_thread_join(&s->thread);
1086 s->migration_thread_running = false;
1087 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001088 qemu_mutex_lock_iothread();
1089
Juan Quintelaf986c3d2016-01-14 16:52:55 +01001090 if (multifd_save_cleanup(&local_err) != 0) {
1091 error_report_err(local_err);
1092 }
zhanghailiang89a02a92016-01-15 11:37:42 +08001093 qemu_fclose(s->to_dst_file);
1094 s->to_dst_file = NULL;
aliguori065e2812008-11-11 16:46:33 +00001095 }
1096
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001097 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
1098 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001099
Liang Li94f5a432015-11-02 15:37:00 +08001100 if (s->state == MIGRATION_STATUS_CANCELLING) {
zhanghailiang48781e52015-12-16 11:47:33 +00001101 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
Liang Li94f5a432015-11-02 15:37:00 +08001102 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001103 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001104
Juan Quintela87db1a72017-09-05 12:50:22 +02001105 if (s->error) {
1106 /* It is used on info migrate. We can't free it */
1107 error_report_err(error_copy(s->error));
1108 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001109 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +02001110 block_cleanup_parameters(s);
aliguori065e2812008-11-11 16:46:33 +00001111}
1112
Juan Quintela87db1a72017-09-05 12:50:22 +02001113void migrate_set_error(MigrationState *s, const Error *error)
1114{
1115 qemu_mutex_lock(&s->error_mutex);
1116 if (!s->error) {
1117 s->error = error_copy(error);
1118 }
1119 qemu_mutex_unlock(&s->error_mutex);
1120}
1121
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001122void migrate_fd_error(MigrationState *s, const Error *error)
aliguori065e2812008-11-11 16:46:33 +00001123{
Peter Maydell25174052016-10-21 18:41:45 +01001124 trace_migrate_fd_error(error_get_pretty(error));
zhanghailiang89a02a92016-01-15 11:37:42 +08001125 assert(s->to_dst_file == NULL);
zhanghailiang48781e52015-12-16 11:47:33 +00001126 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1127 MIGRATION_STATUS_FAILED);
Juan Quintela87db1a72017-09-05 12:50:22 +02001128 migrate_set_error(s, error);
Juan Quintela458cf282011-02-22 23:32:54 +01001129}
1130
Juan Quintela0edda1c2010-05-11 16:28:39 +02001131static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +00001132{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001133 int old_state ;
zhanghailiang89a02a92016-01-15 11:37:42 +08001134 QEMUFile *f = migrate_get_current()->to_dst_file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001135 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +00001136
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001137 if (s->rp_state.from_dst_file) {
1138 /* shutdown the rp socket, so causing the rp thread to shutdown */
1139 qemu_file_shutdown(s->rp_state.from_dst_file);
1140 }
1141
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001142 do {
1143 old_state = s->state;
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +00001144 if (!migration_is_setup_or_active(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001145 break;
1146 }
Dr. David Alan Gilberta7b36b42017-10-20 10:05:55 +01001147 /* If the migration is paused, kick it out of the pause */
1148 if (old_state == MIGRATION_STATUS_PRE_SWITCHOVER) {
1149 qemu_sem_post(&s->pause_sem);
1150 }
zhanghailiang48781e52015-12-16 11:47:33 +00001151 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
zhanghailiang31194732015-03-13 16:08:38 +08001152 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001153
1154 /*
1155 * If we're unlucky the migration code might be stuck somewhere in a
1156 * send/write while the network has failed and is waiting to timeout;
1157 * if we've got shutdown(2) available then we can force it to quit.
1158 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1159 * called in a bh, so there is no race against this cancel.
1160 */
zhanghailiang31194732015-03-13 16:08:38 +08001161 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001162 qemu_file_shutdown(f);
1163 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08001164 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
1165 Error *local_err = NULL;
1166
1167 bdrv_invalidate_cache_all(&local_err);
1168 if (local_err) {
1169 error_report_err(local_err);
1170 } else {
1171 s->block_inactive = false;
1172 }
1173 }
aliguori065e2812008-11-11 16:46:33 +00001174}
1175
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001176void add_migration_state_change_notifier(Notifier *notify)
1177{
1178 notifier_list_add(&migration_state_notifiers, notify);
1179}
1180
1181void remove_migration_state_change_notifier(Notifier *notify)
1182{
Paolo Bonzini31552522012-01-13 17:34:01 +01001183 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001184}
1185
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +02001186bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001187{
zhanghailiang31194732015-03-13 16:08:38 +08001188 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001189}
1190
Juan Quintela70736932011-02-23 00:43:59 +01001191bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001192{
zhanghailiang31194732015-03-13 16:08:38 +08001193 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001194}
Juan Quintela0edda1c2010-05-11 16:28:39 +02001195
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001196bool migration_has_failed(MigrationState *s)
1197{
zhanghailiang31194732015-03-13 16:08:38 +08001198 return (s->state == MIGRATION_STATUS_CANCELLED ||
1199 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001200}
1201
Juan Quintela57273092017-03-20 22:25:28 +01001202bool migration_in_postcopy(void)
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001203{
Juan Quintela57273092017-03-20 22:25:28 +01001204 MigrationState *s = migrate_get_current();
1205
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001206 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1207}
1208
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001209bool migration_in_postcopy_after_devices(MigrationState *s)
1210{
Juan Quintela57273092017-03-20 22:25:28 +01001211 return migration_in_postcopy() && s->postcopy_after_devices;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001212}
1213
Juan Quintelafab35002017-03-22 17:36:57 +01001214bool migration_is_idle(void)
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301215{
Juan Quintelafab35002017-03-22 17:36:57 +01001216 MigrationState *s = migrate_get_current();
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301217
1218 switch (s->state) {
1219 case MIGRATION_STATUS_NONE:
1220 case MIGRATION_STATUS_CANCELLED:
1221 case MIGRATION_STATUS_COMPLETED:
1222 case MIGRATION_STATUS_FAILED:
1223 return true;
1224 case MIGRATION_STATUS_SETUP:
1225 case MIGRATION_STATUS_CANCELLING:
1226 case MIGRATION_STATUS_ACTIVE:
1227 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1228 case MIGRATION_STATUS_COLO:
Dr. David Alan Gilbert31e06072017-10-20 10:05:51 +01001229 case MIGRATION_STATUS_PRE_SWITCHOVER:
1230 case MIGRATION_STATUS_DEVICE:
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301231 return false;
1232 case MIGRATION_STATUS__MAX:
1233 g_assert_not_reached();
1234 }
1235
1236 return false;
1237}
1238
Juan Quintelaa0762d92017-04-05 21:00:09 +02001239MigrationState *migrate_init(void)
Juan Quintela0edda1c2010-05-11 16:28:39 +02001240{
Juan Quintela17549e82011-10-05 13:50:43 +02001241 MigrationState *s = migrate_get_current();
Orit Wassermanbbf6da32012-08-06 21:42:47 +03001242
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001243 /*
1244 * Reinitialise all migration state, except
1245 * parameters/capabilities that the user set, and
1246 * locks.
1247 */
1248 s->bytes_xfer = 0;
1249 s->xfer_limit = 0;
1250 s->cleanup_bh = 0;
zhanghailiang89a02a92016-01-15 11:37:42 +08001251 s->to_dst_file = NULL;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001252 s->state = MIGRATION_STATUS_NONE;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001253 s->rp_state.from_dst_file = NULL;
1254 s->rp_state.error = false;
1255 s->mbps = 0.0;
1256 s->downtime = 0;
1257 s->expected_downtime = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001258 s->setup_time = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001259 s->start_postcopy = false;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001260 s->postcopy_after_devices = false;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001261 s->migration_thread_running = false;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001262 error_free(s->error);
1263 s->error = NULL;
Juan Quintela1299c632011-11-09 21:29:01 +01001264
zhanghailiang48781e52015-12-16 11:47:33 +00001265 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +02001266
Peter Xu4af246a2018-01-03 20:20:08 +08001267 s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1268 s->total_time = 0;
Peter Xu7287cbd2018-01-03 20:20:09 +08001269 s->vm_was_running = false;
Peter Xub15df1a2018-01-03 20:20:13 +08001270 s->iteration_initial_bytes = 0;
1271 s->threshold_size = 0;
Juan Quintela0edda1c2010-05-11 16:28:39 +02001272 return s;
1273}
Juan Quintelacab30142011-02-22 23:54:21 +01001274
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001275static GSList *migration_blockers;
1276
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301277int migrate_add_blocker(Error *reason, Error **errp)
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001278{
Peter Xu3df663e2017-06-27 12:10:15 +08001279 if (migrate_get_current()->only_migratable) {
Ashijeet Acharyab67b8c32017-01-16 17:01:54 +05301280 error_propagate(errp, error_copy(reason));
1281 error_prepend(errp, "disallowing migration blocker "
1282 "(--only_migratable) for: ");
1283 return -EACCES;
1284 }
1285
Juan Quintelafab35002017-03-22 17:36:57 +01001286 if (migration_is_idle()) {
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301287 migration_blockers = g_slist_prepend(migration_blockers, reason);
1288 return 0;
1289 }
1290
1291 error_propagate(errp, error_copy(reason));
1292 error_prepend(errp, "disallowing migration blocker (migration in "
1293 "progress) for: ");
1294 return -EBUSY;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001295}
1296
1297void migrate_del_blocker(Error *reason)
1298{
1299 migration_blockers = g_slist_remove(migration_blockers, reason);
1300}
1301
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001302void qmp_migrate_incoming(const char *uri, Error **errp)
1303{
1304 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001305 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001306
1307 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001308 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001309 return;
1310 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001311 if (!once) {
1312 error_setg(errp, "The incoming migration has already been started");
1313 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001314
1315 qemu_start_incoming_migration(uri, &local_err);
1316
1317 if (local_err) {
1318 error_propagate(errp, local_err);
1319 return;
1320 }
1321
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001322 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001323}
1324
Greg Kurz24f39022016-05-04 21:44:19 +02001325bool migration_is_blocked(Error **errp)
1326{
1327 if (qemu_savevm_state_blocked(errp)) {
1328 return true;
1329 }
1330
1331 if (migration_blockers) {
Eduardo Habkost250561e2017-06-08 10:39:05 -03001332 error_propagate(errp, error_copy(migration_blockers->data));
Greg Kurz24f39022016-05-04 21:44:19 +02001333 return true;
1334 }
1335
1336 return false;
1337}
1338
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001339void qmp_migrate(const char *uri, bool has_blk, bool blk,
1340 bool has_inc, bool inc, bool has_detach, bool detach,
1341 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001342{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +02001343 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +02001344 MigrationState *s = migrate_get_current();
Juan Quintelacab30142011-02-22 23:54:21 +01001345 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +01001346
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +00001347 if (migration_is_setup_or_active(s->state) ||
zhanghailiang0b827d52016-10-27 14:42:54 +08001348 s->state == MIGRATION_STATUS_CANCELLING ||
1349 s->state == MIGRATION_STATUS_COLO) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001350 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001351 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001352 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +01001353 if (runstate_check(RUN_STATE_INMIGRATE)) {
1354 error_setg(errp, "Guest is waiting for an incoming migration");
1355 return;
1356 }
1357
Greg Kurz24f39022016-05-04 21:44:19 +02001358 if (migration_is_blocked(errp)) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001359 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001360 }
1361
Juan Quintela2833c592017-04-05 18:32:37 +02001362 if ((has_blk && blk) || (has_inc && inc)) {
1363 if (migrate_use_block() || migrate_use_block_incremental()) {
1364 error_setg(errp, "Command options are incompatible with "
1365 "current migration capabilities");
1366 return;
1367 }
1368 migrate_set_block_enabled(true, &local_err);
1369 if (local_err) {
1370 error_propagate(errp, local_err);
1371 return;
1372 }
1373 s->must_remove_block_options = true;
1374 }
1375
1376 if (has_inc && inc) {
1377 migrate_set_block_incremental(s, true);
1378 }
1379
Juan Quintelaa0762d92017-04-05 21:00:09 +02001380 s = migrate_init();
Juan Quintelacab30142011-02-22 23:54:21 +01001381
1382 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001383 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04001384#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08001385 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04001386 rdma_start_outgoing_migration(s, p, &local_err);
1387#endif
Juan Quintelacab30142011-02-22 23:54:21 +01001388 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001389 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001390 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001391 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001392 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001393 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001394 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001395 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1396 "a valid migration protocol");
zhanghailiang48781e52015-12-16 11:47:33 +00001397 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1398 MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001399 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001400 }
1401
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001402 if (local_err) {
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001403 migrate_fd_error(s, local_err);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001404 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001405 return;
Juan Quintela1299c632011-11-09 21:29:01 +01001406 }
Juan Quintelacab30142011-02-22 23:54:21 +01001407}
1408
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02001409void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001410{
Juan Quintela17549e82011-10-05 13:50:43 +02001411 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01001412}
1413
Dr. David Alan Gilbert89cfc022017-10-20 10:05:53 +01001414void qmp_migrate_continue(MigrationStatus state, Error **errp)
1415{
1416 MigrationState *s = migrate_get_current();
1417 if (s->state != state) {
1418 error_setg(errp, "Migration not in expected state: %s",
1419 MigrationStatus_str(s->state));
1420 return;
1421 }
1422 qemu_sem_post(&s->pause_sem);
1423}
1424
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001425void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1426{
Juan Quintela73af8dd2017-10-05 21:30:10 +02001427 MigrateSetParameters p = {
1428 .has_xbzrle_cache_size = true,
1429 .xbzrle_cache_size = value,
1430 };
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001431
Juan Quintela73af8dd2017-10-05 21:30:10 +02001432 qmp_migrate_set_parameters(&p, errp);
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001433}
1434
1435int64_t qmp_query_migrate_cache_size(Error **errp)
1436{
1437 return migrate_xbzrle_cache_size();
1438}
1439
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001440void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001441{
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001442 MigrateSetParameters p = {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301443 .has_max_bandwidth = true,
1444 .max_bandwidth = value,
1445 };
Juan Quintelacab30142011-02-22 23:54:21 +01001446
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301447 qmp_migrate_set_parameters(&p, errp);
Juan Quintelacab30142011-02-22 23:54:21 +01001448}
1449
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001450void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001451{
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03001452 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
1453 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
1454 "the range of 0 to %d seconds",
1455 MAX_MIGRATE_DOWNTIME_SECONDS);
1456 return;
1457 }
1458
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301459 value *= 1000; /* Convert to milliseconds */
1460 value = MAX(0, MIN(INT64_MAX, value));
1461
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001462 MigrateSetParameters p = {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301463 .has_downtime_limit = true,
1464 .downtime_limit = value,
1465 };
1466
1467 qmp_migrate_set_parameters(&p, errp);
aliguori5bb79102008-10-13 03:12:02 +00001468}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001469
Pavel Butsykin53f09a12017-02-03 18:23:20 +03001470bool migrate_release_ram(void)
1471{
1472 MigrationState *s;
1473
1474 s = migrate_get_current();
1475
1476 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
1477}
1478
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001479bool migrate_postcopy_ram(void)
1480{
1481 MigrationState *s;
1482
1483 s = migrate_get_current();
1484
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +00001485 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001486}
1487
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03001488bool migrate_postcopy(void)
1489{
1490 return migrate_postcopy_ram();
1491}
1492
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06001493bool migrate_auto_converge(void)
1494{
1495 MigrationState *s;
1496
1497 s = migrate_get_current();
1498
1499 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1500}
1501
Peter Lieven323004a2013-07-18 09:48:50 +02001502bool migrate_zero_blocks(void)
1503{
1504 MigrationState *s;
1505
1506 s = migrate_get_current();
1507
1508 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1509}
1510
Liang Li8706d2d2015-03-23 16:32:17 +08001511bool migrate_use_compression(void)
1512{
Liang Lidde4e692015-03-23 16:32:26 +08001513 MigrationState *s;
1514
1515 s = migrate_get_current();
1516
1517 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08001518}
1519
1520int migrate_compress_level(void)
1521{
1522 MigrationState *s;
1523
1524 s = migrate_get_current();
1525
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001526 return s->parameters.compress_level;
Liang Li8706d2d2015-03-23 16:32:17 +08001527}
1528
1529int migrate_compress_threads(void)
1530{
1531 MigrationState *s;
1532
1533 s = migrate_get_current();
1534
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001535 return s->parameters.compress_threads;
Liang Li8706d2d2015-03-23 16:32:17 +08001536}
1537
Liang Li3fcb38c2015-03-23 16:32:18 +08001538int migrate_decompress_threads(void)
1539{
1540 MigrationState *s;
1541
1542 s = migrate_get_current();
1543
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001544 return s->parameters.decompress_threads;
Liang Li3fcb38c2015-03-23 16:32:18 +08001545}
1546
Juan Quintelab05dc722015-07-07 14:44:05 +02001547bool migrate_use_events(void)
1548{
1549 MigrationState *s;
1550
1551 s = migrate_get_current();
1552
1553 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1554}
1555
Juan Quintela30126bb2016-01-14 12:23:00 +01001556bool migrate_use_multifd(void)
1557{
1558 MigrationState *s;
1559
1560 s = migrate_get_current();
1561
1562 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_MULTIFD];
1563}
1564
Dr. David Alan Gilbert93fbd032017-10-20 10:05:50 +01001565bool migrate_pause_before_switchover(void)
1566{
1567 MigrationState *s;
1568
1569 s = migrate_get_current();
1570
1571 return s->enabled_capabilities[
1572 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER];
1573}
1574
Juan Quintela4075fb12016-01-15 08:56:17 +01001575int migrate_multifd_channels(void)
1576{
1577 MigrationState *s;
1578
1579 s = migrate_get_current();
1580
1581 return s->parameters.x_multifd_channels;
1582}
1583
Juan Quintela0fb86602017-04-27 10:48:25 +02001584int migrate_multifd_page_count(void)
1585{
1586 MigrationState *s;
1587
1588 s = migrate_get_current();
1589
1590 return s->parameters.x_multifd_page_count;
1591}
1592
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001593int migrate_use_xbzrle(void)
1594{
1595 MigrationState *s;
1596
1597 s = migrate_get_current();
1598
1599 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1600}
1601
1602int64_t migrate_xbzrle_cache_size(void)
1603{
1604 MigrationState *s;
1605
1606 s = migrate_get_current();
1607
Juan Quintela73af8dd2017-10-05 21:30:10 +02001608 return s->parameters.xbzrle_cache_size;
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001609}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001610
Juan Quintela2833c592017-04-05 18:32:37 +02001611bool migrate_use_block(void)
1612{
1613 MigrationState *s;
1614
1615 s = migrate_get_current();
1616
1617 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
1618}
1619
Peter Xuc788ada2017-06-26 18:28:55 +08001620bool migrate_use_return_path(void)
1621{
1622 MigrationState *s;
1623
1624 s = migrate_get_current();
1625
1626 return s->enabled_capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
1627}
1628
Juan Quintela2833c592017-04-05 18:32:37 +02001629bool migrate_use_block_incremental(void)
1630{
1631 MigrationState *s;
1632
1633 s = migrate_get_current();
1634
1635 return s->parameters.block_incremental;
1636}
1637
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001638/* migration thread support */
1639/*
1640 * Something bad happened to the RP stream, mark an error
1641 * The caller shall print or trace something to indicate why
1642 */
1643static void mark_source_rp_bad(MigrationState *s)
1644{
1645 s->rp_state.error = true;
1646}
1647
1648static struct rp_cmd_args {
1649 ssize_t len; /* -1 = variable */
1650 const char *name;
1651} rp_cmd_args[] = {
1652 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1653 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1654 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001655 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1656 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001657 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1658};
1659
1660/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001661 * Process a request for pages received on the return path,
1662 * We're allowed to send more than requested (e.g. to round to our page size)
1663 * and we don't need to send pages that have already been sent.
1664 */
1665static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1666 ram_addr_t start, size_t len)
1667{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001668 long our_host_ps = getpagesize();
1669
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001670 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001671
1672 /*
1673 * Since we currently insist on matching page sizes, just sanity check
1674 * we're being asked for whole host pages.
1675 */
1676 if (start & (our_host_ps-1) ||
1677 (len & (our_host_ps-1))) {
1678 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1679 " len: %zd", __func__, start, len);
1680 mark_source_rp_bad(ms);
1681 return;
1682 }
1683
Juan Quintela96506892017-03-14 18:41:03 +01001684 if (ram_save_queue_pages(rbname, start, len)) {
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001685 mark_source_rp_bad(ms);
1686 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001687}
1688
1689/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001690 * Handles messages sent on the return path towards the source VM
1691 *
1692 */
1693static void *source_return_path_thread(void *opaque)
1694{
1695 MigrationState *ms = opaque;
1696 QEMUFile *rp = ms->rp_state.from_dst_file;
1697 uint16_t header_len, header_type;
Peter Xu568b01c2016-03-09 14:12:12 +08001698 uint8_t buf[512];
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001699 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001700 ram_addr_t start = 0; /* =0 to silence warning */
1701 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001702 int res;
1703
1704 trace_source_return_path_thread_entry();
1705 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1706 migration_is_setup_or_active(ms->state)) {
1707 trace_source_return_path_thread_loop_top();
1708 header_type = qemu_get_be16(rp);
1709 header_len = qemu_get_be16(rp);
1710
1711 if (header_type >= MIG_RP_MSG_MAX ||
1712 header_type == MIG_RP_MSG_INVALID) {
1713 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1714 header_type, header_len);
1715 mark_source_rp_bad(ms);
1716 goto out;
1717 }
1718
1719 if ((rp_cmd_args[header_type].len != -1 &&
1720 header_len != rp_cmd_args[header_type].len) ||
Peter Xu568b01c2016-03-09 14:12:12 +08001721 header_len > sizeof(buf)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001722 error_report("RP: Received '%s' message (0x%04x) with"
1723 "incorrect length %d expecting %zu",
1724 rp_cmd_args[header_type].name, header_type, header_len,
1725 (size_t)rp_cmd_args[header_type].len);
1726 mark_source_rp_bad(ms);
1727 goto out;
1728 }
1729
1730 /* We know we've got a valid header by this point */
1731 res = qemu_get_buffer(rp, buf, header_len);
1732 if (res != header_len) {
1733 error_report("RP: Failed reading data for message 0x%04x"
1734 " read %d expected %d",
1735 header_type, res, header_len);
1736 mark_source_rp_bad(ms);
1737 goto out;
1738 }
1739
1740 /* OK, we have the message and the data */
1741 switch (header_type) {
1742 case MIG_RP_MSG_SHUT:
Peter Maydell4d885132016-06-10 17:09:22 +01001743 sibling_error = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001744 trace_source_return_path_thread_shut(sibling_error);
1745 if (sibling_error) {
1746 error_report("RP: Sibling indicated error %d", sibling_error);
1747 mark_source_rp_bad(ms);
1748 }
1749 /*
1750 * We'll let the main thread deal with closing the RP
1751 * we could do a shutdown(2) on it, but we're the only user
1752 * anyway, so there's nothing gained.
1753 */
1754 goto out;
1755
1756 case MIG_RP_MSG_PONG:
Peter Maydell4d885132016-06-10 17:09:22 +01001757 tmp32 = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001758 trace_source_return_path_thread_pong(tmp32);
1759 break;
1760
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001761 case MIG_RP_MSG_REQ_PAGES:
Peter Maydell4d885132016-06-10 17:09:22 +01001762 start = ldq_be_p(buf);
1763 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001764 migrate_handle_rp_req_pages(ms, NULL, start, len);
1765 break;
1766
1767 case MIG_RP_MSG_REQ_PAGES_ID:
1768 expected_len = 12 + 1; /* header + termination */
1769
1770 if (header_len >= expected_len) {
Peter Maydell4d885132016-06-10 17:09:22 +01001771 start = ldq_be_p(buf);
1772 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001773 /* Now we expect an idstr */
1774 tmp32 = buf[12]; /* Length of the following idstr */
1775 buf[13 + tmp32] = '\0';
1776 expected_len += tmp32;
1777 }
1778 if (header_len != expected_len) {
1779 error_report("RP: Req_Page_id with length %d expecting %zd",
1780 header_len, expected_len);
1781 mark_source_rp_bad(ms);
1782 goto out;
1783 }
1784 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
1785 break;
1786
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001787 default:
1788 break;
1789 }
1790 }
Dr. David Alan Gilbert5df54162015-11-18 11:48:41 +00001791 if (qemu_file_get_error(rp)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001792 trace_source_return_path_thread_bad_end();
1793 mark_source_rp_bad(ms);
1794 }
1795
1796 trace_source_return_path_thread_end();
1797out:
1798 ms->rp_state.from_dst_file = NULL;
1799 qemu_fclose(rp);
1800 return NULL;
1801}
1802
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001803static int open_return_path_on_source(MigrationState *ms)
1804{
1805
zhanghailiang89a02a92016-01-15 11:37:42 +08001806 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001807 if (!ms->rp_state.from_dst_file) {
1808 return -1;
1809 }
1810
1811 trace_open_return_path_on_source();
1812 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1813 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1814
1815 trace_open_return_path_on_source_continue();
1816
1817 return 0;
1818}
1819
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001820/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1821static int await_return_path_close_on_source(MigrationState *ms)
1822{
1823 /*
1824 * If this is a normal exit then the destination will send a SHUT and the
1825 * rp_thread will exit, however if there's an error we need to cause
1826 * it to exit.
1827 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001828 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001829 /*
1830 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1831 * waiting for the destination.
1832 */
1833 qemu_file_shutdown(ms->rp_state.from_dst_file);
1834 mark_source_rp_bad(ms);
1835 }
1836 trace_await_return_path_close_on_source_joining();
1837 qemu_thread_join(&ms->rp_state.rp_thread);
1838 trace_await_return_path_close_on_source_close();
1839 return ms->rp_state.error;
1840}
1841
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001842/*
1843 * Switch from normal iteration to postcopy
1844 * Returns non-0 on error
1845 */
Peter Xu7287cbd2018-01-03 20:20:09 +08001846static int postcopy_start(MigrationState *ms)
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001847{
1848 int ret;
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001849 QIOChannelBuffer *bioc;
1850 QEMUFile *fb;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001851 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001852 bool restart_block = false;
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01001853 int cur_state = MIGRATION_STATUS_ACTIVE;
1854 if (!migrate_pause_before_switchover()) {
1855 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
1856 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1857 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001858
1859 trace_postcopy_start();
1860 qemu_mutex_lock_iothread();
1861 trace_postcopy_start_set_run();
1862
1863 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001864 global_state_store();
1865 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001866 if (ret < 0) {
1867 goto fail;
1868 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001869
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01001870 ret = migration_maybe_pause(ms, &cur_state,
1871 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1872 if (ret < 0) {
1873 goto fail;
1874 }
1875
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001876 ret = bdrv_inactivate_all();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001877 if (ret < 0) {
1878 goto fail;
1879 }
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001880 restart_block = true;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001881
1882 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001883 * Cause any non-postcopiable, but iterative devices to
1884 * send out their final data.
1885 */
Fam Zhenga1fbe752017-06-17 00:06:58 +08001886 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001887
1888 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001889 * in Finish migrate and with the io-lock held everything should
1890 * be quiet, but we've potentially still got dirty pages and we
1891 * need to tell the destination to throw any pages it's already received
1892 * that are dirty
1893 */
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03001894 if (migrate_postcopy_ram()) {
1895 if (ram_postcopy_send_discard_bitmap(ms)) {
1896 error_report("postcopy send discard bitmap failed");
1897 goto fail;
1898 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001899 }
1900
1901 /*
1902 * send rest of state - note things that are doing postcopy
1903 * will notice we're in POSTCOPY_ACTIVE and not actually
1904 * wrap their state up here
1905 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001906 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03001907 if (migrate_postcopy_ram()) {
1908 /* Ping just for debugging, helps line traces up */
1909 qemu_savevm_send_ping(ms->to_dst_file, 2);
1910 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001911
1912 /*
1913 * While loading the device state we may trigger page transfer
1914 * requests and the fd must be free to process those, and thus
1915 * the destination must read the whole device state off the fd before
1916 * it starts processing it. Unfortunately the ad-hoc migration format
1917 * doesn't allow the destination to know the size to read without fully
1918 * parsing it through each devices load-state code (especially the open
1919 * coded devices that use get/put).
1920 * So we wrap the device state up in a package with a length at the start;
1921 * to do this we use a qemu_buf to hold the whole of the device state.
1922 */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001923 bioc = qio_channel_buffer_new(4096);
Daniel P. Berrange6f01f132016-09-30 11:57:14 +01001924 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001925 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
1926 object_unref(OBJECT(bioc));
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001927
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001928 /*
1929 * Make sure the receiver can get incoming pages before we send the rest
1930 * of the state
1931 */
1932 qemu_savevm_send_postcopy_listen(fb);
1933
Fam Zhenga1fbe752017-06-17 00:06:58 +08001934 qemu_savevm_state_complete_precopy(fb, false, false);
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03001935 if (migrate_postcopy_ram()) {
1936 qemu_savevm_send_ping(fb, 3);
1937 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001938
1939 qemu_savevm_send_postcopy_run(fb);
1940
1941 /* <><> end of stuff going into the package */
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001942
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001943 /* Last point of recovery; as soon as we send the package the destination
1944 * can open devices and potentially start running.
1945 * Lets just check again we've not got any errors.
1946 */
1947 ret = qemu_file_get_error(ms->to_dst_file);
1948 if (ret) {
1949 error_report("postcopy_start: Migration stream errored (pre package)");
1950 goto fail_closefb;
1951 }
1952
1953 restart_block = false;
1954
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001955 /* Now send that blob */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001956 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001957 goto fail_closefb;
1958 }
1959 qemu_fclose(fb);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001960
1961 /* Send a notify to give a chance for anything that needs to happen
1962 * at the transition to postcopy and after the device state; in particular
1963 * spice needs to trigger a transition now
1964 */
1965 ms->postcopy_after_devices = true;
1966 notifier_list_notify(&migration_state_notifiers, ms);
1967
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001968 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
1969
1970 qemu_mutex_unlock_iothread();
1971
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03001972 if (migrate_postcopy_ram()) {
1973 /*
1974 * Although this ping is just for debug, it could potentially be
1975 * used for getting a better measurement of downtime at the source.
1976 */
1977 qemu_savevm_send_ping(ms->to_dst_file, 4);
1978 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001979
Pavel Butsykinced1c612017-02-03 18:23:21 +03001980 if (migrate_release_ram()) {
1981 ram_postcopy_migrated_memory_release(ms);
1982 }
1983
zhanghailiang89a02a92016-01-15 11:37:42 +08001984 ret = qemu_file_get_error(ms->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001985 if (ret) {
1986 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00001987 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001988 MIGRATION_STATUS_FAILED);
1989 }
1990
1991 return ret;
1992
1993fail_closefb:
1994 qemu_fclose(fb);
1995fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001996 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001997 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001998 if (restart_block) {
1999 /* A failure happened early enough that we know the destination hasn't
2000 * accessed block devices, so we're safe to recover.
2001 */
2002 Error *local_err = NULL;
2003
2004 bdrv_invalidate_cache_all(&local_err);
2005 if (local_err) {
2006 error_report_err(local_err);
2007 }
2008 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002009 qemu_mutex_unlock_iothread();
2010 return -1;
2011}
2012
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002013/**
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002014 * migration_maybe_pause: Pause if required to by
2015 * migrate_pause_before_switchover called with the iothread locked
2016 * Returns: 0 on success
2017 */
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002018static int migration_maybe_pause(MigrationState *s,
2019 int *current_active_state,
2020 int new_state)
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002021{
2022 if (!migrate_pause_before_switchover()) {
2023 return 0;
2024 }
2025
2026 /* Since leaving this state is not atomic with posting the semaphore
2027 * it's possible that someone could have issued multiple migrate_continue
2028 * and the semaphore is incorrectly positive at this point;
2029 * the docs say it's undefined to reinit a semaphore that's already
2030 * init'd, so use timedwait to eat up any existing posts.
2031 */
2032 while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) {
2033 /* This block intentionally left blank */
2034 }
2035
2036 qemu_mutex_unlock_iothread();
2037 migrate_set_state(&s->state, *current_active_state,
2038 MIGRATION_STATUS_PRE_SWITCHOVER);
2039 qemu_sem_wait(&s->pause_sem);
2040 migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER,
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002041 new_state);
2042 *current_active_state = new_state;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002043 qemu_mutex_lock_iothread();
2044
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002045 return s->state == new_state ? 0 : -EINVAL;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002046}
2047
2048/**
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002049 * migration_completion: Used by migration_thread when there's not much left.
2050 * The caller 'breaks' the loop when this returns.
2051 *
2052 * @s: Current migration state
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002053 */
Peter Xu2ad87302018-01-03 20:20:14 +08002054static void migration_completion(MigrationState *s)
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002055{
2056 int ret;
Peter Xu2ad87302018-01-03 20:20:14 +08002057 int current_active_state = s->state;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002058
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002059 if (s->state == MIGRATION_STATUS_ACTIVE) {
2060 qemu_mutex_lock_iothread();
Peter Xu64909f92018-01-03 20:20:10 +08002061 s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002062 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
Peter Xu7287cbd2018-01-03 20:20:09 +08002063 s->vm_was_running = runstate_is_running();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002064 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002065
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002066 if (!ret) {
Fam Zhenga1fbe752017-06-17 00:06:58 +08002067 bool inactivate = !migrate_colo_enabled();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002068 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02002069 if (ret >= 0) {
Dr. David Alan Gilbert0331c8c2017-10-20 10:05:56 +01002070 ret = migration_maybe_pause(s, &current_active_state,
2071 MIGRATION_STATUS_DEVICE);
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002072 }
2073 if (ret >= 0) {
Kevin Wolff07fa4c2017-05-22 17:10:38 +02002074 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
Fam Zhenga1fbe752017-06-17 00:06:58 +08002075 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
2076 inactivate);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02002077 }
Fam Zhenga1fbe752017-06-17 00:06:58 +08002078 if (inactivate && ret >= 0) {
2079 s->block_inactive = true;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002080 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002081 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002082 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002083
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002084 if (ret < 0) {
2085 goto fail;
2086 }
2087 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2088 trace_migration_completion_postcopy_end();
2089
zhanghailiang89a02a92016-01-15 11:37:42 +08002090 qemu_savevm_state_complete_postcopy(s->to_dst_file);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002091 trace_migration_completion_postcopy_end_after_complete();
2092 }
2093
2094 /*
2095 * If rp was opened we must clean up the thread before
2096 * cleaning everything else up (since if there are no failures
2097 * it will wait for the destination to send it's status in
2098 * a SHUT command).
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002099 */
Peter Xu0425dc92017-05-31 18:35:34 +08002100 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002101 int rp_error;
Peter Xu0425dc92017-05-31 18:35:34 +08002102 trace_migration_return_path_end_before();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002103 rp_error = await_return_path_close_on_source(s);
Peter Xu0425dc92017-05-31 18:35:34 +08002104 trace_migration_return_path_end_after(rp_error);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002105 if (rp_error) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02002106 goto fail_invalidate;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00002107 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002108 }
2109
zhanghailiang89a02a92016-01-15 11:37:42 +08002110 if (qemu_file_get_error(s->to_dst_file)) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002111 trace_migration_completion_file_err();
Greg Kurzfe904ea2016-05-18 15:44:36 +02002112 goto fail_invalidate;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002113 }
2114
zhanghailiang0b827d52016-10-27 14:42:54 +08002115 if (!migrate_colo_enabled()) {
2116 migrate_set_state(&s->state, current_active_state,
2117 MIGRATION_STATUS_COMPLETED);
2118 }
2119
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002120 return;
2121
Greg Kurzfe904ea2016-05-18 15:44:36 +02002122fail_invalidate:
2123 /* If not doing postcopy, vm_start() will be called: let's regain
2124 * control on images.
2125 */
Dr. David Alan Gilbert6039dd52018-02-05 09:13:37 +00002126 if (s->state == MIGRATION_STATUS_ACTIVE ||
2127 s->state == MIGRATION_STATUS_DEVICE) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02002128 Error *local_err = NULL;
2129
zhanghailiang1d2acc32017-01-24 15:59:52 +08002130 qemu_mutex_lock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02002131 bdrv_invalidate_cache_all(&local_err);
2132 if (local_err) {
2133 error_report_err(local_err);
zhanghailiang1d2acc32017-01-24 15:59:52 +08002134 } else {
2135 s->block_inactive = false;
Greg Kurzfe904ea2016-05-18 15:44:36 +02002136 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08002137 qemu_mutex_unlock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02002138 }
2139
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002140fail:
zhanghailiang48781e52015-12-16 11:47:33 +00002141 migrate_set_state(&s->state, current_active_state,
2142 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002143}
2144
zhanghailiang35a6ed42016-10-27 14:42:52 +08002145bool migrate_colo_enabled(void)
2146{
2147 MigrationState *s = migrate_get_current();
2148 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
2149}
2150
Peter Xucf011f02018-01-03 20:20:11 +08002151static void migration_calculate_complete(MigrationState *s)
2152{
2153 uint64_t bytes = qemu_ftell(s->to_dst_file);
2154 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2155
2156 s->total_time = end_time - s->start_time;
2157 if (!s->downtime) {
2158 /*
2159 * It's still not set, so we are precopy migration. For
2160 * postcopy, downtime is calculated during postcopy_start().
2161 */
2162 s->downtime = end_time - s->downtime_start;
2163 }
2164
2165 if (s->total_time) {
2166 s->mbps = ((double) bytes * 8.0) / s->total_time / 1000;
2167 }
2168}
2169
Peter Xub15df1a2018-01-03 20:20:13 +08002170static void migration_update_counters(MigrationState *s,
2171 int64_t current_time)
2172{
2173 uint64_t transferred, time_spent;
Peter Xub15df1a2018-01-03 20:20:13 +08002174 double bandwidth;
2175
2176 if (current_time < s->iteration_start_time + BUFFER_DELAY) {
2177 return;
2178 }
2179
2180 transferred = qemu_ftell(s->to_dst_file) - s->iteration_initial_bytes;
2181 time_spent = current_time - s->iteration_start_time;
2182 bandwidth = (double)transferred / time_spent;
Wei Wang0781c1e2018-01-22 19:36:39 +08002183 s->threshold_size = bandwidth * s->parameters.downtime_limit;
Peter Xub15df1a2018-01-03 20:20:13 +08002184
2185 s->mbps = (((double) transferred * 8.0) /
2186 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
2187
2188 /*
2189 * if we haven't sent anything, we don't want to
2190 * recalculate. 10000 is a small enough number for our purposes
2191 */
2192 if (ram_counters.dirty_pages_rate && transferred > 10000) {
2193 s->expected_downtime = ram_counters.dirty_pages_rate *
2194 qemu_target_page_size() / bandwidth;
2195 }
2196
2197 qemu_file_reset_rate_limit(s->to_dst_file);
2198
2199 s->iteration_start_time = current_time;
2200 s->iteration_initial_bytes = qemu_ftell(s->to_dst_file);
2201
2202 trace_migrate_transferred(transferred, time_spent,
Wei Wang0781c1e2018-01-22 19:36:39 +08002203 bandwidth, s->threshold_size);
Peter Xub15df1a2018-01-03 20:20:13 +08002204}
2205
Peter Xu2ad87302018-01-03 20:20:14 +08002206/* Migration thread iteration status */
2207typedef enum {
2208 MIG_ITERATE_RESUME, /* Resume current iteration */
2209 MIG_ITERATE_SKIP, /* Skip current iteration */
2210 MIG_ITERATE_BREAK, /* Break the loop */
2211} MigIterateState;
2212
2213/*
2214 * Return true if continue to the next iteration directly, false
2215 * otherwise.
2216 */
2217static MigIterateState migration_iteration_run(MigrationState *s)
2218{
2219 uint64_t pending_size, pend_post, pend_nonpost;
2220 bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE;
2221
2222 qemu_savevm_state_pending(s->to_dst_file, s->threshold_size,
2223 &pend_nonpost, &pend_post);
2224 pending_size = pend_nonpost + pend_post;
2225
2226 trace_migrate_pending(pending_size, s->threshold_size,
2227 pend_post, pend_nonpost);
2228
2229 if (pending_size && pending_size >= s->threshold_size) {
2230 /* Still a significant amount to transfer */
2231 if (migrate_postcopy() && !in_postcopy &&
2232 pend_nonpost <= s->threshold_size &&
2233 atomic_read(&s->start_postcopy)) {
2234 if (postcopy_start(s)) {
2235 error_report("%s: postcopy failed to start", __func__);
2236 }
2237 return MIG_ITERATE_SKIP;
2238 }
2239 /* Just another iteration step */
2240 qemu_savevm_state_iterate(s->to_dst_file,
2241 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
2242 } else {
2243 trace_migration_thread_low_pending(pending_size);
2244 migration_completion(s);
2245 return MIG_ITERATE_BREAK;
2246 }
2247
2248 return MIG_ITERATE_RESUME;
2249}
2250
Peter Xu199aa6d2018-01-03 20:20:15 +08002251static void migration_iteration_finish(MigrationState *s)
2252{
2253 /* If we enabled cpu throttling for auto-converge, turn it off. */
2254 cpu_throttle_stop();
2255
2256 qemu_mutex_lock_iothread();
2257 switch (s->state) {
2258 case MIGRATION_STATUS_COMPLETED:
2259 migration_calculate_complete(s);
2260 runstate_set(RUN_STATE_POSTMIGRATE);
2261 break;
2262
2263 case MIGRATION_STATUS_ACTIVE:
2264 /*
2265 * We should really assert here, but since it's during
2266 * migration, let's try to reduce the usage of assertions.
2267 */
2268 if (!migrate_colo_enabled()) {
2269 error_report("%s: critical error: calling COLO code without "
2270 "COLO enabled", __func__);
2271 }
2272 migrate_start_colo_process(s);
2273 /*
2274 * Fixme: we will run VM in COLO no matter its old running state.
2275 * After exited COLO, we will keep running.
2276 */
2277 s->vm_was_running = true;
2278 /* Fallthrough */
2279 case MIGRATION_STATUS_FAILED:
2280 case MIGRATION_STATUS_CANCELLED:
2281 if (s->vm_was_running) {
2282 vm_start();
2283 } else {
2284 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
2285 runstate_set(RUN_STATE_POSTMIGRATE);
2286 }
2287 }
2288 break;
2289
2290 default:
2291 /* Should not reach here, but if so, forgive the VM. */
2292 error_report("%s: Unknown ending state %d", __func__, s->state);
2293 break;
2294 }
2295 qemu_bh_schedule(s->cleanup_bh);
2296 qemu_mutex_unlock_iothread();
2297}
2298
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002299/*
2300 * Master migration thread on the source VM.
2301 * It drives the migration and pumps the data down the outgoing channel.
2302 */
Juan Quintela5f496a12013-02-22 17:36:30 +01002303static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002304{
Juan Quintela9848a402012-12-19 09:55:50 +01002305 MigrationState *s = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +01002306 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Juan Quintela76f59332012-10-03 20:16:24 +02002307
Paolo Bonziniab28bd22015-07-09 08:55:38 +02002308 rcu_register_thread();
2309
Peter Xub15df1a2018-01-03 20:20:13 +08002310 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2311
zhanghailiang89a02a92016-01-15 11:37:42 +08002312 qemu_savevm_state_header(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002313
Peter Xu62a02652017-06-14 15:55:58 +08002314 /*
2315 * If we opened the return path, we need to make sure dst has it
2316 * opened as well.
2317 */
2318 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002319 /* Now tell the dest that it should open its end so it can reply */
zhanghailiang89a02a92016-01-15 11:37:42 +08002320 qemu_savevm_send_open_return_path(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002321
2322 /* And do a ping that will make stuff easier to debug */
zhanghailiang89a02a92016-01-15 11:37:42 +08002323 qemu_savevm_send_ping(s->to_dst_file, 1);
Peter Xu0425dc92017-05-31 18:35:34 +08002324 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002325
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002326 if (migrate_postcopy()) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002327 /*
2328 * Tell the destination that we *might* want to do postcopy later;
2329 * if the other end can't do postcopy it should fail now, nice and
2330 * early.
2331 */
zhanghailiang89a02a92016-01-15 11:37:42 +08002332 qemu_savevm_send_postcopy_advise(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002333 }
2334
Juan Quintela9907e842017-06-28 11:52:24 +02002335 qemu_savevm_state_setup(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002336
Alex Blighbc72ad62013-08-21 16:03:08 +01002337 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang48781e52015-12-16 11:47:33 +00002338 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2339 MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04002340
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00002341 trace_migration_thread_setup_complete();
2342
2343 while (s->state == MIGRATION_STATUS_ACTIVE ||
2344 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +01002345 int64_t current_time;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002346
zhanghailiang89a02a92016-01-15 11:37:42 +08002347 if (!qemu_file_rate_limit(s->to_dst_file)) {
Peter Xu2ad87302018-01-03 20:20:14 +08002348 MigIterateState iter_state = migration_iteration_run(s);
2349 if (iter_state == MIG_ITERATE_SKIP) {
2350 continue;
2351 } else if (iter_state == MIG_ITERATE_BREAK) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002352 break;
Juan Quintelac369f402012-10-03 20:33:34 +02002353 }
2354 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01002355
zhanghailiang89a02a92016-01-15 11:37:42 +08002356 if (qemu_file_get_error(s->to_dst_file)) {
Peter Xu2ad87302018-01-03 20:20:14 +08002357 if (migration_is_setup_or_active(s->state)) {
2358 migrate_set_state(&s->state, s->state,
2359 MIGRATION_STATUS_FAILED);
2360 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002361 trace_migration_thread_file_err();
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01002362 break;
2363 }
Peter Xub15df1a2018-01-03 20:20:13 +08002364
Alex Blighbc72ad62013-08-21 16:03:08 +01002365 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002366
Peter Xub15df1a2018-01-03 20:20:13 +08002367 migration_update_counters(s, current_time);
Michael R. Hines7e114f82013-06-25 21:35:30 -04002368
zhanghailiang89a02a92016-01-15 11:37:42 +08002369 if (qemu_file_rate_limit(s->to_dst_file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002370 /* usleep expects microseconds */
Peter Xub15df1a2018-01-03 20:20:13 +08002371 g_usleep((s->iteration_start_time + BUFFER_DELAY -
2372 current_time) * 1000);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002373 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002374 }
2375
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002376 trace_migration_thread_after_loop();
Peter Xu199aa6d2018-01-03 20:20:15 +08002377 migration_iteration_finish(s);
Paolo Bonziniab28bd22015-07-09 08:55:38 +02002378 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002379 return NULL;
2380}
2381
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00002382void migrate_fd_connect(MigrationState *s, Error *error_in)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002383{
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302384 s->expected_downtime = s->parameters.downtime_limit;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01002385 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Dr. David Alan Gilbertcce80402017-12-15 17:16:54 +00002386 if (error_in) {
2387 migrate_fd_error(s, error_in);
2388 migrate_fd_cleanup(s);
2389 return;
2390 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002391
Daniel P. Berrange9e4d2b92016-04-27 11:04:57 +01002392 qemu_file_set_blocking(s->to_dst_file, true);
zhanghailiang89a02a92016-01-15 11:37:42 +08002393 qemu_file_set_rate_limit(s->to_dst_file,
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302394 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
Paolo Bonzini442773c2013-02-22 17:36:44 +01002395
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02002396 /* Notify before starting migration thread */
2397 notifier_list_notify(&migration_state_notifiers, s);
2398
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002399 /*
Peter Xuc788ada2017-06-26 18:28:55 +08002400 * Open the return path. For postcopy, it is used exclusively. For
2401 * precopy, only if user specified "return-path" capability would
2402 * QEMU uses the return path.
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002403 */
Peter Xuc788ada2017-06-26 18:28:55 +08002404 if (migrate_postcopy_ram() || migrate_use_return_path()) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002405 if (open_return_path_on_source(s)) {
2406 error_report("Unable to open return-path for postcopy");
zhanghailiang48781e52015-12-16 11:47:33 +00002407 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002408 MIGRATION_STATUS_FAILED);
2409 migrate_fd_cleanup(s);
2410 return;
2411 }
2412 }
2413
Juan Quintelaf986c3d2016-01-14 16:52:55 +01002414 if (multifd_save_setup() != 0) {
2415 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2416 MIGRATION_STATUS_FAILED);
2417 migrate_fd_cleanup(s);
2418 return;
2419 }
Pankaj Gupta009fad72017-01-23 19:12:56 +05302420 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01002421 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002422 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002423}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00002424
Peter Xu9d18af92017-06-27 12:10:19 +08002425void migration_global_dump(Monitor *mon)
2426{
2427 MigrationState *ms = migrate_get_current();
2428
Juan Quintela6f0f6422017-10-26 11:49:57 +02002429 monitor_printf(mon, "globals:\n");
2430 monitor_printf(mon, "store-global-state: %s\n",
2431 ms->store_global_state ? "on" : "off");
2432 monitor_printf(mon, "only-migratable: %s\n",
2433 ms->only_migratable ? "on" : "off");
2434 monitor_printf(mon, "send-configuration: %s\n",
2435 ms->send_configuration ? "on" : "off");
2436 monitor_printf(mon, "send-section-footer: %s\n",
2437 ms->send_section_footer ? "on" : "off");
Peter Xu9d18af92017-06-27 12:10:19 +08002438}
2439
Peter Xu20814752017-07-18 11:39:03 +08002440#define DEFINE_PROP_MIG_CAP(name, x) \
2441 DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
2442
Peter Xu52722982017-06-27 12:10:14 +08002443static Property migration_properties[] = {
2444 DEFINE_PROP_BOOL("store-global-state", MigrationState,
2445 store_global_state, true),
Peter Xu3df663e2017-06-27 12:10:15 +08002446 DEFINE_PROP_BOOL("only-migratable", MigrationState, only_migratable, false),
Peter Xu71dd4c12017-06-27 12:10:16 +08002447 DEFINE_PROP_BOOL("send-configuration", MigrationState,
2448 send_configuration, true),
Peter Xu15c38502017-06-27 12:10:17 +08002449 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
2450 send_section_footer, true),
Peter Xu89632fa2017-07-18 11:39:02 +08002451
2452 /* Migration parameters */
Juan Quintela741d4082017-12-01 13:08:38 +01002453 DEFINE_PROP_UINT8("x-compress-level", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002454 parameters.compress_level,
2455 DEFAULT_MIGRATE_COMPRESS_LEVEL),
Juan Quintela741d4082017-12-01 13:08:38 +01002456 DEFINE_PROP_UINT8("x-compress-threads", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002457 parameters.compress_threads,
2458 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
Juan Quintela741d4082017-12-01 13:08:38 +01002459 DEFINE_PROP_UINT8("x-decompress-threads", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002460 parameters.decompress_threads,
2461 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
Juan Quintela741d4082017-12-01 13:08:38 +01002462 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002463 parameters.cpu_throttle_initial,
2464 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
Juan Quintela741d4082017-12-01 13:08:38 +01002465 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002466 parameters.cpu_throttle_increment,
2467 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
Juan Quintela741d4082017-12-01 13:08:38 +01002468 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002469 parameters.max_bandwidth, MAX_THROTTLE),
Juan Quintela741d4082017-12-01 13:08:38 +01002470 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002471 parameters.downtime_limit,
2472 DEFAULT_MIGRATE_SET_DOWNTIME),
Juan Quintela741d4082017-12-01 13:08:38 +01002473 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
Peter Xu89632fa2017-07-18 11:39:02 +08002474 parameters.x_checkpoint_delay,
2475 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
Juan Quintela741d4082017-12-01 13:08:38 +01002476 DEFINE_PROP_UINT8("x-multifd-channels", MigrationState,
Juan Quintela4075fb12016-01-15 08:56:17 +01002477 parameters.x_multifd_channels,
2478 DEFAULT_MIGRATE_MULTIFD_CHANNELS),
Juan Quintela741d4082017-12-01 13:08:38 +01002479 DEFINE_PROP_UINT32("x-multifd-page-count", MigrationState,
Juan Quintela0fb86602017-04-27 10:48:25 +02002480 parameters.x_multifd_page_count,
2481 DEFAULT_MIGRATE_MULTIFD_PAGE_COUNT),
Juan Quintela73af8dd2017-10-05 21:30:10 +02002482 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
2483 parameters.xbzrle_cache_size,
2484 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
Peter Xu20814752017-07-18 11:39:03 +08002485
2486 /* Migration capabilities */
2487 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
2488 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
2489 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
2490 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
2491 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
2492 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
2493 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
2494 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
2495 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
2496 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
2497 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
Juan Quintela30126bb2016-01-14 12:23:00 +01002498 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_X_MULTIFD),
Peter Xu20814752017-07-18 11:39:03 +08002499
Peter Xu52722982017-06-27 12:10:14 +08002500 DEFINE_PROP_END_OF_LIST(),
2501};
2502
Peter Xue5cb7e72017-06-27 12:10:13 +08002503static void migration_class_init(ObjectClass *klass, void *data)
2504{
2505 DeviceClass *dc = DEVICE_CLASS(klass);
2506
2507 dc->user_creatable = false;
Peter Xu52722982017-06-27 12:10:14 +08002508 dc->props = migration_properties;
Peter Xue5cb7e72017-06-27 12:10:13 +08002509}
2510
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01002511static void migration_instance_finalize(Object *obj)
2512{
2513 MigrationState *ms = MIGRATION_OBJ(obj);
2514 MigrationParameters *params = &ms->parameters;
2515
Juan Quintela87db1a72017-09-05 12:50:22 +02002516 qemu_mutex_destroy(&ms->error_mutex);
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01002517 g_free(params->tls_hostname);
2518 g_free(params->tls_creds);
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002519 qemu_sem_destroy(&ms->pause_sem);
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01002520}
2521
Peter Xue5cb7e72017-06-27 12:10:13 +08002522static void migration_instance_init(Object *obj)
2523{
2524 MigrationState *ms = MIGRATION_OBJ(obj);
Peter Xu8b0b29d2017-07-18 11:39:06 +08002525 MigrationParameters *params = &ms->parameters;
Peter Xue5cb7e72017-06-27 12:10:13 +08002526
2527 ms->state = MIGRATION_STATUS_NONE;
Peter Xue5cb7e72017-06-27 12:10:13 +08002528 ms->mbps = -1;
Dr. David Alan Gilberte91d8952017-10-20 10:05:52 +01002529 qemu_sem_init(&ms->pause_sem, 0);
Juan Quintela87db1a72017-09-05 12:50:22 +02002530 qemu_mutex_init(&ms->error_mutex);
Peter Xu8b0b29d2017-07-18 11:39:06 +08002531
2532 params->tls_hostname = g_strdup("");
2533 params->tls_creds = g_strdup("");
2534
2535 /* Set has_* up only for parameter checks */
2536 params->has_compress_level = true;
2537 params->has_compress_threads = true;
2538 params->has_decompress_threads = true;
2539 params->has_cpu_throttle_initial = true;
2540 params->has_cpu_throttle_increment = true;
2541 params->has_max_bandwidth = true;
2542 params->has_downtime_limit = true;
2543 params->has_x_checkpoint_delay = true;
2544 params->has_block_incremental = true;
Juan Quintela4075fb12016-01-15 08:56:17 +01002545 params->has_x_multifd_channels = true;
Juan Quintela0fb86602017-04-27 10:48:25 +02002546 params->has_x_multifd_page_count = true;
Juan Quintela73af8dd2017-10-05 21:30:10 +02002547 params->has_xbzrle_cache_size = true;
Peter Xu8b0b29d2017-07-18 11:39:06 +08002548}
2549
2550/*
2551 * Return true if check pass, false otherwise. Error will be put
2552 * inside errp if provided.
2553 */
2554static bool migration_object_check(MigrationState *ms, Error **errp)
2555{
Peter Xu6b19a7d2017-07-18 11:39:10 +08002556 MigrationCapabilityStatusList *head = NULL;
2557 /* Assuming all off */
2558 bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret;
2559 int i;
2560
Peter Xu8b0b29d2017-07-18 11:39:06 +08002561 if (!migrate_params_check(&ms->parameters, errp)) {
2562 return false;
2563 }
2564
Peter Xu6b19a7d2017-07-18 11:39:10 +08002565 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
2566 if (ms->enabled_capabilities[i]) {
2567 head = migrate_cap_add(head, i, true);
2568 }
2569 }
2570
2571 ret = migrate_caps_check(cap_list, head, errp);
2572
2573 /* It works with head == NULL */
2574 qapi_free_MigrationCapabilityStatusList(head);
2575
2576 return ret;
Peter Xue5cb7e72017-06-27 12:10:13 +08002577}
2578
2579static const TypeInfo migration_type = {
2580 .name = TYPE_MIGRATION,
Peter Xu01f6e142017-06-28 15:15:44 +08002581 /*
Peter Xuc8d3ff32017-07-05 16:21:23 +08002582 * NOTE: TYPE_MIGRATION is not really a device, as the object is
2583 * not created using qdev_create(), it is not attached to the qdev
2584 * device tree, and it is never realized.
2585 *
2586 * TODO: Make this TYPE_OBJECT once QOM provides something like
2587 * TYPE_DEVICE's "-global" properties.
Peter Xu01f6e142017-06-28 15:15:44 +08002588 */
Peter Xue5cb7e72017-06-27 12:10:13 +08002589 .parent = TYPE_DEVICE,
2590 .class_init = migration_class_init,
2591 .class_size = sizeof(MigrationClass),
2592 .instance_size = sizeof(MigrationState),
2593 .instance_init = migration_instance_init,
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01002594 .instance_finalize = migration_instance_finalize,
Peter Xue5cb7e72017-06-27 12:10:13 +08002595};
2596
2597static void register_migration_types(void)
2598{
2599 type_register_static(&migration_type);
2600}
2601
2602type_init(register_migration_types);