]> Git Repo - qemu.git/blob - migration.c
Add spent time for migration
[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, Error **errp)
64 {
65     const char *p;
66     int ret;
67
68     if (strstart(uri, "tcp:", &p))
69         ret = tcp_start_incoming_migration(p, errp);
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         info->ram->total_time = qemu_get_clock_ms(rt_clock)
135             - s->total_time;
136
137         if (blk_mig_active()) {
138             info->has_disk = true;
139             info->disk = g_malloc0(sizeof(*info->disk));
140             info->disk->transferred = blk_mig_bytes_transferred();
141             info->disk->remaining = blk_mig_bytes_remaining();
142             info->disk->total = blk_mig_bytes_total();
143         }
144         break;
145     case MIG_STATE_COMPLETED:
146         info->has_status = true;
147         info->status = g_strdup("completed");
148
149         info->has_ram = true;
150         info->ram = g_malloc0(sizeof(*info->ram));
151         info->ram->transferred = ram_bytes_transferred();
152         info->ram->remaining = 0;
153         info->ram->total = ram_bytes_total();
154         info->ram->total_time = s->total_time;
155         break;
156     case MIG_STATE_ERROR:
157         info->has_status = true;
158         info->status = g_strdup("failed");
159         break;
160     case MIG_STATE_CANCELLED:
161         info->has_status = true;
162         info->status = g_strdup("cancelled");
163         break;
164     }
165
166     return info;
167 }
168
169 /* shared migration helpers */
170
171 static int migrate_fd_cleanup(MigrationState *s)
172 {
173     int ret = 0;
174
175     qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
176
177     if (s->file) {
178         DPRINTF("closing file\n");
179         ret = qemu_fclose(s->file);
180         s->file = NULL;
181     }
182
183     if (s->fd != -1) {
184         close(s->fd);
185         s->fd = -1;
186     }
187
188     return ret;
189 }
190
191 void migrate_fd_error(MigrationState *s)
192 {
193     DPRINTF("setting error state\n");
194     s->state = MIG_STATE_ERROR;
195     notifier_list_notify(&migration_state_notifiers, s);
196     migrate_fd_cleanup(s);
197 }
198
199 static void migrate_fd_completed(MigrationState *s)
200 {
201     DPRINTF("setting completed state\n");
202     if (migrate_fd_cleanup(s) < 0) {
203         s->state = MIG_STATE_ERROR;
204     } else {
205         s->state = MIG_STATE_COMPLETED;
206         runstate_set(RUN_STATE_POSTMIGRATE);
207     }
208     notifier_list_notify(&migration_state_notifiers, s);
209 }
210
211 static void migrate_fd_put_notify(void *opaque)
212 {
213     MigrationState *s = opaque;
214
215     qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
216     qemu_file_put_notify(s->file);
217     if (s->file && qemu_file_get_error(s->file)) {
218         migrate_fd_error(s);
219     }
220 }
221
222 static ssize_t migrate_fd_put_buffer(void *opaque, const void *data,
223                                      size_t size)
224 {
225     MigrationState *s = opaque;
226     ssize_t ret;
227
228     if (s->state != MIG_STATE_ACTIVE) {
229         return -EIO;
230     }
231
232     do {
233         ret = s->write(s, data, size);
234     } while (ret == -1 && ((s->get_error(s)) == EINTR));
235
236     if (ret == -1)
237         ret = -(s->get_error(s));
238
239     if (ret == -EAGAIN) {
240         qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
241     }
242
243     return ret;
244 }
245
246 static void migrate_fd_put_ready(void *opaque)
247 {
248     MigrationState *s = opaque;
249     int ret;
250
251     if (s->state != MIG_STATE_ACTIVE) {
252         DPRINTF("put_ready returning because of non-active state\n");
253         return;
254     }
255
256     DPRINTF("iterate\n");
257     ret = qemu_savevm_state_iterate(s->file);
258     if (ret < 0) {
259         migrate_fd_error(s);
260     } else if (ret == 1) {
261         int old_vm_running = runstate_is_running();
262
263         DPRINTF("done iterating\n");
264         qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
265         vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
266
267         if (qemu_savevm_state_complete(s->file) < 0) {
268             migrate_fd_error(s);
269         } else {
270             migrate_fd_completed(s);
271         }
272         s->total_time = qemu_get_clock_ms(rt_clock) - s->total_time;
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->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     qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
323     return s->close(s);
324 }
325
326 void add_migration_state_change_notifier(Notifier *notify)
327 {
328     notifier_list_add(&migration_state_notifiers, notify);
329 }
330
331 void remove_migration_state_change_notifier(Notifier *notify)
332 {
333     notifier_remove(notify);
334 }
335
336 bool migration_is_active(MigrationState *s)
337 {
338     return s->state == MIG_STATE_ACTIVE;
339 }
340
341 bool migration_has_finished(MigrationState *s)
342 {
343     return s->state == MIG_STATE_COMPLETED;
344 }
345
346 bool migration_has_failed(MigrationState *s)
347 {
348     return (s->state == MIG_STATE_CANCELLED ||
349             s->state == MIG_STATE_ERROR);
350 }
351
352 void migrate_fd_connect(MigrationState *s)
353 {
354     int ret;
355
356     s->state = MIG_STATE_ACTIVE;
357     s->file = qemu_fopen_ops_buffered(s,
358                                       s->bandwidth_limit,
359                                       migrate_fd_put_buffer,
360                                       migrate_fd_put_ready,
361                                       migrate_fd_wait_for_unfreeze,
362                                       migrate_fd_close);
363
364     DPRINTF("beginning savevm\n");
365     ret = qemu_savevm_state_begin(s->file, &s->params);
366     if (ret < 0) {
367         DPRINTF("failed, %d\n", ret);
368         migrate_fd_error(s);
369         return;
370     }
371     migrate_fd_put_ready(s);
372 }
373
374 static MigrationState *migrate_init(const MigrationParams *params)
375 {
376     MigrationState *s = migrate_get_current();
377     int64_t bandwidth_limit = s->bandwidth_limit;
378
379     memset(s, 0, sizeof(*s));
380     s->bandwidth_limit = bandwidth_limit;
381     s->params = *params;
382
383     s->bandwidth_limit = bandwidth_limit;
384     s->state = MIG_STATE_SETUP;
385     s->total_time = qemu_get_clock_ms(rt_clock);
386
387     return s;
388 }
389
390 static GSList *migration_blockers;
391
392 void migrate_add_blocker(Error *reason)
393 {
394     migration_blockers = g_slist_prepend(migration_blockers, reason);
395 }
396
397 void migrate_del_blocker(Error *reason)
398 {
399     migration_blockers = g_slist_remove(migration_blockers, reason);
400 }
401
402 void qmp_migrate(const char *uri, bool has_blk, bool blk,
403                  bool has_inc, bool inc, bool has_detach, bool detach,
404                  Error **errp)
405 {
406     MigrationState *s = migrate_get_current();
407     MigrationParams params;
408     const char *p;
409     int ret;
410
411     params.blk = blk;
412     params.shared = inc;
413
414     if (s->state == MIG_STATE_ACTIVE) {
415         error_set(errp, QERR_MIGRATION_ACTIVE);
416         return;
417     }
418
419     if (qemu_savevm_state_blocked(errp)) {
420         return;
421     }
422
423     if (migration_blockers) {
424         *errp = error_copy(migration_blockers->data);
425         return;
426     }
427
428     s = migrate_init(&params);
429
430     if (strstart(uri, "tcp:", &p)) {
431         ret = tcp_start_outgoing_migration(s, p, errp);
432 #if !defined(WIN32)
433     } else if (strstart(uri, "exec:", &p)) {
434         ret = exec_start_outgoing_migration(s, p);
435     } else if (strstart(uri, "unix:", &p)) {
436         ret = unix_start_outgoing_migration(s, p);
437     } else if (strstart(uri, "fd:", &p)) {
438         ret = fd_start_outgoing_migration(s, p);
439 #endif
440     } else {
441         error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
442         return;
443     }
444
445     if (ret < 0) {
446         if (!error_is_set(errp)) {
447             DPRINTF("migration failed: %s\n", strerror(-ret));
448             /* FIXME: we should return meaningful errors */
449             error_set(errp, QERR_UNDEFINED_ERROR);
450         }
451         return;
452     }
453
454     notifier_list_notify(&migration_state_notifiers, s);
455 }
456
457 void qmp_migrate_cancel(Error **errp)
458 {
459     migrate_fd_cancel(migrate_get_current());
460 }
461
462 void qmp_migrate_set_speed(int64_t value, Error **errp)
463 {
464     MigrationState *s;
465
466     if (value < 0) {
467         value = 0;
468     }
469
470     s = migrate_get_current();
471     s->bandwidth_limit = value;
472     qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
473 }
474
475 void qmp_migrate_set_downtime(double value, Error **errp)
476 {
477     value *= 1e9;
478     value = MAX(0, MIN(UINT64_MAX, value));
479     max_downtime = (uint64_t)value;
480 }
This page took 0.04962 seconds and 4 git commands to generate.