io_uring/
lib.rs

1//! The `io_uring` library for Rust.
2//!
3//! The crate only provides a summary of the parameters.
4//! For more detailed documentation, see manpage.
5//!
6//! ## Supported Architectures
7//!
8//! ### Default Support
9//!
10//! The following architectures are supported by default with prebuilt bindings:
11//!
12//! * `x86_64`
13//! * `aarch64`
14//! * `riscv64`
15//! * `loongarch64`
16//! * `powerpc64`
17//!
18//! ### Custom Bindings for Unsupported Architectures
19//!
20//! If you need to build for a target architecture that is not supported by default, you have two options:
21//!
22//! **Option 1: Use the `bindgen` feature**
23//!
24//! ```toml
25//! [dependencies]
26//! io-uring = { version = "0.7", features = ["bindgen"] }
27//! ```
28//!
29//! This will generate bindings at build time for your specific architecture.
30//!
31//! **Option 2: Use your own bindings with `io_uring_use_own_sys`**
32//!
33//! If you have custom bindings that you want to use:
34//!
35//! 1. Generate or obtain the appropriate `sys.rs` bindings for your target architecture
36//! 2. Set the `IO_URING_OWN_SYS_BINDING` environment variable to point to your binding file
37//! 3. Build with the `io_uring_use_own_sys` cfg flag:
38//!
39//! ```bash
40//! export IO_URING_OWN_SYS_BINDING=/path/to/your/custom/sys.rs
41//! cargo build --cfg io_uring_use_own_sys
42//! ```
43//!
44//! This approach allows you to provide your own bindings without relying on bindgen or the prebuilt bindings.
45
46#[macro_use]
47mod util;
48pub mod cqueue;
49pub mod opcode;
50pub mod register;
51pub mod squeue;
52mod submit;
53mod sys;
54pub mod types;
55
56use std::marker::PhantomData;
57use std::mem::ManuallyDrop;
58use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
59use std::{cmp, io, mem};
60
61#[cfg(feature = "io_safety")]
62use std::os::unix::io::{AsFd, BorrowedFd};
63
64pub use cqueue::CompletionQueue;
65pub use register::Probe;
66pub use squeue::SubmissionQueue;
67pub use submit::EnterFlags;
68pub use submit::Submitter;
69use util::{Mmap, OwnedFd};
70
71/// IoUring instance
72///
73/// - `S`: The ring's submission queue entry (SQE) type, either [`squeue::Entry`] or
74///   [`squeue::Entry128`];
75/// - `C`: The ring's completion queue entry (CQE) type, either [`cqueue::Entry`] or
76///   [`cqueue::Entry32`].
77pub struct IoUring<S = squeue::Entry, C = cqueue::Entry>
78where
79    S: squeue::EntryMarker,
80    C: cqueue::EntryMarker,
81{
82    sq: squeue::Inner<S>,
83    cq: cqueue::Inner<C>,
84    fd: OwnedFd,
85    params: Parameters,
86    memory: ManuallyDrop<MemoryMap>,
87}
88
89#[allow(dead_code)]
90struct MemoryMap {
91    sq_mmap: Mmap,
92    sqe_mmap: Mmap,
93    cq_mmap: Option<Mmap>,
94}
95
96/// IoUring build params
97#[derive(Clone, Default)]
98pub struct Builder<S = squeue::Entry, C = cqueue::Entry>
99where
100    S: squeue::EntryMarker,
101    C: cqueue::EntryMarker,
102{
103    dontfork: bool,
104    params: sys::io_uring_params,
105    phantom: PhantomData<(S, C)>,
106}
107
108/// The parameters that were used to construct an [`IoUring`].
109///
110/// This type is a transparent wrapper over the system structure `io_uring_params`. A value can be
111/// (unsafely) created from any properly laid-out and initialized memory representation.
112#[derive(Clone)]
113#[repr(transparent)]
114pub struct Parameters(sys::io_uring_params);
115
116unsafe impl<S: squeue::EntryMarker, C: cqueue::EntryMarker> Send for IoUring<S, C> {}
117unsafe impl<S: squeue::EntryMarker, C: cqueue::EntryMarker> Sync for IoUring<S, C> {}
118
119impl IoUring<squeue::Entry, cqueue::Entry> {
120    /// Create a new `IoUring` instance with default configuration parameters. See [`Builder`] to
121    /// customize it further.
122    ///
123    /// The `entries` sets the size of queue,
124    /// and its value should be the power of two.
125    pub fn new(entries: u32) -> io::Result<Self> {
126        Self::builder().build(entries)
127    }
128
129    /// Create an `IoUring` instance from a pre-opened file descriptor.
130    ///
131    /// # Safety
132    ///
133    /// The caller must uphold that the file descriptor is owned and refers to a uring. The
134    /// `params` argument must be equivalent to the those previously filled in by the kernel when
135    /// the provided ring was created.
136    pub unsafe fn from_fd(fd: RawFd, params: Parameters) -> io::Result<Self> {
137        Self::with_fd_and_params(OwnedFd::from_raw_fd(fd), params.0)
138    }
139}
140
141impl<S: squeue::EntryMarker, C: cqueue::EntryMarker> IoUring<S, C> {
142    /// Create a [`Builder`] for an `IoUring` instance.
143    ///
144    /// This allows for further customization than [`new`](Self::new).
145    ///
146    /// Unlike [`IoUring::new`], this function is available for any combination of submission
147    /// queue entry (SQE) and completion queue entry (CQE) types.
148    #[must_use]
149    pub fn builder() -> Builder<S, C> {
150        Builder {
151            dontfork: false,
152            params: sys::io_uring_params {
153                flags: S::BUILD_FLAGS | C::BUILD_FLAGS,
154                ..Default::default()
155            },
156            phantom: PhantomData,
157        }
158    }
159
160    fn with_params(entries: u32, mut p: sys::io_uring_params) -> io::Result<Self> {
161        let fd: OwnedFd = unsafe { OwnedFd::from_raw_fd(sys::io_uring_setup(entries, &mut p)?) };
162        unsafe { Self::with_fd_and_params(fd, p) }
163    }
164
165    unsafe fn with_fd_and_params(fd: OwnedFd, p: sys::io_uring_params) -> io::Result<Self> {
166        // NOTE: The `SubmissionQueue` and `CompletionQueue` are references,
167        // and their lifetime can never exceed `MemoryMap`.
168        //
169        // The memory mapped regions of `MemoryMap` never move,
170        // so `SubmissionQueue` and `CompletionQueue` are `Unpin`.
171        //
172        // I really hope that Rust can safely use self-reference types.
173        #[inline]
174        unsafe fn setup_queue<S: squeue::EntryMarker, C: cqueue::EntryMarker>(
175            fd: &OwnedFd,
176            p: &sys::io_uring_params,
177        ) -> io::Result<(MemoryMap, squeue::Inner<S>, cqueue::Inner<C>)> {
178            let sq_len = p.sq_off.array as usize + p.sq_entries as usize * mem::size_of::<u32>();
179            let cq_len = p.cq_off.cqes as usize + p.cq_entries as usize * mem::size_of::<C>();
180            let sqe_len = p.sq_entries as usize * mem::size_of::<S>();
181            let sqe_mmap = Mmap::new(fd, sys::IORING_OFF_SQES as _, sqe_len)?;
182
183            if p.features & sys::IORING_FEAT_SINGLE_MMAP != 0 {
184                let scq_mmap =
185                    Mmap::new(fd, sys::IORING_OFF_SQ_RING as _, cmp::max(sq_len, cq_len))?;
186
187                let sq = squeue::Inner::new(&scq_mmap, &sqe_mmap, p);
188                let cq = cqueue::Inner::new(&scq_mmap, p);
189                let mm = MemoryMap {
190                    sq_mmap: scq_mmap,
191                    cq_mmap: None,
192                    sqe_mmap,
193                };
194
195                Ok((mm, sq, cq))
196            } else {
197                let sq_mmap = Mmap::new(fd, sys::IORING_OFF_SQ_RING as _, sq_len)?;
198                let cq_mmap = Mmap::new(fd, sys::IORING_OFF_CQ_RING as _, cq_len)?;
199
200                let sq = squeue::Inner::new(&sq_mmap, &sqe_mmap, p);
201                let cq = cqueue::Inner::new(&cq_mmap, p);
202                let mm = MemoryMap {
203                    cq_mmap: Some(cq_mmap),
204                    sq_mmap,
205                    sqe_mmap,
206                };
207
208                Ok((mm, sq, cq))
209            }
210        }
211
212        let (mm, sq, cq) = unsafe { setup_queue(&fd, &p)? };
213
214        Ok(IoUring {
215            sq,
216            cq,
217            fd,
218            params: Parameters(p),
219            memory: ManuallyDrop::new(mm),
220        })
221    }
222
223    /// Get the submitter of this io_uring instance, which can be used to submit submission queue
224    /// events to the kernel for execution and to register files or buffers with it.
225    #[inline]
226    pub fn submitter(&self) -> Submitter<'_> {
227        Submitter::new(
228            &self.fd,
229            &self.params,
230            self.sq.head,
231            self.sq.tail,
232            self.sq.flags,
233        )
234    }
235
236    /// Get the parameters that were used to construct this instance.
237    #[inline]
238    pub fn params(&self) -> &Parameters {
239        &self.params
240    }
241
242    /// Initiate asynchronous I/O. See [`Submitter::submit`] for more details.
243    #[inline]
244    pub fn submit(&self) -> io::Result<usize> {
245        self.submitter().submit()
246    }
247
248    /// Initiate and/or complete asynchronous I/O. See [`Submitter::submit_and_wait`] for more
249    /// details.
250    #[inline]
251    pub fn submit_and_wait(&self, want: usize) -> io::Result<usize> {
252        self.submitter().submit_and_wait(want)
253    }
254
255    /// Get the submitter, submission queue and completion queue of the io_uring instance. This can
256    /// be used to operate on the different parts of the io_uring instance independently.
257    ///
258    /// If you use this method to obtain `sq` and `cq`,
259    /// please note that you need to `drop` or `sync` the queue before and after submit,
260    /// otherwise the queue will not be updated.
261    #[inline]
262    pub fn split(
263        &mut self,
264    ) -> (
265        Submitter<'_>,
266        SubmissionQueue<'_, S>,
267        CompletionQueue<'_, C>,
268    ) {
269        let submit = Submitter::new(
270            &self.fd,
271            &self.params,
272            self.sq.head,
273            self.sq.tail,
274            self.sq.flags,
275        );
276        (submit, self.sq.borrow(), self.cq.borrow())
277    }
278
279    /// Get the submission queue of the io_uring instance. This is used to send I/O requests to the
280    /// kernel.
281    #[inline]
282    pub fn submission(&mut self) -> SubmissionQueue<'_, S> {
283        self.sq.borrow()
284    }
285
286    /// Get the submission queue of the io_uring instance from a shared reference.
287    ///
288    /// # Safety
289    ///
290    /// No other [`SubmissionQueue`]s may exist when calling this function.
291    #[inline]
292    pub unsafe fn submission_shared(&self) -> SubmissionQueue<'_, S> {
293        self.sq.borrow_shared()
294    }
295
296    /// Get completion queue of the io_uring instance. This is used to receive I/O completion
297    /// events from the kernel.
298    #[inline]
299    pub fn completion(&mut self) -> CompletionQueue<'_, C> {
300        self.cq.borrow()
301    }
302
303    /// Get the completion queue of the io_uring instance from a shared reference.
304    ///
305    /// # Safety
306    ///
307    /// No other [`CompletionQueue`]s may exist when calling this function.
308    #[inline]
309    pub unsafe fn completion_shared(&self) -> CompletionQueue<'_, C> {
310        self.cq.borrow_shared()
311    }
312}
313
314impl<S: squeue::EntryMarker, C: cqueue::EntryMarker> Drop for IoUring<S, C> {
315    fn drop(&mut self) {
316        // Ensure that `MemoryMap` is released before `fd`.
317        unsafe {
318            ManuallyDrop::drop(&mut self.memory);
319        }
320    }
321}
322
323impl<S: squeue::EntryMarker, C: cqueue::EntryMarker> Builder<S, C> {
324    /// Do not make this io_uring instance accessible by child processes after a fork.
325    pub fn dontfork(&mut self) -> &mut Self {
326        self.dontfork = true;
327        self
328    }
329
330    /// Perform busy-waiting for I/O completion events, as opposed to getting notifications via an
331    /// asynchronous IRQ (Interrupt Request). This will reduce latency, but increases CPU usage.
332    ///
333    /// This is only usable on file systems that support polling and files opened with `O_DIRECT`.
334    pub fn setup_iopoll(&mut self) -> &mut Self {
335        self.params.flags |= sys::IORING_SETUP_IOPOLL;
336        self
337    }
338
339    /// Use a kernel thread to perform submission queue polling. This allows your application to
340    /// issue I/O without ever context switching into the kernel, however it does use up a lot more
341    /// CPU. You should use it when you are expecting very large amounts of I/O.
342    ///
343    /// After `idle` milliseconds, the kernel thread will go to sleep and you will have to wake it up
344    /// again with a system call (this is handled by [`Submitter::submit`] and
345    /// [`Submitter::submit_and_wait`] automatically).
346    ///
347    /// Before version 5.11 of the Linux kernel, to successfully use this feature, the application
348    /// must register a set of files to be used for IO through io_uring_register(2) using the
349    /// IORING_REGISTER_FILES opcode. Failure to do so will result in submitted IO being errored
350    /// with EBADF. The presence of this feature can be detected by the IORING_FEAT_SQPOLL_NONFIXED
351    /// feature flag. In version 5.11 and later, it is no longer necessary to register files to use
352    /// this feature. 5.11 also allows using this as non-root, if the user has the CAP_SYS_NICE
353    /// capability. In 5.13 this requirement was also relaxed, and no special privileges are needed
354    /// for SQPOLL in newer kernels. Certain stable kernels older than 5.13 may also support
355    /// unprivileged SQPOLL.
356    pub fn setup_sqpoll(&mut self, idle: u32) -> &mut Self {
357        self.params.flags |= sys::IORING_SETUP_SQPOLL;
358        self.params.sq_thread_idle = idle;
359        self
360    }
361
362    /// Bind the kernel's poll thread to the specified cpu. This flag is only meaningful when
363    /// [`Builder::setup_sqpoll`] is enabled.
364    pub fn setup_sqpoll_cpu(&mut self, cpu: u32) -> &mut Self {
365        self.params.flags |= sys::IORING_SETUP_SQ_AFF;
366        self.params.sq_thread_cpu = cpu;
367        self
368    }
369
370    /// Create the completion queue with the specified number of entries. The value must be greater
371    /// than `entries`, and may be rounded up to the next power-of-two.
372    pub fn setup_cqsize(&mut self, entries: u32) -> &mut Self {
373        self.params.flags |= sys::IORING_SETUP_CQSIZE;
374        self.params.cq_entries = entries;
375        self
376    }
377
378    /// Clamp the sizes of the submission queue and completion queue at their maximum values instead
379    /// of returning an error when you attempt to resize them beyond their maximum values.
380    pub fn setup_clamp(&mut self) -> &mut Self {
381        self.params.flags |= sys::IORING_SETUP_CLAMP;
382        self
383    }
384
385    /// Share the asynchronous worker thread backend of this io_uring with the specified io_uring
386    /// file descriptor instead of creating a new thread pool.
387    pub fn setup_attach_wq(&mut self, fd: RawFd) -> &mut Self {
388        self.params.flags |= sys::IORING_SETUP_ATTACH_WQ;
389        self.params.wq_fd = fd as _;
390        self
391    }
392
393    /// Start the io_uring instance with all its rings disabled. This allows you to register
394    /// restrictions, buffers and files before the kernel starts processing submission queue
395    /// events. You are only able to [register restrictions](Submitter::register_restrictions) when
396    /// the rings are disabled due to concurrency issues. You can enable the rings with
397    /// [`Submitter::register_enable_rings`]. Available since 5.10.
398    pub fn setup_r_disabled(&mut self) -> &mut Self {
399        self.params.flags |= sys::IORING_SETUP_R_DISABLED;
400        self
401    }
402
403    /// Normally io_uring stops submitting a batch of request, if one of these requests results in
404    /// an error. This can cause submission of less than what is expected, if a request ends in
405    /// error while being submitted. If the ring is created with this flag, io_uring_enter(2) will
406    /// continue submitting requests even if it encounters an error submitting a request. CQEs are
407    /// still posted for errored request regardless of whether or not this flag is set at ring
408    /// creation time, the only difference is if the submit sequence is halted or continued when an
409    /// error is observed. Available since 5.18.
410    pub fn setup_submit_all(&mut self) -> &mut Self {
411        self.params.flags |= sys::IORING_SETUP_SUBMIT_ALL;
412        self
413    }
414
415    /// By default, io_uring will interrupt a task running in userspace when a completion event
416    /// comes in. This is to ensure that completions run in a timely manner. For a lot of use
417    /// cases, this is overkill and can cause reduced performance from both the inter-processor
418    /// interrupt used to do this, the kernel/user transition, the needless interruption of the
419    /// tasks userspace activities, and reduced batching if completions come in at a rapid rate.
420    /// Most applications don't need the forceful interruption, as the events are processed at any
421    /// kernel/user transition. The exception are setups where the application uses multiple
422    /// threads operating on the same ring, where the application waiting on completions isn't the
423    /// one that submitted them. For most other use cases, setting this flag will improve
424    /// performance. Available since 5.19.
425    pub fn setup_coop_taskrun(&mut self) -> &mut Self {
426        self.params.flags |= sys::IORING_SETUP_COOP_TASKRUN;
427        self
428    }
429
430    /// Used in conjunction with IORING_SETUP_COOP_TASKRUN, this provides a flag,
431    /// IORING_SQ_TASKRUN, which is set in the SQ ring flags whenever completions are pending that
432    /// should be processed. As an example, liburing will check for this flag even when doing
433    /// io_uring_peek_cqe(3) and enter the kernel to process them, and applications can do the
434    /// same. This makes IORING_SETUP_TASKRUN_FLAG safe to use even when applications rely on a
435    /// peek style operation on the CQ ring to see if anything might be pending to reap. Available
436    /// since 5.19.
437    pub fn setup_taskrun_flag(&mut self) -> &mut Self {
438        self.params.flags |= sys::IORING_SETUP_TASKRUN_FLAG;
439        self
440    }
441
442    /// By default, io_uring will process all outstanding work at the end of any system call or
443    /// thread interrupt. This can delay the application from making other progress. Setting this
444    /// flag will hint to io_uring that it should defer work until an io_uring_enter(2) call with
445    /// the IORING_ENTER_GETEVENTS flag set. This allows the application to request work to run
446    /// just just before it wants to process completions. This flag requires the
447    /// IORING_SETUP_SINGLE_ISSUER flag to be set, and also enforces that the call to
448    /// io_uring_enter(2) is called from the same thread that submitted requests. Note that if this
449    /// flag is set then it is the application's responsibility to periodically trigger work (for
450    /// example via any of the CQE waiting functions) or else completions may not be delivered.
451    /// Available since 6.1.
452    pub fn setup_defer_taskrun(&mut self) -> &mut Self {
453        self.params.flags |= sys::IORING_SETUP_DEFER_TASKRUN;
454        self
455    }
456
457    /// Hint the kernel that a single task will submit requests. Used for optimizations. This is
458    /// enforced by the kernel, and request that don't respect that will fail with -EEXIST.
459    /// If [`Builder::setup_sqpoll`] is enabled, the polling task is doing the submissions and multiple
460    /// userspace tasks can call [`Submitter::enter`] and higher level APIs. Available since 6.0.
461    pub fn setup_single_issuer(&mut self) -> &mut Self {
462        self.params.flags |= sys::IORING_SETUP_SINGLE_ISSUER;
463        self
464    }
465
466    /// Build an [IoUring], with the specified number of entries in the submission queue and
467    /// completion queue unless [`setup_cqsize`](Self::setup_cqsize) has been called.
468    pub fn build(&self, entries: u32) -> io::Result<IoUring<S, C>> {
469        let ring = IoUring::with_params(entries, self.params)?;
470
471        if self.dontfork {
472            ring.memory.sq_mmap.dontfork()?;
473            ring.memory.sqe_mmap.dontfork()?;
474            if let Some(cq_mmap) = ring.memory.cq_mmap.as_ref() {
475                cq_mmap.dontfork()?;
476            }
477        }
478
479        Ok(ring)
480    }
481}
482
483impl Parameters {
484    /// Whether a kernel thread is performing queue polling. Enabled with [`Builder::setup_sqpoll`].
485    pub fn is_setup_sqpoll(&self) -> bool {
486        self.0.flags & sys::IORING_SETUP_SQPOLL != 0
487    }
488
489    /// Whether waiting for completion events is done with a busy loop instead of using IRQs.
490    /// Enabled with [`Builder::setup_iopoll`].
491    pub fn is_setup_iopoll(&self) -> bool {
492        self.0.flags & sys::IORING_SETUP_IOPOLL != 0
493    }
494
495    /// Whether the single issuer hint is enabled. Enabled with [`Builder::setup_single_issuer`].
496    pub fn is_setup_single_issuer(&self) -> bool {
497        self.0.flags & sys::IORING_SETUP_SINGLE_ISSUER != 0
498    }
499
500    /// If this flag is set, the SQ and CQ rings were mapped with a single `mmap(2)` call. This
501    /// means that only two syscalls were used instead of three.
502    pub fn is_feature_single_mmap(&self) -> bool {
503        self.0.features & sys::IORING_FEAT_SINGLE_MMAP != 0
504    }
505
506    /// If this flag is set, io_uring supports never dropping completion events. If a completion
507    /// event occurs and the CQ ring is full, the kernel stores the event internally until such a
508    /// time that the CQ ring has room for more entries.
509    pub fn is_feature_nodrop(&self) -> bool {
510        self.0.features & sys::IORING_FEAT_NODROP != 0
511    }
512
513    /// If this flag is set, applications can be certain that any data for async offload has been
514    /// consumed when the kernel has consumed the SQE.
515    pub fn is_feature_submit_stable(&self) -> bool {
516        self.0.features & sys::IORING_FEAT_SUBMIT_STABLE != 0
517    }
518
519    /// If this flag is set, applications can specify offset == -1 with [`Readv`](opcode::Readv),
520    /// [`Writev`](opcode::Writev), [`ReadFixed`](opcode::ReadFixed),
521    /// [`WriteFixed`](opcode::WriteFixed), [`Read`](opcode::Read) and [`Write`](opcode::Write),
522    /// which behaves exactly like setting offset == -1 in `preadv2(2)` and `pwritev2(2)`: it’ll use
523    /// (and update) the current file position.
524    ///
525    /// This obviously comes with the caveat that if the application has multiple reads or writes in flight,
526    /// then the end result will not be as expected.
527    /// This is similar to threads sharing a file descriptor and doing IO using the current file position.
528    pub fn is_feature_rw_cur_pos(&self) -> bool {
529        self.0.features & sys::IORING_FEAT_RW_CUR_POS != 0
530    }
531
532    /// If this flag is set, then io_uring guarantees that both sync and async execution of
533    /// a request assumes the credentials of the task that called [`Submitter::enter`] to queue the requests.
534    /// If this flag isn’t set, then requests are issued with the credentials of the task that originally registered the io_uring.
535    /// If only one task is using a ring, then this flag doesn’t matter as the credentials will always be the same.
536    ///
537    /// Note that this is the default behavior, tasks can still register different personalities
538    /// through [`Submitter::register_personality`].
539    pub fn is_feature_cur_personality(&self) -> bool {
540        self.0.features & sys::IORING_FEAT_CUR_PERSONALITY != 0
541    }
542
543    /// Whether async pollable I/O is fast.
544    ///
545    /// See [the commit message that introduced
546    /// it](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=d7718a9d25a61442da8ee8aeeff6a0097f0ccfd6)
547    /// for more details.
548    ///
549    /// If this flag is set, then io_uring supports using an internal poll mechanism to drive
550    /// data/space readiness. This means that requests that cannot read or write data to a file no
551    /// longer need to be punted to an async thread for handling, instead they will begin operation
552    /// when the file is ready. This is similar to doing poll + read/write in userspace, but
553    /// eliminates the need to do so. If this flag is set, requests waiting on space/data consume a
554    /// lot less resources doing so as they are not blocking a thread. Available since kernel 5.7.
555    pub fn is_feature_fast_poll(&self) -> bool {
556        self.0.features & sys::IORING_FEAT_FAST_POLL != 0
557    }
558
559    /// Whether poll events are stored using 32 bits instead of 16. This allows the user to use
560    /// `EPOLLEXCLUSIVE`.
561    ///
562    /// If this flag is set, the IORING_OP_POLL_ADD command accepts the full 32-bit range of epoll
563    /// based flags. Most notably EPOLLEXCLUSIVE which allows exclusive (waking single waiters)
564    /// behavior. Available since kernel 5.9.
565    pub fn is_feature_poll_32bits(&self) -> bool {
566        self.0.features & sys::IORING_FEAT_POLL_32BITS != 0
567    }
568
569    /// If this flag is set, the IORING_SETUP_SQPOLL feature no longer requires the use of fixed
570    /// files. Any normal file descriptor can be used for IO commands without needing registration.
571    /// Available since kernel 5.11.
572    pub fn is_feature_sqpoll_nonfixed(&self) -> bool {
573        self.0.features & sys::IORING_FEAT_SQPOLL_NONFIXED != 0
574    }
575
576    /// If this flag is set, then the io_uring_enter(2) system call supports passing in an extended
577    /// argument instead of just the sigset_t of earlier kernels. This extended argument is of type
578    /// struct io_uring_getevents_arg and allows the caller to pass in both a sigset_t and a
579    /// timeout argument for waiting on events. The struct layout is as follows:
580    ///
581    /// // struct io_uring_getevents_arg {
582    /// //     __u64 sigmask;
583    /// //     __u32 sigmask_sz;
584    /// //     __u32 pad;
585    /// //     __u64 ts;
586    /// // };
587    ///
588    /// and a pointer to this struct must be passed in if IORING_ENTER_EXT_ARG is set in the flags
589    /// for the enter system call. Available since kernel 5.11.
590    pub fn is_feature_ext_arg(&self) -> bool {
591        self.0.features & sys::IORING_FEAT_EXT_ARG != 0
592    }
593
594    /// If this flag is set, io_uring is using native workers for its async helpers. Previous
595    /// kernels used kernel threads that assumed the identity of the original io_uring owning task,
596    /// but later kernels will actively create what looks more like regular process threads
597    /// instead. Available since kernel 5.12.
598    pub fn is_feature_native_workers(&self) -> bool {
599        self.0.features & sys::IORING_FEAT_NATIVE_WORKERS != 0
600    }
601
602    /// Whether the kernel supports tagging resources.
603    ///
604    /// If this flag is set, then io_uring supports a variety of features related to fixed files
605    /// and buffers. In particular, it indicates that registered buffers can be updated in-place,
606    /// whereas before the full set would have to be unregistered first. Available since kernel
607    /// 5.13.
608    pub fn is_feature_resource_tagging(&self) -> bool {
609        self.0.features & sys::IORING_FEAT_RSRC_TAGS != 0
610    }
611
612    /// Whether the kernel supports `IOSQE_CQE_SKIP_SUCCESS`.
613    ///
614    /// This feature allows skipping the generation of a CQE if a SQE executes normally. Available
615    /// since kernel 5.17.
616    pub fn is_feature_skip_cqe_on_success(&self) -> bool {
617        self.0.features & sys::IORING_FEAT_CQE_SKIP != 0
618    }
619
620    /// Whether the kernel supports deferred file assignment.
621    ///
622    /// If this flag is set, then io_uring supports sane assignment of files for SQEs that have
623    /// dependencies. For example, if a chain of SQEs are submitted with IOSQE_IO_LINK, then
624    /// kernels without this flag will prepare the file for each link upfront. If a previous link
625    /// opens a file with a known index, eg if direct descriptors are used with open or accept,
626    /// then file assignment needs to happen post execution of that SQE. If this flag is set, then
627    /// the kernel will defer file assignment until execution of a given request is started.
628    /// Available since kernel 5.17.
629    pub fn is_feature_linked_file(&self) -> bool {
630        self.0.features & sys::IORING_FEAT_LINKED_FILE != 0
631    }
632
633    /// Whether the kernel supports `IORING_RECVSEND_BUNDLE`.
634    ///
635    /// This feature allows sending and recieving multiple buffers as a single bundle. Available
636    /// since kernel 6.10.
637    pub fn is_feature_recvsend_bundle(&self) -> bool {
638        self.0.features & sys::IORING_FEAT_RECVSEND_BUNDLE != 0
639    }
640
641    /// If this flag is set, applications can use the
642    /// [`SubmitArgs::min_wait_usec`](types::SubmitArgs::min_wait_usec) method to specify a timeout
643    /// after which the kernel will return as soon as a single completion is received instead of
644    /// waiting for the minimum specified by the application. Available since kernel 6.12.
645    pub fn is_feature_min_timeout(&self) -> bool {
646        self.0.features & sys::IORING_FEAT_MIN_TIMEOUT != 0
647    }
648
649    /// The number of submission queue entries allocated.
650    pub fn sq_entries(&self) -> u32 {
651        self.0.sq_entries
652    }
653
654    /// The idle time of the SQ poll thread in milliseconds.
655    pub fn sq_thread_idle(&self) -> u32 {
656        self.0.sq_thread_idle
657    }
658
659    /// The number of completion queue entries allocated.
660    pub fn cq_entries(&self) -> u32 {
661        self.0.cq_entries
662    }
663}
664
665impl std::fmt::Debug for Parameters {
666    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
667        f.debug_struct("Parameters")
668            .field("is_setup_sqpoll", &self.is_setup_sqpoll())
669            .field("is_setup_iopoll", &self.is_setup_iopoll())
670            .field("is_setup_single_issuer", &self.is_setup_single_issuer())
671            .field("is_feature_single_mmap", &self.is_feature_single_mmap())
672            .field("is_feature_nodrop", &self.is_feature_nodrop())
673            .field("is_feature_submit_stable", &self.is_feature_submit_stable())
674            .field("is_feature_rw_cur_pos", &self.is_feature_rw_cur_pos())
675            .field(
676                "is_feature_cur_personality",
677                &self.is_feature_cur_personality(),
678            )
679            .field("is_feature_poll_32bits", &self.is_feature_poll_32bits())
680            .field("sq_entries", &self.0.sq_entries)
681            .field("cq_entries", &self.0.cq_entries)
682            .finish()
683    }
684}
685
686impl<S: squeue::EntryMarker, C: cqueue::EntryMarker> AsRawFd for IoUring<S, C> {
687    fn as_raw_fd(&self) -> RawFd {
688        self.fd.as_raw_fd()
689    }
690}
691
692#[cfg(feature = "io_safety")]
693impl<S: squeue::EntryMarker, C: cqueue::EntryMarker> AsFd for IoUring<S, C> {
694    fn as_fd(&self) -> BorrowedFd<'_> {
695        self.fd.as_fd()
696    }
697}