]> Git Repo - qemu.git/blame - util/iov.c
Replaced get_tick_per_sec() by NANOSECONDS_PER_SECOND
[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 <[email protected]>
9 * Amit Shah <[email protected]>
2278a69e 10 * Michael Tokarev <[email protected]>
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
aafd7584 19#include "qemu/osdep.h"
daf015ef 20#include "qemu-common.h"
1de7afc9 21#include "qemu/iov.h"
cc99c6f5 22#include "qemu/sockets.h"
25e5e4c7 23
ad523bca
PB
24size_t iov_from_buf_full(const struct iovec *iov, unsigned int iov_cnt,
25 size_t offset, const void *buf, size_t bytes)
e4d5639d 26{
2278a69e 27 size_t done;
e4d5639d 28 unsigned int i;
2278a69e
MT
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);
33 done += len;
34 offset = 0;
35 } else {
36 offset -= iov[i].iov_len;
348e7b8d 37 }
e4d5639d 38 }
2278a69e
MT
39 assert(offset == 0);
40 return done;
e4d5639d 41}
fa6111f2 42
ad523bca
PB
43size_t iov_to_buf_full(const struct iovec *iov, const unsigned int iov_cnt,
44 size_t offset, void *buf, size_t bytes)
fa6111f2 45{
2278a69e 46 size_t done;
fa6111f2 47 unsigned int i;
2278a69e
MT
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);
52 done += len;
53 offset = 0;
54 } else {
55 offset -= iov[i].iov_len;
fa6111f2 56 }
8d15028e 57 }
2278a69e
MT
58 assert(offset == 0);
59 return done;
8d15028e
GH
60}
61
dcf6f5e1 62size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt,
2278a69e 63 size_t offset, int fillc, size_t bytes)
8d15028e 64{
2278a69e 65 size_t done;
8d15028e 66 unsigned int i;
2278a69e
MT
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);
71 done += len;
72 offset = 0;
73 } else {
74 offset -= iov[i].iov_len;
8d15028e 75 }
fa6111f2 76 }
2278a69e
MT
77 assert(offset == 0);
78 return done;
fa6111f2
AS
79}
80
348e7b8d 81size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt)
fa6111f2
AS
82{
83 size_t len;
84 unsigned int i;
85
86 len = 0;
348e7b8d 87 for (i = 0; i < iov_cnt; i++) {
fa6111f2
AS
88 len += iov[i].iov_len;
89 }
90 return len;
91}
3a1dca94 92
25e5e4c7
MT
93/* helper function for iov_send_recv() */
94static ssize_t
95do_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt, bool do_send)
96{
9adea5f7 97#ifdef CONFIG_POSIX
25e5e4c7
MT
98 ssize_t ret;
99 struct msghdr msg;
100 memset(&msg, 0, sizeof(msg));
101 msg.msg_iov = iov;
102 msg.msg_iovlen = iov_cnt;
103 do {
104 ret = do_send
105 ? sendmsg(sockfd, &msg, 0)
106 : recvmsg(sockfd, &msg, 0);
107 } while (ret < 0 && errno == EINTR);
108 return ret;
109#else
110 /* else send piece-by-piece */
111 /*XXX Note: windows has WSASend() and WSARecv() */
c0958559
SW
112 unsigned i = 0;
113 ssize_t ret = 0;
114 while (i < iov_cnt) {
25e5e4c7
MT
115 ssize_t r = do_send
116 ? send(sockfd, iov[i].iov_base, iov[i].iov_len, 0)
117 : recv(sockfd, iov[i].iov_base, iov[i].iov_len, 0);
118 if (r > 0) {
119 ret += r;
120 } else if (!r) {
121 break;
122 } else if (errno == EINTR) {
123 continue;
124 } else {
125 /* else it is some "other" error,
126 * only return if there was no data processed. */
127 if (ret == 0) {
c0958559 128 ret = -1;
25e5e4c7
MT
129 }
130 break;
131 }
c0958559 132 i++;
25e5e4c7 133 }
c0958559 134 return ret;
25e5e4c7
MT
135#endif
136}
137
6b64640d 138ssize_t iov_send_recv(int sockfd, const struct iovec *_iov, unsigned iov_cnt,
25e5e4c7
MT
139 size_t offset, size_t bytes,
140 bool do_send)
141{
83f75c26 142 ssize_t total = 0;
25e5e4c7 143 ssize_t ret;
5209d675 144 size_t orig_len, tail;
f48869ad 145 unsigned niov;
6b64640d
WC
146 struct iovec *local_iov, *iov;
147
148 if (bytes <= 0) {
149 return 0;
150 }
151
152 local_iov = g_new0(struct iovec, iov_cnt);
153 iov_copy(local_iov, iov_cnt, _iov, iov_cnt, offset, bytes);
154 offset = 0;
155 iov = local_iov;
5209d675 156
83f75c26
PB
157 while (bytes > 0) {
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;
162 }
cb6247a7 163
83f75c26
PB
164 /* niov == iov_cnt would only be valid if bytes == 0, which
165 * we already ruled out in the loop condition. */
f48869ad 166 assert(niov < iov_cnt);
83f75c26
PB
167 iov += niov;
168 iov_cnt -= niov;
169
170 if (offset) {
171 /* second, skip `offset' bytes from the (now) first element,
172 * undo it on exit */
173 iov[0].iov_base += offset;
174 iov[0].iov_len -= offset;
175 }
176 /* Find the end position skipping `bytes' bytes: */
177 /* first, skip all full-sized elements */
178 tail = bytes;
179 for (niov = 0; niov < iov_cnt && iov[niov].iov_len <= tail; ++niov) {
180 tail -= iov[niov].iov_len;
181 }
182 if (tail) {
183 /* second, fixup the last element, and remember the original
184 * length */
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;
2be178a4
MT
189 ret = do_send_recv(sockfd, iov, niov, do_send);
190 /* Undo the changes above before checking for errors */
83f75c26 191 iov[niov-1].iov_len = orig_len;
2be178a4
MT
192 } else {
193 ret = do_send_recv(sockfd, iov, niov, do_send);
83f75c26
PB
194 }
195 if (offset) {
196 iov[0].iov_base -= offset;
197 iov[0].iov_len += offset;
198 }
199
200 if (ret < 0) {
201 assert(errno != EINTR);
6b64640d 202 g_free(local_iov);
83f75c26
PB
203 if (errno == EAGAIN && total > 0) {
204 return total;
205 }
206 return -1;
207 }
208
84004290
MK
209 if (ret == 0 && !do_send) {
210 /* recv returns 0 when the peer has performed an orderly
211 * shutdown. */
212 break;
213 }
214
83f75c26
PB
215 /* Prepare for the next iteration */
216 offset += ret;
217 total += ret;
218 bytes -= ret;
25e5e4c7 219 }
25e5e4c7 220
6b64640d 221 g_free(local_iov);
83f75c26 222 return total;
25e5e4c7
MT
223}
224
225
3a1dca94
GH
226void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
227 FILE *fp, const char *prefix, size_t limit)
228{
6ff66f50
PC
229 int v;
230 size_t size = 0;
231 char *buf;
232
233 for (v = 0; v < iov_cnt; v++) {
234 size += iov[v].iov_len;
3a1dca94 235 }
6ff66f50
PC
236 size = size > limit ? limit : size;
237 buf = g_malloc(size);
238 iov_to_buf(iov, iov_cnt, 0, buf, size);
3568ac2a 239 qemu_hexdump(buf, fp, prefix, size);
6ff66f50 240 g_free(buf);
3a1dca94 241}
0191253c 242
d336336c
MT
243unsigned 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)
246{
247 size_t len;
248 unsigned int i, j;
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;
252 continue;
253 }
254 len = MIN(bytes, iov[i].iov_len - offset);
255
256 dst_iov[j].iov_base = iov[i].iov_base + offset;
257 dst_iov[j].iov_len = len;
258 j++;
259 bytes -= len;
260 offset = 0;
261 }
262 assert(offset == 0);
263 return j;
264}
f563a5d7 265
0191253c
PB
266/* io vectors */
267
268void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint)
269{
e1cf5582 270 qiov->iov = g_new(struct iovec, alloc_hint);
0191253c
PB
271 qiov->niov = 0;
272 qiov->nalloc = alloc_hint;
273 qiov->size = 0;
274}
275
276void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov)
277{
278 int i;
279
280 qiov->iov = iov;
281 qiov->niov = niov;
282 qiov->nalloc = -1;
283 qiov->size = 0;
284 for (i = 0; i < niov; i++)
285 qiov->size += iov[i].iov_len;
286}
287
288void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len)
289{
290 assert(qiov->nalloc != -1);
291
292 if (qiov->niov == qiov->nalloc) {
293 qiov->nalloc = 2 * qiov->nalloc + 1;
e1cf5582 294 qiov->iov = g_renew(struct iovec, qiov->iov, qiov->nalloc);
0191253c
PB
295 }
296 qiov->iov[qiov->niov].iov_base = base;
297 qiov->iov[qiov->niov].iov_len = len;
298 qiov->size += len;
299 ++qiov->niov;
300}
301
302/*
530c0bbd 303 * Concatenates (partial) iovecs from src_iov to the end of dst.
0191253c
PB
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
530c0bbd 307 * of src_iov if it comes first. This way, it is okay to specify
0191253c
PB
308 * very large value for `sbytes' to indicate "up to the end
309 * of src".
310 * Only vector pointers are processed, not the actual data buffers.
311 */
519661ee
PB
312size_t qemu_iovec_concat_iov(QEMUIOVector *dst,
313 struct iovec *src_iov, unsigned int src_cnt,
314 size_t soffset, size_t sbytes)
0191253c
PB
315{
316 int i;
317 size_t done;
facf98ad
AK
318
319 if (!sbytes) {
519661ee 320 return 0;
facf98ad 321 }
0191253c 322 assert(dst->nalloc != -1);
530c0bbd
SH
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);
0191253c
PB
327 done += len;
328 soffset = 0;
329 } else {
530c0bbd 330 soffset -= src_iov[i].iov_len;
0191253c
PB
331 }
332 }
530c0bbd 333 assert(soffset == 0); /* offset beyond end of src */
519661ee
PB
334
335 return done;
530c0bbd
SH
336}
337
338/*
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
345 * of src".
346 * Only vector pointers are processed, not the actual data buffers.
347 */
348void qemu_iovec_concat(QEMUIOVector *dst,
349 QEMUIOVector *src, size_t soffset, size_t sbytes)
350{
351 qemu_iovec_concat_iov(dst, src->iov, src->niov, soffset, sbytes);
0191253c
PB
352}
353
43f35cb5
PL
354/*
355 * Check if the contents of the iovecs are all zero
356 */
357bool qemu_iovec_is_zero(QEMUIOVector *qiov)
358{
359 int i;
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)) {
364 return false;
365 }
366 for (; offs < qiov->iov[i].iov_len; offs++) {
367 if (ptr[offs]) {
368 return false;
369 }
370 }
371 }
372 return true;
373}
374
0191253c
PB
375void qemu_iovec_destroy(QEMUIOVector *qiov)
376{
377 assert(qiov->nalloc != -1);
378
379 qemu_iovec_reset(qiov);
380 g_free(qiov->iov);
381 qiov->nalloc = 0;
382 qiov->iov = NULL;
383}
384
385void qemu_iovec_reset(QEMUIOVector *qiov)
386{
387 assert(qiov->nalloc != -1);
388
389 qiov->niov = 0;
390 qiov->size = 0;
391}
392
393size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
394 void *buf, size_t bytes)
395{
396 return iov_to_buf(qiov->iov, qiov->niov, offset, buf, bytes);
397}
398
399size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
400 const void *buf, size_t bytes)
401{
402 return iov_from_buf(qiov->iov, qiov->niov, offset, buf, bytes);
403}
404
405size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
406 int fillc, size_t bytes)
407{
408 return iov_memset(qiov->iov, qiov->niov, offset, fillc, bytes);
409}
d0277635 410
f70d7f7e
BC
411/**
412 * Check that I/O vector contents are identical
413 *
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().
416 *
417 * @a: I/O vector
418 * @b: I/O vector
419 * @ret: Offset to first mismatching byte or -1 if match
420 */
421ssize_t qemu_iovec_compare(QEMUIOVector *a, QEMUIOVector *b)
422{
423 int i;
424 ssize_t offset = 0;
425
426 assert(a->niov == b->niov);
427 for (i = 0; i < a->niov; i++) {
428 size_t len = 0;
429 uint8_t *p = (uint8_t *)a->iov[i].iov_base;
430 uint8_t *q = (uint8_t *)b->iov[i].iov_base;
431
432 assert(a->iov[i].iov_len == b->iov[i].iov_len);
433 while (len < a->iov[i].iov_len && *p++ == *q++) {
434 len++;
435 }
436
437 offset += len;
438
439 if (len != a->iov[i].iov_len) {
440 return offset;
441 }
442 }
443 return -1;
444}
445
446typedef struct {
447 int src_index;
448 struct iovec *src_iov;
449 void *dest_base;
450} IOVectorSortElem;
451
452static int sortelem_cmp_src_base(const void *a, const void *b)
453{
454 const IOVectorSortElem *elem_a = a;
455 const IOVectorSortElem *elem_b = b;
456
457 /* Don't overflow */
458 if (elem_a->src_iov->iov_base < elem_b->src_iov->iov_base) {
459 return -1;
460 } else if (elem_a->src_iov->iov_base > elem_b->src_iov->iov_base) {
461 return 1;
462 } else {
463 return 0;
464 }
465}
466
467static int sortelem_cmp_src_index(const void *a, const void *b)
468{
469 const IOVectorSortElem *elem_a = a;
470 const IOVectorSortElem *elem_b = b;
471
472 return elem_a->src_index - elem_b->src_index;
473}
474
475/**
476 * Copy contents of I/O vector
477 *
478 * The relative relationships of overlapping iovecs are preserved. This is
479 * necessary to ensure identical semantics in the cloned I/O vector.
480 */
481void qemu_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src, void *buf)
482{
483 IOVectorSortElem sortelems[src->niov];
484 void *last_end;
485 int i;
486
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];
491 }
492 qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_base);
493
494 /* Allocate buffer space taking into account overlapping iovecs */
495 last_end = NULL;
496 for (i = 0; i < src->niov; i++) {
497 struct iovec *cur = sortelems[i].src_iov;
498 ptrdiff_t rewind = 0;
499
500 /* Detect overlap */
501 if (last_end && last_end > cur->iov_base) {
502 rewind = last_end - cur->iov_base;
503 }
504
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);
508 }
509
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);
514 }
515}
516
d0277635
SH
517size_t iov_discard_front(struct iovec **iov, unsigned int *iov_cnt,
518 size_t bytes)
519{
520 size_t total = 0;
521 struct iovec *cur;
522
523 for (cur = *iov; *iov_cnt > 0; cur++) {
524 if (cur->iov_len > bytes) {
525 cur->iov_base += bytes;
526 cur->iov_len -= bytes;
527 total += bytes;
528 break;
529 }
530
531 bytes -= cur->iov_len;
532 total += cur->iov_len;
533 *iov_cnt -= 1;
534 }
535
536 *iov = cur;
537 return total;
538}
539
540size_t iov_discard_back(struct iovec *iov, unsigned int *iov_cnt,
541 size_t bytes)
542{
543 size_t total = 0;
544 struct iovec *cur;
545
546 if (*iov_cnt == 0) {
547 return 0;
548 }
549
550 cur = iov + (*iov_cnt - 1);
551
552 while (*iov_cnt > 0) {
553 if (cur->iov_len > bytes) {
554 cur->iov_len -= bytes;
555 total += bytes;
556 break;
557 }
558
559 bytes -= cur->iov_len;
560 total += cur->iov_len;
561 cur--;
562 *iov_cnt -= 1;
563 }
564
565 return total;
566}
58f423fb
KW
567
568void qemu_iovec_discard_back(QEMUIOVector *qiov, size_t bytes)
569{
570 size_t total;
571 unsigned int niov = qiov->niov;
572
573 assert(qiov->size >= bytes);
574 total = iov_discard_back(qiov->iov, &niov, bytes);
575 assert(total == bytes);
576
577 qiov->niov = niov;
578 qiov->size -= bytes;
579}
This page took 0.486372 seconds and 4 git commands to generate.