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> |
Jeff Moyer | 3ceb15c | 2019-02-08 13:32:21 -0500 | [diff] [blame] | 5 | #include <signal.h> |
Jens Axboe | f16b83b | 2019-01-15 11:14:43 -0700 | [diff] [blame] | 6 | #include "compat.h" |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 7 | #include "io_uring.h" |
| 8 | |
| 9 | /* |
| 10 | * Library interface to io_uring |
| 11 | */ |
| 12 | struct io_uring_sq { |
| 13 | unsigned *khead; |
| 14 | unsigned *ktail; |
| 15 | unsigned *kring_mask; |
| 16 | unsigned *kring_entries; |
| 17 | unsigned *kflags; |
| 18 | unsigned *kdropped; |
| 19 | unsigned *array; |
Jens Axboe | 7bf7e8e | 2019-01-09 15:26:20 -0700 | [diff] [blame] | 20 | struct io_uring_sqe *sqes; |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 21 | |
Jens Axboe | 7bf7e8e | 2019-01-09 15:26:20 -0700 | [diff] [blame] | 22 | unsigned sqe_head; |
| 23 | unsigned sqe_tail; |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 24 | |
| 25 | size_t ring_sz; |
| 26 | }; |
| 27 | |
| 28 | struct io_uring_cq { |
| 29 | unsigned *khead; |
| 30 | unsigned *ktail; |
| 31 | unsigned *kring_mask; |
| 32 | unsigned *kring_entries; |
| 33 | unsigned *koverflow; |
Jens Axboe | 7bf7e8e | 2019-01-09 15:26:20 -0700 | [diff] [blame] | 34 | struct io_uring_cqe *cqes; |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 35 | |
| 36 | size_t ring_sz; |
| 37 | }; |
| 38 | |
Jens Axboe | 7f7a66e | 2019-01-08 15:31:35 -0700 | [diff] [blame] | 39 | struct io_uring { |
| 40 | struct io_uring_sq sq; |
| 41 | struct io_uring_cq cq; |
Jens Axboe | 66a7d05 | 2019-01-08 15:59:09 -0700 | [diff] [blame] | 42 | int ring_fd; |
Jens Axboe | 7f7a66e | 2019-01-08 15:31:35 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 45 | /* |
| 46 | * System calls |
| 47 | */ |
Jens Axboe | d5b4ae1 | 2019-01-10 14:28:10 -0700 | [diff] [blame] | 48 | extern int io_uring_setup(unsigned entries, struct io_uring_params *p); |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 49 | extern int io_uring_enter(unsigned fd, unsigned to_submit, |
Jens Axboe | e377c38 | 2019-02-08 11:40:17 -0700 | [diff] [blame] | 50 | unsigned min_complete, unsigned flags, sigset_t *sig); |
Jeff Moyer | 4500330 | 2019-02-05 13:14:49 -0500 | [diff] [blame] | 51 | extern int io_uring_register(int fd, unsigned int opcode, void *arg, |
| 52 | unsigned int nr_args); |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 53 | |
| 54 | /* |
| 55 | * Library interface |
| 56 | */ |
Jens Axboe | a992ffa | 2019-01-10 15:11:07 -0700 | [diff] [blame] | 57 | extern int io_uring_queue_init(unsigned entries, struct io_uring *ring, |
| 58 | unsigned flags); |
Jeff Moyer | 996b5a6 | 2019-02-08 14:54:02 -0500 | [diff] [blame] | 59 | extern int io_uring_queue_mmap(int fd, struct io_uring_params *p, |
| 60 | struct io_uring *ring); |
Jens Axboe | 66a7d05 | 2019-01-08 15:59:09 -0700 | [diff] [blame] | 61 | extern void io_uring_queue_exit(struct io_uring *ring); |
| 62 | extern int io_uring_get_completion(struct io_uring *ring, |
Jens Axboe | 7bf7e8e | 2019-01-09 15:26:20 -0700 | [diff] [blame] | 63 | struct io_uring_cqe **cqe_ptr); |
Jens Axboe | 66a7d05 | 2019-01-08 15:59:09 -0700 | [diff] [blame] | 64 | extern int io_uring_wait_completion(struct io_uring *ring, |
Jens Axboe | 7bf7e8e | 2019-01-09 15:26:20 -0700 | [diff] [blame] | 65 | struct io_uring_cqe **cqe_ptr); |
Jens Axboe | 66a7d05 | 2019-01-08 15:59:09 -0700 | [diff] [blame] | 66 | extern int io_uring_submit(struct io_uring *ring); |
Jens Axboe | 7bf7e8e | 2019-01-09 15:26:20 -0700 | [diff] [blame] | 67 | 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] | 68 | |
Jens Axboe | 5789a63 | 2019-01-17 18:12:22 -0700 | [diff] [blame] | 69 | /* |
| 70 | * Command prep helpers |
| 71 | */ |
| 72 | static inline void io_uring_sqe_set_data(struct io_uring_sqe *sqe, void *data) |
| 73 | { |
| 74 | sqe->user_data = (unsigned long) data; |
| 75 | } |
| 76 | |
Jens Axboe | 432fa1d | 2019-02-28 13:38:41 -0700 | [diff] [blame] | 77 | static inline void io_uring_prep_rw(int op, struct io_uring_sqe *sqe, int fd, |
| 78 | void *addr, unsigned len, off_t offset) |
| 79 | { |
| 80 | memset(sqe, 0, sizeof(*sqe)); |
| 81 | sqe->opcode = op; |
| 82 | sqe->fd = fd; |
| 83 | sqe->off = offset; |
| 84 | sqe->addr = (unsigned long) addr; |
| 85 | sqe->len = len; |
| 86 | } |
| 87 | |
Jens Axboe | 5789a63 | 2019-01-17 18:12:22 -0700 | [diff] [blame] | 88 | static inline void io_uring_prep_readv(struct io_uring_sqe *sqe, int fd, |
| 89 | struct iovec *iovecs, unsigned nr_vecs, |
| 90 | off_t offset) |
| 91 | { |
Jens Axboe | 432fa1d | 2019-02-28 13:38:41 -0700 | [diff] [blame] | 92 | io_uring_prep_rw(IORING_OP_READV, sqe, fd, iovecs, nr_vecs, offset); |
| 93 | } |
| 94 | |
| 95 | static inline void io_uring_prep_read_fixed(struct io_uring_sqe *sqe, int fd, |
| 96 | void *buf, unsigned nbytes, |
| 97 | off_t offset) |
| 98 | { |
| 99 | io_uring_prep_rw(IORING_OP_READ_FIXED, sqe, fd, buf, nbytes, offset); |
Jens Axboe | 5789a63 | 2019-01-17 18:12:22 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static inline void io_uring_prep_writev(struct io_uring_sqe *sqe, int fd, |
| 103 | struct iovec *iovecs, unsigned nr_vecs, |
| 104 | off_t offset) |
| 105 | { |
Jens Axboe | 432fa1d | 2019-02-28 13:38:41 -0700 | [diff] [blame] | 106 | io_uring_prep_rw(IORING_OP_WRITEV, sqe, fd, iovecs, nr_vecs, offset); |
| 107 | } |
| 108 | |
| 109 | static inline void io_uring_prep_write_fixed(struct io_uring_sqe *sqe, int fd, |
| 110 | void *buf, unsigned nbytes, |
| 111 | off_t offset) |
| 112 | { |
| 113 | io_uring_prep_rw(IORING_OP_WRITE_FIXED, sqe, fd, buf, nbytes, offset); |
Jens Axboe | 5789a63 | 2019-01-17 18:12:22 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Jens Axboe | 3640699 | 2019-01-18 06:10:40 -0700 | [diff] [blame] | 116 | static inline void io_uring_prep_poll_add(struct io_uring_sqe *sqe, int fd, |
| 117 | short poll_mask) |
Jens Axboe | 5789a63 | 2019-01-17 18:12:22 -0700 | [diff] [blame] | 118 | { |
| 119 | memset(sqe, 0, sizeof(*sqe)); |
Jens Axboe | 3640699 | 2019-01-18 06:10:40 -0700 | [diff] [blame] | 120 | sqe->opcode = IORING_OP_POLL_ADD; |
Jens Axboe | 5789a63 | 2019-01-17 18:12:22 -0700 | [diff] [blame] | 121 | sqe->fd = fd; |
| 122 | sqe->poll_events = poll_mask; |
| 123 | } |
| 124 | |
Jens Axboe | 3640699 | 2019-01-18 06:10:40 -0700 | [diff] [blame] | 125 | 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] | 126 | void *user_data) |
| 127 | { |
| 128 | memset(sqe, 0, sizeof(*sqe)); |
Jens Axboe | 3640699 | 2019-01-18 06:10:40 -0700 | [diff] [blame] | 129 | sqe->opcode = IORING_OP_POLL_REMOVE; |
Jens Axboe | 5789a63 | 2019-01-17 18:12:22 -0700 | [diff] [blame] | 130 | sqe->addr = (unsigned long) user_data; |
| 131 | } |
| 132 | |
Jens Axboe | 6726376 | 2019-02-08 16:13:31 -0700 | [diff] [blame] | 133 | static inline void io_uring_prep_fsync(struct io_uring_sqe *sqe, int fd, |
| 134 | int datasync) |
| 135 | { |
| 136 | memset(sqe, 0, sizeof(*sqe)); |
| 137 | sqe->opcode = IORING_OP_FSYNC; |
| 138 | sqe->fd = fd; |
| 139 | if (datasync) |
| 140 | sqe->fsync_flags = IORING_FSYNC_DATASYNC; |
| 141 | } |
| 142 | |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 143 | #endif |