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

Implement GC for per-cluster CT/SNAT maps #24576

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
56 changes: 46 additions & 10 deletions pkg/maps/ctmap/ctmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ type Map struct {
// define maps to the macro used in the datapath portion for the map
// name, for example 'CT_MAP4'.
define string

// This field indicates which cluster this ctmap is. Zero for global
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: We should probably start revising our usage of "global" to mean "the local cluster", since global sounds confusingly like all clusters. I know the naming is grandfathered in since it was referring to "globally across the node" instead of "per-endpoint CT" so the naming is not super easy here, but there's lots of opportunity to further confuse readers by continuing to use the global phrasing going forward.

// maps and non-zero for per-cluster maps.
clusterID uint32
}

// GCFilter contains the necessary fields to filter the CT maps.
Expand Down Expand Up @@ -339,12 +343,28 @@ func purgeCtEntry6(m *Map, key CtKey, natMap *nat.Map) error {
// doGC6 iterates through a CTv6 map and drops entries based on the given
// filter.
func doGC6(m *Map, filter *GCFilter) gcStats {
ctMap := mapInfo[m.mapType]
if ctMap.natMapLock != nil {
ctMap.natMapLock.Lock()
defer ctMap.natMapLock.Unlock()
var natMap *nat.Map

if m.clusterID == 0 {
// global map handling
ctMap := mapInfo[m.mapType]
if ctMap.natMapLock != nil {
ctMap.natMapLock.Lock()
defer ctMap.natMapLock.Unlock()
}
natMap = ctMap.natMap
} else {
// per-cluster map handling
if nat.PerClusterNATMaps != nil {
natm, err := nat.PerClusterNATMaps.GetClusterNATMap(m.clusterID, false)
if err != nil {
log.WithError(err).Error("Unable to get per-cluster NAT map")
} else {
natMap = natm
}
}
}
natMap := ctMap.natMap

stats := statStartGc(m)
defer stats.finish()

Expand Down Expand Up @@ -424,12 +444,28 @@ func purgeCtEntry4(m *Map, key CtKey, natMap *nat.Map) error {
// doGC4 iterates through a CTv4 map and drops entries based on the given
// filter.
func doGC4(m *Map, filter *GCFilter) gcStats {
ctMap := mapInfo[m.mapType]
if ctMap.natMapLock != nil {
ctMap.natMapLock.Lock()
defer ctMap.natMapLock.Unlock()
var natMap *nat.Map

if m.clusterID == 0 {
// global map handling
ctMap := mapInfo[m.mapType]
if ctMap.natMapLock != nil {
ctMap.natMapLock.Lock()
defer ctMap.natMapLock.Unlock()
}
natMap = ctMap.natMap
} else {
// per-cluster map handling
if nat.PerClusterNATMaps != nil {
natm, err := nat.PerClusterNATMaps.GetClusterNATMap(m.clusterID, true)
if err != nil {
log.WithError(err).Error("Unable to get per-cluster NAT map")
} else {
natMap = natm
}
}
}
natMap := ctMap.natMap

stats := statStartGc(m)
defer stats.finish()

Expand Down
12 changes: 12 additions & 0 deletions pkg/maps/ctmap/gc/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,18 @@ func runGC(e *endpoint.Endpoint, ipv4, ipv6, triggeredBySignal bool, filter *ctm

if e == nil {
maps = ctmap.GlobalMaps(ipv4, ipv6)

// We treat per-cluster CT Maps as global map. When we don't enable
// cluster-aware addressing, ctmap.PerClusterCTMaps is nil (this is
// the default).
if ctmap.PerClusterCTMaps != nil {
perClusterMaps, err := ctmap.PerClusterCTMaps.GetAllClusterCTMaps()
if err != nil {
log.Error("Failed to get per-cluster CT maps. Continue without them.")
} else {
maps = append(maps, perClusterMaps...)
}
}
} else {
maps = ctmap.LocalMaps(e, ipv4, ipv6)
}
Expand Down