Skip to content
Draft
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
24 changes: 12 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ align-address = "0.3"
anstyle = { version = "1", default-features = false }
async-executor = { git = "https://github.com/hermit-os/async-executor.git", branch = "no_std", default-features = false, features = ["static"] }
async-lock = { version = "3.4.1", default-features = false }
async-trait = "0.1.89"
bit_field = "0.10"
bitflags = "2"
build-time = "0.1.3"
Expand Down Expand Up @@ -136,6 +135,7 @@ time = { version = "0.3", default-features = false }
volatile = "0.6"
zerocopy = { version = "0.8", default-features = false }
uhyve-interface = "0.1.3"
delegate = "0.13.5"

[dependencies.smoltcp]
version = "0.12"
Expand Down
3 changes: 0 additions & 3 deletions src/fd/eventfd.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use alloc::boxed::Box;
use alloc::collections::vec_deque::VecDeque;
use core::future::{self, Future};
use core::mem;
use core::task::{Poll, Waker, ready};

use async_lock::Mutex;
use async_trait::async_trait;

use crate::errno::Errno;
use crate::fd::{EventFlags, ObjectInterface, PollEvent};
Expand Down Expand Up @@ -43,7 +41,6 @@ impl EventFd {
}
}

#[async_trait]
impl ObjectInterface for EventFd {
async fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
let len = mem::size_of::<u64>();
Expand Down
176 changes: 159 additions & 17 deletions src/fd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
use alloc::boxed::Box;
use alloc::sync::Arc;
use core::future::{self, Future};
use core::mem::MaybeUninit;
use core::task::Poll::{Pending, Ready};
use core::time::Duration;

use async_trait::async_trait;
use delegate::delegate;
#[cfg(feature = "net")]
use smoltcp::wire::{IpEndpoint, IpListenEndpoint};

use crate::arch::kernel::core_local::core_scheduler;
use crate::errno::Errno;
use crate::executor::block_on;
use crate::fs::{FileAttr, SeekWhence};
use crate::fd::eventfd::EventFd;
#[cfg(feature = "tcp")]
use crate::fd::socket::tcp;
#[cfg(feature = "udp")]
use crate::fd::socket::udp;
#[cfg(feature = "vsock")]
use crate::fd::socket::vsock;
use crate::fd::stdio::{
GenericStderr, GenericStdin, GenericStdout, UhyveStderr, UhyveStdin, UhyveStdout,
};
#[cfg(feature = "fuse")]
use crate::fs::fuse::{FuseDirectoryHandle, FuseFileHandle};
use crate::fs::mem::{MemDirectoryInterface, RamFileInterface, RomFileInterface};
use crate::fs::uhyve::UhyveFileHandle;
use crate::fs::{DirectoryReader, FileAttr, SeekWhence};
use crate::io;

mod eventfd;
Expand Down Expand Up @@ -195,7 +208,6 @@ impl Default for AccessPermission {
}
}

