- C 88%
- Zig 11%
- Python <1%
- Makefile <1%
- Docker <1%
Flowd-zig: flow marking daemon but in Zig #
We'll really need to add some content here eventually...
Compiling #
This project currently leverages libbpf for loading and interacting with
eBPF programs. It is compiled from source by including the already bundled version
provided by the fantastic All Your Codebase organisation over at
allyourcodebase/libbpf.
Given libbpf links against libc we can either link it statically or dynamically.
We've chosen the latter as it sidesteps many issues arising when compiling for aarch64
related to atomic primitives and what not. What's more, Zig allows for requiring a
minimum glibc version which we've set to 2.34 as it's what's bundled with AlmaLinux 9.
Despite 'cleanly' including libbpf, the best solution would hinge on providing a Zig
implementation of libbpf à la ebpf-go which is currently out of scope. It
does seem some people in the Zig community have their sights set on this, but some problems
remain:
-
Support for translating structs containing bitfields in Translate C as explained on ziglang/translate-c#179.
-
Native support for eBPF CO:RE so that the Zig compiler can emit the appropriate relocations as explained on ziglang/zig. This would allow for writing eBPF programs directly in Zig!
The good news is this is something in people's mind! Anyway, we can currently compile the code with:
$ zig build
This will produce executables both for x86_64 and aarch64 as well as static libraries
encompassing all the interfaces to eBPF so that other projects can link against them. All
eBPF programs will also be compiled and embedded. These are all provided under zig-out.
The commit compile option allows for embedding an identifier made available to the
root module:
$ zig build -Dcommit=$(git rev-parse --short HEAD)
Other targets can be shown with
$ zig build -l
and the entire build Directed Acyclic Graph (DAG) can be visualised with
$ zig build --summary all
Why not link against musl? #
Well... our C dependencies expect GNU's libc (i.e. glibc): if linking against Musl's libc
a myriad symbols are undefined causing a linking failure. That's quite a bummer...
Bundling Python wheels #
Integrating with Python can be a bit of a headache. Even though it's true it can leverage shared
objects through the standard ctypes module, doing so is 'clunky' in the sense that return and
argument types must be specified; especially if dealing with pointers. A smoother integration can
be attained by directly implementing a Python Extension Module. The result can
later be distributed as a wheel.
We provide such wheels wrapping the marking logic so that it can be easily leveraged from Python. The entire process is automated and can be invoked with:
$ zig build wheel
For the process to work, a Python virtual environment must be active with the requirements listed
in requirements.txt installed. Compilation also requires the necessary Python 3 includes. We could
pull the ones from vanilla Python 3, but that means we have to run
$ ./configure --host=x86_64-linux --build=x86_64-linux
to generate the headers, imposing several requirements on the machine we're compiling on such as
having gcc(1) installed. This is why we have decided to vendor these includes directly as
extracted from the Python 3 installation on an AlmaLinux 9 machine. Given we strictly stick to
the stable C API we don't anticipate this decision to cause any issues.
Testing #
A purposefully defined step exists for running tests:
$ zig build test
If there's no output shown everything looks good!
Running on a container #
Given the binary is statically compile and only depends on glibc we can run it on a regular
container that we can start with:
$ docker run --privileged --name flowd-zig --rm -it -v $(pwd):/root/flowd-zig flowd-zig
Within the container we can simply run
# flowd-zig/zig-out/bin/flowd.aarch64 version
info(main): built commit: unknown
info(main): libbpf version: .{ .major = 1, .minor = 7, .patch = 0, .pre = null, .build = null }
And that's that!