Skip to content

Commit d62cd19

Browse files
committed
Test
Signed-off-by: Anastasios Papagiannis <[email protected]>
1 parent 93fb03c commit d62cd19

File tree

6 files changed

+263
-34
lines changed

6 files changed

+263
-34
lines changed

bpf/process/policy_filter.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#define POLICY_FILTER_MAX_POLICIES 128
1010
#define POLICY_FILTER_MAX_NAMESPACES 1024
11+
#define POLICY_FILTER_MAX_CGROUP_IDS 512
1112

1213
struct {
1314
__uint(type, BPF_MAP_TYPE_LRU_HASH);
@@ -29,17 +30,39 @@ struct {
2930
});
3031
} policy_filter_maps SEC(".maps");
3132

33+
// This map keeps exactly the same information as policy_filter_maps
34+
// but keeps the reverse mappings. i.e.
35+
// policy_filter_maps maps policy_id to cgroup_ids
36+
// policy_filter_reverse_maps maps cgroup_id to policy_ids
37+
struct {
38+
__uint(type, BPF_MAP_TYPE_HASH_OF_MAPS);
39+
__uint(max_entries, POLICY_FILTER_MAX_CGROUP_IDS);
40+
__uint(key_size, sizeof(__u64)); /* cgroup id */
41+
__array(
42+
values, struct {
43+
__uint(type, BPF_MAP_TYPE_HASH);
44+
__uint(max_entries, POLICY_FILTER_MAX_POLICIES);
45+
__type(key, __u32); /* policy id */
46+
__type(value, __u8); /* empty */
47+
});
48+
} policy_filter_reverse_maps SEC(".maps");
49+
3250
// policy_filter_check checks whether the policy applies on the current process.
3351
// Returns true if it does, false otherwise.
3452

3553
FUNC_INLINE bool policy_filter_check(u32 policy_id)
3654
{
3755
void *policy_map;
38-
__u64 cgroupid;
56+
__u64 cgroupid = 0;
3957

4058
if (!policy_id)
4159
return true;
4260

61+
// we just want to make sure that policy_filter_reverse_maps
62+
// is part of the object file in order to read the map
63+
// spec from the user space
64+
map_lookup_elem(&policy_filter_reverse_maps, &cgroupid);
65+
4366
policy_map = map_lookup_elem(&policy_filter_maps, &policy_id);
4467
if (!policy_map)
4568
return false;

cmd/tetra/debug/dump.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,18 +181,33 @@ func PolicyfilterState(fname string) {
181181
return
182182
}
183183

184-
if len(data) == 0 {
184+
fmt.Println("--- Direct Map ---")
185+
186+
if len(data.Direct) == 0 {
185187
fmt.Printf("(empty)\n")
186-
return
187188
}
188189

189-
for polId, cgIDs := range data {
190+
for polId, cgIDs := range data.Direct {
190191
ids := make([]string, 0, len(cgIDs))
191192
for id := range cgIDs {
192193
ids = append(ids, strconv.FormatUint(uint64(id), 10))
193194
}
194195
fmt.Printf("%d: %s\n", polId, strings.Join(ids, ","))
195196
}
197+
198+
fmt.Println("--- Reverse Map ---")
199+
200+
if len(data.Reverse) == 0 {
201+
fmt.Printf("(empty)\n")
202+
}
203+
204+
for cgIDs, polIds := range data.Reverse {
205+
ids := make([]string, 0, len(polIds))
206+
for id := range polIds {
207+
ids = append(ids, strconv.FormatUint(uint64(id), 10))
208+
}
209+
fmt.Printf("%d: %s\n", cgIDs, strings.Join(ids, ","))
210+
}
196211
}
197212

198213
func NamespaceState(fname string) error {

cmd/tetra/policyfilter/policyfilter.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ func dumpCmd() *cobra.Command {
8989
func addCommand() *cobra.Command {
9090
var argType string
9191
mapFname := filepath.Join(defaults.DefaultMapRoot, defaults.DefaultMapPrefix, policyfilter.MapName)
92+
mapRevFname := filepath.Join(defaults.DefaultMapRoot, defaults.DefaultMapPrefix, policyfilter.RevMapName)
9293
ret := &cobra.Command{
9394
Use: "add [policy id] [cgroup]",
9495
Short: "add policyfilter entry",
@@ -121,6 +122,7 @@ func addCommand() *cobra.Command {
121122
flags := ret.Flags()
122123
flags.StringVar(&argType, "arg-type", "file", "cgroup type (id,file)")
123124
flags.StringVar(&mapFname, "map-fname", mapFname, "policyfilter map filename")
125+
flags.StringVar(&mapRevFname, "map-rev-fname", mapRevFname, "policyfilterReverse map filename")
124126
return ret
125127
}
126128

0 commit comments

Comments
 (0)