kobako 0.31.0 → 0.32.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.githooks/linux-tests.sh +29 -0
- data/.release-please-manifest.json +1 -1
- data/CHANGELOG.md +125 -0
- data/Cargo.lock +457 -494
- data/README.md +2 -3
- data/ROADMAP.md +0 -1
- data/crates/kobako-runtime/CHANGELOG.md +90 -0
- data/crates/kobako-runtime/Cargo.toml +14 -15
- data/crates/kobako-runtime/README.md +1 -1
- data/crates/kobako-runtime/src/cred.rs +32 -20
- data/crates/kobako-runtime/src/filesystem/bind/fid.rs +300 -22
- data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +56 -22
- data/crates/kobako-runtime/src/filesystem/bind/unicode.rs +109 -0
- data/crates/kobako-runtime/src/filesystem/bind/walk.rs +33 -14
- data/crates/kobako-runtime/src/filesystem/bind.rs +393 -220
- data/crates/kobako-runtime/src/filesystem/overlay/stack/copy_up.rs +2 -0
- data/crates/kobako-runtime/src/filesystem/overlay/stack.rs +9 -10
- data/crates/kobako-runtime/src/filesystem/overlay.rs +277 -46
- data/crates/kobako-runtime/src/filesystem/rdir.rs +8 -27
- data/crates/kobako-runtime/src/filesystem/testing.rs +31 -0
- data/crates/kobako-runtime/src/filesystem/tmpfs.rs +47 -13
- data/crates/kobako-runtime/src/filesystem.rs +2 -0
- data/crates/kobako-runtime/src/fork.rs +239 -16
- data/crates/kobako-runtime/src/limits.rs +17 -41
- data/crates/kobako-runtime/src/locking/seqlock.rs +29 -3
- data/crates/kobako-runtime/src/locking.rs +508 -113
- data/crates/kobako-runtime/src/memcg.rs +83 -160
- data/crates/kobako-runtime/src/mount.rs +0 -30
- data/crates/kobako-runtime/src/net/connections.rs +53 -13
- data/crates/kobako-runtime/src/net/egress.rs +44 -24
- data/crates/kobako-runtime/src/net/http.rs +888 -456
- data/crates/kobako-runtime/src/net/lookup.rs +150 -50
- data/crates/kobako-runtime/src/net/namespace.rs +97 -95
- data/crates/kobako-runtime/src/net/reactor.rs +298 -203
- data/crates/kobako-runtime/src/net/resolv/darwin.rs +8 -3
- data/crates/kobako-runtime/src/net/resolv/stub/tests.rs +50 -0
- data/crates/kobako-runtime/src/net/resolv/stub.rs +29 -23
- data/crates/kobako-runtime/src/net/resolv/windows.rs +14 -44
- data/crates/kobako-runtime/src/net/resolv.rs +39 -15
- data/crates/kobako-runtime/src/net/skbuff.rs +109 -0
- data/crates/kobako-runtime/src/net/socket.rs +51 -52
- data/crates/kobako-runtime/src/net/task.rs +223 -0
- data/crates/kobako-runtime/src/net/tcp.rs +410 -158
- data/crates/kobako-runtime/src/net/tls.rs +65 -3
- data/crates/kobako-runtime/src/net/transport.rs +72 -47
- data/crates/kobako-runtime/src/net/udp.rs +231 -129
- data/crates/kobako-runtime/src/net.rs +3 -0
- data/crates/kobako-runtime/src/sched.rs +244 -58
- data/crates/kobako-runtime/src/security.rs +27 -16
- data/crates/kobako-runtime/src/vfs/dentry.rs +80 -41
- data/crates/kobako-runtime/src/vfs/file.rs +52 -53
- data/crates/kobako-runtime/src/vfs/kern_path.rs +24 -11
- data/crates/kobako-runtime/src/vfs/namei.rs +399 -184
- data/crates/kobako-runtime/src/vfs/namespace.rs +123 -40
- data/crates/kobako-runtime/src/vfs/stat.rs +105 -16
- data/crates/kobako-runtime/src/vfs.rs +1 -0
- data/crates/kobako-runtime/src/wait.rs +75 -50
- data/crates/kobako-transport/CHANGELOG.md +7 -0
- data/crates/kobako-transport/Cargo.toml +1 -1
- data/crates/kobako-transport/README.md +1 -1
- data/crates/kobako-wasmtime/CHANGELOG.md +47 -0
- data/crates/kobako-wasmtime/Cargo.toml +27 -6
- data/crates/kobako-wasmtime/README.md +3 -3
- data/crates/kobako-wasmtime/src/abi.rs +2 -1
- data/crates/kobako-wasmtime/src/ambient.rs +11 -2
- data/crates/kobako-wasmtime/src/cache/unix.rs +95 -0
- data/crates/kobako-wasmtime/src/cache/windows.rs +359 -0
- data/crates/kobako-wasmtime/src/cache.rs +88 -103
- data/crates/kobako-wasmtime/src/driver.rs +40 -27
- data/crates/kobako-wasmtime/src/invocation.rs +21 -7
- data/crates/kobako-wasmtime/src/lib.rs +2 -0
- data/crates/kobako-wasmtime/src/limiter.rs +78 -25
- data/crates/kobako-wasmtime/src/preview1/descriptors.rs +13 -17
- data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +4 -5
- data/crates/kobako-wasmtime/src/preview1/differential.rs +1 -1
- data/crates/kobako-wasmtime/src/preview1/fd.rs +23 -19
- data/crates/kobako-wasmtime/src/preview1/mount_view.rs +130 -197
- data/crates/kobako-wasmtime/src/preview1/path.rs +3 -3
- data/crates/kobako-wasmtime/src/preview1/poll.rs +28 -22
- data/crates/kobako-wasmtime/src/preview1/random.rs +125 -0
- data/crates/kobako-wasmtime/src/preview1/testing.rs +31 -0
- data/crates/kobako-wasmtime/src/preview1.rs +85 -9
- data/crates/kobako-wasmtime/src/preview2/filesystem.rs +2 -2
- data/crates/kobako-wasmtime/src/preview2/host.rs +609 -249
- data/crates/kobako-wasmtime/src/preview2/http.rs +299 -215
- data/crates/kobako-wasmtime/src/preview2/io.rs +17 -4
- data/crates/kobako-wasmtime/src/preview2/table.rs +24 -72
- data/crates/kobako-wasmtime/src/trap.rs +68 -23
- data/crates/kobako-wasmtime/src/waits.rs +59 -0
- data/crates/kobako-wasmtime/src/wasi.rs +91 -10
- data/crates/kobako-wasmtime/witx/wasi_snapshot_preview1.witx +13 -0
- data/data/kobako.wasm +0 -0
- data/ext/kobako/Cargo.toml +6 -2
- data/ext/kobako/src/escape.rs +332 -26
- data/ext/kobako/src/filesystem/grants.rs +5 -7
- data/ext/kobako/src/filesystem/ruby.rs +10 -3
- data/ext/kobako/src/filesystem.rs +47 -8
- data/ext/kobako/src/fork.rs +72 -0
- data/ext/kobako/src/gvl.rs +499 -76
- data/ext/kobako/src/handoff.rs +490 -0
- data/ext/kobako/src/lib.rs +10 -0
- data/ext/kobako/src/network.rs +5 -3
- data/ext/kobako/src/pinned_value.rs +28 -15
- data/ext/kobako/src/runtime/bridge.rs +32 -25
- data/ext/kobako/src/runtime.rs +74 -43
- data/lib/kobako/catalog/handles.rb +9 -5
- data/lib/kobako/codec/decoder.rb +1 -1
- data/lib/kobako/codec/state.rb +2 -18
- data/lib/kobako/codec/value_budget.rb +4 -4
- data/lib/kobako/codec.rb +5 -6
- data/lib/kobako/errors.rb +4 -7
- data/lib/kobako/filesystem.rb +1 -1
- data/lib/kobako/fork.rb +42 -0
- data/lib/kobako/handle.rb +1 -2
- data/lib/kobako/sandbox.rb +17 -36
- data/lib/kobako/sandbox_options.rb +7 -9
- data/lib/kobako/usage.rb +4 -2
- data/lib/kobako/version.rb +1 -1
- data/lib/kobako.rb +2 -2
- data/sig/kobako/catalog/handles.rbs +2 -0
- data/sig/kobako/codec/state.rbs +0 -4
- data/sig/kobako/codec/value_budget.rbs +1 -1
- data/sig/kobako/codec.rbs +2 -2
- data/sig/kobako/errors.rbs +0 -3
- data/sig/kobako/fork.rbs +17 -0
- data/sig/kobako/runtime.rbs +2 -2
- data/sig/kobako/sandbox.rbs +3 -12
- data/sig/kobako/sandbox_options.rbs +0 -2
- metadata +15 -4
- data/lib/kobako/pool.rb +0 -162
- data/sig/core/thread.rbs +0 -6
- data/sig/kobako/pool.rbs +0 -46
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
//! guest call moves what it can at once, so a buffer the guest drained reads
|
|
6
6
|
//! on without waiting for another event.
|
|
7
7
|
|
|
8
|
-
use std::collections::VecDeque;
|
|
9
8
|
use std::io::{self, Read, Write};
|
|
10
9
|
use std::net::{Shutdown, SocketAddr};
|
|
11
10
|
use std::sync::{Arc, Mutex, MutexGuard, PoisonError, Weak};
|
|
@@ -13,16 +12,15 @@ use std::time::Duration;
|
|
|
13
12
|
|
|
14
13
|
use socket2::{SockRef, TcpKeepalive};
|
|
15
14
|
|
|
16
|
-
use super::connections::{OpenConnections, Place};
|
|
15
|
+
use super::connections::{ConnectionBudget, OpenConnections, Place};
|
|
17
16
|
use super::family::ProtocolFamily;
|
|
18
17
|
use super::namespace::Namespace;
|
|
19
18
|
use super::reactor::{Reactor, Ready, Registration};
|
|
19
|
+
use super::skbuff::SkBuffHead;
|
|
20
20
|
use super::socket::{
|
|
21
21
|
connect4, ensure_within, filter, filter_with, set_hop_limit, take_connect_outcome, BindHash,
|
|
22
|
-
Buffers, Destination, InetSock, ProtoOps, SkState, SockOpt,
|
|
22
|
+
Buffers, Destination, InetSock, ProtoOps, SkState, SockOpt,
|
|
23
23
|
};
|
|
24
|
-
use crate::cred::Cred;
|
|
25
|
-
use crate::memcg::MemoryCharge;
|
|
26
24
|
use crate::network::{Protocol, Ruleset};
|
|
27
25
|
use crate::vfs::PollMask;
|
|
28
26
|
use crate::wait::{poll_wait, PollTable, WaitQueueHead};
|
|
@@ -97,11 +95,11 @@ struct State {
|
|
|
97
95
|
/// The host socket, and its place in the reactor.
|
|
98
96
|
host: Option<(mio::net::TcpStream, Registration)>,
|
|
99
97
|
/// What arrived and the guest has not read, as `sk_receive_queue`.
|
|
100
|
-
receive_queue:
|
|
98
|
+
receive_queue: SkBuffHead,
|
|
101
99
|
/// The peer ended its side, as a read of zero answers.
|
|
102
100
|
received_end: bool,
|
|
103
101
|
/// What the guest wrote and the host has not taken, as `sk_write_queue`.
|
|
104
|
-
write_queue:
|
|
102
|
+
write_queue: SkBuffHead,
|
|
105
103
|
/// The FIN left, once the writing side was shut and the buffer drained.
|
|
106
104
|
fin_sent: bool,
|
|
107
105
|
/// What every inet socket holds. What arrived still reads once the
|
|
@@ -127,6 +125,18 @@ impl State {
|
|
|
127
125
|
.sndbuf
|
|
128
126
|
.saturating_sub(self.write_queue.len())
|
|
129
127
|
}
|
|
128
|
+
|
|
129
|
+
/// Whether a send may queue, as `sk_stream_wait_connect` answers one not
|
|
130
|
+
/// connected: EAGAIN while it connects, a pending failure once, then
|
|
131
|
+
/// EPIPE; and as `sk_stream_error` answers one past its failure or its
|
|
132
|
+
/// writing side's shutdown: the failure once, then EPIPE.
|
|
133
|
+
fn sendable(&mut self) -> Result<(), Errno> {
|
|
134
|
+
match self.phase {
|
|
135
|
+
Phase::Connecting => Err(Errno::WouldBlock),
|
|
136
|
+
Phase::Connected if !self.inet.send_shutdown && self.inet.err.is_none() => Ok(()),
|
|
137
|
+
_ => Err(self.inet.err.take().unwrap_or(Errno::BrokenPipe)),
|
|
138
|
+
}
|
|
139
|
+
}
|
|
130
140
|
}
|
|
131
141
|
|
|
132
142
|
/// One TCP socket, shared by the socket file and the reactor.
|
|
@@ -140,22 +150,17 @@ pub struct TcpSock {
|
|
|
140
150
|
|
|
141
151
|
impl TcpSock {
|
|
142
152
|
/// A socket connected to nothing, which connects through `family`, as
|
|
143
|
-
/// `inet_create` makes one; its
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
family: Arc<dyn ProtocolFamily>,
|
|
147
|
-
cred: &Arc<Cred>,
|
|
148
|
-
) -> Result<Arc<Self>, Errno> {
|
|
149
|
-
let memory = MemoryCharge::new(cred.memory_budget(), 2 * MAX_SOCKET_BUFFER)?;
|
|
150
|
-
Ok(Arc::new_cyclic(|this| Self {
|
|
153
|
+
/// `inet_create` makes one; its queues hold what their buffers allow.
|
|
154
|
+
pub(crate) fn new(family: Arc<dyn ProtocolFamily>) -> Arc<Self> {
|
|
155
|
+
Arc::new_cyclic(|this| Self {
|
|
151
156
|
state: Mutex::new(State {
|
|
152
157
|
phase: Phase::Unconnected,
|
|
153
158
|
host: None,
|
|
154
|
-
receive_queue:
|
|
159
|
+
receive_queue: SkBuffHead::default(),
|
|
155
160
|
received_end: false,
|
|
156
|
-
write_queue:
|
|
161
|
+
write_queue: SkBuffHead::default(),
|
|
157
162
|
fin_sent: false,
|
|
158
|
-
inet: InetSock::new(
|
|
163
|
+
inet: InetSock::new(Buffers::tcp()),
|
|
159
164
|
keepalive: KeepAlive {
|
|
160
165
|
on: false,
|
|
161
166
|
idle: TCP_KEEPALIVE_TIME,
|
|
@@ -168,7 +173,7 @@ impl TcpSock {
|
|
|
168
173
|
wait: Arc::default(),
|
|
169
174
|
family,
|
|
170
175
|
this: this.clone(),
|
|
171
|
-
})
|
|
176
|
+
})
|
|
172
177
|
}
|
|
173
178
|
|
|
174
179
|
/// Whether the socket may begin a connect, as `__inet_stream_connect`
|
|
@@ -193,16 +198,20 @@ impl TcpSock {
|
|
|
193
198
|
/// the SYN to one destination; the connect goes on without blocking, so
|
|
194
199
|
/// this returns at once and a poll finds the socket writable once it
|
|
195
200
|
/// settles. The socket counts against `open_connections` and the process
|
|
196
|
-
/// until released, and
|
|
197
|
-
///
|
|
201
|
+
/// until released, and spends one of `budget` as the SYN is about to
|
|
202
|
+
/// leave; a full count or a spent budget answers `EAGAIN`, never a Trap,
|
|
203
|
+
/// so one Sandbox cannot end another's run.
|
|
198
204
|
pub(crate) fn dial(
|
|
199
205
|
&self,
|
|
200
206
|
addr: SocketAddr,
|
|
201
207
|
peer: SocketAddr,
|
|
202
208
|
open_connections: &Arc<OpenConnections>,
|
|
209
|
+
budget: &ConnectionBudget,
|
|
203
210
|
) -> Result<(), Errno> {
|
|
204
211
|
let mut state = self.state();
|
|
205
|
-
|
|
212
|
+
let place = state.inet.open_place(open_connections)?;
|
|
213
|
+
budget.spend()?;
|
|
214
|
+
if let Some(place) = place {
|
|
206
215
|
state.inet.place = Some(place);
|
|
207
216
|
}
|
|
208
217
|
self.start_connect(&mut state, addr, peer);
|
|
@@ -215,18 +224,21 @@ impl TcpSock {
|
|
|
215
224
|
/// already judged the name the guest asked for, so they see no real
|
|
216
225
|
/// address. The socket holds `place`, the request's own, so a socket
|
|
217
226
|
/// still sending once the request is released counts against the caps
|
|
218
|
-
/// until it closes, as a guest's socket keeps its place.
|
|
227
|
+
/// until it closes, as a guest's socket keeps its place. `spend` is asked
|
|
228
|
+
/// once the filter admits the address, before the SYN leaves.
|
|
219
229
|
pub(crate) fn connect_upstream(
|
|
220
230
|
&self,
|
|
221
231
|
ports: &Arc<BindHash>,
|
|
222
232
|
ruleset: &Ruleset,
|
|
223
233
|
dest: &Destination,
|
|
224
234
|
place: &Arc<Place>,
|
|
235
|
+
spend: impl FnOnce() -> Result<(), Errno>,
|
|
225
236
|
) -> Result<(), Errno> {
|
|
226
237
|
self.check_state()?;
|
|
227
238
|
self.state().inet.place = Some(place.clone());
|
|
228
239
|
self.connect_from_port(ports, || {
|
|
229
240
|
let real = filter_with(ruleset, &*self.family, dest, Protocol::Http)?;
|
|
241
|
+
spend()?;
|
|
230
242
|
self.start_connect(&mut self.state(), real, real);
|
|
231
243
|
Ok(())
|
|
232
244
|
})
|
|
@@ -269,8 +281,8 @@ impl TcpSock {
|
|
|
269
281
|
}
|
|
270
282
|
|
|
271
283
|
/// Start connecting the host socket to `addr`, or fail the connect. The
|
|
272
|
-
/// dial registers on the reactor running as it dials, so a reactor
|
|
273
|
-
///
|
|
284
|
+
/// dial registers on the reactor running as it dials, so a reactor that
|
|
285
|
+
/// stopped meanwhile is replaced, not waited on.
|
|
274
286
|
fn dial_host(&self, state: &mut State, addr: SocketAddr) {
|
|
275
287
|
let dialed = Reactor::current().and_then(|reactor| {
|
|
276
288
|
let stream = self.family.connect(addr)?;
|
|
@@ -280,7 +292,8 @@ impl TcpSock {
|
|
|
280
292
|
let _ = Self::set_options(SockRef::from(&stream), state);
|
|
281
293
|
let mut stream = mio::net::TcpStream::from_std(stream);
|
|
282
294
|
let ready: Weak<dyn Ready> = self.this.clone();
|
|
283
|
-
|
|
295
|
+
// A connect settles as the host socket turns writable.
|
|
296
|
+
let registration = reactor.register(&mut stream, ready, true)?;
|
|
284
297
|
Ok((stream, registration))
|
|
285
298
|
});
|
|
286
299
|
match dialed {
|
|
@@ -304,6 +317,27 @@ impl TcpSock {
|
|
|
304
317
|
state.inet.err = Some(err);
|
|
305
318
|
}
|
|
306
319
|
|
|
320
|
+
/// End a socket whose peer's end was read on the reset `events` report,
|
|
321
|
+
/// as `tcp_reset` sets `sk_err` whatever the socket last read: no read
|
|
322
|
+
/// asks the host socket past the end, so only the event tells. The host
|
|
323
|
+
/// socket's pending error names the failure where it holds one, and
|
|
324
|
+
/// otherwise EPIPE, as `tcp_reset` answers a socket in `TCP_CLOSE_WAIT`.
|
|
325
|
+
fn reset_past_end(state: &mut State, events: PollMask) {
|
|
326
|
+
if state.phase != Phase::Connected || !state.received_end {
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
let pending = state
|
|
330
|
+
.host
|
|
331
|
+
.as_ref()
|
|
332
|
+
.and_then(|(stream, _)| stream.take_error().ok().flatten());
|
|
333
|
+
let err = match pending {
|
|
334
|
+
Some(err) => Errno::from_io_error(&err),
|
|
335
|
+
None if events.contains(PollMask::HUP) && !state.fin_sent => Errno::BrokenPipe,
|
|
336
|
+
None => return,
|
|
337
|
+
};
|
|
338
|
+
Self::done_with_error(state, err);
|
|
339
|
+
}
|
|
340
|
+
|
|
307
341
|
/// End the socket, as `tcp_done` does: `TCP_CLOSE`, both sides shut
|
|
308
342
|
/// down, its port given back. Linux ends one whose FIN left once the
|
|
309
343
|
/// peer's arrived and its own was acknowledged; no acknowledgement shows
|
|
@@ -321,10 +355,10 @@ impl TcpSock {
|
|
|
321
355
|
/// connected, as `tcp_disconnect` clears what the connect left.
|
|
322
356
|
fn disconnect(state: &mut State) {
|
|
323
357
|
state.phase = Phase::Unconnected;
|
|
324
|
-
state.receive_queue.
|
|
358
|
+
state.receive_queue.purge();
|
|
325
359
|
state.received_end = false;
|
|
326
360
|
state.inet.rcv_shutdown = false;
|
|
327
|
-
state.write_queue.
|
|
361
|
+
state.write_queue.purge();
|
|
328
362
|
state.inet.send_shutdown = false;
|
|
329
363
|
state.fin_sent = false;
|
|
330
364
|
state.inet.err = None;
|
|
@@ -340,23 +374,29 @@ impl TcpSock {
|
|
|
340
374
|
}
|
|
341
375
|
}
|
|
342
376
|
|
|
343
|
-
/// Move what the host socket takes and holds now, without blocking
|
|
377
|
+
/// Move what the host socket takes and holds now, without blocking, then
|
|
378
|
+
/// have the reactor watch for room only while something waits for it.
|
|
344
379
|
fn pump(&self, state: &mut State) {
|
|
345
380
|
if state.phase == Phase::Connecting {
|
|
346
381
|
self.settle_connect(state);
|
|
347
382
|
}
|
|
348
|
-
if state.phase
|
|
349
|
-
|
|
383
|
+
if state.phase == Phase::Connected {
|
|
384
|
+
match Self::move_bytes(state) {
|
|
385
|
+
Ok(()) if state.fin_sent && state.received_end => Self::done(state),
|
|
386
|
+
Ok(()) => {}
|
|
387
|
+
Err(err) => Self::done_with_error(state, err),
|
|
388
|
+
}
|
|
350
389
|
}
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
Err(err) => Self::done_with_error(state, err),
|
|
390
|
+
let nospace = state.phase == Phase::Connecting || !state.write_queue.is_empty();
|
|
391
|
+
if let Some((stream, registration)) = state.host.as_mut() {
|
|
392
|
+
registration.set_nospace(stream, nospace);
|
|
355
393
|
}
|
|
356
394
|
}
|
|
357
395
|
|
|
358
396
|
/// Read what arrived and write what is queued, until the host socket
|
|
359
|
-
/// would block; a failure either way is the connection's end.
|
|
397
|
+
/// would block; a failure either way is the connection's end. The
|
|
398
|
+
/// receive queue takes no more than its buffer, so the host socket keeps
|
|
399
|
+
/// the rest and pushes back on the peer.
|
|
360
400
|
fn move_bytes(state: &mut State) -> Result<(), Errno> {
|
|
361
401
|
let Some((stream, _)) = state.host.as_mut() else {
|
|
362
402
|
return Ok(());
|
|
@@ -367,22 +407,24 @@ impl TcpSock {
|
|
|
367
407
|
let room = (rcvbuf - state.receive_queue.len()).min(MAX_HOST_READ);
|
|
368
408
|
match stream.read(&mut chunk[..room]) {
|
|
369
409
|
Ok(0) => state.received_end = true,
|
|
370
|
-
Ok(n) => state.receive_queue.
|
|
410
|
+
Ok(n) => state.receive_queue.push(&chunk[..n]),
|
|
371
411
|
Err(err) if err.kind() == io::ErrorKind::Interrupted => {}
|
|
372
412
|
Err(err) if err.kind() == io::ErrorKind::WouldBlock => break,
|
|
373
413
|
Err(err) => return Err(Errno::from_io_error(&err)),
|
|
374
414
|
}
|
|
375
415
|
}
|
|
416
|
+
let queued = state.write_queue.len();
|
|
376
417
|
while !state.write_queue.is_empty() {
|
|
377
|
-
match stream.write(state.write_queue.
|
|
378
|
-
Ok(n) =>
|
|
379
|
-
state.write_queue.drain(..n);
|
|
380
|
-
}
|
|
418
|
+
match stream.write(state.write_queue.front()) {
|
|
419
|
+
Ok(n) => state.write_queue.advance(n),
|
|
381
420
|
Err(err) if err.kind() == io::ErrorKind::Interrupted => {}
|
|
382
421
|
Err(err) if err.kind() == io::ErrorKind::WouldBlock => break,
|
|
383
422
|
Err(err) => return Err(Errno::from_io_error(&err)),
|
|
384
423
|
}
|
|
385
424
|
}
|
|
425
|
+
if state.write_queue.len() < queued {
|
|
426
|
+
state.inet.buffers.sndbuf_expand();
|
|
427
|
+
}
|
|
386
428
|
if state.inet.send_shutdown && state.write_queue.is_empty() && !state.fin_sent {
|
|
387
429
|
let _ = stream.shutdown(Shutdown::Write);
|
|
388
430
|
state.fin_sent = true;
|
|
@@ -440,12 +482,19 @@ impl TcpSock {
|
|
|
440
482
|
/// Give back the socket's place and memory and close the host socket,
|
|
441
483
|
/// answering the hold an orphan kept on itself.
|
|
442
484
|
fn let_go(state: &mut State) -> Option<Arc<TcpSock>> {
|
|
443
|
-
state.write_queue.
|
|
485
|
+
state.write_queue.purge();
|
|
444
486
|
state.inet.release();
|
|
445
487
|
Self::close_host(state);
|
|
446
488
|
state.orphan.take()
|
|
447
489
|
}
|
|
448
490
|
|
|
491
|
+
/// How many bytes arrived that nothing has read, as Linux's `SIOCINQ`
|
|
492
|
+
/// reads, so a test sees what the reactor queued.
|
|
493
|
+
#[cfg(test)]
|
|
494
|
+
pub(crate) fn unread(&self) -> usize {
|
|
495
|
+
self.state().receive_queue.len()
|
|
496
|
+
}
|
|
497
|
+
|
|
449
498
|
/// Lock the host socket's send buffer at `size`, so the host stops
|
|
450
499
|
/// taking bytes once it holds that many, as a test needs a connection
|
|
451
500
|
/// still sending.
|
|
@@ -493,11 +542,28 @@ impl TcpSock {
|
|
|
493
542
|
self.state().phase == Phase::Connecting
|
|
494
543
|
}
|
|
495
544
|
|
|
496
|
-
/// How many bytes a send would take now.
|
|
545
|
+
/// How many bytes a send would take now: what the send buffer has left.
|
|
497
546
|
pub fn send_room(&self) -> usize {
|
|
498
547
|
self.state().stream_wspace()
|
|
499
548
|
}
|
|
500
549
|
|
|
550
|
+
/// Queue all of `buf`, which the room asked before promised. A send
|
|
551
|
+
/// buffer that shrank since takes none of it, failing whole with ENOBUFS
|
|
552
|
+
/// rather than leave the peer a stream with a gap.
|
|
553
|
+
pub fn send_whole(&self, buf: &[u8]) -> Result<(), Errno> {
|
|
554
|
+
let mut state = self.state();
|
|
555
|
+
state.sendable()?;
|
|
556
|
+
if buf.is_empty() {
|
|
557
|
+
return Ok(());
|
|
558
|
+
}
|
|
559
|
+
if state.stream_wspace() < buf.len() {
|
|
560
|
+
return Err(Errno::NoBufferSpace);
|
|
561
|
+
}
|
|
562
|
+
state.write_queue.push(buf);
|
|
563
|
+
self.pump_and_wake(state);
|
|
564
|
+
Ok(())
|
|
565
|
+
}
|
|
566
|
+
|
|
501
567
|
/// Whether everything sent has left for the host, as Linux's `SIOCOUTQ`
|
|
502
568
|
/// reads zero.
|
|
503
569
|
pub fn is_drained(&self) -> bool {
|
|
@@ -523,17 +589,24 @@ impl TcpSock {
|
|
|
523
589
|
}
|
|
524
590
|
|
|
525
591
|
impl Ready for TcpSock {
|
|
526
|
-
fn on_event(&self) {
|
|
527
|
-
let state = self.state();
|
|
592
|
+
fn on_event(&self, events: PollMask) {
|
|
593
|
+
let mut state = self.state();
|
|
594
|
+
if events.contains(PollMask::HUP) || events.contains(PollMask::ERR) {
|
|
595
|
+
Self::reset_past_end(&mut state, events);
|
|
596
|
+
}
|
|
528
597
|
self.pump_and_wake(state);
|
|
529
598
|
}
|
|
599
|
+
|
|
600
|
+
fn abort(&self) {
|
|
601
|
+
TcpSock::abort(self);
|
|
602
|
+
}
|
|
530
603
|
}
|
|
531
604
|
|
|
532
605
|
/// TCP's answers for its socket file, as `inet_stream_ops` hands each call to
|
|
533
606
|
/// `tcp_prot`.
|
|
534
607
|
impl ProtoOps for TcpSock {
|
|
535
|
-
/// The socket's state first, then `net`'s `connect4`, then
|
|
536
|
-
///
|
|
608
|
+
/// The socket's state first, then `net`'s `connect4`, then a port, the
|
|
609
|
+
/// ruleset, and the dial within the connection caps, as
|
|
537
610
|
/// `inet_stream_connect` runs `connect4` only on a socket connected to
|
|
538
611
|
/// nothing and `tcp_v4_connect` hashes a port before the SYN meets the
|
|
539
612
|
/// filter. A refused destination puts the port back, as that failure
|
|
@@ -541,11 +614,9 @@ impl ProtoOps for TcpSock {
|
|
|
541
614
|
fn connect(&self, net: &Namespace, addr: SocketAddr) -> Result<(), Errno> {
|
|
542
615
|
self.check_state()?;
|
|
543
616
|
let dest = connect4(net, addr)?;
|
|
544
|
-
net.
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
self.dial(real, addr, net.open_connections())
|
|
548
|
-
})
|
|
617
|
+
self.connect_from_port(net.ports_of(Protocol::Tcp), || {
|
|
618
|
+
let real = filter(net, &dest, Protocol::Tcp)?;
|
|
619
|
+
self.dial(real, addr, net.open_connections(), net.connection_budget())
|
|
549
620
|
})
|
|
550
621
|
}
|
|
551
622
|
|
|
@@ -566,10 +637,7 @@ impl ProtoOps for TcpSock {
|
|
|
566
637
|
fn recvmsg(&self, buf: &mut [u8]) -> Result<(usize, Option<SocketAddr>), Errno> {
|
|
567
638
|
let mut state = self.state();
|
|
568
639
|
if !state.receive_queue.is_empty() {
|
|
569
|
-
let take = state.receive_queue.
|
|
570
|
-
for (slot, byte) in buf.iter_mut().zip(state.receive_queue.drain(..take)) {
|
|
571
|
-
*slot = byte;
|
|
572
|
-
}
|
|
640
|
+
let take = state.receive_queue.read_into(buf);
|
|
573
641
|
self.pump_and_wake(state);
|
|
574
642
|
return Ok((take, None));
|
|
575
643
|
}
|
|
@@ -588,23 +656,16 @@ impl ProtoOps for TcpSock {
|
|
|
588
656
|
}
|
|
589
657
|
|
|
590
658
|
/// Queue what fits of `buf`, sent as the host socket takes it; a full
|
|
591
|
-
/// buffer answers EAGAIN
|
|
592
|
-
/// `sk_stream_wait_connect` does: EAGAIN while it connects, a pending
|
|
593
|
-
/// failure once, then EPIPE. A connected one past its failure or its
|
|
594
|
-
/// writing side's shutdown answers as `sk_stream_error`: the failure
|
|
595
|
-
/// once, then EPIPE.
|
|
659
|
+
/// buffer answers EAGAIN, and a socket that may not send answers why.
|
|
596
660
|
fn sendmsg(&self, buf: &[u8]) -> Result<usize, Errno> {
|
|
597
661
|
let mut state = self.state();
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
_ => return Err(state.inet.err.take().unwrap_or(Errno::BrokenPipe)),
|
|
602
|
-
}
|
|
603
|
-
let take = buf.len().min(state.stream_wspace());
|
|
662
|
+
state.sendable()?;
|
|
663
|
+
let copy = buf.len().min(state.stream_wspace());
|
|
664
|
+
let take = copy;
|
|
604
665
|
if take == 0 && !buf.is_empty() {
|
|
605
666
|
return Err(Errno::WouldBlock);
|
|
606
667
|
}
|
|
607
|
-
state.write_queue.
|
|
668
|
+
state.write_queue.push(&buf[..take]);
|
|
608
669
|
self.pump_and_wake(state);
|
|
609
670
|
Ok(take)
|
|
610
671
|
}
|
|
@@ -613,7 +674,8 @@ impl ProtoOps for TcpSock {
|
|
|
613
674
|
/// down, readable once the reading side is, and writable unless it
|
|
614
675
|
/// connects, always so once its writing side is shut down and otherwise
|
|
615
676
|
/// once the room left is half what is queued, as
|
|
616
|
-
/// `__sk_stream_is_writeable` asks
|
|
677
|
+
/// `__sk_stream_is_writeable` asks, and the memory takes a send, so a
|
|
678
|
+
/// writable stream always has room to promise.
|
|
617
679
|
fn poll(&self, table: Option<&mut PollTable>) -> PollMask {
|
|
618
680
|
poll_wait(&self.wait, table);
|
|
619
681
|
let state = self.state();
|
|
@@ -627,7 +689,8 @@ impl ProtoOps for TcpSock {
|
|
|
627
689
|
if !state.receive_queue.is_empty() || rcv_shutdown {
|
|
628
690
|
mask = mask | PollMask::IN;
|
|
629
691
|
}
|
|
630
|
-
let writable =
|
|
692
|
+
let writable =
|
|
693
|
+
state.stream_wspace() >= state.write_queue.len() / 2 && state.stream_wspace() > 0;
|
|
631
694
|
if state.phase != Phase::Connecting && (send_shutdown || writable) {
|
|
632
695
|
mask = mask | PollMask::OUT;
|
|
633
696
|
}
|
|
@@ -714,7 +777,8 @@ impl ProtoOps for TcpSock {
|
|
|
714
777
|
/// Let go of the socket once every view is gone, as `sock_close` runs on
|
|
715
778
|
/// the last `fput` and `__tcp_close` closes it: bytes still queued leave
|
|
716
779
|
/// before the FIN, the socket holding itself and its place until then,
|
|
717
|
-
///
|
|
780
|
+
/// as Linux's orphan holds no descriptor yet stays a connection, and
|
|
781
|
+
/// bytes that arrived unread reset the connection instead.
|
|
718
782
|
fn release(&self) {
|
|
719
783
|
let mut state = self.state();
|
|
720
784
|
let unread = !std::mem::take(&mut state.receive_queue).is_empty();
|
|
@@ -741,16 +805,20 @@ mod tests {
|
|
|
741
805
|
|
|
742
806
|
use super::*;
|
|
743
807
|
use crate::net::socket::testing::wait_for;
|
|
808
|
+
use crate::net::MAX_SOCKET_BUFFER;
|
|
744
809
|
use crate::net::{sock_alloc_file, HostInet};
|
|
745
|
-
use crate::security::prepare_creds;
|
|
746
810
|
use crate::vfs::File;
|
|
747
811
|
|
|
748
812
|
/// A socket connecting to `addr` through the host.
|
|
749
813
|
fn dial(addr: SocketAddr) -> Arc<TcpSock> {
|
|
750
|
-
let sock = TcpSock::new(Arc::new(HostInet)
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
814
|
+
let sock = TcpSock::new(Arc::new(HostInet));
|
|
815
|
+
sock.dial(
|
|
816
|
+
addr,
|
|
817
|
+
addr,
|
|
818
|
+
&OpenConnections::new(None),
|
|
819
|
+
&ConnectionBudget::new(None),
|
|
820
|
+
)
|
|
821
|
+
.expect("an uncapped socket connects");
|
|
754
822
|
sock
|
|
755
823
|
}
|
|
756
824
|
|
|
@@ -782,16 +850,125 @@ mod tests {
|
|
|
782
850
|
(sock, file, peer)
|
|
783
851
|
}
|
|
784
852
|
|
|
853
|
+
/// Wait until what the socket wrote reached `peer`, so its close finds
|
|
854
|
+
/// bytes unread and resets, as Linux's `tcp_close` does.
|
|
855
|
+
fn wait_until_arrived(peer: &std::net::TcpStream) {
|
|
856
|
+
peer.set_read_timeout(Some(crate::sched::testing::MAX_WAIT))
|
|
857
|
+
.unwrap();
|
|
858
|
+
let arrived = peer.peek(&mut [0; 1]);
|
|
859
|
+
assert!(
|
|
860
|
+
arrived.as_ref().is_ok_and(|n| *n > 0),
|
|
861
|
+
"the bytes the socket wrote never reached its peer, got {arrived:?}"
|
|
862
|
+
);
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
// @behavior NW-116
|
|
866
|
+
#[test]
|
|
867
|
+
fn a_stream_holds_its_receive_buffer_until_the_guest_reads() {
|
|
868
|
+
let (sock, file, mut peer) = connected_socket();
|
|
869
|
+
let sent = (0..1 << 20).map(|i| (i % 251) as u8).collect::<Vec<_>>();
|
|
870
|
+
let expected = sent.clone();
|
|
871
|
+
let writer = std::thread::spawn(move || peer.write_all(&sent));
|
|
872
|
+
// Each poll moves what the host socket holds into the queue.
|
|
873
|
+
let started = std::time::Instant::now();
|
|
874
|
+
let rcvbuf = sock.state().inet.buffers.rcvbuf;
|
|
875
|
+
while sock.state().receive_queue.len() < rcvbuf
|
|
876
|
+
&& started.elapsed() < Duration::from_secs(10)
|
|
877
|
+
{
|
|
878
|
+
file.poll(None);
|
|
879
|
+
std::thread::sleep(Duration::from_millis(1));
|
|
880
|
+
}
|
|
881
|
+
file.poll(None);
|
|
882
|
+
let held = sock.state().receive_queue.len();
|
|
883
|
+
let mut got = Vec::new();
|
|
884
|
+
let mut buf = [0; 4096];
|
|
885
|
+
while got.len() < expected.len() && started.elapsed() < Duration::from_secs(10) {
|
|
886
|
+
match sock.recvmsg(&mut buf) {
|
|
887
|
+
Ok((0, _)) => break,
|
|
888
|
+
Ok((n, _)) => got.extend_from_slice(&buf[..n]),
|
|
889
|
+
Err(_) => std::thread::sleep(Duration::from_millis(1)),
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
writer.join().unwrap().expect("the peer writes");
|
|
893
|
+
assert!(
|
|
894
|
+
held == rcvbuf && got == expected,
|
|
895
|
+
"a 1 MiB stream the guest has not read must queue no more than its {rcvbuf}-byte \
|
|
896
|
+
receive buffer, then reach the guest whole and in order; held {held}, got {} bytes",
|
|
897
|
+
got.len()
|
|
898
|
+
);
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
// @behavior NW-239
|
|
902
|
+
#[test]
|
|
903
|
+
fn a_write_past_the_send_buffer_waits_for_the_queue_to_drain() {
|
|
904
|
+
let (sock, file, mut peer) = connected_socket();
|
|
905
|
+
sock.lock_host_send_buffer(4096);
|
|
906
|
+
let chunk = [7; 1024];
|
|
907
|
+
let mut written = 0;
|
|
908
|
+
let refused = loop {
|
|
909
|
+
match sock.sendmsg(&chunk) {
|
|
910
|
+
Ok(n) => written += n,
|
|
911
|
+
Err(err) => break err,
|
|
912
|
+
}
|
|
913
|
+
if written > 64 << 20 {
|
|
914
|
+
break Errno::NoBufferSpace;
|
|
915
|
+
}
|
|
916
|
+
};
|
|
917
|
+
let queued = sock.state().write_queue.len();
|
|
918
|
+
let sndbuf = sock.state().inet.buffers.sndbuf;
|
|
919
|
+
let writable_while_full = file.poll(None).contains(PollMask::OUT);
|
|
920
|
+
let reader = std::thread::spawn(move || {
|
|
921
|
+
peer.set_read_timeout(Some(Duration::from_secs(5))).unwrap();
|
|
922
|
+
let mut buf = [0; 64 * 1024];
|
|
923
|
+
let mut read = 0;
|
|
924
|
+
while read < written {
|
|
925
|
+
match peer.read(&mut buf) {
|
|
926
|
+
Ok(0) | Err(_) => break,
|
|
927
|
+
Ok(n) => read += n,
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
read
|
|
931
|
+
});
|
|
932
|
+
wait_for(|table| file.poll(table), PollMask::OUT);
|
|
933
|
+
drop(sock);
|
|
934
|
+
assert!(
|
|
935
|
+
refused == Errno::WouldBlock && !writable_while_full && queued <= sndbuf,
|
|
936
|
+
"writes to a socket whose peer does not read must queue no more than its \
|
|
937
|
+
{sndbuf}-byte send buffer, then answer EAGAIN and poll unwritable until the peer \
|
|
938
|
+
reads; refused {refused:?}, queued {queued}, writable {writable_while_full}"
|
|
939
|
+
);
|
|
940
|
+
reader.join().unwrap();
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
// A connected socket stays writable, so a reactor watching it for room it
|
|
944
|
+
// never waited on would hear of it again on every turn.
|
|
945
|
+
// @behavior NW-257
|
|
946
|
+
#[test]
|
|
947
|
+
fn a_quiet_connection_leaves_its_waiter_asleep() {
|
|
948
|
+
let (_sock, file, _peer) = connected_socket();
|
|
949
|
+
let waited = crate::net::socket::testing::looks_waiting_to_read(
|
|
950
|
+
|table| file.poll(table),
|
|
951
|
+
Duration::from_millis(200),
|
|
952
|
+
);
|
|
953
|
+
assert_eq!(
|
|
954
|
+
waited,
|
|
955
|
+
(2, false),
|
|
956
|
+
"a guest waiting to read a TCP socket whose peer sends nothing and which has nothing \
|
|
957
|
+
left to send must look once as its wait starts and once as it ends, woken by nothing in \
|
|
958
|
+
between; (looks, found) was {waited:?}"
|
|
959
|
+
);
|
|
960
|
+
}
|
|
961
|
+
|
|
785
962
|
// @behavior RT-381
|
|
786
963
|
#[test]
|
|
787
964
|
fn a_socket_file_never_waits() {
|
|
788
965
|
let (_sock, mut file, _peer) = connected_socket();
|
|
789
966
|
file.setfl(false, false);
|
|
790
|
-
let
|
|
967
|
+
let waits = crate::sched::testing::count_waits();
|
|
791
968
|
let read = crate::sched::testing::within_max_wait(|| file.read(&mut [0; 8]));
|
|
792
969
|
assert_eq!(
|
|
793
|
-
(read,
|
|
794
|
-
(Err(Errno::WouldBlock),
|
|
970
|
+
(read, waits.load(std::sync::atomic::Ordering::SeqCst)),
|
|
971
|
+
(Err(Errno::WouldBlock), 0),
|
|
795
972
|
"File::read of a connected socket whose peer sent nothing must answer EAGAIN at \
|
|
796
973
|
once, its non-blocking flag cleared or not, since WASI's sockets never block"
|
|
797
974
|
);
|
|
@@ -846,14 +1023,18 @@ mod tests {
|
|
|
846
1023
|
fn a_socket_holds_its_options_and_gives_them_to_the_host_socket() {
|
|
847
1024
|
let listener = TcpListener::bind("127.0.0.1:0").expect("a loopback listener");
|
|
848
1025
|
let addr = listener.local_addr().unwrap();
|
|
849
|
-
let sock = TcpSock::new(Arc::new(HostInet)
|
|
850
|
-
.expect("an unbounded socket is made");
|
|
1026
|
+
let sock = TcpSock::new(Arc::new(HostInet));
|
|
851
1027
|
sock.setsockopt(SockOpt::KeepAlive, 1)
|
|
852
1028
|
.expect("an unconnected socket takes an option");
|
|
853
1029
|
let keepalive = |sock: &TcpSock| sock.getsockopt(SockOpt::KeepAlive).unwrap();
|
|
854
1030
|
let before = (keepalive(&sock), host_keepalive(&sock));
|
|
855
|
-
sock.dial(
|
|
856
|
-
|
|
1031
|
+
sock.dial(
|
|
1032
|
+
addr,
|
|
1033
|
+
addr,
|
|
1034
|
+
&OpenConnections::new(None),
|
|
1035
|
+
&ConnectionBudget::new(None),
|
|
1036
|
+
)
|
|
1037
|
+
.expect("an uncapped socket connects");
|
|
857
1038
|
let dialed = host_keepalive(&sock);
|
|
858
1039
|
sock.setsockopt(SockOpt::KeepAlive, 0)
|
|
859
1040
|
.expect("a connected socket takes an option");
|
|
@@ -868,15 +1049,9 @@ mod tests {
|
|
|
868
1049
|
}
|
|
869
1050
|
|
|
870
1051
|
// @behavior RT-367
|
|
871
|
-
// Windows sets keepalive timing through SIO_KEEPALIVE_VALS, which reads
|
|
872
|
-
// nothing back, so only the hosts that read it witness the dialed values.
|
|
873
|
-
#[cfg(not(windows))]
|
|
874
1052
|
#[test]
|
|
875
|
-
fn
|
|
876
|
-
let
|
|
877
|
-
let addr = listener.local_addr().unwrap();
|
|
878
|
-
let sock = TcpSock::new(Arc::new(HostInet), &prepare_creds(&[], None))
|
|
879
|
-
.expect("an unbounded socket is made");
|
|
1053
|
+
fn a_socket_bounds_its_keepalive_probes_as_linux() {
|
|
1054
|
+
let sock = TcpSock::new(Arc::new(HostInet));
|
|
880
1055
|
let get = |opt| sock.getsockopt(opt).unwrap();
|
|
881
1056
|
let defaults = (
|
|
882
1057
|
get(SockOpt::KeepIdle),
|
|
@@ -889,14 +1064,36 @@ mod tests {
|
|
|
889
1064
|
sock.setsockopt(SockOpt::KeepIntvl, 32768),
|
|
890
1065
|
sock.setsockopt(SockOpt::KeepCnt, 128),
|
|
891
1066
|
];
|
|
1067
|
+
assert_eq!(
|
|
1068
|
+
(defaults, refused),
|
|
1069
|
+
((7200, 75, 9), [(); 4].map(|()| Err(Errno::InvalidInput))),
|
|
1070
|
+
"a socket must read Linux's keepalive defaults and refuse with EINVAL an idle time or \
|
|
1071
|
+
interval outside 1 to 32767 and a count outside 1 to 127"
|
|
1072
|
+
);
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
// @behavior RT-367
|
|
1076
|
+
// Windows sets keepalive timing through SIO_KEEPALIVE_VALS, which reads
|
|
1077
|
+
// nothing back, so only the hosts that read it witness the dialed values.
|
|
1078
|
+
#[cfg(not(windows))]
|
|
1079
|
+
#[test]
|
|
1080
|
+
fn a_socket_dials_with_the_keepalive_probes_it_was_set() {
|
|
1081
|
+
let listener = TcpListener::bind("127.0.0.1:0").expect("a loopback listener");
|
|
1082
|
+
let addr = listener.local_addr().unwrap();
|
|
1083
|
+
let sock = TcpSock::new(Arc::new(HostInet));
|
|
892
1084
|
sock.setsockopt(SockOpt::KeepIdle, 30)
|
|
893
1085
|
.expect("an idle time Linux takes");
|
|
894
1086
|
sock.setsockopt(SockOpt::KeepIntvl, 10)
|
|
895
1087
|
.expect("an interval Linux takes");
|
|
896
1088
|
sock.setsockopt(SockOpt::KeepCnt, 3)
|
|
897
1089
|
.expect("a count Linux takes");
|
|
898
|
-
sock.dial(
|
|
899
|
-
|
|
1090
|
+
sock.dial(
|
|
1091
|
+
addr,
|
|
1092
|
+
addr,
|
|
1093
|
+
&OpenConnections::new(None),
|
|
1094
|
+
&ConnectionBudget::new(None),
|
|
1095
|
+
)
|
|
1096
|
+
.expect("an uncapped socket connects");
|
|
900
1097
|
let host = sock.state().host.as_ref().map(|(stream, _)| {
|
|
901
1098
|
let host = SockRef::from(stream);
|
|
902
1099
|
(
|
|
@@ -906,15 +1103,10 @@ mod tests {
|
|
|
906
1103
|
)
|
|
907
1104
|
});
|
|
908
1105
|
assert_eq!(
|
|
909
|
-
|
|
910
|
-
(
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
Some((Duration::from_secs(30), Duration::from_secs(10), 3))
|
|
914
|
-
),
|
|
915
|
-
"a socket must read Linux's keepalive defaults, refuse with EINVAL an idle time or \
|
|
916
|
-
interval outside 1 to 32767 and a count outside 1 to 127, and give the host stream it \
|
|
917
|
-
dials the probes it was set"
|
|
1106
|
+
host,
|
|
1107
|
+
Some((Duration::from_secs(30), Duration::from_secs(10), 3)),
|
|
1108
|
+
"a socket set to keepalive probes Linux takes must give the host stream it dials \
|
|
1109
|
+
those probes"
|
|
918
1110
|
);
|
|
919
1111
|
}
|
|
920
1112
|
|
|
@@ -923,8 +1115,7 @@ mod tests {
|
|
|
923
1115
|
fn a_socket_holds_its_hop_limit_for_the_host_socket() {
|
|
924
1116
|
let listener = TcpListener::bind("127.0.0.1:0").expect("a loopback listener");
|
|
925
1117
|
let addr = listener.local_addr().unwrap();
|
|
926
|
-
let sock = TcpSock::new(Arc::new(HostInet)
|
|
927
|
-
.expect("an unbounded socket is made");
|
|
1118
|
+
let sock = TcpSock::new(Arc::new(HostInet));
|
|
928
1119
|
let host_ttl = |sock: &TcpSock| {
|
|
929
1120
|
sock.state()
|
|
930
1121
|
.host
|
|
@@ -938,8 +1129,13 @@ mod tests {
|
|
|
938
1129
|
];
|
|
939
1130
|
sock.setsockopt(SockOpt::Ttl, 7)
|
|
940
1131
|
.expect("a hop limit Linux takes");
|
|
941
|
-
sock.dial(
|
|
942
|
-
|
|
1132
|
+
sock.dial(
|
|
1133
|
+
addr,
|
|
1134
|
+
addr,
|
|
1135
|
+
&OpenConnections::new(None),
|
|
1136
|
+
&ConnectionBudget::new(None),
|
|
1137
|
+
)
|
|
1138
|
+
.expect("an uncapped socket connects");
|
|
943
1139
|
let dialed = host_ttl(&sock);
|
|
944
1140
|
sock.setsockopt(SockOpt::Ttl, 9)
|
|
945
1141
|
.expect("a connected socket takes a hop limit");
|
|
@@ -987,7 +1183,7 @@ mod tests {
|
|
|
987
1183
|
true
|
|
988
1184
|
),
|
|
989
1185
|
"SO_RCVBUF and SO_SNDBUF must double a size as Linux does, within the socket's \
|
|
990
|
-
|
|
1186
|
+
cap and above Linux's least, and the queues must hold no more than they \
|
|
991
1187
|
say; the receive queue held {held}"
|
|
992
1188
|
);
|
|
993
1189
|
}
|
|
@@ -997,19 +1193,22 @@ mod tests {
|
|
|
997
1193
|
fn a_socket_binds_once_and_not_after_its_connect() {
|
|
998
1194
|
let any: SocketAddr = "0.0.0.0:0".parse().unwrap();
|
|
999
1195
|
let net = loopback(&[], OpenConnections::new(None));
|
|
1000
|
-
let
|
|
1001
|
-
let fresh = TcpSock::new(Arc::new(HostInet), &cred).expect("an unbounded socket is made");
|
|
1196
|
+
let fresh = TcpSock::new(Arc::new(HostInet));
|
|
1002
1197
|
let (first, second) = (fresh.bind(&net, any), fresh.bind(&net, any));
|
|
1003
|
-
let ipv6 = TcpSock::new(Arc::new(HostInet),
|
|
1004
|
-
.expect("an unbounded socket is made")
|
|
1005
|
-
.bind(&net, "[::]:0".parse().unwrap());
|
|
1198
|
+
let ipv6 = TcpSock::new(Arc::new(HostInet)).bind(&net, "[::]:0".parse().unwrap());
|
|
1006
1199
|
let (connected, _file, _peer) = connected_socket();
|
|
1007
1200
|
let udp_peer = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
|
|
1008
|
-
let udp = crate::net::UdpSock::new(
|
|
1201
|
+
let udp = crate::net::UdpSock::new();
|
|
1009
1202
|
let family: Arc<dyn ProtocolFamily> = Arc::new(HostInet);
|
|
1010
1203
|
let at = udp_peer.local_addr().unwrap();
|
|
1011
|
-
udp.dial(
|
|
1012
|
-
|
|
1204
|
+
udp.dial(
|
|
1205
|
+
&family,
|
|
1206
|
+
at,
|
|
1207
|
+
at,
|
|
1208
|
+
&OpenConnections::new(None),
|
|
1209
|
+
&ConnectionBudget::new(None),
|
|
1210
|
+
)
|
|
1211
|
+
.expect("an uncapped socket connects");
|
|
1013
1212
|
assert_eq!(
|
|
1014
1213
|
(
|
|
1015
1214
|
first,
|
|
@@ -1037,15 +1236,18 @@ mod tests {
|
|
|
1037
1236
|
// A privileged port no test can bind, so every connect to it is
|
|
1038
1237
|
// refused and no other socket is handed it meanwhile.
|
|
1039
1238
|
let closed = SocketAddr::from(([127, 0, 0, 1], 1));
|
|
1040
|
-
let cred = prepare_creds(&[], None);
|
|
1041
1239
|
let net = loopback(&[], OpenConnections::new(None));
|
|
1042
1240
|
let failed_after = |own: &str| {
|
|
1043
|
-
let sock =
|
|
1044
|
-
TcpSock::new(Arc::new(HostInet), &cred).expect("an unbounded socket is made");
|
|
1241
|
+
let sock = TcpSock::new(Arc::new(HostInet));
|
|
1045
1242
|
sock.bind(&net, own.parse().unwrap())
|
|
1046
1243
|
.expect("a fresh socket binds");
|
|
1047
|
-
sock.dial(
|
|
1048
|
-
|
|
1244
|
+
sock.dial(
|
|
1245
|
+
closed,
|
|
1246
|
+
closed,
|
|
1247
|
+
&OpenConnections::new(None),
|
|
1248
|
+
&ConnectionBudget::new(None),
|
|
1249
|
+
)
|
|
1250
|
+
.expect("an uncapped socket connects");
|
|
1049
1251
|
let file = sock_alloc_file(sock.clone(), &Arc::default());
|
|
1050
1252
|
wait_for(|table| file.poll(table), PollMask::ERR);
|
|
1051
1253
|
sock.bind(&net, "0.0.0.0:0".parse().unwrap())
|
|
@@ -1063,9 +1265,7 @@ mod tests {
|
|
|
1063
1265
|
#[test]
|
|
1064
1266
|
fn a_port_one_socket_holds_is_no_other_sockets() {
|
|
1065
1267
|
let net = loopback(&[], OpenConnections::new(None));
|
|
1066
|
-
let
|
|
1067
|
-
let socket =
|
|
1068
|
-
|| TcpSock::new(Arc::new(HostInet), &cred).expect("an unbounded socket is made");
|
|
1268
|
+
let socket = || TcpSock::new(Arc::new(HostInet));
|
|
1069
1269
|
let (named, free, again) = (socket(), socket(), socket());
|
|
1070
1270
|
let answers = (
|
|
1071
1271
|
named.bind(&net, "0.0.0.0:32768".parse().unwrap()),
|
|
@@ -1092,10 +1292,8 @@ mod tests {
|
|
|
1092
1292
|
fn a_connect_refused_for_a_full_count_or_by_the_ruleset_gives_back_its_port() {
|
|
1093
1293
|
let listener = TcpListener::bind("127.0.0.1:0").expect("a loopback listener");
|
|
1094
1294
|
let at = listener.local_addr().unwrap();
|
|
1095
|
-
let cred = prepare_creds(&[], None);
|
|
1096
1295
|
let refused_after = |net: &Namespace, to: SocketAddr| {
|
|
1097
|
-
let sock =
|
|
1098
|
-
TcpSock::new(Arc::new(HostInet), &cred).expect("an unbounded socket is made");
|
|
1296
|
+
let sock = TcpSock::new(Arc::new(HostInet));
|
|
1099
1297
|
let connected = ProtoOps::connect(&*sock, net, to);
|
|
1100
1298
|
(connected, sock.bind(net, "0.0.0.0:0".parse().unwrap()))
|
|
1101
1299
|
};
|
|
@@ -1122,12 +1320,15 @@ mod tests {
|
|
|
1122
1320
|
fn a_socket_polls_writable_once_its_room_is_half_what_it_queued() {
|
|
1123
1321
|
let (sock, file, _peer) = connected_socket();
|
|
1124
1322
|
sock.setsockopt(SockOpt::SndBuf, 1000).unwrap();
|
|
1323
|
+
// A reactor event fetched before a deregister can still pump the
|
|
1324
|
+
// queue, so the host socket goes, leaving nowhere to move bytes.
|
|
1125
1325
|
let polled_with = |queued: usize| {
|
|
1126
1326
|
let mut state = sock.state();
|
|
1127
|
-
if let Some((stream, registration)) = state.host.
|
|
1128
|
-
registration.deregister(stream);
|
|
1327
|
+
if let Some((mut stream, registration)) = state.host.take() {
|
|
1328
|
+
registration.deregister(&mut stream);
|
|
1129
1329
|
}
|
|
1130
|
-
state.write_queue
|
|
1330
|
+
state.write_queue.purge();
|
|
1331
|
+
state.write_queue.push(&vec![0; queued]);
|
|
1131
1332
|
drop(state);
|
|
1132
1333
|
file.poll(None).contains(PollMask::OUT)
|
|
1133
1334
|
};
|
|
@@ -1182,9 +1383,7 @@ mod tests {
|
|
|
1182
1383
|
// @behavior RT-364
|
|
1183
1384
|
#[test]
|
|
1184
1385
|
fn a_socket_whose_connect_failed_answers_the_failure_then_connects_afresh() {
|
|
1185
|
-
let
|
|
1186
|
-
let refused_at = closed.local_addr().unwrap();
|
|
1187
|
-
drop(closed);
|
|
1386
|
+
let refused_at = SocketAddr::from(([127, 0, 0, 1], 1));
|
|
1188
1387
|
// One place, which the failed socket still holds, so connecting
|
|
1189
1388
|
// afresh must reuse it rather than ask the full cap for another.
|
|
1190
1389
|
let listener = TcpListener::bind("127.0.0.1:0").expect("a loopback listener");
|
|
@@ -1193,8 +1392,7 @@ mod tests {
|
|
|
1193
1392
|
&[refused_at.port(), open_at.port()],
|
|
1194
1393
|
OpenConnections::new(Some(1)),
|
|
1195
1394
|
);
|
|
1196
|
-
let sock = TcpSock::new(Arc::new(HostInet)
|
|
1197
|
-
.expect("an unbounded socket is made");
|
|
1395
|
+
let sock = TcpSock::new(Arc::new(HostInet));
|
|
1198
1396
|
ProtoOps::connect(&*sock, &net, refused_at).expect("a socket under a cap of one connects");
|
|
1199
1397
|
let file = sock_alloc_file(sock.clone(), &Arc::default());
|
|
1200
1398
|
wait_for(|table| file.poll(table), PollMask::ERR);
|
|
@@ -1223,15 +1421,19 @@ mod tests {
|
|
|
1223
1421
|
#[test]
|
|
1224
1422
|
fn a_socket_whose_connect_failed_takes_a_fresh_virtual_port() {
|
|
1225
1423
|
let closed = SocketAddr::from(([127, 0, 0, 1], 1));
|
|
1226
|
-
let sock = TcpSock::new(Arc::new(HostInet)
|
|
1227
|
-
.expect("an unbounded socket is made");
|
|
1424
|
+
let sock = TcpSock::new(Arc::new(HostInet));
|
|
1228
1425
|
let file = sock_alloc_file(sock.clone(), &Arc::default());
|
|
1229
1426
|
let ports = BindHash::new();
|
|
1230
1427
|
// The port is held before the dial, as a connect holds it.
|
|
1231
1428
|
sock.hash_connect(&ports).expect("a port is free");
|
|
1232
1429
|
let before = sock.getname(false).unwrap().port();
|
|
1233
|
-
sock.dial(
|
|
1234
|
-
|
|
1430
|
+
sock.dial(
|
|
1431
|
+
closed,
|
|
1432
|
+
closed,
|
|
1433
|
+
&OpenConnections::new(None),
|
|
1434
|
+
&ConnectionBudget::new(None),
|
|
1435
|
+
)
|
|
1436
|
+
.expect("an uncapped socket connects");
|
|
1235
1437
|
wait_for(|table| file.poll(table), PollMask::ERR);
|
|
1236
1438
|
let failed = sock.take_connect_outcome();
|
|
1237
1439
|
let kept = sock.getname(false).unwrap().port();
|
|
@@ -1275,8 +1477,8 @@ mod tests {
|
|
|
1275
1477
|
fn a_reset_reads_as_an_error_its_poll_reports() {
|
|
1276
1478
|
let (_sock, mut file, peer) = connected_socket();
|
|
1277
1479
|
file.write(b"unread").unwrap();
|
|
1278
|
-
std::thread::sleep(Duration::from_millis(20));
|
|
1279
1480
|
// A close with bytes unread sends a reset, as Linux's `tcp_close` does.
|
|
1481
|
+
wait_until_arrived(&peer);
|
|
1280
1482
|
drop(peer);
|
|
1281
1483
|
wait_for(|table| file.poll(table), PollMask::ERR);
|
|
1282
1484
|
assert_eq!(
|
|
@@ -1291,7 +1493,7 @@ mod tests {
|
|
|
1291
1493
|
fn reset_socket() -> (Arc<TcpSock>, File) {
|
|
1292
1494
|
let (sock, mut file, peer) = connected_socket();
|
|
1293
1495
|
file.write(b"unread").unwrap();
|
|
1294
|
-
|
|
1496
|
+
wait_until_arrived(&peer);
|
|
1295
1497
|
drop(peer);
|
|
1296
1498
|
wait_for(|table| file.poll(table), PollMask::ERR);
|
|
1297
1499
|
(sock, file)
|
|
@@ -1310,26 +1512,81 @@ mod tests {
|
|
|
1310
1512
|
);
|
|
1311
1513
|
}
|
|
1312
1514
|
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
fn
|
|
1515
|
+
/// A socket file whose peer sent its FIN, which the socket read, then
|
|
1516
|
+
/// reset it, as a close with bytes unread does.
|
|
1517
|
+
fn socket_reset_past_its_end() -> File {
|
|
1316
1518
|
let (_sock, mut file, peer) = connected_socket();
|
|
1317
1519
|
file.write(b"unread").unwrap();
|
|
1318
1520
|
peer.shutdown(Shutdown::Write).unwrap();
|
|
1319
1521
|
wait_for(|table| file.poll(table), PollMask::IN);
|
|
1320
|
-
|
|
1522
|
+
wait_until_arrived(&peer);
|
|
1321
1523
|
drop(peer);
|
|
1322
|
-
|
|
1323
|
-
|
|
1524
|
+
file
|
|
1525
|
+
}
|
|
1526
|
+
|
|
1527
|
+
// @behavior RT-376
|
|
1528
|
+
#[test]
|
|
1529
|
+
fn a_socket_its_peer_ended_reads_the_end_then_a_write_answers_the_reset() {
|
|
1530
|
+
let mut file = socket_reset_past_its_end();
|
|
1531
|
+
let read = file.read(&mut [0; 8]);
|
|
1532
|
+
let started = std::time::Instant::now();
|
|
1533
|
+
let write = loop {
|
|
1534
|
+
match file.write(b"x") {
|
|
1535
|
+
Ok(_) | Err(Errno::WouldBlock)
|
|
1536
|
+
if started.elapsed() < crate::sched::testing::MAX_WAIT =>
|
|
1537
|
+
{
|
|
1538
|
+
std::thread::sleep(Duration::from_millis(1));
|
|
1539
|
+
}
|
|
1540
|
+
answered => break answered,
|
|
1541
|
+
}
|
|
1542
|
+
};
|
|
1543
|
+
assert!(
|
|
1544
|
+
read == Ok(0) && write.is_err(),
|
|
1545
|
+
"a socket whose peer sent its FIN, then reset it, must answer File::read zero ahead \
|
|
1546
|
+
of the failure, as SOCK_DONE stops tcp_recvmsg before sk_err, and a File::write \
|
|
1547
|
+
the failure once the reset arrived; got {read:?} and {write:?}"
|
|
1548
|
+
);
|
|
1549
|
+
}
|
|
1550
|
+
|
|
1551
|
+
// mio reports a closed side only as a best effort, and Windows' AFD
|
|
1552
|
+
// reports nothing past a read side's end, so only the other hosts tell
|
|
1553
|
+
// of the reset before a write.
|
|
1554
|
+
// @behavior RT-376
|
|
1555
|
+
#[cfg(not(windows))]
|
|
1556
|
+
#[test]
|
|
1557
|
+
fn a_socket_reset_past_its_end_polls_err_before_any_write() {
|
|
1558
|
+
let mut file = socket_reset_past_its_end();
|
|
1324
1559
|
wait_for(|table| file.poll(table), PollMask::ERR);
|
|
1325
1560
|
let mut buf = [0; 8];
|
|
1326
1561
|
let (read, write) = (file.read(&mut buf), file.write(b"x"));
|
|
1327
1562
|
assert!(
|
|
1328
1563
|
read == Ok(0) && write.is_err(),
|
|
1329
|
-
"
|
|
1330
|
-
|
|
1331
|
-
|
|
1564
|
+
"a socket whose peer sent its FIN, then reset it, must poll ERR before any write \
|
|
1565
|
+
where the host reports the reset, then answer File::read zero and File::write the \
|
|
1566
|
+
failure; got {read:?} and {write:?}"
|
|
1567
|
+
);
|
|
1568
|
+
}
|
|
1569
|
+
|
|
1570
|
+
// A host may tell of a reset only through its event, its socket holding
|
|
1571
|
+
// no pending error: the hang-up alone ends the socket.
|
|
1572
|
+
// @behavior RT-376
|
|
1573
|
+
#[test]
|
|
1574
|
+
fn a_hang_up_past_the_peers_end_fails_a_socket_its_host_holds_no_error_for() {
|
|
1575
|
+
let (sock, mut file, peer) = connected_socket();
|
|
1576
|
+
peer.shutdown(Shutdown::Write).unwrap();
|
|
1577
|
+
wait_for(|table| file.poll(table), PollMask::IN);
|
|
1578
|
+
sock.on_event(PollMask::HUP);
|
|
1579
|
+
let found = file.poll(None);
|
|
1580
|
+
let mut buf = [0; 8];
|
|
1581
|
+
let (read, write) = (file.read(&mut buf), file.write(b"x"));
|
|
1582
|
+
assert_eq!(
|
|
1583
|
+
(found.contains(PollMask::ERR), read, write),
|
|
1584
|
+
(true, Ok(0), Err(Errno::BrokenPipe)),
|
|
1585
|
+
"a socket whose peer's FIN was read, then an event reported a hang-up its host \
|
|
1586
|
+
socket holds no error for, must poll ERR, read zero, and answer File::write EPIPE, \
|
|
1587
|
+
as tcp_reset sets sk_err in TCP_CLOSE_WAIT"
|
|
1332
1588
|
);
|
|
1589
|
+
drop(peer);
|
|
1333
1590
|
}
|
|
1334
1591
|
|
|
1335
1592
|
// @behavior RT-377
|
|
@@ -1411,8 +1668,8 @@ mod tests {
|
|
|
1411
1668
|
{
|
|
1412
1669
|
let mut state = sock.state();
|
|
1413
1670
|
state.orphan = Some(sock.clone());
|
|
1414
|
-
state.write_queue.
|
|
1415
|
-
state.receive_queue.
|
|
1671
|
+
state.write_queue.push(&[7]);
|
|
1672
|
+
state.receive_queue.push(b"after the close");
|
|
1416
1673
|
}
|
|
1417
1674
|
sock.pump_and_wake(sock.state());
|
|
1418
1675
|
let held = {
|
|
@@ -1457,9 +1714,7 @@ mod tests {
|
|
|
1457
1714
|
// @behavior RT-358
|
|
1458
1715
|
#[test]
|
|
1459
1716
|
fn a_reset_or_refused_socket_polls_hung_up() {
|
|
1460
|
-
let
|
|
1461
|
-
let addr = closed.local_addr().unwrap();
|
|
1462
|
-
drop(closed);
|
|
1717
|
+
let addr = SocketAddr::from(([127, 0, 0, 1], 1));
|
|
1463
1718
|
let refused = sock_alloc_file(dial(addr), &Arc::default());
|
|
1464
1719
|
wait_for(|table| refused.poll(table), PollMask::ERR);
|
|
1465
1720
|
let every = PollMask::IN | PollMask::OUT | PollMask::ERR | PollMask::HUP;
|
|
@@ -1559,10 +1814,7 @@ mod tests {
|
|
|
1559
1814
|
// @behavior RT-345
|
|
1560
1815
|
#[test]
|
|
1561
1816
|
fn a_refused_connect_answers_econnrefused() {
|
|
1562
|
-
let
|
|
1563
|
-
.and_then(|listener| listener.local_addr())
|
|
1564
|
-
.unwrap();
|
|
1565
|
-
let sock = dial(addr);
|
|
1817
|
+
let sock = dial(SocketAddr::from(([127, 0, 0, 1], 1)));
|
|
1566
1818
|
let file = sock_alloc_file(sock.clone(), &Arc::default());
|
|
1567
1819
|
wait_for(|table| file.poll(table), PollMask::OUT);
|
|
1568
1820
|
assert_eq!(
|