-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathiour.rs
More file actions
383 lines (313 loc) · 10.8 KB
/
iour.rs
File metadata and controls
383 lines (313 loc) · 10.8 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
use std::ffi::c_int;
use io_uring::{opcode, types::Fd};
use crate::{IourOpCode as OpCode, OpEntry, sys::op::*};
unsafe impl OpCode for CreateSocket {
type Control = ();
fn create_entry(&mut self, _: &mut Self::Control) -> OpEntry {
opcode::Socket::new(
self.domain.as_raw() as _,
self.socket_type.as_raw() as c_int | libc::SOCK_CLOEXEC,
self.protocol.map(|p| p.as_raw().get()).unwrap_or_default() as _,
)
.build()
.into()
}
fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
self.call()
}
unsafe fn set_result(&mut self, _: &mut Self::Control, res: &io::Result<usize>, _: &Extra) {
if let Ok(fd) = res {
// SAFETY: fd is a valid fd returned from kernel
let fd = unsafe { Socket2::from_raw_fd(*fd as _) };
self.opened_fd = Some(fd);
}
}
}
unsafe impl<S: AsFd> OpCode for Bind<S> {
type Control = ();
fn create_entry(&mut self, _: &mut Self::Control) -> OpEntry {
opcode::Bind::new(
Fd(self.fd.as_fd().as_raw_fd()),
self.addr.as_ptr().cast(),
self.addr.len(),
)
.build()
.into()
}
fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
self.call()
}
}
unsafe impl<S: AsFd> OpCode for Listen<S> {
type Control = ();
fn create_entry(&mut self, _: &mut Self::Control) -> OpEntry {
opcode::Listen::new(Fd(self.fd.as_fd().as_raw_fd()), self.backlog)
.build()
.into()
}
fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
self.call()
}
}
unsafe impl<S: AsFd> OpCode for ShutdownSocket<S> {
type Control = ();
fn create_entry(&mut self, _: &mut Self::Control) -> OpEntry {
opcode::Shutdown::new(Fd(self.fd.as_fd().as_raw_fd()), self.how())
.build()
.into()
}
fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
self.call()
}
}
unsafe impl OpCode for CloseSocket {
type Control = ();
fn create_entry(&mut self, _: &mut Self::Control) -> OpEntry {
opcode::Close::new(Fd(self.fd.as_fd().as_raw_fd()))
.build()
.into()
}
fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
self.call()
}
}
unsafe impl<S: AsFd> OpCode for Accept<S> {
type Control = ();
fn create_entry(&mut self, _: &mut Self::Control) -> OpEntry {
let entry = opcode::Accept::new(
Fd(self.fd.as_fd().as_raw_fd()),
unsafe { self.buffer.view_as::<libc::sockaddr>() },
&raw mut self.addr_len,
)
.flags(libc::SOCK_CLOEXEC)
.build();
let entry = set_poll_first(entry, self.poll_first);
entry.into()
}
unsafe fn set_result(&mut self, _: &mut Self::Control, res: &io::Result<usize>, _: &Extra) {
if let Ok(fd) = res {
// SAFETY: fd is a valid fd returned from kernel
let fd = unsafe { Socket2::from_raw_fd(*fd as _) };
self.accepted_fd = Some(fd);
}
}
fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
self.call()
}
}
unsafe impl<S: AsFd> OpCode for Connect<S> {
type Control = ();
fn create_entry(&mut self, _: &mut Self::Control) -> OpEntry {
opcode::Connect::new(
Fd(self.fd.as_fd().as_raw_fd()),
self.addr.as_ptr().cast(),
self.addr.len(),
)
.build()
.into()
}
fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
self.call()
}
}
unsafe impl<T: IoBuf, S: AsFd> OpCode for Send<T, S> {
type Control = ();
fn create_entry(&mut self, _: &mut Self::Control) -> OpEntry {
let slice = self.buffer.as_init();
opcode::Send::new(
Fd(self.fd.as_fd().as_raw_fd()),
slice.as_ptr(),
slice.len().try_into().unwrap_or(u32::MAX),
)
.flags(self.flags.bits() as _)
.build()
.into()
}
fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
self.call()
}
}
unsafe impl<T: IoVectoredBuf, S: AsFd> OpCode for SendVectored<T, S> {
type Control = SendVectoredControl;
unsafe fn init(&mut self, ctrl: &mut Self::Control) {
self.init_control(ctrl)
}
fn create_entry(&mut self, control: &mut Self::Control) -> OpEntry {
opcode::SendMsg::new(Fd(self.fd.as_fd().as_raw_fd()), &control.msg)
.flags(self.flags.bits() as _)
.build()
.into()
}
fn call_blocking(&mut self, control: &mut Self::Control) -> io::Result<usize> {
self.call(control)
}
}
unsafe impl<T: IoBuf, S: AsFd> OpCode for SendTo<T, S> {
type Control = SendMsgControl;
unsafe fn init(&mut self, ctrl: &mut Self::Control) {
self.header.create_control(ctrl, [self.buffer.sys_slice()])
}
fn create_entry(&mut self, control: &mut Self::Control) -> OpEntry {
opcode::SendMsg::new(Fd(self.header.fd.as_fd().as_raw_fd()), &control.msg)
.flags(self.header.flags.bits() as _)
.build()
.into()
}
fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
self.call()
}
}
unsafe impl<T: IoVectoredBuf, S: AsFd> OpCode for SendToVectored<T, S> {
type Control = SendMsgControl;
unsafe fn init(&mut self, ctrl: &mut Self::Control) {
self.header.create_control(ctrl, self.buffer.sys_slices())
}
fn create_entry(&mut self, control: &mut Self::Control) -> OpEntry {
opcode::SendMsg::new(Fd(self.header.fd.as_fd().as_raw_fd()), &control.msg)
.flags(self.header.flags.bits() as _)
.build()
.into()
}
fn call_blocking(&mut self, control: &mut Self::Control) -> io::Result<usize> {
self.call(control)
}
}
unsafe impl<T: IoVectoredBuf, C: IoBuf, S: AsFd> OpCode for SendMsg<T, C, S> {
type Control = SendMsgControl;
unsafe fn init(&mut self, ctrl: &mut Self::Control) {
self.init_control(ctrl)
}
fn create_entry(&mut self, control: &mut Self::Control) -> OpEntry {
opcode::SendMsg::new(Fd(self.fd.as_fd().as_raw_fd()), &control.msg)
.flags(self.flags.bits() as _)
.build()
.into()
}
fn call_blocking(&mut self, control: &mut Self::Control) -> io::Result<usize> {
self.call(control)
}
}
unsafe impl<T: IoBufMut, S: AsFd> OpCode for Recv<T, S> {
type Control = ();
fn create_entry(&mut self, _: &mut Self::Control) -> OpEntry {
let fd = self.fd.as_fd().as_raw_fd();
let slice = self.buffer.sys_slice_mut();
let entry = opcode::Recv::new(
Fd(fd),
slice.ptr() as _,
slice.len().try_into().unwrap_or(u32::MAX),
)
.flags(self.flags.bits() as _)
.build();
let entry = set_poll_first(entry, self.poll_first);
entry.into()
}
fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
self.call()
}
}
unsafe impl<T: IoVectoredBufMut, S: AsFd> OpCode for RecvVectored<T, S> {
type Control = RecvVectoredControl;
unsafe fn init(&mut self, ctrl: &mut Self::Control) {
self.init_control(ctrl)
}
fn create_entry(&mut self, control: &mut Self::Control) -> OpEntry {
let entry = opcode::RecvMsg::new(Fd(self.fd.as_fd().as_raw_fd()), &mut control.msg)
.flags(self.flags.bits() as _)
.build();
let entry = set_poll_first(entry, self.poll_first);
entry.into()
}
fn call_blocking(&mut self, control: &mut Self::Control) -> io::Result<usize> {
self.call(control)
}
}
impl<S: AsFd> RecvFromHeader<S> {
pub fn create_control(
&mut self,
ctrl: &mut RecvMsgControl,
slices: impl Into<Multi<SysSlice>>,
) {
ctrl.msg.msg_name = &raw mut self.addr as _;
ctrl.msg.msg_namelen = self.addr.size_of() as _;
ctrl.slices = slices.into();
ctrl.msg.msg_iov = ctrl.slices.as_mut_ptr().cast();
ctrl.msg.msg_iovlen = ctrl.slices.len() as _;
}
pub fn create_entry(&mut self, control: &mut RecvMsgControl) -> OpEntry {
let entry = opcode::RecvMsg::new(Fd(self.fd.as_fd().as_raw_fd()), &mut control.msg)
.flags(self.flags.bits() as _)
.build();
let entry = set_poll_first(entry, self.poll_first);
entry.into()
}
pub fn set_result(&mut self, control: &mut RecvMsgControl) {
self.addr_len = control.msg.msg_namelen;
}
}
unsafe impl<T: IoBufMut, S: AsFd> OpCode for RecvFrom<T, S> {
type Control = RecvMsgControl;
unsafe fn init(&mut self, ctrl: &mut Self::Control) {
self.header
.create_control(ctrl, [self.buffer.sys_slice_mut()])
}
fn create_entry(&mut self, control: &mut Self::Control) -> OpEntry {
self.header.create_entry(control)
}
unsafe fn set_result(
&mut self,
control: &mut Self::Control,
_: &io::Result<usize>,
_: &crate::Extra,
) {
self.header.set_result(control);
}
fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
self.call()
}
}
unsafe impl<T: IoVectoredBufMut, S: AsFd> OpCode for RecvFromVectored<T, S> {
type Control = RecvMsgControl;
unsafe fn init(&mut self, ctrl: &mut Self::Control) {
self.header
.create_control(ctrl, self.buffer.sys_slices_mut())
}
fn create_entry(&mut self, control: &mut Self::Control) -> OpEntry {
self.header.create_entry(control)
}
unsafe fn set_result(
&mut self,
control: &mut Self::Control,
_: &io::Result<usize>,
_: &crate::Extra,
) {
self.header.set_result(control);
}
fn call_blocking(&mut self, control: &mut Self::Control) -> io::Result<usize> {
self.call(control)
}
}
unsafe impl<T: IoVectoredBufMut, C: IoBufMut, S: AsFd> OpCode for RecvMsg<T, C, S> {
type Control = RecvMsgControl;
unsafe fn init(&mut self, ctrl: &mut Self::Control) {
self.init_control(ctrl)
}
fn create_entry(&mut self, control: &mut Self::Control) -> OpEntry {
let entry = opcode::RecvMsg::new(Fd(self.header.fd.as_fd().as_raw_fd()), &mut control.msg)
.flags(self.header.flags.bits() as _)
.build();
let entry = set_poll_first(entry, self.poll_first);
entry.into()
}
unsafe fn set_result(
&mut self,
control: &mut Self::Control,
_: &io::Result<usize>,
_: &crate::Extra,
) {
self.update_control(control);
}
fn call_blocking(&mut self, control: &mut Self::Control) -> io::Result<usize> {
self.call(control)
}
}