目录


前言
Route Table
Route Table 的来源
  1. Interface Network
  2. Static Route
  3. Dynamic Routing Protocol
Route Table 解说
Route 的筛选
Subnetted Route 与 Longest Match
Static Route 的 Next Hop 设定
  用 Next Hop IP
  用 Next Hop Interface
  用 Next Hop Interface 及 IP
Cisco Express Forwarding (CEF) 思科快递交换
Policy Based Routing (PBR) 基于策略的路由
  Step 1:设定 Access List
  Step 2:设定 Route Map
  Step 3:把 Route Map 放进 Interface

前言

当一个 Packet 从一个 Incoming Interface 到达 Router,Router 如何处理呢?当然它希望尽快把 Packet 放到适当的 Outgoing Interface 送出去。不过 Router 可能同时接驳着多个 Interface,所以 Router 需要查看自己的 Route Table 去决定把 Packet 放到那一个 Interface 送走。本篇文章会剖析 Router 所谓的 Route Decision 流程,并讲解一些基本的 Static Route 设定。

Route Table

当一个 Packet 到达,Router 会立刻检查 Packet 的 Layer 3 Destination Address (即 Destination IP),然后在 Route Table 中找出一条最适合的纪录,依据纪录,把 Packet 放到对应的 Interface 送走,这就是 Router 每分每秒的工作了。要查看 Route Table,可以用一个极常用的指令:show ip route

以下是一个 Route Table 的例子:

R1#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

C    192.168.12.0/24 is directly connected, Ethernet0/0
     2.0.0.0/32 is subnetted, 1 subnets
S       2.2.2.2 [1/0] via 192.168.30.1, Ethernet0/0
                is directly connected, Ethernet0/0
     192.168.1.0/24 is variably subnetted, 4 subnets, 4 masks
S       192.168.1.32/29 [1/0] via 192.168.12.12
S       192.168.1.32/28 [1/0] via 192.168.12.12
S       192.168.1.0/26 [1/0] via 192.168.12.11
S       192.168.1.0/24 [1/0] via 192.168.12.2
D    192.168.2.0/23 [90/409600] via 192.168.12.2, 01:21:55, Ethernet0/0

Route Table 的来源

那么 Route Table 的纪录是从那裡来呢?Router 其本上会用从以下三个来源去建立自己的 Route Table。

1. Interface Network

Router 会收集每一个使用中的 Interface,如果有设 IP 的话,这个 Network 就会被放进 Route Table。以下是一个例子,在 R1 上的 Route Table 原本是空空的。

R1#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

然后我们在 Interface 设定 IP Address 并把 Interface 启动 (假设这个 Interface 所连接的另一端已经启动了),这时再看看 Route Table 就发现多了一条 Route。

R1#configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
R1(config)#int ethernet 0/0
R1(config-if)#ip address 192.168.12.1 255.255.255.0
R1(config-if)#no shutdown
R1#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

C    192.168.12.0/24 is directly connected, Ethernet0/0

2. Static Route

如果 Router Configuration 中有设 Static Route 的话,也会被放进 Route Table。以下我们试加一条 Static Route。

R1#configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
R1(config)#ip route 192.168.1.0 255.255.255.0 192.168.12.2
R1(config)#end
R1#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

C    192.168.12.0/24 is directly connected, Ethernet0/0
S    192.168.1.0/24 [1/0] via 192.168.12.2

3. Dynamic Routing Protocol

如果 Router 有设 Dynamic Routing Protocol,Router 与 Router 之间就会交换 Route 资讯,Dynamic Routing Protocol 会对这些资讯进行运算,最后把结果放进 Route Table。现在我们试试在 Router 上设定 EIGRP,假设 Interface 另一端的 Router 已经设好了 EIGRP。完成设定后便可看到由 Dynamic Routing Protocol 产生的 Route。

R1(config)#router eigrp 1
R1(config-router)#network 192.168.12.0
R1#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

