Sub topic 9
Cost and the SPF Calculation
This chapter assumes you know how routers build link-state databases (LSDBs) and flood LSAs, and that you understand routing tables and longest-prefix match. It covers how OSPF turns the LSDB into routes. The two parts are the cost metric attached to each interface and the shortest path first (SPF) algorithm that every router runs on its own.
Cost: the OSPF metric
OSPF cost is a dimensionless number assigned to each interface. A lower number is better. By default a router derives it from bandwidth:
cost = reference bandwidth / interface bandwidthThe result is rounded down, with a minimum of 1. Cisco IOS uses a default reference bandwidth of 100 Mbps. Under that default, FastEthernet, Gigabit, and 10 Gigabit links all get cost 1, so OSPF cannot tell them apart.
| Link speed | Cost with 100 Mbps reference | Cost with 10000 Mbps reference |
|---|---|---|
| 10 Mbps | 10 | 1000 |
| 100 Mbps | 1 | 100 |
| 1 Gbps | 1 | 10 |
| 10 Gbps | 1 | 1 |
You fix this with auto-cost reference-bandwidth 10000, which the configuration chapter's example uses. You can also override one interface with ip ospf cost N. A manual cost wins over the computed value.
Three properties matter in practice:
- Cost applies to the outgoing direction. A router adds the cost of the interface it sends traffic out of. The two ends of a link can have different costs, which makes paths asymmetric.
- Path cost is a sum. The cost to a destination is the sum of the outgoing interface costs along the path, plus the cost of the last link to the destination network.
- The reference bandwidth must match on every router. A mismatch does not break adjacencies. It silently skews path selection, so routers may disagree about which path is best.
How a router runs SPF
Each router builds a shortest-path tree with itself as the root. Dijkstra's algorithm is the standard method:
- Put the root in a candidate list with distance 0.
- Take the candidate with the lowest total distance and mark it final.
- For each neighbor of that router, compute distance-so-far plus link cost. If this beats the neighbor's known distance, record it and remember which neighbor to send traffic through (the next hop).
- Repeat until the candidate list is empty.
The input graph comes from the LSDB. Router LSAs (Type 1) describe point-to-point links and the router's connections to transit networks. Network LSAs (Type 2) represent a multi-access segment as a pseudonode, so a broadcast segment appears as a node that routers connect to.
After the tree is built, the router attaches stub networks (leaf prefixes) to it. Each prefix's cost is the cost to reach the advertising router plus the prefix's own interface cost. The best results go into the routing table.
The algorithm in code
This Python version models the same computation on a three-router graph. The direct R1–R2 link costs 10, but the path through R3 costs 1 + 1 = 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, 'R2': 2, 'R3': 1}The if d > dist.get(...) check discards stale heap entries. R2 is first pushed with distance 10, then improved to 2 once R3 is processed. The old entry is skipped when it pops.
A real router also records the first hop for each destination. Here R1 reaches R2 through R3, so R1 installs a route to R2's networks with R3 as the next hop and ignores the direct link.
Worked example: a tie
Suppose the reference bandwidth is 10000 on all routers. R1 connects to R2 and R3 over gigabit links, and R2 connects to R3 over gigabit. On R1, the link to R3 has a manual cost of 20, as in the earlier configuration.
R1's cost to R3's loopback is 20 by either path. OSPF installs both next hops and splits traffic between them. This is equal-cost multipath (ECMP). Many platforms limit the number of parallel paths, often to 4 by default on IOS, and a tunable maximum controls it.
If you lower the manual cost to 15, the direct link wins and R2 is no longer used for that destination.
Triggers and cost of running SPF
SPF reruns when the LSDB topology changes. A new, removed, or changed Router or Network LSA triggers a full SPF. A change that only affects a leaf prefix, such as an inter-area (Type 3) route or an external route, usually triggers only a partial calculation on many implementations.
In a large area, repeated link flaps can drive the CPU up. Routers use SPF throttle timers (an initial delay, then growing hold times) to batch changes. Areas, covered next, limit how many routers share one LSDB and therefore how often each one runs SPF.
Common errors
- Assuming bandwidth setting changes the physical speed.
bandwidthonly changes a value used by cost calculation (and QoS). It does not change the link. - Mixed reference bandwidths. One router at 100 and another at 10000 produces inconsistent best paths and can cause suboptimal routing.
- Expecting all gigabit and faster links to differ. Without raising the reference, they tie at cost 1.
- Reading only the neighbor's cost. The metric shown for a route is cumulative from the local router, so compare it against the sum along the path.
Exercise
The reference bandwidth is 10000 Mbps. R1 has a 1 Gbps link to R2 and a 100 Mbps link to R3, and R2 and R3 are connected by a 1 Gbps link. What is R1's cost to R3 through each path, and which does it choose?
Show answer
The R1–R3 link costs 10000/100 = 100, so the direct path costs 100. The R1–R2 link costs 10000/1000 = 10 and the R2–R3 link costs 10, so the path through R2 costs 20. R1 chooses the path through R2 because 20 is lower than 100.
Exercise
Why does OSPF with the default 100 Mbps reference bandwidth fail to prefer a 10 Gbps link over a 100 Mbps link, and what is the fix?
Show answer
Both links compute to cost 1, because the result is rounded down with a minimum of 1. Raise the reference bandwidth with auto-cost reference-bandwidth (for example 10000) on every router, or set a manual cost with ip ospf cost on each interface.