blob: 533717c681e5b08a365112b4632d8483edeeaa40 [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
Juan Quintela859bc752012-08-13 09:42:49 +020056MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +020057{
58 static MigrationState current_migration = {
zhanghailiang31194732015-03-13 16:08:38 +080059 .state = MIGRATION_STATUS_NONE,
Juan Quintelad0ae46c2011-02-23 00:33:19 +010060 .bandwidth_limit = MAX_THROTTLE,
Orit Wasserman17ad9b32012-08-06 21:42:53 +030061 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
Michael R. Hines7e114f82013-06-25 21:35:30 -040062 .mbps = -1,
Liang Li43c60a82015-03-23 16:32:27 +080063 .parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] =
64 DEFAULT_MIGRATE_COMPRESS_LEVEL,
65 .parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
66 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
67 .parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
68 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
Juan Quintela17549e82011-10-05 13:50:43 +020069 };
70
71 return &current_migration;
72}
73
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000074/*
75 * Called on -incoming with a defer: uri.
76 * The migration can be started later after any parameters have been
77 * changed.
78 */
79static void deferred_incoming_migration(Error **errp)
80{
81 if (deferred_incoming) {
82 error_setg(errp, "Incoming migration already deferred");
83 }
84 deferred_incoming = true;
85}
86
Paolo Bonzini43eaae22012-10-02 18:21:18 +020087void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +000088{
aliguori34c9dd82008-10-13 03:14:31 +000089 const char *p;
90
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000091 if (!strcmp(uri, "defer")) {
92 deferred_incoming_migration(errp);
93 } else if (strstart(uri, "tcp:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +020094 tcp_start_incoming_migration(p, errp);
Michael R. Hines2da776d2013-07-22 10:01:54 -040095#ifdef CONFIG_RDMA
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +000096 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -040097 rdma_start_incoming_migration(p, errp);
98#endif
aliguori065e2812008-11-11 16:46:33 +000099#if !defined(WIN32)
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000100 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200101 exec_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000102 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200103 unix_start_incoming_migration(p, errp);
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000104 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzini43eaae22012-10-02 18:21:18 +0200105 fd_start_incoming_migration(p, errp);
aliguori065e2812008-11-11 16:46:33 +0000106#endif
Dr. David Alan Gilbertadde2202015-02-19 11:40:27 +0000107 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100108 error_setg(errp, "unknown migration protocol: %s", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +0200109 }
aliguori5bb79102008-10-13 03:12:02 +0000110}
111
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200112static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +0200113{
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200114 QEMUFile *f = opaque;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100115 Error *local_err = NULL;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200116 int ret;
117
118 ret = qemu_loadvm_state(f);
119 qemu_fclose(f);
Gonglei (Arei)905f26f2014-01-30 20:08:35 +0200120 free_xbzrle_decoded_buf();
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +0200121 if (ret < 0) {
Peter Lievendb80fac2014-06-10 11:29:16 +0200122 error_report("load of migration failed: %s", strerror(-ret));
Liang Li3fcb38c2015-03-23 16:32:18 +0800123 migrate_decompress_threads_join();
Eric Blake4aead692013-04-16 15:50:41 -0600124 exit(EXIT_FAILURE);
Juan Quintela511c0232010-06-09 14:10:55 +0200125 }
126 qemu_announce_self();
Juan Quintela511c0232010-06-09 14:10:55 +0200127
Anthony Liguori0f154232011-11-14 15:09:45 -0600128 /* Make sure all file formats flush their mutable metadata */
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100129 bdrv_invalidate_cache_all(&local_err);
130 if (local_err) {
Markus Armbruster97baf9d2015-02-18 19:21:52 +0100131 error_report_err(local_err);
Liang Li3fcb38c2015-03-23 16:32:18 +0800132 migrate_decompress_threads_join();
Kevin Wolf5a8a30d2014-03-12 15:59:16 +0100133 exit(EXIT_FAILURE);
134 }
Anthony Liguori0f154232011-11-14 15:09:45 -0600135
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300136 if (autostart) {
Juan Quintela511c0232010-06-09 14:10:55 +0200137 vm_start();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300138 } else {
Paolo Bonzini29ed72f2012-10-19 16:45:24 +0200139 runstate_set(RUN_STATE_PAUSED);
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300140 }
Liang Li3fcb38c2015-03-23 16:32:18 +0800141 migrate_decompress_threads_join();
Juan Quintela511c0232010-06-09 14:10:55 +0200142}
143
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200144void process_incoming_migration(QEMUFile *f)
145{
146 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
147 int fd = qemu_get_fd(f);
148
149 assert(fd != -1);
Liang Li3fcb38c2015-03-23 16:32:18 +0800150 migrate_decompress_threads_create();
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100151 qemu_set_nonblock(fd);
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200152 qemu_coroutine_enter(co, f);
153}
154
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400155/* amount of nanoseconds we are willing to wait for migration to be down.
156 * the choice of nanoseconds is because it is the maximum resolution that
157 * get_clock() can achieve. It is an internal measure. All user-visible
158 * units must be in seconds */
Alexey Kardashevskiyf7cd55a2014-03-27 14:57:26 +1100159static uint64_t max_downtime = 300000000;
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400160
161uint64_t migrate_max_downtime(void)
162{
163 return max_downtime;
164}
165
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300166MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
167{
168 MigrationCapabilityStatusList *head = NULL;
169 MigrationCapabilityStatusList *caps;
170 MigrationState *s = migrate_get_current();
171 int i;
172
Michael Tokarev387eede2013-10-05 13:18:28 +0400173 caps = NULL; /* silence compiler warning */
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300174 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
175 if (head == NULL) {
176 head = g_malloc0(sizeof(*caps));
177 caps = head;
178 } else {
179 caps->next = g_malloc0(sizeof(*caps));
180 caps = caps->next;
181 }
182 caps->value =
183 g_malloc(sizeof(*caps->value));
184 caps->value->capability = i;
185 caps->value->state = s->enabled_capabilities[i];
186 }
187
188 return head;
189}
190
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300191static void get_xbzrle_cache_stats(MigrationInfo *info)
192{
193 if (migrate_use_xbzrle()) {
194 info->has_xbzrle_cache = true;
195 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
196 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
197 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
198 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
199 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
ChenLiang8bc39232014-04-04 17:57:56 +0800200 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300201 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
202 }
203}
204
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300205MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000206{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300207 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200208 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000209
Juan Quintela17549e82011-10-05 13:50:43 +0200210 switch (s->state) {
zhanghailiang31194732015-03-13 16:08:38 +0800211 case MIGRATION_STATUS_NONE:
Juan Quintela17549e82011-10-05 13:50:43 +0200212 /* no migration has happened ever */
213 break;
zhanghailiang31194732015-03-13 16:08:38 +0800214 case MIGRATION_STATUS_SETUP:
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400215 info->has_status = true;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400216 info->has_total_time = false;
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400217 break;
zhanghailiang31194732015-03-13 16:08:38 +0800218 case MIGRATION_STATUS_ACTIVE:
219 case MIGRATION_STATUS_CANCELLING:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300220 info->has_status = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200221 info->has_total_time = true;
Alex Blighbc72ad62013-08-21 16:03:08 +0100222 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
Juan Quintela7aa939a2012-08-18 13:17:10 +0200223 - s->total_time;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200224 info->has_expected_downtime = true;
225 info->expected_downtime = s->expected_downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400226 info->has_setup_time = true;
227 info->setup_time = s->setup_time;
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200228
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300229 info->has_ram = true;
230 info->ram = g_malloc0(sizeof(*info->ram));
231 info->ram->transferred = ram_bytes_transferred();
232 info->ram->remaining = ram_bytes_remaining();
233 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300234 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100235 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300236 info->ram->normal = norm_mig_pages_transferred();
237 info->ram->normal_bytes = norm_mig_bytes_transferred();
Juan Quintela8d017192012-08-13 12:31:25 +0200238 info->ram->dirty_pages_rate = s->dirty_pages_rate;
Michael R. Hines7e114f82013-06-25 21:35:30 -0400239 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800240 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela8d017192012-08-13 12:31:25 +0200241
Juan Quintela17549e82011-10-05 13:50:43 +0200242 if (blk_mig_active()) {
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300243 info->has_disk = true;
244 info->disk = g_malloc0(sizeof(*info->disk));
245 info->disk->transferred = blk_mig_bytes_transferred();
246 info->disk->remaining = blk_mig_bytes_remaining();
247 info->disk->total = blk_mig_bytes_total();
aliguoriff8d81d2008-10-24 22:10:31 +0000248 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300249
250 get_xbzrle_cache_stats(info);
Juan Quintela17549e82011-10-05 13:50:43 +0200251 break;
zhanghailiang31194732015-03-13 16:08:38 +0800252 case MIGRATION_STATUS_COMPLETED:
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300253 get_xbzrle_cache_stats(info);
254
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300255 info->has_status = true;
Pawit Pornkitprasan00c14992013-07-19 11:23:45 +0900256 info->has_total_time = true;
Juan Quintela7aa939a2012-08-18 13:17:10 +0200257 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200258 info->has_downtime = true;
259 info->downtime = s->downtime;
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400260 info->has_setup_time = true;
261 info->setup_time = s->setup_time;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200262
263 info->has_ram = true;
264 info->ram = g_malloc0(sizeof(*info->ram));
265 info->ram->transferred = ram_bytes_transferred();
266 info->ram->remaining = 0;
267 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300268 info->ram->duplicate = dup_mig_pages_transferred();
Peter Lievenf1c72792013-03-26 10:58:37 +0100269 info->ram->skipped = skipped_mig_pages_transferred();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300270 info->ram->normal = norm_mig_pages_transferred();
271 info->ram->normal_bytes = norm_mig_bytes_transferred();
Michael R. Hines7e114f82013-06-25 21:35:30 -0400272 info->ram->mbps = s->mbps;
ChenLiang58570ed2014-04-04 17:57:55 +0800273 info->ram->dirty_sync_count = s->dirty_sync_count;
Juan Quintela17549e82011-10-05 13:50:43 +0200274 break;
zhanghailiang31194732015-03-13 16:08:38 +0800275 case MIGRATION_STATUS_FAILED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300276 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200277 break;
zhanghailiang31194732015-03-13 16:08:38 +0800278 case MIGRATION_STATUS_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300279 info->has_status = true;
Juan Quintela17549e82011-10-05 13:50:43 +0200280 break;
aliguori5bb79102008-10-13 03:12:02 +0000281 }
zhanghailiangcde63fb2015-03-13 16:08:41 +0800282 info->status = s->state;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300283
284 return info;
aliguori5bb79102008-10-13 03:12:02 +0000285}
286
Orit Wasserman00458432012-08-06 21:42:48 +0300287void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
288 Error **errp)
289{
290 MigrationState *s = migrate_get_current();
291 MigrationCapabilityStatusList *cap;
292
zhanghailiang31194732015-03-13 16:08:38 +0800293 if (s->state == MIGRATION_STATUS_ACTIVE ||
294 s->state == MIGRATION_STATUS_SETUP) {
Orit Wasserman00458432012-08-06 21:42:48 +0300295 error_set(errp, QERR_MIGRATION_ACTIVE);
296 return;
297 }
298
299 for (cap = params; cap; cap = cap->next) {
300 s->enabled_capabilities[cap->value->capability] = cap->value->state;
301 }
302}
303
aliguori065e2812008-11-11 16:46:33 +0000304/* shared migration helpers */
305
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000306static void migrate_set_state(MigrationState *s, int old_state, int new_state)
307{
308 if (atomic_cmpxchg(&s->state, old_state, new_state) == new_state) {
309 trace_migrate_set_state(new_state);
310 }
311}
312
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100313static void migrate_fd_cleanup(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000314{
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100315 MigrationState *s = opaque;
316
317 qemu_bh_delete(s->cleanup_bh);
318 s->cleanup_bh = NULL;
319
aliguori065e2812008-11-11 16:46:33 +0000320 if (s->file) {
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100321 trace_migrate_fd_cleanup();
Paolo Bonzini404a7c02013-02-22 17:36:46 +0100322 qemu_mutex_unlock_iothread();
323 qemu_thread_join(&s->thread);
324 qemu_mutex_lock_iothread();
325
Liang Li8706d2d2015-03-23 16:32:17 +0800326 migrate_compress_threads_join();
Paolo Bonzini6f190a02013-02-22 17:36:48 +0100327 qemu_fclose(s->file);
328 s->file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000329 }
330
zhanghailiang31194732015-03-13 16:08:38 +0800331 assert(s->state != MIGRATION_STATUS_ACTIVE);
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100332
zhanghailiang31194732015-03-13 16:08:38 +0800333 if (s->state != MIGRATION_STATUS_COMPLETED) {
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100334 qemu_savevm_state_cancel();
zhanghailiang31194732015-03-13 16:08:38 +0800335 if (s->state == MIGRATION_STATUS_CANCELLING) {
336 migrate_set_state(s, MIGRATION_STATUS_CANCELLING,
337 MIGRATION_STATUS_CANCELLED);
Zhanghaoyu (A)51cf4c12013-11-07 11:01:15 +0000338 }
Paolo Bonzini7a2c1722013-02-22 17:36:09 +0100339 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100340
341 notifier_list_notify(&migration_state_notifiers, s);
aliguori065e2812008-11-11 16:46:33 +0000342}
343
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200344void migrate_fd_error(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000345{
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100346 trace_migrate_fd_error();
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100347 assert(s->file == NULL);
zhanghailiang31194732015-03-13 16:08:38 +0800348 s->state = MIGRATION_STATUS_FAILED;
349 trace_migrate_set_state(MIGRATION_STATUS_FAILED);
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100350 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela458cf282011-02-22 23:32:54 +0100351}
352
Juan Quintela0edda1c2010-05-11 16:28:39 +0200353static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000354{
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000355 int old_state ;
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000356 QEMUFile *f = migrate_get_current()->file;
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100357 trace_migrate_fd_cancel();
aliguori065e2812008-11-11 16:46:33 +0000358
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000359 do {
360 old_state = s->state;
zhanghailiang31194732015-03-13 16:08:38 +0800361 if (old_state != MIGRATION_STATUS_SETUP &&
362 old_state != MIGRATION_STATUS_ACTIVE) {
Zhanghaoyu (A)6f2b8112013-11-07 08:21:23 +0000363 break;
364 }
zhanghailiang31194732015-03-13 16:08:38 +0800365 migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
366 } while (s->state != MIGRATION_STATUS_CANCELLING);
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000367
368 /*
369 * If we're unlucky the migration code might be stuck somewhere in a
370 * send/write while the network has failed and is waiting to timeout;
371 * if we've got shutdown(2) available then we can force it to quit.
372 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
373 * called in a bh, so there is no race against this cancel.
374 */
zhanghailiang31194732015-03-13 16:08:38 +0800375 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
Dr. David Alan Gilberta26ba262015-01-08 11:11:32 +0000376 qemu_file_shutdown(f);
377 }
aliguori065e2812008-11-11 16:46:33 +0000378}
379
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100380void add_migration_state_change_notifier(Notifier *notify)
381{
382 notifier_list_add(&migration_state_notifiers, notify);
383}
384
385void remove_migration_state_change_notifier(Notifier *notify)
386{
Paolo Bonzini31552522012-01-13 17:34:01 +0100387 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100388}
389
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +0200390bool migration_in_setup(MigrationState *s)
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200391{
zhanghailiang31194732015-03-13 16:08:38 +0800392 return s->state == MIGRATION_STATUS_SETUP;
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200393}
394
Juan Quintela70736932011-02-23 00:43:59 +0100395bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100396{
zhanghailiang31194732015-03-13 16:08:38 +0800397 return s->state == MIGRATION_STATUS_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100398}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200399
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200400bool migration_has_failed(MigrationState *s)
401{
zhanghailiang31194732015-03-13 16:08:38 +0800402 return (s->state == MIGRATION_STATUS_CANCELLED ||
403 s->state == MIGRATION_STATUS_FAILED);
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200404}
405
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300406static MigrationState *migrate_init(const MigrationParams *params)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200407{
Juan Quintela17549e82011-10-05 13:50:43 +0200408 MigrationState *s = migrate_get_current();
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100409 int64_t bandwidth_limit = s->bandwidth_limit;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300410 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300411 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
Liang Li43c60a82015-03-23 16:32:27 +0800412 int compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
413 int compress_thread_count =
414 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
415 int decompress_thread_count =
416 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300417
418 memcpy(enabled_capabilities, s->enabled_capabilities,
419 sizeof(enabled_capabilities));
Juan Quintela0edda1c2010-05-11 16:28:39 +0200420
Juan Quintela17549e82011-10-05 13:50:43 +0200421 memset(s, 0, sizeof(*s));
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300422 s->params = *params;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300423 memcpy(s->enabled_capabilities, enabled_capabilities,
424 sizeof(enabled_capabilities));
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300425 s->xbzrle_cache_size = xbzrle_cache_size;
Juan Quintela1299c632011-11-09 21:29:01 +0100426
Liang Li43c60a82015-03-23 16:32:27 +0800427 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
428 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
429 compress_thread_count;
430 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
431 decompress_thread_count;
Juan Quintela0edda1c2010-05-11 16:28:39 +0200432 s->bandwidth_limit = bandwidth_limit;
zhanghailiang31194732015-03-13 16:08:38 +0800433 s->state = MIGRATION_STATUS_SETUP;
434 trace_migrate_set_state(MIGRATION_STATUS_SETUP);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200435
Alex Blighbc72ad62013-08-21 16:03:08 +0100436 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200437 return s;
438}
Juan Quintelacab30142011-02-22 23:54:21 +0100439
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600440static GSList *migration_blockers;
441
442void migrate_add_blocker(Error *reason)
443{
444 migration_blockers = g_slist_prepend(migration_blockers, reason);
445}
446
447void migrate_del_blocker(Error *reason)
448{
449 migration_blockers = g_slist_remove(migration_blockers, reason);
450}
451
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000452void qmp_migrate_incoming(const char *uri, Error **errp)
453{
454 Error *local_err = NULL;
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000455 static bool once = true;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000456
457 if (!deferred_incoming) {
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000458 error_setg(errp, "For use with '-incoming defer'");
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000459 return;
460 }
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000461 if (!once) {
462 error_setg(errp, "The incoming migration has already been started");
463 }
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000464
465 qemu_start_incoming_migration(uri, &local_err);
466
467 if (local_err) {
468 error_propagate(errp, local_err);
469 return;
470 }
471
Dr. David Alan Gilbert4debb5f2015-02-26 14:54:41 +0000472 once = false;
Dr. David Alan Gilbertbf1ae1f2015-02-19 11:40:28 +0000473}
474
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200475void qmp_migrate(const char *uri, bool has_blk, bool blk,
476 bool has_inc, bool inc, bool has_detach, bool detach,
477 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100478{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +0200479 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +0200480 MigrationState *s = migrate_get_current();
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300481 MigrationParams params;
Juan Quintelacab30142011-02-22 23:54:21 +0100482 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +0100483
Pawit Pornkitprasan8c0426a2013-07-30 08:39:52 +0900484 params.blk = has_blk && blk;
485 params.shared = has_inc && inc;
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300486
zhanghailiang31194732015-03-13 16:08:38 +0800487 if (s->state == MIGRATION_STATUS_ACTIVE ||
488 s->state == MIGRATION_STATUS_SETUP ||
489 s->state == MIGRATION_STATUS_CANCELLING) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200490 error_set(errp, QERR_MIGRATION_ACTIVE);
491 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100492 }
493
Dr. David Alan Gilbertca999932014-04-14 17:03:59 +0100494 if (runstate_check(RUN_STATE_INMIGRATE)) {
495 error_setg(errp, "Guest is waiting for an incoming migration");
496 return;
497 }
498
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200499 if (qemu_savevm_state_blocked(errp)) {
500 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100501 }
502
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600503 if (migration_blockers) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200504 *errp = error_copy(migration_blockers->data);
505 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600506 }
507
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300508 s = migrate_init(&params);
Juan Quintelacab30142011-02-22 23:54:21 +0100509
510 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200511 tcp_start_outgoing_migration(s, p, &local_err);
Michael R. Hines2da776d2013-07-22 10:01:54 -0400512#ifdef CONFIG_RDMA
Michael R. Hines41310c62013-12-19 04:52:01 +0800513 } else if (strstart(uri, "rdma:", &p)) {
Michael R. Hines2da776d2013-07-22 10:01:54 -0400514 rdma_start_outgoing_migration(s, p, &local_err);
515#endif
Juan Quintelacab30142011-02-22 23:54:21 +0100516#if !defined(WIN32)
517 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200518 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100519 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200520 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100521 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200522 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100523#endif
524 } else {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200525 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
zhanghailiang31194732015-03-13 16:08:38 +0800526 s->state = MIGRATION_STATUS_FAILED;
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200527 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100528 }
529
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200530 if (local_err) {
Paolo Bonzini342ab8d2012-10-02 09:59:38 +0200531 migrate_fd_error(s);
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200532 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200533 return;
Juan Quintela1299c632011-11-09 21:29:01 +0100534 }
Juan Quintelacab30142011-02-22 23:54:21 +0100535}
536
Luiz Capitulino6cdedb02011-11-27 22:54:09 -0200537void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100538{
Juan Quintela17549e82011-10-05 13:50:43 +0200539 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +0100540}
541
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300542void qmp_migrate_set_cache_size(int64_t value, Error **errp)
543{
544 MigrationState *s = migrate_get_current();
Orit Wassermanc91e6812014-01-30 20:08:34 +0200545 int64_t new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300546
547 /* Check for truncation */
548 if (value != (size_t)value) {
549 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
550 "exceeding address space");
551 return;
552 }
553
Orit Wassermana5615b12014-01-30 20:08:36 +0200554 /* Cache should not be larger than guest ram size */
555 if (value > ram_bytes_total()) {
556 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
557 "exceeds guest ram size ");
558 return;
559 }
560
Orit Wassermanc91e6812014-01-30 20:08:34 +0200561 new_size = xbzrle_cache_resize(value);
562 if (new_size < 0) {
563 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
564 "is smaller than page size");
565 return;
566 }
567
568 s->xbzrle_cache_size = new_size;
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300569}
570
571int64_t qmp_query_migrate_cache_size(Error **errp)
572{
573 return migrate_xbzrle_cache_size();
574}
575
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200576void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100577{
Juan Quintelacab30142011-02-22 23:54:21 +0100578 MigrationState *s;
579
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200580 if (value < 0) {
581 value = 0;
Juan Quintelacab30142011-02-22 23:54:21 +0100582 }
Paolo Bonzini442773c2013-02-22 17:36:44 +0100583 if (value > SIZE_MAX) {
584 value = SIZE_MAX;
585 }
Juan Quintelacab30142011-02-22 23:54:21 +0100586
Juan Quintela17549e82011-10-05 13:50:43 +0200587 s = migrate_get_current();
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200588 s->bandwidth_limit = value;
Paolo Bonzini442773c2013-02-22 17:36:44 +0100589 if (s->file) {
590 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
591 }
Juan Quintelacab30142011-02-22 23:54:21 +0100592}
593
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200594void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100595{
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200596 value *= 1e9;
597 value = MAX(0, MIN(UINT64_MAX, value));
598 max_downtime = (uint64_t)value;
aliguori5bb79102008-10-13 03:12:02 +0000599}
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300600
Chegu Vinodbde1e2e2013-06-24 03:49:42 -0600601bool migrate_auto_converge(void)
602{
603 MigrationState *s;
604
605 s = migrate_get_current();
606
607 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
608}
609
Peter Lieven323004a2013-07-18 09:48:50 +0200610bool migrate_zero_blocks(void)
611{
612 MigrationState *s;
613
614 s = migrate_get_current();
615
616 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
617}
618
Liang Li8706d2d2015-03-23 16:32:17 +0800619bool migrate_use_compression(void)
620{
Liang Lidde4e692015-03-23 16:32:26 +0800621 MigrationState *s;
622
623 s = migrate_get_current();
624
625 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
Liang Li8706d2d2015-03-23 16:32:17 +0800626}
627
628int migrate_compress_level(void)
629{
630 MigrationState *s;
631
632 s = migrate_get_current();
633
Liang Li43c60a82015-03-23 16:32:27 +0800634 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
Liang Li8706d2d2015-03-23 16:32:17 +0800635}
636
637int migrate_compress_threads(void)
638{
639 MigrationState *s;
640
641 s = migrate_get_current();
642
Liang Li43c60a82015-03-23 16:32:27 +0800643 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
Liang Li8706d2d2015-03-23 16:32:17 +0800644}
645
Liang Li3fcb38c2015-03-23 16:32:18 +0800646int migrate_decompress_threads(void)
647{
648 MigrationState *s;
649
650 s = migrate_get_current();
651
Liang Li43c60a82015-03-23 16:32:27 +0800652 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
Liang Li3fcb38c2015-03-23 16:32:18 +0800653}
654
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300655int migrate_use_xbzrle(void)
656{
657 MigrationState *s;
658
659 s = migrate_get_current();
660
661 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
662}
663
664int64_t migrate_xbzrle_cache_size(void)
665{
666 MigrationState *s;
667
668 s = migrate_get_current();
669
670 return s->xbzrle_cache_size;
671}
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200672
673/* migration thread support */
674
Juan Quintela5f496a12013-02-22 17:36:30 +0100675static void *migration_thread(void *opaque)
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200676{
Juan Quintela9848a402012-12-19 09:55:50 +0100677 MigrationState *s = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100678 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
679 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
Paolo Bonzinibe7172e2013-02-22 17:36:43 +0100680 int64_t initial_bytes = 0;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200681 int64_t max_size = 0;
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100682 int64_t start_time = initial_time;
683 bool old_vm_running = false;
Juan Quintela76f59332012-10-03 20:16:24 +0200684
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100685 qemu_savevm_state_begin(s->file, &s->params);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200686
Alex Blighbc72ad62013-08-21 16:03:08 +0100687 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
zhanghailiang31194732015-03-13 16:08:38 +0800688 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
Michael R. Hines29ae8a42013-07-22 10:01:57 -0400689
zhanghailiang31194732015-03-13 16:08:38 +0800690 while (s->state == MIGRATION_STATUS_ACTIVE) {
Juan Quintelaa3e879c2013-02-01 12:39:08 +0100691 int64_t current_time;
Juan Quintelac369f402012-10-03 20:33:34 +0200692 uint64_t pending_size;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200693
Paolo Bonzinia0ff0442013-02-22 17:36:35 +0100694 if (!qemu_file_rate_limit(s->file)) {
Juan Quintelac369f402012-10-03 20:33:34 +0200695 pending_size = qemu_savevm_state_pending(s->file, max_size);
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100696 trace_migrate_pending(pending_size, max_size);
Juan Quintelab22ff1f2012-10-17 21:06:31 +0200697 if (pending_size && pending_size >= max_size) {
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100698 qemu_savevm_state_iterate(s->file);
Juan Quintelac369f402012-10-03 20:33:34 +0200699 } else {
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200700 int ret;
701
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100702 qemu_mutex_lock_iothread();
Alex Blighbc72ad62013-08-21 16:03:08 +0100703 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintelac369f402012-10-03 20:33:34 +0200704 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100705 old_vm_running = runstate_is_running();
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200706
707 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
708 if (ret >= 0) {
Matthew Garrett40596832013-11-25 14:42:43 -0500709 qemu_file_set_rate_limit(s->file, INT64_MAX);
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200710 qemu_savevm_state_complete(s->file);
711 }
Paolo Bonzini32c835b2013-02-22 17:36:27 +0100712 qemu_mutex_unlock_iothread();
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200713
714 if (ret < 0) {
zhanghailiang31194732015-03-13 16:08:38 +0800715 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
716 MIGRATION_STATUS_FAILED);
Kevin Wolf0e1146a2013-07-05 13:54:55 +0200717 break;
718 }
719
Paolo Bonzini059f8962013-02-22 17:36:32 +0100720 if (!qemu_file_get_error(s->file)) {
zhanghailiang31194732015-03-13 16:08:38 +0800721 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
722 MIGRATION_STATUS_COMPLETED);
Paolo Bonzini059f8962013-02-22 17:36:32 +0100723 break;
724 }
Juan Quintelac369f402012-10-03 20:33:34 +0200725 }
726 }
Paolo Bonzinif4410a52013-02-22 17:36:20 +0100727
Paolo Bonzinifd45ee22013-02-22 17:36:33 +0100728 if (qemu_file_get_error(s->file)) {
zhanghailiang31194732015-03-13 16:08:38 +0800729 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
730 MIGRATION_STATUS_FAILED);
Paolo Bonzinifd45ee22013-02-22 17:36:33 +0100731 break;
732 }
Alex Blighbc72ad62013-08-21 16:03:08 +0100733 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200734 if (current_time >= initial_time + BUFFER_DELAY) {
Paolo Bonzinibe7172e2013-02-22 17:36:43 +0100735 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
Michael Roth77417f12013-05-16 16:25:44 -0500736 uint64_t time_spent = current_time - initial_time;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200737 double bandwidth = transferred_bytes / time_spent;
738 max_size = bandwidth * migrate_max_downtime() / 1000000;
739
Michael R. Hines7e114f82013-06-25 21:35:30 -0400740 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
741 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
742
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100743 trace_migrate_transferred(transferred_bytes, time_spent,
744 bandwidth, max_size);
Juan Quintela90f8ae72013-02-01 13:22:37 +0100745 /* if we haven't sent anything, we don't want to recalculate
746 10000 is a small enough number for our purposes */
747 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
748 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
749 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200750
Paolo Bonzini1964a392013-02-22 17:36:45 +0100751 qemu_file_reset_rate_limit(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200752 initial_time = current_time;
Paolo Bonzinibe7172e2013-02-22 17:36:43 +0100753 initial_bytes = qemu_ftell(s->file);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200754 }
Paolo Bonzinia0ff0442013-02-22 17:36:35 +0100755 if (qemu_file_rate_limit(s->file)) {
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200756 /* usleep expects microseconds */
757 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
758 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100759 }
760
Paolo Bonzinif4410a52013-02-22 17:36:20 +0100761 qemu_mutex_lock_iothread();
zhanghailiang31194732015-03-13 16:08:38 +0800762 if (s->state == MIGRATION_STATUS_COMPLETED) {
Alex Blighbc72ad62013-08-21 16:03:08 +0100763 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Peter Lievend6ed7312014-05-12 10:46:00 +0200764 uint64_t transferred_bytes = qemu_ftell(s->file);
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100765 s->total_time = end_time - s->total_time;
766 s->downtime = end_time - start_time;
Peter Lievend6ed7312014-05-12 10:46:00 +0200767 if (s->total_time) {
768 s->mbps = (((double) transferred_bytes * 8.0) /
769 ((double) s->total_time)) / 1000;
770 }
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100771 runstate_set(RUN_STATE_POSTMIGRATE);
772 } else {
773 if (old_vm_running) {
Paolo Bonzinia3fa1d72013-02-22 17:36:18 +0100774 vm_start();
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100775 }
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200776 }
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100777 qemu_bh_schedule(s->cleanup_bh);
Paolo Bonzinidba433c2013-02-22 17:36:17 +0100778 qemu_mutex_unlock_iothread();
Paolo Bonzinif4410a52013-02-22 17:36:20 +0100779
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200780 return NULL;
781}
782
Juan Quintela9848a402012-12-19 09:55:50 +0100783void migrate_fd_connect(MigrationState *s)
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200784{
zhanghailiang31194732015-03-13 16:08:38 +0800785 s->state = MIGRATION_STATUS_SETUP;
786 trace_migrate_set_state(MIGRATION_STATUS_SETUP);
Kazuya Saitoc09e5bb2013-02-22 17:36:19 +0100787
Juan Quintelacc283e32013-02-01 11:12:26 +0100788 /* This is a best 1st approximation. ns to ms */
789 s->expected_downtime = max_downtime/1000000;
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100790 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200791
Paolo Bonzini442773c2013-02-22 17:36:44 +0100792 qemu_file_set_rate_limit(s->file,
793 s->bandwidth_limit / XFER_LIMIT_RATIO);
794
Stefan Hajnoczi9287ac22013-07-29 15:01:57 +0200795 /* Notify before starting migration thread */
796 notifier_list_notify(&migration_state_notifiers, s);
797
Liang Li8706d2d2015-03-23 16:32:17 +0800798 migrate_compress_threads_create();
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +0000799 qemu_thread_create(&s->thread, "migration", migration_thread, s,
Paolo Bonzinibb1fadc2013-02-22 17:36:21 +0100800 QEMU_THREAD_JOINABLE);
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200801}