bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Yonghong Song <yhs@fb.com>
To: Martin KaFai Lau <kafai@fb.com>, <bpf@vger.kernel.org>
Cc: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Eric Dumazet <edumazet@google.com>, <kernel-team@fb.com>,
	Neal Cardwell <ncardwell@google.com>, <netdev@vger.kernel.org>,
	Yuchung Cheng <ycheng@google.com>
Subject: Re: [PATCH bpf-next 8/8] bpf: selftest: Test batching and bpf_setsockopt in bpf tcp iter
Date: Tue, 29 Jun 2021 12:00:53 -0700	[thread overview]
Message-ID: <9a9e81ef-7896-4df7-d3a7-601466d70588@fb.com> (raw)
In-Reply-To: <20210625200536.728323-1-kafai@fb.com>



On 6/25/21 1:05 PM, Martin KaFai Lau wrote:
> This patch adds tests for the batching and bpf_setsockopt in bpf tcp iter.
> 
> It first creates:
> a) 1 non SO_REUSEPORT listener in lhash2.
> b) 256 passive and active fds connected to the listener in (a).
> c) 256 SO_REUSEPORT listeners in one of the lhash2 bucket.
> 
> The test sets all listeners and connections to bpf_cubic before
> running the bpf iter.
> 
> The bpf iter then calls setsockopt(TCP_CONGESTION) to switch
> each listener and connection from bpf_cubic to bpf_dctcp.
> 
> The bpf iter has a random_retry mode such that it can return EAGAIN
> to the usespace in the middle of a batch.
> 
> Signed-off-by: Martin KaFai Lau <kafai@fb.com>
> ---
>   tools/testing/selftests/bpf/network_helpers.c |  85 ++++++-
>   tools/testing/selftests/bpf/network_helpers.h |   4 +
>   .../bpf/prog_tests/bpf_iter_setsockopt.c      | 226 ++++++++++++++++++
>   .../selftests/bpf/progs/bpf_iter_setsockopt.c |  76 ++++++
>   .../selftests/bpf/progs/bpf_tracing_net.h     |   4 +
>   5 files changed, 386 insertions(+), 9 deletions(-)
>   create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_iter_setsockopt.c
>   create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_setsockopt.c
> 
> diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c
> index 2060bc122c53..26468a8f44f3 100644
> --- a/tools/testing/selftests/bpf/network_helpers.c
> +++ b/tools/testing/selftests/bpf/network_helpers.c
> @@ -66,17 +66,13 @@ int settimeo(int fd, int timeout_ms)
>   
>   #define save_errno_close(fd) ({ int __save = errno; close(fd); errno = __save; })
>   
> -int start_server(int family, int type, const char *addr_str, __u16 port,
> -		 int timeout_ms)
> +static int __start_server(int type, const struct sockaddr *addr,
> +			  socklen_t addrlen, int timeout_ms, bool reuseport)
>   {
> -	struct sockaddr_storage addr = {};
> -	socklen_t len;
> +	int on = 1;
>   	int fd;
>   
> -	if (make_sockaddr(family, addr_str, port, &addr, &len))
> -		return -1;
> -
> -	fd = socket(family, type, 0);
> +	fd = socket(addr->sa_family, type, 0);
>   	if (fd < 0) {
>   		log_err("Failed to create server socket");
>   		return -1;
> @@ -85,7 +81,13 @@ int start_server(int family, int type, const char *addr_str, __u16 port,
>   	if (settimeo(fd, timeout_ms))
>   		goto error_close;
>   
> -	if (bind(fd, (const struct sockaddr *)&addr, len) < 0) {
> +	if (reuseport &&
> +	    setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on))) {
> +		log_err("Failed to set SO_REUSEPORT");
> +		return -1;
> +	}
> +
> +	if (bind(fd, addr, addrlen) < 0) {
>   		log_err("Failed to bind socket");
>   		goto error_close;
>   	}
> @@ -104,6 +106,69 @@ int start_server(int family, int type, const char *addr_str, __u16 port,
>   	return -1;
>   }
>   
[...]
> +void test_bpf_iter_setsockopt(void)
> +{
> +	struct bpf_iter_setsockopt *iter_skel = NULL;
> +	struct bpf_cubic *cubic_skel = NULL;
> +	struct bpf_dctcp *dctcp_skel = NULL;
> +	struct bpf_link *cubic_link = NULL;
> +	struct bpf_link *dctcp_link = NULL;
> +
> +	if (create_netns())
> +		return;
> +
> +	/* Load iter_skel */
> +	iter_skel = bpf_iter_setsockopt__open_and_load();
> +	if (!ASSERT_OK_PTR(iter_skel, "iter_skel"))
> +		return;
> +	iter_skel->links.change_tcp_cc = bpf_program__attach_iter(iter_skel->progs.change_tcp_cc, NULL);
> +	if (!ASSERT_OK_PTR(iter_skel->links.change_tcp_cc, "attach iter"))
> +		goto done;
> +
> +	/* Load bpf_cubic */
> +	cubic_skel = bpf_cubic__open_and_load();
> +	if (!ASSERT_OK_PTR(cubic_skel, "cubic_skel"))
> +		goto done;
> +	cubic_link = bpf_map__attach_struct_ops(cubic_skel->maps.cubic);
> +	if (!ASSERT_OK_PTR(cubic_link, "cubic_link"))
> +		goto done;
> +
> +	/* Load bpf_dctcp */
> +	dctcp_skel = bpf_dctcp__open_and_load();
> +	if (!ASSERT_OK_PTR(dctcp_skel, "dctcp_skel"))
> +		goto done;
> +	dctcp_link = bpf_map__attach_struct_ops(dctcp_skel->maps.dctcp);
> +	if (!ASSERT_OK_PTR(dctcp_link, "dctcp_link"))
> +		goto done;
> +
> +	do_bpf_iter_setsockopt(iter_skel, true);
> +	do_bpf_iter_setsockopt(iter_skel, false);
> +
> +done:
> +	bpf_link__destroy(cubic_link);
> +	bpf_link__destroy(dctcp_link);
> +	bpf_cubic__destroy(cubic_skel);
> +	bpf_dctcp__destroy(dctcp_skel);
> +	bpf_iter_setsockopt__destroy(iter_skel);
> +}
> diff --git a/tools/testing/selftests/bpf/progs/bpf_iter_setsockopt.c b/tools/testing/selftests/bpf/progs/bpf_iter_setsockopt.c
> new file mode 100644
> index 000000000000..72cc4c1c681e
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/bpf_iter_setsockopt.c
> @@ -0,0 +1,76 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* Copyright (c) 2021 Facebook */
> +#include "bpf_iter.h"
> +#include "bpf_tracing_net.h"
> +#include <bpf/bpf_helpers.h>
> +#include <bpf/bpf_endian.h>
> +
> +#define sk_num			__sk_common.skc_num
> +#define sk_dport		__sk_common.skc_dport
> +#define sk_state		__sk_common.skc_state
> +#define sk_family		__sk_common.skc_family

The above macros can be defined in bpf_tracing_net.h. For example,
sk_state and sk_family are alraedy defined in bpf_tracing_net.h.

#define sk_family               __sk_common.skc_family
#define sk_rmem_alloc           sk_backlog.rmem_alloc
#define sk_refcnt               __sk_common.skc_refcnt
#define sk_state                __sk_common.skc_state
#define sk_v6_daddr             __sk_common.skc_v6_daddr
#define sk_v6_rcv_saddr         __sk_common.skc_v6_rcv_saddr

> +
> +#define bpf_tcp_sk(skc)	({				\
> +	struct sock_common *_skc = skc;			\
> +	sk = NULL;					\
> +	tp = NULL;					\
> +	if (_skc) {					\
> +		tp = bpf_skc_to_tcp_sock(_skc);		\
> +		sk = (struct sock *)tp;			\
> +	}						\
> +	tp;						\
> +})
[...]
> +
> +char _license[] SEC("license") = "GPL";
> diff --git a/tools/testing/selftests/bpf/progs/bpf_tracing_net.h b/tools/testing/selftests/bpf/progs/bpf_tracing_net.h
> index 01378911252b..d66c4caaeec1 100644
> --- a/tools/testing/selftests/bpf/progs/bpf_tracing_net.h
> +++ b/tools/testing/selftests/bpf/progs/bpf_tracing_net.h
> @@ -5,6 +5,10 @@
>   #define AF_INET			2
>   #define AF_INET6		10
>   
> +#define SOL_TCP			6
> +#define TCP_CONGESTION		13
> +#define TCP_CA_NAME_MAX		16
> +
>   #define ICSK_TIME_RETRANS	1
>   #define ICSK_TIME_PROBE0	3
>   #define ICSK_TIME_LOSS_PROBE	5
> 

  reply	other threads:[~2021-06-29 19:01 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-25 20:04 [PATCH bpf-next 0/8] bpf: Allow bpf tcp iter to do bpf_setsockopt Martin KaFai Lau
2021-06-25 20:04 ` [PATCH bpf-next 1/8] tcp: seq_file: Avoid skipping sk during tcp_seek_last_pos Martin KaFai Lau
2021-06-25 20:04 ` [PATCH bpf-next 2/8] tcp: seq_file: Refactor net and family matching Martin KaFai Lau
2021-06-25 20:05 ` [PATCH bpf-next 3/8] bpf: tcp: seq_file: Remove bpf_seq_afinfo from tcp_iter_state Martin KaFai Lau
2021-06-25 20:05 ` [PATCH bpf-next 4/8] tcp: seq_file: Add listening_get_first() Martin KaFai Lau
2021-06-25 20:05 ` [PATCH bpf-next 5/8] tcp: seq_file: Replace listening_hash with lhash2 Martin KaFai Lau
2021-06-25 20:05 ` [PATCH bpf-next 6/8] bpf: tcp: bpf iter batching and lock_sock Martin KaFai Lau
2021-06-29 17:27   ` Yonghong Song
2021-06-29 17:44     ` Martin KaFai Lau
2021-06-29 17:57       ` Yonghong Song
2021-06-29 18:06         ` Martin KaFai Lau
2021-06-29 18:55           ` Yonghong Song
2021-06-25 20:05 ` [PATCH bpf-next 7/8] bpf: tcp: Support bpf_setsockopt in bpf tcp iter Martin KaFai Lau
2021-06-25 20:05 ` [PATCH bpf-next 8/8] bpf: selftest: Test batching and " Martin KaFai Lau
2021-06-29 19:00   ` Yonghong Song [this message]
2021-06-29 19:04 ` [PATCH bpf-next 0/8] bpf: Allow bpf tcp iter to do bpf_setsockopt Yonghong Song

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=9a9e81ef-7896-4df7-d3a7-601466d70588@fb.com \
    --to=yhs@fb.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=edumazet@google.com \
    --cc=kafai@fb.com \
    --cc=kernel-team@fb.com \
    --cc=ncardwell@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=ycheng@google.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).