Skip to content

Commit

Permalink
Add socketpair syscall
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalmiel committed Jun 9, 2024
1 parent 3c83b5d commit 653b62d
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 9 deletions.
11 changes: 9 additions & 2 deletions cykusz-rs/src/kernel/net/socket.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use alloc::sync::Arc;

use syscall_defs::net::{MsgFlags, MsgHdr, SockAddrPtr, SockDomain, SockFlags, 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 @@ -9,7 +11,12 @@ use crate::kernel::net::ip::{Ip, Ip4};
use crate::kernel::net::Packet;

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

match (domain, SockType::from(typ)) {
(SockDomain::AfInet, SockType::Stream) => {
Expand Down
25 changes: 23 additions & 2 deletions cykusz-rs/src/kernel/net/unix/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::kernel::utils::wait_queue::{WaitQueue, WaitQueueFlags};
use alloc::sync::{Arc, Weak};
use alloc::vec::Vec;
use core::sync::atomic::{AtomicUsize, Ordering};
use syscall_defs::net::{MsgFlags, MsgHdr, SockAddrPtr, SockAddrUn};
use syscall_defs::net::{MsgFlags, MsgHdr, SockAddrPtr, SockAddrUn, SockTypeFlags};
use syscall_defs::poll::PollEventFlags;
use syscall_defs::stat::{Mode, Stat};
use syscall_defs::{OpenFlags, SyscallError, SyscallResult};
Expand All @@ -23,7 +23,10 @@ struct ConnectionQueue {

impl ConnectionQueue {
fn new(addr: SockAddrUn) -> ConnectionQueue {
ConnectionQueue { queue: Vec::new(), addr }
ConnectionQueue {
queue: Vec::new(),
addr,
}
}

fn addr(&self) -> &SockAddrUn {
Expand Down Expand Up @@ -63,6 +66,24 @@ impl Socket {
})
}

pub fn new_connected(_flags: SockTypeFlags) -> (Arc<Socket>, Arc<Socket>) {
let s1 = Socket::new_unbound(None);
let s2 = Socket::new_unbound(None);

s1.buffer.init_size(4096 * 4);
s2.buffer.init_size(4096 * 4);

*s1.data.lock() = SocketState::Connected(s2.clone());
*s2.data.lock() = SocketState::Connected(s1.clone());

s1.inc_readers();
s1.inc_writers();
s2.inc_readers();
s2.inc_writers();

(s1, s2)
}

fn target(&self) -> Arc<Socket> {
if let SocketState::Connected(s) = &*self.data.lock() {
s.clone()
Expand Down
1 change: 1 addition & 0 deletions cykusz-rs/src/kernel/syscall/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub fn syscall_handler(num: u64, a: u64, b: u64, c: u64, d: u64, e: u64, f: u64)
SYS_POWEROFF => sys::sys_poweroff(),
SYS_REBOOT => sys::sys_reboot(),
SYS_SOCKET => sys::sys_socket(a, b, c),
SYS_SOCKETPAIR => sys::sys_socketpair(a, b, c, d),
SYS_BIND => sys::sys_bind(a, b, c),
SYS_CONNECT => sys::sys_connect(a, b, c).maybe_into_erestartsys(),
SYS_ACCEPT => sys::sys_accept(a, b, c, d).maybe_into_erestartsys(),
Expand Down
29 changes: 28 additions & 1 deletion cykusz-rs/src/kernel/syscall/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,29 @@ pub fn sys_socket(domain: u64, typ: u64, _protocol: u64) -> SyscallResult {
res
}

pub fn sys_socketpair(domain: u64, typ: u64, _protocol: u64, fds: u64) -> SyscallResult {
let sock_domain = SockDomain::try_from(domain)?;
let typ = SockTypeFlags::new(typ);

if sock_domain != SockDomain::AfUnix {
return Err(SyscallError::ENOTSUP);
}

let (s1, s2) = crate::kernel::net::unix::socket::Socket::new_connected(typ);

let task = current_task_ref();

let fd1 = task.open_file(DirEntry::inode_wrap(s1.clone()), OpenFlags::RDWR)?;
let fd2 = task.open_file(DirEntry::inode_wrap(s2.clone()), OpenFlags::RDWR)?;

let fds = unsafe { VirtAddr(fds as usize).read_mut::<[i32; 2]>() };

fds[0] = fd1 as i32;
fds[1] = fd2 as i32;

Ok(0)
}

fn get_socket(fd: usize) -> Result<Arc<dyn SocketService>, SyscallError> {
let task = current_task_ref();

Expand Down Expand Up @@ -868,7 +891,11 @@ pub fn sys_msg_send(sockfd: u64, hdr: u64, flags: u64) -> SyscallResult {

let hdr = unsafe { VirtAddr(hdr as usize).read_ref::<MsgHdr>() };

logln!("hdr name len {} name addr {:#X}", hdr.msg_namelen, hdr.msg_name.addr());
logln!(
"hdr name len {} name addr {:#X}",
hdr.msg_namelen,
hdr.msg_name.addr()
);

let sock = get_socket(sockfd as usize)?;

Expand Down
1 change: 0 additions & 1 deletion cykusz-rs/src/kernel/utils/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ impl BufferQueue {
})?
.unwrap();


logln!("READ DATA STARTING");
let read = if transient {
buffer.read_data_transient_from(offset, buf)
Expand Down
5 changes: 3 additions & 2 deletions syscall-defs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub const SYS_SLEEP: usize = 9;
pub const SYS_POWEROFF: usize = 10;
pub const SYS_REBOOT: usize = 11;
pub const SYS_GETADDRINFO: usize = 12;
pub const SYS_BIND: usize = 13;
pub const SYS_CONNECT: usize = 14;
pub const SYS_SELECT: usize = 15;
pub const SYS_MOUNT: usize = 16;
pub const SYS_UMOUNT: usize = 17;
Expand Down Expand Up @@ -91,8 +93,6 @@ pub const SYS_TRUNCATE: usize = 61;
pub const SYS_POLL: usize = 62;

pub const SYS_SOCKET: usize = 63;
pub const SYS_BIND: usize = 13;
pub const SYS_CONNECT: usize = 14;
pub const SYS_ACCEPT: usize = 64;
pub const SYS_LISTEN: usize = 65;
pub const SYS_MSGRECV: usize = 66;
Expand All @@ -103,6 +103,7 @@ pub const SYS_YIELD: usize = 70;
pub const SYS_CHMOD: usize = 71;
pub const SYS_UTIME: usize = 72;
pub const SYS_MKNODE: usize = 73;
pub const SYS_SOCKETPAIR: usize = 74;

#[derive(Copy, Clone, PartialEq, Debug)]
#[repr(u64)]
Expand Down
4 changes: 3 additions & 1 deletion syscall-defs/src/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ impl FdSet {
impl PollFd {
pub fn new(fd: i32, events: PollEventFlags) -> PollFd {
PollFd {
fd, events, revents: PollEventFlags::empty()
fd,
events,
revents: PollEventFlags::empty(),
}
}
}

0 comments on commit 653b62d

Please sign in to comment.