OSPF协议中Cost值的计算

2018年7月15日23:25:27

OSPF度量的计算

 

默认是108次方除带宽就是OSPF的度量值。 带宽是以bit为单位,以100cost值就是:100000000/100000000=1;10cost值就是:100000000/10000000=10。如结果出现小数,小数点后面记得应该是直接舍掉不用五入。 如果带宽是1000cost值也是1,最小值就是1。那么OSPF就会计算路径时就会出现象距离知量算法一样的结果,比如到目的网络有两条路径,一条要经过两个路由器但是链路带宽都是1000兆,那么两个1000兆的累计COST将是2。一条路径只有一跳到达目的,但是带宽只有100兆,那么它的COST只有1。结果OSPF会选度量值小的为最佳路径,最后导致选择100兆的链路。

R1(config)#router ospf 1

R1(config-router)#auto-costreference-bandwidth <1-4294967>

OSPF进程中用该命令可以修改度量值计算基数,避免这种问题。

注意这里用的单位是Mbits

 R1(config-if)#ip ospf cost <1-65535>

接口下用该命令可以直接改动COST值来影响路径计算结果。

 

Cisco路由器的缺省接口代价:

OSPF协议中Cost值的计算

 

下面用华为的模拟器来模拟实验验证OSPF的度量计算:

OSPF协议中Cost值的计算

 

 

R1的配置:

ospf 1

area 0.0.0.0

network 12.0.0.0 0.0.0.255

network 13.0.0.0 0.0.0.255

network 1.1.1.1 0.0.0.0

 

R2的配置:

ospf 1

area 0.0.0.0

network 12.0.0.0 0.0.0.255

network 24.0.0.0 0.0.0.255

network 2.2.2.2 0.0.0.0

 

R3的配置:

ospf 1

area 0.0.0.0

network 13.0.0.0 0.0.0.255

network 34.0.0.0 0.0.0.255

network 3.3.3.3 0.0.0.0

 

R4的配置:

ospf 1

area 0.0.0.0

network 24.0.0.0 0.0.0.255

network 34.0.0.0 0.0.0.255

network 4.4.4.4 0.0.0.0

 

查看R1的路由表可知到达R4由两条路径,如下:

 

[R1-ospf-1]dis ip routing-table

Route Flags: R - relay, D - download to fib

------------------------------------------------------------------------------

Routing Tables: Public

         Destinations : 12       Routes : 13      

 

Destination/Mask    Proto   Pre  Cost      Flags NextHop         Interface

 

        1.1.1.1/32  Direct              127.0.0.1       LoopBack0

        2.2.2.2/32  OSPF    10             12.0.0.2        Ethernet0/0/0

        3.3.3.3/32  OSPF    10              13.0.0.3        Ethernet0/0/1

        4.4.4.4/32  OSPF    10             12.0.0.2        Ethernet0/0/0

                             OSPF    10             13.0.0.3        Ethernet0/0/1

       12.0.0.0/24  Direct              12.0.0.1        Ethernet0/0/0

       12.0.0.1/32  Direct              127.0.0.1       Ethernet0/0/0

       13.0.0.0/24  Direct              13.0.0.1        Ethernet0/0/1

       13.0.0.1/32  Direct              127.0.0.1       Ethernet0/0/1

       24.0.0.0/24  OSPF    10             12.0.0.2        Ethernet0/0/0

       34.0.0.0/24  OSPF    10             13.0.0.3        Ethernet0/0/1

      127.0.0.0/8   Direct              127.0.0.1       InLoopBack0

      127.0.0.1/32  Direct              127.0.0.1       InLoopBack0

 

R4收到R1的路由条目也是两各路径上的,且cost值为2

 

[R4-ospf-1]dis ip routing-table

Route Flags: R - relay, D - download to fib

------------------------------------------------------------------------------

Routing Tables: Public

         Destinations : 12       Routes : 13      

 

Destination/Mask    Proto   Pre  Cost      Flags NextHop         Interface

 

        1.1.1.1/32  OSPF    10             24.0.0.2        Ethernet0/0/1

                             OSPF    10             34.0.0.3        Ethernet0/0/0

        2.2.2.2/32  OSPF    10             24.0.0.2        Ethernet0/0/1

        3.3.3.3/32  OSPF    10             34.0.0.3        Ethernet0/0/0

        4.4.4.4/32  Direct               127.0.0.1       LoopBack0

       12.0.0.0/24  OSPF    10             24.0.0.2        Ethernet0/0/1

       13.0.0.0/24  OSPF    10             34.0.0.3        Ethernet0/0/0

       24.0.0.0/24  Direct              24.0.0.4        Ethernet0/0/1

       24.0.0.4/32  Direct              127.0.0.1       Ethernet0/0/1

       34.0.0.0/24  Direct              34.0.0.4        Ethernet0/0/0

       34.0.0.4/32  Direct              127.0.0.1       Ethernet0/0/0

      127.0.0.0/8   Direct              127.0.0.1       InLoopBack0

      127.0.0.1/32  Direct              127.0.0.1       InLoopBack0

 

 

