GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - WebRTCventures/turn-ebpf-demo

BranchesTags
Open more actions menu

Latest commit

 

History

27 Commits
27 Commits

Folders and files

NameName
Last commit message
Last commit date
.vscode
.vscode
 
 
bpf
bpf
 
 
load_generator
load_generator
 
 
signaling
signaling
 
 
tests
tests
 
 
turn_server
turn_server
 
 
web-ui
web-ui
 
 
.envrc
.envrc
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
flake.lock
flake.lock
 
 
flake.nix
flake.nix
 
 
main.py
main.py
 
 
requirements-dev.txt
requirements-dev.txt
 
 
requirements.txt
requirements.txt
 
 
run_loadtest.py
run_loadtest.py
 
 

Repository files navigation

Supercharging TURN with eBPF/XDP

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.

Architecture

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

Project Structure

├── 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)

Prerequisites

  • Linux kernel ≥ 5.8 (for BPF_MAP_TYPE_RINGBUF)
  • Python 3.11+
  • BCC (BPF Compiler Collection) with Python bindings
  • iproute2 (for tc commands in TC mode)
  • NIC with native XDP support if using XDP mode (e.g. mlx5, ENA on c5n+, ixgbe, i40e)

Using Nix (recommended)

nix develop   # drops you into a shell with all deps

Manual

pip install -r requirements.txt
# BCC must be installed system-wide (apt install bpfcc-tools python3-bpfcc on Ubuntu)

Running the TURN Server

Machine A — TURN server on EC2

# 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)

Machine B — Load generator

# 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

Machine C — Web UI (browser)

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.

Benchmarking Workflow

  1. Launch the TURN server on Machine A in userspace mode, run load from Machine B, save results.
  2. Restart with --interface eth0 (eBPF enabled), repeat, save results.
  3. Compare the JSON output files to observe PPS and loss differences.

Running Tests

# Unit tests (no root required):
pytest tests/ -x

# BPF integration tests (requires root + BCC):
sudo -E pytest tests/test_bpf_integration.py -x

Configuration

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.

How the eBPF Offload Works

  1. Python handles all STUN control-plane messages (Allocate, ChannelBind, Refresh).
  2. On successful ChannelBind, the server inserts a forwarding entry into the BPF channel_map hash map.
  3. 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) in channel_map.
    • On hit: rewrites Ethernet MACs, IP addresses, UDP ports; recomputes IP checksum; redirects.
    • On miss: passes to kernel stack → Python handles it.
  4. Sampled events (1-in-N packets) are emitted to a ringbuf for metrics collection.

License

MIT — © 2025 WebRTC.ventures

About

No description, website, or topics provided.

Resources

Readme
MIT license
Activity
Custom properties

Stars

0 stars

Watchers

0 watching

Forks

1 fork
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.