Skip to content

Commit 575e175

Browse files
smallcjyMemoryShore1037827920fslongjinGodones
authored
feat: 初步实现Unix stream socket (#916)
* 修复mprotect系统调用未正确设置vm_flags的错误 (#847) * fix(time): modify update wall time (#836) 更改了时间子系统的update_wall_time函数,通过读取当前周期数,计算delta值进行更新,而不是通过传入delta值进行更新 * chore: 调整triagebot.toml以适应新的组织架构 (#848) * doc: 完善README.md (#849) * doc: 完善README.md * chore: 更新sphinx相关配置,适应read the docs的更新 (#850) 根据read the docs在7月15日blog,进行此修改 https://about.readthedocs.com/blog/2024/07/addons-by-default/ * feat(driver/net): 实现Loopback网卡接口 (#845) * 初步实现loopback设备 * fix: build-scripts和tools目录下的make check指定工具链版本 (#861) * fix: tcp poll没有正确处理posix socket的listen状态的问题 (#859) * chore: 将工具链更新到2024-07-23 (#864) * chore: 将工具链更新到2024-07-23 * feat(fs): add eventfd syscall support (#858) * feat(fs): add eventfd syscall support * refactor: 删除过时的va-pa转换函数,改为统一使用MMArch (#862) * 默认nightly-2024-07-23 & config改为config.toml (#872) * fix: 修复由于升级到2024-07-23工具链之后,某些机器上面内核运行一直fault的问题。 (#870) * fix: 修复由于升级到2024-07-23工具链之后,某些机器上面内核运行一直fault的问题。 * feat(cred): 初步实现Cred (#846) * 初步实现Cred * 添加seteuid和setegid * 添加cred测试程序 * 修改Cred::fscmp返回结果为CredFsCmp枚举 * 完善root用户相关信息 * fix: 修复键盘码解析器没能正确处理类似ctrl C的控制字符的问题 (#877) * fix: 修复键盘码解析器没能正确处理类似ctrl C的控制字符的问题 * fix: 解决ntty潜在的panic问题 * ci: enable ci workflow on branches other than master (#891) * 修复unlink、unlinkat系统调用的路径错误 (#892) * fix: socket shutdown wrong implement (#893) * feat: 增加tokio异步运行时支持 (#894) * fix the EventFdFlags error * feat: support tokio (Single thread version) Fix deadlock issue on closing file. Add function for PipeInode and EventFdInode. * feat: 实现unix stream sock和其状态 * 握手支持 * feat:建立连接功能实现 * feat:unix stream socket 初版 * fix: 消除标红 * feat: 阻塞式读写buffer * feat: 实现unix socket buffer * 不小心改了inode --------- Co-authored-by: MemoryShore <[email protected]> Co-authored-by: 黄铭涛 <[email protected]> Co-authored-by: LoGin <[email protected]> Co-authored-by: linfeng <[email protected]> Co-authored-by: Jomo <[email protected]> Co-authored-by: Chiichen <[email protected]> Co-authored-by: Samuel Dai <[email protected]>
1 parent 10bce9d commit 575e175

File tree

9 files changed

+557
-240
lines changed

9 files changed

+557
-240
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,5 @@
161161
],
162162
"rust-analyzer.showUnlinkedFileNotification": false,
163163
"editor.accessibilityPageSize": 15,
164+
"makefile.configureOnOpen": false,
164165
}

kernel/src/net/socket/base.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use core::fmt::Debug;
44
use alloc::sync::Arc;
55
use system_error::SystemError::{self, *};
6-
use crate::filesystem::vfs::IndexNode;
76
use crate::net::socket::*;
87
use crate::net::syscall_util::MsgHdr;
98

