初识路由
引言
-
在《TCP/IP基础知识》一文中介绍到:网络层的主要作用是IP编址和路由选择;路由器属于网络层设备,可以实现不同网络之间的通信。那么路由器如何完成路由选择过程?本篇将围绕此问题展开相关内容的学习总结。
内容
-
网络通信的过程对于网络设备而言是封装与解封装的过程,在这个过程中必须获得有关数据链路标识和数据封装的信息,并且这些信息要保存在数据库中,如二层的ARP表项,同样传输层和网络层也需要获取和保存所涉及到的相关信息,这些信息被保存在路由表中,接下来将主要介绍路由表的来源、内容以及路由器的路由选择规则。
(1)路由表的来源
直连路由
-
直连路由是一切路由产生的前提
-
条件:接口UP,合法的IP地址,只要满足此条件,接口的所在的网络地址将作为直接相连的网络加入到路由表中
-
静态路由的管理距离【AD】值为0,管理距离是路由选择的一个重要指标,路由表中的每条路由条目都会标识该条目的AD值,其范围是0-255,越低越优先,0表示绝对信任,255表示不可达路由
静态路由
-
静态路由的AD值为1
-
优点:配置简单、对路由的控制精确性高
-
缺点:大中型网络中配置量大,网络拓扑发生变化时需要重新进行手工配置
动态路由
-
常见的动态路由协议包括:RIP、EIGRP、OSPF、ISIS、BGP
-
动态路由要求更少的人为干涉
(2)路由表的内容
目标地址——路由器到达的网路地址,路由表中可能会存在多条路由到达相同的地址或者相同主网IP地址下的一组等长或变长的子网
指向目标的指针——指向目标的指针就是该路由器到达目标网络的下一跳地址或本地的出接口
(3)路由选择规则
最长匹配子网掩码——路由条目中的子网掩码决定数据包的目的IP地址必须有多少位匹配才能使用这条路由;路由器会尽量进行最精确的匹配,按照精确程度递减的顺序,可选的地址排列如下:主机地址、子网、汇总路由、主网号、一组主网号、缺省路由。
递归查找(路由解析)——路由条目中仅有下一跳IP地址而没有出接口,那么必须将其解析为具有出接口的路由,为此会对下一跳IP地址执行递归查找,直到将该路由解析为某一出接口为止。
应用
-
案例一:静态路由的配置
配置方法:1.出接口;2.下一跳;3.出接口+下一跳
配置命令:ip route + 目的网络地址+ 子网掩码+下一跳地址/出接口
配置区别:如果使用下一跳地址,那么该地址必须可达;同时出接口必须配置IP地址,接口必须正常工作(状态为UP);
两种配置方法产生的路由表存在差别:使用出接口配置的静态路由,它们将作为直连网络输入在路由表中;使用下一跳地址配置的静态路由,在路由表中产生的路由条目为MD=1的静态路由;
推荐配置方法:出接口+下一跳:若使用出接口的配置方法,可能会导致广播网络上出现过多的流量,原因是,配置出接口的默认路由,路由器会认为是直连网络,当路由器向目标主机转发数据包时,路由器会发送ARP请求以便获取目标主机的MAC地址,这样路由器若要到达不同的目标地址时,都会发送ARP请求,就会导致ARP表项的无限膨胀;而配置下一跳默认路由,路由器不再认为目标网络是直连路由,ARP的请求对象只可能是下一跳地址,这样只需进行一次ARP请求即可;如果仅配置下一跳默认路由,根据递归查找原则,路由器必须找出发送报文的出接口,这样的话就会增加查找的时间;因此指定出接口和下一跳地址可以最小化与下一跳地址关联的出接口查询,并且把广播网络上的流量减到最小。
典型案例:(使用GNS3完成)
拓扑:实现3台路由器互通
配置:
步骤一:配置接口IP地址: R1(config)#interface fastEthernet 0/0 R1(config-if)#ip add 12.1.1.1 255.255.255.0 R1(config-if)#no shutdown *Mar 1 00:20:30.947: %LINK-3-UPDOWN: Interface FastEthernet0/0, changed state to up *Mar 1 00:20:31.947: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up R1(config-if)#exit R1(config)#end R1#show ip interface brief Interface IP-Address OK? Method Status Protocol FastEthernet0/0 12.1.1.1 YES manual up up FastEthernet0/1 unassigned YES unset administratively down down R2(config)#interface fastEthernet 0/0 R2(config-if)#ip add 12.1.1.2 255.255.255.0 R2(config-if)#no shutdown *Mar 1 00:20:23.551: %LINK-3-UPDOWN: Interface FastEthernet0/0, changed state to up *Mar 1 00:20:24.551: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up R2(config-if)#exit R2(config)#interface fastEthernet 0/1 R2(config-if)#ip add 23.1.1.1 255.255.255.0 R2(config-if)#no shutdown R2(config-if)#end *Mar 1 00:20:41.367: %LINK-3-UPDOWN: Interface FastEthernet0/1, changed state to up *Mar 1 00:20:42.367: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/1, changed state to up R2#show ip interface brief Interface IP-Address OK? Method Status Protocol FastEthernet0/0 12.1.1.2 YES manual up up FastEthernet0/1 23.1.1.1 YES manual up up R2# R3(config)#interface fastEthernet 0/1 R3(config-if)#ip add 23.1.1.2 255.255.255.0 R3(config-if)#no shutdown R3(config-if)#end *Mar 1 00:22:17.687: %LINK-3-UPDOWN: Interface FastEthernet0/1, changed state to up *Mar 1 00:22:18.687: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/1, changed state to up R3#show ip interface brief Interface IP-Address OK? Method Status Protocol FastEthernet0/0 unassigned YES unset administratively down down FastEthernet0/1 23.1.1.2 YES manual up up R3# 步骤二:配置静态路由: R1上配置出接口默认路由: R1(config)#ip route 23.1.1.0 255.255.255.0 fastEthernet 0/0 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 23.0.0.0/24 is subnetted, 1 subnets S 23.1.1.0 is directly connected, FastEthernet0/0 12.0.0.0/24 is subnetted, 1 subnets C 12.1.1.0 is directly connected, FastEthernet0/0 R1上配置出接口+下一跳默认路由: R1(config)#ip route 23.1.1.0 255.255.255.0 fastEthernet 0/0 12.1.1.2 R1(config)#end R1#shyo *Mar 1 00:33:58.491: %SYS-5-CONFIG_I: Configured from console by console R1#sh 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 23.0.0.0/24 is subnetted, 1 subnets S 23.1.1.0 [1/0] via 12.1.1.2, FastEthernet0/0 12.0.0.0/24 is subnetted, 1 subnets C 12.1.1.0 is directly connected, FastEthernet0/0 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 23.0.0.0/24 is subnetted, 1 subnets C 23.1.1.0 is directly connected, FastEthernet0/1 12.0.0.0/24 is subnetted, 1 subnets C 12.1.1.0 is directly connected, FastEthernet0/0 R3上配置下一跳默认路由: R3(config)#ip route 12.1.1.0 255.255.255.0 23.1.1.1 R3(config)#end R3#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 23.0.0.0/24 is subnetted, 1 subnets C 23.1.1.0 is directly connected, FastEthernet0/1 12.0.0.0/24 is subnetted, 1 subnets S 12.1.1.0 [1/0] via 23.1.1.1 步骤3:结果检验: R1#ping 23.1.1.2 Type escape sequence to abort. Sending 5, 100-byte ICMP Echos to 23.1.1.2, timeout is 2 seconds: !!!!! Success rate is 100 percent (5/5), round-trip min/avg/max = 60/92/152 ms R3#ping 12.1.1.1 Type escape sequence to abort. Sending 5, 100-byte ICMP Echos to 12.1.1.1, timeout is 2 seconds: !!!!! Success rate is 100 percent (5/5), round-trip min/avg/max = 72/108/140 ms
-
案例二:汇总路由
解释:汇总路由是一个包含路由表中几个更加精确地址的路由条目。通过使用合适的子网掩码,可以为多个目标地址生成一条单一的汇总路由。
优缺点:汇总路由可以减少路由条目,但是汇总处理不正确,可能会带来意想不到的路由行为发生,如:路由黑洞等问题
举例:在R1设备上配置一条可以到达任意网段的默认路由
默认汇总路由的配置方法: R1(config)#ip route 0.0.0.0 0.0.0.0 fastEthernet 0/0 12.1.1.2 R1(config)#end R1# R1#s *Mar 1 00:35:54.275: %SYS-5-CONFIG_I: Configured from console by console 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 12.1.1.2 to network 0.0.0.0 23.0.0.0/24 is subnetted, 1 subnets S 23.1.1.0 [1/0] via 12.1.1.2, FastEthernet0/0 12.0.0.0/24 is subnetted, 1 subnets C 12.1.1.0 is directly connected, FastEthernet0/0 S* 0.0.0.0/0 [1/0] via 12.1.1.2, FastEthernet0/0
-
案例三:浮动静态路由
解释:浮动静态路由与其他路由不同,路由表中的路由总是优先于浮动静态路由,仅在特殊情况下,即首选路由发生失败的时候。浮动路由才会出现在路由表中。目的是实现对主线路的备份作用。
实现:通过修改路由的管理距离来实现
配置案例:
拓扑:实现当以太网线路断开时,通过广域网线路通信
配置: -
步骤一:在案例一的基础上配置接口IP地址: R1(config)#interface serial 1/0 R1(config-if)#ip add 10.1.1.1 255.255.255.0 R1(config-if)#no shutdown 步骤二:配置到达目标网段的主备线路: R1(config)#ip route 23.1.1.0 255.255.255.0 fastEthernet 0/0 R1(config)#ip route 23.1.1.0 255.255.255.0 serial 1/0 12.1.1.2 10 //10为管理距离,可以自己设置,在【2-254】之间即可。 R1(config)#end *Mar 1 00:15:41.287: %SYS-5-CONFIG_I: Configured from console by console 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 23.0.0.0/24 is subnetted, 1 subnets S 23.1.1.0 is directly connected, FastEthernet0/0 10.0.0.0/24 is subnetted, 1 subnets C 10.1.1.0 is directly connected, Serial1/0 12.0.0.0/24 is subnetted, 1 subnets C 12.1.1.0 is directly connected, FastEthernet0/0 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 23.0.0.0/24 is subnetted, 1 subnets C 23.1.1.0 is directly connected, FastEthernet0/1 10.0.0.0/24 is subnetted, 1 subnets C 10.1.1.0 is directly connected, Serial0/0 12.0.0.0/24 is subnetted, 1 subnets C 12.1.1.0 is directly connected, FastEthernet0/0 R3#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 23.0.0.0/24 is subnetted, 1 subnets C 23.1.1.0 is directly connected, FastEthernet0/1 10.0.0.0/24 is subnetted, 1 subnets S 10.1.1.0 is directly connected, FastEthernet0/1 //注意:R3上面也要存在到达备份线路网段的路由,因为数据通信是双向的; 12.0.0.0/24 is subnetted, 1 subnets S 12.1.1.0 [1/0] via 23.1.1.1 is directly connected, FastEthernet0/1 R1#ping 23.1.1.2 Type escape sequence to abort. Sending 5, 100-byte ICMP Echos to 23.1.1.2, timeout is 2 seconds: !!!!! 步骤三:检验浮动静态路由是否生效:当主接口down掉的时候,是否存在备份路由; R1(config)#interface fastEthernet 0/0 R1(config-if)#shutdown R1(config-if)#end *Mar 1 00:19:23.779: %LINK-5-CHANGED: Interface FastEthernet0/0, changed state to administratively down *Mar 1 00:19:24.779: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to down 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 23.0.0.0/24 is subnetted, 1 subnets S 23.1.1.0 [10/0] via 12.1.1.2, Serial1/0 //存在备份路由 10.0.0.0/24 is subnetted, 1 subnets C 10.1.1.0 is directly connected, Serial1/0 R1#ping 23.1.1.2 Type escape sequence to abort. Sending 5, 100-byte ICMP Echos to 23.1.1.2, timeout is 2 seconds: !!!!! Success rate is 100 percent (5/5), round-trip min/avg/max = 44/88/148 ms
推荐书籍
《TCP/IP路由技术卷一》