public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
From: Stefano Brivio <sbrivio@redhat.com>
To: Aleksei Oladko <aleksey.oladko@virtuozzo.com>
Cc: "David S . Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>, Shuah Khan <shuah@kernel.org>,
	Aaron Conole <aconole@redhat.com>,
	Ilya Maximets <imaximet@redhat.com>,
	Eelco Chaudron <echaudro@redhat.com>,
	netdev@vger.kernel.org, linux-kselftest@vger.kernel.org,
	linux-kernel@vger.kernel.org, dev@openvswitch.org
Subject: Re: [PATCH net v4 2/5] selftests: net: run reuseport in an isolated netns
Date: Tue, 10 Mar 2026 17:38:39 +0100 (CET)	[thread overview]
Message-ID: <20260310173838.143871a1@elisabeth> (raw)
In-Reply-To: <20260309152013.565216-3-aleksey.oladko@virtuozzo.com>

Sorry for the late review, just one hint here (no changes requested):

On Mon,  9 Mar 2026 15:20:10 +0000
Aleksei Oladko <aleksey.oladko@virtuozzo.com> wrote:

> The reuseport_* tests (bpf, bpf_cpu, bpf_numa, dualstack) currently use
> a fixed port range. This can cause intermittent test failures when the
> ports are already in use by other services:
> 
>   failed to bind receive socket: Address already in use
> 
> To avoid conflicts, run these tests in separate network namespaces using
> unshare. Each test now has its own isolated network stack, preventing
> port collisions with the host services.
> 
> Signed-off-by: Aleksei Oladko <aleksey.oladko@virtuozzo.com>
> ---
>  tools/testing/selftests/net/reuseport_bpf.c       | 11 +++++++++++
>  tools/testing/selftests/net/reuseport_bpf_cpu.c   | 10 ++++++++++
>  tools/testing/selftests/net/reuseport_bpf_numa.c  | 10 ++++++++++
>  tools/testing/selftests/net/reuseport_dualstack.c | 11 +++++++++++
>  4 files changed, 42 insertions(+)
> 
> diff --git a/tools/testing/selftests/net/reuseport_bpf.c b/tools/testing/selftests/net/reuseport_bpf.c
> index b6634d6da3d6..12e48b97b862 100644
> --- a/tools/testing/selftests/net/reuseport_bpf.c
> +++ b/tools/testing/selftests/net/reuseport_bpf.c
> @@ -23,6 +23,7 @@
>  #include <sys/socket.h>
>  #include <sys/resource.h>
>  #include <unistd.h>
> +#include <sched.h>
>  
>  #include "kselftest.h"
>  
> @@ -455,8 +456,18 @@ static __attribute__((destructor)) void main_dtor(void)
>  	setrlimit(RLIMIT_MEMLOCK, &rlim_old);
>  }
>  
> +static void setup_netns(void)
> +{
> +	if (unshare(CLONE_NEWNET))
> +		error(1, errno, "failed to unshare netns");
> +	if (system("ip link set lo up"))
> +		error(1, 0, "failed to bring up lo interface in netns");

I don't have much against this approach, except that it needs root /
UID 0 (but these are BPF tests anyway, and that was also the case for
3/5 before), and that it's a bit ugly to invoke ip-link like that.

Anyway, should you or somebody else ever need a snippet that does this
as unprivileged user (giving CLONE_NEWUSER at the same time) and without
external tools or libraries, I made this a while ago:

  https://archives.passt.top/passt-dev/20231206160808.3d312733@elisabeth/

It detaches the network namespace, brings up 'lo' with a minimalistic
netlink implementation, and lets you do whatever you need there.

I guess ideally it could live under selftests/net/lib/ and be used from
C programs or as stand-alone tool for shell scripts as well.

I don't see myself polishing things up and proposing that in the
foreseeable future, but I thought maybe you're interested in it.

> +}
> +
>  int main(void)
>  {
> +	setup_netns();
> +
>  	fprintf(stderr, "---- IPv4 UDP ----\n");
>  	/* NOTE: UDP socket lookups traverse a different code path when there
>  	 * are > 10 sockets in a group.  Run the bpf test through both paths.
> diff --git a/tools/testing/selftests/net/reuseport_bpf_cpu.c b/tools/testing/selftests/net/reuseport_bpf_cpu.c
> index 2d646174729f..ddfe92f6597a 100644
> --- a/tools/testing/selftests/net/reuseport_bpf_cpu.c
> +++ b/tools/testing/selftests/net/reuseport_bpf_cpu.c
> @@ -228,10 +228,20 @@ static void test(int *rcv_fd, int len, int family, int proto)
>  		close(rcv_fd[cpu]);
>  }
>  
> +static void setup_netns(void)
> +{
> +	if (unshare(CLONE_NEWNET))
> +		error(1, errno, "failed to unshare netns");
> +	if (system("ip link set lo up"))
> +		error(1, 0, "failed to bring up lo interface in netns");
> +}
> +
>  int main(void)
>  {
>  	int *rcv_fd, cpus;
>  
> +	setup_netns();
> +
>  	cpus = sysconf(_SC_NPROCESSORS_ONLN);
>  	if (cpus <= 0)
>  		error(1, errno, "failed counting cpus");
> diff --git a/tools/testing/selftests/net/reuseport_bpf_numa.c b/tools/testing/selftests/net/reuseport_bpf_numa.c
> index 2ffd957ffb15..8ec52fc5ef41 100644
> --- a/tools/testing/selftests/net/reuseport_bpf_numa.c
> +++ b/tools/testing/selftests/net/reuseport_bpf_numa.c
> @@ -230,10 +230,20 @@ static void test(int *rcv_fd, int len, int family, int proto)
>  		close(rcv_fd[node]);
>  }
>  
> +static void setup_netns(void)
> +{
> +	if (unshare(CLONE_NEWNET))
> +		error(1, errno, "failed to unshare netns");
> +	if (system("ip link set lo up"))
> +		error(1, 0, "failed to bring up lo interface in netns");
> +}
> +
>  int main(void)
>  {
>  	int *rcv_fd, nodes;
>  
> +	setup_netns();
> +
>  	if (numa_available() < 0)
>  		ksft_exit_skip("no numa api support\n");
>  
> diff --git a/tools/testing/selftests/net/reuseport_dualstack.c b/tools/testing/selftests/net/reuseport_dualstack.c
> index fb7a59ed759e..0eaf739d0c85 100644
> --- a/tools/testing/selftests/net/reuseport_dualstack.c
> +++ b/tools/testing/selftests/net/reuseport_dualstack.c
> @@ -25,6 +25,7 @@
>  #include <sys/types.h>
>  #include <sys/socket.h>
>  #include <unistd.h>
> +#include <sched.h>
>  
>  static const int PORT = 8888;
>  
> @@ -156,10 +157,20 @@ static void test(int *rcv_fds, int count, int proto)
>  	close(epfd);
>  }
>  
> +static void setup_netns(void)
> +{
> +	if (unshare(CLONE_NEWNET))
> +		error(1, errno, "failed to unshare netns");
> +	if (system("ip link set lo up"))
> +		error(1, 0, "failed to bring up lo interface in netns");
> +}
> +
>  int main(void)
>  {
>  	int rcv_fds[32], i;
>  
> +	setup_netns();
> +
>  	fprintf(stderr, "---- UDP IPv4 created before IPv6 ----\n");
>  	build_rcv_fd(AF_INET, SOCK_DGRAM, rcv_fds, 5);
>  	build_rcv_fd(AF_INET6, SOCK_DGRAM, &(rcv_fds[5]), 5);

-- 
Stefano


  reply	other threads:[~2026-03-10 16:38 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-09 15:20 [PATCH net v4 0/5] selftests: net: fix false failures due to missing features and host interference Aleksei Oladko
2026-03-09 15:20 ` [PATCH net v4 1/5] selftests: net: make ovs-dpctl.py fail when pyroute2 is unsupported Aleksei Oladko
2026-03-11 12:48   ` Aaron Conole
2026-03-09 15:20 ` [PATCH net v4 2/5] selftests: net: run reuseport in an isolated netns Aleksei Oladko
2026-03-10 16:38   ` Stefano Brivio [this message]
2026-03-09 15:20 ` [PATCH net v4 3/5] selftests: net: rename pmtu.sh to pmtu-test.sh Aleksei Oladko
2026-03-10 16:38   ` Stefano Brivio
2026-03-12  3:10   ` Jakub Kicinski
2026-03-12  3:12     ` Jakub Kicinski
2026-03-15 19:25     ` Aleksei Oladko
2026-03-16 20:55       ` Stefano Brivio
2026-03-09 15:20 ` [PATCH net v4 4/5] selftests: net: run pmtu.sh in netns to avoid host firewall interference Aleksei Oladko
2026-03-09 15:20 ` [PATCH net v4 5/5] selftests: net: io_uring_zerocopy: enable io_uring for the test Aleksei Oladko
2026-03-12  3:08 ` [PATCH net v4 0/5] selftests: net: fix false failures due to missing features and host interference Jakub Kicinski

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=20260310173838.143871a1@elisabeth \
    --to=sbrivio@redhat.com \
    --cc=aconole@redhat.com \
    --cc=aleksey.oladko@virtuozzo.com \
    --cc=davem@davemloft.net \
    --cc=dev@openvswitch.org \
    --cc=echaudro@redhat.com \
    --cc=edumazet@google.com \
    --cc=horms@kernel.org \
    --cc=imaximet@redhat.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=shuah@kernel.org \
    /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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox