blob: 22d6996ccec037e66d6757fd1879ca78a3490077 [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"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010020#include "qemu/iov.h"
Stefan Weilcc99c6f2014-02-23 18:02:07 +010021#include "qemu/sockets.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020022#include "qemu/cutils.h"
Michael Tokarev25e5e4c2012-03-14 11:18:54 +040023
Paolo Bonziniad523bc2015-12-22 12:03:33 +010024size_t iov_from_buf_full(const struct iovec *iov, unsigned int iov_cnt,
25 size_t offset, const void *buf, size_t bytes)
Amit Shahe4d56392010-04-27 18:04:05 +053026{
Michael Tokarev2278a692012-06-07 20:08:19 +040027 size_t done;
Amit Shahe4d56392010-04-27 18:04:05 +053028 unsigned int i;
Michael Tokarev2278a692012-06-07 20:08:19 +040029 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;
Amit Shahfa6111f2010-04-27 18:04:06 +053037 }
Amit Shahfa6111f2010-04-27 18:04:06 +053038 }
Michael Tokarev2278a692012-06-07 20:08:19 +040039 assert(offset == 0);
40 return done;
Amit Shahfa6111f2010-04-27 18:04:06 +053041}
42
Paolo Bonziniad523bc2015-12-22 12:03:33 +010043size_t iov_to_buf_full(const struct iovec *iov, const unsigned int iov_cnt,
44 size_t offset, void *buf, size_t bytes)
Hannes Reinecke348e7b82011-07-11 15:02:23 +020045{
Michael Tokarev2278a692012-06-07 20:08:19 +040046 size_t done;
Hannes Reinecke348e7b82011-07-11 15:02:23 +020047 unsigned int i;
Michael Tokarev2278a692012-06-07 20:08:19 +040048 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;
Hannes Reinecke348e7b82011-07-11 15:02:23 +020056 }
Hannes Reinecke348e7b82011-07-11 15:02:23 +020057 }
Michael Tokarev2278a692012-06-07 20:08:19 +040058 assert(offset == 0);
59 return done;
Hannes Reinecke348e7b82011-07-11 15:02:23 +020060}
61
Michael Tokarevdcf6f5e2012-03-11 18:05:12 +040062size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt,
Michael Tokarev2278a692012-06-07 20:08:19 +040063 size_t offset, int fillc, size_t bytes)
Gerd Hoffmann8d150282011-07-13 15:16:08 +020064{
Michael Tokarev2278a692012-06-07 20:08:19 +040065 size_t done;
Gerd Hoffmann8d150282011-07-13 15:16:08 +020066 unsigned int i;
Michael Tokarev2278a692012-06-07 20:08:19 +040067 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;
Gerd Hoffmann8d150282011-07-13 15:16:08 +020075 }
Gerd Hoffmann8d150282011-07-13 15:16:08 +020076 }
Michael Tokarev2278a692012-06-07 20:08:19 +040077 assert(offset == 0);
78 return done;
Gerd Hoffmann8d150282011-07-13 15:16:08 +020079}
80
Hannes Reinecke348e7b82011-07-11 15:02:23 +020081size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt)
Amit Shahfa6111f2010-04-27 18:04:06 +053082{
83 size_t len;
84 unsigned int i;
85
86 len = 0;
Hannes Reinecke348e7b82011-07-11 15:02:23 +020087 for (i = 0; i < iov_cnt; i++) {
Amit Shahfa6111f2010-04-27 18:04:06 +053088 len += iov[i].iov_len;
89 }
90 return len;
91}
Gerd Hoffmann3a1dca92011-07-12 13:35:10 +020092
Michael Tokarev25e5e4c2012-03-14 11:18:54 +040093/* helper function for iov_send_recv() */
94static ssize_t
95do_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt, bool do_send)
96{
Paolo Bonzini9adea5f2013-04-21 12:01:06 +020097#ifdef CONFIG_POSIX
Michael Tokarev25e5e4c2012-03-14 11:18:54 +040098 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() */
Stefan Weilc0958552012-07-11 07:09:05 +0200112 unsigned i = 0;
113 ssize_t ret = 0;
114 while (i < iov_cnt) {
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400115 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) {
Stefan Weilc0958552012-07-11 07:09:05 +0200128 ret = -1;
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400129 }
130 break;
131 }
Stefan Weilc0958552012-07-11 07:09:05 +0200132 i++;
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400133 }
Stefan Weilc0958552012-07-11 07:09:05 +0200134 return ret;
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400135#endif
136}
137
Wen Congyang6b646402015-05-21 09:50:10 +0800138ssize_t iov_send_recv(int sockfd, const struct iovec *_iov, unsigned iov_cnt,
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400139 size_t offset, size_t bytes,
140 bool do_send)
141{
Paolo Bonzini83f75c22013-03-27 17:36:31 +0100142 ssize_t total = 0;
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400143 ssize_t ret;
Paolo Bonzini5209d672013-03-27 17:36:29 +0100144 size_t orig_len, tail;
Paolo Bonzinif48869a2013-03-27 17:36:30 +0100145 unsigned niov;
Wen Congyang6b646402015-05-21 09:50:10 +0800146 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;
Paolo Bonzini5209d672013-03-27 17:36:29 +0100156
Paolo Bonzini83f75c22013-03-27 17:36:31 +0100157 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 }
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400163
Paolo Bonzini83f75c22013-03-27 17:36:31 +0100164 /* niov == iov_cnt would only be valid if bytes == 0, which
165 * we already ruled out in the loop condition. */
Paolo Bonzinif48869a2013-03-27 17:36:30 +0100166 assert(niov < iov_cnt);
Paolo Bonzini83f75c22013-03-27 17:36:31 +0100167 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;
Michael Tokarev2be178a2013-09-14 13:11:36 +0400189 ret = do_send_recv(sockfd, iov, niov, do_send);
190 /* Undo the changes above before checking for errors */
Paolo Bonzini83f75c22013-03-27 17:36:31 +0100191 iov[niov-1].iov_len = orig_len;
Michael Tokarev2be178a2013-09-14 13:11:36 +0400192 } else {
193 ret = do_send_recv(sockfd, iov, niov, do_send);
Paolo Bonzini83f75c22013-03-27 17:36:31 +0100194 }
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);
Wen Congyang6b646402015-05-21 09:50:10 +0800202 g_free(local_iov);
Paolo Bonzini83f75c22013-03-27 17:36:31 +0100203 if (errno == EAGAIN && total > 0) {
204 return total;
205 }
206 return -1;
207 }
208
MORITA Kazutaka84004292013-07-23 17:30:12 +0900209 if (ret == 0 && !do_send) {
210 /* recv returns 0 when the peer has performed an orderly
211 * shutdown. */
212 break;
213 }
214
Paolo Bonzini83f75c22013-03-27 17:36:31 +0100215 /* Prepare for the next iteration */
216 offset += ret;
217 total += ret;
218 bytes -= ret;
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400219 }
220
Wen Congyang6b646402015-05-21 09:50:10 +0800221 g_free(local_iov);
Paolo Bonzini83f75c22013-03-27 17:36:31 +0100222 return total;
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400223}
224
225
Gerd Hoffmann3a1dca92011-07-12 13:35:10 +0200226void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
227 FILE *fp, const char *prefix, size_t limit)
228{
Peter Crosthwaite6ff66f52013-03-15 16:41:58 +0000229 int v;
230 size_t size = 0;
231 char *buf;
Gerd Hoffmann3a1dca92011-07-12 13:35:10 +0200232
Peter Crosthwaite6ff66f52013-03-15 16:41:58 +0000233 for (v = 0; v < iov_cnt; v++) {
234 size += iov[v].iov_len;
Gerd Hoffmann3a1dca92011-07-12 13:35:10 +0200235 }
Peter Crosthwaite6ff66f52013-03-15 16:41:58 +0000236 size = size > limit ? limit : size;
237 buf = g_malloc(size);
238 iov_to_buf(iov, iov_cnt, 0, buf, size);
Philippe Mathieu-Daudéb42581f2020-08-22 20:09:50 +0200239 qemu_hexdump(fp, prefix, buf, size);
Peter Crosthwaite6ff66f52013-03-15 16:41:58 +0000240 g_free(buf);
Gerd Hoffmann3a1dca92011-07-12 13:35:10 +0200241}
Paolo Bonzini01912532012-10-24 14:58:39 +0200242
Michael S. Tsirkind3363362012-09-24 13:02:52 +0200243unsigned 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;
Shmulik Ladkanie9117652016-08-02 12:41:20 +0300249 for (i = 0, j = 0;
250 i < iov_cnt && j < dst_iov_cnt && (offset || bytes); i++) {
Michael S. Tsirkind3363362012-09-24 13:02:52 +0200251 if (offset >= iov[i].iov_len) {
252 offset -= iov[i].iov_len;
253 continue;
254 }
255 len = MIN(bytes, iov[i].iov_len - offset);
256
257 dst_iov[j].iov_base = iov[i].iov_base + offset;
258 dst_iov[j].iov_len = len;
259 j++;
260 bytes -= len;
261 offset = 0;
262 }
263 assert(offset == 0);
264 return j;
265}
Paolo Bonzinif563a5d2012-10-31 10:42:51 +0100266
Paolo Bonzini01912532012-10-24 14:58:39 +0200267/* io vectors */
268
269void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint)
270{
Markus Armbrustere1cf5582014-12-04 15:00:03 +0100271 qiov->iov = g_new(struct iovec, alloc_hint);
Paolo Bonzini01912532012-10-24 14:58:39 +0200272 qiov->niov = 0;
273 qiov->nalloc = alloc_hint;
274 qiov->size = 0;
275}
276
277void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov)
278{
279 int i;
280
281 qiov->iov = iov;
282 qiov->niov = niov;
283 qiov->nalloc = -1;
284 qiov->size = 0;
285 for (i = 0; i < niov; i++)
286 qiov->size += iov[i].iov_len;
287}
288
289void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len)
290{
291 assert(qiov->nalloc != -1);
292
293 if (qiov->niov == qiov->nalloc) {
294 qiov->nalloc = 2 * qiov->nalloc + 1;
Markus Armbrustere1cf5582014-12-04 15:00:03 +0100295 qiov->iov = g_renew(struct iovec, qiov->iov, qiov->nalloc);
Paolo Bonzini01912532012-10-24 14:58:39 +0200296 }
297 qiov->iov[qiov->niov].iov_base = base;
298 qiov->iov[qiov->niov].iov_len = len;
299 qiov->size += len;
300 ++qiov->niov;
301}
302
303/*
Stefan Hajnoczi530c0bb2012-11-22 16:06:06 +0100304 * Concatenates (partial) iovecs from src_iov to the end of dst.
305 * It starts copying after skipping `soffset' bytes at the
306 * beginning of src and adds individual vectors from src to
307 * dst copies up to `sbytes' bytes total, or up to the end
308 * of src_iov if it comes first. This way, it is okay to specify
309 * very large value for `sbytes' to indicate "up to the end
310 * of src".
311 * Only vector pointers are processed, not the actual data buffers.
312 */
Paolo Bonzini519661e2014-06-10 16:21:28 +0200313size_t qemu_iovec_concat_iov(QEMUIOVector *dst,
314 struct iovec *src_iov, unsigned int src_cnt,
315 size_t soffset, size_t sbytes)
Stefan Hajnoczi530c0bb2012-11-22 16:06:06 +0100316{
317 int i;
318 size_t done;
Aneesh Kumar K.Vfacf98a2013-02-05 11:27:45 +0530319
320 if (!sbytes) {
Paolo Bonzini519661e2014-06-10 16:21:28 +0200321 return 0;
Aneesh Kumar K.Vfacf98a2013-02-05 11:27:45 +0530322 }
Stefan Hajnoczi530c0bb2012-11-22 16:06:06 +0100323 assert(dst->nalloc != -1);
324 for (i = 0, done = 0; done < sbytes && i < src_cnt; i++) {
325 if (soffset < src_iov[i].iov_len) {
326 size_t len = MIN(src_iov[i].iov_len - soffset, sbytes - done);
327 qemu_iovec_add(dst, src_iov[i].iov_base + soffset, len);
328 done += len;
329 soffset = 0;
330 } else {
331 soffset -= src_iov[i].iov_len;
332 }
333 }
334 assert(soffset == 0); /* offset beyond end of src */
Paolo Bonzini519661e2014-06-10 16:21:28 +0200335
336 return done;
Stefan Hajnoczi530c0bb2012-11-22 16:06:06 +0100337}
338
339/*
Paolo Bonzini01912532012-10-24 14:58:39 +0200340 * Concatenates (partial) iovecs from src to the end of dst.
341 * It starts copying after skipping `soffset' bytes at the
342 * beginning of src and adds individual vectors from src to
343 * dst copies up to `sbytes' bytes total, or up to the end
344 * of src if it comes first. This way, it is okay to specify
345 * very large value for `sbytes' to indicate "up to the end
346 * of src".
347 * Only vector pointers are processed, not the actual data buffers.
348 */
349void qemu_iovec_concat(QEMUIOVector *dst,
350 QEMUIOVector *src, size_t soffset, size_t sbytes)
351{
Stefan Hajnoczi530c0bb2012-11-22 16:06:06 +0100352 qemu_iovec_concat_iov(dst, src->iov, src->niov, soffset, sbytes);
Paolo Bonzini01912532012-10-24 14:58:39 +0200353}
354
Peter Lieven43f35cb2014-05-18 00:58:17 +0200355/*
Vladimir Sementsov-Ogievskiyd9531692019-06-04 19:15:03 +0300356 * qiov_find_iov
357 *
358 * Return pointer to iovec structure, where byte at @offset in original vector
359 * @iov exactly is.
360 * Set @remaining_offset to be offset inside that iovec to the same byte.
361 */
362static struct iovec *iov_skip_offset(struct iovec *iov, size_t offset,
363 size_t *remaining_offset)
364{
365 while (offset > 0 && offset >= iov->iov_len) {
366 offset -= iov->iov_len;
367 iov++;
368 }
369 *remaining_offset = offset;
370
371 return iov;
372}
373
374/*
375 * qiov_slice
376 *
377 * Find subarray of iovec's, containing requested range. @head would
378 * be offset in first iov (returned by the function), @tail would be
379 * count of extra bytes in last iovec (returned iov + @niov - 1).
380 */
381static struct iovec *qiov_slice(QEMUIOVector *qiov,
382 size_t offset, size_t len,
383 size_t *head, size_t *tail, int *niov)
384{
385 struct iovec *iov, *end_iov;
386
387 assert(offset + len <= qiov->size);
388
389 iov = iov_skip_offset(qiov->iov, offset, head);
390 end_iov = iov_skip_offset(iov, *head + len, tail);
391
392 if (*tail > 0) {
393 assert(*tail < end_iov->iov_len);
394 *tail = end_iov->iov_len - *tail;
395 end_iov++;
396 }
397
398 *niov = end_iov - iov;
399
400 return iov;
401}
402
Vladimir Sementsov-Ogievskiy53962342019-06-04 19:15:14 +0300403int qemu_iovec_subvec_niov(QEMUIOVector *qiov, size_t offset, size_t len)
404{
405 size_t head, tail;
406 int niov;
407
408 qiov_slice(qiov, offset, len, &head, &tail, &niov);
409
410 return niov;
411}
412
Vladimir Sementsov-Ogievskiyd9531692019-06-04 19:15:03 +0300413/*
414 * Compile new iovec, combining @head_buf buffer, sub-qiov of @mid_qiov,
415 * and @tail_buf buffer into new qiov.
416 */
Vladimir Sementsov-Ogievskiy4c002ce2020-12-11 21:39:20 +0300417int qemu_iovec_init_extended(
Vladimir Sementsov-Ogievskiyd9531692019-06-04 19:15:03 +0300418 QEMUIOVector *qiov,
419 void *head_buf, size_t head_len,
420 QEMUIOVector *mid_qiov, size_t mid_offset, size_t mid_len,
421 void *tail_buf, size_t tail_len)
422{
423 size_t mid_head, mid_tail;
424 int total_niov, mid_niov = 0;
Vladimir Sementsov-Ogievskiyd38d6de2019-09-10 12:03:10 +0300425 struct iovec *p, *mid_iov = NULL;
Vladimir Sementsov-Ogievskiyd9531692019-06-04 19:15:03 +0300426
Vladimir Sementsov-Ogievskiy4c002ce2020-12-11 21:39:20 +0300427 assert(mid_qiov->niov <= IOV_MAX);
428
429 if (SIZE_MAX - head_len < mid_len ||
430 SIZE_MAX - head_len - mid_len < tail_len)
431 {
432 return -EINVAL;
433 }
434
Vladimir Sementsov-Ogievskiyd9531692019-06-04 19:15:03 +0300435 if (mid_len) {
436 mid_iov = qiov_slice(mid_qiov, mid_offset, mid_len,
437 &mid_head, &mid_tail, &mid_niov);
438 }
439
440 total_niov = !!head_len + mid_niov + !!tail_len;
Vladimir Sementsov-Ogievskiy4c002ce2020-12-11 21:39:20 +0300441 if (total_niov > IOV_MAX) {
442 return -EINVAL;
443 }
444
Vladimir Sementsov-Ogievskiyd9531692019-06-04 19:15:03 +0300445 if (total_niov == 1) {
446 qemu_iovec_init_buf(qiov, NULL, 0);
447 p = &qiov->local_iov;
448 } else {
449 qiov->niov = qiov->nalloc = total_niov;
450 qiov->size = head_len + mid_len + tail_len;
451 p = qiov->iov = g_new(struct iovec, qiov->niov);
452 }
453
454 if (head_len) {
455 p->iov_base = head_buf;
456 p->iov_len = head_len;
457 p++;
458 }
459
Vladimir Sementsov-Ogievskiyd38d6de2019-09-10 12:03:10 +0300460 assert(!mid_niov == !mid_len);
461 if (mid_niov) {
Vladimir Sementsov-Ogievskiyd9531692019-06-04 19:15:03 +0300462 memcpy(p, mid_iov, mid_niov * sizeof(*p));
463 p[0].iov_base = (uint8_t *)p[0].iov_base + mid_head;
464 p[0].iov_len -= mid_head;
465 p[mid_niov - 1].iov_len -= mid_tail;
466 p += mid_niov;
467 }
468
469 if (tail_len) {
470 p->iov_base = tail_buf;
471 p->iov_len = tail_len;
472 }
Vladimir Sementsov-Ogievskiy4c002ce2020-12-11 21:39:20 +0300473
474 return 0;
Vladimir Sementsov-Ogievskiyd9531692019-06-04 19:15:03 +0300475}
476
477/*
Vladimir Sementsov-Ogievskiyf76889e2019-06-04 19:15:04 +0300478 * Check if the contents of subrange of qiov data is all zeroes.
Peter Lieven43f35cb2014-05-18 00:58:17 +0200479 */
Vladimir Sementsov-Ogievskiyf76889e2019-06-04 19:15:04 +0300480bool qemu_iovec_is_zero(QEMUIOVector *qiov, size_t offset, size_t bytes)
Peter Lieven43f35cb2014-05-18 00:58:17 +0200481{
Vladimir Sementsov-Ogievskiyf76889e2019-06-04 19:15:04 +0300482 struct iovec *iov;
483 size_t current_offset;
484
485 assert(offset + bytes <= qiov->size);
486
487 iov = iov_skip_offset(qiov->iov, offset, &current_offset);
488
489 while (bytes) {
490 uint8_t *base = (uint8_t *)iov->iov_base + current_offset;
491 size_t len = MIN(iov->iov_len - current_offset, bytes);
492
493 if (!buffer_is_zero(base, len)) {
Peter Lieven43f35cb2014-05-18 00:58:17 +0200494 return false;
495 }
Vladimir Sementsov-Ogievskiyf76889e2019-06-04 19:15:04 +0300496
497 current_offset = 0;
498 bytes -= len;
499 iov++;
Peter Lieven43f35cb2014-05-18 00:58:17 +0200500 }
Vladimir Sementsov-Ogievskiyf76889e2019-06-04 19:15:04 +0300501
Peter Lieven43f35cb2014-05-18 00:58:17 +0200502 return true;
503}
504
Vladimir Sementsov-Ogievskiyd9531692019-06-04 19:15:03 +0300505void qemu_iovec_init_slice(QEMUIOVector *qiov, QEMUIOVector *source,
506 size_t offset, size_t len)
507{
Vladimir Sementsov-Ogievskiy4c002ce2020-12-11 21:39:20 +0300508 int ret;
509
510 assert(source->size >= len);
511 assert(source->size - len >= offset);
512
513 /* We shrink the request, so we can't overflow neither size_t nor MAX_IOV */
514 ret = qemu_iovec_init_extended(qiov, NULL, 0, source, offset, len, NULL, 0);
515 assert(ret == 0);
Vladimir Sementsov-Ogievskiyd9531692019-06-04 19:15:03 +0300516}
517
Paolo Bonzini01912532012-10-24 14:58:39 +0200518void qemu_iovec_destroy(QEMUIOVector *qiov)
519{
Vladimir Sementsov-Ogievskiyd9531692019-06-04 19:15:03 +0300520 if (qiov->nalloc != -1) {
521 g_free(qiov->iov);
522 }
Paolo Bonzini01912532012-10-24 14:58:39 +0200523
Vladimir Sementsov-Ogievskiyd9531692019-06-04 19:15:03 +0300524 memset(qiov, 0, sizeof(*qiov));
Paolo Bonzini01912532012-10-24 14:58:39 +0200525}
526
527void qemu_iovec_reset(QEMUIOVector *qiov)
528{
529 assert(qiov->nalloc != -1);
530
531 qiov->niov = 0;
532 qiov->size = 0;
533}
534
535size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
536 void *buf, size_t bytes)
537{
538 return iov_to_buf(qiov->iov, qiov->niov, offset, buf, bytes);
539}
540
541size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
542 const void *buf, size_t bytes)
543{
544 return iov_from_buf(qiov->iov, qiov->niov, offset, buf, bytes);
545}
546
547size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
548 int fillc, size_t bytes)
549{
550 return iov_memset(qiov->iov, qiov->niov, offset, fillc, bytes);
551}
Stefan Hajnoczid0277632012-11-21 17:41:10 +0100552
Benoît Canetf70d7f72014-02-21 22:21:13 +0100553/**
554 * Check that I/O vector contents are identical
555 *
556 * The IO vectors must have the same structure (same length of all parts).
557 * A typical usage is to compare vectors created with qemu_iovec_clone().
558 *
559 * @a: I/O vector
560 * @b: I/O vector
561 * @ret: Offset to first mismatching byte or -1 if match
562 */
563ssize_t qemu_iovec_compare(QEMUIOVector *a, QEMUIOVector *b)
564{
565 int i;
566 ssize_t offset = 0;
567
568 assert(a->niov == b->niov);
569 for (i = 0; i < a->niov; i++) {
570 size_t len = 0;
571 uint8_t *p = (uint8_t *)a->iov[i].iov_base;
572 uint8_t *q = (uint8_t *)b->iov[i].iov_base;
573
574 assert(a->iov[i].iov_len == b->iov[i].iov_len);
575 while (len < a->iov[i].iov_len && *p++ == *q++) {
576 len++;
577 }
578
579 offset += len;
580
581 if (len != a->iov[i].iov_len) {
582 return offset;
583 }
584 }
585 return -1;
586}
587
588typedef struct {
589 int src_index;
590 struct iovec *src_iov;
591 void *dest_base;
592} IOVectorSortElem;
593
594static int sortelem_cmp_src_base(const void *a, const void *b)
595{
596 const IOVectorSortElem *elem_a = a;
597 const IOVectorSortElem *elem_b = b;
598
599 /* Don't overflow */
600 if (elem_a->src_iov->iov_base < elem_b->src_iov->iov_base) {
601 return -1;
602 } else if (elem_a->src_iov->iov_base > elem_b->src_iov->iov_base) {
603 return 1;
604 } else {
605 return 0;
606 }
607}
608
609static int sortelem_cmp_src_index(const void *a, const void *b)
610{
611 const IOVectorSortElem *elem_a = a;
612 const IOVectorSortElem *elem_b = b;
613
614 return elem_a->src_index - elem_b->src_index;
615}
616
617/**
618 * Copy contents of I/O vector
619 *
620 * The relative relationships of overlapping iovecs are preserved. This is
621 * necessary to ensure identical semantics in the cloned I/O vector.
622 */
623void qemu_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src, void *buf)
624{
625 IOVectorSortElem sortelems[src->niov];
626 void *last_end;
627 int i;
628
629 /* Sort by source iovecs by base address */
630 for (i = 0; i < src->niov; i++) {
631 sortelems[i].src_index = i;
632 sortelems[i].src_iov = &src->iov[i];
633 }
634 qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_base);
635
636 /* Allocate buffer space taking into account overlapping iovecs */
637 last_end = NULL;
638 for (i = 0; i < src->niov; i++) {
639 struct iovec *cur = sortelems[i].src_iov;
640 ptrdiff_t rewind = 0;
641
642 /* Detect overlap */
643 if (last_end && last_end > cur->iov_base) {
644 rewind = last_end - cur->iov_base;
645 }
646
647 sortelems[i].dest_base = buf - rewind;
648 buf += cur->iov_len - MIN(rewind, cur->iov_len);
649 last_end = MAX(cur->iov_base + cur->iov_len, last_end);
650 }
651
652 /* Sort by source iovec index and build destination iovec */
653 qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_index);
654 for (i = 0; i < src->niov; i++) {
655 qemu_iovec_add(dest, sortelems[i].dest_base, src->iov[i].iov_len);
656 }
657}
658
Stefan Hajnoczi9dd6f7c2020-09-17 10:44:53 +0100659void iov_discard_undo(IOVDiscardUndo *undo)
660{
661 /* Restore original iovec if it was modified */
662 if (undo->modified_iov) {
663 *undo->modified_iov = undo->orig;
664 }
665}
666
667size_t iov_discard_front_undoable(struct iovec **iov,
668 unsigned int *iov_cnt,
669 size_t bytes,
670 IOVDiscardUndo *undo)
Stefan Hajnoczid0277632012-11-21 17:41:10 +0100671{
672 size_t total = 0;
673 struct iovec *cur;
674
Stefan Hajnoczi9dd6f7c2020-09-17 10:44:53 +0100675 if (undo) {
676 undo->modified_iov = NULL;
677 }
678
Stefan Hajnoczid0277632012-11-21 17:41:10 +0100679 for (cur = *iov; *iov_cnt > 0; cur++) {
680 if (cur->iov_len > bytes) {
Stefan Hajnoczi9dd6f7c2020-09-17 10:44:53 +0100681 if (undo) {
682 undo->modified_iov = cur;
683 undo->orig = *cur;
684 }
685
Stefan Hajnoczid0277632012-11-21 17:41:10 +0100686 cur->iov_base += bytes;
687 cur->iov_len -= bytes;
688 total += bytes;
689 break;
690 }
691
692 bytes -= cur->iov_len;
693 total += cur->iov_len;
694 *iov_cnt -= 1;
695 }
696
697 *iov = cur;
698 return total;
699}
700
Stefan Hajnoczi9dd6f7c2020-09-17 10:44:53 +0100701size_t iov_discard_front(struct iovec **iov, unsigned int *iov_cnt,
702 size_t bytes)
703{
704 return iov_discard_front_undoable(iov, iov_cnt, bytes, NULL);
705}
706
707size_t iov_discard_back_undoable(struct iovec *iov,
708 unsigned int *iov_cnt,
709 size_t bytes,
710 IOVDiscardUndo *undo)
Stefan Hajnoczid0277632012-11-21 17:41:10 +0100711{
712 size_t total = 0;
713 struct iovec *cur;
714
Stefan Hajnoczi9dd6f7c2020-09-17 10:44:53 +0100715 if (undo) {
716 undo->modified_iov = NULL;
717 }
718
Stefan Hajnoczid0277632012-11-21 17:41:10 +0100719 if (*iov_cnt == 0) {
720 return 0;
721 }
722
723 cur = iov + (*iov_cnt - 1);
724
725 while (*iov_cnt > 0) {
726 if (cur->iov_len > bytes) {
Stefan Hajnoczi9dd6f7c2020-09-17 10:44:53 +0100727 if (undo) {
728 undo->modified_iov = cur;
729 undo->orig = *cur;
730 }
731
Stefan Hajnoczid0277632012-11-21 17:41:10 +0100732 cur->iov_len -= bytes;
733 total += bytes;
734 break;
735 }
736
737 bytes -= cur->iov_len;
738 total += cur->iov_len;
739 cur--;
740 *iov_cnt -= 1;
741 }
742
743 return total;
744}
Kevin Wolf58f423f2014-07-09 19:17:30 +0200745
Stefan Hajnoczi9dd6f7c2020-09-17 10:44:53 +0100746size_t iov_discard_back(struct iovec *iov, unsigned int *iov_cnt,
747 size_t bytes)
748{
749 return iov_discard_back_undoable(iov, iov_cnt, bytes, NULL);
750}
751
Kevin Wolf58f423f2014-07-09 19:17:30 +0200752void qemu_iovec_discard_back(QEMUIOVector *qiov, size_t bytes)
753{
754 size_t total;
755 unsigned int niov = qiov->niov;
756
757 assert(qiov->size >= bytes);
758 total = iov_discard_back(qiov->iov, &niov, bytes);
759 assert(total == bytes);
760
761 qiov->niov = niov;
762 qiov->size -= bytes;
763}