bryancall opened a new pull request, #14035:
URL: https://github.com/apache/trafficserver/pull/14035

   With the OpenSSL QUIC backend, all HTTP/3 traffic on a port runs on one net 
thread, however many net threads there are. Every connection an OpenSSL 
listener accepts shares that listener's reactor: the listener's 
`SSL_handle_events()` receives the connection's datagrams and runs its timers, 
so the connection has to be served on the listener's thread. TCP can accept on 
one thread and hand a connection to another, but QUIC cannot. One listener per 
port therefore means one thread per port.
   
   This is a design change, so it is opened as a draft for discussion.
   
   ### What runs where
   
   - On Linux and FreeBSD, `QUICNetProcessor::main_accept()` makes one 
`QUICPacketHandlerIn` per ET_NET thread (the original plus `clone()`s, sharing 
one `NetAcceptAction`). Each acceptor has its own UDP socket, bound to the port 
with `SO_REUSEPORT` (and `SO_REUSEPORT_LB` where it exists), its own OpenSSL 
listener, and its own closed-connection collector. Other platforms keep one 
listener per port and log a note, because the per-thread design relies on the 
kernel balancing a shared UDP port, which only Linux `SO_REUSEPORT` and FreeBSD 
`SO_REUSEPORT_LB` are documented to do.
   - The kernel picks a socket for each datagram by hashing the source and 
destination addresses and ports. All of a connection's datagrams reach one 
listener. That listener accepts the connection on its own thread, and the 
connection stays there.
   - All sockets are bound, and all listeners started, on the calling thread 
before any acceptor is scheduled. The hash is taken over the sockets bound at 
that moment, so a group that was still growing while connections were set up 
would move some of those connections' later datagrams to other sockets.
   - If the first socket cannot be made to share the port, the port falls back 
to one listener with a warning. If a later listener fails to start, it is 
closed, the listeners already started serve the port between them, and a 
warning says how many net threads accept. If none starts, on either setting, an 
error names the port and its address, and its socket is closed. Every listener 
error names the address and, for per-thread listeners, the net thread.
   - `proxy.config.quic.server.listen_per_thread` (default `1`, restart 
required) set to `0` keeps today's single listener. A port with an inherited 
`fd=` descriptor keeps its single socket. quiche ignores the setting.
   
   ### The session acceptor mutex
   
   Today `init_accept()` lends the session acceptor (`QUICNextProtocolAccept`) 
its listener thread's mutex when the acceptor has none. Per-thread acceptors 
must not do that, and they leave it unset:
   
   - `Continuation::handleEvent()` release-asserts that the caller holds the 
continuation's mutex, and every EThread holds its own mutex for its whole life. 
If the session acceptor borrowed thread 0's mutex, the first connection 
accepted on any other thread would abort when 
`QUICNetVConnection::acceptEvent()` calls 
`action_.continuation->handleEvent(NET_EVENT_ACCEPT, ...)`.
   - TCP per-thread accept (`NetAccept::init_accept_per_thread()`) also leaves 
it unset, and TCP's accept-thread mode already calls its session acceptors from 
every net thread.
   - Without a mutex, nothing serializes these calls. That is safe because the 
QUIC session acceptors hold only state fixed at startup, and the IP-allow 
configuration they read is a reference-counted snapshot. 
`QUICNextProtocolAccept::mainEvent()` only hands the connection the protocol 
set fixed when the port was opened. `Http3SessionAccept` (already constructed 
with a null mutex) reads its `const` options, takes a reference-counted 
IP-allow snapshot and creates a new session per connection.
   
   Other state the acceptors share: the default `SSL_CTX` (OpenSSL allows 
concurrent use by many `SSL` objects; session ticket keys live there, so 
resumption works across listeners, as `test_h3_session_ticket` exercises), the 
`NetAcceptAction` (reference-counted, read-only after setup), and config 
snapshots taken through `scoped_config`. The peer-address capture in 
`OpenSSLQUICPacketHandler.cc` is `thread_local`, and each listener's BIO is 
only read on its own thread. `SSL_CTX_set_new_pending_conn_cb()` is still 
called once per listener, now on the calling thread before that port's 
listeners run. With more than one QUIC port, a later port's call rewrites the 
same pointer while an earlier port's listeners are running. That happens today 
with two QUIC ports as well, and this change does not make it worse.
   
   ### Address changes, NAT rebinding and retry tokens
   
   OpenSSL 3.5's QUIC server does not support connection migration with either 
