Amit Shah | e4d5639 | 2010-04-27 18:04:05 +0530 | [diff] [blame] | 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> |
Michael Tokarev | 2278a69 | 2012-06-07 20:08:19 +0400 | [diff] [blame] | 10 | * Michael Tokarev <mjt@tls.msk.ru> |
Amit Shah | e4d5639 | 2010-04-27 18:04:05 +0530 | [diff] [blame] | 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. |
Paolo Bonzini | 6b620ca | 2012-01-13 17:44:23 +0100 | [diff] [blame] | 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. |
Amit Shah | e4d5639 | 2010-04-27 18:04:05 +0530 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include "iov.h" |
| 20 | |
Michael Tokarev | 25e5e4c | 2012-03-14 11:18:54 +0400 | [diff] [blame] | 21 | #ifdef _WIN32 |
| 22 | # include <windows.h> |
| 23 | # include <winsock2.h> |
| 24 | #else |
| 25 | # include <sys/types.h> |
| 26 | # include <sys/socket.h> |
| 27 | #endif |
| 28 | |
Michael S. Tsirkin | 844b5ce | 2012-09-24 12:50:32 +0200 | [diff] [blame] | 29 | size_t iov_from_buf(const struct iovec *iov, unsigned int iov_cnt, |
Michael Tokarev | 2278a69 | 2012-06-07 20:08:19 +0400 | [diff] [blame] | 30 | size_t offset, const void *buf, size_t bytes) |
Amit Shah | e4d5639 | 2010-04-27 18:04:05 +0530 | [diff] [blame] | 31 | { |
Michael Tokarev | 2278a69 | 2012-06-07 20:08:19 +0400 | [diff] [blame] | 32 | size_t done; |
Amit Shah | e4d5639 | 2010-04-27 18:04:05 +0530 | [diff] [blame] | 33 | unsigned int i; |
Michael Tokarev | 2278a69 | 2012-06-07 20:08:19 +0400 | [diff] [blame] | 34 | for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) { |
| 35 | if (offset < iov[i].iov_len) { |
| 36 | size_t len = MIN(iov[i].iov_len - offset, bytes - done); |
| 37 | memcpy(iov[i].iov_base + offset, buf + done, len); |
| 38 | done += len; |
| 39 | offset = 0; |
| 40 | } else { |
| 41 | offset -= iov[i].iov_len; |
Amit Shah | fa6111f | 2010-04-27 18:04:06 +0530 | [diff] [blame] | 42 | } |
Amit Shah | fa6111f | 2010-04-27 18:04:06 +0530 | [diff] [blame] | 43 | } |
Michael Tokarev | 2278a69 | 2012-06-07 20:08:19 +0400 | [diff] [blame] | 44 | assert(offset == 0); |
| 45 | return done; |
Amit Shah | fa6111f | 2010-04-27 18:04:06 +0530 | [diff] [blame] | 46 | } |
| 47 | |
Michael Tokarev | 2278a69 | 2012-06-07 20:08:19 +0400 | [diff] [blame] | 48 | size_t iov_to_buf(const struct iovec *iov, const unsigned int iov_cnt, |
| 49 | size_t offset, void *buf, size_t bytes) |
Hannes Reinecke | 348e7b8 | 2011-07-11 15:02:23 +0200 | [diff] [blame] | 50 | { |
Michael Tokarev | 2278a69 | 2012-06-07 20:08:19 +0400 | [diff] [blame] | 51 | size_t done; |
Hannes Reinecke | 348e7b8 | 2011-07-11 15:02:23 +0200 | [diff] [blame] | 52 | unsigned int i; |
Michael Tokarev | 2278a69 | 2012-06-07 20:08:19 +0400 | [diff] [blame] | 53 | for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) { |
| 54 | if (offset < iov[i].iov_len) { |
| 55 | size_t len = MIN(iov[i].iov_len - offset, bytes - done); |
| 56 | memcpy(buf + done, iov[i].iov_base + offset, len); |
| 57 | done += len; |
| 58 | offset = 0; |
| 59 | } else { |
| 60 | offset -= iov[i].iov_len; |
Hannes Reinecke | 348e7b8 | 2011-07-11 15:02:23 +0200 | [diff] [blame] | 61 | } |
Hannes Reinecke | 348e7b8 | 2011-07-11 15:02:23 +0200 | [diff] [blame] | 62 | } |
Michael Tokarev | 2278a69 | 2012-06-07 20:08:19 +0400 | [diff] [blame] | 63 | assert(offset == 0); |
| 64 | return done; |
Hannes Reinecke | 348e7b8 | 2011-07-11 15:02:23 +0200 | [diff] [blame] | 65 | } |
| 66 | |
Michael Tokarev | dcf6f5e | 2012-03-11 18:05:12 +0400 | [diff] [blame] | 67 | size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt, |
Michael Tokarev | 2278a69 | 2012-06-07 20:08:19 +0400 | [diff] [blame] | 68 | size_t offset, int fillc, size_t bytes) |
Gerd Hoffmann | 8d15028 | 2011-07-13 15:16:08 +0200 | [diff] [blame] | 69 | { |
Michael Tokarev | 2278a69 | 2012-06-07 20:08:19 +0400 | [diff] [blame] | 70 | size_t done; |
Gerd Hoffmann | 8d15028 | 2011-07-13 15:16:08 +0200 | [diff] [blame] | 71 | unsigned int i; |
Michael Tokarev | 2278a69 | 2012-06-07 20:08:19 +0400 | [diff] [blame] | 72 | for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) { |
| 73 | if (offset < iov[i].iov_len) { |
| 74 | size_t len = MIN(iov[i].iov_len - offset, bytes - done); |
| 75 | memset(iov[i].iov_base + offset, fillc, len); |
| 76 | done += len; |
| 77 | offset = 0; |
| 78 | } else { |
| 79 | offset -= iov[i].iov_len; |
Gerd Hoffmann | 8d15028 | 2011-07-13 15:16:08 +0200 | [diff] [blame] | 80 | } |
Gerd Hoffmann | 8d15028 | 2011-07-13 15:16:08 +0200 | [diff] [blame] | 81 | } |
Michael Tokarev | 2278a69 | 2012-06-07 20:08:19 +0400 | [diff] [blame] | 82 | assert(offset == 0); |
| 83 | return done; |
Gerd Hoffmann | 8d15028 | 2011-07-13 15:16:08 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Hannes Reinecke | 348e7b8 | 2011-07-11 15:02:23 +0200 | [diff] [blame] | 86 | size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt) |
Amit Shah | fa6111f | 2010-04-27 18:04:06 +0530 | [diff] [blame] | 87 | { |
| 88 | size_t len; |
| 89 | unsigned int i; |
| 90 | |
| 91 | len = 0; |
Hannes Reinecke | 348e7b8 | 2011-07-11 15:02:23 +0200 | [diff] [blame] | 92 | for (i = 0; i < iov_cnt; i++) { |
Amit Shah | fa6111f | 2010-04-27 18:04:06 +0530 | [diff] [blame] | 93 | len += iov[i].iov_len; |
| 94 | } |
| 95 | return len; |
| 96 | } |
Gerd Hoffmann | 3a1dca9 | 2011-07-12 13:35:10 +0200 | [diff] [blame] | 97 | |
Michael Tokarev | 25e5e4c | 2012-03-14 11:18:54 +0400 | [diff] [blame] | 98 | /* helper function for iov_send_recv() */ |
| 99 | static ssize_t |
| 100 | do_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt, bool do_send) |
| 101 | { |
| 102 | #if defined CONFIG_IOVEC && defined CONFIG_POSIX |
| 103 | ssize_t ret; |
| 104 | struct msghdr msg; |
| 105 | memset(&msg, 0, sizeof(msg)); |
| 106 | msg.msg_iov = iov; |
| 107 | msg.msg_iovlen = iov_cnt; |
| 108 | do { |
| 109 | ret = do_send |
| 110 | ? sendmsg(sockfd, &msg, 0) |
| 111 | : recvmsg(sockfd, &msg, 0); |
| 112 | } while (ret < 0 && errno == EINTR); |
| 113 | return ret; |
| 114 | #else |
| 115 | /* else send piece-by-piece */ |
| 116 | /*XXX Note: windows has WSASend() and WSARecv() */ |
Stefan Weil | c095855 | 2012-07-11 07:09:05 +0200 | [diff] [blame] | 117 | unsigned i = 0; |
| 118 | ssize_t ret = 0; |
| 119 | while (i < iov_cnt) { |
Michael Tokarev | 25e5e4c | 2012-03-14 11:18:54 +0400 | [diff] [blame] | 120 | ssize_t r = do_send |
| 121 | ? send(sockfd, iov[i].iov_base, iov[i].iov_len, 0) |
| 122 | : recv(sockfd, iov[i].iov_base, iov[i].iov_len, 0); |
| 123 | if (r > 0) { |
| 124 | ret += r; |
| 125 | } else if (!r) { |
| 126 | break; |
| 127 | } else if (errno == EINTR) { |
| 128 | continue; |
| 129 | } else { |
| 130 | /* else it is some "other" error, |
| 131 | * only return if there was no data processed. */ |
| 132 | if (ret == 0) { |
Stefan Weil | c095855 | 2012-07-11 07:09:05 +0200 | [diff] [blame] | 133 | ret = -1; |
Michael Tokarev | 25e5e4c | 2012-03-14 11:18:54 +0400 | [diff] [blame] | 134 | } |
| 135 | break; |
| 136 | } |
Stefan Weil | c095855 | 2012-07-11 07:09:05 +0200 | [diff] [blame] | 137 | i++; |
Michael Tokarev | 25e5e4c | 2012-03-14 11:18:54 +0400 | [diff] [blame] | 138 | } |
Stefan Weil | c095855 | 2012-07-11 07:09:05 +0200 | [diff] [blame] | 139 | return ret; |
Michael Tokarev | 25e5e4c | 2012-03-14 11:18:54 +0400 | [diff] [blame] | 140 | #endif |
| 141 | } |
| 142 | |
| 143 | ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt, |
| 144 | size_t offset, size_t bytes, |
| 145 | bool do_send) |
| 146 | { |
| 147 | ssize_t ret; |
| 148 | unsigned si, ei; /* start and end indexes */ |
Peter Maydell | bb95586 | 2012-08-11 22:24:35 +0100 | [diff] [blame] | 149 | if (bytes == 0) { |
| 150 | /* Catch the do-nothing case early, as otherwise we will pass an |
| 151 | * empty iovec to sendmsg/recvmsg(), and not all implementations |
| 152 | * accept this. |
| 153 | */ |
| 154 | return 0; |
| 155 | } |
Michael Tokarev | 25e5e4c | 2012-03-14 11:18:54 +0400 | [diff] [blame] | 156 | |
| 157 | /* Find the start position, skipping `offset' bytes: |
| 158 | * first, skip all full-sized vector elements, */ |
| 159 | for (si = 0; si < iov_cnt && offset >= iov[si].iov_len; ++si) { |
| 160 | offset -= iov[si].iov_len; |
| 161 | } |
| 162 | if (offset) { |
| 163 | assert(si < iov_cnt); |
| 164 | /* second, skip `offset' bytes from the (now) first element, |
| 165 | * undo it on exit */ |
| 166 | iov[si].iov_base += offset; |
| 167 | iov[si].iov_len -= offset; |
| 168 | } |
| 169 | /* Find the end position skipping `bytes' bytes: */ |
| 170 | /* first, skip all full-sized elements */ |
| 171 | for (ei = si; ei < iov_cnt && iov[ei].iov_len <= bytes; ++ei) { |
| 172 | bytes -= iov[ei].iov_len; |
| 173 | } |
| 174 | if (bytes) { |
| 175 | /* second, fixup the last element, and remember |
| 176 | * the length we've cut from the end of it in `bytes' */ |
| 177 | size_t tail; |
| 178 | assert(ei < iov_cnt); |
| 179 | assert(iov[ei].iov_len > bytes); |
| 180 | tail = iov[ei].iov_len - bytes; |
| 181 | iov[ei].iov_len = bytes; |
| 182 | bytes = tail; /* bytes is now equal to the tail size */ |
| 183 | ++ei; |
| 184 | } |
| 185 | |
| 186 | ret = do_send_recv(sockfd, iov + si, ei - si, do_send); |
| 187 | |
| 188 | /* Undo the changes above */ |
| 189 | if (offset) { |
| 190 | iov[si].iov_base -= offset; |
| 191 | iov[si].iov_len += offset; |
| 192 | } |
| 193 | if (bytes) { |
| 194 | iov[ei-1].iov_len += bytes; |
| 195 | } |
| 196 | |
| 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | |
Gerd Hoffmann | 3a1dca9 | 2011-07-12 13:35:10 +0200 | [diff] [blame] | 201 | void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt, |
| 202 | FILE *fp, const char *prefix, size_t limit) |
| 203 | { |
| 204 | unsigned int i, v, b; |
| 205 | uint8_t *c; |
| 206 | |
| 207 | c = iov[0].iov_base; |
| 208 | for (i = 0, v = 0, b = 0; b < limit; i++, b++) { |
| 209 | if (i == iov[v].iov_len) { |
| 210 | i = 0; v++; |
| 211 | if (v == iov_cnt) { |
| 212 | break; |
| 213 | } |
| 214 | c = iov[v].iov_base; |
| 215 | } |
| 216 | if ((b % 16) == 0) { |
| 217 | fprintf(fp, "%s: %04x:", prefix, b); |
| 218 | } |
| 219 | if ((b % 4) == 0) { |
| 220 | fprintf(fp, " "); |
| 221 | } |
| 222 | fprintf(fp, " %02x", c[i]); |
| 223 | if ((b % 16) == 15) { |
| 224 | fprintf(fp, "\n"); |
| 225 | } |
| 226 | } |
| 227 | if ((b % 16) != 0) { |
| 228 | fprintf(fp, "\n"); |
| 229 | } |
| 230 | } |
Paolo Bonzini | 0191253 | 2012-10-24 14:58:39 +0200 | [diff] [blame] | 231 | |
Michael S. Tsirkin | d336336 | 2012-09-24 13:02:52 +0200 | [diff] [blame] | 232 | unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt, |
| 233 | const struct iovec *iov, unsigned int iov_cnt, |
| 234 | size_t offset, size_t bytes) |
| 235 | { |
| 236 | size_t len; |
| 237 | unsigned int i, j; |
| 238 | for (i = 0, j = 0; i < iov_cnt && j < dst_iov_cnt && bytes; i++) { |
| 239 | if (offset >= iov[i].iov_len) { |
| 240 | offset -= iov[i].iov_len; |
| 241 | continue; |
| 242 | } |
| 243 | len = MIN(bytes, iov[i].iov_len - offset); |
| 244 | |
| 245 | dst_iov[j].iov_base = iov[i].iov_base + offset; |
| 246 | dst_iov[j].iov_len = len; |
| 247 | j++; |
| 248 | bytes -= len; |
| 249 | offset = 0; |
| 250 | } |
| 251 | assert(offset == 0); |
| 252 | return j; |
| 253 | } |
Paolo Bonzini | f563a5d | 2012-10-31 10:42:51 +0100 | [diff] [blame] | 254 | |
Paolo Bonzini | 0191253 | 2012-10-24 14:58:39 +0200 | [diff] [blame] | 255 | /* io vectors */ |
| 256 | |
| 257 | void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint) |
| 258 | { |
| 259 | qiov->iov = g_malloc(alloc_hint * sizeof(struct iovec)); |
| 260 | qiov->niov = 0; |
| 261 | qiov->nalloc = alloc_hint; |
| 262 | qiov->size = 0; |
| 263 | } |
| 264 | |
| 265 | void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov) |
| 266 | { |
| 267 | int i; |
| 268 | |
| 269 | qiov->iov = iov; |
| 270 | qiov->niov = niov; |
| 271 | qiov->nalloc = -1; |
| 272 | qiov->size = 0; |
| 273 | for (i = 0; i < niov; i++) |
| 274 | qiov->size += iov[i].iov_len; |
| 275 | } |
| 276 | |
| 277 | void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len) |
| 278 | { |
| 279 | assert(qiov->nalloc != -1); |
| 280 | |
| 281 | if (qiov->niov == qiov->nalloc) { |
| 282 | qiov->nalloc = 2 * qiov->nalloc + 1; |
| 283 | qiov->iov = g_realloc(qiov->iov, qiov->nalloc * sizeof(struct iovec)); |
| 284 | } |
| 285 | qiov->iov[qiov->niov].iov_base = base; |
| 286 | qiov->iov[qiov->niov].iov_len = len; |
| 287 | qiov->size += len; |
| 288 | ++qiov->niov; |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | * Concatenates (partial) iovecs from src to the end of dst. |
| 293 | * It starts copying after skipping `soffset' bytes at the |
| 294 | * beginning of src and adds individual vectors from src to |
| 295 | * dst copies up to `sbytes' bytes total, or up to the end |
| 296 | * of src if it comes first. This way, it is okay to specify |
| 297 | * very large value for `sbytes' to indicate "up to the end |
| 298 | * of src". |
| 299 | * Only vector pointers are processed, not the actual data buffers. |
| 300 | */ |
| 301 | void qemu_iovec_concat(QEMUIOVector *dst, |
| 302 | QEMUIOVector *src, size_t soffset, size_t sbytes) |
| 303 | { |
| 304 | int i; |
| 305 | size_t done; |
| 306 | struct iovec *siov = src->iov; |
| 307 | assert(dst->nalloc != -1); |
| 308 | assert(src->size >= soffset); |
| 309 | for (i = 0, done = 0; done < sbytes && i < src->niov; i++) { |
| 310 | if (soffset < siov[i].iov_len) { |
| 311 | size_t len = MIN(siov[i].iov_len - soffset, sbytes - done); |
| 312 | qemu_iovec_add(dst, siov[i].iov_base + soffset, len); |
| 313 | done += len; |
| 314 | soffset = 0; |
| 315 | } else { |
| 316 | soffset -= siov[i].iov_len; |
| 317 | } |
| 318 | } |
| 319 | /* return done; */ |
| 320 | } |
| 321 | |
| 322 | void qemu_iovec_destroy(QEMUIOVector *qiov) |
| 323 | { |
| 324 | assert(qiov->nalloc != -1); |
| 325 | |
| 326 | qemu_iovec_reset(qiov); |
| 327 | g_free(qiov->iov); |
| 328 | qiov->nalloc = 0; |
| 329 | qiov->iov = NULL; |
| 330 | } |
| 331 | |
| 332 | void qemu_iovec_reset(QEMUIOVector *qiov) |
| 333 | { |
| 334 | assert(qiov->nalloc != -1); |
| 335 | |
| 336 | qiov->niov = 0; |
| 337 | qiov->size = 0; |
| 338 | } |
| 339 | |
| 340 | size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset, |
| 341 | void *buf, size_t bytes) |
| 342 | { |
| 343 | return iov_to_buf(qiov->iov, qiov->niov, offset, buf, bytes); |
| 344 | } |
| 345 | |
| 346 | size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset, |
| 347 | const void *buf, size_t bytes) |
| 348 | { |
| 349 | return iov_from_buf(qiov->iov, qiov->niov, offset, buf, bytes); |
| 350 | } |
| 351 | |
| 352 | size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset, |
| 353 | int fillc, size_t bytes) |
| 354 | { |
| 355 | return iov_memset(qiov->iov, qiov->niov, offset, fillc, bytes); |
| 356 | } |