Sub topic 16
Forwarding versus routing
This chapter assumes you can read prefix notation such as 10.1.0.0/16 and that you know what longest-prefix match does. It also assumes you know what a router is and have seen weighted graphs. Here you will separate two jobs that people often blur together: forwarding a packet and computing the routes that forwarding uses.
Two jobs, two time scales
A router does two different things.
- Forwarding is the per-packet action. The router reads the destination address, finds the best matching entry in its forwarding table, and sends the packet out of the chosen interface. This happens for every packet, so it must take nanoseconds to microseconds. It is usually done in hardware.
- Routing is the process that builds the table. Routers exchange information with one another and run an algorithm to decide which next hop is best for each destination. This happens in the background, over seconds to minutes, and it runs in software.
Forwarding answers "where does this packet go now?". Routing answers "what should the table contain?".
| Aspect | Forwarding | Routing |
|---|---|---|
| Plane | Data plane | Control plane |
| Trigger | Arrival of each packet | Topology change or timer |
| Input | Destination address and forwarding table | Neighbour messages, link costs, configuration |
| Output | Chosen outgoing interface | Routing table and forwarding table |
| Typical speed | Nanoseconds to microseconds | Milliseconds to minutes |
| Where it runs | Line cards, hardware | Route processor, software |
The routing table and the forwarding table are related but not identical. The routing table holds what the routing protocols learned, often including several candidate routes for one prefix. The forwarding table holds only the winning entries, in a form optimized for fast lookup. In exam questions, "routing table" is used for both.
The forwarding step in detail
For each arriving packet the router does the following.
- Check that the header is valid and decrease the TTL by 1. If the TTL reaches 0, discard the packet.
- Recompute the header checksum, since the TTL changed.
- Find the entry with the longest prefix that matches the destination address.
- Send the packet to the next hop through the interface named in that entry.
The packet's source and destination addresses do not change during forwarding, apart from the NAT case covered in other chapters. Only the link-layer frame around the packet is rebuilt at each hop.
import ipaddress
table = [
("10.0.0.0/8", "eth0"),
("10.1.0.0/16", "eth1"),
("0.0.0.0/0", "eth2"),
]
def forward(dst, ttl):
ttl -= 1
if ttl <= 0:
return None, "dropped: TTL expired"
ip = ipaddress.ip_address(dst)
best = None
for prefix, iface in table:
net = ipaddress.ip_network(prefix)
if ip in net and (best is None or net.prefixlen > best[0].prefixlen):
best = (net, iface)
if best is None:
return None, "dropped: no route"
return best[1], f"ttl now {ttl}"
print(forward("10.1.5.5", 64))
print(forward("10.9.9.9", 1))
print(forward("172.16.0.1", 10))The three calls print ('eth1', 'ttl now 63'), (None, 'dropped: TTL expired'), and ('eth2', 'ttl now 9'). The second packet arrives with TTL 1, so the decrement leaves 0 and the router drops it before any lookup.
The routing step in detail
A routing protocol has to solve three problems.
- Discovery: each router learns who its neighbours are and what the links cost.
- Distribution: routers share information, either their distance estimates (distance vector) or the state of their own links (link state).
- Computation: each router turns what it knows into a best next hop per destination.
The result is installed into the forwarding table. If a link fails, routing recomputes and replaces the affected entries. Until it finishes, forwarding keeps using the old entries, so packets can be lost or looped for a short time. This gap is why convergence time matters in the later chapters.
placeholderBecause the diagram below is the one this chapter uses, here it is in the required form.
Where the next hop comes from
Each forwarding entry names a next hop and an interface. Entries come from three sources:
- Directly connected networks, known as soon as an interface is configured.
- Static routes, entered by an administrator.
- Dynamic routes, learned from RIP, OSPF or BGP.
When two sources offer a route to the same prefix, the router prefers the source with the lower administrative distance. When one source offers several routes to the same prefix, its own metric decides. Longest-prefix match is applied after this, across different prefixes. A /24 learned by RIP beats a /16 learned by OSPF for an address inside the /24, because prefix length is compared first and the protocols are never compared against each other for different prefixes.
Common errors
- Treating the TTL as part of routing. The TTL is decremented by forwarding, per packet. Routing never touches it.
- Saying a router "runs Dijkstra on every packet". Dijkstra or Bellman-Ford runs when the topology changes. Packets only cause table lookups.
- Assuming the table lists whole paths. A forwarding table gives only the next hop. Each router on the way makes its own decision, which is why inconsistent tables can cause loops.
- Forgetting the default route. Without a matching entry, even
0.0.0.0/0, the packet is dropped. A router may then send an ICMP "destination unreachable" message to the source.
Exercise
A router receives a packet with TTL 1 and a destination that has a matching entry in its table. State what happens and which of forwarding or routing is responsible for that outcome.
Show answer
The router decrements the TTL to 0 and discards the packet without sending it on. It typically sends an ICMP time-exceeded message to the source. This is forwarding, because it is a per-packet action in the data plane. Routing is not involved.
Exercise
A link fails and the routing protocol needs 30 seconds to converge. During this time a router keeps receiving packets whose best entry points at the failed link. Explain why forwarding still sends them there, and name the part of the router that fixes the problem.
Show answer
Forwarding only follows the table and does not know why an entry exists. Until the routing process recomputes and replaces the entry, the stale entry stays in the table, so packets are sent into the failed link and lost. The control plane (the routing protocol running on the route processor) fixes this by installing a new entry.