aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lib/bpf/libbpf.c
diff options
context:
space:
mode:
authorMartin KaFai Lau <kafai@fb.com>2017-09-27 17:37:54 -0400
committerDavid S. Miller <davem@davemloft.net>2017-09-29 01:17:05 -0400
commit88cda1c9da02c8aa31e1d5dcf22e8a35cc8c19f2 (patch)
tree5096ddd73981e33a2164606461a45b56a189889c /tools/lib/bpf/libbpf.c
parentad5b177bd73f5107d97c36f56395c4281fb6f089 (diff)
bpf: libbpf: Provide basic API support to specify BPF obj name
This patch extends the libbpf to provide API support to allow specifying BPF object name. In tools/lib/bpf/libbpf, the C symbol of the function and the map is used. Regarding section name, all maps are under the same section named "maps". Hence, section name is not a good choice for map's name. To be consistent with map, bpf_prog also follows and uses its function symbol as the prog's name. This patch adds logic to collect function's symbols in libbpf. There is existing codes to collect the map's symbols and no change is needed. The bpf_load_program_name() and bpf_map_create_name() are added to take the name argument. For the other bpf_map_create_xxx() variants, a name argument is directly added to them. In samples/bpf, bpf_load.c in particular, the symbol is also used as the map's name and the map symbols has already been collected in the existing code. For bpf_prog, bpf_load.c does not collect the function symbol name. We can consider to collect them later if there is a need to continue supporting the bpf_load.c. Signed-off-by: Martin KaFai Lau <kafai@fb.com> Acked-by: Alexei Starovoitov <ast@fb.com> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'tools/lib/bpf/libbpf.c')
-rw-r--r--tools/lib/bpf/libbpf.c109
1 files changed, 83 insertions, 26 deletions
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 35f6dfcdc565..4f402dcdf372 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -171,6 +171,7 @@ int libbpf_strerror(int err, char *buf, size_t size)
171struct bpf_program { 171struct bpf_program {
172 /* Index in elf obj file, for relocation use. */ 172 /* Index in elf obj file, for relocation use. */
173 int idx; 173 int idx;
174 char *name;
174 char *section_name; 175 char *section_name;
175 struct bpf_insn *insns; 176 struct bpf_insn *insns;
176 size_t insns_cnt; 177 size_t insns_cnt;
@@ -283,6 +284,7 @@ static void bpf_program__exit(struct bpf_program *prog)
283 prog->clear_priv = NULL; 284 prog->clear_priv = NULL;
284 285
285 bpf_program__unload(prog); 286 bpf_program__unload(prog);
287 zfree(&prog->name);
286 zfree(&prog->section_name); 288 zfree(&prog->section_name);
287 zfree(&prog->insns); 289 zfree(&prog->insns);
288 zfree(&prog->reloc_desc); 290 zfree(&prog->reloc_desc);
@@ -293,26 +295,27 @@ static void bpf_program__exit(struct bpf_program *prog)
293} 295}
294 296
295static int 297static int
296bpf_program__init(void *data, size_t size, char *name, int idx, 298bpf_program__init(void *data, size_t size, char *section_name, int idx,
297 struct bpf_program *prog) 299 struct bpf_program *prog)
298{ 300{
299 if (size < sizeof(struct bpf_insn)) { 301 if (size < sizeof(struct bpf_insn)) {
300 pr_warning("corrupted section '%s'\n", name); 302 pr_warning("corrupted section '%s'\n", section_name);
301 return -EINVAL; 303 return -EINVAL;
302 } 304 }
303 305
304 bzero(prog, sizeof(*prog)); 306 bzero(prog, sizeof(*prog));
305 307
306 prog->section_name = strdup(name); 308 prog->section_name = strdup(section_name);
307 if (!prog->section_name) { 309 if (!prog->section_name) {
308 pr_warning("failed to alloc name for prog %s\n", 310 pr_warning("failed to alloc name for prog under section %s\n",
309 name); 311 section_name);
310 goto errout; 312 goto errout;
311 } 313 }
312 314
313 prog->insns = malloc(size); 315 prog->insns = malloc(size);
314 if (!prog->insns) { 316 if (!prog->insns) {
315 pr_warning("failed to alloc insns for %s\n", name); 317 pr_warning("failed to alloc insns for prog under section %s\n",
318 section_name);
316 goto errout; 319 goto errout;
317 } 320 }
318 prog->insns_cnt = size / sizeof(struct bpf_insn); 321 prog->insns_cnt = size / sizeof(struct bpf_insn);
@@ -331,12 +334,12 @@ errout:
331 334
332static int 335static int
333bpf_object__add_program(struct bpf_object *obj, void *data, size_t size, 336bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
334 char *name, int idx) 337 char *section_name, int idx)
335{ 338{
336 struct bpf_program prog, *progs; 339 struct bpf_program prog, *progs;
337 int nr_progs, err; 340 int nr_progs, err;
338 341
339 err = bpf_program__init(data, size, name, idx, &prog); 342 err = bpf_program__init(data, size, section_name, idx, &prog);
340 if (err) 343 if (err)
341 return err; 344 return err;
342 345
@@ -350,8 +353,8 @@ bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
350 * is still valid, so don't need special treat for 353 * is still valid, so don't need special treat for
351 * bpf_close_object(). 354 * bpf_close_object().
352 */ 355 */
353 pr_warning("failed to alloc a new program '%s'\n", 356 pr_warning("failed to alloc a new program under section '%s'\n",
354 name); 357 section_name);
355 bpf_program__exit(&prog); 358 bpf_program__exit(&prog);
356 return -ENOMEM; 359 return -ENOMEM;
357 } 360 }
@@ -364,6 +367,54 @@ bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
364 return 0; 367 return 0;
365} 368}
366 369
370static int
371bpf_object__init_prog_names(struct bpf_object *obj)
372{
373 Elf_Data *symbols = obj->efile.symbols;
374 struct bpf_program *prog;
375 size_t pi, si;
376
377 for (pi = 0; pi < obj->nr_programs; pi++) {
378 char *name = NULL;
379
380 prog = &obj->programs[pi];
381
382 for (si = 0; si < symbols->d_size / sizeof(GElf_Sym) && !name;
383 si++) {
384 GElf_Sym sym;
385
386 if (!gelf_getsym(symbols, si, &sym))
387 continue;
388 if (sym.st_shndx != prog->idx)
389 continue;
390
391 name = elf_strptr(obj->efile.elf,
392 obj->efile.strtabidx,
393 sym.st_name);
394 if (!name) {
395 pr_warning("failed to get sym name string for prog %s\n",
396 prog->section_name);
397 return -LIBBPF_ERRNO__LIBELF;
398 }
399 }
400
401 if (!name) {
402 pr_warning("failed to find sym for prog %s\n",
403 prog->section_name);
404 return -EINVAL;
405 }
406
407 prog->name = strdup(name);
408 if (!prog->name) {
409 pr_warning("failed to allocate memory for prog sym %s\n",
410 name);
411 return -ENOMEM;
412 }
413 }
414
415 return 0;
416}
417
367static struct bpf_object *bpf_object__new(const char *path, 418static struct bpf_object *bpf_object__new(const char *path,
368 void *obj_buf, 419 void *obj_buf,
369 size_t obj_buf_sz) 420 size_t obj_buf_sz)
@@ -766,8 +817,12 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
766 pr_warning("Corrupted ELF file: index of strtab invalid\n"); 817 pr_warning("Corrupted ELF file: index of strtab invalid\n");
767 return LIBBPF_ERRNO__FORMAT; 818 return LIBBPF_ERRNO__FORMAT;
768 } 819 }
769 if (obj->efile.maps_shndx >= 0) 820 if (obj->efile.maps_shndx >= 0) {
770 err = bpf_object__init_maps(obj); 821 err = bpf_object__init_maps(obj);
822 if (err)
823 goto out;
824 }
825 err = bpf_object__init_prog_names(obj);
771out: 826out:
772 return err; 827 return err;
773} 828}
@@ -870,11 +925,12 @@ bpf_object__create_maps(struct bpf_object *obj)
870 struct bpf_map_def *def = &obj->maps[i].def; 925 struct bpf_map_def *def = &obj->maps[i].def;
871 int *pfd = &obj->maps[i].fd; 926 int *pfd = &obj->maps[i].fd;
872 927
873 *pfd = bpf_create_map(def->type, 928 *pfd = bpf_create_map_name(def->type,
874 def->key_size, 929 obj->maps[i].name,
875 def->value_size, 930 def->key_size,
876 def->max_entries, 931 def->value_size,
877 0); 932 def->max_entries,
933 0);
878 if (*pfd < 0) { 934 if (*pfd < 0) {
879 size_t j; 935 size_t j;
880 int err = *pfd; 936 int err = *pfd;
@@ -982,7 +1038,7 @@ static int bpf_object__collect_reloc(struct bpf_object *obj)
982} 1038}
983 1039
984static int 1040static int
985load_program(enum bpf_prog_type type, struct bpf_insn *insns, 1041load_program(enum bpf_prog_type type, const char *name, struct bpf_insn *insns,
986 int insns_cnt, char *license, u32 kern_version, int *pfd) 1042 int insns_cnt, char *license, u32 kern_version, int *pfd)
987{ 1043{
988 int ret; 1044 int ret;
@@ -995,8 +1051,8 @@ load_program(enum bpf_prog_type type, struct bpf_insn *insns,
995 if (!log_buf) 1051 if (!log_buf)
996 pr_warning("Alloc log buffer for bpf loader error, continue without log\n"); 1052 pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
997 1053
998 ret = bpf_load_program(type, insns, insns_cnt, license, 1054 ret = bpf_load_program_name(type, name, insns, insns_cnt, license,
999 kern_version, log_buf, BPF_LOG_BUF_SIZE); 1055 kern_version, log_buf, BPF_LOG_BUF_SIZE);
1000 1056
1001 if (ret >= 0) { 1057 if (ret >= 0) {
1002 *pfd = ret; 1058 *pfd = ret;
@@ -1021,9 +1077,9 @@ load_program(enum bpf_prog_type type, struct bpf_insn *insns,
1021 if (type != BPF_PROG_TYPE_KPROBE) { 1077 if (type != BPF_PROG_TYPE_KPROBE) {
1022 int fd; 1078 int fd;
1023 1079
1024 fd = bpf_load_program(BPF_PROG_TYPE_KPROBE, insns, 1080 fd = bpf_load_program_name(BPF_PROG_TYPE_KPROBE, name,
1025 insns_cnt, license, kern_version, 1081 insns, insns_cnt, license,
1026 NULL, 0); 1082 kern_version, NULL, 0);
1027 if (fd >= 0) { 1083 if (fd >= 0) {
1028 close(fd); 1084 close(fd);
1029 ret = -LIBBPF_ERRNO__PROGTYPE; 1085 ret = -LIBBPF_ERRNO__PROGTYPE;
@@ -1067,8 +1123,8 @@ bpf_program__load(struct bpf_program *prog,
1067 pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n", 1123 pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
1068 prog->section_name, prog->instances.nr); 1124 prog->section_name, prog->instances.nr);
1069 } 1125 }
1070 err = load_program(prog->type, prog->insns, prog->insns_cnt, 1126 err = load_program(prog->type, prog->name, prog->insns,
1071 license, kern_version, &fd); 1127 prog->insns_cnt, license, kern_version, &fd);
1072 if (!err) 1128 if (!err)
1073 prog->instances.fds[0] = fd; 1129 prog->instances.fds[0] = fd;
1074 goto out; 1130 goto out;
@@ -1096,7 +1152,8 @@ bpf_program__load(struct bpf_program *prog,
1096 continue; 1152 continue;
1097 } 1153 }
1098 1154
1099 err = load_program(prog->type, result.new_insn_ptr, 1155 err = load_program(prog->type, prog->name,
1156 result.new_insn_ptr,
1100 result.new_insn_cnt, 1157 result.new_insn_cnt,
1101 license, kern_version, &fd); 1158 license, kern_version, &fd);
1102 1159