Back to articles
Security & Anti-DDoS

eBPF-based Firewall on VPS: Effective 10Gbps DDoS Mitigation with Low CPU Usage

Technical guide to deploy an eBPF-based firewall on VPS for effective 10Gbps DDoS blocking with minimal CPU resource consumption, optimizing network performance and security.

DPTCloud Engineering4 min read
eBPF-based Firewall on VPS: Effective 10Gbps DDoS Mitigation with Low CPU Usage

Image credit: Tumisu · Pixabay Content License

Introduction: eBPF-based Firewall on VPS – Effective 10Gbps DDoS Blocking

As large-scale DDoS attacks become increasingly common, deploying effective security solutions on VPS systems is essential. eBPF (Extended Berkeley Packet Filter) enables powerful firewall implementations at the kernel level, allowing malicious traffic to be dropped directly in the kernel without significantly increasing CPU load. This guide demonstrates how to deploy an eBPF-based firewall on a VPS to block 10Gbps DDoS attacks efficiently, with minimal CPU resource usage, ensuring high performance and robust network security.

Solution: eBPF-based Firewall with XDP and BPF

eBPF allows safe code execution within the kernel without loading modules. When combined with XDP (eXpress Data Path), it enables packet processing at the network layer, before packets reach the TCP/IP stack, reducing latency and improving performance. Key components include:

  • XDP: Processes packets immediately upon receipt from the network card, allowing early drop or forward decisions in the kernel.
  • BPF: Loads eBPF programs into the kernel to execute filtering logic (accept, drop).
  • TC (Traffic Control): Integrates BPF into the network stack for packet handling after XDP.

Deploying eBPF-based Firewall on VPS

System requirements:

  • Linux kernel 5.6 or later (with XDP and BPF support)
  • Network card supporting XDP (driver-dependent)
  • Tools: bpftool, libbpf, clang, llvm

Follow these steps:

1. Install eBPF Tools

sudo apt update
sudo apt install -y clang llvm libelf-dev libbpf-dev

2. Write BPF Program to Block DDoS

Create file ddos_filter.bpf.c:

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

#define MAX_CONNECTIONS 1000
#define TIME_WINDOW 10

struct conn_key {
    __u32 src_ip;
    __u16 src_port;
};

struct conn_counter {
    __u32 count;
    __u64 last_time;
};

struct bpf_map_def SEC("maps") conn_map = {
    .type = BPF_MAP_TYPE_HASH,
    .key_size = sizeof(struct conn_key),
    .value_size = sizeof(struct conn_counter),
    .max_entries = MAX_CONNECTIONS,
};

SEC("xdp")
int xdp_filter(struct xdp_md *ctx) {
    void *data = (void *)(long)ctx->data;
    void *data_end = (void *)(long)ctx->data_end;
    struct eth_hdr *eth = data;
    struct iphdr *ip = data + sizeof(*eth);
    struct tcphdr *tcp = data + sizeof(*eth) + sizeof(*ip);

    if (data + sizeof(*eth) + sizeof(*ip) + sizeof(*tcp) > data_end)
        return XDP_PASS;

    struct conn_key key = {
        .src_ip = bpf_ntohl(ip->saddr),
        .src_port = bpf_ntohs(tcp->source)
    };

    struct conn_counter *counter = bpf_map_lookup_elem(&conn_map, &key);
    if (counter) {
        if (bpf_ktime_get_ns() - counter->last_time < TIME_WINDOW * 1000000000) {
            if (counter->count > 500) {
                return XDP_DROP;
            }
            counter->count++;
        } else {
            counter->count = 1;
        }
        counter->last_time = bpf_ktime_get_ns();
    } else {
        struct conn_counter init = { .count = 1, .last_time = bpf_ktime_get_ns() };
        bpf_map_update_elem(&conn_map, &key, &init, BPF_ANY);
    }

    return XDP_PASS;
}

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

3. Compile and Load the Program

clang -O2 -target bpf -c ddos_filter.bpf.c -o ddos_filter.bpf.o
bpftool prog load ddos_filter.bpf.o /sys/fs/bpf/xdp_filter
bpftool map show

4. Attach BPF to Network Interface

sudo bpftool prog attach /sys/fs/bpf/xdp_filter dev eth0 ingress

Testing and Optimization

After deployment, verify performance using:

  • bpftool prog show: Check BPF program status.
  • watch -n 1 'cat /sys/fs/bpf/conn_map': Monitor connection counts.
  • iftop or nethogs: Monitor network traffic.

To optimize performance:

  • Reduce map size if connection counts are low.
  • Set appropriate time window limits.
  • Use TC if XDP support is limited.

Enhanced Solution: Combine with DPTCloud

To strengthen 10Gbps DDoS blocking, integrate the eBPF-based firewall with Cloud VPS Anti-DDoS Anycast L3/L4/L7 from DPTCloud. This solution provides:

  • DDoS protection at network (L3/L4) and application (L7) levels.
  • Traffic distribution across multiple edge points (Anycast).
  • 99.98% SLA and comprehensive VPS protection.

Alternative Solution: eBPF with Tetragon

For deeper system monitoring, integrate Tetragon to detect early malicious behavior. Tetragon uses eBPF to monitor system activity, helping detect and block internal DDoS attacks.

Recommendations and Notes

  • Do not deploy eBPF-based firewall on VPS with outdated kernels.
  • Test in staging environments before production deployment.
  • Monitor CPU and RAM to ensure resource usage stays within limits.
  • Combine with other security solutions like iptables, WAF, and provider-level DDoS mitigation.

Troubleshooting

  • Unable to load BPF program: Verify kernel supports XDP and BPF.
  • DDoS not blocked: Check BPF logic, time window, and connection limits.
  • High CPU usage: Reduce map size and optimize processing logic.

References

DPTCloud Tier III Infrastructure

Recommended NVMe Cloud VPS Plans to Deploy This Setup

Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.

View 22 VPS Plans
ECO · vn-02

VPS-2E

94.600 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 2 GB
  • SSD: 30 GB NVMe
ECO · vn-02Recommended

VPS-3E

170.500 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 4 GB
  • SSD: 50 GB NVMe
BASIC · vn-01Recommended

VPS-05

457.600 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe

Related Articles

View all
ZaloZalo SupportDiscord0384 753 478