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: Clean up interfaceLockMap entries on endpoint deletion #1249

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
9 changes: 1 addition & 8 deletions pkg/plugin/packetparser/packetparser_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ func (p *packetParser) Init() error {
}

p.tcMap = &sync.Map{}
p.interfaceLockMap = &sync.Map{}

return nil
}
Expand Down Expand Up @@ -380,24 +379,18 @@ func (p *packetParser) endpointWatcherCallbackFn(obj interface{}) {
}

iface := event.Obj.(netlink.LinkAttrs)

ifaceKey := ifaceToKey(iface)
lockMapVal, _ := p.interfaceLockMap.LoadOrStore(ifaceKey, &sync.Mutex{})
anubhabMajumdar marked this conversation as resolved.
Show resolved Hide resolved
mu := lockMapVal.(*sync.Mutex)
mu.Lock()
defer mu.Unlock()

switch event.Type {
case endpoint.EndpointCreated:
p.l.Debug("Endpoint created", zap.String("name", iface.Name))
p.createQdiscAndAttach(iface, Veth)
case endpoint.EndpointDeleted:
p.l.Debug("Endpoint deleted", zap.String("name", iface.Name))
// Clean.
// Clean tcMap.
if value, ok := p.tcMap.Load(ifaceKey); ok {
v := value.(*tcValue)
p.clean(v.tc, v.qdisc)
// Delete from map.
p.tcMap.Delete(ifaceKey)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

We need to delete the ifacekey from interfaceLockMap if it's deleted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reverted my latest changes, so I brought back the interfaceLockMap and removing the ifacekey from the map

default:
Expand Down
17 changes: 11 additions & 6 deletions pkg/plugin/packetparser/packetparser_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,22 @@ func TestEndpointWatcherCallbackFn_EndpointDeleted(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

// Initialize packetParser with both maps.
p := &packetParser{
cfg: cfgPodLevelEnabled,
l: log.Logger().Named("test"),
interfaceLockMap: &sync.Map{},
tcMap: &sync.Map{},
}
p.tcMap = &sync.Map{}

// Create test interface attributes.
linkAttr := netlink.LinkAttrs{
Name: "test",
HardwareAddr: []byte("test"),
NetNsID: 1,
}
key := ifaceToKey(linkAttr)

// Pre-populate the map to simulate existing interface
p.tcMap.Store(key, &tcValue{nil, &tc.Object{}})

// Create EndpointDeleted event.
Expand All @@ -182,10 +186,14 @@ func TestEndpointWatcherCallbackFn_EndpointDeleted(t *testing.T) {
Obj: linkAttr,
}

// Execute the callback.
p.endpointWatcherCallbackFn(e)

// Verify both maps are cleaned up.
_, ok := p.tcMap.Load(key)
assert.False(t, ok)

// Assert both maps are cleaned up
assert.False(t, ok, "tcMap entry should be deleted")
}

func TestCreateQdiscAndAttach(t *testing.T) {
Expand Down Expand Up @@ -227,7 +235,6 @@ func TestCreateQdiscAndAttach(t *testing.T) {
cfg: cfgPodLevelEnabled,
l: log.Logger().Named("test"),
objs: pObj,
interfaceLockMap: &sync.Map{},
endpointIngressInfo: &ebpf.ProgramInfo{
Name: "ingress",
},
Expand Down Expand Up @@ -417,7 +424,6 @@ func TestStartWithDataAggregationLevelLow(t *testing.T) {
objs: pObj,
reader: mockReader,
recordsChannel: make(chan perf.Record, buffer),
interfaceLockMap: &sync.Map{},
endpointIngressInfo: &ebpf.ProgramInfo{
Name: "ingress",
},
Expand Down Expand Up @@ -496,7 +502,6 @@ func TestStartWithDataAggregationLevelHigh(t *testing.T) {
objs: pObj,
reader: mockReader,
recordsChannel: make(chan perf.Record, buffer),
interfaceLockMap: &sync.Map{},
endpointIngressInfo: &ebpf.ProgramInfo{
Name: "ingress",
},
Expand Down
2 changes: 0 additions & 2 deletions pkg/plugin/packetparser/types_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ type packetParser struct {
tcMap *sync.Map
reader perfReader
enricher enricher.EnricherInterface
// interfaceLockMap is a map of key to *sync.Mutex.
interfaceLockMap *sync.Map
endpointIngressInfo *ebpf.ProgramInfo
endpointEgressInfo *ebpf.ProgramInfo
hostIngressInfo *ebpf.ProgramInfo
Expand Down
Loading