Re: [PATCH bpf-next v3 14/15] selftests/bpf: Test attaching struct_ops to a cgroup

"Emil Tsalapatis" <[email protected]> Fri, 31 Jul 2026 17:35:47 -0400
Newsgroups org.kernel.vger.bpf,org.kernel.vger.netdev
Message-ID <[email protected]>
On Mon Jul 6, 2026 at 1:19 PM EDT, Amery Hung wrote:
> From: Martin KaFai Lau <[email protected]>
>
> Exercise attaching the bpf_tcp_ops struct_ops to cgroups via the generic
> cgroup link infrastructure. The struct_ops instances record their
> execution order and the previous return value to validate correctness.
>
> Subtests:
> - query:        BPF_F_QUERY_EFFECTIVE and attached query return the maps
> - order:        BPF_F_PREORDER vs attach order within a cgroup
> - before_after: BPF_F_BEFORE/BPF_F_AFTER relative positioning
> - update:       bpf_link__update_map swaps a link's map, keeping its slot
> - retval:       int return value chained across timeout_init progs of
>                 multiple bpf_tcp_ops attached to a cgroup
> - hierarchy:    parent and child attachments merge in the child's
>                 effective array (descendant before ancestor)
> - inherit:      a child created after the attach inherits the parent's
>                 prog
>
> Signed-off-by: Martin KaFai Lau <[email protected]>
> Signed-off-by: Amery Hung <[email protected]>

Reviewed-by: Emil Tsalapatis <[email protected]>

> ---
>  .../selftests/bpf/prog_tests/bpf_tcp_ops.c    | 560 ++++++++++++++++++
>  .../testing/selftests/bpf/progs/bpf_tcp_ops.c | 141 +++++
>  2 files changed, 701 insertions(+)
>  create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_tcp_ops.c
>  create mode 100644 tools/testing/selftests/bpf/progs/bpf_tcp_ops.c
>

<SNIP>

> +static void test_query(int cgroup_fd, struct bpf_tcp_ops *skel)
> +{
> +	struct bpf_map_info info = {};
> +	__u32 info_len = sizeof(info);
> +	LIBBPF_OPTS(bpf_prog_query_opts, query_opts);
> +	struct bpf_link *link1 = NULL, *link2 = NULL;
> +	__u32 map1_id, map2_id, map_ids[2] = {};
> +	__s32 type_id;
> +
> +	type_id = get_bpf_tcp_ops_type_id();
> +	if (type_id <= 0)
> +		return;
> +
> +	bpf_map_get_info_by_fd(bpf_map__fd(skel->maps.tcp_ops1), &info, &info_len);
> +	map1_id = info.id;
> +
> +	bpf_map_get_info_by_fd(bpf_map__fd(skel->maps.tcp_ops2), &info, &info_len);
> +	map2_id = info.id;
> +

Maybe attach with BPF_F_BEFORE to make sure queries for attachment and effetive order
are different?


> +	link1 = bpf_map__attach_cgroup_opts(skel->maps.tcp_ops1, cgroup_fd, NULL);
> +	if (!ASSERT_OK_PTR(link1, "attach_ops1"))
> +		goto done;
> +
> +	link2 = bpf_map__attach_cgroup_opts(skel->maps.tcp_ops2, cgroup_fd, NULL);
> +	if (!ASSERT_OK_PTR(link2, "attach_ops2"))
> +		goto done;

<SNIP>