十八、CCNP-BGP(选路规则)

18.6 BGP选路规则

18.6.1 介绍

BGP选路规则提供了丰富的路由策略部署依据

拓扑

按顺序一次比较。

18.6.1.1 试验准备

R1基础配置,并引入一条路由

R1(config)#interface serial 0/0
R1(config-if)#ip address 10.1.13.1 255.255.255.0
R1(config-if)#no shutdown
R1(config)#interface loopback 0
R1(config-if)#ip address 1.1.1.1 255.255.255.255
R1(config-if)#no shutdown
R1(config)#interface loopback 1
R1(config-if)#ip address 100.0.1.1 255.255.255.0
R1(config-if)#no shutdown
R1(config)#interface loopback 2
R1(config-if)#ip address 100.0.2.1 255.255.255.0
R1(config-if)#no shutdown

R1(config)#router bgp 100
R1(config-router)#neighbor 10.1.13.3 remote-as 345
R1(config-router)#network 100.0.1.0 mask 255.255.255.0

R2基础配置,并引入一条路由

R2(config)#interface serial 0/0
R2(config-if)#ip address 10.1.25.2 255.255.255.0
R2(config-if)#no shutdown
R2(config)#interface loopback 0
R2(config-if)#ip address 2.2.2.2 255.255.255.255
R2(config-if)#no shutdown
R2(config)#interface loopback 1
R2(config-if)#ip address 100.0.1.1 255.255.255.0
R2(config-if)#no shutdown
R2(config)#interface loopback 2
R2(config-if)#ip address 100.0.2.1 255.255.255.0
R2(config-if)#no shutdown

R2(config)#router bgp 200
R2(config-router)#bgp router-id 2.2.2.2
R2(config-router)#neighbor 10.1.25.5 remote-as 345
R2(config-router)#network 100.0.1.0 mask 255.255.255.0

R3基础配置,OSPF及BGP

R3(config)#interface serial 0/0
R3(config-if)#ip address 10.1.13.3 255.255.255.0
R3(config-if)#no shutdown
R3(config)#interface serial 0/1
R3(config-if)#ip address 10.1.34.3 255.255.255.0
R3(config-if)#no shutdown
R3(config)#interface loopback 0
R3(config-if)#ip address 3.3.3.3 255.255.255.255
R3(config-if)#no shutdown

R3(config)#router ospf 1 
R4(config-router)#router-id 3.3.3.3
R3(config-router)#network 10.1.34.3 0.0.0.0 area 0
R3(config-router)#network 3.3.3.3 0.0.0.0 area 0

R3(config)#router bgp 345
R3(config-router)#neighbor 10.1.13.1 remote-as 100
R3(config-router)#neighbor 4.4.4.4 remote-as 345
R3(config-router)#neighbor 4.4.4.4 update-source loopback 0
R3(config-router)#bgp router-id 3.3.3.3

R4配置,OSPF及BGP配置

R4(config)#interface serial 0/0
R4(config-if)#ip address 10.1.34.4 255.255.255.0
R4(config-if)#no shutdown
R4(config)#interface serial 0/1
R4(config-if)#ip address 10.1.45.4 255.255.255.0
R4(config-if)#no shutdown
R4(config)#interface loopback 0
R4(config-if)#ip address 4.4.4.4 255.255.255.255
R4(config-if)#no shutdown

R4(config)#router ospf 1
R4(config-router)#router-id 4.4.4.4
R4(config-router)#network 10.1.34.4 0.0.0.0 area 0
R4(config-router)#network 10.1.45.4 0.0.0.0 area 0
R4(config-router)#network 4.4.4.4 0.0.0.0 area 0

R4(config)#router bgp 345 
R4(config-router)#bgp router-id 4.4.4.4
R4(config-router)#neighbor 3.3.3.3 remote-as 345
R4(config-router)#neighbor 3.3.3.3 update-source loopback 0
R4(config-router)#neighbor 5.5.5.5 remote-as 345
R4(config-router)#neighbor 5.5.5.5 update-source loopback 0

R5基础配置,OSPF,BGP配置

R5(config)#interface serial 0/0
R5(config-if)#ip address 10.1.45.5 255.255.255.0 
R5(config-if)#no shutdown
R5(config)#interface serial 0/1
R5(config-if)#ip address 10.1.25.5 255.255.255.0
R5(config-if)#no shutdown
R5(config)#interface loopback 0
R5(config-if)#ip address 5.5.5.5 255.255.255.255
R5(config-if)#no shutdown

R5(config)#router ospf 1 
R4(config-router)#router-id 5.5.5.5
R5(config-router)#network 10.1.45.5 0.0.0.0 area 0
R5(config-router)#network 5.5.5.5 0.0.0.0 area 0

R5(config)#router bgp 345
R5(config-router)#bgp router-id 5.5.5.5
R5(config-router)#neighbor 4.4.4.4 remote-as 345
R5(config-router)#neighbor 4.4.4.4 update-source loopback 0
R5(config-router)#neighbor 10.1.25.2 remote-as 200
R4#show ip bgp
BGP table version is 1, local router ID is 4.4.4.4
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
* i100.0.1.0/24     10.1.25.2                0    100      0 200 i
* i                 10.1.13.1                0    100      0 100 i

