High-Speed Packet Filtering with Linux XDP: Defending Servers at the Driver Layer

Linux tutorial - IT technology blog
Linux tutorial - IT technology blog

The Problem: Why Traditional Linux Firewalls Choke Under Floods

Hit a standard Linux server with a 5 Mpps (million packets per second) SYN flood or an amplified DNS reflection attack, and iptables or nftables will buckle. CPU cores pin at 100%. SoftIRQ processing consumes every spare cycle. Within seconds, legitimate SSH sessions drop and API endpoints stop responding.

Adding more CPU cores barely delays the crash. Traditional packet filtering architectures hit a structural ceiling long before saturating a modern 10GbE or 25GbE link.

The Bottleneck: Kernel Network Stack Overhead

Linux was built for reliability, routing flexibility, and rich protocol handling, not raw packet disposal. When an incoming Ethernet frame lands on your network interface card (NIC), the kernel runs through a heavy pipeline before user firewall rules ever see it:

  • DMA transfer and hardware IRQs: The NIC moves frame data into ring buffers and fires an interrupt to schedule SoftIRQ processing via NAPI.
  • SKB allocation: The kernel allocates a sk_buff metadata structure—roughly 240 bytes of overhead plus headroom—for every single packet.
  • Cache thrashing and state checks: Parsing headers, running conntrack table lookups, and updating route caches evict hot data from L2 and L3 caches.

If an attacker floods your host with 64-byte packets at line rate, the CPU exhausts its budget just allocating and tearing down sk_buff structs. The firewall rules never stood a chance.

The Solution: Dropping Packets Early with XDP

eXpress Data Path (XDP) solves this by moving packet inspection down into the driver layer. Paired with eBPF (Extended Berkeley Packet Filter), XDP executes verified bytecode directly on raw packet memory inside the driver receive ring—before the OS allocates any sk_buff.

On a single Xeon core, standard iptables drops roughly 1.5 to 2 Mpps. Native XDP can discard 14 to 20+ Mpps on the same hardware.

Your XDP hook inspects the frame buffer and returns one of four core verdicts:

  • XDP_DROP: Discards the packet immediately inside the driver ring, consuming almost no CPU cycles.
  • XDP_TX: Turns the packet around and bounces it back out the same interface for ultra-low latency load balancing.
  • XDP_PASS: Hands the clean packet up to the standard Linux IP stack for normal application delivery.
  • XDP_REDIRECT: Bypasses the kernel stack to forward the raw frame to another NIC, another CPU core, or directly into user space via AF_XDP.

Hands-on Implementation: Building an XDP Port Filter

Here is a working XDP program written in C that parses raw Ethernet frames, identifies IPv4 UDP traffic, and discards anything targeting UDP port 9999.

1. Preparing Build Dependencies

Install the required compiler toolchain and eBPF development headers on Ubuntu or Debian:

sudo apt update
sudo apt install -y clang llvm libelf-dev libbpf-dev gcc-multilib linux-headers-$(uname -r) iproute2

2. Writing the XDP C Program

Create a file named xdp_drop_udp.c. Every pointer dereference must be bounded with safety checks, or the kernel eBPF verifier will reject the program during loading:

#include <linux/bpf.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/udp.h>
#include <linux/in.h>
#include <bpf/bpf_helpers.h>

#define TARGET_PORT 9999

SEC("xdp")
int filter_udp(struct xdp_md *ctx) {
    void *data_end = (void *)(long)ctx->data_end;
    void *data = (void *)(long)ctx->data;

    // Verify Ethernet header fits within bounds
    struct ethhdr *eth = data;
    if ((void *)(eth + 1) > data_end)
        return XDP_PASS;

    // Inspect only IPv4 frames
    if (eth->h_proto != __constant_htons(ETH_P_IP))
        return XDP_PASS;

    // Verify IPv4 header bounds
    struct iphdr *ip = (void *)(eth + 1);
    if ((void *)(ip + 1) > data_end)
        return XDP_PASS;

    // Isolate UDP traffic
    if (ip->protocol != IPPROTO_UDP)
        return XDP_PASS;

    // Verify UDP header bounds using IP header length
    struct udphdr *udp = (void *)((unsigned char *)ip + (ip->ihl * 4));
    if ((void *)(udp + 1) > data_end)
        return XDP_PASS;

    // Drop target port traffic instantly
    if (udp->dest == __constant_htons(TARGET_PORT)) {
        return XDP_DROP;
    }

    return XDP_PASS;
}

char _license[] SEC("license") = "GPL";

3. Compiling to eBPF Bytecode

Use Clang to target the BPF backend architecture:

clang -O2 -target bpf -c xdp_drop_udp.c -o xdp_drop_udp.o

4. Attaching the Filter to Your NIC

XDP operates in two primary modes on production systems: native mode (xdpdrv), which hooks directly into driver receive queues for peak throughput, and generic mode (xdpgeneric), a testing fallback that runs after sk_buff creation.

Always verify network configurations on an isolated staging VM or with IPMI console access ready. A faulty parser rule can instantly lock you out of SSH.

Attach the filter in native driver mode on interface eth0:

# Attach via native driver mode for line-rate performance
sudo ip link set dev eth0 xdpdrv obj xdp_drop_udp.o sec xdp

If your cloud hypervisor driver lacks native XDP support (common on older virtual NICs), substitute xdpgeneric for testing.

Confirm the filter is attached:

ip link show dev eth0

Look for the xdp or xdpdrv flag accompanied by the loaded BPF program ID.

5. Verification and Teardown

From an external host, blast UDP packets toward port 9999:

nc -u -z -v <SERVER_IP> 9999

Now inspect traffic on the destination server using tcpdump:

sudo tcpdump -i eth0 udp port 9999

Under native xdpdrv, tcpdump registers zero packets. The driver drops them before the kernel packet tap (AF_PACKET) ever gets invoked. Your user-space stack stays completely quiet.

To detach the program and return the interface to normal behavior:

sudo ip link set dev eth0 xdpdrv off

When to Use XDP in Production

XDP does not replace nftables for stateful outbound connection tracking or application routing. Instead, it serves as a lightweight first line of defense. By discarding garbage packets right as they touch the NIC ring buffer, your server retains the CPU headroom needed to serve real traffic under intense flood conditions.

Share: