2 * QEMU migration support
4 * Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
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
29 #include "qemu-timer.h"
30 #include "migration.h"
31 #include "qemu_socket.h"
38 #define MIN_FINALIZE_SIZE (200 << 10)
39 #define MAX_ITERATIONS 30
40 #define MAX_RAPID_WRITES 5
42 typedef struct MigrationState
48 int last_updated_pages
;
50 int n_buffer
; /* number of bytes in @buffer already sent */
51 int l_buffer
; /* number of bytes in @buffer to send */
54 char buffer
[TARGET_PAGE_SIZE
+ 4 + 4];
59 int (*release
)(void *opaque
);
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 */
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,
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,
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
;
139 while (offset
< size
) {
142 len
= write(s
->fd
, buf
+ offset
, size
- offset
);
144 if (errno
== EAGAIN
|| errno
== EINTR
)
146 term_printf("migration: write failed (%s)\n", strerror(errno
));
147 *s
->has_error
= MIG_STAT_WRITE_FAILED
;
149 } else if (len
== 0) {
150 term_printf("migration: other side closed connection\n");
151 *s
->has_error
= MIG_STAT_CONNECTION_CLOSED
;
159 static void migrate_close(void *opaque
)
161 MigrationState
*s
= opaque
;
164 s
->release(s
->opaque
);
167 current_migration
= NULL
;
170 /* Outgoing migration routines */
171 static void migrate_finish(MigrationState
*s
)
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);
183 f
= qemu_fopen(s
, migrate_put_buffer
, NULL
, migrate_close
);
186 } while (qemu_bh_poll());
189 if (kvm_allowed
&& !*s
->has_error
&& kvm_update_dirty_pages_log())
190 *s
->has_error
= MIG_STAT_KVM_UPDATE_DIRTY_PAGES_LOG_FAILED
;
193 ret
= qemu_live_savevm_state(f
);
194 #ifdef MIGRATION_VERIFY
195 save_verify_memory(f
, NULL
);
196 #endif /* MIGRATION_VERIFY */
203 status
= MIG_STAT_SAVEVM_FAILED
;
205 term_printf("Migration failed! ret=%d error=%d\n", ret
, *has_error
);
206 if (saved_vm_running
)
211 qemu_free(has_error
);
212 cpu_physical_memory_set_dirty_tracking(0);
215 static int migrate_write_buffer(MigrationState
*s
)
220 if (s
->n_buffer
< s
->l_buffer
) {
223 len
= write(s
->fd
, s
->buffer
+ s
->n_buffer
, s
->l_buffer
- s
->n_buffer
);
229 *s
->has_error
= MIG_STAT_WRITE_FAILED
;
233 *s
->has_error
= MIG_STAT_CONNECTION_CLOSED
;
237 s
->throttle_count
+= len
;
239 if (s
->n_buffer
< s
->l_buffer
)
243 if (s
->throttle_count
> max_throttle
) {
245 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
252 static int migrate_check_convergence(MigrationState
*s
)
257 if ((s
->iteration
>= MAX_ITERATIONS
) ||
258 (s
->rapid_writes
>= MAX_RAPID_WRITES
) ) {
262 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
264 if (kvm_allowed
&& (addr
>=0xa0000) && (addr
<0xc0000)) /* do not access video-addresses */
267 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
271 return ((dirty_count
* TARGET_PAGE_SIZE
) < MIN_FINALIZE_SIZE
);
274 static int ram_page_is_homogeneous(uint32_t addr
)
279 n
= TARGET_PAGE_SIZE
/ sizeof(v
);
280 p
= (uint32
*)(phys_ram_base
+ addr
);
289 static void migrate_prepare_page(MigrationState
*s
)
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 */
305 bufflen
= TARGET_PAGE_SIZE
;
308 buff
= phys_ram_base
+ s
->addr
;
310 memcpy(s
->buffer
+ 4 + 1, phys_ram_base
+ s
->addr
, bufflen
);
312 s
->l_buffer
= 4 + 1 + bufflen
;
315 static void migrate_write(void *opaque
)
317 MigrationState
*s
= opaque
;
319 if (migrate_write_buffer(s
))
323 if (kvm_allowed
&& !*s
->has_error
&& kvm_update_dirty_pages_log())
324 *s
->has_error
= MIG_STAT_KVM_UPDATE_DIRTY_PAGES_LOG_FAILED
;
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
);
335 while (s
->addr
< phys_ram_size
) {
337 if (kvm_allowed
&& (s
->addr
>=0xa0000) && (s
->addr
<0xc0000)) /* do not access video-addresses */
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
;
349 if (migrate_write_buffer(s
))
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;
366 static void migrate_reset_throttle(void *opaque
)
368 MigrationState
*s
= opaque
;
370 s
->bps
= s
->throttle_count
;
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
)
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
;
406 unsigned char running
= vm_running
?2:1; /* 1 + vm_running */
410 unsigned char *phys_ram_page_exist_bitmap
= NULL
;
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");
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");
427 r
= MIG_STAT_WRITE_FAILED
;
428 if (write_whole_buffer(s
->fd
, &running
, sizeof(running
)))
430 if (write_whole_buffer(s
->fd
, &value
, sizeof(value
)))
435 value
= cpu_to_be32(n
);
436 if (write_whole_buffer(s
->fd
, &value
, sizeof(value
)))
438 if (write_whole_buffer(s
->fd
, phys_ram_page_exist_bitmap
, n
))
442 fcntl(s
->fd
, F_SETFL
, O_NONBLOCK
);
444 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
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
);
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
;
464 s
->updated_pages
= 0;
465 s
->last_updated_pages
= 0;
466 s
->n_buffer
= s
->l_buffer
= 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
);
475 if (phys_ram_page_exist_bitmap
)
476 qemu_free(phys_ram_page_exist_bitmap
);
481 static MigrationState
*migration_init_fd(int detach
, int fd
)
485 s
= qemu_mallocz(sizeof(MigrationState
));
487 term_printf("Allocation error\n");
492 s
->has_error
= qemu_mallocz(sizeof(int));
493 if (s
->has_error
== NULL
) {
494 term_printf("malloc failed (for has_error)\n");
499 current_migration
= s
;
501 if (start_migration(s
) == -1) {
502 term_printf("Could not start migration\n");
512 typedef struct MigrationCmdState
518 static int cmd_release(void *opaque
)
520 MigrationCmdState
*c
= opaque
;
526 ret
= waitpid(c
->pid
, &status
, 0);
527 if (ret
== -1 && errno
== EINTR
)
531 term_printf("migration: waitpid failed (%s)\n", strerror(errno
));
534 /* FIXME: check and uncomment
535 * if (WIFEXITED(status))
536 * status = WEXITSTATUS(status);
541 static MigrationState
*migration_init_cmd(int detach
, const char *command
, char **argv
)
548 if (pipe(fds
) == -1) {
549 term_printf("pipe() (%s)\n", strerror(errno
));
557 term_printf("fork error (%s)\n", strerror(errno
));
562 dup2(fds
[0], STDIN_FILENO
);
563 execvp(command
, argv
);
568 for (i
= 0; argv
[i
]; i
++)
572 s
= migration_init_fd(detach
, fds
[1]);
574 MigrationCmdState
*c
= qemu_mallocz(sizeof(*c
));
577 s
->release
= cmd_release
;
584 static MigrationState
*migration_init_exec(int detach
, const char *command
)
588 argv
= qemu_mallocz(sizeof(char *) * 4);
589 argv
[0] = strdup("sh");
590 argv
[1] = strdup("-c");
591 argv
[2] = strdup(command
);
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
;
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
]);
620 argv
[3 + i
++] = strdup("-daemonize");
622 argv
[3 + i
++] = strdup("-incoming");
623 argv
[3 + i
++] = strdup("stdio");
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
)
637 int64_t now
, expiration
, delta
; /* milliseconds */
640 now
= qemu_get_clock(rt_clock
);
641 expiration
= now
+ timeout
;
645 tv
.tv_sec
= tv
.tv_usec
= 0;
646 now
= qemu_get_clock(rt_clock
);
647 delta
= expiration
- now
;
649 tv
.tv_usec
= delta
* 1000;
650 n
= select(fd
+ 1, &rfds
, NULL
, NULL
, &tv
);
651 } while ( (n
== -1) && (errno
== EINTR
) );
655 fprintf(stderr
, "%s FAILED: ", msg
);
657 return MIG_STAT_SELECT_FAILED
;
658 case 0: /* timeout */
659 fprintf(stderr
, "%s: timeout reached\n", msg
);
660 return MIG_STAT_SELECT_TIMEOUT
;
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
;
674 static int tcp_release(void *opaque
)
676 MigrationState
*s
= opaque
;
684 n
= wait_for_message("WAIT FOR ACK", s
->fd
, wait_for_message_timeout
);
691 len
= read(s
->fd
, &status
, 1);
692 if (len
== -1 && errno
== EINTR
)
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
));
702 len
= write(s
->fd
, &status
, 1);
703 if (len
== -1 && errno
== EINTR
)
706 fprintf(stderr
, "migration: send_go: write error l=%zu (%s)\n",
707 len
, strerror(errno
));
708 *s
->has_error
= MIG_STAT_WRITE_FAILED
;
714 return (len
!= 1 || status
!= 0);
717 static MigrationState
*migration_init_tcp(int detach
, const char *host
)
720 struct sockaddr_in addr
;
723 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
725 term_printf("socket() failed %s\n", strerror(errno
));
726 status
= MIG_STAT_SOCKET_FAILED
;
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
);
734 status
= MIG_STAT_INVALID_ADDR
;
739 if (connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)) == -1) {
742 term_printf("connect() failed %s\n", strerror(errno
));
744 status
= MIG_STAT_CONNECT_FAILED
;
748 s
= migration_init_fd(detach
, fd
);
751 s
->release
= tcp_release
;
756 /* Incoming migration */
758 static void migrate_incoming_homogeneous_page(uint32_t addr
, uint32_t v
)
763 n
= TARGET_PAGE_SIZE
/ sizeof(v
);
764 p
= (uint32
*)(phys_ram_base
+ addr
);
770 static int migrate_incoming_page(QEMUFile
*f
, uint32_t addr
)
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
;
780 case 1: /* homogeneous page -- a single byte */
781 l
= qemu_get_buffer(f
, (void*)&v
, 4);
783 ret
= MIG_STAT_DST_GET_PAGE_FAILED
;
784 migrate_incoming_homogeneous_page(addr
, v
);
787 ret
= MIG_STAT_DST_GET_PAGE_UNKNOWN_TYPE
;
793 static int migrate_incoming_fd(int fd
)
796 QEMUFile
*f
= qemu_fopen_fd(fd
);
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",
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
;
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
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
);
842 addr
= qemu_get_be32(f
);
845 if (addr
>= phys_ram_size
)
846 return MIG_STAT_DST_MEM_OUT_OF_RANGE
;
847 ret
= migrate_incoming_page(f
, addr
);
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 */
865 static int migrate_incoming_tcp(const char *host
)
867 struct sockaddr_in addr
;
868 socklen_t addrlen
= sizeof(addr
);
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
;
882 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
884 perror("socket failed");
885 rc
= MIG_STAT_DST_SOCKET_FAILED
;
889 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &reuse
, sizeof(reuse
)) == -1) {
890 perror("setsockopt() failed");
891 rc
= MIG_STAT_DST_SOCKOPT_FAILED
;
895 if (bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)) == -1) {
896 perror("bind() failed");
897 rc
= MIG_STAT_DST_BIND_FAILED
;
901 if (listen(fd
, 1) == -1) {
902 perror("listen() failed");
903 rc
= MIG_STAT_DST_LISTEN_FAILED
;
908 sfd
= accept(fd
, (struct sockaddr
*)&addr
, &addrlen
);
912 perror("accept() failed");
913 rc
= MIG_STAT_DST_ACCEPT_FAILED
;
917 rc
= migrate_incoming_fd(sfd
);
919 fprintf(stderr
, "migrate_incoming_fd failed (rc=%d)\n", rc
);
924 len
= write(sfd
, &status
, 1);
925 if (len
== -1 && errno
== EAGAIN
)
928 fprintf(stderr
, "migration: send_ack: write error len=%zu (%s)\n",
929 len
, strerror(errno
));
930 rc
= MIG_STAT_DST_WRITE_FAILED
;
934 rc
= wait_for_message("WAIT FOR GO", sfd
, wait_for_message_timeout
);
941 len
= read(sfd
, &status
, 1);
942 if (len
== -1 && errno
== EAGAIN
)
945 rc
= MIG_STAT_DST_READ_FAILED
;
946 fprintf(stderr
, "migration: wait_for_go: read error len=%zu (%s)\n",
947 len
, strerror(errno
));
958 int migrate_incoming(const char *device
)
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
);
968 ret
= MIG_STAT_DST_INVALID_PARAMS
;
970 ret
= migrate_incoming_fd(fd
);
973 } else if (strstart(device
, "tcp://", &ptr
)) {
976 end
= strchr(host
, '/');
978 ret
= migrate_incoming_tcp(host
);
982 ret
= MIG_STAT_DST_INVALID_PARAMS
;
988 /* Migration monitor command */
991 1) audit all error paths
994 void do_migrate(int detach
, const char *uri
)
997 MigrationState
*s
= current_migration
;
1000 term_printf("Migration already active\n");
1004 status
= MIG_STAT_INVALID_PARAMS
;
1005 if (strstart(uri
, "exec:", &ptr
)) {
1006 char *command
= urldecode(ptr
);
1007 migration_init_exec(detach
, command
);
1009 } else if (strstart(uri
, "ssh://", &ptr
)) {
1013 end
= strchr(host
, '/');
1015 migration_init_ssh(detach
, host
);
1017 } else if (strstart(uri
, "tcp://", &ptr
)) {
1021 end
= strchr(host
, '/');
1024 if (migration_init_tcp(detach
, host
) == NULL
)
1025 term_printf("migration failed (migration_init_tcp for %s failed)\n", host
);
1028 term_printf("Unknown migration protocol '%s'\n", uri
);
1033 void do_migrate_set_speed(const char *value
)
1038 d
= strtod(value
, &ptr
);
1050 max_throttle
= (uint32_t)d
;
1053 void do_info_migration(void)
1055 MigrationState
*s
= current_migration
;
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);
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
);
1068 term_printf("Last iteration found %d dirty pages\n", s
->last_updated_pages
);
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);
1077 term_printf("%3.1f mb/s\n", (double)max_throttle
/ (1024 * 1024));
1080 void do_migrate_cancel(void)
1082 MigrationState
*s
= current_migration
;
1085 *s
->has_error
= MIG_STAT_MIGRATION_CANCEL
;
1090 #ifdef MIGRATION_VERIFY
1091 unsigned int calc_page_checksum(target_ulong addr
)
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
++)
1103 static int save_verify_memory(QEMUFile
*f
, void *opaque
)
1108 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
1110 if (kvm_allowed
&& (addr
>=0xa0000) && (addr
<0xc0000)) /* do not access video-addresses */
1113 sum
= calc_page_checksum(addr
);
1114 qemu_put_be32(f
, addr
);
1115 qemu_put_be32(f
, sum
);
1120 static int load_verify_memory(QEMUFile
*f
, void *opaque
, int version_id
)
1122 unsigned int addr
, raddr
;
1123 unsigned int sum
, rsum
;
1126 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
1128 if (kvm_allowed
&& (addr
>=0xa0000) && (addr
<0xc0000)) /* do not access video-addresses */
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
);
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 */