OSPF Metric
Disclaimer: This is meant to be instructive to all networking folks, but as I am learning Juniper, all snippets and commands will be from JUNOS. Credit to Knox from CBT Nuggets as this was a valuable lesson.
Diagram:
As a link-state routing, OSPF utilizes cost as its metric to determine the best path. Lower cost equals a better path to OSPF. While sounding simple, there are a few caveats to determining what lower cost route gets put into the routing table.
OSPF Metric
OSPF uses a reference bandwidth to find the cost on a per link basis. If the reference bandwidth is 10Gbps, and the interface is 1Gbps, then the cost of that link is 10. Cisco uses a default reference bandwidth of 40Gbps while Juniper utilizes 100Mbps. OSPF cost is rounded up to 1 should the reference bandwidth be lower than the actual interface bandwidth. On Juniper, this can be verified by changing the interface bandwidth of a 1G interface, and checking how OSPF costs the link.
root@R4# set interfaces ge-0/0/2 unit 0 bandwidth 10m
root@R4# run show ospf interface extensive
Interface State Area DR ID BDR ID Nbrs
ge-0/0/2.0 BDR 0.0.0.50 1.1.1.1 4.4.4.4 1
Type: LAN, Address: 10.0.0.6, Mask: 255.255.255.252, MTU: 1500, Cost: 10
DR addr: 10.0.0.5, BDR addr: 10.0.0.6, Priority: 128
Adj count: 1
Hello: 10, Dead: 40, ReXmit: 5, Not Stub
Auth type: None
Protection type: None
Topology default (ID 0) -> Cost: 10
root@R4# set interfaces ge-0/0/2 unit 0 bandwidth 100m
[edit]
root@R4# run show ospf interface extensive
Interface State Area DR ID BDR ID Nbrs
ge-0/0/2.0 BDR 0.0.0.50 1.1.1.1 4.4.4.4 1
Type: LAN, Address: 10.0.0.6, Mask: 255.255.255.252, MTU: 1500, Cost: 1
DR addr: 10.0.0.5, BDR addr: 10.0.0.6, Priority: 128
Adj count: 1
Hello: 10, Dead: 40, ReXmit: 5, Not Stub
Auth type: None
Protection type: None
Topology default (ID 0) -> Cost: 1
Flood list:
The reference bandwidth can be changed on each router and that will change the cost for every interface participating in that OSPF process.
root@R4# set protocols ospf reference-bandwidth 100g
root@R4# run show ospf interface extensive ge-0/0/2.0
Interface State Area DR ID BDR ID Nbrs
ge-0/0/2.0 BDR 0.0.0.50 1.1.1.1 4.4.4.4 1
Type: LAN, Address: 10.0.0.6, Mask: 255.255.255.252, MTU: 1500, Cost: 100
DR addr: 10.0.0.5, BDR addr: 10.0.0.6, Priority: 128
Adj count: 1
Hello: 10, Dead: 40, ReXmit: 5, Not Stub
Auth type: None
Protection type: None
Topology default (ID 0) -> Cost: 100
Again, the cost as the formula is Cost = ReferenceBandwidth / Interface Bandwidth à Cost = 100G /1G à Cost = 100.
Cost is a straight-forward concept if we were to stop here; however, as I mentioned, there are some idiosyncrasies about determining cost. OSPF uses a bi-directional graph, so if the cost is manually changed on one end, it should be changed on the other to prevent asymmetric traffic behaviors.
Lets look at the following scenario and see what happens. If the link between R2 and R3 were to go down, and the link between R1 and R4 was costed to 1500. In that case, R2 has two different ways to get to R3s networks. Either, it can go to R1 for a cost of 100 (with the reference bandwidth set to 100G) and from R1 to R3 for another cost of 100 totaling 200. The other option is to go from R2 to R4 for 100, then R4 to R1 for 1500, and then from R1 to R3 for another 100 which totals to 1700. If lowest cost was the only factor, it should prefer the link to R1.
That is not the case though, it actually prefers the R2–>R4–> R1–>R3.
As we can see, R2 has a metric cost of 1700 to get to R3s loopback. This is due to how OSPF prefers routes. The traffic from R2 to R1 and then to R3 would go from Area 0 to Area 50, even though R3 is an Area 50 router. OSPF prefers IntraArea routes over InterArea routes. As R2 has a link into Area 50, it will prefer to take a path through area 50 for the routes that it has learned from Area 50.