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