原理概述:

      当一台BGP路由器中存在多条去往同一目标网络的BGP路由时,BGP协议会对这些BGP路由的属性进行比较,以确定去往该目标的最优BGP路由。BGP首先比较的是路由信息

的首选值(PreVal),如果PreVal相同,就会比较本地优先级(Local Preference,缩写为LocPrf)属性。

      Local Preference属性可以用于选择流量离开AS时的最佳路由,也就是控制流量从哪个出口离开AS。当BGP路由器通过不同的IBGP对等体接收到目标网络相同但下一跳不同的

多条路由时,将优先选择Local Preference值较高的路由。

      Local Preference只在IBGP对等体之间进行通告,EBGP对等体之间传递BGP路由时,不携带Local Preference属性。默认情况下,本地使用network命令通告或者import命令引

入到BGP中的路由的Local Preference值为空。当从IBGP对等体接收到的路由的Local Preference值为空时,接收路由器会傅100作为这条路由的Local Preference默认值。当从IBGP

对等体接收到的路由的Local Preference值不为空时,接收路由器默认不做修改。

      Local Preference值是一个32比特的整数,取值范围是0到2的32次方。

 

网络图:

BGP 路径选择--Local Preference_默认值

 

 BGP 路径选择--Local Preference_取值范围_02

 

 BGP 路径选择--Local Preference_路由表_03

 

 BGP 路径选择--Local Preference_取值范围_04

 

 BGP 路径选择--Local Preference_默认值_05

 接下来,为R1, R2, R3, R4配置BGP协议:

BGP 路径选择--Local Preference_取值范围_06

 

 BGP 路径选择--Local Preference_路由表_07

 

 BGP 路径选择--Local Preference_取值范围_08

 

 BGP 路径选择--Local Preference_取值范围_09

 

查看R1的BGP邻居情况:

BGP 路径选择--Local Preference_取值范围_10

 

 可以看到,R1和R2 , R3 形成了EBGP的关系

 

在R4上查看BGP路由表:

BGP 路径选择--Local Preference_路由表_11

 

 在R2上查看BGP路由表:

BGP 路径选择--Local Preference_默认值_12

 

可以看到,Local Preference属性不会通告给EBGP对等体,仅在AS内传递时才会通告。

 

修改R3的Local preference的值为200:

BGP 路径选择--Local Preference_路由表_13

 

 再次查看R4的BGP路由表:

BGP 路径选择--Local Preference_默认值_14

 

 可以看到,从R4到R1,权经过R3路由器

 

从R4到R1的跟踪:

BGP 路径选择--Local Preference_路由表_15

 

 

 

 

实现从R4到R1 ,经过R2

BGP 路径选择--Local Preference_默认值_16

 

 

BGP 路径选择--Local Preference_取值范围_17

 

 设置了R2的Local  Preference的值 为500,再看从R4到R1的跟踪路径:

BGP 路径选择--Local Preference_路由表_18

 

 从跟踪结果可以看出,实现了负载均衡。