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