Skip to content

Commit

Permalink
Refactor default route and route redistribution templates (FRR, CL, E…
Browse files Browse the repository at this point in the history
…OS, IOS)
  • Loading branch information
ipspace committed Sep 5, 2024
1 parent 567b8c9 commit 7444e87
Show file tree
Hide file tree
Showing 25 changed files with 95 additions and 116 deletions.
11 changes: 2 additions & 9 deletions netsim/ansible/templates/bgp/eos.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% import "eos.macro.j2" as bgpcfg %}
{% import "templates/routing/_redistribute.eos.j2" as redistribute with context %}
!
{% set opts = namespace(nhs_ipv4=false,nhs_ipv6=false,intf=false) %}
{% for n in bgp.neighbors if n.type == 'ibgp' and bgp.next_hop_self|default(False) %}
Expand Down Expand Up @@ -59,15 +60,7 @@ router bgp {{ bgp.as }}
{% if bgp[af] is defined %}
!
address-family {{ af }}
{% if bgp.import is defined %}
!
{% for s_proto,s_data in bgp.import.items() %}
redistribute {{ s_proto }}{%
if s_proto == 'ospf' and af == 'ipv6' %}v3{% endif %}{%
if 'policy' in s_data %} route-map {{ s_data.policy }}-{{ af }}{% endif +%}
{% endfor %}
{% endif %}
!
{{ redistribute.config(bgp,af=af) }}
{% if loopback[af] is defined and bgp.advertise_loopback %}
network {{ loopback[af]|ipaddr('0') }}
{% endif %}
Expand Down
9 changes: 2 additions & 7 deletions netsim/ansible/templates/bgp/frr.j2
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% import "templates/routing/_redistribute.frr.j2" as redistribute with context %}
!
router bgp {{ bgp.as }}
no bgp ebgp-requires-policy
Expand Down Expand Up @@ -37,14 +38,8 @@ router bgp {{ bgp.as }}
#}
{% for af in ['ipv4','ipv6'] if bgp[af] is defined %}
address-family {{ af }} unicast
{% if bgp.import is defined %}
!
{% for s_proto,s_data in bgp.import.items() %}
redistribute {{ s_proto }}{%
if s_proto == 'ospf' and af == 'ipv6' %}6{% endif %}{%
if 'policy' in s_data %} route-map {{ s_data.policy }}-{{ af }}{% endif +%}
{% endfor %}
{% endif %}
{{ redistribute.config(bgp,af=af) }}
{% if loopback[af] is defined and bgp.advertise_loopback %}
!
network {{ loopback[af]|ipaddr(0) }}
Expand Down
10 changes: 2 additions & 8 deletions netsim/ansible/templates/bgp/ios.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% import "ios.macro.j2" as bgpcfg %}
{% import "templates/routing/_redistribute.ios.j2" as redistribute with context %}
{% set ospf_pid = ospf.process|default(1) %}
!
ip bgp-community new-format
Expand Down Expand Up @@ -27,14 +28,7 @@ router bgp {{ bgp.as }}
{% for af in ['ipv4','ipv6'] if bgp[af] is defined %}
address-family {{ af }}
bgp scan-time 5
{% if bgp.import is defined %}
!
{% for s_proto,s_data in bgp.import.items() %}
redistribute {{ s_proto }}{%
if s_proto == 'ospf' %} {{ ospf_pid }} match internal external{% endif %}{%
if 'policy' in s_data %} route-map {{ s_data.policy }}-{{ af }}{% endif +%}
{% endfor %}
{% endif %}
{{ redistribute.config(bgp,af=af,ospf_pid=ospf_pid) }}
!
{% if loopback[af] is defined and bgp.advertise_loopback %}
{{ bgpcfg.bgp_network(af,loopback[af]) }}
Expand Down
4 changes: 2 additions & 2 deletions netsim/ansible/templates/ospf/arubacx.ospfv2.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% import "ios.default.j2" as ospf_default %}

{% import "templates/routing/_default.ios.j2" as ospf_default %}
!
router ospf {{ pid }}
{% if ospf.router_id|ipv4 %}
router-id {{ ospf.router_id }}
Expand Down
4 changes: 2 additions & 2 deletions netsim/ansible/templates/ospf/arubacx.ospfv3.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% import "ios.default.j2" as ospf_default %}

