目录
一、什么是路由?
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 protocol
IP 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: Public
Destinations : 7 Routes : 7
Destination/Mask Proto Pre Cost Flags NextHop Interface
10.0.0.0/24 Direct 0 0 D 10.0.0.1 GigabitEthernet
0/0/0
10.0.0.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
10.0.0.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
127.0.0.0/8 Direct 0 0 D 127.0.0.1 InLoopBack0
127.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.2
PING 10.0.0.2: 56 data bytes, press CTRL_C to break
Reply from 10.0.0.2: bytes=56 Sequence=1 ttl=255 time=100 ms
Reply from 10.0.0.2: bytes=56 Sequence=2 ttl=255 time=40 ms
Reply from 10.0.0.2: bytes=56 Sequence=3 ttl=255 time=30 ms
Reply from 10.0.0.2: bytes=56 Sequence=4 ttl=255 time=20 ms
Reply from 10.0.0.2: bytes=56 Sequence=5 ttl=255 time=20 ms
--- 10.0.0.2 ping statistics ---
5 packet(s) transmitted
5 packet(s) received
0.00% packet loss
round-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: Public
Destinations : 8 Routes : 8
Destination/Mask Proto Pre Cost Flags NextHop Interface
10.0.0.0/24 Direct 0 0 D 10.0.0.1 GigabitEthernet
0/0/0
10.0.0.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
10.0.0.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
30.0.0.0/24 Static 60 0 RD 10.0.0.2 GigabitEthernet
0/0/0
127.0.0.0/8 Direct 0 0 D 127.0.0.1 InLoopBack0
127.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.2
PING 30.0.0.2: 56 data bytes, press CTRL_C to break
Reply from 30.0.0.2: bytes=56 Sequence=1 ttl=255 time=20 ms
Reply from 30.0.0.2: bytes=56 Sequence=2 ttl=255 time=30 ms
Reply from 30.0.0.2: bytes=56 Sequence=3 ttl=255 time=30 ms
Reply from 30.0.0.2: bytes=56 Sequence=4 ttl=255 time=20 ms
Reply from 30.0.0.2: bytes=56 Sequence=5 ttl=255 time=30 ms
--- 30.0.0.2 ping statistics ---
5 packet(s) transmitted
5 packet(s) received
0.00% packet loss
round-trip min/avg/max = 20/26/30 ms
[Huawei]ping 30.0.0.1
PING 30.0.0.1: 56 data bytes, press CTRL_C to break
Reply from 30.0.0.1: bytes=56 Sequence=1 ttl=254 time=30 ms
Reply from 30.0.0.1: bytes=56 Sequence=2 ttl=254 time=40 ms
Reply from 30.0.0.1: bytes=56 Sequence=3 ttl=254 time=40 ms
Reply from 30.0.0.1: bytes=56 Sequence=4 ttl=254 time=30 ms
Reply from 30.0.0.1: bytes=56 Sequence=5 ttl=254 time=20 ms
--- 30.0.0.1 ping statistics ---
5 packet(s) transmitted
5 packet(s) received
0.00% packet loss
round-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 protocol
IP 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 protocol
IP 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 time
Configuration 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 protocol
IP 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 time
Configuration 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: Public
Destinations : 8 Routes : 8
Destination/Mask Proto Pre Cost Flags NextHop Interface
10.0.0.0/24 Static 60 0 RD 30.0.0.2 GigabitEthernet
0/0/0
30.0.0.0/24 Direct 0 0 D 30.0.0.1 GigabitEthernet
0/0/0
30.0.0.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
30.0.0.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
127.0.0.0/8 Direct 0 0 D 127.0.0.1 InLoopBack0
127.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.2
PING 30.0.0.2: 56 data bytes, press CTRL_C to break
Reply from 30.0.0.2: bytes=56 Sequence=1 ttl=255 time=80 ms
Reply from 30.0.0.2: bytes=56 Sequence=2 ttl=255 time=40 ms
Reply from 30.0.0.2: bytes=56 Sequence=3 ttl=255 time=20 ms
Reply from 30.0.0.2: bytes=56 Sequence=4 ttl=255 time=20 ms
Reply from 30.0.0.2: bytes=56 Sequence=5 ttl=255 time=20 ms
--- 30.0.0.2 ping statistics ---
5 packet(s) transmitted
5 packet(s) received
0.00% packet loss
round-trip min/avg/max = 20/36/80 ms
[Huawei]ping 10.0.0.2
PING 10.0.0.2: 56 data bytes, press CTRL_C to break
Reply from 10.0.0.2: bytes=56 Sequence=1 ttl=255 time=10 ms
Reply from 10.0.0.2: bytes=56 Sequence=2 ttl=255 time=30 ms
Reply from 10.0.0.2: bytes=56 Sequence=3 ttl=255 time=30 ms
Reply from 10.0.0.2: bytes=56 Sequence=4 ttl=255 time=20 ms
Reply from 10.0.0.2: bytes=56 Sequence=5 ttl=255 time=30 ms
--- 10.0.0.2 ping statistics ---
5 packet(s) transmitted
5 packet(s) received
0.00% packet loss
round-trip min/avg/max = 10/24/30 ms
[Huawei]ping 10.0.0.1
PING 10.0.0.1: 56 data bytes, press CTRL_C to break
Reply from 10.0.0.1: bytes=56 Sequence=1 ttl=254 time=50 ms
Reply from 10.0.0.1: bytes=56 Sequence=2 ttl=254 time=30 ms
Reply from 10.0.0.1: bytes=56 Sequence=3 ttl=254 time=30 ms
Reply from 10.0.0.1: bytes=56 Sequence=4 ttl=254 time=30 ms
Reply from 10.0.0.1: bytes=56 Sequence=5 ttl=254 time=30 ms
--- 10.0.0.1 ping statistics ---
5 packet(s) transmitted
5 packet(s) received
0.00% packet loss
round-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.2
PING 10.0.0.2: 56 data bytes, press CTRL_C to break
Request time out
Request time out
Request time out
Request time out
Request time out
--- 10.0.0.2 ping statistics ---
5 packet(s) transmitted
0 packet(s) received
100.00% packet loss
[Huawei]ip route-static 10.0.0.0 24 30.0.0.2
[Huawei]ping 10.0.0.1
PING 10.0.0.1: 56 data bytes, press CTRL_C to break
Reply from 10.0.0.1: bytes=56 Sequence=1 ttl=254 time=30 ms
Reply from 10.0.0.1: bytes=56 Sequence=2 ttl=254 time=40 ms
Reply from 10.0.0.1: bytes=56 Sequence=3 ttl=254 time=30 ms
Reply from 10.0.0.1: bytes=56 Sequence=4 ttl=254 time=40 ms
Reply from 10.0.0.1: bytes=56 Sequence=5 ttl=254 time=20 ms
--- 10.0.0.1 ping statistics ---
5 packet(s) transmitted
5 packet(s) received
0.00% packet loss
round-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之后,优先级较低的路由条目就开始发挥其作用。