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