blob: 494c6eb435ea9a83986a95b387e53018c20f238a [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 Armbrustercc7a8ea2015-03-17 17:22:46 +010033#include "qapi/qmp/qerror.h"
Paolo Bonziniab28bd22015-07-09 08:55:38 +020034#include "qemu/rcu.h"
Juan Quintela2c9e6fe2017-04-21 14:31:22 +020035#include "block.h"
Juan Quintelabe07b0a2017-04-20 13:12:24 +020036#include "postcopy-ram.h"
Juan Quintela766bd172012-07-23 05:45:29 +020037#include "qemu/thread.h"
Luiz Capitulino791e7c82011-09-13 17:37:16 -030038#include "qmp-commands.h"
Kazuya Saitoc09e5bb2013-02-22 17:36:19 +010039#include "trace.h"
Juan Quintela598cd2b2015-05-20 12:16:15 +020040#include "qapi-event.h"
Juan Quintela51180422017-04-24 20:50:19 +020041#include "exec/target_page.h"
Daniel P. Berrange61b67d42016-04-27 11:05:01 +010042#include "io/channel-buffer.h"
zhanghailiang35a6ed42016-10-27 14:42:52 +080043#include "migration/colo.h"
Peter Xu4ffdb332017-06-27 12:10:18 +080044#include "hw/boards.h"
Peter Xu9d18af92017-06-27 12:10:19 +080045#include "monitor/monitor.h"
aliguori065e2812008-11-11 16:46:33 +000046
Jason J. Hernedc325622015-09-08 13:12:37 -040047#define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000048
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010049/* Amount of time to allocate to each "chunk" of bandwidth-throttled
50 * data. */
51#define BUFFER_DELAY 100
52#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
53
Ashijeet Acharya2ff30252016-09-15 21:50:28 +053054/* Time in milliseconds we are allowed to stop the source,
55 * for sending the last part */
56#define DEFAULT_MIGRATE_SET_DOWNTIME 300
57
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -030058/* Maximum migrate downtime set to 2000 seconds */
59#define MAX_MIGRATE_DOWNTIME_SECONDS 2000
60#define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
61
Liang Li8706d2d2015-03-23 16:32:17 +080062/* Default compression thread count */
63#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080064/* Default decompression thread count, usually decompression is at
65 * least 4 times as fast as compression.*/
66#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080067/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
68#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
Jason J. Herne1626fee2015-09-08 13:12:34 -040069/* Define default autoconverge cpu throttle migration parameters */
Jason J. Herned85a31d2016-04-21 14:07:18 -040070#define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
71#define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
Liang Li8706d2d2015-03-23 16:32:17 +080072
Orit Wasserman17ad9b32012-08-06 21:42:53 +030073/* Migration XBZRLE default cache size */
74#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
75
zhanghailiang68b53592016-10-27 14:43:01 +080076/* The delay time (in ms) between two COLO checkpoints
77 * Note: Please change this default value to 10000 when we support hybrid mode.
78 */
79#define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY 200
Juan Quintela4075fb12016-01-15 08:56:17 +010080#define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2
Juan Quintela0fb86602017-04-27 10:48:25 +020081#define DEFAULT_MIGRATE_MULTIFD_PAGE_COUNT 16
zhanghailiang68b53592016-10-27 14:43:01 +080082
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010083static NotifierList migration_state_notifiers =
84 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
85
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000086static bool deferred_incoming;
87
Juan Quintelada6f1792017-04-24 17:37:14 +020088/* Messages sent on the return path from destination to source */
89enum mig_rp_message_type {
90 MIG_RP_MSG_INVALID = 0, /* Must be 0 */
91 MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */
92 MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */
93
94 MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */
95 MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */
96
97 MIG_RP_MSG_MAX
98};
99
Juan Quintela17549e82011-10-05 13:50:43 +0200100/* When we add fault tolerance, we could have several
101 migrations at once. For now we don't need to add
102 dynamic creation of migration */
103
Peter Xue5cb7e72017-06-27 12:10:13 +0800104static MigrationState *current_migration;
105
Peter Xu8b0b29d2017-07-18 11:39:06 +0800106static bool migration_object_check(MigrationState *ms, Error **errp);
107
Peter Xue5cb7e72017-06-27 12:10:13 +0800108void migration_object_init(void)
109{
Peter Xu4ffdb332017-06-27 12:10:18 +0800110 MachineState *ms = MACHINE(qdev_get_machine());
Peter Xu8b0b29d2017-07-18 11:39:06 +0800111 Error *err = NULL;
Peter Xu4ffdb332017-06-27 12:10:18 +0800112
Peter Xue5cb7e72017-06-27 12:10:13 +0800113 /* This can only be called once. */
114 assert(!current_migration);
115 current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION));
Peter Xu4ffdb332017-06-27 12:10:18 +0800116
Peter Xu8b0b29d2017-07-18 11:39:06 +0800117 if (!migration_object_check(current_migration, &err)) {
118 error_report_err(err);
119 exit(1);
120 }
121
Peter Xu4ffdb332017-06-27 12:10:18 +0800122 /*
123 * We cannot really do this in migration_instance_init() since at
124 * that time global properties are not yet applied, then this
125 * value will be definitely replaced by something else.
126 */
127 if (ms->enforce_config_section) {
128 current_migration->send_configuration = true;
129 }
Peter Xue5cb7e72017-06-27 12:10:13 +0800130}
131
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100132/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +0200133MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +0200134{
Peter Xue5cb7e72017-06-27 12:10:13 +0800135 /* This can only be called after the object created. */
136 assert(current_migration);
137 return current_migration;
Juan Quintela17549e82011-10-05 13:50:43 +0200138}
139
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100140MigrationIncomingState *migration_incoming_get_current(void)
141{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100142 static bool once;
143 static MigrationIncomingState mis_current;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100144
Juan Quintelab4b076d2017-01-23 22:32:06 +0100145 if (!once) {
146 mis_current.state = MIGRATION_STATUS_NONE;
147 memset(&mis_current, 0, sizeof(MigrationIncomingState));
Juan Quintelab4b076d2017-01-23 22:32:06 +0100148 qemu_mutex_init(&mis_current.rp_mutex);
149 qemu_event_init(&mis_current.main_thread_load_event, false);
150 once = true;
151 }
152 return &mis_current;
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100153}
154
155void migration_incoming_state_destroy(void)
156{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100157 struct MigrationIncomingState *mis = migration_incoming_get_current();
158
Peter Xu34826552017-05-19 14:43:29 +0800159 if (mis->to_src_file) {
Peter Xu660819b2017-05-19 14:43:30 +0800160 /* Tell source that we are done */
161 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
Peter Xu34826552017-05-19 14:43:29 +0800162 qemu_fclose(mis->to_src_file);
163 mis->to_src_file = NULL;
164 }
165
Peter Xu660819b2017-05-19 14:43:30 +0800166 if (mis->from_src_file) {
167 qemu_fclose(mis->from_src_file);
168 mis->from_src_file = NULL;
169 }
170
Dr. David Alan Gilbert5089e182017-08-25 15:19:39 +0100171 qemu_event_reset(&mis->main_thread_load_event);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100172}
173
Juan Quintelab05dc722015-07-07 14:44:05 +0200174static void migrate_generate_event(int new_state)
175{
176 if (migrate_use_events()) {
177 qapi_event_send_migration(new_state, &error_abort);
Juan Quintelab05dc722015-07-07 14:44:05 +0200178 }
179}
180
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000181/*
182 * Called on -incoming with a defer: uri.
183 * The migration can be started later after any parameters have been
184 * changed.
185 */
186static void deferred_incoming_migration(Error **errp)
187{
188 if (deferred_incoming) {
189 error_setg(errp, "Incoming migration already deferred");
190 }
191 deferred_incoming = true;
192}
193
Juan Quintelada6f1792017-04-24 17:37:14 +0200194/*
195 * Send a message on the return channel back to the source
196 * of the migration.
197 */
198static void migrate_send_rp_message(MigrationIncomingState *mis,
199 enum mig_rp_message_type message_type,
200 uint16_t len, void *data)
201{
202 trace_migrate_send_rp_message((int)message_type, len);
203 qemu_mutex_lock(&mis->rp_mutex);
204 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
205 qemu_put_be16(mis->to_src_file, len);
206 qemu_put_buffer(mis->to_src_file, data, len);
207 qemu_fflush(mis->to_src_file);
208 qemu_mutex_unlock(&mis->rp_mutex);
209}
210
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000211/* Request a range of pages from the source VM at the given
212 * start address.
213 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
214 * as the last request (a name must have been given previously)
215 * Start: Address offset within the RB
216 * Len: Length in bytes required - must be a multiple of pagesize
217 */
218void migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
219 ram_addr_t start, size_t len)
220{
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100221 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000222 size_t msglen = 12; /* start + len */
223
224 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
225 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
226
227 if (rbname) {
228 int rbname_len = strlen(rbname);
229 assert(rbname_len < 256);
230
231 bufc[msglen++] = rbname_len;
232 memcpy(bufc + msglen, rbname, rbname_len);
233 msglen += rbname_len;
234 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES_ID, msglen, bufc);
235 } else {
236 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES, msglen, bufc);
237 }
238}
239
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200240void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000241{
aliguori34c9dd82008-10-13 03:14:31 +0000242 const char *p;
243
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200244 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000245 if (!strcmp(uri, "defer")) {
246 deferred_incoming_migration(errp);
247 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200248 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400249#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000250 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400251 rdma_start_incoming_migration(p, errp);
252#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000253 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200254 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000255 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200256 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000257 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200258 fd_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000259 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100260 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200261 }
aliguori5bb79102008-10-13 03:12:02 +0000262}
263
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300264static void process_incoming_migration_bh(void *opaque)
265{
266 Error *local_err = NULL;
267 MigrationIncomingState *mis = opaque;
268
Kevin Wolface21a52017-05-04 18:52:36 +0200269 /* Make sure all file formats flush their mutable metadata.
270 * If we get an error here, just don't restart the VM yet. */
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300271 bdrv_invalidate_cache_all(&local_err);
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200272 if (local_err) {
Kevin Wolface21a52017-05-04 18:52:36 +0200273 error_report_err(local_err);
Kevin Wolfd35ff5e2017-04-04 17:29:03 +0200274 local_err = NULL;
275 autostart = false;
276 }
277
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300278 /*
279 * This must happen after all error conditions are dealt with and
280 * we're sure the VM is going to be running on this host.
281 */
282 qemu_announce_self();
283
284 /* If global state section was not received or we are in running
285 state, we need to obey autostart. Any other state is set with
286 runstate_set. */
287
288 if (!global_state_received() ||
289 global_state_get_runstate() == RUN_STATE_RUNNING) {
290 if (autostart) {
291 vm_start();
292 } else {
293 runstate_set(RUN_STATE_PAUSED);
294 }
295 } else {
296 runstate_set(global_state_get_runstate());
297 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300298 /*
299 * This must happen after any state changes since as soon as an external
300 * observer sees this event they might start to prod at the VM assuming
301 * it's ready to use.
302 */
303 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
304 MIGRATION_STATUS_COMPLETED);
305 qemu_bh_delete(mis->bh);
306 migration_incoming_state_destroy();
307}
308
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200309static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200310{
Juan Quintelab4b076d2017-01-23 22:32:06 +0100311 MigrationIncomingState *mis = migration_incoming_get_current();
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000312 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200313 int ret;
314
Juan Quintela4f0fae72017-07-24 12:42:02 +0200315 assert(mis->from_src_file);
Dr. David Alan Gilbert67f11b52017-02-24 18:28:34 +0000316 mis->largest_page_size = qemu_ram_pagesize_largest();
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000317 postcopy_state_set(POSTCOPY_INCOMING_NONE);
zhanghailiang93d7af62015-12-16 11:47:34 +0000318 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
319 MIGRATION_STATUS_ACTIVE);
Juan Quintela4f0fae72017-07-24 12:42:02 +0200320 ret = qemu_loadvm_state(mis->from_src_file);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100321
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000322 ps = postcopy_state_get();
323 trace_process_incoming_migration_co_end(ret, ps);
324 if (ps != POSTCOPY_INCOMING_NONE) {
325 if (ps == POSTCOPY_INCOMING_ADVISE) {
326 /*
327 * Where a migration had postcopy enabled (and thus went to advise)
328 * but managed to complete within the precopy period, we can use
329 * the normal exit.
330 */
331 postcopy_ram_incoming_cleanup(mis);
332 } else if (ret >= 0) {
333 /*
334 * Postcopy was started, cleanup should happen at the end of the
335 * postcopy thread.
336 */
337 trace_process_incoming_migration_co_postcopy_end_main();
338 return;
339 }
340 /* Else if something went wrong then just fall out of the normal exit */
341 }
342
zhanghailiang25d0c162016-10-27 14:42:55 +0800343 /* we get COLO info, and know if we are in COLO mode */
344 if (!ret && migration_incoming_enable_colo()) {
345 mis->migration_incoming_co = qemu_coroutine_self();
346 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
347 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
348 mis->have_colo_incoming_thread = true;
349 qemu_coroutine_yield();
350
351 /* Wait checkpoint incoming thread exit before free resource */
352 qemu_thread_join(&mis->colo_incoming_thread);
353 }
354
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200355 if (ret < 0) {
zhanghailiang93d7af62015-12-16 11:47:34 +0000356 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
357 MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200358 error_report("load of migration failed: %s", strerror(-ret));
Dr. David Alan Gilbert3a0f2ce2017-07-17 12:09:32 +0100359 qemu_fclose(mis->from_src_file);
Eric Blake4aead692013-04-16 15:50:41 -0600360 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200361 }
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300362 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
363 qemu_bh_schedule(mis->bh);
Juan Quintela511c0232010-06-09 14:10:55 +0200364}
365
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100366void migration_fd_process_incoming(QEMUFile *f)
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200367{
Juan Quintela4f0fae72017-07-24 12:42:02 +0200368 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL);
369 MigrationIncomingState *mis = migration_incoming_get_current();
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200370
Juan Quintela4f0fae72017-07-24 12:42:02 +0200371 if (!mis->from_src_file) {
372 mis->from_src_file = f;
373 }
Daniel P. Berrange06ad5132016-04-27 11:04:56 +0100374 qemu_file_set_blocking(f, false);
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200375 qemu_coroutine_enter(co);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200376}
377
Juan Quintela4f0fae72017-07-24 12:42:02 +0200378void migration_ioc_process_incoming(QIOChannel *ioc)
379{
380 MigrationIncomingState *mis = migration_incoming_get_current();
381
382 if (!mis->from_src_file) {
383 QEMUFile *f = qemu_fopen_channel_input(ioc);
384 migration_fd_process_incoming(f);
385 }
386 /* We still only have a single channel. Nothing to do here yet */
387}
388
Juan Quintela428d8902017-07-24 13:06:25 +0200389/**
390 * @migration_has_all_channels: We have received all channels that we need
391 *
392 * Returns true when we have got connections to all the channels that
393 * we need for migration.
394 */
395bool migration_has_all_channels(void)
396{
397 return true;
398}
399
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000400/*
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000401 * Send a 'SHUT' message on the return channel with the given value
402 * to indicate that we've finished with the RP. Non-0 value indicates
403 * error.
404 */
405void migrate_send_rp_shut(MigrationIncomingState *mis,
406 uint32_t value)
407{
408 uint32_t buf;
409
410 buf = cpu_to_be32(value);
411 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
412}
413
414/*
415 * Send a 'PONG' message on the return channel with the given value
416 * (normally in response to a 'PING')
417 */
418void migrate_send_rp_pong(MigrationIncomingState *mis,
419 uint32_t value)
420{
421 uint32_t buf;
422
423 buf = cpu_to_be32(value);
424 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
425}
426
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300427MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
428{
429 MigrationCapabilityStatusList *head = NULL;
430 MigrationCapabilityStatusList *caps;
431 MigrationState *s = migrate_get_current();
432 int i;
433
Michael Tokarev387eede2013-10-05 13:18:28 +0400434 caps = NULL; /* silence compiler warning */
Eric Blake7fb1cf12015-11-18 01:52:57 -0700435 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Dr. David Alan Gilberted1701c2017-05-15 15:05:29 +0100436#ifndef CONFIG_LIVE_BLOCK_MIGRATION
437 if (i == MIGRATION_CAPABILITY_BLOCK) {
438 continue;
439 }
440#endif
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300441 if (head == NULL) {
442 head = g_malloc0(sizeof(*caps));
443 caps = head;
444 } else {
445 caps->next = g_malloc0(sizeof(*caps));
446 caps = caps->next;
447 }
448 caps->value =
449 g_malloc(sizeof(*caps->value));
450 caps->value->capability = i;
451 caps->value->state = s->enabled_capabilities[i];
452 }
453
454 return head;
455}
456
Liang Li85de8322015-03-23 16:32:28 +0800457MigrationParameters *qmp_query_migrate_parameters(Error **errp)
458{
459 MigrationParameters *params;
460 MigrationState *s = migrate_get_current();
461
Markus Armbrustere87fae42017-07-18 12:57:38 +0200462 /* TODO use QAPI_CLONE() instead of duplicating it inline */
Liang Li85de8322015-03-23 16:32:28 +0800463 params = g_malloc0(sizeof(*params));
Eric Blakede63ab62016-09-08 22:14:15 -0500464 params->has_compress_level = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100465 params->compress_level = s->parameters.compress_level;
Eric Blakede63ab62016-09-08 22:14:15 -0500466 params->has_compress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100467 params->compress_threads = s->parameters.compress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500468 params->has_decompress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100469 params->decompress_threads = s->parameters.decompress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500470 params->has_cpu_throttle_initial = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100471 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
Eric Blakede63ab62016-09-08 22:14:15 -0500472 params->has_cpu_throttle_increment = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100473 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200474 params->has_tls_creds = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100475 params->tls_creds = g_strdup(s->parameters.tls_creds);
Markus Armbruster8cc99dc2017-07-18 12:04:54 +0200476 params->has_tls_hostname = true;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100477 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530478 params->has_max_bandwidth = true;
479 params->max_bandwidth = s->parameters.max_bandwidth;
480 params->has_downtime_limit = true;
481 params->downtime_limit = s->parameters.downtime_limit;
zhanghailiangfe39a4d2016-11-02 15:42:09 +0800482 params->has_x_checkpoint_delay = true;
zhanghailiang68b53592016-10-27 14:43:01 +0800483 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
Juan Quintela2833c592017-04-05 18:32:37 +0200484 params->has_block_incremental = true;
485 params->block_incremental = s->parameters.block_incremental;
Juan Quintela4075fb12016-01-15 08:56:17 +0100486 params->has_x_multifd_channels = true;
487 params->x_multifd_channels = s->parameters.x_multifd_channels;
Juan Quintela0fb86602017-04-27 10:48:25 +0200488 params->has_x_multifd_page_count = true;
489 params->x_multifd_page_count = s->parameters.x_multifd_page_count;
Liang Li85de8322015-03-23 16:32:28 +0800490
491 return params;
492}
493
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000494/*
495 * Return true if we're already in the middle of a migration
496 * (i.e. any of the active or setup states)
497 */
498static bool migration_is_setup_or_active(int state)
499{
500 switch (state) {
501 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000502 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000503 case MIGRATION_STATUS_SETUP:
504 return true;
505
506 default:
507 return false;
508
509 }
510}
511
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100512static void populate_ram_info(MigrationInfo *info, MigrationState *s)
513{
514 info->has_ram = true;
515 info->ram = g_malloc0(sizeof(*info->ram));
Juan Quintela93604472017-06-06 19:49:03 +0200516 info->ram->transferred = ram_counters.transferred;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100517 info->ram->total = ram_bytes_total();
Juan Quintela93604472017-06-06 19:49:03 +0200518 info->ram->duplicate = ram_counters.duplicate;
Juan Quintelabedf53c2017-03-13 20:35:54 +0100519 /* legacy value. It is not used anymore */
520 info->ram->skipped = 0;
Juan Quintela93604472017-06-06 19:49:03 +0200521 info->ram->normal = ram_counters.normal;
522 info->ram->normal_bytes = ram_counters.normal *
Juan Quintela20afaed2017-03-21 09:09:14 +0100523 qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100524 info->ram->mbps = s->mbps;
Juan Quintela93604472017-06-06 19:49:03 +0200525 info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
526 info->ram->postcopy_requests = ram_counters.postcopy_requests;
Chao Fan030ce1f2017-03-21 10:22:43 +0800527 info->ram->page_size = qemu_target_page_size();
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100528
Juan Quintela114f5ae2017-05-04 10:09:21 +0200529 if (migrate_use_xbzrle()) {
530 info->has_xbzrle_cache = true;
531 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
532 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
Juan Quintela93604472017-06-06 19:49:03 +0200533 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
534 info->xbzrle_cache->pages = xbzrle_counters.pages;
535 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
536 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
537 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
Juan Quintela114f5ae2017-05-04 10:09:21 +0200538 }
539
Juan Quintela338182c2017-05-03 13:16:38 +0200540 if (cpu_throttle_active()) {
541 info->has_cpu_throttle_percentage = true;
542 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
543 }
544
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100545 if (s->state != MIGRATION_STATUS_COMPLETED) {
546 info->ram->remaining = ram_bytes_remaining();
Juan Quintela93604472017-06-06 19:49:03 +0200547 info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100548 }
549}
550
Juan Quintela930ac042017-05-04 10:21:46 +0200551static void populate_disk_info(MigrationInfo *info)
552{
553 if (blk_mig_active()) {
554 info->has_disk = true;
555 info->disk = g_malloc0(sizeof(*info->disk));
556 info->disk->transferred = blk_mig_bytes_transferred();
557 info->disk->remaining = blk_mig_bytes_remaining();
558 info->disk->total = blk_mig_bytes_total();
559 }
560}
561
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300562MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000563{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300564 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200565 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000566
Juan Quintela17549e82011-10-05 13:50:43 +0200567 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800568 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200569 /* no migration has happened ever */
570 break;
zhanghailiang31194732015-03-13 16:08:38 +0800571 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400572 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400573 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400574 break;
zhanghailiang31194732015-03-13 16:08:38 +0800575 case MIGRATION_STATUS_ACTIVE:
576 case MIGRATION_STATUS_CANCELLING:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000577 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Juan Quintelac8f9f4f2017-06-06 19:21:29 +0200578 /* TODO add some postcopy stats */
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000579 info->has_status = true;
580 info->has_total_time = true;
581 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
582 - s->total_time;
583 info->has_expected_downtime = true;
584 info->expected_downtime = s->expected_downtime;
585 info->has_setup_time = true;
586 info->setup_time = s->setup_time;
587
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100588 populate_ram_info(info, s);
Juan Quintela930ac042017-05-04 10:21:46 +0200589 populate_disk_info(info);
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000590 break;
zhanghailiang0b827d52016-10-27 14:42:54 +0800591 case MIGRATION_STATUS_COLO:
592 info->has_status = true;
593 /* TODO: display COLO specific information (checkpoint info etc.) */
594 break;
zhanghailiang31194732015-03-13 16:08:38 +0800595 case MIGRATION_STATUS_COMPLETED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300596 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900597 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200598 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200599 info->has_downtime = true;
600 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400601 info->has_setup_time = true;
602 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200603
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100604 populate_ram_info(info, s);
Juan Quintela17549e82011-10-05 13:50:43 +0200605 break;
zhanghailiang31194732015-03-13 16:08:38 +0800606 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300607 info->has_status = true;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100608 if (s->error) {
609 info->has_error_desc = true;
610 info->error_desc = g_strdup(error_get_pretty(s->error));
611 }
Juan Quintela17549e82011-10-05 13:50:43 +0200612 break;
zhanghailiang31194732015-03-13 16:08:38 +0800613 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300614 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200615 break;
aliguori5bb79102008-10-13 03:12:02 +0000616 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800617 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300618
619 return info;
aliguori5bb79102008-10-13 03:12:02 +0000620}
621
Peter Xu4a842142017-07-18 11:39:08 +0800622/**
623 * @migration_caps_check - check capability validity
624 *
625 * @cap_list: old capability list, array of bool
626 * @params: new capabilities to be applied soon
627 * @errp: set *errp if the check failed, with reason
628 *
629 * Returns true if check passed, otherwise false.
630 */
631static bool migrate_caps_check(bool *cap_list,
632 MigrationCapabilityStatusList *params,
633 Error **errp)
Orit Wasserman00458432012-08-06 21:42:48 +0300634{
Orit Wasserman00458432012-08-06 21:42:48 +0300635 MigrationCapabilityStatusList *cap;
Peter Xu4a842142017-07-18 11:39:08 +0800636 bool old_postcopy_cap;
Orit Wasserman00458432012-08-06 21:42:48 +0300637
Peter Xu4a842142017-07-18 11:39:08 +0800638 old_postcopy_cap = cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Orit Wasserman00458432012-08-06 21:42:48 +0300639
640 for (cap = params; cap; cap = cap->next) {
Peter Xu4a842142017-07-18 11:39:08 +0800641 cap_list[cap->value->capability] = cap->value->state;
Orit Wasserman00458432012-08-06 21:42:48 +0300642 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000643
Peter Xu4a842142017-07-18 11:39:08 +0800644#ifndef CONFIG_LIVE_BLOCK_MIGRATION
645 if (cap_list[MIGRATION_CAPABILITY_BLOCK]) {
646 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
647 "block migration");
648 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
649 return false;
650 }
651#endif
652
653 if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
654 if (cap_list[MIGRATION_CAPABILITY_COMPRESS]) {
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000655 /* The decompression threads asynchronously write into RAM
656 * rather than use the atomic copies needed to avoid
657 * userfaulting. It should be possible to fix the decompression
658 * threads for compatibility in future.
659 */
Peter Xu4a842142017-07-18 11:39:08 +0800660 error_setg(errp, "Postcopy is not currently compatible "
661 "with compression");
662 return false;
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000663 }
Peter Xu4a842142017-07-18 11:39:08 +0800664
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100665 /* This check is reasonably expensive, so only when it's being
666 * set the first time, also it's only the destination that needs
667 * special support.
668 */
669 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
670 !postcopy_ram_supported_by_host()) {
671 /* postcopy_ram_supported_by_host will have emitted a more
672 * detailed message
673 */
Peter Xu4a842142017-07-18 11:39:08 +0800674 error_setg(errp, "Postcopy is not supported");
675 return false;
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100676 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000677 }
Peter Xu4a842142017-07-18 11:39:08 +0800678
679 return true;
680}
681
682void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
683 Error **errp)
684{
685 MigrationState *s = migrate_get_current();
686 MigrationCapabilityStatusList *cap;
687
688 if (migration_is_setup_or_active(s->state)) {
689 error_setg(errp, QERR_MIGRATION_ACTIVE);
690 return;
691 }
692
693 if (!migrate_caps_check(s->enabled_capabilities, params, errp)) {
694 return;
695 }
696
697 for (cap = params; cap; cap = cap->next) {
698 s->enabled_capabilities[cap->value->capability] = cap->value->state;
699 }
Orit Wasserman00458432012-08-06 21:42:48 +0300700}
701
Peter Xu16d063b2017-07-18 11:39:04 +0800702/*
703 * Check whether the parameters are valid. Error will be put into errp
704 * (if provided). Return true if valid, otherwise false.
705 */
706static bool migrate_params_check(MigrationParameters *params, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +0800707{
Eric Blake7f375e02016-09-08 22:14:16 -0500708 if (params->has_compress_level &&
709 (params->compress_level < 0 || params->compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100710 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
711 "is invalid, it should be in the range of 0 to 9");
Peter Xu16d063b2017-07-18 11:39:04 +0800712 return false;
Liang Li85de8322015-03-23 16:32:28 +0800713 }
Peter Xu16d063b2017-07-18 11:39:04 +0800714
Eric Blake7f375e02016-09-08 22:14:16 -0500715 if (params->has_compress_threads &&
716 (params->compress_threads < 1 || params->compress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100717 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
718 "compress_threads",
719 "is invalid, it should be in the range of 1 to 255");
Peter Xu16d063b2017-07-18 11:39:04 +0800720 return false;
Liang Li85de8322015-03-23 16:32:28 +0800721 }
Peter Xu16d063b2017-07-18 11:39:04 +0800722
Eric Blake7f375e02016-09-08 22:14:16 -0500723 if (params->has_decompress_threads &&
724 (params->decompress_threads < 1 || params->decompress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100725 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
726 "decompress_threads",
727 "is invalid, it should be in the range of 1 to 255");
Peter Xu16d063b2017-07-18 11:39:04 +0800728 return false;
Liang Li85de8322015-03-23 16:32:28 +0800729 }
Peter Xu16d063b2017-07-18 11:39:04 +0800730
Eric Blake7f375e02016-09-08 22:14:16 -0500731 if (params->has_cpu_throttle_initial &&
732 (params->cpu_throttle_initial < 1 ||
733 params->cpu_throttle_initial > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400734 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400735 "cpu_throttle_initial",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400736 "an integer in the range of 1 to 99");
Peter Xu16d063b2017-07-18 11:39:04 +0800737 return false;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400738 }
Peter Xu16d063b2017-07-18 11:39:04 +0800739
Eric Blake7f375e02016-09-08 22:14:16 -0500740 if (params->has_cpu_throttle_increment &&
741 (params->cpu_throttle_increment < 1 ||
742 params->cpu_throttle_increment > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400743 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400744 "cpu_throttle_increment",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400745 "an integer in the range of 1 to 99");
Peter Xu16d063b2017-07-18 11:39:04 +0800746 return false;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400747 }
Peter Xu16d063b2017-07-18 11:39:04 +0800748
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530749 if (params->has_max_bandwidth &&
750 (params->max_bandwidth < 0 || params->max_bandwidth > SIZE_MAX)) {
751 error_setg(errp, "Parameter 'max_bandwidth' expects an integer in the"
752 " range of 0 to %zu bytes/second", SIZE_MAX);
Peter Xu16d063b2017-07-18 11:39:04 +0800753 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530754 }
Peter Xu16d063b2017-07-18 11:39:04 +0800755
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530756 if (params->has_downtime_limit &&
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -0300757 (params->downtime_limit < 0 ||
758 params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
759 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
760 "the range of 0 to %d milliseconds",
761 MAX_MIGRATE_DOWNTIME);
Peter Xu16d063b2017-07-18 11:39:04 +0800762 return false;
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530763 }
Peter Xu16d063b2017-07-18 11:39:04 +0800764
zhanghailiang68b53592016-10-27 14:43:01 +0800765 if (params->has_x_checkpoint_delay && (params->x_checkpoint_delay < 0)) {
766 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
767 "x_checkpoint_delay",
768 "is invalid, it should be positive");
Peter Xu16d063b2017-07-18 11:39:04 +0800769 return false;
770 }
Juan Quintela4075fb12016-01-15 08:56:17 +0100771 if (params->has_x_multifd_channels &&
772 (params->x_multifd_channels < 1 || params->x_multifd_channels > 255)) {
773 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
774 "multifd_channels",
775 "is invalid, it should be in the range of 1 to 255");
776 return false;
777 }
Juan Quintela0fb86602017-04-27 10:48:25 +0200778 if (params->has_x_multifd_page_count &&
779 (params->x_multifd_page_count < 1 ||
780 params->x_multifd_page_count > 10000)) {
781 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
782 "multifd_page_count",
783 "is invalid, it should be in the range of 1 to 10000");
784 return false;
785 }
Peter Xu16d063b2017-07-18 11:39:04 +0800786
787 return true;
788}
789
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200790static void migrate_params_test_apply(MigrateSetParameters *params,
791 MigrationParameters *dest)
792{
793 *dest = migrate_get_current()->parameters;
794
795 /* TODO use QAPI_CLONE() instead of duplicating it inline */
796
797 if (params->has_compress_level) {
798 dest->compress_level = params->compress_level;
799 }
800
801 if (params->has_compress_threads) {
802 dest->compress_threads = params->compress_threads;
803 }
804
805 if (params->has_decompress_threads) {
806 dest->decompress_threads = params->decompress_threads;
807 }
808
809 if (params->has_cpu_throttle_initial) {
810 dest->cpu_throttle_initial = params->cpu_throttle_initial;
811 }
812
813 if (params->has_cpu_throttle_increment) {
814 dest->cpu_throttle_increment = params->cpu_throttle_increment;
815 }
816
817 if (params->has_tls_creds) {
Markus Armbruster01fa5592017-07-18 14:42:04 +0200818 assert(params->tls_creds->type == QTYPE_QSTRING);
819 dest->tls_creds = g_strdup(params->tls_creds->u.s);
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200820 }
821
822 if (params->has_tls_hostname) {
Markus Armbruster01fa5592017-07-18 14:42:04 +0200823 assert(params->tls_hostname->type == QTYPE_QSTRING);
824 dest->tls_hostname = g_strdup(params->tls_hostname->u.s);
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200825 }
826
827 if (params->has_max_bandwidth) {
828 dest->max_bandwidth = params->max_bandwidth;
829 }
830
831 if (params->has_downtime_limit) {
832 dest->downtime_limit = params->downtime_limit;
833 }
834
835 if (params->has_x_checkpoint_delay) {
836 dest->x_checkpoint_delay = params->x_checkpoint_delay;
837 }
838
839 if (params->has_block_incremental) {
840 dest->block_incremental = params->block_incremental;
841 }
842}
843
844static void migrate_params_apply(MigrateSetParameters *params)
Peter Xu16d063b2017-07-18 11:39:04 +0800845{
846 MigrationState *s = migrate_get_current();
847
Markus Armbrustere87fae42017-07-18 12:57:38 +0200848 /* TODO use QAPI_CLONE() instead of duplicating it inline */
849
Eric Blake7f375e02016-09-08 22:14:16 -0500850 if (params->has_compress_level) {
851 s->parameters.compress_level = params->compress_level;
Liang Li85de8322015-03-23 16:32:28 +0800852 }
Peter Xu476c72a2017-07-18 11:39:05 +0800853
Eric Blake7f375e02016-09-08 22:14:16 -0500854 if (params->has_compress_threads) {
855 s->parameters.compress_threads = params->compress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800856 }
Peter Xu476c72a2017-07-18 11:39:05 +0800857
Eric Blake7f375e02016-09-08 22:14:16 -0500858 if (params->has_decompress_threads) {
859 s->parameters.decompress_threads = params->decompress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800860 }
Peter Xu476c72a2017-07-18 11:39:05 +0800861
Eric Blake7f375e02016-09-08 22:14:16 -0500862 if (params->has_cpu_throttle_initial) {
863 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400864 }
Peter Xu476c72a2017-07-18 11:39:05 +0800865
Eric Blake7f375e02016-09-08 22:14:16 -0500866 if (params->has_cpu_throttle_increment) {
867 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400868 }
Peter Xu476c72a2017-07-18 11:39:05 +0800869
Eric Blake7f375e02016-09-08 22:14:16 -0500870 if (params->has_tls_creds) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100871 g_free(s->parameters.tls_creds);
Markus Armbruster01fa5592017-07-18 14:42:04 +0200872 assert(params->tls_creds->type == QTYPE_QSTRING);
873 s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100874 }
Peter Xu476c72a2017-07-18 11:39:05 +0800875
Eric Blake7f375e02016-09-08 22:14:16 -0500876 if (params->has_tls_hostname) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100877 g_free(s->parameters.tls_hostname);
Markus Armbruster01fa5592017-07-18 14:42:04 +0200878 assert(params->tls_hostname->type == QTYPE_QSTRING);
879 s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100880 }
Peter Xu476c72a2017-07-18 11:39:05 +0800881
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530882 if (params->has_max_bandwidth) {
883 s->parameters.max_bandwidth = params->max_bandwidth;
884 if (s->to_dst_file) {
885 qemu_file_set_rate_limit(s->to_dst_file,
886 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
887 }
888 }
Peter Xu476c72a2017-07-18 11:39:05 +0800889
Ashijeet Acharya2ff30252016-09-15 21:50:28 +0530890 if (params->has_downtime_limit) {
891 s->parameters.downtime_limit = params->downtime_limit;
892 }
zhanghailiang68b53592016-10-27 14:43:01 +0800893
894 if (params->has_x_checkpoint_delay) {
895 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
zhanghailiang479125d2017-01-17 20:57:42 +0800896 if (migration_in_colo_state()) {
897 colo_checkpoint_notify(s);
898 }
zhanghailiang68b53592016-10-27 14:43:01 +0800899 }
Peter Xu476c72a2017-07-18 11:39:05 +0800900
Juan Quintela2833c592017-04-05 18:32:37 +0200901 if (params->has_block_incremental) {
902 s->parameters.block_incremental = params->block_incremental;
903 }
Juan Quintela4075fb12016-01-15 08:56:17 +0100904 if (params->has_x_multifd_channels) {
905 s->parameters.x_multifd_channels = params->x_multifd_channels;
906 }
Juan Quintela0fb86602017-04-27 10:48:25 +0200907 if (params->has_x_multifd_page_count) {
908 s->parameters.x_multifd_page_count = params->x_multifd_page_count;
909 }
Liang Li85de8322015-03-23 16:32:28 +0800910}
911
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200912void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
Peter Xu476c72a2017-07-18 11:39:05 +0800913{
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200914 MigrationParameters tmp;
915
Markus Armbruster01fa5592017-07-18 14:42:04 +0200916 /* TODO Rewrite "" to null instead */
917 if (params->has_tls_creds
918 && params->tls_creds->type == QTYPE_QNULL) {
919 QDECREF(params->tls_creds->u.n);
920 params->tls_creds->type = QTYPE_QSTRING;
921 params->tls_creds->u.s = strdup("");
922 }
923 /* TODO Rewrite "" to null instead */
924 if (params->has_tls_hostname
925 && params->tls_hostname->type == QTYPE_QNULL) {
926 QDECREF(params->tls_hostname->u.n);
927 params->tls_hostname->type = QTYPE_QSTRING;
928 params->tls_hostname->u.s = strdup("");
929 }
930
Markus Armbruster1bda8b32017-07-18 13:42:11 +0200931 migrate_params_test_apply(params, &tmp);
932
933 if (!migrate_params_check(&tmp, errp)) {
Peter Xu476c72a2017-07-18 11:39:05 +0800934 /* Invalid parameter */
935 return;
936 }
937
938 migrate_params_apply(params);
939}
940
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100941
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000942void qmp_migrate_start_postcopy(Error **errp)
943{
944 MigrationState *s = migrate_get_current();
945
946 if (!migrate_postcopy_ram()) {
Dr. David Alan Gilberta54d3402015-11-12 11:34:44 +0000947 error_setg(errp, "Enable postcopy with migrate_set_capability before"
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000948 " the start of migration");
949 return;
950 }
951
952 if (s->state == MIGRATION_STATUS_NONE) {
953 error_setg(errp, "Postcopy must be started after migration has been"
954 " started");
955 return;
956 }
957 /*
958 * we don't error if migration has finished since that would be racy
959 * with issuing this command.
960 */
961 atomic_set(&s->start_postcopy, true);
962}
963
aliguori065e2812008-11-11 16:46:33 +0000964/* shared migration helpers */
965
zhanghailiang48781e52015-12-16 11:47:33 +0000966void migrate_set_state(int *state, int old_state, int new_state)
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000967{
Peter Xua31fede2017-08-30 16:32:01 +0800968 assert(new_state < MIGRATION_STATUS__MAX);
zhanghailiang48781e52015-12-16 11:47:33 +0000969 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
Peter Xua31fede2017-08-30 16:32:01 +0800970 trace_migrate_set_state(MigrationStatus_str(new_state));
Juan Quintelab05dc722015-07-07 14:44:05 +0200971 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000972 }
973}
974
Peter Xu4e4a3d32017-07-18 11:39:09 +0800975static MigrationCapabilityStatusList *migrate_cap_add(
976 MigrationCapabilityStatusList *list,
977 MigrationCapability index,
978 bool state)
Juan Quintela2833c592017-04-05 18:32:37 +0200979{
980 MigrationCapabilityStatusList *cap;
981
982 cap = g_new0(MigrationCapabilityStatusList, 1);
983 cap->value = g_new0(MigrationCapabilityStatus, 1);
Peter Xu4e4a3d32017-07-18 11:39:09 +0800984 cap->value->capability = index;
985 cap->value->state = state;
986 cap->next = list;
987
988 return cap;
989}
990
991void migrate_set_block_enabled(bool value, Error **errp)
992{
993 MigrationCapabilityStatusList *cap;
994
995 cap = migrate_cap_add(NULL, MIGRATION_CAPABILITY_BLOCK, value);
Juan Quintela2833c592017-04-05 18:32:37 +0200996 qmp_migrate_set_capabilities(cap, errp);
997 qapi_free_MigrationCapabilityStatusList(cap);
998}
999
1000static void migrate_set_block_incremental(MigrationState *s, bool value)
1001{
1002 s->parameters.block_incremental = value;
1003}
1004
1005static void block_cleanup_parameters(MigrationState *s)
1006{
1007 if (s->must_remove_block_options) {
1008 /* setting to false can never fail */
1009 migrate_set_block_enabled(false, &error_abort);
1010 migrate_set_block_incremental(s, false);
1011 s->must_remove_block_options = false;
1012 }
1013}
1014
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001015static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +00001016{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001017 MigrationState *s = opaque;
1018
1019 qemu_bh_delete(s->cleanup_bh);
1020 s->cleanup_bh = NULL;
1021
zhanghailiang89a02a92016-01-15 11:37:42 +08001022 if (s->to_dst_file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001023 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001024 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001025 if (s->migration_thread_running) {
1026 qemu_thread_join(&s->thread);
1027 s->migration_thread_running = false;
1028 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +01001029 qemu_mutex_lock_iothread();
1030
zhanghailiang89a02a92016-01-15 11:37:42 +08001031 qemu_fclose(s->to_dst_file);
1032 s->to_dst_file = NULL;
aliguori065e2812008-11-11 16:46:33 +00001033 }
1034
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001035 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
1036 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001037
Liang Li94f5a432015-11-02 15:37:00 +08001038 if (s->state == MIGRATION_STATUS_CANCELLING) {
zhanghailiang48781e52015-12-16 11:47:33 +00001039 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
Liang Li94f5a432015-11-02 15:37:00 +08001040 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +01001041 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001042
1043 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +02001044 block_cleanup_parameters(s);
aliguori065e2812008-11-11 16:46:33 +00001045}
1046
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001047void migrate_fd_error(MigrationState *s, const Error *error)
aliguori065e2812008-11-11 16:46:33 +00001048{
Peter Maydell25174052016-10-21 18:41:45 +01001049 trace_migrate_fd_error(error_get_pretty(error));
zhanghailiang89a02a92016-01-15 11:37:42 +08001050 assert(s->to_dst_file == NULL);
zhanghailiang48781e52015-12-16 11:47:33 +00001051 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1052 MIGRATION_STATUS_FAILED);
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001053 if (!s->error) {
1054 s->error = error_copy(error);
1055 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001056 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela2833c592017-04-05 18:32:37 +02001057 block_cleanup_parameters(s);
Juan Quintela458cf282011-02-22 23:32:54 +01001058}
1059
Juan Quintela0edda1c2010-05-11 16:28:39 +02001060static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +00001061{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001062 int old_state ;
zhanghailiang89a02a92016-01-15 11:37:42 +08001063 QEMUFile *f = migrate_get_current()->to_dst_file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001064 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +00001065
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001066 if (s->rp_state.from_dst_file) {
1067 /* shutdown the rp socket, so causing the rp thread to shutdown */
1068 qemu_file_shutdown(s->rp_state.from_dst_file);
1069 }
1070
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001071 do {
1072 old_state = s->state;
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +00001073 if (!migration_is_setup_or_active(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +00001074 break;
1075 }
zhanghailiang48781e52015-12-16 11:47:33 +00001076 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
zhanghailiang31194732015-03-13 16:08:38 +08001077 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001078
1079 /*
1080 * If we're unlucky the migration code might be stuck somewhere in a
1081 * send/write while the network has failed and is waiting to timeout;
1082 * if we've got shutdown(2) available then we can force it to quit.
1083 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1084 * called in a bh, so there is no race against this cancel.
1085 */
zhanghailiang31194732015-03-13 16:08:38 +08001086 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +00001087 qemu_file_shutdown(f);
1088 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08001089 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
1090 Error *local_err = NULL;
1091
1092 bdrv_invalidate_cache_all(&local_err);
1093 if (local_err) {
1094 error_report_err(local_err);
1095 } else {
1096 s->block_inactive = false;
1097 }
1098 }
Juan Quintela2833c592017-04-05 18:32:37 +02001099 block_cleanup_parameters(s);
aliguori065e2812008-11-11 16:46:33 +00001100}
1101
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001102void add_migration_state_change_notifier(Notifier *notify)
1103{
1104 notifier_list_add(&migration_state_notifiers, notify);
1105}
1106
1107void remove_migration_state_change_notifier(Notifier *notify)
1108{
Paolo Bonzini31552522012-01-13 17:34:01 +01001109 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001110}
1111
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +02001112bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001113{
zhanghailiang31194732015-03-13 16:08:38 +08001114 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001115}
1116
Juan Quintela70736932011-02-23 00:43:59 +01001117bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001118{
zhanghailiang31194732015-03-13 16:08:38 +08001119 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +01001120}
Juan Quintela0edda1c2010-05-11 16:28:39 +02001121
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001122bool migration_has_failed(MigrationState *s)
1123{
zhanghailiang31194732015-03-13 16:08:38 +08001124 return (s->state == MIGRATION_STATUS_CANCELLED ||
1125 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +02001126}
1127
Juan Quintela57273092017-03-20 22:25:28 +01001128bool migration_in_postcopy(void)
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001129{
Juan Quintela57273092017-03-20 22:25:28 +01001130 MigrationState *s = migrate_get_current();
1131
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001132 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1133}
1134
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001135bool migration_in_postcopy_after_devices(MigrationState *s)
1136{
Juan Quintela57273092017-03-20 22:25:28 +01001137 return migration_in_postcopy() && s->postcopy_after_devices;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001138}
1139
Juan Quintelafab35002017-03-22 17:36:57 +01001140bool migration_is_idle(void)
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301141{
Juan Quintelafab35002017-03-22 17:36:57 +01001142 MigrationState *s = migrate_get_current();
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301143
1144 switch (s->state) {
1145 case MIGRATION_STATUS_NONE:
1146 case MIGRATION_STATUS_CANCELLED:
1147 case MIGRATION_STATUS_COMPLETED:
1148 case MIGRATION_STATUS_FAILED:
1149 return true;
1150 case MIGRATION_STATUS_SETUP:
1151 case MIGRATION_STATUS_CANCELLING:
1152 case MIGRATION_STATUS_ACTIVE:
1153 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1154 case MIGRATION_STATUS_COLO:
1155 return false;
1156 case MIGRATION_STATUS__MAX:
1157 g_assert_not_reached();
1158 }
1159
1160 return false;
1161}
1162
Juan Quintelaa0762d92017-04-05 21:00:09 +02001163MigrationState *migrate_init(void)
Juan Quintela0edda1c2010-05-11 16:28:39 +02001164{
Juan Quintela17549e82011-10-05 13:50:43 +02001165 MigrationState *s = migrate_get_current();
Orit Wassermanbbf6da32012-08-06 21:42:47 +03001166
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001167 /*
1168 * Reinitialise all migration state, except
1169 * parameters/capabilities that the user set, and
1170 * locks.
1171 */
1172 s->bytes_xfer = 0;
1173 s->xfer_limit = 0;
1174 s->cleanup_bh = 0;
zhanghailiang89a02a92016-01-15 11:37:42 +08001175 s->to_dst_file = NULL;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001176 s->state = MIGRATION_STATUS_NONE;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001177 s->rp_state.from_dst_file = NULL;
1178 s->rp_state.error = false;
1179 s->mbps = 0.0;
1180 s->downtime = 0;
1181 s->expected_downtime = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001182 s->setup_time = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001183 s->start_postcopy = false;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001184 s->postcopy_after_devices = false;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001185 s->migration_thread_running = false;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001186 error_free(s->error);
1187 s->error = NULL;
Juan Quintela1299c632011-11-09 21:29:01 +01001188
zhanghailiang48781e52015-12-16 11:47:33 +00001189 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +02001190
Alex Blighbc72ad62013-08-21 16:03:08 +01001191 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +02001192 return s;
1193}
Juan Quintelacab30142011-02-22 23:54:21 +01001194
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001195static GSList *migration_blockers;
1196
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301197int migrate_add_blocker(Error *reason, Error **errp)
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001198{
Peter Xu3df663e2017-06-27 12:10:15 +08001199 if (migrate_get_current()->only_migratable) {
Ashijeet Acharyab67b8c32017-01-16 17:01:54 +05301200 error_propagate(errp, error_copy(reason));
1201 error_prepend(errp, "disallowing migration blocker "
1202 "(--only_migratable) for: ");
1203 return -EACCES;
1204 }
1205
Juan Quintelafab35002017-03-22 17:36:57 +01001206 if (migration_is_idle()) {
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301207 migration_blockers = g_slist_prepend(migration_blockers, reason);
1208 return 0;
1209 }
1210
1211 error_propagate(errp, error_copy(reason));
1212 error_prepend(errp, "disallowing migration blocker (migration in "
1213 "progress) for: ");
1214 return -EBUSY;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001215}
1216
1217void migrate_del_blocker(Error *reason)
1218{
1219 migration_blockers = g_slist_remove(migration_blockers, reason);
1220}
1221
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001222void qmp_migrate_incoming(const char *uri, Error **errp)
1223{
1224 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001225 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001226
1227 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001228 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001229 return;
1230 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001231 if (!once) {
1232 error_setg(errp, "The incoming migration has already been started");
1233 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001234
1235 qemu_start_incoming_migration(uri, &local_err);
1236
1237 if (local_err) {
1238 error_propagate(errp, local_err);
1239 return;
1240 }
1241
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001242 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001243}
1244
Greg Kurz24f39022016-05-04 21:44:19 +02001245bool migration_is_blocked(Error **errp)
1246{
1247 if (qemu_savevm_state_blocked(errp)) {
1248 return true;
1249 }
1250
1251 if (migration_blockers) {
Eduardo Habkost250561e2017-06-08 10:39:05 -03001252 error_propagate(errp, error_copy(migration_blockers->data));
Greg Kurz24f39022016-05-04 21:44:19 +02001253 return true;
1254 }
1255
1256 return false;
1257}
1258
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001259void qmp_migrate(const char *uri, bool has_blk, bool blk,
1260 bool has_inc, bool inc, bool has_detach, bool detach,
1261 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001262{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +02001263 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +02001264 MigrationState *s = migrate_get_current();
Juan Quintelacab30142011-02-22 23:54:21 +01001265 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +01001266
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +00001267 if (migration_is_setup_or_active(s->state) ||
zhanghailiang0b827d52016-10-27 14:42:54 +08001268 s->state == MIGRATION_STATUS_CANCELLING ||
1269 s->state == MIGRATION_STATUS_COLO) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001270 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001271 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001272 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +01001273 if (runstate_check(RUN_STATE_INMIGRATE)) {
1274 error_setg(errp, "Guest is waiting for an incoming migration");
1275 return;
1276 }
1277
Greg Kurz24f39022016-05-04 21:44:19 +02001278 if (migration_is_blocked(errp)) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001279 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001280 }
1281
Juan Quintela2833c592017-04-05 18:32:37 +02001282 if ((has_blk && blk) || (has_inc && inc)) {
1283 if (migrate_use_block() || migrate_use_block_incremental()) {
1284 error_setg(errp, "Command options are incompatible with "
1285 "current migration capabilities");
1286 return;
1287 }
1288 migrate_set_block_enabled(true, &local_err);
1289 if (local_err) {
1290 error_propagate(errp, local_err);
1291 return;
1292 }
1293 s->must_remove_block_options = true;
1294 }
1295
1296 if (has_inc && inc) {
1297 migrate_set_block_incremental(s, true);
1298 }
1299
Juan Quintelaa0762d92017-04-05 21:00:09 +02001300 s = migrate_init();
Juan Quintelacab30142011-02-22 23:54:21 +01001301
1302 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001303 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04001304#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08001305 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04001306 rdma_start_outgoing_migration(s, p, &local_err);
1307#endif
Juan Quintelacab30142011-02-22 23:54:21 +01001308 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001309 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001310 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001311 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001312 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001313 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001314 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001315 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1316 "a valid migration protocol");
zhanghailiang48781e52015-12-16 11:47:33 +00001317 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1318 MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001319 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001320 }
1321
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001322 if (local_err) {
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001323 migrate_fd_error(s, local_err);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001324 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001325 return;
Juan Quintela1299c632011-11-09 21:29:01 +01001326 }
Juan Quintelacab30142011-02-22 23:54:21 +01001327}
1328
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02001329void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001330{
Juan Quintela17549e82011-10-05 13:50:43 +02001331 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01001332}
1333
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001334void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1335{
1336 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +02001337 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001338
1339 /* Check for truncation */
1340 if (value != (size_t)value) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001341 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1342 "exceeding address space");
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001343 return;
1344 }
1345
Orit Wassermana5615b12014-01-30 20:08:36 +02001346 /* Cache should not be larger than guest ram size */
1347 if (value > ram_bytes_total()) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001348 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1349 "exceeds guest ram size ");
Orit Wassermana5615b12014-01-30 20:08:36 +02001350 return;
1351 }
1352
Orit Wassermanc91e6812014-01-30 20:08:34 +02001353 new_size = xbzrle_cache_resize(value);
1354 if (new_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001355 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1356 "is smaller than page size");
Orit Wassermanc91e6812014-01-30 20:08:34 +02001357 return;
1358 }
1359
1360 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001361}
1362
1363int64_t qmp_query_migrate_cache_size(Error **errp)
1364{
1365 return migrate_xbzrle_cache_size();
1366}
1367
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001368void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001369{
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001370 MigrateSetParameters p = {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301371 .has_max_bandwidth = true,
1372 .max_bandwidth = value,
1373 };
Juan Quintelacab30142011-02-22 23:54:21 +01001374
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301375 qmp_migrate_set_parameters(&p, errp);
Juan Quintelacab30142011-02-22 23:54:21 +01001376}
1377
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001378void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001379{
Daniel Henrique Barboza87c9cc12017-02-22 12:17:29 -03001380 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
1381 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
1382 "the range of 0 to %d seconds",
1383 MAX_MIGRATE_DOWNTIME_SECONDS);
1384 return;
1385 }
1386
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301387 value *= 1000; /* Convert to milliseconds */
1388 value = MAX(0, MIN(INT64_MAX, value));
1389
Markus Armbruster1bda8b32017-07-18 13:42:11 +02001390 MigrateSetParameters p = {
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05301391 .has_downtime_limit = true,
1392 .downtime_limit = value,
1393 };
1394
1395 qmp_migrate_set_parameters(&p, errp);
aliguori5bb79102008-10-13 03:12:02 +00001396}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001397
Pavel Butsykin53f09a12017-02-03 18:23:20 +03001398bool migrate_release_ram(void)
1399{
1400 MigrationState *s;
1401
1402 s = migrate_get_current();
1403
1404 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
1405}
1406
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001407bool migrate_postcopy_ram(void)
1408{
1409 MigrationState *s;
1410
1411 s = migrate_get_current();
1412
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +00001413 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001414}
1415
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06001416bool migrate_auto_converge(void)
1417{
1418 MigrationState *s;
1419
1420 s = migrate_get_current();
1421
1422 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1423}
1424
Peter Lieven323004a2013-07-18 09:48:50 +02001425bool migrate_zero_blocks(void)
1426{
1427 MigrationState *s;
1428
1429 s = migrate_get_current();
1430
1431 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1432}
1433
Liang Li8706d2d2015-03-23 16:32:17 +08001434bool migrate_use_compression(void)
1435{
Liang Lidde4e692015-03-23 16:32:26 +08001436 MigrationState *s;
1437
1438 s = migrate_get_current();
1439
1440 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08001441}
1442
1443int migrate_compress_level(void)
1444{
1445 MigrationState *s;
1446
1447 s = migrate_get_current();
1448
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001449 return s->parameters.compress_level;
Liang Li8706d2d2015-03-23 16:32:17 +08001450}
1451
1452int migrate_compress_threads(void)
1453{
1454 MigrationState *s;
1455
1456 s = migrate_get_current();
1457
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001458 return s->parameters.compress_threads;
Liang Li8706d2d2015-03-23 16:32:17 +08001459}
1460
Liang Li3fcb38c2015-03-23 16:32:18 +08001461int migrate_decompress_threads(void)
1462{
1463 MigrationState *s;
1464
1465 s = migrate_get_current();
1466
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001467 return s->parameters.decompress_threads;
Liang Li3fcb38c2015-03-23 16:32:18 +08001468}
1469
Juan Quintelab05dc722015-07-07 14:44:05 +02001470bool migrate_use_events(void)
1471{
1472 MigrationState *s;
1473
1474 s = migrate_get_current();
1475
1476 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1477}
1478
Juan Quintela30126bb2016-01-14 12:23:00 +01001479bool migrate_use_multifd(void)
1480{
1481 MigrationState *s;
1482
1483 s = migrate_get_current();
1484
1485 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_MULTIFD];
1486}
1487
Juan Quintela4075fb12016-01-15 08:56:17 +01001488int migrate_multifd_channels(void)
1489{
1490 MigrationState *s;
1491
1492 s = migrate_get_current();
1493
1494 return s->parameters.x_multifd_channels;
1495}
1496
Juan Quintela0fb86602017-04-27 10:48:25 +02001497int migrate_multifd_page_count(void)
1498{
1499 MigrationState *s;
1500
1501 s = migrate_get_current();
1502
1503 return s->parameters.x_multifd_page_count;
1504}
1505
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001506int migrate_use_xbzrle(void)
1507{
1508 MigrationState *s;
1509
1510 s = migrate_get_current();
1511
1512 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1513}
1514
1515int64_t migrate_xbzrle_cache_size(void)
1516{
1517 MigrationState *s;
1518
1519 s = migrate_get_current();
1520
1521 return s->xbzrle_cache_size;
1522}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001523
Juan Quintela2833c592017-04-05 18:32:37 +02001524bool migrate_use_block(void)
1525{
1526 MigrationState *s;
1527
1528 s = migrate_get_current();
1529
1530 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
1531}
1532
Peter Xuc788ada2017-06-26 18:28:55 +08001533bool migrate_use_return_path(void)
1534{
1535 MigrationState *s;
1536
1537 s = migrate_get_current();
1538
1539 return s->enabled_capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
1540}
1541
Juan Quintela2833c592017-04-05 18:32:37 +02001542bool migrate_use_block_incremental(void)
1543{
1544 MigrationState *s;
1545
1546 s = migrate_get_current();
1547
1548 return s->parameters.block_incremental;
1549}
1550
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001551/* migration thread support */
1552/*
1553 * Something bad happened to the RP stream, mark an error
1554 * The caller shall print or trace something to indicate why
1555 */
1556static void mark_source_rp_bad(MigrationState *s)
1557{
1558 s->rp_state.error = true;
1559}
1560
1561static struct rp_cmd_args {
1562 ssize_t len; /* -1 = variable */
1563 const char *name;
1564} rp_cmd_args[] = {
1565 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1566 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1567 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001568 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1569 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001570 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1571};
1572
1573/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001574 * Process a request for pages received on the return path,
1575 * We're allowed to send more than requested (e.g. to round to our page size)
1576 * and we don't need to send pages that have already been sent.
1577 */
1578static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1579 ram_addr_t start, size_t len)
1580{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001581 long our_host_ps = getpagesize();
1582
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001583 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001584
1585 /*
1586 * Since we currently insist on matching page sizes, just sanity check
1587 * we're being asked for whole host pages.
1588 */
1589 if (start & (our_host_ps-1) ||
1590 (len & (our_host_ps-1))) {
1591 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1592 " len: %zd", __func__, start, len);
1593 mark_source_rp_bad(ms);
1594 return;
1595 }
1596
Juan Quintela96506892017-03-14 18:41:03 +01001597 if (ram_save_queue_pages(rbname, start, len)) {
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001598 mark_source_rp_bad(ms);
1599 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001600}
1601
1602/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001603 * Handles messages sent on the return path towards the source VM
1604 *
1605 */
1606static void *source_return_path_thread(void *opaque)
1607{
1608 MigrationState *ms = opaque;
1609 QEMUFile *rp = ms->rp_state.from_dst_file;
1610 uint16_t header_len, header_type;
Peter Xu568b01c2016-03-09 14:12:12 +08001611 uint8_t buf[512];
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001612 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001613 ram_addr_t start = 0; /* =0 to silence warning */
1614 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001615 int res;
1616
1617 trace_source_return_path_thread_entry();
1618 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1619 migration_is_setup_or_active(ms->state)) {
1620 trace_source_return_path_thread_loop_top();
1621 header_type = qemu_get_be16(rp);
1622 header_len = qemu_get_be16(rp);
1623
1624 if (header_type >= MIG_RP_MSG_MAX ||
1625 header_type == MIG_RP_MSG_INVALID) {
1626 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1627 header_type, header_len);
1628 mark_source_rp_bad(ms);
1629 goto out;
1630 }
1631
1632 if ((rp_cmd_args[header_type].len != -1 &&
1633 header_len != rp_cmd_args[header_type].len) ||
Peter Xu568b01c2016-03-09 14:12:12 +08001634 header_len > sizeof(buf)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001635 error_report("RP: Received '%s' message (0x%04x) with"
1636 "incorrect length %d expecting %zu",
1637 rp_cmd_args[header_type].name, header_type, header_len,
1638 (size_t)rp_cmd_args[header_type].len);
1639 mark_source_rp_bad(ms);
1640 goto out;
1641 }
1642
1643 /* We know we've got a valid header by this point */
1644 res = qemu_get_buffer(rp, buf, header_len);
1645 if (res != header_len) {
1646 error_report("RP: Failed reading data for message 0x%04x"
1647 " read %d expected %d",
1648 header_type, res, header_len);
1649 mark_source_rp_bad(ms);
1650 goto out;
1651 }
1652
1653 /* OK, we have the message and the data */
1654 switch (header_type) {
1655 case MIG_RP_MSG_SHUT:
Peter Maydell4d885132016-06-10 17:09:22 +01001656 sibling_error = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001657 trace_source_return_path_thread_shut(sibling_error);
1658 if (sibling_error) {
1659 error_report("RP: Sibling indicated error %d", sibling_error);
1660 mark_source_rp_bad(ms);
1661 }
1662 /*
1663 * We'll let the main thread deal with closing the RP
1664 * we could do a shutdown(2) on it, but we're the only user
1665 * anyway, so there's nothing gained.
1666 */
1667 goto out;
1668
1669 case MIG_RP_MSG_PONG:
Peter Maydell4d885132016-06-10 17:09:22 +01001670 tmp32 = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001671 trace_source_return_path_thread_pong(tmp32);
1672 break;
1673
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001674 case MIG_RP_MSG_REQ_PAGES:
Peter Maydell4d885132016-06-10 17:09:22 +01001675 start = ldq_be_p(buf);
1676 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001677 migrate_handle_rp_req_pages(ms, NULL, start, len);
1678 break;
1679
1680 case MIG_RP_MSG_REQ_PAGES_ID:
1681 expected_len = 12 + 1; /* header + termination */
1682
1683 if (header_len >= expected_len) {
Peter Maydell4d885132016-06-10 17:09:22 +01001684 start = ldq_be_p(buf);
1685 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001686 /* Now we expect an idstr */
1687 tmp32 = buf[12]; /* Length of the following idstr */
1688 buf[13 + tmp32] = '\0';
1689 expected_len += tmp32;
1690 }
1691 if (header_len != expected_len) {
1692 error_report("RP: Req_Page_id with length %d expecting %zd",
1693 header_len, expected_len);
1694 mark_source_rp_bad(ms);
1695 goto out;
1696 }
1697 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
1698 break;
1699
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001700 default:
1701 break;
1702 }
1703 }
Dr. David Alan Gilbert5df54162015-11-18 11:48:41 +00001704 if (qemu_file_get_error(rp)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001705 trace_source_return_path_thread_bad_end();
1706 mark_source_rp_bad(ms);
1707 }
1708
1709 trace_source_return_path_thread_end();
1710out:
1711 ms->rp_state.from_dst_file = NULL;
1712 qemu_fclose(rp);
1713 return NULL;
1714}
1715
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001716static int open_return_path_on_source(MigrationState *ms)
1717{
1718
zhanghailiang89a02a92016-01-15 11:37:42 +08001719 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001720 if (!ms->rp_state.from_dst_file) {
1721 return -1;
1722 }
1723
1724 trace_open_return_path_on_source();
1725 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1726 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1727
1728 trace_open_return_path_on_source_continue();
1729
1730 return 0;
1731}
1732
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001733/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1734static int await_return_path_close_on_source(MigrationState *ms)
1735{
1736 /*
1737 * If this is a normal exit then the destination will send a SHUT and the
1738 * rp_thread will exit, however if there's an error we need to cause
1739 * it to exit.
1740 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001741 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001742 /*
1743 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1744 * waiting for the destination.
1745 */
1746 qemu_file_shutdown(ms->rp_state.from_dst_file);
1747 mark_source_rp_bad(ms);
1748 }
1749 trace_await_return_path_close_on_source_joining();
1750 qemu_thread_join(&ms->rp_state.rp_thread);
1751 trace_await_return_path_close_on_source_close();
1752 return ms->rp_state.error;
1753}
1754
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001755/*
1756 * Switch from normal iteration to postcopy
1757 * Returns non-0 on error
1758 */
1759static int postcopy_start(MigrationState *ms, bool *old_vm_running)
1760{
1761 int ret;
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001762 QIOChannelBuffer *bioc;
1763 QEMUFile *fb;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001764 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001765 bool restart_block = false;
zhanghailiang48781e52015-12-16 11:47:33 +00001766 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001767 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1768
1769 trace_postcopy_start();
1770 qemu_mutex_lock_iothread();
1771 trace_postcopy_start_set_run();
1772
1773 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1774 *old_vm_running = runstate_is_running();
1775 global_state_store();
1776 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001777 if (ret < 0) {
1778 goto fail;
1779 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001780
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001781 ret = bdrv_inactivate_all();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001782 if (ret < 0) {
1783 goto fail;
1784 }
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001785 restart_block = true;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001786
1787 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001788 * Cause any non-postcopiable, but iterative devices to
1789 * send out their final data.
1790 */
Fam Zhenga1fbe752017-06-17 00:06:58 +08001791 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001792
1793 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001794 * in Finish migrate and with the io-lock held everything should
1795 * be quiet, but we've potentially still got dirty pages and we
1796 * need to tell the destination to throw any pages it's already received
1797 * that are dirty
1798 */
1799 if (ram_postcopy_send_discard_bitmap(ms)) {
1800 error_report("postcopy send discard bitmap failed");
1801 goto fail;
1802 }
1803
1804 /*
1805 * send rest of state - note things that are doing postcopy
1806 * will notice we're in POSTCOPY_ACTIVE and not actually
1807 * wrap their state up here
1808 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001809 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001810 /* Ping just for debugging, helps line traces up */
zhanghailiang89a02a92016-01-15 11:37:42 +08001811 qemu_savevm_send_ping(ms->to_dst_file, 2);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001812
1813 /*
1814 * While loading the device state we may trigger page transfer
1815 * requests and the fd must be free to process those, and thus
1816 * the destination must read the whole device state off the fd before
1817 * it starts processing it. Unfortunately the ad-hoc migration format
1818 * doesn't allow the destination to know the size to read without fully
1819 * parsing it through each devices load-state code (especially the open
1820 * coded devices that use get/put).
1821 * So we wrap the device state up in a package with a length at the start;
1822 * to do this we use a qemu_buf to hold the whole of the device state.
1823 */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001824 bioc = qio_channel_buffer_new(4096);
Daniel P. Berrange6f01f132016-09-30 11:57:14 +01001825 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001826 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
1827 object_unref(OBJECT(bioc));
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001828
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001829 /*
1830 * Make sure the receiver can get incoming pages before we send the rest
1831 * of the state
1832 */
1833 qemu_savevm_send_postcopy_listen(fb);
1834
Fam Zhenga1fbe752017-06-17 00:06:58 +08001835 qemu_savevm_state_complete_precopy(fb, false, false);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001836 qemu_savevm_send_ping(fb, 3);
1837
1838 qemu_savevm_send_postcopy_run(fb);
1839
1840 /* <><> end of stuff going into the package */
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001841
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001842 /* Last point of recovery; as soon as we send the package the destination
1843 * can open devices and potentially start running.
1844 * Lets just check again we've not got any errors.
1845 */
1846 ret = qemu_file_get_error(ms->to_dst_file);
1847 if (ret) {
1848 error_report("postcopy_start: Migration stream errored (pre package)");
1849 goto fail_closefb;
1850 }
1851
1852 restart_block = false;
1853
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001854 /* Now send that blob */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001855 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001856 goto fail_closefb;
1857 }
1858 qemu_fclose(fb);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001859
1860 /* Send a notify to give a chance for anything that needs to happen
1861 * at the transition to postcopy and after the device state; in particular
1862 * spice needs to trigger a transition now
1863 */
1864 ms->postcopy_after_devices = true;
1865 notifier_list_notify(&migration_state_notifiers, ms);
1866
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001867 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
1868
1869 qemu_mutex_unlock_iothread();
1870
1871 /*
1872 * Although this ping is just for debug, it could potentially be
1873 * used for getting a better measurement of downtime at the source.
1874 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001875 qemu_savevm_send_ping(ms->to_dst_file, 4);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001876
Pavel Butsykinced1c612017-02-03 18:23:21 +03001877 if (migrate_release_ram()) {
1878 ram_postcopy_migrated_memory_release(ms);
1879 }
1880
zhanghailiang89a02a92016-01-15 11:37:42 +08001881 ret = qemu_file_get_error(ms->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001882 if (ret) {
1883 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00001884 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001885 MIGRATION_STATUS_FAILED);
1886 }
1887
1888 return ret;
1889
1890fail_closefb:
1891 qemu_fclose(fb);
1892fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001893 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001894 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbertef8d6482017-02-02 15:59:09 +00001895 if (restart_block) {
1896 /* A failure happened early enough that we know the destination hasn't
1897 * accessed block devices, so we're safe to recover.
1898 */
1899 Error *local_err = NULL;
1900
1901 bdrv_invalidate_cache_all(&local_err);
1902 if (local_err) {
1903 error_report_err(local_err);
1904 }
1905 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001906 qemu_mutex_unlock_iothread();
1907 return -1;
1908}
1909
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001910/**
1911 * migration_completion: Used by migration_thread when there's not much left.
1912 * The caller 'breaks' the loop when this returns.
1913 *
1914 * @s: Current migration state
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001915 * @current_active_state: The migration state we expect to be in
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001916 * @*old_vm_running: Pointer to old_vm_running flag
1917 * @*start_time: Pointer to time to update
1918 */
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001919static void migration_completion(MigrationState *s, int current_active_state,
1920 bool *old_vm_running,
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001921 int64_t *start_time)
1922{
1923 int ret;
1924
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001925 if (s->state == MIGRATION_STATUS_ACTIVE) {
1926 qemu_mutex_lock_iothread();
1927 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1928 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1929 *old_vm_running = runstate_is_running();
1930 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001931
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001932 if (!ret) {
Fam Zhenga1fbe752017-06-17 00:06:58 +08001933 bool inactivate = !migrate_colo_enabled();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001934 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02001935 if (ret >= 0) {
1936 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
Fam Zhenga1fbe752017-06-17 00:06:58 +08001937 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
1938 inactivate);
Kevin Wolff07fa4c2017-05-22 17:10:38 +02001939 }
Fam Zhenga1fbe752017-06-17 00:06:58 +08001940 if (inactivate && ret >= 0) {
1941 s->block_inactive = true;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001942 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001943 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001944 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001945
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001946 if (ret < 0) {
1947 goto fail;
1948 }
1949 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1950 trace_migration_completion_postcopy_end();
1951
zhanghailiang89a02a92016-01-15 11:37:42 +08001952 qemu_savevm_state_complete_postcopy(s->to_dst_file);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001953 trace_migration_completion_postcopy_end_after_complete();
1954 }
1955
1956 /*
1957 * If rp was opened we must clean up the thread before
1958 * cleaning everything else up (since if there are no failures
1959 * it will wait for the destination to send it's status in
1960 * a SHUT command).
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001961 */
Peter Xu0425dc92017-05-31 18:35:34 +08001962 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001963 int rp_error;
Peter Xu0425dc92017-05-31 18:35:34 +08001964 trace_migration_return_path_end_before();
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001965 rp_error = await_return_path_close_on_source(s);
Peter Xu0425dc92017-05-31 18:35:34 +08001966 trace_migration_return_path_end_after(rp_error);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001967 if (rp_error) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02001968 goto fail_invalidate;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001969 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001970 }
1971
zhanghailiang89a02a92016-01-15 11:37:42 +08001972 if (qemu_file_get_error(s->to_dst_file)) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001973 trace_migration_completion_file_err();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001974 goto fail_invalidate;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001975 }
1976
zhanghailiang0b827d52016-10-27 14:42:54 +08001977 if (!migrate_colo_enabled()) {
1978 migrate_set_state(&s->state, current_active_state,
1979 MIGRATION_STATUS_COMPLETED);
1980 }
1981
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001982 return;
1983
Greg Kurzfe904ea2016-05-18 15:44:36 +02001984fail_invalidate:
1985 /* If not doing postcopy, vm_start() will be called: let's regain
1986 * control on images.
1987 */
1988 if (s->state == MIGRATION_STATUS_ACTIVE) {
1989 Error *local_err = NULL;
1990
zhanghailiang1d2acc32017-01-24 15:59:52 +08001991 qemu_mutex_lock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001992 bdrv_invalidate_cache_all(&local_err);
1993 if (local_err) {
1994 error_report_err(local_err);
zhanghailiang1d2acc32017-01-24 15:59:52 +08001995 } else {
1996 s->block_inactive = false;
Greg Kurzfe904ea2016-05-18 15:44:36 +02001997 }
zhanghailiang1d2acc32017-01-24 15:59:52 +08001998 qemu_mutex_unlock_iothread();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001999 }
2000
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002001fail:
zhanghailiang48781e52015-12-16 11:47:33 +00002002 migrate_set_state(&s->state, current_active_state,
2003 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002004}
2005
zhanghailiang35a6ed42016-10-27 14:42:52 +08002006bool migrate_colo_enabled(void)
2007{
2008 MigrationState *s = migrate_get_current();
2009 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
2010}
2011
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00002012/*
2013 * Master migration thread on the source VM.
2014 * It drives the migration and pumps the data down the outgoing channel.
2015 */
Juan Quintela5f496a12013-02-22 17:36:30 +01002016static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002017{
Juan Quintela9848a402012-12-19 09:55:50 +01002018 MigrationState *s = opaque;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002019 /* Used by the bandwidth calcs, updated later */
Alex Blighbc72ad62013-08-21 16:03:08 +01002020 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2021 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01002022 int64_t initial_bytes = 0;
Peter Xufaec0662017-04-01 16:18:43 +08002023 /*
2024 * The final stage happens when the remaining data is smaller than
2025 * this threshold; it's calculated from the requested downtime and
2026 * measured bandwidth
2027 */
2028 int64_t threshold_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002029 int64_t start_time = initial_time;
Liang Li94f5a432015-11-02 15:37:00 +08002030 int64_t end_time;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002031 bool old_vm_running = false;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002032 bool entered_postcopy = false;
2033 /* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */
2034 enum MigrationStatus current_active_state = MIGRATION_STATUS_ACTIVE;
zhanghailiang0b827d52016-10-27 14:42:54 +08002035 bool enable_colo = migrate_colo_enabled();
Juan Quintela76f59332012-10-03 20:16:24 +02002036
Paolo Bonziniab28bd22015-07-09 08:55:38 +02002037 rcu_register_thread();
2038
zhanghailiang89a02a92016-01-15 11:37:42 +08002039 qemu_savevm_state_header(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002040
Peter Xu62a02652017-06-14 15:55:58 +08002041 /*
2042 * If we opened the return path, we need to make sure dst has it
2043 * opened as well.
2044 */
2045 if (s->rp_state.from_dst_file) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002046 /* Now tell the dest that it should open its end so it can reply */
zhanghailiang89a02a92016-01-15 11:37:42 +08002047 qemu_savevm_send_open_return_path(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002048
2049 /* And do a ping that will make stuff easier to debug */
zhanghailiang89a02a92016-01-15 11:37:42 +08002050 qemu_savevm_send_ping(s->to_dst_file, 1);
Peter Xu0425dc92017-05-31 18:35:34 +08002051 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002052
Peter Xu0425dc92017-05-31 18:35:34 +08002053 if (migrate_postcopy_ram()) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002054 /*
2055 * Tell the destination that we *might* want to do postcopy later;
2056 * if the other end can't do postcopy it should fail now, nice and
2057 * early.
2058 */
zhanghailiang89a02a92016-01-15 11:37:42 +08002059 qemu_savevm_send_postcopy_advise(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002060 }
2061
Juan Quintela9907e842017-06-28 11:52:24 +02002062 qemu_savevm_state_setup(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002063
Alex Blighbc72ad62013-08-21 16:03:08 +01002064 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang48781e52015-12-16 11:47:33 +00002065 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2066 MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04002067
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00002068 trace_migration_thread_setup_complete();
2069
2070 while (s->state == MIGRATION_STATUS_ACTIVE ||
2071 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +01002072 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +02002073 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002074
zhanghailiang89a02a92016-01-15 11:37:42 +08002075 if (!qemu_file_rate_limit(s->to_dst_file)) {
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00002076 uint64_t pend_post, pend_nonpost;
2077
Peter Xufaec0662017-04-01 16:18:43 +08002078 qemu_savevm_state_pending(s->to_dst_file, threshold_size,
2079 &pend_nonpost, &pend_post);
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00002080 pending_size = pend_nonpost + pend_post;
Peter Xufaec0662017-04-01 16:18:43 +08002081 trace_migrate_pending(pending_size, threshold_size,
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00002082 pend_post, pend_nonpost);
Peter Xufaec0662017-04-01 16:18:43 +08002083 if (pending_size && pending_size >= threshold_size) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002084 /* Still a significant amount to transfer */
2085
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002086 if (migrate_postcopy_ram() &&
2087 s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE &&
Peter Xufaec0662017-04-01 16:18:43 +08002088 pend_nonpost <= threshold_size &&
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002089 atomic_read(&s->start_postcopy)) {
2090
2091 if (!postcopy_start(s, &old_vm_running)) {
2092 current_active_state = MIGRATION_STATUS_POSTCOPY_ACTIVE;
2093 entered_postcopy = true;
2094 }
2095
2096 continue;
2097 }
2098 /* Just another iteration step */
zhanghailiang89a02a92016-01-15 11:37:42 +08002099 qemu_savevm_state_iterate(s->to_dst_file, entered_postcopy);
Juan Quintelac369f402012-10-03 20:33:34 +02002100 } else {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002101 trace_migration_thread_low_pending(pending_size);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002102 migration_completion(s, current_active_state,
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00002103 &old_vm_running, &start_time);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01002104 break;
Juan Quintelac369f402012-10-03 20:33:34 +02002105 }
2106 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01002107
zhanghailiang89a02a92016-01-15 11:37:42 +08002108 if (qemu_file_get_error(s->to_dst_file)) {
zhanghailiang48781e52015-12-16 11:47:33 +00002109 migrate_set_state(&s->state, current_active_state,
2110 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002111 trace_migration_thread_file_err();
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01002112 break;
2113 }
Alex Blighbc72ad62013-08-21 16:03:08 +01002114 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002115 if (current_time >= initial_time + BUFFER_DELAY) {
zhanghailiang89a02a92016-01-15 11:37:42 +08002116 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file) -
2117 initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -05002118 uint64_t time_spent = current_time - initial_time;
Paolo Bonzinia694ee32015-01-26 12:12:27 +01002119 double bandwidth = (double)transferred_bytes / time_spent;
Peter Xufaec0662017-04-01 16:18:43 +08002120 threshold_size = bandwidth * s->parameters.downtime_limit;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002121
Wei Yang5b648de2016-01-24 14:09:57 +00002122 s->mbps = (((double) transferred_bytes * 8.0) /
2123 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
Michael R. Hines7e114f82013-06-25 21:35:30 -04002124
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11002125 trace_migrate_transferred(transferred_bytes, time_spent,
Peter Xufaec0662017-04-01 16:18:43 +08002126 bandwidth, threshold_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +01002127 /* if we haven't sent anything, we don't want to recalculate
2128 10000 is a small enough number for our purposes */
Juan Quintela93604472017-06-06 19:49:03 +02002129 if (ram_counters.dirty_pages_rate && transferred_bytes > 10000) {
2130 s->expected_downtime = ram_counters.dirty_pages_rate *
Juan Quintela20afaed2017-03-21 09:09:14 +01002131 qemu_target_page_size() / bandwidth;
Juan Quintela90f8ae72013-02-01 13:22:37 +01002132 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002133
zhanghailiang89a02a92016-01-15 11:37:42 +08002134 qemu_file_reset_rate_limit(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002135 initial_time = current_time;
zhanghailiang89a02a92016-01-15 11:37:42 +08002136 initial_bytes = qemu_ftell(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002137 }
zhanghailiang89a02a92016-01-15 11:37:42 +08002138 if (qemu_file_rate_limit(s->to_dst_file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002139 /* usleep expects microseconds */
2140 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
2141 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002142 }
2143
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002144 trace_migration_thread_after_loop();
Jason J. Herne070afca2015-09-08 13:12:35 -04002145 /* If we enabled cpu throttling for auto-converge, turn it off. */
2146 cpu_throttle_stop();
Liang Li94f5a432015-11-02 15:37:00 +08002147 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jason J. Herne070afca2015-09-08 13:12:35 -04002148
Paolo Bonzinif4410a52013-02-22 17:36:20 +01002149 qemu_mutex_lock_iothread();
zhanghailiang0b827d52016-10-27 14:42:54 +08002150 /*
2151 * The resource has been allocated by migration will be reused in COLO
2152 * process, so don't release them.
2153 */
2154 if (!enable_colo) {
2155 qemu_savevm_state_cleanup();
2156 }
zhanghailiang31194732015-03-13 16:08:38 +08002157 if (s->state == MIGRATION_STATUS_COMPLETED) {
zhanghailiang89a02a92016-01-15 11:37:42 +08002158 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002159 s->total_time = end_time - s->total_time;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002160 if (!entered_postcopy) {
2161 s->downtime = end_time - start_time;
2162 }
Peter Lievend6ed7312014-05-12 10:46:00 +02002163 if (s->total_time) {
2164 s->mbps = (((double) transferred_bytes * 8.0) /
2165 ((double) s->total_time)) / 1000;
2166 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002167 runstate_set(RUN_STATE_POSTMIGRATE);
2168 } else {
zhanghailiang0b827d52016-10-27 14:42:54 +08002169 if (s->state == MIGRATION_STATUS_ACTIVE && enable_colo) {
2170 migrate_start_colo_process(s);
2171 qemu_savevm_state_cleanup();
2172 /*
2173 * Fixme: we will run VM in COLO no matter its old running state.
2174 * After exited COLO, we will keep running.
2175 */
2176 old_vm_running = true;
2177 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002178 if (old_vm_running && !entered_postcopy) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01002179 vm_start();
Dr. David Alan Gilbert42da5552016-07-15 17:44:46 +01002180 } else {
2181 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
2182 runstate_set(RUN_STATE_POSTMIGRATE);
2183 }
Paolo Bonzinidba433c2013-02-22 17:36:17 +01002184 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002185 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01002186 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01002187 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +01002188
Paolo Bonziniab28bd22015-07-09 08:55:38 +02002189 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002190 return NULL;
2191}
2192
Juan Quintela9848a402012-12-19 09:55:50 +01002193void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002194{
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302195 s->expected_downtime = s->parameters.downtime_limit;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01002196 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002197
Daniel P. Berrange9e4d2b92016-04-27 11:04:57 +01002198 qemu_file_set_blocking(s->to_dst_file, true);
zhanghailiang89a02a92016-01-15 11:37:42 +08002199 qemu_file_set_rate_limit(s->to_dst_file,
Ashijeet Acharya2ff30252016-09-15 21:50:28 +05302200 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
Paolo Bonzini442773c2013-02-22 17:36:44 +01002201
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02002202 /* Notify before starting migration thread */
2203 notifier_list_notify(&migration_state_notifiers, s);
2204
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002205 /*
Peter Xuc788ada2017-06-26 18:28:55 +08002206 * Open the return path. For postcopy, it is used exclusively. For
2207 * precopy, only if user specified "return-path" capability would
2208 * QEMU uses the return path.
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002209 */
Peter Xuc788ada2017-06-26 18:28:55 +08002210 if (migrate_postcopy_ram() || migrate_use_return_path()) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002211 if (open_return_path_on_source(s)) {
2212 error_report("Unable to open return-path for postcopy");
zhanghailiang48781e52015-12-16 11:47:33 +00002213 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002214 MIGRATION_STATUS_FAILED);
2215 migrate_fd_cleanup(s);
2216 return;
2217 }
2218 }
2219
Pankaj Gupta009fad72017-01-23 19:12:56 +05302220 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01002221 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00002222 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02002223}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00002224
Peter Xu9d18af92017-06-27 12:10:19 +08002225void migration_global_dump(Monitor *mon)
2226{
2227 MigrationState *ms = migrate_get_current();
2228
2229 monitor_printf(mon, "globals: store-global-state=%d, only_migratable=%d, "
2230 "send-configuration=%d, send-section-footer=%d\n",
2231 ms->store_global_state, ms->only_migratable,
2232 ms->send_configuration, ms->send_section_footer);
2233}
2234
Peter Xu20814752017-07-18 11:39:03 +08002235#define DEFINE_PROP_MIG_CAP(name, x) \
2236 DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
2237
Peter Xu52722982017-06-27 12:10:14 +08002238static Property migration_properties[] = {
2239 DEFINE_PROP_BOOL("store-global-state", MigrationState,
2240 store_global_state, true),
Peter Xu3df663e2017-06-27 12:10:15 +08002241 DEFINE_PROP_BOOL("only-migratable", MigrationState, only_migratable, false),
Peter Xu71dd4c12017-06-27 12:10:16 +08002242 DEFINE_PROP_BOOL("send-configuration", MigrationState,
2243 send_configuration, true),
Peter Xu15c38502017-06-27 12:10:17 +08002244 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
2245 send_section_footer, true),
Peter Xu89632fa2017-07-18 11:39:02 +08002246
2247 /* Migration parameters */
2248 DEFINE_PROP_INT64("x-compress-level", MigrationState,
2249 parameters.compress_level,
2250 DEFAULT_MIGRATE_COMPRESS_LEVEL),
2251 DEFINE_PROP_INT64("x-compress-threads", MigrationState,
2252 parameters.compress_threads,
2253 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
2254 DEFINE_PROP_INT64("x-decompress-threads", MigrationState,
2255 parameters.decompress_threads,
2256 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
2257 DEFINE_PROP_INT64("x-cpu-throttle-initial", MigrationState,
2258 parameters.cpu_throttle_initial,
2259 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
2260 DEFINE_PROP_INT64("x-cpu-throttle-increment", MigrationState,
2261 parameters.cpu_throttle_increment,
2262 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
2263 DEFINE_PROP_INT64("x-max-bandwidth", MigrationState,
2264 parameters.max_bandwidth, MAX_THROTTLE),
2265 DEFINE_PROP_INT64("x-downtime-limit", MigrationState,
2266 parameters.downtime_limit,
2267 DEFAULT_MIGRATE_SET_DOWNTIME),
2268 DEFINE_PROP_INT64("x-checkpoint-delay", MigrationState,
2269 parameters.x_checkpoint_delay,
2270 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
Juan Quintela4075fb12016-01-15 08:56:17 +01002271 DEFINE_PROP_INT64("x-multifd-channels", MigrationState,
2272 parameters.x_multifd_channels,
2273 DEFAULT_MIGRATE_MULTIFD_CHANNELS),
Juan Quintela0fb86602017-04-27 10:48:25 +02002274 DEFINE_PROP_INT64("x-multifd-page-count", MigrationState,
2275 parameters.x_multifd_page_count,
2276 DEFAULT_MIGRATE_MULTIFD_PAGE_COUNT),
Peter Xu20814752017-07-18 11:39:03 +08002277
2278 /* Migration capabilities */
2279 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
2280 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
2281 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
2282 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
2283 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
2284 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
2285 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
2286 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
2287 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
2288 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
2289 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
Juan Quintela30126bb2016-01-14 12:23:00 +01002290 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_X_MULTIFD),
Peter Xu20814752017-07-18 11:39:03 +08002291
Peter Xu52722982017-06-27 12:10:14 +08002292 DEFINE_PROP_END_OF_LIST(),
2293};
2294
Peter Xue5cb7e72017-06-27 12:10:13 +08002295static void migration_class_init(ObjectClass *klass, void *data)
2296{
2297 DeviceClass *dc = DEVICE_CLASS(klass);
2298
2299 dc->user_creatable = false;
Peter Xu52722982017-06-27 12:10:14 +08002300 dc->props = migration_properties;
Peter Xue5cb7e72017-06-27 12:10:13 +08002301}
2302
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01002303static void migration_instance_finalize(Object *obj)
2304{
2305 MigrationState *ms = MIGRATION_OBJ(obj);
2306 MigrationParameters *params = &ms->parameters;
2307
2308 g_free(params->tls_hostname);
2309 g_free(params->tls_creds);
2310}
2311
Peter Xue5cb7e72017-06-27 12:10:13 +08002312static void migration_instance_init(Object *obj)
2313{
2314 MigrationState *ms = MIGRATION_OBJ(obj);
Peter Xu8b0b29d2017-07-18 11:39:06 +08002315 MigrationParameters *params = &ms->parameters;
Peter Xue5cb7e72017-06-27 12:10:13 +08002316
2317 ms->state = MIGRATION_STATUS_NONE;
2318 ms->xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE;
2319 ms->mbps = -1;
Peter Xu8b0b29d2017-07-18 11:39:06 +08002320
2321 params->tls_hostname = g_strdup("");
2322 params->tls_creds = g_strdup("");
2323
2324 /* Set has_* up only for parameter checks */
2325 params->has_compress_level = true;
2326 params->has_compress_threads = true;
2327 params->has_decompress_threads = true;
2328 params->has_cpu_throttle_initial = true;
2329 params->has_cpu_throttle_increment = true;
2330 params->has_max_bandwidth = true;
2331 params->has_downtime_limit = true;
2332 params->has_x_checkpoint_delay = true;
2333 params->has_block_incremental = true;
Juan Quintela4075fb12016-01-15 08:56:17 +01002334 params->has_x_multifd_channels = true;
Juan Quintela0fb86602017-04-27 10:48:25 +02002335 params->has_x_multifd_page_count = true;
Peter Xu8b0b29d2017-07-18 11:39:06 +08002336}
2337
2338/*
2339 * Return true if check pass, false otherwise. Error will be put
2340 * inside errp if provided.
2341 */
2342static bool migration_object_check(MigrationState *ms, Error **errp)
2343{
Peter Xu6b19a7d2017-07-18 11:39:10 +08002344 MigrationCapabilityStatusList *head = NULL;
2345 /* Assuming all off */
2346 bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret;
2347 int i;
2348
Peter Xu8b0b29d2017-07-18 11:39:06 +08002349 if (!migrate_params_check(&ms->parameters, errp)) {
2350 return false;
2351 }
2352
Peter Xu6b19a7d2017-07-18 11:39:10 +08002353 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
2354 if (ms->enabled_capabilities[i]) {
2355 head = migrate_cap_add(head, i, true);
2356 }
2357 }
2358
2359 ret = migrate_caps_check(cap_list, head, errp);
2360
2361 /* It works with head == NULL */
2362 qapi_free_MigrationCapabilityStatusList(head);
2363
2364 return ret;
Peter Xue5cb7e72017-06-27 12:10:13 +08002365}
2366
2367static const TypeInfo migration_type = {
2368 .name = TYPE_MIGRATION,
Peter Xu01f6e142017-06-28 15:15:44 +08002369 /*
Peter Xuc8d3ff32017-07-05 16:21:23 +08002370 * NOTE: TYPE_MIGRATION is not really a device, as the object is
2371 * not created using qdev_create(), it is not attached to the qdev
2372 * device tree, and it is never realized.
2373 *
2374 * TODO: Make this TYPE_OBJECT once QOM provides something like
2375 * TYPE_DEVICE's "-global" properties.
Peter Xu01f6e142017-06-28 15:15:44 +08002376 */
Peter Xue5cb7e72017-06-27 12:10:13 +08002377 .parent = TYPE_DEVICE,
2378 .class_init = migration_class_init,
2379 .class_size = sizeof(MigrationClass),
2380 .instance_size = sizeof(MigrationState),
2381 .instance_init = migration_instance_init,
Marc-André Lureaub91bf5e2017-08-01 17:04:18 +01002382 .instance_finalize = migration_instance_finalize,
Peter Xue5cb7e72017-06-27 12:10:13 +08002383};
2384
2385static void register_migration_types(void)
2386{
2387 type_register_static(&migration_type);
2388}
2389
2390type_init(register_migration_types);