OSPF Cost Calculation and Path Selection
OSPF selects shortest paths using interface cost. Understanding cost helps explain equal-cost load balancing, reference bandwidth problems and why a higher-bandwidth link should normally win.
OSPF Cost at a Glance
OSPF adds the outgoing interface costs along a path. The path with the lowest total cost wins. If two paths have the same total cost, OSPF can install both and share traffic across them.
In This Lesson
1. Understand the Cost Formula
On Cisco IOS, the automatic interface cost comes from two values: the OSPF reference bandwidth and the interface bandwidth. Both values must use the same unit before you divide them.
Cisco IOS uses an integer cost and does not use an automatic interface cost below 1. Lower cost is better.
Worked example with the default reference
The default reference bandwidth is 100 Mbps. A 10 Mbps link has a cost of 10 because 100 ÷ 10 = 10. A 100 Mbps link has a cost of 1 because 100 ÷ 100 = 1.
| Interface speed | Calculation | Automatic cost | Meaning |
|---|---|---|---|
| 10 Mbps | 100 ÷ 10 | 10 | Less preferred than FastEthernet |
| 100 Mbps | 100 ÷ 100 | 1 | Preferred over a 10 Mbps link |
| 1 Gbps | 100 ÷ 1000 | 1 | Result is limited to the minimum cost |
| 10 Gbps | 100 ÷ 10000 | 1 | Looks equal to 1 Gbps with this reference |
2. Choose a Useful Reference Bandwidth
The old 100 Mbps default cannot show the difference between modern high-speed links. A 1 Gbps link and a 10 Gbps link both receive cost 1. Raising the reference bandwidth restores useful separation.
| Link speed | 100 Mbps reference | 100,000 Mbps reference |
|---|---|---|
| 100 Mbps | 1 | 1000 |
| 1 Gbps | 1 | 100 |
| 10 Gbps | 1 | 10 |
| 100 Gbps | 1 | 1 |
3. See How OSPF Selects a Path
R1 has two ways to reach R4's loopback. OSPF adds the outgoing costs along each path. The loopback contributes cost 1 to both choices, so R1 still prefers the path with the lower transit cost.
Selected
Backup
4. Configure Reference Bandwidth and Manual Cost
Reference bandwidth changes the automatic cost calculation for all OSPF-enabled interfaces on a router. Manual cost changes one interface and overrides the automatically calculated value.
Set a 100 Gbps reference
R1# configure terminal
R1(config)# router ospf 1
R1(config-router)# auto-cost reference-bandwidth 100000
% OSPF: Reference bandwidth is changed.
Set one manual interface cost
R1# configure terminal
R1(config)# interface GigabitEthernet0/1
R1(config-if)# ip ospf cost 25
R1(config-if)# end
- Use reference bandwidth when interface speed should drive the metric across the whole OSPF design.
- Use manual cost when the design requires a specific primary or backup path independent of reported bandwidth.
- Do not use the bandwidth command only to tune OSPF. It can affect other routing protocols, QoS, monitoring, and interface statistics. Use
ip ospf costwhen only OSPF should change.
5. Read the Verification Output
Verify the local interface cost and the complete route metric together. The interface output explains one link. The routing table shows the accumulated cost to the destination.
Check interface and reference values
R1# show ip ospf interface brief
Interface PID Area Cost State
Gi0/0 1 0 10 P2P
Gi0/1 1 0 25 P2P
R1# show ip ospf | include Reference
Reference bandwidth unit is 100000 mbps
Check the selected route
R1# show ip route ospf 10.4.4.4
Routing entry for 10.4.4.4/32
Known via "ospf 1", distance 110, metric 21
* 192.168.12.2, via GigabitEthernet0/0
[110/21] in a short OSPF route entry means administrative distance 110 and total OSPF metric 21. It does not mean two separate interface costs.6. Troubleshoot Unexpected Path Choices
Do not change the metric immediately. First prove which value is different from the design.
- Confirm the next hop. Use
show ip route ospfand record the selected next hop and total metric. - Check every outgoing interface in the path. One manual
ip ospf costcan override the bandwidth calculation. - Compare reference bandwidth. A router using a different value can calculate different local interface costs.
- Check topology and LSAs. A failed link or missing LSA can remove the expected path before cost comparison happens.
- Look for equal totals. Multiple next hops are correct when ECMP is enabled and the complete path costs match.
7. Guided Practice Lab
Build a four-router topology with the two paths shown above. Capture the baseline before each change so you can explain why SPF changed its decision.
Task 1: Prove the Lower-Cost Path
- Configure all links in OSPF Area 0.
- Set the upper path costs to 10 and 10.
- Set the lower path costs to 5 and 30.
- Check R1's route to R4's loopback.
Expected result: R1 selects the upper path through R2 with total cost 21, including R4's loopback cost 1.
Task 2: Create Equal-Cost Paths
- Change the R3-to-R4 outgoing cost from 30 to 15.
- Calculate both totals before checking the router.
- Run
show ip route ospfon R1.
Expected result: Both paths total 21, so R1 can install two next hops for the destination.
Task 3: Test Reference Bandwidth
- Remove the manual costs from the lab interfaces.
- Record the automatic costs with the default reference.
- Set
auto-cost reference-bandwidth 100000on every router. - Compare interface costs and the selected route again.
Expected result: Faster links receive meaningfully lower costs, and every router reports the same reference bandwidth.
8. Frequently Asked Questions
Does lower OSPF cost mean a better path?
Yes. OSPF prefers the path with the lowest accumulated cost from the local router to the destination.
Why do 1 Gbps and 10 Gbps links both show cost 1?
The default 100 Mbps reference bandwidth is too low to distinguish them. Raise the reference bandwidth consistently across all OSPF routers.
Does OSPF add the cost of every interface?
OSPF adds outgoing interface costs along the path from the calculating router. The route metric is the total, not just the cost of the local interface.
Does manual OSPF cost override automatic cost?
Yes. ip ospf cost sets the value OSPF uses on that interface instead of the bandwidth-based calculation.
What happens when two OSPF paths have the same cost?
OSPF can install multiple equal-cost next hops and share traffic across them, subject to the platform's equal-cost multipath limit.