C    192.168.12.0/24 is directly connected, Ethernet0/0
S    192.168.1.0/24 [1/0] via 192.168.12.2
D    192.168.2.0/24 [90/409600] via 192.168.12.2, 00:01:31, Ethernet0/0

Route Table 解说

每条 Route 都包含几项资讯,我们用最后的一条由 EIGRP 所产生的 Route 作例子,解释如下:

R1(config)#router eigrp 1
R1#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

C    192.168.12.0/24 is directly connected, Ethernet0/0
S    192.168.1.0/24 [1/0] via 192.168.12.2
D    192.168.2.0/24 [90/409600] via 192.168.12.2, 00:01:31, Ethernet0/0

D - Codes

就是 Route 的来源,只要在 Route Table 的上方查看注解,就可得知每个 Code 所代表的意思。例如 D 就昰代表这条 Route 是从 EIGRP 得来的。

192.168.1.0/24 - Network

就是不同 Network 的 Network ID 和 Prefix,Router 就是在这裡查看目的地 IP 符合那一条 Route。如果 IP 是落在某一个 Network 的话,就会使用该条 Route 的资料去处理 Packet。

[90/409600] - AD/Metric

括号中的两个数字,前面名为 Administrative Distance (AD),后面名为 Metric,有什麽用呢?下一个部份再说。

192.168.12.2 - Next Hop

Next Hop 就是 Packet 要到的下一站了,可以昰 IP Address 或者是 Interface。只要跌入这条 Route 的 Packet 都会被送到 Next Hop。所以,当一隻 Packet 到达 R1,R1 就会先查看这个 Packet 的目的地 IP。假设目的地是 192.168.1.150,Router 就会到 Route Table 裡面做 Route Lookup,找到 S 192.168.1.0/24 [1/0] via 192.168.12.2 这条 Route 符合了条件,香港俗语说 192.168.1.150 「跌」入这条 Route 了。于是,Router 便会把 Packet 送到 Next Hop 192.168.12.2。慢着!Router 不知道 192.168.12.2 在哪裡啊?不要紧,可以再做一次 Route Lookup 查看 Route Table,192.168.12.2 跌入 C 192.168.12.0/24 is directly connected, Ethernet0/0 这条 Route,于是就把 Packet 从 Ethernet0/0 那裡送出去了。第二次的 Route Lookup 又称作 Recursive Lookup,不重要的,有需时拿出来抛抛书包就好。

Route 的筛选

但从不同来源收集回来的 Route 有可能会出现相同的 Network,这时 Router 就只会把 Administrative Distance (AD) 较小的 Route 放进 Route Table。如果 AD 相同的话,Route Table 通常只会把 Metric 最小的 Route 放进 Route Table,除非 Routing Protocol 有设定 Load Balance。不过 Metric 和 Load Balance 设定已超出本篇讨论范围,建议读者可以参考本网的 Routing Protocol 教学,例如:OSPFEIGRP

做个小实验,试试加入一条 AD 小于 90 的 192.168.2.0/24 Static Route 应可把 EIGRP Route 杀掉。例如加入一条 AD 50 的 Static Route。

R1#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

C    192.168.12.0/24 is directly connected, Ethernet0/0
S    192.168.1.0/24 [1/0] via 192.168.12.2
D    192.168.2.0/24 [90/409600] via 192.168.12.2, 00:01:31, Ethernet0/0
R1#configure terminal
R1(config)#ip route 192.168.2.0 255.255.255.0 192.168.12.10 50
R1(config)#end
R1#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

C    192.168.12.0/24 is directly connected, Ethernet0/0
S    192.168.1.0/24 [1/0] via 192.168.12.2
S    192.168.2.0/24 [50/0] via 192.168.12.10

至于 Metric 的比较则複杂一点,建议参考各 Routing Protocol 的教学。

Subnetted Route 与 Longest Match

有时侯 Network 会被切割成 Subnet,如 Route Table 收到这些 Subnet Route 的话,会用以下方式表示:

R1#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

