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