|  | 
| 7 | 7 | 	cd /usr/src/linux | 
| 8 | 8 | 	make defconfig && make && make headers_install | 
| 9 | 9 | 
 | 
| 10 |  | -## 使用demo | 
|  | 10 | +## Using Ubuntu 22.04(jammy) as example | 
| 11 | 11 | 
 | 
| 12 |  | -在当前目录操作使用对应文件 | 
|  | 12 | +[Ubuntu get source code](https://wiki.ubuntu.com/Kernel/SourceCode) | 
|  | 13 | +[Ubuntu Build Your Own Kernel](https://wiki.ubuntu.com/Kernel/BuildYourOwnKernel) | 
| 13 | 14 | 
 | 
| 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 | +安装必要的软件依赖 | 
| 17 | 16 | 
 | 
| 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 | 
| 19 | 21 | 
 | 
| 20 |  | -    make KERNEL_DIR=/usr/src/linux | 
|  | 22 | +ubuntu解决安装包冲突问题 | 
| 21 | 23 | 
 | 
| 22 |  | -用file查看下编译输出文件(demo_user.o和demo_kern.o) | 
|  | 24 | +	apt-get install aptitude | 
|  | 25 | +	aptitude install <package-name> | 
| 23 | 26 | 
 | 
| 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目录下 | 
| 26 | 28 | 
 | 
| 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 | 
| 29 | 47 | 
 | 
| 30 |  | -## 使用内核中的samples(commit bpf: add sample usages for persistent maps/progs) | 
|  | 48 | +安装头文件到当前编译目录 | 
| 31 | 49 | 
 | 
| 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 | +编译内核自带的用例 | 
| 33 | 61 | 
 | 
| 34 |  | -    cd /usr/src/linux | 
| 35 | 62 | 	make M=samples/bpf | 
| 36 | 63 | 
 | 
| 37 | 64 | 或者在bpf目录编译 | 
| @@ -66,3 +93,24 @@ get elm | 
| 66 | 93 | 	./fds_example -F /sys/fs/bpf/m -G -m -k 1 | 
| 67 | 94 | 	bpf: get fd:3 (Success) | 
| 68 | 95 | 	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