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