示例:配置策略子例程
此示例演示如何在路由策略匹配条件中使用策略子例程。
要求
配置此示例之前,不需要除设备初始化之外的特殊配置。
概述
在设备 R1 上,配置了名为的策略 main
。
user@R1# show policy-options policy-statement main { term subroutine-as-a-match { from policy subroutine; then accept; } term nothing-else { then reject; } }
此主策略调用名为 subroutine
的子例程。
user@R1# show policy-options policy-statement subroutine { term get-routes { from protocol static; then accept; } term nothing-else { then reject; } }
路由器以定义的方式评估 的 main
逻辑。的 from policy subroutine
匹配标准允许路由设备找到子例程。按照正常的策略处理规则,按顺序评估子例程的所有术语。在此示例中,路由表中的所有静态路由都将子例程与 accept 操作匹配。这会将真实结果返回到原始或调用策略,通知设备发生了正匹配。将执行调用策略中的操作并接受路由。路由表中的所有其他路由都与子例程不匹配,并向调用策略返回错误结果。设备评估路由的 main
第二项并拒绝路由。
子例程中的操作实际上不会接受或拒绝特定路由。子例程操作仅转换为真或假结果。但是,无论子例程的结果如何,修改路由属性的操作都会应用于路由。
AS 64510 中的设备 R1 有多个客户路由,其中一些是本地配置的静态路由,还有一些通过内部 BGP (IBGP) 从设备 R2 和设备 R3 接收。AS 64510 连接到 AS 64511 中的设备 R4。该策略 main
将作为导出策略应用到设备 R1 与设备 R4 的 BGP 对等会话中。这会导致设备 R1 仅将自己的静态路由发送到设备 R4。由于策略 main
的原因,设备 R1 不会发送从其内部对等方设备 R2 和设备 R3 接收的路由。
使用策略子例程时,请务必记住,默认的 EBGP 导出策略是将所有学习的 BGP 路由播发到所有 EBGP 对等方。此默认策略在主策略和子例程中有效。因此,如此示例所示,如果您不希望默认 EBGP 导出策略生效,则必须在主策略和策略子例程中将终止操作配置为 then reject
最终术语。此示例演示了当主策略或策略子例程中缺少最后一个 then reject
术语时会发生什么情况。
配置
CLI 快速配置
要快速配置此示例,请复制以下命令,将其粘贴到文本文件中,删除所有换行符,更改与您的网络配置匹配所需的任何详细信息,然后将命令复制并粘贴到层次结构级别的 CLI [edit]
中。
设备 R1
set interfaces fe-1/2/0 unit 0 description to_R2 set interfaces fe-1/2/0 unit 0 family inet address 10.0.0.1/30 set interfaces fe-1/2/2 unit 0 description to_R3 set interfaces fe-1/2/2 unit 0 family inet address 10.0.0.5/30 set interfaces fe-1/2/3 unit 0 description to_R4 set interfaces fe-1/2/3 unit 0 family inet address 10.1.0.5/30 set interfaces lo0 unit 0 family inet address 192.168.0.1/32 set protocols bgp group int type internal set protocols bgp group int local-address 192.168.0.1 set protocols bgp group int neighbor 192.168.0.2 set protocols bgp group int neighbor 192.168.0.3 set protocols bgp group to_64511 type external set protocols bgp group to_64511 export main set protocols bgp group to_64511 neighbor 10.1.0.6 peer-as 64511 set protocols ospf area 0.0.0.0 interface fe-1/2/0.0 set protocols ospf area 0.0.0.0 interface fe-1/2/2.0 set protocols ospf area 0.0.0.0 interface lo0.0 passive set policy-options policy-statement main term subroutine-as-a-match from policy subroutine set policy-options policy-statement main term subroutine-as-a-match then accept set policy-options policy-statement main term nothing-else then reject set policy-options policy-statement subroutine term get-routes from protocol static set policy-options policy-statement subroutine term get-routes then accept set policy-options policy-statement subroutine term nothing-else then reject set routing-options static route 172.16.1.16/28 discard set routing-options static route 172.16.1.32/28 discard set routing-options static route 172.16.1.48/28 discard set routing-options static route 172.16.1.64/28 discard set routing-options router-id 192.168.0.1 set routing-options autonomous-system 64510
设备 R2
set interfaces fe-1/2/0 unit 0 description to_R1 set interfaces fe-1/2/0 unit 0 family inet address 10.0.0.2/30 set interfaces fe-1/2/1 unit 0 description to_R3 set interfaces fe-1/2/1 unit 0 family inet address 10.1.0.1/30 set interfaces lo0 unit 0 family inet address 192.168.0.2/32 set protocols bgp group int type internal set protocols bgp group int local-address 192.168.0.2 set protocols bgp group int neighbor 192.168.0.1 export send-static set protocols bgp group int neighbor 192.168.0.3 set protocols ospf area 0.0.0.0 interface fe-1/2/0.0 set protocols ospf area 0.0.0.0 interface fe-1/2/1.0 set protocols ospf area 0.0.0.0 interface lo0.0 passive set policy-options policy-statement send-static term 1 from protocol static set policy-options policy-statement send-static term 1 then accept set routing-options static route 172.16.2.16/28 discard set routing-options static route 172.16.2.32/28 discard set routing-options static route 172.16.2.48/28 discard set routing-options static route 172.16.2.64/28 discard set routing-options router-id 192.168.0.2 set routing-options autonomous-system 64510
设备 R3
set interfaces fe-1/2/1 unit 0 description to_R2 set interfaces fe-1/2/1 unit 0 family inet address 10.1.0.2/30 set interfaces fe-1/2/2 unit 0 description to_R1 set interfaces fe-1/2/2 unit 0 family inet address 10.0.0.6/30 set interfaces lo0 unit 0 family inet address 192.168.0.3/32 set protocols bgp group int type internal set protocols bgp group int local-address 192.168.0.3 set protocols bgp group int neighbor 192.168.0.1 export send-static set protocols bgp group int neighbor 192.168.0.2 set protocols ospf area 0.0.0.0 interface fe-1/2/2.6 set protocols ospf area 0.0.0.0 interface fe-1/2/0.4 set protocols ospf area 0.0.0.0 interface lo0.0 passive set policy-options policy-statement send-static from protocol static set policy-options policy-statement send-static then accept set routing-options static route 172.16.3.16/28 discard set routing-options static route 172.16.3.32/28 discard set routing-options static route 172.16.3.48/28 discard set routing-options static route 172.16.3.64/28 discard set routing-options router-id 192.168.0.3 set routing-options autonomous-system 64510
设备 R4
set interfaces fe-1/2/3 unit 0 description to_R1 set interfaces fe-1/2/3 unit 0 family inet address 10.1.0.6/30 set interfaces lo0 unit 0 family inet address 192.168.0.4/32 set protocols bgp group ext type external set protocols bgp group ext peer-as 64510 set protocols bgp group ext neighbor 10.1.0.5 set routing-options autonomous-system 64511
程序
分步过程
以下示例要求您在配置层次结构中导航各个级别。有关导航 CLI 的信息,请参阅 在配置模式下使用 CLI 编辑器Junos OS CLI 用户指南。
要配置设备 R1:
配置设备接口。
[edit interfaces] user@R1# set fe-1/2/0 unit 0 description to_R2 user@R1# set fe-1/2/0 unit 0 family inet address 10.0.0.1/30 user@R1# set fe-1/2/2 unit 0 description to_R3 user@R1# set fe-1/2/2 unit 0 family inet address 10.0.0.5/30 user@R1# set fe-1/2/3 unit 0 description to_R4 user@R1# set fe-1/2/3 unit 0 family inet address 10.1.0.5/30 user@R1# set lo0 unit 0 family inet address 192.168.0.1/32
配置与设备 R2 和设备 R3 的内部 BGP (IBGP) 连接。
[edit protocols bgp group int] user@R1# set type internal user@R1# set local-address 192.168.0.1 user@R1# set neighbor 192.168.0.2 user@R1# set neighbor 192.168.0.3
配置与设备 R4 的 EBGP 连接。
[edit protocols bgp group to_64511] user@R1# set type external user@R1# set export main user@R1# set neighbor 10.1.0.6 peer-as 64511
配置与设备 R2 和设备 R3 的 OSPF 连接。
[edit protocols ospf area 0.0.0.0] user@R1# set interface fe-1/2/0.0 user@R1# set interface fe-1/2/2.0 user@R1# set interface lo0.0 passive
配置策略
main
。[edit policy-options policy-statement main term subroutine-as-a-match] user@R1# set from policy subroutine user@R1# set then accept [edit policy-options policy-statement main term nothing-else] user@R1# set then reject
配置策略
subroutine
。[edit policy-options policy-statement subroutine term get-routes] user@R1# set from protocol static user@R1# set then accept [edit policy-options policy-statement subroutine term nothing-else] user@R1# set then reject
配置到 172.16.5.0/24 网络的静态路由。
[edit routing-options static] user@R1# set route 172.16.1.16/28 discard user@R1# set route 172.16.1.32/28 discard user@R1# set route 172.16.1.48/28 discard user@R1# set route 172.16.1.64/28 discard
配置自治系统 (AS) 编号和路由器 ID。
[edit routing-options] user@R1# set router-id 192.168.0.1 user@R1# set autonomous-system 64510
结果
在配置模式下,输入 show interfaces
、show protocols
、show policy-options
和 show routing-options
命令,以确认您的配置。如果输出未显示预期的配置,请重复此示例中的说明,以便进行更正。
user@R1# show interfaces fe-1/2/0 { unit 0 { description to_R2; family inet { address 10.0.0.1/30; } } } fe-1/2/2 { unit 0 { description to_R3; family inet { address 10.0.0.5/30; } } } fe-1/2/3 { unit 0 { description to_R4; family inet { address 10.1.0.5/30; } } } lo0 { unit 0 { family inet { address 192.168.0.1/32; } } }
user@R1# show protocols bgp { group int { type internal; local-address 192.168.0.1; neighbor 192.168.0.2; neighbor 192.168.0.3; } group to_64511 { type external; export main; neighbor 10.1.0.6 { peer-as 64511; } } } ospf { area 0.0.0.0 { interface fe-1/2/0.0; interface fe-1/2/2.0; interface lo0.0 { passive; } } }
user@R1# show policy-options policy-statement main { term subroutine-as-a-match { from policy subroutine; then accept; } term nothing-else { then reject; } } policy-statement subroutine { term get-routes { from protocol static; then accept; } term nothing-else { then reject; } }
user@R1# show routing-options static { route 172.6.1.16/28 discard; route 172.6.1.32/28 discard; route 172.6.1.48/28 discard; route 172.6.1.64/28 discard; } router-id 192.168.0.1; autonomous-system 64510;
如果完成设备配置,请从配置模式输入 commit
。
验证
确认配置工作正常。
验证设备 R1 上的路由
目的
在设备 R1 上,检查路由表中的静态路由。
操作
user@R1> show route protocol static inet.0: 23 destinations, 23 routes (23 active, 0 holddown, 0 hidden) + = Active Route, - = Last Active, * = Both 172.16.1.16/28 *[Static/5] 1d 02:02:13 Discard 172.16.1.32/28 *[Static/5] 1d 02:02:13 Discard 172.16.1.48/28 *[Static/5] 1d 02:02:13 Discard 172.16.1.64/28 *[Static/5] 1d 02:02:13 Discard
意义
设备 R1 有四个静态路由。
验证发往设备 R4 的路由播发
目的
在设备 R1 上,确保静态路由已播发到设备 R4。
操作
user@R1> show route advertising-protocol bgp 10.1.0.6 inet.0: 23 destinations, 23 routes (23 active, 0 holddown, 0 hidden) Prefix Nexthop MED Lclpref AS path * 172.16.1.16/28 Self I * 172.16.1.32/28 Self I * 172.16.1.48/28 Self I * 172.16.1.64/28 Self I
意义
正如预期的那样,设备 R1 仅将其静态路由通告给设备 R4。
试验默认 BGP 导出策略
目的
查看从策略main
或策略subroutine
中删除最终then reject
术语时会发生什么。
操作
在设备 R1 上,停用策略
main
中的最后一个期限。[edit policy-options policy-statement main] user@R1# deactivate term nothing-else user@R1# commit
在设备 R1 上,检查哪些路由播发到设备 R4。
user@R1> show route advertising-protocol bgp 10.1.0.6 inet.0: 23 destinations, 23 routes (23 active, 0 holddown, 0 hidden) Prefix Nexthop MED Lclpref AS path * 172.16.1.16/28 Self I * 172.16.1.32/28 Self I * 172.16.1.48/28 Self I * 172.16.1.64/28 Self I * 172.16.2.16/28 Self I * 172.16.2.32/28 Self I * 172.16.2.48/28 Self I * 172.16.2.64/28 Self I * 172.16.3.16/28 Self I * 172.16.3.32/28 Self I * 172.16.3.48/28 Self I * 172.16.3.64/28 Self I
现在,来自设备 R1 的所有 BGP 路由都将发送到设备 R4。这是因为在处理返回到策略
main
后,默认 BGP 导出策略生效。在设备 R1 上,重新激活策略
main
中的最终期限,然后停用策略subroutine
中的最终期限。[edit policy-options policy-statement main] user@R1# activate term nothing-else [edit policy-options policy-statement subroutine] user@R1# deactivate term nothing-else user@R1# commit
在设备 R1 上,检查哪些路由播发到设备 R4。
user@R1> show route advertising-protocol bgp 10.1.0.6 inet.0: 23 destinations, 23 routes (23 active, 0 holddown, 0 hidden) Prefix Nexthop MED Lclpref AS path * 172.16.1.16/28 Self I * 172.16.1.32/28 Self I * 172.16.1.48/28 Self I * 172.16.1.64/28 Self I * 172.16.2.16/28 Self I * 172.16.2.32/28 Self I * 172.16.2.48/28 Self I * 172.16.2.64/28 Self I * 172.16.3.16/28 Self I * 172.16.3.32/28 Self I * 172.16.3.48/28 Self I * 172.16.3.64/28 Self I
现在,来自设备 R1 的所有 BGP 路由都将发送到设备 R4。这是因为在处理返回到策略
main
之前,默认的BGP导出策略在策略subroutine
中生效。
意义
要防止默认 BGP 导出策略生效,必须在主策略和所有引用的子例程中包含最终 then reject
术语。