blob: b04b4571a84744a9c5cf81f7dbd173deeec04343 [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"
Alex Bligh6a1751b2013-08-21 16:02:47 +010017#include "qemu/main-loop.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010018#include "migration/migration.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010019#include "monitor/monitor.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"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010023#include "qemu/sockets.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010024#include "migration/block.h"
Juan Quintela766bd172012-07-23 05:45:29 +020025#include "qemu/thread.h"
Luiz Capitulino791e7c82011-09-13 17:37:16 -030026#include "qmp-commands.h"
Kazuya Saitoc09e5bb2013-02-22 17:36:19 +010027#include "trace.h"
aliguori065e2812008-11-11 16:46:33 +000028
Juan Quintelad0ae46c2011-02-23 00:33:19 +010029#define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000030
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010031/* Amount of time to allocate to each "chunk" of bandwidth-throttled
32 * data. */
33#define BUFFER_DELAY 100
34#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
35
Liang Li8706d2d2015-03-23 16:32:17 +080036/* Default compression thread count */
37#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
Liang Li3fcb38c2015-03-23 16:32:18 +080038/* Default decompression thread count, usually decompression is at
39 * least 4 times as fast as compression.*/
40#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
Liang Li8706d2d2015-03-23 16:32:17 +080041/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
42#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
43
Orit Wasserman17ad9b32012-08-06 21:42:53 +030044/* Migration XBZRLE default cache size */
45#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
46
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010047static NotifierList migration_state_notifiers =
48 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
49
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000050static bool deferred_incoming;
51
Juan Quintela17549e82011-10-05 13:50:43 +020052/* When we add fault tolerance, we could have several
53 migrations at once. For now we don't need to add
54 dynamic creation of migration */
55
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010056/* For outgoing */
Juan Quintela859bc752012-08-13 09:42:49 +020057MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +020058{
59 static MigrationState current_migration = {
zhanghailiang31194732015-03-13 16:08:38 +080060 .state = MIGRATION_STATUS_NONE,
Juan Quintelad0ae46c2011-02-23 00:33:19 +010061 .bandwidth_limit = MAX_THROTTLE,
Orit Wasserman17ad9b32012-08-06 21:42:53 +030062 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
Michael R. Hines7e114f82013-06-25 21:35:30 -040063 .mbps = -1,
Liang Li43c60a82015-03-23 16:32:27 +080064 .parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] =
65 DEFAULT_MIGRATE_COMPRESS_LEVEL,
66 .parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
67 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
68 .parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
69 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
Juan Quintela17549e82011-10-05 13:50:43 +020070 };
71
72 return &current_migration;
73}
74
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010075/* For incoming */
76static MigrationIncomingState *mis_current;
77
78MigrationIncomingState *migration_incoming_get_current(void)
79{
80 return mis_current;
81}
82
83MigrationIncomingState *migration_incoming_state_new(QEMUFile* f)
84{
85 mis_current = g_malloc0(sizeof(MigrationIncomingState));
86 mis_current->file = f;
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +010087 QLIST_INIT(&mis_current->loadvm_handlers);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010088
89 return mis_current;
90}
91
92void migration_incoming_state_destroy(void)
93{
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +010094 loadvm_free_handlers(mis_current);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +010095 g_free(mis_current);
96 mis_current = NULL;
97}
98
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000099/*
100 * Called on -incoming with a defer: uri.
101 * The migration can be started later after any parameters have been
102 * changed.
103 */
104static void deferred_incoming_migration(Error **errp)
105{
106 if (deferred_incoming) {
107 error_setg(errp, "Incoming migration already deferred");
108 }
109 deferred_incoming = true;
110}
111
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200112void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000113{
aliguori34c9dd82008-10-13 03:14:31 +0000114 const char *p;
115
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000116 if (!strcmp(uri, "defer")) {
117 deferred_incoming_migration(errp);
118 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200119 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400120#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000121 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400122 rdma_start_incoming_migration(p, errp);
123#endif
aliguori065e2812008-11-11 16:46:33 +0000124#if !defined(WIN32)
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000125 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200126 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000127 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200128 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000129 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200130 fd_start_incoming_migration(p, errp);
aliguori065e2812008-11-11 16:46:33 +0000131#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000132 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100133 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200134 }
aliguori5bb79102008-10-13 03:12:02 +0000135}
136
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200137static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200138{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200139 QEMUFile *f = opaque;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100140 Error *local_err = NULL;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200141 int ret;
142
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100143 migration_incoming_state_new(f);
144
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200145 ret = qemu_loadvm_state(f);
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100146
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200147 qemu_fclose(f);
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200148 free_xbzrle_decoded_buf();
Dr. David Alan Gilbertbca78562015-05-21 13:24:14 +0100149 migration_incoming_state_destroy();
150
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200151 if (ret < 0) {
Peter Lievendb80fac2014-06-10 11:29:16 +0200152 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800153 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600154 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200155 }
156 qemu_announce_self();
Juan Quintela511c0232010-06-09 14:10:55 +0200157
Anthony Liguori0f154232011-11-14 15:09:45 -0600158 /* Make sure all file formats flush their mutable metadata */
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100159 bdrv_invalidate_cache_all(&local_err);
160 if (local_err) {
Markus Armbruster97baf9d2015-02-18 19:21:52 +0100161 error_report_err(local_err);
Liang Li3fcb38c2015-03-23 16:32:18 +0800162 migrate_decompress_threads_join();
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100163 exit(EXIT_FAILURE);
164 }
Anthony Liguori0f154232011-11-14 15:09:45 -0600165
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300166 if (autostart) {
Juan Quintela511c0232010-06-09 14:10:55 +0200167 vm_start();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300168 } else {
Paolo Bonzini29ed72f2012-10-19 16:45:24 +0200169 runstate_set(RUN_STATE_PAUSED);
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300170 }
Liang Li3fcb38c2015-03-23 16:32:18 +0800171 migrate_decompress_threads_join();
Juan Quintela511c0232010-06-09 14:10:55 +0200172}
173
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200174void process_incoming_migration(QEMUFile *f)
175{
176 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
177 int fd = qemu_get_fd(f);
178
179 assert(fd != -1);
Liang Li3fcb38c2015-03-23 16:32:18 +0800180 migrate_decompress_threads_create();
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100181 qemu_set_nonblock(fd);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200182 qemu_coroutine_enter(co, f);
183}
184
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400185/* amount of nanoseconds we are willing to wait for migration to be down.
186 * the choice of nanoseconds is because it is the maximum resolution that
187 * get_clock() can achieve. It is an internal measure. All user-visible
188 * units must be in seconds */
Alexey Kardashevskiyf7cd55a2014-03-27 14:57:26 +1100189static uint64_t max_downtime = 300000000;
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400190
191uint64_t migrate_max_downtime(void)
192{
193 return max_downtime;
194}
195
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300196MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
197{
198 MigrationCapabilityStatusList *head = NULL;
199 MigrationCapabilityStatusList *caps;
200 MigrationState *s = migrate_get_current();
201 int i;
202
Michael Tokarev387eede2013-10-05 13:18:28 +0400203 caps = NULL; /* silence compiler warning */
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300204 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
205 if (head == NULL) {
206 head = g_malloc0(sizeof(*caps));
207 caps = head;
208 } else {
209 caps->next = g_malloc0(sizeof(*caps));
210 caps = caps->next;
211 }
212 caps->value =
213 g_malloc(sizeof(*caps->value));
214 caps->value->capability = i;
215 caps->value->state = s->enabled_capabilities[i];
216 }
217
218 return head;
219}
220
Liang Li85de8322015-03-23 16:32:28 +0800221MigrationParameters *qmp_query_migrate_parameters(Error **errp)
222{
223 MigrationParameters *params;
224 MigrationState *s = migrate_get_current();
225
226 params = g_malloc0(sizeof(*params));
227 params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
228 params->compress_threads =
229 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
230 params->decompress_threads =
231 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
232
233 return params;
234}
235
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300236static void get_xbzrle_cache_stats(MigrationInfo *info)
237{
238 if (migrate_use_xbzrle()) {
239 info->has_xbzrle_cache = true;
240 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
241 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
242 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
243 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
244 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
ChenLiang8bc39232014-04-04 17:57:56 +0800245 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300246 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
247 }
248}
249
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300250MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000251{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300252 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200253 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000254
Juan Quintela17549e82011-10-05 13:50:43 +0200255 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800256 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200257 /* no migration has happened ever */
258 break;
zhanghailiang31194732015-03-13 16:08:38 +0800259 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400260 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400261 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400262 break;
zhanghailiang31194732015-03-13 16:08:38 +0800263 case MIGRATION_STATUS_ACTIVE:
264 case MIGRATION_STATUS_CANCELLING:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300265 info->has_status = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200266 info->has_total_time = true;
Alex Blighbc72ad62013-08-21 16:03:08 +0100267 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Juan Quintela7aa939a2012-08-18 13:17:10 +0200268 - s->total_time;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200269 info->has_expected_downtime = true;
270 info->expected_downtime = s->expected_downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400271 info->has_setup_time = true;
272 info->setup_time = s->setup_time;
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200273
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300274 info->has_ram = true;
275 info->ram = g_malloc0(sizeof(*info->ram));
276 info->ram->transferred = ram_bytes_transferred();
277 info->ram->remaining = ram_bytes_remaining();
278 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300279 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100280 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300281 info->ram->normal = norm_mig_pages_transferred();
282 info->ram->normal_bytes = norm_mig_bytes_transferred();
Juan Quintela8d017192012-08-13 12:31:25 +0200283 info->ram->dirty_pages_rate = s->dirty_pages_rate;
Michael R. Hines7e114f82013-06-25 21:35:30 -0400284 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800285 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela8d017192012-08-13 12:31:25 +0200286
Juan Quintela17549e82011-10-05 13:50:43 +0200287 if (blk_mig_active()) {
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300288 info->has_disk = true;
289 info->disk = g_malloc0(sizeof(*info->disk));
290 info->disk->transferred = blk_mig_bytes_transferred();
291 info->disk->remaining = blk_mig_bytes_remaining();
292 info->disk->total = blk_mig_bytes_total();
aliguoriff8d81d2008-10-24 22:10:31 +0000293 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300294
295 get_xbzrle_cache_stats(info);
Juan Quintela17549e82011-10-05 13:50:43 +0200296 break;
zhanghailiang31194732015-03-13 16:08:38 +0800297 case MIGRATION_STATUS_COMPLETED:
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300298 get_xbzrle_cache_stats(info);
299
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300300 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900301 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200302 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200303 info->has_downtime = true;
304 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400305 info->has_setup_time = true;
306 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200307
308 info->has_ram = true;
309 info->ram = g_malloc0(sizeof(*info->ram));
310 info->ram->transferred = ram_bytes_transferred();
311 info->ram->remaining = 0;
312 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300313 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100314 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300315 info->ram->normal = norm_mig_pages_transferred();
316 info->ram->normal_bytes = norm_mig_bytes_transferred();
Michael R. Hines7e114f82013-06-25 21:35:30 -0400317 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800318 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela17549e82011-10-05 13:50:43 +0200319 break;
zhanghailiang31194732015-03-13 16:08:38 +0800320 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300321 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200322 break;
zhanghailiang31194732015-03-13 16:08:38 +0800323 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300324 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200325 break;
aliguori5bb79102008-10-13 03:12:02 +0000326 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800327 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300328
329 return info;
aliguori5bb79102008-10-13 03:12:02 +0000330}
331
Orit Wasserman00458432012-08-06 21:42:48 +0300332void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
333 Error **errp)
334{
335 MigrationState *s = migrate_get_current();
336 MigrationCapabilityStatusList *cap;
337
zhanghailiang31194732015-03-13 16:08:38 +0800338 if (s->state == MIGRATION_STATUS_ACTIVE ||
339 s->state == MIGRATION_STATUS_SETUP) {
Orit Wasserman00458432012-08-06 21:42:48 +0300340 error_set(errp, QERR_MIGRATION_ACTIVE);
341 return;
342 }
343
344 for (cap = params; cap; cap = cap->next) {
345 s->enabled_capabilities[cap->value->capability] = cap->value->state;
346 }
347}
348
Liang Li85de8322015-03-23 16:32:28 +0800349void qmp_migrate_set_parameters(bool has_compress_level,
350 int64_t compress_level,
351 bool has_compress_threads,
352 int64_t compress_threads,
353 bool has_decompress_threads,
354 int64_t decompress_threads, Error **errp)
355{
356 MigrationState *s = migrate_get_current();
357
358 if (has_compress_level && (compress_level < 0 || compress_level > 9)) {
359 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
360 "is invalid, it should be in the range of 0 to 9");
361 return;
362 }
363 if (has_compress_threads &&
364 (compress_threads < 1 || compress_threads > 255)) {
365 error_set(errp, QERR_INVALID_PARAMETER_VALUE,
366 "compress_threads",
367 "is invalid, it should be in the range of 1 to 255");
368 return;
369 }
370 if (has_decompress_threads &&
371 (decompress_threads < 1 || decompress_threads > 255)) {
372 error_set(errp, QERR_INVALID_PARAMETER_VALUE,
373 "decompress_threads",
374 "is invalid, it should be in the range of 1 to 255");
375 return;
376 }
377
378 if (has_compress_level) {
379 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
380 }
381 if (has_compress_threads) {
382 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads;
383 }
384 if (has_decompress_threads) {
385 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
386 decompress_threads;
387 }
388}
389
aliguori065e2812008-11-11 16:46:33 +0000390/* shared migration helpers */
391
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000392static void migrate_set_state(MigrationState *s, int old_state, int new_state)
393{
394 if (atomic_cmpxchg(&s->state, old_state, new_state) == new_state) {
395 trace_migrate_set_state(new_state);
396 }
397}
398
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100399static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000400{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100401 MigrationState *s = opaque;
402
403 qemu_bh_delete(s->cleanup_bh);
404 s->cleanup_bh = NULL;
405
aliguori065e2812008-11-11 16:46:33 +0000406 if (s->file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100407 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100408 qemu_mutex_unlock_iothread();
409 qemu_thread_join(&s->thread);
410 qemu_mutex_lock_iothread();
411
Liang Li8706d2d2015-03-23 16:32:17 +0800412 migrate_compress_threads_join();
Paolo Bonzini6f190a02013-02-22 17:36:48 +0100413 qemu_fclose(s->file);
414 s->file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000415 }
416
zhanghailiang31194732015-03-13 16:08:38 +0800417 assert(s->state != MIGRATION_STATUS_ACTIVE);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100418
zhanghailiang31194732015-03-13 16:08:38 +0800419 if (s->state != MIGRATION_STATUS_COMPLETED) {
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100420 qemu_savevm_state_cancel();
zhanghailiang31194732015-03-13 16:08:38 +0800421 if (s->state == MIGRATION_STATUS_CANCELLING) {
422 migrate_set_state(s, MIGRATION_STATUS_CANCELLING,
423 MIGRATION_STATUS_CANCELLED);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000424 }
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100425 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100426
427 notifier_list_notify(&migration_state_notifiers, s);
aliguori065e2812008-11-11 16:46:33 +0000428}
429
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200430void migrate_fd_error(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000431{
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100432 trace_migrate_fd_error();
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100433 assert(s->file == NULL);
zhanghailiang31194732015-03-13 16:08:38 +0800434 s->state = MIGRATION_STATUS_FAILED;
435 trace_migrate_set_state(MIGRATION_STATUS_FAILED);
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100436 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela458cf282011-02-22 23:32:54 +0100437}
438
Juan Quintela0edda1c2010-05-11 16:28:39 +0200439static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000440{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000441 int old_state ;
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000442 QEMUFile *f = migrate_get_current()->file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100443 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000444
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000445 do {
446 old_state = s->state;
zhanghailiang31194732015-03-13 16:08:38 +0800447 if (old_state != MIGRATION_STATUS_SETUP &&
448 old_state != MIGRATION_STATUS_ACTIVE) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000449 break;
450 }
zhanghailiang31194732015-03-13 16:08:38 +0800451 migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
452 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000453
454 /*
455 * If we're unlucky the migration code might be stuck somewhere in a
456 * send/write while the network has failed and is waiting to timeout;
457 * if we've got shutdown(2) available then we can force it to quit.
458 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
459 * called in a bh, so there is no race against this cancel.
460 */
zhanghailiang31194732015-03-13 16:08:38 +0800461 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000462 qemu_file_shutdown(f);
463 }
aliguori065e2812008-11-11 16:46:33 +0000464}
465
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100466void add_migration_state_change_notifier(Notifier *notify)
467{
468 notifier_list_add(&migration_state_notifiers, notify);
469}
470
471void remove_migration_state_change_notifier(Notifier *notify)
472{
Paolo Bonzini31552522012-01-13 17:34:01 +0100473 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100474}
475
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +0200476bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200477{
zhanghailiang31194732015-03-13 16:08:38 +0800478 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200479}
480
Juan Quintela70736932011-02-23 00:43:59 +0100481bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100482{
zhanghailiang31194732015-03-13 16:08:38 +0800483 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100484}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200485
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200486bool migration_has_failed(MigrationState *s)
487{
zhanghailiang31194732015-03-13 16:08:38 +0800488 return (s->state == MIGRATION_STATUS_CANCELLED ||
489 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200490}
491
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300492static MigrationState *migrate_init(const MigrationParams *params)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200493{
Juan Quintela17549e82011-10-05 13:50:43 +0200494 MigrationState *s = migrate_get_current();
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100495 int64_t bandwidth_limit = s->bandwidth_limit;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300496 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300497 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
Liang Li43c60a82015-03-23 16:32:27 +0800498 int compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
499 int compress_thread_count =
500 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
501 int decompress_thread_count =
502 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300503
504 memcpy(enabled_capabilities, s->enabled_capabilities,
505 sizeof(enabled_capabilities));
Juan Quintela0edda1c2010-05-11 16:28:39 +0200506
Juan Quintela17549e82011-10-05 13:50:43 +0200507 memset(s, 0, sizeof(*s));
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300508 s->params = *params;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300509 memcpy(s->enabled_capabilities, enabled_capabilities,
510 sizeof(enabled_capabilities));
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300511 s->xbzrle_cache_size = xbzrle_cache_size;
Juan Quintela1299c632011-11-09 21:29:01 +0100512
Liang Li43c60a82015-03-23 16:32:27 +0800513 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
514 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
515 compress_thread_count;
516 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
517 decompress_thread_count;
Juan Quintela0edda1c2010-05-11 16:28:39 +0200518 s->bandwidth_limit = bandwidth_limit;
zhanghailiang31194732015-03-13 16:08:38 +0800519 s->state = MIGRATION_STATUS_SETUP;
520 trace_migrate_set_state(MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200521
Alex Blighbc72ad62013-08-21 16:03:08 +0100522 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200523 return s;
524}
Juan Quintelacab30142011-02-22 23:54:21 +0100525
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600526static GSList *migration_blockers;
527
528void migrate_add_blocker(Error *reason)
529{
530 migration_blockers = g_slist_prepend(migration_blockers, reason);
531}
532
533void migrate_del_blocker(Error *reason)
534{
535 migration_blockers = g_slist_remove(migration_blockers, reason);
536}
537
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000538void qmp_migrate_incoming(const char *uri, Error **errp)
539{
540 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000541 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000542
543 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000544 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000545 return;
546 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000547 if (!once) {
548 error_setg(errp, "The incoming migration has already been started");
549 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000550
551 qemu_start_incoming_migration(uri, &local_err);
552
553 if (local_err) {
554 error_propagate(errp, local_err);
555 return;
556 }
557
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000558 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000559}
560
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200561void qmp_migrate(const char *uri, bool has_blk, bool blk,
562 bool has_inc, bool inc, bool has_detach, bool detach,
563 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100564{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +0200565 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +0200566 MigrationState *s = migrate_get_current();
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300567 MigrationParams params;
Juan Quintelacab30142011-02-22 23:54:21 +0100568 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +0100569
Pawit Pornkitprasan8c0426a2013-07-30 08:39:52 +0900570 params.blk = has_blk && blk;
571 params.shared = has_inc && inc;
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300572
zhanghailiang31194732015-03-13 16:08:38 +0800573 if (s->state == MIGRATION_STATUS_ACTIVE ||
574 s->state == MIGRATION_STATUS_SETUP ||
575 s->state == MIGRATION_STATUS_CANCELLING) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200576 error_set(errp, QERR_MIGRATION_ACTIVE);
577 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100578 }
579
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +0100580 if (runstate_check(RUN_STATE_INMIGRATE)) {
581 error_setg(errp, "Guest is waiting for an incoming migration");
582 return;
583 }
584
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200585 if (qemu_savevm_state_blocked(errp)) {
586 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100587 }
588
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600589 if (migration_blockers) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200590 *errp = error_copy(migration_blockers->data);
591 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600592 }
593
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300594 s = migrate_init(&params);
Juan Quintelacab30142011-02-22 23:54:21 +0100595
596 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200597 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400598#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +0800599 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400600 rdma_start_outgoing_migration(s, p, &local_err);
601#endif
Juan Quintelacab30142011-02-22 23:54:21 +0100602#if !defined(WIN32)
603 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200604 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100605 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200606 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100607 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200608 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100609#endif
610 } else {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200611 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
zhanghailiang31194732015-03-13 16:08:38 +0800612 s->state = MIGRATION_STATUS_FAILED;
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200613 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100614 }
615
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200616 if (local_err) {
Paolo Bonzini342ab8d2012-10-02 09:59:38 +0200617 migrate_fd_error(s);
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200618 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200619 return;
Juan Quintela1299c632011-11-09 21:29:01 +0100620 }
Juan Quintelacab30142011-02-22 23:54:21 +0100621}
622
Luiz Capitulino6cdedb02011-11-27 22:54:09 -0200623void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100624{
Juan Quintela17549e82011-10-05 13:50:43 +0200625 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +0100626}
627
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300628void qmp_migrate_set_cache_size(int64_t value, Error **errp)
629{
630 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +0200631 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300632
633 /* Check for truncation */
634 if (value != (size_t)value) {
635 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
636 "exceeding address space");
637 return;
638 }
639
Orit Wassermana5615b12014-01-30 20:08:36 +0200640 /* Cache should not be larger than guest ram size */
641 if (value > ram_bytes_total()) {
642 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
643 "exceeds guest ram size ");
644 return;
645 }
646
Orit Wassermanc91e6812014-01-30 20:08:34 +0200647 new_size = xbzrle_cache_resize(value);
648 if (new_size < 0) {
649 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
650 "is smaller than page size");
651 return;
652 }
653
654 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300655}
656
657int64_t qmp_query_migrate_cache_size(Error **errp)
658{
659 return migrate_xbzrle_cache_size();
660}
661
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200662void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100663{
Juan Quintelacab30142011-02-22 23:54:21 +0100664 MigrationState *s;
665
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200666 if (value < 0) {
667 value = 0;
Juan Quintelacab30142011-02-22 23:54:21 +0100668 }
Paolo Bonzini442773c2013-02-22 17:36:44 +0100669 if (value > SIZE_MAX) {
670 value = SIZE_MAX;
671 }
Juan Quintelacab30142011-02-22 23:54:21 +0100672
Juan Quintela17549e82011-10-05 13:50:43 +0200673 s = migrate_get_current();
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200674 s->bandwidth_limit = value;
Paolo Bonzini442773c2013-02-22 17:36:44 +0100675 if (s->file) {
676 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
677 }
Juan Quintelacab30142011-02-22 23:54:21 +0100678}
679
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200680void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100681{
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200682 value *= 1e9;
683 value = MAX(0, MIN(UINT64_MAX, value));
684 max_downtime = (uint64_t)value;
aliguori5bb79102008-10-13 03:12:02 +0000685}
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300686
Chegu Vinodbde1e2e2013-06-24 03:49:42 -0600687bool migrate_auto_converge(void)
688{
689 MigrationState *s;
690
691 s = migrate_get_current();
692
693 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
694}
695
Peter Lieven323004a2013-07-18 09:48:50 +0200696bool migrate_zero_blocks(void)
697{
698 MigrationState *s;
699
700 s = migrate_get_current();
701
702 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
703}
704
Liang Li8706d2d2015-03-23 16:32:17 +0800705bool migrate_use_compression(void)
706{
Liang Lidde4e692015-03-23 16:32:26 +0800707 MigrationState *s;
708
709 s = migrate_get_current();
710
711 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +0800712}
713
714int migrate_compress_level(void)
715{
716 MigrationState *s;
717
718 s = migrate_get_current();
719
Liang Li43c60a82015-03-23 16:32:27 +0800720 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
Liang Li8706d2d2015-03-23 16:32:17 +0800721}
722
723int migrate_compress_threads(void)
724{
725 MigrationState *s;
726
727 s = migrate_get_current();
728
Liang Li43c60a82015-03-23 16:32:27 +0800729 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
Liang Li8706d2d2015-03-23 16:32:17 +0800730}
731
Liang Li3fcb38c2015-03-23 16:32:18 +0800732int migrate_decompress_threads(void)
733{
734 MigrationState *s;
735
736 s = migrate_get_current();
737
Liang Li43c60a82015-03-23 16:32:27 +0800738 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Liang Li3fcb38c2015-03-23 16:32:18 +0800739}
740
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300741int migrate_use_xbzrle(void)
742{
743 MigrationState *s;
744
745 s = migrate_get_current();
746
747 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
748}
749
750int64_t migrate_xbzrle_cache_size(void)
751{
752 MigrationState *s;
753
754 s = migrate_get_current();
755
756 return s->xbzrle_cache_size;
757}
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200758
759/* migration thread support */
760
Juan Quintela5f496a12013-02-22 17:36:30 +0100761static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200762{
Juan Quintela9848a402012-12-19 09:55:50 +0100763 MigrationState *s = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100764 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
765 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +0100766 int64_t initial_bytes = 0;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200767 int64_t max_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100768 int64_t start_time = initial_time;
769 bool old_vm_running = false;
Juan Quintela76f59332012-10-03 20:16:24 +0200770
Dr. David Alan Gilbertf796baa2015-05-21 13:24:12 +0100771 qemu_savevm_state_header(s->file);
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100772 qemu_savevm_state_begin(s->file, &s->params);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200773
Alex Blighbc72ad62013-08-21 16:03:08 +0100774 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang31194732015-03-13 16:08:38 +0800775 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400776
zhanghailiang31194732015-03-13 16:08:38 +0800777 while (s->state == MIGRATION_STATUS_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +0100778 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +0200779 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200780
Paolo Bonzinia0ff0442013-02-22 17:36:35 +0100781 if (!qemu_file_rate_limit(s->file)) {
Juan Quintelac369f402012-10-03 20:33:34 +0200782 pending_size = qemu_savevm_state_pending(s->file, max_size);
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100783 trace_migrate_pending(pending_size, max_size);
Juan Quintelab22ff1f2012-10-17 21:06:31 +0200784 if (pending_size && pending_size >= max_size) {
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100785 qemu_savevm_state_iterate(s->file);
Juan Quintelac369f402012-10-03 20:33:34 +0200786 } else {
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200787 int ret;
788
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100789 qemu_mutex_lock_iothread();
Alex Blighbc72ad62013-08-21 16:03:08 +0100790 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintelac369f402012-10-03 20:33:34 +0200791 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100792 old_vm_running = runstate_is_running();
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200793
794 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
795 if (ret >= 0) {
Matthew Garrett40596832013-11-25 14:42:43 -0500796 qemu_file_set_rate_limit(s->file, INT64_MAX);
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200797 qemu_savevm_state_complete(s->file);
798 }
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100799 qemu_mutex_unlock_iothread();
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200800
801 if (ret < 0) {
zhanghailiang31194732015-03-13 16:08:38 +0800802 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
803 MIGRATION_STATUS_FAILED);
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200804 break;
805 }
806
Paolo Bonzini059f8962013-02-22 17:36:32 +0100807 if (!qemu_file_get_error(s->file)) {
zhanghailiang31194732015-03-13 16:08:38 +0800808 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
809 MIGRATION_STATUS_COMPLETED);
Paolo Bonzini059f8962013-02-22 17:36:32 +0100810 break;
811 }
Juan Quintelac369f402012-10-03 20:33:34 +0200812 }
813 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +0100814
Paolo Bonzinifd45ee22013-02-22 17:36:33 +0100815 if (qemu_file_get_error(s->file)) {
zhanghailiang31194732015-03-13 16:08:38 +0800816 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
817 MIGRATION_STATUS_FAILED);
Paolo Bonzinifd45ee22013-02-22 17:36:33 +0100818 break;
819 }
Alex Blighbc72ad62013-08-21 16:03:08 +0100820 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200821 if (current_time >= initial_time + BUFFER_DELAY) {
Paolo Bonzinibe7172e2013-02-22 17:36:43 +0100822 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -0500823 uint64_t time_spent = current_time - initial_time;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200824 double bandwidth = transferred_bytes / time_spent;
825 max_size = bandwidth * migrate_max_downtime() / 1000000;
826
Michael R. Hines7e114f82013-06-25 21:35:30 -0400827 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
828 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
829
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100830 trace_migrate_transferred(transferred_bytes, time_spent,
831 bandwidth, max_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +0100832 /* if we haven't sent anything, we don't want to recalculate
833 10000 is a small enough number for our purposes */
834 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
835 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
836 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200837
Paolo Bonzini1964a392013-02-22 17:36:45 +0100838 qemu_file_reset_rate_limit(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200839 initial_time = current_time;
Paolo Bonzinibe7172e2013-02-22 17:36:43 +0100840 initial_bytes = qemu_ftell(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200841 }
Paolo Bonzinia0ff0442013-02-22 17:36:35 +0100842 if (qemu_file_rate_limit(s->file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200843 /* usleep expects microseconds */
844 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
845 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100846 }
847
Paolo Bonzinif4410a52013-02-22 17:36:20 +0100848 qemu_mutex_lock_iothread();
zhanghailiang31194732015-03-13 16:08:38 +0800849 if (s->state == MIGRATION_STATUS_COMPLETED) {
Alex Blighbc72ad62013-08-21 16:03:08 +0100850 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Peter Lievend6ed7312014-05-12 10:46:00 +0200851 uint64_t transferred_bytes = qemu_ftell(s->file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100852 s->total_time = end_time - s->total_time;
853 s->downtime = end_time - start_time;
Peter Lievend6ed7312014-05-12 10:46:00 +0200854 if (s->total_time) {
855 s->mbps = (((double) transferred_bytes * 8.0) /
856 ((double) s->total_time)) / 1000;
857 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100858 runstate_set(RUN_STATE_POSTMIGRATE);
859 } else {
860 if (old_vm_running) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100861 vm_start();
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100862 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200863 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100864 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100865 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +0100866
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200867 return NULL;
868}
869
Juan Quintela9848a402012-12-19 09:55:50 +0100870void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200871{
Juan Quintelacc283e32013-02-01 11:12:26 +0100872 /* This is a best 1st approximation. ns to ms */
873 s->expected_downtime = max_downtime/1000000;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100874 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200875
Paolo Bonzini442773c2013-02-22 17:36:44 +0100876 qemu_file_set_rate_limit(s->file,
877 s->bandwidth_limit / XFER_LIMIT_RATIO);
878
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +0200879 /* Notify before starting migration thread */
880 notifier_list_notify(&migration_state_notifiers, s);
881
Liang Li8706d2d2015-03-23 16:32:17 +0800882 migrate_compress_threads_create();
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +0000883 qemu_thread_create(&s->thread, "migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100884 QEMU_THREAD_JOINABLE);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200885}