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