perf: убрать аллокации на горячем пути, TCP DPI-split и KCP FEC

- Атомарный счётчик nonce вместо per-packet rand.Read (syscall getrandom);
  wire-формат не изменён, сервер декодирует без правок
- wrapInto: TX-путь без аллокаций с заранее выделенным буфером;
  wrapPacket оставлен для тестов и control-путей
- Кеш указателя при приёме WG исключает аллокацию addr.String() на пакет
  (быстрый путь — сравнение указателей, медленный — строк)
- Атомики статистики throughput спрятаны за isDebug — снижает трафик
  cache-line по N стримам в production-сборках
- splitFirstWriteConn разбивает первую TCP-запись на байте 6 + задержка
  20ms, ломая STUN magic cookie между сегментами против поверхностного DPI
- VK_TURN_KCP_FEC=data:parity включает Reed-Solomon FEC поверх KCP;
  обе стороны должны совпадать; по умолчанию 0:0 (выключено)
- Исправлен WaitGroup: wg.Add(1) → wg.Add(2) для двух горутин в
  oneDtlsConnection; раньше wg.Wait() возвращался слишком рано
This commit is contained in:
samosvalishe authored and antongospod committed 2026-05-11 23:46:27 +03:00
1 parent 334a17f1af
commit 7a0f98f730
7 files changed
+168 -36

No files matched your search

+52 -9
View File
@@ -4,9 +4,11 @@ package main
import (
"crypto/rand"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"sync/atomic"
"golang.org/x/crypto/chacha20"
)
@@ -16,6 +18,31 @@ const (
wrapKeyLen = 32
)
// Nonce layout: [4B session prefix | 8B atomic counter, big-endian].
// Session prefix is randomised once at process start; counter monotonically
// increments per-packet. This eliminates the per-packet crypto/rand syscall
// (which on Linux is a getrandom() round-trip ~hundreds of ns) while keeping
// (key, nonce) pairs unique across 2^64 packets per session and 2^32 sessions
// per key. Wire format is byte-identical to the previous random-nonce scheme,
// so server-side decryption is unchanged.
var (
nonceSessionPrefix [4]byte
nonceCounter atomic.Uint64
)
func init() {
// Best-effort: if rand.Read fails this early, the OS is in trouble and the
// binary is going to crash anyway during DTLS cert generation. Leaving the
// prefix zero would still keep nonces unique within this process.
_, _ = rand.Read(nonceSessionPrefix[:])
}
func fillWrapNonce(dst []byte) {
_ = dst[wrapNonceLen-1] // bounds-check hint
copy(dst[:4], nonceSessionPrefix[:])
binary.BigEndian.PutUint64(dst[4:wrapNonceLen], nonceCounter.Add(1))
}
func genWrapKeyHex() (string, error) {
key := make([]byte, wrapKeyLen)
if _, err := rand.Read(key); err != nil {
@@ -41,20 +68,36 @@ func decodeWrapKey(enabled bool, raw string) ([]byte, error) {
return key, nil
}
func wrapPacket(key, payload []byte) ([]byte, error) {
// wrapInto encrypts payload into dst using the shared scheme
// [12B nonce | ChaCha20-XOR(payload)]. dst must have capacity for
// wrapNonceLen+len(payload). Returns total bytes written. Hot-path callers
// should reuse a single dst buffer to avoid per-packet allocation.
func wrapInto(dst, key, payload []byte) (int, error) {
if len(key) != wrapKeyLen {
return nil, fmt.Errorf("wrap: key must be %d bytes (got %d)", wrapKeyLen, len(key))
return 0, fmt.Errorf("wrap: key must be %d bytes (got %d)", wrapKeyLen, len(key))
}
out := make([]byte, wrapNonceLen+len(payload))
if _, err := rand.Read(out[:wrapNonceLen]); err != nil {
return nil, fmt.Errorf("wrap: nonce gen: %w", err)
total := wrapNonceLen + len(payload)
if len(dst) < total {
return 0, errors.New("wrap: dst buffer too small")
}
cipher, err := chacha20.NewUnauthenticatedCipher(key, out[:wrapNonceLen])
fillWrapNonce(dst[:wrapNonceLen])
cipher, err := chacha20.NewUnauthenticatedCipher(key, dst[:wrapNonceLen])
if err != nil {
return nil, fmt.Errorf("wrap: cipher init: %w", err)
return 0, fmt.Errorf("wrap: cipher init: %w", err)
}
cipher.XORKeyStream(out[wrapNonceLen:], payload)
return out, nil
cipher.XORKeyStream(dst[wrapNonceLen:total], payload)
return total, nil
}
// wrapPacket allocates a fresh wire buffer. Retained for tests and control
// paths; hot-path code should call wrapInto with a reusable scratch buffer.
func wrapPacket(key, payload []byte) ([]byte, error) {
out := make([]byte, wrapNonceLen+len(payload))
n, err := wrapInto(out, key, payload)
if err != nil {
return nil, err
}
return out[:n], nil
}
func unwrapPacket(key, wire, dst []byte) (int, error) {