Sub topic 7
Metrics: how a protocol ranks paths
This chapter assumes you know IPv4 prefixes and CIDR notation, can tell a router from a switch and a host, and can run basic commands on a router or Linux host. It also assumes you have read the earlier chapters on the routing table and dynamic routing.
What a metric is
A dynamic routing protocol often learns several paths to the same prefix. It needs a rule to pick one. That rule is the metric, a number the protocol attaches to each route. In most protocols a lower metric means a better path.
A metric only compares routes learned by the same protocol. If OSPF and RIP both offer a route to 10.2.0.0/16, their metrics are not compared, because the two numbers measure different things. The router uses administrative distance (covered in a later chapter) to choose between protocols.
Common metric types
Each protocol defines its own metric.
| Protocol | Metric | What it measures |
|---|---|---|
| RIP | Hop count | Number of routers crossed. The maximum is 15, and 16 means unreachable. |
| OSPF | Cost | Derived from interface bandwidth. Costs add up along the path. |
| EIGRP | Composite | Bandwidth and delay by default. Load and reliability are optional. |
| BGP | Path attributes | A list of rules such as local preference and AS-path length, not one number. |
Hop count versus bandwidth-based cost
Hop count is simple but ignores link speed. Suppose a router can reach a network by two paths:
- Path A crosses 2 routers over 10 Mbps links.
- Path B crosses 4 routers over 1 Gbps links.
RIP prefers path A because 2 is less than 4, even though path B is far faster. OSPF would prefer path B.
OSPF computes the cost of an interface as the reference bandwidth divided by the interface bandwidth. A common default reference bandwidth is 100 Mbps. The path cost is the sum of the costs of the outgoing interfaces along the path.
# OSPF cost = reference bandwidth / interface bandwidth, with a minimum of 1
REFERENCE_MBPS = 100
def ospf_cost(link_mbps):
return max(1, REFERENCE_MBPS // link_mbps)
path_a = [10, 10] # two 10 Mbps links
path_b = [1000, 1000, 1000] # three 1 Gbps links
cost_a = sum(ospf_cost(m) for m in path_a)
cost_b = sum(ospf_cost(m) for m in path_b)
print("Path A cost:", cost_a)
print("Path B cost:", cost_b)
print("OSPF chooses:", "A" if cost_a < cost_b else "B")This prints costs of 20 and 3, so OSPF chooses B. Notice that with a 100 Mbps reference, a 1 Gbps link and a 100 Mbps link both get cost 1. That makes fast links look equal.
Equal-cost paths and tuning
When two paths have the same metric, many protocols install both and share traffic between them. This is equal-cost multipath. Unequal metrics normally leave only the best route in the table.
Common mistakes:
- Leaving the default reference bandwidth on a network with 1 Gbps and 10 Gbps links, so they all show the same cost.
- Setting different reference bandwidths on different routers, which makes their cost calculations disagree.
- Comparing metrics across protocols instead of relying on administrative distance.
Exercise
A router learns 10.2.0.0/16 by RIP with metric 3 and by OSPF with cost 30. Does it choose RIP because 3 is lower than 30? Explain.
Show answer
No. Metrics from different protocols are not comparable because they measure different things. The router compares administrative distance first. OSPF (110) beats RIP (120), so the OSPF route is installed.