On 01/21/2019 10:10 AM, Maciej Fijalkowski wrote:
> XDP samples are mostly cooperating with eBPF maps through their file
> descriptors. In case of a eBPF program that contains multiple maps it
> might be tiresome to iterate through them and call bpf_map__fd for each
> one. Add a helper mostly based on bpf_object__find_map_by_name, but
> instead of returning the struct bpf_map pointer, return map fd.
>
> Bump libbpf ABI version to 0.0.2.
>
> Suggested-by: Jakub Kicinski <[email protected]>
> Signed-off-by: Maciej Fijalkowski <[email protected]>
> Reviewed-by: Jakub Kicinski <[email protected]>
> ---
> tools/lib/bpf/libbpf.c | 12 ++++++++++++
> tools/lib/bpf/libbpf.h | 3 +++
> tools/lib/bpf/libbpf.map | 4 ++++
> 3 files changed, 19 insertions(+)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 169e347c76f6..dc838bea403f 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -2840,6 +2840,18 @@ bpf_object__find_map_by_name(struct bpf_object *obj,
> const char *name)
> return NULL;
> }
Application could just do: bpf_map__fd(bpf_object__find_map_by_name(...)) or
bpf_object__find_map_by_name(...)->fd as both are exposed via library, though
I guess it may be okay to have a helper for it as it feels this might be needed
in many cases.
> +int
> +bpf_object__find_map_fd_by_name(struct bpf_object *obj, const char *name)
> +{
> + struct bpf_map *pos;
> +
> + bpf_map__for_each(pos, obj) {
> + if (pos->name && !strcmp(pos->name, name))
> + return bpf_map__fd(pos);
> + }
> + return -ENOENT;
Can we instead just do:
int
bpf_object__find_map_fd_by_name(struct bpf_object *obj, const char *name)
{
return bpf_map__fd(bpf_object__find_map_by_name(obj, name));
}
> +}
> +
> struct bpf_map *
> bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
> {
> diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
> index 5f68d7b75215..7f10d36abdde 100644
> --- a/tools/lib/bpf/libbpf.h
> +++ b/tools/lib/bpf/libbpf.h
> @@ -264,6 +264,9 @@ struct bpf_map;
> LIBBPF_API struct bpf_map *
> bpf_object__find_map_by_name(struct bpf_object *obj, const char *name);
>
> +LIBBPF_API int
> +bpf_object__find_map_fd_by_name(struct bpf_object *obj, const char *name);
> +
> /*
> * Get bpf_map through the offset of corresponding struct bpf_map_def
> * in the BPF object file.
> diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
> index cd02cd4e2cc3..7c59e4f64082 100644
> --- a/tools/lib/bpf/libbpf.map
> +++ b/tools/lib/bpf/libbpf.map
> @@ -124,3 +124,7 @@ LIBBPF_0.0.1 {
> local:
> *;
> };
> +LIBBPF_0.0.2 {
> + global:
> + bpf_object__find_map_fd_by_name;
> +} LIBBPF_0.0.1;
>