blob: db3d2dd43eb79833305cfbf0909090093f9fbafa [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
16#include "qemu-common.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010017#include "qemu/error-report.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010018#include "qemu/main-loop.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010019#include "migration/migration.h"
Juan Quintela0d82d0e2012-10-03 14:18:33 +020020#include "migration/qemu-file.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010021#include "sysemu/sysemu.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010022#include "block/block.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010023#include "qapi/qmp/qerror.h"
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000024#include "qapi/util.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010025#include "qemu/sockets.h"
Paolo Bonziniab28bd22015-07-09 08:55:38 +020026#include "qemu/rcu.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010027#include "migration/block.h"
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +000028#include "migration/postcopy-ram.h"
Juan Quintela766bd172012-07-23 05:45:29 +020029#include "qemu/thread.h"
Luiz Capitulino791e7c82011-09-13 17:37:16 -030030#include "qmp-commands.h"
Kazuya Saitoc09e5bb2013-02-22 17:36:19 +010031#include "trace.h"
Juan Quintela598cd2b2015-05-20 12:16:15 +020032#include "qapi-event.h"
Jason J. Herne070afca2015-09-08 13:12:35 -040033#include "qom/cpu.h"
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000034#include "exec/memory.h"
35#include "exec/address-spaces.h"
aliguori065e2812008-11-11 16:46:33 +000036
Jason J. Hernedc325622015-09-08 13:12:37 -040037#define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000038
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010039/* Amount of time to allocate to each "chunk" of bandwidth-throttled
40 * data. */
41#define BUFFER_DELAY 100
42#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
43
Liang Li8706d2d2015-03-23 16:32:17 +080044/* Default compression thread count */
45#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080046/* Default decompression thread count, usually decompression is at
47 * least 4 times as fast as compression.*/
48#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080049/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
50#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
Jason J. Herne1626fee2015-09-08 13:12:34 -040051/* Define default autoconverge cpu throttle migration parameters */
52#define DEFAULT_MIGRATE_X_CPU_THROTTLE_INITIAL 20
53#define DEFAULT_MIGRATE_X_CPU_THROTTLE_INCREMENT 10
Liang Li8706d2d2015-03-23 16:32:17 +080054
Orit Wasserman17ad9b32012-08-06 21:42:53 +030055/* Migration XBZRLE default cache size */
56#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
57
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010058static NotifierList migration_state_notifiers =
59 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
60
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000061static bool deferred_incoming;
62
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +000063/*
64 * Current state of incoming postcopy; note this is not part of
65 * MigrationIncomingState since it's state is used during cleanup
66 * at the end as MIS is being freed.
67 */
68static PostcopyState incoming_postcopy_state;
69
Juan Quintela17549e82011-10-05 13:50:43 +020070/* When we add fault tolerance, we could have several
71 migrations at once. For now we don't need to add
72 dynamic creation of migration */
73
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010074/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +020075MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +020076{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000077 static bool once;
Juan Quintela17549e82011-10-05 13:50:43 +020078 static MigrationState current_migration = {
zhanghailiang31194732015-03-13 16:08:38 +080079 .state = MIGRATION_STATUS_NONE,
Juan Quintelad0ae46c2011-02-23 00:33:19 +010080 .bandwidth_limit = MAX_THROTTLE,
Orit Wasserman17ad9b32012-08-06 21:42:53 +030081 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
Michael R. Hines7e114f82013-06-25 21:35:30 -040082 .mbps = -1,
Liang Li43c60a82015-03-23 16:32:27 +080083 .parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] =
84 DEFAULT_MIGRATE_COMPRESS_LEVEL,
85 .parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
86 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
87 .parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
88 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
Jason J. Herne1626fee2015-09-08 13:12:34 -040089 .parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
90 DEFAULT_MIGRATE_X_CPU_THROTTLE_INITIAL,
91 .parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
92 DEFAULT_MIGRATE_X_CPU_THROTTLE_INCREMENT,
Juan Quintela17549e82011-10-05 13:50:43 +020093 };
94
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000095 if (!once) {
96 qemu_mutex_init(&current_migration.src_page_req_mutex);
97 once = true;
98 }
Juan Quintela17549e82011-10-05 13:50:43 +020099 return &current_migration;
100}
101
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100102/* For incoming */
103static MigrationIncomingState *mis_current;
104
105MigrationIncomingState *migration_incoming_get_current(void)
106{
107 return mis_current;
108}
109
110MigrationIncomingState *migration_incoming_state_new(QEMUFile* f)
111{
Markus Armbruster97f3ad32015-09-14 13:51:31 +0200112 mis_current = g_new0(MigrationIncomingState, 1);
Dr. David Alan Gilbert42e2aa52015-11-05 18:10:34 +0000113 mis_current->from_src_file = f;
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +0100114 QLIST_INIT(&mis_current->loadvm_handlers);
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000115 qemu_mutex_init(&mis_current->rp_mutex);
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +0000116 qemu_event_init(&mis_current->main_thread_load_event, false);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100117
118 return mis_current;
119}
120
121void migration_incoming_state_destroy(void)
122{
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +0000123 qemu_event_destroy(&mis_current->main_thread_load_event);
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +0100124 loadvm_free_handlers(mis_current);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100125 g_free(mis_current);
126 mis_current = NULL;
127}
128
Juan Quinteladf4b1022014-10-08 10:58:10 +0200129
130typedef struct {
Juan Quintela13d16812014-10-08 13:58:24 +0200131 bool optional;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200132 uint32_t size;
133 uint8_t runstate[100];
Juan Quintela172c4352015-07-08 13:56:26 +0200134 RunState state;
135 bool received;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200136} GlobalState;
137
138static GlobalState global_state;
139
Juan Quintela560d0272015-07-15 09:53:46 +0200140int global_state_store(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200141{
142 if (!runstate_store((char *)global_state.runstate,
143 sizeof(global_state.runstate))) {
144 error_report("runstate name too big: %s", global_state.runstate);
145 trace_migrate_state_too_big();
146 return -EINVAL;
147 }
148 return 0;
149}
150
Anthony PERARDc69adea2015-08-03 15:29:19 +0100151void global_state_store_running(void)
152{
153 const char *state = RunState_lookup[RUN_STATE_RUNNING];
154 strncpy((char *)global_state.runstate,
155 state, sizeof(global_state.runstate));
156}
157
Juan Quintela172c4352015-07-08 13:56:26 +0200158static bool global_state_received(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200159{
Juan Quintela172c4352015-07-08 13:56:26 +0200160 return global_state.received;
161}
162
163static RunState global_state_get_runstate(void)
164{
165 return global_state.state;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200166}
167
Juan Quintela13d16812014-10-08 13:58:24 +0200168void global_state_set_optional(void)
169{
170 global_state.optional = true;
171}
172
173static bool global_state_needed(void *opaque)
174{
175 GlobalState *s = opaque;
176 char *runstate = (char *)s->runstate;
177
178 /* If it is not optional, it is mandatory */
179
180 if (s->optional == false) {
181 return true;
182 }
183
184 /* If state is running or paused, it is not needed */
185
186 if (strcmp(runstate, "running") == 0 ||
187 strcmp(runstate, "paused") == 0) {
188 return false;
189 }
190
191 /* for any other state it is needed */
192 return true;
193}
194
Juan Quinteladf4b1022014-10-08 10:58:10 +0200195static int global_state_post_load(void *opaque, int version_id)
196{
197 GlobalState *s = opaque;
Juan Quintela172c4352015-07-08 13:56:26 +0200198 Error *local_err = NULL;
199 int r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200200 char *runstate = (char *)s->runstate;
201
Juan Quintela172c4352015-07-08 13:56:26 +0200202 s->received = true;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200203 trace_migrate_global_state_post_load(runstate);
204
Juan Quintela172c4352015-07-08 13:56:26 +0200205 r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE_MAX,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200206 -1, &local_err);
207
Juan Quintela172c4352015-07-08 13:56:26 +0200208 if (r == -1) {
209 if (local_err) {
210 error_report_err(local_err);
Juan Quinteladf4b1022014-10-08 10:58:10 +0200211 }
Juan Quintela172c4352015-07-08 13:56:26 +0200212 return -EINVAL;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200213 }
Juan Quintela172c4352015-07-08 13:56:26 +0200214 s->state = r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200215
Juan Quintela172c4352015-07-08 13:56:26 +0200216 return 0;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200217}
218
219static void global_state_pre_save(void *opaque)
220{
221 GlobalState *s = opaque;
222
223 trace_migrate_global_state_pre_save((char *)s->runstate);
224 s->size = strlen((char *)s->runstate) + 1;
225}
226
227static const VMStateDescription vmstate_globalstate = {
228 .name = "globalstate",
229 .version_id = 1,
230 .minimum_version_id = 1,
231 .post_load = global_state_post_load,
232 .pre_save = global_state_pre_save,
Juan Quintela13d16812014-10-08 13:58:24 +0200233 .needed = global_state_needed,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200234 .fields = (VMStateField[]) {
235 VMSTATE_UINT32(size, GlobalState),
236 VMSTATE_BUFFER(runstate, GlobalState),
237 VMSTATE_END_OF_LIST()
238 },
239};
240
241void register_global_state(void)
242{
243 /* We would use it independently that we receive it */
244 strcpy((char *)&global_state.runstate, "");
Juan Quintela172c4352015-07-08 13:56:26 +0200245 global_state.received = false;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200246 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
247}
248
Juan Quintelab05dc722015-07-07 14:44:05 +0200249static void migrate_generate_event(int new_state)
250{
251 if (migrate_use_events()) {
252 qapi_event_send_migration(new_state, &error_abort);
Juan Quintelab05dc722015-07-07 14:44:05 +0200253 }
254}
255
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000256/*
257 * Called on -incoming with a defer: uri.
258 * The migration can be started later after any parameters have been
259 * changed.
260 */
261static void deferred_incoming_migration(Error **errp)
262{
263 if (deferred_incoming) {
264 error_setg(errp, "Incoming migration already deferred");
265 }
266 deferred_incoming = true;
267}
268
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000269/* Request a range of pages from the source VM at the given
270 * start address.
271 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
272 * as the last request (a name must have been given previously)
273 * Start: Address offset within the RB
274 * Len: Length in bytes required - must be a multiple of pagesize
275 */
276void migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
277 ram_addr_t start, size_t len)
278{
279 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname upto 256 */
280 size_t msglen = 12; /* start + len */
281
282 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
283 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
284
285 if (rbname) {
286 int rbname_len = strlen(rbname);
287 assert(rbname_len < 256);
288
289 bufc[msglen++] = rbname_len;
290 memcpy(bufc + msglen, rbname, rbname_len);
291 msglen += rbname_len;
292 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES_ID, msglen, bufc);
293 } else {
294 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES, msglen, bufc);
295 }
296}
297
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200298void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000299{
aliguori34c9dd82008-10-13 03:14:31 +0000300 const char *p;
301
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200302 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000303 if (!strcmp(uri, "defer")) {
304 deferred_incoming_migration(errp);
305 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200306 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400307#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000308 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400309 rdma_start_incoming_migration(p, errp);
310#endif
aliguori065e2812008-11-11 16:46:33 +0000311#if !defined(WIN32)
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000312 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200313 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000314 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200315 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000316 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200317 fd_start_incoming_migration(p, errp);
aliguori065e2812008-11-11 16:46:33 +0000318#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000319 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100320 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200321 }
aliguori5bb79102008-10-13 03:12:02 +0000322}
323
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200324static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200325{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200326 QEMUFile *f = opaque;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100327 Error *local_err = NULL;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200328 int ret;
329
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100330 migration_incoming_state_new(f);
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000331 postcopy_state_set(POSTCOPY_INCOMING_NONE);
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200332 migrate_generate_event(MIGRATION_STATUS_ACTIVE);
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200333 ret = qemu_loadvm_state(f);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100334
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200335 qemu_fclose(f);
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200336 free_xbzrle_decoded_buf();
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100337 migration_incoming_state_destroy();
338
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200339 if (ret < 0) {
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200340 migrate_generate_event(MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200341 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800342 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600343 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200344 }
Juan Quintela511c0232010-06-09 14:10:55 +0200345
Anthony Liguori0f154232011-11-14 15:09:45 -0600346 /* Make sure all file formats flush their mutable metadata */
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100347 bdrv_invalidate_cache_all(&local_err);
348 if (local_err) {
Dr. David Alan Gilberted1f3e02015-10-13 12:21:27 +0100349 migrate_generate_event(MIGRATION_STATUS_FAILED);
Markus Armbruster97baf9d2015-02-18 19:21:52 +0100350 error_report_err(local_err);
Liang Li3fcb38c2015-03-23 16:32:18 +0800351 migrate_decompress_threads_join();
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100352 exit(EXIT_FAILURE);
353 }
Anthony Liguori0f154232011-11-14 15:09:45 -0600354
Amit Shah92e37622015-10-14 17:37:19 +0530355 /*
356 * This must happen after all error conditions are dealt with and
357 * we're sure the VM is going to be running on this host.
358 */
359 qemu_announce_self();
360
Juan Quintela172c4352015-07-08 13:56:26 +0200361 /* If global state section was not received or we are in running
362 state, we need to obey autostart. Any other state is set with
363 runstate_set. */
Juan Quinteladf4b1022014-10-08 10:58:10 +0200364
Juan Quintela172c4352015-07-08 13:56:26 +0200365 if (!global_state_received() ||
366 global_state_get_runstate() == RUN_STATE_RUNNING) {
Juan Quinteladf4b1022014-10-08 10:58:10 +0200367 if (autostart) {
368 vm_start();
369 } else {
370 runstate_set(RUN_STATE_PAUSED);
371 }
Juan Quintela172c4352015-07-08 13:56:26 +0200372 } else {
373 runstate_set(global_state_get_runstate());
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300374 }
Liang Li3fcb38c2015-03-23 16:32:18 +0800375 migrate_decompress_threads_join();
Dr. David Alan Gilberted1f3e02015-10-13 12:21:27 +0100376 /*
377 * This must happen after any state changes since as soon as an external
378 * observer sees this event they might start to prod at the VM assuming
379 * it's ready to use.
380 */
381 migrate_generate_event(MIGRATION_STATUS_COMPLETED);
Juan Quintela511c0232010-06-09 14:10:55 +0200382}
383
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200384void process_incoming_migration(QEMUFile *f)
385{
386 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
387 int fd = qemu_get_fd(f);
388
389 assert(fd != -1);
Liang Li3fcb38c2015-03-23 16:32:18 +0800390 migrate_decompress_threads_create();
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100391 qemu_set_nonblock(fd);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200392 qemu_coroutine_enter(co, f);
393}
394
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000395/*
396 * Send a message on the return channel back to the source
397 * of the migration.
398 */
399void migrate_send_rp_message(MigrationIncomingState *mis,
400 enum mig_rp_message_type message_type,
401 uint16_t len, void *data)
402{
403 trace_migrate_send_rp_message((int)message_type, len);
404 qemu_mutex_lock(&mis->rp_mutex);
405 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
406 qemu_put_be16(mis->to_src_file, len);
407 qemu_put_buffer(mis->to_src_file, data, len);
408 qemu_fflush(mis->to_src_file);
409 qemu_mutex_unlock(&mis->rp_mutex);
410}
411
412/*
413 * Send a 'SHUT' message on the return channel with the given value
414 * to indicate that we've finished with the RP. Non-0 value indicates
415 * error.
416 */
417void migrate_send_rp_shut(MigrationIncomingState *mis,
418 uint32_t value)
419{
420 uint32_t buf;
421
422 buf = cpu_to_be32(value);
423 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
424}
425
426/*
427 * Send a 'PONG' message on the return channel with the given value
428 * (normally in response to a 'PING')
429 */
430void migrate_send_rp_pong(MigrationIncomingState *mis,
431 uint32_t value)
432{
433 uint32_t buf;
434
435 buf = cpu_to_be32(value);
436 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
437}
438
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400439/* amount of nanoseconds we are willing to wait for migration to be down.
440 * the choice of nanoseconds is because it is the maximum resolution that
441 * get_clock() can achieve. It is an internal measure. All user-visible
442 * units must be in seconds */
Alexey Kardashevskiyf7cd55a2014-03-27 14:57:26 +1100443static uint64_t max_downtime = 300000000;
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400444
445uint64_t migrate_max_downtime(void)
446{
447 return max_downtime;
448}
449
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300450MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
451{
452 MigrationCapabilityStatusList *head = NULL;
453 MigrationCapabilityStatusList *caps;
454 MigrationState *s = migrate_get_current();
455 int i;
456
Michael Tokarev387eede2013-10-05 13:18:28 +0400457 caps = NULL; /* silence compiler warning */
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300458 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
459 if (head == NULL) {
460 head = g_malloc0(sizeof(*caps));
461 caps = head;
462 } else {
463 caps->next = g_malloc0(sizeof(*caps));
464 caps = caps->next;
465 }
466 caps->value =
467 g_malloc(sizeof(*caps->value));
468 caps->value->capability = i;
469 caps->value->state = s->enabled_capabilities[i];
470 }
471
472 return head;
473}
474
Liang Li85de8322015-03-23 16:32:28 +0800475MigrationParameters *qmp_query_migrate_parameters(Error **errp)
476{
477 MigrationParameters *params;
478 MigrationState *s = migrate_get_current();
479
480 params = g_malloc0(sizeof(*params));
481 params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
482 params->compress_threads =
483 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
484 params->decompress_threads =
485 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Jason J. Herne1626fee2015-09-08 13:12:34 -0400486 params->x_cpu_throttle_initial =
487 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL];
488 params->x_cpu_throttle_increment =
489 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT];
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
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300512static void get_xbzrle_cache_stats(MigrationInfo *info)
513{
514 if (migrate_use_xbzrle()) {
515 info->has_xbzrle_cache = true;
516 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
517 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
518 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
519 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
520 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
ChenLiang8bc39232014-04-04 17:57:56 +0800521 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300522 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
523 }
524}
525
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300526MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000527{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300528 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200529 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000530
Juan Quintela17549e82011-10-05 13:50:43 +0200531 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800532 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200533 /* no migration has happened ever */
534 break;
zhanghailiang31194732015-03-13 16:08:38 +0800535 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400536 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400537 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400538 break;
zhanghailiang31194732015-03-13 16:08:38 +0800539 case MIGRATION_STATUS_ACTIVE:
540 case MIGRATION_STATUS_CANCELLING:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300541 info->has_status = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200542 info->has_total_time = true;
Alex Blighbc72ad62013-08-21 16:03:08 +0100543 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Juan Quintela7aa939a2012-08-18 13:17:10 +0200544 - s->total_time;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200545 info->has_expected_downtime = true;
546 info->expected_downtime = s->expected_downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400547 info->has_setup_time = true;
548 info->setup_time = s->setup_time;
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200549
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300550 info->has_ram = true;
551 info->ram = g_malloc0(sizeof(*info->ram));
552 info->ram->transferred = ram_bytes_transferred();
553 info->ram->remaining = ram_bytes_remaining();
554 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300555 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100556 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300557 info->ram->normal = norm_mig_pages_transferred();
558 info->ram->normal_bytes = norm_mig_bytes_transferred();
Juan Quintela8d017192012-08-13 12:31:25 +0200559 info->ram->dirty_pages_rate = s->dirty_pages_rate;
Michael R. Hines7e114f82013-06-25 21:35:30 -0400560 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800561 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela8d017192012-08-13 12:31:25 +0200562
Juan Quintela17549e82011-10-05 13:50:43 +0200563 if (blk_mig_active()) {
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300564 info->has_disk = true;
565 info->disk = g_malloc0(sizeof(*info->disk));
566 info->disk->transferred = blk_mig_bytes_transferred();
567 info->disk->remaining = blk_mig_bytes_remaining();
568 info->disk->total = blk_mig_bytes_total();
aliguoriff8d81d2008-10-24 22:10:31 +0000569 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300570
Jason J. Herne47828932015-09-08 13:12:36 -0400571 if (cpu_throttle_active()) {
572 info->has_x_cpu_throttle_percentage = true;
573 info->x_cpu_throttle_percentage = cpu_throttle_get_percentage();
574 }
575
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300576 get_xbzrle_cache_stats(info);
Juan Quintela17549e82011-10-05 13:50:43 +0200577 break;
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000578 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
579 /* Mostly the same as active; TODO add some postcopy stats */
580 info->has_status = true;
581 info->has_total_time = true;
582 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
583 - s->total_time;
584 info->has_expected_downtime = true;
585 info->expected_downtime = s->expected_downtime;
586 info->has_setup_time = true;
587 info->setup_time = s->setup_time;
588
589 info->has_ram = true;
590 info->ram = g_malloc0(sizeof(*info->ram));
591 info->ram->transferred = ram_bytes_transferred();
592 info->ram->remaining = ram_bytes_remaining();
593 info->ram->total = ram_bytes_total();
594 info->ram->duplicate = dup_mig_pages_transferred();
595 info->ram->skipped = skipped_mig_pages_transferred();
596 info->ram->normal = norm_mig_pages_transferred();
597 info->ram->normal_bytes = norm_mig_bytes_transferred();
598 info->ram->dirty_pages_rate = s->dirty_pages_rate;
599 info->ram->mbps = s->mbps;
600
601 if (blk_mig_active()) {
602 info->has_disk = true;
603 info->disk = g_malloc0(sizeof(*info->disk));
604 info->disk->transferred = blk_mig_bytes_transferred();
605 info->disk->remaining = blk_mig_bytes_remaining();
606 info->disk->total = blk_mig_bytes_total();
607 }
608
609 get_xbzrle_cache_stats(info);
610 break;
zhanghailiang31194732015-03-13 16:08:38 +0800611 case MIGRATION_STATUS_COMPLETED:
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300612 get_xbzrle_cache_stats(info);
613
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300614 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900615 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200616 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200617 info->has_downtime = true;
618 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400619 info->has_setup_time = true;
620 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200621
622 info->has_ram = true;
623 info->ram = g_malloc0(sizeof(*info->ram));
624 info->ram->transferred = ram_bytes_transferred();
625 info->ram->remaining = 0;
626 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300627 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100628 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300629 info->ram->normal = norm_mig_pages_transferred();
630 info->ram->normal_bytes = norm_mig_bytes_transferred();
Michael R. Hines7e114f82013-06-25 21:35:30 -0400631 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800632 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela17549e82011-10-05 13:50:43 +0200633 break;
zhanghailiang31194732015-03-13 16:08:38 +0800634 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300635 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200636 break;
zhanghailiang31194732015-03-13 16:08:38 +0800637 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300638 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200639 break;
aliguori5bb79102008-10-13 03:12:02 +0000640 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800641 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300642
643 return info;
aliguori5bb79102008-10-13 03:12:02 +0000644}
645
Orit Wasserman00458432012-08-06 21:42:48 +0300646void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
647 Error **errp)
648{
649 MigrationState *s = migrate_get_current();
650 MigrationCapabilityStatusList *cap;
651
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000652 if (migration_is_setup_or_active(s->state)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100653 error_setg(errp, QERR_MIGRATION_ACTIVE);
Orit Wasserman00458432012-08-06 21:42:48 +0300654 return;
655 }
656
657 for (cap = params; cap; cap = cap->next) {
658 s->enabled_capabilities[cap->value->capability] = cap->value->state;
659 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000660
661 if (migrate_postcopy_ram()) {
662 if (migrate_use_compression()) {
663 /* The decompression threads asynchronously write into RAM
664 * rather than use the atomic copies needed to avoid
665 * userfaulting. It should be possible to fix the decompression
666 * threads for compatibility in future.
667 */
668 error_report("Postcopy is not currently compatible with "
669 "compression");
670 s->enabled_capabilities[MIGRATION_CAPABILITY_X_POSTCOPY_RAM] =
671 false;
672 }
673 }
Orit Wasserman00458432012-08-06 21:42:48 +0300674}
675
Liang Li85de8322015-03-23 16:32:28 +0800676void qmp_migrate_set_parameters(bool has_compress_level,
677 int64_t compress_level,
678 bool has_compress_threads,
679 int64_t compress_threads,
680 bool has_decompress_threads,
Jason J. Herne1626fee2015-09-08 13:12:34 -0400681 int64_t decompress_threads,
682 bool has_x_cpu_throttle_initial,
683 int64_t x_cpu_throttle_initial,
684 bool has_x_cpu_throttle_increment,
685 int64_t x_cpu_throttle_increment, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +0800686{
687 MigrationState *s = migrate_get_current();
688
689 if (has_compress_level && (compress_level < 0 || compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100690 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
691 "is invalid, it should be in the range of 0 to 9");
Liang Li85de8322015-03-23 16:32:28 +0800692 return;
693 }
694 if (has_compress_threads &&
695 (compress_threads < 1 || compress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100696 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
697 "compress_threads",
698 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800699 return;
700 }
701 if (has_decompress_threads &&
702 (decompress_threads < 1 || decompress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100703 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
704 "decompress_threads",
705 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800706 return;
707 }
Jason J. Herne1626fee2015-09-08 13:12:34 -0400708 if (has_x_cpu_throttle_initial &&
709 (x_cpu_throttle_initial < 1 || x_cpu_throttle_initial > 99)) {
710 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
711 "x_cpu_throttle_initial",
712 "an integer in the range of 1 to 99");
713 }
714 if (has_x_cpu_throttle_increment &&
715 (x_cpu_throttle_increment < 1 || x_cpu_throttle_increment > 99)) {
716 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
717 "x_cpu_throttle_increment",
718 "an integer in the range of 1 to 99");
719 }
Liang Li85de8322015-03-23 16:32:28 +0800720
721 if (has_compress_level) {
722 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
723 }
724 if (has_compress_threads) {
725 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads;
726 }
727 if (has_decompress_threads) {
728 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
729 decompress_threads;
730 }
Jason J. Herne1626fee2015-09-08 13:12:34 -0400731 if (has_x_cpu_throttle_initial) {
732 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
733 x_cpu_throttle_initial;
734 }
735
736 if (has_x_cpu_throttle_increment) {
737 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
738 x_cpu_throttle_increment;
739 }
Liang Li85de8322015-03-23 16:32:28 +0800740}
741
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000742void qmp_migrate_start_postcopy(Error **errp)
743{
744 MigrationState *s = migrate_get_current();
745
746 if (!migrate_postcopy_ram()) {
747 error_setg(errp, "Enable postcopy with migration_set_capability before"
748 " the start of migration");
749 return;
750 }
751
752 if (s->state == MIGRATION_STATUS_NONE) {
753 error_setg(errp, "Postcopy must be started after migration has been"
754 " started");
755 return;
756 }
757 /*
758 * we don't error if migration has finished since that would be racy
759 * with issuing this command.
760 */
761 atomic_set(&s->start_postcopy, true);
762}
763
aliguori065e2812008-11-11 16:46:33 +0000764/* shared migration helpers */
765
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000766static void migrate_set_state(MigrationState *s, int old_state, int new_state)
767{
Juan Quintelaa5c17b52015-06-17 02:06:20 +0200768 if (atomic_cmpxchg(&s->state, old_state, new_state) == old_state) {
Juan Quintela4ba4bc52015-07-08 13:58:27 +0200769 trace_migrate_set_state(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200770 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000771 }
772}
773
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100774static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000775{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100776 MigrationState *s = opaque;
777
778 qemu_bh_delete(s->cleanup_bh);
779 s->cleanup_bh = NULL;
780
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +0000781 flush_page_queue(s);
782
aliguori065e2812008-11-11 16:46:33 +0000783 if (s->file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100784 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100785 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +0000786 if (s->migration_thread_running) {
787 qemu_thread_join(&s->thread);
788 s->migration_thread_running = false;
789 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100790 qemu_mutex_lock_iothread();
791
Liang Li8706d2d2015-03-23 16:32:17 +0800792 migrate_compress_threads_join();
Paolo Bonzini6f190a02013-02-22 17:36:48 +0100793 qemu_fclose(s->file);
794 s->file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000795 }
796
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000797 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
798 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100799
Liang Li94f5a432015-11-02 15:37:00 +0800800 if (s->state == MIGRATION_STATUS_CANCELLING) {
801 migrate_set_state(s, MIGRATION_STATUS_CANCELLING,
802 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100803 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100804
805 notifier_list_notify(&migration_state_notifiers, s);
aliguori065e2812008-11-11 16:46:33 +0000806}
807
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200808void migrate_fd_error(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000809{
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100810 trace_migrate_fd_error();
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100811 assert(s->file == NULL);
Juan Quintela78443372015-06-17 01:36:40 +0200812 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100813 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela458cf282011-02-22 23:32:54 +0100814}
815
Juan Quintela0edda1c2010-05-11 16:28:39 +0200816static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000817{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000818 int old_state ;
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000819 QEMUFile *f = migrate_get_current()->file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100820 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000821
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +0000822 if (s->rp_state.from_dst_file) {
823 /* shutdown the rp socket, so causing the rp thread to shutdown */
824 qemu_file_shutdown(s->rp_state.from_dst_file);
825 }
826
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000827 do {
828 old_state = s->state;
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000829 if (!migration_is_setup_or_active(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000830 break;
831 }
zhanghailiang31194732015-03-13 16:08:38 +0800832 migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
833 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000834
835 /*
836 * If we're unlucky the migration code might be stuck somewhere in a
837 * send/write while the network has failed and is waiting to timeout;
838 * if we've got shutdown(2) available then we can force it to quit.
839 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
840 * called in a bh, so there is no race against this cancel.
841 */
zhanghailiang31194732015-03-13 16:08:38 +0800842 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000843 qemu_file_shutdown(f);
844 }
aliguori065e2812008-11-11 16:46:33 +0000845}
846
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100847void add_migration_state_change_notifier(Notifier *notify)
848{
849 notifier_list_add(&migration_state_notifiers, notify);
850}
851
852void remove_migration_state_change_notifier(Notifier *notify)
853{
Paolo Bonzini31552522012-01-13 17:34:01 +0100854 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100855}
856
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +0200857bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200858{
zhanghailiang31194732015-03-13 16:08:38 +0800859 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200860}
861
Juan Quintela70736932011-02-23 00:43:59 +0100862bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100863{
zhanghailiang31194732015-03-13 16:08:38 +0800864 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100865}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200866
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200867bool migration_has_failed(MigrationState *s)
868{
zhanghailiang31194732015-03-13 16:08:38 +0800869 return (s->state == MIGRATION_STATUS_CANCELLED ||
870 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200871}
872
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000873bool migration_in_postcopy(MigrationState *s)
874{
875 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
876}
877
Dr. David Alan Gilbertaefeb182015-11-05 18:10:40 +0000878MigrationState *migrate_init(const MigrationParams *params)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200879{
Juan Quintela17549e82011-10-05 13:50:43 +0200880 MigrationState *s = migrate_get_current();
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100881 int64_t bandwidth_limit = s->bandwidth_limit;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300882 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300883 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
Liang Li43c60a82015-03-23 16:32:27 +0800884 int compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
885 int compress_thread_count =
886 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
887 int decompress_thread_count =
888 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Jason J. Herne1626fee2015-09-08 13:12:34 -0400889 int x_cpu_throttle_initial =
890 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL];
891 int x_cpu_throttle_increment =
892 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT];
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300893
894 memcpy(enabled_capabilities, s->enabled_capabilities,
895 sizeof(enabled_capabilities));
Juan Quintela0edda1c2010-05-11 16:28:39 +0200896
Juan Quintela17549e82011-10-05 13:50:43 +0200897 memset(s, 0, sizeof(*s));
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300898 s->params = *params;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300899 memcpy(s->enabled_capabilities, enabled_capabilities,
900 sizeof(enabled_capabilities));
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300901 s->xbzrle_cache_size = xbzrle_cache_size;
Juan Quintela1299c632011-11-09 21:29:01 +0100902
Liang Li43c60a82015-03-23 16:32:27 +0800903 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
904 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
905 compress_thread_count;
906 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
907 decompress_thread_count;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400908 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
909 x_cpu_throttle_initial;
910 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
911 x_cpu_throttle_increment;
Juan Quintela0edda1c2010-05-11 16:28:39 +0200912 s->bandwidth_limit = bandwidth_limit;
Juan Quintela78443372015-06-17 01:36:40 +0200913 migrate_set_state(s, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200914
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +0000915 QSIMPLEQ_INIT(&s->src_page_requests);
916
Alex Blighbc72ad62013-08-21 16:03:08 +0100917 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200918 return s;
919}
Juan Quintelacab30142011-02-22 23:54:21 +0100920
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600921static GSList *migration_blockers;
922
923void migrate_add_blocker(Error *reason)
924{
925 migration_blockers = g_slist_prepend(migration_blockers, reason);
926}
927
928void migrate_del_blocker(Error *reason)
929{
930 migration_blockers = g_slist_remove(migration_blockers, reason);
931}
932
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000933void qmp_migrate_incoming(const char *uri, Error **errp)
934{
935 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000936 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000937
938 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000939 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000940 return;
941 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000942 if (!once) {
943 error_setg(errp, "The incoming migration has already been started");
944 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000945
946 qemu_start_incoming_migration(uri, &local_err);
947
948 if (local_err) {
949 error_propagate(errp, local_err);
950 return;
951 }
952
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000953 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000954}
955
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200956void qmp_migrate(const char *uri, bool has_blk, bool blk,
957 bool has_inc, bool inc, bool has_detach, bool detach,
958 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100959{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +0200960 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +0200961 MigrationState *s = migrate_get_current();
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300962 MigrationParams params;
Juan Quintelacab30142011-02-22 23:54:21 +0100963 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +0100964
Pawit Pornkitprasan8c0426a2013-07-30 08:39:52 +0900965 params.blk = has_blk && blk;
966 params.shared = has_inc && inc;
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300967
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000968 if (migration_is_setup_or_active(s->state) ||
zhanghailiang31194732015-03-13 16:08:38 +0800969 s->state == MIGRATION_STATUS_CANCELLING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100970 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200971 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100972 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +0100973 if (runstate_check(RUN_STATE_INMIGRATE)) {
974 error_setg(errp, "Guest is waiting for an incoming migration");
975 return;
976 }
977
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200978 if (qemu_savevm_state_blocked(errp)) {
979 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100980 }
981
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600982 if (migration_blockers) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200983 *errp = error_copy(migration_blockers->data);
984 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600985 }
986
Juan Quintela656a2332015-07-01 09:32:29 +0200987 /* We are starting a new migration, so we want to start in a clean
988 state. This change is only needed if previous migration
989 failed/was cancelled. We don't use migrate_set_state() because
990 we are setting the initial state, not changing it. */
991 s->state = MIGRATION_STATUS_NONE;
992
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300993 s = migrate_init(&params);
Juan Quintelacab30142011-02-22 23:54:21 +0100994
995 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200996 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400997#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +0800998 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400999 rdma_start_outgoing_migration(s, p, &local_err);
1000#endif
Juan Quintelacab30142011-02-22 23:54:21 +01001001#if !defined(WIN32)
1002 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001003 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001004 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001005 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001006 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001007 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001008#endif
1009 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001010 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1011 "a valid migration protocol");
Juan Quintela78443372015-06-17 01:36:40 +02001012 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001013 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001014 }
1015
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001016 if (local_err) {
Paolo Bonzini342ab8d2012-10-02 09:59:38 +02001017 migrate_fd_error(s);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001018 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001019 return;
Juan Quintela1299c632011-11-09 21:29:01 +01001020 }
Juan Quintelacab30142011-02-22 23:54:21 +01001021}
1022
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02001023void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001024{
Juan Quintela17549e82011-10-05 13:50:43 +02001025 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01001026}
1027
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001028void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1029{
1030 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +02001031 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001032
1033 /* Check for truncation */
1034 if (value != (size_t)value) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001035 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1036 "exceeding address space");
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001037 return;
1038 }
1039
Orit Wassermana5615b12014-01-30 20:08:36 +02001040 /* Cache should not be larger than guest ram size */
1041 if (value > ram_bytes_total()) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001042 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1043 "exceeds guest ram size ");
Orit Wassermana5615b12014-01-30 20:08:36 +02001044 return;
1045 }
1046
Orit Wassermanc91e6812014-01-30 20:08:34 +02001047 new_size = xbzrle_cache_resize(value);
1048 if (new_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001049 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1050 "is smaller than page size");
Orit Wassermanc91e6812014-01-30 20:08:34 +02001051 return;
1052 }
1053
1054 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001055}
1056
1057int64_t qmp_query_migrate_cache_size(Error **errp)
1058{
1059 return migrate_xbzrle_cache_size();
1060}
1061
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001062void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001063{
Juan Quintelacab30142011-02-22 23:54:21 +01001064 MigrationState *s;
1065
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001066 if (value < 0) {
1067 value = 0;
Juan Quintelacab30142011-02-22 23:54:21 +01001068 }
Paolo Bonzini442773c2013-02-22 17:36:44 +01001069 if (value > SIZE_MAX) {
1070 value = SIZE_MAX;
1071 }
Juan Quintelacab30142011-02-22 23:54:21 +01001072
Juan Quintela17549e82011-10-05 13:50:43 +02001073 s = migrate_get_current();
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001074 s->bandwidth_limit = value;
Paolo Bonzini442773c2013-02-22 17:36:44 +01001075 if (s->file) {
1076 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
1077 }
Juan Quintelacab30142011-02-22 23:54:21 +01001078}
1079
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001080void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001081{
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001082 value *= 1e9;
1083 value = MAX(0, MIN(UINT64_MAX, value));
1084 max_downtime = (uint64_t)value;
aliguori5bb79102008-10-13 03:12:02 +00001085}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001086
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001087bool migrate_postcopy_ram(void)
1088{
1089 MigrationState *s;
1090
1091 s = migrate_get_current();
1092
1093 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_POSTCOPY_RAM];
1094}
1095
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06001096bool migrate_auto_converge(void)
1097{
1098 MigrationState *s;
1099
1100 s = migrate_get_current();
1101
1102 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1103}
1104
Peter Lieven323004a2013-07-18 09:48:50 +02001105bool migrate_zero_blocks(void)
1106{
1107 MigrationState *s;
1108
1109 s = migrate_get_current();
1110
1111 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1112}
1113
Liang Li8706d2d2015-03-23 16:32:17 +08001114bool migrate_use_compression(void)
1115{
Liang Lidde4e692015-03-23 16:32:26 +08001116 MigrationState *s;
1117
1118 s = migrate_get_current();
1119
1120 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08001121}
1122
1123int migrate_compress_level(void)
1124{
1125 MigrationState *s;
1126
1127 s = migrate_get_current();
1128
Liang Li43c60a82015-03-23 16:32:27 +08001129 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
Liang Li8706d2d2015-03-23 16:32:17 +08001130}
1131
1132int migrate_compress_threads(void)
1133{
1134 MigrationState *s;
1135
1136 s = migrate_get_current();
1137
Liang Li43c60a82015-03-23 16:32:27 +08001138 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
Liang Li8706d2d2015-03-23 16:32:17 +08001139}
1140
Liang Li3fcb38c2015-03-23 16:32:18 +08001141int migrate_decompress_threads(void)
1142{
1143 MigrationState *s;
1144
1145 s = migrate_get_current();
1146
Liang Li43c60a82015-03-23 16:32:27 +08001147 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Liang Li3fcb38c2015-03-23 16:32:18 +08001148}
1149
Juan Quintelab05dc722015-07-07 14:44:05 +02001150bool migrate_use_events(void)
1151{
1152 MigrationState *s;
1153
1154 s = migrate_get_current();
1155
1156 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1157}
1158
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001159int migrate_use_xbzrle(void)
1160{
1161 MigrationState *s;
1162
1163 s = migrate_get_current();
1164
1165 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1166}
1167
1168int64_t migrate_xbzrle_cache_size(void)
1169{
1170 MigrationState *s;
1171
1172 s = migrate_get_current();
1173
1174 return s->xbzrle_cache_size;
1175}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001176
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001177/* migration thread support */
1178/*
1179 * Something bad happened to the RP stream, mark an error
1180 * The caller shall print or trace something to indicate why
1181 */
1182static void mark_source_rp_bad(MigrationState *s)
1183{
1184 s->rp_state.error = true;
1185}
1186
1187static struct rp_cmd_args {
1188 ssize_t len; /* -1 = variable */
1189 const char *name;
1190} rp_cmd_args[] = {
1191 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1192 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1193 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001194 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1195 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001196 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1197};
1198
1199/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001200 * Process a request for pages received on the return path,
1201 * We're allowed to send more than requested (e.g. to round to our page size)
1202 * and we don't need to send pages that have already been sent.
1203 */
1204static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1205 ram_addr_t start, size_t len)
1206{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001207 long our_host_ps = getpagesize();
1208
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001209 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001210
1211 /*
1212 * Since we currently insist on matching page sizes, just sanity check
1213 * we're being asked for whole host pages.
1214 */
1215 if (start & (our_host_ps-1) ||
1216 (len & (our_host_ps-1))) {
1217 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1218 " len: %zd", __func__, start, len);
1219 mark_source_rp_bad(ms);
1220 return;
1221 }
1222
1223 if (ram_save_queue_pages(ms, rbname, start, len)) {
1224 mark_source_rp_bad(ms);
1225 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001226}
1227
1228/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001229 * Handles messages sent on the return path towards the source VM
1230 *
1231 */
1232static void *source_return_path_thread(void *opaque)
1233{
1234 MigrationState *ms = opaque;
1235 QEMUFile *rp = ms->rp_state.from_dst_file;
1236 uint16_t header_len, header_type;
1237 const int max_len = 512;
1238 uint8_t buf[max_len];
1239 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001240 ram_addr_t start = 0; /* =0 to silence warning */
1241 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001242 int res;
1243
1244 trace_source_return_path_thread_entry();
1245 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1246 migration_is_setup_or_active(ms->state)) {
1247 trace_source_return_path_thread_loop_top();
1248 header_type = qemu_get_be16(rp);
1249 header_len = qemu_get_be16(rp);
1250
1251 if (header_type >= MIG_RP_MSG_MAX ||
1252 header_type == MIG_RP_MSG_INVALID) {
1253 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1254 header_type, header_len);
1255 mark_source_rp_bad(ms);
1256 goto out;
1257 }
1258
1259 if ((rp_cmd_args[header_type].len != -1 &&
1260 header_len != rp_cmd_args[header_type].len) ||
1261 header_len > max_len) {
1262 error_report("RP: Received '%s' message (0x%04x) with"
1263 "incorrect length %d expecting %zu",
1264 rp_cmd_args[header_type].name, header_type, header_len,
1265 (size_t)rp_cmd_args[header_type].len);
1266 mark_source_rp_bad(ms);
1267 goto out;
1268 }
1269
1270 /* We know we've got a valid header by this point */
1271 res = qemu_get_buffer(rp, buf, header_len);
1272 if (res != header_len) {
1273 error_report("RP: Failed reading data for message 0x%04x"
1274 " read %d expected %d",
1275 header_type, res, header_len);
1276 mark_source_rp_bad(ms);
1277 goto out;
1278 }
1279
1280 /* OK, we have the message and the data */
1281 switch (header_type) {
1282 case MIG_RP_MSG_SHUT:
1283 sibling_error = be32_to_cpup((uint32_t *)buf);
1284 trace_source_return_path_thread_shut(sibling_error);
1285 if (sibling_error) {
1286 error_report("RP: Sibling indicated error %d", sibling_error);
1287 mark_source_rp_bad(ms);
1288 }
1289 /*
1290 * We'll let the main thread deal with closing the RP
1291 * we could do a shutdown(2) on it, but we're the only user
1292 * anyway, so there's nothing gained.
1293 */
1294 goto out;
1295
1296 case MIG_RP_MSG_PONG:
1297 tmp32 = be32_to_cpup((uint32_t *)buf);
1298 trace_source_return_path_thread_pong(tmp32);
1299 break;
1300
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001301 case MIG_RP_MSG_REQ_PAGES:
1302 start = be64_to_cpup((uint64_t *)buf);
1303 len = be32_to_cpup((uint32_t *)(buf + 8));
1304 migrate_handle_rp_req_pages(ms, NULL, start, len);
1305 break;
1306
1307 case MIG_RP_MSG_REQ_PAGES_ID:
1308 expected_len = 12 + 1; /* header + termination */
1309
1310 if (header_len >= expected_len) {
1311 start = be64_to_cpup((uint64_t *)buf);
1312 len = be32_to_cpup((uint32_t *)(buf + 8));
1313 /* Now we expect an idstr */
1314 tmp32 = buf[12]; /* Length of the following idstr */
1315 buf[13 + tmp32] = '\0';
1316 expected_len += tmp32;
1317 }
1318 if (header_len != expected_len) {
1319 error_report("RP: Req_Page_id with length %d expecting %zd",
1320 header_len, expected_len);
1321 mark_source_rp_bad(ms);
1322 goto out;
1323 }
1324 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
1325 break;
1326
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001327 default:
1328 break;
1329 }
1330 }
1331 if (rp && qemu_file_get_error(rp)) {
1332 trace_source_return_path_thread_bad_end();
1333 mark_source_rp_bad(ms);
1334 }
1335
1336 trace_source_return_path_thread_end();
1337out:
1338 ms->rp_state.from_dst_file = NULL;
1339 qemu_fclose(rp);
1340 return NULL;
1341}
1342
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001343static int open_return_path_on_source(MigrationState *ms)
1344{
1345
1346 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->file);
1347 if (!ms->rp_state.from_dst_file) {
1348 return -1;
1349 }
1350
1351 trace_open_return_path_on_source();
1352 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1353 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1354
1355 trace_open_return_path_on_source_continue();
1356
1357 return 0;
1358}
1359
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001360/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1361static int await_return_path_close_on_source(MigrationState *ms)
1362{
1363 /*
1364 * If this is a normal exit then the destination will send a SHUT and the
1365 * rp_thread will exit, however if there's an error we need to cause
1366 * it to exit.
1367 */
1368 if (qemu_file_get_error(ms->file) && ms->rp_state.from_dst_file) {
1369 /*
1370 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1371 * waiting for the destination.
1372 */
1373 qemu_file_shutdown(ms->rp_state.from_dst_file);
1374 mark_source_rp_bad(ms);
1375 }
1376 trace_await_return_path_close_on_source_joining();
1377 qemu_thread_join(&ms->rp_state.rp_thread);
1378 trace_await_return_path_close_on_source_close();
1379 return ms->rp_state.error;
1380}
1381
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001382/*
1383 * Switch from normal iteration to postcopy
1384 * Returns non-0 on error
1385 */
1386static int postcopy_start(MigrationState *ms, bool *old_vm_running)
1387{
1388 int ret;
1389 const QEMUSizedBuffer *qsb;
1390 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1391 migrate_set_state(ms, MIGRATION_STATUS_ACTIVE,
1392 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1393
1394 trace_postcopy_start();
1395 qemu_mutex_lock_iothread();
1396 trace_postcopy_start_set_run();
1397
1398 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1399 *old_vm_running = runstate_is_running();
1400 global_state_store();
1401 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
1402
1403 if (ret < 0) {
1404 goto fail;
1405 }
1406
1407 /*
1408 * in Finish migrate and with the io-lock held everything should
1409 * be quiet, but we've potentially still got dirty pages and we
1410 * need to tell the destination to throw any pages it's already received
1411 * that are dirty
1412 */
1413 if (ram_postcopy_send_discard_bitmap(ms)) {
1414 error_report("postcopy send discard bitmap failed");
1415 goto fail;
1416 }
1417
1418 /*
1419 * send rest of state - note things that are doing postcopy
1420 * will notice we're in POSTCOPY_ACTIVE and not actually
1421 * wrap their state up here
1422 */
1423 qemu_file_set_rate_limit(ms->file, INT64_MAX);
1424 /* Ping just for debugging, helps line traces up */
1425 qemu_savevm_send_ping(ms->file, 2);
1426
1427 /*
1428 * While loading the device state we may trigger page transfer
1429 * requests and the fd must be free to process those, and thus
1430 * the destination must read the whole device state off the fd before
1431 * it starts processing it. Unfortunately the ad-hoc migration format
1432 * doesn't allow the destination to know the size to read without fully
1433 * parsing it through each devices load-state code (especially the open
1434 * coded devices that use get/put).
1435 * So we wrap the device state up in a package with a length at the start;
1436 * to do this we use a qemu_buf to hold the whole of the device state.
1437 */
1438 QEMUFile *fb = qemu_bufopen("w", NULL);
1439 if (!fb) {
1440 error_report("Failed to create buffered file");
1441 goto fail;
1442 }
1443
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001444 /*
1445 * Make sure the receiver can get incoming pages before we send the rest
1446 * of the state
1447 */
1448 qemu_savevm_send_postcopy_listen(fb);
1449
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001450 qemu_savevm_state_complete_precopy(fb);
1451 qemu_savevm_send_ping(fb, 3);
1452
1453 qemu_savevm_send_postcopy_run(fb);
1454
1455 /* <><> end of stuff going into the package */
1456 qsb = qemu_buf_get(fb);
1457
1458 /* Now send that blob */
1459 if (qemu_savevm_send_packaged(ms->file, qsb)) {
1460 goto fail_closefb;
1461 }
1462 qemu_fclose(fb);
1463 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
1464
1465 qemu_mutex_unlock_iothread();
1466
1467 /*
1468 * Although this ping is just for debug, it could potentially be
1469 * used for getting a better measurement of downtime at the source.
1470 */
1471 qemu_savevm_send_ping(ms->file, 4);
1472
1473 ret = qemu_file_get_error(ms->file);
1474 if (ret) {
1475 error_report("postcopy_start: Migration stream errored");
1476 migrate_set_state(ms, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1477 MIGRATION_STATUS_FAILED);
1478 }
1479
1480 return ret;
1481
1482fail_closefb:
1483 qemu_fclose(fb);
1484fail:
1485 migrate_set_state(ms, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1486 MIGRATION_STATUS_FAILED);
1487 qemu_mutex_unlock_iothread();
1488 return -1;
1489}
1490
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001491/**
1492 * migration_completion: Used by migration_thread when there's not much left.
1493 * The caller 'breaks' the loop when this returns.
1494 *
1495 * @s: Current migration state
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001496 * @current_active_state: The migration state we expect to be in
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001497 * @*old_vm_running: Pointer to old_vm_running flag
1498 * @*start_time: Pointer to time to update
1499 */
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001500static void migration_completion(MigrationState *s, int current_active_state,
1501 bool *old_vm_running,
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001502 int64_t *start_time)
1503{
1504 int ret;
1505
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001506 if (s->state == MIGRATION_STATUS_ACTIVE) {
1507 qemu_mutex_lock_iothread();
1508 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1509 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1510 *old_vm_running = runstate_is_running();
1511 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001512
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001513 if (!ret) {
1514 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
1515 if (ret >= 0) {
1516 qemu_file_set_rate_limit(s->file, INT64_MAX);
1517 qemu_savevm_state_complete_precopy(s->file);
1518 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001519 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001520 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001521
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001522 if (ret < 0) {
1523 goto fail;
1524 }
1525 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1526 trace_migration_completion_postcopy_end();
1527
1528 qemu_savevm_state_complete_postcopy(s->file);
1529 trace_migration_completion_postcopy_end_after_complete();
1530 }
1531
1532 /*
1533 * If rp was opened we must clean up the thread before
1534 * cleaning everything else up (since if there are no failures
1535 * it will wait for the destination to send it's status in
1536 * a SHUT command).
1537 * Postcopy opens rp if enabled (even if it's not avtivated)
1538 */
1539 if (migrate_postcopy_ram()) {
1540 int rp_error;
1541 trace_migration_completion_postcopy_end_before_rp();
1542 rp_error = await_return_path_close_on_source(s);
1543 trace_migration_completion_postcopy_end_after_rp(rp_error);
1544 if (rp_error) {
1545 goto fail;
1546 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001547 }
1548
1549 if (qemu_file_get_error(s->file)) {
1550 trace_migration_completion_file_err();
1551 goto fail;
1552 }
1553
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001554 migrate_set_state(s, current_active_state, MIGRATION_STATUS_COMPLETED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001555 return;
1556
1557fail:
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001558 migrate_set_state(s, current_active_state, MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001559}
1560
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001561/*
1562 * Master migration thread on the source VM.
1563 * It drives the migration and pumps the data down the outgoing channel.
1564 */
Juan Quintela5f496a12013-02-22 17:36:30 +01001565static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001566{
Juan Quintela9848a402012-12-19 09:55:50 +01001567 MigrationState *s = opaque;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001568 /* Used by the bandwidth calcs, updated later */
Alex Blighbc72ad62013-08-21 16:03:08 +01001569 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1570 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001571 int64_t initial_bytes = 0;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001572 int64_t max_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001573 int64_t start_time = initial_time;
Liang Li94f5a432015-11-02 15:37:00 +08001574 int64_t end_time;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001575 bool old_vm_running = false;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001576 bool entered_postcopy = false;
1577 /* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */
1578 enum MigrationStatus current_active_state = MIGRATION_STATUS_ACTIVE;
Juan Quintela76f59332012-10-03 20:16:24 +02001579
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001580 rcu_register_thread();
1581
Dr. David Alan Gilbertf796baa2015-05-21 13:24:12 +01001582 qemu_savevm_state_header(s->file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001583
1584 if (migrate_postcopy_ram()) {
1585 /* Now tell the dest that it should open its end so it can reply */
1586 qemu_savevm_send_open_return_path(s->file);
1587
1588 /* And do a ping that will make stuff easier to debug */
1589 qemu_savevm_send_ping(s->file, 1);
1590
1591 /*
1592 * Tell the destination that we *might* want to do postcopy later;
1593 * if the other end can't do postcopy it should fail now, nice and
1594 * early.
1595 */
1596 qemu_savevm_send_postcopy_advise(s->file);
1597 }
1598
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001599 qemu_savevm_state_begin(s->file, &s->params);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001600
Alex Blighbc72ad62013-08-21 16:03:08 +01001601 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001602 current_active_state = MIGRATION_STATUS_ACTIVE;
zhanghailiang31194732015-03-13 16:08:38 +08001603 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001604
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001605 trace_migration_thread_setup_complete();
1606
1607 while (s->state == MIGRATION_STATUS_ACTIVE ||
1608 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +01001609 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +02001610 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001611
Paolo Bonzinia0ff0442013-02-22 17:36:35 +01001612 if (!qemu_file_rate_limit(s->file)) {
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001613 uint64_t pend_post, pend_nonpost;
1614
1615 qemu_savevm_state_pending(s->file, max_size, &pend_nonpost,
1616 &pend_post);
1617 pending_size = pend_nonpost + pend_post;
1618 trace_migrate_pending(pending_size, max_size,
1619 pend_post, pend_nonpost);
Juan Quintelab22ff1f2012-10-17 21:06:31 +02001620 if (pending_size && pending_size >= max_size) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001621 /* Still a significant amount to transfer */
1622
1623 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1624 if (migrate_postcopy_ram() &&
1625 s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE &&
1626 pend_nonpost <= max_size &&
1627 atomic_read(&s->start_postcopy)) {
1628
1629 if (!postcopy_start(s, &old_vm_running)) {
1630 current_active_state = MIGRATION_STATUS_POSTCOPY_ACTIVE;
1631 entered_postcopy = true;
1632 }
1633
1634 continue;
1635 }
1636 /* Just another iteration step */
Dr. David Alan Gilbert35ecd942015-11-05 18:11:14 +00001637 qemu_savevm_state_iterate(s->file, entered_postcopy);
Juan Quintelac369f402012-10-03 20:33:34 +02001638 } else {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001639 trace_migration_thread_low_pending(pending_size);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001640 migration_completion(s, current_active_state,
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001641 &old_vm_running, &start_time);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001642 break;
Juan Quintelac369f402012-10-03 20:33:34 +02001643 }
1644 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001645
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01001646 if (qemu_file_get_error(s->file)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001647 migrate_set_state(s, current_active_state, MIGRATION_STATUS_FAILED);
1648 trace_migration_thread_file_err();
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01001649 break;
1650 }
Alex Blighbc72ad62013-08-21 16:03:08 +01001651 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001652 if (current_time >= initial_time + BUFFER_DELAY) {
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001653 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -05001654 uint64_t time_spent = current_time - initial_time;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001655 double bandwidth = transferred_bytes / time_spent;
1656 max_size = bandwidth * migrate_max_downtime() / 1000000;
1657
Michael R. Hines7e114f82013-06-25 21:35:30 -04001658 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
1659 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
1660
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001661 trace_migrate_transferred(transferred_bytes, time_spent,
1662 bandwidth, max_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +01001663 /* if we haven't sent anything, we don't want to recalculate
1664 10000 is a small enough number for our purposes */
1665 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
1666 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
1667 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001668
Paolo Bonzini1964a392013-02-22 17:36:45 +01001669 qemu_file_reset_rate_limit(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001670 initial_time = current_time;
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001671 initial_bytes = qemu_ftell(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001672 }
Paolo Bonzinia0ff0442013-02-22 17:36:35 +01001673 if (qemu_file_rate_limit(s->file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001674 /* usleep expects microseconds */
1675 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
1676 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001677 }
1678
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001679 trace_migration_thread_after_loop();
Jason J. Herne070afca2015-09-08 13:12:35 -04001680 /* If we enabled cpu throttling for auto-converge, turn it off. */
1681 cpu_throttle_stop();
Liang Li94f5a432015-11-02 15:37:00 +08001682 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jason J. Herne070afca2015-09-08 13:12:35 -04001683
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001684 qemu_mutex_lock_iothread();
Liang Liea7415f2015-11-02 15:37:01 +08001685 qemu_savevm_state_cleanup();
zhanghailiang31194732015-03-13 16:08:38 +08001686 if (s->state == MIGRATION_STATUS_COMPLETED) {
Peter Lievend6ed7312014-05-12 10:46:00 +02001687 uint64_t transferred_bytes = qemu_ftell(s->file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001688 s->total_time = end_time - s->total_time;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001689 if (!entered_postcopy) {
1690 s->downtime = end_time - start_time;
1691 }
Peter Lievend6ed7312014-05-12 10:46:00 +02001692 if (s->total_time) {
1693 s->mbps = (((double) transferred_bytes * 8.0) /
1694 ((double) s->total_time)) / 1000;
1695 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001696 runstate_set(RUN_STATE_POSTMIGRATE);
1697 } else {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001698 if (old_vm_running && !entered_postcopy) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001699 vm_start();
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001700 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001701 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001702 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001703 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001704
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001705 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001706 return NULL;
1707}
1708
Juan Quintela9848a402012-12-19 09:55:50 +01001709void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001710{
Juan Quintelacc283e32013-02-01 11:12:26 +01001711 /* This is a best 1st approximation. ns to ms */
1712 s->expected_downtime = max_downtime/1000000;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001713 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001714
Paolo Bonzini442773c2013-02-22 17:36:44 +01001715 qemu_file_set_rate_limit(s->file,
1716 s->bandwidth_limit / XFER_LIMIT_RATIO);
1717
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02001718 /* Notify before starting migration thread */
1719 notifier_list_notify(&migration_state_notifiers, s);
1720
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001721 /*
1722 * Open the return path; currently for postcopy but other things might
1723 * also want it.
1724 */
1725 if (migrate_postcopy_ram()) {
1726 if (open_return_path_on_source(s)) {
1727 error_report("Unable to open return-path for postcopy");
1728 migrate_set_state(s, MIGRATION_STATUS_SETUP,
1729 MIGRATION_STATUS_FAILED);
1730 migrate_fd_cleanup(s);
1731 return;
1732 }
1733 }
1734
Liang Li8706d2d2015-03-23 16:32:17 +08001735 migrate_compress_threads_create();
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001736 qemu_thread_create(&s->thread, "migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001737 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001738 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001739}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001740
1741PostcopyState postcopy_state_get(void)
1742{
1743 return atomic_mb_read(&incoming_postcopy_state);
1744}
1745
1746/* Set the state and return the old state */
1747PostcopyState postcopy_state_set(PostcopyState new_state)
1748{
1749 return atomic_xchg(&incoming_postcopy_state, new_state);
1750}
1751