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