Repository navigation
perf(udp): open a new flow's upstream socket in three syscalls, not eight - #1660
Merged
Merged
Conversation
…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
requested review from
Wonshtrum and
llenotre
as code owners
September 28, 2026 14:17
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, setO_NONBLOCKwith anfcntl(F_GETFL)/fcntl(F_SETFL)pair, and then issued anfcntl(F_GETFL), agetsockname(2)and agetpeername(2)whose only consumers aredebug_assert!s. Release builds therefore paid for them too.SOCK_NONBLOCKthrough socket2'sType::nonblocking.Socket::newalready addsSOCK_CLOEXECon Linux (set_common_type), so no.cloexec()is needed. Other platforms (macOS paths exist insocket.rs) keepset_nonblocking, behind#[cfg(not(target_os = "linux"))].#[cfg(debug_assertions)]. Its behaviour in debug builds is unchanged.Measurement
Method: release build (
cargo build --release -p sozu), 1 worker, a UDP listener on127.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 fromsocketto the nextepoll_ctl. Kernel 7.2.7, x86_64.udp_connect+ registration)f5136d67)socket bind fcntl fcntl connect fcntl getsockname getpeername epoll_ctl(ADD): 9 (×5)fcntl15,getsockname5,getpeername5)socket bind connect epoll_ctl(ADD): 4 (×5)recvfrom15,sendto10,epoll_ctl10,close5)intentrace names the two first
fcntlsoperation #3(F_GETFL) and#4(F_SETFL), and the third#3.Test
socket::tests::udp_connect_is_born_nonblocking_and_skips_release_queriesinstalls a seccomp user-notification filter on a dedicated thread only, withoutTSYNC. The filter watchesfcntl,getsocknameandgetpeername. The test thread counts each notification and lets the call through withSECCOMP_USER_NOTIF_FLAG_CONTINUE, soudp_connectstill sees the real kernel answers. The expected counts are[0, 0, 0]in release and[1, 1, 1]in debug. The test then checksO_NONBLOCKandFD_CLOEXECwithfcntlon the returned socket, a non-zero local port andpeer_addr() == backend. It needs Linux ≥ 5.8 on a 64-bit target and fails loudly if seccomp is unavailable.Red/green, run locally:
udp_connectfromorigin/main), debug:left: [3, 1, 1] right: [1, 1, 1], FAILED--release:left: [3, 1, 1] right: [0, 0, 0], FAILEDType::DGRAMwithout.nonblocking()),--release:the socket must be non-blocking, FAILED. In debug, the existingdebug_assert!inudp_connectfires first.--release: okCI runs
cargo testin 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: aperf(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: 0cargo build --locked: 0cargo build --all-features --locked: 0cargo clippy --all-targets --locked -- -D warnings: 0cargo clippy --all-targets --locked --no-default-features --features crypto-ring,opentelemetry,splice,simd -- -D warnings: 0cargo test --workspace --locked -- --skip tests::fuzz_tests:: --test-threads=8: 0. This includes the 12 e2eudp_testsandudp_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: 0python3 .github/scripts/check_doc_citations.py --root .: 0python3 .github/scripts/check_doc_citations.py --root . --base origin/main: 0python3 .github/scripts/check_doc_citations.py --audit --root .: 0RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked: 0Also checked, although not required:
cargo clippy --all-targets --locked --release -- -D warningsexits 101 on errors that already exist onmain. They are unused debug-only items inlib/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 onlyx86_64-unknown-linux-gnuis installed.Signed-off-by: Florentin Dubois [email protected]