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