]> git.proxmox.com Git - mirror_qemu.git/blob - util/iov.c
Merge tag 'for-upstream' of https://gitlab.com/bonzini/qemu into staging
[mirror_qemu.git] / util / iov.c
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/iov.h"
21 #include "qemu/sockets.h"
22 #include "qemu/cutils.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(fp, prefix, buf, 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;
250 i < iov_cnt && j < dst_iov_cnt && (offset || bytes); i++) {
251 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 }
266
267 /* io vectors */
268
269 void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint)
270 {
271 qiov->iov = g_new(struct iovec, alloc_hint);
272 qiov->niov = 0;
273 qiov->nalloc = alloc_hint;
274 qiov->size = 0;
275 }
276
277 void 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
289 void 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;
295 qiov->iov = g_renew(struct iovec, qiov->iov, qiov->nalloc);
296 }
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 /*
304 * 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 */
313 size_t qemu_iovec_concat_iov(QEMUIOVector *dst,
314 struct iovec *src_iov, unsigned int src_cnt,
315 size_t soffset, size_t sbytes)
316 {
317 int i;
318 size_t done;
319
320 if (!sbytes) {
321 return 0;
322 }
323 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 */
335
336 return done;
337 }
338
339 /*
340 * 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 */
349 void qemu_iovec_concat(QEMUIOVector *dst,
350 QEMUIOVector *src, size_t soffset, size_t sbytes)
351 {
352 qemu_iovec_concat_iov(dst, src->iov, src->niov, soffset, sbytes);
353 }
354
355 /*
356 * 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 */
362 static 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 */
381 static 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
403 int 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
413 /*
414 * Compile new iovec, combining @head_buf buffer, sub-qiov of @mid_qiov,
415 * and @tail_buf buffer into new qiov.
416 */
417 int qemu_iovec_init_extended(
418 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;
425 struct iovec *p, *mid_iov = NULL;
426
427 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
435 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;
441 if (total_niov > IOV_MAX) {
442 return -EINVAL;
443 }
444
445 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
460 assert(!mid_niov == !mid_len);
461 if (mid_niov) {
462 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 }
473
474 return 0;
475 }
476
477 /*
478 * Check if the contents of subrange of qiov data is all zeroes.
479 */
480 bool qemu_iovec_is_zero(QEMUIOVector *qiov, size_t offset, size_t bytes)
481 {
482 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)) {
494 return false;
495 }
496
497 current_offset = 0;
498 bytes -= len;
499 iov++;
500 }
501
502 return true;
503 }
504
505 void qemu_iovec_init_slice(QEMUIOVector *qiov, QEMUIOVector *source,
506 size_t offset, size_t len)
507 {
508 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);
516 }
517
518 void qemu_iovec_destroy(QEMUIOVector *qiov)
519 {
520 if (qiov->nalloc != -1) {
521 g_free(qiov->iov);
522 }
523
524 memset(qiov, 0, sizeof(*qiov));
525 }
526
527 void qemu_iovec_reset(QEMUIOVector *qiov)
528 {
529 assert(qiov->nalloc != -1);
530
531 qiov->niov = 0;
532 qiov->size = 0;
533 }
534
535 size_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
541 size_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
547 size_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 }
552
553 /**
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 */
563 ssize_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
588 typedef struct {
589 int src_index;
590 struct iovec *src_iov;
591 void *dest_base;
592 } IOVectorSortElem;
593
594 static 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
609 static 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 */
623 void 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
659 void 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
667 size_t iov_discard_front_undoable(struct iovec **iov,
668 unsigned int *iov_cnt,
669 size_t bytes,
670 IOVDiscardUndo *undo)
671 {
672 size_t total = 0;
673 struct iovec *cur;
674
675 if (undo) {
676 undo->modified_iov = NULL;
677 }
678
679 for (cur = *iov; *iov_cnt > 0; cur++) {
680 if (cur->iov_len > bytes) {
681 if (undo) {
682 undo->modified_iov = cur;
683 undo->orig = *cur;
684 }
685
686 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
701 size_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
707 size_t iov_discard_back_undoable(struct iovec *iov,
708 unsigned int *iov_cnt,
709 size_t bytes,
710 IOVDiscardUndo *undo)
711 {
712 size_t total = 0;
713 struct iovec *cur;
714
715 if (undo) {
716 undo->modified_iov = NULL;
717 }
718
719 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) {
727 if (undo) {
728 undo->modified_iov = cur;
729 undo->orig = *cur;
730 }
731
732 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 }
745
746 size_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
752 void 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 }