All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf] selftests: bpf: Fix bind on used port
@ 2022-01-17 14:07 Felix Maurer
  2022-01-17 18:57 ` Jakub Sitnicki
  0 siblings, 1 reply; 2+ messages in thread
From: Felix Maurer @ 2022-01-17 14:07 UTC (permalink / raw)
  To: bpf; +Cc: sdf, kafai, ast

The bind_perm BPF selftest failed when port 111/tcp was already in use
during the test. To fix this, the test now runs in its own network name
space.

To use unshare, it is necessary to reorder the includes. The style of
the includes is adapted to be consistent with the other prog_tests.

Fixes: 8259fdeb30326 ("selftests/bpf: Verify that rebinding to port < 1024 from BPF works")
Signed-off-by: Felix Maurer <fmaurer@redhat.com>
---
 .../selftests/bpf/prog_tests/bind_perm.c      | 22 ++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/bind_perm.c b/tools/testing/selftests/bpf/prog_tests/bind_perm.c
index d0f06e40c16d..cbd739d36e4d 100644
--- a/tools/testing/selftests/bpf/prog_tests/bind_perm.c
+++ b/tools/testing/selftests/bpf/prog_tests/bind_perm.c
@@ -1,13 +1,26 @@
 // SPDX-License-Identifier: GPL-2.0
-#include <test_progs.h>
-#include "bind_perm.skel.h"
-
+#define _GNU_SOURCE
+#include <sched.h>
+#include <stdlib.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/capability.h>
 
+#include "test_progs.h"
+#include "bind_perm.skel.h"
+
 static int duration;
 
+static int create_netns(void)
+{
+	if (CHECK(unshare(CLONE_NEWNET), "create netns",
+		  "unshare(CLONE_NEWNET): %s (%d)",
+		  strerror(errno), errno))
+		return -1;
+
+	return 0;
+}
+
 void try_bind(int family, int port, int expected_errno)
 {
 	struct sockaddr_storage addr = {};
@@ -75,6 +88,9 @@ void test_bind_perm(void)
 	struct bind_perm *skel;
 	int cgroup_fd;
 
+	if (create_netns())
+		return;
+
 	cgroup_fd = test__join_cgroup("/bind_perm");
 	if (CHECK(cgroup_fd < 0, "cg-join", "errno %d", errno))
 		return;
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH bpf] selftests: bpf: Fix bind on used port
  2022-01-17 14:07 [PATCH bpf] selftests: bpf: Fix bind on used port Felix Maurer
@ 2022-01-17 18:57 ` Jakub Sitnicki
  0 siblings, 0 replies; 2+ messages in thread
From: Jakub Sitnicki @ 2022-01-17 18:57 UTC (permalink / raw)
  To: Felix Maurer; +Cc: bpf, sdf, kafai, ast

On Mon, Jan 17, 2022 at 03:07 PM CET, Felix Maurer wrote:
> The bind_perm BPF selftest failed when port 111/tcp was already in use
> during the test. To fix this, the test now runs in its own network name
> space.
>
> To use unshare, it is necessary to reorder the includes. The style of
> the includes is adapted to be consistent with the other prog_tests.
>
> Fixes: 8259fdeb30326 ("selftests/bpf: Verify that rebinding to port < 1024 from BPF works")
> Signed-off-by: Felix Maurer <fmaurer@redhat.com>
> ---
>  .../selftests/bpf/prog_tests/bind_perm.c      | 22 ++++++++++++++++---
>  1 file changed, 19 insertions(+), 3 deletions(-)
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/bind_perm.c b/tools/testing/selftests/bpf/prog_tests/bind_perm.c
> index d0f06e40c16d..cbd739d36e4d 100644
> --- a/tools/testing/selftests/bpf/prog_tests/bind_perm.c
> +++ b/tools/testing/selftests/bpf/prog_tests/bind_perm.c
> @@ -1,13 +1,26 @@
>  // SPDX-License-Identifier: GPL-2.0
> -#include <test_progs.h>
> -#include "bind_perm.skel.h"
> -
> +#define _GNU_SOURCE
> +#include <sched.h>
> +#include <stdlib.h>
>  #include <sys/types.h>
>  #include <sys/socket.h>
>  #include <sys/capability.h>
>  
> +#include "test_progs.h"
> +#include "bind_perm.skel.h"
> +
>  static int duration;
>  
> +static int create_netns(void)
> +{
> +	if (CHECK(unshare(CLONE_NEWNET), "create netns",
> +		  "unshare(CLONE_NEWNET): %s (%d)",
> +		  strerror(errno), errno))

CHECK() macro is deprecated. See [1].

[1] https://lore.kernel.org/bpf/20220107221115.326171-2-toke@redhat.com/

[...]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-01-17 18:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-17 14:07 [PATCH bpf] selftests: bpf: Fix bind on used port Felix Maurer
2022-01-17 18:57 ` Jakub Sitnicki

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.