#![allow(clippy::new_without_default)]
use crate::squeue::Entry;
use crate::sys;
pub mod types {
use std::os::unix::io::RawFd;
use bitflags::bitflags;
use crate::sys;
pub use sys::__kernel_timespec as Timespec;
pub use sys::__kernel_rwf_t as RwFlags;
#[derive(Debug, Clone, Copy)]
pub enum Target {
Fd(RawFd),
Fixed(u32)
}
bitflags!{
pub struct TimeoutFlags: u32 {
const ABS = sys::IORING_TIMEOUT_ABS;
}
}
bitflags!{
pub struct FsyncFlags: u32 {
const DATASYNC = sys::IORING_FSYNC_DATASYNC;
}
}
}
macro_rules! assign_fd {
( $sqe:ident . fd = $opfd:expr ) => {
match $opfd {
types::Target::Fd(fd) => $sqe.fd = fd,
types::Target::Fixed(i) => {
$sqe.fd = i as _;
$sqe.flags |= crate::squeue::Flags::FIXED_FILE.bits();
}
}
}
}
macro_rules! opcode {
(
$( #[$outer:meta] )*
pub struct $name:ident {
$( #[$new_meta:meta] )*
$( $field:ident : $tname:ty ),* $(,)?
;;
$(
$( #[$opt_meta:meta] )*
$opt_field:ident : $opt_tname:ty = $default:expr
),* $(,)?
}
pub fn build($self:ident) -> Entry $build_block:block
) => {
$( #[$outer] )*
pub struct $name {
$( $field : $tname, )*
$( $opt_field : $opt_tname, )*
}
impl $name {
$( #[$new_meta] )*
pub const fn new( $( $field : $tname ),* ) -> Self {
$name {
$( $field , )*
$( $opt_field: $default, )*
}
}
$(
$( #[$opt_meta] )*
pub const fn $opt_field(mut self, $opt_field: $opt_tname) -> Self {
self.$opt_field = $opt_field;
self
}
)*
pub fn build($self) -> Entry $build_block
}
}
}
#[inline(always)]
fn sqe_zeroed() -> sys::io_uring_sqe {
unsafe { std::mem::zeroed() }
}
opcode!(
#[derive(Debug)]
pub struct Nop { ;; }
pub fn build(self) -> Entry {
let Nop {} = self;
let mut sqe = sqe_zeroed();
sqe.opcode = sys::IORING_OP_NOP as _;
sqe.fd = -1;
Entry(sqe)
}
);
opcode!(
#[derive(Debug)]
pub struct Readv {
fd: types::Target,
iovec: *mut libc::iovec,
len: u32,
;;
ioprio: u16 = 0,
offset: libc::off_t = 0,
rw_flags: types::RwFlags = 0
}
pub fn build(self) -> Entry {
let Readv {
fd,
iovec, len, offset,
ioprio, rw_flags
} = self;
let mut sqe = sqe_zeroed();
sqe.opcode = sys::IORING_OP_READV as _;
assign_fd!(sqe.fd = fd);
sqe.ioprio = ioprio;
sqe.addr = iovec as _;
sqe.len = len;
sqe.__bindgen_anon_1.off = offset as _;
sqe.__bindgen_anon_2.rw_flags = rw_flags;
Entry(sqe)
}
);
opcode!(
#[derive(Debug)]
pub struct Writev {
fd: types::Target,
iovec: *const libc::iovec,
len: u32,
;;
ioprio: u16 = 0,
offset: libc::off_t = 0,
rw_flags: types::RwFlags = 0
}
pub fn build(self) -> Entry {
let Writev {
fd,
iovec, len, offset,
ioprio, rw_flags
} = self;
let mut sqe = sqe_zeroed();
sqe.opcode = sys::IORING_OP_WRITEV as _;
assign_fd!(sqe.fd = fd);
sqe.ioprio = ioprio;
sqe.addr = iovec as _;
sqe.len = len;
sqe.__bindgen_anon_1.off = offset as _;
sqe.__bindgen_anon_2.rw_flags = rw_flags;
Entry(sqe)
}
);
opcode!(
#[derive(Debug)]
pub struct Fsync {
fd: types::Target,
;;
flags: types::FsyncFlags = types::FsyncFlags::empty()
}
pub fn build(self) -> Entry {
let Fsync { fd, flags } = self;
let mut sqe = sqe_zeroed();
sqe.opcode = sys::IORING_OP_FSYNC as _;
assign_fd!(sqe.fd = fd);
sqe.__bindgen_anon_2.fsync_flags = flags.bits();
Entry(sqe)
}
);
opcode!(
#[derive(Debug)]
pub struct ReadFixed {
fd: types::Target,
buf: *mut u8,
len: u32,
buf_index: u16,
;;
ioprio: u16 = 0,
offset: libc::off_t = 0,
rw_flags: types::RwFlags = 0
}
pub fn build(self) -> Entry {
let ReadFixed {
fd,
buf, len, offset,
buf_index,
ioprio, rw_flags
} = self;
let mut sqe = sqe_zeroed();
sqe.opcode = sys::IORING_OP_READ_FIXED as _;
assign_fd!(sqe.fd = fd);
sqe.ioprio = ioprio;
sqe.addr = buf as _;
sqe.len = len;
sqe.__bindgen_anon_1.off = offset as _;
sqe.__bindgen_anon_2.rw_flags = rw_flags;
sqe.__bindgen_anon_3.buf_index = buf_index;
Entry(sqe)
}
);
opcode!(
#[derive(Debug)]
pub struct WriteFixed {
fd: types::Target,
buf: *const u8,
len: u32,
buf_index: u16,
;;
ioprio: u16 = 0,
offset: libc::off_t = 0,
rw_flags: types::RwFlags = 0
}
pub fn build(self) -> Entry {
let WriteFixed {
fd,
buf, len, offset,
buf_index,
ioprio, rw_flags
} = self;
let mut sqe = sqe_zeroed();
sqe.opcode = sys::IORING_OP_WRITE_FIXED as _;
assign_fd!(sqe.fd = fd);
sqe.ioprio = ioprio;
sqe.addr = buf as _;
sqe.len = len;
sqe.__bindgen_anon_1.off = offset as _;
sqe.__bindgen_anon_2.rw_flags = rw_flags;
sqe.__bindgen_anon_3.buf_index = buf_index;
Entry(sqe)
}
);
opcode!(
#[derive(Debug)]
pub struct PollAdd {
fd: types::Target,
flags: libc::c_short,
;;
}
pub fn build(self) -> Entry {
let PollAdd { fd, flags } = self;
let mut sqe = sqe_zeroed();
sqe.opcode = sys::IORING_OP_POLL_ADD as _;
assign_fd!(sqe.fd = fd);
sqe.__bindgen_anon_2.poll_events = flags as _;
Entry(sqe)
}
);
opcode!(
#[derive(Debug)]
pub struct PollRemove {
user_data: u64
;;
}
pub fn build(self) -> Entry {
let PollRemove { user_data } = self;
let mut sqe = sqe_zeroed();
sqe.opcode = sys::IORING_OP_POLL_REMOVE as _;
sqe.fd = -1;
sqe.addr = user_data as _;
Entry(sqe)
}
);
opcode!(
#[derive(Debug)]
pub struct SyncFileRange {
fd: types::Target,
len: u32,
;;
offset: libc::off_t = 0,
flags: u32 = 0
}
pub fn build(self) -> Entry {
let SyncFileRange {
fd,
len, offset,
flags
} = self;
let mut sqe = sqe_zeroed();
sqe.opcode = sys::IORING_OP_SYNC_FILE_RANGE as _;
assign_fd!(sqe.fd = fd);
sqe.len = len;
sqe.__bindgen_anon_1.off = offset as _;
sqe.__bindgen_anon_2.sync_range_flags = flags;
Entry(sqe)
}
);
opcode!(
#[derive(Debug)]
pub struct SendMsg {
fd: types::Target,
msg: *const libc::msghdr,
;;
ioprio: u16 = 0,
flags: u32 = 0
}
pub fn build(self) -> Entry {
let SendMsg { fd, msg, ioprio, flags } = self;
let mut sqe = sqe_zeroed();
sqe.opcode = sys::IORING_OP_SENDMSG as _;
assign_fd!(sqe.fd = fd);
sqe.ioprio = ioprio;
sqe.addr = msg as _;
sqe.len = 1;
sqe.__bindgen_anon_2.msg_flags = flags;
Entry(sqe)
}
);
opcode!(
#[derive(Debug)]
pub struct RecvMsg {
fd: types::Target,
msg: *mut libc::msghdr,
;;
ioprio: u16 = 0,
flags: u32 = 0
}
pub fn build(self) -> Entry {
let RecvMsg { fd, msg, ioprio, flags } = self;
let mut sqe = sqe_zeroed();
sqe.opcode = sys::IORING_OP_RECVMSG as _;
assign_fd!(sqe.fd = fd);
sqe.ioprio = ioprio;
sqe.addr = msg as _;
sqe.len = 1;
sqe.__bindgen_anon_2.msg_flags = flags;
Entry(sqe)
}
);
opcode!(
#[derive(Debug)]
pub struct Timeout {
timespec: *const types::Timespec,
;;
count: u32 = 0,
flags: types::TimeoutFlags = types::TimeoutFlags::empty()
}
pub fn build(self) -> Entry {
let Timeout { timespec, count, flags } = self;
let mut sqe = sqe_zeroed();
sqe.opcode = sys::IORING_OP_TIMEOUT as _;
sqe.fd = -1;
sqe.addr = timespec as _;
sqe.len = 1;
sqe.__bindgen_anon_1.off = count as _;
sqe.__bindgen_anon_2.timeout_flags = flags.bits();
Entry(sqe)
}
);
opcode!(
pub struct TimeoutRemove {
user_data: u64,
;;
flags: types::TimeoutFlags = types::TimeoutFlags::empty()
}
pub fn build(self) -> Entry {
let TimeoutRemove { user_data, flags } = self;
let mut sqe = sqe_zeroed();
sqe.opcode = sys::IORING_OP_TIMEOUT_REMOVE as _;
sqe.fd = -1;
sqe.addr = user_data as _;
sqe.__bindgen_anon_2.timeout_flags = flags.bits();
Entry(sqe)
}
);
opcode!(
pub struct Accept {
fd: types::Target,
addr: *mut libc::sockaddr,
addrlen: *mut libc::socklen_t
;;
flags: u32 = 0
}
pub fn build(self) -> Entry {
let Accept { fd, addr, addrlen, flags } = self;
let mut sqe = sqe_zeroed();
sqe.opcode = sys::IORING_OP_ACCEPT as _;
assign_fd!(sqe.fd = fd);
sqe.addr = addr as _;
sqe.__bindgen_anon_1.addr2 = addrlen as _;
sqe.__bindgen_anon_2.accept_flags = flags;
Entry(sqe)
}
);
opcode!(
pub struct AsyncCancel {
user_data: u64,
;;
}
pub fn build(self) -> Entry {
let AsyncCancel { user_data } = self;
let mut sqe = sqe_zeroed();
sqe.opcode = sys::IORING_OP_ASYNC_CANCEL as _;
sqe.fd = -1;
sqe.addr = user_data as _;
Entry(sqe)
}
);
opcode!(
pub struct LinkTimeout {
timespec: *const types::Timespec,
;;
flags: types::TimeoutFlags = types::TimeoutFlags::empty()
}
pub fn build(self) -> Entry {
let LinkTimeout { timespec, flags } = self;
let mut sqe = sqe_zeroed();
sqe.opcode = sys::IORING_OP_LINK_TIMEOUT as _;
sqe.fd = -1;
sqe.addr = timespec as _;
sqe.len = 1;
sqe.__bindgen_anon_2.timeout_flags = flags.bits();
Entry(sqe)
}
);
opcode!(
pub struct Connect {
fd: types::Target,
addr: *const libc::sockaddr,
addrlen: libc::socklen_t
;;
}
pub fn build(self) -> Entry {
let Connect { fd, addr, addrlen } = self;
let mut sqe = sqe_zeroed();
sqe.opcode = sys::IORING_OP_CONNECT as _;
assign_fd!(sqe.fd = fd);
sqe.addr = addr as _;
sqe.__bindgen_anon_1.off = addrlen as _;
Entry(sqe)
}
);