开放式最短路径优先 OSPF(Open Shortest Path First)是IETF 组织开发的一个基于链路状态的内部网关协议(Interior Gateway Protocol,IGP)。目前针对 IPv4 协议使用的是 OSPFVersion 2(RFC2328);OSPF作为基于链路状态的协议,具有以下优点:
由于 OSPF 具有以上优势,使得 OSPF 作为优秀的内部网关协议被快速接收并广泛使用。
本实验将通过配置单区域 OSPF,帮助理解 OSPF 基本配置与原理。
AR1、AR2、AR3都是各自网络的网关设备,现在需要通过OSPF动态路由协议,来实现这些网络之间的互联互通。
安装有eNSP模拟器的PC一台,要求PC能联网。
根据表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的规划完成配置。
# 进入系统视图,命名设备,并禁用信息中心
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: Public
Destinations : 11 Routes : 11
Destination/Mask Proto Pre Cost Flags NextHop Interface
10.0.1.1/32 Direct 0 0 D 127.0.0.1 LoopBack0
10.0.12.0/24 Direct 0 0 D 10.0.12.1 GigabitEthernet 0/0/0
10.0.12.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet 0/0/0
10.0.12.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet 0/0/0
10.0.13.0/24 Direct 0 0 D 10.0.13.1 GigabitEthernet 0/0/2
10.0.13.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet 0/0/2
10.0.13.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet 0/0/2
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
此时设备上仅存在直连路由。
创建 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
可用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
使用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
执行【display ospf peer】命令可查看OSPF中各区域邻居的信息。包括邻居所属的区域、邻居Router ID、邻居状态、DR 和 BDR 路由器等信息。
[AR1]display ospf peer
OSPF Process 1 with Router ID 10.0.1.1
Neighbors
Area 0.0.0.0 interface 10.0.12.1(GigabitEthernet0/0/0)'s neighbors
Router ID: 10.0.1.2 Address: 10.0.12.2
State: Full Mode:Nbr is Master Priority: 1
DR: 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 neighbors
Router ID: 10.0.1.3 Address: 10.0.13.3
State: Full Mode:Nbr is Master Priority: 1
DR: 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 ]
执行【display ip routing-table protocol ospf】命令可查看 IP 路由表中由 OSPF 学习到的路由。
display ip routing-table protocol ospf
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Public routing table : OSPF
Destinations : 3 Routes : 4
OSPF routing table status :
Destinations : 3 Routes : 4
Destination/Mask Proto Pre Cost Flags NextHop Interface
10.0.1.2/32 OSPF 10 1 D 10.0.12.2 GigabitEthernet 0/0/0
10.0.1.3/32 OSPF 10 1 D 10.0.13.3 GigabitEthernet 0/0/2
10.0.23.0/24 OSPF 10 2 D 10.0.12.2 GigabitEthernet 0/0/0
OSPF 10 2 D 10.0.13.3 GigabitEthernet 0/0/2
OSPF routing table status :
Destinations : 0 Routes : 0
执行【display ospf peer brief】命令可以查看OSPF邻居状态。
(1)查看AR1路由器的OSPF邻居状态
[AR1]display ospf peer brief
OSPF Process 1 with Router ID 10.0.1.1
Peer 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 brief
OSPF Process 1 with Router ID 10.0.1.2
Peer 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邻居状态
display ospf peer brief
OSPF Process 1 with Router ID 10.0.1.3
Peer 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
----------------------------------------------------------------------------
假设 AR1 为所有网络的出口,则需要在 AR1 上向 OSPF 宣告默认路由,将默认路由通告到普通OSPF区域。宣告默认路由的命令为default-route-advertise,其后通常紧跟always参数。
如果没有配置 always参数,本机路由表中必须有激活的非本 OSPF 默认路由时才向其他路由器发布默认路由。本例中,本地路由表中没有默认路由,故需要增加 always 参数。
# 进入ospf进程视图
[AR1]ospf
# 宣告默认路由
[AR1-ospf-1]default-route-advertise always
[AR2]display ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: Public
Destinations : 15 Routes : 16
Destination/Mask Proto Pre Cost Flags NextHop Interface
0.0.0.0/0 O_ASE 150 1 D 10.0.12.1 GigabitEthernet 0/0/0
……此处省略其它输出……
可以看到AR2已学习到默认路由。
display ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: Public
Destinations : 15 Routes : 16
Destination/Mask Proto Pre Cost Flags NextHop Interface
0.0.0.0/0 O_ASE 150 1 D 10.0.13.1 GigabitEthernet 0/0/2
……此处省略其它输出……
可以看到AR3已学习到默认路由。
现要求AR1的LoopBack0 接口通过 AR1-> AR3->AR2 的路径访问 AR2的 LoopBack0接口。
display ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: Public
Destinations : 14 Routes : 15
Destination/Mask Proto Pre Cost Flags NextHop Interface
10.0.1.1/32 Direct 0 0 D 127.0.0.1 LoopBack0
10.0.1.2/32 OSPF 10 1 D 10.0.12.2 GigabitEthernet 0/0/0
10.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即可满足需求。
因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
display ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: Public
Destinations : 14 Routes : 14
Destination/Mask Proto Pre Cost Flags NextHop Interface
10.0.1.1/32 Direct 0 0 D 127.0.0.1 LoopBack0
10.0.1.2/32 OSPF 10 2 D 10.0.13.3 GigabitEthernet 0/0/2
……此处省略其它输出……
此时可以看到 AR1 访问 AR2 的 LoopBack0 接囗的下一跳为 AR3 的 GE0/0/2 接口,路由开销为2。
tracert -a 10.0.1.1 10.0.1.2
traceroute 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
通过 ping 功能检查设备各接口之间的连通性。
以AR1为例,根据表1,除AR1的各接口外,测试所有其它设备的各个接口IP地址的连通性。
ping 10.0.12.2
PING 10.0.12.2: 56 data bytes, press CTRL_C to break
Reply from 10.0.12.2: bytes=56 Sequence=1 ttl=255 time=20 ms
Reply from 10.0.12.2: bytes=56 Sequence=2 ttl=255 time=10 ms
ping 10.0.23.2
PING 10.0.23.2: 56 data bytes, press CTRL_C to break
Reply from 10.0.23.2: bytes=56 Sequence=1 ttl=255 time=20 ms
Reply from 10.0.23.2: bytes=56 Sequence=2 ttl=255 time=20 ms
ping 10.0.1.2
PING 10.0.1.2: 56 data bytes, press CTRL_C to break
Reply from 10.0.1.2: bytes=56 Sequence=1 ttl=255 time=30 ms
Reply from 10.0.1.2: bytes=56 Sequence=2 ttl=255 time=10 ms
ping 10.0.23.3
PING 10.0.23.3: 56 data bytes, press CTRL_C to break
Reply from 10.0.23.3: bytes=56 Sequence=1 ttl=255 time=30 ms
Reply from 10.0.23.3: bytes=56 Sequence=2 ttl=255 time=10 ms
ping 10.0.13.3
PING 10.0.13.3: 56 data bytes, press CTRL_C to break
Reply from 10.0.13.3: bytes=56 Sequence=1 ttl=255 time=30 ms
Reply from 10.0.13.3: bytes=56 Sequence=2 ttl=255 time=30 ms
ping 10.0.1.3
PING 10.0.1.3: 56 data bytes, press CTRL_C to break
Reply from 10.0.1.3: bytes=56 Sequence=1 ttl=255 time=20 ms
Reply from 10.0.1.3: bytes=56 Sequence=2 ttl=255 time=20 ms
关闭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
以查看AR2的路由表为例。
display ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: Public
Destinations : 12 Routes : 12
Destination/Mask Proto Pre Cost Flags NextHop Interface
0.0.0.0/0 O_ASE 150 1 D 10.0.23.3 GigabitEthernet 0/0/1
10.0.1.1/32 OSPF 10 2 D 10.0.23.3 GigabitEthernet 0/0/1
10.0.1.2/32 Direct 0 0 D 127.0.0.1 LoopBack0
10.0.1.3/32 OSPF 10 1 D 10.0.23.3 GigabitEthernet 0/0/1
10.0.13.0/24 OSPF 10 2 D 10.0.23.3 GigabitEthernet 0/0/1
……此处省略后续输出……
从路由表可以看出,所有从AR2前往AR1的路由均需通过AR3。
再次测试连通性,除AR1的GE 0/0/0外,所有接口依然均可ping通(这里不再演示)。
save
The current configuration will be written to the device.
Are you sure to continue? (y/n)[n]:y
It will take several minutes to save configuration file, please wait.......
Configuration file had been saved successfully
Note: The configuration file will take effect after being activated
# 查看配置
display saved-configuration
[V200R003C00]
#
sysname AR1
#
snmp-agent local-engineid 800007DB03000000000000
snmp-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 default
authorization-scheme default
accounting-scheme default
domain default
domain default_admin
local-user admin password cipher %$%$K8m.Nt84DZ}e#<0`8bmE3Uw}%$%$
local-user admin service-type http
#
firewall zone Local
priority 15
#
interface GigabitEthernet0/0/0
shutdown
ip address 10.0.12.1 255.255.255.0
ospf cost 10
#
interface GigabitEthernet0/0/1
#
interface GigabitEthernet0/0/2
ip address 10.0.13.1 255.255.255.0
#
interface NULL0
#
interface LoopBack0
ip address 10.0.1.1 255.255.255.255
#
ospf 1 router-id 10.0.1.1
default-route-advertise always
area 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 0
authentication-mode password
user-interface vty 0 4
user-interface vty 16 20
#
wlan ac
#
return
save
The current configuration will be written to the device.
Are you sure to continue? (y/n)[n]:y
It will take several minutes to save configuration file, please wait.......
Configuration file had been saved successfully
Note: The configuration file will take effect after being activated
# 查看配置
display saved-configuration
[V200R003C00]
#
sysname AR2
#
snmp-agent local-engineid 800007DB03000000000000
snmp-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 default
authorization-scheme default
accounting-scheme default
domain default
domain default_admin
local-user admin password cipher %$%$K8m.Nt84DZ}e#<0`8bmE3Uw}%$%$
local-user admin service-type http
#
firewall zone Local
priority 15
#
interface GigabitEthernet0/0/0
ip address 10.0.12.2 255.255.255.0
#
interface GigabitEthernet0/0/1
ip address 10.0.23.2 255.255.255.0
#
interface GigabitEthernet0/0/2
#
interface NULL0
#
interface LoopBack0
ip 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 0
authentication-mode password
user-interface vty 0 4
user-interface vty 16 20
#
wlan ac
#
return
save
The current configuration will be written to the device.
Are you sure to continue? (y/n)[n]:y
It will take several minutes to save configuration file, please wait.......
Configuration file had been saved successfully
Note: The configuration file will take effect after being activated
# 查看配置
display saved-configuration
[V200R003C00]
#
sysname AR3
#
snmp-agent local-engineid 800007DB03000000000000
snmp-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 default
authorization-scheme default
accounting-scheme default
domain default
domain default_admin
local-user admin password cipher %$%$K8m.Nt84DZ}e#<0`8bmE3Uw}%$%$
local-user admin service-type http
#
firewall zone Local
priority 15
#
interface GigabitEthernet0/0/0
#
interface GigabitEthernet0/0/1
ip address 10.0.23.3 255.255.255.0
#
interface GigabitEthernet0/0/2
ip address 10.0.13.3 255.255.255.0
#
interface NULL0
#
interface LoopBack0
ip 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 0
authentication-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之和。