113776c11Slogin use core::{ 213776c11Slogin fmt::{self, Debug}, 313776c11Slogin sync::atomic::AtomicUsize, 413776c11Slogin }; 54454d1a2Slogin 64ad52e57S裕依2439 use alloc::{collections::BTreeMap, sync::Arc}; 713776c11Slogin 84ad52e57S裕依2439 use crate::{driver::net::NetDriver, libs::rwlock::RwLock}; 94ad52e57S裕依2439 use smoltcp::wire::IpEndpoint; 1013776c11Slogin 1140609970SGnoCiYeH pub mod event_poll; 1213776c11Slogin pub mod net_core; 1313776c11Slogin pub mod socket; 14cde5492fSlogin pub mod syscall; 1513776c11Slogin 1613776c11Slogin lazy_static! { 174ad52e57S裕依2439 /// # 所有网络接口的列表 180d6cf65aSLoGin /// 这个列表在中断上下文会使用到,因此需要irqsave 1913776c11Slogin pub static ref NET_DRIVERS: RwLock<BTreeMap<usize, Arc<dyn NetDriver>>> = RwLock::new(BTreeMap::new()); 2013776c11Slogin } 2113776c11Slogin 220d6cf65aSLoGin /// 生成网络接口的id (全局自增) 2313776c11Slogin pub fn generate_iface_id() -> usize { 2413776c11Slogin static IFACE_ID: AtomicUsize = AtomicUsize::new(0); 254ad52e57S裕依2439 return IFACE_ID.fetch_add(1, core::sync::atomic::Ordering::SeqCst); 2613776c11Slogin } 2713776c11Slogin 2840609970SGnoCiYeH bitflags! { 2913776c11Slogin /// @brief 用于指定socket的关闭类型 30e7071df6SLoGin /// 参考:https://code.dragonos.org.cn/xref/linux-6.1.9/include/net/sock.h?fi=SHUTDOWN_MASK#1573 3140609970SGnoCiYeH pub struct ShutdownType: u8 { 3240609970SGnoCiYeH const RCV_SHUTDOWN = 1; 3340609970SGnoCiYeH const SEND_SHUTDOWN = 2; 3440609970SGnoCiYeH const SHUTDOWN_MASK = 3; 35cde5492fSlogin } 3613776c11Slogin } 3713776c11Slogin 3813776c11Slogin #[derive(Debug, Clone)] 3913776c11Slogin pub enum Endpoint { 4013776c11Slogin /// 链路层端点 414ad52e57S裕依2439 LinkLayer(LinkLayerEndpoint), 4213776c11Slogin /// 网络层端点 43cde5492fSlogin Ip(Option<IpEndpoint>), 444ad52e57S裕依2439 /// 不需要端点 454ad52e57S裕依2439 Unused, 4613776c11Slogin // todo: 增加NetLink机制后,增加NetLink端点 4713776c11Slogin } 484454d1a2Slogin 494ad52e57S裕依2439 /// @brief 链路层端点 504ad52e57S裕依2439 #[derive(Debug, Clone)] 514ad52e57S裕依2439 pub struct LinkLayerEndpoint { 524ad52e57S裕依2439 /// 网卡的接口号 534ad52e57S裕依2439 pub interface: usize, 5413776c11Slogin } 5513776c11Slogin 564ad52e57S裕依2439 impl LinkLayerEndpoint { 574ad52e57S裕依2439 /// @brief 创建一个链路层端点 5813776c11Slogin /// 594ad52e57S裕依2439 /// @param interface 网卡的接口号 6013776c11Slogin /// 614ad52e57S裕依2439 /// @return 返回创建的链路层端点 624ad52e57S裕依2439 pub fn new(interface: usize) -> Self { 634ad52e57S裕依2439 Self { interface } 6413776c11Slogin } 6513776c11Slogin } 6613776c11Slogin 6713776c11Slogin /// IP datagram encapsulated protocol. 6813776c11Slogin #[derive(Debug, PartialEq, Eq, Clone, Copy)] 6913776c11Slogin #[repr(u8)] 7013776c11Slogin pub enum Protocol { 7113776c11Slogin HopByHop = 0x00, 7213776c11Slogin Icmp = 0x01, 7313776c11Slogin Igmp = 0x02, 7413776c11Slogin Tcp = 0x06, 7513776c11Slogin Udp = 0x11, 7613776c11Slogin Ipv6Route = 0x2b, 7713776c11Slogin Ipv6Frag = 0x2c, 7813776c11Slogin Icmpv6 = 0x3a, 7913776c11Slogin Ipv6NoNxt = 0x3b, 8013776c11Slogin Ipv6Opts = 0x3c, 8113776c11Slogin Unknown(u8), 8213776c11Slogin } 8313776c11Slogin 8413776c11Slogin impl fmt::Display for Protocol { 8513776c11Slogin fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 8613776c11Slogin match *self { 8713776c11Slogin Protocol::HopByHop => write!(f, "Hop-by-Hop"), 8813776c11Slogin Protocol::Icmp => write!(f, "ICMP"), 8913776c11Slogin Protocol::Igmp => write!(f, "IGMP"), 9013776c11Slogin Protocol::Tcp => write!(f, "TCP"), 9113776c11Slogin Protocol::Udp => write!(f, "UDP"), 9213776c11Slogin Protocol::Ipv6Route => write!(f, "IPv6-Route"), 9313776c11Slogin Protocol::Ipv6Frag => write!(f, "IPv6-Frag"), 9413776c11Slogin Protocol::Icmpv6 => write!(f, "ICMPv6"), 9513776c11Slogin Protocol::Ipv6NoNxt => write!(f, "IPv6-NoNxt"), 9613776c11Slogin Protocol::Ipv6Opts => write!(f, "IPv6-Opts"), 9713776c11Slogin Protocol::Unknown(id) => write!(f, "0x{id:02x}"), 9813776c11Slogin } 9913776c11Slogin } 10013776c11Slogin } 10113776c11Slogin 10213776c11Slogin impl From<smoltcp::wire::IpProtocol> for Protocol { 10313776c11Slogin fn from(value: smoltcp::wire::IpProtocol) -> Self { 10413776c11Slogin let x: u8 = value.into(); 10513776c11Slogin Protocol::from(x) 10613776c11Slogin } 10713776c11Slogin } 10813776c11Slogin 10913776c11Slogin impl From<u8> for Protocol { 11013776c11Slogin fn from(value: u8) -> Self { 11113776c11Slogin match value { 11213776c11Slogin 0x00 => Protocol::HopByHop, 11313776c11Slogin 0x01 => Protocol::Icmp, 11413776c11Slogin 0x02 => Protocol::Igmp, 11513776c11Slogin 0x06 => Protocol::Tcp, 11613776c11Slogin 0x11 => Protocol::Udp, 11713776c11Slogin 0x2b => Protocol::Ipv6Route, 11813776c11Slogin 0x2c => Protocol::Ipv6Frag, 11913776c11Slogin 0x3a => Protocol::Icmpv6, 12013776c11Slogin 0x3b => Protocol::Ipv6NoNxt, 12113776c11Slogin 0x3c => Protocol::Ipv6Opts, 12213776c11Slogin _ => Protocol::Unknown(value), 12313776c11Slogin } 12413776c11Slogin } 12513776c11Slogin } 12613776c11Slogin 127*b5b571e0SLoGin impl From<Protocol> for u8 { 128*b5b571e0SLoGin fn from(value: Protocol) -> Self { 129*b5b571e0SLoGin match value { 13013776c11Slogin Protocol::HopByHop => 0x00, 13113776c11Slogin Protocol::Icmp => 0x01, 13213776c11Slogin Protocol::Igmp => 0x02, 13313776c11Slogin Protocol::Tcp => 0x06, 13413776c11Slogin Protocol::Udp => 0x11, 13513776c11Slogin Protocol::Ipv6Route => 0x2b, 13613776c11Slogin Protocol::Ipv6Frag => 0x2c, 13713776c11Slogin Protocol::Icmpv6 => 0x3a, 13813776c11Slogin Protocol::Ipv6NoNxt => 0x3b, 13913776c11Slogin Protocol::Ipv6Opts => 0x3c, 14013776c11Slogin Protocol::Unknown(id) => id, 14113776c11Slogin } 14213776c11Slogin } 1434454d1a2Slogin } 144