]>
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); | |
46 | #endif | |
34c9dd8e AL |
47 | else |
48 | fprintf(stderr, "unknown migration protocol: %s\n", uri); | |
5bb7910a AL |
49 | } |
50 | ||
376253ec | 51 | void do_migrate(Monitor *mon, int detach, const char *uri) |
5bb7910a | 52 | { |
34c9dd8e AL |
53 | MigrationState *s = NULL; |
54 | const char *p; | |
55 | ||
56 | if (strstart(uri, "tcp:", &p)) | |
ff8d81d8 | 57 | s = tcp_start_outgoing_migration(p, max_throttle, detach); |
065e2813 AL |
58 | #if !defined(WIN32) |
59 | else if (strstart(uri, "exec:", &p)) | |
60 | s = exec_start_outgoing_migration(p, max_throttle, detach); | |
61 | #endif | |
34c9dd8e | 62 | else |
376253ec | 63 | monitor_printf(mon, "unknown migration protocol: %s\n", uri); |
34c9dd8e AL |
64 | |
65 | if (s == NULL) | |
376253ec | 66 | monitor_printf(mon, "migration failed\n"); |
34c9dd8e | 67 | else { |
ff8d81d8 AL |
68 | if (current_migration) |
69 | current_migration->release(current_migration); | |
34c9dd8e | 70 | |
ff8d81d8 | 71 | current_migration = s; |
34c9dd8e | 72 | } |
5bb7910a AL |
73 | } |
74 | ||
376253ec | 75 | void do_migrate_cancel(Monitor *mon) |
5bb7910a AL |
76 | { |
77 | MigrationState *s = current_migration; | |
78 | ||
79 | if (s) | |
ff8d81d8 | 80 | s->cancel(s); |
5bb7910a AL |
81 | } |
82 | ||
376253ec | 83 | void do_migrate_set_speed(Monitor *mon, const char *value) |
5bb7910a AL |
84 | { |
85 | double d; | |
86 | char *ptr; | |
87 | ||
88 | d = strtod(value, &ptr); | |
89 | switch (*ptr) { | |
90 | case 'G': case 'g': | |
ff8d81d8 | 91 | d *= 1024; |
5bb7910a | 92 | case 'M': case 'm': |
ff8d81d8 | 93 | d *= 1024; |
5bb7910a | 94 | case 'K': case 'k': |
ff8d81d8 | 95 | d *= 1024; |
5bb7910a | 96 | default: |
ff8d81d8 | 97 | break; |
5bb7910a AL |
98 | } |
99 | ||
100 | max_throttle = (uint32_t)d; | |
101 | } | |
102 | ||
376253ec | 103 | void do_info_migrate(Monitor *mon) |
5bb7910a AL |
104 | { |
105 | MigrationState *s = current_migration; | |
376253ec | 106 | |
5bb7910a | 107 | if (s) { |
376253ec | 108 | monitor_printf(mon, "Migration status: "); |
ff8d81d8 AL |
109 | switch (s->get_status(s)) { |
110 | case MIG_STATE_ACTIVE: | |
376253ec | 111 | monitor_printf(mon, "active\n"); |
ff8d81d8 AL |
112 | break; |
113 | case MIG_STATE_COMPLETED: | |
376253ec | 114 | monitor_printf(mon, "completed\n"); |
ff8d81d8 AL |
115 | break; |
116 | case MIG_STATE_ERROR: | |
376253ec | 117 | monitor_printf(mon, "failed\n"); |
ff8d81d8 AL |
118 | break; |
119 | case MIG_STATE_CANCELLED: | |
376253ec | 120 | monitor_printf(mon, "cancelled\n"); |
ff8d81d8 AL |
121 | break; |
122 | } | |
5bb7910a AL |
123 | } |
124 | } | |
125 | ||
065e2813 AL |
126 | /* shared migration helpers */ |
127 | ||
731b0364 AL |
128 | void migrate_fd_monitor_suspend(FdMigrationState *s) |
129 | { | |
130 | s->mon_resume = cur_mon; | |
cde76ee1 AL |
131 | if (monitor_suspend(cur_mon) == 0) |
132 | dprintf("suspending monitor\n"); | |
133 | else | |
134 | monitor_printf(cur_mon, "terminal does not allow synchronous " | |
135 | "migration, continuing detached\n"); | |
731b0364 AL |
136 | } |
137 | ||
065e2813 AL |
138 | void migrate_fd_error(FdMigrationState *s) |
139 | { | |
140 | dprintf("setting error state\n"); | |
141 | s->state = MIG_STATE_ERROR; | |
142 | migrate_fd_cleanup(s); | |
143 | } | |
144 | ||
145 | void migrate_fd_cleanup(FdMigrationState *s) | |
146 | { | |
147 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); | |
148 | ||
149 | if (s->file) { | |
150 | dprintf("closing file\n"); | |
151 | qemu_fclose(s->file); | |
152 | } | |
153 | ||
154 | if (s->fd != -1) | |
155 | close(s->fd); | |
156 | ||
157 | /* Don't resume monitor until we've flushed all of the buffers */ | |
731b0364 AL |
158 | if (s->mon_resume) |
159 | monitor_resume(s->mon_resume); | |
065e2813 AL |
160 | |
161 | s->fd = -1; | |
162 | } | |
163 | ||
164 | void migrate_fd_put_notify(void *opaque) | |
165 | { | |
166 | FdMigrationState *s = opaque; | |
167 | ||
168 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); | |
169 | qemu_file_put_notify(s->file); | |
170 | } | |
171 | ||
172 | ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size) | |
173 | { | |
174 | FdMigrationState *s = opaque; | |
175 | ssize_t ret; | |
176 | ||
177 | do { | |
178 | ret = s->write(s, data, size); | |
179 | } while (ret == -1 && ((s->get_error(s)) == EINTR || (s->get_error(s)) == EWOULDBLOCK)); | |
180 | ||
181 | if (ret == -1) | |
182 | ret = -(s->get_error(s)); | |
183 | ||
184 | if (ret == -EAGAIN) | |
185 | qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s); | |
186 | ||
187 | return ret; | |
188 | } | |
189 | ||
190 | void migrate_fd_connect(FdMigrationState *s) | |
191 | { | |
192 | int ret; | |
193 | ||
194 | s->file = qemu_fopen_ops_buffered(s, | |
195 | s->bandwidth_limit, | |
196 | migrate_fd_put_buffer, | |
197 | migrate_fd_put_ready, | |
198 | migrate_fd_wait_for_unfreeze, | |
199 | migrate_fd_close); | |
200 | ||
201 | dprintf("beginning savevm\n"); | |
202 | ret = qemu_savevm_state_begin(s->file); | |
203 | if (ret < 0) { | |
204 | dprintf("failed, %d\n", ret); | |
205 | migrate_fd_error(s); | |
206 | return; | |
207 | } | |
208 | ||
209 | migrate_fd_put_ready(s); | |
210 | } | |
211 | ||
212 | void migrate_fd_put_ready(void *opaque) | |
213 | { | |
214 | FdMigrationState *s = opaque; | |
215 | ||
216 | if (s->state != MIG_STATE_ACTIVE) { | |
217 | dprintf("put_ready returning because of non-active state\n"); | |
218 | return; | |
219 | } | |
220 | ||
221 | dprintf("iterate\n"); | |
222 | if (qemu_savevm_state_iterate(s->file) == 1) { | |
b161d123 | 223 | int state; |
065e2813 AL |
224 | dprintf("done iterating\n"); |
225 | vm_stop(0); | |
226 | ||
227 | bdrv_flush_all(); | |
b161d123 AL |
228 | if ((qemu_savevm_state_complete(s->file)) < 0) { |
229 | vm_start(); | |
230 | state = MIG_STATE_ERROR; | |
231 | } else { | |
232 | state = MIG_STATE_COMPLETED; | |
233 | } | |
065e2813 | 234 | migrate_fd_cleanup(s); |
b161d123 | 235 | s->state = state; |
065e2813 AL |
236 | } |
237 | } | |
238 | ||
239 | int migrate_fd_get_status(MigrationState *mig_state) | |
240 | { | |
241 | FdMigrationState *s = migrate_to_fms(mig_state); | |
242 | return s->state; | |
243 | } | |
244 | ||
245 | void migrate_fd_cancel(MigrationState *mig_state) | |
246 | { | |
247 | FdMigrationState *s = migrate_to_fms(mig_state); | |
248 | ||
249 | if (s->state != MIG_STATE_ACTIVE) | |
250 | return; | |
251 | ||
252 | dprintf("cancelling migration\n"); | |
253 | ||
254 | s->state = MIG_STATE_CANCELLED; | |
255 | ||
256 | migrate_fd_cleanup(s); | |
257 | } | |
258 | ||
259 | void migrate_fd_release(MigrationState *mig_state) | |
260 | { | |
261 | FdMigrationState *s = migrate_to_fms(mig_state); | |
262 | ||
263 | dprintf("releasing state\n"); | |
264 | ||
265 | if (s->state == MIG_STATE_ACTIVE) { | |
266 | s->state = MIG_STATE_CANCELLED; | |
267 | migrate_fd_cleanup(s); | |
268 | } | |
269 | free(s); | |
270 | } | |
271 | ||
272 | void migrate_fd_wait_for_unfreeze(void *opaque) | |
273 | { | |
274 | FdMigrationState *s = opaque; | |
275 | int ret; | |
276 | ||
277 | dprintf("wait for unfreeze\n"); | |
278 | if (s->state != MIG_STATE_ACTIVE) | |
279 | return; | |
280 | ||
281 | do { | |
282 | fd_set wfds; | |
283 | ||
284 | FD_ZERO(&wfds); | |
285 | FD_SET(s->fd, &wfds); | |
286 | ||
287 | ret = select(s->fd + 1, NULL, &wfds, NULL, NULL); | |
288 | } while (ret == -1 && (s->get_error(s)) == EINTR); | |
289 | } | |
290 | ||
291 | int migrate_fd_close(void *opaque) | |
292 | { | |
293 | FdMigrationState *s = opaque; | |
294 | return s->close(s); | |
295 | } |