Cleanup and improve kvm_load/save_registers usage
[qemu-kvm/fedora.git] / migration.c
bloba64a28744e89e3fc3f9bb0319f2ae3e0b703b375
1 /*
2 * QEMU migration support
3 *
4 * Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "hw/hw.h"
26 #include "sysemu.h"
27 #include "block.h"
28 #include "console.h"
29 #include "qemu-timer.h"
30 #include "migration.h"
31 #include "qemu_socket.h"
32 #include "qemu-kvm.h"
34 #include <sys/wait.h>
36 #define MIN_FINALIZE_SIZE (200 << 10)
37 #define MAX_ITERATIONS 30
38 #define MAX_RAPID_WRITES 5
40 typedef struct MigrationState
42 int fd;
43 int throttle_count;
44 int bps;
45 int updated_pages;
46 int last_updated_pages;
47 int iteration;
48 int n_buffer; /* number of bytes in @buffer already sent */
49 int l_buffer; /* number of bytes in @buffer to send */
50 int throttled;
51 int *has_error;
52 char buffer[TARGET_PAGE_SIZE + 4 + 4];
53 target_ulong addr;
54 QEMUTimer *timer;
55 void *opaque;
56 int detach;
57 int (*release)(void *opaque);
58 int rapid_writes;
59 } MigrationState;
61 static uint32_t max_throttle = (32 << 20);
62 static MigrationState *current_migration;
63 static int wait_for_message_timeout = 3000; /* 3 seconds */
64 static int status; /* last migration status */
66 enum { /* migration status values */
67 MIG_STAT_SUCCESS = 0,
69 /* src error codes */
70 MIG_STAT_INVALID_PARAMS = 1,
71 MIG_STAT_INVALID_ADDR = 2,
72 MIG_STAT_SOCKET_FAILED = 3,
73 MIG_STAT_SOCKOPT_FAILED = 4,
74 MIG_STAT_BIND_FAILED = 5,
75 MIG_STAT_LISTEN_FAILED = 6,
76 MIG_STAT_ACCEPT_FAILED = 7,
77 MIG_STAT_CONNECT_FAILED = 8,
78 MIG_STAT_WRITE_FAILED = 9,
79 MIG_STAT_READ_FAILED = 10,
80 MIG_STAT_CONNECTION_CLOSED = 11,
81 MIG_STAT_SELECT_FAILED = 12,
82 MIG_STAT_SELECT_TIMEOUT = 13,
83 MIG_STAT_SELECT_FD_NOT_SET = 14,
85 MIG_STAT_SAVEVM_FAILED = 15,
86 MIG_STAT_NO_MEM = 16,
88 MIG_STAT_MIGRATION_CANCEL = 20,
90 /* kvm error codes (on src) */
91 MIG_STAT_KVM_UPDATE_DIRTY_PAGES_LOG_FAILED = 101,
92 MIG_STAT_KVM_SET_DIRTY_TRACKING_FAILED = 102,
93 MIG_STAT_KVM_GET_PAGE_BITMAP = 103,
95 /* dst error codes */
96 MIG_STAT_DST_INVALID_PARAMS = 200 + MIG_STAT_INVALID_PARAMS,
97 MIG_STAT_DST_INVALID_ADDR = 200 + MIG_STAT_INVALID_ADDR,
98 MIG_STAT_DST_SOCKET_FAILED = 200 + MIG_STAT_SOCKET_FAILED,
99 MIG_STAT_DST_SOCKOPT_FAILED = 200 + MIG_STAT_SOCKOPT_FAILED,
100 MIG_STAT_DST_BIND_FAILED = 200 + MIG_STAT_BIND_FAILED,
101 MIG_STAT_DST_LISTEN_FAILED = 200 + MIG_STAT_LISTEN_FAILED,
102 MIG_STAT_DST_ACCEPT_FAILED = 200 + MIG_STAT_ACCEPT_FAILED,
103 MIG_STAT_DST_CONNECT_FAILED = 200 + MIG_STAT_CONNECT_FAILED,
104 MIG_STAT_DST_WRITE_FAILED = 200 + MIG_STAT_WRITE_FAILED,
105 MIG_STAT_DST_READ_FAILED = 200 + MIG_STAT_READ_FAILED,
107 MIG_STAT_DST_CONNECTION_CLOSED = 200 + MIG_STAT_CONNECTION_CLOSED,
108 MIG_STAT_DST_SELECT_FAILED = 200 + MIG_STAT_SELECT_FAILED,
109 MIG_STAT_DST_SELECT_TIMEOUT = 200 + MIG_STAT_SELECT_TIMEOUT,
110 MIG_STAT_DST_SELECT_FD_NOT_SET = 200 + MIG_STAT_SELECT_FD_NOT_SET,
112 MIG_STAT_DST_LOADVM_FAILED = 200 + MIG_STAT_SAVEVM_FAILED,
113 MIG_STAT_DST_NO_MEM = 200 + MIG_STAT_NO_MEM,
115 MIG_STAT_DST_GET_PAGE_FAILED = 230,
116 MIG_STAT_DST_GET_PAGE_UNKNOWN_TYPE = 231,
117 MIG_STAT_DST_MEM_SIZE_MISMATCH = 232,
118 MIG_STAT_DST_MEM_OUT_OF_RANGE = 233,
121 //#define MIGRATION_VERIFY
122 #ifdef MIGRATION_VERIFY
123 static int save_verify_memory(QEMUFile *f, void *opaque);
124 static int load_verify_memory(QEMUFile *f, void *opaque, int version_id);
125 #endif /* MIGRATION_VERIFY */
127 /* QEMUFile migration implementation */
129 static void migrate_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
131 MigrationState *s = opaque;
132 int offset = 0;
134 if (*s->has_error)
135 return;
137 while (offset < size) {
138 ssize_t len;
140 len = write(s->fd, buf + offset, size - offset);
141 if (len == -1) {
142 if (errno == EAGAIN || errno == EINTR)
143 continue;
144 term_printf("migration: write failed (%s)\n", strerror(errno));
145 *s->has_error = MIG_STAT_WRITE_FAILED;
146 break;
147 } else if (len == 0) {
148 term_printf("migration: other side closed connection\n");
149 *s->has_error = MIG_STAT_CONNECTION_CLOSED;
150 break;
153 offset += len;
157 static void migrate_close(void *opaque)
159 MigrationState *s = opaque;
161 if (s->release)
162 s->release(s->opaque);
164 qemu_free(s);
165 current_migration = NULL;
168 /* Outgoing migration routines */
169 static void migrate_finish(MigrationState *s)
171 QEMUFile *f;
172 int ret = 0;
173 int *has_error = s->has_error;
174 int saved_vm_running = vm_running;
175 int detach = s->detach;
177 fcntl(s->fd, F_SETFL, 0);
179 if (! *has_error) {
180 vm_stop(0);
181 f = qemu_fopen(s, migrate_put_buffer, NULL, migrate_close);
182 do {
183 qemu_aio_flush();
184 } while (qemu_bh_poll());
185 bdrv_flush_all();
187 if (kvm_enabled() && !*s->has_error && kvm_update_dirty_pages_log())
188 *s->has_error = MIG_STAT_KVM_UPDATE_DIRTY_PAGES_LOG_FAILED;
190 qemu_put_be32(f, 1);
191 ret = qemu_live_savevm_state(f);
192 #ifdef MIGRATION_VERIFY
193 save_verify_memory(f, NULL);
194 #endif /* MIGRATION_VERIFY */
195 qemu_fclose(f);
196 } else {
197 migrate_close(s);
199 status = *has_error;
200 if (ret && !status)
201 status = MIG_STAT_SAVEVM_FAILED;
202 if (status) {
203 term_printf("Migration failed! ret=%d error=%d\n", ret, *has_error);
204 if (saved_vm_running)
205 vm_start();
207 if (!detach)
208 monitor_resume();
209 qemu_free(has_error);
210 cpu_physical_memory_set_dirty_tracking(0);
213 static int migrate_write_buffer(MigrationState *s)
215 if (*s->has_error)
216 return 0;
218 if (s->n_buffer < s->l_buffer) {
219 ssize_t len;
220 again:
221 len = write(s->fd, s->buffer + s->n_buffer, s->l_buffer - s->n_buffer);
222 if (len == -1) {
223 if (errno == EINTR)
224 goto again;
225 if (errno == EAGAIN)
226 return 1;
227 *s->has_error = MIG_STAT_WRITE_FAILED;
228 return 0;
230 if (len == 0) {
231 *s->has_error = MIG_STAT_CONNECTION_CLOSED;
232 return 0;
235 s->throttle_count += len;
236 s->n_buffer += len;
237 if (s->n_buffer < s->l_buffer)
238 goto again;
241 if (s->throttle_count > max_throttle) {
242 s->throttled = 1;
243 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
244 return 1;
247 return 0;
250 static int migrate_check_convergence(MigrationState *s)
252 target_ulong addr;
253 int dirty_count = 0;
255 if ((s->iteration >= MAX_ITERATIONS) ||
256 (s->rapid_writes >= MAX_RAPID_WRITES) ) {
257 return 1;
260 for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
261 if (kvm_enabled() && (addr>=0xa0000) && (addr<0xc0000)) /* do not access video-addresses */
262 continue;
263 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
264 dirty_count++;
267 return ((dirty_count * TARGET_PAGE_SIZE) < MIN_FINALIZE_SIZE);
270 static int ram_page_is_homogeneous(uint32_t addr)
272 int i, n;
273 uint32_t *p, v;
275 n = TARGET_PAGE_SIZE / sizeof(v);
276 p = (uint32 *)(phys_ram_base + addr);
278 v = p[0];
279 for (i=1; i<n; i++)
280 if (p[i] != v)
281 return 0;
282 return 1;
285 static void migrate_prepare_page(MigrationState *s)
287 uint32_t value;
288 char type;
289 const char *buff;
290 int bufflen;
292 value = cpu_to_be32(s->addr);
293 memcpy(s->buffer, &value, 4);
295 if (ram_page_is_homogeneous(s->addr)) {
296 type = 1; /* keeping ram_get_page() values */
297 bufflen = 4;
299 else {
300 type = 0;
301 bufflen = TARGET_PAGE_SIZE;
304 buff = phys_ram_base + s->addr;
305 s->buffer[4] = type;
306 memcpy(s->buffer + 4 + 1, phys_ram_base + s->addr, bufflen);
307 s->n_buffer = 0;
308 s->l_buffer = 4 + 1 + bufflen;
311 static void migrate_write(void *opaque)
313 MigrationState *s = opaque;
315 if (migrate_write_buffer(s))
316 return;
318 if (kvm_enabled() && !*s->has_error && kvm_update_dirty_pages_log())
319 *s->has_error = MIG_STAT_KVM_UPDATE_DIRTY_PAGES_LOG_FAILED;
321 if (migrate_check_convergence(s) || *s->has_error) {
322 qemu_del_timer(s->timer);
323 qemu_free_timer(s->timer);
324 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
325 migrate_finish(s);
326 return;
329 while (s->addr < phys_ram_size) {
330 if (kvm_enabled() && (s->addr>=0xa0000) && (s->addr<0xc0000)) /* do not access video-addresses */
331 s->addr = 0xc0000;
333 if (cpu_physical_memory_get_dirty(s->addr, MIGRATION_DIRTY_FLAG)) {
334 migrate_prepare_page(s);
335 cpu_physical_memory_reset_dirty(s->addr, s->addr + TARGET_PAGE_SIZE, MIGRATION_DIRTY_FLAG);
337 s->addr += TARGET_PAGE_SIZE;
339 s->updated_pages++;
341 if (migrate_write_buffer(s))
342 return;
343 } else
344 s->addr += TARGET_PAGE_SIZE;
347 if ((s->iteration) && (s->last_updated_pages <= s->updated_pages)) {
348 s->rapid_writes++; /* "dirt-speed" is faster than transfer speed */
349 if (max_throttle < (1 << 30))
350 max_throttle *= 2; /* try harder */
352 s->last_updated_pages = s->updated_pages;
353 s->updated_pages = 0;
354 s->addr = 0;
355 s->iteration++;
358 static void migrate_reset_throttle(void *opaque)
360 MigrationState *s = opaque;
362 s->bps = s->throttle_count;
364 if (s->throttled) {
365 s->throttled = 0;
366 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_write, s);
368 s->throttle_count = 0;
369 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
372 static int write_whole_buffer(int fd, const void *buff, size_t size)
374 size_t offset = 0, len;
376 while (offset < size) {
377 len = write(fd, buff + offset, size - offset);
378 if (len == -1 && errno == EINTR)
379 continue;
380 if (len < 1)
381 return -EIO;
383 offset += len;
385 return !(offset == size); /* returns 0 on success */
388 static int bit_is_set(int bit, unsigned char *map)
390 return map[bit/8] & (1 << (bit%8));
393 static int start_migration(MigrationState *s)
395 uint32_t value = cpu_to_be32(phys_ram_size);
396 target_phys_addr_t addr;
397 int r;
398 unsigned char running = vm_running?2:1; /* 1 + vm_running */
399 int n = 0;
400 unsigned char *phys_ram_page_exist_bitmap = NULL;
402 if (kvm_enabled()) {
403 n = BITMAP_SIZE(phys_ram_size);
404 phys_ram_page_exist_bitmap = qemu_malloc(n);
405 if (!phys_ram_page_exist_bitmap) {
406 perror("failed to allocate page bitmap");
407 r = MIG_STAT_NO_MEM;
408 goto out;
410 if (kvm_get_phys_ram_page_bitmap(phys_ram_page_exist_bitmap)) {
411 r = MIG_STAT_KVM_GET_PAGE_BITMAP;
412 perror("kvm_get_mem_map failed");
413 goto out;
417 r = MIG_STAT_WRITE_FAILED;
418 if (write_whole_buffer(s->fd, &running, sizeof(running))) {
419 perror("vm_running write failed");
420 goto out;
422 if (write_whole_buffer(s->fd, &value, sizeof(value))) {
423 perror("phys_ram_size write failed");
424 goto out;
427 if (kvm_enabled()) {
428 value = cpu_to_be32(n);
429 if (write_whole_buffer(s->fd, &value, sizeof(value))) {
430 perror("phys_ram_size_bitmap size write failed");
431 goto out;
433 if (write_whole_buffer(s->fd, phys_ram_page_exist_bitmap, n)) {
434 perror("phys_ram_page_exist_bitmap write failed");
435 goto out;
439 fcntl(s->fd, F_SETFL, O_NONBLOCK);
441 for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
442 if (kvm_enabled() && !bit_is_set(addr>>TARGET_PAGE_BITS, phys_ram_page_exist_bitmap)) {
443 cpu_physical_memory_reset_dirty(addr,
444 addr + TARGET_PAGE_SIZE,
445 MIGRATION_DIRTY_FLAG);
446 continue;
448 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
449 cpu_physical_memory_set_dirty(addr);
452 if (cpu_physical_memory_set_dirty_tracking(1)) {
453 *s->has_error = MIG_STAT_KVM_SET_DIRTY_TRACKING_FAILED;
454 r = MIG_STAT_KVM_SET_DIRTY_TRACKING_FAILED;
455 goto out;
458 s->addr = 0;
459 s->iteration = 0;
460 s->updated_pages = 0;
461 s->last_updated_pages = 0;
462 s->n_buffer = s->l_buffer = 0;
463 s->rapid_writes = 0;
464 s->timer = qemu_new_timer(rt_clock, migrate_reset_throttle, s);
466 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock));
467 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_write, s);
469 r = 0;
471 out:
472 if (kvm_enabled() && phys_ram_page_exist_bitmap)
473 qemu_free(phys_ram_page_exist_bitmap);
474 return r;
477 static MigrationState *migration_init_fd(int detach, int fd)
479 MigrationState *s;
481 s = qemu_mallocz(sizeof(MigrationState));
482 if (s == NULL) {
483 term_printf("Allocation error\n");
484 return NULL;
487 s->fd = fd;
488 s->has_error = qemu_mallocz(sizeof(int));
489 if (s->has_error == NULL) {
490 term_printf("malloc failed (for has_error)\n");
491 return NULL;
493 s->detach = detach;
495 current_migration = s;
497 if (start_migration(s)) {
498 term_printf("Could not start migration\n");
499 migrate_finish(s);
500 return NULL;
503 if (!detach)
504 monitor_suspend();
506 return s;
509 typedef struct MigrationCmdState
511 int fd;
512 pid_t pid;
513 } MigrationCmdState;
515 static int cmd_release(void *opaque)
517 MigrationCmdState *c = opaque;
518 int status, ret;
520 close(c->fd);
522 again:
523 ret = waitpid(c->pid, &status, 0);
524 if (ret == -1 && errno == EINTR)
525 goto again;
527 if (ret == -1) {
528 term_printf("migration: waitpid failed (%s)\n", strerror(errno));
529 return -1;
531 /* FIXME: check and uncomment
532 * if (WIFEXITED(status))
533 * status = WEXITSTATUS(status);
535 return status;
538 static MigrationState *migration_init_cmd(int detach, const char *command, char **argv)
540 int fds[2];
541 pid_t pid;
542 int i;
543 MigrationState *s;
545 if (pipe(fds) == -1) {
546 term_printf("pipe() (%s)\n", strerror(errno));
547 return NULL;
550 pid = fork();
551 if (pid == -1) {
552 close(fds[0]);
553 close(fds[1]);
554 term_printf("fork error (%s)\n", strerror(errno));
555 return NULL;
557 if (pid == 0) {
558 close(fds[1]);
559 dup2(fds[0], STDIN_FILENO);
560 execvp(command, argv);
561 exit(1);
562 } else
563 close(fds[0]);
565 for (i = 0; argv[i]; i++)
566 qemu_free(argv[i]);
567 qemu_free(argv);
569 s = migration_init_fd(detach, fds[1]);
570 if (s) {
571 MigrationCmdState *c = qemu_mallocz(sizeof(*c));
572 c->pid = pid;
573 c->fd = fds[1];
574 s->release = cmd_release;
575 s->opaque = c;
578 return s;
581 static MigrationState *migration_init_exec(int detach, const char *command)
583 char **argv = NULL;
585 argv = qemu_mallocz(sizeof(char *) * 4);
586 argv[0] = strdup("sh");
587 argv[1] = strdup("-c");
588 argv[2] = strdup(command);
589 argv[3] = NULL;
591 return migration_init_cmd(detach, "/bin/sh", argv);
595 static int file_release(void *opaque)
597 int fd = (int)(long)opaque;
598 close(fd);
599 return 0;
602 static MigrationState *migration_init_file(int detach, const char *filename)
604 int fd;
605 MigrationState *s;
607 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
608 if (fd == -1) {
609 term_printf("open file %s failed (%s)\n", filename, strerror(errno));
610 return NULL;
613 s = migration_init_fd(detach, fd);
614 if (s) {
615 s->release = file_release;
616 s->opaque = (void*)(long)fd;
617 max_throttle = 1U << 31;
620 return s;
623 static MigrationState *migration_init_ssh(int detach, const char *host)
625 int qemu_argc, daemonize = 0, argc, i;
626 char **qemu_argv, **argv;
627 const char *incoming = NULL;
629 qemu_get_launch_info(&qemu_argc, &qemu_argv, &daemonize, &incoming);
631 argc = 3 + qemu_argc;
632 if (!daemonize)
633 argc++;
634 if (!incoming)
635 argc+=2;
637 argv = qemu_mallocz(sizeof(char *) * (argc + 1));
638 argv[0] = strdup("ssh");
639 argv[1] = strdup("-XC");
640 argv[2] = strdup(host);
642 for (i = 0; i < qemu_argc; i++)
643 argv[3 + i] = strdup(qemu_argv[i]);
645 if (!daemonize)
646 argv[3 + i++] = strdup("-daemonize");
647 if (!incoming) {
648 argv[3 + i++] = strdup("-incoming");
649 argv[3 + i++] = strdup("stdio");
652 argv[3 + i] = NULL;
654 return migration_init_cmd(detach, "ssh", argv);
657 /* (busy) wait timeout (miliseconds) for a message to arrive on fd. */
658 /* returns 0 on success error_code otherwise (18 for timeout) */
659 static int wait_for_message(const char *msg, int fd, int timeout)
661 fd_set rfds;
662 struct timeval tv;
663 int64_t now, expiration, delta; /* milliseconds */
664 int n = 0;
666 now = qemu_get_clock(rt_clock);
667 expiration = now + timeout;
668 do {
669 FD_ZERO(&rfds);
670 FD_SET(fd, &rfds);
671 tv.tv_sec = tv.tv_usec = 0;
672 now = qemu_get_clock(rt_clock);
673 delta = expiration - now;
674 if (delta > 0)
675 tv.tv_usec = delta * 1000;
676 n = select(fd + 1, &rfds, NULL, NULL, &tv);
677 } while ( (n == -1) && (errno == EINTR) );
679 switch (n) {
680 case -1: /* error */
681 fprintf(stderr, "%s FAILED: ", msg);
682 perror("");
683 return MIG_STAT_SELECT_FAILED;
684 case 0: /* timeout */
685 fprintf(stderr, "%s: timeout reached\n", msg);
686 return MIG_STAT_SELECT_TIMEOUT;
687 case 1:
688 break;
689 default:
690 fprintf(stderr, "wait_for_message: %s: select returned %d\n", msg, n);
692 if (!FD_ISSET(fd, &rfds)) {
693 fprintf(stderr, "wait_for_message: %s: s->fd not set\n", msg);
694 return MIG_STAT_SELECT_FD_NOT_SET;
697 return 0;
700 static int tcp_release(void *opaque)
702 MigrationState *s = opaque;
703 uint8_t status = 0;
704 ssize_t len = 0;
705 int n;
707 if (*s->has_error)
708 goto out;
710 n = wait_for_message("WAIT FOR ACK", s->fd, wait_for_message_timeout);
711 if (n) {
712 *s->has_error = n;
713 goto out;
716 wait_for_ack:
717 len = read(s->fd, &status, 1);
718 if (len == -1 && errno == EINTR)
719 goto wait_for_ack;
720 if (len != 1 || status != 0) {
721 *s->has_error = MIG_STAT_READ_FAILED;
722 fprintf(stderr, "migration: wait_for_ack: read error l=%zu s=%d(%s)\n",
723 len, status, strerror(errno));
724 goto out;
727 send_go:
728 len = write(s->fd, &status, 1);
729 if (len == -1 && errno == EINTR)
730 goto send_go;
731 if (len != 1) {
732 fprintf(stderr, "migration: send_go: write error l=%zu (%s)\n",
733 len, strerror(errno));
734 *s->has_error = MIG_STAT_WRITE_FAILED;
737 out:
738 close(s->fd);
740 return (len != 1 || status != 0);
743 static MigrationState *migration_init_tcp(int detach, const char *host)
745 int fd;
746 struct sockaddr_in addr;
747 MigrationState *s;
749 fd = socket(PF_INET, SOCK_STREAM, 0);
750 if (fd == -1) {
751 term_printf("socket() failed %s\n", strerror(errno));
752 status = MIG_STAT_SOCKET_FAILED;
753 return NULL;
756 addr.sin_family = AF_INET;
757 if (parse_host_port(&addr, host) == -1) {
758 term_printf("parse_host_port() FAILED for %s\n", host);
759 close(fd);
760 status = MIG_STAT_INVALID_ADDR;
761 return NULL;
764 again:
765 if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
766 if (errno == EINTR)
767 goto again;
768 term_printf("connect() failed %s\n", strerror(errno));
769 close(fd);
770 status = MIG_STAT_CONNECT_FAILED;
771 return NULL;
774 s = migration_init_fd(detach, fd);
775 if (s) {
776 s->opaque = s;
777 s->release = tcp_release;
779 return s;
782 /* Incoming migration */
784 static void migrate_incoming_homogeneous_page(uint32_t addr, uint32_t v)
786 int i, n;
787 uint32_t *p;
789 n = TARGET_PAGE_SIZE / sizeof(v);
790 p = (uint32 *)(phys_ram_base + addr);
792 for (i=0; i<n; i++)
793 p[i] = v;
796 static int migrate_incoming_page(QEMUFile *f, uint32_t addr)
798 int l, v, ret = 0;
800 switch (qemu_get_byte(f)) {
801 case 0: /* the whole page */
802 l = qemu_get_buffer(f, phys_ram_base + addr, TARGET_PAGE_SIZE);
803 if (l != TARGET_PAGE_SIZE)
804 ret = MIG_STAT_DST_GET_PAGE_FAILED;
805 break;
806 case 1: /* homogeneous page -- a single byte */
807 l = qemu_get_buffer(f, (void*)&v, 4);
808 if (l != 4)
809 ret = MIG_STAT_DST_GET_PAGE_FAILED;
810 migrate_incoming_homogeneous_page(addr, v);
811 break;
812 default:
813 ret = MIG_STAT_DST_GET_PAGE_UNKNOWN_TYPE;
816 return ret;
819 static int migrate_incoming_fd(int fd)
821 int ret = 0;
822 QEMUFile *f = qemu_fopen_fd(fd);
823 uint32_t addr, size;
824 extern void qemu_announce_self(void);
825 unsigned char running;
827 running = qemu_get_byte(f);
828 if ((running != 1) && (running != 2)) {
829 fprintf(stderr, "migration: illegal running value %u, not (1 or 2)\n",
830 running);
831 return MIG_STAT_DST_MEM_SIZE_MISMATCH;
833 autostart = running - 1;
835 size = qemu_get_be32(f);
836 if (size != phys_ram_size) {
837 fprintf(stderr, "migration: memory size mismatch: recv %u mine %llu\n",
838 size, (unsigned long long)phys_ram_size);
839 return MIG_STAT_DST_MEM_SIZE_MISMATCH;
842 if (kvm_enabled()) {
843 int n, m;
844 unsigned char *phys_ram_page_exist_bitmap = NULL;
846 /* allocate memory bitmap */
847 n = BITMAP_SIZE(phys_ram_size);
848 m = qemu_get_be32(f);
849 if (m > n) // allocate enough space
850 n = m;
851 phys_ram_page_exist_bitmap = qemu_malloc(n);
852 if (!phys_ram_page_exist_bitmap) {
853 perror("failed to allocate page bitmap");
854 return MIG_STAT_NO_MEM;
857 /* receive memory bitmap */
858 qemu_get_buffer(f, phys_ram_page_exist_bitmap, m);
860 /* FIXME: free dellocated-at-src guest memory pages */
862 qemu_free(phys_ram_page_exist_bitmap);
865 do {
866 addr = qemu_get_be32(f);
867 if (addr == 1)
868 break;
869 if (addr >= phys_ram_size)
870 return MIG_STAT_DST_MEM_OUT_OF_RANGE;
871 ret = migrate_incoming_page(f, addr);
872 if (ret)
873 return ret;
874 } while (1);
877 qemu_aio_flush();
878 vm_stop(0);
879 if (qemu_live_loadvm_state(f))
880 ret = MIG_STAT_DST_LOADVM_FAILED;
881 #ifdef MIGRATION_VERIFY
882 if (ret==0) ret=load_verify_memory(f, NULL, 1);
883 #endif /* MIGRATION_VERIFY */
884 qemu_fclose(f);
886 return ret;
889 static int migrate_incoming_tcp(const char *host)
891 struct sockaddr_in addr;
892 socklen_t addrlen = sizeof(addr);
893 int fd, sfd;
894 ssize_t len;
895 uint8_t status = 0;
896 int reuse = 1;
897 int rc;
899 addr.sin_family = AF_INET;
900 if (parse_host_port(&addr, host) == -1) {
901 fprintf(stderr, "parse_host_port() failed for %s\n", host);
902 rc = MIG_STAT_DST_INVALID_ADDR;
903 goto error;
906 fd = socket(PF_INET, SOCK_STREAM, 0);
907 if (fd == -1) {
908 perror("socket failed");
909 rc = MIG_STAT_DST_SOCKET_FAILED;
910 goto error;
913 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1) {
914 perror("setsockopt() failed");
915 rc = MIG_STAT_DST_SOCKOPT_FAILED;
916 goto error_socket;
919 if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
920 perror("bind() failed");
921 rc = MIG_STAT_DST_BIND_FAILED;
922 goto error_socket;
925 if (listen(fd, 1) == -1) {
926 perror("listen() failed");
927 rc = MIG_STAT_DST_LISTEN_FAILED;
928 goto error_socket;
931 again:
932 sfd = accept(fd, (struct sockaddr *)&addr, &addrlen);
933 if (sfd == -1) {
934 if (errno == EINTR)
935 goto again;
936 perror("accept() failed");
937 rc = MIG_STAT_DST_ACCEPT_FAILED;
938 goto error_socket;
941 rc = migrate_incoming_fd(sfd);
942 if (rc != 0) {
943 fprintf(stderr, "migrate_incoming_fd failed (rc=%d)\n", rc);
944 goto error_accept;
947 send_ack:
948 len = write(sfd, &status, 1);
949 if (len == -1 && errno == EAGAIN)
950 goto send_ack;
951 if (len != 1) {
952 fprintf(stderr, "migration: send_ack: write error len=%zu (%s)\n",
953 len, strerror(errno));
954 rc = MIG_STAT_DST_WRITE_FAILED;
955 goto error_accept;
958 rc = wait_for_message("WAIT FOR GO", sfd, wait_for_message_timeout);
959 if (rc) {
960 rc += 200;
961 goto error_accept;
964 wait_for_go:
965 len = read(sfd, &status, 1);
966 if (len == -1 && errno == EAGAIN)
967 goto wait_for_go;
968 if (len != 1) {
969 rc = MIG_STAT_DST_READ_FAILED;
970 fprintf(stderr, "migration: wait_for_go: read error len=%zu (%s)\n",
971 len, strerror(errno));
974 error_accept:
975 close(sfd);
976 error_socket:
977 close(fd);
978 error:
979 return rc;
982 int migrate_incoming(const char *device)
984 const char *ptr;
985 int ret = 0;
987 if (strcmp(device, "stdio") == 0)
988 ret = migrate_incoming_fd(STDIN_FILENO);
989 else if (strstart(device, "file://", &ptr)) {
990 int fd = open(ptr, O_RDONLY);
991 if (fd < 0) {
992 ret = MIG_STAT_DST_INVALID_PARAMS;
993 } else {
994 ret = migrate_incoming_fd(fd);
995 close(fd);
997 } else if (strstart(device, "tcp://", &ptr)) {
998 char *host, *end;
999 host = strdup(ptr);
1000 end = strchr(host, '/');
1001 if (end) *end = 0;
1002 ret = migrate_incoming_tcp(host);
1003 qemu_free(host);
1004 } else {
1005 errno = EINVAL;
1006 ret = MIG_STAT_DST_INVALID_PARAMS;
1009 return ret;
1012 /* Migration monitor command */
1014 /* TODO:
1015 1) audit all error paths
1018 void do_migrate(int detach, const char *uri)
1020 const char *ptr;
1021 MigrationState *s = current_migration;
1023 if (s) {
1024 term_printf("Migration already active\n");
1025 return;
1028 status = MIG_STAT_INVALID_PARAMS;
1029 if (strstart(uri, "exec:", &ptr)) {
1030 char *command = urldecode(ptr);
1031 migration_init_exec(detach, command);
1032 free(command);
1033 } else if (strstart(uri, "ssh://", &ptr)) {
1034 char *host, *end;
1036 host = strdup(ptr);
1037 end = strchr(host, '/');
1038 if (end) *end = 0;
1039 migration_init_ssh(detach, host);
1040 qemu_free(host);
1041 } else if (strstart(uri, "tcp://", &ptr)) {
1042 char *host, *end;
1044 host = strdup(ptr);
1045 end = strchr(host, '/');
1046 if (end) *end = 0;
1048 if (migration_init_tcp(detach, host) == NULL)
1049 term_printf("migration failed (migration_init_tcp for %s failed)\n", host);
1050 free(host);
1051 } else if (strstart(uri, "file://", &ptr)) {
1052 if (migration_init_file(detach, ptr) == NULL)
1053 term_printf("migration failed (migration_init_file for %s failed)\n", ptr);
1054 } else {
1055 term_printf("Unknown migration protocol '%s'\n", uri);
1056 return;
1060 void do_migrate_set_speed(const char *value)
1062 double d;
1063 char *ptr;
1065 d = strtod(value, &ptr);
1066 switch (*ptr) {
1067 case 'G': case 'g':
1068 d *= 1024;
1069 case 'M': case 'm':
1070 d *= 1024;
1071 case 'K': case 'k':
1072 d *= 1024;
1073 default:
1074 break;
1077 max_throttle = (uint32_t)d;
1080 void do_info_migration(void)
1082 MigrationState *s = current_migration;
1084 if (s) {
1085 term_printf("Migration active\n");
1086 if (s->bps < (1 << 20))
1087 term_printf("Transfer rate %3.1f kb/s\n",
1088 (double)s->bps / 1024);
1089 else
1090 term_printf("Transfer rate %3.1f mb/s\n",
1091 (double)s->bps / (1024 * 1024));
1092 term_printf("Iteration %d\n", s->iteration);
1093 term_printf("Transferred %d/%llu pages\n", s->updated_pages,
1094 (unsigned long long)phys_ram_size >> TARGET_PAGE_BITS);
1095 if (s->iteration)
1096 term_printf("Last iteration found %d dirty pages\n", s->last_updated_pages);
1097 } else {
1098 term_printf("Migration inactive\n");
1099 term_printf("last migration status is %d\n", status);
1101 term_printf("Maximum migration speed is ");
1102 if (max_throttle < (1 << 20))
1103 term_printf("%3.1f kb/s\n", (double)max_throttle / 1024);
1104 else
1105 term_printf("%3.1f mb/s\n", (double)max_throttle / (1024 * 1024));
1108 void do_migrate_cancel(void)
1110 MigrationState *s = current_migration;
1112 if (s)
1113 *s->has_error = MIG_STAT_MIGRATION_CANCEL;
1118 #ifdef MIGRATION_VERIFY
1119 unsigned int calc_page_checksum(target_ulong addr)
1121 unsigned int sum=0;
1122 unsigned int *p = (unsigned int *)(phys_ram_base + addr);
1123 unsigned int *q = p + (TARGET_PAGE_SIZE / sizeof(unsigned int));
1125 for ( /*initialized already */ ; p<q ; p++)
1126 sum += *p;
1127 return sum;
1131 static int save_verify_memory(QEMUFile *f, void *opaque)
1133 unsigned int addr;
1134 unsigned int sum;
1136 for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
1137 if (kvm_enabled() && (addr>=0xa0000) && (addr<0xc0000)) /* do not access video-addresses */
1138 continue;
1139 sum = calc_page_checksum(addr);
1140 qemu_put_be32(f, addr);
1141 qemu_put_be32(f, sum);
1143 return 0;
1146 static int load_verify_memory(QEMUFile *f, void *opaque, int version_id)
1148 unsigned int addr, raddr;
1149 unsigned int sum, rsum;
1150 int num_errors = 0;
1152 for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
1153 if (kvm_enabled() && (addr>=0xa0000) && (addr<0xc0000)) /* do not access video-addresses */
1154 continue;
1155 sum = calc_page_checksum(addr);
1156 raddr = qemu_get_be32(f);
1157 rsum = qemu_get_be32(f);
1158 if ((raddr != addr) || (rsum != sum)) {
1159 term_printf("checksum mismatch: src:0x%x 0x%x , dst:0x%x 0x%x\n",
1160 raddr, rsum, addr, sum);
1161 num_errors++;
1164 printf("memory_verify: num_errors=%d\n", num_errors);
1165 term_printf("memory_verify: num_errors=%d\n", num_errors);
1166 return 0/* num_errors */;
1168 #endif /* MIGRATION_VERIFY */