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