OSPF Cost and SPF Calculation
Open Shortest Path First (OSPF) is a link-state routing protocol that determines the best path for data packets using the Shortest Path First (SPF) algorithm. The cost metric is central to OSPF’s decision-making process, as it quantifies the efficiency of a route based on bandwidth and other configurable parameters. Mastering how OSPF calculates cost and selects paths ensures optimal network performance and troubleshooting.
Key Concepts
1. OSPF Cost Calculation
OSPF evaluates paths using a cost metric derived from the formula:
Cost = Reference Bandwidth / Interface Bandwidth
- Reference Bandwidth: Defaults to
100 Mbps(100,000,000 bits per second). - Interface Bandwidth: The configured or default speed of the interface (in bits per second).
- Lower cost = preferred path.
Note: The
bandwidthcommand in Cisco IOS modifies the interface’s bandwidth value for metric calculation but does not change the physical speed.
Default Cost Examples
| Interface Type | Bandwidth | OSPF Cost |
|---|---|---|
| Fast Ethernet | 100 Mbps | 1 |
| Ethernet (10 Mbps) | 10 Mbps | 10 |
| Serial (1.544 Mbps) | 1.544 Mbps | ~64 |
Example Calculation: For a 1.544 Mbps serial link:
100,000,000 / 1,544,000 ≈ 64
2. SPF Algorithm (Dijkstra’s Algorithm)
OSPF uses Dijkstra’s algorithm to:
- Build a shortest path tree (SPT) from the local router’s perspective.
- Calculate the cumulative cost of each path to a destination.
- Select the path with the lowest total cost.
How Path Selection Works
- Sum the costs of all outgoing interfaces along a path.
- Compare cumulative costs across all available paths.
- Install the lowest-cost path in the routing table.
Example:
Router R1 needs to reach 10.10.10.0/24 via two paths:
- Path 1: Serial (cost 64) + Fast Ethernet (cost 1) = Total cost 65
- Path 2: Multiple slower links = Higher total cost
OSPF selects Path 1 (cost 65) and adds this entry to the routing table:
O 10.10.10.0/24 [110/65] via x.x.x.x
O: OSPF route.110: Administrative distance (trustworthiness).65: Total OSPF cost.
3. Visualizing OSPF Path Selection
Network Topology
(Cost 64)
R1 ------------- R2
| |
| | (Cost 1)
| |
+------- R3 -----+
Higher cost
Shortest Path Tree (SPF Output)
R1
|
R2
|
10.10.10.0/24
Modifying OSPF Cost
Adjusting Interface Bandwidth
Use the bandwidth command to influence OSPF cost (values in Kbps):
interface serial 0/0/0
bandwidth 64 # Sets bandwidth to 64 Kbps (64,000 bps)
Calculation:
100,000,000 / 64,000 = 1562.5 ≈ 1562
New OSPF cost = 1562 (less preferable).
Warning: The
bandwidthcommand does not alter physical link speed—only the metric.
Adjusting Reference Bandwidth
Modern networks (e.g., Gigabit Ethernet) may require increasing the reference bandwidth to avoid cost collisions:
router ospf 1
auto-cost reference-bandwidth 1000 # Sets reference to 1 Gbps
Impact:
- 1 Gbps link: Cost =
1000 / 1000 = 1 - 100 Mbps link: Cost =
1000 / 100 = 10
Common Mistakes and Best Practices
Pitfalls to Avoid
- Confusing cost and bandwidth: Cost is derived from bandwidth but is not the same.
- Ignoring cumulative cost: OSPF sums costs across all hops in a path.
- Default reference bandwidth: May not suit modern networks (e.g., Gigabit links default to cost 1).
- Misusing the
bandwidthcommand: It does not change physical speed.
Best Practices
- Adjust reference bandwidth for networks with high-speed links (e.g.,
auto-cost reference-bandwidth 10000for 10 Gbps). - Verify costs with
show ip ospf interfaceorshow ip route ospf. - Document changes to bandwidth/reference bandwidth to avoid confusion.
Practical Use Cases
Scenario: Small Business Network
Problem:
- Primary fiber link (1 Gbps) and backup DSL (20 Mbps) both show cost = 1 (default reference bandwidth = 100 Mbps).
- OSPF cannot distinguish between the two paths.
Solution:
router ospf 1
auto-cost reference-bandwidth 1000 # 1 Gbps reference
Result:
- Fiber (1 Gbps): Cost = 1
- DSL (20 Mbps): Cost = 50
- OSPF now prefers the fiber link.
Quick Summary
- OSPF cost = Reference Bandwidth / Interface Bandwidth.
- Default reference bandwidth: 100 Mbps.
- Lower cost = preferred path.
- SPF algorithm (Dijkstra’s) calculates the shortest path tree.
- Administrative distance: 110 (OSPF’s trustworthiness).
- Bandwidth command: Configures metric (in Kbps), not physical speed.
- Adjust reference bandwidth for modern networks (e.g., Gigabit/10G).
Learn More
- RFC 2328: OSPF Version 2 (IETF)
- Cisco Documentation: OSPF Configuration Guide
- IETF: Link-State Routing Principles
- IEEE 802.3: Ethernet Standards