From b62eb94dedd679c02bf6b8e8738befac0b4b5f8a Mon Sep 17 00:00:00 2001 From: Hua Liu <58683130+liuh-80@users.noreply.github.com> Date: Tue, 23 Jan 2024 01:59:12 +0800 Subject: [PATCH] Fix IPV6 forced-mgmt-route not work issue (#17299) ix IPV6 forced-mgmt-route not work issue Why I did it IPV6 forced-mgmt-route not work When add a IPV6 route, should use 'ip -6 rule add pref 32764 address' command, but currently in the template the '-6' parameter are missing, so the IPV6 route been add to IPV4 route table. Also this PR depends on #17281 , which will fix the IPV6 'default' route table missing in IPV6 route lookup issue. Microsoft ADO (number only):24719238 --- files/image_config/interfaces/interfaces.j2 | 4 +-- src/sonic-config-engine/minigraph.py | 30 +++++++++++++++++-- .../tests/sample_output/py2/mvrf_interfaces | 10 ++++--- .../tests/sample_output/py3/mvrf_interfaces | 10 ++++--- .../tests/t0-sample-graph-mvrf.xml | 2 +- 5 files changed, 43 insertions(+), 13 deletions(-) diff --git a/files/image_config/interfaces/interfaces.j2 b/files/image_config/interfaces/interfaces.j2 index 3702eb1f6798..a75b617f1ec5 100644 --- a/files/image_config/interfaces/interfaces.j2 +++ b/files/image_config/interfaces/interfaces.j2 @@ -86,7 +86,7 @@ iface {{ name }} {{ 'inet' if prefix | ipv4 else 'inet6' }} static up ip {{ '-4' if prefix | ipv4 else '-6' }} route add {{ prefix | network }}/{{ prefix | prefixlen }} dev {{ name }} table {{ vrf_table }} up ip {{ '-4' if prefix | ipv4 else '-6' }} rule add pref {{ force_mgmt_route_priority + 1 }} from {{ prefix | ip }}/{{ '32' if prefix | ipv4 else '128' }} table {{ vrf_table }} {% for route in MGMT_INTERFACE[(name, prefix)]['forced_mgmt_routes'] %} - up ip rule add pref {{ force_mgmt_route_priority }} to {{ route }} table {{ vrf_table }} + up ip {{ '-4' if prefix | ipv4 else '-6' }} rule add pref {{ force_mgmt_route_priority }} to {{ route }} table {{ vrf_table }} {% endfor %} {% if prefix | ipv6 and vrf_table == 'default'%} # IPV6 default table not add to lookup by default, management server need this to access IPV6 address when BGP shutdown @@ -97,7 +97,7 @@ iface {{ name }} {{ 'inet' if prefix | ipv4 else 'inet6' }} static pre-down ip {{ '-4' if prefix | ipv4 else '-6' }} route delete {{ prefix | network }}/{{ prefix | prefixlen }} dev {{ name }} table {{ vrf_table }} pre-down ip {{ '-4' if prefix | ipv4 else '-6' }} rule delete pref {{ force_mgmt_route_priority + 1 }} from {{ prefix | ip }}/{{ '32' if prefix | ipv4 else '128' }} table {{ vrf_table }} {% for route in MGMT_INTERFACE[(name, prefix)]['forced_mgmt_routes'] %} - pre-down ip rule delete pref {{ force_mgmt_route_priority }} to {{ route }} table {{ vrf_table }} + pre-down ip {{ '-4' if route | ipv4 else '-6' }} rule delete pref {{ force_mgmt_route_priority }} to {{ route }} table {{ vrf_table }} {% endfor %} {% if prefix | ipv6 and vrf_table == 'default'%} pre-down ip -6 rule delete pref {{ force_mgmt_route_priority + 3 }} lookup {{ vrf_table }} diff --git a/src/sonic-config-engine/minigraph.py b/src/sonic-config-engine/minigraph.py index ea89373dff04..4979e665b46c 100644 --- a/src/sonic-config-engine/minigraph.py +++ b/src/sonic-config-engine/minigraph.py @@ -1437,6 +1437,33 @@ def select_mmu_profiles(profile, platform, hwsku): base_file = os.path.join(path, file_item) exec_cmd(["sudo", "cp", file_in_dir, base_file]) +def address_type(address): + # encode and decode to unicode, because when address is bytes type, ip_network will throw AddressValueError + # set strict to False because address may set host bit, for example 192.168.0.1/24 + return type(ipaddress.ip_network(UNICODE_TYPE(address), False)) + +def update_forced_mgmt_route(mgmt_intf, mgmt_routes): + for mgmt_intf_key in mgmt_intf.keys(): + forced_mgmt_routes = [] + + try: + # get mgmt interface type + mgmt_intf_addr = mgmt_intf_key[1] + mgmt_iftype = address_type(mgmt_intf_addr) + + # add mgmt route to different mgmt interface by address type + for mgmt_route in mgmt_routes: + route_iftype = address_type(mgmt_route) + if mgmt_iftype == route_iftype: + forced_mgmt_routes.append(mgmt_route) + except ValueError as e: + print("Warning: invalid management routes in minigraph, exception: {}".format(e), file=sys.stderr) + continue + + # forced_mgmt_routes yang model not support empty list + if len(forced_mgmt_routes) > 0: + mgmt_intf[mgmt_intf_key]['forced_mgmt_routes'] = forced_mgmt_routes + ############################################################################### # # Main functions @@ -1677,8 +1704,7 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw results['BGP_INTERNAL_NEIGHBOR'] = bgp_internal_sessions results['BGP_VOQ_CHASSIS_NEIGHBOR'] = bgp_voq_chassis_sessions if mgmt_routes: - # TODO: differentiate v4 and v6 - next(iter(mgmt_intf.values()))['forced_mgmt_routes'] = mgmt_routes + update_forced_mgmt_route(mgmt_intf, mgmt_routes) results['MGMT_PORT'] = {} results['MGMT_INTERFACE'] = {} mgmt_intf_count = 0 diff --git a/src/sonic-config-engine/tests/sample_output/py2/mvrf_interfaces b/src/sonic-config-engine/tests/sample_output/py2/mvrf_interfaces index 1c33cfe819c6..518fbb0389a1 100644 --- a/src/sonic-config-engine/tests/sample_output/py2/mvrf_interfaces +++ b/src/sonic-config-engine/tests/sample_output/py2/mvrf_interfaces @@ -34,14 +34,14 @@ iface eth0 inet static up ip -4 route add default via 10.0.0.1 dev eth0 table 5000 metric 201 up ip -4 route add 10.0.0.0/24 dev eth0 table 5000 up ip -4 rule add pref 32765 from 10.0.0.100/32 table 5000 - up ip rule add pref 32764 to 11.11.11.11 table 5000 - up ip rule add pref 32764 to 22.22.22.0/23 table 5000 + up ip -4 rule add pref 32764 to 11.11.11.11 table 5000 + up ip -4 rule add pref 32764 to 22.22.22.0/23 table 5000 # management port down rules pre-down ip -4 route delete default via 10.0.0.1 dev eth0 table 5000 pre-down ip -4 route delete 10.0.0.0/24 dev eth0 table 5000 pre-down ip -4 rule delete pref 32765 from 10.0.0.100/32 table 5000 - pre-down ip rule delete pref 32764 to 11.11.11.11 table 5000 - pre-down ip rule delete pref 32764 to 22.22.22.0/23 table 5000 + pre-down ip -4 rule delete pref 32764 to 11.11.11.11 table 5000 + pre-down ip -4 rule delete pref 32764 to 22.22.22.0/23 table 5000 iface eth0 inet6 static address 2603:10e2:0:2902::8 netmask 64 @@ -53,10 +53,12 @@ iface eth0 inet6 static up ip -6 route add default via 2603:10e2:0:2902::1 dev eth0 table 5000 metric 201 up ip -6 route add 2603:10e2:0:2902::/64 dev eth0 table 5000 up ip -6 rule add pref 32765 from 2603:10e2:0:2902::8/128 table 5000 + up ip -6 rule add pref 32764 to 33:33:33::0/64 table 5000 # management port down rules pre-down ip -6 route delete default via 2603:10e2:0:2902::1 dev eth0 table 5000 pre-down ip -6 route delete 2603:10e2:0:2902::/64 dev eth0 table 5000 pre-down ip -6 rule delete pref 32765 from 2603:10e2:0:2902::8/128 table 5000 + pre-down ip -6 rule delete pref 32764 to 33:33:33::0/64 table 5000 # source /etc/network/interfaces.d/* # diff --git a/src/sonic-config-engine/tests/sample_output/py3/mvrf_interfaces b/src/sonic-config-engine/tests/sample_output/py3/mvrf_interfaces index 1c33cfe819c6..518fbb0389a1 100644 --- a/src/sonic-config-engine/tests/sample_output/py3/mvrf_interfaces +++ b/src/sonic-config-engine/tests/sample_output/py3/mvrf_interfaces @@ -34,14 +34,14 @@ iface eth0 inet static up ip -4 route add default via 10.0.0.1 dev eth0 table 5000 metric 201 up ip -4 route add 10.0.0.0/24 dev eth0 table 5000 up ip -4 rule add pref 32765 from 10.0.0.100/32 table 5000 - up ip rule add pref 32764 to 11.11.11.11 table 5000 - up ip rule add pref 32764 to 22.22.22.0/23 table 5000 + up ip -4 rule add pref 32764 to 11.11.11.11 table 5000 + up ip -4 rule add pref 32764 to 22.22.22.0/23 table 5000 # management port down rules pre-down ip -4 route delete default via 10.0.0.1 dev eth0 table 5000 pre-down ip -4 route delete 10.0.0.0/24 dev eth0 table 5000 pre-down ip -4 rule delete pref 32765 from 10.0.0.100/32 table 5000 - pre-down ip rule delete pref 32764 to 11.11.11.11 table 5000 - pre-down ip rule delete pref 32764 to 22.22.22.0/23 table 5000 + pre-down ip -4 rule delete pref 32764 to 11.11.11.11 table 5000 + pre-down ip -4 rule delete pref 32764 to 22.22.22.0/23 table 5000 iface eth0 inet6 static address 2603:10e2:0:2902::8 netmask 64 @@ -53,10 +53,12 @@ iface eth0 inet6 static up ip -6 route add default via 2603:10e2:0:2902::1 dev eth0 table 5000 metric 201 up ip -6 route add 2603:10e2:0:2902::/64 dev eth0 table 5000 up ip -6 rule add pref 32765 from 2603:10e2:0:2902::8/128 table 5000 + up ip -6 rule add pref 32764 to 33:33:33::0/64 table 5000 # management port down rules pre-down ip -6 route delete default via 2603:10e2:0:2902::1 dev eth0 table 5000 pre-down ip -6 route delete 2603:10e2:0:2902::/64 dev eth0 table 5000 pre-down ip -6 rule delete pref 32765 from 2603:10e2:0:2902::8/128 table 5000 + pre-down ip -6 rule delete pref 32764 to 33:33:33::0/64 table 5000 # source /etc/network/interfaces.d/* # diff --git a/src/sonic-config-engine/tests/t0-sample-graph-mvrf.xml b/src/sonic-config-engine/tests/t0-sample-graph-mvrf.xml index 3fd73f7369fe..996720ba2d7f 100644 --- a/src/sonic-config-engine/tests/t0-sample-graph-mvrf.xml +++ b/src/sonic-config-engine/tests/t0-sample-graph-mvrf.xml @@ -787,7 +787,7 @@ ForcedMgmtRoutes - 11.11.11.11;22.22.22.0/23 + 11.11.11.11;22.22.22.0/23;33:33:33::0/64 ErspanDestinationIpv4