Skip to content

Commit

Permalink
ctmap: limit DSR purge to CT entries with .dsr flag
Browse files Browse the repository at this point in the history
Clarify which CT entries potentially require purging of a DSR-related NAT
entry. This reduces the risk of accidentally purging unrelated NAT entries,
and allows the GC logic to do less work.

Signed-off-by: Julian Wiedmann <jwi@isovalent.com>
  • Loading branch information
julianwiedmann committed Oct 30, 2023
1 parent 57c5155 commit 4b15472
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
24 changes: 14 additions & 10 deletions pkg/maps/ctmap/ctmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func newMap(mapName string, m mapType) *Map {
return result
}

func purgeCtEntry6(m *Map, key CtKey, natMap *nat.Map) error {
func purgeCtEntry6(m *Map, key CtKey, entry *CtEntry, natMap *nat.Map) error {
err := m.Delete(key)
if err != nil || natMap == nil {
return err
Expand All @@ -289,8 +289,10 @@ func purgeCtEntry6(m *Map, key CtKey, natMap *nat.Map) error {
t := key.GetTupleKey()

if t.GetFlags()&tuple.TUPLE_F_IN != 0 {
// To delete NAT entries created by DSR
nat.DeleteSwappedMapping6(natMap, t.(*tuple.TupleKey6Global))
if entry.isDsrEntry() {
// To delete NAT entries created by DSR
nat.DeleteSwappedMapping6(natMap, t.(*tuple.TupleKey6Global))
}
} else {
nat.DeleteMapping6(natMap, t.(*tuple.TupleKey6Global))
}
Expand Down Expand Up @@ -348,7 +350,7 @@ func doGC6(m *Map, filter *GCFilter) gcStats {

switch action {
case deleteEntry:
err := purgeCtEntry6(m, currentKey6Global, natMap)
err := purgeCtEntry6(m, currentKey6Global, entry, natMap)
if err != nil {
log.WithError(err).WithField(logfields.Key, currentKey6Global.String()).Error("Unable to delete CT entry")
} else {
Expand All @@ -368,7 +370,7 @@ func doGC6(m *Map, filter *GCFilter) gcStats {

switch action {
case deleteEntry:
err := purgeCtEntry6(m, currentKey6, natMap)
err := purgeCtEntry6(m, currentKey6, entry, natMap)
if err != nil {
log.WithError(err).WithField(logfields.Key, currentKey6.String()).Error("Unable to delete CT entry")
} else {
Expand All @@ -389,7 +391,7 @@ func doGC6(m *Map, filter *GCFilter) gcStats {
return stats
}

func purgeCtEntry4(m *Map, key CtKey, natMap *nat.Map) error {
func purgeCtEntry4(m *Map, key CtKey, entry *CtEntry, natMap *nat.Map) error {
err := m.Delete(key)
if err != nil || natMap == nil {
return err
Expand All @@ -398,8 +400,10 @@ func purgeCtEntry4(m *Map, key CtKey, natMap *nat.Map) error {
t := key.GetTupleKey()

if t.GetFlags()&tuple.TUPLE_F_IN != 0 {
// To delete NAT entries created by DSR
nat.DeleteSwappedMapping4(natMap, t.(*tuple.TupleKey4Global))
if entry.isDsrEntry() {
// To delete NAT entries created by DSR
nat.DeleteSwappedMapping4(natMap, t.(*tuple.TupleKey4Global))
}
} else {
nat.DeleteMapping4(natMap, t.(*tuple.TupleKey4Global))
}
Expand Down Expand Up @@ -456,7 +460,7 @@ func doGC4(m *Map, filter *GCFilter) gcStats {

switch action {
case deleteEntry:
err := purgeCtEntry4(m, currentKey4Global, natMap)
err := purgeCtEntry4(m, currentKey4Global, entry, natMap)
if err != nil {
log.WithError(err).WithField(logfields.Key, currentKey4Global.String()).Error("Unable to delete CT entry")
} else {
Expand All @@ -476,7 +480,7 @@ func doGC4(m *Map, filter *GCFilter) gcStats {

switch action {
case deleteEntry:
err := purgeCtEntry4(m, currentKey4, natMap)
err := purgeCtEntry4(m, currentKey4, entry, natMap)
if err != nil {
log.WithError(err).WithField(logfields.Key, currentKey4.String()).Error("Unable to delete CT entry")
} else {
Expand Down
4 changes: 4 additions & 0 deletions pkg/maps/ctmap/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,10 @@ const (
MaxFlags
)

func (c *CtEntry) isDsrEntry() bool {
return c.Flags&DSR != 0
}

func (c *CtEntry) flagsString() string {
var sb strings.Builder

Expand Down

0 comments on commit 4b15472

Please sign in to comment.