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