@@ -27,7 +26,7 @@ pub trait Socket: Sync + Send + Debug {
2726
/// 接受连接,仅用于listening stream socket
2827
/// ## Block
2928
/// 如果没有连接到来,会阻塞
30-
fn accept(&self) -> Result<(Arc<dyn IndexNode>, Endpoint), SystemError> {
29+
fn accept(&self) -> Result<(Arc<Inode>, Endpoint), SystemError> {
3130
Err(ENOSYS)
3231
}
3332

kernel/src/net/socket/buffer.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
use alloc::vec::Vec;
2+
3+
use alloc::sync::Arc;
4+
use system_error::SystemError;
5+
6+
use crate::libs::spinlock::SpinLock;
7+
8+
#[derive(Debug)]
9+
pub struct Buffer {
10+
metadata: Metadata,
11+
read_buffer: SpinLock<Vec<u8>>,
12+
write_buffer: SpinLock<Vec<u8>>,
13+
}
14+
15+
impl Buffer {
16+
pub fn new() -> Arc<Self> {
17+
Arc::new(Self {
18+
metadata: Metadata::default(),
19+
read_buffer: SpinLock::new(Vec::new()),
20+
write_buffer: SpinLock::new(Vec::new())
21+
})
22+
}
23+
24+
pub fn is_read_buf_empty(&self) -> bool {
25+
return self.read_buffer.lock().is_empty();
26+
}
27+
28+
pub fn is_write_buf_empty(&self) -> bool {
29+
return self.write_buffer.lock().is_empty();
30+
}
31+
32+
pub fn read_read_buffer(&self, buf: &mut [u8]) -> Result<usize, SystemError> {
33+
let mut read_buffer = self.read_buffer.lock_irqsave();
34+
let len = core::cmp::min(buf.len(), read_buffer.len());
35+
buf[..len].copy_from_slice(&read_buffer[..len]);
36+
read_buffer.split_off(len);
37+
return Ok(len);
38+
}
39+
40+
pub fn write_read_buffer(&self, buf: &[u8]) -> Result<usize, SystemError> {
41+
let mut buffer = self.read_buffer.lock_irqsave();
42+
43+
let len = buf.len();
44+
if self.metadata.buf_size - buffer.len() < len {
45+
return Err(SystemError::ENOBUFS);
46+
}
47+
buffer.extend_from_slice(buf);
48+
49+
Ok(len)
50+
}
51+
52+
}
53+
54+
#[derive(Debug)]
55+
pub struct Metadata {
56+
/// 默认的元数据缓冲区大小
57+
metadata_buf_size: usize,
58+
/// 默认的缓冲区大小
59+
buf_size: usize,
60+
}
61+
62+
impl Default for Metadata {
63+
fn default() -> Self {
64+
Self {
65+
metadata_buf_size: 1024,
66+
buf_size: 64 * 1024,
67+
}
68+
}
69+
}
70+

kernel/src/net/socket/inet/stream/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,9 @@ impl Socket for TcpSocket {
329329
}
330330
}
331331

332-
fn accept(&self) -> Result<(Arc<dyn IndexNode>, crate::net::Endpoint), SystemError> {
332+
fn accept(&self) -> Result<(Arc<Inode>, crate::net::socket::Endpoint), SystemError> {
333333
self.try_accept().map(|(stream, remote)|
334-
(stream as Arc<dyn IndexNode>, crate::net::Endpoint::from(remote))
334+
(stream as Arc<Inode>, crate::net::socket::Endpoint::from(remote))
335335
)
336336
}
337337
}
@@ -376,7 +376,7 @@ impl TcpStream {
376376
}
377377
}
378378

379-
use crate::net::socket::Socket;
379+
use crate::net::socket::{Inode, Socket};
380380
use crate::filesystem::vfs::IndexNode;
381381

382382
impl IndexNode for TcpStream {

kernel/src/net/socket/inode.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ impl Socket for Inode {
7171
}
7272

7373
impl Inode {
74+
pub fn new(socket: Arc<dyn Socket>) -> Arc<Self>{
75+
return Arc::new(Self{inner: socket.clone()});
76+
}
77+
7478
pub fn set_nonblock(&self, nonblock: bool) {
7579
todo!()
7680
}

kernel/src/net/socket/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod inode;
77
mod family;
88
mod utils;
99
mod base;
10+
mod buffer;
1011
mod endpoint;
1112

1213
pub use define::*;

kernel/src/net/socket/unix/mod.rs

Lines changed: 1 addition & 235 deletions
Original file line numberDiff line numberDiff line change
@@ -1,235 +1 @@
1-
use core::any::Any;
2-
3-
use alloc::{boxed::Box, sync::Arc, vec::Vec};
4-
use system_error::SystemError;
5-
6-
use crate::{libs::spinlock::SpinLock, net::Endpoint};
7-
8-
use super::{
9-
handle::GlobalSocketHandle, PosixSocketHandleItem, Socket, inode::Inode, SocketMetadata,
10-
Options, InetSocketType,
11-
};
12-
13-
#[derive(Debug, Clone)]
14-
pub struct StreamSocket {
15-
metadata: SocketMetadata,
16-
buffer: Arc<SpinLock<Vec<u8>>>,
17-
peer_inode: Option<Arc<Inode>>,
18-
handle: GlobalSocketHandle,
19-
posix_item: Arc<PosixSocketHandleItem>,
20-
}
21-
22-
impl StreamSocket {
23-
/// 默认的元数据缓冲区大小
24-
pub const DEFAULT_METADATA_BUF_SIZE: usize = 1024;
25-
/// 默认的缓冲区大小
26-
pub const DEFAULT_BUF_SIZE: usize = 64 * 1024;
27-
28-
/// # 创建一个 Stream Socket
29-
///
30-
/// ## 参数
31-
/// - `options`: socket选项
32-
pub fn new(options: Options) -> Self {
33-
let buffer = Arc::new(SpinLock::new(Vec::with_capacity(Self::DEFAULT_BUF_SIZE)));
34-
35-
let metadata = SocketMetadata::new(
36-
InetSocketType::Unix,
37-
Self::DEFAULT_BUF_SIZE,
38-
Self::DEFAULT_BUF_SIZE,
39-
Self::DEFAULT_METADATA_BUF_SIZE,
40-
options,
41-
);
42-
43-
let posix_item = Arc::new(PosixSocketHandleItem::new(None));
44-
45-
Self {
46-
metadata,
47-
buffer,
48-
peer_inode: None,
49-
handle: GlobalSocketHandle::new_kernel_handle(),
50-
posix_item,
51-
}
52-
}
53-
}
54-
55-
impl Socket for StreamSocket {
56-
fn socket_handle(&self) -> GlobalSocketHandle {
57-
self.handle
58-
}
59-
60-
fn close(&mut self) {}
61-
62-
fn read(&self, buf: &mut [u8]) -> (Result<usize, SystemError>, Endpoint) {
63-
let mut buffer = self.buffer.lock_irqsave();
64-
65-
let len = core::cmp::min(buf.len(), buffer.len());
66-
buf[..len].copy_from_slice(&buffer[..len]);
67-
68-
let _ = buffer.split_off(len);
69-
70-
(Ok(len), Endpoint::Inode(self.peer_inode.clone()))
71-
}
72-
73-
fn write(&self, buf: &[u8], _to: Option<Endpoint>) -> Result<usize, SystemError> {
74-
if self.peer_inode.is_none() {
75-
return Err(SystemError::ENOTCONN);
76-
}
77-
78-
let peer_inode = self.peer_inode.clone().unwrap();
79-
let len = peer_inode.inner().write_buffer(buf)?;
80-
Ok(len)
81-
}
82-
83-
fn connect(&mut self, endpoint: Endpoint) -> Result<(), SystemError> {
84-
if self.peer_inode.is_some() {
85-
return Err(SystemError::EISCONN);
86-
}
87-
88-
if let Endpoint::Inode(inode) = endpoint {
89-
self.peer_inode = inode;
90-
Ok(())
91-
} else {
92-
Err(SystemError::EINVAL)
93-
}
94-
}
95-
96-
fn write_buffer(&self, buf: &[u8]) -> Result<usize, SystemError> {
97-
let mut buffer = self.buffer.lock_irqsave();
98-
99-
let len = buf.len();
100-
if buffer.capacity() - buffer.len() < len {
101-
return Err(SystemError::ENOBUFS);
102-
}
103-
buffer.extend_from_slice(buf);
104-
105-
Ok(len)
106-
}
107-
108-
fn metadata(&self) -> SocketMetadata {
109-
self.metadata.clone()
110-
}
111-
112-
fn box_clone(&self) -> Box<dyn Socket> {
113-
Box::new(self.clone())
114-
}
115-
116-
fn as_any_ref(&self) -> &dyn core::any::Any {
117-
self
118-
}
119-
120-
fn as_any_mut(&mut self) -> &mut dyn core::any::Any {
121-
self
122-
}
123-
}
124-
125-
#[derive(Debug, Clone)]
126-
pub struct SeqpacketSocket {
127-
metadata: SocketMetadata,
128-
buffer: Arc<SpinLock<Vec<u8>>>,
129-
peer_inode: Option<Arc<Inode>>,
130-
handle: GlobalSocketHandle,
131-
posix_item: Arc<PosixSocketHandleItem>,
132-
}
133-
134-
impl SeqpacketSocket {
135-
/// 默认的元数据缓冲区大小
136-
pub const DEFAULT_METADATA_BUF_SIZE: usize = 1024;
137-
/// 默认的缓冲区大小
138-
pub const DEFAULT_BUF_SIZE: usize = 64 * 1024;
139-
140-
/// # 创建一个 Seqpacket Socket
141-
///
142-
/// ## 参数
143-
/// - `options`: socket选项
144-
pub fn new(options: Options) -> Self {
145-
let buffer = Arc::new(SpinLock::new(Vec::with_capacity(Self::DEFAULT_BUF_SIZE)));
146-
147-
let metadata = SocketMetadata::new(
148-
InetSocketType::Unix,
149-
Self::DEFAULT_BUF_SIZE,
150-
Self::DEFAULT_BUF_SIZE,
151-
Self::DEFAULT_METADATA_BUF_SIZE,
152-
options,
153-
);
154-
155-
let posix_item = Arc::new(PosixSocketHandleItem::new(None));
156-
157-
Self {
158-
metadata,
159-
buffer,
160-
peer_inode: None,
161-
handle: GlobalSocketHandle::new_kernel_handle(),
162-
posix_item,
163-
}
164-
}
165-
}
166-
167-
impl Socket for SeqpacketSocket {
168-
fn close(&mut self) {}
169-
170-
fn read(&self, buf: &mut [u8]) -> (Result<usize, SystemError>, Endpoint) {
171-
let mut buffer = self.buffer.lock_irqsave();
172-
173-
let len = core::cmp::min(buf.len(), buffer.len());
174-
buf[..len].copy_from_slice(&buffer[..len]);
175-
176-
let _ = buffer.split_off(len);
177-
178-
(Ok(len), Endpoint::Inode(self.peer_inode.clone()))
179-
}
180-
181-
fn write(&self, buf: &[u8], _to: Option<Endpoint>) -> Result<usize, SystemError> {
182-
if self.peer_inode.is_none() {
183-
return Err(SystemError::ENOTCONN);
184-
}
185-
186-
let peer_inode = self.peer_inode.clone().unwrap();
187-
let len = peer_inode.inner().write_buffer(buf)?;
188-
Ok(len)
189-
}
190-
191-
fn connect(&mut self, endpoint: Endpoint) -> Result<(), SystemError> {
192-
if self.peer_inode.is_some() {
193-
return Err(SystemError::EISCONN);
194-
}
195-
196-
if let Endpoint::Inode(inode) = endpoint {
197-
self.peer_inode = inode;
198-
Ok(())
199-
} else {
200-
Err(SystemError::EINVAL)
201-
}
202-
}
203-
204-
fn write_buffer(&self, buf: &[u8]) -> Result<usize, SystemError> {
205-
let mut buffer = self.buffer.lock_irqsave();
206-
207-
let len = buf.len();
208-
if buffer.capacity() - buffer.len() < len {
209-
return Err(SystemError::ENOBUFS);
210-
}
211-
buffer.extend_from_slice(buf);
212-
213-
Ok(len)
214-
}
215-
216-
fn socket_handle(&self) -> GlobalSocketHandle {
217-
self.handle
218-
}
219-
220-
fn metadata(&self) -> SocketMetadata {
221-
self.metadata.clone()
222-
}
223-
224-
fn box_clone(&self) -> Box<dyn Socket> {
225-
Box::new(self.clone())
226-
}
227-
228-
fn as_any_ref(&self) -> &dyn core::any::Any {
229-
self
230-
}
231-
232-
fn as_any_mut(&mut self) -> &mut dyn core::any::Any {
233-
self
234-
}
235-
}
1+
mod stream;

0 commit comments

Comments
 (0)