Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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

HelperIDArgsDescription
map-lookup-elem1--(use map-lookup instead)
map-update-elem2--(use map-update instead)
map-delete-elem3--(use map-delete instead)
probe-read43Read kernel memory (legacy)
ktime-get-ns50Monotonic clock, nanoseconds
trace-printk62--5Debug printf to trace_pipe (format, its size, up to 3 values)
get-prandom-u3270Pseudo-random u32
get-smp-processor-id80Current CPU number
tail-call12--(use the tail-call form instead)
get-current-pid-tgid140PID in low 32, TGID in high 32
get-current-uid-gid150UID in low 32, GID in high 32
get-current-comm162Copy task comm to buffer
redirect232Redirect packet to ifindex
perf-event-output255Send data via perf event
skb-load-bytes264Load bytes from skb
get-current-task350Pointer to current task_struct
probe-read-str453Read kernel string
get-socket-cookie461Socket cookie for tracking
setsockopt495Set a socket option (sockops, cgroup sockopt)
get-current-cgroup-id800Current cgroup v2 ID
probe-read-user1123Read user-space memory
probe-read-kernel1133Read kernel memory (modern)
probe-read-user-str1143Read user-space string
ringbuf-output1304Copy data to ring buffer
ringbuf-reserve1313Reserve ring buffer space
ringbuf-submit1322Submit ring buffer entry
ringbuf-discard1332Discard ring buffer entry
get-current-task-btf1580Current task_struct (BTF-aware)
ktime-get-coarse-ns1600Coarse 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)