Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion apps/starry/qemu/apk-curl/qemu-aarch64.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ shell_prefix = "root@starry:"
shell_init_cmd = "/usr/bin/apk-curl-tests.sh"
success_regex = ["(?m)^APK_CURL_TEST_PASSED\\s*$"]
fail_regex = ['(?i)\bpanic(?:ked)?\b', "(?m)^lockdep fatal violation\\s*$", "(?m)^APK_CURL_TEST_FAILED\\s*$"]
timeout = 420
timeout = 720
2 changes: 1 addition & 1 deletion apps/starry/qemu/apk-curl/qemu-loongarch64.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ shell_prefix = "root@starry:"
shell_init_cmd = "/usr/bin/apk-curl-tests.sh"
success_regex = ["(?m)^APK_CURL_TEST_PASSED\\s*$"]
fail_regex = ['(?i)\bpanic(?:ked)?\b', "(?m)^lockdep fatal violation\\s*$", "(?m)^APK_CURL_TEST_FAILED\\s*$"]
timeout = 420
timeout = 720
2 changes: 1 addition & 1 deletion apps/starry/qemu/apk-curl/qemu-riscv64.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ shell_prefix = "root@starry:"
shell_init_cmd = "/usr/bin/apk-curl-tests.sh"
success_regex = ["(?m)^APK_CURL_TEST_PASSED\\s*$"]
fail_regex = ['(?i)\bpanic(?:ked)?\b', "(?m)^lockdep fatal violation\\s*$", "(?m)^APK_CURL_TEST_FAILED\\s*$"]
timeout = 420
timeout = 720
2 changes: 1 addition & 1 deletion apps/starry/qemu/apk-curl/qemu-x86_64.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ shell_prefix = "root@starry:"
shell_init_cmd = "/usr/bin/apk-curl-tests.sh"
success_regex = ["(?m)^APK_CURL_TEST_PASSED\\s*$"]
fail_regex = ['(?i)\bpanic(?:ked)?\b', "(?m)^lockdep fatal violation\\s*$", "(?m)^APK_CURL_TEST_FAILED\\s*$"]
timeout = 420
timeout = 720
15 changes: 9 additions & 6 deletions apps/starry/qemu/apk-curl/sh/apk-curl-tests.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#!/bin/sh

fetch_timeout=60
default_fetch_timeout=180
repo_file=/etc/apk/repositories
original_repos="$(cat "$repo_file")"

try_apk_curl() {
mirror="$1"
label="$2"
fetch_timeout="$3"
probe_url="$mirror/MIRRORS.txt"
printf '%s\n' "$original_repos" |
sed "s#http://[^/]*/alpine/#$mirror/#g;s#https://[^/]*/alpine/#$mirror/#g" > "$repo_file"
Expand All @@ -26,14 +27,16 @@ try_apk_curl() {

i=0
for repo in \
"https://mirrors.cernet.edu.cn/alpine cernet" \
"https://dl-cdn.alpinelinux.org/alpine upstream"
"https://mirrors.cernet.edu.cn/alpine cernet 60" \
"https://dl-cdn.alpinelinux.org/alpine upstream $default_fetch_timeout"
do
i=$((i + 1))
mirror=${repo% *}
label=${repo#* }
set -- $repo
mirror=$1
label=$2
fetch_timeout=$3
echo "APK_CURL_ATTEMPT_$i"
if try_apk_curl "$mirror" "$label"; then
if try_apk_curl "$mirror" "$label" "$fetch_timeout"; then
echo 'APK_CURL_TEST_PASSED'
exit 0
fi
Expand Down
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
Loading