From c48cc1e8979e5475f8e8a291076355504c934bc8 Mon Sep 17 00:00:00 2001 From: Yaqiang Zhu Date: Thu, 16 May 2024 14:19:04 +0800 Subject: [PATCH] [bgp] Add explictly configure bgp router id for multi-asic (#18764) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Why I did it HLD: sonic-net/SONiC#1643 Remove hard coupling between bgp router-id and IPv4 address of Loopback4096 Add support to explictly specify bgp router id for multi asic device Work item tracking Microsoft ADO (number only): 27801007 How I did it When bgp_router_id configured in DEVICE_METADATA, use it as bgp router-id and originator-id. Remove the hard dependency on loopback4096 ipv4 address when adding ibgp peer. Add UTs. Behavior of set bgp router-id To be clarified that when bgp router-id hasn't been explicitly set, bgp actions would totally like previous Loopback4096 IPv4 address exists Loopback4096 IPv4 address doesn't exist bgp_router_id configured Honor bgp_router_id Honor bgp_router_id bgp_router_id doesn't be configured Honor Loopback4096 IPv4 address FRR default router ID value is selected as the largest IP address of the device. When router zebra is not enabled bgpd can’t get interface information, so router-id is set to 0.0.0.0 Behavior of add bgp peer To be clarified that when bgp router-id hasn't been explicitly set, bgp actions would totally like previous Loopback4096 IPv4 address exists Loopback4096 IPv4 address doesn't exist bgp_router_id configured Add BGP peer Add BGP peer bgp_router_id doesn't be configured Add BGP peer Do not add iBGP peer How to verify it UT passed. --- .../docker-fpm-frr/frr/bgpd/bgpd.main.conf.j2 | 11 ++- .../bgpd/templates/internal/policies.conf.j2 | 13 +++- src/sonic-bgpcfgd/bgpcfgd/managers_bgp.py | 8 +- .../policies.conf/param_back_no_lo4096.json | 13 ++++ .../policies.conf/param_back_router_id.json | 17 +++++ .../param_back_router_id_no_lo4096.json | 14 ++++ .../policies.conf/result_back_no_lo4096.conf | 21 ++++++ .../policies.conf/result_back_router_id.conf | 23 ++++++ .../result_back_router_id_no_lo4096.conf | 23 ++++++ .../packet_chassis_ipv6_lo4096.conf | 72 ++++++++++++++++++ .../packet_chassis_ipv6_lo4096.json | 35 +++++++++ .../packet_chassis_ipv6_lo4096_router_id.conf | 73 +++++++++++++++++++ .../packet_chassis_ipv6_lo4096_router_id.json | 36 +++++++++ .../packet_chassis_router_id.conf | 73 +++++++++++++++++++ .../packet_chassis_router_id.json | 37 ++++++++++ src/sonic-bgpcfgd/tests/test_bgp.py | 32 +++++++- src/sonic-bgpcfgd/tests/test_sonic-cfggen.py | 22 +++++- 17 files changed, 508 insertions(+), 15 deletions(-) create mode 100644 src/sonic-bgpcfgd/tests/data/internal/policies.conf/param_back_no_lo4096.json create mode 100644 src/sonic-bgpcfgd/tests/data/internal/policies.conf/param_back_router_id.json create mode 100644 src/sonic-bgpcfgd/tests/data/internal/policies.conf/param_back_router_id_no_lo4096.json create mode 100644 src/sonic-bgpcfgd/tests/data/internal/policies.conf/result_back_no_lo4096.conf create mode 100644 src/sonic-bgpcfgd/tests/data/internal/policies.conf/result_back_router_id.conf create mode 100644 src/sonic-bgpcfgd/tests/data/internal/policies.conf/result_back_router_id_no_lo4096.conf create mode 100644 src/sonic-bgpcfgd/tests/data/sonic-cfggen/bgpd.main.conf.j2/packet_chassis_ipv6_lo4096.conf create mode 100644 src/sonic-bgpcfgd/tests/data/sonic-cfggen/bgpd.main.conf.j2/packet_chassis_ipv6_lo4096.json create mode 100644 src/sonic-bgpcfgd/tests/data/sonic-cfggen/bgpd.main.conf.j2/packet_chassis_ipv6_lo4096_router_id.conf create mode 100644 src/sonic-bgpcfgd/tests/data/sonic-cfggen/bgpd.main.conf.j2/packet_chassis_ipv6_lo4096_router_id.json create mode 100644 src/sonic-bgpcfgd/tests/data/sonic-cfggen/bgpd.main.conf.j2/packet_chassis_router_id.conf create mode 100644 src/sonic-bgpcfgd/tests/data/sonic-cfggen/bgpd.main.conf.j2/packet_chassis_router_id.json diff --git a/dockers/docker-fpm-frr/frr/bgpd/bgpd.main.conf.j2 b/dockers/docker-fpm-frr/frr/bgpd/bgpd.main.conf.j2 index 1ea4c8ac612c..da6358a00ca6 100644 --- a/dockers/docker-fpm-frr/frr/bgpd/bgpd.main.conf.j2 +++ b/dockers/docker-fpm-frr/frr/bgpd/bgpd.main.conf.j2 @@ -9,6 +9,7 @@ ! TSA configuration ! {% set lo0_ipv4 = get_ipv4_loopback_address(LOOPBACK_INTERFACE, "Loopback0") | ip %} +{% set lo4096_ipv4 = get_ipv4_loopback_address(LOOPBACK_INTERFACE, "Loopback4096") | ip %} {% if lo0_ipv4 is not none %} ip prefix-list PL_LoopbackV4 permit {{ lo0_ipv4 }}/32 {% endif %} @@ -89,7 +90,11 @@ router bgp {{ DEVICE_METADATA['localhost']['bgp_asn'] }} ! {# set router-id #} {% if DEVICE_METADATA['localhost']['sub_role'] == 'BackEnd' or DEVICE_METADATA['localhost']['switch_type'] == 'voq' or DEVICE_METADATA['localhost']['switch_type'] == 'chassis-packet' %} - bgp router-id {{ get_ipv4_loopback_address(LOOPBACK_INTERFACE, "Loopback4096") | ip }} +{% if 'bgp_router_id' in DEVICE_METADATA['localhost'] %} + bgp router-id {{ DEVICE_METADATA["localhost"]["bgp_router_id"] }} +{% elif lo4096_ipv4 is not none %} + bgp router-id {{ lo4096_ipv4 }} +{% endif %} {% else %} {% if "localhost" in DEVICE_METADATA and "bgp_router_id" in DEVICE_METADATA["localhost"] %} bgp router-id {{ DEVICE_METADATA["localhost"]["bgp_router_id"] }} @@ -102,8 +107,8 @@ router bgp {{ DEVICE_METADATA['localhost']['bgp_asn'] }} {% if lo0_ipv4 is not none %} network {{ lo0_ipv4 }}/32 {% endif %} -{% if ((multi_asic is defined and DEVICE_METADATA['localhost']['switch_type'] != 'chassis-packet') or (DEVICE_METADATA['localhost']['switch_type'] == 'voq')) %} - network {{ get_ipv4_loopback_address(LOOPBACK_INTERFACE, "Loopback4096") | ip }}/32 route-map HIDE_INTERNAL +{% if lo4096_ipv4 is not none and ((multi_asic is defined and DEVICE_METADATA['localhost']['switch_type'] != 'chassis-packet') or (DEVICE_METADATA['localhost']['switch_type'] == 'voq')) %} + network {{ lo4096_ipv4 }}/32 route-map HIDE_INTERNAL {% endif %} ! {% if get_ipv6_loopback_address(LOOPBACK_INTERFACE, "Loopback0") != 'None' %} diff --git a/dockers/docker-fpm-frr/frr/bgpd/templates/internal/policies.conf.j2 b/dockers/docker-fpm-frr/frr/bgpd/templates/internal/policies.conf.j2 index bedc6eeea422..bd341e70d7f4 100644 --- a/dockers/docker-fpm-frr/frr/bgpd/templates/internal/policies.conf.j2 +++ b/dockers/docker-fpm-frr/frr/bgpd/templates/internal/policies.conf.j2 @@ -4,16 +4,25 @@ ! {% from "common/functions.conf.j2" import get_ipv4_loopback_address %} ! +{% set lo4096_ipv4 = get_ipv4_loopback_address(CONFIG_DB__LOOPBACK_INTERFACE, "Loopback4096") | ip %} {% if CONFIG_DB__DEVICE_METADATA['localhost']['sub_role'] == 'BackEnd' %} route-map FROM_BGP_INTERNAL_PEER_V4 permit 1 - set originator-id {{ get_ipv4_loopback_address(CONFIG_DB__LOOPBACK_INTERFACE, "Loopback4096") | ip }} +{% if "localhost" in CONFIG_DB__DEVICE_METADATA and "bgp_router_id" in CONFIG_DB__DEVICE_METADATA["localhost"] %} + set originator-id {{ CONFIG_DB__DEVICE_METADATA["localhost"]["bgp_router_id"] }} +{% elif lo4096_ipv4 is not none%} + set originator-id {{ lo4096_ipv4 }} +{% endif %} ! route-map FROM_BGP_INTERNAL_PEER_V6 permit 1 set ipv6 next-hop prefer-global on-match next ! route-map FROM_BGP_INTERNAL_PEER_V6 permit 2 - set originator-id {{ get_ipv4_loopback_address(CONFIG_DB__LOOPBACK_INTERFACE, "Loopback4096") | ip }} +{% if "localhost" in CONFIG_DB__DEVICE_METADATA and "bgp_router_id" in CONFIG_DB__DEVICE_METADATA["localhost"] %} + set originator-id {{ CONFIG_DB__DEVICE_METADATA["localhost"]["bgp_router_id"] }} + {% elif lo4096_ipv4 is not none%} + set originator-id {{ lo4096_ipv4 }} + {% endif %} {% elif CONFIG_DB__DEVICE_METADATA['localhost']['switch_type'] == 'chassis-packet' %} bgp community-list standard DEVICE_INTERNAL_COMMUNITY permit {{ constants.bgp.internal_community }} bgp community-list standard NO_EXPORT permit no-export diff --git a/src/sonic-bgpcfgd/bgpcfgd/managers_bgp.py b/src/sonic-bgpcfgd/bgpcfgd/managers_bgp.py index 347270f94a50..26202ebc5016 100644 --- a/src/sonic-bgpcfgd/bgpcfgd/managers_bgp.py +++ b/src/sonic-bgpcfgd/bgpcfgd/managers_bgp.py @@ -128,9 +128,6 @@ def __init__(self, common_objs, db_name, table_name, peer_type, check_neig_meta) if self.check_deployment_id: deps.append(("CONFIG_DB", swsscommon.CFG_DEVICE_METADATA_TABLE_NAME, "localhost/deployment_id")) - if self.peer_type == 'internal': - deps.append(("CONFIG_DB", swsscommon.CFG_LOOPBACK_INTERFACE_TABLE_NAME, "Loopback4096")) - super(BGPPeerMgrBase, self).__init__( common_objs, deps, @@ -175,8 +172,9 @@ def add_peer(self, vrf, nbr, data): # if self.peer_type == 'internal': lo4096_ipv4 = self.get_lo_ipv4("Loopback4096|") - if lo4096_ipv4 is None: - log_warn("Loopback4096 ipv4 address is not presented yet") + if (lo4096_ipv4 is None and "bgp_router_id" + not in self.directory.get_slot("CONFIG_DB", swsscommon.CFG_DEVICE_METADATA_TABLE_NAME)["localhost"]): + log_warn("Loopback4096 ipv4 address is not presented yet and bgp_router_id not configured") return False if "local_addr" not in data: diff --git a/src/sonic-bgpcfgd/tests/data/internal/policies.conf/param_back_no_lo4096.json b/src/sonic-bgpcfgd/tests/data/internal/policies.conf/param_back_no_lo4096.json new file mode 100644 index 000000000000..824193e7e446 --- /dev/null +++ b/src/sonic-bgpcfgd/tests/data/internal/policies.conf/param_back_no_lo4096.json @@ -0,0 +1,13 @@ +{ + "CONFIG_DB__DEVICE_METADATA": { + "localhost": { + "sub_role": "BackEnd" + } + }, + "constants": { + "bgp": { + "internal_community": "12345:556", + "internal_community_match_tag": "101" + } + } +} diff --git a/src/sonic-bgpcfgd/tests/data/internal/policies.conf/param_back_router_id.json b/src/sonic-bgpcfgd/tests/data/internal/policies.conf/param_back_router_id.json new file mode 100644 index 000000000000..8240db6bb5df --- /dev/null +++ b/src/sonic-bgpcfgd/tests/data/internal/policies.conf/param_back_router_id.json @@ -0,0 +1,17 @@ +{ + "CONFIG_DB__DEVICE_METADATA": { + "localhost": { + "sub_role": "BackEnd", + "bgp_router_id": "8.8.8.8" + } + }, + "CONFIG_DB__LOOPBACK_INTERFACE": { + "Loopback4096|10.10.10.10/32": {} + }, + "constants": { + "bgp": { + "internal_community": "12345:556", + "internal_community_match_tag": "101" + } + } +} diff --git a/src/sonic-bgpcfgd/tests/data/internal/policies.conf/param_back_router_id_no_lo4096.json b/src/sonic-bgpcfgd/tests/data/internal/policies.conf/param_back_router_id_no_lo4096.json new file mode 100644 index 000000000000..13d782a6840c --- /dev/null +++ b/src/sonic-bgpcfgd/tests/data/internal/policies.conf/param_back_router_id_no_lo4096.json @@ -0,0 +1,14 @@ +{ + "CONFIG_DB__DEVICE_METADATA": { + "localhost": { + "sub_role": "BackEnd", + "bgp_router_id": "8.8.8.8" + } + }, + "constants": { + "bgp": { + "internal_community": "12345:556", + "internal_community_match_tag": "101" + } + } +} diff --git a/src/sonic-bgpcfgd/tests/data/internal/policies.conf/result_back_no_lo4096.conf b/src/sonic-bgpcfgd/tests/data/internal/policies.conf/result_back_no_lo4096.conf new file mode 100644 index 000000000000..a51f97aac5a8 --- /dev/null +++ b/src/sonic-bgpcfgd/tests/data/internal/policies.conf/result_back_no_lo4096.conf @@ -0,0 +1,21 @@ +! +! template: bgpd/templates/internal/policies.conf.j2 +! +route-map FROM_BGP_INTERNAL_PEER_V4 permit 1 +! +route-map FROM_BGP_INTERNAL_PEER_V6 permit 1 + set ipv6 next-hop prefer-global + on-match next +! +route-map FROM_BGP_INTERNAL_PEER_V6 permit 2 +! +route-map FROM_BGP_INTERNAL_PEER_V4 permit 100 +! +route-map FROM_BGP_INTERNAL_PEER_V6 permit 100 +! +route-map TO_BGP_INTERNAL_PEER_V4 permit 100 +! +route-map TO_BGP_INTERNAL_PEER_V6 permit 100 +! +! end of template: bgpd/templates/internal/policies.conf.j2 +! diff --git a/src/sonic-bgpcfgd/tests/data/internal/policies.conf/result_back_router_id.conf b/src/sonic-bgpcfgd/tests/data/internal/policies.conf/result_back_router_id.conf new file mode 100644 index 000000000000..d067d862d781 --- /dev/null +++ b/src/sonic-bgpcfgd/tests/data/internal/policies.conf/result_back_router_id.conf @@ -0,0 +1,23 @@ +! +! template: bgpd/templates/internal/policies.conf.j2 +! +route-map FROM_BGP_INTERNAL_PEER_V4 permit 1 + set originator-id 8.8.8.8 +! +route-map FROM_BGP_INTERNAL_PEER_V6 permit 1 + set ipv6 next-hop prefer-global + on-match next +! +route-map FROM_BGP_INTERNAL_PEER_V6 permit 2 + set originator-id 8.8.8.8 +! +route-map FROM_BGP_INTERNAL_PEER_V4 permit 100 +! +route-map FROM_BGP_INTERNAL_PEER_V6 permit 100 +! +route-map TO_BGP_INTERNAL_PEER_V4 permit 100 +! +route-map TO_BGP_INTERNAL_PEER_V6 permit 100 +! +! end of template: bgpd/templates/internal/policies.conf.j2 +! diff --git a/src/sonic-bgpcfgd/tests/data/internal/policies.conf/result_back_router_id_no_lo4096.conf b/src/sonic-bgpcfgd/tests/data/internal/policies.conf/result_back_router_id_no_lo4096.conf new file mode 100644 index 000000000000..d067d862d781 --- /dev/null +++ b/src/sonic-bgpcfgd/tests/data/internal/policies.conf/result_back_router_id_no_lo4096.conf @@ -0,0 +1,23 @@ +! +! template: bgpd/templates/internal/policies.conf.j2 +! +route-map FROM_BGP_INTERNAL_PEER_V4 permit 1 + set originator-id 8.8.8.8 +! +route-map FROM_BGP_INTERNAL_PEER_V6 permit 1 + set ipv6 next-hop prefer-global + on-match next +! +route-map FROM_BGP_INTERNAL_PEER_V6 permit 2 + set originator-id 8.8.8.8 +! +route-map FROM_BGP_INTERNAL_PEER_V4 permit 100 +! +route-map FROM_BGP_INTERNAL_PEER_V6 permit 100 +! +route-map TO_BGP_INTERNAL_PEER_V4 permit 100 +! +route-map TO_BGP_INTERNAL_PEER_V6 permit 100 +! +! end of template: bgpd/templates/internal/policies.conf.j2 +! diff --git a/src/sonic-bgpcfgd/tests/data/sonic-cfggen/bgpd.main.conf.j2/packet_chassis_ipv6_lo4096.conf b/src/sonic-bgpcfgd/tests/data/sonic-cfggen/bgpd.main.conf.j2/packet_chassis_ipv6_lo4096.conf new file mode 100644 index 000000000000..381c5b05a1f0 --- /dev/null +++ b/src/sonic-bgpcfgd/tests/data/sonic-cfggen/bgpd.main.conf.j2/packet_chassis_ipv6_lo4096.conf @@ -0,0 +1,72 @@ +! +! template: bgpd/bgpd.main.conf.j2 +! +! bgp multiple-instance +! +! BGP configuration +! +! TSA configuration +! +ip prefix-list PL_LoopbackV4 permit 55.55.55.55/32 +! +ipv6 prefix-list PL_LoopbackV6 permit fc00::/64 +! +ip prefix-list LOCAL_VLAN_IPV4_PREFIX seq 5 permit 10.10.10.0/24 +! +ipv6 prefix-list LOCAL_VLAN_IPV6_PREFIX seq 10 permit fc01::/64 +! +ip prefix-list V4_P2P_IP permit 0.0.0.0/0 ge 31 le 31 +! +ipv6 prefix-list V6_P2P_IP permit ::/0 ge 126 le 126 +! +route-map V4_CONNECTED_ROUTES permit 10 + match ip address prefix-list V4_P2P_IP + call HIDE_INTERNAL +! +route-map V6_CONNECTED_ROUTES permit 10 + match ipv6 address prefix-list V6_P2P_IP + call HIDE_INTERNAL +! +route-map HIDE_INTERNAL permit 10 + set community no-export +! +! +router bgp 55555 +! + bgp log-neighbor-changes + bgp suppress-fib-pending + no bgp default ipv4-unicast + no bgp ebgp-requires-policy +! + bgp bestpath as-path multipath-relax +! +! +! + network 55.55.55.55/32 +! + address-family ipv6 + network fc00::1/64 + network fc00::1/128 route-map HIDE_INTERNAL + exit-address-family +! + network 10.10.10.1/24 + address-family ipv6 + network fc01::1/64 + exit-address-family +! + address-family ipv4 + redistribute connected route-map V4_CONNECTED_ROUTES + exit-address-family + address-family ipv6 + redistribute connected route-map V6_CONNECTED_ROUTES + exit-address-family +! + address-family ipv4 + maximum-paths 64 + exit-address-family + address-family ipv6 + maximum-paths 64 + exit-address-family +! +! end of template: bgpd/bgpd.main.conf.j2 +! diff --git a/src/sonic-bgpcfgd/tests/data/sonic-cfggen/bgpd.main.conf.j2/packet_chassis_ipv6_lo4096.json b/src/sonic-bgpcfgd/tests/data/sonic-cfggen/bgpd.main.conf.j2/packet_chassis_ipv6_lo4096.json new file mode 100644 index 000000000000..34bc6b33904f --- /dev/null +++ b/src/sonic-bgpcfgd/tests/data/sonic-cfggen/bgpd.main.conf.j2/packet_chassis_ipv6_lo4096.json @@ -0,0 +1,35 @@ +{ + "DEVICE_METADATA": { + "localhost": { + "bgp_asn": "55555", + "sub_role": "FrontEnd", + "switch_type": "chassis-packet", + "type": "SpineRouter" + } + }, + "LOOPBACK_INTERFACE": { + "Loopback0|55.55.55.55/32": {}, + "Loopback0|fc00::1/128": {}, + "Loopback4096|fc00::2/128": {} + }, + "VLAN_INTERFACE": { + "Vlan10|10.10.10.1/24": {}, + "Vlan10|fc01::1/64": {}, + "Vlan20": {"vnet_name": "Vnet1"}, + "Vlan20|20.20.20.1/24": {}, + "Vlan20|fd01::1/64": {} + }, + "constants": { + "bgp": { + "multipath_relax": { + "enabled": true + }, + "graceful_restart": { + "enabled": true + }, + "maximum_paths": { + "enabled": true + } + } + } +} diff --git a/src/sonic-bgpcfgd/tests/data/sonic-cfggen/bgpd.main.conf.j2/packet_chassis_ipv6_lo4096_router_id.conf b/src/sonic-bgpcfgd/tests/data/sonic-cfggen/bgpd.main.conf.j2/packet_chassis_ipv6_lo4096_router_id.conf new file mode 100644 index 000000000000..213872f45c70 --- /dev/null +++ b/src/sonic-bgpcfgd/tests/data/sonic-cfggen/bgpd.main.conf.j2/packet_chassis_ipv6_lo4096_router_id.conf @@ -0,0 +1,73 @@ +! +! template: bgpd/bgpd.main.conf.j2 +! +! bgp multiple-instance +! +! BGP configuration +! +! TSA configuration +! +ip prefix-list PL_LoopbackV4 permit 55.55.55.55/32 +! +ipv6 prefix-list PL_LoopbackV6 permit fc00::/64 +! +ip prefix-list LOCAL_VLAN_IPV4_PREFIX seq 5 permit 10.10.10.0/24 +! +ipv6 prefix-list LOCAL_VLAN_IPV6_PREFIX seq 10 permit fc01::/64 +! +ip prefix-list V4_P2P_IP permit 0.0.0.0/0 ge 31 le 31 +! +ipv6 prefix-list V6_P2P_IP permit ::/0 ge 126 le 126 +! +route-map V4_CONNECTED_ROUTES permit 10 + match ip address prefix-list V4_P2P_IP + call HIDE_INTERNAL +! +route-map V6_CONNECTED_ROUTES permit 10 + match ipv6 address prefix-list V6_P2P_IP + call HIDE_INTERNAL +! +route-map HIDE_INTERNAL permit 10 + set community no-export +! +! +router bgp 55555 +! + bgp log-neighbor-changes + bgp suppress-fib-pending + no bgp default ipv4-unicast + no bgp ebgp-requires-policy +! + bgp bestpath as-path multipath-relax +! +! + bgp router-id 8.8.8.8 +! + network 55.55.55.55/32 +! + address-family ipv6 + network fc00::1/64 + network fc00::1/128 route-map HIDE_INTERNAL + exit-address-family +! + network 10.10.10.1/24 + address-family ipv6 + network fc01::1/64 + exit-address-family +! + address-family ipv4 + redistribute connected route-map V4_CONNECTED_ROUTES + exit-address-family + address-family ipv6 + redistribute connected route-map V6_CONNECTED_ROUTES + exit-address-family +! + address-family ipv4 + maximum-paths 64 + exit-address-family + address-family ipv6 + maximum-paths 64 + exit-address-family +! +! end of template: bgpd/bgpd.main.conf.j2 +! diff --git a/src/sonic-bgpcfgd/tests/data/sonic-cfggen/bgpd.main.conf.j2/packet_chassis_ipv6_lo4096_router_id.json b/src/sonic-bgpcfgd/tests/data/sonic-cfggen/bgpd.main.conf.j2/packet_chassis_ipv6_lo4096_router_id.json new file mode 100644 index 000000000000..8ff0d8afb231 --- /dev/null +++ b/src/sonic-bgpcfgd/tests/data/sonic-cfggen/bgpd.main.conf.j2/packet_chassis_ipv6_lo4096_router_id.json @@ -0,0 +1,36 @@ +{ + "DEVICE_METADATA": { + "localhost": { + "bgp_asn": "55555", + "sub_role": "FrontEnd", + "switch_type": "chassis-packet", + "type": "SpineRouter", + "bgp_router_id": "8.8.8.8" + } + }, + "LOOPBACK_INTERFACE": { + "Loopback0|55.55.55.55/32": {}, + "Loopback0|fc00::1/128": {}, + "Loopback4096|fc00::2/128": {} + }, + "VLAN_INTERFACE": { + "Vlan10|10.10.10.1/24": {}, + "Vlan10|fc01::1/64": {}, + "Vlan20": {"vnet_name": "Vnet1"}, + "Vlan20|20.20.20.1/24": {}, + "Vlan20|fd01::1/64": {} + }, + "constants": { + "bgp": { + "multipath_relax": { + "enabled": true + }, + "graceful_restart": { + "enabled": true + }, + "maximum_paths": { + "enabled": true + } + } + } +} diff --git a/src/sonic-bgpcfgd/tests/data/sonic-cfggen/bgpd.main.conf.j2/packet_chassis_router_id.conf b/src/sonic-bgpcfgd/tests/data/sonic-cfggen/bgpd.main.conf.j2/packet_chassis_router_id.conf new file mode 100644 index 000000000000..213872f45c70 --- /dev/null +++ b/src/sonic-bgpcfgd/tests/data/sonic-cfggen/bgpd.main.conf.j2/packet_chassis_router_id.conf @@ -0,0 +1,73 @@ +! +! template: bgpd/bgpd.main.conf.j2 +! +! bgp multiple-instance +! +! BGP configuration +! +! TSA configuration +! +ip prefix-list PL_LoopbackV4 permit 55.55.55.55/32 +! +ipv6 prefix-list PL_LoopbackV6 permit fc00::/64 +! +ip prefix-list LOCAL_VLAN_IPV4_PREFIX seq 5 permit 10.10.10.0/24 +! +ipv6 prefix-list LOCAL_VLAN_IPV6_PREFIX seq 10 permit fc01::/64 +! +ip prefix-list V4_P2P_IP permit 0.0.0.0/0 ge 31 le 31 +! +ipv6 prefix-list V6_P2P_IP permit ::/0 ge 126 le 126 +! +route-map V4_CONNECTED_ROUTES permit 10 + match ip address prefix-list V4_P2P_IP + call HIDE_INTERNAL +! +route-map V6_CONNECTED_ROUTES permit 10 + match ipv6 address prefix-list V6_P2P_IP + call HIDE_INTERNAL +! +route-map HIDE_INTERNAL permit 10 + set community no-export +! +! +router bgp 55555 +! + bgp log-neighbor-changes + bgp suppress-fib-pending + no bgp default ipv4-unicast + no bgp ebgp-requires-policy +! + bgp bestpath as-path multipath-relax +! +! + bgp router-id 8.8.8.8 +! + network 55.55.55.55/32 +! + address-family ipv6 + network fc00::1/64 + network fc00::1/128 route-map HIDE_INTERNAL + exit-address-family +! + network 10.10.10.1/24 + address-family ipv6 + network fc01::1/64 + exit-address-family +! + address-family ipv4 + redistribute connected route-map V4_CONNECTED_ROUTES + exit-address-family + address-family ipv6 + redistribute connected route-map V6_CONNECTED_ROUTES + exit-address-family +! + address-family ipv4 + maximum-paths 64 + exit-address-family + address-family ipv6 + maximum-paths 64 + exit-address-family +! +! end of template: bgpd/bgpd.main.conf.j2 +! diff --git a/src/sonic-bgpcfgd/tests/data/sonic-cfggen/bgpd.main.conf.j2/packet_chassis_router_id.json b/src/sonic-bgpcfgd/tests/data/sonic-cfggen/bgpd.main.conf.j2/packet_chassis_router_id.json new file mode 100644 index 000000000000..9b037b681bf4 --- /dev/null +++ b/src/sonic-bgpcfgd/tests/data/sonic-cfggen/bgpd.main.conf.j2/packet_chassis_router_id.json @@ -0,0 +1,37 @@ +{ + "DEVICE_METADATA": { + "localhost": { + "bgp_asn": "55555", + "sub_role": "FrontEnd", + "switch_type": "chassis-packet", + "type": "SpineRouter", + "bgp_router_id": "8.8.8.8" + } + }, + "LOOPBACK_INTERFACE": { + "Loopback0|55.55.55.55/32": {}, + "Loopback0|fc00::1/128": {}, + "Loopback4096|55.55.55.56/32": {}, + "Loopback4096|fc00::2/128": {} + }, + "VLAN_INTERFACE": { + "Vlan10|10.10.10.1/24": {}, + "Vlan10|fc01::1/64": {}, + "Vlan20": {"vnet_name": "Vnet1"}, + "Vlan20|20.20.20.1/24": {}, + "Vlan20|fd01::1/64": {} + }, + "constants": { + "bgp": { + "multipath_relax": { + "enabled": true + }, + "graceful_restart": { + "enabled": true + }, + "maximum_paths": { + "enabled": true + } + } + } +} diff --git a/src/sonic-bgpcfgd/tests/test_bgp.py b/src/sonic-bgpcfgd/tests/test_bgp.py index 6add8eaf09ed..5b494af4d79a 100644 --- a/src/sonic-bgpcfgd/tests/test_bgp.py +++ b/src/sonic-bgpcfgd/tests/test_bgp.py @@ -21,7 +21,7 @@ def load_constant_files(): return constant_files -def constructor(constants_path, with_lo0_ipv4=True, bgp_router_id=""): +def constructor(constants_path, bgp_router_id="", peer_type="general", with_lo0_ipv4=True, with_lo4096_ipv4=False): cfg_mgr = MagicMock() constants = load_constants(constants_path)['constants'] common_objs = { @@ -37,14 +37,16 @@ def constructor(constants_path, with_lo0_ipv4=True, bgp_router_id=""): } bgpcfgd.managers_bgp.run_command = lambda cmd: return_value_map[str(cmd)] - m = bgpcfgd.managers_bgp.BGPPeerMgrBase(common_objs, "CONFIG_DB", swsscommon.CFG_BGP_NEIGHBOR_TABLE_NAME, "general", True) - assert m.peer_type == "general" + m = bgpcfgd.managers_bgp.BGPPeerMgrBase(common_objs, "CONFIG_DB", swsscommon.CFG_BGP_NEIGHBOR_TABLE_NAME, peer_type, True) + assert m.peer_type == peer_type assert m.check_neig_meta == ('bgp' in constants and 'use_neighbors_meta' in constants['bgp'] and constants['bgp']['use_neighbors_meta']) localhost_obj = {"bgp_asn": "65100"} if len(bgp_router_id) != 0: localhost_obj["bgp_router_id"] = bgp_router_id m.directory.put("CONFIG_DB", swsscommon.CFG_DEVICE_METADATA_TABLE_NAME, "localhost", localhost_obj) + if with_lo4096_ipv4: + m.directory.put("CONFIG_DB", swsscommon.CFG_LOOPBACK_INTERFACE_TABLE_NAME, "Loopback4096|11.11.11.11/32", {}) if with_lo0_ipv4: m.directory.put("CONFIG_DB", swsscommon.CFG_LOOPBACK_INTERFACE_TABLE_NAME, "Loopback0|11.11.11.11/32", {}) m.directory.put("CONFIG_DB", swsscommon.CFG_LOOPBACK_INTERFACE_TABLE_NAME, "Loopback0|FC00:1::32/128", {}) @@ -104,6 +106,28 @@ def test_add_peer(): res = m.set_handler("30.30.30.1", {'asn': '65200', 'holdtime': '180', 'keepalive': '60', 'local_addr': '30.30.30.30', 'name': 'TOR', 'nhopself': '0', 'rrclient': '0'}) assert res, "Expect True return value" +def test_add_peer_internal(): + for constant in load_constant_files(): + m = constructor(constant, peer_type="internal", with_lo4096_ipv4=True) + res = m.set_handler("30.30.30.1", {'asn': '65200', 'holdtime': '180', 'keepalive': '60', 'local_addr': '30.30.30.30', 'name': 'TOR', 'nhopself': '0', 'rrclient': '0'}) + assert res, "Expect True return value" + +def test_add_peer_internal_no_router_id_no_lo4096(): + for constant in load_constant_files(): + m = constructor(constant, peer_type="internal") + res = m.set_handler("30.30.30.1", {'asn': '65200', 'holdtime': '180', 'keepalive': '60', 'local_addr': '30.30.30.30', 'name': 'TOR', 'nhopself': '0', 'rrclient': '0'}) + assert not res, "Expect False return value" + +def test_add_peer_internal_router_id(): + for constant in load_constant_files(): + m = constructor(constant, bgp_router_id="8.8.8.8", peer_type="internal", with_lo4096_ipv4=True) + res = m.set_handler("30.30.30.1", {'asn': '65200', 'holdtime': '180', 'keepalive': '60', 'local_addr': '30.30.30.30', 'name': 'TOR', 'nhopself': '0', 'rrclient': '0'}) + assert res, "Expect True return value" + +def test_add_peer_internal_router_id_no_lo4096(): + for constant in load_constant_files(): + m = constructor(constant, bgp_router_id="8.8.8.8", peer_type="internal") + def test_add_peer_router_id(): for constant in load_constant_files(): m = constructor(constant, bgp_router_id="8.8.8.8") @@ -118,7 +142,7 @@ def test_add_peer_without_lo_ipv4(): def test_add_peer_without_lo_ipv4_router_id(): for constant in load_constant_files(): - m = constructor(constant, with_lo0_ipv4=False, bgp_router_id="8.8.8.8") + m = constructor(constant, bgp_router_id="8.8.8.8", with_lo0_ipv4=False) res = m.set_handler("30.30.30.1", {'asn': '65200', 'holdtime': '180', 'keepalive': '60', 'local_addr': '30.30.30.30', 'name': 'TOR', 'nhopself': '0', 'rrclient': '0'}) assert res, "Expect True return value" diff --git a/src/sonic-bgpcfgd/tests/test_sonic-cfggen.py b/src/sonic-bgpcfgd/tests/test_sonic-cfggen.py index d230197b710c..b5685b7332f0 100644 --- a/src/sonic-bgpcfgd/tests/test_sonic-cfggen.py +++ b/src/sonic-bgpcfgd/tests/test_sonic-cfggen.py @@ -24,6 +24,9 @@ def run_test(name, template_path, json_path, match_path): with open(match_path) as result_fp: raw_saved_result = result_fp.read() canonical_saved_result = ConfigMgr.to_canonical(raw_saved_result) + print(canonical_generated_result) + print("----") + print(canonical_saved_result) assert canonical_saved_result == canonical_generated_result, "Test %s" % name @@ -153,6 +156,24 @@ def test_bgp_conf_all(): "bgpd.conf.j2/all.json", "bgpd.conf.j2/all.conf") +def test_bgp_conf_packet_chassis_ipv6_lo4096(): + run_test("packet chassis ipv6 loopback4096 bgpd.main.conf.j2", + "bgpd/bgpd.main.conf.j2", + "bgpd.main.conf.j2/packet_chassis_ipv6_lo4096.json", + "bgpd.main.conf.j2/packet_chassis_ipv6_lo4096.conf") + +def test_bgp_conf_packet_chassis_ipv6_lo4096_router_id(): + run_test("packet chassis ipv6 loopback4096 with router_id bgpd.main.conf.j2", + "bgpd/bgpd.main.conf.j2", + "bgpd.main.conf.j2/packet_chassis_ipv6_lo4096_router_id.json", + "bgpd.main.conf.j2/packet_chassis_ipv6_lo4096_router_id.conf") + +def test_bgp_conf_packet_chassis_router_id(): + run_test("packet chassis with router_id bgpd.main.conf.j2", + "bgpd/bgpd.main.conf.j2", + "bgpd.main.conf.j2/packet_chassis_router_id.json", + "bgpd.main.conf.j2/packet_chassis_router_id.conf") + def test_bgpd_main_conf_lo0_ipv6_only(): run_test("Base bgpd.main.conf.j2", "bgpd/bgpd.main.conf.j2", @@ -170,4 +191,3 @@ def test_bgpd_main_conf_defaults_router_id(): "bgpd/bgpd.main.conf.j2", "bgpd.main.conf.j2/defaults_router_id.json", "bgpd.main.conf.j2/defaults_router_id.conf") - \ No newline at end of file