初識路由
引言
-
在《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
-
案例三:浮動靜態路由
解釋:浮動靜態路由與其他路由不同,路由表中的路由總是優先於浮動靜態路由,僅在特殊情況下,即首選路由發生失敗的時候。浮動路由才會出現在路由表中。目的是實現對主線路的備份作用。
實現:通過修改路由的管理距離來實現
配置案例:
拓撲:實現當以太網線路斷開時,通過廣域網線路通信

配置: -
config步驟一:在案例一的基礎上配置接口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路由技術卷一》
