OSPF单区域配置-点到点链路上的配置:

 

一、配置IP地址

Router>en
Router#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
Router(config)#no ip domain-lookup
Router(config)#hostname r1
r1(config)#line console 0
r1(config-line)#no exec-timeout
r1(config-line)#logging synchronous
r1(config-line)#exit
r1(config)#int lo0
r1(config-if)#ip add 1.1.1.1 255.255.255.0
r1(config-if)#no shut
r1(config-if)#int s1/0
r1(config-if)#ip add 192.168.1.1 255.255.255.0
r1(config-if)#no shut
r1(config-if)#end
r1#show ip int brief
Interface                  IP-Address      OK? Method Status                Protocol
FastEthernet0/0            unassigned      YES unset  administratively down down   
Serial1/0                  192.168.1.1     YES manual up                    up     
Serial1/1                  unassigned      YES unset  administratively down down   
Serial1/2                  unassigned      YES unset  administratively down down   
Serial1/3                  unassigned      YES unset  administratively down down   
Loopback0                  1.1.1.1         YES manual up                    up     
r1#

Router>en
Router#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
Router(config)#no ip domain-lookup
Router(config)#hostnam r2
r2(config)#int lo0
r2(config-if)#ip add 2.2.2.2 255.255.255.0
r2(config-if)#no shut
r2(config-line)#no exec-timeout
r2(config-line)#logging syn
r2(config-line)#logging synchronous
r2(config-line)#exit
r2(config)#int s1/0
r2(config-if)#ip add 192.168.1.2 255.255.255.0
r2(config-if)#no shut
r2(config-if)#int s1/1
r2(config-if)#
00:02:13: %LINK-3-UPDOWN: Interface Serial1/0, changed state to up
00:02:14: %LINEPROTO-5-UPDOWN: Line protocol on Interface Serial1/0, changed state to up
r2(config-if)#ip add 192.168.2.1 255.255.255.0
r2(config-if)#no shut
r2(config-if)#end
r2#show
00:02:22: %SYS-5-CONFIG_I: Configured from console by console
r2#show i
00:02:23: %LINK-3-UPDOWN: Interface Serial1/1, changed state to up
00:02:24: %LINEPROTO-5-UPDOWN: Line protocol on Interface Serial1/1, changed state to up
r2#show ip int brief
Interface                  IP-Address      OK? Method Status                Protocol
FastEthernet0/0            unassigned      YES unset  administratively down down   
Serial1/0                  192.168.1.2     YES manual up                    up     
Serial1/1                  192.168.2.1     YES manual up                    up     
Serial1/2                  unassigned      YES unset  administratively down down   
Serial1/3                  unassigned      YES unset  administratively down down   
Loopback0                  2.2.2.2         YES manual up                    up     
r2#

 

r3#show ip int brief
Interface                  IP-Address      OK? Method Status                Protocol
FastEthernet0/0            unassigned      YES unset  administratively down down   
Serial1/0                  unassigned      YES unset  administratively down down   
Serial1/1                  192.168.2.2     YES manual up                    up     
Serial1/2                  unassigned      YES unset  administratively down down   
Serial1/3                  unassigned      YES unset  administratively down down   
Loopback0                  3.3.3.3         YES manual up                    up     
r3#

 

二、配置OSPF进程1区域0

r1(config)#router ospf 1
r1(config-router)#router

r1(config-router)#router-id 1.1.1.1

r1(config-router)#network 192.168.1.0 0.0.0.255 area 0
r1(config-router)#network 1.1.1.1 0.0.0.0 area 0
r1(config-router)#end

r2#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
r2(config)#router ospf 1
r2(config-router)#router
r2(config-router)#router-id 2.2.2.2
r2(config-router)#network 192.168.1.0 0.0.0.255 area 0
r2(config-router)#network 192.168.2.0
00:06:36: %OSPF-5-ADJCHG: Process 1, Nbr 1.1.1.1 on Serial1/0 from LOADING to FULL, Loading Done
r2(config-router)#network 192.168.2.0 0.0.0.255 area 0
r2(config-router)#network 2.2.2.2 0.0.0.0 area 0
r2(config-router)#end
r2#

