0%

libbpf中BPF_PROG宏实现机制分析学习

BPF_PROG实现原理分析

BPF_PROG实现的相关代码摘抄如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#ifndef ___bpf_concat
#define ___bpf_concat(a, b) a ## b
#endif
#ifndef ___bpf_apply
#define ___bpf_apply(fn, n) ___bpf_concat(fn, n)
#endif
#ifndef ___bpf_nth
#define ___bpf_nth(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b, _c, N, ...) N
#endif
#ifndef ___bpf_narg
#define ___bpf_narg(...) ___bpf_nth(_, ##__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
#endif

#define ___bpf_ctx_cast0() ctx
#define ___bpf_ctx_cast1(x) ___bpf_ctx_cast0(), (void *)ctx[0]
#define ___bpf_ctx_cast2(x, args...) ___bpf_ctx_cast1(args), (void *)ctx[1]
#define ___bpf_ctx_cast3(x, args...) ___bpf_ctx_cast2(args), (void *)ctx[2]
#define ___bpf_ctx_cast4(x, args...) ___bpf_ctx_cast3(args), (void *)ctx[3]
#define ___bpf_ctx_cast5(x, args...) ___bpf_ctx_cast4(args), (void *)ctx[4]
#define ___bpf_ctx_cast6(x, args...) ___bpf_ctx_cast5(args), (void *)ctx[5]
#define ___bpf_ctx_cast7(x, args...) ___bpf_ctx_cast6(args), (void *)ctx[6]
#define ___bpf_ctx_cast8(x, args...) ___bpf_ctx_cast7(args), (void *)ctx[7]
#define ___bpf_ctx_cast9(x, args...) ___bpf_ctx_cast8(args), (void *)ctx[8]
#define ___bpf_ctx_cast10(x, args...) ___bpf_ctx_cast9(args), (void *)ctx[9]
#define ___bpf_ctx_cast11(x, args...) ___bpf_ctx_cast10(args), (void *)ctx[10]
#define ___bpf_ctx_cast12(x, args...) ___bpf_ctx_cast11(args), (void *)ctx[11]
#define ___bpf_ctx_cast(args...) ___bpf_apply(___bpf_ctx_cast, ___bpf_narg(args))(args)

/*
* BPF_PROG is a convenience wrapper for generic tp_btf/fentry/fexit and
* similar kinds of BPF programs, that accept input arguments as a single
* pointer to untyped u64 array, where each u64 can actually be a typed
* pointer or integer of different size. Instead of requring user to write
* manual casts and work with array elements by index, BPF_PROG macro
* allows user to declare a list of named and typed input arguments in the
* same syntax as for normal C function. All the casting is hidden and
* performed transparently, while user code can just assume working with
* function arguments of specified type and name.
*
* Original raw context argument is preserved as well as 'ctx' argument.
* This is useful when using BPF helpers that expect original context
* as one of the parameters (e.g., for bpf_perf_event_output()).
*/
#define BPF_PROG(name, args...) \
name(unsigned long long *ctx); \
static __always_inline typeof(name(0)) \
____##name(unsigned long long *ctx, ##args); \
typeof(name(0)) name(unsigned long long *ctx) \
{ \
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
return ____##name(___bpf_ctx_cast(args)); \
_Pragma("GCC diagnostic pop") \
} \
static __always_inline typeof(name(0)) \
____##name(unsigned long long *ctx, ##args)

当使用这个macro, 比如这样:

1
2
3
4
SEC("tp_btf/sched_switch")
int BPF_PROG(sched_switch, bool preempt, struct task_struct *prev, struct task_struct *next){
/* ... */
}

实际上会展开宏得到这样的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
int sched_switch(unsigned long long *ctx);
static __always_inline typeof(sched_switch(0)) ____sched_switch(unsigned long long *ctx, bool preempt, struct task_struct *prev, struct task_struct *next);
typeof(sched_switch(0)) sched_switch(unsigned long long *ctx)
{
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
return ____sched_switch(___bpf_ctx_cast(bool preempt, struct task_struct *prev, struct task_struct *next)); \
_Pragma("GCC diagnostic pop") \
}
static __always_inline typeof(sched_switch(0)) \
____sched_switch(unsigned long long *ctx, bool preempt, struct task_struct *prev, struct task_struct *next) {
/* ... */
}

___bpf_ctx_cast(bool preempt, struct task_struct *prev, struct task_struct *next)展开后得到:

1
ctx, (void *)ctx[0], (void *)ctx[1], (void *)ctx[2]

所以这个宏实际上是展开后先声明了sched_switch____sched_switch 函数, 再写了具体实现, 我们定义的函数实现放在____sched_switch 中, 由sched_switch 负责调用. typeof(sched_switch(0)) 是获得sched_switch(0) 执行的返回值的类型int ,此时的ctx是0即NULL, 这样传入一个NULL参数去为了能都正确地拿到返回值的类型, 这个小技巧值得学习使用.

也就是说这个宏帮助我们自动做了解析ctx数组与实际参数类型的转化, 并隐藏了ctx指针. 从这个实现中也能看出其支持的最大args的个数是12, 这个在实际使用中也是足够的了.