Skip to content

Commit c2cfd1d

Browse files
feat: AF_VSOCK with SOCK_STREAM (#12)
* starry: support AF_VSOCK Stream mode Signed-off-by: Weikang Guo <guoweikang@kylinos.cn> * vsock: default disable qemu vsock device Signed-off-by: Weikang Guo <guoweikang@kylinos.cn> * Unified terminology using VSOCK Signed-off-by: Weikang Guo <guoweikang@kylinos.cn> * vsock: update arceos support vsock Signed-off-by: Weikang Guo <guoweikang@kylinos.cn> * vsock: add linux sockaddr_vm define Signed-off-by: Weikang Guo <guoweikang@kylinos.cn> * chore: cleanup --------- Signed-off-by: Weikang Guo <guoweikang@kylinos.cn> Co-authored-by: Weikang Guo <guoweikang@kylinos.cn> Co-authored-by: Asakura Mizu <asakuramizu111@gmail.com>
1 parent 7a2d82d commit c2cfd1d

File tree

7 files changed

+83
-14
lines changed

7 files changed

+83
-14
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@
1111
/.cargo
1212
/.crates*
1313
/mnt
14+
*.swp
15+
*.swo
16+

Cargo.lock

Lines changed: 18 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ axfeat = { path = "arceos/api/axfeat", features = [
2626
"rtc",
2727
# "sched-fifo",
2828
"sched-rr",
29+
"vsock",
2930
] }
3031

3132
axalloc = { path = "arceos/modules/axalloc" }
@@ -87,6 +88,7 @@ default = []
8788
qemu = [
8889
"axfeat/driver-virtio-blk",
8990
"axfeat/driver-virtio-net",
91+
"axfeat/driver-virtio-socket",
9092

9193
"axfeat/driver-virtio-gpu",
9294
"axfeat/display",
@@ -102,6 +104,7 @@ qemu = [
102104
"axfeat/fs-times",
103105
"starry-api/dev-log",
104106
]
107+
105108
vf2 = ["dep:axplat-riscv64-visionfive2", "axfeat/driver-sdmmc-gpt"]
106109
2k1000la = ["dep:axplat-loongarch64-2k1000la", "axfeat/driver-ahci-gpt"]
107110

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export MEMTRACK := n
77
# QEMU Options
88
export BLK := y
99
export NET := y
10+
export VSOCK := n
1011
export MEM := 1G
1112
export ICOUNT := n
1213

api/src/socket.rs

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ use core::{
88
};
99

1010
use axerrno::{AxError, AxResult, LinuxError};
11-
use axnet::{SocketAddrEx, unix::UnixSocketAddr};
11+
use axnet::{SocketAddrEx, unix::UnixSocketAddr, vsock::VsockAddr};
1212
use linux_raw_sys::net::{
13-
__kernel_sa_family_t, AF_INET, AF_INET6, AF_UNIX, in_addr, in6_addr, sockaddr, sockaddr_in,
14-
sockaddr_in6, socklen_t,
13+
__kernel_sa_family_t, AF_INET, AF_INET6, AF_UNIX, AF_VSOCK, in_addr, in6_addr, sockaddr,
14+
sockaddr_in, sockaddr_in6, socklen_t,
1515
};
1616

1717
use crate::mm::{UserConstPtr, UserPtr};
@@ -195,11 +195,57 @@ impl SocketAddrExt for UnixSocketAddr {
195195
}
196196
}
197197

198+
// This type should be provided by linux_raw_sys but it's missing.
199+
// See https://github.com/sunfishcode/linux-raw-sys/issues/169
200+
#[allow(non_camel_case_types)]
201+
#[repr(C)]
202+
#[derive(Copy, Clone)]
203+
pub struct sockaddr_vm {
204+
pub svm_family: __kernel_sa_family_t,
205+
pub svm_reserved1: u16,
206+
pub svm_port: u32,
207+
pub svm_cid: u32,
208+
pub svm_zero: [u8; 4],
209+
}
210+
211+
impl SocketAddrExt for VsockAddr {
212+
fn read_from_user(addr: UserConstPtr<sockaddr>, addrlen: socklen_t) -> AxResult<Self> {
213+
if addrlen != size_of::<sockaddr_vm>() as socklen_t {
214+
return Err(AxError::InvalidInput);
215+
}
216+
217+
let addr_vsock = addr.cast::<sockaddr_vm>().get_as_ref()?;
218+
if addr_vsock.svm_family as u32 != AF_VSOCK {
219+
return Err(AxError::Other(LinuxError::EAFNOSUPPORT));
220+
}
221+
Ok(VsockAddr {
222+
cid: addr_vsock.svm_cid,
223+
port: addr_vsock.svm_port,
224+
})
225+
}
226+
227+
fn write_to_user(&self, addr: UserPtr<sockaddr>, addrlen: &mut socklen_t) -> AxResult<()> {
228+
let sockvm_addr = sockaddr_vm {
229+
svm_family: AF_VSOCK as _,
230+
svm_reserved1: 0,
231+
svm_port: self.port,
232+
svm_cid: self.cid,
233+
svm_zero: [0_u8; 4],
234+
};
235+
fill_addr(addr, addrlen, unsafe { cast_to_slice(&sockvm_addr) })
236+
}
237+
238+
fn family(&self) -> u16 {
239+
AF_VSOCK as u16
240+
}
241+
}
242+
198243
impl SocketAddrExt for SocketAddrEx {
199244
fn read_from_user(addr: UserConstPtr<sockaddr>, addrlen: socklen_t) -> AxResult<Self> {
200245
match read_family(addr, addrlen)? as u32 {
201246
AF_INET | AF_INET6 => SocketAddr::read_from_user(addr, addrlen).map(Self::Ip),
202247
AF_UNIX => UnixSocketAddr::read_from_user(addr, addrlen).map(Self::Unix),
248+
AF_VSOCK => VsockAddr::read_from_user(addr, addrlen).map(Self::Vsock),
203249
_ => Err(AxError::Other(LinuxError::EAFNOSUPPORT)),
204250
}
205251
}
@@ -208,6 +254,7 @@ impl SocketAddrExt for SocketAddrEx {
208254
match self {
209255
SocketAddrEx::Ip(ip_addr) => ip_addr.write_to_user(addr, addrlen),
210256
SocketAddrEx::Unix(unix_addr) => unix_addr.write_to_user(addr, addrlen),
257+
SocketAddrEx::Vsock(vsock_addr) => vsock_addr.write_to_user(addr, addrlen),
211258
}
212259
}
213260

api/src/syscall/net/socket.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ use axnet::{
44
tcp::TcpSocket,
55
udp::UdpSocket,
66
unix::{DgramTransport, StreamTransport, UnixSocket},
7+
vsock::{VsockSocket, VsockStreamTransport},
78
};
89
use axtask::current;
910
use linux_raw_sys::{
1011
general::{O_CLOEXEC, O_NONBLOCK},
1112
net::{
12-
AF_INET, AF_UNIX, IPPROTO_TCP, IPPROTO_UDP, SHUT_RD, SHUT_RDWR, SHUT_WR, SOCK_DGRAM,
13-
SOCK_SEQPACKET, SOCK_STREAM, sockaddr, socklen_t,
13+
AF_INET, AF_UNIX, AF_VSOCK, IPPROTO_TCP, IPPROTO_UDP, SHUT_RD, SHUT_RDWR, SHUT_WR,
14+
SOCK_DGRAM, SOCK_SEQPACKET, SOCK_STREAM, sockaddr, socklen_t,
1415
},
1516
};
1617
use starry_core::task::AsThread;
@@ -44,7 +45,10 @@ pub fn sys_socket(domain: u32, raw_ty: u32, proto: u32) -> AxResult<isize> {
4445
}
4546
(AF_UNIX, SOCK_STREAM) => axnet::Socket::Unix(UnixSocket::new(StreamTransport::new(pid))),
4647
(AF_UNIX, SOCK_DGRAM) => axnet::Socket::Unix(UnixSocket::new(DgramTransport::new(pid))),
47-
(AF_INET, _) | (AF_UNIX, _) => {
48+
(AF_VSOCK, SOCK_STREAM) => {
49+
axnet::Socket::Vsock(VsockSocket::new(VsockStreamTransport::new()))
50+
}
51+
(AF_INET, _) | (AF_UNIX, _) | (AF_VSOCK, _) => {
4852
warn!("Unsupported socket type: domain: {}, ty: {}", domain, ty);
4953
return Err(AxError::Other(LinuxError::ESOCKTNOSUPPORT));
5054
}

0 commit comments

Comments
 (0)