]> Git Repo - qemu.git/blob - migration.c
kvmvapic: Disable if there is insufficient memory
[qemu.git] / migration.c
1 /*
2  * QEMU live migration
3  *
4  * Copyright IBM, Corp. 2008
5  *
6  * Authors:
7  *  Anthony Liguori   <[email protected]>
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 static void get_xbzrle_cache_stats(MigrationInfo *info)
145 {
146     if (migrate_use_xbzrle()) {
147         info->has_xbzrle_cache = true;
148         info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
149         info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
150         info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
151         info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
152         info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
153         info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
154     }
155 }
156
157 MigrationInfo *qmp_query_migrate(Error **errp)
158 {
159     MigrationInfo *info = g_malloc0(sizeof(*info));
160     MigrationState *s = migrate_get_current();
161
162     switch (s->state) {
163     case MIG_STATE_SETUP:
164         /* no migration has happened ever */
165         break;
166     case MIG_STATE_ACTIVE:
167         info->has_status = true;
168         info->status = g_strdup("active");
169
170         info->has_ram = true;
171         info->ram = g_malloc0(sizeof(*info->ram));
172         info->ram->transferred = ram_bytes_transferred();
173         info->ram->remaining = ram_bytes_remaining();
174         info->ram->total = ram_bytes_total();
175         info->ram->total_time = qemu_get_clock_ms(rt_clock)
176             - s->total_time;
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
181         if (blk_mig_active()) {
182             info->has_disk = true;
183             info->disk = g_malloc0(sizeof(*info->disk));
184             info->disk->transferred = blk_mig_bytes_transferred();
185             info->disk->remaining = blk_mig_bytes_remaining();
186             info->disk->total = blk_mig_bytes_total();
187         }
188
189         get_xbzrle_cache_stats(info);
190         break;
191     case MIG_STATE_COMPLETED:
192         get_xbzrle_cache_stats(info);
193
194         info->has_status = true;
195         info->status = g_strdup("completed");
196
197         info->has_ram = true;
198         info->ram = g_malloc0(sizeof(*info->ram));
199         info->ram->transferred = ram_bytes_transferred();
200         info->ram->remaining = 0;
201         info->ram->total = ram_bytes_total();
202         info->ram->total_time = s->total_time;
203         info->ram->duplicate = dup_mig_pages_transferred();
204         info->ram->normal = norm_mig_pages_transferred();
205         info->ram->normal_bytes = norm_mig_bytes_transferred();
206         break;
207     case MIG_STATE_ERROR:
208         info->has_status = true;
209         info->status = g_strdup("failed");
210         break;
211     case MIG_STATE_CANCELLED:
212         info->has_status = true;
213         info->status = g_strdup("cancelled");
214         break;
215     }
216
217     return info;
218 }
219
220 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
221                                   Error **errp)
222 {
223     MigrationState *s = migrate_get_current();
224     MigrationCapabilityStatusList *cap;
225
226     if (s->state == MIG_STATE_ACTIVE) {
227         error_set(errp, QERR_MIGRATION_ACTIVE);
228         return;
229     }
230
231     for (cap = params; cap; cap = cap->next) {
232         s->enabled_capabilities[cap->value->capability] = cap->value->state;
233     }
234 }
235
236 /* shared migration helpers */
237
238 static int migrate_fd_cleanup(MigrationState *s)
239 {
240     int ret = 0;
241
242     qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
243
244     if (s->file) {
245         DPRINTF("closing file\n");
246         ret = qemu_fclose(s->file);
247         s->file = NULL;
248     }
249
250     if (s->fd != -1) {
251         close(s->fd);
252         s->fd = -1;
253     }
254
255     return ret;
256 }
257
258 void migrate_fd_error(MigrationState *s)
259 {
260     DPRINTF("setting error state\n");
261     s->state = MIG_STATE_ERROR;
262     notifier_list_notify(&migration_state_notifiers, s);
263     migrate_fd_cleanup(s);
264 }
265
266 static void migrate_fd_completed(MigrationState *s)
267 {
268     DPRINTF("setting completed state\n");
269     if (migrate_fd_cleanup(s) < 0) {
270         s->state = MIG_STATE_ERROR;
271     } else {
272         s->state = MIG_STATE_COMPLETED;
273         runstate_set(RUN_STATE_POSTMIGRATE);
274     }
275     notifier_list_notify(&migration_state_notifiers, s);
276 }
277
278 static void migrate_fd_put_notify(void *opaque)
279 {
280     MigrationState *s = opaque;
281
282     qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
283     qemu_file_put_notify(s->file);
284     if (s->file && qemu_file_get_error(s->file)) {
285         migrate_fd_error(s);
286     }
287 }
288
289 static ssize_t migrate_fd_put_buffer(void *opaque, const void *data,
290                                      size_t size)
291 {
292     MigrationState *s = opaque;
293     ssize_t ret;
294
295     if (s->state != MIG_STATE_ACTIVE) {
296         return -EIO;
297     }
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
310     return ret;
311 }
312
313 static void migrate_fd_put_ready(void *opaque)
314 {
315     MigrationState *s = opaque;
316     int ret;
317
318     if (s->state != MIG_STATE_ACTIVE) {
319         DPRINTF("put_ready returning because of non-active state\n");
320         return;
321     }
322
323     DPRINTF("iterate\n");
324     ret = qemu_savevm_state_iterate(s->file);
325     if (ret < 0) {
326         migrate_fd_error(s);
327     } else if (ret == 1) {
328         int old_vm_running = runstate_is_running();
329
330         DPRINTF("done iterating\n");
331         qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
332         vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
333
334         if (qemu_savevm_state_complete(s->file) < 0) {
335             migrate_fd_error(s);
336         } else {
337             migrate_fd_completed(s);
338         }
339         s->total_time = qemu_get_clock_ms(rt_clock) - s->total_time;
340         if (s->state != MIG_STATE_COMPLETED) {
341             if (old_vm_running) {
342                 vm_start();
343             }
344         }
345     }
346 }
347
348 static void migrate_fd_cancel(MigrationState *s)
349 {
350     if (s->state != MIG_STATE_ACTIVE)
351         return;
352
353     DPRINTF("cancelling migration\n");
354
355     s->state = MIG_STATE_CANCELLED;
356     notifier_list_notify(&migration_state_notifiers, s);
357     qemu_savevm_state_cancel(s->file);
358
359     migrate_fd_cleanup(s);
360 }
361
362 static void migrate_fd_wait_for_unfreeze(void *opaque)
363 {
364     MigrationState *s = opaque;
365     int ret;
366
367     DPRINTF("wait for unfreeze\n");
368     if (s->state != MIG_STATE_ACTIVE)
369         return;
370
371     do {
372         fd_set wfds;
373
374         FD_ZERO(&wfds);
375         FD_SET(s->fd, &wfds);
376
377         ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
378     } while (ret == -1 && (s->get_error(s)) == EINTR);
379
380     if (ret == -1) {
381         qemu_file_set_error(s->file, -s->get_error(s));
382     }
383 }
384
385 static int migrate_fd_close(void *opaque)
386 {
387     MigrationState *s = opaque;
388
389     qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
390     return s->close(s);
391 }
392
393 void add_migration_state_change_notifier(Notifier *notify)
394 {
395     notifier_list_add(&migration_state_notifiers, notify);
396 }
397
398 void remove_migration_state_change_notifier(Notifier *notify)
399 {
400     notifier_remove(notify);
401 }
402
403 bool migration_is_active(MigrationState *s)
404 {
405     return s->state == MIG_STATE_ACTIVE;
406 }
407
408 bool migration_has_finished(MigrationState *s)
409 {
410     return s->state == MIG_STATE_COMPLETED;
411 }
412
413 bool migration_has_failed(MigrationState *s)
414 {
415     return (s->state == MIG_STATE_CANCELLED ||
416             s->state == MIG_STATE_ERROR);
417 }
418
419 void migrate_fd_connect(MigrationState *s)
420 {
421     int ret;
422
423     s->state = MIG_STATE_ACTIVE;
424     s->file = qemu_fopen_ops_buffered(s,
425                                       s->bandwidth_limit,
426                                       migrate_fd_put_buffer,
427                                       migrate_fd_put_ready,
428                                       migrate_fd_wait_for_unfreeze,
429                                       migrate_fd_close);
430
431     DPRINTF("beginning savevm\n");
432     ret = qemu_savevm_state_begin(s->file, &s->params);
433     if (ret < 0) {
434         DPRINTF("failed, %d\n", ret);
435         migrate_fd_error(s);
436         return;
437     }
438     migrate_fd_put_ready(s);
439 }
440
441 static MigrationState *migrate_init(const MigrationParams *params)
442 {
443     MigrationState *s = migrate_get_current();
444     int64_t bandwidth_limit = s->bandwidth_limit;
445     bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
446     int64_t xbzrle_cache_size = s->xbzrle_cache_size;
447
448     memcpy(enabled_capabilities, s->enabled_capabilities,
449            sizeof(enabled_capabilities));
450
451     memset(s, 0, sizeof(*s));
452     s->bandwidth_limit = bandwidth_limit;
453     s->params = *params;
454     memcpy(s->enabled_capabilities, enabled_capabilities,
455            sizeof(enabled_capabilities));
456     s->xbzrle_cache_size = xbzrle_cache_size;
457
458     s->bandwidth_limit = bandwidth_limit;
459     s->state = MIG_STATE_SETUP;
460     s->total_time = qemu_get_clock_ms(rt_clock);
461
462     return s;
463 }
464
465 static GSList *migration_blockers;
466
467 void migrate_add_blocker(Error *reason)
468 {
469     migration_blockers = g_slist_prepend(migration_blockers, reason);
470 }
471
472 void migrate_del_blocker(Error *reason)
473 {
474     migration_blockers = g_slist_remove(migration_blockers, reason);
475 }
476
477 void qmp_migrate(const char *uri, bool has_blk, bool blk,
478                  bool has_inc, bool inc, bool has_detach, bool detach,
479                  Error **errp)
480 {
481     MigrationState *s = migrate_get_current();
482     MigrationParams params;
483     const char *p;
484     int ret;
485
486     params.blk = blk;
487     params.shared = inc;
488
489     if (s->state == MIG_STATE_ACTIVE) {
490         error_set(errp, QERR_MIGRATION_ACTIVE);
491         return;
492     }
493
494     if (qemu_savevm_state_blocked(errp)) {
495         return;
496     }
497
498     if (migration_blockers) {
499         *errp = error_copy(migration_blockers->data);
500         return;
501     }
502
503     s = migrate_init(&params);
504
505     if (strstart(uri, "tcp:", &p)) {
506         ret = tcp_start_outgoing_migration(s, p, errp);
507 #if !defined(WIN32)
508     } else if (strstart(uri, "exec:", &p)) {
509         ret = exec_start_outgoing_migration(s, p);
510     } else if (strstart(uri, "unix:", &p)) {
511         ret = unix_start_outgoing_migration(s, p);
512     } else if (strstart(uri, "fd:", &p)) {
513         ret = fd_start_outgoing_migration(s, p);
514 #endif
515     } else {
516         error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
517         return;
518     }
519
520     if (ret < 0) {
521         if (!error_is_set(errp)) {
522             DPRINTF("migration failed: %s\n", strerror(-ret));
523             /* FIXME: we should return meaningful errors */
524             error_set(errp, QERR_UNDEFINED_ERROR);
525         }
526         return;
527     }
528
529     notifier_list_notify(&migration_state_notifiers, s);
530 }
531
532 void qmp_migrate_cancel(Error **errp)
533 {
534     migrate_fd_cancel(migrate_get_current());
535 }
536
537 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
538 {
539     MigrationState *s = migrate_get_current();
540
541     /* Check for truncation */
542     if (value != (size_t)value) {
543         error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
544                   "exceeding address space");
545         return;
546     }
547
548     s->xbzrle_cache_size = xbzrle_cache_resize(value);
549 }
550
551 int64_t qmp_query_migrate_cache_size(Error **errp)
552 {
553     return migrate_xbzrle_cache_size();
554 }
555
556 void qmp_migrate_set_speed(int64_t value, Error **errp)
557 {
558     MigrationState *s;
559
560     if (value < 0) {
561         value = 0;
562     }
563
564     s = migrate_get_current();
565     s->bandwidth_limit = value;
566     qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
567 }
568
569 void qmp_migrate_set_downtime(double value, Error **errp)
570 {
571     value *= 1e9;
572     value = MAX(0, MIN(UINT64_MAX, value));
573     max_downtime = (uint64_t)value;
574 }
575
576 int migrate_use_xbzrle(void)
577 {
578     MigrationState *s;
579
580     s = migrate_get_current();
581
582     return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
583 }
584
585 int64_t migrate_xbzrle_cache_size(void)
586 {
587     MigrationState *s;
588
589     s = migrate_get_current();
590
591     return s->xbzrle_cache_size;
592 }
This page took 0.056462 seconds and 4 git commands to generate.