BPF Helpers
BPF helper functions are kernel-provided routines callable from BPF programs. In Whistler, call them by name in function position. Arguments are passed in registers R1--R5 and the return value is in R0.
(get-current-pid-tgid) ; 0 args
(probe-read-user dst size src) ; 3 args
(get-current-comm buf-ptr buf-size) ; 2 args
(ringbuf-reserve map-name size flags) ; 3 args (special: map arg)
The compiler checks each call's argument count.
Every helper the kernel has
Every helper in the kernel's UAPI header can be called. The name is the
kernel's minus the bpf_ prefix, with hyphens for underscores:
bpf_get_numa_node_id is (get-numa-node-id), bpf_setsockopt is
(setsockopt ...).
The list comes from src/helper-table.lisp, generated from
linux/bpf.h and shipped with Whistler, so every helper is available
whether or not the machine has kernel headers or BTF. Helper IDs are
kernel ABI and never change. For each helper the table records its ID,
argument names and C prototype; the argument count is checked, and
trace-printk, the one variadic helper, takes its format, the format's
size, and up to three values. Argument types are left to the verifier,
which checks them when the program loads.
A helper newer than the table is still available when the running
kernel has BTF: Whistler reads /sys/kernel/btf/vmlinux at compile
time and takes its ID and arity from there. Maintainers refresh the
table with make helper-table (see scripts/gen-helper-table.lisp).
A helper name never hides a macro: bpf_loop exists, but (loop ...)
in a program is still CL's loop, and a macro of your own named like a
helper expands as usual.
Argument lists in your editor
The package whistler/helpers exports one function per helper, carrying
its argument names, with its C prototype as documentation. In an editor
connected through SLIME or Sly, typing (setsockopt shows
(setsockopt bpf-socket level optname optval optlen), and describing it
shows long bpf_setsockopt(void *bpf_socket, int level, ...).
whistler-user uses whistler/helpers. To get the same in your own
package, add it to the :use list (see
Using Whistler from your own package).
Prefer :use over :import-from of particular helpers: a helper newer
than the table exists only on kernels that have it. The functions only
document; calling one from ordinary Lisp signals an error, because
helpers run in the kernel. Compiling doesn't need the package at all:
a helper call is recognized by name in any package.
Common helpers
| Helper | ID | Args | Description |
|---|---|---|---|
map-lookup-elem | 1 | -- | (use map-lookup instead) |
map-update-elem | 2 | -- | (use map-update instead) |
map-delete-elem | 3 | -- | (use map-delete instead) |
probe-read | 4 | 3 | Read kernel memory (legacy) |
ktime-get-ns | 5 | 0 | Monotonic clock, nanoseconds |
trace-printk | 6 | 2--5 | Debug printf to trace_pipe (format, its size, up to 3 values) |
get-prandom-u32 | 7 | 0 | Pseudo-random u32 |
get-smp-processor-id | 8 | 0 | Current CPU number |
tail-call | 12 | -- | (use the tail-call form instead) |
get-current-pid-tgid | 14 | 0 | PID in low 32, TGID in high 32 |
get-current-uid-gid | 15 | 0 | UID in low 32, GID in high 32 |
get-current-comm | 16 | 2 | Copy task comm to buffer |
redirect | 23 | 2 | Redirect packet to ifindex |
perf-event-output | 25 | 5 | Send data via perf event |
skb-load-bytes | 26 | 4 | Load bytes from skb |
get-current-task | 35 | 0 | Pointer to current task_struct |
probe-read-str | 45 | 3 | Read kernel string |
get-socket-cookie | 46 | 1 | Socket cookie for tracking |
setsockopt | 49 | 5 | Set a socket option (sockops, cgroup sockopt) |
get-current-cgroup-id | 80 | 0 | Current cgroup v2 ID |
probe-read-user | 112 | 3 | Read user-space memory |
probe-read-kernel | 113 | 3 | Read kernel memory (modern) |
probe-read-user-str | 114 | 3 | Read user-space string |
ringbuf-output | 130 | 4 | Copy data to ring buffer |
ringbuf-reserve | 131 | 3 | Reserve ring buffer space |
ringbuf-submit | 132 | 2 | Submit ring buffer entry |
ringbuf-discard | 133 | 2 | Discard ring buffer entry |
get-current-task-btf | 158 | 0 | Current task_struct (BTF-aware) |
ktime-get-coarse-ns | 160 | 0 | Coarse monotonic clock |
Map helpers (1--3) and tail-call (12) are called through dedicated
Whistler forms rather than by name. The table lists them for
completeness.
Example
(defprog trace-fork (:type :tracepoint
:section "tracepoint/sched/sched_process_fork"
:license "GPL")
(let ((tgid (get-current-pid-tgid))
(pid (cast u32 (>> tgid 32)))
(ts (ktime-get-ns)))
(setf (getmap fork-times pid) ts))
0)