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