C    192.168.12.0/24 is directly connected, Ethernet0/0
     192.168.1.0/24 is variably subnetted, 4 subnets, 4 masks
S       192.168.1.32/29 [1/0] via 192.168.12.12
S       192.168.1.32/28 [1/0] via 192.168.12.12
S       192.168.1.0/26 [1/0] via 192.168.12.11
S       192.168.1.0/24 [1/0] via 192.168.12.2
D    192.168.2.0/23 [90/409600] via 192.168.12.2, 00:00:03, Ethernet0/0

意思是收到了 4 个 Subnet 都是存在于 192.168.1.0/24 这个 Class C Network 中,分别有:192.168.1.32/29、192.168.1.32/28、192.168.1.0/26 和 192.168.1.0/24。每条 Subnet Route 的 Next Hop 可以相同,也可以不一样。

那么,如果 Destination IP 是 192.168.1.33 的 Packet 来到了,而由于所有 4 条 Route 都符合 192.168.1.33,应该用那一条 Route 的 Next-Hop 呢?Router 会用 Longest Match 去选择。即是找符合 (Match) 的 Route 中 Prefix 最长 (Longest) 的 Route 来使用。所以 192.168.1.33 会跌入 192.168.1.32/29,Router 会把 Packet 送到 Next Hop 192.168.12.12。

Static Route 的 Next Hop 设定

设定 Static Route 可使用以下 3 种 Next Hop 设定,必需了解其分别。我们会用以下网络去验证 3 种设定的不同之处。

routing decision

用 Next Hop IP

先用把 Static Route 设定好 Next Hop IP。

R1#show run | i route
ip route 192.168.101.0 255.255.255.0 192.168.12.2
ip route 192.168.102.0 255.255.255.0 192.168.12.2
ip route 192.168.103.0 255.255.255.0 192.168.12.2
ip route 192.168.104.0 255.255.255.0 192.168.12.2
R1#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

C    192.168.12.0/24 is directly connected, Ethernet0/0
S    192.168.101.0/24 [1/0] via 192.168.12.2
S    192.168.102.0/24 [1/0] via 192.168.12.2
S    192.168.103.0/24 [1/0] via 192.168.12.2
S    192.168.104.0/24 [1/0] via 192.168.12.2

用 Next Hop IP 的话会发生什么事?假设 R1 要送 Packet 到 192.168.101.1,跟据 Route Table,它会送到 Next Hop 192.168.12.2。但它不知道 192.168.12.2 在那裡啊?所以它要做一次 Recursive Lookup,然后找到 192.168.12.0/24 的 Next Hop Interface 是 E0/0。然后在 E0/0 发一个 ARP Request 问 192.168.12.2 的 MAC Address,于是 R2 回应并发 ARP Reply 使 R1 得知 MAC Address 并记录在 ARP Table 之中。以后不管 Destination 是 192.168.101.1、192.168.102.1、192.168.103.1、 或 192.168.104.1,都不需要再发 ARP 了,因为 Next-hop 192.168.12.2 已经在 ARP Table 中。

R1#ping 192.168.101.1

Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 192.168.101.1, timeout is 2 seconds:
.!!!!
Success rate is 80 percent (4/5), round-trip min/avg/max = 8/35/117 ms
R1#show arp
Protocol  Address          Age (min)  Hardware Addr   Type   Interface
Internet  192.168.12.1            -   cc01.064c.0000  ARPA   Ethernet0/0
Internet  192.168.12.2          101   cc02.065b.0000  ARPA   Ethernet0/0

用 Next Hop Interface

用 Interface 来当 Next Hop 的话,情况则有点不同。假设 R1 要送 Packet 到 192.168.101.1,跟据 Route Table,发现 Next Hop 是 Interface E0/0,没有 Next Hop IP,Router 唯有用 ARP Request 在 E0/0 问 192.168.101.1 的 MAC Address,虽然 R2 的 E0/0 并非 192.168.101.1,但 R2 的 E0/0 居然会回覆自己的 MAC Address,原因是 Interface 的 Proxy ARP 预设是 Enable 的,如果 R2 的 Route Table 有 192.168.101.1 的话,Proxy ARP 设定会使 R2 的 E0/0 回覆。

