Commit 0b3d1efa authored by Wang Nan's avatar Wang Nan Committed by Arnaldo Carvalho de Melo

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: default avatarWang Nan <wangnan0@huawei.com>
Acked-by: default avatarAlexei 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.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent cb1e5e96
...@@ -81,6 +81,9 @@ void libbpf_set_print(libbpf_print_fn_t warn, ...@@ -81,6 +81,9 @@ void libbpf_set_print(libbpf_print_fn_t warn,
struct bpf_object { struct bpf_object {
char license[64]; char license[64];
u32 kern_version; u32 kern_version;
void *maps_buf;
size_t maps_buf_sz;
/* /*
* Information when doing elf related work. Only valid if fd * Information when doing elf related work. Only valid if fd
* is valid. * is valid.
...@@ -250,6 +253,28 @@ bpf_object__init_kversion(struct bpf_object *obj, ...@@ -250,6 +253,28 @@ bpf_object__init_kversion(struct bpf_object *obj,
return 0; return 0;
} }
static int
bpf_object__init_maps(struct bpf_object *obj, void *data,
size_t size)
{
if (size == 0) {
pr_debug("%s doesn't need map definition\n",
obj->path);
return 0;
}
obj->maps_buf = malloc(size);
if (!obj->maps_buf) {
pr_warning("malloc maps failed: %s\n", obj->path);
return -ENOMEM;
}
obj->maps_buf_sz = size;
memcpy(obj->maps_buf, data, size);
pr_debug("maps in %s: %ld bytes\n", obj->path, (long)size);
return 0;
}
static int bpf_object__elf_collect(struct bpf_object *obj) static int bpf_object__elf_collect(struct bpf_object *obj)
{ {
Elf *elf = obj->efile.elf; Elf *elf = obj->efile.elf;
...@@ -305,6 +330,9 @@ static int bpf_object__elf_collect(struct bpf_object *obj) ...@@ -305,6 +330,9 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
err = bpf_object__init_kversion(obj, err = bpf_object__init_kversion(obj,
data->d_buf, data->d_buf,
data->d_size); data->d_size);
else if (strcmp(name, "maps") == 0)
err = bpf_object__init_maps(obj, data->d_buf,
data->d_size);
if (err) if (err)
goto out; goto out;
} }
...@@ -382,5 +410,6 @@ void bpf_object__close(struct bpf_object *obj) ...@@ -382,5 +410,6 @@ void bpf_object__close(struct bpf_object *obj)
bpf_object__elf_finish(obj); bpf_object__elf_finish(obj);
zfree(&obj->maps_buf);
free(obj); free(obj);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment