Files
netbird/client/internal/stdnet/wgdetector.go
T
Riccardo Manfrin 3e85e40be2 [client] Cache the WireGuard interface check shared by ICE agents (#8001)
* [client] Take a WireGuard detector through the interface filter

The interface filter answers whether an interface is a WireGuard device by opening
a wgctrl client and asking for it, and it does that for every interface it is given.
Nothing about that call is tied to the caller, so it can be answered by a shared
object instead of being repeated, but the filter has no way to receive one.

InterfaceFilter and the constructors that build one now take a detector, and the ICE
config carries it so that every agent can be handed the same one. Nobody supplies a
detector yet: a nil one probes on every call, which is what the filter did before, so
this changes no behaviour.

* [client] Share one WireGuard detector across every ICE agent

Creating an ICE agent builds two interface filters, one for the agent and one for
the transport net it sits on, and each is asked about every host interface. For an
interface the disallow list does not settle, answering means opening a wgctrl client,
which builds a kernel and a userspace client and resolves the netlink family, and
then a round trip that usually just reports the device does not exist. An agent is
created per peer connection attempt, so on a large network that runs constantly:
on a routing peer with ~16000 peers it measured 2.40s of a 66.59s CPU profile, 3.6%,
split evenly between opening the client and the round trip.

The engine now owns a detector and passes it to every agent through the ICE config,
so the answer for an interface is reused instead of being asked again for each agent.
It is kept for a second, short enough that a WireGuard interface appearing is picked
up before ICE settles on candidates over it.

The callers that build one filter and keep it, the relay and the UDP mux, keep
passing nil and so keep probing, which costs them nothing at their rate.

* [client] Recheck the WireGuard cache inside the singleflight group

A caller that saw an expired entry could enter the singleflight group
after another caller had already refreshed the entry and left it, and
probe the interface a second time. Read the cache again inside the group
before probing.

This also makes the concurrent probe test independent of scheduling: a
late caller finds the fresh entry instead of starting a new probe.

* [client] Drop expired WireGuard detector entries

The detector lives as long as the engine and kept an entry for every
interface name it was ever asked about. On hosts that churn interfaces,
such as container veths, the map only grew. Remove expired entries when
a new answer is stored; the map holds a few dozen names at most, so the
sweep is cheap and runs at most once per interface per TTL.

* [client] Skip the disallow-list filter test on iOS

InterfaceFilter does not apply the disallow list on iOS, so the subtest
reaches the probe there and its no-probe assertion cannot hold.
2026-10-09 11:03:34 +02:00

120 lines
3.2 KiB
Go

package stdnet
import (
"sync"
"time"
log "github.com/sirupsen/logrus"
"golang.org/x/sync/singleflight"
"golang.zx2c4.com/wireguard/wgctrl"
)
// wgDetectorTTL bounds how long a cached answer is trusted. An interface rarely
// becomes, or stops being, a WireGuard device, and the window only has to be short
// enough that ICE does not keep gathering candidates on one that just appeared.
const wgDetectorTTL = 1 * time.Second
type wgDetectorEntry struct {
isWireGuard bool
expireAt time.Time
}
// WGDetector answers whether an interface is a WireGuard device, remembering the
// answer for a short while.
//
// The question is asked once per interface for every ICE agent, and an agent is
// created per peer connection attempt, so on a large network the uncached form runs
// constantly. Answering it means opening a wgctrl client, which builds both a kernel
// and a userspace client and resolves the netlink family, and then a round trip that
// usually just reports the device does not exist.
//
// A detector is safe for concurrent use and is meant to be shared by every agent.
type WGDetector struct {
ttl time.Duration
// probe is replaced in tests; it is the call this type exists to avoid repeating.
probe func(string) bool
mu sync.RWMutex
cache map[string]wgDetectorEntry
sf singleflight.Group
}
// NewWGDetector returns a detector with the default time to live.
func NewWGDetector() *WGDetector {
return &WGDetector{
ttl: wgDetectorTTL,
probe: probeWireGuard,
cache: make(map[string]wgDetectorEntry),
}
}
// IsWireGuard reports whether the named interface is a WireGuard device. An interface
// it cannot ask about is reported as not being one, which leaves it available to ICE
// exactly as an uncached probe would.
func (d *WGDetector) IsWireGuard(iFace string) bool {
if d == nil {
return probeWireGuard(iFace)
}
if isWireGuard, ok := d.cached(iFace); ok {
return isWireGuard
}
result, _, _ := d.sf.Do(iFace, func() (interface{}, error) {
// A caller that saw the entry expire may get here after another caller already
// refreshed it and left the singleflight group.
if isWireGuard, ok := d.cached(iFace); ok {
return isWireGuard, nil
}
isWireGuard := d.probe(iFace)
d.store(iFace, isWireGuard)
return isWireGuard, nil
})
return result.(bool)
}
func (d *WGDetector) cached(iFace string) (isWireGuard, ok bool) {
d.mu.RLock()
defer d.mu.RUnlock()
entry, found := d.cache[iFace]
if !found || !time.Now().Before(entry.expireAt) {
return false, false
}
return entry.isWireGuard, true
}
// store records an answer and drops the expired ones, so names of interfaces that came
// and went, such as container veths, do not pile up for the life of the engine.
func (d *WGDetector) store(iFace string, isWireGuard bool) {
now := time.Now()
d.mu.Lock()
defer d.mu.Unlock()
for name, entry := range d.cache {
if !now.Before(entry.expireAt) {
delete(d.cache, name)
}
}
d.cache[iFace] = wgDetectorEntry{isWireGuard: isWireGuard, expireAt: now.Add(d.ttl)}
}
func probeWireGuard(iFace string) bool {
wg, err := wgctrl.New()
if err != nil {
log.Debugf("trying to create a wgctrl client failed with: %v", err)
return false
}
defer func() {
_ = wg.Close()
}()
_, err = wg.Device(iFace)
return err == nil
}