setting:
   
   - It always sends `disable_active_migration` in its transport parameters, so 
clients are told not to migrate.
   - It sets a connection's send address once, when it accepts the connection, 
and never updates it. A client whose address or port changes during a 
connection (NAT rebinding) keeps getting replies at its old address, so the 
connection already fails on master and times out.
   
   With per-thread listeners, a rebound client's datagrams usually hash to a 
different socket. That listener has no connection for the destination 
connection ID. OpenSSL drops short-header packets it cannot match, and it sends 
no stateless reset (3.5 cannot send one, and it advertises no reset token). The 
client still loses the connection. The difference is that the server stops 
hearing from it and frees the connection at its idle timeout, instead of 
hearing it and answering an address it can no longer reach. This change does 
not make rebinding worse. A real fix would need connection IDs that name their 
listener plus a BPF `SO_ATTACH_REUSEPORT_EBPF` program that steers by them. 
OpenSSL 3.5 has no hook for choosing server connection IDs, so that is not 
possible yet. Migration support in OpenSSL itself would need the same.
   
   Retry and address-validation tokens: each OpenSSL listener encrypts its 
tokens with its own random key.
   - A Retry token comes back in the client's second Initial, sent from the 
same address and port, which reaches the same socket. The new test runs 32 
connections with `stateless_retry_enabled: 1` and checks that every one of them 
was retried and completed.
   - A NEW_TOKEN token used on a later connection from a different source port 
usually reaches another listener, which cannot decrypt it. With stateless retry 
on, that listener answers with a Retry, which costs one extra round trip. With 
it off, the handshake proceeds without the token, as it does now after a 
restart, since the keys are random per process.
   
   ### Tests
   
   `tests/uranium_tests/h3/test_h3_listener_threads.py` runs 4 net threads and 
an aioquic client that opens 32 HTTP/3 connections at once, each from its own 
UDP port, with 4 requests per connection. The client reuses the request and 
response checks from `h3_client.py`. Every test checks that every request 
completes, that the `http3` debug line `accepted connection from` appears 
exactly once per connection, that each comes from one of `[ET_NET 0]` to 
`[ET_NET 3]`, and that no listener reported a failure to start:
   
   - Default configuration: more than one thread accepts, and the 
`openssl_quic` debug line says `accepting OpenSSL QUIC connections on 4 of 4 
net threads`. A second round of 32 connections after the first round's 
connections are collected is served too, all four threads accept across the two 
rounds, and Traffic Server is still running. The chance that all 32 connections 
of a round hash to one of the 4 sockets is 4^-31, about one in 5 x 10^18, and 
the chance that the 64 connections of both rounds miss one of the sockets is 
about 4 x 10^-8.
   - `listen_per_thread: 0`: exactly one thread accepts, and there is no 
per-thread summary line.
   - `stateless_retry_enabled` 0 and 1: all four listeners start, more than one 
thread accepts, and the client reports a Retry on no connection or on all 32 of 
them, respectively.
   - No listener, with `listen_per_thread` 0 and 1: a plain UDP socket (no 
`SO_REUSEADDR` or `SO_REUSEPORT`) holds the port on 0.0.0.0 before Traffic 
Server starts, so the first QUIC socket's bind fails with `EADDRINUSE` on Linux 
and FreeBSD. The test expects that bind failure for the port, the error `HTTP/3 
port N: no OpenSSL QUIC listener started on 0.0.0.0:N`, and no fallback or 
partial-start warning.
   
   On master (d10662601), the three tests that need more than one thread fail 
