blob: 04b706f69ec73808ec564513365a05471a7095b8 [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"
Daniel P. Berrangee1226362016-04-27 11:05:16 +010038#include "io/channel-tls.h"
aliguori065e2812008-11-11 16:46:33 +000039
Jason J. Hernedc325622015-09-08 13:12:37 -040040#define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000041
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010042/* Amount of time to allocate to each "chunk" of bandwidth-throttled
43 * data. */
44#define BUFFER_DELAY 100
45#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
46
Liang Li8706d2d2015-03-23 16:32:17 +080047/* Default compression thread count */
48#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080049/* Default decompression thread count, usually decompression is at
50 * least 4 times as fast as compression.*/
51#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080052/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
53#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
Jason J. Herne1626fee2015-09-08 13:12:34 -040054/* Define default autoconverge cpu throttle migration parameters */
Jason J. Herned85a31d2016-04-21 14:07:18 -040055#define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
56#define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
Liang Li8706d2d2015-03-23 16:32:17 +080057
Orit Wasserman17ad9b32012-08-06 21:42:53 +030058/* Migration XBZRLE default cache size */
59#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
60
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010061static NotifierList migration_state_notifiers =
62 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
63
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000064static bool deferred_incoming;
65
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +000066/*
67 * Current state of incoming postcopy; note this is not part of
68 * MigrationIncomingState since it's state is used during cleanup
69 * at the end as MIS is being freed.
70 */
71static PostcopyState incoming_postcopy_state;
72
Juan Quintela17549e82011-10-05 13:50:43 +020073/* When we add fault tolerance, we could have several
74 migrations at once. For now we don't need to add
75 dynamic creation of migration */
76
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010077/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +020078MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +020079{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000080 static bool once;
Juan Quintela17549e82011-10-05 13:50:43 +020081 static MigrationState current_migration = {
zhanghailiang31194732015-03-13 16:08:38 +080082 .state = MIGRATION_STATUS_NONE,
Juan Quintelad0ae46c2011-02-23 00:33:19 +010083 .bandwidth_limit = MAX_THROTTLE,
Orit Wasserman17ad9b32012-08-06 21:42:53 +030084 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
Michael R. Hines7e114f82013-06-25 21:35:30 -040085 .mbps = -1,
Daniel P. Berrange2594f562016-04-27 11:05:14 +010086 .parameters = {
87 .compress_level = DEFAULT_MIGRATE_COMPRESS_LEVEL,
88 .compress_threads = DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
89 .decompress_threads = DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
90 .cpu_throttle_initial = DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL,
91 .cpu_throttle_increment = DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT,
92 },
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;
zhanghailiang93d7af62015-12-16 11:47:34 +0000114 mis_current->state = MIGRATION_STATUS_NONE;
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +0100115 QLIST_INIT(&mis_current->loadvm_handlers);
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000116 qemu_mutex_init(&mis_current->rp_mutex);
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +0000117 qemu_event_init(&mis_current->main_thread_load_event, false);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100118
119 return mis_current;
120}
121
122void migration_incoming_state_destroy(void)
123{
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +0000124 qemu_event_destroy(&mis_current->main_thread_load_event);
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +0100125 loadvm_free_handlers(mis_current);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100126 g_free(mis_current);
127 mis_current = NULL;
128}
129
Juan Quinteladf4b1022014-10-08 10:58:10 +0200130
131typedef struct {
Juan Quintela13d16812014-10-08 13:58:24 +0200132 bool optional;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200133 uint32_t size;
134 uint8_t runstate[100];
Juan Quintela172c4352015-07-08 13:56:26 +0200135 RunState state;
136 bool received;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200137} GlobalState;
138
139static GlobalState global_state;
140
Juan Quintela560d0272015-07-15 09:53:46 +0200141int global_state_store(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200142{
143 if (!runstate_store((char *)global_state.runstate,
144 sizeof(global_state.runstate))) {
145 error_report("runstate name too big: %s", global_state.runstate);
146 trace_migrate_state_too_big();
147 return -EINVAL;
148 }
149 return 0;
150}
151
Anthony PERARDc69adea2015-08-03 15:29:19 +0100152void global_state_store_running(void)
153{
154 const char *state = RunState_lookup[RUN_STATE_RUNNING];
155 strncpy((char *)global_state.runstate,
156 state, sizeof(global_state.runstate));
157}
158
Juan Quintela172c4352015-07-08 13:56:26 +0200159static bool global_state_received(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200160{
Juan Quintela172c4352015-07-08 13:56:26 +0200161 return global_state.received;
162}
163
164static RunState global_state_get_runstate(void)
165{
166 return global_state.state;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200167}
168
Juan Quintela13d16812014-10-08 13:58:24 +0200169void global_state_set_optional(void)
170{
171 global_state.optional = true;
172}
173
174static bool global_state_needed(void *opaque)
175{
176 GlobalState *s = opaque;
177 char *runstate = (char *)s->runstate;
178
179 /* If it is not optional, it is mandatory */
180
181 if (s->optional == false) {
182 return true;
183 }
184
185 /* If state is running or paused, it is not needed */
186
187 if (strcmp(runstate, "running") == 0 ||
188 strcmp(runstate, "paused") == 0) {
189 return false;
190 }
191
192 /* for any other state it is needed */
193 return true;
194}
195
Juan Quinteladf4b1022014-10-08 10:58:10 +0200196static int global_state_post_load(void *opaque, int version_id)
197{
198 GlobalState *s = opaque;
Juan Quintela172c4352015-07-08 13:56:26 +0200199 Error *local_err = NULL;
200 int r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200201 char *runstate = (char *)s->runstate;
202
Juan Quintela172c4352015-07-08 13:56:26 +0200203 s->received = true;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200204 trace_migrate_global_state_post_load(runstate);
205
Eric Blake7fb1cf12015-11-18 01:52:57 -0700206 r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE__MAX,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200207 -1, &local_err);
208
Juan Quintela172c4352015-07-08 13:56:26 +0200209 if (r == -1) {
210 if (local_err) {
211 error_report_err(local_err);
Juan Quinteladf4b1022014-10-08 10:58:10 +0200212 }
Juan Quintela172c4352015-07-08 13:56:26 +0200213 return -EINVAL;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200214 }
Juan Quintela172c4352015-07-08 13:56:26 +0200215 s->state = r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200216
Juan Quintela172c4352015-07-08 13:56:26 +0200217 return 0;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200218}
219
220static void global_state_pre_save(void *opaque)
221{
222 GlobalState *s = opaque;
223
224 trace_migrate_global_state_pre_save((char *)s->runstate);
225 s->size = strlen((char *)s->runstate) + 1;
226}
227
228static const VMStateDescription vmstate_globalstate = {
229 .name = "globalstate",
230 .version_id = 1,
231 .minimum_version_id = 1,
232 .post_load = global_state_post_load,
233 .pre_save = global_state_pre_save,
Juan Quintela13d16812014-10-08 13:58:24 +0200234 .needed = global_state_needed,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200235 .fields = (VMStateField[]) {
236 VMSTATE_UINT32(size, GlobalState),
237 VMSTATE_BUFFER(runstate, GlobalState),
238 VMSTATE_END_OF_LIST()
239 },
240};
241
242void register_global_state(void)
243{
244 /* We would use it independently that we receive it */
245 strcpy((char *)&global_state.runstate, "");
Juan Quintela172c4352015-07-08 13:56:26 +0200246 global_state.received = false;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200247 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
248}
249
Juan Quintelab05dc722015-07-07 14:44:05 +0200250static void migrate_generate_event(int new_state)
251{
252 if (migrate_use_events()) {
253 qapi_event_send_migration(new_state, &error_abort);
Juan Quintelab05dc722015-07-07 14:44:05 +0200254 }
255}
256
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000257/*
258 * Called on -incoming with a defer: uri.
259 * The migration can be started later after any parameters have been
260 * changed.
261 */
262static void deferred_incoming_migration(Error **errp)
263{
264 if (deferred_incoming) {
265 error_setg(errp, "Incoming migration already deferred");
266 }
267 deferred_incoming = true;
268}
269
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000270/* Request a range of pages from the source VM at the given
271 * start address.
272 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
273 * as the last request (a name must have been given previously)
274 * Start: Address offset within the RB
275 * Len: Length in bytes required - must be a multiple of pagesize
276 */
277void migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
278 ram_addr_t start, size_t len)
279{
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100280 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000281 size_t msglen = 12; /* start + len */
282
283 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
284 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
285
286 if (rbname) {
287 int rbname_len = strlen(rbname);
288 assert(rbname_len < 256);
289
290 bufc[msglen++] = rbname_len;
291 memcpy(bufc + msglen, rbname, rbname_len);
292 msglen += rbname_len;
293 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES_ID, msglen, bufc);
294 } else {
295 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES, msglen, bufc);
296 }
297}
298
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200299void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000300{
aliguori34c9dd82008-10-13 03:14:31 +0000301 const char *p;
302
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200303 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000304 if (!strcmp(uri, "defer")) {
305 deferred_incoming_migration(errp);
306 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200307 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400308#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000309 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400310 rdma_start_incoming_migration(p, errp);
311#endif
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);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000318 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100319 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200320 }
aliguori5bb79102008-10-13 03:12:02 +0000321}
322
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300323static void process_incoming_migration_bh(void *opaque)
324{
325 Error *local_err = NULL;
326 MigrationIncomingState *mis = opaque;
327
328 /* Make sure all file formats flush their mutable metadata */
329 bdrv_invalidate_cache_all(&local_err);
330 if (local_err) {
331 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
332 MIGRATION_STATUS_FAILED);
333 error_report_err(local_err);
334 migrate_decompress_threads_join();
335 exit(EXIT_FAILURE);
336 }
337
338 /*
339 * This must happen after all error conditions are dealt with and
340 * we're sure the VM is going to be running on this host.
341 */
342 qemu_announce_self();
343
344 /* If global state section was not received or we are in running
345 state, we need to obey autostart. Any other state is set with
346 runstate_set. */
347
348 if (!global_state_received() ||
349 global_state_get_runstate() == RUN_STATE_RUNNING) {
350 if (autostart) {
351 vm_start();
352 } else {
353 runstate_set(RUN_STATE_PAUSED);
354 }
355 } else {
356 runstate_set(global_state_get_runstate());
357 }
358 migrate_decompress_threads_join();
359 /*
360 * This must happen after any state changes since as soon as an external
361 * observer sees this event they might start to prod at the VM assuming
362 * it's ready to use.
363 */
364 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
365 MIGRATION_STATUS_COMPLETED);
366 qemu_bh_delete(mis->bh);
367 migration_incoming_state_destroy();
368}
369
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200370static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200371{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200372 QEMUFile *f = opaque;
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000373 MigrationIncomingState *mis;
374 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200375 int ret;
376
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000377 mis = migration_incoming_state_new(f);
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000378 postcopy_state_set(POSTCOPY_INCOMING_NONE);
zhanghailiang93d7af62015-12-16 11:47:34 +0000379 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
380 MIGRATION_STATUS_ACTIVE);
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200381 ret = qemu_loadvm_state(f);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100382
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000383 ps = postcopy_state_get();
384 trace_process_incoming_migration_co_end(ret, ps);
385 if (ps != POSTCOPY_INCOMING_NONE) {
386 if (ps == POSTCOPY_INCOMING_ADVISE) {
387 /*
388 * Where a migration had postcopy enabled (and thus went to advise)
389 * but managed to complete within the precopy period, we can use
390 * the normal exit.
391 */
392 postcopy_ram_incoming_cleanup(mis);
393 } else if (ret >= 0) {
394 /*
395 * Postcopy was started, cleanup should happen at the end of the
396 * postcopy thread.
397 */
398 trace_process_incoming_migration_co_postcopy_end_main();
399 return;
400 }
401 /* Else if something went wrong then just fall out of the normal exit */
402 }
403
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200404 qemu_fclose(f);
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200405 free_xbzrle_decoded_buf();
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100406
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200407 if (ret < 0) {
zhanghailiang93d7af62015-12-16 11:47:34 +0000408 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
409 MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200410 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800411 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600412 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200413 }
Juan Quintela511c0232010-06-09 14:10:55 +0200414
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300415 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
416 qemu_bh_schedule(mis->bh);
Juan Quintela511c0232010-06-09 14:10:55 +0200417}
418
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100419void migration_fd_process_incoming(QEMUFile *f)
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200420{
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200421 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, f);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200422
Liang Li3fcb38c2015-03-23 16:32:18 +0800423 migrate_decompress_threads_create();
Daniel P. Berrange06ad5132016-04-27 11:04:56 +0100424 qemu_file_set_blocking(f, false);
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200425 qemu_coroutine_enter(co);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200426}
427
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100428
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100429void migration_channel_process_incoming(MigrationState *s,
430 QIOChannel *ioc)
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100431{
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100432 trace_migration_set_incoming_channel(
433 ioc, object_get_typename(OBJECT(ioc)));
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100434
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100435 if (s->parameters.tls_creds &&
436 !object_dynamic_cast(OBJECT(ioc),
437 TYPE_QIO_CHANNEL_TLS)) {
438 Error *local_err = NULL;
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100439 migration_tls_channel_process_incoming(s, ioc, &local_err);
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100440 if (local_err) {
441 error_report_err(local_err);
442 }
443 } else {
444 QEMUFile *f = qemu_fopen_channel_input(ioc);
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100445 migration_fd_process_incoming(f);
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100446 }
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100447}
448
449
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100450void migration_channel_connect(MigrationState *s,
451 QIOChannel *ioc,
452 const char *hostname)
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100453{
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100454 trace_migration_set_outgoing_channel(
455 ioc, object_get_typename(OBJECT(ioc)), hostname);
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100456
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100457 if (s->parameters.tls_creds &&
458 !object_dynamic_cast(OBJECT(ioc),
459 TYPE_QIO_CHANNEL_TLS)) {
460 Error *local_err = NULL;
Daniel P. Berrange22724f42016-06-01 11:17:14 +0100461 migration_tls_channel_connect(s, ioc, hostname, &local_err);
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100462 if (local_err) {
463 migrate_fd_error(s, local_err);
464 error_free(local_err);
465 }
466 } else {
467 QEMUFile *f = qemu_fopen_channel_output(ioc);
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100468
Daniel P. Berrangee1226362016-04-27 11:05:16 +0100469 s->to_dst_file = f;
470
471 migrate_fd_connect(s);
472 }
Daniel P. Berrange48f07482016-04-27 11:04:59 +0100473}
474
475
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000476/*
477 * Send a message on the return channel back to the source
478 * of the migration.
479 */
480void migrate_send_rp_message(MigrationIncomingState *mis,
481 enum mig_rp_message_type message_type,
482 uint16_t len, void *data)
483{
484 trace_migrate_send_rp_message((int)message_type, len);
485 qemu_mutex_lock(&mis->rp_mutex);
486 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
487 qemu_put_be16(mis->to_src_file, len);
488 qemu_put_buffer(mis->to_src_file, data, len);
489 qemu_fflush(mis->to_src_file);
490 qemu_mutex_unlock(&mis->rp_mutex);
491}
492
493/*
494 * Send a 'SHUT' message on the return channel with the given value
495 * to indicate that we've finished with the RP. Non-0 value indicates
496 * error.
497 */
498void migrate_send_rp_shut(MigrationIncomingState *mis,
499 uint32_t value)
500{
501 uint32_t buf;
502
503 buf = cpu_to_be32(value);
504 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
505}
506
507/*
508 * Send a 'PONG' message on the return channel with the given value
509 * (normally in response to a 'PING')
510 */
511void migrate_send_rp_pong(MigrationIncomingState *mis,
512 uint32_t value)
513{
514 uint32_t buf;
515
516 buf = cpu_to_be32(value);
517 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
518}
519
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400520/* amount of nanoseconds we are willing to wait for migration to be down.
521 * the choice of nanoseconds is because it is the maximum resolution that
522 * get_clock() can achieve. It is an internal measure. All user-visible
523 * units must be in seconds */
Alexey Kardashevskiyf7cd55a2014-03-27 14:57:26 +1100524static uint64_t max_downtime = 300000000;
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400525
526uint64_t migrate_max_downtime(void)
527{
528 return max_downtime;
529}
530
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300531MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
532{
533 MigrationCapabilityStatusList *head = NULL;
534 MigrationCapabilityStatusList *caps;
535 MigrationState *s = migrate_get_current();
536 int i;
537
Michael Tokarev387eede2013-10-05 13:18:28 +0400538 caps = NULL; /* silence compiler warning */
Eric Blake7fb1cf12015-11-18 01:52:57 -0700539 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300540 if (head == NULL) {
541 head = g_malloc0(sizeof(*caps));
542 caps = head;
543 } else {
544 caps->next = g_malloc0(sizeof(*caps));
545 caps = caps->next;
546 }
547 caps->value =
548 g_malloc(sizeof(*caps->value));
549 caps->value->capability = i;
550 caps->value->state = s->enabled_capabilities[i];
551 }
552
553 return head;
554}
555
Liang Li85de8322015-03-23 16:32:28 +0800556MigrationParameters *qmp_query_migrate_parameters(Error **errp)
557{
558 MigrationParameters *params;
559 MigrationState *s = migrate_get_current();
560
561 params = g_malloc0(sizeof(*params));
Eric Blakede63ab62016-09-08 22:14:15 -0500562 params->has_compress_level = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100563 params->compress_level = s->parameters.compress_level;
Eric Blakede63ab62016-09-08 22:14:15 -0500564 params->has_compress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100565 params->compress_threads = s->parameters.compress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500566 params->has_decompress_threads = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100567 params->decompress_threads = s->parameters.decompress_threads;
Eric Blakede63ab62016-09-08 22:14:15 -0500568 params->has_cpu_throttle_initial = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100569 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
Eric Blakede63ab62016-09-08 22:14:15 -0500570 params->has_cpu_throttle_increment = true;
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100571 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
Eric Blakede63ab62016-09-08 22:14:15 -0500572 params->has_tls_creds = !!s->parameters.tls_creds;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100573 params->tls_creds = g_strdup(s->parameters.tls_creds);
Eric Blakede63ab62016-09-08 22:14:15 -0500574 params->has_tls_hostname = !!s->parameters.tls_hostname;
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100575 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
Liang Li85de8322015-03-23 16:32:28 +0800576
577 return params;
578}
579
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000580/*
581 * Return true if we're already in the middle of a migration
582 * (i.e. any of the active or setup states)
583 */
584static bool migration_is_setup_or_active(int state)
585{
586 switch (state) {
587 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000588 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000589 case MIGRATION_STATUS_SETUP:
590 return true;
591
592 default:
593 return false;
594
595 }
596}
597
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300598static void get_xbzrle_cache_stats(MigrationInfo *info)
599{
600 if (migrate_use_xbzrle()) {
601 info->has_xbzrle_cache = true;
602 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
603 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
604 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
605 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
606 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
ChenLiang8bc39232014-04-04 17:57:56 +0800607 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300608 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
609 }
610}
611
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100612static void populate_ram_info(MigrationInfo *info, MigrationState *s)
613{
614 info->has_ram = true;
615 info->ram = g_malloc0(sizeof(*info->ram));
616 info->ram->transferred = ram_bytes_transferred();
617 info->ram->total = ram_bytes_total();
618 info->ram->duplicate = dup_mig_pages_transferred();
619 info->ram->skipped = skipped_mig_pages_transferred();
620 info->ram->normal = norm_mig_pages_transferred();
621 info->ram->normal_bytes = norm_mig_bytes_transferred();
622 info->ram->mbps = s->mbps;
623 info->ram->dirty_sync_count = s->dirty_sync_count;
Dr. David Alan Gilbertd3bf5412016-06-13 12:16:42 +0100624 info->ram->postcopy_requests = s->postcopy_requests;
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100625
626 if (s->state != MIGRATION_STATUS_COMPLETED) {
627 info->ram->remaining = ram_bytes_remaining();
628 info->ram->dirty_pages_rate = s->dirty_pages_rate;
629 }
630}
631
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300632MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000633{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300634 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200635 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000636
Juan Quintela17549e82011-10-05 13:50:43 +0200637 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800638 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200639 /* no migration has happened ever */
640 break;
zhanghailiang31194732015-03-13 16:08:38 +0800641 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400642 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400643 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400644 break;
zhanghailiang31194732015-03-13 16:08:38 +0800645 case MIGRATION_STATUS_ACTIVE:
646 case MIGRATION_STATUS_CANCELLING:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300647 info->has_status = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200648 info->has_total_time = true;
Alex Blighbc72ad62013-08-21 16:03:08 +0100649 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Juan Quintela7aa939a2012-08-18 13:17:10 +0200650 - s->total_time;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200651 info->has_expected_downtime = true;
652 info->expected_downtime = s->expected_downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400653 info->has_setup_time = true;
654 info->setup_time = s->setup_time;
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200655
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100656 populate_ram_info(info, s);
Juan Quintela8d017192012-08-13 12:31:25 +0200657
Juan Quintela17549e82011-10-05 13:50:43 +0200658 if (blk_mig_active()) {
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300659 info->has_disk = true;
660 info->disk = g_malloc0(sizeof(*info->disk));
661 info->disk->transferred = blk_mig_bytes_transferred();
662 info->disk->remaining = blk_mig_bytes_remaining();
663 info->disk->total = blk_mig_bytes_total();
aliguoriff8d81d2008-10-24 22:10:31 +0000664 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300665
Jason J. Herne47828932015-09-08 13:12:36 -0400666 if (cpu_throttle_active()) {
Jason J. Herned85a31d2016-04-21 14:07:18 -0400667 info->has_cpu_throttle_percentage = true;
668 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
Jason J. Herne47828932015-09-08 13:12:36 -0400669 }
670
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300671 get_xbzrle_cache_stats(info);
Juan Quintela17549e82011-10-05 13:50:43 +0200672 break;
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000673 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
674 /* Mostly the same as active; TODO add some postcopy stats */
675 info->has_status = true;
676 info->has_total_time = true;
677 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
678 - s->total_time;
679 info->has_expected_downtime = true;
680 info->expected_downtime = s->expected_downtime;
681 info->has_setup_time = true;
682 info->setup_time = s->setup_time;
683
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100684 populate_ram_info(info, s);
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000685
686 if (blk_mig_active()) {
687 info->has_disk = true;
688 info->disk = g_malloc0(sizeof(*info->disk));
689 info->disk->transferred = blk_mig_bytes_transferred();
690 info->disk->remaining = blk_mig_bytes_remaining();
691 info->disk->total = blk_mig_bytes_total();
692 }
693
694 get_xbzrle_cache_stats(info);
695 break;
zhanghailiang31194732015-03-13 16:08:38 +0800696 case MIGRATION_STATUS_COMPLETED:
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300697 get_xbzrle_cache_stats(info);
698
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300699 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900700 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200701 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200702 info->has_downtime = true;
703 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400704 info->has_setup_time = true;
705 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200706
Dr. David Alan Gilberta22463a2016-06-13 12:16:41 +0100707 populate_ram_info(info, s);
Juan Quintela17549e82011-10-05 13:50:43 +0200708 break;
zhanghailiang31194732015-03-13 16:08:38 +0800709 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300710 info->has_status = true;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100711 if (s->error) {
712 info->has_error_desc = true;
713 info->error_desc = g_strdup(error_get_pretty(s->error));
714 }
Juan Quintela17549e82011-10-05 13:50:43 +0200715 break;
zhanghailiang31194732015-03-13 16:08:38 +0800716 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300717 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200718 break;
aliguori5bb79102008-10-13 03:12:02 +0000719 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800720 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300721
722 return info;
aliguori5bb79102008-10-13 03:12:02 +0000723}
724
Orit Wasserman00458432012-08-06 21:42:48 +0300725void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
726 Error **errp)
727{
728 MigrationState *s = migrate_get_current();
729 MigrationCapabilityStatusList *cap;
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100730 bool old_postcopy_cap = migrate_postcopy_ram();
Orit Wasserman00458432012-08-06 21:42:48 +0300731
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000732 if (migration_is_setup_or_active(s->state)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100733 error_setg(errp, QERR_MIGRATION_ACTIVE);
Orit Wasserman00458432012-08-06 21:42:48 +0300734 return;
735 }
736
737 for (cap = params; cap; cap = cap->next) {
738 s->enabled_capabilities[cap->value->capability] = cap->value->state;
739 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000740
741 if (migrate_postcopy_ram()) {
742 if (migrate_use_compression()) {
743 /* The decompression threads asynchronously write into RAM
744 * rather than use the atomic copies needed to avoid
745 * userfaulting. It should be possible to fix the decompression
746 * threads for compatibility in future.
747 */
748 error_report("Postcopy is not currently compatible with "
749 "compression");
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +0000750 s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM] =
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000751 false;
752 }
Dr. David Alan Gilbert096631b2016-06-13 12:16:45 +0100753 /* This check is reasonably expensive, so only when it's being
754 * set the first time, also it's only the destination that needs
755 * special support.
756 */
757 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
758 !postcopy_ram_supported_by_host()) {
759 /* postcopy_ram_supported_by_host will have emitted a more
760 * detailed message
761 */
762 error_report("Postcopy is not supported");
763 s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM] =
764 false;
765 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000766 }
Orit Wasserman00458432012-08-06 21:42:48 +0300767}
768
Eric Blake7f375e02016-09-08 22:14:16 -0500769void qmp_migrate_set_parameters(MigrationParameters *params, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +0800770{
771 MigrationState *s = migrate_get_current();
772
Eric Blake7f375e02016-09-08 22:14:16 -0500773 if (params->has_compress_level &&
774 (params->compress_level < 0 || params->compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100775 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
776 "is invalid, it should be in the range of 0 to 9");
Liang Li85de8322015-03-23 16:32:28 +0800777 return;
778 }
Eric Blake7f375e02016-09-08 22:14:16 -0500779 if (params->has_compress_threads &&
780 (params->compress_threads < 1 || params->compress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100781 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
782 "compress_threads",
783 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800784 return;
785 }
Eric Blake7f375e02016-09-08 22:14:16 -0500786 if (params->has_decompress_threads &&
787 (params->decompress_threads < 1 || params->decompress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100788 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
789 "decompress_threads",
790 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800791 return;
792 }
Eric Blake7f375e02016-09-08 22:14:16 -0500793 if (params->has_cpu_throttle_initial &&
794 (params->cpu_throttle_initial < 1 ||
795 params->cpu_throttle_initial > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400796 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400797 "cpu_throttle_initial",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400798 "an integer in the range of 1 to 99");
Ashijeet Acharya091ecc82016-09-10 02:43:17 +0530799 return;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400800 }
Eric Blake7f375e02016-09-08 22:14:16 -0500801 if (params->has_cpu_throttle_increment &&
802 (params->cpu_throttle_increment < 1 ||
803 params->cpu_throttle_increment > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400804 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400805 "cpu_throttle_increment",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400806 "an integer in the range of 1 to 99");
Ashijeet Acharya091ecc82016-09-10 02:43:17 +0530807 return;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400808 }
Liang Li85de8322015-03-23 16:32:28 +0800809
Eric Blake7f375e02016-09-08 22:14:16 -0500810 if (params->has_compress_level) {
811 s->parameters.compress_level = params->compress_level;
Liang Li85de8322015-03-23 16:32:28 +0800812 }
Eric Blake7f375e02016-09-08 22:14:16 -0500813 if (params->has_compress_threads) {
814 s->parameters.compress_threads = params->compress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800815 }
Eric Blake7f375e02016-09-08 22:14:16 -0500816 if (params->has_decompress_threads) {
817 s->parameters.decompress_threads = params->decompress_threads;
Liang Li85de8322015-03-23 16:32:28 +0800818 }
Eric Blake7f375e02016-09-08 22:14:16 -0500819 if (params->has_cpu_throttle_initial) {
820 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400821 }
Eric Blake7f375e02016-09-08 22:14:16 -0500822 if (params->has_cpu_throttle_increment) {
823 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400824 }
Eric Blake7f375e02016-09-08 22:14:16 -0500825 if (params->has_tls_creds) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100826 g_free(s->parameters.tls_creds);
Eric Blake7f375e02016-09-08 22:14:16 -0500827 s->parameters.tls_creds = g_strdup(params->tls_creds);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100828 }
Eric Blake7f375e02016-09-08 22:14:16 -0500829 if (params->has_tls_hostname) {
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100830 g_free(s->parameters.tls_hostname);
Eric Blake7f375e02016-09-08 22:14:16 -0500831 s->parameters.tls_hostname = g_strdup(params->tls_hostname);
Daniel P. Berrange69ef1f32016-04-27 11:05:15 +0100832 }
Liang Li85de8322015-03-23 16:32:28 +0800833}
834
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100835
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000836void qmp_migrate_start_postcopy(Error **errp)
837{
838 MigrationState *s = migrate_get_current();
839
840 if (!migrate_postcopy_ram()) {
Dr. David Alan Gilberta54d3402015-11-12 11:34:44 +0000841 error_setg(errp, "Enable postcopy with migrate_set_capability before"
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000842 " the start of migration");
843 return;
844 }
845
846 if (s->state == MIGRATION_STATUS_NONE) {
847 error_setg(errp, "Postcopy must be started after migration has been"
848 " started");
849 return;
850 }
851 /*
852 * we don't error if migration has finished since that would be racy
853 * with issuing this command.
854 */
855 atomic_set(&s->start_postcopy, true);
856}
857
aliguori065e2812008-11-11 16:46:33 +0000858/* shared migration helpers */
859
zhanghailiang48781e52015-12-16 11:47:33 +0000860void migrate_set_state(int *state, int old_state, int new_state)
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000861{
zhanghailiang48781e52015-12-16 11:47:33 +0000862 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
Juan Quintela4ba4bc52015-07-08 13:58:27 +0200863 trace_migrate_set_state(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200864 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000865 }
866}
867
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100868static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000869{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100870 MigrationState *s = opaque;
871
872 qemu_bh_delete(s->cleanup_bh);
873 s->cleanup_bh = NULL;
874
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +0000875 flush_page_queue(s);
876
zhanghailiang89a02a92016-01-15 11:37:42 +0800877 if (s->to_dst_file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100878 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100879 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +0000880 if (s->migration_thread_running) {
881 qemu_thread_join(&s->thread);
882 s->migration_thread_running = false;
883 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100884 qemu_mutex_lock_iothread();
885
Liang Li8706d2d2015-03-23 16:32:17 +0800886 migrate_compress_threads_join();
zhanghailiang89a02a92016-01-15 11:37:42 +0800887 qemu_fclose(s->to_dst_file);
888 s->to_dst_file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000889 }
890
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000891 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
892 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100893
Liang Li94f5a432015-11-02 15:37:00 +0800894 if (s->state == MIGRATION_STATUS_CANCELLING) {
zhanghailiang48781e52015-12-16 11:47:33 +0000895 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
Liang Li94f5a432015-11-02 15:37:00 +0800896 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100897 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100898
899 notifier_list_notify(&migration_state_notifiers, s);
aliguori065e2812008-11-11 16:46:33 +0000900}
901
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100902void migrate_fd_error(MigrationState *s, const Error *error)
aliguori065e2812008-11-11 16:46:33 +0000903{
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100904 trace_migrate_fd_error(error ? error_get_pretty(error) : "");
zhanghailiang89a02a92016-01-15 11:37:42 +0800905 assert(s->to_dst_file == NULL);
zhanghailiang48781e52015-12-16 11:47:33 +0000906 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
907 MIGRATION_STATUS_FAILED);
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100908 if (!s->error) {
909 s->error = error_copy(error);
910 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100911 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela458cf282011-02-22 23:32:54 +0100912}
913
Juan Quintela0edda1c2010-05-11 16:28:39 +0200914static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000915{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000916 int old_state ;
zhanghailiang89a02a92016-01-15 11:37:42 +0800917 QEMUFile *f = migrate_get_current()->to_dst_file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100918 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000919
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +0000920 if (s->rp_state.from_dst_file) {
921 /* shutdown the rp socket, so causing the rp thread to shutdown */
922 qemu_file_shutdown(s->rp_state.from_dst_file);
923 }
924
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000925 do {
926 old_state = s->state;
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000927 if (!migration_is_setup_or_active(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000928 break;
929 }
zhanghailiang48781e52015-12-16 11:47:33 +0000930 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
zhanghailiang31194732015-03-13 16:08:38 +0800931 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000932
933 /*
934 * If we're unlucky the migration code might be stuck somewhere in a
935 * send/write while the network has failed and is waiting to timeout;
936 * if we've got shutdown(2) available then we can force it to quit.
937 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
938 * called in a bh, so there is no race against this cancel.
939 */
zhanghailiang31194732015-03-13 16:08:38 +0800940 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000941 qemu_file_shutdown(f);
942 }
aliguori065e2812008-11-11 16:46:33 +0000943}
944
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100945void add_migration_state_change_notifier(Notifier *notify)
946{
947 notifier_list_add(&migration_state_notifiers, notify);
948}
949
950void remove_migration_state_change_notifier(Notifier *notify)
951{
Paolo Bonzini31552522012-01-13 17:34:01 +0100952 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100953}
954
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +0200955bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200956{
zhanghailiang31194732015-03-13 16:08:38 +0800957 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200958}
959
Juan Quintela70736932011-02-23 00:43:59 +0100960bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100961{
zhanghailiang31194732015-03-13 16:08:38 +0800962 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100963}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200964
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200965bool migration_has_failed(MigrationState *s)
966{
zhanghailiang31194732015-03-13 16:08:38 +0800967 return (s->state == MIGRATION_STATUS_CANCELLED ||
968 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200969}
970
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000971bool migration_in_postcopy(MigrationState *s)
972{
973 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
974}
975
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +0000976bool migration_in_postcopy_after_devices(MigrationState *s)
977{
978 return migration_in_postcopy(s) && s->postcopy_after_devices;
979}
980
Dr. David Alan Gilbertaefeb182015-11-05 18:10:40 +0000981MigrationState *migrate_init(const MigrationParams *params)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200982{
Juan Quintela17549e82011-10-05 13:50:43 +0200983 MigrationState *s = migrate_get_current();
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300984
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000985 /*
986 * Reinitialise all migration state, except
987 * parameters/capabilities that the user set, and
988 * locks.
989 */
990 s->bytes_xfer = 0;
991 s->xfer_limit = 0;
992 s->cleanup_bh = 0;
zhanghailiang89a02a92016-01-15 11:37:42 +0800993 s->to_dst_file = NULL;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000994 s->state = MIGRATION_STATUS_NONE;
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300995 s->params = *params;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000996 s->rp_state.from_dst_file = NULL;
997 s->rp_state.error = false;
998 s->mbps = 0.0;
999 s->downtime = 0;
1000 s->expected_downtime = 0;
1001 s->dirty_pages_rate = 0;
1002 s->dirty_bytes_rate = 0;
1003 s->setup_time = 0;
1004 s->dirty_sync_count = 0;
1005 s->start_postcopy = false;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001006 s->postcopy_after_devices = false;
Dr. David Alan Gilbertd3bf5412016-06-13 12:16:42 +01001007 s->postcopy_requests = 0;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +00001008 s->migration_thread_running = false;
1009 s->last_req_rb = NULL;
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001010 error_free(s->error);
1011 s->error = NULL;
Juan Quintela1299c632011-11-09 21:29:01 +01001012
zhanghailiang48781e52015-12-16 11:47:33 +00001013 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +02001014
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001015 QSIMPLEQ_INIT(&s->src_page_requests);
1016
Alex Blighbc72ad62013-08-21 16:03:08 +01001017 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +02001018 return s;
1019}
Juan Quintelacab30142011-02-22 23:54:21 +01001020
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001021static GSList *migration_blockers;
1022
1023void migrate_add_blocker(Error *reason)
1024{
1025 migration_blockers = g_slist_prepend(migration_blockers, reason);
1026}
1027
1028void migrate_del_blocker(Error *reason)
1029{
1030 migration_blockers = g_slist_remove(migration_blockers, reason);
1031}
1032
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001033void qmp_migrate_incoming(const char *uri, Error **errp)
1034{
1035 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001036 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001037
1038 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001039 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001040 return;
1041 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001042 if (!once) {
1043 error_setg(errp, "The incoming migration has already been started");
1044 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001045
1046 qemu_start_incoming_migration(uri, &local_err);
1047
1048 if (local_err) {
1049 error_propagate(errp, local_err);
1050 return;
1051 }
1052
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +00001053 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +00001054}
1055
Greg Kurz24f39022016-05-04 21:44:19 +02001056bool migration_is_blocked(Error **errp)
1057{
1058 if (qemu_savevm_state_blocked(errp)) {
1059 return true;
1060 }
1061
1062 if (migration_blockers) {
1063 *errp = error_copy(migration_blockers->data);
1064 return true;
1065 }
1066
1067 return false;
1068}
1069
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001070void qmp_migrate(const char *uri, bool has_blk, bool blk,
1071 bool has_inc, bool inc, bool has_detach, bool detach,
1072 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001073{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +02001074 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +02001075 MigrationState *s = migrate_get_current();
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001076 MigrationParams params;
Juan Quintelacab30142011-02-22 23:54:21 +01001077 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +01001078
Pawit Pornkitprasan8c0426a2013-07-30 08:39:52 +09001079 params.blk = has_blk && blk;
1080 params.shared = has_inc && inc;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001081
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +00001082 if (migration_is_setup_or_active(s->state) ||
zhanghailiang31194732015-03-13 16:08:38 +08001083 s->state == MIGRATION_STATUS_CANCELLING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001084 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001085 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001086 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +01001087 if (runstate_check(RUN_STATE_INMIGRATE)) {
1088 error_setg(errp, "Guest is waiting for an incoming migration");
1089 return;
1090 }
1091
Greg Kurz24f39022016-05-04 21:44:19 +02001092 if (migration_is_blocked(errp)) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001093 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001094 }
1095
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001096 s = migrate_init(&params);
Juan Quintelacab30142011-02-22 23:54:21 +01001097
1098 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001099 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04001100#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08001101 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04001102 rdma_start_outgoing_migration(s, p, &local_err);
1103#endif
Juan Quintelacab30142011-02-22 23:54:21 +01001104 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001105 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001106 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001107 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001108 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001109 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001110 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001111 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1112 "a valid migration protocol");
zhanghailiang48781e52015-12-16 11:47:33 +00001113 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1114 MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001115 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001116 }
1117
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001118 if (local_err) {
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +01001119 migrate_fd_error(s, local_err);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001120 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001121 return;
Juan Quintela1299c632011-11-09 21:29:01 +01001122 }
Juan Quintelacab30142011-02-22 23:54:21 +01001123}
1124
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02001125void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001126{
Juan Quintela17549e82011-10-05 13:50:43 +02001127 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01001128}
1129
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001130void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1131{
1132 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +02001133 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001134
1135 /* Check for truncation */
1136 if (value != (size_t)value) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001137 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1138 "exceeding address space");
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001139 return;
1140 }
1141
Orit Wassermana5615b12014-01-30 20:08:36 +02001142 /* Cache should not be larger than guest ram size */
1143 if (value > ram_bytes_total()) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001144 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1145 "exceeds guest ram size ");
Orit Wassermana5615b12014-01-30 20:08:36 +02001146 return;
1147 }
1148
Orit Wassermanc91e6812014-01-30 20:08:34 +02001149 new_size = xbzrle_cache_resize(value);
1150 if (new_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001151 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1152 "is smaller than page size");
Orit Wassermanc91e6812014-01-30 20:08:34 +02001153 return;
1154 }
1155
1156 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001157}
1158
1159int64_t qmp_query_migrate_cache_size(Error **errp)
1160{
1161 return migrate_xbzrle_cache_size();
1162}
1163
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001164void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001165{
Juan Quintelacab30142011-02-22 23:54:21 +01001166 MigrationState *s;
1167
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001168 if (value < 0) {
1169 value = 0;
Juan Quintelacab30142011-02-22 23:54:21 +01001170 }
Paolo Bonzini442773c2013-02-22 17:36:44 +01001171 if (value > SIZE_MAX) {
1172 value = SIZE_MAX;
1173 }
Juan Quintelacab30142011-02-22 23:54:21 +01001174
Juan Quintela17549e82011-10-05 13:50:43 +02001175 s = migrate_get_current();
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001176 s->bandwidth_limit = value;
zhanghailiang89a02a92016-01-15 11:37:42 +08001177 if (s->to_dst_file) {
1178 qemu_file_set_rate_limit(s->to_dst_file,
1179 s->bandwidth_limit / XFER_LIMIT_RATIO);
Paolo Bonzini442773c2013-02-22 17:36:44 +01001180 }
Juan Quintelacab30142011-02-22 23:54:21 +01001181}
1182
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001183void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001184{
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001185 value *= 1e9;
1186 value = MAX(0, MIN(UINT64_MAX, value));
1187 max_downtime = (uint64_t)value;
aliguori5bb79102008-10-13 03:12:02 +00001188}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001189
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001190bool migrate_postcopy_ram(void)
1191{
1192 MigrationState *s;
1193
1194 s = migrate_get_current();
1195
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +00001196 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001197}
1198
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06001199bool migrate_auto_converge(void)
1200{
1201 MigrationState *s;
1202
1203 s = migrate_get_current();
1204
1205 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1206}
1207
Peter Lieven323004a2013-07-18 09:48:50 +02001208bool migrate_zero_blocks(void)
1209{
1210 MigrationState *s;
1211
1212 s = migrate_get_current();
1213
1214 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1215}
1216
Liang Li8706d2d2015-03-23 16:32:17 +08001217bool migrate_use_compression(void)
1218{
Liang Lidde4e692015-03-23 16:32:26 +08001219 MigrationState *s;
1220
1221 s = migrate_get_current();
1222
1223 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08001224}
1225
1226int migrate_compress_level(void)
1227{
1228 MigrationState *s;
1229
1230 s = migrate_get_current();
1231
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001232 return s->parameters.compress_level;
Liang Li8706d2d2015-03-23 16:32:17 +08001233}
1234
1235int migrate_compress_threads(void)
1236{
1237 MigrationState *s;
1238
1239 s = migrate_get_current();
1240
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001241 return s->parameters.compress_threads;
Liang Li8706d2d2015-03-23 16:32:17 +08001242}
1243
Liang Li3fcb38c2015-03-23 16:32:18 +08001244int migrate_decompress_threads(void)
1245{
1246 MigrationState *s;
1247
1248 s = migrate_get_current();
1249
Daniel P. Berrange2594f562016-04-27 11:05:14 +01001250 return s->parameters.decompress_threads;
Liang Li3fcb38c2015-03-23 16:32:18 +08001251}
1252
Juan Quintelab05dc722015-07-07 14:44:05 +02001253bool migrate_use_events(void)
1254{
1255 MigrationState *s;
1256
1257 s = migrate_get_current();
1258
1259 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1260}
1261
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001262int migrate_use_xbzrle(void)
1263{
1264 MigrationState *s;
1265
1266 s = migrate_get_current();
1267
1268 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1269}
1270
1271int64_t migrate_xbzrle_cache_size(void)
1272{
1273 MigrationState *s;
1274
1275 s = migrate_get_current();
1276
1277 return s->xbzrle_cache_size;
1278}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001279
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001280/* migration thread support */
1281/*
1282 * Something bad happened to the RP stream, mark an error
1283 * The caller shall print or trace something to indicate why
1284 */
1285static void mark_source_rp_bad(MigrationState *s)
1286{
1287 s->rp_state.error = true;
1288}
1289
1290static struct rp_cmd_args {
1291 ssize_t len; /* -1 = variable */
1292 const char *name;
1293} rp_cmd_args[] = {
1294 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1295 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1296 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001297 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1298 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001299 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1300};
1301
1302/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001303 * Process a request for pages received on the return path,
1304 * We're allowed to send more than requested (e.g. to round to our page size)
1305 * and we don't need to send pages that have already been sent.
1306 */
1307static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1308 ram_addr_t start, size_t len)
1309{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001310 long our_host_ps = getpagesize();
1311
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001312 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001313
1314 /*
1315 * Since we currently insist on matching page sizes, just sanity check
1316 * we're being asked for whole host pages.
1317 */
1318 if (start & (our_host_ps-1) ||
1319 (len & (our_host_ps-1))) {
1320 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1321 " len: %zd", __func__, start, len);
1322 mark_source_rp_bad(ms);
1323 return;
1324 }
1325
1326 if (ram_save_queue_pages(ms, rbname, start, len)) {
1327 mark_source_rp_bad(ms);
1328 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001329}
1330
1331/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001332 * Handles messages sent on the return path towards the source VM
1333 *
1334 */
1335static void *source_return_path_thread(void *opaque)
1336{
1337 MigrationState *ms = opaque;
1338 QEMUFile *rp = ms->rp_state.from_dst_file;
1339 uint16_t header_len, header_type;
Peter Xu568b01c2016-03-09 14:12:12 +08001340 uint8_t buf[512];
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001341 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001342 ram_addr_t start = 0; /* =0 to silence warning */
1343 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001344 int res;
1345
1346 trace_source_return_path_thread_entry();
1347 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1348 migration_is_setup_or_active(ms->state)) {
1349 trace_source_return_path_thread_loop_top();
1350 header_type = qemu_get_be16(rp);
1351 header_len = qemu_get_be16(rp);
1352
1353 if (header_type >= MIG_RP_MSG_MAX ||
1354 header_type == MIG_RP_MSG_INVALID) {
1355 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1356 header_type, header_len);
1357 mark_source_rp_bad(ms);
1358 goto out;
1359 }
1360
1361 if ((rp_cmd_args[header_type].len != -1 &&
1362 header_len != rp_cmd_args[header_type].len) ||
Peter Xu568b01c2016-03-09 14:12:12 +08001363 header_len > sizeof(buf)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001364 error_report("RP: Received '%s' message (0x%04x) with"
1365 "incorrect length %d expecting %zu",
1366 rp_cmd_args[header_type].name, header_type, header_len,
1367 (size_t)rp_cmd_args[header_type].len);
1368 mark_source_rp_bad(ms);
1369 goto out;
1370 }
1371
1372 /* We know we've got a valid header by this point */
1373 res = qemu_get_buffer(rp, buf, header_len);
1374 if (res != header_len) {
1375 error_report("RP: Failed reading data for message 0x%04x"
1376 " read %d expected %d",
1377 header_type, res, header_len);
1378 mark_source_rp_bad(ms);
1379 goto out;
1380 }
1381
1382 /* OK, we have the message and the data */
1383 switch (header_type) {
1384 case MIG_RP_MSG_SHUT:
Peter Maydell4d885132016-06-10 17:09:22 +01001385 sibling_error = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001386 trace_source_return_path_thread_shut(sibling_error);
1387 if (sibling_error) {
1388 error_report("RP: Sibling indicated error %d", sibling_error);
1389 mark_source_rp_bad(ms);
1390 }
1391 /*
1392 * We'll let the main thread deal with closing the RP
1393 * we could do a shutdown(2) on it, but we're the only user
1394 * anyway, so there's nothing gained.
1395 */
1396 goto out;
1397
1398 case MIG_RP_MSG_PONG:
Peter Maydell4d885132016-06-10 17:09:22 +01001399 tmp32 = ldl_be_p(buf);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001400 trace_source_return_path_thread_pong(tmp32);
1401 break;
1402
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001403 case MIG_RP_MSG_REQ_PAGES:
Peter Maydell4d885132016-06-10 17:09:22 +01001404 start = ldq_be_p(buf);
1405 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001406 migrate_handle_rp_req_pages(ms, NULL, start, len);
1407 break;
1408
1409 case MIG_RP_MSG_REQ_PAGES_ID:
1410 expected_len = 12 + 1; /* header + termination */
1411
1412 if (header_len >= expected_len) {
Peter Maydell4d885132016-06-10 17:09:22 +01001413 start = ldq_be_p(buf);
1414 len = ldl_be_p(buf + 8);
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001415 /* Now we expect an idstr */
1416 tmp32 = buf[12]; /* Length of the following idstr */
1417 buf[13 + tmp32] = '\0';
1418 expected_len += tmp32;
1419 }
1420 if (header_len != expected_len) {
1421 error_report("RP: Req_Page_id with length %d expecting %zd",
1422 header_len, expected_len);
1423 mark_source_rp_bad(ms);
1424 goto out;
1425 }
1426 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
1427 break;
1428
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001429 default:
1430 break;
1431 }
1432 }
Dr. David Alan Gilbert5df54162015-11-18 11:48:41 +00001433 if (qemu_file_get_error(rp)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001434 trace_source_return_path_thread_bad_end();
1435 mark_source_rp_bad(ms);
1436 }
1437
1438 trace_source_return_path_thread_end();
1439out:
1440 ms->rp_state.from_dst_file = NULL;
1441 qemu_fclose(rp);
1442 return NULL;
1443}
1444
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001445static int open_return_path_on_source(MigrationState *ms)
1446{
1447
zhanghailiang89a02a92016-01-15 11:37:42 +08001448 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001449 if (!ms->rp_state.from_dst_file) {
1450 return -1;
1451 }
1452
1453 trace_open_return_path_on_source();
1454 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1455 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1456
1457 trace_open_return_path_on_source_continue();
1458
1459 return 0;
1460}
1461
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001462/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1463static int await_return_path_close_on_source(MigrationState *ms)
1464{
1465 /*
1466 * If this is a normal exit then the destination will send a SHUT and the
1467 * rp_thread will exit, however if there's an error we need to cause
1468 * it to exit.
1469 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001470 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001471 /*
1472 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1473 * waiting for the destination.
1474 */
1475 qemu_file_shutdown(ms->rp_state.from_dst_file);
1476 mark_source_rp_bad(ms);
1477 }
1478 trace_await_return_path_close_on_source_joining();
1479 qemu_thread_join(&ms->rp_state.rp_thread);
1480 trace_await_return_path_close_on_source_close();
1481 return ms->rp_state.error;
1482}
1483
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001484/*
1485 * Switch from normal iteration to postcopy
1486 * Returns non-0 on error
1487 */
1488static int postcopy_start(MigrationState *ms, bool *old_vm_running)
1489{
1490 int ret;
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001491 QIOChannelBuffer *bioc;
1492 QEMUFile *fb;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001493 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
zhanghailiang48781e52015-12-16 11:47:33 +00001494 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001495 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1496
1497 trace_postcopy_start();
1498 qemu_mutex_lock_iothread();
1499 trace_postcopy_start_set_run();
1500
1501 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1502 *old_vm_running = runstate_is_running();
1503 global_state_store();
1504 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001505 if (ret < 0) {
1506 goto fail;
1507 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001508
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001509 ret = bdrv_inactivate_all();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001510 if (ret < 0) {
1511 goto fail;
1512 }
1513
1514 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001515 * Cause any non-postcopiable, but iterative devices to
1516 * send out their final data.
1517 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001518 qemu_savevm_state_complete_precopy(ms->to_dst_file, true);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001519
1520 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001521 * in Finish migrate and with the io-lock held everything should
1522 * be quiet, but we've potentially still got dirty pages and we
1523 * need to tell the destination to throw any pages it's already received
1524 * that are dirty
1525 */
1526 if (ram_postcopy_send_discard_bitmap(ms)) {
1527 error_report("postcopy send discard bitmap failed");
1528 goto fail;
1529 }
1530
1531 /*
1532 * send rest of state - note things that are doing postcopy
1533 * will notice we're in POSTCOPY_ACTIVE and not actually
1534 * wrap their state up here
1535 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001536 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001537 /* Ping just for debugging, helps line traces up */
zhanghailiang89a02a92016-01-15 11:37:42 +08001538 qemu_savevm_send_ping(ms->to_dst_file, 2);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001539
1540 /*
1541 * While loading the device state we may trigger page transfer
1542 * requests and the fd must be free to process those, and thus
1543 * the destination must read the whole device state off the fd before
1544 * it starts processing it. Unfortunately the ad-hoc migration format
1545 * doesn't allow the destination to know the size to read without fully
1546 * parsing it through each devices load-state code (especially the open
1547 * coded devices that use get/put).
1548 * So we wrap the device state up in a package with a length at the start;
1549 * to do this we use a qemu_buf to hold the whole of the device state.
1550 */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001551 bioc = qio_channel_buffer_new(4096);
1552 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
1553 object_unref(OBJECT(bioc));
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001554
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001555 /*
1556 * Make sure the receiver can get incoming pages before we send the rest
1557 * of the state
1558 */
1559 qemu_savevm_send_postcopy_listen(fb);
1560
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001561 qemu_savevm_state_complete_precopy(fb, false);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001562 qemu_savevm_send_ping(fb, 3);
1563
1564 qemu_savevm_send_postcopy_run(fb);
1565
1566 /* <><> end of stuff going into the package */
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001567
1568 /* Now send that blob */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001569 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001570 goto fail_closefb;
1571 }
1572 qemu_fclose(fb);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001573
1574 /* Send a notify to give a chance for anything that needs to happen
1575 * at the transition to postcopy and after the device state; in particular
1576 * spice needs to trigger a transition now
1577 */
1578 ms->postcopy_after_devices = true;
1579 notifier_list_notify(&migration_state_notifiers, ms);
1580
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001581 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
1582
1583 qemu_mutex_unlock_iothread();
1584
1585 /*
1586 * Although this ping is just for debug, it could potentially be
1587 * used for getting a better measurement of downtime at the source.
1588 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001589 qemu_savevm_send_ping(ms->to_dst_file, 4);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001590
zhanghailiang89a02a92016-01-15 11:37:42 +08001591 ret = qemu_file_get_error(ms->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001592 if (ret) {
1593 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00001594 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001595 MIGRATION_STATUS_FAILED);
1596 }
1597
1598 return ret;
1599
1600fail_closefb:
1601 qemu_fclose(fb);
1602fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001603 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001604 MIGRATION_STATUS_FAILED);
1605 qemu_mutex_unlock_iothread();
1606 return -1;
1607}
1608
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001609/**
1610 * migration_completion: Used by migration_thread when there's not much left.
1611 * The caller 'breaks' the loop when this returns.
1612 *
1613 * @s: Current migration state
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001614 * @current_active_state: The migration state we expect to be in
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001615 * @*old_vm_running: Pointer to old_vm_running flag
1616 * @*start_time: Pointer to time to update
1617 */
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001618static void migration_completion(MigrationState *s, int current_active_state,
1619 bool *old_vm_running,
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001620 int64_t *start_time)
1621{
1622 int ret;
1623
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001624 if (s->state == MIGRATION_STATUS_ACTIVE) {
1625 qemu_mutex_lock_iothread();
1626 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1627 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1628 *old_vm_running = runstate_is_running();
1629 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001630
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001631 if (!ret) {
1632 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
1633 if (ret >= 0) {
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001634 ret = bdrv_inactivate_all();
1635 }
1636 if (ret >= 0) {
zhanghailiang89a02a92016-01-15 11:37:42 +08001637 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
1638 qemu_savevm_state_complete_precopy(s->to_dst_file, false);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001639 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001640 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001641 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001642
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001643 if (ret < 0) {
1644 goto fail;
1645 }
1646 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1647 trace_migration_completion_postcopy_end();
1648
zhanghailiang89a02a92016-01-15 11:37:42 +08001649 qemu_savevm_state_complete_postcopy(s->to_dst_file);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001650 trace_migration_completion_postcopy_end_after_complete();
1651 }
1652
1653 /*
1654 * If rp was opened we must clean up the thread before
1655 * cleaning everything else up (since if there are no failures
1656 * it will wait for the destination to send it's status in
1657 * a SHUT command).
1658 * Postcopy opens rp if enabled (even if it's not avtivated)
1659 */
1660 if (migrate_postcopy_ram()) {
1661 int rp_error;
1662 trace_migration_completion_postcopy_end_before_rp();
1663 rp_error = await_return_path_close_on_source(s);
1664 trace_migration_completion_postcopy_end_after_rp(rp_error);
1665 if (rp_error) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02001666 goto fail_invalidate;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001667 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001668 }
1669
zhanghailiang89a02a92016-01-15 11:37:42 +08001670 if (qemu_file_get_error(s->to_dst_file)) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001671 trace_migration_completion_file_err();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001672 goto fail_invalidate;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001673 }
1674
zhanghailiang48781e52015-12-16 11:47:33 +00001675 migrate_set_state(&s->state, current_active_state,
1676 MIGRATION_STATUS_COMPLETED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001677 return;
1678
Greg Kurzfe904ea2016-05-18 15:44:36 +02001679fail_invalidate:
1680 /* If not doing postcopy, vm_start() will be called: let's regain
1681 * control on images.
1682 */
1683 if (s->state == MIGRATION_STATUS_ACTIVE) {
1684 Error *local_err = NULL;
1685
1686 bdrv_invalidate_cache_all(&local_err);
1687 if (local_err) {
1688 error_report_err(local_err);
1689 }
1690 }
1691
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001692fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001693 migrate_set_state(&s->state, current_active_state,
1694 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001695}
1696
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001697/*
1698 * Master migration thread on the source VM.
1699 * It drives the migration and pumps the data down the outgoing channel.
1700 */
Juan Quintela5f496a12013-02-22 17:36:30 +01001701static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001702{
Juan Quintela9848a402012-12-19 09:55:50 +01001703 MigrationState *s = opaque;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001704 /* Used by the bandwidth calcs, updated later */
Alex Blighbc72ad62013-08-21 16:03:08 +01001705 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1706 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001707 int64_t initial_bytes = 0;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001708 int64_t max_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001709 int64_t start_time = initial_time;
Liang Li94f5a432015-11-02 15:37:00 +08001710 int64_t end_time;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001711 bool old_vm_running = false;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001712 bool entered_postcopy = false;
1713 /* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */
1714 enum MigrationStatus current_active_state = MIGRATION_STATUS_ACTIVE;
Juan Quintela76f59332012-10-03 20:16:24 +02001715
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001716 rcu_register_thread();
1717
zhanghailiang89a02a92016-01-15 11:37:42 +08001718 qemu_savevm_state_header(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001719
1720 if (migrate_postcopy_ram()) {
1721 /* Now tell the dest that it should open its end so it can reply */
zhanghailiang89a02a92016-01-15 11:37:42 +08001722 qemu_savevm_send_open_return_path(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001723
1724 /* And do a ping that will make stuff easier to debug */
zhanghailiang89a02a92016-01-15 11:37:42 +08001725 qemu_savevm_send_ping(s->to_dst_file, 1);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001726
1727 /*
1728 * Tell the destination that we *might* want to do postcopy later;
1729 * if the other end can't do postcopy it should fail now, nice and
1730 * early.
1731 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001732 qemu_savevm_send_postcopy_advise(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001733 }
1734
zhanghailiang89a02a92016-01-15 11:37:42 +08001735 qemu_savevm_state_begin(s->to_dst_file, &s->params);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001736
Alex Blighbc72ad62013-08-21 16:03:08 +01001737 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001738 current_active_state = MIGRATION_STATUS_ACTIVE;
zhanghailiang48781e52015-12-16 11:47:33 +00001739 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1740 MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001741
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001742 trace_migration_thread_setup_complete();
1743
1744 while (s->state == MIGRATION_STATUS_ACTIVE ||
1745 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +01001746 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +02001747 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001748
zhanghailiang89a02a92016-01-15 11:37:42 +08001749 if (!qemu_file_rate_limit(s->to_dst_file)) {
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001750 uint64_t pend_post, pend_nonpost;
1751
zhanghailiang89a02a92016-01-15 11:37:42 +08001752 qemu_savevm_state_pending(s->to_dst_file, max_size, &pend_nonpost,
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001753 &pend_post);
1754 pending_size = pend_nonpost + pend_post;
1755 trace_migrate_pending(pending_size, max_size,
1756 pend_post, pend_nonpost);
Juan Quintelab22ff1f2012-10-17 21:06:31 +02001757 if (pending_size && pending_size >= max_size) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001758 /* Still a significant amount to transfer */
1759
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001760 if (migrate_postcopy_ram() &&
1761 s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE &&
1762 pend_nonpost <= max_size &&
1763 atomic_read(&s->start_postcopy)) {
1764
1765 if (!postcopy_start(s, &old_vm_running)) {
1766 current_active_state = MIGRATION_STATUS_POSTCOPY_ACTIVE;
1767 entered_postcopy = true;
1768 }
1769
1770 continue;
1771 }
1772 /* Just another iteration step */
zhanghailiang89a02a92016-01-15 11:37:42 +08001773 qemu_savevm_state_iterate(s->to_dst_file, entered_postcopy);
Juan Quintelac369f402012-10-03 20:33:34 +02001774 } else {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001775 trace_migration_thread_low_pending(pending_size);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001776 migration_completion(s, current_active_state,
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001777 &old_vm_running, &start_time);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001778 break;
Juan Quintelac369f402012-10-03 20:33:34 +02001779 }
1780 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001781
zhanghailiang89a02a92016-01-15 11:37:42 +08001782 if (qemu_file_get_error(s->to_dst_file)) {
zhanghailiang48781e52015-12-16 11:47:33 +00001783 migrate_set_state(&s->state, current_active_state,
1784 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001785 trace_migration_thread_file_err();
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01001786 break;
1787 }
Alex Blighbc72ad62013-08-21 16:03:08 +01001788 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001789 if (current_time >= initial_time + BUFFER_DELAY) {
zhanghailiang89a02a92016-01-15 11:37:42 +08001790 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file) -
1791 initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -05001792 uint64_t time_spent = current_time - initial_time;
Paolo Bonzinia694ee32015-01-26 12:12:27 +01001793 double bandwidth = (double)transferred_bytes / time_spent;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001794 max_size = bandwidth * migrate_max_downtime() / 1000000;
1795
Wei Yang5b648de2016-01-24 14:09:57 +00001796 s->mbps = (((double) transferred_bytes * 8.0) /
1797 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
Michael R. Hines7e114f82013-06-25 21:35:30 -04001798
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001799 trace_migrate_transferred(transferred_bytes, time_spent,
1800 bandwidth, max_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +01001801 /* if we haven't sent anything, we don't want to recalculate
1802 10000 is a small enough number for our purposes */
1803 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
1804 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
1805 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001806
zhanghailiang89a02a92016-01-15 11:37:42 +08001807 qemu_file_reset_rate_limit(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001808 initial_time = current_time;
zhanghailiang89a02a92016-01-15 11:37:42 +08001809 initial_bytes = qemu_ftell(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001810 }
zhanghailiang89a02a92016-01-15 11:37:42 +08001811 if (qemu_file_rate_limit(s->to_dst_file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001812 /* usleep expects microseconds */
1813 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
1814 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001815 }
1816
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001817 trace_migration_thread_after_loop();
Jason J. Herne070afca2015-09-08 13:12:35 -04001818 /* If we enabled cpu throttling for auto-converge, turn it off. */
1819 cpu_throttle_stop();
Liang Li94f5a432015-11-02 15:37:00 +08001820 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jason J. Herne070afca2015-09-08 13:12:35 -04001821
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001822 qemu_mutex_lock_iothread();
Liang Liea7415f2015-11-02 15:37:01 +08001823 qemu_savevm_state_cleanup();
zhanghailiang31194732015-03-13 16:08:38 +08001824 if (s->state == MIGRATION_STATUS_COMPLETED) {
zhanghailiang89a02a92016-01-15 11:37:42 +08001825 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001826 s->total_time = end_time - s->total_time;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001827 if (!entered_postcopy) {
1828 s->downtime = end_time - start_time;
1829 }
Peter Lievend6ed7312014-05-12 10:46:00 +02001830 if (s->total_time) {
1831 s->mbps = (((double) transferred_bytes * 8.0) /
1832 ((double) s->total_time)) / 1000;
1833 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001834 runstate_set(RUN_STATE_POSTMIGRATE);
1835 } else {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001836 if (old_vm_running && !entered_postcopy) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001837 vm_start();
Dr. David Alan Gilbert42da5552016-07-15 17:44:46 +01001838 } else {
1839 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
1840 runstate_set(RUN_STATE_POSTMIGRATE);
1841 }
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001842 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001843 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001844 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001845 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001846
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001847 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001848 return NULL;
1849}
1850
Juan Quintela9848a402012-12-19 09:55:50 +01001851void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001852{
Juan Quintelacc283e32013-02-01 11:12:26 +01001853 /* This is a best 1st approximation. ns to ms */
1854 s->expected_downtime = max_downtime/1000000;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001855 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001856
Daniel P. Berrange9e4d2b92016-04-27 11:04:57 +01001857 qemu_file_set_blocking(s->to_dst_file, true);
zhanghailiang89a02a92016-01-15 11:37:42 +08001858 qemu_file_set_rate_limit(s->to_dst_file,
Paolo Bonzini442773c2013-02-22 17:36:44 +01001859 s->bandwidth_limit / XFER_LIMIT_RATIO);
1860
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02001861 /* Notify before starting migration thread */
1862 notifier_list_notify(&migration_state_notifiers, s);
1863
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001864 /*
1865 * Open the return path; currently for postcopy but other things might
1866 * also want it.
1867 */
1868 if (migrate_postcopy_ram()) {
1869 if (open_return_path_on_source(s)) {
1870 error_report("Unable to open return-path for postcopy");
zhanghailiang48781e52015-12-16 11:47:33 +00001871 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001872 MIGRATION_STATUS_FAILED);
1873 migrate_fd_cleanup(s);
1874 return;
1875 }
1876 }
1877
Liang Li8706d2d2015-03-23 16:32:17 +08001878 migrate_compress_threads_create();
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001879 qemu_thread_create(&s->thread, "migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001880 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001881 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001882}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001883
1884PostcopyState postcopy_state_get(void)
1885{
1886 return atomic_mb_read(&incoming_postcopy_state);
1887}
1888
1889/* Set the state and return the old state */
1890PostcopyState postcopy_state_set(PostcopyState new_state)
1891{
1892 return atomic_xchg(&incoming_postcopy_state, new_state);
1893}
1894