Skip to content

Commit 944a1ee

Browse files
committed
BPF : using ubuntu jammy environment
1 parent 06c3af8 commit 944a1ee

File tree

1 file changed

+63
-15
lines changed

1 file changed

+63
-15
lines changed

x86/bpf/README.md

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,58 @@
77
cd /usr/src/linux
88
make defconfig && make && make headers_install
99

10-
## 使用demo
10+
## Using Ubuntu 22.04(jammy) as example
1111

12-
在当前目录操作使用对应文件
12+
[Ubuntu get source code](https://wiki.ubuntu.com/Kernel/SourceCode)
13+
[Ubuntu Build Your Own Kernel](https://wiki.ubuntu.com/Kernel/BuildYourOwnKernel)
1314

14-
ln -s $PWD/demo_kern.c /usr/src/linux/samples/bpf
15-
ln -s $PWD/demo_user.c /usr/src/linux/samples/bpf
16-
ln -s $PWD/Makefile /usr/src/linux/samples/bpf
15+
安装必要的软件依赖
1716

18-
在当前目录编译时指定kernel代码路径
17+
apt install -y dpkg-dev
18+
apt-get build-dep linux linux-image-$(uname -r)
19+
apt-get install libncurses-dev gawk flex bison openssl libssl-dev dkms \
20+
libelf-dev libudev-dev libpci-dev libiberty-dev autoconf llvm libcap2
1921

20-
make KERNEL_DIR=/usr/src/linux
22+
ubuntu解决安装包冲突问题
2123

22-
用file查看下编译输出文件(demo_user.o和demo_kern.o)
24+
apt-get install aptitude
25+
aptitude install <package-name>
2326

24-
demo_user.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
25-
demo_kern.o: ELF 64-bit LSB relocatable, eBPF, version 1 (SYSV), with debug_info, not stripped
27+
安装代码(等价于下面的git命令)到/usr/src/linux目录下
2628

27-
- demo_kern.c中的文件是被编译成eBPF的二进制文件,是要被eBPF虚拟机执行的代码
28-
- demo_user.c是将eBPF二进制文件加载到内核的代码
29+
apt-get -y source linux-image-unsigned-$(uname -r)
30+
==> git clone git://git.launchpad.net/~ubuntu-kernel/ubuntu/+source/linux/+git/jammy
31+
32+
cd /path/to/download/ubuntu/source
33+
ln -s $PWD /usr/src/linux
34+
35+
修改配置
36+
37+
chmod +x scripts/*
38+
make clean
39+
make defconfig
40+
./scripts/config -e CONFIG_FTRACE
41+
./scripts/config -e CONFIG_DEBUG_INFO
42+
./scripts/config -e CONFIG_DEBUG_INFO_DWARF5
43+
./scripts/config -e CONFIG_BPF_SYSCALL
44+
./scripts/config -e CONFIG_DEBUG_INFO_BTF
45+
./scripts/config -d CONFIG_DEBUG_INFO_REDUCED
46+
yes "" | make oldconfig
2947

30-
## 使用内核中的samples(commit bpf: add sample usages for persistent maps/progs)
48+
安装头文件到当前编译目录
3149

32-
编译bpf模块
50+
make headers_install
51+
52+
编译内核
53+
54+
make -j$(nproc) \
55+
KERNEL_DIR=/usr/src/linux \
56+
BPF_TOOLS_PATH=bpf_tools \
57+
BPF_EXAMPLES_PATH=bpf_examples \
58+
CC=/usr/bin/gcc
59+
60+
编译内核自带的用例
3361

34-
cd /usr/src/linux
3562
make M=samples/bpf
3663

3764
或者在bpf目录编译
@@ -66,3 +93,24 @@ get elm
6693
./fds_example -F /sys/fs/bpf/m -G -m -k 1
6794
bpf: get fd:3 (Success)
6895
bpf: fd:3 l->(1):24 ret:(0,Success)
96+
97+
## 使用本demo
98+
99+
在当前目录操作使用对应文件
100+
101+
ln -s $PWD/demo_kern.c /usr/src/linux/samples/bpf
102+
ln -s $PWD/demo_user.c /usr/src/linux/samples/bpf
103+
mv /usr/src/linux/samples/bpf/Makefile /usr/src/linux/samples/bpf/Makefile-org
104+
ln -s $PWD/Makefile /usr/src/linux/samples/bpf
105+
106+
在当前目录编译时指定kernel代码路径
107+
108+
make KERNEL_DIR=/usr/src/linux
109+
110+
用file查看下编译输出文件(demo_user.o和demo_kern.o)
111+
112+
demo_user.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
113+
demo_kern.o: ELF 64-bit LSB relocatable, eBPF, version 1 (SYSV), with debug_info, not stripped
114+
115+
- demo_kern.c中的文件是被编译成eBPF的二进制文件,是要被eBPF虚拟机执行的代码
116+
- demo_user.c是将eBPF二进制文件加载到内核的代码

0 commit comments

Comments
 (0)