diff --git a/src/sonic-bgpcfgd/bgpcfgd/managers_static_rt.py b/src/sonic-bgpcfgd/bgpcfgd/managers_static_rt.py index 3af75d20e1..072a10b3b1 100644 --- a/src/sonic-bgpcfgd/bgpcfgd/managers_static_rt.py +++ b/src/sonic-bgpcfgd/bgpcfgd/managers_static_rt.py @@ -27,6 +27,8 @@ class StaticRouteMgr(Manager): OP_DELETE = 'DELETE' OP_ADD = 'ADD' + ROUTE_ADVERTISE_ENABLE_TAG = '1' + ROUTE_ADVERTISE_DISABLE_TAG = '2' def set_handler(self, key, data): vrf, ip_prefix = self.split_key(key) @@ -38,11 +40,12 @@ class StaticRouteMgr(Manager): intf_list = arg_list(data['ifname']) if 'ifname' in data else None dist_list = arg_list(data['distance']) if 'distance' in data else None nh_vrf_list = arg_list(data['nexthop-vrf']) if 'nexthop-vrf' in data else None + route_tag = self.ROUTE_ADVERTISE_DISABLE_TAG if 'advertise' in data and data['advertise'] == "false" else self.ROUTE_ADVERTISE_ENABLE_TAG try: ip_nh_set = IpNextHopSet(is_ipv6, bkh_list, nh_list, intf_list, dist_list, nh_vrf_list) - cur_nh_set = self.static_routes.get(vrf, {}).get(ip_prefix, IpNextHopSet(is_ipv6)) - cmd_list = self.static_route_commands(ip_nh_set, cur_nh_set, ip_prefix, vrf) + cur_nh_set, cur_route_tag = self.static_routes.get(vrf, {}).get(ip_prefix, (IpNextHopSet(is_ipv6), route_tag)) + cmd_list = self.static_route_commands(ip_nh_set, cur_nh_set, ip_prefix, vrf, route_tag, cur_route_tag) except Exception as exc: log_crit("Got an exception %s: Traceback: %s" % (str(exc), traceback.format_exc())) return False @@ -60,7 +63,7 @@ class StaticRouteMgr(Manager): else: log_debug("Nothing to update for static route {}".format(key)) - self.static_routes.setdefault(vrf, {})[ip_prefix] = ip_nh_set + self.static_routes.setdefault(vrf, {})[ip_prefix] = (ip_nh_set, route_tag) return True @@ -70,8 +73,8 @@ class StaticRouteMgr(Manager): is_ipv6 = TemplateFabric.is_ipv6(ip_prefix) ip_nh_set = IpNextHopSet(is_ipv6) - cur_nh_set = self.static_routes.get(vrf, {}).get(ip_prefix, IpNextHopSet(is_ipv6)) - cmd_list = self.static_route_commands(ip_nh_set, cur_nh_set, ip_prefix, vrf) + cur_nh_set, route_tag = self.static_routes.get(vrf, {}).get(ip_prefix, (IpNextHopSet(is_ipv6), self.ROUTE_ADVERTISE_DISABLE_TAG)) + cmd_list = self.static_route_commands(ip_nh_set, cur_nh_set, ip_prefix, vrf, route_tag, route_tag) # Disable redistribution of static routes when it is the last one to delete if self.static_routes.get(vrf, {}).keys() == {ip_prefix}: @@ -99,36 +102,47 @@ class StaticRouteMgr(Manager): else: return tuple(key.split('|', 1)) - def static_route_commands(self, ip_nh_set, cur_nh_set, ip_prefix, vrf): - diff_set = ip_nh_set.symmetric_difference(cur_nh_set) - + def static_route_commands(self, ip_nh_set, cur_nh_set, ip_prefix, vrf, route_tag, cur_route_tag): op_cmd_list = {} - for ip_nh in diff_set: - if ip_nh in cur_nh_set: - op = self.OP_DELETE - else: - op = self.OP_ADD + if route_tag != cur_route_tag: + for ip_nh in cur_nh_set: + op_cmds = op_cmd_list.setdefault(self.OP_DELETE, []) + op_cmds.append(self.generate_command(self.OP_DELETE, ip_nh, ip_prefix, vrf, cur_route_tag)) + for ip_nh in ip_nh_set: + op_cmds = op_cmd_list.setdefault(self.OP_ADD, []) + op_cmds.append(self.generate_command(self.OP_ADD, ip_nh, ip_prefix, vrf, route_tag)) + else: + diff_set = ip_nh_set.symmetric_difference(cur_nh_set) - op_cmds = op_cmd_list.setdefault(op, []) - op_cmds.append(self.generate_command(op, ip_nh, ip_prefix, vrf)) + for ip_nh in diff_set: + if ip_nh in cur_nh_set: + op = self.OP_DELETE + else: + op = self.OP_ADD + + op_cmds = op_cmd_list.setdefault(op, []) + op_cmds.append(self.generate_command(op, ip_nh, ip_prefix, vrf, route_tag)) cmd_list = op_cmd_list.get(self.OP_DELETE, []) cmd_list += op_cmd_list.get(self.OP_ADD, []) return cmd_list - def generate_command(self, op, ip_nh, ip_prefix, vrf): - return '{}{} route {}{}{}'.format( + def generate_command(self, op, ip_nh, ip_prefix, vrf, route_tag): + return '{}{} route {}{}{}{}'.format( 'no ' if op == self.OP_DELETE else '', 'ipv6' if ip_nh.af == socket.AF_INET6 else 'ip', ip_prefix, ip_nh, - ' vrf {}'.format(vrf) if vrf != 'default' else '' + ' vrf {}'.format(vrf) if vrf != 'default' else '', + ' tag {}'.format(route_tag) ) def enable_redistribution_command(self, vrf): log_debug("Enabling static route redistribution") cmd_list = [] + cmd_list.append("route-map STATIC_ROUTE_FILTER permit 10") + cmd_list.append(" match tag %s" % self.ROUTE_ADVERTISE_ENABLE_TAG) bgp_asn = self.directory.get_slot("CONFIG_DB", swsscommon.CFG_DEVICE_METADATA_TABLE_NAME)["localhost"]["bgp_asn"] if vrf == 'default': cmd_list.append("router bgp %s" % bgp_asn) @@ -136,7 +150,7 @@ class StaticRouteMgr(Manager): cmd_list.append("router bgp %s vrf %s" % (bgp_asn, vrf)) for af in ["ipv4", "ipv6"]: cmd_list.append(" address-family %s" % af) - cmd_list.append(" redistribute static") + cmd_list.append(" redistribute static route-map STATIC_ROUTE_FILTER") return cmd_list def disable_redistribution_command(self, vrf): @@ -149,7 +163,8 @@ class StaticRouteMgr(Manager): cmd_list.append("router bgp %s vrf %s" % (bgp_asn, vrf)) for af in ["ipv4", "ipv6"]: cmd_list.append(" address-family %s" % af) - cmd_list.append(" no redistribute static") + cmd_list.append(" no redistribute static route-map STATIC_ROUTE_FILTER") + cmd_list.append("no route-map STATIC_ROUTE_FILTER") return cmd_list def on_bgp_asn_change(self): diff --git a/src/sonic-bgpcfgd/tests/test_static_rt.py b/src/sonic-bgpcfgd/tests/test_static_rt.py index 09d4a0c6c6..881bba1563 100644 --- a/src/sonic-bgpcfgd/tests/test_static_rt.py +++ b/src/sonic-bgpcfgd/tests/test_static_rt.py @@ -62,12 +62,14 @@ def test_set(): }), True, [ - "ip route 10.1.0.0/24 10.0.0.57", + "ip route 10.1.0.0/24 10.0.0.57 tag 1", + "route-map STATIC_ROUTE_FILTER permit 10", + " match tag 1", "router bgp 65100", " address-family ipv4", - " redistribute static", + " redistribute static route-map STATIC_ROUTE_FILTER", " address-family ipv6", - " redistribute static" + " redistribute static route-map STATIC_ROUTE_FILTER" ] ) @@ -81,12 +83,14 @@ def test_set_nhportchannel(): }), True, [ - "ip route 10.1.0.0/24 PortChannel0001", + "ip route 10.1.0.0/24 PortChannel0001 tag 1", + "route-map STATIC_ROUTE_FILTER permit 10", + " match tag 1", "router bgp 65100", " address-family ipv4", - " redistribute static", + " redistribute static route-map STATIC_ROUTE_FILTER", " address-family ipv6", - " redistribute static" + " redistribute static route-map STATIC_ROUTE_FILTER" ] ) @@ -96,12 +100,13 @@ def test_set_nhportchannel(): ("10.1.0.0/24",), True, [ - "no ip route 10.1.0.0/24 PortChannel0001", + "no ip route 10.1.0.0/24 PortChannel0001 tag 1", "router bgp 65100", " address-family ipv4", - " no redistribute static", + " no redistribute static route-map STATIC_ROUTE_FILTER", " address-family ipv6", - " no redistribute static" + " no redistribute static route-map STATIC_ROUTE_FILTER", + "no route-map STATIC_ROUTE_FILTER" ] ) @@ -115,13 +120,15 @@ def test_set_several_nhportchannels(): }), True, [ - "ip route 10.1.0.0/24 PortChannel0003", - "ip route 10.1.0.0/24 PortChannel0004", + "ip route 10.1.0.0/24 PortChannel0003 tag 1", + "ip route 10.1.0.0/24 PortChannel0004 tag 1", + "route-map STATIC_ROUTE_FILTER permit 10", + " match tag 1", "router bgp 65100", " address-family ipv4", - " redistribute static", + " redistribute static route-map STATIC_ROUTE_FILTER", " address-family ipv6", - " redistribute static" + " redistribute static route-map STATIC_ROUTE_FILTER" ] ) @@ -139,12 +146,14 @@ def test_set_nhvrf(): }), True, [ - "ip route 10.1.1.0/24 10.0.0.57 PortChannel0001 10 nexthop-vrf nh_vrf", + "ip route 10.1.1.0/24 10.0.0.57 PortChannel0001 10 nexthop-vrf nh_vrf tag 1", + "route-map STATIC_ROUTE_FILTER permit 10", + " match tag 1", "router bgp 65100", " address-family ipv4", - " redistribute static", + " redistribute static route-map STATIC_ROUTE_FILTER", " address-family ipv6", - " redistribute static" + " redistribute static route-map STATIC_ROUTE_FILTER" ] ) @@ -162,12 +171,14 @@ def test_set_blackhole(): }), True, [ - "ip route 10.1.2.0/24 blackhole 10", + "ip route 10.1.2.0/24 blackhole 10 tag 1", + "route-map STATIC_ROUTE_FILTER permit 10", + " match tag 1", "router bgp 65100", " address-family ipv4", - " redistribute static", + " redistribute static route-map STATIC_ROUTE_FILTER", " address-family ipv6", - " redistribute static" + " redistribute static route-map STATIC_ROUTE_FILTER" ] ) @@ -185,12 +196,14 @@ def test_set_vrf(): }), True, [ - "ip route 10.1.3.0/24 10.0.0.57 PortChannel0001 10 nexthop-vrf nh_vrf vrf vrfRED", + "ip route 10.1.3.0/24 10.0.0.57 PortChannel0001 10 nexthop-vrf nh_vrf vrf vrfRED tag 1", + "route-map STATIC_ROUTE_FILTER permit 10", + " match tag 1", "router bgp 65100 vrf vrfRED", " address-family ipv4", - " redistribute static", + " redistribute static route-map STATIC_ROUTE_FILTER", " address-family ipv6", - " redistribute static" + " redistribute static route-map STATIC_ROUTE_FILTER" ] ) @@ -208,12 +221,14 @@ def test_set_ipv6(): }), True, [ - "ipv6 route fc00:10::/64 fc00::72 PortChannel0001 10", + "ipv6 route fc00:10::/64 fc00::72 PortChannel0001 10 tag 1", + "route-map STATIC_ROUTE_FILTER permit 10", + " match tag 1", "router bgp 65100", " address-family ipv4", - " redistribute static", + " redistribute static route-map STATIC_ROUTE_FILTER", " address-family ipv6", - " redistribute static" + " redistribute static route-map STATIC_ROUTE_FILTER" ] ) @@ -230,14 +245,16 @@ def test_set_nh_only(): }), True, [ - "ip route 10.1.3.0/24 10.0.0.57 10 nexthop-vrf nh_vrf vrf vrfRED", - "ip route 10.1.3.0/24 10.0.0.59 20 vrf vrfRED", - "ip route 10.1.3.0/24 10.0.0.61 30 nexthop-vrf default vrf vrfRED", + "ip route 10.1.3.0/24 10.0.0.57 10 nexthop-vrf nh_vrf vrf vrfRED tag 1", + "ip route 10.1.3.0/24 10.0.0.59 20 vrf vrfRED tag 1", + "ip route 10.1.3.0/24 10.0.0.61 30 nexthop-vrf default vrf vrfRED tag 1", + "route-map STATIC_ROUTE_FILTER permit 10", + " match tag 1", "router bgp 65100 vrf vrfRED", " address-family ipv4", - " redistribute static", + " redistribute static route-map STATIC_ROUTE_FILTER", " address-family ipv6", - " redistribute static" + " redistribute static route-map STATIC_ROUTE_FILTER" ] ) @@ -254,14 +271,16 @@ def test_set_ifname_only(): }), True, [ - "ip route 10.1.3.0/24 PortChannel0001 10 nexthop-vrf nh_vrf vrf vrfRED", - "ip route 10.1.3.0/24 PortChannel0002 20 vrf vrfRED", - "ip route 10.1.3.0/24 PortChannel0003 30 nexthop-vrf default vrf vrfRED", + "ip route 10.1.3.0/24 PortChannel0001 10 nexthop-vrf nh_vrf vrf vrfRED tag 1", + "ip route 10.1.3.0/24 PortChannel0002 20 vrf vrfRED tag 1", + "ip route 10.1.3.0/24 PortChannel0003 30 nexthop-vrf default vrf vrfRED tag 1", + "route-map STATIC_ROUTE_FILTER permit 10", + " match tag 1", "router bgp 65100 vrf vrfRED", " address-family ipv4", - " redistribute static", + " redistribute static route-map STATIC_ROUTE_FILTER", " address-family ipv6", - " redistribute static" + " redistribute static route-map STATIC_ROUTE_FILTER" ] ) @@ -279,14 +298,16 @@ def test_set_with_empty_ifname(): }), True, [ - "ip route 10.1.3.0/24 10.0.0.57 PortChannel0001 10 nexthop-vrf nh_vrf vrf vrfRED", - "ip route 10.1.3.0/24 10.0.0.59 20 vrf vrfRED", - "ip route 10.1.3.0/24 10.0.0.61 PortChannel0003 30 nexthop-vrf default vrf vrfRED", + "ip route 10.1.3.0/24 10.0.0.57 PortChannel0001 10 nexthop-vrf nh_vrf vrf vrfRED tag 1", + "ip route 10.1.3.0/24 10.0.0.59 20 vrf vrfRED tag 1", + "ip route 10.1.3.0/24 10.0.0.61 PortChannel0003 30 nexthop-vrf default vrf vrfRED tag 1", + "route-map STATIC_ROUTE_FILTER permit 10", + " match tag 1", "router bgp 65100 vrf vrfRED", " address-family ipv4", - " redistribute static", + " redistribute static route-map STATIC_ROUTE_FILTER", " address-family ipv6", - " redistribute static" + " redistribute static route-map STATIC_ROUTE_FILTER" ] ) @@ -304,14 +325,16 @@ def test_set_with_empty_nh(): }), True, [ - "ip route 10.1.3.0/24 10.0.0.57 PortChannel0001 10 nexthop-vrf nh_vrf vrf vrfRED", - "ip route 10.1.3.0/24 PortChannel0002 20 vrf vrfRED", - "ip route 10.1.3.0/24 PortChannel0003 30 nexthop-vrf default vrf vrfRED", + "ip route 10.1.3.0/24 10.0.0.57 PortChannel0001 10 nexthop-vrf nh_vrf vrf vrfRED tag 1", + "ip route 10.1.3.0/24 PortChannel0002 20 vrf vrfRED tag 1", + "ip route 10.1.3.0/24 PortChannel0003 30 nexthop-vrf default vrf vrfRED tag 1", + "route-map STATIC_ROUTE_FILTER permit 10", + " match tag 1", "router bgp 65100 vrf vrfRED", " address-family ipv4", - " redistribute static", + " redistribute static route-map STATIC_ROUTE_FILTER", " address-family ipv6", - " redistribute static" + " redistribute static route-map STATIC_ROUTE_FILTER" ] ) @@ -329,14 +352,16 @@ def test_set_del(): }), True, [ - "ip route 10.1.3.0/24 10.0.0.57 PortChannel0001 10 nexthop-vrf nh_vrf vrf vrfRED", - "ip route 10.1.3.0/24 10.0.0.59 PortChannel0002 20 vrf vrfRED", - "ip route 10.1.3.0/24 10.0.0.61 PortChannel0003 30 nexthop-vrf default vrf vrfRED", + "ip route 10.1.3.0/24 10.0.0.57 PortChannel0001 10 nexthop-vrf nh_vrf vrf vrfRED tag 1", + "ip route 10.1.3.0/24 10.0.0.59 PortChannel0002 20 vrf vrfRED tag 1", + "ip route 10.1.3.0/24 10.0.0.61 PortChannel0003 30 nexthop-vrf default vrf vrfRED tag 1", + "route-map STATIC_ROUTE_FILTER permit 10", + " match tag 1", "router bgp 65100 vrf vrfRED", " address-family ipv4", - " redistribute static", + " redistribute static route-map STATIC_ROUTE_FILTER", " address-family ipv6", - " redistribute static" + " redistribute static route-map STATIC_ROUTE_FILTER" ] ) set_del_test( @@ -345,14 +370,15 @@ def test_set_del(): ("vrfRED|10.1.3.0/24",), True, [ - "no ip route 10.1.3.0/24 10.0.0.57 PortChannel0001 10 nexthop-vrf nh_vrf vrf vrfRED", - "no ip route 10.1.3.0/24 10.0.0.59 PortChannel0002 20 vrf vrfRED", - "no ip route 10.1.3.0/24 10.0.0.61 PortChannel0003 30 nexthop-vrf default vrf vrfRED", + "no ip route 10.1.3.0/24 10.0.0.57 PortChannel0001 10 nexthop-vrf nh_vrf vrf vrfRED tag 1", + "no ip route 10.1.3.0/24 10.0.0.59 PortChannel0002 20 vrf vrfRED tag 1", + "no ip route 10.1.3.0/24 10.0.0.61 PortChannel0003 30 nexthop-vrf default vrf vrfRED tag 1", "router bgp 65100 vrf vrfRED", " address-family ipv4", - " no redistribute static", + " no redistribute static route-map STATIC_ROUTE_FILTER", " address-family ipv6", - " no redistribute static" + " no redistribute static route-map STATIC_ROUTE_FILTER", + "no route-map STATIC_ROUTE_FILTER" ] ) set_del_test( @@ -367,14 +393,16 @@ def test_set_del(): }), True, [ - "ip route 10.1.3.0/24 10.0.0.57 PortChannel0001 10 nexthop-vrf nh_vrf vrf vrfRED", - "ip route 10.1.3.0/24 10.0.0.59 PortChannel0002 20 vrf vrfRED", - "ip route 10.1.3.0/24 10.0.0.61 PortChannel0003 30 nexthop-vrf default vrf vrfRED", + "ip route 10.1.3.0/24 10.0.0.57 PortChannel0001 10 nexthop-vrf nh_vrf vrf vrfRED tag 1", + "ip route 10.1.3.0/24 10.0.0.59 PortChannel0002 20 vrf vrfRED tag 1", + "ip route 10.1.3.0/24 10.0.0.61 PortChannel0003 30 nexthop-vrf default vrf vrfRED tag 1", + "route-map STATIC_ROUTE_FILTER permit 10", + " match tag 1", "router bgp 65100 vrf vrfRED", " address-family ipv4", - " redistribute static", + " redistribute static route-map STATIC_ROUTE_FILTER", " address-family ipv6", - " redistribute static" + " redistribute static route-map STATIC_ROUTE_FILTER" ] ) @@ -392,14 +420,16 @@ def test_set_same_route(): }), True, [ - "ip route 10.1.3.0/24 10.0.0.57 PortChannel0001 10 nexthop-vrf nh_vrf vrf vrfRED", - "ip route 10.1.3.0/24 10.0.0.59 PortChannel0002 20 vrf vrfRED", - "ip route 10.1.3.0/24 10.0.0.61 PortChannel0003 30 nexthop-vrf default vrf vrfRED", + "ip route 10.1.3.0/24 10.0.0.57 PortChannel0001 10 nexthop-vrf nh_vrf vrf vrfRED tag 1", + "ip route 10.1.3.0/24 10.0.0.59 PortChannel0002 20 vrf vrfRED tag 1", + "ip route 10.1.3.0/24 10.0.0.61 PortChannel0003 30 nexthop-vrf default vrf vrfRED tag 1", + "route-map STATIC_ROUTE_FILTER permit 10", + " match tag 1", "router bgp 65100 vrf vrfRED", " address-family ipv4", - " redistribute static", + " redistribute static route-map STATIC_ROUTE_FILTER", " address-family ipv6", - " redistribute static" + " redistribute static route-map STATIC_ROUTE_FILTER" ] ) set_del_test( @@ -414,12 +444,12 @@ def test_set_same_route(): }), True, [ - "no ip route 10.1.3.0/24 10.0.0.57 PortChannel0001 10 nexthop-vrf nh_vrf vrf vrfRED", - "no ip route 10.1.3.0/24 10.0.0.59 PortChannel0002 20 vrf vrfRED", - "no ip route 10.1.3.0/24 10.0.0.61 PortChannel0003 30 nexthop-vrf default vrf vrfRED", - "ip route 10.1.3.0/24 10.0.0.57 PortChannel0001 40 nexthop-vrf nh_vrf vrf vrfRED", - "ip route 10.1.3.0/24 10.0.0.59 PortChannel0002 50 vrf vrfRED", - "ip route 10.1.3.0/24 10.0.0.61 PortChannel0003 60 nexthop-vrf default vrf vrfRED" + "no ip route 10.1.3.0/24 10.0.0.57 PortChannel0001 10 nexthop-vrf nh_vrf vrf vrfRED tag 1", + "no ip route 10.1.3.0/24 10.0.0.59 PortChannel0002 20 vrf vrfRED tag 1", + "no ip route 10.1.3.0/24 10.0.0.61 PortChannel0003 30 nexthop-vrf default vrf vrfRED tag 1", + "ip route 10.1.3.0/24 10.0.0.57 PortChannel0001 40 nexthop-vrf nh_vrf vrf vrfRED tag 1", + "ip route 10.1.3.0/24 10.0.0.59 PortChannel0002 50 vrf vrfRED tag 1", + "ip route 10.1.3.0/24 10.0.0.61 PortChannel0003 60 nexthop-vrf default vrf vrfRED tag 1" ] ) @@ -437,14 +467,16 @@ def test_set_add_del_nh(): }), True, [ - "ip route 10.1.3.0/24 10.0.0.57 PortChannel0001 10 nexthop-vrf nh_vrf vrf vrfRED", - "ip route 10.1.3.0/24 10.0.0.59 PortChannel0002 20 vrf vrfRED", - "ip route 10.1.3.0/24 10.0.0.61 PortChannel0003 30 nexthop-vrf default vrf vrfRED", + "ip route 10.1.3.0/24 10.0.0.57 PortChannel0001 10 nexthop-vrf nh_vrf vrf vrfRED tag 1", + "ip route 10.1.3.0/24 10.0.0.59 PortChannel0002 20 vrf vrfRED tag 1", + "ip route 10.1.3.0/24 10.0.0.61 PortChannel0003 30 nexthop-vrf default vrf vrfRED tag 1", + "route-map STATIC_ROUTE_FILTER permit 10", + " match tag 1", "router bgp 65100 vrf vrfRED", " address-family ipv4", - " redistribute static", + " redistribute static route-map STATIC_ROUTE_FILTER", " address-family ipv6", - " redistribute static" + " redistribute static route-map STATIC_ROUTE_FILTER" ] ) set_del_test( @@ -459,7 +491,7 @@ def test_set_add_del_nh(): }), True, [ - "ip route 10.1.3.0/24 10.0.0.63 PortChannel0004 30 vrf vrfRED", + "ip route 10.1.3.0/24 10.0.0.63 PortChannel0004 30 vrf vrfRED tag 1", ] ) set_del_test( @@ -474,8 +506,8 @@ def test_set_add_del_nh(): }), True, [ - "no ip route 10.1.3.0/24 10.0.0.61 PortChannel0003 30 nexthop-vrf default vrf vrfRED", - "no ip route 10.1.3.0/24 10.0.0.63 PortChannel0004 30 vrf vrfRED", + "no ip route 10.1.3.0/24 10.0.0.61 PortChannel0003 30 nexthop-vrf default vrf vrfRED tag 1", + "no ip route 10.1.3.0/24 10.0.0.63 PortChannel0004 30 vrf vrfRED tag 1", ] ) @@ -493,14 +525,16 @@ def test_set_add_del_nh_ethernet(): }), True, [ - "ip route 20.1.3.0/24 20.0.0.57 Ethernet4 10 nexthop-vrf default", - "ip route 20.1.3.0/24 20.0.0.59 Ethernet8 20", - "ip route 20.1.3.0/24 20.0.0.61 Ethernet12 30 nexthop-vrf default", + "ip route 20.1.3.0/24 20.0.0.57 Ethernet4 10 nexthop-vrf default tag 1", + "ip route 20.1.3.0/24 20.0.0.59 Ethernet8 20 tag 1", + "ip route 20.1.3.0/24 20.0.0.61 Ethernet12 30 nexthop-vrf default tag 1", + "route-map STATIC_ROUTE_FILTER permit 10", + " match tag 1", "router bgp 65100", " address-family ipv4", - " redistribute static", + " redistribute static route-map STATIC_ROUTE_FILTER", " address-family ipv6", - " redistribute static" + " redistribute static route-map STATIC_ROUTE_FILTER" ] ) set_del_test( @@ -515,7 +549,7 @@ def test_set_add_del_nh_ethernet(): }), True, [ - "ip route 20.1.3.0/24 20.0.0.63 Ethernet16 30", + "ip route 20.1.3.0/24 20.0.0.63 Ethernet16 30 tag 1", ] ) set_del_test( @@ -530,8 +564,8 @@ def test_set_add_del_nh_ethernet(): }), True, [ - "no ip route 20.1.3.0/24 20.0.0.61 Ethernet12 30 nexthop-vrf default", - "no ip route 20.1.3.0/24 20.0.0.63 Ethernet16 30", + "no ip route 20.1.3.0/24 20.0.0.61 Ethernet12 30 nexthop-vrf default tag 1", + "no ip route 20.1.3.0/24 20.0.0.63 Ethernet16 30 tag 1", ] ) @@ -548,12 +582,14 @@ def test_set_no_action(mocked_log_debug): }), True, [ - "ip route 10.1.1.0/24 blackhole", + "ip route 10.1.1.0/24 blackhole tag 1", + "route-map STATIC_ROUTE_FILTER permit 10", + " match tag 1", "router bgp 65100", " address-family ipv4", - " redistribute static", + " redistribute static route-map STATIC_ROUTE_FILTER", " address-family ipv6", - " redistribute static" + " redistribute static route-map STATIC_ROUTE_FILTER" ] ) @@ -608,11 +644,13 @@ def test_set_invalid_blackhole(mocked_log_err): }), True, [ + "route-map STATIC_ROUTE_FILTER permit 10", + " match tag 1", "router bgp 65100", " address-family ipv4", - " redistribute static", + " redistribute static route-map STATIC_ROUTE_FILTER", " address-family ipv6", - " redistribute static" + " redistribute static route-map STATIC_ROUTE_FILTER" ] ) mocked_log_err.assert_called_with("Mandatory attribute not found for nexthop") @@ -643,9 +681,9 @@ def test_set_del_no_bgp_asn(): }), True, [ - "ip route 10.1.3.0/24 10.0.0.57 PortChannel0001 10 nexthop-vrf nh_vrf vrf vrfRED", - "ip route 10.1.3.0/24 10.0.0.59 PortChannel0002 20 vrf vrfRED", - "ip route 10.1.3.0/24 10.0.0.61 PortChannel0003 30 nexthop-vrf default vrf vrfRED", + "ip route 10.1.3.0/24 10.0.0.57 PortChannel0001 10 nexthop-vrf nh_vrf vrf vrfRED tag 1", + "ip route 10.1.3.0/24 10.0.0.59 PortChannel0002 20 vrf vrfRED tag 1", + "ip route 10.1.3.0/24 10.0.0.61 PortChannel0003 30 nexthop-vrf default vrf vrfRED tag 1", ] ) set_del_test( @@ -654,9 +692,9 @@ def test_set_del_no_bgp_asn(): ("vrfRED|10.1.3.0/24",), True, [ - "no ip route 10.1.3.0/24 10.0.0.57 PortChannel0001 10 nexthop-vrf nh_vrf vrf vrfRED", - "no ip route 10.1.3.0/24 10.0.0.59 PortChannel0002 20 vrf vrfRED", - "no ip route 10.1.3.0/24 10.0.0.61 PortChannel0003 30 nexthop-vrf default vrf vrfRED", + "no ip route 10.1.3.0/24 10.0.0.57 PortChannel0001 10 nexthop-vrf nh_vrf vrf vrfRED tag 1", + "no ip route 10.1.3.0/24 10.0.0.59 PortChannel0002 20 vrf vrfRED tag 1", + "no ip route 10.1.3.0/24 10.0.0.61 PortChannel0003 30 nexthop-vrf default vrf vrfRED tag 1", ] ) @@ -674,20 +712,22 @@ def test_set_del_bgp_asn_change(): }), True, [ - "ip route 10.1.3.0/24 10.0.0.57 PortChannel0001 10 nexthop-vrf nh_vrf vrf vrfRED", - "ip route 10.1.3.0/24 10.0.0.59 PortChannel0002 20 vrf vrfRED", - "ip route 10.1.3.0/24 10.0.0.61 PortChannel0003 30 nexthop-vrf default vrf vrfRED", + "ip route 10.1.3.0/24 10.0.0.57 PortChannel0001 10 nexthop-vrf nh_vrf vrf vrfRED tag 1", + "ip route 10.1.3.0/24 10.0.0.59 PortChannel0002 20 vrf vrfRED tag 1", + "ip route 10.1.3.0/24 10.0.0.61 PortChannel0003 30 nexthop-vrf default vrf vrfRED tag 1", ] ) assert mgr.vrf_pending_redistribution == {"vrfRED"} expected_cmds = [ + "route-map STATIC_ROUTE_FILTER permit 10", + " match tag 1", "router bgp 65100 vrf vrfRED", " address-family ipv4", - " redistribute static", + " redistribute static route-map STATIC_ROUTE_FILTER", " address-family ipv6", - " redistribute static" + " redistribute static route-map STATIC_ROUTE_FILTER" ] def push_list(cmds): set_del_test.push_list_called = True @@ -705,3 +745,79 @@ def test_set_del_bgp_asn_change(): mgr.directory.put("CONFIG_DB", swsscommon.CFG_DEVICE_METADATA_TABLE_NAME, "localhost", {"bgp_asn": "65100"}) assert not mgr.vrf_pending_redistribution + +def test_set_tag_enable(): + mgr = constructor() + set_del_test( + mgr, + "SET", + ("10.1.0.0/24", { + "nexthop": "10.0.0.57","advertise":"true" + }), + True, + [ + "ip route 10.1.0.0/24 10.0.0.57 tag 1", + "route-map STATIC_ROUTE_FILTER permit 10", + " match tag 1", + "router bgp 65100", + " address-family ipv4", + " redistribute static route-map STATIC_ROUTE_FILTER", + " address-family ipv6", + " redistribute static route-map STATIC_ROUTE_FILTER" + ] + ) + +def test_set_tag_disable(): + mgr = constructor() + set_del_test( + mgr, + "SET", + ("10.1.0.0/24", { + "nexthop": "10.0.0.57","advertise":"false" + }), + True, + [ + "ip route 10.1.0.0/24 10.0.0.57 tag 2", + "route-map STATIC_ROUTE_FILTER permit 10", + " match tag 1", + "router bgp 65100", + " address-family ipv4", + " redistribute static route-map STATIC_ROUTE_FILTER", + " address-family ipv6", + " redistribute static route-map STATIC_ROUTE_FILTER" + ] + ) + +def test_set_tag_change(): + mgr = constructor() + set_del_test( + mgr, + "SET", + ("10.1.0.0/24", { + "nexthop": "10.0.0.57","advertise":"true" + }), + True, + [ + "ip route 10.1.0.0/24 10.0.0.57 tag 1", + "route-map STATIC_ROUTE_FILTER permit 10", + " match tag 1", + "router bgp 65100", + " address-family ipv4", + " redistribute static route-map STATIC_ROUTE_FILTER", + " address-family ipv6", + " redistribute static route-map STATIC_ROUTE_FILTER" + ] + ) + + set_del_test( + mgr, + "SET", + ("10.1.0.0/24", { + "nexthop": "10.0.0.57","advertise":"false" + }), + True, + [ + "no ip route 10.1.0.0/24 10.0.0.57 tag 1", + "ip route 10.1.0.0/24 10.0.0.57 tag 2", + ] + ) diff --git a/src/sonic-config-engine/minigraph.py b/src/sonic-config-engine/minigraph.py index 8d7f9551ae..e6f08f68c5 100644 --- a/src/sonic-config-engine/minigraph.py +++ b/src/sonic-config-engine/minigraph.py @@ -512,7 +512,8 @@ def parse_dpg(dpg, hname): elif ipnh.find(str(QName(ns, "Type"))).text == 'StaticRoute': prefix = ipnh.find(str(QName(ns, "AttachTo"))).text nexthop = ipnh.find(str(QName(ns, "Address"))).text - static_routes[prefix] = {'nexthop': nexthop } + advertise = ipnh.find(str(QName(ns, "Advertise"))).text + static_routes[prefix] = {'nexthop': nexthop, 'advertise': advertise} if port_nhipv4_map and port_nhipv6_map: subnet_check_ip = list(port_nhipv4_map.values())[0] diff --git a/src/sonic-config-engine/tests/sample-chassis-packet-lc-graph.xml b/src/sonic-config-engine/tests/sample-chassis-packet-lc-graph.xml index 26e6ccb932..65d435539d 100644 --- a/src/sonic-config-engine/tests/sample-chassis-packet-lc-graph.xml +++ b/src/sonic-config-engine/tests/sample-chassis-packet-lc-graph.xml @@ -124,6 +124,7 @@ 8.0.0.1/32
192.168.1.2,192.168.2.2
StaticRoute + false @@ -213,7 +214,8 @@ 8.0.0.1/32
192.168.1.2,192.168.2.2
- StaticRoute + StaticRoute + false diff --git a/src/sonic-config-engine/tests/test_cfggen.py b/src/sonic-config-engine/tests/test_cfggen.py index 77cce84ae3..cbef4bbe8e 100644 --- a/src/sonic-config-engine/tests/test_cfggen.py +++ b/src/sonic-config-engine/tests/test_cfggen.py @@ -904,14 +904,14 @@ class TestCfgGen(TestCase): output = self.run_script(argument) self.assertEqual( utils.to_dict(output.strip()), - utils.to_dict("{'8.0.0.1/32': {'nexthop': '192.168.1.2,192.168.2.2'}}") + utils.to_dict("{'8.0.0.1/32': {'nexthop': '192.168.1.2,192.168.2.2', 'advertise':'false'}}") ) argument = '-m "' + self.packet_chassis_graph + '" -p "' + self.packet_chassis_port_ini + '" -n "' + "asic1" + '" -v "STATIC_ROUTE"' output = self.run_script(argument) self.assertEqual( utils.to_dict(output.strip()), - utils.to_dict("{'8.0.0.1/32': {'nexthop': '192.168.1.2,192.168.2.2'}}") + utils.to_dict("{'8.0.0.1/32': {'nexthop': '192.168.1.2,192.168.2.2', 'advertise':'false'}}") ) def test_minigraph_bgp_packet_chassis_vlan_subintf(self):