Skip to content

Commit

Permalink
cleanup: move custom tail calls in sys_exit
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Terzolo <[email protected]>
  • Loading branch information
Andreagit97 committed Nov 26, 2024
1 parent 40b1fe3 commit 07e5ada
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 117 deletions.
119 changes: 116 additions & 3 deletions driver/modern_bpf/programs/attached/dispatchers/syscall_exit.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,119 @@

#include <helpers/interfaces/syscalls_dispatcher.h>
#include <bpf/bpf_helpers.h>
#include <helpers/interfaces/fixed_size_event.h>

SEC("tp_btf/sys_exit")
int BPF_PROG(t_hotplug) {
/* We assume that the ring buffer for CPU 0 is always there so we send the
* HOT-PLUG event through this buffer.
*/
uint32_t cpu_0 = 0;
struct ringbuf_map *rb = bpf_map_lookup_elem(&ringbuf_maps, &cpu_0);
if(!rb) {
bpf_printk("unable to obtain the ring buffer for CPU 0");
return 0;
}

struct counter_map *counter = bpf_map_lookup_elem(&counter_maps, &cpu_0);
if(!counter) {
bpf_printk("unable to obtain the counter map for CPU 0");
return 0;
}

/* This counts the event seen by the drivers even if they are dropped because the buffer is
* full. */
counter->n_evts++;

/* If we are not able to reserve space we stop here
* the event collection.
*/
struct ringbuf_struct ringbuf;
ringbuf.reserved_event_size = HOTPLUG_E_SIZE;
ringbuf.event_type = PPME_CPU_HOTPLUG_E;
ringbuf.data = bpf_ringbuf_reserve(rb, HOTPLUG_E_SIZE, 0);
if(!ringbuf.data) {
counter->n_drops_buffer++;
return 0;
}

ringbuf__store_event_header(&ringbuf);

/*=============================== COLLECT PARAMETERS ===========================*/

/* Parameter 1: cpu (type: PT_UINT32) */
uint32_t current_cpu_id = (uint32_t)bpf_get_smp_processor_id();
ringbuf__store_u32(&ringbuf, current_cpu_id);

/* Parameter 2: action (type: PT_UINT32) */
/* Right now we don't have actions we always send 0 */
ringbuf__store_u32(&ringbuf, 0);

/*=============================== COLLECT PARAMETERS ===========================*/

ringbuf__submit_event(&ringbuf);
return 0;
}

SEC("tp_btf/sys_exit")
int BPF_PROG(t_drop_e) {
struct ringbuf_struct ringbuf;
if(!ringbuf__reserve_space(&ringbuf, DROP_E_SIZE, PPME_DROP_E)) {
return 0;
}

ringbuf__store_event_header(&ringbuf);

/*=============================== COLLECT PARAMETERS ===========================*/

ringbuf__store_u32(&ringbuf, maps__get_sampling_ratio());

/*=============================== COLLECT PARAMETERS ===========================*/

ringbuf__submit_event(&ringbuf);
return 0;
}

SEC("tp_btf/sys_exit")
int BPF_PROG(t_drop_x) {
struct ringbuf_struct ringbuf;
if(!ringbuf__reserve_space(&ringbuf, DROP_X_SIZE, PPME_DROP_X)) {
return 0;
}

ringbuf__store_event_header(&ringbuf);

/*=============================== COLLECT PARAMETERS ===========================*/

ringbuf__store_u32(&ringbuf, maps__get_sampling_ratio());

/*=============================== COLLECT PARAMETERS ===========================*/

ringbuf__submit_event(&ringbuf);
return 0;
}

enum custom_sys_exit_logic_codes {
T_HOTPLUG,
T_DROP_E,
T_DROP_X,
// add more codes here.
T_CUSTOM_MAX,
};

struct {
__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
__uint(max_entries, T_CUSTOM_MAX);
__uint(key_size, sizeof(__u32));
__array(values, int(void *));
} custom_sys_exit_calls SEC(".maps") = {
.values =
{
[T_HOTPLUG] = (void *)&t_hotplug,
[T_DROP_E] = (void *)&t_drop_e,
[T_DROP_X] = (void *)&t_drop_x,
},
};

static __always_inline bool sampling_logic_exit(void *ctx, uint32_t id) {
/* If dropping mode is not enabled we don't perform any sampling
Expand Down Expand Up @@ -39,15 +152,15 @@ static __always_inline bool sampling_logic_exit(void *ctx, uint32_t id) {
* an iteration we will synchronize again the next time the logic is enabled.
*/
maps__set_is_dropping(true);
bpf_tail_call(ctx, &extra_syscall_calls, T1_DROP_E);
bpf_tail_call(ctx, &custom_sys_exit_calls, T_DROP_E);
bpf_printk("unable to tail call into 'drop_e' prog");
}
return true;
}

if(maps__get_is_dropping()) {
maps__set_is_dropping(false);
bpf_tail_call(ctx, &extra_syscall_calls, T1_DROP_X);
bpf_tail_call(ctx, &custom_sys_exit_calls, T_DROP_X);
bpf_printk("unable to tail call into 'drop_x' prog");
}

Expand Down Expand Up @@ -120,7 +233,7 @@ int BPF_PROG(sys_exit, struct pt_regs *regs, long ret) {
// we change our architecture we may need to update this logic.
struct ringbuf_map *rb = maps__get_ringbuf_map();
if(!rb) {
bpf_tail_call(ctx, &extra_syscall_calls, T1_HOTPLUG_E);
bpf_tail_call(ctx, &custom_sys_exit_calls, T_HOTPLUG);
bpf_printk("failed to tail call into the 'hotplug' prog");
return 0;
}
Expand Down

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions driver/ppm_events_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -1536,9 +1536,6 @@ enum extra_syscall_codes {
T2_CLONE3_X,
T2_FORK_X,
T2_VFORK_X,
T1_DROP_E,
T1_DROP_X,
T1_HOTPLUG_E,
T1_OPEN_BY_HANDLE_AT_X,
T2_EXECVE_X,
T2_EXECVEAT_X,
Expand Down
3 changes: 0 additions & 3 deletions userspace/libpman/src/events_prog_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,6 @@ static const char* extra_event_prog_names[TAIL_EXTRA_EVENT_PROG_MAX] = {
[T2_CLONE3_X] = "t2_clone3_x",
[T2_FORK_X] = "t2_fork_x",
[T2_VFORK_X] = "t2_vfork_x",
[T1_DROP_E] = "t1_drop_e",
[T1_DROP_X] = "t1_drop_x",
[T1_HOTPLUG_E] = "t1_hotplug_e",
[T1_OPEN_BY_HANDLE_AT_X] = "t1_open_by_handle_at_x",
[T2_EXECVE_X] = "t2_execve_x",
[T2_EXECVEAT_X] = "t2_execveat_x",
Expand Down

0 comments on commit 07e5ada

Please sign in to comment.