1 #include "qemu-common.h"
3 #include "qemu/sockets.h"
4 #include "block/coroutine.h"
5 #include "migration/migration.h"
6 #include "migration/qemu-file.h"
8 #define IO_BUF_SIZE 32768
9 #define MAX_IOV_SIZE MIN(IOV_MAX, 64)
12 const QEMUFileOps
*ops
;
18 int64_t pos
; /* start of buffer when writing, end of buffer
21 int buf_size
; /* 0 when writing */
22 uint8_t buf
[IO_BUF_SIZE
];
24 struct iovec iov
[MAX_IOV_SIZE
];
30 typedef struct QEMUFileStdio
{
35 typedef struct QEMUFileSocket
{
40 static ssize_t
socket_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
,
43 QEMUFileSocket
*s
= opaque
;
45 ssize_t size
= iov_size(iov
, iovcnt
);
47 len
= iov_send(s
->fd
, iov
, iovcnt
, 0, size
);
49 len
= -socket_error();
54 static int socket_get_fd(void *opaque
)
56 QEMUFileSocket
*s
= opaque
;
61 static int socket_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
63 QEMUFileSocket
*s
= opaque
;
67 len
= qemu_recv(s
->fd
, buf
, size
, 0);
71 if (socket_error() == EAGAIN
) {
72 yield_until_fd_readable(s
->fd
);
73 } else if (socket_error() != EINTR
) {
79 len
= -socket_error();
84 static int socket_close(void *opaque
)
86 QEMUFileSocket
*s
= opaque
;
92 static int stdio_get_fd(void *opaque
)
94 QEMUFileStdio
*s
= opaque
;
96 return fileno(s
->stdio_file
);
99 static int stdio_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
,
102 QEMUFileStdio
*s
= opaque
;
105 res
= fwrite(buf
, 1, size
, s
->stdio_file
);
108 return -EIO
; /* fake errno value */
113 static int stdio_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
115 QEMUFileStdio
*s
= opaque
;
116 FILE *fp
= s
->stdio_file
;
121 bytes
= fread(buf
, 1, size
, fp
);
122 if (bytes
!= 0 || !ferror(fp
)) {
125 if (errno
== EAGAIN
) {
126 yield_until_fd_readable(fileno(fp
));
127 } else if (errno
!= EINTR
) {
134 static int stdio_pclose(void *opaque
)
136 QEMUFileStdio
*s
= opaque
;
138 ret
= pclose(s
->stdio_file
);
141 } else if (!WIFEXITED(ret
) || WEXITSTATUS(ret
) != 0) {
142 /* close succeeded, but non-zero exit code: */
143 ret
= -EIO
; /* fake errno value */
149 static int stdio_fclose(void *opaque
)
151 QEMUFileStdio
*s
= opaque
;
154 if (s
->file
->ops
->put_buffer
|| s
->file
->ops
->writev_buffer
) {
155 int fd
= fileno(s
->stdio_file
);
158 ret
= fstat(fd
, &st
);
159 if (ret
== 0 && S_ISREG(st
.st_mode
)) {
161 * If the file handle is a regular file make sure the
162 * data is flushed to disk before signaling success.
171 if (fclose(s
->stdio_file
) == EOF
) {
178 static const QEMUFileOps stdio_pipe_read_ops
= {
179 .get_fd
= stdio_get_fd
,
180 .get_buffer
= stdio_get_buffer
,
181 .close
= stdio_pclose
184 static const QEMUFileOps stdio_pipe_write_ops
= {
185 .get_fd
= stdio_get_fd
,
186 .put_buffer
= stdio_put_buffer
,
187 .close
= stdio_pclose
190 QEMUFile
*qemu_popen_cmd(const char *command
, const char *mode
)
195 if (mode
== NULL
|| (mode
[0] != 'r' && mode
[0] != 'w') || mode
[1] != 0) {
196 fprintf(stderr
, "qemu_popen: Argument validity check failed\n");
200 stdio_file
= popen(command
, mode
);
201 if (stdio_file
== NULL
) {
205 s
= g_malloc0(sizeof(QEMUFileStdio
));
207 s
->stdio_file
= stdio_file
;
209 if (mode
[0] == 'r') {
210 s
->file
= qemu_fopen_ops(s
, &stdio_pipe_read_ops
);
212 s
->file
= qemu_fopen_ops(s
, &stdio_pipe_write_ops
);
217 static const QEMUFileOps stdio_file_read_ops
= {
218 .get_fd
= stdio_get_fd
,
219 .get_buffer
= stdio_get_buffer
,
220 .close
= stdio_fclose
223 static const QEMUFileOps stdio_file_write_ops
= {
224 .get_fd
= stdio_get_fd
,
225 .put_buffer
= stdio_put_buffer
,
226 .close
= stdio_fclose
229 static ssize_t
unix_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
,
232 QEMUFileSocket
*s
= opaque
;
234 ssize_t size
= iov_size(iov
, iovcnt
);
240 /* Find the next start position; skip all full-sized vector elements */
241 while (offset
>= iov
[0].iov_len
) {
242 offset
-= iov
[0].iov_len
;
246 /* skip `offset' bytes from the (now) first element, undo it on exit */
248 iov
[0].iov_base
+= offset
;
249 iov
[0].iov_len
-= offset
;
252 len
= writev(s
->fd
, iov
, iovcnt
);
253 } while (len
== -1 && errno
== EINTR
);
258 /* Undo the changes above */
259 iov
[0].iov_base
-= offset
;
260 iov
[0].iov_len
+= offset
;
262 /* Prepare for the next iteration */
271 static int unix_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
273 QEMUFileSocket
*s
= opaque
;
277 len
= read(s
->fd
, buf
, size
);
281 if (errno
== EAGAIN
) {
282 yield_until_fd_readable(s
->fd
);
283 } else if (errno
!= EINTR
) {
294 static int unix_close(void *opaque
)
296 QEMUFileSocket
*s
= opaque
;
302 static const QEMUFileOps unix_read_ops
= {
303 .get_fd
= socket_get_fd
,
304 .get_buffer
= unix_get_buffer
,
308 static const QEMUFileOps unix_write_ops
= {
309 .get_fd
= socket_get_fd
,
310 .writev_buffer
= unix_writev_buffer
,
314 QEMUFile
*qemu_fdopen(int fd
, const char *mode
)
319 (mode
[0] != 'r' && mode
[0] != 'w') ||
320 mode
[1] != 'b' || mode
[2] != 0) {
321 fprintf(stderr
, "qemu_fdopen: Argument validity check failed\n");
325 s
= g_malloc0(sizeof(QEMUFileSocket
));
328 if (mode
[0] == 'r') {
329 s
->file
= qemu_fopen_ops(s
, &unix_read_ops
);
331 s
->file
= qemu_fopen_ops(s
, &unix_write_ops
);
336 static const QEMUFileOps socket_read_ops
= {
337 .get_fd
= socket_get_fd
,
338 .get_buffer
= socket_get_buffer
,
339 .close
= socket_close
342 static const QEMUFileOps socket_write_ops
= {
343 .get_fd
= socket_get_fd
,
344 .writev_buffer
= socket_writev_buffer
,
345 .close
= socket_close
348 bool qemu_file_mode_is_not_valid(const char *mode
)
351 (mode
[0] != 'r' && mode
[0] != 'w') ||
352 mode
[1] != 'b' || mode
[2] != 0) {
353 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
360 QEMUFile
*qemu_fopen_socket(int fd
, const char *mode
)
364 if (qemu_file_mode_is_not_valid(mode
)) {
368 s
= g_malloc0(sizeof(QEMUFileSocket
));
370 if (mode
[0] == 'w') {
371 qemu_set_block(s
->fd
);
372 s
->file
= qemu_fopen_ops(s
, &socket_write_ops
);
374 s
->file
= qemu_fopen_ops(s
, &socket_read_ops
);
379 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
383 if (qemu_file_mode_is_not_valid(mode
)) {
387 s
= g_malloc0(sizeof(QEMUFileStdio
));
389 s
->stdio_file
= fopen(filename
, mode
);
390 if (!s
->stdio_file
) {
394 if (mode
[0] == 'w') {
395 s
->file
= qemu_fopen_ops(s
, &stdio_file_write_ops
);
397 s
->file
= qemu_fopen_ops(s
, &stdio_file_read_ops
);
405 QEMUFile
*qemu_fopen_ops(void *opaque
, const QEMUFileOps
*ops
)
409 f
= g_malloc0(sizeof(QEMUFile
));
417 * Get last error for stream f
419 * Return negative error value if there has been an error on previous
420 * operations, return 0 if no error happened.
423 int qemu_file_get_error(QEMUFile
*f
)
425 return f
->last_error
;
428 void qemu_file_set_error(QEMUFile
*f
, int ret
)
430 if (f
->last_error
== 0) {
435 static inline bool qemu_file_is_writable(QEMUFile
*f
)
437 return f
->ops
->writev_buffer
|| f
->ops
->put_buffer
;
441 * Flushes QEMUFile buffer
443 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
446 void qemu_fflush(QEMUFile
*f
)
450 if (!qemu_file_is_writable(f
)) {
454 if (f
->ops
->writev_buffer
) {
456 ret
= f
->ops
->writev_buffer(f
->opaque
, f
->iov
, f
->iovcnt
, f
->pos
);
459 if (f
->buf_index
> 0) {
460 ret
= f
->ops
->put_buffer(f
->opaque
, f
->buf
, f
->pos
, f
->buf_index
);
469 qemu_file_set_error(f
, ret
);
473 void ram_control_before_iterate(QEMUFile
*f
, uint64_t flags
)
477 if (f
->ops
->before_ram_iterate
) {
478 ret
= f
->ops
->before_ram_iterate(f
, f
->opaque
, flags
);
480 qemu_file_set_error(f
, ret
);
485 void ram_control_after_iterate(QEMUFile
*f
, uint64_t flags
)
489 if (f
->ops
->after_ram_iterate
) {
490 ret
= f
->ops
->after_ram_iterate(f
, f
->opaque
, flags
);
492 qemu_file_set_error(f
, ret
);
497 void ram_control_load_hook(QEMUFile
*f
, uint64_t flags
)
501 if (f
->ops
->hook_ram_load
) {
502 ret
= f
->ops
->hook_ram_load(f
, f
->opaque
, flags
);
504 qemu_file_set_error(f
, ret
);
507 qemu_file_set_error(f
, ret
);
511 size_t ram_control_save_page(QEMUFile
*f
, ram_addr_t block_offset
,
512 ram_addr_t offset
, size_t size
, int *bytes_sent
)
514 if (f
->ops
->save_page
) {
515 int ret
= f
->ops
->save_page(f
, f
->opaque
, block_offset
,
516 offset
, size
, bytes_sent
);
518 if (ret
!= RAM_SAVE_CONTROL_DELAYED
) {
519 if (bytes_sent
&& *bytes_sent
> 0) {
520 qemu_update_position(f
, *bytes_sent
);
521 } else if (ret
< 0) {
522 qemu_file_set_error(f
, ret
);
529 return RAM_SAVE_CONTROL_NOT_SUPP
;
532 static void qemu_fill_buffer(QEMUFile
*f
)
537 assert(!qemu_file_is_writable(f
));
539 pending
= f
->buf_size
- f
->buf_index
;
541 memmove(f
->buf
, f
->buf
+ f
->buf_index
, pending
);
544 f
->buf_size
= pending
;
546 len
= f
->ops
->get_buffer(f
->opaque
, f
->buf
+ pending
, f
->pos
,
547 IO_BUF_SIZE
- pending
);
551 } else if (len
== 0) {
552 qemu_file_set_error(f
, -EIO
);
553 } else if (len
!= -EAGAIN
) {
554 qemu_file_set_error(f
, len
);
558 int qemu_get_fd(QEMUFile
*f
)
560 if (f
->ops
->get_fd
) {
561 return f
->ops
->get_fd(f
->opaque
);
566 void qemu_update_position(QEMUFile
*f
, size_t size
)
573 * Returns negative error value if any error happened on previous operations or
574 * while closing the file. Returns 0 or positive number on success.
576 * The meaning of return value on success depends on the specific backend
579 int qemu_fclose(QEMUFile
*f
)
583 ret
= qemu_file_get_error(f
);
586 int ret2
= f
->ops
->close(f
->opaque
);
591 /* If any error was spotted before closing, we should report it
592 * instead of the close() return value.
601 static void add_to_iovec(QEMUFile
*f
, const uint8_t *buf
, int size
)
603 /* check for adjacent buffer and coalesce them */
604 if (f
->iovcnt
> 0 && buf
== f
->iov
[f
->iovcnt
- 1].iov_base
+
605 f
->iov
[f
->iovcnt
- 1].iov_len
) {
606 f
->iov
[f
->iovcnt
- 1].iov_len
+= size
;
608 f
->iov
[f
->iovcnt
].iov_base
= (uint8_t *)buf
;
609 f
->iov
[f
->iovcnt
++].iov_len
= size
;
612 if (f
->iovcnt
>= MAX_IOV_SIZE
) {
617 void qemu_put_buffer_async(QEMUFile
*f
, const uint8_t *buf
, int size
)
619 if (!f
->ops
->writev_buffer
) {
620 qemu_put_buffer(f
, buf
, size
);
628 f
->bytes_xfer
+= size
;
629 add_to_iovec(f
, buf
, size
);
632 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
641 l
= IO_BUF_SIZE
- f
->buf_index
;
645 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
647 if (f
->ops
->writev_buffer
) {
648 add_to_iovec(f
, f
->buf
+ f
->buf_index
, l
);
651 if (f
->buf_index
== IO_BUF_SIZE
) {
654 if (qemu_file_get_error(f
)) {
662 void qemu_put_byte(QEMUFile
*f
, int v
)
668 f
->buf
[f
->buf_index
] = v
;
670 if (f
->ops
->writev_buffer
) {
671 add_to_iovec(f
, f
->buf
+ f
->buf_index
, 1);
674 if (f
->buf_index
== IO_BUF_SIZE
) {
679 void qemu_file_skip(QEMUFile
*f
, int size
)
681 if (f
->buf_index
+ size
<= f
->buf_size
) {
682 f
->buf_index
+= size
;
686 int qemu_peek_buffer(QEMUFile
*f
, uint8_t *buf
, int size
, size_t offset
)
691 assert(!qemu_file_is_writable(f
));
693 index
= f
->buf_index
+ offset
;
694 pending
= f
->buf_size
- index
;
695 if (pending
< size
) {
697 index
= f
->buf_index
+ offset
;
698 pending
= f
->buf_size
- index
;
704 if (size
> pending
) {
708 memcpy(buf
, f
->buf
+ index
, size
);
712 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
717 while (pending
> 0) {
720 res
= qemu_peek_buffer(f
, buf
, pending
, 0);
724 qemu_file_skip(f
, res
);
732 int qemu_peek_byte(QEMUFile
*f
, int offset
)
734 int index
= f
->buf_index
+ offset
;
736 assert(!qemu_file_is_writable(f
));
738 if (index
>= f
->buf_size
) {
740 index
= f
->buf_index
+ offset
;
741 if (index
>= f
->buf_size
) {
745 return f
->buf
[index
];
748 int qemu_get_byte(QEMUFile
*f
)
752 result
= qemu_peek_byte(f
, 0);
753 qemu_file_skip(f
, 1);
757 int64_t qemu_ftell(QEMUFile
*f
)
763 int qemu_file_rate_limit(QEMUFile
*f
)
765 if (qemu_file_get_error(f
)) {
768 if (f
->xfer_limit
> 0 && f
->bytes_xfer
> f
->xfer_limit
) {
774 int64_t qemu_file_get_rate_limit(QEMUFile
*f
)
776 return f
->xfer_limit
;
779 void qemu_file_set_rate_limit(QEMUFile
*f
, int64_t limit
)
781 f
->xfer_limit
= limit
;
784 void qemu_file_reset_rate_limit(QEMUFile
*f
)
789 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
791 qemu_put_byte(f
, v
>> 8);
795 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
797 qemu_put_byte(f
, v
>> 24);
798 qemu_put_byte(f
, v
>> 16);
799 qemu_put_byte(f
, v
>> 8);
803 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
805 qemu_put_be32(f
, v
>> 32);
809 unsigned int qemu_get_be16(QEMUFile
*f
)
812 v
= qemu_get_byte(f
) << 8;
813 v
|= qemu_get_byte(f
);
817 unsigned int qemu_get_be32(QEMUFile
*f
)
820 v
= qemu_get_byte(f
) << 24;
821 v
|= qemu_get_byte(f
) << 16;
822 v
|= qemu_get_byte(f
) << 8;
823 v
|= qemu_get_byte(f
);
827 uint64_t qemu_get_be64(QEMUFile
*f
)
830 v
= (uint64_t)qemu_get_be32(f
) << 32;
831 v
|= qemu_get_be32(f
);