一、OSPF路由协议(Open Shortest Path First),开放最短路径优先协议。是一种典型的链路状态的路由协议,OSFP将链路状态广播数据包LSA(Link State Advertisement)传送给在某一区域内的所有路由器。
二、OSPF路由协议特点
1、支持层次化的网络结构,便于简化大型网络
2、划分区域(area)网络,方便扩展
3、快速响应网络变化、触发更新
4、网络带宽占用少、路由收敛速度快
5、属于无类路由协议,支持VLSM和CIDR
三、OSPF的工作原理
1、双方发送HELLO报文,
2、建立邻接关系
3、发送LSA(链路状态通告)
4、LSA泛洪
5、区域中的每台路由器形成相同的LSDB(链路状态数据库)
6、根据LSDB形成无环路的拓朴图
7、每台路由器利用SPF算法,以自己为根计算出到达目标网络的最短路径,并写入路由表
四、OSPF报文类型:HELLO DBD LSR LSU LSACK
Hello报文:用于发现、验证、重新发现邻居的报文,采用周期性的组播(224.0.0.5)
在广播型的网络中,更新的周期为10S/40S
非广播多路访问的网络中,更新的周期30s/120s
DBD:数据库描述报文,在发送前要先协商主从关系(master/slave),由主方发送自己建立的链路状态数据库的格式和序列号。
LSR:向邻居发送OSPF的链路状态的请求
LSU:向请求的邻居发送关于链路状态的更新
LSACK:确认收到的链路状态更新
五、建立邻接关系的接口状态:down init 2-way exchangestart exchange loading full
down: 接口处于OSPF协议的DOWN状态,没有接收邻居的验证和发现。
init:接口初始化状态,准备发送和接收hello报文。
two-way:邻居双方通过HELLO报方发现对方准备建立双向通信。
exchangestart:双方进入交换LS的准备阶段。
exchange:双方交换链路状态报文。
loading:装载由邻居泛洪的LSA,形成完整的链路状态数据库。
full:邻居双方进入稳定的邻接关系状态,即全邻接。
六、OSPF网络类型:
根据路由器所连接的物理网络不同,OSPF将网络划分为四类型:
1、 广播多路访问型(Broadcast multiAccess),如:Ethernet、Token Ring、FDDI。采用组播的方式进行报文传输(224.0.0.5,224.0.0.6)
2、 非广播多路访问型(None Broadcast multiAccess,简称:NBMA)如:Frame Reley、X.25、SMDS。采用单播的更新。
3、 点到点型(Point to Point)如:专线连接PPP、HDLC。采用目标地址为224.0.0.5进行报文的传输
4、 点到多点型(Point to MultiPoint)
七、OSPF相关概念及术语:
1、 路由器ID:路由器在OSPF网络中的标识,由路由器中的最高物理接口IP地址来表示,如果有回环接口,那么ROUTER ID由回环接口的最高IP来表示。一般OSPF网络都会设定Loopback接口,OSPF路由器ID就会很稳定。
2、计时器: HELL0报文:是运行OSPF协议网络中,用来建立邻接关系,在广播型网络中,OSPF的HELL0报文计时器每隔10秒发送一次,保持时间40秒,即如果在40秒内没收到hello,则认为居不存在.在非广播网络中(帧中继),每隔30秒发送一次,保持时间120秒
3、 OSPF度量:路由器开销或度量,决定了到达一个目的网络最佳路径。具由最低度量的路由被认为最佳路由。OSPF使用一个基于接口的带宽的无因次的度量,用以下公式计算:
开销(cost)=100000000/(接口的带宽)
不同接口的OSPF开销如下:
接口类型    接口的带宽    OSPF开销
以太网      10000000     10
快速以太网  100000000     1
吉比特以太网 100000000    1
T1          1544000      64
E1          2048000      48
Cost的计算公式是108/带宽(Bps),然后取整,计算出通过的所有链路入口的cost之和。
其中环回接口的cost为1。
4、 链路状态:接口状态(接口的连接情况、接口参数、接口的第三层信息)
5、 链路状态协议:通过运行协议中的报文来从邻居获得整个网络的链路状态信息,建立全网的链路状态数据库(Link-State Database),生成拓朴图,再根据自己的链路状态算法算出路由表。
八、广播型的网络环境的问题:
多个路由器能过广播型的交换设备相连,形成了一个物理上的星形拓朴,而且协议所建立的逻辑结构上是个网状全互联的结构,每两个路由器之间都有处于全邻接关系,造成大量的重复的LSA,降低了链路的利用率,影响了网络的整体性能。
解决办法:选举一个指定的全邻接关系的路由器,同时选举一个备份指定路由器用于备份邻接。
DR(Designated Router):指定路由器
BDR(Backup Designated Router):备份指定路由器
OSPF要求在区域中选举一个DR,每个路由器都与之建立完全相邻关系。DR负责收集所有的链路状态信息,并发布给其他路由器。同时选举一个BDR,当DR失效时,BDR担当DR职责。
DR/BDR的选举过程:
环境:广播/非广播多路访问的网络
选举标准:priority(优先级)+RID,最高值为DR,次高为BDR
优级级:0-255,默认都为1,0不参加选举
1、先比较网络中路由器接口的优先级,高的优先,如果优先级为0,则不参与选举
2、如果优先级相同,则比较路由器的router-id,选router-id高的.
DR 与BDR的选举过程:在所有路由器中通过发送的HELLO报文中选举,所有路由器开始都称自己为DR,然后先选举BDR,BDR选举产生后,再把刚选举出的BDR推举为DR,再次选举BDR,选举完成。一旦选举成功,以后如果有路由器改了比DR/BDR还要大的优先级,仍然不会成为DR/BDR,除非重新进行选举,即重启ospf进程
选举完成后所有路由只与DR/BDR建立全邻接FULL关系。
DR/BDR选举完成后,DRother只和DR/BDR形成邻接关系,DR将组播洪泛LSU到224.0.0.5,DRother只组播LSU到所有的DRother,地址为224.0.0.6,只有DR/BDR才监听这个地址。
九、OSPF基本命令:
R1(config)#router ospf 进程号 //指定使用OSPF协议
R1(config-router)#network 接口IP 0.0.0.0(反掩码) area 区域号
R1(config-router)#router-id ip地址 //手工指定router-id
备注:如果router-id已经产生,要改变router-id则必须手工指定,且必须重启OSPF进程才生效。
重启OSPF进程:R1#clear ip ospf process
反掩码是: 1 和 0 反转的子网掩码,即用255.255.255.255减去原掩码得出反掩码,又称为通配符,0位 表示必须匹配,1位表示不必匹配。例如:
Router(config)# network 2.3.6.0 0.0.0.255 area 0
表示可以是 2.3.6.0 这个网段的任意主机
Router(config)# network 2.2.2.2 0.0.0.0 area 0
表示唯一地址为 2.2.2.2 (常用于设定接口地址****经常使用)
Router(config)# network 0.0.0.0 0.0.0.0 area 0
表示可以是任意网段的任意主机
在存在双链路的网络环境中,可以实现等价负载均衡
通过修改链路成本实现:进入接口模式
r1(config)#interface 接口编号
r1(config-if)#ip ospf cost 成本(1-65535)
配置相关的接口参数:
hello时间:在接口模式下:
r1(config)#interface 接口编号
r1(config-if)#ip ospf hello-interval 时间
**两台路由器相连的接口都要修改,保持时间默认会是hello的时间4倍
保持时间:
hello时间:在接口模式下:
r1(config)#interface 接口编号
r1(config-if)#ip ospf dead-interval 时间
查看OSPF配置命令:
查看OPSF邻居表:r1#show ip ospf neighbors
查看rouer-id,管理距离:r1#show ip protocols
查看接口的相关OSPF属性:r1#show ip ospf interface 接口
可以看到hello时间,保持时间,接口类型等等
ospf学习到的路由条目用字母O表示,即同一个区域内所学到的,管理距离值110
设置接口的优先级:
R1(config)# interface s0
R1(config-if)# ip ospf priority 100
显示接口的优先级信息
R1# show ip ospf interface s0
配置OSPF的网络类型:
R1 (config-if) # ip ospf network {broadcast | point-to-point | point-to-multicast}
OSPF的认证:
为了安全的原因,在相同OSPF区域的路由器上启用身份验证的功能,只有经过身份验证的同一区域的路由器才能互相通告路由信息。
OSPF的纯文本验证:
简单的纯文本验证允许一个区只配置一个密码(Password)同一个区中的路由器要想参与路由,他们必须配置相同的密码。这种方法的缺点是易受***。
使用下面的命令启动密码验证:
R1(config-if)#ip ospf authentication-key mypassword
R1(config-router)#area 0 authentication //”0”是area的ID
OSPF的MD5认证:
MD5(Message Digest Authentication)是采用加密验证,每个路由器上都必须配置密码和密码ID。路由器使用一种算法,基于OSPF报文、密码和密码ID产生一个 “Message Digest”,然后加到OSPF报文中。不像简单密码验证,MD5验证密码不在网络上传输。每个OSPF报文中还包含有一个序列号以保护网络不受***。
使用下面的命令来配置MD5验证:
R1(config-if)#ip ospf message-digest-key md5
R1(config-router)#area 0 authentication message-digest
十、实例:
下面网络拓朴图中配置OSPF协议,并且在R2与R3实现等负载均衡,并且手动修改R2的router-id为10.10.10.10,查看DR和BDR路由是哪个?查看Router ID?通过改变RID来改变DR和BDR。
 
