Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CIDR labels computation #28788

Merged
merged 5 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 7 additions & 8 deletions pkg/labels/cidr.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ func GetCIDRLabels(prefix netip.Prefix) Labels {
nil, // avoid allocating space for the intermediate results until we need it
addr,
ones,
0,
)
addWorldLabel(addr, lbls)

Expand All @@ -134,7 +133,7 @@ var (
mu lock.Mutex
)

const cidrLabelsCacheMaxSize = 16384
const cidrLabelsCacheMaxSize = 8192

func addWorldLabel(addr netip.Addr, lbls Labels) {
switch {
Expand All @@ -155,12 +154,12 @@ var (
worldLabelV6 = Label{Source: LabelSourceReserved, Key: IDNameWorldIPv6}
)

func computeCIDRLabels(cache *simplelru.LRU[netip.Prefix, []Label], lbls Labels, results []Label, addr netip.Addr, ones, i int) []Label {
if i > ones {
func computeCIDRLabels(cache *simplelru.LRU[netip.Prefix, []Label], lbls Labels, results []Label, addr netip.Addr, ones int) []Label {
if ones < 0 {
return results
}

prefix := netip.PrefixFrom(addr, i)
prefix, _ := addr.Prefix(ones)

mu.Lock()
cachedLbls, ok := cache.Get(prefix)
Expand All @@ -177,20 +176,20 @@ func computeCIDRLabels(cache *simplelru.LRU[netip.Prefix, []Label], lbls Labels,
}

// Compute the label for this prefix (e.g. "cidr:10.0.0.0/8")
prefixLabel := maskedIPToLabel(prefix.Masked().Addr(), i)
prefixLabel := maskedIPToLabel(prefix.Addr(), ones)
lbls[prefixLabel.Key] = prefixLabel

// Keep computing the rest (e.g. "cidr:10.0.0.0/7", ...).
results = computeCIDRLabels(
cache,
lbls,
append(results, prefixLabel),
addr, ones, i+1,
prefix.Addr(), ones-1,
)

// Cache the resulting labels derived from this prefix, e.g. /8, /7, ...
mu.Lock()
cache.Add(prefix, results[i:])
cache.Add(prefix, results[len(results)-ones-1:])
mu.Unlock()

return results
Expand Down