From 5789a6351a2ae69e810fca79b9e9b68797169e17 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Thu, 17 Jan 2019 18:12:22 -0700 Subject: Add sqe prep helpers Signed-off-by: Jens Axboe --- src/liburing.h | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ test/io_uring-cp.c | 21 +++------------------ test/io_uring-test.c | 9 +-------- test/poll-cancel.c | 22 ++++++++++------------ test/poll.c | 15 ++++----------- 5 files changed, 67 insertions(+), 49 deletions(-) diff --git a/src/liburing.h b/src/liburing.h index 038dd15..4a8c55f 100644 --- a/src/liburing.h +++ b/src/liburing.h @@ -62,4 +62,53 @@ extern int io_uring_wait_completion(struct io_uring *ring, extern int io_uring_submit(struct io_uring *ring); extern struct io_uring_sqe *io_uring_get_sqe(struct io_uring *ring); +/* + * Command prep helpers + */ +static inline void io_uring_sqe_set_data(struct io_uring_sqe *sqe, void *data) +{ + sqe->user_data = (unsigned long) data; +} + +static inline void io_uring_prep_readv(struct io_uring_sqe *sqe, int fd, + struct iovec *iovecs, unsigned nr_vecs, + off_t offset) +{ + memset(sqe, 0, sizeof(*sqe)); + sqe->opcode = IORING_OP_READV; + sqe->fd = fd; + sqe->off = offset; + sqe->addr = (unsigned long) iovecs; + sqe->len = 1; +} + +static inline void io_uring_prep_writev(struct io_uring_sqe *sqe, int fd, + struct iovec *iovecs, unsigned nr_vecs, + off_t offset) +{ + memset(sqe, 0, sizeof(*sqe)); + sqe->opcode = IORING_OP_WRITEV; + sqe->fd = fd; + sqe->off = offset; + sqe->addr = (unsigned long) iovecs; + sqe->len = 1; +} + +static inline void io_uring_prep_poll(struct io_uring_sqe *sqe, int fd, + short poll_mask) +{ + memset(sqe, 0, sizeof(*sqe)); + sqe->opcode = IORING_OP_POLL; + sqe->fd = fd; + sqe->poll_events = poll_mask; +} + +static inline void io_uring_prep_poll_cancel(struct io_uring_sqe *sqe, + void *user_data) +{ + memset(sqe, 0, sizeof(*sqe)); + sqe->opcode = IORING_OP_POLL_CANCEL; + sqe->addr = (unsigned long) user_data; +} + #endif diff --git a/test/io_uring-cp.c b/test/io_uring-cp.c index 5f928c0..d1f9769 100644 --- a/test/io_uring-cp.c +++ b/test/io_uring-cp.c @@ -68,16 +68,9 @@ static int queue_read(int fd, off_t size, off_t offset) data->offset = offset; data->iov = &iovecs[sqe_index(sqe)]; - sqe->opcode = IORING_OP_READV; - sqe->flags = 0; - sqe->ioprio = 0; - sqe->fd = fd; - sqe->off = offset; - sqe->addr = (unsigned long) data->iov; - sqe->buf_index = 0; - sqe->user_data = (unsigned long) data; + io_uring_prep_readv(sqe, fd, data->iov, 1, offset); + io_uring_sqe_set_data(sqe, data); iovecs[sqe_index(sqe)].iov_len = size; - sqe->len = 1; return 0; } @@ -118,16 +111,8 @@ static void queue_write(int fd, struct io_uring_cqe *cqe) struct io_uring_sqe *sqe; sqe = io_uring_get_sqe(&out_ring); - sqe->opcode = IORING_OP_WRITEV; - sqe->flags = 0; - sqe->ioprio = 0; - sqe->fd = fd; - sqe->off = data->offset; - sqe->addr = (unsigned long) data->iov; - sqe->buf_index = 0; - sqe->user_data = 0; + io_uring_prep_writev(sqe, fd, data->iov, 1, data->offset); data->iov->iov_len = cqe->res; - sqe->len = 1; free(data); } diff --git a/test/io_uring-test.c b/test/io_uring-test.c index a8e8cdf..caca379 100644 --- a/test/io_uring-test.c +++ b/test/io_uring-test.c @@ -54,14 +54,7 @@ int main(int argc, char *argv[]) sqe = io_uring_get_sqe(&ring); if (!sqe) break; - sqe->opcode = IORING_OP_READV; - sqe->flags = 0; - sqe->ioprio = 0; - sqe->fd = fd; - sqe->off = offset; - sqe->addr = (unsigned long) &iovecs[i]; - sqe->len = 1; - sqe->buf_index = 0; + io_uring_prep_readv(sqe, fd, &iovecs[i], 1, offset); offset += iovecs[i].iov_len; } while (1); diff --git a/test/poll-cancel.c b/test/poll-cancel.c index 8e0f301..ce82c60 100644 --- a/test/poll-cancel.c +++ b/test/poll-cancel.c @@ -18,7 +18,7 @@ int main(int argc, char *argv[]) int pipe1[2]; struct io_uring_cqe *cqe; struct io_uring_sqe *sqe; - unsigned long addr; + void *addr; int ret; if (pipe(pipe1) != 0) { @@ -37,11 +37,10 @@ int main(int argc, char *argv[]) printf("child: get sqe failed\n"); return 1; } - memset(sqe, 0, sizeof(*sqe)); - sqe->opcode = IORING_OP_POLL; - sqe->fd = pipe1[0]; - sqe->poll_events = POLLIN; - sqe->user_data = addr = (unsigned long) &sqe; + + io_uring_prep_poll(sqe, pipe1[0], POLLIN); + io_uring_sqe_set_data(sqe, sqe); + addr = sqe; ret = io_uring_submit(&ring); if (ret <= 0) { @@ -54,10 +53,9 @@ int main(int argc, char *argv[]) printf("child: get sqe failed\n"); return 1; } - memset(sqe, 0, sizeof(*sqe)); - sqe->opcode = IORING_OP_POLL_CANCEL; - sqe->addr = addr; - sqe->user_data = (unsigned long) &sqe; + + io_uring_prep_poll_cancel(sqe, addr); + io_uring_sqe_set_data(sqe, sqe); ret = io_uring_submit(&ring); if (ret <= 0) { @@ -71,7 +69,7 @@ int main(int argc, char *argv[]) return 1; } - if (cqe->user_data != addr) { + if (cqe->user_data != (unsigned long) addr) { printf("first complete not poll\n"); return 1; } @@ -81,7 +79,7 @@ int main(int argc, char *argv[]) printf("parent: get failed\n"); return 1; } - if (cqe->user_data != (unsigned long) &sqe) { + if (cqe->user_data != (unsigned long) sqe) { printf("second not cancel\n"); return 1; } diff --git a/test/poll.c b/test/poll.c index e2fde51..79788f4 100644 --- a/test/poll.c +++ b/test/poll.c @@ -48,11 +48,7 @@ int main(int argc, char *argv[]) return 1; } - memset(sqe, 0, sizeof(*sqe)); - sqe->opcode = IORING_OP_POLL; - sqe->fd = pipe1[0]; - sqe->poll_events = POLLIN; - sqe->user_data = (unsigned long) &sqe; + io_uring_prep_poll(sqe, pipe1[0], POLLIN); ret = io_uring_submit(&cring); if (ret <= 0) { @@ -95,11 +91,8 @@ int main(int argc, char *argv[]) return 1; } - memset(sqe, 0, sizeof(*sqe)); - sqe->opcode = IORING_OP_POLL; - sqe->fd = pipe2[0]; - sqe->poll_events = POLLIN; - sqe->user_data = (unsigned long) &sqe; + io_uring_prep_poll(sqe, pipe2[0], POLLIN); + io_uring_sqe_set_data(sqe, sqe); ret = io_uring_submit(&pring); if (ret <= 0) { @@ -114,7 +107,7 @@ int main(int argc, char *argv[]) printf("parent: cqe get failed\n"); return 1; } - if (cqe->user_data != (unsigned long) &sqe) { + if (cqe->user_data != (unsigned long) sqe) { printf("parent: cqe wrong fd\n"); return 1; } -- cgit