OSPF Cost and SPF Calculation
Open Shortest Path First (OSPF) is a link-state routing protocol that uses the Shortest Path First (SPF) algorithm to determine optimal routes through a network. The protocol's decision-making relies on a cost metric that quantifies route efficiency based on interface bandwidth. Understanding how OSPF calculates cost and selects paths is essential for network optimization and troubleshooting.
How OSPF Calculates Cost
OSPF determines the cost of each interface using a simple formula:
Cost = Reference Bandwidth / Interface Bandwidth
Key components:
- Reference Bandwidth: Default is 100 Mbps (100,000,000 bps)
- Interface Bandwidth: The configured or default speed of the interface in bps
- Lower cost values indicate preferred paths
Default Cost Values
| Interface Type | Bandwidth | OSPF Cost |
|---|---|---|
| Fast Ethernet | 100 Mbps | 1 |
| Ethernet | 10 Mbps | 10 |
| Serial (T1) | 1.544 Mbps | 64 |
| Gigabit Ethernet | 1000 Mbps | 1 |
Example calculation for a T1 serial link:
100,000,000 / 1,544,000 = 64.76 ≈ 64
Important: The
bandwidthcommand in Cisco IOS modifies the bandwidth value used for metric calculation but does not change the physical link speed.
The SPF Algorithm Process
OSPF uses Dijkstra's algorithm to build a shortest path tree (SPT) and select optimal routes:
- Calculate cumulative cost: Sum the costs of all outgoing interfaces along each possible path
- Compare all paths: Evaluate total costs for all available routes to each destination
- Select lowest cost: Install the path with the lowest total cost in the routing table
Path Selection Example
Consider Router R1 reaching network 10.10.10.0/24 via two possible paths:
Path 1: Serial interface (cost 64) → Fast Ethernet (cost 1) = Total: 65
Path 2: Multiple slower links = Total: 85
OSPF selects Path 1 and creates this routing table entry:
O 10.10.10.0/24 [110/65] via 192.168.1.2
Where:
O= OSPF route110= Administrative distance (OSPF's trustworthiness level)65= Total cumulative OSPF cost
Modifying OSPF Cost
Adjusting Interface Bandwidth
You can influence OSPF cost calculations by modifying the interface bandwidth value (specified in Kbps):
interface serial 0/0/0
bandwidth 64
This sets the bandwidth to 64 Kbps for metric calculation:
100,000,000 / 64,000 = 1,562.5 ≈ 1,562
The interface now has an OSPF cost of 1,562, making it less preferable.
Adjusting Reference Bandwidth
For modern high-speed networks, increase the reference bandwidth to differentiate between fast links:
router ospf 1
auto-cost reference-bandwidth 1000
This sets the reference to 1 Gbps (1,000 Mbps), resulting in:
- 1 Gbps link: Cost = 1,000 / 1,000 = 1
- 100 Mbps link: Cost = 1,000 / 100 = 10
- 10 Mbps link: Cost = 1,000 / 10 = 100
Best Practice: Apply the same reference bandwidth across all routers in the OSPF domain to ensure consistent cost calculations.
Common Pitfalls and Solutions
Issues to Avoid
Cost collision on high-speed links
With default settings, both 100 Mbps and 1 Gbps interfaces receive a cost of 1, preventing OSPF from distinguishing between them.
Confusing bandwidth with physical speed
The bandwidth command only affects routing metrics—it doesn't change actual link capacity.
Ignoring cumulative costs
OSPF sums costs across all hops. A single high-cost link can make an entire path undesirable.
Inconsistent reference bandwidth
Different reference bandwidth values across routers lead to suboptimal routing decisions.
Recommended Practices
- Set appropriate reference bandwidth based on your fastest links (e.g., 10,000 for 10 Gbps networks)
- Verify cost values using
show ip ospf interfaceandshow ip route ospf - Document all changes to bandwidth and reference bandwidth configurations
- Apply settings consistently across all OSPF routers in your network
Practical Use Case: Dual-Link Failover
Scenario
A small business has two internet connections:
- Primary: 1 Gbps fiber link
- Backup: 20 Mbps DSL connection
With default OSPF settings (100 Mbps reference bandwidth), both links show cost = 1, preventing proper failover.
Solution
router ospf 1
auto-cost reference-bandwidth 1000
Result
- Fiber (1 Gbps): Cost = 1,000 / 1,000 = 1
- DSL (20 Mbps): Cost = 1,000 / 20 = 50
OSPF now correctly prefers the fiber link and only uses DSL during fiber outages.
Verification Commands
Monitor and troubleshoot OSPF cost calculations with these commands:
show ip ospf interface # View cost per interface
show ip route ospf # Display OSPF routes with costs
show ip ospf neighbor # Verify OSPF adjacencies
show ip protocols # Check reference bandwidth setting
Key Takeaways
- OSPF cost is calculated as Reference Bandwidth / Interface Bandwidth
- Default reference bandwidth is 100 Mbps
- Lower cost values indicate preferred paths
- The SPF algorithm selects paths based on cumulative cost
- OSPF administrative distance is 110
- Modern networks require adjusted reference bandwidth to differentiate high-speed links
- The
bandwidthcommand affects metrics only, not physical speed
Learn More
- RFC 2328: OSPF Version 2 Specification
- Cisco OSPF Configuration Guide: Official Documentation
- IETF Link-State Routing: RFC 1131