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