mirror of
https://github.com/tailscale/tailscale.git
synced 2026-10-29 06:28:57 -04: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>
94 lines
3.5 KiB
Go
94 lines
3.5 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package netstack
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"gvisor.dev/gvisor/pkg/tcpip"
|
|
"gvisor.dev/gvisor/pkg/tcpip/stack"
|
|
"tailscale.com/feature/buildfeatures"
|
|
"tailscale.com/util/clientmetric"
|
|
"tailscale.com/util/usermetric"
|
|
)
|
|
|
|
func TestOutboundQueueFullMetrics(t *testing.T) {
|
|
route := map[*stack.PacketBuffer]outboundQueue{}
|
|
ep := newLinkEndpoint(1, 1280, "", groNotSupported, func(pkt *stack.PacketBuffer) outboundQueue { return route[pkt] })
|
|
t.Cleanup(ep.Close)
|
|
ns := &Impl{linkEP: ep, ipstack: stack.New(stack.Options{})}
|
|
t.Cleanup(func() { ns.ipstack.Close(); ns.ipstack.Wait() })
|
|
stacksForMetrics.Store(ns, struct{}{})
|
|
t.Cleanup(func() { stacksForMetrics.Delete(ns) })
|
|
var reg usermetric.Registry
|
|
ns.SetMetricsRegistry(®)
|
|
debug := ns.ExpVar() // must reflect updates after registration
|
|
|
|
// With one slot per queue, the second WireGuard and loopback packets are
|
|
// dropped. A full queue must not drop later packets bound for other queues.
|
|
var released atomic.Int32
|
|
var pkts stack.PacketBufferList
|
|
for _, q := range []outboundQueue{outboundToWireGuard, outboundToWireGuard, outboundToHost, outboundLoopback, outboundLoopback} {
|
|
pkt := newQueueTestPacket(&released)
|
|
route[pkt] = q
|
|
pkts.PushBack(pkt)
|
|
}
|
|
n, err := ep.WritePackets(pkts)
|
|
pkts.DecRef()
|
|
if _, ok := err.(*tcpip.ErrNoBufferSpace); !ok || n != 3 {
|
|
t.Fatalf("WritePackets = %d, %v; want 3, ErrNoBufferSpace", n, err)
|
|
}
|
|
if got := released.Load(); got != 2 {
|
|
t.Errorf("released = %d, want 2", got)
|
|
}
|
|
want := map[string]int64{"wireguard": 1, "host": 0, "loopback": 1}
|
|
for q, name := range map[outboundQueue]string{outboundToWireGuard: "wireguard", outboundToHost: "host", outboundLoopback: "loopback"} {
|
|
if got := ep.queueFullDropped[q].Value(); got != want[name] {
|
|
t.Errorf("%s queue-full drops = %d, want %d", name, got, want[name])
|
|
}
|
|
if s := fmt.Sprintf("\"counter_outbound_queue_full_dropped_packets_%s\": %d", name, want[name]); !strings.Contains(debug.String(), s) {
|
|
t.Errorf("debug metrics missing %s: %s", s, debug.String())
|
|
}
|
|
}
|
|
if buildfeatures.HasUserMetrics {
|
|
rr := httptest.NewRecorder()
|
|
reg.Handler(rr, httptest.NewRequest("GET", "/metrics", nil))
|
|
if s := "tailscaled_outbound_dropped_packets_total{reason=\"queue_full\"} 1\n"; !strings.Contains(rr.Body.String(), s) {
|
|
t.Errorf("user metrics missing %q: %s", s, rr.Body.String())
|
|
}
|
|
}
|
|
if buildfeatures.HasClientMetrics {
|
|
// Client metrics sum all queues of all instances.
|
|
other := &Impl{linkEP: newLinkEndpoint(0, 1280, "", groNotSupported, nil), ipstack: ns.ipstack}
|
|
other.linkEP.queueFullDropped[outboundToHost].Add(2)
|
|
stacksForMetrics.Store(other, struct{}{})
|
|
t.Cleanup(func() { stacksForMetrics.Delete(other); other.linkEP.Close() })
|
|
var b bytes.Buffer
|
|
clientmetric.WritePrometheusExpositionFormat(&b)
|
|
if s := "netstack_outbound_queue_full_dropped_packets 4\n"; !strings.Contains(b.String(), s) {
|
|
t.Errorf("client metrics missing %q: %s", s, b.String())
|
|
}
|
|
}
|
|
|
|
// Shutdown loss is not queue-full loss.
|
|
ep.Close()
|
|
pkt := newQueueTestPacket(&released)
|
|
route[pkt] = outboundToWireGuard
|
|
var closedPkts stack.PacketBufferList
|
|
closedPkts.PushBack(pkt)
|
|
n, err = ep.WritePackets(closedPkts)
|
|
closedPkts.DecRef()
|
|
if _, ok := err.(*tcpip.ErrClosedForSend); !ok || n != 0 {
|
|
t.Errorf("closed WritePackets = %d, %v", n, err)
|
|
}
|
|
if got := ep.queueFullDroppedTotal(); got != 2 {
|
|
t.Errorf("closed write changed queue-full drops to %d, want 2", got)
|
|
}
|
|
}
|