OSPF 路由协议基础实验
开放式最短路径优先 OSPF(Open Shortest Path First)是IETF 组织开发的一个基于链路状态的内部网关协议(Interior Gateway Protocol,IGP)。目前针对 IPv4 协议使用的是 OSPFVersion 2(RFC2328);OSPF作为基于链路状态的协议,具有以下优点:
- OSPF 采用组播形式收发报文,这样可以减少对其它不运行 OSPF 路由器的影响。
- OSPF 支持无类型域间选路(CIDR)
- OSPF 支持对等价路由进行负载分担。
- OSPF 支持报文认证。
由于 OSPF 具有以上优势,使得 OSPF 作为优秀的内部网关协议被快速接收并广泛使用。
本实验将通过配置单区域 OSPF,帮助理解 OSPF 基本配置与原理。
一、实验目的
- 掌握 OSPF 的基本配置命令
- 掌握如何查看 OSPF 的运行状态
- 掌握如何通过 Cost 控制 OSPF 的选路
- 掌握 OSPF 发布默认路由的方法
- 掌握 OSPF 认证配置方法
二、实验拓扑结构
AR1、AR2、AR3都是各自网络的网关设备,现在需要通过OSPF动态路由协议,来实现这些网络之间的互联互通。
三、实验环境
安装有eNSP模拟器的PC一台,要求PC能联网。
四、实验配置思路
- 创建设备上的 OSPF 进程并使能接口上的 OSPF功能
- 配置 OSPF 认证
- 通过 OSPF 发布默认路由
- 通过修改 Cost 值控制 OSPF 选路
五、实验过程
5.1 设备基础配置
根据表1,完成路由器的命名、物理接口和 LoopBack接口的IP地址配置。
路由器 | 接口 | IP地址/网络前缀长度 |
---|---|---|
AR1 | GE 0/0/0 | 10.0.12.1/24 |
GE 0/0/2 | 10.0.13.1/24 | |
LoopBack 0 | 10.0.1.1/32 | |
AR2 | GE 0/0/0 | 10.0.12.2/24 |
GE 0/0/1 | 10.0.23.2/24 | |
LoopBack 0 | 10.0.1.2/32 | |
AR3 | GE 0/0/1 | 10.0.23.3/24 |
GE 0/0/2 | 10.0.13.3/24 | |
LoopBack 0 | 10.0.1.3/32 |
以下以AR1为例,其它两个设备请参照AR1的配置和表1的规划完成配置。
# 进入系统视图,命名设备,并禁用信息中心
<Huawei>system-view
[Huawei]sysname AR1
[AR1]undo info-center enable # 配置GE 0/0/0接口IP地址
[AR1]interface GigabitEthernet 0/0/0
[AR1-GigabitEthernet0/0/0]ip address 10.0.12.1 24
[AR1-GigabitEthernet0/0/0]quit# 配置GE 0/0/2接口IP地址
[AR1]interface GigabitEthernet 0/0/2
[AR1-GigabitEthernet0/0/2]ip address 10.0.13.1 24
[AR1-GigabitEthernet0/0/2]quit# 配置LoopBack 0接口IP地址
[AR1]interface LoopBack 0
[AR1-LoopBack0]ip address 10.0.1.1 32
[AR1-LoopBack0]quit# 查看路由表
[AR1]display ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: PublicDestinations : 11 Routes : 11 Destination/Mask Proto Pre Cost Flags NextHop Interface10.0.1.1/32 Direct 0 0 D 127.0.0.1 LoopBack010.0.12.0/24 Direct 0 0 D 10.0.12.1 GigabitEthernet 0/0/010.0.12.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet 0/0/010.0.12.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet 0/0/010.0.13.0/24 Direct 0 0 D 10.0.13.1 GigabitEthernet 0/0/210.0.13.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet 0/0/210.0.13.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet 0/0/2127.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
此时设备上仅存在直连路由。
5.2 OSPF的基本配置
5.2.1 创建 OSPF 进程
创建 OSPF 进程是配置与 OSPF 协议有关参数的首要步骤。
OSPF 支持多进程,在同一台设备上可以运行多个不同的 OSPF 进程,它们之间互不影响,彼此独立。
不同 OSPF 进程之间的路由交互相当于不同路由协议之间的路由交互。
创建OSPF进程的命令格式如下:
ospf 进程号 router-id router-id
其中进程号为1-65535之间的整数,默认为1
router-id为唯一标识一台路由器的标识符,通常用LoopBack接口地址表示。
(1)在AR1上创建OSPF进程,指定进程号为1。
# 创建 OSPF 进程
[AR1]ospf 1 router-id 10.0.1.1
(2)在AR2上创建OSPF进程,指定进程号为1。
# 创建 OSPF 进程
[AR2]ospf 1 router-id 10.0.1.2
(3)在AR3上创建OSPF进程,指定进程号为1。
# 创建 OSPF 进程
[AR3]ospf 1 router-id 10.0.1.3
5.2.2 创建 OSPF 区域并使能相应的接口
可用area 命令用来创建OSPF 区域,并进入 OSPF区域视图。在OSPF进程视图下,创建OSPF区域的命令格式如下:
area 区域id
其中区域可以用一个整数或者采用IP地址的格式来表示。
(1)在AR1上创建OSPF区域,指定区域id为0,注意是在OSPF进程视图下。
# 创建 OSPF 区域并使能相应的接口
[AR1-ospf-1]area 0
(2)在AR2上创建OSPF区域,指定区域id为0,注意是在OSPF进程视图下。
# 创建 OSPF 区域并使能相应的接口
[AR2-ospf-1]area 0
(3)在AR3上创建OSPF区域,指定区域id为0,注意是在OSPF进程视图下。
# 创建 OSPF 区域并使能相应的接口
[AR3-ospf-1]area 0
5.2.3 声明区域网络
使用network命令来声明区域网络,命令格式如下:
network network-address wildcard-mask
其中network-address是接口的IP地址或其所在网络地址。
wildcard-mask是反掩码,例如掩码255.255.255.0的反掩码为0.0.0.255。
(1)在AR1上声明区域网络,注意是在OSPF区域网络视图下。
# 声明AR1与AR2之间的网络
[AR1-ospf-1-area-0.0.0.0]network 10.0.12.0 0.0.0.255# 声明AR1与AR3之间的网络
[AR1-ospf-1-area-0.0.0.0]network 10.0.13.0 0.0.0.255# 声明LoopBack0接口地址网络
[AR1-ospf-1-area-0.0.0.0]network 10.0.1.1 0.0.0.0
[AR1-ospf-1-area-0.0.0.0]return
(2)在AR2上声明区域网络,注意是在OSPF区域网络视图下。
# 声明AR2与AR1之间的网络
[AR2-ospf-1-area-0.0.0.0]network 10.0.12.0 0.0.0.255# 声明AR2与AR3之间的网络
[AR2-ospf-1-area-0.0.0.0]network 10.0.23.0 0.0.0.255# 声明LoopBack0接口地址网络
[AR2-ospf-1-area-0.0.0.0]network 10.0.1.2 0.0.0.0
[AR2-ospf-1-area-0.0.0.0]return
(3)在AR3上声明区域网络,注意是在OSPF区域网络视图下。
# 声明AR3与AR1之间的网络
[AR3-ospf-1-area-0.0.0.0]network 10.0.13.0 0.0.0.255# 声明AR3与AR2之间的网络
[AR3-ospf-1-area-0.0.0.0]network 10.0.23.0 0.0.0.255# 声明LoopBack0接口地址网络
[AR3-ospf-1-area-0.0.0.0]network 10.0.1.3 0.0.0.0
[AR3-ospf-1-area-0.0.0.0]return
5.3 查看OSPF状态
5.3.1 查看 OSPF 邻居
执行【display ospf peer】命令可查看OSPF中各区域邻居的信息。包括邻居所属的区域、邻居Router ID、邻居状态、DR 和 BDR 路由器等信息。
[AR1]display ospf peer OSPF Process 1 with Router ID 10.0.1.1Neighbors Area 0.0.0.0 interface 10.0.12.1(GigabitEthernet0/0/0)'s neighborsRouter ID: 10.0.1.2 Address: 10.0.12.2 State: Full Mode:Nbr is Master Priority: 1DR: 10.0.12.1 BDR: 10.0.12.2 MTU: 0 Dead timer due in 30 sec Retrans timer interval: 5 Neighbor is up for 00:15:16 Authentication Sequence: [ 0 ] Neighbors Area 0.0.0.0 interface 10.0.13.1(GigabitEthernet0/0/2)'s neighborsRouter ID: 10.0.1.3 Address: 10.0.13.3 State: Full Mode:Nbr is Master Priority: 1DR: 10.0.13.1 BDR: 10.0.13.3 MTU: 0 Dead timer due in 37 sec Retrans timer interval: 5 Neighbor is up for 00:13:23 Authentication Sequence: [ 0 ]
5.3.2 查看OSPF 学习到的路由
执行【display ip routing-table protocol ospf】命令可查看 IP 路由表中由 OSPF 学习到的路由。
<AR1>display ip routing-table protocol ospf
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Public routing table : OSPFDestinations : 3 Routes : 4 OSPF routing table status : <Active>Destinations : 3 Routes : 4Destination/Mask Proto Pre Cost Flags NextHop Interface10.0.1.2/32 OSPF 10 1 D 10.0.12.2 GigabitEthernet 0/0/010.0.1.3/32 OSPF 10 1 D 10.0.13.3 GigabitEthernet 0/0/210.0.23.0/24 OSPF 10 2 D 10.0.12.2 GigabitEthernet 0/0/0OSPF 10 2 D 10.0.13.3 GigabitEthernet 0/0/2OSPF routing table status : <Inactive>Destinations : 0 Routes : 0
5.3.3 查看邻居状态
执行【display ospf peer brief】命令可以查看OSPF邻居状态。
(1)查看AR1路由器的OSPF邻居状态
[AR1]display ospf peer brief OSPF Process 1 with Router ID 10.0.1.1Peer Statistic Information----------------------------------------------------------------------------Area Id Interface Neighbor id State 0.0.0.0 GigabitEthernet0/0/0 10.0.1.2 Full 0.0.0.0 GigabitEthernet0/0/2 10.0.1.3 Full ----------------------------------------------------------------------------
(2)查看AR2路由器的OSPF邻居状态
[AR2]display ospf peer briefOSPF Process 1 with Router ID 10.0.1.2Peer Statistic Information----------------------------------------------------------------------------Area Id Interface Neighbor id State 0.0.0.0 GigabitEthernet0/0/0 10.0.1.1 Full 0.0.0.0 GigabitEthernet0/0/1 10.0.1.3 Full ----------------------------------------------------------------------------
(3)查看AR3路由器的OSPF邻居状态
<AR3>display ospf peer brief OSPF Process 1 with Router ID 10.0.1.3Peer Statistic Information----------------------------------------------------------------------------Area Id Interface Neighbor id State 0.0.0.0 GigabitEthernet0/0/1 10.0.1.2 Full 0.0.0.0 GigabitEthernet0/0/2 10.0.1.1 Full ----------------------------------------------------------------------------
5.4 宣告默认路由
假设 AR1 为所有网络的出口,则需要在 AR1 上向 OSPF 宣告默认路由,将默认路由通告到普通OSPF区域。宣告默认路由的命令为default-route-advertise,其后通常紧跟always参数。
如果没有配置 always参数,本机路由表中必须有激活的非本 OSPF 默认路由时才向其他路由器发布默认路由。本例中,本地路由表中没有默认路由,故需要增加 always 参数。
5.4.1 在AR1上宣告默认路由
# 进入ospf进程视图
[AR1]ospf# 宣告默认路由
[AR1-ospf-1]default-route-advertise always
5.4.2 在AR2上查看路由表
[AR2]display ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: PublicDestinations : 15 Routes : 16 Destination/Mask Proto Pre Cost Flags NextHop Interface0.0.0.0/0 O_ASE 150 1 D 10.0.12.1 GigabitEthernet 0/0/0
……此处省略其它输出……
可以看到AR2已学习到默认路由。
5.4.3 在AR3上查看路由表
<AR3>display ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: PublicDestinations : 15 Routes : 16 Destination/Mask Proto Pre Cost Flags NextHop Interface0.0.0.0/0 O_ASE 150 1 D 10.0.13.1 GigabitEthernet 0/0/2
……此处省略其它输出……
可以看到AR3已学习到默认路由。
5.5 修改路由开销
现要求AR1的LoopBack0 接口通过 AR1-> AR3->AR2 的路径访问 AR2的 LoopBack0接口。
5.5.1 查看AR1的路由表
<AR1>display ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: PublicDestinations : 14 Routes : 15 Destination/Mask Proto Pre Cost Flags NextHop Interface10.0.1.1/32 Direct 0 0 D 127.0.0.1 LoopBack010.0.1.2/32 OSPF 10 1 D 10.0.12.2 GigabitEthernet 0/0/010.0.1.3/32 OSPF 10 1 D 10.0.13.3 GigabitEthernet 0/0/2……此处省略其它输出……
从 AR1 的路由表可知,AR1 通过 AR1->AR2的路径访问 AR2的 LoopBack0接口的路由开销(Cost)为1。
同样,通过查看AR3的路由表可知,AR3 通过 AR3->AR2的路径访问 AR2的 LoopBack0接口的路由开销也为1。
故从 AR1->AR3->AR2的路由开销为 2,也就是只要使 AR1->AR2的路由开销大于2即可满足需求。
5.5.2 修改路由开销
因AR1 通过 AR1->AR2的路径访问 AR2的 LoopBack0接口的出口为GE 0/0/0,故需要进入该接口的配置视图进行配置。
# 进入接口配置视图
[AR1]interface GigabitEthernet 0/0/0# 修改路由开销
[AR1-GigabitEthernet0/0/0]ospf cost 10
[AR1-GigabitEthernet0/0/0]quit
5.2.3 再次查看AR1的路由表
<AR1>display ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: PublicDestinations : 14 Routes : 14 Destination/Mask Proto Pre Cost Flags NextHop Interface10.0.1.1/32 Direct 0 0 D 127.0.0.1 LoopBack010.0.1.2/32 OSPF 10 2 D 10.0.13.3 GigabitEthernet 0/0/2
……此处省略其它输出……
此时可以看到 AR1 访问 AR2 的 LoopBack0 接囗的下一跳为 AR3 的 GE0/0/2 接口,路由开销为2。
5.2.4 通过 Tracert 命令验证
<AR1>tracert -a 10.0.1.1 10.0.1.2traceroute to 10.0.1.2(10.0.1.2), max hops: 30 ,packet length: 40,press CTRL_C to break 1 10.0.13.3 30 ms 20 ms 20 ms 2 10.0.23.2 20 ms 20 ms 20 ms
六、结果验证
6.1 测试连通性
通过 ping 功能检查设备各接口之间的连通性。
以AR1为例,根据表1,除AR1的各接口外,测试所有其它设备的各个接口IP地址的连通性。
<AR1>ping 10.0.12.2PING 10.0.12.2: 56 data bytes, press CTRL_C to breakReply from 10.0.12.2: bytes=56 Sequence=1 ttl=255 time=20 msReply from 10.0.12.2: bytes=56 Sequence=2 ttl=255 time=10 ms
<AR1>ping 10.0.23.2PING 10.0.23.2: 56 data bytes, press CTRL_C to breakReply from 10.0.23.2: bytes=56 Sequence=1 ttl=255 time=20 msReply from 10.0.23.2: bytes=56 Sequence=2 ttl=255 time=20 ms
<AR1>ping 10.0.1.2PING 10.0.1.2: 56 data bytes, press CTRL_C to breakReply from 10.0.1.2: bytes=56 Sequence=1 ttl=255 time=30 msReply from 10.0.1.2: bytes=56 Sequence=2 ttl=255 time=10 ms
<AR1>ping 10.0.23.3PING 10.0.23.3: 56 data bytes, press CTRL_C to breakReply from 10.0.23.3: bytes=56 Sequence=1 ttl=255 time=30 msReply from 10.0.23.3: bytes=56 Sequence=2 ttl=255 time=10 ms
<AR1>ping 10.0.13.3PING 10.0.13.3: 56 data bytes, press CTRL_C to breakReply from 10.0.13.3: bytes=56 Sequence=1 ttl=255 time=30 msReply from 10.0.13.3: bytes=56 Sequence=2 ttl=255 time=30 ms
<AR1>ping 10.0.1.3PING 10.0.1.3: 56 data bytes, press CTRL_C to breakReply from 10.0.1.3: bytes=56 Sequence=1 ttl=255 time=20 msReply from 10.0.1.3: bytes=56 Sequence=2 ttl=255 time=20 ms
6.2 模拟故障
6.2.1 关闭接口模拟故障
关闭AR1的GE 0/0/0接口模拟链路故障,对比5.4完成后的路由表,说明其中的变化。
# 进入GE 0/0/0接口视图
[AR1]interface GigabitEthernet 0/0/0# 关闭接口
[AR1-GigabitEthernet0/0/0]shutdown
[AR1-GigabitEthernet0/0/0]quit
6.2.2 查看路由表
以查看AR2的路由表为例。
<AR2>display ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: PublicDestinations : 12 Routes : 12 Destination/Mask Proto Pre Cost Flags NextHop Interface0.0.0.0/0 O_ASE 150 1 D 10.0.23.3 GigabitEthernet 0/0/110.0.1.1/32 OSPF 10 2 D 10.0.23.3 GigabitEthernet 0/0/110.0.1.2/32 Direct 0 0 D 127.0.0.1 LoopBack010.0.1.3/32 OSPF 10 1 D 10.0.23.3 GigabitEthernet 0/0/110.0.13.0/24 OSPF 10 2 D 10.0.23.3 GigabitEthernet 0/0/1
……此处省略后续输出……
从路由表可以看出,所有从AR2前往AR1的路由均需通过AR3。
6.2.3 测试连通性
再次测试连通性,除AR1的GE 0/0/0外,所有接口依然均可ping通(这里不再演示)。
七、保存并查看配置
7.1 AR1的配置
<AR1>saveThe current configuration will be written to the device. Are you sure to continue? (y/n)[n]:yIt will take several minutes to save configuration file, please wait.......Configuration file had been saved successfullyNote: The configuration file will take effect after being activated# 查看配置
<AR1>display saved-configuration
[V200R003C00]
#sysname AR1
#snmp-agent local-engineid 800007DB03000000000000snmp-agent
#clock timezone China-Standard-Time minus 08:00:00
#
portal local-server load portalpage.zip
#drop illegal-mac alarm
#undo info-center enable
#set cpu-usage threshold 80 restore 75
#
aaa authentication-scheme defaultauthorization-scheme defaultaccounting-scheme defaultdomain default domain default_admin local-user admin password cipher %$%$K8m.Nt84DZ}e#<0`8bmE3Uw}%$%$local-user admin service-type http
#
firewall zone Localpriority 15
#
interface GigabitEthernet0/0/0shutdownip address 10.0.12.1 255.255.255.0 ospf cost 10
#
interface GigabitEthernet0/0/1
#
interface GigabitEthernet0/0/2ip address 10.0.13.1 255.255.255.0
#
interface NULL0
#
interface LoopBack0ip address 10.0.1.1 255.255.255.255
#
ospf 1 router-id 10.0.1.1 default-route-advertise alwaysarea 0.0.0.0 network 10.0.1.1 0.0.0.0 network 10.0.12.0 0.0.0.255 network 10.0.13.0 0.0.0.255
#
user-interface con 0authentication-mode password
user-interface vty 0 4
user-interface vty 16 20
#
wlan ac
#
return
7.2 AR2的配置
<AR2>saveThe current configuration will be written to the device. Are you sure to continue? (y/n)[n]:yIt will take several minutes to save configuration file, please wait.......Configuration file had been saved successfullyNote: The configuration file will take effect after being activated# 查看配置
<AR2>display saved-configuration
[V200R003C00]
#sysname AR2
#snmp-agent local-engineid 800007DB03000000000000snmp-agent
#clock timezone China-Standard-Time minus 08:00:00
#
portal local-server load portalpage.zip
#drop illegal-mac alarm
#undo info-center enable
#set cpu-usage threshold 80 restore 75
#
aaa authentication-scheme defaultauthorization-scheme defaultaccounting-scheme defaultdomain default domain default_admin local-user admin password cipher %$%$K8m.Nt84DZ}e#<0`8bmE3Uw}%$%$local-user admin service-type http
#
firewall zone Localpriority 15
#
interface GigabitEthernet0/0/0ip address 10.0.12.2 255.255.255.0
#
interface GigabitEthernet0/0/1ip address 10.0.23.2 255.255.255.0
#
interface GigabitEthernet0/0/2
#
interface NULL0
#
interface LoopBack0ip address 10.0.1.2 255.255.255.255
#
ospf 1 router-id 10.0.1.2 area 0.0.0.0 network 10.0.1.2 0.0.0.0 network 10.0.12.0 0.0.0.255 network 10.0.23.0 0.0.0.255
#
user-interface con 0authentication-mode password
user-interface vty 0 4
user-interface vty 16 20
#
wlan ac
#
return
7.3 AR3的配置
<AR3>saveThe current configuration will be written to the device. Are you sure to continue? (y/n)[n]:yIt will take several minutes to save configuration file, please wait.......Configuration file had been saved successfullyNote: The configuration file will take effect after being activated# 查看配置
<AR3>display saved-configuration
[V200R003C00]
#sysname AR3
#snmp-agent local-engineid 800007DB03000000000000snmp-agent
#clock timezone China-Standard-Time minus 08:00:00
#
portal local-server load portalpage.zip
#drop illegal-mac alarm
#undo info-center enable
#set cpu-usage threshold 80 restore 75
#
aaa authentication-scheme defaultauthorization-scheme defaultaccounting-scheme defaultdomain default domain default_admin local-user admin password cipher %$%$K8m.Nt84DZ}e#<0`8bmE3Uw}%$%$local-user admin service-type http
#
firewall zone Localpriority 15
#
interface GigabitEthernet0/0/0
#
interface GigabitEthernet0/0/1ip address 10.0.23.3 255.255.255.0
#
interface GigabitEthernet0/0/2ip address 10.0.13.3 255.255.255.0
#
interface NULL0
#
interface LoopBack0ip address 10.0.1.3 255.255.255.255
#
ospf 1 router-id 10.0.1.3 area 0.0.0.0 network 10.0.1.3 0.0.0.0 network 10.0.13.0 0.0.0.255 network 10.0.23.0 0.0.0.255
#
user-interface con 0authentication-mode password
user-interface vty 0 4
user-interface vty 16 20
#
wlan ac
#
return
思考题:在5.5节中,AR2 回复 AR1 的 ICMP 报文的路径是什么样的?试着解释一下原因。
答案:AR2 回复AR1 的路径是:R2->R1。OSPF计算到达路由时,依据的是接口出方向的 Cost之和。