]> Git Repo - qemu.git/blob - migration.c
vga: pass owner to vga_init
[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/migration.h"
18 #include "monitor/monitor.h"
19 #include "migration/qemu-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 #include "trace.h"
27
28 //#define DEBUG_MIGRATION
29
30 #ifdef DEBUG_MIGRATION
31 #define DPRINTF(fmt, ...) \
32     do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
33 #else
34 #define DPRINTF(fmt, ...) \
35     do { } while (0)
36 #endif
37
38 enum {
39     MIG_STATE_ERROR,
40     MIG_STATE_SETUP,
41     MIG_STATE_CANCELLED,
42     MIG_STATE_ACTIVE,
43     MIG_STATE_COMPLETED,
44 };
45
46 #define MAX_THROTTLE  (32 << 20)      /* Migration speed throttling */
47
48 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
49  * data. */
50 #define BUFFER_DELAY     100
51 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
52
53 /* Migration XBZRLE default cache size */
54 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
55
56 static NotifierList migration_state_notifiers =
57     NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
58
59 /* When we add fault tolerance, we could have several
60    migrations at once.  For now we don't need to add
61    dynamic creation of migration */
62
63 MigrationState *migrate_get_current(void)
64 {
65     static MigrationState current_migration = {
66         .state = MIG_STATE_SETUP,
67         .bandwidth_limit = MAX_THROTTLE,
68         .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
69         .mbps = -1,
70     };
71
72     return &current_migration;
73 }
74
75 void qemu_start_incoming_migration(const char *uri, Error **errp)
76 {
77     const char *p;
78
79     if (strstart(uri, "tcp:", &p))
80         tcp_start_incoming_migration(p, errp);
81 #if !defined(WIN32)
82     else if (strstart(uri, "exec:", &p))
83         exec_start_incoming_migration(p, errp);
84     else if (strstart(uri, "unix:", &p))
85         unix_start_incoming_migration(p, errp);
86     else if (strstart(uri, "fd:", &p))
87         fd_start_incoming_migration(p, errp);
88 #endif
89     else {
90         error_setg(errp, "unknown migration protocol: %s", uri);
91     }
92 }
93
94 static void process_incoming_migration_co(void *opaque)
95 {
96     QEMUFile *f = opaque;
97     int ret;
98
99     ret = qemu_loadvm_state(f);
100     qemu_fclose(f);
101     if (ret < 0) {
102         fprintf(stderr, "load of migration failed\n");
103         exit(EXIT_FAILURE);
104     }
105     qemu_announce_self();
106     DPRINTF("successfully loaded vm state\n");
107
108     bdrv_clear_incoming_migration_all();
109     /* Make sure all file formats flush their mutable metadata */
110     bdrv_invalidate_cache_all();
111
112     if (autostart) {
113         vm_start();
114     } else {
115         runstate_set(RUN_STATE_PAUSED);
116     }
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     qemu_set_nonblock(fd);
126     qemu_coroutine_enter(co, f);
127 }
128
129 /* amount of nanoseconds we are willing to wait for migration to be down.
130  * the choice of nanoseconds is because it is the maximum resolution that
131  * get_clock() can achieve. It is an internal measure. All user-visible
132  * units must be in seconds */
133 static uint64_t max_downtime = 30000000;
134
135 uint64_t migrate_max_downtime(void)
136 {
137     return max_downtime;
138 }
139
140 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
141 {
142     MigrationCapabilityStatusList *head = NULL;
143     MigrationCapabilityStatusList *caps;
144     MigrationState *s = migrate_get_current();
145     int i;
146
147     for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
148         if (head == NULL) {
149             head = g_malloc0(sizeof(*caps));
150             caps = head;
151         } else {
152             caps->next = g_malloc0(sizeof(*caps));
153             caps = caps->next;
154         }
155         caps->value =
156             g_malloc(sizeof(*caps->value));
157         caps->value->capability = i;
158         caps->value->state = s->enabled_capabilities[i];
159     }
160
161     return head;
162 }
163
164 static void get_xbzrle_cache_stats(MigrationInfo *info)
165 {
166     if (migrate_use_xbzrle()) {
167         info->has_xbzrle_cache = true;
168         info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
169         info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
170         info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
171         info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
172         info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
173         info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
174     }
175 }
176
177 MigrationInfo *qmp_query_migrate(Error **errp)
178 {
179     MigrationInfo *info = g_malloc0(sizeof(*info));
180     MigrationState *s = migrate_get_current();
181
182     switch (s->state) {
183     case MIG_STATE_SETUP:
184         /* no migration has happened ever */
185         break;
186     case MIG_STATE_ACTIVE:
187         info->has_status = true;
188         info->status = g_strdup("active");
189         info->has_total_time = true;
190         info->total_time = qemu_get_clock_ms(rt_clock)
191             - s->total_time;
192         info->has_expected_downtime = true;
193         info->expected_downtime = s->expected_downtime;
194
195         info->has_ram = true;
196         info->ram = g_malloc0(sizeof(*info->ram));
197         info->ram->transferred = ram_bytes_transferred();
198         info->ram->remaining = ram_bytes_remaining();
199         info->ram->total = ram_bytes_total();
200         info->ram->duplicate = dup_mig_pages_transferred();
201         info->ram->skipped = skipped_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         info->ram->mbps = s->mbps;
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->skipped = skipped_mig_pages_transferred();
233         info->ram->normal = norm_mig_pages_transferred();
234         info->ram->normal_bytes = norm_mig_bytes_transferred();
235         info->ram->mbps = s->mbps;
236         break;
237     case MIG_STATE_ERROR:
238         info->has_status = true;
239         info->status = g_strdup("failed");
240         break;
241     case MIG_STATE_CANCELLED:
242         info->has_status = true;
243         info->status = g_strdup("cancelled");
244         break;
245     }
246
247     return info;
248 }
249
250 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
251                                   Error **errp)
252 {
253     MigrationState *s = migrate_get_current();
254     MigrationCapabilityStatusList *cap;
255
256     if (s->state == MIG_STATE_ACTIVE) {
257         error_set(errp, QERR_MIGRATION_ACTIVE);
258         return;
259     }
260
261     for (cap = params; cap; cap = cap->next) {
262         s->enabled_capabilities[cap->value->capability] = cap->value->state;
263     }
264 }
265
266 /* shared migration helpers */
267
268 static void migrate_fd_cleanup(void *opaque)
269 {
270     MigrationState *s = opaque;
271
272     qemu_bh_delete(s->cleanup_bh);
273     s->cleanup_bh = NULL;
274
275     if (s->file) {
276         DPRINTF("closing file\n");
277         qemu_mutex_unlock_iothread();
278         qemu_thread_join(&s->thread);
279         qemu_mutex_lock_iothread();
280
281         qemu_fclose(s->file);
282         s->file = NULL;
283     }
284
285     assert(s->state != MIG_STATE_ACTIVE);
286
287     if (s->state != MIG_STATE_COMPLETED) {
288         qemu_savevm_state_cancel();
289     }
290
291     notifier_list_notify(&migration_state_notifiers, s);
292 }
293
294 static void migrate_finish_set_state(MigrationState *s, int new_state)
295 {
296     if (__sync_val_compare_and_swap(&s->state, MIG_STATE_ACTIVE,
297                                     new_state) == new_state) {
298         trace_migrate_set_state(new_state);
299     }
300 }
301
302 void migrate_fd_error(MigrationState *s)
303 {
304     DPRINTF("setting error state\n");
305     assert(s->file == NULL);
306     s->state = MIG_STATE_ERROR;
307     trace_migrate_set_state(MIG_STATE_ERROR);
308     notifier_list_notify(&migration_state_notifiers, s);
309 }
310
311 static void migrate_fd_cancel(MigrationState *s)
312 {
313     DPRINTF("cancelling migration\n");
314
315     migrate_finish_set_state(s, MIG_STATE_CANCELLED);
316 }
317
318 void add_migration_state_change_notifier(Notifier *notify)
319 {
320     notifier_list_add(&migration_state_notifiers, notify);
321 }
322
323 void remove_migration_state_change_notifier(Notifier *notify)
324 {
325     notifier_remove(notify);
326 }
327
328 bool migration_is_active(MigrationState *s)
329 {
330     return s->state == MIG_STATE_ACTIVE;
331 }
332
333 bool migration_has_finished(MigrationState *s)
334 {
335     return s->state == MIG_STATE_COMPLETED;
336 }
337
338 bool migration_has_failed(MigrationState *s)
339 {
340     return (s->state == MIG_STATE_CANCELLED ||
341             s->state == MIG_STATE_ERROR);
342 }
343
344 static MigrationState *migrate_init(const MigrationParams *params)
345 {
346     MigrationState *s = migrate_get_current();
347     int64_t bandwidth_limit = s->bandwidth_limit;
348     bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
349     int64_t xbzrle_cache_size = s->xbzrle_cache_size;
350
351     memcpy(enabled_capabilities, s->enabled_capabilities,
352            sizeof(enabled_capabilities));
353
354     memset(s, 0, sizeof(*s));
355     s->params = *params;
356     memcpy(s->enabled_capabilities, enabled_capabilities,
357            sizeof(enabled_capabilities));
358     s->xbzrle_cache_size = xbzrle_cache_size;
359
360     s->bandwidth_limit = bandwidth_limit;
361     s->state = MIG_STATE_SETUP;
362     trace_migrate_set_state(MIG_STATE_SETUP);
363
364     s->total_time = qemu_get_clock_ms(rt_clock);
365     return s;
366 }
367
368 static GSList *migration_blockers;
369
370 void migrate_add_blocker(Error *reason)
371 {
372     migration_blockers = g_slist_prepend(migration_blockers, reason);
373 }
374
375 void migrate_del_blocker(Error *reason)
376 {
377     migration_blockers = g_slist_remove(migration_blockers, reason);
378 }
379
380 void qmp_migrate(const char *uri, bool has_blk, bool blk,
381                  bool has_inc, bool inc, bool has_detach, bool detach,
382                  Error **errp)
383 {
384     Error *local_err = NULL;
385     MigrationState *s = migrate_get_current();
386     MigrationParams params;
387     const char *p;
388
389     params.blk = blk;
390     params.shared = inc;
391
392     if (s->state == MIG_STATE_ACTIVE) {
393         error_set(errp, QERR_MIGRATION_ACTIVE);
394         return;
395     }
396
397     if (qemu_savevm_state_blocked(errp)) {
398         return;
399     }
400
401     if (migration_blockers) {
402         *errp = error_copy(migration_blockers->data);
403         return;
404     }
405
406     s = migrate_init(&params);
407
408     if (strstart(uri, "tcp:", &p)) {
409         tcp_start_outgoing_migration(s, p, &local_err);
410 #if !defined(WIN32)
411     } else if (strstart(uri, "exec:", &p)) {
412         exec_start_outgoing_migration(s, p, &local_err);
413     } else if (strstart(uri, "unix:", &p)) {
414         unix_start_outgoing_migration(s, p, &local_err);
415     } else if (strstart(uri, "fd:", &p)) {
416         fd_start_outgoing_migration(s, p, &local_err);
417 #endif
418     } else {
419         error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
420         return;
421     }
422
423     if (local_err) {
424         migrate_fd_error(s);
425         error_propagate(errp, local_err);
426         return;
427     }
428 }
429
430 void qmp_migrate_cancel(Error **errp)
431 {
432     migrate_fd_cancel(migrate_get_current());
433 }
434
435 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
436 {
437     MigrationState *s = migrate_get_current();
438
439     /* Check for truncation */
440     if (value != (size_t)value) {
441         error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
442                   "exceeding address space");
443         return;
444     }
445
446     s->xbzrle_cache_size = xbzrle_cache_resize(value);
447 }
448
449 int64_t qmp_query_migrate_cache_size(Error **errp)
450 {
451     return migrate_xbzrle_cache_size();
452 }
453
454 void qmp_migrate_set_speed(int64_t value, Error **errp)
455 {
456     MigrationState *s;
457
458     if (value < 0) {
459         value = 0;
460     }
461     if (value > SIZE_MAX) {
462         value = SIZE_MAX;
463     }
464
465     s = migrate_get_current();
466     s->bandwidth_limit = value;
467     if (s->file) {
468         qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
469     }
470 }
471
472 void qmp_migrate_set_downtime(double value, Error **errp)
473 {
474     value *= 1e9;
475     value = MAX(0, MIN(UINT64_MAX, value));
476     max_downtime = (uint64_t)value;
477 }
478
479 bool migrate_rdma_pin_all(void)
480 {
481     MigrationState *s;
482
483     s = migrate_get_current();
484
485     return s->enabled_capabilities[MIGRATION_CAPABILITY_X_RDMA_PIN_ALL];
486 }
487
488 int migrate_use_xbzrle(void)
489 {
490     MigrationState *s;
491
492     s = migrate_get_current();
493
494     return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
495 }
496
497 int64_t migrate_xbzrle_cache_size(void)
498 {
499     MigrationState *s;
500
501     s = migrate_get_current();
502
503     return s->xbzrle_cache_size;
504 }
505
506 /* migration thread support */
507
508 static void *migration_thread(void *opaque)
509 {
510     MigrationState *s = opaque;
511     int64_t initial_time = qemu_get_clock_ms(rt_clock);
512     int64_t initial_bytes = 0;
513     int64_t max_size = 0;
514     int64_t start_time = initial_time;
515     bool old_vm_running = false;
516
517     DPRINTF("beginning savevm\n");
518     qemu_savevm_state_begin(s->file, &s->params);
519
520     while (s->state == MIG_STATE_ACTIVE) {
521         int64_t current_time;
522         uint64_t pending_size;
523
524         if (!qemu_file_rate_limit(s->file)) {
525             DPRINTF("iterate\n");
526             pending_size = qemu_savevm_state_pending(s->file, max_size);
527             DPRINTF("pending size %lu max %lu\n", pending_size, max_size);
528             if (pending_size && pending_size >= max_size) {
529                 qemu_savevm_state_iterate(s->file);
530             } else {
531                 DPRINTF("done iterating\n");
532                 qemu_mutex_lock_iothread();
533                 start_time = qemu_get_clock_ms(rt_clock);
534                 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
535                 old_vm_running = runstate_is_running();
536                 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
537                 qemu_file_set_rate_limit(s->file, INT_MAX);
538                 qemu_savevm_state_complete(s->file);
539                 qemu_mutex_unlock_iothread();
540                 if (!qemu_file_get_error(s->file)) {
541                     migrate_finish_set_state(s, MIG_STATE_COMPLETED);
542                     break;
543                 }
544             }
545         }
546
547         if (qemu_file_get_error(s->file)) {
548             migrate_finish_set_state(s, MIG_STATE_ERROR);
549             break;
550         }
551         current_time = qemu_get_clock_ms(rt_clock);
552         if (current_time >= initial_time + BUFFER_DELAY) {
553             uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
554             uint64_t time_spent = current_time - initial_time;
555             double bandwidth = transferred_bytes / time_spent;
556             max_size = bandwidth * migrate_max_downtime() / 1000000;
557
558             s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
559                     ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
560
561             DPRINTF("transferred %" PRIu64 " time_spent %" PRIu64
562                     " bandwidth %g max_size %" PRId64 "\n",
563                     transferred_bytes, time_spent, bandwidth, max_size);
564             /* if we haven't sent anything, we don't want to recalculate
565                10000 is a small enough number for our purposes */
566             if (s->dirty_bytes_rate && transferred_bytes > 10000) {
567                 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
568             }
569
570             qemu_file_reset_rate_limit(s->file);
571             initial_time = current_time;
572             initial_bytes = qemu_ftell(s->file);
573         }
574         if (qemu_file_rate_limit(s->file)) {
575             /* usleep expects microseconds */
576             g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
577         }
578     }
579
580     qemu_mutex_lock_iothread();
581     if (s->state == MIG_STATE_COMPLETED) {
582         int64_t end_time = qemu_get_clock_ms(rt_clock);
583         s->total_time = end_time - s->total_time;
584         s->downtime = end_time - start_time;
585         runstate_set(RUN_STATE_POSTMIGRATE);
586     } else {
587         if (old_vm_running) {
588             vm_start();
589         }
590     }
591     qemu_bh_schedule(s->cleanup_bh);
592     qemu_mutex_unlock_iothread();
593
594     return NULL;
595 }
596
597 void migrate_fd_connect(MigrationState *s)
598 {
599     s->state = MIG_STATE_ACTIVE;
600     trace_migrate_set_state(MIG_STATE_ACTIVE);
601
602     /* This is a best 1st approximation. ns to ms */
603     s->expected_downtime = max_downtime/1000000;
604     s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
605
606     qemu_file_set_rate_limit(s->file,
607                              s->bandwidth_limit / XFER_LIMIT_RATIO);
608
609     qemu_thread_create(&s->thread, migration_thread, s,
610                        QEMU_THREAD_JOINABLE);
611     notifier_list_notify(&migration_state_notifiers, s);
612 }
This page took 0.055426 seconds and 4 git commands to generate.