Re: [PATCH bpf-next] libbpf: add bpf_object__token_fd accessor

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Sep 12, 2024 at 2:49 PM Ihor Solodrai <ihor.solodrai@xxxxx> wrote:
>
> Add a LIBBPF_API function to retrieve the token_fd from a bpf_object.
>
> Without this accessor, if user needs a token FD they have to get it
> manually via bpf_token_create, even though a token might have been
> already created by bpf_object_load.

bpf_object__load() is a public API, let's use its name in the commit.

>
> Suggested-by: Andrii Nakryiko <andrii@xxxxxxxxxx>
> Signed-off-by: Ihor Solodrai <ihor.solodrai@xxxxx>
> ---
>  tools/lib/bpf/libbpf.c   | 5 +++++
>  tools/lib/bpf/libbpf.h   | 2 ++
>  tools/lib/bpf/libbpf.map | 1 +
>  3 files changed, 8 insertions(+)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 84d4ec0e1f60..e9ac950ce2ff 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -9059,6 +9059,11 @@ unsigned int bpf_object__kversion(const struct bpf_object *obj)
>         return obj ? obj->kern_version : 0;
>  }
>

Can you please add a little bit of doc comment, see other APIs that do
have it for an example and syntax

> +int bpf_object__token_fd(const struct bpf_object *obj)
> +{
> +       return obj ? obj->token_fd : -1;

we should assume obj is valid and non-NULL, but also token_fd will be
>= 3 *after the load, if it's at all created*, so I think we should do
something like this:

return obj->token_fd ?: -1;

Other than that, it looks good.

pw-bot: cr


> +}
> +
>  struct btf *bpf_object__btf(const struct bpf_object *obj)
>  {
>         return obj ? obj->btf : NULL;
> diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
> index 6917653ef9fa..5cd143e83f95 100644
> --- a/tools/lib/bpf/libbpf.h
> +++ b/tools/lib/bpf/libbpf.h
> @@ -294,6 +294,8 @@ LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj);
>  LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);
>  LIBBPF_API int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version);
>
> +LIBBPF_API int bpf_object__token_fd(const struct bpf_object *obj);
> +
>  struct btf;
>  LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);
>  LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
> diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
> index 8f0d9ea3b1b4..0096e483f7eb 100644
> --- a/tools/lib/bpf/libbpf.map
> +++ b/tools/lib/bpf/libbpf.map
> @@ -423,6 +423,7 @@ LIBBPF_1.5.0 {
>                 btf__relocate;
>                 bpf_map__autoattach;
>                 bpf_map__set_autoattach;
> +               bpf_object__token_fd;
>                 bpf_program__attach_sockmap;
>                 ring__consume_n;
>                 ring_buffer__consume_n;
> --
> 2.34.1
>
>





[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux