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