Path: blob/main/tests/sys/net/routing/test_routing.sh
289024 views
#1# Copyright (c) 2026 Pouria Mousavizadeh Tehrani <[email protected]>2#3# SPDX-License-Identifier: BSD-2-Clause4#56. $(atf_get_srcdir)/../../common/vnet.subr78jq_rtentry()9{10local route="$1"1112jq -r '.statistics."route-information"."route-table"."rt-family".[]."rt-entry".[] |13select(.destination == "'${route}'")'14}1516jq_nhop_filter()17{18local nhop="$1"19local weight="$2"20local metric="$3"2122jq -r 'select(.gateway == "'${nhop}'") |23select(.weight == '${weight}') |24select(.metric == '${metric}') |25.gateway'26}272829atf_test_case "add_lowest_metric" "cleanup"30add_lowest_metric_head()31{32atf_set descr 'Create 4 routes to same dst and verify the lowest metric wins'33atf_set require.user root34atf_set require.progs jq35}3637add_lowest_metric_body()38{39local epair laddr route nhop1 nhop2 nhop34041laddr="3fff::1"42route="3fff:a::"43nhop1="3fff::1"44nhop2="3fff::2"45nhop3="3fff::3"4647vnet_init48epair=$(vnet_mkepair)4950atf_check -o ignore \51ifconfig ${epair}a inet6 ${laddr} up5253# Create an ECMP route with metric 254atf_check -o ignore \55route -6 add -net ${route}/64 -gateway ${nhop2} -weight 10 -metric 256atf_check -o ignore \57route -6 add -net ${route}/64 -gateway ${nhop3} -weight 10 -metric 25859# Validate routes60atf_check -o save:netstat \61netstat -rn6 --libxo json62output=$(cat netstat | jq_rtentry ${route}/64 | jq_nhop_filter ${nhop2} 10 2)63atf_check_equal "$output" "$nhop2"64output=$(cat netstat | jq_rtentry ${route}/64 | jq_nhop_filter ${nhop3} 10 2)65atf_check_equal "$output" "$nhop3"6667# Create a route with metric 368atf_check -o ignore \69route -6 add -net ${route}/64 -gateway ${nhop1} -metric 370# Verify that nhop1 is not the best route71atf_check -o not-match:".*gateway: ${nhop1}.*" \72route -n6 get -net ${route}/647374# Create a route to the same nhop with same metric 3 and verify it fails75atf_check -s exit:1 -o ignore -e match:".*exists.*" \76route -6 add -net ${route}/64 -gateway ${nhop1} -metric 37778# Create a route to an existing nhop with lower metric79atf_check -o ignore \80route -6 add -net ${route}/64 -gateway ${nhop1} -metric 181# Verify that nhop1 is now the best route82atf_check -o match:".*gateway: ${nhop1}.*" \83route -n6 get -net ${route}/6484}8586add_lowest_metric_cleanup()87{88vnet_cleanup89}9091atf_test_case "add_default_metric" "cleanup"92add_default_metric_head()93{94atf_set descr 'Create a route and verify the default metric is set'95atf_set require.user root96atf_set require.progs jq97}9899add_default_metric_body()100{101local epair laddr route nhop1102103laddr="3fff::1"104route="3fff:a::"105nhop1="3fff::1"106107vnet_init108epair=$(vnet_mkepair)109110atf_check -o ignore \111ifconfig ${epair}a inet6 ${laddr} up112113# Create a route without specifying its metric114atf_check -o ignore \115route -6 add -net ${route}/64 -gateway ${nhop1}116117# Verify the route has the default metric of 1118atf_check -o save:netstat \119netstat -rn6 --libxo json120output=$(cat netstat | jq_rtentry ${route}/64 | jq_nhop_filter ${nhop1} 1 1)121atf_check_equal "$output" "$nhop1"122}123124add_default_metric_cleanup()125{126vnet_cleanup127}128129atf_test_case "delete_route_with_metric" "cleanup"130delete_route_with_metric_head()131{132atf_set descr 'Create multiple routes to same dst and delete routes with specific metric'133atf_set require.user root134atf_set require.progs jq135}136137delete_route_with_metric_body()138{139local epair laddr route nhop1 nhop2140141laddr="3fff::1"142route="3fff:a::"143nhop1="3fff::1"144nhop2="3fff::2"145146vnet_init147epair=$(vnet_mkepair)148149atf_check -o ignore \150ifconfig ${epair}a inet6 ${laddr} up151152# Create two groups of ECMP routes with metric 2 and 3, and153# another route with metric 4.154atf_check -o ignore \155route -6 add -net ${route}/64 -gateway ${nhop1} -metric 3156atf_check -o ignore \157route -6 add -net ${route}/64 -gateway ${nhop1} -weight 10 -metric 2158atf_check -o ignore \159route -6 add -net ${route}/64 -gateway ${nhop2} -weight 10 -metric 2160atf_check -o ignore \161route -6 add -net ${route}/64 -gateway ${nhop2} -metric 3162atf_check -o ignore \163route -6 add -net ${route}/64 -gateway ${nhop2} -metric 4164165# Validate we have 5 routes166atf_check -o save:netstat \167netstat -rn6 --libxo json168output=$(cat netstat | jq_rtentry ${route}/64 | jq_nhop_filter ${nhop1} 1 3)169atf_check_equal "$output" "$nhop1"170output=$(cat netstat | jq_rtentry ${route}/64 | jq_nhop_filter ${nhop1} 10 2)171atf_check_equal "$output" "$nhop1"172output=$(cat netstat | jq_rtentry ${route}/64 | jq_nhop_filter ${nhop2} 10 2)173atf_check_equal "$output" "$nhop2"174output=$(cat netstat | jq_rtentry ${route}/64 | jq_nhop_filter ${nhop2} 1 3)175atf_check_equal "$output" "$nhop2"176output=$(cat netstat | jq_rtentry ${route}/64 | jq_nhop_filter ${nhop2} 1 4)177atf_check_equal "$output" "$nhop2"178179# Delete one of the nexthops of them best ECMP route180# Test that deleting a route by specifying gateway + metric works.181atf_check -o ignore \182route -n6 delete -net ${route}/64 -gateway ${nhop2} -metric 2183184# Verify that nhop1 is the best route now185atf_check -o match:".*gateway: ${nhop1}.*" \186route -n6 get -net ${route}/64187188# But other route with nhops2 should exists.189atf_check -o save:netstat \190netstat -rn6 --libxo json191output=$(cat netstat | jq_rtentry ${route}/64 | jq_nhop_filter ${nhop2} 1 3)192atf_check_equal "$output" "$nhop2"193output=$(cat netstat | jq_rtentry ${route}/64 | jq_nhop_filter ${nhop2} 1 4)194atf_check_equal "$output" "$nhop2"195196# Delete routes with nhop1 as nexthop without specifying metric.197# Test that deleting a route by gateway removes all routes with198# that gateway, regardless of metric value.199atf_check -o ignore \200route -n6 delete -net ${route}/64 -gateway ${nhop1}201202# Verify that nhop2 is the best route now203atf_check -o match:".*gateway: ${nhop2}.*" \204route -n6 get -net ${route}/64205206# Delete routes with metric 3 without specifying their gateway.207# Test that deleting a route by metric removes all routes with208# that metric, regardless of gateway value.209atf_check -o ignore \210route -n6 delete -net ${route}/64 -metric 3211212# Verify that nhop2 is still the best route with metric of 4213atf_check -o match:".*gateway: ${nhop2}.*" \214route -n6 get -net ${route}/64215output=$(cat netstat | jq_rtentry ${route}/64 | jq_nhop_filter ${nhop2} 1 4)216atf_check_equal "$output" "$nhop2"217}218219delete_route_with_metric_cleanup()220{221vnet_cleanup222}223224225atf_init_test_cases()226{227atf_add_test_case "add_lowest_metric"228atf_add_test_case "add_default_metric"229atf_add_test_case "delete_route_with_metric"230}231232233