我们知道当路由器收到两个来自同一AS的具有不同MED值的相同路由条目时,在高优先级属性值相等的情况下它将选择MED值小的路由作为最优路径。


路由器不会对来自不同AS的MED值进行比较。当路由器收到来自两个不同AS的路由条目且都带有MED值时,如果不考虑Weight,local preference,as_path以及origin type的情况,路由器必须选择一条最优路径,考虑以下情况:


路由器A收到以下到达相同目的地1.0.0.0/8的三条路由:



1     ASPATH 200   MED 100   internal    IGP metric to NEXT_HOP 10


2     ASPATH 100   MED 150   internal    IGP metric to NEXT_HOP 5


3     ASPATH 200   MED 200   external   



路由器将选择哪条路径作为最优路径?



在这种情况下路由器缺省先比较最晚学到的两条路由。假设3最早进入bgp路由表,1最晚进入bgp路由表。


此时首先比较第1和第2条路由。由于它们来自不同的AS,不比较MED值,并且都是从IBGP学到,此时选择路由条目2(拥有最小的到下一跳的IGP metric值)。路由条目2再与3进行比较,同样不比较MED,结果路由3将被选为最优路径(从EBGP学到路由)。即从最晚学到的两条路由开始比较,比较出一条临时的最优路径,再与稍早的路由进行比较,比较出来的路由再与更早的路由进行比较,两两比较一直到与最早的路由进行比较从而得到一条最优路径。从比较结果可以看出MED值可能没有起到其应有的作用:第3条路由的MED小于第一条路由,但却被选为最优路由。



有两条命令可以改变这种默认行为:


bgp deterministic-med


bgp always-compare-med



分别在bgp路由进程模式下配置



bgp deterministic-med 命令被配置后,比较步骤是首先根据AS号对从不同AS学到的路由进行分类,结果上述的三条路由将被分为两组:


Group 1     1    ASPATH 200   MED 100   internal    IGP metric to NEXT_HOP 10


                  3    ASPATH 200   MED 200   external   


                 


Group 2     2    ASPATH 100   MED 150   internal    IGP metric to NEXT_HOP 5



先对每个组选出一条最优路由,路由1作为group1的最优路由(拥有最小的MED),路由2作为group2的最优路由。路由1和2再进行比较,由于2的IGP metric to NEXT_HOP 小,所以最终选择路由2作为最优路由。



不论bgp deterministic-med 是否被配置,路由器都不会对来自不同AS的路由比较MED值,此时可以通过配置


bgp always-compare-med命令进行改变,直接比较MED值并选择值最小的作为最优路由,即使路由来自从不同AS。结果是路由1被选为最优。




实验验证:


拓扑图见帖子最下方


连接R1,R2,R4,R5,R3之间的网段分别是


192.168.1.0/24,192.168.2.0/24,192.168.3.0/24,192.168.4.0/24



R3,R4之间192.168.5.0/24  R1,R5配置环回接口1.1.1.1/24并用network命令通告。



主要配置如下:


R1:


router bgp 100


network 1.0.0.0


neighbor 192.168.1.2 remote-as 300


neighbor 192.168.1.2 route-map set_med_150 out



access-list 1 permit 1.0.0.0


route-map set_med_150 permit 10


match ip address 1


set metric 150



R5:


router bgp 200


network 1.0.0.0


neighbor 192.168.3.1 remote-as 300


neighbor 192.168.3.1 route-map set_med_200 out


neighbor 192.168.4.1 remote-as 300


neighbor 192.168.4.1 route-map set_med_100 out



access-list 1 permit 1.0.0.0


route-map set_med_200 permit 10


match ip address 1


set metric 200



route-map set_med_100 permit 10


match ip address 1


set metric 100



默认情况下:


R4#s ip bgp


BGP table version is 6, local router ID is 192.168.3.1


Status codes: s suppressed, d damped, h history, * valid, > best, i - internal


Origin codes: i - IGP, e - EGP, ? - incomplete



   Network            Next Hop            Metric LocPrf Weight Path


* i1.0.0.0            192.168.1.1            150    100      0 100 i


* i                        192.168.4.2            100    100      0 200 i


*>                       192.168.3.2            200             0 200 i


*>i192.168.1.0   192.168.2.1              0    100      0 i


*>i192.168.4.0   192.168.5.1              0    100      0 i



R4(config-router)#bgp deterministic-med


R4#s ip bgp


BGP table version is 6, local router ID is 192.168.3.1


Status codes: s suppressed, d damped, h history, * valid, > best, i - internal


Origin codes: i - IGP, e - EGP, ? - incomplete



   Network          Next Hop            Metric LocPrf Weight Path


*>i1.0.0.0          192.168.1.1            150    100      0 100 i


* i                 192.168.4.2            100    100      0 200 i


*                   192.168.3.2            200             0 200 i


*>i192.168.1.0      192.168.2.1              0    100      0 i


*>i192.168.4.0      192.168.5.1              0    100      0 i




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



R4#s ip bgp


BGP table version is 6, local router ID is 192.168.3.1


Status codes: s suppressed, d damped, h history, * valid, > best, i - internal


Origin codes: i - IGP, e - EGP, ? - incomplete



   Network             Next Hop            Metric LocPrf Weight Path


* i1.0.0.0             192.168.1.1            150    100      0 100 i


*>i                       192.168.4.2            100    100      0 200 i


*                          192.168.3.2            200             0 200 i


*>i192.168.1.0    192.168.2.1              0    100      0 i


*>i192.168.4.0    192.168.5.1              0    100      0 i



R4#s ip bgp 1.0.0.0


BGP routing table entry for 1.0.0.0/8, version 6


Paths: (3 available, best #2, table Default-IP-Routing-Table)


  Advertised to non peer-group peers:


  192.168.3.2


  100


    192.168.1.1 (metric 5) from 192.168.2.1 (192.168.2.1)


      Origin IGP, metric 150, localpref 100, valid, internal


  200


    192.168.4.2 (metric 10) from 192.168.5.1 (192.168.5.1)


      Origin IGP, metric 100, localpref 100, valid, internal, best


  200


    192.168.3.2 from 192.168.3.2 (1.1.1.1)


      Origin IGP, metric 200, localpref 100, valid, external