Skip to content

Commit df921d3

Browse files
socket: more socket work
Signed-off-by: Andy-Python-Programmer <[email protected]>
1 parent 3246e58 commit df921d3

File tree

11 files changed

+136
-12
lines changed

11 files changed

+136
-12
lines changed

aero.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ def run_in_emulator(args, iso_path):
524524

525525
qemu_args = ['-cdrom', iso_path,
526526
'-M', 'q35',
527-
'-m', '5G',
527+
'-m', '6G',
528528
'-smp', '5',
529529
'-serial', 'stdio']
530530

patches/mlibc/mlibc.patch

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
From 24a4e9494f8b2d78936d08ffdfe4cf84cf35e76b Mon Sep 17 00:00:00 2001
1+
From 796581911f51ff414a2fa03e53fd470a1f440329 Mon Sep 17 00:00:00 2001
22
From: Andy-Python-Programmer <[email protected]>
33
Date: Fri, 8 Jul 2022 12:32:32 +1000
44
Subject: [PATCH] yes
@@ -8,12 +8,13 @@ Signed-off-by: Andy-Python-Programmer <[email protected]>
88
.gitignore | 2 +
99
options/ansi/generic/stdlib-stubs.cpp | 142 +++++++++++++++++++++-----
1010
options/glibc/generic/execinfo.cpp | 5 +-
11+
options/posix/generic/sys-uio.cpp | 5 +
1112
options/rtdl/generic/linker.cpp | 2 +-
1213
sysdeps/aero/generic/aero.cpp | 38 +++----
1314
sysdeps/aero/generic/filesystem.cpp | 25 ++++-
1415
sysdeps/aero/generic/sockets.cpp | 77 +++++++++++++-
1516
sysdeps/aero/include/aero/syscall.h | 4 +
16-
8 files changed, 241 insertions(+), 54 deletions(-)
17+
9 files changed, 246 insertions(+), 54 deletions(-)
1718

1819
diff --git a/.gitignore b/.gitignore
1920
index fdd60a00..9f811f47 100644
@@ -195,6 +196,22 @@ index 3474615e..10a2109e 100644
195196
}
196197

197198
char **backtrace_symbols(void *const *, int) {
199+
diff --git a/options/posix/generic/sys-uio.cpp b/options/posix/generic/sys-uio.cpp
200+
index b1623525..a5f3e893 100644
201+
--- a/options/posix/generic/sys-uio.cpp
202+
+++ b/options/posix/generic/sys-uio.cpp
203+
@@ -17,6 +17,11 @@ ssize_t readv(int, const struct iovec *, int) {
204+
}
205+
206+
ssize_t writev(int fd, const struct iovec *iovs, int iovc) {
207+
+ if (iovc == 0) {
208+
+ errno = EAGAIN;
209+
+ return -1;
210+
+ }
211+
+
212+
__ensure(iovc);
213+
214+
ssize_t written = 0;
198215
diff --git a/options/rtdl/generic/linker.cpp b/options/rtdl/generic/linker.cpp
199216
index 6716ef4f..e5ec8cff 100644
200217
--- a/options/rtdl/generic/linker.cpp

src/aero_kernel/src/drivers/tty.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ impl INodeInterface for Tty {
253253
state.parser.advance(&mut performer, *character);
254254
}
255255

256+
log::debug!("TTY::write_at(): {}", unsafe {
257+
core::str::from_utf8_unchecked(buffer)
258+
});
259+
256260
Ok(buffer.len())
257261
}
258262

src/aero_kernel/src/fs/inode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ pub trait INodeInterface: Send + Sync {
232232
Err(FileSystemError::NotSocket)
233233
}
234234

235-
fn recv(&self, _message_header: &mut MessageHeader) -> Result<()> {
235+
fn recv(&self, _message_header: &mut MessageHeader) -> Result<usize> {
236236
Err(FileSystemError::NotSocket)
237237
}
238238

src/aero_kernel/src/fs/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ pub enum FileSystemError {
134134
InvalidPath,
135135
NotSocket,
136136
ConnectionRefused,
137+
NotConnected,
138+
WouldBlock,
137139
}
138140

139141
impl From<FileSystemError> for SyscallError {
@@ -151,6 +153,8 @@ impl From<FileSystemError> for SyscallError {
151153
FileSystemError::NotSocket => Self::ENOTSOCK,
152154
FileSystemError::ConnectionRefused => Self::ECONNREFUSED,
153155
FileSystemError::IsDir => Self::EISDIR,
156+
FileSystemError::NotConnected => Self::ENOTCONN,
157+
FileSystemError::WouldBlock => Self::EAGAIN,
154158
}
155159
}
156160
}

src/aero_kernel/src/socket/unix.rs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ struct UnixSocketInner {
8787
listening: bool,
8888
peer: Option<Arc<UnixSocket>>,
8989
connected: bool,
90+
address: SocketAddrUnix,
9091
}
9192

9293
pub struct UnixSocket {
@@ -110,9 +111,22 @@ impl UnixSocket {
110111
})
111112
}
112113

