Hello eBPF world! Hello Java world! Let's discover eBPF together and write Java user-land library along the way.
Hello eBPF world! Hello Java world! Let's discover eBPF together and write Java user-land library along the way.
There are user land libraries for eBPF that allow you to write eBPF applications in C++, Rust, Go, Python and even Lua. But there are none for Java, which is a pity. So... I decided to write my own, which allows you to write eBPF programs directly in Java.
This is still in the early stages, but you can already use it for developing small tools and more coming in the future.
Based on the overview from ebpf.io, duke image from OpenJDK.
Let's discover eBPF together. Join me on the journey and learn a lot about eBPF and Java along the way.
Consider for a brief moment that you want to test how your server application behaves when every third incoming network packet is dropped. We can write a simple eBPF program to do this:
…You can find this example as XDPDropEveryThirdPacket.java.
Hooks
kprobe.multi) support. docs/kprobes.md@StructOps — implement kernel struct_ops in Java: write a sched-ext CPU scheduler, TCP congestion controller, qdisc, or HID BPF driver as a plain Java interface. docs/struct-ops.md@StructOps, including a userspace-scheduler bridge. docs/sched-ext/Data & Communication
HASH_OF_MAPS and ARRAY_OF_MAPS for dynamic inner-map selection. docs/map-of-maps.md@BPF programs with @SharedFrom. docs/shared-maps.md@InArena pointers for zero-copy data exchange. docs/arenas.md@BPFTimer. docs/timers.mdProgram Structure
@TailCallSlot, and slots auto-register at load; hot-swap included. docs/tail-calls.mdu64 cookies and kprobe.multi / uprobe.multi. docs/attach-cookies-multi.mdProvide a library (and documentation) for Java developers to explore eBPF and write their own eBPF programs, like firewalls, directly in Java, using the libbpf under the hood.
The goal is neither to replace existing eBPF libraries nor to provide a higher abstractions.
These might change in the future, but for now, you need the following:
Either a Linux machine with the following:
__BPF_FEATURE_ADDR_SPACE_CAST floor)apt install libbpf-dev linux-tools-common linux-tools-$(uname -r) on UbuntuTo verify your setup is complete, run ./scripts/doctor.sh from the
repo root — it walks every prerequisite and prints the apt line to
install anything missing. (On macOS it short-circuits with a pointer
to SYNC_WORKFLOW.md for the split-host workflow.)
hello-ebpf.yaml file as a guide to install the prerequisites):limactl start hello-ebpf.yaml --mount-writable
limactl shell hello-ebpf sudo bin/install.sh
limactl shell hello-ebpf
# You'll need to be root for most of the examples
sudo -s PATH=$PATHThe scheduler examples require a 6.14+ kernel (Ubuntu 25.04 has it, install on older Ubuntus via the mainline tool).
Posts covering the development of this project:
I wrote a few samples that showcase the usage of the library in the bpf-samples module, you can use them as a starting point for your own eBPF programs.
| Inspiration | Name and Java Class | Description |
|---|---|---|
| HelloWorld | A simple hello world example | |
| LogOpenAt2Call | Logs all openat2 calls | |
| Ansil H | RingSample | Record openat2 calls in a ring buffer |
| HashMapSample | Record openat2 calls in a hash map | |
| XDPDropEveryThirdPacket | Use XDP to block every third incoming packet | |
| sematext | XDPPacketFilter | Use XDP to block incoming packages from specific URLs in Java |
| sematext | XDPPacketFilter2 | The previous example but with the eBPF program as C code |
| TCDropEveryThirdOutgoingPacket | Implement a Traffic Control to block every third outgoing packet at random | |
| PacketLogger | TC and XDP based packet logger, capturing incoming and outgoing packets | |
| nfil.dev | CGroupBlockHTTPEgress | Block all outgoing HTTP packets using cgroups |
| demo.ForbiddenFile | Block access to a specific file via openat2 | |
| Firewall | A simple firewall that blocks all incoming packets | |
| FirewallSpring | A spring boot based web front-end for the Firewall | |
| MinimalScheduler | A minimal Linux scheduler |
No open issues yet, or sync has not completed.