R1#show run | i route
ip route 192.168.101.0 255.255.255.0 Ethernet0/0
ip route 192.168.102.0 255.255.255.0 Ethernet0/0
ip route 192.168.103.0 255.255.255.0 Ethernet0/0
ip route 192.168.104.0 255.255.255.0 Ethernet0/0
R1#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

C    192.168.12.0/24 is directly connected, Ethernet0/0
S    192.168.101.0/24 is directly connected, Ethernet0/0
S    192.168.102.0/24 is directly connected, Ethernet0/0
S    192.168.103.0/24 is directly connected, Ethernet0/0
S    192.168.104.0/24 is directly connected, Ethernet0/0

R1#ping 192.168.101.1

Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 192.168.101.1, timeout is 2 seconds:
.!!!!
Success rate is 80 percent (4/5), round-trip min/avg/max = 8/36/116 ms
R1#show arp
Protocol  Address          Age (min)  Hardware Addr   Type   Interface
Internet  192.168.101.1           0   cc02.065b.0000  ARPA   Ethernet0/0
Internet  192.168.12.1            -   cc01.064c.0000  ARPA   Ethernet0/0
Internet  192.168.12.2          112   cc02.065b.0000  ARPA   Ethernet0/0

问题来了,按照这个机制,即是说 R1 要为每个 Destination IP 查询 ARP,这样 ARP Record 就会迅速增加了!这会大大影响 Router 的效能,因此请避免使用这种 Static Route 设定。

R1#show arp
Protocol  Address          Age (min)  Hardware Addr   Type   Interface
Internet  192.168.104.1           0   cc02.065b.0000  ARPA   Ethernet0/0
Internet  192.168.101.1          12   cc02.065b.0000  ARPA   Ethernet0/0
Internet  192.168.103.1           0   cc02.065b.0000  ARPA   Ethernet0/0
Internet  192.168.102.1           0   cc02.065b.0000  ARPA   Ethernet0/0
Internet  192.168.12.1            -   cc01.064c.0000  ARPA   Ethernet0/0
Internet  192.168.12.2          124   cc02.065b.0000  ARPA   Ethernet0/0

用 Next Hop Interface 及 IP

如果必需要使用 Next Hop Interface 来限制 Packet 送到某个 Outgoing Interface 的话,最好把 Next Hop IP 也一併写进去,目的是避免 ARP Record 记录多个 Destination IP。

R1#show run | i route
ip route 192.168.101.0 255.255.255.0 Ethernet0/0 192.168.12.2
ip route 192.168.102.0 255.255.255.0 Ethernet0/0 192.168.12.2
ip route 192.168.103.0 255.255.255.0 Ethernet0/0 192.168.12.2
ip route 192.168.104.0 255.255.255.0 Ethernet0/0 192.168.12.2

透过这个设定,Packet 必然会使用 E0/0 作为 Outgoing Interface 而 Next Hop IP 则使用 192.168.12.2。问题是如果 E0/0 Down 了的话,所有以此 Interface 作为出口的 Route 就会消失。

R1#show arp
Protocol  Address          Age (min)  Hardware Addr   Type   Interface
Internet  192.168.12.1            -   cc01.064c.0000  ARPA   Ethernet0/0
Internet  192.168.12.2            3   cc02.065b.0000  ARPA   Ethernet0/0

Cisco Express Forwarding (CEF) 思科快递交换

所以还有什麽问题呢?不够快!试想想如果每次有 Packet 进来,都要从 Router Table 找 Route,而且可能不只一次 (因为 Recursive Lookup),但若果 Route Table 没有更新的话,基本上每次结果都一様。为了省掉这个 Route Lookup 的程序,就出现了 CEF。CEF 可以把 Route Lookup 的结果保存在 CEF Table,当 Router 再次收到同一个 Destination 的 Packet 的话,不用找 Route Table 了,按 CEF Table 的纪录直接转走 (Switching)。所以 CEF 所做的是所谓 Route Once Switch Many。当 Route Table 有更新时,CEF Table 也会被更新。用 show ip cef 可以查看 CEF Table。

