[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAEf4BzZ3Jb296zJ7bfsntk7v5fkynrBcKncGQgrSHJ2zqifgsA@mail.gmail.com>
Date: Fri, 26 Jun 2020 15:45:04 -0700
From: Andrii Nakryiko <andrii.nakryiko@...il.com>
To: Martin KaFai Lau <kafai@...com>
Cc: bpf <bpf@...r.kernel.org>, Alexei Starovoitov <ast@...nel.org>,
Daniel Borkmann <daniel@...earbox.net>,
Eric Dumazet <edumazet@...gle.com>,
Kernel Team <kernel-team@...com>,
Lawrence Brakmo <brakmo@...com>,
Neal Cardwell <ncardwell@...gle.com>,
Networking <netdev@...r.kernel.org>,
Yuchung Cheng <ycheng@...gle.com>
Subject: Re: [PATCH bpf-next 07/10] bpf: selftests: Restore netns after each test
On Fri, Jun 26, 2020 at 10:56 AM Martin KaFai Lau <kafai@...com> wrote:
>
> 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.
>
> Signed-off-by: Martin KaFai Lau <kafai@...com>
> ---
> tools/testing/selftests/bpf/test_progs.c | 21 +++++++++++++++++++++
> tools/testing/selftests/bpf/test_progs.h | 2 ++
> 2 files changed, 23 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c
> index 54fa5fa688ce..b521ce366381 100644
> --- a/tools/testing/selftests/bpf/test_progs.c
> +++ b/tools/testing/selftests/bpf/test_progs.c
> @@ -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;
> @@ -643,6 +661,7 @@ int main(int argc, char **argv)
> return -1;
> }
>
> + save_netns();
you should probably do this also after each sub-test in test__end_subtest()?
Otherwise everything looks good.
> stdio_hijack();
> for (i = 0; i < prog_test_cnt; i++) {
> struct prog_test_def *test = &prog_test_defs[i];
> @@ -673,6 +692,7 @@ int main(int argc, char **argv)
> test->error_cnt ? "FAIL" : "OK");
>
> reset_affinity();
> + restore_netns();
> if (test->need_cgroup_cleanup)
> cleanup_cgroup_environment();
> }
> @@ -686,6 +706,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);
>
> return env.fail_cnt ? EXIT_FAILURE : EXIT_SUCCESS;
> }
> diff --git a/tools/testing/selftests/bpf/test_progs.h b/tools/testing/selftests/bpf/test_progs.h
> index f4503c926aca..b80924603918 100644
> --- a/tools/testing/selftests/bpf/test_progs.h
> +++ b/tools/testing/selftests/bpf/test_progs.h
> @@ -78,6 +78,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;
> --
> 2.24.1
>
Powered by blists - more mailing lists