]> git.proxmox.com Git - mirror_qemu.git/blob - migration.c
monitor: Port handler_1 to use QDict
[mirror_qemu.git] / migration.c
1 /*
2 * QEMU live migration
3 *
4 * Copyright IBM, Corp. 2008
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
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
22 //#define DEBUG_MIGRATION
23
24 #ifdef DEBUG_MIGRATION
25 #define dprintf(fmt, ...) \
26 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
27 #else
28 #define dprintf(fmt, ...) \
29 do { } while (0)
30 #endif
31
32 /* Migration speed throttling */
33 static uint32_t max_throttle = (32 << 20);
34
35 static MigrationState *current_migration;
36
37 void qemu_start_incoming_migration(const char *uri)
38 {
39 const char *p;
40
41 if (strstart(uri, "tcp:", &p))
42 tcp_start_incoming_migration(p);
43 #if !defined(WIN32)
44 else if (strstart(uri, "exec:", &p))
45 exec_start_incoming_migration(p);
46 else if (strstart(uri, "unix:", &p))
47 unix_start_incoming_migration(p);
48 else if (strstart(uri, "fd:", &p))
49 fd_start_incoming_migration(p);
50 #endif
51 else
52 fprintf(stderr, "unknown migration protocol: %s\n", uri);
53 }
54
55 void do_migrate(Monitor *mon, int detach, const char *uri)
56 {
57 MigrationState *s = NULL;
58 const char *p;
59
60 if (strstart(uri, "tcp:", &p))
61 s = tcp_start_outgoing_migration(p, max_throttle, detach);
62 #if !defined(WIN32)
63 else if (strstart(uri, "exec:", &p))
64 s = exec_start_outgoing_migration(p, max_throttle, detach);
65 else if (strstart(uri, "unix:", &p))
66 s = unix_start_outgoing_migration(p, max_throttle, detach);
67 else if (strstart(uri, "fd:", &p))
68 s = fd_start_outgoing_migration(mon, p, max_throttle, detach);
69 #endif
70 else
71 monitor_printf(mon, "unknown migration protocol: %s\n", uri);
72
73 if (s == NULL)
74 monitor_printf(mon, "migration failed\n");
75 else {
76 if (current_migration)
77 current_migration->release(current_migration);
78
79 current_migration = s;
80 }
81 }
82
83 void do_migrate_cancel(Monitor *mon, const QDict *qdict)
84 {
85 MigrationState *s = current_migration;
86
87 if (s)
88 s->cancel(s);
89 }
90
91 void do_migrate_set_speed(Monitor *mon, const QDict *qdict)
92 {
93 double d;
94 char *ptr;
95 FdMigrationState *s;
96 const char *value = qdict_get_str(qdict, "value");
97
98 d = strtod(value, &ptr);
99 switch (*ptr) {
100 case 'G': case 'g':
101 d *= 1024;
102 case 'M': case 'm':
103 d *= 1024;
104 case 'K': case 'k':
105 d *= 1024;
106 default:
107 break;
108 }
109
110 max_throttle = (uint32_t)d;
111 s = migrate_to_fms(current_migration);
112
113 if (s) {
114 qemu_file_set_rate_limit(s->file, max_throttle);
115 }
116
117 }
118
119 /* amount of nanoseconds we are willing to wait for migration to be down.
120 * the choice of nanoseconds is because it is the maximum resolution that
121 * get_clock() can achieve. It is an internal measure. All user-visible
122 * units must be in seconds */
123 static uint64_t max_downtime = 30000000;
124
125 uint64_t migrate_max_downtime(void)
126 {
127 return max_downtime;
128 }
129
130 void do_migrate_set_downtime(Monitor *mon, const QDict *qdict)
131 {
132 char *ptr;
133 double d;
134 const char *value = qdict_get_str(qdict, "value");
135
136 d = strtod(value, &ptr);
137 if (!strcmp(ptr,"ms")) {
138 d *= 1000000;
139 } else if (!strcmp(ptr,"us")) {
140 d *= 1000;
141 } else if (!strcmp(ptr,"ns")) {
142 } else {
143 /* all else considered to be seconds */
144 d *= 1000000000;
145 }
146
147 max_downtime = (uint64_t)d;
148 }
149
150 void do_info_migrate(Monitor *mon)
151 {
152 MigrationState *s = current_migration;
153
154 if (s) {
155 monitor_printf(mon, "Migration status: ");
156 switch (s->get_status(s)) {
157 case MIG_STATE_ACTIVE:
158 monitor_printf(mon, "active\n");
159 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n", ram_bytes_transferred() >> 10);
160 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n", ram_bytes_remaining() >> 10);
161 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n", ram_bytes_total() >> 10);
162 break;
163 case MIG_STATE_COMPLETED:
164 monitor_printf(mon, "completed\n");
165 break;
166 case MIG_STATE_ERROR:
167 monitor_printf(mon, "failed\n");
168 break;
169 case MIG_STATE_CANCELLED:
170 monitor_printf(mon, "cancelled\n");
171 break;
172 }
173 }
174 }
175
176 /* shared migration helpers */
177
178 void migrate_fd_monitor_suspend(FdMigrationState *s)
179 {
180 s->mon_resume = cur_mon;
181 if (monitor_suspend(cur_mon) == 0)
182 dprintf("suspending monitor\n");
183 else
184 monitor_printf(cur_mon, "terminal does not allow synchronous "
185 "migration, continuing detached\n");
186 }
187
188 void migrate_fd_error(FdMigrationState *s)
189 {
190 dprintf("setting error state\n");
191 s->state = MIG_STATE_ERROR;
192 migrate_fd_cleanup(s);
193 }
194
195 void migrate_fd_cleanup(FdMigrationState *s)
196 {
197 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
198
199 if (s->file) {
200 dprintf("closing file\n");
201 qemu_fclose(s->file);
202 }
203
204 if (s->fd != -1)
205 close(s->fd);
206
207 /* Don't resume monitor until we've flushed all of the buffers */
208 if (s->mon_resume)
209 monitor_resume(s->mon_resume);
210
211 s->fd = -1;
212 }
213
214 void migrate_fd_put_notify(void *opaque)
215 {
216 FdMigrationState *s = opaque;
217
218 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
219 qemu_file_put_notify(s->file);
220 }
221
222 ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size)
223 {
224 FdMigrationState *s = opaque;
225 ssize_t ret;
226
227 do {
228 ret = s->write(s, data, size);
229 } while (ret == -1 && ((s->get_error(s)) == EINTR));
230
231 if (ret == -1)
232 ret = -(s->get_error(s));
233
234 if (ret == -EAGAIN)
235 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
236
237 return ret;
238 }
239
240 void migrate_fd_connect(FdMigrationState *s)
241 {
242 int ret;
243
244 s->file = qemu_fopen_ops_buffered(s,
245 s->bandwidth_limit,
246 migrate_fd_put_buffer,
247 migrate_fd_put_ready,
248 migrate_fd_wait_for_unfreeze,
249 migrate_fd_close);
250
251 dprintf("beginning savevm\n");
252 ret = qemu_savevm_state_begin(s->file);
253 if (ret < 0) {
254 dprintf("failed, %d\n", ret);
255 migrate_fd_error(s);
256 return;
257 }
258
259 migrate_fd_put_ready(s);
260 }
261
262 void migrate_fd_put_ready(void *opaque)
263 {
264 FdMigrationState *s = opaque;
265
266 if (s->state != MIG_STATE_ACTIVE) {
267 dprintf("put_ready returning because of non-active state\n");
268 return;
269 }
270
271 dprintf("iterate\n");
272 if (qemu_savevm_state_iterate(s->file) == 1) {
273 int state;
274 int old_vm_running = vm_running;
275
276 dprintf("done iterating\n");
277 vm_stop(0);
278
279 qemu_aio_flush();
280 bdrv_flush_all();
281 if ((qemu_savevm_state_complete(s->file)) < 0) {
282 if (old_vm_running) {
283 vm_start();
284 }
285 state = MIG_STATE_ERROR;
286 } else {
287 state = MIG_STATE_COMPLETED;
288 }
289 migrate_fd_cleanup(s);
290 s->state = state;
291 }
292 }
293
294 int migrate_fd_get_status(MigrationState *mig_state)
295 {
296 FdMigrationState *s = migrate_to_fms(mig_state);
297 return s->state;
298 }
299
300 void migrate_fd_cancel(MigrationState *mig_state)
301 {
302 FdMigrationState *s = migrate_to_fms(mig_state);
303
304 if (s->state != MIG_STATE_ACTIVE)
305 return;
306
307 dprintf("cancelling migration\n");
308
309 s->state = MIG_STATE_CANCELLED;
310
311 migrate_fd_cleanup(s);
312 }
313
314 void migrate_fd_release(MigrationState *mig_state)
315 {
316 FdMigrationState *s = migrate_to_fms(mig_state);
317
318 dprintf("releasing state\n");
319
320 if (s->state == MIG_STATE_ACTIVE) {
321 s->state = MIG_STATE_CANCELLED;
322 migrate_fd_cleanup(s);
323 }
324 free(s);
325 }
326
327 void migrate_fd_wait_for_unfreeze(void *opaque)
328 {
329 FdMigrationState *s = opaque;
330 int ret;
331
332 dprintf("wait for unfreeze\n");
333 if (s->state != MIG_STATE_ACTIVE)
334 return;
335
336 do {
337 fd_set wfds;
338
339 FD_ZERO(&wfds);
340 FD_SET(s->fd, &wfds);
341
342 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
343 } while (ret == -1 && (s->get_error(s)) == EINTR);
344 }
345
346 int migrate_fd_close(void *opaque)
347 {
348 FdMigrationState *s = opaque;
349
350 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
351 return s->close(s);
352 }