Skip to main content
← PROBLEMS / ARCHITECTURE
An end-to-end domain learning journey

Design a Sub-Microsecond Longest-Prefix IP
Routing Table with vEB Trees.

A 32-level radix trie chases 32 pointers per packet, blowing the 25 ns line-rate budget on 40M pps NICs.
Start the journey3 stages
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

Representing prefix lengths /0..32 with a 64-bit hardware bitmask + vEB binary search over prefix lengths (O(log W) = 5 hash probes) or a 2-level DIR-24-8 flat array achieves <20 ns LPM.

TL;DR30-second simplified summary
▾

Use vEB predecessor/binary search on prefix lengths (Waldvogel's scheme: <=5 probes for IPv4) or L1-resident compressed summary bitmasks.

Before you begin · Building blocksAlready know these? Jump to Stage 01 ↓
Van Emde Boas Predecessor Trees
Halving the bit-width at each step for O(log log U) predecessor lookups.
CPU L1/L2 Cache Latency
L1 hit = ~1 ns; DRAM miss = ~70 ns. 40M pps allows only 25 ns total per packet.
Full walkthrough · 3 stages
01
L4 · FUNDAMENTALS

R & E — The 25-Nanosecond Line-Rate Budget.

32-Level Radix Trie vs. vEB Prefix-Length Search

Before · 32-Level Bitwise Trie
After · vEB Prefix-Length / Bitmask LPM

At 40 million packets/sec on a single DPDK core, the entire LPM lookup budget is 25 ns. A single uncached DRAM fetch takes ~70 ns, which means a 32-level bitwise trie that walks 32 pointers can take 500–1,500 ns—dropping 95% of packets.

We must fit the hot decision metadata inside L1/L2 cache (32 KB – 256 KB) and cap worst-case hash/array probes at O(log W) = 5 for 32-bit IPv4 (log2(32) = 5).

02
L4 · FUNDAMENTALS

H & D — Bit-Packed vEB LPM Engine in Python.

Sub-Microsecond LPM Forwarding Pipeline

NIC RX RingvEB Prefix Search(<=5)L1 Summary BitmaskNext-Hop Egressdst IPv4probe /16,/24,/28egress port ID
Engineering Decision Framework
Problem
How should a 40M pps software router perform Longest-Prefix Match across 1M BGP routes?
Naive Solution
Walk a 32-level binary radix trie bit-by-bit.
Why It Fails
Up to 32 dependent pointer chases cause DRAM stalls far exceeding the 25 ns/packet budget.
Alternatives
vEB / Binary Search on Prefix Lengths (O(log W) = 5 probes, scales to IPv6 /128 in 7 probes),DIR-24-8 Flat Table (1-2 array reads for IPv4, 64 MB RAM, fails for IPv6),32-level Radix Trie
Engineering Decision
Use DIR-24-8 for pure IPv4 when 64 MB L3/hugepage RAM is available, and vEB prefix-length search for IPv6 (/128 -> 7 probes) or memory-constrained eBPF maps.
Trade-Offs
vEB prefix-length search requires marker maintenance on BGP route updates, whereas DIR-24-8 requires 64 MB contiguous hugepages.
Decision Exercise · Make the Call

Why does vEB / binary search on prefix lengths scale seamlessly from IPv4 (/32) to IPv6 (/128)?

Waldvogel's binary search on prefix lengths (a direct application of vEB halving on W = 32 bits) stores a hash table for each prefix length L in 1..32, augmented with markers at midpoint lengths so a match at length 16 tells the engine whether a longer /24 prefix might exist:

from typing import Dict, Optional, Tuple

class VEBPrefixRouter:
    """O(log W) = 5-step Longest-Prefix Match for 32-bit IPv4 using prefix-length halving."""
    def __init__(self) -> None:
        self.tables: Dict[int, Dict[int, Tuple[int, bool]]] = {l: {} for l in range(33)}
        self.default_hop: int = 0

    def insert_route(self, ip_int: int, prefix_len: int, next_hop: int) -> None:
        masked = ip_int & (((1 << prefix_len) - 1) << (32 - prefix_len)) if prefix_len > 0 else 0
        self.tables[prefix_len][masked] = (next_hop, True)
        # Populate binary-search markers on ancestor lengths (16, 24, 28, ...)
        lo, hi = 0, 32
        while lo < hi:
            mid = (lo + hi + 1) // 2
            if prefix_len > mid:
                m_key = ip_int & (((1 << mid) - 1) << (32 - mid))
                if m_key not in self.tables[mid]:
                    self.tables[mid][m_key] = (self.default_hop, False)
                lo = mid
            else:
                hi = mid - 1

    def lookup(self, ip_int: int) -> int:
        best_hop = self.default_hop
        lo, hi = 0, 32
        while lo < hi:
            mid = (lo + hi + 1) // 2
            key = ip_int & (((1 << mid) - 1) << (32 - mid))
            entry = self.tables[mid].get(key)
            if entry is not None:
                hop, is_real = entry
                if is_real:
                    best_hop = hop
                lo = mid
            else:
                hi = mid - 1
        return best_hop
03
L5 · APPLIED DESIGN

D — Lock-Free RCU BGP Route Updates.

RCU Route Table Swap

BGP UPDATE receivedMarkers computedEpoch N+1 activeLOCKFREE_FWD_EPOC…BUILD_SHADOW_MARK…ATOMIC_PTR_SWAP

BGP churn can inject 10,000 route updates/sec. Forwarding cores read the active tables snapshot lock-free via Read-Copy-Update (RCU): a control-plane thread builds the new marker diff on a shadow copy and atomically swaps the pointer between packet bursts.

Recommended Learning Flow