1 use core::{any::Any, fmt::Debug, sync::atomic::AtomicUsize};
2 
3 use alloc::{
4     boxed::Box,
5     collections::LinkedList,
6     string::String,
7     sync::{Arc, Weak},
8     vec::Vec,
9 };
10 use hashbrown::HashMap;
11 use smoltcp::{
12     iface::{SocketHandle, SocketSet},
13     socket::{self, tcp, udp},
14 };
15 use system_error::SystemError;
16 
17 use crate::{
18     arch::{rand::rand, sched::sched},
19     filesystem::vfs::{
20         file::FileMode, syscall::ModeType, FilePrivateData, FileSystem, FileType, IndexNode,
21         Metadata,
22     },
23     libs::{
24         rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard},
25         spinlock::{SpinLock, SpinLockGuard},
26         wait_queue::EventWaitQueue,
27     },
28 };
29 
30 use self::sockets::{RawSocket, SeqpacketSocket, TcpSocket, UdpSocket};
31 
32 use super::{
33     event_poll::{EPollEventType, EPollItem, EventPoll},
34     net_core::poll_ifaces,
35     Endpoint, Protocol, ShutdownType,
36 };
37 
38 pub mod sockets;
39 
40 lazy_static! {
41     /// 所有socket的集合
42     /// TODO: 优化这里,自己实现SocketSet!!!现在这样的话,不管全局有多少个网卡,每个时间点都只会有1个进程能够访问socket
43     pub static ref SOCKET_SET: SpinLock<SocketSet<'static >> = SpinLock::new(SocketSet::new(vec![]));
44     /// SocketHandle表,每个SocketHandle对应一个SocketHandleItem,
45     /// 注意!:在网卡中断中需要拿到这张表的��,在获取读锁时应该确保关中断避免死锁
46     pub static ref HANDLE_MAP: RwLock<HashMap<SocketHandle, SocketHandleItem>> = RwLock::new(HashMap::new());
47     /// 端口管理器
48     pub static ref PORT_MANAGER: PortManager = PortManager::new();
49 }
50 
51 /* For setsockopt(2) */
52 // See: linux-5.19.10/include/uapi/asm-generic/socket.h#9
53 pub const SOL_SOCKET: u8 = 1;
54 
55 /// 根据地址族、socket类型和协议创建socket
new_socket( address_family: AddressFamily, socket_type: PosixSocketType, protocol: Protocol, ) -> Result<Box<dyn Socket>, SystemError>56 pub(super) fn new_socket(
57     address_family: AddressFamily,
58     socket_type: PosixSocketType,
59     protocol: Protocol,
60 ) -> Result<Box<dyn Socket>, SystemError> {
61     let socket: Box<dyn Socket> = match address_family {
62         AddressFamily::Unix => match socket_type {
63             PosixSocketType::Stream => Box::new(TcpSocket::new(SocketOptions::default())),
64             PosixSocketType::Datagram => Box::new(UdpSocket::new(SocketOptions::default())),
65             PosixSocketType::Raw => Box::new(RawSocket::new(protocol, SocketOptions::default())),
66             PosixSocketType::SeqPacket => Box::new(SeqpacketSocket::new(SocketOptions::default())),
67             _ => {
68                 return Err(SystemError::EINVAL);
69             }
70         },
71         AddressFamily::INet => match socket_type {
72             PosixSocketType::Stream => Box::new(TcpSocket::new(SocketOptions::default())),
73             PosixSocketType::Datagram => Box::new(UdpSocket::new(SocketOptions::default())),
74             PosixSocketType::Raw => Box::new(RawSocket::new(protocol, SocketOptions::default())),
75             _ => {
76                 return Err(SystemError::EINVAL);
77             }
78         },
79         _ => {
80             return Err(SystemError::EAFNOSUPPORT);
81         }
82     };
83     Ok(socket)
84 }
85 
86 pub trait Socket: Sync + Send + Debug + Any {
as_any_ref(&self) -> &dyn Any87     fn as_any_ref(&self) -> &dyn Any;
88 
as_any_mut(&mut self) -> &mut dyn Any89     fn as_any_mut(&mut self) -> &mut dyn Any;
90     /// @brief 从socket中读取数据,如果socket是阻塞的,那么直到读取到数据才返回
91     ///
92     /// @param buf 读取到的数据存放的缓冲区
93     ///
94     /// @return - 成功:(返回读取的数据的长度,读取数据的端点).
95     ///         - 失败:错误码
read(&mut self, buf: &mut [u8]) -> (Result<usize, SystemError>, Endpoint)96     fn read(&mut self, buf: &mut [u8]) -> (Result<usize, SystemError>, Endpoint);
97 
98     /// @brief 向socket中写入数据。如果socket是阻塞的,那么直到写入的数据全部写入socket中才返回
99     ///
100     /// @param buf 要写入的数据
101     /// @param to 要写入的目的端点,如果是None,那么写入的数据将会被丢弃
102     ///
103     /// @return 返回写入的数据的长度
write(&self, buf: &[u8], to: Option<Endpoint>) -> Result<usize, SystemError>104     fn write(&self, buf: &[u8], to: Option<Endpoint>) -> Result<usize, SystemError>;
105 
106     /// @brief 对应于POSIX的connect函数,用于连接到指定的远程服务器端点
107     ///
108     /// It is used to establish a connection to a remote server.
109     /// When a socket is connected to a remote server,
110     /// the operating system will establish a network connection with the server
111     /// and allow data to be sent and received between the local socket and the remote server.
112     ///
113     /// @param endpoint 要连接的端点
114     ///
115     /// @return 返回连接是否成功
connect(&mut self, _endpoint: Endpoint) -> Result<(), SystemError>116     fn connect(&mut self, _endpoint: Endpoint) -> Result<(), SystemError> {
117         return Err(SystemError::ENOSYS);
118     }
119 
120     /// @brief 对应于POSIX的bind函数,用于绑定到本机指定的端点
121     ///
122     /// The bind() function is used to associate a socket with a particular IP address and port number on the local machine.
123     ///
124     /// @param endpoint 要绑定的端点
125     ///
126     /// @return 返回绑定是否成功
bind(&mut self, _endpoint: Endpoint) -> Result<(), SystemError>127     fn bind(&mut self, _endpoint: Endpoint) -> Result<(), SystemError> {
128         return Err(SystemError::ENOSYS);
129     }
130 
131     /// @brief 对应于 POSIX 的 shutdown 函数,用于关闭socket。
132     ///
133     /// shutdown() 函数用于启动网络连接的正常关闭。
134     /// 当在两个端点之间建立网络连接时,任一端点都可以通过调用其端点对象上的 shutdown() 函数来启动关闭序列。
135     /// 此函数向远程端点发送关闭消息以指示本地端点不再接受新数据。
136     ///
137     /// @return 返回是否成功关闭
shutdown(&mut self, _type: ShutdownType) -> Result<(), SystemError>138     fn shutdown(&mut self, _type: ShutdownType) -> Result<(), SystemError> {
139         return Err(SystemError::ENOSYS);
140     }
141 
142     /// @brief 对应于POSIX的listen函数,用于监听端点
143     ///
144     /// @param backlog 最大的等待连接数
145     ///
146     /// @return 返回监听是否成功
listen(&mut self, _backlog: usize) -> Result<(), SystemError>147     fn listen(&mut self, _backlog: usize) -> Result<(), SystemError> {
148         return Err(SystemError::ENOSYS);
149     }
150 
151     /// @brief 对应于POSIX的accept函数,用于接受连接
152     ///
153     /// @param endpoint 对端的端点
154     ///
155     /// @return 返回接受连接是否成功
accept(&mut self) -> Result<(Box<dyn Socket>, Endpoint), SystemError>156     fn accept(&mut self) -> Result<(Box<dyn Socket>, Endpoint), SystemError> {
157         return Err(SystemError::ENOSYS);
158     }
159 
160     /// @brief 获取socket的端点
161     ///
162     /// @return 返回socket的端点
endpoint(&self) -> Option<Endpoint>163     fn endpoint(&self) -> Option<Endpoint> {
164         return None;
165     }
166 
167     /// @brief 获取socket的对端端点
168     ///
169     /// @return 返回socket的对端端点
peer_endpoint(&self) -> Option<Endpoint>170     fn peer_endpoint(&self) -> Option<Endpoint> {
171         return None;
172     }
173 
socketpair_ops(&self) -> Option<&'static dyn SocketpairOps>174     fn socketpair_ops(&self) -> Option<&'static dyn SocketpairOps> {
175         None
176     }
177 
178     /// @brief
179     ///     The purpose of the poll function is to provide
180     ///     a non-blocking way to check if a socket is ready for reading or writing,
181     ///     so that you can efficiently handle multiple sockets in a single thread or event loop.
182     ///
183     /// @return (in, out, err)
184     ///
185     ///     The first boolean value indicates whether the socket is ready for reading. If it is true, then there is data available to be read from the socket without blocking.
186     ///     The second boolean value indicates whether the socket is ready for writing. If it is true, then data can be written to the socket without blocking.
187     ///     The third boolean value indicates whether the socket has encountered an error condition. If it is true, then the socket is in an error state and should be closed or reset
188     ///
poll(&self) -> EPollEventType189     fn poll(&self) -> EPollEventType {
190         return EPollEventType::empty();
191     }
192 
193     /// @brief socket的ioctl函数
194     ///
195     /// @param cmd ioctl命令
196     /// @param arg0 ioctl命令的第一个参数
197     /// @param arg1 ioctl命令的第二个参数
198     /// @param arg2 ioctl命令的第三个参数
199     ///
200     /// @return 返回ioctl命令的返回值
ioctl( &self, _cmd: usize, _arg0: usize, _arg1: usize, _arg2: usize, ) -> Result<usize, SystemError>201     fn ioctl(
202         &self,
203         _cmd: usize,
204         _arg0: usize,
205         _arg1: usize,
206         _arg2: usize,
207     ) -> Result<usize, SystemError> {
208         return Ok(0);
209     }
210 
211     /// @brief 获取socket的元数据
metadata(&self) -> Result<SocketMetadata, SystemError>212     fn metadata(&self) -> Result<SocketMetadata, SystemError>;
213 
box_clone(&self) -> Box<dyn Socket>214     fn box_clone(&self) -> Box<dyn Socket>;
215 
216     /// @brief 设置socket的选项
217     ///
218     /// @param level 选项的层次
219     /// @param optname 选项的名称
220     /// @param optval 选项的值
221     ///
222     /// @return 返回设置是否成功, 如果不支持该选项,返回ENOSYS
setsockopt( &self, _level: usize, _optname: usize, _optval: &[u8], ) -> Result<(), SystemError>223     fn setsockopt(
224         &self,
225         _level: usize,
226         _optname: usize,
227         _optval: &[u8],
228     ) -> Result<(), SystemError> {
229         kwarn!("setsockopt is not implemented");
230         return Ok(());
231     }
232 
socket_handle(&self) -> SocketHandle233     fn socket_handle(&self) -> SocketHandle {
234         todo!()
235     }
236 
add_epoll(&mut self, epitem: Arc<EPollItem>) -> Result<(), SystemError>237     fn add_epoll(&mut self, epitem: Arc<EPollItem>) -> Result<(), SystemError> {
238         HANDLE_MAP
239             .write_irqsave()
240             .get_mut(&self.socket_handle())
241             .unwrap()
242             .add_epoll(epitem);
243         Ok(())
244     }
245 
remove_epoll(&mut self, epoll: &Weak<SpinLock<EventPoll>>) -> Result<(), SystemError>246     fn remove_epoll(&mut self, epoll: &Weak<SpinLock<EventPoll>>) -> Result<(), SystemError> {
247         HANDLE_MAP
248             .write_irqsave()
249             .get_mut(&self.socket_handle())
250             .unwrap()
251             .remove_epoll(epoll)?;
252 
253         Ok(())
254     }
255 
clear_epoll(&mut self) -> Result<(), SystemError>256     fn clear_epoll(&mut self) -> Result<(), SystemError> {
257         let mut handle_map_guard = HANDLE_MAP.write_irqsave();
258         let handle_item = handle_map_guard.get_mut(&self.socket_handle()).unwrap();
259 
260         for epitem in handle_item.epitems.lock_irqsave().iter() {
261             let epoll = epitem.epoll();
262             if epoll.upgrade().is_some() {
263                 EventPoll::ep_remove(
264                     &mut epoll.upgrade().unwrap().lock_irqsave(),
265                     epitem.fd(),
266                     None,
267                 )?;
268             }
269         }
270 
271         Ok(())
272     }
273 }
274 
275 impl Clone for Box<dyn Socket> {
clone(&self) -> Box<dyn Socket>276     fn clone(&self) -> Box<dyn Socket> {
277         self.box_clone()
278     }
279 }
280 
281 pub trait SocketpairOps {
282     /// 执行socketpair
socketpair(&self, socket0: &mut Box<dyn Socket>, socket1: &mut Box<dyn Socket>)283     fn socketpair(&self, socket0: &mut Box<dyn Socket>, socket1: &mut Box<dyn Socket>);
284 }
285 
286 /// # Socket在文件系统中的inode封装
287 #[derive(Debug)]
288 pub struct SocketInode(SpinLock<Box<dyn Socket>>, AtomicUsize);
289 
290 impl SocketInode {
new(socket: Box<dyn Socket>) -> Arc<Self>291     pub fn new(socket: Box<dyn Socket>) -> Arc<Self> {
292         Arc::new(Self(SpinLock::new(socket), AtomicUsize::new(0)))
293     }
294 
295     #[inline]
inner(&self) -> SpinLockGuard<Box<dyn Socket>>296     pub fn inner(&self) -> SpinLockGuard<Box<dyn Socket>> {
297         return self.0.lock();
298     }
299 
inner_no_preempt(&self) -> SpinLockGuard<Box<dyn Socket>>300     pub unsafe fn inner_no_preempt(&self) -> SpinLockGuard<Box<dyn Socket>> {
301         return self.0.lock_no_preempt();
302     }
303 }
304 
305 impl IndexNode for SocketInode {
open(&self, _data: &mut FilePrivateData, _mode: &FileMode) -> Result<(), SystemError>306     fn open(&self, _data: &mut FilePrivateData, _mode: &FileMode) -> Result<(), SystemError> {
307         self.1.fetch_add(1, core::sync::atomic::Ordering::SeqCst);
308         Ok(())
309     }
310 
close(&self, _data: &mut FilePrivateData) -> Result<(), SystemError>311     fn close(&self, _data: &mut FilePrivateData) -> Result<(), SystemError> {
312         let prev_ref_count = self.1.fetch_sub(1, core::sync::atomic::Ordering::SeqCst);
313         if prev_ref_count == 1 {
314             // 最后一次关闭,需要释放
315             let mut socket = self.0.lock_irqsave();
316 
317             if socket.metadata().unwrap().socket_type == SocketType::SeqpacketSocket {
318                 return Ok(());
319             }
320 
321             if let Some(Endpoint::Ip(Some(ip))) = socket.endpoint() {
322                 PORT_MANAGER.unbind_port(socket.metadata().unwrap().socket_type, ip.port)?;
323             }
324 
325             socket.clear_epoll()?;
326 
327             HANDLE_MAP
328                 .write_irqsave()
329                 .remove(&socket.socket_handle())
330                 .unwrap();
331         }
332         Ok(())
333     }
334 
read_at( &self, _offset: usize, len: usize, buf: &mut [u8], _data: &mut FilePrivateData, ) -> Result<usize, SystemError>335     fn read_at(
336         &self,
337         _offset: usize,
338         len: usize,
339         buf: &mut [u8],
340         _data: &mut FilePrivateData,
341     ) -> Result<usize, SystemError> {
342         return self.0.lock_no_preempt().read(&mut buf[0..len]).0;
343     }
344 
write_at( &self, _offset: usize, len: usize, buf: &[u8], _data: &mut FilePrivateData, ) -> Result<usize, SystemError>345     fn write_at(
346         &self,
347         _offset: usize,
348         len: usize,
349         buf: &[u8],
350         _data: &mut FilePrivateData,
351     ) -> Result<usize, SystemError> {
352         return self.0.lock_no_preempt().write(&buf[0..len], None);
353     }
354 
poll(&self, _private_data: &FilePrivateData) -> Result<usize, SystemError>355     fn poll(&self, _private_data: &FilePrivateData) -> Result<usize, SystemError> {
356         let events = self.0.lock_irqsave().poll();
357         return Ok(events.bits() as usize);
358     }
359 
fs(&self) -> Arc<dyn FileSystem>360     fn fs(&self) -> Arc<dyn FileSystem> {
361         todo!()
362     }
363 
as_any_ref(&self) -> &dyn Any364     fn as_any_ref(&self) -> &dyn Any {
365         self
366     }
367 
list(&self) -> Result<Vec<String>, SystemError>368     fn list(&self) -> Result<Vec<String>, SystemError> {
369         return Err(SystemError::ENOTDIR);
370     }
371 
metadata(&self) -> Result<Metadata, SystemError>372     fn metadata(&self) -> Result<Metadata, SystemError> {
373         let meta = Metadata {
374             mode: ModeType::from_bits_truncate(0o755),
375             file_type: FileType::Socket,
376             ..Default::default()
377         };
378 
379         return Ok(meta);
380     }
381 
resize(&self, _len: usize) -> Result<(), SystemError>382     fn resize(&self, _len: usize) -> Result<(), SystemError> {
383         return Ok(());
384     }
385 }
386 
387 #[allow(dead_code)]
388 #[derive(Debug)]
389 pub struct SocketHandleItem {
390     /// socket元数据
391     metadata: SocketMetadata,
392     /// shutdown状态
393     pub shutdown_type: RwLock<ShutdownType>,
394     /// socket的waitqueue
395     pub wait_queue: EventWaitQueue,
396     /// epitems,考虑写在这是否是最优解?
397     pub epitems: SpinLock<LinkedList<Arc<EPollItem>>>,
398 }
399 
400 impl SocketHandleItem {
new(socket: &Box<dyn Socket>) -> Self401     pub fn new(socket: &Box<dyn Socket>) -> Self {
402         Self {
403             metadata: socket.metadata().unwrap(),
404             shutdown_type: RwLock::new(ShutdownType::empty()),
405             wait_queue: EventWaitQueue::new(),
406             epitems: SpinLock::new(LinkedList::new()),
407         }
408     }
409 
from_socket<A: Socket>(socket: &Box<A>) -> Self410     pub fn from_socket<A: Socket>(socket: &Box<A>) -> Self {
411         Self {
412             metadata: socket.metadata().unwrap(),
413             shutdown_type: RwLock::new(ShutdownType::empty()),
414             wait_queue: EventWaitQueue::new(),
415             epitems: SpinLock::new(LinkedList::new()),
416         }
417     }
418 
419     /// ### 在socket的等待队列上睡眠
sleep( socket_handle: SocketHandle, events: u64, handle_map_guard: RwLockReadGuard<'_, HashMap<SocketHandle, SocketHandleItem>>, )420     pub fn sleep(
421         socket_handle: SocketHandle,
422         events: u64,
423         handle_map_guard: RwLockReadGuard<'_, HashMap<SocketHandle, SocketHandleItem>>,
424     ) {
425         unsafe {
426             handle_map_guard
427                 .get(&socket_handle)
428                 .unwrap()
429                 .wait_queue
430                 .sleep_without_schedule(events)
431         };
432         drop(handle_map_guard);
433         sched();
434     }
435 
shutdown_type(&self) -> ShutdownType436     pub fn shutdown_type(&self) -> ShutdownType {
437         self.shutdown_type.read().clone()
438     }
439 
shutdown_type_writer(&mut self) -> RwLockWriteGuard<ShutdownType>440     pub fn shutdown_type_writer(&mut self) -> RwLockWriteGuard<ShutdownType> {
441         self.shutdown_type.write_irqsave()
442     }
443 
add_epoll(&mut self, epitem: Arc<EPollItem>)444     pub fn add_epoll(&mut self, epitem: Arc<EPollItem>) {
445         self.epitems.lock_irqsave().push_back(epitem)
446     }
447 
remove_epoll(&mut self, epoll: &Weak<SpinLock<EventPoll>>) -> Result<(), SystemError>448     pub fn remove_epoll(&mut self, epoll: &Weak<SpinLock<EventPoll>>) -> Result<(), SystemError> {
449         let is_remove = !self
450             .epitems
451             .lock_irqsave()
452             .extract_if(|x| x.epoll().ptr_eq(epoll))
453             .collect::<Vec<_>>()
454             .is_empty();
455 
456         if is_remove {
457             return Ok(());
458         }
459 
460         Err(SystemError::ENOENT)
461     }
462 }
463 
464 /// # TCP 和 UDP 的端口管理器。
465 /// 如果 TCP/UDP 的 socket 绑定了某个端口,它会在对应的表中记录,以检测端口冲突。
466 pub struct PortManager {
467     // TCP 端口记录表
468     tcp_port_table: SpinLock<HashMap<u16, Arc<GlobalSocketHandle>>>,
469     // UDP 端口记录表
470     udp_port_table: SpinLock<HashMap<u16, Arc<GlobalSocketHandle>>>,
471 }
472 
473 impl PortManager {
new() -> Self474     pub fn new() -> Self {
475         return Self {
476             tcp_port_table: SpinLock::new(HashMap::new()),
477             udp_port_table: SpinLock::new(HashMap::new()),
478         };
479     }
480 
481     /// @brief 自动分配一个相对应协议中未被使用的PORT,如果动态端口均已被占用,返回错误码 EADDRINUSE
get_ephemeral_port(&self, socket_type: SocketType) -> Result<u16, SystemError>482     pub fn get_ephemeral_port(&self, socket_type: SocketType) -> Result<u16, SystemError> {
483         // TODO: selects non-conflict high port
484 
485         static mut EPHEMERAL_PORT: u16 = 0;
486         unsafe {
487             if EPHEMERAL_PORT == 0 {
488                 EPHEMERAL_PORT = (49152 + rand() % (65536 - 49152)) as u16;
489             }
490         }
491 
492         let mut remaining = 65536 - 49152; // 剩余尝试分配端口次数
493         let mut port: u16;
494         while remaining > 0 {
495             unsafe {
496                 if EPHEMERAL_PORT == 65535 {
497                     EPHEMERAL_PORT = 49152;
498                 } else {
499                     EPHEMERAL_PORT = EPHEMERAL_PORT + 1;
500                 }
501                 port = EPHEMERAL_PORT;
502             }
503 
504             // 使用 ListenTable 检查端口是否被占用
505             let listen_table_guard = match socket_type {
506                 SocketType::UdpSocket => self.udp_port_table.lock(),
507                 SocketType::TcpSocket => self.tcp_port_table.lock(),
508                 _ => panic!("{:?} cann't get a port", socket_type),
509             };
510             if let None = listen_table_guard.get(&port) {
511                 drop(listen_table_guard);
512                 return Ok(port);
513             }
514             remaining -= 1;
515         }
516         return Err(SystemError::EADDRINUSE);
517     }
518 
519     /// @brief 检测给定端口是否已被占用,如果未被占用则在 TCP/UDP 对应的表中记录
520     ///
521     /// TODO: 增加支持端口复用的逻辑
bind_port( &self, socket_type: SocketType, port: u16, handle: Arc<GlobalSocketHandle>, ) -> Result<(), SystemError>522     pub fn bind_port(
523         &self,
524         socket_type: SocketType,
525         port: u16,
526         handle: Arc<GlobalSocketHandle>,
527     ) -> Result<(), SystemError> {
528         if port > 0 {
529             let mut listen_table_guard = match socket_type {
530                 SocketType::UdpSocket => self.udp_port_table.lock(),
531                 SocketType::TcpSocket => self.tcp_port_table.lock(),
532                 _ => panic!("{:?} cann't bind a port", socket_type),
533             };
534             match listen_table_guard.get(&port) {
535                 Some(_) => return Err(SystemError::EADDRINUSE),
536                 None => listen_table_guard.insert(port, handle),
537             };
538             drop(listen_table_guard);
539         }
540         return Ok(());
541     }
542 
543     /// @brief 在对应的端口记录表中将端口和 socket 解绑
unbind_port(&self, socket_type: SocketType, port: u16) -> Result<(), SystemError>544     pub fn unbind_port(&self, socket_type: SocketType, port: u16) -> Result<(), SystemError> {
545         let mut listen_table_guard = match socket_type {
546             SocketType::UdpSocket => self.udp_port_table.lock(),
547             SocketType::TcpSocket => self.tcp_port_table.lock(),
548             _ => return Ok(()),
549         };
550         listen_table_guard.remove(&port);
551         drop(listen_table_guard);
552         return Ok(());
553     }
554 }
555 
556 /// # socket的句柄管理组件
557 /// 它在smoltcp的SocketHandle上封装了一层,增加更多的功能。
558 /// 比如,在socket被关闭时,自动释放socket的资源,通知系统的其他组件。
559 #[derive(Debug)]
560 pub struct GlobalSocketHandle(SocketHandle);
561 
562 impl GlobalSocketHandle {
new(handle: SocketHandle) -> Arc<Self>563     pub fn new(handle: SocketHandle) -> Arc<Self> {
564         return Arc::new(Self(handle));
565     }
566 }
567 
568 impl Clone for GlobalSocketHandle {
clone(&self) -> Self569     fn clone(&self) -> Self {
570         Self(self.0)
571     }
572 }
573 
574 impl Drop for GlobalSocketHandle {
drop(&mut self)575     fn drop(&mut self) {
576         let mut socket_set_guard = SOCKET_SET.lock_irqsave();
577         socket_set_guard.remove(self.0); // 删除的时候,会发送一条FINISH的信息?
578         drop(socket_set_guard);
579         poll_ifaces();
580     }
581 }
582 
583 /// @brief socket的类型
584 #[derive(Debug, Clone, Copy, PartialEq)]
585 pub enum SocketType {
586     /// 原始的socket
587     RawSocket,
588     /// 用于Tcp通信的 Socket
589     TcpSocket,
590     /// 用于Udp通信的 Socket
591     UdpSocket,
592     /// 用于进程间通信的 Socket
593     SeqpacketSocket,
594 }
595 
596 bitflags! {
597     /// @brief socket的选项
598     #[derive(Default)]
599     pub struct SocketOptions: u32 {
600         /// 是否阻塞
601         const BLOCK = 1 << 0;
602         /// 是否允许广播
603         const BROADCAST = 1 << 1;
604         /// 是否允许多播
605         const MULTICAST = 1 << 2;
606         /// 是否允许重用地址
607         const REUSEADDR = 1 << 3;
608         /// 是否允许重用端口
609         const REUSEPORT = 1 << 4;
610     }
611 }
612 
613 #[derive(Debug, Clone)]
614 /// @brief 在trait Socket的metadata函数中返回该结构体供外部使用
615 pub struct SocketMetadata {
616     /// socket的类型
617     pub socket_type: SocketType,
618     /// 接收缓冲区的大小
619     pub rx_buf_size: usize,
620     /// 发送缓冲区的大小
621     pub tx_buf_size: usize,
622     /// 元数据的缓冲区的大小
623     pub metadata_buf_size: usize,
624     /// socket的选项
625     pub options: SocketOptions,
626 }
627 
628 impl SocketMetadata {
new( socket_type: SocketType, rx_buf_size: usize, tx_buf_size: usize, metadata_buf_size: usize, options: SocketOptions, ) -> Self629     fn new(
630         socket_type: SocketType,
631         rx_buf_size: usize,
632         tx_buf_size: usize,
633         metadata_buf_size: usize,
634         options: SocketOptions,
635     ) -> Self {
636         Self {
637             socket_type,
638             rx_buf_size,
639             tx_buf_size,
640             metadata_buf_size,
641             options,
642         }
643     }
644 }
645 
646 /// @brief 地址族的枚举
647 ///
648 /// 参考:https://code.dragonos.org.cn/xref/linux-5.19.10/include/linux/socket.h#180
649 #[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive, ToPrimitive)]
650 pub enum AddressFamily {
651     /// AF_UNSPEC 表示地址族未指定
652     Unspecified = 0,
653     /// AF_UNIX 表示Unix域的socket (与AF_LOCAL相同)
654     Unix = 1,
655     ///  AF_INET 表示IPv4的socket
656     INet = 2,
657     /// AF_AX25 表示AMPR AX.25的socket
658     AX25 = 3,
659     /// AF_IPX 表示IPX的socket
660     IPX = 4,
661     /// AF_APPLETALK 表示Appletalk的socket
662     Appletalk = 5,
663     /// AF_NETROM 表示AMPR NET/ROM的socket
664     Netrom = 6,
665     /// AF_BRIDGE 表示多协议桥接的socket
666     Bridge = 7,
667     /// AF_ATMPVC 表示ATM PVCs的socket
668     Atmpvc = 8,
669     /// AF_X25 表示X.25的socket
670     X25 = 9,
671     /// AF_INET6 表示IPv6的socket
672     INet6 = 10,
673     /// AF_ROSE 表示AMPR ROSE的socket
674     Rose = 11,
675     /// AF_DECnet Reserved for DECnet project
676     Decnet = 12,
677     /// AF_NETBEUI Reserved for 802.2LLC project
678     Netbeui = 13,
679     /// AF_SECURITY 表示Security callback的伪AF
680     Security = 14,
681     /// AF_KEY 表示Key management API
682     Key = 15,
683     /// AF_NETLINK 表示Netlink的socket
684     Netlink = 16,
685     /// AF_PACKET 表示Low level packet interface
686     Packet = 17,
687     /// AF_ASH 表示Ash
688     Ash = 18,
689     /// AF_ECONET 表示Acorn Econet
690     Econet = 19,
691     /// AF_ATMSVC 表示ATM SVCs
692     Atmsvc = 20,
693     /// AF_RDS 表示Reliable Datagram Sockets
694     Rds = 21,
695     /// AF_SNA 表示Linux SNA Project
696     Sna = 22,
697     /// AF_IRDA 表示IRDA sockets
698     Irda = 23,
699     /// AF_PPPOX 表示PPPoX sockets
700     Pppox = 24,
701     /// AF_WANPIPE 表示WANPIPE API sockets
702     WanPipe = 25,
703     /// AF_LLC 表示Linux LLC
704     Llc = 26,
705     /// AF_IB 表示Native InfiniBand address
706     /// 介绍:https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html-single/configuring_infiniband_and_rdma_networks/index#understanding-infiniband-and-rdma_configuring-infiniband-and-rdma-networks
707     Ib = 27,
708     /// AF_MPLS 表示MPLS
709     Mpls = 28,
710     /// AF_CAN 表示Controller Area Network
711     Can = 29,
712     /// AF_TIPC 表示TIPC sockets
713     Tipc = 30,
714     /// AF_BLUETOOTH 表示Bluetooth sockets
715     Bluetooth = 31,
716     /// AF_IUCV 表示IUCV sockets
717     Iucv = 32,
718     /// AF_RXRPC 表示RxRPC sockets
719     Rxrpc = 33,
720     /// AF_ISDN 表示mISDN sockets
721     Isdn = 34,
722     /// AF_PHONET 表示Phonet sockets
723     Phonet = 35,
724     /// AF_IEEE802154 表示IEEE 802.15.4 sockets
725     Ieee802154 = 36,
726     /// AF_CAIF 表示CAIF sockets
727     Caif = 37,
728     /// AF_ALG 表示Algorithm sockets
729     Alg = 38,
730     /// AF_NFC 表示NFC sockets
731     Nfc = 39,
732     /// AF_VSOCK 表示vSockets
733     Vsock = 40,
734     /// AF_KCM 表示Kernel Connection Multiplexor
735     Kcm = 41,
736     /// AF_QIPCRTR 表示Qualcomm IPC Router
737     Qipcrtr = 42,
738     /// AF_SMC 表示SMC-R sockets.
739     /// reserve number for PF_SMC protocol family that reuses AF_INET address family
740     Smc = 43,
741     /// AF_XDP 表示XDP sockets
742     Xdp = 44,
743     /// AF_MCTP 表示Management Component Transport Protocol
744     Mctp = 45,
745     /// AF_MAX 表示最大的地址族
746     Max = 46,
747 }
748 
749 impl TryFrom<u16> for AddressFamily {
750     type Error = SystemError;
try_from(x: u16) -> Result<Self, Self::Error>751     fn try_from(x: u16) -> Result<Self, Self::Error> {
752         use num_traits::FromPrimitive;
753         return <Self as FromPrimitive>::from_u16(x).ok_or_else(|| SystemError::EINVAL);
754     }
755 }
756 
757 /// @brief posix套接字类型的枚举(这些值与linux内核中的值一致)
758 #[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive, ToPrimitive)]
759 pub enum PosixSocketType {
760     Stream = 1,
761     Datagram = 2,
762     Raw = 3,
763     Rdm = 4,
764     SeqPacket = 5,
765     Dccp = 6,
766     Packet = 10,
767 }
768 
769 impl TryFrom<u8> for PosixSocketType {
770     type Error = SystemError;
try_from(x: u8) -> Result<Self, Self::Error>771     fn try_from(x: u8) -> Result<Self, Self::Error> {
772         use num_traits::FromPrimitive;
773         return <Self as FromPrimitive>::from_u8(x).ok_or_else(|| SystemError::EINVAL);
774     }
775 }
776 
777 /// ### 为socket提供无锁的poll方法
778 ///
779 /// 因为在网卡中断中,需要轮询socket的状态,如果使用socket文件或者其inode来poll
780 /// 在当前的设计,会必然死锁,所以引用这一个设计来解决,提供无��的poll
781 pub struct SocketPollMethod;
782 
783 impl SocketPollMethod {
poll(socket: &socket::Socket, shutdown: ShutdownType) -> EPollEventType784     pub fn poll(socket: &socket::Socket, shutdown: ShutdownType) -> EPollEventType {
785         match socket {
786             socket::Socket::Udp(udp) => Self::udp_poll(udp, shutdown),
787             socket::Socket::Tcp(tcp) => Self::tcp_poll(tcp, shutdown),
788             _ => todo!(),
789         }
790     }
791 
tcp_poll(socket: &tcp::Socket, shutdown: ShutdownType) -> EPollEventType792     pub fn tcp_poll(socket: &tcp::Socket, shutdown: ShutdownType) -> EPollEventType {
793         let mut events = EPollEventType::empty();
794         if socket.is_listening() && socket.is_active() {
795             events.insert(EPollEventType::EPOLLIN | EPollEventType::EPOLLRDNORM);
796             return events;
797         }
798 
799         // socket已经关闭
800         if !socket.is_open() {
801             events.insert(EPollEventType::EPOLLHUP)
802         }
803         if shutdown.contains(ShutdownType::RCV_SHUTDOWN) {
804             events.insert(
805                 EPollEventType::EPOLLIN | EPollEventType::EPOLLRDNORM | EPollEventType::EPOLLRDHUP,
806             );
807         }
808 
809         let state = socket.state();
810         if state != tcp::State::SynSent && state != tcp::State::SynReceived {
811             // socket有可读数据
812             if socket.can_recv() {
813                 events.insert(EPollEventType::EPOLLIN | EPollEventType::EPOLLRDNORM);
814             }
815 
816             if !(shutdown.contains(ShutdownType::SEND_SHUTDOWN)) {
817                 // 缓冲区可写
818                 if socket.send_queue() < socket.send_capacity() {
819                     events.insert(EPollEventType::EPOLLOUT | EPollEventType::EPOLLWRNORM);
820                 } else {
821                     // TODO:触发缓冲区已满的信号
822                     todo!("A signal that the buffer is full needs to be sent");
823                 }
824             } else {
825                 // 如果我们的socket关闭了SEND_SHUTDOWN,epoll事件就是EPOLLOUT
826                 events.insert(EPollEventType::EPOLLOUT | EPollEventType::EPOLLWRNORM);
827             }
828         } else if state == tcp::State::SynSent {
829             events.insert(EPollEventType::EPOLLOUT | EPollEventType::EPOLLWRNORM);
830         }
831 
832         // socket发生错误
833         if !socket.is_active() {
834             events.insert(EPollEventType::EPOLLERR);
835         }
836 
837         events
838     }
839 
udp_poll(socket: &udp::Socket, shutdown: ShutdownType) -> EPollEventType840     pub fn udp_poll(socket: &udp::Socket, shutdown: ShutdownType) -> EPollEventType {
841         let mut event = EPollEventType::empty();
842 
843         if shutdown.contains(ShutdownType::RCV_SHUTDOWN) {
844             event.insert(
845                 EPollEventType::EPOLLRDHUP | EPollEventType::EPOLLIN | EPollEventType::EPOLLRDNORM,
846             );
847         }
848         if shutdown.contains(ShutdownType::SHUTDOWN_MASK) {
849             event.insert(EPollEventType::EPOLLHUP);
850         }
851 
852         if socket.can_recv() {
853             event.insert(EPollEventType::EPOLLIN | EPollEventType::EPOLLRDNORM);
854         }
855 
856         if socket.can_send() {
857             event.insert(
858                 EPollEventType::EPOLLOUT
859                     | EPollEventType::EPOLLWRNORM
860                     | EPollEventType::EPOLLWRBAND,
861             );
862         } else {
863             // TODO: 缓冲区空间不够,需要使用信号处理
864             todo!()
865         }
866 
867         return event;
868     }
869 }
870