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"
9 #define IO_BUF_SIZE 32768
10 #define MAX_IOV_SIZE MIN(IOV_MAX, 64)
13 const QEMUFileOps
*ops
;
19 int64_t pos
; /* start of buffer when writing, end of buffer
22 int buf_size
; /* 0 when writing */
23 uint8_t buf
[IO_BUF_SIZE
];
25 struct iovec iov
[MAX_IOV_SIZE
];
31 typedef struct QEMUFileStdio
{
36 typedef struct QEMUFileSocket
{
41 static ssize_t
socket_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
,
44 QEMUFileSocket
*s
= opaque
;
46 ssize_t size
= iov_size(iov
, iovcnt
);
48 len
= iov_send(s
->fd
, iov
, iovcnt
, 0, size
);
50 len
= -socket_error();
55 static int socket_get_fd(void *opaque
)
57 QEMUFileSocket
*s
= opaque
;
62 static int socket_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
64 QEMUFileSocket
*s
= opaque
;
68 len
= qemu_recv(s
->fd
, buf
, size
, 0);
72 if (socket_error() == EAGAIN
) {
73 yield_until_fd_readable(s
->fd
);
74 } else if (socket_error() != EINTR
) {
80 len
= -socket_error();
85 static int socket_close(void *opaque
)
87 QEMUFileSocket
*s
= opaque
;
93 static int stdio_get_fd(void *opaque
)
95 QEMUFileStdio
*s
= opaque
;
97 return fileno(s
->stdio_file
);
100 static int stdio_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
,
103 QEMUFileStdio
*s
= opaque
;
106 res
= fwrite(buf
, 1, size
, s
->stdio_file
);
114 static int stdio_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
116 QEMUFileStdio
*s
= opaque
;
117 FILE *fp
= s
->stdio_file
;
122 bytes
= fread(buf
, 1, size
, fp
);
123 if (bytes
!= 0 || !ferror(fp
)) {
126 if (errno
== EAGAIN
) {
127 yield_until_fd_readable(fileno(fp
));
128 } else if (errno
!= EINTR
) {
135 static int stdio_pclose(void *opaque
)
137 QEMUFileStdio
*s
= opaque
;
139 ret
= pclose(s
->stdio_file
);
142 } else if (!WIFEXITED(ret
) || WEXITSTATUS(ret
) != 0) {
143 /* close succeeded, but non-zero exit code: */
144 ret
= -EIO
; /* fake errno value */
150 static int stdio_fclose(void *opaque
)
152 QEMUFileStdio
*s
= opaque
;
155 if (s
->file
->ops
->put_buffer
|| s
->file
->ops
->writev_buffer
) {
156 int fd
= fileno(s
->stdio_file
);
159 ret
= fstat(fd
, &st
);
160 if (ret
== 0 && S_ISREG(st
.st_mode
)) {
162 * If the file handle is a regular file make sure the
163 * data is flushed to disk before signaling success.
172 if (fclose(s
->stdio_file
) == EOF
) {
179 static const QEMUFileOps stdio_pipe_read_ops
= {
180 .get_fd
= stdio_get_fd
,
181 .get_buffer
= stdio_get_buffer
,
182 .close
= stdio_pclose
185 static const QEMUFileOps stdio_pipe_write_ops
= {
186 .get_fd
= stdio_get_fd
,
187 .put_buffer
= stdio_put_buffer
,
188 .close
= stdio_pclose
191 QEMUFile
*qemu_popen_cmd(const char *command
, const char *mode
)
196 if (mode
== NULL
|| (mode
[0] != 'r' && mode
[0] != 'w') || mode
[1] != 0) {
197 fprintf(stderr
, "qemu_popen: Argument validity check failed\n");
201 stdio_file
= popen(command
, mode
);
202 if (stdio_file
== NULL
) {
206 s
= g_malloc0(sizeof(QEMUFileStdio
));
208 s
->stdio_file
= stdio_file
;
210 if (mode
[0] == 'r') {
211 s
->file
= qemu_fopen_ops(s
, &stdio_pipe_read_ops
);
213 s
->file
= qemu_fopen_ops(s
, &stdio_pipe_write_ops
);
218 static const QEMUFileOps stdio_file_read_ops
= {
219 .get_fd
= stdio_get_fd
,
220 .get_buffer
= stdio_get_buffer
,
221 .close
= stdio_fclose
224 static const QEMUFileOps stdio_file_write_ops
= {
225 .get_fd
= stdio_get_fd
,
226 .put_buffer
= stdio_put_buffer
,
227 .close
= stdio_fclose
230 static ssize_t
unix_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
,
233 QEMUFileSocket
*s
= opaque
;
235 ssize_t size
= iov_size(iov
, iovcnt
);
241 /* Find the next start position; skip all full-sized vector elements */
242 while (offset
>= iov
[0].iov_len
) {
243 offset
-= iov
[0].iov_len
;
247 /* skip `offset' bytes from the (now) first element, undo it on exit */
249 iov
[0].iov_base
+= offset
;
250 iov
[0].iov_len
-= offset
;
253 len
= writev(s
->fd
, iov
, iovcnt
);
254 } while (len
== -1 && errno
== EINTR
);
259 /* Undo the changes above */
260 iov
[0].iov_base
-= offset
;
261 iov
[0].iov_len
+= offset
;
263 /* Prepare for the next iteration */
272 static int unix_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
274 QEMUFileSocket
*s
= opaque
;
278 len
= read(s
->fd
, buf
, size
);
282 if (errno
== EAGAIN
) {
283 yield_until_fd_readable(s
->fd
);
284 } else if (errno
!= EINTR
) {
295 static int unix_close(void *opaque
)
297 QEMUFileSocket
*s
= opaque
;
303 static const QEMUFileOps unix_read_ops
= {
304 .get_fd
= socket_get_fd
,
305 .get_buffer
= unix_get_buffer
,
309 static const QEMUFileOps unix_write_ops
= {
310 .get_fd
= socket_get_fd
,
311 .writev_buffer
= unix_writev_buffer
,
315 QEMUFile
*qemu_fdopen(int fd
, const char *mode
)
320 (mode
[0] != 'r' && mode
[0] != 'w') ||
321 mode
[1] != 'b' || mode
[2] != 0) {
322 fprintf(stderr
, "qemu_fdopen: Argument validity check failed\n");
326 s
= g_malloc0(sizeof(QEMUFileSocket
));
329 if (mode
[0] == 'r') {
330 s
->file
= qemu_fopen_ops(s
, &unix_read_ops
);
332 s
->file
= qemu_fopen_ops(s
, &unix_write_ops
);
337 static const QEMUFileOps socket_read_ops
= {
338 .get_fd
= socket_get_fd
,
339 .get_buffer
= socket_get_buffer
,
340 .close
= socket_close
343 static const QEMUFileOps socket_write_ops
= {
344 .get_fd
= socket_get_fd
,
345 .writev_buffer
= socket_writev_buffer
,
346 .close
= socket_close
349 bool qemu_file_mode_is_not_valid(const char *mode
)
352 (mode
[0] != 'r' && mode
[0] != 'w') ||
353 mode
[1] != 'b' || mode
[2] != 0) {
354 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
361 QEMUFile
*qemu_fopen_socket(int fd
, const char *mode
)
365 if (qemu_file_mode_is_not_valid(mode
)) {
369 s
= g_malloc0(sizeof(QEMUFileSocket
));
371 if (mode
[0] == 'w') {
372 qemu_set_block(s
->fd
);
373 s
->file
= qemu_fopen_ops(s
, &socket_write_ops
);
375 s
->file
= qemu_fopen_ops(s
, &socket_read_ops
);
380 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
384 if (qemu_file_mode_is_not_valid(mode
)) {
388 s
= g_malloc0(sizeof(QEMUFileStdio
));
390 s
->stdio_file
= fopen(filename
, mode
);
391 if (!s
->stdio_file
) {
395 if (mode
[0] == 'w') {
396 s
->file
= qemu_fopen_ops(s
, &stdio_file_write_ops
);
398 s
->file
= qemu_fopen_ops(s
, &stdio_file_read_ops
);
406 QEMUFile
*qemu_fopen_ops(void *opaque
, const QEMUFileOps
*ops
)
410 f
= g_malloc0(sizeof(QEMUFile
));
418 * Get last error for stream f
420 * Return negative error value if there has been an error on previous
421 * operations, return 0 if no error happened.
424 int qemu_file_get_error(QEMUFile
*f
)
426 return f
->last_error
;
429 void qemu_file_set_error(QEMUFile
*f
, int ret
)
431 if (f
->last_error
== 0) {
436 static inline bool qemu_file_is_writable(QEMUFile
*f
)
438 return f
->ops
->writev_buffer
|| f
->ops
->put_buffer
;
442 * Flushes QEMUFile buffer
444 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
447 void qemu_fflush(QEMUFile
*f
)
451 if (!qemu_file_is_writable(f
)) {
455 if (f
->ops
->writev_buffer
) {
457 ret
= f
->ops
->writev_buffer(f
->opaque
, f
->iov
, f
->iovcnt
, f
->pos
);
460 if (f
->buf_index
> 0) {
461 ret
= f
->ops
->put_buffer(f
->opaque
, f
->buf
, f
->pos
, f
->buf_index
);
470 qemu_file_set_error(f
, ret
);
474 void ram_control_before_iterate(QEMUFile
*f
, uint64_t flags
)
478 if (f
->ops
->before_ram_iterate
) {
479 ret
= f
->ops
->before_ram_iterate(f
, f
->opaque
, flags
);
481 qemu_file_set_error(f
, ret
);
486 void ram_control_after_iterate(QEMUFile
*f
, uint64_t flags
)
490 if (f
->ops
->after_ram_iterate
) {
491 ret
= f
->ops
->after_ram_iterate(f
, f
->opaque
, flags
);
493 qemu_file_set_error(f
, ret
);
498 void ram_control_load_hook(QEMUFile
*f
, uint64_t flags
)
502 if (f
->ops
->hook_ram_load
) {
503 ret
= f
->ops
->hook_ram_load(f
, f
->opaque
, flags
);
505 qemu_file_set_error(f
, ret
);
508 qemu_file_set_error(f
, ret
);
512 size_t ram_control_save_page(QEMUFile
*f
, ram_addr_t block_offset
,
513 ram_addr_t offset
, size_t size
, int *bytes_sent
)
515 if (f
->ops
->save_page
) {
516 int ret
= f
->ops
->save_page(f
, f
->opaque
, block_offset
,
517 offset
, size
, bytes_sent
);
519 if (ret
!= RAM_SAVE_CONTROL_DELAYED
) {
520 if (bytes_sent
&& *bytes_sent
> 0) {
521 qemu_update_position(f
, *bytes_sent
);
522 } else if (ret
< 0) {
523 qemu_file_set_error(f
, ret
);
530 return RAM_SAVE_CONTROL_NOT_SUPP
;
533 static void qemu_fill_buffer(QEMUFile
*f
)
538 assert(!qemu_file_is_writable(f
));
540 pending
= f
->buf_size
- f
->buf_index
;
542 memmove(f
->buf
, f
->buf
+ f
->buf_index
, pending
);
545 f
->buf_size
= pending
;
547 len
= f
->ops
->get_buffer(f
->opaque
, f
->buf
+ pending
, f
->pos
,
548 IO_BUF_SIZE
- pending
);
552 } else if (len
== 0) {
553 qemu_file_set_error(f
, -EIO
);
554 } else if (len
!= -EAGAIN
) {
555 qemu_file_set_error(f
, len
);
559 int qemu_get_fd(QEMUFile
*f
)
561 if (f
->ops
->get_fd
) {
562 return f
->ops
->get_fd(f
->opaque
);
567 void qemu_update_position(QEMUFile
*f
, size_t size
)
574 * Returns negative error value if any error happened on previous operations or
575 * while closing the file. Returns 0 or positive number on success.
577 * The meaning of return value on success depends on the specific backend
580 int qemu_fclose(QEMUFile
*f
)
584 ret
= qemu_file_get_error(f
);
587 int ret2
= f
->ops
->close(f
->opaque
);
592 /* If any error was spotted before closing, we should report it
593 * instead of the close() return value.
599 trace_qemu_file_fclose();
603 static void add_to_iovec(QEMUFile
*f
, const uint8_t *buf
, int size
)
605 /* check for adjacent buffer and coalesce them */
606 if (f
->iovcnt
> 0 && buf
== f
->iov
[f
->iovcnt
- 1].iov_base
+
607 f
->iov
[f
->iovcnt
- 1].iov_len
) {
608 f
->iov
[f
->iovcnt
- 1].iov_len
+= size
;
610 f
->iov
[f
->iovcnt
].iov_base
= (uint8_t *)buf
;
611 f
->iov
[f
->iovcnt
++].iov_len
= size
;
614 if (f
->iovcnt
>= MAX_IOV_SIZE
) {
619 void qemu_put_buffer_async(QEMUFile
*f
, const uint8_t *buf
, int size
)
621 if (!f
->ops
->writev_buffer
) {
622 qemu_put_buffer(f
, buf
, size
);
630 f
->bytes_xfer
+= size
;
631 add_to_iovec(f
, buf
, size
);
634 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
643 l
= IO_BUF_SIZE
- f
->buf_index
;
647 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
649 if (f
->ops
->writev_buffer
) {
650 add_to_iovec(f
, f
->buf
+ f
->buf_index
, l
);
653 if (f
->buf_index
== IO_BUF_SIZE
) {
656 if (qemu_file_get_error(f
)) {
664 void qemu_put_byte(QEMUFile
*f
, int v
)
670 f
->buf
[f
->buf_index
] = v
;
672 if (f
->ops
->writev_buffer
) {
673 add_to_iovec(f
, f
->buf
+ f
->buf_index
, 1);
676 if (f
->buf_index
== IO_BUF_SIZE
) {
681 void qemu_file_skip(QEMUFile
*f
, int size
)
683 if (f
->buf_index
+ size
<= f
->buf_size
) {
684 f
->buf_index
+= size
;
688 int qemu_peek_buffer(QEMUFile
*f
, uint8_t *buf
, int size
, size_t offset
)
693 assert(!qemu_file_is_writable(f
));
695 index
= f
->buf_index
+ offset
;
696 pending
= f
->buf_size
- index
;
697 if (pending
< size
) {
699 index
= f
->buf_index
+ offset
;
700 pending
= f
->buf_size
- index
;
706 if (size
> pending
) {
710 memcpy(buf
, f
->buf
+ index
, size
);
714 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
719 while (pending
> 0) {
722 res
= qemu_peek_buffer(f
, buf
, pending
, 0);
726 qemu_file_skip(f
, res
);
734 int qemu_peek_byte(QEMUFile
*f
, int offset
)
736 int index
= f
->buf_index
+ offset
;
738 assert(!qemu_file_is_writable(f
));
740 if (index
>= f
->buf_size
) {
742 index
= f
->buf_index
+ offset
;
743 if (index
>= f
->buf_size
) {
747 return f
->buf
[index
];
750 int qemu_get_byte(QEMUFile
*f
)
754 result
= qemu_peek_byte(f
, 0);
755 qemu_file_skip(f
, 1);
759 int64_t qemu_ftell(QEMUFile
*f
)
765 int qemu_file_rate_limit(QEMUFile
*f
)
767 if (qemu_file_get_error(f
)) {
770 if (f
->xfer_limit
> 0 && f
->bytes_xfer
> f
->xfer_limit
) {
776 int64_t qemu_file_get_rate_limit(QEMUFile
*f
)
778 return f
->xfer_limit
;
781 void qemu_file_set_rate_limit(QEMUFile
*f
, int64_t limit
)
783 f
->xfer_limit
= limit
;
786 void qemu_file_reset_rate_limit(QEMUFile
*f
)
791 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
793 qemu_put_byte(f
, v
>> 8);
797 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
799 qemu_put_byte(f
, v
>> 24);
800 qemu_put_byte(f
, v
>> 16);
801 qemu_put_byte(f
, v
>> 8);
805 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
807 qemu_put_be32(f
, v
>> 32);
811 unsigned int qemu_get_be16(QEMUFile
*f
)
814 v
= qemu_get_byte(f
) << 8;
815 v
|= qemu_get_byte(f
);
819 unsigned int qemu_get_be32(QEMUFile
*f
)
822 v
= qemu_get_byte(f
) << 24;
823 v
|= qemu_get_byte(f
) << 16;
824 v
|= qemu_get_byte(f
) << 8;
825 v
|= qemu_get_byte(f
);
829 uint64_t qemu_get_be64(QEMUFile
*f
)
832 v
= (uint64_t)qemu_get_be32(f
) << 32;
833 v
|= qemu_get_be32(f
);