Repository navigation
device: limit staged packets when pools are bounded - #111
Closed
SunflowersLwtech wants to merge 1 commit into
Closed
SunflowersLwtech wants to merge 1 commit into
SunflowersLwtech wants to merge 1 commit into
Conversation
With WithPreallocatedBuffersPerPool, the TUN readers and the receive path take packet buffers from the same bounded pools, and a packet staged for a peer without a session holds its buffer until a handshake completes or gives up. A peer that keeps getting traffic but cannot complete a handshake fills its staged queue, which with the iOS settings (64 buffers, a staged queue of 64) takes every buffer. The TUN readers and the receive goroutines then block in WaitPool.Get, which stops traffic to and from all peers, including the receive of a handshake message that would release the staged packets. The stall ends when handshake attempts give up and flush the staged queue, about 100s later. When the unreachable peer is an exit node, traffic keeps coming both ways, and in tests the give-up stopped happening: the stall outlasted the outage by minutes, or did not end within 10 minutes of the exit node being reachable again. When pools are bounded, limit the packets staged across all peers to half of the pool size. A TUN reader that stages past the limit drops the oldest staged batches of the peers with the most staged, so that a peer that is still waiting for its first handshake keeps its packet. Devices with unbounded pools keep no count and are unaffected. Updates tailscale/tailscale#21773 Signed-off-by: LiuWei <[email protected]>
bradfitz
approved these changes
Nov 6, 2026
Member
|
Thanks! This looks good to me (minimal, nice tests) and seems like a reasonable short-term fix that doesn’t get in the way of the future buffer management rework. Objections, @tailscale/dataplane, @raggi? |
Member
|
This should now be addressed by #128 @SunflowersLwtech, can you confirm? |
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.
Updates tailscale/tailscale#21773
With
WithPreallocatedBuffersPerPool(the iOS settings inwgengine/wgcfg/mem_ios.go: 64 buffers, staged queue of 64), the TUN readers and the receive goroutines take packet buffers from the same bounded pool, and a packet staged for a peer without a session keeps its buffer until a handshake completes or gives up. A peer that keeps getting traffic but cannot complete a handshake fills its staged queue and takes every buffer. The TUN reader and the receive goroutines then wait inWaitPool.Get, so nothing moves to or from any peer, including the handshake message that would release the staged packets.This change limits the packets staged across all peers to half of the pool size when the pool is bounded. After each batch, a TUN reader that finds more than that staged drops the oldest staged batches of the peers with the most staged packets, so a peer that is still waiting for its first handshake keeps its packet. The count is kept only when the pool is bounded; devices with unbounded pools skip it.
Why not the other fixes listed in the issue
handshakeAttemptson rate-limited sends would end the stall at the 90 s give-up instead of preventing it, and in the exit node runs below the count stopped moving while the device was stalled, so the give-up did not come at all. It would also diverge from the kernel, which resets the count the same way.tstun.Wrapper.Read) would still stop.Tests
device/staged_test.goadds an in-memory network (memNet) that can cut a link and add latency, so the tests run in atesting/synctestbubble and cover minutes of handshake timers in well under a second.TestStagedPacketsBoundedPool: one unreachable peer, and eight with a staged queue of 16. Sends twice the pool size to each, then checks that a healthy peer is reachable both ways and that a peer needing its first handshake gets its packet through.TestStagedPacketsExitNode: the worst case. The unreachable peer is an exit node (0.0.0.0/0) over a link with 100 ms one-way latency, the device sends 100 packets/s through it and gets 50 packets/s back. The link is cut for 210 s (pastRejectAfterTime), then restored. Checks that a healthy peer stays reachable every second, and that traffic with the exit node resumes within 12 s.TestStagedPacketsCount,TestStagedPacketsCountConcurrent: the staged count against the queues, through trimming, per-peer drops of the oldest batch, keepalives, handshake give-up, peer removal, and four TUN readers at once.Test teardown flushes staged packets before
Device.Close, because on the unfixed codeClose(andRemovePeer) can hang in this state:BindClosewaits for receive goroutines that are waiting for a buffer, andpeer.Stopwaits for a keepalive timer callback that is waiting for an outbound element.Without the fix
origin/tailscale(9d89615) plusstaged_test.gowithout the two count tests, which read the new counter:With a staged queue of 16 per peer instead of this change (same unfixed tree, extra rows added to the table):
With the fix
go test -race ./...,go run ./cmd/check-lockorderand the CI cross-build matrix pass. The lock-order checker reports 252 lock-after observations against 250 before, and no edge from the base is lost.Mutations
Each row breaks one part of the change and runs
go test -race -count=3 -run TestStagedPackets ./device:SendStagedPacketstakes a batchStagePacketsFlushStagedPacketstrimStagedPacketslen(elems.elems)after the send)M10 is a data race rather than a logic error, so it is caught only under
-race, and not in every run; the count is taken before the batch is handed to the queue for that reason.How long the exit node case lasts without the fix
A variant of
TestStagedPacketsExitNode(not included) that, after the link comes back, waits up to 10 minutes and records in 10 s steps whether traffic reaches the exit node again:An earlier version of the test, without latency and with a 10 minute deadline, recovered once after 1m11.6s. The three 9m10s results match the
zeroKeyMaterialtimer (3 ×RejectAfterTime, which flushes the staged queue) if it was armed by a handshake right after the link came back; that is not confirmed. In the four stalled runs of the first sweep the handshake attempt count stayed at 7 for the whole 10 minutes, and in another run it went to 0 after one give-up and stayed there, so the 90 s give-up that ends the stall in the original report stopped happening. I have not traced why the retransmit timer stops; this change does not depend on it.End to end with tailscaled
Three
tailscaledprocesses on one Linux host in userspace-networking mode, with thetstest/integrationtest control server and DERP server: a "phone", an exit node (0.0.0.0/0 approved withtestcontrol.Server.SetSubnetRoutes) and a healthy peer. All three are built from tailscalemainwithgo.modreplacing wireguard-go with either 9d89615 or this branch, plus a local patch (not proposed anywhere) that makes the phone use themem_ios.gooptions and a batch size of 1 on Linux when an environment variable is set.With that batch size, magicsock's Linux UDP receive funcs exit at startup (
len(packets): 1 < MinimumReadBatchSize(64)), so the phone used DERP only, as an iPhone away from home often does. That is also why both runs report "The MagicSock function ReceiveIPv4 is not running"; it has nothing to do with this change and is left out below.The phone keeps 64 connection attempts to 198.51.100.0/24 going through its SOCKS5 proxy (about 20 per second, all through the exit node) and, once a second, sends a TSMP ping with a 1 s timeout to the exit node and to the healthy peer. The exit node is frozen with SIGSTOP for 200 s, resumed with SIGCONT, and the phone is observed for 300 s more.
(The counts are probes, not seconds: on 9d89615 each probe took about 3 s because both pings timed out.)
Goroutines of the 9d89615 phone during the stall, from its
/debug/pprof/goroutine?debug=2, trimmed:Goroutine 105 is the DERP receive goroutine, the only one left. On this branch at the same point, the TUN reader is in
selectand the DERP receive goroutine is inmagicsock.(*connBind).receiveDERP.Cost
Unbounded pools: a branch on a field that does not change after
NewDevice, at each place a batch enters or leaves a staged queue. Bounded pools: an atomic add at those places and an atomic load per TUN read; the peer scan intrimStagedPacketsruns only when the limit is exceeded, which takes a peer that cannot complete a handshake.