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