r3#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
r3(config)#router ospf 1
r3(config-router)#router
r3(config-router)#router-id 3.3.3.3
r3(config-router)#network 192.168.2.0 0.0.0.255 area 0
r3(config-router)#network 3.3.3.3 0.
00:04:44: %OSPF-5-ADJCHG: Process 1, Nbr 2.2.2.2 on Serial1/1 from LOADING to FULL, Loading Done
r3(config-router)#network 3.3.3.3 0.0.0.0 area 0
r3(config-router)#end

【技术要点】
(1)OSPF 路由进程 ID 的范围必须在 1-65535 之间,而且只有本地含义,不同路由器
的路由进程 ID 可以不同。如果要想启动 OSPF 路由进程,至少确保有一个接口是 up 的;
(2)区域 ID 是在0-4294967295 内的十进制数,也可以是 IP地址的格式 A.B.C.D。当
网络区域 ID为 0 或0.0.0.0 时称为主干区域;
(3)在高版本的 IOS 中通告 OSPF 网络的时候,网络号的后面可以跟网络掩码,也可以
跟反掩码,都是可以的;
(4)确定 Router ID 遵循如下顺序: 
① 最优先的是在 OSPF 进程中用命令“router-id”指定了路由器 ID;
② 如果没有在 OSPF 进程中指定路由器 ID,那么选择 IP 地址最大的环回接口的 IP 地
址为Router ID;
③ 如果没有环回接口,就选择最大的活动的物理接口的 IP地址为 Router ID。
建议用命令“router-id”来指定路由器 ID,这样可控性比较好。

三、调试

r2#show ip route
Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP
       D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
       N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
       E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
       i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
       ia - IS-IS inter area, * - candidate default, U - per-user static route
       o - ODR, P - periodic downloaded static route

Gateway of last resort is not set

     1.0.0.0/32 is subnetted, 1 subnets
O       1.1.1.1 [110/65] via 192.168.1.1, 00:08:05, Serial1/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/65] via 192.168.2.2, 00:08:05, Serial1/1
C    192.168.1.0/24 is directly connected, Serial1/0
C    192.168.2.0/24 is directly connected, Serial1/1r1#show ip ospf ?
  <1-65535>            Process ID number
  border-routers       Border and Boundary Router Information
  database             Database summary
  flood-list           Link state flood list
  interface            Interface information
  mpls                 MPLS related information
  neighbor             Neighbor list
  request-list         Link state request list
  retransmission-list  Link state retransmission list
  summary-address      Summary-address redistribution Information
  virtual-links        Virtual link information
  |                    Output modifiers
  <cr>

【说明】
① 环回接口 OSPF 路由条目的掩码长度都是 32 位,这是环回接口的特性,不管我们通告了
多少 位,解决的办法是在环回接口下修改网络类型为“Point-to-Point” ,操作如下:
R2(config)#interface loopback 0
R2(config-if)#ip ospf network point-to-point
这样收到的路由条目的掩码长度和通告的一致。

我们在R2上通告环回接口24位:

r2#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
r2(config)#router ospf 1
r2(config-router)#no network 2.2.2.2 0.0.0.0 area 0
r2(config-router)#network
00:24:10: OSPF: Interface Loopback0 going Down
00:24:10: OSPF: 2.2.2.2 address 2.2.2.2 on Loopback0 is dead, state DOWN
00:24:10: OSPF: Build router LSA for area 0, router ID 2.2.2.2, seq 0x8000000A
r2(config-router)#network 2.2.2.0 0.0.0.255 area 0
r2(config-router)#

 修改之后,我们在R3上看路由表:

r3#show ip route
Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP
       D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
       N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
       E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
       i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
       ia - IS-IS inter area, * - candidate default, U - per-user static route
       o - ODR, P - periodic downloaded static route

Gateway of last resort is not set

     1.0.0.0/32 is subnetted, 1 subnets
O       1.1.1.1 [110/129] via 192.168.2.1, 00:00:02, Serial1/1
     2.0.0.0/24 is subnetted, 1 subnets
O       2.2.2.0 [110/65] via 192.168.2.1, 00:00:02, Serial1/1  //这里已经变了!!!!!!!
     3.0.0.0/24 is subnetted, 1 subnets
