目錄
一、什么是路由?
1.1.定義
1.2.路由作用
1.3.路由類型
1.3.1.直連路由
1.3.2.靜態路由
1.3.3.動態路由
1.3.4.路由表
1.5.路由器的匹配原則
1.6.路由配置
1.6.1.靜態路由配置
1.6.2.動態路由配置
二、實驗
2.1.靜態路由
2.1.1.實驗拓撲
2.1.2.實驗過程
2.2.缺省(默認)路由
2.3.浮動路由
一、什么是路由?
1.1.定義
路由是指路由器從一個接口上收到數據包,根據數據包的目的地址進行定向并轉發到另一個接口的過程。
1.2.路由作用
路由設備作為第三層的工作設備,其最大的功能在于它連接了一個一個的局域網,使之構成一個更大的網絡。(從局部的自己玩 ---> 大家一起玩)
如:當路由器設備在接收到一個數據包時,首先它會檢查包中的目標網絡地址以判斷該包的目的地址在當前的路由表中是否存在(即路由器是否知道到達目標網絡的路徑)。如果發現包的目標地址與本路由器的某個接口所連接的網絡地址相同,那么馬上數據轉發到相應接口;如果發現包的目標地址不是自己的直連網段,路由器會查看自己的路由表,查找包的目的網絡所對應的接口,并從相應的接口轉發出去;如果路由表中記錄的網絡地址與包的目標地址不匹配,則根據路由器配置轉發到默認接口,在沒有配置默認接口的情況下會給用戶返回目標地址不可達的 ICMP 信息。
1.3.路由類型
路由的類型分為:直連路由、靜態路由、動態路由
1.3.1.直連路由
直連路由指向本地直連網絡的路由,由設備連接,接口配置后自動生成。
當路由器為路由轉發的最后一跳路由器時,IP報文匹配直連路由,路由器轉發IP報文到目的主機。使用直連路由進行路由轉發時,報文的目的IP和路由器接口IP在一個網段之中。
1.3.2.靜態路由
靜態路由是由管理員在路由器進行手工配置的固定的路由,但是當網絡發生故障或者拓撲發生變化后,靜態路由不會自動更新,必須重新手動配置。
靜態路由是在路由器中設置的固定的路由表。除非網絡管理員進行干預,否則靜態路由不會發生變化。這種配置一般用于對路由行為的精確控制、減少網絡流量,一般用于網絡規模不大、拓撲結構簡單固定的網絡中。在所有的路由中,靜態路由的優先級最高,當動態路由與靜態路由發生沖突時,以靜態路由為準。
1.3.3.動態路由
動態路由是網絡中的路由器之間根據實時網絡拓撲變化,相互通信傳遞路由信息,利用收到的路由信息通過路由選擇協議計算,更新路由表的過程。這一操作使得網絡能根據網絡實時情況進行變化,從而一定程度減少了網絡的管理任務。
1.3.4.路由表
每臺路由器中都保存著一張本地核心路由表(即設備的IP路由表),同時各個路由協議也維護著自己的路由表。
-
本地核心路由表
路由器使用本地核心路由表用來保存決策優選路由,并負責把優選路由下發到FIB表,通過FIB表指導報文進行轉發。這張路由表依據各種路由協議的優先級和度量值來選取路由。
-
協議路由表
協議路由表中存放著該協議發現的路由信息。
路由協議可以引入并發布其他協議生成的路由。例如,在路由器上運行OSPF協議,需要使用OSPF協議通告直連路由、靜態路由或者IS-IS路由時,要將這些路由引入到OSPF協議的路由表中。
路由表中的內容:
路由表的詳細結構為如下:
路由表包含信息:
1.目的地址(Destination)/掩碼(Mask):共同作用,用于標識目的網段
2.協議(Protocol):表示協議信息
3.優先級(Priority):表示優先級,數值越低,優先級越高
路由協議的類型 | 路由協議的外部優先級 |
---|---|
Direct | 0 |
OSPF | 10 |
IS-IS | 15 |
Static | 60 |
RIP | 100 |
OSPF ASE | 150 |
OSPF NSSA | 150 |
IBGP | 255 |
EBGP | 255 |
4.開銷(Cost):表示路徑開銷,開銷值越小,優先級越高
????????計算公式為:接口開銷 = 帶寬參考值 / 接口帶寬,取計算結果的整數部分作為接口開銷值(當結果小于1時取1)?
5.標志(Flags):R代表要遞歸查詢,D代表在fib轉發表中,可以直接進行轉發。
U | 該路由可以使用 |
G | 該路由是到一個網關,如果沒有該標志,說明該目的地址是直連的,區分了直接路由和間接路由 |
H | 該路由是到一個主機 |
D | D代表download to fib,在fib轉發表中的路由,可以直接進行轉發 |
R | R代表要遞歸查詢 |
M | 該路由已被重定向報文修改 |
6.下一條(NextHop):路由器轉發到達目的網段的數據包使用的嚇一跳的地址
7.出接口(Interface):數據包在經過路由識別之后,離開本路由的接口
1.5.路由器的匹配原則
最長前綴匹配原則 ---> 優先級 ---> 比較開銷值
路由器在接收到一個數據后,首先會對數據包進行解析,拿到目的IP地址之后,將IP與自己本地路由表中的所有路由表項進行逐位(Bit-By-Bit)比對,直到找到匹配度最長的條目,這就是最長前綴匹配機制。
1.6.路由配置
1.6.1.靜態路由配置
方式1.關聯下一跳IP的方式
????????ip route-static ip-address {mask mask-length}?nexthop-address
方式2.關聯出接口的方式
????????ip route static ip address {mask mask-length}interface-type interface-number
方式3.關聯出接口和下一跳IP方式
????????ip route-static ipaddress {mask mask-length} interface-type interface-number [ nexthop-address ]
在創建靜態路由時,可以同時指定出接口和下一跳。對于不同的出接口類型,也可以只指定出接口或只指定下跳。
對于點到點接口(如串口),必須指定出接口。
對于廣播接口(如以太網接口)和VT(Virtual-template)接口,必須指定下一跳。
1.6.2.動態路由配置
后邊補充
二、實驗
2.1.靜態路由
2.1.1.實驗拓撲
實驗環境:
通過網盤分享的文件:ENSP安裝軟件包.zip
鏈接: https://pan.baidu.com/s/139YeJnOe6yQhENsGmJ7AJA?pwd=js2y 提取碼: js2y?
--來自百度網盤超級會員v3的分享
2.1.2.實驗過程
AR4上轉發目的地址屬于30.0.0.0/24的報文,在只有直連路由的情況下沒有路由匹配。此時可以通過手動配置靜態路由,使AR4發送前往30.0.0.0/24網段的報文交給下一跳10.0.0.2/24轉發。
AR4:
The device is running!<Huawei>
<Huawei>sys
Enter system view, return user view with Ctrl+Z.
[Huawei]int
[Huawei]interface Gb
[Huawei]interface Gi
[Huawei]interface GigabitEthernet 0/0/0
[Huawei-GigabitEthernet0/0/0]ip address 10.0.0.1 24
Jun 11 2025 20:11:05-08:00 Huawei %%01IFNET/4/LINK_STATE(l)[0]:The line protocolIP on the interface GigabitEthernet0/0/0 has entered the UP state.
[Huawei-GigabitEthernet0/0/0]q
[Huawei]ip route
[Huawei]ip route-s
[Huawei]dis
[Huawei]display ip rou
[Huawei]display ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: PublicDestinations : 7 Routes : 7 Destination/Mask Proto Pre Cost Flags NextHop Interface10.0.0.0/24 Direct 0 0 D 10.0.0.1 GigabitEthernet
0/0/010.0.0.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/010.0.0.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0127.0.0.0/8 Direct 0 0 D 127.0.0.1 InLoopBack0127.0.0.1/32 Direct 0 0 D 127.0.0.1 InLoopBack0
127.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
255.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0[Huawei]ping 10.0.0.2PING 10.0.0.2: 56 data bytes, press CTRL_C to breakReply from 10.0.0.2: bytes=56 Sequence=1 ttl=255 time=100 msReply from 10.0.0.2: bytes=56 Sequence=2 ttl=255 time=40 msReply from 10.0.0.2: bytes=56 Sequence=3 ttl=255 time=30 msReply from 10.0.0.2: bytes=56 Sequence=4 ttl=255 time=20 msReply from 10.0.0.2: bytes=56 Sequence=5 ttl=255 time=20 ms--- 10.0.0.2 ping statistics ---5 packet(s) transmitted5 packet(s) received0.00% packet lossround-trip min/avg/max = 20/42/100 ms[Huawei]ip rou
[Huawei]ip route-s
[Huawei]ip route-static 30.0.0.0 24 10.0.0.2
[Huawei]ip rou
[Huawei]display ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: PublicDestinations : 8 Routes : 8 Destination/Mask Proto Pre Cost Flags NextHop Interface10.0.0.0/24 Direct 0 0 D 10.0.0.1 GigabitEthernet
0/0/010.0.0.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/010.0.0.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/030.0.0.0/24 Static 60 0 RD 10.0.0.2 GigabitEthernet
0/0/0127.0.0.0/8 Direct 0 0 D 127.0.0.1 InLoopBack0127.0.0.1/32 Direct 0 0 D 127.0.0.1 InLoopBack0
127.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
255.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0[Huawei]ip rout
[Huawei]ping 30.0.0.2PING 30.0.0.2: 56 data bytes, press CTRL_C to breakReply from 30.0.0.2: bytes=56 Sequence=1 ttl=255 time=20 msReply from 30.0.0.2: bytes=56 Sequence=2 ttl=255 time=30 msReply from 30.0.0.2: bytes=56 Sequence=3 ttl=255 time=30 msReply from 30.0.0.2: bytes=56 Sequence=4 ttl=255 time=20 msReply from 30.0.0.2: bytes=56 Sequence=5 ttl=255 time=30 ms--- 30.0.0.2 ping statistics ---5 packet(s) transmitted5 packet(s) received0.00% packet lossround-trip min/avg/max = 20/26/30 ms[Huawei]ping 30.0.0.1PING 30.0.0.1: 56 data bytes, press CTRL_C to breakReply from 30.0.0.1: bytes=56 Sequence=1 ttl=254 time=30 msReply from 30.0.0.1: bytes=56 Sequence=2 ttl=254 time=40 msReply from 30.0.0.1: bytes=56 Sequence=3 ttl=254 time=40 msReply from 30.0.0.1: bytes=56 Sequence=4 ttl=254 time=30 msReply from 30.0.0.1: bytes=56 Sequence=5 ttl=254 time=20 ms--- 30.0.0.1 ping statistics ---5 packet(s) transmitted5 packet(s) received0.00% packet lossround-trip min/avg/max = 20/32/40 ms[Huawei]
AR5:
The device is running!<Huawei>system
<Huawei>system-view
Enter system view, return user view with Ctrl+Z.
[Huawei]inte
[Huawei]interface Gi
[Huawei]interface GigabitEthernet 0/0/0
[Huawei-GigabitEthernet0/0/0]ip address 10.0.0.2 24
Jun 11 2025 20:11:35-08:00 Huawei %%01IFNET/4/LINK_STATE(l)[0]:The line protocolIP on the interface GigabitEthernet0/0/0 has entered the UP state.
[Huawei-GigabitEthernet0/0/0]q
[Huawei]interface GigabitEthernet 0/0/1
[Huawei-GigabitEthernet0/0/1]ip address 30.0.0.2 24
Jun 11 2025 20:12:01-08:00 Huawei %%01IFNET/4/LINK_STATE(l)[1]:The line protocolIP on the interface GigabitEthernet0/0/1 has entered the UP state.
[Huawei-GigabitEthernet0/0/1]q
[Huawei]Please check whether system data has been changed, and save data in timeConfiguration console time out, please press any key to log on
AR6:
The device is running!<Huawei>sys
<Huawei>system-view
Enter system view, return user view with Ctrl+Z.
[Huawei]int
[Huawei]interface G
[Huawei]interface GigabitEthernet 0/0/0
[Huawei-GigabitEthernet0/0/0]ip address 30.0.0.1 24
Jun 11 2025 20:12:53-08:00 Huawei %%01IFNET/4/LINK_STATE(l)[0]:The line protocolIP on the interface GigabitEthernet0/0/0 has entered the UP state.
[Huawei-GigabitEthernet0/0/0]q
[Huawei]Please check whether system data has been changed, and save data in timeConfiguration console time out, please press any key to log on<Huawei>sys
Enter system view, return user view with Ctrl+Z.
[Huawei]ip rou
[Huawei]ip route-s
[Huawei]ip route-static 10.0.0.0 24 30.0.0.2
[Huawei]dis
[Huawei]display ip rou
[Huawei]display ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: PublicDestinations : 8 Routes : 8 Destination/Mask Proto Pre Cost Flags NextHop Interface10.0.0.0/24 Static 60 0 RD 30.0.0.2 GigabitEthernet
0/0/030.0.0.0/24 Direct 0 0 D 30.0.0.1 GigabitEthernet
0/0/030.0.0.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/030.0.0.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0127.0.0.0/8 Direct 0 0 D 127.0.0.1 InLoopBack0127.0.0.1/32 Direct 0 0 D 127.0.0.1 InLoopBack0
127.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
255.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0[Huawei]ping 30.0.0.2PING 30.0.0.2: 56 data bytes, press CTRL_C to breakReply from 30.0.0.2: bytes=56 Sequence=1 ttl=255 time=80 msReply from 30.0.0.2: bytes=56 Sequence=2 ttl=255 time=40 msReply from 30.0.0.2: bytes=56 Sequence=3 ttl=255 time=20 msReply from 30.0.0.2: bytes=56 Sequence=4 ttl=255 time=20 msReply from 30.0.0.2: bytes=56 Sequence=5 ttl=255 time=20 ms--- 30.0.0.2 ping statistics ---5 packet(s) transmitted5 packet(s) received0.00% packet lossround-trip min/avg/max = 20/36/80 ms[Huawei]ping 10.0.0.2PING 10.0.0.2: 56 data bytes, press CTRL_C to breakReply from 10.0.0.2: bytes=56 Sequence=1 ttl=255 time=10 msReply from 10.0.0.2: bytes=56 Sequence=2 ttl=255 time=30 msReply from 10.0.0.2: bytes=56 Sequence=3 ttl=255 time=30 msReply from 10.0.0.2: bytes=56 Sequence=4 ttl=255 time=20 msReply from 10.0.0.2: bytes=56 Sequence=5 ttl=255 time=30 ms--- 10.0.0.2 ping statistics ---5 packet(s) transmitted5 packet(s) received0.00% packet lossround-trip min/avg/max = 10/24/30 ms[Huawei]ping 10.0.0.1PING 10.0.0.1: 56 data bytes, press CTRL_C to breakReply from 10.0.0.1: bytes=56 Sequence=1 ttl=254 time=50 msReply from 10.0.0.1: bytes=56 Sequence=2 ttl=254 time=30 msReply from 10.0.0.1: bytes=56 Sequence=3 ttl=254 time=30 msReply from 10.0.0.1: bytes=56 Sequence=4 ttl=254 time=30 msReply from 10.0.0.1: bytes=56 Sequence=5 ttl=254 time=30 ms--- 10.0.0.1 ping statistics ---5 packet(s) transmitted5 packet(s) received0.00% packet lossround-trip min/avg/max = 30/34/50 ms[Huawei]
驗證:
先刪除從AR6到AR4的靜態路由驗證是否ping的通,再添加靜態路由之后,看是否ping的通
[Huawei]undo ip route-static 10.0.0.0 24
[Huawei]ping 10.0.0.2PING 10.0.0.2: 56 data bytes, press CTRL_C to breakRequest time outRequest time outRequest time outRequest time outRequest time out--- 10.0.0.2 ping statistics ---5 packet(s) transmitted0 packet(s) received100.00% packet loss[Huawei]ip route-static 10.0.0.0 24 30.0.0.2
[Huawei]ping 10.0.0.1PING 10.0.0.1: 56 data bytes, press CTRL_C to breakReply from 10.0.0.1: bytes=56 Sequence=1 ttl=254 time=30 msReply from 10.0.0.1: bytes=56 Sequence=2 ttl=254 time=40 msReply from 10.0.0.1: bytes=56 Sequence=3 ttl=254 time=30 msReply from 10.0.0.1: bytes=56 Sequence=4 ttl=254 time=40 msReply from 10.0.0.1: bytes=56 Sequence=5 ttl=254 time=20 ms--- 10.0.0.1 ping statistics ---5 packet(s) transmitted5 packet(s) received0.00% packet lossround-trip min/avg/max = 20/32/40 ms[Huawei]
2.2.缺省(默認)路由
缺省路由是一種特殊的路由,當報文沒有在路由表中找到匹配(最長前綴匹配-->優先級-->開銷)的具體路由表項時才使用的路由。如果報文的目的地址不能與路由表的任何目的地址相匹配,那么該報文將選取缺省路由進行轉發。
缺省路由在路由表中的形式為0.0.0.0/0,缺省路由也被叫做默認路由。
ip route-static 0.0.0.0 下一跳
2.3.浮動路由
浮動路由指的是,當目地網段相同,但存在多條路徑可到達時(即目的地址/掩碼相同,優先級相同、下一條不同的情況下),這種情況同時配置的時候,由于優先級不同,根據路由匹配原則,優先級高的路由條目會被優先挑選進路由表中,從而優先通過優先級高的路徑進行轉發。
當優先級高的鏈路down之后,優先級較低的路由條目就開始發揮其作用。