Add opts-based variants of the bpf_*_get_next_id() functions which allow passing in a token fd to add to the bpf() syscall. Signed-off-by: Toke Høiland-Jørgensen --- tools/lib/bpf/bpf.c | 43 +++++++++++++++++++++++++++++++++++++------ tools/lib/bpf/bpf.h | 16 ++++++++++++++++ tools/lib/bpf/libbpf.map | 4 ++++ 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index 38dcb31e8c0e..773efb585bca 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -1149,14 +1149,21 @@ int bpf_prog_test_run_opts(int prog_fd, struct bpf_test_run_opts *opts) return libbpf_err_errno(ret); } -static int bpf_obj_get_next_id(__u32 start_id, __u32 *next_id, int cmd) +static int bpf_obj_get_next_id(__u32 start_id, __u32 *next_id, int cmd, + const struct bpf_get_next_id_opts *opts) { - const size_t attr_sz = offsetofend(union bpf_attr, open_flags); + const size_t attr_sz = offsetofend(union bpf_attr, get_next_id_token_fd); union bpf_attr attr; int err; + if (!OPTS_VALID(opts, bpf_get_next_id_opts)) + return libbpf_err(-EINVAL); + memset(&attr, 0, attr_sz); attr.start_id = start_id; + attr.get_next_id_token_fd = OPTS_GET(opts, token_fd, 0); + if (attr.get_next_id_token_fd) + attr.open_flags = BPF_F_TOKEN_FD; err = sys_bpf(cmd, &attr, attr_sz); if (!err) @@ -1167,22 +1174,46 @@ static int bpf_obj_get_next_id(__u32 start_id, __u32 *next_id, int cmd) int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id) { - return bpf_obj_get_next_id(start_id, next_id, BPF_PROG_GET_NEXT_ID); + return bpf_obj_get_next_id(start_id, next_id, BPF_PROG_GET_NEXT_ID, NULL); +} + +int bpf_prog_get_next_id_opts(__u32 start_id, __u32 *next_id, + const struct bpf_get_next_id_opts *opts) +{ + return bpf_obj_get_next_id(start_id, next_id, BPF_PROG_GET_NEXT_ID, opts); } int bpf_map_get_next_id(__u32 start_id, __u32 *next_id) { - return bpf_obj_get_next_id(start_id, next_id, BPF_MAP_GET_NEXT_ID); + return bpf_obj_get_next_id(start_id, next_id, BPF_MAP_GET_NEXT_ID, NULL); +} + +int bpf_map_get_next_id_opts(__u32 start_id, __u32 *next_id, + const struct bpf_get_next_id_opts *opts) +{ + return bpf_obj_get_next_id(start_id, next_id, BPF_MAP_GET_NEXT_ID, opts); } int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id) { - return bpf_obj_get_next_id(start_id, next_id, BPF_BTF_GET_NEXT_ID); + return bpf_obj_get_next_id(start_id, next_id, BPF_BTF_GET_NEXT_ID, NULL); +} + +int bpf_btf_get_next_id_opts(__u32 start_id, __u32 *next_id, + const struct bpf_get_next_id_opts *opts) +{ + return bpf_obj_get_next_id(start_id, next_id, BPF_BTF_GET_NEXT_ID, opts); } int bpf_link_get_next_id(__u32 start_id, __u32 *next_id) { - return bpf_obj_get_next_id(start_id, next_id, BPF_LINK_GET_NEXT_ID); + return bpf_obj_get_next_id(start_id, next_id, BPF_LINK_GET_NEXT_ID, NULL); +} + +int bpf_link_get_next_id_opts(__u32 start_id, __u32 *next_id, + const struct bpf_get_next_id_opts *opts) +{ + return bpf_obj_get_next_id(start_id, next_id, BPF_LINK_GET_NEXT_ID, opts); } int bpf_prog_get_fd_by_id_opts(__u32 id, diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h index fe3b7fd8512b..cb5104734710 100644 --- a/tools/lib/bpf/bpf.h +++ b/tools/lib/bpf/bpf.h @@ -524,6 +524,22 @@ LIBBPF_API int bpf_map_get_next_id(__u32 start_id, __u32 *next_id); LIBBPF_API int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id); LIBBPF_API int bpf_link_get_next_id(__u32 start_id, __u32 *next_id); +struct bpf_get_next_id_opts { + size_t sz; /* size of this struct for forward/backward compatibility */ + __u32 token_fd; + size_t :0; +}; +#define bpf_get_next_id_opts__last_field token_fd + +LIBBPF_API int bpf_prog_get_next_id_opts(__u32 start_id, __u32 *next_id, + const struct bpf_get_next_id_opts *opts); +LIBBPF_API int bpf_map_get_next_id_opts(__u32 start_id, __u32 *next_id, + const struct bpf_get_next_id_opts *opts); +LIBBPF_API int bpf_btf_get_next_id_opts(__u32 start_id, __u32 *next_id, + const struct bpf_get_next_id_opts *opts); +LIBBPF_API int bpf_link_get_next_id_opts(__u32 start_id, __u32 *next_id, + const struct bpf_get_next_id_opts *opts); + struct bpf_get_fd_by_id_opts { size_t sz; /* size of this struct for forward/backward compatibility */ __u32 open_flags; /* permissions requested for the operation on fd */ diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index a45666f546fa..4a87b3500741 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -476,6 +476,10 @@ LIBBPF_1.8.0 { LIBBPF_1.9.0 { global: + bpf_btf_get_next_id_opts; + bpf_link_get_next_id_opts; + bpf_map_get_next_id_opts; + bpf_prog_get_next_id_opts; bpf_program__load; bpf_program__load_strategy; bpf_program__set_load_strategy; -- 2.56.1