blob: 5c1233fb0166e0f81b24d7f782ca0a5e15de8d60 [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 Bonzinicaf71f82012-12-17 18:19:50 +010025#include "migration/block.h"
Juan Quintela766bd172012-07-23 05:45:29 +020026#include "qemu/thread.h"
Luiz Capitulino791e7c82011-09-13 17:37:16 -030027#include "qmp-commands.h"
Kazuya Saitoc09e5bb2013-02-22 17:36:19 +010028#include "trace.h"
Juan Quinteladf4b1022014-10-08 10:58:10 +020029#include "qapi/util.h"
aliguori065e2812008-11-11 16:46:33 +000030
Juan Quintelad0ae46c2011-02-23 00:33:19 +010031#define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000032
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010033/* Amount of time to allocate to each "chunk" of bandwidth-throttled
34 * data. */
35#define BUFFER_DELAY 100
36#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
37
Liang Li8706d2d2015-03-23 16:32:17 +080038/* Default compression thread count */
39#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080040/* Default decompression thread count, usually decompression is at
41 * least 4 times as fast as compression.*/
42#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080043/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
44#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
45
Orit Wasserman17ad9b32012-08-06 21:42:53 +030046/* Migration XBZRLE default cache size */
47#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
48
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010049static NotifierList migration_state_notifiers =
50 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
51
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000052static bool deferred_incoming;
53
Juan Quintela17549e82011-10-05 13:50:43 +020054/* When we add fault tolerance, we could have several
55 migrations at once. For now we don't need to add
56 dynamic creation of migration */
57
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010058/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +020059MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +020060{
61 static MigrationState current_migration = {
zhanghailiang31194732015-03-13 16:08:38 +080062 .state = MIGRATION_STATUS_NONE,
Juan Quintelad0ae46c2011-02-23 00:33:19 +010063 .bandwidth_limit = MAX_THROTTLE,
Orit Wasserman17ad9b32012-08-06 21:42:53 +030064 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
Michael R. Hines7e114f82013-06-25 21:35:30 -040065 .mbps = -1,
Liang Li43c60a82015-03-23 16:32:27 +080066 .parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] =
67 DEFAULT_MIGRATE_COMPRESS_LEVEL,
68 .parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
69 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
70 .parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
71 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
Juan Quintela17549e82011-10-05 13:50:43 +020072 };
73
74 return &current_migration;
75}
76
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010077/* For incoming */
78static MigrationIncomingState *mis_current;
79
80MigrationIncomingState *migration_incoming_get_current(void)
81{
82 return mis_current;
83}
84
85MigrationIncomingState *migration_incoming_state_new(QEMUFile* f)
86{
87 mis_current = g_malloc0(sizeof(MigrationIncomingState));
88 mis_current->file = f;
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +010089 QLIST_INIT(&mis_current->loadvm_handlers);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010090
91 return mis_current;
92}
93
94void migration_incoming_state_destroy(void)
95{
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +010096 loadvm_free_handlers(mis_current);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010097 g_free(mis_current);
98 mis_current = NULL;
99}
100
Juan Quinteladf4b1022014-10-08 10:58:10 +0200101
102typedef struct {
Juan Quintela13d16812014-10-08 13:58:24 +0200103 bool optional;
Juan Quinteladf4b1022014-10-08 10:58:10 +0200104 uint32_t size;
105 uint8_t runstate[100];
106} GlobalState;
107
108static GlobalState global_state;
109
110static int global_state_store(void)
111{
112 if (!runstate_store((char *)global_state.runstate,
113 sizeof(global_state.runstate))) {
114 error_report("runstate name too big: %s", global_state.runstate);
115 trace_migrate_state_too_big();
116 return -EINVAL;
117 }
118 return 0;
119}
120
121static char *global_state_get_runstate(void)
122{
123 return (char *)global_state.runstate;
124}
125
Juan Quintela13d16812014-10-08 13:58:24 +0200126void global_state_set_optional(void)
127{
128 global_state.optional = true;
129}
130
131static bool global_state_needed(void *opaque)
132{
133 GlobalState *s = opaque;
134 char *runstate = (char *)s->runstate;
135
136 /* If it is not optional, it is mandatory */
137
138 if (s->optional == false) {
139 return true;
140 }
141
142 /* If state is running or paused, it is not needed */
143
144 if (strcmp(runstate, "running") == 0 ||
145 strcmp(runstate, "paused") == 0) {
146 return false;
147 }
148
149 /* for any other state it is needed */
150 return true;
151}
152
Juan Quinteladf4b1022014-10-08 10:58:10 +0200153static int global_state_post_load(void *opaque, int version_id)
154{
155 GlobalState *s = opaque;
156 int ret = 0;
157 char *runstate = (char *)s->runstate;
158
159 trace_migrate_global_state_post_load(runstate);
160
161 if (strcmp(runstate, "running") != 0) {
162 Error *local_err = NULL;
163 int r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE_MAX,
164 -1, &local_err);
165
166 if (r == -1) {
167 if (local_err) {
168 error_report_err(local_err);
169 }
170 return -EINVAL;
171 }
172 ret = vm_stop_force_state(r);
173 }
174
175 return ret;
176}
177
178static void global_state_pre_save(void *opaque)
179{
180 GlobalState *s = opaque;
181
182 trace_migrate_global_state_pre_save((char *)s->runstate);
183 s->size = strlen((char *)s->runstate) + 1;
184}
185
186static const VMStateDescription vmstate_globalstate = {
187 .name = "globalstate",
188 .version_id = 1,
189 .minimum_version_id = 1,
190 .post_load = global_state_post_load,
191 .pre_save = global_state_pre_save,
Juan Quintela13d16812014-10-08 13:58:24 +0200192 .needed = global_state_needed,
Juan Quinteladf4b1022014-10-08 10:58:10 +0200193 .fields = (VMStateField[]) {
194 VMSTATE_UINT32(size, GlobalState),
195 VMSTATE_BUFFER(runstate, GlobalState),
196 VMSTATE_END_OF_LIST()
197 },
198};
199
200void register_global_state(void)
201{
202 /* We would use it independently that we receive it */
203 strcpy((char *)&global_state.runstate, "");
204 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
205}
206
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000207/*
208 * Called on -incoming with a defer: uri.
209 * The migration can be started later after any parameters have been
210 * changed.
211 */
212static void deferred_incoming_migration(Error **errp)
213{
214 if (deferred_incoming) {
215 error_setg(errp, "Incoming migration already deferred");
216 }
217 deferred_incoming = true;
218}
219
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200220void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000221{
aliguori34c9dd82008-10-13 03:14:31 +0000222 const char *p;
223
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000224 if (!strcmp(uri, "defer")) {
225 deferred_incoming_migration(errp);
226 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200227 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400228#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000229 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400230 rdma_start_incoming_migration(p, errp);
231#endif
aliguori065e2812008-11-11 16:46:33 +0000232#if !defined(WIN32)
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000233 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200234 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000235 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200236 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000237 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200238 fd_start_incoming_migration(p, errp);
aliguori065e2812008-11-11 16:46:33 +0000239#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000240 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100241 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200242 }
aliguori5bb79102008-10-13 03:12:02 +0000243}
244
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200245static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200246{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200247 QEMUFile *f = opaque;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100248 Error *local_err = NULL;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200249 int ret;
250
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100251 migration_incoming_state_new(f);
252
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200253 ret = qemu_loadvm_state(f);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100254
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200255 qemu_fclose(f);
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200256 free_xbzrle_decoded_buf();
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100257 migration_incoming_state_destroy();
258
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200259 if (ret < 0) {
Peter Lievendb80fac2014-06-10 11:29:16 +0200260 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800261 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600262 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200263 }
264 qemu_announce_self();
Juan Quintela511c0232010-06-09 14:10:55 +0200265
Anthony Liguori0f154232011-11-14 15:09:45 -0600266 /* Make sure all file formats flush their mutable metadata */
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100267 bdrv_invalidate_cache_all(&local_err);
268 if (local_err) {
Markus Armbruster97baf9d2015-02-18 19:21:52 +0100269 error_report_err(local_err);
Liang Li3fcb38c2015-03-23 16:32:18 +0800270 migrate_decompress_threads_join();
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100271 exit(EXIT_FAILURE);
272 }
Anthony Liguori0f154232011-11-14 15:09:45 -0600273
Juan Quinteladf4b1022014-10-08 10:58:10 +0200274 /* runstate == "" means that we haven't received it through the
275 * wire, so we obey autostart. runstate == runing means that we
276 * need to run it, we need to make sure that we do it after
277 * everything else has finished. Every other state change is done
278 * at the post_load function */
279
280 if (strcmp(global_state_get_runstate(), "running") == 0) {
Juan Quintela511c0232010-06-09 14:10:55 +0200281 vm_start();
Juan Quinteladf4b1022014-10-08 10:58:10 +0200282 } else if (strcmp(global_state_get_runstate(), "") == 0) {
283 if (autostart) {
284 vm_start();
285 } else {
286 runstate_set(RUN_STATE_PAUSED);
287 }
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300288 }
Liang Li3fcb38c2015-03-23 16:32:18 +0800289 migrate_decompress_threads_join();
Juan Quintela511c0232010-06-09 14:10:55 +0200290}
291
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200292void process_incoming_migration(QEMUFile *f)
293{
294 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
295 int fd = qemu_get_fd(f);
296
297 assert(fd != -1);
Liang Li3fcb38c2015-03-23 16:32:18 +0800298 migrate_decompress_threads_create();
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100299 qemu_set_nonblock(fd);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200300 qemu_coroutine_enter(co, f);
301}
302
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400303/* amount of nanoseconds we are willing to wait for migration to be down.
304 * the choice of nanoseconds is because it is the maximum resolution that
305 * get_clock() can achieve. It is an internal measure. All user-visible
306 * units must be in seconds */
Alexey Kardashevskiyf7cd55a2014-03-27 14:57:26 +1100307static uint64_t max_downtime = 300000000;
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400308
309uint64_t migrate_max_downtime(void)
310{
311 return max_downtime;
312}
313
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300314MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
315{
316 MigrationCapabilityStatusList *head = NULL;
317 MigrationCapabilityStatusList *caps;
318 MigrationState *s = migrate_get_current();
319 int i;
320
Michael Tokarev387eede2013-10-05 13:18:28 +0400321 caps = NULL; /* silence compiler warning */
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300322 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
323 if (head == NULL) {
324 head = g_malloc0(sizeof(*caps));
325 caps = head;
326 } else {
327 caps->next = g_malloc0(sizeof(*caps));
328 caps = caps->next;
329 }
330 caps->value =
331 g_malloc(sizeof(*caps->value));
332 caps->value->capability = i;
333 caps->value->state = s->enabled_capabilities[i];
334 }
335
336 return head;
337}
338
Liang Li85de8322015-03-23 16:32:28 +0800339MigrationParameters *qmp_query_migrate_parameters(Error **errp)
340{
341 MigrationParameters *params;
342 MigrationState *s = migrate_get_current();
343
344 params = g_malloc0(sizeof(*params));
345 params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
346 params->compress_threads =
347 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
348 params->decompress_threads =
349 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
350
351 return params;
352}
353
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300354static void get_xbzrle_cache_stats(MigrationInfo *info)
355{
356 if (migrate_use_xbzrle()) {
357 info->has_xbzrle_cache = true;
358 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
359 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
360 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
361 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
362 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
ChenLiang8bc39232014-04-04 17:57:56 +0800363 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300364 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
365 }
366}
367
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300368MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000369{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300370 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200371 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000372
Juan Quintela17549e82011-10-05 13:50:43 +0200373 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800374 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200375 /* no migration has happened ever */
376 break;
zhanghailiang31194732015-03-13 16:08:38 +0800377 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400378 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400379 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400380 break;
zhanghailiang31194732015-03-13 16:08:38 +0800381 case MIGRATION_STATUS_ACTIVE:
382 case MIGRATION_STATUS_CANCELLING:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300383 info->has_status = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200384 info->has_total_time = true;
Alex Blighbc72ad62013-08-21 16:03:08 +0100385 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Juan Quintela7aa939a2012-08-18 13:17:10 +0200386 - s->total_time;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200387 info->has_expected_downtime = true;
388 info->expected_downtime = s->expected_downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400389 info->has_setup_time = true;
390 info->setup_time = s->setup_time;
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200391
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300392 info->has_ram = true;
393 info->ram = g_malloc0(sizeof(*info->ram));
394 info->ram->transferred = ram_bytes_transferred();
395 info->ram->remaining = ram_bytes_remaining();
396 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300397 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100398 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300399 info->ram->normal = norm_mig_pages_transferred();
400 info->ram->normal_bytes = norm_mig_bytes_transferred();
Juan Quintela8d017192012-08-13 12:31:25 +0200401 info->ram->dirty_pages_rate = s->dirty_pages_rate;
Michael R. Hines7e114f82013-06-25 21:35:30 -0400402 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800403 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela8d017192012-08-13 12:31:25 +0200404
Juan Quintela17549e82011-10-05 13:50:43 +0200405 if (blk_mig_active()) {
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300406 info->has_disk = true;
407 info->disk = g_malloc0(sizeof(*info->disk));
408 info->disk->transferred = blk_mig_bytes_transferred();
409 info->disk->remaining = blk_mig_bytes_remaining();
410 info->disk->total = blk_mig_bytes_total();
aliguoriff8d81d2008-10-24 22:10:31 +0000411 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300412
413 get_xbzrle_cache_stats(info);
Juan Quintela17549e82011-10-05 13:50:43 +0200414 break;
zhanghailiang31194732015-03-13 16:08:38 +0800415 case MIGRATION_STATUS_COMPLETED:
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300416 get_xbzrle_cache_stats(info);
417
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300418 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900419 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200420 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200421 info->has_downtime = true;
422 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400423 info->has_setup_time = true;
424 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200425
426 info->has_ram = true;
427 info->ram = g_malloc0(sizeof(*info->ram));
428 info->ram->transferred = ram_bytes_transferred();
429 info->ram->remaining = 0;
430 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300431 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100432 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300433 info->ram->normal = norm_mig_pages_transferred();
434 info->ram->normal_bytes = norm_mig_bytes_transferred();
Michael R. Hines7e114f82013-06-25 21:35:30 -0400435 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800436 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela17549e82011-10-05 13:50:43 +0200437 break;
zhanghailiang31194732015-03-13 16:08:38 +0800438 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300439 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200440 break;
zhanghailiang31194732015-03-13 16:08:38 +0800441 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300442 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200443 break;
aliguori5bb79102008-10-13 03:12:02 +0000444 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800445 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300446
447 return info;
aliguori5bb79102008-10-13 03:12:02 +0000448}
449
Orit Wasserman00458432012-08-06 21:42:48 +0300450void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
451 Error **errp)
452{
453 MigrationState *s = migrate_get_current();
454 MigrationCapabilityStatusList *cap;
455
zhanghailiang31194732015-03-13 16:08:38 +0800456 if (s->state == MIGRATION_STATUS_ACTIVE ||
457 s->state == MIGRATION_STATUS_SETUP) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100458 error_setg(errp, QERR_MIGRATION_ACTIVE);
Orit Wasserman00458432012-08-06 21:42:48 +0300459 return;
460 }
461
462 for (cap = params; cap; cap = cap->next) {
463 s->enabled_capabilities[cap->value->capability] = cap->value->state;
464 }
465}
466
Liang Li85de8322015-03-23 16:32:28 +0800467void qmp_migrate_set_parameters(bool has_compress_level,
468 int64_t compress_level,
469 bool has_compress_threads,
470 int64_t compress_threads,
471 bool has_decompress_threads,
472 int64_t decompress_threads, Error **errp)
473{
474 MigrationState *s = migrate_get_current();
475
476 if (has_compress_level && (compress_level < 0 || compress_level > 9)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100477 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
478 "is invalid, it should be in the range of 0 to 9");
Liang Li85de8322015-03-23 16:32:28 +0800479 return;
480 }
481 if (has_compress_threads &&
482 (compress_threads < 1 || compress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100483 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
484 "compress_threads",
485 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800486 return;
487 }
488 if (has_decompress_threads &&
489 (decompress_threads < 1 || decompress_threads > 255)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100490 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
491 "decompress_threads",
492 "is invalid, it should be in the range of 1 to 255");
Liang Li85de8322015-03-23 16:32:28 +0800493 return;
494 }
495
496 if (has_compress_level) {
497 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
498 }
499 if (has_compress_threads) {
500 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads;
501 }
502 if (has_decompress_threads) {
503 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
504 decompress_threads;
505 }
506}
507
aliguori065e2812008-11-11 16:46:33 +0000508/* shared migration helpers */
509
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000510static void migrate_set_state(MigrationState *s, int old_state, int new_state)
511{
Juan Quintelaa5c17b52015-06-17 02:06:20 +0200512 if (atomic_cmpxchg(&s->state, old_state, new_state) == old_state) {
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000513 trace_migrate_set_state(new_state);
514 }
515}
516
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100517static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000518{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100519 MigrationState *s = opaque;
520
521 qemu_bh_delete(s->cleanup_bh);
522 s->cleanup_bh = NULL;
523
aliguori065e2812008-11-11 16:46:33 +0000524 if (s->file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100525 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100526 qemu_mutex_unlock_iothread();
527 qemu_thread_join(&s->thread);
528 qemu_mutex_lock_iothread();
529
Liang Li8706d2d2015-03-23 16:32:17 +0800530 migrate_compress_threads_join();
Paolo Bonzini6f190a02013-02-22 17:36:48 +0100531 qemu_fclose(s->file);
532 s->file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000533 }
534
zhanghailiang31194732015-03-13 16:08:38 +0800535 assert(s->state != MIGRATION_STATUS_ACTIVE);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100536
zhanghailiang31194732015-03-13 16:08:38 +0800537 if (s->state != MIGRATION_STATUS_COMPLETED) {
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100538 qemu_savevm_state_cancel();
zhanghailiang31194732015-03-13 16:08:38 +0800539 if (s->state == MIGRATION_STATUS_CANCELLING) {
540 migrate_set_state(s, MIGRATION_STATUS_CANCELLING,
541 MIGRATION_STATUS_CANCELLED);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000542 }
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100543 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100544
545 notifier_list_notify(&migration_state_notifiers, s);
aliguori065e2812008-11-11 16:46:33 +0000546}
547
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200548void migrate_fd_error(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000549{
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100550 trace_migrate_fd_error();
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100551 assert(s->file == NULL);
zhanghailiang31194732015-03-13 16:08:38 +0800552 s->state = MIGRATION_STATUS_FAILED;
553 trace_migrate_set_state(MIGRATION_STATUS_FAILED);
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100554 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela458cf282011-02-22 23:32:54 +0100555}
556
Juan Quintela0edda1c2010-05-11 16:28:39 +0200557static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000558{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000559 int old_state ;
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000560 QEMUFile *f = migrate_get_current()->file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100561 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000562
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000563 do {
564 old_state = s->state;
zhanghailiang31194732015-03-13 16:08:38 +0800565 if (old_state != MIGRATION_STATUS_SETUP &&
566 old_state != MIGRATION_STATUS_ACTIVE) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000567 break;
568 }
zhanghailiang31194732015-03-13 16:08:38 +0800569 migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
570 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000571
572 /*
573 * If we're unlucky the migration code might be stuck somewhere in a
574 * send/write while the network has failed and is waiting to timeout;
575 * if we've got shutdown(2) available then we can force it to quit.
576 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
577 * called in a bh, so there is no race against this cancel.
578 */
zhanghailiang31194732015-03-13 16:08:38 +0800579 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000580 qemu_file_shutdown(f);
581 }
aliguori065e2812008-11-11 16:46:33 +0000582}
583
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100584void add_migration_state_change_notifier(Notifier *notify)
585{
586 notifier_list_add(&migration_state_notifiers, notify);
587}
588
589void remove_migration_state_change_notifier(Notifier *notify)
590{
Paolo Bonzini31552522012-01-13 17:34:01 +0100591 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100592}
593
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +0200594bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200595{
zhanghailiang31194732015-03-13 16:08:38 +0800596 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200597}
598
Juan Quintela70736932011-02-23 00:43:59 +0100599bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100600{
zhanghailiang31194732015-03-13 16:08:38 +0800601 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100602}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200603
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200604bool migration_has_failed(MigrationState *s)
605{
zhanghailiang31194732015-03-13 16:08:38 +0800606 return (s->state == MIGRATION_STATUS_CANCELLED ||
607 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200608}
609
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300610static MigrationState *migrate_init(const MigrationParams *params)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200611{
Juan Quintela17549e82011-10-05 13:50:43 +0200612 MigrationState *s = migrate_get_current();
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100613 int64_t bandwidth_limit = s->bandwidth_limit;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300614 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300615 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
Liang Li43c60a82015-03-23 16:32:27 +0800616 int compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
617 int compress_thread_count =
618 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
619 int decompress_thread_count =
620 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300621
622 memcpy(enabled_capabilities, s->enabled_capabilities,
623 sizeof(enabled_capabilities));
Juan Quintela0edda1c2010-05-11 16:28:39 +0200624
Juan Quintela17549e82011-10-05 13:50:43 +0200625 memset(s, 0, sizeof(*s));
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300626 s->params = *params;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300627 memcpy(s->enabled_capabilities, enabled_capabilities,
628 sizeof(enabled_capabilities));
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300629 s->xbzrle_cache_size = xbzrle_cache_size;
Juan Quintela1299c632011-11-09 21:29:01 +0100630
Liang Li43c60a82015-03-23 16:32:27 +0800631 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
632 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
633 compress_thread_count;
634 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
635 decompress_thread_count;
Juan Quintela0edda1c2010-05-11 16:28:39 +0200636 s->bandwidth_limit = bandwidth_limit;
zhanghailiang31194732015-03-13 16:08:38 +0800637 s->state = MIGRATION_STATUS_SETUP;
638 trace_migrate_set_state(MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200639
Alex Blighbc72ad62013-08-21 16:03:08 +0100640 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200641 return s;
642}
Juan Quintelacab30142011-02-22 23:54:21 +0100643
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600644static GSList *migration_blockers;
645
646void migrate_add_blocker(Error *reason)
647{
648 migration_blockers = g_slist_prepend(migration_blockers, reason);
649}
650
651void migrate_del_blocker(Error *reason)
652{
653 migration_blockers = g_slist_remove(migration_blockers, reason);
654}
655
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000656void qmp_migrate_incoming(const char *uri, Error **errp)
657{
658 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000659 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000660
661 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000662 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000663 return;
664 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000665 if (!once) {
666 error_setg(errp, "The incoming migration has already been started");
667 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000668
669 qemu_start_incoming_migration(uri, &local_err);
670
671 if (local_err) {
672 error_propagate(errp, local_err);
673 return;
674 }
675
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000676 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000677}
678
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200679void qmp_migrate(const char *uri, bool has_blk, bool blk,
680 bool has_inc, bool inc, bool has_detach, bool detach,
681 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100682{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +0200683 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +0200684 MigrationState *s = migrate_get_current();
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300685 MigrationParams params;
Juan Quintelacab30142011-02-22 23:54:21 +0100686 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +0100687
Pawit Pornkitprasan8c0426a2013-07-30 08:39:52 +0900688 params.blk = has_blk && blk;
689 params.shared = has_inc && inc;
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300690
zhanghailiang31194732015-03-13 16:08:38 +0800691 if (s->state == MIGRATION_STATUS_ACTIVE ||
692 s->state == MIGRATION_STATUS_SETUP ||
693 s->state == MIGRATION_STATUS_CANCELLING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100694 error_setg(errp, QERR_MIGRATION_ACTIVE);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200695 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100696 }
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +0100697 if (runstate_check(RUN_STATE_INMIGRATE)) {
698 error_setg(errp, "Guest is waiting for an incoming migration");
699 return;
700 }
701
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200702 if (qemu_savevm_state_blocked(errp)) {
703 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100704 }
705
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600706 if (migration_blockers) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200707 *errp = error_copy(migration_blockers->data);
708 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600709 }
710
Juan Quintela656a2332015-07-01 09:32:29 +0200711 /* We are starting a new migration, so we want to start in a clean
712 state. This change is only needed if previous migration
713 failed/was cancelled. We don't use migrate_set_state() because
714 we are setting the initial state, not changing it. */
715 s->state = MIGRATION_STATUS_NONE;
716
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300717 s = migrate_init(&params);
Juan Quintelacab30142011-02-22 23:54:21 +0100718
719 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200720 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400721#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +0800722 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400723 rdma_start_outgoing_migration(s, p, &local_err);
724#endif
Juan Quintelacab30142011-02-22 23:54:21 +0100725#if !defined(WIN32)
726 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200727 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100728 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200729 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100730 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200731 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100732#endif
733 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100734 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
735 "a valid migration protocol");
zhanghailiang31194732015-03-13 16:08:38 +0800736 s->state = MIGRATION_STATUS_FAILED;
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200737 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100738 }
739
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200740 if (local_err) {
Paolo Bonzini342ab8d2012-10-02 09:59:38 +0200741 migrate_fd_error(s);
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200742 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200743 return;
Juan Quintela1299c632011-11-09 21:29:01 +0100744 }
Juan Quintelacab30142011-02-22 23:54:21 +0100745}
746
Luiz Capitulino6cdedb02011-11-27 22:54:09 -0200747void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100748{
Juan Quintela17549e82011-10-05 13:50:43 +0200749 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +0100750}
751
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300752void qmp_migrate_set_cache_size(int64_t value, Error **errp)
753{
754 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +0200755 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300756
757 /* Check for truncation */
758 if (value != (size_t)value) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100759 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
760 "exceeding address space");
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300761 return;
762 }
763
Orit Wassermana5615b12014-01-30 20:08:36 +0200764 /* Cache should not be larger than guest ram size */
765 if (value > ram_bytes_total()) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100766 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
767 "exceeds guest ram size ");
Orit Wassermana5615b12014-01-30 20:08:36 +0200768 return;
769 }
770
Orit Wassermanc91e6812014-01-30 20:08:34 +0200771 new_size = xbzrle_cache_resize(value);
772 if (new_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100773 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
774 "is smaller than page size");
Orit Wassermanc91e6812014-01-30 20:08:34 +0200775 return;
776 }
777
778 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300779}
780
781int64_t qmp_query_migrate_cache_size(Error **errp)
782{
783 return migrate_xbzrle_cache_size();
784}
785
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200786void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100787{
Juan Quintelacab30142011-02-22 23:54:21 +0100788 MigrationState *s;
789
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200790 if (value < 0) {
791 value = 0;
Juan Quintelacab30142011-02-22 23:54:21 +0100792 }
Paolo Bonzini442773c2013-02-22 17:36:44 +0100793 if (value > SIZE_MAX) {
794 value = SIZE_MAX;
795 }
Juan Quintelacab30142011-02-22 23:54:21 +0100796
Juan Quintela17549e82011-10-05 13:50:43 +0200797 s = migrate_get_current();
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200798 s->bandwidth_limit = value;
Paolo Bonzini442773c2013-02-22 17:36:44 +0100799 if (s->file) {
800 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
801 }
Juan Quintelacab30142011-02-22 23:54:21 +0100802}
803
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200804void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100805{
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200806 value *= 1e9;
807 value = MAX(0, MIN(UINT64_MAX, value));
808 max_downtime = (uint64_t)value;
aliguori5bb79102008-10-13 03:12:02 +0000809}
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300810
Chegu Vinodbde1e2e2013-06-24 03:49:42 -0600811bool migrate_auto_converge(void)
812{
813 MigrationState *s;
814
815 s = migrate_get_current();
816
817 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
818}
819
Peter Lieven323004a2013-07-18 09:48:50 +0200820bool migrate_zero_blocks(void)
821{
822 MigrationState *s;
823
824 s = migrate_get_current();
825
826 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
827}
828
Liang Li8706d2d2015-03-23 16:32:17 +0800829bool migrate_use_compression(void)
830{
Liang Lidde4e692015-03-23 16:32:26 +0800831 MigrationState *s;
832
833 s = migrate_get_current();
834
835 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +0800836}
837
838int migrate_compress_level(void)
839{
840 MigrationState *s;
841
842 s = migrate_get_current();
843
Liang Li43c60a82015-03-23 16:32:27 +0800844 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
Liang Li8706d2d2015-03-23 16:32:17 +0800845}
846
847int migrate_compress_threads(void)
848{
849 MigrationState *s;
850
851 s = migrate_get_current();
852
Liang Li43c60a82015-03-23 16:32:27 +0800853 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
Liang Li8706d2d2015-03-23 16:32:17 +0800854}
855
Liang Li3fcb38c2015-03-23 16:32:18 +0800856int migrate_decompress_threads(void)
857{
858 MigrationState *s;
859
860 s = migrate_get_current();
861
Liang Li43c60a82015-03-23 16:32:27 +0800862 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Liang Li3fcb38c2015-03-23 16:32:18 +0800863}
864
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300865int migrate_use_xbzrle(void)
866{
867 MigrationState *s;
868
869 s = migrate_get_current();
870
871 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
872}
873
874int64_t migrate_xbzrle_cache_size(void)
875{
876 MigrationState *s;
877
878 s = migrate_get_current();
879
880 return s->xbzrle_cache_size;
881}
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200882
883/* migration thread support */
884
Juan Quintela5f496a12013-02-22 17:36:30 +0100885static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200886{
Juan Quintela9848a402012-12-19 09:55:50 +0100887 MigrationState *s = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100888 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
889 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +0100890 int64_t initial_bytes = 0;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200891 int64_t max_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100892 int64_t start_time = initial_time;
893 bool old_vm_running = false;
Juan Quintela76f59332012-10-03 20:16:24 +0200894
Dr. David Alan Gilbertf796baa2015-05-21 13:24:12 +0100895 qemu_savevm_state_header(s->file);
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100896 qemu_savevm_state_begin(s->file, &s->params);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200897
Alex Blighbc72ad62013-08-21 16:03:08 +0100898 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang31194732015-03-13 16:08:38 +0800899 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400900
zhanghailiang31194732015-03-13 16:08:38 +0800901 while (s->state == MIGRATION_STATUS_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +0100902 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +0200903 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200904
Paolo Bonzinia0ff0442013-02-22 17:36:35 +0100905 if (!qemu_file_rate_limit(s->file)) {
Juan Quintelac369f402012-10-03 20:33:34 +0200906 pending_size = qemu_savevm_state_pending(s->file, max_size);
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100907 trace_migrate_pending(pending_size, max_size);
Juan Quintelab22ff1f2012-10-17 21:06:31 +0200908 if (pending_size && pending_size >= max_size) {
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100909 qemu_savevm_state_iterate(s->file);
Juan Quintelac369f402012-10-03 20:33:34 +0200910 } else {
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200911 int ret;
912
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100913 qemu_mutex_lock_iothread();
Alex Blighbc72ad62013-08-21 16:03:08 +0100914 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintelac369f402012-10-03 20:33:34 +0200915 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100916 old_vm_running = runstate_is_running();
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200917
Juan Quinteladf4b1022014-10-08 10:58:10 +0200918 ret = global_state_store();
919 if (!ret) {
920 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
921 if (ret >= 0) {
922 qemu_file_set_rate_limit(s->file, INT64_MAX);
923 qemu_savevm_state_complete(s->file);
924 }
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200925 }
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100926 qemu_mutex_unlock_iothread();
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200927
928 if (ret < 0) {
zhanghailiang31194732015-03-13 16:08:38 +0800929 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
930 MIGRATION_STATUS_FAILED);
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200931 break;
932 }
933
Paolo Bonzini059f8962013-02-22 17:36:32 +0100934 if (!qemu_file_get_error(s->file)) {
zhanghailiang31194732015-03-13 16:08:38 +0800935 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
936 MIGRATION_STATUS_COMPLETED);
Paolo Bonzini059f8962013-02-22 17:36:32 +0100937 break;
938 }
Juan Quintelac369f402012-10-03 20:33:34 +0200939 }
940 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +0100941
Paolo Bonzinifd45ee22013-02-22 17:36:33 +0100942 if (qemu_file_get_error(s->file)) {
zhanghailiang31194732015-03-13 16:08:38 +0800943 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
944 MIGRATION_STATUS_FAILED);
Paolo Bonzinifd45ee22013-02-22 17:36:33 +0100945 break;
946 }
Alex Blighbc72ad62013-08-21 16:03:08 +0100947 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200948 if (current_time >= initial_time + BUFFER_DELAY) {
Paolo Bonzinibe7172e2013-02-22 17:36:43 +0100949 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -0500950 uint64_t time_spent = current_time - initial_time;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200951 double bandwidth = transferred_bytes / time_spent;
952 max_size = bandwidth * migrate_max_downtime() / 1000000;
953
Michael R. Hines7e114f82013-06-25 21:35:30 -0400954 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
955 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
956
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100957 trace_migrate_transferred(transferred_bytes, time_spent,
958 bandwidth, max_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +0100959 /* if we haven't sent anything, we don't want to recalculate
960 10000 is a small enough number for our purposes */
961 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
962 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
963 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200964
Paolo Bonzini1964a392013-02-22 17:36:45 +0100965 qemu_file_reset_rate_limit(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200966 initial_time = current_time;
Paolo Bonzinibe7172e2013-02-22 17:36:43 +0100967 initial_bytes = qemu_ftell(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200968 }
Paolo Bonzinia0ff0442013-02-22 17:36:35 +0100969 if (qemu_file_rate_limit(s->file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200970 /* usleep expects microseconds */
971 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
972 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100973 }
974
Paolo Bonzinif4410a52013-02-22 17:36:20 +0100975 qemu_mutex_lock_iothread();
zhanghailiang31194732015-03-13 16:08:38 +0800976 if (s->state == MIGRATION_STATUS_COMPLETED) {
Alex Blighbc72ad62013-08-21 16:03:08 +0100977 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Peter Lievend6ed7312014-05-12 10:46:00 +0200978 uint64_t transferred_bytes = qemu_ftell(s->file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100979 s->total_time = end_time - s->total_time;
980 s->downtime = end_time - start_time;
Peter Lievend6ed7312014-05-12 10:46:00 +0200981 if (s->total_time) {
982 s->mbps = (((double) transferred_bytes * 8.0) /
983 ((double) s->total_time)) / 1000;
984 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100985 runstate_set(RUN_STATE_POSTMIGRATE);
986 } else {
987 if (old_vm_running) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100988 vm_start();
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100989 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200990 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100991 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100992 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +0100993
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200994 return NULL;
995}
996
Juan Quintela9848a402012-12-19 09:55:50 +0100997void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200998{
Juan Quintelacc283e32013-02-01 11:12:26 +0100999 /* This is a best 1st approximation. ns to ms */
1000 s->expected_downtime = max_downtime/1000000;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001001 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001002
Paolo Bonzini442773c2013-02-22 17:36:44 +01001003 qemu_file_set_rate_limit(s->file,
1004 s->bandwidth_limit / XFER_LIMIT_RATIO);
1005
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +02001006 /* Notify before starting migration thread */
1007 notifier_list_notify(&migration_state_notifiers, s);
1008
Liang Li8706d2d2015-03-23 16:32:17 +08001009 migrate_compress_threads_create();
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001010 qemu_thread_create(&s->thread, "migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +01001011 QEMU_THREAD_JOINABLE);
Juan Quintela0d82d0e2012-10-03 14:18:33 +02001012}