R1#show ip cef
Prefix              Next Hop             Interface
0.0.0.0/0           drop                 Null0 (default route handler entry)
0.0.0.0/32          receive
192.168.2.0/23      192.168.12.2         Ethernet0/0
192.168.12.0/24     attached             Ethernet0/0
192.168.12.0/32     receive
192.168.12.1/32     receive
192.168.12.2/32     192.168.12.2         Ethernet0/0
192.168.12.255/32   receive
192.168.101.0/24    192.168.12.2         Ethernet0/0
192.168.102.0/24    192.168.12.2         Ethernet0/0
192.168.103.0/24    192.168.12.2         Ethernet0/0
192.168.104.0/24    192.168.12.2         Ethernet0/0
224.0.0.0/4         drop
224.0.0.0/24        receive
255.255.255.255/32  receive

Policy Based Routing (PBR) 基于策略的路由

关于 Routing Decision 最后要说的是 Policy Based Routing。PBR 是一双可以干预 Routing Decision 的幕后黑手,它可以把符合特定条件的 Packet,硬性设定其 Next Hop 而不查询 Route Table 或 CEF Table。一个比较常用的例子为检查 Packet 的 Source Address,为特定来源的 Packet 预设 Routing Path。现在用以下网络来测试一下。

mst

网络只设定了基本的 EIGRP 作为 Dynamic Routing Protocol。各 Router 设定如下:

hostname R1
!
interface Loopback1
 ip address 1.1.1.1 255.255.255.255
!
interface Loopback2
 ip address 2.2.2.2 255.255.255.255
!
interface Ethernet0/0
 ip address 192.168.12.1 255.255.255.0
!
router eigrp 1
 network 0.0.0.0
 no auto-summary
hostname R2
!
interface Ethernet0/0
 ip address 192.168.12.2 255.255.255.0
!
interface Ethernet0/1
 ip address 192.168.23.2 255.255.255.0
!
interface Ethernet0/2
 bandwidth 5  // 故意减少 Bandwidth 来增加 Cost
 ip address 192.168.24.2 255.255.255.0
!
router eigrp 1
 network 0.0.0.0
 no auto-summary
hostname R3
!
interface Ethernet0/0
 ip address 192.168.23.3 255.255.255.0
!
interface Ethernet0/1
 ip address 192.168.35.3 255.255.255.0
!
router eigrp 1
 network 0.0.0.0
 no auto-summary
hostname R4
!
interface Ethernet0/0
 ip address 192.168.24.4 255.255.255.0
!
interface Ethernet0/1
 ip address 192.168.45.4 255.255.255.0
!
router eigrp 1
 network 0.0.0.0
 no auto-summary
hostname R5
interface Loopback1
 ip address 5.5.5.5 255.255.255.255
!
interface Ethernet0/0
 ip address 192.168.35.5 255.255.255.0
!
interface Ethernet0/1
 ip address 192.168.45.5 255.255.255.0
!
router eigrp 1
 network 0.0.0.0
 no auto-summary

由于故意减少了 R2 E0/2 的 Bandwidth 来增加 Cost,基于 EIGRP 的特性,R2 必然把所有要到 5.5.5.5 的 Packet 传到 R3。请看 R2 的 Route Table。

R2#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

C    192.168.12.0/24 is directly connected, Ethernet0/0
     1.0.0.0/32 is subnetted, 1 subnets
D       1.1.1.1 [90/409600] via 192.168.12.1, 00:30:03, Ethernet0/0
     2.0.0.0/32 is subnetted, 1 subnets
