【转】BGP Cost Community详解

1、Cost Community概述

  • Cost Community是一个扩展的Community属性,只能传递给iBGP邻居或联邦peer(含联邦iBGP及联邦eBGP邻居),不能传递给eBGP邻居;
  • 通过利用Cost Community,我们能够在一个AS或联邦内部自定义BGP的最优路径选择。Cost Community事实上是提供给我们除了“BGP13条选路规则”之外的又一“插入点”( point of insertion),相当于提供给我们另一个操控路由优选的手柄。
  • 针对internal路由,在route-map中使用set extcommunity cost命令去设置cost community值。
  • 在上述set命令后,配置一个ID(0-255)以及cost number(0- 4294967295)。Cost number值可以影响路径的优选,越小越优先。如果两个路径cost number值相等,那么拥有小ID值的被优选。
  • Cost community属性值是扩展community,在向邻居发送前需配置neighbor send-community extand。
  • 下述命令可以跟route-map来设置Cost Community:
aggregate-address
neighbor default-originate route-map {in | out}
neighbor route-map
network route-map
redistribute route-map

2、Cost Community特性的限制

  • Cost Community特性只能部署在一个AS或联邦AS(大AS)内部。她是一个扩展Community属性并且只能传递给iBGP邻居或联邦邻居(联邦iBGP或联邦eBGP邻居),不能传递给eBGP邻居(不会报错,就是单纯的不携带)。
  • 在部署Cost Community之前需确保AS或联邦内所有路由器都能识别她,并且要在AS或联邦内保证Cost Community配置的连续性,以防止潜在的环路问题。
  • Multiple cost community set clauses may be configured with the set extcommunity cost command in a single route map block or sequence. However, each set clause must be configured with a different ID value (0-255) for each point of insertion (POI). The ID value determines preference when all other attributes are equal. The lowest ID value is preferred.

3、Cost Community如何影响BGP最佳路径选择

  • Cost Community事实上是提供给我们除了“BGP13条选路规则”之外的又一“插入点”( point of insertion),相当于提供给我们另一个操控路由优选的手柄。
  • 默认情况下,这个插入点在“BGP13条选路规则”的规则八之后,规则九之前,也就是在负载均衡规则的前面。当一个BGP路由器有多条路径可达同一个目的地,选路规则进程会决定哪条路径是best,这条best的路径最终被安装进路由表使用。Cost Community允许我们在此之前横刀进入,干预路由的优选。如果路由器在本地路由优选过程中不能识别cost community,那么就默默的忽略cost community。
  • POI一共有两个,一个在规则八之后,体现在配置上就是set extcommunity cost igp,这是Cisco IOS的默认POI,也就是如果前面这条命令不加igp关键字,则默认就是igp;另一个POI是在所有的13条选路规则之前,也就是比weight的优先级还要高,体现在配置上就是set extcommunity cost pre-bestpath。
  • 我们可以针对多条路径,在同一个POI设置不同的Cost Community。当这些路由进行PK时,cost value最低的优选,如果cost value相等,则优选拥有最小cost ID的路由。如果某条路由没有携带cost community,那么在cost community PK的这个环节,一个默认的cost value会被赋给这条路由, 这个值是2147483647,也就是cost community最大可选值4294967295的一半儿。
  • 默认情况下,在CISCO IOS中如果BGP路由器收到的某条路由携带了Cost Community,那么这玩意儿就开始工作了(开始在POI影响选路规则并进行路由间的PK),如果本路由器想完全忽略Cost Community,那么可以在BGP进程中配置bgp bestpath cost-community ignore,即可无视Cost Community。
  • 多条同一目的地的路径,可以针对特定的POI配置多个Cost

在PK的时候,从最小的CostID比起,例如下面两条路由:

bgp-cost-community-1

我们可以理解为在Cost Community的比较时,以CostID为组别进行,上面大家都只携带了一个CostCommunity,那就从最小的CostID=1比起,路由1的Cost Value是10,大于路由2的9,因此路由2胜出。

bgp-cost-community-2

