Skip to content

Commit

Permalink
net: Interface cleanup, unix sockets stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalmiel committed Jun 1, 2024
1 parent b9901ba commit d38af51
Show file tree
Hide file tree
Showing 15 changed files with 241 additions and 169 deletions.
6 changes: 4 additions & 2 deletions cykusz-rs/src/kernel/net/dhcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use core::marker::PhantomData;
use syscall_defs::net::{NetU16, NetU32, NetU8};

use crate::kernel::net::ip::{Ip, Ip4};
use crate::kernel::net::socket::SocketService;
use crate::kernel::net::socket::{NetSocketService, SocketService};
use crate::kernel::net::udp::Udp;
use crate::kernel::net::{
default_driver, Packet, PacketDownHierarchy, PacketHeader, PacketKind, PacketUpHierarchy,
Expand Down Expand Up @@ -433,7 +433,9 @@ fn process_packet_udp(packet: Packet<Udp>) {

struct DhcpService {}

impl SocketService for DhcpService {
impl SocketService for DhcpService {}

impl NetSocketService for DhcpService {
fn process_packet(&self, packet: Packet<Ip>) {
process_packet_udp(packet.upgrade());
}
Expand Down
6 changes: 4 additions & 2 deletions cykusz-rs/src/kernel/net/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use syscall_defs::net::NetU16;

use crate::kernel::mm::VirtAddr;
use crate::kernel::net::ip::{Ip, Ip4};
use crate::kernel::net::socket::SocketService;
use crate::kernel::net::socket::{NetSocketService, SocketService};
use crate::kernel::net::udp::Udp;
use crate::kernel::net::{
default_driver, Packet, PacketDownHierarchy, PacketHeader, PacketKind, PacketUpHierarchy,
Expand Down Expand Up @@ -275,7 +275,9 @@ impl DnsService {
}
}

impl SocketService for DnsService {
impl SocketService for DnsService {}

impl NetSocketService for DnsService {
fn process_packet(&self, packet: Packet<Ip>) {
let udp_packet: Packet<Udp> = packet.upgrade();
let mut packet: Packet<Dns> = udp_packet.upgrade();
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 @@ -27,6 +27,7 @@ pub mod packet;
pub mod socket;
pub mod tcp;
pub mod udp;
pub mod unix;
pub mod util;

pub trait NetDriver: Sync + Send {
Expand Down
40 changes: 22 additions & 18 deletions cykusz-rs/src/kernel/net/socket.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloc::sync::Arc;

use syscall_defs::net::{
MsgFlags, MsgHdr, SockAddr, SockDomain, SockOption, SockType, SockTypeFlags,
MsgFlags, MsgHdr, SockAddrPtr, SockDomain, SockOption, SockType, SockTypeFlags,
};
use syscall_defs::SyscallError::ENOTSUP;
use syscall_defs::{SyscallError, SyscallResult};
Expand All @@ -11,40 +11,40 @@ use crate::kernel::net::ip::{Ip, Ip4};
use crate::kernel::net::Packet;

pub fn new(domain: SockDomain, typ: SockTypeFlags) -> Result<Arc<dyn INode>, SyscallError> {
if domain != SockDomain::AfInet {
return Err(SyscallError::ENOTSUP);
}

logln4!("new socket: {:?} {:?}", domain, SockType::from(typ));

match SockType::from(typ) {
SockType::Stream => Ok(crate::kernel::net::tcp::socket::Socket::new_unbound()),
SockType::Dgram => Ok(crate::kernel::net::udp::socket::Socket::new_unbound()),
match (domain, SockType::from(typ)) {
(SockDomain::AfInet, SockType::Stream) => {
Ok(crate::kernel::net::tcp::socket::Socket::new_unbound())
}
(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())
}
_ => Err(ENOTSUP),
}
}

pub trait SocketService: Sync + Send {
fn process_packet(&self, packet: Packet<Ip>);
fn port_unreachable(&self, port: u32, dst_port: u32);

fn listen(&self, _backlog: i32) -> SyscallResult {
Err(SyscallError::EOPNOTSUPP)
}

fn accept(
&self,
_sock_addr: Option<&mut SockAddr>,
_sock_addr: SockAddrPtr,
_addrlen: Option<&mut u32>,
) -> Result<Arc<dyn SocketService>, SyscallError> {
Err(SyscallError::EOPNOTSUPP)
}

fn bind(&self, _sock_addr: &SockAddr, _addrlen: u32) -> SyscallResult {
fn bind(&self, _sock_addr: SockAddrPtr, _addrlen: u32) -> SyscallResult {
Err(SyscallError::EOPNOTSUPP)
}

fn connect(&self, _sock_addr: &SockAddr, _addrlen: u32) -> SyscallResult {
fn connect(&self, _sock_addr: SockAddrPtr, _addrlen: u32) -> SyscallResult {
Err(SyscallError::EOPNOTSUPP)
}

Expand All @@ -56,10 +56,6 @@ pub trait SocketService: Sync + Send {
Err(SyscallError::EOPNOTSUPP)
}

fn src_port(&self) -> u32;
fn target(&self) -> Ip4;
fn set_src_port(&self, _src_port: u32) {}

fn set_socket_option(
&self,
_layer: i32,
Expand All @@ -84,3 +80,11 @@ pub trait SocketService: Sync + Send {
None
}
}

pub trait NetSocketService: SocketService {
fn process_packet(&self, packet: Packet<Ip>);
fn port_unreachable(&self, port: u32, dst_port: u32);
fn src_port(&self) -> u32;
fn target(&self) -> Ip4;
fn set_src_port(&self, _src_port: u32) {}
}
8 changes: 4 additions & 4 deletions cykusz-rs/src/kernel/net/tcp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bit_field::BitField;
use syscall_defs::net::{NetU16, NetU32};

use crate::kernel::net::ip::{Ip, Ip4, IpHeader, IpType};
use crate::kernel::net::socket::SocketService;
use crate::kernel::net::socket::NetSocketService;
use crate::kernel::net::tcp::socket::TcpFlags;
use crate::kernel::net::util::checksum;
use crate::kernel::net::{
Expand Down Expand Up @@ -272,10 +272,10 @@ pub fn port_unreachable(port: u32, dst_port: u32, _dst_ip: Ip4) {
}
}

static HANDLERS: RwSpin<BTreeMap<(u32, Ip4), Arc<dyn SocketService>>> =
static HANDLERS: RwSpin<BTreeMap<(u32, Ip4), Arc<dyn NetSocketService>>> =
RwSpin::new(BTreeMap::new());

pub fn register_handler(handler: Arc<dyn SocketService>) -> Option<u32> {
pub fn register_handler(handler: Arc<dyn NetSocketService>) -> Option<u32> {
let port = handler.src_port();
if port == 0 {
let port = register_ephemeral_handler(handler)?;
Expand All @@ -296,7 +296,7 @@ pub fn register_handler(handler: Arc<dyn SocketService>) -> Option<u32> {
None
}

pub fn register_ephemeral_handler(handler: Arc<dyn SocketService>) -> Option<u32> {
pub fn register_ephemeral_handler(handler: Arc<dyn NetSocketService>) -> Option<u32> {
let target = handler.target();
let mut handlers = HANDLERS.write();

Expand Down
105 changes: 54 additions & 51 deletions cykusz-rs/src/kernel/net/tcp/socket.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloc::sync::{Arc, Weak};
use alloc::vec::Vec;

use syscall_defs::net::{MsgFlags, MsgHdr, SockAddr, SockAddrIn, SockOption};
use syscall_defs::net::{MsgFlags, MsgHdr, SockAddrIn, SockAddrPtr, SockOption};
use syscall_defs::poll::PollEventFlags;
use syscall_defs::stat::Stat;
use syscall_defs::{OpenFlags, SyscallError, SyscallResult};
Expand All @@ -11,7 +11,7 @@ use crate::kernel::fs::poll::PollTable;
use crate::kernel::fs::vfs::{FsError, Result};
use crate::kernel::mm::PAGE_SIZE;
use crate::kernel::net::ip::{Ip, Ip4, IpHeader};
use crate::kernel::net::socket::SocketService;
use crate::kernel::net::socket::{NetSocketService, SocketService};
use crate::kernel::net::tcp::{Tcp, TcpHeader};
use crate::kernel::net::{
default_driver, Packet, PacketDownHierarchy, PacketHeader, PacketTrait, PacketUpHierarchy,
Expand Down Expand Up @@ -1061,31 +1061,6 @@ impl INode for Socket {
}

impl SocketService for Socket {
fn process_packet(&self, packet: Packet<Ip>) {
logln5!("process packet start");
let mut data = self.data.lock();
logln5!("process packet locked");

if !data.is_listening() {
data.process(packet.upgrade(), &self.in_buffer);

if data.state == State::Closed {
self.in_buffer.readers_queue().notify_all();
}
} else {
if let Some(sock) = data.process_new_connection(packet.upgrade()) {
self.connections.lock().connections.push(sock);
self.connections_wq.notify_all();
}
}
}

fn port_unreachable(&self, _port: u32, dst_port: u32) {
println!("Failed to send to port {}", dst_port);

self.data.lock().finalize();
}

fn listen(&self, backlog: i32) -> SyscallResult {
self.init(true);

Expand All @@ -1098,7 +1073,7 @@ impl SocketService for Socket {

fn accept(
&self,
sock_addr: Option<&mut SockAddr>,
mut sock_addr: SockAddrPtr,
_addrlen: Option<&mut u32>,
) -> core::result::Result<Arc<dyn SocketService>, SyscallError> {
let mut lock = self
Expand All @@ -1108,33 +1083,34 @@ impl SocketService for Socket {
})?
.unwrap();

if let Some(addr) = sock_addr {
*addr = SockAddrIn::new(self.dst_port(), self.target().into()).into_sock_addr();
if !sock_addr.is_null() {
let addr = sock_addr.as_sock_addr_in_mut();
*addr = SockAddrIn::new(self.dst_port(), self.target().into());
}

Ok(lock.connections.pop().unwrap())
}

fn bind(&self, sock_addr: &SockAddr, addrlen: u32) -> SyscallResult {
fn bind(&self, sock_addr: SockAddrPtr, addrlen: u32) -> SyscallResult {
logln5!("tcp bind");
if addrlen as usize != core::mem::size_of::<SockAddr>() {
let sock_addr = sock_addr.as_sock_addr_in();
if addrlen as usize != core::mem::size_of::<SockAddrIn>() {
return Err(SyscallError::EINVAL);
}
self.set_src_port(sock_addr.as_sock_addr_in().port());
self.set_src_port(sock_addr.port());

Ok(0)
}

fn connect(&self, sock_addr: &SockAddr, addrlen: u32) -> SyscallResult {
fn connect(&self, sock_addr: SockAddrPtr, addrlen: u32) -> SyscallResult {
logln5!("tcp connect");
if addrlen as usize != core::mem::size_of::<SockAddr>() {
let sock_addr = sock_addr.as_sock_addr_in();
if addrlen as usize != core::mem::size_of::<SockAddrIn>() {
return Err(SyscallError::EINVAL);
}

let addr_in = sock_addr.as_sock_addr_in();

self.set_dst_port(addr_in.port());
self.set_target(addr_in.sin_addr.s_addr.into());
self.set_dst_port(sock_addr.port());
self.set_target(sock_addr.sin_addr.s_addr.into());

self.in_buffer.init_size(PAGE_SIZE * 18);
self.init(false);
Expand Down Expand Up @@ -1191,18 +1167,6 @@ impl SocketService for Socket {
return Ok(total);
}

fn src_port(&self) -> u32 {
self.src_port() as u32
}

fn target(&self) -> Ip4 {
self.target()
}

fn set_src_port(&self, src_port: u32) {
self.set_src_port(src_port as u16);
}

fn get_socket_option(
&self,
_layer: i32,
Expand All @@ -1219,6 +1183,45 @@ impl SocketService for Socket {
}
}

impl NetSocketService for Socket {
fn process_packet(&self, packet: Packet<Ip>) {
logln5!("process packet start");
let mut data = self.data.lock();
logln5!("process packet locked");

if !data.is_listening() {
data.process(packet.upgrade(), &self.in_buffer);

if data.state == State::Closed {
self.in_buffer.readers_queue().notify_all();
}
} else {
if let Some(sock) = data.process_new_connection(packet.upgrade()) {
self.connections.lock().connections.push(sock);
self.connections_wq.notify_all();
}
}
}

fn port_unreachable(&self, _port: u32, dst_port: u32) {
println!("Failed to send to port {}", dst_port);

self.data.lock().finalize();
}

fn src_port(&self) -> u32 {
self.src_port() as u32
}

fn target(&self) -> Ip4 {
self.target()
}

fn set_src_port(&self, src_port: u32) {
self.set_src_port(src_port as u16);
}
}

impl Drop for Socket {
fn drop(&mut self) {
self.data.lock().close();
Expand Down
8 changes: 4 additions & 4 deletions cykusz-rs/src/kernel/net/udp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use alloc::sync::Arc;
use syscall_defs::net::NetU16;

use crate::kernel::net::ip::{Ip, Ip4, IpHeader, IpType};
use crate::kernel::net::socket::SocketService;
use crate::kernel::net::socket::NetSocketService;
use crate::kernel::net::util::checksum;
use crate::kernel::net::{
ConstPacketKind, Packet, PacketDownHierarchy, PacketHeader, PacketUpHierarchy,
Expand Down Expand Up @@ -109,9 +109,9 @@ pub fn port_unreachable(port: u32, dst_port: u32) {
}
}

static HANDLERS: RwSpin<BTreeMap<u32, Arc<dyn SocketService>>> = RwSpin::new(BTreeMap::new());
static HANDLERS: RwSpin<BTreeMap<u32, Arc<dyn NetSocketService>>> = RwSpin::new(BTreeMap::new());

pub fn register_handler(handler: Arc<dyn SocketService>) -> Option<u32> {
pub fn register_handler(handler: Arc<dyn NetSocketService>) -> Option<u32> {
let port = handler.src_port();

if port == 0 {
Expand All @@ -129,7 +129,7 @@ pub fn register_handler(handler: Arc<dyn SocketService>) -> Option<u32> {
None
}

pub fn register_ephemeral_handler(handler: Arc<dyn SocketService>) -> Option<u32> {
pub fn register_ephemeral_handler(handler: Arc<dyn NetSocketService>) -> Option<u32> {
let mut handlers = HANDLERS.write();

for p in 49152..=65535 {
Expand Down
Loading

0 comments on commit d38af51

Please sign in to comment.