Sulabh Biswas / Blog

Deep dives into Linux networking and Go

Getting Started with eBPF

eBPF (extended Berkeley Packet Filter) is a revolutionary technology that allows safe, sandboxed programs to run in the Linux kernel. In this post, we’ll explore the basics and get you started with your first eBPF program.

What is eBPF?

eBPF is a kernel technology that enables developers to run custom programs within the Linux kernel space. It provides a safe and efficient way to extend kernel functionality without modifying kernel source code.

Key Benefits

  • Safety: eBPF programs are verified before execution
  • Performance: Runs in kernel space with minimal overhead
  • Flexibility: Can be attached to various kernel events
  • Observability: Powerful tracing and monitoring capabilities

Simple Example

Here’s a basic eBPF program that counts network packets:

#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>

struct {
    __uint(type, BPF_MAP_TYPE_ARRAY);
    __type(key, __u32);
    __type(value, __u64);
    __uint(max_entries, 1);
} packet_count SEC(".maps");

SEC("xdp")
int count_packets(struct xdp_md *ctx) {
    __u32 key = 0;
    __u64 *count = bpf_map_lookup_elem(&packet_count, &key);
    if (count) {
        __sync_fetch_and_add(count, 1);
    }
    return XDP_PASS;
}

Getting Started

  1. Install required tools: clang, llvm, libbpf
  2. Write your eBPF program in C
  3. Compile with: clang -target bpf -O2 -c program.c -o program.o
  4. Load and attach using libbpf or bpftool

Next Steps

In future posts, we’ll explore more advanced eBPF use cases including:

  • Network monitoring and security
  • Performance profiling
  • Container networking
  • Kubernetes observability

Stay tuned!