Update irqs on reset and device load
[qemu/hppa.git] / migration.c
blob401383cf0aee45ca2e8505db5a160ea96a2053b8
1 /*
2 * QEMU live migration
4 * Copyright IBM, Corp. 2008
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
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"
22 //#define DEBUG_MIGRATION
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
32 /* Migration speed throttling */
33 static uint32_t max_throttle = (32 << 20);
35 static MigrationState *current_migration;
37 void qemu_start_incoming_migration(const char *uri)
39 const char *p;
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);
51 void do_migrate(Monitor *mon, int detach, const char *uri)
53 MigrationState *s = NULL;
54 const char *p;
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);
65 if (s == NULL)
66 monitor_printf(mon, "migration failed\n");
67 else {
68 if (current_migration)
69 current_migration->release(current_migration);
71 current_migration = s;
75 void do_migrate_cancel(Monitor *mon)
77 MigrationState *s = current_migration;
79 if (s)
80 s->cancel(s);
83 void do_migrate_set_speed(Monitor *mon, const char *value)
85 double d;
86 char *ptr;
87 FdMigrationState *s;
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;
101 max_throttle = (uint32_t)d;
102 s = migrate_to_fms(current_migration);
104 if (s) {
105 qemu_file_set_rate_limit(s->file, max_throttle);
110 void do_info_migrate(Monitor *mon)
112 MigrationState *s = current_migration;
114 if (s) {
115 monitor_printf(mon, "Migration status: ");
116 switch (s->get_status(s)) {
117 case MIG_STATE_ACTIVE:
118 monitor_printf(mon, "active\n");
119 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n", ram_bytes_transferred() >> 10);
120 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n", ram_bytes_remaining() >> 10);
121 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n", ram_bytes_total() >> 10);
122 break;
123 case MIG_STATE_COMPLETED:
124 monitor_printf(mon, "completed\n");
125 break;
126 case MIG_STATE_ERROR:
127 monitor_printf(mon, "failed\n");
128 break;
129 case MIG_STATE_CANCELLED:
130 monitor_printf(mon, "cancelled\n");
131 break;
136 /* shared migration helpers */
138 void migrate_fd_monitor_suspend(FdMigrationState *s)
140 s->mon_resume = cur_mon;
141 if (monitor_suspend(cur_mon) == 0)
142 dprintf("suspending monitor\n");
143 else
144 monitor_printf(cur_mon, "terminal does not allow synchronous "
145 "migration, continuing detached\n");
148 void migrate_fd_error(FdMigrationState *s)
150 dprintf("setting error state\n");
151 s->state = MIG_STATE_ERROR;
152 migrate_fd_cleanup(s);
155 void migrate_fd_cleanup(FdMigrationState *s)
157 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
159 if (s->file) {
160 dprintf("closing file\n");
161 qemu_fclose(s->file);
164 if (s->fd != -1)
165 close(s->fd);
167 /* Don't resume monitor until we've flushed all of the buffers */
168 if (s->mon_resume)
169 monitor_resume(s->mon_resume);
171 s->fd = -1;
174 void migrate_fd_put_notify(void *opaque)
176 FdMigrationState *s = opaque;
178 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
179 qemu_file_put_notify(s->file);
182 ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size)
184 FdMigrationState *s = opaque;
185 ssize_t ret;
187 do {
188 ret = s->write(s, data, size);
189 } while (ret == -1 && ((s->get_error(s)) == EINTR));
191 if (ret == -1)
192 ret = -(s->get_error(s));
194 if (ret == -EAGAIN)
195 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
197 return ret;
200 void migrate_fd_connect(FdMigrationState *s)
202 int ret;
204 s->file = qemu_fopen_ops_buffered(s,
205 s->bandwidth_limit,
206 migrate_fd_put_buffer,
207 migrate_fd_put_ready,
208 migrate_fd_wait_for_unfreeze,
209 migrate_fd_close);
211 dprintf("beginning savevm\n");
212 ret = qemu_savevm_state_begin(s->file);
213 if (ret < 0) {
214 dprintf("failed, %d\n", ret);
215 migrate_fd_error(s);
216 return;
219 migrate_fd_put_ready(s);
222 void migrate_fd_put_ready(void *opaque)
224 FdMigrationState *s = opaque;
226 if (s->state != MIG_STATE_ACTIVE) {
227 dprintf("put_ready returning because of non-active state\n");
228 return;
231 dprintf("iterate\n");
232 if (qemu_savevm_state_iterate(s->file) == 1) {
233 int state;
234 dprintf("done iterating\n");
235 vm_stop(0);
237 bdrv_flush_all();
238 if ((qemu_savevm_state_complete(s->file)) < 0) {
239 vm_start();
240 state = MIG_STATE_ERROR;
241 } else {
242 state = MIG_STATE_COMPLETED;
244 migrate_fd_cleanup(s);
245 s->state = state;
249 int migrate_fd_get_status(MigrationState *mig_state)
251 FdMigrationState *s = migrate_to_fms(mig_state);
252 return s->state;
255 void migrate_fd_cancel(MigrationState *mig_state)
257 FdMigrationState *s = migrate_to_fms(mig_state);
259 if (s->state != MIG_STATE_ACTIVE)
260 return;
262 dprintf("cancelling migration\n");
264 s->state = MIG_STATE_CANCELLED;
266 migrate_fd_cleanup(s);
269 void migrate_fd_release(MigrationState *mig_state)
271 FdMigrationState *s = migrate_to_fms(mig_state);
273 dprintf("releasing state\n");
275 if (s->state == MIG_STATE_ACTIVE) {
276 s->state = MIG_STATE_CANCELLED;
277 migrate_fd_cleanup(s);
279 free(s);
282 void migrate_fd_wait_for_unfreeze(void *opaque)
284 FdMigrationState *s = opaque;
285 int ret;
287 dprintf("wait for unfreeze\n");
288 if (s->state != MIG_STATE_ACTIVE)
289 return;
291 do {
292 fd_set wfds;
294 FD_ZERO(&wfds);
295 FD_SET(s->fd, &wfds);
297 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
298 } while (ret == -1 && (s->get_error(s)) == EINTR);
301 int migrate_fd_close(void *opaque)
303 FdMigrationState *s = opaque;
304 return s->close(s);