This lesson discusses how OSPF determines the best paths to destinations using its metric called "OSPF Cost". It is a fundamental concept of the operation of the link-state routing protocol that is important for CCNA candidates to understand well.

In the Download section at the end of the lesson, you can find the EVE-NG file, which you can use to practice OSPF traffic engineering by manipulating the Cost of different paths. 

What is OSPF Cost?

Every dynamic routing protocol, such as OSPF, has a way to compare different paths to a destination and determine which one is best. Every protocol uses different methods of comparison based on different metric systems. OSPF uses a metric called OSPF Cost based on each hop's outgoing interface bandwidth.

Every router calculates the Cost of every OSPF-enabled link using a formula we will see later. The cost is a property of every link, as shown in the diagram below.

What is OSPF Cost?
Figure 1. What is OSPF Cost?

Every router then advertises the Cost of each link as part of the LSA Type 1 advertisement to neighbors. Since all routers within an area have all LSAs - in the end, all routers know the cost of all links in the area

For example, let's look at the LSA Type 1 advertisement for the router shown in Figure 1. Notice that in the LSA advertisement, the router describes its four links. The IP address, Subnet Mask, and Cost are properties of each link (highlighted in blue). Notice that the cost is called "Metric" in this output, but in the next output, it is called "Cost." Both terms are interchangeable and mean the same thing. The most commonly used term is Cost.

R1# sh ip ospf database router 1.1.1.1
            OSPF Router with ID (1.1.1.1) (Process ID 1)
                Router Link States (Area 0)
  LS age: 277
  Options: (No TOS-capability, DC)
  LS Type: Router Links
  Link State ID: 1.1.1.1
  Advertising Router: 1.1.1.1
  LS Seq Number: 80000021
  Checksum: 0xB6A0
  Length: 72
  Number of Links: 4
    Link connected to: a Stub Network
     (Link ID) Network/subnet number: 10.6.7.0
     (Link Data) Network Mask: 255.255.255.0
      Number of MTID metrics: 0
       TOS 0 Metrics: 1
    Link connected to: a Stub Network
     (Link ID) Network/subnet number: 10.3.1.0
     (Link Data) Network Mask: 255.255.255.0
      Number of MTID metrics: 0
       TOS 0 Metrics: 10
    Link connected to: a Transit Network
     (Link ID) Designated Router address: 10.1.0.1
     (Link Data) Router Interface address: 10.1.0.1
      Number of MTID metrics: 0
       TOS 0 Metrics: 1
    Link connected to: a Transit Network
     (Link ID) Designated Router address: 10.0.5.1
     (Link Data) Router Interface address: 10.0.5.1
      Number of MTID metrics: 0
       TOS 0 Metrics: 10

The cost of each interface can also be revealed using the show ip ospf interface brief command, as shown in the output below.

R1# sh ip ospf interface brief
Interface    PID   Area            IP Address/Mask    Cost  State Nbrs F/C
Lo0          1     0               172.16.1.1/24      1     P2P   0/0
Gi0/0        1     0               10.1.3.1/24        1     DR    1/1
Fa1/0        1     0               10.1.2.1/24        1     P2P   0/0
Et0/1        1     0               10.1.1.1/24        10    DR    1/1
Et0/2        1     0               10.1.1.1/24        10    P2P   0/0

Notice that the cost is an OSPF property of every interface participating in the routing process. Now, let's see how the cost is calculated.

OSPF Cost Formula 

The OSPF cost is calculated based on the bandwidth of the interface using the formula:

OSPF Cost Formula
Figure 2. OSPF Cost Formula

By default, the Reference Bandwidth is 100 Mbps. The Link Bandwidth is the actual bandwidth of the interface (in bits per second). Using the default Reference Bandwidth, we can calculate the cost of the following interface types:

