aboutgitcodebugslistschat
diff options
context:
space:
mode:
authorBenjamin Schubert <bschubert15@bloomberg.net>2026-12-01 14:07:58 +0000
committerStefano Brivio <sbrivio@redhat.com>2026-12-02 22:36:25 +0100
commit05d3780b82d858041f982a276db0de7167f8b661 (patch)
tree290f7ebc1523cb7d955f7bfc2d142eb5ad0a1d81
parent14b5f2b4fb561efee7e2bad55c4014c8ca76d7db (diff)
downloadpasst-05d3780b82d858041f982a276db0de7167f8b661.tar
passt-05d3780b82d858041f982a276db0de7167f8b661.tar.gz
passt-05d3780b82d858041f982a276db0de7167f8b661.tar.bz2
passt-05d3780b82d858041f982a276db0de7167f8b661.tar.lz
passt-05d3780b82d858041f982a276db0de7167f8b661.tar.xz
passt-05d3780b82d858041f982a276db0de7167f8b661.tar.zst
passt-05d3780b82d858041f982a276db0de7167f8b661.zip
netlink: Fix gateway selection for multipath default routes
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 nexthop is not a gateway candidate, 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> [sbrivio: Fix typos in commit message] Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
-rw-r--r--netlink.c7
-rw-r--r--test/pasta_options/multipath_gateway97
-rwxr-xr-xtest/run1
3 files changed, 101 insertions, 4 deletions
diff --git a/netlink.c b/netlink.c
index 8b16ebc..37c55cc 100644
--- a/netlink.c
+++ b/netlink.c
@@ -362,7 +362,7 @@ unsigned int nl_get_ext_if(int s, sa_family_t af)
}
/**
- * 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
*
@@ -380,11 +380,9 @@ static bool nl_route_get_def_multipath(struct rtattr *rta, void *gw)
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)) {
@@ -392,6 +390,7 @@ static bool nl_route_get_def_multipath(struct rtattr *rta, void *gw)
continue;
memcpy(gw, RTA_DATA(rta_inner), RTA_PAYLOAD(rta_inner));
+ hops = rtnh->rtnh_hops;
found = true;
}
}
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
@@ -88,6 +88,7 @@ run() {
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