{"id":37834,"date":"2025-04-22T22:03:35","date_gmt":"2025-04-22T14:03:35","guid":{"rendered":"https:\/\/www.yunshan.net\/blog\/?p=37834"},"modified":"2025-04-22T22:03:35","modified_gmt":"2025-04-22T14:03:35","slug":"ebpf%e7%a8%8b%e5%ba%8f%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e5%af%b9%e7%b3%bb%e7%bb%9f%e6%9c%8d%e5%8a%a1%e7%9a%84%e5%ae%89%e5%85%a8%e7%ad%96%e7%95%a5%e5%ae%9e%e6%96%bd","status":"publish","type":"post","link":"https:\/\/www.yunshan.net\/blog\/archives\/37834.html","title":{"rendered":"EBPF\u7a0b\u5e8f\u5982\u4f55\u5b9e\u73b0\u5bf9\u7cfb\u7edf\u670d\u52a1\u7684\u5b89\u5168\u7b56\u7565\u5b9e\u65bd"},"content":{"rendered":"<p style=\"text-align: center;\">\n<p><img decoding=\"async\" src=\"https:\/\/www.yunshan.net\/blog\/wp-content\/uploads\/2025\/04\/NA93ddI.png\" alt=\"EBPF\u7a0b\u5e8f\u5982\u4f55\u5b9e\u73b0\u5bf9\u7cfb\u7edf\u670d\u52a1\u7684\u5b89\u5168\u7b56\u7565\u5b9e\u65bd\" ><\/p>\n<\/p>\n<p><strong>EBPF\uff08Extended Berkeley Packet Filter\uff09<\/strong> \u662f\u4e00\u79cd\u5185\u6838\u6280\u672f\uff0c\u5141\u8bb8\u7528\u6237\u5728\u4e0d\u4fee\u6539\u5185\u6838\u6e90\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\uff0c\u5728\u5185\u6838\u6001\u8fd0\u884c\u81ea\u5b9a\u4e49\u7684\u7a0b\u5e8f\u3002EBPF\u6700\u521d\u7528\u4e8e\u7f51\u7edc\u6570\u636e\u5305\u8fc7\u6ee4\uff0c\u4f46\u73b0\u5728\u5df2\u7ecf\u6269\u5c55\u5230\u4e86\u7cfb\u7edf\u8c03\u7528\u8fc7\u6ee4\u3001\u6027\u80fd\u5206\u6790\u3001\u5b89\u5168\u7b56\u7565\u5b9e\u65bd\u7b49\u591a\u4e2a\u9886\u57df\u3002<\/p>\n<h2>\u4e8c\u3001EBPF\u7a0b\u5e8f\u7c7b\u578b<\/h2>\n<p>EBPF\u7a0b\u5e8f\u53ef\u4ee5\u5206\u4e3a\u4e24\u79cd\u7c7b\u578b\uff1a<\/p>\n<p>1. <strong>\u7f51\u7edc\u7c7b\u578b<\/strong>\uff1a\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u7f51\u7edc\u6570\u636e\u5305\uff0c\u53ef\u4ee5\u6302\u8f7d\u5230\u7f51\u7edc\u63a5\u53e3\u3001IPtables\u7b49\u4f4d\u7f6e\u3002<\/p>\n<p>2. <strong>\u7cfb\u7edf\u8c03\u7528\u7c7b\u578b<\/strong>\uff1a\u7528\u4e8e\u62e6\u622a\u548c\u8fc7\u6ee4\u7cfb\u7edf\u8c03\u7528\uff0c\u53ef\u4ee5\u6302\u8f7d\u5230\u7cfb\u7edf\u8c03\u7528\u8868\u4e2d\u3002<\/p>\n<h2>\u4e09\u3001\u5b89\u5168\u7b56\u7565\u5b9e\u65bd\u6b65\u9aa4<\/h2>\n<h2>1. \u5b9a\u4e49\u5b89\u5168\u7b56\u7565<\/h2>\n<p>\u9700\u8981\u660e\u786e\u8981\u5b9e\u65bd\u7684\u5b89\u5168\u7b56\u7565\u3002\u4f8b\u5982\uff0c\u7981\u6b62\u7279\u5b9a\u7528\u6237\u6267\u884c\u67d0\u4e9b\u7cfb\u7edf\u547d\u4ee4\uff0c\u6216\u8005\u9650\u5236\u7f51\u7edc\u6d41\u91cf\u7684\u6765\u6e90\u548c\u53bb\u5411\u3002<\/p>\n<h2>2. \u7f16\u5199EBPF\u7a0b\u5e8f<\/h2>\n<p>\u6839\u636e\u5b89\u5168\u7b56\u7565\u7f16\u5199\u76f8\u5e94\u7684EBPF\u7a0b\u5e8f\u3002\u5bf9\u4e8e\u7cfb\u7edf\u8c03\u7528\u8fc7\u6ee4\uff0c\u53ef\u4ee5\u4f7f\u7528SEC(&#8220;tracepoint\/syscalls\/sys_enter_execve&#8221;)\u6765\u62e6\u622aexecve\u7cfb\u7edf\u8c03\u7528\uff0c\u68c0\u67e5\u4f20\u9012\u7ed9execve\u7684\u53c2\u6570\u662f\u5426\u5305\u542b\u88ab\u7981\u6b62\u7684\u547d\u4ee4\u3002\u5bf9\u4e8e\u7f51\u7edc\u8fc7\u6ee4\uff0c\u53ef\u4ee5\u4f7f\u7528SEC(&#8220;xdp&#8221;)\u6765\u7f16\u5199XDP\uff08eXpress Data Path\uff09\u7a0b\u5e8f\uff0c\u68c0\u67e5\u7f51\u7edc\u6570\u636e\u5305\u7684\u6e90\u5730\u5740\u3001\u76ee\u7684\u5730\u5740\u3001\u7aef\u53e3\u53f7\u7b49\u4fe1\u606f\u3002<\/p>\n<p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>include <linux>\ninclude <linux>\ninclude <linux>\ninclude <linux>\ninclude <linux>\n<p>include <bpf><\/p>\n<p>include <bpf><\/p>\n<p>\/\/ \u5b9a\u4e49\u5b89\u5168\u7b56\u7565\uff1a\u7981\u6b62\u8bbf\u95ee\u7279\u5b9aIP\u5730\u5740<\/p>\n<p>__u32 blocked_ip = 0xC0A80164;<\/p>\n<p>\/\/ XDP\u7a0b\u5e8f\uff0c\u7528\u4e8e\u8fc7\u6ee4\u7f51\u7edc\u6570\u636e\u5305<\/p>\n<p>SEC(&#8220;xdp&#8221;)<\/p>\n<p>int xdp_drop_blocked_ip(struct xdp_md ctx) {<\/p>\n<p>void data = (void )(long)ctx->data;<\/p>\n<p>void data_end = (void )(long)ctx->data_end;<\/p>\n<p>struct ethhdr eth = data;<\/p>\n<p>if ((void )eth + sizeof(eth) &lt;= data_end) {<\/p>\n<p>struct iphdr ip = data + sizeof(eth);<\/p>\n<p>if ((void )ip + sizeof(ip) &lt;= data_end) {<\/p>\n<p>if (ip->saddr == blocked_ip) {<\/p>\n<p>return XDP_DROP;<\/p>\n<p>return XDP_PASS;<\/p>\n<p>char _license[] SEC(&#8220;license&#8221;) = &#8220;GPL&#8221;;<\/p>\n<h2>3. \u7f16\u8bd1\u548c\u52a0\u8f7dEBPF\u7a0b\u5e8f<\/h2>\n<p>\u4f7f\u7528LLVM\u7f16\u8bd1\u5668\u5c06\u7f16\u5199\u597d\u7684EBPF\u7a0b\u5e8f\u7f16\u8bd1\u6210\u5b57\u8282\u7801\uff0c\u7136\u540e\u4f7f\u7528bpftool\u6216\u5176\u4ed6\u5de5\u5177\u5c06\u5b57\u8282\u7801\u52a0\u8f7d\u5230\u5185\u6838\u4e2d\u3002\u52a0\u8f7d\u8fc7\u7a0b\u4e2d\uff0cEBPF\u7a0b\u5e8f\u4f1a\u88ab\u9a8c\u8bc1\u4ee5\u786e\u4fdd\u5176\u5b89\u5168\u6027\u548c\u6b63\u786e\u6027\u3002<\/p>\n<p>\u793a\u4f8b\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n<p>bash<\/p>\n<p>clang -O2 -target bpf -c xdp_drop_blocked_ip.c -o xdp_drop_blocked_ip.o<\/p>\n<p>sudo bpftool prog load xdp_drop_blocked_ip.o \/sys\/fs\/bpf\/xdp_drop_blocked_ip<\/p>\n<h2>4. \u5c06EBPF\u7a0b\u5e8f\u6302\u8f7d\u5230\u5408\u9002\u7684\u94a9\u5b50\u70b9<\/h2>\n<p>\u6839\u636eEBPF\u7a0b\u5e8f\u7684\u7c7b\u578b\uff0c\u5c06\u5176\u6302\u8f7d\u5230\u76f8\u5e94\u7684\u5185\u6838\u94a9\u5b50\u70b9\u4e0a\u3002\u5bf9\u4e8e\u7cfb\u7edf\u8c03\u7528\u8fc7\u6ee4\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u6302\u8f7d\u5230\u7cfb\u7edf\u8c03\u7528\u8868\u4e2d\uff1b\u5bf9\u4e8e\u7f51\u7edc\u8fc7\u6ee4\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u6302\u8f7d\u5230\u7f51\u7edc\u63a5\u53e3\u4e0a\u3002<\/p>\n<p>\u793a\u4f8b\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n<p>bash<\/p>\n<p>sudo bpftool cgroup attach \/sys\/fs\/cgroup\/unified\/ ebpf pinned \/sys\/fs\/bpf\/xdp_drop_blocked_ip<\/p>\n<h2>5. \u76d1\u63a7\u548c\u8c03\u6574<\/h2>\n<p>\u4f7f\u7528\u5de5\u5177\u5982bpftrace\u6216perf\u6765\u76d1\u63a7EBPF\u7a0b\u5e8f\u7684\u6267\u884c\u60c5\u51b5\uff0c\u786e\u4fdd\u5176\u6309\u9884\u671f\u5de5\u4f5c\u3002\u5982\u679c\u9700\u8981\u8c03\u6574\u5b89\u5168\u7b56\u7565\uff0c\u53ef\u4ee5\u4fee\u6539EBPF\u7a0b\u5e8f\u5e76\u91cd\u65b0\u7f16\u8bd1\u548c\u52a0\u8f7d\u3002<\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0cEBPF\u7a0b\u5e8f\u53ef\u4ee5\u6709\u6548\u5730\u5b9e\u73b0\u5bf9\u7cfb\u7edf\u670d\u52a1\u7684\u5b89\u5168\u7b56\u7565\u5b9e\u65bd\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u63d0\u4f9b\u4e86\u9ad8\u5ea6\u7684\u7075\u6d3b\u6027\uff0c\u8fd8\u80fd\u5728\u4e0d\u4fee\u6539\u5185\u6838\u6e90\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u589e\u5f3a\u7cfb\u7edf\u7684\u5b89\u5168\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>EBPF\uff08Extended Berkeley Packet Filter\uff09 \u662f\u4e00\u79cd\u5185\u6838\u6280\u672f\uff0c\u5141\u8bb8\u7528\u6237\u5728\u4e0d\u4fee\u6539\u5185\u6838\u6e90\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\uff0c\u5728\u5185\u6838\u6001\u8fd0\u884c\u81ea\u5b9a\u4e49\u7684\u7a0b\u5e8f\u3002EBPF\u6700\u521d\u7528\u4e8e\u7f51\u7edc\u6570\u636e\u5305\u8fc7\u6ee4\uff0c\u4f46\u73b0\u5728\u5df2\u7ecf\u6269\u5c55\u5230\u4e86\u7cfb\u7edf\u8c03\u7528\u8fc7\u6ee4\u3001\u6027\u80fd\u5206\u6790\u3001\u5b89\u5168\u7b56\u7565\u5b9e\u65bd\u7b49\u591a\u4e2a\u9886\u57df\u3002 \u4e8c\u3001EBPF\u7a0b\u5e8f\u7c7b\u578b EBPF\u7a0b\u5e8f\u53ef\u4ee5\u5206\u4e3a\u4e24\u79cd\u7c7b\u578b\uff1a 1. \u7f51\u7edc\u7c7b\u578b\uff1a\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u7f51\u7edc\u6570\u636e\u5305\uff0c\u53ef\u4ee5\u6302\u8f7d\u5230\u7f51\u7edc\u63a5\u53e3\u3001IPtables\u7b49\u4f4d\u7f6e\u3002 2. \u7cfb\u7edf\u8c03\u7528\u7c7b\u578b\uff1a\u7528\u4e8e\u62e6\u622a\u548c\u8fc7\u6ee4\u7cfb\u7edf\u8c03\u7528\uff0c\u53ef\u4ee5\u6302\u8f7d\u5230\u7cfb\u7edf\u8c03\u7528\u8868\u4e2d\u3002 \u4e09\u3001\u5b89\u5168\u7b56\u7565\u5b9e\u65bd\u6b65\u9aa4 1. \u5b9a\u4e49\u5b89\u5168\u7b56\u7565 \u9700\u8981\u660e\u786e\u8981\u5b9e\u65bd\u7684\u5b89\u5168\u7b56\u7565\u3002\u4f8b\u5982\uff0c\u7981\u6b62\u7279\u5b9a\u7528\u6237\u6267\u884c\u67d0\u4e9b\u7cfb\u7edf\u547d\u4ee4\uff0c\u6216\u8005\u9650\u5236\u7f51\u7edc\u6d41\u91cf\u7684\u6765\u6e90\u548c\u53bb\u5411\u3002 2. \u7f16\u5199EBPF\u7a0b\u5e8f \u6839\u636e\u5b89\u5168\u7b56\u7565\u7f16\u5199\u76f8\u5e94\u7684EBPF\u7a0b\u5e8f\u3002\u5bf9\u4e8e\u7cfb\u7edf\u8c03\u7528\u8fc7\u6ee4\uff0c\u53ef\u4ee5\u4f7f\u7528SEC(&#8220;tracepoint\/syscalls\/sys_enter_execve&#8221;)\u6765\u62e6\u622aexecve\u7cfb\u7edf\u8c03\u7528\uff0c\u68c0\u67e5\u4f20\u9012\u7ed9execve\u7684\u53c2\u6570\u662f\u5426\u5305\u542b\u88ab\u7981\u6b62\u7684\u547d\u4ee4\u3002\u5bf9\u4e8e\u7f51\u7edc\u8fc7\u6ee4\uff0c\u53ef\u4ee5\u4f7f\u7528SEC(&#8220;xdp&#8221;)\u6765\u7f16\u5199XDP\uff08eXpress Data Path\uff09\u7a0b\u5e8f\uff0c\u68c0\u67e5\u7f51\u7edc\u6570\u636e\u5305\u7684\u6e90\u5730\u5740\u3001\u76ee\u7684\u5730\u5740\u3001\u7aef\u53e3\u53f7\u7b49\u4fe1\u606f\u3002 \u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a include include include include include include include \/\/ \u5b9a\u4e49\u5b89\u5168\u7b56\u7565\uff1a\u7981\u6b62\u8bbf\u95ee\u7279\u5b9aIP\u5730\u5740 __u32 blocked_ip = 0xC0A80164; \/\/ XDP\u7a0b\u5e8f\uff0c\u7528\u4e8e\u8fc7\u6ee4\u7f51\u7edc\u6570\u636e\u5305 SEC(&#8220;xdp&#8221;) int xdp_drop_blocked_ip(struct xdp_md ctx) { void data = (void )(long)ctx->data; void data_end = (void )(long)ctx->data_end; struct ethhdr eth [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-37834","post","type-post","status-publish","format-standard","hentry","category-discussion"],"_links":{"self":[{"href":"https:\/\/www.yunshan.net\/blog\/wp-json\/wp\/v2\/posts\/37834","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yunshan.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yunshan.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yunshan.net\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yunshan.net\/blog\/wp-json\/wp\/v2\/comments?post=37834"}],"version-history":[{"count":1,"href":"https:\/\/www.yunshan.net\/blog\/wp-json\/wp\/v2\/posts\/37834\/revisions"}],"predecessor-version":[{"id":37836,"href":"https:\/\/www.yunshan.net\/blog\/wp-json\/wp\/v2\/posts\/37834\/revisions\/37836"}],"wp:attachment":[{"href":"https:\/\/www.yunshan.net\/blog\/wp-json\/wp\/v2\/media?parent=37834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yunshan.net\/blog\/wp-json\/wp\/v2\/categories?post=37834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yunshan.net\/blog\/wp-json\/wp\/v2\/tags?post=37834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}