]> git.proxmox.com Git - qemu.git/blob - migration.c
Add migration accounting for normal and duplicate pages
[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 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
14 */
15
16 #include "qemu-common.h"
17 #include "migration.h"
18 #include "monitor.h"
19 #include "buffered_file.h"
20 #include "sysemu.h"
21 #include "block.h"
22 #include "qemu_socket.h"
23 #include "block-migration.h"
24 #include "qmp-commands.h"
25
26 //#define DEBUG_MIGRATION
27
28 #ifdef DEBUG_MIGRATION
29 #define DPRINTF(fmt, ...) \
30 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
31 #else
32 #define DPRINTF(fmt, ...) \
33 do { } while (0)
34 #endif
35
36 enum {
37 MIG_STATE_ERROR,
38 MIG_STATE_SETUP,
39 MIG_STATE_CANCELLED,
40 MIG_STATE_ACTIVE,
41 MIG_STATE_COMPLETED,
42 };
43
44 #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
45
46 /* Migration XBZRLE default cache size */
47 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
48
49 static NotifierList migration_state_notifiers =
50 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
51
52 /* When we add fault tolerance, we could have several
53 migrations at once. For now we don't need to add
54 dynamic creation of migration */
55
56 static MigrationState *migrate_get_current(void)
57 {
58 static MigrationState current_migration = {
59 .state = MIG_STATE_SETUP,
60 .bandwidth_limit = MAX_THROTTLE,
61 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
62 };
63
64 return &current_migration;
65 }
66
67 int qemu_start_incoming_migration(const char *uri, Error **errp)
68 {
69 const char *p;
70 int ret;
71
72 if (strstart(uri, "tcp:", &p))
73 ret = tcp_start_incoming_migration(p, errp);
74 #if !defined(WIN32)
75 else if (strstart(uri, "exec:", &p))
76 ret = exec_start_incoming_migration(p);
77 else if (strstart(uri, "unix:", &p))
78 ret = unix_start_incoming_migration(p);
79 else if (strstart(uri, "fd:", &p))
80 ret = fd_start_incoming_migration(p);
81 #endif
82 else {
83 fprintf(stderr, "unknown migration protocol: %s\n", uri);
84 ret = -EPROTONOSUPPORT;
85 }
86 return ret;
87 }
88
89 void process_incoming_migration(QEMUFile *f)
90 {
91 if (qemu_loadvm_state(f) < 0) {
92 fprintf(stderr, "load of migration failed\n");
93 exit(0);
94 }
95 qemu_announce_self();
96 DPRINTF("successfully loaded vm state\n");
97
98 bdrv_clear_incoming_migration_all();
99 /* Make sure all file formats flush their mutable metadata */
100 bdrv_invalidate_cache_all();
101
102 if (autostart) {
103 vm_start();
104 } else {
105 runstate_set(RUN_STATE_PRELAUNCH);
106 }
107 }
108
109 /* amount of nanoseconds we are willing to wait for migration to be down.
110 * the choice of nanoseconds is because it is the maximum resolution that
111 * get_clock() can achieve. It is an internal measure. All user-visible
112 * units must be in seconds */
113 static uint64_t max_downtime = 30000000;
114
115 uint64_t migrate_max_downtime(void)
116 {
117 return max_downtime;
118 }
119
120 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
121 {
122 MigrationCapabilityStatusList *head = NULL;
123 MigrationCapabilityStatusList *caps;
124 MigrationState *s = migrate_get_current();
125 int i;
126
127 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
128 if (head == NULL) {
129 head = g_malloc0(sizeof(*caps));
130 caps = head;
131 } else {
132 caps->next = g_malloc0(sizeof(*caps));
133 caps = caps->next;
134 }
135 caps->value =
136 g_malloc(sizeof(*caps->value));
137 caps->value->capability = i;
138 caps->value->state = s->enabled_capabilities[i];
139 }
140
141 return head;
142 }
143
144 MigrationInfo *qmp_query_migrate(Error **errp)
145 {
146 MigrationInfo *info = g_malloc0(sizeof(*info));
147 MigrationState *s = migrate_get_current();
148
149 switch (s->state) {
150 case MIG_STATE_SETUP:
151 /* no migration has happened ever */
152 break;
153 case MIG_STATE_ACTIVE:
154 info->has_status = true;
155 info->status = g_strdup("active");
156
157 info->has_ram = true;
158 info->ram = g_malloc0(sizeof(*info->ram));
159 info->ram->transferred = ram_bytes_transferred();
160 info->ram->remaining = ram_bytes_remaining();
161 info->ram->total = ram_bytes_total();
162 info->ram->total_time = qemu_get_clock_ms(rt_clock)
163 - s->total_time;
164 info->ram->duplicate = dup_mig_pages_transferred();
165 info->ram->normal = norm_mig_pages_transferred();
166 info->ram->normal_bytes = norm_mig_bytes_transferred();
167
168 if (blk_mig_active()) {
169 info->has_disk = true;
170 info->disk = g_malloc0(sizeof(*info->disk));
171 info->disk->transferred = blk_mig_bytes_transferred();
172 info->disk->remaining = blk_mig_bytes_remaining();
173 info->disk->total = blk_mig_bytes_total();
174 }
175 break;
176 case MIG_STATE_COMPLETED:
177 info->has_status = true;
178 info->status = g_strdup("completed");
179
180 info->has_ram = true;
181 info->ram = g_malloc0(sizeof(*info->ram));
182 info->ram->transferred = ram_bytes_transferred();
183 info->ram->remaining = 0;
184 info->ram->total = ram_bytes_total();
185 info->ram->total_time = s->total_time;
186 info->ram->duplicate = dup_mig_pages_transferred();
187 info->ram->normal = norm_mig_pages_transferred();
188 info->ram->normal_bytes = norm_mig_bytes_transferred();
189 break;
190 case MIG_STATE_ERROR:
191 info->has_status = true;
192 info->status = g_strdup("failed");
193 break;
194 case MIG_STATE_CANCELLED:
195 info->has_status = true;
196 info->status = g_strdup("cancelled");
197 break;
198 }
199
200 return info;
201 }
202
203 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
204 Error **errp)
205 {
206 MigrationState *s = migrate_get_current();
207 MigrationCapabilityStatusList *cap;
208
209 if (s->state == MIG_STATE_ACTIVE) {
210 error_set(errp, QERR_MIGRATION_ACTIVE);
211 return;
212 }
213
214 for (cap = params; cap; cap = cap->next) {
215 s->enabled_capabilities[cap->value->capability] = cap->value->state;
216 }
217 }
218
219 /* shared migration helpers */
220
221 static int migrate_fd_cleanup(MigrationState *s)
222 {
223 int ret = 0;
224
225 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
226
227 if (s->file) {
228 DPRINTF("closing file\n");
229 ret = qemu_fclose(s->file);
230 s->file = NULL;
231 }
232
233 if (s->fd != -1) {
234 close(s->fd);
235 s->fd = -1;
236 }
237
238 return ret;
239 }
240
241 void migrate_fd_error(MigrationState *s)
242 {
243 DPRINTF("setting error state\n");
244 s->state = MIG_STATE_ERROR;
245 notifier_list_notify(&migration_state_notifiers, s);
246 migrate_fd_cleanup(s);
247 }
248
249 static void migrate_fd_completed(MigrationState *s)
250 {
251 DPRINTF("setting completed state\n");
252 if (migrate_fd_cleanup(s) < 0) {
253 s->state = MIG_STATE_ERROR;
254 } else {
255 s->state = MIG_STATE_COMPLETED;
256 runstate_set(RUN_STATE_POSTMIGRATE);
257 }
258 notifier_list_notify(&migration_state_notifiers, s);
259 }
260
261 static void migrate_fd_put_notify(void *opaque)
262 {
263 MigrationState *s = opaque;
264
265 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
266 qemu_file_put_notify(s->file);
267 if (s->file && qemu_file_get_error(s->file)) {
268 migrate_fd_error(s);
269 }
270 }
271
272 static ssize_t migrate_fd_put_buffer(void *opaque, const void *data,
273 size_t size)
274 {
275 MigrationState *s = opaque;
276 ssize_t ret;
277
278 if (s->state != MIG_STATE_ACTIVE) {
279 return -EIO;
280 }
281
282 do {
283 ret = s->write(s, data, size);
284 } while (ret == -1 && ((s->get_error(s)) == EINTR));
285
286 if (ret == -1)
287 ret = -(s->get_error(s));
288
289 if (ret == -EAGAIN) {
290 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
291 }
292
293 return ret;
294 }
295
296 static void migrate_fd_put_ready(void *opaque)
297 {
298 MigrationState *s = opaque;
299 int ret;
300
301 if (s->state != MIG_STATE_ACTIVE) {
302 DPRINTF("put_ready returning because of non-active state\n");
303 return;
304 }
305
306 DPRINTF("iterate\n");
307 ret = qemu_savevm_state_iterate(s->file);
308 if (ret < 0) {
309 migrate_fd_error(s);
310 } else if (ret == 1) {
311 int old_vm_running = runstate_is_running();
312
313 DPRINTF("done iterating\n");
314 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
315 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
316
317 if (qemu_savevm_state_complete(s->file) < 0) {
318 migrate_fd_error(s);
319 } else {
320 migrate_fd_completed(s);
321 }
322 s->total_time = qemu_get_clock_ms(rt_clock) - s->total_time;
323 if (s->state != MIG_STATE_COMPLETED) {
324 if (old_vm_running) {
325 vm_start();
326 }
327 }
328 }
329 }
330
331 static void migrate_fd_cancel(MigrationState *s)
332 {
333 if (s->state != MIG_STATE_ACTIVE)
334 return;
335
336 DPRINTF("cancelling migration\n");
337
338 s->state = MIG_STATE_CANCELLED;
339 notifier_list_notify(&migration_state_notifiers, s);
340 qemu_savevm_state_cancel(s->file);
341
342 migrate_fd_cleanup(s);
343 }
344
345 static void migrate_fd_wait_for_unfreeze(void *opaque)
346 {
347 MigrationState *s = opaque;
348 int ret;
349
350 DPRINTF("wait for unfreeze\n");
351 if (s->state != MIG_STATE_ACTIVE)
352 return;
353
354 do {
355 fd_set wfds;
356
357 FD_ZERO(&wfds);
358 FD_SET(s->fd, &wfds);
359
360 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
361 } while (ret == -1 && (s->get_error(s)) == EINTR);
362
363 if (ret == -1) {
364 qemu_file_set_error(s->file, -s->get_error(s));
365 }
366 }
367
368 static int migrate_fd_close(void *opaque)
369 {
370 MigrationState *s = opaque;
371
372 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
373 return s->close(s);
374 }
375
376 void add_migration_state_change_notifier(Notifier *notify)
377 {
378 notifier_list_add(&migration_state_notifiers, notify);
379 }
380
381 void remove_migration_state_change_notifier(Notifier *notify)
382 {
383 notifier_remove(notify);
384 }
385
386 bool migration_is_active(MigrationState *s)
387 {
388 return s->state == MIG_STATE_ACTIVE;
389 }
390
391 bool migration_has_finished(MigrationState *s)
392 {
393 return s->state == MIG_STATE_COMPLETED;
394 }
395
396 bool migration_has_failed(MigrationState *s)
397 {
398 return (s->state == MIG_STATE_CANCELLED ||
399 s->state == MIG_STATE_ERROR);
400 }
401
402 void migrate_fd_connect(MigrationState *s)
403 {
404 int ret;
405
406 s->state = MIG_STATE_ACTIVE;
407 s->file = qemu_fopen_ops_buffered(s,
408 s->bandwidth_limit,
409 migrate_fd_put_buffer,
410 migrate_fd_put_ready,
411 migrate_fd_wait_for_unfreeze,
412 migrate_fd_close);
413
414 DPRINTF("beginning savevm\n");
415 ret = qemu_savevm_state_begin(s->file, &s->params);
416 if (ret < 0) {
417 DPRINTF("failed, %d\n", ret);
418 migrate_fd_error(s);
419 return;
420 }
421 migrate_fd_put_ready(s);
422 }
423
424 static MigrationState *migrate_init(const MigrationParams *params)
425 {
426 MigrationState *s = migrate_get_current();
427 int64_t bandwidth_limit = s->bandwidth_limit;
428 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
429 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
430
431 memcpy(enabled_capabilities, s->enabled_capabilities,
432 sizeof(enabled_capabilities));
433
434 memset(s, 0, sizeof(*s));
435 s->bandwidth_limit = bandwidth_limit;
436 s->params = *params;
437 memcpy(s->enabled_capabilities, enabled_capabilities,
438 sizeof(enabled_capabilities));
439 s->xbzrle_cache_size = xbzrle_cache_size;
440
441 s->bandwidth_limit = bandwidth_limit;
442 s->state = MIG_STATE_SETUP;
443 s->total_time = qemu_get_clock_ms(rt_clock);
444
445 return s;
446 }
447
448 static GSList *migration_blockers;
449
450 void migrate_add_blocker(Error *reason)
451 {
452 migration_blockers = g_slist_prepend(migration_blockers, reason);
453 }
454
455 void migrate_del_blocker(Error *reason)
456 {
457 migration_blockers = g_slist_remove(migration_blockers, reason);
458 }
459
460 void qmp_migrate(const char *uri, bool has_blk, bool blk,
461 bool has_inc, bool inc, bool has_detach, bool detach,
462 Error **errp)
463 {
464 MigrationState *s = migrate_get_current();
465 MigrationParams params;
466 const char *p;
467 int ret;
468
469 params.blk = blk;
470 params.shared = inc;
471
472 if (s->state == MIG_STATE_ACTIVE) {
473 error_set(errp, QERR_MIGRATION_ACTIVE);
474 return;
475 }
476
477 if (qemu_savevm_state_blocked(errp)) {
478 return;
479 }
480
481 if (migration_blockers) {
482 *errp = error_copy(migration_blockers->data);
483 return;
484 }
485
486 s = migrate_init(&params);
487
488 if (strstart(uri, "tcp:", &p)) {
489 ret = tcp_start_outgoing_migration(s, p, errp);
490 #if !defined(WIN32)
491 } else if (strstart(uri, "exec:", &p)) {
492 ret = exec_start_outgoing_migration(s, p);
493 } else if (strstart(uri, "unix:", &p)) {
494 ret = unix_start_outgoing_migration(s, p);
495 } else if (strstart(uri, "fd:", &p)) {
496 ret = fd_start_outgoing_migration(s, p);
497 #endif
498 } else {
499 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
500 return;
501 }
502
503 if (ret < 0) {
504 if (!error_is_set(errp)) {
505 DPRINTF("migration failed: %s\n", strerror(-ret));
506 /* FIXME: we should return meaningful errors */
507 error_set(errp, QERR_UNDEFINED_ERROR);
508 }
509 return;
510 }
511
512 notifier_list_notify(&migration_state_notifiers, s);
513 }
514
515 void qmp_migrate_cancel(Error **errp)
516 {
517 migrate_fd_cancel(migrate_get_current());
518 }
519
520 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
521 {
522 MigrationState *s = migrate_get_current();
523
524 /* Check for truncation */
525 if (value != (size_t)value) {
526 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
527 "exceeding address space");
528 return;
529 }
530
531 s->xbzrle_cache_size = xbzrle_cache_resize(value);
532 }
533
534 int64_t qmp_query_migrate_cache_size(Error **errp)
535 {
536 return migrate_xbzrle_cache_size();
537 }
538
539 void qmp_migrate_set_speed(int64_t value, Error **errp)
540 {
541 MigrationState *s;
542
543 if (value < 0) {
544 value = 0;
545 }
546
547 s = migrate_get_current();
548 s->bandwidth_limit = value;
549 qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
550 }
551
552 void qmp_migrate_set_downtime(double value, Error **errp)
553 {
554 value *= 1e9;
555 value = MAX(0, MIN(UINT64_MAX, value));
556 max_downtime = (uint64_t)value;
557 }
558
559 int migrate_use_xbzrle(void)
560 {
561 MigrationState *s;
562
563 s = migrate_get_current();
564
565 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
566 }
567
568 int64_t migrate_xbzrle_cache_size(void)
569 {
570 MigrationState *s;
571
572 s = migrate_get_current();
573
574 return s->xbzrle_cache_size;
575 }