Single-source-of-truth schema compilation, packed binary codecs, endianness conversion, and map geometry compatibility checks for eBPF maps, events, and global variables.
schema = MaeBPF.Schema.compile!(enabled: :bool, comm: {:string, 16}, ports: {:array, :u16, 4})
bin = MaeBPF.Schema.encode!(schema, %{enabled: true, comm: "nginx", ports: [80, 443]})
{:ok, %{enabled: true, comm: "nginx", ports: [80, 443, 0, 0]}} = MaeBPF.Schema.decode(schema, bin)Supported Types
- Boolean:
:bool(1B:true<-><<1>>,false<-><<0>>) - Little-endian integers:
:u8,:u16,:u32,:u64,:i8,:i16,:i32,:i64 - Network big-endian integers:
:be16,:be32,:be64 - Network primitives:
:ipv4(4B),:ipv6(16B),:mac(6B),:lpm_ipv4(8B) - Fixed NUL-terminated C strings:
{:string, n}(n >= 1, maxn - 1bytes) - Fixed byte arrays:
{:bytes, n} - Fixed-length typed arrays:
{:array, elem_type, n}(n >= 1) - Packed composite structs:
[field_a: type_a, field_b: type_b, ...] - Packed tuple keys:
{type_0, type_1, ...}
Summary
Functions
Returns the exact packed byte size (with zero padding bytes) of a compiled schema or raw schema specification.
Checks the exact Linux kernel immutability contract for bpf_map__reuse_fd:
type, key_size, value_size, max_entries, and flags must all match.
Compiles a schema specification into a %MaeBPF.Schema{} struct.
Compiles a schema specification, raising ArgumentError if invalid.
Decodes an exact-size packed binary into its Elixir representation according
to schema_or_spec.
Same as decode/2, raising ArgumentError on error.
Computes a structured diff between two %MaeBPF.Schema.MapSpec{} definitions.
Encodes an Elixir value into an exact-size packed binary according to
schema_or_spec.
Same as encode/3, raising ArgumentError on error.
Returns {:ok, field_info} containing exact byte :offset, :size,
:type, :endian, and :name for compiler IR code generation.
Same as field_offset/2, raising ArgumentError if the field does not exist.
Formats an IPv4 4-tuple, 4-byte binary, or 32-bit unsigned integer as a
canonical dotted-quad string (for example "1.2.3.4").
Formats an IPv6 8-tuple or 16-byte binary as a canonical RFC 5952 string.
Formats a decoded :lpm_ipv4 tuple {{a, b, c, d}, prefixlen} as a CIDR
string (for example "10.0.0.0/8").
Formats a 6-tuple or 6-byte binary MAC address as lowercase colon-separated
hex (for example "aa:bb:cc:dd:ee:ff").
Builds a validated %MaeBPF.Schema.MapSpec{} with compiled key/value schemas
and a deterministic SHA-256 fingerprint.
Maps a symbolic BPF map type atom to its <linux/bpf.h> integer type ID.
Returns {min, max} inclusive integer bounds for a numeric primitive type.
Returns true if type is one of the numeric integer primitive types.
Returns true if map_type is a per-CPU kernel BPF map type.
Types
@type endian() :: :little | :big | :raw
@type field_info() :: %{ name: atom() | non_neg_integer(), type: spec(), offset: non_neg_integer(), size: non_neg_integer(), endian: endian() }
@type primitive_type() :: :bool | :u8 | :u16 | :u32 | :u64 | :i8 | :i16 | :i32 | :i64 | :be16 | :be32 | :be64 | :ipv4 | :ipv6 | :mac | :lpm_ipv4 | {:bytes, pos_integer()} | {:string, pos_integer()}
@type spec() :: :none | primitive_type() | {:array, spec(), pos_integer()} | [{atom(), spec()}] | tuple()
@type t() :: %MaeBPF.Schema{ byte_size: non_neg_integer(), children: %{optional(atom() | non_neg_integer()) => t()}, fields: [field_info()], fingerprint: <<_::256>>, kind: :primitive | :array | :struct | :tuple | :none, spec: spec() }
Functions
@spec byte_size(t() | spec()) :: non_neg_integer()
Returns the exact packed byte size (with zero padding bytes) of a compiled schema or raw schema specification.
@spec compatible_for_reuse?(MaeBPF.Schema.MapSpec.t(), MaeBPF.Schema.MapSpec.t()) :: boolean()
Checks the exact Linux kernel immutability contract for bpf_map__reuse_fd:
type, key_size, value_size, max_entries, and flags must all match.
Compiles a schema specification into a %MaeBPF.Schema{} struct.
Returns {:ok, schema} on success or {:error, reason} if the spec is
malformed. Passing an already-compiled %MaeBPF.Schema{} returns it as-is.
Compiles a schema specification, raising ArgumentError if invalid.
Decodes an exact-size packed binary into its Elixir representation according
to schema_or_spec.
Same as decode/2, raising ArgumentError on error.
@spec diff(MaeBPF.Schema.MapSpec.t(), MaeBPF.Schema.MapSpec.t()) :: :identical | {:changed, map()}
Computes a structured diff between two %MaeBPF.Schema.MapSpec{} definitions.
Returns :identical when map type, capacity, flags, and both key and value
schema fingerprints match; otherwise returns {:changed, diff_map} detailing
exact differences (:type, :max_entries, :flags, :key_size,
:value_size, :key, :value).
Encodes an Elixir value into an exact-size packed binary according to
schema_or_spec.
Options:
:defaults- whentrue, omitted numeric fields in struct schemas default to0(defaultfalse):pad- whentrue,{:bytes, n}inputs shorter thannbytes are right-padded with zero bytes (defaultfalse)
Same as encode/3, raising ArgumentError on error.
@spec field_offset( t() | spec(), atom() | non_neg_integer() | [atom() | non_neg_integer()] ) :: {:ok, field_info()} | {:error, {:unknown_field, term()}}
Returns {:ok, field_info} containing exact byte :offset, :size,
:type, :endian, and :name for compiler IR code generation.
field_name may be a struct field atom, a tuple element index (0-based), or
a list path [outer_field, inner_field, ...] into nested composite schemas.
@spec field_offset!( t() | spec(), atom() | non_neg_integer() | [atom() | non_neg_integer()] ) :: field_info()
Same as field_offset/2, raising ArgumentError if the field does not exist.
Formats an IPv4 4-tuple, 4-byte binary, or 32-bit unsigned integer as a
canonical dotted-quad string (for example "1.2.3.4").
Formats an IPv6 8-tuple or 16-byte binary as a canonical RFC 5952 string.
Formats a decoded :lpm_ipv4 tuple {{a, b, c, d}, prefixlen} as a CIDR
string (for example "10.0.0.0/8").
Formats a 6-tuple or 6-byte binary MAC address as lowercase colon-separated
hex (for example "aa:bb:cc:dd:ee:ff").
@spec map_spec(String.t() | atom(), MaeBPF.Schema.MapSpec.map_type(), keyword()) :: MaeBPF.Schema.MapSpec.t()
Builds a validated %MaeBPF.Schema.MapSpec{} with compiled key/value schemas
and a deterministic SHA-256 fingerprint.
Options:
:key- key schema spec or%MaeBPF.Schema{}(omitted or:nonefor:ringbuf; defaults to:u32for:array,:percpu_array,:prog_array,:perf_event_array):value- value schema spec or%MaeBPF.Schema{}(omitted or:nonefor:ringbuf; defaults to:u32for:prog_arrayand:perf_event_array):max_entries- positive integer entry count (required; for:ringbuf, must be a power-of-two byte size):flags- non-negative integermap_flagsbitmask (default0)
@spec map_type_id(atom()) :: {:ok, pos_integer()} | :error
Maps a symbolic BPF map type atom to its <linux/bpf.h> integer type ID.
Returns {min, max} inclusive integer bounds for a numeric primitive type.
Returns true if type is one of the numeric integer primitive types.
Returns true if map_type is a per-CPU kernel BPF map type.