mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-24 09:03:23 +00:00
ready for merge in master (#964)
uevent should be format Enum of smoltcp socket should be optimized. need to add interface for routing subsys actix is still not abled to run. clean some casual added code to other places
This commit is contained in:
@ -1,3 +1,7 @@
|
||||
//! # 网络模块
|
||||
//! 注意,net模块下,为了方便导入,模块细分,且共用部分模块直接使用
|
||||
//! `pub use`导出,导入时也常见`use crate::net::socket::*`的写法,
|
||||
//! 敬请注意。
|
||||
use core::{
|
||||
fmt::{self, Debug},
|
||||
sync::atomic::AtomicUsize,
|
||||
@ -5,20 +9,18 @@ use core::{
|
||||
|
||||
use alloc::{collections::BTreeMap, sync::Arc};
|
||||
|
||||
use crate::{driver::net::NetDevice, libs::rwlock::RwLock};
|
||||
use smoltcp::wire::IpEndpoint;
|
||||
|
||||
use self::socket::SocketInode;
|
||||
use crate::{driver::net::Iface, libs::rwlock::RwLock};
|
||||
|
||||
pub mod event_poll;
|
||||
pub mod net_core;
|
||||
pub mod socket;
|
||||
pub mod syscall;
|
||||
pub mod syscall_util;
|
||||
|
||||
lazy_static! {
|
||||
/// # 所有网络接口的列表
|
||||
/// 这个列表在中断上下文会使用到,因此需要irqsave
|
||||
pub static ref NET_DEVICES: RwLock<BTreeMap<usize, Arc<dyn NetDevice>>> = RwLock::new(BTreeMap::new());
|
||||
pub static ref NET_DEVICES: RwLock<BTreeMap<usize, Arc<dyn Iface>>> = RwLock::new(BTreeMap::new());
|
||||
}
|
||||
|
||||
/// 生成网络接口的id (全局自增)
|
||||
@ -26,120 +28,3 @@ pub fn generate_iface_id() -> usize {
|
||||
static IFACE_ID: AtomicUsize = AtomicUsize::new(0);
|
||||
return IFACE_ID.fetch_add(1, core::sync::atomic::Ordering::SeqCst);
|
||||
}
|
||||
|
||||
bitflags! {
|
||||
/// @brief 用于指定socket的关闭类型
|
||||
/// 参考:https://code.dragonos.org.cn/xref/linux-6.1.9/include/net/sock.h?fi=SHUTDOWN_MASK#1573
|
||||
pub struct ShutdownType: u8 {
|
||||
const RCV_SHUTDOWN = 1;
|
||||
const SEND_SHUTDOWN = 2;
|
||||
const SHUTDOWN_MASK = 3;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Endpoint {
|
||||
/// 链路层端点
|
||||
LinkLayer(LinkLayerEndpoint),
|
||||
/// 网络层端点
|
||||
Ip(Option<IpEndpoint>),
|
||||
/// inode端点
|
||||
Inode(Option<Arc<SocketInode>>),
|
||||
// todo: 增加NetLink机制后,增加NetLink端点
|
||||
}
|
||||
|
||||
/// @brief 链路层端点
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LinkLayerEndpoint {
|
||||
/// 网卡的接口号
|
||||
pub interface: usize,
|
||||
}
|
||||
|
||||
impl LinkLayerEndpoint {
|
||||
/// @brief 创建一个链路层端点
|
||||
///
|
||||
/// @param interface 网卡的接口号
|
||||
///
|
||||
/// @return 返回创建的链路层端点
|
||||
pub fn new(interface: usize) -> Self {
|
||||
Self { interface }
|
||||
}
|
||||
}
|
||||
|
||||
/// IP datagram encapsulated protocol.
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||
#[repr(u8)]
|
||||
pub enum Protocol {
|
||||
HopByHop = 0x00,
|
||||
Icmp = 0x01,
|
||||
Igmp = 0x02,
|
||||
Tcp = 0x06,
|
||||
Udp = 0x11,
|
||||
Ipv6Route = 0x2b,
|
||||
Ipv6Frag = 0x2c,
|
||||
Icmpv6 = 0x3a,
|
||||
Ipv6NoNxt = 0x3b,
|
||||
Ipv6Opts = 0x3c,
|
||||
Unknown(u8),
|
||||
}
|
||||
|
||||
impl fmt::Display for Protocol {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
Protocol::HopByHop => write!(f, "Hop-by-Hop"),
|
||||
Protocol::Icmp => write!(f, "ICMP"),
|
||||
Protocol::Igmp => write!(f, "IGMP"),
|
||||
Protocol::Tcp => write!(f, "TCP"),
|
||||
Protocol::Udp => write!(f, "UDP"),
|
||||
Protocol::Ipv6Route => write!(f, "IPv6-Route"),
|
||||
Protocol::Ipv6Frag => write!(f, "IPv6-Frag"),
|
||||
Protocol::Icmpv6 => write!(f, "ICMPv6"),
|
||||
Protocol::Ipv6NoNxt => write!(f, "IPv6-NoNxt"),
|
||||
Protocol::Ipv6Opts => write!(f, "IPv6-Opts"),
|
||||
Protocol::Unknown(id) => write!(f, "0x{id:02x}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<smoltcp::wire::IpProtocol> for Protocol {
|
||||
fn from(value: smoltcp::wire::IpProtocol) -> Self {
|
||||
let x: u8 = value.into();
|
||||
Protocol::from(x)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u8> for Protocol {
|
||||
fn from(value: u8) -> Self {
|
||||
match value {
|
||||
0x00 => Protocol::HopByHop,
|
||||
0x01 => Protocol::Icmp,
|
||||
0x02 => Protocol::Igmp,
|
||||
0x06 => Protocol::Tcp,
|
||||
0x11 => Protocol::Udp,
|
||||
0x2b => Protocol::Ipv6Route,
|
||||
0x2c => Protocol::Ipv6Frag,
|
||||
0x3a => Protocol::Icmpv6,
|
||||
0x3b => Protocol::Ipv6NoNxt,
|
||||
0x3c => Protocol::Ipv6Opts,
|
||||
_ => Protocol::Unknown(value),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Protocol> for u8 {
|
||||
fn from(value: Protocol) -> Self {
|
||||
match value {
|
||||
Protocol::HopByHop => 0x00,
|
||||
Protocol::Icmp => 0x01,
|
||||
Protocol::Igmp => 0x02,
|
||||
Protocol::Tcp => 0x06,
|
||||
Protocol::Udp => 0x11,
|
||||
Protocol::Ipv6Route => 0x2b,
|
||||
Protocol::Ipv6Frag => 0x2c,
|
||||
Protocol::Icmpv6 => 0x3a,
|
||||
Protocol::Ipv6NoNxt => 0x3b,
|
||||
Protocol::Ipv6Opts => 0x3c,
|
||||
Protocol::Unknown(id) => id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user