#[async_trait]
pub(crate) trait ObjectInterface: Sync + Send {
/// check if an IO event is possible
async fn poll(&self, _event: PollEvent) -> io::Result<PollEvent> {
Expand Down Expand Up @@ -233,9 +245,7 @@ pub(crate) trait ObjectInterface: Sync + Send {

/// `accept` a connection on a socket
#[cfg(any(feature = "net", feature = "vsock"))]
async fn accept(
&mut self,
) -> io::Result<(Arc<async_lock::RwLock<dyn ObjectInterface>>, Endpoint)> {
async fn accept(&mut self) -> io::Result<(Arc<async_lock::RwLock<Fd>>, Endpoint)> {
Err(Errno::Inval)
}

Expand Down Expand Up @@ -332,6 +342,144 @@ pub(crate) trait ObjectInterface: Sync + Send {
}
}

pub(crate) enum Fd {
GenericStdin(GenericStdin),
GenericStdout(GenericStdout),
GenericStderr(GenericStderr),
UhyveStdin(UhyveStdin),
UhyveStdout(UhyveStdout),
UhyveStderr(UhyveStderr),
EventFd(EventFd),
#[cfg(feature = "tcp")]
TcpSocket(tcp::Socket),
#[cfg(feature = "udp")]
UdpSocket(udp::Socket),
#[cfg(feature = "vsock")]
VsockNullSocket(vsock::NullSocket),
#[cfg(feature = "vsock")]
VsockSocket(vsock::Socket),
#[cfg(feature = "fuse")]
FuseFileHandle(FuseFileHandle),
#[cfg(feature = "fuse")]
FuseDirectoryHandle(FuseDirectoryHandle),
RomFileInterface(RomFileInterface),
RamFileInterface(RamFileInterface),
MemDirectoryInterface(MemDirectoryInterface),
DirectoryReader(DirectoryReader),
UhyveFileHandle(UhyveFileHandle),
}

macro_rules! fd_from {
() => {};
(
$(#[$meta:meta])*
$ident:ident($ty:ty),
$($rest:tt)*
) => {
$(#[$meta])*
impl From<$ty> for Fd {
fn from(value: $ty) -> Self {
Self::$ident(value)
}
}

fd_from!($($rest)*);
};
}

fd_from! {
GenericStdin(GenericStdin),
GenericStdout(GenericStdout),
GenericStderr(GenericStderr),
UhyveStdin(UhyveStdin),
UhyveStdout(UhyveStdout),
UhyveStderr(UhyveStderr),
EventFd(EventFd),
#[cfg(feature = "tcp")]
TcpSocket(tcp::Socket),
#[cfg(feature = "udp")]
UdpSocket(udp::Socket),
#[cfg(feature = "vsock")]
VsockNullSocket(vsock::NullSocket),
#[cfg(feature = "vsock")]
VsockSocket(vsock::Socket),
#[cfg(feature = "fuse")]
FuseFileHandle(FuseFileHandle),
#[cfg(feature = "fuse")]
FuseDirectoryHandle(FuseDirectoryHandle),
RomFileInterface(RomFileInterface),
RamFileInterface(RamFileInterface),
MemDirectoryInterface(MemDirectoryInterface),
DirectoryReader(DirectoryReader),
UhyveFileHandle(UhyveFileHandle),
}

impl ObjectInterface for Fd {
delegate! {
to match self {
Self::GenericStdin(fd) => fd,
Self::GenericStdout(fd) => fd,
Self::GenericStderr(fd) => fd,
Self::UhyveStdin(fd) => fd,
Self::UhyveStdout(fd) => fd,
Self::UhyveStderr(fd) => fd,
Self::EventFd(fd) => fd,
#[cfg(feature = "tcp")]
Self::TcpSocket(fd) => fd,
#[cfg(feature = "udp")]
Self::UdpSocket(fd) => fd,
#[cfg(feature = "vsock")]
Self::VsockNullSocket(fd) => fd,
#[cfg(feature = "vsock")]
Self::VsockSocket(fd) => fd,
#[cfg(feature = "fuse")]
Self::FuseFileHandle(fd) => fd,
#[cfg(feature = "fuse")]
Self::FuseDirectoryHandle(fd) => fd,
Self::RomFileInterface(fd) => fd,
Self::RamFileInterface(fd) => fd,
Self::MemDirectoryInterface(fd) => fd,
Self::DirectoryReader(fd) => fd,
Self::UhyveFileHandle(fd) => fd,
} {
async fn poll(&self, event: PollEvent) -> io::Result<PollEvent>;
async fn read(&self, buf: &mut [u8]) -> io::Result<usize>;
async fn write(&self, buf: &[u8]) -> io::Result<usize>;
async fn lseek(&self, offset: isize, whence: SeekWhence) -> io::Result<isize>;
async fn fstat(&self) -> io::Result<FileAttr>;
async fn getdents(&self, buf: &mut [MaybeUninit<u8>]) -> io::Result<usize>;
#[cfg(any(feature = "net", feature = "vsock"))]
async fn accept(&mut self) -> io::Result<(Arc<async_lock::RwLock<Fd>>, Endpoint)>;
#[cfg(any(feature = "net", feature = "vsock"))]
async fn connect(&mut self, endpoint: Endpoint) -> io::Result<()>;
#[cfg(any(feature = "net", feature = "vsock"))]
async fn bind(&mut self, _name: ListenEndpoint) -> io::Result<()>;
#[cfg(any(feature = "net", feature = "vsock"))]
async fn listen(&mut self, _backlog: i32) -> io::Result<()>;
#[cfg(any(feature = "net", feature = "vsock"))]
async fn setsockopt(&self, _opt: SocketOption, _optval: bool) -> io::Result<()>;
#[cfg(any(feature = "net", feature = "vsock"))]
async fn getsockopt(&self, _opt: SocketOption) -> io::Result<bool>;
#[cfg(any(feature = "net", feature = "vsock"))]
async fn getsockname(&self) -> io::Result<Option<Endpoint>>;
#[cfg(any(feature = "net", feature = "vsock"))]
#[allow(dead_code)]
async fn getpeername(&self) -> io::Result<Option<Endpoint>>;
#[cfg(any(feature = "net", feature = "vsock"))]
async fn recvfrom(&self, _buffer: &mut [MaybeUninit<u8>]) -> io::Result<(usize, Endpoint)>;
#[cfg(any(feature = "net", feature = "vsock"))]
async fn sendto(&self, _buffer: &[u8], _endpoint: Endpoint) -> io::Result<usize>;
#[cfg(any(feature = "net", feature = "vsock"))]
async fn shutdown(&self, _how: i32) -> io::Result<()>;
async fn status_flags(&self) -> io::Result<StatusFlags>;
async fn set_status_flags(&mut self, _status_flags: StatusFlags) -> io::Result<()>;
async fn truncate(&self, _size: usize) -> io::Result<()>;
async fn chmod(&self, _access_permission: AccessPermission) -> io::Result<()>;
async fn isatty(&self) -> io::Result<bool>;
}
}
}

pub(crate) fn read(fd: FileDescriptor, buf: &mut [u8]) -> io::Result<usize> {
let obj = get_object(fd)?;

Expand Down Expand Up @@ -442,20 +590,16 @@ pub fn fstat(fd: FileDescriptor) -> io::Result<FileAttr> {
pub fn eventfd(initval: u64, flags: EventFlags) -> io::Result<FileDescriptor> {
let obj = self::eventfd::EventFd::new(initval, flags);

let fd = core_scheduler().insert_object(Arc::new(async_lock::RwLock::new(obj)))?;
let fd = core_scheduler().insert_object(Arc::new(async_lock::RwLock::new(obj.into())))?;

Ok(fd)
}

pub(crate) fn get_object(
fd: FileDescriptor,
) -> io::Result<Arc<async_lock::RwLock<dyn ObjectInterface>>> {
pub(crate) fn get_object(fd: FileDescriptor) -> io::Result<Arc<async_lock::RwLock<Fd>>> {
core_scheduler().get_object(fd)
}

pub(crate) fn insert_object(
obj: Arc<async_lock::RwLock<dyn ObjectInterface>>,
) -> io::Result<FileDescriptor> {
pub(crate) fn insert_object(obj: Arc<async_lock::RwLock<Fd>>) -> io::Result<FileDescriptor> {
core_scheduler().insert_object(obj)
}

Expand All @@ -471,9 +615,7 @@ pub(crate) fn dup_object2(fd1: FileDescriptor, fd2: FileDescriptor) -> io::Resul
core_scheduler().dup_object2(fd1, fd2)
}

pub(crate) fn remove_object(
fd: FileDescriptor,
) -> io::Result<Arc<async_lock::RwLock<dyn ObjectInterface>>> {
pub(crate) fn remove_object(fd: FileDescriptor) -> io::Result<Arc<async_lock::RwLock<Fd>>> {
core_scheduler().remove_object(fd)
}

Expand Down
11 changes: 3 additions & 8 deletions src/fd/socket/tcp.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use alloc::boxed::Box;
use alloc::collections::BTreeSet;
use alloc::sync::Arc;
use core::future;
use core::sync::atomic::{AtomicU16, Ordering};
use core::task::Poll;

use async_trait::async_trait;
use smoltcp::iface;
use smoltcp::socket::tcp;
use smoltcp::time::Duration;
Expand All @@ -14,7 +12,7 @@ use smoltcp::wire::{IpEndpoint, Ipv4Address, Ipv6Address};
use crate::errno::Errno;
use crate::executor::block_on;
use crate::executor::network::{Handle, NIC};
use crate::fd::{self, Endpoint, ListenEndpoint, ObjectInterface, PollEvent, SocketOption};
use crate::fd::{self, Endpoint, Fd, ListenEndpoint, ObjectInterface, PollEvent, SocketOption};
use crate::syscalls::socket::Af;
use crate::{DEFAULT_KEEP_ALIVE_INTERVAL, io};

Expand Down Expand Up @@ -111,7 +109,6 @@ impl Socket {
}
}

#[async_trait]
impl ObjectInterface for Socket {
async fn poll(&self, event: PollEvent) -> io::Result<PollEvent> {
future::poll_fn(|cx| {
Expand Down Expand Up @@ -300,9 +297,7 @@ impl ObjectInterface for Socket {
}
}

async fn accept(
&mut self,
) -> io::Result<(Arc<async_lock::RwLock<dyn ObjectInterface>>, Endpoint)> {
async fn accept(&mut self) -> io::Result<(Arc<async_lock::RwLock<Fd>>, Endpoint)> {
if !self.is_listen {
self.listen(DEFAULT_BACKLOG).await?;
}
Expand Down Expand Up @@ -361,7 +356,7 @@ impl ObjectInterface for Socket {
is_listen: false,
};

Ok((Arc::new(async_lock::RwLock::new(socket)), endpoint))
Ok((Arc::new(async_lock::RwLock::new(socket.into())), endpoint))
}

async fn getpeername(&self) -> io::Result<Option<Endpoint>> {
Expand Down
3 changes: 0 additions & 3 deletions src/fd/socket/udp.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use alloc::boxed::Box;
use core::future;
use core::mem::MaybeUninit;
use core::task::Poll;

use async_trait::async_trait;
use smoltcp::socket::udp;
use smoltcp::socket::udp::UdpMetadata;
use smoltcp::wire::{IpEndpoint, Ipv4Address, Ipv6Address};
Expand Down Expand Up @@ -75,7 +73,6 @@ impl Socket {
}
}

#[async_trait]
impl ObjectInterface for Socket {
async fn poll(&self, event: PollEvent) -> io::Result<PollEvent> {
future::poll_fn(|cx| {
Expand Down
Loading