Re: [PATCH net-next v2 2/2] selftests: net: add tcp_l3mdev_exempt_ports test

From: Kalash Nainwal

Date: Fri Nov 27 2026 - 17:15:43 EST


On Thu, Nov 26, 2026 at 2:20 PM <netdev-bot+sashiko@xxxxxxxxxx> wrote:
>
> Thank you for your contribution! Sashiko AI review found more severe
> issue(s) in other patches of this series. This mail is only an FYI, in case
> you want to address the nit picks below when you respin.
>
> Low priority / nit picks for your consideration:
>
> - [Low] The header comment of the new test script
> tools/testing/selftests/net/tcp_l3mdev_exempt_ports.sh (lines 8-9)…
> - [Low] In vrf_and_unbound_listeners() in
> tools/testing/selftests/net/tcp_l3mdev_exempt_ports.sh, the first…
>
> --
>
> selftests: net: add tcp_l3mdev_exempt_ports test
>
> This adds a selftest for net.ipv4.tcp_l3mdev_exempt_ports. With
> tcp_l3mdev_accept=1 and the port exempted, it checks that a VRF
> connection reaches the VRF-bound listener in either creation order and
> is refused by an unbound-only listener. It also checks that default-VRF
> connections still reach the unbound listener, for both IPv4 and IPv6.
>
> > diff --git a/tools/testing/selftests/net/tcp_l3mdev_exempt_ports.sh b/tools/testing/selftests/net/tcp_l3mdev_exempt_ports.sh
> > new file mode 100755
> > index 0000000000000..da7f20bf2e9e6
> > --- /dev/null
> > +++ b/tools/testing/selftests/net/tcp_l3mdev_exempt_ports.sh
> > @@ -0,0 +1,260 @@
> > +#!/bin/bash
> > +# SPDX-License-Identifier: GPL-2.0
> > +#
> > +# Test net.ipv4.tcp_l3mdev_exempt_ports.
> > +#
> > +# With tcp_l3mdev_accept=1, a listener that is not bound to a device also
> > +# matches connections that arrive in a VRF. For a port listed in
> > +# tcp_l3mdev_exempt_ports it must not: only a listener bound to the VRF
> > +# may take such a connection. Connections in the default VRF are not
> > +# affected.
>
> [Severity: Low]
> This isn't a bug, but is this comment narrower than what the kernel
> does?
>
> Documentation/networking/ip-sysctl.rst describes tcp_l3mdev_exempt_ports
> as:
>
> only a listen socket bound to the L3
> master device or to the receiving device can accept them.
>
> The check in compute_score() (net/ipv4/inet_hashtables.c, and its twin
> in net/ipv6/inet6_hashtables.c) only rejects sockets with no bound
> device:
>
> if (!sk->sk_bound_dev_if && sdif &&
> tcp_port_l3mdev_exempt(net, hnum))
> return -1;
>
> A listener bound to the slave device (veth_vrf here) still matches
> through the sdif comparison in inet_sk_bound_dev_eq().
>
> Could the comment say "bound to the VRF or to the receiving device"?
>
> Also, none of the cases uses a listener bound to the slave device, so
> that documented behaviour has no test coverage. Would it be worth adding
> one?
>
> [ ... ]
>
> > +# Start a server in the background. nettest sets SO_REUSEPORT on its
> > +# listeners, so an unbound and a VRF-bound listener can share the port.
> > +# $1: IP version, $2: "vrf" or "unbound"
> > +start_server()
> > +{
> > + local args=()
> > +
> > + [ "$1" = 6 ] && args+=(-6)
> > + [ "$2" = vrf ] && args+=(-I "$VRF")
> > + ip netns exec "$ns_s" nettest "${args[@]}" -s -p "$PORT" -t 3 -q &
> > +}
>
> [ ... ]
>
> > + for order in "unbound vrf" "vrf unbound"; do
> > + read -r first second <<< "$order"
> > + RET=0
> > +
> > + start_server "$ver" "$first"
> > + pid_first=$!
> > + wait_listeners 1
> > + check_err $? "first listener did not start"
> > + start_server "$ver" "$second"
> > + pid_second=$!
> > + wait_listeners 2
> > + check_err $? "second listener did not start"
> > +
> > + connect "$ns_c" "$ver" "$addr"
> > + check_err $? "VRF connection failed"
>
> [Severity: Low]
> Can the first server time out before the client's SYN arrives here?
>
> start_server() gives each nettest a fixed -t 3. In nettest.c, do_server()
> does a single select() with that budget right after listen(), and closes
> the listener when it expires:
>
> tools/testing/selftests/net/nettest.c:do_server() {
> ...
> rc = select(lsd+1, &rfds, NULL, NULL, ptval);
> if (rc == 0) {
> rc = 2;
> break;
> }
> ...
> }
>
> The first server's 3 second timer is already running while the script
> does all of this:
>
> - finishes the slowwait poll
> - starts the second server through ip netns exec
> - polls until wait_listeners 2 sees two listeners (up to 2s)
> - starts the client through ip netns exec
>
> On a slow or debug kernel (KASAN, lockdep) these steps can add up to
> about 3s.
>
> With order "vrf unbound", the VRF listener would then be gone. The
> exempt port makes the unbound listener refuse the connection, so the
> connect fails and rc_vrf is 2, even though the kernel behaved correctly.
>
> With order "unbound vrf", the unbound listener exits early. The
> check_timeout() on rc_unbound then passes trivially, and the subtest no
> longer shows that the unbound listener was skipped.
>
> If wait_listeners itself times out, check_err records it, but the
> subtest still goes on to connect.
>
> Would a server -t larger than the total wait budget avoid this?
>
> > +
> > + wait "$pid_first"
> > + rc_first=$?
> > + wait "$pid_second"
> > + rc_second=$?
>
> [ ... ]

Thanks. Both are fixed in v3. The header comment now says "bound to the
VRF or to the receiving device", and new cases cover a listener bound to
the VRF slave device. The listener that must not get the connection is
now stopped by the test after the client connects, instead of relying on
its timeout, so a slow setup cannot make it exit early.