Getting Started with XDP--Compilation of the eBPF sample kernel sample code

The Linux kernel code provides a lot of eBPF sample codes (take the linux6.1 kernel code as an example), and we can find the sample codes in the /Linux/samples/bpf directory.

1. View the Linux kernel version

This article does not discuss the situation of cross-compilation, because eBPF is still developing rapidly, and the support of different versions of the Linux kernel is not uniform. So in the first step, we need to check the version number of the local Linux kernel through the uname command, so that the following steps can be carried out.

pi@raspberrypi:/usr/src/linux-6.1/samples/bpf $ uname -a
Linux raspberrypi 6.1.21+ #1642 Mon Apr  3 17:19:14 BST 2023 armv6l GNU/Linux

2. Download the linux kernel code

The Linux kernel code is hosted on github . We can find the kernel code of the corresponding version of the current linux kernel queried in Chapter 1 after pulling down the master on the left. Click to select the v6.1 version as shown in the figure below.
insert image description hereThen select Download ZIP in the drop-down box of code on the left to download directly or copy the URL over HTTPS and download the source code to the local host through git.
insert image description here

3. Copy and install the source code

This step is very simple, just copy or decompress the downloaded file directly to the /usr/src directory.

pi@raspberrypi:~ $ sudo unzip ./linux-6.1.zip -d /usr/src/

After the decompression is complete, check the following command. If you see the /usr/src/linux-6.1/samples directory, it means that the kernel source code has been installed correctly.

pi@raspberrypi:~ $ cd /usr/src/linux-6.1/samples/
pi@raspberrypi:/usr/src/linux-6.1/samples $ ls -l | grep bpf
drwxr-xr-x 2 root root 4096 May 25 13:36 bpf

4. Configure the compilation environment

sudo apt install make
sudo apt install clang
sudo apt install llvm
sudo apt install gcc
sudo apt install flex
sudo apt install bison
sudo make defconfig
sudo make headers_install
sudo make modules_prepare
sudo cp /sys/kernel/btf/vmlinux /usr/src/linux-5.19
sudo ln -s ./x86_64-linux-gnu/sys/ ./sys
sudo ln -s /usr/include/linux/capability.h /usr/include/sys/capability.h
sudo ln -s /usr/lib/x86_64-linux-gnu/libcap.so.2.44 /usr/lib/libcap.so
sudo ln -s /usr/src/linux-5.19/include/asm-generic/ /usr/src/linux-5.19/include/include/asm
sudo ln -s /usr/src/linux-5.19/samples/bpf/Module.symvers /usr/src/linux-5.19

5. Compile

Note that each step in step 4 above must be done, otherwise there will be errors that the header file cannot be found and the library file cannot be found.
Then run the following command to compile all the sample codes successfully.

sudo make M=samples/bpf

Guess you like

Origin blog.csdn.net/meihualing/article/details/130865317