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

defprog

defprog defines a BPF program. Each defprog form compiles into one program section in the output ELF object.

Syntax

(defprog name (:type :xdp :section nil :license "GPL")
  body...)
ParameterDefaultDescription
name--Symbol naming the program
:type:xdpBPF program type
:sectionnilELF section name (defaults to lowercase type)
:license"GPL"License string embedded in the ELF

Program Types

KeywordBPF Program TypeContext Argument
:xdpBPF_PROG_TYPE_XDPxdp_md pointer
:socket-filterBPF_PROG_TYPE_SOCKET_FILTER__sk_buff pointer
:tracepointBPF_PROG_TYPE_TRACEPOINTTracepoint args pointer
:kprobeBPF_PROG_TYPE_KPROBEpt_regs pointer
:cgroup-skbBPF_PROG_TYPE_CGROUP_SKB__sk_buff pointer
:cgroup-sockBPF_PROG_TYPE_CGROUP_SOCKbpf_sock pointer
:cgroup-sock-addrBPF_PROG_TYPE_CGROUP_SOCK_ADDRbpf_sock_addr pointer
:sock-opsBPF_PROG_TYPE_SOCK_OPSbpf_sock_ops pointer

:tc (__sk_buff) and :sk-lookup (bpf_sk_lookup) also have context structs for (ctx field) access.

The context pointer

The kernel calls every BPF program with one argument: a pointer to the program type's context struct, the "Context Argument" column above. There is no parameter list in defprog because Whistler binds that pointer for you, keeping it live for the whole body. You reach it in three ways:

  • (ctx field) reads a context field by name, resolved against the struct for this program's :type. (setf (ctx field) value) writes one where the kernel allows it. See ctx.

  • (ctx-ptr) is the raw pointer itself. Helpers that take the context as an argument want it:

    (skb-load-bytes (ctx-ptr) 0 buf 20)       ; cgroup_skb, tc
    (get-socket-cookie (ctx-ptr))
    (setsockopt (ctx-ptr) SOL_TCP TCP_BPF_IW (stack-addr iw) 4)   ; sock_ops
    
  • Program-type accessors read it for you: (pt-regs-parm1) ... (pt-regs-parm6) and (pt-regs-ret) in kprobes and uprobes (kprobe), and the (tp-FIELD) accessors from deftracepoint in tracepoints.

Which fields (ctx field) knows is decided by :type, so declare the right one: the same field name can sit at different offsets in different context structs.

Return Value

The last expression in the body is implicitly returned as the program's return code. To return early, use (return value) (see return). For XDP programs, the return value is typically an XDP action constant:

(defprog drop-all (:type :xdp)
  XDP_DROP)

Section Names

The ELF section name defaults to the lowercase string form of the program type. For example, a :kprobe program gets the section name "kprobe". :sock-ops is the exception: it defaults to "sockops", the section name loaders expect. To override this -- for instance, to attach to a specific kernel function -- pass :section explicitly:

(defprog trace-exec (:type :kprobe :section "kprobe/sys_execve")
  0)

License

The :license parameter controls the license string embedded in the ELF. It defaults to "GPL". Some BPF helper functions are restricted to GPL-licensed programs. If you set a non-GPL license, calls to GPL-only helpers will be rejected by the kernel verifier at load time.

Multiple Programs

Multiple defprog forms in the same source file compile into a single ELF object, each in its own section. This is useful for tail call dispatch or for bundling related programs:

(defmap dispatch :type :prog-array
  :key-size 4
  :value-size 4
  :max-entries 4)

(defprog handler-a (:type :xdp :section "xdp/handler_a")
  ;; Handle protocol A
  XDP_PASS)

(defprog handler-b (:type :xdp :section "xdp/handler_b")
  ;; Handle protocol B
  XDP_DROP)

(defprog main (:type :xdp)
  ;; Dispatch to sub-programs via tail call
  (tail-call dispatch 0)
  XDP_PASS)

Redefining

At the REPL, defprog and defmap behave like defun. Evaluating one again under the same name replaces the earlier definition where it stands, so its section and map index don't move. The next compile-to-elf sees only the new version. As with defun, a redefinition signals a style warning (redefining NAME), which also catches the same name used twice in one file.

(defprog count (:type :xdp) (incf (getmap hits 0)) (oops) XDP_PASS)
(compile-to-elf "count.bpf.o")   ; error: while compiling (defprog COUNT ...)
(defprog count (:type :xdp) (incf (getmap hits 0)) XDP_PASS)
(compile-to-elf "count.bpf.o")   ; compiles the corrected COUNT

A compile error names the defprog it came from, which matters at the REPL, where every program evaluated so far is compiled again. Renaming a program leaves the old name defined. To start over, call (reset-compilation-state), which drops every map, program and struct. compile-file* and with-bpf-session start from an empty state automatically.

Full Example

A minimal tracepoint program that records events to a ring buffer:

(defstruct event
  (pid u32)
  (ts u64))

(defmap events :type :ringbuf
  :max-entries (* 256 1024))

(defprog trace-sched (:type :tracepoint
                      :section "tracepoint/sched/sched_process_exec")
  (with-ringbuf (e events (sizeof event))
    (setf (event-pid e) (get-current-pid-tgid)
          (event-ts e) (ktime-get-ns)))
  0)