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;
103 return fwrite(buf, 1, size, s->stdio_file);
106 static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
108 QEMUFileStdio *s = opaque;
109 FILE *fp = s->stdio_file;
114 bytes = fread(buf, 1, size, fp);
115 if (bytes != 0 || !ferror(fp)) {
118 if (errno == EAGAIN) {
119 yield_until_fd_readable(fileno(fp));
120 } else if (errno != EINTR) {
127 static int stdio_pclose(void *opaque)
129 QEMUFileStdio *s = opaque;
131 ret = pclose(s->stdio_file);
134 } else if (!WIFEXITED(ret) || WEXITSTATUS(ret) != 0) {
135 /* close succeeded, but non-zero exit code: */
136 ret = -EIO; /* fake errno value */
142 static int stdio_fclose(void *opaque)
144 QEMUFileStdio *s = opaque;
147 if (s->file->ops->put_buffer || s->file->ops->writev_buffer) {
148 int fd = fileno(s->stdio_file);
151 ret = fstat(fd, &st);
152 if (ret == 0 && S_ISREG(st.st_mode)) {
154 * If the file handle is a regular file make sure the
155 * data is flushed to disk before signaling success.
164 if (fclose(s->stdio_file) == EOF) {
171 static const QEMUFileOps stdio_pipe_read_ops = {
172 .get_fd = stdio_get_fd,
173 .get_buffer = stdio_get_buffer,
174 .close = stdio_pclose
177 static const QEMUFileOps stdio_pipe_write_ops = {
178 .get_fd = stdio_get_fd,
179 .put_buffer = stdio_put_buffer,
180 .close = stdio_pclose
183 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
188 if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
189 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
193 stdio_file = popen(command, mode);
194 if (stdio_file == NULL) {
198 s = g_malloc0(sizeof(QEMUFileStdio));
200 s->stdio_file = stdio_file;
202 if (mode[0] == 'r') {
203 s->file = qemu_fopen_ops(s, &stdio_pipe_read_ops);
205 s->file = qemu_fopen_ops(s, &stdio_pipe_write_ops);
210 static const QEMUFileOps stdio_file_read_ops = {
211 .get_fd = stdio_get_fd,
212 .get_buffer = stdio_get_buffer,
213 .close = stdio_fclose
216 static const QEMUFileOps stdio_file_write_ops = {
217 .get_fd = stdio_get_fd,
218 .put_buffer = stdio_put_buffer,
219 .close = stdio_fclose
222 static ssize_t unix_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
225 QEMUFileSocket *s = opaque;
227 ssize_t size = iov_size(iov, iovcnt);
233 /* Find the next start position; skip all full-sized vector elements */
234 while (offset >= iov[0].iov_len) {
235 offset -= iov[0].iov_len;
239 /* skip `offset' bytes from the (now) first element, undo it on exit */
241 iov[0].iov_base += offset;
242 iov[0].iov_len -= offset;
245 len = writev(s->fd, iov, iovcnt);
246 } while (len == -1 && errno == EINTR);
251 /* Undo the changes above */
252 iov[0].iov_base -= offset;
253 iov[0].iov_len += offset;
255 /* Prepare for the next iteration */
264 static int unix_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
266 QEMUFileSocket *s = opaque;
270 len = read(s->fd, buf, size);
274 if (errno == EAGAIN) {
275 yield_until_fd_readable(s->fd);
276 } else if (errno != EINTR) {
287 static int unix_close(void *opaque)
289 QEMUFileSocket *s = opaque;
295 static const QEMUFileOps unix_read_ops = {
296 .get_fd = socket_get_fd,
297 .get_buffer = unix_get_buffer,
301 static const QEMUFileOps unix_write_ops = {
302 .get_fd = socket_get_fd,
303 .writev_buffer = unix_writev_buffer,
307 QEMUFile *qemu_fdopen(int fd, const char *mode)
312 (mode[0] != 'r' && mode[0] != 'w') ||
313 mode[1] != 'b' || mode[2] != 0) {
314 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
318 s = g_malloc0(sizeof(QEMUFileSocket));
321 if (mode[0] == 'r') {
322 s->file = qemu_fopen_ops(s, &unix_read_ops);
324 s->file = qemu_fopen_ops(s, &unix_write_ops);
329 static const QEMUFileOps socket_read_ops = {
330 .get_fd = socket_get_fd,
331 .get_buffer = socket_get_buffer,
332 .close = socket_close
335 static const QEMUFileOps socket_write_ops = {
336 .get_fd = socket_get_fd,
337 .writev_buffer = socket_writev_buffer,
338 .close = socket_close
341 bool qemu_file_mode_is_not_valid(const char *mode)
344 (mode[0] != 'r' && mode[0] != 'w') ||
345 mode[1] != 'b' || mode[2] != 0) {
346 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
353 QEMUFile *qemu_fopen_socket(int fd, const char *mode)
357 if (qemu_file_mode_is_not_valid(mode)) {
361 s = g_malloc0(sizeof(QEMUFileSocket));
363 if (mode[0] == 'w') {
364 qemu_set_block(s->fd);
365 s->file = qemu_fopen_ops(s, &socket_write_ops);
367 s->file = qemu_fopen_ops(s, &socket_read_ops);
372 QEMUFile *qemu_fopen(const char *filename, const char *mode)
376 if (qemu_file_mode_is_not_valid(mode)) {
380 s = g_malloc0(sizeof(QEMUFileStdio));
382 s->stdio_file = fopen(filename, mode);
383 if (!s->stdio_file) {
387 if (mode[0] == 'w') {
388 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
390 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
398 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
402 f = g_malloc0(sizeof(QEMUFile));
410 * Get last error for stream f
412 * Return negative error value if there has been an error on previous
413 * operations, return 0 if no error happened.
416 int qemu_file_get_error(QEMUFile *f)
418 return f->last_error;
421 void qemu_file_set_error(QEMUFile *f, int ret)
423 if (f->last_error == 0) {
428 static inline bool qemu_file_is_writable(QEMUFile *f)
430 return f->ops->writev_buffer || f->ops->put_buffer;
434 * Flushes QEMUFile buffer
436 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
439 void qemu_fflush(QEMUFile *f)
443 if (!qemu_file_is_writable(f)) {
447 if (f->ops->writev_buffer) {
449 ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
452 if (f->buf_index > 0) {
453 ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
462 qemu_file_set_error(f, ret);
466 void ram_control_before_iterate(QEMUFile *f, uint64_t flags)
470 if (f->ops->before_ram_iterate) {
471 ret = f->ops->before_ram_iterate(f, f->opaque, flags);
473 qemu_file_set_error(f, ret);
478 void ram_control_after_iterate(QEMUFile *f, uint64_t flags)
482 if (f->ops->after_ram_iterate) {
483 ret = f->ops->after_ram_iterate(f, f->opaque, flags);
485 qemu_file_set_error(f, ret);
490 void ram_control_load_hook(QEMUFile *f, uint64_t flags)
494 if (f->ops->hook_ram_load) {
495 ret = f->ops->hook_ram_load(f, f->opaque, flags);
497 qemu_file_set_error(f, ret);
500 qemu_file_set_error(f, ret);
504 size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
505 ram_addr_t offset, size_t size, int *bytes_sent)
507 if (f->ops->save_page) {
508 int ret = f->ops->save_page(f, f->opaque, block_offset,
509 offset, size, bytes_sent);
511 if (ret != RAM_SAVE_CONTROL_DELAYED) {
512 if (bytes_sent && *bytes_sent > 0) {
513 qemu_update_position(f, *bytes_sent);
514 } else if (ret < 0) {
515 qemu_file_set_error(f, ret);
522 return RAM_SAVE_CONTROL_NOT_SUPP;
525 static void qemu_fill_buffer(QEMUFile *f)
530 assert(!qemu_file_is_writable(f));
532 pending = f->buf_size - f->buf_index;
534 memmove(f->buf, f->buf + f->buf_index, pending);
537 f->buf_size = pending;
539 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
540 IO_BUF_SIZE - pending);
544 } else if (len == 0) {
545 qemu_file_set_error(f, -EIO);
546 } else if (len != -EAGAIN) {
547 qemu_file_set_error(f, len);
551 int qemu_get_fd(QEMUFile *f)
553 if (f->ops->get_fd) {
554 return f->ops->get_fd(f->opaque);
559 void qemu_update_position(QEMUFile *f, size_t size)
566 * Returns negative error value if any error happened on previous operations or
567 * while closing the file. Returns 0 or positive number on success.
569 * The meaning of return value on success depends on the specific backend
572 int qemu_fclose(QEMUFile *f)
576 ret = qemu_file_get_error(f);
579 int ret2 = f->ops->close(f->opaque);
584 /* If any error was spotted before closing, we should report it
585 * instead of the close() return value.
594 static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size)
596 /* check for adjacent buffer and coalesce them */
597 if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
598 f->iov[f->iovcnt - 1].iov_len) {
599 f->iov[f->iovcnt - 1].iov_len += size;
601 f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
602 f->iov[f->iovcnt++].iov_len = size;
605 if (f->iovcnt >= MAX_IOV_SIZE) {
610 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size)
612 if (!f->ops->writev_buffer) {
613 qemu_put_buffer(f, buf, size);
621 f->bytes_xfer += size;
622 add_to_iovec(f, buf, size);
625 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
634 l = IO_BUF_SIZE - f->buf_index;
638 memcpy(f->buf + f->buf_index, buf, l);
640 if (f->ops->writev_buffer) {
641 add_to_iovec(f, f->buf + f->buf_index, l);
644 if (f->buf_index == IO_BUF_SIZE) {
647 if (qemu_file_get_error(f)) {
655 void qemu_put_byte(QEMUFile *f, int v)
661 f->buf[f->buf_index] = v;
663 if (f->ops->writev_buffer) {
664 add_to_iovec(f, f->buf + f->buf_index, 1);
667 if (f->buf_index == IO_BUF_SIZE) {
672 void qemu_file_skip(QEMUFile *f, int size)
674 if (f->buf_index + size <= f->buf_size) {
675 f->buf_index += size;
679 int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
684 assert(!qemu_file_is_writable(f));
686 index = f->buf_index + offset;
687 pending = f->buf_size - index;
688 if (pending < size) {
690 index = f->buf_index + offset;
691 pending = f->buf_size - index;
697 if (size > pending) {
701 memcpy(buf, f->buf + index, size);
705 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
710 while (pending > 0) {
713 res = qemu_peek_buffer(f, buf, pending, 0);
717 qemu_file_skip(f, res);
725 int qemu_peek_byte(QEMUFile *f, int offset)
727 int index = f->buf_index + offset;
729 assert(!qemu_file_is_writable(f));
731 if (index >= f->buf_size) {
733 index = f->buf_index + offset;
734 if (index >= f->buf_size) {
738 return f->buf[index];
741 int qemu_get_byte(QEMUFile *f)
745 result = qemu_peek_byte(f, 0);
746 qemu_file_skip(f, 1);
750 int64_t qemu_ftell(QEMUFile *f)
756 int qemu_file_rate_limit(QEMUFile *f)
758 if (qemu_file_get_error(f)) {
761 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
767 int64_t qemu_file_get_rate_limit(QEMUFile *f)
769 return f->xfer_limit;
772 void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
774 f->xfer_limit = limit;
777 void qemu_file_reset_rate_limit(QEMUFile *f)
782 void qemu_put_be16(QEMUFile *f, unsigned int v)
784 qemu_put_byte(f, v >> 8);
788 void qemu_put_be32(QEMUFile *f, unsigned int v)
790 qemu_put_byte(f, v >> 24);
791 qemu_put_byte(f, v >> 16);
792 qemu_put_byte(f, v >> 8);
796 void qemu_put_be64(QEMUFile *f, uint64_t v)
798 qemu_put_be32(f, v >> 32);
802 unsigned int qemu_get_be16(QEMUFile *f)
805 v = qemu_get_byte(f) << 8;
806 v |= qemu_get_byte(f);
810 unsigned int qemu_get_be32(QEMUFile *f)
813 v = qemu_get_byte(f) << 24;
814 v |= qemu_get_byte(f) << 16;
815 v |= qemu_get_byte(f) << 8;
816 v |= qemu_get_byte(f);
820 uint64_t qemu_get_be64(QEMUFile *f)
823 v = (uint64_t)qemu_get_be32(f) << 32;
824 v |= qemu_get_be32(f);