MaeBPF.Schema (maebpf v0.1.0)

Copy Markdown View Source

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, max n - 1 bytes)
  • 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.

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.

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

endian()

@type endian() :: :little | :big | :raw

field_info()

@type field_info() :: %{
  name: atom() | non_neg_integer(),
  type: spec(),
  offset: non_neg_integer(),
  size: non_neg_integer(),
  endian: endian()
}

primitive_type()

@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()}

spec()

@type spec() ::
  :none
  | primitive_type()
  | {:array, spec(), pos_integer()}
  | [{atom(), spec()}]
  | tuple()

t()

@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

byte_size(spec)

@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.

compatible_for_reuse?(old_spec, new_spec)

@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.

compile(schema)

@spec compile(t() | spec()) :: {:ok, t()} | {:error, term()}

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.

compile!(spec)

@spec compile!(t() | spec()) :: t()

Compiles a schema specification, raising ArgumentError if invalid.

decode(schema_or_spec, binary)

@spec decode(t() | spec(), binary()) :: {:ok, term()} | {:error, term()}

Decodes an exact-size packed binary into its Elixir representation according to schema_or_spec.

decode!(schema_or_spec, binary)

@spec decode!(t() | spec(), binary()) :: term()

Same as decode/2, raising ArgumentError on error.

diff(old_spec, new_spec)

@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).

encode(schema_or_spec, value, opts \\ [])

@spec encode(t() | spec(), term(), keyword()) :: {:ok, binary()} | {:error, term()}

Encodes an Elixir value into an exact-size packed binary according to schema_or_spec.

Options:

  • :defaults - when true, omitted numeric fields in struct schemas default to 0 (default false)
  • :pad - when true, {:bytes, n} inputs shorter than n bytes are right-padded with zero bytes (default false)

encode!(schema_or_spec, value, opts \\ [])

@spec encode!(t() | spec(), term(), keyword()) :: binary()

Same as encode/3, raising ArgumentError on error.

field_offset(schema_or_spec, field_name)

@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.

field_offset!(schema_or_spec, field_name)

@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.

format_ipv4(n)

@spec format_ipv4({byte(), byte(), byte(), byte()} | <<_::32>> | non_neg_integer()) ::
  String.t()

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").

format_ipv6(addr)

@spec format_ipv6(tuple() | <<_::128>>) :: String.t()

Formats an IPv6 8-tuple or 16-byte binary as a canonical RFC 5952 string.

format_lpm_ipv4(other)

@spec format_lpm_ipv4({{byte(), byte(), byte(), byte()}, 0..32}) :: String.t()

Formats a decoded :lpm_ipv4 tuple {{a, b, c, d}, prefixlen} as a CIDR string (for example "10.0.0.0/8").

format_mac(other)

@spec format_mac(tuple() | <<_::48>>) :: String.t()

Formats a 6-tuple or 6-byte binary MAC address as lowercase colon-separated hex (for example "aa:bb:cc:dd:ee:ff").

map_spec(name, type, opts \\ [])

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 :none for :ringbuf; defaults to :u32 for :array, :percpu_array, :prog_array, :perf_event_array)
  • :value - value schema spec or %MaeBPF.Schema{} (omitted or :none for :ringbuf; defaults to :u32 for :prog_array and :perf_event_array)
  • :max_entries - positive integer entry count (required; for :ringbuf, must be a power-of-two byte size)
  • :flags - non-negative integer map_flags bitmask (default 0)

map_type_id(type)

@spec map_type_id(atom()) :: {:ok, pos_integer()} | :error

Maps a symbolic BPF map type atom to its <linux/bpf.h> integer type ID.

numeric_bounds(type)

@spec numeric_bounds(atom()) :: {:ok, {integer(), integer()}} | :error

Returns {min, max} inclusive integer bounds for a numeric primitive type.

numeric_type?(type)

@spec numeric_type?(term()) :: boolean()

Returns true if type is one of the numeric integer primitive types.

percpu_map_type?(type)

@spec percpu_map_type?(atom()) :: boolean()

Returns true if map_type is a per-CPU kernel BPF map type.