Linux eBPF & XDP Networking Primer
Linux eBPF & XDP Networking Primer
Panduan praktis untuk program BPF, XDP kait, dan kernel- bypass proses paket untuk insinyur jaringan.
Apa itu eBPF?
eBPF (extended Berkeley Packet Filter) adalah subsistem Linux kernel yang memungkinkan Anda menjalankan program sandboxed di dalam kernel tanpa memodifikasi kode sumber kernel atau memuat modul kernel. Program diverifikasi oleh verifikasi bytecode kernel sebelum eksekusi, memastikan keselamatan.
Untuk jaringan, program eBPF melampirkan ke di tumpukan jaringan kernel dan dapat memeriksa, memodifikasi, mengalihkan, atau menjatuhkan paket. Keuntungan kunci atas atau modul kernel adalah kinerja dan programmbility: program eBPF adalah JIT- dikompile ke kode asli dan dapat berbagi keadaan melalui (toko key-value dibagi antara kernel dan userspace).
| Hook | Lokasi | Latensi | Gunakan Kasus |
|---|---|---|---|
| XDP | Driver NIC, sebelum alokasi sk _ buff | Terrendah | DDoS drop, load balancing |
| tc ingress / egress | Setelah alokasi sk _ buff | Rendah | Lalu Lintas membentuk, menandai, mengarahkan kembali |
| penyaring socket | Path menerima socket | Sedang | tcpdump-style penyaringan |
| kprobe / tracepoint | Masuk / keluar fungsi kernel | Variunit-format | Observasi, pelacakan |
Titik Hook XDP
XDP (EXpress Data Path) program dijalankan pada titik paling awal mungkin dalam stack jaringan - di dalam driver NIC, sebelum kernel mengalokasikan Ini berarti:
- XDP Asli
-
XDP Generik
sk_buff - Oloaded XDP
Sebuah program XDP mengembalikan satu dari lima versi:
| Kode Kembali | Aksi |
|---|---|
XDP_DROP |
Jatuhkan paket segera - latensi terendah mengabaikan |
XDP_PASS |
Pass hingga tumpukan jaringan normal |
XDP_TX |
Kirim kembali antarmuka yang sama (bounce) |
XDP_REDIRECT |
Arahkan ke soket antar muka lain atau AF _ XDP |
XDP_ABORTED |
Galat lokasi - drop dengan acara jejak |
3. Contoh Jatuhkan Paket XDP
Program berikut ini menjatuhkan semua paket UDP dari IP sumber yang disimpan dalam peta eBPF, memungkinkan pesawat kendali ruang pengguna untuk memperbarui blocklist saat runtime.
// xdp_drop_udp.c — Drop UDP from IPs in a BPF map
#include
#include
#include
#include
#include
// BPF map: src IP → drop flag (1 = drop)
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 1024);
__type(key, __u32); // source IPv4 address
__type(value, __u32); // 1 = block
} blocklist SEC(".maps");
SEC("xdp")
int xdp_drop_udp(struct xdp_md *ctx) {
void *data = (void *)(long)ctx->data;
void *data_end = (void *)(long)ctx->data_end;
// Parse Ethernet header
struct ethhdr *eth = data;
if ((void *)(eth + 1) > data_end) return XDP_PASS;
if (eth->h_proto != __constant_htons(ETH_P_IP)) return XDP_PASS;
// Parse IPv4 header
struct iphdr *ip = (void *)(eth + 1);
if ((void *)(ip + 1) > data_end) return XDP_PASS;
if (ip->protocol != IPPROTO_UDP) return XDP_PASS;
// Check blocklist map
__u32 src = ip->saddr;
__u32 *val = bpf_map_lookup_elem(&blocklist, &src);
if (val && *val == 1) return XDP_DROP;
return XDP_PASS;
}
char _license[] SEC("license") = "GPL";
data_end
Muat dan kaitkan dengan :
# Compile
clang -O2 -target bpf -c xdp_drop_udp.c -o xdp_drop_udp.o
# Attach to interface (native XDP)
ip link set eth0 xdp obj xdp_drop_udp.o sec xdp
# Add an IP to the blocklist via bpftool
bpftool map update name blocklist key 0x01 0x02 0x03 0x04 value 0x01 0x00 0x00 0x00
# Remove XDP program
ip link set eth0 xdp off
4. AF _ XDP: Kernel- Bypass
AF_XDPXDP_REDIRECT
Komponen kunci:
- UMEM
- Cincin
- Mode Zero- salin
AF _ XDP ideal untuk pemrosesan paket kustom pada tingkat baris tanpa kerumitan operasional DPDK (tanpa hugepages, tanpa CPU pinning diperlukan untuk penggunaan dasar).
5. tc BPF: Macet & Penyaring
tcclsactsk_buff
// tc_mark.c — Mark packets with DSCP EF (46) for VoIP traffic on port 5060
#include
#include
#include
#include
#include
SEC("classifier")
int tc_mark_voip(struct __sk_buff *skb) {
void *data = (void *)(long)skb->data;
void *data_end = (void *)(long)skb->data_end;
struct ethhdr *eth = data;
if ((void *)(eth + 1) > data_end) return TC_ACT_OK;
if (eth->h_proto != __constant_htons(ETH_P_IP)) return TC_ACT_OK;
struct iphdr *ip = (void *)(eth + 1);
if ((void *)(ip + 1) > data_end) return TC_ACT_OK;
if (ip->protocol != IPPROTO_UDP) return TC_ACT_OK;
struct udphdr *udp = (void *)(ip + 1);
if ((void *)(udp + 1) > data_end) return TC_ACT_OK;
// Mark SIP traffic (port 5060) with DSCP EF (46 = 0xB8 in TOS byte)
if (udp->dest == __constant_htons(5060) || udp->source == __constant_htons(5060)) {
// DSCP EF = 46, shifted left 2 bits in TOS field = 184 (0xB8)
bpf_skb_store_bytes(skb, offsetof(struct iphdr, tos) + sizeof(struct ethhdr),
&((__u8){184}), 1, BPF_F_RECOMPUTE_CSUM);
}
return TC_ACT_OK;
}
char _license[] SEC("license") = "GPL";
# Attach tc BPF program
tc qdisc add dev eth0 clsact
tc filter add dev eth0 egress bpf da obj tc_mark.o sec classifier
Batas Rate dengan eBPF Maps
peta eBPF memungkinkan pemrosesan status. Pola berikut menerapkan per- source-IP rate membatasi menggunakan sebuah ember token disimpan dalam :
// Conceptual token bucket per source IP — checks tokens, drops if exceeded
struct ratelimit_entry {
__u64 tokens; // current token count
__u64 last_update; // nanoseconds timestamp
};
struct {
__uint(type, BPF_MAP_TYPE_LRU_HASH);
__uint(max_entries, 65536);
__type(key, __u32); // source IP
__type(value, struct ratelimit_entry);
} rate_map SEC(".maps");
// In XDP program:
// 1. bpf_ktime_get_ns() — get current time
// 2. Lookup entry for src IP
// 3. Refill tokens: tokens += (elapsed_ns / 1e9) * rate_pps
// 4. If tokens >= 1: decrement and XDP_PASS
// 5. Else: XDP_DROP
7. bpftool & bpftrace Introspeksi
Dua alat penting untuk bekerja dengan program eBPF hidup:
# bpftool — inspect loaded programs and maps
bpftool prog list # list all loaded eBPF programs
bpftool prog show id 42 # details for program ID 42
bpftool prog dump xlated id 42 # disassemble to eBPF bytecode
bpftool prog dump jited id 42 # dump JIT-compiled native code
bpftool map list # list all BPF maps
bpftool map dump name blocklist # dump all entries in map "blocklist"
bpftool map update name blocklist \
key 192 168 1 100 value 1 0 0 0 # add entry (network byte order)
# bpftrace — DTrace-style one-liners for kernel tracing
# Count XDP drops per second
bpftrace -e 'tracepoint:xdp:xdp_exception { @drops[args->action] = count(); } interval:s:1 { print(@drops); clear(@drops); }'
# Trace tcp_retransmit_skb — show retransmit events with comm name
bpftrace -e 'kprobe:tcp_retransmit_skb { printf("%s retransmit\n", comm); }'
# Histogram of packet sizes on eth0
bpftrace -e 'tracepoint:net:netif_receive_skb /args->name == "eth0"/ { @size = hist(args->len); }'
Perbandingan: eBPF / XDP vs DPDK vs RDMA
| Fitur | eBPF / XDP | DPDK | RDMA |
|---|---|---|---|
| Keterlibatan kernel | Minimal (XDP dalam driver) | Tidak ada (bypass penuh) | Tidak ada (RDMA NIC) |
| Model memori | UMEM + AF _ XDP Standar | Perlu hugepages | Daerah memori terdaftar |
| Melalui Max | ~ 100 Gbps asli XDP | > 100 Gbps | 200 + Gbps (InfiniBand) |
| Penggunaan CPU | Rendah (event- didorong) | Tinggi (bus-jajak pendapat) | Dekat nol (lepas) |
| Ops kompleksitas | Perkakas standar rendah | High - dedicated core, hugepages | Manajemen kain tinggi |
| Gunakan huruf besar kecil | Mitigasi DDoS, LB, observasi | router virtual, NFV, gen paket | Penyimpanan (NVMe-oF), HPC MPI |
| Bahasa | Terbatas C / Rust | C / Rust | Verbs API (C) |