D       2.2.2.2 [90/409600] via 192.168.12.1, 00:30:03, Ethernet0/0
D    192.168.45.0/24 [90/332800] via 192.168.23.3, 00:28:07, Ethernet0/1
C    192.168.24.0/24 is directly connected, Ethernet0/2
     5.0.0.0/32 is subnetted, 1 subnets
D       5.5.5.5 [90/435200] via 192.168.23.3, 00:28:07, Ethernet0/1
C    192.168.23.0/24 is directly connected, Ethernet0/1
D    192.168.35.0/24 [90/307200] via 192.168.23.3, 00:28:15, Ethernet0/1

因此,在 R1 无论 Source IP 是什么,都会用 R1 ➡️ R2 ➡️ R3 ➡️ R5 这条 Path。

R1#traceroute 5.5.5.5

Type escape sequence to abort.
Tracing the route to 5.5.5.5

  1 192.168.12.2 760 msec 456 msec 576 msec
  2 192.168.23.3 1664 msec 1260 msec 504 msec
  3 192.168.35.5 40 msec 32 msec 28 msec
R1#
R1#traceroute 5.5.5.5 source 1.1.1.1

Type escape sequence to abort.
Tracing the route to 5.5.5.5

  1 192.168.12.2 36 msec 20 msec 20 msec
  2 192.168.23.3 40 msec 44 msec 36 msec
  3 192.168.35.5 64 msec 60 msec 60 msec
R1#
R1#traceroute 5.5.5.5 source 2.2.2.2

Type escape sequence to abort.
Tracing the route to 5.5.5.5

  1 192.168.12.2 28 msec 8 msec 12 msec
  2 192.168.23.3 28 msec 24 msec 20 msec
  3 192.168.35.5 40 msec 32 msec 28 msec

重头戏来了,现在我们在 R2 用 Route Map 去设定 PBR。

Step 1:设定 Access List

假设我们想干预来自 Source IP 2.2.2.2 而 Destination IP 5.5.5.5 的 Traffic,我们先设定 Access List。

R2(config)#access-list 100 permit ip host 2.2.2.2 host 5.5.5.5

Step 2:设定 Route Map

整个 Route Map 设定的意思是:如果 match access-list 100,就把 next-hop 改为 192.168.24.4。

R2(config)#route-map PBR permit 10
R2(config-route-map)# match ip address 100  
R2(config-route-map)# set ip next-hop 192.168.24.4

Step 3:把 Route Map 放进 Interface

R2(config)#int ethernet 0/0
R2(config-if)# ip policy route-map PBR

于是,现在由 2.2.2.2 到 5.5.5.5 的 Traffic 就会被硬性更改 Next Hop 为 192.168.24.4,使用 R1 ➡️ R2 ➡️ R4 ➡️ R5 这条 path,其他 Source 则维持不变。

R1#traceroute 5.5.5.5

Type escape sequence to abort.
Tracing the route to 5.5.5.5

  1 192.168.12.2 1056 msec 1268 msec 280 msec
  2 192.168.23.3 40 msec 28 msec 16 msec
  3 192.168.35.5 52 msec 40 msec 40 msec
R1#
R1#traceroute 5.5.5.5 source 1.1.1.1

Type escape sequence to abort.
Tracing the route to 5.5.5.5

  1 192.168.12.2 52 msec 12 msec 8 msec
  2 192.168.23.3 20 msec 16 msec 20 msec
  3 192.168.35.5 32 msec 32 msec 28 msec
R1#
R1#traceroute 5.5.5.5 source 2.2.2.2

Type escape sequence to abort.
Tracing the route to 5.5.5.5

  1 192.168.12.2 40 msec 20 msec 20 msec
  2 192.168.24.4 40 msec 40 msec 40 msec
  3 192.168.45.5 32 msec 40 msec 28 msec

使用 PBR 时请了解,由于用 PBR 改动 Next Hop 后就不能应用 CEF 带来的 Fast Switching,取而代之的是耗用了 CPU 资源去处理 Packet,在 Traffic 量大的 Interface 使用时必需留意 CPU 使用量。