From 92e4c99ed1186f4a83f22f08a5685ec3d4264a23 Mon Sep 17 00:00:00 2001 From: Jiri Olsa Date: Sat, 25 Nov 2023 20:09:00 +0000 Subject: [PATCH] tetragon: Add TAIL_CALL_* enums for generic tail calls Adding TAIL_CALL_* enum values to identify tail calls for generic probes and make it more human friendly. Suggested-by: Djalal Harouni Signed-off-by: Jiri Olsa --- bpf/process/bpf_generic_kprobe.c | 7 ++----- bpf/process/bpf_generic_tracepoint.c | 9 +++------ bpf/process/bpf_generic_uprobe.c | 4 ++-- bpf/process/generic_calls.h | 4 ++-- bpf/process/types/basic.h | 25 +++++++++++++++---------- 5 files changed, 24 insertions(+), 25 deletions(-) diff --git a/bpf/process/bpf_generic_kprobe.c b/bpf/process/bpf_generic_kprobe.c index 3bd1df2d66d..c7dda50866e 100644 --- a/bpf/process/bpf_generic_kprobe.c +++ b/bpf/process/bpf_generic_kprobe.c @@ -113,7 +113,7 @@ generic_kprobe_start_process_filter(void *ctx) #endif /* Tail call into filters. */ - tail_call(ctx, &kprobe_calls, 2); + tail_call(ctx, &kprobe_calls, TAIL_CALL_FILTER); return 0; } @@ -187,7 +187,7 @@ generic_kprobe_process_filter(void *ctx) ret = generic_process_filter(&msg->sel, &msg->current, &msg->ns, &msg->caps, &filter_map, msg->idx); if (ret == PFILTER_CONTINUE) - tail_call(ctx, &kprobe_calls, 2); + tail_call(ctx, &kprobe_calls, TAIL_CALL_FILTER); else if (ret == PFILTER_ACCEPT) tail_call(ctx, &kprobe_calls, 0); /* If filter does not accept drop it. Ideally we would @@ -196,9 +196,6 @@ generic_kprobe_process_filter(void *ctx) return PFILTER_REJECT; } -// Filter tailcalls: kprobe/6...kprobe/10 -// see also: MIN_FILTER_TAILCALL, MAX_FILTER_TAILCALL - __attribute__((section("kprobe/3"), used)) int generic_kprobe_filter_arg(void *ctx) { diff --git a/bpf/process/bpf_generic_tracepoint.c b/bpf/process/bpf_generic_tracepoint.c index 36a84959f06..04758262e0f 100644 --- a/bpf/process/bpf_generic_tracepoint.c +++ b/bpf/process/bpf_generic_tracepoint.c @@ -196,7 +196,7 @@ generic_tracepoint_event(struct generic_tracepoint_event_arg *ctx) #ifdef __CAP_CHANGES_FILTER msg->sel.match_cap = 0; #endif - tail_call(ctx, &tp_calls, 2); + tail_call(ctx, &tp_calls, TAIL_CALL_FILTER); return 0; } @@ -221,18 +221,15 @@ generic_tracepoint_filter(void *ctx) ret = generic_process_filter(&msg->sel, &msg->current, &msg->ns, &msg->caps, &filter_map, msg->idx); if (ret == PFILTER_CONTINUE) - tail_call(ctx, &tp_calls, 2); + tail_call(ctx, &tp_calls, TAIL_CALL_FILTER); else if (ret == PFILTER_ACCEPT) - tail_call(ctx, &tp_calls, 1); + tail_call(ctx, &tp_calls, TAIL_CALL_PROCESS); /* If filter does not accept drop it. Ideally we would * log error codes for later review, TBD. */ return PFILTER_REJECT; } -// Filter tailcalls: tracepoint/6...tracepoint/10 -// see also: MIN_FILTER_TAILCALL, MAX_FILTER_TAILCALL - __attribute__((section("tracepoint/3"), used)) int generic_tracepoint_arg(void *ctx) { diff --git a/bpf/process/bpf_generic_uprobe.c b/bpf/process/bpf_generic_uprobe.c index 5f414c9e760..63b7531e484 100644 --- a/bpf/process/bpf_generic_uprobe.c +++ b/bpf/process/bpf_generic_uprobe.c @@ -89,7 +89,7 @@ generic_uprobe_start_process_filter(void *ctx) if (!generic_process_filter_binary(config)) return 0; /* Tail call into filters. */ - tail_call(ctx, &uprobe_calls, 2); + tail_call(ctx, &uprobe_calls, TAIL_CALL_FILTER); return 0; } @@ -130,7 +130,7 @@ generic_uprobe_process_filter(void *ctx) ret = generic_process_filter(&msg->sel, &msg->current, &msg->ns, &msg->caps, &filter_map, msg->idx); if (ret == PFILTER_CONTINUE) - tail_call(ctx, &uprobe_calls, 2); + tail_call(ctx, &uprobe_calls, TAIL_CALL_FILTER); else if (ret == PFILTER_ACCEPT) tail_call(ctx, &uprobe_calls, 0); /* If filter does not accept drop it. Ideally we would diff --git a/bpf/process/generic_calls.h b/bpf/process/generic_calls.h index 20c80507546..d162b712f25 100644 --- a/bpf/process/generic_calls.h +++ b/bpf/process/generic_calls.h @@ -61,12 +61,12 @@ generic_process_event(void *ctx, struct bpf_map_def *heap_map, /* Continue to process other arguments. */ if (index < 4) { e->filter_tailcall_index = index + 1; - tail_call(ctx, tailcals, 1); + tail_call(ctx, tailcals, TAIL_CALL_PROCESS); } /* Last argument, go send.. */ e->filter_tailcall_index = 0; - tail_call(ctx, tailcals, 3); + tail_call(ctx, tailcals, TAIL_CALL_ARGS); return 0; } diff --git a/bpf/process/types/basic.h b/bpf/process/types/basic.h index 66b5566f333..445e7cfcb31 100644 --- a/bpf/process/types/basic.h +++ b/bpf/process/types/basic.h @@ -95,6 +95,14 @@ enum { FGS_SIGKILL = 9, }; +enum { + TAIL_CALL_PROCESS = 1, + TAIL_CALL_FILTER = 2, + TAIL_CALL_ARGS = 3, + TAIL_CALL_ACTIONS = 4, + TAIL_CALL_SEND = 5, +}; + struct selector_action { __u32 actionlen; __u32 act[]; @@ -183,12 +191,9 @@ static inline __attribute__((always_inline)) __u32 get_index(void *ctx) #define get_index(ctx) 0 #endif -// Filter tailcalls are {kprobe,tracepoint}/{6,7,8,9,10} -// We do one tail-call per selector, so we can have up to 5 selectors. -#define MIN_FILTER_TAILCALL 6 -#define MAX_FILTER_TAILCALL 10 -#define MAX_SELECTORS (MAX_FILTER_TAILCALL - MIN_FILTER_TAILCALL + 1) -#define MAX_SELECTORS_MASK 7 +// We do one tail-call per selector, we can have up to 5 selectors. +#define MAX_SELECTORS 5 +#define MAX_SELECTORS_MASK 7 static inline __attribute__((always_inline)) long filter_32ty_map(struct selector_arg_filter *filter, char *args); @@ -2182,7 +2187,7 @@ filter_read_arg(void *ctx, struct bpf_map_def *heap, index++; if (index <= MAX_SELECTORS && e->sel.active[index & MAX_SELECTORS_MASK]) { e->filter_tailcall_index = index; - tail_call(ctx, tailcalls, 3); + tail_call(ctx, tailcalls, TAIL_CALL_ARGS); } // reject if we did not attempt to tailcall, or if tailcall failed. return filter_args_reject(e->func_id); @@ -2192,10 +2197,10 @@ filter_read_arg(void *ctx, struct bpf_map_def *heap, // otherwise pass==1 indicates using default action. if (pass > 1) { e->pass = pass; - tail_call(ctx, tailcalls, 4); + tail_call(ctx, tailcalls, TAIL_CALL_ACTIONS); } - tail_call(ctx, tailcalls, 5); + tail_call(ctx, tailcalls, TAIL_CALL_SEND); return 1; } @@ -2237,7 +2242,7 @@ generic_actions(void *ctx, struct bpf_map_def *heap, postit = do_actions(ctx, e, actions, override_tasks); if (postit) - tail_call(ctx, tailcalls, 5); + tail_call(ctx, tailcalls, TAIL_CALL_SEND); return 1; }