Skip to content

Commit 811d7e3

Browse files
iamkafaiborkmann
authored andcommitted
bpf: selftests: Restore netns after each test
It is common for networking tests creating its netns and making its own setting under this new netns (e.g. changing tcp sysctl). If the test forgot to restore to the original netns, it would affect the result of other tests. This patch saves the original netns at the beginning and then restores it after every test. Since the restore "setns()" is not expensive, it does it on all tests without tracking if a test has created a new netns or not. The new restore_netns() could also be done in test__end_subtest() such that each subtest will get an automatic netns reset. However, the individual test would lose flexibility to have total control on netns for its own subtests. In some cases, forcing a test to do unnecessary netns re-configure for each subtest is time consuming. e.g. In my vm, forcing netns re-configure on each subtest in sk_assign.c increased the runtime from 1s to 8s. On top of that, test_progs.c is also doing per-test (instead of per-subtest) cleanup for cgroup. Thus, this patch also does per-test restore_netns(). The only existing per-subtest cleanup is reset_affinity() and no test is depending on this. Thus, it is removed from test__end_subtest() to give a consistent expectation to the individual tests. test_progs.c only ensures any affinity/netns/cgroup change made by an earlier test does not affect the following tests. Signed-off-by: Martin KaFai Lau <kafai@fb.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Andrii Nakryiko <andriin@fb.com> Link: https://lore.kernel.org/bpf/20200702004858.2103728-1-kafai@fb.com
1 parent 99126ab commit 811d7e3

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

‎tools/testing/selftests/bpf/test_progs.c‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,24 @@ static void reset_affinity() {
121121
}
122122
}
123123

124+
static void save_netns(void)
125+
{
126+
env.saved_netns_fd = open("/proc/self/ns/net", O_RDONLY);
127+
if (env.saved_netns_fd == -1) {
128+
perror("open(/proc/self/ns/net)");
129+
exit(-1);
130+
}
131+
}
132+
133+
static void restore_netns(void)
134+
{
135+
if (setns(env.saved_netns_fd, CLONE_NEWNET) == -1) {
136+
stdio_restore();
137+
perror("setns(CLONE_NEWNS)");
138+
exit(-1);
139+
}
140+
}
141+
124142
void test__end_subtest()
125143
{
126144
struct prog_test_def *test = env.test;
@@ -138,8 +156,6 @@ void test__end_subtest()
138156
test->test_num, test->subtest_num,
139157
test->subtest_name, sub_error_cnt ? "FAIL" : "OK");
140158

141-
reset_affinity();
142-
143159
free(test->subtest_name);
144160
test->subtest_name = NULL;
145161
}
@@ -655,6 +671,7 @@ int main(int argc, char **argv)
655671
return -1;
656672
}
657673

674+
save_netns();
658675
stdio_hijack();
659676
for (i = 0; i < prog_test_cnt; i++) {
660677
struct prog_test_def *test = &prog_test_defs[i];
@@ -696,6 +713,7 @@ int main(int argc, char **argv)
696713
test->error_cnt ? "FAIL" : "OK");
697714

698715
reset_affinity();
716+
restore_netns();
699717
if (test->need_cgroup_cleanup)
700718
cleanup_cgroup_environment();
701719
}
@@ -719,6 +737,7 @@ int main(int argc, char **argv)
719737
free_str_set(&env.subtest_selector.blacklist);
720738
free_str_set(&env.subtest_selector.whitelist);
721739
free(env.subtest_selector.num_set);
740+
close(env.saved_netns_fd);
722741

723742
if (env.succ_cnt + env.fail_cnt + env.skip_cnt == 0)
724743
return EXIT_FAILURE;

‎tools/testing/selftests/bpf/test_progs.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ struct test_env {
8080
int sub_succ_cnt; /* successful sub-tests */
8181
int fail_cnt; /* total failed tests + sub-tests */
8282
int skip_cnt; /* skipped tests */
83+
84+
int saved_netns_fd;
8385
};
8486

8587
extern struct test_env env;

0 commit comments

Comments
 (0)