OSPF Metric - Cost

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.

 

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.

				
					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
				
			
				
					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
				
			
				
					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
				
			
				
					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#
				
			
				
					 We apply this to every router - R1 through R8
router ospf 1
 auto-cost reference-bandwidth 40000
				
			

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

				
					R1# conf t
Enter configuration commands, one per line.  End with CNTL/Z.
R1(config)# interface Ethernet0/0
R1(config-if)# ip ospf cost ?
  <1-65535>  Cost
R1(config-if)# ip ospf cost 50
R1(config)# interface Ethernet0/1
R1(config-if)# ip ospf cost 100
R1(config)# interface Ethernet0/2
R1(config-if)# ip ospf cost 200
R1(config-if)# end
R1#
				
			

You can see how we can directly configure the OSPF Cost of a link. The output of the sh ip ospf interface brief command shows the Cost values we configured under the interfaces.

				
					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
Et0/2        1     0               10.1.3.1/24        200   BDR   1/1
Et0/1        1     0               10.1.2.1/24        100   BDR   1/1
Et0/0        1     0               10.1.1.1/24        50    BDR   1/1
				
			

This method is typically used when you want to influence the OSPF traffic path for a particular interface. For example, an interface experiences some packet loss, and you want to move all traffic away from the interface. What do you do? You configure a very high OSPF cost on that link (for example, cost 1,500), and the SPF algorithm makes it less preferred and doesn’t use it as the best path to any destination.

 

Method 3. The interface bandwidth

The third method of manipulating the OSPF cost is to change the interface bandwidth. Each router interface has a default bandwidth value that represents the interface speed.  
By default, the interface bandwidth value matches the physical transmission speed of the link, as shown in the example below. For example, the bandwidth of the Ethernet interface is set to 10Mbps. The bandwidth of the FastEthernet interface is set to 100Mbps. The bandwidth of the GigabitEthernet interface is set to 1000Mbps and so on.

				
					R1# show interfaces Eth0/0
Ethernet0/0 is up, line protocol is up
  Hardware is AmdP2, address is aabb.cc00.1000 (bia aabb.cc00.1000)
  Internet address is 10.1.1.1/24
  MTU 1500 bytes, BW 10000 Kbit/sec, DLY 1000 usec,
     reliability 255/255, txload 1/255, rxload 1/255
  Encapsulation ARPA, loopback not set
  Keepalive set (10 sec)
  ARP type: ARPA, ARP Timeout 04:00:00
  Last input 00:00:01, output 00:00:03, output hang never
  Last clearing of "show interface" counters never
  Input queue: 0/75/0/0 (size/max/drops/flushes); Total output drops: 0
  Queueing strategy: fifo
  Output queue: 0/40 (size/max)
  5 minute input rate 0 bits/sec, 0 packets/sec
  5 minute output rate 0 bits/sec, 0 packets/sec
     14144 packets input, 2194919 bytes, 0 no buffer
     Received 14115 broadcasts (0 IP multicasts)
     0 runts, 0 giants, 0 throttles
     0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
     0 input packets with dribble condition detected
     25123 packets output, 2863273 bytes, 0 underruns
     Output 14198 broadcasts (0 IP multicasts)
     0 output errors, 0 collisions, 2 interface resets
     7 unknown protocol drops
     0 babbles, 0 late collision, 0 deferred
     0 lost carrier, 0 no carrier
     0 output buffer failures, 0 output buffers swapped out
				
			

Notice something very important: The interface bandwidth does not influence the interface’s actual transmission speed and does not necessarily need to match the transmission speed. For example, we can change the interface bandwidth of R1’s Ethernet0/0 interface to 40,000Mbps even though the interface cannot physically transmit at that speed.

The interface bandwidth has only a representative purpose.

Let’s reconfigure the interface bandwidth of R1’s Eth0/0 to 40,000Mbps.

				
					R1# conf t
Enter configuration commands, one per line.  End with CNTL/Z.
R1(config)# interface Eth0/0
R1(config-if)# bandwidth ?
  <1-200000000>  Bandwidth in kilobits
  inherit        Specify how bandwidth is inherited
  qos-reference  Reference bandwidth for QOS
  receive        Specify receive-side bandwidth
