xref: /openbmc/qemu/util/iov.c (revision 8c1f72da)
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/iov.h"
20 
21 #ifdef _WIN32
22 # include <windows.h>
23 # include <winsock2.h>
24 #else
25 # include <sys/types.h>
26 # include <sys/socket.h>
27 #endif
28 
29 size_t iov_from_buf(const struct iovec *iov, unsigned int iov_cnt,
30                     size_t offset, const void *buf, size_t bytes)
31 {
32     size_t done;
33     unsigned int i;
34     for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) {
35         if (offset < iov[i].iov_len) {
36             size_t len = MIN(iov[i].iov_len - offset, bytes - done);
37             memcpy(iov[i].iov_base + offset, buf + done, len);
38             done += len;
39             offset = 0;
40         } else {
41             offset -= iov[i].iov_len;
42         }
43     }
44     assert(offset == 0);
45     return done;
46 }
47 
48 size_t iov_to_buf(const struct iovec *iov, const unsigned int iov_cnt,
49                   size_t offset, void *buf, size_t bytes)
50 {
51     size_t done;
52     unsigned int i;
53     for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) {
54         if (offset < iov[i].iov_len) {
55             size_t len = MIN(iov[i].iov_len - offset, bytes - done);
56             memcpy(buf + done, iov[i].iov_base + offset, len);
57             done += len;
58             offset = 0;
59         } else {
60             offset -= iov[i].iov_len;
61         }
62     }
63     assert(offset == 0);
64     return done;
65 }
66 
67 size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt,
68                   size_t offset, int fillc, size_t bytes)
69 {
70     size_t done;
71     unsigned int i;
72     for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) {
73         if (offset < iov[i].iov_len) {
74             size_t len = MIN(iov[i].iov_len - offset, bytes - done);
75             memset(iov[i].iov_base + offset, fillc, len);
76             done += len;
77             offset = 0;
78         } else {
79             offset -= iov[i].iov_len;
80         }
81     }
82     assert(offset == 0);
83     return done;
84 }
85 
86 size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt)
87 {
88     size_t len;
89     unsigned int i;
90 
91     len = 0;
92     for (i = 0; i < iov_cnt; i++) {
93         len += iov[i].iov_len;
94     }
95     return len;
96 }
97 
98 /* helper function for iov_send_recv() */
99 static ssize_t
100 do_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt, bool do_send)
101 {
102 #ifdef CONFIG_POSIX
103     ssize_t ret;
104     struct msghdr msg;
105     memset(&msg, 0, sizeof(msg));
106     msg.msg_iov = iov;
107     msg.msg_iovlen = iov_cnt;
108     do {
109         ret = do_send
110             ? sendmsg(sockfd, &msg, 0)
111             : recvmsg(sockfd, &msg, 0);
112     } while (ret < 0 && errno == EINTR);
113     return ret;
114 #else
115     /* else send piece-by-piece */
116     /*XXX Note: windows has WSASend() and WSARecv() */
117     unsigned i = 0;
118     ssize_t ret = 0;
119     while (i < iov_cnt) {
120         ssize_t r = do_send
121             ? send(sockfd, iov[i].iov_base, iov[i].iov_len, 0)
122             : recv(sockfd, iov[i].iov_base, iov[i].iov_len, 0);
123         if (r > 0) {
124             ret += r;
125         } else if (!r) {
126             break;
127         } else if (errno == EINTR) {
128             continue;
129         } else {
130             /* else it is some "other" error,
131              * only return if there was no data processed. */
132             if (ret == 0) {
133                 ret = -1;
134             }
135             break;
136         }
137         i++;
138     }
139     return ret;
140 #endif
141 }
142 
143 ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt,
144                       size_t offset, size_t bytes,
145                       bool do_send)
146 {
147     ssize_t total = 0;
148     ssize_t ret;
149     size_t orig_len, tail;
150     unsigned niov;
151 
152     while (bytes > 0) {
153         /* Find the start position, skipping `offset' bytes:
154          * first, skip all full-sized vector elements, */
155         for (niov = 0; niov < iov_cnt && offset >= iov[niov].iov_len; ++niov) {
156             offset -= iov[niov].iov_len;
157         }
158 
159         /* niov == iov_cnt would only be valid if bytes == 0, which
160          * we already ruled out in the loop condition.  */
161         assert(niov < iov_cnt);
162         iov += niov;
163         iov_cnt -= niov;
164 
165         if (offset) {
166             /* second, skip `offset' bytes from the (now) first element,
167              * undo it on exit */
168             iov[0].iov_base += offset;
169             iov[0].iov_len -= offset;
170         }
171         /* Find the end position skipping `bytes' bytes: */
172         /* first, skip all full-sized elements */
173         tail = bytes;
174         for (niov = 0; niov < iov_cnt && iov[niov].iov_len <= tail; ++niov) {
175             tail -= iov[niov].iov_len;
176         }
177         if (tail) {
178             /* second, fixup the last element, and remember the original
179              * length */
180             assert(niov < iov_cnt);
181             assert(iov[niov].iov_len > tail);
182             orig_len = iov[niov].iov_len;
183             iov[niov++].iov_len = tail;
184         }
185 
186         ret = do_send_recv(sockfd, iov, niov, do_send);
187 
188         /* Undo the changes above before checking for errors */
189         if (tail) {
190             iov[niov-1].iov_len = orig_len;
191         }
192         if (offset) {
193             iov[0].iov_base -= offset;
194             iov[0].iov_len += offset;
195         }
196 
197         if (ret < 0) {
198             assert(errno != EINTR);
199             if (errno == EAGAIN && total > 0) {
200                 return total;
201             }
202             return -1;
203         }
204 
205         /* Prepare for the next iteration */
206         offset += ret;
207         total += ret;
208         bytes -= ret;
209     }
210 
211     return total;
212 }
213 
214 
215 void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
216                  FILE *fp, const char *prefix, size_t limit)
217 {
218     int v;
219     size_t size = 0;
220     char *buf;
221 
222     for (v = 0; v < iov_cnt; v++) {
223         size += iov[v].iov_len;
224     }
225     size = size > limit ? limit : size;
226     buf = g_malloc(size);
227     iov_to_buf(iov, iov_cnt, 0, buf, size);
228     qemu_hexdump(buf, fp, prefix, size);
229     g_free(buf);
230 }
231 
232 unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt,
233                  const struct iovec *iov, unsigned int iov_cnt,
234                  size_t offset, size_t bytes)
235 {
236     size_t len;
237     unsigned int i, j;
238     for (i = 0, j = 0; i < iov_cnt && j < dst_iov_cnt && bytes; i++) {
239         if (offset >= iov[i].iov_len) {
240             offset -= iov[i].iov_len;
241             continue;
242         }
243         len = MIN(bytes, iov[i].iov_len - offset);
244 
245         dst_iov[j].iov_base = iov[i].iov_base + offset;
246         dst_iov[j].iov_len = len;
247         j++;
248         bytes -= len;
249         offset = 0;
250     }
251     assert(offset == 0);
252     return j;
253 }
254 
255 /* io vectors */
256 
257 void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint)
258 {
259     qiov->iov = g_malloc(alloc_hint * sizeof(struct iovec));
260     qiov->niov = 0;
261     qiov->nalloc = alloc_hint;
262     qiov->size = 0;
263 }
264 
265 void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov)
266 {
267     int i;
268 
269     qiov->iov = iov;
270     qiov->niov = niov;
271     qiov->nalloc = -1;
272     qiov->size = 0;
273     for (i = 0; i < niov; i++)
274         qiov->size += iov[i].iov_len;
275 }
276 
277 void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len)
278 {
279     assert(qiov->nalloc != -1);
280 
281     if (qiov->niov == qiov->nalloc) {
282         qiov->nalloc = 2 * qiov->nalloc + 1;
283         qiov->iov = g_realloc(qiov->iov, qiov->nalloc * sizeof(struct iovec));
284     }
285     qiov->iov[qiov->niov].iov_base = base;
286     qiov->iov[qiov->niov].iov_len = len;
287     qiov->size += len;
288     ++qiov->niov;
289 }
290 
291 /*
292  * Concatenates (partial) iovecs from src_iov to the end of dst.
293  * It starts copying after skipping `soffset' bytes at the
294  * beginning of src and adds individual vectors from src to
295  * dst copies up to `sbytes' bytes total, or up to the end
296  * of src_iov if it comes first.  This way, it is okay to specify
297  * very large value for `sbytes' to indicate "up to the end
298  * of src".
299  * Only vector pointers are processed, not the actual data buffers.
300  */
301 void qemu_iovec_concat_iov(QEMUIOVector *dst,
302                            struct iovec *src_iov, unsigned int src_cnt,
303                            size_t soffset, size_t sbytes)
304 {
305     int i;
306     size_t done;
307 
308     if (!sbytes) {
309         return;
310     }
311     assert(dst->nalloc != -1);
312     for (i = 0, done = 0; done < sbytes && i < src_cnt; i++) {
313         if (soffset < src_iov[i].iov_len) {
314             size_t len = MIN(src_iov[i].iov_len - soffset, sbytes - done);
315             qemu_iovec_add(dst, src_iov[i].iov_base + soffset, len);
316             done += len;
317             soffset = 0;
318         } else {
319             soffset -= src_iov[i].iov_len;
320         }
321     }
322     assert(soffset == 0); /* offset beyond end of src */
323 }
324 
325 /*
326  * Concatenates (partial) iovecs from src to the end of dst.
327  * It starts copying after skipping `soffset' bytes at the
328  * beginning of src and adds individual vectors from src to
329  * dst copies up to `sbytes' bytes total, or up to the end
330  * of src if it comes first.  This way, it is okay to specify
331  * very large value for `sbytes' to indicate "up to the end
332  * of src".
333  * Only vector pointers are processed, not the actual data buffers.
334  */
335 void qemu_iovec_concat(QEMUIOVector *dst,
336                        QEMUIOVector *src, size_t soffset, size_t sbytes)
337 {
338     qemu_iovec_concat_iov(dst, src->iov, src->niov, soffset, sbytes);
339 }
340 
341 void qemu_iovec_destroy(QEMUIOVector *qiov)
342 {
343     assert(qiov->nalloc != -1);
344 
345     qemu_iovec_reset(qiov);
346     g_free(qiov->iov);
347     qiov->nalloc = 0;
348     qiov->iov = NULL;
349 }
350 
351 void qemu_iovec_reset(QEMUIOVector *qiov)
352 {
353     assert(qiov->nalloc != -1);
354 
355     qiov->niov = 0;
356     qiov->size = 0;
357 }
358 
359 size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
360                          void *buf, size_t bytes)
361 {
362     return iov_to_buf(qiov->iov, qiov->niov, offset, buf, bytes);
363 }
364 
365 size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
366                            const void *buf, size_t bytes)
367 {
368     return iov_from_buf(qiov->iov, qiov->niov, offset, buf, bytes);
369 }
370 
371 size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
372                          int fillc, size_t bytes)
373 {
374     return iov_memset(qiov->iov, qiov->niov, offset, fillc, bytes);
375 }
376 
377 size_t iov_discard_front(struct iovec **iov, unsigned int *iov_cnt,
378                          size_t bytes)
379 {
380     size_t total = 0;
381     struct iovec *cur;
382 
383     for (cur = *iov; *iov_cnt > 0; cur++) {
384         if (cur->iov_len > bytes) {
385             cur->iov_base += bytes;
386             cur->iov_len -= bytes;
387             total += bytes;
388             break;
389         }
390 
391         bytes -= cur->iov_len;
392         total += cur->iov_len;
393         *iov_cnt -= 1;
394     }
395 
396     *iov = cur;
397     return total;
398 }
399 
400 size_t iov_discard_back(struct iovec *iov, unsigned int *iov_cnt,
401                         size_t bytes)
402 {
403     size_t total = 0;
404     struct iovec *cur;
405 
406     if (*iov_cnt == 0) {
407         return 0;
408     }
409 
410     cur = iov + (*iov_cnt - 1);
411 
412     while (*iov_cnt > 0) {
413         if (cur->iov_len > bytes) {
414             cur->iov_len -= bytes;
415             total += bytes;
416             break;
417         }
418 
419         bytes -= cur->iov_len;
420         total += cur->iov_len;
421         cur--;
422         *iov_cnt -= 1;
423     }
424 
425     return total;
426 }
427