Cisco OSPF Cost Metric

In this Cisco CCNA tutorial, you’ll learn about the OSPF metric which is the cost. Scroll down for the video and also text tutorial.

Cisco OSPF Cost Metric Video Tutorial

YouTube Video

Kelvin Kapita

Kelvin Kapita

I passed the exam on the first attempt, thank you so much. I am recommending this course 100% to anyone who would like to pass CCNA on the first attempt.

Kelvin Kapita

 

OSPF Metric Calculation

 

As OSPF is a Link State routing protocol, the router will learn about all the destinations in its area, the links to get there, and their cost. The router will select routes based on the lowest cost to get to the destination, and that's the route that will make it into the routing table.

 

Cisco OSPF Metric Calculation

 

Let’s have a look at an example of an OSPF metric calculation. R2 over on the left has got two possible paths that you could take to get to the 10.0.1.0/24 network behind R1. It could either go over the single link directly to R1, or it could go via R3.

 

Cisco OSPF Metric Calculation 1

 

If it goes over directly the link to R1, the cost there would be 50, plus 10 which is the cost to R1, the link itself. Therefore, the total cost would be 60. If it goes along the bottom path via R3, the cost would be 10 plus 10 plus 10 which is equal to 30.

 

The bottom path has got a lower cost. Even though it's a longer hop count, it has a lower cost because of its better quality links with higher bandwidth, so it's going to prefer to take the lower path.

 

Reference Bandwidth

 

The cost for OSPF is automatically derived from the interface bandwidth. A higher bandwidth interface will be automatically preferred. The way that the cost is calculated is:

reference bandwidth / interface bandwidth

 

The default reference bandwidth is 100 Mbps. This means is if you've got a fast ethernet link, that would default to a cost of 1 because fast ethernet’s default bandwidth is 100 Mbps. We divide 100 by 100 and that gives us a cost of 1. On a serial interface, like a T1 in that example here, the bandwidth is 1.544 Mbps. That will default to a cost of 64 because a hundred divided by 1.544 is 64.

 

Reference Bandwidth

 

You can see that the higher bandwidth interface is going to be automatically preferred. However, there's a problem here because OSPF will treat all interfaces of 100 Mbps or faster as equal. The best possible cost is 1. We don't have a 0.1 cost. So fast ethernet, gigabit ethernet, and 10 gigabit ethernet, a hundred gigabit ethernet, etc., will all default to a cost of 1.

 

This can cause undesirable routing in modern networks with modern high-speed ethernet interfaces. You can see in the example below that the link directly between R1 and R2 on the top path is fast ethernet. So, with a default reference bandwidth of 100, it gets a cost of 1.

 

On the bottom path going via our R3, they're gigabit ethernet interfaces so they're faster, they've got higher bandwidth. But because we've got the default reference bandwidth of 100, they get a cost of 1 each as well.

 

Reference Bandwidth 1

 

The top path will have a default cost of 2, which is the cost from R2 to R1, plus the cost of the link itself. Then, along the bottom path, it's going to have a cost of 3. Therefore, the bottom path is not going to be preferred. The router is going to use the top path, which is the fast ethernet, even though it's slower than the bottom path which is gigabit ethernet.

 

We would prefer the traffic to go along the bottom path. The way that we can force this is by changing the reference bandwidth. The reason that the default reference bandwidth is a hundred, by the way, is that OSPF has been around for a long time. When OSPF first came out, we were back on ethernet networks, like old-style classical ethernet of 10 Mbps.

 

At the time, network engineers thought, "Well, maybe sometime in the future, we'll have a hundred megabits per second. Well, that's way off. We'll never get faster than that." Obviously, times have moved on and we do have much faster ethernet interfaces than fast ethernet, 100 Mbps now. We've got gigabit ethernet, we've got 10 gigabit ethernet, and we've even got 40 gigabit and 100 gigabit ethernet now.

 

Using the old default reference bandwidth, it'll all be treated the same equal cost by OSPF. We want to set the reference bandwidth to a higher volume. The way you do that is at the global config, we issue the commands:

router ospf 1

auto-cost reference-bandwidth 100000

 

100000 is what you want to set the reference bandwidth to. If you set it to 1000 for example, that would mean that gigabit ethernet interfaces would have a cost of one, fast ethernet would get a cost of 10.

 

But you want to think a bit further ahead in the future. If on your current production network, your fastest interfaces right now are gigabit ethernet, don't set a reference bandwidth of 1000 because maybe in a year or two's time, you're going to have 10 gigabit, 40 gigabit ethernet, and 100 gigabit ethernet.

 

Set it to a high value that you're not going to have to change it again in the future. When you set the reference bandwidth, you need to do it the same on all routers so they're all using a consistent metric.

 

In the example here, I've set the reference bandwidth to 100,000 which is 100 gigabit ethernet. The fast ethernet interface along the top path will get a cost of 1000, the gigabit ethernet interfaces along the bottom path, get a cost of 100. Now, it will prefer the bottom path because we changed the reference bandwidth.

 

Reference Bandwidth 2

 

Manipulating the OSPF Metric

 

In real-world networks, typically all you'll have to do is just change the reference bandwidth and then OSPF is going to automatically select the highest bandwidth paths, which is what you would normally prefer.

 

Manipulating the OSPF Metric

 

However, you might want to manipulate this. For example, say you've got a high latency satellite link, which has higher bandwidth, but you want to prefer the lower bandwidth interface. You can do that by manipulating the OSPF metric.

 

Another reason would be just if you want to spread the load of your traffic across the different paths in your network. So, OSPF takes the bandwidth of an interface into account when calculating the metric so paths along higher bandwidth links will be preferred. The most desirable path will typically be automatically selected.

 

But if you want to use a different path, you can manipulate that by manually changing the bandwidth or the OSPF cost on interfaces. It's recommended to use cost because the bandwidth setting can affect many features of OSPF, such as QoS.

 

Manipulating the OSPF Metric 1

 

OSPF Metric - Bandwidth

 

With OSPF we manipulate the cost rather than the bandwidth, but both would have the same effect. In the example below, on R1, I've said:

show interface serial1/0

 

I can see there that the bandwidth is 1544 Kbit/sec, which is the default bandwidth on a serial interface. If I wanted to change this in the global config, I can issue the commands:

interface serial1/0

bandwidth 768

 

OSPF Metric - Bandwidth

 

Again, at the physical level, the link is still going to run at the clock rate. If the clock rate is 1554, it's still going to run at 1554. Setting the bandwidth does not change the actual physical speed of the interface, it just changes how IOS will look at that interface for software policy. This is how we could manipulate the overall cost for that link by changing the bandwidth.

 

OSPF Metric - Cost

 

The better way of doing it is by directly changing the cost because that wouldn't affect other software policies like QoS. To do that, we can say:

interface fastethernet 0/0

ip ospf cost 50

 

OSPF Metric - Cost

 

To verify what the costs on the link are, we can do a:

show ip ospf interface

 

If you've got a lot of interfaces on the router, you're going to get a quite long output. So, you can also specify the individual interface at the end of the command and you'll just get information about that one interface.

 

To see the cost on all of our interfaces in a short output, we can use the command:

show ip ospf interface brief

 

OSPF Metric - Cost 1

 

Cisco OSPF Cost Metric Configuration Example

 

This configuration example is taken from my free ‘Cisco CCNA Lab Guide’ which includes over 350 pages of lab exercises and full instructions to set up the lab for free on your laptop.

Click here to download your free Cisco CCNA Lab Guide.

 

YouTube Video

 

Cisco OSPF Cost Metric Configuration Example

 

  1. There are two possible paths which R1 could use to reach the 10.1.2.0/24 network – either through R2 or R5. Which route is in the routing table?

The path via R5 at 10.0.3.2.

 

R1#sh ip route

Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP

       D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area

       N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2

       E1 - OSPF external type 1, E2 - OSPF external type 2

       i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2

       ia - IS-IS inter area, * - candidate default, U - per-user static route

       o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP

       + - replicated route, % - next hop override

 

Gateway of last resort is not set

 

10.0.0.0/8 is variably subnetted, 12 subnets, 2 masks

C 10.0.0.0/24 is directly connected, FastEthernet0/0

L 10.0.0.1/32 is directly connected, FastEthernet0/0

C 10.0.1.0/24 is directly connected, FastEthernet0/1

L 10.0.1.1/32 is directly connected, FastEthernet0/1

C 10.0.2.0/24 is directly connected, FastEthernet1/0

L 10.0.2.1/32 is directly connected, FastEthernet1/0

C 10.0.3.0/24 is directly connected, FastEthernet1/1

L 10.0.3.1/32 is directly connected, FastEthernet1/1

O 10.1.0.0/24 [110/2000] via 10.0.0.2, 00:01:04, FastEthernet0/0

O 10.1.1.0/24 [110/3000] via 10.0.0.2, 00:01:04, FastEthernet0/0

  [110/3000] via 10.0.3.2, 00:01:04, FastEthernet1/1

O 10.1.2.0/24 [110/3000] via 10.0.3.2, 00:01:04, FastEthernet1/1

O 10.1.3.0/24 [110/2000] via 10.0.3.2, 00:01:04, FastEthernet1/1

192.168.0.0/32 is subnetted, 5 subnets

C 192.168.0.1/32 is directly connected, Loopback0

O 192.168.0.2/32 [110/1012] via 10.0.0.2, 00:01:04, FastEthernet0/0

