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