2 #include "qemu_socket.h"
5 #define TO_BE_IMPLEMENTED term_printf("%s: TO_BE_IMPLEMENTED\n", __FUNCTION__)
6 #define USE_NONBLOCKING_SOCKETS
8 #ifndef CONFIG_USER_ONLY
11 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
);
12 #ifdef USE_NONBLOCKING_SOCKETS
13 void socket_set_block(int fd
) /* should be in vl.c ? */
16 val
= fcntl(fd
, F_GETFL
);
17 fcntl(fd
, F_SETFL
, val
& ~O_NONBLOCK
);
22 #define QEMU_MIGRATION_MAGIC 0x5145564d /* FIXME: our own magic ??? */
23 #define QEMU_MIGRATION_VERSION 0x00000001
32 MIG_STAT_NONE
= 0, /* disconnected */
33 MIG_STAT_LISTEN
= 1, /* listening, waiting for the other to connect */
34 MIG_STAT_CONN
= 2, /* connection established */
35 MIG_STAT_START
= 3, /* migration started */
36 MIG_STAT_SUCC
= 4, /* migration completed successfully */
37 MIG_STAT_FAIL
= 5, /* migration failed */
38 MIG_STAT_CANCEL
= 6 /* migration canceled */
42 /* page types to be used when migrating ram pages */
44 MIG_XFER_PAGE_TYPE_REGULAR
= 0, /* regular page */
45 MIG_XFER_PAGE_TYPE_HOMOGENEOUS
= 1, /* all bytes are the same */
46 MIG_XFER_PAGE_TYPE_END
= 15, /* go to the next phase */
49 typedef struct migration_bandwith_params
{
50 int min
, max
, offline
, seconds
;
51 } migration_bandwith_params_t
;
53 typedef struct migration_state
{
55 migration_status_t status
;
56 #define BUFFSIZE ( 256 * 1024)
57 unsigned char buff
[BUFFSIZE
]; /* FIXME: allocate dynamically; use mutli/double buffer */
60 migration_role_t role
;
61 int64_t head_counter
, tail_counter
;
62 migration_bandwith_params_t bw
;
70 static migration_state_t ms
= {
72 .status
= MIG_STAT_NONE
,
86 static const char *reader_default_addr
="localhost:4455";
87 static const char *writer_default_addr
="localhost:4456";
89 /* forward declarations */
90 static void migration_cleanup(migration_state_t
*pms
, migration_status_t stat
);
91 static void migration_start_src(migration_state_t
*pms
);
92 static void migration_phase_1_src(migration_state_t
*pms
);
93 static void migration_phase_2_src(migration_state_t
*pms
);
94 static void migration_phase_3_src(migration_state_t
*pms
);
95 static void migration_phase_4_src(migration_state_t
*pms
);
96 static void migration_start_dst(migration_state_t
*pms
);
97 static void migration_phase_1_dst(migration_state_t
*pms
);
98 static void migration_phase_2_dst(migration_state_t
*pms
);
99 static void migration_phase_3_dst(migration_state_t
*pms
);
100 static void migration_phase_4_dst(migration_state_t
*pms
);
102 static void migration_ram_send(migration_state_t
*pms
, int whole_ram
);
103 static void migration_ram_recv(migration_state_t
*pms
);
105 typedef void (*QemuMigrationPhaseCB
)(migration_state_t
*pms
);
106 #define MIGRATION_NUM_PHASES 5
107 QemuMigrationPhaseCB migration_phase_funcs
[2][MIGRATION_NUM_PHASES
] = {
110 migration_phase_1_src
,
111 migration_phase_2_src
,
112 migration_phase_3_src
,
113 migration_phase_4_src
},
116 migration_phase_1_dst
,
117 migration_phase_2_dst
,
118 migration_phase_3_dst
,
119 migration_phase_4_dst
}
123 * assuming pms is defined in the function calling MIG_ASSERT
124 * retuns non-0 if the condition is false, 0 if all is OK
126 #define MIG_ASSERT(p) mig_assert(pms, !!(p), __FUNCTION__, __LINE__)
127 int mig_assert(migration_state_t
*pms
, int cond
, const char *fname
, int line
)
130 term_printf("assertion failed at %s():%d\n", fname
, line
);
131 migration_cleanup(&ms
, MIG_STAT_FAIL
);
136 static const char *mig_stat_str(migration_status_t mig_stat
)
139 migration_status_t stat
;
142 {MIG_STAT_NONE
, "disconnected"},
143 {MIG_STAT_LISTEN
, "listening"},
144 {MIG_STAT_CONN
, "connected"},
145 {MIG_STAT_START
, "migration stared"},
146 {MIG_STAT_SUCC
, "migration completed successfully"},
147 {MIG_STAT_FAIL
, "migration failed"},
148 {MIG_STAT_CANCEL
, "migration canceled"}
153 for (i
=0 ; i
<sizeof(stat_strs
)/sizeof(stat_strs
[0]) ; i
++)
154 if (stat_strs
[i
].stat
== mig_stat
)
155 return stat_strs
[i
].str
;
157 return "unknown migration_status";
160 /* circular buffer functions */
161 static int migration_buffer_empty(migration_state_t
*pms
)
163 return (pms
->head
== pms
->tail
);
166 static int migration_buffer_bytes_filled(migration_state_t
*pms
)
168 return (pms
->head
- pms
->tail
) % pms
->buffsize
;
171 static int migration_buffer_bytes_empty(migration_state_t
*pms
)
173 return (pms
->tail
- pms
->head
-1) % pms
->buffsize
;
176 static int migration_buffer_bytes_head_end(migration_state_t
*pms
)
178 return pms
->buffsize
- pms
->head
;
181 static int migration_buffer_bytes_tail_end(migration_state_t
*pms
)
183 return pms
->buffsize
- pms
->tail
;
186 static void migration_state_inc_head(migration_state_t
*pms
, int n
)
188 pms
->head
= (pms
->head
+ n
) % pms
->buffsize
;
189 pms
->head_counter
+= n
;
192 static void migration_state_inc_tail(migration_state_t
*pms
, int n
)
194 pms
->tail
= (pms
->tail
+ n
) % pms
->buffsize
;
195 pms
->tail_counter
+= n
;
199 /* create a network address according to arg/default_addr */
200 static int parse_host_port_and_message(struct sockaddr_in
*saddr
,
202 const char *default_addr
,
207 if (parse_host_port(saddr
, arg
) < 0) {
208 term_printf("%s: invalid argument '%s'\n", name
, arg
);
209 migration_cleanup(&ms
, MIG_STAT_FAIL
);
215 static void migration_reset_buffer(migration_state_t
*pms
)
217 memset(pms
->buff
, 0, pms
->buffsize
);
220 pms
->head_counter
= 0;
221 pms
->tail_counter
= 0;
224 static void migration_cleanup(migration_state_t
*pms
, migration_status_t stat
)
226 if (pms
->fd
!= FD_UNUSED
) {
227 #ifdef USE_NONBLOCKING_SOCKETS
228 qemu_set_fd_handler(pms
->fd
, NULL
, NULL
, NULL
);
236 static int migration_read_from_socket(void *opaque
)
238 migration_state_t
*pms
= (migration_state_t
*)opaque
;
241 if (pms
->status
!= MIG_STAT_START
)
243 if (pms
->fd
== FD_UNUSED
) /* not connected */
246 while (1) { /* breaking if O.K. */
247 size
= migration_buffer_bytes_empty(pms
); /* available size */
248 toend
= migration_buffer_bytes_head_end(pms
);
249 if (size
> toend
) /* read till end of buffer */
251 size
= read(pms
->fd
, pms
->buff
+ pms
->head
, size
);
253 if (socket_error() == EINTR
)
255 term_printf("migration_read_from_socket: read failed (%s)\n", strerror(errno
) );
256 migration_cleanup(pms
, MIG_STAT_FAIL
);
260 /* connection closed */
261 term_printf("migration_read_from_socket: CONNECTION CLOSED\n");
262 migration_cleanup(pms
, MIG_STAT_FAIL
);
263 /* FIXME: call vm_start on A or B according to migration status ? */
266 else /* we did read something */
270 migration_state_inc_head(pms
, size
);
274 static int migration_write_into_socket(void *opaque
, int len
)
276 migration_state_t
*pms
= (migration_state_t
*)opaque
;
279 if (pms
->status
!= MIG_STAT_START
)
281 if (pms
->fd
== FD_UNUSED
) /* not connected */
283 while (1) { /* breaking if O.K. */
284 size
= migration_buffer_bytes_filled(pms
); /* available size */
285 toend
= migration_buffer_bytes_tail_end(pms
);
286 if (size
> toend
) /* write till end of buffer */
290 size
= write(pms
->fd
, pms
->buff
+ pms
->tail
, size
);
292 if (socket_error() == EINTR
)
294 term_printf("migration_write_into_socket: write failed (%s)\n",
295 strerror(socket_error()) );
296 migration_cleanup(pms
, MIG_STAT_FAIL
);
300 /* connection closed */
301 term_printf("migration_write_into_socket: CONNECTION CLOSED\n");
302 migration_cleanup(pms
, MIG_STAT_FAIL
);
305 else /* we did write something */
309 migration_state_inc_tail(pms
, size
);
313 static void migration_start_now(void *opaque
)
315 migration_state_t
*pms
= (migration_state_t
*)opaque
;
317 migration_start_dst(pms
);
320 static void migration_accept(void *opaque
)
322 migration_state_t
*pms
= (migration_state_t
*)opaque
;
324 struct sockaddr_in sockaddr
;
328 len
= sizeof(sockaddr
);
329 new_fd
= accept(pms
->fd
, (struct sockaddr
*)&sockaddr
, &len
);
330 if (new_fd
< 0 && errno
!= EINTR
) {
331 term_printf("migration listen: accept failed (%s)\n",
333 migration_cleanup(pms
, MIG_STAT_FAIL
);
335 } else if (new_fd
>= 0) {
340 /* FIXME: Need to be modified if we want to have a control connection
343 migration_cleanup(pms
, MIG_STAT_CONN
); /* clean old fd */
346 term_printf("accepted new socket as fd %d\n", pms
->fd
);
348 #ifdef USE_NONBLOCKING_SOCKETS
349 /* start handling I/O */
350 qemu_set_fd_handler(pms
->fd
, migration_start_now
, NULL
, pms
);
352 term_printf("waiting for migration to start...\n");
353 migration_start_now(pms
);
358 void do_migration_listen(char *arg1
, char *arg2
)
360 struct sockaddr_in local
, remote
;
363 if (ms
.fd
!= FD_UNUSED
) {
364 term_printf("Already listening or connection established\n");
368 if (parse_host_port_and_message(&local
, arg1
, reader_default_addr
, "migration listen"))
371 if (parse_host_port_and_message(&remote
, arg2
, writer_default_addr
, "migration listen"))
374 ms
.fd
= socket(PF_INET
, SOCK_STREAM
, 0);
376 term_printf("migration listen: socket() failed (%s)\n",
378 migration_cleanup(&ms
, MIG_STAT_FAIL
);
382 /* fast reuse of address */
384 setsockopt(ms
.fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
386 if (bind(ms
.fd
, &local
, sizeof local
) < 0 ) {
387 migration_cleanup(&ms
, MIG_STAT_FAIL
);
388 term_printf("migration listen: bind() failed (%s)\n", strerror(errno
));
392 if (listen(ms
.fd
, 1) < 0) { /* allow only one connection */
393 migration_cleanup(&ms
, MIG_STAT_FAIL
);
394 term_printf("migration listen: listen() failed (%s)\n", strerror(errno
));
398 ms
.status
= MIG_STAT_LISTEN
;
399 term_printf("migration listen: listening on fd %d\n", ms
.fd
);
401 #ifdef USE_NONBLOCKING_SOCKETS
402 /* FIXME: should I allow BLOCKING socket after vm_stop() to get full bandwidth? */
403 socket_set_nonblock(ms
.fd
); /* do not block and delay the guest */
405 qemu_set_fd_handler(ms
.fd
, migration_accept
, NULL
, &ms
); /* wait for connect() */
407 migration_accept(&ms
);
411 /* reads from the socket if needed
412 * returns 0 if connection closed
413 * >0 if buffer is not empty, number of bytes filled
416 static int migration_read_some(void)
420 if (migration_buffer_empty(&ms
))
421 size
= migration_read_from_socket(&ms
);
423 size
= migration_buffer_bytes_filled(&ms
);
428 /* returns the byte read or 0 on error/connection closed */
429 int migration_read_byte(void)
433 if (migration_read_some() > 0) {
434 val
= ms
.buff
[ms
.tail
];
435 migration_state_inc_tail(&ms
, 1);
440 /* returns >=0 the number of bytes actually read,
441 * or <0 if error occured
443 int migration_read_buffer(char *buff
, int len
)
445 int size
, toend
, len_req
= len
;
447 size
= migration_read_some();
452 toend
= migration_buffer_bytes_tail_end(&ms
);
457 memcpy(buff
, &ms
.buff
[ms
.tail
], size
);
458 migration_state_inc_tail(&ms
, size
);
462 return len_req
- len
;
468 * buffer the bytes, and send when threshold reached
469 * FIXME: bandwidth control can be implemented here
470 * returns 0 on success, <0 on error
472 static int migration_write_some(int force
)
474 int size
, threshold
= 1024;
476 if (ms
.status
!= MIG_STAT_START
)
479 if (threshold
>= ms
.buffsize
) /* if buffsize is small */
480 threshold
= ms
.buffsize
/ 2;
481 size
= migration_buffer_bytes_filled(&ms
);
482 while (size
&& (force
|| (size
> threshold
))) {
483 size
= migration_write_into_socket(&ms
, size
);
484 if (size
< 0) /* error */
486 if (size
== 0) { /* connection closed -- announce ERROR */
487 term_printf("migration: other side closed connection\n");
490 size
= migration_buffer_bytes_filled(&ms
);
495 static int migration_write_byte(int val
)
499 rc
= migration_write_some(0);
502 ms
.buff
[ms
.head
] = val
;
503 migration_state_inc_head(&ms
, 1);
508 static int migration_write_buffer(const char *buff
, int len
)
510 int size
, toend
, len_req
= len
;
513 if (migration_write_some(0) < 0)
515 size
= migration_buffer_bytes_empty(&ms
);
516 toend
= migration_buffer_bytes_head_end(&ms
);
521 memcpy(&ms
.buff
[ms
.head
], buff
, size
);
522 migration_state_inc_head(&ms
, size
);
526 return len_req
- len
;
529 static void migration_connect_check(void *opaque
)
531 migration_state_t
*pms
= (migration_state_t
*)opaque
;
533 socklen_t len
=sizeof(err
);
535 rc
= getsockopt(pms
->fd
, SOL_SOCKET
, SO_ERROR
, (void *)&err
, &len
);
537 term_printf("migration connect: getsockopt FAILED (%s)\n", strerror(errno
));
538 migration_cleanup(pms
, MIG_STAT_FAIL
);
542 term_printf("migration connect: connected through fd %d\n", pms
->fd
);
543 pms
->status
= MIG_STAT_CONN
;
546 term_printf("migration connect: failed to conenct (%s)\n", strerror(err
));
547 migration_cleanup(pms
, MIG_STAT_FAIL
);
551 #ifdef USE_NONBLOCKING_SOCKETS
552 qemu_set_fd_handler(pms
->fd
, migration_start_now
, NULL
, pms
);
557 void do_migration_connect(char *arg1
, char *arg2
)
559 struct sockaddr_in local
, remote
;
561 if (ms
.fd
!= FD_UNUSED
) {
562 term_printf("Already connecting or connection established\n");
568 if (parse_host_port_and_message(&local
, arg1
, writer_default_addr
, "migration connect"))
571 if (parse_host_port_and_message(&remote
, arg2
, reader_default_addr
, "migration connect"))
574 ms
.fd
= socket(PF_INET
, SOCK_STREAM
, 0);
576 term_printf("migration connect: socket() failed (%s)\n",
578 migration_cleanup(&ms
, MIG_STAT_FAIL
);
582 #ifdef USE_NONBLOCKING_SOCKETS
583 socket_set_nonblock(ms
.fd
);
584 qemu_set_fd_handler(ms
.fd
, NULL
, migration_connect_check
, &ms
);
587 while (connect(ms
.fd
, (struct sockaddr
*)&remote
, sizeof remote
) < 0) {
590 if (errno
!= EINPROGRESS
) {
591 term_printf("migration connect: connect() failed (%s)\n",
593 migration_cleanup(&ms
, MIG_STAT_FAIL
);
598 migration_connect_check(&ms
);
601 static void migration_disconnect(void *opaque
)
603 migration_state_t
*pms
= (migration_state_t
*)opaque
;
604 migration_cleanup(pms
, pms
->status
);
607 static void migration_phase_set(migration_state_t
*pms
, int phase
)
609 int64_t t
= qemu_get_clock(rt_clock
);
611 term_printf("migration: starting phase %d at %" PRId64
"\n",
615 static void migration_phase_inc(migration_state_t
*pms
)
617 migration_phase_set(pms
, pms
->phase
+ 1);
620 /* four phases for the migration:
621 * phase 0: initialization
622 * phase 1: online or offline
623 * transfer all RAM pages (online only)
624 * enable dirty pages logging (for offline migration the whole ram is dirty)
626 * phase 2: online only
627 * repeat: transfer all dirty pages
631 * transfer whatever left (dirty pages + non-ram states)
632 * for offline migration dirty pages are the whole memory.
634 * phase 4: offline or online
635 * The grand finale: decide with host should continue
636 * send a "to whom it may concern..."
639 * The function migration_main_loop just runs the appropriate function
640 * according to phase.
643 void migration_main_loop(void *opaque
)
645 migration_state_t
*pms
= (migration_state_t
*)opaque
;
648 while (! pms
->yield
) {
649 if (pms
->status
!= MIG_STAT_START
)
650 pms
->phase
= MIGRATION_NUM_PHASES
-1; /* last phase -- report */
651 if (MIG_ASSERT(pms
->phase
< MIGRATION_NUM_PHASES
))
653 migration_phase_funcs
[pms
->role
-1][pms
->phase
](pms
);
657 static void migration_start_common(migration_state_t
*pms
)
661 if (pms
->status
!= MIG_STAT_CONN
) {
662 switch (pms
->status
) {
666 case MIG_STAT_CANCEL
:
667 term_printf("migration start: not connected to peer\n");
670 term_printf("migration start: migration already running\n");
673 term_printf("migration start: UNKNOWN state %d\n", pms
->status
);
678 #ifdef USE_NONBLOCKING_SOCKETS
679 qemu_set_fd_handler(pms
->fd
, NULL
, NULL
, NULL
);
680 socket_set_block(pms
->fd
); /* read as fast as you can */
683 start_time
= qemu_get_clock(rt_clock
);
684 term_printf("\nstarting migration (at %" PRId64
")\n", start_time
);
685 migration_phase_set(pms
, 0);
686 migration_reset_buffer(pms
);
687 pms
->status
= MIG_STAT_START
;
689 pms
->f
= &qemu_savevm_method_socket
;
690 pms
->f
->open(pms
->f
, NULL
, NULL
);
692 migration_phase_inc(pms
);
693 migration_main_loop(pms
);
696 static void migration_start_src(migration_state_t
*pms
)
699 migration_start_common(pms
);
702 static void migration_start_dst(migration_state_t
*pms
)
705 migration_start_common(pms
);
709 static void migration_phase_1_src(migration_state_t
*pms
)
711 int goto_next_phase
= 1;
713 if (pms
->next_page
== 0) {
715 qemu_put_be32(pms
->f
, QEMU_MIGRATION_MAGIC
);
716 qemu_put_be32(pms
->f
, QEMU_MIGRATION_VERSION
);
717 qemu_put_byte(pms
->f
, pms
->online
);
718 qemu_put_be32(pms
->f
, phys_ram_size
>> TARGET_PAGE_BITS
);
719 qemu_set_fd_handler(pms
->fd
, NULL
, migration_main_loop
, pms
);
720 for (addr
=0; addr
<phys_ram_size
; addr
+=TARGET_PAGE_SIZE
)
721 cpu_physical_memory_set_dirty_flags(addr
, MIG_DIRTY_FLAG
);
725 migration_ram_send(pms
, 0);
726 if (pms
->next_page
< (phys_ram_size
>> TARGET_PAGE_BITS
)) {
731 if (goto_next_phase
) {
732 qemu_put_byte(pms
->f
, MIG_XFER_PAGE_TYPE_END
);
733 migration_phase_inc(pms
);
734 qemu_set_fd_handler(pms
->fd
, NULL
, NULL
, pms
);
740 static void migration_phase_2_src(migration_state_t
*pms
)
742 migration_phase_inc(pms
);
745 static void migration_phase_3_common(migration_state_t
*pms
,
746 int (*migrate
)(const char*, QEMUFile
*))
748 const char *dummy
= "migrating";
751 vm_stop(EXCP_INTERRUPT
); /* FIXME: use EXCP_MIGRATION ? */
752 rc
= migrate(dummy
, &qemu_savevm_method_socket
);
753 if ((rc
==0) && (pms
->status
== MIG_STAT_START
))
754 pms
->status
= MIG_STAT_SUCC
;
756 if (pms
->status
== MIG_STAT_START
)
757 pms
->status
= MIG_STAT_FAIL
;
759 migration_phase_inc(pms
);
761 static void migration_phase_3_src(migration_state_t
*pms
)
763 migration_phase_3_common(pms
, qemu_savevm
);
765 static void migration_phase_4_common(migration_state_t
*pms
, int cont
)
767 int64_t end_time
= qemu_get_clock(rt_clock
);
768 term_printf("migration %s at %" PRId64
"\n",
769 (pms
->status
!=MIG_STAT_SUCC
)?"failed":"completed successfully",
772 migration_cleanup(pms
, pms
->status
);
776 if (pms
->fd
!= FD_UNUSED
)
777 qemu_set_fd_handler(pms
->fd
, migration_disconnect
, NULL
, pms
);
782 static void migration_phase_4_src(migration_state_t
*pms
)
784 migration_phase_4_common(pms
, pms
->status
!= MIG_STAT_SUCC
);
787 static void migration_phase_1_dst(migration_state_t
*pms
)
789 uint32_t magic
, version
, online
, npages
;
791 if (pms
->next_page
== 0) {
792 magic
= qemu_get_be32(pms
->f
);
793 version
= qemu_get_be32(pms
->f
);
794 online
= qemu_get_byte(pms
->f
);
795 npages
= qemu_get_be32(pms
->f
);
797 if ((magic
!= QEMU_MIGRATION_MAGIC
) ||
798 (version
!= QEMU_MIGRATION_VERSION
)) {
799 term_printf("migration header: recv 0x%x 0x%x expecting 0x%x 0x%x\n",
801 QEMU_MIGRATION_MAGIC
, QEMU_MIGRATION_VERSION
);
802 migration_cleanup(pms
, MIG_STAT_FAIL
);
806 if (npages
!= (phys_ram_size
>> TARGET_PAGE_BITS
)) {
807 term_printf("phys_memory_mismatch: %uMB %uMB\n",
808 npages
>> (20-TARGET_PAGE_BITS
), phys_ram_size
>>20);
809 migration_cleanup(pms
, MIG_STAT_FAIL
);
812 pms
->online
= online
;
813 term_printf("===>received online=%u\n", online
);
816 migration_ram_recv(pms
);
818 if (pms
->next_page
>= (phys_ram_size
>> TARGET_PAGE_BITS
)) {
819 migration_phase_inc(pms
);
822 static void migration_phase_2_dst(migration_state_t
*pms
)
824 migration_phase_inc(pms
);
826 static void migration_phase_3_dst(migration_state_t
*pms
)
828 migration_phase_3_common(pms
, qemu_loadvm
);
830 static void migration_phase_4_dst(migration_state_t
*pms
)
832 migration_phase_4_common(pms
, pms
->status
== MIG_STAT_SUCC
);
837 * FIXME: make it share code in vl.c
839 static int ram_page_homogeneous(const uint8_t *buf
, const int len
)
844 for (i
=1; i
<len
; i
++)
850 static void mig_ram_dirty_reset_page(unsigned page_number
)
852 ram_addr_t start
, end
;
853 start
= page_number
<< TARGET_PAGE_BITS
;
854 end
= start
+ TARGET_PAGE_SIZE
;
855 cpu_physical_memory_reset_dirty(start
, end
, MIG_DIRTY_FLAG
);
859 * Sends a single ram page
860 * As in vl.c a single byte is being sent as data if page is "homogeneous"
863 * byte -- migration transfer page type
864 * uint32 -- page number
866 * a single byte or the whole page (TARGET_PAGE_SIZE bytes).
868 static void mig_send_ram_page(migration_state_t
*pms
, unsigned page_number
)
870 const uint8_t* ptr
= (const uint8_t *)(unsigned long)phys_ram_base
;
874 if (page_number
>= (phys_ram_size
>> TARGET_PAGE_BITS
)) {
875 term_printf("mig_send_ram_page: page_number is too large: %u (max is %u)\n",
876 page_number
, (phys_ram_size
>> TARGET_PAGE_BITS
));
877 migration_cleanup(pms
, MIG_STAT_FAIL
);
881 ptr
+= (page_number
<< TARGET_PAGE_BITS
);
882 if (ram_page_homogeneous(ptr
, TARGET_PAGE_SIZE
)) {
883 val
= MIG_XFER_PAGE_TYPE_HOMOGENEOUS
;
887 val
= MIG_XFER_PAGE_TYPE_REGULAR
;
888 buflen
= TARGET_PAGE_SIZE
;
890 qemu_put_byte(pms
->f
, val
);
891 qemu_put_be32(pms
->f
, page_number
);
892 qemu_put_buffer(pms
->f
, ptr
, buflen
);
894 mig_ram_dirty_reset_page(page_number
);
897 /* returns 0 on success,
898 * 1 if this phase is over
901 static int mig_recv_ram_page(migration_state_t
*pms
)
903 uint8_t *ptr
= (uint8_t *)(unsigned long)phys_ram_base
;
904 unsigned page_number
;
908 val
= qemu_get_byte(pms
->f
);
910 case MIG_XFER_PAGE_TYPE_END
: /* go to the next phase */;
911 pms
->next_page
= phys_ram_size
>> TARGET_PAGE_BITS
;
913 case MIG_XFER_PAGE_TYPE_REGULAR
:
914 buflen
= TARGET_PAGE_SIZE
;
916 case MIG_XFER_PAGE_TYPE_HOMOGENEOUS
:
920 term_printf("mig_recv_ram_page: illegal val received %d\n", val
);
921 migration_cleanup(pms
, MIG_STAT_FAIL
);
925 page_number
= qemu_get_be32(pms
->f
);
926 if (page_number
>= (phys_ram_size
>> TARGET_PAGE_BITS
)) {
927 term_printf("mig_recv_ram_page: page_number is too large: %u (max is %u)\n",
928 page_number
, (phys_ram_size
>> TARGET_PAGE_BITS
));
932 ptr
+= (page_number
<< TARGET_PAGE_BITS
);
933 qemu_get_buffer(pms
->f
, ptr
, buflen
);
935 if (val
== MIG_XFER_PAGE_TYPE_HOMOGENEOUS
)
936 memset(ptr
, ptr
[0], TARGET_PAGE_SIZE
);
942 /* In order to enable the guest to run while memory is transferred,
943 * the number of page continuously sent is limited by this constant.
944 * When the limit is reached we take a break and continue to send pages
945 * upon another call to migration_ram_send (which would be when data can
946 * be sent over the socket ( using qemu_set_fd_handler() ).
948 #define PAGES_CHUNK ((phys_ram_size >> TARGET_PAGE_BITS) /16 )
950 /* Sends the whole ram in chunks, each call a few pages are being sent
951 * (needs to be called multiple times).
952 * State is kept in pms->next_page.
954 static void migration_ram_send(migration_state_t
*pms
, int whole_ram
)
956 unsigned num_pages
= (phys_ram_size
>> TARGET_PAGE_BITS
);
965 /* send a few pages (or until network buffers full) */
966 if (num_pages
- pms
->next_page
> chunk
) {
967 num_pages
= pms
->next_page
+ chunk
;
969 for ( /*none*/ ; pms
->next_page
< num_pages
; pms
->next_page
++) {
970 addr
= pms
->next_page
<< TARGET_PAGE_BITS
;
971 if ((kvm_allowed
) && (addr
>= 0xa0000) && (addr
< 0xc0000))
973 if (cpu_physical_memory_get_dirty(addr
, MIG_DIRTY_FLAG
))
974 mig_send_ram_page(pms
, pms
->next_page
);
978 /* recv the whole ram (first phase) */
979 static void migration_ram_recv(migration_state_t
*pms
)
985 num_pages
= phys_ram_size
>> TARGET_PAGE_BITS
;
987 for (/* none */ ; rc
==0 ; pms
->next_page
++) {
988 addr
= pms
->next_page
<< TARGET_PAGE_BITS
;
989 if ((kvm_allowed
) && (addr
>= 0xa0000) && (addr
< 0xc0000))
991 rc
= mig_recv_ram_page(pms
);
993 term_printf("mig_recv_ram_page FAILED after %u pages\n", pms
->next_page
);
994 migration_cleanup(pms
, MIG_STAT_FAIL
);
999 if (pms
->next_page
< num_pages
)
1000 term_printf("migration_ram_recv: WARNING goto next phase after %u pages (of %u)\n",
1001 pms
->next_page
, num_pages
);
1004 void do_migration_getfd(int fd
) { TO_BE_IMPLEMENTED
; }
1005 void do_migration_start(char *deadoralive
)
1007 if (strcmp(deadoralive
, "online") == 0) {
1009 if (kvm_allowed
) { /* online migration is not supported yet for kvm */
1011 term_printf("Currently online migration is not supported for kvm,"
1012 " using offline migration\n");
1015 else if (strcmp(deadoralive
, "offline") == 0)
1018 term_printf("migration start: please specify 'online' or 'offline'\n");
1021 migration_start_src(&ms
);
1024 void do_migration_cancel(void)
1026 migration_cleanup(&ms
, MIG_STAT_CANCEL
);
1028 void do_migration_status(void){
1029 term_printf("migration status: %s\n", mig_stat_str(ms
.status
));
1031 void do_migration_set_rate(int min
, int max
, int offline
)
1033 if ((min
<0) || (max
<0) || (offline
<0)) {
1034 term_printf("%s: positive values only please\n", __FUNCTION__
);
1039 ms
.bw
.offline
= offline
;
1042 void do_migration_set_total_time(int seconds
)
1045 term_printf("%s: positive values only please\n", __FUNCTION__
);
1048 ms
.bw
.seconds
= seconds
;
1050 void do_migration_show(void)
1052 term_printf("%8s %8s %8s %8s\n%8d %8d %8d %8d\n",
1053 "min", "max", "offline", "seconds",
1054 ms
.bw
.min
, ms
.bw
.max
, ms
.bw
.offline
, ms
.bw
.seconds
);
1060 * =============================================
1061 * qemu_savevm_method implementation for sockets
1062 * =============================================
1064 static int qemu_savevm_method_socket_open(QEMUFile
*f
, const char *filename
,
1067 if (ms
.fd
== FD_UNUSED
)
1069 f
->opaque
= (void*)&ms
;
1073 static void qemu_savevm_method_socket_close(QEMUFile
*f
)
1075 migration_state_t
*pms
= (migration_state_t
*)f
->opaque
;
1076 if (pms
->role
== WRITER
) {
1077 migration_write_some(1); /* sync */
1081 static void qemu_savevm_method_socket_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
1083 migration_write_buffer(buf
, size
);
1086 static void qemu_savevm_method_socket_put_byte(QEMUFile
*f
, int v
)
1088 migration_write_byte(v
);
1091 static int qemu_savevm_method_socket_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
1093 return migration_read_buffer(buf
, size
);
1096 static int qemu_savevm_method_socket_get_byte(QEMUFile
*f
)
1098 return migration_read_byte();
1101 static int64_t qemu_savevm_method_socket_tell(QEMUFile
*f
)
1103 migration_state_t
*pms
= (migration_state_t
*)f
->opaque
;
1105 if (pms
->role
== WRITER
)
1106 cnt
= pms
->head_counter
;
1107 else if (pms
->role
== READER
)
1108 cnt
= pms
->tail_counter
;
1113 * hack alert: written to overcome a weakness of our solution (not yet generic).
1114 * READER: read 4 bytes of the actual length (and maybe do something)
1115 * WRITER: ignore (or maybe do something)
1117 static int64_t qemu_savevm_method_socket_seek(QEMUFile
*f
, int64_t pos
, int whence
)
1119 migration_state_t
*pms
= (migration_state_t
*)f
->opaque
;
1120 unsigned int record_len
;
1122 if (pms
->role
== READER
) {
1123 record_len
= qemu_get_be32(f
);
1128 static int qemu_savevm_method_socket_eof(QEMUFile
*f
)
1130 migration_state_t
*pms
= (migration_state_t
*)f
->opaque
;
1132 return (pms
->fd
== FD_UNUSED
);
1136 static void qemu_savevm_method_socket_ram_save(QEMUFile
*f
, void *opaque
)
1138 migration_state_t
*pms
= (migration_state_t
*)f
->opaque
;
1141 migration_ram_send(pms
, 1);
1142 qemu_put_byte(pms
->f
, MIG_XFER_PAGE_TYPE_END
);
1145 static int qemu_savevm_method_socket_ram_load(QEMUFile
*f
, void *opaque
, int ver
)
1147 migration_state_t
*pms
= (migration_state_t
*)f
->opaque
;
1150 migration_ram_recv(pms
);
1153 QEMUFile qemu_savevm_method_socket
= {
1155 .open
= qemu_savevm_method_socket_open
,
1156 .close
= qemu_savevm_method_socket_close
,
1157 .put_byte
= qemu_savevm_method_socket_put_byte
,
1158 .get_byte
= qemu_savevm_method_socket_get_byte
,
1159 .put_buffer
= qemu_savevm_method_socket_put_buffer
,
1160 .get_buffer
= qemu_savevm_method_socket_get_buffer
,
1161 .tell
= qemu_savevm_method_socket_tell
,
1162 .seek
= qemu_savevm_method_socket_seek
,
1163 .eof
= qemu_savevm_method_socket_eof
,
1164 .ram_save
= qemu_savevm_method_socket_ram_save
,
1165 .ram_load
= qemu_savevm_method_socket_ram_load
,
1172 #else /* CONFIG_USER_ONLY is defined */
1174 void do_migration_listen(char *arg1
, char *arg2
) { TO_BE_IMPLEMENTED
; }
1175 void do_migration_connect(char *arg1
, char *arg2
) { TO_BE_IMPLEMENTED
; }
1176 void do_migration_getfd(int fd
) { TO_BE_IMPLEMENTED
; }
1177 void do_migration_start(char *deadoralive
) { TO_BE_IMPLEMENTED
; }
1178 void do_migration_cancel(void){ TO_BE_IMPLEMENTED
; }
1179 void do_migration_status(void){ TO_BE_IMPLEMENTED
; }
1180 void do_migration_set_rate(int min
, int max
, int offline
) { TO_BE_IMPLEMENTED
; }
1181 void do_migration_set_total_time(int seconds
) { TO_BE_IMPLEMENTED
; }
1182 void do_migration_show(void){ TO_BE_IMPLEMENTED
; }
1184 #endif /* of CONFIG_USER_ONLY is defined */