OSPF path preference

OSPF chooses by route type before cost: intra-area routes, from type 1 and 2 LSAs, always beat inter-area routes, which beat external type 1, which beats external type 2; NSSA N1 and N2 sit beside E1 and E2. So a higher-cost path can win. A type 1 external adds the path cost to the ASBR; a type 2 keeps the seed metric, so equal ones need another tie-breaker.

In Daveth Pro (Network)11 min video · lab: OSPF v2/v3

By , founder of Daveth · Updated

Opening frame of the video walkthrough of OSPF path preference

Watch this lesson

11 min · Video walkthrough

In Daveth Pro (Network)

The full 11 min walkthrough is in Daveth Pro (Network). See what the plan opens

Upcoming practical lab

Lab R3: OSPF v2/v3

What the lab covers

What this page answers

How does OSPF choose between routes?

By route type first: intra-area, then inter-area, then external type 1, then external type 2, with NSSA N1 and N2 beside E1 and E2. Only within the same type does the lowest cost win, so an intra-area path beats a cheaper inter-area one. Read it in “Rule one: the type is read before the cost”

What is the difference between OSPF E1 and E2 routes?

An E1 route's metric grows with the path cost to the ASBR on top of the seed metric; an E2 route keeps the seed metric whatever the distance. Type 1 is preferred over type 2 before metrics are compared. Read it in “Externals, type 1: the distance to the ASBR counts”

A routing table that looks wrong

Here's a routing table that looks like a mistake. R1 and R2 share a link in area 0 at cost 1. Everything else is area 1: R1 to R3 at cost 10, R2 to R4 at cost 10, and a slow serial link from R3 to R4 at cost 100. R4 has the network 10.4.4.0/24.

Network drawing: area 0 · cost 1, area 1 · cost 10, serial · cost 100, 10.4.4.0/24, 10.13.1.3 (R3), R1, R2, R3, R4, LAN

R1 and R2 are the ABRs: each has a link in area 0 and links in area 1. The terminals in this section come from these four routers.

Before you read on: which way does R1 send traffic to 10.4.4.0/24, and what metric will its routing table show? Add it up both ways.

R1# show ip route ospf | begin Gateway
Gateway of last resort is not set

! subnet header lines omitted
O        10.4.4.0 [110/111] via 10.13.1.3, 00:01:30, GigabitEthernet0/1
! metric 111, through R3
O        10.24.1.0 [110/120] via 10.13.1.3, 00:01:30, GigabitEthernet0/1
O        10.34.1.0 [110/110] via 10.13.1.3, 00:01:30, GigabitEthernet0/1
R1#

Metric 111, next hop 10.13.1.3: R3, across the serial link. Through R2 the sum is 1 + 10 + 1 = 12. R1 had a path nine times cheaper and didn't take it.

It isn't a miscount. R1 skipped the fast Ethernet path through R2 and took the slow serial link through R3 because the serial path is intra-area, and an intra-area path is selected even when an inter-area path has the lower total metric. The route's own entry, captured at another moment, says which kind it is:

R1# show ip route 10.4.4.0
Routing entry for 10.4.4.0/24
 Known via "ospf 1", distance 110, metric 111, type intra a…
 Last update from 10.13.1.3 on GigabitEthernet0/1, 00:00:42
 Routing Descriptor Blocks:
 * 10.13.1.3, from 10.34.1.4, 00:00:42 ago, via GigabitEthe…
     Route metric is 111, traffic share count is 1

The second line is cut short at "type intra a": it reads "type intra area". Why is the R2 path inter-area at all? R1's link to R2 sits in area 0, so what R1 hears about 10.4.4.0/24 from that side is R2's type 3 summary, the signpost from the LSDB lesson; R1 is itself an ABR with a foot in each area. Two routes to one network, of two different types.

That's the lesson in one entry. OSPF reads the type of a route before it reads the cost, and the cost only settles a contest between routes of the same type. The rest of this page rebuilds the rulebook that produced metric 111, one rule at a time, and then uses it.

Where the numbers come from

First, the numbers the rulebook compares. In [110/111] the first number is the AD and the second is the metric of the path, and from OSPF's point of view path cost and path metric are the same thing.