with every connection on one thread (`{'ET_NET 2'}`), and the single-listener 
test passes. Both no-listener cases fail waiting for the port-level error: 
master logs the bind failure (`failed to bind OpenSSL QUIC socket: Address 
already in use`) but nothing for the port. Two checks that master never reaches 
were each shown to fail on a deliberately broken build. The retry count check 
fails against a client that reports 10 retried connections where 0 are 
expected, which a substring match would have let through. The every-thread 
check fails against a build that runs the fourth listener on net thread 0: the 
first round still spreads and logs `4 of 4`, but `ET_NET 3` never accepts. With 
this change all six pass. There are no timing thresholds; the test sleeps 0.5 s 
before the second round so the collectors, which run every 100 ms, can free the 
first round.
   
   The h3 and qmux Uranium suites pass under ASAN (17 passed; 2 skipped for a 
missing Go version and missing QMux). There are no AddressSanitizer errors and 
no runtime errors. Leak reports: `Http3ProtocolEnforcer`, allocated in 
`Http3App`'s constructor and never deleted, which master shows in the same 
tests. The listener-thread tests also report SNI name buffers from 
`QUICNetVConnection::init()` for connections still open when Traffic Server 
stops. That predates this change. Two rounds of 32 connections followed by a 
stop leak 31 or 32 of them on master in each of 7 runs, and 19 to 32 with this 
change: 20, 28, 19 and 26 with per-thread listeners, 31 or 32 with 
`listen_per_thread: 0`. Waiting 2 s before the stop does not change that.
   
   The no-listener path has the test above. The other startup fallbacks (a 
first socket that cannot share the port, a partial start, a platform without 
load balancing) have no automated test: on Linux nothing short of a seccomp 
filter makes `SO_REUSEPORT` fail on one socket but not the next.
   
   ### Measurements
   
   h2load from a separate client host, tuned config with 32 net threads. 1 KB 
