compio_driver/sys/op/socket/
iour.rs1use 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 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 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 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 .into()
249 }
250
251 fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
252 self.call()
253 }
254}
255
256unsafe impl<T: IoVectoredBufMut, S: AsFd> OpCode for RecvVectored<T, S> {
257 type Control = RecvVectoredControl;
258
259 unsafe fn init(&mut self, ctrl: &mut Self::Control) {
260 self.init_control(ctrl)
261 }
262
263 fn create_entry(&mut self, control: &mut Self::Control) -> OpEntry {
264 opcode::RecvMsg::new(Fd(self.fd.as_fd().as_raw_fd()), &mut control.msg)
265 .flags(self.flags.bits() as _)
266 .build()
267 .into()
268 }
269
270 fn call_blocking(&mut self, control: &mut Self::Control) -> io::Result<usize> {
271 self.call(control)
272 }
273}
274
275impl<S: AsFd> RecvFromHeader<S> {
276 pub fn create_control(
277 &mut self,
278 ctrl: &mut RecvMsgControl,
279 slices: impl Into<Multi<SysSlice>>,
280 ) {
281 ctrl.msg.msg_name = &raw mut self.addr as _;
282 ctrl.msg.msg_namelen = self.addr.size_of() as _;
283 ctrl.slices = slices.into();
284 ctrl.msg.msg_iov = ctrl.slices.as_mut_ptr().cast();
285 ctrl.msg.msg_iovlen = ctrl.slices.len() as _;
286 }
287
288 pub fn create_entry(&mut self, control: &mut RecvMsgControl) -> OpEntry {
289 opcode::RecvMsg::new(Fd(self.fd.as_fd().as_raw_fd()), &mut control.msg)
290 .flags(self.flags.bits() as _)
291 .build()
292 .into()
293 }
294
295 pub fn set_result(&mut self, control: &mut RecvMsgControl) {
296 self.addr_len = control.msg.msg_namelen;
297 }
298}
299
300unsafe impl<T: IoBufMut, S: AsFd> OpCode for RecvFrom<T, S> {
301 type Control = RecvMsgControl;
302
303 unsafe fn init(&mut self, ctrl: &mut Self::Control) {
304 self.header
305 .create_control(ctrl, [self.buffer.sys_slice_mut()])
306 }
307
308 fn create_entry(&mut self, control: &mut Self::Control) -> OpEntry {
309 self.header.create_entry(control)
310 }
311
312 unsafe fn set_result(
313 &mut self,
314 control: &mut Self::Control,
315 _: &io::Result<usize>,
316 _: &crate::Extra,
317 ) {
318 self.header.set_result(control);
319 }
320
321 fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
322 self.call()
323 }
324}
325
326unsafe impl<T: IoVectoredBufMut, S: AsFd> OpCode for RecvFromVectored<T, S> {
327 type Control = RecvMsgControl;
328
329 unsafe fn init(&mut self, ctrl: &mut Self::Control) {
330 self.header
331 .create_control(ctrl, self.buffer.sys_slices_mut())
332 }
333
334 fn create_entry(&mut self, control: &mut Self::Control) -> OpEntry {
335 self.header.create_entry(control)
336 }
337
338 unsafe fn set_result(
339 &mut self,
340 control: &mut Self::Control,
341 _: &io::Result<usize>,
342 _: &crate::Extra,
343 ) {
344 self.header.set_result(control);
345 }
346
347 fn call_blocking(&mut self, control: &mut Self::Control) -> io::Result<usize> {
348 self.call(control)
349 }
350}
351
352unsafe impl<T: IoVectoredBufMut, C: IoBufMut, S: AsFd> OpCode for RecvMsg<T, C, S> {
353 type Control = RecvMsgControl;
354
355 unsafe fn init(&mut self, ctrl: &mut Self::Control) {
356 self.init_control(ctrl)
357 }
358
359 fn create_entry(&mut self, control: &mut Self::Control) -> OpEntry {
360 opcode::RecvMsg::new(Fd(self.header.fd.as_fd().as_raw_fd()), &mut control.msg)
361 .flags(self.header.flags.bits() as _)
362 .build()
363 .into()
364 }
365
366 unsafe fn set_result(
367 &mut self,
368 control: &mut Self::Control,
369 _: &io::Result<usize>,
370 _: &crate::Extra,
371 ) {
372 self.update_control(control);
373 }
374
375 fn call_blocking(&mut self, control: &mut Self::Control) -> io::Result<usize> {
376 self.call(control)
377 }
378}