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

Unified compile-time DSL backing `use MaeBPF`.

One Elixir module compiles to one multi-program, multi-map little-endian
`bpfel` ELF64 object embedded directly into the `.beam` at `mix compile` time
(on both macOS and Linux with zero `clang` dependency).

Example:

    defmodule MyEdge.Gateway do
      use MaeBPF

      counter   :blocked_dns
      blocklist :bad_ips, type: :lpm_ipv4, max_entries: 10_000
      stream    :alerts,  ringbuf_kb: 256

      xdp :ingress do
        plug MaeBPF.Plugs.DropInvalid
        drop ipv4(src_in: :bad_ips), count: :blocked_dns
        drop udp(dport: 53),         count: :blocked_dns, emit: :alerts
        pass tcp(dport: 443)
        default :pass
      end

      tcx :egress, direction: :egress do
        drop tcp(dport: 25)
        default :pass
      end
    end

# `allowlist`
*macro* 

Declares a module-level IPv4 or domain allowlist map
(`type: :lpm_domain | :lpm_ipv4 | :lru_hash | :hash`).

    allowlist :allowed_domains, type: :lpm_domain, max_entries: 10_000

# `blocklist`
*macro* 

Declares a module-level IPv4 or domain blocklist map
(`type: :lpm_ipv4 | :lpm_domain | :lru_hash | :hash`).

# `bpf_map`
*macro* 

Declares a custom BPF map using explicit `%MaeBPF.IR.MapDef{}` fields.

# `c_preamble`
*macro* 

Declares module-level C code (structs, `static __always_inline` helpers, or
`extern` declarations) emitted before hook functions in `use MaeBPFC` modules.

    c_preamble ~BPFC"""
    static __always_inline int is_watched(__u16 port) {
        return port == target_port;
    }
    """

# `cgroup`
*macro* 

Declares a Linux cgroup v2 `cgroup_skb` hook block (`direction: :ingress | :egress`).

Compiles to ELF section `"cgroup_skb/ingress"` or `"cgroup_skb/egress"` with
L3 IPv4 packet offsets (no 14-byte Ethernet header) and `cgroup_skb` verdicts
(`1` = `:pass`, `0` = `:drop`).

# `cgroup`
*macro* 

# `counter`
*macro* 

Declares a module-level counter map (`:percpu_array` by default, `key: u32`,
`value: u64`).

# `default`
*macro* 

Sets the default fallback action (`:pass`, `:drop`, `:tx`, `:next`, etc.) for
packets that reach the end of the current hook pipeline.

# `drop`
*macro* 

Declarative drop rule sugar inside a hook block:
  * `drop :invalid` -> `MaeBPF.Plugs.DropInvalid`
  * `drop udp(dport: 53), count: :c, emit: :s` -> `MaeBPF.Plugs.DropUDP`
  * `drop tcp(dport: 25), count: :c, emit: :s` -> `MaeBPF.Plugs.DropTCP`
  * `drop ipv4(src_in: :bad_ips), count: :c, emit: :s` -> `MaeBPF.Plugs.Blocklist`

# `global`
*macro* 

Declares a typed eBPF global variable in `.rodata` (`readonly: true`, default)
or `.data`/`.bss` (`readonly: false`) for `use MaeBPFC` modules.

    global :target_comm, {:string, 16}, default: "curl"
    global :target_port, :u32, default: 443
    global :watch_ports, {:array, :u16, 4}, default: [80, 443, 8080, 8443]
    global :magic_hdr, {:bytes, 4}, default: <<0xDE, 0xAD, 0xBE, 0xEF>>
    global :debug_mode, :bool, default: false, readonly: false

Supported types: `:bool`, `:u8`, `:u16`, `:u32`, `:u64`, `:i8`, `:i16`,
`:i32`, `:i64`, `:be16`, `:be32`, `:be64`, `:ipv4`, `:ipv6`, `:mac`,
`{:string, n}`, `{:bytes, n}`, and `{:array, elem_type, n}`.

# `pass`
*macro* 

Declarative pass rule sugar inside a hook block:
  * `pass udp(dport: 443), count: :c, emit: :s` -> `MaeBPF.Plugs.PassUDP`
  * `pass tcp(dport: 443), count: :c, emit: :s` -> `MaeBPF.Plugs.PassTCP`
  * `pass ipv4(src_in: :allowlist), count: :c, emit: :s` -> `MaeBPF.Plugs.Blocklist` (`action: :pass`)

# `plug`
*macro* 

Appends a `MaeBPF.Plug` module to the current hook pipeline.

# `scope`
*macro* 

Scopes a block of packet rules to matching L4 ports and/or protocol inside an
`xdp`, `tcx`, or `cgroup` hook block.

Packets that do not match the scope guard skip every rule inside the block
untouched and continue to subsequent rules or `default`.

Options:
  * `:ports` - integer port or list of ports in `1..65535` (also accepts
    `:dport` or `:sport` shorthands)
  * `:proto` - `:any` (default, matching TCP or UDP), `:tcp`, or `:udp`
  * `:match` - `:dport` (default), `:sport`, or `:either`

# `stream`
*macro* 

Declares a module-level RingBuf event stream (`ringbuf_kb:` default `256`).
IR normalization automatically registers `"__maebpf_rb_drops_<name>"`.

# `tcx`
*macro* 

Declares a `tcx` program hook block (`direction: :ingress | :egress`).

# `tcx`
*macro* 

# `trace`
*macro* 

Declares a kernel `tracepoint` hook block.

Target may be a `"category/name"` string or keyword options
`[tracepoint: "category/name"]` / `[category: "syscalls", name: "sys_enter_openat"]`.
Pass `filter_pids: true` or `pids: [os_pid, ...]` to compile in an entry PID
filter backed by the hidden `"__maebpf_pid_filter"` `HASH` map.

# `trace`
*macro* 

# `xdp`
*macro* 

Declares an `xdp` program hook block sharing all module-level maps.

# `xdp`
*macro* 

---

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