ebpf linux go observability videonotes
Core idea
eBPF lets developers load verified programs into the Linux kernel and run them in response to events. Liz Rice builds an example with C for the kernel program and Go for the userspace application. The application loads and attaches the program, then receives event data and counts system calls by command.
Video
The eBPF model
Rice describes eBPF as a Linux kernel technology for dynamically loading custom programs that run when selected events occur. Uses include observability and security. Programs can be changed without rebooting the machine.
Three concepts structure the talk:
- Program: code loaded into the kernel and run when an attached event occurs.
- Hook: the event or attachment point that triggers the program.
- Map: a kernel-managed data structure that can support communication between eBPF programs and userspace.
Userspace interacts with the kernel through system calls. In the demonstration, the bpf system call performs operations such as creating maps, updating map elements, and loading programs.
Verification and compilation
Because the code runs in the kernel, the verifier checks a program before it is accepted. Rice describes writing restricted C, compiling it with Clang into eBPF bytecode, and using helper functions to access selected kernel information or emit debugging output.
Historical context
This talk was recorded in 2021. Rice describes the restrictions relevant to her example, including avoiding loops. Do not treat that statement as a complete description of verifier behavior on current kernels. Helper availability and supported programs depend on the kernel and program type.
Events and attachment
Examples of attachment points include system-call tracepoints, kernel function probes, userspace probes, and network events. Rice distinguishes a kprobe at a kernel function entry from a kretprobe at its return; corresponding userspace probes target userspace functions.
In the traced bpftrace example, loading the program returns a file descriptor. A perf_event_open call creates an event, and an ioctl associates the event with the loaded program. This is the attachment path she demonstrates, not a claim that all eBPF attachment types use the same calls.
Starting point: count system calls with bpftrace
Rice runs a bpftrace script at the system-call entry tracepoint. It counts calls by command and prints the totals when interrupted. She then uses strace to inspect the underlying BPF system calls and show that the concise script creates maps and loads a program.
This example establishes the outcome she later recreates with separate C and Go components. The demonstration runs with elevated privileges; access requirements depend on the system and kernel configuration.
Build a Go and eBPF hello world
The demonstration separates responsibilities:
| Component | Role in the example |
|---|---|
| C and Clang | Define the eBPF function and compile it into an object file. |
| libbpf | Provide a C library abstraction over BPF operations. |
| libbpfgo | Provide Go bindings around libbpf for the userspace application. |
| Go application | Open and load the object, attach its program, consume output, and clean up. |
Rice’s C function writes a debug trace and is associated with a kprobe for the kernel function related to execve. The object file contains program and section information that helps the loader identify the program. The kernel function name is specific to the demonstrated kernel.
The Go application follows this sequence:
- Open the compiled object as a module and arrange cleanup with
defer. - Load the module into the kernel and retrieve the
helloprogram. - Attach the program to the selected kprobe.
- Read and print debug trace output in userspace.
When Rice runs the example, it prints a trace as the associated execve event occurs. Her demo error helper terminates on errors; she explicitly cautions against that approach in production.
Handle shutdown in Go
The initial blocking trace reader prevents the example from reaching its intended cleanup when interrupted. Rice moves the reader into a goroutine, receives the operating-system interrupt on a Go channel, and lets the main function return so deferred cleanup can run.
Send event data through a perf buffer
The initial debug-print approach writes to a shared tracing location. Rice presents it as useful for debugging, not as the preferred output path for a real application where multiple programs may write to the same place.
She replaces it with a perf event array map and a perf buffer:
- The C program defines the map and uses the perf-event output helper. It passes event context, the map, a current-CPU flag, a data pointer, and a size.
- The Go program initializes and starts a perf buffer associated with the map.
- A Go channel receives event payloads as byte slices. A goroutine reads the channel.
- On shutdown, the application stops the buffer.
Rice first sends a fixed unsigned 64-bit value and decodes it as little-endian on her demonstration machine. She then changes the payload to characters representing the current command and converts the received bytes to a string.
Demo simplification
Rice ignores lost events in the perf-buffer example. A production consumer should account for loss rather than assume every event was received. The talk’s byte decoding also reflects the demonstrated payload and machine.
Synthesized data flow:
flowchart TD
A[Kernel event] --> B[Attached eBPF program]
B --> C[Perf event array and buffer]
C --> D[Go event channel]
D --> E[Decode and count]Recreate the counter
Rice adds a Go map from command name to count. The userspace reader increments the count for each received event and prints the totals when the program ends.
At first, the kprobe observes the selected execve path. She then changes both the C section declaration and Go attachment to the raw sys_enter tracepoint. This broadens the example to system-call entries, reproducing the earlier per-command bpftrace counter. Rice reports that the final example uses fewer than 60 lines of Go and a handful of C lines.
The counter described here is in Go userspace. The perf event array transports events from the kernel; it is distinct from the Go map used to aggregate the counts.
Key takeaways
- Choose an event first, then load and attach a program suited to that event.
- Treat the verifier and program-type restrictions as part of development, not an afterthought.
- Use a userspace library to manage loading, attachment, event handling, and cleanup.
- Use debug tracing for initial inspection; use a suitable event data path for an application.
- Track event loss, handle shutdown, and validate assumptions about payload layout and kernel compatibility.