mod.rs 3.3 KB

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