DSL helpers for BPF STRUCT_OPS programs.
STRUCT_OPS allows BPF programs to implement kernel function pointers defined in structures. The primary use case is implementing TCP congestion control algorithms entirely in BPF.
Common struct_ops targets:
TCP Congestion Control Callbacks:
Example: ;; Simple ssthresh implementation (def ssthresh-prog (dsl/assemble (vec (concat (struct-ops/struct-ops-prologue :r6) ;; Return tp->snd_cwnd (just pass through) [(struct-ops/tcp-sock-load-cwnd :r6 :r0)] (struct-ops/struct-ops-return)))))
DSL helpers for BPF STRUCT_OPS programs.
STRUCT_OPS allows BPF programs to implement kernel function pointers
defined in structures. The primary use case is implementing TCP
congestion control algorithms entirely in BPF.
Common struct_ops targets:
- tcp_congestion_ops: TCP congestion control algorithms
- bpf_struct_ops: Generic struct_ops infrastructure
TCP Congestion Control Callbacks:
- ssthresh: Calculate slow start threshold
- cong_avoid: Congestion avoidance algorithm
- set_state: Handle state changes
- cwnd_event: Handle congestion window events
- pkts_acked: Handle ACK events
- undo_cwnd: Undo congestion window changes
- cong_control: Main congestion control logic
Example:
;; Simple ssthresh implementation
(def ssthresh-prog
(dsl/assemble
(vec (concat
(struct-ops/struct-ops-prologue :r6)
;; Return tp->snd_cwnd (just pass through)
[(struct-ops/tcp-sock-load-cwnd :r6 :r0)]
(struct-ops/struct-ops-return)))))(aimd-ssthresh sk-reg tmp-reg)Generate AIMD (Additive Increase Multiplicative Decrease) ssthresh.
This is the classic TCP Reno behavior: ssthresh = max(cwnd/2, 2)
Parameters:
Returns vector of instructions.
Generate AIMD (Additive Increase Multiplicative Decrease) ssthresh. This is the classic TCP Reno behavior: ssthresh = max(cwnd/2, 2) Parameters: - sk-reg: Register containing sock pointer - tmp-reg: Temporary register for calculations Returns vector of instructions.
(cong-avoid-prologue sk-reg ack-reg acked-reg)Prologue for cong_avoid callback.
Signature: void (*cong_avoid)(struct sock *sk, u32 ack, u32 acked)
Parameters:
Returns vector of instructions.
Prologue for cong_avoid callback. Signature: void (*cong_avoid)(struct sock *sk, u32 ack, u32 acked) Parameters: - sk-reg: Register to save sock pointer - ack-reg: Register to save ack parameter - acked-reg: Register to save acked parameter Returns vector of instructions.
(cong-control-prologue sk-reg rs-reg)Prologue for cong_control callback.
Signature: void (*cong_control)(struct sock *sk, const struct rate_sample *rs)
Parameters:
Returns vector of instructions.
Prologue for cong_control callback. Signature: void (*cong_control)(struct sock *sk, const struct rate_sample *rs) Parameters: - sk-reg: Register to save sock pointer - rs-reg: Register to save rate_sample pointer Returns vector of instructions.
(cwnd-event-prologue sk-reg event-reg)Prologue for cwnd_event callback.
Signature: void (*cwnd_event)(struct sock *sk, enum tcp_ca_event ev)
Parameters:
Returns vector of instructions.
Prologue for cwnd_event callback. Signature: void (*cwnd_event)(struct sock *sk, enum tcp_ca_event ev) Parameters: - sk-reg: Register to save sock pointer - event-reg: Register to save event parameter Returns vector of instructions.
(get-callback-info callback)Get information about a TCP congestion control callback.
Parameters:
Returns map with :args, :return, :required.
Get information about a TCP congestion control callback. Parameters: - callback: Callback keyword (e.g., :ssthresh) Returns map with :args, :return, :required.
(increment-cwnd sk-reg tmp-reg)Increment cwnd by 1 (slow start).
Parameters:
Returns vector of instructions.
Increment cwnd by 1 (slow start). Parameters: - sk-reg: Register containing sock pointer - tmp-reg: Temporary register Returns vector of instructions.
(init-prologue sk-reg)Prologue for init callback.
Signature: void (*init)(struct sock *sk)
Parameters:
Returns vector of instructions.
Prologue for init callback. Signature: void (*init)(struct sock *sk) Parameters: - sk-reg: Register to save sock pointer Returns vector of instructions.
(make-struct-ops-info prog-name struct-name callback instructions)Create struct_ops program metadata.
Parameters:
Returns map with program metadata.
Create struct_ops program metadata. Parameters: - prog-name: Program name - struct-name: Target struct name - callback: Callback name - instructions: Vector of instructions Returns map with program metadata.
(make-tcp-cong-ops-info prog-name callback instructions)Create TCP congestion control program metadata.
Parameters:
Returns map with program metadata.
Create TCP congestion control program metadata. Parameters: - prog-name: Program name - callback: Callback name - instructions: Vector of instructions Returns map with program metadata.
(minimal-cong-avoid-program)Generate minimal cong_avoid program (void return).
Generate minimal cong_avoid program (void return).
(minimal-init-program)Generate minimal init program (void return).
Generate minimal init program (void return).
(minimal-release-program)Generate minimal release program (void return).
Generate minimal release program (void return).
(minimal-ssthresh-program)Generate minimal ssthresh program (returns cwnd/2, min 2).
This implements basic AIMD behavior.
Generate minimal ssthresh program (returns cwnd/2, min 2). This implements basic AIMD behavior.
(minimal-undo-cwnd-program)Generate minimal undo_cwnd program (returns current cwnd).
Generate minimal undo_cwnd program (returns current cwnd).
(passthrough-ssthresh-program)Generate ssthresh program that returns current ssthresh.
Generate ssthresh program that returns current ssthresh.
(pkts-acked-prologue sk-reg sample-reg)Prologue for pkts_acked callback.
Signature: void (*pkts_acked)(struct sock *sk, const struct ack_sample *sample)
Parameters:
Returns vector of instructions.
Prologue for pkts_acked callback. Signature: void (*pkts_acked)(struct sock *sk, const struct ack_sample *sample) Parameters: - sk-reg: Register to save sock pointer - sample-reg: Register to save sample pointer Returns vector of instructions.
(release-prologue sk-reg)Prologue for release callback.
Signature: void (*release)(struct sock *sk)
Parameters:
Returns vector of instructions.
Prologue for release callback. Signature: void (*release)(struct sock *sk) Parameters: - sk-reg: Register to save sock pointer Returns vector of instructions.
(set-state-prologue sk-reg state-reg)Prologue for set_state callback.
Signature: void (*set_state)(struct sock *sk, u8 new_state)
Parameters:
Returns vector of instructions.
Prologue for set_state callback. Signature: void (*set_state)(struct sock *sk, u8 new_state) Parameters: - sk-reg: Register to save sock pointer - state-reg: Register to save new_state parameter Returns vector of instructions.
(slow-start-check sk-reg cwnd-reg ssthresh-reg slow-start-skip)Check if in slow start (cwnd < ssthresh).
Parameters:
Returns vector of instructions.
Check if in slow start (cwnd < ssthresh). Parameters: - sk-reg: Register containing sock pointer - cwnd-reg: Register to load cwnd into - ssthresh-reg: Register to load ssthresh into - slow-start-skip: Instructions to skip if NOT in slow start Returns vector of instructions.
(ssthresh-prologue sk-reg)Prologue for ssthresh callback.
Signature: u32 (*ssthresh)(struct sock *sk)
Parameters:
Returns vector of instructions.
Prologue for ssthresh callback. Signature: u32 (*ssthresh)(struct sock *sk) Parameters: - sk-reg: Register to save sock pointer Returns vector of instructions.
(struct-ops-prologue sk-reg)Generate standard struct_ops program prologue.
For TCP congestion control, the first argument (r1) is struct sock * (which is at the base of tcp_sock).
Parameters:
Returns vector of instructions.
Generate standard struct_ops program prologue. For TCP congestion control, the first argument (r1) is struct sock * (which is at the base of tcp_sock). Parameters: - sk-reg: Register to save sock pointer (e.g., :r6) Returns vector of instructions.
(struct-ops-prologue-2arg sk-reg arg2-reg)Generate prologue for callbacks with 2 arguments.
Parameters:
Returns vector of instructions.
Generate prologue for callbacks with 2 arguments. Parameters: - sk-reg: Register to save sock pointer - arg2-reg: Register to save second argument Returns vector of instructions.
(struct-ops-prologue-3arg sk-reg arg2-reg arg3-reg)Generate prologue for callbacks with 3 arguments.
Parameters:
Returns vector of instructions.
Generate prologue for callbacks with 3 arguments. Parameters: - sk-reg: Register to save sock pointer - arg2-reg: Register to save second argument - arg3-reg: Register to save third argument Returns vector of instructions.
(struct-ops-return)Generate return instructions for struct_ops callback.
Returns r0 and exits.
Generate return instructions for struct_ops callback. Returns r0 and exits.
(struct-ops-return-imm value)Generate instructions to return immediate value.
Parameters:
Returns vector of instructions.
Generate instructions to return immediate value. Parameters: - value: Immediate value to return Returns vector of instructions.
(struct-ops-return-reg reg)Generate instructions to return register value.
Parameters:
Returns vector of instructions.
Generate instructions to return register value. Parameters: - reg: Register containing return value Returns vector of instructions.
(struct-ops-return-void)Generate instructions for void return (returns 0).
Returns vector of instructions.
Generate instructions for void return (returns 0). Returns vector of instructions.
(struct-ops-section-name struct-name callback)Generate ELF section name for struct_ops program.
Parameters:
Returns section name string.
Generate ELF section name for struct_ops program. Parameters: - struct-name: Target struct name (e.g., "tcp_congestion_ops") - callback: Callback name (e.g., "ssthresh") Returns section name string.
Flags for in_ack_event callback
Flags for in_ack_event callback
TCP Congestion Avoidance states (from tcp.h)
TCP Congestion Avoidance states (from tcp.h)
(tcp-cong-ops-section-name callback)Generate ELF section name for TCP congestion control callback.
Parameters:
Returns section name string.
Generate ELF section name for TCP congestion control callback. Parameters: - callback: Callback name (e.g., "ssthresh") Returns section name string.
TCP congestion control operation callbacks.
TCP congestion control operation callbacks.
(tcp-sock-load-ca-state sk-reg dst-reg)Load icsk_ca_state from tcp_sock.
Load icsk_ca_state from tcp_sock.
(tcp-sock-load-cwnd sk-reg dst-reg)Load snd_cwnd from tcp_sock.
Load snd_cwnd from tcp_sock.
(tcp-sock-load-packets-out sk-reg dst-reg)Load packets_out from tcp_sock.
Load packets_out from tcp_sock.
(tcp-sock-load-srtt sk-reg dst-reg)Load srtt_us from tcp_sock (smoothed RTT in usec << 3).
Load srtt_us from tcp_sock (smoothed RTT in usec << 3).
(tcp-sock-load-ssthresh sk-reg dst-reg)Load snd_ssthresh from tcp_sock.
Load snd_ssthresh from tcp_sock.
(tcp-sock-load-u16 sk-reg dst-reg field)Load a 16-bit field from tcp_sock.
Parameters:
Returns ldx instruction.
Load a 16-bit field from tcp_sock. Parameters: - sk-reg: Register containing sock pointer - dst-reg: Destination register - field: Field keyword Returns ldx instruction.
(tcp-sock-load-u32 sk-reg dst-reg field)Load a 32-bit field from tcp_sock.
Parameters:
Returns ldx instruction.
Load a 32-bit field from tcp_sock. Parameters: - sk-reg: Register containing sock pointer - dst-reg: Destination register - field: Field keyword (e.g., :snd-cwnd) Returns ldx instruction.
(tcp-sock-load-u8 sk-reg dst-reg field)Load an 8-bit field from tcp_sock.
Parameters:
Returns ldx instruction.
Load an 8-bit field from tcp_sock. Parameters: - sk-reg: Register containing sock pointer - dst-reg: Destination register - field: Field keyword Returns ldx instruction.
(tcp-sock-offset field)Get offset for a tcp_sock field.
Parameters:
Returns offset or throws on invalid field.
Get offset for a tcp_sock field. Parameters: - field: Field keyword Returns offset or throws on invalid field.
Common offsets in struct tcp_sock. Note: These are approximate and kernel-version dependent. Use BTF for production code.
Common offsets in struct tcp_sock. Note: These are approximate and kernel-version dependent. Use BTF for production code.
(tcp-sock-store-cwnd sk-reg value-reg)Store value to snd_cwnd in tcp_sock.
Store value to snd_cwnd in tcp_sock.
(tcp-sock-store-ssthresh sk-reg value-reg)Store value to snd_ssthresh in tcp_sock.
Store value to snd_ssthresh in tcp_sock.
(tcp-sock-store-u32 sk-reg field value-reg)Store a 32-bit value to tcp_sock field.
Parameters:
Returns stx instruction.
Store a 32-bit value to tcp_sock field. Parameters: - sk-reg: Register containing sock pointer - field: Field keyword - value-reg: Register containing value to store Returns stx instruction.
(undo-cwnd-prologue sk-reg)Prologue for undo_cwnd callback.
Signature: u32 (*undo_cwnd)(struct sock *sk)
Parameters:
Returns vector of instructions.
Prologue for undo_cwnd callback. Signature: u32 (*undo_cwnd)(struct sock *sk) Parameters: - sk-reg: Register to save sock pointer Returns vector of instructions.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |