r/eBPF • u/nycolazsec • 2d ago
AF_XDP for a SYN scanner: a single TX socket in copy mode tops out around 24k frames/s. Shared UMEM per queue, or keep TX on raw sockets?
I maintain [tcpcat](https://github.com/NycolazSec/tcpcat), an open-source port scanner in Go (AGPL). It has an optional AF_XDP engine, and I'd like a sanity check from people who know AF_XDP better than I do.
**Current design**
- An XDP program (built with cilium/ebpf, assembled in Go, no C/clang) matches replies to the scanner's source ports and redirects them through an XSKMAP to one AF_XDP socket per RX queue. Everything else gets `XDP_PASS`.
- Attach: native first, generic as a fallback. We bind with flags 0, then read `XDP_OPTIONS` back to find out whether we got zero-copy.
- TX: every probe (a 64-byte SYN) goes out through **one** AF_XDP socket. A single flusher goroutine owns its TX ring and kicks it with `sendto`.
**What we measured** (KVM, 8 vCPUs, kernel 6.12, veth, so copy mode, 131,070 probes)
- AF_XDP TX plateaus at about **24k frames/s**, whatever the rate ceiling. More workers don't help: the single TX ring and the per-`sendto` copy (32 descriptors per call in copy mode) are the wall.
- Our raw-socket engine sends from every core and finished the same scan about 3× faster (2.26 s vs 6.74 s). Both found all 80/80 open ports.
- So since v1.7, `--ebpf` falls back to raw sockets when the bind comes up in copy mode.
- We once had a bug where the TX ring backed up under high concurrency, and probes that were never sent got reported as "filtered". It's fixed, but it made me wary of other traps.
I have **never measured zero-copy**: I don't have a NIC that supports it. That's the gap.
**Questions**
- For a TX-heavy workload of tiny packets, is the right move one AF_XDP socket per queue with `XDP_SHARED_UMEM` and per-core TX? Or is AF_XDP TX in copy mode simply a dead end, and the sane design is `sendmmsg`/`PACKET_MMAP` (or io_uring) for TX with AF_XDP only on RX?
- In practice, which NICs and drivers give zero-copy (mlx5, ice, i40e…)? Does any **cloud** virtual NIC (ENA, gVNIC, multi-queue virtio-net) do zero-copy AF_XDP today?
- Are there pitfalls in redirecting only a port range to XSKs while passing everything else that I should know about?
If someone with a zero-copy-capable NIC has 10 minutes, the benchmark is one script ([benchmarks/README.md](https://github.com/NycolazSec/tcpcat/blob/main/benchmarks/README.md)). I'd gladly publish the numbers, good or bad.
