xref: /openbmc/qemu/util/iov.c (revision 97841438)
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-common.h"
21 #include "qemu/iov.h"
22 #include "qemu/sockets.h"
23 #include "qemu/cutils.h"
24 
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)
27 {
28     size_t done;
29     unsigned int i;
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);
34             done += len;
35             offset = 0;
36         } else {
37             offset -= iov[i].iov_len;
38         }
39     }
40     assert(offset == 0);
41     return done;
42 }
43 
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)
46 {
47     size_t done;
48     unsigned int i;
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);
53             done += len;
54             offset = 0;
55         } else {
56             offset -= iov[i].iov_len;
57         }
58     }
59     assert(offset == 0);
60     return done;
61 }
62 
63 size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt,
64                   size_t offset, int fillc, size_t bytes)
65 {
66     size_t done;
67     unsigned int i;
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);
72             done += len;
73             offset = 0;
74         } else {
75             offset -= iov[i].iov_len;
76         }
77     }
78     assert(offset == 0);
79     return done;
80 }
81 
82 size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt)
83 {
84     size_t len;
85     unsigned int i;
86 
87     len = 0;
88     for (i = 0; i < iov_cnt; i++) {
89         len += iov[i].iov_len;
90     }
91     return len;
92 }
93 
94 /* helper function for iov_send_recv() */
95 static ssize_t
96 do_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt, bool do_send)
97 {
98 #ifdef CONFIG_POSIX
99     ssize_t ret;
100     struct msghdr msg;
101     memset(&msg, 0, sizeof(msg));
102     msg.msg_iov = iov;
103     msg.msg_iovlen = iov_cnt;
104     do {
105         ret = do_send
106             ? sendmsg(sockfd, &msg, 0)
107             : recvmsg(sockfd, &msg, 0);
108     } while (ret < 0 && errno == EINTR);
109     return ret;
110 #else
111     /* else send piece-by-piece */
112     /*XXX Note: windows has WSASend() and WSARecv() */
113     unsigned i = 0;
114     ssize_t ret = 0;
115     while (i < iov_cnt) {
116         ssize_t r = do_send
117             ? send(sockfd, iov[i].iov_base, iov[i].iov_len, 0)
118             : recv(sockfd, iov[i].iov_base, iov[i].iov_len, 0);
119         if (r > 0) {
120             ret += r;
121         } else if (!r) {
122             break;
123         } else if (errno == EINTR) {
124             continue;
125         } else {
126             /* else it is some "other" error,
127              * only return if there was no data processed. */
128             if (ret == 0) {
129                 ret = -1;
130             }
131             break;
132         }
133         i++;
134     }
135     return ret;
136 #endif
137 }
138 
139 ssize_t iov_send_recv(int sockfd, const struct iovec *_iov, unsigned iov_cnt,
140                       size_t offset, size_t bytes,
141                       bool do_send)
142 {
143     ssize_t total = 0;
144     ssize_t ret;
145     size_t orig_len, tail;
146     unsigned niov;
147     struct iovec *local_iov, *iov;
148 
149     if (bytes <= 0) {
150         return 0;
151     }
152 
153     local_iov = g_new0(struct iovec, iov_cnt);
154     iov_copy(local_iov, iov_cnt, _iov, iov_cnt, offset, bytes);
155     offset = 0;
156     iov = local_iov;
157 
158     while (bytes > 0) {
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;
163         }
164 
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);
168         iov += niov;
169         iov_cnt -= niov;
170 
171         if (offset) {
172             /* second, skip `offset' bytes from the (now) first element,
173              * undo it on exit */
174             iov[0].iov_base += offset;
175             iov[0].iov_len -= offset;
176         }
177         /* Find the end position skipping `bytes' bytes: */
178         /* first, skip all full-sized elements */
179         tail = bytes;
180         for (niov = 0; niov < iov_cnt && iov[niov].iov_len <= tail; ++niov) {
181             tail -= iov[niov].iov_len;
182         }
183         if (tail) {
184             /* second, fixup the last element, and remember the original
185              * length */
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;
193         } else {
194             ret = do_send_recv(sockfd, iov, niov, do_send);
195         }
196         if (offset) {
197             iov[0].iov_base -= offset;
198             iov[0].iov_len += offset;
199         }
200 
201         if (ret < 0) {
202             assert(errno != EINTR);
203             g_free(local_iov);
204             if (errno == EAGAIN && total > 0) {
205                 return total;
206             }
207             return -1;
208         }
209 
210         if (ret == 0 && !do_send) {
211             /* recv returns 0 when the peer has performed an orderly
212              * shutdown. */
213             break;
214         }
215 
216         /* Prepare for the next iteration */
217         offset += ret;
218         total += ret;
219         bytes -= ret;
220     }
221 
222     g_free(local_iov);
223     return total;
224 }
225 
226 
227 void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
228                  FILE *fp, const char *prefix, size_t limit)
229 {
230     int v;
231     size_t size = 0;
232     char *buf;
233 
234     for (v = 0; v < iov_cnt; v++) {
235         size += iov[v].iov_len;
236     }
237     size = size > limit ? limit : size;
238     buf = g_malloc(size);
239     iov_to_buf(iov, iov_cnt, 0, buf, size);
240     qemu_hexdump(fp, prefix, buf, size);
241     g_free(buf);
242 }
243 
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)
247 {
248     size_t len;
249     unsigned int i, j;
250     for (i = 0, j = 0;
251          i < iov_cnt && j < dst_iov_cnt && (offset || bytes); i++) {
252         if (offset >= iov[i].iov_len) {
253             offset -= iov[i].iov_len;
254             continue;
255         }
256         len = MIN(bytes, iov[i].iov_len - offset);
257 
258         dst_iov[j].iov_base = iov[i].iov_base + offset;
259         dst_iov[j].iov_len = len;
260         j++;
261         bytes -= len;
262         offset = 0;
263     }
264     assert(offset == 0);
265     return j;
266 }
267 
268 /* io vectors */
269 
270 void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint)
271 {
272     qiov->iov = g_new(struct iovec, alloc_hint);
273     qiov->niov = 0;
274     qiov->nalloc = alloc_hint;
275     qiov->size = 0;
276 }
277 
278 void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov)
279 {
280     int i;
281 
282     qiov->iov = iov;
283     qiov->niov = niov;
284     qiov->nalloc = -1;
285     qiov->size = 0;
286     for (i = 0; i < niov; i++)
287         qiov->size += iov[i].iov_len;
288 }
289 
290 void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len)
291 {
292     assert(qiov->nalloc != -1);
293 
294     if (qiov->niov == qiov->nalloc) {
295         qiov->nalloc = 2 * qiov->nalloc + 1;
296         qiov->iov = g_renew(struct iovec, qiov->iov, qiov->nalloc);
297     }
298     qiov->iov[qiov->niov].iov_base = base;
299     qiov->iov[qiov->niov].iov_len = len;
300     qiov->size += len;
301     ++qiov->niov;
302 }
303 
304 /*
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
311  * of src".
312  * Only vector pointers are processed, not the actual data buffers.
313  */
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)
317 {
318     int i;
319     size_t done;
320 
321     if (!sbytes) {
322         return 0;
323     }
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);
329             done += len;
330             soffset = 0;
331         } else {
332             soffset -= src_iov[i].iov_len;
333         }
334     }
335     assert(soffset == 0); /* offset beyond end of src */
336 
337     return done;
338 }
339 
340 /*
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
347  * of src".
348  * Only vector pointers are processed, not the actual data buffers.
349  */
350 void qemu_iovec_concat(QEMUIOVector *dst,
351                        QEMUIOVector *src, size_t soffset, size_t sbytes)
352 {
353     qemu_iovec_concat_iov(dst, src->iov, src->niov, soffset, sbytes);
354 }
355 
356 /*
357  * qiov_find_iov
358  *
359  * Return pointer to iovec structure, where byte at @offset in original vector
360  * @iov exactly is.
361  * Set @remaining_offset to be offset inside that iovec to the same byte.
362  */
363 static struct iovec *iov_skip_offset(struct iovec *iov, size_t offset,
364                                      size_t *remaining_offset)
365 {
366     while (offset > 0 && offset >= iov->iov_len) {
367         offset -= iov->iov_len;
368         iov++;
369     }
370     *remaining_offset = offset;
371 
372     return iov;
373 }
374 
375 /*
376  * qiov_slice
377  *
378  * Find subarray of iovec's, containing requested range. @head would
379  * be offset in first iov (returned by the function), @tail would be
380  * count of extra bytes in last iovec (returned iov + @niov - 1).
381  */
382 static struct iovec *qiov_slice(QEMUIOVector *qiov,
383                                 size_t offset, size_t len,
384                                 size_t *head, size_t *tail, int *niov)
385 {
386     struct iovec *iov, *end_iov;
387 
388     assert(offset + len <= qiov->size);
389 
390     iov = iov_skip_offset(qiov->iov, offset, head);
391     end_iov = iov_skip_offset(iov, *head + len, tail);
392 
393     if (*tail > 0) {
394         assert(*tail < end_iov->iov_len);
395         *tail = end_iov->iov_len - *tail;
396         end_iov++;
397     }
398 
399     *niov = end_iov - iov;
400 
401     return iov;
402 }
403 
404 int qemu_iovec_subvec_niov(QEMUIOVector *qiov, size_t offset, size_t len)
405 {
406     size_t head, tail;
407     int niov;
408 
409     qiov_slice(qiov, offset, len, &head, &tail, &niov);
410 
411     return niov;
412 }
413 
414 /*
415  * Compile new iovec, combining @head_buf buffer, sub-qiov of @mid_qiov,
416  * and @tail_buf buffer into new qiov.
417  */
418 void qemu_iovec_init_extended(
419         QEMUIOVector *qiov,
420         void *head_buf, size_t head_len,
421         QEMUIOVector *mid_qiov, size_t mid_offset, size_t mid_len,
422         void *tail_buf, size_t tail_len)
423 {
424     size_t mid_head, mid_tail;
425     int total_niov, mid_niov = 0;
426     struct iovec *p, *mid_iov = NULL;
427 
428     if (mid_len) {
429         mid_iov = qiov_slice(mid_qiov, mid_offset, mid_len,
430                              &mid_head, &mid_tail, &mid_niov);
431     }
432 
433     total_niov = !!head_len + mid_niov + !!tail_len;
434     if (total_niov == 1) {
435         qemu_iovec_init_buf(qiov, NULL, 0);
436         p = &qiov->local_iov;
437     } else {
438         qiov->niov = qiov->nalloc = total_niov;
439         qiov->size = head_len + mid_len + tail_len;
440         p = qiov->iov = g_new(struct iovec, qiov->niov);
441     }
442 
443     if (head_len) {
444         p->iov_base = head_buf;
445         p->iov_len = head_len;
446         p++;
447     }
448 
449     assert(!mid_niov == !mid_len);
450     if (mid_niov) {
451         memcpy(p, mid_iov, mid_niov * sizeof(*p));
452         p[0].iov_base = (uint8_t *)p[0].iov_base + mid_head;
453         p[0].iov_len -= mid_head;
454         p[mid_niov - 1].iov_len -= mid_tail;
455         p += mid_niov;
456     }
457 
458     if (tail_len) {
459         p->iov_base = tail_buf;
460         p->iov_len = tail_len;
461     }
462 }
463 
464 /*
465  * Check if the contents of subrange of qiov data is all zeroes.
466  */
467 bool qemu_iovec_is_zero(QEMUIOVector *qiov, size_t offset, size_t bytes)
468 {
469     struct iovec *iov;
470     size_t current_offset;
471 
472     assert(offset + bytes <= qiov->size);
473 
474     iov = iov_skip_offset(qiov->iov, offset, &current_offset);
475 
476     while (bytes) {
477         uint8_t *base = (uint8_t *)iov->iov_base + current_offset;
478         size_t len = MIN(iov->iov_len - current_offset, bytes);
479 
480         if (!buffer_is_zero(base, len)) {
481             return false;
482         }
483 
484         current_offset = 0;
485         bytes -= len;
486         iov++;
487     }
488 
489     return true;
490 }
491 
492 void qemu_iovec_init_slice(QEMUIOVector *qiov, QEMUIOVector *source,
493                            size_t offset, size_t len)
494 {
495     qemu_iovec_init_extended(qiov, NULL, 0, source, offset, len, NULL, 0);
496 }
497 
498 void qemu_iovec_destroy(QEMUIOVector *qiov)
499 {
500     if (qiov->nalloc != -1) {
501         g_free(qiov->iov);
502     }
503 
504     memset(qiov, 0, sizeof(*qiov));
505 }
506 
507 void qemu_iovec_reset(QEMUIOVector *qiov)
508 {
509     assert(qiov->nalloc != -1);
510 
511     qiov->niov = 0;
512     qiov->size = 0;
513 }
514 
515 size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
516                          void *buf, size_t bytes)
517 {
518     return iov_to_buf(qiov->iov, qiov->niov, offset, buf, bytes);
519 }
520 
521 size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
522                            const void *buf, size_t bytes)
523 {
524     return iov_from_buf(qiov->iov, qiov->niov, offset, buf, bytes);
525 }
526 
527 size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
528                          int fillc, size_t bytes)
529 {
530     return iov_memset(qiov->iov, qiov->niov, offset, fillc, bytes);
531 }
532 
533 /**
534  * Check that I/O vector contents are identical
535  *
536  * The IO vectors must have the same structure (same length of all parts).
537  * A typical usage is to compare vectors created with qemu_iovec_clone().
538  *
539  * @a:          I/O vector
540  * @b:          I/O vector
541  * @ret:        Offset to first mismatching byte or -1 if match
542  */
543 ssize_t qemu_iovec_compare(QEMUIOVector *a, QEMUIOVector *b)
544 {
545     int i;
546     ssize_t offset = 0;
547 
548     assert(a->niov == b->niov);
549     for (i = 0; i < a->niov; i++) {
550         size_t len = 0;
551         uint8_t *p = (uint8_t *)a->iov[i].iov_base;
552         uint8_t *q = (uint8_t *)b->iov[i].iov_base;
553 
554         assert(a->iov[i].iov_len == b->iov[i].iov_len);
555         while (len < a->iov[i].iov_len && *p++ == *q++) {
556             len++;
557         }
558 
559         offset += len;
560 
561         if (len != a->iov[i].iov_len) {
562             return offset;
563         }
564     }
565     return -1;
566 }
567 
568 typedef struct {
569     int src_index;
570     struct iovec *src_iov;
571     void *dest_base;
572 } IOVectorSortElem;
573 
574 static int sortelem_cmp_src_base(const void *a, const void *b)
575 {
576     const IOVectorSortElem *elem_a = a;
577     const IOVectorSortElem *elem_b = b;
578 
579     /* Don't overflow */
580     if (elem_a->src_iov->iov_base < elem_b->src_iov->iov_base) {
581         return -1;
582     } else if (elem_a->src_iov->iov_base > elem_b->src_iov->iov_base) {
583         return 1;
584     } else {
585         return 0;
586     }
587 }
588 
589 static int sortelem_cmp_src_index(const void *a, const void *b)
590 {
591     const IOVectorSortElem *elem_a = a;
592     const IOVectorSortElem *elem_b = b;
593 
594     return elem_a->src_index - elem_b->src_index;
595 }
596 
597 /**
598  * Copy contents of I/O vector
599  *
600  * The relative relationships of overlapping iovecs are preserved.  This is
601  * necessary to ensure identical semantics in the cloned I/O vector.
602  */
603 void qemu_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src, void *buf)
604 {
605     IOVectorSortElem sortelems[src->niov];
606     void *last_end;
607     int i;
608 
609     /* Sort by source iovecs by base address */
610     for (i = 0; i < src->niov; i++) {
611         sortelems[i].src_index = i;
612         sortelems[i].src_iov = &src->iov[i];
613     }
614     qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_base);
615 
616     /* Allocate buffer space taking into account overlapping iovecs */
617     last_end = NULL;
618     for (i = 0; i < src->niov; i++) {
619         struct iovec *cur = sortelems[i].src_iov;
620         ptrdiff_t rewind = 0;
621 
622         /* Detect overlap */
623         if (last_end && last_end > cur->iov_base) {
624             rewind = last_end - cur->iov_base;
625         }
626 
627         sortelems[i].dest_base = buf - rewind;
628         buf += cur->iov_len - MIN(rewind, cur->iov_len);
629         last_end = MAX(cur->iov_base + cur->iov_len, last_end);
630     }
631 
632     /* Sort by source iovec index and build destination iovec */
633     qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_index);
634     for (i = 0; i < src->niov; i++) {
635         qemu_iovec_add(dest, sortelems[i].dest_base, src->iov[i].iov_len);
636     }
637 }
638 
639 void iov_discard_undo(IOVDiscardUndo *undo)
640 {
641     /* Restore original iovec if it was modified */
642     if (undo->modified_iov) {
643         *undo->modified_iov = undo->orig;
644     }
645 }
646 
647 size_t iov_discard_front_undoable(struct iovec **iov,
648                                   unsigned int *iov_cnt,
649                                   size_t bytes,
650                                   IOVDiscardUndo *undo)
651 {
652     size_t total = 0;
653     struct iovec *cur;
654 
655     if (undo) {
656         undo->modified_iov = NULL;
657     }
658 
659     for (cur = *iov; *iov_cnt > 0; cur++) {
660         if (cur->iov_len > bytes) {
661             if (undo) {
662                 undo->modified_iov = cur;
663                 undo->orig = *cur;
664             }
665 
666             cur->iov_base += bytes;
667             cur->iov_len -= bytes;
668             total += bytes;
669             break;
670         }
671 
672         bytes -= cur->iov_len;
673         total += cur->iov_len;
674         *iov_cnt -= 1;
675     }
676 
677     *iov = cur;
678     return total;
679 }
680 
681 size_t iov_discard_front(struct iovec **iov, unsigned int *iov_cnt,
682                          size_t bytes)
683 {
684     return iov_discard_front_undoable(iov, iov_cnt, bytes, NULL);
685 }
686 
687 size_t iov_discard_back_undoable(struct iovec *iov,
688                                  unsigned int *iov_cnt,
689                                  size_t bytes,
690                                  IOVDiscardUndo *undo)
691 {
692     size_t total = 0;
693     struct iovec *cur;
694 
695     if (undo) {
696         undo->modified_iov = NULL;
697     }
698 
699     if (*iov_cnt == 0) {
700         return 0;
701     }
702 
703     cur = iov + (*iov_cnt - 1);
704 
705     while (*iov_cnt > 0) {
706         if (cur->iov_len > bytes) {
707             if (undo) {
708                 undo->modified_iov = cur;
709                 undo->orig = *cur;
710             }
711 
712             cur->iov_len -= bytes;
713             total += bytes;
714             break;
715         }
716 
717         bytes -= cur->iov_len;
718         total += cur->iov_len;
719         cur--;
720         *iov_cnt -= 1;
721     }
722 
723     return total;
724 }
725 
726 size_t iov_discard_back(struct iovec *iov, unsigned int *iov_cnt,
727                         size_t bytes)
728 {
729     return iov_discard_back_undoable(iov, iov_cnt, bytes, NULL);
730 }
731 
732 void qemu_iovec_discard_back(QEMUIOVector *qiov, size_t bytes)
733 {
734     size_t total;
735     unsigned int niov = qiov->niov;
736 
737     assert(qiov->size >= bytes);
738     total = iov_discard_back(qiov->iov, &niov, bytes);
739     assert(total == bytes);
740 
741     qiov->niov = niov;
742     qiov->size -= bytes;
743 }
744