In Open Shortest Path First (OSPF), the metric, referred to as cost, determines the best path to a destination. The cost is derived from interface bandwidth, reflecting the monetary cost of higher-bandwidth links. This post details OSPF cost calculation, methods to adjust the cost, and how metrics are handled for intra-area, inter-area, and external routes.
OSPF Cost Calculation
OSPF calculates the cost for an interface using the formula:
Cost = Reference Bandwidth / Interface Bandwidth
Reference Bandwidth: Default is 100 Mbps (10^8 bits per second) in Cisco implementations.
Interface Bandwidth: Determined by the interface type (e.g., Ethernet, FastEthernet, GigabitEthernet, etc.).
Rounding: The result is rounded down to the nearest integer, with a minimum cost of 1.
Example Calculations
Ethernet (10 Mbps): 100 Mbps / 10 Mbps = 10
FastEthernet (100 Mbps): 100 Mbps / 100 Mbps = 1
Serial T1 (1.544 Mbps): 100 Mbps / 1.544 Mbps ≈ 64.76, rounded to 64
GigabitEthernet (1000 Mbps): 100 Mbps / 1000 Mbps = 0.1, rounded to 1
10GigabitEthernet (10000 Mbps): 100 Mbps / 10000 Mbps = 0.01, rounded to 1
Issue with higher BW interfaces: The default reference bandwidth of 100 Mbps causes interfaces faster than 100 Mbps (e.g., GigabitEthernet, 10GigabitEthernet) to have the same cost (1), making OSPF unable to differentiate between them. This issue arose as network speeds exceeded the reference bandwidth defined in OSPF’s original design (RFC 2328, 1998).
Adjusting OSPF Cost
To address the issue with higher bandwidth interfaces or customize path selection, OSPF cost can be adjusted in three ways:
Change Reference Bandwidth:
Increases the reference bandwidth to differentiate faster interfaces (e.g., 1000 Mbps or higher).
Requirement: Must be configured consistently across all OSPF routers in the domain to ensure correct path selection.
Configuration:
IOS/IOS XE:
R1(config)# router ospf 1
R1(config-router)# auto-cost reference-bandwidth 10000
Sets reference bandwidth to 10 Gbps (10000 Mbps).
Example: GigabitEthernet (1000 Mbps) cost = 10000 Mbps / 1000 Mbps = 10; 10GigabitEthernet (10000 Mbps) cost = 1.
IOS XR:
XR(config)# router ospf 1
XR(config-ospf)# auto-cost reference-bandwidth 10000
XR(config-ospf)# commit
Range: 1–4294967 Mbps.
Change Interface BWv
Modifies the interface’s bandwidth value to influence OSPF cost.
Not Recommended: Affects other protocols (e.g., QoS, EIGRP) that rely on interface bandwidth.
Configuration:
R1(config)# interface GigabitEthernet0/0
R1(config-if)# bandwidth 100000 ! In Kbps (100 Mbps)
Example: Sets interface bandwidth to 100 Mbps, resulting in cost = 100 Mbps / 100 Mbps = 1.
Manually Set OSPF Cost:
Directly assigns a cost to an interface, overriding the bandwidth-based calculation.
Recommended: Allows precise control but requires a planned cost assignment strategy for consistency.
Configuration:
IOS/IOS XE:
R1(config)# interface GigabitEthernet0/0
R1(config-if)# ip ospf cost 10
IOS XR:
XR(config)# router ospf 1
XR(config-ospf)# area 0
XR(config-ospf-ar)# interface GigabitEthernet0/0/0/0
XR(config-ospf-ar-if)# cost 10
XR(config-ospf-ar-if)# commit
Range: 1–65535.
Path Cost Calculation
To calculate the total cost to a destination network:
Identify the destination network and the path(s) to reach it.
Sum the OSPF costs of all outgoing interfaces along the path, including the local router’s egress interface.
Note: When you are calculating the cost to reach a destination network that is a loopback interface, make sure to count the loopback interface cost as part of the accumulated path cost, and your local outgoing interface as well.
SPF Algorithm:
OSPF uses the Dijkstra Shortest Path First (SPF) algorithm to compute the best path.
The local router is placed as the root of a shortest-path tree, and costs are calculated to all destinations by summing the costs of outgoing interfaces.
The process is repeated for each router in the area, building a consistent LSDB.
Metrics for Different LSA Types
The OSPF metric behaves differently depending on the LSA type and route type:
Intra-Area Routes (Type 1 Router LSA, Type 2 Network LSA):
Cost is the sum of the interface costs along the path to the destination within the same area.
Example: To reach a network in Area 0, sum the costs of all outgoing interfaces from the local router to the destination.
Inter-Area Routes (Type 3 Summary LSA):
Generated by Area Border Routers (ABRs) to advertise prefixes from one area to another.
Cost is the sum of:
The intra-area cost from the ABR to the destination network in the originating area.
The cost from the local router to the ABR (via intra-area path in the local area).
Example: If an ABR advertises a Type 3 LSA for 10.0.1.0/24 with a cost of 10, and the cost to reach the ABR is 5, the total cost is 15.
External Routes (Type 5 External LSA):
External routes are redistributed into OSPF from another protocol (e.g., BGP, static) or a default route.
Sub-Type 2 (E2, Default):
Uses the external metric set by the Autonomous System Boundary Router (ASBR), defaulting to 20 for redistributed routes.
Internal OSPF costs (to reach the ASBR) are not added to the metric.
Forwarding Metric: If multiple ASBRs advertise the same E2 route with equal external metrics, the internal OSPF cost to each ASBR is used as a tiebreaker for path selection.
Example: Two ASBRs advertise 192.168.1.0/24 with metric 20. The path with the lower internal cost to the ASBR is chosen.
Supports equal-cost load balancing if internal costs are equal.
Sub-Type 1 (E1):
Combines the external metric (e.g., 20) with the internal OSPF cost to reach the ASBR.
Preferred over E2 routes because it accounts for the full path cost.
Example: If the external metric is 20 and the cost to the ASBR is 5, the total cost is 25.
Configuration (set during redistribution):
R1(config)# router ospf 1
R1(config-router)# redistribute static metric 20 metric-type 1
Verification Commands
To verify OSPF costs and metrics, use the following Cisco IOS/IOS XE commands:
Show OSPF Interface:
R1# show ip ospf interface [type number]
Displays interface cost, bandwidth, and OSPF settings.
Show OSPF Database:
R1# show ip ospf database
Shows LSA types and their metrics (e.g., Type 3 Summary, Type 5 External).
Show IP Route OSPF:
R1# show ip route ospf
Displays OSPF routes with their total costs and route types (O, O IA, O E1, O E2).
Debug OSPF SPF (use with caution in production):
R1# debug ip ospf spf
Shows SPF calculations and cost assignments.
IOS XR Equivalents:
show ospf interface [type number]
show ospf database
show ip route ospf
debug ospf spf
Key Considerations
Reference Bandwidth Consistency: Changing the reference bandwidth must be applied uniformly across all OSPF routers to avoid inconsistent path selection.
Manual Cost Override: Use ip ospf cost for precise control, but maintain a consistent cost assignment strategy (e.g., 10 for GigabitEthernet, 1 for 10GigabitEthernet).
External Route Preference: E1 routes are preferred over E2 routes due to their inclusion of internal costs, providing a more accurate path cost.
Loopback Interfaces: Loopback prefixes (/32) have a default cost of 1.
SPF Algorithm: The Dijkstra algorithm ensures optimal path selection by summing interface costs, prioritizing the lowest total cost.
Forwarding Metric: For E2 routes, the internal cost to the ASBR acts as a tiebreaker, not a proportional load-balancing factor.