Sub topic 13
Route Types and Path Preference
This chapter assumes you know longest-prefix match, how the LSA types 1 to 5 are generated, and how OSPF cost is calculated. It covers how OSPF labels each route it installs and how it chooses among several candidate paths to the same prefix.
Where OSPF Path Preference Fits
A router makes three decisions in a fixed order, and each one only breaks ties the earlier one left open.
- Longest-prefix match picks the most specific route. A /24 always beats a /16, whatever the protocol or metric.
- Administrative distance picks between routing sources for the same prefix. OSPF's default is 110.
- Inside OSPF, route type decides first and metric second.
The third step is the one people get wrong. OSPF does not compare metrics across route types. A route with a higher type rank wins even when its metric is much larger.
The Route Types
Every OSPF route in the table carries a code that records how it was learned.
| Code | Name | Learned from | Metric |
|---|---|---|---|
| O | Intra-area | Type 1 and Type 2 LSAs in the same area | Sum of interface costs |
| O IA | Inter-area | Type 3 LSAs from an ABR | Cost to the ABR plus the ABR's advertised cost |
| O E1 | External type 1 | Type 5 LSAs, resolved through Type 4 | Seed metric plus internal cost to the ASBR |
| O E2 | External type 2 | Type 5 LSAs | Seed metric only (the default) |
| O N1, O N2 | NSSA external | Type 7 LSAs | Same rules as E1 and E2 |
The preference order is O, then O IA, then E1, then E2. N1 ranks with E1 and N2 with E2.
Why This Order
The order follows how trustworthy the information is. A router builds an intra-area route from its own area's full topology. An inter-area route depends on an ABR's summary, so the router sees only a distance, not the path. An external route comes from a protocol OSPF does not control, and its seed metric may not be in OSPF cost units at all.
The order also prevents loops. If a router could prefer an inter-area route over an intra-area route because of cost, traffic could leave an area and come back through a different ABR.
E1 Versus E2
The two external types differ in how the metric is built.
- E2 (default) keeps the seed metric unchanged as it floods. Every router in the domain sees the same value, and the internal path cost is ignored. A tie on the seed metric is broken by the lower forward cost, which is the cost to reach the ASBR.
- E1 adds the internal cost to the ASBR at each hop, so the metric grows as you move away from the ASBR.
Use E2 when a single exit point is the obvious choice or you want a stable metric. Use E1 when several ASBRs advertise the same prefix and you want each router to choose the closest exit.
If the same prefix arrives as both E1 and E2, the E1 route wins, even if its total metric is larger.
Worked Example
A router learns 172.16.0.0/16 from three sources.
- ASBR A as E2, seed 20, forward cost 10.
- ASBR B as E2, seed 20, forward cost 5.
- ASBR C as E1, seed 20, internal cost 50, so the total metric is 70.
The script below applies the OSPF comparison rules.
RANK = {"O": 0, "O IA": 1, "E1": 2, "E2": 3}
def sort_key(route):
rank = RANK[route["type"]]
if route["type"] == "E2":
# E2: seed metric first, forward cost only breaks ties
return (rank, route["seed"], route["forward"])
# Other types compare one total metric
return (rank, route["seed"] + route["forward"])
candidates = [
{"via": "ASBR A", "type": "E2", "seed": 20, "forward": 10},
{"via": "ASBR B", "type": "E2", "seed": 20, "forward": 5},
{"via": "ASBR C", "type": "E1", "seed": 20, "forward": 50},
]
best = min(candidates, key=sort_key)
print(best["via"], best["type"])
without_e1 = [r for r in candidates if r["type"] != "E1"]
best = min(without_e1, key=sort_key)
print(best["via"], best["type"])The first line prints ASBR C E1. The E1 route wins on type even though its metric of 70 is the largest. After you remove it, the second line prints ASBR B E2, because the seed metrics tie at 20 and ASBR B has the lower forward cost.
Reading the Routing Table
The routing table shows the type code and the metric in [distance/metric] form.
R1# show ip route ospf
O 10.0.23.0/30 [110/2] via 10.0.12.2, 00:10:41, GigabitEthernet0/0
O IA 10.2.0.0/16 [110/21] via 10.0.12.2, 00:09:15, GigabitEthernet0/0
O E2 172.16.0.0/16 [110/20] via 10.0.13.3, 00:08:02, GigabitEthernet0/1
O E1 172.17.0.0/16 [110/70] via 10.0.12.2, 00:08:02, GigabitEthernet0/0The E2 line shows 20 because only the seed metric appears. Use show ip route 172.16.0.0 to see the forward cost, which the summary line omits.
Gotchas
- A low metric does not override a better type. If an area has a 1 Gbps intra-area path and a 10 Gbps inter-area path to the same prefix, the intra-area path wins. This case is rare, but it appears when a prefix is advertised both inside an area and by an ABR.
- E2 metrics ignore your cost tuning. Changing interface costs on a router does not change which E2 route wins unless the seed metrics tie.
- Mixing E1 and E2 for one prefix gives results that are hard to predict by eye. Pick one type per redistribution policy.
- Equal-cost paths are installed only when type and metric match. An O IA and an O E1 path never load-share, however close their metrics are.
- Redistributed routes on different ASBRs can create suboptimal paths when you use E2. A router far from the nearest ASBR still follows the lowest forward cost only on seed ties.
Exercise
R1 has three OSPF routes to 10.50.0.0/16. Route X is O IA with metric 15. Route Y is O E1 with metric 12. Route Z is O E2 with seed 5 and forward cost 2. Which route does R1 install, and why?
Show answer
Route X. Route type is compared before metric, and the order is O, O IA, E1, E2. O IA ranks above E1 and E2, so X wins even though Y and Z have lower metrics.
Exercise
Two ASBRs advertise 192.0.2.0/24 as E2 with seed 20. R1's forward cost is 30 to ASBR A and 10 to ASBR B. Which does R1 use? What changes if both are advertised as E1 with seed 20?
Show answer
With E2, the seed metrics tie, so the lower forward cost decides and R1 uses ASBR B. With E1, the total metrics are 50 (A) and 30 (B), so R1 still uses ASBR B, but the installed metric is 30 instead of 20.