BPF List
 help / color / mirror / Atom feed
From: "Emil Tsalapatis" <emil@etsalapatis.com>
To: "Amery Hung" <ameryhung@gmail.com>, <bpf@vger.kernel.org>
Cc: <netdev@vger.kernel.org>, <alexei.starovoitov@gmail.com>,
	<andrii@kernel.org>, <daniel@iogearbox.net>, <eddyz87@gmail.com>,
	<memxor@gmail.com>, <martin.lau@kernel.org>,
	<shakeel.butt@linux.dev>, <roman.gushchin@linux.dev>,
	<kuniyu@google.com>, <kerneljasonxing@gmail.com>,
	<kernel-team@meta.com>
Subject: Re: [PATCH bpf-next v3 15/15] selftests/bpf: Add test for bpf_tcp_ops header option hooks
Date: Fri, 31 Jul 2026 18:01:28 -0400	[thread overview]
Message-ID: <DKD3FNK5CT7H.3CCFT5539KE2F@etsalapatis.com> (raw)
In-Reply-To: <20260706171918.317102-16-ameryhung@gmail.com>

On Mon Jul 6, 2026 at 1:19 PM EDT, Amery Hung wrote:
> Add a test exercising the bpf_tcp_ops parse_hdr, hdr_opt_len and
> write_hdr_opt members together with the header option helpers.
>
> The struct_ops program (progs/bpf_tcp_ops_hdr.c) reserves space in
> hdr_opt_len via bpf_reserve_hdr_opt(), writes an experimental option in
> write_hdr_opt via bpf_store_hdr_opt(), and recovers it in parse_hdr via
> bpf_load_hdr_opt() on the incoming skb. Each hook bumps a counter and the
> parse hook records the option payload, so the three callbacks and all
> three overloaded helpers are covered.
>
> Signed-off-by: Amery Hung <ameryhung@gmail.com>
> ---

Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com>

Since the struct_ops methods are wrapping the existing sockops code I
don't think we need more extensive tests (e.g., trying to exhaust 
all available space with reserve).

