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-
2018use alloc:: boxed:: Box ;
2119use alloc:: sync:: Arc ;
2220use alloc:: vec:: Vec ;
2321use spin:: RwLock ;
2422
2523pub mod arp;
26- pub mod ethernet;
2724pub mod tcp;
2825pub mod udp;
2926
30- pub use ethernet:: Eth ;
3127use netstack:: data_link:: MacAddr ;
3228
33- use crate :: mem:: paging:: VirtAddr ;
3429use crate :: userland:: scheduler;
3530use crate :: userland:: task:: Task ;
3631use crate :: utils:: dma:: DmaAllocator ;
3732
3833use netstack:: network:: Ipv4Addr ;
3934
40- // #[downcastable]
35+ #[ downcastable]
4136pub 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-
212104static DEVICES : RwLock < Vec < Arc < NetworkDevice > > > = RwLock :: new ( Vec :: new ( ) ) ;
213105static DEFAULT_DEVICE : RwLock < Option < Arc < NetworkDevice > > > = RwLock :: new ( None ) ;
214106
215107fn 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