blob: 7777116123d0fc579b8fc4d189ad7154a9b1c602 [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.
Alejandro Zeise78a58222024-10-08 09:57:13 +02006 * Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates
Amit Shahe4d56392010-04-27 18:04:05 +05307 *
8 * Author(s):
9 * Anthony Liguori <aliguori@us.ibm.com>
10 * Amit Shah <amit.shah@redhat.com>
Michael Tokarev2278a692012-06-07 20:08:19 +040011 * Michael Tokarev <mjt@tls.msk.ru>
Amit Shahe4d56392010-04-27 18:04:05 +053012 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010015 *
16 * Contributions after 2012-01-13 are licensed under the terms of the
17 * GNU GPL, version 2 or (at your option) any later version.
Amit Shahe4d56392010-04-27 18:04:05 +053018 */
19
Peter Maydellaafd7582016-01-29 17:49:55 +000020#include "qemu/osdep.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
Alejandro Zeise78a58222024-10-08 09:57:13 +020096do_send_recv(int sockfd, int flags, struct iovec *iov, unsigned iov_cnt,
97 bool do_send)
Michael Tokarev25e5e4c2012-03-14 11:18:54 +040098{
Paolo Bonzini9adea5f2013-04-21 12:01:06 +020099#ifdef CONFIG_POSIX
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400100 ssize_t ret;
101 struct msghdr msg;
102 memset(&msg, 0, sizeof(msg));
103 msg.msg_iov = iov;
104 msg.msg_iovlen = iov_cnt;
105 do {
106 ret = do_send
Alejandro Zeise78a58222024-10-08 09:57:13 +0200107 ? sendmsg(sockfd, &msg, flags)
108 : recvmsg(sockfd, &msg, flags);
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400109 } while (ret < 0 && errno == EINTR);
110 return ret;
111#else
112 /* else send piece-by-piece */
113 /*XXX Note: windows has WSASend() and WSARecv() */
Stefan Weilc0958552012-07-11 07:09:05 +0200114 unsigned i = 0;
115 ssize_t ret = 0;
Marc-André Lureau3f083762022-10-06 15:36:53 +0400116 ssize_t off = 0;
Stefan Weilc0958552012-07-11 07:09:05 +0200117 while (i < iov_cnt) {
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400118 ssize_t r = do_send
Alejandro Zeise78a58222024-10-08 09:57:13 +0200119 ? send(sockfd, iov[i].iov_base + off, iov[i].iov_len - off, flags)
120 : recv(sockfd, iov[i].iov_base + off, iov[i].iov_len - off, flags);
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400121 if (r > 0) {
122 ret += r;
Marc-André Lureau3f083762022-10-06 15:36:53 +0400123 off += r;
124 if (off < iov[i].iov_len) {
125 continue;
126 }
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400127 } else if (!r) {
128 break;
129 } else if (errno == EINTR) {
130 continue;
131 } else {
132 /* else it is some "other" error,
133 * only return if there was no data processed. */
134 if (ret == 0) {
Stefan Weilc0958552012-07-11 07:09:05 +0200135 ret = -1;
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400136 }
137 break;
138 }
Marc-André Lureau3f083762022-10-06 15:36:53 +0400139 off = 0;
Stefan Weilc0958552012-07-11 07:09:05 +0200140 i++;
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400141 }
Stefan Weilc0958552012-07-11 07:09:05 +0200142 return ret;
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400143#endif
144}
145
Wen Congyang6b646402015-05-21 09:50:10 +0800146ssize_t iov_send_recv(int sockfd, const struct iovec *_iov, unsigned iov_cnt,
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400147 size_t offset, size_t bytes,
148 bool do_send)
149{
Alejandro Zeise78a58222024-10-08 09:57:13 +0200150 return iov_send_recv_with_flags(sockfd, 0, _iov, iov_cnt, offset, bytes,
151 do_send);
152}
153
154ssize_t iov_send_recv_with_flags(int sockfd, int sockflags,
155 const struct iovec *_iov,
156 unsigned iov_cnt, size_t offset,
157 size_t bytes, bool do_send)
158{
Paolo Bonzini83f75c22013-03-27 17:36:31 +0100159 ssize_t total = 0;
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400160 ssize_t ret;
Paolo Bonzini5209d672013-03-27 17:36:29 +0100161 size_t orig_len, tail;
Paolo Bonzinif48869a2013-03-27 17:36:30 +0100162 unsigned niov;
Wen Congyang6b646402015-05-21 09:50:10 +0800163 struct iovec *local_iov, *iov;
164
165 if (bytes <= 0) {
166 return 0;
167 }
168
169 local_iov = g_new0(struct iovec, iov_cnt);
170 iov_copy(local_iov, iov_cnt, _iov, iov_cnt, offset, bytes);
171 offset = 0;
172 iov = local_iov;
Paolo Bonzini5209d672013-03-27 17:36:29 +0100173
Paolo Bonzini83f75c22013-03-27 17:36:31 +0100174 while (bytes > 0) {
175 /* Find the start position, skipping `offset' bytes:
176 * first, skip all full-sized vector elements, */
177 for (niov = 0; niov < iov_cnt && offset >= iov[niov].iov_len; ++niov) {
178 offset -= iov[niov].iov_len;
179 }
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400180
Paolo Bonzini83f75c22013-03-27 17:36:31 +0100181 /* niov == iov_cnt would only be valid if bytes == 0, which
182 * we already ruled out in the loop condition. */
Paolo Bonzinif48869a2013-03-27 17:36:30 +0100183 assert(niov < iov_cnt);
Paolo Bonzini83f75c22013-03-27 17:36:31 +0100184 iov += niov;
185 iov_cnt -= niov;
186
187 if (offset) {
188 /* second, skip `offset' bytes from the (now) first element,
189 * undo it on exit */
190 iov[0].iov_base += offset;
191 iov[0].iov_len -= offset;
192 }
193 /* Find the end position skipping `bytes' bytes: */
194 /* first, skip all full-sized elements */
195 tail = bytes;
196 for (niov = 0; niov < iov_cnt && iov[niov].iov_len <= tail; ++niov) {
197 tail -= iov[niov].iov_len;
198 }
199 if (tail) {
200 /* second, fixup the last element, and remember the original
201 * length */
202 assert(niov < iov_cnt);
203 assert(iov[niov].iov_len > tail);
204 orig_len = iov[niov].iov_len;
205 iov[niov++].iov_len = tail;
Alejandro Zeise78a58222024-10-08 09:57:13 +0200206 ret = do_send_recv(sockfd, sockflags, iov, niov, do_send);
Michael Tokarev2be178a2013-09-14 13:11:36 +0400207 /* Undo the changes above before checking for errors */
Paolo Bonzini83f75c22013-03-27 17:36:31 +0100208 iov[niov-1].iov_len = orig_len;
Michael Tokarev2be178a2013-09-14 13:11:36 +0400209 } else {
Alejandro Zeise78a58222024-10-08 09:57:13 +0200210 ret = do_send_recv(sockfd, sockflags, iov, niov, do_send);
Paolo Bonzini83f75c22013-03-27 17:36:31 +0100211 }
212 if (offset) {
213 iov[0].iov_base -= offset;
214 iov[0].iov_len += offset;
215 }
216
217 if (ret < 0) {
218 assert(errno != EINTR);
Wen Congyang6b646402015-05-21 09:50:10 +0800219 g_free(local_iov);
Paolo Bonzini83f75c22013-03-27 17:36:31 +0100220 if (errno == EAGAIN && total > 0) {
221 return total;
222 }
223 return -1;
224 }
225
MORITA Kazutaka84004292013-07-23 17:30:12 +0900226 if (ret == 0 && !do_send) {
227 /* recv returns 0 when the peer has performed an orderly
228 * shutdown. */
229 break;
230 }
231
Paolo Bonzini83f75c22013-03-27 17:36:31 +0100232 /* Prepare for the next iteration */
233 offset += ret;
234 total += ret;
235 bytes -= ret;
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400236 }
237
Wen Congyang6b646402015-05-21 09:50:10 +0800238 g_free(local_iov);
Paolo Bonzini83f75c22013-03-27 17:36:31 +0100239 return total;
Michael Tokarev25e5e4c2012-03-14 11:18:54 +0400240}
241
242
Gerd Hoffmann3a1dca92011-07-12 13:35:10 +0200243void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
244 FILE *fp, const char *prefix, size_t limit)
245{
Peter Crosthwaite6ff66f52013-03-15 16:41:58 +0000246 int v;
247 size_t size = 0;
248 char *buf;
Gerd Hoffmann3a1dca92011-07-12 13:35:10 +0200249
Peter Crosthwaite6ff66f52013-03-15 16:41:58 +0000250 for (v = 0; v < iov_cnt; v++) {
251 size += iov[v].iov_len;
Gerd Hoffmann3a1dca92011-07-12 13:35:10 +0200252 }
Peter Crosthwaite6ff66f52013-03-15 16:41:58 +0000253 size = size > limit ? limit : size;
254 buf = g_malloc(size);
255 iov_to_buf(iov, iov_cnt, 0, buf, size);
Philippe Mathieu-Daudéb42581f2020-08-22 20:09:50 +0200256 qemu_hexdump(fp, prefix, buf, size);
Peter Crosthwaite6ff66f52013-03-15 16:41:58 +0000257 g_free(buf);
Gerd Hoffmann3a1dca92011-07-12 13:35:10 +0200258}
Paolo Bonzini01912532012-10-24 14:58:39 +0200259
Michael S. Tsirkind3363362012-09-24 13:02:52 +0200260unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt,
261 const struct iovec *iov, unsigned int iov_cnt,
262 size_t offset, size_t bytes)
263{
264 size_t len;
265 unsigned int i, j;
Shmulik Ladkanie9117652016-08-02 12:41:20 +0300266 for (i = 0, j = 0;
267 i < iov_cnt && j < dst_iov_cnt && (offset || bytes); i++) {
Michael S. Tsirkind3363362012-09-24 13:02:52 +0200268 if (offset >= iov[i].iov_len) {
269 offset -= iov[i].iov_len;
270 continue;
271 }
272 len = MIN(bytes, iov[i].iov_len - offset);
273
274 dst_iov[j].iov_base = iov[i].iov_base + offset;
275 dst_iov[j].iov_len = len;
276 j++;
277 bytes -= len;
278 offset = 0;
279 }
280 assert(offset == 0);
281 return j;
282}
Paolo Bonzinif563a5d2012-10-31 10:42:51 +0100283
Paolo Bonzini01912532012-10-24 14:58:39 +0200284/* io vectors */
285
286void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint)
287{
Markus Armbrustere1cf5582014-12-04 15:00:03 +0100288 qiov->iov = g_new(struct iovec, alloc_hint);
Paolo Bonzini01912532012-10-24 14:58:39 +0200289 qiov->niov = 0;
290 qiov->nalloc = alloc_hint;
291 qiov->size = 0;
292}
293
294void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov)
295{
296 int i;
297
298 qiov->iov = iov;
299 qiov->niov = niov;
300 qiov->nalloc = -1;
301 qiov->size = 0;
302 for (i = 0; i < niov; i++)
303 qiov->size += iov[i].iov_len;
304}
305
306void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len)
307{
308 assert(qiov->nalloc != -1);
309
310 if (qiov->niov == qiov->nalloc) {
311 qiov->nalloc = 2 * qiov->nalloc + 1;
Markus Armbrustere1cf5582014-12-04 15:00:03 +0100312 qiov->iov = g_renew(struct iovec, qiov->iov, qiov->nalloc);
Paolo Bonzini01912532012-10-24 14:58:39 +0200313 }
314 qiov->iov[qiov->niov].iov_base = base;
315 qiov->iov[qiov->niov].iov_len = len;
316 qiov->size += len;
317 ++qiov->niov;
318}
319
320/*
Stefan Hajnoczi530c0bb2012-11-22 16:06:06 +0100321 * Concatenates (partial) iovecs from src_iov to the end of dst.
322 * It starts copying after skipping `soffset' bytes at the
323 * beginning of src and adds individual vectors from src to
324 * dst copies up to `sbytes' bytes total, or up to the end
325 * of src_iov if it comes first. This way, it is okay to specify
326 * very large value for `sbytes' to indicate "up to the end
327 * of src".
328 * Only vector pointers are processed, not the actual data buffers.
329 */
Paolo Bonzini519661e2014-06-10 16:21:28 +0200330size_t qemu_iovec_concat_iov(QEMUIOVector *dst,
331 struct iovec *src_iov, unsigned int src_cnt,
332 size_t soffset, size_t sbytes)
Stefan Hajnoczi530c0bb2012-11-22 16:06:06 +0100333{
334 int i;
335 size_t done;
Aneesh Kumar K.Vfacf98a2013-02-05 11:27:45 +0530336
337 if (!sbytes) {
Paolo Bonzini519661e2014-06-10 16:21:28 +0200338 return 0;
Aneesh Kumar K.Vfacf98a2013-02-05 11:27:45 +0530339 }
Stefan Hajnoczi530c0bb2012-11-22 16:06:06 +0100340 assert(dst->nalloc != -1);
341 for (i = 0, done = 0; done < sbytes && i < src_cnt; i++) {
342 if (soffset < src_iov[i].iov_len) {
343 size_t len = MIN(src_iov[i].iov_len - soffset, sbytes - done);
344 qemu_iovec_add(dst, src_iov[i].iov_base + soffset, len);
345 done += len;
346 soffset = 0;
347 } else {
348 soffset -= src_iov[i].iov_len;
349 }
350 }
351 assert(soffset == 0); /* offset beyond end of src */
Paolo Bonzini519661e2014-06-10 16:21:28 +0200352
353 return done;
Stefan Hajnoczi530c0bb2012-11-22 16:06:06 +0100354}
355
356/*
Paolo Bonzini01912532012-10-24 14:58:39 +0200357 * Concatenates (partial) iovecs from src to the end of dst.
358 * It starts copying after skipping `soffset' bytes at the
359 * beginning of src and adds individual vectors from src to
360 * dst copies up to `sbytes' bytes total, or up to the end
361 * of src if it comes first. This way, it is okay to specify
362 * very large value for `sbytes' to indicate "up to the end
363 * of src".
364 * Only vector pointers are processed, not the actual data buffers.
365 */
366void qemu_iovec_concat(QEMUIOVector *dst,
367 QEMUIOVector *src, size_t soffset, size_t sbytes)
368{
Stefan Hajnoczi530c0bb2012-11-22 16:06:06 +0100369 qemu_iovec_concat_iov(dst, src->iov, src->niov, soffset, sbytes);
Paolo Bonzini01912532012-10-24 14:58:39 +0200370}
371
Peter Lieven43f35cb2014-05-18 00:58:17 +0200372/*
Vladimir Sementsov-Ogievskiyd9531692019-06-04 19:15:03 +0300373 * qiov_find_iov
374 *
375 * Return pointer to iovec structure, where byte at @offset in original vector
376 * @iov exactly is.
377 * Set @remaining_offset to be offset inside that iovec to the same byte.
378 */
379static struct iovec *iov_skip_offset(struct iovec *iov, size_t offset,
380 size_t *remaining_offset)
381{
382 while (offset > 0 && offset >= iov->iov_len) {
383 offset -= iov->iov_len;
384 iov++;
385 }
386 *remaining_offset = offset;
387
388 return iov;
389}
390
391/*
Hanna Czenczek3d06cea2023-04-11 19:34:15 +0200392 * qemu_iovec_slice
Vladimir Sementsov-Ogievskiyd9531692019-06-04 19:15:03 +0300393 *
394 * Find subarray of iovec's, containing requested range. @head would
395 * be offset in first iov (returned by the function), @tail would be
396 * count of extra bytes in last iovec (returned iov + @niov - 1).
397 */
Hanna Czenczek3d06cea2023-04-11 19:34:15 +0200398struct iovec *qemu_iovec_slice(QEMUIOVector *qiov,
399 size_t offset, size_t len,
400 size_t *head, size_t *tail, int *niov)
Vladimir Sementsov-Ogievskiyd9531692019-06-04 19:15:03 +0300401{
402 struct iovec *iov, *end_iov;
403
404 assert(offset + len <= qiov->size);
405
406 iov = iov_skip_offset(qiov->iov, offset, head);
407 end_iov = iov_skip_offset(iov, *head + len, tail);
408
409 if (*tail > 0) {
410 assert(*tail < end_iov->iov_len);
411 *tail = end_iov->iov_len - *tail;
412 end_iov++;
413 }
414
415 *niov = end_iov - iov;
416
417 return iov;
418}
419
Vladimir Sementsov-Ogievskiy53962342019-06-04 19:15:14 +0300420int qemu_iovec_subvec_niov(QEMUIOVector *qiov, size_t offset, size_t len)
421{
422 size_t head, tail;
423 int niov;
424
Hanna Czenczek3d06cea2023-04-11 19:34:15 +0200425 qemu_iovec_slice(qiov, offset, len, &head, &tail, &niov);
Vladimir Sementsov-Ogievskiy53962342019-06-04 19:15:14 +0300426
427 return niov;
428}
429
Vladimir Sementsov-Ogievskiyd9531692019-06-04 19:15:03 +0300430/*
Vladimir Sementsov-Ogievskiyf76889e2019-06-04 19:15:04 +0300431 * Check if the contents of subrange of qiov data is all zeroes.
Peter Lieven43f35cb2014-05-18 00:58:17 +0200432 */
Vladimir Sementsov-Ogievskiyf76889e2019-06-04 19:15:04 +0300433bool qemu_iovec_is_zero(QEMUIOVector *qiov, size_t offset, size_t bytes)
Peter Lieven43f35cb2014-05-18 00:58:17 +0200434{
Vladimir Sementsov-Ogievskiyf76889e2019-06-04 19:15:04 +0300435 struct iovec *iov;
436 size_t current_offset;
437
438 assert(offset + bytes <= qiov->size);
439
440 iov = iov_skip_offset(qiov->iov, offset, &current_offset);
441
442 while (bytes) {
443 uint8_t *base = (uint8_t *)iov->iov_base + current_offset;
444 size_t len = MIN(iov->iov_len - current_offset, bytes);
445
446 if (!buffer_is_zero(base, len)) {
Peter Lieven43f35cb2014-05-18 00:58:17 +0200447 return false;
448 }
Vladimir Sementsov-Ogievskiyf76889e2019-06-04 19:15:04 +0300449
450 current_offset = 0;
451 bytes -= len;
452 iov++;
Peter Lieven43f35cb2014-05-18 00:58:17 +0200453 }
Vladimir Sementsov-Ogievskiyf76889e2019-06-04 19:15:04 +0300454
Peter Lieven43f35cb2014-05-18 00:58:17 +0200455 return true;
456}
457
Vladimir Sementsov-Ogievskiyd9531692019-06-04 19:15:03 +0300458void qemu_iovec_init_slice(QEMUIOVector *qiov, QEMUIOVector *source,
459 size_t offset, size_t len)
460{
Hanna Czenczekcc63f6f2023-04-11 19:34:17 +0200461 struct iovec *slice_iov;
462 int slice_niov;
463 size_t slice_head, slice_tail;
Vladimir Sementsov-Ogievskiy4c002ce2020-12-11 21:39:20 +0300464
465 assert(source->size >= len);
466 assert(source->size - len >= offset);
467
Hanna Czenczekcc63f6f2023-04-11 19:34:17 +0200468 slice_iov = qemu_iovec_slice(source, offset, len,
469 &slice_head, &slice_tail, &slice_niov);
470 if (slice_niov == 1) {
471 qemu_iovec_init_buf(qiov, slice_iov[0].iov_base + slice_head, len);
472 } else {
473 qemu_iovec_init(qiov, slice_niov);
474 qemu_iovec_concat_iov(qiov, slice_iov, slice_niov, slice_head, len);
475 }
Vladimir Sementsov-Ogievskiyd9531692019-06-04 19:15:03 +0300476}
477
Paolo Bonzini01912532012-10-24 14:58:39 +0200478void qemu_iovec_destroy(QEMUIOVector *qiov)
479{
Vladimir Sementsov-Ogievskiyd9531692019-06-04 19:15:03 +0300480 if (qiov->nalloc != -1) {
481 g_free(qiov->iov);
482 }
Paolo Bonzini01912532012-10-24 14:58:39 +0200483
Vladimir Sementsov-Ogievskiyd9531692019-06-04 19:15:03 +0300484 memset(qiov, 0, sizeof(*qiov));
Paolo Bonzini01912532012-10-24 14:58:39 +0200485}
486
487void qemu_iovec_reset(QEMUIOVector *qiov)
488{
489 assert(qiov->nalloc != -1);
490
491 qiov->niov = 0;
492 qiov->size = 0;
493}
494
495size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
496 void *buf, size_t bytes)
497{
498 return iov_to_buf(qiov->iov, qiov->niov, offset, buf, bytes);
499}
500
501size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
502 const void *buf, size_t bytes)
503{
504 return iov_from_buf(qiov->iov, qiov->niov, offset, buf, bytes);
505}
506
507size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
508 int fillc, size_t bytes)
509{
510 return iov_memset(qiov->iov, qiov->niov, offset, fillc, bytes);
511}
Stefan Hajnoczid0277632012-11-21 17:41:10 +0100512
Benoît Canetf70d7f72014-02-21 22:21:13 +0100513/**
514 * Check that I/O vector contents are identical
515 *
516 * The IO vectors must have the same structure (same length of all parts).
517 * A typical usage is to compare vectors created with qemu_iovec_clone().
518 *
519 * @a: I/O vector
520 * @b: I/O vector
521 * @ret: Offset to first mismatching byte or -1 if match
522 */
523ssize_t qemu_iovec_compare(QEMUIOVector *a, QEMUIOVector *b)
524{
525 int i;
526 ssize_t offset = 0;
527
528 assert(a->niov == b->niov);
529 for (i = 0; i < a->niov; i++) {
530 size_t len = 0;
531 uint8_t *p = (uint8_t *)a->iov[i].iov_base;
532 uint8_t *q = (uint8_t *)b->iov[i].iov_base;
533
534 assert(a->iov[i].iov_len == b->iov[i].iov_len);
535 while (len < a->iov[i].iov_len && *p++ == *q++) {
536 len++;
537 }
538
539 offset += len;
540
541 if (len != a->iov[i].iov_len) {
542 return offset;
543 }
544 }
545 return -1;
546}
547
548typedef struct {
549 int src_index;
550 struct iovec *src_iov;
551 void *dest_base;
552} IOVectorSortElem;
553
554static int sortelem_cmp_src_base(const void *a, const void *b)
555{
556 const IOVectorSortElem *elem_a = a;
557 const IOVectorSortElem *elem_b = b;
558
559 /* Don't overflow */
560 if (elem_a->src_iov->iov_base < elem_b->src_iov->iov_base) {
561 return -1;
562 } else if (elem_a->src_iov->iov_base > elem_b->src_iov->iov_base) {
563 return 1;
564 } else {
565 return 0;
566 }
567}
568
569static int sortelem_cmp_src_index(const void *a, const void *b)
570{
571 const IOVectorSortElem *elem_a = a;
572 const IOVectorSortElem *elem_b = b;
573
574 return elem_a->src_index - elem_b->src_index;
575}
576
577/**
578 * Copy contents of I/O vector
579 *
580 * The relative relationships of overlapping iovecs are preserved. This is
581 * necessary to ensure identical semantics in the cloned I/O vector.
582 */
583void qemu_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src, void *buf)
584{
Philippe Mathieu-Daudé522a9b92023-08-24 17:47:06 +0100585 g_autofree IOVectorSortElem *sortelems = g_new(IOVectorSortElem, src->niov);
Benoît Canetf70d7f72014-02-21 22:21:13 +0100586 void *last_end;
587 int i;
588
589 /* Sort by source iovecs by base address */
590 for (i = 0; i < src->niov; i++) {
591 sortelems[i].src_index = i;
592 sortelems[i].src_iov = &src->iov[i];
593 }
594 qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_base);
595
596 /* Allocate buffer space taking into account overlapping iovecs */
597 last_end = NULL;
598 for (i = 0; i < src->niov; i++) {
599 struct iovec *cur = sortelems[i].src_iov;
600 ptrdiff_t rewind = 0;
601
602 /* Detect overlap */
603 if (last_end && last_end > cur->iov_base) {
604 rewind = last_end - cur->iov_base;
605 }
606
607 sortelems[i].dest_base = buf - rewind;
608 buf += cur->iov_len - MIN(rewind, cur->iov_len);
609 last_end = MAX(cur->iov_base + cur->iov_len, last_end);
610 }
611
612 /* Sort by source iovec index and build destination iovec */
613 qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_index);
614 for (i = 0; i < src->niov; i++) {
615 qemu_iovec_add(dest, sortelems[i].dest_base, src->iov[i].iov_len);
616 }
617}
618
Stefan Hajnoczi9dd6f7c2020-09-17 10:44:53 +0100619void iov_discard_undo(IOVDiscardUndo *undo)
620{
621 /* Restore original iovec if it was modified */
622 if (undo->modified_iov) {
623 *undo->modified_iov = undo->orig;
624 }
625}
626
627size_t iov_discard_front_undoable(struct iovec **iov,
628 unsigned int *iov_cnt,
629 size_t bytes,
630 IOVDiscardUndo *undo)
Stefan Hajnoczid0277632012-11-21 17:41:10 +0100631{
632 size_t total = 0;
633 struct iovec *cur;
634
Stefan Hajnoczi9dd6f7c2020-09-17 10:44:53 +0100635 if (undo) {
636 undo->modified_iov = NULL;
637 }
638
Stefan Hajnoczid0277632012-11-21 17:41:10 +0100639 for (cur = *iov; *iov_cnt > 0; cur++) {
640 if (cur->iov_len > bytes) {
Stefan Hajnoczi9dd6f7c2020-09-17 10:44:53 +0100641 if (undo) {
642 undo->modified_iov = cur;
643 undo->orig = *cur;
644 }
645
Stefan Hajnoczid0277632012-11-21 17:41:10 +0100646 cur->iov_base += bytes;
647 cur->iov_len -= bytes;
648 total += bytes;
649 break;
650 }
651
652 bytes -= cur->iov_len;
653 total += cur->iov_len;
654 *iov_cnt -= 1;
655 }
656
657 *iov = cur;
658 return total;
659}
660
Stefan Hajnoczi9dd6f7c2020-09-17 10:44:53 +0100661size_t iov_discard_front(struct iovec **iov, unsigned int *iov_cnt,
662 size_t bytes)
663{
664 return iov_discard_front_undoable(iov, iov_cnt, bytes, NULL);
665}
666
667size_t iov_discard_back_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 if (*iov_cnt == 0) {
680 return 0;
681 }
682
683 cur = iov + (*iov_cnt - 1);
684
685 while (*iov_cnt > 0) {
686 if (cur->iov_len > bytes) {
Stefan Hajnoczi9dd6f7c2020-09-17 10:44:53 +0100687 if (undo) {
688 undo->modified_iov = cur;
689 undo->orig = *cur;
690 }
691
Stefan Hajnoczid0277632012-11-21 17:41:10 +0100692 cur->iov_len -= bytes;
693 total += bytes;
694 break;
695 }
696
697 bytes -= cur->iov_len;
698 total += cur->iov_len;
699 cur--;
700 *iov_cnt -= 1;
701 }
702
703 return total;
704}
Kevin Wolf58f423f2014-07-09 19:17:30 +0200705
Stefan Hajnoczi9dd6f7c2020-09-17 10:44:53 +0100706size_t iov_discard_back(struct iovec *iov, unsigned int *iov_cnt,
707 size_t bytes)
708{
709 return iov_discard_back_undoable(iov, iov_cnt, bytes, NULL);
710}
711
Kevin Wolf58f423f2014-07-09 19:17:30 +0200712void qemu_iovec_discard_back(QEMUIOVector *qiov, size_t bytes)
713{
714 size_t total;
715 unsigned int niov = qiov->niov;
716
717 assert(qiov->size >= bytes);
718 total = iov_discard_back(qiov->iov, &niov, bytes);
719 assert(total == bytes);
720
721 qiov->niov = niov;
722 qiov->size -= bytes;
723}