说明:
1:该笔记作用与 BGP 选路规则属性参考,同时也兼顾 BGP 路由策略参考
2:该笔记所作实验拓扑图用于 BGP 通用实验拓扑图,配置也相同,可直接参考该笔记.md 快速建立即可
参考:红茶三杯
该文章可能存在的理解错误改正:BGP 路由的等价负载均衡该概念也是与运维服务中的等价负载均衡概念相同,是一种优化数据,流量的方式,关键点在于:多个数据包实现数据包拆分流,组成的一个过程
前置
该 BGP 选路规则存在大概十三条,每个规则都代表着 BGP 的路由策略分布部署方案的变化,
所以 BGP 选路规则就像 OSPF 中的 LSA 一样重要,每一条都要去理解,实验,总结
以下为 BGP 的十三条选路规则参考:
其中我们也没必要去衡量如果一致该怎么比,BGP 选路完全按照该表匹配,优先比较 Weight 值,若相等则持续往下比较。
0x01:优选具有最大 Weight 值的路由
我们先来回顾以下 BGP 的 Weight 属性
1:Weight 属性回顾
Cisco 私有,越大越优先
作用范围是本地路由器,发送 update 包中不能够携带 weight 属性,如果基于 weight 去部署路由策略,则只能在本地部署,因为 weight 只能影响自己,而不够传递给任何 BGP Peer
范围 0-65535
如果路由是从其他 Peer 学过来的则 (在本地路由的 WT) 默认值为 0
若为本地 network 产生,重发布直连,静态路由禅僧,本地汇总产生则为 32768
2:基本操作 Weight 值命令参考 (不常用)
该命令会修改从特定 Peer 收到的所有路由的权重 (相当于,不能路由分流)
Router(config-router)# neighbor ip-address weight weight-value
参考图如下:
3:Route-map 操作 weight 值命令参考 ?(常用)
我们通过 route-map 可以匹配我们想要的路由,从而部署该 weight 路由选路,可以使得流量分流
0x02 优选具有最大 Local-Preference 值的路由
1:LP 属性回顾
LP 只能在本地 AS 内传播
本地始发路由 LP 值为 100
BGP 路由器在向其 EBGP 邻居发送路由跟新的时候,不能携带 LP 属性,但是对方会在本地为这条路由附上一个默认值 100,如果有 IBGP Peer 则传递给 IBGP Peer,若为 EBGP 则不携带传递
2:基本操作 LP 属性值命令参考 (不常用)
bgp default local-preference ?!! 修改 LP 默认值
3:Route-map 操作 LP 值命令参考 (常用)
0x03 优选起源于本地的路由
1:解析
优选起源于本地的路由即本地 network aggregate 或 redistribute 宣告的路由,均为起源于本地
通常起源本地的路由有一个特点:Next-hop = 0.0.0.0
2:实验
3:配置命令与验证
!! R2 router bgp 234 network 8.0.0.0 mask 255.0.0.0 ip route 8.0.0.0 255.0.0.0 Null0 ------------------------------------------------------------------------------------- !! R3 router bgp 234 network 8.0.0.0 route-map test ip route 8.0.0.0 255.0.0.0 Null0 route-map test permit 10 set weight 0
验证如下:
R3#show ip bgp BGP table version is 23, 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 * i8.0.0.0 2.2.2.2 0 100 0 i *> 0.0.0.0 0 0 i
0x04 优选 AS-Path 最短的路由
1:基础回顾
AS-path 只在穿越 EBGP Peer 之间会改变,用于防环,
若处在 AS 域内则 AS-path 属性不会改变,则 AS-Path 防环失败,所以有了水平分割。
需要注意的是:基于 AS-Path 的路由策略部署,其一般建立在 EBGP 邻居之间
2:实验
3:验证
R2#show ip bgp BGP table version is 11, local router ID is 2.2.2.2 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 *> 10.1.1.0/24 0.0.0.0 0 32768 i * 10.1.1.1 0 0 100 i *>i40.1.1.0/24 4.4.4.4 0 100 0 i *> 100.100.100.0/24 10.1.1.1 0 0 100 i *>i200.200.200.0 4.4.4.4 0 100 0 500 i * 10.1.1.1 0 0 100 888 i
4:AS-SET 路径选择补充
在做聚合路由时,使用 AS-SET 关键字后产生的 AS-Path {} 无序列表,不管里面的 AS 有多长,们都算它为一个 AS 长度。
关于下图所谓 EBGP 最老优先,则谁先发送给的 BGP Update 包过来,谁优先
5:AS-Confederation_Sequence 路径选择补充
联邦 AS-Path
注意联邦内的 AS-Confer 时忽略不计的 AS-Path 长度
0x05 Origin 路径优选 ( i -> e -> ?)
关于 Origin BGP 属性,其可以翻译为一个源:
1:由 Network 声明为 i
2:由 EGP 声明为 e
3:由 redistribute 为 ?
1:实验
我们可以将 Origin 比作一个引入路由的方式,其优先级为、为 (i -> e -> ?)
关键命令如下
R2(config-route-map)#set origin ? egp remote EGP igp local IGP incomplete unknown heritage
以上方法采用 Route-map 的方法,当然我们也可以根据 Origin 本身的规律去操作
比如重发布操作 Origin。
不过我们不推荐操作 Origin 值取影响 BGP 路由决策或数据走向
0x06 优选 MED (metric) 最小的路由 ?
1:在默认情况下仅有当所有的备选路由来自同一 AS 才会比较 MED 值
2:MED 为可选非传递属性,值越小越优先,一般用于 AS 之间影响 BGP 路由决策,而非 IBGP 之间
1:MED 值属性回顾
将 IGP 路由引入 BGP 时关联 route-map 进行设置
对 BGP Peer 应用 IN/OUT 方向的 Route-map 进行设置
非 Route-map (自动) 方式:
使用 network 或 redistribute 方式将 IGP 路由引入 BGP 时,MED 值将继承 IGP 路由的 Metric (直连与静态路由 Metric 均为 0 )
使用手工汇总 aggregate-address 方式引入,则 MED 为 空
2:操作 MED 属性注意事项 ?
1:在默认情况下,只比较来自同一邻居 AS 的 BGP 路由 MED 值,就是说如果同一个目的的两条路由来自不同的 AS,则不进行 MED 值比较,如果仍然希望比较来自不同邻居 AS 的路由,可使用如下命令:
?
bgp always-conpare-med
2:MED 值是在直接相连的自治系统间影响业务量,而不会跨 AS 传递
MED 值比较操作 BGP 路径优选,一般 MED 值比较只会发生在 AS 内部,而外部来的 MED 不会被比较,如果非要比较,则必须键入上方 always-compare-med
代码激活
3:实验验证
R3#show ip bgp BGP table version is 16, 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 *>i10.1.1.0/24 2.2.2.2 0 100 0 i *>i40.1.1.0/24 4.4.4.4 0 100 0 i * i100.100.100.0/24 4.4.4.4 0 100 0 500 i *>i 2.2.2.2 0 100 0 100 i * i200.200.200.0 2.2.2.2 400 100 0 100 i *>i 4.4.4.4 10 100 0 500 i
0x07 优选 EBGP 邻居发来的路由
1:相对于 IBGP Peer 学过来的 路由,其优选 EBGP Peer 的
2:在联邦 EBGP 和 IBGP 中首选联邦 EBGP 路由
关于该比较,我们可以之间看实验拓扑中的 R2,R4 路由表即可
R4#show ip b 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 *>i10.1.1.0/24 2.2.2.2 0 100 0 i * 40.1.1.0/24 40.1.1.2 0 0 500 i *> 0.0.0.0 0 32768 i *> 100.100.100.0/24 40.1.1.2 0 0 500 i * i 2.2.2.2 0 100 0 100 i *> 200.200.200.0 40.1.1.2 0 0 500 i * i 2.2.2.2 0 100 0 100 i
参考图:
0x08 优选到 Next-hop IP IGP metric 值最小的路由
注:我从两个 BGP 邻居各收到一条路由,这两条 BGP 路由有相同的路由前缀,首先,这两条 BGP 路由的 Next-hop 是不相同的,否则不具有可比性,那么我比较本地到达这两个 Next-hop 的 IGP 度量值,谁 metric 小,我就选谁
1:前置
既然我们是比较 Next-hop 的 IGP Metric 值,首先必须先查看:
我们可以看到,二者的 OSPF Metric 均为 11,但最终 best 为 router-id 小的,因为比较到了 11 条
R3#show ip b 100.100.100.0 BGP routing table entry for 100.100.100.0/24, version 4 Paths: (2 available, best #1, table Default-IP-Routing-Table) Advertised to update-groups: 1 100, (Received from a RR-client) 2.2.2.2 (`metric 11`) from 2.2.2.2 (2.2.2.2) Origin IGP, metric 0, localpref 100, valid, internal, best 500 4.4.4.4 (`metric 11`) from 4.4.4.4 (4.4.4.4) Origin IGP, metric 0, localpref 100, valid, interna
2:实验与验证
既然该选路规则指定为 Next-hop IGP metric 值,那么我们又运行的 OSPF 链路状态路由协议,我们都知道, OSPF 是一个接口敏感型的动态路由协议
R3#show ip b 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 `*>i100.100.100.0/24 2.2.2.2 0 100 0 100 i` * i 4.4.4.4 0 100 0 500 i R3(config)#int e0/1 `R3(config-if)#ip ospf cost 1000` R3(config-if)#end R3#show ip b BGP table version is 5, 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 * i100.100.100.0/24 2.2.2.2 0 100 0 100 i `*>i 4.4.4.4 0 100 0 500 i`
3:扩展
0x09 BGP 等价负载均衡
注意:该特性其实并不算是一种 BGP 的路径优选规则,可以说是 BGP 的一种优化特性
1:基础知识
(1):当前面 8 条选路原则都无法选出优选路径时,如果在 BGP 进程下配置了 maximum-paths [ibgp] n
该 N 标识等价负载均衡路由条目最大容量几条路由的范围上限
那么将执行等价负载均衡,也就是将这些等价的 BGP 路径都放假 IP 路由表使用,目的是起到备份的作用,但是要注意,虽然这些路径在本地都使用了,但是最终只会又一条更新给 IBGP Peer 那就是 Best 路由,那么到底谁是 Best,这里就需要查看 bgp
表或者 cef
表
(2):具备等价负载均衡条件的候选路径必须满足以下条件 ?
我们在 Linux 服务中的等价负载均衡,即是为了分担压力而出现的一种特性;
而在 BGP 等价负载均衡,其实也存在这一概念说法,但是其必须具备条件
必须有相同的路径属性,Weight,LP,AS_Path (不仅是长度,整个 AS-PATH 包括 AS 号都需相同)、Origin code、MED、及 IGP 的 Distance 值
Next-hop 每一条路径的下一跳都不相同
2:命令参考
maximum-paths [ibgp] n ~[该 N 标识等价负载均衡路由条目最大容量几条路由的范围上限]
如果不关联 IBGP 关键字,那么只会对 external 路由执行等价负载均衡,也就是 EBGP 传过来的路由
如果要对 Internal [IBGP] 路由做负载均衡,就需要键入关键字 IBGP
如果不配置
maximum-paths
,那么将进行到下一条选路原则
3:实验与验证
R3(config-router)#maximum-paths ibgp 2 R3#show 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 2.0.0.0/32 is subnetted, 1 subnets O 2.2.2.2 [110/11] via 20.1.1.1, 00:00:01, Ethernet0/1 100.0.0.0/24 is subnetted, 1 subnets B 100.100.100.0 [200/0] via 4.4.4.4, 00:00:01 [200/0] via 2.2.2.2, 00:00:01 3.0.0.0/32 is subnetted, 1 subnets C 3.3.3.3 is directly connected, Loopback0 4.0.0.0/32 is subnetted, 1 subnets O 4.4.4.4 [110/11] via 30.1.1.2, 00:00:01, Ethernet0/0 20.0.0.0/24 is subnetted, 1 subnets C 20.1.1.0 is directly connected, Ethernet0/1 30.0.0.0/24 is subnetted, 1 subnets C 30.1.1.0 is directly connected, Ethernet0/0 ------------------------------------------------------------------------------------- R3#show ip b BGP table version is 9, 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 * i100.100.100.0/24 4.4.4.4 0 100 0 100 i *>i 2.2.2.2 0 100 0 100 i ------------------------------------------------------------------------------------- R3#show ip b 100.100.100.0 BGP routing table entry for 100.100.100.0/24, version 9 Paths: (2 available, best #2, table Default-IP-Routing-Table) Multipath: iBGP Flag: 0x800 Advertised to update-groups: 1 100 4.4.4.4 (metric 11) from 4.4.4.4 (4.4.4.4) Origin IGP, metric 0, localpref 100, valid, internal, multipath 100, (Received from a RR-client) 2.2.2.2 (metric 11) from 2.2.2.2 (2.2.2.2) Origin IGP, metric 0, localpref 100, valid, internal, multipath, best
4:扩展
0x10 优选最老的 EBGP Peer 传来的路由
该条 BGP 选路规则主要针对 EBGP 路由有效果,但是现在基本不使用该条来做 BGP 的路径选择操作,因为不确定性很大,会导致 BGP 网络震荡频繁
理解该选路规则:
其实就是看和哪个 EBGP Peer 感情深,交流时间最长果然路由世界也是人情世故啊
就是看 Up/Down
时间,哪个长,我用哪个~
R3#show ip b summary BGP router identifier 3.3.3.3, local AS number 234 BGP table version is 9, main routing table version 9 1 network entries using 117 bytes of memory 2 path entries using 104 bytes of memory 1 multipath network entries and 2 multipath paths 2/1 BGP path/bestpath attribute entries using 248 bytes of memory 1 BGP AS-PATH entries using 24 bytes of memory 0 BGP route-map cache entries using 0 bytes of memory 0 BGP filter-list cache entries using 0 bytes of memory BGP using 493 total bytes of memory BGP activity 1/0 prefixes, 5/3 paths, scan interval 60 secs Neighbor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd 2.2.2.2 4 234 96 100 9 0 0 01:08:46 1 4.4.4.4 4 234 96 100 9 0 0 01:12:04 1
1:实验与验证
R2#show ip b BGP table version is 2, local router ID is 20.1.1.1 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 * 100.100.100.0/24 20.1.1.2 0 0 300 i *> 10.1.1.1 0 0 100 i R2#clear ip b 10.1.1.1 R2# *Mar 1 00:07:45.531: %BGP-5-ADJCHANGE: neighbor 10.1.1.1 Down User reset *Mar 1 00:07:46.475: %BGP-5-ADJCHANGE: neighbor 10.1.1.1 Up R2#show ip b BGP table version is 3, local router ID is 20.1.1.1 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 * 100.100.100.0/24 10.1.1.1 0 0 100 i *> 20.1.1.2 0 0 300 i
R2#show ip b summary BGP router identifier 20.1.1.1, local AS number 200 BGP table version is 3, main routing table version 3 1 network entries using 117 bytes of memory 2 path entries using 104 bytes of memory 3/1 BGP path/bestpath attribute entries using 372 bytes of memory 2 BGP AS-PATH entries using 48 bytes of memory 0 BGP route-map cache entries using 0 bytes of memory 0 BGP filter-list cache entries using 0 bytes of memory BGP using 641 total bytes of memory BGP activity 1/0 prefixes, 3/1 paths, scan interval 60 secs Neighbor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd 10.1.1.1 4 100 21 24 3 0 0 00:01:20 1 20.1.1.2 4 300 13 14 3 0 0 00:04:27 1
??
在配置 bgp bestpath compare-routerid
命令后,将跳过该条比较规则,拥有最小 BGP routerID
的 Peer 将被作为路径优选
0x11 优选 Router-ID 最小的 BGP 邻居路由
??
在配置 bgp bestpath compare-routerid
命令后,将跳过第 10 条比较规则,拥有最小 BGP routerID
的 Peer 将被作为路径优选
1:实验与验证
R2#show ip b BGP table version is 2, local router ID is 2.2.2.2 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 * 100.100.100.0/24 20.1.1.2 0 0 300 i *> 10.1.1.1 0 0 100 i ------------------------------------------------------------------------------------- R2#show ip bgp 100.100.100.0 BGP routing table entry for 100.100.100.0/24, version 2 Paths: (2 available, best #2, table Default-IP-Routing-Table) Flag: 0x820 Advertised to update-groups: 1 300 20.1.1.2 from 20.1.1.2 (3.3.3.3) Origin IGP, metric 0, localpref 100, valid, external 100 10.1.1.1 from 10.1.1.1 (1.1.1.1) Origin IGP, metric 0, localpref 100, valid, external, best
2:扩展 ?
该扩展意义记住一点就可以:?
如果配置了路由反射器 (RR),且均为 IBGP Peer 对等体,那么反射过来的 Originator 将会替代 Router -ID,所以在路由反射器环境下规则 11 不生效。
0x12 优选 Cluster-List 最短的路由 (RR路由反射器环境存在)
我们学习第 11 条 BGP 路径选择规则中的扩展知识,是该 12 条规则的注意点
1:实验与验证
R3#show ip b 100.100.100.0 BGP routing table entry for 100.100.100.0/24, version 2 Paths: (2 available, best #2, table Default-IP-Routing-Table) Not advertised to any peer Local 1.1.1.1 (metric 11) from 2.2.2.2 (100.100.100.100) Origin IGP, metric 0, localpref 100, valid, internal `Originator: 1.1.1.1, Cluster list: 100.100.100.100` Local 1.1.1.1 (metric 11) from 1.1.1.1 (1.1.1.1) Origin IGP, metric 0, localpref 100, valid, internal, best
0x13 优选邻居 IP 地址最小
BGP neighbor 配置中的哪个地址,最小路由原则
1:实验与验证
R2#show ip b 100.100.100.0 BGP routing table entry for 100.100.100.0/24, version 3 Paths: (2 available, best #1, table Default-IP-Routing-Table) Not advertised to any peer Local 10.1.1.1 from 10.1.1.1 (100.100.100.100) Origin IGP, metric 0, localpref 100, valid, internal, best Local 20.1.1.2 from 20.1.1.2 (100.100.100.100) Origin IGP, metric 0, localpref 100, valid, internal