blob: 1ab9535f0004f9e921f219b6037eaaa2d9913126 [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"
Alex Bligh6a1751b2013-08-21 16:02:47 +010019#include "qemu/main-loop.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010020#include "migration/migration.h"
Juan Quintela0d82d0e2012-10-03 14:18:33 +020021#include "migration/qemu-file.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010022#include "sysemu/sysemu.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010023#include "block/block.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010024#include "qapi/qmp/qerror.h"
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000025#include "qapi/util.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010026#include "qemu/sockets.h"
Paolo Bonziniab28bd22015-07-09 08:55:38 +020027#include "qemu/rcu.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010028#include "migration/block.h"
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +000029#include "migration/postcopy-ram.h"
Juan Quintela766bd172012-07-23 05:45:29 +020030#include "qemu/thread.h"
Luiz Capitulino791e7c82011-09-13 17:37:16 -030031#include "qmp-commands.h"
Kazuya Saitoc09e5bb2013-02-22 17:36:19 +010032#include "trace.h"
Juan Quintela598cd2b2015-05-20 12:16:15 +020033#include "qapi-event.h"
Jason J. Herne070afca2015-09-08 13:12:35 -040034#include "qom/cpu.h"
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000035#include "exec/memory.h"
36#include "exec/address-spaces.h"
Daniel P. Berrange61b67d42016-04-27 11:05:01 +010037#include "io/channel-buffer.h"
aliguori065e2812008-11-11 16:46:33 +000038
Jason J. Hernedc325622015-09-08 13:12:37 -040039#define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000040
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010041/* Amount of time to allocate to each "chunk" of bandwidth-throttled
42 * data. */
43#define BUFFER_DELAY 100
44#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
45
Liang Li8706d2d2015-03-23 16:32:17 +080046/* Default compression thread count */
47#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080048/* Default decompression thread count, usually decompression is at
49 * least 4 times as fast as compression.*/
50#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080051/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
52#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
Jason J. Herne1626fee2015-09-08 13:12:34 -040053/* Define default autoconverge cpu throttle migration parameters */
Jason J. Herned85a31d2016-04-21 14:07:18 -040054#define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
55#define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
Liang Li8706d2d2015-03-23 16:32:17 +080056
Orit Wasserman17ad9b32012-08-06 21:42:53 +030057/* Migration XBZRLE default cache size */
58#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
59
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010060static NotifierList migration_state_notifiers =
61 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
62
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000063static bool deferred_incoming;
64
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +000065/*
66 * Current state of incoming postcopy; note this is not part of
67 * MigrationIncomingState since it's state is used during cleanup
68 * at the end as MIS is being freed.
69 */
70static PostcopyState incoming_postcopy_state;
71
Juan Quintela17549e82011-10-05 13:50:43 +020072/* When we add fault tolerance, we could have several
73 migrations at once. For now we don't need to add
74 dynamic creation of migration */
75
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010076/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +020077MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +020078{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000079 static bool once;
Juan Quintela17549e82011-10-05 13:50:43 +020080 static MigrationState current_migration = {
zhanghailiang31194732015-03-13 16:08:38 +080081 .state = MIGRATION_STATUS_NONE,
Juan Quintelad0ae46c2011-02-23 00:33:19 +010082 .bandwidth_limit = MAX_THROTTLE,
Orit Wasserman17ad9b32012-08-06 21:42:53 +030083 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
Michael R. Hines7e114f82013-06-25 21:35:30 -040084 .mbps = -1,
Liang Li43c60a82015-03-23 16:32:27 +080085 .parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] =
86 DEFAULT_MIGRATE_COMPRESS_LEVEL,
87 .parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
88 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
89 .parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
90 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
Jason J. Herned85a31d2016-04-21 14:07:18 -040091 .parameters[MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL] =
92 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL,
93 .parameters[MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT] =
94 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT,
Juan Quintela17549e82011-10-05 13:50:43 +020095 };
96
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000097 if (!once) {
98 qemu_mutex_init(&current_migration.src_page_req_mutex);
99 once = true;
100 }
Juan Quintela17549e82011-10-05 13:50:43 +0200101 return &current_migration;
102}
103
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100104/* For incoming */
105static MigrationIncomingState *mis_current;
106
107MigrationIncomingState *migration_incoming_get_current(void)
108{
109 return mis_current;
110}
111
112MigrationIncomingState *migration_incoming_state_new(QEMUFile* f)
113{
Markus Armbruster97f3ad32015-09-14 13:51:31 +0200114 mis_current = g_new0(MigrationIncomingState, 1);
Dr. David Alan Gilbert42e2aa52015-11-05 18:10:34 +0000115 mis_current->from_src_file = f;
zhanghailiang93d7af62015-12-16 11:47:34 +0000116 mis_current->state = MIGRATION_STATUS_NONE;
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +0100117 QLIST_INIT(&mis_current->loadvm_handlers);
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000118 qemu_mutex_init(&mis_current->rp_mutex);
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +0000119 qemu_event_init(&mis_current->main_thread_load_event, false);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100120
121 return mis_current;
122}
123
124void migration_incoming_state_destroy(void)
125{
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +0000126 qemu_event_destroy(&mis_current->main_thread_load_event);
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +0100127 loadvm_free_handlers(mis_current);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100128 g_free(mis_current);
129 mis_current = NULL;
130}
131
Juan Quinteladf4b1022014-10-08 10:58:10 +0200132
133typedef struct {
Juan Quintela13d16812014-10-08 13:58:24 +0200134 bool optional;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200135 uint32_t size;
136 uint8_t runstate[100];
Juan Quintela172c4352015-07-08 13:56:26 +0200137 RunState state;
138 bool received;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200139} GlobalState;
140
141static GlobalState global_state;
142
Juan Quintela560d0272015-07-15 09:53:46 +0200143int global_state_store(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200144{
145 if (!runstate_store((char *)global_state.runstate,
146 sizeof(global_state.runstate))) {
147 error_report("runstate name too big: %s", global_state.runstate);
148 trace_migrate_state_too_big();
149 return -EINVAL;
150 }
151 return 0;
152}
153
Anthony PERARDc69adea2015-08-03 15:29:19 +0100154void global_state_store_running(void)
155{
156 const char *state = RunState_lookup[RUN_STATE_RUNNING];
157 strncpy((char *)global_state.runstate,
158 state, sizeof(global_state.runstate));
159}
160
Juan Quintela172c4352015-07-08 13:56:26 +0200161static bool global_state_received(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200162{
Juan Quintela172c4352015-07-08 13:56:26 +0200163 return global_state.received;
164}
165
166static RunState global_state_get_runstate(void)
167{
168 return global_state.state;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200169}
170
Juan Quintela13d16812014-10-08 13:58:24 +0200171void global_state_set_optional(void)
172{
173 global_state.optional = true;
174}
175
176static bool global_state_needed(void *opaque)
177{
178 GlobalState *s = opaque;
179 char *runstate = (char *)s->runstate;
180
181 /* If it is not optional, it is mandatory */
182
183 if (s->optional == false) {
184 return true;
185 }
186
187 /* If state is running or paused, it is not needed */
188
189 if (strcmp(runstate, "running") == 0 ||
190 strcmp(runstate, "paused") == 0) {
191 return false;
192 }
193
194 /* for any other state it is needed */
195 return true;
196}
197
Juan Quinteladf4b1022014-10-08 10:58:10 +0200198static int global_state_post_load(void *opaque, int version_id)
199{
200 GlobalState *s = opaque;
Juan Quintela172c4352015-07-08 13:56:26 +0200201 Error *local_err = NULL;
202 int r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200203 char *runstate = (char *)s->runstate;
204
Juan Quintela172c4352015-07-08 13:56:26 +0200205 s->received = true;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200206 trace_migrate_global_state_post_load(runstate);
207
Eric Blake7fb1cf12015-11-18 01:52:57 -0700208 r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE__MAX,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200209 -1, &local_err);
210
Juan Quintela172c4352015-07-08 13:56:26 +0200211 if (r == -1) {
212 if (local_err) {
213 error_report_err(local_err);
Juan Quinteladf4b1022014-10-08 10:58:10 +0200214 }
Juan Quintela172c4352015-07-08 13:56:26 +0200215 return -EINVAL;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200216 }
Juan Quintela172c4352015-07-08 13:56:26 +0200217 s->state = r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200218
Juan Quintela172c4352015-07-08 13:56:26 +0200219 return 0;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200220}
221
222static void global_state_pre_save(void *opaque)
223{
224 GlobalState *s = opaque;
225
226 trace_migrate_global_state_pre_save((char *)s->runstate);
227 s->size = strlen((char *)s->runstate) + 1;
228}
229
230static const VMStateDescription vmstate_globalstate = {
231 .name = "globalstate",
232 .version_id = 1,
233 .minimum_version_id = 1,
234 .post_load = global_state_post_load,
235 .pre_save = global_state_pre_save,
Juan Quintela13d16812014-10-08 13:58:24 +0200236 .needed = global_state_needed,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200237 .fields = (VMStateField[]) {
238 VMSTATE_UINT32(size, GlobalState),
239 VMSTATE_BUFFER(runstate, GlobalState),
240 VMSTATE_END_OF_LIST()
241 },
242};
243
244void register_global_state(void)
245{
246 /* We would use it independently that we receive it */
247 strcpy((char *)&global_state.runstate, "");
Juan Quintela172c4352015-07-08 13:56:26 +0200248 global_state.received = false;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200249 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
250}
251
Juan Quintelab05dc722015-07-07 14:44:05 +0200252static void migrate_generate_event(int new_state)
253{
254 if (migrate_use_events()) {
255 qapi_event_send_migration(new_state, &error_abort);
Juan Quintelab05dc722015-07-07 14:44:05 +0200256 }
257}
258
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000259/*
260 * Called on -incoming with a defer: uri.
261 * The migration can be started later after any parameters have been
262 * changed.
263 */
264static void deferred_incoming_migration(Error **errp)
265{
266 if (deferred_incoming) {
267 error_setg(errp, "Incoming migration already deferred");
268 }
269 deferred_incoming = true;
270}
271
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000272/* Request a range of pages from the source VM at the given
273 * start address.
274 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
275 * as the last request (a name must have been given previously)
276 * Start: Address offset within the RB
277 * Len: Length in bytes required - must be a multiple of pagesize
278 */
279void migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
280 ram_addr_t start, size_t len)
281{
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100282 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000283 size_t msglen = 12; /* start + len */
284
285 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
286 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
287
288 if (rbname) {
289 int rbname_len = strlen(rbname);
290 assert(rbname_len < 256);
291
292 bufc[msglen++] = rbname_len;
293 memcpy(bufc + msglen, rbname, rbname_len);
294 msglen += rbname_len;
295 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES_ID, msglen, bufc);
296 } else {
297 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES, msglen, bufc);
298 }
299}
300
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200301void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000302{
aliguori34c9dd82008-10-13 03:14:31 +0000303 const char *p;
304
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200305 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000306 if (!strcmp(uri, "defer")) {
307 deferred_incoming_migration(errp);
308 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200309 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400310#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000311 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400312 rdma_start_incoming_migration(p, errp);
313#endif
aliguori065e2812008-11-11 16:46:33 +0000314#if !defined(WIN32)
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000315 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200316 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000317 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200318 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000319 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200320 fd_start_incoming_migration(p, errp);
aliguori065e2812008-11-11 16:46:33 +0000321#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000322 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100323 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200324 }
aliguori5bb79102008-10-13 03:12:02 +0000325}
326
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300327static void process_incoming_migration_bh(void *opaque)
328{
329 Error *local_err = NULL;
330 MigrationIncomingState *mis = opaque;
331
332 /* Make sure all file formats flush their mutable metadata */
333 bdrv_invalidate_cache_all(&local_err);
334 if (local_err) {
335 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
336 MIGRATION_STATUS_FAILED);
337 error_report_err(local_err);
338 migrate_decompress_threads_join();
339 exit(EXIT_FAILURE);
340 }
341
342 /*
343 * This must happen after all error conditions are dealt with and
344 * we're sure the VM is going to be running on this host.
345 */
346 qemu_announce_self();
347
348 /* If global state section was not received or we are in running
349 state, we need to obey autostart. Any other state is set with
350 runstate_set. */
351
352 if (!global_state_received() ||
353 global_state_get_runstate() == RUN_STATE_RUNNING) {
354 if (autostart) {
355 vm_start();
356 } else {
357 runstate_set(RUN_STATE_PAUSED);
358 }
359 } else {
360 runstate_set(global_state_get_runstate());
361 }
362 migrate_decompress_threads_join();
363 /*
364 * This must happen after any state changes since as soon as an external
365 * observer sees this event they might start to prod at the VM assuming
366 * it's ready to use.
367 */
368 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
369 MIGRATION_STATUS_COMPLETED);
370 qemu_bh_delete(mis->bh);
371 migration_incoming_state_destroy();
372}
373
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200374static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200375{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200376 QEMUFile *f = opaque;
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000377 MigrationIncomingState *mis;
378 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200379 int ret;
380
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000381 mis = migration_incoming_state_new(f);
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000382 postcopy_state_set(POSTCOPY_INCOMING_NONE);
zhanghailiang93d7af62015-12-16 11:47:34 +0000383 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
384 MIGRATION_STATUS_ACTIVE);
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200385 ret = qemu_loadvm_state(f);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100386
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000387 ps = postcopy_state_get();
388 trace_process_incoming_migration_co_end(ret, ps);
389 if (ps != POSTCOPY_INCOMING_NONE) {
390 if (ps == POSTCOPY_INCOMING_ADVISE) {
391 /*
392 * Where a migration had postcopy enabled (and thus went to advise)
393 * but managed to complete within the precopy period, we can use
394 * the normal exit.
395 */
396 postcopy_ram_incoming_cleanup(mis);
397 } else if (ret >= 0) {
398 /*
399 * Postcopy was started, cleanup should happen at the end of the
400 * postcopy thread.
401 */
402 trace_process_incoming_migration_co_postcopy_end_main();
403 return;
404 }
405 /* Else if something went wrong then just fall out of the normal exit */
406 }
407
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200408 qemu_fclose(f);
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200409 free_xbzrle_decoded_buf();
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100410
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200411 if (ret < 0) {
zhanghailiang93d7af62015-12-16 11:47:34 +0000412 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
413 MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200414 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800415 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600416 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200417 }
Juan Quintela511c0232010-06-09 14:10:55 +0200418
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300419 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
420 qemu_bh_schedule(mis->bh);
Juan Quintela511c0232010-06-09 14:10:55 +0200421}
422
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200423void process_incoming_migration(QEMUFile *f)
424{
425 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200426
Liang Li3fcb38c2015-03-23 16:32:18 +0800427 migrate_decompress_threads_create();
Daniel P. Berrange06ad5132016-04-27 11:04:56 +0100428 qemu_file_set_blocking(f, false);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200429 qemu_coroutine_enter(co, f);
430}
431
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100432
433void migration_set_incoming_channel(MigrationState *s,
434 QIOChannel *ioc)
435{
436 QEMUFile *f = qemu_fopen_channel_input(ioc);
437
438 process_incoming_migration(f);
439}
440
441
442void migration_set_outgoing_channel(MigrationState *s,
443 QIOChannel *ioc)
444{
445 QEMUFile *f = qemu_fopen_channel_output(ioc);
446
447 s->to_dst_file = f;
448
449 migrate_fd_connect(s);
450}
451
452
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000453/*
454 * Send a message on the return channel back to the source
455 * of the migration.
456 */
457void migrate_send_rp_message(MigrationIncomingState *mis,
458 enum mig_rp_message_type message_type,
459 uint16_t len, void *data)
460{
461 trace_migrate_send_rp_message((int)message_type, len);
462 qemu_mutex_lock(&mis->rp_mutex);
463 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
464 qemu_put_be16(mis->to_src_file, len);
465 qemu_put_buffer(mis->to_src_file, data, len);
466 qemu_fflush(mis->to_src_file);
467 qemu_mutex_unlock(&mis->rp_mutex);
468}
469
470/*
471 * Send a 'SHUT' message on the return channel with the given value
472 * to indicate that we've finished with the RP. Non-0 value indicates
473 * error.
474 */
475void migrate_send_rp_shut(MigrationIncomingState *mis,
476 uint32_t value)
477{
478 uint32_t buf;
479
480 buf = cpu_to_be32(value);
481 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
482}
483
484/*
485 * Send a 'PONG' message on the return channel with the given value
486 * (normally in response to a 'PING')
487 */
488void migrate_send_rp_pong(MigrationIncomingState *mis,
489 uint32_t value)
490{
491 uint32_t buf;
492
493 buf = cpu_to_be32(value);
494 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
495}
496
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400497/* amount of nanoseconds we are willing to wait for migration to be down.
498 * the choice of nanoseconds is because it is the maximum resolution that
499 * get_clock() can achieve. It is an internal measure. All user-visible
500 * units must be in seconds */
Alexey Kardashevskiyf7cd55a2014-03-27 14:57:26 +1100501static uint64_t max_downtime = 300000000;
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400502
503uint64_t migrate_max_downtime(void)
504{
505 return max_downtime;
506}
507
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300508MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
509{
510 MigrationCapabilityStatusList *head = NULL;
511 MigrationCapabilityStatusList *caps;
512 MigrationState *s = migrate_get_current();
513 int i;
514
Michael Tokarev387eede2013-10-05 13:18:28 +0400515 caps = NULL; /* silence compiler warning */
Eric Blake7fb1cf12015-11-18 01:52:57 -0700516 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300517 if (head == NULL) {
518 head = g_malloc0(sizeof(*caps));
519 caps = head;
520 } else {
521 caps->next = g_malloc0(sizeof(*caps));
522 caps = caps->next;
523 }
524 caps->value =
525 g_malloc(sizeof(*caps->value));
526 caps->value->capability = i;
527 caps->value->state = s->enabled_capabilities[i];
528 }
529
530 return head;
531}
532
Liang Li85de8322015-03-23 16:32:28 +0800533MigrationParameters *qmp_query_migrate_parameters(Error **errp)
534{
535 MigrationParameters *params;
536 MigrationState *s = migrate_get_current();
537
538 params = g_malloc0(sizeof(*params));
539 params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
540 params->compress_threads =
541 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
542 params->decompress_threads =
543 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Jason J. Herned85a31d2016-04-21 14:07:18 -0400544 params->cpu_throttle_initial =
545 s->parameters[MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL];
546 params->cpu_throttle_increment =
547 s->parameters[MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT];
Liang Li85de8322015-03-23 16:32:28 +0800548
549 return params;
550}
551
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000552/*
553 * Return true if we're already in the middle of a migration
554 * (i.e. any of the active or setup states)
555 */
556static bool migration_is_setup_or_active(int state)
557{
558 switch (state) {
559 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000560 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000561 case MIGRATION_STATUS_SETUP:
562 return true;
563
564 default:
565 return false;
566
567 }
568}
569
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300570static void get_xbzrle_cache_stats(MigrationInfo *info)
571{
572 if (migrate_use_xbzrle()) {
573 info->has_xbzrle_cache = true;
574 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
575 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
576 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
577 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
578 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
ChenLiang8bc39232014-04-04 17:57:56 +0800579 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300580 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
581 }
582}
583
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300584MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000585{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300586 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200587 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000588
Juan Quintela17549e82011-10-05 13:50:43 +0200589 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800590 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200591 /* no migration has happened ever */
592 break;
zhanghailiang31194732015-03-13 16:08:38 +0800593 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400594 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400595 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400596 break;
zhanghailiang31194732015-03-13 16:08:38 +0800597 case MIGRATION_STATUS_ACTIVE:
598 case MIGRATION_STATUS_CANCELLING:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300599 info->has_status = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200600 info->has_total_time = true;
Alex Blighbc72ad62013-08-21 16:03:08 +0100601 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Juan Quintela7aa939a2012-08-18 13:17:10 +0200602 - s->total_time;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200603 info->has_expected_downtime = true;
604 info->expected_downtime = s->expected_downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400605 info->has_setup_time = true;
606 info->setup_time = s->setup_time;
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200607
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300608 info->has_ram = true;
609 info->ram = g_malloc0(sizeof(*info->ram));
610 info->ram->transferred = ram_bytes_transferred();
611 info->ram->remaining = ram_bytes_remaining();
612 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300613 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100614 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300615 info->ram->normal = norm_mig_pages_transferred();
616 info->ram->normal_bytes = norm_mig_bytes_transferred();
Juan Quintela8d017192012-08-13 12:31:25 +0200617 info->ram->dirty_pages_rate = s->dirty_pages_rate;
Michael R. Hines7e114f82013-06-25 21:35:30 -0400618 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800619 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela8d017192012-08-13 12:31:25 +0200620
Juan Quintela17549e82011-10-05 13:50:43 +0200621 if (blk_mig_active()) {
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300622 info->has_disk = true;
623 info->disk = g_malloc0(sizeof(*info->disk));
624 info->disk->transferred = blk_mig_bytes_transferred();
625 info->disk->remaining = blk_mig_bytes_remaining();
626 info->disk->total = blk_mig_bytes_total();
aliguoriff8d81d2008-10-24 22:10:31 +0000627 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300628
Jason J. Herne47828932015-09-08 13:12:36 -0400629 if (cpu_throttle_active()) {
Jason J. Herned85a31d2016-04-21 14:07:18 -0400630 info->has_cpu_throttle_percentage = true;
631 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
Jason J. Herne47828932015-09-08 13:12:36 -0400632 }
633
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300634 get_xbzrle_cache_stats(info);
Juan Quintela17549e82011-10-05 13:50:43 +0200635 break;
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000636 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
637 /* Mostly the same as active; TODO add some postcopy stats */
638 info->has_status = true;
639 info->has_total_time = true;
640 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
641 - s->total_time;
642 info->has_expected_downtime = true;
643 info->expected_downtime = s->expected_downtime;
644 info->has_setup_time = true;
645 info->setup_time = s->setup_time;
646
647 info->has_ram = true;
648 info->ram = g_malloc0(sizeof(*info->ram));
649 info->ram->transferred = ram_bytes_transferred();
650 info->ram->remaining = ram_bytes_remaining();
651 info->ram->total = ram_bytes_total();
652 info->ram->duplicate = dup_mig_pages_transferred();
653 info->ram->skipped = skipped_mig_pages_transferred();
654 info->ram->normal = norm_mig_pages_transferred();
655 info->ram->normal_bytes = norm_mig_bytes_transferred();
656 info->ram->dirty_pages_rate = s->dirty_pages_rate;
657 info->ram->mbps = s->mbps;
Dr. David Alan Gilbert614e8012016-02-25 10:03:51 +0000658 info->ram->dirty_sync_count = s->dirty_sync_count;
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000659
660 if (blk_mig_active()) {
661 info->has_disk = true;
662 info->disk = g_malloc0(sizeof(*info->disk));
663 info->disk->transferred = blk_mig_bytes_transferred();
664 info->disk->remaining = blk_mig_bytes_remaining();
665 info->disk->total = blk_mig_bytes_total();
666 }
667
668 get_xbzrle_cache_stats(info);
669 break;
zhanghailiang31194732015-03-13 16:08:38 +0800670 case MIGRATION_STATUS_COMPLETED:
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300671 get_xbzrle_cache_stats(info);
672
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300673 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900674 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200675 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200676 info->has_downtime = true;
677 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400678 info->has_setup_time = true;
679 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200680
681 info->has_ram = true;
682 info->ram = g_malloc0(sizeof(*info->ram));
683 info->ram->transferred = ram_bytes_transferred();
684 info->ram->remaining = 0;
685 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300686 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100687 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300688 info->ram->normal = norm_mig_pages_transferred();
689 info->ram->normal_bytes = norm_mig_bytes_transferred();
Michael R. Hines7e114f82013-06-25 21:35:30 -0400690 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800691 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela17549e82011-10-05 13:50:43 +0200692 break;
zhanghailiang31194732015-03-13 16:08:38 +0800693 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300694 info->has_status = true;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100695 if (s->error) {
696 info->has_error_desc = true;
697 info->error_desc = g_strdup(error_get_pretty(s->error));
698 }
Juan Quintela17549e82011-10-05 13:50:43 +0200699 break;
zhanghailiang31194732015-03-13 16:08:38 +0800700 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300701 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200702 break;
aliguori5bb79102008-10-13 03:12:02 +0000703 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800704 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300705
706 return info;
aliguori5bb79102008-10-13 03:12:02 +0000707}
708
Orit Wasserman00458432012-08-06 21:42:48 +0300709void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
710 Error **errp)
711{
712 MigrationState *s = migrate_get_current();
713 MigrationCapabilityStatusList *cap;
714
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000715 if (migration_is_setup_or_active(s->state)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100716 error_setg(errp, QERR_MIGRATION_ACTIVE);
Orit Wasserman00458432012-08-06 21:42:48 +0300717 return;
718 }
719
720 for (cap = params; cap; cap = cap->next) {
721 s->enabled_capabilities[cap->value->capability] = cap->value->state;
722 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000723
724 if (migrate_postcopy_ram()) {
725 if (migrate_use_compression()) {
726 /* The decompression threads asynchronously write into RAM
727 * rather than use the atomic copies needed to avoid
728 * userfaulting. It should be possible to fix the decompression
729 * threads for compatibility in future.
730 */
731 error_report("Postcopy is not currently compatible with "
732 "compression");
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +0000733 s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM] =
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000734 false;
735 }
736 }
Orit Wasserman00458432012-08-06 21:42:48 +0300737}
738
Liang Li85de8322015-03-23 16:32:28 +0800739void qmp_migrate_set_parameters(bool has_compress_level,
740 int64_t compress_level,
741 bool has_compress_threads,
742 int64_t compress_threads,
743 bool has_decompress_threads,
Jason J. Herne1626fee2015-09-08 13:12:34 -0400744 int64_t decompress_threads,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400745 bool has_cpu_throttle_initial,
746 int64_t cpu_throttle_initial,
747 bool has_cpu_throttle_increment,
748 int64_t cpu_throttle_increment, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +0800749{
750 MigrationState *s = migrate_get_current();
751
752 if (has_compress_level && (compress_level < 0 || compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100753 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
754 "is invalid, it should be in the range of 0 to 9");
Liang Li85de8322015-03-23 16:32:28 +0800755 return;
756 }
757 if (has_compress_threads &&
758 (compress_threads < 1 || compress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100759 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
760 "compress_threads",
761 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800762 return;
763 }
764 if (has_decompress_threads &&
765 (decompress_threads < 1 || decompress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100766 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
767 "decompress_threads",
768 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800769 return;
770 }
Jason J. Herned85a31d2016-04-21 14:07:18 -0400771 if (has_cpu_throttle_initial &&
772 (cpu_throttle_initial < 1 || cpu_throttle_initial > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400773 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400774 "cpu_throttle_initial",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400775 "an integer in the range of 1 to 99");
776 }
Jason J. Herned85a31d2016-04-21 14:07:18 -0400777 if (has_cpu_throttle_increment &&
778 (cpu_throttle_increment < 1 || cpu_throttle_increment > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400779 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400780 "cpu_throttle_increment",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400781 "an integer in the range of 1 to 99");
782 }
Liang Li85de8322015-03-23 16:32:28 +0800783
784 if (has_compress_level) {
785 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
786 }
787 if (has_compress_threads) {
788 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads;
789 }
790 if (has_decompress_threads) {
791 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
792 decompress_threads;
793 }
Jason J. Herned85a31d2016-04-21 14:07:18 -0400794 if (has_cpu_throttle_initial) {
795 s->parameters[MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL] =
796 cpu_throttle_initial;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400797 }
798
Jason J. Herned85a31d2016-04-21 14:07:18 -0400799 if (has_cpu_throttle_increment) {
800 s->parameters[MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT] =
801 cpu_throttle_increment;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400802 }
Liang Li85de8322015-03-23 16:32:28 +0800803}
804
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000805void qmp_migrate_start_postcopy(Error **errp)
806{
807 MigrationState *s = migrate_get_current();
808
809 if (!migrate_postcopy_ram()) {
Dr. David Alan Gilberta54d3402015-11-12 11:34:44 +0000810 error_setg(errp, "Enable postcopy with migrate_set_capability before"
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000811 " the start of migration");
812 return;
813 }
814
815 if (s->state == MIGRATION_STATUS_NONE) {
816 error_setg(errp, "Postcopy must be started after migration has been"
817 " started");
818 return;
819 }
820 /*
821 * we don't error if migration has finished since that would be racy
822 * with issuing this command.
823 */
824 atomic_set(&s->start_postcopy, true);
825}
826
aliguori065e2812008-11-11 16:46:33 +0000827/* shared migration helpers */
828
zhanghailiang48781e52015-12-16 11:47:33 +0000829void migrate_set_state(int *state, int old_state, int new_state)
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000830{
zhanghailiang48781e52015-12-16 11:47:33 +0000831 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
Juan Quintela4ba4bc52015-07-08 13:58:27 +0200832 trace_migrate_set_state(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200833 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000834 }
835}
836
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100837static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000838{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100839 MigrationState *s = opaque;
840
841 qemu_bh_delete(s->cleanup_bh);
842 s->cleanup_bh = NULL;
843
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +0000844 flush_page_queue(s);
845
zhanghailiang89a02a92016-01-15 11:37:42 +0800846 if (s->to_dst_file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100847 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100848 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +0000849 if (s->migration_thread_running) {
850 qemu_thread_join(&s->thread);
851 s->migration_thread_running = false;
852 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100853 qemu_mutex_lock_iothread();
854
Liang Li8706d2d2015-03-23 16:32:17 +0800855 migrate_compress_threads_join();
zhanghailiang89a02a92016-01-15 11:37:42 +0800856 qemu_fclose(s->to_dst_file);
857 s->to_dst_file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000858 }
859
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000860 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
861 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100862
Liang Li94f5a432015-11-02 15:37:00 +0800863 if (s->state == MIGRATION_STATUS_CANCELLING) {
zhanghailiang48781e52015-12-16 11:47:33 +0000864 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
Liang Li94f5a432015-11-02 15:37:00 +0800865 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100866 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100867
868 notifier_list_notify(&migration_state_notifiers, s);
aliguori065e2812008-11-11 16:46:33 +0000869}
870
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100871void migrate_fd_error(MigrationState *s, const Error *error)
aliguori065e2812008-11-11 16:46:33 +0000872{
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100873 trace_migrate_fd_error(error ? error_get_pretty(error) : "");
zhanghailiang89a02a92016-01-15 11:37:42 +0800874 assert(s->to_dst_file == NULL);
zhanghailiang48781e52015-12-16 11:47:33 +0000875 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
876 MIGRATION_STATUS_FAILED);
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100877 if (!s->error) {
878 s->error = error_copy(error);
879 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100880 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela458cf282011-02-22 23:32:54 +0100881}
882
Juan Quintela0edda1c2010-05-11 16:28:39 +0200883static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000884{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000885 int old_state ;
zhanghailiang89a02a92016-01-15 11:37:42 +0800886 QEMUFile *f = migrate_get_current()->to_dst_file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100887 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000888
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +0000889 if (s->rp_state.from_dst_file) {
890 /* shutdown the rp socket, so causing the rp thread to shutdown */
891 qemu_file_shutdown(s->rp_state.from_dst_file);
892 }
893
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000894 do {
895 old_state = s->state;
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000896 if (!migration_is_setup_or_active(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000897 break;
898 }
zhanghailiang48781e52015-12-16 11:47:33 +0000899 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
zhanghailiang31194732015-03-13 16:08:38 +0800900 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000901
902 /*
903 * If we're unlucky the migration code might be stuck somewhere in a
904 * send/write while the network has failed and is waiting to timeout;
905 * if we've got shutdown(2) available then we can force it to quit.
906 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
907 * called in a bh, so there is no race against this cancel.
908 */
zhanghailiang31194732015-03-13 16:08:38 +0800909 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000910 qemu_file_shutdown(f);
911 }
aliguori065e2812008-11-11 16:46:33 +0000912}
913
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100914void add_migration_state_change_notifier(Notifier *notify)
915{
916 notifier_list_add(&migration_state_notifiers, notify);
917}
918
919void remove_migration_state_change_notifier(Notifier *notify)
920{
Paolo Bonzini31552522012-01-13 17:34:01 +0100921 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100922}
923
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +0200924bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200925{
zhanghailiang31194732015-03-13 16:08:38 +0800926 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200927}
928
Juan Quintela70736932011-02-23 00:43:59 +0100929bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100930{
zhanghailiang31194732015-03-13 16:08:38 +0800931 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100932}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200933
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200934bool migration_has_failed(MigrationState *s)
935{
zhanghailiang31194732015-03-13 16:08:38 +0800936 return (s->state == MIGRATION_STATUS_CANCELLED ||
937 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200938}
939
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000940bool migration_in_postcopy(MigrationState *s)
941{
942 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
943}
944
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +0000945bool migration_in_postcopy_after_devices(MigrationState *s)
946{
947 return migration_in_postcopy(s) && s->postcopy_after_devices;
948}
949
Dr. David Alan Gilbertaefeb182015-11-05 18:10:40 +0000950MigrationState *migrate_init(const MigrationParams *params)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200951{
Juan Quintela17549e82011-10-05 13:50:43 +0200952 MigrationState *s = migrate_get_current();
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300953
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000954 /*
955 * Reinitialise all migration state, except
956 * parameters/capabilities that the user set, and
957 * locks.
958 */
959 s->bytes_xfer = 0;
960 s->xfer_limit = 0;
961 s->cleanup_bh = 0;
zhanghailiang89a02a92016-01-15 11:37:42 +0800962 s->to_dst_file = NULL;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000963 s->state = MIGRATION_STATUS_NONE;
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300964 s->params = *params;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000965 s->rp_state.from_dst_file = NULL;
966 s->rp_state.error = false;
967 s->mbps = 0.0;
968 s->downtime = 0;
969 s->expected_downtime = 0;
970 s->dirty_pages_rate = 0;
971 s->dirty_bytes_rate = 0;
972 s->setup_time = 0;
973 s->dirty_sync_count = 0;
974 s->start_postcopy = false;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +0000975 s->postcopy_after_devices = false;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000976 s->migration_thread_running = false;
977 s->last_req_rb = NULL;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100978 error_free(s->error);
979 s->error = NULL;
Juan Quintela1299c632011-11-09 21:29:01 +0100980
zhanghailiang48781e52015-12-16 11:47:33 +0000981 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200982
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +0000983 QSIMPLEQ_INIT(&s->src_page_requests);
984
Alex Blighbc72ad62013-08-21 16:03:08 +0100985 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200986 return s;
987}
Juan Quintelacab30142011-02-22 23:54:21 +0100988
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600989static GSList *migration_blockers;
990
991void migrate_add_blocker(Error *reason)
992{
993 migration_blockers = g_slist_prepend(migration_blockers, reason);
994}
995
996void migrate_del_blocker(Error *reason)
997{
998 migration_blockers = g_slist_remove(migration_blockers, reason);
999}
1000
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001001void qmp_migrate_incoming(const char *uri, Error **errp)
1002{
1003 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001004 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001005
1006 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001007 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001008 return;
1009 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001010 if (!once) {
1011 error_setg(errp, "The incoming migration has already been started");
1012 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001013
1014 qemu_start_incoming_migration(uri, &local_err);
1015
1016 if (local_err) {
1017 error_propagate(errp, local_err);
1018 return;
1019 }
1020
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001021 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001022}
1023
Greg Kurz24f39022016-05-04 21:44:19 +02001024bool migration_is_blocked(Error **errp)
1025{
1026 if (qemu_savevm_state_blocked(errp)) {
1027 return true;
1028 }
1029
1030 if (migration_blockers) {
1031 *errp = error_copy(migration_blockers->data);
1032 return true;
1033 }
1034
1035 return false;
1036}
1037
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001038void qmp_migrate(const char *uri, bool has_blk, bool blk,
1039 bool has_inc, bool inc, bool has_detach, bool detach,
1040 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001041{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +02001042 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +02001043 MigrationState *s = migrate_get_current();
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001044 MigrationParams params;
Juan Quintelacab30142011-02-22 23:54:21 +01001045 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +01001046
Pawit Pornkitprasan8c0426a2013-07-30 08:39:52 +09001047 params.blk = has_blk && blk;
1048 params.shared = has_inc && inc;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001049
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +00001050 if (migration_is_setup_or_active(s->state) ||
zhanghailiang31194732015-03-13 16:08:38 +08001051 s->state == MIGRATION_STATUS_CANCELLING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001052 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001053 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001054 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +01001055 if (runstate_check(RUN_STATE_INMIGRATE)) {
1056 error_setg(errp, "Guest is waiting for an incoming migration");
1057 return;
1058 }
1059
Greg Kurz24f39022016-05-04 21:44:19 +02001060 if (migration_is_blocked(errp)) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001061 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001062 }
1063
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001064 s = migrate_init(&params);
Juan Quintelacab30142011-02-22 23:54:21 +01001065
1066 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001067 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04001068#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08001069 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04001070 rdma_start_outgoing_migration(s, p, &local_err);
1071#endif
Juan Quintelacab30142011-02-22 23:54:21 +01001072#if !defined(WIN32)
1073 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001074 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001075 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001076 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001077 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001078 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001079#endif
1080 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001081 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1082 "a valid migration protocol");
zhanghailiang48781e52015-12-16 11:47:33 +00001083 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1084 MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001085 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001086 }
1087
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001088 if (local_err) {
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001089 migrate_fd_error(s, local_err);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001090 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001091 return;
Juan Quintela1299c632011-11-09 21:29:01 +01001092 }
Juan Quintelacab30142011-02-22 23:54:21 +01001093}
1094
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02001095void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001096{
Juan Quintela17549e82011-10-05 13:50:43 +02001097 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01001098}
1099
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001100void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1101{
1102 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +02001103 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001104
1105 /* Check for truncation */
1106 if (value != (size_t)value) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001107 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1108 "exceeding address space");
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001109 return;
1110 }
1111
Orit Wassermana5615b12014-01-30 20:08:36 +02001112 /* Cache should not be larger than guest ram size */
1113 if (value > ram_bytes_total()) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001114 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1115 "exceeds guest ram size ");
Orit Wassermana5615b12014-01-30 20:08:36 +02001116 return;
1117 }
1118
Orit Wassermanc91e6812014-01-30 20:08:34 +02001119 new_size = xbzrle_cache_resize(value);
1120 if (new_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001121 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1122 "is smaller than page size");
Orit Wassermanc91e6812014-01-30 20:08:34 +02001123 return;
1124 }
1125
1126 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001127}
1128
1129int64_t qmp_query_migrate_cache_size(Error **errp)
1130{
1131 return migrate_xbzrle_cache_size();
1132}
1133
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001134void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001135{
Juan Quintelacab30142011-02-22 23:54:21 +01001136 MigrationState *s;
1137
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001138 if (value < 0) {
1139 value = 0;
Juan Quintelacab30142011-02-22 23:54:21 +01001140 }
Paolo Bonzini442773c2013-02-22 17:36:44 +01001141 if (value > SIZE_MAX) {
1142 value = SIZE_MAX;
1143 }
Juan Quintelacab30142011-02-22 23:54:21 +01001144
Juan Quintela17549e82011-10-05 13:50:43 +02001145 s = migrate_get_current();
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001146 s->bandwidth_limit = value;
zhanghailiang89a02a92016-01-15 11:37:42 +08001147 if (s->to_dst_file) {
1148 qemu_file_set_rate_limit(s->to_dst_file,
1149 s->bandwidth_limit / XFER_LIMIT_RATIO);
Paolo Bonzini442773c2013-02-22 17:36:44 +01001150 }
Juan Quintelacab30142011-02-22 23:54:21 +01001151}
1152
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001153void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001154{
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001155 value *= 1e9;
1156 value = MAX(0, MIN(UINT64_MAX, value));
1157 max_downtime = (uint64_t)value;
aliguori5bb79102008-10-13 03:12:02 +00001158}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001159
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001160bool migrate_postcopy_ram(void)
1161{
1162 MigrationState *s;
1163
1164 s = migrate_get_current();
1165
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +00001166 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001167}
1168
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06001169bool migrate_auto_converge(void)
1170{
1171 MigrationState *s;
1172
1173 s = migrate_get_current();
1174
1175 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1176}
1177
Peter Lieven323004a2013-07-18 09:48:50 +02001178bool migrate_zero_blocks(void)
1179{
1180 MigrationState *s;
1181
1182 s = migrate_get_current();
1183
1184 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1185}
1186
Liang Li8706d2d2015-03-23 16:32:17 +08001187bool migrate_use_compression(void)
1188{
Liang Lidde4e692015-03-23 16:32:26 +08001189 MigrationState *s;
1190
1191 s = migrate_get_current();
1192
1193 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08001194}
1195
1196int migrate_compress_level(void)
1197{
1198 MigrationState *s;
1199
1200 s = migrate_get_current();
1201
Liang Li43c60a82015-03-23 16:32:27 +08001202 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
Liang Li8706d2d2015-03-23 16:32:17 +08001203}
1204
1205int migrate_compress_threads(void)
1206{
1207 MigrationState *s;
1208
1209 s = migrate_get_current();
1210
Liang Li43c60a82015-03-23 16:32:27 +08001211 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
Liang Li8706d2d2015-03-23 16:32:17 +08001212}
1213
Liang Li3fcb38c2015-03-23 16:32:18 +08001214int migrate_decompress_threads(void)
1215{
1216 MigrationState *s;
1217
1218 s = migrate_get_current();
1219
Liang Li43c60a82015-03-23 16:32:27 +08001220 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Liang Li3fcb38c2015-03-23 16:32:18 +08001221}
1222
Juan Quintelab05dc722015-07-07 14:44:05 +02001223bool migrate_use_events(void)
1224{
1225 MigrationState *s;
1226
1227 s = migrate_get_current();
1228
1229 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1230}
1231
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001232int migrate_use_xbzrle(void)
1233{
1234 MigrationState *s;
1235
1236 s = migrate_get_current();
1237
1238 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1239}
1240
1241int64_t migrate_xbzrle_cache_size(void)
1242{
1243 MigrationState *s;
1244
1245 s = migrate_get_current();
1246
1247 return s->xbzrle_cache_size;
1248}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001249
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001250/* migration thread support */
1251/*
1252 * Something bad happened to the RP stream, mark an error
1253 * The caller shall print or trace something to indicate why
1254 */
1255static void mark_source_rp_bad(MigrationState *s)
1256{
1257 s->rp_state.error = true;
1258}
1259
1260static struct rp_cmd_args {
1261 ssize_t len; /* -1 = variable */
1262 const char *name;
1263} rp_cmd_args[] = {
1264 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1265 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1266 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001267 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1268 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001269 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1270};
1271
1272/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001273 * Process a request for pages received on the return path,
1274 * We're allowed to send more than requested (e.g. to round to our page size)
1275 * and we don't need to send pages that have already been sent.
1276 */
1277static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1278 ram_addr_t start, size_t len)
1279{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001280 long our_host_ps = getpagesize();
1281
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001282 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001283
1284 /*
1285 * Since we currently insist on matching page sizes, just sanity check
1286 * we're being asked for whole host pages.
1287 */
1288 if (start & (our_host_ps-1) ||
1289 (len & (our_host_ps-1))) {
1290 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1291 " len: %zd", __func__, start, len);
1292 mark_source_rp_bad(ms);
1293 return;
1294 }
1295
1296 if (ram_save_queue_pages(ms, rbname, start, len)) {
1297 mark_source_rp_bad(ms);
1298 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001299}
1300
1301/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001302 * Handles messages sent on the return path towards the source VM
1303 *
1304 */
1305static void *source_return_path_thread(void *opaque)
1306{
1307 MigrationState *ms = opaque;
1308 QEMUFile *rp = ms->rp_state.from_dst_file;
1309 uint16_t header_len, header_type;
Peter Xu568b01c2016-03-09 14:12:12 +08001310 uint8_t buf[512];
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001311 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001312 ram_addr_t start = 0; /* =0 to silence warning */
1313 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001314 int res;
1315
1316 trace_source_return_path_thread_entry();
1317 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1318 migration_is_setup_or_active(ms->state)) {
1319 trace_source_return_path_thread_loop_top();
1320 header_type = qemu_get_be16(rp);
1321 header_len = qemu_get_be16(rp);
1322
1323 if (header_type >= MIG_RP_MSG_MAX ||
1324 header_type == MIG_RP_MSG_INVALID) {
1325 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1326 header_type, header_len);
1327 mark_source_rp_bad(ms);
1328 goto out;
1329 }
1330
1331 if ((rp_cmd_args[header_type].len != -1 &&
1332 header_len != rp_cmd_args[header_type].len) ||
Peter Xu568b01c2016-03-09 14:12:12 +08001333 header_len > sizeof(buf)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001334 error_report("RP: Received '%s' message (0x%04x) with"
1335 "incorrect length %d expecting %zu",
1336 rp_cmd_args[header_type].name, header_type, header_len,
1337 (size_t)rp_cmd_args[header_type].len);
1338 mark_source_rp_bad(ms);
1339 goto out;
1340 }
1341
1342 /* We know we've got a valid header by this point */
1343 res = qemu_get_buffer(rp, buf, header_len);
1344 if (res != header_len) {
1345 error_report("RP: Failed reading data for message 0x%04x"
1346 " read %d expected %d",
1347 header_type, res, header_len);
1348 mark_source_rp_bad(ms);
1349 goto out;
1350 }
1351
1352 /* OK, we have the message and the data */
1353 switch (header_type) {
1354 case MIG_RP_MSG_SHUT:
1355 sibling_error = be32_to_cpup((uint32_t *)buf);
1356 trace_source_return_path_thread_shut(sibling_error);
1357 if (sibling_error) {
1358 error_report("RP: Sibling indicated error %d", sibling_error);
1359 mark_source_rp_bad(ms);
1360 }
1361 /*
1362 * We'll let the main thread deal with closing the RP
1363 * we could do a shutdown(2) on it, but we're the only user
1364 * anyway, so there's nothing gained.
1365 */
1366 goto out;
1367
1368 case MIG_RP_MSG_PONG:
1369 tmp32 = be32_to_cpup((uint32_t *)buf);
1370 trace_source_return_path_thread_pong(tmp32);
1371 break;
1372
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001373 case MIG_RP_MSG_REQ_PAGES:
1374 start = be64_to_cpup((uint64_t *)buf);
1375 len = be32_to_cpup((uint32_t *)(buf + 8));
1376 migrate_handle_rp_req_pages(ms, NULL, start, len);
1377 break;
1378
1379 case MIG_RP_MSG_REQ_PAGES_ID:
1380 expected_len = 12 + 1; /* header + termination */
1381
1382 if (header_len >= expected_len) {
1383 start = be64_to_cpup((uint64_t *)buf);
1384 len = be32_to_cpup((uint32_t *)(buf + 8));
1385 /* Now we expect an idstr */
1386 tmp32 = buf[12]; /* Length of the following idstr */
1387 buf[13 + tmp32] = '\0';
1388 expected_len += tmp32;
1389 }
1390 if (header_len != expected_len) {
1391 error_report("RP: Req_Page_id with length %d expecting %zd",
1392 header_len, expected_len);
1393 mark_source_rp_bad(ms);
1394 goto out;
1395 }
1396 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
1397 break;
1398
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001399 default:
1400 break;
1401 }
1402 }
Dr. David Alan Gilbert5df54162015-11-18 11:48:41 +00001403 if (qemu_file_get_error(rp)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001404 trace_source_return_path_thread_bad_end();
1405 mark_source_rp_bad(ms);
1406 }
1407
1408 trace_source_return_path_thread_end();
1409out:
1410 ms->rp_state.from_dst_file = NULL;
1411 qemu_fclose(rp);
1412 return NULL;
1413}
1414
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001415static int open_return_path_on_source(MigrationState *ms)
1416{
1417
zhanghailiang89a02a92016-01-15 11:37:42 +08001418 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001419 if (!ms->rp_state.from_dst_file) {
1420 return -1;
1421 }
1422
1423 trace_open_return_path_on_source();
1424 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1425 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1426
1427 trace_open_return_path_on_source_continue();
1428
1429 return 0;
1430}
1431
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001432/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1433static int await_return_path_close_on_source(MigrationState *ms)
1434{
1435 /*
1436 * If this is a normal exit then the destination will send a SHUT and the
1437 * rp_thread will exit, however if there's an error we need to cause
1438 * it to exit.
1439 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001440 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001441 /*
1442 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1443 * waiting for the destination.
1444 */
1445 qemu_file_shutdown(ms->rp_state.from_dst_file);
1446 mark_source_rp_bad(ms);
1447 }
1448 trace_await_return_path_close_on_source_joining();
1449 qemu_thread_join(&ms->rp_state.rp_thread);
1450 trace_await_return_path_close_on_source_close();
1451 return ms->rp_state.error;
1452}
1453
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001454/*
1455 * Switch from normal iteration to postcopy
1456 * Returns non-0 on error
1457 */
1458static int postcopy_start(MigrationState *ms, bool *old_vm_running)
1459{
1460 int ret;
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001461 QIOChannelBuffer *bioc;
1462 QEMUFile *fb;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001463 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
zhanghailiang48781e52015-12-16 11:47:33 +00001464 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001465 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1466
1467 trace_postcopy_start();
1468 qemu_mutex_lock_iothread();
1469 trace_postcopy_start_set_run();
1470
1471 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1472 *old_vm_running = runstate_is_running();
1473 global_state_store();
1474 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001475 if (ret < 0) {
1476 goto fail;
1477 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001478
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001479 ret = bdrv_inactivate_all();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001480 if (ret < 0) {
1481 goto fail;
1482 }
1483
1484 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001485 * Cause any non-postcopiable, but iterative devices to
1486 * send out their final data.
1487 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001488 qemu_savevm_state_complete_precopy(ms->to_dst_file, true);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001489
1490 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001491 * in Finish migrate and with the io-lock held everything should
1492 * be quiet, but we've potentially still got dirty pages and we
1493 * need to tell the destination to throw any pages it's already received
1494 * that are dirty
1495 */
1496 if (ram_postcopy_send_discard_bitmap(ms)) {
1497 error_report("postcopy send discard bitmap failed");
1498 goto fail;
1499 }
1500
1501 /*
1502 * send rest of state - note things that are doing postcopy
1503 * will notice we're in POSTCOPY_ACTIVE and not actually
1504 * wrap their state up here
1505 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001506 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001507 /* Ping just for debugging, helps line traces up */
zhanghailiang89a02a92016-01-15 11:37:42 +08001508 qemu_savevm_send_ping(ms->to_dst_file, 2);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001509
1510 /*
1511 * While loading the device state we may trigger page transfer
1512 * requests and the fd must be free to process those, and thus
1513 * the destination must read the whole device state off the fd before
1514 * it starts processing it. Unfortunately the ad-hoc migration format
1515 * doesn't allow the destination to know the size to read without fully
1516 * parsing it through each devices load-state code (especially the open
1517 * coded devices that use get/put).
1518 * So we wrap the device state up in a package with a length at the start;
1519 * to do this we use a qemu_buf to hold the whole of the device state.
1520 */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001521 bioc = qio_channel_buffer_new(4096);
1522 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
1523 object_unref(OBJECT(bioc));
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001524
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001525 /*
1526 * Make sure the receiver can get incoming pages before we send the rest
1527 * of the state
1528 */
1529 qemu_savevm_send_postcopy_listen(fb);
1530
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001531 qemu_savevm_state_complete_precopy(fb, false);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001532 qemu_savevm_send_ping(fb, 3);
1533
1534 qemu_savevm_send_postcopy_run(fb);
1535
1536 /* <><> end of stuff going into the package */
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001537
1538 /* Now send that blob */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001539 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001540 goto fail_closefb;
1541 }
1542 qemu_fclose(fb);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001543
1544 /* Send a notify to give a chance for anything that needs to happen
1545 * at the transition to postcopy and after the device state; in particular
1546 * spice needs to trigger a transition now
1547 */
1548 ms->postcopy_after_devices = true;
1549 notifier_list_notify(&migration_state_notifiers, ms);
1550
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001551 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
1552
1553 qemu_mutex_unlock_iothread();
1554
1555 /*
1556 * Although this ping is just for debug, it could potentially be
1557 * used for getting a better measurement of downtime at the source.
1558 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001559 qemu_savevm_send_ping(ms->to_dst_file, 4);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001560
zhanghailiang89a02a92016-01-15 11:37:42 +08001561 ret = qemu_file_get_error(ms->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001562 if (ret) {
1563 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00001564 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001565 MIGRATION_STATUS_FAILED);
1566 }
1567
1568 return ret;
1569
1570fail_closefb:
1571 qemu_fclose(fb);
1572fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001573 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001574 MIGRATION_STATUS_FAILED);
1575 qemu_mutex_unlock_iothread();
1576 return -1;
1577}
1578
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001579/**
1580 * migration_completion: Used by migration_thread when there's not much left.
1581 * The caller 'breaks' the loop when this returns.
1582 *
1583 * @s: Current migration state
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001584 * @current_active_state: The migration state we expect to be in
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001585 * @*old_vm_running: Pointer to old_vm_running flag
1586 * @*start_time: Pointer to time to update
1587 */
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001588static void migration_completion(MigrationState *s, int current_active_state,
1589 bool *old_vm_running,
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001590 int64_t *start_time)
1591{
1592 int ret;
1593
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001594 if (s->state == MIGRATION_STATUS_ACTIVE) {
1595 qemu_mutex_lock_iothread();
1596 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1597 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1598 *old_vm_running = runstate_is_running();
1599 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001600
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001601 if (!ret) {
1602 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
1603 if (ret >= 0) {
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001604 ret = bdrv_inactivate_all();
1605 }
1606 if (ret >= 0) {
zhanghailiang89a02a92016-01-15 11:37:42 +08001607 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
1608 qemu_savevm_state_complete_precopy(s->to_dst_file, false);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001609 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001610 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001611 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001612
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001613 if (ret < 0) {
1614 goto fail;
1615 }
1616 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1617 trace_migration_completion_postcopy_end();
1618
zhanghailiang89a02a92016-01-15 11:37:42 +08001619 qemu_savevm_state_complete_postcopy(s->to_dst_file);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001620 trace_migration_completion_postcopy_end_after_complete();
1621 }
1622
1623 /*
1624 * If rp was opened we must clean up the thread before
1625 * cleaning everything else up (since if there are no failures
1626 * it will wait for the destination to send it's status in
1627 * a SHUT command).
1628 * Postcopy opens rp if enabled (even if it's not avtivated)
1629 */
1630 if (migrate_postcopy_ram()) {
1631 int rp_error;
1632 trace_migration_completion_postcopy_end_before_rp();
1633 rp_error = await_return_path_close_on_source(s);
1634 trace_migration_completion_postcopy_end_after_rp(rp_error);
1635 if (rp_error) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02001636 goto fail_invalidate;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001637 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001638 }
1639
zhanghailiang89a02a92016-01-15 11:37:42 +08001640 if (qemu_file_get_error(s->to_dst_file)) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001641 trace_migration_completion_file_err();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001642 goto fail_invalidate;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001643 }
1644
zhanghailiang48781e52015-12-16 11:47:33 +00001645 migrate_set_state(&s->state, current_active_state,
1646 MIGRATION_STATUS_COMPLETED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001647 return;
1648
Greg Kurzfe904ea2016-05-18 15:44:36 +02001649fail_invalidate:
1650 /* If not doing postcopy, vm_start() will be called: let's regain
1651 * control on images.
1652 */
1653 if (s->state == MIGRATION_STATUS_ACTIVE) {
1654 Error *local_err = NULL;
1655
1656 bdrv_invalidate_cache_all(&local_err);
1657 if (local_err) {
1658 error_report_err(local_err);
1659 }
1660 }
1661
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001662fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001663 migrate_set_state(&s->state, current_active_state,
1664 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001665}
1666
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001667/*
1668 * Master migration thread on the source VM.
1669 * It drives the migration and pumps the data down the outgoing channel.
1670 */
Juan Quintela5f496a12013-02-22 17:36:30 +01001671static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001672{
Juan Quintela9848a402012-12-19 09:55:50 +01001673 MigrationState *s = opaque;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001674 /* Used by the bandwidth calcs, updated later */
Alex Blighbc72ad62013-08-21 16:03:08 +01001675 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1676 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001677 int64_t initial_bytes = 0;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001678 int64_t max_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001679 int64_t start_time = initial_time;
Liang Li94f5a432015-11-02 15:37:00 +08001680 int64_t end_time;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001681 bool old_vm_running = false;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001682 bool entered_postcopy = false;
1683 /* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */
1684 enum MigrationStatus current_active_state = MIGRATION_STATUS_ACTIVE;
Juan Quintela76f59332012-10-03 20:16:24 +02001685
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001686 rcu_register_thread();
1687
zhanghailiang89a02a92016-01-15 11:37:42 +08001688 qemu_savevm_state_header(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001689
1690 if (migrate_postcopy_ram()) {
1691 /* Now tell the dest that it should open its end so it can reply */
zhanghailiang89a02a92016-01-15 11:37:42 +08001692 qemu_savevm_send_open_return_path(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001693
1694 /* And do a ping that will make stuff easier to debug */
zhanghailiang89a02a92016-01-15 11:37:42 +08001695 qemu_savevm_send_ping(s->to_dst_file, 1);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001696
1697 /*
1698 * Tell the destination that we *might* want to do postcopy later;
1699 * if the other end can't do postcopy it should fail now, nice and
1700 * early.
1701 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001702 qemu_savevm_send_postcopy_advise(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001703 }
1704
zhanghailiang89a02a92016-01-15 11:37:42 +08001705 qemu_savevm_state_begin(s->to_dst_file, &s->params);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001706
Alex Blighbc72ad62013-08-21 16:03:08 +01001707 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001708 current_active_state = MIGRATION_STATUS_ACTIVE;
zhanghailiang48781e52015-12-16 11:47:33 +00001709 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1710 MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001711
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001712 trace_migration_thread_setup_complete();
1713
1714 while (s->state == MIGRATION_STATUS_ACTIVE ||
1715 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +01001716 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +02001717 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001718
zhanghailiang89a02a92016-01-15 11:37:42 +08001719 if (!qemu_file_rate_limit(s->to_dst_file)) {
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001720 uint64_t pend_post, pend_nonpost;
1721
zhanghailiang89a02a92016-01-15 11:37:42 +08001722 qemu_savevm_state_pending(s->to_dst_file, max_size, &pend_nonpost,
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001723 &pend_post);
1724 pending_size = pend_nonpost + pend_post;
1725 trace_migrate_pending(pending_size, max_size,
1726 pend_post, pend_nonpost);
Juan Quintelab22ff1f2012-10-17 21:06:31 +02001727 if (pending_size && pending_size >= max_size) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001728 /* Still a significant amount to transfer */
1729
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001730 if (migrate_postcopy_ram() &&
1731 s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE &&
1732 pend_nonpost <= max_size &&
1733 atomic_read(&s->start_postcopy)) {
1734
1735 if (!postcopy_start(s, &old_vm_running)) {
1736 current_active_state = MIGRATION_STATUS_POSTCOPY_ACTIVE;
1737 entered_postcopy = true;
1738 }
1739
1740 continue;
1741 }
1742 /* Just another iteration step */
zhanghailiang89a02a92016-01-15 11:37:42 +08001743 qemu_savevm_state_iterate(s->to_dst_file, entered_postcopy);
Juan Quintelac369f402012-10-03 20:33:34 +02001744 } else {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001745 trace_migration_thread_low_pending(pending_size);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001746 migration_completion(s, current_active_state,
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001747 &old_vm_running, &start_time);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001748 break;
Juan Quintelac369f402012-10-03 20:33:34 +02001749 }
1750 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001751
zhanghailiang89a02a92016-01-15 11:37:42 +08001752 if (qemu_file_get_error(s->to_dst_file)) {
zhanghailiang48781e52015-12-16 11:47:33 +00001753 migrate_set_state(&s->state, current_active_state,
1754 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001755 trace_migration_thread_file_err();
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01001756 break;
1757 }
Alex Blighbc72ad62013-08-21 16:03:08 +01001758 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001759 if (current_time >= initial_time + BUFFER_DELAY) {
zhanghailiang89a02a92016-01-15 11:37:42 +08001760 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file) -
1761 initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -05001762 uint64_t time_spent = current_time - initial_time;
Paolo Bonzinia694ee32015-01-26 12:12:27 +01001763 double bandwidth = (double)transferred_bytes / time_spent;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001764 max_size = bandwidth * migrate_max_downtime() / 1000000;
1765
Wei Yang5b648de2016-01-24 14:09:57 +00001766 s->mbps = (((double) transferred_bytes * 8.0) /
1767 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
Michael R. Hines7e114f82013-06-25 21:35:30 -04001768
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001769 trace_migrate_transferred(transferred_bytes, time_spent,
1770 bandwidth, max_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +01001771 /* if we haven't sent anything, we don't want to recalculate
1772 10000 is a small enough number for our purposes */
1773 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
1774 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
1775 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001776
zhanghailiang89a02a92016-01-15 11:37:42 +08001777 qemu_file_reset_rate_limit(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001778 initial_time = current_time;
zhanghailiang89a02a92016-01-15 11:37:42 +08001779 initial_bytes = qemu_ftell(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001780 }
zhanghailiang89a02a92016-01-15 11:37:42 +08001781 if (qemu_file_rate_limit(s->to_dst_file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001782 /* usleep expects microseconds */
1783 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
1784 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001785 }
1786
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001787 trace_migration_thread_after_loop();
Jason J. Herne070afca2015-09-08 13:12:35 -04001788 /* If we enabled cpu throttling for auto-converge, turn it off. */
1789 cpu_throttle_stop();
Liang Li94f5a432015-11-02 15:37:00 +08001790 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jason J. Herne070afca2015-09-08 13:12:35 -04001791
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001792 qemu_mutex_lock_iothread();
Liang Liea7415f2015-11-02 15:37:01 +08001793 qemu_savevm_state_cleanup();
zhanghailiang31194732015-03-13 16:08:38 +08001794 if (s->state == MIGRATION_STATUS_COMPLETED) {
zhanghailiang89a02a92016-01-15 11:37:42 +08001795 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001796 s->total_time = end_time - s->total_time;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001797 if (!entered_postcopy) {
1798 s->downtime = end_time - start_time;
1799 }
Peter Lievend6ed7312014-05-12 10:46:00 +02001800 if (s->total_time) {
1801 s->mbps = (((double) transferred_bytes * 8.0) /
1802 ((double) s->total_time)) / 1000;
1803 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001804 runstate_set(RUN_STATE_POSTMIGRATE);
1805 } else {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001806 if (old_vm_running && !entered_postcopy) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001807 vm_start();
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001808 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001809 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001810 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001811 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001812
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001813 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001814 return NULL;
1815}
1816
Juan Quintela9848a402012-12-19 09:55:50 +01001817void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001818{
Juan Quintelacc283e32013-02-01 11:12:26 +01001819 /* This is a best 1st approximation. ns to ms */
1820 s->expected_downtime = max_downtime/1000000;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001821 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001822
Daniel P. Berrange9e4d2b92016-04-27 11:04:57 +01001823 qemu_file_set_blocking(s->to_dst_file, true);
zhanghailiang89a02a92016-01-15 11:37:42 +08001824 qemu_file_set_rate_limit(s->to_dst_file,
Paolo Bonzini442773c2013-02-22 17:36:44 +01001825 s->bandwidth_limit / XFER_LIMIT_RATIO);
1826
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02001827 /* Notify before starting migration thread */
1828 notifier_list_notify(&migration_state_notifiers, s);
1829
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001830 /*
1831 * Open the return path; currently for postcopy but other things might
1832 * also want it.
1833 */
1834 if (migrate_postcopy_ram()) {
1835 if (open_return_path_on_source(s)) {
1836 error_report("Unable to open return-path for postcopy");
zhanghailiang48781e52015-12-16 11:47:33 +00001837 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001838 MIGRATION_STATUS_FAILED);
1839 migrate_fd_cleanup(s);
1840 return;
1841 }
1842 }
1843
Liang Li8706d2d2015-03-23 16:32:17 +08001844 migrate_compress_threads_create();
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001845 qemu_thread_create(&s->thread, "migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001846 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001847 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001848}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001849
1850PostcopyState postcopy_state_get(void)
1851{
1852 return atomic_mb_read(&incoming_postcopy_state);
1853}
1854
1855/* Set the state and return the old state */
1856PostcopyState postcopy_state_set(PostcopyState new_state)
1857{
1858 return atomic_xchg(&incoming_postcopy_state, new_state);
1859}
1860