Sub topic 9
Cost and the SPF Calculation
This chapter assumes you know IPv4 addressing, how a routing table uses longest-prefix match, and what a link-state database (LSDB) is. By now every router in an area holds the same LSDB. The next step is turning that shared map into routes.
What cost means
OSPF assigns each interface a cost. A lower cost is better. The cost of a path is the sum of the outgoing interface costs along it. By default, Cisco IOS computes interface cost as:
cost = reference bandwidth / interface bandwidth
The default reference bandwidth is 100 Mbps. The result is rounded down, with a minimum of 1.
| Link speed | Cost with 100 Mbps reference | Cost with 10 Gbps reference |
|---|---|---|
| 10 Mbps | 10 | 1000 |
| 100 Mbps | 1 | 100 |
| 1 Gbps | 1 | 10 |
| 10 Gbps | 1 | 1 |
You can also set a cost by hand on an interface with ip ospf cost 20. A manual cost overrides the calculated one.
How SPF runs
Each router runs the shortest path first (SPF) algorithm, which is Dijkstra's algorithm, with itself as the root. It works from the LSDB graph:
- Put the root in the tree at distance 0.
- Look at every link from the newest tree member and compute the total cost to each neighbor.
- Keep the lowest-cost candidate seen so far for each router.
- Move the cheapest unfinished candidate into the tree and repeat until none remain.
Each router runs SPF on its own, yet all of them reach consistent results. They start from identical LSDBs and apply the same rules, so the paths are loop-free.
SPF in code
This Python version shows the same logic on the triangle above.
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 heap always yields the cheapest unfinished router. The continue line skips stale entries that a cheaper path has already replaced.
Common errors
- Mismatched reference bandwidth. If routers use different references, they disagree about link costs. Routing can still work, but paths may be suboptimal or asymmetric.
- Forgetting cost is outgoing. Cost counts on the interface that sends traffic out. The two ends of a link may have different costs.
- Assuming equal cost means one path. If two paths have the same total cost, OSPF installs both and load-balances across them, up to the configured maximum.
- Expecting bandwidth statements to be read live. The
bandwidthinterface command changes the calculated cost, but it does not change the real link speed.
Exercise
A router has a Gigabit Ethernet link and a Fast Ethernet link to the same destination. The reference bandwidth is left at the default. Which path does OSPF prefer, and what fixes it?
Show answer
Both links get cost 1, so OSPF sees them as equal and may load-balance across them, which sends traffic over the slower link too. Set auto-cost reference-bandwidth 10000 on every router. The Gigabit link then costs 10 and the Fast Ethernet link costs 100, so OSPF prefers Gigabit.
Exercise
In the topology above, R2's link to R3 is changed to cost 20. What is R1's best path cost to R2, and by which path?
Show answer
The path through R3 now costs 1 + 20 = 21, and the direct link costs 10. R1 prefers the direct link at cost 10.