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

Attachment, atomic program hot-swap, iterator reads, introspection, and
teardown of kernel `bpf_link` resources.

    {:ok, link} = MaeBPF.Link.attach_auto(obj, "on_tcp_connect")
    {:ok, link} = MaeBPF.Link.attach_uprobe(obj, "on_ssl_write", libssl, "SSL_write")
    {:ok, data} = MaeBPF.Link.read_iter(iter_link)

# `attach`

Attaches a loaded program according to a `MaeBPF.Target` specification.

# `attach_auto`

Auto-attaches a loaded program via `bpf_program__attach` using its ELF
section declaration (`kprobe/`, `kretprobe/`, `uprobe/`, `fentry/`, `fexit/`,
`fmod_ret/`, `lsm/`, `tracepoint/`, `raw_tp/`, `tp_btf/`, `iter/`, `freplace/`).

    {:ok, link} = MaeBPF.Link.attach_auto(obj, "on_tcp_connect")

# `attach_cgroup`

Attaches a cgroup eBPF program (`cgroup_skb/ingress`, `cgroup_skb/egress`,
etc.) to a Linux cgroup v2 directory path via `bpf_program__attach_cgroup`.

# `attach_kprobe`

Attaches a kprobe or kretprobe program to kernel symbol `func_name`.

    {:ok, link} = MaeBPF.Link.attach_kprobe(obj, "kp_open", "do_sys_openat2")
    {:ok, link} = MaeBPF.Link.attach_kprobe(obj, "krp_open", "do_sys_openat2", retprobe: true)

Options:
  * `:retprobe` - boolean (default `false`)
  * `:offset` - non-negative integer byte offset into `func_name` (default `0`)
  * `:bpf_cookie` - non-negative 64-bit integer cookie (default `0`)

# `attach_raw_tracepoint`

Attaches a raw tracepoint program to `tp_name` via
`bpf_program__attach_raw_tracepoint`.

    {:ok, link} = MaeBPF.Link.attach_raw_tracepoint(obj, "rtp_sched", "sched_switch")

# `attach_tcx`

Attaches a TCX program (`:ingress` or `:egress`) to a network interface given
by name or ifindex.

# `attach_tcx`

Attaches a TCX program (`:ingress` or `:egress`) with explicit chain ordering
options.

Options:
  * `:position` - `:last` (default), `:first`, `{:before, link_or_fd}`, or
    `{:after, link_or_fd}`

# `attach_tracepoint`

Attaches a tracepoint program to `<category>/<name>`.

# `attach_uprobe`

Attaches a uprobe or uretprobe program to `binary_path` at ELF symbol name
or byte offset `sym_or_off`.

    {:ok, link} = MaeBPF.Link.attach_uprobe(obj, "up_ssl", "/lib/libssl.so.3", "SSL_write")
    {:ok, link} = MaeBPF.Link.attach_uprobe(obj, "up_fn", "/usr/bin/app", 0x1234, pid: 4321)

Options:
  * `:pid` - `-1` (all processes, default), `:self` (`0`), or OS PID `> 0`
  * `:retprobe` - boolean (default `false`)
  * `:func_offset` - additional byte offset when `sym_or_off` is a symbol name (default `0`)
  * `:ref_ctr_offset` - optional USDT semaphore offset (default `0`)
  * `:bpf_cookie` - non-negative 64-bit integer cookie (default `0`)

# `attach_xdp`

Attaches an XDP program to a network interface given by name or ifindex.

# `attach_xdp`

Attaches an XDP program with explicit attachment mode and exclusivity guard.

Options:
  * `:mode` - `:auto` (default: try `:native` `XDP_FLAGS_DRV_MODE`, falling
    back to `:generic` `XDP_FLAGS_SKB_MODE` on `:eopnotsupp` / `:enotsup` /
    `:einval`), `:native`, `:generic`, or `:hw`
  * `:exclusive` - `true` (default) returns `{:error, {:xdp_busy, message}}`
    when another XDP program is already attached on `ifindex` instead of
    silently clobbering it; `false` returns the underlying kernel error

# `attach_xdp_with_runner`

Executes the XDP mode fallback and exclusivity policy against a supplied
`attach_fn.(mode)` callback.

# `destroy`

Destroys a `bpf_link` resource handle via `bpf_link__destroy`.

For unpinned links this detaches the program from the kernel hook. For links
pinned in `/sys/fs/bpf/`, only the userspace file descriptor is closed while
the pinned kernel link remains active until unpinned.

# `if_nametoindex`

Resolves a Linux network interface name to its positive integer ifindex.

# `info`

Returns kernel metadata (`id`, `type`, `type_id`, `prog_id`) for a link.

# `info_by_id`

Returns kernel metadata for a link by its kernel link ID.

# `normalize_tcx_opts`

Validates and normalizes `attach_tcx/5` options into `{:ok, {pos_atom, rel}}`.

# `open_pinned`

Opens an existing pinned `bpf_link` from `path` via `bpf_link__open` and
returns an RAII link resource handle.

# `pin`

Pins a `bpf_link` to `path` under `/sys/fs/bpf/` via `bpf_link__pin`.

# `pin_path`

Returns `{:ok, pin_path | nil}` for a link handle.

# `read_iter`

Creates a BPF iterator stream (`bpf_iter_create`) on an attached `iter` link
and reads up to `:max_bytes` (default `4 MiB`, max `64 MiB`).

    {:ok, output_bin} = MaeBPF.Link.read_iter(iter_link, max_bytes: 1_048_576)

# `set_attach_target`

Configures a program's attach target prior to `bpf_object__load` via
`bpf_program__set_attach_target`.

Pass `target_prog_fd = 0` for kernel BTF hooks (`fentry`, `fexit`,
`fmod_ret`, `lsm`) or a positive target program FD for `freplace`.

    :ok = MaeBPF.Link.set_attach_target(obj, "my_fentry", 0, "tcp_v4_connect")

# `supports_atomic_update?`

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

Returns `true` if `link_type` supports atomic in-place `BPF_LINK_UPDATE` in
the Linux kernel (`:xdp`, `:tcx`, `:cgroup`, `:netkit`, `:netns`, `:sockmap`).

    true = MaeBPF.Link.supports_atomic_update?(:tcx)
    false = MaeBPF.Link.supports_atomic_update?(:perf_event)
    false = MaeBPF.Link.supports_atomic_update?(:tracing)

# `unpin`

Unpins a previously pinned `bpf_link` using its recorded pin path.

# `unpin`

Unpins a `bpf_link` from `path` under `/sys/fs/bpf/` via `bpf_link__unpin`.

# `update_program`

Atomically replaces the program attached to an active `bpf_link` via
`BPF_LINK_UPDATE`.

---

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