1
2
3
4
5
6
7
8
9
10
11
12
13
14#ifndef IOV_H
15#define IOV_H
16
17
18
19
20
21size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt);
22
23
24
25
26
27
28
29
30
31
32
33
34
35size_t iov_from_buf_full(const struct iovec *iov, unsigned int iov_cnt,
36 size_t offset, const void *buf, size_t bytes);
37size_t iov_to_buf_full(const struct iovec *iov, const unsigned int iov_cnt,
38 size_t offset, void *buf, size_t bytes);
39
40static inline size_t
41iov_from_buf(const struct iovec *iov, unsigned int iov_cnt,
42 size_t offset, const void *buf, size_t bytes)
43{
44 if (__builtin_constant_p(bytes) && iov_cnt &&
45 offset <= iov[0].iov_len && bytes <= iov[0].iov_len - offset) {
46 memcpy(iov[0].iov_base + offset, buf, bytes);
47 return bytes;
48 } else {
49 return iov_from_buf_full(iov, iov_cnt, offset, buf, bytes);
50 }
51}
52
53static inline size_t
54iov_to_buf(const struct iovec *iov, const unsigned int iov_cnt,
55 size_t offset, void *buf, size_t bytes)
56{
57 if (__builtin_constant_p(bytes) && iov_cnt &&
58 offset <= iov[0].iov_len && bytes <= iov[0].iov_len - offset) {
59 memcpy(buf, iov[0].iov_base + offset, bytes);
60 return bytes;
61 } else {
62 return iov_to_buf_full(iov, iov_cnt, offset, buf, bytes);
63 }
64}
65
66
67
68
69
70
71
72
73
74
75size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt,
76 size_t offset, int fillc, size_t bytes);
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96ssize_t iov_send_recv(int sockfd, const struct iovec *iov, unsigned iov_cnt,
97 size_t offset, size_t bytes, bool do_send);
98#define iov_recv(sockfd, iov, iov_cnt, offset, bytes) \
99 iov_send_recv(sockfd, iov, iov_cnt, offset, bytes, false)
100#define iov_send(sockfd, iov, iov_cnt, offset, bytes) \
101 iov_send_recv(sockfd, iov, iov_cnt, offset, bytes, true)
102
103
104
105
106
107
108void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
109 FILE *fp, const char *prefix, size_t limit);
110
111
112
113
114
115
116unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt,
117 const struct iovec *iov, unsigned int iov_cnt,
118 size_t offset, size_t bytes);
119
120
121
122
123
124
125
126
127
128size_t iov_discard_front(struct iovec **iov, unsigned int *iov_cnt,
129 size_t bytes);
130size_t iov_discard_back(struct iovec *iov, unsigned int *iov_cnt,
131 size_t bytes);
132
133
134typedef struct {
135 struct iovec *modified_iov;
136 struct iovec orig;
137} IOVDiscardUndo;
138
139
140
141
142
143
144
145void iov_discard_undo(IOVDiscardUndo *undo);
146
147
148
149
150
151size_t iov_discard_front_undoable(struct iovec **iov, unsigned int *iov_cnt,
152 size_t bytes, IOVDiscardUndo *undo);
153size_t iov_discard_back_undoable(struct iovec *iov, unsigned int *iov_cnt,
154 size_t bytes, IOVDiscardUndo *undo);
155
156typedef struct QEMUIOVector {
157 struct iovec *iov;
158 int niov;
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174 union {
175 struct {
176 int nalloc;
177 struct iovec local_iov;
178 };
179 struct {
180 char __pad[sizeof(int) + offsetof(struct iovec, iov_len)];
181 size_t size;
182 };
183 };
184} QEMUIOVector;
185
186QEMU_BUILD_BUG_ON(offsetof(QEMUIOVector, size) !=
187 offsetof(QEMUIOVector, local_iov.iov_len));
188
189#define QEMU_IOVEC_INIT_BUF(self, buf, len) \
190{ \
191 .iov = &(self).local_iov, \
192 .niov = 1, \
193 .nalloc = -1, \
194 .local_iov = { \
195 .iov_base = (void *)(buf), \
196 .iov_len = (len), \
197 }, \
198}
199
200
201
202
203
204
205
206
207
208
209static inline void qemu_iovec_init_buf(QEMUIOVector *qiov,
210 const void *buf, size_t len)
211{
212 *qiov = (QEMUIOVector) QEMU_IOVEC_INIT_BUF(*qiov, buf, len);
213}
214
215static inline void *qemu_iovec_buf(QEMUIOVector *qiov)
216{
217
218 assert(qiov->nalloc == -1 && qiov->iov == &qiov->local_iov);
219
220 return qiov->local_iov.iov_base;
221}
222
223void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint);
224void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov);
225int qemu_iovec_init_extended(
226 QEMUIOVector *qiov,
227 void *head_buf, size_t head_len,
228 QEMUIOVector *mid_qiov, size_t mid_offset, size_t mid_len,
229 void *tail_buf, size_t tail_len);
230void qemu_iovec_init_slice(QEMUIOVector *qiov, QEMUIOVector *source,
231 size_t offset, size_t len);
232int qemu_iovec_subvec_niov(QEMUIOVector *qiov, size_t offset, size_t len);
233void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len);
234void qemu_iovec_concat(QEMUIOVector *dst,
235 QEMUIOVector *src, size_t soffset, size_t sbytes);
236size_t qemu_iovec_concat_iov(QEMUIOVector *dst,
237 struct iovec *src_iov, unsigned int src_cnt,
238 size_t soffset, size_t sbytes);
239bool qemu_iovec_is_zero(QEMUIOVector *qiov, size_t qiov_offeset, size_t bytes);
240void qemu_iovec_destroy(QEMUIOVector *qiov);
241void qemu_iovec_reset(QEMUIOVector *qiov);
242size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
243 void *buf, size_t bytes);
244size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
245 const void *buf, size_t bytes);
246size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
247 int fillc, size_t bytes);
248ssize_t qemu_iovec_compare(QEMUIOVector *a, QEMUIOVector *b);
249void qemu_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src, void *buf);
250void qemu_iovec_discard_back(QEMUIOVector *qiov, size_t bytes);
251
252#endif
253