Skip to main content

Overview

This guide walks you through creating your first network monitoring application using eBPF Event Interceptor. You’ll learn how to:
  • Load the TCP and UDP interceptor libraries
  • Define custom eBPF programs
  • Capture and process network events in real-time
All examples require root privileges to load eBPF programs into the kernel.

TCP Event Monitoring

Monitor TCP connections using the libtcpEvent.so library.

Basic TCP Monitor

1

Include headers and define structures

Create a new C file with the necessary includes:
tcp_monitor.c
2

Define your eBPF program

Write the kernel-side eBPF code that captures TCP state changes:
This eBPF program attaches to the tcp_set_state kernel function and captures TCP connection events when connections close. It tracks connection lifetime, process information, and traffic statistics.
3

Load the library and initialize

Dynamically load the TCP interceptor library:
4

Process events

Read and display TCP events in a loop:

Build and Run

Compile your TCP monitor:
Run with root privileges:

Expected Output

When TCP connections close on your system, you’ll see events like:

UDP Event Monitoring

Monitor UDP traffic using the libudpEvent.so library.

Basic UDP Monitor

Build and Run

Expected Output

Key API Functions

Both libraries expose the same core API via dynamic loading:
Important differences:
  • TCP’s AddProbe() requires your custom eBPF program as a parameter
  • UDP’s AddProbe() takes no arguments (uses built-in eBPF code)
  • Always call cleanup() before exiting to properly detach kernel probes

Understanding the Event Structures

TCP Event Fields

UDP Event Fields

Best Practices

Error Handling

Always check return values from dlopen() and dlsym(). Use dlerror() for detailed error messages.

Signal Handling

Implement signal handlers to call cleanup() on SIGINT/SIGTERM to properly detach probes.

Resource Management

Call dlclose() before exiting to release library resources.

Privileges

Run with sudo or set appropriate capabilities (CAP_BPF, CAP_PERFMON).

Next Steps

  • Customize eBPF Programs: Modify the eBPF code to filter specific connections or capture additional metrics
  • Integrate with Observability: Send events to your telemetry pipeline (Prometheus, Kafka, etc.)
  • Performance Tuning: Adjust BPF map sizes and filtering logic for high-traffic environments
  • Security Monitoring: Build real-time alerts for suspicious network patterns
For production deployments, consider implementing event buffering, rate limiting, and log rotation to handle high-volume network environments.