ebpf learning and practice
- bpfcc-tools
sudo apt-get install bpfcc-tools linux-headers-$(uname -r)
- iovisor packages
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 4052245BD4284CDD
echo "deb https://repo.iovisor.org/apt/$(lsb_release -cs) $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/iovisor.list
sudo apt-get update
sudo apt-get install bcc-tools libbcc-examples linux-headers-$(uname -r)
- build source codes
sudo apt-get -y install linux-headers-$(uname -r)
sudo apt-get -y install bison build-essential cmake flex libedit-dev libllvm6.0 llvm-6.0-dev libclang-6.0-dev python zlib1g-dev libelf-dev libfl-dev python3-distutils
tar zxvf bcc-src-with-submodule.tar.gz #0.24.0
mkdir bcc/build;cd bcc/build
cmake ..
make
sudo make install
cmake -DPYTHON_CMD=python3 ..
pushd src/python/
make
sudo make install
popd
ls -l /usr/sbin/*bpfcc
sudo bashreadline-bpfcc
sudo syncsnoop-bpfcc
or
sudo /usr/share/bcc/tools/tcplife
sudo /usr/share/bcc/tools/tcptracer
sudo /usr/share/bcc/tools/execsnoop
sudo /usr/share/bcc/tools/opensnoop
sudo /usr/share/bcc/tools/biolatency
/usr/share/bcc/tools/trace 'pam:pam_start "%s: %s", arg1, arg2'
install libelf libbpf
more details in samples_bpf_build_in_ubuntu-xenial
在Makefile中修改如下:
hostprogs-y += hello
hello-objs := bpf_load.o libbpf.o hello_user.o
always += hello_kern.o
HOSTLOADLIBES_hello += -lelf
编译: sudo make M=samples/bpf/
使用该 Makefile
然后直接make即可
install libelf
more in samples_bpf_build_in_linux-4.15.0
在Makefile中修改如下:
hostprogs-y += hello
hello-objs := bpf_load.o $(LIBBPF) hello_user.o
always += hello_kern.o
HOSTLOADLIBES_hello += -lelf
编译: sudo make M=samples/bpf/
运行: sudo samples/bpf/hello
bash-4981 [008] .... 1733.269269: 0x00000001: Hello BPF!
bash-4982 [000] .... 1754.544873: 0x00000001: Hello BPF!
clang一次性编译
sudo clang -O2 -Wall -target bpf -I /usr/include/x86_64-linux-gnu/ -c hello_kern.c -o hello_kern.o
clang,llvm前端生成.ll文件,然后llvm后端把.ll文件生成bpf字节码
sudo clang -O2 -Wall -emit-llvm -S hello_kern.c
sudo llc hello_kern.ll -march=bpf -filetype=obj -o hello_kern.o
查看字节码
llvm-objdump -d hello_kern.o
using ebpf for XDP
using ebpf for TC
bpf-maps show in demo
Need To Do ...
sudo snap install --devmode bpftrace
sudo snap connect bpftrace:system-trace
sudo bpftrace -e 'BEGIN {print("hello world!\n")}'
please click here
高效入门eBPF
BPF C编程入门
透过ebpf观察进程行为
ebpf-and-k8s-zh
understanding-ebpf-datapath-in-cilium-zh
how-to-make-linux-microservice-aware-with-cilium-ebpf