wgengine/netlog: stop using netmap.NetworkMap type, use LocalBackend

The Logger previously took a *netmap.NetworkMap at Startup and on every
ReconfigNetworkMap call, denormalizing it into per-IP and self lookup
maps. That denormalization is O(n) over all peers and ran on every
netmap update, contributing to the broader quadratic behavior we want
to eliminate when a single peer is added or removed.

Instead, this makes netlog ask LocalBackend (well, nodeBackend) for
the info it needs, letting us remove the netmap.NetworkMap type
entirely from the netlog package.

This is a dependency to removing the netmap.NetworkMap type from
upstream callers, like wgengine.Engine in general.

Updates #12542

Change-Id: Ib5f2de96e788a667332c0a6f7ac833b3d0053b5c
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2026-06-17 15:11:57 -07:00
committed by Brad Fitzpatrick
parent 994b2c8459
commit 8f210454dd
13 changed files with 180 additions and 83 deletions
+66 -12
View File
@@ -21,22 +21,74 @@ import (
"tailscale.com/types/bools"
"tailscale.com/types/ipproto"
"tailscale.com/types/netlogtype"
"tailscale.com/types/netmap"
"tailscale.com/wgengine/router"
)
// nodeAndUser pairs a [tailcfg.NodeView] with its owning user profile.
type nodeAndUser struct {
node tailcfg.NodeView
user tailcfg.UserProfileView
}
// fakeNodeSource is a [NodeSource] implementation backed by static maps,
// for tests.
type fakeNodeSource struct {
self tailcfg.NodeView
selfUser tailcfg.UserProfileView
byAddr map[netip.Addr]nodeAndUser
}
func (s *fakeNodeSource) SelfNode() (tailcfg.NodeView, tailcfg.UserProfileView) {
return s.self, s.selfUser
}
func (s *fakeNodeSource) NodeByAddr(a netip.Addr) (_ tailcfg.NodeView, _ tailcfg.UserProfileView, ok bool) {
nu, ok := s.byAddr[a]
return nu.node, nu.user, ok
}
// newFakeNodeSource builds a [fakeNodeSource] from a self [tailcfg.NodeView],
// a list of peer [tailcfg.NodeView] values, and a UserProfile map.
// All single-IP addresses on self and the peers are indexed for NodeByAddr.
func newFakeNodeSource(self tailcfg.NodeView, peers []tailcfg.NodeView, users map[tailcfg.UserID]tailcfg.UserProfileView) *fakeNodeSource {
s := &fakeNodeSource{
byAddr: map[netip.Addr]nodeAndUser{},
}
if self.Valid() {
s.self = self
s.selfUser = users[self.User()]
for _, p := range self.Addresses().All() {
if p.IsSingleIP() {
s.byAddr[p.Addr()] = nodeAndUser{self, s.selfUser}
}
}
}
for _, peer := range peers {
if !peer.Valid() {
continue
}
pu := users[peer.User()]
for _, p := range peer.Addresses().All() {
if p.IsSingleIP() {
s.byAddr[p.Addr()] = nodeAndUser{peer, pu}
}
}
}
return s
}
func TestEmbedNodeInfo(t *testing.T) {
// Initialize the logger with a particular view of the netmap.
// Initialize the logger with a particular view of the node state.
var logger Logger
logger.ReconfigNetworkMap(&netmap.NetworkMap{
SelfNode: (&tailcfg.Node{
logger.source = newFakeNodeSource(
(&tailcfg.Node{
StableID: "n123456CNTL",
ID: 123456,
Name: "test.tail123456.ts.net",
Addresses: []netip.Prefix{prefix("100.1.2.3")},
Tags: []string{"tag:foo", "tag:bar"},
}).View(),
Peers: []tailcfg.NodeView{
[]tailcfg.NodeView{
(&tailcfg.Node{
StableID: "n123457CNTL",
ID: 123457,
@@ -52,10 +104,10 @@ func TestEmbedNodeInfo(t *testing.T) {
User: 54321,
}).View(),
},
UserProfiles: map[tailcfg.UserID]tailcfg.UserProfileView{
map[tailcfg.UserID]tailcfg.UserProfileView{
54321: (&tailcfg.UserProfile{ID: 54321, LoginName: "peer@example.com"}).View(),
},
})
)
logger.ReconfigRoutes(&router.Config{
SubnetRoutes: []netip.Prefix{
prefix("172.16.1.1/16"),
@@ -148,6 +200,9 @@ func TestEmbedNodeInfo(t *testing.T) {
func TestUpdateRace(t *testing.T) {
var logger Logger
// Install an empty fake source so NodeByAddr / SelfNode are exercised on
// the lookup path without returning any matches.
logger.source = &fakeNodeSource{}
logger.recordsChan = make(chan record, 1)
go func(recordsChan chan record) {
for range recordsChan {
@@ -167,11 +222,6 @@ func TestUpdateRace(t *testing.T) {
}
}
})
group.Go(func() {
for range 1000 {
logger.ReconfigNetworkMap(new(netmap.NetworkMap))
}
})
group.Go(func() {
for range 1000 {
logger.ReconfigRoutes(new(router.Config))
@@ -194,6 +244,7 @@ func randAddrPort() netip.AddrPort {
func TestAutoFlushMaxConns(t *testing.T) {
var logger Logger
logger.source = &fakeNodeSource{}
logger.recordsChan = make(chan record, 1)
for i := 0; len(logger.recordsChan) == 0; i++ {
logger.updateVirtConn(0, netip.AddrPortFrom(netip.Addr{}, uint16(i)), netip.AddrPort{}, 1, 1, false)
@@ -206,6 +257,7 @@ func TestAutoFlushMaxConns(t *testing.T) {
func TestAutoFlushTimeout(t *testing.T) {
var logger Logger
logger.source = &fakeNodeSource{}
logger.recordsChan = make(chan record, 1)
synctest.Test(t, func(t *testing.T) {
logger.updateVirtConn(0, netip.AddrPort{}, netip.AddrPort{}, 1, 1, false)
@@ -222,6 +274,7 @@ func TestAutoFlushTimeout(t *testing.T) {
func BenchmarkUpdateSameConn(b *testing.B) {
var logger Logger
logger.source = &fakeNodeSource{}
b.ReportAllocs()
for range b.N {
logger.updateVirtConn(0, netip.AddrPort{}, netip.AddrPort{}, 1, 1, false)
@@ -230,6 +283,7 @@ func BenchmarkUpdateSameConn(b *testing.B) {
func BenchmarkUpdateNewConns(b *testing.B) {
var logger Logger
logger.source = &fakeNodeSource{}
b.ReportAllocs()
for i := range b.N {
logger.updateVirtConn(0, netip.AddrPortFrom(netip.Addr{}, uint16(i)), netip.AddrPort{}, 1, 1, false)