mirror of
https://github.com/tailscale/tailscale.git
synced 2026-12-06 23:36:34 +09:00
wgengine/router/osrouter: reconcile rp_filter rules off the eventbus
The netmon ChangeDelta callback reconciled the rp_filter underlay rules itself, taking r.mu and making netlink calls on the router's eventbus goroutine, which delivers events one at a time: a long Set held up RuleDeleted and PortUpdate delivery behind it. Hand the reconcile to a goroutine instead, and coalesce bursts: while one is waiting for r.mu, further changes are dropped, since it reads the latest interface state once it runs. At most one goroutine waits and one runs at a time. Updates #3310 Signed-off-by: Mike O'Driscoll <mikeo@tailscale.com>
This commit is contained in:
3 files changed
+72
-5
No files matched your search
@@ -126,6 +126,10 @@ type linuxRouter struct {
|
||||
// before subscribeEvents, or later with mu held. Read with mu held.
|
||||
underlayAddrs func() []netip.Addr
|
||||
|
||||
// netChangePending is whether an underlay source rule reconcile for a
|
||||
// network change is queued but hasn't yet acquired mu.
|
||||
netChangePending atomic.Bool
|
||||
|
||||
// lastUnderlayErr is the last underlay rule error logged, so a
|
||||
// persistent failure isn't logged on every network change.
|
||||
lastUnderlayErr string
|
||||
@@ -310,7 +314,7 @@ func (r *linuxRouter) subscribeEvents(bus *eventbus.Bus) {
|
||||
}
|
||||
})
|
||||
eventbus.SubscribeFunc(ec, func(netmon.ChangeDelta) {
|
||||
r.onNetworkChange()
|
||||
r.queueNetworkChange()
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -578,13 +578,30 @@ func (r *linuxRouter) underlayNetlinkUsable() bool {
|
||||
return ok
|
||||
}
|
||||
|
||||
// onNetworkChange is the netmon.ChangeDelta eventbus callback. It
|
||||
// reconciles the underlay rules with the current interface state so they
|
||||
// follow address changes (DHCP renewals, interfaces coming and going, IPv6
|
||||
// privacy address rotation).
|
||||
// queueNetworkChange is the netmon.ChangeDelta eventbus callback. It must
|
||||
// return quickly: the router's eventbus client delivers events one at a
|
||||
// time, so blocking here (on r.mu behind a long Set, then on netlink
|
||||
// syscalls) would delay RuleDeleted and PortUpdate delivery. It hands the
|
||||
// work to a goroutine and coalesces bursts: while a reconcile is pending
|
||||
// (waiting for r.mu), further changes are dropped, since the pending
|
||||
// reconcile reads the latest interface state once it runs. At most one
|
||||
// goroutine waits and one runs at a time.
|
||||
func (r *linuxRouter) queueNetworkChange() {
|
||||
if r.closed.Load() || r.netChangePending.Swap(true) {
|
||||
return
|
||||
}
|
||||
go r.onNetworkChange()
|
||||
}
|
||||
|
||||
// onNetworkChange reconciles the underlay rules with the current interface
|
||||
// state so they follow address changes (DHCP renewals, interfaces coming
|
||||
// and going, IPv6 privacy address rotation).
|
||||
func (r *linuxRouter) onNetworkChange() {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
// Clear only once we hold mu: changes arriving from here on queue a
|
||||
// new reconcile, as this one may already have read older state.
|
||||
r.netChangePending.Store(false)
|
||||
if r.closed.Load() {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1198,6 +1198,52 @@ func TestIsIPRuleNotFound(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestQueueNetworkChangeCoalesces checks that the ChangeDelta callback never
|
||||
// blocks on r.mu and that a burst of changes while a reconcile is pending
|
||||
// collapses into that one reconcile.
|
||||
func TestQueueNetworkChangeCoalesces(t *testing.T) {
|
||||
lr, fake := newTestLinuxRouter(t)
|
||||
var calls atomic.Int32
|
||||
addrs := []netip.Addr{netip.MustParseAddr("192.168.1.5")}
|
||||
lr.mu.Lock()
|
||||
lr.underlayAddrs = func() []netip.Addr { calls.Add(1); return slices.Clone(addrs) }
|
||||
lr.mu.Unlock()
|
||||
if err := lr.Set(exitConfig("0.0.0.0/0")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
checkUnderlayRules(t, fake, "192.168.1.5")
|
||||
calls.Store(0)
|
||||
|
||||
// Hold mu, as a long Set would, and fire a burst of changes. Each
|
||||
// callback must return immediately.
|
||||
lr.mu.Lock()
|
||||
addrs = []netip.Addr{netip.MustParseAddr("192.168.1.77")}
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
for range 100 {
|
||||
lr.queueNetworkChange()
|
||||
}
|
||||
close(done)
|
||||
}()
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(5 * time.Second):
|
||||
lr.mu.Unlock()
|
||||
t.Fatal("queueNetworkChange blocked while r.mu was held")
|
||||
}
|
||||
lr.mu.Unlock()
|
||||
|
||||
waitFor(t, "coalesced reconcile", func() bool {
|
||||
lr.mu.Lock()
|
||||
defer lr.mu.Unlock()
|
||||
return !lr.netChangePending.Load() && lr.underlaySrcRules.Contains(underlayRule{fam: v4, src: netip.MustParseAddr("192.168.1.77")})
|
||||
})
|
||||
checkUnderlayRules(t, fake, "192.168.1.77")
|
||||
if n := calls.Load(); n != 1 {
|
||||
t.Errorf("burst of 100 changes ran %d reconciles; want 1", n)
|
||||
}
|
||||
}
|
||||
|
||||
// TestUnderlaySrcRulesBeforeExitRoute checks that enabling an exit node
|
||||
// installs the underlay rules before the default route, so there is no
|
||||
// window in which replies to tailscaled's sockets are dropped, and that
|
||||
|
||||
Reference in new issue
Block a user