Skip to main content

strat9_kernel/async_io/
ops.rs

1/*
2 * This file is part of strat9-os.
3 *
4 * strat9-os is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * strat9-os is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with strat9-os. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * Website: https://www.strat9-os.org
18 * Repository: https://github.com/strat9-os/strat9-os
19 *
20 * Copyright (C) 2023-26 Guillaume Gielly
21 */
22
23//! Asynchronous I/O ABI types : stable across kernel versions.
24//!
25//! Defines the [`AsyncOp`] opcode enum, [`AsyncSqe`] (submission queue entry),
26//! and [`AsyncCqe`] (completion queue event) as `#[repr(C)]` structs.
27
28/// Operation opcode submitted through the async ring.
29///
30/// **Do not reorder** existing variants : the numeric value is part of the
31/// userspace ABI and must remain stable.
32#[repr(u8)]
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
34#[allow(dead_code)]
35pub enum AsyncOp {
36    /// No-op : used for testing and ring probing.
37    Nop = 0,
38    /// VFS: read from an open file descriptor.
39    Read = 1,
40    /// VFS: write to an open file descriptor.
41    Write = 2,
42    /// VFS: open a file (async version).
43    Open = 3,
44    /// VFS: close a file descriptor.
45    Close = 4,
46    /// VFS: stat / fstat a file.
47    Stat = 5,
48
49    /// IPC: send a message to a port (non-blocking).
50    IpcSend = 10,
51    /// IPC: receive a message from a port (non-blocking).
52    IpcRecv = 11,
53    /// IPC: combined send + recv (reply channel).
54    IpcCall = 12,
55    /// IPC v2: send on an endpoint handle (`sqe.off`), `*MsgBuf` in `addr`.
56    EpSend = 13,
57    /// IPC v2: call; `*MsgCall` in `addr`. CQE when the reply is copied out.
58    EpCall = 14,
59    /// IPC v2: receive; CQE `res` = message kind.
60    EpRecv = 15,
61    /// IPC v2: reply on a Reply handle; never blocks.
62    EpReply = 16,
63    /// IPC v2: notification wait; bits written to `*u64` in `addr`.
64    NotifWait = 17,
65
66    /// Poll: add an fd to the epoll set.
67    PollAdd = 20,
68    /// Poll: remove an fd from the epoll set.
69    PollRemove = 21,
70
71    /// Timer: arm a timeout completion.
72    Timeout = 30,
73    /// Timer: remove a previously armed timeout.
74    TimeoutRemove = 31,
75
76    /// Network: accept a connection on a listening socket.
77    Accept = 40,
78    /// Network: connect to a remote endpoint.
79    Connect = 41,
80
81    /// Storage: read sectors from a block device (AHCI / NVMe).
82    StorageRead = 50,
83    /// Storage: write sectors to a block device.
84    StorageWrite = 51,
85
86    /// Cancel an in-flight operation by its `user_data` token.
87    Cancel = 254,
88}
89
90// =============================================================================
91// SQE : Submission Queue Entry (64 bytes, cache-line aligned)
92// =============================================================================
93
94/// A single I/O submission, written by userspace into the SQ ring.
95#[repr(C)]
96#[derive(Debug, Clone, Copy)]
97pub struct AsyncSqe {
98    /// Operation opcode ([`AsyncOp`] variant).
99    pub opcode: u8,
100    /// Flags: `FIXED_FILE`, `IO_LINK`, `IO_DRAIN`, `IO_HARDLINK`.
101    pub flags: u8,
102    /// Priority hint (ioprio class << 13 | ioprio data).
103    pub ioprio: u16,
104    /// File descriptor (VFS fd) or IPC port id.
105    pub fd: u32,
106    /// File offset (for read/write) or endpoint id (for IPC).
107    pub off: u64,
108    /// Buffer virtual address (userspace pointer : validated by kernel).
109    pub addr: u64,
110    /// Buffer length in bytes.
111    pub len: u32,
112    /// Operation-specific flags (e.g. `RW_FSYNC` for write).
113    pub op_flags: u32,
114    /// Opaque correlation token : echoed in the CQE on completion.
115    pub user_data: u64,
116    /// Personality / capability context (silo token).
117    pub personality: u16,
118    /// Padding to 64 bytes (cache-line aligned).
119    pub _pad: [u8; 22],
120}
121
122// Compile-time size check : must be exactly 64 bytes.
123const _: () = assert!(core::mem::size_of::<AsyncSqe>() == 64);
124
125// =============================================================================
126// CQE : Completion Queue Event (16 bytes)
127// =============================================================================
128
129/// A single I/O completion, written by the kernel into the CQ ring.
130#[repr(C)]
131#[derive(Debug, Clone, Copy)]
132pub struct AsyncCqe {
133    /// Opaque correlation token from the originating SQE.
134    pub user_data: u64,
135    /// Result: bytes transferred on success, or a negative errno on failure.
136    pub res: i32,
137    /// Flags: `CQE_F_BUFFER`, `CQE_F_MORE`.
138    pub flags: u32,
139}
140
141// Compile-time size check : must be exactly 16 bytes.
142const _: () = assert!(core::mem::size_of::<AsyncCqe>() == 16);
143
144// =============================================================================
145// Shared constants
146// =============================================================================
147
148/// Maximum number of in-flight operations per ring.
149pub const MAX_IN_FLIGHT: usize = 4096;
150
151/// Default SQE ring size (must be a power of two).
152pub const DEFAULT_RING_ENTRIES: u32 = 256;