15
15
// You should have received a copy of the GNU General Public License
16
16
// along with Aero. If not, see <https://www.gnu.org/licenses/>.
17
17
18
- use core:: marker:: PhantomData ;
19
-
20
18
use alloc:: boxed:: Box ;
21
19
use alloc:: sync:: Arc ;
22
20
use alloc:: vec:: Vec ;
23
21
use spin:: RwLock ;
24
22
25
23
pub mod arp;
26
- pub mod ethernet;
27
24
pub mod tcp;
28
25
pub mod udp;
29
26
30
- pub use ethernet:: Eth ;
31
27
use netstack:: data_link:: MacAddr ;
32
28
33
- use crate :: mem:: paging:: VirtAddr ;
34
29
use crate :: userland:: scheduler;
35
30
use crate :: userland:: task:: Task ;
36
31
use crate :: utils:: dma:: DmaAllocator ;
37
32
38
33
use netstack:: network:: Ipv4Addr ;
39
34
40
- // #[downcastable]
35
+ #[ downcastable]
41
36
pub trait NetworkDriver : Send + Sync {
42
37
fn send ( & self , packet : Box < [ u8 ] , DmaAllocator > ) ;
43
38
fn recv ( & self ) -> RecvPacket ;
@@ -106,114 +101,11 @@ impl<'a> Drop for RecvPacket<'a> {
106
101
}
107
102
}
108
103
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
-
212
104
static DEVICES : RwLock < Vec < Arc < NetworkDevice > > > = RwLock :: new ( Vec :: new ( ) ) ;
213
105
static DEFAULT_DEVICE : RwLock < Option < Arc < NetworkDevice > > > = RwLock :: new ( None ) ;
214
106
215
107
fn packet_processor_thread ( ) {
216
- use netstack:: data_link:: { Eth , EthType } ;
108
+ use netstack:: data_link:: { Arp , Eth , EthType } ;
217
109
use netstack:: network:: { Ipv4 , Ipv4Type } ;
218
110
use netstack:: transport:: Udp ;
219
111
use netstack:: PacketParser ;
@@ -236,7 +128,9 @@ fn packet_processor_thread() {
236
128
}
237
129
}
238
130
239
- EthType :: Arp => todo ! ( ) ,
131
+ EthType :: Arp => {
132
+ arp:: do_recv ( parser. next :: < Arp > ( ) ) ;
133
+ }
240
134
}
241
135
}
242
136
}
@@ -282,7 +176,7 @@ pub mod shim {
282
176
use crate :: net:: { self , arp} ;
283
177
use crate :: utils:: dma:: DmaAllocator ;
284
178
285
- use netstack:: data_link:: { Arp , Eth } ;
179
+ use netstack:: data_link:: { Arp , Eth , EthType , MacAddr } ;
286
180
use netstack:: network:: Ipv4 ;
287
181
use netstack:: { IntoBoxedBytes , Protocol , Stacked } ;
288
182
@@ -310,16 +204,15 @@ pub mod shim {
310
204
}
311
205
}
312
206
313
- impl PacketSend for Stacked < Eth , Arp > {
207
+ impl PacketSend for Arp {
314
208
fn send ( self ) {
315
- // let device = net::default_device();
209
+ let device = net:: default_device ( ) ;
316
210
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 ( ) ) ;
319
214
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 ) ) ;
323
216
}
324
217
}
325
218
0 commit comments