Use rtl8139 as default network adapter
[qemu-kvm/fedora.git] / migration.c
blob17ad77cedb4d11fdce5de5b822b5d03bce63afe4
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 #ifdef USE_KVM
33 #include "qemu-kvm.h"
34 #endif
36 #include <sys/wait.h>
38 #define MIN_FINALIZE_SIZE (200 << 10)
39 #define MAX_ITERATIONS 30
40 #define MAX_RAPID_WRITES 5
42 typedef struct MigrationState
44 int fd;
45 int throttle_count;
46 int bps;
47 int updated_pages;
48 int last_updated_pages;
49 int iteration;
50 int n_buffer; /* number of bytes in @buffer already sent */
51 int l_buffer; /* number of bytes in @buffer to send */
52 int throttled;
53 int *has_error;
54 char buffer[TARGET_PAGE_SIZE + 4 + 4];
55 target_ulong addr;
56 QEMUTimer *timer;
57 void *opaque;
58 int detach;
59 int (*release)(void *opaque);
60 int rapid_writes;
61 } MigrationState;
63 static uint32_t max_throttle = (32 << 20);
64 static MigrationState *current_migration;
65 static int wait_for_message_timeout = 3000; /* 3 seconds */
66 static int status; /* last migration status */
68 enum { /* migration status values */
69 MIG_STAT_SUCCESS = 0,
71 /* src error codes */
72 MIG_STAT_INVALID_PARAMS = 1,
73 MIG_STAT_INVALID_ADDR = 2,
74 MIG_STAT_SOCKET_FAILED = 3,
75 MIG_STAT_SOCKOPT_FAILED = 4,
76 MIG_STAT_BIND_FAILED = 5,
77 MIG_STAT_LISTEN_FAILED = 6,
78 MIG_STAT_ACCEPT_FAILED = 7,
79 MIG_STAT_CONNECT_FAILED = 8,
80 MIG_STAT_WRITE_FAILED = 9,
81 MIG_STAT_READ_FAILED = 10,
82 MIG_STAT_CONNECTION_CLOSED = 11,
83 MIG_STAT_SELECT_FAILED = 12,
84 MIG_STAT_SELECT_TIMEOUT = 13,
85 MIG_STAT_SELECT_FD_NOT_SET = 14,
87 MIG_STAT_SAVEVM_FAILED = 15,
88 MIG_STAT_NO_MEM = 16,
90 MIG_STAT_MIGRATION_CANCEL = 20,
92 /* kvm error codes (on src) */
93 MIG_STAT_KVM_UPDATE_DIRTY_PAGES_LOG_FAILED = 101,
94 MIG_STAT_KVM_SET_DIRTY_TRACKING_FAILED = 102,
95 MIG_STAT_KVM_GET_PAGE_BITMAP = 103,
97 /* dst error codes */
98 MIG_STAT_DST_INVALID_PARAMS = 200 + MIG_STAT_INVALID_PARAMS,
99 MIG_STAT_DST_INVALID_ADDR = 200 + MIG_STAT_INVALID_ADDR,
100 MIG_STAT_DST_SOCKET_FAILED = 200 + MIG_STAT_SOCKET_FAILED,
101 MIG_STAT_DST_SOCKOPT_FAILED = 200 + MIG_STAT_SOCKOPT_FAILED,
102 MIG_STAT_DST_BIND_FAILED = 200 + MIG_STAT_BIND_FAILED,
103 MIG_STAT_DST_LISTEN_FAILED = 200 + MIG_STAT_LISTEN_FAILED,
104 MIG_STAT_DST_ACCEPT_FAILED = 200 + MIG_STAT_ACCEPT_FAILED,
105 MIG_STAT_DST_CONNECT_FAILED = 200 + MIG_STAT_CONNECT_FAILED,
106 MIG_STAT_DST_WRITE_FAILED = 200 + MIG_STAT_WRITE_FAILED,
107 MIG_STAT_DST_READ_FAILED = 200 + MIG_STAT_READ_FAILED,
109 MIG_STAT_DST_CONNECTION_CLOSED = 200 + MIG_STAT_CONNECTION_CLOSED,
110 MIG_STAT_DST_SELECT_FAILED = 200 + MIG_STAT_SELECT_FAILED,
111 MIG_STAT_DST_SELECT_TIMEOUT = 200 + MIG_STAT_SELECT_TIMEOUT,
112 MIG_STAT_DST_SELECT_FD_NOT_SET = 200 + MIG_STAT_SELECT_FD_NOT_SET,
114 MIG_STAT_DST_LOADVM_FAILED = 200 + MIG_STAT_SAVEVM_FAILED,
115 MIG_STAT_DST_NO_MEM = 200 + MIG_STAT_NO_MEM,
117 MIG_STAT_DST_GET_PAGE_FAILED = 230,
118 MIG_STAT_DST_GET_PAGE_UNKNOWN_TYPE = 231,
119 MIG_STAT_DST_MEM_SIZE_MISMATCH = 232,
120 MIG_STAT_DST_MEM_OUT_OF_RANGE = 233,
123 //#define MIGRATION_VERIFY
124 #ifdef MIGRATION_VERIFY
125 static int save_verify_memory(QEMUFile *f, void *opaque);
126 static int load_verify_memory(QEMUFile *f, void *opaque, int version_id);
127 #endif /* MIGRATION_VERIFY */
129 /* QEMUFile migration implementation */
131 static void migrate_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
133 MigrationState *s = opaque;
134 int offset = 0;
136 if (*s->has_error)
137 return;
139 while (offset < size) {
140 ssize_t len;
142 len = write(s->fd, buf + offset, size - offset);
143 if (len == -1) {
144 if (errno == EAGAIN || errno == EINTR)
145 continue;
146 term_printf("migration: write failed (%s)\n", strerror(errno));
147 *s->has_error = MIG_STAT_WRITE_FAILED;
148 break;
149 } else if (len == 0) {
150 term_printf("migration: other side closed connection\n");
151 *s->has_error = MIG_STAT_CONNECTION_CLOSED;
152 break;
155 offset += len;
159 static void migrate_close(void *opaque)
161 MigrationState *s = opaque;
163 if (s->release)
164 s->release(s->opaque);
166 qemu_free(s);
167 current_migration = NULL;
170 /* Outgoing migration routines */
171 static void migrate_finish(MigrationState *s)
173 QEMUFile *f;
174 int ret = 0;
175 int *has_error = s->has_error;
176 int saved_vm_running = vm_running;
177 int detach = s->detach;
179 fcntl(s->fd, F_SETFL, 0);
181 if (! *has_error) {
182 vm_stop(0);
183 f = qemu_fopen(s, migrate_put_buffer, NULL, migrate_close);
184 do {
185 qemu_aio_flush();
186 } while (qemu_bh_poll());
187 bdrv_flush_all();
188 #ifdef USE_KVM
189 if (kvm_allowed && !*s->has_error && kvm_update_dirty_pages_log())
190 *s->has_error = MIG_STAT_KVM_UPDATE_DIRTY_PAGES_LOG_FAILED;
191 #endif
192 qemu_put_be32(f, 1);
193 ret = qemu_live_savevm_state(f);
194 #ifdef MIGRATION_VERIFY
195 save_verify_memory(f, NULL);
196 #endif /* MIGRATION_VERIFY */
197 qemu_fclose(f);
198 } else {
199 migrate_close(s);
201 status = *has_error;
202 if (ret && !status)
203 status = MIG_STAT_SAVEVM_FAILED;
204 if (status) {
205 term_printf("Migration failed! ret=%d error=%d\n", ret, *has_error);
206 if (saved_vm_running)
207 vm_start();
209 if (!detach)
210 monitor_resume();
211 qemu_free(has_error);
212 cpu_physical_memory_set_dirty_tracking(0);
215 static int migrate_write_buffer(MigrationState *s)
217 if (*s->has_error)
218 return 0;
220 if (s->n_buffer < s->l_buffer) {
221 ssize_t len;
222 again:
223 len = write(s->fd, s->buffer + s->n_buffer, s->l_buffer - s->n_buffer);
224 if (len == -1) {
225 if (errno == EINTR)
226 goto again;
227 if (errno == EAGAIN)
228 return 1;
229 *s->has_error = MIG_STAT_WRITE_FAILED;
230 return 0;
232 if (len == 0) {
233 *s->has_error = MIG_STAT_CONNECTION_CLOSED;
234 return 0;
237 s->throttle_count += len;
238 s->n_buffer += len;
239 if (s->n_buffer < s->l_buffer)
240 goto again;
243 if (s->throttle_count > max_throttle) {
244 s->throttled = 1;
245 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
246 return 1;
249 return 0;
252 static int migrate_check_convergence(MigrationState *s)
254 target_ulong addr;
255 int dirty_count = 0;
257 if ((s->iteration >= MAX_ITERATIONS) ||
258 (s->rapid_writes >= MAX_RAPID_WRITES) ) {
259 return 1;
262 for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
263 #ifdef USE_KVM
264 if (kvm_allowed && (addr>=0xa0000) && (addr<0xc0000)) /* do not access video-addresses */
265 continue;
266 #endif
267 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
268 dirty_count++;
271 return ((dirty_count * TARGET_PAGE_SIZE) < MIN_FINALIZE_SIZE);
274 static int ram_page_is_homogeneous(uint32_t addr)
276 int i, n;
277 uint32_t *p, v;
279 n = TARGET_PAGE_SIZE / sizeof(v);
280 p = (uint32 *)(phys_ram_base + addr);
282 v = p[0];
283 for (i=1; i<n; i++)
284 if (p[i] != v)
285 return 0;
286 return 1;
289 static void migrate_prepare_page(MigrationState *s)
291 uint32_t value;
292 char type;
293 const char *buff;
294 int bufflen;
296 value = cpu_to_be32(s->addr);
297 memcpy(s->buffer, &value, 4);
299 if (ram_page_is_homogeneous(s->addr)) {
300 type = 1; /* keeping ram_get_page() values */
301 bufflen = 4;
303 else {
304 type = 0;
305 bufflen = TARGET_PAGE_SIZE;
308 buff = phys_ram_base + s->addr;
309 s->buffer[4] = type;
310 memcpy(s->buffer + 4 + 1, phys_ram_base + s->addr, bufflen);
311 s->n_buffer = 0;
312 s->l_buffer = 4 + 1 + bufflen;
315 static void migrate_write(void *opaque)
317 MigrationState *s = opaque;
319 if (migrate_write_buffer(s))
320 return;
322 #ifdef USE_KVM
323 if (kvm_allowed && !*s->has_error && kvm_update_dirty_pages_log())
324 *s->has_error = MIG_STAT_KVM_UPDATE_DIRTY_PAGES_LOG_FAILED;
325 #endif
327 if (migrate_check_convergence(s) || *s->has_error) {
328 qemu_del_timer(s->timer);
329 qemu_free_timer(s->timer);
330 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
331 migrate_finish(s);
332 return;
335 while (s->addr < phys_ram_size) {
336 #ifdef USE_KVM
337 if (kvm_allowed && (s->addr>=0xa0000) && (s->addr<0xc0000)) /* do not access video-addresses */
338 s->addr = 0xc0000;
339 #endif
341 if (cpu_physical_memory_get_dirty(s->addr, MIGRATION_DIRTY_FLAG)) {
342 migrate_prepare_page(s);
343 cpu_physical_memory_reset_dirty(s->addr, s->addr + TARGET_PAGE_SIZE, MIGRATION_DIRTY_FLAG);
345 s->addr += TARGET_PAGE_SIZE;
347 s->updated_pages++;
349 if (migrate_write_buffer(s))
350 return;
351 } else
352 s->addr += TARGET_PAGE_SIZE;
355 if ((s->iteration) && (s->last_updated_pages <= s->updated_pages)) {
356 s->rapid_writes++; /* "dirt-speed" is faster than transfer speed */
357 if (max_throttle < (1 << 30))
358 max_throttle *= 2; /* try harder */
360 s->last_updated_pages = s->updated_pages;
361 s->updated_pages = 0;
362 s->addr = 0;
363 s->iteration++;
366 static void migrate_reset_throttle(void *opaque)
368 MigrationState *s = opaque;
370 s->bps = s->throttle_count;
372 if (s->throttled) {
373 s->throttled = 0;
374 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_write, s);
376 s->throttle_count = 0;
377 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
380 static int write_whole_buffer(int fd, const void *buff, size_t size)
382 size_t offset = 0, len;
384 while (offset < size) {
385 len = write(fd, buff + offset, size - offset);
386 if (len == -1 && errno == EINTR)
387 continue;
388 if (len < 1)
389 return -EIO;
391 offset += len;
393 return !(offset == size); /* returns 0 on success */
396 static int bit_is_set(int bit, unsigned char *map)
398 return map[bit/8] & (1 << (bit%8));
401 static int start_migration(MigrationState *s)
403 uint32_t value = cpu_to_be32(phys_ram_size);
404 target_phys_addr_t addr;
405 int r;
406 unsigned char running = vm_running?2:1; /* 1 + vm_running */
408 #ifdef USE_KVM
409 int n = 0;
410 unsigned char *phys_ram_page_exist_bitmap = NULL;
411 if (kvm_allowed) {
412 n = BITMAP_SIZE(phys_ram_size);
413 phys_ram_page_exist_bitmap = qemu_malloc(n);
414 if (!phys_ram_page_exist_bitmap) {
415 perror("failed to allocate page bitmap");
416 r = MIG_STAT_NO_MEM;
417 goto out;
419 if (kvm_get_phys_ram_page_bitmap(phys_ram_page_exist_bitmap)) {
420 r = MIG_STAT_KVM_GET_PAGE_BITMAP;
421 perror("kvm_get_mem_map failed");
422 goto out;
425 #endif
427 r = MIG_STAT_WRITE_FAILED;
428 if (write_whole_buffer(s->fd, &running, sizeof(running)))
429 goto out;
430 if (write_whole_buffer(s->fd, &value, sizeof(value)))
431 goto out;
433 #ifdef USE_KVM
434 if (kvm_allowed) {
435 value = cpu_to_be32(n);
436 if (write_whole_buffer(s->fd, &value, sizeof(value)))
437 goto out;
438 if (write_whole_buffer(s->fd, phys_ram_page_exist_bitmap, n))
439 goto out;
441 #endif
442 fcntl(s->fd, F_SETFL, O_NONBLOCK);
444 for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
445 #ifdef USE_KVM
446 if (kvm_allowed && !bit_is_set(addr>>TARGET_PAGE_BITS, phys_ram_page_exist_bitmap)) {
447 cpu_physical_memory_reset_dirty(addr,
448 addr + TARGET_PAGE_SIZE,
449 MIGRATION_DIRTY_FLAG);
450 continue;
452 #endif
453 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
454 cpu_physical_memory_set_dirty(addr);
457 if (cpu_physical_memory_set_dirty_tracking(1)) {
458 *s->has_error = MIG_STAT_KVM_SET_DIRTY_TRACKING_FAILED;
459 return -1;
462 s->addr = 0;
463 s->iteration = 0;
464 s->updated_pages = 0;
465 s->last_updated_pages = 0;
466 s->n_buffer = s->l_buffer = 0;
467 s->rapid_writes = 0;
468 s->timer = qemu_new_timer(rt_clock, migrate_reset_throttle, s);
470 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock));
471 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_write, s);
473 out:
474 #ifdef USE_KVM
475 if (phys_ram_page_exist_bitmap)
476 qemu_free(phys_ram_page_exist_bitmap);
477 #endif
478 return r;
481 static MigrationState *migration_init_fd(int detach, int fd)
483 MigrationState *s;
485 s = qemu_mallocz(sizeof(MigrationState));
486 if (s == NULL) {
487 term_printf("Allocation error\n");
488 return NULL;
491 s->fd = fd;
492 s->has_error = qemu_mallocz(sizeof(int));
493 if (s->has_error == NULL) {
494 term_printf("malloc failed (for has_error)\n");
495 return NULL;
497 s->detach = detach;
499 current_migration = s;
501 if (start_migration(s) == -1) {
502 term_printf("Could not start migration\n");
503 return NULL;
506 if (!detach)
507 monitor_suspend();
509 return s;
512 typedef struct MigrationCmdState
514 int fd;
515 pid_t pid;
516 } MigrationCmdState;
518 static int cmd_release(void *opaque)
520 MigrationCmdState *c = opaque;
521 int status, ret;
523 close(c->fd);
525 again:
526 ret = waitpid(c->pid, &status, 0);
527 if (ret == -1 && errno == EINTR)
528 goto again;
530 if (ret == -1) {
531 term_printf("migration: waitpid failed (%s)\n", strerror(errno));
532 return -1;
534 /* FIXME: check and uncomment
535 * if (WIFEXITED(status))
536 * status = WEXITSTATUS(status);
538 return status;
541 static MigrationState *migration_init_cmd(int detach, const char *command, char **argv)
543 int fds[2];
544 pid_t pid;
545 int i;
546 MigrationState *s;
548 if (pipe(fds) == -1) {
549 term_printf("pipe() (%s)\n", strerror(errno));
550 return NULL;
553 pid = fork();
554 if (pid == -1) {
555 close(fds[0]);
556 close(fds[1]);
557 term_printf("fork error (%s)\n", strerror(errno));
558 return NULL;
560 if (pid == 0) {
561 close(fds[1]);
562 dup2(fds[0], STDIN_FILENO);
563 execvp(command, argv);
564 exit(1);
565 } else
566 close(fds[0]);
568 for (i = 0; argv[i]; i++)
569 qemu_free(argv[i]);
570 qemu_free(argv);
572 s = migration_init_fd(detach, fds[1]);
573 if (s) {
574 MigrationCmdState *c = qemu_mallocz(sizeof(*c));
575 c->pid = pid;
576 c->fd = fds[1];
577 s->release = cmd_release;
578 s->opaque = c;
581 return s;
584 static MigrationState *migration_init_exec(int detach, const char *command)
586 char **argv = NULL;
588 argv = qemu_mallocz(sizeof(char *) * 4);
589 argv[0] = strdup("sh");
590 argv[1] = strdup("-c");
591 argv[2] = strdup(command);
592 argv[3] = NULL;
594 return migration_init_cmd(detach, "/bin/sh", argv);
597 static MigrationState *migration_init_ssh(int detach, const char *host)
599 int qemu_argc, daemonize = 0, argc, i;
600 char **qemu_argv, **argv;
601 const char *incoming = NULL;
603 qemu_get_launch_info(&qemu_argc, &qemu_argv, &daemonize, &incoming);
605 argc = 3 + qemu_argc;
606 if (!daemonize)
607 argc++;
608 if (!incoming)
609 argc+=2;
611 argv = qemu_mallocz(sizeof(char *) * (argc + 1));
612 argv[0] = strdup("ssh");
613 argv[1] = strdup("-XC");
614 argv[2] = strdup(host);
616 for (i = 0; i < qemu_argc; i++)
617 argv[3 + i] = strdup(qemu_argv[i]);
619 if (!daemonize)
620 argv[3 + i++] = strdup("-daemonize");
621 if (!incoming) {
622 argv[3 + i++] = strdup("-incoming");
623 argv[3 + i++] = strdup("stdio");
626 argv[3 + i] = NULL;
628 return migration_init_cmd(detach, "ssh", argv);
631 /* (busy) wait timeout (miliseconds) for a message to arrive on fd. */
632 /* returns 0 on success error_code otherwise (18 for timeout) */
633 static int wait_for_message(const char *msg, int fd, int timeout)
635 fd_set rfds;
636 struct timeval tv;
637 int64_t now, expiration, delta; /* milliseconds */
638 int n = 0;
640 now = qemu_get_clock(rt_clock);
641 expiration = now + timeout;
642 do {
643 FD_ZERO(&rfds);
644 FD_SET(fd, &rfds);
645 tv.tv_sec = tv.tv_usec = 0;
646 now = qemu_get_clock(rt_clock);
647 delta = expiration - now;
648 if (delta > 0)
649 tv.tv_usec = delta * 1000;
650 n = select(fd + 1, &rfds, NULL, NULL, &tv);
651 } while ( (n == -1) && (errno == EINTR) );
653 switch (n) {
654 case -1: /* error */
655 fprintf(stderr, "%s FAILED: ", msg);
656 perror("");
657 return MIG_STAT_SELECT_FAILED;
658 case 0: /* timeout */
659 fprintf(stderr, "%s: timeout reached\n", msg);
660 return MIG_STAT_SELECT_TIMEOUT;
661 case 1:
662 break;
663 default:
664 fprintf(stderr, "wait_for_message: %s: select returned %d\n", msg, n);
666 if (!FD_ISSET(fd, &rfds)) {
667 fprintf(stderr, "wait_for_message: %s: s->fd not set\n", msg);
668 return MIG_STAT_SELECT_FD_NOT_SET;
671 return 0;
674 static int tcp_release(void *opaque)
676 MigrationState *s = opaque;
677 uint8_t status = 0;
678 ssize_t len = 0;
679 int n;
681 if (*s->has_error)
682 goto out;
684 n = wait_for_message("WAIT FOR ACK", s->fd, wait_for_message_timeout);
685 if (n) {
686 *s->has_error = n;
687 goto out;
690 wait_for_ack:
691 len = read(s->fd, &status, 1);
692 if (len == -1 && errno == EINTR)
693 goto wait_for_ack;
694 if (len != 1 || status != 0) {
695 *s->has_error = MIG_STAT_READ_FAILED;
696 fprintf(stderr, "migration: wait_for_ack: read error l=%zu s=%d(%s)\n",
697 len, status, strerror(errno));
698 goto out;
701 send_go:
702 len = write(s->fd, &status, 1);
703 if (len == -1 && errno == EINTR)
704 goto send_go;
705 if (len != 1) {
706 fprintf(stderr, "migration: send_go: write error l=%zu (%s)\n",
707 len, strerror(errno));
708 *s->has_error = MIG_STAT_WRITE_FAILED;
711 out:
712 close(s->fd);
714 return (len != 1 || status != 0);
717 static MigrationState *migration_init_tcp(int detach, const char *host)
719 int fd;
720 struct sockaddr_in addr;
721 MigrationState *s;
723 fd = socket(PF_INET, SOCK_STREAM, 0);
724 if (fd == -1) {
725 term_printf("socket() failed %s\n", strerror(errno));
726 status = MIG_STAT_SOCKET_FAILED;
727 return NULL;
730 addr.sin_family = AF_INET;
731 if (parse_host_port(&addr, host) == -1) {
732 term_printf("parse_host_port() FAILED for %s\n", host);
733 close(fd);
734 status = MIG_STAT_INVALID_ADDR;
735 return NULL;
738 again:
739 if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
740 if (errno == EINTR)
741 goto again;
742 term_printf("connect() failed %s\n", strerror(errno));
743 close(fd);
744 status = MIG_STAT_CONNECT_FAILED;
745 return NULL;
748 s = migration_init_fd(detach, fd);
749 if (s) {
750 s->opaque = s;
751 s->release = tcp_release;
753 return s;
756 /* Incoming migration */
758 static void migrate_incoming_homogeneous_page(uint32_t addr, uint32_t v)
760 int i, n;
761 uint32_t *p;
763 n = TARGET_PAGE_SIZE / sizeof(v);
764 p = (uint32 *)(phys_ram_base + addr);
766 for (i=0; i<n; i++)
767 p[i] = v;
770 static int migrate_incoming_page(QEMUFile *f, uint32_t addr)
772 int l, v, ret = 0;
774 switch (qemu_get_byte(f)) {
775 case 0: /* the whole page */
776 l = qemu_get_buffer(f, phys_ram_base + addr, TARGET_PAGE_SIZE);
777 if (l != TARGET_PAGE_SIZE)
778 ret = MIG_STAT_DST_GET_PAGE_FAILED;
779 break;
780 case 1: /* homogeneous page -- a single byte */
781 l = qemu_get_buffer(f, (void*)&v, 4);
782 if (l != 4)
783 ret = MIG_STAT_DST_GET_PAGE_FAILED;
784 migrate_incoming_homogeneous_page(addr, v);
785 break;
786 default:
787 ret = MIG_STAT_DST_GET_PAGE_UNKNOWN_TYPE;
790 return ret;
793 static int migrate_incoming_fd(int fd)
795 int ret = 0;
796 QEMUFile *f = qemu_fopen_fd(fd);
797 uint32_t addr, size;
798 extern void qemu_announce_self(void);
799 unsigned char running;
801 running = qemu_get_byte(f);
802 if ((running != 1) && (running != 2)) {
803 fprintf(stderr, "migration: illegal running value %u, not (1 or 2)\n",
804 running);
805 return MIG_STAT_DST_MEM_SIZE_MISMATCH;
807 autostart = running - 1;
809 size = qemu_get_be32(f);
810 if (size != phys_ram_size) {
811 fprintf(stderr, "migration: memory size mismatch: recv %u mine %u\n",
812 size, phys_ram_size);
813 return MIG_STAT_DST_MEM_SIZE_MISMATCH;
816 #ifdef USE_KVM
817 if (kvm_allowed) {
818 int n, m;
819 unsigned char *phys_ram_page_exist_bitmap = NULL;
821 /* allocate memory bitmap */
822 n = BITMAP_SIZE(phys_ram_size);
823 m = qemu_get_be32(f);
824 if (m > n) // allocate enough space
825 n = m;
826 phys_ram_page_exist_bitmap = qemu_malloc(n);
827 if (!phys_ram_page_exist_bitmap) {
828 perror("failed to allocate page bitmap");
829 return MIG_STAT_NO_MEM;
832 /* receive memory bitmap */
833 qemu_get_buffer(f, phys_ram_page_exist_bitmap, m);
835 /* FIXME: free dellocated-at-src guest memory pages */
837 qemu_free(phys_ram_page_exist_bitmap);
839 #endif
841 do {
842 addr = qemu_get_be32(f);
843 if (addr == 1)
844 break;
845 if (addr >= phys_ram_size)
846 return MIG_STAT_DST_MEM_OUT_OF_RANGE;
847 ret = migrate_incoming_page(f, addr);
848 if (ret)
849 return ret;
850 } while (1);
853 qemu_aio_flush();
854 vm_stop(0);
855 if (qemu_live_loadvm_state(f))
856 ret = MIG_STAT_DST_LOADVM_FAILED;
857 #ifdef MIGRATION_VERIFY
858 if (ret==0) ret=load_verify_memory(f, NULL, 1);
859 #endif /* MIGRATION_VERIFY */
860 qemu_fclose(f);
862 return ret;
865 static int migrate_incoming_tcp(const char *host)
867 struct sockaddr_in addr;
868 socklen_t addrlen = sizeof(addr);
869 int fd, sfd;
870 ssize_t len;
871 uint8_t status = 0;
872 int reuse = 1;
873 int rc;
875 addr.sin_family = AF_INET;
876 if (parse_host_port(&addr, host) == -1) {
877 fprintf(stderr, "parse_host_port() failed for %s\n", host);
878 rc = MIG_STAT_DST_INVALID_ADDR;
879 goto error;
882 fd = socket(PF_INET, SOCK_STREAM, 0);
883 if (fd == -1) {
884 perror("socket failed");
885 rc = MIG_STAT_DST_SOCKET_FAILED;
886 goto error;
889 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1) {
890 perror("setsockopt() failed");
891 rc = MIG_STAT_DST_SOCKOPT_FAILED;
892 goto error_socket;
895 if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
896 perror("bind() failed");
897 rc = MIG_STAT_DST_BIND_FAILED;
898 goto error_socket;
901 if (listen(fd, 1) == -1) {
902 perror("listen() failed");
903 rc = MIG_STAT_DST_LISTEN_FAILED;
904 goto error_socket;
907 again:
908 sfd = accept(fd, (struct sockaddr *)&addr, &addrlen);
909 if (sfd == -1) {
910 if (errno == EINTR)
911 goto again;
912 perror("accept() failed");
913 rc = MIG_STAT_DST_ACCEPT_FAILED;
914 goto error_socket;
917 rc = migrate_incoming_fd(sfd);
918 if (rc != 0) {
919 fprintf(stderr, "migrate_incoming_fd failed (rc=%d)\n", rc);
920 goto error_accept;
923 send_ack:
924 len = write(sfd, &status, 1);
925 if (len == -1 && errno == EAGAIN)
926 goto send_ack;
927 if (len != 1) {
928 fprintf(stderr, "migration: send_ack: write error len=%zu (%s)\n",
929 len, strerror(errno));
930 rc = MIG_STAT_DST_WRITE_FAILED;
931 goto error_accept;
934 rc = wait_for_message("WAIT FOR GO", sfd, wait_for_message_timeout);
935 if (rc) {
936 rc += 200;
937 goto error_accept;
940 wait_for_go:
941 len = read(sfd, &status, 1);
942 if (len == -1 && errno == EAGAIN)
943 goto wait_for_go;
944 if (len != 1) {
945 rc = MIG_STAT_DST_READ_FAILED;
946 fprintf(stderr, "migration: wait_for_go: read error len=%zu (%s)\n",
947 len, strerror(errno));
950 error_accept:
951 close(sfd);
952 error_socket:
953 close(fd);
954 error:
955 return rc;
958 int migrate_incoming(const char *device)
960 const char *ptr;
961 int ret = 0;
963 if (strcmp(device, "stdio") == 0)
964 ret = migrate_incoming_fd(STDIN_FILENO);
965 else if (strstart(device, "file://", &ptr)) {
966 int fd = open(ptr, O_RDONLY);
967 if (fd < 0) {
968 ret = MIG_STAT_DST_INVALID_PARAMS;
969 } else {
970 ret = migrate_incoming_fd(fd);
971 close(fd);
973 } else if (strstart(device, "tcp://", &ptr)) {
974 char *host, *end;
975 host = strdup(ptr);
976 end = strchr(host, '/');
977 if (end) *end = 0;
978 ret = migrate_incoming_tcp(host);
979 qemu_free(host);
980 } else {
981 errno = EINVAL;
982 ret = MIG_STAT_DST_INVALID_PARAMS;
985 return ret;
988 /* Migration monitor command */
990 /* TODO:
991 1) audit all error paths
994 void do_migrate(int detach, const char *uri)
996 const char *ptr;
997 MigrationState *s = current_migration;
999 if (s) {
1000 term_printf("Migration already active\n");
1001 return;
1004 status = MIG_STAT_INVALID_PARAMS;
1005 if (strstart(uri, "exec:", &ptr)) {
1006 char *command = urldecode(ptr);
1007 migration_init_exec(detach, command);
1008 free(command);
1009 } else if (strstart(uri, "ssh://", &ptr)) {
1010 char *host, *end;
1012 host = strdup(ptr);
1013 end = strchr(host, '/');
1014 if (end) *end = 0;
1015 migration_init_ssh(detach, host);
1016 qemu_free(host);
1017 } else if (strstart(uri, "tcp://", &ptr)) {
1018 char *host, *end;
1020 host = strdup(ptr);
1021 end = strchr(host, '/');
1022 if (end) *end = 0;
1024 if (migration_init_tcp(detach, host) == NULL)
1025 term_printf("migration failed (migration_init_tcp for %s failed)\n", host);
1026 free(host);
1027 } else {
1028 term_printf("Unknown migration protocol '%s'\n", uri);
1029 return;
1033 void do_migrate_set_speed(const char *value)
1035 double d;
1036 char *ptr;
1038 d = strtod(value, &ptr);
1039 switch (*ptr) {
1040 case 'G': case 'g':
1041 d *= 1024;
1042 case 'M': case 'm':
1043 d *= 1024;
1044 case 'K': case 'k':
1045 d *= 1024;
1046 default:
1047 break;
1050 max_throttle = (uint32_t)d;
1053 void do_info_migration(void)
1055 MigrationState *s = current_migration;
1057 if (s) {
1058 term_printf("Migration active\n");
1059 if (s->bps < (1 << 20))
1060 term_printf("Transfer rate %3.1f kb/s\n",
1061 (double)s->bps / 1024);
1062 else
1063 term_printf("Transfer rate %3.1f mb/s\n",
1064 (double)s->bps / (1024 * 1024));
1065 term_printf("Iteration %d\n", s->iteration);
1066 term_printf("Transferred %d/%d pages\n", s->updated_pages, phys_ram_size >> TARGET_PAGE_BITS);
1067 if (s->iteration)
1068 term_printf("Last iteration found %d dirty pages\n", s->last_updated_pages);
1069 } else {
1070 term_printf("Migration inactive\n");
1071 term_printf("last migration status is %d\n", status);
1073 term_printf("Maximum migration speed is ");
1074 if (max_throttle < (1 << 20))
1075 term_printf("%3.1f kb/s\n", (double)max_throttle / 1024);
1076 else
1077 term_printf("%3.1f mb/s\n", (double)max_throttle / (1024 * 1024));
1080 void do_migrate_cancel(void)
1082 MigrationState *s = current_migration;
1084 if (s)
1085 *s->has_error = MIG_STAT_MIGRATION_CANCEL;
1090 #ifdef MIGRATION_VERIFY
1091 unsigned int calc_page_checksum(target_ulong addr)
1093 unsigned int sum=0;
1094 unsigned int *p = (unsigned int *)(phys_ram_base + addr);
1095 unsigned int *q = p + (TARGET_PAGE_SIZE / sizeof(unsigned int));
1097 for ( /*initialized already */ ; p<q ; p++)
1098 sum += *p;
1099 return sum;
1103 static int save_verify_memory(QEMUFile *f, void *opaque)
1105 unsigned int addr;
1106 unsigned int sum;
1108 for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
1109 #ifdef USE_KVM
1110 if (kvm_allowed && (addr>=0xa0000) && (addr<0xc0000)) /* do not access video-addresses */
1111 continue;
1112 #endif
1113 sum = calc_page_checksum(addr);
1114 qemu_put_be32(f, addr);
1115 qemu_put_be32(f, sum);
1117 return 0;
1120 static int load_verify_memory(QEMUFile *f, void *opaque, int version_id)
1122 unsigned int addr, raddr;
1123 unsigned int sum, rsum;
1124 int num_errors = 0;
1126 for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
1127 #ifdef USE_KVM
1128 if (kvm_allowed && (addr>=0xa0000) && (addr<0xc0000)) /* do not access video-addresses */
1129 continue;
1130 #endif
1131 sum = calc_page_checksum(addr);
1132 raddr = qemu_get_be32(f);
1133 rsum = qemu_get_be32(f);
1134 if ((raddr != addr) || (rsum != sum)) {
1135 term_printf("checksum mismatch: src:0x%x 0x%x , dst:0x%x 0x%x\n",
1136 raddr, rsum, addr, sum);
1137 num_errors++;
1140 printf("memory_verify: num_errors=%d\n", num_errors);
1141 term_printf("memory_verify: num_errors=%d\n", num_errors);
1142 return 0/* num_errors */;
1144 #endif /* MIGRATION_VERIFY */