1 use crate::arch::rand::rand;
2 use crate::driver::base::class::Class;
3 use crate::driver::base::device::bus::Bus;
4 use crate::driver::base::device::driver::Driver;
5 use crate::driver::base::device::{Device, DeviceCommonData, DeviceType, IdTable};
6 use crate::driver::base::kobject::{
7 KObjType, KObject, KObjectCommonData, KObjectState, LockedKObjectState,
8 };
9 use crate::driver::base::kset::KSet;
10 use crate::filesystem::kernfs::KernFSInode;
11 use crate::init::initcall::INITCALL_DEVICE;
12 use crate::libs::rwlock::{RwLockReadGuard, RwLockWriteGuard};
13 use crate::libs::spinlock::{SpinLock, SpinLockGuard};
14 use crate::net::{generate_iface_id, NET_DEVICES};
15 use crate::time::Instant;
16 use alloc::collections::VecDeque;
17 use alloc::fmt::Debug;
18 use alloc::string::{String, ToString};
19 use alloc::sync::{Arc, Weak};
20 use alloc::vec::Vec;
21 use core::cell::UnsafeCell;
22 use core::ops::{Deref, DerefMut};
23 use smoltcp::wire::HardwareAddress;
24 use smoltcp::{
25 phy::{self},
26 wire::{IpAddress, IpCidr},
27 };
28 use system_error::SystemError;
29 use unified_init::macros::unified_init;
30
31 use super::{register_netdevice, NetDeivceState, NetDevice, NetDeviceCommonData, Operstate};
32
33 const DEVICE_NAME: &str = "loopback";
34
35 /// ## 环回接收令牌
36 /// 用于储存lo网卡接收到的数据
37 pub struct LoopbackRxToken {
38 buffer: Vec<u8>,
39 }
40
41 impl phy::RxToken for LoopbackRxToken {
42 /// ## 实现Rxtoken的consume函数
43 /// 接受一个函数 `f`,并在 `self.buffer` 上调用它。
44 ///
45 /// ## 参数
46 /// - mut self :一个可变的 `LoopbackRxToken` 实例。
47 /// - f :接受一个可变的 u8 切片,并返回类型 `R` 的结果。
48 ///
49 /// ## 返回值
50 /// 返回函数 `f` 在 `self.buffer` 上的调用结果。
consume<R, F>(mut self, f: F) -> R where F: FnOnce(&mut [u8]) -> R,51 fn consume<R, F>(mut self, f: F) -> R
52 where
53 F: FnOnce(&mut [u8]) -> R,
54 {
55 f(self.buffer.as_mut_slice())
56 }
57 }
58
59 /// ## 环回发送令牌
60 /// 返回驱动用于操作lo设备
61 pub struct LoopbackTxToken {
62 driver: LoopbackDriver,
63 }
64
65 impl phy::TxToken for LoopbackTxToken {
66 /// ## 实现TxToken的consume函数
67 /// 向lo的队列推入待发送的数据报,实现环回
68 ///
69 /// ## 参数
70 /// - self
71 /// - len:数据包的长度
72 /// - f:接受一个可变的 u8 切片,并返回类型 `R` 的结果。
73 ///
74 /// ## 返回值
75 /// 返回f对数据包操纵的结果
consume<R, F>(self, len: usize, f: F) -> R where F: FnOnce(&mut [u8]) -> R,76 fn consume<R, F>(self, len: usize, f: F) -> R
77 where
78 F: FnOnce(&mut [u8]) -> R,
79 {
80 let mut buffer = vec![0; len];
81 let result = f(buffer.as_mut_slice());
82 let mut device = self.driver.inner.lock();
83 device.loopback_transmit(buffer);
84 result
85 }
86 }
87
88 /// ## Loopback设备
89 /// 成员是一个队列,用来存放接受到的数据包。
90 /// 当使用lo发送数据包时,不会把数据包传到link层,而是直接发送到该队列,实现环回。
91 pub struct Loopback {
92 //回环设备的缓冲区,接受的数据包会存放在这里,发送的数据包也会发送到这里,实现环回
93 queue: VecDeque<Vec<u8>>,
94 }
95
96 impl Loopback {
97 /// ## Loopback创建函数
98 /// 创建lo设备
new() -> Self99 pub fn new() -> Self {
100 let queue = VecDeque::new();
101 Loopback { queue }
102 }
103 /// ## Loopback处理接受到的数据包函数
104 /// Loopback接受到数据后会调用这个函数来弹出接收的数据,返回给协议栈
105 ///
106 /// ## 参数
107 /// - &mut self :自身可变引用
108 ///
109 /// ## 返回值
110 /// - queue的头部数据包
loopback_receive(&mut self) -> Vec<u8>111 pub fn loopback_receive(&mut self) -> Vec<u8> {
112 let buffer = self.queue.pop_front();
113 match buffer {
114 Some(buffer) => {
115 //debug!("lo receive:{:?}", buffer);
116 return buffer;
117 }
118 None => {
119 return Vec::new();
120 }
121 }
122 }
123 /// ## Loopback发送数据包的函数
124 /// Loopback发送数据包给自己的接收队列,实现环回
125 ///
126 /// ## 参数
127 /// - &mut self:自身可变引用
128 /// - buffer:需要发送的数据包
loopback_transmit(&mut self, buffer: Vec<u8>)129 pub fn loopback_transmit(&mut self, buffer: Vec<u8>) {
130 //debug!("lo transmit!");
131 self.queue.push_back(buffer)
132 }
133 }
134
135 /// ## driver的包裹器
136 /// 为实现获得不可变引用的Interface的内部可变性,故为Driver提供UnsafeCell包裹器
137 ///
138 /// 参考virtio_net.rs
139 struct LoopbackDriverWapper(UnsafeCell<LoopbackDriver>);
140 unsafe impl Send for LoopbackDriverWapper {}
141 unsafe impl Sync for LoopbackDriverWapper {}
142
143 /// ## deref 方法返回一个指向 `LoopbackDriver` 的引用。
144 impl Deref for LoopbackDriverWapper {
145 type Target = LoopbackDriver;
deref(&self) -> &Self::Target146 fn deref(&self) -> &Self::Target {
147 unsafe { &*self.0.get() }
148 }
149 }
150 /// ## `deref_mut` 方法返回一个指向可变 `LoopbackDriver` 的引用。
151 impl DerefMut for LoopbackDriverWapper {
deref_mut(&mut self) -> &mut Self::Target152 fn deref_mut(&mut self) -> &mut Self::Target {
153 unsafe { &mut *self.0.get() }
154 }
155 }
156
157 impl LoopbackDriverWapper {
158 /// ## force_get_mut返回一个指向可变 `LoopbackDriver` 的引用。
159 #[allow(clippy::mut_from_ref)]
160 #[allow(clippy::mut_from_ref)]
force_get_mut(&self) -> &mut LoopbackDriver161 fn force_get_mut(&self) -> &mut LoopbackDriver {
162 unsafe { &mut *self.0.get() }
163 }
164 }
165
166 /// ## Loopback驱动
167 /// 负责操作Loopback设备实现基本的网卡功能
168 pub struct LoopbackDriver {
169 pub inner: Arc<SpinLock<Loopback>>,
170 }
171
172 impl LoopbackDriver {
173 /// ## LoopbackDriver创建函数
new() -> Self174 pub fn new() -> Self {
175 let inner = Arc::new(SpinLock::new(Loopback::new()));
176 LoopbackDriver { inner }
177 }
178 }
179
180 impl Clone for LoopbackDriver {
clone(&self) -> Self181 fn clone(&self) -> Self {
182 LoopbackDriver {
183 inner: self.inner.clone(),
184 }
185 }
186 }
187
188 impl phy::Device for LoopbackDriver {
189 type RxToken<'a> = LoopbackRxToken where Self: 'a;
190 type TxToken<'a> = LoopbackTxToken where Self: 'a;
191 /// ## 返回设备的物理层特性。
192 /// lo设备的最大传输单元为65535,最大突发大小为1,传输介质默认为Ethernet
capabilities(&self) -> phy::DeviceCapabilities193 fn capabilities(&self) -> phy::DeviceCapabilities {
194 let mut result = phy::DeviceCapabilities::default();
195 result.max_transmission_unit = 65535;
196 result.max_burst_size = Some(1);
197 result.medium = smoltcp::phy::Medium::Ethernet;
198 return result;
199 }
200 /// ## Loopback驱动处理接受数据事件
201 /// 驱动调用Loopback的receive函数,处理buffer封装成(rx,tx)返回给上层
202 ///
203 /// ## 参数
204 /// - `&mut self` :自身可变引用
205 /// - `_timestamp`
206 ///
207 /// ## 返回值
208 /// - None: 如果接收队列为空,返回 `None`,以通知上层没有可以接收的包
209 /// - Option::Some((rx, tx)):如果接收队列不为空,返回 `Some`,其中包含一个接收令牌 `rx` 和一个发送令牌 `tx`
receive( &mut self, _timestamp: smoltcp::time::Instant, ) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)>210 fn receive(
211 &mut self,
212 _timestamp: smoltcp::time::Instant,
213 ) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
214 let buffer = self.inner.lock().loopback_receive();
215 //receive队列为为空,返回NONE值以通知上层没有可以receive的包
216 if buffer.is_empty() {
217 return Option::None;
218 }
219 let rx = LoopbackRxToken { buffer };
220 let tx = LoopbackTxToken {
221 driver: self.clone(),
222 };
223 return Option::Some((rx, tx));
224 }
225 /// ## Loopback驱动处理发送数据包事件
226 /// Loopback驱动在需要发送数据时会调用这个函数来获取一个发送令牌。
227 ///
228 /// ## 参数
229 /// - `&mut self` :自身可变引用
230 /// - `_timestamp`
231 ///
232 /// ## 返回值
233 /// - 返回一个 `Some`,其中包含一个发送令牌,该令牌包含一个对自身的克隆引用
transmit(&mut self, _timestamp: smoltcp::time::Instant) -> Option<Self::TxToken<'_>>234 fn transmit(&mut self, _timestamp: smoltcp::time::Instant) -> Option<Self::TxToken<'_>> {
235 Some(LoopbackTxToken {
236 driver: self.clone(),
237 })
238 }
239 }
240
241 /// ## LoopbackInterface结构
242 /// 封装驱动包裹器和iface,设置接口名称
243 #[cast_to([sync] NetDevice)]
244 #[cast_to([sync] Device)]
245 pub struct LoopbackInterface {
246 driver: LoopbackDriverWapper,
247 iface_id: usize,
248 iface: SpinLock<smoltcp::iface::Interface>,
249 name: String,
250 inner: SpinLock<InnerLoopbackInterface>,
251 locked_kobj_state: LockedKObjectState,
252 }
253
254 #[derive(Debug)]
255 pub struct InnerLoopbackInterface {
256 netdevice_common: NetDeviceCommonData,
257 device_common: DeviceCommonData,
258 kobj_common: KObjectCommonData,
259 }
260
261 impl LoopbackInterface {
262 /// ## `new` 是一个公共函数,用于创建一个新的 `LoopbackInterface` 实例。
263 /// 生成一个新的接口 ID。创建一个新的接口配置,设置其硬件地址和随机种子,使用接口配置和驱动器创建一个新的 `smoltcp::iface::Interface` 实例。
264 /// 设置接口的 IP 地址为 127.0.0.1。
265 /// 创建一个新的 `LoopbackDriverWapper` 实例,包装驱动器。
266 /// 创建一个新的 `LoopbackInterface` 实例,包含驱动器、接口 ID、接口和名称,并将其封装在一个 `Arc` 中。
267 /// ## 参数
268 /// - `driver`:一个 `LoopbackDriver` 实例,用于驱动网络环回操作。
269 ///
270 /// ## 返回值
271 /// 返回一个 `Arc<Self>`,即一个指向新创建的 `LoopbackInterface` 实例的智能指针。
new(mut driver: LoopbackDriver) -> Arc<Self>272 pub fn new(mut driver: LoopbackDriver) -> Arc<Self> {
273 let iface_id = generate_iface_id();
274 let mut iface_config = smoltcp::iface::Config::new(HardwareAddress::Ethernet(
275 smoltcp::wire::EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]),
276 ));
277 iface_config.random_seed = rand() as u64;
278
279 let mut iface =
280 smoltcp::iface::Interface::new(iface_config, &mut driver, Instant::now().into());
281 //设置网卡地址为127.0.0.1
282 iface.update_ip_addrs(|ip_addrs| {
283 ip_addrs
284 .push(IpCidr::new(IpAddress::v4(127, 0, 0, 1), 8))
285 .unwrap();
286 });
287 let driver = LoopbackDriverWapper(UnsafeCell::new(driver));
288 Arc::new(LoopbackInterface {
289 driver,
290 iface_id,
291 iface: SpinLock::new(iface),
292 name: "lo".to_string(),
293 inner: SpinLock::new(InnerLoopbackInterface {
294 netdevice_common: NetDeviceCommonData::default(),
295 device_common: DeviceCommonData::default(),
296 kobj_common: KObjectCommonData::default(),
297 }),
298 locked_kobj_state: LockedKObjectState::default(),
299 })
300 }
301
inner(&self) -> SpinLockGuard<InnerLoopbackInterface>302 fn inner(&self) -> SpinLockGuard<InnerLoopbackInterface> {
303 return self.inner.lock();
304 }
305 }
306
307 impl Debug for LoopbackInterface {
fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result308 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
309 f.debug_struct("LoopbackInterface")
310 .field("iface_id", &self.iface_id)
311 .field("iface", &"smtoltcp::iface::Interface")
312 .field("name", &self.name)
313 .finish()
314 }
315 }
316
317 impl KObject for LoopbackInterface {
as_any_ref(&self) -> &dyn core::any::Any318 fn as_any_ref(&self) -> &dyn core::any::Any {
319 self
320 }
321
set_inode(&self, inode: Option<Arc<KernFSInode>>)322 fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
323 self.inner().kobj_common.kern_inode = inode;
324 }
325
inode(&self) -> Option<Arc<KernFSInode>>326 fn inode(&self) -> Option<Arc<KernFSInode>> {
327 self.inner().kobj_common.kern_inode.clone()
328 }
329
parent(&self) -> Option<Weak<dyn KObject>>330 fn parent(&self) -> Option<Weak<dyn KObject>> {
331 self.inner().kobj_common.parent.clone()
332 }
333
set_parent(&self, parent: Option<Weak<dyn KObject>>)334 fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
335 self.inner().kobj_common.parent = parent;
336 }
337
kset(&self) -> Option<Arc<KSet>>338 fn kset(&self) -> Option<Arc<KSet>> {
339 self.inner().kobj_common.kset.clone()
340 }
341
set_kset(&self, kset: Option<Arc<KSet>>)342 fn set_kset(&self, kset: Option<Arc<KSet>>) {
343 self.inner().kobj_common.kset = kset;
344 }
345
kobj_type(&self) -> Option<&'static dyn KObjType>346 fn kobj_type(&self) -> Option<&'static dyn KObjType> {
347 self.inner().kobj_common.kobj_type
348 }
349
name(&self) -> String350 fn name(&self) -> String {
351 self.name.clone()
352 }
353
set_name(&self, _name: String)354 fn set_name(&self, _name: String) {
355 // do nothing
356 }
357
kobj_state(&self) -> RwLockReadGuard<KObjectState>358 fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
359 self.locked_kobj_state.read()
360 }
361
kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState>362 fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
363 self.locked_kobj_state.write()
364 }
365
set_kobj_state(&self, state: KObjectState)366 fn set_kobj_state(&self, state: KObjectState) {
367 *self.locked_kobj_state.write() = state;
368 }
369
set_kobj_type(&self, ktype: Option<&'static dyn KObjType>)370 fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
371 self.inner().kobj_common.kobj_type = ktype;
372 }
373 }
374
375 impl Device for LoopbackInterface {
dev_type(&self) -> DeviceType376 fn dev_type(&self) -> DeviceType {
377 DeviceType::Net
378 }
379
id_table(&self) -> IdTable380 fn id_table(&self) -> IdTable {
381 IdTable::new(DEVICE_NAME.to_string(), None)
382 }
383
bus(&self) -> Option<Weak<dyn Bus>>384 fn bus(&self) -> Option<Weak<dyn Bus>> {
385 self.inner().device_common.bus.clone()
386 }
387
set_bus(&self, bus: Option<Weak<dyn Bus>>)388 fn set_bus(&self, bus: Option<Weak<dyn Bus>>) {
389 self.inner().device_common.bus = bus;
390 }
391
class(&self) -> Option<Arc<dyn Class>>392 fn class(&self) -> Option<Arc<dyn Class>> {
393 let mut guard = self.inner();
394 let r = guard.device_common.class.clone()?.upgrade();
395 if r.is_none() {
396 guard.device_common.class = None;
397 }
398
399 return r;
400 }
401
set_class(&self, class: Option<Weak<dyn Class>>)402 fn set_class(&self, class: Option<Weak<dyn Class>>) {
403 self.inner().device_common.class = class;
404 }
405
driver(&self) -> Option<Arc<dyn Driver>>406 fn driver(&self) -> Option<Arc<dyn Driver>> {
407 let r = self.inner().device_common.driver.clone()?.upgrade();
408 if r.is_none() {
409 self.inner().device_common.driver = None;
410 }
411
412 return r;
413 }
414
set_driver(&self, driver: Option<Weak<dyn Driver>>)415 fn set_driver(&self, driver: Option<Weak<dyn Driver>>) {
416 self.inner().device_common.driver = driver;
417 }
418
is_dead(&self) -> bool419 fn is_dead(&self) -> bool {
420 false
421 }
422
can_match(&self) -> bool423 fn can_match(&self) -> bool {
424 self.inner().device_common.can_match
425 }
426
set_can_match(&self, can_match: bool)427 fn set_can_match(&self, can_match: bool) {
428 self.inner().device_common.can_match = can_match;
429 }
430
state_synced(&self) -> bool431 fn state_synced(&self) -> bool {
432 true
433 }
434
dev_parent(&self) -> Option<Weak<dyn Device>>435 fn dev_parent(&self) -> Option<Weak<dyn Device>> {
436 self.inner().device_common.get_parent_weak_or_clear()
437 }
438
set_dev_parent(&self, parent: Option<Weak<dyn Device>>)439 fn set_dev_parent(&self, parent: Option<Weak<dyn Device>>) {
440 self.inner().device_common.parent = parent;
441 }
442 }
443
444 impl NetDevice for LoopbackInterface {
445 /// 由于lo网卡设备不是实际的物理设备,其mac地址需要手动设置为一个默认值,这里默认为00:00:00:00:00
mac(&self) -> smoltcp::wire::EthernetAddress446 fn mac(&self) -> smoltcp::wire::EthernetAddress {
447 let mac = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
448 smoltcp::wire::EthernetAddress(mac)
449 }
450
451 #[inline]
nic_id(&self) -> usize452 fn nic_id(&self) -> usize {
453 self.iface_id
454 }
455
456 #[inline]
iface_name(&self) -> String457 fn iface_name(&self) -> String {
458 self.name.clone()
459 }
460 /// ## `update_ip_addrs` 用于更新接口的 IP 地址。
461 ///
462 /// ## 参数
463 /// - `&self` :自身引用
464 /// - `ip_addrs` :一个包含 `smoltcp::wire::IpCidr` 的切片,表示要设置的 IP 地址和子网掩码
465 ///
466 /// ## 返回值
467 /// - 如果 `ip_addrs` 的长度不为 1,返回 `Err(SystemError::EINVAL)`,表示输入参数无效
468 /// - 如果更新成功,返回 `Ok(())`
update_ip_addrs( &self, ip_addrs: &[smoltcp::wire::IpCidr], ) -> Result<(), system_error::SystemError>469 fn update_ip_addrs(
470 &self,
471 ip_addrs: &[smoltcp::wire::IpCidr],
472 ) -> Result<(), system_error::SystemError> {
473 if ip_addrs.len() != 1 {
474 return Err(SystemError::EINVAL);
475 }
476
477 self.iface.lock().update_ip_addrs(|addrs| {
478 let dest = addrs.iter_mut().next();
479
480 if let Some(dest) = dest {
481 *dest = ip_addrs[0];
482 } else {
483 addrs.push(ip_addrs[0]).expect("Push ipCidr failed: full");
484 }
485 });
486 return Ok(());
487 }
488 /// ## `poll` 用于轮询接口的状态。
489 ///
490 /// ## 参数
491 /// - `&self` :自身引用
492 /// - `sockets` :一个可变引用到 `smoltcp::iface::SocketSet`,表示要轮询的套接字集
493 ///
494 /// ## 返回值
495 /// - 如果轮询成功,返回 `Ok(())`
496 /// - 如果轮询失败,返回 `Err(SystemError::EAGAIN_OR_EWOULDBLOCK)`,表示需要再次尝试或者操作会阻塞
poll(&self, sockets: &mut smoltcp::iface::SocketSet) -> Result<(), SystemError>497 fn poll(&self, sockets: &mut smoltcp::iface::SocketSet) -> Result<(), SystemError> {
498 let timestamp: smoltcp::time::Instant = Instant::now().into();
499 let mut guard = self.iface.lock();
500 let poll_res = guard.poll(timestamp, self.driver.force_get_mut(), sockets);
501 if poll_res {
502 return Ok(());
503 }
504 return Err(SystemError::EAGAIN_OR_EWOULDBLOCK);
505 }
506
507 #[inline(always)]
inner_iface(&self) -> &SpinLock<smoltcp::iface::Interface>508 fn inner_iface(&self) -> &SpinLock<smoltcp::iface::Interface> {
509 return &self.iface;
510 }
511
addr_assign_type(&self) -> u8512 fn addr_assign_type(&self) -> u8 {
513 return self.inner().netdevice_common.addr_assign_type;
514 }
515
net_device_type(&self) -> u16516 fn net_device_type(&self) -> u16 {
517 self.inner().netdevice_common.net_device_type = 24; // 环回设备
518 return self.inner().netdevice_common.net_device_type;
519 }
520
net_state(&self) -> NetDeivceState521 fn net_state(&self) -> NetDeivceState {
522 return self.inner().netdevice_common.state;
523 }
524
set_net_state(&self, state: NetDeivceState)525 fn set_net_state(&self, state: NetDeivceState) {
526 self.inner().netdevice_common.state |= state;
527 }
528
operstate(&self) -> Operstate529 fn operstate(&self) -> Operstate {
530 return self.inner().netdevice_common.operstate;
531 }
532
set_operstate(&self, state: Operstate)533 fn set_operstate(&self, state: Operstate) {
534 self.inner().netdevice_common.operstate = state;
535 }
536 }
537
loopback_probe()538 pub fn loopback_probe() {
539 loopback_driver_init();
540 }
541 /// ## lo网卡设备初始化函数
542 /// 创建驱动和iface,初始化一个lo网卡,添加到全局NET_DEVICES中
loopback_driver_init()543 pub fn loopback_driver_init() {
544 let driver = LoopbackDriver::new();
545 let iface = LoopbackInterface::new(driver);
546 // 标识网络设备已经启动
547 iface.set_net_state(NetDeivceState::__LINK_STATE_START);
548
549 NET_DEVICES
550 .write_irqsave()
551 .insert(iface.iface_id, iface.clone());
552
553 register_netdevice(iface.clone()).expect("register lo device failed");
554 }
555
556 /// ## lo网卡设备的注册函数
557 #[unified_init(INITCALL_DEVICE)]
loopback_init() -> Result<(), SystemError>558 pub fn loopback_init() -> Result<(), SystemError> {
559 loopback_probe();
560 return Ok(());
561 }
562