Statistics
| Branch: | Revision:

root / include / qemu / iov.h @ 5a37532d

History | View | Annotate | Download (4.4 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 cb9c377f Paolo Bonzini
#ifndef IOV_H
15 cb9c377f Paolo Bonzini
#define IOV_H
16 cb9c377f Paolo Bonzini
17 e4d5639d Amit Shah
#include "qemu-common.h"
18 e4d5639d Amit Shah
19 dcf6f5e1 Michael Tokarev
/**
20 dcf6f5e1 Michael Tokarev
 * count and return data size, in bytes, of an iovec
21 dcf6f5e1 Michael Tokarev
 * starting at `iov' of `iov_cnt' number of elements.
22 dcf6f5e1 Michael Tokarev
 */
23 dcf6f5e1 Michael Tokarev
size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt);
24 dcf6f5e1 Michael Tokarev
25 dcf6f5e1 Michael Tokarev
/**
26 dcf6f5e1 Michael Tokarev
 * Copy from single continuous buffer to scatter-gather vector of buffers
27 dcf6f5e1 Michael Tokarev
 * (iovec) and back like memcpy() between two continuous memory regions.
28 dcf6f5e1 Michael Tokarev
 * Data in single continuous buffer starting at address `buf' and
29 dcf6f5e1 Michael Tokarev
 * `bytes' bytes long will be copied to/from an iovec `iov' with
30 dcf6f5e1 Michael Tokarev
 * `iov_cnt' number of elements, starting at byte position `offset'
31 dcf6f5e1 Michael Tokarev
 * within the iovec.  If the iovec does not contain enough space,
32 dcf6f5e1 Michael Tokarev
 * only part of data will be copied, up to the end of the iovec.
33 dcf6f5e1 Michael Tokarev
 * Number of bytes actually copied will be returned, which is
34 dcf6f5e1 Michael Tokarev
 *  min(bytes, iov_size(iov)-offset)
35 2278a69e Michael Tokarev
 * `Offset' must point to the inside of iovec.
36 2278a69e Michael Tokarev
 * It is okay to use very large value for `bytes' since we're
37 2278a69e Michael Tokarev
 * limited by the size of the iovec anyway, provided that the
38 2278a69e Michael Tokarev
 * buffer pointed to by buf has enough space.  One possible
39 2278a69e Michael Tokarev
 * such "large" value is -1 (sinice size_t is unsigned),
40 2278a69e Michael Tokarev
 * so specifying `-1' as `bytes' means 'up to the end of iovec'.
41 dcf6f5e1 Michael Tokarev
 */
42 844b5cea Michael S. Tsirkin
size_t iov_from_buf(const struct iovec *iov, unsigned int iov_cnt,
43 dcf6f5e1 Michael Tokarev
                    size_t offset, const void *buf, size_t bytes);
44 348e7b8d Hannes Reinecke
size_t iov_to_buf(const struct iovec *iov, const unsigned int iov_cnt,
45 dcf6f5e1 Michael Tokarev
                  size_t offset, void *buf, size_t bytes);
46 dcf6f5e1 Michael Tokarev
47 dcf6f5e1 Michael Tokarev
/**
48 dcf6f5e1 Michael Tokarev
 * Set data bytes pointed out by iovec `iov' of size `iov_cnt' elements,
49 dcf6f5e1 Michael Tokarev
 * starting at byte offset `start', to value `fillc', repeating it
50 2278a69e Michael Tokarev
 * `bytes' number of times.  `Offset' must point to the inside of iovec.
51 dcf6f5e1 Michael Tokarev
 * If `bytes' is large enough, only last bytes portion of iovec,
52 dcf6f5e1 Michael Tokarev
 * up to the end of it, will be filled with the specified value.
53 dcf6f5e1 Michael Tokarev
 * Function return actual number of bytes processed, which is
54 dcf6f5e1 Michael Tokarev
 * min(size, iov_size(iov) - offset).
55 2278a69e Michael Tokarev
 * Again, it is okay to use large value for `bytes' to mean "up to the end".
56 dcf6f5e1 Michael Tokarev
 */
57 dcf6f5e1 Michael Tokarev
size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt,
58 dcf6f5e1 Michael Tokarev
                  size_t offset, int fillc, size_t bytes);
