Skip to content

eBPF Support

This is an experimental, gated feature that enables advanced system monitoring using eBPF tracepoints for tracking syscalls and other low-level events. The eBPF support provides detailed insights into a process's syscall patterns and resource utilization behaviors by tracking individual syscall types and frequencies.

Prerequisites

To build with eBPF support, you need:

sudo apt install clang libcap2-bin

libbpf headers are vendored in the repo (src/ebpf/include/bpf/), so libbpf-dev is no longer required. See portability.md for details on other distros and what the .o bytecode needs at runtime.

Kernel Settings

With the capabilities below (or as root), no sysctl needs changing. These settings restrict processes without the matching capability, so denet with CAP_BPF and CAP_PERFMON works under the hardened defaults:

  • kernel.unprivileged_bpf_disabled = 1 or 2: only blocks processes without CAP_BPF.
  • kernel.perf_event_paranoid = 2, or Ubuntu's 3 and 4: only blocks processes without CAP_PERFMON.
  • kernel.kptr_restrict: does not affect loading or attaching. It only decides whether off-CPU kernel stacks get function names or hex addresses (see Off-CPU Waits and Kernel Stacks).

Checked on Ubuntu 26.04 (kernel 7.0) with unprivileged_bpf_disabled=2, perf_event_paranoid=4 and kptr_restrict=1.

Building with eBPF Support

cargo build --release --features ebpf

System Configuration for eBPF Access

Option 1: Run with sudo (simplest approach)

You can run denet with sudo to get full access to eBPF features:

sudo target/release/denet --enable-ebpf run -- your_command_here

eBPF tracepoints require: 1. The CAP_BPF, CAP_PERFMON, and CAP_DAC_READ_SEARCH capabilities 2. Access to the tracefs filesystem in /sys/kernel/debug/tracing

1. Add Required Capabilities to the Binary

# Required capabilities for eBPF tracepoint access
sudo setcap cap_bpf,cap_perfmon,cap_dac_read_search=ep /path/to/denet/target/release/denet

# Verify capabilities are set
getcap /path/to/denet/target/release/denet
# Should show: cap_dac_read_search,cap_perfmon,cap_bpf=ep

