xref: /openbmc/qemu/include/qemu/iov.h (revision c964b660)
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_full(const struct iovec *iov, unsigned int iov_cnt,
43                          size_t offset, const void *buf, size_t bytes);
44 size_t iov_to_buf_full(const struct iovec *iov, const unsigned int iov_cnt,
45 		       size_t offset, void *buf, size_t bytes);
46 
47 static inline size_t
48 iov_from_buf(const struct iovec *iov, unsigned int iov_cnt,
49              size_t offset, const void *buf, size_t bytes)
50 {
51     if (__builtin_constant_p(bytes) && iov_cnt &&
52         offset <= iov[0].iov_len && bytes <= iov[0].iov_len - offset) {
53         memcpy(iov[0].iov_base + offset, buf, bytes);
54         return bytes;
55     } else {
56         return iov_from_buf_full(iov, iov_cnt, offset, buf, bytes);
57     }
58 }
59 
60 static inline size_t
61 iov_to_buf(const struct iovec *iov, const unsigned int iov_cnt,
62            size_t offset, void *buf, size_t bytes)
63 {
64     if (__builtin_constant_p(bytes) && iov_cnt &&
65         offset <= iov[0].iov_len && bytes <= iov[0].iov_len - offset) {
66         memcpy(buf, iov[0].iov_base + offset, bytes);
67         return bytes;
68     } else {
69         return iov_to_buf_full(iov, iov_cnt, offset, buf, bytes);
70     }
71 }
72 
73 /**
74  * Set data bytes pointed out by iovec `iov' of size `iov_cnt' elements,
75  * starting at byte offset `start', to value `fillc', repeating it
76  * `bytes' number of times.  `Offset' must point to the inside of iovec.
77  * If `bytes' is large enough, only last bytes portion of iovec,
78  * up to the end of it, will be filled with the specified value.
79  * Function return actual number of bytes processed, which is
80  * min(size, iov_size(iov) - offset).
81  * Again, it is okay to use large value for `bytes' to mean "up to the end".
82  */
83 size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt,
84                   size_t offset, int fillc, size_t bytes);
85 
86 /*
87  * Send/recv data from/to iovec buffers directly
88  *
89  * `offset' bytes in the beginning of iovec buffer are skipped and
90  * next `bytes' bytes are used, which must be within data of iovec.
91  *
92  *   r = iov_send_recv(sockfd, iov, iovcnt, offset, bytes, true);
93  *
94  * is logically equivalent to
95  *
96  *   char *buf = malloc(bytes);
97  *   iov_to_buf(iov, iovcnt, offset, buf, bytes);
98  *   r = send(sockfd, buf, bytes, 0);
99  *   free(buf);
100  *
101  * For iov_send_recv() _whole_ area being sent or received
102  * should be within the iovec, not only beginning of it.
103  */
104 ssize_t iov_send_recv(int sockfd, const struct iovec *iov, unsigned iov_cnt,
105                       size_t offset, size_t bytes, bool do_send);
106 #define iov_recv(sockfd, iov, iov_cnt, offset, bytes) \
107   iov_send_recv(sockfd, iov, iov_cnt, offset, bytes, false)
108 #define iov_send(sockfd, iov, iov_cnt, offset, bytes) \
109   iov_send_recv(sockfd, iov, iov_cnt, offset, bytes, true)
110 
111 /**
112  * Produce a text hexdump of iovec `iov' with `iov_cnt' number of elements
113  * in file `fp', prefixing each line with `prefix' and processing not more
114  * than `limit' data bytes.
115  */
116 void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
117                  FILE *fp, const char *prefix, size_t limit);
118 
119 /*
120  * Partial copy of vector from iov to dst_iov (data is not copied).
121  * dst_iov overlaps iov at a specified offset.
122  * size of dst_iov is at most bytes. dst vector count is returned.
123  */
124 unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt,
125                  const struct iovec *iov, unsigned int iov_cnt,
126                  size_t offset, size_t bytes);
127 
128 /*
129  * Remove a given number of bytes from the front or back of a vector.
130  * This may update iov and/or iov_cnt to exclude iovec elements that are
131  * no longer required.
132  *
133  * The number of bytes actually discarded is returned.  This number may be
134  * smaller than requested if the vector is too small.
135  */
136 size_t iov_discard_front(struct iovec **iov, unsigned int *iov_cnt,
137                          size_t bytes);
138 size_t iov_discard_back(struct iovec *iov, unsigned int *iov_cnt,
139                         size_t bytes);
140 
141 #endif
142