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

Add tetra policyfilter listpolicies command #3122

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions bpf/process/policy_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#define POLICY_FILTER_MAX_POLICIES 128
#define POLICY_FILTER_MAX_NAMESPACES 1024
#define POLICY_FILTER_MAX_CGROUP_IDS 32768 /* same as polMapSize in policyfilter/state.go */

struct {
__uint(type, BPF_MAP_TYPE_LRU_HASH);
Expand All @@ -30,6 +31,23 @@ struct {
});
} policy_filter_maps SEC(".maps");

// This map keeps exactly the same information as policy_filter_maps
// but keeps the reverse mappings. i.e.
// policy_filter_maps maps policy_id to cgroup_ids
// policy_filter_reverse_maps maps cgroup_id to policy_ids
struct {
__uint(type, BPF_MAP_TYPE_HASH_OF_MAPS);
__uint(max_entries, POLICY_FILTER_MAX_CGROUP_IDS);
__uint(key_size, sizeof(__u64)); /* cgroup id */
__array(
values, struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, POLICY_FILTER_MAX_POLICIES);
__type(key, __u32); /* policy id */
__type(value, __u8); /* empty */
});
} policy_filter_reverse_maps SEC(".maps");

Copy link
Contributor

Choose a reason for hiding this comment

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

I think naming the map "reverse" will make the code/etc. harder to understand.
How about cgroup_policies or policyfilter_cgoup_policies?
What do you think?

// policy_filter_check checks whether the policy applies on the current process.
// Returns true if it does, false otherwise.

Expand Down
21 changes: 18 additions & 3 deletions cmd/tetra/debug/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,33 @@ func PolicyfilterState(fname string) {
return
}

if len(data) == 0 {
fmt.Println("--- Direct Map ---")

if len(data.Direct) == 0 {
fmt.Printf("(empty)\n")
return
}

for polId, cgIDs := range data {
for polId, cgIDs := range data.Direct {
ids := make([]string, 0, len(cgIDs))
for id := range cgIDs {
ids = append(ids, strconv.FormatUint(uint64(id), 10))
}
fmt.Printf("%d: %s\n", polId, strings.Join(ids, ","))
}

fmt.Println("--- Reverse Map ---")
Copy link
Contributor

Choose a reason for hiding this comment

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

Even if we do not rename the map I find that "Direct" and "Reverse" would make things harder to understand. Let's describe what is the key and what is the value here to make it easier to understand the output.


if len(data.Reverse) == 0 {
fmt.Printf("(empty)\n")
}

for cgIDs, polIds := range data.Reverse {
ids := make([]string, 0, len(polIds))
for id := range polIds {
ids = append(ids, strconv.FormatUint(uint64(id), 10))
}
fmt.Printf("%d: %s\n", cgIDs, strings.Join(ids, ","))
}
}

func NamespaceState(fname string) error {
Expand Down
Loading