BGP选路原则第1条至第8条

Topology 1:

Platform:cisco iou

实验目的:通过实验理解BGP的选路原则。

BGP选路原则:
如果下一跳不可达,是不会参加选举的!
1. weight
Prefer the path that has the largest weight.思科的专有属性。
2. Local Preference
If the routes have the same weight, use the route with the highest local preference.
The local preference attribute only is local to the autonomous system and does not get passed to EBGP neighbors. The higher the local preference, the more preferred the route is.
默认情况下,从EBGP学来的路由local preference是100,解决本AS域出口路由选择。他不会把此参数传递给EBGP的邻居。
3. Originated
If the routes have the same local preference, prefer the route that was originated by BGP on this router.
优选从本路由器使发的路由,包括本地network,重分发和或IGP已有的路由,有BGP配置的聚合地址也包括在内。
查看BGP路由表时可以看到有时路由前面带了一个“r”,说明在IGP中已经存在此路由了。
4. AS Path
If no route was originated, prefer the route with the shortest AS path.
5. Origin Code
If all paths are of the same AS length, prefer the route with lowest origin code (IGP < EGP < INCOMPLETE).
EGP是指那种老的协议,现在已经不再用了。
6. MED
If the origin codes are the same, prefer the path with the lowest Multi Exit Discriminator (MED 范围0到4,294,967,295).
The MED is exchanged between ASs; however, the MED that comes into an AS does not leave.
The MED can be used to influence the outbound decision of another AS. The lower the MED, the more preferred the route.
而且MED只在相同AS的情况下才对比,默认情况下不同AS的路由是不对比MED的。解决本AS域的入口路由选择。
另外MED的通告规则:
(1. ebgp在发布从ibgp学来的路由时,会清除MED的值。如果想强制通告,可以在宣告的路由上使用“set metric-type internal”
(2. 如果使用network或redistribute命令将来自IGP的路由通告到BGP中,那么BGP的MED是从IGP的metric导出的。
(3. 如果是aggregate-address命令注入路由,BGP的MED不被设置。
7. External paths over internal paths
If the MEDs are the same, prefer external paths over internal paths.
EBGP > confederation > IBGP
8. The path through the closest IGP neighbor
If they are still the same, prefer the path through the closest IGP neighbor.
优选对BGP下一跳具有最低IGP度量值的路径。
9. Multiple paths
Determine if multiple paths require installation in the routing table for BGP Multipath.
如果配置了“maximum-path N”。而且从同一个AS的对等体收到多条外部路由,侧可以将N条路由加入到路由表中,使EBGP负载分担。
N最大不能超过6;没有配置此命令是,默认是1。可以通过“sh ip bgp X.X.X.X”来查看最优的路径,这个最优是按接收时间来定的。
在这条最优路由转发到内部对等体之前,需要执行与“next-hop-self”一样的功能。
10. Prefer the path that was received first
When both paths are external, prefer the path that was received first (the oldest one)。
这可以使路由抖动重新收敛的影响降到最低,因为新的路径不会取代老的路径。
11. BGP Router ID
If they are still the same, prefer the path with the lowest BGP Router ID.
12. Cluster-ID
如果从路由反射器学到,优选最小Cluster-ID的路由。
13. Peer Adress
优选具有最低对等体地址的路由。

一、我们现在从第八条往上看吧。8. The path through the closest IGP neighbor
Step 1、配置各台路由器的IP地址,并且使用Ping命令确认各路由器的直连口的互通性。
Step 2、R1,R2运行OSPF,通告环回口,再使用环回口建IBGP。R2通告2.2.2.2/32 。R2,R3之间运行EBGP,R1,R5之间运行EBGP。
R1------------------------------------------------------------------
router ospf 100
 router-id 1.1.1.1
 log-adjacency-changes
 network 1.1.1.1 0.0.0.0 area 0
 network 12.1.1.1 0.0.0.0 area 0
!
router bgp 12
 no synchronization
 bgp router-id 1.1.1.1
 bgp log-neighbor-changes
 neighbor 2.2.2.2 remote-as 12
 neighbor 2.2.2.2 update-source Loopback0
 neighbor 2.2.2.2 next-hop-self
 neighbor 15.1.1.5 remote-as 345
 no auto-summary
--------------------------------------------------------------------
R2------------------------------------------------------------------
router ospf 100
 router-id 2.2.2.2
 log-adjacency-changes
 network 2.2.2.2 0.0.0.0 area 0
 network 12.1.1.2 0.0.0.0 area 0
!
router bgp 12
 no synchronization
 bgp router-id 2.2.2.2
 bgp log-neighbor-changes
 network 2.2.2.2 mask 255.255.255.255
 neighbor 1.1.1.1 remote-as 12
 neighbor 1.1.1.1 update-source Loopback0
 neighbor 1.1.1.1 next-hop-self
 neighbor 23.1.1.3 remote-as 345
 no auto-summary
--------------------------------------------------------------------
R3-----------------------------------------------------------------
router bgp 345
 no synchronization
 neighbor 23.1.1.2 remote-as 12
---------------------------------------------------------------------
R5-----------------------------------------------------------------
router bgp 345
 no synchronization
 neighbor 15.1.1.1 remote-as 12
---------------------------------------------------------------------

Step 3、R3,R4,R5之间运行OSPF,以及之间运行IBGP。
R3-----------------------------------------------------------------
router ospf 100
 router-id 3.3.3.3
 log-adjacency-changes
 network 3.3.3.3 0.0.0.0 area 0
 network 34.1.1.3 0.0.0.0 area 0
!
router bgp 345
 no synchronization
 bgp log-neighbor-changes
 neighbor 4.4.4.4 remote-as 345
 neighbor 4.4.4.4 update-source Loopback0
 neighbor 4.4.4.4 next-hop-self
 neighbor 5.5.5.5 remote-as 345
 neighbor 5.5.5.5 update-source Loopback0
 neighbor 5.5.5.5 next-hop-self
 no auto-summary
--------------------------------------------------------------------
R4----------------------------------------------------------------
router ospf 100
 router-id 4.4.4.4
 log-adjacency-changes
 network 4.4.4.4 0.0.0.0 area 0
 network 34.1.1.4 0.0.0.0 area 0
 network 45.1.1.4 0.0.0.0 area 0
!
router bgp 345
 no synchronization
 bgp router-id 4.4.4.4
 bgp log-neighbor-changes
 neighbor 3.3.3.3 remote-as 345
 neighbor 3.3.3.3 update-source Loopback0
 neighbor 3.3.3.3 next-hop-self
 neighbor 5.5.5.5 remote-as 345
 neighbor 5.5.5.5 update-source Loopback0
 neighbor 5.5.5.5 next-hop-self
 network 4.4.4.4 mask 255.255.255.255
 no auto-summary
--------------------------------------------------------------------
R5-----------------------------------------------------------------
router ospf 100
 router-id 5.5.5.5
 log-adjacency-changes
 network 5.5.5.5 0.0.0.0 area 0
 network 45.1.1.5 0.0.0.0 area 0
!
router bgp 345
 no synchronization
 bgp router-id 5.5.5.5
 bgp log-neighbor-changes
 neighbor 3.3.3.3 remote-as 345
 neighbor 3.3.3.3 update-source Loopback0
 neighbor 3.3.3.3 next-hop-self
 neighbor 4.4.4.4 remote-as 345
 neighbor 4.4.4.4 update-source Loopback0
 neighbor 4.4.4.4 next-hop-self
 no auto-summary
--------------------------------------------------------------------
Step 4、在R4上show ip bgp 。
R4#sh 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
*>i2.2.2.2/32       5.5.5.5                  0    100      0 12 i
* i                 3.3.3.3                  0    100      0 12 i

从以上信息,我们可以得出,R4去2.2.2.2的下一跳是5.5.5.5。原因是选路原则中,前七条均无法比较,所以根据第八条优先最近的IGP邻居。
R4,R5之间是以太网链路,开销是:19
R4,R3之间是T1的链路,所以开销要比以太网的大。

二、第七条:EBGP优于IBGP。
EBGP优于IBGP可以说是简单的根据AD的比较,但是,事实并不是这样的。大家可以回去做下实验,可以发现把IBGP的管理距离改为小于20,路由器依然会选择EBGP的路由做为最优。我个人的理解:AD是不同协议的比较,而IBGP,EBGP同属于BGP,AD根本不会参与路由决策。所以,这条选路原则只是一样硬性的规定,不作任何比较。

三、第六条:优先于低MED。
在基于以上配置的基础上,我们加以修改,来研究使用MED来影响选路。
Step 1、在R1,R2上使用前缀列表匹配2.2.2.2/32,然后在R1,R2的出方向使用route-map将其MED值设为5和10。
R1-------------------------------------------------------------
ip prefix-list A seq 5 permit 2.2.2.2/32
!
route-map AAA permit 10
 match ip address prefix-list A
 set metric 10
!
route-map AAA permit 20
router bgp 12
 neighbor 15.1.1.5 route-map AAA out
------------------------------------------------------------------
R2--------------------------------------------------------------
ip prefix-list A seq 5 permit 2.2.2.2/32
!
route-map ABC permit 10
 match ip address prefix-list A
 set metric 5
!
route-map ABC permit 20
router bgp 12
 neighbor 23.1.1.3 route-map ABC out
Step 2、分别查看各路由器的BGP表。
R3#sh ip bgp 
BGP table version is 4, 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
*> 2.2.2.2/32       23.1.1.2                 5             0 12 i
r>i4.4.4.4/32       4.4.4.4                  0    100      0 i


R4#sh 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
*>i2.2.2.2/32       3.3.3.3                  5    100      0 12 i
*> 4.4.4.4/32       0.0.0.0                  0         32768 i

R5#sh ip bgp 
BGP table version is 7, 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
*>i2.2.2.2/32       3.3.3.3                  5    100      0 12 i
*                   15.1.1.1                10             0 12 i
r>i4.4.4.4/32       4.4.4.4                  0    100      0 i

因为来自于R3的2.2.2.2/32的MED值较低,所以R4去2.2.2.2的路由会选择R3为下一跳路由。

四、第五条,IGP<EGP<incomplate
基于以上命令,我们尝使用第五条选路原则去影响选路,在R4上配置以下命令:
ip prefix-list A seq 5 permit 2.2.2.2/32
route-map AAA permit 10
 match ip address prefix-list A
 set origin incomplete
route-map AAA permit 20
router bgp 345
 neighbor 3.3.3.3 route-map AAA in

然后,sh ip bgp 
R4#sh 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
* i2.2.2.2/32       3.3.3.3                  0    100      0 12 ?
*>i                 5.5.5.5                  0    100      0 12 i
*> 4.4.4.4/32       0.0.0.0                  0         32768 i
 
去2.2.2.2的最优路径的下一跳变成了R5。

五、第四条,Prefer shortest AS path。
通过以上配置,我们知道R4去R2的路由的最优下一跳是R5,在这一步,我们使用第四条选路原则,把最优路径的下一跳使用R3。
R1--------------------------------------------------------------------------
ip prefix-list A seq 5 permit 2.2.2.2/32
!
route-map AAA permit 10
 match ip address prefix-list A
 set as-path prepend 12 12
!
route-map AAA permit 20
router bgp 12
 neighbor 15.1.1.5 route-map AAA out
-----------------------------------------------------------------------------
R5#sh ip bgp 
BGP table version is 6, 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
*  2.2.2.2/32       15.1.1.1                               0 12 12 12 i
*>i                 3.3.3.3                  0    100      0 12 i
r>i4.4.4.4/32       4.4.4.4                  0    100      0 i

R4#sh ip bgp 2.2.2.2
BGP routing table entry for 2.2.2.2/32, version 11
Paths: (1 available, best #1, table Default-IP-Routing-Table)
  Not advertised to any peer
  12
    3.3.3.3 (metric 65) from 3.3.3.3 (3.3.3.3)
      Origin incomplete, metric 0, localpref 100, valid, internal, best
R4#sh ip bgp 
BGP table version is 11, 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
*>i2.2.2.2/32       3.3.3.3                  0    100      0 12 ?
*> 4.4.4.4/32       0.0.0.0                  0         32768 i

六、第三条:起源于本地优先。

七、第二条:优先于高本地优先级。
在R3,R5上配置local_preference值影响BGP选路。
R3的local_preference的默认值为100。
那么我在只需要在R5上配置local_preference为200。
R5:
router bgp 345
 neighbor 15.1.1.1 route-map AAA in

ip prefix-list A seq 5 permit 2.2.2.2/32
!
route-map AAA permit 10
 match ip address prefix-list A
 set local-preference 200
!
route-map AAA permit 20

然后,在R4上show ip bgp 
R4#sh 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
*>i2.2.2.2/32       5.5.5.5                  0    200      0 12 12 12 i
*> 4.4.4.4/32       0.0.0.0                  0         32768 i

八、第一条选路原则是思科私有的:Weight,高优先。
上面我们配置了本地优先,使R4去R2的路由走R5,现在我们通过配置权值,使其路由走R3。
R4:
router bgp 345
 neighbor 3.3.3.3 weight 100
 neighbor 5.5.5.5 weight 50

R4#sh 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
* i2.2.2.2/32       5.5.5.5                  0    100     50 12 i
*>i                 3.3.3.3                  0    100    100 12 i
*> 4.4.4.4/32       0.0.0.0                  0         32768 i

配置权值时需注意的是,它只在本地路由器有效。
配置本地优先级需注意的是,它在本地AS有效。


 

发布了220 篇原创文章 · 获赞 2 · 访问量 4463

猜你喜欢

转载自blog.csdn.net/qq_43207781/article/details/105480943