2 * Helpers for getting linearized buffers from iov / filling buffers into iovs
4 * Copyright IBM, Corp. 2007, 2008
5 * Copyright (C) 2010 Red Hat, Inc.
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Amit Shah <amit.shah@redhat.com>
10 * Michael Tokarev <mjt@tls.msk.ru>
12 * This work is licensed under the terms of the GNU GPL, version 2. See
13 * the COPYING file in the top-level directory.
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.
20 #include "qemu/sockets.h"
22 size_t iov_from_buf(const struct iovec
*iov
, unsigned int iov_cnt
,
23 size_t offset
, const void *buf
, size_t bytes
)
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
);
34 offset
-= iov
[i
].iov_len
;
41 size_t iov_to_buf(const struct iovec
*iov
, const unsigned int iov_cnt
,
42 size_t offset
, void *buf
, size_t bytes
)
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
);
53 offset
-= iov
[i
].iov_len
;
60 size_t iov_memset(const struct iovec
*iov
, const unsigned int iov_cnt
,
61 size_t offset
, int fillc
, size_t bytes
)
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
);
72 offset
-= iov
[i
].iov_len
;
79 size_t iov_size(const struct iovec
*iov
, const unsigned int iov_cnt
)
85 for (i
= 0; i
< iov_cnt
; i
++) {
86 len
+= iov
[i
].iov_len
;
91 /* helper function for iov_send_recv() */
93 do_send_recv(int sockfd
, struct iovec
*iov
, unsigned iov_cnt
, bool do_send
)
98 memset(&msg
, 0, sizeof(msg
));
100 msg
.msg_iovlen
= iov_cnt
;
103 ? sendmsg(sockfd
, &msg
, 0)
104 : recvmsg(sockfd
, &msg
, 0);
105 } while (ret
< 0 && errno
== EINTR
);
108 /* else send piece-by-piece */
109 /*XXX Note: windows has WSASend() and WSARecv() */
112 while (i
< iov_cnt
) {
114 ? send(sockfd
, iov
[i
].iov_base
, iov
[i
].iov_len
, 0)
115 : recv(sockfd
, iov
[i
].iov_base
, iov
[i
].iov_len
, 0);
120 } else if (errno
== EINTR
) {
123 /* else it is some "other" error,
124 * only return if there was no data processed. */
136 ssize_t
iov_send_recv(int sockfd
, const struct iovec
*_iov
, unsigned iov_cnt
,
137 size_t offset
, size_t bytes
,
142 size_t orig_len
, tail
;
144 struct iovec
*local_iov
, *iov
;
150 local_iov
= g_new0(struct iovec
, iov_cnt
);
151 iov_copy(local_iov
, iov_cnt
, _iov
, iov_cnt
, offset
, bytes
);
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
;
162 /* niov == iov_cnt would only be valid if bytes == 0, which
163 * we already ruled out in the loop condition. */
164 assert(niov
< iov_cnt
);
169 /* second, skip `offset' bytes from the (now) first element,
171 iov
[0].iov_base
+= offset
;
172 iov
[0].iov_len
-= offset
;
174 /* Find the end position skipping `bytes' bytes: */
175 /* first, skip all full-sized elements */
177 for (niov
= 0; niov
< iov_cnt
&& iov
[niov
].iov_len
<= tail
; ++niov
) {
178 tail
-= iov
[niov
].iov_len
;
181 /* second, fixup the last element, and remember the original
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
;
187 ret
= do_send_recv(sockfd
, iov
, niov
, do_send
);
188 /* Undo the changes above before checking for errors */
189 iov
[niov
-1].iov_len
= orig_len
;
191 ret
= do_send_recv(sockfd
, iov
, niov
, do_send
);
194 iov
[0].iov_base
-= offset
;
195 iov
[0].iov_len
+= offset
;
199 assert(errno
!= EINTR
);
201 if (errno
== EAGAIN
&& total
> 0) {
207 if (ret
== 0 && !do_send
) {
208 /* recv returns 0 when the peer has performed an orderly
213 /* Prepare for the next iteration */
224 void iov_hexdump(const struct iovec
*iov
, const unsigned int iov_cnt
,
225 FILE *fp
, const char *prefix
, size_t limit
)
231 for (v
= 0; v
< iov_cnt
; v
++) {
232 size
+= iov
[v
].iov_len
;
234 size
= size
> limit
? limit
: size
;
235 buf
= g_malloc(size
);
236 iov_to_buf(iov
, iov_cnt
, 0, buf
, size
);
237 qemu_hexdump(buf
, fp
, prefix
, size
);
241 unsigned 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
)
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
;
252 len
= MIN(bytes
, iov
[i
].iov_len
- offset
);
254 dst_iov
[j
].iov_base
= iov
[i
].iov_base
+ offset
;
255 dst_iov
[j
].iov_len
= len
;
266 void qemu_iovec_init(QEMUIOVector
*qiov
, int alloc_hint
)
268 qiov
->iov
= g_new(struct iovec
, alloc_hint
);
270 qiov
->nalloc
= alloc_hint
;
274 void qemu_iovec_init_external(QEMUIOVector
*qiov
, struct iovec
*iov
, int niov
)
282 for (i
= 0; i
< niov
; i
++)
283 qiov
->size
+= iov
[i
].iov_len
;
286 void qemu_iovec_add(QEMUIOVector
*qiov
, void *base
, size_t len
)
288 assert(qiov
->nalloc
!= -1);
290 if (qiov
->niov
== qiov
->nalloc
) {
291 qiov
->nalloc
= 2 * qiov
->nalloc
+ 1;
292 qiov
->iov
= g_renew(struct iovec
, qiov
->iov
, qiov
->nalloc
);
294 qiov
->iov
[qiov
->niov
].iov_base
= base
;
295 qiov
->iov
[qiov
->niov
].iov_len
= len
;
301 * Concatenates (partial) iovecs from src_iov to the end of dst.
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
305 * of src_iov if it comes first. This way, it is okay to specify
306 * very large value for `sbytes' to indicate "up to the end
308 * Only vector pointers are processed, not the actual data buffers.
310 size_t qemu_iovec_concat_iov(QEMUIOVector
*dst
,
311 struct iovec
*src_iov
, unsigned int src_cnt
,
312 size_t soffset
, size_t sbytes
)
320 assert(dst
->nalloc
!= -1);
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
);
328 soffset
-= src_iov
[i
].iov_len
;
331 assert(soffset
== 0); /* offset beyond end of src */
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
344 * Only vector pointers are processed, not the actual data buffers.
346 void qemu_iovec_concat(QEMUIOVector
*dst
,
347 QEMUIOVector
*src
, size_t soffset
, size_t sbytes
)
349 qemu_iovec_concat_iov(dst
, src
->iov
, src
->niov
, soffset
, sbytes
);
353 * Check if the contents of the iovecs are all zero
355 bool qemu_iovec_is_zero(QEMUIOVector
*qiov
)
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
)) {
364 for (; offs
< qiov
->iov
[i
].iov_len
; offs
++) {
373 void qemu_iovec_destroy(QEMUIOVector
*qiov
)
375 assert(qiov
->nalloc
!= -1);
377 qemu_iovec_reset(qiov
);
383 void qemu_iovec_reset(QEMUIOVector
*qiov
)
385 assert(qiov
->nalloc
!= -1);
391 size_t qemu_iovec_to_buf(QEMUIOVector
*qiov
, size_t offset
,
392 void *buf
, size_t bytes
)
394 return iov_to_buf(qiov
->iov
, qiov
->niov
, offset
, buf
, bytes
);
397 size_t qemu_iovec_from_buf(QEMUIOVector
*qiov
, size_t offset
,
398 const void *buf
, size_t bytes
)
400 return iov_from_buf(qiov
->iov
, qiov
->niov
, offset
, buf
, bytes
);
403 size_t qemu_iovec_memset(QEMUIOVector
*qiov
, size_t offset
,
404 int fillc
, size_t bytes
)
406 return iov_memset(qiov
->iov
, qiov
->niov
, offset
, fillc
, bytes
);
410 * Check that I/O vector contents are identical
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().
417 * @ret: Offset to first mismatching byte or -1 if match
419 ssize_t
qemu_iovec_compare(QEMUIOVector
*a
, QEMUIOVector
*b
)
424 assert(a
->niov
== b
->niov
);
425 for (i
= 0; i
< a
->niov
; i
++) {
427 uint8_t *p
= (uint8_t *)a
->iov
[i
].iov_base
;
428 uint8_t *q
= (uint8_t *)b
->iov
[i
].iov_base
;
430 assert(a
->iov
[i
].iov_len
== b
->iov
[i
].iov_len
);
431 while (len
< a
->iov
[i
].iov_len
&& *p
++ == *q
++) {
437 if (len
!= a
->iov
[i
].iov_len
) {
446 struct iovec
*src_iov
;
450 static int sortelem_cmp_src_base(const void *a
, const void *b
)
452 const IOVectorSortElem
*elem_a
= a
;
453 const IOVectorSortElem
*elem_b
= b
;
456 if (elem_a
->src_iov
->iov_base
< elem_b
->src_iov
->iov_base
) {
458 } else if (elem_a
->src_iov
->iov_base
> elem_b
->src_iov
->iov_base
) {
465 static int sortelem_cmp_src_index(const void *a
, const void *b
)
467 const IOVectorSortElem
*elem_a
= a
;
468 const IOVectorSortElem
*elem_b
= b
;
470 return elem_a
->src_index
- elem_b
->src_index
;
474 * Copy contents of I/O vector
476 * The relative relationships of overlapping iovecs are preserved. This is
477 * necessary to ensure identical semantics in the cloned I/O vector.
479 void qemu_iovec_clone(QEMUIOVector
*dest
, const QEMUIOVector
*src
, void *buf
)
481 IOVectorSortElem sortelems
[src
->niov
];
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
];
490 qsort(sortelems
, src
->niov
, sizeof(sortelems
[0]), sortelem_cmp_src_base
);
492 /* Allocate buffer space taking into account overlapping iovecs */
494 for (i
= 0; i
< src
->niov
; i
++) {
495 struct iovec
*cur
= sortelems
[i
].src_iov
;
496 ptrdiff_t rewind
= 0;
499 if (last_end
&& last_end
> cur
->iov_base
) {
500 rewind
= last_end
- cur
->iov_base
;
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
);
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
);
515 size_t iov_discard_front(struct iovec
**iov
, unsigned int *iov_cnt
,
521 for (cur
= *iov
; *iov_cnt
> 0; cur
++) {
522 if (cur
->iov_len
> bytes
) {
523 cur
->iov_base
+= bytes
;
524 cur
->iov_len
-= bytes
;
529 bytes
-= cur
->iov_len
;
530 total
+= cur
->iov_len
;
538 size_t iov_discard_back(struct iovec
*iov
, unsigned int *iov_cnt
,
548 cur
= iov
+ (*iov_cnt
- 1);
550 while (*iov_cnt
> 0) {
551 if (cur
->iov_len
> bytes
) {
552 cur
->iov_len
-= bytes
;
557 bytes
-= cur
->iov_len
;
558 total
+= cur
->iov_len
;
566 void qemu_iovec_discard_back(QEMUIOVector
*qiov
, size_t bytes
)
569 unsigned int niov
= qiov
->niov
;
571 assert(qiov
->size
>= bytes
);
572 total
= iov_discard_back(qiov
->iov
, &niov
, bytes
);
573 assert(total
== bytes
);