Skip to content

Commit

Permalink
chore(driver/modern_bpf): split out a tail call when sendmmsg/recvmms…
Browse files Browse the repository at this point in the history
…g fail.

Signed-off-by: Federico Di Pierro <[email protected]>
  • Loading branch information
FedeDP committed Jan 24, 2025
1 parent 24581f6 commit 8fbe3b3
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,39 +113,73 @@ static __always_inline long handle_exit(uint32_t index, void *ctx) {
return 0;
}

enum extra_recvmmsg_codes {
T_RECVMMSG_FAIL,
// add more codes here.
T_RECVMMSG_MAX,
};

/*
* FORWARD DECLARATIONS:
* See the `BPF_PROG` macro in libbpf `libbpf/src/bpf_tracing.h`
* #define BPF_PROG(name, args...) \
* name(unsigned long long *ctx); \
*/
int t_recvmmsg_fail(unsigned long long *ctx);

struct {
__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
__uint(max_entries, T_RECVMMSG_MAX);
__uint(key_size, sizeof(__u32));
__array(values, int(void *));
} extra_recvmmsg_calls SEC(".maps") = {
.values =
{
[T_RECVMMSG_FAIL] = (void *)&t_recvmmsg_fail,
// add more tail calls here.
},
};

SEC("tp_btf/sys_exit")
int BPF_PROG(recvmmsg_x, struct pt_regs *regs, long ret) {
if(ret <= 0) {
unsigned long fd = 0;
struct auxiliary_map *auxmap = auxmap__get();
if(!auxmap) {
return 0;
}
int BPF_PROG(t_recvmmsg_fail, struct pt_regs *regs, long ret) {
unsigned long fd = 0;
struct auxiliary_map *auxmap = auxmap__get();
if(!auxmap) {
return 0;
}

auxmap__preload_event_header(auxmap, PPME_SOCKET_RECVMMSG_X);
auxmap__preload_event_header(auxmap, PPME_SOCKET_RECVMMSG_X);

/* Parameter 1: res (type: PT_ERRNO) */
auxmap__store_s64_param(auxmap, ret);
/* Parameter 1: res (type: PT_ERRNO) */
auxmap__store_s64_param(auxmap, ret);

/* Parameter 2: fd (type: PT_FD) */
extract__network_args(&fd, 1, regs);
auxmap__store_s64_param(auxmap, (int64_t)(int32_t)fd);
/* Parameter 2: fd (type: PT_FD) */
extract__network_args(&fd, 1, regs);
auxmap__store_s64_param(auxmap, (int64_t)(int32_t)fd);

/* Parameter 3: size (type: PT_UINT32) */
auxmap__store_u32_param(auxmap, 0);
/* Parameter 3: size (type: PT_UINT32) */
auxmap__store_u32_param(auxmap, 0);

/* Parameter 4: data (type: PT_BYTEBUF) */
auxmap__store_empty_param(auxmap);
/* Parameter 4: data (type: PT_BYTEBUF) */
auxmap__store_empty_param(auxmap);

/* Parameter 5: tuple (type: PT_SOCKTUPLE) */
auxmap__store_empty_param(auxmap);
/* Parameter 5: tuple (type: PT_SOCKTUPLE) */
auxmap__store_empty_param(auxmap);

/* Parameter 6: msg_control (type: PT_BYTEBUF) */
auxmap__store_empty_param(auxmap);
/* Parameter 6: msg_control (type: PT_BYTEBUF) */
auxmap__store_empty_param(auxmap);

auxmap__finalize_event_header(auxmap);
auxmap__finalize_event_header(auxmap);

auxmap__submit_event(auxmap);
return 0;
}

auxmap__submit_event(auxmap);
SEC("tp_btf/sys_exit")
int BPF_PROG(recvmmsg_x, struct pt_regs *regs, long ret) {
if(ret <= 0) {
bpf_tail_call(ctx, &extra_recvmmsg_calls, T_RECVMMSG_FAIL);
bpf_printk("failed to tail call into the 'recvmmsg_fail' prog");
return 0;
}

Expand All @@ -168,36 +202,8 @@ int BPF_PROG(recvmmsg_x, struct pt_regs *regs, long ret) {
SEC("tp_btf/sys_exit")
int BPF_PROG(recvmmsg_old_x, struct pt_regs *regs, long ret) {
if(ret <= 0) {
unsigned long fd = 0;
struct auxiliary_map *auxmap = auxmap__get();
if(!auxmap) {
return 0;
}

auxmap__preload_event_header(auxmap, PPME_SOCKET_RECVMMSG_X);

/* Parameter 1: res (type: PT_ERRNO) */
auxmap__store_s64_param(auxmap, ret);

/* Parameter 2: fd (type: PT_FD) */
extract__network_args(&fd, 1, regs);
auxmap__store_s64_param(auxmap, (int64_t)(int32_t)fd);

/* Parameter 3: size (type: PT_UINT32) */
auxmap__store_u32_param(auxmap, 0);

/* Parameter 4: data (type: PT_BYTEBUF) */
auxmap__store_empty_param(auxmap);

/* Parameter 5: tuple (type: PT_SOCKTUPLE) */
auxmap__store_empty_param(auxmap);

/* Parameter 6: msg_control (type: PT_BYTEBUF) */
auxmap__store_empty_param(auxmap);

auxmap__finalize_event_header(auxmap);

auxmap__submit_event(auxmap);
bpf_tail_call(ctx, &extra_recvmmsg_calls, T_RECVMMSG_FAIL);
bpf_printk("failed to tail call into the 'recvmmsg_fail' prog");
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,36 +109,70 @@ static __always_inline long handle_exit(uint32_t index, void *ctx) {
return 0;
}

enum extra_sendmmsg_codes {
T_SENDMMSG_FAIL,
// add more codes here.
T_SENDMMSG_MAX,
};

/*
* FORWARD DECLARATIONS:
* See the `BPF_PROG` macro in libbpf `libbpf/src/bpf_tracing.h`
* #define BPF_PROG(name, args...) \
* name(unsigned long long *ctx); \
*/
int t_sendmmsg_fail(unsigned long long *ctx);

struct {
__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
__uint(max_entries, T_SENDMMSG_MAX);
__uint(key_size, sizeof(__u32));
__array(values, int(void *));
} extra_sendmmsg_calls SEC(".maps") = {
.values =
{
[T_SENDMMSG_FAIL] = (void *)&t_sendmmsg_fail,
// add more tail calls here.
},
};

SEC("tp_btf/sys_exit")
int BPF_PROG(sendmmsg_x, struct pt_regs *regs, long ret) {
if(ret <= 0) {
unsigned long fd = 0;
struct auxiliary_map *auxmap = auxmap__get();
if(!auxmap) {
return 0;
}
int BPF_PROG(t_sendmmsg_fail, struct pt_regs *regs, long ret) {
unsigned long fd = 0;
struct auxiliary_map *auxmap = auxmap__get();
if(!auxmap) {
return 0;
}

auxmap__preload_event_header(auxmap, PPME_SOCKET_SENDMMSG_X);
auxmap__preload_event_header(auxmap, PPME_SOCKET_SENDMMSG_X);

/* Parameter 1: res (type: PT_ERRNO) */
auxmap__store_s64_param(auxmap, ret);
/* Parameter 1: res (type: PT_ERRNO) */
auxmap__store_s64_param(auxmap, ret);

/* Parameter 2: fd (type: PT_FD) */
extract__network_args(&fd, 1, regs);
auxmap__store_s64_param(auxmap, (int64_t)(int32_t)fd);
/* Parameter 2: fd (type: PT_FD) */
extract__network_args(&fd, 1, regs);
auxmap__store_s64_param(auxmap, (int64_t)(int32_t)fd);

/* Parameter 3: size (type: PT_UINT32) */
auxmap__store_u32_param(auxmap, 0);
/* Parameter 3: size (type: PT_UINT32) */
auxmap__store_u32_param(auxmap, 0);

/* Parameter 4: data (type: PT_BYTEBUF) */
auxmap__store_empty_param(auxmap);
/* Parameter 4: data (type: PT_BYTEBUF) */
auxmap__store_empty_param(auxmap);

/* Parameter 5: tuple (type: PT_SOCKTUPLE) */
auxmap__store_empty_param(auxmap);
/* Parameter 5: tuple (type: PT_SOCKTUPLE) */
auxmap__store_empty_param(auxmap);

auxmap__finalize_event_header(auxmap);
auxmap__finalize_event_header(auxmap);

auxmap__submit_event(auxmap);
auxmap__submit_event(auxmap);
return 0;
}

SEC("tp_btf/sys_exit")
int BPF_PROG(sendmmsg_x, struct pt_regs *regs, long ret) {
if(ret <= 0) {
bpf_tail_call(ctx, &extra_sendmmsg_calls, T_SENDMMSG_FAIL);
bpf_printk("failed to tail call into the 'sendmmsg_fail' prog");
return 0;
}

Expand All @@ -161,33 +195,8 @@ int BPF_PROG(sendmmsg_x, struct pt_regs *regs, long ret) {
SEC("tp_btf/sys_exit")
int BPF_PROG(sendmmsg_old_x, struct pt_regs *regs, long ret) {
if(ret <= 0) {
unsigned long fd = 0;
struct auxiliary_map *auxmap = auxmap__get();
if(!auxmap) {
return 0;
}

auxmap__preload_event_header(auxmap, PPME_SOCKET_SENDMMSG_X);

/* Parameter 1: res (type: PT_ERRNO) */
auxmap__store_s64_param(auxmap, ret);

/* Parameter 2: fd (type: PT_FD) */
extract__network_args(&fd, 1, regs);
auxmap__store_s64_param(auxmap, (int64_t)(int32_t)fd);

/* Parameter 3: size (type: PT_UINT32) */
auxmap__store_u32_param(auxmap, 0);

/* Parameter 4: data (type: PT_BYTEBUF) */
auxmap__store_empty_param(auxmap);

/* Parameter 5: tuple (type: PT_SOCKTUPLE) */
auxmap__store_empty_param(auxmap);

auxmap__finalize_event_header(auxmap);

auxmap__submit_event(auxmap);
bpf_tail_call(ctx, &extra_sendmmsg_calls, T_SENDMMSG_FAIL);
bpf_printk("failed to tail call into the 'sendmmsg_fail' prog");
return 0;
}

Expand Down

0 comments on commit 8fbe3b3

Please sign in to comment.