oscall 0.1.1

Low-level cross-platform interface to operating system services: files, memory mapping, networking and more, with no libc on Linux
Documentation
//! io_uring on Linux and fullrust: the three system calls, the layouts the
//! kernel shares with the program, and the constants.
//!
//! Everything is from `include/uapi/linux/io_uring.h`. The structures have
//! the same layout on every architecture: their 64-bit members sit at
//! offsets that are multiples of eight whatever the alignment of `u64` is
//! (it is four on 32-bit x86), and the sizes are asserted below.

use core::ffi::c_void;

use crate::sys::syscall;
use crate::{OwnedHandle, Result};

/// `struct io_sqring_offsets`.
#[derive(Clone, Copy, Default)]
#[repr(C)]
pub(crate) struct SqOffsets {
    pub(crate) head: u32,
    pub(crate) tail: u32,
    pub(crate) ring_mask: u32,
    pub(crate) ring_entries: u32,
    pub(crate) flags: u32,
    pub(crate) dropped: u32,
    pub(crate) array: u32,
    pub(crate) resv1: u32,
    pub(crate) user_addr: u64,
}

/// `struct io_cqring_offsets`.
#[derive(Clone, Copy, Default)]
#[repr(C)]
pub(crate) struct CqOffsets {
    pub(crate) head: u32,
    pub(crate) tail: u32,
    pub(crate) ring_mask: u32,
    pub(crate) ring_entries: u32,
    pub(crate) overflow: u32,
    pub(crate) cqes: u32,
    pub(crate) flags: u32,
    pub(crate) resv1: u32,
    pub(crate) user_addr: u64,
}

/// `struct io_uring_params`.
#[derive(Clone, Copy, Default)]
#[repr(C)]
pub(crate) struct Params {
    pub(crate) sq_entries: u32,
    pub(crate) cq_entries: u32,
    pub(crate) flags: u32,
    pub(crate) sq_thread_cpu: u32,
    pub(crate) sq_thread_idle: u32,
    pub(crate) features: u32,
    pub(crate) wq_fd: u32,
    pub(crate) resv: [u32; 3],
    pub(crate) sq_off: SqOffsets,
    pub(crate) cq_off: CqOffsets,
}

/// `struct io_uring_getevents_arg`: what `io_uring_enter` takes with
/// `IORING_ENTER_EXT_ARG`.
#[repr(C)]
pub(crate) struct GeteventsArg {
    pub(crate) sigmask: u64,
    pub(crate) sigmask_sz: u32,
    pub(crate) min_wait_usec: u32,
    pub(crate) ts: u64,
}

/// `struct io_uring_probe_op`.
#[derive(Clone, Copy)]
#[repr(C)]
pub(crate) struct ProbeOp {
    pub(crate) op: u8,
    pub(crate) resv: u8,
    pub(crate) flags: u16,
    pub(crate) resv2: u32,
}

/// How many operations a probe has room for: an opcode is a byte.
pub(crate) const PROBE_OPS: usize = 256;

/// `struct io_uring_probe`, with room for every opcode there can be.
#[repr(C)]
pub(crate) struct Probe {
    pub(crate) last_op: u8,
    pub(crate) ops_len: u8,
    pub(crate) resv: u16,
    pub(crate) resv2: [u32; 3],
    pub(crate) ops: [ProbeOp; PROBE_OPS],
}

/// `struct io_uring_rsrc_update`: what `IORING_REGISTER_FILES_UPDATE` and
/// `IORING_REGISTER_RING_FDS` take.
#[repr(C)]
pub(crate) struct RsrcUpdate {
    pub(crate) offset: u32,
    pub(crate) resv: u32,
    pub(crate) data: u64,
}

/// `struct io_uring_rsrc_register`: what `IORING_REGISTER_FILES2` takes.
#[repr(C)]
pub(crate) struct RsrcRegister {
    pub(crate) nr: u32,
    pub(crate) flags: u32,
    pub(crate) resv2: u64,
    pub(crate) data: u64,
    pub(crate) tags: u64,
}

/// `struct io_uring_buf`: an entry of a ring of buffers. The last two
/// bytes of the first entry of a ring are the ring's tail (`struct
/// io_uring_buf_ring`), which is why `resv` is never written.
#[repr(C)]
pub(crate) struct Buf {
    pub(crate) addr: u64,
    pub(crate) len: u32,
    pub(crate) bid: u16,
    pub(crate) resv: u16,
}

/// Where the tail of a ring of buffers is, in bytes from its start.
pub(crate) const BUF_RING_TAIL: usize = 14;

