Skip to content

Commit 59f7b71

Browse files
authored
Some io_uring additions (#546)
1 parent 8169194 commit 59f7b71

File tree

3 files changed

+106
-44
lines changed

3 files changed

+106
-44
lines changed

src/backend/libc/io_uring/syscalls.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! libc syscalls supporting `rustix::io_uring`.
22
33
use super::super::c;
4-
use super::super::conv::{borrowed_fd, syscall_ret, syscall_ret_owned_fd, syscall_ret_u32};
4+
use super::super::conv::{borrowed_fd, syscall_ret_owned_fd, syscall_ret_u32};
55
use crate::fd::{BorrowedFd, OwnedFd};
66
use crate::io;
77
use crate::io_uring::{io_uring_params, IoringEnterFlags, IoringRegisterOp};
@@ -24,8 +24,8 @@ pub(crate) unsafe fn io_uring_register(
2424
opcode: IoringRegisterOp,
2525
arg: *const c::c_void,
2626
nr_args: u32,
27-
) -> io::Result<()> {
28-
syscall_ret(c::syscall(
27+
) -> io::Result<u32> {
28+
syscall_ret_u32(c::syscall(
2929
__NR_io_uring_register as _,
3030
borrowed_fd(fd),
3131
opcode as u32 as usize,

src/backend/linux_raw/io_uring/syscalls.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#![allow(unsafe_code)]
77
#![allow(clippy::undocumented_unsafe_blocks)]
88

9-
use super::super::conv::{by_mut, c_uint, pass_usize, ret, ret_c_uint, ret_owned_fd};
9+
use super::super::conv::{by_mut, c_uint, pass_usize, ret_c_uint, ret_owned_fd};
1010
use crate::fd::{BorrowedFd, OwnedFd};
1111
use crate::io;
1212
use crate::io_uring::{io_uring_params, IoringEnterFlags, IoringRegisterOp};
@@ -29,8 +29,8 @@ pub(crate) unsafe fn io_uring_register(
2929
opcode: IoringRegisterOp,
3030
arg: *const c_void,
3131
nr_args: u32,
32-
) -> io::Result<()> {
33-
ret(syscall_readonly!(
32+
) -> io::Result<u32> {
33+
ret_c_uint(syscall_readonly!(
3434
__NR_io_uring_register,
3535
fd,
3636
c_uint(opcode as u32),

src/io_uring.rs

Lines changed: 100 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub unsafe fn io_uring_register<Fd: AsFd>(
5656
opcode: IoringRegisterOp,
5757
arg: *const c_void,
5858
nr_args: u32,
59-
) -> io::Result<()> {
59+
) -> io::Result<u32> {
6060
backend::io_uring::syscalls::io_uring_register(fd.as_fd(), opcode, arg, nr_args)
6161
}
6262

@@ -467,6 +467,12 @@ bitflags::bitflags! {
467467

468468
/// `IORING_CQE_F_MORE`
469469
const MORE = sys::IORING_CQE_F_MORE as _;
470+
471+
/// `IORING_CQE_F_SOCK_NONEMPTY`
472+
const SOCK_NONEMPTY = sys::IORING_CQE_F_SOCK_NONEMPTY as _;
473+
474+
/// `IORING_CQE_F_NOTIF`
475+
const NOTIF = sys::IORING_CQE_F_NOTIF as _;
470476
}
471477
}
472478

@@ -558,6 +564,9 @@ bitflags::bitflags! {
558564

559565
/// `IORING_FEAT_SUBMIT_STABLE`
560566
const SUBMIT_STABLE = sys::IORING_FEAT_SUBMIT_STABLE;
567+
568+
/// `IORING_FEAT_LINKED_FILE`
569+
const LINKED_FILE = sys::IORING_FEAT_LINKED_FILE;
561570
}
562571
}
563572

@@ -588,6 +597,9 @@ bitflags::bitflags! {
588597

589598
/// `IORING_SQ_CQ_OVERFLOW`
590599
const CQ_OVERFLOW = sys::IORING_SQ_CQ_OVERFLOW;
600+
601+
/// `IORING_SQ_TASKRUN`
602+
const TASKRUN = sys::IORING_SQ_TASKRUN;
591603
}
592604
}
593605

@@ -612,11 +624,61 @@ bitflags::bitflags! {
612624

613625
/// `IORING_POLL_UPDATE_USER_DATA`
614626
const UPDATE_USER_DATA = sys::IORING_POLL_UPDATE_USER_DATA;
627+
628+
/// `IORING_POLL_ADD_LEVEL`
629+
const ADD_LEVEL = sys::IORING_POLL_ADD_LEVEL;
630+
}
631+
}
632+
633+
bitflags::bitflags! {
634+
/// send/sendmsg & recv/recvmsg flags (`sqe.ioprio`)
635+
#[derive(Default)]
636+
pub struct IoringRecvSendFlags: u16 {
637+
/// `IORING_RECVSEND_POLL_FIRST`
638+
const POLL_FIRST = sys::IORING_RECVSEND_POLL_FIRST as _;
639+
640+
/// `IORING_RECV_MULTISHOT`
641+
const MULTISHOT = sys::IORING_RECV_MULTISHOT as _;
642+
643+
/// `IORING_RECVSEND_FIXED_BUF`
644+
const FIXED_BUF = sys::IORING_RECVSEND_FIXED_BUF as _;
645+
}
646+
}
647+
648+
bitflags::bitflags! {
649+
/// accept flags (`sqe.ioprio`)
650+
#[derive(Default)]
651+
pub struct IoringAcceptFlags: u16 {
652+
/// `IORING_ACCEPT_MULTISHOT`
653+
const MULTISHOT = sys::IORING_ACCEPT_MULTISHOT as _;
654+
}
655+
}
656+
657+
bitflags::bitflags! {
658+
/// recvmsg out flags
659+
#[derive(Default)]
660+
pub struct RecvmsgOutFlags: u32 {
661+
/// `MSG_EOR`
662+
const EOR = sys::MSG_EOR;
663+
664+
/// `MSG_TRUNC`
665+
const TRUNC = sys::MSG_TRUNC;
666+
667+
/// `MSG_CTRUNC`
668+
const CTRUNC = sys::MSG_CTRUNC;
669+
670+
/// `MSG_OOB`
671+
const OOB = sys::MSG_OOB;
672+
673+
/// `MSG_ERRQUEUE`
674+
const ERRQUEUE = sys::MSG_ERRQUEUE;
615675
}
616676
}
617677

618678
#[allow(missing_docs)]
619679
pub const IORING_CQE_BUFFER_SHIFT: u32 = sys::IORING_CQE_BUFFER_SHIFT as _;
680+
#[allow(missing_docs)]
681+
pub const IORING_FILE_INDEX_ALLOC: i32 = sys::IORING_FILE_INDEX_ALLOC as _;
620682

621683
// Re-export these as `u64`, which is the `offset` type in `rustix::io::mmap`.
622684
#[allow(missing_docs)]
@@ -757,7 +819,7 @@ impl core::fmt::Debug for io_uring_user_data {
757819
pub struct io_uring_sqe {
758820
pub opcode: IoringOp,
759821
pub flags: IoringSqeFlags,
760-
pub ioprio: u16,
822+
pub ioprio_or_flags: ioprio_or_flags_union,
761823
pub fd: RawFd,
762824
pub off_or_addr2: off_or_addr2_union,
763825
pub addr_or_splice_off_in: addr_or_splice_off_in_union,
@@ -770,6 +832,15 @@ pub struct io_uring_sqe {
770832
pub addr3_or_cmd: addr3_or_cmd_union,
771833
}
772834

835+
#[allow(missing_docs)]
836+
#[repr(C)]
837+
#[derive(Copy, Clone)]
838+
pub union ioprio_or_flags_union {
839+
pub recvsend: IoringRecvSendFlags,
840+
pub accept: IoringAcceptFlags,
841+
pub ioprio: u16,
842+
}
843+
773844
#[allow(missing_docs)]
774845
#[repr(C)]
775846
#[derive(Copy, Clone)]
@@ -1001,6 +1072,16 @@ pub struct io_uring_getevents_arg {
10011072
pub ts: u64,
10021073
}
10031074

1075+
#[allow(missing_docs)]
1076+
#[repr(C)]
1077+
#[derive(Debug, Default, Copy, Clone)]
1078+
pub struct io_uring_recvmsg_out {
1079+
pub namelen: u32,
1080+
pub controllen: u32,
1081+
pub payloadlen: u32,
1082+
pub flags: RecvmsgOutFlags,
1083+
}
1084+
10041085
#[allow(missing_docs)]
10051086
#[repr(C)]
10061087
#[derive(Debug, Copy, Clone)]
@@ -1047,87 +1128,67 @@ pub struct io_uring_buf {
10471128
pub resv: u16,
10481129
}
10491130

1131+
impl Default for ioprio_or_flags_union {
1132+
#[inline]
1133+
fn default() -> Self {
1134+
// Safety: All of Linux's io_uring structs may be zero-initialized.
1135+
unsafe { ::core::mem::zeroed::<Self>() }
1136+
}
1137+
}
1138+
10501139
impl Default for off_or_addr2_union {
10511140
#[inline]
10521141
fn default() -> Self {
1053-
let mut s = ::core::mem::MaybeUninit::<Self>::uninit();
10541142
// Safety: All of Linux's io_uring structs may be zero-initialized.
1055-
unsafe {
1056-
::core::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
1057-
s.assume_init()
1058-
}
1143+
unsafe { ::core::mem::zeroed::<Self>() }
10591144
}
10601145
}
10611146

10621147
impl Default for addr_or_splice_off_in_union {
10631148
#[inline]
10641149
fn default() -> Self {
1065-
let mut s = ::core::mem::MaybeUninit::<Self>::uninit();
10661150
// Safety: All of Linux's io_uring structs may be zero-initialized.
1067-
unsafe {
1068-
::core::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
1069-
s.assume_init()
1070-
}
1151+
unsafe { ::core::mem::zeroed::<Self>() }
10711152
}
10721153
}
10731154

10741155
impl Default for addr3_or_cmd_union {
10751156
#[inline]
10761157
fn default() -> Self {
1077-
let mut s = ::core::mem::MaybeUninit::<Self>::uninit();
10781158
// Safety: All of Linux's io_uring structs may be zero-initialized.
1079-
unsafe {
1080-
::core::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
1081-
s.assume_init()
1082-
}
1159+
unsafe { ::core::mem::zeroed::<Self>() }
10831160
}
10841161
}
10851162

10861163
impl Default for op_flags_union {
10871164
#[inline]
10881165
fn default() -> Self {
1089-
let mut s = ::core::mem::MaybeUninit::<Self>::uninit();
10901166
// Safety: All of Linux's io_uring structs may be zero-initialized.
1091-
unsafe {
1092-
::core::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
1093-
s.assume_init()
1094-
}
1167+
unsafe { ::core::mem::zeroed::<Self>() }
10951168
}
10961169
}
10971170

10981171
impl Default for buf_union {
10991172
#[inline]
11001173
fn default() -> Self {
1101-
let mut s = ::core::mem::MaybeUninit::<Self>::uninit();
11021174
// Safety: All of Linux's io_uring structs may be zero-initialized.
1103-
unsafe {
1104-
::core::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
1105-
s.assume_init()
1106-
}
1175+
unsafe { ::core::mem::zeroed::<Self>() }
11071176
}
11081177
}
11091178

11101179
impl Default for splice_fd_in_or_file_index_union {
11111180
#[inline]
11121181
fn default() -> Self {
1113-
let mut s = ::core::mem::MaybeUninit::<Self>::uninit();
11141182
// Safety: All of Linux's io_uring structs may be zero-initialized.
1115-
unsafe {
1116-
::core::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
1117-
s.assume_init()
1118-
}
1183+
unsafe { ::core::mem::zeroed::<Self>() }
11191184
}
11201185
}
11211186

11221187
impl Default for register_or_sqe_op_or_sqe_flags_union {
11231188
#[inline]
11241189
fn default() -> Self {
1125-
let mut s = ::core::mem::MaybeUninit::<Self>::uninit();
11261190
// Safety: All of Linux's io_uring structs may be zero-initialized.
1127-
unsafe {
1128-
::core::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
1129-
s.assume_init()
1130-
}
1191+
unsafe { ::core::mem::zeroed::<Self>() }
11311192
}
11321193
}
11331194

@@ -1214,7 +1275,7 @@ fn io_uring_layouts() {
12141275
check_type!(io_uring_sqe);
12151276
check_struct_field!(io_uring_sqe, opcode);
12161277
check_struct_field!(io_uring_sqe, flags);
1217-
check_struct_field!(io_uring_sqe, ioprio);
1278+
check_struct_renamed_field!(io_uring_sqe, ioprio_or_flags, ioprio);
12181279
check_struct_field!(io_uring_sqe, fd);
12191280
check_struct_renamed_field!(io_uring_sqe, off_or_addr2, __bindgen_anon_1);
12201281
check_struct_renamed_field!(io_uring_sqe, addr_or_splice_off_in, __bindgen_anon_2);
@@ -1274,6 +1335,7 @@ fn io_uring_layouts() {
12741335
resv1,
12751336
resv2
12761337
);
1338+
check_struct!(io_uring_recvmsg_out, namelen, controllen, payloadlen, flags);
12771339
check_struct!(io_uring_probe, last_op, ops_len, resv, resv2, ops);
12781340
check_struct!(io_uring_probe_op, op, resv, flags, resv2);
12791341
check_struct!(io_uring_files_update, offset, resv, fds);

0 commit comments

Comments
 (0)