Merge remote-tracking branch 'kraxel/bios.2' into staging
[qemu.git] / iov.c
blobb3330610bba4fb1e72f3f87266e6fa8bec6da19a
1 /*
2 * Helpers for getting linearized buffers from iov / filling buffers into iovs
4 * Copyright IBM, Corp. 2007, 2008
5 * Copyright (C) 2010 Red Hat, Inc.
7 * Author(s):
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Amit Shah <amit.shah@redhat.com>
10 * Michael Tokarev <mjt@tls.msk.ru>
12 * This work is licensed under the terms of the GNU GPL, version 2. See
13 * the COPYING file in the top-level directory.
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.
19 #include "iov.h"
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
29 size_t iov_from_buf(struct iovec *iov, unsigned int iov_cnt,
30 size_t offset, const void *buf, size_t bytes)
32 size_t done;
33 unsigned int i;
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;
44 assert(offset == 0);
45 return done;
48 size_t iov_to_buf(const struct iovec *iov, const unsigned int iov_cnt,
49 size_t offset, void *buf, size_t bytes)
51 size_t done;
52 unsigned int i;
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;
63 assert(offset == 0);
64 return done;
67 size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt,
68 size_t offset, int fillc, size_t bytes)
70 size_t done;
71 unsigned int i;
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;
82 assert(offset == 0);
83 return done;
86 size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt)
88 size_t len;
89 unsigned int i;
91 len = 0;
92 for (i = 0; i < iov_cnt; i++) {
93 len += iov[i].iov_len;
95 return len;
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)
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() */
117 unsigned i = 0;
118 ssize_t ret = 0;
119 while (i < iov_cnt) {
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) {
133 ret = -1;
135 break;
137 i++;
139 return ret;
140 #endif
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)
147 ssize_t ret;
148 unsigned si, ei; /* start and end indexes */
150 /* Find the start position, skipping `offset' bytes:
151 * first, skip all full-sized vector elements, */
152 for (si = 0; si < iov_cnt && offset >= iov[si].iov_len; ++si) {
153 offset -= iov[si].iov_len;
155 if (offset) {
156 assert(si < iov_cnt);
157 /* second, skip `offset' bytes from the (now) first element,
158 * undo it on exit */
159 iov[si].iov_base += offset;
160 iov[si].iov_len -= offset;
162 /* Find the end position skipping `bytes' bytes: */
163 /* first, skip all full-sized elements */
164 for (ei = si; ei < iov_cnt && iov[ei].iov_len <= bytes; ++ei) {
165 bytes -= iov[ei].iov_len;
167 if (bytes) {
168 /* second, fixup the last element, and remember
169 * the length we've cut from the end of it in `bytes' */
170 size_t tail;
171 assert(ei < iov_cnt);
172 assert(iov[ei].iov_len > bytes);
173 tail = iov[ei].iov_len - bytes;
174 iov[ei].iov_len = bytes;
175 bytes = tail; /* bytes is now equal to the tail size */
176 ++ei;
179 ret = do_send_recv(sockfd, iov + si, ei - si, do_send);
181 /* Undo the changes above */
182 if (offset) {
183 iov[si].iov_base -= offset;
184 iov[si].iov_len += offset;
186 if (bytes) {
187 iov[ei-1].iov_len += bytes;
190 return ret;
194 void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
195 FILE *fp, const char *prefix, size_t limit)
197 unsigned int i, v, b;
198 uint8_t *c;
200 c = iov[0].iov_base;
201 for (i = 0, v = 0, b = 0; b < limit; i++, b++) {
202 if (i == iov[v].iov_len) {
203 i = 0; v++;
204 if (v == iov_cnt) {
205 break;
207 c = iov[v].iov_base;
209 if ((b % 16) == 0) {
210 fprintf(fp, "%s: %04x:", prefix, b);
212 if ((b % 4) == 0) {
213 fprintf(fp, " ");
215 fprintf(fp, " %02x", c[i]);
216 if ((b % 16) == 15) {
217 fprintf(fp, "\n");
220 if ((b % 16) != 0) {
221 fprintf(fp, "\n");