Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 104 additions & 3 deletions os/StarryOS/kernel/src/syscall/net/opt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use ax_errno::{AxError, AxResult, LinuxError};
use axnet::options::{Configurable, GetSocketOption, SetSocketOption};
use linux_raw_sys::net::{IPPROTO_IPV6, IPV6_V6ONLY, socklen_t};
use axnet::options::{
Configurable, GetSocketOption, SetSocketOption, TcpInfo, TcpInfoOptions, TcpState,
};
use linux_raw_sys::net::{
IPPROTO_IPV6, IPV6_V6ONLY, TCP_INFO, TCPI_OPT_ECN, TCPI_OPT_ECN_SEEN, TCPI_OPT_SACK,
TCPI_OPT_SYN_DATA, TCPI_OPT_TIMESTAMPS, TCPI_OPT_WSCALE, socklen_t, tcp_info,
};
use starry_vm::vm_write_slice;

use crate::{
file::{FileLike, Socket, netlink::NetlinkSocket},
Expand All @@ -18,6 +24,93 @@ fn read_int_sockopt(optval: UserConstPtr<u8>, optlen: socklen_t) -> AxResult<i32
Ok(*optval.cast::<i32>().get_as_ref()?)
}

fn tcp_state_to_linux(state: TcpState) -> u8 {
match state {
TcpState::Established => 1,
TcpState::SynSent => 2,
TcpState::SynReceived => 3,
TcpState::FinWait1 => 4,
TcpState::FinWait2 => 5,
TcpState::TimeWait => 6,
TcpState::Closed => 7,
TcpState::CloseWait => 8,
TcpState::LastAck => 9,
TcpState::Listen => 10,
TcpState::Closing => 11,
}
}

fn tcp_options_to_linux(options: TcpInfoOptions) -> u8 {
let mut raw = 0u8;
if options.contains(TcpInfoOptions::TIMESTAMPS) {
raw |= TCPI_OPT_TIMESTAMPS as u8;
}
if options.contains(TcpInfoOptions::SACK) {
raw |= TCPI_OPT_SACK as u8;
}
if options.contains(TcpInfoOptions::WSCALE) {
raw |= TCPI_OPT_WSCALE as u8;
}
if options.contains(TcpInfoOptions::ECN) {
raw |= TCPI_OPT_ECN as u8;
}
if options.contains(TcpInfoOptions::ECN_SEEN) {
raw |= TCPI_OPT_ECN_SEEN as u8;
}
if options.contains(TcpInfoOptions::SYN_DATA) {
raw |= TCPI_OPT_SYN_DATA as u8;
}
raw
}

fn to_linux_tcp_info(info: TcpInfo) -> tcp_info {
// SAFETY: linux-raw-sys tcp_info is a plain C data record. An all-zero
// value is a valid baseline before the supported fields are filled below.
let mut raw = unsafe { core::mem::zeroed::<tcp_info>() };
raw.tcpi_state = tcp_state_to_linux(info.state);
raw.tcpi_ca_state = info.ca_state;
raw.tcpi_retransmits = info.retransmits;
raw.tcpi_probes = info.probes;
raw.tcpi_backoff = info.backoff;
raw.tcpi_options = tcp_options_to_linux(info.options);
raw.set_tcpi_snd_wscale(info.snd_wscale);
raw.set_tcpi_rcv_wscale(info.rcv_wscale);
raw.tcpi_rto = info.rto_micros;
raw.tcpi_ato = info.ato_micros;
raw.tcpi_snd_mss = info.snd_mss;
raw.tcpi_rcv_mss = info.rcv_mss;
raw.tcpi_notsent_bytes = info.notsent_bytes;
raw.tcpi_pmtu = info.pmtu;
raw.tcpi_advmss = info.advmss;
raw.tcpi_snd_cwnd = info.snd_cwnd;
raw.tcpi_reordering = info.reordering;
raw.tcpi_rcv_space = info.rcv_space;
raw.tcpi_snd_wnd = info.snd_wnd;
raw.tcpi_rcv_wnd = info.rcv_wnd;
raw
}