cells use 64 connections x 8 streams, and the 1 MB cell uses 32 x 2. All builds 
are Release on master 8bbb06314:
   - A = master
   - B = A + this change
   - C = B + the stream-free change (#14027)
   
   Arms were interleaved A, B, C per round, with 3 runs per cell per round; the 
table shows the median of each round's 3-run medians. Rounds 2 and 3 are shown 
for B and C; their round 1 overlapped unrelated load on the proxy host and is 
excluded. A has three clean rounds, and its spread across rounds is the A/A 
noise.
   
   | cell | A master | B per-thread | C per-thread + stream free | A/A spread |
   |---|---|---|---|---|
   | h3, 1 KB to origin, req/s | 5,898 (5,972 / 5,837 / 5,898) | 44,793 (44,371 
/ 45,215), 7.6x | 117,303 (118,371 / 116,236), 19.9x | 2.3% |
   | h3, 1 KB cache hit, req/s | 8,104 (8,304 / 8,080 / 8,104) | 73,760 (74,057 
/ 73,462), 9.1x | 173,202 (173,289 / 173,115), 21.4x | 2.8% |
   | h3, 1 MB to origin, Gbps body | 0.37 (0.37 / 0.32 / 0.38) | 0.48 (0.47 / 
0.48) | 0.48 (0.48 / 0.48) | 0.32-0.38 |
   | control: h2, 1 KB to origin, req/s | 863,328 (865,670 / 863,328 / 863,092) 
| 847,660 (845,814 / 849,505), -1.8% | 849,086 (849,499 / 848,672), -1.6% | 
0.3% |
   
   Per-run values (req/s, or Gbps for 1 MB):
   - A h3 1 KB origin: 5837, 5994, 5972 / 5837, 5810, 5854 / 5928, 5898, 5846
   - B: 44322, 44371, 47224 / 45577, 45215, 44983
   - C: 121581, 113203, 118371 / 132542, 116045, 116236
   - A h3 1 KB cache: 7702, 8314, 8304 / 7613, 8080, 8196 / 8104, 7865, 8213
   - B: 74057, 74944, 73199 / 71593, 74718, 73462
   - C: 173289, 162387, 174835 / 173159, 173115, 162465
   - A h3 1 MB: 0.38, 0.32, 0.37 / 0.38, 0.32, 0.32 / 0.38, 0.38, 0.38
   - B: 0.47, 0.47, 0.48 / 0.48, 0.48, 0.48
   - C: 0.49, 0.48, 0.47 / 0.49, 0.47, 0.48
   - A h2 control: 864726, 865670, 869342 / 864086, 863328, 863048 / 862320, 
863092, 864723
   - B: 845814, 841451, 846350 / 847186, 853774, 849505
   - C: 848880, 849499, 849850 / 845189, 849675, 848672
   
   Proxy-host CPU (whole machine, median of runs):
   
   | cell | A | B | C |
   |---|---|---|---|
   | h3 1 KB origin | 3.5% | 35-36% | 28% |
   | h3 1 KB cache | 3.4% | 64% | 29% |
   | h3 1 MB | 1.5% | 2.1% | 2.1-2.2% |
   | h2 control | 99% | 99.7% | 99.7% |
   
   Idle, benchmark config (32 net threads): `traffic_server` used 0.007 cores 
with A, 0.028 with B and 0.024 with C. Rebased onto master with #14027, two 
rounds each: 0.005 and 0.006 cores with master, 0.029 and 0.027 with this 
change at 32 net threads; with 4 net threads, 0.004 and 0.004 with master, 
0.006 and 0.007 with this change. Each per-thread listener still wakes every 2 
ms when idle.
   
   How to read it:
   - Spreading QUIC over the net threads is most of the 1 KB gain. In the same 
cell, B costs more CPU per request than A (about 0.79% versus 0.59% of the 
machine per 1k req/s for 1 KB to origin). That is because a connection still 
walks every stream it has ever served on each event. The stream-free change 
removes that walk. With both, C uses about 0.24% per 1k req/s, which is why the 
two changes multiply.
   - C is not CPU-bound (28%), so its ceiling here is something other than 
proxy CPU.
   - 1 MB is limited elsewhere (2% CPU): per-thread listeners help it only a 
little, and the stream-free change does not move it.
   - The h2 control cell costs about 2% with per-thread listeners on, and the 
cost comes from the listeners, not from the build. On the rebased builds, in 
two interleaved rounds (medians of 3 runs), the same binary with 
`listen_per_thread: 0` matches master: 858,834 and 862,560 req/s against 
master's 854,945 and 859,789, +0.5% and +0.3%, inside master's 0.6% spread 
between rounds. Turning the setting on drops it to 840,966 and 841,420 req/s: 
-2.1% and -2.5% against the same binary with it off, -1.6% and -2.1% against 
master. That cell runs the proxy host at 99% CPU. The likeliest cause is the 2 
ms wake-up of each of the 32 listeners, which an event-driven wake-up would 
remove, but that has not been measured. With the setting off, the h3 1 KB to 
origin cell is back at the single-listener rate (6,047 req/s).
   
   ### Idle wake-ups
   
   Each per-thread listener is still serviced every 2 ms, as the single 
listener is today, which is the idle cost noted above. #14021 ("HTTP/3: stop 
polling idle QUIC connections for writes") worked on the same packet handler 
and was closed without merging. If that idea comes back, it composes with this 
change: each per-thread listener would track its own connection set, inserted 
and erased on that listener's thread, so the set needs no locking. A test 
cherry-pick of this change onto #14021 had one trivial conflict (both add a 
member after `_event` in `P_QUICPacketHandler.h`) and passed the h3 and qmux 
suites plus the new tests under ASAN. Making the wake-up event-driven (socket 
readiness plus `SSL_get_event_timeout()`) would remove the idle cost and is a 
natural follow-up.
   
   ### Open questions for review
   
   1. Default on? Today a port's HTTP/3 traffic is capped at one thread, and 
TCP's settings don't fit QUIC: `accept_threads` hands connections to other 
threads, and `exec_thread.listen=0` shares one socket. So a separate setting 
that defaults to on seemed right. Is the name 
`proxy.config.quic.server.listen_per_thread` good, or should it be tied to 
`exec_thread.listen` instead?
   2. One listener per ET_NET thread, or a separate count? With many net 
threads, every listener also costs a 2 ms idle tick until the wake-up is 
event-driven.
   3. Are you OK with documenting rebinding as unsupported until OpenSSL can 
migrate connections or let us choose connection IDs, or is BPF steering wanted 
sooner?
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to