From: Song Liu <song@kernel.org>
To: Xu Liu <liuxu623@gmail.com>
Cc: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Martin KaFai Lau <kafai@fb.com>, Song Liu <songliubraving@fb.com>,
Yonghong Song <yhs@fb.com>,
John Fastabend <john.fastabend@gmail.com>,
KP Singh <kpsingh@kernel.org>,
"David S . Miller" <davem@davemloft.net>,
Jakub Kicinski <kuba@kernel.org>,
Networking <netdev@vger.kernel.org>, bpf <bpf@vger.kernel.org>,
open list <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH bpf-next v2 2/2] selftests/bpf: Test for get_netns_cookie
Date: Wed, 18 Aug 2021 10:48:15 -0700 [thread overview]
Message-ID: <CAPhsuW7KmO0ErC3_KySMzag1bnbJAEAmXgOQS9jd8-scFai=tg@mail.gmail.com> (raw)
In-Reply-To: <20210818105820.91894-3-liuxu623@gmail.com>
On Wed, Aug 18, 2021 at 4:00 AM Xu Liu <liuxu623@gmail.com> wrote:
>
> Add test to use get_netns_cookie() from BPF_PROG_TYPE_SOCK_OPS.
>
> Signed-off-by: Xu Liu <liuxu623@gmail.com>
Acked-by: Song Liu <songliubraving@fb.com>
> ---
> .../selftests/bpf/prog_tests/netns_cookie.c | 61 +++++++++++++++++++
> .../selftests/bpf/progs/netns_cookie_prog.c | 39 ++++++++++++
> 2 files changed, 100 insertions(+)
> create mode 100644 tools/testing/selftests/bpf/prog_tests/netns_cookie.c
> create mode 100644 tools/testing/selftests/bpf/progs/netns_cookie_prog.c
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/netns_cookie.c b/tools/testing/selftests/bpf/prog_tests/netns_cookie.c
> new file mode 100644
> index 000000000000..6f3cd472fb65
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/netns_cookie.c
> @@ -0,0 +1,61 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#include <test_progs.h>
> +#include "netns_cookie_prog.skel.h"
> +#include "network_helpers.h"
> +
> +#ifndef SO_NETNS_COOKIE
> +#define SO_NETNS_COOKIE 71
> +#endif
> +
> +static int duration;
> +
> +void test_netns_cookie(void)
> +{
> + int server_fd = 0, client_fd = 0, cgroup_fd = 0, err = 0, val = 0;
> + struct netns_cookie_prog *skel;
> + uint64_t cookie_expected_value;
> + socklen_t vallen = sizeof(cookie_expected_value);
> +
> + skel = netns_cookie_prog__open_and_load();
> + if (!ASSERT_OK_PTR(skel, "skel_open"))
> + return;
> +
> + cgroup_fd = test__join_cgroup("/netns_cookie");
> + if (CHECK(cgroup_fd < 0, "join_cgroup", "cgroup creation failed\n"))
> + goto out;
> +
> + skel->links.get_netns_cookie_sockops = bpf_program__attach_cgroup(
> + skel->progs.get_netns_cookie_sockops, cgroup_fd);
> + if (!ASSERT_OK_PTR(skel->links.get_netns_cookie_sockops, "prog_attach"))
> + goto close_cgroup_fd;
> +
> + server_fd = start_server(AF_INET6, SOCK_STREAM, "::1", 0, 0);
> + if (CHECK(server_fd < 0, "start_server", "errno %d\n", errno))
> + goto close_cgroup_fd;
> +
> + client_fd = connect_to_fd(server_fd, 0);
> + if (CHECK(client_fd < 0, "connect_to_fd", "errno %d\n", errno))
> + goto close_server_fd;
> +
> + err = bpf_map_lookup_elem(bpf_map__fd(skel->maps.netns_cookies),
> + &client_fd, &val);
> + if (!ASSERT_OK(err, "map_lookup(socket_cookies)"))
> + goto close_client_fd;
> +
> + err = getsockopt(client_fd, SOL_SOCKET, SO_NETNS_COOKIE,
> + &cookie_expected_value, &vallen);
> + if (!ASSERT_OK(err, "getsockopt)"))
> + goto close_client_fd;
> +
> + ASSERT_EQ(val, cookie_expected_value, "cookie_value");
> +
> +close_client_fd:
> + close(client_fd);
> +close_server_fd:
> + close(server_fd);
> +close_cgroup_fd:
> + close(cgroup_fd);
> +out:
> + netns_cookie_prog__destroy(skel);
> +}
> diff --git a/tools/testing/selftests/bpf/progs/netns_cookie_prog.c b/tools/testing/selftests/bpf/progs/netns_cookie_prog.c
> new file mode 100644
> index 000000000000..4ed8d75aa299
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/netns_cookie_prog.c
> @@ -0,0 +1,39 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#include "vmlinux.h"
> +
> +#include <bpf/bpf_helpers.h>
> +
> +#define AF_INET6 10
> +
> +struct {
> + __uint(type, BPF_MAP_TYPE_SK_STORAGE);
> + __uint(map_flags, BPF_F_NO_PREALLOC);
> + __type(key, int);
> + __type(value, int);
> +} netns_cookies SEC(".maps");
> +
> +SEC("sockops")
> +int get_netns_cookie_sockops(struct bpf_sock_ops *ctx)
> +{
> + struct bpf_sock *sk = ctx->sk;
> + int *cookie;
> +
> + if (ctx->family != AF_INET6)
> + return 1;
> +
> + if (ctx->op != BPF_SOCK_OPS_TCP_CONNECT_CB)
> + return 1;
> +
> + if (!sk)
> + return 1;
> +
> + cookie = bpf_sk_storage_get(&netns_cookies, sk, 0,
> + BPF_SK_STORAGE_GET_F_CREATE);
> + if (!cookie)
> + return 1;
> +
> + *cookie = bpf_get_netns_cookie(ctx);
> +
> + return 1;
> +}
> --
> 2.28.0
>
next prev parent reply other threads:[~2021-08-18 17:49 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-08-18 10:58 [PATCH bpf-next v2 0/2] bpf: Allow bpf_get_netns_cookie in BPF_PROG_TYPE_SOCK_OPS Xu Liu
2021-08-18 10:58 ` [PATCH bpf-next v2 1/2] " Xu Liu
2021-08-18 17:49 ` Song Liu
2021-08-18 10:58 ` [PATCH bpf-next v2 2/2] selftests/bpf: Test for get_netns_cookie Xu Liu
2021-08-18 17:48 ` Song Liu [this message]
2021-08-18 22:40 ` [PATCH bpf-next v2 0/2] bpf: Allow bpf_get_netns_cookie in BPF_PROG_TYPE_SOCK_OPS patchwork-bot+netdevbpf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAPhsuW7KmO0ErC3_KySMzag1bnbJAEAmXgOQS9jd8-scFai=tg@mail.gmail.com' \
--to=song@kernel.org \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=john.fastabend@gmail.com \
--cc=kafai@fb.com \
--cc=kpsingh@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=liuxu623@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=songliubraving@fb.com \
--cc=yhs@fb.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).