From: Benjamin Schubert <contact@benschubert.me>
To: "passt-dev@passt.top" <passt-dev@passt.top>
Subject: [PATCH] netlink: Fix gateway selection for multipath default routes
Date: Tue, 01 Dec 2026 14:07:58 +0000 [thread overview]
Message-ID: <QIr6uPC0-xe6v19HEpGrvgm6LtBk80iLbC3tg_oDczvPfKn2vONGGmYapurX-fEeS-MHBlTBjlkIs3NM6EZp5qWUHwKXSWpVgD_zWv6oJqs=@benschubert.me> (raw)
From: Benjamin Schubert <bschubert15@bloomberg.net>
The documentation of `--gateway` in `passt.1` states that
for a multipath default route, the first nexthop router returned
by the kernel with the highest weight is selected.
`nl_route_get_def_multipath()` however allowed a subsequent route with
the same weight to override the nexthop, so it was the last gateway
with a nexthop that would actually be chosen.
This aligns the code with the documentation, by fixing the code to
only select a new gateway if it has strictly a higher weight.
Also update the selected weight only after finding a gateway.
Since a device-only enxthop is not a gateway cnadidate, it must
not prevent a subsequent gateway from being selected.
Add 7 tests for various ipv4 and ipv6 scenarios with multipath
gateways. Note that ipv6 with no default gateway is not accepted by
the kernel, and thus ipv6 has one less test.
All 7 tests fail before this change.
Link: https://bugs.passt.top/show_bug.cgi?id=230
Signed-off-by: Benjamin Schubert <bschubert15@bloomberg.net>
---
netlink.c | 7 +-
test/pasta_options/multipath_gateway | 97 ++++++++++++++++++++++++++++
test/run | 1 +
3 files changed, 101 insertions(+), 4 deletions(-)
create mode 100644 test/pasta_options/multipath_gateway
diff --git a/netlink.c b/netlink.c
index 8b16ebc..37c55cc 100644
--- a/netlink.c
+++ b/netlink.c
@@ -360,11 +360,11 @@ unsigned int nl_get_ext_if(int s, sa_family_t af)
return 0;
}
/**
- * nl_route_get_def_multipath() - Get lowest-weight route from nexthop list
+ * nl_route_get_def_multipath() - Get first highest-weight route from nexthop list
* @rta: Routing netlink attribute with type RTA_MULTIPATH
* @gw: Default gateway to fill
*
* Return: true if a gateway was found, false otherwise
*/
@@ -378,22 +378,21 @@ static bool nl_route_get_def_multipath(struct rtattr *rta, void *gw)
for (rtnh = (struct rtnexthop *)RTA_DATA(rta);
RTNH_OK(rtnh, nh_len); rtnh = RTNH_NEXT_AND_DEC(rtnh, nh_len)) {
size_t len = rtnh->rtnh_len - sizeof(*rtnh);
struct rtattr *rta_inner;
- if (rtnh->rtnh_hops < hops)
+ if (rtnh->rtnh_hops <= hops)
continue;
- hops = rtnh->rtnh_hops;
-
for (rta_inner = RTNH_DATA(rtnh); RTA_OK(rta_inner, len);
rta_inner = RTA_NEXT(rta_inner, len)) {
if (rta_inner->rta_type != RTA_GATEWAY)
continue;
memcpy(gw, RTA_DATA(rta_inner), RTA_PAYLOAD(rta_inner));
+ hops = rtnh->rtnh_hops;
found = true;
}
}
return found;
diff --git a/test/pasta_options/multipath_gateway b/test/pasta_options/multipath_gateway
new file mode 100644
index 0000000..f7bd45d
--- /dev/null
+++ b/test/pasta_options/multipath_gateway
@@ -0,0 +1,97 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# PASST - Plug A Simple Socket Transport
+# for qemu/UNIX domain socket mode
+#
+# PASTA - Pack A Subtle Tap Abstraction
+# for network namespace/tap device mode
+#
+# test/pasta_options/multipath_gateway - Check gateway weights and ties
+
+htools ip unshare sed grep
+
+set OUT __STATEDIR__/multipath-gateway.out
+set ERR __STATEDIR__/multipath-gateway.err
+
+def ipv4
+set FAMILY 4
+set PREFIX 192.0.2.
+set ADDRESS 192.0.2.10/24
+set LINKLOCAL true
+set ADDRESS_SETUP ip -__FAMILY__ address add __ADDRESS__ dev dummy0; __LINKLOCAL__
+endef
+
+def ipv6
+set FAMILY 6
+set PREFIX 2001:db8:1::
+set ADDRESS 2001:db8:1::10/64
+set LINKLOCAL ip -6 addr add fe80::10/64 dev dummy0 nodad
+set ADDRESS_SETUP ip -__FAMILY__ address add __ADDRESS__ dev dummy0 nodad; __LINKLOCAL__
+endef
+
+# Keep each invocation in its own user/network namespace
+# Reuse unshare's user namespace instead of creating another with pasta
+def check_gateway
+set LOG __STATEDIR__/ipv__FAMILY__-__CASE__.log
+set LINK_SETUP ip link set lo up; ip link add dummy0 type dummy; ip link set dummy0 up
+set ROUTE_SETUP ip -__FAMILY__ route add default __NEXTHOPS__
+set ROUTE_SHOW ip -__FAMILY__ route show default
+set NETWORK_SHOW ip -__FAMILY__ address show dev dummy0; ip -__FAMILY__ route show table all
+set SETUP __LINK_SETUP__; __ADDRESS_SETUP__; __NETWORK_SHOW__; __ROUTE_SETUP__
+set PASTA ./pasta --netns-only --ipv__FAMILY__-only --debug --interface dummy0
+hout STATUS unshare --map-root-user --net -- sh -ec '__SETUP__; __ROUTE_SHOW__; exec __PASTA__ -- true' >'__LOG__' 2>&1; echo \$?
+host cat '__LOG__'
+check [ '__STATUS__' = '0' ]
+hout ACTUAL sed --quiet 's/.*router: *//p' '__LOG__'
+info Gateway: expected __EXPECTED__, actual __ACTUAL__
+check [ '__ACTUAL__' = '__EXPECTED__' ]
+endef
+
+test IPv4: first gateway wins an equal-weight tie
+set CASE equal
+ipv4
+set NEXTHOPS nexthop via __PREFIX__1 dev dummy0 weight 1 nexthop via __PREFIX__2 dev dummy0 weight 1
+set EXPECTED __PREFIX__1
+check_gateway
+
+test IPv6: first gateway wins an equal-weight tie
+set CASE equal
+ipv6
+set NEXTHOPS nexthop via __PREFIX__1 dev dummy0 weight 1 nexthop via __PREFIX__2 dev dummy0 weight 1
+set EXPECTED __PREFIX__1
+check_gateway
+
+test IPv4: first gateway with the highest weight wins
+set CASE highest
+ipv4
+set NEXTHOPS nexthop via __PREFIX__1 dev dummy0 weight 1 nexthop via __PREFIX__2 dev dummy0 weight 3 nexthop via __PREFIX__3 dev dummy0 weight 3
+set EXPECTED __PREFIX__2
+check_gateway
+
+test IPv6: first gateway with the highest weight wins
+set CASE highest
+ipv6
+set NEXTHOPS nexthop via __PREFIX__1 dev dummy0 weight 1 nexthop via __PREFIX__2 dev dummy0 weight 3 nexthop via __PREFIX__3 dev dummy0 weight 3
+set EXPECTED __PREFIX__2
+check_gateway
+
+test IPv4: lower-weight gateway does not replace the selected gateway
+set CASE lower
+ipv4
+set NEXTHOPS nexthop via __PREFIX__1 dev dummy0 weight 3 nexthop via __PREFIX__2 dev dummy0 weight 1
+set EXPECTED __PREFIX__1
+check_gateway
+
+test IPv6: lower-weight gateway does not replace the selected gateway
+set CASE lower
+ipv6
+set NEXTHOPS nexthop via __PREFIX__1 dev dummy0 weight 3 nexthop via __PREFIX__2 dev dummy0 weight 1
+set EXPECTED __PREFIX__1
+check_gateway
+
+test IPv4: gateway-less nexthop does not mask an equal-weight gateway
+set CASE direct
+ipv4
+set NEXTHOPS nexthop dev dummy0 weight 1 nexthop via __PREFIX__2 dev dummy0 weight 1
+set EXPECTED __PREFIX__2
+check_gateway
diff --git a/test/run b/test/run
index 751ff50..fe9f177 100755
--- a/test/run
+++ b/test/run
@@ -86,10 +86,11 @@ run() {
setup pasta_options
test pasta_options/log_to_file
test pasta_options/netns_only
test pasta_options/no_pidns
test pasta_options/dns_host_follow
+ test pasta_options/multipath_gateway
teardown pasta_options
setup build
test pasta_podman/bats
teardown build
--
2.56.1
reply other threads:[~2026-12-01 14:08 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='QIr6uPC0-xe6v19HEpGrvgm6LtBk80iLbC3tg_oDczvPfKn2vONGGmYapurX-fEeS-MHBlTBjlkIs3NM6EZp5qWUHwKXSWpVgD_zWv6oJqs=@benschubert.me' \
--to=contact@benschubert.me \
--cc=passt-dev@passt.top \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).