Skip to content

Commit 7475a97

Browse files
committed
pkg/option: allow policy-filter-map-entries configurable via flag
This commit introduces a new flag to configure the number of entries in policy filter maps. This allows users to tune the map size based on workload scale and system resources, improving flexibility in policy handling. Note: this commit only affects policies with k8s segmentation primitives (i.e., either podSelectors or namespaced policies). Fixes: #4260 Signed-off-by: Kyle Dong <[email protected]>
1 parent 440f3aa commit 7475a97

File tree

8 files changed

+34
-2
lines changed

8 files changed

+34
-2
lines changed

bpf/process/policy_filter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct {
2020

2121
struct {
2222
__uint(type, BPF_MAP_TYPE_HASH_OF_MAPS);
23-
__uint(max_entries, POLICY_FILTER_MAX_POLICIES);
23+
__uint(max_entries, 1); // will be resized by agent when needed
2424
__type(key, u32); /* policy id */
2525
__array(
2626
values, struct {

docs/data/tetragon_flags.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/defaults/defaults.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ const (
5959

6060
// defaults for the {k,u}retprobes lru cache
6161
DefaultRetprobesCacheSize = 4096
62+
63+
// defaults for the policy filter map
64+
DefaultPolicyFilterMapEntries = 128
6265
)
6366

6467
var (

pkg/defaults/defaults_windows.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,7 @@ const (
5151

5252
// defaults for the {k,u}retprobes lru cache
5353
DefaultRetprobesCacheSize = 4096
54+
55+
// defaults for the policy filter map
56+
DefaultPolicyFilterMapEntries = 128
5457
)

pkg/option/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ type config struct {
130130
ExecveMapSize string
131131

132132
RetprobesCacheSize int
133+
134+
PolicyFilterMapEntries int
133135
}
134136

135137
var (
@@ -155,6 +157,9 @@ var (
155157

156158
// Set default value for {k,u}retprobes lru events cache
157159
RetprobesCacheSize: defaults.DefaultRetprobesCacheSize,
160+
161+
// set default value for the policy filter map
162+
PolicyFilterMapEntries: defaults.DefaultPolicyFilterMapEntries,
158163
}
159164
)
160165

pkg/option/flags.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ const (
135135
KeyExecveMapSize = "execve-map-size"
136136

137137
KeyRetprobesCacheSize = "retprobes-cache-size"
138+
139+
KeyPolicyFilterMapEntries = "policy-filter-map-entries"
138140
)
139141

140142
type UsernameMetadaCode int
@@ -289,6 +291,8 @@ func ReadAndSetFlags() error {
289291
Config.ExecveMapSize = viper.GetString(KeyExecveMapSize)
290292

291293
Config.RetprobesCacheSize = viper.GetInt(KeyRetprobesCacheSize)
294+
295+
Config.PolicyFilterMapEntries = viper.GetInt(KeyPolicyFilterMapEntries)
292296
return nil
293297
}
294298

@@ -483,4 +487,6 @@ func AddFlags(flags *pflag.FlagSet) {
483487
flags.String(KeyExecveMapSize, "", "Set size for execve_map table (allows K/M/G suffix)")
484488

485489
flags.Int(KeyRetprobesCacheSize, defaults.DefaultRetprobesCacheSize, "Set {k,u}retprobes events cache maximum size")
490+
491+
flags.Int(KeyPolicyFilterMapEntries, defaults.DefaultPolicyFilterMapEntries, "Set entries for policy_filter_map table (default 128)")
486492
}

pkg/policyfilter/map.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/cilium/tetragon/pkg/bpf"
1616
"github.com/cilium/tetragon/pkg/config"
17+
"github.com/cilium/tetragon/pkg/option"
1718
)
1819

1920
const (
@@ -72,6 +73,10 @@ func newPfMap(enableCgroupMap bool) (PfMap, error) {
7273
return PfMap{}, fmt.Errorf("loading spec for %s failed: %w", objPath, err)
7374
}
7475

76+
if _, ok := spec.Maps["policy_filter_maps"]; ok {
77+
spec.Maps["policy_filter_maps"].MaxEntries = uint32(option.Config.PolicyFilterMapEntries)
78+
}
79+
7580
var ret PfMap
7681
if ret.policyMap, err = openMap(spec, MapName, polMapSize); err != nil {
7782
return PfMap{}, fmt.Errorf("opening map %s failed: %w", MapName, err)
@@ -80,7 +85,7 @@ func newPfMap(enableCgroupMap bool) (PfMap, error) {
8085
if enableCgroupMap {
8186
if ret.cgroupMap, err = openMap(spec, CgroupMapName, polMaxPolicies); err != nil {
8287
releaseMap(ret.policyMap)
83-
return PfMap{}, fmt.Errorf("opening cgroup map %s failed: %w", MapName, err)
88+
return PfMap{}, fmt.Errorf("opening cgroup map %s failed: %w", CgroupMapName, err)
8489
}
8590
}
8691

pkg/sensors/program/loader_linux.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
cachedbtf "github.com/cilium/tetragon/pkg/btf"
1919
"github.com/cilium/tetragon/pkg/logger"
2020
"github.com/cilium/tetragon/pkg/logger/logfields"
21+
"github.com/cilium/tetragon/pkg/option"
2122
"github.com/cilium/tetragon/pkg/sensors/unloader"
2223
)
2324

@@ -976,6 +977,12 @@ func doLoadProgram(
976977
}
977978
}
978979

980+
// Set MaxEntries for policy_filter_maps if it exists in the spec.
981+
// This ensures the spec matches the user-defined value.
982+
if ms, ok := spec.Maps["policy_filter_maps"]; ok {
983+
ms.MaxEntries = uint32(option.Config.PolicyFilterMapEntries)
984+
}
985+
979986
// Find all the maps referenced by the program, so we'll rewrite only
980987
// the ones used.
981988
var progSpec *ebpf.ProgramSpec

0 commit comments

Comments
 (0)