1

I have a bunch of ebpf-programs (using c and libbpf; no BCC/bpftrace) in a folder, let's call them File1.bpf.c, File2.bpf.c, .... Usually, I compile bpf programs with clang and llc, every file by itself:

  1. clang -I [...]/libbpf/build/root/usr/include/ -target bpf -S -D BPF_PROG -D __BPF_TRACING__ -D__TARGET_ARCH_x86 -Wall -O2 -emit-llvm -c -g File1.bpf.c
  2. llc-11 -march=bpf -filetype=obj -o File1.bpf.o File1.bpf.ll

With this, nothing is linked. I might want use bpftool to link some of the files afterwards.

So my questions are:

  1. How to do this two-staged build process in cmake? I have seen some snippets using functions, but it did not work for me and feels very "hacky". Current idea is to use a python-script as a fake-Compiler. For cmake, the compiler is set to be the Python script, which invokes clang and llc. I think this is a possible solution, but it does not "feel right" and has problems, too. Additionally, I wonder why I can not find a Tutorial or a question on Stackoverflow about this topic, which leads to my second question:
  2. Is this procedure up to date? If not: What is the preferred way to handle bpf compilation?

A visualization of the build process I want to achieve in cmake with every row being independent: Structure

I do not want to use skeletons; the bpf objects should be independently loadable afterwards.

When looking online, usually in blogposts clang and llvm are used when it is c code. However, it seems like clang can directly compile to a bpf object, which has the problem that -mcpu cannot be specified 1. With mcpu the instruction set version is set. Therefore, another question is:

  1. When should -mcpu be specified?

Thanks in advance for any help :)

Dennis
  • 150
  • 1
  • 11
  • I've tried to document a bit the `mcpu` parameter at https://pchaigno.github.io/bpf/2021/10/20/ebpf-instruction-sets.html. In most cases you should be fine with the default value for `mcpu`. Hope it helps. – pchaigno Mar 01 '22 at 13:06
  • Thanks @pchaigno; thats very good for understanding the `-mcpu` part. So it should be ok to just ignore that parameter und use clang only; however then I still hav to convince clang to not try linking anything. But better have it configurable? How do you compile your bpf programs? – Dennis Mar 01 '22 at 13:20
  • In Cilium we use the two-step process, calling `clang` then `llc` from the Go process. – pchaigno Mar 01 '22 at 13:39
  • ok thanks! Interesting, tat not a common build system is used. Or is it a combination with make ([https://github.com/cilium/cilium/blob/master/bpf/Makefile.bpf](https://github.com/cilium/cilium/blob/master/bpf/Makefile.bpf))? – Dennis Mar 01 '22 at 14:40

0 Answers0