blob: fd4f99b84e3b2c7b5eca22d7801122d9980c8966 [file] [log] [blame]
aliguori5bb79102008-10-13 03:12:02 +00001/*
2 * QEMU live migration
3 *
4 * Copyright IBM, Corp. 2008
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010012 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
aliguori5bb79102008-10-13 03:12:02 +000014 */
15
16#include "qemu-common.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010017#include "qemu/error-report.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010018#include "qemu/main-loop.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010019#include "migration/migration.h"
Juan Quintela0d82d0e2012-10-03 14:18:33 +020020#include "migration/qemu-file.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010021#include "sysemu/sysemu.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010022#include "block/block.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010023#include "qapi/qmp/qerror.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010024#include "qemu/sockets.h"
Paolo Bonziniab28bd22015-07-09 08:55:38 +020025#include "qemu/rcu.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010026#include "migration/block.h"
Juan Quintela766bd172012-07-23 05:45:29 +020027#include "qemu/thread.h"
Luiz Capitulino791e7c82011-09-13 17:37:16 -030028#include "qmp-commands.h"
Kazuya Saitoc09e5bb2013-02-22 17:36:19 +010029#include "trace.h"
Juan Quinteladf4b1022014-10-08 10:58:10 +020030#include "qapi/util.h"
Juan Quintela598cd2b2015-05-20 12:16:15 +020031#include "qapi-event.h"
aliguori065e2812008-11-11 16:46:33 +000032
Juan Quintelad0ae46c2011-02-23 00:33:19 +010033#define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000034
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010035/* Amount of time to allocate to each "chunk" of bandwidth-throttled
36 * data. */
37#define BUFFER_DELAY 100
38#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
39
Liang Li8706d2d2015-03-23 16:32:17 +080040/* Default compression thread count */
41#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080042/* Default decompression thread count, usually decompression is at
43 * least 4 times as fast as compression.*/
44#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080045/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
46#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
47
Orit Wasserman17ad9b32012-08-06 21:42:53 +030048/* Migration XBZRLE default cache size */
49#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
50
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010051static NotifierList migration_state_notifiers =
52 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
53
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000054static bool deferred_incoming;
55
Juan Quintela17549e82011-10-05 13:50:43 +020056/* When we add fault tolerance, we could have several
57 migrations at once. For now we don't need to add
58 dynamic creation of migration */
59
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010060/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +020061MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +020062{
63 static MigrationState current_migration = {
zhanghailiang31194732015-03-13 16:08:38 +080064 .state = MIGRATION_STATUS_NONE,
Juan Quintelad0ae46c2011-02-23 00:33:19 +010065 .bandwidth_limit = MAX_THROTTLE,
Orit Wasserman17ad9b32012-08-06 21:42:53 +030066 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
Michael R. Hines7e114f82013-06-25 21:35:30 -040067 .mbps = -1,
Liang Li43c60a82015-03-23 16:32:27 +080068 .parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] =
69 DEFAULT_MIGRATE_COMPRESS_LEVEL,
70 .parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
71 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
72 .parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
73 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
Juan Quintela17549e82011-10-05 13:50:43 +020074 };
75
76 return &current_migration;
77}
78
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010079/* For incoming */
80static MigrationIncomingState *mis_current;
81
82MigrationIncomingState *migration_incoming_get_current(void)
83{
84 return mis_current;
85}
86
87MigrationIncomingState *migration_incoming_state_new(QEMUFile* f)
88{
89 mis_current = g_malloc0(sizeof(MigrationIncomingState));
90 mis_current->file = f;
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +010091 QLIST_INIT(&mis_current->loadvm_handlers);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010092
93 return mis_current;
94}
95
96void migration_incoming_state_destroy(void)
97{
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +010098 loadvm_free_handlers(mis_current);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010099 g_free(mis_current);
100 mis_current = NULL;
101}
102
Juan Quinteladf4b1022014-10-08 10:58:10 +0200103
104typedef struct {
Juan Quintela13d16812014-10-08 13:58:24 +0200105 bool optional;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200106 uint32_t size;
107 uint8_t runstate[100];
Juan Quintela172c4352015-07-08 13:56:26 +0200108 RunState state;
109 bool received;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200110} GlobalState;
111
112static GlobalState global_state;
113
Juan Quintela560d0272015-07-15 09:53:46 +0200114int global_state_store(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200115{
116 if (!runstate_store((char *)global_state.runstate,
117 sizeof(global_state.runstate))) {
118 error_report("runstate name too big: %s", global_state.runstate);
119 trace_migrate_state_too_big();
120 return -EINVAL;
121 }
122 return 0;
123}
124
Juan Quintela172c4352015-07-08 13:56:26 +0200125static bool global_state_received(void)
Juan Quinteladf4b1022014-10-08 10:58:10 +0200126{
Juan Quintela172c4352015-07-08 13:56:26 +0200127 return global_state.received;
128}
129
130static RunState global_state_get_runstate(void)
131{
132 return global_state.state;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200133}
134
Juan Quintela13d16812014-10-08 13:58:24 +0200135void global_state_set_optional(void)
136{
137 global_state.optional = true;
138}
139
140static bool global_state_needed(void *opaque)
141{
142 GlobalState *s = opaque;
143 char *runstate = (char *)s->runstate;
144
145 /* If it is not optional, it is mandatory */
146
147 if (s->optional == false) {
148 return true;
149 }
150
151 /* If state is running or paused, it is not needed */
152
153 if (strcmp(runstate, "running") == 0 ||
154 strcmp(runstate, "paused") == 0) {
155 return false;
156 }
157
158 /* for any other state it is needed */
159 return true;
160}
161
Juan Quinteladf4b1022014-10-08 10:58:10 +0200162static int global_state_post_load(void *opaque, int version_id)
163{
164 GlobalState *s = opaque;
Juan Quintela172c4352015-07-08 13:56:26 +0200165 Error *local_err = NULL;
166 int r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200167 char *runstate = (char *)s->runstate;
168
Juan Quintela172c4352015-07-08 13:56:26 +0200169 s->received = true;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200170 trace_migrate_global_state_post_load(runstate);
171
Juan Quintela172c4352015-07-08 13:56:26 +0200172 r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE_MAX,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200173 -1, &local_err);
174
Juan Quintela172c4352015-07-08 13:56:26 +0200175 if (r == -1) {
176 if (local_err) {
177 error_report_err(local_err);
Juan Quinteladf4b1022014-10-08 10:58:10 +0200178 }
Juan Quintela172c4352015-07-08 13:56:26 +0200179 return -EINVAL;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200180 }
Juan Quintela172c4352015-07-08 13:56:26 +0200181 s->state = r;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200182
Juan Quintela172c4352015-07-08 13:56:26 +0200183 return 0;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200184}
185
186static void global_state_pre_save(void *opaque)
187{
188 GlobalState *s = opaque;
189
190 trace_migrate_global_state_pre_save((char *)s->runstate);
191 s->size = strlen((char *)s->runstate) + 1;
192}
193
194static const VMStateDescription vmstate_globalstate = {
195 .name = "globalstate",
196 .version_id = 1,
197 .minimum_version_id = 1,
198 .post_load = global_state_post_load,
199 .pre_save = global_state_pre_save,
Juan Quintela13d16812014-10-08 13:58:24 +0200200 .needed = global_state_needed,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200201 .fields = (VMStateField[]) {
202 VMSTATE_UINT32(size, GlobalState),
203 VMSTATE_BUFFER(runstate, GlobalState),
204 VMSTATE_END_OF_LIST()
205 },
206};
207
208void register_global_state(void)
209{
210 /* We would use it independently that we receive it */
211 strcpy((char *)&global_state.runstate, "");
Juan Quintela172c4352015-07-08 13:56:26 +0200212 global_state.received = false;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200213 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
214}
215
Juan Quintelab05dc722015-07-07 14:44:05 +0200216static void migrate_generate_event(int new_state)
217{
218 if (migrate_use_events()) {
219 qapi_event_send_migration(new_state, &error_abort);
Juan Quintelab05dc722015-07-07 14:44:05 +0200220 }
221}
222
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000223/*
224 * Called on -incoming with a defer: uri.
225 * The migration can be started later after any parameters have been
226 * changed.
227 */
228static void deferred_incoming_migration(Error **errp)
229{
230 if (deferred_incoming) {
231 error_setg(errp, "Incoming migration already deferred");
232 }
233 deferred_incoming = true;
234}
235
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200236void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000237{
aliguori34c9dd82008-10-13 03:14:31 +0000238 const char *p;
239
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200240 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000241 if (!strcmp(uri, "defer")) {
242 deferred_incoming_migration(errp);
243 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200244 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400245#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000246 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400247 rdma_start_incoming_migration(p, errp);
248#endif
aliguori065e2812008-11-11 16:46:33 +0000249#if !defined(WIN32)
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000250 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200251 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000252 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200253 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000254 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200255 fd_start_incoming_migration(p, errp);
aliguori065e2812008-11-11 16:46:33 +0000256#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000257 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100258 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200259 }
aliguori5bb79102008-10-13 03:12:02 +0000260}
261
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200262static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200263{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200264 QEMUFile *f = opaque;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100265 Error *local_err = NULL;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200266 int ret;
267
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100268 migration_incoming_state_new(f);
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200269 migrate_generate_event(MIGRATION_STATUS_ACTIVE);
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200270 ret = qemu_loadvm_state(f);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100271
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200272 qemu_fclose(f);
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200273 free_xbzrle_decoded_buf();
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100274 migration_incoming_state_destroy();
275
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200276 if (ret < 0) {
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200277 migrate_generate_event(MIGRATION_STATUS_FAILED);
Peter Lievendb80fac2014-06-10 11:29:16 +0200278 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800279 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600280 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200281 }
Juan Quintela7cf1fe62015-05-20 17:15:42 +0200282 migrate_generate_event(MIGRATION_STATUS_COMPLETED);
Juan Quintela511c0232010-06-09 14:10:55 +0200283 qemu_announce_self();
Juan Quintela511c0232010-06-09 14:10:55 +0200284
Anthony Liguori0f154232011-11-14 15:09:45 -0600285 /* Make sure all file formats flush their mutable metadata */
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100286 bdrv_invalidate_cache_all(&local_err);
287 if (local_err) {
Markus Armbruster97baf9d2015-02-18 19:21:52 +0100288 error_report_err(local_err);
Liang Li3fcb38c2015-03-23 16:32:18 +0800289 migrate_decompress_threads_join();
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100290 exit(EXIT_FAILURE);
291 }
Anthony Liguori0f154232011-11-14 15:09:45 -0600292
Juan Quintela172c4352015-07-08 13:56:26 +0200293 /* If global state section was not received or we are in running
294 state, we need to obey autostart. Any other state is set with
295 runstate_set. */
Juan Quinteladf4b1022014-10-08 10:58:10 +0200296
Juan Quintela172c4352015-07-08 13:56:26 +0200297 if (!global_state_received() ||
298 global_state_get_runstate() == RUN_STATE_RUNNING) {
Juan Quinteladf4b1022014-10-08 10:58:10 +0200299 if (autostart) {
300 vm_start();
301 } else {
302 runstate_set(RUN_STATE_PAUSED);
303 }
Juan Quintela172c4352015-07-08 13:56:26 +0200304 } else {
305 runstate_set(global_state_get_runstate());
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300306 }
Liang Li3fcb38c2015-03-23 16:32:18 +0800307 migrate_decompress_threads_join();
Juan Quintela511c0232010-06-09 14:10:55 +0200308}
309
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200310void process_incoming_migration(QEMUFile *f)
311{
312 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
313 int fd = qemu_get_fd(f);
314
315 assert(fd != -1);
Liang Li3fcb38c2015-03-23 16:32:18 +0800316 migrate_decompress_threads_create();
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100317 qemu_set_nonblock(fd);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200318 qemu_coroutine_enter(co, f);
319}
320
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400321/* amount of nanoseconds we are willing to wait for migration to be down.
322 * the choice of nanoseconds is because it is the maximum resolution that
323 * get_clock() can achieve. It is an internal measure. All user-visible
324 * units must be in seconds */
Alexey Kardashevskiyf7cd55a2014-03-27 14:57:26 +1100325static uint64_t max_downtime = 300000000;
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400326
327uint64_t migrate_max_downtime(void)
328{
329 return max_downtime;
330}
331
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300332MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
333{
334 MigrationCapabilityStatusList *head = NULL;
335 MigrationCapabilityStatusList *caps;
336 MigrationState *s = migrate_get_current();
337 int i;
338
Michael Tokarev387eede2013-10-05 13:18:28 +0400339 caps = NULL; /* silence compiler warning */
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300340 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
341 if (head == NULL) {
342 head = g_malloc0(sizeof(*caps));
343 caps = head;
344 } else {
345 caps->next = g_malloc0(sizeof(*caps));
346 caps = caps->next;
347 }
348 caps->value =
349 g_malloc(sizeof(*caps->value));
350 caps->value->capability = i;
351 caps->value->state = s->enabled_capabilities[i];
352 }
353
354 return head;
355}
356
Liang Li85de8322015-03-23 16:32:28 +0800357MigrationParameters *qmp_query_migrate_parameters(Error **errp)
358{
359 MigrationParameters *params;
360 MigrationState *s = migrate_get_current();
361
362 params = g_malloc0(sizeof(*params));
363 params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
364 params->compress_threads =
365 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
366 params->decompress_threads =
367 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
368
369 return params;
370}
371
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300372static void get_xbzrle_cache_stats(MigrationInfo *info)
373{
374 if (migrate_use_xbzrle()) {
375 info->has_xbzrle_cache = true;
376 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
377 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
378 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
379 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
380 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
ChenLiang8bc39232014-04-04 17:57:56 +0800381 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300382 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
383 }
384}
385
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300386MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000387{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300388 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200389 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000390
Juan Quintela17549e82011-10-05 13:50:43 +0200391 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800392 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200393 /* no migration has happened ever */
394 break;
zhanghailiang31194732015-03-13 16:08:38 +0800395 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400396 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400397 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400398 break;
zhanghailiang31194732015-03-13 16:08:38 +0800399 case MIGRATION_STATUS_ACTIVE:
400 case MIGRATION_STATUS_CANCELLING:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300401 info->has_status = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200402 info->has_total_time = true;
Alex Blighbc72ad62013-08-21 16:03:08 +0100403 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Juan Quintela7aa939a2012-08-18 13:17:10 +0200404 - s->total_time;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200405 info->has_expected_downtime = true;
406 info->expected_downtime = s->expected_downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400407 info->has_setup_time = true;
408 info->setup_time = s->setup_time;
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200409
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300410 info->has_ram = true;
411 info->ram = g_malloc0(sizeof(*info->ram));
412 info->ram->transferred = ram_bytes_transferred();
413 info->ram->remaining = ram_bytes_remaining();
414 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300415 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100416 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300417 info->ram->normal = norm_mig_pages_transferred();
418 info->ram->normal_bytes = norm_mig_bytes_transferred();
Juan Quintela8d017192012-08-13 12:31:25 +0200419 info->ram->dirty_pages_rate = s->dirty_pages_rate;
Michael R. Hines7e114f82013-06-25 21:35:30 -0400420 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800421 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela8d017192012-08-13 12:31:25 +0200422
Juan Quintela17549e82011-10-05 13:50:43 +0200423 if (blk_mig_active()) {
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300424 info->has_disk = true;
425 info->disk = g_malloc0(sizeof(*info->disk));
426 info->disk->transferred = blk_mig_bytes_transferred();
427 info->disk->remaining = blk_mig_bytes_remaining();
428 info->disk->total = blk_mig_bytes_total();
aliguoriff8d81d2008-10-24 22:10:31 +0000429 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300430
431 get_xbzrle_cache_stats(info);
Juan Quintela17549e82011-10-05 13:50:43 +0200432 break;
zhanghailiang31194732015-03-13 16:08:38 +0800433 case MIGRATION_STATUS_COMPLETED:
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300434 get_xbzrle_cache_stats(info);
435
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300436 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900437 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200438 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200439 info->has_downtime = true;
440 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400441 info->has_setup_time = true;
442 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200443
444 info->has_ram = true;
445 info->ram = g_malloc0(sizeof(*info->ram));
446 info->ram->transferred = ram_bytes_transferred();
447 info->ram->remaining = 0;
448 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300449 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100450 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300451 info->ram->normal = norm_mig_pages_transferred();
452 info->ram->normal_bytes = norm_mig_bytes_transferred();
Michael R. Hines7e114f82013-06-25 21:35:30 -0400453 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800454 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela17549e82011-10-05 13:50:43 +0200455 break;
zhanghailiang31194732015-03-13 16:08:38 +0800456 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300457 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200458 break;
zhanghailiang31194732015-03-13 16:08:38 +0800459 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300460 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200461 break;
aliguori5bb79102008-10-13 03:12:02 +0000462 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800463 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300464
465 return info;
aliguori5bb79102008-10-13 03:12:02 +0000466}
467
Orit Wasserman00458432012-08-06 21:42:48 +0300468void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
469 Error **errp)
470{
471 MigrationState *s = migrate_get_current();
472 MigrationCapabilityStatusList *cap;
473
zhanghailiang31194732015-03-13 16:08:38 +0800474 if (s->state == MIGRATION_STATUS_ACTIVE ||
475 s->state == MIGRATION_STATUS_SETUP) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100476 error_setg(errp, QERR_MIGRATION_ACTIVE);
Orit Wasserman00458432012-08-06 21:42:48 +0300477 return;
478 }
479
480 for (cap = params; cap; cap = cap->next) {
481 s->enabled_capabilities[cap->value->capability] = cap->value->state;
482 }
483}
484
Liang Li85de8322015-03-23 16:32:28 +0800485void qmp_migrate_set_parameters(bool has_compress_level,
486 int64_t compress_level,
487 bool has_compress_threads,
488 int64_t compress_threads,
489 bool has_decompress_threads,
490 int64_t decompress_threads, Error **errp)
491{
492 MigrationState *s = migrate_get_current();
493
494 if (has_compress_level && (compress_level < 0 || compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100495 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
496 "is invalid, it should be in the range of 0 to 9");
Liang Li85de8322015-03-23 16:32:28 +0800497 return;
498 }
499 if (has_compress_threads &&
500 (compress_threads < 1 || compress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100501 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
502 "compress_threads",
503 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800504 return;
505 }
506 if (has_decompress_threads &&
507 (decompress_threads < 1 || decompress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100508 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
509 "decompress_threads",
510 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800511 return;
512 }
513
514 if (has_compress_level) {
515 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
516 }
517 if (has_compress_threads) {
518 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads;
519 }
520 if (has_decompress_threads) {
521 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
522 decompress_threads;
523 }
524}
525
aliguori065e2812008-11-11 16:46:33 +0000526/* shared migration helpers */
527
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000528static void migrate_set_state(MigrationState *s, int old_state, int new_state)
529{
Juan Quintelaa5c17b52015-06-17 02:06:20 +0200530 if (atomic_cmpxchg(&s->state, old_state, new_state) == old_state) {
Juan Quintela4ba4bc52015-07-08 13:58:27 +0200531 trace_migrate_set_state(new_state);
Juan Quintelab05dc722015-07-07 14:44:05 +0200532 migrate_generate_event(new_state);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000533 }
534}
535
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100536static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000537{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100538 MigrationState *s = opaque;
539
540 qemu_bh_delete(s->cleanup_bh);
541 s->cleanup_bh = NULL;
542
aliguori065e2812008-11-11 16:46:33 +0000543 if (s->file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100544 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100545 qemu_mutex_unlock_iothread();
546 qemu_thread_join(&s->thread);
547 qemu_mutex_lock_iothread();
548
Liang Li8706d2d2015-03-23 16:32:17 +0800549 migrate_compress_threads_join();
Paolo Bonzini6f190a02013-02-22 17:36:48 +0100550 qemu_fclose(s->file);
551 s->file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000552 }
553
zhanghailiang31194732015-03-13 16:08:38 +0800554 assert(s->state != MIGRATION_STATUS_ACTIVE);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100555
zhanghailiang31194732015-03-13 16:08:38 +0800556 if (s->state != MIGRATION_STATUS_COMPLETED) {
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100557 qemu_savevm_state_cancel();
zhanghailiang31194732015-03-13 16:08:38 +0800558 if (s->state == MIGRATION_STATUS_CANCELLING) {
559 migrate_set_state(s, MIGRATION_STATUS_CANCELLING,
560 MIGRATION_STATUS_CANCELLED);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000561 }
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100562 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100563
564 notifier_list_notify(&migration_state_notifiers, s);
aliguori065e2812008-11-11 16:46:33 +0000565}
566
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200567void migrate_fd_error(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000568{
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100569 trace_migrate_fd_error();
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100570 assert(s->file == NULL);
Juan Quintela78443372015-06-17 01:36:40 +0200571 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100572 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela458cf282011-02-22 23:32:54 +0100573}
574
Juan Quintela0edda1c2010-05-11 16:28:39 +0200575static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000576{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000577 int old_state ;
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000578 QEMUFile *f = migrate_get_current()->file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100579 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000580
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000581 do {
582 old_state = s->state;
zhanghailiang31194732015-03-13 16:08:38 +0800583 if (old_state != MIGRATION_STATUS_SETUP &&
584 old_state != MIGRATION_STATUS_ACTIVE) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000585 break;
586 }
zhanghailiang31194732015-03-13 16:08:38 +0800587 migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
588 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000589
590 /*
591 * If we're unlucky the migration code might be stuck somewhere in a
592 * send/write while the network has failed and is waiting to timeout;
593 * if we've got shutdown(2) available then we can force it to quit.
594 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
595 * called in a bh, so there is no race against this cancel.
596 */
zhanghailiang31194732015-03-13 16:08:38 +0800597 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000598 qemu_file_shutdown(f);
599 }
aliguori065e2812008-11-11 16:46:33 +0000600}
601
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100602void add_migration_state_change_notifier(Notifier *notify)
603{
604 notifier_list_add(&migration_state_notifiers, notify);
605}
606
607void remove_migration_state_change_notifier(Notifier *notify)
608{
Paolo Bonzini31552522012-01-13 17:34:01 +0100609 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100610}
611
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +0200612bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200613{
zhanghailiang31194732015-03-13 16:08:38 +0800614 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200615}
616
Juan Quintela70736932011-02-23 00:43:59 +0100617bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100618{
zhanghailiang31194732015-03-13 16:08:38 +0800619 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100620}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200621
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200622bool migration_has_failed(MigrationState *s)
623{
zhanghailiang31194732015-03-13 16:08:38 +0800624 return (s->state == MIGRATION_STATUS_CANCELLED ||
625 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200626}
627
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300628static MigrationState *migrate_init(const MigrationParams *params)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200629{
Juan Quintela17549e82011-10-05 13:50:43 +0200630 MigrationState *s = migrate_get_current();
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100631 int64_t bandwidth_limit = s->bandwidth_limit;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300632 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300633 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
Liang Li43c60a82015-03-23 16:32:27 +0800634 int compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
635 int compress_thread_count =
636 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
637 int decompress_thread_count =
638 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300639
640 memcpy(enabled_capabilities, s->enabled_capabilities,
641 sizeof(enabled_capabilities));
Juan Quintela0edda1c2010-05-11 16:28:39 +0200642
Juan Quintela17549e82011-10-05 13:50:43 +0200643 memset(s, 0, sizeof(*s));
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300644 s->params = *params;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300645 memcpy(s->enabled_capabilities, enabled_capabilities,
646 sizeof(enabled_capabilities));
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300647 s->xbzrle_cache_size = xbzrle_cache_size;
Juan Quintela1299c632011-11-09 21:29:01 +0100648
Liang Li43c60a82015-03-23 16:32:27 +0800649 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
650 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
651 compress_thread_count;
652 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
653 decompress_thread_count;
Juan Quintela0edda1c2010-05-11 16:28:39 +0200654 s->bandwidth_limit = bandwidth_limit;
Juan Quintela78443372015-06-17 01:36:40 +0200655 migrate_set_state(s, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200656
Alex Blighbc72ad62013-08-21 16:03:08 +0100657 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200658 return s;
659}
Juan Quintelacab30142011-02-22 23:54:21 +0100660
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600661static GSList *migration_blockers;
662
663void migrate_add_blocker(Error *reason)
664{
665 migration_blockers = g_slist_prepend(migration_blockers, reason);
666}
667
668void migrate_del_blocker(Error *reason)
669{
670 migration_blockers = g_slist_remove(migration_blockers, reason);
671}
672
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000673void qmp_migrate_incoming(const char *uri, Error **errp)
674{
675 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000676 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000677
678 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000679 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000680 return;
681 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000682 if (!once) {
683 error_setg(errp, "The incoming migration has already been started");
684 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000685
686 qemu_start_incoming_migration(uri, &local_err);
687
688 if (local_err) {
689 error_propagate(errp, local_err);
690 return;
691 }
692
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000693 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000694}
695
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200696void qmp_migrate(const char *uri, bool has_blk, bool blk,
697 bool has_inc, bool inc, bool has_detach, bool detach,
698 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100699{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +0200700 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +0200701 MigrationState *s = migrate_get_current();
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300702 MigrationParams params;
Juan Quintelacab30142011-02-22 23:54:21 +0100703 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +0100704
Pawit Pornkitprasan8c0426a2013-07-30 08:39:52 +0900705 params.blk = has_blk && blk;
706 params.shared = has_inc && inc;
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300707
zhanghailiang31194732015-03-13 16:08:38 +0800708 if (s->state == MIGRATION_STATUS_ACTIVE ||
709 s->state == MIGRATION_STATUS_SETUP ||
710 s->state == MIGRATION_STATUS_CANCELLING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100711 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200712 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100713 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +0100714 if (runstate_check(RUN_STATE_INMIGRATE)) {
715 error_setg(errp, "Guest is waiting for an incoming migration");
716 return;
717 }
718
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200719 if (qemu_savevm_state_blocked(errp)) {
720 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100721 }
722
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600723 if (migration_blockers) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200724 *errp = error_copy(migration_blockers->data);
725 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600726 }
727
Juan Quintela656a2332015-07-01 09:32:29 +0200728 /* We are starting a new migration, so we want to start in a clean
729 state. This change is only needed if previous migration
730 failed/was cancelled. We don't use migrate_set_state() because
731 we are setting the initial state, not changing it. */
732 s->state = MIGRATION_STATUS_NONE;
733
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300734 s = migrate_init(&params);
Juan Quintelacab30142011-02-22 23:54:21 +0100735
736 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200737 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400738#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +0800739 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400740 rdma_start_outgoing_migration(s, p, &local_err);
741#endif
Juan Quintelacab30142011-02-22 23:54:21 +0100742#if !defined(WIN32)
743 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200744 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100745 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200746 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100747 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200748 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100749#endif
750 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100751 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
752 "a valid migration protocol");
Juan Quintela78443372015-06-17 01:36:40 +0200753 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200754 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100755 }
756
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200757 if (local_err) {
Paolo Bonzini342ab8d2012-10-02 09:59:38 +0200758 migrate_fd_error(s);
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200759 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200760 return;
Juan Quintela1299c632011-11-09 21:29:01 +0100761 }
Juan Quintelacab30142011-02-22 23:54:21 +0100762}
763
Luiz Capitulino6cdedb02011-11-27 22:54:09 -0200764void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100765{
Juan Quintela17549e82011-10-05 13:50:43 +0200766 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +0100767}
768
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300769void qmp_migrate_set_cache_size(int64_t value, Error **errp)
770{
771 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +0200772 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300773
774 /* Check for truncation */
775 if (value != (size_t)value) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100776 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
777 "exceeding address space");
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300778 return;
779 }
780
Orit Wassermana5615b12014-01-30 20:08:36 +0200781 /* Cache should not be larger than guest ram size */
782 if (value > ram_bytes_total()) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100783 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
784 "exceeds guest ram size ");
Orit Wassermana5615b12014-01-30 20:08:36 +0200785 return;
786 }
787
Orit Wassermanc91e6812014-01-30 20:08:34 +0200788 new_size = xbzrle_cache_resize(value);
789 if (new_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100790 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
791 "is smaller than page size");
Orit Wassermanc91e6812014-01-30 20:08:34 +0200792 return;
793 }
794
795 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300796}
797
798int64_t qmp_query_migrate_cache_size(Error **errp)
799{
800 return migrate_xbzrle_cache_size();
801}
802
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200803void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100804{
Juan Quintelacab30142011-02-22 23:54:21 +0100805 MigrationState *s;
806
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200807 if (value < 0) {
808 value = 0;
Juan Quintelacab30142011-02-22 23:54:21 +0100809 }
Paolo Bonzini442773c2013-02-22 17:36:44 +0100810 if (value > SIZE_MAX) {
811 value = SIZE_MAX;
812 }
Juan Quintelacab30142011-02-22 23:54:21 +0100813
Juan Quintela17549e82011-10-05 13:50:43 +0200814 s = migrate_get_current();
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200815 s->bandwidth_limit = value;
Paolo Bonzini442773c2013-02-22 17:36:44 +0100816 if (s->file) {
817 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
818 }
Juan Quintelacab30142011-02-22 23:54:21 +0100819}
820
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200821void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100822{
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200823 value *= 1e9;
824 value = MAX(0, MIN(UINT64_MAX, value));
825 max_downtime = (uint64_t)value;
aliguori5bb79102008-10-13 03:12:02 +0000826}
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300827
Chegu Vinodbde1e2e2013-06-24 03:49:42 -0600828bool migrate_auto_converge(void)
829{
830 MigrationState *s;
831
832 s = migrate_get_current();
833
834 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
835}
836
Peter Lieven323004a2013-07-18 09:48:50 +0200837bool migrate_zero_blocks(void)
838{
839 MigrationState *s;
840
841 s = migrate_get_current();
842
843 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
844}
845
Liang Li8706d2d2015-03-23 16:32:17 +0800846bool migrate_use_compression(void)
847{
Liang Lidde4e692015-03-23 16:32:26 +0800848 MigrationState *s;
849
850 s = migrate_get_current();
851
852 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +0800853}
854
855int migrate_compress_level(void)
856{
857 MigrationState *s;
858
859 s = migrate_get_current();
860
Liang Li43c60a82015-03-23 16:32:27 +0800861 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
Liang Li8706d2d2015-03-23 16:32:17 +0800862}
863
864int migrate_compress_threads(void)
865{
866 MigrationState *s;
867
868 s = migrate_get_current();
869
Liang Li43c60a82015-03-23 16:32:27 +0800870 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
Liang Li8706d2d2015-03-23 16:32:17 +0800871}
872
Liang Li3fcb38c2015-03-23 16:32:18 +0800873int migrate_decompress_threads(void)
874{
875 MigrationState *s;
876
877 s = migrate_get_current();
878
Liang Li43c60a82015-03-23 16:32:27 +0800879 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Liang Li3fcb38c2015-03-23 16:32:18 +0800880}
881
Juan Quintelab05dc722015-07-07 14:44:05 +0200882bool migrate_use_events(void)
883{
884 MigrationState *s;
885
886 s = migrate_get_current();
887
888 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
889}
890
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300891int migrate_use_xbzrle(void)
892{
893 MigrationState *s;
894
895 s = migrate_get_current();
896
897 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
898}
899
900int64_t migrate_xbzrle_cache_size(void)
901{
902 MigrationState *s;
903
904 s = migrate_get_current();
905
906 return s->xbzrle_cache_size;
907}
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200908
909/* migration thread support */
910
Juan Quintela5f496a12013-02-22 17:36:30 +0100911static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200912{
Juan Quintela9848a402012-12-19 09:55:50 +0100913 MigrationState *s = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100914 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
915 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +0100916 int64_t initial_bytes = 0;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200917 int64_t max_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100918 int64_t start_time = initial_time;
919 bool old_vm_running = false;
Juan Quintela76f59332012-10-03 20:16:24 +0200920
Paolo Bonziniab28bd22015-07-09 08:55:38 +0200921 rcu_register_thread();
922
Dr. David Alan Gilbertf796baa2015-05-21 13:24:12 +0100923 qemu_savevm_state_header(s->file);
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100924 qemu_savevm_state_begin(s->file, &s->params);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200925
Alex Blighbc72ad62013-08-21 16:03:08 +0100926 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang31194732015-03-13 16:08:38 +0800927 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400928
zhanghailiang31194732015-03-13 16:08:38 +0800929 while (s->state == MIGRATION_STATUS_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +0100930 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +0200931 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200932
Paolo Bonzinia0ff0442013-02-22 17:36:35 +0100933 if (!qemu_file_rate_limit(s->file)) {
Juan Quintelac369f402012-10-03 20:33:34 +0200934 pending_size = qemu_savevm_state_pending(s->file, max_size);
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100935 trace_migrate_pending(pending_size, max_size);
Juan Quintelab22ff1f2012-10-17 21:06:31 +0200936 if (pending_size && pending_size >= max_size) {
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100937 qemu_savevm_state_iterate(s->file);
Juan Quintelac369f402012-10-03 20:33:34 +0200938 } else {
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200939 int ret;
940
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100941 qemu_mutex_lock_iothread();
Alex Blighbc72ad62013-08-21 16:03:08 +0100942 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintelac369f402012-10-03 20:33:34 +0200943 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100944 old_vm_running = runstate_is_running();
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200945
Juan Quinteladf4b1022014-10-08 10:58:10 +0200946 ret = global_state_store();
947 if (!ret) {
948 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
949 if (ret >= 0) {
950 qemu_file_set_rate_limit(s->file, INT64_MAX);
951 qemu_savevm_state_complete(s->file);
952 }
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200953 }
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100954 qemu_mutex_unlock_iothread();
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200955
956 if (ret < 0) {
zhanghailiang31194732015-03-13 16:08:38 +0800957 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
958 MIGRATION_STATUS_FAILED);
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200959 break;
960 }
961
Paolo Bonzini059f8962013-02-22 17:36:32 +0100962 if (!qemu_file_get_error(s->file)) {
zhanghailiang31194732015-03-13 16:08:38 +0800963 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
964 MIGRATION_STATUS_COMPLETED);
Paolo Bonzini059f8962013-02-22 17:36:32 +0100965 break;
966 }
Juan Quintelac369f402012-10-03 20:33:34 +0200967 }
968 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +0100969
Paolo Bonzinifd45ee22013-02-22 17:36:33 +0100970 if (qemu_file_get_error(s->file)) {
zhanghailiang31194732015-03-13 16:08:38 +0800971 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
972 MIGRATION_STATUS_FAILED);
Paolo Bonzinifd45ee22013-02-22 17:36:33 +0100973 break;
974 }
Alex Blighbc72ad62013-08-21 16:03:08 +0100975 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200976 if (current_time >= initial_time + BUFFER_DELAY) {
Paolo Bonzinibe7172e2013-02-22 17:36:43 +0100977 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -0500978 uint64_t time_spent = current_time - initial_time;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200979 double bandwidth = transferred_bytes / time_spent;
980 max_size = bandwidth * migrate_max_downtime() / 1000000;
981
Michael R. Hines7e114f82013-06-25 21:35:30 -0400982 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
983 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
984
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100985 trace_migrate_transferred(transferred_bytes, time_spent,
986 bandwidth, max_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +0100987 /* if we haven't sent anything, we don't want to recalculate
988 10000 is a small enough number for our purposes */
989 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
990 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
991 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200992
Paolo Bonzini1964a392013-02-22 17:36:45 +0100993 qemu_file_reset_rate_limit(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200994 initial_time = current_time;
Paolo Bonzinibe7172e2013-02-22 17:36:43 +0100995 initial_bytes = qemu_ftell(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200996 }
Paolo Bonzinia0ff0442013-02-22 17:36:35 +0100997 if (qemu_file_rate_limit(s->file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200998 /* usleep expects microseconds */
999 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
1000 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001001 }
1002
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001003 qemu_mutex_lock_iothread();
zhanghailiang31194732015-03-13 16:08:38 +08001004 if (s->state == MIGRATION_STATUS_COMPLETED) {
Alex Blighbc72ad62013-08-21 16:03:08 +01001005 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Peter Lievend6ed7312014-05-12 10:46:00 +02001006 uint64_t transferred_bytes = qemu_ftell(s->file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001007 s->total_time = end_time - s->total_time;
1008 s->downtime = end_time - start_time;
Peter Lievend6ed7312014-05-12 10:46:00 +02001009 if (s->total_time) {
1010 s->mbps = (((double) transferred_bytes * 8.0) /
1011 ((double) s->total_time)) / 1000;
1012 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001013 runstate_set(RUN_STATE_POSTMIGRATE);
1014 } else {
1015 if (old_vm_running) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +01001016 vm_start();
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001017 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001018 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001019 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +01001020 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +01001021
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001022 rcu_unregister_thread();
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001023 return NULL;
1024}
1025
Juan Quintela9848a402012-12-19 09:55:50 +01001026void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001027{
Juan Quintelacc283e32013-02-01 11:12:26 +01001028 /* This is a best 1st approximation. ns to ms */
1029 s->expected_downtime = max_downtime/1000000;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001030 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001031
Paolo Bonzini442773c2013-02-22 17:36:44 +01001032 qemu_file_set_rate_limit(s->file,
1033 s->bandwidth_limit / XFER_LIMIT_RATIO);
1034
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02001035 /* Notify before starting migration thread */
1036 notifier_list_notify(&migration_state_notifiers, s);
1037
Liang Li8706d2d2015-03-23 16:32:17 +08001038 migrate_compress_threads_create();
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001039 qemu_thread_create(&s->thread, "migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001040 QEMU_THREAD_JOINABLE);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001041}