上图就有所不同了,按照Cost ID来PK,先看Cost ID=1,路由1的值是10,而路由2没有定义CostID=1的value,因此系统给了一个默认值2147483647,这样一来当然是路由1优选了。所以这里绝不能简单的理解成:“先比cost value,再比cost ID”。要充分理解比较的方法。

bgp-cost-community-3

再来看一个例子加深理解,上图中两条路由来PK,首先最小的CostID=1,所以先从1比起,路由1的CostID=1对应的Cost Value=2147483647,其实就是默认值,而路由2没有配置costID=1的costValue,因此系统分配一个默认值,这样在costID=1这组的PK,两人打了个平手。接下去比CostID=3,路由1的CostID=3对应的Cost Value没有配置,因此分配一个默认值2147483647,而路由2配置的是10,明显10更小,所以最终路由2被优选。

4、实验(初始环境)

bgp-cost-community-4

先考虑一下上述拓扑中的初始环境,R1-R2,R2-R3之间建立iBGP邻居关系。

R1及R3都向R2通告一条8.8.8.8/32的BGP路由(直连网段)。所有路由器上没有部署策略。

那么在默认情况下,在R2上这两条关于8.8.8.8/32的BGP路径具备等价负载均衡的条件,我们此时并没有开启maximum-paths,R2最终会优选来自R1的路由,因为R1的BGP RouterID1.1.1.1小于R3的BGP RouterID 3.3.3.3,这是规则11的作用。

现在开启maximum-paths ibgp 2,那么这两条路径都会被放入IP路由表执行等价负载均衡。但是BGP表中的best路由仍然是优选的R1传来的路由。这些内容我们都已经烂熟了,当做复习也罢。

5、实验(Cost Community基础)

bgp-cost-community-5

现在我们在R1及R3上部署route-map,使得各自的BGP路由更新如上图所示。

R1的配置如下:

route-map setComm permit 10
 set extcommunity cost 1 20 // 注意,这里extcommunity后不加POI关键字的话,默认为igp

router bgp 123
 network 8.8.8.8 mask 255.255.255.255 route-map setComm
 neighbor 2.2.2.2 send-community extended

R3的配置类似,不再赘述。