fn write_tcp_info(socket: &Socket, optval: UserPtr<u8>, optlen: &mut socklen_t) -> AxResult<()> {
let mut info = TcpInfo::default();
socket.get_option(GetSocketOption::TcpInfo(&mut info))?;

let write_len = (*optlen as usize).min(size_of::<tcp_info>());
*optlen = write_len as socklen_t;
if write_len == 0 {
return Ok(());
}

let raw = to_linux_tcp_info(info);
// SAFETY: raw lives for the whole copy and is viewed as its C byte layout.
let raw_bytes = unsafe {
core::slice::from_raw_parts(
(&raw as *const tcp_info).cast::<u8>(),
size_of::<tcp_info>(),
)
};
Ok(vm_write_slice(optval.as_ptr(), &raw_bytes[..write_len])?)
}

mod conv {
use ax_errno::{AxError, AxResult};
use axnet::options::UnixCredentials;
Expand Down Expand Up @@ -110,7 +203,6 @@ macro_rules! call_dispatch {
(PROTO_TCP, TCP_KEEPINTVL) => TcpKeepInterval as Int<u32>,
(PROTO_TCP, TCP_KEEPCNT) => TcpKeepCount as Int<u32>,
(PROTO_TCP, TCP_USER_TIMEOUT) => TcpUserTimeout as Int<u32>,
(PROTO_TCP, TCP_INFO) => TcpInfo, // TODO: stub, returns empty struct

(PROTO_IP, IP_TTL) => Ttl as Int<u8>,
(PROTO_IP, IP_RECVERR) => RecvErr as IntBool, // TODO: hardcoded false, no errqueue support
Expand Down Expand Up @@ -198,6 +290,11 @@ pub fn sys_getsockopt(
return Ok(0);
}

if level == PROTO_TCP && optname == TCP_INFO {
Comment thread
ZR233 marked this conversation as resolved.
write_tcp_info(&socket, optval, optlen)?;
return Ok(0);
}

macro_rules! dispatch {
($which:ident) => {
socket.get_option(GetSocketOption::$which(get(optval, optlen)?))?;
Expand Down Expand Up @@ -269,6 +366,10 @@ pub fn sys_setsockopt(
}

let socket = Socket::from_fd(fd)?;
if level == PROTO_TCP && optname == TCP_INFO {
return Err(AxError::from(LinuxError::ENOPROTOOPT));
}

if level == IPPROTO_IPV6 as u32 && optname == IPV6_V6ONLY {
// TODO: Store and enforce IPV6_V6ONLY once native IPv6 sockets exist.
let _ = *get::<i32>(optval, optlen)?;
Expand Down
94 changes: 93 additions & 1 deletion os/arceos/modules/axnet-ng/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,98 @@ use core::time::Duration;
use ax_errno::{AxError, AxResult, LinuxError};
use enum_dispatch::enum_dispatch;

/// Linux-like TCP connection state reported by TCP_INFO.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum TcpState {
/// No active TCP connection.
#[default]
Closed,
/// Passive listener.
Listen,
/// SYN sent, waiting for a matching SYN+ACK.
SynSent,
/// SYN received, waiting for the final ACK.
SynReceived,
/// Fully established connection.
Established,
/// Local endpoint has closed and sent FIN.
FinWait1,
/// Local FIN has been acknowledged.
FinWait2,
/// Remote endpoint has closed first.
CloseWait,
/// Both endpoints have closed simultaneously.
Closing,
/// Waiting for ACK of the local FIN after remote close.
LastAck,
/// Waiting for delayed packets to expire.
TimeWait,
}

bitflags::bitflags! {
/// Negotiated TCP options reported by TCP_INFO.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct TcpInfoOptions: u8 {
/// TCP timestamps are enabled.
const TIMESTAMPS = 1 << 0;
/// Selective ACK is enabled.
const SACK = 1 << 1;
/// Window scaling is enabled.
const WSCALE = 1 << 2;
/// Explicit congestion notification is enabled.
const ECN = 1 << 3;
/// ECN has been seen on the connection.
const ECN_SEEN = 1 << 4;
/// SYN data was used by the connection.
const SYN_DATA = 1 << 5;
}
}

/// Transport-independent TCP_INFO snapshot.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct TcpInfo {
/// Current TCP state.
pub state: TcpState,
/// Congestion-avoidance state. Zero means open.
pub ca_state: u8,
/// Number of unacknowledged retransmits.
pub retransmits: u8,
/// Number of pending keepalive or zero-window probes.
pub probes: u8,
/// Exponential backoff counter.
pub backoff: u8,
/// Negotiated TCP options.
pub options: TcpInfoOptions,
/// Send window scale.
pub snd_wscale: u8,
/// Receive window scale.
pub rcv_wscale: u8,
/// Retransmission timeout in microseconds.
pub rto_micros: u32,
/// Delayed ACK timeout in microseconds.
pub ato_micros: u32,
/// Send maximum segment size.
pub snd_mss: u32,
/// Receive maximum segment size.
pub rcv_mss: u32,
/// Bytes currently queued for transmit.
pub notsent_bytes: u32,
/// Advertised path MTU.
pub pmtu: u32,
/// Advertised maximum segment size.
pub advmss: u32,
/// Current send congestion window, in segments.
pub snd_cwnd: u32,
/// Packet reordering tolerance.
pub reordering: u32,
/// Available receive buffer space.
pub rcv_space: u32,
/// Current send window estimate.
pub snd_wnd: u32,
/// Current receive window estimate.
pub rcv_wnd: u32,
}

macro_rules! define_options {
($($name:ident($value:ty),)*) => {
/// Operation to get a socket option.
Expand Down Expand Up @@ -75,7 +167,7 @@ define_options! {
TcpKeepInterval(u32),
TcpKeepCount(u32),
TcpUserTimeout(u32),
TcpInfo(()),
TcpInfo(TcpInfo),

// ---- IP level options (IP_*) ----
Ttl(u8),
Expand Down
96 changes: 92 additions & 4 deletions os/arceos/modules/axnet-ng/src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
consts::{TCP_RX_BUF_LEN, TCP_TX_BUF_LEN},
general::GeneralOptions,
get_service,
options::{Configurable, GetSocketOption, SetSocketOption},
options::{Configurable, GetSocketOption, SetSocketOption, TcpInfo, TcpInfoOptions, TcpState},
poll_interfaces,
state::*,
};
Expand All @@ -43,6 +43,10 @@ const TCP_USER_TIMEOUT_DEFAULT_MS: u32 = 0;
const TCP_KEEPIDLE_MAX_SECS: u32 = 32767;
const TCP_KEEPINTVL_MAX_SECS: u32 = 32767;
const TCP_KEEPCNT_MAX: u32 = 127;
const TCP_INFO_DEFAULT_MSS: u32 = 1460;
const TCP_INFO_DEFAULT_PMTU: u32 = 1500;
const TCP_INFO_INITIAL_RTO_MICROS: u32 = 1_000_000;
const TCP_INFO_DEFAULT_REORDERING: u32 = 3;

/// A TCP socket that provides POSIX-like APIs.
pub struct TcpSocket {
Expand Down Expand Up @@ -141,6 +145,45 @@ impl TcpSocket {
Duration::from_secs(self.keep_idle_secs.load(Ordering::Relaxed) as u64)
}

fn tcp_info_snapshot(&self) -> TcpInfo {
self.with_smol_socket(|socket| {
let send_capacity = saturating_u32(socket.send_capacity());
let send_queue = saturating_u32(socket.send_queue());
let recv_capacity = saturating_u32(socket.recv_capacity());
let recv_queue = saturating_u32(socket.recv_queue());
let send_space = send_capacity.saturating_sub(send_queue);
let recv_space = recv_capacity.saturating_sub(recv_queue);
let snd_mss = TCP_INFO_DEFAULT_MSS;
let cwnd_segments = send_capacity.div_ceil(snd_mss).max(1);

let mut options = TcpInfoOptions::empty();
if socket.timestamp_enabled() {
options |= TcpInfoOptions::TIMESTAMPS;
}

TcpInfo {
state: tcp_state_info(socket.state()),
options,
rto_micros: socket
.timeout()
.map(duration_micros_u32)
.unwrap_or(TCP_INFO_INITIAL_RTO_MICROS),
ato_micros: socket.ack_delay().map(duration_micros_u32).unwrap_or(0),
snd_mss,
rcv_mss: snd_mss,
notsent_bytes: send_queue,
pmtu: TCP_INFO_DEFAULT_PMTU,
advmss: snd_mss,
snd_cwnd: cwnd_segments,
reordering: TCP_INFO_DEFAULT_REORDERING,
rcv_space: recv_space,
snd_wnd: send_space,
Comment thread
ZR233 marked this conversation as resolved.
Outdated
rcv_wnd: recv_space,
Comment thread
cqwhfhh marked this conversation as resolved.
Outdated
Comment thread
cqwhfhh marked this conversation as resolved.
Outdated
..Default::default()
}
})
}

fn bound_endpoint(&self) -> AxResult<IpListenEndpoint> {
let endpoint = *self.bound_endpoint.lock();
if endpoint.port == 0 {
Expand Down Expand Up @@ -266,8 +309,8 @@ impl Configurable for TcpSocket {
O::ReceiveBuffer(size) => {
**size = TCP_RX_BUF_LEN;
}
O::TcpInfo(_) => {
// TODO(mivik): implement TCP_INFO
O::TcpInfo(info) => {
**info = self.tcp_info_snapshot();
}
_ => return Ok(false),
}
Expand Down Expand Up @@ -629,6 +672,30 @@ impl Drop for TcpSocket {
}
}

fn saturating_u32(value: usize) -> u32 {
value.min(u32::MAX as usize) as u32
}

fn duration_micros_u32(value: Duration) -> u32 {
value.total_micros().min(u32::MAX as u64) as u32
}

fn tcp_state_info(state: smol::State) -> TcpState {
match state {
smol::State::Closed => TcpState::Closed,
smol::State::Listen => TcpState::Listen,
smol::State::SynSent => TcpState::SynSent,
smol::State::SynReceived => TcpState::SynReceived,
smol::State::Established => TcpState::Established,
smol::State::FinWait1 => TcpState::FinWait1,
smol::State::FinWait2 => TcpState::FinWait2,
smol::State::CloseWait => TcpState::CloseWait,
smol::State::Closing => TcpState::Closing,
smol::State::LastAck => TcpState::LastAck,
smol::State::TimeWait => TcpState::TimeWait,
}
}

const fn empty_endpoint() -> IpListenEndpoint {
IpListenEndpoint {
addr: None,
Expand Down Expand Up @@ -800,10 +867,31 @@ mod tests {

use super::*;
use crate::{
options::{Configurable, SetSocketOption},
options::{Configurable, GetSocketOption, SetSocketOption, TcpState},
test_support::{LOCAL_ADDR, LOCAL_MASK, PEER_ADDR, PEER_MASK, init_split_route_network},
};

#[test]
fn tcp_info_reports_default_socket_metrics() {
init_split_route_network();

let socket = TcpSocket::new();
let mut info = TcpInfo::default();

socket
.get_option(GetSocketOption::TcpInfo(&mut info))
.unwrap();

assert_eq!(info.state, TcpState::Closed);
assert_eq!(info.snd_mss, TCP_INFO_DEFAULT_MSS);
assert_eq!(info.rcv_mss, TCP_INFO_DEFAULT_MSS);
assert_eq!(info.pmtu, TCP_INFO_DEFAULT_PMTU);
assert_eq!(info.notsent_bytes, 0);
assert_eq!(info.snd_wnd, saturating_u32(TCP_TX_BUF_LEN));
assert_eq!(info.rcv_space, saturating_u32(TCP_RX_BUF_LEN));
assert_eq!(info.rcv_wnd, saturating_u32(TCP_RX_BUF_LEN));
Comment thread
cqwhfhh marked this conversation as resolved.
Outdated
}

#[test]
fn connect_uses_peer_route_for_device_mask() {
init_split_route_network();
Expand Down
Loading