It is 2:14 AM, and your phone will not stop buzzing. Packet loss is spiking on your monitoring dashboard, SSH feels like typing through mud, and top shows ksoftirqd pinned at 100% across every CPU core. A volumetric UDP flood is slamming your edge Linux server. You add an emergency drop rule in iptables or nftables, yet the box stays wedged.
Why does the kernel lock up? By the time traditional Linux firewalls inspect a packet, the kernel has already done the most expensive work. It allocates and populates a socket buffer (sk_buff) metadata structure—roughly 240 bytes of struct overhead plus shared info—for every single incoming frame. At 10 million packets per second (Mpps), those slab allocations and cache misses starve the CPU before a single firewall rule runs.
Surviving a flood like that without buying dedicated scrubbing hardware requires filtering traffic inside the NIC driver using eBPF/XDP (eXpress Data Path). On a 25GbE edge node hit by a 12 Mpps UDP flood, moving the drop rule from nftables to native XDP cut ksoftirqd CPU usage from 100% down to under 15%.
Approach Comparison: iptables/nftables vs. tc eBPF vs. XDP
Staring at a saturated interface at 2 AM is much easier to debug when you know where each Linux subsystem hooks into the packet lifecycle. Three main filtering layers handle packets at very different stages:
- Netfilter (iptables / nftables): Sits deep inside the kernel network stack. Every frame triggers a NAPI poll, enters a ring buffer, and gets a freshly allocated
sk_buffbefore Netfilter ever reads the IP header. Even bypassing connection tracking via therawPREROUTINGtable cannot avoid that allocation cost, which caps single-core drop rates around 1–2 Mpps. - tc (Traffic Control) with eBPF: Runs earlier than Netfilter, right at the ingress qdisc layer. Executing eBPF bytecode is fast, but
tcstill runs aftersk_buffallocation. You skip Netfilter traversal, yet you still pay the memory allocation tax. - XDP (eXpress Data Path): Hooks directly inside the NIC driver’s NAPI RX ring poll—before the kernel network stack knows a packet exists. An XDP program inspects raw DMA-mapped packet bytes and returns
XDP_DROP, recycling the RX memory page immediately at wire speed.
| Filtering Layer | Hook Point | sk_buff Allocated? | Typical Drop Capacity (per core) | Best Use Case |
|---|---|---|---|---|
| iptables / nftables | Netfilter hooks | Yes | ~1–2 Mpps | Stateful L3/L4 rules, NAT, normal traffic |
| tc (eBPF) | Ingress qdisc | Yes | ~4–6 Mpps | Traffic shaping, QoS, advanced packet mangling |
| XDP (Native Driver) | NIC driver RX ring | No | 10–24+ Mpps | High-speed DDoS drops, IP blocklists, SYN/UDP floods |
Pros and Cons of Driver-Level XDP Filtering
Before ripping out your existing firewall rules, weigh what XDP gives you against what it takes away.
Pros
- Zero
sk_buffoverhead: Dropping a packet in XDP skips allocating, initializing, and freeing kernel socket buffers. CPU and memory bus pressure collapse immediately. - Wire-speed performance: Running on the RX ring lets a single modern 3.0 GHz server core hit 14.88 Mpps (10GbE line rate) and push 20–24+ Mpps on 25GbE or 100GbE NICs.
- Live updates without restarts: User-space scripts can insert or remove attacker IPs in eBPF maps on the fly. You never need to detach or recompile the XDP program.
- Verifier safety: Before loading your bytecode, the in-kernel eBPF verifier checks for unbounded loops and out-of-bounds reads. A logic bug in your filter will not panic the kernel.
Cons
- Invisible to
tcpdump: Packets dropped viaXDP_DROPnever reach thesk_buffstage, sotcpdumpstays blind to them. Track drops instead withBPF_MAP_TYPE_PERCPU_ARRAYcounters,xdpdump, orethtool -S. - Driver dependency: Native XDP (
drvmode) requires explicit driver support, such asmlx5_core,ixgbe,i40e,ena, or modernvirtio_net. Unsupported drivers fall back to generic mode (xdpgeneric), which runs aftersk_buffallocation. - Stateless by default: XDP has no built-in connection tracking like Netfilter’s
conntrack. Either build lightweight state tables in eBPF maps or pass clean traffic up tonftables.
Recommended Setup for Production DDoS Mitigation
Do not try to replace your entire firewall with XDP on a production edge node. Use a two-tier defense instead:
- Tier 1 (XDP in
drvmode): Run a lean eBPF/XDP program at the driver layer to discard obvious junk—known bad prefixes in a BPF LPM trie map, malformed headers, or volumetric UDP floods hitting unused ports. - Tier 2 (nftables + Application limits): Return
XDP_PASSfor everything else. Legitimate traffic enters the kernel stack normally, wherenftablesenforces stateful rules and your reverse proxy handles Layer 7 rate limits.
Keep one strict 2 AM safety rule: always test over SSH with a fallback timer. Schedule a temporary at or systemd-run job to detach XDP after 3 minutes before loading a new filter. One typo that returns XDP_DROP on all packets will sever your SSH session instantly.
Implementation Guide: Dropping Flood Packets Before sk_buff
Let’s build a focused XDP filter that drops incoming UDP packets aimed at port 9999—a common pattern in amplification floods—while passing all other traffic untouched.
Step 1: Verify NIC Driver Support and Install Tools
Start by checking which driver powers your network interface (swap eth0 for your interface name) and installing the eBPF toolchain on Ubuntu or Debian:
ethtool -i eth0 | grep driver
sudo apt update && sudo apt install -y clang llvm libbpf-dev linux-headers-$(uname -r) bpftool iproute2
Look for output like driver: mlx5_core, ixgbe, i40e, ena, or virtio_net, all of which support native XDP on modern Linux kernels.
Step 2: Write the XDP Program in C
Save the following code as xdp_drop_udp.c. Pay close attention to the pointer bounds checks: the eBPF verifier rejects any program that reads a single byte without proving it sits before data_end.
#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>
#include <bpf/bpf_endian.h>
#define FLOOD_PORT 9999
SEC("xdp")
int xdp_drop_udp_flood(struct xdp_md *ctx) {
void *data = (void *)(long)ctx->data;
void *data_end = (void *)(long)ctx->data_end;
// 1. Parse Ethernet header
struct ethhdr *eth = data;
if ((void *)(eth + 1) > data_end)
return XDP_PASS;
// Only inspect untagged IPv4 packets
if (eth->h_proto != bpf_htons(ETH_P_IP))
return XDP_PASS;
// 2. Parse IPv4 header
struct iphdr *ip = (void *)(eth + 1);
if ((void *)(ip + 1) > data_end)
return XDP_PASS;
// Only inspect UDP packets
if (ip->protocol != IPPROTO_UDP)
return XDP_PASS;
// Account for IPv4 options using ihl
__u32 ip_hdr_len = ip->ihl * 4;
if (ip_hdr_len < sizeof(struct iphdr))
return XDP_PASS;
// 3. Parse UDP header
struct udphdr *udp = (void *)ip + ip_hdr_len;
if ((void *)(udp + 1) > data_end)
return XDP_PASS;
// Drop flood packets before sk_buff is ever allocated
if (udp->dest == bpf_htons(FLOOD_PORT))
return XDP_DROP;
return XDP_PASS;
}
char _license[] SEC("license") = "GPL";
Note that if your traffic arrives on a VLAN trunk (ETH_P_8021Q), you will need to advance past the 4-byte 802.1Q tag before parsing the IPv4 header.
Step 3: Compile and Attach to the NIC Driver
Compile the C source into eBPF bytecode with clang, then attach the object file directly to your driver via iproute2:
clang -O2 -g -Wall -target bpf -c xdp_drop_udp.c -o xdp_drop_udp.o
sudo ip link set dev eth0 xdpdrv obj xdp_drop_udp.o sec xdp
Virtual NICs or older drivers without native support will reject xdpdrv with an Operation not supported error. You can test the logic with xdpgeneric instead, though it will not bypass sk_buff allocation.
Step 4: Verify and Detach
Confirm the filter is running at the driver layer:
sudo bpftool net show dev eth0
ip link show dev eth0
Look for an entry such as eth0(2) driver id 42 in bpftool or xdp next to the interface in ip link. When the attack subsides or you want to roll back, detach the program cleanly:
sudo ip link set dev eth0 xdpdrv off
Watch ksoftirqd drop back to idle in top while legitimate connections stay responsive. Once you see XDP absorb a multi-million-pps flood without breaking a sweat, fighting volumetric attacks with iptables alone feels like bringing a sponge to a firehose.

