[RFC next-net 3/3] selftests: fib_nexthops: test route flush with nexthop objects
From: Stephen Hemminger
Date: Fri Nov 20 2026 - 15:07:06 EST
fib_nexthops.sh exercises "ip nexthop flush" but never "ip route flush"
against routes using nexthop objects, which is how the dump and delete
paths drifted apart.
Add ipv4_route_flush and ipv6_route_flush: a single nexthop object, a
group, a blackhole object and, for IPv4, a route via an IPv6 nexthop
object, since each expands differently in the dump. Flush a batch mixing
nexthop object routes with an ordinary one, and check the nexthop objects
outlive the routes.
Also check what must keep failing: a delete naming the wrong id, and an
add combining an id with a nexthop spec.
Signed-off-by: Stephen Hemminger <stephen@xxxxxxxxxxxxxxxxxx>
---
tools/testing/selftests/net/fib_nexthops.sh | 126 ++++++++++++++++++++
1 file changed, 126 insertions(+)
diff --git a/tools/testing/selftests/net/fib_nexthops.sh b/tools/testing/selftests/net/fib_nexthops.sh
index adbec51917eb..501ed0fc05a8 100755
--- a/tools/testing/selftests/net/fib_nexthops.sh
+++ b/tools/testing/selftests/net/fib_nexthops.sh
@@ -29,6 +29,7 @@ IPV4_TESTS="
ipv4_large_grp
ipv4_large_res_grp
ipv4_compat_mode
+ ipv4_route_flush
ipv4_fdb_grp_fcnal
ipv4_fdb_port_fcnal
ipv4_mpath_select
@@ -44,6 +45,7 @@ IPV6_TESTS="
ipv6_large_grp
ipv6_large_res_grp
ipv6_compat_mode
+ ipv6_route_flush
ipv6_fdb_grp_fcnal
ipv6_fdb_port_fcnal
ipv6_mpath_select
@@ -2148,6 +2150,130 @@ ipv6_compat_mode()
sysctl_nexthop_compat_mode_set 1 "IPv6"
}
+ipv4_route_flush()
+{
+ echo
+ echo "IPv4 route flush with nexthop objects"
+ echo "-------------------------------------"
+
+ # "ip route flush" dumps the routes and sends each dumped message
+ # back as RTM_DELROUTE. With nexthop_compat_mode on, a dump of a
+ # route using a nexthop object carries RTA_NH_ID plus the legacy
+ # expansion of that nexthop, so the delete has to accept both.
+
+ run_cmd "$IP nexthop add id 601 via 172.16.1.2 dev veth1"
+ run_cmd "$IP nexthop add id 602 via 172.16.1.3 dev veth1"
+ run_cmd "$IP nexthop add id 610 group 601/602"
+ run_cmd "$IP nexthop add id 603 blackhole"
+ run_cmd "$IP -6 nexthop add id 604 via 2001:db8:91::2 dev veth1"
+
+ # single nexthop object: dump has RTA_NH_ID, RTA_GATEWAY, RTA_OIF
+ run_cmd "$IP ro add 172.16.101.0/24 nhid 601"
+ run_cmd "$IP ro flush 172.16.101.0/24"
+ log_test $? 0 "Flush route with single nexthop object"
+ check_route "172.16.101.0/24" ""
+ log_test $? 0 "Flushed single nexthop object route is gone"
+
+ # nexthop group: dump has RTA_NH_ID plus RTA_MULTIPATH
+ run_cmd "$IP ro add 172.16.102.0/24 nhid 610"
+ run_cmd "$IP ro flush 172.16.102.0/24"
+ log_test $? 0 "Flush route with nexthop group"
+ check_route "172.16.102.0/24" ""
+ log_test $? 0 "Flushed nexthop group route is gone"
+
+ # blackhole nexthop object: the dump reports the route as
+ # RTN_BLACKHOLE even though the FIB alias is unicast
+ run_cmd "$IP ro add 172.16.103.0/24 nhid 603"
+ run_cmd "$IP ro flush 172.16.103.0/24"
+ log_test $? 0 "Flush route with blackhole nexthop object"
+ check_route "172.16.103.0/24" ""
+ log_test $? 0 "Flushed blackhole nexthop object route is gone"
+
+ # IPv4 route via an IPv6 nexthop object expands to RTA_VIA
+ run_cmd "$IP ro add 172.16.104.0/24 nhid 604"
+ run_cmd "$IP ro flush 172.16.104.0/24"
+ log_test $? 0 "Flush route with cross-family nexthop object"
+ check_route "172.16.104.0/24" ""
+ log_test $? 0 "Flushed cross-family route is gone"
+
+ # flushing a mix in one batch must not disturb ordinary routes
+ run_cmd "$IP ro add 172.16.105.0/24 nhid 601"
+ run_cmd "$IP ro add 172.16.106.0/24 nhid 610"
+ run_cmd "$IP ro add 172.16.107.0/24 via 172.16.1.2 dev veth1"
+ run_cmd "$IP ro flush dev veth1 proto boot"
+ log_test $? 0 "Flush mixed batch of nexthop object and ordinary routes"
+
+ # the nexthop objects themselves must survive a route flush
+ run_cmd "$IP nexthop get id 601"
+ log_test $? 0 "Nexthop object survives route flush"
+ run_cmd "$IP nexthop get id 610"
+ log_test $? 0 "Nexthop group survives route flush"
+
+ # deleting by nexthop id must still ignore a conflicting selector
+ # rather than silently deleting the wrong route
+ run_cmd "$IP ro add 172.16.108.0/24 nhid 601"
+ run_cmd "$IP ro del 172.16.108.0/24 nhid 602"
+ log_test $? 2 "Delete by wrong nexthop id fails"
+ check_route "172.16.108.0/24" "172.16.108.0/24 nhid 601 via 172.16.1.2 dev veth1"
+ log_test $? 0 "Route with other nexthop id is retained"
+ run_cmd "$IP ro del 172.16.108.0/24 nhid 601"
+ log_test $? 0 "Delete by correct nexthop id succeeds"
+
+ # adding a route must still reject nhid together with a nexthop spec
+ run_cmd "$IP ro add 172.16.109.0/24 nhid 601 via 172.16.1.2 dev veth1"
+ log_test $? 2 "Add rejects nexthop id with nexthop specification"
+
+ $IP nexthop flush >/dev/null 2>&1
+ $IP ro flush dev veth1 proto boot >/dev/null 2>&1
+}
+
+ipv6_route_flush()
+{
+ echo
+ echo "IPv6 route flush with nexthop objects"
+ echo "-------------------------------------"
+
+ run_cmd "$IP -6 nexthop add id 611 via 2001:db8:91::2 dev veth1"
+ run_cmd "$IP -6 nexthop add id 612 via 2001:db8:91::3 dev veth1"
+ run_cmd "$IP nexthop add id 620 group 611/612"
+ run_cmd "$IP -6 nexthop add id 613 blackhole"
+
+ run_cmd "$IP -6 ro add 2001:db8:101::/64 nhid 611"
+ run_cmd "$IP -6 ro flush 2001:db8:101::/64"
+ log_test $? 0 "Flush IPv6 route with single nexthop object"
+ check_route6 "2001:db8:101::/64" ""
+ log_test $? 0 "Flushed IPv6 single nexthop object route is gone"
+
+ run_cmd "$IP -6 ro add 2001:db8:102::/64 nhid 620"
+ run_cmd "$IP -6 ro flush 2001:db8:102::/64"
+ log_test $? 0 "Flush IPv6 route with nexthop group"
+ check_route6 "2001:db8:102::/64" ""
+ log_test $? 0 "Flushed IPv6 nexthop group route is gone"
+
+ run_cmd "$IP -6 ro add 2001:db8:103::/64 nhid 613"
+ run_cmd "$IP -6 ro flush 2001:db8:103::/64"
+ log_test $? 0 "Flush IPv6 route with blackhole nexthop object"
+ check_route6 "2001:db8:103::/64" ""
+ log_test $? 0 "Flushed IPv6 blackhole nexthop object route is gone"
+
+ run_cmd "$IP -6 nexthop get id 611"
+ log_test $? 0 "IPv6 nexthop object survives route flush"
+ run_cmd "$IP nexthop get id 620"
+ log_test $? 0 "IPv6 nexthop group survives route flush"
+
+ run_cmd "$IP -6 ro add 2001:db8:104::/64 nhid 611"
+ run_cmd "$IP -6 ro del 2001:db8:104::/64 nhid 612"
+ log_test $? 2 "Delete IPv6 route by wrong nexthop id fails"
+ run_cmd "$IP -6 ro del 2001:db8:104::/64 nhid 611"
+ log_test $? 0 "Delete IPv6 route by correct nexthop id succeeds"
+
+ run_cmd "$IP -6 ro add 2001:db8:105::/64 nhid 611 via 2001:db8:91::2 dev veth1"
+ log_test $? 2 "Add rejects IPv6 nexthop id with nexthop specification"
+
+ $IP nexthop flush >/dev/null 2>&1
+ $IP -6 ro flush dev veth1 proto boot >/dev/null 2>&1
+}
+
ipv4_compat_mode()
{
local rc
--
2.53.0