Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 1 | #ifndef LIB_URING_H |
| 2 | #define LIB_URING_H |
| 3 | |
| 4 | #include <sys/uio.h> |
Jens Axboe | f16b83b | 2019-01-15 11:14:43 -0700 | [diff] [blame] | 5 | #include "compat.h" |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 6 | #include "io_uring.h" |
| 7 | |
| 8 | /* |
| 9 | * Library interface to io_uring |
| 10 | */ |
| 11 | struct 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 Axboe | 7bf7e8e | 2019-01-09 15:26:20 -0700 | [diff] [blame] | 19 | struct io_uring_sqe *sqes; |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 20 | |
Jens Axboe | 7bf7e8e | 2019-01-09 15:26:20 -0700 | [diff] [blame] | 21 | unsigned sqe_head; |
| 22 | unsigned sqe_tail; |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 23 | |
| 24 | size_t ring_sz; |
| 25 | }; |
| 26 | |
| 27 | struct io_uring_cq { |
| 28 | unsigned *khead; |
| 29 | unsigned *ktail; |
| 30 | unsigned *kring_mask; |
| 31 | unsigned *kring_entries; |
| 32 | unsigned *koverflow; |
Jens Axboe | 7bf7e8e | 2019-01-09 15:26:20 -0700 | [diff] [blame] | 33 | struct io_uring_cqe *cqes; |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 34 | |
| 35 | size_t ring_sz; |
| 36 | }; |
| 37 | |
Jens Axboe | 7f7a66e | 2019-01-08 15:31:35 -0700 | [diff] [blame] | 38 | struct io_uring { |
| 39 | struct io_uring_sq sq; |
| 40 | struct io_uring_cq cq; |
Jens Axboe | 66a7d05 | 2019-01-08 15:59:09 -0700 | [diff] [blame] | 41 | int ring_fd; |
Jens Axboe | 7f7a66e | 2019-01-08 15:31:35 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 44 | /* |
| 45 | * System calls |
| 46 | */ |
Jens Axboe | d5b4ae1 | 2019-01-10 14:28:10 -0700 | [diff] [blame] | 47 | extern int io_uring_setup(unsigned entries, struct io_uring_params *p); |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 48 | extern int io_uring_enter(unsigned fd, unsigned to_submit, |
| 49 | unsigned min_complete, unsigned flags); |
Jeff Moyer | 4500330 | 2019-02-05 13:14:49 -0500 | [diff] [blame^] | 50 | extern int io_uring_register(int fd, unsigned int opcode, void *arg, |
| 51 | unsigned int nr_args); |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 52 | |
| 53 | /* |
| 54 | * Library interface |
| 55 | */ |
Jens Axboe | a992ffa | 2019-01-10 15:11:07 -0700 | [diff] [blame] | 56 | extern int io_uring_queue_init(unsigned entries, struct io_uring *ring, |
| 57 | unsigned flags); |
Jens Axboe | 66a7d05 | 2019-01-08 15:59:09 -0700 | [diff] [blame] | 58 | extern void io_uring_queue_exit(struct io_uring *ring); |
| 59 | extern int io_uring_get_completion(struct io_uring *ring, |
Jens Axboe | 7bf7e8e | 2019-01-09 15:26:20 -0700 | [diff] [blame] | 60 | struct io_uring_cqe **cqe_ptr); |
Jens Axboe | 66a7d05 | 2019-01-08 15:59:09 -0700 | [diff] [blame] | 61 | extern int io_uring_wait_completion(struct io_uring *ring, |
Jens Axboe | 7bf7e8e | 2019-01-09 15:26:20 -0700 | [diff] [blame] | 62 | struct io_uring_cqe **cqe_ptr); |
Jens Axboe | 66a7d05 | 2019-01-08 15:59:09 -0700 | [diff] [blame] | 63 | extern int io_uring_submit(struct io_uring *ring); |
Jens Axboe | 7bf7e8e | 2019-01-09 15:26:20 -0700 | [diff] [blame] | 64 | extern struct io_uring_sqe *io_uring_get_sqe(struct io_uring *ring); |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 65 | |
Jens Axboe | 5789a63 | 2019-01-17 18:12:22 -0700 | [diff] [blame] | 66 | /* |
| 67 | * Command prep helpers |
| 68 | */ |
| 69 | static inline void io_uring_sqe_set_data(struct io_uring_sqe *sqe, void *data) |
| 70 | { |
| 71 | sqe->user_data = (unsigned long) data; |
| 72 | } |
| 73 | |
| 74 | static 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 Axboe | 2575780 | 2019-01-17 21:33:39 -0700 | [diff] [blame] | 83 | sqe->len = nr_vecs; |
Jens Axboe | 5789a63 | 2019-01-17 18:12:22 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static 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 Axboe | 2575780 | 2019-01-17 21:33:39 -0700 | [diff] [blame] | 95 | sqe->len = nr_vecs; |
Jens Axboe | 5789a63 | 2019-01-17 18:12:22 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Jens Axboe | 3640699 | 2019-01-18 06:10:40 -0700 | [diff] [blame] | 98 | static inline void io_uring_prep_poll_add(struct io_uring_sqe *sqe, int fd, |
| 99 | short poll_mask) |
Jens Axboe | 5789a63 | 2019-01-17 18:12:22 -0700 | [diff] [blame] | 100 | { |
| 101 | memset(sqe, 0, sizeof(*sqe)); |
Jens Axboe | 3640699 | 2019-01-18 06:10:40 -0700 | [diff] [blame] | 102 | sqe->opcode = IORING_OP_POLL_ADD; |
Jens Axboe | 5789a63 | 2019-01-17 18:12:22 -0700 | [diff] [blame] | 103 | sqe->fd = fd; |
| 104 | sqe->poll_events = poll_mask; |
| 105 | } |
| 106 | |
Jens Axboe | 3640699 | 2019-01-18 06:10:40 -0700 | [diff] [blame] | 107 | static inline void io_uring_prep_poll_remove(struct io_uring_sqe *sqe, |
Jens Axboe | 5789a63 | 2019-01-17 18:12:22 -0700 | [diff] [blame] | 108 | void *user_data) |
| 109 | { |
| 110 | memset(sqe, 0, sizeof(*sqe)); |
Jens Axboe | 3640699 | 2019-01-18 06:10:40 -0700 | [diff] [blame] | 111 | sqe->opcode = IORING_OP_POLL_REMOVE; |
Jens Axboe | 5789a63 | 2019-01-17 18:12:22 -0700 | [diff] [blame] | 112 | sqe->addr = (unsigned long) user_data; |
| 113 | } |
| 114 | |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 115 | #endif |