Sub topic 21
OSPF: areas, flooding and cost
This chapter assumes you know how link-state routing works. That means each router learns the whole topology and runs Dijkstra's algorithm on it. You should also be comfortable with weighted graphs and with CIDR prefixes. OSPF (Open Shortest Path First) is the main link-state protocol used inside one organisation's network. This chapter covers how it divides a network into areas, how it floods link information, and how it computes cost.
Where OSPF fits
OSPF is an interior gateway protocol (IGP). It runs inside a single autonomous system (AS), which is a network under one administrative control. It is the link-state counterpart of RIP. RIP counts hops and uses distance-vector updates. OSPF uses a configurable cost and a full topology map.
OSPF runs directly over IP, as IP protocol number 89. It does not use TCP or UDP.
| Property | RIP | OSPF |
|---|---|---|
| Algorithm | Distance vector (Bellman-Ford) | Link state (Dijkstra) |
| Metric | Hop count, with 16 as infinity | Cost, derived from link bandwidth |
| Information shared | Whole routing table, to neighbours | Link states, to every router in the area |
| Convergence | Slow, can count to infinity | Fast |
| Scale | Small networks | Large networks, through areas |
The cost metric
Each OSPF interface has a cost. The path cost is the sum of the interface costs along the path. By default, cost is inversely proportional to bandwidth:
cost = reference bandwidth / interface bandwidth
A common default reference bandwidth is 100 Mbps. A 10 Mbps link then has cost 10, and a 100 Mbps link has cost 1. Costs are integers, and the result is never below 1. This causes a known problem. With a 100 Mbps reference, a 1 Gbps link and a 100 Mbps link both get cost 1, so OSPF cannot tell them apart. Administrators fix this by raising the reference bandwidth or by setting costs by hand.
Because costs are additive, the best route is not always the one with the fewest hops. A path of three fast links can beat a single slow link.
Flooding link-state advertisements
OSPF routers describe their links in link-state advertisements (LSAs). Each router floods its LSAs to every other router in the area. The LSAs are stored in the link-state database (LSDB). Every router in an area ends up with an identical LSDB, and then runs Dijkstra on it with itself as the source.
The flooding process works like this:
- A router detects a change, such as a link going down.
- It sends an updated LSA, with a higher sequence number, out of all its interfaces.
- A neighbour that receives a newer LSA stores it and forwards it out of every interface except the one it arrived on.
- Neighbours acknowledge LSAs. An LSA with an older or equal sequence number is not forwarded again, so flooding stops.
Sequence numbers prevent loops. Each LSA also has an age. LSAs are refreshed periodically, about every 30 minutes, and are removed from the database when they expire.
OSPF also sends small Hello packets to neighbours at regular intervals. A neighbour that stops answering is declared dead. This is how a router detects link failures quickly.
Designated routers on broadcast networks
On a shared segment such as Ethernet, every router forming adjacencies with every other one would create many redundant exchanges. OSPF elects a designated router (DR) and a backup designated router (BDR). Other routers exchange LSAs only with the DR and BDR, and the DR floods the information onward.
Areas
Flooding every change to every router does not scale. The LSDB grows, Dijkstra takes longer, and one unstable link triggers recomputation everywhere. OSPF solves this by dividing an AS into areas.
- Each area has its own LSDB. Routers inside an area know the detailed topology only of that area.
- Area 0 is the backbone. Every other area must connect to it.
- An area border router (ABR) connects an area to the backbone. It holds one LSDB per area it belongs to.
- ABRs summarise an area's prefixes before advertising them to other areas. This is route aggregation, as covered earlier with CIDR.
- An AS boundary router (ASBR) connects the OSPF domain to other networks and imports external routes.
Areas limit the effect of failures. If a link in Area 1 flaps, only routers in Area 1 rerun Dijkstra. Routers in other areas see at most a change in a summarised route.
The cost of this design is that paths between areas always pass through the backbone. The route found may not be the shortest one possible in the whole network.
Computing routes: a worked example
Each router runs Dijkstra on its LSDB. This code builds the same graph used in the link-state chapter and finds the next hop from a source for each destination, which is what ends up in a forwarding table.
import heapq
def ospf_routes(graph, source):
dist = {n: float("inf") for n in graph}
first_hop = {n: None for n in graph}
dist[source] = 0
heap = [(0, source)]
while heap:
d, u = heapq.heappop(heap)
if d > dist[u]:
continue
for v, w in graph[u].items():
if d + w < dist[v]:
dist[v] = d + w
first_hop[v] = v if u == source else first_hop[u]
heapq.heappush(heap, (dist[v], v))
return {n: (dist[n], first_hop[n]) for n in graph if n != source}
graph = {
"A": {"B": 2, "C": 5},
"B": {"A": 2, "C": 1, "D": 4},
"C": {"A": 5, "B": 1, "D": 1},
"D": {"B": 4, "C": 1},
}
for dest, (cost, hop) in sorted(ospf_routes(graph, "A").items()):
print(dest, cost, hop)The output is:
B 2 B
C 3 B
D 4 BRouter A reaches C at cost 3 through B, not directly at cost 5. Every route goes through B as the first hop.
Common errors and exam traps
- OSPF is not a distance-vector protocol. Routers exchange link states, not routing tables, and all routers in an area compute routes themselves.
- The metric is cost, not hop count. Add interface costs along the path.
- Cost is computed from bandwidth, so faster links have lower cost. A higher bandwidth never means a higher cost.
- Flooding is limited to an area. LSAs for internal links do not cross an ABR. Only summaries do.
- Area 0 must exist, and other areas must attach to it, directly or through a virtual link.
- OSPF avoids count-to-infinity because each router sees the full topology and recomputes from it. It does not need split horizon.
Exercise
An OSPF network uses a reference bandwidth of 100 Mbps. A router has a path to a destination over three links of 10 Mbps, 100 Mbps and 100 Mbps, and a second path over a single 10 Mbps link. Which path does OSPF choose, and why?
Show answer
The first path costs 10 + 1 + 1 = 12. The second costs 10. OSPF chooses the single 10 Mbps link, because it has the lower total cost. The path with more links loses because its summed cost is higher.
Exercise
Link-state routers flood LSAs. A link inside Area 1 goes down. Which routers rerun Dijkstra, and why?
Show answer
Only the routers in Area 1. LSAs describing internal links are flooded within the area and are not passed through the ABR into other areas. Other areas may see a change in the summary route, but they do not recompute from the failed link.