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
))) {
429 perror("vm_running write failed");
432 if (write_whole_buffer(s
->fd
, &value
, sizeof(value
))) {
433 perror("phys_ram_size write failed");
439 value
= cpu_to_be32(n
);
440 if (write_whole_buffer(s
->fd
, &value
, sizeof(value
))) {
441 perror("phys_ram_size_bitmap size write failed");
444 if (write_whole_buffer(s
->fd
, phys_ram_page_exist_bitmap
, n
)) {
445 perror("phys_ram_page_exist_bitmap write failed");
450 fcntl(s
->fd
, F_SETFL
, O_NONBLOCK
);
452 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
454 if (kvm_allowed
&& !bit_is_set(addr
>>TARGET_PAGE_BITS
, phys_ram_page_exist_bitmap
)) {
455 cpu_physical_memory_reset_dirty(addr
,
456 addr
+ TARGET_PAGE_SIZE
,
457 MIGRATION_DIRTY_FLAG
);
461 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
462 cpu_physical_memory_set_dirty(addr
);
465 if (cpu_physical_memory_set_dirty_tracking(1)) {
466 *s
->has_error
= MIG_STAT_KVM_SET_DIRTY_TRACKING_FAILED
;
467 r
= MIG_STAT_KVM_SET_DIRTY_TRACKING_FAILED
;
473 s
->updated_pages
= 0;
474 s
->last_updated_pages
= 0;
475 s
->n_buffer
= s
->l_buffer
= 0;
477 s
->timer
= qemu_new_timer(rt_clock
, migrate_reset_throttle
, s
);
479 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
));
480 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, migrate_write
, s
);
486 if (phys_ram_page_exist_bitmap
)
487 qemu_free(phys_ram_page_exist_bitmap
);
492 static MigrationState
*migration_init_fd(int detach
, int fd
)
496 s
= qemu_mallocz(sizeof(MigrationState
));
498 term_printf("Allocation error\n");
503 s
->has_error
= qemu_mallocz(sizeof(int));
504 if (s
->has_error
== NULL
) {
505 term_printf("malloc failed (for has_error)\n");
510 current_migration
= s
;
512 if (start_migration(s
)) {
513 term_printf("Could not start migration\n");
524 typedef struct MigrationCmdState
530 static int cmd_release(void *opaque
)
532 MigrationCmdState
*c
= opaque
;
538 ret
= waitpid(c
->pid
, &status
, 0);
539 if (ret
== -1 && errno
== EINTR
)
543 term_printf("migration: waitpid failed (%s)\n", strerror(errno
));
546 /* FIXME: check and uncomment
547 * if (WIFEXITED(status))
548 * status = WEXITSTATUS(status);
553 static MigrationState
*migration_init_cmd(int detach
, const char *command
, char **argv
)
560 if (pipe(fds
) == -1) {
561 term_printf("pipe() (%s)\n", strerror(errno
));
569 term_printf("fork error (%s)\n", strerror(errno
));
574 dup2(fds
[0], STDIN_FILENO
);
575 execvp(command
, argv
);
580 for (i
= 0; argv
[i
]; i
++)
584 s
= migration_init_fd(detach
, fds
[1]);
586 MigrationCmdState
*c
= qemu_mallocz(sizeof(*c
));
589 s
->release
= cmd_release
;
596 static MigrationState
*migration_init_exec(int detach
, const char *command
)
600 argv
= qemu_mallocz(sizeof(char *) * 4);
601 argv
[0] = strdup("sh");
602 argv
[1] = strdup("-c");
603 argv
[2] = strdup(command
);
606 return migration_init_cmd(detach
, "/bin/sh", argv
);
609 static MigrationState
*migration_init_ssh(int detach
, const char *host
)
611 int qemu_argc
, daemonize
= 0, argc
, i
;
612 char **qemu_argv
, **argv
;
613 const char *incoming
= NULL
;
615 qemu_get_launch_info(&qemu_argc
, &qemu_argv
, &daemonize
, &incoming
);
617 argc
= 3 + qemu_argc
;
623 argv
= qemu_mallocz(sizeof(char *) * (argc
+ 1));
624 argv
[0] = strdup("ssh");
625 argv
[1] = strdup("-XC");
626 argv
[2] = strdup(host
);
628 for (i
= 0; i
< qemu_argc
; i
++)
629 argv
[3 + i
] = strdup(qemu_argv
[i
]);
632 argv
[3 + i
++] = strdup("-daemonize");
634 argv
[3 + i
++] = strdup("-incoming");
635 argv
[3 + i
++] = strdup("stdio");
640 return migration_init_cmd(detach
, "ssh", argv
);
643 /* (busy) wait timeout (miliseconds) for a message to arrive on fd. */
644 /* returns 0 on success error_code otherwise (18 for timeout) */
645 static int wait_for_message(const char *msg
, int fd
, int timeout
)
649 int64_t now
, expiration
, delta
; /* milliseconds */
652 now
= qemu_get_clock(rt_clock
);
653 expiration
= now
+ timeout
;
657 tv
.tv_sec
= tv
.tv_usec
= 0;
658 now
= qemu_get_clock(rt_clock
);
659 delta
= expiration
- now
;
661 tv
.tv_usec
= delta
* 1000;
662 n
= select(fd
+ 1, &rfds
, NULL
, NULL
, &tv
);
663 } while ( (n
== -1) && (errno
== EINTR
) );
667 fprintf(stderr
, "%s FAILED: ", msg
);
669 return MIG_STAT_SELECT_FAILED
;
670 case 0: /* timeout */
671 fprintf(stderr
, "%s: timeout reached\n", msg
);
672 return MIG_STAT_SELECT_TIMEOUT
;
676 fprintf(stderr
, "wait_for_message: %s: select returned %d\n", msg
, n
);
678 if (!FD_ISSET(fd
, &rfds
)) {
679 fprintf(stderr
, "wait_for_message: %s: s->fd not set\n", msg
);
680 return MIG_STAT_SELECT_FD_NOT_SET
;
686 static int tcp_release(void *opaque
)
688 MigrationState
*s
= opaque
;
696 n
= wait_for_message("WAIT FOR ACK", s
->fd
, wait_for_message_timeout
);
703 len
= read(s
->fd
, &status
, 1);
704 if (len
== -1 && errno
== EINTR
)
706 if (len
!= 1 || status
!= 0) {
707 *s
->has_error
= MIG_STAT_READ_FAILED
;
708 fprintf(stderr
, "migration: wait_for_ack: read error l=%zu s=%d(%s)\n",
709 len
, status
, strerror(errno
));
714 len
= write(s
->fd
, &status
, 1);
715 if (len
== -1 && errno
== EINTR
)
718 fprintf(stderr
, "migration: send_go: write error l=%zu (%s)\n",
719 len
, strerror(errno
));
720 *s
->has_error
= MIG_STAT_WRITE_FAILED
;
726 return (len
!= 1 || status
!= 0);
729 static MigrationState
*migration_init_tcp(int detach
, const char *host
)
732 struct sockaddr_in addr
;
735 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
737 term_printf("socket() failed %s\n", strerror(errno
));
738 status
= MIG_STAT_SOCKET_FAILED
;
742 addr
.sin_family
= AF_INET
;
743 if (parse_host_port(&addr
, host
) == -1) {
744 term_printf("parse_host_port() FAILED for %s\n", host
);
746 status
= MIG_STAT_INVALID_ADDR
;
751 if (connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)) == -1) {
754 term_printf("connect() failed %s\n", strerror(errno
));
756 status
= MIG_STAT_CONNECT_FAILED
;
760 s
= migration_init_fd(detach
, fd
);
763 s
->release
= tcp_release
;
768 /* Incoming migration */
770 static void migrate_incoming_homogeneous_page(uint32_t addr
, uint32_t v
)
775 n
= TARGET_PAGE_SIZE
/ sizeof(v
);
776 p
= (uint32
*)(phys_ram_base
+ addr
);
782 static int migrate_incoming_page(QEMUFile
*f
, uint32_t addr
)
786 switch (qemu_get_byte(f
)) {
787 case 0: /* the whole page */
788 l
= qemu_get_buffer(f
, phys_ram_base
+ addr
, TARGET_PAGE_SIZE
);
789 if (l
!= TARGET_PAGE_SIZE
)
790 ret
= MIG_STAT_DST_GET_PAGE_FAILED
;
792 case 1: /* homogeneous page -- a single byte */
793 l
= qemu_get_buffer(f
, (void*)&v
, 4);
795 ret
= MIG_STAT_DST_GET_PAGE_FAILED
;
796 migrate_incoming_homogeneous_page(addr
, v
);
799 ret
= MIG_STAT_DST_GET_PAGE_UNKNOWN_TYPE
;
805 static int migrate_incoming_fd(int fd
)
808 QEMUFile
*f
= qemu_fopen_fd(fd
);
810 extern void qemu_announce_self(void);
811 unsigned char running
;
813 running
= qemu_get_byte(f
);
814 if ((running
!= 1) && (running
!= 2)) {
815 fprintf(stderr
, "migration: illegal running value %u, not (1 or 2)\n",
817 return MIG_STAT_DST_MEM_SIZE_MISMATCH
;
819 autostart
= running
- 1;
821 size
= qemu_get_be32(f
);
822 if (size
!= phys_ram_size
) {
823 fprintf(stderr
, "migration: memory size mismatch: recv %u mine %u\n",
824 size
, phys_ram_size
);
825 return MIG_STAT_DST_MEM_SIZE_MISMATCH
;
831 unsigned char *phys_ram_page_exist_bitmap
= NULL
;
833 /* allocate memory bitmap */
834 n
= BITMAP_SIZE(phys_ram_size
);
835 m
= qemu_get_be32(f
);
836 if (m
> n
) // allocate enough space
838 phys_ram_page_exist_bitmap
= qemu_malloc(n
);
839 if (!phys_ram_page_exist_bitmap
) {
840 perror("failed to allocate page bitmap");
841 return MIG_STAT_NO_MEM
;
844 /* receive memory bitmap */
845 qemu_get_buffer(f
, phys_ram_page_exist_bitmap
, m
);
847 /* FIXME: free dellocated-at-src guest memory pages */
849 qemu_free(phys_ram_page_exist_bitmap
);
854 addr
= qemu_get_be32(f
);
857 if (addr
>= phys_ram_size
)
858 return MIG_STAT_DST_MEM_OUT_OF_RANGE
;
859 ret
= migrate_incoming_page(f
, addr
);
867 if (qemu_live_loadvm_state(f
))
868 ret
= MIG_STAT_DST_LOADVM_FAILED
;
869 #ifdef MIGRATION_VERIFY
870 if (ret
==0) ret
=load_verify_memory(f
, NULL
, 1);
871 #endif /* MIGRATION_VERIFY */
877 static int migrate_incoming_tcp(const char *host
)
879 struct sockaddr_in addr
;
880 socklen_t addrlen
= sizeof(addr
);
887 addr
.sin_family
= AF_INET
;
888 if (parse_host_port(&addr
, host
) == -1) {
889 fprintf(stderr
, "parse_host_port() failed for %s\n", host
);
890 rc
= MIG_STAT_DST_INVALID_ADDR
;
894 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
896 perror("socket failed");
897 rc
= MIG_STAT_DST_SOCKET_FAILED
;
901 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &reuse
, sizeof(reuse
)) == -1) {
902 perror("setsockopt() failed");
903 rc
= MIG_STAT_DST_SOCKOPT_FAILED
;
907 if (bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)) == -1) {
908 perror("bind() failed");
909 rc
= MIG_STAT_DST_BIND_FAILED
;
913 if (listen(fd
, 1) == -1) {
914 perror("listen() failed");
915 rc
= MIG_STAT_DST_LISTEN_FAILED
;
920 sfd
= accept(fd
, (struct sockaddr
*)&addr
, &addrlen
);
924 perror("accept() failed");
925 rc
= MIG_STAT_DST_ACCEPT_FAILED
;
929 rc
= migrate_incoming_fd(sfd
);
931 fprintf(stderr
, "migrate_incoming_fd failed (rc=%d)\n", rc
);
936 len
= write(sfd
, &status
, 1);
937 if (len
== -1 && errno
== EAGAIN
)
940 fprintf(stderr
, "migration: send_ack: write error len=%zu (%s)\n",
941 len
, strerror(errno
));
942 rc
= MIG_STAT_DST_WRITE_FAILED
;
946 rc
= wait_for_message("WAIT FOR GO", sfd
, wait_for_message_timeout
);
953 len
= read(sfd
, &status
, 1);
954 if (len
== -1 && errno
== EAGAIN
)
957 rc
= MIG_STAT_DST_READ_FAILED
;
958 fprintf(stderr
, "migration: wait_for_go: read error len=%zu (%s)\n",
959 len
, strerror(errno
));
970 int migrate_incoming(const char *device
)
975 if (strcmp(device
, "stdio") == 0)
976 ret
= migrate_incoming_fd(STDIN_FILENO
);
977 else if (strstart(device
, "file://", &ptr
)) {
978 int fd
= open(ptr
, O_RDONLY
);
980 ret
= MIG_STAT_DST_INVALID_PARAMS
;
982 ret
= migrate_incoming_fd(fd
);
985 } else if (strstart(device
, "tcp://", &ptr
)) {
988 end
= strchr(host
, '/');
990 ret
= migrate_incoming_tcp(host
);
994 ret
= MIG_STAT_DST_INVALID_PARAMS
;
1000 /* Migration monitor command */
1003 1) audit all error paths
1006 void do_migrate(int detach
, const char *uri
)
1009 MigrationState
*s
= current_migration
;
1012 term_printf("Migration already active\n");
1016 status
= MIG_STAT_INVALID_PARAMS
;
1017 if (strstart(uri
, "exec:", &ptr
)) {
1018 char *command
= urldecode(ptr
);
1019 migration_init_exec(detach
, command
);
1021 } else if (strstart(uri
, "ssh://", &ptr
)) {
1025 end
= strchr(host
, '/');
1027 migration_init_ssh(detach
, host
);
1029 } else if (strstart(uri
, "tcp://", &ptr
)) {
1033 end
= strchr(host
, '/');
1036 if (migration_init_tcp(detach
, host
) == NULL
)
1037 term_printf("migration failed (migration_init_tcp for %s failed)\n", host
);
1040 term_printf("Unknown migration protocol '%s'\n", uri
);
1045 void do_migrate_set_speed(const char *value
)
1050 d
= strtod(value
, &ptr
);
1062 max_throttle
= (uint32_t)d
;
1065 void do_info_migration(void)
1067 MigrationState
*s
= current_migration
;
1070 term_printf("Migration active\n");
1071 if (s
->bps
< (1 << 20))
1072 term_printf("Transfer rate %3.1f kb/s\n",
1073 (double)s
->bps
/ 1024);
1075 term_printf("Transfer rate %3.1f mb/s\n",
1076 (double)s
->bps
/ (1024 * 1024));
1077 term_printf("Iteration %d\n", s
->iteration
);
1078 term_printf("Transferred %d/%d pages\n", s
->updated_pages
, phys_ram_size
>> TARGET_PAGE_BITS
);
1080 term_printf("Last iteration found %d dirty pages\n", s
->last_updated_pages
);
1082 term_printf("Migration inactive\n");
1083 term_printf("last migration status is %d\n", status
);
1085 term_printf("Maximum migration speed is ");
1086 if (max_throttle
< (1 << 20))
1087 term_printf("%3.1f kb/s\n", (double)max_throttle
/ 1024);
1089 term_printf("%3.1f mb/s\n", (double)max_throttle
/ (1024 * 1024));
1092 void do_migrate_cancel(void)
1094 MigrationState
*s
= current_migration
;
1097 *s
->has_error
= MIG_STAT_MIGRATION_CANCEL
;
1102 #ifdef MIGRATION_VERIFY
1103 unsigned int calc_page_checksum(target_ulong addr
)
1106 unsigned int *p
= (unsigned int *)(phys_ram_base
+ addr
);
1107 unsigned int *q
= p
+ (TARGET_PAGE_SIZE
/ sizeof(unsigned int));
1109 for ( /*initialized already */ ; p
<q
; p
++)
1115 static int save_verify_memory(QEMUFile
*f
, void *opaque
)
1120 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
1122 if (kvm_allowed
&& (addr
>=0xa0000) && (addr
<0xc0000)) /* do not access video-addresses */
1125 sum
= calc_page_checksum(addr
);
1126 qemu_put_be32(f
, addr
);
1127 qemu_put_be32(f
, sum
);
1132 static int load_verify_memory(QEMUFile
*f
, void *opaque
, int version_id
)
1134 unsigned int addr
, raddr
;
1135 unsigned int sum
, rsum
;
1138 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
1140 if (kvm_allowed
&& (addr
>=0xa0000) && (addr
<0xc0000)) /* do not access video-addresses */
1143 sum
= calc_page_checksum(addr
);
1144 raddr
= qemu_get_be32(f
);
1145 rsum
= qemu_get_be32(f
);
1146 if ((raddr
!= addr
) || (rsum
!= sum
)) {
1147 term_printf("checksum mismatch: src:0x%x 0x%x , dst:0x%x 0x%x\n",
1148 raddr
, rsum
, addr
, sum
);
1152 printf("memory_verify: num_errors=%d\n", num_errors
);
1153 term_printf("memory_verify: num_errors=%d\n", num_errors
);
1154 return 0/* num_errors */;
1156 #endif /* MIGRATION_VERIFY */