]> git.proxmox.com Git - qemu.git/blame - util/iov.c
iov: Factor out hexdumper
[qemu.git] / util / iov.c
CommitLineData
e4d5639d
AS
1/*
2 * Helpers for getting linearized buffers from iov / filling buffers into iovs
3 *
4 * Copyright IBM, Corp. 2007, 2008
5 * Copyright (C) 2010 Red Hat, Inc.
6 *
7 * Author(s):
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Amit Shah <amit.shah@redhat.com>
2278a69e 10 * Michael Tokarev <mjt@tls.msk.ru>
e4d5639d
AS
11 *
12 * This work is licensed under the terms of the GNU GPL, version 2. See
13 * the COPYING file in the top-level directory.
6b620ca3
PB
14 *
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.
e4d5639d
AS
17 */
18
1de7afc9 19#include "qemu/iov.h"
e4d5639d 20
25e5e4c7
MT
21#ifdef _WIN32
22# include <windows.h>
23# include <winsock2.h>
24#else
25# include <sys/types.h>
26# include <sys/socket.h>
27#endif
28
844b5cea 29size_t iov_from_buf(const struct iovec *iov, unsigned int iov_cnt,
2278a69e 30 size_t offset, const void *buf, size_t bytes)
e4d5639d 31{
2278a69e 32 size_t done;
e4d5639d 33 unsigned int i;
2278a69e
MT
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);
38 done += len;
39 offset = 0;
40 } else {
41 offset -= iov[i].iov_len;
348e7b8d 42 }
e4d5639d 43 }
2278a69e
MT
44 assert(offset == 0);
45 return done;
e4d5639d 46}
fa6111f2 47
2278a69e
MT
48size_t iov_to_buf(const struct iovec *iov, const unsigned int iov_cnt,
49 size_t offset, void *buf, size_t bytes)
fa6111f2 50{
2278a69e 51 size_t done;
fa6111f2 52 unsigned int i;
2278a69e
MT
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);
57 done += len;
58 offset = 0;
59 } else {
60 offset -= iov[i].iov_len;
fa6111f2 61 }
8d15028e 62 }
2278a69e
MT
63 assert(offset == 0);
64 return done;
8d15028e
GH
65}
66
dcf6f5e1 67size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt,
2278a69e 68 size_t offset, int fillc, size_t bytes)
8d15028e 69{
2278a69e 70 size_t done;
8d15028e 71 unsigned int i;
2278a69e
MT
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);
76 done += len;
77 offset = 0;
78 } else {
79 offset -= iov[i].iov_len;
8d15028e 80 }
fa6111f2 81 }
2278a69e
MT
82 assert(offset == 0);
83 return done;
fa6111f2
AS
84}
85
348e7b8d 86size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt)
fa6111f2
AS
87{
88 size_t len;
89 unsigned int i;
90
91 len = 0;
348e7b8d 92 for (i = 0; i < iov_cnt; i++) {
fa6111f2
AS
93 len += iov[i].iov_len;
94 }
95 return len;
96}
3a1dca94 97
25e5e4c7
MT
98/* helper function for iov_send_recv() */
99static ssize_t
100do_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt, bool do_send)
101{
102#if defined CONFIG_IOVEC && defined CONFIG_POSIX
103 ssize_t ret;
104 struct msghdr msg;
105 memset(&msg, 0, sizeof(msg));
106 msg.msg_iov = iov;
107 msg.msg_iovlen = iov_cnt;
108 do {
109 ret = do_send
110 ? sendmsg(sockfd, &msg, 0)
111 : recvmsg(sockfd, &msg, 0);
112 } while (ret < 0 && errno == EINTR);
113 return ret;
114#else
115 /* else send piece-by-piece */
116 /*XXX Note: windows has WSASend() and WSARecv() */
c0958559
SW
117 unsigned i = 0;
118 ssize_t ret = 0;
119 while (i < iov_cnt) {
25e5e4c7
MT
120 ssize_t r = do_send
121 ? send(sockfd, iov[i].iov_base, iov[i].iov_len, 0)
122 : recv(sockfd, iov[i].iov_base, iov[i].iov_len, 0);
123 if (r > 0) {
124 ret += r;
125 } else if (!r) {
126 break;
127 } else if (errno == EINTR) {
128 continue;
129 } else {
130 /* else it is some "other" error,
131 * only return if there was no data processed. */
132 if (ret == 0) {
c0958559 133 ret = -1;
25e5e4c7
MT
134 }
135 break;
136 }
c0958559 137 i++;
25e5e4c7 138 }
c0958559 139 return ret;
25e5e4c7
MT
140#endif
141}
142
143ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt,
144 size_t offset, size_t bytes,
145 bool do_send)
146{
147 ssize_t ret;
148 unsigned si, ei; /* start and end indexes */
bb955867
PM
149 if (bytes == 0) {
150 /* Catch the do-nothing case early, as otherwise we will pass an
151 * empty iovec to sendmsg/recvmsg(), and not all implementations
152 * accept this.
153 */
154 return 0;
155 }
25e5e4c7
MT
156
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;
161 }
162 if (offset) {
163 assert(si < iov_cnt);
164 /* second, skip `offset' bytes from the (now) first element,
165 * undo it on exit */
166 iov[si].iov_base += offset;
167 iov[si].iov_len -= offset;
168 }
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;
173 }
174 if (bytes) {
175 /* second, fixup the last element, and remember
176 * the length we've cut from the end of it in `bytes' */
177 size_t tail;
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 */
183 ++ei;
184 }
185
186 ret = do_send_recv(sockfd, iov + si, ei - si, do_send);
187
188 /* Undo the changes above */
189 if (offset) {
190 iov[si].iov_base -= offset;
191 iov[si].iov_len += offset;
192 }
193 if (bytes) {
194 iov[ei-1].iov_len += bytes;
195 }
196
197 return ret;
198}
199
200
3a1dca94
GH
201void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
202 FILE *fp, const char *prefix, size_t limit)
203{
6ff66f50
PC
204 int v;
205 size_t size = 0;
206 char *buf;
207
208 for (v = 0; v < iov_cnt; v++) {
209 size += iov[v].iov_len;
3a1dca94 210 }
6ff66f50
PC
211 size = size > limit ? limit : size;
212 buf = g_malloc(size);
213 iov_to_buf(iov, iov_cnt, 0, buf, size);
214 hexdump(buf, fp, prefix, size);
215 g_free(buf);
3a1dca94 216}
0191253c 217
d336336c
MT
218unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt,
219 const struct iovec *iov, unsigned int iov_cnt,
220 size_t offset, size_t bytes)
221{
222 size_t len;
223 unsigned int i, j;
224 for (i = 0, j = 0; i < iov_cnt && j < dst_iov_cnt && bytes; i++) {
225 if (offset >= iov[i].iov_len) {
226 offset -= iov[i].iov_len;
227 continue;
228 }
229 len = MIN(bytes, iov[i].iov_len - offset);
230
231 dst_iov[j].iov_base = iov[i].iov_base + offset;
232 dst_iov[j].iov_len = len;
233 j++;
234 bytes -= len;
235 offset = 0;
236 }
237 assert(offset == 0);
238 return j;
239}
f563a5d7 240
0191253c
PB
241/* io vectors */
242
243void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint)
244{
245 qiov->iov = g_malloc(alloc_hint * sizeof(struct iovec));
246 qiov->niov = 0;
247 qiov->nalloc = alloc_hint;
248 qiov->size = 0;
249}
250
251void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov)
252{
253 int i;
254
255 qiov->iov = iov;
256 qiov->niov = niov;
257 qiov->nalloc = -1;
258 qiov->size = 0;
259 for (i = 0; i < niov; i++)
260 qiov->size += iov[i].iov_len;
261}
262
263void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len)
264{
265 assert(qiov->nalloc != -1);
266
267 if (qiov->niov == qiov->nalloc) {
268 qiov->nalloc = 2 * qiov->nalloc + 1;
269 qiov->iov = g_realloc(qiov->iov, qiov->nalloc * sizeof(struct iovec));
270 }
271 qiov->iov[qiov->niov].iov_base = base;
272 qiov->iov[qiov->niov].iov_len = len;
273 qiov->size += len;
274 ++qiov->niov;
275}
276
277/*
530c0bbd 278 * Concatenates (partial) iovecs from src_iov to the end of dst.
0191253c
PB
279 * It starts copying after skipping `soffset' bytes at the
280 * beginning of src and adds individual vectors from src to
281 * dst copies up to `sbytes' bytes total, or up to the end
530c0bbd 282 * of src_iov if it comes first. This way, it is okay to specify
0191253c
PB
283 * very large value for `sbytes' to indicate "up to the end
284 * of src".
285 * Only vector pointers are processed, not the actual data buffers.
286 */
530c0bbd
SH
287void qemu_iovec_concat_iov(QEMUIOVector *dst,
288 struct iovec *src_iov, unsigned int src_cnt,
289 size_t soffset, size_t sbytes)
0191253c
PB
290{
291 int i;
292 size_t done;
facf98ad
AK
293
294 if (!sbytes) {
295 return;
296 }
0191253c 297 assert(dst->nalloc != -1);
530c0bbd
SH
298 for (i = 0, done = 0; done < sbytes && i < src_cnt; i++) {
299 if (soffset < src_iov[i].iov_len) {
300 size_t len = MIN(src_iov[i].iov_len - soffset, sbytes - done);
301 qemu_iovec_add(dst, src_iov[i].iov_base + soffset, len);
0191253c
PB
302 done += len;
303 soffset = 0;
304 } else {
530c0bbd 305 soffset -= src_iov[i].iov_len;
0191253c
PB
306 }
307 }
530c0bbd
SH
308 assert(soffset == 0); /* offset beyond end of src */
309}
310
311/*
312 * Concatenates (partial) iovecs from src to the end of dst.
313 * It starts copying after skipping `soffset' bytes at the
314 * beginning of src and adds individual vectors from src to
315 * dst copies up to `sbytes' bytes total, or up to the end
316 * of src if it comes first. This way, it is okay to specify
317 * very large value for `sbytes' to indicate "up to the end
318 * of src".
319 * Only vector pointers are processed, not the actual data buffers.
320 */
321void qemu_iovec_concat(QEMUIOVector *dst,
322 QEMUIOVector *src, size_t soffset, size_t sbytes)
323{
324 qemu_iovec_concat_iov(dst, src->iov, src->niov, soffset, sbytes);
0191253c
PB
325}
326
327void qemu_iovec_destroy(QEMUIOVector *qiov)
328{
329 assert(qiov->nalloc != -1);
330
331 qemu_iovec_reset(qiov);
332 g_free(qiov->iov);
333 qiov->nalloc = 0;
334 qiov->iov = NULL;
335}
336
337void qemu_iovec_reset(QEMUIOVector *qiov)
338{
339 assert(qiov->nalloc != -1);
340
341 qiov->niov = 0;
342 qiov->size = 0;
343}
344
345size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
346 void *buf, size_t bytes)
347{
348 return iov_to_buf(qiov->iov, qiov->niov, offset, buf, bytes);
349}
350
351size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
352 const void *buf, size_t bytes)
353{
354 return iov_from_buf(qiov->iov, qiov->niov, offset, buf, bytes);
355}
356
357size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
358 int fillc, size_t bytes)
359{
360 return iov_memset(qiov->iov, qiov->niov, offset, fillc, bytes);
361}
d0277635
SH
362
363size_t iov_discard_front(struct iovec **iov, unsigned int *iov_cnt,
364 size_t bytes)
365{
366 size_t total = 0;
367 struct iovec *cur;
368
369 for (cur = *iov; *iov_cnt > 0; cur++) {
370 if (cur->iov_len > bytes) {
371 cur->iov_base += bytes;
372 cur->iov_len -= bytes;
373 total += bytes;
374 break;
375 }
376
377 bytes -= cur->iov_len;
378 total += cur->iov_len;
379 *iov_cnt -= 1;
380 }
381
382 *iov = cur;
383 return total;
384}
385
386size_t iov_discard_back(struct iovec *iov, unsigned int *iov_cnt,
387 size_t bytes)
388{
389 size_t total = 0;
390 struct iovec *cur;
391
392 if (*iov_cnt == 0) {
393 return 0;
394 }
395
396 cur = iov + (*iov_cnt - 1);
397
398 while (*iov_cnt > 0) {
399 if (cur->iov_len > bytes) {
400 cur->iov_len -= bytes;
401 total += bytes;
402 break;
403 }
404
405 bytes -= cur->iov_len;
406 total += cur->iov_len;
407 cur--;
408 *iov_cnt -= 1;
409 }
410
411 return total;
412}