04 · cost and spf

OSPF Cost and Route Selection Visualization

R1 reaches 10.5.5.0/24 behind R5 either directly over R2 in two hops or the long way through R3 and R4 in three. Cost is reference bandwidth divided by interface bandwidth, not hop count, so set the interface speeds and the reference bandwidth to see which path wins, when two paths tie for equal-cost multipath, and how route source outranks metric.

AREA 0single area, pure SPFA · cost 64Serial T1B · cost 1100 MbC · cost 11 GbD · cost 11 GbE · cost 11 GbF · cost 1010 Mbcost 1R11.1.1.1R21.1.1.2R31.1.1.3R41.1.1.4R51.1.1.510.5.5.0/24destination
Winning path: R1 → R3 → R4 → R5 → 10.5.5.0/24 at cost 4.
  • installed shortest path
  • in the database, not used

Best cost

4

Equal-cost paths

1

Reference bw

100 Mb

Candidates seen

4

Candidate paths, cheapest first

OSPF sees all of them in the database and installs only the minimum.

PathLinksPer-hop costTotal
R1 → R3 → R4 → R5 → 10.5.5.0/24C D E LAN1 + 1 + 1 + 14installed
R1 → R3 → R4 → R2 → R5 → 10.5.5.0/24C D F B LAN1 + 1 + 10 + 1 + 11410 worse
R1 → R2 → R5 → 10.5.5.0/24A B LAN64 + 1 + 16662 worse
R1 → R2 → R4 → R5 → 10.5.5.0/24A F E LAN64 + 10 + 1 + 17672 worse

Slide sideways to see the whole table.

Cost per interface speed

Truncated, never below 1.

Interfaceref 100ref 1000ref 10000ref 100000
Serial T164647647664766
10 Mb10100100010000
100 Mb1101001000
1 Gb1110100
10 Gb11110

Slide sideways to see the whole table.

R1(config)# router ospf 1
R1(config-ospf-1)#   auto-cost reference-bandwidth 100
R1(config-ospf-1)#   maximum-paths 4
R1(config)# interface 1/1/2
R1(config-if)#   ip ospf 1 area 0
R1(config-if)#   ip ospf cost 40      ! overrides the formula entirely
R1# show ip ospf routes

Route source beats metric, always

OSPF compares these ranks before it ever looks at cost.

RankCodeSourceNote
1OIntra-areaLearned from type 1 and 2 inside our own area.
2O IAInter-areaLearned from a type 3 summary at an ABR.
3O E1External type 1External metric plus the internal cost to the ASBR.
4O E2External type 2External metric only; internal cost ignored.

Slide sideways to see the whole table.

An intra-area route with cost 900 is preferred over an inter-area route with cost 3 for the same prefix. Longest prefix match still comes first: a /24 always beats a /16, whatever the code.

Three demonstrations worth doing live

Key point

Speed does not equal preference. Leave the defaults: A+B is two hops but costs 65, while C+D+E is three hops and costs 4. A hop-count protocol would have chosen the slower path.

Make a tie. Set A to 1 Gb and B to 1 Gb; both paths reach cost 3 and OSPF installs both. Point out maximum-paths and that load sharing is per-destination by default.

Break the reference bandwidth. Change it on one router only — in a real lab you get asymmetric routing and a mismatch warning, because cost is calculated locally but every router trusts what it is told.