Sub topic 1
Link-State Routing: Every Router Holds the Map
Prerequisites
This chapter assumes you already know the following:
- IPv4 addressing and subnet masks.
- Basic router interface configuration.
- The difference between static and dynamic routing.
- How a routing table works, including longest-prefix match.
- How Ethernet and point-to-point links behave.
You do not need any OSPF knowledge. This is the first chapter, and it builds the model that the rest of the tutorial relies on.
The core idea
A link-state routing protocol gives every router in a routing domain the same map of the network. Each router describes its own links and shares that description with all other routers. Every router then collects the descriptions into one database and computes its own best paths from it.
Three steps repeat for the life of the network:
- Describe. A router lists its directly connected links, their states, and their costs.
- Flood. The router sends that description to every other router in the domain. Each receiving router forwards it onward unchanged.
- Compute. Each router runs a shortest-path algorithm over the shared map, with itself as the root.
Because all routers hold the same map, they reach consistent answers. Loops are very unlikely in a stable network.
Link-state versus distance-vector
A distance-vector protocol such as RIP shares its routing table with neighbors only. Each router learns a distance and a next hop, but never sees the topology. This is often called "routing by rumor". A router trusts what its neighbor says without knowing how the neighbor learned it.
| Property | Distance-vector (RIP) | Link-state (OSPF) |
|---|---|---|
| What is shared | Routes (prefix and metric) | Links and their states |
| Who receives it | Direct neighbors only | Every router in the area |
| View of the network | Next hop and distance only | Full topology map |
| Path calculation | Bellman-Ford, spread across routers | Dijkstra SPF, run locally |
| Convergence | Slower, can count to infinity | Faster, loop-free in steady state |
| Resource use | Low CPU and memory | More CPU and memory |
The cost of the link-state approach is resources. Each router stores the whole map and reruns the calculation when the map changes. This is the main reason OSPF divides large networks into areas, which a later chapter covers.
The link-state database
The map each router holds is called the link-state database (LSDB). It is built from link-state advertisements (LSAs), which are small records that each router originates to describe its own links. The LSDB is the same on every router in an area. Routes are not the same on every router, because each router computes them with itself as the root.
This separation matters when you troubleshoot. If two routers in one area have different LSDB contents, something is wrong with flooding or adjacency. If the LSDBs match but the routes differ, that is normal.
Running the shortest-path calculation
Each router runs Dijkstra's shortest path first (SPF) algorithm on the LSDB. The algorithm starts at the router itself with distance 0. It repeatedly picks the unvisited node with the lowest known total cost, then checks whether going through that node gives a cheaper path to each of its neighbors. It stops when every node has a final cost.
The following Python code does this for a three-router topology. The direct link between R1 and R2 costs 10. The path R1 to R3 to R2 costs 1 + 1 = 2, so SPF prefers it.
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}The if d > dist.get(u, ...) check skips stale heap entries. Without it, the code still gives correct answers but does extra work. The output shows that R1 reaches R2 at total cost 2 through R3, not at cost 10 over the direct link.
Real OSPF does the same thing with extra detail. Costs are assigned per outgoing interface. Networks such as Ethernet segments appear as nodes too. Ties between equal-cost paths are kept, so traffic can be split across them. Later chapters cover these points.
How a change propagates
The sequence below shows what happens when a link fails. Only the affected description is flooded, not the whole table.
The steps are:
- The router next to the failure notices it, for example through a lost Hello or a down interface.
- That router originates a new version of its LSA with a higher sequence number.
- Neighbors accept the newer version, store it, and forward it.
- Every router reruns SPF over the changed LSDB and updates its routing table.
Each LSA carries a sequence number and an age. Routers use these to tell newer information from older information and to discard duplicates. A later chapter covers this in detail.
Common mistakes
- Treating the LSDB as a routing table. The LSDB is the map. The routing table is the result of running SPF over it.
- Expecting identical routing tables. Routers share a map but each one computes routes from its own position.
- Assuming flooding is free. Flooding cost grows with network size and instability. A flapping link makes every router recompute repeatedly.
- Confusing metric with hop count. OSPF adds up link costs. A path with fewer hops can be more expensive than one with more hops, as the R1 to R2 example shows.
What comes next
The next chapter introduces OSPF itself and its five packet types. They are the messages routers use to carry out the describe, flood, and compute cycle described here.
Exercise
In the Python example, R1 reaches R2 at cost 2 instead of 10. If the cost of the R1-R3 link were raised to 9, which path would R1 choose to reach R2, and what would the total cost be?
Show answer
The path through R3 would cost 9 + 1 = 10, which ties with the direct link of cost 10. SPF would then treat the two paths as equal cost and could use both. Any R1-R3 cost above 9 would make the direct link the only best path.
Exercise
Two routers in the same OSPF area have identical LSDBs but different routing tables. Is this a problem? Explain.
Show answer
No. Each router runs SPF with itself as the root, so the same map produces different next hops and costs on different routers. A problem would exist only if the LSDBs differed.