mirror of https://gitee.com/openkylin/linux.git
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
This commit is contained in:
parent
99126abec5
commit
811d7e375d
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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];
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue