Skip to content
cpp-bpfPublic

About

C++ library for BPF programs, built on clang and libbpf

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

2 stars

Watchers

1 watching

Forks

cpp-bpf

Test License Status Vision Contributing

cpp-bpf is a C++ library for writing BPF programs. It builds on clang and libbpf rather than replacing them: C++ BPF programs use the same libbpf headers, the same loader and the same skeletons as C programs.

Why cpp-bpf

Its wrappers inline to the same helper calls a C program makes, and it catches more mistakes at compile time instead of at load time:

  • RAII releases locks and ring buffer reservations on every path
  • maps are typed: keys with padding or of the wrong type don't compile, and a lookup result has to be taken out explicitly before it is checked and used
  • program arguments are typed, without casts from the context
  • templates replace macros, and types are shared with C++ userspace code
cppbpf::hash_map<__u32, proc_stats, 10240> procs SEC(".maps");

CPPBPF_PROG("tp_btf/sys_enter", on_sys_enter, struct pt_regs *regs, long id)
{
	__u32 tgid = bpf_get_current_pid_tgid() >> 32;

	proc_stats *s = procs.lookup_or_init(tgid).get();
	if (!s)
		return 0;
	__sync_fetch_and_add(&s->syscalls, 1);
	return 0;
}

The userspace side reads the same map with the same types:

auto obj = cppbpf::user::object<syscount>::open_and_load();
obj.attach();
cppbpf::user::map_ref<__u32, proc_stats> procs(obj->maps.procs);
for (auto [pid, s] : procs)
	std::printf("%u %llu\n", pid, (unsigned long long)s.syscalls);

Status

cpp-bpf is in early development and has no release yet. It needs fixes that are not in released toolchains: the tracking issue lists them.

Getting started

You need:

  • clang with C++ records in BTF (see below)
  • libbpf with its BPF-side headers, and bpftool; tested with libbpf 1.5 and 1.8 (on Debian: libbpf-dev, bpftool, linux-libc-dev; older distributions may need libbpf built from source)
  • Linux 6.1 or newer with BTF

clang

Released clang versions crash or emit wrong BTF on C++ classes with member functions, base classes or the class keyword. The fix, llvm/llvm-project#233715, is under review. Until it is merged, build clang from that branch with scripts/build-clang.sh, the same script CI uses:

scripts/build-clang.sh $HOME/.local/cppbpf-clang

It needs git, cmake, ninja and a C++ compiler, builds only the BPF target and takes 20-40 minutes on a recent machine. Only the BPF programs need this clang; the userspace part builds with any C++20 compiler.

Once the fix is merged, the nightly packages from apt.llvm.org will work, and later LLVM releases.

Example

Build and run the example, which counts system calls per process:

cd examples/syscount
make CLANG=$HOME/.local/cppbpf-clang/bin/clang BPFTOOL=bpftool LIBBPF_INC=/usr/include
sudo build/syscount 5

mk/cppbpf.mk has the build rules and flags for your own programs. C++ BPF programs need -std=gnu++20 -fno-exceptions -fno-asynchronous-unwind-tables -gsimple-template-names, and programs and kfunc declarations must be extern "C" (CPPBPF_PROG does this).

Kernel types

The vmlinux.h that bpftool generates is not valid C++. scripts/gen-vmlinux.py generates one that is, from the kernel BTF, and mk/cppbpf.mk builds it as $(OUT)/include/vmlinux.h, so programs can #include "vmlinux.h". Set VMLINUX_BTF to use another kernel's BTF, or empty to build without it. The header differs from the C one in places:

  • names that are C++ keywords get a ___cpp suffix: struct class___cpp, page->private___cpp;
  • so do a few types whose names clash in C++, e.g. struct irte___cpp;
  • members whose type is an empty struct, such as struct lock_class_key without lockdep, are zero-length arrays, because an empty struct has size 1 in C++;
  • enumerators of an unnamed enum defined inside a struct are in the scope of that struct, as C++ requires.

Read kernel memory with BPF_CORE_READ(): clang ignores preserve_access_index in C++, so plain -> accesses get no CO-RE relocations. Relocations against renamed types work, because libbpf ignores the ___ suffix. Relocations against renamed fields, and against members that became arrays where the kernel type isn't empty, don't match.

Layout

  • include/cppbpf/bpf.hpp: the BPF side (maps, ring buffer, locks, programs)
  • include/cppbpf/user.hpp: the userspace side (objects, maps, ring buffer)
  • mk/cppbpf.mk: build rules
  • examples/, tests/
  • scripts/gen-vmlinux.py: vmlinux.h for C++
  • compat/: checks that the libbpf headers patched for C++ leave C programs unchanged

Users

If you write BPF programs in C++, with or without cpp-bpf, please add yourself to USERS.md.

License

cpp-bpf is dual-licensed under the GNU Lesser General Public License v2.1 or the BSD 2-Clause License, at your option, the same as libbpf.

About

C++ library for BPF programs, built on clang and libbpf

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages