Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion litebox/src/net/phy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ impl<Platform: platform::IPInterfaceProvider> smoltcp::phy::Device for Device<Pl
buffer: &mut self.send_buffer,
},
)),
Err(platform::ReceiveError::WouldBlock) => None,
Err(
platform::ReceiveError::WouldBlock
| platform::ReceiveError::ProtocolError
| platform::ReceiveError::Eof,
) => None,
}
}

Expand Down
2 changes: 2 additions & 0 deletions litebox/src/platform/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ impl MockPlatform {

impl Provider for MockPlatform {}

impl RawMessageProvider for MockPlatform {}

pub(crate) struct MockRawMutex {
inner: AtomicU32,
internal_state: std::sync::RwLock<MockRawMutexInternalState>,
Expand Down
44 changes: 41 additions & 3 deletions litebox/src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ macro_rules! log_println {
pub trait Provider:
RawMutexProvider
+ IPInterfaceProvider
+ RawMessageProvider
+ TimeProvider
+ PunchthroughProvider
+ DebugLogProvider
Expand Down Expand Up @@ -382,17 +383,54 @@ pub trait IPInterfaceProvider {
fn receive_ip_packet(&self, packet: &mut [u8]) -> Result<usize, ReceiveError>;
}

/// A non-exhaustive list of errors that can be thrown by [`IPInterfaceProvider::send_ip_packet`].
/// Errors from send operations on [`IPInterfaceProvider`] and [`RawMessageProvider`].
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum SendError {}
pub enum SendError {
/// The underlying device returned an I/O error. The packet was not sent.
#[error("I/O error on send: errno {0}")]
Io(i32),
Comment on lines +390 to +392
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned on #743, hardcoding an i32 here is wrong; pick actually relevant errors

/// The channel is not available on this platform.
#[error("send channel unavailable")]
Unavailable,
}

/// A non-exhaustive list of errors that can be thrown by [`IPInterfaceProvider::receive_ip_packet`].
/// Errors from receive operations on [`IPInterfaceProvider`] and [`RawMessageProvider`].
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum ReceiveError {
#[error("Receive operation would block")]
WouldBlock,
#[error("IPC protocol error: oversized frame")]
ProtocolError,
#[error("Channel closed (EOF)")]
Eof,
}

/// A raw byte-stream channel for direct message passing between the guest and
/// the host (bypassing the IP network stack).
///
/// When available, this provides a fast path for protocols like 9P that would
/// otherwise pay the overhead of traversing two smoltcp stacks.
Comment on lines +413 to +414
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: "network stacks" not "smoltcp stacks". Doc strings should not promise unnecessary internal details.

///
/// The default implementation returns [`ReceiveError::WouldBlock`] /
/// [`SendError::Unavailable`], indicating the channel is not available.
Comment on lines +416 to +417
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the default should be Unavailable for both, because otherwise a shim has no way to know if it tries to recv first whether it is just that something has not shown up (so it should try again) or whether it is never going to become available.

/// Platforms that support direct messaging override these methods.
pub trait RawMessageProvider {
/// Send bytes to the host over the raw channel.
///
/// Returns `Ok(n)` with the number of bytes sent, or an error.
fn send_raw_message(&self, _data: &[u8]) -> Result<usize, SendError> {
Err(SendError::Unavailable)
}

/// Receive bytes from the host over the raw channel.
///
/// Returns `Ok(n)` with the number of bytes read into `buf`, or
/// [`ReceiveError::WouldBlock`] if no data is available yet.
fn recv_raw_message(&self, _buf: &mut [u8]) -> Result<usize, ReceiveError> {
Err(ReceiveError::WouldBlock)
}
}

/// An interface to understanding time.
Expand Down
2 changes: 2 additions & 0 deletions litebox_platform_linux_kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ impl<'a, Host: HostInterface> PunchthroughToken for LinuxPunchthroughToken<'a, H

impl<Host: HostInterface> Provider for LinuxKernel<Host> {}

impl<Host: HostInterface> litebox::platform::RawMessageProvider for LinuxKernel<Host> {}

// TODO: implement pointer validation to ensure the pointers are in user space.
type UserConstPtr<T> = litebox::platform::common_providers::userspace_pointers::UserConstPtr<
litebox::platform::common_providers::userspace_pointers::NoValidation,
Expand Down
2 changes: 2 additions & 0 deletions litebox_platform_linux_userland/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,8 @@ impl LinuxUserland {

impl litebox::platform::Provider for LinuxUserland {}

impl litebox::platform::RawMessageProvider for LinuxUserland {}

impl litebox::platform::SignalProvider for LinuxUserland {
type Signal = litebox_common_linux::signal::Signal;

Expand Down
2 changes: 2 additions & 0 deletions litebox_platform_lvbs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,8 @@ impl<Host: HostInterface> litebox::platform::SystemInfoProvider for LinuxKernel<
}
}

impl<Host: HostInterface> litebox::platform::RawMessageProvider for LinuxKernel<Host> {}

#[cfg(feature = "optee_syscall")]
/// Checks whether the given physical addresses are contiguous with respect to ALIGN.
fn is_contiguous<const ALIGN: usize>(addrs: &[PhysPageAddr<ALIGN>]) -> bool {
Expand Down
2 changes: 2 additions & 0 deletions litebox_platform_windows_userland/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ impl WindowsUserland {
}
}

impl litebox::platform::RawMessageProvider for WindowsUserland {}

impl litebox::platform::Provider for WindowsUserland {}

impl litebox::platform::SignalProvider for WindowsUserland {
Expand Down
Loading