BGP有丰富的路由属性。我们通常控制BGP路由属性,来控制BGP路由流向。
cisco路由器常见路由属性如下:

1.优先选择最大weight(权重)的路由 (范围:0~65535)
2.优先选择有最大LOCAL_OREF(本地优先级)的路由 (范围:0~4294967295)
3.优先选择从本地路由器始发的路由 (即下一跳0.0.0.0)
4.优先选择AS-path数量最少的路由
5.根据origin属性,优先选择具有最低起源类型的路由 (IGP优先于EGP优先于incomplete)
6.优先选择具有最小MED值的路由 (范围:0~4294967295)
7.EBGP优先于IBGP
8.优先选择能够通过最近的IGP邻居到达的路径 (即对BGP下一跳具有最低IGP度量值的路径)
9.如果配置了maximum-path N(2≤N≤6),可实现负载均衡 (但BGP会继续比较,只有一条路径被打上“>”标签)
10.如果都是external(外部的)路由,优先选择最老(即最早学到)的路由
11.优先选择拥有最小BGP-router-ID的路由
12.如果是从路由反射器(RR)学习到的路由,优先选择拥有最小cluster-ID(路由反射器的router-ID)的路由
13.优先选择具有最低对等体地址接收到的路由(即邻居IP地址最小的优先)

实验拓扑图
BGP路由选择相关实验_第1张图片

配置前R1上的路由
R1#sh ip route
Codes: C - connected, S - static, R - RIP, M - mobile, B - BGP
D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
E1 - OSPF external type 1, E2 - OSPF external type 2
i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
ia - IS-IS inter area, * - candidate default, U - per-user static route
o - ODR, P - periodic downloaded static route

Gateway of last resort is not set

 172.16.0.0/24 is subnetted, 2 subnets

C 172.16.12.0 is directly connected, FastEthernet0/0
C 172.16.13.0 is directly connected, FastEthernet0/1
10.0.0.0/32 is subnetted, 2 subnets
B 10.10.6.6 [20/0] via 172.16.12.2, 00:27:41
C 10.10.1.1 is directly connected, Loopback0

使用weight改变路由

R1(config-router)#neighbor 172.16.13.3 weight 10
R1#clear ip bgp * soft

更改weight后路由
R1#sh ip route
Codes: C - connected, S - static, R - RIP, M - mobile, B - BGP
D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
E1 - OSPF external type 1, E2 - OSPF external type 2
i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
ia - IS-IS inter area, * - candidate default, U - per-user static route
o - ODR, P - periodic downloaded static route

Gateway of last resort is not set

 172.16.0.0/24 is subnetted, 2 subnets

C 172.16.12.0 is directly connected, FastEthernet0/0
C 172.16.13.0 is directly connected, FastEthernet0/1
10.0.0.0/32 is subnetted, 2 subnets
B 10.10.6.6 [20/0] via 172.16.13.3, 00:01:09
C 10.10.1.1 is directly connected, Loopback0

使用local-preference更改路由

更改R1配置:
R1(config)#ip prefix-list mei permit 10.10.6.6/32
R1(config)#route-map paul permit 10
R1(config-route-map)#match ip address prefix-list mei
R1(config-route-map)#set local-preference 200
R1(config)#route-map paul permit 99
R1(config-route-map)#router bgp 100
R1(config-router)#neighbor 172.16.13.3 remote-as 2345
R1(config-router)#neighbor 172.16.13.3 route-map mei in

R1(config-router)#do clear ip bgp * so
1(config-router)#do sh ip bgp 10.10.6.6/32
BGP routing table entry for 10.10.6.6/32, version 4
Paths: (2 available, best #1, table Default-IP-Routing-Table)
Flag: 0x840
Advertised to update-groups:
1
2345 600
172.16.13.3 from 172.16.13.3 (10.10.3.3)
Origin IGP, localpref 200, valid, external, best
2345 600

使用AS-path更改路由
更改R1配置
R1(config-route-map)#do sh run | s route-map
route-map paul permit 10
match ip address prefix-list mei
set as-path prepend 100 100
route-map paul permit 99

R1(config-router)#do sh run | s bgp
router bgp 100
no synchronization
bgp router-id 10.10.1.1
bgp log-neighbor-changes
network 10.10.1.1 mask 255.255.255.255
neighbor 172.16.12.2 remote-as 2345
neighbor 172.16.12.2 route-map paul in
neighbor 172.16.13.3 remote-as 2345
no auto-summary

R1(config-router)#do clear ip bgp * so
R1(config-router)#do sh ip bgp 10.10.6.6
BGP routing table entry for 10.10.6.6/32, version 2
Paths: (2 available, best #2, table Default-IP-Routing-Table)
Advertised to update-groups:
1
100 100 2345 600
172.16.12.2 from 172.16.12.2 (10.10.2.2)
Origin IGP, localpref 100, valid, external
2345 600
172.16.13.3 from 172.16.13.3 (10.10.3.3)
Origin IGP, localpref 100, valid, external, best

使用MED更改路由
更改R1配置
R1(config-router)#do sh run | s route-map
neighbor 172.16.12.2 route-map paul in
route-map paul permit 10
match ip address prefix-list mei
set metric 10000
route-map paul permit 99
R1(config-router)#do sh run | s bgp
router bgp 100
no synchronization
bgp router-id 10.10.1.1
bgp log-neighbor-changes
network 10.10.1.1 mask 255.255.255.255
neighbor 172.16.12.2 remote-as 2345
neighbor 172.16.12.2 route-map paul in
neighbor 172.16.13.3 remote-as 2345
no auto-summary

R1(config-router)#do clear ip bgp * soft
R1(config-router)#do sh ip bgp 10.10.6.6/32
BGP routing table entry for 10.10.6.6/32, version 2
Paths: (2 available, best #1, table Default-IP-Routing-Table)
Flag: 0x820
Advertised to update-groups:
1
2345 600
172.16.13.3 from 172.16.13.3 (10.10.3.3)
Origin IGP, localpref 100, valid, external, best
2345 600
172.16.12.2 from 172.16.12.2 (10.10.2.2)
Origin IGP, metric 10000, localpref 100, valid, external
R1(config-router)#