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.
23 # include <winsock2.h>
25 # include <sys/types.h>
26 # include <sys/socket.h>
29 size_t iov_from_buf(const struct iovec
*iov
, unsigned int iov_cnt
,
30 size_t offset
, const void *buf
, size_t bytes
)
34 for (i
= 0, done
= 0; (offset
|| done
< bytes
) && i
< iov_cnt
; i
++) {
35 if (offset
< iov
[i
].iov_len
) {
36 size_t len
= MIN(iov
[i
].iov_len
- offset
, bytes
- done
);
37 memcpy(iov
[i
].iov_base
+ offset
, buf
+ done
, len
);
41 offset
-= iov
[i
].iov_len
;
48 size_t iov_to_buf(const struct iovec
*iov
, const unsigned int iov_cnt
,
49 size_t offset
, void *buf
, size_t bytes
)
53 for (i
= 0, done
= 0; (offset
|| done
< bytes
) && i
< iov_cnt
; i
++) {
54 if (offset
< iov
[i
].iov_len
) {
55 size_t len
= MIN(iov
[i
].iov_len
- offset
, bytes
- done
);
56 memcpy(buf
+ done
, iov
[i
].iov_base
+ offset
, len
);
60 offset
-= iov
[i
].iov_len
;
67 size_t iov_memset(const struct iovec
*iov
, const unsigned int iov_cnt
,
68 size_t offset
, int fillc
, size_t bytes
)
72 for (i
= 0, done
= 0; (offset
|| done
< bytes
) && i
< iov_cnt
; i
++) {
73 if (offset
< iov
[i
].iov_len
) {
74 size_t len
= MIN(iov
[i
].iov_len
- offset
, bytes
- done
);
75 memset(iov
[i
].iov_base
+ offset
, fillc
, len
);
79 offset
-= iov
[i
].iov_len
;
86 size_t iov_size(const struct iovec
*iov
, const unsigned int iov_cnt
)
92 for (i
= 0; i
< iov_cnt
; i
++) {
93 len
+= iov
[i
].iov_len
;
98 /* helper function for iov_send_recv() */
100 do_send_recv(int sockfd
, struct iovec
*iov
, unsigned iov_cnt
, bool do_send
)
102 #if defined CONFIG_IOVEC && defined CONFIG_POSIX
105 memset(&msg
, 0, sizeof(msg
));
107 msg
.msg_iovlen
= iov_cnt
;
110 ? sendmsg(sockfd
, &msg
, 0)
111 : recvmsg(sockfd
, &msg
, 0);
112 } while (ret
< 0 && errno
== EINTR
);
115 /* else send piece-by-piece */
116 /*XXX Note: windows has WSASend() and WSARecv() */
119 while (i
< iov_cnt
) {
121 ? send(sockfd
, iov
[i
].iov_base
, iov
[i
].iov_len
, 0)
122 : recv(sockfd
, iov
[i
].iov_base
, iov
[i
].iov_len
, 0);
127 } else if (errno
== EINTR
) {
130 /* else it is some "other" error,
131 * only return if there was no data processed. */
143 ssize_t
iov_send_recv(int sockfd
, struct iovec
*iov
, unsigned iov_cnt
,
144 size_t offset
, size_t bytes
,
148 unsigned si
, ei
; /* start and end indexes */
150 /* Catch the do-nothing case early, as otherwise we will pass an
151 * empty iovec to sendmsg/recvmsg(), and not all implementations
157 /* Find the start position, skipping `offset' bytes:
158 * first, skip all full-sized vector elements, */
159 for (si
= 0; si
< iov_cnt
&& offset
>= iov
[si
].iov_len
; ++si
) {
160 offset
-= iov
[si
].iov_len
;
163 assert(si
< iov_cnt
);
164 /* second, skip `offset' bytes from the (now) first element,
166 iov
[si
].iov_base
+= offset
;
167 iov
[si
].iov_len
-= offset
;
169 /* Find the end position skipping `bytes' bytes: */
170 /* first, skip all full-sized elements */
171 for (ei
= si
; ei
< iov_cnt
&& iov
[ei
].iov_len
<= bytes
; ++ei
) {
172 bytes
-= iov
[ei
].iov_len
;
175 /* second, fixup the last element, and remember
176 * the length we've cut from the end of it in `bytes' */
178 assert(ei
< iov_cnt
);
179 assert(iov
[ei
].iov_len
> bytes
);
180 tail
= iov
[ei
].iov_len
- bytes
;
181 iov
[ei
].iov_len
= bytes
;
182 bytes
= tail
; /* bytes is now equal to the tail size */
186 ret
= do_send_recv(sockfd
, iov
+ si
, ei
- si
, do_send
);
188 /* Undo the changes above */
190 iov
[si
].iov_base
-= offset
;
191 iov
[si
].iov_len
+= offset
;
194 iov
[ei
-1].iov_len
+= bytes
;
201 void iov_hexdump(const struct iovec
*iov
, const unsigned int iov_cnt
,
202 FILE *fp
, const char *prefix
, size_t limit
)
204 unsigned int i
, v
, b
;
208 for (i
= 0, v
= 0, b
= 0; b
< limit
; i
++, b
++) {
209 if (i
== iov
[v
].iov_len
) {
217 fprintf(fp
, "%s: %04x:", prefix
, b
);
222 fprintf(fp
, " %02x", c
[i
]);
223 if ((b
% 16) == 15) {
234 void qemu_iovec_init(QEMUIOVector
*qiov
, int alloc_hint
)
236 qiov
->iov
= g_malloc(alloc_hint
* sizeof(struct iovec
));
238 qiov
->nalloc
= alloc_hint
;
242 void qemu_iovec_init_external(QEMUIOVector
*qiov
, struct iovec
*iov
, int niov
)
250 for (i
= 0; i
< niov
; i
++)
251 qiov
->size
+= iov
[i
].iov_len
;
254 void qemu_iovec_add(QEMUIOVector
*qiov
, void *base
, size_t len
)
256 assert(qiov
->nalloc
!= -1);
258 if (qiov
->niov
== qiov
->nalloc
) {
259 qiov
->nalloc
= 2 * qiov
->nalloc
+ 1;
260 qiov
->iov
= g_realloc(qiov
->iov
, qiov
->nalloc
* sizeof(struct iovec
));
262 qiov
->iov
[qiov
->niov
].iov_base
= base
;
263 qiov
->iov
[qiov
->niov
].iov_len
= len
;
269 * Concatenates (partial) iovecs from src to the end of dst.
270 * It starts copying after skipping `soffset' bytes at the
271 * beginning of src and adds individual vectors from src to
272 * dst copies up to `sbytes' bytes total, or up to the end
273 * of src if it comes first. This way, it is okay to specify
274 * very large value for `sbytes' to indicate "up to the end
276 * Only vector pointers are processed, not the actual data buffers.
278 void qemu_iovec_concat(QEMUIOVector
*dst
,
279 QEMUIOVector
*src
, size_t soffset
, size_t sbytes
)
283 struct iovec
*siov
= src
->iov
;
284 assert(dst
->nalloc
!= -1);
285 assert(src
->size
>= soffset
);
286 for (i
= 0, done
= 0; done
< sbytes
&& i
< src
->niov
; i
++) {
287 if (soffset
< siov
[i
].iov_len
) {
288 size_t len
= MIN(siov
[i
].iov_len
- soffset
, sbytes
- done
);
289 qemu_iovec_add(dst
, siov
[i
].iov_base
+ soffset
, len
);
293 soffset
-= siov
[i
].iov_len
;
299 void qemu_iovec_destroy(QEMUIOVector
*qiov
)
301 assert(qiov
->nalloc
!= -1);
303 qemu_iovec_reset(qiov
);
309 void qemu_iovec_reset(QEMUIOVector
*qiov
)
311 assert(qiov
->nalloc
!= -1);
317 size_t qemu_iovec_to_buf(QEMUIOVector
*qiov
, size_t offset
,
318 void *buf
, size_t bytes
)
320 return iov_to_buf(qiov
->iov
, qiov
->niov
, offset
, buf
, bytes
);
323 size_t qemu_iovec_from_buf(QEMUIOVector
*qiov
, size_t offset
,
324 const void *buf
, size_t bytes
)
326 return iov_from_buf(qiov
->iov
, qiov
->niov
, offset
, buf
, bytes
);
329 size_t qemu_iovec_memset(QEMUIOVector
*qiov
, size_t offset
,
330 int fillc
, size_t bytes
)
332 return iov_memset(qiov
->iov
, qiov
->niov
, offset
, fillc
, bytes
);