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"
21 #include "qemu/sockets.h"
22 #include "qemu/cutils.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(fp
, prefix
, buf
, 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
)
250 i
< iov_cnt
&& j
< dst_iov_cnt
&& (offset
|| bytes
); i
++) {
251 if (offset
>= iov
[i
].iov_len
) {
252 offset
-= iov
[i
].iov_len
;
255 len
= MIN(bytes
, iov
[i
].iov_len
- offset
);
257 dst_iov
[j
].iov_base
= iov
[i
].iov_base
+ offset
;
258 dst_iov
[j
].iov_len
= len
;
269 void qemu_iovec_init(QEMUIOVector
*qiov
, int alloc_hint
)
271 qiov
->iov
= g_new(struct iovec
, alloc_hint
);
273 qiov
->nalloc
= alloc_hint
;
277 void qemu_iovec_init_external(QEMUIOVector
*qiov
, struct iovec
*iov
, int niov
)
285 for (i
= 0; i
< niov
; i
++)
286 qiov
->size
+= iov
[i
].iov_len
;
289 void qemu_iovec_add(QEMUIOVector
*qiov
, void *base
, size_t len
)
291 assert(qiov
->nalloc
!= -1);
293 if (qiov
->niov
== qiov
->nalloc
) {
294 qiov
->nalloc
= 2 * qiov
->nalloc
+ 1;
295 qiov
->iov
= g_renew(struct iovec
, qiov
->iov
, qiov
->nalloc
);
297 qiov
->iov
[qiov
->niov
].iov_base
= base
;
298 qiov
->iov
[qiov
->niov
].iov_len
= len
;
304 * Concatenates (partial) iovecs from src_iov to the end of dst.
305 * It starts copying after skipping `soffset' bytes at the
306 * beginning of src and adds individual vectors from src to
307 * dst copies up to `sbytes' bytes total, or up to the end
308 * of src_iov if it comes first. This way, it is okay to specify
309 * very large value for `sbytes' to indicate "up to the end
311 * Only vector pointers are processed, not the actual data buffers.
313 size_t qemu_iovec_concat_iov(QEMUIOVector
*dst
,
314 struct iovec
*src_iov
, unsigned int src_cnt
,
315 size_t soffset
, size_t sbytes
)
323 assert(dst
->nalloc
!= -1);
324 for (i
= 0, done
= 0; done
< sbytes
&& i
< src_cnt
; i
++) {
325 if (soffset
< src_iov
[i
].iov_len
) {
326 size_t len
= MIN(src_iov
[i
].iov_len
- soffset
, sbytes
- done
);
327 qemu_iovec_add(dst
, src_iov
[i
].iov_base
+ soffset
, len
);
331 soffset
-= src_iov
[i
].iov_len
;
334 assert(soffset
== 0); /* offset beyond end of src */
340 * Concatenates (partial) iovecs from src to the end of dst.
341 * It starts copying after skipping `soffset' bytes at the
342 * beginning of src and adds individual vectors from src to
343 * dst copies up to `sbytes' bytes total, or up to the end
344 * of src if it comes first. This way, it is okay to specify
345 * very large value for `sbytes' to indicate "up to the end
347 * Only vector pointers are processed, not the actual data buffers.
349 void qemu_iovec_concat(QEMUIOVector
*dst
,
350 QEMUIOVector
*src
, size_t soffset
, size_t sbytes
)
352 qemu_iovec_concat_iov(dst
, src
->iov
, src
->niov
, soffset
, sbytes
);
358 * Return pointer to iovec structure, where byte at @offset in original vector
360 * Set @remaining_offset to be offset inside that iovec to the same byte.
362 static struct iovec
*iov_skip_offset(struct iovec
*iov
, size_t offset
,
363 size_t *remaining_offset
)
365 while (offset
> 0 && offset
>= iov
->iov_len
) {
366 offset
-= iov
->iov_len
;
369 *remaining_offset
= offset
;
377 * Find subarray of iovec's, containing requested range. @head would
378 * be offset in first iov (returned by the function), @tail would be
379 * count of extra bytes in last iovec (returned iov + @niov - 1).
381 static struct iovec
*qiov_slice(QEMUIOVector
*qiov
,
382 size_t offset
, size_t len
,
383 size_t *head
, size_t *tail
, int *niov
)
385 struct iovec
*iov
, *end_iov
;
387 assert(offset
+ len
<= qiov
->size
);
389 iov
= iov_skip_offset(qiov
->iov
, offset
, head
);
390 end_iov
= iov_skip_offset(iov
, *head
+ len
, tail
);
393 assert(*tail
< end_iov
->iov_len
);
394 *tail
= end_iov
->iov_len
- *tail
;
398 *niov
= end_iov
- iov
;
403 int qemu_iovec_subvec_niov(QEMUIOVector
*qiov
, size_t offset
, size_t len
)
408 qiov_slice(qiov
, offset
, len
, &head
, &tail
, &niov
);
414 * Compile new iovec, combining @head_buf buffer, sub-qiov of @mid_qiov,
415 * and @tail_buf buffer into new qiov.
417 int qemu_iovec_init_extended(
419 void *head_buf
, size_t head_len
,
420 QEMUIOVector
*mid_qiov
, size_t mid_offset
, size_t mid_len
,
421 void *tail_buf
, size_t tail_len
)
423 size_t mid_head
, mid_tail
;
424 int total_niov
, mid_niov
= 0;
425 struct iovec
*p
, *mid_iov
= NULL
;
427 assert(mid_qiov
->niov
<= IOV_MAX
);
429 if (SIZE_MAX
- head_len
< mid_len
||
430 SIZE_MAX
- head_len
- mid_len
< tail_len
)
436 mid_iov
= qiov_slice(mid_qiov
, mid_offset
, mid_len
,
437 &mid_head
, &mid_tail
, &mid_niov
);
440 total_niov
= !!head_len
+ mid_niov
+ !!tail_len
;
441 if (total_niov
> IOV_MAX
) {
445 if (total_niov
== 1) {
446 qemu_iovec_init_buf(qiov
, NULL
, 0);
447 p
= &qiov
->local_iov
;
449 qiov
->niov
= qiov
->nalloc
= total_niov
;
450 qiov
->size
= head_len
+ mid_len
+ tail_len
;
451 p
= qiov
->iov
= g_new(struct iovec
, qiov
->niov
);
455 p
->iov_base
= head_buf
;
456 p
->iov_len
= head_len
;
460 assert(!mid_niov
== !mid_len
);
462 memcpy(p
, mid_iov
, mid_niov
* sizeof(*p
));
463 p
[0].iov_base
= (uint8_t *)p
[0].iov_base
+ mid_head
;
464 p
[0].iov_len
-= mid_head
;
465 p
[mid_niov
- 1].iov_len
-= mid_tail
;
470 p
->iov_base
= tail_buf
;
471 p
->iov_len
= tail_len
;
478 * Check if the contents of subrange of qiov data is all zeroes.
480 bool qemu_iovec_is_zero(QEMUIOVector
*qiov
, size_t offset
, size_t bytes
)
483 size_t current_offset
;
485 assert(offset
+ bytes
<= qiov
->size
);
487 iov
= iov_skip_offset(qiov
->iov
, offset
, ¤t_offset
);
490 uint8_t *base
= (uint8_t *)iov
->iov_base
+ current_offset
;
491 size_t len
= MIN(iov
->iov_len
- current_offset
, bytes
);
493 if (!buffer_is_zero(base
, len
)) {
505 void qemu_iovec_init_slice(QEMUIOVector
*qiov
, QEMUIOVector
*source
,
506 size_t offset
, size_t len
)
510 assert(source
->size
>= len
);
511 assert(source
->size
- len
>= offset
);
513 /* We shrink the request, so we can't overflow neither size_t nor MAX_IOV */
514 ret
= qemu_iovec_init_extended(qiov
, NULL
, 0, source
, offset
, len
, NULL
, 0);
518 void qemu_iovec_destroy(QEMUIOVector
*qiov
)
520 if (qiov
->nalloc
!= -1) {
524 memset(qiov
, 0, sizeof(*qiov
));
527 void qemu_iovec_reset(QEMUIOVector
*qiov
)
529 assert(qiov
->nalloc
!= -1);
535 size_t qemu_iovec_to_buf(QEMUIOVector
*qiov
, size_t offset
,
536 void *buf
, size_t bytes
)
538 return iov_to_buf(qiov
->iov
, qiov
->niov
, offset
, buf
, bytes
);
541 size_t qemu_iovec_from_buf(QEMUIOVector
*qiov
, size_t offset
,
542 const void *buf
, size_t bytes
)
544 return iov_from_buf(qiov
->iov
, qiov
->niov
, offset
, buf
, bytes
);
547 size_t qemu_iovec_memset(QEMUIOVector
*qiov
, size_t offset
,
548 int fillc
, size_t bytes
)
550 return iov_memset(qiov
->iov
, qiov
->niov
, offset
, fillc
, bytes
);
554 * Check that I/O vector contents are identical
556 * The IO vectors must have the same structure (same length of all parts).
557 * A typical usage is to compare vectors created with qemu_iovec_clone().
561 * @ret: Offset to first mismatching byte or -1 if match
563 ssize_t
qemu_iovec_compare(QEMUIOVector
*a
, QEMUIOVector
*b
)
568 assert(a
->niov
== b
->niov
);
569 for (i
= 0; i
< a
->niov
; i
++) {
571 uint8_t *p
= (uint8_t *)a
->iov
[i
].iov_base
;
572 uint8_t *q
= (uint8_t *)b
->iov
[i
].iov_base
;
574 assert(a
->iov
[i
].iov_len
== b
->iov
[i
].iov_len
);
575 while (len
< a
->iov
[i
].iov_len
&& *p
++ == *q
++) {
581 if (len
!= a
->iov
[i
].iov_len
) {
590 struct iovec
*src_iov
;
594 static int sortelem_cmp_src_base(const void *a
, const void *b
)
596 const IOVectorSortElem
*elem_a
= a
;
597 const IOVectorSortElem
*elem_b
= b
;
600 if (elem_a
->src_iov
->iov_base
< elem_b
->src_iov
->iov_base
) {
602 } else if (elem_a
->src_iov
->iov_base
> elem_b
->src_iov
->iov_base
) {
609 static int sortelem_cmp_src_index(const void *a
, const void *b
)
611 const IOVectorSortElem
*elem_a
= a
;
612 const IOVectorSortElem
*elem_b
= b
;
614 return elem_a
->src_index
- elem_b
->src_index
;
618 * Copy contents of I/O vector
620 * The relative relationships of overlapping iovecs are preserved. This is
621 * necessary to ensure identical semantics in the cloned I/O vector.
623 void qemu_iovec_clone(QEMUIOVector
*dest
, const QEMUIOVector
*src
, void *buf
)
625 IOVectorSortElem sortelems
[src
->niov
];
629 /* Sort by source iovecs by base address */
630 for (i
= 0; i
< src
->niov
; i
++) {
631 sortelems
[i
].src_index
= i
;
632 sortelems
[i
].src_iov
= &src
->iov
[i
];
634 qsort(sortelems
, src
->niov
, sizeof(sortelems
[0]), sortelem_cmp_src_base
);
636 /* Allocate buffer space taking into account overlapping iovecs */
638 for (i
= 0; i
< src
->niov
; i
++) {
639 struct iovec
*cur
= sortelems
[i
].src_iov
;
640 ptrdiff_t rewind
= 0;
643 if (last_end
&& last_end
> cur
->iov_base
) {
644 rewind
= last_end
- cur
->iov_base
;
647 sortelems
[i
].dest_base
= buf
- rewind
;
648 buf
+= cur
->iov_len
- MIN(rewind
, cur
->iov_len
);
649 last_end
= MAX(cur
->iov_base
+ cur
->iov_len
, last_end
);
652 /* Sort by source iovec index and build destination iovec */
653 qsort(sortelems
, src
->niov
, sizeof(sortelems
[0]), sortelem_cmp_src_index
);
654 for (i
= 0; i
< src
->niov
; i
++) {
655 qemu_iovec_add(dest
, sortelems
[i
].dest_base
, src
->iov
[i
].iov_len
);
659 void iov_discard_undo(IOVDiscardUndo
*undo
)
661 /* Restore original iovec if it was modified */
662 if (undo
->modified_iov
) {
663 *undo
->modified_iov
= undo
->orig
;
667 size_t iov_discard_front_undoable(struct iovec
**iov
,
668 unsigned int *iov_cnt
,
670 IOVDiscardUndo
*undo
)
676 undo
->modified_iov
= NULL
;
679 for (cur
= *iov
; *iov_cnt
> 0; cur
++) {
680 if (cur
->iov_len
> bytes
) {
682 undo
->modified_iov
= cur
;
686 cur
->iov_base
+= bytes
;
687 cur
->iov_len
-= bytes
;
692 bytes
-= cur
->iov_len
;
693 total
+= cur
->iov_len
;
701 size_t iov_discard_front(struct iovec
**iov
, unsigned int *iov_cnt
,
704 return iov_discard_front_undoable(iov
, iov_cnt
, bytes
, NULL
);
707 size_t iov_discard_back_undoable(struct iovec
*iov
,
708 unsigned int *iov_cnt
,
710 IOVDiscardUndo
*undo
)
716 undo
->modified_iov
= NULL
;
723 cur
= iov
+ (*iov_cnt
- 1);
725 while (*iov_cnt
> 0) {
726 if (cur
->iov_len
> bytes
) {
728 undo
->modified_iov
= cur
;
732 cur
->iov_len
-= bytes
;
737 bytes
-= cur
->iov_len
;
738 total
+= cur
->iov_len
;
746 size_t iov_discard_back(struct iovec
*iov
, unsigned int *iov_cnt
,
749 return iov_discard_back_undoable(iov
, iov_cnt
, bytes
, NULL
);
752 void qemu_iovec_discard_back(QEMUIOVector
*qiov
, size_t bytes
)
755 unsigned int niov
= qiov
->niov
;
757 assert(qiov
->size
>= bytes
);
758 total
= iov_discard_back(qiov
->iov
, &niov
, bytes
);
759 assert(total
== bytes
);