BGP选路规则
BGP 选路规则:
- weight值大的优选(思科私有)
- Local-Preference本地优先级大的优选
- 优选本AS产生的路由(优先选用本地宣告的条目,下一跳地址为0.0.0.0)
- AS-Path较短的优选(AS数量越少,优先级越高)
- 起始属性(i优于e,e优于?)
- MED 度量值 越小越优
- EBGP优于IBGP的路由
- 到达目的网络的下一跳在IGP中的度量值,越小越优
- 负载均衡(前8条必须一致才能开启负载均衡)
- 选择最老邻居的路由(仅限于EBGP)
- Router ID越小越优
- Cluster-list 越短越优(记录经过的路由反射器的Router ID)
- 邻居IP地址 越小越优
之后所有实验使用的环境

| 路由器 | 接口 | 地址 | 路由器 | 接口 | 地址 |
|---|---|---|---|---|---|
| R1 | Lo 0 | 1.1.1.1/32 | R4 | Lo 0 | 192.168.1.1/24 |
| Lo 1 | 192.168.100.1 /24 | Lo 1 | 192.168.2.1/24 | ||
| E0/0 | 12.0.0.1/24 | Lo 2 | 192.168.3.1/24 | ||
| E0/1 | 13.0.0.1/24 | E0/0 | 24.0.0.4/24 | ||
| E0/2 | 15.0.0.1/24 | E0/1 | 34.0.0.4/24 | ||
| R2 | Lo 0 | 2.2.2.2/32 | R5 | Lo 0 | 192.168.10.1/24 |
| Lo 1 | 192.168.20.1/24 | Lo 1 | 192.168.11.1/24 | ||
| E0/0 | 24.0.0.2/24 | Lo 2 | 192.168.12.1/24 | ||
| E0/1 | 12.0.0.2/24 | E0/0 | 15.0.0.5/24 | ||
| R3 | Lo 0 | 3.3.3.3 | |||
| E0/0 | 34.0.0.3/24 | ||||
| E0/1 | 13.0.0.3/24 |
R1:
interface Loopback0
ip address 1.1.1.1 255.255.255.255
ip ospf 1 area 0
!
interface Loopback1
ip address 192.168.100.1 255.255.255.0
ip ospf 1 area 0
!
interface Ethernet0/0
ip address 12.0.0.1 255.255.255.0
ip ospf 1 area 0
duplex auto
!
interface Ethernet0/1
ip address 13.0.0.1 255.255.255.0
ip ospf 1 area 0
duplex auto
!
interface Ethernet0/2
ip address 15.0.0.1 255.255.255.0
duplex auto
!
interface Ethernet0/3
no ip address
shutdown
duplex auto
!
router ospf 1
router-id 1.1.1.1
!
router bgp 123
bgp router-id 1.1.1.1
bgp log-neighbor-changes
bgp default local-preference 200
network 192.168.100.0
neighbor 2.2.2.2 remote-as 123
neighbor 2.2.2.2 update-source Loopback0
neighbor 2.2.2.2 route-reflector-client
neighbor 2.2.2.2 next-hop-self
neighbor 3.3.3.3 remote-as 123
neighbor 3.3.3.3 update-source Loopback0
neighbor 3.3.3.3 route-reflector-client
neighbor 3.3.3.3 next-hop-self
neighbor 15.0.0.5 remote-as 500
!
R2:
interface Loopback0
ip address 2.2.2.2 255.255.255.255
ip ospf 1 area 0
interface Loopback1
ip address 192.168.20.1 255.255.255.0
ip ospf 1 area 0
interface Ethernet0/0
ip address 24.0.0.2 255.255.255.0
duplex auto
interface Ethernet0/1
ip address 12.0.0.2 255.255.255.0
ip ospf 1 area 0
duplex auto
interface Ethernet0/2
no ip address
shutdown
duplex auto
interface Ethernet0/3
no ip address
shutdown
duplex auto
router ospf 1
router-id 2.2.2.2
router bgp 123
bgp router-id 2.2.2.2
bgp log-neighbor-changes
network 192.168.10.0
network 192.168.20.0
neighbor 1.1.1.1 remote-as 123
neighbor 1.1.1.1 update-source Loopback0
neighbor 1.1.1.1 next-hop-self
neighbor 24.0.0.4 remote-as 400
R3:
interface Loopback0
ip address 3.3.3.3 255.255.255.255
ip ospf 1 area 0
interface Loopback1
ip address 192.168.30.1 255.255.255.0
ip ospf 1 area 0
interface Ethernet0/0
ip address 34.0.0.3 255.255.255.0
duplex auto
interface Ethernet0/1
ip address 13.0.0.3 255.255.255.0
ip ospf 1 area 0
duplex auto
interface Ethernet0/2
no ip address
shutdown
duplex auto
interface Ethernet0/3
no ip address
shutdown
duplex auto
router ospf 1
router-id 3.3.3.3
router bgp 123
bgp router-id 3.3.3.3
bgp log-neighbor-changes
network 192.168.30.0
neighbor 1.1.1.1 remote-as 123
neighbor 1.1.1.1 update-source Loopback0
neighbor 1.1.1.1 next-hop-self
neighbor 34.0.0.4 remote-as 400
R4:
interface Loopback0
ip address 192.168.1.1 255.255.255.0
interface Loopback1
ip address 192.168.2.1 255.255.255.0
interface Loopback2
ip address 192.168.3.1 255.255.255.0
interface Ethernet0/0
ip address 24.0.0.4 255.255.255.0
duplex auto
interface Ethernet0/1
ip address 34.0.0.4 255.255.255.0
duplex auto
router bgp 400
bgp router-id 192.168.1.1
bgp log-neighbor-changes
network 192.168.1.0
network 192.168.2.0
network 192.168.3.0
neighbor 24.0.0.2 remote-as 123
neighbor 34.0.0.3 remote-as 123
R5:
interface Loopback0
ip address 192.168.10.1 255.255.255.0
interface Loopback1
ip address 192.168.11.1 255.255.255.0
interface Loopback2
ip address 192.168.12.1 255.255.255.0
interface Ethernet0/0
ip address 15.0.0.5 255.255.255.0
duplex auto
router bgp 500
bgp router-id 192.168.10.1
bgp log-neighbor-changes
network 192.168.10.0
network 192.168.11.0
network 192.168.12.0
neighbor 15.0.0.1 remote-as 123
1.weight
- weight默认是0,本AS内宣告的路由默认weight值为32768
- 数字越高,越优先
- 思科私有
- 本地自有属性,不传递,只对in方向路由才有作用
实验
weight 配置
##创建一条前缀列表 匹配路由
ip prefix-list weight seq 5 permit 192.168.10.0/24
##创建一条route-map 引用前缀列表,然后修改weight
route-map weight permit 10
match ip address prefix-list weight
set weight 200
##要方放行所有
route-map weight permit 20
exit
##在bgp中引用route-map
router bgp 400
neighbor 24.0.0.2 route-map weight in
更改前

