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.
19 #include "qemu/osdep.h"
20 #include "qemu-common.h"
22 #include "qemu/sockets.h"
24 size_t iov_from_buf_full(const struct iovec
*iov
, unsigned int iov_cnt
,
25 size_t offset
, const void *buf
, size_t bytes
)
29 for (i
= 0, done
= 0; (offset
|| done
< bytes
) && i
< iov_cnt
; i
++) {
30 if (offset
< iov
[i
].iov_len
) {
31 size_t len
= MIN(iov
[i
].iov_len
- offset
, bytes
- done
);
32 memcpy(iov
[i
].iov_base
+ offset
, buf
+ done
, len
);
36 offset
-= iov
[i
].iov_len
;
43 size_t iov_to_buf_full(const struct iovec
*iov
, const unsigned int iov_cnt
,
44 size_t offset
, void *buf
, size_t bytes
)
48 for (i
= 0, done
= 0; (offset
|| done
< bytes
) && i
< iov_cnt
; i
++) {
49 if (offset
< iov
[i
].iov_len
) {
50 size_t len
= MIN(iov
[i
].iov_len
- offset
, bytes
- done
);
51 memcpy(buf
+ done
, iov
[i
].iov_base
+ offset
, len
);
55 offset
-= iov
[i
].iov_len
;
62 size_t iov_memset(const struct iovec
*iov
, const unsigned int iov_cnt
,
63 size_t offset
, int fillc
, size_t bytes
)
67 for (i
= 0, done
= 0; (offset
|| done
< bytes
) && i
< iov_cnt
; i
++) {
68 if (offset
< iov
[i
].iov_len
) {
69 size_t len
= MIN(iov
[i
].iov_len
- offset
, bytes
- done
);
70 memset(iov
[i
].iov_base
+ offset
, fillc
, len
);
74 offset
-= iov
[i
].iov_len
;
81 size_t iov_size(const struct iovec
*iov
, const unsigned int iov_cnt
)
87 for (i
= 0; i
< iov_cnt
; i
++) {
88 len
+= iov
[i
].iov_len
;
93 /* helper function for iov_send_recv() */
95 do_send_recv(int sockfd
, struct iovec
*iov
, unsigned iov_cnt
, bool do_send
)
100 memset(&msg
, 0, sizeof(msg
));
102 msg
.msg_iovlen
= iov_cnt
;
105 ? sendmsg(sockfd
, &msg
, 0)
106 : recvmsg(sockfd
, &msg
, 0);
107 } while (ret
< 0 && errno
== EINTR
);
110 /* else send piece-by-piece */
111 /*XXX Note: windows has WSASend() and WSARecv() */
114 while (i
< iov_cnt
) {
116 ? send(sockfd
, iov
[i
].iov_base
, iov
[i
].iov_len
, 0)
117 : recv(sockfd
, iov
[i
].iov_base
, iov
[i
].iov_len
, 0);
122 } else if (errno
== EINTR
) {
125 /* else it is some "other" error,
126 * only return if there was no data processed. */
138 ssize_t
iov_send_recv(int sockfd
, const struct iovec
*_iov
, unsigned iov_cnt
,
139 size_t offset
, size_t bytes
,
144 size_t orig_len
, tail
;
146 struct iovec
*local_iov
, *iov
;
152 local_iov
= g_new0(struct iovec
, iov_cnt
);
153 iov_copy(local_iov
, iov_cnt
, _iov
, iov_cnt
, offset
, bytes
);
158 /* Find the start position, skipping `offset' bytes:
159 * first, skip all full-sized vector elements, */
160 for (niov
= 0; niov
< iov_cnt
&& offset
>= iov
[niov
].iov_len
; ++niov
) {
161 offset
-= iov
[niov
].iov_len
;
164 /* niov == iov_cnt would only be valid if bytes == 0, which
165 * we already ruled out in the loop condition. */
166 assert(niov
< iov_cnt
);
171 /* second, skip `offset' bytes from the (now) first element,
173 iov
[0].iov_base
+= offset
;
174 iov
[0].iov_len
-= offset
;
176 /* Find the end position skipping `bytes' bytes: */
177 /* first, skip all full-sized elements */
179 for (niov
= 0; niov
< iov_cnt
&& iov
[niov
].iov_len
<= tail
; ++niov
) {
180 tail
-= iov
[niov
].iov_len
;
183 /* second, fixup the last element, and remember the original
185 assert(niov
< iov_cnt
);
186 assert(iov
[niov
].iov_len
> tail
);
187 orig_len
= iov
[niov
].iov_len
;
188 iov
[niov
++].iov_len
= tail
;
189 ret
= do_send_recv(sockfd
, iov
, niov
, do_send
);
190 /* Undo the changes above before checking for errors */
191 iov
[niov
-1].iov_len
= orig_len
;
193 ret
= do_send_recv(sockfd
, iov
, niov
, do_send
);
196 iov
[0].iov_base
-= offset
;
197 iov
[0].iov_len
+= offset
;
201 assert(errno
!= EINTR
);
203 if (errno
== EAGAIN
&& total
> 0) {
209 if (ret
== 0 && !do_send
) {
210 /* recv returns 0 when the peer has performed an orderly
215 /* Prepare for the next iteration */
226 void iov_hexdump(const struct iovec
*iov
, const unsigned int iov_cnt
,
227 FILE *fp
, const char *prefix
, size_t limit
)
233 for (v
= 0; v
< iov_cnt
; v
++) {
234 size
+= iov
[v
].iov_len
;
236 size
= size
> limit
? limit
: size
;
237 buf
= g_malloc(size
);
238 iov_to_buf(iov
, iov_cnt
, 0, buf
, size
);
239 qemu_hexdump(buf
, fp
, prefix
, size
);
243 unsigned iov_copy(struct iovec
*dst_iov
, unsigned int dst_iov_cnt
,
244 const struct iovec
*iov
, unsigned int iov_cnt
,
245 size_t offset
, size_t bytes
)
249 for (i
= 0, j
= 0; i
< iov_cnt
&& j
< dst_iov_cnt
&& bytes
; i
++) {
250 if (offset
>= iov
[i
].iov_len
) {
251 offset
-= iov
[i
].iov_len
;
254 len
= MIN(bytes
, iov
[i
].iov_len
- offset
);
256 dst_iov
[j
].iov_base
= iov
[i
].iov_base
+ offset
;
257 dst_iov
[j
].iov_len
= len
;
268 void qemu_iovec_init(QEMUIOVector
*qiov
, int alloc_hint
)
270 qiov
->iov
= g_new(struct iovec
, alloc_hint
);
272 qiov
->nalloc
= alloc_hint
;
276 void qemu_iovec_init_external(QEMUIOVector
*qiov
, struct iovec
*iov
, int niov
)
284 for (i
= 0; i
< niov
; i
++)
285 qiov
->size
+= iov
[i
].iov_len
;
288 void qemu_iovec_add(QEMUIOVector
*qiov
, void *base
, size_t len
)
290 assert(qiov
->nalloc
!= -1);
292 if (qiov
->niov
== qiov
->nalloc
) {
293 qiov
->nalloc
= 2 * qiov
->nalloc
+ 1;
294 qiov
->iov
= g_renew(struct iovec
, qiov
->iov
, qiov
->nalloc
);
296 qiov
->iov
[qiov
->niov
].iov_base
= base
;
297 qiov
->iov
[qiov
->niov
].iov_len
= len
;
303 * Concatenates (partial) iovecs from src_iov to the end of dst.
304 * It starts copying after skipping `soffset' bytes at the
305 * beginning of src and adds individual vectors from src to
306 * dst copies up to `sbytes' bytes total, or up to the end
307 * of src_iov if it comes first. This way, it is okay to specify
308 * very large value for `sbytes' to indicate "up to the end
310 * Only vector pointers are processed, not the actual data buffers.
312 size_t qemu_iovec_concat_iov(QEMUIOVector
*dst
,
313 struct iovec
*src_iov
, unsigned int src_cnt
,
314 size_t soffset
, size_t sbytes
)
322 assert(dst
->nalloc
!= -1);
323 for (i
= 0, done
= 0; done
< sbytes
&& i
< src_cnt
; i
++) {
324 if (soffset
< src_iov
[i
].iov_len
) {
325 size_t len
= MIN(src_iov
[i
].iov_len
- soffset
, sbytes
- done
);
326 qemu_iovec_add(dst
, src_iov
[i
].iov_base
+ soffset
, len
);
330 soffset
-= src_iov
[i
].iov_len
;
333 assert(soffset
== 0); /* offset beyond end of src */
339 * Concatenates (partial) iovecs from src to the end of dst.
340 * It starts copying after skipping `soffset' bytes at the
341 * beginning of src and adds individual vectors from src to
342 * dst copies up to `sbytes' bytes total, or up to the end
343 * of src if it comes first. This way, it is okay to specify
344 * very large value for `sbytes' to indicate "up to the end
346 * Only vector pointers are processed, not the actual data buffers.
348 void qemu_iovec_concat(QEMUIOVector
*dst
,
349 QEMUIOVector
*src
, size_t soffset
, size_t sbytes
)
351 qemu_iovec_concat_iov(dst
, src
->iov
, src
->niov
, soffset
, sbytes
);
355 * Check if the contents of the iovecs are all zero
357 bool qemu_iovec_is_zero(QEMUIOVector
*qiov
)
360 for (i
= 0; i
< qiov
->niov
; i
++) {
361 size_t offs
= QEMU_ALIGN_DOWN(qiov
->iov
[i
].iov_len
, 4 * sizeof(long));
362 uint8_t *ptr
= qiov
->iov
[i
].iov_base
;
363 if (offs
&& !buffer_is_zero(qiov
->iov
[i
].iov_base
, offs
)) {
366 for (; offs
< qiov
->iov
[i
].iov_len
; offs
++) {
375 void qemu_iovec_destroy(QEMUIOVector
*qiov
)
377 assert(qiov
->nalloc
!= -1);
379 qemu_iovec_reset(qiov
);
385 void qemu_iovec_reset(QEMUIOVector
*qiov
)
387 assert(qiov
->nalloc
!= -1);
393 size_t qemu_iovec_to_buf(QEMUIOVector
*qiov
, size_t offset
,
394 void *buf
, size_t bytes
)
396 return iov_to_buf(qiov
->iov
, qiov
->niov
, offset
, buf
, bytes
);
399 size_t qemu_iovec_from_buf(QEMUIOVector
*qiov
, size_t offset
,
400 const void *buf
, size_t bytes
)
402 return iov_from_buf(qiov
->iov
, qiov
->niov
, offset
, buf
, bytes
);
405 size_t qemu_iovec_memset(QEMUIOVector
*qiov
, size_t offset
,
406 int fillc
, size_t bytes
)
408 return iov_memset(qiov
->iov
, qiov
->niov
, offset
, fillc
, bytes
);
412 * Check that I/O vector contents are identical
414 * The IO vectors must have the same structure (same length of all parts).
415 * A typical usage is to compare vectors created with qemu_iovec_clone().
419 * @ret: Offset to first mismatching byte or -1 if match
421 ssize_t
qemu_iovec_compare(QEMUIOVector
*a
, QEMUIOVector
*b
)
426 assert(a
->niov
== b
->niov
);
427 for (i
= 0; i
< a
->niov
; i
++) {
429 uint8_t *p
= (uint8_t *)a
->iov
[i
].iov_base
;
430 uint8_t *q
= (uint8_t *)b
->iov
[i
].iov_base
;
432 assert(a
->iov
[i
].iov_len
== b
->iov
[i
].iov_len
);
433 while (len
< a
->iov
[i
].iov_len
&& *p
++ == *q
++) {
439 if (len
!= a
->iov
[i
].iov_len
) {
448 struct iovec
*src_iov
;
452 static int sortelem_cmp_src_base(const void *a
, const void *b
)
454 const IOVectorSortElem
*elem_a
= a
;
455 const IOVectorSortElem
*elem_b
= b
;
458 if (elem_a
->src_iov
->iov_base
< elem_b
->src_iov
->iov_base
) {
460 } else if (elem_a
->src_iov
->iov_base
> elem_b
->src_iov
->iov_base
) {
467 static int sortelem_cmp_src_index(const void *a
, const void *b
)
469 const IOVectorSortElem
*elem_a
= a
;
470 const IOVectorSortElem
*elem_b
= b
;
472 return elem_a
->src_index
- elem_b
->src_index
;
476 * Copy contents of I/O vector
478 * The relative relationships of overlapping iovecs are preserved. This is
479 * necessary to ensure identical semantics in the cloned I/O vector.
481 void qemu_iovec_clone(QEMUIOVector
*dest
, const QEMUIOVector
*src
, void *buf
)
483 IOVectorSortElem sortelems
[src
->niov
];
487 /* Sort by source iovecs by base address */
488 for (i
= 0; i
< src
->niov
; i
++) {
489 sortelems
[i
].src_index
= i
;
490 sortelems
[i
].src_iov
= &src
->iov
[i
];
492 qsort(sortelems
, src
->niov
, sizeof(sortelems
[0]), sortelem_cmp_src_base
);
494 /* Allocate buffer space taking into account overlapping iovecs */
496 for (i
= 0; i
< src
->niov
; i
++) {
497 struct iovec
*cur
= sortelems
[i
].src_iov
;
498 ptrdiff_t rewind
= 0;
501 if (last_end
&& last_end
> cur
->iov_base
) {
502 rewind
= last_end
- cur
->iov_base
;
505 sortelems
[i
].dest_base
= buf
- rewind
;
506 buf
+= cur
->iov_len
- MIN(rewind
, cur
->iov_len
);
507 last_end
= MAX(cur
->iov_base
+ cur
->iov_len
, last_end
);
510 /* Sort by source iovec index and build destination iovec */
511 qsort(sortelems
, src
->niov
, sizeof(sortelems
[0]), sortelem_cmp_src_index
);
512 for (i
= 0; i
< src
->niov
; i
++) {
513 qemu_iovec_add(dest
, sortelems
[i
].dest_base
, src
->iov
[i
].iov_len
);
517 size_t iov_discard_front(struct iovec
**iov
, unsigned int *iov_cnt
,
523 for (cur
= *iov
; *iov_cnt
> 0; cur
++) {
524 if (cur
->iov_len
> bytes
) {
525 cur
->iov_base
+= bytes
;
526 cur
->iov_len
-= bytes
;
531 bytes
-= cur
->iov_len
;
532 total
+= cur
->iov_len
;
540 size_t iov_discard_back(struct iovec
*iov
, unsigned int *iov_cnt
,
550 cur
= iov
+ (*iov_cnt
- 1);
552 while (*iov_cnt
> 0) {
553 if (cur
->iov_len
> bytes
) {
554 cur
->iov_len
-= bytes
;
559 bytes
-= cur
->iov_len
;
560 total
+= cur
->iov_len
;
568 void qemu_iovec_discard_back(QEMUIOVector
*qiov
, size_t bytes
)
571 unsigned int niov
= qiov
->niov
;
573 assert(qiov
->size
>= bytes
);
574 total
= iov_discard_back(qiov
->iov
, &niov
, bytes
);
575 assert(total
== bytes
);