PPC: e500: implement PCI INTx routing
[qemu/ar7.git] / util / iov.c
blob49f88388f800d8656a7dfb3b1558f965be01d631
1 /*
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.
7 * Author(s):
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.
19 #include "qemu/iov.h"
20 #include "qemu/sockets.h"
22 size_t iov_from_buf(const struct iovec *iov, unsigned int iov_cnt,
23 size_t offset, const void *buf, size_t bytes)
25 size_t done;
26 unsigned int i;
27 for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) {
28 if (offset < iov[i].iov_len) {
29 size_t len = MIN(iov[i].iov_len - offset, bytes - done);
30 memcpy(iov[i].iov_base + offset, buf + done, len);
31 done += len;
32 offset = 0;
33 } else {
34 offset -= iov[i].iov_len;
37 assert(offset == 0);
38 return done;
41 size_t iov_to_buf(const struct iovec *iov, const unsigned int iov_cnt,
42 size_t offset, void *buf, size_t bytes)
44 size_t done;
45 unsigned int i;
46 for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) {
47 if (offset < iov[i].iov_len) {
48 size_t len = MIN(iov[i].iov_len - offset, bytes - done);
49 memcpy(buf + done, iov[i].iov_base + offset, len);
50 done += len;
51 offset = 0;
52 } else {
53 offset -= iov[i].iov_len;
56 assert(offset == 0);
57 return done;
60 size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt,
61 size_t offset, int fillc, size_t bytes)
63 size_t done;
64 unsigned int i;
65 for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) {
66 if (offset < iov[i].iov_len) {
67 size_t len = MIN(iov[i].iov_len - offset, bytes - done);
68 memset(iov[i].iov_base + offset, fillc, len);
69 done += len;
70 offset = 0;
71 } else {
72 offset -= iov[i].iov_len;
75 assert(offset == 0);
76 return done;
79 size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt)
81 size_t len;
82 unsigned int i;
84 len = 0;
85 for (i = 0; i < iov_cnt; i++) {
86 len += iov[i].iov_len;
88 return len;
91 /* helper function for iov_send_recv() */
92 static ssize_t
93 do_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt, bool do_send)
95 #ifdef CONFIG_POSIX
96 ssize_t ret;
97 struct msghdr msg;
98 memset(&msg, 0, sizeof(msg));
99 msg.msg_iov = iov;
100 msg.msg_iovlen = iov_cnt;
101 do {
102 ret = do_send
103 ? sendmsg(sockfd, &msg, 0)
104 : recvmsg(sockfd, &msg, 0);
105 } while (ret < 0 && errno == EINTR);
106 return ret;
107 #else
108 /* else send piece-by-piece */
109 /*XXX Note: windows has WSASend() and WSARecv() */
110 unsigned i = 0;
111 ssize_t ret = 0;
112 while (i < iov_cnt) {
113 ssize_t r = do_send
114 ? send(sockfd, iov[i].iov_base, iov[i].iov_len, 0)
115 : recv(sockfd, iov[i].iov_base, iov[i].iov_len, 0);
116 if (r > 0) {
117 ret += r;
118 } else if (!r) {
119 break;
120 } else if (errno == EINTR) {
121 continue;
122 } else {
123 /* else it is some "other" error,
124 * only return if there was no data processed. */
125 if (ret == 0) {
126 ret = -1;
128 break;
130 i++;
132 return ret;
133 #endif
136 ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt,
137 size_t offset, size_t bytes,
138 bool do_send)
140 ssize_t total = 0;
141 ssize_t ret;
142 size_t orig_len, tail;
143 unsigned niov;
145 while (bytes > 0) {
146 /* Find the start position, skipping `offset' bytes:
147 * first, skip all full-sized vector elements, */
148 for (niov = 0; niov < iov_cnt && offset >= iov[niov].iov_len; ++niov) {
149 offset -= iov[niov].iov_len;
152 /* niov == iov_cnt would only be valid if bytes == 0, which
153 * we already ruled out in the loop condition. */
154 assert(niov < iov_cnt);
155 iov += niov;
156 iov_cnt -= niov;
158 if (offset) {
159 /* second, skip `offset' bytes from the (now) first element,
160 * undo it on exit */
161 iov[0].iov_base += offset;
162 iov[0].iov_len -= offset;
164 /* Find the end position skipping `bytes' bytes: */
165 /* first, skip all full-sized elements */
166 tail = bytes;
167 for (niov = 0; niov < iov_cnt && iov[niov].iov_len <= tail; ++niov) {
168 tail -= iov[niov].iov_len;
170 if (tail) {
171 /* second, fixup the last element, and remember the original
172 * length */
173 assert(niov < iov_cnt);
174 assert(iov[niov].iov_len > tail);
175 orig_len = iov[niov].iov_len;
176 iov[niov++].iov_len = tail;
177 ret = do_send_recv(sockfd, iov, niov, do_send);
178 /* Undo the changes above before checking for errors */
179 iov[niov-1].iov_len = orig_len;
180 } else {
181 ret = do_send_recv(sockfd, iov, niov, do_send);
183 if (offset) {
184 iov[0].iov_base -= offset;
185 iov[0].iov_len += offset;
188 if (ret < 0) {
189 assert(errno != EINTR);
190 if (errno == EAGAIN && total > 0) {
191 return total;
193 return -1;
196 if (ret == 0 && !do_send) {
197 /* recv returns 0 when the peer has performed an orderly
198 * shutdown. */
199 break;
202 /* Prepare for the next iteration */
203 offset += ret;
204 total += ret;
205 bytes -= ret;
208 return total;
212 void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
213 FILE *fp, const char *prefix, size_t limit)
215 int v;
216 size_t size = 0;
217 char *buf;
219 for (v = 0; v < iov_cnt; v++) {
220 size += iov[v].iov_len;
222 size = size > limit ? limit : size;
223 buf = g_malloc(size);
224 iov_to_buf(iov, iov_cnt, 0, buf, size);
225 qemu_hexdump(buf, fp, prefix, size);
226 g_free(buf);
229 unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt,
230 const struct iovec *iov, unsigned int iov_cnt,
231 size_t offset, size_t bytes)
233 size_t len;
234 unsigned int i, j;
235 for (i = 0, j = 0; i < iov_cnt && j < dst_iov_cnt && bytes; i++) {
236 if (offset >= iov[i].iov_len) {
237 offset -= iov[i].iov_len;
238 continue;
240 len = MIN(bytes, iov[i].iov_len - offset);
242 dst_iov[j].iov_base = iov[i].iov_base + offset;
243 dst_iov[j].iov_len = len;
244 j++;
245 bytes -= len;
246 offset = 0;
248 assert(offset == 0);
249 return j;
252 /* io vectors */
254 void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint)
256 qiov->iov = g_malloc(alloc_hint * sizeof(struct iovec));
257 qiov->niov = 0;
258 qiov->nalloc = alloc_hint;
259 qiov->size = 0;
262 void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov)
264 int i;
266 qiov->iov = iov;
267 qiov->niov = niov;
268 qiov->nalloc = -1;
269 qiov->size = 0;
270 for (i = 0; i < niov; i++)
271 qiov->size += iov[i].iov_len;
274 void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len)
276 assert(qiov->nalloc != -1);
278 if (qiov->niov == qiov->nalloc) {
279 qiov->nalloc = 2 * qiov->nalloc + 1;
280 qiov->iov = g_realloc(qiov->iov, qiov->nalloc * sizeof(struct iovec));
282 qiov->iov[qiov->niov].iov_base = base;
283 qiov->iov[qiov->niov].iov_len = len;
284 qiov->size += len;
285 ++qiov->niov;
289 * Concatenates (partial) iovecs from src_iov to the end of dst.
290 * It starts copying after skipping `soffset' bytes at the
291 * beginning of src and adds individual vectors from src to
292 * dst copies up to `sbytes' bytes total, or up to the end
293 * of src_iov if it comes first. This way, it is okay to specify
294 * very large value for `sbytes' to indicate "up to the end
295 * of src".
296 * Only vector pointers are processed, not the actual data buffers.
298 void qemu_iovec_concat_iov(QEMUIOVector *dst,
299 struct iovec *src_iov, unsigned int src_cnt,
300 size_t soffset, size_t sbytes)
302 int i;
303 size_t done;
305 if (!sbytes) {
306 return;
308 assert(dst->nalloc != -1);
309 for (i = 0, done = 0; done < sbytes && i < src_cnt; i++) {
310 if (soffset < src_iov[i].iov_len) {
311 size_t len = MIN(src_iov[i].iov_len - soffset, sbytes - done);
312 qemu_iovec_add(dst, src_iov[i].iov_base + soffset, len);
313 done += len;
314 soffset = 0;
315 } else {
316 soffset -= src_iov[i].iov_len;
319 assert(soffset == 0); /* offset beyond end of src */
323 * Concatenates (partial) iovecs from src to the end of dst.
324 * It starts copying after skipping `soffset' bytes at the
325 * beginning of src and adds individual vectors from src to
326 * dst copies up to `sbytes' bytes total, or up to the end
327 * of src if it comes first. This way, it is okay to specify
328 * very large value for `sbytes' to indicate "up to the end
329 * of src".
330 * Only vector pointers are processed, not the actual data buffers.
332 void qemu_iovec_concat(QEMUIOVector *dst,
333 QEMUIOVector *src, size_t soffset, size_t sbytes)
335 qemu_iovec_concat_iov(dst, src->iov, src->niov, soffset, sbytes);
339 * Check if the contents of the iovecs are all zero
341 bool qemu_iovec_is_zero(QEMUIOVector *qiov)
343 int i;
344 for (i = 0; i < qiov->niov; i++) {
345 size_t offs = QEMU_ALIGN_DOWN(qiov->iov[i].iov_len, 4 * sizeof(long));
346 uint8_t *ptr = qiov->iov[i].iov_base;
347 if (offs && !buffer_is_zero(qiov->iov[i].iov_base, offs)) {
348 return false;
350 for (; offs < qiov->iov[i].iov_len; offs++) {
351 if (ptr[offs]) {
352 return false;
356 return true;
359 void qemu_iovec_destroy(QEMUIOVector *qiov)
361 assert(qiov->nalloc != -1);
363 qemu_iovec_reset(qiov);
364 g_free(qiov->iov);
365 qiov->nalloc = 0;
366 qiov->iov = NULL;
369 void qemu_iovec_reset(QEMUIOVector *qiov)
371 assert(qiov->nalloc != -1);
373 qiov->niov = 0;
374 qiov->size = 0;
377 size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
378 void *buf, size_t bytes)
380 return iov_to_buf(qiov->iov, qiov->niov, offset, buf, bytes);
383 size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
384 const void *buf, size_t bytes)
386 return iov_from_buf(qiov->iov, qiov->niov, offset, buf, bytes);
389 size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
390 int fillc, size_t bytes)
392 return iov_memset(qiov->iov, qiov->niov, offset, fillc, bytes);
396 * Check that I/O vector contents are identical
398 * The IO vectors must have the same structure (same length of all parts).
399 * A typical usage is to compare vectors created with qemu_iovec_clone().
401 * @a: I/O vector
402 * @b: I/O vector
403 * @ret: Offset to first mismatching byte or -1 if match
405 ssize_t qemu_iovec_compare(QEMUIOVector *a, QEMUIOVector *b)
407 int i;
408 ssize_t offset = 0;
410 assert(a->niov == b->niov);
411 for (i = 0; i < a->niov; i++) {
412 size_t len = 0;
413 uint8_t *p = (uint8_t *)a->iov[i].iov_base;
414 uint8_t *q = (uint8_t *)b->iov[i].iov_base;
416 assert(a->iov[i].iov_len == b->iov[i].iov_len);
417 while (len < a->iov[i].iov_len && *p++ == *q++) {
418 len++;
421 offset += len;
423 if (len != a->iov[i].iov_len) {
424 return offset;
427 return -1;
430 typedef struct {
431 int src_index;
432 struct iovec *src_iov;
433 void *dest_base;
434 } IOVectorSortElem;
436 static int sortelem_cmp_src_base(const void *a, const void *b)
438 const IOVectorSortElem *elem_a = a;
439 const IOVectorSortElem *elem_b = b;
441 /* Don't overflow */
442 if (elem_a->src_iov->iov_base < elem_b->src_iov->iov_base) {
443 return -1;
444 } else if (elem_a->src_iov->iov_base > elem_b->src_iov->iov_base) {
445 return 1;
446 } else {
447 return 0;
451 static int sortelem_cmp_src_index(const void *a, const void *b)
453 const IOVectorSortElem *elem_a = a;
454 const IOVectorSortElem *elem_b = b;
456 return elem_a->src_index - elem_b->src_index;
460 * Copy contents of I/O vector
462 * The relative relationships of overlapping iovecs are preserved. This is
463 * necessary to ensure identical semantics in the cloned I/O vector.
465 void qemu_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src, void *buf)
467 IOVectorSortElem sortelems[src->niov];
468 void *last_end;
469 int i;
471 /* Sort by source iovecs by base address */
472 for (i = 0; i < src->niov; i++) {
473 sortelems[i].src_index = i;
474 sortelems[i].src_iov = &src->iov[i];
476 qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_base);
478 /* Allocate buffer space taking into account overlapping iovecs */
479 last_end = NULL;
480 for (i = 0; i < src->niov; i++) {
481 struct iovec *cur = sortelems[i].src_iov;
482 ptrdiff_t rewind = 0;
484 /* Detect overlap */
485 if (last_end && last_end > cur->iov_base) {
486 rewind = last_end - cur->iov_base;
489 sortelems[i].dest_base = buf - rewind;
490 buf += cur->iov_len - MIN(rewind, cur->iov_len);
491 last_end = MAX(cur->iov_base + cur->iov_len, last_end);
494 /* Sort by source iovec index and build destination iovec */
495 qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_index);
496 for (i = 0; i < src->niov; i++) {
497 qemu_iovec_add(dest, sortelems[i].dest_base, src->iov[i].iov_len);
501 size_t iov_discard_front(struct iovec **iov, unsigned int *iov_cnt,
502 size_t bytes)
504 size_t total = 0;
505 struct iovec *cur;
507 for (cur = *iov; *iov_cnt > 0; cur++) {
508 if (cur->iov_len > bytes) {
509 cur->iov_base += bytes;
510 cur->iov_len -= bytes;
511 total += bytes;
512 break;
515 bytes -= cur->iov_len;
516 total += cur->iov_len;
517 *iov_cnt -= 1;
520 *iov = cur;
521 return total;
524 size_t iov_discard_back(struct iovec *iov, unsigned int *iov_cnt,
525 size_t bytes)
527 size_t total = 0;
528 struct iovec *cur;
530 if (*iov_cnt == 0) {
531 return 0;
534 cur = iov + (*iov_cnt - 1);
536 while (*iov_cnt > 0) {
537 if (cur->iov_len > bytes) {
538 cur->iov_len -= bytes;
539 total += bytes;
540 break;
543 bytes -= cur->iov_len;
544 total += cur->iov_len;
545 cur--;
546 *iov_cnt -= 1;
549 return total;