更改后

2.Local-preference 本地优先级
- 公认自决
- 默认不传递,默认值 100,越大越优
- 在IBGP环境中,修改参数值时in方向和out方向均有效。
- 在EBGP环境中,修改参数值时只针对in方向有效。且不传递IBGP中Local-preference。
- 本地优先级应用环境偏向于IBGP环境。
- 可以开启全局
实验
全局开启
在R1上开启
router bgp 123
bgp default local-preference 200

从EBGP 传入IBGP
修改的Local-Preference会传给其他IBGP
R4 lo1 192.168.1.1
R5 lo1192.168.12.1
R4的路由表192.168.12.1是R3加表
R4向R2和R3传递192.168.1.1的路由
在R2上进方向设置了192.168.1.1的值,修改完后R3的路由原来是从R4方向上过来的,现在变成了从R2下一跳(原因是local-preference优先级高)
R4ping192.168.12.1 源地址192.168.1.1
发包方向R3-R1-R5
回包方向R1-R2-R4
R2:
access-list 2 permit 192.168.1.0 0.0.0.255
route-map LP permit 10
match ip address 2
set local-preference 111
route-map LP permit 20
router bgp 123
neighbor 24.0.0.4 route-map LP in
R4:
access-list 1 permit 192.168.12.0 0.0.0.255
route-map Local_Preference permit 10
match ip address 1
set local-preference 200
route-map Local_Preference permit 20
router bgp 400
neighbor 34.0.0.3 route-map Local_Preference in
R4:

R2:

R3:

