[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20190117010115.18234-2-maciejromanfijalkowski@gmail.com>
Date: Thu, 17 Jan 2019 02:01:10 +0100
From: Maciej Fijalkowski <maciejromanfijalkowski@...il.com>
To: daniel@...earbox.net, ast@...nel.org
Cc: netdev@...r.kernel.org, jakub.kicinski@...ronome.com,
brouer@...hat.com
Subject: [PATCH bpf-next 1/6] libbpf: Add a helper for retrieving a map fd for a given name
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@...ronome.com>
Signed-off-by: Maciej Fijalkowski <maciejromanfijalkowski@...il.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@...ronome.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;
}
+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;
--
2.16.1
Powered by blists - more mailing lists