Skip to content

About

Lightweight network traffic monitor built from scratch in Go: AF_PACKET + PACKET_MMAP zero-copy capture, sharded flow aggregation, custom binary log, live HTTP dashboard.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

WonderWall

A lightweight, zero-dependency network traffic monitor for Linux, written in Go.

WonderWall captures raw Ethernet frames straight off a network interface using AF_PACKET + PACKET_MMAP, aggregates them into bidirectional flows, persists closed flows to disk in a compact binary format, and serves a live dashboard over HTTP — all from a single static binary, with no external dependencies: no libpcap, no cgo, no third-party Go modules. Just the standard library and raw syscalls.

This project started as a deep dive into how packet capture actually works under the hood, and grew into a small but complete monitoring pipeline: kernel → memory → aggregation → disk → browser.


Why no libpcap?

Tools like tcpdump and Wireshark, and Go wrappers like gopacket, rely on libpcap for packet capture. In its classic mode, libpcap reads packets with read()/recvfrom(), which means the kernel copies every single packet from its internal buffer into a userspace buffer on every call.

Modern libpcap (and gopacket/afpacket) can use the same PACKET_MMAP mechanism WonderWall uses — but it's an optional path, wrapped behind a C library (or golang.org/x/sys/unix), pulled in as an external dependency either way.

WonderWall makes the fast path the only path, implemented directly with syscall.Syscall/syscall.Syscall6 — no cgo, no external packages, full control over every byte crossing the kernel/userspace boundary.


How packet capture works here

Instead of asking the kernel for packets one at a time (read()), WonderWall sets up a shared memory ring buffer between the kernel and the process. The kernel writes incoming packets directly into this shared region; the userspace process reads them out — no read() syscall per packet, no memcpy per packet.

Zero-copy packet capture via PACKET_MMAP

The setup is two syscalls:

  1. setsockopt(PACKET_RX_RING) — tells the kernel to reserve a ring of fixed-size frames instead of using its normal, scattered per-packet allocation.
  2. mmap() — maps that kernel-owned ring into the process's own virtual address space, so both sides see the exact same physical memory.

Each frame in the ring starts with a tpacket_hdr — a small header whose tp_status field acts as a lock-free handoff flag between kernel and userspace: TP_STATUS_KERNEL means "the kernel may write here", TP_STATUS_USER means "a packet is ready to read". No mutex, no syscall required to check it — just a memory read of an agreed-upon offset. When there's nothing to read, the loop calls poll() instead of busy-spinning, so it sleeps without burning CPU and wakes up the instant a frame becomes ready.


Pipeline architecture

Once a raw frame is captured, it flows through a pipeline designed so the hot path (packet capture) is never blocked by slower, mutex-guarded work.

Aggregation and persistence pipeline

  • Main goroutine reads frames straight from the ring buffer, parses Ethernet/IPv4/TCP headers in place (no copying), and pushes a small Task onto a buffered channel.
  • The send is non-blocking (select / default): if the channel is full, the packet's statistics are dropped rather than stalling capture. Capture throughput is prioritized over 100% counting accuracy — an explicit tradeoff, not an oversight.
  • A pool of Aggregator workers consumes the channel and updates flow statistics in a sharded map (N independent maps, each with its own mutex, selected by fnv32a(flowKey) % N). Sharding reduces — but doesn't eliminate — lock contention between workers, since flows that hash to the same shard still serialize.
  • Flow keys are normalized: the two IPs of a connection are sorted so that both directions of the same conversation (A→B and B→A) land in the same bucket.
  • A background Reaper goroutine periodically scans for flows that have gone idle, removes them from the in-memory map, and appends a closed-flow record to disk — using short, per-key lock/unlock pairs rather than holding a lock for the whole scan, so it never blocks the aggregators for long.

On-disk format

Closed flows are persisted in a small, purpose-built binary format (no JSON, no external serialization library) — designed the same way the kernel's tpacket_hdr contract was: fixed offsets, fixed sizes, explicit versioning.

File header (8 bytes, written once):

Offset Size Field Notes
0 4 Magic "EZWL"
4 2 Version uint16, little-endian
6 2 RecordSize uint16, little-endian

Record (32 bytes, repeated):

Offset Size Field Notes
0 4 IP_A smaller IP of the normalized pair
4 4 IP_B larger IP of the normalized pair
8 8 Packets uint64, little-endian
16 8 Bytes uint64, little-endian
24 4 FirstSeen unix timestamp, uint32
28 4 LastSeen unix timestamp, uint32

Fixed-size records mean any record can be located with header_size + N * record_size, no parsing required to skip ahead. The version field exists so the format can evolve (ports, protocol, IPv6) without breaking old files or requiring a rewrite of the reader.


Dashboard

A small HTTP server (standard net/http, no framework) reads the binary log and exposes it as JSON at /api/flows. A single static HTML page polls that endpoint and renders a sortable table of flows — source, destination, packet count, byte count, first/last seen.


What's implemented

  • Raw packet capture via AF_PACKET + PACKET_MMAP ring buffer, built from raw syscalls
  • Ethernet / IPv4 / TCP header parsing
  • Bidirectional flow key normalization
  • Sharded, mutex-protected flow aggregation
  • Channel-based decoupling between capture and aggregation
  • Idle-flow expiration (Reaper)
  • Custom versioned binary persistence format
  • HTTP dashboard (JSON API + static page)
  • Verified race-free under go run -race after fixing three real data races found during development

What's not (yet)

  • UDP / ARP / IPv6 parsing
  • Protocol field in the on-disk format
  • Incremental (tail) reading of the log instead of full re-reads
  • Live (push-based) dashboard updates instead of polling
  • CPU/throughput profiling with pprof

Building and running

go build -o WonderWall .
sudo ./WonderWall

Raw socket capture requires elevated privileges — either run as root or grant the capability explicitly:

sudo setcap cap_net_raw+ep ./WonderWall
./WonderWall

Then open the dashboard:

http://localhost:8080

Closed flows are appended to data/output.bin as they expire (default idle timeout: 10 seconds, checked every 5 seconds).


Why this exists

This project wasn't built to compete with Wireshark or NetFlow collectors. It was built to actually understand — down to the byte — how a packet gets from a network card into a Go program: what a syscall really is, why AF_PACKET isn't a socket type but a socket domain, why byte order matters, why unsafe.Pointer conversions have to happen inline for the GC's sake, why a mutex protects an operation and not the memory a pointer happens to point to. Every design decision above came out of hitting a real wall — a wrong htons, a leaked pointer past a lock, a poll() call pointed at a slice header instead of its first element — and fixing it by understanding why it broke, not by copying a fix from somewhere else.

About

Lightweight network traffic monitor built from scratch in Go: AF_PACKET + PACKET_MMAP zero-copy capture, sharded flow aggregation, custom binary log, live HTTP dashboard.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages