# `MaeBPF.Schema`
[🔗](https://github.com/cocoa-xu/MaeBPF/blob/v0.1.0/maebpf/lib/maebpf/schema.ex#L1)

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, ...}`

# `endian`

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

# `field_info`

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

# `primitive_type`

```elixir
@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`

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

# `t`

```elixir
@type t() :: %MaeBPF.Schema{
  byte_size: non_neg_integer(),
  children: %{optional(atom() | non_neg_integer()) =&gt; t()},
  fields: [field_info()],
  fingerprint: &lt;&lt;_::256&gt;&gt;,
  kind: :primitive | :array | :struct | :tuple | :none,
  spec: spec()
}
```

# `byte_size`

```elixir
@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?`

```elixir
@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`

```elixir
@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!`

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

Compiles a schema specification, raising `ArgumentError` if invalid.

# `decode`

```elixir
@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!`

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

Same as `decode/2`, raising `ArgumentError` on error.

# `diff`

```elixir
@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`

```elixir
@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!`

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

Same as `encode/3`, raising `ArgumentError` on error.

# `field_offset`

```elixir
@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!`

```elixir
@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`

```elixir
@spec format_ipv4({byte(), byte(), byte(), byte()} | &lt;&lt;_::32&gt;&gt; | 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`

```elixir
@spec format_ipv6(tuple() | &lt;&lt;_::128&gt;&gt;) :: String.t()
```

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

# `format_lpm_ipv4`

```elixir
@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`

```elixir
@spec format_mac(tuple() | &lt;&lt;_::48&gt;&gt;) :: 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`

```elixir
@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 `: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`

```elixir
@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`

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

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

# `numeric_type?`

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

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

# `percpu_map_type?`

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

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

---

*Consult [api-reference.md](api-reference.md) for complete listing*