C       3.3.3.0 is directly connected, Loopback0
O    192.168.1.0/24 [110/128] via 192.168.2.1, 00:00:02, Serial1/1
C    192.168.2.0/24 is directly connected, Serial1/1

cost值的计算:

r2#show ip route         
Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP
       D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
       N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
       E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
       i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
       ia - IS-IS inter area, * - candidate default, U - per-user static route
       o - ODR, P - periodic downloaded static route

Gateway of last resort is not set

     1.0.0.0/32 is subnetted, 1 subnets
O       1.1.1.1 [110/65] via 192.168.1.1, 00:09:12, Serial1/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/65] via 192.168.2.2, 00:09:12, Serial1/1

C    192.168.1.0/24 is directly connected, Serial1/0
C    192.168.2.0/24 is directly connected, Serial1/1

路由条目3.3.3.3的COST值为65,怎么计算呢?

cost 的计算公式为 10
8
/带宽(bps) ,然后取整,而且是所有链路入口的 cost 之和,环
回接口的 cost 为 1,路由条目“3.3.3.3”到路由器 R3 经过R2的s1/1出接口到达路由器 R3 的
loopback0,所以计算如下:

s1/1的COST值为:

r2#show ip ospf interface
Loopback0 is up, line protocol is up
  Internet Address 2.2.2.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
  Index 2/2, flood queue length 0
  Next 0x0(0)/0x0(0)
  Last flood scan length is 0, maximum is 0
  Last flood scan time is 0 msec, maximum is 0 msec
  Neighbor Count is 0, Adjacent neighbor count is 0
  Suppress hello for 0 neighbor(s)
Serial1/0 is up, line protocol is up
  Internet Address 192.168.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
    Hello due in 00:00:02
  Index 1/1, 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 1.1.1.1
  Suppress hello for 0 neighbor(s)
Serial1/1 is up, line protocol is up
  Internet Address 192.168.2.1/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
    Hello due in 00:00:00
  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 0 msec
  Neighbor Count is 1, Adjacent neighbor count is 1
    Adjacent with neighbor 3.3.3.3
  Suppress hello for 0 neighbor(s)

所以其计算结果为:

64+1=65
也可以直接通过命令“ip ospf cost”设置接口的 cost
值,并且它是优先计算的 cost 值的。

r2#conf t
r2(config)#int s1/1
r2(conf-if)#ip ospf cost 80

然后我们再看,路由条目3.3.3.3的COST值

r2#show ip route
Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP
       D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
       N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
       E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
       i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
       ia - IS-IS inter area, * - candidate default, U - per-user static route
       o - ODR, P - periodic downloaded static route

Gateway of last resort is not set

     1.0.0.0/32 is subnetted, 1 subnets
O       1.1.1.1 [110/65] via 192.168.1.1, 00:03:46, Serial1/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/81] via 192.168.2.2, 00:03:46, Serial1/1   //已经变成81了!!!!
C    192.168.1.0/24 is directly connected, Serial1/0
C    192.168.2.0/24 is directly connected, Serial1/1
r2#

r2#show 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   //支持等价路径数目

我们可以在OSPF配置模式下更改:

r2#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
r2(config)#router ospf ?
  <1-65535>  Process ID

r2(config)#router ospf 1
r2(config-router)#ma
r2(config-router)#maximum-paths ?
  <1-6>  Number of paths   //支持最大等价路径条数为6

r2(config-router)#end          

 

r2#show ip ospf interface s1/1
Serial1/1 is up, line protocol is up
  Internet Address 192.168.2.1/24, Area 0 

//该接口的地址和运行的OSPF区域 
  Process ID 1, Router ID 2.2.2.2, Network Type POINT_TO_POINT, Cost: 80

//进程ID,路由器ID,网络类型,接口Cost值 
  Transmit Delay is 1 sec, State POINT_TO_POINT,

//接口的延迟和状态 
  Timer intervals configured, Hello 10, Dead 40, Wait 40, Retransmit 5
    Hello due in 00:00:00

//显示几个计时器的值 
  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 0 msec
  Neighbor Count is 1, Adjacent neighbor count is 1 

//邻居的个数以及已建立邻接关系的邻居的个数
    Adjacent with neighbor 3.3.3.3

