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"
36 #define MIN_FINALIZE_SIZE (200 << 10)
37 #define MAX_ITERATIONS 30
38 #define MAX_RAPID_WRITES 5
40 typedef struct MigrationState
46 int last_updated_pages
;
48 int n_buffer
; /* number of bytes in @buffer already sent */
49 int l_buffer
; /* number of bytes in @buffer to send */
52 char buffer
[TARGET_PAGE_SIZE
+ 4 + 4];
57 int (*release
)(void *opaque
);
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 */
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,
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,
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
;
137 while (offset
< size
) {
140 len
= write(s
->fd
, buf
+ offset
, size
- offset
);
142 if (errno
== EAGAIN
|| errno
== EINTR
)
144 term_printf("migration: write failed (%s)\n", strerror(errno
));
145 *s
->has_error
= MIG_STAT_WRITE_FAILED
;
147 } else if (len
== 0) {
148 term_printf("migration: other side closed connection\n");
149 *s
->has_error
= MIG_STAT_CONNECTION_CLOSED
;
157 static void migrate_close(void *opaque
)
159 MigrationState
*s
= opaque
;
162 s
->release(s
->opaque
);
165 current_migration
= NULL
;
168 /* Outgoing migration routines */
169 static void migrate_finish(MigrationState
*s
)
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);
181 f
= qemu_fopen(s
, migrate_put_buffer
, NULL
, migrate_close
);
184 } while (qemu_bh_poll());
187 if (kvm_enabled() && !*s
->has_error
&& kvm_update_dirty_pages_log())
188 *s
->has_error
= MIG_STAT_KVM_UPDATE_DIRTY_PAGES_LOG_FAILED
;
191 ret
= qemu_live_savevm_state(f
);
192 #ifdef MIGRATION_VERIFY
193 save_verify_memory(f
, NULL
);
194 #endif /* MIGRATION_VERIFY */
201 status
= MIG_STAT_SAVEVM_FAILED
;
203 term_printf("Migration failed! ret=%d error=%d\n", ret
, *has_error
);
204 if (saved_vm_running
)
209 qemu_free(has_error
);
210 cpu_physical_memory_set_dirty_tracking(0);
213 static int migrate_write_buffer(MigrationState
*s
)
218 if (s
->n_buffer
< s
->l_buffer
) {
221 len
= write(s
->fd
, s
->buffer
+ s
->n_buffer
, s
->l_buffer
- s
->n_buffer
);
227 *s
->has_error
= MIG_STAT_WRITE_FAILED
;
231 *s
->has_error
= MIG_STAT_CONNECTION_CLOSED
;
235 s
->throttle_count
+= len
;
237 if (s
->n_buffer
< s
->l_buffer
)
241 if (s
->throttle_count
> max_throttle
) {
243 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
250 static int migrate_check_convergence(MigrationState
*s
)
255 if ((s
->iteration
>= MAX_ITERATIONS
) ||
256 (s
->rapid_writes
>= MAX_RAPID_WRITES
) ) {
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 */
263 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
267 return ((dirty_count
* TARGET_PAGE_SIZE
) < MIN_FINALIZE_SIZE
);
270 static int ram_page_is_homogeneous(uint32_t addr
)
275 n
= TARGET_PAGE_SIZE
/ sizeof(v
);
276 p
= (uint32
*)(phys_ram_base
+ addr
);
285 static void migrate_prepare_page(MigrationState
*s
)
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 */
301 bufflen
= TARGET_PAGE_SIZE
;
304 buff
= phys_ram_base
+ s
->addr
;
306 memcpy(s
->buffer
+ 4 + 1, phys_ram_base
+ s
->addr
, bufflen
);
308 s
->l_buffer
= 4 + 1 + bufflen
;
311 static void migrate_write(void *opaque
)
313 MigrationState
*s
= opaque
;
315 if (migrate_write_buffer(s
))
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
);
329 while (s
->addr
< phys_ram_size
) {
330 if (kvm_enabled() && (s
->addr
>=0xa0000) && (s
->addr
<0xc0000)) /* do not access video-addresses */
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
;
341 if (migrate_write_buffer(s
))
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;
358 static void migrate_reset_throttle(void *opaque
)
360 MigrationState
*s
= opaque
;
362 s
->bps
= s
->throttle_count
;
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
)
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
;
398 unsigned char running
= vm_running
?2:1; /* 1 + vm_running */
400 unsigned char *phys_ram_page_exist_bitmap
= NULL
;
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");
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");
417 r
= MIG_STAT_WRITE_FAILED
;
418 if (write_whole_buffer(s
->fd
, &running
, sizeof(running
))) {
419 perror("vm_running write failed");
422 if (write_whole_buffer(s
->fd
, &value
, sizeof(value
))) {
423 perror("phys_ram_size write failed");
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");
433 if (write_whole_buffer(s
->fd
, phys_ram_page_exist_bitmap
, n
)) {
434 perror("phys_ram_page_exist_bitmap write failed");
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
);
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
;
460 s
->updated_pages
= 0;
461 s
->last_updated_pages
= 0;
462 s
->n_buffer
= s
->l_buffer
= 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
);
472 if (kvm_enabled() && phys_ram_page_exist_bitmap
)
473 qemu_free(phys_ram_page_exist_bitmap
);
477 static MigrationState
*migration_init_fd(int detach
, int fd
)
481 s
= qemu_mallocz(sizeof(MigrationState
));
483 term_printf("Allocation error\n");
488 s
->has_error
= qemu_mallocz(sizeof(int));
489 if (s
->has_error
== NULL
) {
490 term_printf("malloc failed (for has_error)\n");
495 current_migration
= s
;
497 if (start_migration(s
)) {
498 term_printf("Could not start migration\n");
509 typedef struct MigrationCmdState
515 static int cmd_release(void *opaque
)
517 MigrationCmdState
*c
= opaque
;
523 ret
= waitpid(c
->pid
, &status
, 0);
524 if (ret
== -1 && errno
== EINTR
)
528 term_printf("migration: waitpid failed (%s)\n", strerror(errno
));
531 /* FIXME: check and uncomment
532 * if (WIFEXITED(status))
533 * status = WEXITSTATUS(status);
538 static MigrationState
*migration_init_cmd(int detach
, const char *command
, char **argv
)
545 if (pipe(fds
) == -1) {
546 term_printf("pipe() (%s)\n", strerror(errno
));
554 term_printf("fork error (%s)\n", strerror(errno
));
559 dup2(fds
[0], STDIN_FILENO
);
560 execvp(command
, argv
);
565 for (i
= 0; argv
[i
]; i
++)
569 s
= migration_init_fd(detach
, fds
[1]);
571 MigrationCmdState
*c
= qemu_mallocz(sizeof(*c
));
574 s
->release
= cmd_release
;
581 static MigrationState
*migration_init_exec(int detach
, const char *command
)
585 argv
= qemu_mallocz(sizeof(char *) * 4);
586 argv
[0] = strdup("sh");
587 argv
[1] = strdup("-c");
588 argv
[2] = strdup(command
);
591 return migration_init_cmd(detach
, "/bin/sh", argv
);
595 static int file_release(void *opaque
)
597 int fd
= (int)(long)opaque
;
602 static MigrationState
*migration_init_file(int detach
, const char *filename
)
607 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
609 term_printf("open file %s failed (%s)\n", filename
, strerror(errno
));
613 s
= migration_init_fd(detach
, fd
);
615 s
->release
= file_release
;
616 s
->opaque
= (void*)(long)fd
;
617 max_throttle
= 1U << 31;
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
;
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
]);
646 argv
[3 + i
++] = strdup("-daemonize");
648 argv
[3 + i
++] = strdup("-incoming");
649 argv
[3 + i
++] = strdup("stdio");
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
)
663 int64_t now
, expiration
, delta
; /* milliseconds */
666 now
= qemu_get_clock(rt_clock
);
667 expiration
= now
+ timeout
;
671 tv
.tv_sec
= tv
.tv_usec
= 0;
672 now
= qemu_get_clock(rt_clock
);
673 delta
= expiration
- now
;
675 tv
.tv_usec
= delta
* 1000;
676 n
= select(fd
+ 1, &rfds
, NULL
, NULL
, &tv
);
677 } while ( (n
== -1) && (errno
== EINTR
) );
681 fprintf(stderr
, "%s FAILED: ", msg
);
683 return MIG_STAT_SELECT_FAILED
;
684 case 0: /* timeout */
685 fprintf(stderr
, "%s: timeout reached\n", msg
);
686 return MIG_STAT_SELECT_TIMEOUT
;
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
;
700 static int tcp_release(void *opaque
)
702 MigrationState
*s
= opaque
;
710 n
= wait_for_message("WAIT FOR ACK", s
->fd
, wait_for_message_timeout
);
717 len
= read(s
->fd
, &status
, 1);
718 if (len
== -1 && errno
== EINTR
)
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
));
728 len
= write(s
->fd
, &status
, 1);
729 if (len
== -1 && errno
== EINTR
)
732 fprintf(stderr
, "migration: send_go: write error l=%zu (%s)\n",
733 len
, strerror(errno
));
734 *s
->has_error
= MIG_STAT_WRITE_FAILED
;
740 return (len
!= 1 || status
!= 0);
743 static MigrationState
*migration_init_tcp(int detach
, const char *host
)
746 struct sockaddr_in addr
;
749 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
751 term_printf("socket() failed %s\n", strerror(errno
));
752 status
= MIG_STAT_SOCKET_FAILED
;
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
);
760 status
= MIG_STAT_INVALID_ADDR
;
765 if (connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)) == -1) {
768 term_printf("connect() failed %s\n", strerror(errno
));
770 status
= MIG_STAT_CONNECT_FAILED
;
774 s
= migration_init_fd(detach
, fd
);
777 s
->release
= tcp_release
;
782 /* Incoming migration */
784 static void migrate_incoming_homogeneous_page(uint32_t addr
, uint32_t v
)
789 n
= TARGET_PAGE_SIZE
/ sizeof(v
);
790 p
= (uint32
*)(phys_ram_base
+ addr
);
796 static int migrate_incoming_page(QEMUFile
*f
, uint32_t addr
)
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
;
806 case 1: /* homogeneous page -- a single byte */
807 l
= qemu_get_buffer(f
, (void*)&v
, 4);
809 ret
= MIG_STAT_DST_GET_PAGE_FAILED
;
810 migrate_incoming_homogeneous_page(addr
, v
);
813 ret
= MIG_STAT_DST_GET_PAGE_UNKNOWN_TYPE
;
819 static int migrate_incoming_fd(int fd
)
822 QEMUFile
*f
= qemu_fopen_fd(fd
);
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",
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 %u\n",
838 size
, phys_ram_size
);
839 return MIG_STAT_DST_MEM_SIZE_MISMATCH
;
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
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
);
866 addr
= qemu_get_be32(f
);
869 if (addr
>= phys_ram_size
)
870 return MIG_STAT_DST_MEM_OUT_OF_RANGE
;
871 ret
= migrate_incoming_page(f
, addr
);
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 */
889 static int migrate_incoming_tcp(const char *host
)
891 struct sockaddr_in addr
;
892 socklen_t addrlen
= sizeof(addr
);
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
;
906 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
908 perror("socket failed");
909 rc
= MIG_STAT_DST_SOCKET_FAILED
;
913 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &reuse
, sizeof(reuse
)) == -1) {
914 perror("setsockopt() failed");
915 rc
= MIG_STAT_DST_SOCKOPT_FAILED
;
919 if (bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)) == -1) {
920 perror("bind() failed");
921 rc
= MIG_STAT_DST_BIND_FAILED
;
925 if (listen(fd
, 1) == -1) {
926 perror("listen() failed");
927 rc
= MIG_STAT_DST_LISTEN_FAILED
;
932 sfd
= accept(fd
, (struct sockaddr
*)&addr
, &addrlen
);
936 perror("accept() failed");
937 rc
= MIG_STAT_DST_ACCEPT_FAILED
;
941 rc
= migrate_incoming_fd(sfd
);
943 fprintf(stderr
, "migrate_incoming_fd failed (rc=%d)\n", rc
);
948 len
= write(sfd
, &status
, 1);
949 if (len
== -1 && errno
== EAGAIN
)
952 fprintf(stderr
, "migration: send_ack: write error len=%zu (%s)\n",
953 len
, strerror(errno
));
954 rc
= MIG_STAT_DST_WRITE_FAILED
;
958 rc
= wait_for_message("WAIT FOR GO", sfd
, wait_for_message_timeout
);
965 len
= read(sfd
, &status
, 1);
966 if (len
== -1 && errno
== EAGAIN
)
969 rc
= MIG_STAT_DST_READ_FAILED
;
970 fprintf(stderr
, "migration: wait_for_go: read error len=%zu (%s)\n",
971 len
, strerror(errno
));
982 int migrate_incoming(const char *device
)
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
);
992 ret
= MIG_STAT_DST_INVALID_PARAMS
;
994 ret
= migrate_incoming_fd(fd
);
997 } else if (strstart(device
, "tcp://", &ptr
)) {
1000 end
= strchr(host
, '/');
1002 ret
= migrate_incoming_tcp(host
);
1006 ret
= MIG_STAT_DST_INVALID_PARAMS
;
1012 /* Migration monitor command */
1015 1) audit all error paths
1018 void do_migrate(int detach
, const char *uri
)
1021 MigrationState
*s
= current_migration
;
1024 term_printf("Migration already active\n");
1028 status
= MIG_STAT_INVALID_PARAMS
;
1029 if (strstart(uri
, "exec:", &ptr
)) {
1030 char *command
= urldecode(ptr
);
1031 migration_init_exec(detach
, command
);
1033 } else if (strstart(uri
, "ssh://", &ptr
)) {
1037 end
= strchr(host
, '/');
1039 migration_init_ssh(detach
, host
);
1041 } else if (strstart(uri
, "tcp://", &ptr
)) {
1045 end
= strchr(host
, '/');
1048 if (migration_init_tcp(detach
, host
) == NULL
)
1049 term_printf("migration failed (migration_init_tcp for %s failed)\n", 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
);
1055 term_printf("Unknown migration protocol '%s'\n", uri
);
1060 void do_migrate_set_speed(const char *value
)
1065 d
= strtod(value
, &ptr
);
1077 max_throttle
= (uint32_t)d
;
1080 void do_info_migration(void)
1082 MigrationState
*s
= current_migration
;
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);
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/%d pages\n", s
->updated_pages
, phys_ram_size
>> TARGET_PAGE_BITS
);
1095 term_printf("Last iteration found %d dirty pages\n", s
->last_updated_pages
);
1097 term_printf("Migration inactive\n");
1098 term_printf("last migration status is %d\n", status
);
1100 term_printf("Maximum migration speed is ");
1101 if (max_throttle
< (1 << 20))
1102 term_printf("%3.1f kb/s\n", (double)max_throttle
/ 1024);
1104 term_printf("%3.1f mb/s\n", (double)max_throttle
/ (1024 * 1024));
1107 void do_migrate_cancel(void)
1109 MigrationState
*s
= current_migration
;
1112 *s
->has_error
= MIG_STAT_MIGRATION_CANCEL
;
1117 #ifdef MIGRATION_VERIFY
1118 unsigned int calc_page_checksum(target_ulong addr
)
1121 unsigned int *p
= (unsigned int *)(phys_ram_base
+ addr
);
1122 unsigned int *q
= p
+ (TARGET_PAGE_SIZE
/ sizeof(unsigned int));
1124 for ( /*initialized already */ ; p
<q
; p
++)
1130 static int save_verify_memory(QEMUFile
*f
, void *opaque
)
1135 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
1136 if (kvm_enabled() && (addr
>=0xa0000) && (addr
<0xc0000)) /* do not access video-addresses */
1138 sum
= calc_page_checksum(addr
);
1139 qemu_put_be32(f
, addr
);
1140 qemu_put_be32(f
, sum
);
1145 static int load_verify_memory(QEMUFile
*f
, void *opaque
, int version_id
)
1147 unsigned int addr
, raddr
;
1148 unsigned int sum
, rsum
;
1151 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
1152 if (kvm_enabled() && (addr
>=0xa0000) && (addr
<0xc0000)) /* do not access video-addresses */
1154 sum
= calc_page_checksum(addr
);
1155 raddr
= qemu_get_be32(f
);
1156 rsum
= qemu_get_be32(f
);
1157 if ((raddr
!= addr
) || (rsum
!= sum
)) {
1158 term_printf("checksum mismatch: src:0x%x 0x%x , dst:0x%x 0x%x\n",
1159 raddr
, rsum
, addr
, sum
);
1163 printf("memory_verify: num_errors=%d\n", num_errors
);
1164 term_printf("memory_verify: num_errors=%d\n", num_errors
);
1165 return 0/* num_errors */;
1167 #endif /* MIGRATION_VERIFY */