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