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