>  .../bpf/prog_tests/bpf_tcp_ops_hdr.c          | 77 +++++++++++++++++
>  .../selftests/bpf/progs/bpf_tcp_ops_hdr.c     | 83 +++++++++++++++++++
>  2 files changed, 160 insertions(+)
>  create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_tcp_ops_hdr.c
>  create mode 100644 tools/testing/selftests/bpf/progs/bpf_tcp_ops_hdr.c
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ops_hdr.c b/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ops_hdr.c
> new file mode 100644
> index 000000000000..244605e69e40
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ops_hdr.c
> @@ -0,0 +1,77 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
> +
> +#include <test_progs.h>
> +#include <network_helpers.h>
> +#include "cgroup_helpers.h"
> +#include "bpf_tcp_ops_hdr.skel.h"
> +
> +#define CGROUP_PATH	"/bpf_tcp_ops_hdr"
> +#define TEST_NETNS	"bpf_tcp_ops_hdr"
> +
> +#define TEST_OPT_D0	0xAB
> +#define TEST_OPT_D1	0xCD
> +
> +static void run_hdr_opt(void)
> +{
> +	struct bpf_tcp_ops_hdr *skel = NULL;
> +	struct bpf_link *link = NULL;
> +	struct netns_obj *ns = NULL;
> +	int cgroup_fd, lfd = -1, fd = -1;
> +
> +	cgroup_fd = test__join_cgroup(CGROUP_PATH);
> +	if (!ASSERT_GE(cgroup_fd, 0, "join_cgroup"))
> +		return;
> +
> +	ns = netns_new(TEST_NETNS, true);
> +	if (!ASSERT_OK_PTR(ns, "netns_new"))
> +		goto done;
> +
> +	skel = bpf_tcp_ops_hdr__open_and_load();
> +	if (!ASSERT_OK_PTR(skel, "open_and_load"))
> +		goto done;
> +
> +	link = bpf_map__attach_cgroup_opts(skel->maps.test_hdr_ops, cgroup_fd, NULL);
> +	if (!ASSERT_OK_PTR(link, "attach_cgroup"))
> +		goto done;
> +
> +	/*
> +	 * One direction of data is enough to exercise all hooks: both peers
> +	 * share the cgroup struct_ops, so the sender runs hdr_opt_len/write
> +	 * and the receiver runs parse.
> +	 */
> +	lfd = start_server(AF_INET6, SOCK_STREAM, "::1", 0, 0);
> +	if (!ASSERT_GE(lfd, 0, "start_server"))
> +		goto done;
> +
> +	fd = connect_to_fd(lfd, 0);
> +	if (!ASSERT_OK_FD(fd, "connect_to_fd"))
> +		goto done;
> +
> +	if (!ASSERT_OK(send_recv_data(lfd, fd, 64), "send_recv_data"))
> +		goto done;
> +
> +	/* Reserve + write hooks ran while sending. */
> +	ASSERT_GT(skel->bss->hdr_opt_len_cnt, 0, "hdr_opt_len_cnt");
> +	ASSERT_GT(skel->bss->write_cnt, 0, "write_cnt");
> +	/* Parse hook ran and recovered our option on the receive side. */
> +	ASSERT_GT(skel->bss->parse_cnt, 0, "parse_cnt");
> +	ASSERT_GT(skel->bss->found_cnt, 0, "found_cnt");
> +	ASSERT_EQ(skel->bss->found_d0, TEST_OPT_D0, "found_d0");
> +	ASSERT_EQ(skel->bss->found_d1, TEST_OPT_D1, "found_d1");
> +
> +done:
> +	if (fd >= 0)
> +		close(fd);
> +	if (lfd >= 0)
> +		close(lfd);
> +	bpf_link__destroy(link);
> +	bpf_tcp_ops_hdr__destroy(skel);
> +	netns_free(ns);
> +	close(cgroup_fd);
> +}
> +
> +void test_bpf_tcp_ops_hdr(void)
> +{
> +	run_hdr_opt();
> +}
> diff --git a/tools/testing/selftests/bpf/progs/bpf_tcp_ops_hdr.c b/tools/testing/selftests/bpf/progs/bpf_tcp_ops_hdr.c
> new file mode 100644
> index 000000000000..46618a604d96
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/bpf_tcp_ops_hdr.c
> @@ -0,0 +1,83 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
> +
> +#include "vmlinux.h"
> +#include <bpf/bpf_helpers.h>
> +#include <bpf/bpf_tracing.h>
> +
> +/* Experimental option kind and payload written/parsed by this test. */
> +#define TEST_OPT_KIND	0xFD
> +#define TEST_OPT_LEN	4
> +#define TEST_OPT_D0	0xAB
> +#define TEST_OPT_D1	0xCD
> +
> +int hdr_opt_len_cnt;
> +int write_cnt;
> +int parse_cnt;
> +int found_cnt;
> +__u8 found_d0;
> +__u8 found_d1;
> +
> +SEC("struct_ops")
> +void BPF_PROG(test_hdr_opt_len, struct sock *sk, struct sk_buff *skb,
> +	      struct request_sock *req, struct sk_buff *syn_skb,
> +	      enum tcp_synack_type synack_type, unsigned int *remaining)
> +{
> +	hdr_opt_len_cnt++;
> +
> +	/* Reserve TEST_OPT_LEN bytes; the helper decrements *remaining. Stacks
> +	 * with other progs in the cgroup hierarchy.
> +	 */
> +	bpf_reserve_hdr_opt(ctx, TEST_OPT_LEN, 0);
> +}
> +
> +SEC("struct_ops")
> +void BPF_PROG(test_write_hdr_opt, struct sock *sk, struct sk_buff *skb,
> +	      struct request_sock *req, struct sk_buff *syn_skb,
> +	      enum tcp_synack_type synack_type, __u32 opt_off)
> +{
> +	__u8 opt[TEST_OPT_LEN] = {
> +		TEST_OPT_KIND, TEST_OPT_LEN, TEST_OPT_D0, TEST_OPT_D1,
> +	};
> +
> +	/* bpf_store_hdr_opt() takes the program ctx (the kernel reads the
> +	 * outgoing skb from it); it appends after any options already written
> +	 * in the reserved window, rejects duplicates, and confines the write to
> +	 * the header option scratch. Stacks across progs in the cgroup hierarchy.
> +	 */
> +	if (bpf_store_hdr_opt(ctx, opt, sizeof(opt), 0))
> +		return;
> +
> +	write_cnt++;
> +}
> +
> +SEC("struct_ops")
> +void BPF_PROG(test_parse_hdr, struct sock *sk, struct sk_buff *skb)
> +{
> +	__u8 opt[TEST_OPT_LEN] = {
> +		TEST_OPT_KIND, TEST_OPT_LEN, TEST_OPT_D0, TEST_OPT_D1,
> +	};
> +
> +	parse_cnt++;
> +
> +	/* Look up the experimental option written by test_write_hdr_opt() in
> +	 * the incoming skb. For an experimental kind the search matches on the
> +	 * 2-byte magic in opt[2..3]; on a match the found option is copied back
> +	 * into opt[].
> +	 */
> +	if (bpf_load_hdr_opt(ctx, opt, sizeof(opt), 0) < 0)
> +		return;
> +
> +	found_d0 = opt[2];
> +	found_d1 = opt[3];
> +	found_cnt++;
> +}
> +
> +SEC(".struct_ops.link")
> +struct bpf_tcp_ops test_hdr_ops = {
> +	.hdr_opt_len	= (void *)test_hdr_opt_len,
> +	.write_hdr_opt	= (void *)test_write_hdr_opt,
> +	.parse_hdr	= (void *)test_parse_hdr,
> +};
> +
> +char _license[] SEC("license") = "GPL";


      parent reply	other threads:[~2026-07-31 22:01 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-06 17:19 [PATCH bpf-next v3 00/15] bpf: A common way to attach struct_ops to a cgroup Amery Hung
2026-07-06 17:19 ` [PATCH bpf-next v3 01/15] bpf: Remove __rcu tagging in st_link->map Amery Hung
2026-07-13 19:02   ` Emil Tsalapatis
2026-07-06 17:19 ` [PATCH bpf-next v3 02/15] bpf: Make struct_ops tasks_rcu grace period optional Amery Hung
2026-07-06 17:44   ` sashiko-bot
2026-07-13 19:01   ` Emil Tsalapatis
2026-07-06 17:19 ` [PATCH bpf-next v3 03/15] bpf: Add bpf_struct_ops accessor helpers Amery Hung
2026-07-06 17:34   ` sashiko-bot
2026-07-06 18:16     ` Amery Hung
2026-07-13 20:36   ` Emil Tsalapatis
2026-07-06 17:19 ` [PATCH bpf-next v3 04/15] bpf: Remove unnecessary prog_list_prog() check Amery Hung
2026-07-13 20:35   ` Emil Tsalapatis
2026-07-06 17:19 ` [PATCH bpf-next v3 05/15] bpf: Replace prog_list_prog() check with direct pl->prog and pl->link check Amery Hung
2026-07-13 21:27   ` Emil Tsalapatis
2026-07-06 17:19 ` [PATCH bpf-next v3 06/15] bpf: Add prog_list_init_item(), prog_list_replace_item(), and prog_list_id() Amery Hung
2026-07-13 21:56   ` Emil Tsalapatis
2026-07-06 17:19 ` [PATCH bpf-next v3 07/15] bpf: Move LSM trampoline unlink into bpf_cgroup_link_auto_detach() Amery Hung
2026-07-13 21:57   ` Emil Tsalapatis
2026-07-06 17:19 ` [PATCH bpf-next v3 08/15] bpf: Add a few bpf_cgroup_array_* helper functions Amery Hung
2026-07-13 21:57   ` Emil Tsalapatis
2026-07-06 17:19 ` [PATCH bpf-next v3 09/15] bpf: Add infrastructure to support attaching struct_ops to cgroups Amery Hung
2026-07-06 17:47   ` sashiko-bot
2026-07-14  6:21   ` Emil Tsalapatis
2026-07-06 17:19 ` [PATCH bpf-next v3 10/15] bpf: Allow all struct_ops to use bpf_dynptr_from_skb() Amery Hung
2026-07-14  6:23   ` Emil Tsalapatis
2026-07-06 17:19 ` [PATCH bpf-next v3 11/15] bpf: tcp: Support selected sock_ops callbacks as struct_ops Amery Hung
2026-07-06 18:28   ` bot+bpf-ci
2026-07-06 23:11     ` Amery Hung
2026-07-31 21:11   ` Emil Tsalapatis
2026-07-06 17:19 ` [PATCH bpf-next v3 12/15] bpf: tcp: Support parse/len/write header option hooks in bpf_tcp_ops Amery Hung
2026-07-06 17:45   ` sashiko-bot
2026-07-06 22:31     ` Amery Hung
2026-07-31 22:19   ` Emil Tsalapatis
2026-07-06 17:19 ` [PATCH bpf-next v3 13/15] libbpf: Support attaching struct_ops to a cgroup Amery Hung
2026-07-14  6:48   ` Emil Tsalapatis
2026-07-06 17:19 ` [PATCH bpf-next v3 14/15] selftests/bpf: Test " Amery Hung
2026-07-31 21:35   ` Emil Tsalapatis
2026-07-06 17:19 ` [PATCH bpf-next v3 15/15] selftests/bpf: Add test for bpf_tcp_ops header option hooks Amery Hung
2026-07-06 17:47   ` sashiko-bot
2026-07-06 18:18     ` Amery Hung
2026-07-31 22:01   ` Emil Tsalapatis [this message]

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=DKD3FNK5CT7H.3CCFT5539KE2F@etsalapatis.com \
    --to=emil@etsalapatis.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=ameryhung@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=kernel-team@meta.com \
    --cc=kerneljasonxing@gmail.com \
    --cc=kuniyu@google.com \
    --cc=martin.lau@kernel.org \
    --cc=memxor@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=roman.gushchin@linux.dev \
    --cc=shakeel.butt@linux.dev \
    /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