Sub topic 1
Link-State Routing: Every Router Holds the Map
Prerequisites
This chapter assumes you already know:
- IPv4 addressing and subnet masks.
- How a routing table works and how longest-prefix match picks a route.
- The difference between static and dynamic routing.
- Basic point-to-point and Ethernet link behavior.
The Working Model
A link-state routing protocol gives every router in a routing domain the same detailed map of the network. Each router describes its own links and their costs, and floods that description to all other routers. Every router stores the descriptions it receives in a database. It then calculates its own best paths from that database.
A distance-vector protocol works differently. A router tells its neighbors what it can reach and how far away it is. Each neighbor trusts that summary and passes it on. No router sees the whole topology, so routes are learned secondhand.
| Feature | Distance vector | Link state |
|---|---|---|
| What a router shares | Its routing table | Its own links and costs |
| View of the network | Neighbors' summaries | Full topology map |
| Path calculation | Incremental, hop by hop | Each router runs SPF on its own |
| Convergence | Usually slower | Usually faster |
| Memory and CPU use | Lower | Higher |
OSPF is a link-state protocol. Its path calculation is the Shortest Path First (SPF) algorithm, also called Dijkstra's algorithm.
The Three Steps
- Discover neighbors. Routers on the same link find each other and agree to exchange information.
- Flood and synchronize. Each router advertises its links. Every router in the area ends up with an identical database.
- Compute routes. Each router runs SPF with itself as the root and installs the best paths in its routing table.
SPF in Code
SPF finds the lowest total cost from the root router to every other router. This Python version uses a priority queue. The topology has a direct R1-R2 link with cost 10 and a two-hop path through R3 with total cost 2.
import heapq
def spf(graph, root):
dist = {root: 0}
heap = [(0, root)]
while heap:
d, u = heapq.heappop(heap)
if d > dist.get(u, float('inf')):
continue
for v, cost in graph[u].items():
nd = d + cost
if nd < dist.get(v, float('inf')):
dist[v] = nd
heapq.heappush(heap, (nd, v))
return dist
topology = {
'R1': {'R2': 10, 'R3': 1},
'R2': {'R1': 10, 'R3': 1},
'R3': {'R1': 1, 'R2': 1},
}
print(spf(topology, 'R1')) # {'R1': 0, 'R3': 1, 'R2': 2}R1 prefers the path through R3 to reach R2, because 1 + 1 is less than 10. A distance-vector protocol that counted hops would choose the direct link instead.
Common Errors
- Assuming the lowest hop count wins. Link-state protocols compare total cost, not hops.
- Expecting databases to differ. Within one area, every router must hold the same database. If they differ, routing can loop or fail.
- Ignoring the load. Every router runs SPF and stores the full map. Large flat networks consume memory and CPU, which is why OSPF later introduces areas.
Exercise
R1 has a direct link to R2 with cost 10, and a path through R3 whose two links cost 1 each. Which path does R1 use to reach R2, and why does this show a difference from hop-count routing?
Show answer
R1 uses the path through R3, with total cost 2. SPF selects the lowest total cost, not the fewest hops, so it avoids the direct one-hop link that costs 10.