Skip to content

perf(udp): open a new flow's upstream socket in three syscalls, not eight - #1660

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
perf/udp-connect-syscalls
Sep 28, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
perf/udp-connect-syscalls

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

What

udp_connect (lib/src/socket.rs) opens the connected upstream socket of every new UDP flow and every UDP health probe. It created the socket blocking, set O_NONBLOCK with an fcntl(F_GETFL)/fcntl(F_SETFL) pair, and then issued an fcntl(F_GETFL), a getsockname(2) and a getpeername(2) whose only consumers are debug_assert!s. Release builds therefore paid for them too.

  • On Linux the socket is created with SOCK_NONBLOCK through socket2's Type::nonblocking. Socket::new already adds SOCK_CLOEXEC on Linux (set_common_type), so no .cloexec() is needed. Other platforms (macOS paths exist in socket.rs) keep set_nonblocking, behind #[cfg(not(target_os = "linux"))].
  • The post-condition block (non-blocking, family, concrete source port, peer pinned to the backend) runs under #[cfg(debug_assertions)]. Its behaviour in debug builds is unchanged.

Measurement

Method: release build (cargo build --release -p sozu), 1 worker, a UDP listener on 127.0.0.1:25353, a cluster with one Python echo backend, responses = 1, affinity_key = SOURCE_IP_PORT, and 5 client flows, each on a new source port. intentrace -p <worker pid> ran on the worker, with the ANSI codes stripped. Counts are keyed on the syscall-name field, and the per-flow sequence is cut from socket to the next epoll_ctl. Kernel 7.2.7, x86_64.

per new flow (udp_connect + registration) whole 5-flow trace
before (f5136d67) socket bind fcntl fcntl connect fcntl getsockname getpeername epoll_ctl(ADD): 9 (×5) 99 syscalls (fcntl 15, getsockname 5, getpeername 5)
after socket bind connect epoll_ctl(ADD): 4 (×5) 74 syscalls (the other families are unchanged: recvfrom 15, sendto 10, epoll_ctl 10, close 5)

intentrace names the two first fcntls operation #3 (F_GETFL) and #4 (F_SETFL), and the third #3.

Test

socket::tests::udp_connect_is_born_nonblocking_and_skips_release_queries installs a seccomp user-notification filter on a dedicated thread only, without TSYNC. The filter watches fcntl, getsockname and getpeername. The test thread counts each notification and lets the call through with SECCOMP_USER_NOTIF_FLAG_CONTINUE, so udp_connect still sees the real kernel answers. The expected counts are [0, 0, 0] in release and [1, 1, 1] in debug. The test then checks O_NONBLOCK and FD_CLOEXEC with fcntl on the returned socket, a non-zero local port and peer_addr() == backend. It needs Linux ≥ 5.8 on a 64-bit target and fails loudly if seccomp is unavailable.

Red/green, run locally:

  • fix reverted (udp_connect from origin/main), debug: left: [3, 1, 1] right: [1, 1, 1], FAILED
  • fix reverted, --release: left: [3, 1, 1] right: [0, 0, 0], FAILED
  • fix kept but the socket born blocking (Type::DGRAM without .nonblocking()), --release: the socket must be non-blocking, FAILED. In debug, the existing debug_assert! in udp_connect fires first.
  • with the fix, debug and --release: ok

CI runs cargo test in debug only, so the [0, 0, 0] release branch was exercised only locally (cargo test -p sozu-lib --lib --release udp_connect_is_born).

Docs

  • lib/src/protocol/udp/LIFECYCLE.md §13: the "new flow" row and the section intro, which said that no UDP scenario had been traced.
  • doc/lifetime_of_a_session.md §10: the per-new-flow bullet.
  • doc/hot_path_zero_copy.md §3.14: the UDP bullet, which said UDP had not been traced beyond §3.10.
  • CHANGELOG.md: a perf(udp) entry under Changed.

This branch was rebased on #1656 (b3f2c787), which introduced those §13/§10 cost descriptions.

Validation (exit codes, on 0f54f6fe)

  • cargo +nightly fmt --all -- --check: 0
  • cargo build --locked: 0
  • cargo build --all-features --locked: 0
  • cargo clippy --all-targets --locked -- -D warnings: 0
  • cargo clippy --all-targets --locked --no-default-features --features crypto-ring,opentelemetry,splice,simd -- -D warnings: 0
  • cargo test --workspace --locked -- --skip tests::fuzz_tests:: --test-threads=8: 0. This includes the 12 e2e udp_tests and udp_health_descriptor_exhaustion.
  • RUSTFLAGS="--cfg tokio_unstable" cargo test -p sozu-sim --locked --test udp_simulation: 0 (6 passed)
  • python3 .github/scripts/check_doc_citations.py --self-test: 0
  • python3 .github/scripts/check_doc_citations.py --root .: 0
  • python3 .github/scripts/check_doc_citations.py --root . --base origin/main: 0
  • python3 .github/scripts/check_doc_citations.py --audit --root .: 0
  • RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked: 0

Also checked, although not required: cargo clippy --all-targets --locked --release -- -D warnings exits 101 on errors that already exist on main. They are unused debug-only items in lib/src/protocol/mux/{mod,router,debug,h2_header_reassembly,h2_stream_table}.rs, and none of them is in a file this PR touches.

Not verified: the non-Linux path (Type::DGRAM + set_nonblocking) was not compiled here, because only x86_64-unknown-linux-gnu is installed.

Signed-off-by: Florentin Dubois [email protected]

…ight

udp_connect created the per-flow connected UDP socket blocking, set
O_NONBLOCK with an fcntl(F_GETFL)/fcntl(F_SETFL) pair, then issued an
fcntl(F_GETFL), a getsockname(2) and a getpeername(2) whose only consumers
are debug_assert!s, so release builds paid for them on every new flow and
every UDP health probe.

On Linux the socket is now created with SOCK_NONBLOCK through socket2's
Type::nonblocking (Socket::new already adds SOCK_CLOEXEC there), and the
post-condition queries run only under cfg(debug_assertions), unchanged in
debug builds. Other platforms keep set_nonblocking.

Measured with intentrace -p on the worker, release build, one worker,
Python echo backend, five client flows, per new flow:
  before: socket bind fcntl fcntl connect fcntl getsockname getpeername
          epoll_ctl(ADD)  (9)
  after:  socket bind connect epoll_ctl(ADD)  (4)

udp_connect_is_born_nonblocking_and_skips_release_queries counts the
fcntl/getsockname/getpeername calls of one udp_connect with a seccomp
user-notification filter installed on a dedicated thread ([0, 0, 0] in
release, [1, 1, 1] in debug) and checks O_NONBLOCK and FD_CLOEXEC on the
returned socket. It fails on origin/main in both profiles with [3, 1, 1].

Update the new-flow cost in udp/LIFECYCLE.md §13, in
lifetime_of_a_session.md §10 and in hot_path_zero_copy.md §3.14, which
described the old sequence, and add the CHANGELOG entry.

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 4857123 into main Sep 28, 2026
28 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the perf/udp-connect-syscalls branch September 28, 2026 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant