Repository navigation
Conversation
After a TLS 1.3 handshake with AES-GCM or ChaCha20-Poly1305 the traffic keys go to the kernel, which encrypts and decrypts during the copies it makes anyway; the connection's data path is then that of plain TCP, in-place sends included. WithKTLS (default on), Conn.KernelTLS and Stats.KernelTLSConnections. - The handshake now reads and writes the socket directly from its goroutine (on a dup of the fd), never past the end of the current TLS record, so everything after it stays in the socket for the kernel. The loop arms recv only after the handshake. - Secrets come from a per-connection KeyLogWriter (the user's writer still receives the lines); key and IV via HKDF-Expand-Label; record sequence 0, which holds because kTLS servers send no session tickets (Go sends them with the application keys in its first flight). - TLS_RX_EXPECT_NO_PAD lets the kernel decrypt into the receive buffer. - Non-data records make recv fail with EIO: read with recvmsg and TLS_GET_RECORD_TYPE; close_notify ends the connection like EOF, session tickets are ignored, KeyUpdate closes. close_notify is sent with TLS_SET_RECORD_TYPE before shutdown. - No tls module (unprivileged processes cannot load it), TLS 1.2, or WithKTLS(false): crypto/tls in user space as before. Measured against a crypto/tls server (server on 1 core, arm64 VM): kTLS 1.00-1.13x at 128B, 0.98x at 16KB, 0.90x at 64KB, versus 0.92x / 0.77x for user-space TLS at 16KB / 64KB. perf showed the copies gone (18% -> 6% of CPU) while the kernel's AES-GCM is no faster than Go's here. Tests run with kTLS on and off, assert that kTLS is used when the module is loaded, cover TLS 1.2, data sent right after the handshake, and pass with the module unloaded; CI loads the module. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #31 +/- ##
==========================================
- Coverage 70.22% 70.19% -0.04%
==========================================
Files 25 25
Lines 2146 2335 +189
==========================================
+ Hits 1507 1639 +132
- Misses 639 696 +57 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
kTLS
After a TLS 1.3 handshake (AES-GCM / ChaCha20-Poly1305) the traffic keys go to the kernel: it encrypts/decrypts during the copies it makes anyway, and the connection's data path becomes that of plain TCP (in-place sends included).
WithKTLS(default on),Conn.KernelTLS,Stats.KernelTLSConnections.KeyLogWriter(user's writer still gets the lines) → HKDF-Expand-Label →TCP_ULP tls,TLS_RX,TLS_TXwith record sequence 0. That holds because kTLS servers send no session tickets (Go sends them under the application keys in its first flight) — documented;WithKTLS(false)keeps them.TLS_RX_EXPECT_NO_PADlets the kernel decrypt straight into the receive buffer (+6–14% at 16–64KB in the benchmark).EIO→recvmsg+TLS_GET_RECORD_TYPE; close_notify = EOF, tickets ignored, KeyUpdate closes. close_notify sent withTLS_SET_RECORD_TYPE.tlsmodule (unprivileged processes cannot load it), for TLS 1.2, or withWithKTLS(false).Performance (vs a crypto/tls server, server on 1 core, arm64 VM)
perf: copies drop from 18% to 6% of CPU, but this VM's kernel AES-GCM (gcm-aes-ce) is no faster than Go's assembly, which caps the gain here.Tests
TLS tests run with kTLS on and off and assert kTLS is used when the module is loaded; new: TLS 1.2 stays in user space, data sent right after the handshake is not lost. Pass with the module unloaded, on both backends, 5× each; CI now loads the module.
Also: CHANGELOG v0.3.0 (epoll fallback, TLS, kTLS). After merging,
v0.3.0will be tagged with a GitHub release.🤖 Generated with Claude Code