* i                 10.1.13.1                0    100      0 100 i

R4收到100.0.1.0网段的两个下一跳,即去往R1和R2。

两条路由都没有优先BEST,路由都不可达。

R3(config)#router bgp 345
R3(config-router)#neighbor 4.4.4.4 next-hop-self

R5(config)#router bgp 345
R5(config-router)#neighbor 4.4.4.4 next-hop-self
R4#show ip bgp
BGP table version is 2, local router ID is 4.4.4.4
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
* i100.0.1.0/24     5.5.5.5                  0    100      0 200 i
*>i                 3.3.3.3                  0    100      0 100 i

这时出现了优先,从R3走。

根据11条规则,一条条对比,最后到BGP邻居ROUTER-ID越小越优先,所以从R3走。

18.6.2 规则1:Weight最大

Weight本地属性,不传递。

18.6.2.1 方法1:手动修改

18.6.2.2 方法2:ROUTE-MAP修改

R4自己设置ROUTE-MAP,影响自己路由。

18.6.2.3 试验

当前情况下,优先R3,改为R5。

R4(config)#router bgp 345
R4(config-router)#neighbor 5.5.5.5 weight 10

设置WEIGHT值为10

R4#show ip bgp
BGP table version is 3, local router ID is 4.4.4.4
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*>i100.0.1.0/24     5.5.5.5                  0    100     10 200 i
* i                 3.3.3.3                  0    100      0 100 i

这时,R5的WEIGHT为10,链路就为最优了。

R4#show 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

     100.0.0.0/24 is subnetted, 1 subnets
B       100.0.1.0 [200/0] via 5.5.5.5, 00:14:11
     3.0.0.0/32 is subnetted, 1 subnets
O       3.3.3.3 [110/65] via 10.1.34.3, 00:53:26, Serial0/0
     4.0.0.0/32 is subnetted, 1 subnets
C       4.4.4.4 is directly connected, Loopback0
     5.0.0.0/32 is subnetted, 1 subnets
O       5.5.5.5 [110/65] via 10.1.45.5, 00:53:26, Serial0/1
     10.0.0.0/24 is subnetted, 2 subnets
C       10.1.45.0 is directly connected, Serial0/1
C       10.1.34.0 is directly connected, Serial0/0

可以看到下一跳为5.5.5.5

R4(config)#router bgp 345
R4(config-router)#no neighbor 5.5.5.5 weight 10

去除WEIGHT设置

R4#show ip bgp
BGP table version is 4, local router ID is 4.4.4.4
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
* i100.0.1.0/24     5.5.5.5                  0    100      0 200 i
*>i                 3.3.3.3                  0    100      0 100 i

重新回到R3

R4(config)#ip prefix-list 1 permit 100.0.1.0/24

R4(config)#route-map test permit 10
R4(config-route-map)#match ip address prefix-list 1
R4(config-route-map)#set weight 100

R4(config)#router bgp 345
R4(config-router)#neighbor 5.5.5.5 route-map test in

通过ROUTE-MAP修改WEIGHT值

R4#show ip bgp
BGP table version is 5, local router ID is 4.4.4.4
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*>i100.0.1.0/24     5.5.5.5                  0    100    100 200 i
* i                 3.3.3.3                  0    100      0 100 i

此时WEIGHT已经修改为100,走R5了。

R4(config)#router bgp 345
R4(config-router)#no neighbor 5.5.5.5 route-map test in

R4(config)#no route-map test

恢复环境,供下一次使用

18.6.3 规则2:Local_Pref最大

只能在AS内部传递。

18.6.3.1 试验

R3#show ip bgp
BGP table version is 2, local router ID is 3.3.3.3
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*> 100.0.1.0/24     10.1.13.1                0             0 100 i

从R1传过来的LOCAL_PREF为空,由于从EBGP传过来,不能携带LOCAL_PREF,所以为空,但是在本地赋值,默认为100

