blob: 74e6ca8ed7298c833e52257923c10fb73c9377ad [file] [log] [blame]
Amit Shahe4d56392010-04-27 18:04:05 +05301/*
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>
Michael Tokarev2278a692012-06-07 20:08:19 +040010 * Michael Tokarev <mjt@tls.msk.ru>
Amit Shahe4d56392010-04-27 18:04:05 +053011 *
12 * This work is licensed under the terms of the GNU GPL, version 2. See
13 * the COPYING file in the top-level directory.
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010014 *
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.
Amit Shahe4d56392010-04-27 18:04:05 +053017 */
18
Peter Maydellaafd7582016-01-29 17:49:55 +000019#include "qemu/osdep.h"
Markus Armbrusterdaf015e2016-03-09 10:52:44 +010020#include "qemu-common.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010021#include "qemu/iov.h"
Stefan Weilcc99c6f2014-02-23 18:02:07 +010022#include "qemu/sockets.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020023#include "qemu/cutils.h"
Michael Tokarev25e5e4c2012-03-14 11:18:54 +040024
Paolo Bonziniad523bc2015-12-22 12:03:33 +010025size_t iov_from_buf_full(const struct iovec *iov, unsigned int iov_cnt,
26 size_t offset, const void *buf, size_t bytes)
Amit Shahe4d56392010-04-27 18:04:05 +053027{
Michael Tokarev2278a692012-06-07 20:08:19 +040028 size_t done;
Amit Shahe4d56392010-04-27 18:04:05 +053029 unsigned int i;
Michael Tokarev2278a692012-06-07 20:08:19 +040030 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;
Amit Shahfa6111f2010-04-27 18:04:06 +053038 }
Amit Shahfa6111f2010-04-27 18:04:06 +053039 }
Michael Tokarev2278a692012-06-07 20:08:19 +040040 assert(offset == 0);
41 return done;
Amit Shahfa6111f2010-04-27 18:04:06 +053042}
43
Paolo Bonziniad523bc2015-12-22 12:03:33 +010044size_t iov_to_buf_full(const struct iovec *iov, const unsigned int iov_cnt,
45 size_t offset, void *buf, size_t bytes)
Hannes Reinecke348e7b82011-07-11 15:02:23 +020046{
Michael Tokarev2278a692012-06-07 20:08:19 +040047 size_t done;
Hannes Reinecke348e7b82011-07-11 15:02:23 +020048 unsigned int i;
Michael Tokarev2278a692012-06-07 20:08:19 +040049 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;
Hannes Reinecke348e7b82011-07-11 15:02:23 +020057 }
Hannes Reinecke348e7b82011-07-11 15:02:23 +020058 }
Michael Tokarev2278a692012-06-07 20:08:19 +040059 assert(offset == 0);
60 return done;
Hannes Reinecke348e7b82011-07-11 15:02:23 +020061}
62
Michael Tokarevdcf6f5e2012-03-11 18:05:12 +040063size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt,
Michael Tokarev2278a692012-06-07 20:08:19 +040064 size_t offset, int fillc, size_t bytes)
Gerd Hoffmann8d150282011-07-13 15:16:08 +020065{
Michael Tokarev2278a692012-06-07 20:08:19 +040066 size_t done;
Gerd Hoffmann8d150282011-07-13 15:16:08 +020067 unsigned int i;
Michael Tokarev2278a692012-06-07 20:08:19 +040068 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;
Gerd Hoffmann8d150282011-07-13 15:16:08 +020076 }
Gerd Hoffmann8d150282011-07-13 15:16:08 +020077 }
Michael Tokarev2278a692012-06-07 20:08:19 +040078 assert(offset == 0);
79 return done;
Gerd Hoffmann8d150282011-07-13 15:16:08 +020080}
81
Hannes Reinecke348e7b82011-07-11 15:02:23 +020082size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt)
Amit Shahfa6111f2010-04-27 18:04:06 +053083{
84 size_t len;
85 unsigned int i;
86
87 len = 0;
Hannes Reinecke348e7b82011-07-11 15:02:23 +020088 for (i = 0; i < iov_cnt; i++) {
Amit Shahfa6111f2010-04-27 18:04:06 +053089 len += iov[i].iov_len;
90 }
91 return len;
92}
Gerd Hoffmann3a1dca92011-07-12 13:35:10 +020093
Michael Tokarev25e5e4c2012-03-14 11:18:54 +040094/* helper function for iov_send_recv() */
95static ssize_t
96do_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt, bool do_send)
97{
Paolo Bonzini9adea5f2013-04-21 12:01:06 +020098#ifdef CONFIG_POSIX
Michael Tokarev25e5e4c2012-03-14 11:18:54 +040099 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() */
Stefan Weilc0958552012-07-11 07:09:05 +0200113 unsigned i = 0;
114 ssize_t ret = 0;
115 while (i < iov_cnt) {
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400116 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) {
Stefan Weilc0958552012-07-11 07:09:05 +0200129 ret = -1;
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400130 }
131 break;
132 }
Stefan Weilc0958552012-07-11 07:09:05 +0200133 i++;
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400134 }
Stefan Weilc0958552012-07-11 07:09:05 +0200135 return ret;
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400136#endif
137}
138
Wen Congyang6b646402015-05-21 09:50:10 +0800139ssize_t iov_send_recv(int sockfd, const struct iovec *_iov, unsigned iov_cnt,
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400140 size_t offset, size_t bytes,
141 bool do_send)
142{
Paolo Bonzini83f75c22013-03-27 17:36:31 +0100143 ssize_t total = 0;
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400144 ssize_t ret;
Paolo Bonzini5209d672013-03-27 17:36:29 +0100145 size_t orig_len, tail;
Paolo Bonzinif48869a2013-03-27 17:36:30 +0100146 unsigned niov;
Wen Congyang6b646402015-05-21 09:50:10 +0800147 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;
Paolo Bonzini5209d672013-03-27 17:36:29 +0100157
Paolo Bonzini83f75c22013-03-27 17:36:31 +0100158 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 }
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400164
Paolo Bonzini83f75c22013-03-27 17:36:31 +0100165 /* niov == iov_cnt would only be valid if bytes == 0, which
166 * we already ruled out in the loop condition. */
Paolo Bonzinif48869a2013-03-27 17:36:30 +0100167 assert(niov < iov_cnt);
Paolo Bonzini83f75c22013-03-27 17:36:31 +0100168 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;
Michael Tokarev2be178a2013-09-14 13:11:36 +0400190 ret = do_send_recv(sockfd, iov, niov, do_send);
191 /* Undo the changes above before checking for errors */
Paolo Bonzini83f75c22013-03-27 17:36:31 +0100192 iov[niov-1].iov_len = orig_len;
Michael Tokarev2be178a2013-09-14 13:11:36 +0400193 } else {
194 ret = do_send_recv(sockfd, iov, niov, do_send);
Paolo Bonzini83f75c22013-03-27 17:36:31 +0100195 }
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);
Wen Congyang6b646402015-05-21 09:50:10 +0800203 g_free(local_iov);
Paolo Bonzini83f75c22013-03-27 17:36:31 +0100204 if (errno == EAGAIN && total > 0) {
205 return total;
206 }
207 return -1;
208 }
209
MORITA Kazutaka84004292013-07-23 17:30:12 +0900210 if (ret == 0 && !do_send) {
211 /* recv returns 0 when the peer has performed an orderly
212 * shutdown. */
213 break;
214 }
215
Paolo Bonzini83f75c22013-03-27 17:36:31 +0100216 /* Prepare for the next iteration */
217 offset += ret;
218 total += ret;
219 bytes -= ret;
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400220 }
221
Wen Congyang6b646402015-05-21 09:50:10 +0800222 g_free(local_iov);
Paolo Bonzini83f75c22013-03-27 17:36:31 +0100223 return total;
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400224}
225
226
Gerd Hoffmann3a1dca92011-07-12 13:35:10 +0200227void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
228 FILE *fp, const char *prefix, size_t limit)
229{
Peter Crosthwaite6ff66f52013-03-15 16:41:58 +0000230 int v;
231 size_t size = 0;
232 char *buf;
Gerd Hoffmann3a1dca92011-07-12 13:35:10 +0200233
Peter Crosthwaite6ff66f52013-03-15 16:41:58 +0000234 for (v = 0; v < iov_cnt; v++) {
235 size += iov[v].iov_len;
Gerd Hoffmann3a1dca92011-07-12 13:35:10 +0200236 }
Peter Crosthwaite6ff66f52013-03-15 16:41:58 +0000237 size = size > limit ? limit : size;
238 buf = g_malloc(size);
239 iov_to_buf(iov, iov_cnt, 0, buf, size);
Ed Maste3568ac22013-05-16 11:32:28 -0400240 qemu_hexdump(buf, fp, prefix, size);
Peter Crosthwaite6ff66f52013-03-15 16:41:58 +0000241 g_free(buf);
Gerd Hoffmann3a1dca92011-07-12 13:35:10 +0200242}
Paolo Bonzini01912532012-10-24 14:58:39 +0200243
Michael S. Tsirkind3363362012-09-24 13:02:52 +0200244unsigned 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;
Shmulik Ladkanie9117652016-08-02 12:41:20 +0300250 for (i = 0, j = 0;
251 i < iov_cnt && j < dst_iov_cnt && (offset || bytes); i++) {
Michael S. Tsirkind3363362012-09-24 13:02:52 +0200252 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}
Paolo Bonzinif563a5d2012-10-31 10:42:51 +0100267
Paolo Bonzini01912532012-10-24 14:58:39 +0200268/* io vectors */
269
270void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint)
271{
Markus Armbrustere1cf5582014-12-04 15:00:03 +0100272 qiov->iov = g_new(struct iovec, alloc_hint);
Paolo Bonzini01912532012-10-24 14:58:39 +0200273 qiov->niov = 0;
274 qiov->nalloc = alloc_hint;
275 qiov->size = 0;
276}
277
278void 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
290void 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;
Markus Armbrustere1cf5582014-12-04 15:00:03 +0100296 qiov->iov = g_renew(struct iovec, qiov->iov, qiov->nalloc);
Paolo Bonzini01912532012-10-24 14:58:39 +0200297 }
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/*
Stefan Hajnoczi530c0bb2012-11-22 16:06:06 +0100305 * 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 */
Paolo Bonzini519661e2014-06-10 16:21:28 +0200314size_t qemu_iovec_concat_iov(QEMUIOVector *dst,
315 struct iovec *src_iov, unsigned int src_cnt,
316 size_t soffset, size_t sbytes)
Stefan Hajnoczi530c0bb2012-11-22 16:06:06 +0100317{
318 int i;
319 size_t done;
Aneesh Kumar K.Vfacf98a2013-02-05 11:27:45 +0530320
321 if (!sbytes) {
Paolo Bonzini519661e2014-06-10 16:21:28 +0200322 return 0;
Aneesh Kumar K.Vfacf98a2013-02-05 11:27:45 +0530323 }
Stefan Hajnoczi530c0bb2012-11-22 16:06:06 +0100324 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 */
Paolo Bonzini519661e2014-06-10 16:21:28 +0200336
337 return done;
Stefan Hajnoczi530c0bb2012-11-22 16:06:06 +0100338}
339
340/*
Paolo Bonzini01912532012-10-24 14:58:39 +0200341 * 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 */
350void qemu_iovec_concat(QEMUIOVector *dst,
351 QEMUIOVector *src, size_t soffset, size_t sbytes)
352{
Stefan Hajnoczi530c0bb2012-11-22 16:06:06 +0100353 qemu_iovec_concat_iov(dst, src->iov, src->niov, soffset, sbytes);
Paolo Bonzini01912532012-10-24 14:58:39 +0200354}
355
Peter Lieven43f35cb2014-05-18 00:58:17 +0200356/*
357 * Check if the contents of the iovecs are all zero
358 */
359bool qemu_iovec_is_zero(QEMUIOVector *qiov)
360{
361 int i;
362 for (i = 0; i < qiov->niov; i++) {
363 size_t offs = QEMU_ALIGN_DOWN(qiov->iov[i].iov_len, 4 * sizeof(long));
364 uint8_t *ptr = qiov->iov[i].iov_base;
365 if (offs && !buffer_is_zero(qiov->iov[i].iov_base, offs)) {
366 return false;
367 }
368 for (; offs < qiov->iov[i].iov_len; offs++) {
369 if (ptr[offs]) {
370 return false;
371 }
372 }
373 }
374 return true;
375}
376
Paolo Bonzini01912532012-10-24 14:58:39 +0200377void qemu_iovec_destroy(QEMUIOVector *qiov)
378{
379 assert(qiov->nalloc != -1);
380
381 qemu_iovec_reset(qiov);
382 g_free(qiov->iov);
383 qiov->nalloc = 0;
384 qiov->iov = NULL;
385}
386
387void qemu_iovec_reset(QEMUIOVector *qiov)
388{
389 assert(qiov->nalloc != -1);
390
391 qiov->niov = 0;
392 qiov->size = 0;
393}
394
395size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
396 void *buf, size_t bytes)
397{
398 return iov_to_buf(qiov->iov, qiov->niov, offset, buf, bytes);
399}
400
401size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
402 const void *buf, size_t bytes)
403{
404 return iov_from_buf(qiov->iov, qiov->niov, offset, buf, bytes);
405}
406
407size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
408 int fillc, size_t bytes)
409{
410 return iov_memset(qiov->iov, qiov->niov, offset, fillc, bytes);
411}
Stefan Hajnoczid0277632012-11-21 17:41:10 +0100412
Benoît Canetf70d7f72014-02-21 22:21:13 +0100413/**
414 * Check that I/O vector contents are identical
415 *
416 * The IO vectors must have the same structure (same length of all parts).
417 * A typical usage is to compare vectors created with qemu_iovec_clone().
418 *
419 * @a: I/O vector
420 * @b: I/O vector
421 * @ret: Offset to first mismatching byte or -1 if match
422 */
423ssize_t qemu_iovec_compare(QEMUIOVector *a, QEMUIOVector *b)
424{
425 int i;
426 ssize_t offset = 0;
427
428 assert(a->niov == b->niov);
429 for (i = 0; i < a->niov; i++) {
430 size_t len = 0;
431 uint8_t *p = (uint8_t *)a->iov[i].iov_base;
432 uint8_t *q = (uint8_t *)b->iov[i].iov_base;
433
434 assert(a->iov[i].iov_len == b->iov[i].iov_len);
435 while (len < a->iov[i].iov_len && *p++ == *q++) {
436 len++;
437 }
438
439 offset += len;
440
441 if (len != a->iov[i].iov_len) {
442 return offset;
443 }
444 }
445 return -1;
446}
447
448typedef struct {
449 int src_index;
450 struct iovec *src_iov;
451 void *dest_base;
452} IOVectorSortElem;
453
454static int sortelem_cmp_src_base(const void *a, const void *b)
455{
456 const IOVectorSortElem *elem_a = a;
457 const IOVectorSortElem *elem_b = b;
458
459 /* Don't overflow */
460 if (elem_a->src_iov->iov_base < elem_b->src_iov->iov_base) {
461 return -1;
462 } else if (elem_a->src_iov->iov_base > elem_b->src_iov->iov_base) {
463 return 1;
464 } else {
465 return 0;
466 }
467}
468
469static int sortelem_cmp_src_index(const void *a, const void *b)
470{
471 const IOVectorSortElem *elem_a = a;
472 const IOVectorSortElem *elem_b = b;
473
474 return elem_a->src_index - elem_b->src_index;
475}
476
477/**
478 * Copy contents of I/O vector
479 *
480 * The relative relationships of overlapping iovecs are preserved. This is
481 * necessary to ensure identical semantics in the cloned I/O vector.
482 */
483void qemu_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src, void *buf)
484{
485 IOVectorSortElem sortelems[src->niov];
486 void *last_end;
487 int i;
488
489 /* Sort by source iovecs by base address */
490 for (i = 0; i < src->niov; i++) {
491 sortelems[i].src_index = i;
492 sortelems[i].src_iov = &src->iov[i];
493 }
494 qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_base);
495
496 /* Allocate buffer space taking into account overlapping iovecs */
497 last_end = NULL;
498 for (i = 0; i < src->niov; i++) {
499 struct iovec *cur = sortelems[i].src_iov;
500 ptrdiff_t rewind = 0;
501
502 /* Detect overlap */
503 if (last_end && last_end > cur->iov_base) {
504 rewind = last_end - cur->iov_base;
505 }
506
507 sortelems[i].dest_base = buf - rewind;
508 buf += cur->iov_len - MIN(rewind, cur->iov_len);
509 last_end = MAX(cur->iov_base + cur->iov_len, last_end);
510 }
511
512 /* Sort by source iovec index and build destination iovec */
513 qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_index);
514 for (i = 0; i < src->niov; i++) {
515 qemu_iovec_add(dest, sortelems[i].dest_base, src->iov[i].iov_len);
516 }
517}
518
Stefan Hajnoczid0277632012-11-21 17:41:10 +0100519size_t iov_discard_front(struct iovec **iov, unsigned int *iov_cnt,
520 size_t bytes)
521{
522 size_t total = 0;
523 struct iovec *cur;
524
525 for (cur = *iov; *iov_cnt > 0; cur++) {
526 if (cur->iov_len > bytes) {
527 cur->iov_base += bytes;
528 cur->iov_len -= bytes;
529 total += bytes;
530 break;
531 }
532
533 bytes -= cur->iov_len;
534 total += cur->iov_len;
535 *iov_cnt -= 1;
536 }
537
538 *iov = cur;
539 return total;
540}
541
542size_t iov_discard_back(struct iovec *iov, unsigned int *iov_cnt,
543 size_t bytes)
544{
545 size_t total = 0;
546 struct iovec *cur;
547
548 if (*iov_cnt == 0) {
549 return 0;
550 }
551
552 cur = iov + (*iov_cnt - 1);
553
554 while (*iov_cnt > 0) {
555 if (cur->iov_len > bytes) {
556 cur->iov_len -= bytes;
557 total += bytes;
558 break;
559 }
560
561 bytes -= cur->iov_len;
562 total += cur->iov_len;
563 cur--;
564 *iov_cnt -= 1;
565 }
566
567 return total;
568}
Kevin Wolf58f423f2014-07-09 19:17:30 +0200569
570void qemu_iovec_discard_back(QEMUIOVector *qiov, size_t bytes)
571{
572 size_t total;
573 unsigned int niov = qiov->niov;
574
575 assert(qiov->size >= bytes);
576 total = iov_discard_back(qiov->iov, &niov, bytes);
577 assert(total == bytes);
578
579 qiov->niov = niov;
580 qiov->size -= bytes;
581}