59 dcf6f5e1 Michael Tokarev
60 3e80bf93 Michael Tokarev
/*
61 3e80bf93 Michael Tokarev
 * Send/recv data from/to iovec buffers directly
62 3e80bf93 Michael Tokarev
 *
63 3e80bf93 Michael Tokarev
 * `offset' bytes in the beginning of iovec buffer are skipped and
64 3e80bf93 Michael Tokarev
 * next `bytes' bytes are used, which must be within data of iovec.
65 3e80bf93 Michael Tokarev
 *
66 25e5e4c7 Michael Tokarev
 *   r = iov_send_recv(sockfd, iov, iovcnt, offset, bytes, true);
67 3e80bf93 Michael Tokarev
 *
68 3e80bf93 Michael Tokarev
 * is logically equivalent to
69 3e80bf93 Michael Tokarev
 *
70 3e80bf93 Michael Tokarev
 *   char *buf = malloc(bytes);
71 3e80bf93 Michael Tokarev
 *   iov_to_buf(iov, iovcnt, offset, buf, bytes);
72 3e80bf93 Michael Tokarev
 *   r = send(sockfd, buf, bytes, 0);
73 3e80bf93 Michael Tokarev
 *   free(buf);
74 25e5e4c7 Michael Tokarev
 *
75 25e5e4c7 Michael Tokarev
 * For iov_send_recv() _whole_ area being sent or received
76 25e5e4c7 Michael Tokarev
 * should be within the iovec, not only beginning of it.
77 3e80bf93 Michael Tokarev
 */
78 25e5e4c7 Michael Tokarev
ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt,
79 e3e87df4 Michael Tokarev
                      size_t offset, size_t bytes, bool do_send);
80 25e5e4c7 Michael Tokarev
#define iov_recv(sockfd, iov, iov_cnt, offset, bytes) \
81 25e5e4c7 Michael Tokarev
  iov_send_recv(sockfd, iov, iov_cnt, offset, bytes, false)
82 25e5e4c7 Michael Tokarev
#define iov_send(sockfd, iov, iov_cnt, offset, bytes) \
83 25e5e4c7 Michael Tokarev
  iov_send_recv(sockfd, iov, iov_cnt, offset, bytes, true)
84 3e80bf93 Michael Tokarev
85 dcf6f5e1 Michael Tokarev
/**
86 dcf6f5e1 Michael Tokarev
 * Produce a text hexdump of iovec `iov' with `iov_cnt' number of elements
87 dcf6f5e1 Michael Tokarev
 * in file `fp', prefixing each line with `prefix' and processing not more
88 dcf6f5e1 Michael Tokarev
 * than `limit' data bytes.
89 dcf6f5e1 Michael Tokarev
 */
90 3a1dca94 Gerd Hoffmann
void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
91 3a1dca94 Gerd Hoffmann
                 FILE *fp, const char *prefix, size_t limit);
92 d336336c Michael S. Tsirkin
93 d336336c Michael S. Tsirkin
/*
94 d336336c Michael S. Tsirkin
 * Partial copy of vector from iov to dst_iov (data is not copied).
95 d336336c Michael S. Tsirkin
 * dst_iov overlaps iov at a specified offset.
96 d336336c Michael S. Tsirkin
 * size of dst_iov is at most bytes. dst vector count is returned.
97 d336336c Michael S. Tsirkin
 */
98 d336336c Michael S. Tsirkin
unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt,
99 d336336c Michael S. Tsirkin
                 const struct iovec *iov, unsigned int iov_cnt,
100 d336336c Michael S. Tsirkin
                 size_t offset, size_t bytes);
101 cb9c377f Paolo Bonzini
102 d0277635 Stefan Hajnoczi
/*
103 d0277635 Stefan Hajnoczi
 * Remove a given number of bytes from the front or back of a vector.
104 d0277635 Stefan Hajnoczi
 * This may update iov and/or iov_cnt to exclude iovec elements that are
105 d0277635 Stefan Hajnoczi
 * no longer required.
106 d0277635 Stefan Hajnoczi
 *
107 d0277635 Stefan Hajnoczi
 * The number of bytes actually discarded is returned.  This number may be
108 d0277635 Stefan Hajnoczi
 * smaller than requested if the vector is too small.
109 d0277635 Stefan Hajnoczi
 */
110 d0277635 Stefan Hajnoczi
size_t iov_discard_front(struct iovec **iov, unsigned int *iov_cnt,
111 d0277635 Stefan Hajnoczi
                         size_t bytes);
112 d0277635 Stefan Hajnoczi
size_t iov_discard_back(struct iovec *iov, unsigned int *iov_cnt,
113 d0277635 Stefan Hajnoczi
                        size_t bytes);
114 d0277635 Stefan Hajnoczi
115 cb9c377f Paolo Bonzini
#endif