root / drivers / tapdisk-ring.c @ abdb293f
History | View | Annotate | Download (8.8 kB)
1 |
/*
|
---|---|
2 |
* Copyright (c) 2008, XenSource Inc.
|
3 |
* All rights reserved.
|
4 |
*
|
5 |
* Redistribution and use in source and binary forms, with or without
|
6 |
* modification, are permitted provided that the following conditions are met:
|
7 |
* * Redistributions of source code must retain the above copyright
|
8 |
* notice, this list of conditions and the following disclaimer.
|
9 |
* * Redistributions in binary form must reproduce the above copyright
|
10 |
* notice, this list of conditions and the following disclaimer in the
|
11 |
* documentation and/or other materials provided with the distribution.
|
12 |
* * Neither the name of XenSource Inc. nor the names of its contributors
|
13 |
* may be used to endorse or promote products derived from this software
|
14 |
* without specific prior written permission.
|
15 |
*
|
16 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
17 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
18 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
19 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
|
20 |
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
21 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
22 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
23 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
24 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
25 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
26 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
27 |
*/
|
28 |
|
29 |
#ifdef HAVE_CONFIG_H
|
30 |
#include "config.h" |
31 |
#endif
|
32 |
|
33 |
#include <errno.h> |
34 |
|
35 |
#include "tapdisk-ring.h" |
36 |
|
37 |
static int |
38 |
tapdisk_uring_create_ctlfd(td_uring_t *ring) |
39 |
{ |
40 |
int fd, err;
|
41 |
struct sockaddr_un saddr;
|
42 |
|
43 |
if (strnlen(ring->ctlfd_path, sizeof(saddr.sun_family)) >= |
44 |
sizeof(saddr.sun_family))
|
45 |
return -ENAMETOOLONG;
|
46 |
|
47 |
fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
48 |
if (fd == -1) |
49 |
return -errno;
|
50 |
|
51 |
memset(&saddr, 0, sizeof(struct sockaddr_un)); |
52 |
saddr.sun_family = AF_UNIX; |
53 |
memcpy(saddr.sun_path, ring->ctlfd_path, strlen(ring->ctlfd_path)); |
54 |
|
55 |
err = unlink(ring->ctlfd_path); |
56 |
if (err == -1 && errno != ENOENT) { |
57 |
err = -errno; |
58 |
goto fail;
|
59 |
} |
60 |
|
61 |
err = bind(fd, &saddr, sizeof(struct sockaddr_un)); |
62 |
if (err == -1) { |
63 |
err = -errno; |
64 |
goto fail;
|
65 |
} |
66 |
|
67 |
err = listen(fd, 1);
|
68 |
if (err == -1) { |
69 |
err = -errno; |
70 |
goto fail;
|
71 |
} |
72 |
|
73 |
ring->ctlfd = fd; |
74 |
return 0; |
75 |
|
76 |
fail:
|
77 |
close(fd); |
78 |
return err;
|
79 |
} |
80 |
|
81 |
static void |
82 |
tapdisk_uring_destroy_ctlfd(td_uring_t *ring) |
83 |
{ |
84 |
if (ring->ctlfd) {
|
85 |
close(ring->ctlfd); |
86 |
ring->ctlfd = 0;
|
87 |
} |
88 |
|
89 |
if (ring->ctlfd_path) {
|
90 |
unlink(ring->ctlfd_path); |
91 |
free(ring->ctlfd_path); |
92 |
ring->ctlfd_path = NULL;
|
93 |
} |
94 |
} |
95 |
|
96 |
static int |
97 |
tapdisk_uring_connect_ctlfd(td_uring_t *ring) |
98 |
{ |
99 |
int fd, err;
|
100 |
struct sockaddr_un saddr;
|
101 |
|
102 |
if (strnlen(ring->ctlfd_path, sizeof(saddr.sun_path)) >= |
103 |
sizeof(saddr.sun_path))
|
104 |
return -ENAMETOOLONG;
|
105 |
|
106 |
fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
107 |
if (fd == -1) |
108 |
return -errno;
|
109 |
|
110 |
memset(&saddr, 0, sizeof(struct sockaddr_un)); |
111 |
saddr.sun_family = AF_UNIX; |
112 |
memcpy(saddr.sun_path, ring->ctlfd_path, strlen(ring->ctlfd_path)); |
113 |
|
114 |
err = connect(fd, &saddr, sizeof(saddr));
|
115 |
if (err == -1) { |
116 |
err = -errno; |
117 |
goto fail;
|
118 |
} |
119 |
|
120 |
ring->ctlfd = fd; |
121 |
return 0; |
122 |
|
123 |
fail:
|
124 |
close(fd); |
125 |
return err;
|
126 |
} |
127 |
|
128 |
static void |
129 |
tapdisk_uring_disconnect_ctlfd(td_uring_t *ring) |
130 |
{ |
131 |
if (ring->ctlfd)
|
132 |
close(ring->ctlfd); |
133 |
free(ring->ctlfd_path); |
134 |
ring->ctlfd_path = NULL;
|
135 |
} |
136 |
|
137 |
static int |
138 |
tapdisk_uring_create_shmem(td_uring_t *ring) |
139 |
{ |
140 |
int fd, err;
|
141 |
|
142 |
fd = shm_open(ring->shmem_path, O_CREAT | O_RDWR, 0750);
|
143 |
if (fd == -1) |
144 |
return -errno;
|
145 |
|
146 |
err = ftruncate(fd, ring->shmem_size); |
147 |
if (err == -1) { |
148 |
err = -errno; |
149 |
goto out;
|
150 |
} |
151 |
|
152 |
ring->shmem = mmap(NULL, ring->shmem_size,
|
153 |
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
154 |
if (ring->shmem == MAP_FAILED) {
|
155 |
ring->shmem = NULL;
|
156 |
err = -errno; |
157 |
goto out;
|
158 |
} |
159 |
|
160 |
err = 0;
|
161 |
|
162 |
out:
|
163 |
close(fd); |
164 |
return err;
|
165 |
} |
166 |
|
167 |
static void |
168 |
tapdisk_uring_destroy_shmem(td_uring_t *ring) |
169 |
{ |
170 |
if (ring->shmem) {
|
171 |
munmap(ring->shmem, ring->shmem_size); |
172 |
ring->shmem = NULL;
|
173 |
} |
174 |
|
175 |
if (ring->shmem_path) {
|
176 |
shm_unlink(ring->shmem_path); |
177 |
free(ring->shmem_path); |
178 |
ring->shmem_path = NULL;
|
179 |
} |
180 |
} |
181 |
|
182 |
static int |
183 |
tapdisk_uring_connect_shmem(td_uring_t *ring) |
184 |
{ |
185 |
int fd, err;
|
186 |
td_uring_header_t header, *p; |
187 |
|
188 |
fd = shm_open(ring->shmem_path, O_RDWR); |
189 |
if (fd == -1) |
190 |
return -errno;
|
191 |
|
192 |
p = mmap(NULL, sizeof(td_uring_header_t), |
193 |
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
194 |
if (p == MAP_FAILED) {
|
195 |
err = -errno; |
196 |
goto out;
|
197 |
} |
198 |
|
199 |
memcpy(&header, p, sizeof(td_uring_header_t));
|
200 |
munmap(p, sizeof(td_uring_header_t));
|
201 |
|
202 |
if (memcmp(header.cookie,
|
203 |
TAPDISK_URING_COOKIE, sizeof(header.cookie))) {
|
204 |
err = -EINVAL; |
205 |
goto out;
|
206 |
} |
207 |
|
208 |
if (header.version != TD_URING_CURRENT_VERSION) {
|
209 |
err = -EINVAL; |
210 |
goto out;
|
211 |
} |
212 |
|
213 |
ring->ring_size = header.ring_size; |
214 |
ring->data_size = header.data_size; |
215 |
ring->shmem_size = header.shmem_size; |
216 |
|
217 |
ring->shmem = mmap(NULL, ring->shmem_size,
|
218 |
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
219 |
if (ring->shmem == MAP_FAILED) {
|
220 |
rint->shmem = NULL;
|
221 |
err = -errno; |
222 |
goto out;
|
223 |
} |
224 |
|
225 |
err = 0;
|
226 |
|
227 |
out:
|
228 |
close(fd); |
229 |
return err;
|
230 |
} |
231 |
|
232 |
static void |
233 |
tapdisk_uring_disconnect_shmem(td_uring_t *ring) |
234 |
{ |
235 |
if (ring->shmem)
|
236 |
munmap(ring->shmem, ring->shmem_size); |
237 |
free(ring->shmem_path); |
238 |
ring->shmem_path = NULL;
|
239 |
} |
240 |
|
241 |
int
|
242 |
tapdisk_uring_create(td_uring_t *ring, const char *location, |
243 |
uint32_t ring_size, uint32_t data_size) |
244 |
{ |
245 |
int fd, err;
|
246 |
|
247 |
memset(ring, 0, sizeof(td_uring_t)); |
248 |
|
249 |
ring->ring_size = ring_size; |
250 |
ring->data_size = data_size; |
251 |
ring->shmem_size = ring_size + data_size + sizeof(td_uring_header_t);
|
252 |
|
253 |
err = asprintf(&ring->shmem_path, "%s.shm", location);
|
254 |
if (err == -1) { |
255 |
ring->shmem_path = NULL;
|
256 |
err = -errno; |
257 |
goto fail;
|
258 |
} |
259 |
|
260 |
err = asprintf(&ring->ctlfd_path, "%s.cfd", location);
|
261 |
if (err == -1) { |
262 |
ring->ctlfd_path = NULL;
|
263 |
err = -errno; |
264 |
goto fail;
|
265 |
} |
266 |
|
267 |
err = tapdisk_uring_create_ctlfd(ring); |
268 |
if (err)
|
269 |
goto fail;
|
270 |
|
271 |
err = tapdisk_uring_create_shmem(ring); |
272 |
if (err)
|
273 |
goto fail;
|
274 |
|
275 |
ring->ring_area = (unsigned long)ring->shmem + sizeof(td_uring_header_t); |
276 |
ring->data_area = (unsigned long)ring->ring_area + ring->ring_size; |
277 |
|
278 |
return 0; |
279 |
|
280 |
fail:
|
281 |
tapdisk_uring_destroy(ring); |
282 |
return err;
|
283 |
} |
284 |
|
285 |
int
|
286 |
tapdisk_uring_destroy(td_uring_t *ring) |
287 |
{ |
288 |
tapdisk_uring_destroy_shmem(ring); |
289 |
tapdisk_uring_destroy_ctlfd(ring); |
290 |
return 0; |
291 |
} |
292 |
|
293 |
int
|
294 |
tapdisk_uring_connect(td_uring_t *ring, const char *location) |
295 |
{ |
296 |
int fd, err;
|
297 |
|
298 |
memset(ring, 0, sizeof(td_uring_t)); |
299 |
|
300 |
err = asprintf(&ring->shmem_path, "%s.shm", location);
|
301 |
if (err == -1) { |
302 |
ring->shmem_path = NULL;
|
303 |
err = -errno; |
304 |
goto fail;
|
305 |
} |
306 |
|
307 |
err = asprintf(&ring->ctlfd_path, "%s.cfd", location);
|
308 |
if (err == -1) { |
309 |
ring->ctlfd_path = NULL;
|
310 |
err = -errno; |
311 |
goto fail;
|
312 |
} |
313 |
|
314 |
err = tapdisk_uring_connect_ctlfd(ring); |
315 |
if (err)
|
316 |
goto fail;
|
317 |
|
318 |
err = tapdisk_uring_connect_shmem(ring); |
319 |
if (err)
|
320 |
goto fail;
|
321 |
|
322 |
err = 0;
|
323 |
|
324 |
fail:
|
325 |
} |
326 |
|
327 |
int
|
328 |
tapdisk_uring_disconnect(td_uring_t *ring) |
329 |
{ |
330 |
tapdisk_uring_disconnect_shmem(ring); |
331 |
tapdisk_uring_disconnect_ctlfd(ring); |
332 |
return 0; |
333 |
} |
334 |
|
335 |
static int |
336 |
tapdisk_ring_read_message(int fd, td_uring_message_t *message, int timeout) |
337 |
{ |
338 |
fd_set readfds; |
339 |
int ret, len, offset;
|
340 |
struct timeval tv, *t;
|
341 |
|
342 |
t = NULL;
|
343 |
offset = 0;
|
344 |
len = sizeof(td_uring_message_t);
|
345 |
|
346 |
if (timeout) {
|
347 |
tv.tv_sec = timeout; |
348 |
tv.tv_usec = 0;
|
349 |
t = &tv; |
350 |
} |
351 |
|
352 |
while (offset < len) {
|
353 |
FD_ZERO(&readfds); |
354 |
FD_SET(fd, &readfds); |
355 |
|
356 |
/* we don't bother reinitializing tv. at worst, it will wait a
|
357 |
* bit more time than expected. */
|
358 |
|
359 |
ret = select(fd + 1, &readfds, NULL, NULL, t); |
360 |
if (ret == -1) |
361 |
break;
|
362 |
else if (FD_ISSET(fd, &readfds)) { |
363 |
ret = read(fd, message + offset, len - offset); |
364 |
if (ret <= 0) |
365 |
break;
|
366 |
offset += ret; |
367 |
} else
|
368 |
break;
|
369 |
} |
370 |
|
371 |
if (offset != len)
|
372 |
return -EIO;
|
373 |
|
374 |
return 0; |
375 |
} |
376 |
|
377 |
static int |
378 |
tapdisk_ring_write_message(int fd, td_uring_message_t *message, int timeout) |
379 |
{ |
380 |
fd_set writefds; |
381 |
int ret, len, offset;
|
382 |
struct timeval tv, *t;
|
383 |
|
384 |
t = NULL;
|
385 |
offset = 0;
|
386 |
len = sizeof(td_uring_message_t);
|
387 |
|
388 |
if (timeout) {
|
389 |
tv.tv_sec = timeout; |
390 |
tv.tv_usec = 0;
|
391 |
t = &tv; |
392 |
} |
393 |
|
394 |
while (offset < len) {
|
395 |
FD_ZERO(&writefds); |
396 |
FD_SET(fd, &writefds); |
397 |
|
398 |
/* we don't bother reinitializing tv. at worst, it will wait a
|
399 |
* bit more time than expected. */
|
400 |
|
401 |
ret = select(fd + 1, NULL, &writefds, NULL, t); |
402 |
if (ret == -1) |
403 |
break;
|
404 |
else if (FD_ISSET(fd, &writefds)) { |
405 |
ret = write(fd, message + offset, len - offset); |
406 |
if (ret <= 0) |
407 |
break;
|
408 |
offset += ret; |
409 |
} else
|
410 |
break;
|
411 |
} |
412 |
|
413 |
if (offset != len)
|
414 |
return -EIO;
|
415 |
|
416 |
return 0; |
417 |
} |
418 |
|
419 |
int
|
420 |
tapdisk_uring_poll(td_uring_t *ring) |
421 |
{ |
422 |
int err;
|
423 |
td_uring_message_t message; |
424 |
|
425 |
err = tapdisk_uring_read_message(ring->ctlfd, &message, 1);
|
426 |
if (err)
|
427 |
return err;
|
428 |
|
429 |
if (message.type != TAPDISK_URING_MESSAGE_KICK)
|
430 |
return -EINVAL;
|
431 |
|
432 |
return 0; |
433 |
} |
434 |
|
435 |
int
|
436 |
tapdisk_uring_kick(td_uring_t *ring) |
437 |
{ |
438 |
td_uring_message_t message; |
439 |
|
440 |
memset(&message, 0, sizeof(td_uring_message_t)); |
441 |
message.type = TAPDISK_URING_MESSAGE_KICK; |
442 |
|
443 |
return tapdisk_uring_write_message(ring->ctlfd, &message, 1); |
444 |
} |