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