/// `struct io_uring_buf_reg`: what `IORING_REGISTER_PBUF_RING` takes.
#[repr(C)]
pub(crate) struct BufReg {
    pub(crate) ring_addr: u64,
    pub(crate) ring_entries: u32,
    pub(crate) bgid: u16,
    pub(crate) flags: u16,
    pub(crate) resv: [u64; 3],
}

const _: () = {
    assert!(size_of::<RsrcUpdate>() == 16);
    assert!(size_of::<RsrcRegister>() == 32);
    assert!(size_of::<Buf>() == 16);
    assert!(core::mem::offset_of!(Buf, resv) == BUF_RING_TAIL);
    assert!(size_of::<BufReg>() == 40);
    assert!(size_of::<SqOffsets>() == 40);
    assert!(size_of::<CqOffsets>() == 40);
    assert!(size_of::<Params>() == 120);
    assert!(size_of::<GeteventsArg>() == 24);
    assert!(size_of::<ProbeOp>() == 8);
    assert!(size_of::<Probe>() == 16 + 8 * PROBE_OPS);
};

pub(crate) const IORING_SETUP_SQPOLL: u32 = 1 << 1;
pub(crate) const IORING_SETUP_SQ_AFF: u32 = 1 << 2;
pub(crate) const IORING_SETUP_CQSIZE: u32 = 1 << 3;
pub(crate) const IORING_SETUP_CLAMP: u32 = 1 << 4;
pub(crate) const IORING_SETUP_ATTACH_WQ: u32 = 1 << 5;
pub(crate) const IORING_SETUP_R_DISABLED: u32 = 1 << 6;
pub(crate) const IORING_SETUP_SUBMIT_ALL: u32 = 1 << 7;
pub(crate) const IORING_SETUP_COOP_TASKRUN: u32 = 1 << 8;
pub(crate) const IORING_SETUP_SQE128: u32 = 1 << 10;
pub(crate) const IORING_SETUP_CQE32: u32 = 1 << 11;
pub(crate) const IORING_SETUP_SINGLE_ISSUER: u32 = 1 << 12;
pub(crate) const IORING_SETUP_DEFER_TASKRUN: u32 = 1 << 13;

pub(crate) const IORING_FEAT_SINGLE_MMAP: u32 = 1 << 0;

pub(crate) const IORING_OFF_SQ_RING: u64 = 0;
pub(crate) const IORING_OFF_CQ_RING: u64 = 0x800_0000;
pub(crate) const IORING_OFF_SQES: u64 = 0x1000_0000;

pub(crate) const IORING_SQ_NEED_WAKEUP: u32 = 1 << 0;
pub(crate) const IORING_SQ_CQ_OVERFLOW: u32 = 1 << 1;

pub(crate) const IORING_ENTER_GETEVENTS: u32 = 1 << 0;
pub(crate) const IORING_ENTER_SQ_WAKEUP: u32 = 1 << 1;
pub(crate) const IORING_ENTER_SQ_WAIT: u32 = 1 << 2;
pub(crate) const IORING_ENTER_EXT_ARG: u32 = 1 << 3;
pub(crate) const IORING_ENTER_REGISTERED_RING: u32 = 1 << 4;

pub(crate) const IORING_REGISTER_BUFFERS: u32 = 0;
pub(crate) const IORING_UNREGISTER_BUFFERS: u32 = 1;
pub(crate) const IORING_REGISTER_FILES: u32 = 2;
pub(crate) const IORING_UNREGISTER_FILES: u32 = 3;
pub(crate) const IORING_REGISTER_EVENTFD: u32 = 4;
pub(crate) const IORING_UNREGISTER_EVENTFD: u32 = 5;
pub(crate) const IORING_REGISTER_FILES_UPDATE: u32 = 6;
pub(crate) const IORING_REGISTER_PROBE: u32 = 8;
pub(crate) const IORING_REGISTER_RESTRICTIONS: u32 = 11;
pub(crate) const IORING_REGISTER_ENABLE_RINGS: u32 = 12;
pub(crate) const IORING_REGISTER_FILES2: u32 = 13;
pub(crate) const IORING_REGISTER_IOWQ_AFF: u32 = 17;
pub(crate) const IORING_UNREGISTER_IOWQ_AFF: u32 = 18;
pub(crate) const IORING_REGISTER_IOWQ_MAX_WORKERS: u32 = 19;
pub(crate) const IORING_REGISTER_RING_FDS: u32 = 20;
pub(crate) const IORING_UNREGISTER_RING_FDS: u32 = 21;
pub(crate) const IORING_REGISTER_PBUF_RING: u32 = 22;
pub(crate) const IORING_UNREGISTER_PBUF_RING: u32 = 23;

