Ospf Metric Cost Calculation Formula Explained
Ospf Metric Cost Calculation Formula Explained
SPF
algorithm was invented in 1956 by Edsger W. Dijkstra. It is also referred as Dijkstra algorithm. SPF
is a quite complex algorithm. In this tutorial we will explain a simplified overview of this
algorithm.
This tutorial is the last part of our article OSPF Routing Protocol Explained with examples". You
can read other parts of this article here.
http://computernetworkingnotes.com/ccna-study-guide/ospf-fundamental-terminology-
explained.html
This tutorial is the first part of this article. In this part we explained basic terminology of OSPF such as
Feature , Advantage and Disadvantage, Autonomous System, Area concept, ABR, IR, Link, State ,LSA and
LSDB with example.
http://computernetworkingnotes.com/ccna-study-guide/ospf-neighborship-condition-and-
requirement.html
This tutorial is the second part of this article. OSPF neighborship is built between two routers only if
configuration value of Area ID, Authentication, Hello and Dead interval, Stub Area and MTU are matched.
This part explains these parameters and OSPF adjacency in detail with examples.
http://computernetworkingnotes.com/ccna-study-guide/ospf-neighbor-states-explained-with-
example.html
This tutorial is the third part of this article. OSPF adjacency process goes through the seven states; OSPF
State down, OSPF State Init, OSPF State two ways, OSPF State Exstart, OSPF State Exchange, OSPF
State Loading and OSPF State full. This part explains these states with DR BDR selection process in detail
with examples.
http://computernetworkingnotes.com/ccna-study-guide/ospf-configuration-step-by-step-
guide.html
This tutorial is the fourth part of this article. Configuration part of OSPF includes process ID, Area ID and
wildcard mask which make its setup a litter bit harder. This part explains these parameters in detail with
examples.
Shortest Path First (SPF) Algorithm
As we know upon initialization or due to any change in routing information an OSPF router
generates a LSA. This LSA (Link State Advertisement) contains the collection of all link-states on
that router. Router propagates this LSA in network. Each router that receives this LSA would store
a copy of it in its LSA database then flood this LSA to other routers.
To learn how this happens, please see third part of this article. In that part I explained this process in detail with
examples.
After database is updated, router selects a single best route for each destination from all available
routes. Router uses SPF algorithm to select the best route.
Just like other routing algorithm SPF also uses a metric component called cost to select the best
route for routing table.
Logically a packet will face more overhead in crossing a 56Kbps serial link than crossing a
100Mbps Ethernet link. Respectively it will take less time in crossing a higher bandwidth link than
a lower bandwidth link. OSPF uses this logic to calculate the cost. Cost is the inverse proportional
of bandwidth. Higher bandwidth has a lower cost. Lower bandwidth has a higher cost.
Reference bandwidth was defined as arbitrary value in OSPF documentation (RFC 2338). Vendors
need to use their own reference bandwidth. Cisco uses 100Mbps (108) bandwidth as reference
bandwidth. With this bandwidth, our equation would be
Key points
Now we know the equation, lets do some math and figure out the default cost of some essential
interfaces.
Default cost of essential interfaces.
FastEthernet
100Mbps 100000000/100000000 = 1 1
Link
Best route for routing table = Route which has the lowest cumulative cost
Summary
OSPF uses SPT tree to calculate the best route for routing table.
A SPT tree cannot grow beyond the area. So if a router has interfaces in multiple areas, it
needs to build separate tree for each area.
SPF algorithm calculates all possible routes from source router to destination network.
Cumulative cost is the sum of the all costs of the outgoing OSPF interfaces in the path.
While calculating cumulative cost, OSPF consider only outgoing interfaces in path. It does
not add the cost of incoming interfaces in cumulative cost.
If multiple routes exist, SPF compares the cumulative costs. Route which has the lowest
cumulative cost will be chosen for routing table.
Now we have a basic understanding of SPF algorithm. In remaining part this tutorial we will
explain how SPF algorithm selects the best route from available routes.
For demonstration purpose we will use same network topology which we have created in previous
part of this article.
Run show ip route ospf command from privilege mode to view all learned routes through the
OSPF protocol.
As output shows, Router0 has six routes from OSPF in routing table. We will go through the each
route and find out why it was chosen as the best route for routing table by OSPF.
Route 20.0.0.0
We have three routes to get 20.0.0.0/8 network. Lets calculate the cumulative cost of each route.
Via route R0 R3 R4 R6
Via route R0 R5 R6
Route 192.168.0.4
Via Route R0 R1
R0s Serial 0/0/0 cost (1562) + R1s Serial 0/0/1 cost (1562) = 3124 (Cumulative cost)
Via Route R0 R3 R4 R6 R2
R0s Serial 0/0/1 cost (64) + R3s Serial 0/0/0 cost (64) + R4s Serial 0/0/1 cost (64) + R6s Serial
0/0/0 cost (64) + R2s Serial 0/0/1 cost (64) = 320 (Cumulative cost)
Via Route R0 R5 R6 R2
Ros FastEthernet 0/1 cost (1) + R5s FastEthernet 0/0 cost (1) + R6s Serial 0/0/0 cost (64) +R2s
Serial 0/0/1 cost (64) = 130 (Cumulative cost)
Among these routes, Route R0 R5 R6 R2 has the lowest cost so it was picked for routing table.
Route 192.168.0.8
Via Route R0 R1
R0s Serial 0/0/0 cost (1562) + R1s Serial 0/0/1 cost (1562) + R2s Serial 0/0/0 (1562) = 4686
(Cumulative cost)
Via Route R0 R3 R4 R6
R0s Serial 0/0/1 cost (64) + R3s Serial 0/0/0 cost (64) + R4s Serial 0/0/1 cost (64) + R6s Serial
0/0/0 cost (64) = 256 (Cumulative cost)
Via Route R0 R5 R6
Ros FastEthernet 0/1 cost (1) + R5s FastEthernet 0/0 cost (1) + R6s Serial 0/0/0 cost (64) = 66
(Cumulative cost)
Among these routes, Route R0 R5 R6 has the lowest cost so it was picked for routing table.
Route 192.168.1.4
Via Route R0 R1 R2 R6
R0s Serial 0/0/0 cost (1562) + R1s Serial 0/0/1 (1562) + R2s Serial 0/0/0 (1562) + R6s
FastEthernet 0/0 (1) = 4687 (Cumulative cost)
Via R0 R3 R4 R6
R0s Serial 0/0/1 cost (64) + R3s Serial 0/0/0 cost (64) + R4s Serial 0/0/1 cost (64) + R6s
FastEthernet 0/0 (1) = 193
Via R0 R5
R0s FastEthernet 0/1 cost (1) + R5s FastEthernet 0/0 cost (1) = 2
Among these routes, Route R0 R5 has the lowest cost so it was selected as the best route.
Route 192.168.2.4
Via Route R0 R1 R2 R6 R4
R0s Serial 0/0/0 cost (1562) + R1s Serial 0/0/1 cost (1562) + R2s Serial 0/0/0 cost (1562) + R6s
Serial 0/0/1 cost (64) + R4s Serial 0/0/0 cost (64) = 4814
Via Route R0 R5 R6 R4
R0s FastEthernet 0/1 cost (1) + R5s FastEthernet 0/0 cost (1) + R6s Serial 0/0/1 (64) + R4s Serial
0/0/0 cost (64) = 130
Via Route R0 R3
R0s Serial 0/0/1 cost (64) + R3s serial 0/0/0 cost (64) = 128
Among these routes, Route R0 - R3 has the lowest cost for destination 192.168.2.4.
Route 192.168.2.8
Via Route R0 R3 R4
R0s Serial 0/0/1 cost (64) + R3s Serial 0/0/0 cost (64) + R4s Serial 0/0/1 cost (64) = 192
Via Route R0 R1 R2 R6
Ros Serial 0/0/0 cost (1562) + R1s Serial 0/0/1 cost (1562) + R2s Serial 0/0/0 cost (1562) + R6s
Serial 0/0/1 cost (64) = 4750
Via Route R0 R5 R6
R0s FastEthernet 0/1 cost (1) + R5s FastEthernet 0/0 cost (1) + R6s Serial 0/0/1 cost (64) = 66
Sub interface mode command Bandwidth is used to set the bandwidth of supported interface.
If bandwidth is set through this command, OSPF will use it. If bandwidth is not set, it will use
interfaces default bandwidth.
When we enable an interface, router automatically assign a bandwidth value to it based on its type.
For example serial interface has a default bandwidth value of 1544k. Until we change this value
with bandwidth command, it will be used where it is required.
Let me clear one more thing about bandwidth. Changing default bandwidth with bandwidth
command does not change actual bandwidth of interface. Neither default bandwidth nor
bandwidth set by bandwidth command has anything to do with actual layer one link bandwidth.
Then what purpose does this command solve?
This command is only used to influence the routing protocol which uses bandwidth in route
selection process such as OSPF and EIGRP.
We have already seen an example of this method in our example. We changed default bandwidth
(1544Kbps) to custom (64kbps) bandwidth on R0s serial 0/0/0, R1s serial 0/0/1 and R2s serial
0/0/0. Due to this change R0 took another router for 192.168.0.4 network.
Via Route R0 R1
R0s Serial 0/0/0 cost (1562) + R1s Serial 0/0/1 cost (1562) = 3124 (Cumulative cost)
Via Route R0 R5 R6 R2
Ros FastEthernet 0/1 cost (1) + R5s FastEthernet 0/0 cost (1) + R6s Serial 0/0/0 cost (64) +R2s
Serial 0/0/1 cost (64) = 130 (Cumulative cost)
Via Route R0 R3 R4 R6 R2
R0s Serial 0/0/1 cost (64) + R3s Serial 0/0/0 cost (64) + R4s Serial 0/0/1 cost (64) + R6s Serial
0/0/0 cost (64) + R2s Serial 0/0/1 cost (64) = 320 (Cumulative cost)
Among these routes, Route R0 R5 R6 R2 has the lowest cost so it was picked for routing table.
Well Which route would have selected, if we had used default bandwidth?
Via Route R0 R1
R0s Serial 0/0/0 cost (64) + R1s Serial 0/0/1 cost (64) = 128 (Cumulative cost)
Via Route R0 R5 R6 R2
Ros FastEthernet 0/1 cost (1) + R5s FastEthernet 0/0 cost (1) + R6s Serial 0/0/0 cost (64) +R2s
Serial 0/0/1 cost (64) = 130 (Cumulative cost)
Via Route R0 R3 R4 R6 R2
R0s Serial 0/0/1 cost (64) + R3s Serial 0/0/0 cost (64) + R4s Serial 0/0/1 cost (64) + R6s Serial
0/0/0 cost (64) + R2s Serial 0/0/1 cost (64) = 320 (Cumulative cost)
Among these routes, Route R0 R1 has the lowest cost value so it would be selected for routing
table. Thus by changing interface bandwidth we actually influenced route selection process.
By changing reference bandwidth value
Route R2 R3
In this route we have two exit points. Both points have default 1oo Mbps speed.
Route R2 R1 R3
In this route we have three exit points. Two exit points (R2 and R1) have 1 Gbps link.
With default reference bandwidth R2 will choose Route R2 R3, which is not good.
Sadly packet tracer does not include this command. For the practice of this command please use other simulator
software which support this command or use real router.
Lets change reference bandwidth to 1000Mbps on all three routers using following commands
Route R2 R3
Route R2 R1 R3
In this case Route R2-R1-R3 will be selected, which is the shortest route for destination.
Thats all for this article. I hope now you have better understanding of OSPF Routing protocol. In
next article I will explain Access List in detail with examples.