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