三个路由器基配置
R1
enable
configure terminal
no ip domain-lookup
line console 0
no exec-timeout
logging syn
password cisco
login
hostname r1
interface l0
ip add 1.1.1.1 255.255.255.0
no sh
interface s0/0
ip address 192.1.1.1 255.255.255.0
no shutdown
router ospf 1
network 192.1.1.1 0.0.0.0 area 0
network 1.1.1.1 0.0.0.0 area 0

R2
enable
configure terminal
no ip domain-lookup
line console 0
no exec-timeout
logging syn
password cisco
login
hostname r2
interface l0
ip add 2.2.2.2 255.255.255.0
no sh
interface s0/0
ip address 192.1.1.2 255.255.255.0
no shutdown
interface s0/1
ip address 193.1.1.2 255.255.255.0
no shutdown
interface f1/0
ip address 194.1.1.2 255.255.255.0
no shutdown
router ospf 1
network 192.1.1.2 0.0.0.0 area 0
network 193.1.1.2 0.0.0.0 area 0
network 194.1.1.2 0.0.0.0 area 0
network 2.2.2.2 0.0.0.0 area 0

R3
enable
configure terminal
no ip domain-lookup
line console 0
no exec-timeout
logging syn
password cisco
login
hostname r3
interface l0
ip add 3.3.3.3 255.255.255.0
no sh
interface s0/1
ip address 193.1.1.1 255.255.255.0
no shutdown
interface f1/0
ip address 194.1.1.1 255.255.255.0
no shutdown
router ospf 1
network 193.1.1.1 0.0.0.0 area 0
network 194.1.1.1 0.0.0.0 area 0
network 3.3.3.3 0.0.0.0 area 0
 
