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 * Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates 7 * 8 * Author(s): 9 * Anthony Liguori <aliguori@us.ibm.com> 10 * Amit Shah <amit.shah@redhat.com> 11 * Michael Tokarev <mjt@tls.msk.ru> 12 * 13 * This work is licensed under the terms of the GNU GPL, version 2. See 14 * the COPYING file in the top-level directory. 15 * 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. 18 */ 19 20 #include "qemu/osdep.h" 21 #include "qemu/iov.h" 22 #include "qemu/sockets.h" 23 #include "qemu/cutils.h" 24 25 size_t iov_from_buf_full(const struct iovec *iov, unsigned int iov_cnt, 26 size_t offset, const void *buf, size_t bytes) 27 { 28 size_t done; 29 unsigned int i; 30 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; 38 } 39 } 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 return done; 59 } 60 61 size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt, 62 size_t offset, int fillc, size_t bytes) 63 { 64 size_t done; 65 unsigned int i; 66 for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) { 67 if (offset < iov[i].iov_len) { 68 size_t len = MIN(iov[i].iov_len - offset, bytes - done); 69 memset(iov[i].iov_base + offset, fillc, len); 70 done += len; 71 offset = 0; 72 } else { 73 offset -= iov[i].iov_len; 74 } 75 } 76 return done; 77 } 78 79 size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt) 80 { 81 size_t len; 82 unsigned int i; 83 84 len = 0; 85 for (i = 0; i < iov_cnt; i++) { 86 len += iov[i].iov_len; 87 } 88 return len; 89 } 90 91 /* helper function for iov_send_recv() */ 92 static ssize_t 93 do_send_recv(int sockfd, int flags, struct iovec *iov, unsigned iov_cnt, 94 bool do_send) 95 { 96 #ifdef CONFIG_POSIX 97 ssize_t ret; 98 struct msghdr msg; 99 memset(&msg, 0, sizeof(msg)); 100 msg.msg_iov = iov; 101 msg.msg_iovlen = iov_cnt; 102 do { 103 ret = do_send 104 ? sendmsg(sockfd, &msg, flags) 105 : recvmsg(sockfd, &msg, flags); 106 } while (ret < 0 && errno == EINTR); 107 return ret; 108 #else 109 /* else send piece-by-piece */ 110 /*XXX Note: windows has WSASend() and WSARecv() */ 111 unsigned i = 0; 112 ssize_t ret = 0; 113 ssize_t off = 0; 114 while (i < iov_cnt) { 115 ssize_t r = do_send 116 ? send(sockfd, iov[i].iov_base + off, iov[i].iov_len - off, flags) 117 : recv(sockfd, iov[i].iov_base + off, iov[i].iov_len - off, flags); 118 if (r > 0) { 119 ret += r; 120 off += r; 121 if (off < iov[i].iov_len) { 122 continue; 123 } 124 } else if (!r) { 125 break; 126 } else if (errno == EINTR) { 127 continue; 128 } else { 129 /* else it is some "other" error, 130 * only return if there was no data processed. */ 131 if (ret == 0) { 132 ret = -1; 133 } 134 break; 135 } 136 off = 0; 137 i++; 138 } 139 return ret; 140 #endif 141 } 142 143 ssize_t iov_send_recv(int sockfd, const struct iovec *_iov, unsigned iov_cnt, 144 size_t offset, size_t bytes, 145 bool do_send) 146 { 147 return iov_send_recv_with_flags(sockfd, 0, _iov, iov_cnt, offset, bytes, 148 do_send); 149 } 150 151 ssize_t iov_send_recv_with_flags(int sockfd, int sockflags, 152 const struct iovec *_iov, 153 unsigned iov_cnt, size_t offset, 154 size_t bytes, bool do_send) 155 { 156 ssize_t total = 0; 157 ssize_t ret; 158 size_t orig_len, tail; 159 unsigned niov; 160 struct iovec *local_iov, *iov; 161 162 if (bytes <= 0) { 163 return 0; 164 } 165 166 local_iov = g_new0(struct iovec, iov_cnt); 167 iov_copy(local_iov, iov_cnt, _iov, iov_cnt, offset, bytes); 168 offset = 0; 169 iov = local_iov; 170 171 while (bytes > 0) { 172 /* Find the start position, skipping `offset' bytes: 173 * first, skip all full-sized vector elements, */ 174 for (niov = 0; niov < iov_cnt && offset >= iov[niov].iov_len; ++niov) { 175 offset -= iov[niov].iov_len; 176 } 177 178 /* niov == iov_cnt would only be valid if bytes == 0, which 179 * we already ruled out in the loop condition. */ 180 assert(niov < iov_cnt); 181 iov += niov; 182 iov_cnt -= niov; 183 184 if (offset) { 185 /* second, skip `offset' bytes from the (now) first element, 186 * undo it on exit */ 187 iov[0].iov_base += offset; 188 iov[0].iov_len -= offset; 189 } 190 /* Find the end position skipping `bytes' bytes: */ 191 /* first, skip all full-sized elements */ 192 tail = bytes; 193 for (niov = 0; niov < iov_cnt && iov[niov].iov_len <= tail; ++niov) { 194 tail -= iov[niov].iov_len; 195 } 196 if (tail) { 197 /* second, fixup the last element, and remember the original 198 * length */ 199 assert(niov < iov_cnt); 200 assert(iov[niov].iov_len > tail); 201 orig_len = iov[niov].iov_len; 202 iov[niov++].iov_len = tail; 203 ret = do_send_recv(sockfd, sockflags, iov, niov, do_send); 204 /* Undo the changes above before checking for errors */ 205 iov[niov-1].iov_len = orig_len; 206 } else { 207 ret = do_send_recv(sockfd, sockflags, iov, niov, do_send); 208 } 209 if (offset) { 210 iov[0].iov_base -= offset; 211 iov[0].iov_len += offset; 212 } 213 214 if (ret < 0) { 215 assert(errno != EINTR); 216 g_free(local_iov); 217 if (errno == EAGAIN && total > 0) { 218 return total; 219 } 220 return -1; 221 } 222 223 if (ret == 0 && !do_send) { 224 /* recv returns 0 when the peer has performed an orderly 225 * shutdown. */ 226 break; 227 } 228 229 /* Prepare for the next iteration */ 230 offset += ret; 231 total += ret; 232 bytes -= ret; 233 } 234 235 g_free(local_iov); 236 return total; 237 } 238 239 240 void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt, 241 FILE *fp, const char *prefix, size_t limit) 242 { 243 int v; 244 size_t size = 0; 245 char *buf; 246 247 for (v = 0; v < iov_cnt; v++) { 248 size += iov[v].iov_len; 249 } 250 size = size > limit ? limit : size; 251 buf = g_malloc(size); 252 iov_to_buf(iov, iov_cnt, 0, buf, size); 253 qemu_hexdump(fp, prefix, buf, size); 254 g_free(buf); 255 } 256 257 unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt, 258 const struct iovec *iov, unsigned int iov_cnt, 259 size_t offset, size_t bytes) 260 { 261 size_t len; 262 unsigned int i, j; 263 for (i = 0, j = 0; 264 i < iov_cnt && j < dst_iov_cnt && (offset || bytes); i++) { 265 if (offset >= iov[i].iov_len) { 266 offset -= iov[i].iov_len; 267 continue; 268 } 269 len = MIN(bytes, iov[i].iov_len - offset); 270 271 dst_iov[j].iov_base = iov[i].iov_base + offset; 272 dst_iov[j].iov_len = len; 273 j++; 274 bytes -= len; 275 offset = 0; 276 } 277 return j; 278 } 279 280 /* io vectors */ 281 282 void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint) 283 { 284 qiov->iov = g_new(struct iovec, alloc_hint); 285 qiov->niov = 0; 286 qiov->nalloc = alloc_hint; 287 qiov->size = 0; 288 } 289 290 void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov) 291 { 292 int i; 293 294 qiov->iov = iov; 295 qiov->niov = niov; 296 qiov->nalloc = -1; 297 qiov->size = 0; 298 for (i = 0; i < niov; i++) 299 qiov->size += iov[i].iov_len; 300 } 301 302 void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len) 303 { 304 assert(qiov->nalloc != -1); 305 306 if (qiov->niov == qiov->nalloc) { 307 qiov->nalloc = 2 * qiov->nalloc + 1; 308 qiov->iov = g_renew(struct iovec, qiov->iov, qiov->nalloc); 309 } 310 qiov->iov[qiov->niov].iov_base = base; 311 qiov->iov[qiov->niov].iov_len = len; 312 qiov->size += len; 313 ++qiov->niov; 314 } 315 316 /* 317 * Concatenates (partial) iovecs from src_iov to the end of dst. 318 * It starts copying after skipping `soffset' bytes at the 319 * beginning of src and adds individual vectors from src to 320 * dst copies up to `sbytes' bytes total, or up to the end 321 * of src_iov if it comes first. This way, it is okay to specify 322 * very large value for `sbytes' to indicate "up to the end 323 * of src". 324 * Only vector pointers are processed, not the actual data buffers. 325 */ 326 size_t qemu_iovec_concat_iov(QEMUIOVector *dst, 327 struct iovec *src_iov, unsigned int src_cnt, 328 size_t soffset, size_t sbytes) 329 { 330 int i; 331 size_t done; 332 333 if (!sbytes) { 334 return 0; 335 } 336 assert(dst->nalloc != -1); 337 for (i = 0, done = 0; done < sbytes && i < src_cnt; i++) { 338 if (soffset < src_iov[i].iov_len) { 339 size_t len = MIN(src_iov[i].iov_len - soffset, sbytes - done); 340 qemu_iovec_add(dst, src_iov[i].iov_base + soffset, len); 341 done += len; 342 soffset = 0; 343 } else { 344 soffset -= src_iov[i].iov_len; 345 } 346 } 347 348 return done; 349 } 350 351 /* 352 * Concatenates (partial) iovecs from src to the end of dst. 353 * It starts copying after skipping `soffset' bytes at the 354 * beginning of src and adds individual vectors from src to 355 * dst copies up to `sbytes' bytes total, or up to the end 356 * of src if it comes first. This way, it is okay to specify 357 * very large value for `sbytes' to indicate "up to the end 358 * of src". 359 * Only vector pointers are processed, not the actual data buffers. 360 */ 361 void qemu_iovec_concat(QEMUIOVector *dst, 362 QEMUIOVector *src, size_t soffset, size_t sbytes) 363 { 364 qemu_iovec_concat_iov(dst, src->iov, src->niov, soffset, sbytes); 365 } 366 367 /* 368 * qiov_find_iov 369 * 370 * Return pointer to iovec structure, where byte at @offset in original vector 371 * @iov exactly is. 372 * Set @remaining_offset to be offset inside that iovec to the same byte. 373 */ 374 static struct iovec *iov_skip_offset(struct iovec *iov, size_t offset, 375 size_t *remaining_offset) 376 { 377 while (offset > 0 && offset >= iov->iov_len) { 378 offset -= iov->iov_len; 379 iov++; 380 } 381 *remaining_offset = offset; 382 383 return iov; 384 } 385 386 /* 387 * qemu_iovec_slice 388 * 389 * Find subarray of iovec's, containing requested range. @head would 390 * be offset in first iov (returned by the function), @tail would be 391 * count of extra bytes in last iovec (returned iov + @niov - 1). 392 */ 393 struct iovec *qemu_iovec_slice(QEMUIOVector *qiov, 394 size_t offset, size_t len, 395 size_t *head, size_t *tail, int *niov) 396 { 397 struct iovec *iov, *end_iov; 398 399 assert(offset + len <= qiov->size); 400 401 iov = iov_skip_offset(qiov->iov, offset, head); 402 end_iov = iov_skip_offset(iov, *head + len, tail); 403 404 if (*tail > 0) { 405 assert(*tail < end_iov->iov_len); 406 *tail = end_iov->iov_len - *tail; 407 end_iov++; 408 } 409 410 *niov = end_iov - iov; 411 412 return iov; 413 } 414 415 int qemu_iovec_subvec_niov(QEMUIOVector *qiov, size_t offset, size_t len) 416 { 417 size_t head, tail; 418 int niov; 419 420 qemu_iovec_slice(qiov, offset, len, &head, &tail, &niov); 421 422 return niov; 423 } 424 425 /* 426 * Check if the contents of subrange of qiov data is all zeroes. 427 */ 428 bool qemu_iovec_is_zero(QEMUIOVector *qiov, size_t offset, size_t bytes) 429 { 430 struct iovec *iov; 431 size_t current_offset; 432 433 assert(offset + bytes <= qiov->size); 434 435 iov = iov_skip_offset(qiov->iov, offset, ¤t_offset); 436 437 while (bytes) { 438 uint8_t *base = (uint8_t *)iov->iov_base + current_offset; 439 size_t len = MIN(iov->iov_len - current_offset, bytes); 440 441 if (!buffer_is_zero(base, len)) { 442 return false; 443 } 444 445 current_offset = 0; 446 bytes -= len; 447 iov++; 448 } 449 450 return true; 451 } 452 453 void qemu_iovec_init_slice(QEMUIOVector *qiov, QEMUIOVector *source, 454 size_t offset, size_t len) 455 { 456 struct iovec *slice_iov; 457 int slice_niov; 458 size_t slice_head, slice_tail; 459 460 assert(source->size >= len); 461 assert(source->size - len >= offset); 462 463 slice_iov = qemu_iovec_slice(source, offset, len, 464 &slice_head, &slice_tail, &slice_niov); 465 if (slice_niov == 1) { 466 qemu_iovec_init_buf(qiov, slice_iov[0].iov_base + slice_head, len); 467 } else { 468 qemu_iovec_init(qiov, slice_niov); 469 qemu_iovec_concat_iov(qiov, slice_iov, slice_niov, slice_head, len); 470 } 471 } 472 473 void qemu_iovec_destroy(QEMUIOVector *qiov) 474 { 475 if (qiov->nalloc != -1) { 476 g_free(qiov->iov); 477 } 478 479 memset(qiov, 0, sizeof(*qiov)); 480 } 481 482 void qemu_iovec_reset(QEMUIOVector *qiov) 483 { 484 assert(qiov->nalloc != -1); 485 486 qiov->niov = 0; 487 qiov->size = 0; 488 } 489 490 size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset, 491 void *buf, size_t bytes) 492 { 493 return iov_to_buf(qiov->iov, qiov->niov, offset, buf, bytes); 494 } 495 496 size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset, 497 const void *buf, size_t bytes) 498 { 499 return iov_from_buf(qiov->iov, qiov->niov, offset, buf, bytes); 500 } 501 502 size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset, 503 int fillc, size_t bytes) 504 { 505 return iov_memset(qiov->iov, qiov->niov, offset, fillc, bytes); 506 } 507 508 /** 509 * Check that I/O vector contents are identical 510 * 511 * The IO vectors must have the same structure (same length of all parts). 512 * A typical usage is to compare vectors created with qemu_iovec_clone(). 513 * 514 * @a: I/O vector 515 * @b: I/O vector 516 * @ret: Offset to first mismatching byte or -1 if match 517 */ 518 ssize_t qemu_iovec_compare(QEMUIOVector *a, QEMUIOVector *b) 519 { 520 int i; 521 ssize_t offset = 0; 522 523 assert(a->niov == b->niov); 524 for (i = 0; i < a->niov; i++) { 525 size_t len = 0; 526 uint8_t *p = (uint8_t *)a->iov[i].iov_base; 527 uint8_t *q = (uint8_t *)b->iov[i].iov_base; 528 529 assert(a->iov[i].iov_len == b->iov[i].iov_len); 530 while (len < a->iov[i].iov_len && *p++ == *q++) { 531 len++; 532 } 533 534 offset += len; 535 536 if (len != a->iov[i].iov_len) { 537 return offset; 538 } 539 } 540 return -1; 541 } 542 543 typedef struct { 544 int src_index; 545 struct iovec *src_iov; 546 void *dest_base; 547 } IOVectorSortElem; 548 549 static int sortelem_cmp_src_base(const void *a, const void *b) 550 { 551 const IOVectorSortElem *elem_a = a; 552 const IOVectorSortElem *elem_b = b; 553 554 /* Don't overflow */ 555 if (elem_a->src_iov->iov_base < elem_b->src_iov->iov_base) { 556 return -1; 557 } else if (elem_a->src_iov->iov_base > elem_b->src_iov->iov_base) { 558 return 1; 559 } else { 560 return 0; 561 } 562 } 563 564 static int sortelem_cmp_src_index(const void *a, const void *b) 565 { 566 const IOVectorSortElem *elem_a = a; 567 const IOVectorSortElem *elem_b = b; 568 569 return elem_a->src_index - elem_b->src_index; 570 } 571 572 /** 573 * Copy contents of I/O vector 574 * 575 * The relative relationships of overlapping iovecs are preserved. This is 576 * necessary to ensure identical semantics in the cloned I/O vector. 577 */ 578 void qemu_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src, void *buf) 579 { 580 g_autofree IOVectorSortElem *sortelems = g_new(IOVectorSortElem, src->niov); 581 void *last_end; 582 int i; 583 584 /* Sort by source iovecs by base address */ 585 for (i = 0; i < src->niov; i++) { 586 sortelems[i].src_index = i; 587 sortelems[i].src_iov = &src->iov[i]; 588 } 589 qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_base); 590 591 /* Allocate buffer space taking into account overlapping iovecs */ 592 last_end = NULL; 593 for (i = 0; i < src->niov; i++) { 594 struct iovec *cur = sortelems[i].src_iov; 595 ptrdiff_t rewind = 0; 596 597 /* Detect overlap */ 598 if (last_end && last_end > cur->iov_base) { 599 rewind = last_end - cur->iov_base; 600 } 601 602 sortelems[i].dest_base = buf - rewind; 603 buf += cur->iov_len - MIN(rewind, cur->iov_len); 604 last_end = MAX(cur->iov_base + cur->iov_len, last_end); 605 } 606 607 /* Sort by source iovec index and build destination iovec */ 608 qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_index); 609 for (i = 0; i < src->niov; i++) { 610 qemu_iovec_add(dest, sortelems[i].dest_base, src->iov[i].iov_len); 611 } 612 } 613 614 void iov_discard_undo(IOVDiscardUndo *undo) 615 { 616 /* Restore original iovec if it was modified */ 617 if (undo->modified_iov) { 618 *undo->modified_iov = undo->orig; 619 } 620 } 621 622 size_t iov_discard_front_undoable(struct iovec **iov, 623 unsigned int *iov_cnt, 624 size_t bytes, 625 IOVDiscardUndo *undo) 626 { 627 size_t total = 0; 628 struct iovec *cur; 629 630 if (undo) { 631 undo->modified_iov = NULL; 632 } 633 634 for (cur = *iov; *iov_cnt > 0; cur++) { 635 if (cur->iov_len > bytes) { 636 if (undo) { 637 undo->modified_iov = cur; 638 undo->orig = *cur; 639 } 640 641 cur->iov_base += bytes; 642 cur->iov_len -= bytes; 643 total += bytes; 644 break; 645 } 646 647 bytes -= cur->iov_len; 648 total += cur->iov_len; 649 *iov_cnt -= 1; 650 } 651 652 *iov = cur; 653 return total; 654 } 655 656 size_t iov_discard_front(struct iovec **iov, unsigned int *iov_cnt, 657 size_t bytes) 658 { 659 return iov_discard_front_undoable(iov, iov_cnt, bytes, NULL); 660 } 661 662 size_t iov_discard_back_undoable(struct iovec *iov, 663 unsigned int *iov_cnt, 664 size_t bytes, 665 IOVDiscardUndo *undo) 666 { 667 size_t total = 0; 668 struct iovec *cur; 669 670 if (undo) { 671 undo->modified_iov = NULL; 672 } 673 674 if (*iov_cnt == 0) { 675 return 0; 676 } 677 678 cur = iov + (*iov_cnt - 1); 679 680 while (*iov_cnt > 0) { 681 if (cur->iov_len > bytes) { 682 if (undo) { 683 undo->modified_iov = cur; 684 undo->orig = *cur; 685 } 686 687 cur->iov_len -= bytes; 688 total += bytes; 689 break; 690 } 691 692 bytes -= cur->iov_len; 693 total += cur->iov_len; 694 cur--; 695 *iov_cnt -= 1; 696 } 697 698 return total; 699 } 700 701 size_t iov_discard_back(struct iovec *iov, unsigned int *iov_cnt, 702 size_t bytes) 703 { 704 return iov_discard_back_undoable(iov, iov_cnt, bytes, NULL); 705 } 706 707 void qemu_iovec_discard_back(QEMUIOVector *qiov, size_t bytes) 708 { 709 size_t total; 710 unsigned int niov = qiov->niov; 711 712 assert(qiov->size >= bytes); 713 total = iov_discard_back(qiov->iov, &niov, bytes); 714 assert(total == bytes); 715 716 qiov->niov = niov; 717 qiov->size -= bytes; 718 } 719