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