xref: /openbmc/qemu/include/qemu/iov.h (revision a719a27c)
1 /*
2  * Helpers for using (partial) iovecs.
3  *
4  * Copyright (C) 2010 Red Hat, Inc.
5  *
6  * Author(s):
7  *  Amit Shah <amit.shah@redhat.com>
8  *  Michael Tokarev <mjt@tls.msk.ru>
9  *
10  * This work is licensed under the terms of the GNU GPL, version 2.  See
11  * the COPYING file in the top-level directory.
12  */
13 
14 #ifndef IOV_H
15 #define IOV_H
16 
17 #include "qemu-common.h"
18 
19 /**
20  * count and return data size, in bytes, of an iovec
21  * starting at `iov' of `iov_cnt' number of elements.
22  */
23 size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt);
24 
25 /**
26  * Copy from single continuous buffer to scatter-gather vector of buffers
27  * (iovec) and back like memcpy() between two continuous memory regions.
28  * Data in single continuous buffer starting at address `buf' and
29  * `bytes' bytes long will be copied to/from an iovec `iov' with
30  * `iov_cnt' number of elements, starting at byte position `offset'
31  * within the iovec.  If the iovec does not contain enough space,
32  * only part of data will be copied, up to the end of the iovec.
33  * Number of bytes actually copied will be returned, which is
34  *  min(bytes, iov_size(iov)-offset)
35  * `Offset' must point to the inside of iovec.
36  * It is okay to use very large value for `bytes' since we're
37  * limited by the size of the iovec anyway, provided that the
38  * buffer pointed to by buf has enough space.  One possible
39  * such "large" value is -1 (sinice size_t is unsigned),
40  * so specifying `-1' as `bytes' means 'up to the end of iovec'.
41  */
42 size_t iov_from_buf(const struct iovec *iov, unsigned int iov_cnt,
43                     size_t offset, const void *buf, size_t bytes);
44 size_t iov_to_buf(const struct iovec *iov, const unsigned int iov_cnt,
45                   size_t offset, void *buf, size_t bytes);
46 
47 /**
48  * Set data bytes pointed out by iovec `iov' of size `iov_cnt' elements,
49  * starting at byte offset `start', to value `fillc', repeating it
50  * `bytes' number of times.  `Offset' must point to the inside of iovec.
51  * If `bytes' is large enough, only last bytes portion of iovec,
52  * up to the end of it, will be filled with the specified value.
53  * Function return actual number of bytes processed, which is
54  * min(size, iov_size(iov) - offset).
55  * Again, it is okay to use large value for `bytes' to mean "up to the end".
56  */
57 size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt,
58                   size_t offset, int fillc, size_t bytes);
59 
60 /*
61  * Send/recv data from/to iovec buffers directly
62  *
63  * `offset' bytes in the beginning of iovec buffer are skipped and
64  * next `bytes' bytes are used, which must be within data of iovec.
65  *
66  *   r = iov_send_recv(sockfd, iov, iovcnt, offset, bytes, true);
67  *
68  * is logically equivalent to
69  *
70  *   char *buf = malloc(bytes);
71  *   iov_to_buf(iov, iovcnt, offset, buf, bytes);
72  *   r = send(sockfd, buf, bytes, 0);
73  *   free(buf);
74  *
75  * For iov_send_recv() _whole_ area being sent or received
76  * should be within the iovec, not only beginning of it.
77  */
78 ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt,
79                       size_t offset, size_t bytes, bool do_send);
80 #define iov_recv(sockfd, iov, iov_cnt, offset, bytes) \
81   iov_send_recv(sockfd, iov, iov_cnt, offset, bytes, false)
82 #define iov_send(sockfd, iov, iov_cnt, offset, bytes) \
83   iov_send_recv(sockfd, iov, iov_cnt, offset, bytes, true)
84 
85 /**
86  * Produce a text hexdump of iovec `iov' with `iov_cnt' number of elements
87  * in file `fp', prefixing each line with `prefix' and processing not more
88  * than `limit' data bytes.
89  */
90 void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
91                  FILE *fp, const char *prefix, size_t limit);
92 
93 /*
94  * Partial copy of vector from iov to dst_iov (data is not copied).
95  * dst_iov overlaps iov at a specified offset.
96  * size of dst_iov is at most bytes. dst vector count is returned.
97  */
98 unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt,
99                  const struct iovec *iov, unsigned int iov_cnt,
100                  size_t offset, size_t bytes);
101 
102 /*
103  * Remove a given number of bytes from the front or back of a vector.
104  * This may update iov and/or iov_cnt to exclude iovec elements that are
105  * no longer required.
106  *
107  * The number of bytes actually discarded is returned.  This number may be
108  * smaller than requested if the vector is too small.
109  */
110 size_t iov_discard_front(struct iovec **iov, unsigned int *iov_cnt,
111                          size_t bytes);
112 size_t iov_discard_back(struct iovec *iov, unsigned int *iov_cnt,
113                         size_t bytes);
114 
115 #endif
116