IBGP之间 out方向
修改的Local-Preference会传给其他IBGP
access-list 1 permit 192.168.20.0 0.0.0.255
route-map Local_Preference permit 10
match ip address 1
set local-preference 200
route-map Local_Preference permit 20
router bgp 123
neighbor 1.1.1.1 route-map Local_Preference out
修改前

修改后
R1作为路由反射器,R2和R3作为客户端。

从EBGP传入
access-list 1 permit 192.168.12.0 0.0.0.255
route-map Local_Preference permit 10
match ip address 1
set local-preference 200
route-map Local_Preference permit 20
router bgp 400
neighbor 34.0.0.3 route-map Local_Preference in
修改前

修改后

3.优选本AS产生的路由
- 优先选用本地宣告的条目,下一跳地址为0.0.0.0
实验
![]()
4.as-patch
路由传递所经过的AS号(show ip bgp 里的Path)
-
公认必遵
-
AS-Path越短越优
-
只能在EBGP上生效
-
调整AS-Path属性
-
手动设置经过的AS号(不推荐)
route-map AS permit 10match ip address 1 set as-path prepend 9 8 7 6 5 4 3 2 ``` 注:在原来path下增加 9 8 7 6 5 4 3 2- 自动复制最后经过的AS号(推荐)
route-map AS permit 10 match ip address 1 set as-path prepend last-as 5
注:最后进过的AS号复制添加5个
- 自动复制最后经过的AS号(推荐)
-
实验
方法一:
在R4 E0/0调用进方向的route-map
##前缀列表匹配网段
ip prefix-list AS seq 5 permit 192.168.11.0/24
##route-map调用和自动增加5个最后收到的as-path号
route-map weight permit 1
match ip address prefix-list AS
set as-path prepend last-as 5
## 允许所有
route-map weight permit 20
##BGP中调用
router bgp 400
neighbor 24.0.0.2 route-map weight in
更改前

更改后

5.起始属性(origin)(i优于e,e优于?)
i是 IGP 、e是EGP(现在不使用)、?是incomplete (重分布进来的路由)
调整起始属性
通过 route-map中set origin来实现
access-list 2 permit 192.168.3.0 0.0.0.255
route-map Origin permit 10
match ip address 2
set origin incomplete
router bgp 123
neighbor 2.2.2.2 route-map Origin in
更改前

更改后

6.优选 MED值更小的路由
med即BGP表中的metric值,越小越优先。
在IBGP环境和EBGP环境中都可以修改。
但是在EBGP环境中需要比较metri值时,需要先在router bgp下开启。命令:bgp always-compare-med
定义metric值通过route-map中的metric
实验
access-list 2 permit 192.168.3.0 0.0.0.255
route-map metric permit 10
match ip address 2
set metric 10
router bgp 123
neighbor 2.2.2.2 route-map metric in
更改前
192.168.3.0下一跳是 2.2.2.2

更改后
192.168.3.0 下一跳2.2.2.2metric变为10,根据metric越小越优先,192.168.3.0的下一跳变为3.3.3.3

7.EBGP优于IBGP的路由
如果上面6条完全相等的情况下,优先选择EBGP
使EBGP和IBGP前面6条完全相等的情况下

8.到达目的网络的下一跳在IGP中的度量值,越小越优
BGP选路时,默认不参考带宽。然而第八条选路原则参考igp的metric值,实际上是变相的参考了带宽。
9.负载均衡
前8条必须一致才能开启负载均衡,默认不开
maximum-paths
- 直接跟负载条目表示对EBGP开负载
- 跟ibgp+条目数,表示对IBGP开负载
- ebgp+条目数,表示EBGP和IBGP都开启负载均衡
实验
在R1上开启ibgp负载均衡
router bgp 123
maximum-paths ibgp 2
在R4上开启egp负载均衡
router bgp 400
maximum-paths 2
更改前
R1:

R4:

更改后
m代表multipath
R1:

R4:

10、选择最老邻居的路由(仅限于EBGP)
11、Router ID越小越优
12、Cluster-list 越短越优
经过的反射路径越短越优
查看cluster-list路径
方法1:
show ip bgp rr

origin为路由起源的ID
Cluster是经过的路由反射器的ID
如果发现origin是自身的router-id 则拒收用于防环
方法2:
show ip bgp 192.168.10.0



浙公网安备 33010602011771号