Skip to content

Commit 7681240

Browse files
misc(net): cleanup
Signed-off-by: Anhad Singh <[email protected]>
1 parent 8e52f9c commit 7681240

File tree

4 files changed

+34
-251
lines changed

4 files changed

+34
-251
lines changed

src/aero_kernel/src/drivers/e1000.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::userland::scheduler;
2727
use crate::utils::dma::DmaAllocator;
2828
use crate::utils::sync::{Mutex, WaitQueue};
2929

30-
use crate::net::{self, ethernet, NetworkDevice, NetworkDriver};
30+
use crate::net::{self, NetworkDevice, NetworkDriver};
3131
use netstack::data_link::MacAddr;
3232

3333
const TX_DESC_NUM: u32 = 32;

src/aero_kernel/src/net/arp.rs

Lines changed: 21 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@
1919
2020
use alloc::collections::BTreeMap;
2121
use alloc::vec::Vec;
22-
use byte_endian::BigEndian;
2322
use spin::{Once, RwLock};
2423

2524
use crate::net::default_device;
2625
use crate::net::shim::PacketSend;
2726

28-
use netstack::data_link::{Arp, ArpAddress, ArpHardwareType, ArpOpcode, Eth, EthType};
27+
use netstack::data_link::{Arp, ArpAddress, ArpHardwareType, ArpOpcode, EthType};
2928
use netstack::network::Ipv4Addr;
3029

3130
// use super::{ConstPacketKind, Eth, Packet, PacketDownHierarchy, PacketHeader};
@@ -68,7 +67,7 @@ impl Cache {
6867
log::trace!("[ ARP ] (!!) Sending queued packed to {ip:?} {mac:?}");
6968
// packet.header_mut().dest_mac = mac;
7069
// super::default_device().send(packet);
71-
unreachable!()
70+
todo!()
7271
}
7372
}
7473
} else {
@@ -116,49 +115,6 @@ pub fn init() {
116115
});
117116
}
118117

119-
/// Hardware Address Space (e.g., Ethernet, Packet Radio Net.)
120-
#[derive(Copy, Clone)]
121-
#[repr(u16)]
122-
pub enum HType {
123-
Ethernet = 1u16.swap_bytes(),
124-
}
125-
126-
/// Internetwork Protocol for which the ARP request is intended.
127-
#[derive(Copy, Clone)]
128-
#[repr(u16)]
129-
pub enum PType {
130-
Ipv4 = 0x0800u16.swap_bytes(),
131-
}
132-
133-
/// ARP Opcode
134-
#[derive(Copy, Clone, Eq, PartialEq)]
135-
#[repr(u16)]
136-
pub enum Opcode {
137-
Request = 1u16.swap_bytes(),
138-
Reply = 2u16.swap_bytes(),
139-
}
140-
141-
#[repr(C, packed)]
142-
pub struct ArpHeader {
143-
pub htype: HType,
144-
pub ptype: PType,
145-
/// Length (in octets) of a hardware address.
146-
pub hlen: BigEndian<u8>,
147-
/// Length (in octets) of internetwork addresses.
148-
pub plen: BigEndian<u8>,
149-
pub opcode: Opcode,
150-
pub src_mac: MacAddr,
151-
pub src_ip: Ipv4Addr,
152-
pub dest_mac: MacAddr,
153-
pub dest_ip: Ipv4Addr,
154-
}
155-
156-
impl ArpHeader {
157-
pub fn opcode(&self) -> Opcode {
158-
self.opcode
159-
}
160-
}
161-
162118
// #[derive(Debug, Copy, Clone)]
163119
// pub struct Arp {}
164120

@@ -214,6 +170,24 @@ impl ArpHeader {
214170
// }
215171
// }
216172

173+
pub fn do_recv(arp: &Arp) {
174+
CACHE
175+
.get()
176+
.as_ref()
177+
.expect("arp: cache not initialized")
178+
.write()
179+
.insert(arp.src_ip(), arp.src_mac());
180+
181+
let device = default_device();
182+
183+
if arp.opcode() == ArpOpcode::Request && arp.dest_ip() == device.ip() {
184+
let addr = ArpAddress::new(arp.src_mac(), arp.src_ip());
185+
let reply_arp = make_arp(ArpOpcode::Reply, addr);
186+
187+
reply_arp.send();
188+
}
189+
}
190+
217191
pub fn request_ip(target: Ipv4Addr, to: RawPacket) {
218192
let arp = make_arp(ArpOpcode::Request, ArpAddress::new(MacAddr::NULL, target));
219193

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

229-
let eth = Eth::new(MacAddr::NULL, MacAddr::BROADCAST, EthType::Arp);
230-
(eth / arp).send();
203+
arp.send();
231204
}
232205

233206
fn make_arp(opcode: ArpOpcode, dest_addr: ArpAddress) -> Arp {

src/aero_kernel/src/net/ethernet.rs

Lines changed: 0 additions & 83 deletions
This file was deleted.

src/aero_kernel/src/net/mod.rs

Lines changed: 12 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,24 @@
1515
// You should have received a copy of the GNU General Public License
1616
// along with Aero. If not, see <https://www.gnu.org/licenses/>.
1717

18-
use core::marker::PhantomData;
19-
2018
use alloc::boxed::Box;
2119
use alloc::sync::Arc;
2220
use alloc::vec::Vec;
2321
use spin::RwLock;
2422

2523
pub mod arp;
26-
pub mod ethernet;
2724
pub mod tcp;
2825
pub mod udp;
2926

30-
pub use ethernet::Eth;
3127
use netstack::data_link::MacAddr;
3228

33-
use crate::mem::paging::VirtAddr;
3429
use crate::userland::scheduler;
3530
use crate::userland::task::Task;
3631
use crate::utils::dma::DmaAllocator;
3732

3833
use netstack::network::Ipv4Addr;
3934

40-
// #[downcastable]
35+
#[downcastable]
4136
pub trait NetworkDriver: Send + Sync {
4237
fn send(&self, packet: Box<[u8], DmaAllocator>);
4338
fn recv(&self) -> RecvPacket;
@@ -106,114 +101,11 @@ impl<'a> Drop for RecvPacket<'a> {
106101
}
107102
}
108103

109-
pub trait PacketKind {}
110-
111-
pub trait ConstPacketKind: PacketKind {
112-
const HSIZE: usize;
113-
}
114-
115-
impl<T: ConstPacketKind> PacketKind for T {}
116-
117-
impl<U, D> PacketDownHierarchy<D> for Packet<U>
118-
where
119-
U: PacketKind,
120-
D: ConstPacketKind,
121-
Packet<D>: PacketUpHierarchy<U>,
122-
{
123-
}
124-
125-
pub trait PacketBaseTrait {
126-
fn addr(&self) -> VirtAddr;
127-
fn len(&self) -> usize;
128-
}
129-
130-
pub trait PacketTrait: PacketBaseTrait {
131-
fn header_size(&self) -> usize;
132-
133-
// TODO: Rename as_slice{_mut} to payload{_mut}?
134-
fn as_slice_mut(&mut self) -> &mut [u8] {
135-
let hsize = self.header_size();
136-
137-
let start = self.addr() + hsize;
138-
let size = self.len() - hsize;
139-
140-
unsafe { core::slice::from_raw_parts_mut(start.as_mut_ptr(), size) }
141-
}
142-
143-
fn as_slice(&self) -> &[u8] {
144-
let hsize = self.header_size();
145-
146-
let start = self.addr() + hsize;
147-
let size = self.len() - hsize;
148-
149-
unsafe { core::slice::from_raw_parts(start.as_ptr(), size) }
150-
}
151-
}
152-
153-
impl<T: ConstPacketKind> PacketTrait for Packet<T> {
154-
fn header_size(&self) -> usize {
155-
T::HSIZE
156-
}
157-
}
158-
159-
#[derive(Debug, Copy, Clone)]
160-
pub struct Packet<T: PacketKind> {
161-
pub addr: VirtAddr,
162-
pub len: usize,
163-
_phantom: PhantomData<T>,
164-
}
165-
166-
impl<T: PacketKind> PacketBaseTrait for Packet<T> {
167-
fn addr(&self) -> VirtAddr {
168-
self.addr
169-
}
170-
171-
fn len(&self) -> usize {
172-
self.len
173-
}
174-
}
175-
176-
impl<T: PacketKind> Packet<T> {
177-
pub fn new(addr: VirtAddr, len: usize) -> Packet<T> {
178-
Packet::<T> {
179-
addr,
180-
len,
181-
_phantom: PhantomData,
182-
}
183-
}
184-
}
185-
186-
pub trait PacketUpHierarchy<B: PacketKind>: PacketTrait {
187-
fn upgrade(&self) -> Packet<B> {
188-
let header_size = self.header_size();
189-
Packet::<B>::new(self.addr() + header_size, self.len() - header_size)
190-
}
191-
}
192-
193-
pub trait PacketDownHierarchy<B: ConstPacketKind>: PacketBaseTrait {
194-
fn downgrade(&self) -> Packet<B> {
195-
let header_size = B::HSIZE;
196-
Packet::<B>::new(self.addr() - header_size, self.len() + header_size)
197-
}
198-
}
199-
200-
pub trait PacketHeader<H>: PacketBaseTrait {
201-
fn recv(&self);
202-
203-
fn header(&self) -> &H {
204-
self.addr().read_mut::<H>().unwrap()
205-
}
206-
207-
fn header_mut(&mut self) -> &mut H {
208-
self.addr().read_mut::<H>().unwrap()
209-
}
210-
}
211-
212104
static DEVICES: RwLock<Vec<Arc<NetworkDevice>>> = RwLock::new(Vec::new());
213105
static DEFAULT_DEVICE: RwLock<Option<Arc<NetworkDevice>>> = RwLock::new(None);
214106

215107
fn packet_processor_thread() {
216-
use netstack::data_link::{Eth, EthType};
108+
use netstack::data_link::{Arp, Eth, EthType};
217109
use netstack::network::{Ipv4, Ipv4Type};
218110
use netstack::transport::Udp;
219111
use netstack::PacketParser;
@@ -236,7 +128,9 @@ fn packet_processor_thread() {
236128
}
237129
}
238130

239-
EthType::Arp => todo!(),
131+
EthType::Arp => {
132+
arp::do_recv(parser.next::<Arp>());
133+
}
240134
}
241135
}
242136
}
@@ -282,7 +176,7 @@ pub mod shim {
282176
use crate::net::{self, arp};
283177
use crate::utils::dma::DmaAllocator;
284178

285-
use netstack::data_link::{Arp, Eth};
179+
use netstack::data_link::{Arp, Eth, EthType, MacAddr};
286180
use netstack::network::Ipv4;
287181
use netstack::{IntoBoxedBytes, Protocol, Stacked};
288182

@@ -310,16 +204,15 @@ pub mod shim {
310204
}
311205
}
312206

313-
impl PacketSend for Stacked<Eth, Arp> {
207+
impl PacketSend for Arp {
314208
fn send(self) {
315-
// let device = net::default_device();
209+
let device = net::default_device();
316210

317-
// let eth = &mut self.upper;
318-
// let arp = &mut self.lower;
211+
let eth = Eth::new(MacAddr::NULL, MacAddr::BROADCAST, EthType::Arp)
212+
.set_dest_mac(self.dest_mac())
213+
.set_src_mac(device.mac());
319214

320-
// eth.src_mac = device.mac();
321-
// eth.dest_mac = arp.dest_mac;
322-
todo!()
215+
device.send((eth / self).into_boxed_bytes_in(DmaAllocator));
323216
}
324217
}
325218

0 commit comments

Comments
 (0)