blob: 9917b5b9accaf16d9ebcb9255f9a117f61e1f0a0 [file] [log] [blame]
Jens Axboef93c84e2019-01-08 06:51:07 -07001#ifndef LIB_URING_H
2#define LIB_URING_H
3
4#include <sys/uio.h>
Jens Axboef16b83b2019-01-15 11:14:43 -07005#include "compat.h"
Jens Axboef93c84e2019-01-08 06:51:07 -07006#include "io_uring.h"
7
8/*
9 * Library interface to io_uring
10 */
11struct io_uring_sq {
12 unsigned *khead;
13 unsigned *ktail;
14 unsigned *kring_mask;
15 unsigned *kring_entries;
16 unsigned *kflags;
17 unsigned *kdropped;
18 unsigned *array;
Jens Axboe7bf7e8e2019-01-09 15:26:20 -070019 struct io_uring_sqe *sqes;
Jens Axboef93c84e2019-01-08 06:51:07 -070020
Jens Axboe7bf7e8e2019-01-09 15:26:20 -070021 unsigned sqe_head;
22 unsigned sqe_tail;
Jens Axboef93c84e2019-01-08 06:51:07 -070023
24 size_t ring_sz;
25};
26
27struct io_uring_cq {
28 unsigned *khead;
29 unsigned *ktail;
30 unsigned *kring_mask;
31 unsigned *kring_entries;
32 unsigned *koverflow;
Jens Axboe7bf7e8e2019-01-09 15:26:20 -070033 struct io_uring_cqe *cqes;
Jens Axboef93c84e2019-01-08 06:51:07 -070034
35 size_t ring_sz;
36};
37
Jens Axboe7f7a66e2019-01-08 15:31:35 -070038struct io_uring {
39 struct io_uring_sq sq;
40 struct io_uring_cq cq;
Jens Axboe66a7d052019-01-08 15:59:09 -070041 int ring_fd;
Jens Axboe7f7a66e2019-01-08 15:31:35 -070042};
43
Jens Axboef93c84e2019-01-08 06:51:07 -070044/*
45 * System calls
46 */
Jens Axboed5b4ae12019-01-10 14:28:10 -070047extern int io_uring_setup(unsigned entries, struct io_uring_params *p);
Jens Axboef93c84e2019-01-08 06:51:07 -070048extern int io_uring_enter(unsigned fd, unsigned to_submit,
49 unsigned min_complete, unsigned flags);
Jeff Moyer45003302019-02-05 13:14:49 -050050extern int io_uring_register(int fd, unsigned int opcode, void *arg,
51 unsigned int nr_args);
Jens Axboef93c84e2019-01-08 06:51:07 -070052
53/*
54 * Library interface
55 */
Jens Axboea992ffa2019-01-10 15:11:07 -070056extern int io_uring_queue_init(unsigned entries, struct io_uring *ring,
57 unsigned flags);
Jens Axboe66a7d052019-01-08 15:59:09 -070058extern void io_uring_queue_exit(struct io_uring *ring);
59extern int io_uring_get_completion(struct io_uring *ring,
Jens Axboe7bf7e8e2019-01-09 15:26:20 -070060 struct io_uring_cqe **cqe_ptr);
Jens Axboe66a7d052019-01-08 15:59:09 -070061extern int io_uring_wait_completion(struct io_uring *ring,
Jens Axboe7bf7e8e2019-01-09 15:26:20 -070062 struct io_uring_cqe **cqe_ptr);
Jens Axboe66a7d052019-01-08 15:59:09 -070063extern int io_uring_submit(struct io_uring *ring);
Jens Axboe7bf7e8e2019-01-09 15:26:20 -070064extern struct io_uring_sqe *io_uring_get_sqe(struct io_uring *ring);
Jens Axboef93c84e2019-01-08 06:51:07 -070065
Jens Axboe5789a632019-01-17 18:12:22 -070066/*
67 * Command prep helpers
68 */
69static inline void io_uring_sqe_set_data(struct io_uring_sqe *sqe, void *data)
70{
71 sqe->user_data = (unsigned long) data;
72}
73
74static inline void io_uring_prep_readv(struct io_uring_sqe *sqe, int fd,
75 struct iovec *iovecs, unsigned nr_vecs,
76 off_t offset)
77{
78 memset(sqe, 0, sizeof(*sqe));
79 sqe->opcode = IORING_OP_READV;
80 sqe->fd = fd;
81 sqe->off = offset;
82 sqe->addr = (unsigned long) iovecs;
Jens Axboe25757802019-01-17 21:33:39 -070083 sqe->len = nr_vecs;
Jens Axboe5789a632019-01-17 18:12:22 -070084}
85
86static inline void io_uring_prep_writev(struct io_uring_sqe *sqe, int fd,
87 struct iovec *iovecs, unsigned nr_vecs,
88 off_t offset)
89{
90 memset(sqe, 0, sizeof(*sqe));
91 sqe->opcode = IORING_OP_WRITEV;
92 sqe->fd = fd;
93 sqe->off = offset;
94 sqe->addr = (unsigned long) iovecs;
Jens Axboe25757802019-01-17 21:33:39 -070095 sqe->len = nr_vecs;
Jens Axboe5789a632019-01-17 18:12:22 -070096}
97
Jens Axboe36406992019-01-18 06:10:40 -070098static inline void io_uring_prep_poll_add(struct io_uring_sqe *sqe, int fd,
99 short poll_mask)
Jens Axboe5789a632019-01-17 18:12:22 -0700100{
101 memset(sqe, 0, sizeof(*sqe));
Jens Axboe36406992019-01-18 06:10:40 -0700102 sqe->opcode = IORING_OP_POLL_ADD;
Jens Axboe5789a632019-01-17 18:12:22 -0700103 sqe->fd = fd;
104 sqe->poll_events = poll_mask;
105}
106
Jens Axboe36406992019-01-18 06:10:40 -0700107static inline void io_uring_prep_poll_remove(struct io_uring_sqe *sqe,
Jens Axboe5789a632019-01-17 18:12:22 -0700108 void *user_data)
109{
110 memset(sqe, 0, sizeof(*sqe));
Jens Axboe36406992019-01-18 06:10:40 -0700111 sqe->opcode = IORING_OP_POLL_REMOVE;
Jens Axboe5789a632019-01-17 18:12:22 -0700112 sqe->addr = (unsigned long) user_data;
113}
114
Jens Axboef93c84e2019-01-08 06:51:07 -0700115#endif