R1(config-if)# bandwidth 40000000
R1(config-if)# end
R1#
				
			

Now, if we check the interface bandwidth, we can see it is not set to the physical interface speed of 10Mbps but to the user-defined value of 40,000Mbps.

				
					R1# sh interface Eth0/0
Ethernet0/0 is up, line protocol is up
  Hardware is AmdP2, address is aabb.cc00.1000 (bia aabb.cc00.1000)
  Internet address is 10.1.1.1/24
  MTU 1500 bytes, BW 40000000 Kbit/sec, DLY 1000 usec,
     reliability 255/255, txload 1/255, rxload 1/255
  Encapsulation ARPA, loopback not set
  Keepalive set (10 sec)
  ARP type: ARPA, ARP Timeout 04:00:00
  Last input 00:00:00, output 00:00:00, output hang never
  Last clearing of "show interface" counters never
  Input queue: 0/75/0/0 (size/max/drops/flushes); Total output drops: 0
  Queueing strategy: fifo
  Output queue: 0/40 (size/max)
  5 minute input rate 0 bits/sec, 0 packets/sec
  5 minute output rate 0 bits/sec, 0 packets/sec
     14953 packets input, 2318360 bytes, 0 no buffer
     Received 14924 broadcasts (0 IP multicasts)
     0 runts, 0 giants, 0 throttles
     0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
     0 input packets with dribble condition detected
     26563 packets output, 3025605 bytes, 0 underruns
     Output 15021 broadcasts (0 IP multicasts)
     0 output errors, 0 collisions, 2 interface resets
     7 unknown protocol drops
     0 babbles, 0 late collision, 0 deferred
     0 lost carrier, 0 no carrier
     0 output buffer failures, 0 output buffers swapped out
				
			

Now, the OSPF cost of the interface has changed from 40000/10 (Cost 4000) to 40000/40000, which is equal to Cost 1.

Notice that other IOS features use the interface bandwidth to make decisions. It is not a good practice to change the default interface bandwidth unless really necessary. If you want to manipulate the OSPF best path decision, It is recommended to use the Reference Bandwidth value or the method of setting the OSPF cost directly on the interface using the ip ospf cost command. 

OSPF Cost (Open Shortest Path First)

1. What is OSPF Cost?

OSPF cost is a metric used by the routing protocol OSPF (Open Shortest Path First) to determine the best path to reach a destination network.
In simple words, cost = path preference value.
The lower the cost, the better the path.


2. Why OSPF Uses Cost?

OSPF may have multiple paths to reach the same destination. To select the best one, it compares the total cost of each path.
The path with the lowest cumulative cost is chosen as the best route.

3. How OSPF Cost is Calculated?

OSPF cost is based on bandwidth using the formula:

Cost = Reference Bandwidth / Interface Bandwidth\textbf{Cost = Reference Bandwidth / Interface Bandwidth}

👉 Default Reference Bandwidth = 100 Mbps (100,000,000 bps)


4. Common OSPF Cost Values (Default)

Interface SpeedCost CalculationOSPF Cost
10 Mbps100 / 1010
100 Mbps100 / 1001
1 Gbps100 / 10001 (rounded)

⚠️ Note: For high-speed links (like 1 Gbps or more), cost becomes 1 unless reference bandwidth is changed.

5. Manual Cost Configuration (Cisco)

You can manually change OSPF cost in **Cisco routers:

Command:

				
					interface GigabitEthernet0/0
ip ospf cost 20
				
			

👉 This overrides automatic calculation.

6. Changing Reference Bandwidth (Important in Real Networks)

To support modern high-speed networks:

				
					router ospf 1
auto-cost reference-bandwidth 1000
				
			

👉 Now:

  • 1 Gbps link → cost = 1
  • 100 Mbps → cost = 10

Key Points to Remember

  • OSPF prefers lowest cost path
  • Cost is inversely proportional to bandwidth
  • Total path cost = sum of all interface costs
  • Default reference bandwidth = 100 Mbps
  • Can be manually configured