114+
pub fn set_address(&self, address: SocketAddrUnix) {
115+
self.inner.lock_irq().address = address;
116+
}
117+
118+
pub fn get_address(&self) -> SocketAddrUnix {
119+
self.inner.lock_irq().address.clone()
120+
}
121+
113122
pub fn sref(&self) -> Arc<Self> {
114123
self.weak.upgrade().unwrap()
115124
}
125+
126+
/// Returns wether the socket is connected or not.
127+
pub fn is_connected(&self) -> bool {
128+
self.inner.lock_irq().connected
129+
}
116130
}
117131

118132
impl INodeInterface for UnixSocket {
@@ -160,6 +174,7 @@ impl INodeInterface for UnixSocket {
160174

161175
// create the socket inode.
162176
DirEntry::from_socket_inode(fs::lookup_path(parent)?, String::from(name), self.sref())?;
177+
self.set_address(address.clone());
163178

164179
Ok(())
165180
}
@@ -204,7 +219,11 @@ impl INodeInterface for UnixSocket {
204219
return Err(FileSystemError::ConnectionRefused);
205220
}
206221

207-
let mut this = self.wq.block_on(&self.inner, |e| e.backlog.len() != 0)?;
222+
let mut this = self.inner.lock_irq();
223+
224+
if this.backlog.len() == 0 {
225+
return Err(FileSystemError::WouldBlock);
226+
}
208227

209228
let peer = this
210229
.backlog
@@ -224,12 +243,31 @@ impl INodeInterface for UnixSocket {
224243
peer_data.connected = true;
225244
}
226245

246+
sock.set_address(peer.get_address());
247+
227248
peer.wq.notify_complete();
228249
Ok(sock)
229250
}
230251