Table 1. Cost of different interface types.
InterfaceFormula [Mbps]Cost
DSL (768 Kbps)100/0.768133
DS1 (1.544 Mbps)100/1.54464
Ethernet (10 Mbps)100/1010
FastEthernet (100 Mbps)100/1001
GigabitEthernet (1 Gbps)100/10001
TenGigabitEthernet (10 Gbps)100/100001
40-GigabitEthernet (40 Gbps)100/400001
100-GigabitEthernet (100 Gbps)100/1000001

Notice that when the interface bandwidth is greater than the reference bandwidth, the value is rounded to 1. For example, in the case of a 1Gbps interface - the cost is calculated as 100/1000, which is 0.1, but it is rounded to Cost 1.

From the table, it is obvious that every high-speed interface has the same Cost of 1. That's because the default Reference Bandwidth value of 100Mbps was set back in a time when interface speeds rarely surpassed 10 Mbps. However, even 100Mbps is considered slow by today's network standards.

How does OSPF Cost work?

OSPF calculates the sum of the costs for all outgoing interfaces in a path to a destination. Then, it compares the sum of each available path to the destination and chooses the route with the lowest total cost.

Let's take the following diagram as an example. There are three possible paths from R1 to subnet 172.16.8.0/24.

Initial Topology
Figure 3. Initial Topology

The following table compares the sum of the cost of the outgoing interfaces of each path. 

Table 1. The total cost of each available path is in Figure 3.
RouteTotal Cost [lower is better]
R1-R2-R3-R4-R5-R850
R1-R5-R6-R840
R1-R7-R830 (best)

As a result, R1 adds the route via R7 to its routing table as best for destination 172.16.8.0/24.

R1# sh ip route 172.16.8.0
Routing entry for 172.16.8.0/24
  Known via "ospf 1", distance 110, metric 30, type intra area
  Last update from 10.1.3.2 on Ethernet0/2, 00:00:03 ago
  Routing Descriptor Blocks:
  * 10.1.3.2, from 8.8.8.8, 00:00:03 ago, via Ethernet0/2
      Route metric is 30, traffic share count is 1

Using this technique, the link state protocol chooses the best routes to every destination in the network. It worked well in the old days when the typical interface speed was less than 100Mbps. However, since the default Reference Bandwidth value is 100Mbps, all high-speed links have the same cost of 1. This creates the following issue in today's high-speed networks. 

Look at the diagram below. Which path to destination 172.16.8.0/24 will R1 choose as best? Which one do you think is actually the best path? 

Issue with high-speed interfaces
Figure 4. The issue with high-speed interfaces.

Let's compare the three available routes to 172.16.8.0/24 in the context of the link state protocol.

Table 2. The total cost of each available path is in Figure 4.
PathTotal Cost [lower is better]
R1-R2-R3-R4-R5-R85
R1-R5-R6-R84
R1-R7-R83 (best)

According to the default OSPF best-path algorithm, the path R1-R7-R8 is the best to 172.16.8.0/24. However, is it really?

If you look at the diagram again, you can easily figure out that you can send 40 Gbps over the path via R2-R3-R4-R8. Also, you can transmit 10 Gbps via R5-R6-R8. However, over the path via R7-R8, you can only transfer 1Gbps, so the path that OSPF has chosen as best is actually the slowest of the three

The problem is that because of the default reference bandwidth of 100Mbps, the calculated cost of every link is 1. Fortunately, the protocol was designed to account for that and allow us to change the reference bandwidth. Let's see how.

Manipulating the OSPF Cost

Since the metric formula has three components, there are three ways to manipulate the OSPF cost of links, as shown in the diagram below. 

Three ways to manipulate the OSPF Cost
Figure 5. Three ways to manipulate the OSPF Cost
  1. You can change the Reference Bandwidth value of a router.
  2. You can directly change the OSPF cost of a particular link.
  3. You can change the Link Bandwidth of a particular link.

Generally, each method is used to achieve different objectives. Let's walk through each one and see when and how to use it.

