Skip to content

feat: bundle ssh socketpair wire and opt-in SEND_ZC - #8183

Merged
oferchen merged 12 commits into
masterfrom
feat/ssh-zero-copy-bundle
Oct 14, 2026
Merged

oferchen merged 12 commits into
masterfrom
feat/ssh-zero-copy-bundle

Conversation

@oferchen

@oferchen oferchen commented Oct 13, 2026 •

Copy link
Copy Markdown
Owner

Bundles three changes to the ssh and socket write paths.

ssh wire over socketpairs

The spawned ssh child's stdin and stdout are now two AF_UNIX SOCK_STREAM socketpairs, one per direction, as upstream creates them (pipe.c:57 piped_child(), util1.c:84 fd_pair()). If the kernel refuses a socketpair, the spawn falls back to anonymous pipes and traces the fallback at --debug=connect. Windows uses pipes. By default the wire uses plain read/write, because io_uring showed no gain there.

Embedded ssh bridge

Removes the per-chunk Vec copies in the embedded ssh bridge.

Zero-copy is a local, per-node opt-in

Zero-copy (io_uring SEND_ZC) is off by default. It applies only on the node where it is requested and is never forwarded to the peer.

  • The client's --zero-copy / --no-zero-copy affect only its own sockets. They never appear in the remote argv or the -e string.
  • A server opts in on its own side, in any of three ways:
    • oc-rsync --daemon --zero-copy
    • OC_RSYNC_ZERO_COPY=on|off|auto, read by the daemon and by --server
    • --rsync-path='oc-rsync --zero-copy'
  • Precedence is flag, then environment variable, then the default (off).
  • The daemon policy is stamped on each module and re-applied on config reload.
  • Behaviour change: a client --zero-copy sent to an oc daemon is now ignored rather than applied.

Tests

  • Golden argv tests pin that zero-copy never reaches the ssh or daemon argv, for either role or policy.
  • Precedence tests cover flag, environment variable and default, each with a mutation check.
  • A daemon started with --zero-copy raises the send_zc counters; one started without it leaves them unchanged. This was measured on Linux 7.2, where SEND_ZC really runs.
  • Interop with rsync 3.5.1 over rsh and daemon, in both directions, is byte-identical with no unknown-option errors.

Not covered here

  • The --server stdio path and client daemon push have no SEND_ZC writer. SEND_ZC also cannot run on the pipe or AF_UNIX stdout that sshd provides.
  • crates/transfer/src/lib.rs:895 scans the whole compact flag string, including the -e payload, for z. This is a latent bug and is tracked separately.

@github-actions github-actions Bot added the enhancement New feature or request label Oct 13, 2026
@oferchen
oferchen force-pushed the feat/ssh-zero-copy-bundle branch from b3f84ed to 0d225be Compare October 13, 2026 23:48
The remote-shell child's stdin and stdout are now two AF_UNIX
SOCK_STREAM socketpairs, one per direction, as upstream pipe.c:57
piped_child() creates them through util1.c:84 fd_pair(). The child's
stdout is left non-blocking unless --blocking-io (or rsh/remsh), as in
pipe.c:80-82. If either socketpair cannot be created the spawn falls
back to anonymous pipes. The choice and any fallback reason are traced
at --debug=connect and exposed as SshConnection::wire_primitive().
Windows keeps pipes.

On Linux the split halves drive the socket ends through the fast_io
io_uring RECV/SEND paths under --io-uring/--no-io-uring, falling back
to read/write. With --zero-copy the writer probes the socket once at
setup with a zero-length IORING_OP_SEND_ZC; AF_UNIX refuses it with
EOPNOTSUPP and the writer settles on plain SEND, traced at
--debug=iouring.

Wiring the io_uring writer to a stream socket exposed two fast_io
defects, fixed here:
- submit_send_batch kept several SENDs in flight on one stream socket.
  A short completion followed by a later chunk reordered the stream
  (a 2 MiB AF_UNIX write corrupted at offset 262144). Sends are now
  serial, one SQE in flight, like a write(2) loop.
