Skip to contentSkip to conversation

selftests/bpf: Restore the netns after flow dissector reattach test

PATCH In review Jakub Sitnicki 3 participants 4 messages Oct 17, 2019 – Oct 18, 2019

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/bpf: Restore the netns after flow dissector reattach test

flow_dissector_reattach test changes the netns we run in but does not restore it to the one we started in when finished. This interferes with tests that run after it. Fix it by restoring the netns when done.

Fixes f97eea1756f3 ("selftests/bpf: Check that flow dissector can be re-attached")
---
 .../bpf/prog_tests/flow_dissector_reattach.c  | 21 +++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c +17 -4
index 777faffc4639..1f51ba66b98b 100644
@@ -91,12 +91,18 @@ static void do_flow_dissector_reattach(void)
 
 void test_flow_dissector_reattach(void)
 {
-	int init_net, err;
+	int init_net, self_net, err;
+
+	self_net = open("/proc/self/ns/net", O_RDONLY);
+	if (CHECK_FAIL(self_net < 0)) {
+		perror("open(/proc/self/ns/net");
+		return;
+	}
 
 	init_net = open("/proc/1/ns/net", O_RDONLY);
 	if (CHECK_FAIL(init_net < 0)) {
 		perror("open(/proc/1/ns/net)");
-		return;
+		goto out_close;
 	}
 
 	err = setns(init_net, CLONE_NEWNET);
@@ -108,7 +114,7 @@ void test_flow_dissector_reattach(void)
 	if (is_attached(init_net)) {
 		test__skip();
 		printf("Can't test with flow dissector attached to init_net\n");
-		return;
+		goto out_setns;
 	}
 
 	/* First run tests in root network namespace */
@@ -118,10 +124,17 @@ void test_flow_dissector_reattach(void)
 	err = unshare(CLONE_NEWNET);
 	if (CHECK_FAIL(err)) {
 		perror("unshare(CLONE_NEWNET)");
-		goto out_close;
+		goto out_setns;
 	}
 	do_flow_dissector_reattach();
 
+out_setns:
+	/* Move back to netns we started in. */
+	err = setns(self_net, CLONE_NEWNET);
+	if (CHECK_FAIL(err))
+		perror("setns(/proc/self/ns/net)");
+
 out_close:
 	close(init_net);
+	close(self_net);
 }
Signature · 1 line
2.20.1
Acked-by on v1 1/1
#1 lore raw
Quoted 24 lines · Jakub Sitnicki

flow_dissector_reattach test changes the netns we run in but does not restore it to the one we started in when finished. This interferes with tests that run after it. Fix it by restoring the netns when done.

Fixes f97eea1756f3 ("selftests/bpf: Check that flow dissector can be re-attached")
---
 .../bpf/prog_tests/flow_dissector_reattach.c  | 21 +++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c +3 -1
index 777faffc4639..1f51ba66b98b 100644
@@ -91,12 +91,18 @@ static void do_flow_dissector_reattach(void)
 
 void test_flow_dissector_reattach(void)
 {
-	int init_net, err;
+	int init_net, self_net, err;
+
+	self_net = open("/proc/self/ns/net", O_RDONLY);

tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c static void do_flow_dissector_reattach(void) · line 105

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c +5 -1
+	if (CHECK_FAIL(self_net < 0)) {
+		perror("open(/proc/self/ns/net");
+		return;
+	}
 
 	init_net = open("/proc/1/ns/net", O_RDONLY);
 	if (CHECK_FAIL(init_net < 0)) {
 		perror("open(/proc/1/ns/net)");
-		return;
+		goto out_close;

Mostly nit. close(-1) is ok-ish... The same goes for the "out_close" in do_flow_dissector_reattach().

Quoted 34 lines
tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c +9 -2
 	}
 
 	err = setns(init_net, CLONE_NEWNET);
@@ -108,7 +114,7 @@ void test_flow_dissector_reattach(void)
 	if (is_attached(init_net)) {
 		test__skip();
 		printf("Can't test with flow dissector attached to init_net\n");
-		return;
+		goto out_setns;
 	}
 
 	/* First run tests in root network namespace */
@@ -118,10 +124,17 @@ void test_flow_dissector_reattach(void)
 	err = unshare(CLONE_NEWNET);
 	if (CHECK_FAIL(err)) {
 		perror("unshare(CLONE_NEWNET)");
-		goto out_close;
+		goto out_setns;
 	}
 	do_flow_dissector_reattach();
 
+out_setns:
+	/* Move back to netns we started in. */
+	err = setns(self_net, CLONE_NEWNET);
+	if (CHECK_FAIL(err))
+		perror("setns(/proc/self/ns/net)");
+
 out_close:
 	close(init_net);
+	close(self_net);
 }
Signature · 1 line
2.20.1
#2 lore raw
Quoted 39 lines · Martin Lau
Quoted 24 lines · Jakub Sitnicki

flow_dissector_reattach test changes the netns we run in but does not restore it to the one we started in when finished. This interferes with tests that run after it. Fix it by restoring the netns when done.

Fixes f97eea1756f3 ("selftests/bpf: Check that flow dissector can be re-attached")
---
 .../bpf/prog_tests/flow_dissector_reattach.c  | 21 +++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c +3 -1
index 777faffc4639..1f51ba66b98b 100644
@@ -91,12 +91,18 @@ static void do_flow_dissector_reattach(void)
 
 void test_flow_dissector_reattach(void)
 {
-     int init_net, err;
+     int init_net, self_net, err;
+
+     self_net = open("/proc/self/ns/net", O_RDONLY);

tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c static void do_flow_dissector_reattach(void) · line 105

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c +5 -1
+     if (CHECK_FAIL(self_net < 0)) {
+             perror("open(/proc/self/ns/net");
+             return;
+     }
 
      init_net = open("/proc/1/ns/net", O_RDONLY);
      if (CHECK_FAIL(init_net < 0)) {
              perror("open(/proc/1/ns/net)");
-             return;
+             goto out_close;

Mostly nit. close(-1) is ok-ish... The same goes for the "out_close" in do_flow_dissector_reattach().

Quoted 1 line

Yeah. Not ideal, but applied the fix to bpf-next
to get selftests working again.
Please follow up with any cleanups.
Thanks

#3 lore raw
In reply to Martin Lau
Quoted 1 line · Martin Lau

On Thu, Oct 17, 2019 at 10:37:52AM +0200, Jakub Sitnicki wrote:

[...]

Quoted 21 lines
Quoted 21 lines
---
 .../bpf/prog_tests/flow_dissector_reattach.c  | 21 +++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c +7 -1
index 777faffc4639..1f51ba66b98b 100644
@@ -91,12 +91,18 @@ static void do_flow_dissector_reattach(void)
 
 void test_flow_dissector_reattach(void)
 {
-	int init_net, err;
+	int init_net, self_net, err;
+
+	self_net = open("/proc/self/ns/net", O_RDONLY);
+	if (CHECK_FAIL(self_net < 0)) {
+		perror("open(/proc/self/ns/net");
+		return;
+	}
 
Quoted 7 lines

tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c static void do_flow_dissector_reattach(void) · line 105

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c +1 -1
 	init_net = open("/proc/1/ns/net", O_RDONLY);
 	if (CHECK_FAIL(init_net < 0)) {
 		perror("open(/proc/1/ns/net)");
-		return;
+		goto out_close;

Mostly nit. close(-1) is ok-ish... The same goes for the "out_close" in do_flow_dissector_reattach().

Happy to fix it up. Is your concern that calls to close invalid FDs clutter strace output or something else?

Quoted 2 lines

Thanks for the review.

-Jakub