第 21 章路由优化 在当今高性能的网络中, 为了保证网络的伸缩性 稳定性 安全性和快速收敛, 必须对网络进行优化 路由过滤和策略路由是路由优化的常用方法 21.1 路由优化概述 路由过滤是指在路由更新中抑制某些路由不被发送和接收, 被动接口 分布控制列表 重分布结合路由策略等都可以实现路由过滤 策略路由提供了根据网络管理者制定的标准来进行数据包转发的一种机制 基于策略的路由比传统路由能力更强, 使用更灵活, 它使网络管理者不仅能够根据目的地址而且能够根据协议类型 报文大小 应用或 IP 源地址来选择转发路径 策略路由的策略由路由映射图 (route map) 来定义 route map 命令中最为重要的是 和 set 用来定义匹配的条件, 匹配语句在路由器的输入端口对数据包进行检测 常用的匹配条件包括 IP 地址 接口 度量值以及数据包长度等 set 定义对符合匹配条件的语句采取的行为 通常的行为如表 21-1 所示 set 行为 set ip next hop set interface set ip default next hop set default interface set ip tos set ip precedence 表 21-1 set 的行为描述设定数据包的下一跳地址设定数据包出接口设定缺省的下一跳地址, 用于当路由表里没有到数据包目的地址路由条目时设定缺省的出接口设定 IP 数据包的 IP ToS 值设定 IP 数据包的优先级 注意 1. 一个 route map 的最后默认 deny any 这个 deny 的使用结果依赖于这个 route map 是怎样使用的 如果一个数据包对于 route map 没有匹配项, 它会按照正常的目的地址路由转发, 而对于路由条目如果 route map 没有匹配项, 则被拒绝 ; 2. 一个 route map 可以包含多个 route map 陈述, 这些语句的执行顺序像 ACL 一样, 从上到下被执行 21.2 实验 1: 用分布控制列表控制路由更新 1. 实验目的通过本实验可以掌握 (1) 被动接口的配置 (2) 分布控制列表的配置 2. 拓扑结构实验拓扑如图 21-1 所示
图 21-1 用分布控制列表控制路由更新 本实验通过使用分布控制列表控制路由器 R1 只发送环回接口中第三位为奇数的路由和 g0/0 接口的路由更新给 R2, 整个网络运行 RIPv2 路由协议 3. 实验步骤 (1) 步骤 1: 配置路由器 R1 R1(config)#access-list 1 permit 172.16.1.0 R1(config)#access-list 1 permit 1.1.1.0 0.0.254.0 // 允许第三位为奇数的路由 R1(config)#router rip R1(config-router)#version 2 R1(config-router)#no auto-summary R1(config-router)#network 1.0.0.0 R1(config-router)#network 172.16.0.0 R1(config-router)#network 192.168.12.0 R1(config-router)#passive-interface default // 默认为被动接口 R1(config-router)#no passive-interface Serial0/0/0 // 关闭被动接口 R1(config-router)#distribute-list 1 out Serial0/0/0// 出方向配置分布控制列表 注意 distribute-list 命令可以全局的在一个出或入方向的路由更新中过滤路由, 也可以为一个路由进程所涉及到的每一个接口的入方向或出方向设置路由过滤 (2) 步骤 2: 配置路由器 R2 R2(config)#router rip R2(config-router)#version 2 R2(config-router)#no auto-summary R2(config-router)#network 2.0.0.0 R2(config-router)#network 192.168.12.0 4. 实验调试 (1) 在 R2 上查看路由表 : 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, 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 R R R R C R 192.168.12.0/24 is directly connected, Serial0/0/0 1.0.0.0/24 is subnetted, 4 subnets 1.1.1.0 [120/1] via 192.168.12.1, 00:00:01, Serial0/0/0 1.1.3.0 [120/1] via 192.168.12.1, 00:00:01, Serial0/0/0 1.1.5.0 [120/1] via 192.168.12.1, 00:00:01, Serial0/0/0 1.1.7.0 [120/1] via 192.168.12.1, 00:00:01, Serial0/0/0 2.0.0.0/24 is subnetted, 1 subnets 2.2.2.0 is directly connected, Loopback0 172.16.0.0/24 is subnetted, 1 subnets 172.16.1.0 [120/1] via 192.168.12.1, 00:00:01, Serial0/0/0 以上输出表明路由器 R2 只收到 R1 的以太口和第三位为奇数的环回接口的路由 (2)show ip protocols R1#show ip protocols Routing Protocol is "rip" Sending updates every 30 seconds, next due in 2 seconds Invalid after 180 seconds, hold down 180, flushed after 240 Outgoing update filter list for all interfaces is not set Serial0/0/0 filtered by 1 (per-user), default is 1 Incoming update filter list for all interfaces is not set Redistributing: rip Default version control: send version 2, receive version 2... 以上输出信息表明, 全局下没有作用分布控制列表, 在 s0/0/0 接口的出方向作用了分布控制列表 1 21.3 策略路由 21.3.1 实验 2: 基于源 IP 地址的策略路由 1. 实验目的通过本实验可以掌握 (1) 用 route-map 定义路由策略 (2) 在接口下应用路由策略 (3) 基于源 IP 地址的策略路由的调试 2. 拓扑结构实验拓扑如图 21-2 所示
图 21-2 基于源 IP 地址的策略路由 本实验设计如下 : 在路由器 R1 的 g0/0 接口应用 IP 策略路由 CCNA, 使得从主机 A 来的数据设置下一跳地址为 192.168.12.2; 从主机 B 来的数据设置下一跳地址为 192.168.21.2, 所有其它的数据包正常转发, 整个网络运行 EIGRP 路由协议 3. 实验步骤 (1) 步骤 1: 配置路由器 R1 R1(config)#access-list 1 permit 10.1.1.2 R1(config)#access-list 2 permit 10.1.1.3 R1(config)#route-map CCNA permit 10 R1(config-route-map)# ip address 1 R1(config-route-map)#set ip next-hop 192.168.12.2 R1(config)#route-map CCNA permit 20 R1(config-route-map)# ip address 2 R1(config-route-map)#set ip next-hop 192.168.21.2 R1(config)#interface g0/0 R1(config-if)#ip policy route-map CCNA R1(config)#router eigrp 1 R1(config-router)#no auto-summary R1(config-router)#network 10.1.1.0 255.255.255.0 R1(config-router)#network 192.168.12.0 R1(config-router)#network 192.168.21.0 4. 实验调试 (1) 主机 A 上 ping 地址 2.2.2.2, 路由器 R1 上显示的调试信息如下 : R1#debug ip policy *Feb 20 00:18:33.368: IP: s=10.1.1.2 (GigabitEthernet0/0), d=2.2.2.2, len 60, policy *Feb 20 00:18:33.368: IP: route map CCNA, item 10, permit *Feb 20 00:18:33.372: IP: s=10.1.1.2 (GigabitEthernet0/0), d=2.2.2.2 (Serial0/0/0), len 60, policy routed *Feb 20 00:18:36.381: IP: GigabitEthernet0/0 to Serial0/0/0 192.168.12.2 以上输出信息表明源地址为 10.1.1.2 的主机发送给目的主机 2.2.2.2 的数据包在接口 GigabitEthernet0/0 匹配 route-map CCNA 的序列号 10 所定义的策略, 执行策略路由, 设置数据包下一跳地址为 192.168.12.2
(2) 主机 B 上 ping 地址 2.2.2.2, 路由器 R1 上显示的调试信息如下 : *Feb 20 00:21:12.449: IP: s=10.1.1.3 (GigabitEthernet0/0), d=2.2.2.2, len 60, policy *Feb 20 00:21:12.449: IP: route map CCNA, item 20, permit *Feb 20 00:21:12.453: IP: s=10.1.1.3 (GigabitEthernet0/0), d=2.2.2.2 (Serial0/0/1), len 60, policy routed *Feb 20 00:21:12.453: IP: GigabitEthernet0/0 to Serial0/0/1 192.168.21.2 数以上输出信息表明源地址为 10.1.1.3 的主机发送给目的主机 2.2.2.2 数据包在接口 GigabitEthernet0/0 匹配 route-map CCNA 的序列号 20 所定义的策略, 执行策略路由, 设置数据包下一跳地址为 192.168.21.2 (3) 在主机 10.1.1.6 上 ping 地址 2.2.2.2, 路由器 R1 上显示的调试信息如下 : *Feb 20 00:14:17.416: IP: s=10.1.1.6 (GigabitEthernet0/0), d=2.2.2.2 (Serial0/0/0), len 60, policy rejected -- normal forwarding 以上输出信息表明源地址为 10.1.1.6 的主机发送到目的主机 2.2.2.2 数据包在接口 GigabitEthernet0/0 不匹配路由策略, 数据包正常转发 (4)show ip policy 该命令显示了在哪些接口上应用了哪些策略 R1#show ip policy Interface Route map Gi0/0 CCNA 以上输出信息表明在 Gi0/0 接口应用了路由策略 CCNA 21.3.2 实验 3: 基于报文大小的策略路由 1. 实验目的通过本实验可以掌握 (1) 用 route-map 定义路由策略 (2) 在接口下应用路由策略 (3) 基于报文大小的策略路由的调试 2. 拓扑结构实验拓扑如图 21-3 所示 图 21-3 基于报文大小的策略路由 本实验设计如下 : 在路由器 R1 的 g0/0 接口应用 IP 策略路由 CCNP, 使得对大小为 64-100 字节的数据包设置出接口为 s0/0/0; 大小为 101-1000 字节的数据包设置出接口为 s0/0/1, 所有其它的数据包正常转发, 整个网络运行 EIGRP 路由协议
3. 实验步骤 (1) 步骤 1: 配置路由器 R1 R1(config)#route-map CCNP permit 10 R1(config-route-map)# length 64 100 R1(config-route-map)#set interface s0/0/0 R1(config)#route-map CCNP permit 20 R1(config-route-map)# length 101 1000 R1(config-route-map)#set interface s0/0/1 R1(config)#interface g0/0 R1(config-if)#ip policy route-map CCNP R1(config)#ip local policy route-map CCNP R1(config)#router eigrp 1 R1(config-router)#no auto-summary R1(config-router)#network 10.1.1.0 255.255.255.0 R1(config-router)#network 192.168.12.0 R1(config-router)#network 192.168.21.0 注意 在接口下应用的策略路由对路由器本地产生的数据包不起作用 如果需要对本地路由器产生的数据包执行策略路由, 要用 ip local policy route-map 命令配置本地策略 4. 实验调试 (1) 执行扩展 ping 命令, 数据包的长度为 90, 源地址为 10.1.1.6( 路由器 R3 的以太口地址 ): R3#ping Protocol [ip]: Target IP address: 2.2.2.2 Repeat count [5]: 1 Datagram size [100]: 90 Timeout in seconds [2]: Extended commands [n]: y Source address or interface: 10.1.1.6 Type of service [0]: Set DF bit in IP header? [no]: Validate reply data? [no]: Data pattern [0xABCD]: Loose, Strict, Record, Timestamp, Verbose[none]: Sweep range of sizes [n]: Type escape sequence to abort. Sending 1, 90-byte ICMP Echos to 2.2.2.2, timeout is 2 seconds: Packet sent with a source address of 10.1.1.6 路由器 R1 上显示的调试信息如下 : R1#debug ip policy *Feb 20 20:57:06.535: IP: s=10.1.1.6 (GigabitEthernet0/0), d=2.2.2.2, len 90, FIB policy
*Feb 20 20:57:06.535: IP: s=10.1.1.6 (GigabitEthernet0/0), d=2.2.2.2 (Serial0/0/0), len 90, FIB policy routed 以上输出信息表明长度为 90 字节的数据包在接口 GigabitEthernet0/0 匹配策略, 执行策略路由, 设置数据包出接口为 s0/0/0 (2) 执行扩展 ping 命令, 数据包的长度为 300, 源地址为 10.1.1.6: R3#ping Protocol [ip]: Target IP address: 2.2.2.2 Repeat count [5]: 1 Datagram size [100]: 300 Timeout in seconds [2]: Extended commands [n]: y Source address or interface: 10.1.1.6 Type of service [0]: Set DF bit in IP header? [no]: Validate reply data? [no]: Data pattern [0xABCD]: Loose, Strict, Record, Timestamp, Verbose[none]: Sweep range of sizes [n]: Type escape sequence to abort. Sending 1, 300-byte ICMP Echos to 2.2.2.2, timeout is 2 seconds: Packet sent with a source address of 10.1.1.6 路由器 R1 上显示的调试信息如下 : *Feb 20 20:58:39.311: IP: s=10.1.1.6 (GigabitEthernet0/0), d=2.2.2.2, len 300, FIB policy *Feb 20 20:58:39.311: IP: s=10.1.1.6 (GigabitEthernet0/0), d=2.2.2.2 (Serial0/0/1), len 300, FIB policy routed 以上输出信息表明长度为 300 字节的数据包在接口 GigabitEthernet0/0 匹配策略, 执行策略路由, 设置数据包出接口为 s0/0/1 (3) 执行扩展 ping 命令, 数据包的长度为 1200, 源地址为 10.1.1.6: R3#ping Protocol [ip]: Target IP address: 2.2.2.2 Repeat count [5]: 1 Datagram size [100]: 1200 Timeout in seconds [2]: Extended commands [n]: y Source address or interface: 10.1.1.6 Type of service [0]: Set DF bit in IP header? [no]: Validate reply data? [no]: Data pattern [0xABCD]: Loose, Strict, Record, Timestamp, Verbose[none]:
Sweep range of sizes [n]: Type escape sequence to abort. Sending 1, 1200-byte ICMP Echos to 2.2.2.2, timeout is 2 seconds: Packet sent with a source address of 10.1.1.6 路由器 R1 上显示的调试信息如下 : *Feb 20 21:01:20.583: IP: s=10.1.1.6 (GigabitEthernet0/0), d=2.2.2.2, len 1200, FIB policy rejected(no ) - normal forwarding 以上输出信息表明长度为 1200 字节的数据包在接口 GigabitEthernet0/0 不匹配路由策略, 数据包正常转发 (4)show route-map 该命令显示定义的所有路由策略及路由策略匹配的情况 R1#show route-map route-map CCNP, permit, sequence 10 Match clauses: length 64 100 Set clauses: interface Serial0/0/0 Policy routing es: 3 packets, 292 bytes route-map CCNP, permit, sequence 20 Match clauses: length 101 1000 Set clauses: interface Serial0/0/1 Policy routing es: 1 packets, 314 bytes (5)show ip policy R1#show ip policy Interface Route map local CCNP Gi0/0 CCNP 以上输出信息表明在接口 Gi0/0 和本地应用了路由策略 CCNP 21.3.2 实验 4: 基于应用的策略路由 1. 实验目的通过本实验可以掌握 (1) 用 route-map 定义路由策略 (2) 在接口下应用路由策略 (3) 基于应用的策略路由的调试 2. 拓扑结构实验拓扑如图 21-4 所示
图 21-4 基于应用的策略路由 本实验设计如下 : 在路由器 R1 的 g0/0 接口应用 IP 策略路由 CCIE, 使得对 HTTP 数据包设置下一跳地址为 192.168.12.2, 并且设置 IP 数据包优先级为 flash, 为 Telnet 数据包设置下一跳地址为 192.168.21.2, 并且设置 IP 数据包优先级为 critical, 所有其它的数据包正常转发, 整个网络运行 EIGRP 路由协议 3. 实验步骤 (1) 步骤 1: 配置路由器 R1 R1(config)#ip access-list extended HTTP R1(config-ext-nacl)#permit tcp any any eq 80 R1(config)#ip access-list extended TELNET R1(config-ext-nacl)#permit tcp any any eq 23 R1(config)#route-map CCIE permit 10 R1(config-route-map)# ip address HTTP R1(config-route-map)#set ip precedence flash R1(config-route-map)#set ip next-hop 192.168.12.2 R1(config)#route-map CCIE permit 20 R1(config-route-map)# ip address TELNET R1(config-route-map)#set ip precedence critical R1(config-route-map)#set ip next-hop 192.168.21.2 R1(config)#interface g0/0 R1(config-if)#ip policy route-map CCIE R1(config)#ip local policy route-map CCIE R1(config)#router eigrp 1 R1(config-router)#no auto-summary R1(config-router)#network 10.1.1.0 255.255.255.0 R1(config-router)#network 192.168.12.0 R1(config-router)#network 192.168.21.0 (2) 步骤 2: 配置路由器 R2 R2(config)#router eigrp 1 R2(config-router)#network 2.2.2.0 0.0.0.255 R2(config-router)#network 192.168.12.0 R2(config-router)#network 192.168.21.0
R2(config-router)#no auto-summary R2(config)#ip http server R2(config)#line vty 0 4 R2(config-line)#no login R2(config-line)#privilege level 15 4. 实验调试 (1) 在主机 A 上访问 2.2.2.2 的 http 服务, 路由器 R1 上显示的调试信息如下 : R1#debug ip policy *Feb 20 22:32:44.407: IP: s=10.1.1.2 (GigabitEthernet0/0), d=2.2.2.2, len 48, FIB policy *Feb 20 22:32:44.407: IP: s=10.1.1.2 (GigabitEthernet0/0), d=2.2.2.2, g=192.168.12.2, len 48, FIB policy routed 以上输出信息表明 http 的数据包在接口 GigabitEthernet0/0 匹配策略, 执行策略路由, 设置数据包下一跳地址为 192.168.12.2 (2) 在主机 A 上访问 2.2.2.2 的 telnet 服务, 路由器 R1 上显示的调试信息如下 : *Feb 20 22:29:28.127: IP: s=10.1.1.2 (GigabitEthernet0/0), d=2.2.2.2, len 43, FIB policy *Feb 20 22:29:28.131: IP: s=10.1.1.2 (GigabitEthernet0/0), d=2.2.2.2, g=192.168.21.2, len 43, FIB policy routed 以上输出信息表明 telnet 的数据包在接口 GigabitEthernet0/0 匹配策略, 执行策略路由, 设置数据包下一跳地址为 192.168.21.2 (3) 在主机 A 上 ping 2.2.2.2, 路由器 R1 上显示的调试信息如下 : *Feb 20 22:35:12.431: IP: s=10.1.1.2 (FastEthernet0/0), d=2.2.2.2, len 100, FIB policy rejected(no ) - normal forwarding 以上输出信息表明 ping 的数据包在接口 GigabitEthernet0/0 不匹配路由策略, 数据包正常转发 21.4 路由优化命令汇总 表 21-2 列出了本章涉及到的主要的命令 命令 show ip policy show route-map debug ip policy passive-interface distribute-list route-map set ip policy route-map ip local policy route-map 表 21-2 本章命令汇总作用查看策略路由及作用的接口查看定义的所有路由策略及路由策略匹配的情况动态查看策略路由的匹配情况配置被动接口配置分布控制列表定义路由策略定义匹配的条件定义对符合匹配条件的语句采取的行为应用路由策略本地应用路由策略