Skip to content

Commit

Permalink
misc(net): cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Anhad Singh <[email protected]>
  • Loading branch information
Andy-Python-Programmer committed Sep 19, 2023
1 parent 8e52f9c commit 7681240
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 251 deletions.
2 changes: 1 addition & 1 deletion src/aero_kernel/src/drivers/e1000.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::userland::scheduler;
use crate::utils::dma::DmaAllocator;
use crate::utils::sync::{Mutex, WaitQueue};

use crate::net::{self, ethernet, NetworkDevice, NetworkDriver};
use crate::net::{self, NetworkDevice, NetworkDriver};
use netstack::data_link::MacAddr;

const TX_DESC_NUM: u32 = 32;
Expand Down
69 changes: 21 additions & 48 deletions src/aero_kernel/src/net/arp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@

use alloc::collections::BTreeMap;
use alloc::vec::Vec;
use byte_endian::BigEndian;
use spin::{Once, RwLock};

use crate::net::default_device;
use crate::net::shim::PacketSend;

use netstack::data_link::{Arp, ArpAddress, ArpHardwareType, ArpOpcode, Eth, EthType};
use netstack::data_link::{Arp, ArpAddress, ArpHardwareType, ArpOpcode, EthType};
use netstack::network::Ipv4Addr;

// use super::{ConstPacketKind, Eth, Packet, PacketDownHierarchy, PacketHeader};
Expand Down Expand Up @@ -68,7 +67,7 @@ impl Cache {
log::trace!("[ ARP ] (!!) Sending queued packed to {ip:?} {mac:?}");
// packet.header_mut().dest_mac = mac;
// super::default_device().send(packet);
unreachable!()
todo!()
}
}
} else {
Expand Down Expand Up @@ -116,49 +115,6 @@ pub fn init() {
});
}

/// Hardware Address Space (e.g., Ethernet, Packet Radio Net.)
#[derive(Copy, Clone)]
#[repr(u16)]
pub enum HType {
Ethernet = 1u16.swap_bytes(),
}

/// Internetwork Protocol for which the ARP request is intended.
#[derive(Copy, Clone)]
#[repr(u16)]
pub enum PType {
Ipv4 = 0x0800u16.swap_bytes(),
}

/// ARP Opcode
#[derive(Copy, Clone, Eq, PartialEq)]
#[repr(u16)]
pub enum Opcode {
Request = 1u16.swap_bytes(),
Reply = 2u16.swap_bytes(),
}

#[repr(C, packed)]
pub struct ArpHeader {
pub htype: HType,
pub ptype: PType,
/// Length (in octets) of a hardware address.
pub hlen: BigEndian<u8>,
/// Length (in octets) of internetwork addresses.
pub plen: BigEndian<u8>,
pub opcode: Opcode,
pub src_mac: MacAddr,
pub src_ip: Ipv4Addr,
pub dest_mac: MacAddr,
pub dest_ip: Ipv4Addr,
}

impl ArpHeader {
pub fn opcode(&self) -> Opcode {
self.opcode
}
}

// #[derive(Debug, Copy, Clone)]
// pub struct Arp {}

