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