blob: ac7790f8afc237d4a6be57982cd63616fe13b036 [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"
aliguori065e2812008-11-11 16:46:33 +000037
Jason J. Hernedc325622015-09-08 13:12:37 -040038#define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000039
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010040/* Amount of time to allocate to each "chunk" of bandwidth-throttled
41 * data. */
42#define BUFFER_DELAY 100
43#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
44
Liang Li8706d2d2015-03-23 16:32:17 +080045/* Default compression thread count */
46#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080047/* Default decompression thread count, usually decompression is at
48 * least 4 times as fast as compression.*/
49#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080050/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
51#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
Jason J. Herne1626fee2015-09-08 13:12:34 -040052/* Define default autoconverge cpu throttle migration parameters */
Jason J. Herned85a31d2016-04-21 14:07:18 -040053#define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
54#define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
Liang Li8706d2d2015-03-23 16:32:17 +080055
Orit Wasserman17ad9b32012-08-06 21:42:53 +030056/* Migration XBZRLE default cache size */
57#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
58
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010059static NotifierList migration_state_notifiers =
60 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
61
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000062static bool deferred_incoming;
63
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +000064/*
65 * Current state of incoming postcopy; note this is not part of
66 * MigrationIncomingState since it's state is used during cleanup
67 * at the end as MIS is being freed.
68 */
69static PostcopyState incoming_postcopy_state;
70
Juan Quintela17549e82011-10-05 13:50:43 +020071/* When we add fault tolerance, we could have several
72 migrations at once. For now we don't need to add
73 dynamic creation of migration */
74
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010075/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +020076MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +020077{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000078 static bool once;
Juan Quintela17549e82011-10-05 13:50:43 +020079 static MigrationState current_migration = {
zhanghailiang31194732015-03-13 16:08:38 +080080 .state = MIGRATION_STATUS_NONE,
Juan Quintelad0ae46c2011-02-23 00:33:19 +010081 .bandwidth_limit = MAX_THROTTLE,
Orit Wasserman17ad9b32012-08-06 21:42:53 +030082 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
Michael R. Hines7e114f82013-06-25 21:35:30 -040083 .mbps = -1,
Liang Li43c60a82015-03-23 16:32:27 +080084 .parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] =
85 DEFAULT_MIGRATE_COMPRESS_LEVEL,
86 .parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
87 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
88 .parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
89 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
Jason J. Herned85a31d2016-04-21 14:07:18 -040090 .parameters[MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL] =
91 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL,
92 .parameters[MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT] =
93 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT,
Juan Quintela17549e82011-10-05 13:50:43 +020094 };
95
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +000096 if (!once) {
97 qemu_mutex_init(&current_migration.src_page_req_mutex);
98 once = true;
99 }
Juan Quintela17549e82011-10-05 13:50:43 +0200100 return &current_migration;
101}
102
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100103/* For incoming */
104static MigrationIncomingState *mis_current;
105
106MigrationIncomingState *migration_incoming_get_current(void)
107{
108 return mis_current;
109}
110
111MigrationIncomingState *migration_incoming_state_new(QEMUFile* f)
112{
Markus Armbruster97f3ad32015-09-14 13:51:31 +0200113 mis_current = g_new0(MigrationIncomingState, 1);
Dr. David Alan Gilbert42e2aa52015-11-05 18:10:34 +0000114 mis_current->from_src_file = f;
zhanghailiang93d7af62015-12-16 11:47:34 +0000115 mis_current->state = MIGRATION_STATUS_NONE;
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +0100116 QLIST_INIT(&mis_current->loadvm_handlers);
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000117 qemu_mutex_init(&mis_current->rp_mutex);
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +0000118 qemu_event_init(&mis_current->main_thread_load_event, false);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100119
120 return mis_current;
121}
122
123void migration_incoming_state_destroy(void)
124{
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +0000125 qemu_event_destroy(&mis_current->main_thread_load_event);
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +0100126 loadvm_free_handlers(mis_current);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100127 g_free(mis_current);
128 mis_current = NULL;
129}
130
Juan Quinteladf4b1022014-10-08 10:58:10 +0200131
132typedef struct {
Juan Quintela13d16812014-10-08 13:58:24 +0200133 bool optional;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200134 uint32_t size;
135 uint8_t runstate[100];
Juan Quintela172c4352015-07-08 13:56:26 +0200136 RunState state;
137 bool received;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200138} GlobalState;
139
140static GlobalState global_state;
141
Juan Quintela560d0272015-07-15 09:53:46 +0200142int global_state_store(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200143{
144 if (!runstate_store((char *)global_state.runstate,
145 sizeof(global_state.runstate))) {
146 error_report("runstate name too big: %s", global_state.runstate);
147 trace_migrate_state_too_big();
148 return -EINVAL;
149 }
150 return 0;
151}
152
Anthony PERARDc69adea2015-08-03 15:29:19 +0100153void global_state_store_running(void)
154{
155 const char *state = RunState_lookup[RUN_STATE_RUNNING];
156 strncpy((char *)global_state.runstate,
157 state, sizeof(global_state.runstate));
158}
159
Juan Quintela172c4352015-07-08 13:56:26 +0200160static bool global_state_received(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200161{
Juan Quintela172c4352015-07-08 13:56:26 +0200162 return global_state.received;
163}
164
165static RunState global_state_get_runstate(void)
166{
167 return global_state.state;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200168}
169
Juan Quintela13d16812014-10-08 13:58:24 +0200170void global_state_set_optional(void)
171{
172 global_state.optional = true;
173}
174
175static bool global_state_needed(void *opaque)
176{
177 GlobalState *s = opaque;
178 char *runstate = (char *)s->runstate;
179
180 /* If it is not optional, it is mandatory */
181
182 if (s->optional == false) {
183 return true;
184 }
185
186 /* If state is running or paused, it is not needed */
187
188 if (strcmp(runstate, "running") == 0 ||
189 strcmp(runstate, "paused") == 0) {
190 return false;
191 }
192
193 /* for any other state it is needed */
194 return true;
195}
196
Juan Quinteladf4b1022014-10-08 10:58:10 +0200197static int global_state_post_load(void *opaque, int version_id)
198{
199 GlobalState *s = opaque;
Juan Quintela172c4352015-07-08 13:56:26 +0200200 Error *local_err = NULL;
201 int r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200202 char *runstate = (char *)s->runstate;
203
Juan Quintela172c4352015-07-08 13:56:26 +0200204 s->received = true;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200205 trace_migrate_global_state_post_load(runstate);
206
Eric Blake7fb1cf12015-11-18 01:52:57 -0700207 r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE__MAX,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200208 -1, &local_err);
209
Juan Quintela172c4352015-07-08 13:56:26 +0200210 if (r == -1) {
211 if (local_err) {
212 error_report_err(local_err);
Juan Quinteladf4b1022014-10-08 10:58:10 +0200213 }
Juan Quintela172c4352015-07-08 13:56:26 +0200214 return -EINVAL;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200215 }
Juan Quintela172c4352015-07-08 13:56:26 +0200216 s->state = r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200217
Juan Quintela172c4352015-07-08 13:56:26 +0200218 return 0;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200219}
220
221static void global_state_pre_save(void *opaque)
222{
223 GlobalState *s = opaque;
224
225 trace_migrate_global_state_pre_save((char *)s->runstate);
226 s->size = strlen((char *)s->runstate) + 1;
227}
228
229static const VMStateDescription vmstate_globalstate = {
230 .name = "globalstate",
231 .version_id = 1,
232 .minimum_version_id = 1,
233 .post_load = global_state_post_load,
234 .pre_save = global_state_pre_save,
Juan Quintela13d16812014-10-08 13:58:24 +0200235 .needed = global_state_needed,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200236 .fields = (VMStateField[]) {
237 VMSTATE_UINT32(size, GlobalState),
238 VMSTATE_BUFFER(runstate, GlobalState),
239 VMSTATE_END_OF_LIST()
240 },
241};
242
243void register_global_state(void)
244{
245 /* We would use it independently that we receive it */
246 strcpy((char *)&global_state.runstate, "");
Juan Quintela172c4352015-07-08 13:56:26 +0200247 global_state.received = false;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200248 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
249}
250
Juan Quintelab05dc722015-07-07 14:44:05 +0200251static void migrate_generate_event(int new_state)
252{
253 if (migrate_use_events()) {
254 qapi_event_send_migration(new_state, &error_abort);
Juan Quintelab05dc722015-07-07 14:44:05 +0200255 }
256}
257
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000258/*
259 * Called on -incoming with a defer: uri.
260 * The migration can be started later after any parameters have been
261 * changed.
262 */
263static void deferred_incoming_migration(Error **errp)
264{
265 if (deferred_incoming) {
266 error_setg(errp, "Incoming migration already deferred");
267 }
268 deferred_incoming = true;
269}
270
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000271/* Request a range of pages from the source VM at the given
272 * start address.
273 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
274 * as the last request (a name must have been given previously)
275 * Start: Address offset within the RB
276 * Len: Length in bytes required - must be a multiple of pagesize
277 */
278void migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
279 ram_addr_t start, size_t len)
280{
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100281 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +0000282 size_t msglen = 12; /* start + len */
283
284 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
285 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
286
287 if (rbname) {
288 int rbname_len = strlen(rbname);
289 assert(rbname_len < 256);
290
291 bufc[msglen++] = rbname_len;
292 memcpy(bufc + msglen, rbname, rbname_len);
293 msglen += rbname_len;
294 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES_ID, msglen, bufc);
295 } else {
296 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES, msglen, bufc);
297 }
298}
299
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200300void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000301{
aliguori34c9dd82008-10-13 03:14:31 +0000302 const char *p;
303
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200304 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000305 if (!strcmp(uri, "defer")) {
306 deferred_incoming_migration(errp);
307 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200308 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400309#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000310 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400311 rdma_start_incoming_migration(p, errp);
312#endif
aliguori065e2812008-11-11 16:46:33 +0000313#if !defined(WIN32)
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000314 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200315 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000316 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200317 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000318 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200319 fd_start_incoming_migration(p, errp);
aliguori065e2812008-11-11 16:46:33 +0000320#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000321 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100322 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200323 }
aliguori5bb79102008-10-13 03:12:02 +0000324}
325
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300326static void process_incoming_migration_bh(void *opaque)
327{
328 Error *local_err = NULL;
329 MigrationIncomingState *mis = opaque;
330
331 /* Make sure all file formats flush their mutable metadata */
332 bdrv_invalidate_cache_all(&local_err);
333 if (local_err) {
334 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
335 MIGRATION_STATUS_FAILED);
336 error_report_err(local_err);
337 migrate_decompress_threads_join();
338 exit(EXIT_FAILURE);
339 }
340
341 /*
342 * This must happen after all error conditions are dealt with and
343 * we're sure the VM is going to be running on this host.
344 */
345 qemu_announce_self();
346
347 /* If global state section was not received or we are in running
348 state, we need to obey autostart. Any other state is set with
349 runstate_set. */
350
351 if (!global_state_received() ||
352 global_state_get_runstate() == RUN_STATE_RUNNING) {
353 if (autostart) {
354 vm_start();
355 } else {
356 runstate_set(RUN_STATE_PAUSED);
357 }
358 } else {
359 runstate_set(global_state_get_runstate());
360 }
361 migrate_decompress_threads_join();
362 /*
363 * This must happen after any state changes since as soon as an external
364 * observer sees this event they might start to prod at the VM assuming
365 * it's ready to use.
366 */
367 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
368 MIGRATION_STATUS_COMPLETED);
369 qemu_bh_delete(mis->bh);
370 migration_incoming_state_destroy();
371}
372
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200373static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200374{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200375 QEMUFile *f = opaque;
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000376 MigrationIncomingState *mis;
377 PostcopyState ps;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200378 int ret;
379
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000380 mis = migration_incoming_state_new(f);
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000381 postcopy_state_set(POSTCOPY_INCOMING_NONE);
zhanghailiang93d7af62015-12-16 11:47:34 +0000382 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
383 MIGRATION_STATUS_ACTIVE);
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200384 ret = qemu_loadvm_state(f);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100385
Dr. David Alan Gilberte9bef232015-11-05 18:11:21 +0000386 ps = postcopy_state_get();
387 trace_process_incoming_migration_co_end(ret, ps);
388 if (ps != POSTCOPY_INCOMING_NONE) {
389 if (ps == POSTCOPY_INCOMING_ADVISE) {
390 /*
391 * Where a migration had postcopy enabled (and thus went to advise)
392 * but managed to complete within the precopy period, we can use
393 * the normal exit.
394 */
395 postcopy_ram_incoming_cleanup(mis);
396 } else if (ret >= 0) {
397 /*
398 * Postcopy was started, cleanup should happen at the end of the
399 * postcopy thread.
400 */
401 trace_process_incoming_migration_co_postcopy_end_main();
402 return;
403 }
404 /* Else if something went wrong then just fall out of the normal exit */
405 }
406
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200407 qemu_fclose(f);
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200408 free_xbzrle_decoded_buf();
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100409
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200410 if (ret < 0) {
zhanghailiang93d7af62015-12-16 11:47:34 +0000411 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
412 MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200413 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800414 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600415 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200416 }
Juan Quintela511c0232010-06-09 14:10:55 +0200417
Denis V. Lunev0aa6aef2016-02-24 11:53:38 +0300418 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
419 qemu_bh_schedule(mis->bh);
Juan Quintela511c0232010-06-09 14:10:55 +0200420}
421
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200422void process_incoming_migration(QEMUFile *f)
423{
424 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200425
Liang Li3fcb38c2015-03-23 16:32:18 +0800426 migrate_decompress_threads_create();
Daniel P. Berrange06ad5132016-04-27 11:04:56 +0100427 qemu_file_set_blocking(f, false);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200428 qemu_coroutine_enter(co, f);
429}
430
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +0000431/*
432 * Send a message on the return channel back to the source
433 * of the migration.
434 */
435void migrate_send_rp_message(MigrationIncomingState *mis,
436 enum mig_rp_message_type message_type,
437 uint16_t len, void *data)
438{
439 trace_migrate_send_rp_message((int)message_type, len);
440 qemu_mutex_lock(&mis->rp_mutex);
441 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
442 qemu_put_be16(mis->to_src_file, len);
443 qemu_put_buffer(mis->to_src_file, data, len);
444 qemu_fflush(mis->to_src_file);
445 qemu_mutex_unlock(&mis->rp_mutex);
446}
447
448/*
449 * Send a 'SHUT' message on the return channel with the given value
450 * to indicate that we've finished with the RP. Non-0 value indicates
451 * error.
452 */
453void migrate_send_rp_shut(MigrationIncomingState *mis,
454 uint32_t value)
455{
456 uint32_t buf;
457
458 buf = cpu_to_be32(value);
459 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
460}
461
462/*
463 * Send a 'PONG' message on the return channel with the given value
464 * (normally in response to a 'PING')
465 */
466void migrate_send_rp_pong(MigrationIncomingState *mis,
467 uint32_t value)
468{
469 uint32_t buf;
470
471 buf = cpu_to_be32(value);
472 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
473}
474
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400475/* amount of nanoseconds we are willing to wait for migration to be down.
476 * the choice of nanoseconds is because it is the maximum resolution that
477 * get_clock() can achieve. It is an internal measure. All user-visible
478 * units must be in seconds */
Alexey Kardashevskiyf7cd55a2014-03-27 14:57:26 +1100479static uint64_t max_downtime = 300000000;
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400480
481uint64_t migrate_max_downtime(void)
482{
483 return max_downtime;
484}
485
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300486MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
487{
488 MigrationCapabilityStatusList *head = NULL;
489 MigrationCapabilityStatusList *caps;
490 MigrationState *s = migrate_get_current();
491 int i;
492
Michael Tokarev387eede2013-10-05 13:18:28 +0400493 caps = NULL; /* silence compiler warning */
Eric Blake7fb1cf12015-11-18 01:52:57 -0700494 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300495 if (head == NULL) {
496 head = g_malloc0(sizeof(*caps));
497 caps = head;
498 } else {
499 caps->next = g_malloc0(sizeof(*caps));
500 caps = caps->next;
501 }
502 caps->value =
503 g_malloc(sizeof(*caps->value));
504 caps->value->capability = i;
505 caps->value->state = s->enabled_capabilities[i];
506 }
507
508 return head;
509}
510
Liang Li85de8322015-03-23 16:32:28 +0800511MigrationParameters *qmp_query_migrate_parameters(Error **errp)
512{
513 MigrationParameters *params;
514 MigrationState *s = migrate_get_current();
515
516 params = g_malloc0(sizeof(*params));
517 params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
518 params->compress_threads =
519 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
520 params->decompress_threads =
521 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Jason J. Herned85a31d2016-04-21 14:07:18 -0400522 params->cpu_throttle_initial =
523 s->parameters[MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL];
524 params->cpu_throttle_increment =
525 s->parameters[MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT];
Liang Li85de8322015-03-23 16:32:28 +0800526
527 return params;
528}
529
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000530/*
531 * Return true if we're already in the middle of a migration
532 * (i.e. any of the active or setup states)
533 */
534static bool migration_is_setup_or_active(int state)
535{
536 switch (state) {
537 case MIGRATION_STATUS_ACTIVE:
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000538 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000539 case MIGRATION_STATUS_SETUP:
540 return true;
541
542 default:
543 return false;
544
545 }
546}
547
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300548static void get_xbzrle_cache_stats(MigrationInfo *info)
549{
550 if (migrate_use_xbzrle()) {
551 info->has_xbzrle_cache = true;
552 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
553 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
554 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
555 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
556 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
ChenLiang8bc39232014-04-04 17:57:56 +0800557 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300558 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
559 }
560}
561
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300562MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000563{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300564 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200565 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000566
Juan Quintela17549e82011-10-05 13:50:43 +0200567 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800568 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200569 /* no migration has happened ever */
570 break;
zhanghailiang31194732015-03-13 16:08:38 +0800571 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400572 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400573 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400574 break;
zhanghailiang31194732015-03-13 16:08:38 +0800575 case MIGRATION_STATUS_ACTIVE:
576 case MIGRATION_STATUS_CANCELLING:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300577 info->has_status = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200578 info->has_total_time = true;
Alex Blighbc72ad62013-08-21 16:03:08 +0100579 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Juan Quintela7aa939a2012-08-18 13:17:10 +0200580 - s->total_time;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200581 info->has_expected_downtime = true;
582 info->expected_downtime = s->expected_downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400583 info->has_setup_time = true;
584 info->setup_time = s->setup_time;
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200585
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300586 info->has_ram = true;
587 info->ram = g_malloc0(sizeof(*info->ram));
588 info->ram->transferred = ram_bytes_transferred();
589 info->ram->remaining = ram_bytes_remaining();
590 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300591 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100592 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300593 info->ram->normal = norm_mig_pages_transferred();
594 info->ram->normal_bytes = norm_mig_bytes_transferred();
Juan Quintela8d017192012-08-13 12:31:25 +0200595 info->ram->dirty_pages_rate = s->dirty_pages_rate;
Michael R. Hines7e114f82013-06-25 21:35:30 -0400596 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800597 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela8d017192012-08-13 12:31:25 +0200598
Juan Quintela17549e82011-10-05 13:50:43 +0200599 if (blk_mig_active()) {
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300600 info->has_disk = true;
601 info->disk = g_malloc0(sizeof(*info->disk));
602 info->disk->transferred = blk_mig_bytes_transferred();
603 info->disk->remaining = blk_mig_bytes_remaining();
604 info->disk->total = blk_mig_bytes_total();
aliguoriff8d81d2008-10-24 22:10:31 +0000605 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300606
Jason J. Herne47828932015-09-08 13:12:36 -0400607 if (cpu_throttle_active()) {
Jason J. Herned85a31d2016-04-21 14:07:18 -0400608 info->has_cpu_throttle_percentage = true;
609 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
Jason J. Herne47828932015-09-08 13:12:36 -0400610 }
611
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300612 get_xbzrle_cache_stats(info);
Juan Quintela17549e82011-10-05 13:50:43 +0200613 break;
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000614 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
615 /* Mostly the same as active; TODO add some postcopy stats */
616 info->has_status = true;
617 info->has_total_time = true;
618 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
619 - s->total_time;
620 info->has_expected_downtime = true;
621 info->expected_downtime = s->expected_downtime;
622 info->has_setup_time = true;
623 info->setup_time = s->setup_time;
624
625 info->has_ram = true;
626 info->ram = g_malloc0(sizeof(*info->ram));
627 info->ram->transferred = ram_bytes_transferred();
628 info->ram->remaining = ram_bytes_remaining();
629 info->ram->total = ram_bytes_total();
630 info->ram->duplicate = dup_mig_pages_transferred();
631 info->ram->skipped = skipped_mig_pages_transferred();
632 info->ram->normal = norm_mig_pages_transferred();
633 info->ram->normal_bytes = norm_mig_bytes_transferred();
634 info->ram->dirty_pages_rate = s->dirty_pages_rate;
635 info->ram->mbps = s->mbps;
Dr. David Alan Gilbert614e8012016-02-25 10:03:51 +0000636 info->ram->dirty_sync_count = s->dirty_sync_count;
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000637
638 if (blk_mig_active()) {
639 info->has_disk = true;
640 info->disk = g_malloc0(sizeof(*info->disk));
641 info->disk->transferred = blk_mig_bytes_transferred();
642 info->disk->remaining = blk_mig_bytes_remaining();
643 info->disk->total = blk_mig_bytes_total();
644 }
645
646 get_xbzrle_cache_stats(info);
647 break;
zhanghailiang31194732015-03-13 16:08:38 +0800648 case MIGRATION_STATUS_COMPLETED:
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300649 get_xbzrle_cache_stats(info);
650
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300651 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900652 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200653 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200654 info->has_downtime = true;
655 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400656 info->has_setup_time = true;
657 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200658
659 info->has_ram = true;
660 info->ram = g_malloc0(sizeof(*info->ram));
661 info->ram->transferred = ram_bytes_transferred();
662 info->ram->remaining = 0;
663 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300664 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100665 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300666 info->ram->normal = norm_mig_pages_transferred();
667 info->ram->normal_bytes = norm_mig_bytes_transferred();
Michael R. Hines7e114f82013-06-25 21:35:30 -0400668 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800669 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela17549e82011-10-05 13:50:43 +0200670 break;
zhanghailiang31194732015-03-13 16:08:38 +0800671 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300672 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200673 break;
zhanghailiang31194732015-03-13 16:08:38 +0800674 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300675 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200676 break;
aliguori5bb79102008-10-13 03:12:02 +0000677 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800678 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300679
680 return info;
aliguori5bb79102008-10-13 03:12:02 +0000681}
682
Orit Wasserman00458432012-08-06 21:42:48 +0300683void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
684 Error **errp)
685{
686 MigrationState *s = migrate_get_current();
687 MigrationCapabilityStatusList *cap;
688
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000689 if (migration_is_setup_or_active(s->state)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100690 error_setg(errp, QERR_MIGRATION_ACTIVE);
Orit Wasserman00458432012-08-06 21:42:48 +0300691 return;
692 }
693
694 for (cap = params; cap; cap = cap->next) {
695 s->enabled_capabilities[cap->value->capability] = cap->value->state;
696 }
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000697
698 if (migrate_postcopy_ram()) {
699 if (migrate_use_compression()) {
700 /* The decompression threads asynchronously write into RAM
701 * rather than use the atomic copies needed to avoid
702 * userfaulting. It should be possible to fix the decompression
703 * threads for compatibility in future.
704 */
705 error_report("Postcopy is not currently compatible with "
706 "compression");
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +0000707 s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM] =
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +0000708 false;
709 }
710 }
Orit Wasserman00458432012-08-06 21:42:48 +0300711}
712
Liang Li85de8322015-03-23 16:32:28 +0800713void qmp_migrate_set_parameters(bool has_compress_level,
714 int64_t compress_level,
715 bool has_compress_threads,
716 int64_t compress_threads,
717 bool has_decompress_threads,
Jason J. Herne1626fee2015-09-08 13:12:34 -0400718 int64_t decompress_threads,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400719 bool has_cpu_throttle_initial,
720 int64_t cpu_throttle_initial,
721 bool has_cpu_throttle_increment,
722 int64_t cpu_throttle_increment, Error **errp)
Liang Li85de8322015-03-23 16:32:28 +0800723{
724 MigrationState *s = migrate_get_current();
725
726 if (has_compress_level && (compress_level < 0 || compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100727 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
728 "is invalid, it should be in the range of 0 to 9");
Liang Li85de8322015-03-23 16:32:28 +0800729 return;
730 }
731 if (has_compress_threads &&
732 (compress_threads < 1 || compress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100733 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
734 "compress_threads",
735 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800736 return;
737 }
738 if (has_decompress_threads &&
739 (decompress_threads < 1 || decompress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100740 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
741 "decompress_threads",
742 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800743 return;
744 }
Jason J. Herned85a31d2016-04-21 14:07:18 -0400745 if (has_cpu_throttle_initial &&
746 (cpu_throttle_initial < 1 || cpu_throttle_initial > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400747 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400748 "cpu_throttle_initial",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400749 "an integer in the range of 1 to 99");
750 }
Jason J. Herned85a31d2016-04-21 14:07:18 -0400751 if (has_cpu_throttle_increment &&
752 (cpu_throttle_increment < 1 || cpu_throttle_increment > 99)) {
Jason J. Herne1626fee2015-09-08 13:12:34 -0400753 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Jason J. Herned85a31d2016-04-21 14:07:18 -0400754 "cpu_throttle_increment",
Jason J. Herne1626fee2015-09-08 13:12:34 -0400755 "an integer in the range of 1 to 99");
756 }
Liang Li85de8322015-03-23 16:32:28 +0800757
758 if (has_compress_level) {
759 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
760 }
761 if (has_compress_threads) {
762 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads;
763 }
764 if (has_decompress_threads) {
765 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
766 decompress_threads;
767 }
Jason J. Herned85a31d2016-04-21 14:07:18 -0400768 if (has_cpu_throttle_initial) {
769 s->parameters[MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL] =
770 cpu_throttle_initial;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400771 }
772
Jason J. Herned85a31d2016-04-21 14:07:18 -0400773 if (has_cpu_throttle_increment) {
774 s->parameters[MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT] =
775 cpu_throttle_increment;
Jason J. Herne1626fee2015-09-08 13:12:34 -0400776 }
Liang Li85de8322015-03-23 16:32:28 +0800777}
778
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000779void qmp_migrate_start_postcopy(Error **errp)
780{
781 MigrationState *s = migrate_get_current();
782
783 if (!migrate_postcopy_ram()) {
Dr. David Alan Gilberta54d3402015-11-12 11:34:44 +0000784 error_setg(errp, "Enable postcopy with migrate_set_capability before"
Dr. David Alan Gilbert4886a1b2015-11-05 18:10:56 +0000785 " the start of migration");
786 return;
787 }
788
789 if (s->state == MIGRATION_STATUS_NONE) {
790 error_setg(errp, "Postcopy must be started after migration has been"
791 " started");
792 return;
793 }
794 /*
795 * we don't error if migration has finished since that would be racy
796 * with issuing this command.
797 */
798 atomic_set(&s->start_postcopy, true);
799}
800
aliguori065e2812008-11-11 16:46:33 +0000801/* shared migration helpers */
802
zhanghailiang48781e52015-12-16 11:47:33 +0000803void migrate_set_state(int *state, int old_state, int new_state)
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000804{
zhanghailiang48781e52015-12-16 11:47:33 +0000805 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
Juan Quintela4ba4bc52015-07-08 13:58:27 +0200806 trace_migrate_set_state(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200807 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000808 }
809}
810
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100811static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000812{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100813 MigrationState *s = opaque;
814
815 qemu_bh_delete(s->cleanup_bh);
816 s->cleanup_bh = NULL;
817
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +0000818 flush_page_queue(s);
819
zhanghailiang89a02a92016-01-15 11:37:42 +0800820 if (s->to_dst_file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100821 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100822 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +0000823 if (s->migration_thread_running) {
824 qemu_thread_join(&s->thread);
825 s->migration_thread_running = false;
826 }
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100827 qemu_mutex_lock_iothread();
828
Liang Li8706d2d2015-03-23 16:32:17 +0800829 migrate_compress_threads_join();
zhanghailiang89a02a92016-01-15 11:37:42 +0800830 qemu_fclose(s->to_dst_file);
831 s->to_dst_file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000832 }
833
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000834 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
835 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100836
Liang Li94f5a432015-11-02 15:37:00 +0800837 if (s->state == MIGRATION_STATUS_CANCELLING) {
zhanghailiang48781e52015-12-16 11:47:33 +0000838 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
Liang Li94f5a432015-11-02 15:37:00 +0800839 MIGRATION_STATUS_CANCELLED);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100840 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100841
842 notifier_list_notify(&migration_state_notifiers, s);
aliguori065e2812008-11-11 16:46:33 +0000843}
844
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200845void migrate_fd_error(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000846{
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100847 trace_migrate_fd_error();
zhanghailiang89a02a92016-01-15 11:37:42 +0800848 assert(s->to_dst_file == NULL);
zhanghailiang48781e52015-12-16 11:47:33 +0000849 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
850 MIGRATION_STATUS_FAILED);
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100851 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela458cf282011-02-22 23:32:54 +0100852}
853
Juan Quintela0edda1c2010-05-11 16:28:39 +0200854static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000855{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000856 int old_state ;
zhanghailiang89a02a92016-01-15 11:37:42 +0800857 QEMUFile *f = migrate_get_current()->to_dst_file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100858 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000859
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +0000860 if (s->rp_state.from_dst_file) {
861 /* shutdown the rp socket, so causing the rp thread to shutdown */
862 qemu_file_shutdown(s->rp_state.from_dst_file);
863 }
864
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000865 do {
866 old_state = s->state;
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +0000867 if (!migration_is_setup_or_active(old_state)) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000868 break;
869 }
zhanghailiang48781e52015-12-16 11:47:33 +0000870 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
zhanghailiang31194732015-03-13 16:08:38 +0800871 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000872
873 /*
874 * If we're unlucky the migration code might be stuck somewhere in a
875 * send/write while the network has failed and is waiting to timeout;
876 * if we've got shutdown(2) available then we can force it to quit.
877 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
878 * called in a bh, so there is no race against this cancel.
879 */
zhanghailiang31194732015-03-13 16:08:38 +0800880 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000881 qemu_file_shutdown(f);
882 }
aliguori065e2812008-11-11 16:46:33 +0000883}
884
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100885void add_migration_state_change_notifier(Notifier *notify)
886{
887 notifier_list_add(&migration_state_notifiers, notify);
888}
889
890void remove_migration_state_change_notifier(Notifier *notify)
891{
Paolo Bonzini31552522012-01-13 17:34:01 +0100892 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100893}
894
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +0200895bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200896{
zhanghailiang31194732015-03-13 16:08:38 +0800897 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200898}
899
Juan Quintela70736932011-02-23 00:43:59 +0100900bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100901{
zhanghailiang31194732015-03-13 16:08:38 +0800902 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100903}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200904
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200905bool migration_has_failed(MigrationState *s)
906{
zhanghailiang31194732015-03-13 16:08:38 +0800907 return (s->state == MIGRATION_STATUS_CANCELLED ||
908 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200909}
910
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +0000911bool migration_in_postcopy(MigrationState *s)
912{
913 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
914}
915
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +0000916bool migration_in_postcopy_after_devices(MigrationState *s)
917{
918 return migration_in_postcopy(s) && s->postcopy_after_devices;
919}
920
Dr. David Alan Gilbertaefeb182015-11-05 18:10:40 +0000921MigrationState *migrate_init(const MigrationParams *params)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200922{
Juan Quintela17549e82011-10-05 13:50:43 +0200923 MigrationState *s = migrate_get_current();
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300924
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000925 /*
926 * Reinitialise all migration state, except
927 * parameters/capabilities that the user set, and
928 * locks.
929 */
930 s->bytes_xfer = 0;
931 s->xfer_limit = 0;
932 s->cleanup_bh = 0;
zhanghailiang89a02a92016-01-15 11:37:42 +0800933 s->to_dst_file = NULL;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000934 s->state = MIGRATION_STATUS_NONE;
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300935 s->params = *params;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000936 s->rp_state.from_dst_file = NULL;
937 s->rp_state.error = false;
938 s->mbps = 0.0;
939 s->downtime = 0;
940 s->expected_downtime = 0;
941 s->dirty_pages_rate = 0;
942 s->dirty_bytes_rate = 0;
943 s->setup_time = 0;
944 s->dirty_sync_count = 0;
945 s->start_postcopy = false;
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +0000946 s->postcopy_after_devices = false;
Dr. David Alan Gilbert389775d2015-11-12 15:38:27 +0000947 s->migration_thread_running = false;
948 s->last_req_rb = NULL;
Juan Quintela1299c632011-11-09 21:29:01 +0100949
zhanghailiang48781e52015-12-16 11:47:33 +0000950 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200951
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +0000952 QSIMPLEQ_INIT(&s->src_page_requests);
953
Alex Blighbc72ad62013-08-21 16:03:08 +0100954 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200955 return s;
956}
Juan Quintelacab30142011-02-22 23:54:21 +0100957
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600958static GSList *migration_blockers;
959
960void migrate_add_blocker(Error *reason)
961{
962 migration_blockers = g_slist_prepend(migration_blockers, reason);
963}
964
965void migrate_del_blocker(Error *reason)
966{
967 migration_blockers = g_slist_remove(migration_blockers, reason);
968}
969
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000970void qmp_migrate_incoming(const char *uri, Error **errp)
971{
972 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000973 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000974
975 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000976 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000977 return;
978 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000979 if (!once) {
980 error_setg(errp, "The incoming migration has already been started");
981 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000982
983 qemu_start_incoming_migration(uri, &local_err);
984
985 if (local_err) {
986 error_propagate(errp, local_err);
987 return;
988 }
989
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000990 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000991}
992
Greg Kurz24f39022016-05-04 21:44:19 +0200993bool migration_is_blocked(Error **errp)
994{
995 if (qemu_savevm_state_blocked(errp)) {
996 return true;
997 }
998
999 if (migration_blockers) {
1000 *errp = error_copy(migration_blockers->data);
1001 return true;
1002 }
1003
1004 return false;
1005}
1006
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001007void qmp_migrate(const char *uri, bool has_blk, bool blk,
1008 bool has_inc, bool inc, bool has_detach, bool detach,
1009 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001010{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +02001011 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +02001012 MigrationState *s = migrate_get_current();
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001013 MigrationParams params;
Juan Quintelacab30142011-02-22 23:54:21 +01001014 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +01001015
Pawit Pornkitprasan8c0426a2013-07-30 08:39:52 +09001016 params.blk = has_blk && blk;
1017 params.shared = has_inc && inc;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001018
Dr. David Alan Gilbertf6844b92015-11-05 18:10:48 +00001019 if (migration_is_setup_or_active(s->state) ||
zhanghailiang31194732015-03-13 16:08:38 +08001020 s->state == MIGRATION_STATUS_CANCELLING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001021 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001022 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001023 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +01001024 if (runstate_check(RUN_STATE_INMIGRATE)) {
1025 error_setg(errp, "Guest is waiting for an incoming migration");
1026 return;
1027 }
1028
Greg Kurz24f39022016-05-04 21:44:19 +02001029 if (migration_is_blocked(errp)) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001030 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -06001031 }
1032
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001033 s = migrate_init(&params);
Juan Quintelacab30142011-02-22 23:54:21 +01001034
1035 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001036 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -04001037#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +08001038 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -04001039 rdma_start_outgoing_migration(s, p, &local_err);
1040#endif
Juan Quintelacab30142011-02-22 23:54:21 +01001041#if !defined(WIN32)
1042 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001043 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001044 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001045 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001046 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001047 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +01001048#endif
1049 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001050 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1051 "a valid migration protocol");
zhanghailiang48781e52015-12-16 11:47:33 +00001052 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1053 MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001054 return;
Juan Quintelacab30142011-02-22 23:54:21 +01001055 }
1056
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001057 if (local_err) {
Paolo Bonzini342ab8d2012-10-02 09:59:38 +02001058 migrate_fd_error(s);
Paolo Bonzinif37afb52012-10-02 10:02:46 +02001059 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001060 return;
Juan Quintela1299c632011-11-09 21:29:01 +01001061 }
Juan Quintelacab30142011-02-22 23:54:21 +01001062}
1063
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02001064void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001065{
Juan Quintela17549e82011-10-05 13:50:43 +02001066 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +01001067}
1068
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001069void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1070{
1071 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +02001072 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001073
1074 /* Check for truncation */
1075 if (value != (size_t)value) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001076 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1077 "exceeding address space");
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001078 return;
1079 }
1080
Orit Wassermana5615b12014-01-30 20:08:36 +02001081 /* Cache should not be larger than guest ram size */
1082 if (value > ram_bytes_total()) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001083 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1084 "exceeds guest ram size ");
Orit Wassermana5615b12014-01-30 20:08:36 +02001085 return;
1086 }
1087
Orit Wassermanc91e6812014-01-30 20:08:34 +02001088 new_size = xbzrle_cache_resize(value);
1089 if (new_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001090 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1091 "is smaller than page size");
Orit Wassermanc91e6812014-01-30 20:08:34 +02001092 return;
1093 }
1094
1095 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001096}
1097
1098int64_t qmp_query_migrate_cache_size(Error **errp)
1099{
1100 return migrate_xbzrle_cache_size();
1101}
1102
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001103void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001104{
Juan Quintelacab30142011-02-22 23:54:21 +01001105 MigrationState *s;
1106
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001107 if (value < 0) {
1108 value = 0;
Juan Quintelacab30142011-02-22 23:54:21 +01001109 }
Paolo Bonzini442773c2013-02-22 17:36:44 +01001110 if (value > SIZE_MAX) {
1111 value = SIZE_MAX;
1112 }
Juan Quintelacab30142011-02-22 23:54:21 +01001113
Juan Quintela17549e82011-10-05 13:50:43 +02001114 s = migrate_get_current();
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001115 s->bandwidth_limit = value;
zhanghailiang89a02a92016-01-15 11:37:42 +08001116 if (s->to_dst_file) {
1117 qemu_file_set_rate_limit(s->to_dst_file,
1118 s->bandwidth_limit / XFER_LIMIT_RATIO);
Paolo Bonzini442773c2013-02-22 17:36:44 +01001119 }
Juan Quintelacab30142011-02-22 23:54:21 +01001120}
1121
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001122void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +01001123{
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001124 value *= 1e9;
1125 value = MAX(0, MIN(UINT64_MAX, value));
1126 max_downtime = (uint64_t)value;
aliguori5bb79102008-10-13 03:12:02 +00001127}
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001128
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001129bool migrate_postcopy_ram(void)
1130{
1131 MigrationState *s;
1132
1133 s = migrate_get_current();
1134
Dr. David Alan Gilbert32c3db52016-03-11 09:53:36 +00001135 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
Dr. David Alan Gilbert53dd3702015-11-05 18:10:51 +00001136}
1137
Chegu Vinodbde1e2e2013-06-24 03:49:42 -06001138bool migrate_auto_converge(void)
1139{
1140 MigrationState *s;
1141
1142 s = migrate_get_current();
1143
1144 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1145}
1146
Peter Lieven323004a2013-07-18 09:48:50 +02001147bool migrate_zero_blocks(void)
1148{
1149 MigrationState *s;
1150
1151 s = migrate_get_current();
1152
1153 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1154}
1155
Liang Li8706d2d2015-03-23 16:32:17 +08001156bool migrate_use_compression(void)
1157{
Liang Lidde4e692015-03-23 16:32:26 +08001158 MigrationState *s;
1159
1160 s = migrate_get_current();
1161
1162 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +08001163}
1164
1165int migrate_compress_level(void)
1166{
1167 MigrationState *s;
1168
1169 s = migrate_get_current();
1170
Liang Li43c60a82015-03-23 16:32:27 +08001171 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
Liang Li8706d2d2015-03-23 16:32:17 +08001172}
1173
1174int migrate_compress_threads(void)
1175{
1176 MigrationState *s;
1177
1178 s = migrate_get_current();
1179
Liang Li43c60a82015-03-23 16:32:27 +08001180 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
Liang Li8706d2d2015-03-23 16:32:17 +08001181}
1182
Liang Li3fcb38c2015-03-23 16:32:18 +08001183int migrate_decompress_threads(void)
1184{
1185 MigrationState *s;
1186
1187 s = migrate_get_current();
1188
Liang Li43c60a82015-03-23 16:32:27 +08001189 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Liang Li3fcb38c2015-03-23 16:32:18 +08001190}
1191
Juan Quintelab05dc722015-07-07 14:44:05 +02001192bool migrate_use_events(void)
1193{
1194 MigrationState *s;
1195
1196 s = migrate_get_current();
1197
1198 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1199}
1200
Orit Wasserman17ad9b32012-08-06 21:42:53 +03001201int migrate_use_xbzrle(void)
1202{
1203 MigrationState *s;
1204
1205 s = migrate_get_current();
1206
1207 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1208}
1209
1210int64_t migrate_xbzrle_cache_size(void)
1211{
1212 MigrationState *s;
1213
1214 s = migrate_get_current();
1215
1216 return s->xbzrle_cache_size;
1217}
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001218
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001219/* migration thread support */
1220/*
1221 * Something bad happened to the RP stream, mark an error
1222 * The caller shall print or trace something to indicate why
1223 */
1224static void mark_source_rp_bad(MigrationState *s)
1225{
1226 s->rp_state.error = true;
1227}
1228
1229static struct rp_cmd_args {
1230 ssize_t len; /* -1 = variable */
1231 const char *name;
1232} rp_cmd_args[] = {
1233 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1234 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1235 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001236 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1237 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001238 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1239};
1240
1241/*
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001242 * Process a request for pages received on the return path,
1243 * We're allowed to send more than requested (e.g. to round to our page size)
1244 * and we don't need to send pages that have already been sent.
1245 */
1246static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1247 ram_addr_t start, size_t len)
1248{
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001249 long our_host_ps = getpagesize();
1250
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001251 trace_migrate_handle_rp_req_pages(rbname, start, len);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001252
1253 /*
1254 * Since we currently insist on matching page sizes, just sanity check
1255 * we're being asked for whole host pages.
1256 */
1257 if (start & (our_host_ps-1) ||
1258 (len & (our_host_ps-1))) {
1259 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1260 " len: %zd", __func__, start, len);
1261 mark_source_rp_bad(ms);
1262 return;
1263 }
1264
1265 if (ram_save_queue_pages(ms, rbname, start, len)) {
1266 mark_source_rp_bad(ms);
1267 }
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001268}
1269
1270/*
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001271 * Handles messages sent on the return path towards the source VM
1272 *
1273 */
1274static void *source_return_path_thread(void *opaque)
1275{
1276 MigrationState *ms = opaque;
1277 QEMUFile *rp = ms->rp_state.from_dst_file;
1278 uint16_t header_len, header_type;
Peter Xu568b01c2016-03-09 14:12:12 +08001279 uint8_t buf[512];
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001280 uint32_t tmp32, sibling_error;
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001281 ram_addr_t start = 0; /* =0 to silence warning */
1282 size_t len = 0, expected_len;
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001283 int res;
1284
1285 trace_source_return_path_thread_entry();
1286 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1287 migration_is_setup_or_active(ms->state)) {
1288 trace_source_return_path_thread_loop_top();
1289 header_type = qemu_get_be16(rp);
1290 header_len = qemu_get_be16(rp);
1291
1292 if (header_type >= MIG_RP_MSG_MAX ||
1293 header_type == MIG_RP_MSG_INVALID) {
1294 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1295 header_type, header_len);
1296 mark_source_rp_bad(ms);
1297 goto out;
1298 }
1299
1300 if ((rp_cmd_args[header_type].len != -1 &&
1301 header_len != rp_cmd_args[header_type].len) ||
Peter Xu568b01c2016-03-09 14:12:12 +08001302 header_len > sizeof(buf)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001303 error_report("RP: Received '%s' message (0x%04x) with"
1304 "incorrect length %d expecting %zu",
1305 rp_cmd_args[header_type].name, header_type, header_len,
1306 (size_t)rp_cmd_args[header_type].len);
1307 mark_source_rp_bad(ms);
1308 goto out;
1309 }
1310
1311 /* We know we've got a valid header by this point */
1312 res = qemu_get_buffer(rp, buf, header_len);
1313 if (res != header_len) {
1314 error_report("RP: Failed reading data for message 0x%04x"
1315 " read %d expected %d",
1316 header_type, res, header_len);
1317 mark_source_rp_bad(ms);
1318 goto out;
1319 }
1320
1321 /* OK, we have the message and the data */
1322 switch (header_type) {
1323 case MIG_RP_MSG_SHUT:
1324 sibling_error = be32_to_cpup((uint32_t *)buf);
1325 trace_source_return_path_thread_shut(sibling_error);
1326 if (sibling_error) {
1327 error_report("RP: Sibling indicated error %d", sibling_error);
1328 mark_source_rp_bad(ms);
1329 }
1330 /*
1331 * We'll let the main thread deal with closing the RP
1332 * we could do a shutdown(2) on it, but we're the only user
1333 * anyway, so there's nothing gained.
1334 */
1335 goto out;
1336
1337 case MIG_RP_MSG_PONG:
1338 tmp32 = be32_to_cpup((uint32_t *)buf);
1339 trace_source_return_path_thread_pong(tmp32);
1340 break;
1341
Dr. David Alan Gilbert1e2d90e2015-11-05 18:11:07 +00001342 case MIG_RP_MSG_REQ_PAGES:
1343 start = be64_to_cpup((uint64_t *)buf);
1344 len = be32_to_cpup((uint32_t *)(buf + 8));
1345 migrate_handle_rp_req_pages(ms, NULL, start, len);
1346 break;
1347
1348 case MIG_RP_MSG_REQ_PAGES_ID:
1349 expected_len = 12 + 1; /* header + termination */
1350
1351 if (header_len >= expected_len) {
1352 start = be64_to_cpup((uint64_t *)buf);
1353 len = be32_to_cpup((uint32_t *)(buf + 8));
1354 /* Now we expect an idstr */
1355 tmp32 = buf[12]; /* Length of the following idstr */
1356 buf[13 + tmp32] = '\0';
1357 expected_len += tmp32;
1358 }
1359 if (header_len != expected_len) {
1360 error_report("RP: Req_Page_id with length %d expecting %zd",
1361 header_len, expected_len);
1362 mark_source_rp_bad(ms);
1363 goto out;
1364 }
1365 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
1366 break;
1367
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001368 default:
1369 break;
1370 }
1371 }
Dr. David Alan Gilbert5df54162015-11-18 11:48:41 +00001372 if (qemu_file_get_error(rp)) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001373 trace_source_return_path_thread_bad_end();
1374 mark_source_rp_bad(ms);
1375 }
1376
1377 trace_source_return_path_thread_end();
1378out:
1379 ms->rp_state.from_dst_file = NULL;
1380 qemu_fclose(rp);
1381 return NULL;
1382}
1383
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001384static int open_return_path_on_source(MigrationState *ms)
1385{
1386
zhanghailiang89a02a92016-01-15 11:37:42 +08001387 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001388 if (!ms->rp_state.from_dst_file) {
1389 return -1;
1390 }
1391
1392 trace_open_return_path_on_source();
1393 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1394 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1395
1396 trace_open_return_path_on_source_continue();
1397
1398 return 0;
1399}
1400
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001401/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1402static int await_return_path_close_on_source(MigrationState *ms)
1403{
1404 /*
1405 * If this is a normal exit then the destination will send a SHUT and the
1406 * rp_thread will exit, however if there's an error we need to cause
1407 * it to exit.
1408 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001409 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001410 /*
1411 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1412 * waiting for the destination.
1413 */
1414 qemu_file_shutdown(ms->rp_state.from_dst_file);
1415 mark_source_rp_bad(ms);
1416 }
1417 trace_await_return_path_close_on_source_joining();
1418 qemu_thread_join(&ms->rp_state.rp_thread);
1419 trace_await_return_path_close_on_source_close();
1420 return ms->rp_state.error;
1421}
1422
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001423/*
1424 * Switch from normal iteration to postcopy
1425 * Returns non-0 on error
1426 */
1427static int postcopy_start(MigrationState *ms, bool *old_vm_running)
1428{
1429 int ret;
1430 const QEMUSizedBuffer *qsb;
1431 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
zhanghailiang48781e52015-12-16 11:47:33 +00001432 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001433 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1434
1435 trace_postcopy_start();
1436 qemu_mutex_lock_iothread();
1437 trace_postcopy_start_set_run();
1438
1439 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1440 *old_vm_running = runstate_is_running();
1441 global_state_store();
1442 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001443 if (ret < 0) {
1444 goto fail;
1445 }
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001446
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001447 ret = bdrv_inactivate_all();
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001448 if (ret < 0) {
1449 goto fail;
1450 }
1451
1452 /*
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001453 * Cause any non-postcopiable, but iterative devices to
1454 * send out their final data.
1455 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001456 qemu_savevm_state_complete_precopy(ms->to_dst_file, true);
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001457
1458 /*
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001459 * in Finish migrate and with the io-lock held everything should
1460 * be quiet, but we've potentially still got dirty pages and we
1461 * need to tell the destination to throw any pages it's already received
1462 * that are dirty
1463 */
1464 if (ram_postcopy_send_discard_bitmap(ms)) {
1465 error_report("postcopy send discard bitmap failed");
1466 goto fail;
1467 }
1468
1469 /*
1470 * send rest of state - note things that are doing postcopy
1471 * will notice we're in POSTCOPY_ACTIVE and not actually
1472 * wrap their state up here
1473 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001474 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001475 /* Ping just for debugging, helps line traces up */
zhanghailiang89a02a92016-01-15 11:37:42 +08001476 qemu_savevm_send_ping(ms->to_dst_file, 2);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001477
1478 /*
1479 * While loading the device state we may trigger page transfer
1480 * requests and the fd must be free to process those, and thus
1481 * the destination must read the whole device state off the fd before
1482 * it starts processing it. Unfortunately the ad-hoc migration format
1483 * doesn't allow the destination to know the size to read without fully
1484 * parsing it through each devices load-state code (especially the open
1485 * coded devices that use get/put).
1486 * So we wrap the device state up in a package with a length at the start;
1487 * to do this we use a qemu_buf to hold the whole of the device state.
1488 */
1489 QEMUFile *fb = qemu_bufopen("w", NULL);
1490 if (!fb) {
1491 error_report("Failed to create buffered file");
1492 goto fail;
1493 }
1494
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001495 /*
1496 * Make sure the receiver can get incoming pages before we send the rest
1497 * of the state
1498 */
1499 qemu_savevm_send_postcopy_listen(fb);
1500
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001501 qemu_savevm_state_complete_precopy(fb, false);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001502 qemu_savevm_send_ping(fb, 3);
1503
1504 qemu_savevm_send_postcopy_run(fb);
1505
1506 /* <><> end of stuff going into the package */
1507 qsb = qemu_buf_get(fb);
1508
1509 /* Now send that blob */
zhanghailiang89a02a92016-01-15 11:37:42 +08001510 if (qemu_savevm_send_packaged(ms->to_dst_file, qsb)) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001511 goto fail_closefb;
1512 }
1513 qemu_fclose(fb);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +00001514
1515 /* Send a notify to give a chance for anything that needs to happen
1516 * at the transition to postcopy and after the device state; in particular
1517 * spice needs to trigger a transition now
1518 */
1519 ms->postcopy_after_devices = true;
1520 notifier_list_notify(&migration_state_notifiers, ms);
1521
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001522 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
1523
1524 qemu_mutex_unlock_iothread();
1525
1526 /*
1527 * Although this ping is just for debug, it could potentially be
1528 * used for getting a better measurement of downtime at the source.
1529 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001530 qemu_savevm_send_ping(ms->to_dst_file, 4);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001531
zhanghailiang89a02a92016-01-15 11:37:42 +08001532 ret = qemu_file_get_error(ms->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001533 if (ret) {
1534 error_report("postcopy_start: Migration stream errored");
zhanghailiang48781e52015-12-16 11:47:33 +00001535 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001536 MIGRATION_STATUS_FAILED);
1537 }
1538
1539 return ret;
1540
1541fail_closefb:
1542 qemu_fclose(fb);
1543fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001544 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001545 MIGRATION_STATUS_FAILED);
1546 qemu_mutex_unlock_iothread();
1547 return -1;
1548}
1549
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001550/**
1551 * migration_completion: Used by migration_thread when there's not much left.
1552 * The caller 'breaks' the loop when this returns.
1553 *
1554 * @s: Current migration state
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001555 * @current_active_state: The migration state we expect to be in
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001556 * @*old_vm_running: Pointer to old_vm_running flag
1557 * @*start_time: Pointer to time to update
1558 */
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001559static void migration_completion(MigrationState *s, int current_active_state,
1560 bool *old_vm_running,
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001561 int64_t *start_time)
1562{
1563 int ret;
1564
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001565 if (s->state == MIGRATION_STATUS_ACTIVE) {
1566 qemu_mutex_lock_iothread();
1567 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1568 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1569 *old_vm_running = runstate_is_running();
1570 ret = global_state_store();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001571
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001572 if (!ret) {
1573 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
1574 if (ret >= 0) {
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01001575 ret = bdrv_inactivate_all();
1576 }
1577 if (ret >= 0) {
zhanghailiang89a02a92016-01-15 11:37:42 +08001578 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
1579 qemu_savevm_state_complete_precopy(s->to_dst_file, false);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001580 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001581 }
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001582 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001583
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001584 if (ret < 0) {
1585 goto fail;
1586 }
1587 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1588 trace_migration_completion_postcopy_end();
1589
zhanghailiang89a02a92016-01-15 11:37:42 +08001590 qemu_savevm_state_complete_postcopy(s->to_dst_file);
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001591 trace_migration_completion_postcopy_end_after_complete();
1592 }
1593
1594 /*
1595 * If rp was opened we must clean up the thread before
1596 * cleaning everything else up (since if there are no failures
1597 * it will wait for the destination to send it's status in
1598 * a SHUT command).
1599 * Postcopy opens rp if enabled (even if it's not avtivated)
1600 */
1601 if (migrate_postcopy_ram()) {
1602 int rp_error;
1603 trace_migration_completion_postcopy_end_before_rp();
1604 rp_error = await_return_path_close_on_source(s);
1605 trace_migration_completion_postcopy_end_after_rp(rp_error);
1606 if (rp_error) {
Greg Kurzfe904ea2016-05-18 15:44:36 +02001607 goto fail_invalidate;
Dr. David Alan Gilbertb10ac0c2015-11-05 18:11:06 +00001608 }
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001609 }
1610
zhanghailiang89a02a92016-01-15 11:37:42 +08001611 if (qemu_file_get_error(s->to_dst_file)) {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001612 trace_migration_completion_file_err();
Greg Kurzfe904ea2016-05-18 15:44:36 +02001613 goto fail_invalidate;
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001614 }
1615
zhanghailiang48781e52015-12-16 11:47:33 +00001616 migrate_set_state(&s->state, current_active_state,
1617 MIGRATION_STATUS_COMPLETED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001618 return;
1619
Greg Kurzfe904ea2016-05-18 15:44:36 +02001620fail_invalidate:
1621 /* If not doing postcopy, vm_start() will be called: let's regain
1622 * control on images.
1623 */
1624 if (s->state == MIGRATION_STATUS_ACTIVE) {
1625 Error *local_err = NULL;
1626
1627 bdrv_invalidate_cache_all(&local_err);
1628 if (local_err) {
1629 error_report_err(local_err);
1630 }
1631 }
1632
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001633fail:
zhanghailiang48781e52015-12-16 11:47:33 +00001634 migrate_set_state(&s->state, current_active_state,
1635 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001636}
1637
Dr. David Alan Gilbert70b20472015-11-05 18:10:49 +00001638/*
1639 * Master migration thread on the source VM.
1640 * It drives the migration and pumps the data down the outgoing channel.
1641 */
Juan Quintela5f496a12013-02-22 17:36:30 +01001642static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001643{
Juan Quintela9848a402012-12-19 09:55:50 +01001644 MigrationState *s = opaque;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001645 /* Used by the bandwidth calcs, updated later */
Alex Blighbc72ad62013-08-21 16:03:08 +01001646 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1647 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +01001648 int64_t initial_bytes = 0;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001649 int64_t max_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001650 int64_t start_time = initial_time;
Liang Li94f5a432015-11-02 15:37:00 +08001651 int64_t end_time;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001652 bool old_vm_running = false;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001653 bool entered_postcopy = false;
1654 /* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */
1655 enum MigrationStatus current_active_state = MIGRATION_STATUS_ACTIVE;
Juan Quintela76f59332012-10-03 20:16:24 +02001656
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001657 rcu_register_thread();
1658
zhanghailiang89a02a92016-01-15 11:37:42 +08001659 qemu_savevm_state_header(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001660
1661 if (migrate_postcopy_ram()) {
1662 /* Now tell the dest that it should open its end so it can reply */
zhanghailiang89a02a92016-01-15 11:37:42 +08001663 qemu_savevm_send_open_return_path(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001664
1665 /* And do a ping that will make stuff easier to debug */
zhanghailiang89a02a92016-01-15 11:37:42 +08001666 qemu_savevm_send_ping(s->to_dst_file, 1);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001667
1668 /*
1669 * Tell the destination that we *might* want to do postcopy later;
1670 * if the other end can't do postcopy it should fail now, nice and
1671 * early.
1672 */
zhanghailiang89a02a92016-01-15 11:37:42 +08001673 qemu_savevm_send_postcopy_advise(s->to_dst_file);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001674 }
1675
zhanghailiang89a02a92016-01-15 11:37:42 +08001676 qemu_savevm_state_begin(s->to_dst_file, &s->params);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001677
Alex Blighbc72ad62013-08-21 16:03:08 +01001678 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001679 current_active_state = MIGRATION_STATUS_ACTIVE;
zhanghailiang48781e52015-12-16 11:47:33 +00001680 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1681 MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -04001682
Dr. David Alan Gilbert9ec055a2015-11-05 18:10:58 +00001683 trace_migration_thread_setup_complete();
1684
1685 while (s->state == MIGRATION_STATUS_ACTIVE ||
1686 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +01001687 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +02001688 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001689
zhanghailiang89a02a92016-01-15 11:37:42 +08001690 if (!qemu_file_rate_limit(s->to_dst_file)) {
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001691 uint64_t pend_post, pend_nonpost;
1692
zhanghailiang89a02a92016-01-15 11:37:42 +08001693 qemu_savevm_state_pending(s->to_dst_file, max_size, &pend_nonpost,
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001694 &pend_post);
1695 pending_size = pend_nonpost + pend_post;
1696 trace_migrate_pending(pending_size, max_size,
1697 pend_post, pend_nonpost);
Juan Quintelab22ff1f2012-10-17 21:06:31 +02001698 if (pending_size && pending_size >= max_size) {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001699 /* Still a significant amount to transfer */
1700
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001701 if (migrate_postcopy_ram() &&
1702 s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE &&
1703 pend_nonpost <= max_size &&
1704 atomic_read(&s->start_postcopy)) {
1705
1706 if (!postcopy_start(s, &old_vm_running)) {
1707 current_active_state = MIGRATION_STATUS_POSTCOPY_ACTIVE;
1708 entered_postcopy = true;
1709 }
1710
1711 continue;
1712 }
1713 /* Just another iteration step */
zhanghailiang89a02a92016-01-15 11:37:42 +08001714 qemu_savevm_state_iterate(s->to_dst_file, entered_postcopy);
Juan Quintelac369f402012-10-03 20:33:34 +02001715 } else {
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001716 trace_migration_thread_low_pending(pending_size);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001717 migration_completion(s, current_active_state,
Dr. David Alan Gilbert36f48562015-11-05 18:10:57 +00001718 &old_vm_running, &start_time);
Dr. David Alan Gilbert09f6c852015-08-13 11:51:31 +01001719 break;
Juan Quintelac369f402012-10-03 20:33:34 +02001720 }
1721 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001722
zhanghailiang89a02a92016-01-15 11:37:42 +08001723 if (qemu_file_get_error(s->to_dst_file)) {
zhanghailiang48781e52015-12-16 11:47:33 +00001724 migrate_set_state(&s->state, current_active_state,
1725 MIGRATION_STATUS_FAILED);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001726 trace_migration_thread_file_err();
Paolo Bonzinifd45ee22013-02-22 17:36:33 +01001727 break;
1728 }
Alex Blighbc72ad62013-08-21 16:03:08 +01001729 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001730 if (current_time >= initial_time + BUFFER_DELAY) {
zhanghailiang89a02a92016-01-15 11:37:42 +08001731 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file) -
1732 initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -05001733 uint64_t time_spent = current_time - initial_time;
Paolo Bonzinia694ee32015-01-26 12:12:27 +01001734 double bandwidth = (double)transferred_bytes / time_spent;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001735 max_size = bandwidth * migrate_max_downtime() / 1000000;
1736
Wei Yang5b648de2016-01-24 14:09:57 +00001737 s->mbps = (((double) transferred_bytes * 8.0) /
1738 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
Michael R. Hines7e114f82013-06-25 21:35:30 -04001739
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001740 trace_migrate_transferred(transferred_bytes, time_spent,
1741 bandwidth, max_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +01001742 /* if we haven't sent anything, we don't want to recalculate
1743 10000 is a small enough number for our purposes */
1744 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
1745 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
1746 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001747
zhanghailiang89a02a92016-01-15 11:37:42 +08001748 qemu_file_reset_rate_limit(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001749 initial_time = current_time;
zhanghailiang89a02a92016-01-15 11:37:42 +08001750 initial_bytes = qemu_ftell(s->to_dst_file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001751 }
zhanghailiang89a02a92016-01-15 11:37:42 +08001752 if (qemu_file_rate_limit(s->to_dst_file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001753 /* usleep expects microseconds */
1754 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
1755 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001756 }
1757
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001758 trace_migration_thread_after_loop();
Jason J. Herne070afca2015-09-08 13:12:35 -04001759 /* If we enabled cpu throttling for auto-converge, turn it off. */
1760 cpu_throttle_stop();
Liang Li94f5a432015-11-02 15:37:00 +08001761 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Jason J. Herne070afca2015-09-08 13:12:35 -04001762
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001763 qemu_mutex_lock_iothread();
Liang Liea7415f2015-11-02 15:37:01 +08001764 qemu_savevm_state_cleanup();
zhanghailiang31194732015-03-13 16:08:38 +08001765 if (s->state == MIGRATION_STATUS_COMPLETED) {
zhanghailiang89a02a92016-01-15 11:37:42 +08001766 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001767 s->total_time = end_time - s->total_time;
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001768 if (!entered_postcopy) {
1769 s->downtime = end_time - start_time;
1770 }
Peter Lievend6ed7312014-05-12 10:46:00 +02001771 if (s->total_time) {
1772 s->mbps = (((double) transferred_bytes * 8.0) /
1773 ((double) s->total_time)) / 1000;
1774 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001775 runstate_set(RUN_STATE_POSTMIGRATE);
1776 } else {
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001777 if (old_vm_running && !entered_postcopy) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001778 vm_start();
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001779 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001780 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001781 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001782 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001783
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001784 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001785 return NULL;
1786}
1787
Juan Quintela9848a402012-12-19 09:55:50 +01001788void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001789{
Juan Quintelacc283e32013-02-01 11:12:26 +01001790 /* This is a best 1st approximation. ns to ms */
1791 s->expected_downtime = max_downtime/1000000;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001792 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001793
zhanghailiang89a02a92016-01-15 11:37:42 +08001794 qemu_file_set_rate_limit(s->to_dst_file,
Paolo Bonzini442773c2013-02-22 17:36:44 +01001795 s->bandwidth_limit / XFER_LIMIT_RATIO);
1796
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02001797 /* Notify before starting migration thread */
1798 notifier_list_notify(&migration_state_notifiers, s);
1799
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001800 /*
1801 * Open the return path; currently for postcopy but other things might
1802 * also want it.
1803 */
1804 if (migrate_postcopy_ram()) {
1805 if (open_return_path_on_source(s)) {
1806 error_report("Unable to open return-path for postcopy");
zhanghailiang48781e52015-12-16 11:47:33 +00001807 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001808 MIGRATION_STATUS_FAILED);
1809 migrate_fd_cleanup(s);
1810 return;
1811 }
1812 }
1813
Liang Li8706d2d2015-03-23 16:32:17 +08001814 migrate_compress_threads_create();
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001815 qemu_thread_create(&s->thread, "migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001816 QEMU_THREAD_JOINABLE);
Dr. David Alan Gilbert1d34e4b2015-11-05 18:11:05 +00001817 s->migration_thread_running = true;
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001818}
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001819
1820PostcopyState postcopy_state_get(void)
1821{
1822 return atomic_mb_read(&incoming_postcopy_state);
1823}
1824
1825/* Set the state and return the old state */
1826PostcopyState postcopy_state_set(PostcopyState new_state)
1827{
1828 return atomic_xchg(&incoming_postcopy_state, new_state);
1829}
1830