Skip to content

Commit 3a722c0

Browse files
yunwei37LinuxDev9002github-actions[bot]claude
authored
feat: add tcx and bpf token tutorials (#203)
* feat: add tcx and bpf token tutorials * docs: auto-generate documentation * docs: rewrite tcx and bpf_token tutorials with richer content and consistent style Rewrote all 4 README files (EN/ZH for both tutorials) to match the existing tutorial style with detailed background, full code listings, step-by-step explanations, comparison tables, and proper references. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * style: replace em dashes with colons, commas, and parentheses Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: auto-generate documentation * ci: add tcx and bpf_token builds to CI; simplify execl in token_userns_demo - Add make targets for src/50-tcx and src/features/bpf_token to the test-libbpf CI workflow. - Replace four separate execl() calls with a single execv() using a dynamically built argv array, reducing complexity and eliminating CodeFactor command-injection false positives. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * ci: fix mkdocs path in trigger-sync workflow Use .venv/bin/mkdocs instead of bare mkdocs, since make install puts it inside a virtualenv. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: LinuxDev9002 <linuxdev8883@example.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent ee6d522 commit 3a722c0

22 files changed

Lines changed: 2212 additions & 1 deletion

.github/workflows/test-libbpf.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,11 @@ jobs:
111111
- name: test features bpf_wq
112112
run: |
113113
make -C src/features/bpf_wq
114+
115+
- name: test 50 tcx
116+
run: |
117+
make -C src/50-tcx
118+
119+
- name: test features bpf_token
120+
run: |
121+
make -C src/features/bpf_token

.github/workflows/trigger-sync.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
2929
- name: Test page build
3030
run: |
31-
mkdocs build -v
31+
.venv/bin/mkdocs build -v
3232
3333
- name: Trigger sync workflow
3434
if: github.event_name == 'push' && github.ref == 'refs/heads/main'

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Networking:
7474
- [lesson 41-xdp-tcpdump](src/41-xdp-tcpdump/README.md) Capturing TCP Information with XDP
7575
- [lesson 42-xdp-loadbalancer](src/42-xdp-loadbalancer/README.md) XDP Load Balancer
7676
- [lesson 46-xdp-test](src/46-xdp-test/README.md) Building a High-Performance XDP Packet Generator
77+
- [lesson 50-tcx](src/50-tcx/README.md) Composable Traffic Control with TCX Links
7778

7879

7980
Tracing:
@@ -103,6 +104,7 @@ Features:
103104
- [lesson 36-userspace-ebpf](src/36-userspace-ebpf/README.md) Userspace eBPF Runtimes: Overview and Applications
104105
- [lesson 38-btf-uprobe](src/38-btf-uprobe/README.md) Expanding eBPF Compile Once, Run Everywhere(CO-RE) to Userspace Compatibility
105106
- [lesson 43-kfuncs](src/43-kfuncs/README.md) Extending eBPF Beyond Its Limits: Custom kfuncs in Kernel Modules
107+
- [features bpf_token](src/features/bpf_token/README.md) BPF Token for Delegated Privilege and Secure Program Loading
106108
- [features bpf_wq](src/features/bpf_wq/README.md) BPF Workqueues for Asynchronous Sleepable Tasks
107109
- [features struct_ops](src/features/struct_ops/README.md) Extending Kernel Subsystems with BPF struct_ops
108110
- [features dynptr](src/features/dynptr/README.md) BPF Dynamic Pointers for Variable-Length Data

README.zh.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ GPU:
6767
- [lesson 41-xdp-tcpdump](src/41-xdp-tcpdump/README.zh.md) eBPF 示例教程:使用 XDP 捕获 TCP 信息
6868
- [lesson 42-xdp-loadbalancer](src/42-xdp-loadbalancer/README.zh.md) eBPF 开发者教程: 简单的 XDP 负载均衡器
6969
- [lesson 46-xdp-test](src/46-xdp-test/README.zh.md) eBPF 实例教程:构建高性能 XDP 数据包生成器
70+
- [lesson 50-tcx](src/50-tcx/README.zh.md) eBPF 入门实践教程第五十篇:使用 TCX Link 实现可组合的流量控制
7071
安全:
7172

7273
- [lesson 24-hide](src/24-hide/README.zh.md) eBPF 开发实践:使用 eBPF 隐藏进程或文件信息
@@ -81,6 +82,7 @@ GPU:
8182
- [lesson 36-userspace-ebpf](src/36-userspace-ebpf/README.zh.md) 用户空间 eBPF 运行时:深度解析与应用实践
8283
- [lesson 38-btf-uprobe](src/38-btf-uprobe/README.zh.md) 借助 eBPF 和 BTF,让用户态也能一次编译、到处运行
8384
- [lesson 43-kfuncs](src/43-kfuncs/README.zh.md) 超越 eBPF 的极限:在内核模块中定义自定义 kfunc
85+
- [features bpf_token](src/features/bpf_token/README.zh.md) eBPF 入门实践教程:BPF Token,安全的委托式权限与程序加载
8486
- [features bpf_wq](src/features/bpf_wq/README.zh.md) eBPF 教程:BPF 工作队列用于异步可睡眠任务
8587
- [features struct_ops](src/features/struct_ops/README.zh.md) eBPF 教程:使用 BPF struct_ops 扩展内核子系统
8688
- [features dynptr](src/features/dynptr/README.zh.md) BPF Dynamic Pointers for Variable-Length Data

src/50-tcx/.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
level=Depth
2+
type=Networking

src/50-tcx/.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Build artifacts
2+
.output/
3+
*.o
4+
*.skel.h
5+
6+
# Generated binaries
7+
tcx_demo
8+
9+
# Editor files
10+
*.swp
11+
*~
12+
.vscode/

src/50-tcx/Makefile

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2+
OUTPUT := .output
3+
CLANG ?= clang
4+
LIBBPF_SRC := $(abspath ../third_party/libbpf/src)
5+
BPFTOOL_SRC := $(abspath ../third_party/bpftool/src)
6+
LIBBPF_OBJ := $(abspath $(OUTPUT)/libbpf.a)
7+
BPFTOOL_OUTPUT ?= $(abspath $(OUTPUT)/bpftool)
8+
BPFTOOL ?= $(BPFTOOL_OUTPUT)/bootstrap/bpftool
9+
ARCH ?= $(shell uname -m | sed 's/x86_64/x86/' \
10+
| sed 's/arm.*/arm/' \
11+
| sed 's/aarch64/arm64/' \
12+
| sed 's/ppc64le/powerpc/' \
13+
| sed 's/mips.*/mips/' \
14+
| sed 's/riscv64/riscv/' \
15+
| sed 's/loongarch64/loongarch/')
16+
VMLINUX := ../third_party/vmlinux/$(ARCH)/vmlinux.h
17+
INCLUDES := -I$(OUTPUT) -I../third_party/libbpf/include/uapi -I$(dir $(VMLINUX)) -I.
18+
CFLAGS := -g -Wall
19+
ALL_LDFLAGS := $(LDFLAGS) $(EXTRA_LDFLAGS)
20+
21+
APPS = tcx_demo
22+
23+
CLANG_BPF_SYS_INCLUDES ?= $(shell $(CLANG) -v -E - </dev/null 2>&1 \
24+
| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }')
25+
26+
ifeq ($(V),1)
27+
Q =
28+
msg =
29+
else
30+
Q = @
31+
msg = @printf ' %-8s %s%s\n' \
32+
"$(1)" \
33+
"$(patsubst $(abspath $(OUTPUT))/%,%,$(2))" \
34+
"$(if $(3), $(3))";
35+
MAKEFLAGS += --no-print-directory
36+
endif
37+
38+
define allow-override
39+
$(if $(or $(findstring environment,$(origin $(1))),\
40+
$(findstring command line,$(origin $(1)))),,\
41+
$(eval $(1) = $(2)))
42+
endef
43+
44+
$(call allow-override,CC,$(CROSS_COMPILE)cc)
45+
$(call allow-override,LD,$(CROSS_COMPILE)ld)
46+
47+
.PHONY: all
48+
all: $(APPS)
49+
50+
.PHONY: clean
51+
clean:
52+
$(call msg,CLEAN)
53+
$(Q)rm -rf $(OUTPUT) $(APPS)
54+
55+
$(OUTPUT) $(OUTPUT)/libbpf $(BPFTOOL_OUTPUT):
56+
$(call msg,MKDIR,$@)
57+
$(Q)mkdir -p $@
58+
59+
$(LIBBPF_OBJ): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(OUTPUT)/libbpf
60+
$(call msg,LIB,$@)
61+
$(Q)$(MAKE) -C $(LIBBPF_SRC) BUILD_STATIC_ONLY=1 \
62+
OBJDIR=$(dir $@)/libbpf DESTDIR=$(dir $@) \
63+
INCLUDEDIR= LIBDIR= UAPIDIR= \
64+
install
65+
66+
$(BPFTOOL): | $(BPFTOOL_OUTPUT)
67+
$(call msg,BPFTOOL,$@)
68+
$(Q)$(MAKE) ARCH= CROSS_COMPILE= OUTPUT=$(BPFTOOL_OUTPUT)/ -C $(BPFTOOL_SRC) bootstrap
69+
70+
$(OUTPUT)/%.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(VMLINUX) | $(OUTPUT) $(BPFTOOL)
71+
$(call msg,BPF,$@)
72+
$(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \
73+
$(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) \
74+
-c $(filter %.c,$^) -o $(patsubst %.bpf.o,%.tmp.bpf.o,$@)
75+
$(Q)$(BPFTOOL) gen object $@ $(patsubst %.bpf.o,%.tmp.bpf.o,$@)
76+
77+
$(OUTPUT)/%.skel.h: $(OUTPUT)/%.bpf.o | $(OUTPUT) $(BPFTOOL)
78+
$(call msg,GEN-SKEL,$@)
79+
$(Q)$(BPFTOOL) gen skeleton $< > $@
80+
81+
$(patsubst %,$(OUTPUT)/%.o,$(APPS)): %.o: %.skel.h
82+
83+
$(OUTPUT)/%.o: %.c $(wildcard %.h) | $(OUTPUT)
84+
$(call msg,CC,$@)
85+
$(Q)$(CC) $(CFLAGS) $(INCLUDES) -c $(filter %.c,$^) -o $@
86+
87+
$(APPS): %: $(OUTPUT)/%.o $(LIBBPF_OBJ) | $(OUTPUT)
88+
$(call msg,BINARY,$@)
89+
$(Q)$(CC) $(CFLAGS) $^ $(ALL_LDFLAGS) -lelf -lz -o $@
90+
91+
.DELETE_ON_ERROR:
92+
.SECONDARY:

0 commit comments

Comments
 (0)