Skip to content

Commit

Permalink
ctmap: unconditionally assume map to be LRU hash
Browse files Browse the repository at this point in the history
As of the previous commit we unconditionally assume support for LRU hash
BPF maps. Thus the map type dependent GC interval calculation for the
ctmap can be simplified to always assume LRU hash maps.

Signed-off-by: Tobias Klauser <tobias@cilium.io>
  • Loading branch information
tklauser committed Mar 15, 2023
1 parent 5139c1f commit 40d65b7
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 52 deletions.
3 changes: 0 additions & 3 deletions pkg/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,6 @@ const (
// ConntrackGCMaxLRUInterval is the maximum conntrack GC interval when using LRU maps
ConntrackGCMaxLRUInterval = 12 * time.Hour

// ConntrackGCMaxInterval is the maximum conntrack GC interval for non-LRU maps
ConntrackGCMaxInterval = 30 * time.Minute

// ConntrackGCMinInterval is the minimum conntrack GC interval
ConntrackGCMinInterval = 10 * time.Second

Expand Down
3 changes: 1 addition & 2 deletions pkg/fqdn/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@
// into the global cache for the same domain or a different one (or both).
// Note: The CT GC has a variable run period. This ranges from 30s to 12 hours
// and is shorter when more connection churn is observed (the constants are
// ConntrackGCMinInterval, ConntrackGCMaxInterval and ConntrackGCMaxLRUInterval
// in package defaults).
// ConntrackGCMinInterval and ConntrackGCMaxLRUInterval in package defaults).
//
// === Flow of DNS data ===
//
Expand Down
18 changes: 3 additions & 15 deletions pkg/maps/cidrmap/cidrmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,9 @@ func OpenMapElems(path string, prefixlen int, prefixdyn bool, maxelem uint32) (*
)

if err != nil {
log.Debug("Kernel does not support CIDR maps, using hash table instead.")
typeMap = bpf.MapTypeHash
fd, isNewMap, err = bpf.OpenOrCreateMap(
path,
typeMap,
uint32(unsafe.Sizeof(uint32(0))+uintptr(bytes)),
uint32(LPM_MAP_VALUE_SIZE),
maxelem,
bpf.BPF_F_NO_PREALLOC, 0, true,
)
if err != nil {
scopedLog := log.WithError(err).WithField(logfields.Path, path)
scopedLog.Warning("Failed to create CIDR map")
return nil, false, err
}
scopedLog := log.WithError(err).WithField(logfields.Path, path)
scopedLog.Warning("Failed to create CIDR map")
return nil, false, err
}

m := &CIDRMap{
Expand Down
18 changes: 5 additions & 13 deletions pkg/maps/ctmap/ctmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ var cachedGCInterval time.Duration

// GetInterval returns the interval adjusted based on the deletion ratio of the
// last run
func GetInterval(mapType bpf.MapType, maxDeleteRatio float64) (interval time.Duration) {
func GetInterval(maxDeleteRatio float64) (interval time.Duration) {
if val := option.Config.ConntrackGCInterval; val != time.Duration(0) {
interval = val
return
Expand All @@ -794,10 +794,10 @@ func GetInterval(mapType bpf.MapType, maxDeleteRatio float64) (interval time.Dur
interval = defaults.ConntrackGCStartingInterval
}

return calculateInterval(mapType, interval, maxDeleteRatio)
return calculateInterval(interval, maxDeleteRatio)
}

func calculateInterval(mapType bpf.MapType, prevInterval time.Duration, maxDeleteRatio float64) (interval time.Duration) {
func calculateInterval(prevInterval time.Duration, maxDeleteRatio float64) (interval time.Duration) {
interval = prevInterval

if maxDeleteRatio == 0.0 {
Expand All @@ -822,16 +822,8 @@ func calculateInterval(mapType bpf.MapType, prevInterval time.Duration, maxDelet
// as a new node may not be seeing workloads yet and thus the
// scan will return a low deletion ratio at first.
interval = time.Duration(float64(interval) * 1.5).Round(time.Second)

switch mapType {
case bpf.MapTypeLRUHash:
if interval > defaults.ConntrackGCMaxLRUInterval {
interval = defaults.ConntrackGCMaxLRUInterval
}
default:
if interval > defaults.ConntrackGCMaxInterval {
interval = defaults.ConntrackGCMaxInterval
}
if interval > defaults.ConntrackGCMaxLRUInterval {
interval = defaults.ConntrackGCMaxLRUInterval
}
}

Expand Down
21 changes: 9 additions & 12 deletions pkg/maps/ctmap/ctmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

. "gopkg.in/check.v1"

"github.com/cilium/cilium/pkg/bpf"
"github.com/cilium/cilium/pkg/defaults"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/tuple"
Expand Down Expand Up @@ -61,21 +60,19 @@ func (t *CTMapTestSuite) TestInit(c *C) {
}

func (t *CTMapTestSuite) TestCalculateInterval(c *C) {
c.Assert(calculateInterval(bpf.MapTypeLRUHash, time.Minute, 0.1), Equals, time.Minute) // no change
c.Assert(calculateInterval(bpf.MapTypeLRUHash, time.Minute, 0.2), Equals, time.Minute) // no change
c.Assert(calculateInterval(bpf.MapTypeLRUHash, time.Minute, 0.25), Equals, time.Minute) // no change
c.Assert(calculateInterval(time.Minute, 0.1), Equals, time.Minute) // no change
c.Assert(calculateInterval(time.Minute, 0.2), Equals, time.Minute) // no change
c.Assert(calculateInterval(time.Minute, 0.25), Equals, time.Minute) // no change

c.Assert(calculateInterval(bpf.MapTypeLRUHash, time.Minute, 0.40), Equals, 36*time.Second)
c.Assert(calculateInterval(bpf.MapTypeLRUHash, time.Minute, 0.60), Equals, 24*time.Second)
c.Assert(calculateInterval(time.Minute, 0.40), Equals, 36*time.Second)
c.Assert(calculateInterval(time.Minute, 0.60), Equals, 24*time.Second)

c.Assert(calculateInterval(bpf.MapTypeLRUHash, 10*time.Second, 0.01), Equals, 15*time.Second)
c.Assert(calculateInterval(bpf.MapTypeLRUHash, 10*time.Second, 0.04), Equals, 15*time.Second)
c.Assert(calculateInterval(10*time.Second, 0.01), Equals, 15*time.Second)
c.Assert(calculateInterval(10*time.Second, 0.04), Equals, 15*time.Second)

c.Assert(calculateInterval(bpf.MapTypeLRUHash, 1*time.Second, 0.9), Equals, defaults.ConntrackGCMinInterval)
c.Assert(calculateInterval(bpf.MapTypeHash, 1*time.Second, 0.9), Equals, defaults.ConntrackGCMinInterval)
c.Assert(calculateInterval(1*time.Second, 0.9), Equals, defaults.ConntrackGCMinInterval)

c.Assert(calculateInterval(bpf.MapTypeLRUHash, 24*time.Hour, 0.01), Equals, defaults.ConntrackGCMaxLRUInterval)
c.Assert(calculateInterval(bpf.MapTypeHash, 24*time.Hour, 0.01), Equals, defaults.ConntrackGCMaxInterval)
c.Assert(calculateInterval(24*time.Hour, 0.01), Equals, defaults.ConntrackGCMaxLRUInterval)
}

func (t *CTMapTestSuite) TestFilterMapsByProto(c *C) {
Expand Down
10 changes: 3 additions & 7 deletions pkg/maps/ctmap/gc/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/sirupsen/logrus"

"github.com/cilium/cilium/pkg/bpf"
"github.com/cilium/cilium/pkg/datapath/types"
"github.com/cilium/cilium/pkg/endpoint"
"github.com/cilium/cilium/pkg/inctimer"
Expand Down Expand Up @@ -40,7 +39,6 @@ func Enable(ipv4, ipv6 bool, restoredEndpoints []*endpoint.Endpoint, mgr Endpoin
var (
initialScan = true
initialScanComplete = make(chan struct{})
mapType bpf.MapType
)

go func() {
Expand Down Expand Up @@ -94,7 +92,7 @@ func Enable(ipv4, ipv6 bool, restoredEndpoints []*endpoint.Endpoint, mgr Endpoin

if len(eps) > 0 || initialScan {
gcFilter := createGCFilter(initialScan, restoredEndpoints, emitEntryCB, nodeAddressing)
mapType, maxDeleteRatio = runGC(nil, ipv4, ipv6, triggeredBySignal, gcFilter)
maxDeleteRatio = runGC(nil, ipv4, ipv6, triggeredBySignal, gcFilter)
}
for _, e := range eps {
if !e.ConntrackLocal() {
Expand Down Expand Up @@ -139,7 +137,7 @@ func Enable(ipv4, ipv6 bool, restoredEndpoints []*endpoint.Endpoint, mgr Endpoin
ipv6 = true
}
}
case <-ctTimer.After(ctmap.GetInterval(mapType, maxDeleteRatio)):
case <-ctTimer.After(ctmap.GetInterval(maxDeleteRatio)):
ipv4 = ipv4Orig
ipv6 = ipv6Orig
}
Expand All @@ -163,7 +161,7 @@ func Enable(ipv4, ipv6 bool, restoredEndpoints []*endpoint.Endpoint, mgr Endpoin
// The provided endpoint is optional; if it is provided, then its map will be
// garbage collected and any failures will be logged to the endpoint log.
// Otherwise it will garbage-collect the global map and use the global log.
func runGC(e *endpoint.Endpoint, ipv4, ipv6, triggeredBySignal bool, filter *ctmap.GCFilter) (mapType bpf.MapType, maxDeleteRatio float64) {
func runGC(e *endpoint.Endpoint, ipv4, ipv6, triggeredBySignal bool, filter *ctmap.GCFilter) (maxDeleteRatio float64) {
var maps []*ctmap.Map

if e == nil {
Expand Down Expand Up @@ -191,8 +189,6 @@ func runGC(e *endpoint.Endpoint, ipv4, ipv6, triggeredBySignal bool, filter *ctm
}
defer m.Close()

mapType = m.MapInfo.MapType

deleted := ctmap.GC(m, filter)

if deleted > 0 {
Expand Down

0 comments on commit 40d65b7

Please sign in to comment.