Expand Down Expand Up @@ -214,6 +170,24 @@ impl ArpHeader {
// }
// }

pub fn do_recv(arp: &Arp) {
CACHE
.get()
.as_ref()
.expect("arp: cache not initialized")
.write()
.insert(arp.src_ip(), arp.src_mac());

let device = default_device();

if arp.opcode() == ArpOpcode::Request && arp.dest_ip() == device.ip() {
let addr = ArpAddress::new(arp.src_mac(), arp.src_ip());
let reply_arp = make_arp(ArpOpcode::Reply, addr);

reply_arp.send();
}
}

pub fn request_ip(target: Ipv4Addr, to: RawPacket) {
let arp = make_arp(ArpOpcode::Request, ArpAddress::new(MacAddr::NULL, target));

Expand All @@ -226,8 +200,7 @@ pub fn request_ip(target: Ipv4Addr, to: RawPacket) {
.write()
.request(target, to);

let eth = Eth::new(MacAddr::NULL, MacAddr::BROADCAST, EthType::Arp);
(eth / arp).send();
arp.send();
}

fn make_arp(opcode: ArpOpcode, dest_addr: ArpAddress) -> Arp {
Expand Down
83 changes: 0 additions & 83 deletions src/aero_kernel/src/net/ethernet.rs

This file was deleted.

131 changes: 12 additions & 119 deletions src/aero_kernel/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,24 @@
// You should have received a copy of the GNU General Public License
// along with Aero. If not, see <https://www.gnu.org/licenses/>.

use core::marker::PhantomData;

use alloc::boxed::Box;
use alloc::sync::Arc;
use alloc::vec::Vec;
use spin::RwLock;

pub mod arp;
pub mod ethernet;
pub mod tcp;
pub mod udp;

pub use ethernet::Eth;
use netstack::data_link::MacAddr;

use crate::mem::paging::VirtAddr;
use crate::userland::scheduler;
use crate::userland::task::Task;
use crate::utils::dma::DmaAllocator;

use netstack::network::Ipv4Addr;

// #[downcastable]
#[downcastable]
pub trait NetworkDriver: Send + Sync {
fn send(&self, packet: Box<[u8], DmaAllocator>);
fn recv(&self) -> RecvPacket;
Expand Down Expand Up @@ -106,114 +101,11 @@ impl<'a> Drop for RecvPacket<'a> {
}
}

pub trait PacketKind {}

pub trait ConstPacketKind: PacketKind {
const HSIZE: usize;
}

impl<T: ConstPacketKind> PacketKind for T {}

impl<U, D> PacketDownHierarchy<D> for Packet<U>
where
U: PacketKind,
D: ConstPacketKind,
Packet<D>: PacketUpHierarchy<U>,
{
}

pub trait PacketBaseTrait {
fn addr(&self) -> VirtAddr;
fn len(&self) -> usize;
}

pub trait PacketTrait: PacketBaseTrait {
fn header_size(&self) -> usize;

// TODO: Rename as_slice{_mut} to payload{_mut}?
fn as_slice_mut(&mut self) -> &mut [u8] {
let hsize = self.header_size();

let start = self.addr() + hsize;
let size = self.len() - hsize;

unsafe { core::slice::from_raw_parts_mut(start.as_mut_ptr(), size) }
}

fn as_slice(&self) -> &[u8] {
let hsize = self.header_size();

let start = self.addr() + hsize;
let size = self.len() - hsize;

unsafe { core::slice::from_raw_parts(start.as_ptr(), size) }
}
}

impl<T: ConstPacketKind> PacketTrait for Packet<T> {
fn header_size(&self) -> usize {
T::HSIZE
}
}

#[derive(Debug, Copy, Clone)]
pub struct Packet<T: PacketKind> {
pub addr: VirtAddr,
pub len: usize,
_phantom: PhantomData<T>,
}

impl<T: PacketKind> PacketBaseTrait for Packet<T> {
fn addr(&self) -> VirtAddr {
self.addr
}

fn len(&self) -> usize {
self.len
}
}

impl<T: PacketKind> Packet<T> {
pub fn new(addr: VirtAddr, len: usize) -> Packet<T> {
Packet::<T> {
addr,
len,
_phantom: PhantomData,
}
}
}

pub trait PacketUpHierarchy<B: PacketKind>: PacketTrait {
fn upgrade(&self) -> Packet<B> {
let header_size = self.header_size();
Packet::<B>::new(self.addr() + header_size, self.len() - header_size)
}
}

pub trait PacketDownHierarchy<B: ConstPacketKind>: PacketBaseTrait {
fn downgrade(&self) -> Packet<B> {
let header_size = B::HSIZE;
Packet::<B>::new(self.addr() - header_size, self.len() + header_size)
}
}

pub trait PacketHeader<H>: PacketBaseTrait {
fn recv(&self);

fn header(&self) -> &H {
self.addr().read_mut::<H>().unwrap()
}

fn header_mut(&mut self) -> &mut H {
self.addr().read_mut::<H>().unwrap()
}
}

static DEVICES: RwLock<Vec<Arc<NetworkDevice>>> = RwLock::new(Vec::new());
static DEFAULT_DEVICE: RwLock<Option<Arc<NetworkDevice>>> = RwLock::new(None);

fn packet_processor_thread() {
use netstack::data_link::{Eth, EthType};
use netstack::data_link::{Arp, Eth, EthType};
use netstack::network::{Ipv4, Ipv4Type};
use netstack::transport::Udp;
use netstack::PacketParser;
Expand All @@ -236,7 +128,9 @@ fn packet_processor_thread() {
}
}

EthType::Arp => todo!(),
EthType::Arp => {
arp::do_recv(parser.next::<Arp>());
}
}
}
}
Expand Down Expand Up @@ -282,7 +176,7 @@ pub mod shim {
use crate::net::{self, arp};
use crate::utils::dma::DmaAllocator;

use netstack::data_link::{Arp, Eth};
use netstack::data_link::{Arp, Eth, EthType, MacAddr};
use netstack::network::Ipv4;
use netstack::{IntoBoxedBytes, Protocol, Stacked};

Expand Down Expand Up @@ -310,16 +204,15 @@ pub mod shim {
}
}

impl PacketSend for Stacked<Eth, Arp> {
impl PacketSend for Arp {
fn send(self) {
// let device = net::default_device();
let device = net::default_device();

// let eth = &mut self.upper;
// let arp = &mut self.lower;
let eth = Eth::new(MacAddr::NULL, MacAddr::BROADCAST, EthType::Arp)
.set_dest_mac(self.dest_mac())
.set_src_mac(device.mac());

// eth.src_mac = device.mac();
// eth.dest_mac = arp.dest_mac;
todo!()
device.send((eth / self).into_boxed_bytes_in(DmaAllocator));
}
}

Expand Down

0 comments on commit 7681240

Please sign in to comment.