OSPF runs Dijkstra's SPF algorithm to build a loop-free topology of shortest paths, and every router uses the same logic to calculate the shortest path for each network. The path metric is the cumulative interface cost from the router to the destination, and each interface's cost comes from one formula: cost equals the reference bandwidth divided by the interface bandwidth.

The default reference bandwidth is 100 Mbps. Run it. A FastEthernet interface: 100 / 100 = 1. A 10-Gigabit interface: 100 / 10,000 is a hundredth, and there's no difference in cost between a Fast Ethernet interface and a 10-Gigabit Ethernet interface: both cost 1. So at the default, anything at 100 Mbps or faster looks the same to OSPF.

Raise the reference bandwidth and the faster interfaces get different costs again. The command is auto-cost reference-bandwidth <bandwidth-in-mbps> under the OSPF process, and it changes the reference for every OSPF interface in that process. At 10000, a 1-Gbps interface works out at 10 and a 10-Gbps one at 1.

Now the catch. If you change the reference bandwidth on one router, change it on all of them, so SPF uses the same logic and you don't get routing loops; the same reference everywhere is the best practice. One router alone at 10000 sees its gigabit links ten times dearer than its neighbors see theirs.

Or skip the formula. ip ospf cost <1-65535> under the interface sets the cost by hand. The ceiling is real: the LSA's metric field is 16 bits, so an interface cost can't go past 65,535. A path can, though: the path metric can exceed 65,535 because the link costs are added up locally.

File away a trap answer: NX-OS uses a default reference of 40,000 Mbps. On IOS routers the answer is 100 Mbps.

Where you read these: the cost is among show ip ospf interface's fields, the reference bandwidth among show ip ospf's, and the maximum paths for load balancing among show ip protocols'.

Rule one: the type is read before the cost

Here's the order. OSPF prioritizes paths as intra-area first, then inter-area, then external type 1, then external type 2. NSSA externals sit beside their twins: N1 with E1, N2 with E2.

Rule one: intra-area routes, from type 1 and type 2 LSAs, are always preferred over inter-area routes, from type 3 LSAs. "Always" is the word that produced metric 111.

Inside one type, cost decides. If several intra-area routes exist for the same network, the one with the lowest total path metric is installed in the OSPF RIB and then presented to the router's global RIB. A tie in metric installs both routes into the OSPF RIB. (How many can share comes later.)

Rulebook so far:

  1. Type first: intra, inter, E1/N1, E2/N2.
  2. Same type: lowest total; equal totals all go in.

Inter-area: the lowest total, through area 0

A different network, and you do the adding this time. R1 sits in area 123 with links to R2 (cost 10) and R3 (cost 20). Area 0 holds R2 to R4 (20), R2 to R3 (20) and R3 to R5 (5). Area 456 holds R4 to R6 (10) and R5 to R6 (10), and R6's network, 10.6.6.0/24, costs 1. Which way does R1 go to R6, and at what total?

Network drawing: area 123, area 0, area 456, cost 10, cost 20, cost 5, 10.6.6.0/24 · cost 1, R1, R2, R3, R4, R5, R6, LAN

A different network: R1 in area 123, a ladder of four routers in area 0, and R6 in area 456. Every cost is on its link; the totals are yours to add.

The rule for a network in a different area is the one you'd expect: the path with the lowest total path metric to the destination. A tie in metric installs both paths into the OSPF RIB. And one restriction: every inter-area path for a route must go through area 0 to be considered, the two-tier rule from the LSDB lesson turning up in path selection.

R1 uses R1 → R3 → R5 → R6, because its total of 35 beats the 40 of R1 → R2 → R4 → R6. Those totals reach R6, the router; to R6's network add the final 1: 36 against 41. Same winner.

Notice what R1 is adding, though. R1 can't see area 0's links: it adds its own cost to each ABR and the metric that ABR put in its type 3. The totals come out the same; how those metrics travel was the LSDB lesson's.

Rulebook: inter-area takes the lowest total, and only through area 0.

Externals, type 1: the distance to the ASBR counts

External routes are classified as type 1 or type 2, and here's the second type rule: type 1 routes are preferred over type 2 routes. Like rule one, it's read before the metric.

