]> Git Repo - qemu.git/blob - migration.c
hw/9pfs: File system helper process for qemu 9p proxy FS
[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  */
13
14 #include "qemu-common.h"
15 #include "migration.h"
16 #include "monitor.h"
17 #include "buffered_file.h"
18 #include "sysemu.h"
19 #include "block.h"
20 #include "qemu_socket.h"
21 #include "block-migration.h"
22 #include "qmp-commands.h"
23
24 //#define DEBUG_MIGRATION
25
26 #ifdef DEBUG_MIGRATION
27 #define DPRINTF(fmt, ...) \
28     do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
29 #else
30 #define DPRINTF(fmt, ...) \
31     do { } while (0)
32 #endif
33
34 enum {
35     MIG_STATE_ERROR,
36     MIG_STATE_SETUP,
37     MIG_STATE_CANCELLED,
38     MIG_STATE_ACTIVE,
39     MIG_STATE_COMPLETED,
40 };
41
42 #define MAX_THROTTLE  (32 << 20)      /* Migration speed throttling */
43
44 static NotifierList migration_state_notifiers =
45     NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
46
47 /* When we add fault tolerance, we could have several
48    migrations at once.  For now we don't need to add
49    dynamic creation of migration */
50
51 static MigrationState *migrate_get_current(void)
52 {
53     static MigrationState current_migration = {
54         .state = MIG_STATE_SETUP,
55         .bandwidth_limit = MAX_THROTTLE,
56     };
57
58     return &current_migration;
59 }
60
61 int qemu_start_incoming_migration(const char *uri)
62 {
63     const char *p;
64     int ret;
65
66     if (strstart(uri, "tcp:", &p))
67         ret = tcp_start_incoming_migration(p);
68 #if !defined(WIN32)
69     else if (strstart(uri, "exec:", &p))
70         ret =  exec_start_incoming_migration(p);
71     else if (strstart(uri, "unix:", &p))
72         ret = unix_start_incoming_migration(p);
73     else if (strstart(uri, "fd:", &p))
74         ret = fd_start_incoming_migration(p);
75 #endif
76     else {
77         fprintf(stderr, "unknown migration protocol: %s\n", uri);
78         ret = -EPROTONOSUPPORT;
79     }
80     return ret;
81 }
82
83 void process_incoming_migration(QEMUFile *f)
84 {
85     if (qemu_loadvm_state(f) < 0) {
86         fprintf(stderr, "load of migration failed\n");
87         exit(0);
88     }
89     qemu_announce_self();
90     DPRINTF("successfully loaded vm state\n");
91
92     /* Make sure all file formats flush their mutable metadata */
93     bdrv_invalidate_cache_all();
94
95     if (autostart) {
96         vm_start();
97     } else {
98         runstate_set(RUN_STATE_PRELAUNCH);
99     }
100 }
101
102 /* amount of nanoseconds we are willing to wait for migration to be down.
103  * the choice of nanoseconds is because it is the maximum resolution that
104  * get_clock() can achieve. It is an internal measure. All user-visible
105  * units must be in seconds */
106 static uint64_t max_downtime = 30000000;
107
108 uint64_t migrate_max_downtime(void)
109 {
110     return max_downtime;
111 }
112
113 MigrationInfo *qmp_query_migrate(Error **errp)
114 {
115     MigrationInfo *info = g_malloc0(sizeof(*info));
116     MigrationState *s = migrate_get_current();
117
118     switch (s->state) {
119     case MIG_STATE_SETUP:
120         /* no migration has happened ever */
121         break;
122     case MIG_STATE_ACTIVE:
123         info->has_status = true;
124         info->status = g_strdup("active");
125
126         info->has_ram = true;
127         info->ram = g_malloc0(sizeof(*info->ram));
128         info->ram->transferred = ram_bytes_transferred();
129         info->ram->remaining = ram_bytes_remaining();
130         info->ram->total = ram_bytes_total();
131
132         if (blk_mig_active()) {
133             info->has_disk = true;
134             info->disk = g_malloc0(sizeof(*info->disk));
135             info->disk->transferred = blk_mig_bytes_transferred();
136             info->disk->remaining = blk_mig_bytes_remaining();
137             info->disk->total = blk_mig_bytes_total();
138         }
139         break;
140     case MIG_STATE_COMPLETED:
141         info->has_status = true;
142         info->status = g_strdup("completed");
143         break;
144     case MIG_STATE_ERROR:
145         info->has_status = true;
146         info->status = g_strdup("failed");
147         break;
148     case MIG_STATE_CANCELLED:
149         info->has_status = true;
150         info->status = g_strdup("cancelled");
151         break;
152     }
153
154     return info;
155 }
156
157 /* shared migration helpers */
158
159 static void migrate_fd_monitor_suspend(MigrationState *s, Monitor *mon)
160 {
161     if (monitor_suspend(mon) == 0) {
162         DPRINTF("suspending monitor\n");
163     } else {
164         monitor_printf(mon, "terminal does not allow synchronous "
165                        "migration, continuing detached\n");
166     }
167 }
168
169 static int migrate_fd_cleanup(MigrationState *s)
170 {
171     int ret = 0;
172
173     qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
174
175     if (s->file) {
176         DPRINTF("closing file\n");
177         ret = qemu_fclose(s->file);
178         s->file = NULL;
179     } else {
180         if (s->mon) {
181             monitor_resume(s->mon);
182         }
183     }
184
185     if (s->fd != -1) {
186         close(s->fd);
187         s->fd = -1;
188     }
189
190     return ret;
191 }
192
193 void migrate_fd_error(MigrationState *s)
194 {
195     DPRINTF("setting error state\n");
196     s->state = MIG_STATE_ERROR;
197     notifier_list_notify(&migration_state_notifiers, s);
198     migrate_fd_cleanup(s);
199 }
200
201 static void migrate_fd_completed(MigrationState *s)
202 {
203     DPRINTF("setting completed state\n");
204     if (migrate_fd_cleanup(s) < 0) {
205         s->state = MIG_STATE_ERROR;
206     } else {
207         s->state = MIG_STATE_COMPLETED;
208         runstate_set(RUN_STATE_POSTMIGRATE);
209     }
210     notifier_list_notify(&migration_state_notifiers, s);
211 }
212
213 static void migrate_fd_put_notify(void *opaque)
214 {
215     MigrationState *s = opaque;
216
217     qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
218     qemu_file_put_notify(s->file);
219     if (s->file && qemu_file_get_error(s->file)) {
220         migrate_fd_error(s);
221     }
222 }
223
224 static ssize_t migrate_fd_put_buffer(void *opaque, const void *data,
225                                      size_t size)
226 {
227     MigrationState *s = opaque;
228     ssize_t ret;
229
230     if (s->state != MIG_STATE_ACTIVE) {
231         return -EIO;
232     }
233
234     do {
235         ret = s->write(s, data, size);
236     } while (ret == -1 && ((s->get_error(s)) == EINTR));
237
238     if (ret == -1)
239         ret = -(s->get_error(s));
240
241     if (ret == -EAGAIN) {
242         qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
243     }
244
245     return ret;
246 }
247
248 static void migrate_fd_put_ready(void *opaque)
249 {
250     MigrationState *s = opaque;
251     int ret;
252
253     if (s->state != MIG_STATE_ACTIVE) {
254         DPRINTF("put_ready returning because of non-active state\n");
255         return;
256     }
257
258     DPRINTF("iterate\n");
259     ret = qemu_savevm_state_iterate(s->mon, s->file);
260     if (ret < 0) {
261         migrate_fd_error(s);
262     } else if (ret == 1) {
263         int old_vm_running = runstate_is_running();
264
265         DPRINTF("done iterating\n");
266         vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
267
268         if (qemu_savevm_state_complete(s->mon, s->file) < 0) {
269             migrate_fd_error(s);
270         } else {
271             migrate_fd_completed(s);
272         }
273         if (s->state != MIG_STATE_COMPLETED) {
274             if (old_vm_running) {
275                 vm_start();
276             }
277         }
278     }
279 }
280
281 static void migrate_fd_cancel(MigrationState *s)
282 {
283     if (s->state != MIG_STATE_ACTIVE)
284         return;
285
286     DPRINTF("cancelling migration\n");
287
288     s->state = MIG_STATE_CANCELLED;
289     notifier_list_notify(&migration_state_notifiers, s);
290     qemu_savevm_state_cancel(s->mon, s->file);
291
292     migrate_fd_cleanup(s);
293 }
294
295 static void migrate_fd_wait_for_unfreeze(void *opaque)
296 {
297     MigrationState *s = opaque;
298     int ret;
299
300     DPRINTF("wait for unfreeze\n");
301     if (s->state != MIG_STATE_ACTIVE)
302         return;
303
304     do {
305         fd_set wfds;
306
307         FD_ZERO(&wfds);
308         FD_SET(s->fd, &wfds);
309
310         ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
311     } while (ret == -1 && (s->get_error(s)) == EINTR);
312
313     if (ret == -1) {
314         qemu_file_set_error(s->file, -s->get_error(s));
315     }
316 }
317
318 static int migrate_fd_close(void *opaque)
319 {
320     MigrationState *s = opaque;
321
322     if (s->mon) {
323         monitor_resume(s->mon);
324     }
325     qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
326     return s->close(s);
327 }
328
329 void add_migration_state_change_notifier(Notifier *notify)
330 {
331     notifier_list_add(&migration_state_notifiers, notify);
332 }
333
334 void remove_migration_state_change_notifier(Notifier *notify)
335 {
336     notifier_list_remove(&migration_state_notifiers, notify);
337 }
338
339 bool migration_is_active(MigrationState *s)
340 {
341     return s->state == MIG_STATE_ACTIVE;
342 }
343
344 bool migration_has_finished(MigrationState *s)
345 {
346     return s->state == MIG_STATE_COMPLETED;
347 }
348
349 bool migration_has_failed(MigrationState *s)
350 {
351     return (s->state == MIG_STATE_CANCELLED ||
352             s->state == MIG_STATE_ERROR);
353 }
354
355 void migrate_fd_connect(MigrationState *s)
356 {
357     int ret;
358
359     s->state = MIG_STATE_ACTIVE;
360     s->file = qemu_fopen_ops_buffered(s,
361                                       s->bandwidth_limit,
362                                       migrate_fd_put_buffer,
363                                       migrate_fd_put_ready,
364                                       migrate_fd_wait_for_unfreeze,
365                                       migrate_fd_close);
366
367     DPRINTF("beginning savevm\n");
368     ret = qemu_savevm_state_begin(s->mon, s->file, s->blk, s->shared);
369     if (ret < 0) {
370         DPRINTF("failed, %d\n", ret);
371         migrate_fd_error(s);
372         return;
373     }
374     migrate_fd_put_ready(s);
375 }
376
377 static MigrationState *migrate_init(Monitor *mon, int detach, int blk, int inc)
378 {
379     MigrationState *s = migrate_get_current();
380     int64_t bandwidth_limit = s->bandwidth_limit;
381
382     memset(s, 0, sizeof(*s));
383     s->bandwidth_limit = bandwidth_limit;
384     s->blk = blk;
385     s->shared = inc;
386
387     /* s->mon is used for two things:
388        - pass fd in fd migration
389        - suspend/resume monitor for not detached migration
390     */
391     s->mon = mon;
392     s->bandwidth_limit = bandwidth_limit;
393     s->state = MIG_STATE_SETUP;
394
395     if (!detach) {
396         migrate_fd_monitor_suspend(s, mon);
397     }
398
399     return s;
400 }
401
402 static GSList *migration_blockers;
403
404 void migrate_add_blocker(Error *reason)
405 {
406     migration_blockers = g_slist_prepend(migration_blockers, reason);
407 }
408
409 void migrate_del_blocker(Error *reason)
410 {
411     migration_blockers = g_slist_remove(migration_blockers, reason);
412 }
413
414 int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
415 {
416     MigrationState *s = migrate_get_current();
417     const char *p;
418     int detach = qdict_get_try_bool(qdict, "detach", 0);
419     int blk = qdict_get_try_bool(qdict, "blk", 0);
420     int inc = qdict_get_try_bool(qdict, "inc", 0);
421     const char *uri = qdict_get_str(qdict, "uri");
422     int ret;
423
424     if (s->state == MIG_STATE_ACTIVE) {
425         monitor_printf(mon, "migration already in progress\n");
426         return -1;
427     }
428
429     if (qemu_savevm_state_blocked(mon)) {
430         return -1;
431     }
432
433     if (migration_blockers) {
434         Error *err = migration_blockers->data;
435         qerror_report_err(err);
436         return -1;
437     }
438
439     s = migrate_init(mon, detach, blk, inc);
440
441     if (strstart(uri, "tcp:", &p)) {
442         ret = tcp_start_outgoing_migration(s, p);
443 #if !defined(WIN32)
444     } else if (strstart(uri, "exec:", &p)) {
445         ret = exec_start_outgoing_migration(s, p);
446     } else if (strstart(uri, "unix:", &p)) {
447         ret = unix_start_outgoing_migration(s, p);
448     } else if (strstart(uri, "fd:", &p)) {
449         ret = fd_start_outgoing_migration(s, p);
450 #endif
451     } else {
452         monitor_printf(mon, "unknown migration protocol: %s\n", uri);
453         ret  = -EINVAL;
454     }
455
456     if (ret < 0) {
457         monitor_printf(mon, "migration failed: %s\n", strerror(-ret));
458         return ret;
459     }
460
461     if (detach) {
462         s->mon = NULL;
463     }
464
465     notifier_list_notify(&migration_state_notifiers, s);
466     return 0;
467 }
468
469 void qmp_migrate_cancel(Error **errp)
470 {
471     migrate_fd_cancel(migrate_get_current());
472 }
473
474 void qmp_migrate_set_speed(int64_t value, Error **errp)
475 {
476     MigrationState *s;
477
478     if (value < 0) {
479         value = 0;
480     }
481
482     s = migrate_get_current();
483     s->bandwidth_limit = value;
484     qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
485 }
486
487 void qmp_migrate_set_downtime(double value, Error **errp)
488 {
489     value *= 1e9;
490     value = MAX(0, MIN(UINT64_MAX, value));
491     max_downtime = (uint64_t)value;
492 }
This page took 0.054923 seconds and 4 git commands to generate.