1、查看DR和BDR路由:
r2#sh ip ospf neighbor
Neighbor ID Pri State Dead Time Address Interface
3.3.3.3 1 FULL/BDR 00:00:32 194.1.1.1 FastEthernet1/0
1.1.1.1 0 FULL/ - 00:00:39 192.1.1.1 Serial0/0
显示R2是DR,R3是BDR

2、查看R2的Router ID
r2#sho ip protocols
Routing Protocol is "ospf 1"
Outgoing update filter list for all interfaces is not set
Incoming update filter list for all interfaces is not set
Router ID 2.2.2.2
Number of areas in this router is 1. 1 normal 0 stub 0 nssa
Maximum path: 4
Routing for Networks:
2.2.2.2 0.0.0.0 area 0
192.1.1.2 0.0.0.0 area 0
193.1.1.2 0.0.0.0 area 0
194.1.1.2 0.0.0.0 area 0
Reference bandwidth unit is 100 mbps
Routing Information Sources:
Gateway Distance Last Update
3.3.3.3 110 00:14:50
1.1.1.1 110 00:14:50
Distance: (default is 110)
显示Router ID为:2.2.2.2
 
3、改变其Router ID:
r2(config)#router ospf 1
r2(config-router)#router-id 10.10.10.10
Reload or use "clear ip ospf process" command, for this to take effect
r2(config-router)#end
r2#clear ip ospf process
Reset ALL OSPF processes? [no]: yes
r2#sh ip protocols
Routing Protocol is "ospf 1"
Outgoing update filter list for all interfaces is not set
Incoming update filter list for all interfaces is not set
Router ID 10.10.10.10
Number of areas in this router is 1. 1 normal 0 stub 0 nssa
Maximum path: 4
Routing for Networks:
2.2.2.2 0.0.0.0 area 0
192.1.1.2 0.0.0.0 area 0
193.1.1.2 0.0.0.0 area 0
194.1.1.2 0.0.0.0 area 0
Reference bandwidth unit is 100 mbps
Routing Information Sources:
Gateway Distance Last Update
3.3.3.3 110 00:19:38
1.1.1.1 110 00:00:12
Distance: (default is 110)

