policy/matcher, policy/v2: parse a via grant's sources once

Every router reparsed them for its matchers. via-members, 5000 nodes:
590 -> 11.7 ms, 555 -> 7.2 MB per pass.
This commit is contained in:
Kristoffer Dalby committed 2026-11-05 12:32:04 +01:00
1 parent 800392ae89
commit 91b920cc33
2 files changed
+21 -3

No files matched your search

+13 -1
View File
@@ -63,6 +63,13 @@ func MatchesFromFilterRules(rules []tailcfg.FilterRule) []Match {
// [policy.ReduceNodes], hiding the cap target
// from the source unless a companion IP-level rule also exists.
func MatchFromFilterRule(rule tailcfg.FilterRule) Match {
return MatchFromFilterRuleSrcs(rule, buildIPSet(rule.SrcIPs))
}
// MatchFromFilterRuleSrcs is [MatchFromFilterRule] for a rule whose SrcIPs
// are already parsed as srcs, see [ParseSrcs]: rules sharing one source
// list, such as a via grant's on every router, parse it once.
func MatchFromFilterRuleSrcs(rule tailcfg.FilterRule, srcs *netipx.IPSet) Match {
dests := new(netipx.IPSetBuilder)
for _, dp := range rule.DstPorts {
@@ -78,7 +85,12 @@ func MatchFromFilterRule(rule tailcfg.FilterRule) Match {
destsSet, _ := dests.IPSet()
return newMatch(buildIPSet(rule.SrcIPs), destsSet)
return newMatch(srcs, destsSet)
}
// ParseSrcs parses SrcIPs as [MatchFromFilterRule] does.
func ParseSrcs(srcIPs []string) *netipx.IPSet {
return buildIPSet(srcIPs)
}
// buildIPSet parses each string via [util.ParseIPSet] and unions the
+8 -2
View File
@@ -102,6 +102,9 @@ type viaGrantData struct {
hasAutoGroupInternet bool
internetProtocols []ProtocolPort
srcIPStrings []string
// srcSet is srcIPStrings parsed once for the matchers of the grant's
// rules, which every router of the grant shares.
srcSet *netipx.IPSet
// keyPrefix and keys, by IPProto, build the merge keys of the grant's
// rules once, see [viaRuleKey].
keyPrefix string
@@ -474,6 +477,7 @@ func (pol *Policy) compileOneViaGrant(
hasAutoGroupInternet: hasAutoGroupInternet,
internetProtocols: grant.InternetProtocols,
srcIPStrings: srcIPStrings,
srcSet: matcher.ParseSrcs(srcIPStrings),
keyPrefix: fmt.Sprintf("via:%v|%s|", grant.Via, strings.Join(srcIPStrings, ",")),
}
@@ -801,7 +805,7 @@ func peerRulesForNode(
// nodeOnlyMatchers returns the matchers of the self and via rules a node
// adds to the pre-compiled rules every node shares. Self matchers are
// shared per user; via rules are parsed for the node.
// shared per user; via rules reuse their grant's parsed sources.
func nodeOnlyMatchers(
grants []compiledGrant,
node types.NodeView,
@@ -818,7 +822,9 @@ func nodeOnlyMatchers(
case grantCategorySelf:
matchers = append(matchers, ownSelfRules(cg, node, userIdx).matchers...)
case grantCategoryVia:
matchers = append(matchers, matcher.MatchesFromFilterRules(viaRules(cg, node))...)
for _, rule := range viaRules(cg, node) {
matchers = append(matchers, matcher.MatchFromFilterRuleSrcs(rule, cg.via.srcSet))
}
}
}