Routing Selection – Specificity vs Metric Explained

iproute2linuxmetricsrouterouting

I understand that Linux chooses the most specific route to the destination when it does routing selection. But what about a route's metric? Does it have a higher priority than route's specificity?

A reference to the details of the routing selection algorithm used by Linux would also be appreciated.

Best Answer

The routes metric is to set preference among routes with equal specificity. That is true of routing in general (i.e. Cisco, Windows, etc). So the model works like:

  1. Find the most specific route (aka the longest prefix match*)
  2. If there are multiple routes with the same specificity, pick the one with the lowest administrative distance (This distinguishes between things like directly attached routes, static routes, and various routing protocols).
  3. Within that routing protocol and specific route (if route specificity and administrative distance are the same), chose the route with the lowest metric

Note that there are other things that could be going on such a policy based routing that lets you do things like route based on the source IP address. But route specificity, administrative distance, and then metric are what I would consider to be the main three things.

*It is called the longest prefix match because a subnet in binary (/24 for example) looks like 11111111.11111111.11111111.00000000. So a router can just scan the prefix for binary 1s and stop once it hits a zero, and then it has matched the prefix.