]> git.proxmox.com Git - qemu.git/blame - migration.c
LAN9118 improvements
[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 *
12 */
13
14#include "qemu-common.h"
15#include "migration.h"
376253ec 16#include "monitor.h"
065e2813
AL
17#include "buffered_file.h"
18#include "sysemu.h"
19#include "block.h"
20#include "qemu_socket.h"
25f23643 21#include "block-migration.h"
c86a6683 22#include "qemu-objects.h"
065e2813
AL
23
24//#define DEBUG_MIGRATION
25
26#ifdef DEBUG_MIGRATION
27#define dprintf(fmt, ...) \
28 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
29#else
30#define dprintf(fmt, ...) \
31 do { } while (0)
32#endif
5bb7910a
AL
33
34/* Migration speed throttling */
35static uint32_t max_throttle = (32 << 20);
36
37static MigrationState *current_migration;
38
39void qemu_start_incoming_migration(const char *uri)
40{
34c9dd8e
AL
41 const char *p;
42
43 if (strstart(uri, "tcp:", &p))
44 tcp_start_incoming_migration(p);
065e2813
AL
45#if !defined(WIN32)
46 else if (strstart(uri, "exec:", &p))
47 exec_start_incoming_migration(p);
4951f65b
CL
48 else if (strstart(uri, "unix:", &p))
49 unix_start_incoming_migration(p);
5ac1fad3
PB
50 else if (strstart(uri, "fd:", &p))
51 fd_start_incoming_migration(p);
065e2813 52#endif
34c9dd8e
AL
53 else
54 fprintf(stderr, "unknown migration protocol: %s\n", uri);
5bb7910a
AL
55}
56
5f79da00 57void do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
5bb7910a 58{
34c9dd8e
AL
59 MigrationState *s = NULL;
60 const char *p;
f18c16de
LC
61 int detach = qdict_get_int(qdict, "detach");
62 const char *uri = qdict_get_str(qdict, "uri");
1302425d
JK
63
64 if (current_migration &&
65 current_migration->get_status(current_migration) == MIG_STATE_ACTIVE) {
66 monitor_printf(mon, "migration already in progress\n");
67 return;
68 }
69
34c9dd8e 70 if (strstart(uri, "tcp:", &p))
f327aa0c 71 s = tcp_start_outgoing_migration(mon, p, max_throttle, detach,
c163b5ca 72 (int)qdict_get_int(qdict, "blk"),
73 (int)qdict_get_int(qdict, "inc"));
065e2813
AL
74#if !defined(WIN32)
75 else if (strstart(uri, "exec:", &p))
f327aa0c 76 s = exec_start_outgoing_migration(mon, p, max_throttle, detach,
c163b5ca 77 (int)qdict_get_int(qdict, "blk"),
78 (int)qdict_get_int(qdict, "inc"));
4951f65b 79 else if (strstart(uri, "unix:", &p))
f327aa0c 80 s = unix_start_outgoing_migration(mon, p, max_throttle, detach,
c163b5ca 81 (int)qdict_get_int(qdict, "blk"),
82 (int)qdict_get_int(qdict, "inc"));
5ac1fad3 83 else if (strstart(uri, "fd:", &p))
c163b5ca 84 s = fd_start_outgoing_migration(mon, p, max_throttle, detach,
85 (int)qdict_get_int(qdict, "blk"),
86 (int)qdict_get_int(qdict, "inc"));
065e2813 87#endif
34c9dd8e 88 else
376253ec 89 monitor_printf(mon, "unknown migration protocol: %s\n", uri);
34c9dd8e
AL
90
91 if (s == NULL)
376253ec 92 monitor_printf(mon, "migration failed\n");
34c9dd8e 93 else {
ff8d81d8
AL
94 if (current_migration)
95 current_migration->release(current_migration);
34c9dd8e 96
ff8d81d8 97 current_migration = s;
34c9dd8e 98 }
5bb7910a
AL
99}
100
911d2963 101void do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data)
5bb7910a
AL
102{
103 MigrationState *s = current_migration;
104
105 if (s)
ff8d81d8 106 s->cancel(s);
5bb7910a
AL
107}
108
13232b8f 109void do_migrate_set_speed(Monitor *mon, const QDict *qdict)
5bb7910a
AL
110{
111 double d;
112 char *ptr;
daa91de2 113 FdMigrationState *s;
d54908a5 114 const char *value = qdict_get_str(qdict, "value");
5bb7910a
AL
115
116 d = strtod(value, &ptr);
117 switch (*ptr) {
118 case 'G': case 'g':
ff8d81d8 119 d *= 1024;
5bb7910a 120 case 'M': case 'm':
ff8d81d8 121 d *= 1024;
5bb7910a 122 case 'K': case 'k':
ff8d81d8 123 d *= 1024;
5bb7910a 124 default:
ff8d81d8 125 break;
5bb7910a
AL
126 }
127
128 max_throttle = (uint32_t)d;
daa91de2 129
5d39c799
JK
130 s = migrate_to_fms(current_migration);
131 if (s && s->file) {
daa91de2
GC
132 qemu_file_set_rate_limit(s->file, max_throttle);
133 }
5bb7910a
AL
134}
135
a0a3fd60
GC
136/* amount of nanoseconds we are willing to wait for migration to be down.
137 * the choice of nanoseconds is because it is the maximum resolution that
138 * get_clock() can achieve. It is an internal measure. All user-visible
139 * units must be in seconds */
140static uint64_t max_downtime = 30000000;
141
142uint64_t migrate_max_downtime(void)
143{
144 return max_downtime;
145}
146
d54908a5 147void do_migrate_set_downtime(Monitor *mon, const QDict *qdict)
2ea42952
GC
148{
149 char *ptr;
150 double d;
d54908a5 151 const char *value = qdict_get_str(qdict, "value");
2ea42952
GC
152
153 d = strtod(value, &ptr);
154 if (!strcmp(ptr,"ms")) {
155 d *= 1000000;
156 } else if (!strcmp(ptr,"us")) {
157 d *= 1000;
158 } else if (!strcmp(ptr,"ns")) {
159 } else {
160 /* all else considered to be seconds */
161 d *= 1000000000;
162 }
163
164 max_downtime = (uint64_t)d;
165}
166
c86a6683
LC
167static void migrate_print_status(Monitor *mon, const char *name,
168 const QDict *status_dict)
5bb7910a 169{
c86a6683
LC
170 QDict *qdict;
171
172 qdict = qobject_to_qdict(qdict_get(status_dict, name));
173
174 monitor_printf(mon, "transferred %s: %" PRIu64 " kbytes\n", name,
175 qdict_get_int(qdict, "transferred") >> 10);
176 monitor_printf(mon, "remaining %s: %" PRIu64 " kbytes\n", name,
177 qdict_get_int(qdict, "remaining") >> 10);
178 monitor_printf(mon, "total %s: %" PRIu64 " kbytes\n", name,
179 qdict_get_int(qdict, "total") >> 10);
180}
181
182void do_info_migrate_print(Monitor *mon, const QObject *data)
183{
184 QDict *qdict;
185
186 qdict = qobject_to_qdict(data);
187
188 monitor_printf(mon, "Migration status: %s\n",
189 qdict_get_str(qdict, "status"));
190
191 if (qdict_haskey(qdict, "ram")) {
192 migrate_print_status(mon, "ram", qdict);
193 }
194
195 if (qdict_haskey(qdict, "disk")) {
196 migrate_print_status(mon, "disk", qdict);
197 }
198}
199
200static void migrate_put_status(QDict *qdict, const char *name,
201 uint64_t trans, uint64_t rem, uint64_t total)
202{
203 QObject *obj;
204
205 obj = qobject_from_jsonf("{ 'transferred': %" PRId64 ", "
206 "'remaining': %" PRId64 ", "
207 "'total': %" PRId64 " }", trans, rem, total);
208 assert(obj != NULL);
209
210 qdict_put_obj(qdict, name, obj);
211}
212
213/**
214 * do_info_migrate(): Migration status
215 *
216 * Return a QDict. If migration is active there will be another
217 * QDict with RAM migration status and if block migration is active
218 * another one with block migration status.
219 *
220 * The main QDict contains the following:
221 *
222 * - "status": migration status
223 * - "ram": only present if "status" is "active", it is a QDict with the
224 * following RAM information (in bytes):
225 * - "transferred": amount transferred
226 * - "remaining": amount remaining
227 * - "total": total
228 * - "disk": only present if "status" is "active" and it is a block migration,
229 * it is a QDict with the following disk information (in bytes):
230 * - "transferred": amount transferred
231 * - "remaining": amount remaining
232 * - "total": total
233 *
234 * Examples:
235 *
236 * 1. Migration is "completed":
237 *
238 * { "status": "completed" }
239 *
240 * 2. Migration is "active" and it is not a block migration:
241 *
242 * { "status": "active",
243 * "ram": { "transferred": 123, "remaining": 123, "total": 246 } }
244 *
245 * 3. Migration is "active" and it is a block migration:
246 *
247 * { "status": "active",
248 * "ram": { "total": 1057024, "remaining": 1053304, "transferred": 3720 },
249 * "disk": { "total": 20971520, "remaining": 20880384, "transferred": 91136 }}
250 */
251void do_info_migrate(Monitor *mon, QObject **ret_data)
252{
253 QDict *qdict;
5bb7910a 254 MigrationState *s = current_migration;
376253ec 255
5bb7910a 256 if (s) {
ff8d81d8
AL
257 switch (s->get_status(s)) {
258 case MIG_STATE_ACTIVE:
c86a6683
LC
259 qdict = qdict_new();
260 qdict_put(qdict, "status", qstring_from_str("active"));
261
262 migrate_put_status(qdict, "ram", ram_bytes_transferred(),
263 ram_bytes_remaining(), ram_bytes_total());
264
25f23643 265 if (blk_mig_active()) {
c86a6683
LC
266 migrate_put_status(qdict, "disk", blk_mig_bytes_transferred(),
267 blk_mig_bytes_remaining(),
268 blk_mig_bytes_total());
25f23643 269 }
c86a6683
LC
270
271 *ret_data = QOBJECT(qdict);
ff8d81d8
AL
272 break;
273 case MIG_STATE_COMPLETED:
c86a6683 274 *ret_data = qobject_from_jsonf("{ 'status': 'completed' }");
ff8d81d8
AL
275 break;
276 case MIG_STATE_ERROR:
c86a6683 277 *ret_data = qobject_from_jsonf("{ 'status': 'failed' }");
ff8d81d8
AL
278 break;
279 case MIG_STATE_CANCELLED:
c86a6683 280 *ret_data = qobject_from_jsonf("{ 'status': 'cancelled' }");
ff8d81d8
AL
281 break;
282 }
c86a6683 283 assert(*ret_data != NULL);
5bb7910a
AL
284 }
285}
286
065e2813
AL
287/* shared migration helpers */
288
f327aa0c 289void migrate_fd_monitor_suspend(FdMigrationState *s, Monitor *mon)
731b0364 290{
f327aa0c
JK
291 s->mon = mon;
292 if (monitor_suspend(mon) == 0) {
cde76ee1 293 dprintf("suspending monitor\n");
f327aa0c
JK
294 } else {
295 monitor_printf(mon, "terminal does not allow synchronous "
cde76ee1 296 "migration, continuing detached\n");
f327aa0c 297 }
731b0364
AL
298}
299
065e2813
AL
300void migrate_fd_error(FdMigrationState *s)
301{
302 dprintf("setting error state\n");
303 s->state = MIG_STATE_ERROR;
304 migrate_fd_cleanup(s);
305}
306
307void migrate_fd_cleanup(FdMigrationState *s)
308{
309 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
310
311 if (s->file) {
312 dprintf("closing file\n");
313 qemu_fclose(s->file);
5d39c799 314 s->file = NULL;
065e2813
AL
315 }
316
317 if (s->fd != -1)
318 close(s->fd);
319
320 /* Don't resume monitor until we've flushed all of the buffers */
f327aa0c
JK
321 if (s->mon) {
322 monitor_resume(s->mon);
323 }
065e2813
AL
324
325 s->fd = -1;
326}
327
328void migrate_fd_put_notify(void *opaque)
329{
330 FdMigrationState *s = opaque;
331
332 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
333 qemu_file_put_notify(s->file);
334}
335
336ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size)
337{
338 FdMigrationState *s = opaque;
339 ssize_t ret;
340
341 do {
342 ret = s->write(s, data, size);
95b134ea 343 } while (ret == -1 && ((s->get_error(s)) == EINTR));
065e2813
AL
344
345 if (ret == -1)
346 ret = -(s->get_error(s));
347
348 if (ret == -EAGAIN)
349 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
350
351 return ret;
352}
353
354void migrate_fd_connect(FdMigrationState *s)
355{
356 int ret;
357
358 s->file = qemu_fopen_ops_buffered(s,
359 s->bandwidth_limit,
360 migrate_fd_put_buffer,
361 migrate_fd_put_ready,
362 migrate_fd_wait_for_unfreeze,
363 migrate_fd_close);
364
365 dprintf("beginning savevm\n");
f327aa0c 366 ret = qemu_savevm_state_begin(s->mon, s->file, s->mig_state.blk,
c163b5ca 367 s->mig_state.shared);
065e2813
AL
368 if (ret < 0) {
369 dprintf("failed, %d\n", ret);
370 migrate_fd_error(s);
371 return;
372 }
c163b5ca 373
065e2813
AL
374 migrate_fd_put_ready(s);
375}
376
377void migrate_fd_put_ready(void *opaque)
378{
379 FdMigrationState *s = opaque;
380
381 if (s->state != MIG_STATE_ACTIVE) {
382 dprintf("put_ready returning because of non-active state\n");
383 return;
384 }
385
386 dprintf("iterate\n");
f327aa0c 387 if (qemu_savevm_state_iterate(s->mon, s->file) == 1) {
b161d123 388 int state;
eeb34af9
AL
389 int old_vm_running = vm_running;
390
065e2813
AL
391 dprintf("done iterating\n");
392 vm_stop(0);
393
0884657b 394 qemu_aio_flush();
065e2813 395 bdrv_flush_all();
f327aa0c 396 if ((qemu_savevm_state_complete(s->mon, s->file)) < 0) {
eeb34af9
AL
397 if (old_vm_running) {
398 vm_start();
399 }
b161d123
AL
400 state = MIG_STATE_ERROR;
401 } else {
402 state = MIG_STATE_COMPLETED;
403 }
065e2813 404 migrate_fd_cleanup(s);
b161d123 405 s->state = state;
065e2813
AL
406 }
407}
408
409int migrate_fd_get_status(MigrationState *mig_state)
410{
411 FdMigrationState *s = migrate_to_fms(mig_state);
412 return s->state;
413}
414
415void migrate_fd_cancel(MigrationState *mig_state)
416{
417 FdMigrationState *s = migrate_to_fms(mig_state);
418
419 if (s->state != MIG_STATE_ACTIVE)
420 return;
421
422 dprintf("cancelling migration\n");
423
424 s->state = MIG_STATE_CANCELLED;
f327aa0c 425 qemu_savevm_state_cancel(s->mon, s->file);
065e2813
AL
426
427 migrate_fd_cleanup(s);
428}
429
430void migrate_fd_release(MigrationState *mig_state)
431{
432 FdMigrationState *s = migrate_to_fms(mig_state);
433
434 dprintf("releasing state\n");
435
436 if (s->state == MIG_STATE_ACTIVE) {
437 s->state = MIG_STATE_CANCELLED;
438 migrate_fd_cleanup(s);
439 }
440 free(s);
441}
442
443void migrate_fd_wait_for_unfreeze(void *opaque)
444{
445 FdMigrationState *s = opaque;
446 int ret;
447
448 dprintf("wait for unfreeze\n");
449 if (s->state != MIG_STATE_ACTIVE)
450 return;
451
452 do {
453 fd_set wfds;
454
455 FD_ZERO(&wfds);
456 FD_SET(s->fd, &wfds);
457
458 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
459 } while (ret == -1 && (s->get_error(s)) == EINTR);
460}
461
462int migrate_fd_close(void *opaque)
463{
464 FdMigrationState *s = opaque;
e19252d3
UL
465
466 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
065e2813
AL
467 return s->close(s);
468}