Skip to content

Commit c9cc7a9

Browse files
committed
introduce container struct for TCP non-inherited opts
Signed-off-by: Roman Volosatovs <rvolosatovs@riseup.net>
1 parent 412c9b8 commit c9cc7a9

2 files changed

Lines changed: 32 additions & 48 deletions

File tree

crates/wasi/src/p3/sockets/host/types/tcp.rs

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,8 @@ struct ListenTask {
5858
listener: Arc<TcpListener>,
5959
family: SocketAddressFamily,
6060
tx: StreamWriter<Option<Resource<TcpSocket>>>,
61-
62-
// The socket options below are not automatically inherited from the listener
63-
// on all platforms. So we keep track of which options have been explicitly
64-
// set and manually apply those values to newly accepted clients.
65-
#[cfg(target_os = "macos")]
66-
receive_buffer_size: Arc<core::sync::atomic::AtomicUsize>,
67-
#[cfg(target_os = "macos")]
68-
send_buffer_size: Arc<core::sync::atomic::AtomicUsize>,
6961
#[cfg(target_os = "macos")]
70-
hop_limit: Arc<core::sync::atomic::AtomicU8>,
71-
#[cfg(target_os = "macos")]
72-
keep_alive_idle_time: Arc<core::sync::atomic::AtomicU64>, // nanoseconds
62+
options: Arc<crate::p3::sockets::tcp::NonInheritedOptions>,
7363
}
7464

7565
impl<T> AccessorTask<T, WasiSockets, wasmtime::Result<()>> for ListenTask {
@@ -95,6 +85,7 @@ impl<T> AccessorTask<T, WasiSockets, wasmtime::Result<()>> for ListenTask {
9585
// and only if a specific value was explicitly set on the listener.
9686

9787
let receive_buffer_size = self
88+
.options
9889
.receive_buffer_size
9990
.load(core::sync::atomic::Ordering::Relaxed);
10091
if receive_buffer_size > 0 {
@@ -106,6 +97,7 @@ impl<T> AccessorTask<T, WasiSockets, wasmtime::Result<()>> for ListenTask {
10697
}
10798

10899
let send_buffer_size = self
100+
.options
109101
.send_buffer_size
110102
.load(core::sync::atomic::Ordering::Relaxed);
111103
if send_buffer_size > 0 {
@@ -118,8 +110,10 @@ impl<T> AccessorTask<T, WasiSockets, wasmtime::Result<()>> for ListenTask {
118110

119111
// For some reason, IP_TTL is inherited, but IPV6_UNICAST_HOPS isn't.
120112
if self.family == SocketAddressFamily::Ipv6 {
121-
let hop_limit =
122-
self.hop_limit.load(core::sync::atomic::Ordering::Relaxed);
113+
let hop_limit = self
114+
.options
115+
.hop_limit
116+
.load(core::sync::atomic::Ordering::Relaxed);
123117
if hop_limit > 0 {
124118
// Ignore potential error.
125119
_ = rustix::net::sockopt::set_ipv6_unicast_hops(
@@ -130,6 +124,7 @@ impl<T> AccessorTask<T, WasiSockets, wasmtime::Result<()>> for ListenTask {
130124
}
131125

132126
let keep_alive_idle_time = self
127+
.options
133128
.keep_alive_idle_time
134129
.load(core::sync::atomic::Ordering::Relaxed);
135130
if keep_alive_idle_time > 0 {
@@ -331,13 +326,7 @@ impl HostTcpSocketConcurrent for WasiSockets {
331326
listen_backlog_size,
332327
family,
333328
#[cfg(target_os = "macos")]
334-
receive_buffer_size,
335-
#[cfg(target_os = "macos")]
336-
send_buffer_size,
337-
#[cfg(target_os = "macos")]
338-
hop_limit,
339-
#[cfg(target_os = "macos")]
340-
keep_alive_idle_time,
329+
options,
341330
} = get_socket_mut(view.get().table, &socket)?;
342331
let sock = match mem::replace(tcp_state, TcpState::Closed) {
343332
TcpState::Default(sock) | TcpState::Bound(sock) => sock,
@@ -373,13 +362,7 @@ impl HostTcpSocketConcurrent for WasiSockets {
373362
family: *family,
374363
tx,
375364
#[cfg(target_os = "macos")]
376-
receive_buffer_size: Arc::clone(&receive_buffer_size),
377-
#[cfg(target_os = "macos")]
378-
send_buffer_size: Arc::clone(&send_buffer_size),
379-
#[cfg(target_os = "macos")]
380-
hop_limit: Arc::clone(&hop_limit),
381-
#[cfg(target_os = "macos")]
382-
keep_alive_idle_time: Arc::clone(&keep_alive_idle_time),
365+
options: Arc::clone(&options),
383366
};
384367
view.spawn(task);
385368
Ok(Ok(rx.into()))

crates/wasi/src/p3/sockets/tcp.rs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ impl Debug for TcpState {
6161
}
6262
}
6363

64+
// The socket options below are not automatically inherited from the listener
65+
// on all platforms. So we keep track of which options have been explicitly
66+
// set and manually apply those values to newly accepted clients.
67+
#[cfg(target_os = "macos")]
68+
#[derive(Default)]
69+
pub struct NonInheritedOptions {
70+
pub receive_buffer_size: core::sync::atomic::AtomicUsize,
71+
pub send_buffer_size: core::sync::atomic::AtomicUsize,
72+
pub hop_limit: core::sync::atomic::AtomicU8,
73+
pub keep_alive_idle_time: core::sync::atomic::AtomicU64, // nanoseconds
74+
}
75+
6476
/// A host TCP socket, plus associated bookkeeping.
6577
pub struct TcpSocket {
6678
/// The current state in the bind/listen/accept/connect progression.
@@ -71,17 +83,8 @@ pub struct TcpSocket {
7183

7284
pub family: SocketAddressFamily,
7385

74-
// The socket options below are not automatically inherited from the listener
75-
// on all platforms. So we keep track of which options have been explicitly
76-
// set and manually apply those values to newly accepted clients.
77-
#[cfg(target_os = "macos")]
78-
pub receive_buffer_size: Arc<core::sync::atomic::AtomicUsize>,
79-
#[cfg(target_os = "macos")]
80-
pub send_buffer_size: Arc<core::sync::atomic::AtomicUsize>,
8186
#[cfg(target_os = "macos")]
82-
pub hop_limit: Arc<core::sync::atomic::AtomicU8>,
83-
#[cfg(target_os = "macos")]
84-
pub keep_alive_idle_time: Arc<core::sync::atomic::AtomicU64>, // nanoseconds
87+
pub options: Arc<NonInheritedOptions>,
8588
}
8689

8790
impl TcpSocket {
@@ -111,13 +114,7 @@ impl TcpSocket {
111114
listen_backlog_size: DEFAULT_TCP_BACKLOG,
112115
family,
113116
#[cfg(target_os = "macos")]
114-
receive_buffer_size: Arc::default(),
115-
#[cfg(target_os = "macos")]
116-
send_buffer_size: Arc::default(),
117-
#[cfg(target_os = "macos")]
118-
hop_limit: Arc::default(),
119-
#[cfg(target_os = "macos")]
120-
keep_alive_idle_time: Arc::default(),
117+
options: Arc::default(),
121118
}
122119
}
123120

@@ -252,7 +249,8 @@ impl TcpSocket {
252249
let value = set_keep_alive_idle_time(fd, value)?;
253250
#[cfg(target_os = "macos")]
254251
{
255-
self.keep_alive_idle_time
252+
self.options
253+
.keep_alive_idle_time
256254
.store(value, core::sync::atomic::Ordering::Relaxed);
257255
}
258256
Ok(())
@@ -293,7 +291,8 @@ impl TcpSocket {
293291
set_unicast_hop_limit(fd, self.family, value)?;
294292
#[cfg(target_os = "macos")]
295293
{
296-
self.hop_limit
294+
self.options
295+
.hop_limit
297296
.store(value, core::sync::atomic::Ordering::Relaxed);
298297
}
299298
Ok(())
@@ -311,7 +310,8 @@ impl TcpSocket {
311310
#[cfg(target_os = "macos")]
312311
{
313312
let value = res?;
314-
self.receive_buffer_size
313+
self.options
314+
.receive_buffer_size
315315
.store(value, core::sync::atomic::Ordering::Relaxed);
316316
}
317317
#[cfg(not(target_os = "macos"))]
@@ -333,7 +333,8 @@ impl TcpSocket {
333333
#[cfg(target_os = "macos")]
334334
{
335335
let value = res?;
336-
self.send_buffer_size
336+
self.options
337+
.send_buffer_size
337338
.store(value, core::sync::atomic::Ordering::Relaxed);
338339
}
339340
#[cfg(not(target_os = "macos"))]

0 commit comments

Comments
 (0)