forked from ringbahn/iou
/
sqe.rs
306 lines (261 loc) · 8.97 KB
/
sqe.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
use std::io;
use std::mem;
use std::os::unix::io::RawFd;
use std::ptr::{self, NonNull};
use std::marker::PhantomData;
use std::time::Duration;
use super::IoUring;
use uring_sys::__kernel_timespec;
pub struct SubmissionQueue<'ring> {
ring: NonNull<uring_sys::io_uring>,
_marker: PhantomData<&'ring mut IoUring>,
}
impl<'ring> SubmissionQueue<'ring> {
pub(crate) fn new(ring: &'ring IoUring) -> SubmissionQueue<'ring> {
SubmissionQueue {
ring: NonNull::from(&ring.ring),
_marker: PhantomData,
}
}
pub fn next_sqe<'a>(&'a mut self) -> Option<SubmissionQueueEvent<'a>> {
unsafe {
let sqe = uring_sys::io_uring_get_sqe(self.ring.as_ptr());
if sqe != ptr::null_mut() {
let mut sqe = SubmissionQueueEvent::new(&mut *sqe);
sqe.clear();
Some(sqe)
} else {
None
}
}
}
pub fn submit(&mut self) -> io::Result<usize> {
resultify!(unsafe { uring_sys::io_uring_submit(self.ring.as_ptr()) })
}
pub fn submit_and_wait(&mut self, wait_for: u32) -> io::Result<usize> {
resultify!(unsafe { uring_sys::io_uring_submit_and_wait(self.ring.as_ptr(), wait_for as _) })
}
pub fn submit_and_wait_with_timeout(&mut self, wait_for: u32, duration: Duration)
-> io::Result<usize>
{
let ts = uring_sys::__kernel_timespec {
tv_sec: duration.as_secs() as _,
tv_nsec: duration.subsec_nanos() as _
};
loop {
if let Some(mut sqe) = self.next_sqe() {
sqe.clear();
unsafe {
sqe.prep_timeout(&ts);
return resultify!(uring_sys::io_uring_submit_and_wait(self.ring.as_ptr(), wait_for as _))
}
}
self.submit()?;
}
}
}
unsafe impl<'ring> Send for SubmissionQueue<'ring> { }
unsafe impl<'ring> Sync for SubmissionQueue<'ring> { }
pub struct SubmissionQueueEvent<'a> {
sqe: &'a mut uring_sys::io_uring_sqe,
}
impl<'a> SubmissionQueueEvent<'a> {
pub(crate) fn new(sqe: &'a mut uring_sys::io_uring_sqe) -> SubmissionQueueEvent<'a> {
SubmissionQueueEvent { sqe }
}
pub fn user_data(&self) -> u64 {
self.sqe.user_data as u64
}
pub fn set_user_data(&mut self, user_data: u64) {
self.sqe.user_data = user_data as _;
}
pub fn flags(&self) -> SubmissionFlags {
unsafe { SubmissionFlags::from_bits_unchecked(self.sqe.flags as _) }
}
pub fn set_flags(&mut self, flags: SubmissionFlags) {
self.sqe.flags = flags.bits() as _;
}
#[inline]
pub unsafe fn prep_read_vectored(
&mut self,
fd: RawFd,
bufs: &mut [io::IoSliceMut<'_>],
offset: usize,
) {
let len = bufs.len();
let addr = bufs.as_mut_ptr();
uring_sys::io_uring_prep_readv(self.sqe, fd, addr as _, len as _, offset as _);
}
#[inline]
pub unsafe fn prep_read_fixed(
&mut self,
fd: RawFd,
buf: &mut [u8],
offset: usize,
buf_index: usize,
) {
let len = buf.len();
let addr = buf.as_mut_ptr();
uring_sys::io_uring_prep_read_fixed(self.sqe,
fd,
addr as _,
len as _,
offset as _,
buf_index as _);
}
#[inline]
pub unsafe fn prep_write_vectored(
&mut self,
fd: RawFd,
bufs: &[io::IoSlice<'_>],
offset: usize,
) {
let len = bufs.len();
let addr = bufs.as_ptr();
uring_sys::io_uring_prep_writev(self.sqe, fd, addr as _, len as _, offset as _);
}
#[inline]
pub unsafe fn prep_write_fixed(
&mut self,
fd: RawFd,
buf: &[u8],
offset: usize,
buf_index: usize,
) {
let len = buf.len();
let addr = buf.as_ptr();
uring_sys::io_uring_prep_write_fixed(self.sqe,
fd, addr as _,
len as _,
offset as _,
buf_index as _);
}
#[inline]
pub unsafe fn prep_fsync(&mut self, fd: RawFd, flags: FsyncFlags) {
uring_sys::io_uring_prep_fsync(self.sqe, fd, flags.bits() as _);
}
#[inline]
pub unsafe fn prep_timeout(&mut self, ts: &uring_sys::__kernel_timespec) {
self.prep_timeout_with_flags(ts, 0, TimeoutFlags::empty());
}
#[inline]
pub unsafe fn prep_timeout_remove(&mut self, user_data: u64) {
uring_sys::io_uring_prep_timeout_remove(self.sqe, user_data as _, 0)
}
#[inline]
pub unsafe fn prep_timeout_with_flags(
&mut self,
ts: &uring_sys::__kernel_timespec,
count: usize,
flags: TimeoutFlags,
) {
uring_sys::io_uring_prep_timeout(self.sqe,
ts as *const _ as *mut _,
count as _,
flags.bits() as _);
}
#[inline]
pub unsafe fn prep_sendmsg(&mut self, fd: RawFd, msg: &libc::msghdr, flags: SendMsgFlags) {
uring_sys::io_uring_prep_sendmsg(self.sqe, fd, msg, flags.bits() as _)
}
#[inline]
pub unsafe fn prep_recvmsg(&mut self, fd: RawFd, msg: &mut libc::msghdr, flags: RecvMsgFlags) {
uring_sys::io_uring_prep_recvmsg(self.sqe, fd, msg, flags.bits() as _)
}
#[inline]
pub unsafe fn prep_poll_add(&mut self, fd: RawFd, poll_mask: PollMask) {
uring_sys::io_uring_prep_poll_add(self.sqe, fd, poll_mask.bits())
}
#[inline]
pub unsafe fn prep_poll_remove(&mut self, user_data: u64) {
uring_sys::io_uring_prep_poll_remove(self.sqe, user_data as *mut _)
}
#[inline]
pub unsafe fn prep_accept(&mut self, fd: RawFd, addr: &mut libc::sockaddr, addr_len: &mut libc::socklen_t, flags: AcceptFlags) {
uring_sys::io_uring_prep_accept(self.sqe, fd, addr, addr_len, flags.bits())
}
#[inline]
pub unsafe fn io_uring_prep_cancel(&mut self, user_data: u64) {
uring_sys::io_uring_prep_cancel(self.sqe, user_data as _, 0)
}
#[inline]
pub unsafe fn prep_link_timeout(&mut self, ts: &mut __kernel_timespec) {
uring_sys::io_uring_prep_link_timeout(self.sqe, ts, 0)
}
#[link_name = "rust_io_uring_prep_connect"]
pub unsafe fn io_uring_prep_connect(&mut self, fd: RawFd, addr: &mut libc::sockaddr, addr_len: libc::socklen_t) {
uring_sys::io_uring_prep_connect(self.sqe, fd, addr, addr_len)
}
#[inline]
pub unsafe fn prep_nop(&mut self) {
uring_sys::io_uring_prep_nop(self.sqe);
}
pub fn clear(&mut self) {
*self.sqe = unsafe { mem::zeroed() };
}
pub fn raw(&self) -> &uring_sys::io_uring_sqe {
&self.sqe
}
pub fn raw_mut(&mut self) -> &mut uring_sys::io_uring_sqe {
&mut self.sqe
}
}
unsafe impl<'a> Send for SubmissionQueueEvent<'a> { }
unsafe impl<'a> Sync for SubmissionQueueEvent<'a> { }
bitflags::bitflags! {
pub struct SubmissionFlags: u8 {
const FIXED_FILE = 1 << 0; /* use fixed fileset */
const IO_DRAIN = 1 << 1; /* issue after inflight IO */
const IO_LINK = 1 << 2; /* next IO depends on this one */
}
}
bitflags::bitflags! {
pub struct FsyncFlags: u32 {
const FSYNC_DATASYNC = 1 << 0;
}
}
bitflags::bitflags! {
pub struct TimeoutFlags: u32 {
const TIMEOUT_ABS = 1 << 0;
}
}
bitflags::bitflags! {
pub struct PollMask: i16 {
const POLLIN = libc::POLLIN;
const POLLPRI = libc::POLLPRI;
const POLLOUT = libc::POLLOUT;
const POLLERR = libc::POLLERR;
const POLLHUP = libc::POLLHUP;
const POLLNVAL = libc::POLLNVAL;
const POLLRDNORM = libc::POLLRDNORM;
const POLLRDBAND = libc::POLLRDBAND;
}
}
bitflags::bitflags! {
pub struct SendMsgFlags: libc::c_int {
const MSG_CONFIRM = libc::MSG_CONFIRM;
const MSG_DONTROUTE = libc::MSG_DONTROUTE;
const MSG_DONTWAIT = libc::MSG_DONTWAIT;
const MSG_EOR = libc::MSG_EOR;
const MSG_MORE = libc::MSG_MORE;
const MSG_NOSIGNAL = libc::MSG_NOSIGNAL;
const MSG_OOB = libc::MSG_OOB;
}
}
bitflags::bitflags! {
pub struct RecvMsgFlags: libc::c_int {
const MSG_CMSG_CLOEXEC = libc::MSG_CMSG_CLOEXEC;
const MSG_DONTWAIT = libc::MSG_DONTWAIT;
const MSG_ERRQUEUE = libc::MSG_ERRQUEUE;
const MSG_OOB = libc::MSG_OOB;
const MSG_PEEK = libc::MSG_PEEK;
const MSG_TRUNC = libc::MSG_TRUNC;
const MSG_WAITALL = libc::MSG_WAITALL;
}
}
bitflags::bitflags! {
pub struct AcceptFlags: libc::c_int {
const SOCK_NONBLOCK = libc::SOCK_NONBLOCK;
const SOCK_CLOEXEC = libc::SOCK_CLOEXEC;
}
}