R3#show ip bgp 100.0.1.0
BGP routing table entry for 100.0.1.0/24, version 2
Paths: (1 available, best #1, table Default-IP-Routing-Table)
  Advertised to update-groups:
     2
  100
    10.1.13.1 from 10.1.13.1 (100.0.2.1)
      Origin IGP, metric 0, localpref 100, valid, external, best

可以看到默认值为100

R5(config)#ip prefix-list 1 permit 100.0.1.0/24

R5(config)#route-map test
R5(config-route-map)#match ip address prefix-list 1
R5(config-route-map)#set local-preference 101

R5(config)#router bgp 345
R5(config-router)#neighbor 4.4.4.4 route-map test out

可以在R5的OUT方向上做

R4#show ip bgp
BGP table version is 7, local router ID is 4.4.4.4
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*>i100.0.1.0/24     5.5.5.5                  0    101      0 200 i
* i                 3.3.3.3                  0    100      0 100 i

从R4上可以看到,已经修改为101了,从R5路径走

R5(config)#router bgp 345
R5(config-router)#no neighbor 4.4.4.4 route-map test out

R5(config)#no route-map test

恢复环境

还可以从R4的IN方向做ROUTE-MAP。

18.6.4 规则3:起源于本地的路由

18.6.4.1 试验

R1#show ip bgp
BGP table version is 2, local router ID is 100.0.2.1
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*> 100.0.1.0/24     0.0.0.0                  0         32768 i

如R1宣告了100.0.1.0,为起源于本地的路由,NET HOP为全0。

在R3和R4都配同一条静态路由,并宣告经BGP测试。

R3(config)#ip route 8.0.0.0 255.0.0.0 null 0
R3(config)#router bgp 345
R3(config-router)#network 8.0.0.0 mask 255.0.0.0

将静态路由宣告进BGP

R4#show ip bgp
BGP table version is 9, local router ID is 4.4.4.4
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*>i8.0.0.0          3.3.3.3                  0    100      0 i
* i100.0.1.0/24     5.5.5.5                  0    100      0 200 i
*>i                 3.3.3.3                  0    100      0 100 i

R4中也能收到,去往8.0.0.0的下一跳为3.3.3.3

R4(config)#ip route 8.0.0.0 255.0.0.0 null 0
R4(config)#router bgp 345
R4(config-router)#network 8.0.0.0 mask 255.0.0.0

R4也宣告同一条静态路由进BGP

R4#show ip bgp
BGP table version is 12, local router ID is 4.4.4.4
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*> 8.0.0.0          0.0.0.0                  0         32768 i
* i                 3.3.3.3                  0    100      0 i
* i100.0.1.0/24     5.5.5.5                  0    100      0 200 i
*>i                 3.3.3.3                  0    100      0 100 i

这事R4的BGP表里面关于8.0.0.0就有两条路由了,优选本地,由于WEIGHT大,所以没法证明本地优先。

R4(config)#route-map test permit
R4(config-route-map)#set weight 0

R4(config)#router bgp 345
R4(config-router)#network 8.0.0.0 mask 255.0.0.0 route-map test

宣告静态路由时,就设置ROUTE-MAP,设置WEIGHT为0

R4#show ip bgp
BGP table version is 13, local router ID is 4.4.4.4
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*> 8.0.0.0          0.0.0.0                  0             0 i
* i                 3.3.3.3                  0    100      0 i
* i100.0.1.0/24     5.5.5.5                  0    100      0 200 i
*>i                 3.3.3.3                  0    100      0 100 i

这时两条weight都为0,两条LOCAK_PREF都为100,这时比对第三条规则,优选本地。

18.6.5 规则4:AS_PATH最短路由

AS_PATH只针对EBGP邻居,因为只在EBGP邻居间发生变化。

希望100.0.2.0从R5走,只需要加上从R1过来的AS_PATH长度。

18.6.5.1 试验

R3(config)#no ip route 8.0.0.0 255.0.0.0 null 0
R3(config)#router bgp 345
R3(config-router)#no network 8.0.0.0 mask 255.0.0.0

恢复R3

R4(config)#no ip route 8.0.0.0 255.0.0.0 null 0
R4(config)#router bgp 345
R4(config-router)#no network 8.0.0.0 mask 255.0.0.0 route-map test

恢复R4

通过操控AS_PATH,让100.0.1.0走R5。

R3(config)#ip prefix-list 1 permit 100.0.1.0/24
R3(config)#route-map test
R3(config-route-map)#match ip address prefix-list 1
R3(config-route-map)#set as-path prepend 888
R3(config)#router bgp 345
R3(config-router)#neighbor 10.1.13.1 route-map test in

设置从R1过来的100.0.1.0,增加AS_PATH 888

R3#show ip bgp
BGP table version is 8, local router ID is 3.3.3.3
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*> 100.0.1.0/24     10.1.13.1                0             0 888 100 i

可以看出,AS_PATH为888 100

R4#show ip bgp
BGP table version is 15, local router ID is 4.4.4.4
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*>i100.0.1.0/24     5.5.5.5                  0    100      0 200 i
* i                 3.3.3.3                  0    100      0 888 100 i

从R4上看到从R3过来的路由AS_PATH为888 100,AS_PATH更长,则优选短的,从R5走为最优。

R3(config)#router bgp 345
R3(config-router)#no neighbor 10.1.13.1 route-map test in

R3(config)#no route-map test

恢复

18.6.5.2 规则补充1

第10条规则,优选最老的。谁先传过来,就优选谁

18.6.5.3 规则补充2

从R5看到的,从R4过来的AS_PATH为1,从R3过来的AS_PATH为3,所以从R4走。

R1更新给R4是,插入了100号,依旧选R4。

当R4插入100 100后,两个长度则一样了,对比规则额,则优先从R3走。

18.6.6 规则5:Origin(不建议)

表示路由起源类型,EGP是一个古老的协议,现在基本不用了。

默认优先R3,现在改为R5

18.6.6.1 方式1:修改注入方式

最简单的方式,就是R1注入路由时,不用宣告方式NETWORK,而是用重发布方式。

R1(config)#router bgp 100
R1(config-router)#no network 100.0.1.0 mask 255.255.255.0

删除宣告方式

R1(config)#ip prefix-list 1 permit 100.0.1.0/24

R1(config)#route-map test
R1(config-route-map)#match ip address prefix-list 1

R1(config)#router bgp 100
R1(config-router)#redistribute connected route-map test

改为重发布方式

R1#show ip bgp
BGP table version is 6, local router ID is 100.0.2.1
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*> 100.0.1.0/24     0.0.0.0                  0         32768 ?

这时可以看到ORGIN方式为?,即重发布方式

R4#show ip bgp
BGP table version is 17, local router ID is 4.4.4.4
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*>i100.0.1.0/24     5.5.5.5                  0    100      0 200 i
* i                 3.3.3.3                  0    100      0 100 ?

R4上看到的也是?,重发布,这时根据ORGIN优先级,选择宣告方式,走R5。

R1(config)#router bgp 100
R1(config-router)#no redistribute connected

R1(config)#router bgp 100
R1(config-router)#no redistribute connected route-map test
R1(config-router)#network 100.0.1.0 mask 255.255.255.0

恢复注入方式

18.6.6.2 方式2(建议):ROUTE-MAP

利用之前定的ROUTE-MAP ,添加

R1(config)#route-map test permit 10
R1(config-route-map)#set origin incomplete

R1(config)#router bgp 100
R1(config-router)#network 100.0.1.0 mask 255.255.255.0 route-map test
R4#show ip bgp
BGP table version is 25, local router ID is 4.4.4.4
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*>i100.0.1.0/24     5.5.5.5                  0    100      0 200 i
* i                 3.3.3.3                  0    100      0 100 ?

这时可以看到R3为?,走R5。

R1(config)#router bgp 100
R1(config-router)#no network 100.0.1.0 mask 255.255.255.0 route-map test
R1(config-router)#network 100.0.1.0 mask 255.255.255.0

恢复环境

R4#show ip bgp
BGP table version is 26, local router ID is 4.4.4.4
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*>i100.0.1.0/24     3.3.3.3                  0    100      0 100 i
* i                 5.5.5.5                  0    100      0 200 i

恢复完成

18.6.7 规则6:MED最小值

18.6.7.1 试验

调大R3的METRIC值,优先走R5

R4(config)#no route-map test

R4(config)#route-map test
R4(config-route-map)#match ip address prefix-list 1
R4(config-route-map)#set metric 100

R4(config)#router bgp 345
R4(config-router)#neighbor 3.3.3.3 route-map test in

对R3方向做ROUTE-MAP

R4#show ip bgp
BGP table version is 27, local router ID is 4.4.4.4
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*>i100.0.1.0/24     3.3.3.3                100    100      0 100 i
* i                 5.5.5.5                  0    100      0 200 i

R3方向METRIC值为100,比R5大,但还是走R3?,因为这两个来不不同的AS 100、200,而METRIC默认之比较来自同一邻居的AS。

R4(config)#router bgp 345
R4(config-router)#bgp always-compare-med

增加bgp always-compare-med属性

R4#show ip bgp
BGP table version is 2, local router ID is 4.4.4.4
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*>i100.0.1.0/24     5.5.5.5                  0    100      0 200 i
* i                 3.3.3.3                100    100      0 100 i

这时就比较METRIC值了,走R5(此处需要等待时间长,或者直接硬重置CLEAR IP BGP *)

R4(config)#router bgp 345
R4(config-router)#no bgp always-compare-med

不建议使用,恢复

R4(config)#router bgp 345
R4(config-router)#no neighbor 3.3.3.3 route-map test in
R4#show ip bgp
BGP table version is 3, local router ID is 4.4.4.4
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*>i100.0.1.0/24     3.3.3.3                  0    100      0 100 i
* i                 5.5.5.5                  0    100      0 200 i

环境已恢复,METRIC恢复为0

18.6.8 规则7:EBGP邻居过来路由

18.6.8.1 试验

前面6步都一样。

R5#show ip bgp
BGP table version is 4, local router ID is 5.5.5.5
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*> 100.0.1.0/24     10.1.25.2                0             0 200 i

目前R5上只有一条BGP路由,由于水平分割规则,R4不能把路由传给R5。

R4(config)#router bgp 345
R4(config-router)#neighbor 3.3.3.3 route-reflector-client

将R4配置为路由反射器

R5#show ip bgp
BGP table version is 4, local router ID is 5.5.5.5
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
* i100.0.1.0/24     3.3.3.3                  0    100      0 100 i
*>                  10.1.25.2                0             0 200 i

这时就有两条路由了,一条是IBGP传过来的路由,一条是EBGP传过来的路由,最优显示是从R2 EBGP过来的。

18.6.9 规则8:NEXT_HOP 最近路由

18.6.9.1 场景1

18.6.9.1.1 试验
R4#show ip bgp
BGP table version is 5, local router ID is 4.4.4.4
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*>i100.0.1.0/24     3.3.3.3                  0    100      0 100 i
* i                 5.5.5.5                  0    100      0 200 i

优选R3,基于规则11,比较ROUTE-ID。

R4#show 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

     100.0.0.0/24 is subnetted, 1 subnets
B       100.0.1.0 [200/0] via 3.3.3.3, 00:11:30
     3.0.0.0/32 is subnetted, 1 subnets
O       3.3.3.3 [110/65] via 10.1.34.3, 04:39:52, Serial0/0
     4.0.0.0/32 is subnetted, 1 subnets
C       4.4.4.4 is directly connected, Loopback0
     5.0.0.0/32 is subnetted, 1 subnets
O       5.5.5.5 [110/65] via 10.1.45.5, 04:39:52, Serial0/1
     10.0.0.0/24 is subnetted, 2 subnets
C       10.1.45.0 is directly connected, Serial0/1
C       10.1.34.0 is directly connected, Serial0/0

这两条路由的COST度量值都是[110/65]

R4#show ip bgp 100.0.1.0
BGP routing table entry for 100.0.1.0/24, version 5
Paths: (2 available, best #1, table Default-IP-Routing-Table)
  Advertised to update-groups:
     1
  100, (Received from a RR-client)
    3.3.3.3 (metric 65) from 3.3.3.3 (3.3.3.3)
      Origin IGP, metric 0, localpref 100, valid, internal, best
  200
    5.5.5.5 (metric 65) from 5.5.5.5 (5.5.5.5)
      Origin IGP, metric 0, localpref 100, valid, internal

也可以看出,都是(metric 65)

R4(config)#interface serial 0/0
R4(config-if)#ip ospf cost 1111

直接修改接口COST值

R4#show ip bgp 100.0.1.0
BGP routing table entry for 100.0.1.0/24, version 6
Paths: (2 available, best #2, table Default-IP-Routing-Table)
Flag: 0x820
  Advertised to update-groups:
     2
  100, (Received from a RR-client)
    3.3.3.3 (metric 1112) from 3.3.3.3 (3.3.3.3)
      Origin IGP, metric 0, localpref 100, valid, internal
  200
    5.5.5.5 (metric 65) from 5.5.5.5 (5.5.5.5)
      Origin IGP, metric 0, localpref 100, valid, internal, best

可以看出COST值发生了变化

R4#show ip bgp
BGP table version is 6, local router ID is 4.4.4.4
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
* i100.0.1.0/24     3.3.3.3                  0    100      0 100 i
*>i                 5.5.5.5                  0    100      0 200 i

这时看出优选R5了

R4(config)#interface serial 0/0
R4(config-if)#no ip ospf cost 1111

去除设置COST属性

18.6.9.2 场景2

18.6.10 规则9:负载均衡

18.6.10.1 IBGP负载

需要同一个AS号过来路由。之前过来的AS_PATH值不一样,不具备做等价负载均衡的条件。

18.6.10.1.1 试验
R1(config)#no route bgp 100

R1(config)#router bgp 12
R1(config-router)#neighbor 10.1.13.3 remote-as 345
R1(config-router)#network 100.0.1.0 mask 255.255.255.0

修改AS号

R3(config)#router bgp 345 
R3(config-router)#no neighbor 10.1.13.1 
R3(config-router)#neighbor 10.1.13.1 remote-as 12

重置EBGP邻居关系

R2(config)#no router bgp 200

R2(config)#router bgp 12
R2(config-router)#neighbor 10.1.25.5 remote-as 345
R2(config-router)#network 100.0.1.0 mask 255.255.255.0

修改AS号

R5(config)#router bgp 345
R5(config-router)#no neighbor 10.1.25.2
R5(config-router)#neighbor 10.1.25.2 remote-as 12

重置EBGP邻居关系

R4#show ip  bgp
BGP table version is 10, local router ID is 4.4.4.4
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
* i100.0.1.0/24     5.5.5.5                  0    100      0 12 i
*>i                 3.3.3.3                  0    100      0 12 i

两条路由

R4#show ip bgp 100.0.1.0
BGP routing table entry for 100.0.1.0/24, version 10
Paths: (2 available, best #2, table Default-IP-Routing-Table)
Flag: 0x800
  Advertised to update-groups:
     1
  12
    5.5.5.5 (metric 65) from 5.5.5.5 (5.5.5.5)
      Origin IGP, metric 0, localpref 100, valid, internal
  12, (Received from a RR-client)
    3.3.3.3 (metric 65) from 3.3.3.3 (3.3.3.3)
      Origin IGP, metric 0, localpref 100, valid, internal, best

从R3R5过来的两条路由具备了等价负载均衡的条件

R4(config)#router bgp 345
R4(config-router)#maximum-paths ibgp 2

最大允许两条,如果不加IBGP,则只针对EBGP生效

R4#show ip bgp 100.0.1.0
BGP routing table entry for 100.0.1.0/24, version 11
Paths: (2 available, best #2, table Default-IP-Routing-Table)
Multipath: iBGP
Flag: 0x800
  Advertised to update-groups:
     1
  12
    5.5.5.5 (metric 65) from 5.5.5.5 (5.5.5.5)
      Origin IGP, metric 0, localpref 100, valid, internal, multipath
  12, (Received from a RR-client)
    3.3.3.3 (metric 65) from 3.3.3.3 (3.3.3.3)
      Origin IGP, metric 0, localpref 100, valid, internal, multipath, best

增加了:Multipath: iBGP,及路径的multipath属性

R4#show 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

     100.0.0.0/24 is subnetted, 1 subnets
B       100.0.1.0 [200/0] via 5.5.5.5, 00:01:55
                  [200/0] via 3.3.3.3, 00:05:00
     3.0.0.0/32 is subnetted, 1 subnets
O       3.3.3.3 [110/65] via 10.1.34.3, 00:05:06, Serial0/0
     4.0.0.0/32 is subnetted, 1 subnets
C       4.4.4.4 is directly connected, Loopback0
     5.0.0.0/32 is subnetted, 1 subnets
O       5.5.5.5 [110/65] via 10.1.45.5, 00:05:06, Serial0/1
     10.0.0.0/24 is subnetted, 2 subnets
C       10.1.45.0 is directly connected, Serial0/1
C       10.1.34.0 is directly connected, Serial0/0

路由表出现两个下一跳,都会使用

B       100.0.1.0 [200/0] via 5.5.5.5, 00:01:55
                  [200/0] via 3.3.3.3, 00:05:00

虽然说两条都用,但是只有一个BEST,只有BEST才会传递给邻居。

18.6.10.2 EBGP负载

右边不做任何策略时,所有路径属性都一致。

左边AS号不一样,虽然其他路径属性一致,但是AS_PATH内容不一致。则不符合条件

18.6.11 规则10:老EBGP邻居传递的路由

R4#show ip bgp summary
BGP router identifier 4.4.4.4, local AS number 345
BGP table version is 3, main routing table version 3
1 network entries using 117 bytes of memory
2 path entries using 104 bytes of memory
1 multipath network entries and 2 multipath paths
2/1 BGP path/bestpath attribute entries using 248 bytes of memory
1 BGP AS-PATH entries using 24 bytes of memory
0 BGP route-map cache entries using 0 bytes of memory
0 BGP filter-list cache entries using 0 bytes of memory
BGP using 493 total bytes of memory
BGP activity 1/0 prefixes, 2/0 paths, scan interval 60 secs

Neighbor        V    AS MsgRcvd MsgSent   TblVer  InQ OutQ Up/Down  State/PfxRcd
3.3.3.3         4   345      12      11        3    0    0 00:08:48        1
5.5.5.5         4   345      12      13        3    0    0 00:08:45        1

作为最老,即UP/DOWN时间最长的。邻居关系越老,越稳定。

18.6.11.1 试验1:EBGP

R1基础配置

R1(config)#interface loopback 0
R1(config-if)#ip address 100.0.1.1 255.255.255.0
R1(config-if)#no shutdown
R1(config)#interface serial 0/0
R1(config-if)#ip address 10.1.12.1 255.255.255.0
R1(config-if)#no shutdown

R1(config)#router bgp 100
R1(config-router)#neighbor 10.1.12.2 remote-as 200
R1(config-router)#network 100.0.1.0 mask 255.255.255.0

R2基础配置

R2(config)#interface serial 0/0
R2(config-if)#ip address 10.1.12.2 255.255.255.0
R2(config-if)#no shutdown
R2(config)#interface serial 0/1
R2(config-if)#ip address 10.1.23.2 255.255.255.0
R2(config-if)#no shutdown

R2(config)#router bgp 200
R2(config-router)#neighbor 10.1.12.1 remote-as 100
R2(config-router)#neighbor 10.1.23.3 remote-as 300

R3基础配置

R3(config)#interface loopback 0
R3(config-if)#ip address 100.0.1.1 255.255.255.0
R3(config-if)#no shutdown
R3(config)#interface serial 0/0
R3(config-if)#ip address 10.1.23.3 255.255.255.0
R3(config-if)#no shutdown

R3(config)#router bgp 300
R3(config-router)#neighbor 10.1.23.2 remote-as 200
R3(config-router)#network 100.0.1.0 mask 255.255.255.0
R2#show ip bgp summary
BGP router identifier 10.1.23.2, local AS number 200
BGP table version is 2, main routing table version 2
1 network entries using 117 bytes of memory
2 path entries using 104 bytes of memory
3/1 BGP path/bestpath attribute entries using 372 bytes of memory
2 BGP AS-PATH entries using 48 bytes of memory
0 BGP route-map cache entries using 0 bytes of memory
0 BGP filter-list cache entries using 0 bytes of memory
BGP using 641 total bytes of memory
BGP activity 1/0 prefixes, 2/0 paths, scan interval 60 secs

Neighbor        V    AS MsgRcvd MsgSent   TblVer  InQ OutQ Up/Down  State/PfxRcd
10.1.12.1       4   100       5       5        2    0    0 00:00:08        1
10.1.23.3       4   300       8       8        2    0    0 00:04:44        1

EBGP邻居关系都启来了

R2#show ip bgp
BGP table version is 2, local router ID is 10.1.23.2
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*  100.0.1.0/24     10.1.12.1                0             0 100 i
*>                  10.1.23.3                0             0 300 i

路由也都起来了,R2从R1和R3都学到了路由。

R2#show ip bgp 100.0.1.0
BGP routing table entry for 100.0.1.0/24, version 2
Paths: (2 available, best #2, table Default-IP-Routing-Table)
  Advertised to update-groups:
     1
  100
    10.1.12.1 from 10.1.12.1 (10.1.12.1)
      Origin IGP, metric 0, localpref 100, valid, external
  300
    10.1.23.3 from 10.1.23.3 (10.1.23.3)
      Origin IGP, metric 0, localpref 100, valid, external, best

优选的是10.1.23.3,因为EBGP邻居关系老,建立的时间早

R2#clear ip bgp 10.1.23.3

重建邻居关系

R2#show ip bgp summary
BGP router identifier 10.1.23.2, local AS number 200
BGP table version is 3, main routing table version 3
1 network entries using 117 bytes of memory
2 path entries using 104 bytes of memory
3/1 BGP path/bestpath attribute entries using 372 bytes of memory
2 BGP AS-PATH entries using 48 bytes of memory
0 BGP route-map cache entries using 0 bytes of memory
0 BGP filter-list cache entries using 0 bytes of memory
BGP using 641 total bytes of memory
BGP activity 1/0 prefixes, 3/1 paths, scan interval 60 secs

Neighbor        V    AS MsgRcvd MsgSent   TblVer  InQ OutQ Up/Down  State/PfxRcd
10.1.12.1       4   100      17      18        3    0    0 00:12:11        1
10.1.23.3       4   300      25      25        3    0    0 00:00:32        1

10.1.23.3EBGP邻居关系时间新了

R2#show ip bgp 100.0.1.0
BGP routing table entry for 100.0.1.0/24, version 3
Paths: (2 available, best #2, table Default-IP-Routing-Table)
Flag: 0x820
  Advertised to update-groups:
     1
  300
    10.1.23.3 from 10.1.23.3 (10.1.23.3)
      Origin IGP, metric 0, localpref 100, valid, external
  100
    10.1.12.1 from 10.1.12.1 (10.1.12.1)
      Origin IGP, metric 0, localpref 100, valid, external, best

这时R1变为最优了。

18.6.11.2 试验2:IBGP

清除EBGP关系,利用IBGP建立

R1(config)#no router bgp 100
R2(config)#no router bgp 200
R3(config)#no router bgp 300

R1基础配置

R1(config)#router bgp 123
R1(config-router)#neighbor 10.1.12.2 remote-as 123
R1(config-router)#network 100.0.1.0 mask 255.255.255.0

R2基础配置

R2(config)#router bgp 123
R2(config-router)#neighbor 10.1.12.1 remote-as 123
R2(config-router)#neighbor 10.1.23.3 remote-as 123

R3基础配置

R3(config)#router bgp 123
R3(config-router)#neighbor 10.1.23.2 remote-as 123
R3(config-router)#network 100.0.1.0 mask 255.255.255.0
R2#show ip bgp summary
BGP router identifier 10.1.23.2, local AS number 123
BGP table version is 2, main routing table version 2
1 network entries using 117 bytes of memory
1 path entries using 52 bytes of memory
2/1 BGP path/bestpath attribute entries using 248 bytes of memory
0 BGP route-map cache entries using 0 bytes of memory
0 BGP filter-list cache entries using 0 bytes of memory
BGP using 417 total bytes of memory
BGP activity 1/0 prefixes, 1/0 paths, scan interval 60 secs

Neighbor        V    AS MsgRcvd MsgSent   TblVer  InQ OutQ Up/Down  State/PfxRcd
10.1.12.1       4   123       5       5        2    0    0 00:02:35        0
10.1.23.3       4   123       5       4        2    0    0 00:00:03        1

IBGP关系已建立

R2#show ip bgp
BGP table version is 3, local router ID is 10.1.23.2
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*>i100.0.1.0/24     10.1.12.1                0    100      0 i
* i                 10.1.23.3                0    100      0 i

两条路由都有了,优先级为R1。

R2#clear ip bgp 10.1.12.1
R2#show ip bgp
BGP table version is 5, local router ID is 10.1.23.2
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*>i100.0.1.0/24     10.1.12.1                0    100      0 i
* i                 10.1.23.3                0    100      0 i

重置R1后,依然选择最优路径为R1,说明老路由规则没有体现出来。

18.6.12 规则11:最小RouterID

18.6.12.1 试验1

R2#show ip bgp
BGP table version is 5, local router ID is 10.1.23.2
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*>i100.0.1.0/24     10.1.12.1                0    100      0 i
* i                 10.1.23.3                0    100      0 i
R2#show ip bgp 100.0.1.0
BGP routing table entry for 100.0.1.0/24, version 7
Paths: (2 available, best #1, table Default-IP-Routing-Table)
Flag: 0x840
  Not advertised to any peer
  Local
    10.1.12.1 from 10.1.12.1 (100.0.1.1)
      Origin IGP, metric 0, localpref 100, valid, internal, best
  Local
    10.1.23.3 from 10.1.23.3 (100.0.1.1)
      Origin IGP, metric 0, localpref 100, valid, internal

由于R1和R3上开了LOOPBACK0地址,所以默认ROUTERID为该地址

R1(config)#router bgp 123
R1(config-router)#bgp router-id 1.1.1.1

修改R1的ROUTERID

R2#show ip bgp 100.0.1.0
BGP routing table entry for 100.0.1.0/24, version 9
Paths: (2 available, best #1, table Default-IP-Routing-Table)
Flag: 0x840
  Not advertised to any peer
  Local
    10.1.12.1 from 10.1.12.1 (1.1.1.1)
      Origin IGP, metric 0, localpref 100, valid, internal, best
  Local
    10.1.23.3 from 10.1.23.3 (100.0.1.1)
      Origin IGP, metric 0, localpref 100, valid, internal

这时优选路径为R1,即ROUTERID中1.1.1.1最小的。

R1(config)#router bgp 123
R1(config-router)#bgp router-id 111.111.111.111

修改R1的ROUTERID

R2#show ip bgp 100.0.1.0
BGP routing table entry for 100.0.1.0/24, version 10
Paths: (2 available, best #2, table Default-IP-Routing-Table)
Flag: 0x840
  Not advertised to any peer
  Local
    10.1.12.1 from 10.1.12.1 (111.111.111.111)
      Origin IGP, metric 0, localpref 100, valid, internal
  Local
    10.1.23.3 from 10.1.23.3 (100.0.1.1)
      Origin IGP, metric 0, localpref 100, valid, internal, best

这时优选路径为R3,即100.0.1.1最小。

18.6.12.2 试验2

删除BGP配置

R1(config)#no router bgp 123
R2(config)#no router bgp 123
R3(config)#no router bgp 123

创建OSPF关系

R1(config)#interface loopback 0
R1(config-if)#ip address 1.1.1.1 255.255.255.255
R1(config-if)#no shutdown

R1(config)#interface loopback 1
R1(config-if)#ip address 11.11.11.11 255.255.255.255
R1(config-if)#no shutdown

R1(config)#router ospf 1
R1(config-router)#network 10.1.12.1 0.0.0.0 area 0
R1(config-router)#network 1.1.1.1 0.0.0.0 area 0
R2(config)#interface loopback 0
R2(config-if)#ip address 2.2.2.2 255.255.255.255
R2(config-if)#no shutdown

R2(config)#router ospf 1
R2(config-router)#network 10.1.12.2 0.0.0.0 area 0
R2(config-router)#network 10.1.23.2 0.0.0.0 area 0
R2(config-router)#network 2.2.2.2 0.0.0.0 area 0
R3(config)#interface loopback 0
R3(config-if)#ip address 3.3.3.3 255.255.255.255
R3(config-if)#no shutdown

R3(config)#router ospf 1
R3(config-router)#network 10.1.23.3 0.0.0.0 area 0
R3(config-router)#network 3.3.3.3 0.0.0.0 area 0

R1R2R3都用LOOPBACK0建立邻居关系

R1建立邻居关系

R1(config)#router bgp 123
R1(config-router)#neighbor 2.2.2.2 remote-as 123
R1(config-router)#neighbor 2.2.2.2 update-source loopback 0
R1(config-router)#neighbor 3.3.3.3 remote-as 123 
R1(config-router)#neighbor 3.3.3.3 update-source loopback 0
R1(config-router)#network 11.11.11.11 mask 255.255.255.255

R2配置为RR,R1为CLIENT

R2(config)#router bgp 123
R2(config-router)#neighbor 1.1.1.1 remote-as 123
R2(config-router)#neighbor 1.1.1.1 update-source loopback 0
R2(config-router)#neighbor 3.3.3.3 remote-as 123
R2(config-router)#neighbor 3.3.3.3 update-source loopback 0
R2(config-router)#neighbor 1.1.1.1 route-reflector-client

R3建立邻居关系

R3(config)#router bgp 123
R3(config-router)#neighbor 2.2.2.2 remote-as 123
R3(config-router)#neighbor 2.2.2.2 update-source loopback 0
R3(config-router)#neighbor 1.1.1.1 remote-as 123
R3(config-router)#neighbor 1.1.1.1 update-source loopback 0
R3#show ip bgp 11.11.11.11
BGP routing table entry for 11.11.11.11/32, version 3
Paths: (2 available, best #1, table Default-IP-Routing-Table)
  Not advertised to any peer
  Local
    1.1.1.1 (metric 129) from 1.1.1.1 (1.1.1.1)
      Origin IGP, metric 0, localpref 100, valid, internal, best
  Local
    1.1.1.1 (metric 129) from 2.2.2.2 (2.2.2.2)
      Origin IGP, metric 0, localpref 100, valid, internal
      Originator: 1.1.1.1, Cluster list: 2.2.2.2

R3收到了来自R1和R2的去往11.11.11.11的路由。

此处用的是Originator: 1.1.1.1和ROUTERID比,比不出结果,需要借助规则12,比较CLUSTER LIST最短的路由。一个是1,一个是0。所以选择是0的那个。

18.6.13 规则12:最短Cluster-List

18.6.14 规则13:最小邻居IP地址

发表回复