泰克实验室塑造 发表于 2014-4-18 14:50:32

Cisco BGP Best Path Selection Algorithm

Cisco BGP Best Path Selection Algorithm

         Cisco IOS把所有【目标Prefix相同】的【有效Routes】都放在一个List中。
      
         最新收到Route放在List的最上方。
      
         然后,Routes将从上至下一一对比,例如 ——
                ○ Route①和Route②对比
                ○ ①②中的胜出者和Route③对比
                ○ 胜出者再和Route④对比……
               
         直到和最底部的Route对比完毕,就能选择出最优的Route。

         List中的Routes根据以下规则进行对比
      ——
      
                ①. 优选Weight最大的Route
                ——
                        § Weight是Cisco私有参数,只在BGP Speaker本地有效
                        
                ②. 优选Local_Pref最大的Route
                ——
                        § 如果Route不带Local_Pref Attribute,就被认为Local_Pref值等于100。
                        
                        § 可以在BGP进程下使用bgp default local-preference改变默认的Local_Pref值。
                        
                ③. 优选本地始发的Route
                ——
                        § 本地始发指在本地使用network、redistribute、aggregate生成的Routes
                        
                        § network、redistribute比aggregate优先
                        
                ④. 优选AS Path最短的Route
                ——
                        § 如果使用了bgp bestpath as瀢愀琀栀 ignore命令(2821不支持?),将不会对比AS Path
                        
                        § AS_Set作为1个AS计算(不管其中包含多少个AS)
                        
                        § AS_Confed_Sequence和AS_Confed_Set不纳入AS Path长度计算。
                        
                ⑤. 优选Origin Type最优的Route
                ——
                        § IGP优于EGP优于Incomplete
                        
                ⑥. 优选MED最小的Route
                ——
                        § MED的对比只会发生在AS_Sequence中首个AS号相同的Routes之间。
                        
                        § 也就是说,默认下
                              □ 只会对比来自同一邻居AS的Routes
                              □ 无视AS_Confed_Sequence,MED在Confederation子AS间不起作用
                              
                        § 如果使用了bgp always挢漀洀瀀愀爀攀洢攀搀紀令,将对比所有Routes的MED
                              □ 从上至下按顺序对比
                              
                        § 如果使用了bgp bestpath med confed命令,MED的对比只也会发生在AS_Confed_Sequence中首个AS号相同的Routes之间。
                        
                        § MED Value为4,294,967,295的Route在放到BGP Table前改变为4,294,967,294
                        
                        § 如果Route不带MED Attribute,就被认为MED值等于0。
                        
                        § 如果使用了bgp bestpath med missing愢猀眢漀爀猀琀紀令,不带MED Attribute的Route被认为MED值等于4,294,967,294。
                        
                        § 如果使用了bgp deterministic洢攀搀紀令,可能会影响Routes对比的结果
                              □ List中从相同AS中收到的Routes将被“挪”到一起,并分组
                              □http://bbs.tech-lab.cn/data/attachment/forum/201205/03/1338390ur12ryr2e92c79y.png

                              □ 然后,所有Routes将先在各自的Group中进行对比,PK出胜负,再从上至下进行对比。
                              □ 此命令一般用于在BGP Confederation中通过MED优选Route。
                              
                ⑦. 优选从eBGP Peer学来的Route(相对iBGP Peer)
                ——
                        § 也就是说,优选域外路径,以减少AS内部消耗。
                        
                        § 从Confederation eBGP Peer学来的Routes(含有AS_Confed_Sequence和AS_Confed_Set的Routes)被视为从iBGP Peer学来的Routes。
                        
                        § 如果选出了最优的Route,跳到Step 9。
                        
                ⑧. 优选到达其BGP Next Hop的IGP Metric最小的Route。
                ——
                        § 即使选出了最优的Route,继续下一个Step
                        
                ⑨. 判定是否启用了BGP Multiple(负载均衡)
                ——
                        § 如果最优Route还没选出,继续下一个Step
                        
                ⑩. 如果Routes都是从eBGP Peer学来,优选先收到的Route(最旧的Route)
                ——
                        § 比较Routes新旧可以减少Routes翻动(越旧的Route一般越稳定)
                        
                        § 如果符合以下条件,跳过这个Step
                        ——
                                        a. 启用了bgp best path compare爢漀甀琀攀爀椀搀紀令
                                        b. Routes学自同一个eBGP Peer(从Router-ID相同的eBGP Peer收到的Routes)
                                        c. 当前没有最优Route(例如,通告最优Route的邻居挂了,丢失了最优Route)
                                       
                11. 优选从Router-ID小的BGP Peer学到的Route
                ——
                        § 如果Route包含Originator ID Attribute(Route从RR学来),用Originator ID代替Router-ID进行对比。
                        
                12. 在BGP RR环境中,如果Originator ID或Router-ID都一样,优选最短的Cluster List(Length最小)
               

                13.优选从地址(neighborx.x.x.x)最小的BGP Peer学到的Route
