policy/v2: build matchers once per compiled rule

Per-node matchers reuse the global ones and parse only self and via
rules, instead of every rule for every node.
This commit is contained in:
Kristoffer Dalby committed 2026-11-05 12:32:04 +01:00
1 parent 31b78b77ad
commit 137a66d59c
3 files changed
+92 -6

No files matched your search

+25
View File
@@ -805,6 +805,31 @@ func peerRulesForNode(
return rulesForNode(grants, node, userIdx, viaPeerRule)
}
// nodeOnlyRules returns the self and via rules a node adds to the
// pre-compiled rules every node shares.
func nodeOnlyRules(
grants []compiledGrant,
node types.NodeView,
userIdx userNodeIndex,
viaRules func(*compiledGrant, types.NodeView) []tailcfg.FilterRule,
) []tailcfg.FilterRule {
var rules []tailcfg.FilterRule
for i := range grants {
cg := &grants[i]
switch cg.category {
case grantCategoryRegular:
case grantCategorySelf:
rules = append(rules, compileAutogroupSelf(cg, node, userIdx)...)
case grantCategoryVia:
rules = append(rules, viaRules(cg, node)...)
}
}
return rules
}
func rulesForNode(
grants []compiledGrant,
node types.NodeView,
+14 -6
View File
@@ -926,8 +926,7 @@ func (pm *PolicyManager) BuildPeerMap(nodes views.Slice[types.NodeView]) map[typ
// but peer relationships require the full bidirectional access rules.
nodeMatchers := make(map[types.NodeID][]matcher.Match, nodes.Len())
for _, node := range nodes.All() {
unreduced := pm.peerRulesForNodeLocked(node)
nodeMatchers[node.ID()] = matcher.MatchesFromFilterRules(unreduced)
nodeMatchers[node.ID()] = pm.nodeMatchersLocked(node, viaPeerRule)
}
// Check each node pair for peer relationships.
@@ -1011,6 +1010,18 @@ func (pm *PolicyManager) filterRulesForNodeLocked(
)
}
// nodeMatchersLocked returns the global matchers plus matchers for the self
// and via rules node adds. Matchers do not depend on how rules merge, so the
// global ones, parsed once per compile, are shared rather than re-parsed per
// node.
func (pm *PolicyManager) nodeMatchersLocked(
node types.NodeView,
viaRules func(*compiledGrant, types.NodeView) []tailcfg.FilterRule,
) []matcher.Match {
return slices.Concat(pm.matchers, matcher.MatchesFromFilterRules(
nodeOnlyRules(pm.compiledGrants, node, pm.userNodeIdx, viaRules)))
}
// peerRulesForNodeLocked returns the rules that decide a node's peers, see
// [peerRulesForNode].
func (pm *PolicyManager) peerRulesForNodeLocked(
@@ -1106,10 +1117,7 @@ func (pm *PolicyManager) MatchersForNode(node types.NodeView) ([]matcher.Match,
return cached, nil
}
// For autogroup:self or via grants, derive matchers from
// the stored compiled grants for this specific node.
unreduced := pm.filterRulesForNodeLocked(node)
matchers := matcher.MatchesFromFilterRules(unreduced)
matchers := pm.nodeMatchersLocked(node, compileViaForNode)
if pm.cacheableLocked(node) {
pm.matchersForNodeMap.Store(node.ID(), matchers)
+53
View File
@@ -3613,6 +3613,9 @@ var benchPolicies = []struct {
{name: "via", policy: `{
"tagOwners": {"tag:router": ["u1@"]},
"grants": [{"src": ["u2@"], "dst": ["10.0.0.0/8"], "ip": ["*"], "via": ["tag:router"]}]}`, routerFiltered: true},
// Names every user node, so its sources grow with the tailnet.
{name: "member-all", policy: `{
"acls": [{"action": "accept", "src": ["autogroup:member"], "dst": ["*:*"]}]}`, routerFiltered: true},
}
// benchSetNodesPolicies is the subset of benchPolicies BenchmarkSetNodes
@@ -3688,6 +3691,56 @@ func BenchmarkBuildPeerMap(b *testing.B) {
}
}
// BenchmarkFilterForAllNodes measures FilterForNode for every node from a
// cold cache: the filter work one policy change costs.
func BenchmarkFilterForAllNodes(b *testing.B) {
for _, pol := range benchPolicies {
for _, n := range []int{100, 1000, 5000} {
b.Run(fmt.Sprintf("%s/n=%d", pol.name, n), func(b *testing.B) {
users, nodes := benchNodes(n)
pm, err := NewPolicyManager([]byte(pol.policy), users, nodes.ViewSlice())
require.NoError(b, err)
b.ReportAllocs()
for b.Loop() {
pm.filterRulesMap.Clear()
for _, node := range nodes {
_, err := pm.FilterForNode(node.View())
require.NoError(b, err)
}
}
})
}
}
}
// BenchmarkMatchersForAllNodes measures MatchersForNode for every node from
// a cold cache.
func BenchmarkMatchersForAllNodes(b *testing.B) {
for _, pol := range benchPolicies {
for _, n := range []int{100, 1000, 5000} {
b.Run(fmt.Sprintf("%s/n=%d", pol.name, n), func(b *testing.B) {
users, nodes := benchNodes(n)
pm, err := NewPolicyManager([]byte(pol.policy), users, nodes.ViewSlice())
require.NoError(b, err)
b.ReportAllocs()
for b.Loop() {
pm.matchersForNodeMap.Clear()
for _, node := range nodes {
_, err := pm.MatchersForNode(node.View())
require.NoError(b, err)
}
}
})
}
}
}
// BenchmarkViaRoutesForPeer measures the mean per-peer cost of one map
// response: a user node against every peer in turn. "via-mixed" adds
// ordinary ACLs next to the via grant.