xref: /DragonOS/kernel/src/driver/net/mod.rs (revision fae6e9ade46a52976ad5d099643d51cc20876448)
1 use alloc::{string::String, sync::Arc};
2 use smoltcp::{
3     iface,
4     wire::{self, EthernetAddress},
5 };
6 use sysfs::netdev_register_kobject;
7 
8 use super::base::device::Device;
9 use crate::libs::spinlock::SpinLock;
10 use system_error::SystemError;
11 
12 pub mod class;
13 mod dma;
14 pub mod e1000e;
15 pub mod irq_handle;
16 pub mod loopback;
17 pub mod sysfs;
18 pub mod virtio_net;
19 
20 bitflags! {
21     pub struct NetDeivceState: u16 {
22         /// 表示网络设备已经启动
23         const __LINK_STATE_START = 1 << 0;
24         /// 表示网络设备在系统中存在,即注册到sysfs中
25         const __LINK_STATE_PRESENT = 1 << 1;
26         /// 表示网络设备没有检测到载波信号
27         const __LINK_STATE_NOCARRIER = 1 << 2;
28         /// 表示设备的链路监视操作处于挂起状态
29         const __LINK_STATE_LINKWATCH_PENDING = 1 << 3;
30         /// 表示设备处于休眠状态
31         const __LINK_STATE_DORMANT = 1 << 4;
32     }
33 }
34 
35 #[derive(Debug, Copy, Clone)]
36 #[allow(dead_code, non_camel_case_types)]
37 pub enum Operstate {
38     /// 网络接口的状态未知
39     IF_OPER_UNKNOWN = 0,
40     /// 网络接口不存在
41     IF_OPER_NOTPRESENT = 1,
42     /// 网络接口已禁用或未连接
43     IF_OPER_DOWN = 2,
44     /// 网络接口的下层接口已关闭
45     IF_OPER_LOWERLAYERDOWN = 3,
46     /// 网络接口正在测试
47     IF_OPER_TESTING = 4,
48     /// 网络接口处于休眠状态
49     IF_OPER_DORMANT = 5,
50     /// 网络接口已启用
51     IF_OPER_UP = 6,
52 }
53 
54 #[allow(dead_code)]
55 pub trait NetDevice: Device {
56     /// @brief 获取网卡的MAC地址
57     fn mac(&self) -> EthernetAddress;
58 
59     fn iface_name(&self) -> String;
60 
61     /// @brief 获取网卡的id
62     fn nic_id(&self) -> usize;
63 
64     fn poll(&self, sockets: &mut iface::SocketSet) -> Result<(), SystemError>;
65 
66     fn update_ip_addrs(&self, ip_addrs: &[wire::IpCidr]) -> Result<(), SystemError>;
67 
68     /// @brief 获取smoltcp的网卡接口类型
69     fn inner_iface(&self) -> &SpinLock<smoltcp::iface::Interface>;
70     // fn as_any_ref(&'static self) -> &'static dyn core::any::Any;
71 
72     fn addr_assign_type(&self) -> u8;
73 
74     fn net_device_type(&self) -> u16;
75 
76     fn net_state(&self) -> NetDeivceState;
77 
78     fn set_net_state(&self, state: NetDeivceState);
79 
80     fn operstate(&self) -> Operstate;
81 
82     fn set_operstate(&self, state: Operstate);
83 }
84 
85 /// 网络设备的公共数据
86 #[derive(Debug)]
87 pub struct NetDeviceCommonData {
88     /// 表示网络接口的地址分配类型
89     pub addr_assign_type: u8,
90     /// 表示网络接口的类型
91     pub net_device_type: u16,
92     /// 表示网络接口的状态
93     pub state: NetDeivceState,
94     /// 表示网络接口的操作状态
95     pub operstate: Operstate,
96 }
97 
98 impl Default for NetDeviceCommonData {
99     fn default() -> Self {
100         Self {
101             addr_assign_type: 0,
102             net_device_type: 1,
103             state: NetDeivceState::empty(),
104             operstate: Operstate::IF_OPER_UNKNOWN,
105         }
106     }
107 }
108 
109 /// 将网络设备注册到sysfs中
110 /// 参考:https://code.dragonos.org.cn/xref/linux-2.6.39/net/core/dev.c?fi=register_netdev#5373
111 fn register_netdevice(dev: Arc<dyn NetDevice>) -> Result<(), SystemError> {
112     // 在sysfs中注册设备
113     netdev_register_kobject(dev.clone())?;
114 
115     // 标识网络设备在系统中存在
116     dev.set_net_state(NetDeivceState::__LINK_STATE_PRESENT);
117 
118     return Ok(());
119 }
120