io_uring/submit.rs
1use std::os::unix::io::{AsRawFd, RawFd};
2use std::sync::atomic;
3use std::{io, mem, ptr};
4
5use crate::register::{execute, Probe};
6use crate::sys;
7use crate::types::{CancelBuilder, Timespec};
8use crate::util::{cast_ptr, OwnedFd};
9use crate::Parameters;
10use bitflags::bitflags;
11
12use crate::register::Restriction;
13
14use crate::types;
15
16bitflags!(
17 /// See man page for complete description:
18 /// https://man7.org/linux/man-pages/man2/io_uring_enter.2.html
19 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
20 pub struct EnterFlags: u32 {
21 /// Wait for at least `min_complete` events to complete.
22 const GETEVENTS = sys::IORING_ENTER_GETEVENTS;
23
24 /// If the kernel thread is sleeping, wake it up.
25 const SQ_WAKEUP = sys::IORING_ENTER_SQ_WAKEUP;
26
27 /// Wait for at least one submission queue entry to be available.
28 const SQ_WAIT = sys::IORING_ENTER_SQ_WAIT;
29
30 /// Use the extended argument structure.
31 const EXT_ARG = sys::IORING_ENTER_EXT_ARG;
32
33 /// Submit using registered submission queue ring.
34 const REGISTERED_RING = sys::IORING_ENTER_REGISTERED_RING;
35
36 /// Timeout argument interpreted as absolute time.
37 const ABS_TIMER = sys::IORING_ENTER_ABS_TIMER;
38
39 /// Arg is offset into an area of wait regions previously registered.
40 const EXT_ARG_REG = sys::IORING_ENTER_EXT_ARG_REG;
41
42 /// Don't mark waiting task as being in iowait in certain cases.
43 const NO_IOWAIT = sys::IORING_ENTER_NO_IOWAIT;
44 }
45);
46
47/// Interface for submitting submission queue events in an io_uring instance to the kernel for
48/// executing and registering files or buffers with the instance.
49///
50/// io_uring supports both directly performing I/O on buffers and file descriptors and registering
51/// them beforehand. Registering is slow, but it makes performing the actual I/O much faster.
52pub struct Submitter<'a> {
53 fd: &'a OwnedFd,
54 params: &'a Parameters,
55
56 sq_head: *const atomic::AtomicU32,
57 sq_tail: *const atomic::AtomicU32,
58 sq_flags: *const atomic::AtomicU32,
59}
60
61impl<'a> Submitter<'a> {
62 #[inline]
63 pub(crate) const fn new(
64 fd: &'a OwnedFd,
65 params: &'a Parameters,
66 sq_head: *const atomic::AtomicU32,
67 sq_tail: *const atomic::AtomicU32,
68 sq_flags: *const atomic::AtomicU32,
69 ) -> Submitter<'a> {
70 Submitter {
71 fd,
72 params,
73 sq_head,
74 sq_tail,
75 sq_flags,
76 }
77 }
78
79 #[inline]
80 fn sq_len(&self) -> usize {
81 unsafe {
82 let head = (*self.sq_head).load(atomic::Ordering::Acquire);
83 let tail = (*self.sq_tail).load(atomic::Ordering::Acquire);
84
85 tail.wrapping_sub(head) as usize
86 }
87 }
88
89 /// Whether the kernel thread has gone to sleep because it waited for too long without
90 /// submission queue entries.
91 #[inline]
92 fn sq_need_wakeup(&self) -> bool {
93 unsafe {
94 (*self.sq_flags).load(atomic::Ordering::Relaxed) & sys::IORING_SQ_NEED_WAKEUP != 0
95 }
96 }
97
98 /// CQ ring is overflown
99 fn sq_cq_overflow(&self) -> bool {
100 unsafe {
101 (*self.sq_flags).load(atomic::Ordering::Relaxed) & sys::IORING_SQ_CQ_OVERFLOW != 0
102 }
103 }
104
105 /// Initiate and/or complete asynchronous I/O. This is a low-level wrapper around
106 /// `io_uring_enter` - see `man io_uring_enter` (or [its online
107 /// version](https://manpages.debian.org/unstable/liburing-dev/io_uring_enter.2.en.html) for
108 /// more details.
109 ///
110 /// You will probably want to use a more high-level API such as
111 /// [`submit`](Self::submit) or [`submit_and_wait`](Self::submit_and_wait).
112 ///
113 /// # Safety
114 ///
115 /// This provides a raw interface so developer must ensure that parameters are correct.
116 pub unsafe fn enter<T: Sized>(
117 &self,
118 to_submit: u32,
119 min_complete: u32,
120 flag: u32,
121 arg: Option<&T>,
122 ) -> io::Result<usize> {
123 let arg = arg
124 .map(|arg| cast_ptr(arg).cast())
125 .unwrap_or_else(ptr::null);
126 let size = mem::size_of::<T>();
127 sys::io_uring_enter(
128 self.fd.as_raw_fd(),
129 to_submit,
130 min_complete,
131 flag,
132 arg,
133 size,
134 )
135 .map(|res| res as _)
136 }
137
138 /// Submit all queued submission queue events to the kernel.
139 #[inline]
140 pub fn submit(&self) -> io::Result<usize> {
141 self.submit_and_wait(0)
142 }
143
144 /// Submit all queued submission queue events to the kernel and wait for at least `want`
145 /// completion events to complete.
146 pub fn submit_and_wait(&self, want: usize) -> io::Result<usize> {
147 let len = self.sq_len();
148 let mut flags = EnterFlags::empty();
149
150 // This logic suffers from the fact the sq_cq_overflow and sq_need_wakeup
151 // each cause an atomic load of the same variable, self.sq_flags.
152 // In the hottest paths, when a server is running with sqpoll,
153 // this is going to be hit twice, when once would be sufficient.
154 // However, consider that the `SeqCst` barrier required for interpreting
155 // the IORING_ENTER_SQ_WAKEUP bit is required in all paths where sqpoll
156 // is setup when consolidating the reads.
157
158 let sq_cq_overflow = self.sq_cq_overflow();
159
160 // When IORING_FEAT_NODROP is enabled and CQ overflows, the kernel buffers
161 // completion events internally but doesn't automatically flush them when
162 // CQ space becomes available. We must explicitly call io_uring_enter()
163 // to flush these buffered events, even with SQPOLL enabled.
164 //
165 // Without this, completions remain stuck in kernel's internal buffer
166 // after draining CQ, causing missing completion notifications.
167 let need_syscall_for_overflow = sq_cq_overflow && self.params.is_feature_nodrop();
168
169 if want > 0 || self.params.is_setup_iopoll() || sq_cq_overflow {
170 flags.insert(EnterFlags::GETEVENTS);
171 }
172
173 if self.params.is_setup_sqpoll() {
174 // See discussion in [`SubmissionQueue::need_wakeup`].
175 atomic::fence(atomic::Ordering::SeqCst);
176 if self.sq_need_wakeup() {
177 flags.insert(EnterFlags::SQ_WAKEUP);
178 } else if want == 0 && !need_syscall_for_overflow {
179 // The kernel thread is polling and hasn't fallen asleep, so we don't need to tell
180 // it to process events or wake it up
181
182 // However, if the CQ ring is overflown, we need to tell the kernel to process events
183 // by calling io_uring_enter with the IORING_ENTER_GETEVENTS flag.
184 return Ok(len);
185 }
186 }
187
188 unsafe { self.enter::<libc::sigset_t>(len as _, want as _, flags.bits(), None) }
189 }
190
191 /// Submit all queued submission queue events to the kernel and wait for at least `want`
192 /// completion events to complete with additional options
193 ///
194 /// You can specify a set of signals to mask and a timeout for operation, see
195 /// [`SubmitArgs`](types::SubmitArgs) for more details
196 pub fn submit_with_args(
197 &self,
198 want: usize,
199 args: &types::SubmitArgs<'_, '_>,
200 ) -> io::Result<usize> {
201 let len = self.sq_len();
202 let mut flags = EnterFlags::EXT_ARG;
203
204 let sq_cq_overflow = self.sq_cq_overflow();
205 let need_syscall = sq_cq_overflow & self.params.is_feature_nodrop();
206
207 if want > 0 || self.params.is_setup_iopoll() || sq_cq_overflow {
208 flags.insert(EnterFlags::GETEVENTS);
209 }
210
211 if self.params.is_setup_sqpoll() {
212 // See discussion in [`SubmissionQueue::need_wakeup`].
213 atomic::fence(atomic::Ordering::SeqCst);
214 if self.sq_need_wakeup() {
215 flags.insert(EnterFlags::SQ_WAKEUP);
216 } else if want == 0 && !need_syscall {
217 // The kernel thread is polling and hasn't fallen asleep, so we don't need to tell
218 // it to process events or wake it up
219 return Ok(len);
220 }
221 }
222
223 unsafe { self.enter(len as _, want as _, flags.bits(), Some(args)) }
224 }
225
226 /// Wait for the submission queue to have free entries.
227 pub fn squeue_wait(&self) -> io::Result<usize> {
228 unsafe { self.enter::<libc::sigset_t>(0, 0, EnterFlags::SQ_WAIT.bits(), None) }
229 }
230
231 /// Register in-memory fixed buffers for I/O with the kernel. You can use these buffers with the
232 /// [`ReadFixed`](crate::opcode::ReadFixed) and [`WriteFixed`](crate::opcode::WriteFixed)
233 /// operations.
234 ///
235 /// # Safety
236 ///
237 /// Developers must ensure that the `iov_base` and `iov_len` values are valid and will
238 /// be valid until buffers are unregistered or the ring destroyed, otherwise undefined
239 /// behaviour may occur.
240 pub unsafe fn register_buffers(&self, bufs: &[libc::iovec]) -> io::Result<()> {
241 execute(
242 self.fd.as_raw_fd(),
243 sys::IORING_REGISTER_BUFFERS,
244 bufs.as_ptr().cast(),
245 bufs.len() as _,
246 )
247 .map(drop)
248 }
249
250 /// Update a range of fixed buffers starting at `offset`.
251 ///
252 /// This is required to use buffers registered using
253 /// [`register_buffers_sparse`](Self::register_buffers_sparse),
254 /// although it can be also be used with [`register_buffers`](Self::register_buffers).
255 ///
256 /// See [`register_buffers2`](Self::register_buffers2)
257 /// for more information about resource tagging.
258 ///
259 /// Available since Linux 5.13.
260 ///
261 /// # Safety
262 ///
263 /// Developers must ensure that the `iov_base` and `iov_len` values are valid and will
264 /// be valid until buffers are unregistered or the ring destroyed, otherwise undefined
265 /// behaviour may occur.
266 pub unsafe fn register_buffers_update(
267 &self,
268 offset: u32,
269 bufs: &[libc::iovec],
270 tags: Option<&[u64]>,
271 ) -> io::Result<()> {
272 let nr = tags
273 .as_ref()
274 .map_or(bufs.len(), |tags| bufs.len().min(tags.len()));
275
276 let rr = sys::io_uring_rsrc_update2 {
277 nr: nr as _,
278 data: bufs.as_ptr() as _,
279 tags: tags.map(|tags| tags.as_ptr() as _).unwrap_or(0),
280 offset,
281 ..Default::default()
282 };
283
284 execute(
285 self.fd.as_raw_fd(),
286 sys::IORING_REGISTER_BUFFERS_UPDATE,
287 cast_ptr::<sys::io_uring_rsrc_update2>(&rr).cast(),
288 std::mem::size_of::<sys::io_uring_rsrc_update2>() as _,
289 )
290 .map(drop)
291 }
292
293 /// Variant of [`register_buffers`](Self::register_buffers)
294 /// with resource tagging.
295 ///
296 /// `tags` should be the same length as `bufs` and contain the
297 /// tag value corresponding to the buffer at the same index.
298 ///
299 /// If a tag is zero, then tagging for this particular resource
300 /// (a buffer in this case) is disabled. Otherwise, after the
301 /// resource had been unregistered and it's not used anymore,
302 /// a CQE will be posted with `user_data` set to the specified
303 /// tag and all other fields zeroed.
304 ///
305 /// Available since Linux 5.13.
306 ///
307 /// # Safety
308 ///
309 /// Developers must ensure that the `iov_base` and `iov_len` values are valid and will
310 /// be valid until buffers are unregistered or the ring destroyed, otherwise undefined
311 /// behaviour may occur.
312 pub unsafe fn register_buffers2(&self, bufs: &[libc::iovec], tags: &[u64]) -> io::Result<()> {
313 let rr = sys::io_uring_rsrc_register {
314 nr: bufs.len().min(tags.len()) as _,
315 data: bufs.as_ptr() as _,
316 tags: tags.as_ptr() as _,
317 ..Default::default()
318 };
319 execute(
320 self.fd.as_raw_fd(),
321 sys::IORING_REGISTER_BUFFERS2,
322 cast_ptr::<sys::io_uring_rsrc_register>(&rr).cast(),
323 std::mem::size_of::<sys::io_uring_rsrc_register>() as _,
324 )
325 .map(drop)
326 }
327
328 /// Registers an empty table of nr fixed buffers buffers.
329 ///
330 /// These must be updated before use, using eg.
331 /// [`register_buffers_update`](Self::register_buffers_update).
332 ///
333 /// See [`register_buffers`](Self::register_buffers)
334 /// for more information about fixed buffers.
335 ///
336 /// Available since Linux 5.13.
337 pub fn register_buffers_sparse(&self, nr: u32) -> io::Result<()> {
338 let rr = sys::io_uring_rsrc_register {
339 nr,
340 flags: sys::IORING_RSRC_REGISTER_SPARSE,
341 ..Default::default()
342 };
343 execute(
344 self.fd.as_raw_fd(),
345 sys::IORING_REGISTER_BUFFERS2,
346 cast_ptr::<sys::io_uring_rsrc_register>(&rr).cast(),
347 std::mem::size_of::<sys::io_uring_rsrc_register>() as _,
348 )
349 .map(drop)
350 }
351
352 /// Registers an empty file table of nr_files number of file descriptors. The sparse variant is
353 /// available in kernels 5.19 and later.
354 ///
355 /// Registering a file table is a prerequisite for using any request that
356 /// uses direct descriptors.
357 pub fn register_files_sparse(&self, nr: u32) -> io::Result<()> {
358 let rr = sys::io_uring_rsrc_register {
359 nr,
360 flags: sys::IORING_RSRC_REGISTER_SPARSE,
361 resv2: 0,
362 data: 0,
363 tags: 0,
364 };
365 execute(
366 self.fd.as_raw_fd(),
367 sys::IORING_REGISTER_FILES2,
368 cast_ptr::<sys::io_uring_rsrc_register>(&rr).cast(),
369 mem::size_of::<sys::io_uring_rsrc_register>() as _,
370 )
371 .map(drop)
372 }
373
374 /// Register files for I/O. You can use the registered files with
375 /// [`Fixed`](crate::types::Fixed).
376 ///
377 /// Each fd may be -1, in which case it is considered "sparse", and can be filled in later with
378 /// [`register_files_update`](Self::register_files_update).
379 ///
380 /// Note that this will wait for the ring to idle; it will only return once all active requests
381 /// are complete. Use [`register_files_update`](Self::register_files_update) to avoid this.
382 pub fn register_files(&self, fds: &[RawFd]) -> io::Result<()> {
383 execute(
384 self.fd.as_raw_fd(),
385 sys::IORING_REGISTER_FILES,
386 fds.as_ptr().cast(),
387 fds.len() as _,
388 )
389 .map(drop)
390 }
391
392 /// This operation replaces existing files in the registered file set with new ones,
393 /// either turning a sparse entry (one where fd is equal to -1) into a real one, removing an existing entry (new one is set to -1),
394 /// or replacing an existing entry with a new existing entry. The `offset` parameter specifies
395 /// the offset into the list of registered files at which to start updating files.
396 ///
397 /// You can also perform this asynchronously with the
398 /// [`FilesUpdate`](crate::opcode::FilesUpdate) opcode.
399 pub fn register_files_update(&self, offset: u32, fds: &[RawFd]) -> io::Result<usize> {
400 let fu = sys::io_uring_files_update {
401 offset,
402 resv: 0,
403 fds: fds.as_ptr() as _,
404 };
405 let ret = execute(
406 self.fd.as_raw_fd(),
407 sys::IORING_REGISTER_FILES_UPDATE,
408 cast_ptr::<sys::io_uring_files_update>(&fu).cast(),
409 fds.len() as _,
410 )?;
411 Ok(ret as _)
412 }
413
414 /// Register an eventfd created by [`eventfd`](libc::eventfd) with the io_uring instance.
415 pub fn register_eventfd(&self, eventfd: RawFd) -> io::Result<()> {
416 execute(
417 self.fd.as_raw_fd(),
418 sys::IORING_REGISTER_EVENTFD,
419 cast_ptr::<RawFd>(&eventfd).cast(),
420 1,
421 )
422 .map(drop)
423 }
424
425 /// This works just like [`register_eventfd`](Self::register_eventfd), except notifications are
426 /// only posted for events that complete in an async manner, so requests that complete
427 /// immediately will not cause a notification.
428 pub fn register_eventfd_async(&self, eventfd: RawFd) -> io::Result<()> {
429 execute(
430 self.fd.as_raw_fd(),
431 sys::IORING_REGISTER_EVENTFD_ASYNC,
432 cast_ptr::<RawFd>(&eventfd).cast(),
433 1,
434 )
435 .map(drop)
436 }
437
438 /// Fill in the given [`Probe`] with information about the opcodes supported by io_uring on the
439 /// running kernel.
440 ///
441 /// # Examples
442 ///
443 // This is marked no_run as it is only available from Linux 5.6+, however the latest Ubuntu (on
444 // which CI runs) only has Linux 5.4.
445 /// ```no_run
446 /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
447 /// let io_uring = io_uring::IoUring::new(1)?;
448 /// let mut probe = io_uring::Probe::new();
449 /// io_uring.submitter().register_probe(&mut probe)?;
450 ///
451 /// if probe.is_supported(io_uring::opcode::Read::CODE) {
452 /// println!("Reading is supported!");
453 /// }
454 /// # Ok(())
455 /// # }
456 /// ```
457 pub fn register_probe(&self, probe: &mut Probe) -> io::Result<()> {
458 execute(
459 self.fd.as_raw_fd(),
460 sys::IORING_REGISTER_PROBE,
461 probe.as_mut_ptr() as *const _,
462 Probe::COUNT as _,
463 )
464 .map(drop)
465 }
466
467 /// Register credentials of the running application with io_uring, and get an id associated with
468 /// these credentials. This ID can then be [passed](crate::squeue::Entry::personality) into
469 /// submission queue entries to issue the request with this process' credentials.
470 ///
471 /// By default, if [`Parameters::is_feature_cur_personality`] is set then requests will use the
472 /// credentials of the task that called [`Submitter::enter`], otherwise they will use the
473 /// credentials of the task that originally registered the io_uring.
474 ///
475 /// [`Parameters::is_feature_cur_personality`]: crate::Parameters::is_feature_cur_personality
476 pub fn register_personality(&self) -> io::Result<u16> {
477 let id = execute(
478 self.fd.as_raw_fd(),
479 sys::IORING_REGISTER_PERSONALITY,
480 ptr::null(),
481 0,
482 )?;
483 Ok(id as u16)
484 }
485
486 /// Unregister all previously registered buffers.
487 ///
488 /// You do not need to explicitly call this before dropping the [`IoUring`](crate::IoUring), as
489 /// it will be cleaned up by the kernel automatically.
490 ///
491 /// Available since Linux 5.1.
492 pub fn unregister_buffers(&self) -> io::Result<()> {
493 execute(
494 self.fd.as_raw_fd(),
495 sys::IORING_UNREGISTER_BUFFERS,
496 ptr::null(),
497 0,
498 )
499 .map(drop)
500 }
501
502 /// Unregister all previously registered files.
503 ///
504 /// You do not need to explicitly call this before dropping the [`IoUring`](crate::IoUring), as
505 /// it will be cleaned up by the kernel automatically.
506 pub fn unregister_files(&self) -> io::Result<()> {
507 execute(
508 self.fd.as_raw_fd(),
509 sys::IORING_UNREGISTER_FILES,
510 ptr::null(),
511 0,
512 )
513 .map(drop)
514 }
515
516 /// Unregister an eventfd file descriptor to stop notifications.
517 pub fn unregister_eventfd(&self) -> io::Result<()> {
518 execute(
519 self.fd.as_raw_fd(),
520 sys::IORING_UNREGISTER_EVENTFD,
521 ptr::null(),
522 0,
523 )
524 .map(drop)
525 }
526
527 /// Unregister a previously registered personality.
528 pub fn unregister_personality(&self, personality: u16) -> io::Result<()> {
529 execute(
530 self.fd.as_raw_fd(),
531 sys::IORING_UNREGISTER_PERSONALITY,
532 ptr::null(),
533 personality as _,
534 )
535 .map(drop)
536 }
537
538 /// Permanently install a feature allowlist. Once this has been called, attempting to perform
539 /// an operation not on the allowlist will fail with `-EACCES`.
540 ///
541 /// This can only be called once, to prevent untrusted code from removing restrictions.
542 pub fn register_restrictions(&self, res: &mut [Restriction]) -> io::Result<()> {
543 execute(
544 self.fd.as_raw_fd(),
545 sys::IORING_REGISTER_RESTRICTIONS,
546 res.as_mut_ptr().cast(),
547 res.len() as _,
548 )
549 .map(drop)
550 }
551
552 /// Enable the rings of the io_uring instance if they have been disabled with
553 /// [`setup_r_disabled`](crate::Builder::setup_r_disabled).
554 pub fn register_enable_rings(&self) -> io::Result<()> {
555 execute(
556 self.fd.as_raw_fd(),
557 sys::IORING_REGISTER_ENABLE_RINGS,
558 ptr::null(),
559 0,
560 )
561 .map(drop)
562 }
563
564 /// Tell io_uring on what CPUs the async workers can run. By default, async workers
565 /// created by io_uring will inherit the CPU mask of its parent. This is usually
566 /// all the CPUs in the system, unless the parent is being run with a limited set.
567 pub fn register_iowq_aff(&self, cpu_set: &libc::cpu_set_t) -> io::Result<()> {
568 execute(
569 self.fd.as_raw_fd(),
570 sys::IORING_REGISTER_IOWQ_AFF,
571 cpu_set as *const _ as *const libc::c_void,
572 mem::size_of::<libc::cpu_set_t>() as u32,
573 )
574 .map(drop)
575 }
576
577 /// Undoes a CPU mask previously set with register_iowq_aff
578 pub fn unregister_iowq_aff(&self) -> io::Result<()> {
579 execute(
580 self.fd.as_raw_fd(),
581 sys::IORING_UNREGISTER_IOWQ_AFF,
582 ptr::null(),
583 0,
584 )
585 .map(drop)
586 }
587
588 /// Get and/or set the limit for number of io_uring worker threads per NUMA
589 /// node. `max[0]` holds the limit for bounded workers, which process I/O
590 /// operations expected to be bound in time, that is I/O on regular files or
591 /// block devices. While `max[1]` holds the limit for unbounded workers,
592 /// which carry out I/O operations that can never complete, for instance I/O
593 /// on sockets. Passing `0` does not change the current limit. Returns
594 /// previous limits on success.
595 pub fn register_iowq_max_workers(&self, max: &mut [u32; 2]) -> io::Result<()> {
596 execute(
597 self.fd.as_raw_fd(),
598 sys::IORING_REGISTER_IOWQ_MAX_WORKERS,
599 max.as_mut_ptr().cast(),
600 max.len() as _,
601 )
602 .map(drop)
603 }
604
605 /// Register buffer ring for provided buffers.
606 ///
607 /// Details can be found in the io_uring_register_buf_ring.3 man page.
608 ///
609 /// If the register command is not supported, or the ring_entries value exceeds
610 /// 32768, the InvalidInput error is returned.
611 ///
612 /// Available since 5.19.
613 ///
614 /// # Safety
615 ///
616 /// Developers must ensure that the `ring_addr` and its length represented by `ring_entries`
617 /// are valid and will be valid until the bgid is unregistered or the ring destroyed,
618 /// otherwise undefined behaviour may occur.
619 #[deprecated(note = "please use `register_buf_ring_with_flags` instead")]
620 pub unsafe fn register_buf_ring(
621 &self,
622 ring_addr: u64,
623 ring_entries: u16,
624 bgid: u16,
625 ) -> io::Result<()> {
626 self.register_buf_ring_with_flags(ring_addr, ring_entries, bgid, 0)
627 }
628
629 /// Register buffer ring for provided buffers.
630 ///
631 /// Details can be found in the io_uring_register_buf_ring.3 man page.
632 ///
633 /// If the register command is not supported, or the ring_entries value exceeds
634 /// 32768, the InvalidInput error is returned.
635 ///
636 /// Available since 5.19.
637 ///
638 /// # Safety
639 ///
640 /// Developers must ensure that the `ring_addr` and its length represented by `ring_entries`
641 /// are valid and will be valid until the bgid is unregistered or the ring destroyed,
642 /// otherwise undefined behaviour may occur.
643 pub unsafe fn register_buf_ring_with_flags(
644 &self,
645 ring_addr: u64,
646 ring_entries: u16,
647 bgid: u16,
648 flags: u16,
649 ) -> io::Result<()> {
650 // The interface type for ring_entries is u32 but the same interface only allows a u16 for
651 // the tail to be specified, so to try and avoid further confusion, we limit the
652 // ring_entries to u16 here too. The value is actually limited to 2^15 (32768) but we can
653 // let the kernel enforce that.
654 let arg = sys::io_uring_buf_reg {
655 ring_addr,
656 ring_entries: ring_entries as _,
657 bgid,
658 flags,
659 ..Default::default()
660 };
661 execute(
662 self.fd.as_raw_fd(),
663 sys::IORING_REGISTER_PBUF_RING,
664 cast_ptr::<sys::io_uring_buf_reg>(&arg).cast(),
665 1,
666 )
667 .map(drop)
668 }
669
670 /// Unregister a previously registered buffer ring.
671 ///
672 /// Available since 5.19.
673 pub fn unregister_buf_ring(&self, bgid: u16) -> io::Result<()> {
674 let arg = sys::io_uring_buf_reg {
675 ring_addr: 0,
676 ring_entries: 0,
677 bgid,
678 ..Default::default()
679 };
680 execute(
681 self.fd.as_raw_fd(),
682 sys::IORING_UNREGISTER_PBUF_RING,
683 cast_ptr::<sys::io_uring_buf_reg>(&arg).cast(),
684 1,
685 )
686 .map(drop)
687 }
688
689 /// Performs a synchronous cancellation request, similar to [AsyncCancel](crate::opcode::AsyncCancel),
690 /// except that it completes synchronously.
691 ///
692 /// Cancellation can target a specific request, or all requests matching some criteria. The
693 /// [`CancelBuilder`] builder supports describing the match criteria for cancellation.
694 ///
695 /// An optional `timeout` can be provided to specify how long to wait for matched requests to be
696 /// canceled. If no timeout is provided, the default is to wait indefinitely.
697 ///
698 /// ### Errors
699 ///
700 /// If no requests are matched, returns:
701 ///
702 /// [io::ErrorKind::NotFound]: `No such file or directory (os error 2)`
703 ///
704 /// If a timeout is supplied, and the timeout elapses prior to all requests being canceled, returns:
705 ///
706 /// [io::ErrorKind::Uncategorized]: `Timer expired (os error 62)`
707 ///
708 /// ### Notes
709 ///
710 /// Only requests which have been submitted to the ring will be considered for cancellation. Requests
711 /// which have been written to the SQ, but not submitted, will not be canceled.
712 ///
713 /// Available since 6.0.
714 pub fn register_sync_cancel(
715 &self,
716 timeout: Option<Timespec>,
717 builder: CancelBuilder,
718 ) -> io::Result<()> {
719 let timespec = timeout.map(|ts| ts.0).unwrap_or(sys::__kernel_timespec {
720 tv_sec: -1,
721 tv_nsec: -1,
722 });
723 let user_data = builder.user_data.unwrap_or(0);
724 let flags = builder.flags.bits();
725 let fd = builder.to_fd();
726
727 let arg = sys::io_uring_sync_cancel_reg {
728 addr: user_data,
729 fd,
730 flags,
731 timeout: timespec,
732 ..Default::default()
733 };
734
735 execute(
736 self.fd.as_raw_fd(),
737 sys::IORING_REGISTER_SYNC_CANCEL,
738 cast_ptr::<sys::io_uring_sync_cancel_reg>(&arg).cast(),
739 1,
740 )
741 .map(drop)
742 }
743
744 /// Register a netdev hw rx queue for zerocopy.
745 ///
746 /// Available since 6.15.
747 pub fn register_ifq(&self, reg: &sys::io_uring_zcrx_ifq_reg) -> io::Result<()> {
748 execute(
749 self.fd.as_raw_fd(),
750 sys::IORING_REGISTER_ZCRX_IFQ,
751 cast_ptr::<sys::io_uring_zcrx_ifq_reg>(reg) as _,
752 1,
753 )
754 .map(drop)
755 }
756}