当前位置:网站首页>Interpretation of ebpf sockops code
Interpretation of ebpf sockops code
2022-06-24 21:29:00 【already_ skb】
2032 static inline int tcp_call_bpf(struct sock *sk, int op, u32 nargs, u32 *args)
2033 {
2034 struct bpf_sock_ops_kern sock_ops;
2035 int ret;
2036
2037 memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp));
2038 if (sk_fullsock(sk)) {
2039 sock_ops.is_fullsock = 1;
2040 sock_owned_by_me(sk);
2041 }
2042
2043 sock_ops.sk = sk;
2044 sock_ops.op = op;
2045 if (nargs > 0)
2046 memcpy(sock_ops.args, args, nargs * sizeof(*args));
2047
2048 ret = BPF_CGROUP_RUN_PROG_SOCK_OPS(&sock_ops);
2049 if (ret == 0)
2050 ret = sock_ops.reply;
2051 else
2052 ret = -1;
2053 return ret;
2054 }
2055
2056 static inline int tcp_call_bpf_2arg(struct sock *sk, int op, u32 arg1, u32 arg2)
2057 {
2058 u32 args[2] = {arg1, arg2};
2059
2060 return tcp_call_bpf(sk, op, 2, args);
2061 }
2062
2063 static inline int tcp_call_bpf_3arg(struct sock *sk, int op, u32 arg1, u32 arg2,
2064 u32 arg3)
2065 {
2066 u32 args[3] = {arg1, arg2, arg3};
2067
2068 return tcp_call_bpf(sk, op, 3, args);
2069 }ebpf sockops There are three buried point functions defined by :
tcp_call_bpf(struct sock *sk, int op, u32 nargs, u32 *args)
tcp_call_bpf_2arg(struct sock *sk, int op, u32 arg1, u32 arg2)
tcp_call_bpf_3arg(struct sock *sk, int op, u32 arg1, u32 arg2, u32 arg3)
stay TCP The three functions are called in the main path process of , Then the user-defined processing function is loaded on the related linked list of the channel , Finally, the user-defined functions are traversed in these three functions , Finally realize the function .
tcp_call_bpf_2arg / tcp_call_bpf_3arg The number of parameters is different , The final parameters are encapsulated as struct bpf_sock_ops_kern structure , Pass to callback function .
2032 static inline int tcp_call_bpf(struct sock *sk, int op, u32 nargs, u32 *args)
2033 {
2034 struct bpf_sock_ops_kern sock_ops;
2035 int ret;
2036
2037 memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp));
2038 if (sk_fullsock(sk)) {
2039 sock_ops.is_fullsock = 1;
2040 sock_owned_by_me(sk);
2041 }
2042
2043 sock_ops.sk = sk;
2044 sock_ops.op = op;
2045 if (nargs > 0)
2046 memcpy(sock_ops.args, args, nargs * sizeof(*args));
2047
2048 ret = BPF_CGROUP_RUN_PROG_SOCK_OPS(&sock_ops);
2049 if (ret == 0)
2050 ret = sock_ops.reply;
2051 else
2052 ret = -1;
2053 return ret;
2054 }sock_ops Is a parameter structure ,2043、2044 Initialization records sk/op Two parameters ,2046 Copy multiple parameters , Up to four parameters are supported .
2048 That's ok ,BPF_CGROUP_RUN_PROG_SOCK_OPS(&sock_ops) Traverse the callback function , At the same time sock_ops As a parameter to the callback function .
169 #define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops) \
170 ({ \
171 int __ret = 0; \
172 if (cgroup_bpf_enabled && (sock_ops)->sk) { \
173 typeof(sk) __sk = sk_to_full_sk((sock_ops)->sk); \
174 if (__sk && sk_fullsock(__sk)) \
175 __ret = __cgroup_bpf_run_filter_sock_ops(__sk, \
176 sock_ops, \
177 BPF_CGROUP_SOCK_OPS); \
178 } \
179 __ret; \
180 })
612 int __cgroup_bpf_run_filter_sock_ops(struct sock *sk,
613 struct bpf_sock_ops_kern *sock_ops,
614 enum bpf_attach_type type)
615 {
616 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
617 int ret;
618
619 ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], sock_ops,
620 BPF_PROG_RUN);
621 return ret == 1 ? 0 : -EPERM;
622 }
392 #define BPF_PROG_RUN_ARRAY(array, ctx, func) \
393 __BPF_PROG_RUN_ARRAY(array, ctx, func, false)
370
371 #define __BPF_PROG_RUN_ARRAY(array, ctx, func, check_non_null) \
372 ({ \
373 struct bpf_prog **_prog, *__prog; \
374 struct bpf_prog_array *_array; \
375 u32 _ret = 1; \
376 preempt_disable(); \
377 rcu_read_lock(); \
378 _array = rcu_dereference(array); \
379 if (unlikely(check_non_null && !_array))\
380 goto _out; \
381 _prog = _array->progs; \
382 while ((__prog = READ_ONCE(*_prog))) { \
383 _ret &= func(__prog, ctx); \
384 _prog++; \
385 } \
386 _out: \
387 rcu_read_unlock(); \
388 preempt_enable_no_resched(); \
389 _ret; \
390 })382 while ((__prog = READ_ONCE(*_prog))) { \
383 _ret &= func(__prog, ctx); \
384 _prog++; \
385 } The above code snippet really implements the callback function of the registered callback function .ctx The parameter is formally above sock_ops.
__BPF_PROG_RUN_ARRAY One line of code in the macro implementation is critical :preempt_enable_no_resched()
Different from the conventional preempt_enable function , This function brings no_resched, It means that a rescheduled preemption point cannot be created after executing the current code fragment . Just imagine , If preempt_enable, This creates a rescheduling preemption point , At this time, you may switch to other high priority tasks , At this time, deadlock may occur . therefore , The safest way is to execute the callback function , Continue back to the original code path , Continue executing the code that was previously executed for .
边栏推荐
- Geek University cloud native training camp
- Foundations of Cryptography
- Apple mobile phone can see some fun ways to install IPA package
- Shell script
- Comprehensive comparison of the most popular packet capturing tools in the whole network
- HCIA assessment
- data link layer
- Packaging_ Conversion between basic type and string type
- Codeforces Round #720 (Div. 2)
- Physical layer introduction
猜你喜欢

