blob: a63596f9a0ba656291f9e4d9a2c1c2b0141c8cc6 [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"
17#include "migration.h"
aliguori376253e2009-03-05 23:01:23 +000018#include "monitor.h"
aliguori065e2812008-11-11 16:46:33 +000019#include "buffered_file.h"
20#include "sysemu.h"
21#include "block.h"
22#include "qemu_socket.h"
Jan Kiszka25f23642009-11-30 18:21:21 +010023#include "block-migration.h"
Luiz Capitulino791e7c82011-09-13 17:37:16 -030024#include "qmp-commands.h"
aliguori065e2812008-11-11 16:46:33 +000025
26//#define DEBUG_MIGRATION
27
28#ifdef DEBUG_MIGRATION
malcd0f2c4c2010-02-07 02:03:50 +030029#define DPRINTF(fmt, ...) \
aliguori065e2812008-11-11 16:46:33 +000030 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
31#else
malcd0f2c4c2010-02-07 02:03:50 +030032#define DPRINTF(fmt, ...) \
aliguori065e2812008-11-11 16:46:33 +000033 do { } while (0)
34#endif
aliguori5bb79102008-10-13 03:12:02 +000035
Juan Quintela7dc688e2011-02-23 00:48:46 +010036enum {
37 MIG_STATE_ERROR,
38 MIG_STATE_SETUP,
39 MIG_STATE_CANCELLED,
40 MIG_STATE_ACTIVE,
41 MIG_STATE_COMPLETED,
42};
aliguori5bb79102008-10-13 03:12:02 +000043
Juan Quintelad0ae46c2011-02-23 00:33:19 +010044#define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
aliguori5bb79102008-10-13 03:12:02 +000045
Orit Wasserman17ad9b32012-08-06 21:42:53 +030046/* Migration XBZRLE default cache size */
47#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
48
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010049static NotifierList migration_state_notifiers =
50 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
51
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 = {
59 .state = MIG_STATE_SETUP,
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,
Juan Quintela17549e82011-10-05 13:50:43 +020062 };
63
64 return &current_migration;
65}
66
Paolo Bonzini43eaae22012-10-02 18:21:18 +020067void qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +000068{
aliguori34c9dd82008-10-13 03:14:31 +000069 const char *p;
70
71 if (strstart(uri, "tcp:", &p))
Paolo Bonzini43eaae22012-10-02 18:21:18 +020072 tcp_start_incoming_migration(p, errp);
aliguori065e2812008-11-11 16:46:33 +000073#if !defined(WIN32)
74 else if (strstart(uri, "exec:", &p))
Paolo Bonzini43eaae22012-10-02 18:21:18 +020075 exec_start_incoming_migration(p, errp);
Chris Lalancette4951f652009-08-05 17:24:29 +020076 else if (strstart(uri, "unix:", &p))
Paolo Bonzini43eaae22012-10-02 18:21:18 +020077 unix_start_incoming_migration(p, errp);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +020078 else if (strstart(uri, "fd:", &p))
Paolo Bonzini43eaae22012-10-02 18:21:18 +020079 fd_start_incoming_migration(p, errp);
aliguori065e2812008-11-11 16:46:33 +000080#endif
Juan Quintela8ca5e802010-06-09 14:10:54 +020081 else {
Paolo Bonzini43eaae22012-10-02 18:21:18 +020082 error_setg(errp, "unknown migration protocol: %s\n", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +020083 }
aliguori5bb79102008-10-13 03:12:02 +000084}
85
Juan Quintela511c0232010-06-09 14:10:55 +020086void process_incoming_migration(QEMUFile *f)
87{
88 if (qemu_loadvm_state(f) < 0) {
89 fprintf(stderr, "load of migration failed\n");
90 exit(0);
91 }
92 qemu_announce_self();
93 DPRINTF("successfully loaded vm state\n");
94
BenoƮt Canet901862c2012-03-23 08:36:52 +010095 bdrv_clear_incoming_migration_all();
Anthony Liguori0f154232011-11-14 15:09:45 -060096 /* Make sure all file formats flush their mutable metadata */
97 bdrv_invalidate_cache_all();
98
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -030099 if (autostart) {
Juan Quintela511c0232010-06-09 14:10:55 +0200100 vm_start();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300101 } else {
Paolo Bonzini29ed72f2012-10-19 16:45:24 +0200102 runstate_set(RUN_STATE_PAUSED);
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300103 }
Juan Quintela511c0232010-06-09 14:10:55 +0200104}
105
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400106/* amount of nanoseconds we are willing to wait for migration to be down.
107 * the choice of nanoseconds is because it is the maximum resolution that
108 * get_clock() can achieve. It is an internal measure. All user-visible
109 * units must be in seconds */
110static uint64_t max_downtime = 30000000;
111
112uint64_t migrate_max_downtime(void)
113{
114 return max_downtime;
115}
116
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300117MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
118{
119 MigrationCapabilityStatusList *head = NULL;
120 MigrationCapabilityStatusList *caps;
121 MigrationState *s = migrate_get_current();
122 int i;
123
124 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
125 if (head == NULL) {
126 head = g_malloc0(sizeof(*caps));
127 caps = head;
128 } else {
129 caps->next = g_malloc0(sizeof(*caps));
130 caps = caps->next;
131 }
132 caps->value =
133 g_malloc(sizeof(*caps->value));
134 caps->value->capability = i;
135 caps->value->state = s->enabled_capabilities[i];
136 }
137
138 return head;
139}
140
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300141static void get_xbzrle_cache_stats(MigrationInfo *info)
142{
143 if (migrate_use_xbzrle()) {
144 info->has_xbzrle_cache = true;
145 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
146 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
147 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
148 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
149 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
150 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
151 }
152}
153
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300154MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000155{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300156 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200157 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000158
Juan Quintela17549e82011-10-05 13:50:43 +0200159 switch (s->state) {
160 case MIG_STATE_SETUP:
161 /* no migration has happened ever */
162 break;
163 case MIG_STATE_ACTIVE:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300164 info->has_status = true;
165 info->status = g_strdup("active");
Juan Quintela7aa939a2012-08-18 13:17:10 +0200166 info->has_total_time = true;
167 info->total_time = qemu_get_clock_ms(rt_clock)
168 - s->total_time;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200169 info->has_expected_downtime = true;
170 info->expected_downtime = s->expected_downtime;
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200171
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300172 info->has_ram = true;
173 info->ram = g_malloc0(sizeof(*info->ram));
174 info->ram->transferred = ram_bytes_transferred();
175 info->ram->remaining = ram_bytes_remaining();
176 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300177 info->ram->duplicate = dup_mig_pages_transferred();
178 info->ram->normal = norm_mig_pages_transferred();
179 info->ram->normal_bytes = norm_mig_bytes_transferred();
Juan Quintela8d017192012-08-13 12:31:25 +0200180 info->ram->dirty_pages_rate = s->dirty_pages_rate;
181
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200182
Juan Quintela17549e82011-10-05 13:50:43 +0200183 if (blk_mig_active()) {
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300184 info->has_disk = true;
185 info->disk = g_malloc0(sizeof(*info->disk));
186 info->disk->transferred = blk_mig_bytes_transferred();
187 info->disk->remaining = blk_mig_bytes_remaining();
188 info->disk->total = blk_mig_bytes_total();
aliguoriff8d81d2008-10-24 22:10:31 +0000189 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300190
191 get_xbzrle_cache_stats(info);
Juan Quintela17549e82011-10-05 13:50:43 +0200192 break;
193 case MIG_STATE_COMPLETED:
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300194 get_xbzrle_cache_stats(info);
195
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300196 info->has_status = true;
197 info->status = g_strdup("completed");
Juan Quintela7aa939a2012-08-18 13:17:10 +0200198 info->total_time = s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200199 info->has_downtime = true;
200 info->downtime = s->downtime;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200201
202 info->has_ram = true;
203 info->ram = g_malloc0(sizeof(*info->ram));
204 info->ram->transferred = ram_bytes_transferred();
205 info->ram->remaining = 0;
206 info->ram->total = ram_bytes_total();
Orit Wasserman004d4c12012-08-06 21:42:56 +0300207 info->ram->duplicate = dup_mig_pages_transferred();
208 info->ram->normal = norm_mig_pages_transferred();
209 info->ram->normal_bytes = norm_mig_bytes_transferred();
Juan Quintela17549e82011-10-05 13:50:43 +0200210 break;
211 case MIG_STATE_ERROR:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300212 info->has_status = true;
213 info->status = g_strdup("failed");
Juan Quintela17549e82011-10-05 13:50:43 +0200214 break;
215 case MIG_STATE_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300216 info->has_status = true;
217 info->status = g_strdup("cancelled");
Juan Quintela17549e82011-10-05 13:50:43 +0200218 break;
aliguori5bb79102008-10-13 03:12:02 +0000219 }
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300220
221 return info;
aliguori5bb79102008-10-13 03:12:02 +0000222}
223
Orit Wasserman00458432012-08-06 21:42:48 +0300224void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
225 Error **errp)
226{
227 MigrationState *s = migrate_get_current();
228 MigrationCapabilityStatusList *cap;
229
230 if (s->state == MIG_STATE_ACTIVE) {
231 error_set(errp, QERR_MIGRATION_ACTIVE);
232 return;
233 }
234
235 for (cap = params; cap; cap = cap->next) {
236 s->enabled_capabilities[cap->value->capability] = cap->value->state;
237 }
238}
239
aliguori065e2812008-11-11 16:46:33 +0000240/* shared migration helpers */
241
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200242static int migrate_fd_cleanup(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000243{
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500244 int ret = 0;
245
aliguori065e2812008-11-11 16:46:33 +0000246 if (s->file) {
malcd0f2c4c2010-02-07 02:03:50 +0300247 DPRINTF("closing file\n");
Eduardo Habkosta6d34a92011-11-10 10:41:42 -0200248 ret = qemu_fclose(s->file);
Jan Kiszka5d39c792009-11-30 18:21:19 +0100249 s->file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000250 }
251
Paolo Bonzini8dc592e2012-09-27 13:25:45 +0200252 migrate_fd_close(s);
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500253 return ret;
aliguori065e2812008-11-11 16:46:33 +0000254}
255
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200256void migrate_fd_error(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000257{
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200258 DPRINTF("setting error state\n");
259 s->state = MIG_STATE_ERROR;
Juan Quintelae0eb7392011-10-05 14:27:52 +0200260 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200261 migrate_fd_cleanup(s);
262}
263
Juan Quintela458cf282011-02-22 23:32:54 +0100264static void migrate_fd_completed(MigrationState *s)
265{
266 DPRINTF("setting completed state\n");
267 if (migrate_fd_cleanup(s) < 0) {
268 s->state = MIG_STATE_ERROR;
269 } else {
270 s->state = MIG_STATE_COMPLETED;
271 runstate_set(RUN_STATE_POSTMIGRATE);
272 }
Juan Quintelae0eb7392011-10-05 14:27:52 +0200273 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela458cf282011-02-22 23:32:54 +0100274}
275
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200276static void migrate_fd_put_notify(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000277{
Juan Quintela22f00a42010-05-11 15:56:35 +0200278 MigrationState *s = opaque;
Juan Quintelaa2b41352012-09-04 12:45:42 +0200279 int ret;
aliguori065e2812008-11-11 16:46:33 +0000280
281 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
Juan Quintelaa2b41352012-09-04 12:45:42 +0200282 ret = qemu_file_put_notify(s->file);
283 if (ret) {
Yoshiaki Tamura2350e132011-02-23 00:01:24 +0900284 migrate_fd_error(s);
285 }
aliguori065e2812008-11-11 16:46:33 +0000286}
287
Juan Quintelac87b0152012-07-20 13:10:54 +0200288ssize_t migrate_fd_put_buffer(MigrationState *s, const void *data,
289 size_t size)
aliguori065e2812008-11-11 16:46:33 +0000290{
aliguori065e2812008-11-11 16:46:33 +0000291 ssize_t ret;
292
Juan Quintelafdbecb52011-09-21 22:37:29 +0200293 if (s->state != MIG_STATE_ACTIVE) {
294 return -EIO;
295 }
296
aliguori065e2812008-11-11 16:46:33 +0000297 do {
298 ret = s->write(s, data, size);
Uri Lublin95b134e2009-05-19 14:08:53 +0300299 } while (ret == -1 && ((s->get_error(s)) == EINTR));
aliguori065e2812008-11-11 16:46:33 +0000300
301 if (ret == -1)
302 ret = -(s->get_error(s));
303
Marcelo Tosattie447b1a2010-08-19 10:18:39 -0300304 if (ret == -EAGAIN) {
aliguori065e2812008-11-11 16:46:33 +0000305 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
Marcelo Tosattie447b1a2010-08-19 10:18:39 -0300306 }
aliguori065e2812008-11-11 16:46:33 +0000307
308 return ret;
309}
310
Juan Quintela2c9adcb2012-07-20 13:13:59 +0200311void migrate_fd_put_ready(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000312{
313 int ret;
314
aliguori065e2812008-11-11 16:46:33 +0000315 if (s->state != MIG_STATE_ACTIVE) {
malcd0f2c4c2010-02-07 02:03:50 +0300316 DPRINTF("put_ready returning because of non-active state\n");
aliguori065e2812008-11-11 16:46:33 +0000317 return;
318 }
319
malcd0f2c4c2010-02-07 02:03:50 +0300320 DPRINTF("iterate\n");
Luiz Capitulino539de122011-12-05 14:06:56 -0200321 ret = qemu_savevm_state_iterate(s->file);
Juan Quintela39346382011-09-22 11:02:14 +0200322 if (ret < 0) {
323 migrate_fd_error(s);
324 } else if (ret == 1) {
Luiz Capitulino13548692011-07-29 15:36:43 -0300325 int old_vm_running = runstate_is_running();
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200326 int64_t start_time, end_time;
Anthony Liguorieeb34af2009-07-09 13:25:47 -0500327
malcd0f2c4c2010-02-07 02:03:50 +0300328 DPRINTF("done iterating\n");
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200329 start_time = qemu_get_clock_ms(rt_clock);
Gerd Hoffmann7b5d3aa2012-03-07 08:00:26 +0100330 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300331 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
aliguori065e2812008-11-11 16:46:33 +0000332
Luiz Capitulino539de122011-12-05 14:06:56 -0200333 if (qemu_savevm_state_complete(s->file) < 0) {
Juan Quintela67afff72011-02-22 23:18:20 +0100334 migrate_fd_error(s);
aliguorib161d122009-04-05 19:30:33 +0000335 } else {
Juan Quintela458cf282011-02-22 23:32:54 +0100336 migrate_fd_completed(s);
aliguorib161d122009-04-05 19:30:33 +0000337 }
Juan Quintela97d4d962012-08-10 21:53:08 +0200338 end_time = qemu_get_clock_ms(rt_clock);
339 s->total_time = end_time - s->total_time;
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200340 s->downtime = end_time - start_time;
Juan Quintela48a2f4d2010-05-11 23:28:53 +0200341 if (s->state != MIG_STATE_COMPLETED) {
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500342 if (old_vm_running) {
343 vm_start();
344 }
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500345 }
aliguori065e2812008-11-11 16:46:33 +0000346 }
347}
348
Juan Quintela0edda1c2010-05-11 16:28:39 +0200349static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000350{
aliguori065e2812008-11-11 16:46:33 +0000351 if (s->state != MIG_STATE_ACTIVE)
352 return;
353
malcd0f2c4c2010-02-07 02:03:50 +0300354 DPRINTF("cancelling migration\n");
aliguori065e2812008-11-11 16:46:33 +0000355
356 s->state = MIG_STATE_CANCELLED;
Juan Quintelae0eb7392011-10-05 14:27:52 +0200357 notifier_list_notify(&migration_state_notifiers, s);
Luiz Capitulino539de122011-12-05 14:06:56 -0200358 qemu_savevm_state_cancel(s->file);
aliguori065e2812008-11-11 16:46:33 +0000359
360 migrate_fd_cleanup(s);
361}
362
Juan Quintela94997432012-08-24 12:51:48 +0200363int migrate_fd_wait_for_unfreeze(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000364{
aliguori065e2812008-11-11 16:46:33 +0000365 int ret;
366
malcd0f2c4c2010-02-07 02:03:50 +0300367 DPRINTF("wait for unfreeze\n");
aliguori065e2812008-11-11 16:46:33 +0000368 if (s->state != MIG_STATE_ACTIVE)
Juan Quintela94997432012-08-24 12:51:48 +0200369 return -EINVAL;
aliguori065e2812008-11-11 16:46:33 +0000370
371 do {
372 fd_set wfds;
373
374 FD_ZERO(&wfds);
375 FD_SET(s->fd, &wfds);
376
377 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
378 } while (ret == -1 && (s->get_error(s)) == EINTR);
Juan Quintelaaf509452011-09-21 22:46:36 +0200379
380 if (ret == -1) {
Juan Quintela94997432012-08-24 12:51:48 +0200381 return -s->get_error(s);
Juan Quintelaaf509452011-09-21 22:46:36 +0200382 }
Juan Quintela94997432012-08-24 12:51:48 +0200383 return 0;
aliguori065e2812008-11-11 16:46:33 +0000384}
385
Juan Quintela11c76742012-07-20 13:19:36 +0200386int migrate_fd_close(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000387{
Paolo Bonzini8dc592e2012-09-27 13:25:45 +0200388 int rc = 0;
389 if (s->fd != -1) {
390 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
391 rc = s->close(s);
392 s->fd = -1;
393 }
394 return rc;
aliguori065e2812008-11-11 16:46:33 +0000395}
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100396
397void add_migration_state_change_notifier(Notifier *notify)
398{
399 notifier_list_add(&migration_state_notifiers, notify);
400}
401
402void remove_migration_state_change_notifier(Notifier *notify)
403{
Paolo Bonzini31552522012-01-13 17:34:01 +0100404 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100405}
406
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200407bool migration_is_active(MigrationState *s)
408{
409 return s->state == MIG_STATE_ACTIVE;
410}
411
Juan Quintela70736932011-02-23 00:43:59 +0100412bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100413{
Juan Quintela70736932011-02-23 00:43:59 +0100414 return s->state == MIG_STATE_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100415}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200416
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200417bool migration_has_failed(MigrationState *s)
418{
419 return (s->state == MIG_STATE_CANCELLED ||
420 s->state == MIG_STATE_ERROR);
421}
422
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200423void migrate_fd_connect(MigrationState *s)
424{
425 int ret;
426
Juan Quintelad5934dd2010-05-11 23:01:53 +0200427 s->state = MIG_STATE_ACTIVE;
Juan Quintela796b4b02012-07-20 13:33:53 +0200428 s->file = qemu_fopen_ops_buffered(s);
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200429
430 DPRINTF("beginning savevm\n");
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300431 ret = qemu_savevm_state_begin(s->file, &s->params);
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200432 if (ret < 0) {
433 DPRINTF("failed, %d\n", ret);
434 migrate_fd_error(s);
435 return;
aliguori5bb79102008-10-13 03:12:02 +0000436 }
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200437 migrate_fd_put_ready(s);
438}
439
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300440static MigrationState *migrate_init(const MigrationParams *params)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200441{
Juan Quintela17549e82011-10-05 13:50:43 +0200442 MigrationState *s = migrate_get_current();
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100443 int64_t bandwidth_limit = s->bandwidth_limit;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300444 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300445 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300446
447 memcpy(enabled_capabilities, s->enabled_capabilities,
448 sizeof(enabled_capabilities));
Juan Quintela0edda1c2010-05-11 16:28:39 +0200449
Juan Quintela17549e82011-10-05 13:50:43 +0200450 memset(s, 0, sizeof(*s));
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100451 s->bandwidth_limit = bandwidth_limit;
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300452 s->params = *params;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300453 memcpy(s->enabled_capabilities, enabled_capabilities,
454 sizeof(enabled_capabilities));
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300455 s->xbzrle_cache_size = xbzrle_cache_size;
Juan Quintela1299c632011-11-09 21:29:01 +0100456
Juan Quintela0edda1c2010-05-11 16:28:39 +0200457 s->bandwidth_limit = bandwidth_limit;
Juan Quintelad5934dd2010-05-11 23:01:53 +0200458 s->state = MIG_STATE_SETUP;
Juan Quintelad5f8a572012-05-21 22:01:07 +0200459 s->total_time = qemu_get_clock_ms(rt_clock);
Juan Quintela0edda1c2010-05-11 16:28:39 +0200460
Juan Quintela0edda1c2010-05-11 16:28:39 +0200461 return s;
462}
Juan Quintelacab30142011-02-22 23:54:21 +0100463
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600464static GSList *migration_blockers;
465
466void migrate_add_blocker(Error *reason)
467{
468 migration_blockers = g_slist_prepend(migration_blockers, reason);
469}
470
471void migrate_del_blocker(Error *reason)
472{
473 migration_blockers = g_slist_remove(migration_blockers, reason);
474}
475
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200476void qmp_migrate(const char *uri, bool has_blk, bool blk,
477 bool has_inc, bool inc, bool has_detach, bool detach,
478 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100479{
Paolo Bonzinibe7059c2012-10-03 14:34:33 +0200480 Error *local_err = NULL;
Juan Quintela17549e82011-10-05 13:50:43 +0200481 MigrationState *s = migrate_get_current();
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300482 MigrationParams params;
Juan Quintelacab30142011-02-22 23:54:21 +0100483 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +0100484
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300485 params.blk = blk;
486 params.shared = inc;
487
Juan Quintela17549e82011-10-05 13:50:43 +0200488 if (s->state == MIG_STATE_ACTIVE) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200489 error_set(errp, QERR_MIGRATION_ACTIVE);
490 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100491 }
492
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200493 if (qemu_savevm_state_blocked(errp)) {
494 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100495 }
496
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600497 if (migration_blockers) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200498 *errp = error_copy(migration_blockers->data);
499 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600500 }
501
Isaku Yamahata6607ae22012-06-19 18:43:09 +0300502 s = migrate_init(&params);
Juan Quintelacab30142011-02-22 23:54:21 +0100503
504 if (strstart(uri, "tcp:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200505 tcp_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100506#if !defined(WIN32)
507 } else if (strstart(uri, "exec:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200508 exec_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100509 } else if (strstart(uri, "unix:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200510 unix_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100511 } else if (strstart(uri, "fd:", &p)) {
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200512 fd_start_outgoing_migration(s, p, &local_err);
Juan Quintelacab30142011-02-22 23:54:21 +0100513#endif
514 } else {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200515 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
516 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100517 }
518
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200519 if (local_err) {
Paolo Bonzini342ab8d2012-10-02 09:59:38 +0200520 migrate_fd_error(s);
Paolo Bonzinif37afb52012-10-02 10:02:46 +0200521 error_propagate(errp, local_err);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200522 return;
Juan Quintela1299c632011-11-09 21:29:01 +0100523 }
524
Juan Quintelae0eb7392011-10-05 14:27:52 +0200525 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintelacab30142011-02-22 23:54:21 +0100526}
527
Luiz Capitulino6cdedb02011-11-27 22:54:09 -0200528void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100529{
Juan Quintela17549e82011-10-05 13:50:43 +0200530 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +0100531}
532
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300533void qmp_migrate_set_cache_size(int64_t value, Error **errp)
534{
535 MigrationState *s = migrate_get_current();
536
537 /* Check for truncation */
538 if (value != (size_t)value) {
539 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
540 "exceeding address space");
541 return;
542 }
543
544 s->xbzrle_cache_size = xbzrle_cache_resize(value);
545}
546
547int64_t qmp_query_migrate_cache_size(Error **errp)
548{
549 return migrate_xbzrle_cache_size();
550}
551
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200552void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100553{
Juan Quintelacab30142011-02-22 23:54:21 +0100554 MigrationState *s;
555
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200556 if (value < 0) {
557 value = 0;
Juan Quintelacab30142011-02-22 23:54:21 +0100558 }
Juan Quintelacab30142011-02-22 23:54:21 +0100559
Juan Quintela17549e82011-10-05 13:50:43 +0200560 s = migrate_get_current();
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200561 s->bandwidth_limit = value;
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100562 qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
Juan Quintelacab30142011-02-22 23:54:21 +0100563}
564
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200565void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100566{
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200567 value *= 1e9;
568 value = MAX(0, MIN(UINT64_MAX, value));
569 max_downtime = (uint64_t)value;
aliguori5bb79102008-10-13 03:12:02 +0000570}
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300571
572int migrate_use_xbzrle(void)
573{
574 MigrationState *s;
575
576 s = migrate_get_current();
577
578 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
579}
580
581int64_t migrate_xbzrle_cache_size(void)
582{
583 MigrationState *s;
584
585 s = migrate_get_current();
586
587 return s->xbzrle_cache_size;
588}