{% import "templates/routing/_default.ios.j2" as ospf_default %}
!
router ospfv3 {{ pid }}
router-id {{ ospf.router_id }}
{% if ospf.reference_bandwidth is defined %}
Expand Down
10 changes: 4 additions & 6 deletions netsim/ansible/templates/ospf/cumulus.ospfv2.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{% import "ios.default.j2" as ospf_default %}
{% import "templates/routing/_default.ios.j2" as ospf_default %}
{% import "templates/routing/_redistribute.frr.j2" as redistribute with context %}
!
router ospf
{% if ospf.router_id|ipv4 %}
ospf router-id {{ ospf.router_id }}
Expand All @@ -8,16 +10,12 @@ router ospf
{% endfor %}
{% if ospf.reference_bandwidth is defined %}
auto-cost reference-bandwidth {{ ospf.reference_bandwidth }}
{% endif %}
{% if ospf.import is defined %}
{% for s_proto,s_data in ospf.import.items() %}
redistribute {{ s_proto }}{% if 'policy' in s_data %} route-map {{ s_data.policy }}-ipv4{% endif +%}
{% endfor %}
{% endif %}
timers throttle spf 10 50 500
timers throttle lsa all 100
timers lsa min-arrival 100
{{ ospf_default.config(ospf,'ipv4') }}
{{ redistribute.config(ospf,af='ipv4') }}
!
{% if 'ipv4' in loopback %}
interface {{ loopback.ifname }}
Expand Down
10 changes: 4 additions & 6 deletions netsim/ansible/templates/ospf/cumulus.ospfv3.j2
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
{% import "ios.default.j2" as ospf_default %}
{% import "templates/routing/_default.ios.j2" as ospf_default %}
{% import "templates/routing/_redistribute.frr.j2" as redistribute with context %}
!
router ospf6
ospf6 router-id {{ ospf.router_id }}
timers lsa min-arrival 100
timers throttle spf 10 50 500
{% if ospf.reference_bandwidth is defined %}
auto-cost reference-bandwidth {{ ospf.reference_bandwidth }}
{% endif %}
{% if ospf.import is defined %}
{% for s_proto,s_data in ospf.import.items() %}
redistribute {{ s_proto }}{% if 'policy' in s_data %} route-map {{ s_data.policy }}-ipv6{% endif +%}
{% endfor %}
{% endif %}
{{ redistribute.config(ospf,af='ipv6') }}
{{ ospf_default.config(ospf,'ipv6') }}
{% if 'ipv6' in loopback %}
interface {{ loopback.ifname }} area {{ ospf.area }}
Expand Down
12 changes: 4 additions & 8 deletions netsim/ansible/templates/ospf/eos.ospfv2.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{% import "ios.default.j2" as ospf_default %}
{% import "templates/routing/_default.ios.j2" as ospf_default %}
{% import "templates/routing/_redistribute.eos.j2" as redistribute with context %}
!
{% macro config(ospf_pid,ospf_vrf,ospf_data,intf_data,bgp={}) %}
!
! OSPFv2 configuration
Expand All @@ -19,13 +21,7 @@ router ospf {{ ospf_pid }}
timers lsa rx min interval 100
timers lsa tx delay initial 100 200 500
{{ ospf_default.config(ospf_data,'ipv4') }}
{% if ospf_data.import is defined %}
{% for s_proto,s_data in ospf_data.import.items() %}
redistribute {{ s_proto }}{%
if s_proto == 'bgp' and ospf_vrf %} include leaked{% endif %}{%
if 'policy' in s_data %} route-map {{ s_data.policy }}-ipv4{% endif +%}
{% endfor %}
{% endif %}
{{ redistribute.config(ospf_data,af='ipv4',vrf=ospf_vrf) }}
{% if ospf_data.reference_bandwidth is defined %}
auto-cost reference-bandwidth {{ ospf_data.reference_bandwidth }}
{% endif %}
Expand Down
12 changes: 4 additions & 8 deletions netsim/ansible/templates/ospf/eos.ospfv3.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{% import "ios.default.j2" as ospf_default %}
{% import "templates/routing/_default.ios.j2" as ospf_default %}
{% import "templates/routing/_redistribute.eos.j2" as redistribute with context %}
!
{% macro config(ospf_pid,ospf_vrf,ospf_data,intf_data,bgp={}) %}
!
! OSPFv3 configuration
Expand Down Expand Up @@ -36,13 +38,7 @@ ipv6 router ospf {{ ospf_pid }}
auto-cost reference-bandwidth {{ ospf_data.reference_bandwidth }}
{% endif %}
{{ ospf_default.config(ospf_data,'ipv6') }}
{% if ospf_data.import is defined %}
{% for s_proto,s_data in ospf_data.import.items() %}
redistribute {{ s_proto }}{%
if s_proto == 'bgp' and ospf_vrf %} include leaked{% endif %}{%
if 'policy' in s_data %} route-map {{ s_data.policy }}-ipv6{% endif +%}
{% endfor %}
{% endif %}
{{ redistribute.config(ospf_data,af='ipv6',vrf=ospf_vrf) }}
{% for l in intf_data if l.ospf.passive|default(False) %}
passive-interface {{ l.ifname }}
{% endfor %}
Expand Down
9 changes: 3 additions & 6 deletions netsim/ansible/templates/ospf/frr.ospfv2.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% import "ios.default.j2" as ospf_default %}
{% import "templates/routing/_default.ios.j2" as ospf_default %}
{% import "templates/routing/_redistribute.frr.j2" as redistribute with context %}
{% macro config(ospf_pid,ospf_vrf,ospf_data,intf_data,bgp={}) %}
!
! OSPFv2 FRR configuration
Expand All @@ -18,11 +19,7 @@ router ospf
timers throttle lsa all 100
timers lsa min-arrival 100
{{ ospf_default.config(ospf_data,'ipv4') }}
{% if ospf_data.import is defined %}
{% for s_proto,s_data in ospf_data.import.items() %}
redistribute {{ s_proto }}{% if 'policy' in s_data %} route-map {{ s_data.policy }}-ipv4{% endif +%}
{% endfor %}
{% endif %}
{{ redistribute.config(ospf_data,af='ipv4') }}
exit
!
{% for l in intf_data if 'ospf' in l and 'ipv4' in l %}
Expand Down
9 changes: 3 additions & 6 deletions netsim/ansible/templates/ospf/frr.ospfv3.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% import "ios.default.j2" as ospf_default %}
{% import "templates/routing/_default.ios.j2" as ospf_default %}
{% import "templates/routing/_redistribute.frr.j2" as redistribute with context %}
{% macro config(ospf_pid,ospf_vrf,ospf_data,intf_data,bgp={}) %}
!
! OSPFv3 FRR configuration
Expand Down Expand Up @@ -36,11 +37,7 @@ router ospf6
timers lsa min-arrival 100
timers throttle spf 10 50 500
{{ ospf_default.config(ospf_data,'ipv6') }}
{% if ospf_data.import is defined %}
{% for s_proto,s_data in ospf_data.import.items() %}
redistribute {{ s_proto }}{% if 'policy' in s_data %} route-map {{ s_data.policy }}-ipv6{% endif +%}
{% endfor %}
{% endif %}
{{ redistribute.config(ospf_data,af='ipv6') }}
{% if ospf_data.reference_bandwidth is defined %}
auto-cost reference-bandwidth {{ ospf_data.reference_bandwidth }}
{% endif %}
Expand Down
4 changes: 2 additions & 2 deletions netsim/ansible/templates/ospf/ios.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% import "ios.ospfv2.j2" as ospfv2 %}
{% import "ios.ospfv3.j2" as ospfv3 %}
{% import "ios.ospfv2.j2" as ospfv2 with context %}
{% import "ios.ospfv3.j2" as ospfv3 with context %}
{% set ospf_pid = ospf.process|default(1) %}
{% if ospf.af.ipv4 is defined %}
{{ ospfv2.config(ospf_pid,False,ospf,netlab_interfaces,bgp) }}
Expand Down
12 changes: 4 additions & 8 deletions netsim/ansible/templates/ospf/ios.ospfv2.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{% import "ios.default.j2" as ospf_default %}
{% import "templates/routing/_default.ios.j2" as ospf_default %}
!
{% macro config(ospf_pid,ospf_vrf,ospf_data,intf_data,bgp={}) %}
{% import "templates/routing/_redistribute.ios.j2" as redistribute with context %}
{% if ospf_vrf %}
router ospf {{ ospf_pid }} vrf {{ ospf_vrf }}
{% else %}
Expand All @@ -15,13 +17,7 @@ router ospf {{ ospf_pid }}
passive-interface {{ l.ifname }}
{% endfor %}
{{ ospf_default.config(ospf_data,'ipv4') }}
{% if ospf_data.import is defined %}
{% for s_proto,s_data in ospf_data.import.items() %}
redistribute {{ s_proto }}{%
if s_proto == 'bgp' %} {{ bgp.as }}{% endif %} subnets{%
if 'policy' in s_data %} route-map {{ s_data.policy }}-ipv4{% endif +%}
{% endfor %}
{% endif %}
{{ redistribute.config(ospf_data,af='ipv4',t_proto='ospf') }}
!
{% for l in intf_data if 'ospf' in l and ('ipv4' in l or 'ipv4' in l.dhcp.client|default({})) %}
interface {{ l.ifname }}
Expand Down
12 changes: 4 additions & 8 deletions netsim/ansible/templates/ospf/ios.ospfv3.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{% import "ios.default.j2" as ospf_default %}
{% import "templates/routing/_default.ios.j2" as ospf_default %}
!
{% macro config(ospf_pid,ospf_vrf,ospf_data,intf_data,bgp={}) %}
{% import "templates/routing/_redistribute.ios.j2" as redistribute with context %}
router ospfv3 {{ ospf_pid }}
router-id {{ ospf_data.router_id }}
{% if ospf_data.reference_bandwidth is defined %}
Expand All @@ -14,13 +16,7 @@ router ospfv3 {{ ospf_pid }}
passive-interface {{ l.ifname }}
{% endfor %}
{{ ospf_default.config(ospf_data,'ipv6') }}
{% if ospf_data.import is defined %}
{% for s_proto,s_data in ospf_data.import.items() %}
redistribute {{ s_proto }}{%
if s_proto == 'bgp' %} {{ bgp.as }}{% endif %}{%
if 'policy' in s_data %} route-map {{ s_data.policy }}-ipv6{% endif +%}
{% endfor %}
{% endif %}
{{ redistribute.config(ospf_data,af='ipv6') }}
!
{% for l in intf_data if 'ospf' in l and ('ipv6' in l or 'ipv6' in l.dhcp.client|default({})) %}
interface {{ l.ifname }}
Expand Down
13 changes: 13 additions & 0 deletions netsim/ansible/templates/routing/_redistribute.eos.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% macro policy(sp_data,af) -%}
{% if 'policy' in sp_data %} route-map {{ sp_data.policy }}-{{ af }}{% endif +%}
{%- endmacro %}
{% macro config(pdata,af='ipv4',vrf=False) -%}
{% if pdata.import is defined %}
{% for s_proto,s_data in pdata.import.items() %}
{% if s_proto == 'ospf' and af == 'ipv6' %}
{% set s_proto = 'ospfv3' %}
{% endif %}
redistribute {{ s_proto }}{% if vrf %} include leaked{% endif %}{{ policy(s_data,af) }}
{% endfor %}
{% endif %}
{%- endmacro %}
13 changes: 13 additions & 0 deletions netsim/ansible/templates/routing/_redistribute.frr.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% macro policy(sp_data,af) -%}
{% if 'policy' in sp_data %} route-map {{ sp_data.policy }}-{{ af }}{% endif +%}
{%- endmacro %}
{% macro config(pdata,af) -%}
{% if pdata.import is defined %}
{% for s_proto,s_data in pdata.import.items() %}
{% if s_proto == 'ospf' and af == 'ipv6' %}
{% set s_proto = 'ospf6' %}
{% endif %}
redistribute {{ s_proto }}{{ policy(s_data,af) }}
{% endfor %}
{% endif %}
{%- endmacro %}
17 changes: 17 additions & 0 deletions netsim/ansible/templates/routing/_redistribute.ios.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% macro policy(sp_data,af) -%}
{% if 'policy' in sp_data %} route-map {{ sp_data.policy }}-{{ af }}{% endif +%}
{%- endmacro %}
{% macro config(pdata,af='ipv4',t_proto='',ospf_pid=1) -%}
{% if pdata.import is defined %}
{% set r_append = ' subnets' if t_proto == 'ospf' else '' %}
{% for s_proto,s_data in pdata.import.items() %}
{% if s_proto == 'bgp' %}
{% set s_proto = s_proto + ' ' + bgp.as|string %}
{% endif %}
{% if s_proto == 'ospf' %}
{% set s_proto = s_proto + ' ' + ospf_pid|string + ' match internal external' %}
{% endif %}
redistribute {{ s_proto }}{{ policy(s_data,af) }}
{% endfor %}
{% endif %}
{%- endmacro %}
9 changes: 2 additions & 7 deletions netsim/ansible/templates/vrf/eos.bgp.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% import "eos.bgp-macro.j2" as bgpcfg %}
{% import "templates/routing/_redistribute.eos.j2" as redistribute with context %}
!
mpls ip
!
Expand All @@ -24,13 +25,7 @@ router bgp {{ bgp.as }}
{% for af in ['ipv4','ipv6'] if af in vdata.af|default({}) %}
!
address-family {{ af }}
{% if vdata.bgp.import is defined %}
{% for s_proto,s_data in vdata.bgp.import.items() %}
redistribute {{ s_proto }}{%
if s_proto == 'ospf' and af == 'ipv6' %}v3{% endif %}{%
if 'policy' in s_data %} route-map {{ s_data.policy }}-{{ af }}{% endif +%}
{% endfor %}
{% endif %}
{{ redistribute.config(vdata.bgp,af=af) }}
{% for n in vdata.networks|default([]) if af in n %}
network {{ n[af]|ipaddr('0') }}
{% endfor %}
Expand Down
5 changes: 2 additions & 3 deletions netsim/ansible/templates/vrf/eos.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% import "eos.ospfv2.j2" as ospfv2 %}
{% import "eos.ospfv3.j2" as ospfv3 %}
{% import "templates/ospf/eos.ospfv2.j2" as ospfv2 %}
{% import "templates/ospf/eos.ospfv3.j2" as ospfv3 %}
{% if bgp.as is defined %}
{% include 'eos.bgp.j2' %}
{% endif %}
Expand All @@ -9,6 +9,5 @@
{% endif %}
{% if vdata.af.ipv6|default(False) %}
{{ ospfv3.config(vdata.vrfidx,vname,vdata.ospf,vdata.ospf.interfaces|default([]),bgp|default({})) }}
{% include 'eos.ospfv3.j2' %}
{% endif %}
{% endfor %}
1 change: 0 additions & 1 deletion netsim/ansible/templates/vrf/eos.ospfv2.j2

