Sub topic 14
Metrics and administrative distance
This chapter assumes you can read a routing table, know how longest prefix match works, and know that routes come from connected interfaces, static configuration, and dynamic protocols such as OSPF, RIP, and BGP. You should also be comfortable with the ip route command on Linux.
Why a router needs more than longest prefix match
Longest prefix match picks the most specific prefix that contains the destination. It does not say what to do when several sources offer the same prefix. A router running OSPF and RIP may learn 10.2.0.0/16 from both, and a static route may exist for the same prefix. The routing table can install only one of them, or several if they tie exactly.
The router decides in this order:
- Longest prefix: the most specific matching prefix wins, whatever its source.
- Administrative distance: among routes with the same prefix, the most trusted source wins. Lower is better.
- Metric: among routes from the same protocol, the lowest metric wins.
- Tie: if everything is equal, the router may install several next hops and share traffic between them (equal-cost multipath, or ECMP).
Administrative distance
Administrative distance (AD) is a number from 0 to 255 that ranks how much the router trusts a route's source. It is local to the router and is never sent to neighbors. Lower values are preferred. A route with AD 255 is treated as unusable.
The table shows common Cisco IOS defaults. Other vendors use different names and numbers. Juniper calls the value "preference", and its defaults differ (for example, static is 5 and OSPF internal is 10). Check your platform's documentation.
| Route source | Default AD |
|---|---|
| Directly connected interface | 0 |
| Static route | 1 |
| eBGP | 20 |
| EIGRP | 90 |
| OSPF | 110 |
| IS-IS | 115 |
| RIP | 120 |
| iBGP | 200 |
The ranking is a judgment about reliability. Connected networks are certain, because the interface is up. Static routes are what an operator typed deliberately. Among the interior protocols, OSPF is preferred over RIP because its link-state view is more accurate than hop counts.
A Cisco-style routing table shows both numbers in brackets as [AD/metric]:
O 10.2.0.0/16 [110/20] via 192.0.2.2, 00:04:12, GigabitEthernet0/1
R 10.2.5.0/24 [120/3] via 192.0.2.3, 00:00:08, GigabitEthernet0/2
S 10.4.0.0/16 [1/0] via 192.0.2.9Metrics
A metric is a protocol's own measure of how good a path is. Each protocol defines it differently, so you can compare metrics only between routes from the same protocol.
| Protocol | Metric |
|---|---|
| RIP | Hop count. 16 means unreachable. |
| OSPF | Cost: the sum of interface costs along the path. |
| EIGRP | A composite of bandwidth and delay by default. |
| BGP | No single metric. A sequence of attributes is compared: local preference, AS path length, MED, and others. |
OSPF derives each interface cost from a reference bandwidth: cost = reference bandwidth / interface bandwidth, rounded down to a minimum of 1. The default reference on many platforms is 100 Mbps. At that setting, a 100 Mbps link and a 10 Gbps link both cost 1, so OSPF cannot tell them apart. Raising the reference bandwidth fixes this:
| Link speed | Cost with 100 Mbps reference | Cost with 100 Gbps reference |
|---|---|---|
| 100 Mbps | 1 | 1000 |
| 1 Gbps | 1 | 100 |
| 10 Gbps | 1 | 10 |
All routers in an OSPF domain must use the same reference bandwidth. Otherwise they compute inconsistent path costs.
A worked example
A router learns 10.2.0.0/16 from three sources:
- OSPF with metric 20 (AD 110)
- RIP with metric 2 (AD 120)
- A static route with AD 200 (AD 200), configured as a backup
RIP's metric of 2 looks better than OSPF's 20, but the router never compares them. AD decides first, and OSPF's 110 beats RIP's 120. The RIP and static routes stay in the protocol databases as candidates. If OSPF withdraws its route, RIP's route is installed. If RIP also fails, the static route is installed.
A static route given a deliberately high AD to act as a last-resort backup is called a floating static route.
The following Python code models the selection order. It reuses the idea from the longest-prefix-match example and adds the AD and metric tie-breakers.
import ipaddress
from dataclasses import dataclass
@dataclass(frozen=True)
class Route:
prefix: ipaddress.IPv4Network
source: str
admin_distance: int
metric: int
next_hop: str
def net(text):
return ipaddress.ip_network(text)
ROUTES = [
Route(net("10.2.0.0/16"), "ospf", 110, 20, "192.0.2.2"),
Route(net("10.2.0.0/16"), "rip", 120, 2, "192.0.2.3"),
Route(net("10.2.0.0/16"), "static", 200, 0, "192.0.2.4"),
Route(net("10.2.5.0/24"), "rip", 120, 3, "192.0.2.3"),
Route(net("10.3.0.0/16"), "ospf", 110, 20, "192.0.2.2"),
Route(net("10.3.0.0/16"), "ospf", 110, 20, "192.0.2.5"),
]
def select(routes, destination):
"""Return the routes to install for a destination (several means ECMP)."""
addr = ipaddress.ip_address(destination)
candidates = [r for r in routes if addr in r.prefix]
if not candidates:
return []
for key in (
lambda r: -r.prefix.prefixlen, # 1. longest prefix first
lambda r: r.admin_distance, # 2. most trusted source
lambda r: r.metric, # 3. best metric within that source
):
best = min(key(r) for r in candidates)
candidates = [r for r in candidates if key(r) == best]
return candidates
def show(routes, destination):
chosen = select(routes, destination)
print(destination, [(r.source, r.next_hop) for r in chosen])
show(ROUTES, "10.2.1.1") # [('ospf', '192.0.2.2')]
show(ROUTES, "10.2.5.9") # [('rip', '192.0.2.3')]: the /24 beats the /16
show(ROUTES, "10.3.4.4") # two OSPF next hops: ECMP
# Simulate failures: OSPF withdraws its routes, then RIP does too.
no_ospf = [r for r in ROUTES if r.source != "ospf"]
show(no_ospf, "10.2.1.1") # [('rip', '192.0.2.3')]
no_rip = [r for r in no_ospf if r.source != "rip"]
show(no_rip, "10.2.1.1") # [('static', '192.0.2.4')]The query for 10.2.5.9 shows that prefix length outranks everything. The RIP /24 wins even though its AD of 120 is worse than the OSPF /16's 110.
Metrics on Linux
A Linux host has no administrative distance. Routing daemons such as FRRouting install their routes into the kernel with a protocol tag, and the kernel compares only the per-route metric (also called priority). For two routes with the same prefix, the lower metric is preferred:
# Primary and backup routes to the same prefix
sudo ip route add 10.2.0.0/16 via 192.0.2.2 dev eth0 metric 100
sudo ip route add 10.2.0.0/16 via 192.0.2.3 dev eth0 metric 200
# Both routes are listed
ip route show 10.2.0.0/16
# The kernel picks the metric 100 route
ip route get 10.2.1.1The backup route is used only after the primary one is removed, for example when its interface goes down. A next hop that is merely unresponsive does not remove the route.
Edge cases and common errors
- Comparing metrics across protocols. An OSPF cost of 20 and a RIP hop count of 2 are not comparable. AD resolves the conflict before metrics are examined.
- Expecting AD to override prefix length. A more specific route from a less trusted source still wins. A stray
/32from a poorly trusted protocol can pull traffic away from the intended path. - Floating static routes that do not float. If the AD of the static backup is lower than the dynamic protocol's AD, the static route always wins. A backup for OSPF needs an AD above 110.
- Mismatched OSPF reference bandwidth. Routers with different settings compute different costs for the same link, which can cause suboptimal or asymmetric paths.
- Redistribution loops. When a router injects routes from one protocol into another, the injected routes get the new protocol's metric and AD. If two routers redistribute in both directions, routes can loop back and win by AD. Filtering and tagging the redistributed routes prevents this.
- ECMP surprises. Routers usually choose a next hop per flow by hashing header fields, so one flow stays on one path. Two paths do not double the throughput of a single connection.
Exercise
A router holds these routes: 10.8.0.0/16 from OSPF with metric 30, 10.8.0.0/16 from RIP with metric 1, and 10.8.4.0/24 from a static route with AD 250. Which route forwards a packet to 10.8.4.7, and which forwards a packet to 10.8.9.1? Give the reason for each.
Show answer
The packet to 10.8.4.7 uses the static /24 route, even with AD 250, because it is the longest matching prefix. The packet to 10.8.9.1 matches only the two /16 routes. OSPF (AD 110) beats RIP (AD 120), so OSPF is used. RIP's lower metric is never compared, because the metrics belong to different protocols.
Exercise
You want a static route to 10.5.0.0/16 to serve as a backup when OSPF has that prefix, and to be unused otherwise. What AD should you give it, and what happens when OSPF withdraws the prefix?
Show answer
Give it an AD higher than OSPF's 110, such as 200. While OSPF advertises the prefix, the OSPF route is installed and the static route stays out of the table. When OSPF withdraws the prefix, the static route becomes the best remaining candidate and is installed.