mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-26 03:26:26 -08:00
net/udprelay: return the remaining allocation bind lifetime
Preserve the bind deadline when reusing an unbound allocation. Replace expired allocations without waiting for periodic GC. Test concurrent requests, remaining lifetimes, and expired allocations.
This commit is contained in:
1 parent
85d10d3f03
commit
58f61fd003
4 files changed
+161
-11
No files matched your search
@@ -0,0 +1,137 @@
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
package udprelay
|
||||
|
||||
import (
|
||||
"net/netip"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"tailscale.com/net/udprelay/endpoint"
|
||||
"tailscale.com/tstime/mono"
|
||||
"tailscale.com/types/key"
|
||||
"tailscale.com/types/views"
|
||||
"tailscale.com/util/usermetric"
|
||||
)
|
||||
|
||||
// Concurrent requests from both clients must share one allocation.
|
||||
func TestConcurrentAllocationRetries(t *testing.T) {
|
||||
deregisterMetrics()
|
||||
s, err := NewServer(t.Logf, 0, true, new(usermetric.Registry), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer s.Close()
|
||||
s.SetStaticAddrPorts(views.SliceOf([]netip.AddrPort{netip.AddrPortFrom(netip.MustParseAddr("127.0.0.1"), s.uc4Port)}))
|
||||
a, b := key.NewDisco().Public(), key.NewDisco().Public()
|
||||
type result struct {
|
||||
se endpoint.ServerEndpoint
|
||||
err error
|
||||
}
|
||||
const requests = 32
|
||||
results := make(chan result, requests)
|
||||
var wg sync.WaitGroup
|
||||
for i := range requests {
|
||||
wg.Go(func() {
|
||||
x, y := a, b
|
||||
if i%2 != 0 {
|
||||
x, y = y, x
|
||||
}
|
||||
se, err := s.AllocateEndpoint(x, y)
|
||||
results <- result{se, err}
|
||||
})
|
||||
}
|
||||
wg.Wait()
|
||||
close(results)
|
||||
var first endpoint.ServerEndpoint
|
||||
for r := range results {
|
||||
if r.err != nil {
|
||||
t.Fatal(r.err)
|
||||
}
|
||||
if first.ServerDisco.IsZero() {
|
||||
first = r.se
|
||||
}
|
||||
if r.se.ServerDisco != first.ServerDisco || r.se.VNI != first.VNI || r.se.LamportID != first.LamportID || r.se.ClientDisco != first.ClientDisco {
|
||||
t.Fatalf("retry created a different allocation: got %+v, first %+v", r.se, first)
|
||||
}
|
||||
}
|
||||
if got := len(s.GetSessions()); got != 1 {
|
||||
t.Fatalf("sessions = %d, want 1 for %d concurrent requests", got, requests)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAllocationRetryBindLifetime(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
age time.Duration
|
||||
bound bool
|
||||
fresh bool
|
||||
}{
|
||||
{name: "unbound", age: 10 * time.Second},
|
||||
{name: "expired-before-gc", age: defaultBindLifetime + time.Second, fresh: true},
|
||||
{name: "bound", age: defaultBindLifetime + time.Second, bound: true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
deregisterMetrics()
|
||||
s, err := NewServer(t.Logf, 0, true, new(usermetric.Registry), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer s.Close()
|
||||
s.SetStaticAddrPorts(views.SliceOf([]netip.AddrPort{netip.AddrPortFrom(netip.MustParseAddr("127.0.0.1"), s.uc4Port)}))
|
||||
a, b := key.NewDisco().Public(), key.NewDisco().Public()
|
||||
first, err := s.AllocateEndpoint(a, b)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Advance the allocation's age without sleeping or waiting for GC.
|
||||
s.mu.Lock()
|
||||
e := s.serverEndpointByDisco[key.NewSortedPairOfDiscoPublic(a, b)]
|
||||
e.mu.Lock()
|
||||
e.allocatedAt = mono.Now().Add(-tc.age)
|
||||
allocatedAt := e.allocatedAt
|
||||
if tc.bound {
|
||||
e.boundAddrPorts = [2]netip.AddrPort{netip.MustParseAddrPort("127.0.0.1:10001"), netip.MustParseAddrPort("127.0.0.1:10002")}
|
||||
e.lastSeen = [2]mono.Time{mono.Now(), mono.Now()}
|
||||
}
|
||||
e.mu.Unlock()
|
||||
s.mu.Unlock()
|
||||
|
||||
retry, err := s.AllocateEndpoint(b, a)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if tc.fresh {
|
||||
if retry.VNI == first.VNI || retry.LamportID <= first.LamportID {
|
||||
t.Fatalf("expired allocation not replaced: first=%+v retry=%+v", first, retry)
|
||||
}
|
||||
if _, ok := s.serverEndpointByVNI.Load(first.VNI); ok {
|
||||
t.Fatal("expired VNI still registered")
|
||||
}
|
||||
} else {
|
||||
if retry.VNI != first.VNI || retry.LamportID != first.LamportID {
|
||||
t.Fatalf("live allocation changed: first=%+v retry=%+v", first, retry)
|
||||
}
|
||||
e.mu.Lock()
|
||||
got := e.allocatedAt
|
||||
e.mu.Unlock()
|
||||
if got != allocatedAt {
|
||||
t.Fatal("retry renewed the allocation's age")
|
||||
}
|
||||
}
|
||||
wantMax := defaultBindLifetime
|
||||
if !tc.fresh && !tc.bound {
|
||||
wantMax -= tc.age
|
||||
}
|
||||
if got := retry.BindLifetime.Duration; got <= 0 || got > wantMax {
|
||||
t.Fatalf("bind lifetime = %v, want in (0, %v]", got, wantMax)
|
||||
}
|
||||
if got := len(s.GetSessions()); got != 1 {
|
||||
t.Fatalf("sessions = %d, want 1", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -52,9 +52,10 @@ type ServerEndpoint struct {
|
||||
// associated with this endpoint.
|
||||
VNI uint32
|
||||
|
||||
// BindLifetime is amount of time post-allocation the Server will consider
|
||||
// the endpoint active while it has yet to be bound via 3-way bind handshake
|
||||
// from both client parties.
|
||||
// BindLifetime is the time remaining for both clients to complete the
|
||||
// 3-way bind handshake, excluding transit time for this description.
|
||||
// For bound endpoints it is the server's normal bind lifetime;
|
||||
// SteadyStateLifetime governs expiration instead.
|
||||
BindLifetime tstime.GoDuration
|
||||
|
||||
// SteadyStateLifetime is the amount of time post 3-way bind handshake from
|
||||
|
||||
+15
-6
@@ -991,12 +991,21 @@ func (s *Server) AllocateEndpoint(discoA, discoB key.DiscoPublic) (endpoint.Serv
|
||||
|
||||
pair := key.NewSortedPairOfDiscoPublic(discoA, discoB)
|
||||
e, ok := s.serverEndpointByDisco[pair]
|
||||
now := mono.Now()
|
||||
if ok && e.maybeExpire(now, s.bindLifetime, s.steadyStateLifetime, s.metrics) {
|
||||
// Expire stale allocations without waiting for periodic GC.
|
||||
delete(s.serverEndpointByDisco, pair)
|
||||
s.serverEndpointByVNI.Delete(e.vni)
|
||||
ok = false
|
||||
}
|
||||
if ok {
|
||||
// Return the existing allocation. Clients can resolve duplicate
|
||||
// [endpoint.ServerEndpoint]'s via [endpoint.ServerEndpoint.LamportID].
|
||||
//
|
||||
// TODO: consider ServerEndpoint.BindLifetime -= time.Now()-e.allocatedAt
|
||||
// to give the client a more accurate picture of the bind window.
|
||||
// Reuse the allocation without extending its bind deadline.
|
||||
e.mu.Lock()
|
||||
bindLifetime := s.bindLifetime
|
||||
if !e.isBoundLocked() {
|
||||
bindLifetime = max(0, s.bindLifetime-now.Sub(e.allocatedAt))
|
||||
}
|
||||
e.mu.Unlock()
|
||||
return endpoint.ServerEndpoint{
|
||||
ServerDisco: s.discoPublic,
|
||||
// Returning the "latest" addrPorts for an existing allocation is
|
||||
@@ -1008,7 +1017,7 @@ func (s *Server) AllocateEndpoint(discoA, discoB key.DiscoPublic) (endpoint.Serv
|
||||
AddrPorts: s.getAllAddrPortsCopyLocked(),
|
||||
VNI: e.vni,
|
||||
LamportID: e.lamportID,
|
||||
BindLifetime: tstime.GoDuration{Duration: s.bindLifetime},
|
||||
BindLifetime: tstime.GoDuration{Duration: bindLifetime},
|
||||
SteadyStateLifetime: tstime.GoDuration{Duration: s.steadyStateLifetime},
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -238,8 +238,11 @@ func TestServer(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// We expect the same endpoint details pre-handshake.
|
||||
if diff := cmp.Diff(dupEndpoint, endpoint, cmpopts.EquateComparable(netip.AddrPort{}, key.DiscoPublic{})); diff != "" {
|
||||
// Retrying allocation preserves identity but reduces the bind window.
|
||||
if got := dupEndpoint.BindLifetime.Duration; got <= 0 || got > endpoint.BindLifetime.Duration {
|
||||
t.Fatalf("duplicate allocation bind lifetime = %v, want in (0, %v]", got, endpoint.BindLifetime.Duration)
|
||||
}
|
||||
if diff := cmp.Diff(dupEndpoint, endpoint, cmpopts.IgnoreFields(endpoint, "BindLifetime"), cmpopts.EquateComparable(netip.AddrPort{}, key.DiscoPublic{})); diff != "" {
|
||||
t.Fatalf("wrong dupEndpoint (-got +want)\n%s", diff)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user