113776c11Slogin use core::{
213776c11Slogin fmt::{self, Debug},
313776c11Slogin sync::atomic::AtomicUsize,
413776c11Slogin };
54454d1a2Slogin
64ad52e57S裕依2439 use alloc::{collections::BTreeMap, sync::Arc};
713776c11Slogin
8*e32effb1SLoGin use crate::{driver::net::NetDevice, libs::rwlock::RwLock};
94ad52e57S裕依2439 use smoltcp::wire::IpEndpoint;
1013776c11Slogin
116046f775S裕依 use self::socket::SocketInode;
126046f775S裕依
1340609970SGnoCiYeH pub mod event_poll;
1413776c11Slogin pub mod net_core;
1513776c11Slogin pub mod socket;
16cde5492fSlogin pub mod syscall;
1713776c11Slogin
1813776c11Slogin lazy_static! {
194ad52e57S裕依2439 /// # 所有网络接口的列表
200d6cf65aSLoGin /// 这个列表在中断上下文会使用到,因此需要irqsave
21*e32effb1SLoGin pub static ref NET_DEVICES: RwLock<BTreeMap<usize, Arc<dyn NetDevice>>> = RwLock::new(BTreeMap::new());
2213776c11Slogin }
2313776c11Slogin
240d6cf65aSLoGin /// 生成网络接口的id (全局自增)
generate_iface_id() -> usize2513776c11Slogin pub fn generate_iface_id() -> usize {
2613776c11Slogin static IFACE_ID: AtomicUsize = AtomicUsize::new(0);
274ad52e57S裕依2439 return IFACE_ID.fetch_add(1, core::sync::atomic::Ordering::SeqCst);
2813776c11Slogin }
2913776c11Slogin
3040609970SGnoCiYeH bitflags! {
3113776c11Slogin /// @brief 用于指定socket的关闭类型
32e7071df6SLoGin /// 参考:https://code.dragonos.org.cn/xref/linux-6.1.9/include/net/sock.h?fi=SHUTDOWN_MASK#1573
3340609970SGnoCiYeH pub struct ShutdownType: u8 {
3440609970SGnoCiYeH const RCV_SHUTDOWN = 1;
3540609970SGnoCiYeH const SEND_SHUTDOWN = 2;
3640609970SGnoCiYeH const SHUTDOWN_MASK = 3;
37cde5492fSlogin }
3813776c11Slogin }
3913776c11Slogin
4013776c11Slogin #[derive(Debug, Clone)]
4113776c11Slogin pub enum Endpoint {
4213776c11Slogin /// 链路层端点
434ad52e57S裕依2439 LinkLayer(LinkLayerEndpoint),
4413776c11Slogin /// 网络层端点
45cde5492fSlogin Ip(Option<IpEndpoint>),
466046f775S裕依 /// inode端点
476046f775S裕依 Inode(Option<Arc<SocketInode>>),
4813776c11Slogin // todo: 增加NetLink机制后,增加NetLink端点
4913776c11Slogin }
504454d1a2Slogin
514ad52e57S裕依2439 /// @brief 链路层端点
524ad52e57S裕依2439 #[derive(Debug, Clone)]
534ad52e57S裕依2439 pub struct LinkLayerEndpoint {
544ad52e57S裕依2439 /// 网卡的接口号
554ad52e57S裕依2439 pub interface: usize,
5613776c11Slogin }
5713776c11Slogin
584ad52e57S裕依2439 impl LinkLayerEndpoint {
594ad52e57S裕依2439 /// @brief 创建一个链路层端点
6013776c11Slogin ///
614ad52e57S裕依2439 /// @param interface 网卡的接口号
6213776c11Slogin ///
634ad52e57S裕依2439 /// @return 返回创建的链路层端点
new(interface: usize) -> Self644ad52e57S裕依2439 pub fn new(interface: usize) -> Self {
654ad52e57S裕依2439 Self { interface }
6613776c11Slogin }
6713776c11Slogin }
6813776c11Slogin
6913776c11Slogin /// IP datagram encapsulated protocol.
7013776c11Slogin #[derive(Debug, PartialEq, Eq, Clone, Copy)]
7113776c11Slogin #[repr(u8)]
7213776c11Slogin pub enum Protocol {
7313776c11Slogin HopByHop = 0x00,
7413776c11Slogin Icmp = 0x01,
7513776c11Slogin Igmp = 0x02,
7613776c11Slogin Tcp = 0x06,
7713776c11Slogin Udp = 0x11,
7813776c11Slogin Ipv6Route = 0x2b,
7913776c11Slogin Ipv6Frag = 0x2c,
8013776c11Slogin Icmpv6 = 0x3a,
8113776c11Slogin Ipv6NoNxt = 0x3b,
8213776c11Slogin Ipv6Opts = 0x3c,
8313776c11Slogin Unknown(u8),
8413776c11Slogin }
8513776c11Slogin
8613776c11Slogin impl fmt::Display for Protocol {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result8713776c11Slogin fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
8813776c11Slogin match *self {
8913776c11Slogin Protocol::HopByHop => write!(f, "Hop-by-Hop"),
9013776c11Slogin Protocol::Icmp => write!(f, "ICMP"),
9113776c11Slogin Protocol::Igmp => write!(f, "IGMP"),
9213776c11Slogin Protocol::Tcp => write!(f, "TCP"),
9313776c11Slogin Protocol::Udp => write!(f, "UDP"),
9413776c11Slogin Protocol::Ipv6Route => write!(f, "IPv6-Route"),
9513776c11Slogin Protocol::Ipv6Frag => write!(f, "IPv6-Frag"),
9613776c11Slogin Protocol::Icmpv6 => write!(f, "ICMPv6"),
9713776c11Slogin Protocol::Ipv6NoNxt => write!(f, "IPv6-NoNxt"),
9813776c11Slogin Protocol::Ipv6Opts => write!(f, "IPv6-Opts"),
9913776c11Slogin Protocol::Unknown(id) => write!(f, "0x{id:02x}"),
10013776c11Slogin }
10113776c11Slogin }
10213776c11Slogin }
10313776c11Slogin
10413776c11Slogin impl From<smoltcp::wire::IpProtocol> for Protocol {
from(value: smoltcp::wire::IpProtocol) -> Self10513776c11Slogin fn from(value: smoltcp::wire::IpProtocol) -> Self {
10613776c11Slogin let x: u8 = value.into();
10713776c11Slogin Protocol::from(x)
10813776c11Slogin }
10913776c11Slogin }
11013776c11Slogin
11113776c11Slogin impl From<u8> for Protocol {
from(value: u8) -> Self11213776c11Slogin fn from(value: u8) -> Self {
11313776c11Slogin match value {
11413776c11Slogin 0x00 => Protocol::HopByHop,
11513776c11Slogin 0x01 => Protocol::Icmp,
11613776c11Slogin 0x02 => Protocol::Igmp,
11713776c11Slogin 0x06 => Protocol::Tcp,
11813776c11Slogin 0x11 => Protocol::Udp,
11913776c11Slogin 0x2b => Protocol::Ipv6Route,
12013776c11Slogin 0x2c => Protocol::Ipv6Frag,
12113776c11Slogin 0x3a => Protocol::Icmpv6,
12213776c11Slogin 0x3b => Protocol::Ipv6NoNxt,
12313776c11Slogin 0x3c => Protocol::Ipv6Opts,
12413776c11Slogin _ => Protocol::Unknown(value),
12513776c11Slogin }
12613776c11Slogin }
12713776c11Slogin }
12813776c11Slogin
129b5b571e0SLoGin impl From<Protocol> for u8 {
from(value: Protocol) -> Self130b5b571e0SLoGin fn from(value: Protocol) -> Self {
131b5b571e0SLoGin match value {
13213776c11Slogin Protocol::HopByHop => 0x00,
13313776c11Slogin Protocol::Icmp => 0x01,
13413776c11Slogin Protocol::Igmp => 0x02,
13513776c11Slogin Protocol::Tcp => 0x06,
13613776c11Slogin Protocol::Udp => 0x11,
13713776c11Slogin Protocol::Ipv6Route => 0x2b,
13813776c11Slogin Protocol::Ipv6Frag => 0x2c,
13913776c11Slogin Protocol::Icmpv6 => 0x3a,
14013776c11Slogin Protocol::Ipv6NoNxt => 0x3b,
14113776c11Slogin Protocol::Ipv6Opts => 0x3c,
14213776c11Slogin Protocol::Unknown(id) => id,
14313776c11Slogin }
14413776c11Slogin }
1454454d1a2Slogin }
146