root / util / iov.c @ cb6247a7
History | View | Annotate | Download (11.1 kB)
1 |
/*
|
---|---|
2 |
* Helpers for getting linearized buffers from iov / filling buffers into iovs
|
3 |
*
|
4 |
* Copyright IBM, Corp. 2007, 2008
|
5 |
* Copyright (C) 2010 Red Hat, Inc.
|
6 |
*
|
7 |
* Author(s):
|
8 |
* Anthony Liguori <aliguori@us.ibm.com>
|
9 |
* Amit Shah <amit.shah@redhat.com>
|
10 |
* Michael Tokarev <mjt@tls.msk.ru>
|
11 |
*
|
12 |
* This work is licensed under the terms of the GNU GPL, version 2. See
|
13 |
* the COPYING file in the top-level directory.
|
14 |
*
|
15 |
* Contributions after 2012-01-13 are licensed under the terms of the
|
16 |
* GNU GPL, version 2 or (at your option) any later version.
|
17 |
*/
|
18 |
|
19 |
#include "qemu/iov.h" |
20 |
|
21 |
#ifdef _WIN32
|
22 |
# include <windows.h> |
23 |
# include <winsock2.h> |
24 |
#else
|
25 |
# include <sys/types.h> |
26 |
# include <sys/socket.h> |
27 |
#endif
|
28 |
|
29 |
size_t iov_from_buf(const struct iovec *iov, unsigned int iov_cnt, |
30 |
size_t offset, const void *buf, size_t bytes) |
31 |
{ |
32 |
size_t done; |
33 |
unsigned int i; |
34 |
for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) { |
35 |
if (offset < iov[i].iov_len) {
|
36 |
size_t len = MIN(iov[i].iov_len - offset, bytes - done); |
37 |
memcpy(iov[i].iov_base + offset, buf + done, len); |
38 |
done += len; |
39 |
offset = 0;
|
40 |
} else {
|
41 |
offset -= iov[i].iov_len; |
42 |
} |
43 |
} |
44 |
assert(offset == 0);
|
45 |
return done;
|
46 |
} |
47 |
|
48 |
size_t iov_to_buf(const struct iovec *iov, const unsigned int iov_cnt, |
49 |
size_t offset, void *buf, size_t bytes)
|
50 |
{ |
51 |
size_t done; |
52 |
unsigned int i; |
53 |
for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) { |
54 |
if (offset < iov[i].iov_len) {
|
55 |
size_t len = MIN(iov[i].iov_len - offset, bytes - done); |
56 |
memcpy(buf + done, iov[i].iov_base + offset, len); |
57 |
done += len; |
58 |
offset = 0;
|
59 |
} else {
|
60 |
offset -= iov[i].iov_len; |
61 |
} |
62 |
} |
63 |
assert(offset == 0);
|
64 |
return done;
|
65 |
} |
66 |
|
67 |
size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt, |
68 |
size_t offset, int fillc, size_t bytes)
|
69 |
{ |
70 |
size_t done; |
71 |
unsigned int i; |
72 |
for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) { |
73 |
if (offset < iov[i].iov_len) {
|
74 |
size_t len = MIN(iov[i].iov_len - offset, bytes - done); |
75 |
memset(iov[i].iov_base + offset, fillc, len); |
76 |
done += len; |
77 |
offset = 0;
|
78 |
} else {
|
79 |
offset -= iov[i].iov_len; |
80 |
} |
81 |
} |
82 |
assert(offset == 0);
|
83 |
return done;
|
84 |
} |
85 |
|
86 |
size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt) |
87 |
{ |
88 |
size_t len; |
89 |
unsigned int i; |
90 |
|
91 |
len = 0;
|
92 |
for (i = 0; i < iov_cnt; i++) { |
93 |
len += iov[i].iov_len; |
94 |
} |
95 |
return len;
|
96 |
} |
97 |
|
98 |
/* helper function for iov_send_recv() */
|
99 |
static ssize_t
|
100 |
do_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt, bool do_send) |
101 |
{ |
102 |
#if defined CONFIG_IOVEC && defined CONFIG_POSIX
|
103 |
ssize_t ret; |
104 |
struct msghdr msg;
|
105 |
memset(&msg, 0, sizeof(msg)); |
106 |
msg.msg_iov = iov; |
107 |
msg.msg_iovlen = iov_cnt; |
108 |
do {
|
109 |
ret = do_send |
110 |
? sendmsg(sockfd, &msg, 0)
|
111 |
: recvmsg(sockfd, &msg, 0);
|
112 |
} while (ret < 0 && errno == EINTR); |
113 |
return ret;
|
114 |
#else
|
115 |
/* else send piece-by-piece */
|
116 |
/*XXX Note: windows has WSASend() and WSARecv() */
|
117 |
unsigned i = 0; |
118 |
ssize_t ret = 0;
|
119 |
while (i < iov_cnt) {
|
120 |
ssize_t r = do_send |
121 |
? send(sockfd, iov[i].iov_base, iov[i].iov_len, 0)
|
122 |
: recv(sockfd, iov[i].iov_base, iov[i].iov_len, 0);
|
123 |
if (r > 0) { |
124 |
ret += r; |
125 |
} else if (!r) { |
126 |
break;
|
127 |
} else if (errno == EINTR) { |
128 |
continue;
|
129 |
} else {
|
130 |
/* else it is some "other" error,
|
131 |
* only return if there was no data processed. */
|
132 |
if (ret == 0) { |
133 |
ret = -1;
|
134 |
} |
135 |
break;
|
136 |
} |
137 |
i++; |
138 |
} |
139 |
return ret;
|
140 |
#endif
|
141 |
} |
142 |
|
143 |
ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt, |
144 |
size_t offset, size_t bytes, |
145 |
bool do_send)
|
146 |
{ |
147 |
ssize_t ret; |
148 |
unsigned si, ei; /* start and end indexes */ |
149 |
if (bytes == 0) { |
150 |
/* Catch the do-nothing case early, as otherwise we will pass an
|
151 |
* empty iovec to sendmsg/recvmsg(), and not all implementations
|
152 |
* accept this.
|
153 |
*/
|
154 |
return 0; |
155 |
} |
156 |
|
157 |
/* Find the start position, skipping `offset' bytes:
|
158 |
* first, skip all full-sized vector elements, */
|
159 |
for (si = 0; si < iov_cnt && offset >= iov[si].iov_len; ++si) { |
160 |
offset -= iov[si].iov_len; |
161 |
} |
162 |
|
163 |
/* si == iov_cnt would only be valid if bytes == 0, which
|
164 |
* we already ruled out above. */
|
165 |
assert(si < iov_cnt); |
166 |
iov += si; |
167 |
iov_cnt -= si; |
168 |
|
169 |
if (offset) {
|
170 |
/* second, skip `offset' bytes from the (now) first element,
|
171 |
* undo it on exit */
|
172 |
iov[0].iov_base += offset;
|
173 |
iov[0].iov_len -= offset;
|
174 |
} |
175 |
/* Find the end position skipping `bytes' bytes: */
|
176 |
/* first, skip all full-sized elements */
|
177 |
for (ei = 0; ei < iov_cnt && iov[ei].iov_len <= bytes; ++ei) { |
178 |
bytes -= iov[ei].iov_len; |
179 |
} |
180 |
if (bytes) {
|
181 |
/* second, fixup the last element, and remember
|
182 |
* the length we've cut from the end of it in `bytes' */
|
183 |
size_t tail; |
184 |
assert(ei < iov_cnt); |
185 |
assert(iov[ei].iov_len > bytes); |
186 |
tail = iov[ei].iov_len - bytes; |
187 |
iov[ei].iov_len = bytes; |
188 |
bytes = tail; /* bytes is now equal to the tail size */
|
189 |
++ei; |
190 |
} |
191 |
|
192 |
ret = do_send_recv(sockfd, iov, ei, do_send); |
193 |
|
194 |
/* Undo the changes above */
|
195 |
if (offset) {
|
196 |
iov[0].iov_base -= offset;
|
197 |
iov[0].iov_len += offset;
|
198 |
} |
199 |
if (bytes) {
|
200 |
iov[ei-1].iov_len += bytes;
|
201 |
} |
202 |
|
203 |
return ret;
|
204 |
} |
205 |
|
206 |
|
207 |
void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt, |
208 |
FILE *fp, const char *prefix, size_t limit) |
209 |
{ |
210 |
int v;
|
211 |
size_t size = 0;
|
212 |
char *buf;
|
213 |
|
214 |
for (v = 0; v < iov_cnt; v++) { |
215 |
size += iov[v].iov_len; |
216 |
} |
217 |
size = size > limit ? limit : size; |
218 |
buf = g_malloc(size); |
219 |
iov_to_buf(iov, iov_cnt, 0, buf, size);
|
220 |
hexdump(buf, fp, prefix, size); |
221 |
g_free(buf); |
222 |
} |
223 |
|
224 |
unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt, |
225 |
const struct iovec *iov, unsigned int iov_cnt, |
226 |
size_t offset, size_t bytes) |
227 |
{ |
228 |
size_t len; |
229 |
unsigned int i, j; |
230 |
for (i = 0, j = 0; i < iov_cnt && j < dst_iov_cnt && bytes; i++) { |
231 |
if (offset >= iov[i].iov_len) {
|
232 |
offset -= iov[i].iov_len; |
233 |
continue;
|
234 |
} |
235 |
len = MIN(bytes, iov[i].iov_len - offset); |
236 |
|
237 |
dst_iov[j].iov_base = iov[i].iov_base + offset; |
238 |
dst_iov[j].iov_len = len; |
239 |
j++; |
240 |
bytes -= len; |
241 |
offset = 0;
|
242 |
} |
243 |
assert(offset == 0);
|
244 |
return j;
|
245 |
} |
246 |
|
247 |
/* io vectors */
|
248 |
|
249 |
void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint) |
250 |
{ |
251 |
qiov->iov = g_malloc(alloc_hint * sizeof(struct iovec)); |
252 |
qiov->niov = 0;
|
253 |
qiov->nalloc = alloc_hint; |
254 |
qiov->size = 0;
|
255 |
} |
256 |
|
257 |
void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov) |
258 |
{ |
259 |
int i;
|
260 |
|
261 |
qiov->iov = iov; |
262 |
qiov->niov = niov; |
263 |
qiov->nalloc = -1;
|
264 |
qiov->size = 0;
|
265 |
for (i = 0; i < niov; i++) |
266 |
qiov->size += iov[i].iov_len; |
267 |
} |
268 |
|
269 |
void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len) |
270 |
{ |
271 |
assert(qiov->nalloc != -1);
|
272 |
|
273 |
if (qiov->niov == qiov->nalloc) {
|
274 |
qiov->nalloc = 2 * qiov->nalloc + 1; |
275 |
qiov->iov = g_realloc(qiov->iov, qiov->nalloc * sizeof(struct iovec)); |
276 |
} |
277 |
qiov->iov[qiov->niov].iov_base = base; |
278 |
qiov->iov[qiov->niov].iov_len = len; |
279 |
qiov->size += len; |
280 |
++qiov->niov; |
281 |
} |
282 |
|
283 |
/*
|
284 |
* Concatenates (partial) iovecs from src_iov to the end of dst.
|
285 |
* It starts copying after skipping `soffset' bytes at the
|
286 |
* beginning of src and adds individual vectors from src to
|
287 |
* dst copies up to `sbytes' bytes total, or up to the end
|
288 |
* of src_iov if it comes first. This way, it is okay to specify
|
289 |
* very large value for `sbytes' to indicate "up to the end
|
290 |
* of src".
|
291 |
* Only vector pointers are processed, not the actual data buffers.
|
292 |
*/
|
293 |
void qemu_iovec_concat_iov(QEMUIOVector *dst,
|
294 |
struct iovec *src_iov, unsigned int src_cnt, |
295 |
size_t soffset, size_t sbytes) |
296 |
{ |
297 |
int i;
|
298 |
size_t done; |
299 |
|
300 |
if (!sbytes) {
|
301 |
return;
|
302 |
} |
303 |
assert(dst->nalloc != -1);
|
304 |
for (i = 0, done = 0; done < sbytes && i < src_cnt; i++) { |
305 |
if (soffset < src_iov[i].iov_len) {
|
306 |
size_t len = MIN(src_iov[i].iov_len - soffset, sbytes - done); |
307 |
qemu_iovec_add(dst, src_iov[i].iov_base + soffset, len); |
308 |
done += len; |
309 |
soffset = 0;
|
310 |
} else {
|
311 |
soffset -= src_iov[i].iov_len; |
312 |
} |
313 |
} |
314 |
assert(soffset == 0); /* offset beyond end of src */ |
315 |
} |
316 |
|
317 |
/*
|
318 |
* Concatenates (partial) iovecs from src to the end of dst.
|
319 |
* It starts copying after skipping `soffset' bytes at the
|
320 |
* beginning of src and adds individual vectors from src to
|
321 |
* dst copies up to `sbytes' bytes total, or up to the end
|
322 |
* of src if it comes first. This way, it is okay to specify
|
323 |
* very large value for `sbytes' to indicate "up to the end
|
324 |
* of src".
|
325 |
* Only vector pointers are processed, not the actual data buffers.
|
326 |
*/
|
327 |
void qemu_iovec_concat(QEMUIOVector *dst,
|
328 |
QEMUIOVector *src, size_t soffset, size_t sbytes) |
329 |
{ |
330 |
qemu_iovec_concat_iov(dst, src->iov, src->niov, soffset, sbytes); |
331 |
} |
332 |
|
333 |
void qemu_iovec_destroy(QEMUIOVector *qiov)
|
334 |
{ |
335 |
assert(qiov->nalloc != -1);
|
336 |
|
337 |
qemu_iovec_reset(qiov); |
338 |
g_free(qiov->iov); |
339 |
qiov->nalloc = 0;
|
340 |
qiov->iov = NULL;
|
341 |
} |
342 |
|
343 |
void qemu_iovec_reset(QEMUIOVector *qiov)
|
344 |
{ |
345 |
assert(qiov->nalloc != -1);
|
346 |
|
347 |
qiov->niov = 0;
|
348 |
qiov->size = 0;
|
349 |
} |
350 |
|
351 |
size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset, |
352 |
void *buf, size_t bytes)
|
353 |
{ |
354 |
return iov_to_buf(qiov->iov, qiov->niov, offset, buf, bytes);
|
355 |
} |
356 |
|
357 |
size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset, |
358 |
const void *buf, size_t bytes) |
359 |
{ |
360 |
return iov_from_buf(qiov->iov, qiov->niov, offset, buf, bytes);
|
361 |
} |
362 |
|
363 |
size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset, |
364 |
int fillc, size_t bytes)
|
365 |
{ |
366 |
return iov_memset(qiov->iov, qiov->niov, offset, fillc, bytes);
|
367 |
} |
368 |
|
369 |
size_t iov_discard_front(struct iovec **iov, unsigned int *iov_cnt, |
370 |
size_t bytes) |
371 |
{ |
372 |
size_t total = 0;
|
373 |
struct iovec *cur;
|
374 |
|
375 |
for (cur = *iov; *iov_cnt > 0; cur++) { |
376 |
if (cur->iov_len > bytes) {
|
377 |
cur->iov_base += bytes; |
378 |
cur->iov_len -= bytes; |
379 |
total += bytes; |
380 |
break;
|
381 |
} |
382 |
|
383 |
bytes -= cur->iov_len; |
384 |
total += cur->iov_len; |
385 |
*iov_cnt -= 1;
|
386 |
} |
387 |
|
388 |
*iov = cur; |
389 |
return total;
|
390 |
} |
391 |
|
392 |
size_t iov_discard_back(struct iovec *iov, unsigned int *iov_cnt, |
393 |
size_t bytes) |
394 |
{ |
395 |
size_t total = 0;
|
396 |
struct iovec *cur;
|
397 |
|
398 |
if (*iov_cnt == 0) { |
399 |
return 0; |
400 |
} |
401 |
|
402 |
cur = iov + (*iov_cnt - 1);
|
403 |
|
404 |
while (*iov_cnt > 0) { |
405 |
if (cur->iov_len > bytes) {
|
406 |
cur->iov_len -= bytes; |
407 |
total += bytes; |
408 |
break;
|
409 |
} |
410 |
|
411 |
bytes -= cur->iov_len; |
412 |
total += cur->iov_len; |
413 |
cur--; |
414 |
*iov_cnt -= 1;
|
415 |
} |
416 |
|
417 |
return total;
|
418 |
} |