BGP 属性local_preference 本地优先
BGP 属性local_preference 本地优先
BGP 本地优先属性
BGP的本地优先属性是一个公认任意属性,主要用于当一条BGP路由器中存在多条去往同一目标网络的BGP路由时,帮助BGP协议筛选出最佳到达目标网络的通达路径。这个属性只在IBGP对等体之间进行交换,即在同一AS内使用,不会通告给AS域外。它主要用于判断流量离开AS时选择的最佳路由。
规则
- 在BGP优选路由的时候,排在最前面的是prefer value,即优选值,值越大才会越被优先选择,默认情况BGP路由的 prefer value 为0。
- 在prefer value 相同的情况下,进而选择local preference,即:本地优先,与prefer value 相同,值越大越容易被选择。
- local prefer 只在IBGP对等体进行传播。
- 当路由器从IBGP对等体接收路由的local pref 值为空时,那么会将此路由的local pref 设置为100,即:在IBGP对等体传递路由,默认值为100。
- 当路由器从IBGP对等体接收路由的local pref值不为空时,那么将此路由的local pref 进行保留,不做修改。
- 使用network通告或的import-route 引入的路由,local pref 值为空。
实验图
R1 的1.1.1.1/32 路由通告出去,在R4上进行学习,那么R4 学习到的1.1.1.1/32 的R1存在两条路径,这里通过本地优先来进行选路;R1 在AS100 内,与AS200 的R2、R3建立BGP邻居关系,R2、R3同时与R4 建立邻居关系。
测试点
- R2、R3 通告出去的路由(2.2.2.2/32与3.3.3.3/32)在IBGP邻居R4上默认为100:
[R4]dis bgp routing-table ipv4
Total number of routes: 7
BGP local router ID is 4.4.4.4
Status codes: * - valid, > - best, d - dampened, h - history,
s - suppressed, S - stale, i - internal, e - external
a - additional-path
Origin: i - IGP, e - EGP, ? - incomplete
Network NextHop MED LocPrf PrefVal Path/Ogn
* >i 1.1.1.1/32 13.1.1.1 0 100 0 100i
* i 12.1.1.1 0 100 0 100i
* >i 2.2.2.2/32 2.2.2.2 0 100 0 i
* >i 3.3.3.3/32 3.3.3.3 0 100 0 i
* > 4.4.4.4/32 127.0.0.1 0 32768 i
* >i 12.1.1.0/24 2.2.2.2 0 100 0 i
* >i 13.1.1.0/24 3.3.3.3 0 100 0 i
通过上面R4 BGP路由信息表查看到,经过IBGP邻居传递过来的路由,local pref 均被设置为100,这就是由于R4 的IBGP邻居R2、R3未对路由进行设置local pref 属性值,所以传递过来后会被设置为默认的100。
- R1 通告的路由(1.1.1.1/32)本地为空,在R1 的EBGP 邻居R2、R3上接收后,记录BGP路由local pref 为空:
[R2]dis bgp routing-table ipv4
Total number of routes: 4
BGP local router ID is 2.2.2.2
Status codes: * - valid, > - best, d - dampened, h - history,
s - suppressed, S - stale, i - internal, e - external
a - additional-path
Origin: i - IGP, e - EGP, ? - incomplete
Network NextHop MED LocPrf PrefVal Path/Ogn
* >e 1.1.1.1/32 12.1.1.1 0 0 100i
* > 2.2.2.2/32 127.0.0.1 0 32768 i
* >i 4.4.4.4/32 4.4.4.4 0 100 0 i
* > 12.1.1.0/24 12.1.1.2 0 32768 i
通过R2 路由器查看BGP路由表,发现通过EBGP邻居学习来的路由的Local pref 值为空,所以符合上面说的,因为EBGP 邻居之间是不传递local pref 属性的;R3 是同样的道理,这里不进行描述。
- R3 上设置整体路由器的默认本地优先为200,那么经过R3传递到的R4 后路由为200:
[R4]dis bgp routing-table ipv4
Total number of routes: 7
BGP local router ID is 4.4.4.4
Status codes: * - valid, > - best, d - dampened, h - history,
s - suppressed, S - stale, i - internal, e - external
a - additional-path
Origin: i - IGP, e - EGP, ? - incomplete
Network NextHop MED LocPrf PrefVal Path/Ogn
* >i 1.1.1.1/32 13.1.1.1 0 200 0 100i
* i 12.1.1.1 0 100 0 100i
* >i 2.2.2.2/32 2.2.2.2 0 100 0 i
* >i 3.3.3.3/32 3.3.3.3 0 200 0 i
* > 4.4.4.4/32 127.0.0.1 0 32768 i
* >i 12.1.1.0/24 2.2.2.2 0 100 0 i
* >i 13.1.1.0/24 3.3.3.3 0 200 0 i
通过查看R4 路由器的BGP路由表,发现凡是经过R3 路由器传递来的路由,现在都变成了200;那么符合之前的规律:路由器设置了本地优先值后,传递到IBGP邻居后不会对本地优先属性值进行改变。
- route policy 设置指定路由的本地优先值:
在地址族里进行设置本地优先值,这里的本地优先的作用要大于在地址族内设置的默认本地优先值;即: route-policy 设置的本地优先值要大于 default 设置的本地优先值;
在R3 上配置route policy ,ACL 并在export 进行应用:
配置:
acl basic name acl2000
rule 5 permit source 1.1.1.1 0 # 设置ACL 来筛选1.1.1.1 的路由;
#
route-policy rp1 permit node 10
if-match ip address acl name acl2000
apply local-preference 300
#
route-policy rp1 permit node 11
apply local-preference 99
#
address-family ipv4 unicast
default local-preference 200
network 3.3.3.3 255.255.255.255
network 13.1.1.0 255.255.255.0
peer 4.4.4.4 enable
peer 4.4.4.4 route-policy rp1 export
peer 13.1.1.1 enable
在这里我们查看R3 路由器的路由记录:
[R3]dis bgp routing-table ipv4
Total number of routes: 4
BGP local router ID is 3.3.3.3
Status codes: * - valid, > - best, d - dampened, h - history,
s - suppressed, S - stale, i - internal, e - external
a - additional-path
Origin: i - IGP, e - EGP, ? - incomplete
Network NextHop MED LocPrf PrefVal Path/Ogn
* >e 1.1.1.1/32 13.1.1.1 0 0 100i
* > 3.3.3.3/32 127.0.0.1 0 200 32768 i
* >i 4.4.4.4/32 4.4.4.4 0 100 0 i
* > 13.1.1.0/24 13.1.1.3 0 200 32768 i
通过这里的路由信息,清楚的看到,default local prefer 值设置为了200;
那么再查看从本机发送出去的R4 路由器的路由:
[R4]dis bgp routing-table ipv4
Total number of routes: 7
BGP local router ID is 4.4.4.4
Status codes: * - valid, > - best, d - dampened, h - history,
s - suppressed, S - stale, i - internal, e - external
a - additional-path
Origin: i - IGP, e - EGP, ? - incomplete
Network NextHop MED LocPrf PrefVal Path/Ogn
* >i 1.1.1.1/32 13.1.1.1 0 300 0 100i
* i 12.1.1.1 0 100 0 100i
* >i 2.2.2.2/32 2.2.2.2 0 100 0 i
* >i 3.3.3.3/32 3.3.3.3 0 99 0 i
* > 4.4.4.4/32 127.0.0.1 0 32768 i
* >i 12.1.1.0/24 2.2.2.2 0 100 0 i
* >i 13.1.1.0/24 3.3.3.3 0 99 0 i
上面的路由可以看到1.1.1.1/32 的路由经过R3 来的,其local pref 值已经设置为了300,即route policy 已经生效;非1.1.1.1/32 的路由,经过R3传递过来的路由被route policy 设置后,在R4上被设置为99;而经过R2路由器来的路由,仍然是默认的100。
路由器BGP配置
R1
interface LoopBack0
ip address 1.1.1.1 255.255.255.255
#
interface GigabitEthernet0/0/0
port link-mode route
combo enable copper
ip address 12.1.1.1 255.255.255.0
#
interface GigabitEthernet0/0/1
port link-mode route
combo enable copper
ip address 13.1.1.1 255.255.255.0
#
bgp 100
peer 12.1.1.2 as-number 200
peer 13.1.1.3 as-number 200
#
address-family ipv4 unicast
network 1.1.1.1 255.255.255.255
peer 12.1.1.2 enable
peer 13.1.1.3 enable
R2
interface LoopBack0
ip address 2.2.2.2 255.255.255.255
#
interface GigabitEthernet0/0/0
port link-mode route
combo enable copper
ip address 12.1.1.2 255.255.255.0
#
interface GigabitEthernet0/0/1
port link-mode route
combo enable copper
ip address 24.1.1.2 255.255.255.0
#
bgp 200
peer 4.4.4.4 as-number 200
peer 4.4.4.4 connect-interface LoopBack0
peer 12.1.1.1 as-number 100
#
address-family ipv4 unicast
network 2.2.2.2 255.255.255.255
network 12.1.1.0 255.255.255.0
peer 4.4.4.4 enable
peer 12.1.1.1 enable
#
ip route-static 4.4.4.4 32 24.1.1.4
R3
interface LoopBack0
ip address 3.3.3.3 255.255.255.255
#
interface GigabitEthernet0/0/0
port link-mode route
combo enable copper
ip address 13.1.1.3 255.255.255.0
#
interface GigabitEthernet0/0/1
port link-mode route
combo enable copper
ip address 34.1.1.3 255.255.255.0
#
bgp 200
peer 4.4.4.4 as-number 200
peer 4.4.4.4 connect-interface LoopBack0
peer 13.1.1.1 as-number 100
#
address-family ipv4 unicast
network 3.3.3.3 255.255.255.255
network 13.1.1.0 255.255.255.0
peer 4.4.4.4 enable
peer 13.1.1.1 enable
#
ip route-static 4.4.4.4 32 34.1.1.4
R4
interface LoopBack0
ip address 4.4.4.4 255.255.255.255
#
interface GigabitEthernet0/0/0
port link-mode route
combo enable copper
ip address 24.1.1.4 255.255.255.0
#
interface GigabitEthernet0/0/1
port link-mode route
combo enable copper
ip address 34.1.1.4 255.255.255.0
#
bgp 200
peer 2.2.2.2 as-number 200
peer 2.2.2.2 connect-interface LoopBack0
peer 3.3.3.3 as-number 200
peer 3.3.3.3 connect-interface LoopBack0
#
address-family ipv4 unicast
network 4.4.4.4 255.255.255.255
peer 2.2.2.2 enable
peer 3.3.3.3 enable
#
ip route-static 2.2.2.2 32 24.1.1.2
ip route-static 3.3.3.3 32 34.1.1.3