Skip to content

Commit 7e7a703

Browse files
kyledong-susemtardy
authored andcommitted
pkg/sensors: reduce socktrack map memory footprint
Resize the socktrack_map if needed to save memory. Prevent ~2.8MB of unnecessary per-policy memory allocation when socktrack_map is unused. Signed-off-by: Kyle Dong <[email protected]>
1 parent e5a23da commit 7e7a703

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

bpf/process/types/basic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2052,7 +2052,7 @@ struct socket_owner {
20522052
// socktrack_map maintains a mapping of sock to pid_tgid
20532053
struct {
20542054
__uint(type, BPF_MAP_TYPE_LRU_HASH);
2055-
__uint(max_entries, 32000);
2055+
__uint(max_entries, 1); // will be resized by agent when needed
20562056
__type(key, __u64);
20572057
__type(value, struct socket_owner);
20582058
} socktrack_map SEC(".maps");

pkg/selectors/kernel.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,33 @@ func HasStackTrace(selectors []v1alpha1.KProbeSelector) bool {
16821682
return false
16831683
}
16841684

1685+
func HasSockTrack(spec *v1alpha1.KProbeSpec) bool {
1686+
// Check ReturnArgAction
1687+
if spec.ReturnArgAction != "" {
1688+
if a := ActionTypeFromString(spec.ReturnArgAction); a == ActionTypeTrackSock ||
1689+
a == ActionTypeUntrackSock {
1690+
return true
1691+
}
1692+
}
1693+
1694+
// Check selectors MatchActions and MatchReturnActions
1695+
for _, selector := range spec.Selectors {
1696+
for _, matchAction := range selector.MatchActions {
1697+
if a := ActionTypeFromString(matchAction.Action); a == ActionTypeTrackSock ||
1698+
a == ActionTypeUntrackSock {
1699+
return true
1700+
}
1701+
}
1702+
for _, matchReturnAction := range selector.MatchReturnActions {
1703+
if a := ActionTypeFromString(matchReturnAction.Action); a == ActionTypeTrackSock ||
1704+
a == ActionTypeUntrackSock {
1705+
return true
1706+
}
1707+
}
1708+
}
1709+
return false
1710+
}
1711+
16851712
// parseCapabilitiesMask create a capabilities mask
16861713
func parseCapabilitiesMask(s string) (uint64, error) {
16871714
mask, err := strconv.ParseUint(s, 0, 64)

pkg/sensors/tracing/consts.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ const (
1717
enforcerMapMaxEntries = 32768
1818
overrideMapMaxEntries = 32768
1919
sleepableOffloadMaxEntries = 32768
20+
socktrackMapMaxEntries = 32000
2021
)

pkg/sensors/tracing/generickprobe.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,9 @@ func createMultiKprobeSensor(polInfo *policyInfo, multiIDs []idtable.EntryID, ha
319319

320320
if config.EnableLargeProgs() {
321321
socktrack := program.MapBuilderSensor("socktrack_map", load)
322+
if has.sockTrack {
323+
socktrack.SetMaxEntries(socktrackMapMaxEntries)
324+
}
322325
maps = append(maps, socktrack)
323326
}
324327

@@ -382,6 +385,9 @@ func createMultiKprobeSensor(polInfo *policyInfo, multiIDs []idtable.EntryID, ha
382385
maps = append(maps, fdinstall)
383386

384387
socktrack := program.MapBuilderSensor("socktrack_map", loadret)
388+
if has.sockTrack {
389+
socktrack.SetMaxEntries(socktrackMapMaxEntries)
390+
}
385391
maps = append(maps, socktrack)
386392

387393
tailCalls := program.MapBuilderSensor("retkprobe_calls", loadret)
@@ -574,6 +580,7 @@ type hasMaps struct {
574580
fdInstall bool
575581
enforcer bool
576582
override bool
583+
sockTrack bool
577584
}
578585

579586
// hasMapsSetup setups the has maps for the per policy maps. The per kprobe maps
@@ -584,6 +591,7 @@ func hasMapsSetup(spec *v1alpha1.TracingPolicySpec) hasMaps {
584591
has.fdInstall = has.fdInstall || selectors.HasFDInstall(kprobe.Selectors)
585592
has.enforcer = has.enforcer || len(spec.Enforcers) != 0
586593
has.rateLimit = has.rateLimit || selectors.HasRateLimit(kprobe.Selectors)
594+
has.sockTrack = has.sockTrack || selectors.HasSockTrack(&kprobe)
587595
}
588596
return has
589597
}
@@ -1041,6 +1049,9 @@ func createKprobeSensorFromEntry(polInfo *policyInfo, kprobeEntry *genericKprobe
10411049

10421050
if config.EnableLargeProgs() {
10431051
socktrack := program.MapBuilderSensor("socktrack_map", load)
1052+
if has.sockTrack {
1053+
socktrack.SetMaxEntries(socktrackMapMaxEntries)
1054+
}
10441055
maps = append(maps, socktrack)
10451056
}
10461057

@@ -1112,6 +1123,9 @@ func createKprobeSensorFromEntry(polInfo *policyInfo, kprobeEntry *genericKprobe
11121123

11131124
if config.EnableLargeProgs() {
11141125
socktrack := program.MapBuilderSensor("socktrack_map", loadret)
1126+
if has.sockTrack {
1127+
socktrack.SetMaxEntries(socktrackMapMaxEntries)
1128+
}
11151129
maps = append(maps, socktrack)
11161130
}
11171131
}

0 commit comments

Comments
 (0)