]> git.proxmox.com Git - qemu.git/blame - migration.c
update VERSION for 1.1.2
[qemu.git] / migration.c
CommitLineData
5bb7910a
AL
1/*
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 *
6b620ca3
PB
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
5bb7910a
AL
14 */
15
16#include "qemu-common.h"
17#include "migration.h"
376253ec 18#include "monitor.h"
065e2813
AL
19#include "buffered_file.h"
20#include "sysemu.h"
21#include "block.h"
22#include "qemu_socket.h"
25f23643 23#include "block-migration.h"
791e7c82 24#include "qmp-commands.h"
065e2813
AL
25
26//#define DEBUG_MIGRATION
27
28#ifdef DEBUG_MIGRATION
d0f2c4c6 29#define DPRINTF(fmt, ...) \
065e2813
AL
30 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
31#else
d0f2c4c6 32#define DPRINTF(fmt, ...) \
065e2813
AL
33 do { } while (0)
34#endif
5bb7910a 35
7dc688ed
JQ
36enum {
37 MIG_STATE_ERROR,
38 MIG_STATE_SETUP,
39 MIG_STATE_CANCELLED,
40 MIG_STATE_ACTIVE,
41 MIG_STATE_COMPLETED,
42};
5bb7910a 43
d0ae46c1 44#define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
5bb7910a 45
99a0db9b
GH
46static NotifierList migration_state_notifiers =
47 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
48
17549e84
JQ
49/* 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,
d0ae46c1 57 .bandwidth_limit = MAX_THROTTLE,
17549e84
JQ
58 };
59
60 return &current_migration;
61}
62
d5c5dacc 63int qemu_start_incoming_migration(const char *uri, Error **errp)
5bb7910a 64{
34c9dd8e 65 const char *p;
8ca5e801 66 int ret;
34c9dd8e
AL
67
68 if (strstart(uri, "tcp:", &p))
d5c5dacc 69 ret = tcp_start_incoming_migration(p, errp);
065e2813
AL
70#if !defined(WIN32)
71 else if (strstart(uri, "exec:", &p))
8ca5e801 72 ret = exec_start_incoming_migration(p);
4951f65b 73 else if (strstart(uri, "unix:", &p))
8ca5e801 74 ret = unix_start_incoming_migration(p);
5ac1fad3 75 else if (strstart(uri, "fd:", &p))
8ca5e801 76 ret = fd_start_incoming_migration(p);
065e2813 77#endif
8ca5e801 78 else {
34c9dd8e 79 fprintf(stderr, "unknown migration protocol: %s\n", uri);
8ca5e801
JQ
80 ret = -EPROTONOSUPPORT;
81 }
82 return ret;
5bb7910a
AL
83}
84
511c0231
JQ
85void 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
901862cb 94 bdrv_clear_incoming_migration_all();
0f15423c
AL
95 /* Make sure all file formats flush their mutable metadata */
96 bdrv_invalidate_cache_all();
97
f5bbfba1 98 if (autostart) {
511c0231 99 vm_start();
f5bbfba1 100 } else {
0461d5a6 101 runstate_set(RUN_STATE_PRELAUNCH);
f5bbfba1 102 }
511c0231
JQ
103}
104
a0a3fd60
GC
105/* 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
791e7c82 116MigrationInfo *qmp_query_migrate(Error **errp)
5bb7910a 117{
791e7c82 118 MigrationInfo *info = g_malloc0(sizeof(*info));
17549e84
JQ
119 MigrationState *s = migrate_get_current();
120
121 switch (s->state) {
122 case MIG_STATE_SETUP:
123 /* no migration has happened ever */
124 break;
125 case MIG_STATE_ACTIVE:
791e7c82
LC
126 info->has_status = true;
127 info->status = g_strdup("active");
17549e84 128
791e7c82
LC
129 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();
17549e84
JQ
134
135 if (blk_mig_active()) {
791e7c82
LC
136 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();
ff8d81d8 141 }
17549e84
JQ
142 break;
143 case MIG_STATE_COMPLETED:
791e7c82
LC
144 info->has_status = true;
145 info->status = g_strdup("completed");
17549e84
JQ
146 break;
147 case MIG_STATE_ERROR:
791e7c82
LC
148 info->has_status = true;
149 info->status = g_strdup("failed");
17549e84
JQ
150 break;
151 case MIG_STATE_CANCELLED:
791e7c82
LC
152 info->has_status = true;
153 info->status = g_strdup("cancelled");
17549e84 154 break;
5bb7910a 155 }
791e7c82
LC
156
157 return info;
5bb7910a
AL
158}
159
065e2813
AL
160/* shared migration helpers */
161
8b6b99b3 162static int migrate_fd_cleanup(MigrationState *s)
065e2813 163{
41ef56e6
AL
164 int ret = 0;
165
065e2813
AL
166 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
167
168 if (s->file) {
d0f2c4c6 169 DPRINTF("closing file\n");
a6d34a94 170 ret = qemu_fclose(s->file);
5d39c799 171 s->file = NULL;
065e2813
AL
172 }
173
84ec6552 174 if (s->fd != -1) {
065e2813 175 close(s->fd);
84ec6552 176 s->fd = -1;
f327aa0c 177 }
065e2813 178
41ef56e6 179 return ret;
065e2813
AL
180}
181
8b6b99b3 182void migrate_fd_error(MigrationState *s)
065e2813 183{
8b6b99b3
JQ
184 DPRINTF("setting error state\n");
185 s->state = MIG_STATE_ERROR;
e0eb7390 186 notifier_list_notify(&migration_state_notifiers, s);
8b6b99b3
JQ
187 migrate_fd_cleanup(s);
188}
189
458cf28e
JQ
190static 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 }
e0eb7390 199 notifier_list_notify(&migration_state_notifiers, s);
458cf28e
JQ
200}
201
8b6b99b3 202static void migrate_fd_put_notify(void *opaque)
065e2813 203{
22f00a44 204 MigrationState *s = opaque;
065e2813
AL
205
206 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
207 qemu_file_put_notify(s->file);
1fdc11c3 208 if (s->file && qemu_file_get_error(s->file)) {
2350e13c
YT
209 migrate_fd_error(s);
210 }
065e2813
AL
211}
212
8b6b99b3
JQ
213static ssize_t migrate_fd_put_buffer(void *opaque, const void *data,
214 size_t size)
065e2813 215{
22f00a44 216 MigrationState *s = opaque;
065e2813
AL
217 ssize_t ret;
218
fdbecb5d
JQ
219 if (s->state != MIG_STATE_ACTIVE) {
220 return -EIO;
221 }
222
065e2813
AL
223 do {
224 ret = s->write(s, data, size);
95b134ea 225 } while (ret == -1 && ((s->get_error(s)) == EINTR));
065e2813
AL
226
227 if (ret == -1)
228 ret = -(s->get_error(s));
229
e447b1a6 230 if (ret == -EAGAIN) {
065e2813 231 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
e447b1a6 232 }
065e2813
AL
233
234 return ret;
235}
236
8b6b99b3 237static void migrate_fd_put_ready(void *opaque)
065e2813 238{
22f00a44 239 MigrationState *s = opaque;
065e2813
AL
240 int ret;
241
065e2813 242 if (s->state != MIG_STATE_ACTIVE) {
d0f2c4c6 243 DPRINTF("put_ready returning because of non-active state\n");
065e2813
AL
244 return;
245 }
246
d0f2c4c6 247 DPRINTF("iterate\n");
539de124 248 ret = qemu_savevm_state_iterate(s->file);
39346385
JQ
249 if (ret < 0) {
250 migrate_fd_error(s);
251 } else if (ret == 1) {
1354869c 252 int old_vm_running = runstate_is_running();
eeb34af9 253
d0f2c4c6 254 DPRINTF("done iterating\n");
7b5d3aa2 255 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
8a9236f1 256 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
065e2813 257
539de124 258 if (qemu_savevm_state_complete(s->file) < 0) {
67afff79 259 migrate_fd_error(s);
b161d123 260 } else {
458cf28e 261 migrate_fd_completed(s);
b161d123 262 }
48a2f4d6 263 if (s->state != MIG_STATE_COMPLETED) {
41ef56e6
AL
264 if (old_vm_running) {
265 vm_start();
266 }
f5bbfba1 267 }
065e2813
AL
268 }
269}
270
0edda1c4 271static void migrate_fd_cancel(MigrationState *s)
065e2813 272{
065e2813
AL
273 if (s->state != MIG_STATE_ACTIVE)
274 return;
275
d0f2c4c6 276 DPRINTF("cancelling migration\n");
065e2813
AL
277
278 s->state = MIG_STATE_CANCELLED;
e0eb7390 279 notifier_list_notify(&migration_state_notifiers, s);
539de124 280 qemu_savevm_state_cancel(s->file);
065e2813
AL
281
282 migrate_fd_cleanup(s);
283}
284
8b6b99b3 285static void migrate_fd_wait_for_unfreeze(void *opaque)
065e2813 286{
22f00a44 287 MigrationState *s = opaque;
065e2813
AL
288 int ret;
289
d0f2c4c6 290 DPRINTF("wait for unfreeze\n");
065e2813
AL
291 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);
af509450
JQ
302
303 if (ret == -1) {
dcd1d224 304 qemu_file_set_error(s->file, -s->get_error(s));
af509450 305 }
065e2813
AL
306}
307
8b6b99b3 308static int migrate_fd_close(void *opaque)
065e2813 309{
22f00a44 310 MigrationState *s = opaque;
e19252d3
UL
311
312 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
065e2813
AL
313 return s->close(s);
314}
99a0db9b
GH
315
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{
31552529 323 notifier_remove(notify);
99a0db9b
GH
324}
325
afe2df69
GH
326bool migration_is_active(MigrationState *s)
327{
328 return s->state == MIG_STATE_ACTIVE;
329}
330
7073693b 331bool migration_has_finished(MigrationState *s)
99a0db9b 332{
7073693b 333 return s->state == MIG_STATE_COMPLETED;
99a0db9b 334}
0edda1c4 335
afe2df69
GH
336bool migration_has_failed(MigrationState *s)
337{
338 return (s->state == MIG_STATE_CANCELLED ||
339 s->state == MIG_STATE_ERROR);
340}
341
8b6b99b3 342void migrate_fd_connect(MigrationState *s)
99a0db9b 343{
8b6b99b3
JQ
344 int ret;
345
d5934dde 346 s->state = MIG_STATE_ACTIVE;
8b6b99b3
JQ
347 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");
539de124 355 ret = qemu_savevm_state_begin(s->file, s->blk, s->shared);
8b6b99b3
JQ
356 if (ret < 0) {
357 DPRINTF("failed, %d\n", ret);
358 migrate_fd_error(s);
359 return;
360 }
361 migrate_fd_put_ready(s);
362}
363
e1c37d0e 364static MigrationState *migrate_init(int blk, int inc)
0edda1c4 365{
17549e84 366 MigrationState *s = migrate_get_current();
d0ae46c1 367 int64_t bandwidth_limit = s->bandwidth_limit;
0edda1c4 368
17549e84 369 memset(s, 0, sizeof(*s));
d0ae46c1 370 s->bandwidth_limit = bandwidth_limit;
0edda1c4
JQ
371 s->blk = blk;
372 s->shared = inc;
1299c631 373
0edda1c4 374 s->bandwidth_limit = bandwidth_limit;
d5934dde 375 s->state = MIG_STATE_SETUP;
0edda1c4 376
0edda1c4
JQ
377 return s;
378}
cab30143 379
fa2756b7
AL
380static 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
e1c37d0e
LC
392void qmp_migrate(const char *uri, bool has_blk, bool blk,
393 bool has_inc, bool inc, bool has_detach, bool detach,
394 Error **errp)
cab30143 395{
17549e84 396 MigrationState *s = migrate_get_current();
cab30143 397 const char *p;
cab30143
JQ
398 int ret;
399
17549e84 400 if (s->state == MIG_STATE_ACTIVE) {
e1c37d0e
LC
401 error_set(errp, QERR_MIGRATION_ACTIVE);
402 return;
cab30143
JQ
403 }
404
e1c37d0e
LC
405 if (qemu_savevm_state_blocked(errp)) {
406 return;
cab30143
JQ
407 }
408
fa2756b7 409 if (migration_blockers) {
e1c37d0e
LC
410 *errp = error_copy(migration_blockers->data);
411 return;
fa2756b7
AL
412 }
413
e1c37d0e 414 s = migrate_init(blk, inc);
cab30143
JQ
415
416 if (strstart(uri, "tcp:", &p)) {
d5c5dacc 417 ret = tcp_start_outgoing_migration(s, p, errp);
cab30143
JQ
418#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
99a0db9b 426 } else {
e1c37d0e
LC
427 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
428 return;
cab30143
JQ
429 }
430
431 if (ret < 0) {
d5c5dacc
AK
432 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 }
e1c37d0e 437 return;
1299c631
JQ
438 }
439
e0eb7390 440 notifier_list_notify(&migration_state_notifiers, s);
cab30143
JQ
441}
442
6cdedb07 443void qmp_migrate_cancel(Error **errp)
cab30143 444{
17549e84 445 migrate_fd_cancel(migrate_get_current());
cab30143
JQ
446}
447
3dc85383 448void qmp_migrate_set_speed(int64_t value, Error **errp)
cab30143 449{
cab30143
JQ
450 MigrationState *s;
451
3dc85383
LC
452 if (value < 0) {
453 value = 0;
99a0db9b 454 }
cab30143 455
17549e84 456 s = migrate_get_current();
3dc85383 457 s->bandwidth_limit = value;
d0ae46c1 458 qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
cab30143
JQ
459}
460
4f0a993b 461void qmp_migrate_set_downtime(double value, Error **errp)
cab30143 462{
4f0a993b
LC
463 value *= 1e9;
464 value = MAX(0, MIN(UINT64_MAX, value));
465 max_downtime = (uint64_t)value;
99a0db9b 466}