Files
tailscale/net/tstun/fortest.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

103 lines
2.4 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package tstun
import (
"bytes"
"context"
"github.com/tailscale/wireguard-go/tun"
"tailscale.com/util/testenv"
)
// forTest is an unexported type to hide the test-only methods on [Wrapper]
// from godoc.
type forTest struct {
w *Wrapper
tickets []chan struct{} // authorize reads, one channel per queue of w.
results chan readResult
}
// readResult is the outcome of a single [tun.Queue.Read].
type readResult struct {
pkts [][]byte
err error
}
// ForTest returns a handle to test-only methods on t. The resulting
// type is unexported to make it very obvious in godoc that this is
// not stable API. This method panics if called outside of tests,
// which also centralizes all must-be-in-tests validation.
func (t *Wrapper) ForTest() *forTest {
testenv.AssertInTest()
return t.forTest.Get(func() *forTest {
qs := t.Queues()
f := &forTest{
w: t,
tickets: make([]chan struct{}, len(qs)),
results: make(chan readResult),
}
for i, q := range qs {
f.tickets[i] = make(chan struct{}, 1)
go f.read(f.tickets[i], q)
}
return f
})
}
// read hands the outcome of every authorized read of q to [forTest.ReadN],
// until a read fails or f's Wrapper is closed.
func (f *forTest) read(tickets <-chan struct{}, q tun.Queue) {
slab := make([]byte, MaxPacketSize+(2*tun.ReadPacketSpacing))
packets := make([]tun.ReadPacket, 1)
for {
select {
case <-tickets:
case <-f.w.closed:
return
}
n, err := q.Read(slab, packets)
// Copy the packets out, reuse the slab.
pkts := make([][]byte, 0, n)
for _, meta := range packets[:n] {
pkts = append(pkts, bytes.Clone(slab[meta.Offset:meta.Offset+meta.Size]))
}
select {
case f.results <- readResult{pkts, err}:
case <-f.w.closed:
return
}
if err != nil {
return
}
}
}
// ReadN returns result of the next n reads across all queues.
// The packets are copies.
func (f *forTest) ReadN(ctx context.Context, n int) ([][]byte, error) {
var pkts [][]byte
for range n {
for _, tickets := range f.tickets {
select {
case tickets <- struct{}{}:
default:
}
}
select {
case r := <-f.results:
pkts = append(pkts, r.pkts...)
if r.err != nil {
return pkts, r.err
}
case <-f.w.closed:
return pkts, ErrClosed
case <-ctx.Done():
return pkts, ctx.Err()
}
}
return pkts, nil
}