那么在R2#show ip bgp 8.8.8.8
BGP routing table entry for 8.8.8.8/32, version 10
Paths: (2 available, best #2, table Default-IP-Routing-Table)
Multipath: iBGP
Flag: 0xA00
  Not advertised to any peer
  Local
    1.1.1.1 (metric 65) from 1.1.1.1 (1.1.1.1)
      Origin IGP, metric 0, localpref 100, valid, internal
      Extended Community: Cost:igp:1:20
  Local
    3.3.3.3 (metric 65) from 3.3.3.3 (3.3.3.3)
      Origin IGP, metric 0, localpref 100, valid, internal, best
      Extended Community: Cost:igp:1:10

我们看到,由于R3传递过来的路由,Cost Community值为10,要比R1的20小,因此R2优选R3传来的路由。注意这里的插入点POI默认是igp,也就是在规则八之后、规则九之前。此刻由于在规则九(等价负载均衡规则)前R2就已经优选了R3传来的路由,因此再看看R2的IP路由表:

R2#show ip route bgp 
B       8.8.8.8 [200/0] via 3.3.3.3, 00:06:37

就只有来自R3的路由了(即使我们配置了maximum-paths ibgp 2,路由表也不会出现等价负载均衡的现象)。

这里如果完美主义症又犯了的话,可以将R2上、连接R3的接口ospf cost改大,这样R2到3.3.3.3的IGP metric就变大了,R2就又优选R1传来的8.8.8.8,这就充分证明了Cost Community的IGP POI的插入点的位置:

bgp-cost-community-6
最后再来一个测试,如果此刻在R2的BGP进程中增加如下配置:
bgp bestpath cost-community ignore
则完全就当Cost Community不存在了,“选路规则13条”翻身做主人,如果配置了maximum-path照样等价负载均衡无鸭梨。

6、实验(测试默认Cost Community值)

bgp-cost-community-7
实验环境变成了如上所示。R3不携带Cost Community。

那么在R2上,实际上已经收到了来自R1的8.8.8.8路由更新且携带了Cost Community,那么这厮就不能当Cost Community不存在了,自然要进行Cost Community的比较,但是R3发来的8.8.8.8没有Cost Community啊,咋办,于是乎就给丫一个默认值:2147483647,虽然这个值在show ip bgp 8.8.8.8时看不到,但是确实起作用了。

因此最终优选的来自R1的路由,这里我们可以进一步验证,就是把R1传来的路由Cost value改成2147483648,那么这个值比默认值大了1,于是,R2此刻又优选R3传来的路由了。

R2#sh ip b 8.8.8.8
BGP routing table entry for 8.8.8.8/32, version 15
Paths: (2 available, best #1, table Default-IP-Routing-Table)
Multipath: iBGP
Flag: 0xA00
  Not advertised to any peer
  Local
    3.3.3.3 (metric 65) from 3.3.3.3 (3.3.3.3)
      Origin IGP, metric 0, localpref 100, valid, internal, best    // 没有
  Local                                                  costCommu,给默认值
    1.1.1.1 (metric 65) from 1.1.1.1 (1.1.1.1)
      Origin IGP, metric 0, localpref 100, valid, internal
      Extended Community: Cost:igp:1:2147483648 (default+1)   // 比默认值大1

7、实验(Cost ID的PK)

bgp-cost-community-8

环境又发生了一点变化,R1及R3传来的8.8.8.8,Cost Community的value相等。但是costID不同。
前面我们已经说过了,具体的比较方法。最终R2优选来自R3的路由。

再做一个测试,将R3传来的路由CostID改大,则又优选R1了:

bgp-cost-community-9

实际上这里不能死记:“如果cost value相等,则cost ID小的优选”,而要理解个中原因,具体原因在上文中已经说明的非常清楚了。

8、实验(测试pre-bestpath的POI)

bgp-cost-community-10
在R2上做配置,neighbor 1.1.1.1 weight 100及neighbor 3.3.3.3 weight 200。

毫无疑问13条选路规则中的规则一生效了,R2将优选R3传来的路由,因为它的weight值大。

现在我们要通过利用pre-bestpath这一插入点,来在weight之前控制路由优选。

R1的配置如下:

route-map setComm permit 10
 set extcommunity cost pre-bestpath 1 10     // 注意一定要加pre-bestpath关键字

router bgp 123
 network 8.8.8.8 mask 255.255.255.255 route-map setComm
 neighbor 2.2.2.2 send-community extended

R3的配置类似,不再赘述。

这样一来,当R2收到这两条路由,就会在13条选路规则开始之前,先进行Cost Community的比较,最终R1传来的路由胜出,因此优选R1传来的8.8.8.8,如下图。

bgp-cost-community-119、实验(cost community在联邦eBGP邻居之间的传递)

bgp-cost-community-12

环境又改变了。R1、R2、R3属于一个联邦AS123,同时分别属于联邦成员AS64551、63552、64553,三台路由器都使用Loopback口建立联邦eBGP邻居关系,地址分别是1.1.1.1、2.2.2.2、3.3.3.3,现在R1及R3都向R2发布一条8.8.8.8/32的BGP路由。

那么在不做任何策略的情况下,R2最终将优选BGP routerID小的BGP邻居发来的路由,也就是优选R1发来的路由。

bgp-cost-community-13

现在开始在R1及R3上做策略了,如上图所示。

则最终R2优选R3发来的路由。所以Cost Community在联邦eBGP peer之间传递没有问题。

 

红茶三杯(http://weibo.com/vinsoney/)原创文章,作者版权所有,转载请注明出处

红茶三杯
网络工程 | 项目管理 | CCIE培训
学习 沉淀 成长 分享
微博:http://weibo.com/vinsoney
博客:http://blog.sina.com.cn/vinsoney
站点:http://ccietea.com

这篇文章对你有帮助吗?

相关文章

发表评论?

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据