]>
Commit | Line | Data |
---|---|---|
e4d5639d AS |
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 <[email protected]> | |
9 | * Amit Shah <[email protected]> | |
2278a69e | 10 | * Michael Tokarev <[email protected]> |
e4d5639d AS |
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. | |
6b620ca3 PB |
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. | |
e4d5639d AS |
17 | */ |
18 | ||
1de7afc9 | 19 | #include "qemu/iov.h" |
e4d5639d | 20 | |
25e5e4c7 MT |
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 | ||
844b5cea | 29 | size_t iov_from_buf(const struct iovec *iov, unsigned int iov_cnt, |
2278a69e | 30 | size_t offset, const void *buf, size_t bytes) |
e4d5639d | 31 | { |
2278a69e | 32 | size_t done; |
e4d5639d | 33 | unsigned int i; |
2278a69e MT |
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; | |
348e7b8d | 42 | } |
e4d5639d | 43 | } |
2278a69e MT |
44 | assert(offset == 0); |
45 | return done; | |
e4d5639d | 46 | } |
fa6111f2 | 47 | |
2278a69e MT |
48 | size_t iov_to_buf(const struct iovec *iov, const unsigned int iov_cnt, |
49 | size_t offset, void *buf, size_t bytes) | |
fa6111f2 | 50 | { |
2278a69e | 51 | size_t done; |
fa6111f2 | 52 | unsigned int i; |
2278a69e MT |
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; | |
fa6111f2 | 61 | } |
8d15028e | 62 | } |
2278a69e MT |
63 | assert(offset == 0); |
64 | return done; | |
8d15028e GH |
65 | } |
66 | ||
dcf6f5e1 | 67 | size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt, |
2278a69e | 68 | size_t offset, int fillc, size_t bytes) |
8d15028e | 69 | { |
2278a69e | 70 | size_t done; |
8d15028e | 71 | unsigned int i; |
2278a69e MT |
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; | |
8d15028e | 80 | } |
fa6111f2 | 81 | } |
2278a69e MT |
82 | assert(offset == 0); |
83 | return done; | |
fa6111f2 AS |
84 | } |
85 | ||
348e7b8d | 86 | size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt) |
fa6111f2 AS |
87 | { |
88 | size_t len; | |
89 | unsigned int i; | |
90 | ||
91 | len = 0; | |
348e7b8d | 92 | for (i = 0; i < iov_cnt; i++) { |
fa6111f2 AS |
93 | len += iov[i].iov_len; |
94 | } | |
95 | return len; | |
96 | } | |
3a1dca94 | 97 | |
25e5e4c7 MT |
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 | { | |
9adea5f7 | 102 | #ifdef CONFIG_POSIX |
25e5e4c7 MT |
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() */ | |
c0958559 SW |
117 | unsigned i = 0; |
118 | ssize_t ret = 0; | |
119 | while (i < iov_cnt) { | |
25e5e4c7 MT |
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) { | |
c0958559 | 133 | ret = -1; |
25e5e4c7 MT |
134 | } |
135 | break; | |
136 | } | |
c0958559 | 137 | i++; |
25e5e4c7 | 138 | } |
c0958559 | 139 | return ret; |
25e5e4c7 MT |
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 | { | |
83f75c26 | 147 | ssize_t total = 0; |
25e5e4c7 | 148 | ssize_t ret; |
5209d675 | 149 | size_t orig_len, tail; |
f48869ad | 150 | unsigned niov; |
5209d675 | 151 | |
83f75c26 PB |
152 | while (bytes > 0) { |
153 | /* Find the start position, skipping `offset' bytes: | |
154 | * first, skip all full-sized vector elements, */ | |
155 | for (niov = 0; niov < iov_cnt && offset >= iov[niov].iov_len; ++niov) { | |
156 | offset -= iov[niov].iov_len; | |
157 | } | |
cb6247a7 | 158 | |
83f75c26 PB |
159 | /* niov == iov_cnt would only be valid if bytes == 0, which |
160 | * we already ruled out in the loop condition. */ | |
f48869ad | 161 | assert(niov < iov_cnt); |
83f75c26 PB |
162 | iov += niov; |
163 | iov_cnt -= niov; | |
164 | ||
165 | if (offset) { | |
166 | /* second, skip `offset' bytes from the (now) first element, | |
167 | * undo it on exit */ | |
168 | iov[0].iov_base += offset; | |
169 | iov[0].iov_len -= offset; | |
170 | } | |
171 | /* Find the end position skipping `bytes' bytes: */ | |
172 | /* first, skip all full-sized elements */ | |
173 | tail = bytes; | |
174 | for (niov = 0; niov < iov_cnt && iov[niov].iov_len <= tail; ++niov) { | |
175 | tail -= iov[niov].iov_len; | |
176 | } | |
177 | if (tail) { | |
178 | /* second, fixup the last element, and remember the original | |
179 | * length */ | |
180 | assert(niov < iov_cnt); | |
181 | assert(iov[niov].iov_len > tail); | |
182 | orig_len = iov[niov].iov_len; | |
183 | iov[niov++].iov_len = tail; | |
184 | } | |
25e5e4c7 | 185 | |
83f75c26 | 186 | ret = do_send_recv(sockfd, iov, niov, do_send); |
25e5e4c7 | 187 | |
83f75c26 PB |
188 | /* Undo the changes above before checking for errors */ |
189 | if (tail) { | |
190 | iov[niov-1].iov_len = orig_len; | |
191 | } | |
192 | if (offset) { | |
193 | iov[0].iov_base -= offset; | |
194 | iov[0].iov_len += offset; | |
195 | } | |
196 | ||
197 | if (ret < 0) { | |
198 | assert(errno != EINTR); | |
199 | if (errno == EAGAIN && total > 0) { | |
200 | return total; | |
201 | } | |
202 | return -1; | |
203 | } | |
204 | ||
84004290 MK |
205 | if (ret == 0 && !do_send) { |
206 | /* recv returns 0 when the peer has performed an orderly | |
207 | * shutdown. */ | |
208 | break; | |
209 | } | |
210 | ||
83f75c26 PB |
211 | /* Prepare for the next iteration */ |
212 | offset += ret; | |
213 | total += ret; | |
214 | bytes -= ret; | |
25e5e4c7 | 215 | } |
25e5e4c7 | 216 | |
83f75c26 | 217 | return total; |
25e5e4c7 MT |
218 | } |
219 | ||
220 | ||
3a1dca94 GH |
221 | void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt, |
222 | FILE *fp, const char *prefix, size_t limit) | |
223 | { | |
6ff66f50 PC |
224 | int v; |
225 | size_t size = 0; | |
226 | char *buf; | |
227 | ||
228 | for (v = 0; v < iov_cnt; v++) { | |
229 | size += iov[v].iov_len; | |
3a1dca94 | 230 | } |
6ff66f50 PC |
231 | size = size > limit ? limit : size; |
232 | buf = g_malloc(size); | |
233 | iov_to_buf(iov, iov_cnt, 0, buf, size); | |
3568ac2a | 234 | qemu_hexdump(buf, fp, prefix, size); |
6ff66f50 | 235 | g_free(buf); |
3a1dca94 | 236 | } |
0191253c | 237 | |
d336336c MT |
238 | unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt, |
239 | const struct iovec *iov, unsigned int iov_cnt, | |
240 | size_t offset, size_t bytes) | |
241 | { | |
242 | size_t len; | |
243 | unsigned int i, j; | |
244 | for (i = 0, j = 0; i < iov_cnt && j < dst_iov_cnt && bytes; i++) { | |
245 | if (offset >= iov[i].iov_len) { | |
246 | offset -= iov[i].iov_len; | |
247 | continue; | |
248 | } | |
249 | len = MIN(bytes, iov[i].iov_len - offset); | |
250 | ||
251 | dst_iov[j].iov_base = iov[i].iov_base + offset; | |
252 | dst_iov[j].iov_len = len; | |
253 | j++; | |
254 | bytes -= len; | |
255 | offset = 0; | |
256 | } | |
257 | assert(offset == 0); | |
258 | return j; | |
259 | } | |
f563a5d7 | 260 | |
0191253c PB |
261 | /* io vectors */ |
262 | ||
263 | void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint) | |
264 | { | |
265 | qiov->iov = g_malloc(alloc_hint * sizeof(struct iovec)); | |
266 | qiov->niov = 0; | |
267 | qiov->nalloc = alloc_hint; | |
268 | qiov->size = 0; | |
269 | } | |
270 | ||
271 | void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov) | |
272 | { | |
273 | int i; | |
274 | ||
275 | qiov->iov = iov; | |
276 | qiov->niov = niov; | |
277 | qiov->nalloc = -1; | |
278 | qiov->size = 0; | |
279 | for (i = 0; i < niov; i++) | |
280 | qiov->size += iov[i].iov_len; | |
281 | } | |
282 | ||
283 | void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len) | |
284 | { | |
285 | assert(qiov->nalloc != -1); | |
286 | ||
287 | if (qiov->niov == qiov->nalloc) { | |
288 | qiov->nalloc = 2 * qiov->nalloc + 1; | |
289 | qiov->iov = g_realloc(qiov->iov, qiov->nalloc * sizeof(struct iovec)); | |
290 | } | |
291 | qiov->iov[qiov->niov].iov_base = base; | |
292 | qiov->iov[qiov->niov].iov_len = len; | |
293 | qiov->size += len; | |
294 | ++qiov->niov; | |
295 | } | |
296 | ||
297 | /* | |
530c0bbd | 298 | * Concatenates (partial) iovecs from src_iov to the end of dst. |
0191253c PB |
299 | * It starts copying after skipping `soffset' bytes at the |
300 | * beginning of src and adds individual vectors from src to | |
301 | * dst copies up to `sbytes' bytes total, or up to the end | |
530c0bbd | 302 | * of src_iov if it comes first. This way, it is okay to specify |
0191253c PB |
303 | * very large value for `sbytes' to indicate "up to the end |
304 | * of src". | |
305 | * Only vector pointers are processed, not the actual data buffers. | |
306 | */ | |
530c0bbd SH |
307 | void qemu_iovec_concat_iov(QEMUIOVector *dst, |
308 | struct iovec *src_iov, unsigned int src_cnt, | |
309 | size_t soffset, size_t sbytes) | |
0191253c PB |
310 | { |
311 | int i; | |
312 | size_t done; | |
facf98ad AK |
313 | |
314 | if (!sbytes) { | |
315 | return; | |
316 | } | |
0191253c | 317 | assert(dst->nalloc != -1); |
530c0bbd SH |
318 | for (i = 0, done = 0; done < sbytes && i < src_cnt; i++) { |
319 | if (soffset < src_iov[i].iov_len) { | |
320 | size_t len = MIN(src_iov[i].iov_len - soffset, sbytes - done); | |
321 | qemu_iovec_add(dst, src_iov[i].iov_base + soffset, len); | |
0191253c PB |
322 | done += len; |
323 | soffset = 0; | |
324 | } else { | |
530c0bbd | 325 | soffset -= src_iov[i].iov_len; |
0191253c PB |
326 | } |
327 | } | |
530c0bbd SH |
328 | assert(soffset == 0); /* offset beyond end of src */ |
329 | } | |
330 | ||
331 | /* | |
332 | * Concatenates (partial) iovecs from src to the end of dst. | |
333 | * It starts copying after skipping `soffset' bytes at the | |
334 | * beginning of src and adds individual vectors from src to | |
335 | * dst copies up to `sbytes' bytes total, or up to the end | |
336 | * of src if it comes first. This way, it is okay to specify | |
337 | * very large value for `sbytes' to indicate "up to the end | |
338 | * of src". | |
339 | * Only vector pointers are processed, not the actual data buffers. | |
340 | */ | |
341 | void qemu_iovec_concat(QEMUIOVector *dst, | |
342 | QEMUIOVector *src, size_t soffset, size_t sbytes) | |
343 | { | |
344 | qemu_iovec_concat_iov(dst, src->iov, src->niov, soffset, sbytes); | |
0191253c PB |
345 | } |
346 | ||
347 | void qemu_iovec_destroy(QEMUIOVector *qiov) | |
348 | { | |
349 | assert(qiov->nalloc != -1); | |
350 | ||
351 | qemu_iovec_reset(qiov); | |
352 | g_free(qiov->iov); | |
353 | qiov->nalloc = 0; | |
354 | qiov->iov = NULL; | |
355 | } | |
356 | ||
357 | void qemu_iovec_reset(QEMUIOVector *qiov) | |
358 | { | |
359 | assert(qiov->nalloc != -1); | |
360 | ||
361 | qiov->niov = 0; | |
362 | qiov->size = 0; | |
363 | } | |
364 | ||
365 | size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset, | |
366 | void *buf, size_t bytes) | |
367 | { | |
368 | return iov_to_buf(qiov->iov, qiov->niov, offset, buf, bytes); | |
369 | } | |
370 | ||
371 | size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset, | |
372 | const void *buf, size_t bytes) | |
373 | { | |
374 | return iov_from_buf(qiov->iov, qiov->niov, offset, buf, bytes); | |
375 | } | |
376 | ||
377 | size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset, | |
378 | int fillc, size_t bytes) | |
379 | { | |
380 | return iov_memset(qiov->iov, qiov->niov, offset, fillc, bytes); | |
381 | } | |
d0277635 SH |
382 | |
383 | size_t iov_discard_front(struct iovec **iov, unsigned int *iov_cnt, | |
384 | size_t bytes) | |
385 | { | |
386 | size_t total = 0; | |
387 | struct iovec *cur; | |
388 | ||
389 | for (cur = *iov; *iov_cnt > 0; cur++) { | |
390 | if (cur->iov_len > bytes) { | |
391 | cur->iov_base += bytes; | |
392 | cur->iov_len -= bytes; | |
393 | total += bytes; | |
394 | break; | |
395 | } | |
396 | ||
397 | bytes -= cur->iov_len; | |
398 | total += cur->iov_len; | |
399 | *iov_cnt -= 1; | |
400 | } | |
401 | ||
402 | *iov = cur; | |
403 | return total; | |
404 | } | |
405 | ||
406 | size_t iov_discard_back(struct iovec *iov, unsigned int *iov_cnt, | |
407 | size_t bytes) | |
408 | { | |
409 | size_t total = 0; | |
410 | struct iovec *cur; | |
411 | ||
412 | if (*iov_cnt == 0) { | |
413 | return 0; | |
414 | } | |
415 | ||
416 | cur = iov + (*iov_cnt - 1); | |
417 | ||
418 | while (*iov_cnt > 0) { | |
419 | if (cur->iov_len > bytes) { | |
420 | cur->iov_len -= bytes; | |
421 | total += bytes; | |
422 | break; | |
423 | } | |
424 | ||
425 | bytes -= cur->iov_len; | |
426 | total += cur->iov_len; | |
427 | cur--; | |
428 | *iov_cnt -= 1; | |
429 | } | |
430 | ||
431 | return total; | |
432 | } |