mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-05 09:03:12 -05:00
Make link queue writes nonblocking: gVisor can generate replies on the reader needed to drain the queue. A full queue drops only the rejected packet and reports ErrNoBufferSpace, which gVisor counts and TCP handles by arming its retransmit timer. UDP sockets do not see the error unless IP_RECVERR is set, matching Linux. Count WireGuard-bound drops as reason="queue_full" in tailscaled_outbound_dropped_packets_total, and per-queue drops in debug and client metrics. Add Wrapper.TryInjectOutbound for best-effort replies generated while processing packets (TSMP rejects and pongs, conn25 rejects, fake-mode echo replies), so those paths drop instead of stalling behind the injection queue's reader. Its drops are also counted as queue_full. Make outbound injection waits cancellable and close link queues before abort-generated replies. Stop the WireGuard injection pump on ErrClosed. Release injected packets still queued at Close, outside closeOnce to allow reentrant release callbacks; a sender whose send races with Close releases the packet itself. Release temporary capture buffers. Copy injected payload views directly into the WireGuard slab, avoiding an intermediate allocation and copy. Test queue saturation, multiqueue progress, cancellation, concurrent shutdown, reference ownership, fragmented payloads, and GSO. Performance versus the parent commit, on a 4-core Xeon 6975P-C: inject+read 64B / 1280B 245ns / 479ns -> 187ns / 195ns, 0 allocs 4 injectors, 1 reader 843ns -> 451ns 64KiB GSO inject+split 10.9us -> 4.4us, 64KiB/op -> 0 link queue write+read 81.4ns -> 81.5ns tsnet TCP, 1 / 8 streams 448 / 580 MB/s -> 492 / 674 MB/s BenchmarkTCPQueueBottleneck shows that dropping on full queues keeps throughput at the bottleneck rate (117 MB/s at 100k packets/s, as with blocking writes), at the cost of more retransmits with many streams. Fixes #21631 Updates #21638 Updates #21632 Updates #21639 Signed-off-by: James Tucker <james@tailscale.com>
130 lines
3.7 KiB
Go
130 lines
3.7 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package netstack
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"os"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/tailscale/wireguard-go/tun"
|
|
"gvisor.dev/gvisor/pkg/tcpip/header"
|
|
"gvisor.dev/gvisor/pkg/tcpip/stack"
|
|
"tailscale.com/net/tstun"
|
|
"tailscale.com/util/eventbus/eventbustest"
|
|
"tailscale.com/util/usermetric"
|
|
)
|
|
|
|
type queueTestTUN struct {
|
|
tun.Device
|
|
queues []tun.Queue
|
|
closed chan struct{}
|
|
}
|
|
|
|
func newQueueTestTUN(n int) *queueTestTUN {
|
|
d := &queueTestTUN{Device: tstun.NewFake(), closed: make(chan struct{})}
|
|
for range n {
|
|
d.queues = append(d.queues, &queueTestTUNQueue{closed: d.closed, packets: make(chan []byte, 1)})
|
|
}
|
|
return d
|
|
}
|
|
|
|
func (d *queueTestTUN) Queues() []tun.Queue { return d.queues }
|
|
func (d *queueTestTUN) Read(slab []byte, packets []tun.ReadPacket) (int, error) {
|
|
return d.queues[0].Read(slab, packets)
|
|
}
|
|
func (d *queueTestTUN) WriteTo(_ int, bufs [][]byte, offset int) (int, error) {
|
|
return d.Write(bufs, offset)
|
|
}
|
|
func (d *queueTestTUN) Close() error {
|
|
close(d.closed)
|
|
return d.Device.Close()
|
|
}
|
|
|
|
type queueTestTUNQueue struct {
|
|
closed <-chan struct{}
|
|
packets chan []byte
|
|
}
|
|
|
|
func (*queueTestTUNQueue) File() *os.File { return nil }
|
|
func (q *queueTestTUNQueue) Read(slab []byte, packets []tun.ReadPacket) (int, error) {
|
|
select {
|
|
case <-q.closed:
|
|
return 0, io.EOF
|
|
case data := <-q.packets:
|
|
copy(slab[tun.ReadPacketSpacing:], data)
|
|
packets[0] = tun.ReadPacket{Offset: tun.ReadPacketSpacing, Size: len(data)}
|
|
return 1, nil
|
|
}
|
|
}
|
|
|
|
// Concurrent native TUN reads must not block on synchronous netstack replies
|
|
// when the outbound queue is full.
|
|
func TestMultiQueueNetstackReplyWithFullQueue(t *testing.T) {
|
|
const queues = 4
|
|
s, ep := newQueueTestStack(t, 1)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
ns := &Impl{ctx: ctx, ipstack: s, linkEP: ep, logf: t.Logf}
|
|
ns.ready.Store(true)
|
|
d := newQueueTestTUN(queues)
|
|
w := tstun.Wrap(t.Logf, d, new(usermetric.Registry), eventbustest.NewBus(t))
|
|
defer w.Close()
|
|
w.PreFilterPacketOutboundToWireGuardNetstackIntercept = ns.handleLocalPackets
|
|
w.Start()
|
|
|
|
var released atomic.Int32
|
|
filler := newQueueTestPacket(&released)
|
|
err := ep.outboundQueues[outboundToHost].Write(filler)
|
|
filler.DecRef()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ack := queueTestACK(queueTestLocalIP, serviceIP)
|
|
th := header.TCP(ack.TransportHeader().Slice())
|
|
pseudo := tun.PseudoHeaderChecksum(uint8(header.TCPProtocolNumber), queueTestLocalIP.AsSlice(), serviceIP.AsSlice(), uint16(len(th)))
|
|
th.SetChecksum(^tun.Checksum(th, pseudo))
|
|
data := append([]byte(nil), stack.PayloadSince(ack.NetworkHeader()).AsSlice()...)
|
|
ack.DecRef()
|
|
|
|
var wg sync.WaitGroup
|
|
start := make(chan struct{})
|
|
for i, q := range w.Queues()[:queues] {
|
|
d.queues[i].(*queueTestTUNQueue).packets <- data
|
|
wg.Go(func() {
|
|
<-start
|
|
slab := make([]byte, tstun.MaxPacketSize+2*tun.ReadPacketSpacing)
|
|
n, err := q.Read(slab, make([]tun.ReadPacket, 1))
|
|
if n != 0 || err != nil {
|
|
t.Errorf("intercepted read = %d, %v; want 0, nil", n, err)
|
|
}
|
|
})
|
|
}
|
|
done := make(chan struct{})
|
|
go func() { wg.Wait(); close(done) }()
|
|
close(start)
|
|
select {
|
|
case <-done:
|
|
case <-time.After(2 * time.Second):
|
|
ep.Close()
|
|
w.Close()
|
|
<-done
|
|
t.Fatal("a TUN queue blocked generating a reply into a full netstack queue")
|
|
}
|
|
if got := ep.queueFullDropped[outboundToHost].Value(); got != queues {
|
|
t.Fatalf("queue-full drops = %d, want %d", got, queues)
|
|
}
|
|
// Releasing capacity must restore delivery without canceling netstack.
|
|
ep.Read(outboundToHost).DecRef()
|
|
ep.DeliverLoopback(queueTestACK(queueTestLocalIP, serviceIP))
|
|
requireQueueTestReset(t, ep, outboundToHost)
|
|
if got := released.Load(); got != 1 {
|
|
t.Errorf("filler released %d times, want 1", got)
|
|
}
|
|
}
|