Search code examples
linuxsocketslinux-kernelbpfebpf

How to build Linux kernel to support SO_ATTACH_BPF socket option?


I want to build a application which supports eBPF on CentOS 7 (the kernel version is 3.10.0):

if(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, prog_fd, sizeof(prog_f)) {
    ......
}

So I download a 4.0.5 version, make the following configurations on:

CONFIG_BPF=y
CONFIG_BPF_SYSCALL=y

Then follow this link to build and install a 4.0.5 kernel.

After executing make modules_install install, I find there is still no SO_ATTACH_BPF in <asm-generic/socket.h>, so the above code can't be compiled successfully.

How to build Linux kernel to support SO_ATTACH_BPF socket option?


Solution

  • In my setup, which is based on Fedora 21, I use very similar steps to those you linked to compile and install the latest kernel. As an additional step, I will do the following from the kernel build tree to install the kernel header files into /usr/local/include:

    sudo make INSTALL_HDR_PATH=/usr/local headers_install
    

    This will cause both the stock kernel header files to remain installed in /usr/include/{linux,asm,asm-generic,...}, and the new kernel header files to be installed in /usr/local/include/{linux,asm,asm-generic,...}. During your test program compile, depending on which build system you use, you may need to prefix gcc/clang with -I/usr/local/include.