By stretch | Monday, August 16, 2010 at 3:04 a.m. UTC
Suppose a router receives a packet destined for the IP address 192.0.2.73. The router has in its routing table the following three routes:
To which next hop address will the packet be routed?
If you picked 172.16.3.1, you're correct. Why? A router evaluates routes in the following order.
- Prefix Length - The longest-matching route is preferred first. Prefix length trumps all other route attributes.
- Administrative Distance - In the event there are multiple routes to a destination with the same prefix length, the route learned by the protocol with the lowest administrative distance is preferred.
- Metric - In the event there are multiple routes learned by the same protocol with same prefix length, the route with the lowest metric is preferred. (If two or more of these routes have equal metrics, load balancing across them may occur.)
Following these rules, we can see that our RIP-learned route is preferred because it is the most specific route: it has the longest matching prefix (26 bits in length compared to 25 and 24). These rules hold true even when evaluating directly connected routes, which have an AD of zero:
Router# show ip route Codes: C - connected, S - static, R - RIP, M - mobile, B - BGP D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2 E1 - OSPF external type 1, E2 - OSPF external type 2 i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2 ia - IS-IS inter area, * - candidate default, U - per-user static route o - ODR, P - periodic downloaded static route Gateway of last resort is not set 172.16.0.0/24 is subnetted, 3 subnets C 172.16.1.0 is directly connected, Loopback1 C 172.16.2.0 is directly connected, Loopback2 C 172.16.3.0 is directly connected, Loopback3 192.0.2.0/24 is variably subnetted, 3 subnets, 3 masks S 192.0.2.64/26 [120/0] via 172.16.3.2 C 192.0.2.0/24 is directly connected, Loopback99 Router# show ip route 192.0.2.73 Routing entry for 192.0.2.64/26 Known via "static", distance 120, metric 0 Routing Descriptor Blocks: * 172.16.3.2 Route metric is 0, traffic share count is 1
I'm afraid that this core concept is too often glossed over in CCNA and other introductory networking classes. Remember: it doesn't matter if we have a less-specific route with a direct 100 Gbps connection to the destination, and a more-specific route which takes 15 hops over 56 Kbps serial links through a bad neighborhood. The most-specific route will always be preferred.
About the Author
Jeremy Stretch is a network engineer living in the Raleigh-Durham, North Carolina area. He is known for his blog and cheat sheets here at Packet Life. You can reach him by email or follow him on Twitter.
Posted in Routing
August 16, 2010 at 9:07 a.m. UTC
Nice post Jeremy. This was one of the things that puzzled me when doing CCNA at a net academy back in 2003. There is probably still a lot of people that don't know of this even at a higher level of understanding.
August 16, 2010 at 9:10 a.m. UTC
Wow I'm a week off doing the ICND2 exam and I wasn't actually aware that a more direct route trumps a protocol, I love this site.
Now back to Packet tracer
August 16, 2010 at 3:14 p.m. UTC
Awesome!!!!! :) You made my day.... Throw more stuffs like this...
August 16, 2010 at 3:18 p.m. UTC
Jeremy, perhaps this is just semantics, but when you write "If two or more of these routes have equal metrics, load balancing across them may occur", why did you use the word "may"? Is there a possibility that load-balancing would not occur? If so, what would be the cause(s)?
August 16, 2010 at 3:57 p.m. UTC
@Guest: There are intricacies to CEF load sharing that can prevent the expected load balancing behavior in some cases. I might have written "will occur," but didn't want to commit, just in case. :)
August 17, 2010 at 4:02 a.m. UTC
Depends on the protocol that has the tie as well, not all support load balancing.
August 17, 2010 at 5:18 a.m. UTC
The router evaluates all this routes in opposite order (but result is the same):
0) consider routes ONLY if next hop is valid (that generally means "reachable"). That's very important.
1) prefer routes with the best metric (independently for each protocol)
2) prefer route sources with the lowest AD
-now we have ONLY the best routes for each prefix in routing table
If router receive a packet, it looks through the whole routing table (or in CEF case, part of FIB) for longest match.
So result is the same, but the order is completely different. Very important, if you want to understand various "tables", like RIB, FIB, LSDB, bgp table and more.
August 17, 2010 at 8:48 a.m. UTC
Funny things about unicast and multicast routing differences, is that in your multicast routing table, the preference matters MORE than the length of the route.
If you have a static mroute to 0.0.0.0/0 and a MBGP route to 18.104.22.168/32, the RPF for 22.214.171.124 will be the static mroute...
Confusing no... ?
August 17, 2010 at 10:15 a.m. UTC
Nice Article...This should be part of all CCNA Books now.
I did some similar testing too couple of months back.
August 17, 2010 at 7:25 p.m. UTC
In the CCNA Exploration - Routing Protocols and Concepts (both NetAcad online curriculum and printed Companion Guida) this topic is adressed.
August 18, 2010 at 6:24 p.m. UTC
The longest prefix match always wins among the routes actually installed in the routing table, while the routing protocol with the lowest administrative distance always wins when installing routes into the routing table.
an oldie but goldi
August 18, 2010 at 9:39 p.m. UTC
Just now i am prepare ccnp (ROUTE) and i had wrong answer on this Q. Oooo my Good. I don't can believe :)
August 19, 2010 at 11:09 p.m. UTC
Jeremy I read this article 3 times to believe that is this for real ... you know I recently cleared by BSCI exam and never had an Idea about that. you rocks man.
August 20, 2010 at 4:28 a.m. UTC
anon beat me to it.
I was confused at first following the method posted here when I was going through my CCNA. There are two things going on, route installation and route selection.
- Lowest AD
- Lowest Metric
- Longest mask
- Lowest metric
The link anon posted explains it nicely
August 29, 2010 at 6:22 p.m. UTC
pls guys .. i dont know how to start this network ing studies
November 22, 2010 at 6:33 p.m. UTC
@zerozerofourteen (or anyone else for that matter)
What is the difference between 'route installation' and 'route selection' ?
** Ignore that I know understand it after reading through the cisco article above... A route is added for each protocol based upon firstly the AD... the traffic is then forwarded via the route installed depending on firstly on the longest prefix match... I take it that's correct?
December 29, 2010 at 2:43 p.m. UTC
For those who want to go further, I recommend "CCNA Exploration: Routing Protocols and Concepts", Chapter 8 - "The Routing Table: A Closer Look".
January 4, 2011 at 4:39 a.m. UTC
I think this order is just for Cisco box. is it?
October 31, 2011 at 9:47 a.m. UTC
why Maximum prefix length match is first Priority routing table?
October 12, 2015 at 9:19 a.m. UTC
I finding lot of your posts useful. Thanks mate.
By the same token, I tried connected routes (AD = 0) for comparison on my lab and to my surprise longer prefixes still win.
That was a 'ahhha' moment !
If you dont mind adding that to your post, that could be useful to your audience.
November 17, 2015 at 7:49 p.m. UTC
Nice post, has cleared my doubts N got concept. Thanks