xref: /openbmc/qemu/util/iov.c (revision 4771d756f46219762477aaeaaef9bd215e3d5c60)
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 
24 size_t iov_from_buf_full(const struct iovec *iov, unsigned int iov_cnt,
25                          size_t offset, const void *buf, size_t bytes)
26 {
27     size_t done;
28     unsigned int i;
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;
37         }
38     }
39     assert(offset == 0);
40     return done;
41 }
42 
43 size_t iov_to_buf_full(const struct iovec *iov, const unsigned int iov_cnt,
44                        size_t offset, void *buf, size_t bytes)
45 {
46     size_t done;
47     unsigned int i;
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;
56         }
57     }
58     assert(offset == 0);
59     return done;
60 }
61 
62 size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt,
63                   size_t offset, int fillc, size_t bytes)
64 {
65     size_t done;
66     unsigned int i;
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;
75         }
76     }
77     assert(offset == 0);
78     return done;
79 }
80 
81 size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt)
82 {
83     size_t len;
84     unsigned int i;
85 
86     len = 0;
87     for (i = 0; i < iov_cnt; i++) {
88         len += iov[i].iov_len;
89     }
90     return len;
91 }
92 
93 /* helper function for iov_send_recv() */
94 static ssize_t
95 do_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt, bool do_send)
96 {
97 #ifdef CONFIG_POSIX
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() */
112     unsigned i = 0;
113     ssize_t ret = 0;
114     while (i < iov_cnt) {
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) {
128                 ret = -1;
129             }
130             break;
131         }
132         i++;
133     }
134     return ret;
135 #endif
136 }
137 
138 ssize_t iov_send_recv(int sockfd, const struct iovec *_iov, unsigned iov_cnt,
139                       size_t offset, size_t bytes,
140                       bool do_send)
141 {
142     ssize_t total = 0;
143     ssize_t ret;
144     size_t orig_len, tail;
145     unsigned niov;
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;
156 
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         }
163 
164         /* niov == iov_cnt would only be valid if bytes == 0, which
165          * we already ruled out in the loop condition.  */
166         assert(niov < iov_cnt);
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;
189             ret = do_send_recv(sockfd, iov, niov, do_send);
190             /* Undo the changes above before checking for errors */
191             iov[niov-1].iov_len = orig_len;
192         } else {
193             ret = do_send_recv(sockfd, iov, niov, do_send);
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);
202             g_free(local_iov);
203             if (errno == EAGAIN && total > 0) {
204                 return total;
205             }
206             return -1;
207         }
208 
209         if (ret == 0 && !do_send) {
210             /* recv returns 0 when the peer has performed an orderly
211              * shutdown. */
212             break;
213         }
214 
215         /* Prepare for the next iteration */
216         offset += ret;
217         total += ret;
218         bytes -= ret;
219     }
220 
221     g_free(local_iov);
222     return total;
223 }
224 
225 
226 void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
227                  FILE *fp, const char *prefix, size_t limit)
228 {
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;
235     }
236     size = size > limit ? limit : size;
237     buf = g_malloc(size);
238     iov_to_buf(iov, iov_cnt, 0, buf, size);
239     qemu_hexdump(buf, fp, prefix, size);
240     g_free(buf);
241 }
242 
243 unsigned 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 }
265 
266 /* io vectors */
267 
268 void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint)
269 {
270     qiov->iov = g_new(struct iovec, alloc_hint);
271     qiov->niov = 0;
272     qiov->nalloc = alloc_hint;
273     qiov->size = 0;
274 }
275 
276 void 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 
288 void 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;
294         qiov->iov = g_renew(struct iovec, qiov->iov, qiov->nalloc);
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 /*
303  * Concatenates (partial) iovecs from src_iov to the end of dst.
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
307  * of src_iov if it comes first.  This way, it is okay to specify
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  */
312 size_t qemu_iovec_concat_iov(QEMUIOVector *dst,
313                              struct iovec *src_iov, unsigned int src_cnt,
314                              size_t soffset, size_t sbytes)
315 {
316     int i;
317     size_t done;
318 
319     if (!sbytes) {
320         return 0;
321     }
322     assert(dst->nalloc != -1);
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);
327             done += len;
328             soffset = 0;
329         } else {
330             soffset -= src_iov[i].iov_len;
331         }
332     }
333     assert(soffset == 0); /* offset beyond end of src */
334 
335     return done;
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  */
348 void 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);
352 }
353 
354 /*
355  * Check if the contents of the iovecs are all zero
356  */
357 bool 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 
375 void 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 
385 void qemu_iovec_reset(QEMUIOVector *qiov)
386 {
387     assert(qiov->nalloc != -1);
388 
389     qiov->niov = 0;
390     qiov->size = 0;
391 }
392 
393 size_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 
399 size_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 
405 size_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 }
410 
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  */
421 ssize_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 
446 typedef struct {
447     int src_index;
448     struct iovec *src_iov;
449     void *dest_base;
450 } IOVectorSortElem;
451 
452 static 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 
467 static 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  */
481 void 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 
517 size_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 
540 size_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 }
567 
568 void 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 }
580