Sub topic 15
The FIB and Fast Forwarding in Hardware
This chapter assumes you know longest prefix match, next hops, the Layer 2 rewrite, and the split between the control plane and the data plane. It explains how a router turns the routing table into a structure built for per-packet speed, and what goes wrong when that structure and the table disagree.
Why the routing table is not used for forwarding
The routing table, often called the RIB (Routing Information Base), holds every route the router has learned. It records which protocol supplied each route, the metric, the administrative distance, and backup candidates. That is useful for decisions, but it is slow to search.
A router that forwards millions of packets per second cannot compare each destination against a list of routes. It uses a second structure, the FIB (Forwarding Information Base). The FIB contains only the winning route for each prefix, reduced to what the data plane needs: the prefix, the outgoing interface, and the next hop.
| Property | RIB | FIB |
|---|---|---|
| Lives in | Control plane, CPU memory | Data plane, often line card memory or ASIC |
| Contents | All candidate routes with metrics and sources | One best entry per prefix |
| Optimized for | Route selection and protocol updates | Fast lookup per packet |
| Changed by | Routing protocols, static routes, interface events | The control plane, which pushes updates to it |
On Linux, the kernel's routing table plays the FIB role. Routing daemons such as FRR keep their own RIB and install only the selected routes into the kernel.
From RIB to FIB
When a route changes, the control plane reruns route selection. If the winning route for a prefix changes, it sends an update to the FIB. If no route wins, it removes the entry.
Most FIB entries do not store the next hop directly. They point to an adjacency, which holds the next hop's resolved Layer 2 rewrite information: the next hop's MAC address and the egress interface. Many prefixes can share one adjacency. When ARP or neighbor discovery learns a new MAC address, the router changes one adjacency instead of thousands of prefixes.
Data structures for fast lookup
Longest prefix match needs a structure that finds the most specific prefix without scanning every entry. Software routers commonly use a binary trie (a prefix tree). Each bit of the destination address picks a left or right branch, and the lookup remembers the last node that carried a route.
import ipaddress
class Node:
__slots__ = ("children", "next_hop")
def __init__(self):
self.children = [None, None]
self.next_hop = None
class Fib:
def __init__(self):
self.root = Node()
def insert(self, prefix, next_hop):
net = ipaddress.ip_network(prefix)
bits = int(net.network_address)
node = self.root
for i in range(net.prefixlen):
bit = (bits >> (31 - i)) & 1
if node.children[bit] is None:
node.children[bit] = Node()
node = node.children[bit]
node.next_hop = next_hop
def lookup(self, destination):
addr = int(ipaddress.ip_address(destination))
node = self.root
best = node.next_hop # the default route, if one exists
for i in range(32):
bit = (addr >> (31 - i)) & 1
node = node.children[bit]
if node is None:
break
if node.next_hop is not None:
best = node.next_hop
return best
fib = Fib()
fib.insert("0.0.0.0/0", "192.0.2.1")
fib.insert("10.0.0.0/8", "192.0.2.2")
fib.insert("10.1.0.0/16", "192.0.2.3")
print(fib.lookup("10.1.2.3")) # 192.0.2.3
print(fib.lookup("10.9.9.9")) # 192.0.2.2
print(fib.lookup("8.8.8.8")) # 192.0.2.1This sketch handles IPv4 only. Its cost per lookup is at most 32 steps no matter how many routes it holds. The linear scan from earlier chapters grows with the table size. The trie's cost depends on the address length.
Production software uses compressed variants such as Linux's LC-trie, which skip several bits per step. They do this because each step is a memory access, and memory latency dominates the cost.
Hardware forwarding
High-end routers and switches do not run the lookup on a general-purpose CPU. They use dedicated forwarding hardware.
- ASIC or network processor. A chip with a fixed or programmable pipeline parses the headers, looks up the route, decrements the TTL, rewrites the frame, and queues it for the egress port. All of this happens at line rate.
- TCAM. Ternary Content-Addressable Memory compares the destination against all stored prefixes at once. Each bit can be 0, 1, or "don't care", and a /16 prefix stores the last 16 bits as "don't care". Entries are ordered by prefix length, so the first hit is the longest match. A lookup takes one memory cycle, but TCAM is expensive and power-hungry, and its capacity is fixed.
- Algorithmic memory. Other chips store tries or hash tables in ordinary SRAM or DRAM and use several pipelined lookups. They hold more routes at lower cost, but the design is more complex.
- Line card copies. In a modular router, each line card holds its own copy of the FIB. A packet is looked up on the ingress card, then crosses the switch fabric to the egress card. The central CPU is not in the path.
The CPU still takes part. It runs the routing protocols, builds the RIB, and programs the hardware tables.
The slow path and exceptions
Most packets take the fast path, which stays in hardware. Some packets need the CPU, the slow path:
- Packets addressed to the router itself, such as routing protocol messages, SSH, or pings.
- Packets whose TTL is 1 or less, which need an ICMP Time Exceeded message built.
- Packets that need fragmenting, or that need an ICMP "fragmentation needed" reply.
- Packets with IP options.
- The first packets to a next hop whose MAC address is not yet resolved.
The slow path has far less capacity than the fast path. A flood of such packets can overload the CPU even when the link is nearly idle. Routers therefore rate-limit traffic to the CPU. This is why a router may answer traceroute probes inconsistently, or show high latency to a ping while forwarding traffic normally. The ping is handled on the slow path, but the transit traffic is not.
Common errors and gotchas
- RIB and FIB disagree. A route can appear in the RIB but be missing from the FIB. This happens when the next hop is unresolvable, or when the hardware table is full. Always check the table that forwarding uses, not only the one routing protocols report.
- TCAM exhaustion. A full internet routing table has several hundred thousand IPv4 prefixes. A device sized for fewer will drop the extra routes, or fall back to the slow path, depending on the platform. Summarizing routes or filtering them reduces the load.
- Stale adjacencies. If a neighbor's MAC address changes and the adjacency is not refreshed, packets are rewritten to the wrong address and are lost until the entry expires.
- Load balancing. When several equal routes exist, hardware picks one by hashing packet header fields. The hash keeps each flow on one path, so packets of one flow are not reordered. It does not guarantee an even split across paths.
- Convergence gaps. The FIB updates after the control plane finishes. During that interval, packets can follow a stale route or be dropped. This is a normal part of routing changes and not a bug.
On Linux you can inspect what the kernel will do for a given packet:
# Show the route the kernel selects for a destination
ip route get 10.1.2.3
# Show how many routes are installed
ip route show | wc -l
# Show resolved next-hop MAC addresses (the adjacency data)
ip neigh showExercise
A router's RIB shows a BGP route to 203.0.113.0/24, but traffic to that prefix is dropped. The route's next hop has no ARP entry and does not answer. Why can the route be in the RIB but not forward traffic, and where would you look?
Show answer
The FIB entry points to an adjacency, and the adjacency needs the next hop's MAC address. If ARP or neighbor discovery fails, the adjacency is incomplete, so the router cannot build the Layer 2 header and drops the packet, even though the RIB holds a valid route. Check the neighbor table (for example, ip neigh show) and the FIB entry for the prefix, and verify that the next hop is reachable on the connected network.
Exercise
Why does a router rate-limit packets sent to its CPU, and what symptom might you see if it did not?
Show answer
The CPU slow path handles traffic such as packets addressed to the router, TTL expiries, and packets with options. It has far less capacity than the hardware fast path. Without a limit, a flood of such packets could starve routing protocol processing, causing neighbors to time out and routes to be withdrawn, even while the link carries little traffic.