History

bpf: Introduce BPF_MODIFY_RETURN

When multiple programs are attached, each program receives the return value from the previous program on the stack and the last program provides the return value to the attached function. The fmod_ret bpf programs are run after the fentry programs and before the fexit programs. The original function is only called if all the fmod_ret programs return 0 to avoid any unintended side-effects. The success value, i.e. 0 is not currently configurable but can be made so where user-space can specify it at load time. For example: int func_to_be_attached(int a, int b) { <--- do_fentry do_fmod_ret: <update ret by calling fmod_ret> if (ret != 0) goto do_fexit; original_function: <side_effects_happen_here> } <--- do_fexit The fmod_ret program attached to this function can be defined as: SEC("fmod_ret/func_to_be_attached") int BPF_PROG(func_name, int a, int b, int ret) { // This will skip the original function logic. return 1; } The first fmod_ret program is passed 0 in its return argument.

KP Singh committed 6 years ago

Commit ae240821 parent 7e639208 files · +130 −13Browse files

Showing the changes to include/uapi/linux (1 of 8 files). Show all

Unified · Split

include/uapi/linux/bpf.h+1−0
@@ -210,6 +210,7 @@ enum bpf_attach_type { · 209 unmodified lines
210210 BPF_TRACE_RAW_TP,
211211 BPF_TRACE_FENTRY,
212212 BPF_TRACE_FEXIT,
213+ BPF_MODIFY_RETURN,
213214 __MAX_BPF_ATTACH_TYPE
214215 };
215216