From 91b920cc33c79e91fb14144af280a2cddade40d3 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Wed, 4 Nov 2026 16:27:12 +0000 Subject: [PATCH] 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. --- hscontrol/policy/matcher/matcher.go | 14 +++++++++++++- hscontrol/policy/v2/compiled.go | 10 ++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/hscontrol/policy/matcher/matcher.go b/hscontrol/policy/matcher/matcher.go index f03c6d83a..92e779a8e 100644 --- a/hscontrol/policy/matcher/matcher.go +++ b/hscontrol/policy/matcher/matcher.go @@ -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 diff --git a/hscontrol/policy/v2/compiled.go b/hscontrol/policy/v2/compiled.go index 864de6f7a..337f44aba 100644 --- a/hscontrol/policy/v2/compiled.go +++ b/hscontrol/policy/v2/compiled.go @@ -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)) + } } }