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.
6 * Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates
9 * Anthony Liguori <aliguori@us.ibm.com>
10 * Amit Shah <amit.shah@redhat.com>
11 * Michael Tokarev <mjt@tls.msk.ru>
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
16 * Contributions after 2012-01-13 are licensed under the terms of the
17 * GNU GPL, version 2 or (at your option) any later version.
20 #include "qemu/osdep.h"
22 #include "qemu/sockets.h"
23 #include "qemu/cutils.h"
25 size_t iov_from_buf_full(const struct iovec
*iov
, unsigned int iov_cnt
,
26 size_t offset
, const void *buf
, size_t bytes
)
30 for (i
= 0, done
= 0; (offset
|| done
< bytes
) && i
< iov_cnt
; i
++) {
31 if (offset
< iov
[i
].iov_len
) {
32 size_t len
= MIN(iov
[i
].iov_len
- offset
, bytes
- done
);
33 memcpy(iov
[i
].iov_base
+ offset
, buf
+ done
, len
);
37 offset
-= iov
[i
].iov_len
;
44 size_t iov_to_buf_full(const struct iovec
*iov
, const unsigned int iov_cnt
,
45 size_t offset
, void *buf
, size_t bytes
)
49 for (i
= 0, done
= 0; (offset
|| done
< bytes
) && i
< iov_cnt
; i
++) {
50 if (offset
< iov
[i
].iov_len
) {
51 size_t len
= MIN(iov
[i
].iov_len
- offset
, bytes
- done
);
52 memcpy(buf
+ done
, iov
[i
].iov_base
+ offset
, len
);
56 offset
-= iov
[i
].iov_len
;
63 size_t iov_memset(const struct iovec
*iov
, const unsigned int iov_cnt
,
64 size_t offset
, int fillc
, size_t bytes
)
68 for (i
= 0, done
= 0; (offset
|| done
< bytes
) && i
< iov_cnt
; i
++) {
69 if (offset
< iov
[i
].iov_len
) {
70 size_t len
= MIN(iov
[i
].iov_len
- offset
, bytes
- done
);
71 memset(iov
[i
].iov_base
+ offset
, fillc
, len
);
75 offset
-= iov
[i
].iov_len
;
82 size_t iov_size(const struct iovec
*iov
, const unsigned int iov_cnt
)
88 for (i
= 0; i
< iov_cnt
; i
++) {
89 len
+= iov
[i
].iov_len
;
94 /* helper function for iov_send_recv() */
96 do_send_recv(int sockfd
, int flags
, struct iovec
*iov
, unsigned iov_cnt
,
102 memset(&msg
, 0, sizeof(msg
));
104 msg
.msg_iovlen
= iov_cnt
;
107 ? sendmsg(sockfd
, &msg
, flags
)
108 : recvmsg(sockfd
, &msg
, flags
);
109 } while (ret
< 0 && errno
== EINTR
);
112 /* else send piece-by-piece */
113 /*XXX Note: windows has WSASend() and WSARecv() */
117 while (i
< iov_cnt
) {
119 ? send(sockfd
, iov
[i
].iov_base
+ off
, iov
[i
].iov_len
- off
, flags
)
120 : recv(sockfd
, iov
[i
].iov_base
+ off
, iov
[i
].iov_len
- off
, flags
);
124 if (off
< iov
[i
].iov_len
) {
129 } else if (errno
== EINTR
) {
132 /* else it is some "other" error,
133 * only return if there was no data processed. */
146 ssize_t
iov_send_recv(int sockfd
, const struct iovec
*_iov
, unsigned iov_cnt
,
147 size_t offset
, size_t bytes
,
150 return iov_send_recv_with_flags(sockfd
, 0, _iov
, iov_cnt
, offset
, bytes
,
154 ssize_t
iov_send_recv_with_flags(int sockfd
, int sockflags
,
155 const struct iovec
*_iov
,
156 unsigned iov_cnt
, size_t offset
,
157 size_t bytes
, bool do_send
)
161 size_t orig_len
, tail
;
163 struct iovec
*local_iov
, *iov
;
169 local_iov
= g_new0(struct iovec
, iov_cnt
);
170 iov_copy(local_iov
, iov_cnt
, _iov
, iov_cnt
, offset
, bytes
);
175 /* Find the start position, skipping `offset' bytes:
176 * first, skip all full-sized vector elements, */
177 for (niov
= 0; niov
< iov_cnt
&& offset
>= iov
[niov
].iov_len
; ++niov
) {
178 offset
-= iov
[niov
].iov_len
;
181 /* niov == iov_cnt would only be valid if bytes == 0, which
182 * we already ruled out in the loop condition. */
183 assert(niov
< iov_cnt
);
188 /* second, skip `offset' bytes from the (now) first element,
190 iov
[0].iov_base
+= offset
;
191 iov
[0].iov_len
-= offset
;
193 /* Find the end position skipping `bytes' bytes: */
194 /* first, skip all full-sized elements */
196 for (niov
= 0; niov
< iov_cnt
&& iov
[niov
].iov_len
<= tail
; ++niov
) {
197 tail
-= iov
[niov
].iov_len
;
200 /* second, fixup the last element, and remember the original
202 assert(niov
< iov_cnt
);
203 assert(iov
[niov
].iov_len
> tail
);
204 orig_len
= iov
[niov
].iov_len
;
205 iov
[niov
++].iov_len
= tail
;
206 ret
= do_send_recv(sockfd
, sockflags
, iov
, niov
, do_send
);
207 /* Undo the changes above before checking for errors */
208 iov
[niov
-1].iov_len
= orig_len
;
210 ret
= do_send_recv(sockfd
, sockflags
, iov
, niov
, do_send
);
213 iov
[0].iov_base
-= offset
;
214 iov
[0].iov_len
+= offset
;
218 assert(errno
!= EINTR
);
220 if (errno
== EAGAIN
&& total
> 0) {
226 if (ret
== 0 && !do_send
) {
227 /* recv returns 0 when the peer has performed an orderly
232 /* Prepare for the next iteration */
243 void iov_hexdump(const struct iovec
*iov
, const unsigned int iov_cnt
,
244 FILE *fp
, const char *prefix
, size_t limit
)
250 for (v
= 0; v
< iov_cnt
; v
++) {
251 size
+= iov
[v
].iov_len
;
253 size
= size
> limit
? limit
: size
;
254 buf
= g_malloc(size
);
255 iov_to_buf(iov
, iov_cnt
, 0, buf
, size
);
256 qemu_hexdump(fp
, prefix
, buf
, size
);
260 unsigned iov_copy(struct iovec
*dst_iov
, unsigned int dst_iov_cnt
,
261 const struct iovec
*iov
, unsigned int iov_cnt
,
262 size_t offset
, size_t bytes
)
267 i
< iov_cnt
&& j
< dst_iov_cnt
&& (offset
|| bytes
); i
++) {
268 if (offset
>= iov
[i
].iov_len
) {
269 offset
-= iov
[i
].iov_len
;
272 len
= MIN(bytes
, iov
[i
].iov_len
- offset
);
274 dst_iov
[j
].iov_base
= iov
[i
].iov_base
+ offset
;
275 dst_iov
[j
].iov_len
= len
;
286 void qemu_iovec_init(QEMUIOVector
*qiov
, int alloc_hint
)
288 qiov
->iov
= g_new(struct iovec
, alloc_hint
);
290 qiov
->nalloc
= alloc_hint
;
294 void qemu_iovec_init_external(QEMUIOVector
*qiov
, struct iovec
*iov
, int niov
)
302 for (i
= 0; i
< niov
; i
++)
303 qiov
->size
+= iov
[i
].iov_len
;
306 void qemu_iovec_add(QEMUIOVector
*qiov
, void *base
, size_t len
)
308 assert(qiov
->nalloc
!= -1);
310 if (qiov
->niov
== qiov
->nalloc
) {
311 qiov
->nalloc
= 2 * qiov
->nalloc
+ 1;
312 qiov
->iov
= g_renew(struct iovec
, qiov
->iov
, qiov
->nalloc
);
314 qiov
->iov
[qiov
->niov
].iov_base
= base
;
315 qiov
->iov
[qiov
->niov
].iov_len
= len
;
321 * Concatenates (partial) iovecs from src_iov to the end of dst.
322 * It starts copying after skipping `soffset' bytes at the
323 * beginning of src and adds individual vectors from src to
324 * dst copies up to `sbytes' bytes total, or up to the end
325 * of src_iov if it comes first. This way, it is okay to specify
326 * very large value for `sbytes' to indicate "up to the end
328 * Only vector pointers are processed, not the actual data buffers.
330 size_t qemu_iovec_concat_iov(QEMUIOVector
*dst
,
331 struct iovec
*src_iov
, unsigned int src_cnt
,
332 size_t soffset
, size_t sbytes
)
340 assert(dst
->nalloc
!= -1);
341 for (i
= 0, done
= 0; done
< sbytes
&& i
< src_cnt
; i
++) {
342 if (soffset
< src_iov
[i
].iov_len
) {
343 size_t len
= MIN(src_iov
[i
].iov_len
- soffset
, sbytes
- done
);
344 qemu_iovec_add(dst
, src_iov
[i
].iov_base
+ soffset
, len
);
348 soffset
-= src_iov
[i
].iov_len
;
351 assert(soffset
== 0); /* offset beyond end of src */
357 * Concatenates (partial) iovecs from src to the end of dst.
358 * It starts copying after skipping `soffset' bytes at the
359 * beginning of src and adds individual vectors from src to
360 * dst copies up to `sbytes' bytes total, or up to the end
361 * of src if it comes first. This way, it is okay to specify
362 * very large value for `sbytes' to indicate "up to the end
364 * Only vector pointers are processed, not the actual data buffers.
366 void qemu_iovec_concat(QEMUIOVector
*dst
,
367 QEMUIOVector
*src
, size_t soffset
, size_t sbytes
)
369 qemu_iovec_concat_iov(dst
, src
->iov
, src
->niov
, soffset
, sbytes
);
375 * Return pointer to iovec structure, where byte at @offset in original vector
377 * Set @remaining_offset to be offset inside that iovec to the same byte.
379 static struct iovec
*iov_skip_offset(struct iovec
*iov
, size_t offset
,
380 size_t *remaining_offset
)
382 while (offset
> 0 && offset
>= iov
->iov_len
) {
383 offset
-= iov
->iov_len
;
386 *remaining_offset
= offset
;
394 * Find subarray of iovec's, containing requested range. @head would
395 * be offset in first iov (returned by the function), @tail would be
396 * count of extra bytes in last iovec (returned iov + @niov - 1).
398 struct iovec
*qemu_iovec_slice(QEMUIOVector
*qiov
,
399 size_t offset
, size_t len
,
400 size_t *head
, size_t *tail
, int *niov
)
402 struct iovec
*iov
, *end_iov
;
404 assert(offset
+ len
<= qiov
->size
);
406 iov
= iov_skip_offset(qiov
->iov
, offset
, head
);
407 end_iov
= iov_skip_offset(iov
, *head
+ len
, tail
);
410 assert(*tail
< end_iov
->iov_len
);
411 *tail
= end_iov
->iov_len
- *tail
;
415 *niov
= end_iov
- iov
;
420 int qemu_iovec_subvec_niov(QEMUIOVector
*qiov
, size_t offset
, size_t len
)
425 qemu_iovec_slice(qiov
, offset
, len
, &head
, &tail
, &niov
);
431 * Check if the contents of subrange of qiov data is all zeroes.
433 bool qemu_iovec_is_zero(QEMUIOVector
*qiov
, size_t offset
, size_t bytes
)
436 size_t current_offset
;
438 assert(offset
+ bytes
<= qiov
->size
);
440 iov
= iov_skip_offset(qiov
->iov
, offset
, ¤t_offset
);
443 uint8_t *base
= (uint8_t *)iov
->iov_base
+ current_offset
;
444 size_t len
= MIN(iov
->iov_len
- current_offset
, bytes
);
446 if (!buffer_is_zero(base
, len
)) {
458 void qemu_iovec_init_slice(QEMUIOVector
*qiov
, QEMUIOVector
*source
,
459 size_t offset
, size_t len
)
461 struct iovec
*slice_iov
;
463 size_t slice_head
, slice_tail
;
465 assert(source
->size
>= len
);
466 assert(source
->size
- len
>= offset
);
468 slice_iov
= qemu_iovec_slice(source
, offset
, len
,
469 &slice_head
, &slice_tail
, &slice_niov
);
470 if (slice_niov
== 1) {
471 qemu_iovec_init_buf(qiov
, slice_iov
[0].iov_base
+ slice_head
, len
);
473 qemu_iovec_init(qiov
, slice_niov
);
474 qemu_iovec_concat_iov(qiov
, slice_iov
, slice_niov
, slice_head
, len
);
478 void qemu_iovec_destroy(QEMUIOVector
*qiov
)
480 if (qiov
->nalloc
!= -1) {
484 memset(qiov
, 0, sizeof(*qiov
));
487 void qemu_iovec_reset(QEMUIOVector
*qiov
)
489 assert(qiov
->nalloc
!= -1);
495 size_t qemu_iovec_to_buf(QEMUIOVector
*qiov
, size_t offset
,
496 void *buf
, size_t bytes
)
498 return iov_to_buf(qiov
->iov
, qiov
->niov
, offset
, buf
, bytes
);
501 size_t qemu_iovec_from_buf(QEMUIOVector
*qiov
, size_t offset
,
502 const void *buf
, size_t bytes
)
504 return iov_from_buf(qiov
->iov
, qiov
->niov
, offset
, buf
, bytes
);
507 size_t qemu_iovec_memset(QEMUIOVector
*qiov
, size_t offset
,
508 int fillc
, size_t bytes
)
510 return iov_memset(qiov
->iov
, qiov
->niov
, offset
, fillc
, bytes
);
514 * Check that I/O vector contents are identical
516 * The IO vectors must have the same structure (same length of all parts).
517 * A typical usage is to compare vectors created with qemu_iovec_clone().
521 * @ret: Offset to first mismatching byte or -1 if match
523 ssize_t
qemu_iovec_compare(QEMUIOVector
*a
, QEMUIOVector
*b
)
528 assert(a
->niov
== b
->niov
);
529 for (i
= 0; i
< a
->niov
; i
++) {
531 uint8_t *p
= (uint8_t *)a
->iov
[i
].iov_base
;
532 uint8_t *q
= (uint8_t *)b
->iov
[i
].iov_base
;
534 assert(a
->iov
[i
].iov_len
== b
->iov
[i
].iov_len
);
535 while (len
< a
->iov
[i
].iov_len
&& *p
++ == *q
++) {
541 if (len
!= a
->iov
[i
].iov_len
) {
550 struct iovec
*src_iov
;
554 static int sortelem_cmp_src_base(const void *a
, const void *b
)
556 const IOVectorSortElem
*elem_a
= a
;
557 const IOVectorSortElem
*elem_b
= b
;
560 if (elem_a
->src_iov
->iov_base
< elem_b
->src_iov
->iov_base
) {
562 } else if (elem_a
->src_iov
->iov_base
> elem_b
->src_iov
->iov_base
) {
569 static int sortelem_cmp_src_index(const void *a
, const void *b
)
571 const IOVectorSortElem
*elem_a
= a
;
572 const IOVectorSortElem
*elem_b
= b
;
574 return elem_a
->src_index
- elem_b
->src_index
;
578 * Copy contents of I/O vector
580 * The relative relationships of overlapping iovecs are preserved. This is
581 * necessary to ensure identical semantics in the cloned I/O vector.
583 void qemu_iovec_clone(QEMUIOVector
*dest
, const QEMUIOVector
*src
, void *buf
)
585 g_autofree IOVectorSortElem
*sortelems
= g_new(IOVectorSortElem
, src
->niov
);
589 /* Sort by source iovecs by base address */
590 for (i
= 0; i
< src
->niov
; i
++) {
591 sortelems
[i
].src_index
= i
;
592 sortelems
[i
].src_iov
= &src
->iov
[i
];
594 qsort(sortelems
, src
->niov
, sizeof(sortelems
[0]), sortelem_cmp_src_base
);
596 /* Allocate buffer space taking into account overlapping iovecs */
598 for (i
= 0; i
< src
->niov
; i
++) {
599 struct iovec
*cur
= sortelems
[i
].src_iov
;
600 ptrdiff_t rewind
= 0;
603 if (last_end
&& last_end
> cur
->iov_base
) {
604 rewind
= last_end
- cur
->iov_base
;
607 sortelems
[i
].dest_base
= buf
- rewind
;
608 buf
+= cur
->iov_len
- MIN(rewind
, cur
->iov_len
);
609 last_end
= MAX(cur
->iov_base
+ cur
->iov_len
, last_end
);
612 /* Sort by source iovec index and build destination iovec */
613 qsort(sortelems
, src
->niov
, sizeof(sortelems
[0]), sortelem_cmp_src_index
);
614 for (i
= 0; i
< src
->niov
; i
++) {
615 qemu_iovec_add(dest
, sortelems
[i
].dest_base
, src
->iov
[i
].iov_len
);
619 void iov_discard_undo(IOVDiscardUndo
*undo
)
621 /* Restore original iovec if it was modified */
622 if (undo
->modified_iov
) {
623 *undo
->modified_iov
= undo
->orig
;
627 size_t iov_discard_front_undoable(struct iovec
**iov
,
628 unsigned int *iov_cnt
,
630 IOVDiscardUndo
*undo
)
636 undo
->modified_iov
= NULL
;
639 for (cur
= *iov
; *iov_cnt
> 0; cur
++) {
640 if (cur
->iov_len
> bytes
) {
642 undo
->modified_iov
= cur
;
646 cur
->iov_base
+= bytes
;
647 cur
->iov_len
-= bytes
;
652 bytes
-= cur
->iov_len
;
653 total
+= cur
->iov_len
;
661 size_t iov_discard_front(struct iovec
**iov
, unsigned int *iov_cnt
,
664 return iov_discard_front_undoable(iov
, iov_cnt
, bytes
, NULL
);
667 size_t iov_discard_back_undoable(struct iovec
*iov
,
668 unsigned int *iov_cnt
,
670 IOVDiscardUndo
*undo
)
676 undo
->modified_iov
= NULL
;
683 cur
= iov
+ (*iov_cnt
- 1);
685 while (*iov_cnt
> 0) {
686 if (cur
->iov_len
> bytes
) {
688 undo
->modified_iov
= cur
;
692 cur
->iov_len
-= bytes
;
697 bytes
-= cur
->iov_len
;
698 total
+= cur
->iov_len
;
706 size_t iov_discard_back(struct iovec
*iov
, unsigned int *iov_cnt
,
709 return iov_discard_back_undoable(iov
, iov_cnt
, bytes
, NULL
);
712 void qemu_iovec_discard_back(QEMUIOVector
*qiov
, size_t bytes
)
715 unsigned int niov
= qiov
->niov
;
717 assert(qiov
->size
>= bytes
);
718 total
= iov_discard_back(qiov
->iov
, &niov
, bytes
);
719 assert(total
== bytes
);