Skip to content

Commit 583c900

Browse files
rgushchinborkmann
authored andcommitted
libbpf: add ability to guess program type based on section name
The bpf_prog_load() function will guess program type if it's not specified explicitly. This functionality will be used to implement loading of different programs without asking a user to specify the program type. In first order it will be used by bpftool. Signed-off-by: Roman Gushchin <[email protected]> Cc: Alexei Starovoitov <[email protected]> Cc: Daniel Borkmann <[email protected]> Cc: Jakub Kicinski <[email protected]> Cc: Martin KaFai Lau <[email protected]> Cc: Quentin Monnet <[email protected]> Cc: David Ahern <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent f4e2298 commit 583c900

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,45 @@ BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT);
17211721
BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP);
17221722
BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT);
17231723

1724+
#define BPF_PROG_SEC(string, type) { string, sizeof(string), type }
1725+
static const struct {
1726+
const char *sec;
1727+
size_t len;
1728+
enum bpf_prog_type prog_type;
1729+
} section_names[] = {
1730+
BPF_PROG_SEC("socket", BPF_PROG_TYPE_SOCKET_FILTER),
1731+
BPF_PROG_SEC("kprobe/", BPF_PROG_TYPE_KPROBE),
1732+
BPF_PROG_SEC("kretprobe/", BPF_PROG_TYPE_KPROBE),
1733+
BPF_PROG_SEC("tracepoint/", BPF_PROG_TYPE_TRACEPOINT),
1734+
BPF_PROG_SEC("xdp", BPF_PROG_TYPE_XDP),
1735+
BPF_PROG_SEC("perf_event", BPF_PROG_TYPE_PERF_EVENT),
1736+
BPF_PROG_SEC("cgroup/skb", BPF_PROG_TYPE_CGROUP_SKB),
1737+
BPF_PROG_SEC("cgroup/sock", BPF_PROG_TYPE_CGROUP_SOCK),
1738+
BPF_PROG_SEC("cgroup/dev", BPF_PROG_TYPE_CGROUP_DEVICE),
1739+
BPF_PROG_SEC("sockops", BPF_PROG_TYPE_SOCK_OPS),
1740+
BPF_PROG_SEC("sk_skb", BPF_PROG_TYPE_SK_SKB),
1741+
};
1742+
#undef BPF_PROG_SEC
1743+
1744+
static enum bpf_prog_type bpf_program__guess_type(struct bpf_program *prog)
1745+
{
1746+
int i;
1747+
1748+
if (!prog->section_name)
1749+
goto err;
1750+
1751+
for (i = 0; i < ARRAY_SIZE(section_names); i++)
1752+
if (strncmp(prog->section_name, section_names[i].sec,
1753+
section_names[i].len) == 0)
1754+
return section_names[i].prog_type;
1755+
1756+
err:
1757+
pr_warning("failed to guess program type based on section name %s\n",
1758+
prog->section_name);
1759+
1760+
return BPF_PROG_TYPE_UNSPEC;
1761+
}
1762+
17241763
int bpf_map__fd(struct bpf_map *map)
17251764
{
17261765
return map ? map->fd : -EINVAL;
@@ -1832,6 +1871,18 @@ int bpf_prog_load(const char *file, enum bpf_prog_type type,
18321871
return -ENOENT;
18331872
}
18341873

1874+
/*
1875+
* If type is not specified, try to guess it based on
1876+
* section name.
1877+
*/
1878+
if (type == BPF_PROG_TYPE_UNSPEC) {
1879+
type = bpf_program__guess_type(prog);
1880+
if (type == BPF_PROG_TYPE_UNSPEC) {
1881+
bpf_object__close(obj);
1882+
return -EINVAL;
1883+
}
1884+
}
1885+
18351886
bpf_program__set_type(prog, type);
18361887
err = bpf_object__load(obj);
18371888
if (err) {

0 commit comments

Comments
 (0)