]> Git Repo - qemu.git/blame - util/iov.c
Merge remote-tracking branch 'remotes/pmaydell/tags/pull-target-arm-20160203' into...
[qemu.git] / util / iov.c
CommitLineData
e4d5639d
AS
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 <[email protected]>
9 * Amit Shah <[email protected]>
2278a69e 10 * Michael Tokarev <[email protected]>
e4d5639d
AS
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.
6b620ca3
PB
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.
e4d5639d
AS
17 */
18
1de7afc9 19#include "qemu/iov.h"
cc99c6f5 20#include "qemu/sockets.h"
25e5e4c7 21
844b5cea 22size_t iov_from_buf(const struct iovec *iov, unsigned int iov_cnt,
2278a69e 23 size_t offset, const void *buf, size_t bytes)
e4d5639d 24{
2278a69e 25 size_t done;
e4d5639d 26 unsigned int i;
2278a69e
MT
27 for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) {
28 if (offset < iov[i].iov_len) {
29 size_t len = MIN(iov[i].iov_len - offset, bytes - done);
30 memcpy(iov[i].iov_base + offset, buf + done, len);
31 done += len;
32 offset = 0;
33 } else {
34 offset -= iov[i].iov_len;
348e7b8d 35 }
e4d5639d 36 }
2278a69e
MT
37 assert(offset == 0);
38 return done;
e4d5639d 39}
fa6111f2 40
2278a69e
MT
41size_t iov_to_buf(const struct iovec *iov, const unsigned int iov_cnt,
42 size_t offset, void *buf, size_t bytes)
fa6111f2 43{
2278a69e 44 size_t done;
fa6111f2 45 unsigned int i;
2278a69e
MT
46 for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) {
47 if (offset < iov[i].iov_len) {
48 size_t len = MIN(iov[i].iov_len - offset, bytes - done);
49 memcpy(buf + done, iov[i].iov_base + offset, len);
50 done += len;
51 offset = 0;
52 } else {
53 offset -= iov[i].iov_len;
fa6111f2 54 }
8d15028e 55 }
2278a69e
MT
56 assert(offset == 0);
57 return done;
8d15028e
GH
58}
59
dcf6f5e1 60size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt,
2278a69e 61 size_t offset, int fillc, size_t bytes)
8d15028e 62{
2278a69e 63 size_t done;
8d15028e 64 unsigned int i;
2278a69e
MT
65 for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) {
66 if (offset < iov[i].iov_len) {
67 size_t len = MIN(iov[i].iov_len - offset, bytes - done);
68 memset(iov[i].iov_base + offset, fillc, len);
69 done += len;
70 offset = 0;
71 } else {
72 offset -= iov[i].iov_len;
8d15028e 73 }
fa6111f2 74 }
2278a69e
MT
75 assert(offset == 0);
76 return done;
fa6111f2
AS
77}
78
348e7b8d 79size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt)
fa6111f2
AS
80{
81 size_t len;
82 unsigned int i;
83
84 len = 0;
348e7b8d 85 for (i = 0; i < iov_cnt; i++) {
fa6111f2
AS
86 len += iov[i].iov_len;
87 }
88 return len;
89}
3a1dca94 90
25e5e4c7
MT
91/* helper function for iov_send_recv() */
92static ssize_t
93do_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt, bool do_send)
94{
9adea5f7 95#ifdef CONFIG_POSIX
25e5e4c7
MT
96 ssize_t ret;
97 struct msghdr msg;
98 memset(&msg, 0, sizeof(msg));
99 msg.msg_iov = iov;
100 msg.msg_iovlen = iov_cnt;
101 do {
102 ret = do_send
103 ? sendmsg(sockfd, &msg, 0)
104 : recvmsg(sockfd, &msg, 0);
105 } while (ret < 0 && errno == EINTR);
106 return ret;
107#else
108 /* else send piece-by-piece */
109 /*XXX Note: windows has WSASend() and WSARecv() */
c0958559
SW
110 unsigned i = 0;
111 ssize_t ret = 0;
112 while (i < iov_cnt) {
25e5e4c7
MT
113 ssize_t r = do_send
114 ? send(sockfd, iov[i].iov_base, iov[i].iov_len, 0)
115 : recv(sockfd, iov[i].iov_base, iov[i].iov_len, 0);
116 if (r > 0) {
117 ret += r;
118 } else if (!r) {
119 break;
120 } else if (errno == EINTR) {
121 continue;
122 } else {
123 /* else it is some "other" error,
124 * only return if there was no data processed. */
125 if (ret == 0) {
c0958559 126 ret = -1;
25e5e4c7
MT
127 }
128 break;
129 }
c0958559 130 i++;
25e5e4c7 131 }
c0958559 132 return ret;
25e5e4c7
MT
133#endif
134}
135
6b64640d 136ssize_t iov_send_recv(int sockfd, const struct iovec *_iov, unsigned iov_cnt,
25e5e4c7
MT
137 size_t offset, size_t bytes,
138 bool do_send)
139{
83f75c26 140 ssize_t total = 0;
25e5e4c7 141 ssize_t ret;
5209d675 142 size_t orig_len, tail;
f48869ad 143 unsigned niov;
6b64640d
WC
144 struct iovec *local_iov, *iov;
145
146 if (bytes <= 0) {
147 return 0;
148 }
149
150 local_iov = g_new0(struct iovec, iov_cnt);
151 iov_copy(local_iov, iov_cnt, _iov, iov_cnt, offset, bytes);
152 offset = 0;
153 iov = local_iov;
5209d675 154
83f75c26
PB
155 while (bytes > 0) {
156 /* Find the start position, skipping `offset' bytes:
157 * first, skip all full-sized vector elements, */
158 for (niov = 0; niov < iov_cnt && offset >= iov[niov].iov_len; ++niov) {
159 offset -= iov[niov].iov_len;
160 }
cb6247a7 161
83f75c26
PB
162 /* niov == iov_cnt would only be valid if bytes == 0, which
163 * we already ruled out in the loop condition. */
f48869ad 164 assert(niov < iov_cnt);
83f75c26
PB
165 iov += niov;
166 iov_cnt -= niov;
167
168 if (offset) {
169 /* second, skip `offset' bytes from the (now) first element,
170 * undo it on exit */
171 iov[0].iov_base += offset;
172 iov[0].iov_len -= offset;
173 }
174 /* Find the end position skipping `bytes' bytes: */
175 /* first, skip all full-sized elements */
176 tail = bytes;
177 for (niov = 0; niov < iov_cnt && iov[niov].iov_len <= tail; ++niov) {
178 tail -= iov[niov].iov_len;
179 }
180 if (tail) {
181 /* second, fixup the last element, and remember the original
182 * length */
183 assert(niov < iov_cnt);
184 assert(iov[niov].iov_len > tail);
185 orig_len = iov[niov].iov_len;
186 iov[niov++].iov_len = tail;
2be178a4
MT
187 ret = do_send_recv(sockfd, iov, niov, do_send);
188 /* Undo the changes above before checking for errors */
83f75c26 189 iov[niov-1].iov_len = orig_len;
2be178a4
MT
190 } else {
191 ret = do_send_recv(sockfd, iov, niov, do_send);
83f75c26
PB
192 }
193 if (offset) {
194 iov[0].iov_base -= offset;
195 iov[0].iov_len += offset;
196 }
197
198 if (ret < 0) {
199 assert(errno != EINTR);
6b64640d 200 g_free(local_iov);
83f75c26
PB
201 if (errno == EAGAIN && total > 0) {
202 return total;
203 }
204 return -1;
205 }
206
84004290
MK
207 if (ret == 0 && !do_send) {
208 /* recv returns 0 when the peer has performed an orderly
209 * shutdown. */
210 break;
211 }
212
83f75c26
PB
213 /* Prepare for the next iteration */
214 offset += ret;
215 total += ret;
216 bytes -= ret;
25e5e4c7 217 }
25e5e4c7 218
6b64640d 219 g_free(local_iov);
83f75c26 220 return total;
25e5e4c7
MT
221}
222
223
3a1dca94
GH
224void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
225 FILE *fp, const char *prefix, size_t limit)
226{
6ff66f50
PC
227 int v;
228 size_t size = 0;
229 char *buf;
230
231 for (v = 0; v < iov_cnt; v++) {
232 size += iov[v].iov_len;
3a1dca94 233 }
6ff66f50
PC
234 size = size > limit ? limit : size;
235 buf = g_malloc(size);
236 iov_to_buf(iov, iov_cnt, 0, buf, size);
3568ac2a 237 qemu_hexdump(buf, fp, prefix, size);
6ff66f50 238 g_free(buf);
3a1dca94 239}
0191253c 240
d336336c
MT
241unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt,
242 const struct iovec *iov, unsigned int iov_cnt,
243 size_t offset, size_t bytes)
244{
245 size_t len;
246 unsigned int i, j;
247 for (i = 0, j = 0; i < iov_cnt && j < dst_iov_cnt && bytes; i++) {
248 if (offset >= iov[i].iov_len) {
249 offset -= iov[i].iov_len;
250 continue;
251 }
252 len = MIN(bytes, iov[i].iov_len - offset);
253
254 dst_iov[j].iov_base = iov[i].iov_base + offset;
255 dst_iov[j].iov_len = len;
256 j++;
257 bytes -= len;
258 offset = 0;
259 }
260 assert(offset == 0);
261 return j;
262}
f563a5d7 263
0191253c
PB
264/* io vectors */
265
266void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint)
267{
e1cf5582 268 qiov->iov = g_new(struct iovec, alloc_hint);
0191253c
PB
269 qiov->niov = 0;
270 qiov->nalloc = alloc_hint;
271 qiov->size = 0;
272}
273
274void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov)
275{
276 int i;
277
278 qiov->iov = iov;
279 qiov->niov = niov;
280 qiov->nalloc = -1;
281 qiov->size = 0;
282 for (i = 0; i < niov; i++)
283 qiov->size += iov[i].iov_len;
284}
285
286void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len)
287{
288 assert(qiov->nalloc != -1);
289
290 if (qiov->niov == qiov->nalloc) {
291 qiov->nalloc = 2 * qiov->nalloc + 1;
e1cf5582 292 qiov->iov = g_renew(struct iovec, qiov->iov, qiov->nalloc);
0191253c
PB
293 }
294 qiov->iov[qiov->niov].iov_base = base;
295 qiov->iov[qiov->niov].iov_len = len;
296 qiov->size += len;
297 ++qiov->niov;
298}
299
300/*
530c0bbd 301 * Concatenates (partial) iovecs from src_iov to the end of dst.
0191253c
PB
302 * It starts copying after skipping `soffset' bytes at the
303 * beginning of src and adds individual vectors from src to
304 * dst copies up to `sbytes' bytes total, or up to the end
530c0bbd 305 * of src_iov if it comes first. This way, it is okay to specify
0191253c
PB
306 * very large value for `sbytes' to indicate "up to the end
307 * of src".
308 * Only vector pointers are processed, not the actual data buffers.
309 */
519661ee
PB
310size_t qemu_iovec_concat_iov(QEMUIOVector *dst,
311 struct iovec *src_iov, unsigned int src_cnt,
312 size_t soffset, size_t sbytes)
0191253c
PB
313{
314 int i;
315 size_t done;
facf98ad
AK
316
317 if (!sbytes) {
519661ee 318 return 0;
facf98ad 319 }
0191253c 320 assert(dst->nalloc != -1);
530c0bbd
SH
321 for (i = 0, done = 0; done < sbytes && i < src_cnt; i++) {
322 if (soffset < src_iov[i].iov_len) {
323 size_t len = MIN(src_iov[i].iov_len - soffset, sbytes - done);
324 qemu_iovec_add(dst, src_iov[i].iov_base + soffset, len);
0191253c
PB
325 done += len;
326 soffset = 0;
327 } else {
530c0bbd 328 soffset -= src_iov[i].iov_len;
0191253c
PB
329 }
330 }
530c0bbd 331 assert(soffset == 0); /* offset beyond end of src */
519661ee
PB
332
333 return done;
530c0bbd
SH
334}
335
336/*
337 * Concatenates (partial) iovecs from src to the end of dst.
338 * It starts copying after skipping `soffset' bytes at the
339 * beginning of src and adds individual vectors from src to
340 * dst copies up to `sbytes' bytes total, or up to the end
341 * of src if it comes first. This way, it is okay to specify
342 * very large value for `sbytes' to indicate "up to the end
343 * of src".
344 * Only vector pointers are processed, not the actual data buffers.
345 */
346void qemu_iovec_concat(QEMUIOVector *dst,
347 QEMUIOVector *src, size_t soffset, size_t sbytes)
348{
349 qemu_iovec_concat_iov(dst, src->iov, src->niov, soffset, sbytes);
0191253c
PB
350}
351
43f35cb5
PL
352/*
353 * Check if the contents of the iovecs are all zero
354 */
355bool qemu_iovec_is_zero(QEMUIOVector *qiov)
356{
357 int i;
358 for (i = 0; i < qiov->niov; i++) {
359 size_t offs = QEMU_ALIGN_DOWN(qiov->iov[i].iov_len, 4 * sizeof(long));
360 uint8_t *ptr = qiov->iov[i].iov_base;
361 if (offs && !buffer_is_zero(qiov->iov[i].iov_base, offs)) {
362 return false;
363 }
364 for (; offs < qiov->iov[i].iov_len; offs++) {
365 if (ptr[offs]) {
366 return false;
367 }
368 }
369 }
370 return true;
371}
372
0191253c
PB
373void qemu_iovec_destroy(QEMUIOVector *qiov)
374{
375 assert(qiov->nalloc != -1);
376
377 qemu_iovec_reset(qiov);
378 g_free(qiov->iov);
379 qiov->nalloc = 0;
380 qiov->iov = NULL;
381}
382
383void qemu_iovec_reset(QEMUIOVector *qiov)
384{
385 assert(qiov->nalloc != -1);
386
387 qiov->niov = 0;
388 qiov->size = 0;
389}
390
391size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
392 void *buf, size_t bytes)
393{
394 return iov_to_buf(qiov->iov, qiov->niov, offset, buf, bytes);
395}
396
397size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
398 const void *buf, size_t bytes)
399{
400 return iov_from_buf(qiov->iov, qiov->niov, offset, buf, bytes);
401}
402
403size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
404 int fillc, size_t bytes)
405{
406 return iov_memset(qiov->iov, qiov->niov, offset, fillc, bytes);
407}
d0277635 408
f70d7f7e
BC
409/**
410 * Check that I/O vector contents are identical
411 *
412 * The IO vectors must have the same structure (same length of all parts).
413 * A typical usage is to compare vectors created with qemu_iovec_clone().
414 *
415 * @a: I/O vector
416 * @b: I/O vector
417 * @ret: Offset to first mismatching byte or -1 if match
418 */
419ssize_t qemu_iovec_compare(QEMUIOVector *a, QEMUIOVector *b)
420{
421 int i;
422 ssize_t offset = 0;
423
424 assert(a->niov == b->niov);
425 for (i = 0; i < a->niov; i++) {
426 size_t len = 0;
427 uint8_t *p = (uint8_t *)a->iov[i].iov_base;
428 uint8_t *q = (uint8_t *)b->iov[i].iov_base;
429
430 assert(a->iov[i].iov_len == b->iov[i].iov_len);
431 while (len < a->iov[i].iov_len && *p++ == *q++) {
432 len++;
433 }
434
435 offset += len;
436
437 if (len != a->iov[i].iov_len) {
438 return offset;
439 }
440 }
441 return -1;
442}
443
444typedef struct {
445 int src_index;
446 struct iovec *src_iov;
447 void *dest_base;
448} IOVectorSortElem;
449
450static int sortelem_cmp_src_base(const void *a, const void *b)
451{
452 const IOVectorSortElem *elem_a = a;
453 const IOVectorSortElem *elem_b = b;
454
455 /* Don't overflow */
456 if (elem_a->src_iov->iov_base < elem_b->src_iov->iov_base) {
457 return -1;
458 } else if (elem_a->src_iov->iov_base > elem_b->src_iov->iov_base) {
459 return 1;
460 } else {
461 return 0;
462 }
463}
464
465static int sortelem_cmp_src_index(const void *a, const void *b)
466{
467 const IOVectorSortElem *elem_a = a;
468 const IOVectorSortElem *elem_b = b;
469
470 return elem_a->src_index - elem_b->src_index;
471}
472
473/**
474 * Copy contents of I/O vector
475 *
476 * The relative relationships of overlapping iovecs are preserved. This is
477 * necessary to ensure identical semantics in the cloned I/O vector.
478 */
479void qemu_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src, void *buf)
480{
481 IOVectorSortElem sortelems[src->niov];
482 void *last_end;
483 int i;
484
485 /* Sort by source iovecs by base address */
486 for (i = 0; i < src->niov; i++) {
487 sortelems[i].src_index = i;
488 sortelems[i].src_iov = &src->iov[i];
489 }
490 qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_base);
491
492 /* Allocate buffer space taking into account overlapping iovecs */
493 last_end = NULL;
494 for (i = 0; i < src->niov; i++) {
495 struct iovec *cur = sortelems[i].src_iov;
496 ptrdiff_t rewind = 0;
497
498 /* Detect overlap */
499 if (last_end && last_end > cur->iov_base) {
500 rewind = last_end - cur->iov_base;
501 }
502
503 sortelems[i].dest_base = buf - rewind;
504 buf += cur->iov_len - MIN(rewind, cur->iov_len);
505 last_end = MAX(cur->iov_base + cur->iov_len, last_end);
506 }
507
508 /* Sort by source iovec index and build destination iovec */
509 qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_index);
510 for (i = 0; i < src->niov; i++) {
511 qemu_iovec_add(dest, sortelems[i].dest_base, src->iov[i].iov_len);
512 }
513}
514
d0277635
SH
515size_t iov_discard_front(struct iovec **iov, unsigned int *iov_cnt,
516 size_t bytes)
517{
518 size_t total = 0;
519 struct iovec *cur;
520
521 for (cur = *iov; *iov_cnt > 0; cur++) {
522 if (cur->iov_len > bytes) {
523 cur->iov_base += bytes;
524 cur->iov_len -= bytes;
525 total += bytes;
526 break;
527 }
528
529 bytes -= cur->iov_len;
530 total += cur->iov_len;
531 *iov_cnt -= 1;
532 }
533
534 *iov = cur;
535 return total;
536}
537
538size_t iov_discard_back(struct iovec *iov, unsigned int *iov_cnt,
539 size_t bytes)
540{
541 size_t total = 0;
542 struct iovec *cur;
543
544 if (*iov_cnt == 0) {
545 return 0;
546 }
547
548 cur = iov + (*iov_cnt - 1);
549
550 while (*iov_cnt > 0) {
551 if (cur->iov_len > bytes) {
552 cur->iov_len -= bytes;
553 total += bytes;
554 break;
555 }
556
557 bytes -= cur->iov_len;
558 total += cur->iov_len;
559 cur--;
560 *iov_cnt -= 1;
561 }
562
563 return total;
564}
58f423fb
KW
565
566void qemu_iovec_discard_back(QEMUIOVector *qiov, size_t bytes)
567{
568 size_t total;
569 unsigned int niov = qiov->niov;
570
571 assert(qiov->size >= bytes);
572 total = iov_discard_back(qiov->iov, &niov, bytes);
573 assert(total == bytes);
574
575 qiov->niov = niov;
576 qiov->size -= bytes;
577}
This page took 0.474218 seconds and 4 git commands to generate.