Sub topic 11
Link-state routing: everyone holds the map
This chapter assumes you know basic IP addressing, the difference between a router and a switch, how longest-prefix match picks a route, and the graph terms node, edge, weight and shortest path. You should also know that distance-vector routers exchange cost tables with their neighbors, as covered in the earlier chapters.
The core idea
A distance-vector router knows only what its neighbors tell it: "I can reach X at cost 5." It never sees the network itself, so it cannot check whether a neighbor's claim is true. That is why it can be fooled into loops.
A link-state router takes the opposite approach. Each router describes only what it knows first-hand: its own links, their costs, and the neighbors on the other end. It sends that description to every router in the network. Every router then collects the same set of descriptions, builds the same map, and computes its own routes from that map.
The slogan is "tell everyone about your neighbors". Distance-vector says "tell your neighbors about everyone".
The four steps
Every link-state protocol, including OSPF and IS-IS, follows the same cycle:
- Discover neighbors. Each router sends small hello packets on its links to find out who is directly attached and whether the link works.
- Advertise local links. Each router builds a link-state advertisement (LSA in OSPF, LSP in IS-IS). It lists the router's identity, its neighbors, and the cost to each.
- Flood. The advertisement is copied to every router in the flooding scope, not just to neighbors that need it.
- Compute. Each router stores all advertisements in a link-state database and runs a shortest-path algorithm, Dijkstra's, with itself as the root.
Later chapters cover steps 1, 2 and 3 in detail, the database, and Dijkstra's algorithm step by step. Here the goal is to see why the design works.
Why identical databases matter
The design depends on one property: all routers in the same flooding scope hold the same database. If they do, each one runs the same deterministic algorithm on the same input. Their routing decisions are then consistent with each other, because each router computes its own piece of one shared answer.
Compare this with distance-vector. A router there trusts a number it cannot verify, and stale numbers can circulate between routers. In link-state, a router never accepts a neighbor's conclusions. It accepts only facts about links, which are signed by their originator through sequence numbers and checksums, and it draws the conclusions itself. A forwarding loop can still exist briefly while databases differ during a change, but there is no equivalent of count to infinity, because no router is counting upward from a rumor.
Each router, same map, different tree
Identical databases do not mean identical routing tables. Each router runs the shortest-path algorithm from its own position, so it gets a different shortest-path tree. What it keeps from the tree is the first hop toward each destination, which is the entry that goes into its forwarding table.
This Python program uses the spf function from the topic's design. It builds one shared graph and computes the table from every router's point of view.
import heapq
def spf(graph, source):
"""graph: node -> {neighbor: cost}. Returns (distance, first_hop) per destination."""
dist = {source: 0}
first_hop = {}
done = set()
heap = [(0, source, None)]
while heap:
d, node, hop = heapq.heappop(heap)
if node in done:
continue
done.add(node)
if node != source:
first_hop[node] = hop
for nbr, cost in graph[node].items():
nd = d + cost
if nbr not in done and nd < dist.get(nbr, float("inf")):
dist[nbr] = nd
heapq.heappush(heap, (nd, nbr, nbr if node == source else hop))
return dist, first_hop
links = [("A", "B", 1), ("A", "C", 4), ("B", "C", 2), ("B", "D", 5), ("C", "D", 1)]
# The shared link-state database: every router holds this same graph.
graph = {}
for a, b, cost in links:
graph.setdefault(a, {})[b] = cost
graph.setdefault(b, {})[a] = cost
for router in sorted(graph):
dist, first_hop = spf(graph, router)
for dest in sorted(first_hop):
print(f"{router}: {dest} cost {dist[dest]} via {first_hop[dest]}")The output is:
A: B cost 1 via B
A: C cost 3 via B
A: D cost 4 via B
B: A cost 1 via A
B: C cost 2 via C
B: D cost 3 via C
C: A cost 3 via B
C: B cost 2 via B
C: D cost 1 via D
D: A cost 4 via C
D: B cost 3 via C
D: C cost 1 via CRouter A reaches C through B because 1 + 2 = 3 beats the direct link of cost 4. The path A to D goes A, B, C, D with total cost 4. Router D sends traffic to A through C, which is consistent with A's choice, since the path is the same one in reverse.
Costs of the approach
Link-state routing gains accuracy and fast reaction, and it pays for them in several ways:
| Resource | Cost |
|---|---|
| Memory | Every router stores the whole topology, not only a table of destinations. |
| CPU | A topology change triggers a new shortest-path run. Large, unstable networks can spend significant CPU on this. |
| Bandwidth | Each change is flooded to every router. Hello packets run continuously. |
| Design effort | Large networks must be split into areas or levels to limit the flooding scope. |
Because of these costs, link-state protocols give you tools to contain the load. OSPF uses areas and IS-IS uses levels. Both are covered in later chapters.
Common errors and misconceptions
- "Link-state routers exchange routing tables." They do not. They exchange descriptions of links. Routing tables are computed locally and never sent.
- "Flooding means every router sends everything every time." Flooding forwards an advertisement only if it is new, judged by sequence number, and the router does not send it back out the interface it arrived on. Periodic refreshes happen only on a long timer.
- "All routers have the same routing table." They have the same database and different tables.
- Mismatched databases. If two routers disagree because of a lost advertisement, a misconfigured area, or a flapping link, routing decisions can be inconsistent and traffic can loop until the databases agree again. Checking that databases match is a standard troubleshooting step.
Exercise
In the example above, the link B-C is cut. Using the graph, what are Router A's new cost and first hop to D, and which routers must recompute their tables?
Show answer
With B-C removed, the paths from A to D are A-B-D with cost 1 + 5 = 6, and A-C-D with cost 4 + 1 = 5. The best is A-C-D, so A's cost to D is 5 and its first hop is C (previously cost 4 via B). The routers on either end of the cut link, B and C, flood updated advertisements. Every router in the flooding scope, including A and D, receives them, updates its database, and reruns the shortest-path computation, because each one needs the new map even if its own table does not change.