Note: On modern kernels (5.8+) tracepoint access needs: - CAP_BPF — create/load BPF maps and programs. - CAP_PERFMON — perf_event_open(PERF_TYPE_TRACEPOINT, …) to attach the program. - CAP_DAC_READ_SEARCH — read /sys/kernel/tracing/events/<subsystem>/<event>/id, which is mode 0400 root-owned on most distros. Without this cap the attach step fails with No such file or directory (misleading; it's really EACCES when userspace follows the symlink). Dropping this cap means you must add your user to a group with read access to tracefs (see section 2).

2. Configure tracefs Access (Non-Persistent)

These commands will work until the next system reboot:

# Create a tracing group and add your user to it
sudo groupadd -r tracing
sudo usermod -aG tracing $USER

# Set permissions on debugfs and tracefs
sudo mount -o remount,mode=755 /sys/kernel/debug
sudo chgrp -R tracing /sys/kernel/debug/tracing
sudo chmod -R g+rwx /sys/kernel/debug/tracing

# Log out and log back in for group changes to take effect

3. Configure Persistent tracefs Access

For persistent configuration that survives system reboots, we provide setup tools in the setup/ directory:

# Run the automated setup script
sudo ./setup/setup_tracefs_permissions.sh

# Log out and log back in for group changes to take effect

The setup script: - Creates a 'tracing' group - Adds your user to the group - Creates a systemd service for persistent tracefs permissions - Sets up systemd mount overrides for debugfs - Configures kernel parameters for eBPF access - Sets permissions for the current session

Running inside a container

denet's eBPF programs report PIDs in denet's own PID namespace, so denet can run inside a container (Docker, Podman, Apptainer) and profile processes in that container, the same as on the host. The container needs the eBPF capabilities and the host's tracefs, and the kernel must be 5.7 or newer:

docker run --privileged -v /sys/kernel/tracing:/sys/kernel/tracing ... \
  denet --enable-ebpf run <command>

Processes outside denet's namespace are not recorded. Run from the host, in the initial namespace, denet sees every process, including those in containers.

Troubleshooting

If you encounter issues with eBPF:

  1. Verify kernel support:

    grep CONFIG_BPF /boot/config-$(uname -r)
    
    Should show CONFIG_BPF=y and related options.

  2. Test tracefs access:

    ls -la /sys/kernel/debug/tracing/events/syscalls
    
    You should be able to read this directory.

  3. Verify capabilities:

    getcap /path/to/denet/target/release/denet
    
    Should show cap_dac_read_search,cap_perfmon,cap_bpf=ep.

  4. Check if you're in the tracing group:

    groups $USER | grep tracing
    

  5. Run the eBPF diagnostic tool:

    # Build the diagnostic tool
    cargo build --release --bin ebpf_diag --features ebpf
    
    # Set capabilities
    sudo setcap cap_bpf,cap_perfmon,cap_dac_read_search=ep target/release/ebpf_diag
    
    # Run diagnostics
    ./target/release/ebpf_diag --debug
    

Common Issues and Solutions

  1. "Invalid ELF header size or alignment" error:
  2. Verify both CAP_BPF and CAP_PERFMON are set

  3. "No eBPF data available from monitored PIDs":

  4. Make sure the monitored process actually makes syscalls
  5. Commands like sleep make very few syscalls - try wget or file operations instead

  6. Works with sudo but not with capabilities:

  7. Check getcap shows all three capabilities; copying the binary or chown on it clears them
  8. A binary on a nosuid mount ignores its file capabilities

Using eBPF Features

To enable eBPF profiling when running denet:

denet --enable-ebpf run -- your_command_here

This will provide additional metrics about syscall usage and process behavior.

Syscall Tracking and Categorization

Denet tracks individual syscalls using eBPF tracepoints and categorizes them into functional groups to help analyze application behavior patterns. The system monitors the following syscalls:

  • read, write, openat, close (file operations)
  • mmap (memory management)
  • socket, connect, recvfrom, sendto (network operations)

The syscalls are then categorized into these functional groups:

Category Description Example Syscalls
file_io File and I/O operations read, write, open, close, lseek, openat
memory Memory allocation and management mmap, munmap, brk, rt_sigaction
process Process and thread management clone, fork, execve, exit, wait4
network Network-related operations socket, connect, accept, sendto, recvfrom
time Time and scheduling operations nanosleep, gettimeofday, clock_gettime
ipc Inter-process communication msgget, semget, shmget, msgsnd, semop
security Permission and security operations chmod, chown, capget, capset
signal Signal handling operations rt_sigaction, rt_sigprocmask, kill
system System configuration and information sysinfo, uname, reboot
other Uncategorized syscalls Any syscall not in the above categories

Example Output

When eBPF profiling is enabled, JSON output will include additional fields:

"ebpf": {
  "syscalls": {
    "total": 270795,
    "by_category": {
      "file_io": 162477,
      "memory": 40619,
      "network": 27080,
      "time": 13540,
      "process": 8124,
      "signal": 5416,
      "system": 5416,
      "security": 2708,
      "ipc": 2708,
      "other": 2708
    },
    "top_syscalls": [
      {"name": "read", "count": 81239},
      {"name": "write", "count": 54159},
      {"name": "openat", "count": 27080},
      {"name": "close", "count": 21664},
      {"name": "mmap", "count": 13540},
      {"name": "socket", "count": 13540}
    ],
    "analysis": {
      "syscall_rate_per_sec": 32295.16,
      "io_intensity": 0.6,
      "memory_intensity": 0.15,
      "cpu_intensity": 0.0,
      "network_intensity": 0.1
    }
  }
}

The top_syscalls field shows the most frequently called syscalls with their actual counts, and the by_category field shows how these syscalls are distributed across functional categories. The intensity fields are fractions of total tracked syscalls (uncategorized syscalls are excluded, so they do not sum to 1.0).

Per-Process Network Bytes

With eBPF enabled, denet reports network bytes attributed to the monitored process tree — unlike the sys_net_rx_bytes/sys_net_tx_bytes fields, which remain a system-wide approximation from /proc/net/dev and are kept as the fallback when eBPF is unavailable.

How it works: kprobes on the socket-layer send/receive paths (tcp_sendmsg/tcp_recvmsg, udp_sendmsg/udp_recvmsg) run in process context, so every byte is attributed by the kernel to the calling process. A BPF-side PID filter restricts accounting to the monitored tree; children it forks are added to the filter in the kernel (sched_process_fork), so a child is counted from its first byte.

Semantics:

  • Cumulative since monitoring started, including bytes from children that have already exited (totals never decrease).
  • RX counts bytes actually returned to userspace; TX counts bytes requested at the socket layer (a failed send may overcount slightly).
  • Socket-layer application bytes: no TCP/IP headers, no retransmissions.
  • Covers TCP over IPv4 and IPv6, and UDP over IPv4. UDP over IPv6 uses separate kernel entry points (udpv6_sendmsg) and is not yet counted.

JSON output (inside the aggregated metrics' ebpf object):

"network": {
  "rx_bytes": 10019992,
  "tx_bytes": 747,
  "per_pid": {
    "4123": { "rx_bytes": 10019992, "tx_bytes": 512 },
    "4130": { "rx_bytes": 0, "tx_bytes": 235 }
  },
  "retired": { "rx_bytes": 0, "tx_bytes": 0 }
}

rx_bytes/tx_bytes are the tree totals (authoritative). per_pid breaks the bytes out by live PID (root + current children), omitted while empty. retired holds bytes from children that have already exited and are no longer attributable to a live PID, omitted while zero. The three reconcile exactly: sum(per_pid) + retired == {rx,tx}_bytes, so a report's breakdown always adds up to the totals. If eBPF could not attach (missing capabilities), the object carries an error string instead of silently reporting zeros as real data.

From Python: pass enable_ebpf=True to execute_with_monitoring (or the ProcessMonitor constructors). This only does anything when the extension was built with the eBPF feature — the published wheels are eBPF-free, so build it yourself on Linux with pixi run develop-ebpf (needs clang). Without the feature, or without capabilities, the flag degrades to a logged warning. See python/examples/network_children.sh for an end-to-end run.

Required capabilities are the same as the rest of the eBPF features (cap_bpf,cap_perfmon,cap_dac_read_search — see above). To verify the whole capability matrix, including graceful degradation without caps and real data with caps-but-no-root:

./scripts/test_ebpf_caps.sh            # Phases A (no caps) + B (setcap)
./scripts/test_ebpf_caps.sh --with-root

The privileged integration tests can also be run directly:

sudo -E cargo test --features ebpf --test ebpf_net_monitor_tests -- --ignored

Off-CPU Waits and Kernel Stacks

With eBPF enabled, denet records when each thread of the monitored tree leaves the CPU and when it comes back, and the kernel stack it waited in. That answers why a process was idle: blocked writing to a full pipe, on disk I/O, on a lock, sleeping on a timer, or waiting for a child.

How it works: the sched_switch tracepoint captures the stack of the thread going off-CPU (the current task at that moment) and keeps it until the thread is switched back in, when the wait is reported. A kernel-side PID filter restricts this to the monitored tree; children forked by it are followed in the kernel (sched_process_fork), so a child that blocks at once is counted from its first wait, before denet's next sample sees it.

JSON output (inside the ebpf.offcpu object of each tree sample):

"waits": [
  {"pid": 4211, "stack": 17, "time_ns": 1998000000, "count": 1}
],
"kernel_stacks": {
  "17": ["perf_trace_sched_switch", "__schedule", "schedule",
         "anon_pipe_write", "vfs_write", "ksys_write", "__x64_sys_write"]
}
  • waits: off-CPU time per process and kernel stack credited since the previous sample, most time first. A wait is credited when the thread wakes up, so a long wait appears in the sample after it ends.
  • kernel_stacks: function names of each stack, innermost first, written once, in the first sample that refers to it. Stack 4294967295 means the stack couldn't be captured; its time is still counted.
  • Names come from /proc/kallsyms, which shows addresses only to privileged readers (root, or CAP_SYSLOG under kernel.kptr_restrict=1); otherwise frames are given as hex addresses.
  • Stacks are truncated to the 32 innermost kernel frames, which reach from schedule() to the system call entry.

Implementation Details

The eBPF implementation works by:

  1. Tracepoint Attachment: Attaching to syscall tracepoints (sys_enter_read, sys_enter_write, etc.)
  2. Per-syscall Tracking: Counting calls per syscall number for the monitored tree, whose forks are followed in the kernel
  3. Category Mapping: Categorizing each syscall into functional groups
  4. Runtime Analysis: Analyzing syscall patterns to detect performance bottlenecks

Attach Timing

Loading the probes takes a few hundred milliseconds. So that none of a command's activity is missed, denet run --enable-ebpf (and ProcessMonitor(cmd, enable_ebpf=True) in Python) holds the command before it starts: it runs under a small /bin/sh stub that waits on a pipe, the probes attach to the stub's PID, then the pipe is closed and the stub execs the command (same PID). Arguments pass through unchanged.

The hold always lifts, even when eBPF is unavailable or denet dies during setup (the pipe closes and the command runs), so it can never leave a command stuck. A missing command still fails immediately, as without eBPF.

Not covered:

  • attach / from_pid: activity before denet attaches is not recorded.

Children are followed in the kernel from the moment they are forked, so a child that starts and exits between two samples (e.g. a quick curl launched by a script) is counted in full.

Current Limitations

The current eBPF implementation has some limitations:

  1. Limited Syscall Coverage: Only tracks a subset of common syscalls (read, write, openat, close, mmap, socket, connect, recvfrom, sendto).

  2. Linux-only: The eBPF functionality is only available on Linux systems with kernel version 4.18+ for full functionality.

Future Enhancements

Planned improvements include: 1. Expanded syscall tracepoint coverage 2. More detailed syscall arguments analysis 3. Per-thread tracking in addition to per-process 4. Flame graph visualization of syscall patterns