Skip to main content

strate_net_silo/
ipc.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//! Typed helpers to build and parse the VFS scheme wire protocol.
24//!
25//! Every function here delegates to the typed ABI layer
26//! (`strat9_abi::ipc_payload`) instead of hand-rolled payload offsets,
27//! so the wire contract has a single source of truth.
28
29use strat9_abi::ipc_payload::{
30    OpenReply, OpenRequest, ReadReply, ReadRequest, WriteReply, WriteRequest,
31};
32use strat9_syscall::data::{
33    IpcMessage, IPC_FILE_FLAG_CHUNK_READ, IPC_FILE_FLAG_CHUNK_WRITE, IPC_FILE_FLAG_PIPE,
34};
35
36pub(crate) const HANDLE_FLAG_INFO: u32 = IPC_FILE_FLAG_CHUNK_READ;
37pub(crate) const HANDLE_FLAG_CONTROL: u32 = 0;
38pub(crate) const HANDLE_FLAG_STREAM: u32 =
39    IPC_FILE_FLAG_PIPE | IPC_FILE_FLAG_CHUNK_READ | IPC_FILE_FLAG_CHUNK_WRITE;
40pub(crate) const HANDLE_FLAG_DATAGRAM: u32 = IPC_FILE_FLAG_CHUNK_READ;
41
42/// Errno value returned for malformed requests (`EINVAL`).
43const EINVAL: i32 = -22;
44
45pub(crate) fn reply_open(sender: u64, file_id: u64, size: u64, flags: u32) -> IpcMessage {
46    let mut msg = IpcMessage::new(IpcMessage::REPLY_MSG_TYPE);
47    msg.sender = sender;
48    OpenReply {
49        status: 0,
50        file_id,
51        size,
52        file_flags: flags,
53    }
54    .encode_into(&mut msg.payload);
55    msg
56}
57
58pub(crate) fn reply_read(sender: u64, data: &[u8]) -> IpcMessage {
59    let (msg, _n) = ReadReply::encode_ok(sender, data);
60    msg
61}
62
63/// Reply carrying only a byte count, used by the bulk-ring path.
64pub(crate) fn reply_read_count(sender: u64, count: usize) -> IpcMessage {
65    let mut msg = IpcMessage::new(IpcMessage::REPLY_MSG_TYPE);
66    msg.sender = sender;
67    msg.payload[0..4].copy_from_slice(&0u32.to_le_bytes());
68    msg.payload[4..8].copy_from_slice(&(count as u32).to_le_bytes());
69    msg
70}
71
72pub(crate) fn reply_write(sender: u64, n: usize) -> IpcMessage {
73    WriteReply::encode_ok(sender, n)
74}
75
76pub(crate) fn reply_write_data_len(sender: u64, msg: &IpcMessage) -> IpcMessage {
77    match WriteRequest::parse_prefix(&msg.payload) {
78        Some(req) => reply_write(sender, req.data_len as usize),
79        None => reply_write(sender, 0),
80    }
81}
82
83pub(crate) fn reply_ok(sender: u64) -> IpcMessage {
84    IpcMessage::status_reply(sender, 0)
85}
86
87/// Extract the path from an OPEN request, with any leading `/` trimmed.
88pub(crate) fn open_path(msg: &IpcMessage) -> Result<&str, i32> {
89    let (_flags, path) = OpenRequest::parse(&msg.payload).ok_or(EINVAL)?;
90    if path.len() > IpcMessage::OPEN_INLINE_CAPACITY {
91        return Err(EINVAL);
92    }
93    Ok(path.trim_start_matches('/'))
94}
95
96pub(crate) fn message_file_id(msg: &IpcMessage) -> u64 {
97    // Used for CLOSE-style requests whose payload is a single `ino: u64`.
98    u64::from_le_bytes(msg.payload[0..8].try_into().unwrap_or([0u8; 8]))
99}
100
101/// Parse a READ request payload into `(file_id, offset, requested_count)`.
102pub(crate) fn read_request(msg: &IpcMessage) -> (u64, u64, usize) {
103    match ReadRequest::parse(&msg.payload) {
104        Some(req) => (req.ino, req.offset, req.count_usize()),
105        None => (0, 0, 0),
106    }
107}
108
109pub(crate) fn write_data(msg: &IpcMessage) -> &[u8] {
110    WriteRequest::parse_prefix(&msg.payload)
111        .and_then(|req| req.data(&msg.payload))
112        .unwrap_or(&[])
113}