Skip to content

Commit

Permalink
bpf: selftests: Restore netns after each test
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Martin KaFai Lau authored and Daniel Borkmann committed Jul 2, 2020
1 parent 99126ab commit 811d7e3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
23 changes: 21 additions & 2 deletions tools/testing/selftests/bpf/test_progs.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,24 @@ static void reset_affinity() {
}
}

static void save_netns(void)
{
env.saved_netns_fd = open("/proc/self/ns/net", O_RDONLY);
if (env.saved_netns_fd == -1) {
perror("open(/proc/self/ns/net)");
exit(-1);
}
}

static void restore_netns(void)
{
if (setns(env.saved_netns_fd, CLONE_NEWNET) == -1) {
stdio_restore();
perror("setns(CLONE_NEWNS)");
exit(-1);
}
}

void test__end_subtest()
{
struct prog_test_def *test = env.test;
Expand All @@ -138,8 +156,6 @@ void test__end_subtest()
test->test_num, test->subtest_num,
test->subtest_name, sub_error_cnt ? "FAIL" : "OK");

reset_affinity();

free(test->subtest_name);
test->subtest_name = NULL;
}
Expand Down Expand Up @@ -655,6 +671,7 @@ int main(int argc, char **argv)
return -1;
}

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

reset_affinity();
restore_netns();
if (test->need_cgroup_cleanup)
cleanup_cgroup_environment();
}
Expand All @@ -719,6 +737,7 @@ int main(int argc, char **argv)
free_str_set(&env.subtest_selector.blacklist);
free_str_set(&env.subtest_selector.whitelist);
free(env.subtest_selector.num_set);
close(env.saved_netns_fd);

if (env.succ_cnt + env.fail_cnt + env.skip_cnt == 0)
return EXIT_FAILURE;
Expand Down
2 changes: 2 additions & 0 deletions tools/testing/selftests/bpf/test_progs.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ struct test_env {
int sub_succ_cnt; /* successful sub-tests */
int fail_cnt; /* total failed tests + sub-tests */
int skip_cnt; /* skipped tests */

int saved_netns_fd;
};

extern struct test_env env;
Expand Down

0 comments on commit 811d7e3

Please sign in to comment.