]> git.proxmox.com Git - qemu.git/blob - migration.c
Merge branch 'memory/core' of git://git.kernel.org/pub/scm/virt/kvm/qemu-kvm
[qemu.git] / migration.c
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 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
14 */
15
16 #include "qemu-common.h"
17 #include "migration.h"
18 #include "monitor.h"
19 #include "buffered_file.h"
20 #include "sysemu.h"
21 #include "block.h"
22 #include "qemu_socket.h"
23 #include "block-migration.h"
24 #include "qmp-commands.h"
25
26 //#define DEBUG_MIGRATION
27
28 #ifdef DEBUG_MIGRATION
29 #define DPRINTF(fmt, ...) \
30 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
31 #else
32 #define DPRINTF(fmt, ...) \
33 do { } while (0)
34 #endif
35
36 enum {
37 MIG_STATE_ERROR,
38 MIG_STATE_SETUP,
39 MIG_STATE_CANCELLED,
40 MIG_STATE_ACTIVE,
41 MIG_STATE_COMPLETED,
42 };
43
44 #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
45
46 static NotifierList migration_state_notifiers =
47 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
48
49 /* When we add fault tolerance, we could have several
50 migrations at once. For now we don't need to add
51 dynamic creation of migration */
52
53 static MigrationState *migrate_get_current(void)
54 {
55 static MigrationState current_migration = {
56 .state = MIG_STATE_SETUP,
57 .bandwidth_limit = MAX_THROTTLE,
58 };
59
60 return &current_migration;
61 }
62
63 int qemu_start_incoming_migration(const char *uri)
64 {
65 const char *p;
66 int ret;
67
68 if (strstart(uri, "tcp:", &p))
69 ret = tcp_start_incoming_migration(p);
70 #if !defined(WIN32)
71 else if (strstart(uri, "exec:", &p))
72 ret = exec_start_incoming_migration(p);
73 else if (strstart(uri, "unix:", &p))
74 ret = unix_start_incoming_migration(p);
75 else if (strstart(uri, "fd:", &p))
76 ret = fd_start_incoming_migration(p);
77 #endif
78 else {
79 fprintf(stderr, "unknown migration protocol: %s\n", uri);
80 ret = -EPROTONOSUPPORT;
81 }
82 return ret;
83 }
84
85 void process_incoming_migration(QEMUFile *f)
86 {
87 if (qemu_loadvm_state(f) < 0) {
88 fprintf(stderr, "load of migration failed\n");
89 exit(0);
90 }
91 qemu_announce_self();
92 DPRINTF("successfully loaded vm state\n");
93
94 /* Make sure all file formats flush their mutable metadata */
95 bdrv_invalidate_cache_all();
96
97 if (autostart) {
98 vm_start();
99 } else {
100 runstate_set(RUN_STATE_PRELAUNCH);
101 }
102 }
103
104 /* amount of nanoseconds we are willing to wait for migration to be down.
105 * the choice of nanoseconds is because it is the maximum resolution that
106 * get_clock() can achieve. It is an internal measure. All user-visible
107 * units must be in seconds */
108 static uint64_t max_downtime = 30000000;
109
110 uint64_t migrate_max_downtime(void)
111 {
112 return max_downtime;
113 }
114
115 MigrationInfo *qmp_query_migrate(Error **errp)
116 {
117 MigrationInfo *info = g_malloc0(sizeof(*info));
118 MigrationState *s = migrate_get_current();
119
120 switch (s->state) {
121 case MIG_STATE_SETUP:
122 /* no migration has happened ever */
123 break;
124 case MIG_STATE_ACTIVE:
125 info->has_status = true;
126 info->status = g_strdup("active");
127
128 info->has_ram = true;
129 info->ram = g_malloc0(sizeof(*info->ram));
130 info->ram->transferred = ram_bytes_transferred();
131 info->ram->remaining = ram_bytes_remaining();
132 info->ram->total = ram_bytes_total();
133
134 if (blk_mig_active()) {
135 info->has_disk = true;
136 info->disk = g_malloc0(sizeof(*info->disk));
137 info->disk->transferred = blk_mig_bytes_transferred();
138 info->disk->remaining = blk_mig_bytes_remaining();
139 info->disk->total = blk_mig_bytes_total();
140 }
141 break;
142 case MIG_STATE_COMPLETED:
143 info->has_status = true;
144 info->status = g_strdup("completed");
145 break;
146 case MIG_STATE_ERROR:
147 info->has_status = true;
148 info->status = g_strdup("failed");
149 break;
150 case MIG_STATE_CANCELLED:
151 info->has_status = true;
152 info->status = g_strdup("cancelled");
153 break;
154 }
155
156 return info;
157 }
158
159 /* shared migration helpers */
160
161 static int migrate_fd_cleanup(MigrationState *s)
162 {
163 int ret = 0;
164
165 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
166
167 if (s->file) {
168 DPRINTF("closing file\n");
169 ret = qemu_fclose(s->file);
170 s->file = NULL;
171 }
172
173 if (s->fd != -1) {
174 close(s->fd);
175 s->fd = -1;
176 }
177
178 return ret;
179 }
180
181 void migrate_fd_error(MigrationState *s)
182 {
183 DPRINTF("setting error state\n");
184 s->state = MIG_STATE_ERROR;
185 notifier_list_notify(&migration_state_notifiers, s);
186 migrate_fd_cleanup(s);
187 }
188
189 static void migrate_fd_completed(MigrationState *s)
190 {
191 DPRINTF("setting completed state\n");
192 if (migrate_fd_cleanup(s) < 0) {
193 s->state = MIG_STATE_ERROR;
194 } else {
195 s->state = MIG_STATE_COMPLETED;
196 runstate_set(RUN_STATE_POSTMIGRATE);
197 }
198 notifier_list_notify(&migration_state_notifiers, s);
199 }
200
201 static void migrate_fd_put_notify(void *opaque)
202 {
203 MigrationState *s = opaque;
204
205 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
206 qemu_file_put_notify(s->file);
207 if (s->file && qemu_file_get_error(s->file)) {
208 migrate_fd_error(s);
209 }
210 }
211
212 static ssize_t migrate_fd_put_buffer(void *opaque, const void *data,
213 size_t size)
214 {
215 MigrationState *s = opaque;
216 ssize_t ret;
217
218 if (s->state != MIG_STATE_ACTIVE) {
219 return -EIO;
220 }
221
222 do {
223 ret = s->write(s, data, size);
224 } while (ret == -1 && ((s->get_error(s)) == EINTR));
225
226 if (ret == -1)
227 ret = -(s->get_error(s));
228
229 if (ret == -EAGAIN) {
230 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
231 }
232
233 return ret;
234 }
235
236 static void migrate_fd_put_ready(void *opaque)
237 {
238 MigrationState *s = opaque;
239 int ret;
240
241 if (s->state != MIG_STATE_ACTIVE) {
242 DPRINTF("put_ready returning because of non-active state\n");
243 return;
244 }
245
246 DPRINTF("iterate\n");
247 ret = qemu_savevm_state_iterate(s->file);
248 if (ret < 0) {
249 migrate_fd_error(s);
250 } else if (ret == 1) {
251 int old_vm_running = runstate_is_running();
252
253 DPRINTF("done iterating\n");
254 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
255
256 if (qemu_savevm_state_complete(s->file) < 0) {
257 migrate_fd_error(s);
258 } else {
259 migrate_fd_completed(s);
260 }
261 if (s->state != MIG_STATE_COMPLETED) {
262 if (old_vm_running) {
263 vm_start();
264 }
265 }
266 }
267 }
268
269 static void migrate_fd_cancel(MigrationState *s)
270 {
271 if (s->state != MIG_STATE_ACTIVE)
272 return;
273
274 DPRINTF("cancelling migration\n");
275
276 s->state = MIG_STATE_CANCELLED;
277 notifier_list_notify(&migration_state_notifiers, s);
278 qemu_savevm_state_cancel(s->file);
279
280 migrate_fd_cleanup(s);
281 }
282
283 static void migrate_fd_wait_for_unfreeze(void *opaque)
284 {
285 MigrationState *s = opaque;
286 int ret;
287
288 DPRINTF("wait for unfreeze\n");
289 if (s->state != MIG_STATE_ACTIVE)
290 return;
291
292 do {
293 fd_set wfds;
294
295 FD_ZERO(&wfds);
296 FD_SET(s->fd, &wfds);
297
298 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
299 } while (ret == -1 && (s->get_error(s)) == EINTR);
300
301 if (ret == -1) {
302 qemu_file_set_error(s->file, -s->get_error(s));
303 }
304 }
305
306 static int migrate_fd_close(void *opaque)
307 {
308 MigrationState *s = opaque;
309
310 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
311 return s->close(s);
312 }
313
314 void add_migration_state_change_notifier(Notifier *notify)
315 {
316 notifier_list_add(&migration_state_notifiers, notify);
317 }
318
319 void remove_migration_state_change_notifier(Notifier *notify)
320 {
321 notifier_remove(notify);
322 }
323
324 bool migration_is_active(MigrationState *s)
325 {
326 return s->state == MIG_STATE_ACTIVE;
327 }
328
329 bool migration_has_finished(MigrationState *s)
330 {
331 return s->state == MIG_STATE_COMPLETED;
332 }
333
334 bool migration_has_failed(MigrationState *s)
335 {
336 return (s->state == MIG_STATE_CANCELLED ||
337 s->state == MIG_STATE_ERROR);
338 }
339
340 void migrate_fd_connect(MigrationState *s)
341 {
342 int ret;
343
344 s->state = MIG_STATE_ACTIVE;
345 s->file = qemu_fopen_ops_buffered(s,
346 s->bandwidth_limit,
347 migrate_fd_put_buffer,
348 migrate_fd_put_ready,
349 migrate_fd_wait_for_unfreeze,
350 migrate_fd_close);
351
352 DPRINTF("beginning savevm\n");
353 ret = qemu_savevm_state_begin(s->file, s->blk, s->shared);
354 if (ret < 0) {
355 DPRINTF("failed, %d\n", ret);
356 migrate_fd_error(s);
357 return;
358 }
359 migrate_fd_put_ready(s);
360 }
361
362 static MigrationState *migrate_init(int blk, int inc)
363 {
364 MigrationState *s = migrate_get_current();
365 int64_t bandwidth_limit = s->bandwidth_limit;
366
367 memset(s, 0, sizeof(*s));
368 s->bandwidth_limit = bandwidth_limit;
369 s->blk = blk;
370 s->shared = inc;
371
372 s->bandwidth_limit = bandwidth_limit;
373 s->state = MIG_STATE_SETUP;
374
375 return s;
376 }
377
378 static GSList *migration_blockers;
379
380 void migrate_add_blocker(Error *reason)
381 {
382 migration_blockers = g_slist_prepend(migration_blockers, reason);
383 }
384
385 void migrate_del_blocker(Error *reason)
386 {
387 migration_blockers = g_slist_remove(migration_blockers, reason);
388 }
389
390 void qmp_migrate(const char *uri, bool has_blk, bool blk,
391 bool has_inc, bool inc, bool has_detach, bool detach,
392 Error **errp)
393 {
394 MigrationState *s = migrate_get_current();
395 const char *p;
396 int ret;
397
398 if (s->state == MIG_STATE_ACTIVE) {
399 error_set(errp, QERR_MIGRATION_ACTIVE);
400 return;
401 }
402
403 if (qemu_savevm_state_blocked(errp)) {
404 return;
405 }
406
407 if (migration_blockers) {
408 *errp = error_copy(migration_blockers->data);
409 return;
410 }
411
412 s = migrate_init(blk, inc);
413
414 if (strstart(uri, "tcp:", &p)) {
415 ret = tcp_start_outgoing_migration(s, p);
416 #if !defined(WIN32)
417 } else if (strstart(uri, "exec:", &p)) {
418 ret = exec_start_outgoing_migration(s, p);
419 } else if (strstart(uri, "unix:", &p)) {
420 ret = unix_start_outgoing_migration(s, p);
421 } else if (strstart(uri, "fd:", &p)) {
422 ret = fd_start_outgoing_migration(s, p);
423 #endif
424 } else {
425 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
426 return;
427 }
428
429 if (ret < 0) {
430 DPRINTF("migration failed: %s\n", strerror(-ret));
431 /* FIXME: we should return meaningful errors */
432 error_set(errp, QERR_UNDEFINED_ERROR);
433 return;
434 }
435
436 notifier_list_notify(&migration_state_notifiers, s);
437 }
438
439 void qmp_migrate_cancel(Error **errp)
440 {
441 migrate_fd_cancel(migrate_get_current());
442 }
443
444 void qmp_migrate_set_speed(int64_t value, Error **errp)
445 {
446 MigrationState *s;
447
448 if (value < 0) {
449 value = 0;
450 }
451
452 s = migrate_get_current();
453 s->bandwidth_limit = value;
454 qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
455 }
456
457 void qmp_migrate_set_downtime(double value, Error **errp)
458 {
459 value *= 1e9;
460 value = MAX(0, MIN(UINT64_MAX, value));
461 max_downtime = (uint64_t)value;
462 }