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