summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJens Axboe <axboe@kernel.dk>2019-01-08 15:59:09 -0700
committerJens Axboe <axboe@kernel.dk>2019-01-08 16:01:59 -0700
commit66a7d05bd82b362942c4f540754b94723c74e804 (patch)
treebb219e5ab9d4e92903551438b1ccce8e088740e9
parentbaa8d243ccffb4d8d45c8b97a9a9c0b7a25ae143 (diff)
liburing: include the ring fd in the io_uring
No point in keeping these separate. Signed-off-by: Jens Axboe <axboe@kernel.dk>
-rw-r--r--src/io_uring.c30
-rw-r--r--src/liburing.h9
-rw-r--r--test/io_uring-cp.c44
-rw-r--r--test/io_uring-test.c14
4 files changed, 47 insertions, 50 deletions
diff --git a/src/io_uring.c b/src/io_uring.c
index a9b6879..b3996b5 100644
--- a/src/io_uring.c
+++ b/src/io_uring.c
@@ -42,19 +42,19 @@ static int __io_uring_get_completion(int fd, struct io_uring_cq *cq,
/*
* Return an IO completion, if one is readily available
*/
-int io_uring_get_completion(int fd, struct io_uring *ring,
+int io_uring_get_completion(struct io_uring *ring,
struct io_uring_event **ev_ptr)
{
- return __io_uring_get_completion(fd, &ring->cq, ev_ptr, 0);
+ return __io_uring_get_completion(ring->ring_fd, &ring->cq, ev_ptr, 0);
}
/*
* Return an IO completion, waiting for it if necessary
*/
-int io_uring_wait_completion(int fd, struct io_uring *ring,
+int io_uring_wait_completion(struct io_uring *ring,
struct io_uring_event **ev_ptr)
{
- return __io_uring_get_completion(fd, &ring->cq, ev_ptr, 1);
+ return __io_uring_get_completion(ring->ring_fd, &ring->cq, ev_ptr, 1);
}
/*
@@ -62,7 +62,7 @@ int io_uring_wait_completion(int fd, struct io_uring *ring,
*
* Returns number of iocbs submitted
*/
-int io_uring_submit(int fd, struct io_uring *ring)
+int io_uring_submit(struct io_uring *ring)
{
struct io_uring_sq *sq = &ring->sq;
const unsigned mask = *sq->kring_mask;
@@ -108,7 +108,8 @@ int io_uring_submit(int fd, struct io_uring *ring)
}
submit:
- return io_uring_enter(fd, submitted, 0, IORING_ENTER_GETEVENTS);
+ return io_uring_enter(ring->ring_fd, submitted, 0,
+ IORING_ENTER_GETEVENTS);
}
/*
@@ -180,27 +181,30 @@ err:
cq->kring_entries = ptr + p->cq_off.ring_entries;
cq->koverflow = ptr + p->cq_off.overflow;
cq->events = ptr + p->cq_off.events;
- return fd;
+ return 0;
}
/*
- * Returns -1 on error, or an 'fd' on success. On success, 'sq' and 'cq'
- * contain the necessary information to read/write to the rings.
+ * Returns -1 on error, or zero on success. On success, 'ring'
+ * contains the necessary information to read/write to the rings.
*/
int io_uring_queue_init(unsigned entries, struct io_uring_params *p,
struct iovec *iovecs, struct io_uring *ring)
{
- int fd;
+ int fd, ret;
fd = io_uring_setup(entries, iovecs, p);
if (fd < 0)
return fd;
memset(ring, 0, sizeof(*ring));
- return io_uring_mmap(fd, p, &ring->sq, &ring->cq);
+ ret = io_uring_mmap(fd, p, &ring->sq, &ring->cq);
+ if (!ret)
+ ring->ring_fd = fd;
+ return ret;
}
-void io_uring_queue_exit(int fd, struct io_uring *ring)
+void io_uring_queue_exit(struct io_uring *ring)
{
struct io_uring_sq *sq = &ring->sq;
struct io_uring_cq *cq = &ring->cq;
@@ -208,5 +212,5 @@ void io_uring_queue_exit(int fd, struct io_uring *ring)
munmap(sq->iocbs, *sq->kring_entries * sizeof(struct io_uring_iocb));
munmap(sq->khead, sq->ring_sz);
munmap(cq->khead, cq->ring_sz);
- close(fd);
+ close(ring->ring_fd);
}
diff --git a/src/liburing.h b/src/liburing.h
index c5bb32e..ed23747 100644
--- a/src/liburing.h
+++ b/src/liburing.h
@@ -37,6 +37,7 @@ struct io_uring_cq {
struct io_uring {
struct io_uring_sq sq;
struct io_uring_cq cq;
+ int ring_fd;
};
/*
@@ -52,12 +53,12 @@ extern int io_uring_enter(unsigned fd, unsigned to_submit,
*/
extern int io_uring_queue_init(unsigned entries, struct io_uring_params *p,
struct iovec *iovecs, struct io_uring *ring);
-extern void io_uring_queue_exit(int fd, struct io_uring *ring);
-extern int io_uring_get_completion(int fd, struct io_uring *ring,
+extern void io_uring_queue_exit(struct io_uring *ring);
+extern int io_uring_get_completion(struct io_uring *ring,
struct io_uring_event **ev_ptr);
-extern int io_uring_wait_completion(int fd, struct io_uring *ring,
+extern int io_uring_wait_completion(struct io_uring *ring,
struct io_uring_event **ev_ptr);
-extern int io_uring_submit(int fd, struct io_uring *ring);
+extern int io_uring_submit(struct io_uring *ring);
extern struct io_uring_iocb *io_uring_get_iocb(struct io_uring *ring);
static inline struct io_uring_iocb *
diff --git a/test/io_uring-cp.c b/test/io_uring-cp.c
index 8fba13f..67ed589 100644
--- a/test/io_uring-cp.c
+++ b/test/io_uring-cp.c
@@ -14,31 +14,25 @@
#define QD 64
#define BS 4096
-struct ring {
- struct io_uring uring;
- int fd;
-};
-
-static struct ring in_ring;
-static struct ring out_ring;
+static struct io_uring in_ring;
+static struct io_uring out_ring;
static void *bufs[QD];
-static int setup_context(unsigned entries, struct ring *r, int offload)
+static int setup_context(unsigned entries, struct io_uring *ring, int offload)
{
struct io_uring_params p;
- int ring_fd;
+ int ret;
memset(&p, 0, sizeof(p));
if (offload)
p.flags = IORING_SETUP_SQWQ;
- ring_fd = io_uring_queue_init(entries, &p, NULL, &r->uring);
- if (ring_fd < 0) {
- fprintf(stderr, "queue_init: %s\n", strerror(-ring_fd));
+ ret = io_uring_queue_init(entries, &p, NULL, ring);
+ if (ret < 0) {
+ fprintf(stderr, "queue_init: %s\n", strerror(-ret));
return -1;
}
- r->fd = ring_fd;
return 0;
}
@@ -58,14 +52,14 @@ static int get_file_size(int fd, off_t *size)
static unsigned iocb_index(struct io_uring_iocb *iocb)
{
- return iocb - in_ring.uring.sq.iocbs;
+ return iocb - in_ring.sq.iocbs;
}
static int queue_read(int fd, off_t size, off_t offset)
{
struct io_uring_iocb *iocb;
- iocb = io_uring_get_iocb(&in_ring.uring);
+ iocb = io_uring_get_iocb(&in_ring);
if (!iocb)
return 1;
@@ -83,7 +77,7 @@ static int complete_writes(unsigned *writes)
{
int ret, nr;
- ret = io_uring_submit(out_ring.fd, &out_ring.uring);
+ ret = io_uring_submit(&out_ring);
if (ret < 0) {
fprintf(stderr, "io_uring_submit: %s\n", strerror(-ret));
return 1;
@@ -93,7 +87,7 @@ static int complete_writes(unsigned *writes)
while (nr) {
struct io_uring_event *ev = NULL;
- ret = io_uring_wait_completion(out_ring.fd, &out_ring.uring, &ev);
+ ret = io_uring_wait_completion(&out_ring, &ev);
if (ret < 0) {
fprintf(stderr, "io_uring_wait_completion: %s\n",
strerror(-ret));
@@ -114,7 +108,7 @@ static void queue_write(int fd, off_t size, off_t offset, unsigned index)
{
struct io_uring_iocb *iocb;
- iocb = io_uring_get_iocb(&out_ring.uring);
+ iocb = io_uring_get_iocb(&out_ring);
iocb->opcode = IORING_OP_WRITE;
iocb->flags = 0;
iocb->ioprio = 0;
@@ -180,7 +174,7 @@ int main(int argc, char *argv[])
}
skip_read:
- ret = io_uring_submit(in_ring.fd, &in_ring.uring);
+ ret = io_uring_submit(&in_ring);
if (ret < 0) {
fprintf(stderr, "io_uring_submit: %s\n", strerror(-ret));
break;
@@ -192,11 +186,9 @@ skip_read:
*/
while (reads || write_left) {
if (reads)
- ret = io_uring_wait_completion(in_ring.fd,
- &in_ring.uring, &ev);
+ ret = io_uring_wait_completion(&in_ring, &ev);
else
- ret = io_uring_get_completion(in_ring.fd,
- &in_ring.uring, &ev);
+ ret = io_uring_get_completion(&in_ring, &ev);
if (ret < 0) {
fprintf(stderr, "io_uring_get_completion: %s\n",
strerror(-ret));
@@ -210,7 +202,7 @@ skip_read:
strerror(-ev->res));
return 1;
}
- iocb = io_uring_iocb_from_ev(&in_ring.uring, ev);
+ iocb = io_uring_iocb_from_ev(&in_ring, ev);
queue_write(outfd, ev->res, iocb->off, ev->index);
write_left -= ev->res;
writes++;
@@ -221,7 +213,7 @@ skip_read:
close(infd);
close(outfd);
- io_uring_queue_exit(in_ring.fd, &in_ring.uring);
- io_uring_queue_exit(out_ring.fd, &out_ring.uring);
+ io_uring_queue_exit(&in_ring);
+ io_uring_queue_exit(&out_ring);
return 0;
}
diff --git a/test/io_uring-test.c b/test/io_uring-test.c
index 1e47a26..80bac8e 100644
--- a/test/io_uring-test.c
+++ b/test/io_uring-test.c
@@ -15,7 +15,7 @@ int main(int argc, char *argv[])
{
struct io_uring_params p;
struct io_uring ring;
- int i, fd, ring_fd, ret, pending, done;
+ int i, fd, ret, pending, done;
struct io_uring_iocb *iocb;
struct io_uring_event *ev;
off_t offset;
@@ -29,9 +29,9 @@ int main(int argc, char *argv[])
memset(&p, 0, sizeof(p));
p.flags = IORING_SETUP_IOPOLL;
- ring_fd = io_uring_queue_init(4, &p, NULL, &ring);
- if (ring_fd < 0) {
- fprintf(stderr, "queue_init: %s\n", strerror(-ring_fd));
+ ret = io_uring_queue_init(4, &p, NULL, &ring);
+ if (ret < 0) {
+ fprintf(stderr, "queue_init: %s\n", strerror(-ret));
return 1;
}
@@ -59,7 +59,7 @@ int main(int argc, char *argv[])
offset += 4096;
} while (1);
- ret = io_uring_submit(ring_fd, &ring);
+ ret = io_uring_submit(&ring);
if (ret < 0) {
fprintf(stderr, "io_uring_submit: %s\n", strerror(-ret));
return 1;
@@ -69,7 +69,7 @@ int main(int argc, char *argv[])
pending = ret;
for (i = 0; i < pending; i++) {
ev = NULL;
- ret = io_uring_get_completion(ring_fd, &ring, &ev);
+ ret = io_uring_get_completion(&ring, &ev);
if (ret < 0) {
fprintf(stderr, "io_uring_get_completion: %s\n", strerror(-ret));
return 1;
@@ -84,6 +84,6 @@ int main(int argc, char *argv[])
printf("Submitted=%d, completed=%d\n", pending, done);
close(fd);
- io_uring_queue_exit(ring_fd, &ring);
+ io_uring_queue_exit(&ring);
return 0;
}