如果只想R4R2的路径到R1,而不想走R3的路径,则需要增加经过R3的接口cost值(因为改变带宽可能影响到其他路由协议,而且只能增加R3的接口cost值,不能减小R2的接口cost值,因为R2的接口cost值已经最小。)命令如下:

 

[R3]int e0/0/0

[R3-Ethernet0/0/0]ospf cost 50

OSPFcost值增加50因为cost值是在路由条目流入路由器时加到路由条目上的,所以,R4学到R1的路由条目的cost值要在R3E0/0/0上更改),再次查看R4的路由表。

 

[R4]dis 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

 

        1.1.1.1/32  OSPF    10              24.0.0.2        Ethernet0/0/1

        2.2.2.2/32  OSPF    10             24.0.0.2        Ethernet0/0/1

        3.3.3.3/32  OSPF    10             34.0.0.3        Ethernet0/0/0

        4.4.4.4/32  Direct              127.0.0.1       LoopBack0

       12.0.0.0/24  OSPF    10             24.0.0.2        Ethernet0/0/1

       13.0.0.0/24  OSPF    10             24.0.0.2        Ethernet0/0/1

       24.0.0.0/24  Direct              24.0.0.4        Ethernet0/0/1

       24.0.0.4/32  Direct              127.0.0.1       Ethernet0/0/1

       34.0.0.0/24  Direct              34.0.0.4        Ethernet0/0/0

       34.0.0.4/32  Direct               127.0.0.1       Ethernet0/0/0

      127.0.0.0/8   Direct              127.0.0.1       InLoopBack0

      127.0.0.1/32  Direct              127.0.0.1       InLoopBack0

 

这次,R4R1的路由条目只经过R2,不经过R3,因从R3学到的路由条目costR2上的大,不能加入路由表。如果将R4的E0/0/1接口Down掉,则R3的路由条目浮现出来。如下:

 

[R4-Ethernet0/0/1]dis ip rou

Route Flags: R - relay, D - download to fib

------------------------------------------------------------------------------

Routing Tables: Public

         Destinations : 10       Routes : 10       

 

Destination/Mask    Proto   Pre  Cost      Flags NextHop         Interface

 

        1.1.1.1/32  OSPF    10   51           34.0.0.3        Ethernet0/0/0

        2.2.2.2/32  OSPF    10   52           34.0.0.3        Ethernet0/0/0

        3.3.3.3/32  OSPF    10             34.0.0.3        Ethernet0/0/0

        4.4.4.4/32  Direct              127.0.0.1       LoopBack0

       12.0.0.0/24  OSPF    10   52           34.0.0.3        Ethernet0/0/0

       13.0.0.0/24  OSPF    10   51           34.0.0.3        Ethernet0/0/0

       34.0.0.0/24  Direct              34.0.0.4        Ethernet0/0/0

       34.0.0.4/32  Direct              127.0.0.1       Ethernet0/0/0

      127.0.0.0/8   Direct    0             127.0.0.1       InLoopBack0

      127.0.0.1/32  Direct              127.0.0.1       InLoopBack0

 

 

注:路由条目的往返路径可以不同,往返路径的总的cost也可以不同。

OSPF协议中Cost值的计算 实例讲解:

在ospf协议中每个接口根据其速率不同,都有一个默认的公式计算的cost值,ospf协议采用cost值来决定到达目的地的最佳路径,如果存在多条到达目的地的路径,优先选用cost值最低的路径。Cost值的计算是沿目的地途中所有转发数据的接口的cost值的总和,下面通过实验来验证一下。

如下拓扑图:

 

OSPF协议中Cost值的计算

 

在RT11上的路由表如下:

rt11#sh ip route

Codes: C - connected, S - static, 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

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

 

172.19.0.0/24 is subnetted, 1 subnets

O 172.19.10.0 [110/3] via 10.1.1.6, 00:00:12, FastEthernet2/0

10.0.0.0/30 is subnetted, 2 subnets

O 10.3.3.4 [110/2] via 10.1.1.6, 00:00:12, FastEthernet2/0

