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 | |
| 77 | static inline void io_uring_prep_readv(struct io_uring_sqe *sqe, int fd, |
| 78 | struct iovec *iovecs, unsigned nr_vecs, |
| 79 | off_t offset) |
| 80 | { |
| 81 | memset(sqe, 0, sizeof(*sqe)); |
| 82 | sqe->opcode = IORING_OP_READV; |
| 83 | sqe->fd = fd; |
| 84 | sqe->off = offset; |
| 85 | sqe->addr = (unsigned long) iovecs; |
Jens Axboe | 2575780 | 2019-01-17 21:33:39 -0700 | [diff] [blame] | 86 | sqe->len = nr_vecs; |
Jens Axboe | 5789a63 | 2019-01-17 18:12:22 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static inline void io_uring_prep_writev(struct io_uring_sqe *sqe, int fd, |
| 90 | struct iovec *iovecs, unsigned nr_vecs, |
| 91 | off_t offset) |
| 92 | { |
| 93 | memset(sqe, 0, sizeof(*sqe)); |
| 94 | sqe->opcode = IORING_OP_WRITEV; |
| 95 | sqe->fd = fd; |
| 96 | sqe->off = offset; |
| 97 | sqe->addr = (unsigned long) iovecs; |
Jens Axboe | 2575780 | 2019-01-17 21:33:39 -0700 | [diff] [blame] | 98 | sqe->len = nr_vecs; |
Jens Axboe | 5789a63 | 2019-01-17 18:12:22 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Jens Axboe | 3640699 | 2019-01-18 06:10:40 -0700 | [diff] [blame] | 101 | static inline void io_uring_prep_poll_add(struct io_uring_sqe *sqe, int fd, |
| 102 | short poll_mask) |
Jens Axboe | 5789a63 | 2019-01-17 18:12:22 -0700 | [diff] [blame] | 103 | { |
| 104 | memset(sqe, 0, sizeof(*sqe)); |
Jens Axboe | 3640699 | 2019-01-18 06:10:40 -0700 | [diff] [blame] | 105 | sqe->opcode = IORING_OP_POLL_ADD; |
Jens Axboe | 5789a63 | 2019-01-17 18:12:22 -0700 | [diff] [blame] | 106 | sqe->fd = fd; |
| 107 | sqe->poll_events = poll_mask; |
| 108 | } |
| 109 | |
Jens Axboe | 3640699 | 2019-01-18 06:10:40 -0700 | [diff] [blame] | 110 | 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] | 111 | void *user_data) |
| 112 | { |
| 113 | memset(sqe, 0, sizeof(*sqe)); |
Jens Axboe | 3640699 | 2019-01-18 06:10:40 -0700 | [diff] [blame] | 114 | sqe->opcode = IORING_OP_POLL_REMOVE; |
Jens Axboe | 5789a63 | 2019-01-17 18:12:22 -0700 | [diff] [blame] | 115 | sqe->addr = (unsigned long) user_data; |
| 116 | } |
| 117 | |
Jens Axboe | 6726376 | 2019-02-08 16:13:31 -0700 | [diff] [blame^] | 118 | static inline void io_uring_prep_fsync(struct io_uring_sqe *sqe, int fd, |
| 119 | int datasync) |
| 120 | { |
| 121 | memset(sqe, 0, sizeof(*sqe)); |
| 122 | sqe->opcode = IORING_OP_FSYNC; |
| 123 | sqe->fd = fd; |
| 124 | if (datasync) |
| 125 | sqe->fsync_flags = IORING_FSYNC_DATASYNC; |
| 126 | } |
| 127 | |
Jens Axboe | f93c84e | 2019-01-08 06:51:07 -0700 | [diff] [blame] | 128 | #endif |