Liking cljdoc? Tell your friends :D

ADR 0003: BPF Map Operations Design

Status

Accepted

Context

BPF maps are kernel data structures that allow:

  • Communication between BPF programs and userspace
  • State sharing between BPF program invocations
  • Inter-CPU data synchronization

We needed to design an API that:

  • Supports all common map types (hash, array, ring buffer, etc.)
  • Is efficient for bulk operations
  • Handles memory correctly (no leaks)
  • Provides Clojure-idiomatic interfaces

Decision

Map Types as Keywords

Map types are specified as keywords:

(maps/create {:type :hash :key-size 4 :value-size 8 :max-entries 1024})
(maps/create {:type :ringbuf :max-entries 4096})

Immutable-Style API

Map operations return results rather than mutating in place:

(maps/lookup map key)     ; Returns value or nil
(maps/update! map key val) ; Returns true/false
(maps/delete! map key)    ; Returns true/false

Batch Operations

For bulk operations, we provide batch functions:

(maps/lookup-batch map keys)   ; Returns seq of values
(maps/update-batch! map pairs) ; Updates multiple key-value pairs

Lazy Iteration

Map iteration is lazy to handle large maps:

(maps/entries-seq map) ; Returns lazy seq of [key value] pairs

Resource Management

Maps are resources that must be closed:

(with-open [m (maps/create {...})]
  (maps/update! m key val))

Or using the macro:

(maps/with-map [m {:type :hash ...}]
  (maps/update! m key val))

Consequences

Positive

  • Type safety: Map types validated at creation time
  • Memory safety: Automatic cleanup with with-open/with-map
  • Performance: Batch operations minimize syscall overhead
  • Clojure-idiomatic: Works with standard Clojure patterns
  • Lazy sequences: Handles large maps without memory issues

Negative

  • Verbosity: More explicit than mutable map APIs
  • Learning curve: Different from Java/C map APIs
  • Overhead: Some abstraction overhead for simple operations

Map Types Supported

TypeDescription
:hashHash table
:arrayArray with integer keys
:percpu-hashPer-CPU hash table
:percpu-arrayPer-CPU array
:lru-hashLRU hash table
:lru-percpu-hashPer-CPU LRU hash
:ringbufRing buffer for events
:stackLIFO stack
:queueFIFO queue
:lpm-trieLongest prefix match trie
:hash-of-mapsHash containing map FDs
:array-of-mapsArray containing map FDs

References

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close