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