You've met the two metrics before. Call the redistribution metric the seed: the metric the ASBR gives the route as it redistributes it. A type 1 metric is the seed plus the total path metric to the ASBR, so it grows as the LSA travels away from the ASBR. A type 2 metric is the seed alone, the same for the router next to the ASBR as for one 30 hops away, and type 2 is OSPF's default external type. The LSDB lesson had the numbers: O E1 172.16.6.0 at 87 on R1 and 86 on R2, O E2 172.31.6.0 at 20 on both.

Now the choosing. The E1 calculation takes the redistribution metric plus the lowest path metric to reach the ASBR that advertised the network, so it's smaller near that ASBR and grows with every hop away from it. A tie in the path metric installs both routes into the RIB. And if the ASBR sits in a different area, the traffic's path must go through area 0: the inter-area restriction again.

One special case on an NSSA's ABR. An ABR doesn't install an O E1 and an O N1 for the same network at the same time: for a typical NSSA the O N1 is always given preference, and its presence keeps the O E1 off that ABR. The reason comes with the E2 version below.

Rulebook: E1 and N1 take the seed plus the lowest path to the ASBR; equal sums all go in.

Externals, type 2: the tie-breaker you can't see in the brackets

Type 2 routes don't increment in metric, whatever the path metric to the ASBR. So give two ASBRs the same network and the same seed, and the metric can't choose between them.

Another network. R1 sits in area 123 with links to R2 (cost 9) and R3 (cost 10). Area 0: R2 to R4 at 12, R3 to R5 at 10. Area 46: R4 to R6 at 10. Area 57: R5 to R7 at 10. R6 and R7 both redistribute 172.16.0.0/24, each with cost 20. Predict: which ASBR does R1 use, or does it use both?

Network drawing: area 123, area 0, area 46, area 57, cost 9, cost 10, 10.13.1.3, cost 12, 172.16.0.0/24, redistributed, cost 20, R1, R2, R3, R4, R5, R6 (ASBR), R7 (ASBR), outside OSPF

Another network: two ASBRs, R6 in area 46 and R7 in area 57, redistribute the same 172.16.0.0/24 at the same cost.

If the redistribution metrics tie, the router compares the forwarding cost. The forwarding cost is the metric to the ASBR that advertised the network, and the lower one is preferred. If the forwarding costs tie as well, both routes are installed into the routing table.

So: both paths carry metric 20. The forwarding metric through R2 to R6 is 31, and through R3 to R7 it's 30, so R1 installs the path through R3 to R7: 10 + 10 + 10 = 30 to R7, against 9 + 12 + 10 = 31 to R6. One unit of cost, and a number that never shows up in the brackets, decided it. R1's entry, from that network:

R1# show ip route 172.16.0.0
Routing entry for 172.16.0.0/24
 Known via "ospf 1", distance 110, metric 20, type extern 2
 Last update from 10.13.1.3 on GigabitEthernet0/1, 00:12:40
 Routing Descriptor Blocks:
 * 10.13.1.3, from 192.168.7.7, 00:12:40 ago, via GigabitEt…
     Route metric is 20, traffic share count is 1

Metric 20, type extern 2, next hop 10.13.1.3 toward R3, and "from 192.168.7.7", which fits R7 as the advertising ASBR. You work the 30 out yourself, by adding the interface costs along the path to each ASBR.

A classic trap. Two routers redistribute the same network as E2 with the same metric: are both paths installed downstream? Not necessarily. OSPF checks the forwarding cost; equal, both go in; different, the lowest forwarding metric wins.

And the NSSA case again. An ABR doesn't install an O E2 and an O N2 for the same network at the same time: for a typical NSSA the O N2 is always preferred, and its presence keeps the O E2 off the ABR. Choosing an O Nx (type 7) over an O Ex (type 5) is defined in RFC 3101 and is the current Cisco default; RFC 1583 prefers the O Ex, and compatible rfc1583 turns on RFC 1583 path selection.

Rulebook: E2 and N2 take the lowest seed, then the lowest forwarding cost, the distance to the ASBR; equal both ways, all go in. On an NSSA's ABR, for a typical NSSA, the same network as N and as E: the N, whatever the metrics.