Sub topic 14
Metrics and Administrative Distance
This chapter assumes you know longest prefix match, what a routing table holds, and the difference between static and dynamically learned routes. Here you will see how a router chooses when several sources offer a route to the same destination.
Two different questions
Longest prefix match picks the most specific prefix. Sometimes two routes have the same prefix, such as 10.2.0.0/16, but come from different places. The router must then pick one to install. It uses two tools in order:
- Administrative distance (AD) compares routes learned from different sources, such as a static route against an OSPF route.
- Metric compares routes learned from the same routing protocol.
Prefix length always comes first. A /24 from any source beats a /16 from any source. AD and metric only break ties between routes with an identical prefix.
Administrative distance
AD is a number that says how much a router trusts a source. Lower is better. It is local to the router and is never sent to neighbors. The values below are common Cisco defaults. Other vendors use different numbers.
| Source | Typical AD |
|---|---|
| Connected interface | 0 |
| Static route | 1 |
| eBGP | 20 |
| OSPF | 110 |
| RIP | 120 |
| iBGP | 200 |
If a static route and an OSPF route both cover 10.2.0.0/16, the static route wins because 1 is lower than 110. Linux and FRRouting use similar ideas, with FRR defaulting to 1 for static and 110 for OSPF.
Metrics
A metric is a protocol's own cost for a path. Different protocols measure it differently, so metrics from different protocols cannot be compared.
- RIP counts hops. A route through 2 routers is better than one through 5.
- OSPF sums link costs, usually derived from bandwidth.
- BGP has no single metric. It runs an ordered list of attributes, such as local preference and AS path length.
The route with the lowest metric is installed. If two routes tie, many protocols install both and share traffic between them. This is called equal-cost multipath (ECMP).
A worked selection
The router hears about 10.2.0.0/16 from three sources.
candidates = [
{"source": "ospf", "ad": 110, "metric": 20, "next_hop": "192.0.2.2"},
{"source": "rip", "ad": 120, "metric": 2, "next_hop": "192.0.2.3"},
{"source": "ospf", "ad": 110, "metric": 35, "next_hop": "192.0.2.4"},
]
def best_route(routes):
# Lowest AD wins first, then the lowest metric breaks the tie.
return min(routes, key=lambda r: (r["ad"], r["metric"]))
print(best_route(candidates))The output is the first OSPF route, with next hop 192.0.2.2. RIP has the lowest metric, but its metric is never compared with OSPF's, because AD already ruled RIP out.
Floating static routes
You can use AD deliberately. A static route given a higher AD than the dynamic protocol stays out of the table while the dynamic route exists. It is called a floating static route. If the dynamic route disappears, the static route is installed as a backup.
# Backup route with AD 150, worse than OSPF's 110
sudo vtysh -c "configure terminal" -c "ip route 10.2.0.0/16 192.0.2.9 150"This example uses FRRouting's vtysh. Plain ip route add on Linux uses a metric value for the same purpose within the kernel table.
Common errors
- Comparing metrics across protocols. A RIP metric of 2 and an OSPF cost of 20 mean nothing relative to each other.
- Expecting a lower-AD route to win over a longer prefix. It will not. Prefix length is checked first.
- Forgetting that a backup route is hidden. A route can be configured but absent from
show ip routebecause a better source holds the prefix.
Exercise
A router has a static route to 10.5.0.0/16 (AD 1) and an OSPF route to 10.5.1.0/24 (AD 110). Which route forwards a packet for 10.5.1.7, and why?
Show answer
The OSPF route to 10.5.1.0/24. Longest prefix match is applied first, and the /24 is more specific than the /16. Administrative distance only compares routes with the same prefix, so the static route's lower AD does not matter.