pub(crate) const IORING_RSRC_REGISTER_SPARSE: u32 = 1 << 0;

pub(crate) const IORING_RESTRICTION_REGISTER_OP: u16 = 0;
pub(crate) const IORING_RESTRICTION_SQE_OP: u16 = 1;
pub(crate) const IORING_RESTRICTION_SQE_FLAGS_ALLOWED: u16 = 2;
pub(crate) const IORING_RESTRICTION_SQE_FLAGS_REQUIRED: u16 = 3;

pub(crate) const IO_URING_OP_SUPPORTED: u16 = 1 << 0;

pub(crate) const IOSQE_FIXED_FILE: u8 = 1 << 0;
pub(crate) const IOSQE_BUFFER_SELECT: u8 = 1 << 5;

pub(crate) const IORING_CQE_F_BUFFER: u32 = 1 << 0;
pub(crate) const IORING_CQE_F_MORE: u32 = 1 << 1;
pub(crate) const IORING_CQE_F_SOCK_NONEMPTY: u32 = 1 << 2;
pub(crate) const IORING_CQE_F_NOTIF: u32 = 1 << 3;
pub(crate) const IORING_CQE_BUFFER_SHIFT: u32 = 16;

pub(crate) const IORING_FSYNC_DATASYNC: u32 = 1 << 0;
pub(crate) const IORING_TIMEOUT_ABS: u32 = 1 << 0;
pub(crate) const IORING_TIMEOUT_MULTISHOT: u32 = 1 << 6;
pub(crate) const IORING_POLL_ADD_MULTI: u32 = 1 << 0;
pub(crate) const IORING_ASYNC_CANCEL_ALL: u32 = 1 << 0;
pub(crate) const IORING_ASYNC_CANCEL_FD: u32 = 1 << 1;
pub(crate) const IORING_ASYNC_CANCEL_ANY: u32 = 1 << 2;
pub(crate) const IORING_RECV_MULTISHOT: u16 = 1 << 1;
pub(crate) const IORING_ACCEPT_MULTISHOT: u16 = 1 << 0;

/// `FUTEX2_SIZE_U32 | FUTEX2_PRIVATE` (`include/uapi/linux/futex.h`): a
/// 32-bit futex of this process.
pub(crate) const FUTEX2_U32_PRIVATE: i32 = 0x02 | 128;
/// `FUTEX_BITSET_MATCH_ANY`.
pub(crate) const FUTEX_BITSET_MATCH_ANY: u64 = 0xffff_ffff;

/// The size of the kernel's signal set, which `io_uring_enter` wants to be
/// told even when it is given no set.
const SIGSET_SIZE: usize = 8;

/// `io_uring_setup`: a ring of `entries` submissions, as `params` asks; the
/// kernel fills in the rest of `params`. The descriptor is close-on-exec
/// (the kernel makes no other kind).
pub(crate) fn setup(entries: u32, params: &mut Params) -> Result<OwnedHandle> {
    // SAFETY: `params` is the structure the call reads and writes.
    let fd = unsafe { syscall!(IO_URING_SETUP, entries, params as *mut Params) }?;
    // SAFETY: the descriptor was just created.
    Ok(unsafe { OwnedHandle::from_raw(fd as i32) })
}

/// `io_uring_enter`.
///
/// # Safety
///
/// `arg` must be what `flags` says: null, or with `IORING_ENTER_EXT_ARG` a
/// [`GeteventsArg`] that points to a valid timeout or to none. The
/// submissions the kernel takes from the ring must be the caller's to
/// make: see `io::linux::Sqe`.
pub(crate) unsafe fn enter(
    fd: i32,
    to_submit: u32,
    min_complete: u32,
    flags: u32,
    arg: *const GeteventsArg,
) -> Result<usize> {
    let size = if arg.is_null() {
        SIGSET_SIZE
    } else {
        size_of::<GeteventsArg>()
    };
    // SAFETY: the caller's contract.
    unsafe {
        syscall!(
            IO_URING_ENTER,
            fd,
            to_submit,
            min_complete,
            flags,
            arg,
            size
        )
    }
}

/// `io_uring_register`.
///
/// # Safety
///
/// `arg` must be what `opcode` takes, `count` of them.
pub(crate) unsafe fn register(
    fd: i32,
    opcode: u32,
    arg: *const c_void,
    count: u32,
) -> Result<usize> {
    // SAFETY: the caller's contract.
    unsafe { syscall!(IO_URING_REGISTER, fd, opcode, arg, count) }
}