Skip to main content

compio_driver/sys/op/socket/
iour.rs

1use std::ffi::c_int;
2
3use io_uring::{opcode, types::Fd};
4
5use crate::{IourOpCode as OpCode, OpEntry, sys::op::*};
6
7unsafe impl OpCode for CreateSocket {
8    type Control = ();
9
10    fn create_entry(&mut self, _: &mut Self::Control) -> OpEntry {
11        opcode::Socket::new(
12            self.domain.as_raw() as _,
13            self.socket_type.as_raw() as c_int | libc::SOCK_CLOEXEC,
14            self.protocol.map(|p| p.as_raw().get()).unwrap_or_default() as _,
15        )
16        .build()
17        .into()
18    }
19
20    fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
21        self.call()
22    }
23
24    unsafe fn set_result(&mut self, _: &mut Self::Control, res: &io::Result<usize>, _: &Extra) {
25        if let Ok(fd) = res {
26            // SAFETY: fd is a valid fd returned from kernel
27            let fd = unsafe { Socket2::from_raw_fd(*fd as _) };
28            self.opened_fd = Some(fd);
29        }
30    }
31}
32
33unsafe impl<S: AsFd> OpCode for Bind<S> {
34    type Control = ();
35
36    fn create_entry(&mut self, _: &mut Self::Control) -> OpEntry {
37        opcode::Bind::new(
38            Fd(self.fd.as_fd().as_raw_fd()),
39            self.addr.as_ptr().cast(),
40            self.addr.len(),
41        )
42        .build()
43        .into()
44    }
45
46    fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
47        self.call()
48    }
49}
50
51unsafe impl<S: AsFd> OpCode for Listen<S> {
52    type Control = ();
53
54    fn create_entry(&mut self, _: &mut Self::Control) -> OpEntry {
55        opcode::Listen::new(Fd(self.fd.as_fd().as_raw_fd()), self.backlog)
56            .build()
57            .into()
58    }
59
60    fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
61        self.call()
62    }
63}
64
65unsafe impl<S: AsFd> OpCode for ShutdownSocket<S> {
66    type Control = ();
67
68    fn create_entry(&mut self, _: &mut Self::Control) -> OpEntry {
69        opcode::Shutdown::new(Fd(self.fd.as_fd().as_raw_fd()), self.how())
70            .build()
71            .into()
72    }
73
74    fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
75        self.call()
76    }
77}
78
79unsafe impl OpCode for CloseSocket {
80    type Control = ();
81
82    fn create_entry(&mut self, _: &mut Self::Control) -> OpEntry {
83        opcode::Close::new(Fd(self.fd.as_fd().as_raw_fd()))
84            .build()
85            .into()
86    }
87
88    fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
89        self.call()
90    }
91}
92
93unsafe impl<S: AsFd> OpCode for Accept<S> {
94    type Control = ();
95
96    fn create_entry(&mut self, _: &mut Self::Control) -> OpEntry {
97        opcode::Accept::new(
98            Fd(self.fd.as_fd().as_raw_fd()),
99            unsafe { self.buffer.view_as::<libc::sockaddr>() },
100            &raw mut self.addr_len,
101        )
102        .flags(libc::SOCK_CLOEXEC)
103        .build()
104        .into()
105    }
106
107    unsafe fn set_result(&mut self, _: &mut Self::Control, res: &io::Result<usize>, _: &Extra) {
108        if let Ok(fd) = res {
109            // SAFETY: fd is a valid fd returned from kernel
110            let fd = unsafe { Socket2::from_raw_fd(*fd as _) };
111            self.accepted_fd = Some(fd);
112        }
113    }
114
115    fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
116        self.call()
117    }
118}
119
120unsafe impl<S: AsFd> OpCode for Connect<S> {
121    type Control = ();
122
123    fn create_entry(&mut self, _: &mut Self::Control) -> OpEntry {
124        opcode::Connect::new(
125            Fd(self.fd.as_fd().as_raw_fd()),
126            self.addr.as_ptr().cast(),
127            self.addr.len(),
128        )
129        .build()
130        .into()
131    }
132
133    fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
134        self.call()
135    }
136}
137
138unsafe impl<T: IoBuf, S: AsFd> OpCode for Send<T, S> {
139    type Control = ();
140
141    fn create_entry(&mut self, _: &mut Self::Control) -> OpEntry {
142        let slice = self.buffer.as_init();
143        opcode::Send::new(
144            Fd(self.fd.as_fd().as_raw_fd()),
145            slice.as_ptr(),
146            slice.len().try_into().unwrap_or(u32::MAX),
147        )
148        .flags(self.flags.bits() as _)
149        .build()
150        .into()
151    }
152
153    fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
154        self.call()
155    }
156}
157
158unsafe impl<T: IoVectoredBuf, S: AsFd> OpCode for SendVectored<T, S> {
159    type Control = SendVectoredControl;
160
161    unsafe fn init(&mut self, ctrl: &mut Self::Control) {
162        self.init_control(ctrl)
163    }
164
165    fn create_entry(&mut self, control: &mut Self::Control) -> OpEntry {
166        opcode::SendMsg::new(Fd(self.fd.as_fd().as_raw_fd()), &control.msg)
167            .flags(self.flags.bits() as _)
168            .build()
169            .into()
170    }
171
172    fn call_blocking(&mut self, control: &mut Self::Control) -> io::Result<usize> {
173        self.call(control)
174    }
175}
176
177unsafe impl<T: IoBuf, S: AsFd> OpCode for SendTo<T, S> {
178    type Control = SendMsgControl;
179
180    unsafe fn init(&mut self, ctrl: &mut Self::Control) {
181        self.header.create_control(ctrl, [self.buffer.sys_slice()])
182    }
183
184    fn create_entry(&mut self, control: &mut Self::Control) -> OpEntry {
185        opcode::SendMsg::new(Fd(self.header.fd.as_fd().as_raw_fd()), &control.msg)
186            .flags(self.header.flags.bits() as _)
187            .build()
188            .into()
189    }
190
191    fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
192        self.call()
193    }
194}
195
196unsafe impl<T: IoVectoredBuf, S: AsFd> OpCode for SendToVectored<T, S> {
197    type Control = SendMsgControl;
198
199    unsafe fn init(&mut self, ctrl: &mut Self::Control) {
200        self.header.create_control(ctrl, self.buffer.sys_slices())
201    }
202
203    fn create_entry(&mut self, control: &mut Self::Control) -> OpEntry {
204        opcode::SendMsg::new(Fd(self.header.fd.as_fd().as_raw_fd()), &control.msg)
205            .flags(self.header.flags.bits() as _)
206            .build()
207            .into()
208    }
209
210    fn call_blocking(&mut self, control: &mut Self::Control) -> io::Result<usize> {
211        self.call(control)
212    }
213}
214
215unsafe impl<T: IoVectoredBuf, C: IoBuf, S: AsFd> OpCode for SendMsg<T, C, S> {
216    type Control = SendMsgControl;
217
218    unsafe fn init(&mut self, ctrl: &mut Self::Control) {
219        self.init_control(ctrl)
220    }
221
222    fn create_entry(&mut self, control: &mut Self::Control) -> OpEntry {
223        opcode::SendMsg::new(Fd(self.fd.as_fd().as_raw_fd()), &control.msg)
224            .flags(self.flags.bits() as _)
225            .build()
226            .into()
227    }
228
229    fn call_blocking(&mut self, control: &mut Self::Control) -> io::Result<usize> {
230        self.call(control)
231    }
232}
233
234unsafe impl<T: IoBufMut, S: AsFd> OpCode for Recv<T, S> {
235    type Control = ();
236
237    fn create_entry(&mut self, _: &mut Self::Control) -> OpEntry {
238        let fd = self.fd.as_fd().as_raw_fd();
239        let slice = self.buffer.sys_slice_mut();
240
241        let entry = opcode::Recv::new(
242            Fd(fd),
243            slice.ptr() as _,
244            slice.len().try_into().unwrap_or(u32::MAX),
245        )
246        .flags(self.flags.bits() as _)
247        .build();
248        let entry = set_poll_first(entry, self.poll_first);
249        entry.into()
250    }
251
252    fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
253        self.call()
254    }
255}
256
257unsafe impl<T: IoVectoredBufMut, S: AsFd> OpCode for RecvVectored<T, S> {
258    type Control = RecvVectoredControl;
259
260    unsafe fn init(&mut self, ctrl: &mut Self::Control) {
261        self.init_control(ctrl)
262    }
263
264    fn create_entry(&mut self, control: &mut Self::Control) -> OpEntry {
265        let entry = opcode::RecvMsg::new(Fd(self.fd.as_fd().as_raw_fd()), &mut control.msg)
266            .flags(self.flags.bits() as _)
267            .build();
268        let entry = set_poll_first(entry, self.poll_first);
269        entry.into()
270    }
271
272    fn call_blocking(&mut self, control: &mut Self::Control) -> io::Result<usize> {
273        self.call(control)
274    }
275}
276
277impl<S: AsFd> RecvFromHeader<S> {
278    pub fn create_control(
279        &mut self,
280        ctrl: &mut RecvMsgControl,
281        slices: impl Into<Multi<SysSlice>>,
282    ) {
283        ctrl.msg.msg_name = &raw mut self.addr as _;
284        ctrl.msg.msg_namelen = self.addr.size_of() as _;
285        ctrl.slices = slices.into();
286        ctrl.msg.msg_iov = ctrl.slices.as_mut_ptr().cast();
287        ctrl.msg.msg_iovlen = ctrl.slices.len() as _;
288    }
289
290    pub fn create_entry(&mut self, control: &mut RecvMsgControl) -> OpEntry {
291        let entry = opcode::RecvMsg::new(Fd(self.fd.as_fd().as_raw_fd()), &mut control.msg)
292            .flags(self.flags.bits() as _)
293            .build();
294        let entry = set_poll_first(entry, self.poll_first);
295        entry.into()
296    }
297
298    pub fn set_result(&mut self, control: &mut RecvMsgControl) {
299        self.addr_len = control.msg.msg_namelen;
300    }
301}
302
303unsafe impl<T: IoBufMut, S: AsFd> OpCode for RecvFrom<T, S> {
304    type Control = RecvMsgControl;
305
306    unsafe fn init(&mut self, ctrl: &mut Self::Control) {
307        self.header
308            .create_control(ctrl, [self.buffer.sys_slice_mut()])
309    }
310
311    fn create_entry(&mut self, control: &mut Self::Control) -> OpEntry {
312        self.header.create_entry(control)
313    }
314
315    unsafe fn set_result(
316        &mut self,
317        control: &mut Self::Control,
318        _: &io::Result<usize>,
319        _: &crate::Extra,
320    ) {
321        self.header.set_result(control);
322    }
323
324    fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
325        self.call()
326    }
327}
328
329unsafe impl<T: IoVectoredBufMut, S: AsFd> OpCode for RecvFromVectored<T, S> {
330    type Control = RecvMsgControl;
331
332    unsafe fn init(&mut self, ctrl: &mut Self::Control) {
333        self.header
334            .create_control(ctrl, self.buffer.sys_slices_mut())
335    }
336
337    fn create_entry(&mut self, control: &mut Self::Control) -> OpEntry {
338        self.header.create_entry(control)
339    }
340
341    unsafe fn set_result(
342        &mut self,
343        control: &mut Self::Control,
344        _: &io::Result<usize>,
345        _: &crate::Extra,
346    ) {
347        self.header.set_result(control);
348    }
349
350    fn call_blocking(&mut self, control: &mut Self::Control) -> io::Result<usize> {
351        self.call(control)
352    }
353}
354
355unsafe impl<T: IoVectoredBufMut, C: IoBufMut, S: AsFd> OpCode for RecvMsg<T, C, S> {
356    type Control = RecvMsgControl;
357
358    unsafe fn init(&mut self, ctrl: &mut Self::Control) {
359        self.init_control(ctrl)
360    }
361
362    fn create_entry(&mut self, control: &mut Self::Control) -> OpEntry {
363        let entry = opcode::RecvMsg::new(Fd(self.header.fd.as_fd().as_raw_fd()), &mut control.msg)
364            .flags(self.header.flags.bits() as _)
365            .build();
366        let entry = set_poll_first(entry, self.poll_first);
367        entry.into()
368    }
369
370    unsafe fn set_result(
371        &mut self,
372        control: &mut Self::Control,
373        _: &io::Result<usize>,
374        _: &crate::Extra,
375    ) {
376        self.update_control(control);
377    }
378
379    fn call_blocking(&mut self, control: &mut Self::Control) -> io::Result<usize> {
380        self.call(control)
381    }
382}