-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver_iou.c
438 lines (358 loc) · 8.58 KB
/
driver_iou.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#include <err.h>
#include <errno.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/poll.h>
#include <sys/uio.h>
#include <sys/socket.h>
#include <pthread.h>
#include "util.h"
#include "driver.h"
#include "liburing.h"
#define N_REQS 128
#define USE_FIXED_RECV 0
#define USE_FIXED_SEND 0
#define array_size(x) (sizeof(x) / sizeof((x)[0]))
struct drv_queue {
char *label;
pthread_mutex_t mutex;
int head;
int tail;
int size;
struct drv_req *req[N_REQS];
};
static struct drv_req request[N_REQS * 2];
static struct drv_queue req_send_stack;
static struct drv_queue req_recv_stack;
static struct drv_queue send_queue;
static struct drv_queue recv_queue;
pthread_mutex_t io_mutex;
static bool
drv_empty(struct drv_queue *q)
{
return q->head == q->tail;
}
static struct drv_req *
drv_pop(struct drv_queue *q)
{
struct drv_req *req;
pthread_mutex_lock(&q->mutex);
if (!q->head)
errx(1, "pop on empty stack");
req = q->req[--q->head];
if (!req)
errx(1, "WTF, null req for %d\n", q->head);
pthread_mutex_unlock(&q->mutex);
return req;
}
static void
drv_push(struct drv_queue *q, struct drv_req *req)
{
pthread_mutex_lock(&q->mutex);
if (req == NULL)
errx(1, "WTF, pushing null req!");
if (q->head == q->size)
errx(1, "push on full stack");
q->req[q->head++] = req;
pthread_mutex_unlock(&q->mutex);
}
static bool
drv_queue(struct drv_queue *q, struct drv_req *req)
{
bool empty;
int next;
pthread_mutex_lock(&q->mutex);
empty = drv_empty(q);
next = (q->tail + 1) == q->size ? 0 : (q->tail + 1);
if (next == q->head)
errx(1, "queue overflow");
q->req[q->tail] = req;
q->tail = next;
pthread_mutex_unlock(&q->mutex);
return empty;
}
static bool
drv_dequeue(struct drv_queue *q)
{
struct drv_req *req;
bool empty;
if (drv_empty(q))
errx(1, "dequeue on empty queue");
pthread_mutex_lock(&q->mutex);
req = q->req[q->head++];
if (q->head == q->size)
q->head = 0;
empty = drv_empty(q);
pthread_mutex_unlock(&q->mutex);
return empty;
}
static struct drv_req *
drv_peek(struct drv_queue *q)
{
struct drv_req *req = NULL;
if (!drv_empty(q))
req = q->req[q->head];
return req;
}
struct {
struct io_uring ring;
bool stop;
int timeout;
} drv;
void
iou_start(void)
{
unsigned flags = 0;
int fd, i;
#if 0
/* This is incompatible with IORING_OP_POLL */
if (opt.poll)
flags |= IORING_SETUP_IOPOLL;
#endif
#if 0
flags |= IORING_SETUP_SQPOLL; /* submission polling kernel thread */
flags |= IORING_SETUP_SQ_AFF; /* bind poll thread to specific cpu */
#endif
/* simplified api that only sets flags. */
fd = io_uring_queue_init(opt.iou.entries, &drv.ring, flags);
if (fd < 0)
err_with(fd, "iou_queue_init");
req_send_stack.size = N_REQS;
req_recv_stack.size = N_REQS;
send_queue.size = N_REQS;
recv_queue.size = N_REQS;
pthread_mutex_init(&req_send_stack.mutex, NULL);
pthread_mutex_init(&req_recv_stack.mutex, NULL);
pthread_mutex_init(&send_queue.mutex, NULL);
pthread_mutex_init(&recv_queue.mutex, NULL);
pthread_mutex_init(&io_mutex, NULL);
req_send_stack.label = "SEND STACK";
req_recv_stack.label = "RECV STACK";
for (i = 0; i < N_REQS; i++) {
drv_push(&req_send_stack, &request[i]);
drv_push(&req_recv_stack, &request[N_REQS + i]);
}
printf("IOU_START, ring_fd: %d\n", drv.ring.ring_fd);
}
void
iou_stop(void)
{
struct io_uring *ring = &drv.ring;
struct io_uring_sqe *sqe;
int rc;
drv.stop = true;
pthread_mutex_lock(&io_mutex);
sqe = io_uring_get_sqe(ring);
io_uring_prep_nop(sqe);
rc = io_uring_submit(ring);
if (rc != 1)
err(1, "iou_stop: %d", rc);
pthread_mutex_unlock(&io_mutex);
}
void
iou_req_register(struct drv_req *req, int events)
{
}
void
iou_unregister(int fd)
{
}
static struct iovec rbuf_iov[8];
static int rbuf_count;
void
rbuf_rangecheck(int idx, struct iovec *iov)
{
#if 0
uint8_t *start = iov->iov_base;
printf("RC %d range[%p,%p], iov[%p,%p]\n",
idx, rbuf[idx].start, rbuf[idx].end, start, start + iov->iov_len);
if (idx >= rbuf_count)
errx(1, "index %d >= max %d\n", idx, rbuf_count);
if ((start >= rbuf[idx].start) &&
(start + iov->iov_len) <= rbuf[idx].end)
return;
errx(1, "iov out of range, area %d range[%p,%p], iov[%p,%p]\n",
idx, rbuf[idx].start, rbuf[idx].end, start, start + iov->iov_len);
#endif
}
void
iou_register_buffer(struct iovec *iov)
{
struct io_uring *ring = &drv.ring;
int rc;
if (!iov->iov_base) {
rc = io_uring_register_buffers(ring, rbuf_iov, rbuf_count);
if (rc < 0)
err_with(rc, "io_uring_register_buffers");
return;
}
if (rbuf_count == array_size(rbuf_iov))
errx(1, "rbuf_count == maximum %lu", array_size(rbuf_iov));
rbuf_iov[rbuf_count++] = *iov;
}
static void
iou_recv_req(struct drv_req *req)
{
struct io_uring *ring = &drv.ring;
struct io_uring_sqe *sqe;
int rc;
pthread_mutex_lock(&io_mutex);
sqe = io_uring_get_sqe(ring);
io_uring_prep_poll_add(sqe, req->fd, POLLIN);
io_uring_sqe_set_flags(sqe, IOSQE_IO_LINK);
sqe = io_uring_get_sqe(ring);
#if USE_FIXED_RECV
rbuf_rangecheck(req->area, &req->iov);
io_uring_prep_read_fixed(sqe, req->fd,
req->iov.iov_base, req->iov.iov_len, 0, req->area);
#else
io_uring_prep_readv(sqe, req->fd, &req->iov, 1, 0);
#endif
io_uring_sqe_set_data(sqe, req);
//printf("IOU_RECV_REQ %p %zu\n", req->iov.iov_base, req->iov.iov_len);
rc = io_uring_submit(ring);
/* rc may be zero if something else picked up the submission */
if (rc != 2)
err(1, "recv submit: %d", rc);
pthread_mutex_unlock(&io_mutex);
}
static void
drv_recv_cb(struct drv_req *req, int events)
{
bool empty;
//printf("RECV COMPLETE\n");
req->done(req->arg);
empty = drv_dequeue(&recv_queue);
drv_push(&req_recv_stack, req);
if (empty)
return;
req = drv_peek(&recv_queue);
iou_recv_req(req);
}
void
iou_recv(int fd, struct iovec *iov, int area, void (*done)(int), int arg)
{
struct drv_req *req = drv_pop(&req_recv_stack);
bool first;
req->fd = fd;
req->arg = arg;
req->iov = *iov;
req->callback = drv_recv_cb;
req->send = false;
req->done = done;
req->area = area;
first = drv_queue(&recv_queue, req);
if (first)
iou_recv_req(req);
}
static void
iou_send_req(struct drv_req *req)
{
struct io_uring *ring = &drv.ring;
struct io_uring_sqe *sqe;
int rc;
pthread_mutex_lock(&io_mutex);
sqe = io_uring_get_sqe(ring);
#if USE_FIXED_SEND
rbuf_rangecheck(req->area, &req->iov);
io_uring_prep_write_fixed(sqe, req->fd,
req->iov.iov_base, req->iov.iov_len, 0, req->area);
#else
io_uring_prep_writev(sqe, req->fd, &req->iov, 1, 0);
#endif
io_uring_sqe_set_data(sqe, req);
//printf("IOU_SEND_REQ %p %zu\n", req->iov.iov_base, req->iov.iov_len);
rc = io_uring_submit(ring);
/* rc may be zero if something else picked up the submission */
if (rc != 1)
err(1, "send submit: %d", rc);
pthread_mutex_unlock(&io_mutex);
}
static void
drv_send_cb(struct drv_req *req, int events)
{
bool empty;
// req->done(req->arg);
empty = drv_dequeue(&send_queue);
drv_push(&req_send_stack, req);
if (empty)
return;
req = drv_peek(&send_queue);
iou_send_req(req);
}
int send_count;
void
iou_send(int fd, struct iovec *iov, int area)
{
struct drv_req *req = drv_pop(&req_send_stack);
bool first;
req->fd = fd;
req->iov = *iov;
req->callback = drv_send_cb;
req->send = true;
req->area = area;
req->scratch = send_count++;
first = drv_queue(&send_queue, req);
if (first)
iou_send_req(req);
}
static void
iou_handle_cqe(struct io_uring_cqe *cqe)
{
struct drv_req *req;
req = io_uring_cqe_get_data(cqe);
if (!req)
return; /* not req: ignore, continue */
if (cqe->res != req->iov.iov_len) {
if (cqe->res == 0) {
printf("type: %s\n", req->send ? "SEND" : "RECV");
errx(1, "cqe with 0 bytes == unexpected EOF");
}
/* short operation, resubmit */
req->iov.iov_base += cqe->res;
req->iov.iov_len -= cqe->res;
if (req->send)
iou_send_req(req);
else
iou_recv_req(req);
return;
}
req->callback(req, 0);
}
void *
iou_loop(void *arg)
{
struct io_uring *ring = &drv.ring;
struct io_uring_cqe *cqe;
int rc;
while (!drv.stop) {
rc = io_uring_wait_cqe(ring, &cqe);
if (rc < 0)
err(1, "wait_cqe: %d", rc);
if (cqe->res < 0) {
struct drv_req *req = io_uring_cqe_get_data(cqe);
if (req) {
printf("res: %p %zu\n",
req->iov.iov_base, req->iov.iov_len);
printf("type: %s\n", req->send ? "SEND" : "RECV");
}
err_with(cqe->res, "res iou_loop");
}
iou_handle_cqe(cqe);
io_uring_cqe_seen(ring, cqe);
}
return NULL;
}
struct io_driver iou_driver = {
.start = iou_start,
.stop = iou_stop,
.req_register = iou_req_register,
.unregister = iou_unregister,
.register_buffer = iou_register_buffer,
.loop = iou_loop,
.send = iou_send,
.recv = iou_recv,
};