blob: 3f485d33a57513209fcef41e46bd5aa66f858a59 [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
Gerd Hoffmann99a0db92010-12-13 17:30:12 +010046static NotifierList migration_state_notifiers =
47 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
48
Juan Quintela17549e82011-10-05 13:50:43 +020049/* When we add fault tolerance, we could have several
50 migrations at once. For now we don't need to add
51 dynamic creation of migration */
52
53static MigrationState *migrate_get_current(void)
54{
55 static MigrationState current_migration = {
56 .state = MIG_STATE_SETUP,
Juan Quintelad0ae46c2011-02-23 00:33:19 +010057 .bandwidth_limit = MAX_THROTTLE,
Juan Quintela17549e82011-10-05 13:50:43 +020058 };
59
60 return &current_migration;
61}
62
Amos Kongd5c5dac2012-05-11 00:28:35 +080063int qemu_start_incoming_migration(const char *uri, Error **errp)
aliguori5bb79102008-10-13 03:12:02 +000064{
aliguori34c9dd82008-10-13 03:14:31 +000065 const char *p;
Juan Quintela8ca5e802010-06-09 14:10:54 +020066 int ret;
aliguori34c9dd82008-10-13 03:14:31 +000067
68 if (strstart(uri, "tcp:", &p))
Amos Kongd5c5dac2012-05-11 00:28:35 +080069 ret = tcp_start_incoming_migration(p, errp);
aliguori065e2812008-11-11 16:46:33 +000070#if !defined(WIN32)
71 else if (strstart(uri, "exec:", &p))
Juan Quintela8ca5e802010-06-09 14:10:54 +020072 ret = exec_start_incoming_migration(p);
Chris Lalancette4951f652009-08-05 17:24:29 +020073 else if (strstart(uri, "unix:", &p))
Juan Quintela8ca5e802010-06-09 14:10:54 +020074 ret = unix_start_incoming_migration(p);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +020075 else if (strstart(uri, "fd:", &p))
Juan Quintela8ca5e802010-06-09 14:10:54 +020076 ret = fd_start_incoming_migration(p);
aliguori065e2812008-11-11 16:46:33 +000077#endif
Juan Quintela8ca5e802010-06-09 14:10:54 +020078 else {
aliguori34c9dd82008-10-13 03:14:31 +000079 fprintf(stderr, "unknown migration protocol: %s\n", uri);
Juan Quintela8ca5e802010-06-09 14:10:54 +020080 ret = -EPROTONOSUPPORT;
81 }
82 return ret;
aliguori5bb79102008-10-13 03:12:02 +000083}
84
Juan Quintela511c0232010-06-09 14:10:55 +020085void process_incoming_migration(QEMUFile *f)
86{
87 if (qemu_loadvm_state(f) < 0) {
88 fprintf(stderr, "load of migration failed\n");
89 exit(0);
90 }
91 qemu_announce_self();
92 DPRINTF("successfully loaded vm state\n");
93
BenoƮt Canet901862c2012-03-23 08:36:52 +010094 bdrv_clear_incoming_migration_all();
Anthony Liguori0f154232011-11-14 15:09:45 -060095 /* Make sure all file formats flush their mutable metadata */
96 bdrv_invalidate_cache_all();
97
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -030098 if (autostart) {
Juan Quintela511c0232010-06-09 14:10:55 +020099 vm_start();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300100 } else {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300101 runstate_set(RUN_STATE_PRELAUNCH);
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300102 }
Juan Quintela511c0232010-06-09 14:10:55 +0200103}
104
Glauber Costaa0a3fd62009-05-28 15:22:57 -0400105/* amount of nanoseconds we are willing to wait for migration to be down.
106 * the choice of nanoseconds is because it is the maximum resolution that
107 * get_clock() can achieve. It is an internal measure. All user-visible
108 * units must be in seconds */
109static uint64_t max_downtime = 30000000;
110
111uint64_t migrate_max_downtime(void)
112{
113 return max_downtime;
114}
115
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300116MigrationInfo *qmp_query_migrate(Error **errp)
aliguori5bb79102008-10-13 03:12:02 +0000117{
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300118 MigrationInfo *info = g_malloc0(sizeof(*info));
Juan Quintela17549e82011-10-05 13:50:43 +0200119 MigrationState *s = migrate_get_current();
aliguori376253e2009-03-05 23:01:23 +0000120
Juan Quintela17549e82011-10-05 13:50:43 +0200121 switch (s->state) {
122 case MIG_STATE_SETUP:
123 /* no migration has happened ever */
124 break;
125 case MIG_STATE_ACTIVE:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300126 info->has_status = true;
127 info->status = g_strdup("active");
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200128
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300129 info->has_ram = true;
130 info->ram = g_malloc0(sizeof(*info->ram));
131 info->ram->transferred = ram_bytes_transferred();
132 info->ram->remaining = ram_bytes_remaining();
133 info->ram->total = ram_bytes_total();
Luiz Capitulinoc86a6682009-12-10 17:16:05 -0200134
Juan Quintela17549e82011-10-05 13:50:43 +0200135 if (blk_mig_active()) {
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300136 info->has_disk = true;
137 info->disk = g_malloc0(sizeof(*info->disk));
138 info->disk->transferred = blk_mig_bytes_transferred();
139 info->disk->remaining = blk_mig_bytes_remaining();
140 info->disk->total = blk_mig_bytes_total();
aliguoriff8d81d2008-10-24 22:10:31 +0000141 }
Juan Quintela17549e82011-10-05 13:50:43 +0200142 break;
143 case MIG_STATE_COMPLETED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300144 info->has_status = true;
145 info->status = g_strdup("completed");
Juan Quintela17549e82011-10-05 13:50:43 +0200146 break;
147 case MIG_STATE_ERROR:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300148 info->has_status = true;
149 info->status = g_strdup("failed");
Juan Quintela17549e82011-10-05 13:50:43 +0200150 break;
151 case MIG_STATE_CANCELLED:
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300152 info->has_status = true;
153 info->status = g_strdup("cancelled");
Juan Quintela17549e82011-10-05 13:50:43 +0200154 break;
aliguori5bb79102008-10-13 03:12:02 +0000155 }
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300156
157 return info;
aliguori5bb79102008-10-13 03:12:02 +0000158}
159
aliguori065e2812008-11-11 16:46:33 +0000160/* shared migration helpers */
161
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200162static int migrate_fd_cleanup(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000163{
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500164 int ret = 0;
165
aliguori065e2812008-11-11 16:46:33 +0000166 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
167
168 if (s->file) {
malcd0f2c4c2010-02-07 02:03:50 +0300169 DPRINTF("closing file\n");
Eduardo Habkosta6d34a92011-11-10 10:41:42 -0200170 ret = qemu_fclose(s->file);
Jan Kiszka5d39c792009-11-30 18:21:19 +0100171 s->file = NULL;
aliguori065e2812008-11-11 16:46:33 +0000172 }
173
Jan Kiszka84ec6552011-08-05 09:11:26 +0200174 if (s->fd != -1) {
aliguori065e2812008-11-11 16:46:33 +0000175 close(s->fd);
Jan Kiszka84ec6552011-08-05 09:11:26 +0200176 s->fd = -1;
Jan Kiszkaf327aa02009-11-30 18:21:21 +0100177 }
aliguori065e2812008-11-11 16:46:33 +0000178
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500179 return ret;
aliguori065e2812008-11-11 16:46:33 +0000180}
181
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200182void migrate_fd_error(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000183{
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200184 DPRINTF("setting error state\n");
185 s->state = MIG_STATE_ERROR;
Juan Quintelae0eb7392011-10-05 14:27:52 +0200186 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200187 migrate_fd_cleanup(s);
188}
189
Juan Quintela458cf282011-02-22 23:32:54 +0100190static void migrate_fd_completed(MigrationState *s)
191{
192 DPRINTF("setting completed state\n");
193 if (migrate_fd_cleanup(s) < 0) {
194 s->state = MIG_STATE_ERROR;
195 } else {
196 s->state = MIG_STATE_COMPLETED;
197 runstate_set(RUN_STATE_POSTMIGRATE);
198 }
Juan Quintelae0eb7392011-10-05 14:27:52 +0200199 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintela458cf282011-02-22 23:32:54 +0100200}
201
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200202static void migrate_fd_put_notify(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000203{
Juan Quintela22f00a42010-05-11 15:56:35 +0200204 MigrationState *s = opaque;
aliguori065e2812008-11-11 16:46:33 +0000205
206 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
207 qemu_file_put_notify(s->file);
Luiz Capitulino1fdc11c2011-10-28 14:59:52 -0200208 if (s->file && qemu_file_get_error(s->file)) {
Yoshiaki Tamura2350e132011-02-23 00:01:24 +0900209 migrate_fd_error(s);
210 }
aliguori065e2812008-11-11 16:46:33 +0000211}
212
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200213static ssize_t migrate_fd_put_buffer(void *opaque, const void *data,
214 size_t size)
aliguori065e2812008-11-11 16:46:33 +0000215{
Juan Quintela22f00a42010-05-11 15:56:35 +0200216 MigrationState *s = opaque;
aliguori065e2812008-11-11 16:46:33 +0000217 ssize_t ret;
218
Juan Quintelafdbecb52011-09-21 22:37:29 +0200219 if (s->state != MIG_STATE_ACTIVE) {
220 return -EIO;
221 }
222
aliguori065e2812008-11-11 16:46:33 +0000223 do {
224 ret = s->write(s, data, size);
Uri Lublin95b134e2009-05-19 14:08:53 +0300225 } while (ret == -1 && ((s->get_error(s)) == EINTR));
aliguori065e2812008-11-11 16:46:33 +0000226
227 if (ret == -1)
228 ret = -(s->get_error(s));
229
Marcelo Tosattie447b1a2010-08-19 10:18:39 -0300230 if (ret == -EAGAIN) {
aliguori065e2812008-11-11 16:46:33 +0000231 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
Marcelo Tosattie447b1a2010-08-19 10:18:39 -0300232 }
aliguori065e2812008-11-11 16:46:33 +0000233
234 return ret;
235}
236
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200237static void migrate_fd_put_ready(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000238{
Juan Quintela22f00a42010-05-11 15:56:35 +0200239 MigrationState *s = opaque;
aliguori065e2812008-11-11 16:46:33 +0000240 int ret;
241
aliguori065e2812008-11-11 16:46:33 +0000242 if (s->state != MIG_STATE_ACTIVE) {
malcd0f2c4c2010-02-07 02:03:50 +0300243 DPRINTF("put_ready returning because of non-active state\n");
aliguori065e2812008-11-11 16:46:33 +0000244 return;
245 }
246
malcd0f2c4c2010-02-07 02:03:50 +0300247 DPRINTF("iterate\n");
Luiz Capitulino539de122011-12-05 14:06:56 -0200248 ret = qemu_savevm_state_iterate(s->file);
Juan Quintela39346382011-09-22 11:02:14 +0200249 if (ret < 0) {
250 migrate_fd_error(s);
251 } else if (ret == 1) {
Luiz Capitulino13548692011-07-29 15:36:43 -0300252 int old_vm_running = runstate_is_running();
Anthony Liguorieeb34af2009-07-09 13:25:47 -0500253
malcd0f2c4c2010-02-07 02:03:50 +0300254 DPRINTF("done iterating\n");
Gerd Hoffmann7b5d3aa2012-03-07 08:00:26 +0100255 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300256 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
aliguori065e2812008-11-11 16:46:33 +0000257
Luiz Capitulino539de122011-12-05 14:06:56 -0200258 if (qemu_savevm_state_complete(s->file) < 0) {
Juan Quintela67afff72011-02-22 23:18:20 +0100259 migrate_fd_error(s);
aliguorib161d122009-04-05 19:30:33 +0000260 } else {
Juan Quintela458cf282011-02-22 23:32:54 +0100261 migrate_fd_completed(s);
aliguorib161d122009-04-05 19:30:33 +0000262 }
Juan Quintela48a2f4d2010-05-11 23:28:53 +0200263 if (s->state != MIG_STATE_COMPLETED) {
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500264 if (old_vm_running) {
265 vm_start();
266 }
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500267 }
aliguori065e2812008-11-11 16:46:33 +0000268 }
269}
270
Juan Quintela0edda1c2010-05-11 16:28:39 +0200271static void migrate_fd_cancel(MigrationState *s)
aliguori065e2812008-11-11 16:46:33 +0000272{
aliguori065e2812008-11-11 16:46:33 +0000273 if (s->state != MIG_STATE_ACTIVE)
274 return;
275
malcd0f2c4c2010-02-07 02:03:50 +0300276 DPRINTF("cancelling migration\n");
aliguori065e2812008-11-11 16:46:33 +0000277
278 s->state = MIG_STATE_CANCELLED;
Juan Quintelae0eb7392011-10-05 14:27:52 +0200279 notifier_list_notify(&migration_state_notifiers, s);
Luiz Capitulino539de122011-12-05 14:06:56 -0200280 qemu_savevm_state_cancel(s->file);
aliguori065e2812008-11-11 16:46:33 +0000281
282 migrate_fd_cleanup(s);
283}
284
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200285static void migrate_fd_wait_for_unfreeze(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000286{
Juan Quintela22f00a42010-05-11 15:56:35 +0200287 MigrationState *s = opaque;
aliguori065e2812008-11-11 16:46:33 +0000288 int ret;
289
malcd0f2c4c2010-02-07 02:03:50 +0300290 DPRINTF("wait for unfreeze\n");
aliguori065e2812008-11-11 16:46:33 +0000291 if (s->state != MIG_STATE_ACTIVE)
292 return;
293
294 do {
295 fd_set wfds;
296
297 FD_ZERO(&wfds);
298 FD_SET(s->fd, &wfds);
299
300 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
301 } while (ret == -1 && (s->get_error(s)) == EINTR);
Juan Quintelaaf509452011-09-21 22:46:36 +0200302
303 if (ret == -1) {
Juan Quinteladcd1d222011-09-21 23:01:54 +0200304 qemu_file_set_error(s->file, -s->get_error(s));
Juan Quintelaaf509452011-09-21 22:46:36 +0200305 }
aliguori065e2812008-11-11 16:46:33 +0000306}
307
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200308static int migrate_fd_close(void *opaque)
aliguori065e2812008-11-11 16:46:33 +0000309{
Juan Quintela22f00a42010-05-11 15:56:35 +0200310 MigrationState *s = opaque;
Uri Lubline19252d2009-06-08 14:28:01 +0300311
312 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
aliguori065e2812008-11-11 16:46:33 +0000313 return s->close(s);
314}
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100315
316void add_migration_state_change_notifier(Notifier *notify)
317{
318 notifier_list_add(&migration_state_notifiers, notify);
319}
320
321void remove_migration_state_change_notifier(Notifier *notify)
322{
Paolo Bonzini31552522012-01-13 17:34:01 +0100323 notifier_remove(notify);
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100324}
325
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200326bool migration_is_active(MigrationState *s)
327{
328 return s->state == MIG_STATE_ACTIVE;
329}
330
Juan Quintela70736932011-02-23 00:43:59 +0100331bool migration_has_finished(MigrationState *s)
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100332{
Juan Quintela70736932011-02-23 00:43:59 +0100333 return s->state == MIG_STATE_COMPLETED;
Gerd Hoffmann99a0db92010-12-13 17:30:12 +0100334}
Juan Quintela0edda1c2010-05-11 16:28:39 +0200335
Gerd Hoffmannafe2df62011-10-25 13:50:11 +0200336bool migration_has_failed(MigrationState *s)
337{
338 return (s->state == MIG_STATE_CANCELLED ||
339 s->state == MIG_STATE_ERROR);
340}
341
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200342void migrate_fd_connect(MigrationState *s)
343{
344 int ret;
345
Juan Quintelad5934dd2010-05-11 23:01:53 +0200346 s->state = MIG_STATE_ACTIVE;
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200347 s->file = qemu_fopen_ops_buffered(s,
348 s->bandwidth_limit,
349 migrate_fd_put_buffer,
350 migrate_fd_put_ready,
351 migrate_fd_wait_for_unfreeze,
352 migrate_fd_close);
353
354 DPRINTF("beginning savevm\n");
Luiz Capitulino539de122011-12-05 14:06:56 -0200355 ret = qemu_savevm_state_begin(s->file, s->blk, s->shared);
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200356 if (ret < 0) {
357 DPRINTF("failed, %d\n", ret);
358 migrate_fd_error(s);
359 return;
aliguori5bb79102008-10-13 03:12:02 +0000360 }
Juan Quintela8b6b99b2011-09-11 20:28:22 +0200361 migrate_fd_put_ready(s);
362}
363
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200364static MigrationState *migrate_init(int blk, int inc)
Juan Quintela0edda1c2010-05-11 16:28:39 +0200365{
Juan Quintela17549e82011-10-05 13:50:43 +0200366 MigrationState *s = migrate_get_current();
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100367 int64_t bandwidth_limit = s->bandwidth_limit;
Juan Quintela0edda1c2010-05-11 16:28:39 +0200368
Juan Quintela17549e82011-10-05 13:50:43 +0200369 memset(s, 0, sizeof(*s));
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100370 s->bandwidth_limit = bandwidth_limit;
Juan Quintela0edda1c2010-05-11 16:28:39 +0200371 s->blk = blk;
372 s->shared = inc;
Juan Quintela1299c632011-11-09 21:29:01 +0100373
Juan Quintela0edda1c2010-05-11 16:28:39 +0200374 s->bandwidth_limit = bandwidth_limit;
Juan Quintelad5934dd2010-05-11 23:01:53 +0200375 s->state = MIG_STATE_SETUP;
Juan Quintela0edda1c2010-05-11 16:28:39 +0200376
Juan Quintela0edda1c2010-05-11 16:28:39 +0200377 return s;
378}
Juan Quintelacab30142011-02-22 23:54:21 +0100379
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600380static GSList *migration_blockers;
381
382void migrate_add_blocker(Error *reason)
383{
384 migration_blockers = g_slist_prepend(migration_blockers, reason);
385}
386
387void migrate_del_blocker(Error *reason)
388{
389 migration_blockers = g_slist_remove(migration_blockers, reason);
390}
391
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200392void qmp_migrate(const char *uri, bool has_blk, bool blk,
393 bool has_inc, bool inc, bool has_detach, bool detach,
394 Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100395{
Juan Quintela17549e82011-10-05 13:50:43 +0200396 MigrationState *s = migrate_get_current();
Juan Quintelacab30142011-02-22 23:54:21 +0100397 const char *p;
Juan Quintelacab30142011-02-22 23:54:21 +0100398 int ret;
399
Juan Quintela17549e82011-10-05 13:50:43 +0200400 if (s->state == MIG_STATE_ACTIVE) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200401 error_set(errp, QERR_MIGRATION_ACTIVE);
402 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100403 }
404
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200405 if (qemu_savevm_state_blocked(errp)) {
406 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100407 }
408
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600409 if (migration_blockers) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200410 *errp = error_copy(migration_blockers->data);
411 return;
Anthony Liguorifa2756b2011-11-14 15:09:43 -0600412 }
413
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200414 s = migrate_init(blk, inc);
Juan Quintelacab30142011-02-22 23:54:21 +0100415
416 if (strstart(uri, "tcp:", &p)) {
Amos Kongd5c5dac2012-05-11 00:28:35 +0800417 ret = tcp_start_outgoing_migration(s, p, errp);
Juan Quintelacab30142011-02-22 23:54:21 +0100418#if !defined(WIN32)
419 } else if (strstart(uri, "exec:", &p)) {
420 ret = exec_start_outgoing_migration(s, p);
421 } else if (strstart(uri, "unix:", &p)) {
422 ret = unix_start_outgoing_migration(s, p);
423 } else if (strstart(uri, "fd:", &p)) {
424 ret = fd_start_outgoing_migration(s, p);
425#endif
426 } else {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200427 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
428 return;
Juan Quintelacab30142011-02-22 23:54:21 +0100429 }
430
431 if (ret < 0) {
Amos Kongd5c5dac2012-05-11 00:28:35 +0800432 if (!error_is_set(errp)) {
433 DPRINTF("migration failed: %s\n", strerror(-ret));
434 /* FIXME: we should return meaningful errors */
435 error_set(errp, QERR_UNDEFINED_ERROR);
436 }
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200437 return;
Juan Quintela1299c632011-11-09 21:29:01 +0100438 }
439
Juan Quintelae0eb7392011-10-05 14:27:52 +0200440 notifier_list_notify(&migration_state_notifiers, s);
Juan Quintelacab30142011-02-22 23:54:21 +0100441}
442
Luiz Capitulino6cdedb02011-11-27 22:54:09 -0200443void qmp_migrate_cancel(Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100444{
Juan Quintela17549e82011-10-05 13:50:43 +0200445 migrate_fd_cancel(migrate_get_current());
Juan Quintelacab30142011-02-22 23:54:21 +0100446}
447
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200448void qmp_migrate_set_speed(int64_t value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100449{
Juan Quintelacab30142011-02-22 23:54:21 +0100450 MigrationState *s;
451
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200452 if (value < 0) {
453 value = 0;
Juan Quintelacab30142011-02-22 23:54:21 +0100454 }
Juan Quintelacab30142011-02-22 23:54:21 +0100455
Juan Quintela17549e82011-10-05 13:50:43 +0200456 s = migrate_get_current();
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200457 s->bandwidth_limit = value;
Juan Quintelad0ae46c2011-02-23 00:33:19 +0100458 qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
Juan Quintelacab30142011-02-22 23:54:21 +0100459}
460
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200461void qmp_migrate_set_downtime(double value, Error **errp)
Juan Quintelacab30142011-02-22 23:54:21 +0100462{
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200463 value *= 1e9;
464 value = MAX(0, MIN(UINT64_MAX, value));
465 max_downtime = (uint64_t)value;
aliguori5bb79102008-10-13 03:12:02 +0000466}