aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWang Nan <wangnan0@huawei.com>2015-06-30 22:13:58 -0400
committerArnaldo Carvalho de Melo <acme@redhat.com>2015-08-07 09:16:57 -0400
commit0b3d1efade1b7e2ccaf79c40a7481c93cfb5090a (patch)
tree135507a1793f5aa48f55fddda6b7964af4b1f796
parentcb1e5e961991ee9b2cbfd3bf06ef490ea578cd2f (diff)
bpf tools: Collect map definitions from 'maps' section
If maps are used by eBPF programs, corresponding object file(s) should contain a section named 'map'. Which contains map definitions. This patch copies the data of the whole section. Map data parsing should be acted just before map loading. Signed-off-by: Wang Nan <wangnan0@huawei.com> Acked-by: Alexei Starovoitov <ast@plumgrid.com> Cc: Brendan Gregg <brendan.d.gregg@gmail.com> Cc: Daniel Borkmann <daniel@iogearbox.net> Cc: David Ahern <dsahern@gmail.com> Cc: He Kuang <hekuang@huawei.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Kaixu Xia <xiakaixu@huawei.com> Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Zefan Li <lizefan@huawei.com> Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/1435716878-189507-11-git-send-email-wangnan0@huawei.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
-rw-r--r--tools/lib/bpf/libbpf.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 95c8d8e5c350..87f5054a9efb 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -81,6 +81,9 @@ void libbpf_set_print(libbpf_print_fn_t warn,
81struct bpf_object { 81struct bpf_object {
82 char license[64]; 82 char license[64];
83 u32 kern_version; 83 u32 kern_version;
84 void *maps_buf;
85 size_t maps_buf_sz;
86
84 /* 87 /*
85 * Information when doing elf related work. Only valid if fd 88 * Information when doing elf related work. Only valid if fd
86 * is valid. 89 * is valid.
@@ -250,6 +253,28 @@ bpf_object__init_kversion(struct bpf_object *obj,
250 return 0; 253 return 0;
251} 254}
252 255
256static int
257bpf_object__init_maps(struct bpf_object *obj, void *data,
258 size_t size)
259{
260 if (size == 0) {
261 pr_debug("%s doesn't need map definition\n",
262 obj->path);
263 return 0;
264 }
265
266 obj->maps_buf = malloc(size);
267 if (!obj->maps_buf) {
268 pr_warning("malloc maps failed: %s\n", obj->path);
269 return -ENOMEM;
270 }
271
272 obj->maps_buf_sz = size;
273 memcpy(obj->maps_buf, data, size);
274 pr_debug("maps in %s: %ld bytes\n", obj->path, (long)size);
275 return 0;
276}
277
253static int bpf_object__elf_collect(struct bpf_object *obj) 278static int bpf_object__elf_collect(struct bpf_object *obj)
254{ 279{
255 Elf *elf = obj->efile.elf; 280 Elf *elf = obj->efile.elf;
@@ -305,6 +330,9 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
305 err = bpf_object__init_kversion(obj, 330 err = bpf_object__init_kversion(obj,
306 data->d_buf, 331 data->d_buf,
307 data->d_size); 332 data->d_size);
333 else if (strcmp(name, "maps") == 0)
334 err = bpf_object__init_maps(obj, data->d_buf,
335 data->d_size);
308 if (err) 336 if (err)
309 goto out; 337 goto out;
310 } 338 }
@@ -382,5 +410,6 @@ void bpf_object__close(struct bpf_object *obj)
382 410
383 bpf_object__elf_finish(obj); 411 bpf_object__elf_finish(obj);
384 412
413 zfree(&obj->maps_buf);
385 free(obj); 414 free(obj);
386} 415}