究极版:

、bgp_route.c文件中的bgp_process函数中:   structbgp_info *new_select;   structbgp_info *old_select;   ......
   old_select= NULL;   new_select= NULL;   for(ri = rn->info; ri; ri = ri->next)   {      if(CHECK_FLAG (ri->flags, BGP_INFO_SELECTED))      {          old_select= ri;      }      ......      if (bgp_info_cmp (bgp, ri, new_select))      {                   new_select= ri;      }   }

2、bgp_route.c文件中的bgp_info_cmp函数中:// Compare two bgp route entity.bris preferable then return 1. //int bgp_info_cmp (struct bgp *bgp, struct bgp_info *new, struct bgp_info*exist){u_int32_tnew_pref;u_int32_texist_pref;u_int32_tnew_med;u_int32_texist_med;structin_addr new_id;structin_addr exist_id;intnew_cluster;intexist_cluster;intinternal_as_route = 0;intconfed_as_route = 0;intcost_community = 0;intret;// 0. Nullcheck. //if(new == NULL)    return0;if(exist == NULL)    return1;// 1. Weightcheck. //if(new->attr->weight > exist->attr->weight)    return1;if(new->attr->weight < exist->attr->weight)    return0;// 2. Localpreference check. //if(new->attr->flag & ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF))    new_pref= new->attr->local_pref;else    new_pref= bgp->default_local_pref;if(exist->attr->flag & ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF))    exist_pref= exist->attr->local_pref;else    exist_pref= bgp->default_local_pref;
if(new_pref > exist_pref)    return1;if(new_pref < exist_pref)    return0;// 3. Localroute check. //if(new->sub_type == BGP_ROUTE_STATIC)    return1;if(exist->sub_type == BGP_ROUTE_STATIC)    return0;if(new->sub_type == BGP_ROUTE_REDISTRIBUTE)    return1;if(exist->sub_type == BGP_ROUTE_REDISTRIBUTE)    return0;if(new->sub_type == BGP_ROUTE_AGGREGATE)    return1;if(exist->sub_type == BGP_ROUTE_AGGREGATE)    return0;// 4. AS pathlength check. //if(! bgp_flag_check (bgp, BGP_FLAG_ASPATH_IGNORE))    {      if(new->attr->aspath->count < exist->attr->aspath->count)return1;      if(new->attr->aspath->count > exist->attr->aspath->count)return0;    }// 5. Origincheck. //if(new->attr->origin < exist->attr->origin)    return1;if(new->attr->origin > exist->attr->origin)    return0;// 6. MEDcheck. //internal_as_route= (new->attr->aspath->length == 0      &&exist->attr->aspath->length == 0);confed_as_route= (new->attr->aspath->length > 0      &&exist->attr->aspath->length > 0      &&new->attr->aspath->count == 0      &&exist->attr->aspath->count == 0);
if(bgp_flag_check (bgp, BGP_FLAG_ALWAYS_COMPARE_MED)      ||(bgp_flag_check (bgp, BGP_FLAG_MED_CONFED)&&confed_as_route)      ||aspath_cmp_left (new->attr->aspath, exist->attr->aspath)      ||aspath_cmp_left_confed (new->attr->aspath, exist->attr->aspath)      ||internal_as_route)    {      new_med= bgp_med_value (new->attr, bgp);      exist_med= bgp_med_value (exist->attr, bgp);      if(new_med < exist_med)return1;      if(new_med > exist_med)return0;    }// 7. Peertype check. //if(peer_sort (new->peer) == BGP_PEER_EBGP      &&peer_sort (exist->peer) == BGP_PEER_IBGP)    return1;if(peer_sort (new->peer) == BGP_PEER_EBGP      &&peer_sort (exist->peer) == BGP_PEER_CONFED)    return1;if(peer_sort (new->peer) == BGP_PEER_IBGP      &&peer_sort (exist->peer) == BGP_PEER_EBGP)    return0;if(peer_sort (new->peer) == BGP_PEER_CONFED      &&peer_sort (exist->peer) == BGP_PEER_EBGP)    return0;// 8. IGPmetric check. //if(new->igpmetric < exist->igpmetric)    return1;if(new->igpmetric > exist->igpmetric)    return0;// 9. costcommunity check. //if(! bgp_flag_check (bgp, BGP_FLAG_COST_COMMUNITY_IGNORE))    {      cost_community= ecommunity_cost_cmp (new->attr->ecommunity,         exist->attr->ecommunity,ECOMMUNITY_COST_POI_IGP);      if(cost_community > 0)return1;      if(cost_community < 0)return0;    }// 10. Maximumpath check. ////11. If both paths are external, prefer the path that was received   first(the oldest one).This step minimizes route-flap, since a   newerpath won't displace an older one, even if it was the   preferredroute based on the additional decision criteria below.//if(! bgp_flag_check (bgp, BGP_FLAG_COMPARE_ROUTER_ID)      &&peer_sort (new->peer) == BGP_PEER_EBGP      &&peer_sort (exist->peer) == BGP_PEER_EBGP)    {      if(CHECK_FLAG (new->flags, BGP_INFO_SELECTED))return1;      if(CHECK_FLAG (exist->flags, BGP_INFO_SELECTED))return0;    }// 12.Rourter-ID comparision. //if(new->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))    new_id.s_addr= new->attr->originator_id.s_addr;else    new_id.s_addr= new->peer->remote_id.s_addr;if(exist->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))    exist_id.s_addr= exist->attr->originator_id.s_addr;else    exist_id.s_addr= exist->peer->remote_id.s_addr;if(ntohl (new_id.s_addr) < ntohl (exist_id.s_addr))    return1;if(ntohl (new_id.s_addr) > ntohl (exist_id.s_addr))    return0;// 13. Clusterlength comparision. //if(new->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST))    new_cluster= new->attr->cluster->length;else    new_cluster= 0;if(exist->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST))    exist_cluster= exist->attr->cluster->length;else    exist_cluster= 0;if(new_cluster < exist_cluster)    return1;if(new_cluster > exist_cluster)    return0;// 14.Neighbor address comparision. //ret= sockunion_cmp (new->peer->su_remote, exist->peer->su_remote);if(ret == 1)    return0;if(ret == -1)    return1;return1;}

wlcbhrss 发表于 2014-4-29 13:36:46

好东西!好好学习学习!

wlcbhrss 发表于 2014-4-29 13:37:23

好东西!好好学习学习!

capxon 发表于 2015-3-31 22:20:14

路过,看看

capxon 发表于 2015-3-31 22:20:25

好东西

capxon 发表于 2015-3-31 22:20:37

谢谢楼主分享了

capxon 发表于 2015-3-31 22:20:47

{:6_267:}{:6_267:}{:6_267:}

capxon 发表于 2015-3-31 22:20:59

{:6_268:}{:6_268:}{:6_268:}

capxon 发表于 2015-3-31 22:21:10

{:6_290:}{:6_290:}{:6_290:}

capxon 发表于 2015-3-31 22:21:21

{:6_299:}{:6_299:}{:6_299:}
页: [1]
查看完整版本: Cisco BGP Best Path Selection Algorithm