//已经建立邻接关系的邻居路由器ID 
  Suppress hello for 0 neighbor(s)

//没有进行Hello抑制
r2#show ip ospf neighbor

Neighbor ID     Pri   State           Dead Time   Address         Interface
1.1.1.1           1   FULL/  -        00:00:34    192.168.1.1     Serial1/0
3.3.3.3           1   FULL/  -        00:00:35    192.168.2.2     Serial1/1
r2#

以上输出表明路由器 R2 有两个邻居,它们的路由器 ID 分别为 1.1.1.1 和 3.3.3.3,其
它参数解释如下:
① Pri:邻居路由器接口的优先级;
② State:当前邻居路由器接口的状态;
③ Dead Time:清除邻居关系前等待的最长时间;
④ Address:邻居接口的地址;
⑤ Interface:自己和邻居路由器相连接口;
⑥ “-” : 表示点到点的链路上 OSPF 不进行DR选举。

 
【技术要点】
OSPF 邻居关系不能建立的常见原因:
① hello 间隔和 dead 间隔不同;
同一链路上的 hello 包间隔和 dead 间隔必须相同才能建立邻接关系。默认情况下,
hello 包发送间隔如表 6-1 所示。
 
表 6-1 OSPF hello 间隔和 dead 间隔
网络类型 Hello 间隔(秒) Dead 间隔(秒) 

广播多路访问 10 40
非广播多路访问 30 120
点到点 10 40
点到多点 30 120

 
默认时 Dead 间隔是 Hello 间隔的四倍。 可以在接口下通过 “ip ospf hello-interval”
和“ip ospf dead-interval”命令调整。

② 区域号码不一致;
③ 特殊区域(如stub,nssa等)区域类型不匹配;
④ 认证类型或密码不一致;
⑤ 路由器 ID 相同;
⑥ Hello 包被 ACL deny;
⑦ 链路上的MTU 不匹配;
⑧ 接口下OSPF网络类型不匹配。

r2#show ip ospf database

            OSPF Router with ID (2.2.2.2) (Process ID 1)

                Router Link States (Area 0)

Link ID         ADV Router      Age         Seq#       Checksum Link count
1.1.1.1         1.1.1.1         756         0x80000008 0x00287A 3
2.2.2.2         2.2.2.2         1015        0x8000000D 0x00EA09 5
3.3.3.3         3.3.3.3         1035        0x80000004 0x00305B 3
r2#


以上输出是 R2 的区域0 的拓扑结构数据库的信息,标题行的解释如下:
① Link ID:是指 Link State ID,代表整个路由器,而不是某个链路;
② ADV Router:是指通告链路状态信息的路由器 ID;
③ Age:老化时间;
④ Seq#:序列号;
⑤ Checksum:校验和;
⑥ Link count:通告路由器在本区域内的链路数目。

