Skip to content

Commit 504add4

Browse files
authored
use cached api to retrieve interface name to avoid highcpu (#251)
Signed-off-by: Mohamed Mahmoud <[email protected]>
1 parent 3f14e8e commit 504add4

File tree

4 files changed

+20
-21
lines changed

4 files changed

+20
-21
lines changed

pkg/agent/agent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ func (f *Flows) buildAndStartPipeline(ctx context.Context) (*node.Terminal[[]*fl
422422
rbTracer.SendsTo(accounter)
423423

424424
if f.cfg.Deduper == DeduperFirstCome {
425-
deduper := node.AsMiddle(flow.Dedupe(f.cfg.DeduperFCExpiry, f.cfg.DeduperJustMark, f.cfg.DeduperMerge),
425+
deduper := node.AsMiddle(flow.Dedupe(f.cfg.DeduperFCExpiry, f.cfg.DeduperJustMark, f.cfg.DeduperMerge, f.interfaceNamer),
426426
node.ChannelBufferLen(f.cfg.BuffersLength))
427427
mapTracer.SendsTo(deduper)
428428
accounter.SendsTo(deduper)

pkg/flow/deduper.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/sirupsen/logrus"
99

1010
"github.com/netobserv/netobserv-ebpf-agent/pkg/ebpf"
11-
"github.com/netobserv/netobserv-ebpf-agent/pkg/utils"
1211
)
1312

1413
var dlog = logrus.WithField("component", "flow/Deduper")
@@ -40,7 +39,7 @@ type entry struct {
4039
// (no activity for it during the expiration time)
4140
// The justMark argument tells that the deduper should not drop the duplicate flows but
4241
// set their Duplicate field.
43-
func Dedupe(expireTime time.Duration, justMark, mergeDup bool) func(in <-chan []*Record, out chan<- []*Record) {
42+
func Dedupe(expireTime time.Duration, justMark, mergeDup bool, ifaceNamer InterfaceNamer) func(in <-chan []*Record, out chan<- []*Record) {
4443
cache := &deduperCache{
4544
expire: expireTime,
4645
entries: list.New(),
@@ -51,7 +50,7 @@ func Dedupe(expireTime time.Duration, justMark, mergeDup bool) func(in <-chan []
5150
cache.removeExpired()
5251
fwd := make([]*Record, 0, len(records))
5352
for _, record := range records {
54-
cache.checkDupe(record, justMark, mergeDup, &fwd)
53+
cache.checkDupe(record, justMark, mergeDup, &fwd, ifaceNamer)
5554
}
5655
if len(fwd) > 0 {
5756
out <- fwd
@@ -61,7 +60,7 @@ func Dedupe(expireTime time.Duration, justMark, mergeDup bool) func(in <-chan []
6160
}
6261

6362
// checkDupe check current record if its already available nad if not added to fwd records list
64-
func (c *deduperCache) checkDupe(r *Record, justMark, mergeDup bool, fwd *[]*Record) {
63+
func (c *deduperCache) checkDupe(r *Record, justMark, mergeDup bool, fwd *[]*Record, ifaceNamer InterfaceNamer) {
6564
mergeEntry := make(map[string]uint8)
6665
rk := r.Id
6766
// zeroes fields from key that should be ignored from the flow comparison
@@ -95,7 +94,7 @@ func (c *deduperCache) checkDupe(r *Record, justMark, mergeDup bool, fwd *[]*Rec
9594
*fwd = append(*fwd, r)
9695
}
9796
if mergeDup {
98-
ifName := utils.GetInterfaceName(r.Id.IfIndex)
97+
ifName := ifaceNamer(int(r.Id.IfIndex))
9998
mergeEntry[ifName] = r.Id.Direction
10099
if dupEntryNew(*fEntry.dupList, mergeEntry) {
101100
*fEntry.dupList = append(*fEntry.dupList, mergeEntry)
@@ -122,7 +121,7 @@ func (c *deduperCache) checkDupe(r *Record, justMark, mergeDup bool, fwd *[]*Rec
122121
expiryTime: timeNow().Add(c.expire),
123122
}
124123
if mergeDup {
125-
ifName := utils.GetInterfaceName(r.Id.IfIndex)
124+
ifName := ifaceNamer(int(r.Id.IfIndex))
126125
mergeEntry[ifName] = r.Id.Direction
127126
r.DupList = append(r.DupList, mergeEntry)
128127
e.dupList = &r.DupList

pkg/flow/deduper_test.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package flow
22

33
import (
4+
"net"
45
"testing"
56
"time"
67

78
"github.com/stretchr/testify/assert"
89

910
"github.com/netobserv/netobserv-ebpf-agent/pkg/ebpf"
10-
"github.com/netobserv/netobserv-ebpf-agent/pkg/utils"
1111
)
1212

1313
var (
@@ -70,7 +70,7 @@ func TestDedupe(t *testing.T) {
7070
input := make(chan []*Record, 100)
7171
output := make(chan []*Record, 100)
7272

73-
go Dedupe(time.Minute, false, false)(input, output)
73+
go Dedupe(time.Minute, false, false, interfaceNamer)(input, output)
7474

7575
input <- []*Record{
7676
oneIf2, // record 1 at interface 2: should be accepted
@@ -108,7 +108,7 @@ func TestDedupe_EvictFlows(t *testing.T) {
108108
input := make(chan []*Record, 100)
109109
output := make(chan []*Record, 100)
110110

111-
go Dedupe(15*time.Second, false, false)(input, output)
111+
go Dedupe(15*time.Second, false, false, interfaceNamer)(input, output)
112112

113113
// Should only accept records 1 and 2, at interface 1
114114
input <- []*Record{oneIf1, twoIf1, oneIf2}
@@ -143,7 +143,7 @@ func TestDedupeMerge(t *testing.T) {
143143
input := make(chan []*Record, 100)
144144
output := make(chan []*Record, 100)
145145

146-
go Dedupe(time.Minute, false, true)(input, output)
146+
go Dedupe(time.Minute, false, true, interfaceNamer)(input, output)
147147

148148
input <- []*Record{
149149
oneIf2, // record 1 at interface 2: should be accepted
@@ -155,10 +155,10 @@ func TestDedupeMerge(t *testing.T) {
155155

156156
expectedMap := []map[string]uint8{
157157
{
158-
utils.GetInterfaceName(oneIf2.Id.IfIndex): oneIf2.Id.Direction,
158+
interfaceNamer(int(oneIf2.Id.IfIndex)): oneIf2.Id.Direction,
159159
},
160160
{
161-
utils.GetInterfaceName(oneIf1.Id.IfIndex): oneIf1.Id.Direction,
161+
interfaceNamer(int(oneIf1.Id.IfIndex)): oneIf1.Id.Direction,
162162
},
163163
}
164164

@@ -174,3 +174,11 @@ type timerMock struct {
174174
func (tm *timerMock) Now() time.Time {
175175
return tm.now
176176
}
177+
178+
func interfaceNamer(ifIndex int) string {
179+
iface, err := net.InterfaceByIndex(ifIndex)
180+
if err != nil {
181+
return "unknown"
182+
}
183+
return iface.Name
184+
}

pkg/utils/utils.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,3 @@ func utsnameStr[T int8 | uint8](in []T) string {
9191
}
9292
return string(out)
9393
}
94-
95-
func GetInterfaceName(ifIndex uint32) string {
96-
iface, err := net.InterfaceByIndex(int(ifIndex))
97-
if err != nil {
98-
return "unknown"
99-
}
100-
return iface.Name
101-
}

0 commit comments

Comments
 (0)