This file was deleted.

1 change: 0 additions & 1 deletion netsim/ansible/templates/vrf/eos.ospfv3.j2

This file was deleted.

10 changes: 2 additions & 8 deletions netsim/ansible/templates/vrf/frr.bgp.j2
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% import "templates/routing/_redistribute.frr.j2" as redistribute with context %}
!
router bgp {{ vdata.as|default(bgp.as) }} vrf {{ vname }}
no bgp ebgp-requires-policy
Expand All @@ -19,14 +20,7 @@ router bgp {{ vdata.as|default(bgp.as) }} vrf {{ vname }}
{% endfor %}
{% for af in ['ipv4','ipv6'] if vdata.af[af]|default(False) %}
address-family {{ af }} unicast
{% if vdata.bgp.import is defined %}
!
{% for s_proto,s_data in vdata.bgp.import.items() %}
redistribute {{ s_proto }}{%
if s_proto == 'ospf' and af == 'ipv6' %}6{% endif %}{%
if 'policy' in s_data %} route-map {{ s_data.policy }}-{{ af }}{% endif +%}
{% endfor %}
{% endif %}
{{ redistribute.config(vdata.bgp,af=af) }}
label vpn export auto
export vpn
import vpn
Expand Down
Loading

0 comments on commit 7444e87

Please sign in to comment.