tcp: apply tuned keepalive and user timeout on client, accepted, and i2p sockets

This commit is contained in:
DeFiDude committed 2026-06-09 23:03:59 -06:00
1 parent 3fd3fa9077
commit 8a41710550
3 files changed
+89 -9

No files matched your search

+4
View File
@@ -49,6 +49,10 @@ objc2-core-bluetooth = "0.2"
[target.'cfg(target_os = "macos")'.dependencies]
libc = "0.2"
# Keepalive-tuning tests read socket options back via getsockopt.
[target.'cfg(unix)'.dev-dependencies]
libc = "0.2"
# Android JNI (BLE Peripheral, USB Serial)
[target.'cfg(target_os = "android")'.dependencies]
jni = "0.19"
+8
View File
@@ -262,6 +262,14 @@ async fn connect_to_sam(sam_host: &str, sam_port: u16) -> Result<TcpStream, SamE
let addr = format!("{}:{}", sam_host, sam_port);
let stream = TcpStream::connect(&addr).await.map_err(SamError::Io)?;
let _ = stream.set_nodelay(true);
// Python i2p_tunneled tuning: longer probes/timeout to absorb I2P latency.
crate::socket_tuning::set_keepalive_tuned(
&stream,
std::time::Duration::from_secs(I2P_PROBE_AFTER),
std::time::Duration::from_secs(I2P_PROBE_INTERVAL),
I2P_PROBES as u32,
std::time::Duration::from_secs(I2P_TCP_USER_TIMEOUT as u64),
);
Ok(stream)
}
+77 -9
View File
@@ -15,15 +15,27 @@ pub const RECONNECT_WAIT_INITIAL: u64 = 5;
pub const RECONNECT_WAIT_MAX: u64 = 300;
pub const DEFAULT_CONNECT_TIMEOUT: u64 = 5;
pub const DEFAULT_TCP_USER_TIMEOUT: u32 = 24;
/// I2P tunnels need a longer `TCP_USER_TIMEOUT` to absorb latency spikes.
pub const I2P_TCP_USER_TIMEOUT: u32 = 45;
pub const TCP_HW_MTU: u32 = 262144;
// Python TCPClientInterface: TCP_PROBE_AFTER / TCP_PROBE_INTERVAL / TCP_PROBES.
// Dead-peer detection: 5s idle + 12 probes × 2s ≈ 29s.
pub const TCP_KEEPIDLE: u32 = 5;
pub const TCP_KEEPINTVL: u32 = 2;
pub const TCP_KEEPCNT: u32 = 12;
/// Tuned keepalive (idle/interval/count + Linux `TCP_USER_TIMEOUT`) for plain
/// TCP client and accepted sockets, matching Python `set_timeouts_linux`.
pub(crate) fn set_tcp_keepalive_tuned(stream: &TcpStream) {
crate::socket_tuning::set_keepalive_tuned(
stream,
std::time::Duration::from_secs(TCP_KEEPIDLE as u64),
std::time::Duration::from_secs(TCP_KEEPINTVL as u64),
TCP_KEEPCNT,
std::time::Duration::from_secs(DEFAULT_TCP_USER_TIMEOUT as u64),
);
}
#[derive(Debug, Clone)]
pub struct TcpClientConfig {
pub name: String,
@@ -177,7 +189,7 @@ pub fn frame_for_tcp(data: &[u8], kiss_framing: bool) -> Vec<u8> {
}
}
use crate::socket_tuning::{set_keepalive, set_socket_buffers};
use crate::socket_tuning::set_socket_buffers;
async fn tcp_read_loop(
mut reader: tokio::net::tcp::OwnedReadHalf,
@@ -352,9 +364,7 @@ pub async fn spawn_tcp_client(
}
};
if let Err(e) = set_keepalive(&stream) {
tracing::debug!(error = %e, "failed to set TCP keepalive");
}
set_tcp_keepalive_tuned(&stream);
// Nagle off for low-latency packets; larger buffers for bulk transfers.
let _ = stream.set_nodelay(true);
@@ -463,9 +473,7 @@ async fn spawn_tcp_accepted(
let state = TcpInterfaceState::with_shared_counters(shared_rxb.clone(), shared_txb.clone());
state.online.store(true, Ordering::SeqCst);
if let Err(e) = set_keepalive(&stream) {
tracing::debug!(error = %e, "failed to set keepalive on accepted socket");
}
set_tcp_keepalive_tuned(&stream);
let _ = stream.set_nodelay(true);
set_socket_buffers(&stream, 131072);
@@ -613,6 +621,66 @@ mod tests {
assert!(!config.prefer_ipv6);
}
/// T1-2 regression: the client/accept paths must apply the TUNED keepalive
/// (idle 5s + 12 probes × 2s ≈ 29s dead-peer parity with Python), not just
/// SO_KEEPALIVE. Reads the options back via getsockopt.
#[cfg(unix)]
#[tokio::test]
async fn tuned_keepalive_applies_python_probe_values() {
use std::os::fd::AsRawFd;
fn get_opt(fd: i32, level: i32, name: i32) -> i32 {
let mut val: libc::c_int = 0;
let mut len = std::mem::size_of::<libc::c_int>() as libc::socklen_t;
let rc = unsafe {
libc::getsockopt(
fd,
level,
name,
&mut val as *mut _ as *mut libc::c_void,
&mut len,
)
};
assert_eq!(rc, 0, "getsockopt({level},{name}) failed");
val
}
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let (accepted, client) = tokio::join!(listener.accept(), TcpStream::connect(addr));
let client = client.unwrap();
let (server, _) = accepted.unwrap();
for stream in [&client, &server] {
set_tcp_keepalive_tuned(stream);
let fd = stream.as_ref().as_raw_fd();
// BSD getsockopt returns the option bit, not 1 — assert enabled.
assert_ne!(get_opt(fd, libc::SOL_SOCKET, libc::SO_KEEPALIVE), 0);
#[cfg(target_os = "linux")]
let idle = get_opt(fd, libc::IPPROTO_TCP, libc::TCP_KEEPIDLE);
#[cfg(target_os = "macos")]
let idle = get_opt(fd, libc::IPPROTO_TCP, libc::TCP_KEEPALIVE);
#[cfg(any(target_os = "linux", target_os = "macos"))]
{
assert_eq!(idle, TCP_KEEPIDLE as i32);
assert_eq!(
get_opt(fd, libc::IPPROTO_TCP, libc::TCP_KEEPINTVL),
TCP_KEEPINTVL as i32
);
assert_eq!(
get_opt(fd, libc::IPPROTO_TCP, libc::TCP_KEEPCNT),
TCP_KEEPCNT as i32
);
}
#[cfg(target_os = "linux")]
assert_eq!(
get_opt(fd, libc::IPPROTO_TCP, libc::TCP_USER_TIMEOUT),
(DEFAULT_TCP_USER_TIMEOUT * 1000) as i32
);
}
}
#[test]
fn test_frame_for_tcp_hdlc() {
let data = b"test packet";