OSPF(Open Shortest Path First),开放式最短路径优先。OSPF作为链路状态的协议,具有收敛快、路由无环、扩展性好等优点。链路状态算法路由协议互相通告的是链路状态信息,每台路由器都将自己的链路状态信息(包含接口的IP地址和子网掩码、网络类型、该链路的开销等)发送给其它路由器,并在网络中泛洪,当每台路由器收集到的网络内所有链路状态信息后,就能拥有整个网络的拓扑情况,根据网络拓扑情况运行SPF算法,得出所有网段的最短路径。
OSPF 支持区域的划分,区域是从逻辑上划分不同的组,每个组用区域号(Area ID)来标识。一个网段(链路)只能属于一个区域,或者说每个运行OSPF的接口必须指明属于哪一个区域。区域0为骨干区域,骨干区域负责在非骨干区域之间发布区域间的路由信息。在一个OSPF区域中有且只有一个骨干区域。
该拓扑图分为3个办公区,每个区放置一台路由器。3台路由器相互直连,为了使整个公司网络互相通信,在所有路由器上部署OSPF协议,且所有路由器都属于骨干区域。
[Huawei]sys R1
[R1]int g 0/0/2
[R1-GigabitEthernet0/0/2]ip add 172.16.1.254 24.
[R1-GigabitEthernet0/0/2]int g 0/0/0
[R1-GigabitEthernet0/0/0]ip add 172.16.10.1 24
[R1-GigabitEthernet0/0/0]int g 0/0/1
[R1-GigabitEthernet0/0/1]ip add 172.16.20.2 24
[R1]ospf 1 //部署单区域OSPF网络,1代表进程号,默认为1
[R1-ospf-1]area 0 //创建区域ID,本次为单区域配置使用骨干区域,即区域0
[R1-ospf-1-area-0.0.0.0]network 172.16.10.0 0.0.0.255
[R1-ospf-1-area-0.0.0.0]network 172.16.20.0 0.0.0.255
[R1-ospf-1-area-0.0.0.0]network 172.16.1.0 0.0.0.255 //network命令用来指定OSPF协议的接口和所属的区域,精确匹配所通告的网段
**R2/R3配置方法相同,不赘述。
[R1]display ospf interface //查看接口通告是否正确
OSPF Process 1 with Router ID 172.16.1.254 //本地OSPF进程使用的Router-ID为172.16.1.254
Interfaces
Area: 0.0.0.0 (MPLS TE not enabled)
IP Address Type State Cost Pri DR BDR
172.16.1.254 Broadcast DR 1 1 172.16.1.254 0.0.0.0 //有3个接口加入了OSPF进程,“Type为默认的广播网络类型
172.16.10.1 Broadcast DR 1 1 172.16.10.1 0.0.0.0 //当前接口为DR状态,即表示3个接口在它们所在的网段中
172.16.20.2 Broadcast DR 1 1 172.16.20.2 0.0.0.0 // 被选举为DR
[R1]display ospf peer //查看OSPF邻居状态
OSPF Process 1 with Router ID 172.16.1.254
Neighbors
Area 0.0.0.0 interface 172.16.10.1(GigabitEthernet0/0/0)'s neighbors
Router ID: 172.16.2.254 Address: 172.16.10.2 //可以看到R2邻居路由器的标识,和邻居OSPF接口的IP地址
State: Full Mode:Nbr is Master Priority: 1 //目前与该路由器的OSPF邻居状态,Priority与该邻居OSPF接口的DR优先级相等
DR: 172.16.10.1 BDR: 172.16.10.2 MTU: 0
Dead timer due in 30 sec
Retrans timer interval: 5
Neighbor is up for 00:05:33
Authentication Sequence: [ 0 ]
Neighbors
Area 0.0.0.0 interface 172.16.20.2(GigabitEthernet0/0/1)'s neighbors
Router ID: 172.16.3.254 Address: 172.16.20.3
State: Full Mode:Nbr is Master Priority: 1
DR: 172.16.20.2 BDR: 172.16.20.3 MTU: 0
Dead timer due in 38 sec
Retrans timer interval: 5
Neighbor is up for 00:02:36
Authentication Sequence: [ 0 ]
[R1]display ip routing-table protocol ospf //查看R1上的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:路由信息通 )
Destination/Mask Proto Pre Cost Flags NextHop Interface //(Proto:路由信息通过OSPF协议获取) (Pre:路由优先级)
//(Cast:开销值) (NextHop:下一跳地址)(Interface:出接口)
172.16.2.0/24 OSPF 10 2 D 172.16.10.2 GigabitEthernet0/0/0
172.16.3.0/24 OSPF 10 2 D 172.16.20.3 GigabitEthernet0/0/1
172.16.30.0/24 OSPF 10 2 D 172.16.10.2 GigabitEthernet0/0/0
OSPF 10 2 D 172.16.20.3 GigabitEthernet0/0/1
OSPF routing table status :
Destinations : 0 Routes : 0
R1的路由表中已经拥有了去往网络中所有其它网段的路由条目。
PC>ping 172.16.2.1 //PC1 ping 测试PC2/PC3间的连通性
Ping 172.16.2.1: 32 data bytes, Press Ctrl_C to break
From 172.16.2.1: bytes=32 seq=1 ttl=126 time=16 ms
From 172.16.2.1: bytes=32 seq=2 ttl=126 time=15 ms
From 172.16.2.1: bytes=32 seq=3 ttl=126 time=16 ms
From 172.16.2.1: bytes=32 seq=4 ttl=126 time=31 ms
From 172.16.2.1: bytes=32 seq=5 ttl=126 time=15 ms
— 172.16.2.1 ping statistics —
5 packet(s) transmitted
5 packet(s) received
0.00% packet loss
round-trip min/avg/max = 15/18/31 ms
PC>ping 172.16.3.1
Ping 172.16.3.1: 32 data bytes, Press Ctrl_C to break
From 172.16.3.1: bytes=32 seq=1 ttl=126 time=16 ms
From 172.16.3.1: bytes=32 seq=2 ttl=126 time=16 ms
From 172.16.3.1: bytes=32 seq=3 ttl=126 time=15 ms
From 172.16.3.1: bytes=32 seq=4 ttl=126 time=31 ms
From 172.16.3.1: bytes=32 seq=5 ttl=126 time=32 ms
— 172.16.3.1 ping statistics —
5 packet(s) transmitted
5 packet(s) received
0.00% packet loss
round-trip min/avg/max = 15/22/32 ms