1/* 2 * Helpers for using (partial) iovecs. 3 * 4 * Copyright (C) 2010 Red Hat, Inc. 5 * 6 * Author(s): 7 * Amit Shah <amit.shah@redhat.com> 8 * Michael Tokarev <mjt@tls.msk.ru> 9 * 10 * This work is licensed under the terms of the GNU GPL, version 2. See 11 * the COPYING file in the top-level directory. 12 */ 13 14#include "qemu-common.h" 15 16/** 17 * count and return data size, in bytes, of an iovec 18 * starting at `iov' of `iov_cnt' number of elements. 19 */ 20size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt); 21 22/** 23 * Copy from single continuous buffer to scatter-gather vector of buffers 24 * (iovec) and back like memcpy() between two continuous memory regions. 25 * Data in single continuous buffer starting at address `buf' and 26 * `bytes' bytes long will be copied to/from an iovec `iov' with 27 * `iov_cnt' number of elements, starting at byte position `offset' 28 * within the iovec. If the iovec does not contain enough space, 29 * only part of data will be copied, up to the end of the iovec. 30 * Number of bytes actually copied will be returned, which is 31 * min(bytes, iov_size(iov)-offset) 32 * `Offset' must point to the inside of iovec. 33 * It is okay to use very large value for `bytes' since we're 34 * limited by the size of the iovec anyway, provided that the 35 * buffer pointed to by buf has enough space. One possible 36 * such "large" value is -1 (sinice size_t is unsigned), 37 * so specifying `-1' as `bytes' means 'up to the end of iovec'. 38 */ 39size_t iov_from_buf(const struct iovec *iov, unsigned int iov_cnt, 40 size_t offset, const void *buf, size_t bytes); 41size_t iov_to_buf(const struct iovec *iov, const unsigned int iov_cnt, 42 size_t offset, void *buf, size_t bytes); 43 44/** 45 * Set data bytes pointed out by iovec `iov' of size `iov_cnt' elements, 46 * starting at byte offset `start', to value `fillc', repeating it 47 * `bytes' number of times. `Offset' must point to the inside of iovec. 48 * If `bytes' is large enough, only last bytes portion of iovec, 49 * up to the end of it, will be filled with the specified value. 50 * Function return actual number of bytes processed, which is 51 * min(size, iov_size(iov) - offset). 52 * Again, it is okay to use large value for `bytes' to mean "up to the end". 53 */ 54size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt, 55 size_t offset, int fillc, size_t bytes); 56 57/* 58 * Send/recv data from/to iovec buffers directly 59 * 60 * `offset' bytes in the beginning of iovec buffer are skipped and 61 * next `bytes' bytes are used, which must be within data of iovec. 62 * 63 * r = iov_send_recv(sockfd, iov, iovcnt, offset, bytes, true); 64 * 65 * is logically equivalent to 66 * 67 * char *buf = malloc(bytes); 68 * iov_to_buf(iov, iovcnt, offset, buf, bytes); 69 * r = send(sockfd, buf, bytes, 0); 70 * free(buf); 71 * 72 * For iov_send_recv() _whole_ area being sent or received 73 * should be within the iovec, not only beginning of it. 74 */ 75ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt, 76 size_t offset, size_t bytes, bool do_send); 77#define iov_recv(sockfd, iov, iov_cnt, offset, bytes) \ 78 iov_send_recv(sockfd, iov, iov_cnt, offset, bytes, false) 79#define iov_send(sockfd, iov, iov_cnt, offset, bytes) \ 80 iov_send_recv(sockfd, iov, iov_cnt, offset, bytes, true) 81 82/** 83 * Produce a text hexdump of iovec `iov' with `iov_cnt' number of elements 84 * in file `fp', prefixing each line with `prefix' and processing not more 85 * than `limit' data bytes. 86 */ 87void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt, 88 FILE *fp, const char *prefix, size_t limit); 89 90/* 91 * Partial copy of vector from iov to dst_iov (data is not copied). 92 * dst_iov overlaps iov at a specified offset. 93 * size of dst_iov is at most bytes. dst vector count is returned. 94 */ 95unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt, 96 const struct iovec *iov, unsigned int iov_cnt, 97 size_t offset, size_t bytes); 98