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