BPF List
 help / color / mirror / Atom feed
From: Yonghong Song <yhs@fb.com>
To: Lorenz Bauer <lmb@cloudflare.com>, <ast@kernel.org>,
	<daniel@iogearbox.net>, <jakub@cloudflare.com>,
	<john.fastabend@gmail.com>, <kafai@fb.com>
Cc: <bpf@vger.kernel.org>, <kernel-team@cloudflare.com>
Subject: Re: [PATCH bpf-next v5 3/3] selftests: bpf: Test iterating a sockmap
Date: Thu, 10 Sep 2020 11:11:16 -0700	[thread overview]
Message-ID: <45e403c2-223d-3f29-e6ec-7ad71f5893d8@fb.com> (raw)
In-Reply-To: <20200909162712.221874-4-lmb@cloudflare.com>



On 9/9/20 9:27 AM, Lorenz Bauer wrote:
> Add a test that exercises a basic sockmap / sockhash iteration. For
> now we simply count the number of elements seen. Once sockmap update
> from iterators works we can extend this to perform a full copy.
> 
> Signed-off-by: Lorenz Bauer <lmb@cloudflare.com>
> ---
>   .../selftests/bpf/prog_tests/sockmap_basic.c  | 89 +++++++++++++++++++
>   tools/testing/selftests/bpf/progs/bpf_iter.h  |  9 ++
>   .../selftests/bpf/progs/bpf_iter_sockmap.c    | 43 +++++++++
>   .../selftests/bpf/progs/bpf_iter_sockmap.h    |  3 +
>   4 files changed, 144 insertions(+)
>   create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_sockmap.c
>   create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_sockmap.h
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c b/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c
> index 0b79d78b98db..3215f4d22720 100644
> --- a/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c
> +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c
> @@ -6,6 +6,9 @@
>   #include "test_skmsg_load_helpers.skel.h"
>   #include "test_sockmap_update.skel.h"
>   #include "test_sockmap_invalid_update.skel.h"
> +#include "bpf_iter_sockmap.skel.h"
> +
> +#include "progs/bpf_iter_sockmap.h"
>   
>   #define TCP_REPAIR		19	/* TCP sock is under repair right now */
>   
> @@ -171,6 +174,88 @@ static void test_sockmap_invalid_update(void)
>   		test_sockmap_invalid_update__destroy(skel);
>   }
>   
> +static void test_sockmap_iter(enum bpf_map_type map_type)
> +{
> +	DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
> +	int err, len, src_fd, iter_fd, duration;
> +	union bpf_iter_link_info linfo = {0};
> +	__s64 sock_fd[SOCKMAP_MAX_ENTRIES];
> +	__u32 i, num_sockets, max_elems;
> +	struct bpf_iter_sockmap *skel;
> +	struct bpf_link *link;
> +	struct bpf_map *src;
> +	char buf[64];
> +
> +	skel = bpf_iter_sockmap__open_and_load();
> +	if (CHECK(!skel, "bpf_iter_sockmap__open_and_load", "skeleton open_and_load failed\n"))
> +		return;
> +
> +	for (i = 0; i < ARRAY_SIZE(sock_fd); i++)
> +		sock_fd[i] = -1;
> +
> +	/* Make sure we have at least one "empty" entry to test iteration of
> +	 * an empty slot.
> +	 */
> +	num_sockets = ARRAY_SIZE(sock_fd) - 1;
> +
> +	if (map_type == BPF_MAP_TYPE_SOCKMAP) {
> +		src = skel->maps.sockmap;
> +		max_elems = bpf_map__max_entries(src);
> +	} else {
> +		src = skel->maps.sockhash;
> +		max_elems = num_sockets;
> +	}

I know you include the shared header progs/bpf_iter_sockmap.h to
supply SOCKMAP_MAX_ENTRIES in order to define sock_fd array.

I think it is easier to understand if just using bpf_map__max_entries() 
for both sockmap and sockhash to get max_elems and do dynamic allocation 
for sock_fd. WDYT?

> +
> +	src_fd = bpf_map__fd(src);
> +
> +	for (i = 0; i < num_sockets; i++) {
> +		sock_fd[i] = connected_socket_v4();
> +		if (CHECK(sock_fd[i] == -1, "connected_socket_v4", "cannot connect\n"))
> +			goto out;
> +
> +		err = bpf_map_update_elem(src_fd, &i, &sock_fd[i], BPF_NOEXIST);
> +		if (CHECK(err, "map_update", "failed: %s\n", strerror(errno)))
> +			goto out;
> +	}
> +
[...]

  reply	other threads:[~2020-09-10 18:12 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-09 16:27 [PATCH bpf-next v5 0/3] Sockmap iterator Lorenz Bauer
2020-09-09 16:27 ` [PATCH bpf-next v5 1/3] net: sockmap: Remove unnecessary sk_fullsock checks Lorenz Bauer
2020-09-09 16:27 ` [PATCH bpf-next v5 2/3] net: Allow iterating sockmap and sockhash Lorenz Bauer
2020-09-10 17:40   ` Yonghong Song
2020-09-09 16:27 ` [PATCH bpf-next v5 3/3] selftests: bpf: Test iterating a sockmap Lorenz Bauer
2020-09-10 18:11   ` Yonghong Song [this message]
2020-09-10 19:36     ` Alexei Starovoitov

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=45e403c2-223d-3f29-e6ec-7ad71f5893d8@fb.com \
    --to=yhs@fb.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=jakub@cloudflare.com \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=kernel-team@cloudflare.com \
    --cc=lmb@cloudflare.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