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