MaeBPF.Link (maebpf v0.1.0)

Copy Markdown View Source

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)

Summary

Functions

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

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

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

Attaches a kprobe or kretprobe program to kernel symbol func_name.

Attaches a raw tracepoint program to tp_name via bpf_program__attach_raw_tracepoint.

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

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

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

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

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

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

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

Destroys a bpf_link resource handle via bpf_link__destroy.

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

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

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

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

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

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

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

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

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

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

Unpins a previously pinned bpf_link using its recorded pin path.

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

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

Functions

attach(object, prog_name, target)

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

attach_auto(object, prog_name)

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(object, prog_name, cgroup_path)

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(object, prog_name, func_name, opts \\ [])

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(object, prog_name, tp_name)

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(object, prog_name, ifname_or_index, direction)

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

attach_tcx(object, prog_name, ifname_or_index, direction, opts)

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(object, prog_name, category, name)

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

attach_uprobe(object, prog_name, binary_path, sym_or_off, opts \\ [])

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(object, prog_name, ifname_or_index)

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

attach_xdp(object, prog_name, ifname_or_index, opts)

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(ifindex, opts, attach_fn)

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

destroy(link)

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(ifname)

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

info(link)

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

info_by_id(id)

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

normalize_tcx_opts(opts)

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

open_pinned(path)

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

pin(link, path)

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

pin_path(link)

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

read_iter(link, opts \\ [])

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(object, prog_name, target_prog_fd, attach_func_name)

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?(link_type)

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

Unpins a previously pinned bpf_link using its recorded pin path.

unpin(link, path)

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

update_program(link, object, prog_name)

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