Repository navigation
tcp: Use SYN cookies after shrinking a listener's backlog. - #16976
Open
copybara-service[bot] wants to merge 1 commit into
Open
copybara-service[bot] wants to merge 1 commit into
copybara-service[bot] wants to merge 1 commit into
Conversation
Calling `Listen` again on a listening endpoint can reduce the accept queue capacity while preserving existing pending handshakes. The resize check only considers completed connections in the accept queue, so the pending count can exceed the new SYN backlog of `capacity - 1`. The SYN cookie guard currently checks only for equality. After a reduction below the pending count, new SYNs bypass this guard and allocate additional pending handshake state instead of using SYN cookies. Change the guard to `>=`. The `capacity - 1` relationship accounts for the extra accept queue slot added by the syscall layer; it does not imply that existing pending handshakes must always fit within a reduced backlog. The limit controls admission of new pending handshakes. When the completed accept queue has space, use SYN cookies whenever the pending count is at or above that limit. Keep the existing `Listen` resize policy and completed-queue checks unchanged. Enforcing `pending <= capacity - 1` in `Listen` would instead require rejecting otherwise permitted reductions, retaining a larger capacity than requested, or aborting existing handshakes. None of those behavior changes is needed for this fix. Do not move or duplicate the syscall layer's `+1` adjustment. This matches the queue-full comparison in the Linux revision referenced by the existing comment: `inet_csk_reqsk_queue_is_full()` uses `>=`, while `inet_listen()` updates the backlog of an already-listening socket without rejecting the update based on the pending request count. This is a comparison of these specific code paths, not a claim of complete `Listen` compatibility. Update the guard's comment to explain both `capacity - 1` and why the pending count may already exceed the limit. Add `TestListenerShrinkingBacklogUsesCookies`: create two pending handshakes with `Endpoint.Listen(4)`, reduce the capacity with `Endpoint.Listen(2)`, and verify that the next SYN uses a cookie without adding a pending endpoint. Verify that the cookie ACK still produces an accept-ready connection with the expected peer. Use a manual stack clock, port and ACK matching, and listener-mutex synchronization to keep state and statistics checks deterministic with respect to handshake timers and segment processing. Linux references: [backlog update](https://github.com/torvalds/linux/blob/7acac4b3196/net/ipv4/af_inet.c) and [pending queue-full comparison](https://github.com/torvalds/linux/blob/7acac4b3196/include/net/inet_connection_sock.h#L280). Assisted-by: ChatGPT FUTURE_COPYBARA_INTEGRATE_REVIEW=#15038 from 134130:134130/fix-listener-backlog 2269c07 PiperOrigin-RevId: 1013865462
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.
tcp: Use SYN cookies after shrinking a listener's backlog.
Calling
Listenagain on a listening endpoint can reduce the accept queue capacity while preserving existing pending handshakes. The resize check only considers completed connections in the accept queue, so the pending count can exceed the new SYN backlog ofcapacity - 1.The SYN cookie guard currently checks only for equality. After a reduction below the pending count, new SYNs bypass this guard and allocate additional pending handshake state instead of using SYN cookies.
Change the guard to
>=. Thecapacity - 1relationship accounts for the extra accept queue slot added by the syscall layer; it does not imply that existing pending handshakes must always fit within a reduced backlog. The limit controls admission of new pending handshakes. When the completed accept queue has space, use SYN cookies whenever the pending count is at or above that limit.Keep the existing
Listenresize policy and completed-queue checks unchanged. Enforcingpending <= capacity - 1inListenwould instead require rejecting otherwise permitted reductions, retaining a larger capacity than requested, or aborting existing handshakes. None of those behavior changes is needed for this fix. Do not move or duplicate the syscall layer's+1adjustment.This matches the queue-full comparison in the Linux revision referenced by the existing comment:
inet_csk_reqsk_queue_is_full()uses>=, whileinet_listen()updates the backlog of an already-listening socket without rejecting the update based on the pending request count. This is a comparison of these specific code paths, not a claim of completeListencompatibility.Update the guard's comment to explain both
capacity - 1and why the pending count may already exceed the limit. AddTestListenerShrinkingBacklogUsesCookies: create two pending handshakes withEndpoint.Listen(4), reduce the capacity withEndpoint.Listen(2), and verify that the next SYN uses a cookie without adding a pending endpoint. Verify that the cookie ACK still produces an accept-ready connection with the expected peer. Use a manual stack clock, port and ACK matching, and listener-mutex synchronization to keep state and statistics checks deterministic with respect to handshake timers and segment processing.Linux references: backlog update and pending queue-full comparison.
Assisted-by: ChatGPT
FUTURE_COPYBARA_INTEGRATE_REVIEW=#15038 from 134130:134130/fix-listener-backlog 2269c07