Sub topic 7
Metrics: how a protocol ranks paths
This chapter assumes you know how longest-prefix match picks a route, what administrative distance does, and the basics of static and dynamic routing from earlier chapters. You should also be comfortable reading CIDR prefixes and running commands on a Linux host or a Cisco-style router.
What a metric is
A metric is a number a routing protocol attaches to a route. It says how costly that route is compared with other routes to the same prefix. The router prefers the lowest number. A metric answers one narrow question: among several routes to this exact prefix that the same source offers, which is best?
Three separate decisions happen in order when a router forwards a packet:
- Longest-prefix match picks the most specific prefix that contains the destination.
- Administrative distance picks the most trusted source when several sources offer that same prefix.
- The metric picks the best route within one source, or within one protocol.
The diagram shows where the metric sits in that sequence.
Metrics are not comparable across protocols
Each protocol defines its own metric. An OSPF cost of 20 and a RIP hop count of 2 use different units, so a router never compares them. That is why administrative distance exists. The router uses it to choose between protocols, and only then uses the metric inside the winning protocol.
The routing table shows both numbers in square brackets as [administrative distance/metric]. For example, [110/20] means an OSPF route (distance 110) with cost 20.
Common metrics
| Protocol | Metric | Notes |
|---|---|---|
| RIP | Hop count | Each router crossed adds 1. A value of 16 means unreachable, so the longest usable path is 15 hops. |
| OSPF | Cost | Derived from interface bandwidth. The cost of a path is the sum of the outgoing interface costs. |
| EIGRP | Composite of bandwidth and delay | By default it uses the lowest bandwidth along the path and the total delay. Load and reliability are off by default. |
| BGP | No single metric | It compares path attributes in a fixed order, such as local preference and AS-path length. |
| Static route (Linux) | Configurable `metric` value | You choose it. Lower is preferred among routes to the same prefix. |
Hop count
Hop count is the simplest metric. It ignores link speed, so a path of two slow links beats a path of three fast links. This makes RIP easy to understand but a poor fit for networks with mixed link speeds.
Bandwidth-derived cost
OSPF computes the cost of an interface as a reference bandwidth divided by the interface bandwidth. The result is an integer with a minimum of 1. On Cisco IOS the default reference bandwidth is 100 Mbps. Other implementations may use different defaults, so check your platform.
The path cost is the sum of the costs of the outgoing interfaces along the path. Faster links get lower costs, so OSPF prefers fast paths even when they have more hops.
Delay and composite metrics
EIGRP also considers delay, so two links with the same bandwidth but different latency can rank differently. The more inputs a metric uses, the better it matches real conditions. A richer metric is also harder to predict and to troubleshoot.
Worked example: two protocols, two answers
Take two paths to the same network. Path A has 2 hops over 10 Mbps links. Path B has 3 hops over 1 Gbps links. The script below computes what each metric would choose.
def ospf_cost(bandwidth_mbps, reference_mbps=100):
"""Reference bandwidth divided by link bandwidth, integer result, minimum 1."""
return max(1, reference_mbps // bandwidth_mbps)
paths = {
"A": [10, 10], # two 10 Mbps links
"B": [1000, 1000, 1000], # three 1 Gbps links
}
for reference in (100, 10000):
print(f"Reference bandwidth: {reference} Mbps")
costs = {}
for name, links in paths.items():
hops = len(links)
cost = sum(ospf_cost(bw, reference) for bw in links)
costs[name] = (hops, cost)
print(f" Path {name}: hops={hops}, ospf_cost={cost}")
rip_choice = min(costs, key=lambda n: costs[n][0])
ospf_choice = min(costs, key=lambda n: costs[n][1])
print(f" RIP picks {rip_choice}, OSPF picks {ospf_choice}")The output is:
Reference bandwidth: 100 Mbps
Path A: hops=2, ospf_cost=20
Path B: hops=3, ospf_cost=3
RIP picks A, OSPF picks B
Reference bandwidth: 10000 Mbps
Path A: hops=2, ospf_cost=2000
Path B: hops=3, ospf_cost=30
RIP picks A, OSPF picks BRIP chooses the slow path because it has fewer hops. OSPF chooses the fast path in both runs. The second run shows why the reference bandwidth matters. With 100 Mbps as the reference, a 1 Gbps link and a 100 Mbps link both cost 1, so OSPF cannot tell them apart.
Influencing metrics on real systems
You can tune metrics instead of accepting the defaults. On a Cisco router, set the OSPF cost for one interface directly, or change the reference bandwidth for the whole process.
! Cisco IOS: set the OSPF cost explicitly on one interface
interface GigabitEthernet0/0
ip ospf cost 10
! Cisco IOS: raise the reference bandwidth to 10 Gbps (value is in Mbps)
router ospf 1
auto-cost reference-bandwidth 10000On Linux, a static route can carry a metric. When two routes share the same prefix, the kernel prefers the lower metric. The higher-metric route stays in the table as a standby.
# Preferred path (lower metric)
sudo ip route add 10.2.0.0/16 via 192.168.1.2 dev eth0 metric 100
# Standby path to the same prefix (higher metric)
sudo ip route add 10.2.0.0/16 via 192.168.2.2 dev eth1 metric 200
# Both routes are listed; the kernel uses the metric 100 route
ip route show 10.2.0.0/16
# Confirm which route a destination uses
ip route get 10.2.5.7Linux has no administrative distance. The metric alone orders routes to the same prefix, which is different from the floating static route technique on Cisco IOS covered in a later chapter.
Equal-cost paths
When two routes from the same protocol have the same metric, many routers install both and share traffic across them. This is equal-cost multipath (ECMP). Routers usually pick a path per flow, using a hash of fields such as source and destination address and ports, so packets of one connection stay in order. The result is balanced load only across many flows. A single large transfer still uses one path.
Common errors
- Comparing metrics across protocols. A RIP metric of 3 is not better than an OSPF cost of 20. The router decides between them with administrative distance.
- Leaving the default reference bandwidth. Fast links get identical costs, and OSPF picks paths that look equal but are not.
- Mismatched settings between routers. Different reference bandwidths or manual costs on the two ends of a link make the two routers disagree about that link's cost.
- Trusting the metric as a measurement. Bandwidth-derived metrics come from configured interface values, not from measured traffic. A congested 1 Gbps link keeps its low cost.
- Hitting the RIP limit. A metric of 16 means unreachable. Large networks cannot use RIP for this reason.
- Expecting ECMP to split one flow. Per-flow hashing sends each connection down one path.
Reading metrics when you troubleshoot
Look at the bracketed values in the routing table to see which source and which cost the router chose.
! Cisco IOS: show the chosen route and its [distance/metric]
show ip route 10.2.5.7
! Cisco IOS: show OSPF interface costs
show ip ospf interface briefIf a path you expect to be used is not, compare the metric of the chosen route with the metric you expect for the alternative. If the sources differ, compare administrative distance first.
Exercise
A router learns 10.2.0.0/16 from RIP with metric 2 and from OSPF with cost 20. Which route does it install, and why? Then, within OSPF alone, two paths to that prefix cost 20 and 30. Which one is used?
Show answer
The router installs the OSPF route. RIP and OSPF metrics use different units, so the router compares administrative distance first, and OSPF (110) beats RIP (120). Within OSPF, the router uses the path with cost 20, because the lowest metric wins among routes from the same protocol. The cost 30 path is kept as a backup candidate and is used only if the cost 20 path disappears.
Exercise
Your OSPF network uses the default 100 Mbps reference bandwidth, and a router has one 100 Mbps link and one 10 Gbps link to the same neighbor network. What cost does OSPF assign to each link, and what should you change?
Show answer
Both links get cost 1 (100 // 100 = 1, and 100 // 10000 rounds down to 0, which is raised to the minimum of 1). OSPF cannot tell the fast link from the slow one. Raise the reference bandwidth, for example with auto-cost reference-bandwidth 10000, on every router in the OSPF domain so the links get different costs.