Skip to content

Commit

Permalink
tetragon: Move generic_output to generic functions
Browse files Browse the repository at this point in the history
Signed-off-by: Jiri Olsa <[email protected]>
  • Loading branch information
olsajiri committed Dec 19, 2024
1 parent 1b24022 commit bf2736c
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 55 deletions.
2 changes: 1 addition & 1 deletion bpf/process/bpf_generic_kprobe.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ generic_kprobe_actions(void *ctx)
__attribute__((section("kprobe"), used)) int
generic_kprobe_output(void *ctx)
{
return generic_output(ctx, (struct bpf_map_def *)&process_call_heap, MSG_OP_GENERIC_KPROBE);
return generic_output(ctx, MSG_OP_GENERIC_KPROBE);
}

__attribute__((section(OVERRIDE), used)) int
Expand Down
5 changes: 4 additions & 1 deletion bpf/process/bpf_generic_lsm_output.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#include "types/basic.h"
#include "generic_maps.h"

#include "generic_maps.h"
#include "generic_calls.h"

char _license[] __attribute__((section("license"), used)) = "Dual BSD/GPL";

__attribute__((section("lsm/generic_lsm_output"), used)) int
Expand Down Expand Up @@ -43,6 +46,6 @@ generic_lsm_output(void *ctx)
}
#endif
if (e->lsm.post)
generic_output(ctx, (struct bpf_map_def *)&process_call_heap, MSG_OP_GENERIC_LSM);
generic_output(ctx, MSG_OP_GENERIC_LSM);
return try_override(ctx, (struct bpf_map_def *)&override_tasks);
}
6 changes: 4 additions & 2 deletions bpf/process/bpf_generic_retkprobe.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "bpf_tracing.h"
#include "bpf_event.h"
#include "bpf_task.h"
#include "generic_maps.h"
#include "retprobe_map.h"
#include "types/basic.h"

Expand All @@ -35,6 +34,9 @@ struct {
},
};

#include "generic_maps.h"
#include "generic_calls.h"

#ifdef __MULTI_KPROBE
#define MAIN "kprobe.multi/generic_retkprobe"
#else
Expand Down Expand Up @@ -165,5 +167,5 @@ BPF_KRETPROBE(generic_retkprobe_actions)
__attribute__((section("kprobe"), used)) int
BPF_KRETPROBE(generic_retkprobe_output)
{
return generic_output(ctx, (struct bpf_map_def *)&process_call_heap, MSG_OP_GENERIC_KPROBE);
return generic_output(ctx, MSG_OP_GENERIC_KPROBE);
}
2 changes: 1 addition & 1 deletion bpf/process/bpf_generic_tracepoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ generic_tracepoint_actions(void *ctx)
__attribute__((section("tracepoint"), used)) int
generic_tracepoint_output(void *ctx)
{
return generic_output(ctx, (struct bpf_map_def *)&process_call_heap, MSG_OP_GENERIC_TRACEPOINT);
return generic_output(ctx, MSG_OP_GENERIC_TRACEPOINT);
}

char _license[] __attribute__((section("license"), used)) = "Dual BSD/GPL";
2 changes: 1 addition & 1 deletion bpf/process/bpf_generic_uprobe.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,5 @@ generic_uprobe_actions(void *ctx)
__attribute__((section("uprobe"), used)) int
generic_uprobe_output(void *ctx)
{
return generic_output(ctx, (struct bpf_map_def *)&process_call_heap, MSG_OP_GENERIC_UPROBE);
return generic_output(ctx, MSG_OP_GENERIC_UPROBE);
}
49 changes: 49 additions & 0 deletions bpf/process/generic_calls.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,53 @@ generic_process_event_and_setup(struct pt_regs *ctx, struct bpf_map_def *tailcal
return generic_process_event(ctx, tailcals);
}

FUNC_INLINE long
generic_output(void *ctx, u8 op)
{
struct msg_generic_kprobe *e;
int zero = 0;
size_t total;

e = map_lookup_elem(&process_call_heap, &zero);
if (!e)
return 0;

/* We don't need this data in return kprobe event */
#ifndef GENERIC_KRETPROBE
#ifdef __NS_CHANGES_FILTER
/* update the namespaces if we matched a change on that */
if (e->sel.match_ns) {
__u32 pid = (get_current_pid_tgid() >> 32);
struct task_struct *task =
(struct task_struct *)get_current_task();
struct execve_map_value *enter = execve_map_get_noinit(
pid); // we don't want to init that if it does not exist
if (enter)
get_namespaces(&(enter->ns), task);
}
#endif
#ifdef __CAP_CHANGES_FILTER
/* update the capabilities if we matched a change on that */
if (e->sel.match_cap) {
__u32 pid = (get_current_pid_tgid() >> 32);
struct task_struct *task =
(struct task_struct *)get_current_task();
struct execve_map_value *enter = execve_map_get_noinit(
pid); // we don't want to init that if it does not exist
if (enter)
get_current_subj_caps(&enter->caps, task);
}
#endif
#endif // !GENERIC_KRETPROBE

total = e->common.size + generic_kprobe_common_size();
/* Code movement from clang forces us to inline bounds checks here */
asm volatile("%[total] &= 0x7fff;\n"
"if %[total] < 9000 goto +1\n;"
"%[total] = 9000;\n"
: [total] "+r"(total));
perf_event_output_metric(ctx, op, &tcpmon_map, BPF_F_CURRENT_CPU, e, total);
return 0;
}

#endif /* __GENERIC_CALLS_H__ */
49 changes: 0 additions & 49 deletions bpf/process/types/basic.h
Original file line number Diff line number Diff line change
Expand Up @@ -2394,55 +2394,6 @@ generic_actions(void *ctx, struct generic_maps *maps)
return postit;
}

FUNC_INLINE long
generic_output(void *ctx, struct bpf_map_def *heap, u8 op)
{
struct msg_generic_kprobe *e;
int zero = 0;
size_t total;

e = map_lookup_elem(heap, &zero);
if (!e)
return 0;

/* We don't need this data in return kprobe event */
#ifndef GENERIC_KRETPROBE
#ifdef __NS_CHANGES_FILTER
/* update the namespaces if we matched a change on that */
if (e->sel.match_ns) {
__u32 pid = (get_current_pid_tgid() >> 32);
struct task_struct *task =
(struct task_struct *)get_current_task();
struct execve_map_value *enter = execve_map_get_noinit(
pid); // we don't want to init that if it does not exist
if (enter)
get_namespaces(&(enter->ns), task);
}
#endif
#ifdef __CAP_CHANGES_FILTER
/* update the capabilities if we matched a change on that */
if (e->sel.match_cap) {
__u32 pid = (get_current_pid_tgid() >> 32);
struct task_struct *task =
(struct task_struct *)get_current_task();
struct execve_map_value *enter = execve_map_get_noinit(
pid); // we don't want to init that if it does not exist
if (enter)
get_current_subj_caps(&enter->caps, task);
}
#endif
#endif // !GENERIC_KRETPROBE

total = e->common.size + generic_kprobe_common_size();
/* Code movement from clang forces us to inline bounds checks here */
asm volatile("%[total] &= 0x7fff;\n"
"if %[total] < 9000 goto +1\n;"
"%[total] = 9000;\n"
: [total] "+r"(total));
perf_event_output_metric(ctx, op, &tcpmon_map, BPF_F_CURRENT_CPU, e, total);
return 0;
}

/**
* Read a generic argument
*
Expand Down

0 comments on commit bf2736c

Please sign in to comment.