OSPF (Open Shortest Path First) is a link-state routing protocol that uses a cost metric to calculate the best route between two nodes. In this article, we'll look at how OSPF calculates costs and discuss some of the factors that can influence them.
So, if you're curious about how OSPF's cost calculation works or want to learn how to tweak its settings for your network, read on!
I strongly recommend checking the OSPF Training for those who want to learn more about this topic.
What is "OSPF Cost"?
OSPF cost is a metric used by the OSPF routing algorithm to calculate the best path between two OSPF-enabled devices. The cost of a path is determined by adding up the costs of the individual links that make up the path. The cost of a link is typically expressed in terms of bandwidth.
In most cases, the cost of a link is inversely proportional to its bandwidth. As a result, OSPF-enabled devices will typically prefer paths with high-bandwidth links.
Because OSPF is a link-state protocol, every router builds a complete topological database of the network and runs Dijkstra's shortest path first (SPF) algorithm against it. Cost is the metric that algorithm minimises, so the route with the lowest total cost becomes the preferred path in the routing table.
How is OSPF Cost Calculated?
The OSPF cost is calculated based on the link's bandwidth and is used by the OSPF algorithm to determine the best path between two routers. The higher the bandwidth, the lower the cost. The lower the cost, the more preferable the link.
In most cases, the OSPF cost is calculated automatically by the router. However, sometimes it may be necessary to set the OSPF cost manually. To do this, use the ip ospf cost command. The value entered with this command will be used as the link's OSPF cost. Keep in mind that this value must be an integer between 1 and 65535. If it is not, the router will revert to using the default OSPF cost calculation.
The OSPF cost is calculated using this formula: reference bandwidth / interface bandwidth.
On Cisco routers the reference bandwidth defaults to 100 Mbps, so the same formula is often written as Cost = 100,000,000 / Bandwidth, where bandwidth is expressed in bits per second. On a 100 Mbps link this yields a cost of 1, which is why higher-speed links are treated as the most attractive routes.
Here Are the Default Costs
Bandwidth - Cost
- Gigabit Ethernet Interface (1 Gbps) - 1
- Fast Ethernet Interface (100 Mbps) - 1
- Ethernet Interface (10 Mbps) - 10
- DS1 (1.544 Mbps) - 64
- DSL (768 Kbps) - 133
A Little Problem
As you can see, OSPF considers all interfaces with a bandwidth of 100 Mbps or more as equal. The best possible cost is always 1. So no matter the speed of the interface, it will all have a default cost of 1 when it is over 100 Mbps. This can create subpar routing in up-to-date networks that use current high-speed ethernet interfaces.
It is possible to force a router to use a faster route manually. To change the reference bandwidth in an OSPF network, use the following command:
router ospf 1reference-bandwidth
Where the value is the desired reference bandwidth in Mbps. For example, to configure the reference bandwidth to 1 Gbps, use the following command:
router ospfreference-bandwidth 1000
By doing this, you can ensure that your routers will pick the fastest route possible.
Be sure to check the CCIE Enterprise training for all the useful OSPF commands.
OSPF Cost vs. Bandwidth: The Relationship
One of the key factors in OSPF cost calculation is the bandwidth of the links. The default cost of a link is inversely proportional to its bandwidth: the higher the bandwidth, the lower the cost. Adjusting the reference bandwidth is a crucial tactic for network designers, especially in environments where link speeds exceed 100 Mbps, so that the OSPF algorithm accurately reflects the real-world performance and priority of each link.
Example Scenario
Consider a network with varying link bandwidths connecting several routers. Router A connects to Router B over a 1 Gbps link and to Router C over a 100 Mbps link. Using the default OSPF cost formula, the cost from Router A to B is lower than from Router A to C, so OSPF favours the higher-bandwidth path under normal circumstances, which is more efficient for data transfer.
These cost metrics are more than just numbers, though. They are a critical component of traffic engineering, influencing the route your data takes through the network and potentially affecting both performance and reliability.
Under What Circumstances Might You Need to Manipulate OSPF Metrics?
In a typical OSPF network, the cost is automatically calculated based on the bandwidth of the link. However, there are times when it may be necessary to manipulate the cost metric manually. For example, if two links have different bandwidths, but the router considers them to be equally reliable, it may make sense to increase the cost of the lower bandwidth link to reduce traffic on that link.
In another example, if one link is consistently congested while another is not, it may be necessary to decrease the cost of the congested link to encourage more traffic to use that link. These are just a few examples of when it might be necessary to manipulate OSPF cost metrics; in general, any time there is an imbalance in traffic or reliability between two links, manipulating the cost metric can help to restore balance.
Practical Example: Traffic Engineering
Imagine a network where Router A is connected to both Router B and Router C, but the link to Router B is significantly faster than the link to Router C. Under normal OSPF operation, most traffic prefers the path through Router B because of its lower cost. If Router B starts to suffer from congestion, or you need to perform maintenance on it, you can increase the OSPF cost of that interface to steer traffic toward Router C:
router ospfinterface fa0/1
ip ospf cost 2000
This sets a higher cost on the specified interface, making the path through Router C more attractive and balancing the network load or avoiding the congested link.
Enhancing Network Resilience
Cost manipulation also strengthens network resilience. By strategically adjusting OSPF costs, engineers can create redundant paths that prevent single points of failure. During a link failure, OSPF automatically reroutes traffic to the next best path with minimal disruption, based on the prevailing cost metrics. This behaviour is particularly important in disaster-recovery scenarios where maintaining uptime is critical.
Manipulating costs is not free of risk, however. Misconfiguration can lead to suboptimal routing, increased latency, and lower throughput, so cost changes should always be made with a clear understanding of both the network topology and the organisational requirements it supports.
How to Configure OSPF Cost Values on Cisco Routers
Setting OSPF cost values on Cisco routers is a crucial task for network administrators aiming to optimise routing efficiency. The process can be broken down into a series of steps that should be followed methodically to ensure accuracy. First, access the router's console: connect your console cable and use terminal software such as PuTTY to start a console session, making sure you have the administrative privileges needed to change the router configuration.
Accessing Router Configuration Mode
Once logged into the router, enter Global Configuration mode, which lets you make device-wide changes including OSPF settings:
enableconfigure terminal
Configuring the OSPF Interface Cost
To set the OSPF cost on an interface, navigate to that interface in configuration mode and apply the cost:
interface GigabitEthernet0/1ip ospf cost 10
This sets the OSPF cost to 10 on the GigabitEthernet0/1 interface. Remember that the lower the cost value, the more likely the interface will be preferred for routing OSPF traffic, so choose a value that reflects the bandwidth and priority of the interface within your overall topology.
Verifying OSPF Cost Configuration
After configuring the OSPF cost values, verify that the settings are applied correctly and that the network behaves as expected.
Using show Commands
One of the most effective ways to verify an OSPF configuration is with the show commands in Cisco IOS:
show ip ospf interfaceThis command displays the state of OSPF on each interface, including the cost value you set. Look for the line labelled "Cost" to confirm your configuration is active, and check that the interface is in FULL state (fully adjacent with neighbours) or the designated router / backup designated router state, depending on your network setup.
Testing with traceroute
Once you are confident the cost settings are correct, test overall network performance by tracing the route packets take:
traceroute [destination IP]This confirms that packets follow the optimised path intended by your OSPF cost adjustments. If you encounter unexpected or suboptimal routes, reassess your cost settings and adjust them to better reflect your desired traffic patterns.
Key Factors Affecting OSPF Cost
While bandwidth is the dominant factor in OSPF cost, a few other elements also influence the calculation:
- Link type: higher-speed connections generally have lower costs.
- Network design: the way a network is architected influences the calculation, especially where certain links are intended to be backups or secondary routes.
- Administrative tweaking: administrators can adjust OSPF costs directly to manage traffic flow according to organisational needs.
OSPF vs. EIGRP: Cost Calculation Differences
OSPF and EIGRP take different approaches to cost calculation. OSPF, primarily used in larger, heterogeneous environments, calculates cost from bandwidth using Cost = Reference Bandwidth / Link Bandwidth. Its default reference bandwidth is 100 Mbps, but this can be adjusted for newer, faster networks so that older, slower links do not unfairly skew the metric.
EIGRP, a Cisco-proprietary protocol known for rapid convergence, uses a composite metric that considers bandwidth, load, delay, and reliability. These elements are weighted through configurable K-values, and EIGRP's DUAL (Diffusing Update Algorithm) helps it achieve faster convergence times.
The main differences can be summarised as follows:
- Cost metric basis: OSPF uses bandwidth; EIGRP uses a composite of bandwidth, load, delay, and reliability.
- Adjustability: OSPF exposes an adjustable reference bandwidth; EIGRP exposes adjustable K-values.
- Network suitability: OSPF suits large, varied link-speed environments; EIGRP suits dynamic, performance-sensitive networks.
- Convergence speed: OSPF is relatively slower; EIGRP is faster thanks to DUAL.
- Protocol type: OSPF is an open standard; EIGRP is Cisco-proprietary and requires Cisco hardware.
OSPF's simplicity and vendor neutrality make it a reliable choice for standardised high-speed links, while EIGRP offers more granular, dynamic control in networks with fluctuating link qualities.
Best Practices for Managing OSPF Costs
Adjusting OSPF costs is both an art and a science. The following practices help keep routing decisions balanced and efficient:
- Understand the topology first: know the current load and performance metrics, as well as potential growth scenarios, before changing any cost.
- Review configurations regularly: OSPF settings should not be static; revisit them as the network grows so sub-optimal routes are caught and corrected.
- Tweak sparingly: over-tuning complicates management and can obscure the underlying issues that actually need addressing. Make adjustments judiciously and keep them consistent across network segments.
- Make gradual changes: incremental adjustments let you assess the impact of each change without disrupting operations.
- Monitor continuously: real-time analytics and detailed logs reveal when a cost adjustment is needed and how it plays out in practice.
- Document every change: record topology diagrams, initial and new cost values, the rationale for each change, and the verification output. Good documentation speeds up troubleshooting, audits, and future planning.
- Train your team: spreading OSPF knowledge across the IT team means more people can identify and fix OSPF-related issues.
OSPF Cost in Technical Interviews
OSPF cost is a common topic in networking interviews. Being able to explain that cost is inversely proportional to bandwidth, recall the Cost = 100,000,000 / Bandwidth formula, and describe when and how you would manually adjust a cost for traffic engineering demonstrates both theoretical knowledge and practical ability. Balancing that theory with hands-on configuration experience is exactly what interviewers look for in aspiring network engineers.
Conclusion
OSPF cost calculation is not just about numbers; it is about strategically guiding data through a network in the most efficient way possible. By understanding how cost is derived from bandwidth, adjusting the reference bandwidth on modern high-speed links, and manipulating costs deliberately where needed, network professionals can build networks that are both optimised and resilient.
