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 let entry = 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 let entry = set_poll_first(entry, self.poll_first);
105 entry.into()
106 }
107
108 unsafe fn set_result(&mut self, _: &mut Self::Control, res: &io::Result<usize>, _: &Extra) {
109 if let Ok(fd) = res {
110 let fd = unsafe { Socket2::from_raw_fd(*fd as _) };
112 self.accepted_fd = Some(fd);
113 }
114 }
115
116 fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
117 self.call()
118 }
119}
120
121unsafe impl<S: AsFd> OpCode for Connect<S> {
122 type Control = ();
123
124 fn create_entry(&mut self, _: &mut Self::Control) -> OpEntry {
125 opcode::Connect::new(
126 Fd(self.fd.as_fd().as_raw_fd()),
127 self.addr.as_ptr().cast(),
128 self.addr.len(),
129 )
130 .build()
131 .into()
132 }
133
134 fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
135 self.call()
136 }
137}
138
139unsafe impl<T: IoBuf, S: AsFd> OpCode for Send<T, S> {
140 type Control = ();
141
142 fn create_entry(&mut self, _: &mut Self::Control) -> OpEntry {
143 let slice = self.buffer.as_init();
144 opcode::Send::new(
145 Fd(self.fd.as_fd().as_raw_fd()),
146 slice.as_ptr(),
147 slice.len().try_into().unwrap_or(u32::MAX),
148 )
149 .flags(self.flags.bits() as _)
150 .build()
151 .into()
152 }
153
154 fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
155 self.call()
156 }
157}
158
159unsafe impl<T: IoVectoredBuf, S: AsFd> OpCode for SendVectored<T, S> {
160 type Control = SendVectoredControl;
161
162 unsafe fn init(&mut self, ctrl: &mut Self::Control) {
163 self.init_control(ctrl)
164 }
165
166 fn create_entry(&mut self, control: &mut Self::Control) -> OpEntry {
167 opcode::SendMsg::new(Fd(self.fd.as_fd().as_raw_fd()), &control.msg)
168 .flags(self.flags.bits() as _)
169 .build()
170 .into()
171 }
172
173 fn call_blocking(&mut self, control: &mut Self::Control) -> io::Result<usize> {
174 self.call(control)
175 }
176}
177
178unsafe impl<T: IoBuf, S: AsFd> OpCode for SendTo<T, S> {
179 type Control = SendMsgControl;
180
181 unsafe fn init(&mut self, ctrl: &mut Self::Control) {
182 self.header.create_control(ctrl, [self.buffer.sys_slice()])
183 }
184
185 fn create_entry(&mut self, control: &mut Self::Control) -> OpEntry {
186 opcode::SendMsg::new(Fd(self.header.fd.as_fd().as_raw_fd()), &control.msg)
187 .flags(self.header.flags.bits() as _)
188 .build()
189 .into()
190 }
191
192 fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
193 self.call()
194 }
195}
196
197unsafe impl<T: IoVectoredBuf, S: AsFd> OpCode for SendToVectored<T, S> {
198 type Control = SendMsgControl;
199
200 unsafe fn init(&mut self, ctrl: &mut Self::Control) {
201 self.header.create_control(ctrl, self.buffer.sys_slices())
202 }
203
204 fn create_entry(&mut self, control: &mut Self::Control) -> OpEntry {
205 opcode::SendMsg::new(Fd(self.header.fd.as_fd().as_raw_fd()), &control.msg)
206 .flags(self.header.flags.bits() as _)
207 .build()
208 .into()
209 }
210
211 fn call_blocking(&mut self, control: &mut Self::Control) -> io::Result<usize> {
212 self.call(control)
213 }
214}
215
216unsafe impl<T: IoVectoredBuf, C: IoBuf, S: AsFd> OpCode for SendMsg<T, C, S> {
217 type Control = SendMsgControl;
218
219 unsafe fn init(&mut self, ctrl: &mut Self::Control) {
220 self.init_control(ctrl)
221 }
222
223 fn create_entry(&mut self, control: &mut Self::Control) -> OpEntry {
224 opcode::SendMsg::new(Fd(self.fd.as_fd().as_raw_fd()), &control.msg)
225 .flags(self.flags.bits() as _)
226 .build()
227 .into()
228 }
229
230 fn call_blocking(&mut self, control: &mut Self::Control) -> io::Result<usize> {
231 self.call(control)
232 }
233}
234
235unsafe impl<T: IoBufMut, S: AsFd> OpCode for Recv<T, S> {
236 type Control = ();
237
238 fn create_entry(&mut self, _: &mut Self::Control) -> OpEntry {
239 let fd = self.fd.as_fd().as_raw_fd();
240 let slice = self.buffer.sys_slice_mut();
241
242 let entry = opcode::Recv::new(
243 Fd(fd),
244 slice.ptr() as _,
245 slice.len().try_into().unwrap_or(u32::MAX),
246 )
247 .flags(self.flags.bits() as _)
248 .build();
249 let entry = set_poll_first(entry, self.poll_first);
250 entry.into()
251 }
252
253 fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
254 self.call()
255 }
256}
257
258unsafe impl<T: IoVectoredBufMut, S: AsFd> OpCode for RecvVectored<T, S> {
259 type Control = RecvVectoredControl;
260
261 unsafe fn init(&mut self, ctrl: &mut Self::Control) {
262 self.init_control(ctrl)
263 }
264
265 fn create_entry(&mut self, control: &mut Self::Control) -> OpEntry {
266 let entry = opcode::RecvMsg::new(Fd(self.fd.as_fd().as_raw_fd()), &mut control.msg)
267 .flags(self.flags.bits() as _)
268 .build();
269 let entry = set_poll_first(entry, self.poll_first);
270 entry.into()
271 }
272
273 fn call_blocking(&mut self, control: &mut Self::Control) -> io::Result<usize> {
274 self.call(control)
275 }
276}
277
278impl<S: AsFd> RecvFromHeader<S> {
279 pub fn create_control(
280 &mut self,
281 ctrl: &mut RecvMsgControl,
282 slices: impl Into<Multi<SysSlice>>,
283 ) {
284 ctrl.msg.msg_name = &raw mut self.addr as _;
285 ctrl.msg.msg_namelen = self.addr.size_of() as _;
286 ctrl.slices = slices.into();
287 ctrl.msg.msg_iov = ctrl.slices.as_mut_ptr().cast();
288 ctrl.msg.msg_iovlen = ctrl.slices.len() as _;
289 }
290
291 pub fn create_entry(&mut self, control: &mut RecvMsgControl) -> OpEntry {
292 let entry = opcode::RecvMsg::new(Fd(self.fd.as_fd().as_raw_fd()), &mut control.msg)
293 .flags(self.flags.bits() as _)
294 .build();
295 let entry = set_poll_first(entry, self.poll_first);
296 entry.into()
297 }
298
299 pub fn set_result(&mut self, control: &mut RecvMsgControl) {
300 self.addr_len = control.msg.msg_namelen;
301 }
302}
303
304unsafe impl<T: IoBufMut, S: AsFd> OpCode for RecvFrom<T, S> {
305 type Control = RecvMsgControl;
306
307 unsafe fn init(&mut self, ctrl: &mut Self::Control) {
308 self.header
309 .create_control(ctrl, [self.buffer.sys_slice_mut()])
310 }
311
312 fn create_entry(&mut self, control: &mut Self::Control) -> OpEntry {
313 self.header.create_entry(control)
314 }
315
316 unsafe fn set_result(
317 &mut self,
318 control: &mut Self::Control,
319 _: &io::Result<usize>,
320 _: &crate::Extra,
321 ) {
322 self.header.set_result(control);
323 }
324
325 fn call_blocking(&mut self, _: &mut Self::Control) -> io::Result<usize> {
326 self.call()
327 }
328}
329
330unsafe impl<T: IoVectoredBufMut, S: AsFd> OpCode for RecvFromVectored<T, S> {
331 type Control = RecvMsgControl;
332
333 unsafe fn init(&mut self, ctrl: &mut Self::Control) {
334 self.header
335 .create_control(ctrl, self.buffer.sys_slices_mut())
336 }
337
338 fn create_entry(&mut self, control: &mut Self::Control) -> OpEntry {
339 self.header.create_entry(control)
340 }
341
342 unsafe fn set_result(
343 &mut self,
344 control: &mut Self::Control,
345 _: &io::Result<usize>,
346 _: &crate::Extra,
347 ) {
348 self.header.set_result(control);
349 }
350
351 fn call_blocking(&mut self, control: &mut Self::Control) -> io::Result<usize> {
352 self.call(control)
353 }
354}
355
356unsafe impl<T: IoVectoredBufMut, C: IoBufMut, S: AsFd> OpCode for RecvMsg<T, C, S> {
357 type Control = RecvMsgControl;
358
359 unsafe fn init(&mut self, ctrl: &mut Self::Control) {
360 self.init_control(ctrl)
361 }
362
363 fn create_entry(&mut self, control: &mut Self::Control) -> OpEntry {
364 let entry = opcode::RecvMsg::new(Fd(self.header.fd.as_fd().as_raw_fd()), &mut control.msg)
365 .flags(self.header.flags.bits() as _)
366 .build();
367 let entry = set_poll_first(entry, self.poll_first);
368 entry.into()
369 }
370
371 unsafe fn set_result(
372 &mut self,
373 control: &mut Self::Control,
374 _: &io::Result<usize>,
375 _: &crate::Extra,
376 ) {
377 self.update_control(control);
378 }
379
380 fn call_blocking(&mut self, control: &mut Self::Control) -> io::Result<usize> {
381 self.call(control)
382 }
383}