@ -105,6 +105,7 @@ type userspaceEngine struct {
lastEngineSigFull string // of full wireguard config
lastEngineSigFull string // of full wireguard config
lastEngineSigTrim string // of trimmed wireguard config
lastEngineSigTrim string // of trimmed wireguard config
recvActivityAt map [ tailcfg . DiscoKey ] time . Time
recvActivityAt map [ tailcfg . DiscoKey ] time . Time
trimmedDisco map [ tailcfg . DiscoKey ] bool // set of disco keys of peers currently excluded from wireguard config
sentActivityAt map [ packet . IP ] * int64 // value is atomic int64 of unixtime
sentActivityAt map [ packet . IP ] * int64 // value is atomic int64 of unixtime
destIPActivityFuncs map [ packet . IP ] func ( )
destIPActivityFuncs map [ packet . IP ] func ( )
@ -636,9 +637,11 @@ func (e *userspaceEngine) noteReceiveActivity(dk tailcfg.DiscoKey) {
e . wgLock . Lock ( )
e . wgLock . Lock ( )
defer e . wgLock . Unlock ( )
defer e . wgLock . Unlock ( )
was , ok := e . recvActivityAt [ dk ]
if _ , ok := e . recvActivityAt [ dk ] ; ! ok {
if ! ok {
// Not a trimmable peer we care about tracking. (See isTrimmablePeer)
// Not a trimmable peer we care about tracking. (See isTrimmablePeer)
if e . trimmedDisco [ dk ] {
e . logf ( "wgengine: [unexpected] noteReceiveActivity called on idle discokey %v that's not in recvActivityAt" , dk . ShortString ( ) )
}
return
return
}
}
now := e . timeNow ( )
now := e . timeNow ( )
@ -650,7 +653,8 @@ func (e *userspaceEngine) noteReceiveActivity(dk tailcfg.DiscoKey) {
// lazyPeerIdleThreshold without the divide by 2, but
// lazyPeerIdleThreshold without the divide by 2, but
// maybeReconfigWireguardLocked is cheap enough to call every
// maybeReconfigWireguardLocked is cheap enough to call every
// couple minutes (just not on every packet).
// couple minutes (just not on every packet).
if was . IsZero ( ) || now . Sub ( was ) > lazyPeerIdleThreshold / 2 {
if e . trimmedDisco [ dk ] {
e . logf ( "wgengine: idle peer %v now active, reconfiguring wireguard" , dk . ShortString ( ) )
e . maybeReconfigWireguardLocked ( )
e . maybeReconfigWireguardLocked ( )
}
}
}
}
@ -718,6 +722,8 @@ func (e *userspaceEngine) maybeReconfigWireguardLocked() error {
trackDisco := make ( [ ] tailcfg . DiscoKey , 0 , len ( full . Peers ) )
trackDisco := make ( [ ] tailcfg . DiscoKey , 0 , len ( full . Peers ) )
trackIPs := make ( [ ] wgcfg . IP , 0 , len ( full . Peers ) )
trackIPs := make ( [ ] wgcfg . IP , 0 , len ( full . Peers ) )
trimmedDisco := map [ tailcfg . DiscoKey ] bool { } // TODO: don't re-alloc this map each time
for i := range full . Peers {
for i := range full . Peers {
p := & full . Peers [ i ]
p := & full . Peers [ i ]
if ! isTrimmablePeer ( p , len ( full . Peers ) ) {
if ! isTrimmablePeer ( p , len ( full . Peers ) ) {
@ -730,6 +736,8 @@ func (e *userspaceEngine) maybeReconfigWireguardLocked() error {
trackIPs = append ( trackIPs , tsIP )
trackIPs = append ( trackIPs , tsIP )
if e . isActiveSince ( dk , tsIP , activeCutoff ) {
if e . isActiveSince ( dk , tsIP , activeCutoff ) {
min . Peers = append ( min . Peers , * p )
min . Peers = append ( min . Peers , * p )
} else {
trimmedDisco [ dk ] = true
}
}
}
}
@ -738,6 +746,8 @@ func (e *userspaceEngine) maybeReconfigWireguardLocked() error {
return nil
return nil
}
}
e . trimmedDisco = trimmedDisco
e . updateActivityMapsLocked ( trackDisco , trackIPs )
e . updateActivityMapsLocked ( trackDisco , trackIPs )
e . logf ( "wgengine: Reconfig: configuring userspace wireguard config (with %d/%d peers)" , len ( min . Peers ) , len ( full . Peers ) )
e . logf ( "wgengine: Reconfig: configuring userspace wireguard config (with %d/%d peers)" , len ( min . Peers ) , len ( full . Peers ) )