Files
tailscale/net/netns/netns_windows_test.go
James Tucker ae4ab7e7d5 net/netmon,net/netns: cache Windows default interface indices
Windows socket setup enumerates adapters and the routing table through
GetWindowsDefault for every netns socket. These calls can be expensive,
particularly on systems where security software adds syscall latency.

Cache nonzero default interface indices per monitor and address family,
and use them from netns controlC. Invalidate on native route, address,
and IP interface change notifications.

Coalesce concurrent lookups and retry results invalidated during lookup.
Bound lookup and wait attempts; once the budget is exhausted, fall back
to a single uncached lookup rather than failing socket setup, erring
toward a possibly stale interface index over no socket at all. Perform
fresh lookups for zero indices and errors, and retain the existing
unbound-socket fallback for a fresh zero result.

Use uncached lookups after monitor shutdown or when interface-change
notification registration fails.

Test cache reuse, independent address families, notification invalidation,
concurrent fills, invalidation during lookup, route restoration after a
zero result, churn and wait-budget fallbacks, and shutdown.

Fixes #21885

Signed-off-by: James Tucker <[email protected]>
2026-10-23 17:50:52 -04:00

115 lines
2.6 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package netns
import (
"strings"
"testing"
"golang.org/x/sys/windows"
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
"tailscale.com/net/netmon"
"tailscale.com/tsconst"
)
func TestGetInterfaceIndex(t *testing.T) {
oldVal := bindToInterfaceByRoute.Load()
t.Cleanup(func() { bindToInterfaceByRoute.Store(oldVal) })
bindToInterfaceByRoute.Store(true)
netMon := netmon.NewStatic()
defIfaceIdxV4, err := netMon.WindowsDefaultInterfaceIndex(windows.AF_INET)
if err != nil {
t.Fatalf("defaultInterfaceIndex(AF_INET) failed: %v", err)
}
tests := []struct {
name string
addr string
err string
}{
{
name: "IP_and_port",
addr: "8.8.8.8:53",
},
{
name: "bare_ip",
addr: "8.8.8.8",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
addr, err := parseAddress(tc.addr)
if err != nil {
t.Fatal(err)
}
idx, err := getInterfaceIndex(t.Logf, addr, defIfaceIdxV4)
if err != nil {
if tc.err == "" {
t.Fatalf("got unexpected error: %v", err)
}
if errstr := err.Error(); errstr != tc.err {
t.Errorf("expected error %q, got %q", errstr, tc.err)
}
} else {
t.Logf("getInterfaceIndex(%q) = %d", tc.addr, idx)
if tc.err != "" {
t.Fatalf("wanted error %q", tc.err)
}
}
})
}
t.Run("NoTailscale", func(t *testing.T) {
tsIdx, ok, err := tailscaleInterfaceIndex()
if err != nil {
t.Fatal(err)
}
if !ok {
t.Skip("no tailscale interface on this machine")
}
defaultIdx, err := netMon.WindowsDefaultInterfaceIndex(windows.AF_INET)
if err != nil {
t.Fatalf("defaultInterfaceIndex(AF_INET) failed: %v", err)
}
addr, err := parseAddress("100.100.100.100:53")
if err != nil {
t.Fatal(err)
}
idx, err := getInterfaceIndex(t.Logf, addr, defaultIdx)
if err != nil {
t.Fatal(err)
}
t.Logf("tailscaleIdx=%d defaultIdx=%d idx=%d", tsIdx, defaultIdx, idx)
if idx == tsIdx {
t.Fatalf("got idx=%d; wanted not Tailscale interface", idx)
} else if idx != defaultIdx {
t.Fatalf("got idx=%d, want %d", idx, defaultIdx)
}
})
}
func tailscaleInterfaceIndex() (idx uint32, found bool, err error) {
ifs, err := winipcfg.GetAdaptersAddresses(windows.AF_INET, winipcfg.GAAFlagIncludeAllInterfaces)
if err != nil {
return idx, false, err
}
for _, iface := range ifs {
if iface.IfType != winipcfg.IfTypePropVirtual {
continue
}
if strings.Contains(iface.Description(), tsconst.WintunInterfaceDesc) {
return iface.IfIndex, true, nil
}
}
return idx, false, nil
}