231-
fn recv(&self, _message_header: &mut MessageHeader) -> Result<()> {
232-
Ok(())
252+
fn recv(&self, header: &mut MessageHeader) -> Result<usize> {
253+
if !self.is_connected() {
254+
return Err(FileSystemError::NotConnected);
255+
}
256+
257+
let size = header.iovecs().iter().map(|e| e.len()).sum::<usize>();
258+
259+
let mut buffer = self.wq.block_on(&self.buffer, |e| e.len() >= size)?;
260+
log::trace!("UnixSocket::recv(): recieved total of {size} bytes");
261+
262+
header
263+
.name_mut::<SocketAddrUnix>()
264+
.map(|e| *e = self.inner.lock_irq().peer.as_ref().unwrap().get_address());
265+
266+
Ok(header
267+
.iovecs_mut()
268+
.iter_mut()
269+
.map(|iovec| buffer.read_data(iovec.as_mut_slice()))
270+
.sum::<usize>())
233271
}
234272

235273
fn poll(&self, table: Option<&mut PollTable>) -> Result<PollFlags> {

src/aero_kernel/src/syscall/net.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ pub fn sock_recv(
6767
.get_handle(sockfd)
6868
.ok_or(SyscallError::EINVAL)?;
6969

70-
socket.inode().recv(header)?;
71-
Ok(0)
70+
Ok(socket.inode().recv(header)?)
7271
}
7372

7473
/// Marks the socket as a passive socket (i.e. as a socket that will be used to accept incoming

src/aero_kernel/src/userland/vm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ impl VmProtected {
10841084
}
10851085

10861086
fn fork_from(&mut self, parent: &Vm) {
1087-
let data = parent.inner.lock();
1087+
let data = parent.inner.lock_irq();
10881088

10891089
// Copy over all of the mappings from the parent into the child.
10901090
self.mappings = data.mappings.clone();

src/aero_kernel/src/utils/buffer.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ impl Buffer {
3434
!self.data.is_empty()
3535
}
3636

37+
/// Returns the size of the buffer.
38+
pub fn len(&self) -> usize {
39+
self.data.len()
40+
}
41+
3742
pub fn read_data(&mut self, buffer: &mut [u8]) -> usize {
3843
// nothing to read
3944
if self.data.is_empty() {

src/aero_syscall/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,13 +666,24 @@ pub fn sys_sethostname(name: &str) -> Result<usize, SyscallError> {
666666
}
667667

668668
// Sockets
669+
pub trait SocketAddr: Send + Sync {}
670+
669671
#[derive(Debug, Clone)]
670672
#[repr(C)]
671673
pub struct SocketAddrUnix {
672674
pub family: u32,
673675
pub path: [u8; 108],
674676
}
675677

678+
impl Default for SocketAddrUnix {
679+
fn default() -> Self {
680+
Self {
681+
family: AF_UNIX,
682+
path: [0; 108],
683+
}
684+
}
685+
}
686+
676687
#[derive(Debug, Clone)]
677688
#[repr(C)]
678689
pub struct SocketAddrInet {
@@ -682,6 +693,9 @@ pub struct SocketAddrInet {
682693
pub padding: [u8; 8],
683694
}
684695

696+
impl SocketAddr for SocketAddrUnix {}
697+
impl SocketAddr for SocketAddrInet {}
698+
685699
// constants for the socket types:
686700
//
687701
// mlibc/sysdeps/aero/include/abi-bits/socket.h

src/aero_syscall/src/socket.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
use crate::SocketAddr;
2+
13
// sysdeps/aero/include/abi-bits/socket.h
24
#[derive(Debug)]
35
#[repr(C)]
46
pub struct MessageHeader {
5-
name: *const u8,
7+
/// Pointer to the socket address structure.
8+
name: *mut u8,
9+
/// Size of the socket address structure.
610
name_len: usize,
711

812
iovec: *mut IoVec, // todo: use Option<NonNull<IoVec>>
@@ -14,10 +18,49 @@ pub struct MessageHeader {
1418
flags: i32, // todo: use ffi::c_int
1519
}
1620

21+
impl MessageHeader {
22+
pub fn name_mut<T: SocketAddr>(&mut self) -> Option<&mut T> {
23+
if self.name.is_null() {
24+
return None;
25+
}
26+
27+
assert!(self.name_len == core::mem::size_of::<T>());
28+
29+
// SAFETY: We know that the `name` pointer is valid and we have an exclusive reference to it.
30+
// The size of name is checked above with the size of `T` and `T` is a `SocketAddr` so, its
31+
// safe to create a mutable reference of `T` from the ptr.
32+
unsafe { Some(&mut *(self.name as *mut T)) }
33+
}
34+
35+
pub fn iovecs(&self) -> &[IoVec] {
36+
// SAFETY: We know that the `iovec` pointer is valid, initialized.
37+
unsafe { core::slice::from_raw_parts(self.iovec, self.iovec_len as usize) }
38+
}
39+
40+
pub fn iovecs_mut(&mut self) -> &mut [IoVec] {
41+
// SAFETY: We know that the `iovec` pointer is valid, initialized and we have
42+
// exclusive access so, its safe to construct a mutable slice from it.
43+
unsafe { core::slice::from_raw_parts_mut(self.iovec, self.iovec_len as usize) }
44+
}
45+
}
46+
1747
// options/posix/include/bits/posix/iovec.h
1848
#[derive(Debug)]
1949
#[repr(C)]
2050
pub struct IoVec {
21-
base: *mut u8,
51+
base: *mut u8, // todo: use Option<NonNull<u8>>
2252
len: usize,
2353
}
54+
55+
impl IoVec {
56+
pub fn as_mut_slice(&mut self) -> &mut [u8] {
57+
// SAFETY: We know that the `base` pointer is valid, initialized and we have
58+
// exclusive access so, its safe to construct a mutable slice from it.
59+
unsafe { core::slice::from_raw_parts_mut(self.base, self.len) }
60+
}
61+
62+
/// Returns the length of the I/O vector.
63+
pub fn len(&self) -> usize {
64+
self.len
65+
}
66+
}

0 commit comments

Comments
 (0)