4、查看R2路由器的DR和BDR路由信息:
r2#sh ip ospf nei
Neighbor ID Pri State Dead Time Address Interface
3.3.3.3 1 FULL/DR 00:00:32 194.1.1.1 FastEthernet1/0
1.1.1.1 0 FULL/ - 00:00:39 192.1.1.1 Serial0/0
显示R2变成BDR,R3变成DR

5、再到R3重启OSPF协议
r3#clear ip ospf pr
r3#clear ip ospf process
Reset ALL OSPF processes? [no]: yes
再查看R2信息,又变回来了。
r2#sh ip ospf nei
Neighbor ID Pri State Dead Time Address Interface
3.3.3.3 1 FULL/BDR 00:00:34 194.1.1.1 FastEthernet1/0
1.1.1.1 0 FULL/ - 00:00:33 192.1.1.1 Serial0/0
 
1、 通过改变路由器接口的Cost来实现R2与R3负载均衡
R2和R3有两条连接一个s0/1和f1/0,成本cost分别为64和1,所以在R2上到3.3.3.3路由表是194.1.1.1。
r2#sh ip ospf interface s0/1
Serial0/1 is up, line protocol is up
Internet Address 193.1.1.2/24, Area 0
Process ID 1, Router ID 2.2.2.2, Network Type POINT_TO_POINT, Cost: 64
Transmit Delay is 1 sec, State POINT_TO_POINT,
Timer intervals configured, Hello 10, Dead 40, Wait 40, Retransmit 5
oob-resync timeout 40
Hello due in 00:00:06
Supports Link-local Signaling (LLS)
Index 2/2, flood queue length 0
Next 0x0(0)/0x0(0)
Last flood scan length is 1, maximum is 1
Last flood scan time is 0 msec, maximum is 0 msec
Neighbor Count is 1, Adjacent neighbor count is 1
Adjacent with neighbor 3.3.3.3
Suppress hello for 0 neighbor(s)
r2#sh ip ospf interface f1/0
FastEthernet1/0 is up, line protocol is up
Internet Address 194.1.1.2/24, Area 0
Process ID 1, Router ID 2.2.2.2, Network Type BROADCAST, Cost: 1
Transmit Delay is 1 sec, State DR, Priority 1
Designated Router (ID) 2.2.2.2, Interface address 194.1.1.2
Backup Designated router (ID) 3.3.3.3, Interface address 194.1.1.1
Timer intervals configured, Hello 10, Dead 40, Wait 40, Retransmit 5
oob-resync timeout 40
Hello due in 00:00:03
Supports Link-local Signaling (LLS)
Index 3/3, flood queue length 0
Next 0x0(0)/0x0(0)
Last flood scan length is 1, maximum is 2
Last flood scan time is 0 msec, maximum is 4 msec
Neighbor Count is 1, Adjacent neighbor count is 1
Adjacent with neighbor 3.3.3.3 (Backup Designated Router)
Suppress hello for 0 neighbor(s)

