A proof-of-concept TURN server (RFC 5766) with kernel-level ChannelData relay via eBPF. The BPF program intercepts TURN ChannelData packets at the TC or XDP hook, rewrites L2/L3/L4 headers, and forwards them without ever reaching userspace — drastically reducing per-packet CPU cost and latency.
Machine A (EC2 — TURN Server) Machine B (EC2 — Load Generator)
┌─────────────────────────────┐ ┌─────────────────────────────┐
│ Python TURN server │ │ run_loadtest.py │
│ (control plane: Allocate, │◀──STUN────▶│ (TURN client sessions, │
│ Refresh, ChannelBind) │ │ ramps ChannelData PPS) │
│ │ │ │
│ eBPF (TC or XDP) │◀──UDP─────▶│ Outputs JSON results to │
│ (data plane: ChannelData │ │ docs/results/ │
│ header rewrite + forward) │ └─────────────────────────────┘
│ │
│ Metrics server (:9090) │
│ Signaling + Web UI (:8080) │◀──Browser── Machine C (laptop)
└─────────────────────────────┘
Two paths, one server:
| Mode | Flag | How it works |
|---|---|---|
| Userspace (Python) | --no-offload |
asyncio DatagramProtocol handles all relay |
| eBPF TC offload | --interface eth0 --mode tc |
BPF on TC ingress rewrites + bpf_redirect() |
| eBPF XDP offload | --interface eth0 --mode xdp |
BPF at XDP hook rewrites + XDP_TX |
├── main.py # Entry point — wires server, signaling, BPF loader
├── run_loadtest.py # CLI load generator (run from Machine B)
├── turn_server/ # Python TURN implementation
│ ├── server.py # DatagramProtocol (STUN dispatch, relay)
│ ├── stun.py # STUN/TURN message parser (RFC 5389/5766)
│ ├── allocations.py # Allocation state, channel bindings, expiry
│ ├── auth.py # Long-term credential auth (HMAC-SHA1)
│ ├── relay.py # Per-allocation relay socket
│ ├── offload.py # BPF TC offload manager (BCC, map ops)
│ ├── loader.py # Unified TC/XDP loader
│ ├── metrics.py # HTTP metrics endpoint (:9090)
│ └── config.py # Dataclass configs
├── bpf/ # eBPF C sources
│ ├── turn_offload_tc.c # TC ingress program
│ ├── turn_offload_xdp.c # XDP program
│ └── turn_offload_common.h# Shared structs (channel_key, fwd_entry)
├── load_generator/ # Synthetic TURN traffic generator
│ └── generator.py # Asyncio TURN client, PPS ramp stages
├── web-ui/ # Browser demo (served by signaling server)
│ ├── index.html # Page layout (video + histogram)
│ ├── app.js # WebRTC peer pair through TURN
│ ├── dashboard.js # Polls /metrics, renders histogram
│ ├── histogram.js # Log-scale processing-time histogram
│ └── datachannel-probe.js # DataChannel RTT + loss probe
├── signaling/ # HTTP static file server for web UI
│ └── server.py
├── tests/ # pytest suite (unit + BPF integration)
└── flake.nix # Nix dev shell (Python + BCC + clang + iproute2)
- Linux kernel ≥ 5.8 (for
BPF_MAP_TYPE_RINGBUF) - Python 3.11+
- BCC (BPF Compiler Collection) with Python bindings
- iproute2 (for
tccommands in TC mode) - NIC with native XDP support if using XDP mode (e.g. mlx5, ENA on c5n+, ixgbe, i40e)
nix develop # drops you into a shell with all depspip install -r requirements.txt
# BCC must be installed system-wide (apt install bpfcc-tools python3-bpfcc on Ubuntu)# Userspace only (no BPF): sudo python main.py --bind 0.0.0.0 --external-ip <PUBLIC_IP> --no-offload # With eBPF TC offload: sudo python main.py --bind 0.0.0.0 --external-ip <PUBLIC_IP> \ --interface eth0 --relay-ip <PRIVATE_IP> # With eBPF XDP offload: sudo python main.py --bind 0.0.0.0 --external-ip <PUBLIC_IP> \ --interface eth0 --relay-ip <PRIVATE_IP> --mode xdp
The server listens on:
- UDP :3478 — TURN protocol
- HTTP :8080 — Signaling WebSocket + web UI static files
- HTTP :9090 — Metrics endpoint (JSON)
# Simple PPS ramp: python run_loadtest.py --host <SERVER_IP> --pps 50000 --duration 30 # With custom ramp stages: python run_loadtest.py --host <SERVER_IP> --pps 100000 \ --ramp-stages 1000,5000,10000,25000,50000,100000 # Save results to JSON: python run_loadtest.py --host <SERVER_IP> --pps 50000 --duration 30 \ --output docs/results/ebpf.json
Open http://<SERVER_PUBLIC_IP>:8080 in a browser. Enter the server's public IP, click Connect. The WebRTC video stream is forced through the TURN relay. Apply load from Machine B to observe the processing-time histogram shift.
- Launch the TURN server on Machine A in userspace mode, run load from Machine B, save results.
- Restart with
--interface eth0(eBPF enabled), repeat, save results. - Compare the JSON output files to observe PPS and loss differences.
# Unit tests (no root required): pytest tests/ -x # BPF integration tests (requires root + BCC): sudo -E pytest tests/test_bpf_integration.py -x
Default credentials (for demo purposes):
- Username:
user - Password:
pass - Realm:
turn.local
All settings are in turn_server/config.py. Override via CLI flags — run python main.py --help for the full list.
- Python handles all STUN control-plane messages (Allocate, ChannelBind, Refresh).
- On successful ChannelBind, the server inserts a forwarding entry into the BPF
channel_maphash map. - The BPF program (attached at TC ingress or XDP) intercepts incoming UDP on port 3478:
- Discriminates ChannelData (first byte
0x40–0x4F) from STUN (0x00–0x03). - Looks up
(channel_number, src_ip, src_port)inchannel_map. - On hit: rewrites Ethernet MACs, IP addresses, UDP ports; recomputes IP checksum; redirects.
- On miss: passes to kernel stack → Python handles it.
- Discriminates ChannelData (first byte
- Sampled events (1-in-N packets) are emitted to a ringbuf for metrics collection.
MIT — © 2025 WebRTC.ventures