O 192.168.0.3/32 [110/2012] via 10.0.0.2, 00:01:04, FastEthernet0/0

O 192.168.0.4/32 [110/2012] via 10.0.3.2, 00:01:04, FastEthernet1/1

O 192.168.0.5/32 [110/1012] via 10.0.3.2, 00:01:04, FastEthernet1/1

 

2. Change this so that traffic from R1 to 10.1.2.0/24 will be load balanced via both R2 and R5.

Since we changed the reference bandwidth, all interfaces have a cost of 1000. The current path from R1 > R5 > R4 has a cost of 3000 (the cost of the destination interface itself is also counted in the total cost).

 

The path from R1 > R2 > R3 > R4 has a cost of 4000.

 

The easiest way to configure both paths to have the same cost is to configure the links from R1 > R5 and R5 > R4 to have a cost of 1500 each. (R1 > R5 = 1500, plus R5 > R4 = 1500, plus cost of 10.1.2.0/24 interface on R4 = 1000. Total = 4000).

 

R1(config)#int f1/1

R1(config-if)#ip ospf cost 1500

 

R5(config)#int f0/0

R5(config-if)# ip ospf cost 1500

R5(config)#int f0/1

R5(config-if)# ip ospf cost 1500

 

R4(config)#int f1/0

R4(config-if)# ip ospf cost 1500

 

3. Verify that traffic to the 10.1.2.0/24 network from R1 is load balanced via both R2 and R5.

R1#sh ip route

Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP

       D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area

       N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2

       E1 - OSPF external type 1, E2 - OSPF external type 2

       i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2

       ia - IS-IS inter area, * - candidate default, U - per-user static route

       o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP

       + - replicated route, % - next hop override

 

Gateway of last resort is not set

 

10.0.0.0/8 is variably subnetted, 12 subnets, 2 masks

C 10.0.0.0/24 is directly connected, FastEthernet0/0

L 10.0.0.1/32 is directly connected, FastEthernet0/0

C 10.0.1.0/24 is directly connected, FastEthernet0/1

L 10.0.1.1/32 is directly connected, FastEthernet0/1

C 10.0.2.0/24 is directly connected, FastEthernet1/0

L 10.0.2.1/32 is directly connected, FastEthernet1/0

C 10.0.3.0/24 is directly connected, FastEthernet1/1

L 10.0.3.1/32 is directly connected, FastEthernet1/1

O 10.1.0.0/24 [110/2000] via 10.0.0.2, 00:05:57, FastEthernet0/0

O 10.1.1.0/24 [110/3000] via 10.0.0.2, 00:00:38, FastEthernet0/0

O 10.1.2.0/24 [110/4000] via 10.0.3.2, 00:00:25, FastEthernet1/1

  [110/4000] via 10.0.0.2, 00:00:25, FastEthernet0/0

O 10.1.3.0/24 [110/3000] via 10.0.3.2, 00:00:25, FastEthernet1/1

192.168.0.0/32 is subnetted, 5 subnets

C 192.168.0.1/32 is directly connected, Loopback0

O 192.168.0.2/32 [110/1012] via 10.0.0.2, 00:05:57, FastEthernet0/0

O 192.168.0.3/32 [110/2012] via 10.0.0.2, 00:05:57, FastEthernet0/0

O 192.168.0.4/32 [110/3012] via 10.0.3.2, 00:00:25, FastEthernet1/1

     [110/3012] via 10.0.0.2, 00:00:25, FastEthernet0/0

O 192.168.0.5/32 [110/1512] via 10.0.3.2, 00:00:38, FastEthernet1/1

 

Additional Resources

 

Understanding the OSPF Link-State Routing Protocol: https://www.ciscopress.com/articles/article.asp?p=2262897&seqNum=5

OSPF Metric cost Calculation Formula Explained: https://www.computernetworkingnotes.com/ccna-study-guide/ospf-metric-cost-calculation-formula-explained.html

What is OSPF Metric value Cost and OSPF default Cost Reference Bandwidth: https://www.omnisecu.com/cisco-certified-network-associate-ccna/what-is-ospf-metric-value-cost-and-ospf-default-cost-reference-bandwidth.php

 

Want to practice Cisco CCNA technologies on your laptop? Download my complete 350-page Cisco CCNA Lab Guide for free.

 

Click Here to get my Cisco CCNA Gold Bootcamp, the highest rated CCNA course online with a 4.8 star rating from over 20,000 public reviews.

Libby Teofilo

Text by Libby Teofilo, Technical Writer at www.flackbox.com

Libby’s passion for technology drives her to constantly learn and share her insights with others. When she’s not immersed in the world of tech, you’ll find her lost in a good book with a cup of coffee or out exploring nature on her next adventure—always curious, always inspired.