aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lib/bpf
diff options
context:
space:
mode:
authorJakub Kicinski <jakub.kicinski@netronome.com>2018-07-10 17:42:59 -0400
committerDaniel Borkmann <daniel@iogearbox.net>2018-07-11 16:13:33 -0400
commitb60df2a0e11fcd24186c312b0307ab8494031e27 (patch)
tree46028b7d5950e0aaa76e1de07aef751ef64a359a /tools/lib/bpf
parentba6dd679a3e81af023ec091c2fb7c82003a27316 (diff)
tools: libbpf: expose the prog type guessing from section name logic
libbpf can guess program type based on ELF section names. As libbpf becomes more popular its association between section name strings and types becomes more of a standard. Allow libbpf users to use the same logic for matching strings to types, e.g. when the string originates from command line. Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com> Reviewed-by: Quentin Monnet <quentin.monnet@netronome.com> Acked-by: Andrey Ignatov <rdna@fb.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Diffstat (limited to 'tools/lib/bpf')
-rw-r--r--tools/lib/bpf/libbpf.c43
-rw-r--r--tools/lib/bpf/libbpf.h3
2 files changed, 28 insertions, 18 deletions
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 38ed3e92e393..42f31eff5f3f 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -2081,23 +2081,31 @@ static const struct {
2081#undef BPF_S_PROG_SEC 2081#undef BPF_S_PROG_SEC
2082#undef BPF_SA_PROG_SEC 2082#undef BPF_SA_PROG_SEC
2083 2083
2084static int bpf_program__identify_section(struct bpf_program *prog) 2084int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
2085 enum bpf_attach_type *expected_attach_type)
2085{ 2086{
2086 int i; 2087 int i;
2087 2088
2088 if (!prog->section_name) 2089 if (!name)
2089 goto err; 2090 return -EINVAL;
2090
2091 for (i = 0; i < ARRAY_SIZE(section_names); i++)
2092 if (strncmp(prog->section_name, section_names[i].sec,
2093 section_names[i].len) == 0)
2094 return i;
2095 2091
2096err: 2092 for (i = 0; i < ARRAY_SIZE(section_names); i++) {
2097 pr_warning("failed to guess program type based on section name %s\n", 2093 if (strncmp(name, section_names[i].sec, section_names[i].len))
2098 prog->section_name); 2094 continue;
2095 *prog_type = section_names[i].prog_type;
2096 *expected_attach_type = section_names[i].expected_attach_type;
2097 return 0;
2098 }
2099 return -EINVAL;
2100}
2099 2101
2100 return -1; 2102static int
2103bpf_program__identify_section(struct bpf_program *prog,
2104 enum bpf_prog_type *prog_type,
2105 enum bpf_attach_type *expected_attach_type)
2106{
2107 return libbpf_prog_type_by_name(prog->section_name, prog_type,
2108 expected_attach_type);
2101} 2109}
2102 2110
2103int bpf_map__fd(struct bpf_map *map) 2111int bpf_map__fd(struct bpf_map *map)
@@ -2230,7 +2238,6 @@ int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
2230 enum bpf_prog_type prog_type; 2238 enum bpf_prog_type prog_type;
2231 struct bpf_object *obj; 2239 struct bpf_object *obj;
2232 struct bpf_map *map; 2240 struct bpf_map *map;
2233 int section_idx;
2234 int err; 2241 int err;
2235 2242
2236 if (!attr) 2243 if (!attr)
@@ -2252,14 +2259,14 @@ int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
2252 prog->prog_ifindex = attr->ifindex; 2259 prog->prog_ifindex = attr->ifindex;
2253 expected_attach_type = attr->expected_attach_type; 2260 expected_attach_type = attr->expected_attach_type;
2254 if (prog_type == BPF_PROG_TYPE_UNSPEC) { 2261 if (prog_type == BPF_PROG_TYPE_UNSPEC) {
2255 section_idx = bpf_program__identify_section(prog); 2262 err = bpf_program__identify_section(prog, &prog_type,
2256 if (section_idx < 0) { 2263 &expected_attach_type);
2264 if (err < 0) {
2265 pr_warning("failed to guess program type based on section name %s\n",
2266 prog->section_name);
2257 bpf_object__close(obj); 2267 bpf_object__close(obj);
2258 return -EINVAL; 2268 return -EINVAL;
2259 } 2269 }
2260 prog_type = section_names[section_idx].prog_type;
2261 expected_attach_type =
2262 section_names[section_idx].expected_attach_type;
2263 } 2270 }
2264 2271
2265 bpf_program__set_type(prog, prog_type); 2272 bpf_program__set_type(prog, prog_type);
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 564f4be9bae0..d1ce5c828e2e 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -92,6 +92,9 @@ int bpf_object__set_priv(struct bpf_object *obj, void *priv,
92 bpf_object_clear_priv_t clear_priv); 92 bpf_object_clear_priv_t clear_priv);
93void *bpf_object__priv(struct bpf_object *prog); 93void *bpf_object__priv(struct bpf_object *prog);
94 94
95int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
96 enum bpf_attach_type *expected_attach_type);
97
95/* Accessors of bpf_program */ 98/* Accessors of bpf_program */
96struct bpf_program; 99struct bpf_program;
97struct bpf_program *bpf_program__next(struct bpf_program *prog, 100struct bpf_program *bpf_program__next(struct bpf_program *prog,