C 10.1.1.4 is directly connected, FastEthernet2/0

C 192.168.1.0/24 is directly connected, FastEthernet0/0

C 192.168.2.0/24 is directly connected, FastEthernet0/1

在这台路由器上到达172.19.10.0的度量值为3,计算方式为:

RT11的数据转发接口f2/0的cost值“1”+RT12的数据转发接口f2/0的cost值“1”+sw2的目标接口vlan1的cost值“1”=3。

为了证明,将RT12的数据流入接口f0/0的cost值改为50,数据转发接口f2/0的cost值改为60,sw2的vlan1的cost改为70,再来看看路由表:

rt11#sh ip route

Codes: C - connected, S - static, 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

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

 

172.19.0.0/24 is subnetted, 1 subnets

O 172.19.10.0 [110/131] via 10.1.1.6, 00:00:00, FastEthernet2/0

10.0.0.0/30 is subnetted, 2 subnets

O 10.3.3.4 [110/61] via 10.1.1.6, 00:00:00, FastEthernet2/0

C 10.1.1.4 is directly connected, FastEthernet2/0

C 192.168.1.0/24 is directly connected, FastEthernet0/0

C 192.168.2.0/24 is directly connected, FastEthernet0/1

这时到达172.19.10.0的度量值为131了,由1+60+70计算得来,RT12上的数据流入接口f0/0的cost值没有参与计算。

那么再看从SW1上的路由表:

sw2#sh ip route

Codes: C - connected, S - static, 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

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

 

172.19.0.0/24 is subnetted, 1 subnets

C 172.19.10.0 is directly connected, Vlan1

10.0.0.0/30 is subnetted, 2 subnets

C 10.3.3.4 is directly connected, Vlan2

O 10.1.1.4 [110/51] via 10.3.3.5, 00:00:00, Vlan2

O 192.168.1.0/24 [110/52] via 10.3.3.5, 00:00:00, Vlan2

O 192.168.2.0/24 [110/52] via 10.3.3.5, 00:00:00, Vlan2

从这个路由表上就更加验证了ospf协议中度量值的计算,从sw1到192.168.1.0的度量值计算为:

Sw1的数据转发接口vlan2的cost值“1”+RT12的数据转发接口f0/0的cost值“50”+RT11的目的接口的cost值“1”=52

那么针对重发布的路由的度量值又是如何计算的呢,在RT11上将两个直连网络192.168.1.0和192.168.2.0改为重发布:

配置命令:

access-list 10 permit 192.168.1.0

access-list 20 permit 192.168.2.0

!

route-map setmetric permit 10

match ip address 10

set metric 5

!

route-map setmetric permit 20

match ip address 20

set metric 20

 

router ospf 100

log-adjacency-changes

redistribute connected subnets route-map setmetric

这样就将192.168.1.0的度量值改为了5,192.168.2.0的度量值改为了20,在rt12和SW1上查看路由表:

rt12#sh ip route

Codes: C - connected, S - static, 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

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

 

172.19.0.0/24 is subnetted, 1 subnets

O 172.19.10.0 [110/130] via 10.3.3.6, 00:04:08, FastEthernet2/0

10.0.0.0/30 is subnetted, 2 subnets

C 10.3.3.4 is directly connected, FastEthernet2/0

C 10.1.1.4 is directly connected, FastEthernet0/0

O E2 192.168.1.0/24 [110/5] via 10.1.1.5, 00:04:08, FastEthernet0/0

O E2 192.168.2.0/24 [110/20] via 10.1.1.5, 00:04:08, FastEthernet0/0

 

sw2#sh ip route

Codes: C - connected, S - static, 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

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

 

172.19.0.0/24 is subnetted, 1 subnets

C 172.19.10.0 is directly connected, Vlan1

10.0.0.0/30 is subnetted, 2 subnets

C 10.3.3.4 is directly connected, Vlan2

O 10.1.1.4 [110/51] via 10.3.3.5, 00:04:32, Vlan2

O E2 192.168.1.0/24 [110/5] via 10.3.3.5, 00:04:32, Vlan2

O E2 192.168.2.0/24 [110/20] via 10.3.3.5, 00:04:32, Vlan2

RT12和SW1的路由表中,到达192.168.1.0和192.168.2.0的度量值和重发布时指定的度量值是一致的,那么就说明重发布时的度量值与接口的cost值是无关的。

了解了cost值的计算,可以在具有冗余的路径的环境下合理的修改cost值,来控制数据的转发路径。


  • 更新时间:2018年7月15日23:25:27 ,共 10337 字。