r2#debug ip ospf adj  监测OSPF建立邻接的过程
OSPF adjacency events debugging is on
r2#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
r2(config)#int s1/0
r2(config-if)#shut  我们关掉接口在开启接口进行测试.......
r2(config-if)#
01:01:30: OSPF: Interface Serial1/0 going Down   接口DOWN掉了
01:01:30: OSPF: 2.2.2.2 address 192.168.1.2 on Serial1/0 is dead, state DOWN
01:01:30: OSPF: 1.1.1.1 address 192.168.1.1 on Serial1/0 is dead, state DOWN
01:01:30: %OSPF-5-ADJCHG: Process 1, Nbr 1.1.1.1 on Serial1/0 from FULL to DOWN, Neighbor Down: Interface down or detached   邻居关系有FULL转变到DOWN。。。。。。
r2(config-if)#
01:01:30: OSPF: Build router LSA for area 0, router ID 2.2.2.2, seq 0x8000000E
r2(config-if)#
01:01:32: %LINK-5-CHANGED: Interface Serial1/0, changed state to administratively down
01:01:33: %LINEPROTO-5-UPDOWN: Line protocol on Interface Serial1/0, changed state to down
r2(config-if)#no shut   再开启接口........
r2(config-if)#
01:01:44: %LINK-3-UPDOWN: Interface Serial1/0, changed state to up
r2(config-if)#
01:01:44: OSPF: Interface Serial1/0 going Up
01:01:45: OSPF: Build router LSA for area 0, router ID 2.2.2.2, seq 0x8000000F
r2(config-if)#
01:01:45: %LINEPROTO-5-UPDOWN: Line protocol on Interface Serial1/0, changed state to up
r2(config-if)#
01:01:48: OSPF: 2 Way Communication to 1.1.1.1 on Serial1/0, state 2WAY
01:01:48: OSPF: Send DBD to 1.1.1.1 on Serial1/0 seq 0x26D2 opt 0x42 flag 0x7 len 32
01:01:48: OSPF: Rcv DBD from 1.1.1.1 on Serial1/0 seq 0x118 opt 0x42 flag 0x7 len 32  mtu 1500 state EXSTART
01:01:48: OSPF: First DBD and we are not SLAVE
01:01:48: OSPF: Rcv DBD from 1.1.1.1 on Serial1/0 seq 0x26D2 opt 0x42 flag 0x2 len 92  mtu 1500 state EXSTART
01:01:48: OSPF: NBR Negotiation Done. We are the MASTER
01:01:48: OSPF: Send DBD to 1.1.1.1 on Serial1/0 seq 0x26D3 opt 0x42 flag 0x3 len 92
01:01:48: OSPF: Database request to 1.1.1.1
01:01:48: OSPF: sent LS REQ packet to 192.168.1.1, length 12
01:01:48: OSPF: Rcv DBD from 1.1.1.1 on Serial1/0 seq 0x26D3 opt 0x42 flag 0x0 len 32  mtu 1500 state EXCHANGE
01:01:48: OSPF: Send DBD to 1.1.1.1 on Serial1/0 seq 0x26D4 opt 0x42 flag 0x1 len 32
01:01:48: OSPF: Rcv DBD from 1.1.1.1 on Serial1/0 seq 0x26D4 opt 0x42 flag 0x0 len 32  mtu 1500 state EXCHANGE
01:01:48
r2(config-if)#: OSPF: Exchange Done with 1.1.1.1 on Serial1/0
01:01:48: OSPF: Synchronized with 1.1.1.1 on Serial1/0, state FULL
01:01:48: %OSPF-5-ADJCHG: Process 1, Nbr 1.1.1.1 on Serial1/0 from LOADING to FULL, Loading Done
r2(config-if)#
01:01:51: OSPF: Build router LSA for area 0, router ID 2.2.2.2, seq 0x80000010
r2(config-if)#end
r2#
01:02:02: %SYS-5-CONFIG_I: Configured from console by console
r2#

上面的过程基本上是这样的:由于R2上的接口S1/0接口关闭,所以R1和R2这两个邻居关系都为DOWN的状态;当激活接口S1/0时候,两个路由器要发送HELLO给对发(进行打招呼),这时候两个路由器已经有DOWN的状态转换到初始化(INIT)的状态;当R1和R2都从对方的HELLO信息中看到了自己的Router-id时候,转化为双向状态(2-way);接着路由器进入预启动(Exstart)状态 , 预启动状态,ospf路由器建立主从关系(看谁的routerid大)然后协商一个 确定数据库描述报文的序列号(因为 ip 是不可靠的传输采用确认+超时重传就可以)准备传送。谁当了 master序列号就用谁生成的那个序列号,在这里应该是 Router 2 的 0x26D4;当序列号确定之后,就进入到预交换状态(Exchange) 开始预交换数据,master 先发送 lsdb 报文,此报文只是一个index(如同一本书的目录)不包含实际的路由数据,slave 也发送报文,看谁的序列号高,序列高的数据新,相邻路由器可以根据数据库描述数据包的序列号与自身数据库的数据作比较, 若发现接收到的数据比数据库内的数据序列号大, 则相邻路由器会针对序列号较大的数据发出请求, 并用请求得到的数据来更新其链路状态数据库。 
装入状态(loading),路由器从 dd 报文中看出那个是自己需要的路由数据,则发送 lsr报文,请求发送数据,对端发送 lsu报文,此报文包含所需的全部数据。   收到lsu报文后发送确认,完成充满状态(full)。