r2#sh ip route
1.0.0.0/32 is subnetted, 1 subnets
O 1.1.1.1 [110/65] via 192.1.1.1, 00:03:18, Serial0/0
2.0.0.0/24 is subnetted, 1 subnets
C 2.2.2.0 is directly connected, Loopback0
3.0.0.0/32 is subnetted, 1 subnets
O 3.3.3.3 [110/2] via 194.1.1.1, 00:03:18, FastEthernet1/0
C 193.1.1.0/24 is directly connected, Serial0/1
C 192.1.1.0/24 is directly connected, Serial0/0
C 194.1.1.0/24 is directly connected, FastEthernet1/0

改变R2的s0/1接口cost为1
r2(config)#interface s0/1
r2(config-if)#ip ospf cost 1
r2#sh ip ospf interface s0/1
Serial0/1 is up, line protocol is up
Internet Address 193.1.1.2/24, Area 0
Process ID 1, Router ID 2.2.2.2, Network Type POINT_TO_POINT, Cost: 1
Transmit Delay is 1 sec, State POINT_TO_POINT,
Timer intervals configured, Hello 10, Dead 40, Wait 40, Retransmit 5
oob-resync timeout 40
Hello due in 00:00:01
Supports Link-local Signaling (LLS)
Index 2/2, flood queue length 0
Next 0x0(0)/0x0(0)
Last flood scan length is 1, maximum is 1
Last flood scan time is 0 msec, maximum is 0 msec
Neighbor Count is 1, Adjacent neighbor count is 1
Adjacent with neighbor 3.3.3.3
Suppress hello for 0 neighbor(s)
修改成功,再查看R2的路由表,到达3.3.3.3有两条路由条目了。
r2#sh ip route
1.0.0.0/32 is subnetted, 1 subnets
O 1.1.1.1 [110/65] via 192.1.1.1, 00:00:00, Serial0/0
2.0.0.0/24 is subnetted, 1 subnets
C 2.2.2.0 is directly connected, Loopback0
3.0.0.0/32 is subnetted, 1 subnets
O 3.3.3.3 [110/2] via 194.1.1.1, 00:00:00, FastEthernet1/0
[110/2] via 193.1.1.1, 00:00:00, Serial0/1
C 193.1.1.0/24 is directly connected, Serial0/1
C 192.1.1.0/24 is directly connected, Serial0/0
C 194.1.1.0/24 is directly connected, FastEthernet1/0
十一、处理OSPF故障常用命令
OSPF是链路状态协议,维护3个数据库:相邻数据库、拓扑结构数据库、路由表。
OSPF相关的show命令:
Show running-config
Show ip route
Show ip route ospf ;仅显示OSPF路由
Show ip ospf process-id ;显示与特定进程ID相关的信息
Show ip ospf ;显示OSPF相关信息
Show ip ospf border-routers ;显示边界路由器
Show ip ospf database ;显示OSPF的归纳数据库
Show ip ospf interface ;显示指定接口上的OSPF信息
Show ip ospf neighbor ;显示OSPF相邻信息
Show ip ospf request-list ;显示链路状态请求列表
Show ip ospf summary-address ;显示归纳路由的再发布信息
Show ip ospf virtual-links ;显示虚拟链路信息
Show ip interface ;显示接口的IP设置
OSPF相关的debug命令:
Debug ip ospf adj ;
Debug ip ospf events
Debug ip ospf flood
Debug ip ospf lsa-generation
Debug ip ospf packet
Debug ip ospf retransmission
Debug ip ospf spf
Debug ip ospf tree
常见的OSPF故障:OSPF的每个area不超过100台路由器,整个网络不超过700台路由器;通配符掩码配置不当;