Memcached comprehensive analysis – 3 Deletion mechanism and development direction of memcached

Arkit与Character Creator动画曲线的对接

Oauth2.0 introduction

基于STM32的物联网下智能化养鱼鱼缸控制控制系统

memcached全面剖析–2. 理解memcached的內存存儲

B站带货当学新东方

Realization of truth table assignment by discrete mathematical programming

Dynamic routing protocol rip, OSPF

ping: www.baidu. Com: unknown name or service

Station B takes goods to learn from New Oriental
随机推荐
Adding subscribers to a list using mailchimp's API V3
使用 Go 编程语言 66 个陷阱:Golang 开发者的陷阱和常见错误指北
基于C语言实现的足球信息查询系统 课程报告+项目源码+演示PPT+项目截图
Debugging Analysis of Kernel panics and Kernel oopses using System Map
Pod lifecycle in kubernetes
Big factories go out to sea and lose "posture"
Php-pdo parameter binding problem
Distributed basic concepts
Static routing experiment
JUnit unit test
Web automation: web control interaction / multi window processing / Web page frame
Unity关于本地坐标和世界坐标之间的转换
Go coding specification
Return of missing persons
Failed to open after installing Charles without any prompt
Rip/ospf protocol notes sorting
升哲科技 AI 智能防溺水服务上线
Tutorial on obtaining JD cookies by mobile browser
基于STM32的物联网下智能化养鱼鱼缸控制控制系统
Requests requests for web page garbled code resolution