Method 1. The Reference Bandwidth

Cisco set the Reference Bandwidth to 100Mbps decades ago when interface speeds barely surpassed 10 Mbps. Back then, the cost formula worked great. However, nowadays, if OSPF uses the default value, it treats a 100Mbps interface as having the same cost as a 40 Gbps interface, which is obviously not right from the best-path algorithm perspective. To account for that, Cisco IOS routers allow us to change the Reference Bandwidth value, as shown in the output below.

R1# conf t
Enter configuration commands, one per line.  End with CNTL/Z
R1(config)# router ospf 1
R1(config-router)# auto-cost reference-bandwidth ?
  <1-4294967>  The reference bandwidth in terms of Mbits per second
R1(config-router)# auto-cost reference-bandwidth 40000
% OSPF: Reference bandwidth is changed.
        Please ensure reference bandwidth is consistent across all routers.
R1(config-router)# end
R1#

The configuration change is made per device and takes effect immediately. Once we configure the Reference Bandwidth to 40,000 Mbps (40Gbps), the formula changes, as shown in the diagram below.

Reference Bandwidth set to 40 Gbps
Figure 6. Reference Bandwidth set to 40 Gbps.

The result of this change is shown in the following table. The routing protocol can now differentiate that a 100Mbps link is slower than a 10 Gbps link, which leads to a more optimal best-path selection.

Table 3. Cost of different interface types when Ref Bandwidth is set to 40 Gbps.
InterfaceFormula [Mbps]Cost
DSL (768 Kbps)40000/0.76852084
DS1 (1.544 Mbps)40000/1.54425907
Ethernet (10 Mbps)40000/104000
FastEthernet (100 Mbps)40000/100400
GigabitEthernet (1 Gbps)40000/100040
TenGigabitEthernet (10 Gbps)40000/100004
40-GigabitEthernet (40 Gbps)40000/400001
100-GigabitEthernet (100 Gbps)40000/1000001

Now, let's see what happens when configuring this on every router in the topology (the change is performed per device).

! We apply this to every router - R1 through R8
router ospf 1
 auto-cost reference-bandwidth 40000

The following diagram reflects the new link metric when the Reference Bandwidth value is set to 40,000 Mbps on every router in the topology.

Best path selection with ref.bandwidth set to 40Gbps
Figure 7. Best path selection with reference bandwidth set to 40Gbps

Now, the routing protocol calculates the following values for each of the three available paths from R1 to 172.16.8.0/24.

Table 3. The total cost of each available path is in Figure 6.
PathTotal Cost [lower is better]
R1-R2-R3-R4-R5-R85 (best)
R1-R5-R6-R813
R1-R7-R881

You can see that now the best route is via R2-R3-R4-R8, which is indeed the fastest possible path to the destination because it allows end-to-end data transmission at a 40 Gbps rate.

There are two very important recommendations when it comes to the OSPF Reference Bandwidth:

  • Set the Reference Bandwidth to a value equal to the fastest link in the network. 
    • For example, if the fastest link in the domain is 40 Gbps, you set the Reference Bandwidth to 40 Gbps.
  • Ensure that all routers in the network are configured with the same auto-cost reference-bandwidth value.
    • Since the auto-cost reference-bandwidth value is configured on each device separately, changing it across the entire network is susceptible to misconfiguration. For example, you can forget to reconfigure one of the devices, which stays with the default value of 100Mbps. This can lead to suboptimal routing. Always ensure that every device is configured with the same reference bandwidth.

Method 2. The interface subcommand

There is another more granular and direct way to manipulate the cost of a link. We can directly configure it under the interface configuration level, as shown in the output below:

Full Content Access is for Registered Users Only (it's FREE)...

  • Learn any CCNA, DevNet or Network Automation topic with animated explanation.
  • We focus on simplicity. Networking tutorials and examples written in simple, understandable language for beginners.