How a Router Selects the Best Route
A router’s main responsibility is to receive packets and forward them to the right destination. But how does a router think and what is the process of deciding the best route to forward a packet to? This post describes this decision process and preference order based on examples.
1. Longest prefix length – When multiple routes with different prefix length
A destination IP address can belong to various subnets that exist in the routing table. When there are multiple routes with different prefix lengths to a destination, then the longest match is always preferred regardless of the routing protocol used. For example, if the routing table contains the prefixes:
then a packet with destination IP address 10.1.1.1 belongs to all of those subnets. In this case, the longest prefix match is preferred. That means that the router will choose the subnet that is matching most of its bits to the destination IP address, let’s see how:
The network address has the following binary representation:
10.1.1.0 → 00001010.00000001.00000001.00000000
The different subnet mask lengths are:
/20 → 00001010.00000001.0000|0001.00000000
/24 → 00001010.00000001.00000001|.00000000
/27 → 00001010.00000001.00000001.000|00000
The destination IP address in binary form is:
10.1.1.1 → 00001010.00000001.00000001.00000001
As you can see, subnet 10.1.1.0/27 is the longest prefix match of the address 10.1.1.1, so this route is going to be selected by the router in order to forward the packet.
2. Administrative distance – When multiple routes with same prefix length
A particular destination can be advertised to the router by various routing protocols, for example static route, BGP, OSPF, etc. In this case, the selection of the best route is based on the “administrative distance” where the lowest value is preferred. Administrative distance is a value which measures how “trustworthy” a routing protocol is and there are specific values for it (it can be adjusted though manually). The table below describes the administrative distance of some protocols in a Cisco router:
For instance, if the following routes are advertised in the routing table:
the router will select the static route in order to forward the packet, since static has the lowest administrative distance.
3. Metrics – When multiple routes with same prefix length and protocol
A routing protocol can learn multiple paths for the same destination. In this case, each routing protocol uses their own internal metrics (for example based on distance, speed, hops, etc.) to calculate which of those paths is the best one.
- Switch Learning and Forwarding (telconotes.wordpress.com)
- Using the ping command (telconotes.wordpress.com)
- How Traceroute Works (telconotes.wordpress.com)
- Internet Overview (telconotes.wordpress.com)