diff mbox series

[bpf-next,v2,1/8] libbpf: Add a helper for retrieving a map fd for a given name

Message ID 20190121091041.14666-2-maciejromanfijalkowski@gmail.com
State Changes Requested
Delegated to: BPF Maintainers
Headers show
Series xdp: Avoid unloading xdp prog not attached by sample | expand

Commit Message

Maciej Fijalkowski Jan. 21, 2019, 9:10 a.m. UTC
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 <jakub.kicinski@netronome.com>
Signed-off-by: Maciej Fijalkowski <maciejromanfijalkowski@gmail.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
---
 tools/lib/bpf/libbpf.c   | 12 ++++++++++++
 tools/lib/bpf/libbpf.h   |  3 +++
 tools/lib/bpf/libbpf.map |  4 ++++
 3 files changed, 19 insertions(+)

Comments

Daniel Borkmann Jan. 23, 2019, 10:54 a.m. UTC | #1
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 <jakub.kicinski@netronome.com>
> Signed-off-by: Maciej Fijalkowski <maciejromanfijalkowski@gmail.com>
> Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
> ---
>  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;
>
Maciej Fijalkowski Jan. 23, 2019, 2:03 p.m. UTC | #2
On Wed, 23 Jan 2019 11:54:41 +0100
Daniel Borkmann <daniel@iogearbox.net> wrote:

> 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 <jakub.kicinski@netronome.com>
> > Signed-off-by: Maciej Fijalkowski <maciejromanfijalkowski@gmail.com>
> > Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
> > ---
> >  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));
> }
>

Yes of course, I will send v3 once we figure out what's the best way to supply
the prog section name for xdp_redirect_cpu.

> > +}
> > +
> >  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;
> >   
>
diff mbox series

Patch

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;
 }
 
+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;
+}
+
 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;