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.
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"
20 #include "qemu-common.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, struct iovec *iov, unsigned iov_cnt, bool do_send)
101 memset(&msg, 0, sizeof(msg));
103 msg.msg_iovlen = iov_cnt;
106 ? sendmsg(sockfd, &msg, 0)
107 : recvmsg(sockfd, &msg, 0);
108 } while (ret < 0 && errno == EINTR);
111 /* else send piece-by-piece */
112 /*XXX Note: windows has WSASend() and WSARecv() */
115 while (i < iov_cnt) {
117 ? send(sockfd, iov[i].iov_base, iov[i].iov_len, 0)
118 : recv(sockfd, iov[i].iov_base, iov[i].iov_len, 0);
123 } else if (errno == EINTR) {
126 /* else it is some "other" error,
127 * only return if there was no data processed. */
139 ssize_t iov_send_recv(int sockfd, const struct iovec *_iov, unsigned iov_cnt,
140 size_t offset, size_t bytes,
145 size_t orig_len, tail;
147 struct iovec *local_iov, *iov;
153 local_iov = g_new0(struct iovec, iov_cnt);
154 iov_copy(local_iov, iov_cnt, _iov, iov_cnt, offset, bytes);
159 /* Find the start position, skipping `offset' bytes:
160 * first, skip all full-sized vector elements, */
161 for (niov = 0; niov < iov_cnt && offset >= iov[niov].iov_len; ++niov) {
162 offset -= iov[niov].iov_len;
165 /* niov == iov_cnt would only be valid if bytes == 0, which
166 * we already ruled out in the loop condition. */
167 assert(niov < iov_cnt);
172 /* second, skip `offset' bytes from the (now) first element,
174 iov[0].iov_base += offset;
175 iov[0].iov_len -= offset;
177 /* Find the end position skipping `bytes' bytes: */
178 /* first, skip all full-sized elements */
180 for (niov = 0; niov < iov_cnt && iov[niov].iov_len <= tail; ++niov) {
181 tail -= iov[niov].iov_len;
184 /* second, fixup the last element, and remember the original
186 assert(niov < iov_cnt);
187 assert(iov[niov].iov_len > tail);
188 orig_len = iov[niov].iov_len;
189 iov[niov++].iov_len = tail;
190 ret = do_send_recv(sockfd, iov, niov, do_send);
191 /* Undo the changes above before checking for errors */
192 iov[niov-1].iov_len = orig_len;
194 ret = do_send_recv(sockfd, iov, niov, do_send);
197 iov[0].iov_base -= offset;
198 iov[0].iov_len += offset;
202 assert(errno != EINTR);
204 if (errno == EAGAIN && total > 0) {
210 if (ret == 0 && !do_send) {
211 /* recv returns 0 when the peer has performed an orderly
216 /* Prepare for the next iteration */
227 void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
228 FILE *fp, const char *prefix, size_t limit)
234 for (v = 0; v < iov_cnt; v++) {
235 size += iov[v].iov_len;
237 size = size > limit ? limit : size;
238 buf = g_malloc(size);
239 iov_to_buf(iov, iov_cnt, 0, buf, size);
240 qemu_hexdump(buf, fp, prefix, size);
244 unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt,
245 const struct iovec *iov, unsigned int iov_cnt,
246 size_t offset, size_t bytes)
251 i < iov_cnt && j < dst_iov_cnt && (offset || bytes); i++) {
252 if (offset >= iov[i].iov_len) {
253 offset -= iov[i].iov_len;
256 len = MIN(bytes, iov[i].iov_len - offset);
258 dst_iov[j].iov_base = iov[i].iov_base + offset;
259 dst_iov[j].iov_len = len;
270 void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint)
272 qiov->iov = g_new(struct iovec, alloc_hint);
274 qiov->nalloc = alloc_hint;
278 void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov)
286 for (i = 0; i < niov; i++)
287 qiov->size += iov[i].iov_len;
290 void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len)
292 assert(qiov->nalloc != -1);
294 if (qiov->niov == qiov->nalloc) {
295 qiov->nalloc = 2 * qiov->nalloc + 1;
296 qiov->iov = g_renew(struct iovec, qiov->iov, qiov->nalloc);
298 qiov->iov[qiov->niov].iov_base = base;
299 qiov->iov[qiov->niov].iov_len = len;
305 * Concatenates (partial) iovecs from src_iov to the end of dst.
306 * It starts copying after skipping `soffset' bytes at the
307 * beginning of src and adds individual vectors from src to
308 * dst copies up to `sbytes' bytes total, or up to the end
309 * of src_iov if it comes first. This way, it is okay to specify
310 * very large value for `sbytes' to indicate "up to the end
312 * Only vector pointers are processed, not the actual data buffers.
314 size_t qemu_iovec_concat_iov(QEMUIOVector *dst,
315 struct iovec *src_iov, unsigned int src_cnt,
316 size_t soffset, size_t sbytes)
324 assert(dst->nalloc != -1);
325 for (i = 0, done = 0; done < sbytes && i < src_cnt; i++) {
326 if (soffset < src_iov[i].iov_len) {
327 size_t len = MIN(src_iov[i].iov_len - soffset, sbytes - done);
328 qemu_iovec_add(dst, src_iov[i].iov_base + soffset, len);
332 soffset -= src_iov[i].iov_len;
335 assert(soffset == 0); /* offset beyond end of src */
341 * Concatenates (partial) iovecs from src to the end of dst.
342 * It starts copying after skipping `soffset' bytes at the
343 * beginning of src and adds individual vectors from src to
344 * dst copies up to `sbytes' bytes total, or up to the end
345 * of src if it comes first. This way, it is okay to specify
346 * very large value for `sbytes' to indicate "up to the end
348 * Only vector pointers are processed, not the actual data buffers.
350 void qemu_iovec_concat(QEMUIOVector *dst,
351 QEMUIOVector *src, size_t soffset, size_t sbytes)
353 qemu_iovec_concat_iov(dst, src->iov, src->niov, soffset, sbytes);
357 * Check if the contents of the iovecs are all zero
359 bool qemu_iovec_is_zero(QEMUIOVector *qiov)
362 for (i = 0; i < qiov->niov; i++) {
363 size_t offs = QEMU_ALIGN_DOWN(qiov->iov[i].iov_len, 4 * sizeof(long));
364 uint8_t *ptr = qiov->iov[i].iov_base;
365 if (offs && !buffer_is_zero(qiov->iov[i].iov_base, offs)) {
368 for (; offs < qiov->iov[i].iov_len; offs++) {
377 void qemu_iovec_destroy(QEMUIOVector *qiov)
379 assert(qiov->nalloc != -1);
381 qemu_iovec_reset(qiov);
387 void qemu_iovec_reset(QEMUIOVector *qiov)
389 assert(qiov->nalloc != -1);
395 size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
396 void *buf, size_t bytes)
398 return iov_to_buf(qiov->iov, qiov->niov, offset, buf, bytes);
401 size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
402 const void *buf, size_t bytes)
404 return iov_from_buf(qiov->iov, qiov->niov, offset, buf, bytes);
407 size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
408 int fillc, size_t bytes)
410 return iov_memset(qiov->iov, qiov->niov, offset, fillc, bytes);
414 * Check that I/O vector contents are identical
416 * The IO vectors must have the same structure (same length of all parts).
417 * A typical usage is to compare vectors created with qemu_iovec_clone().
421 * @ret: Offset to first mismatching byte or -1 if match
423 ssize_t qemu_iovec_compare(QEMUIOVector *a, QEMUIOVector *b)
428 assert(a->niov == b->niov);
429 for (i = 0; i < a->niov; i++) {
431 uint8_t *p = (uint8_t *)a->iov[i].iov_base;
432 uint8_t *q = (uint8_t *)b->iov[i].iov_base;
434 assert(a->iov[i].iov_len == b->iov[i].iov_len);
435 while (len < a->iov[i].iov_len && *p++ == *q++) {
441 if (len != a->iov[i].iov_len) {
450 struct iovec *src_iov;
454 static int sortelem_cmp_src_base(const void *a, const void *b)
456 const IOVectorSortElem *elem_a = a;
457 const IOVectorSortElem *elem_b = b;
460 if (elem_a->src_iov->iov_base < elem_b->src_iov->iov_base) {
462 } else if (elem_a->src_iov->iov_base > elem_b->src_iov->iov_base) {
469 static int sortelem_cmp_src_index(const void *a, const void *b)
471 const IOVectorSortElem *elem_a = a;
472 const IOVectorSortElem *elem_b = b;
474 return elem_a->src_index - elem_b->src_index;
478 * Copy contents of I/O vector
480 * The relative relationships of overlapping iovecs are preserved. This is
481 * necessary to ensure identical semantics in the cloned I/O vector.
483 void qemu_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src, void *buf)
485 IOVectorSortElem sortelems[src->niov];
489 /* Sort by source iovecs by base address */
490 for (i = 0; i < src->niov; i++) {
491 sortelems[i].src_index = i;
492 sortelems[i].src_iov = &src->iov[i];
494 qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_base);
496 /* Allocate buffer space taking into account overlapping iovecs */
498 for (i = 0; i < src->niov; i++) {
499 struct iovec *cur = sortelems[i].src_iov;
500 ptrdiff_t rewind = 0;
503 if (last_end && last_end > cur->iov_base) {
504 rewind = last_end - cur->iov_base;
507 sortelems[i].dest_base = buf - rewind;
508 buf += cur->iov_len - MIN(rewind, cur->iov_len);
509 last_end = MAX(cur->iov_base + cur->iov_len, last_end);
512 /* Sort by source iovec index and build destination iovec */
513 qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_index);
514 for (i = 0; i < src->niov; i++) {
515 qemu_iovec_add(dest, sortelems[i].dest_base, src->iov[i].iov_len);
519 size_t iov_discard_front(struct iovec **iov, unsigned int *iov_cnt,
525 for (cur = *iov; *iov_cnt > 0; cur++) {
526 if (cur->iov_len > bytes) {
527 cur->iov_base += bytes;
528 cur->iov_len -= bytes;
533 bytes -= cur->iov_len;
534 total += cur->iov_len;
542 size_t iov_discard_back(struct iovec *iov, unsigned int *iov_cnt,
552 cur = iov + (*iov_cnt - 1);
554 while (*iov_cnt > 0) {
555 if (cur->iov_len > bytes) {
556 cur->iov_len -= bytes;
561 bytes -= cur->iov_len;
562 total += cur->iov_len;
570 void qemu_iovec_discard_back(QEMUIOVector *qiov, size_t bytes)
573 unsigned int niov = qiov->niov;
575 assert(qiov->size >= bytes);
576 total = iov_discard_back(qiov->iov, &niov, bytes);
577 assert(total == bytes);