blob: 2937c87f9a30f3addb06df8561aa01fca76c122e [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
Orit Wasserman17ad9b32012-08-06 21:42:53 +030047/* Migration XBZRLE default cache size */
48#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
49
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010050static NotifierList migration_state_notifiers =
51 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
52
Juan Quintela17549e82011-10-05 13:50:43 +020053/* When we add fault tolerance, we could have several
54 migrations at once. For now we don't need to add
55 dynamic creation of migration */
56
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 = {
60 .state = MIG_STATE_SETUP,
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,
Juan Quintela17549e82011-10-05 13:50:43 +020063 };
64
65 return &current_migration;
66}
67
Paolo Bonzini43eaae22012-10-02 18:21:18 +020068void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +000069{
aliguori34c9dd82008-10-13 03:14:31 +000070 const char *p;
71
72 if (strstart(uri, "tcp:", &p))
Paolo Bonzini43eaae22012-10-02 18:21:18 +020073 tcp_start_incoming_migration(p, errp);
aliguori065e2812008-11-11 16:46:33 +000074#if !defined(WIN32)
75 else if (strstart(uri, "exec:", &p))
Paolo Bonzini43eaae22012-10-02 18:21:18 +020076 exec_start_incoming_migration(p, errp);
Chris Lalancette4951f652009-08-05 17:24:29 +020077 else if (strstart(uri, "unix:", &p))
Paolo Bonzini43eaae22012-10-02 18:21:18 +020078 unix_start_incoming_migration(p, errp);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +020079 else if (strstart(uri, "fd:", &p))
Paolo Bonzini43eaae22012-10-02 18:21:18 +020080 fd_start_incoming_migration(p, errp);
aliguori065e2812008-11-11 16:46:33 +000081#endif
Juan Quintela8ca5e802010-06-09 14:10:54 +020082 else {
Paolo Bonzini43eaae22012-10-02 18:21:18 +020083 error_setg(errp, "unknown migration protocol: %s\n", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +020084 }
aliguori5bb79102008-10-13 03:12:02 +000085}
86
Paolo Bonzini82a4da72012-08-07 10:57:43 +020087static void process_incoming_migration_co(void *opaque)
Juan Quintela511c0232010-06-09 14:10:55 +020088{
Paolo Bonzini82a4da72012-08-07 10:57:43 +020089 QEMUFile *f = opaque;
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +020090 int ret;
91
92 ret = qemu_loadvm_state(f);
Paolo Bonzini82a4da72012-08-07 10:57:43 +020093 qemu_set_fd_handler(qemu_get_fd(f), NULL, NULL, NULL);
Paolo Bonzini1c12e1f2012-08-07 10:51:51 +020094 qemu_fclose(f);
95 if (ret < 0) {
Juan Quintela511c0232010-06-09 14:10:55 +020096 fprintf(stderr, "load of migration failed\n");
97 exit(0);
98 }
99 qemu_announce_self();
100 DPRINTF("successfully loaded vm state\n");
101
BenoƮt Canet901862c2012-03-23 08:36:52 +0100102 bdrv_clear_incoming_migration_all();
Anthony Liguori0f154232011-11-14 15:09:45 -0600103 /* Make sure all file formats flush their mutable metadata */
104 bdrv_invalidate_cache_all();
105
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300106 if (autostart) {
Juan Quintela511c0232010-06-09 14:10:55 +0200107 vm_start();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300108 } else {
Paolo Bonzini29ed72f2012-10-19 16:45:24 +0200109 runstate_set(RUN_STATE_PAUSED);
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300110 }
Juan Quintela511c0232010-06-09 14:10:55 +0200111}
112
Paolo Bonzini82a4da72012-08-07 10:57:43 +0200113static void enter_migration_coroutine(void *opaque)
114{
115 Coroutine *co = opaque;
116 qemu_coroutine_enter(co, NULL);
117}
118
119void process_incoming_migration(QEMUFile *f)
120{
121 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
122 int fd = qemu_get_fd(f);
123
124 assert(fd != -1);
125 socket_set_nonblock(fd);
126 qemu_set_fd_handler(fd, enter_migration_coroutine, NULL, co);
127 qemu_coroutine_enter(co, f);
128}
129
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400130/* amount of nanoseconds we are willing to wait for migration to be down.
131 * the choice of nanoseconds is because it is the maximum resolution that
132 * get_clock() can achieve. It is an internal measure. All user-visible
133 * units must be in seconds */
134static uint64_t max_downtime = 30000000;
135
136uint64_t migrate_max_downtime(void)
137{
138 return max_downtime;
139}
140
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300141MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
142{
143 MigrationCapabilityStatusList *head = NULL;
144 MigrationCapabilityStatusList *caps;
145 MigrationState *s = migrate_get_current();
146 int i;
147
148 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
149 if (head == NULL) {
150 head = g_malloc0(sizeof(*caps));
151 caps = head;
152 } else {
153 caps->next = g_malloc0(sizeof(*caps));
154 caps = caps->next;
155 }
156 caps->value =
157 g_malloc(sizeof(*caps->value));
158 caps->value->capability = i;
159 caps->value->state = s->enabled_capabilities[i];
160 }
161
162 return head;
163}
164
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300165static void get_xbzrle_cache_stats(MigrationInfo *info)
166{
167 if (migrate_use_xbzrle()) {
168 info->has_xbzrle_cache = true;
169 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
170 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
171 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
172 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
173 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
174 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
175 }
176}
177
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300178MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000179{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300180 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200181 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000182
Juan Quintela17549e82011-10-05 13:50:43 +0200183 switch (s->state) {
184 case MIG_STATE_SETUP:
185 /* no migration has happened ever */
186 break;
187 case MIG_STATE_ACTIVE:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300188 info->has_status = true;
189 info->status = g_strdup("active");
Juan Quintela7aa939a2012-08-18 13:17:10 +0200190 info->has_total_time = true;
191 info->total_time = qemu_get_clock_ms(rt_clock)
192 - s->total_time;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200193 info->has_expected_downtime = true;
194 info->expected_downtime = s->expected_downtime;
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200195
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300196 info->has_ram = true;
197 info->ram = g_malloc0(sizeof(*info->ram));
198 info->ram->transferred = ram_bytes_transferred();
199 info->ram->remaining = ram_bytes_remaining();
200 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300201 info->ram->duplicate = dup_mig_pages_transferred();
202 info->ram->normal = norm_mig_pages_transferred();
203 info->ram->normal_bytes = norm_mig_bytes_transferred();
Juan Quintela8d017192012-08-13 12:31:25 +0200204 info->ram->dirty_pages_rate = s->dirty_pages_rate;
205
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200206
Juan Quintela17549e82011-10-05 13:50:43 +0200207 if (blk_mig_active()) {
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300208 info->has_disk = true;
209 info->disk = g_malloc0(sizeof(*info->disk));
210 info->disk->transferred = blk_mig_bytes_transferred();
211 info->disk->remaining = blk_mig_bytes_remaining();
212 info->disk->total = blk_mig_bytes_total();
aliguoriff8d81d2008-10-24 22:10:31 +0000213 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300214
215 get_xbzrle_cache_stats(info);
Juan Quintela17549e82011-10-05 13:50:43 +0200216 break;
217 case MIG_STATE_COMPLETED:
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300218 get_xbzrle_cache_stats(info);
219
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300220 info->has_status = true;
221 info->status = g_strdup("completed");
Juan Quintela7aa939a2012-08-18 13:17:10 +0200222 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200223 info->has_downtime = true;
224 info->downtime = s->downtime;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200225
226 info->has_ram = true;
227 info->ram = g_malloc0(sizeof(*info->ram));
228 info->ram->transferred = ram_bytes_transferred();
229 info->ram->remaining = 0;
230 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300231 info->ram->duplicate = dup_mig_pages_transferred();
232 info->ram->normal = norm_mig_pages_transferred();
233 info->ram->normal_bytes = norm_mig_bytes_transferred();
Juan Quintela17549e82011-10-05 13:50:43 +0200234 break;
235 case MIG_STATE_ERROR:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300236 info->has_status = true;
237 info->status = g_strdup("failed");
Juan Quintela17549e82011-10-05 13:50:43 +0200238 break;
239 case MIG_STATE_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300240 info->has_status = true;
241 info->status = g_strdup("cancelled");
Juan Quintela17549e82011-10-05 13:50:43 +0200242 break;
aliguori5bb79102008-10-13 03:12:02 +0000243 }
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300244
245 return info;
aliguori5bb79102008-10-13 03:12:02 +0000246}
247
Orit Wasserman00458432012-08-06 21:42:48 +0300248void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
249 Error **errp)
250{
251 MigrationState *s = migrate_get_current();
252 MigrationCapabilityStatusList *cap;
253
254 if (s->state == MIG_STATE_ACTIVE) {
255 error_set(errp, QERR_MIGRATION_ACTIVE);
256 return;
257 }
258
259 for (cap = params; cap; cap = cap->next) {
260 s->enabled_capabilities[cap->value->capability] = cap->value->state;
261 }
262}
263
aliguori065e2812008-11-11 16:46:33 +0000264/* shared migration helpers */
265
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200266static int migrate_fd_cleanup(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000267{
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500268 int ret = 0;
269
aliguori065e2812008-11-11 16:46:33 +0000270 if (s->file) {
malcd0f2c4c2010-02-07 02:03:50 +0300271 DPRINTF("closing file\n");
Eduardo Habkosta6d34a92011-11-10 10:41:42 -0200272 ret = qemu_fclose(s->file);
Jan Kiszka5d39c792009-11-30 18:21:19 +0100273 s->file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000274 }
275
Paolo Bonzini24ea1e42012-11-10 18:58:40 +0100276 assert(s->fd == -1);
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500277 return ret;
aliguori065e2812008-11-11 16:46:33 +0000278}
279
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200280void migrate_fd_error(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000281{
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200282 DPRINTF("setting error state\n");
283 s->state = MIG_STATE_ERROR;
Juan Quintelae0eb7392011-10-05 14:27:52 +0200284 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200285 migrate_fd_cleanup(s);
286}
287
Juan Quintela458cf282011-02-22 23:32:54 +0100288static void migrate_fd_completed(MigrationState *s)
289{
290 DPRINTF("setting completed state\n");
291 if (migrate_fd_cleanup(s) < 0) {
292 s->state = MIG_STATE_ERROR;
293 } else {
294 s->state = MIG_STATE_COMPLETED;
295 runstate_set(RUN_STATE_POSTMIGRATE);
296 }
Juan Quintelae0eb7392011-10-05 14:27:52 +0200297 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela458cf282011-02-22 23:32:54 +0100298}
299
Juan Quintelac87b0152012-07-20 13:10:54 +0200300ssize_t migrate_fd_put_buffer(MigrationState *s, const void *data,
301 size_t size)
aliguori065e2812008-11-11 16:46:33 +0000302{
aliguori065e2812008-11-11 16:46:33 +0000303 ssize_t ret;
304
Juan Quintelafdbecb52011-09-21 22:37:29 +0200305 if (s->state != MIG_STATE_ACTIVE) {
306 return -EIO;
307 }
308
aliguori065e2812008-11-11 16:46:33 +0000309 do {
310 ret = s->write(s, data, size);
Uri Lublin95b134e2009-05-19 14:08:53 +0300311 } while (ret == -1 && ((s->get_error(s)) == EINTR));
aliguori065e2812008-11-11 16:46:33 +0000312
313 if (ret == -1)
314 ret = -(s->get_error(s));
315
aliguori065e2812008-11-11 16:46:33 +0000316 return ret;
317}
318
Juan Quintelae4ed1542012-09-21 11:18:18 +0200319bool migrate_fd_put_ready(MigrationState *s, uint64_t max_size)
aliguori065e2812008-11-11 16:46:33 +0000320{
321 int ret;
Juan Quintelae4ed1542012-09-21 11:18:18 +0200322 uint64_t pending_size;
323 bool last_round = false;
aliguori065e2812008-11-11 16:46:33 +0000324
Juan Quintelae7627482012-07-23 06:31:30 +0200325 qemu_mutex_lock_iothread();
aliguori065e2812008-11-11 16:46:33 +0000326 if (s->state != MIG_STATE_ACTIVE) {
malcd0f2c4c2010-02-07 02:03:50 +0300327 DPRINTF("put_ready returning because of non-active state\n");
Juan Quintelae7627482012-07-23 06:31:30 +0200328 qemu_mutex_unlock_iothread();
Juan Quintelae4ed1542012-09-21 11:18:18 +0200329 return false;
aliguori065e2812008-11-11 16:46:33 +0000330 }
Juan Quintela766bd172012-07-23 05:45:29 +0200331 if (s->first_time) {
332 s->first_time = false;
333 DPRINTF("beginning savevm\n");
334 ret = qemu_savevm_state_begin(s->file, &s->params);
335 if (ret < 0) {
336 DPRINTF("failed, %d\n", ret);
337 migrate_fd_error(s);
Juan Quintelae7627482012-07-23 06:31:30 +0200338 qemu_mutex_unlock_iothread();
Juan Quintelae4ed1542012-09-21 11:18:18 +0200339 return false;
Juan Quintela766bd172012-07-23 05:45:29 +0200340 }
341 }
aliguori065e2812008-11-11 16:46:33 +0000342
malcd0f2c4c2010-02-07 02:03:50 +0300343 DPRINTF("iterate\n");
Juan Quintelae4ed1542012-09-21 11:18:18 +0200344 pending_size = qemu_savevm_state_pending(s->file, max_size);
345 DPRINTF("pending size %lu max %lu\n", pending_size, max_size);
346 if (pending_size >= max_size) {
347 ret = qemu_savevm_state_iterate(s->file);
348 if (ret < 0) {
349 migrate_fd_error(s);
350 }
351 } else {
Luiz Capitulino13548692011-07-29 15:36:43 -0300352 int old_vm_running = runstate_is_running();
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200353 int64_t start_time, end_time;
Anthony Liguorieeb34af2009-07-09 13:25:47 -0500354
malcd0f2c4c2010-02-07 02:03:50 +0300355 DPRINTF("done iterating\n");
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200356 start_time = qemu_get_clock_ms(rt_clock);
Gerd Hoffmann7b5d3aa2012-03-07 08:00:26 +0100357 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
Juan Quintela766bd172012-07-23 05:45:29 +0200358 if (old_vm_running) {
359 vm_stop(RUN_STATE_FINISH_MIGRATE);
360 } else {
361 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
362 }
aliguori065e2812008-11-11 16:46:33 +0000363
Luiz Capitulino539de122011-12-05 14:06:56 -0200364 if (qemu_savevm_state_complete(s->file) < 0) {
Juan Quintela67afff72011-02-22 23:18:20 +0100365 migrate_fd_error(s);
aliguorib161d122009-04-05 19:30:33 +0000366 } else {
Juan Quintela458cf282011-02-22 23:32:54 +0100367 migrate_fd_completed(s);
aliguorib161d122009-04-05 19:30:33 +0000368 }
Juan Quintela97d4d962012-08-10 21:53:08 +0200369 end_time = qemu_get_clock_ms(rt_clock);
370 s->total_time = end_time - s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200371 s->downtime = end_time - start_time;
Juan Quintela48a2f4d2010-05-11 23:28:53 +0200372 if (s->state != MIG_STATE_COMPLETED) {
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500373 if (old_vm_running) {
374 vm_start();
375 }
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500376 }
Juan Quintelae4ed1542012-09-21 11:18:18 +0200377 last_round = true;
aliguori065e2812008-11-11 16:46:33 +0000378 }
Juan Quintelae7627482012-07-23 06:31:30 +0200379 qemu_mutex_unlock_iothread();
380
Juan Quintelae4ed1542012-09-21 11:18:18 +0200381 return last_round;
aliguori065e2812008-11-11 16:46:33 +0000382}
383
Juan Quintela0edda1c2010-05-11 16:28:39 +0200384static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000385{
aliguori065e2812008-11-11 16:46:33 +0000386 if (s->state != MIG_STATE_ACTIVE)
387 return;
388
malcd0f2c4c2010-02-07 02:03:50 +0300389 DPRINTF("cancelling migration\n");
aliguori065e2812008-11-11 16:46:33 +0000390
391 s->state = MIG_STATE_CANCELLED;
Juan Quintelae0eb7392011-10-05 14:27:52 +0200392 notifier_list_notify(&migration_state_notifiers, s);
Luiz Capitulino539de122011-12-05 14:06:56 -0200393 qemu_savevm_state_cancel(s->file);
aliguori065e2812008-11-11 16:46:33 +0000394
395 migrate_fd_cleanup(s);
396}
397
Juan Quintela11c76742012-07-20 13:19:36 +0200398int migrate_fd_close(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000399{
Paolo Bonzini8dc592e2012-09-27 13:25:45 +0200400 int rc = 0;
401 if (s->fd != -1) {
Paolo Bonzini8dc592e2012-09-27 13:25:45 +0200402 rc = s->close(s);
403 s->fd = -1;
404 }
405 return rc;
aliguori065e2812008-11-11 16:46:33 +0000406}
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100407
408void add_migration_state_change_notifier(Notifier *notify)
409{
410 notifier_list_add(&migration_state_notifiers, notify);
411}
412
413void remove_migration_state_change_notifier(Notifier *notify)
414{
Paolo Bonzini31552522012-01-13 17:34:01 +0100415 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100416}
417
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200418bool migration_is_active(MigrationState *s)
419{
420 return s->state == MIG_STATE_ACTIVE;
421}
422
Juan Quintela70736932011-02-23 00:43:59 +0100423bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100424{
Juan Quintela70736932011-02-23 00:43:59 +0100425 return s->state == MIG_STATE_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100426}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200427
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200428bool migration_has_failed(MigrationState *s)
429{
430 return (s->state == MIG_STATE_CANCELLED ||
431 s->state == MIG_STATE_ERROR);
432}
433
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200434void migrate_fd_connect(MigrationState *s)
435{
Juan Quintelad5934dd2010-05-11 23:01:53 +0200436 s->state = MIG_STATE_ACTIVE;
Juan Quintela766bd172012-07-23 05:45:29 +0200437 s->first_time = true;
Juan Quintelaedfa1af2012-07-23 02:13:23 +0200438 qemu_fopen_ops_buffered(s);
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200439}
440
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300441static MigrationState *migrate_init(const MigrationParams *params)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200442{
Juan Quintela17549e82011-10-05 13:50:43 +0200443 MigrationState *s = migrate_get_current();
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100444 int64_t bandwidth_limit = s->bandwidth_limit;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300445 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300446 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300447
448 memcpy(enabled_capabilities, s->enabled_capabilities,
449 sizeof(enabled_capabilities));
Juan Quintela0edda1c2010-05-11 16:28:39 +0200450
Juan Quintela17549e82011-10-05 13:50:43 +0200451 memset(s, 0, sizeof(*s));
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100452 s->bandwidth_limit = bandwidth_limit;
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300453 s->params = *params;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300454 memcpy(s->enabled_capabilities, enabled_capabilities,
455 sizeof(enabled_capabilities));
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300456 s->xbzrle_cache_size = xbzrle_cache_size;
Juan Quintela1299c632011-11-09 21:29:01 +0100457
Juan Quintela0edda1c2010-05-11 16:28:39 +0200458 s->bandwidth_limit = bandwidth_limit;
Juan Quintelad5934dd2010-05-11 23:01:53 +0200459 s->state = MIG_STATE_SETUP;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200460 s->total_time = qemu_get_clock_ms(rt_clock);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200461
Juan Quintela0edda1c2010-05-11 16:28:39 +0200462 return s;
463}
Juan Quintelacab30142011-02-22 23:54:21 +0100464
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600465static GSList *migration_blockers;
466
467void migrate_add_blocker(Error *reason)
468{
469 migration_blockers = g_slist_prepend(migration_blockers, reason);
470}
471
472void migrate_del_blocker(Error *reason)
473{
474 migration_blockers = g_slist_remove(migration_blockers, reason);
475}
476
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200477void qmp_migrate(const char *uri, bool has_blk, bool blk,
478 bool has_inc, bool inc, bool has_detach, bool detach,
479 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100480{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +0200481 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +0200482 MigrationState *s = migrate_get_current();
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300483 MigrationParams params;
Juan Quintelacab30142011-02-22 23:54:21 +0100484 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +0100485
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300486 params.blk = blk;
487 params.shared = inc;
488
Juan Quintela17549e82011-10-05 13:50:43 +0200489 if (s->state == MIG_STATE_ACTIVE) {
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
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200494 if (qemu_savevm_state_blocked(errp)) {
495 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100496 }
497
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600498 if (migration_blockers) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200499 *errp = error_copy(migration_blockers->data);
500 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600501 }
502
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300503 s = migrate_init(&params);
Juan Quintelacab30142011-02-22 23:54:21 +0100504
505 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200506 tcp_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100507#if !defined(WIN32)
508 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200509 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100510 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200511 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100512 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200513 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100514#endif
515 } else {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200516 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
517 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100518 }
519
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200520 if (local_err) {
Paolo Bonzini342ab8d2012-10-02 09:59:38 +0200521 migrate_fd_error(s);
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200522 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200523 return;
Juan Quintela1299c632011-11-09 21:29:01 +0100524 }
525
Juan Quintelae0eb7392011-10-05 14:27:52 +0200526 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintelacab30142011-02-22 23:54:21 +0100527}
528
Luiz Capitulino6cdedb02011-11-27 22:54:09 -0200529void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100530{
Juan Quintela17549e82011-10-05 13:50:43 +0200531 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +0100532}
533
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300534void qmp_migrate_set_cache_size(int64_t value, Error **errp)
535{
536 MigrationState *s = migrate_get_current();
537
538 /* Check for truncation */
539 if (value != (size_t)value) {
540 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
541 "exceeding address space");
542 return;
543 }
544
545 s->xbzrle_cache_size = xbzrle_cache_resize(value);
546}
547
548int64_t qmp_query_migrate_cache_size(Error **errp)
549{
550 return migrate_xbzrle_cache_size();
551}
552
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200553void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100554{
Juan Quintelacab30142011-02-22 23:54:21 +0100555 MigrationState *s;
556
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200557 if (value < 0) {
558 value = 0;
Juan Quintelacab30142011-02-22 23:54:21 +0100559 }
Juan Quintelacab30142011-02-22 23:54:21 +0100560
Juan Quintela17549e82011-10-05 13:50:43 +0200561 s = migrate_get_current();
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200562 s->bandwidth_limit = value;
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100563 qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
Juan Quintelacab30142011-02-22 23:54:21 +0100564}
565
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200566void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100567{
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200568 value *= 1e9;
569 value = MAX(0, MIN(UINT64_MAX, value));
570 max_downtime = (uint64_t)value;
aliguori5bb79102008-10-13 03:12:02 +0000571}
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300572
573int migrate_use_xbzrle(void)
574{
575 MigrationState *s;
576
577 s = migrate_get_current();
578
579 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
580}
581
582int64_t migrate_xbzrle_cache_size(void)
583{
584 MigrationState *s;
585
586 s = migrate_get_current();
587
588 return s->xbzrle_cache_size;
589}
Juan Quintela0d82d0e2012-10-03 14:18:33 +0200590
591/* migration thread support */
592
593typedef struct QEMUFileBuffered {
594 MigrationState *migration_state;
595 QEMUFile *file;
596 size_t bytes_xfer;
597 size_t xfer_limit;
598 uint8_t *buffer;
599 size_t buffer_size;
600 size_t buffer_capacity;
601 QemuThread thread;
602} QEMUFileBuffered;
603
604static ssize_t buffered_flush(QEMUFileBuffered *s)
605{
606 size_t offset = 0;
607 ssize_t ret = 0;
608
609 DPRINTF("flushing %zu byte(s) of data\n", s->buffer_size);
610
611 while (s->bytes_xfer < s->xfer_limit && offset < s->buffer_size) {
612 size_t to_send = MIN(s->buffer_size - offset, s->xfer_limit - s->bytes_xfer);
613 ret = migrate_fd_put_buffer(s->migration_state, s->buffer + offset,
614 to_send);
615 if (ret <= 0) {
616 DPRINTF("error flushing data, %zd\n", ret);
617 break;
618 } else {
619 DPRINTF("flushed %zd byte(s)\n", ret);
620 offset += ret;
621 s->bytes_xfer += ret;
622 }
623 }
624
625 DPRINTF("flushed %zu of %zu byte(s)\n", offset, s->buffer_size);
626 memmove(s->buffer, s->buffer + offset, s->buffer_size - offset);
627 s->buffer_size -= offset;
628
629 if (ret < 0) {
630 return ret;
631 }
632 return offset;
633}
634
635static int buffered_put_buffer(void *opaque, const uint8_t *buf,
636 int64_t pos, int size)
637{
638 QEMUFileBuffered *s = opaque;
639 ssize_t error;
640
641 DPRINTF("putting %d bytes at %" PRId64 "\n", size, pos);
642
643 error = qemu_file_get_error(s->file);
644 if (error) {
645 DPRINTF("flush when error, bailing: %s\n", strerror(-error));
646 return error;
647 }
648
649 if (size <= 0) {
650 return size;
651 }
652
653 if (size > (s->buffer_capacity - s->buffer_size)) {
654 DPRINTF("increasing buffer capacity from %zu by %zu\n",
655 s->buffer_capacity, size + 1024);
656
657 s->buffer_capacity += size + 1024;
658
659 s->buffer = g_realloc(s->buffer, s->buffer_capacity);
660 }
661
662 memcpy(s->buffer + s->buffer_size, buf, size);
663 s->buffer_size += size;
664
665 return size;
666}
667
668static int buffered_close(void *opaque)
669{
670 QEMUFileBuffered *s = opaque;
671 ssize_t ret = 0;
672 int ret2;
673
674 DPRINTF("closing\n");
675
676 s->xfer_limit = INT_MAX;
677 while (!qemu_file_get_error(s->file) && s->buffer_size) {
678 ret = buffered_flush(s);
679 if (ret < 0) {
680 break;
681 }
682 }
683
684 ret2 = migrate_fd_close(s->migration_state);
685 if (ret >= 0) {
686 ret = ret2;
687 }
688 ret = migrate_fd_close(s->migration_state);
689 s->migration_state->complete = true;
690 return ret;
691}
692
693static int buffered_get_fd(void *opaque)
694{
695 QEMUFileBuffered *s = opaque;
696
697 return qemu_get_fd(s->file);
698}
699
700/*
701 * The meaning of the return values is:
702 * 0: We can continue sending
703 * 1: Time to stop
704 * negative: There has been an error
705 */
706static int buffered_rate_limit(void *opaque)
707{
708 QEMUFileBuffered *s = opaque;
709 int ret;
710
711 ret = qemu_file_get_error(s->file);
712 if (ret) {
713 return ret;
714 }
715
716 if (s->bytes_xfer > s->xfer_limit) {
717 return 1;
718 }
719
720 return 0;
721}
722
723static int64_t buffered_set_rate_limit(void *opaque, int64_t new_rate)
724{
725 QEMUFileBuffered *s = opaque;
726 if (qemu_file_get_error(s->file)) {
727 goto out;
728 }
729 if (new_rate > SIZE_MAX) {
730 new_rate = SIZE_MAX;
731 }
732
733 s->xfer_limit = new_rate / 10;
734
735out:
736 return s->xfer_limit;
737}
738
739static int64_t buffered_get_rate_limit(void *opaque)
740{
741 QEMUFileBuffered *s = opaque;
742
743 return s->xfer_limit;
744}
745
746/* 100ms xfer_limit is the limit that we should write each 100ms */
747#define BUFFER_DELAY 100
748
749static void *buffered_file_thread(void *opaque)
750{
751 QEMUFileBuffered *s = opaque;
752 int64_t initial_time = qemu_get_clock_ms(rt_clock);
753 int64_t max_size = 0;
754 bool last_round = false;
755
756 while (true) {
757 int64_t current_time = qemu_get_clock_ms(rt_clock);
758
759 if (s->migration_state->complete) {
760 break;
761 }
762 if (current_time >= initial_time + BUFFER_DELAY) {
763 uint64_t transferred_bytes = s->bytes_xfer;
764 uint64_t time_spent = current_time - initial_time;
765 double bandwidth = transferred_bytes / time_spent;
766 max_size = bandwidth * migrate_max_downtime() / 1000000;
767
768 DPRINTF("transferred %" PRIu64 " time_spent %" PRIu64
769 " bandwidth %g max_size %" PRId64 "\n",
770 transferred_bytes, time_spent, bandwidth, max_size);
771
772 s->bytes_xfer = 0;
773 initial_time = current_time;
774 }
775 if (!last_round && (s->bytes_xfer >= s->xfer_limit)) {
776 /* usleep expects microseconds */
777 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
778 }
779 if (buffered_flush(s) < 0) {
780 break;
781 }
782
783 DPRINTF("file is ready\n");
784 if (s->bytes_xfer < s->xfer_limit) {
785 DPRINTF("notifying client\n");
786 last_round = migrate_fd_put_ready(s->migration_state, max_size);
787 }
788 }
789
790 g_free(s->buffer);
791 g_free(s);
792 return NULL;
793}
794
795static const QEMUFileOps buffered_file_ops = {
796 .get_fd = buffered_get_fd,
797 .put_buffer = buffered_put_buffer,
798 .close = buffered_close,
799 .rate_limit = buffered_rate_limit,
800 .get_rate_limit = buffered_get_rate_limit,
801 .set_rate_limit = buffered_set_rate_limit,
802};
803
804void qemu_fopen_ops_buffered(MigrationState *migration_state)
805{
806 QEMUFileBuffered *s;
807
808 s = g_malloc0(sizeof(*s));
809
810 s->migration_state = migration_state;
811 s->xfer_limit = migration_state->bandwidth_limit / 10;
812 s->migration_state->complete = false;
813
814 s->file = qemu_fopen_ops(s, &buffered_file_ops);
815
816 migration_state->file = s->file;
817
818 qemu_thread_create(&s->thread, buffered_file_thread, s,
819 QEMU_THREAD_DETACHED);
820}