]> Git Repo - qemu.git/blob - migration.c
RunState: Add additional states
[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 "qemu-objects.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 /* Migration speed throttling */
35 static int64_t max_throttle = (32 << 20);
36
37 static MigrationState *current_migration;
38
39 static NotifierList migration_state_notifiers =
40     NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
41
42 int qemu_start_incoming_migration(const char *uri)
43 {
44     const char *p;
45     int ret;
46
47     if (strstart(uri, "tcp:", &p))
48         ret = tcp_start_incoming_migration(p);
49 #if !defined(WIN32)
50     else if (strstart(uri, "exec:", &p))
51         ret =  exec_start_incoming_migration(p);
52     else if (strstart(uri, "unix:", &p))
53         ret = unix_start_incoming_migration(p);
54     else if (strstart(uri, "fd:", &p))
55         ret = fd_start_incoming_migration(p);
56 #endif
57     else {
58         fprintf(stderr, "unknown migration protocol: %s\n", uri);
59         ret = -EPROTONOSUPPORT;
60     }
61     return ret;
62 }
63
64 void process_incoming_migration(QEMUFile *f)
65 {
66     if (qemu_loadvm_state(f) < 0) {
67         fprintf(stderr, "load of migration failed\n");
68         exit(0);
69     }
70     qemu_announce_self();
71     DPRINTF("successfully loaded vm state\n");
72
73     incoming_expected = false;
74
75     if (autostart) {
76         vm_start();
77     } else {
78         runstate_set(RSTATE_PRE_LAUNCH);
79     }
80 }
81
82 int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
83 {
84     MigrationState *s = NULL;
85     const char *p;
86     int detach = qdict_get_try_bool(qdict, "detach", 0);
87     int blk = qdict_get_try_bool(qdict, "blk", 0);
88     int inc = qdict_get_try_bool(qdict, "inc", 0);
89     const char *uri = qdict_get_str(qdict, "uri");
90
91     if (current_migration &&
92         current_migration->get_status(current_migration) == MIG_STATE_ACTIVE) {
93         monitor_printf(mon, "migration already in progress\n");
94         return -1;
95     }
96
97     if (qemu_savevm_state_blocked(mon)) {
98         return -1;
99     }
100
101     if (strstart(uri, "tcp:", &p)) {
102         s = tcp_start_outgoing_migration(mon, p, max_throttle, detach,
103                                          blk, inc);
104 #if !defined(WIN32)
105     } else if (strstart(uri, "exec:", &p)) {
106         s = exec_start_outgoing_migration(mon, p, max_throttle, detach,
107                                           blk, inc);
108     } else if (strstart(uri, "unix:", &p)) {
109         s = unix_start_outgoing_migration(mon, p, max_throttle, detach,
110                                           blk, inc);
111     } else if (strstart(uri, "fd:", &p)) {
112         s = fd_start_outgoing_migration(mon, p, max_throttle, detach, 
113                                         blk, inc);
114 #endif
115     } else {
116         monitor_printf(mon, "unknown migration protocol: %s\n", uri);
117         return -1;
118     }
119
120     if (s == NULL) {
121         monitor_printf(mon, "migration failed\n");
122         return -1;
123     }
124
125     if (current_migration) {
126         current_migration->release(current_migration);
127     }
128
129     current_migration = s;
130     notifier_list_notify(&migration_state_notifiers, NULL);
131     return 0;
132 }
133
134 int do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data)
135 {
136     MigrationState *s = current_migration;
137
138     if (s)
139         s->cancel(s);
140
141     return 0;
142 }
143
144 int do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data)
145 {
146     int64_t d;
147     FdMigrationState *s;
148
149     d = qdict_get_int(qdict, "value");
150     if (d < 0) {
151         d = 0;
152     }
153     max_throttle = d;
154
155     s = migrate_to_fms(current_migration);
156     if (s && s->file) {
157         qemu_file_set_rate_limit(s->file, max_throttle);
158     }
159
160     return 0;
161 }
162
163 /* amount of nanoseconds we are willing to wait for migration to be down.
164  * the choice of nanoseconds is because it is the maximum resolution that
165  * get_clock() can achieve. It is an internal measure. All user-visible
166  * units must be in seconds */
167 static uint64_t max_downtime = 30000000;
168
169 uint64_t migrate_max_downtime(void)
170 {
171     return max_downtime;
172 }
173
174 int do_migrate_set_downtime(Monitor *mon, const QDict *qdict,
175                             QObject **ret_data)
176 {
177     double d;
178
179     d = qdict_get_double(qdict, "value") * 1e9;
180     d = MAX(0, MIN(UINT64_MAX, d));
181     max_downtime = (uint64_t)d;
182
183     return 0;
184 }
185
186 static void migrate_print_status(Monitor *mon, const char *name,
187                                  const QDict *status_dict)
188 {
189     QDict *qdict;
190
191     qdict = qobject_to_qdict(qdict_get(status_dict, name));
192
193     monitor_printf(mon, "transferred %s: %" PRIu64 " kbytes\n", name,
194                         qdict_get_int(qdict, "transferred") >> 10);
195     monitor_printf(mon, "remaining %s: %" PRIu64 " kbytes\n", name,
196                         qdict_get_int(qdict, "remaining") >> 10);
197     monitor_printf(mon, "total %s: %" PRIu64 " kbytes\n", name,
198                         qdict_get_int(qdict, "total") >> 10);
199 }
200
201 void do_info_migrate_print(Monitor *mon, const QObject *data)
202 {
203     QDict *qdict;
204
205     qdict = qobject_to_qdict(data);
206
207     monitor_printf(mon, "Migration status: %s\n",
208                    qdict_get_str(qdict, "status"));
209
210     if (qdict_haskey(qdict, "ram")) {
211         migrate_print_status(mon, "ram", qdict);
212     }
213
214     if (qdict_haskey(qdict, "disk")) {
215         migrate_print_status(mon, "disk", qdict);
216     }
217 }
218
219 static void migrate_put_status(QDict *qdict, const char *name,
220                                uint64_t trans, uint64_t rem, uint64_t total)
221 {
222     QObject *obj;
223
224     obj = qobject_from_jsonf("{ 'transferred': %" PRId64 ", "
225                                "'remaining': %" PRId64 ", "
226                                "'total': %" PRId64 " }", trans, rem, total);
227     qdict_put_obj(qdict, name, obj);
228 }
229
230 void do_info_migrate(Monitor *mon, QObject **ret_data)
231 {
232     QDict *qdict;
233     MigrationState *s = current_migration;
234
235     if (s) {
236         switch (s->get_status(s)) {
237         case MIG_STATE_ACTIVE:
238             qdict = qdict_new();
239             qdict_put(qdict, "status", qstring_from_str("active"));
240
241             migrate_put_status(qdict, "ram", ram_bytes_transferred(),
242                                ram_bytes_remaining(), ram_bytes_total());
243
244             if (blk_mig_active()) {
245                 migrate_put_status(qdict, "disk", blk_mig_bytes_transferred(),
246                                    blk_mig_bytes_remaining(),
247                                    blk_mig_bytes_total());
248             }
249
250             *ret_data = QOBJECT(qdict);
251             break;
252         case MIG_STATE_COMPLETED:
253             *ret_data = qobject_from_jsonf("{ 'status': 'completed' }");
254             break;
255         case MIG_STATE_ERROR:
256             *ret_data = qobject_from_jsonf("{ 'status': 'failed' }");
257             break;
258         case MIG_STATE_CANCELLED:
259             *ret_data = qobject_from_jsonf("{ 'status': 'cancelled' }");
260             break;
261         }
262     }
263 }
264
265 /* shared migration helpers */
266
267 void migrate_fd_monitor_suspend(FdMigrationState *s, Monitor *mon)
268 {
269     s->mon = mon;
270     if (monitor_suspend(mon) == 0) {
271         DPRINTF("suspending monitor\n");
272     } else {
273         monitor_printf(mon, "terminal does not allow synchronous "
274                        "migration, continuing detached\n");
275     }
276 }
277
278 void migrate_fd_error(FdMigrationState *s)
279 {
280     DPRINTF("setting error state\n");
281     s->state = MIG_STATE_ERROR;
282     notifier_list_notify(&migration_state_notifiers, NULL);
283     migrate_fd_cleanup(s);
284 }
285
286 int migrate_fd_cleanup(FdMigrationState *s)
287 {
288     int ret = 0;
289
290     qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
291
292     if (s->file) {
293         DPRINTF("closing file\n");
294         if (qemu_fclose(s->file) != 0) {
295             ret = -1;
296         }
297         s->file = NULL;
298     } else {
299         if (s->mon) {
300             monitor_resume(s->mon);
301         }
302     }
303
304     if (s->fd != -1) {
305         close(s->fd);
306         s->fd = -1;
307     }
308
309     return ret;
310 }
311
312 void migrate_fd_put_notify(void *opaque)
313 {
314     FdMigrationState *s = opaque;
315
316     qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
317     qemu_file_put_notify(s->file);
318 }
319
320 ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size)
321 {
322     FdMigrationState *s = opaque;
323     ssize_t ret;
324
325     do {
326         ret = s->write(s, data, size);
327     } while (ret == -1 && ((s->get_error(s)) == EINTR));
328
329     if (ret == -1)
330         ret = -(s->get_error(s));
331
332     if (ret == -EAGAIN) {
333         qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
334     } else if (ret < 0) {
335         s->state = MIG_STATE_ERROR;
336         notifier_list_notify(&migration_state_notifiers, NULL);
337     }
338
339     return ret;
340 }
341
342 void migrate_fd_connect(FdMigrationState *s)
343 {
344     int ret;
345
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->mon, s->file, s->mig_state.blk,
355                                   s->mig_state.shared);
356     if (ret < 0) {
357         DPRINTF("failed, %d\n", ret);
358         migrate_fd_error(s);
359         return;
360     }
361     
362     migrate_fd_put_ready(s);
363 }
364
365 void migrate_fd_put_ready(void *opaque)
366 {
367     FdMigrationState *s = opaque;
368
369     if (s->state != MIG_STATE_ACTIVE) {
370         DPRINTF("put_ready returning because of non-active state\n");
371         return;
372     }
373
374     DPRINTF("iterate\n");
375     if (qemu_savevm_state_iterate(s->mon, s->file) == 1) {
376         int state;
377         int old_vm_running = vm_running;
378
379         DPRINTF("done iterating\n");
380         vm_stop(RSTATE_PRE_MIGRATE);
381
382         if ((qemu_savevm_state_complete(s->mon, s->file)) < 0) {
383             if (old_vm_running) {
384                 vm_start();
385             }
386             state = MIG_STATE_ERROR;
387         } else {
388             state = MIG_STATE_COMPLETED;
389         }
390         if (migrate_fd_cleanup(s) < 0) {
391             if (old_vm_running) {
392                 vm_start();
393             }
394             state = MIG_STATE_ERROR;
395         }
396         if (state == MIG_STATE_COMPLETED) {
397             runstate_set(RSTATE_POST_MIGRATE);
398         }
399         s->state = state;
400         notifier_list_notify(&migration_state_notifiers, NULL);
401     }
402 }
403
404 int migrate_fd_get_status(MigrationState *mig_state)
405 {
406     FdMigrationState *s = migrate_to_fms(mig_state);
407     return s->state;
408 }
409
410 void migrate_fd_cancel(MigrationState *mig_state)
411 {
412     FdMigrationState *s = migrate_to_fms(mig_state);
413
414     if (s->state != MIG_STATE_ACTIVE)
415         return;
416
417     DPRINTF("cancelling migration\n");
418
419     s->state = MIG_STATE_CANCELLED;
420     notifier_list_notify(&migration_state_notifiers, NULL);
421     qemu_savevm_state_cancel(s->mon, s->file);
422
423     migrate_fd_cleanup(s);
424 }
425
426 void migrate_fd_release(MigrationState *mig_state)
427 {
428     FdMigrationState *s = migrate_to_fms(mig_state);
429
430     DPRINTF("releasing state\n");
431    
432     if (s->state == MIG_STATE_ACTIVE) {
433         s->state = MIG_STATE_CANCELLED;
434         notifier_list_notify(&migration_state_notifiers, NULL);
435         migrate_fd_cleanup(s);
436     }
437     g_free(s);
438 }
439
440 void migrate_fd_wait_for_unfreeze(void *opaque)
441 {
442     FdMigrationState *s = opaque;
443     int ret;
444
445     DPRINTF("wait for unfreeze\n");
446     if (s->state != MIG_STATE_ACTIVE)
447         return;
448
449     do {
450         fd_set wfds;
451
452         FD_ZERO(&wfds);
453         FD_SET(s->fd, &wfds);
454
455         ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
456     } while (ret == -1 && (s->get_error(s)) == EINTR);
457 }
458
459 int migrate_fd_close(void *opaque)
460 {
461     FdMigrationState *s = opaque;
462
463     if (s->mon) {
464         monitor_resume(s->mon);
465     }
466     qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
467     return s->close(s);
468 }
469
470 void add_migration_state_change_notifier(Notifier *notify)
471 {
472     notifier_list_add(&migration_state_notifiers, notify);
473 }
474
475 void remove_migration_state_change_notifier(Notifier *notify)
476 {
477     notifier_list_remove(&migration_state_notifiers, notify);
478 }
479
480 int get_migration_state(void)
481 {
482     if (current_migration) {
483         return migrate_fd_get_status(current_migration);
484     } else {
485         return MIG_STATE_ERROR;
486     }
487 }
This page took 0.050559 seconds and 4 git commands to generate.