Introduction
Cost is the metric, OSPF uses to judge a path’s feasibility. Normally to adjust cost on a route you would adjust the cost value on an interface itself. This, however, affects all routes learned via this interface.
If router learns multiple routes to a destination,
1) Router first finds longest prefix match in IP routing table to find outgoing interface.
2) AD: If these multiple routes have same subnet mask learned via different protocol on router then router chooses lowest administrative distance (AD).
Example: Router R1 learned 10.1.1.0/24 prefix via OSPF as well as by RIP, router chooses OSPF route as best and install in its routing table, because OSPF (110) has lower AD value than RIP (120)
3) If these multiple routes have same subnet mask learned via same protocol on router then router chooses lowest metric between them.
For Example: Eigrp use composite “metric” and Ospf uses “Cost” for comparison.
Configuration overview:
Unlike the metric in RIP which is determined by hop count and EIGRP’s long mathematical formulated metric, OSPF is a little simpler.
OSPF automatic cost calculation is an inverse function of bandwidth of an interface. As higher the bandwidth values of an interface, the lower the cost value.
Formula for OSPF cost calculation is:
Interface Cost= Reference bandwidth/interface bandwidth
For cost calculation default reference bandwidth value used is 100Mbps (10^8).
Hence By default Interface Cost= 10^8/(interface bandwidth)
This mean for 100Mbps fast Ethernet will have a cost of 1 and furthermore all interface with higher bandwidth.The result of this is that OSPF routers in the network cannot make an accurate path calculation when comparing fast Ethernet interface versus a gigabit Ethernet interface as both interfaces has a cost of 1.
In order to resolve this, the reference bandwidth value can modified to allow higher bandwidth interface to have more granular cost value.
The "auto-cost reference-bandwidth" command allows you to change the reference bandwidth that OSPF uses to calculate its metrics:
Router#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#router ospf 100
Router(config-router)#auto-cost reference-bandwidth 1000
% OSPF: Reference bandwidth is changed.
Please ensure reference bandwidth is consistent across all routers.
Router(config-router)#exit
It is important to set the same reference bandwidth on all routers throughout the OSPF domain. Recall that OSPF allows every router to calculate its own routing table based on the LSAs that they receive. So they must all agree on the relationship between costs and bandwidth. Suppose you set the reference bandwidth differently on two routers, so that they advertise different link costs for their Ethernet interfaces. This could cause seriously strange routing patterns as OSPF will try to avoid using the higher cost links. For example, that a Fast Ethernet interface on one router is faster than a Gigabit Ethernet interface on the other route.
Since the OSPF cost derives from interface bandwidth, changing the interface bandwidth value should change the cost.
Router#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#interface Ethernet0
Router(config-if)#bandwidth 10000000
Router(config-if)# exit
The other way of affecting the cost of an interface is through the "ip ospf cost" command on the interface itself, this will override the calculated cost.
Router1(config)#int f1/0
Router1(config-if)#ip ospf cost 12345
Router(config-if)# exit
You can also set the cost per neighbor using the "neighbor (neighbor-ip) cost value" command. This command is valid only on OSPF network types that allow neighbor command like point-to-multipoint non broadcast.
Router(config)#router ospf 100
Router(config-router)#neighbor 10.1.1.2 cost 5000
Router(config-router)#neighbor 10.1.1.3 cost 2000
Router(config-router)#exit
Verification commands
1) Router#sh ip ospf interface fa0/0 | in Cost
Process ID 100, Router ID 10.1.1.2, Network Type BROADCAST, Cost: 10
2)Router#sh ip ospf interface brief
Interface PID Area IP Address/Mask Cost State Nbrs F/C
Fa0/0 100 0 10.1.1.2/24 10 BDR 1/1
Related Information
Base Initial configuration:
OSPF design guide
Understanding OSPF Cost