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