Route Preference

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:

Protocol AD Metric Prefix Next Hop
OSPF 110 240 192.0.2.0/25 172.16.1.1
EIGRP 90 33789 192.0.2.0/24 172.16.2.1
RIP 120 6 192.0.2.64/26 172.16.3.1

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.

  1. Prefix Length - The longest-matching route is preferred first. Prefix length trumps all other route attributes.
  2. 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.
  3. 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.

 

原帖地址:http://packetlife.net/blog/2010/aug/16/route-preference/