- the socket reader and SEND_ZC drain treated an interrupted
  io_uring_enter as fatal or as a missing CQE; they now resume the
  wait instead of resubmitting.

The two socketpair audits are marked superseded.
The embedded (russh) transport moved every inbound SSH data chunk
through `data.to_vec()` and every outbound write through `buf.to_vec()`,
one fresh allocation and copy per chunk in each direction.

- Inbound chunks now cross the bridge queue as the `bytes::Bytes` russh
  decoded, so the only copy left is the one into the caller's buffer.
- Outbound writes are copied once into a buffer from a bounded
  `ChunkPool`. The chunk handed to russh owns that buffer and returns it
  to the pool when russh drops it, so steady-state writes reuse a few
  allocations. The pool keeps at most one idle buffer per queue slot and
  frees anything above 64 KiB.

Applied to the production bridge in `connect.rs` (`ChannelReader` /
`ChannelWriter`, used by `connect_and_exec`) and to the exported
`sync_bridge` halves. Queue bounds, blocking backpressure, the goodbye
timeout, EOF and `BrokenPipe` behaviour are unchanged.
SEND_ZC default-on measured +44% wall time on a 2 GiB daemon pull, so
the default policy no longer dispatches it. send_zc_policy_permits(Auto)
is now false on every build; only an explicit --zero-copy (Enabled)
reaches the io_uring writer. The iouring-send-zc feature stays in the
default set so --zero-copy works without a rebuild.

The default-path tests now assert the SEND_ZC counters stay untouched,
and new explicit --zero-copy tests assert SEND_ZC sends are counted where
the kernel can run them, with the existing environment classification
elsewhere.
io_uring RECV/SEND on the AF_UNIX remote-shell wire measured no gain
over read/write, so the wire now engages io_uring only under an explicit
--io-uring (IoUringPolicy::Enabled). Auto, SqpollOff and Disabled keep
plain syscalls; --io-uring on a host without io_uring, including one with
OC_RSYNC_DISABLE_IOURING set, still fails loudly.
Zero-copy is a local decision: a client's --zero-copy / --no-zero-copy
covers only its own I/O and is never forwarded, so no argv token or -e
capability letter changes and an upstream peer never sees it.

Servers opt in on their own:
- oc-rsync --daemon --zero-copy / --no-zero-copy sets the daemon-wide
  policy, stamped onto every module and re-applied on SIGHUP reload.
- an SSH server takes --zero-copy via --rsync-path='oc-rsync --zero-copy'.
- OC_RSYNC_ZERO_COPY=on|off|auto is read by the daemon at startup and by
  --server. An explicit flag beats the variable, which beats the default
  (off).

The daemon no longer honours a client-supplied --zero-copy in the
session argv; like any unknown long option it is ignored.
@oferchen
oferchen force-pushed the feat/ssh-zero-copy-bundle branch from 0d225be to b729aeb Compare October 13, 2026 23:53
The pull whine test drove its cut through `dd bs=1`, which cannot
retry EAGAIN. Over the socketpair wire the client leaves the remote
shell's stdout non-blocking unless --blocking-io (upstream
pipe.c:80-82), so a slow reader made dd die after a few hundred bytes.
Both cut tests now pass --blocking-io, as a remote shell that cannot
handle a non-blocking stdout needs with upstream too.

The blocking-mode test read the shell's fdinfo from `grep ... >&2`.
dash applies a command's redirections in the shell before forking, so
it measured stderr instead of the socket. The flags are now captured
first and printed afterwards.
@oferchen
oferchen merged commit 565ec42 into master Oct 14, 2026
89 checks passed
@oferchen
oferchen deleted the feat/ssh-zero-copy-bundle branch October 14, 2026 07:29
mevanlc pushed a commit to mevanlc/rsync-rs that referenced this pull request Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant