Files
tailscale/net/tstun/connreject_test.go
Alex Valiushko 76648a00fd net/tstun: implement independent injection queue (#21681)
Injected packets now arrive on a queue of their own rather than being
multiplexed onto queue 0. Wrapper.Read is deprecated. It still reads the
first kernel-backed queue, but from now on no single queue of a Wrapper
sees every packet.

Deletes the poll goroutine, the bufferConsumed token, the vectorOutbound
channel, tunVectorReadResult, a full-size slab, and the copy of every
accepted packet between the two slabs.

wireguard-go now runs an extra TUN reader goroutine, a net wash compared
to running our poll routine.

Measured on c8gn.2xlarge, Ubuntu 24.04, Kernel v6.14, tailscaled under
load from two peers, each running iperf3 -P8.

            tx mean   rx mean   cores
  main       7.00      8.84      3.49
  branch     8.94      8.86      3.95

Bumps wireguard-go pin to tailscale/wireguard-go@e61b6b7811.

Updates tailscale/corp#37878

Change-Id: Ide0e8a9f345371b2747fad59dd2bf963c8af5b63

Signed-off-by: Alex Valiushko <[email protected]>
2026-10-08 13:13:11 -07:00

161 lines
4.7 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
//go:build !ts_omit_connreject && !ts_omit_netstack
package tstun
import (
"testing"
"go4.org/netipx"
"tailscale.com/net/connreject"
"tailscale.com/net/packet"
"tailscale.com/types/ipproto"
"tailscale.com/types/logger"
"tailscale.com/util/eventbus/eventbustest"
"tailscale.com/wgengine/filter"
)
// TestOutboundTSMPRecordsRejection verifies that when an inbound TCP4 SYN
// is dropped by the packet filter, the TSMP reject we inject outbound is
// also delivered to the connreject callback.
func TestOutboundTSMPRecordsRejection(t *testing.T) {
t.Parallel()
bus := eventbustest.NewBus(t)
_, w := newFakeTUN(t.Logf, bus, false)
defer w.Close()
// The callback fires synchronously from
// filterPacketInboundFromWireGuard on this test's goroutine, so a
// plain slice (no mutex) is sufficient.
var events []connreject.Event
w.SetConnRejectCallback(func(e connreject.Event) { events = append(events, e) })
// Drain the outbound queue so InjectOutbound doesn't block.
go func() {
slab, packets := getSinglePacketReadArgs()
for {
n, err := w.injectionQueue.Read(slab, packets)
_ = n
if err != nil {
return
}
}
}()
// Install an allow-none filter so the inbound SYN is dropped,
// triggering the TSMP reject emission.
w.disableFilter = false
w.SetFilter(filter.NewAllowNone(logger.Discard, new(netipx.IPSet)))
pkt := tcp4syn("1.2.3.4", "100.64.1.2", 1234, 60000)
p := new(packet.Parsed)
p.Decode(pkt)
if res, _ := w.filterPacketInboundFromWireGuard(p, nil, nil, nil); !res.IsDrop() {
t.Fatalf("expected drop, got %v", res)
}
if len(events) != 1 {
t.Fatalf("got %d events, want 1", len(events))
}
got := events[0]
if got.Direction != connreject.Incoming {
t.Errorf("Direction = %v, want Incoming", got.Direction)
}
if got.Source != connreject.SourceTSMPSent {
t.Errorf("Source = %v, want tsmp_sent", got.Source)
}
if got.Reason != connreject.ReasonACL {
t.Errorf("Reason = %q, want %q", got.Reason, connreject.ReasonACL)
}
if got.Proto != ipproto.TCP {
t.Errorf("Proto = %v, want TCP", got.Proto)
}
// Src is the remote peer-side of the incoming flow.
if got.Src.Port() != 1234 {
t.Errorf("Src.Port = %d, want 1234", got.Src.Port())
}
if got.Dst.Port() != 60000 {
t.Errorf("Dst.Port = %d, want 60000", got.Dst.Port())
}
}
// TestSetConnRejectCallbackUninstall verifies that SetConnRejectCallback(nil)
// safely clears the previously installed callback and that
// notifyConnRejectTSMPSent is a safe no-op afterward.
func TestSetConnRejectCallbackUninstall(t *testing.T) {
t.Parallel()
bus := eventbustest.NewBus(t)
_, w := newFakeTUN(t.Logf, bus, false)
defer w.Close()
w.Start() // unblock Read so the drain goroutine makes progress
var events []connreject.Event
w.SetConnRejectCallback(func(e connreject.Event) { events = append(events, e) })
// Drain the outbound queue so InjectOutbound doesn't block.
go func() {
slab, packets := getSinglePacketReadArgs()
for {
if _, err := w.injectionQueue.Read(slab, packets); err != nil {
return
}
}
}()
w.disableFilter = false
w.SetFilter(filter.NewAllowNone(logger.Discard, new(netipx.IPSet)))
pkt := tcp4syn("1.2.3.4", "100.64.1.2", 1234, 60000)
p := new(packet.Parsed)
p.Decode(pkt)
// First emit while installed: should deliver.
w.filterPacketInboundFromWireGuard(p, nil, nil, nil)
if len(events) != 1 {
t.Fatalf("after install: got %d events, want 1", len(events))
}
// Uninstall and emit again: must not panic and must not deliver.
w.SetConnRejectCallback(nil)
w.filterPacketInboundFromWireGuard(p, nil, nil, nil)
if len(events) != 1 {
t.Errorf("after uninstall: got %d events, want still 1", len(events))
}
// Reinstall and emit a third time: should deliver again.
w.SetConnRejectCallback(func(e connreject.Event) { events = append(events, e) })
w.filterPacketInboundFromWireGuard(p, nil, nil, nil)
if len(events) != 2 {
t.Errorf("after reinstall: got %d events, want 2", len(events))
}
}
// TestOutboundTSMPNotRecordedWhenDisabled verifies we skip the note
// callback when disableTSMPRejected is set.
func TestOutboundTSMPNotRecordedWhenDisabled(t *testing.T) {
t.Parallel()
bus := eventbustest.NewBus(t)
_, w := newFakeTUN(t.Logf, bus, false)
defer w.Close()
var events []connreject.Event
w.SetConnRejectCallback(func(e connreject.Event) { events = append(events, e) })
w.disableFilter = false
w.disableTSMPRejected = true
w.SetFilter(filter.NewAllowNone(logger.Discard, new(netipx.IPSet)))
pkt := tcp4syn("1.2.3.4", "100.64.1.2", 1234, 60000)
p := new(packet.Parsed)
p.Decode(pkt)
w.filterPacketInboundFromWireGuard(p, nil, nil, nil)
if len(events) != 0 {
t.Errorf("got %d events, want 0 when disableTSMPRejected was set", len(events))
}
}