blob: 15f22eac401258813362b51e8251423a126d9cfd [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"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010017#include "migration/migration.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010018#include "monitor/monitor.h"
Juan Quintela0d82d0e2012-10-03 14:18:33 +020019#include "migration/qemu-file.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010020#include "sysemu/sysemu.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010021#include "block/block.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010022#include "qemu/sockets.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010023#include "migration/block.h"
Juan Quintela766bd172012-07-23 05:45:29 +020024#include "qemu/thread.h"
Luiz Capitulino791e7c82011-09-13 17:37:16 -030025#include "qmp-commands.h"
aliguori065e2812008-11-11 16:46:33 +000026
27//#define DEBUG_MIGRATION
28
29#ifdef DEBUG_MIGRATION
malcd0f2c4c2010-02-07 02:03:50 +030030#define DPRINTF(fmt, ...) \
aliguori065e2812008-11-11 16:46:33 +000031 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
32#else
malcd0f2c4c2010-02-07 02:03:50 +030033#define DPRINTF(fmt, ...) \
aliguori065e2812008-11-11 16:46:33 +000034 do { } while (0)
35#endif
aliguori5bb79102008-10-13 03:12:02 +000036
Juan Quintela7dc688e2011-02-23 00:48:46 +010037enum {
38 MIG_STATE_ERROR,
39 MIG_STATE_SETUP,
40 MIG_STATE_CANCELLED,
41 MIG_STATE_ACTIVE,
42 MIG_STATE_COMPLETED,
43};
aliguori5bb79102008-10-13 03:12:02 +000044
Juan Quintelad0ae46c2011-02-23 00:33:19 +010045#define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000046
Juan Quintela5b4e1eb2012-12-19 10:40:48 +010047/* Amount of time to allocate to each "chunk" of bandwidth-throttled
48 * data. */
49#define BUFFER_DELAY 100
50#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
51
Orit Wasserman17ad9b32012-08-06 21:42:53 +030052/* Migration XBZRLE default cache size */
53#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
54
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010055static NotifierList migration_state_notifiers =
56 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
57
Juan Quintela17549e82011-10-05 13:50:43 +020058/* When we add fault tolerance, we could have several
59 migrations at once. For now we don't need to add
60 dynamic creation of migration */
61
Juan Quintela859bc752012-08-13 09:42:49 +020062MigrationState *migrate_get_current(void)
Juan Quintela17549e82011-10-05 13:50:43 +020063{
64 static MigrationState current_migration = {
65 .state = MIG_STATE_SETUP,
Juan Quintelad0ae46c2011-02-23 00:33:19 +010066 .bandwidth_limit = MAX_THROTTLE,
Orit Wasserman17ad9b32012-08-06 21:42:53 +030067 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
Juan Quintela17549e82011-10-05 13:50:43 +020068 };
69
70 return &current_migration;
71}
72
Paolo Bonzini43eaae22012-10-02 18:21:18 +020073void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +000074{
aliguori34c9dd82008-10-13 03:14:31 +000075 const char *p;
76
77 if (strstart(uri, "tcp:", &p))
Paolo Bonzini43eaae22012-10-02 18:21:18 +020078 tcp_start_incoming_migration(p, errp);
aliguori065e2812008-11-11 16:46:33 +000079#if !defined(WIN32)
80 else if (strstart(uri, "exec:", &p))
Paolo Bonzini43eaae22012-10-02 18:21:18 +020081 exec_start_incoming_migration(p, errp);
Chris Lalancette4951f652009-08-05 17:24:29 +020082 else if (strstart(uri, "unix:", &p))
Paolo Bonzini43eaae22012-10-02 18:21:18 +020083 unix_start_incoming_migration(p, errp);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +020084 else if (strstart(uri, "fd:", &p))
Paolo Bonzini43eaae22012-10-02 18:21:18 +020085 fd_start_incoming_migration(p, errp);
aliguori065e2812008-11-11 16:46:33 +000086#endif
Juan Quintela8ca5e802010-06-09 14:10:54 +020087 else {
Paolo Bonzini43eaae22012-10-02 18:21:18 +020088 error_setg(errp, "unknown migration protocol: %s\n", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +020089 }
aliguori5bb79102008-10-13 03:12:02 +000090}
91
Paolo Bonzini82a4da72012-08-07 10:57:43 +020092static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +020093{
Paolo Bonzini82a4da72012-08-07 10:57:43 +020094 QEMUFile *f = opaque;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +020095 int ret;
96
97 ret = qemu_loadvm_state(f);
Paolo Bonzini82a4da72012-08-07 10:57:43 +020098 qemu_set_fd_handler(qemu_get_fd(f), NULL, NULL, NULL);
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +020099 qemu_fclose(f);
100 if (ret < 0) {
Juan Quintela511c0232010-06-09 14:10:55 +0200101 fprintf(stderr, "load of migration failed\n");
102 exit(0);
103 }
104 qemu_announce_self();
105 DPRINTF("successfully loaded vm state\n");
106
Benoît Canet901862c2012-03-23 08:36:52 +0100107 bdrv_clear_incoming_migration_all();
Anthony Liguori0f154232011-11-14 15:09:45 -0600108 /* Make sure all file formats flush their mutable metadata */
109 bdrv_invalidate_cache_all();
110
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300111 if (autostart) {
Juan Quintela511c0232010-06-09 14:10:55 +0200112 vm_start();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300113 } else {
Paolo Bonzini29ed72f2012-10-19 16:45:24 +0200114 runstate_set(RUN_STATE_PAUSED);
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300115 }
Juan Quintela511c0232010-06-09 14:10:55 +0200116}
117
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200118static void enter_migration_coroutine(void *opaque)
119{
120 Coroutine *co = opaque;
121 qemu_coroutine_enter(co, NULL);
122}
123
124void process_incoming_migration(QEMUFile *f)
125{
126 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
127 int fd = qemu_get_fd(f);
128
129 assert(fd != -1);
130 socket_set_nonblock(fd);
131 qemu_set_fd_handler(fd, enter_migration_coroutine, NULL, co);
132 qemu_coroutine_enter(co, f);
133}
134
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400135/* amount of nanoseconds we are willing to wait for migration to be down.
136 * the choice of nanoseconds is because it is the maximum resolution that
137 * get_clock() can achieve. It is an internal measure. All user-visible
138 * units must be in seconds */
139static uint64_t max_downtime = 30000000;
140
141uint64_t migrate_max_downtime(void)
142{
143 return max_downtime;
144}
145
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300146MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
147{
148 MigrationCapabilityStatusList *head = NULL;
149 MigrationCapabilityStatusList *caps;
150 MigrationState *s = migrate_get_current();
151 int i;
152
153 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
154 if (head == NULL) {
155 head = g_malloc0(sizeof(*caps));
156 caps = head;
157 } else {
158 caps->next = g_malloc0(sizeof(*caps));
159 caps = caps->next;
160 }
161 caps->value =
162 g_malloc(sizeof(*caps->value));
163 caps->value->capability = i;
164 caps->value->state = s->enabled_capabilities[i];
165 }
166
167 return head;
168}
169
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300170static void get_xbzrle_cache_stats(MigrationInfo *info)
171{
172 if (migrate_use_xbzrle()) {
173 info->has_xbzrle_cache = true;
174 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
175 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
176 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
177 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
178 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
179 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
180 }
181}
182
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300183MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000184{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300185 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200186 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000187
Juan Quintela17549e82011-10-05 13:50:43 +0200188 switch (s->state) {
189 case MIG_STATE_SETUP:
190 /* no migration has happened ever */
191 break;
192 case MIG_STATE_ACTIVE:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300193 info->has_status = true;
194 info->status = g_strdup("active");
Juan Quintela7aa939a2012-08-18 13:17:10 +0200195 info->has_total_time = true;
196 info->total_time = qemu_get_clock_ms(rt_clock)
197 - s->total_time;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200198 info->has_expected_downtime = true;
199 info->expected_downtime = s->expected_downtime;
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200200
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300201 info->has_ram = true;
202 info->ram = g_malloc0(sizeof(*info->ram));
203 info->ram->transferred = ram_bytes_transferred();
204 info->ram->remaining = ram_bytes_remaining();
205 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300206 info->ram->duplicate = dup_mig_pages_transferred();
207 info->ram->normal = norm_mig_pages_transferred();
208 info->ram->normal_bytes = norm_mig_bytes_transferred();
Juan Quintela8d017192012-08-13 12:31:25 +0200209 info->ram->dirty_pages_rate = s->dirty_pages_rate;
210
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200211
Juan Quintela17549e82011-10-05 13:50:43 +0200212 if (blk_mig_active()) {
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300213 info->has_disk = true;
214 info->disk = g_malloc0(sizeof(*info->disk));
215 info->disk->transferred = blk_mig_bytes_transferred();
216 info->disk->remaining = blk_mig_bytes_remaining();
217 info->disk->total = blk_mig_bytes_total();
aliguoriff8d81d2008-10-24 22:10:31 +0000218 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300219
220 get_xbzrle_cache_stats(info);
Juan Quintela17549e82011-10-05 13:50:43 +0200221 break;
222 case MIG_STATE_COMPLETED:
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300223 get_xbzrle_cache_stats(info);
224
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300225 info->has_status = true;
226 info->status = g_strdup("completed");
Juan Quintela7aa939a2012-08-18 13:17:10 +0200227 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200228 info->has_downtime = true;
229 info->downtime = s->downtime;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200230
231 info->has_ram = true;
232 info->ram = g_malloc0(sizeof(*info->ram));
233 info->ram->transferred = ram_bytes_transferred();
234 info->ram->remaining = 0;
235 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300236 info->ram->duplicate = dup_mig_pages_transferred();
237 info->ram->normal = norm_mig_pages_transferred();
238 info->ram->normal_bytes = norm_mig_bytes_transferred();
Juan Quintela17549e82011-10-05 13:50:43 +0200239 break;
240 case MIG_STATE_ERROR:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300241 info->has_status = true;
242 info->status = g_strdup("failed");
Juan Quintela17549e82011-10-05 13:50:43 +0200243 break;
244 case MIG_STATE_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300245 info->has_status = true;
246 info->status = g_strdup("cancelled");
Juan Quintela17549e82011-10-05 13:50:43 +0200247 break;
aliguori5bb79102008-10-13 03:12:02 +0000248 }
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300249
250 return info;
aliguori5bb79102008-10-13 03:12:02 +0000251}
252
Orit Wasserman00458432012-08-06 21:42:48 +0300253void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
254 Error **errp)
255{
256 MigrationState *s = migrate_get_current();
257 MigrationCapabilityStatusList *cap;
258
259 if (s->state == MIG_STATE_ACTIVE) {
260 error_set(errp, QERR_MIGRATION_ACTIVE);
261 return;
262 }
263
264 for (cap = params; cap; cap = cap->next) {
265 s->enabled_capabilities[cap->value->capability] = cap->value->state;
266 }
267}
268
aliguori065e2812008-11-11 16:46:33 +0000269/* shared migration helpers */
270
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200271static int migrate_fd_cleanup(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000272{
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500273 int ret = 0;
274
aliguori065e2812008-11-11 16:46:33 +0000275 if (s->file) {
malcd0f2c4c2010-02-07 02:03:50 +0300276 DPRINTF("closing file\n");
Eduardo Habkosta6d34a92011-11-10 10:41:42 -0200277 ret = qemu_fclose(s->file);
Jan Kiszka5d39c792009-11-30 18:21:19 +0100278 s->file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000279 }
280
Paolo Bonzini24ea1e42012-11-10 18:58:40 +0100281 assert(s->fd == -1);
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500282 return ret;
aliguori065e2812008-11-11 16:46:33 +0000283}
284
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200285void migrate_fd_error(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000286{
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200287 DPRINTF("setting error state\n");
288 s->state = MIG_STATE_ERROR;
Juan Quintelae0eb7392011-10-05 14:27:52 +0200289 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200290 migrate_fd_cleanup(s);
291}
292
Juan Quintela458cf282011-02-22 23:32:54 +0100293static void migrate_fd_completed(MigrationState *s)
294{
295 DPRINTF("setting completed state\n");
296 if (migrate_fd_cleanup(s) < 0) {
297 s->state = MIG_STATE_ERROR;
298 } else {
299 s->state = MIG_STATE_COMPLETED;
300 runstate_set(RUN_STATE_POSTMIGRATE);
301 }
Juan Quintelae0eb7392011-10-05 14:27:52 +0200302 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela458cf282011-02-22 23:32:54 +0100303}
304
Juan Quintelac87b0152012-07-20 13:10:54 +0200305ssize_t migrate_fd_put_buffer(MigrationState *s, const void *data,
306 size_t size)
aliguori065e2812008-11-11 16:46:33 +0000307{
aliguori065e2812008-11-11 16:46:33 +0000308 ssize_t ret;
309
Juan Quintelafdbecb52011-09-21 22:37:29 +0200310 if (s->state != MIG_STATE_ACTIVE) {
311 return -EIO;
312 }
313
aliguori065e2812008-11-11 16:46:33 +0000314 do {
315 ret = s->write(s, data, size);
Uri Lublin95b134e2009-05-19 14:08:53 +0300316 } while (ret == -1 && ((s->get_error(s)) == EINTR));
aliguori065e2812008-11-11 16:46:33 +0000317
318 if (ret == -1)
319 ret = -(s->get_error(s));
320
aliguori065e2812008-11-11 16:46:33 +0000321 return ret;
322}
323
Juan Quintelae4ed1542012-09-21 11:18:18 +0200324bool migrate_fd_put_ready(MigrationState *s, uint64_t max_size)
aliguori065e2812008-11-11 16:46:33 +0000325{
326 int ret;
Juan Quintelae4ed1542012-09-21 11:18:18 +0200327 uint64_t pending_size;
328 bool last_round = false;
aliguori065e2812008-11-11 16:46:33 +0000329
Juan Quintelae7627482012-07-23 06:31:30 +0200330 qemu_mutex_lock_iothread();
aliguori065e2812008-11-11 16:46:33 +0000331 if (s->state != MIG_STATE_ACTIVE) {
malcd0f2c4c2010-02-07 02:03:50 +0300332 DPRINTF("put_ready returning because of non-active state\n");
Juan Quintelae7627482012-07-23 06:31:30 +0200333 qemu_mutex_unlock_iothread();
Juan Quintelae4ed1542012-09-21 11:18:18 +0200334 return false;
aliguori065e2812008-11-11 16:46:33 +0000335 }
Juan Quintela766bd172012-07-23 05:45:29 +0200336 if (s->first_time) {
337 s->first_time = false;
338 DPRINTF("beginning savevm\n");
339 ret = qemu_savevm_state_begin(s->file, &s->params);
340 if (ret < 0) {
341 DPRINTF("failed, %d\n", ret);
342 migrate_fd_error(s);
Juan Quintelae7627482012-07-23 06:31:30 +0200343 qemu_mutex_unlock_iothread();
Juan Quintelae4ed1542012-09-21 11:18:18 +0200344 return false;
Juan Quintela766bd172012-07-23 05:45:29 +0200345 }
346 }
aliguori065e2812008-11-11 16:46:33 +0000347
malcd0f2c4c2010-02-07 02:03:50 +0300348 DPRINTF("iterate\n");
Juan Quintelae4ed1542012-09-21 11:18:18 +0200349 pending_size = qemu_savevm_state_pending(s->file, max_size);
350 DPRINTF("pending size %lu max %lu\n", pending_size, max_size);
351 if (pending_size >= max_size) {
352 ret = qemu_savevm_state_iterate(s->file);
353 if (ret < 0) {
354 migrate_fd_error(s);
355 }
356 } else {
Luiz Capitulino13548692011-07-29 15:36:43 -0300357 int old_vm_running = runstate_is_running();
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200358 int64_t start_time, end_time;
Anthony Liguorieeb34af2009-07-09 13:25:47 -0500359
malcd0f2c4c2010-02-07 02:03:50 +0300360 DPRINTF("done iterating\n");
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200361 start_time = qemu_get_clock_ms(rt_clock);
Gerd Hoffmann7b5d3aa2012-03-07 08:00:26 +0100362 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
Juan Quintela766bd172012-07-23 05:45:29 +0200363 if (old_vm_running) {
364 vm_stop(RUN_STATE_FINISH_MIGRATE);
365 } else {
366 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
367 }
aliguori065e2812008-11-11 16:46:33 +0000368
Luiz Capitulino539de122011-12-05 14:06:56 -0200369 if (qemu_savevm_state_complete(s->file) < 0) {
Juan Quintela67afff72011-02-22 23:18:20 +0100370 migrate_fd_error(s);
aliguorib161d122009-04-05 19:30:33 +0000371 } else {
Juan Quintela458cf282011-02-22 23:32:54 +0100372 migrate_fd_completed(s);
aliguorib161d122009-04-05 19:30:33 +0000373 }
Juan Quintela97d4d962012-08-10 21:53:08 +0200374 end_time = qemu_get_clock_ms(rt_clock);
375 s->total_time = end_time - s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200376 s->downtime = end_time - start_time;
Juan Quintela48a2f4d2010-05-11 23:28:53 +0200377 if (s->state != MIG_STATE_COMPLETED) {
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500378 if (old_vm_running) {
379 vm_start();
380 }
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500381 }
Juan Quintelae4ed1542012-09-21 11:18:18 +0200382 last_round = true;
aliguori065e2812008-11-11 16:46:33 +0000383 }
Juan Quintelae7627482012-07-23 06:31:30 +0200384 qemu_mutex_unlock_iothread();
385
Juan Quintelae4ed1542012-09-21 11:18:18 +0200386 return last_round;
aliguori065e2812008-11-11 16:46:33 +0000387}
388
Juan Quintela0edda1c2010-05-11 16:28:39 +0200389static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000390{
aliguori065e2812008-11-11 16:46:33 +0000391 if (s->state != MIG_STATE_ACTIVE)
392 return;
393
malcd0f2c4c2010-02-07 02:03:50 +0300394 DPRINTF("cancelling migration\n");
aliguori065e2812008-11-11 16:46:33 +0000395
396 s->state = MIG_STATE_CANCELLED;
Juan Quintelae0eb7392011-10-05 14:27:52 +0200397 notifier_list_notify(&migration_state_notifiers, s);
Luiz Capitulino539de122011-12-05 14:06:56 -0200398 qemu_savevm_state_cancel(s->file);
aliguori065e2812008-11-11 16:46:33 +0000399
400 migrate_fd_cleanup(s);
401}
402
Juan Quintela11c76742012-07-20 13:19:36 +0200403int migrate_fd_close(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000404{
Paolo Bonzini8dc592e2012-09-27 13:25:45 +0200405 int rc = 0;
406 if (s->fd != -1) {
Paolo Bonzini8dc592e2012-09-27 13:25:45 +0200407 rc = s->close(s);
408 s->fd = -1;
409 }
410 return rc;
aliguori065e2812008-11-11 16:46:33 +0000411}
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100412
413void add_migration_state_change_notifier(Notifier *notify)
414{
415 notifier_list_add(&migration_state_notifiers, notify);
416}
417
418void remove_migration_state_change_notifier(Notifier *notify)
419{
Paolo Bonzini31552522012-01-13 17:34:01 +0100420 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100421}
422
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200423bool migration_is_active(MigrationState *s)
424{
425 return s->state == MIG_STATE_ACTIVE;
426}
427
Juan Quintela70736932011-02-23 00:43:59 +0100428bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100429{
Juan Quintela70736932011-02-23 00:43:59 +0100430 return s->state == MIG_STATE_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100431}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200432
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200433bool migration_has_failed(MigrationState *s)
434{
435 return (s->state == MIG_STATE_CANCELLED ||
436 s->state == MIG_STATE_ERROR);
437}
438
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200439void migrate_fd_connect(MigrationState *s)
440{
Juan Quintelad5934dd2010-05-11 23:01:53 +0200441 s->state = MIG_STATE_ACTIVE;
Juan Quintela766bd172012-07-23 05:45:29 +0200442 s->first_time = true;
Juan Quintelaedfa1af2012-07-23 02:13:23 +0200443 qemu_fopen_ops_buffered(s);
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200444}
445
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300446static MigrationState *migrate_init(const MigrationParams *params)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200447{
Juan Quintela17549e82011-10-05 13:50:43 +0200448 MigrationState *s = migrate_get_current();
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100449 int64_t bandwidth_limit = s->bandwidth_limit;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300450 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300451 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300452
453 memcpy(enabled_capabilities, s->enabled_capabilities,
454 sizeof(enabled_capabilities));
Juan Quintela0edda1c2010-05-11 16:28:39 +0200455
Juan Quintela17549e82011-10-05 13:50:43 +0200456 memset(s, 0, sizeof(*s));
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100457 s->bandwidth_limit = bandwidth_limit;
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300458 s->params = *params;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300459 memcpy(s->enabled_capabilities, enabled_capabilities,
460 sizeof(enabled_capabilities));
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300461 s->xbzrle_cache_size = xbzrle_cache_size;
Juan Quintela1299c632011-11-09 21:29:01 +0100462
Juan Quintela0edda1c2010-05-11 16:28:39 +0200463 s->bandwidth_limit = bandwidth_limit;
Juan Quintelad5934dd2010-05-11 23:01:53 +0200464 s->state = MIG_STATE_SETUP;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200465 s->total_time = qemu_get_clock_ms(rt_clock);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200466
Juan Quintela0edda1c2010-05-11 16:28:39 +0200467 return s;
468}
Juan Quintelacab30142011-02-22 23:54:21 +0100469
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600470static GSList *migration_blockers;
471
472void migrate_add_blocker(Error *reason)
473{
474 migration_blockers = g_slist_prepend(migration_blockers, reason);
475}
476
477void migrate_del_blocker(Error *reason)
478{
479 migration_blockers = g_slist_remove(migration_blockers, reason);
480}
481
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200482void qmp_migrate(const char *uri, bool has_blk, bool blk,
483 bool has_inc, bool inc, bool has_detach, bool detach,
484 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100485{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +0200486 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +0200487 MigrationState *s = migrate_get_current();
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300488 MigrationParams params;
Juan Quintelacab30142011-02-22 23:54:21 +0100489 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +0100490
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300491 params.blk = blk;
492 params.shared = inc;
493
Juan Quintela17549e82011-10-05 13:50:43 +0200494 if (s->state == MIG_STATE_ACTIVE) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200495 error_set(errp, QERR_MIGRATION_ACTIVE);
496 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100497 }
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);
Juan Quintelacab30142011-02-22 23:54:21 +0100512#if !defined(WIN32)
513 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200514 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100515 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200516 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100517 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200518 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100519#endif
520 } else {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200521 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
522 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100523 }
524
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200525 if (local_err) {
Paolo Bonzini342ab8d2012-10-02 09:59:38 +0200526 migrate_fd_error(s);
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200527 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200528 return;
Juan Quintela1299c632011-11-09 21:29:01 +0100529 }
530
Juan Quintelae0eb7392011-10-05 14:27:52 +0200531 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintelacab30142011-02-22 23:54:21 +0100532}
533
Luiz Capitulino6cdedb02011-11-27 22:54:09 -0200534void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100535{
Juan Quintela17549e82011-10-05 13:50:43 +0200536 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +0100537}
538
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300539void qmp_migrate_set_cache_size(int64_t value, Error **errp)
540{
541 MigrationState *s = migrate_get_current();
542
543 /* Check for truncation */
544 if (value != (size_t)value) {
545 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
546 "exceeding address space");
547 return;
548 }
549
550 s->xbzrle_cache_size = xbzrle_cache_resize(value);
551}
552
553int64_t qmp_query_migrate_cache_size(Error **errp)
554{
555 return migrate_xbzrle_cache_size();
556}
557
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200558void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100559{
Juan Quintelacab30142011-02-22 23:54:21 +0100560 MigrationState *s;
561
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200562 if (value < 0) {
563 value = 0;
Juan Quintelacab30142011-02-22 23:54:21 +0100564 }
Juan Quintelacab30142011-02-22 23:54:21 +0100565
Juan Quintela17549e82011-10-05 13:50:43 +0200566 s = migrate_get_current();
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200567 s->bandwidth_limit = value;
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100568 qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
Juan Quintelacab30142011-02-22 23:54:21 +0100569}
570
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200571void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100572{
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200573 value *= 1e9;
574 value = MAX(0, MIN(UINT64_MAX, value));
575 max_downtime = (uint64_t)value;
aliguori5bb79102008-10-13 03:12:02 +0000576}
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300577
578int migrate_use_xbzrle(void)
579{
580 MigrationState *s;
581
582 s = migrate_get_current();
583
584 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
585}
586
587int64_t migrate_xbzrle_cache_size(void)
588{
589 MigrationState *s;
590
591 s = migrate_get_current();
592
593 return s->xbzrle_cache_size;
594}
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200595
596/* migration thread support */
597
598typedef struct QEMUFileBuffered {
599 MigrationState *migration_state;
600 QEMUFile *file;
601 size_t bytes_xfer;
602 size_t xfer_limit;
603 uint8_t *buffer;
604 size_t buffer_size;
605 size_t buffer_capacity;
606 QemuThread thread;
607} QEMUFileBuffered;
608
609static ssize_t buffered_flush(QEMUFileBuffered *s)
610{
611 size_t offset = 0;
612 ssize_t ret = 0;
613
614 DPRINTF("flushing %zu byte(s) of data\n", s->buffer_size);
615
616 while (s->bytes_xfer < s->xfer_limit && offset < s->buffer_size) {
617 size_t to_send = MIN(s->buffer_size - offset, s->xfer_limit - s->bytes_xfer);
618 ret = migrate_fd_put_buffer(s->migration_state, s->buffer + offset,
619 to_send);
620 if (ret <= 0) {
621 DPRINTF("error flushing data, %zd\n", ret);
622 break;
623 } else {
624 DPRINTF("flushed %zd byte(s)\n", ret);
625 offset += ret;
626 s->bytes_xfer += ret;
627 }
628 }
629
630 DPRINTF("flushed %zu of %zu byte(s)\n", offset, s->buffer_size);
631 memmove(s->buffer, s->buffer + offset, s->buffer_size - offset);
632 s->buffer_size -= offset;
633
634 if (ret < 0) {
635 return ret;
636 }
637 return offset;
638}
639
640static int buffered_put_buffer(void *opaque, const uint8_t *buf,
641 int64_t pos, int size)
642{
643 QEMUFileBuffered *s = opaque;
644 ssize_t error;
645
646 DPRINTF("putting %d bytes at %" PRId64 "\n", size, pos);
647
648 error = qemu_file_get_error(s->file);
649 if (error) {
650 DPRINTF("flush when error, bailing: %s\n", strerror(-error));
651 return error;
652 }
653
654 if (size <= 0) {
655 return size;
656 }
657
658 if (size > (s->buffer_capacity - s->buffer_size)) {
659 DPRINTF("increasing buffer capacity from %zu by %zu\n",
660 s->buffer_capacity, size + 1024);
661
662 s->buffer_capacity += size + 1024;
663
664 s->buffer = g_realloc(s->buffer, s->buffer_capacity);
665 }
666
667 memcpy(s->buffer + s->buffer_size, buf, size);
668 s->buffer_size += size;
669
670 return size;
671}
672
673static int buffered_close(void *opaque)
674{
675 QEMUFileBuffered *s = opaque;
676 ssize_t ret = 0;
677 int ret2;
678
679 DPRINTF("closing\n");
680
681 s->xfer_limit = INT_MAX;
682 while (!qemu_file_get_error(s->file) && s->buffer_size) {
683 ret = buffered_flush(s);
684 if (ret < 0) {
685 break;
686 }
687 }
688
689 ret2 = migrate_fd_close(s->migration_state);
690 if (ret >= 0) {
691 ret = ret2;
692 }
693 ret = migrate_fd_close(s->migration_state);
694 s->migration_state->complete = true;
695 return ret;
696}
697
698static int buffered_get_fd(void *opaque)
699{
700 QEMUFileBuffered *s = opaque;
701
702 return qemu_get_fd(s->file);
703}
704
705/*
706 * The meaning of the return values is:
707 * 0: We can continue sending
708 * 1: Time to stop
709 * negative: There has been an error
710 */
711static int buffered_rate_limit(void *opaque)
712{
713 QEMUFileBuffered *s = opaque;
714 int ret;
715
716 ret = qemu_file_get_error(s->file);
717 if (ret) {
718 return ret;
719 }
720
721 if (s->bytes_xfer > s->xfer_limit) {
722 return 1;
723 }
724
725 return 0;
726}
727
728static int64_t buffered_set_rate_limit(void *opaque, int64_t new_rate)
729{
730 QEMUFileBuffered *s = opaque;
731 if (qemu_file_get_error(s->file)) {
732 goto out;
733 }
734 if (new_rate > SIZE_MAX) {
735 new_rate = SIZE_MAX;
736 }
737
738 s->xfer_limit = new_rate / 10;
739
740out:
741 return s->xfer_limit;
742}
743
744static int64_t buffered_get_rate_limit(void *opaque)
745{
746 QEMUFileBuffered *s = opaque;
747
748 return s->xfer_limit;
749}
750
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200751static void *buffered_file_thread(void *opaque)
752{
753 QEMUFileBuffered *s = opaque;
754 int64_t initial_time = qemu_get_clock_ms(rt_clock);
755 int64_t max_size = 0;
756 bool last_round = false;
757
758 while (true) {
759 int64_t current_time = qemu_get_clock_ms(rt_clock);
760
761 if (s->migration_state->complete) {
762 break;
763 }
764 if (current_time >= initial_time + BUFFER_DELAY) {
765 uint64_t transferred_bytes = s->bytes_xfer;
766 uint64_t time_spent = current_time - initial_time;
767 double bandwidth = transferred_bytes / time_spent;
768 max_size = bandwidth * migrate_max_downtime() / 1000000;
769
770 DPRINTF("transferred %" PRIu64 " time_spent %" PRIu64
771 " bandwidth %g max_size %" PRId64 "\n",
772 transferred_bytes, time_spent, bandwidth, max_size);
773
774 s->bytes_xfer = 0;
775 initial_time = current_time;
776 }
777 if (!last_round && (s->bytes_xfer >= s->xfer_limit)) {
778 /* usleep expects microseconds */
779 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
780 }
781 if (buffered_flush(s) < 0) {
782 break;
783 }
784
785 DPRINTF("file is ready\n");
786 if (s->bytes_xfer < s->xfer_limit) {
787 DPRINTF("notifying client\n");
788 last_round = migrate_fd_put_ready(s->migration_state, max_size);
789 }
790 }
791
792 g_free(s->buffer);
793 g_free(s);
794 return NULL;
795}
796
797static const QEMUFileOps buffered_file_ops = {
798 .get_fd = buffered_get_fd,
799 .put_buffer = buffered_put_buffer,
800 .close = buffered_close,
801 .rate_limit = buffered_rate_limit,
802 .get_rate_limit = buffered_get_rate_limit,
803 .set_rate_limit = buffered_set_rate_limit,
804};
805
806void qemu_fopen_ops_buffered(MigrationState *migration_state)
807{
808 QEMUFileBuffered *s;
809
810 s = g_malloc0(sizeof(*s));
811
812 s->migration_state = migration_state;
Juan Quintela5b4e1eb2012-12-19 10:40:48 +0100813 s->xfer_limit = s->migration_state->bandwidth_limit / XFER_LIMIT_RATIO;
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200814 s->migration_state->complete = false;
815
816 s->file = qemu_fopen_ops(s, &buffered_file_ops);
817
818 migration_state->file = s->file;
819
820 qemu_thread_create(&s->thread, buffered_file_thread, s,
821 QEMU_THREAD_DETACHED);
822}