Skip to content

bpf: Implement mprog API on top of existing cgroup progs #8917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

kernel-patches-daemon-bpf[bot]
Copy link

Pull request for series with
subject: bpf: Implement mprog API on top of existing cgroup progs
version: 2
url: https://patchwork.kernel.org/project/netdevbpf/list/?series=961050

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: b69d441
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=961050
version: 2

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: 32c563d
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=961050
version: 2

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: 0f2d39f
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=961050
version: 2

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: cf15cdc
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=961050
version: 2

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: cb4a119
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=961050
version: 2

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: 5a8cb23
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=961050
version: 2

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: c8ce7db
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=961050
version: 2

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: 7220eab
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=961050
version: 2

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: 149e0cf
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=961050
version: 2

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: 1cb0f56
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=963862
version: 3

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: b615ce5
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=963862
version: 3

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: b615ce5
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=963862
version: 3

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: 25b6d5d
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=963862
version: 3

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: 4e2e684
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=963862
version: 3

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: d90f0bc
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=963862
version: 3

Yonghong Song added 5 commits May 22, 2025 09:35
One of key items in mprog API is revision for prog list. The revision
number will be increased if the prog list changed, e.g., attach, detach
or replace.

Add 'revisions' field to struct cgroup_bpf, representing revisions for
all cgroup related attachment types. The initial revision value is
set to 1, the same as kernel mprog implementations.

Acked-by: Andrii Nakryiko <[email protected]>
Signed-off-by: Yonghong Song <[email protected]>
Current cgroup prog ordering is appending at attachment time. This is not
ideal. In some cases, users want specific ordering at a particular cgroup
level. To address this, the existing mprog API seems an ideal solution with
supporting BPF_F_BEFORE and BPF_F_AFTER flags.

But there are a few obstacles to directly use kernel mprog interface.
Currently cgroup bpf progs already support prog attach/detach/replace
and link-based attach/detach/replace. For example, in struct
bpf_prog_array_item, the cgroup_storage field needs to be together
with bpf prog. But the mprog API struct bpf_mprog_fp only has bpf_prog
as the member, which makes it difficult to use kernel mprog interface.

In another case, the current cgroup prog detach tries to use the
same flag as in attach. This is different from mprog kernel interface
which uses flags passed from user space.

So to avoid modifying existing behavior, I made the following changes to
support mprog API for cgroup progs:
 - The support is for prog list at cgroup level. Cross-level prog list
   (a.k.a. effective prog list) is not supported.
 - Previously, BPF_F_PREORDER is supported only for prog attach, now
   BPF_F_PREORDER is also supported by link-based attach.
 - For attach, BPF_F_BEFORE/BPF_F_AFTER/BPF_F_ID/BPF_F_LINK is supported
   similar to kernel mprog but with different implementation.
 - For detach and replace, use the existing implementation.
 - For attach, detach and replace, the revision for a particular prog
   list, associated with a particular attach type, will be updated
   by increasing count by 1.

Signed-off-by: Yonghong Song <[email protected]>
Currently libbpf supports bpf_program__attach_cgroup() with signature:
  LIBBPF_API struct bpf_link *
  bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd);

To support mprog style attachment, additionsl fields like flags,
relative_{fd,id} and expected_revision are needed.

Add a new API:
  LIBBPF_API struct bpf_link *
  bpf_program__attach_cgroup_opts(const struct bpf_program *prog, int cgroup_fd,
                                  const struct bpf_cgroup_opts *opts);
where bpf_cgroup_opts contains all above needed fields.

Acked-by: Andrii Nakryiko <[email protected]>
Signed-off-by: Yonghong Song <[email protected]>
Move static inline functions id_from_prog_fd() and id_from_link_fd()
from prog_tests/tc_helpers.h to test_progs.h so these two functions
can be reused for later cgroup mprog selftests.

Signed-off-by: Yonghong Song <[email protected]>
Two tests are added:
  - cgroup_mprog_opts, which mimics tc_opts.c ([1]). Both prog and link
    attach are tested. Some negative tests are also included.
  - cgroup_mprog_ordering, which actually runs the program with some mprog
    API flags.

  [1] https://github.com/torvalds/linux/blob/master/tools/testing/selftests/bpf/prog_tests/tc_opts.c

Signed-off-by: Yonghong Song <[email protected]>
@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: 5ead949
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=963862
version: 3

@kernel-patches-daemon-bpf kernel-patches-daemon-bpf bot force-pushed the series/952282=>bpf-next branch from 5c35a15 to 510e1e5 Compare May 22, 2025 16:35
@kernel-patches-daemon-bpf
Copy link
Author

At least one diff in series https://patchwork.kernel.org/project/netdevbpf/list/?series=963862 expired. Closing PR.

@kernel-patches-daemon-bpf kernel-patches-daemon-bpf bot deleted the series/952282=>bpf-next branch May 25, 2025 03:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants