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 #ifndef IOV_H 15 #define IOV_H 16 17 /** 18 * count and return data size, in bytes, of an iovec 19 * starting at `iov' of `iov_cnt' number of elements. 20 */ 21 size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt); 22 23 /** 24 * Copy from single continuous buffer to scatter-gather vector of buffers 25 * (iovec) and back like memcpy() between two continuous memory regions. 26 * Data in single continuous buffer starting at address `buf' and 27 * `bytes' bytes long will be copied to/from an iovec `iov' with 28 * `iov_cnt' number of elements, starting at byte position `offset' 29 * within the iovec. If the iovec does not contain enough space, 30 * only part of data will be copied, up to the end of the iovec. 31 * Number of bytes actually copied will be returned, which is 32 * min(bytes, iov_size(iov)-offset) 33 * `Offset' must point to the inside of iovec. 34 * It is okay to use very large value for `bytes' since we're 35 * limited by the size of the iovec anyway, provided that the 36 * buffer pointed to by buf has enough space. One possible 37 * such "large" value is -1 (sinice size_t is unsigned), 38 * so specifying `-1' as `bytes' means 'up to the end of iovec'. 39 */ 40 size_t iov_from_buf_full(const struct iovec *iov, unsigned int iov_cnt, 41 size_t offset, const void *buf, size_t bytes); 42 size_t iov_to_buf_full(const struct iovec *iov, const unsigned int iov_cnt, 43 size_t offset, void *buf, size_t bytes); 44 45 static inline size_t 46 iov_from_buf(const struct iovec *iov, unsigned int iov_cnt, 47 size_t offset, const void *buf, size_t bytes) 48 { 49 if (__builtin_constant_p(bytes) && iov_cnt && 50 offset <= iov[0].iov_len && bytes <= iov[0].iov_len - offset) { 51 memcpy(iov[0].iov_base + offset, buf, bytes); 52 return bytes; 53 } else { 54 return iov_from_buf_full(iov, iov_cnt, offset, buf, bytes); 55 } 56 } 57 58 static inline size_t 59 iov_to_buf(const struct iovec *iov, const unsigned int iov_cnt, 60 size_t offset, void *buf, size_t bytes) 61 { 62 if (__builtin_constant_p(bytes) && iov_cnt && 63 offset <= iov[0].iov_len && bytes <= iov[0].iov_len - offset) { 64 memcpy(buf, iov[0].iov_base + offset, bytes); 65 return bytes; 66 } else { 67 return iov_to_buf_full(iov, iov_cnt, offset, buf, bytes); 68 } 69 } 70 71 /** 72 * Set data bytes pointed out by iovec `iov' of size `iov_cnt' elements, 73 * starting at byte offset `start', to value `fillc', repeating it 74 * `bytes' number of times. `Offset' must point to the inside of iovec. 75 * If `bytes' is large enough, only last bytes portion of iovec, 76 * up to the end of it, will be filled with the specified value. 77 * Function return actual number of bytes processed, which is 78 * min(size, iov_size(iov) - offset). 79 * Again, it is okay to use large value for `bytes' to mean "up to the end". 80 */ 81 size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt, 82 size_t offset, int fillc, size_t bytes); 83 84 /* 85 * Send/recv data from/to iovec buffers directly 86 * 87 * `offset' bytes in the beginning of iovec buffer are skipped and 88 * next `bytes' bytes are used, which must be within data of iovec. 89 * 90 * r = iov_send_recv(sockfd, iov, iovcnt, offset, bytes, true); 91 * 92 * is logically equivalent to 93 * 94 * char *buf = malloc(bytes); 95 * iov_to_buf(iov, iovcnt, offset, buf, bytes); 96 * r = send(sockfd, buf, bytes, 0); 97 * free(buf); 98 * 99 * For iov_send_recv() _whole_ area being sent or received 100 * should be within the iovec, not only beginning of it. 101 */ 102 ssize_t iov_send_recv(int sockfd, const struct iovec *iov, unsigned iov_cnt, 103 size_t offset, size_t bytes, bool do_send); 104 #define iov_recv(sockfd, iov, iov_cnt, offset, bytes) \ 105 iov_send_recv(sockfd, iov, iov_cnt, offset, bytes, false) 106 #define iov_send(sockfd, iov, iov_cnt, offset, bytes) \ 107 iov_send_recv(sockfd, iov, iov_cnt, offset, bytes, true) 108 109 /** 110 * Produce a text hexdump of iovec `iov' with `iov_cnt' number of elements 111 * in file `fp', prefixing each line with `prefix' and processing not more 112 * than `limit' data bytes. 113 */ 114 void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt, 115 FILE *fp, const char *prefix, size_t limit); 116 117 /* 118 * Partial copy of vector from iov to dst_iov (data is not copied). 119 * dst_iov overlaps iov at a specified offset. 120 * size of dst_iov is at most bytes. dst vector count is returned. 121 */ 122 unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt, 123 const struct iovec *iov, unsigned int iov_cnt, 124 size_t offset, size_t bytes); 125 126 /* 127 * Remove a given number of bytes from the front or back of a vector. 128 * This may update iov and/or iov_cnt to exclude iovec elements that are 129 * no longer required. 130 * 131 * The number of bytes actually discarded is returned. This number may be 132 * smaller than requested if the vector is too small. 133 */ 134 size_t iov_discard_front(struct iovec **iov, unsigned int *iov_cnt, 135 size_t bytes); 136 size_t iov_discard_back(struct iovec *iov, unsigned int *iov_cnt, 137 size_t bytes); 138 139 typedef struct QEMUIOVector { 140 struct iovec *iov; 141 int niov; 142 int nalloc; 143 size_t size; 144 } QEMUIOVector; 145 146 void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint); 147 void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov); 148 void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len); 149 void qemu_iovec_concat(QEMUIOVector *dst, 150 QEMUIOVector *src, size_t soffset, size_t sbytes); 151 size_t qemu_iovec_concat_iov(QEMUIOVector *dst, 152 struct iovec *src_iov, unsigned int src_cnt, 153 size_t soffset, size_t sbytes); 154 bool qemu_iovec_is_zero(QEMUIOVector *qiov); 155 void qemu_iovec_destroy(QEMUIOVector *qiov); 156 void qemu_iovec_reset(QEMUIOVector *qiov); 157 size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset, 158 void *buf, size_t bytes); 159 size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset, 160 const void *buf, size_t bytes); 161 size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset, 162 int fillc, size_t bytes); 163 ssize_t qemu_iovec_compare(QEMUIOVector *a, QEMUIOVector *b); 164 void qemu_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src, void *buf); 165 void qemu_iovec_discard_back(QEMUIOVector *qiov, size_t bytes); 166 167 #endif 168