Skip to content

Commit

Permalink
Add initial unix sockets impl
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalmiel committed Jun 8, 2024
1 parent 4b74e30 commit 58aa6a5
Show file tree
Hide file tree
Showing 29 changed files with 701 additions and 187 deletions.
2 changes: 1 addition & 1 deletion cykusz-rs/src/arch/x86_64/output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ macro_rules! logln {
#[allow(unused)]
macro_rules! log {
($($arg:tt)*) => ({
crate::arch::output::log_fmt_disabled(format_args!($($arg)*)).unwrap();
crate::arch::output::log_fmt(format_args!($($arg)*)).unwrap();
});
}
#[macro_export]
Expand Down
2 changes: 1 addition & 1 deletion cykusz-rs/src/drivers/ps2/kbd/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl INode for KbdState {
}

fn open(&self, flags: OpenFlags) -> crate::kernel::fs::vfs::Result<()> {
if flags == OpenFlags::RDONLY {
if flags.is_open_mode(OpenFlags::RDONLY) {
self.state.lock_irq().opened = true;
Ok(())
} else {
Expand Down
2 changes: 1 addition & 1 deletion cykusz-rs/src/drivers/ps2/mouse/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ pub fn init() {
MOUSE.call_once(|| {
Arc::new_cyclic(|me| MouseState {
state: Spin::new(State::new()),
buf: BufferQueue::new(4 * 32, false, true),
buf: BufferQueue::new(4 * 32, true, true),
dev_id: crate::kernel::device::alloc_id(),
self_ref: me.clone(),
})
Expand Down
2 changes: 2 additions & 0 deletions cykusz-rs/src/kernel/fs/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ impl Mounts {
for key in keys.iter() {
if let Err(_e) = self.unmount_by_key(key) {
logln!("Failed to unmount {:?}", key);
} else {
logln!("Unmounted {:?}", key);
}
}

Expand Down
12 changes: 7 additions & 5 deletions cykusz-rs/src/kernel/fs/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,18 @@ impl INode for Pipe {
}
}

logln!("pipe poll result: {:?}", res_flags);

Ok(res_flags)
}

fn open(&self, flags: OpenFlags) -> Result<()> {
logln!("pipe open: {:?}", flags);
if flags.contains(OpenFlags::RDWR) {
if flags.is_open_mode(OpenFlags::RDWR) {
return Err(FsError::InvalidParam);
}

if flags.contains(OpenFlags::RDONLY) {
if flags.is_open_mode(OpenFlags::RDONLY) {
self.inc_readers();

self.buf.set_has_readers(true);
Expand All @@ -148,7 +150,7 @@ impl INode for Pipe {
}
}

if flags.contains(OpenFlags::WRONLY) {
if flags.is_open_mode(OpenFlags::WRONLY) {
if flags.contains(OpenFlags::NONBLOCK) && !self.has_readers() {
return Err(FsError::NoSuchDevice);
}
Expand All @@ -166,13 +168,13 @@ impl INode for Pipe {
}

fn close(&self, flags: OpenFlags) {
if flags.contains(OpenFlags::RDONLY) {
if flags.is_open_mode(OpenFlags::RDONLY) {
if self.dec_readers() == 0 {
self.buf.set_has_readers(false);
}
}

if flags.contains(OpenFlags::WRONLY) {
if flags.is_open_mode(OpenFlags::WRONLY) {
if self.dec_writers() == 0 {
self.buf.set_has_writers(false);
}
Expand Down
1 change: 1 addition & 0 deletions cykusz-rs/src/kernel/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ pub fn register_net_driver(driver: Arc<dyn NetDriver>) {
}

pub fn init() {
unix::init();
let def = DEFAULT_DRIVER.write();
if let Some(dev) = &*def {
arp::init();
Expand Down
11 changes: 5 additions & 6 deletions cykusz-rs/src/kernel/net/socket.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use alloc::sync::Arc;

use syscall_defs::net::{
MsgFlags, MsgHdr, SockAddrPtr, SockDomain, SockOption, SockType, SockTypeFlags,
};
use syscall_defs::net::{MsgFlags, MsgHdr, SockAddrPtr, SockDomain, SockFlags, SockOption, SockType, SockTypeFlags};
use syscall_defs::SyscallError::ENOTSUP;
use syscall_defs::{SyscallError, SyscallResult};

Expand All @@ -11,7 +9,7 @@ use crate::kernel::net::ip::{Ip, Ip4};
use crate::kernel::net::Packet;

pub fn new(domain: SockDomain, typ: SockTypeFlags) -> Result<Arc<dyn INode>, SyscallError> {
logln4!("new socket: {:?} {:?}", domain, SockType::from(typ));
logln!("new socket: {:?} {:?} {:?}", domain, SockType::from(typ), SockFlags::from(typ));

match (domain, SockType::from(typ)) {
(SockDomain::AfInet, SockType::Stream) => {
Expand All @@ -20,8 +18,9 @@ pub fn new(domain: SockDomain, typ: SockTypeFlags) -> Result<Arc<dyn INode>, Sys
(SockDomain::AfInet, SockType::Dgram) => {
Ok(crate::kernel::net::udp::socket::Socket::new_unbound())
}
(SockDomain::AfUnix, _st @ (SockType::Stream | SockType::Dgram)) => {
Ok(crate::kernel::net::unix::socket::Socket::new_unbound())
(SockDomain::AfUnix, _st @ SockType::Stream) => {
logln!("new unbound!");
Ok(crate::kernel::net::unix::socket::Socket::new_unbound(None))
}
_ => Err(ENOTSUP),
}
Expand Down
7 changes: 4 additions & 3 deletions cykusz-rs/src/kernel/net/tcp/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl SocketData {
SocketData {
src_port: port,
proxy_buffer: BufferQueue::new(4096 * 8, true, true),
snd_buffer: Buffer::new(4096 * 18, true, true),
snd_buffer: Buffer::new(4096 * 18),
socket: socket.clone(),
..Default::default()
}
Expand All @@ -147,7 +147,7 @@ impl SocketData {
pub fn new_unbound(socket: &Weak<Socket>) -> SocketData {
SocketData {
proxy_buffer: BufferQueue::new_empty(true, true),
snd_buffer: Buffer::new_empty(true, true),
snd_buffer: Buffer::new_empty(),
socket: socket.clone(),
..Default::default()
}
Expand All @@ -160,7 +160,7 @@ impl SocketData {
) -> SocketData {
let mut data = SocketData {
proxy_buffer: BufferQueue::new_empty(true, true),
snd_buffer: Buffer::new_empty(true, true),
snd_buffer: Buffer::new_empty(),
ctl: from.ctl,
socket: socket.clone(),
..Default::default()
Expand Down Expand Up @@ -1123,6 +1123,7 @@ impl SocketService for Socket {
}

fn msg_send(&self, hdr: &MsgHdr, _flags: MsgFlags) -> SyscallResult {
logln!("tcp msg_send");
let iovecs = hdr.iovecs();

let mut total = 0;
Expand Down
8 changes: 7 additions & 1 deletion cykusz-rs/src/kernel/net/udp/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ impl Socket {
)
};

logln!("send buf size: {} {:?} : {}", buf.len(), dst_ip, dst_port);

if self.src_port() == 0 {
crate::kernel::net::udp::register_handler(self.me());
}
Expand Down Expand Up @@ -230,8 +232,10 @@ impl SocketService for Socket {

fn connect(&self, sock_addr: SockAddrPtr, addrlen: u32) -> SyscallResult {
let sock_addr = sock_addr.as_sock_addr_in();
logln!("udp connect sock addr: {:?}", sock_addr);

if addrlen as usize != core::mem::size_of::<SockAddrIn>() {
logln!("udp connect fail");
return Err(SyscallError::EINVAL);
}

Expand All @@ -246,7 +250,7 @@ impl SocketService for Socket {
}

fn msg_send(&self, hdr: &MsgHdr, _flags: MsgFlags) -> SyscallResult {
logln5!("UDP msg_send???");
logln!("UDP msg_send???");
let dest = if let Some(addr) = hdr.sock_addr_in() {
Some((addr.port() as u32, addr.sin_addr.s_addr.into()))
} else {
Expand All @@ -256,6 +260,7 @@ impl SocketService for Socket {
let buf = hdr.iovecs();

if buf.len() == 1 {
logln!("send buf 1");
Ok(self.send(buf[0].get_bytes(), dest)?)
} else {
let data = hdr
Expand All @@ -265,6 +270,7 @@ impl SocketService for Socket {
.copied()
.collect::<Vec<_>>();

logln!("send iovec");
Ok(self.send(data.as_slice(), dest)?)
}
}
Expand Down
14 changes: 14 additions & 0 deletions cykusz-rs/src/kernel/net/unix/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
use crate::kernel::sync::{LockApi, Mutex, MutexGuard};
use crate::kernel::utils::node_map::NodeMap;
use spin::Once;

pub mod socket;

pub static SOCKETS: Once<Mutex<NodeMap<socket::Socket>>> = Once::new();

pub fn init() {
SOCKETS.call_once(|| Mutex::new(NodeMap::new()));
}

pub fn sockets<'a>() -> MutexGuard<'a, NodeMap<socket::Socket>> {
unsafe { SOCKETS.get_unchecked().lock() }
}
Loading

1 comment on commit 58aa6a5

@bodgergely
Copy link

Choose a reason for hiding this comment

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

falling behind a lot

Please sign in to comment.